
/* ==== Theme variables ==== */
:root {
  /* DARK is default */
  --bg: #0a0a0c;
  --bg-soft: #14141a;
  --bg-elev: #1d1d24;
  --bg-fade: #0a0a0c;
  --ink: #d4d0c8;
  --ink-soft: #a59b88;
  --ink-mute: #6a6258;
  --gold: #b8956a;
  --gold-hover: #d4af80;
  --gold-glow: rgba(184, 149, 106, 0.18);
  --steel: #5a7a8c;
  --steel-soft: #4a6678;
  --rule: #2a2620;
  --rule-soft: #1a1814;
  --paper-overlay: rgba(255, 246, 230, 0.015);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.55);
  --shadow-card: 0 2px 12px rgba(0, 0, 0, 0.4);
  --topbar-bg: rgba(10, 10, 12, 0.88);
  --warm-highlight: #e8dfc8;
}

[data-theme="light"] {
  --bg: #f4ede0;
  --bg-soft: #ebe1cf;
  --bg-elev: #ffffff;
  --bg-fade: #f4ede0;
  --ink: #2a241a;
  --ink-soft: #5a4a32;
  --ink-mute: #8a7a60;
  --gold: #8a6028;
  --gold-hover: #a07640;
  --gold-glow: rgba(138, 96, 40, 0.10);
  --steel: #3a5564;
  --steel-soft: #4a6878;
  --rule: #c8b890;
  --rule-soft: #d8cca8;
  --paper-overlay: rgba(0, 0, 0, 0.012);
  --shadow: 0 8px 30px rgba(60, 40, 20, 0.10);
  --shadow-card: 0 2px 12px rgba(60, 40, 20, 0.08);
  --topbar-bg: rgba(244, 237, 224, 0.92);
  --warm-highlight: #2a1a08;
}

/* ==== 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: 'Cormorant', Georgia, serif;
  font-size: 19px;
  line-height: 1.65;
  font-weight: 400;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  transition: background-color 0.45s ease, color 0.45s 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.45;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='240' height='240'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.92' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.9  0 0 0 0 0.82  0 0 0 0 0.62  0 0 0 0.4 0'/></filter><rect width='100%' height='100%' filter='url(%23n)' opacity='0.16'/></svg>");
  mix-blend-mode: overlay;
}

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

.serif { font-family: 'Cinzel', serif; }

/* ==== Reading progress bar (top) ==== */
.read-progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  width: 0%;
  background: linear-gradient(90deg, var(--gold), var(--steel));
  z-index: 100;
  transition: width 80ms linear;
  box-shadow: 0 0 8px var(--gold-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(--gold);
  color: var(--gold);
  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; }

.theme-toggle.theme-toggle--inbar {
  position: static;
  top: auto; right: auto;
  width: 32px; height: 32px;
  z-index: auto;
}
.theme-toggle.theme-toggle--inbar svg { width: 14px; height: 14px; }

/* ==== 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(--gold);
  color: var(--gold);
  transform: translateY(-3px);
}

/* ============ INDEX ============ */
.shell {
  max-width: 880px;
  margin: 0 auto;
  padding: 60px 28px 140px;
  position: relative;
  z-index: 1;
}

.book-header {
  text-align: center;
  margin-bottom: 70px;
  padding-bottom: 50px;
  border-bottom: 1px solid var(--rule);
  opacity: 0;
  transform: translateY(20px);
  animation: fade-up 1s ease 0.1s forwards;
  position: relative;
}

