/* ============================================================
   WTR Reader — single stylesheet. All color comes from theme
   variables injected by themes.js onto :root. Fallbacks here
   keep first paint correct before JS runs.
   ============================================================ */

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
:root {
  --bg: #f3eee3; --surface: #fbf8f1; --surface-2: #efe8d8; --text: #2a2620;
  --text-dim: #6b6353; --border: #e0d7c4; --accent: #1f6f55; --accent-contrast: #ffffff;
  --term: #1f7a4d; --tts-highlight: rgba(31,111,85,.16);
  --reader-width: 760px; --reader-font: Merriweather, Georgia, 'Times New Roman', serif;
  --reader-font-size: 1.125rem; --reader-line-height: 1.8; --reader-para-gap: 0.9rem;
  --ui-font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --radius: 12px; --shadow: 0 6px 24px rgba(0,0,0,.12);
}

html { color-scheme: light dark; }
body {
  background: var(--bg); color: var(--text);
  font-family: var(--ui-font);
  -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
  min-height: 100vh;
}
a { color: var(--accent); text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
img { max-width: 100%; height: auto; }

.skip-link {
  position: absolute; left: -9999px; top: 0; background: var(--accent);
  color: var(--accent-contrast); padding: 8px 14px; z-index: 100; border-radius: 0 0 8px 0;
}
.skip-link:focus { left: 0; }

/* ---------- generic controls ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); padding: 8px 14px;
  border-radius: 10px; font-size: .9rem; font-weight: 600; line-height: 1;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.icon { padding: 8px; border-radius: 10px; }
.btn svg { width: 18px; height: 18px; display: block; }
.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px;
  border-radius: 10px; border: 1px solid transparent; background: transparent; color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; }
.chip {
  display: inline-flex; align-items: center; padding: 2px 8px; border-radius: 999px;
  font-size: .68rem; font-weight: 700; letter-spacing: .03em; text-transform: uppercase;
  background: var(--surface-2); color: var(--text-dim); border: 1px solid var(--border);
}
.chip.lang { color: var(--accent); }
.chip.mode { background: transparent; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 12px;
  padding: 10px 18px; background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { display: flex; align-items: center; gap: 9px; font-weight: 800; font-size: 1.05rem; letter-spacing: -.01em; }
.topbar .brand .logo { font-size: 1.25rem; }
.topbar .spacer { flex: 1; }

/* ---------- page container ---------- */
.page { max-width: 1100px; margin: 0 auto; padding: 22px 18px 90px; }
.page h1 { font-size: 1.5rem; margin: 4px 0 2px; }
.muted { color: var(--text-dim); }
.section-label { font-size: .78rem; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; color: var(--text-dim); margin: 26px 0 10px; }

/* ---------- library grid ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 16px; }
.book-card {
  display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; transition: transform .12s, box-shadow .12s, border-color .12s;
}
.book-card:hover { transform: translateY(-3px); box-shadow: var(--shadow); border-color: var(--accent); }
.book-card .cover {
  aspect-ratio: 3 / 4; background: linear-gradient(150deg, var(--accent), color-mix(in srgb, var(--accent) 40%, #000));
  display: grid; place-items: center; color: #fff; position: relative;
}
.book-card .cover img { width: 100%; height: 100%; object-fit: cover; }
.book-card .cover .gen-title { padding: 12px; font-weight: 800; font-size: 1rem; text-align: center; text-shadow: 0 1px 3px rgba(0,0,0,.4); }
.book-card .meta { padding: 10px 11px 12px; display: flex; flex-direction: column; gap: 6px; }
.book-card .title { font-weight: 700; font-size: .92rem; line-height: 1.25; }
.book-card .sub { font-size: .76rem; color: var(--text-dim); }
.book-card .tags { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 2px; }
.progress-bar { height: 4px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.progress-bar > span { display: block; height: 100%; background: var(--accent); }

.shelf { display: flex; gap: 14px; overflow-x: auto; padding-bottom: 8px; scroll-snap-type: x proximity; }
.shelf .book-card { min-width: 150px; max-width: 150px; scroll-snap-align: start; }

.dropzone {
  border: 2px dashed var(--border); border-radius: var(--radius); padding: 26px; text-align: center;
  color: var(--text-dim); background: var(--surface); transition: border-color .15s, background .15s;
}
.dropzone:hover, .dropzone.drag { border-color: var(--accent); background: var(--surface-2); }
.dropzone strong { color: var(--text); }

.empty { text-align: center; padding: 60px 20px; color: var(--text-dim); }

/* ---------- series detail ---------- */
.series-head { display: flex; gap: 22px; flex-wrap: wrap; align-items: flex-start; }
.series-head .cover { width: 180px; aspect-ratio: 3/4; border-radius: var(--radius); overflow: hidden; background: var(--accent); flex: none; box-shadow: var(--shadow); }
.series-head .cover img { width: 100%; height: 100%; object-fit: cover; }
.series-head .info { flex: 1; min-width: 240px; }
.series-head .info h1 { margin: 0 0 4px; }
.series-head .raw-title { font-size: 1.05rem; color: var(--text-dim); margin: 0 0 12px; }
.series-head .stats { display: flex; gap: 18px; flex-wrap: wrap; margin: 14px 0; }
.series-head .stats .stat .n { font-weight: 800; font-size: 1.1rem; }
.series-head .stats .stat .l { font-size: .72rem; color: var(--text-dim); text-transform: uppercase; letter-spacing: .05em; }
.series-actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 8px; }

