:root {
  color-scheme: dark;
  --canvas: oklch(0.12 0.008 285);
  --panel: oklch(0.17 0.015 325);
  --panel-raised: oklch(0.225 0.024 326);
  --line: oklch(0.34 0.022 320);
  --ink: oklch(0.95 0.007 90);
  --muted: oklch(0.7 0.022 315);
  --primary: oklch(0.69 0.19 350);
  --primary-ink: oklch(0.98 0.003 0);
  --accent: oklch(0.77 0.12 205);
  --focus: oklch(0.78 0.14 205);
  --reader-bg: oklch(0.075 0.006 285);
  --bar-height: 72px;
  --control-height: 72px;
  --z-library: 20;
  --z-scrim: 10;
}

* { box-sizing: border-box; }
html, body, #app-shell { min-height: 100%; }
body {
  min-width: 320px;
  margin: 0;
  background: var(--canvas);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.45;
}
button, input { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }
button:focus-visible, a:focus-visible, input:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.app-shell { display: grid; height: 100dvh; min-height: 100dvh; grid-template-columns: minmax(264px, 320px) minmax(0, 1fr); }
.app-shell.reader-fullscreen { display: block; }
.app-shell.reader-fullscreen .library, .app-shell.reader-fullscreen .scrim, .app-shell.reader-fullscreen .reader-bar, .app-shell.reader-fullscreen .reader-controls { display: none; }
.app-shell.reader-fullscreen .reader { height: 100dvh; min-height: 100dvh; grid-template-rows: minmax(0, 1fr); }
.app-shell.reader-fullscreen .pdf-surface { padding: 0; }
.app-shell.reader-fullscreen .pdf-page { width: 100%; margin-bottom: 3px; box-shadow: none; }
.library { z-index: var(--z-library); display: flex; min-height: 0; flex-direction: column; border-right: 1px solid var(--line); background: var(--panel); }
.library-head { display: flex; align-items: flex-start; justify-content: space-between; padding: 28px 20px 18px; }
.eyebrow, .library-meta, .reader-title p, .page-position { margin: 0; color: var(--muted); font-size: 12px; }
.eyebrow { margin-bottom: 5px; color: var(--accent); font-weight: 700; }
h1, h2 { margin: 0; text-wrap: balance; }
h1 { font-size: 28px; font-weight: 760; letter-spacing: 0; line-height: 1.1; }
h2 { overflow: hidden; font-size: 15px; font-weight: 650; letter-spacing: 0; text-overflow: ellipsis; white-space: nowrap; }

.icon-button, .clear-search, .page-button {
  display: inline-grid;
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  place-items: center;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  font-size: 20px;
  line-height: 1;
  text-decoration: none;
  transition: background-color 160ms ease-out, border-color 160ms ease-out, color 160ms ease-out;
}
.icon-button:hover, .clear-search:hover, .page-button:hover:not(:disabled) { border-color: var(--line); background: var(--panel-raised); }
.library-close, .menu-button { display: none; }

