:root {
  --ui-font: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --book-font: Georgia, "Times New Roman", serif;
  --font-size: 19px;
  --app-bg: #080c11;
  --app-bg-soft: #10161d;
  --paper: #161c23;
  --paper-edge: rgba(255, 255, 255, 0.07);
  --ink: #e8e4dc;
  --muted: #9199a3;
  --ui-ink: #e9edf1;
  --accent: #8ac9e8;
  --accent-strong: #b5e5f7;
  --shadow: 0 24px 80px rgba(0, 0, 0, 0.36);
  --header-height: 64px;
  --nav-height: 72px;
}

html[data-theme="sepia"] {
  --app-bg: #1a1713;
  --app-bg-soft: #252019;
  --paper: #eee7d9;
  --paper-edge: rgba(54, 42, 28, 0.16);
  --ink: #29241e;
  --muted: #766c60;
  --ui-ink: #f4ede1;
  --accent: #d59b5d;
  --accent-strong: #f0bd80;
  --shadow: 0 24px 80px rgba(0, 0, 0, 0.32);
}

* { box-sizing: border-box; }

html { scroll-behavior: auto; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background: var(--app-bg);
  color: var(--ui-ink);
  font-family: var(--ui-font);
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; }

button:focus-visible,
a:focus-visible {
  outline: 2px solid var(--accent-strong);
  outline-offset: 3px;
}

.reading-progress {
  position: fixed;
  z-index: 80;
  inset: 0 0 auto;
  height: 3px;
  background: rgba(255, 255, 255, 0.05);
}

.reading-progress span {
  display: block;
  width: 0;
  height: 100%;
  background: var(--accent);
  box-shadow: 0 0 14px color-mix(in srgb, var(--accent), transparent 35%);
  transition: width 120ms linear;
}

.app-header {
  position: fixed;
  z-index: 60;
  top: 3px;
  left: 0;
  right: 0;
  height: var(--header-height);
  display: grid;
  grid-template-columns: 48px 1fr 48px;
  align-items: center;
  gap: 12px;
  padding: 0 max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--app-bg), transparent 12%);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  backdrop-filter: blur(18px);
}

.icon-button,
.settings-button,
.close-button {
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ui-ink);
  cursor: pointer;
}

.icon-button {
  display: grid;
  place-content: center;
  gap: 4px;
}

.icon-button span {
  display: block;
  width: 19px;
  height: 1px;
  background: currentColor;
}

.settings-button { font-weight: 650; }
.settings-button:hover,
.icon-button:hover { background: rgba(255, 255, 255, 0.07); }

.brand { min-width: 0; text-align: center; }
.brand-title { display: block; font-family: var(--book-font); font-size: 17px; letter-spacing: 0.08em; text-transform: uppercase; }
.brand-meta { display: block; margin-top: 2px; color: var(--muted); font-size: 11px; letter-spacing: 0.08em; }

.reader {
  min-height: 100vh;
  padding: calc(var(--header-height) + 32px) clamp(14px, 3vw, 42px) calc(var(--nav-height) + 42px);
  perspective: 1500px;
  overflow-x: clip;
}

.chapter-page {
  width: min(100%, 920px);
  min-height: calc(100vh - var(--header-height) - var(--nav-height) - 74px);
  margin: 0 auto;
  padding: clamp(28px, 5vw, 70px) clamp(22px, 7vw, 92px) clamp(52px, 8vw, 96px);
  background: var(--paper);
  color: var(--ink);
  border: 1px solid var(--paper-edge);
  border-radius: 3px 12px 12px 3px;
  box-shadow: var(--shadow);
  transform-origin: left center;
  backface-visibility: hidden;
}