.tabs { display: flex; gap: 6px; border-bottom: 1px solid var(--border); margin: 26px 0 14px; }
.tabs .tab { background: transparent; border: 0; padding: 10px 4px; margin-right: 16px; font-weight: 700; color: var(--text-dim); border-bottom: 2px solid transparent; }
.tabs .tab.active { color: var(--text); border-bottom-color: var(--accent); }

.synopsis { line-height: 1.7; max-width: 70ch; }

.toc { display: flex; flex-direction: column; }
.toc .ch-row {
  display: flex; align-items: center; gap: 12px; padding: 11px 8px; border-bottom: 1px solid var(--border);
  color: var(--text); background: transparent; border-left: 0; border-right: 0; border-top: 0; text-align: left; width: 100%;
}
.toc .ch-row:hover { background: var(--surface-2); }
.toc .ch-row .n { width: 42px; color: var(--text-dim); font-variant-numeric: tabular-nums; font-size: .85rem; }
.toc .ch-row .t { flex: 1; font-size: .95rem; }
.toc .ch-row .dot { width: 8px; height: 8px; border-radius: 999px; background: var(--border); flex: none; }
.toc .ch-row .dot.read { background: var(--accent); }
.toc .ch-row .dot.current { background: var(--accent); box-shadow: 0 0 0 3px var(--tts-highlight); }

/* ============================================================
   READER
   ============================================================ */