.search-wrap { display: flex; min-height: 44px; align-items: center; gap: 8px; margin: 0 16px; padding: 0 8px 0 11px; border: 1px solid var(--line); border-radius: 8px; background: color-mix(in oklch, var(--canvas) 80%, var(--panel)); color: var(--muted); }
.search-icon { font-size: 21px; line-height: 1; }
.search-wrap input { width: 100%; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--ink); }
.search-wrap input::placeholder { color: var(--muted); opacity: 1; }
.clear-search { width: 32px; height: 32px; flex-basis: 32px; color: var(--muted); }
.library-meta { padding: 17px 20px 12px; color: color-mix(in oklch, var(--muted) 88%, var(--accent)); }
.chapter-tree { min-height: 0; flex: 1; overflow: auto; padding: 0 12px 24px; }
.volume { border-top: 1px solid var(--line); }
.volume:last-child { border-bottom: 1px solid var(--line); }
.volume summary { display: flex; min-height: 50px; align-items: center; justify-content: space-between; padding: 0 8px; color: var(--ink); cursor: pointer; font-size: 14px; font-weight: 700; list-style: none; }
.volume summary::-webkit-details-marker { display: none; }
.volume summary::after { content: '+'; color: var(--muted); font-size: 19px; font-weight: 400; }
.volume[open] summary::after { content: '−'; }
.chapter-list { display: grid; gap: 3px; padding: 1px 0 12px; }
.chapter-button { display: flex; width: 100%; min-height: 40px; align-items: center; gap: 9px; padding: 0 10px; border: 0; border-radius: 7px; background: transparent; color: var(--muted); text-align: left; transition: background-color 160ms ease-out, color 160ms ease-out; }
.chapter-button::before { width: 6px; height: 6px; flex: 0 0 6px; border-radius: 50%; background: color-mix(in oklch, var(--muted) 36%, transparent); content: ''; }
.chapter-button:hover { background: var(--panel-raised); color: var(--ink); }
.chapter-button[aria-current="true"] { background: color-mix(in oklch, var(--primary) 27%, var(--panel)); color: var(--primary-ink); font-weight: 700; }
.chapter-button[aria-current="true"]::before { background: var(--accent); }
.chapter-button span { overflow: hidden; padding: 0; text-overflow: ellipsis; white-space: nowrap; }
.chapter-empty { padding: 12px 10px; color: var(--muted); font-size: 13px; }

.reader { display: grid; min-width: 0; height: 100dvh; min-height: 100dvh; grid-template-rows: var(--bar-height) minmax(0, 1fr) var(--control-height); }
.reader-bar { display: flex; min-width: 0; align-items: center; justify-content: space-between; gap: 16px; padding: 0 24px; border-bottom: 1px solid var(--line); background: color-mix(in oklch, var(--canvas) 88%, var(--panel)); }
.reader-title { display: flex; min-width: 0; align-items: center; gap: 12px; }
.reader-title > div { min-width: 0; }
.reader-title p { margin-bottom: 2px; color: var(--accent); font-weight: 700; }
.reader-actions { display: flex; flex: 0 0 auto; gap: 2px; }
.reading-stage { position: relative; min-height: 0; overflow: hidden; background: var(--reader-bg); }
.pdf-surface { width: 100%; height: 100%; overflow-y: auto; overscroll-behavior: contain; padding: 12px; scroll-behavior: smooth; touch-action: pan-y pinch-zoom; }
.pdf-page { position: relative; width: min(100%, 760px); min-height: 120px; margin: 0 auto 12px; overflow: hidden; background: oklch(1 0 0); box-shadow: 0 6px 8px oklch(0.02 0 0 / 0.44); }
.pdf-page canvas { display: block; width: 100%; height: auto; background: oklch(1 0 0); }
.page-placeholder { position: absolute; inset: 0; display: grid; place-items: center; background: oklch(0.91 0.008 285); }
.page-placeholder::after { width: 18px; height: 18px; border: 2px solid oklch(0.7 0.018 315); border-top-color: var(--accent); border-radius: 50%; content: ''; animation: spin 800ms linear infinite; }
.chapter-end-prompt { display: flex; min-height: 104px; align-items: center; justify-content: center; gap: 8px; margin: 0; color: var(--muted); font-size: 14px; font-weight: 650; }
.chapter-end-prompt::before { color: var(--accent); content: '↑'; font-size: 20px; line-height: 1; }
.pdf-surface.chapter-pull-ready .chapter-end-prompt { color: var(--primary-ink); }
.pdf-surface.chapter-pull-ready .chapter-end-prompt::before { color: var(--primary); }
.loading-state { position: absolute; inset: 0; z-index: 1; display: grid; place-content: center; gap: 12px; padding: 24px; background: color-mix(in oklch, var(--reader-bg) 88%, transparent); color: var(--muted); text-align: center; }
.loading-state[hidden] { display: none; }
.loading-state p { max-width: 32ch; margin: 0; text-wrap: pretty; }
.loading-mark { width: 28px; height: 28px; justify-self: center; border: 2px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin 800ms linear infinite; }
.loading-actions { display: flex; justify-content: center; gap: 8px; }
.text-action { min-height: 40px; border: 0; border-radius: 7px; background: transparent; color: var(--accent); cursor: pointer; font-size: 14px; text-decoration: underline; text-underline-offset: 3px; }
.loading-state.is-fallback .loading-mark { display: none; }

