
/* ==== Theme variables ==== */
:root {
  /* DARK is default */
  --bg: #0d1117;
  --bg-soft: #161c25;
  --bg-elev: #1d2532;
  --bg-fade: #0d1117;
  --ink: #e8dcc4;
  --ink-soft: #a59885;
  --ink-mute: #6a6256;
  --wine: #d96b7a;
  --wine-hover: #e88494;
  --wine-glow: rgba(217, 107, 122, 0.18);
  --bronze: #d4a85f;
  --rule: #2a3340;
  --rule-soft: #1a2030;
  --paper-overlay: rgba(255, 246, 230, 0.02);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
  --shadow-card: 0 2px 12px rgba(0, 0, 0, 0.25);
  --topbar-bg: rgba(13, 17, 23, 0.88);
}

[data-theme="light"] {
  --bg: #f5ede0;
  --bg-soft: #faf6ec;
  --bg-elev: #ffffff;
  --bg-fade: #f5ede0;
  --ink: #1c1c1c;
  --ink-soft: #4a443c;
  --ink-mute: #8a8275;
  --wine: #6b1e2a;
  --wine-hover: #8a2734;
  --wine-glow: rgba(107, 30, 42, 0.12);
  --bronze: #8a6a3c;
  --rule: #d4c8b0;
  --rule-soft: #e6dcc8;
  --paper-overlay: rgba(0, 0, 0, 0.015);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  --shadow-card: 0 2px 12px rgba(0, 0, 0, 0.06);
  --topbar-bg: rgba(245, 237, 224, 0.92);
}

/* ==== Reset & base ==== */
* { box-sizing: border-box; margin: 0; padding: 0; }

html {
  background: var(--bg);
  scroll-behavior: smooth;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: 'PT Serif', Georgia, serif;
  font-size: 18px;
  line-height: 1.7;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  transition: background-color 0.4s ease, color 0.4s ease;
  position: relative;
  overflow-x: hidden;
}

/* Subtle paper grain overlay */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  opacity: 0.4;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 1  0 0 0 0 0.95  0 0 0 0 0.85  0 0 0 0.5 0'/></filter><rect width='100%' height='100%' filter='url(%23n)' opacity='0.18'/></svg>");
  mix-blend-mode: overlay;
}

a { color: var(--wine); text-decoration: none; transition: color .15s; }
a:hover { color: var(--wine-hover); }

.mono {
  font-family: 'IBM Plex Mono', 'Consolas', monospace;
  font-feature-settings: 'tnum';
}

/* ==== Reading progress bar (top) ==== */
.read-progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  width: 0%;
  background: linear-gradient(90deg, var(--bronze), var(--wine));
  z-index: 100;
  transition: width 80ms linear;
  box-shadow: 0 0 8px var(--wine-glow);
}

/* ==== Theme toggle button ==== */
.theme-toggle {
  position: fixed;
  top: 16px;
  right: 18px;
  z-index: 80;
  width: 38px; height: 38px;
  border: 1px solid var(--rule);
  background: var(--bg-soft);
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink-soft);
  transition: all .25s;
  font-size: 16px;
  line-height: 1;
}
.theme-toggle:hover {
  border-color: var(--wine);
  color: var(--wine);
  transform: rotate(20deg);
}
.theme-toggle svg { width: 18px; height: 18px; }
[data-theme="light"] .theme-toggle .icon-sun { display: none; }
[data-theme="light"] .theme-toggle .icon-moon { display: block; }
.theme-toggle .icon-sun { display: block; }
.theme-toggle .icon-moon { display: none; }

/* Inline mode (lives inside .topbar-tools) */
.theme-toggle.theme-toggle--inbar {
  position: static;
  top: auto; right: auto;
  width: 34px; height: 34px;
  z-index: auto;
}
.theme-toggle.theme-toggle--inbar svg { width: 16px; height: 16px; }

/* ==== Up button ==== */
.up-btn {
  position: fixed;
  bottom: 24px;
  right: 18px;
  z-index: 80;
  width: 44px; height: 44px;
  border: 1px solid var(--rule);
  background: var(--bg-soft);
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink-soft);
  font-size: 18px;
  opacity: 0;
  transform: translateY(20px);
  pointer-events: none;
  transition: all .3s;
  box-shadow: var(--shadow-card);
}
.up-btn.visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}
.up-btn:hover {
  border-color: var(--wine);
  color: var(--wine);
  transform: translateY(-3px);
}