.reader { padding: 8px 0 140px; }
.reader .reader-col {
  max-width: var(--reader-width); margin: 0 auto; padding: 18px 20px;
}
.reader-topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 8px;
  padding: 8px 14px; background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.reader-topbar .ctitle { flex: 1; font-weight: 700; font-size: .92rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chapter-tracker { margin-bottom: 28px; }
.chapter-tracker .chapter-title { font-size: 1.45rem; font-weight: 800; margin: 8px 0 22px; line-height: 1.3; font-family: var(--reader-font); }
.chapter-body {
  font-family: var(--reader-font); font-size: var(--reader-font-size);
  line-height: var(--reader-line-height); color: var(--text);
}
.chapter-body p { margin: 0 0 var(--reader-para-gap); white-space: pre-wrap; }
.chapter-body p[data-line] { scroll-margin-top: 64px; border-radius: 6px; transition: background .2s; padding: 2px 6px; margin-left: -6px; margin-right: -6px; }
.chapter-body img { display: block; margin: 16px auto; border-radius: 8px; }
.chapter-body .seg-untranslated { opacity: .55; font-style: italic; }

/* glossary term span */
.text-patch.system_term { color: var(--term); font-weight: 600; cursor: help; border-bottom: 1px dotted color-mix(in srgb, var(--term) 60%, transparent); }
body.no-term-color .text-patch.system_term { color: inherit; font-weight: inherit; border-bottom: 0; }

/* TTS active paragraph */
.chapter-body p.tts-active { background: var(--tts-highlight); box-shadow: inset 3px 0 0 var(--accent); }

.chapter-pager { display: flex; justify-content: space-between; gap: 12px; margin-top: 34px; }
.chapter-pager .btn { flex: 1; justify-content: center; }
.chapter-pager .btn[disabled] { opacity: .4; pointer-events: none; }

/* ---------- bottom reader nav + drawers ---------- */
.bottom-reader-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; display: flex; justify-content: center;
  background: var(--surface); border-top: 1px solid var(--border); box-shadow: 0 -4px 20px rgba(0,0,0,.08);
  transition: transform .25s ease;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.bottom-reader-nav.hidden { transform: translateY(110%); }
.bottom-reader-nav .fix-size { width: 100%; max-width: var(--reader-width); display: flex; }
.bottom-reader-nav .btn-group { display: flex; width: 100%; }
.bottom-reader-nav .wtr {
  flex: 1; display: grid; place-items: center; gap: 3px; padding: 9px 0 8px;
  background: transparent; border: 0; color: var(--text-dim); font-size: .62rem; font-weight: 700;
  letter-spacing: .02em;
}
.bottom-reader-nav .wtr svg { width: 23px; height: 23px; }
.bottom-reader-nav .wtr .aa { font-size: 1.15rem; font-weight: 800; line-height: 23px; height: 23px; font-family: var(--reader-font); }
.bottom-reader-nav .wtr[aria-expanded="true"], .bottom-reader-nav .wtr:hover { color: var(--accent); }

.reader-drawer {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 39; margin: 0 auto; width: 100%;
  max-width: 560px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 18px 18px 0 0; box-shadow: var(--shadow); padding: 16px 18px calc(70px + env(safe-area-inset-bottom, 0));
  transform: translateY(100%); transition: transform .26s ease; max-height: 75vh; overflow-y: auto;
}
.reader-drawer.open { transform: translateY(0); }
.reader-drawer h3 { margin: 2px 0 14px; font-size: 1rem; display: flex; align-items: center; }
.reader-drawer h3 .close { margin-left: auto; }

.field { margin-bottom: 16px; }
.field > label { display: flex; justify-content: space-between; font-size: .82rem; font-weight: 700; margin-bottom: 7px; color: var(--text-dim); }
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.field select, .field input[type="text"] {
  width: 100%; padding: 9px 10px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-family: inherit; font-size: .9rem;
}
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; }
.seg button { background: var(--bg); border: 0; padding: 8px 12px; color: var(--text-dim); font-weight: 700; font-size: .82rem; }
.seg button.active { background: var(--accent); color: var(--accent-contrast); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.toggle { position: relative; width: 44px; height: 24px; flex: none; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle .track { position: absolute; inset: 0; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; transition: background .15s; }
.toggle .track::after { content: ""; position: absolute; left: 2px; top: 1px; width: 20px; height: 20px; border-radius: 999px; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .15s; }
.toggle input:checked + .track { background: var(--accent); }
.toggle input:checked + .track::after { transform: translateX(20px); }

.theme-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 10px; }
.theme-swatch { border: 2px solid var(--border); border-radius: 10px; padding: 8px 6px 6px; cursor: pointer; display: grid; gap: 6px; background: transparent; }
.theme-swatch.active { border-color: var(--accent); }
.theme-swatch .bars { display: grid; gap: 3px; }
.theme-swatch .bars i { height: 5px; border-radius: 999px; display: block; }
.theme-swatch .name { font-size: .64rem; font-weight: 700; color: var(--text-dim); text-align: center; }

/* TTS floating panel sits above nav */
.reader-drawer .tts-controls { display: flex; align-items: center; justify-content: center; gap: 14px; margin: 6px 0 18px; }
.reader-drawer .tts-controls .icon-btn { width: 52px; height: 52px; border: 1px solid var(--border); }
.reader-drawer .tts-controls .icon-btn.play { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }
.reader-drawer .tts-controls .icon-btn svg { width: 26px; height: 26px; }
.tts-note { font-size: .8rem; color: var(--text-dim); background: var(--surface-2); padding: 8px 10px; border-radius: 8px; }

/* ---------- overlays ---------- */
.overlay { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.45); display: grid; place-items: center; padding: 18px; }
.overlay .sheet { background: var(--surface); border-radius: 16px; max-width: 540px; width: 100%; max-height: 86vh; overflow-y: auto; box-shadow: var(--shadow); }
.overlay .sheet header { display: flex; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); font-weight: 800; }
.overlay .sheet header .close { margin-left: auto; }
.overlay .sheet .body { padding: 16px 18px 22px; }

/* ToC overlay reuses .toc */
.toc-overlay .sheet { max-width: 480px; }
.toc-overlay .toc { max-height: 64vh; overflow-y: auto; }

#drop-overlay {
  position: fixed; inset: 0; z-index: 200; background: color-mix(in srgb, var(--accent) 22%, rgba(0,0,0,.55));
  display: grid; place-items: center;
}
#drop-overlay[hidden] { display: none; }
.drop-card { background: var(--surface); color: var(--text); border: 2px dashed var(--accent); border-radius: 16px; padding: 44px 56px; font-size: 1.2rem; }

.toast { position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); z-index: 300; background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: .88rem; box-shadow: var(--shadow); opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }

@media (max-width: 560px) {
  .series-head .cover { width: 130px; }
  .page { padding: 16px 14px 90px; }
}
@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; transition: none !important; }
}