.reader-controls { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); align-items: center; gap: 14px; padding: 0 24px; border-top: 1px solid var(--line); background: color-mix(in oklch, var(--canvas) 88%, var(--panel)); }
.chapter-nav { display: inline-flex; min-height: 44px; align-items: center; gap: 7px; border: 0; border-radius: 7px; background: transparent; color: var(--ink); font-size: 14px; font-weight: 650; }
.chapter-nav:hover:not(:disabled) { background: var(--panel-raised); color: var(--primary-ink); }
.chapter-nav:disabled, .page-button:disabled { cursor: default; color: var(--line); }
.chapter-next { justify-self: end; }
.page-controls { display: inline-flex; align-items: center; gap: 4px; padding: 2px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); }
.page-position { width: 58px; color: var(--ink); font-variant-numeric: tabular-nums; font-weight: 650; text-align: center; }
.scrim { display: none; }
.sr-only { position: fixed; top: 0; left: 0; width: 1px; height: 1px; margin: 0; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }

body.light { color-scheme: light; --canvas: oklch(0.985 0.002 285); --panel: oklch(0.95 0.014 335); --panel-raised: oklch(0.91 0.022 335); --line: oklch(0.76 0.025 325); --ink: oklch(0.24 0.025 315); --muted: oklch(0.47 0.025 315); --primary: oklch(0.57 0.2 350); --primary-ink: oklch(0.99 0.002 0); --accent: oklch(0.5 0.13 205); --focus: oklch(0.56 0.15 205); --reader-bg: oklch(0.78 0.008 285); }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 1023px) {
  .app-shell { display: block; }
  .library { position: fixed; inset: 0 auto 0 0; width: min(340px, calc(100vw - 40px)); transform: translateX(-101%); transition: transform 220ms cubic-bezier(.22, 1, .36, 1); box-shadow: 7px 0 8px oklch(0.02 0 0 / 0.4); }
  .app-shell.library-open .library { transform: translateX(0); }
  .library-close, .menu-button { display: inline-grid; }
  .scrim { position: fixed; z-index: var(--z-scrim); inset: 0; display: block; background: oklch(0.06 0.01 272 / 0.58); opacity: 0; pointer-events: none; transition: opacity 220ms ease-out; }
  .app-shell.library-open .scrim { opacity: 1; pointer-events: auto; }
  .chapter-button { min-height: 44px; }
}

@media (max-width: 540px) {
  :root { --bar-height: 68px; --control-height: 68px; }
  .reader-bar { gap: 8px; padding: max(0px, env(safe-area-inset-top)) 10px 0; }
  .reader-title { gap: 6px; }
  .reader-actions { gap: 0; }
  .icon-button { width: 44px; height: 44px; flex-basis: 44px; }
  .reader-controls { gap: 6px; padding: 0 10px env(safe-area-inset-bottom); }
  .chapter-nav { width: 44px; min-width: 44px; justify-content: center; font-size: 24px; }
  .chapter-nav .control-label { display: none; }
  .page-controls { gap: 2px; }
  .page-button { width: 44px; height: 44px; flex-basis: 44px; }
  .page-position { width: 50px; }
  .pdf-surface { padding: 8px; }
  .pdf-page { margin-bottom: 8px; }
}

@media (pointer: coarse) { .volume summary { min-height: 48px; } .chapter-button { min-height: 44px; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; } }