/* ============ INDEX ============ */
.book {
  max-width: 1100px;
  margin: 0 auto;
  padding: 80px 32px 140px;
  position: relative;
  z-index: 1;
}

.book-hero {
  text-align: center;
  padding: 60px 0 80px;
  border-bottom: 1px solid var(--rule);
  margin-bottom: 80px;
  opacity: 0;
  transform: translateY(20px);
  animation: fade-up 1s ease 0.1s forwards;
}
@keyframes fade-up {
  to { opacity: 1; transform: translateY(0); }
}

.book-mark {
  display: inline-block;
  width: 72px;
  height: 72px;
  border: 2px solid var(--wine);
  border-radius: 50%;
  position: relative;
  margin-bottom: 36px;
  animation: spin 90s linear infinite;
  box-shadow: 0 0 40px var(--wine-glow);
}
.book-mark::before {
  content: "";
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 26px; height: 26px;
  background: var(--bg);
  border: 2px solid var(--wine);
  border-radius: 50%;
}
.book-mark::after {
  content: "";
  position: absolute;
  inset: -10px;
  border-radius: 50%;
  border: 1px dashed var(--bronze);
  opacity: 0.3;
  animation: spin 60s linear infinite reverse;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.book-title {
  font-size: 68px;
  font-weight: 400;
  letter-spacing: -0.015em;
  line-height: 1.05;
  margin-bottom: 18px;
  color: var(--ink);
  background: linear-gradient(180deg, var(--ink) 0%, var(--bronze) 130%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.book-subtitle {
  font-style: italic;
  color: var(--ink-soft);
  font-size: 21px;
  max-width: 620px;
  margin: 0 auto 28px;
  line-height: 1.5;
}
.book-meta {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px;
  color: var(--bronze);
  letter-spacing: 0.05em;
  text-transform: lowercase;
}
.book-meta span + span::before {
  content: " · ";
  color: var(--rule);
  margin: 0 6px;
}

/* Overall reading progress */
.overall-progress {
  max-width: 480px;
  margin: 32px auto 0;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.08em;
  color: var(--ink-mute);
}
.overall-progress-bar {
  height: 2px;
  background: var(--rule);
  margin: 8px 0 0;
  border-radius: 1px;
  overflow: hidden;
}
.overall-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--bronze), var(--wine));
  width: 0%;
  transition: width 0.8s cubic-bezier(0.16, 1, 0.3, 1);
}

.epigraph {
  max-width: 560px;
  margin: 0 auto 80px;
  padding: 32px 40px;
  background: var(--bg-soft);
  border-left: 3px solid var(--wine);
  font-style: italic;
  color: var(--ink-soft);
  font-size: 17px;
  border-radius: 2px;
  box-shadow: var(--shadow-card);
  opacity: 0;
  transform: translateY(20px);
  animation: fade-up 1s ease 0.4s forwards;
}
.epigraph p { margin-bottom: 0.6em; }
.epigraph p:last-child { margin-bottom: 0; }

.toc-section-title {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--bronze);
  text-align: center;
  margin-bottom: 48px;
  position: relative;
}
.toc-section-title::before,
.toc-section-title::after {
  content: "";
  display: inline-block;
  width: 36px;
  height: 1px;
  background: var(--rule);
  vertical-align: middle;
  margin: 0 14px;
}

.toc-grid {
  list-style: none;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 28px;
}

.card {
  background: var(--bg-soft);
  border: 1px solid var(--rule);
  border-radius: 4px;
  overflow: hidden;
  display: block;
  color: inherit;
  position: relative;
  transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
  opacity: 0;
  transform: translateY(28px);
}
.card.visible {
  opacity: 1;
  transform: translateY(0);
}
.card:hover {
  border-color: var(--wine);
  transform: translateY(-4px);
  box-shadow: var(--shadow);
}
.card-cover {
  width: 100%;
  aspect-ratio: 1;
  background: var(--bg-elev);
  background-size: cover;
  background-position: center;
  display: block;
  border-bottom: 1px solid var(--rule);
  position: relative;
  overflow: hidden;
  transition: transform 1.6s cubic-bezier(0.16, 1, 0.3, 1);
}
.card:hover .card-cover {
  transform: scale(1.04);
}
.card-cover::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, transparent 60%, rgba(0,0,0,0.35) 100%);
  opacity: 0;
  transition: opacity 0.4s;
  pointer-events: none;
}
[data-theme="light"] .card-cover::after {
  background: linear-gradient(180deg, transparent 60%, rgba(0,0,0,0.18) 100%);
}
.card:hover .card-cover::after { opacity: 1; }