.book-mark {
  display: inline-block;
  width: 64px;
  height: 64px;
  margin-bottom: 30px;
  position: relative;
  animation: spin 120s linear infinite;
}
.book-mark::before,
.book-mark::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 1px solid var(--gold);
  opacity: 0.7;
}
.book-mark::after {
  inset: 8px;
  border-style: dashed;
  opacity: 0.4;
  animation: spin 90s linear infinite reverse;
}
.book-mark-inner {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 18px; height: 18px;
  background: var(--gold);
  border-radius: 50%;
  box-shadow: 0 0 22px var(--gold-glow);
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.book-title {
  font-family: 'Cinzel', serif;
  font-size: 54px;
  font-weight: 600;
  letter-spacing: 0.02em;
  line-height: 1.05;
  margin-bottom: 14px;
  color: var(--warm-highlight);
  background: linear-gradient(180deg, var(--warm-highlight) 0%, var(--gold) 130%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.book-subtitle {
  font-style: italic;
  color: var(--ink-soft);
  font-size: 18px;
  max-width: 560px;
  margin: 0 auto 24px;
  line-height: 1.5;
}
.book-author {
  font-family: 'Cinzel', serif;
  font-size: 12px;
  letter-spacing: 0.32em;
  color: var(--gold);
  text-transform: uppercase;
}

/* Overall reading progress */
.overall-progress {
  max-width: 480px;
  margin: 32px auto 0;
  font-family: 'Cinzel', serif;
  font-size: 11px;
  letter-spacing: 0.18em;
  color: var(--ink-mute);
  text-transform: uppercase;
}
.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(--gold), var(--steel));
  width: 0%;
  transition: width 0.8s cubic-bezier(0.16, 1, 0.3, 1);
}

.warning {
  margin: 60px auto 70px;
  max-width: 620px;
  padding: 28px 36px;
  border-left: 2px solid var(--gold);
  background: var(--bg-soft);
  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;
}
.warning p { margin-bottom: 0.7em; }
.warning p:last-child { margin-bottom: 0; }

@keyframes fade-up {
  to { opacity: 1; transform: translateY(0); }
}

.toc-section-title {
  font-family: 'Cinzel', serif;
  font-size: 12px;
  letter-spacing: 0.32em;
  text-transform: uppercase;
  color: var(--gold);
  text-align: center;
  margin-bottom: 40px;
  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-list {
  list-style: none;
  display: grid;
  gap: 6px;
}

.toc-item {
  display: flex;
  align-items: stretch;
  gap: 18px;
  padding: 14px 18px;
  border: 1px solid transparent;
  border-radius: 4px;
  transition: all .35s cubic-bezier(0.16, 1, 0.3, 1);
  background: rgba(255,255,255,0.012);
  opacity: 0;
  transform: translateY(20px);
  position: relative;
  overflow: hidden;
}
.toc-item.visible {
  opacity: 1;
  transform: translateY(0);
}
.toc-item:hover {
  background: var(--bg-soft);
  border-color: var(--rule);
  transform: translateY(-2px);
  box-shadow: var(--shadow-card);
}
.toc-item:hover .toc-thumb {
  border-color: var(--gold);
  transform: scale(1.04);
}
.toc-link {
  color: inherit;
  display: contents;
}
.toc-num {
  font-family: 'Cinzel', serif;
  font-size: 13px;
  letter-spacing: 0.06em;
  color: var(--gold);
  width: 56px;
  flex-shrink: 0;
  padding-top: 18px;
  text-transform: uppercase;
}
.toc-thumb {
  width: 64px;
  height: 64px;
  flex-shrink: 0;
  background: var(--bg-elev);
  border-radius: 2px;
  background-size: cover;
  background-position: center;
  border: 1px solid var(--rule);
  position: relative;
  overflow: hidden;
  transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}
.toc-thumb::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, transparent 60%, rgba(0,0,0,0.4) 100%);
  opacity: 0;
  transition: opacity 0.4s;
}
.toc-item:hover .toc-thumb::after { opacity: 1; }

.toc-text {
  flex: 1;
  min-width: 0;
  padding-top: 14px;
}
.toc-name {
  color: var(--ink);
  font-size: 19px;
  display: block;
  font-family: 'Cormorant', serif;
  font-weight: 400;
  line-height: 1.3;
}

/* Read marker */
.toc-item.is-read .toc-num::before {
  content: "✓ ";
  color: var(--gold);
  margin-right: 4px;
}

/* ============ CHAPTER ============ */
.chapter-cover {
  width: 100%;
  height: 70vh;
  max-height: 720px;
  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(10,10,12,0.15) 0%, transparent 35%, var(--bg) 100%);
  pointer-events: none;
}
[data-theme="light"] .chapter-cover::after {
  background: linear-gradient(180deg, rgba(244,237,224,0.10) 0%, transparent 35%, var(--bg) 100%);
}

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

.chapter-head {
  text-align: center;
  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: 'Cinzel', serif;
  font-size: 12px;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--gold);
  margin-bottom: 22px;
}
.chapter-title {
  font-family: 'Cinzel', serif;
  font-size: 42px;
  font-weight: 600;
  line-height: 1.1;
  letter-spacing: 0.02em;
  color: var(--warm-highlight);
}