.chapter-kicker {
  display: block;
  margin-bottom: 12px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 750;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.chapter-page h1 {
  margin: 0 0 clamp(28px, 6vw, 58px);
  font-family: var(--book-font);
  font-size: clamp(34px, 6vw, 64px);
  font-weight: 500;
  line-height: 1;
  letter-spacing: -0.035em;
}

.chapter-page h2,
.chapter-page h3 {
  margin: 2.4em 0 0.8em;
  font-family: var(--book-font);
  font-weight: 500;
  line-height: 1.2;
}

.chapter-page p,
.chapter-page li,
.chapter-page blockquote {
  font-family: var(--book-font);
  font-size: var(--font-size);
  line-height: 1.78;
}

.chapter-page p { margin: 0 0 1.12em; }
.chapter-page p + p { text-indent: 1.25em; }
.chapter-page figure + p { text-indent: 0; }
.chapter-page strong { font-weight: 700; }

.chapter-page hr {
  width: 46px;
  margin: 2.8em auto;
  border: 0;
  border-top: 1px solid color-mix(in srgb, var(--ink), transparent 68%);
}

.chapter-page figure {
  margin: 0 calc(clamp(22px, 7vw, 92px) * -1) clamp(38px, 6vw, 68px);
  overflow: hidden;
  background: #05080c;
}

.chapter-page figure img {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  opacity: 0;
  transform: scale(1.015);
  transition: opacity 500ms ease, transform 900ms cubic-bezier(.2,.8,.2,1);
}

.chapter-page figure img.is-loaded { opacity: 1; transform: scale(1); }

.chapter-page blockquote {
  margin: 2em 0;
  padding-left: 1.25em;
  border-left: 2px solid var(--accent);
  color: var(--muted);
}

.page-out-forward { animation: pageOutForward 180ms ease-in both; }
.page-in-forward { animation: pageInForward 280ms cubic-bezier(.2,.75,.2,1) both; }
.page-out-back { animation: pageOutBack 180ms ease-in both; }
.page-in-back { animation: pageInBack 280ms cubic-bezier(.2,.75,.2,1) both; }

@keyframes pageOutForward { to { opacity: 0; transform: translateX(-2.5%) rotateY(-4deg); } }
@keyframes pageInForward { from { opacity: 0; transform: translateX(3%) rotateY(3deg); } }
@keyframes pageOutBack { to { opacity: 0; transform: translateX(2.5%) rotateY(4deg); } }
@keyframes pageInBack { from { opacity: 0; transform: translateX(-3%) rotateY(-3deg); } }

.page-navigation {
  position: fixed;
  z-index: 55;
  left: 0;
  right: 0;
  bottom: 0;
  height: calc(var(--nav-height) + env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  padding-bottom: env(safe-area-inset-bottom);
  background: rgba(255, 255, 255, 0.08);
  border-top: 1px solid rgba(255, 255, 255, 0.07);
  backdrop-filter: blur(18px);
}

.page-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  border: 0;
  background: color-mix(in srgb, var(--app-bg), transparent 8%);
  color: var(--ui-ink);
  cursor: pointer;
  font-size: 14px;
  font-weight: 650;
}

.page-button:hover:not(:disabled) { background: var(--app-bg-soft); }
.page-button:disabled { color: var(--muted); cursor: default; opacity: 0.48; }
.page-button span[aria-hidden] { color: var(--accent); font-size: 20px; }

.drawer-scrim {
  position: fixed;
  z-index: 68;
  inset: 0;
  background: rgba(0, 0, 0, 0.56);
  opacity: 0;
  transition: opacity 180ms ease;
}

.drawer-scrim.is-visible { opacity: 1; }

.chapter-drawer {
  position: fixed;
  z-index: 70;
  inset: 0 auto 0 0;
  width: min(88vw, 390px);
  padding: max(28px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
  overflow-y: auto;
  background: #10161d;
  color: #eef1f4;
  box-shadow: 30px 0 90px rgba(0, 0, 0, 0.42);
  transform: translateX(-102%);
  transition: transform 240ms cubic-bezier(.2,.8,.2,1);
}

.chapter-drawer.is-open { transform: translateX(0); }
.drawer-header { display: flex; align-items: start; justify-content: space-between; margin-bottom: 24px; }
.drawer-header h2 { margin: 4px 0 0; font-family: var(--book-font); font-size: 32px; font-weight: 500; }
.eyebrow { color: var(--accent); font-size: 11px; font-weight: 750; letter-spacing: 0.16em; text-transform: uppercase; }
.close-button { font-size: 30px; line-height: 1; }

#chapterList { border-top: 1px solid rgba(255, 255, 255, 0.08); }
.chapter-link {
  width: 100%;
  display: grid;
  grid-template-columns: 36px 1fr;
  gap: 10px;
  padding: 15px 8px;
  border: 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
  background: transparent;
  color: #c7ced5;
  text-align: left;
  cursor: pointer;
}
.chapter-link span:first-child { color: #697582; font-variant-numeric: tabular-nums; }
.chapter-link.is-current { color: #fff; }
.chapter-link.is-current span:first-child { color: var(--accent); }

.settings-panel {
  position: fixed;
  z-index: 65;
  top: calc(var(--header-height) + 14px);
  right: max(14px, env(safe-area-inset-right));
  width: min(290px, calc(100vw - 28px));
  padding: 18px;
  background: #141b23;
  border: 1px solid rgba(255, 255, 255, 0.09);
  border-radius: 12px;
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.38);
}
.setting-label { display: block; margin-bottom: 10px; color: #aeb7c0; font-size: 12px; }
.setting-actions { display: grid; grid-template-columns: 1fr 42px 1fr; gap: 8px; }
.setting-actions button,
.theme-toggle { min-height: 42px; border: 1px solid rgba(255,255,255,.1); border-radius: 8px; background: #1d2630; color: #edf1f4; cursor: pointer; }
.setting-actions output { display: grid; place-items: center; color: #b9c2cb; font-size: 13px; }
.theme-toggle { width: 100%; margin-top: 12px; }

noscript { position: fixed; inset: auto 20px 20px; padding: 14px; background: #7d2f2f; color: white; }

@media (max-width: 640px) {
  :root { --header-height: 58px; --nav-height: 64px; --font-size: 18px; }
  .app-header { grid-template-columns: 44px 1fr 44px; padding-inline: max(8px, env(safe-area-inset-left)); }
  .reader { padding: calc(var(--header-height) + 12px) 0 calc(var(--nav-height) + 10px); }
  .chapter-page {
    min-height: calc(100vh - var(--header-height) - var(--nav-height) - 22px);
    padding: 30px 21px 58px;
    border-left: 0;
    border-right: 0;
    border-radius: 0;
    box-shadow: none;
  }
  .chapter-page h1 { margin-bottom: 30px; font-size: clamp(34px, 12vw, 48px); }
  .chapter-page p, .chapter-page li, .chapter-page blockquote { line-height: 1.72; }
  .chapter-page figure { margin: 0 -21px 36px; }
  .page-button { gap: 8px; }
  .page-button span:not([aria-hidden]) { font-size: 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: 1ms !important; transition-duration: 1ms !important; }
}