.card-body {
  padding: 22px 26px 28px;
  position: relative;
}
.card-num {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--bronze);
  margin-bottom: 8px;
  display: block;
}
.card-title {
  font-size: 24px;
  font-weight: 400;
  line-height: 1.25;
  color: var(--ink);
  margin-bottom: 6px;
}

/* Read marker */
.card.is-read .card-cover::before {
  content: "";
  position: absolute;
  top: 12px;
  right: 12px;
  width: 24px;
  height: 24px;
  background: var(--bronze);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%230d1117' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'><polyline points='3 8 7 12 13 4'/></svg>");
  background-size: 14px;
  background-repeat: no-repeat;
  background-position: center;
}

/* ============ CHAPTER ============ */
.chapter-cover {
  width: 100%;
  height: 78vh;
  max-height: 780px;
  background-size: cover;
  background-position: center;
  background-color: var(--bg-elev);
  position: relative;
  margin-bottom: 0;
  overflow: hidden;
  will-change: transform;
}
.chapter-cover::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(13,17,23,0.1) 0%, transparent 35%, var(--bg) 100%);
  pointer-events: none;
}
[data-theme="light"] .chapter-cover::after {
  background: linear-gradient(180deg, rgba(245,237,224,0.05) 0%, transparent 35%, var(--bg) 100%);
}

.chapter-shell {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 24px 140px;
  position: relative;
  z-index: 1;
}

.chapter-head {
  text-align: left;
  padding: 60px 0 48px;
  margin-bottom: 32px;
  border-bottom: 1px solid var(--rule);
  opacity: 0;
  animation: fade-up 0.9s ease 0.2s forwards;
}
.chapter-num {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--bronze);
  margin-bottom: 18px;
}
.chapter-title {
  font-size: 48px;
  font-weight: 400;
  line-height: 1.08;
  letter-spacing: -0.012em;
  color: var(--ink);
}

.chapter-body {
  font-size: 19px;
  line-height: 1.78;
  color: var(--ink);
}
.chapter-body h1 { display: none; }
.chapter-body h2,
.chapter-body h3 {
  font-size: 24px;
  font-weight: 400;
  letter-spacing: -0.005em;
  margin: 56px 0 18px;
  color: var(--wine);
}
.chapter-body p {
  margin-bottom: 1.15em;
  text-align: justify;
  hyphens: auto;
  -webkit-hyphens: auto;
}
.chapter-body p.reveal {
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.7s ease, transform 0.7s ease;
}
.chapter-body p.reveal.visible {
  opacity: 1;
  transform: translateY(0);
}
.chapter-body > p:first-of-type::first-letter,
.chapter-body > .first-p::first-letter {
  font-size: 60px;
  line-height: 0.85;
  float: left;
  margin: 8px 12px 0 0;
  color: var(--wine);
  font-weight: 400;
  text-shadow: 0 0 20px var(--wine-glow);
}
.chapter-body em {
  font-style: italic;
  color: var(--ink-soft);
}
.chapter-body strong {
  font-weight: 700;
  color: var(--ink);
}
.chapter-body blockquote {
  border-left: 3px solid var(--wine);
  margin: 24px 0;
  padding: 4px 0 4px 24px;
  font-style: italic;
  color: var(--ink-soft);
}
.chapter-body hr {
  border: none;
  height: 38px;
  background-image: var(--hr-img);
  background-position: center;
  background-repeat: no-repeat;
  margin: 36px 0;
}

/* HR ring icon — reacts to theme via inline color */
.chapter-body hr {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='28' height='28' viewBox='0 0 24 24'><circle cx='12' cy='12' r='9.5' stroke='%23d96b7a' stroke-width='1.4' fill='none' opacity='0.85'/><circle cx='12' cy='12' r='3' stroke='%23d96b7a' stroke-width='1.4' fill='none'/></svg>");
}
[data-theme="light"] .chapter-body hr {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='28' height='28' viewBox='0 0 24 24'><circle cx='12' cy='12' r='9.5' stroke='%236b1e2a' stroke-width='1.5' fill='none'/><circle cx='12' cy='12' r='3' stroke='%236b1e2a' stroke-width='1.5' fill='none'/></svg>");
}