.chapter-content {
  font-size: 19px;
  line-height: 1.7;
  color: var(--ink);
}
.chapter-content h1 { display: none; }
.chapter-content h2,
.chapter-content h3 {
  font-family: 'Cinzel', serif;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0.01em;
  margin: 56px 0 18px;
  color: var(--gold);
}
.chapter-content p {
  margin-bottom: 1.15em;
  text-align: justify;
  hyphens: auto;
  -webkit-hyphens: auto;
}
.chapter-content p.reveal {
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.7s ease, transform 0.7s ease;
}
.chapter-content p.reveal.visible {
  opacity: 1;
  transform: translateY(0);
}
.chapter-content > p:first-of-type::first-letter,
.chapter-content > .first-p::first-letter {
  font-family: 'Cinzel', serif;
  font-size: 56px;
  line-height: 0.9;
  float: left;
  margin: 6px 12px 0 0;
  color: var(--gold);
  font-weight: 600;
  text-shadow: 0 0 18px var(--gold-glow);
}
.chapter-content em {
  font-style: italic;
  color: var(--ink-soft);
}
.chapter-content strong {
  font-weight: 600;
  color: var(--warm-highlight);
}
.chapter-content blockquote {
  border-left: 2px solid var(--gold);
  margin: 24px 0;
  padding: 4px 0 4px 24px;
  font-style: italic;
  color: var(--ink-soft);
}
.chapter-content hr {
  border: none;
  height: 30px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='60' height='30' viewBox='0 0 60 30'><circle cx='10' cy='15' r='1.6' fill='%23b8956a'/><circle cx='30' cy='15' r='1.6' fill='%23b8956a'/><circle cx='50' cy='15' r='1.6' fill='%23b8956a'/></svg>");
  background-position: center;
  background-repeat: no-repeat;
  margin: 38px 0;
}
[data-theme="light"] .chapter-content hr {
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='60' height='30' viewBox='0 0 60 30'><circle cx='10' cy='15' r='1.6' fill='%238a6028'/><circle cx='30' cy='15' r='1.6' fill='%238a6028'/><circle cx='50' cy='15' r='1.6' fill='%238a6028'/></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: 'Cinzel', serif;
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}
.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(--gold);
  color: var(--gold);
  background: var(--bg-elev);
  transform: translateY(-2px);
}
.chapter-nav .home {
  flex: 0 0 auto;
  min-width: 130px;
  color: var(--gold);
}

/* 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: 'Cinzel', serif;
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-soft);
}
.topbar a { color: var(--gold); }
.topbar .progress { color: var(--gold); }
.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(--gold-hover); }

/* Swipe hint */
.swipe-hint {
  position: fixed;
  bottom: 100px;
  left: 50%;
  transform: translateX(-50%);
  font-family: 'Cinzel', serif;
  font-size: 10px;
  letter-spacing: 0.18em;
  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;
  text-transform: uppercase;
}
.swipe-hint.show { opacity: 1; }

/* Responsive */
@media (max-width: 760px) {
  body { font-size: 17px; }
  .shell { padding: 38px 16px 90px; }
  .book-mark { width: 52px; height: 52px; margin-bottom: 22px; }
  .book-title { font-size: 38px; letter-spacing: 0.01em; }
  .book-subtitle { font-size: 16px; }
  .book-header { padding: 20px 0 36px; margin-bottom: 50px; }
  .warning { margin: 40px auto 50px; padding: 22px 24px; font-size: 15px; }
  .toc-section-title { margin-bottom: 28px; font-size: 11px; }
  .toc-section-title::before,
  .toc-section-title::after { width: 22px; margin: 0 10px; }
  .toc-item { gap: 14px; padding: 12px 14px; }
  .toc-num { width: 44px; font-size: 11px; padding-top: 16px; }
  .toc-thumb { width: 52px; height: 52px; }
  .toc-name { font-size: 17px; }
  .toc-text { padding-top: 12px; }

  .chapter-cover { height: 48vh; max-height: 360px; }
  .chapter-shell { padding: 0 18px 80px; }
  .chapter-head { padding: 30px 0 24px; }
  .chapter-title { font-size: 26px; line-height: 1.18; }
  .chapter-num { font-size: 11px; margin-bottom: 14px; }
  .chapter-content { font-size: 17px; line-height: 1.65; }
  .chapter-content > p:first-of-type::first-letter,
  .chapter-content > .first-p::first-letter {
    font-size: 42px;
    margin: 4px 8px 0 0;
  }
  .chapter-content h2,
  .chapter-content h3 { font-size: 19px; margin: 36px 0 14px; }
  .chapter-content 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;
  }
  .chapter-nav .home { min-width: 0; }

  .topbar {
    padding: 10px 12px;
    gap: 8px;
    font-size: 10px;
    letter-spacing: 0.12em;
  }
  .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: 32px; }
  .chapter-title { font-size: 23px; }
  .chapter-cover { height: 42vh; }
}

/* 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; }
}