.chapter-nav {
  display: flex;
  justify-content: space-between;
  align-items: stretch;
  gap: 12px;
  margin-top: 80px;
  padding-top: 30px;
  border-top: 1px solid var(--rule);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px;
  letter-spacing: 0.06em;
}
.chapter-nav a,
.chapter-nav span {
  flex: 1;
  padding: 16px 18px;
  border: 1px solid var(--rule);
  border-radius: 3px;
  background: var(--bg-soft);
  text-align: center;
  transition: all 0.25s;
  color: var(--ink-soft);
}
.chapter-nav a {
  color: var(--ink);
}
.chapter-nav a:hover {
  border-color: var(--wine);
  color: var(--wine);
  background: var(--bg-elev);
  transform: translateY(-2px);
}
.chapter-nav .home {
  flex: 0 0 auto;
  min-width: 130px;
}

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--topbar-bg);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--rule-soft);
  padding: 10px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-soft);
}
.topbar a { color: var(--wine); }
.topbar .progress {
  color: var(--bronze);
}
.topbar-tools {
  display: flex;
  align-items: center;
  gap: 10px;
}
.topbar-tools .ltts-mount-slot {
  display: inline-flex;
  align-items: center;
}
.topbar a:hover { color: var(--wine-hover); }

/* ============ Chapter swipe hint ============ */
.swipe-hint {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  color: var(--ink-mute);
  background: var(--bg-soft);
  padding: 8px 14px;
  border: 1px solid var(--rule);
  border-radius: 20px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.4s;
  z-index: 90;
  white-space: nowrap;
}
.swipe-hint.show {
  opacity: 1;
}

/* ============ Responsive ============ */
@media (max-width: 760px) {
  body { font-size: 16px; }
  .book { padding: 50px 16px 100px; }
  .book-mark { width: 56px; height: 56px; margin-bottom: 24px; }
  .book-mark::before { width: 20px; height: 20px; }
  .book-title { font-size: 40px; letter-spacing: -0.005em; }
  .book-subtitle { font-size: 17px; }
  .book-hero { padding: 30px 0 50px; margin-bottom: 50px; }
  .epigraph { margin-bottom: 50px; padding: 22px 24px; font-size: 15px; }
  .toc-section-title { margin-bottom: 30px; font-size: 11px; }
  .toc-section-title::before,
  .toc-section-title::after { width: 22px; margin: 0 10px; }
  .toc-grid { grid-template-columns: 1fr; gap: 18px; }

  .chapter-cover { height: 50vh; max-height: 380px; }
  .chapter-shell { padding: 0 18px 80px; }
  .chapter-head { padding: 30px 0 24px; }
  .chapter-title { font-size: 28px; line-height: 1.15; }
  .chapter-num { font-size: 11px; margin-bottom: 12px; }
  .chapter-body { font-size: 17px; line-height: 1.7; }
  .chapter-body > p:first-of-type::first-letter,
  .chapter-body > .first-p::first-letter {
    font-size: 44px;
    margin: 6px 8px 0 0;
  }
  .chapter-body h2,
  .chapter-body h3 { font-size: 20px; margin: 36px 0 14px; }
  .chapter-body p { text-align: left; }
  .chapter-nav {
    flex-direction: column;
    gap: 8px;
    margin-top: 48px;
  }
  .chapter-nav a,
  .chapter-nav span {
    padding: 13px 14px;
    font-size: 11px;
  }

  .topbar {
    padding: 10px 12px;
    gap: 8px;
    font-size: 10px;
    letter-spacing: 0.1em;
  }
  .theme-toggle { top: 9px; right: 9px; width: 34px; height: 34px; }
  .up-btn { width: 40px; height: 40px; bottom: 16px; right: 12px; }

  .overall-progress { font-size: 10px; }
}

@media (max-width: 380px) {
  .book-title { font-size: 34px; }
  .chapter-title { font-size: 24px; }
  .chapter-cover { height: 44vh; }
}

/* Reduce motion */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .book-mark { animation: none; }
}
