/* ───────────── Tokens & themes ───────────── */
:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --text: #1d1c1a;
  --muted: #6d6a64;
  --line: #dedad2;
  --accent: #c2410c;
  --accent-ink: #ffffff;
  --accent-soft: #fbe3d6;
  --page-bg: #ffffff;
  --page-text: #1d1c1a;
  --pdf-filter: none;
  --tts-hl: rgba(255, 196, 0, .45);
  --search-hl: rgba(255, 120, 0, .45);
  --shadow: 0 8px 30px rgba(0,0,0,.12);
  --chrome-bg: color-mix(in srgb, var(--surface) 92%, transparent);
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --hl-yellow: rgba(255, 213, 0, .42);
  --hl-green: rgba(64, 200, 110, .38);
  --hl-blue: rgba(70, 150, 255, .36);
  --hl-pink: rgba(255, 90, 160, .36);
  color-scheme: light;
}
:root[data-theme="sepia"] {
  --bg: #efe6d4; --surface: #f8f0de; --surface-2: #e8dcc4; --text: #3b2f22; --muted: #7d6b55;
  --line: #d9c9aa; --accent: #a5520f; --accent-soft: #f0d7b6;
  --page-bg: #f6ecd6; --page-text: #3b2f22; --pdf-filter: sepia(.35) brightness(.97);
}
:root[data-theme="dark"] {
  --bg: #15151a; --surface: #1f1f26; --surface-2: #2a2a33; --text: #e9e7e2; --muted: #a29f98;
  --line: #34343e; --accent: #fb923c; --accent-ink: #1a1007; --accent-soft: #3d2516;
  --page-bg: #1c1c22; --page-text: #dddad3; --pdf-filter: invert(.88) hue-rotate(180deg) contrast(.95);
  --tts-hl: rgba(255, 190, 40, .35); --shadow: 0 8px 30px rgba(0,0,0,.5);
  color-scheme: dark;
}
:root[data-theme="black"] {
  --bg: #000; --surface: #0e0e10; --surface-2: #1a1a1e; --text: #d8d6d0; --muted: #8e8b85;
  --line: #26262b; --accent: #fb923c; --accent-ink: #1a1007; --accent-soft: #33200f;
  --page-bg: #000; --page-text: #cfccc5; --pdf-filter: invert(.92) hue-rotate(180deg);
  --tts-hl: rgba(255, 190, 40, .32); --shadow: 0 8px 30px rgba(0,0,0,.7);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.tabular { font-variant-numeric: tabular-nums; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row { display: flex; align-items: center; gap: 4px; }
svg.i { width: var(--isz, 22px); height: var(--isz, 22px); fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; display: block; }

.icon-btn {
  width: 42px; height: 42px; border-radius: 50%; border: 0; background: transparent;
  display: inline-grid; place-items: center; flex: none;
}
.icon-btn:active, .icon-btn:hover { background: var(--surface-2); }
.icon-btn.on { color: var(--accent); }
.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 10px 18px; display: inline-flex; align-items: center; gap: 8px; font-weight: 600;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.danger { color: #dc2626; }
.chip {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 999px;
  padding: 6px 12px; font-weight: 600; font-size: 13px; min-width: 56px;
}
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

.view { position: fixed; inset: 0; overflow: hidden; }

/* ───────────── Library ───────────── */
#library-view { overflow-y: auto; padding: calc(var(--safe-top) + 8px) 16px calc(var(--safe-bottom) + 100px); }
.lib-header { display: flex; align-items: center; justify-content: space-between; max-width: 1100px; margin: 0 auto; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 26px; margin: 8px 0; letter-spacing: -.02em; }
.lib-search { max-width: 1100px; margin: 4px auto 8px; }
.lib-search input, .field input[type="search"], .field input[type="text"], textarea {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); outline: none;
}
.lib-search input:focus, .field input:focus, textarea:focus { border-color: var(--accent); }
.lib-toolbar { display: flex; justify-content: space-between; align-items: center; max-width: 1100px; margin: 18px auto 10px; }
.sort { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.sort select { border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 4px 6px; }

#continue-card { max-width: 1100px; margin: 8px auto 0; }
.continue {
  display: flex; gap: 14px; align-items: center; padding: 14px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--line); cursor: pointer;
}
.continue .cover { width: 64px; flex: none; }
.continue .info { flex: 1; min-width: 0; }
.continue .label { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--accent); font-weight: 700; }
.continue .t { font-weight: 700; font-size: 16px; }
.continue .play { flex: none; }

.grid {
  display: grid; gap: 18px 14px; max-width: 1100px; margin: 0 auto;
  grid-template-columns: repeat(auto-fill, minmax(min(140px, 42vw), 1fr));
}
.card { cursor: pointer; position: relative; min-width: 0; }
.cover {
  aspect-ratio: 2 / 3; border-radius: 6px 10px 10px 6px; overflow: hidden; position: relative;
  background: var(--surface-2); box-shadow: 0 2px 6px rgba(0,0,0,.12), inset 3px 0 0 rgba(0,0,0,.08);
}
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover .gen {
  position: absolute; inset: 0; padding: 40px 12px 14px; display: flex; flex-direction: column; justify-content: space-between;
  color: #fff; font-weight: 700; font-size: 14px; line-height: 1.2; overflow: hidden;
}
.cover .gen small { font-weight: 500; opacity: .85; font-size: 11px; }
.cover .badge {
  position: absolute; top: 6px; right: 6px; background: rgba(0,0,0,.55); color: #fff;
  font-size: 10px; font-weight: 700; padding: 2px 6px; border-radius: 6px; letter-spacing: .04em;
}
.bar { height: 3px; background: var(--line); border-radius: 3px; overflow: hidden; margin-top: 8px; }
.bar > i { display: block; height: 100%; background: var(--accent); }
.card .t { font-weight: 600; font-size: 13.5px; margin-top: 8px; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .a { font-size: 12px; }
.card .menu-btn { position: absolute; top: 2px; left: 2px; width: 34px; height: 34px; background: rgba(0,0,0,.4); color: #fff; opacity: 0; }
.card:hover .menu-btn, .card:focus-within .menu-btn { opacity: 1; }
@media (hover: none) { .card .menu-btn { opacity: 1; background: rgba(0,0,0,.3); } }

.empty { text-align: center; max-width: 360px; margin: 12vh auto 0; }
.empty-art { display: inline-grid; place-items: center; width: 96px; height: 96px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); }
.empty h2 { margin: 18px 0 6px; }
.empty p { color: var(--muted); margin: 0 0 18px; }

.fab {
  position: fixed; right: 18px; bottom: calc(var(--safe-bottom) + 18px); z-index: 5;
  display: flex; align-items: center; gap: 8px; padding: 14px 20px 14px 16px; border-radius: 18px; border: 0;
  background: var(--accent); color: var(--accent-ink); font-weight: 700; box-shadow: var(--shadow);
}
.drop-hint {
  position: fixed; inset: 12px; border: 3px dashed var(--accent); border-radius: 20px; z-index: 50;
  display: grid; place-items: center; font-size: 20px; font-weight: 700; background: color-mix(in srgb, var(--bg) 85%, transparent);
}

/* ───────────── Reader ───────────── */
#reader-view { background: var(--page-bg); }
#reader-stage { position: absolute; inset: 0; }
.chrome {
  position: absolute; left: 0; right: 0; z-index: 10; background: var(--chrome-bg);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  transition: transform .22s ease, opacity .22s ease;
}
.chrome.top {
  top: 0; padding: calc(var(--safe-top) + 4px) 6px 4px; display: flex; align-items: center; gap: 2px;
  border-bottom: 1px solid var(--line);
}
.chrome.bottom { bottom: 0; padding: 8px 12px calc(var(--safe-bottom) + 8px); border-top: 1px solid var(--line); }
#reader-view.immersive .chrome.top { transform: translateY(-100%); opacity: 0; pointer-events: none; }
#reader-view.immersive .chrome.bottom { transform: translateY(100%); opacity: 0; pointer-events: none; }
.r-title { flex: 1; min-width: 0; padding: 0 6px; }
#r-title { font-weight: 600; }
.progress-row { display: flex; align-items: center; gap: 10px; }
.progress-row span { min-width: 48px; }
.progress-row span:last-child { text-align: right; }
input[type="range"] { flex: 1; accent-color: var(--accent); min-width: 0; }
.bottom-actions { display: flex; justify-content: space-between; align-items: center; margin-top: 4px; }
.btn.listen { background: var(--accent); color: var(--accent-ink); border: 0; padding: 10px 22px; }

/* PDF */
.pdf-scroller {
  position: absolute; inset: 0; overflow: auto; overscroll-behavior: contain;
  padding: calc(var(--safe-top) + 64px) 0 calc(var(--safe-bottom) + 120px);
  touch-action: pan-x pan-y;
}
.pdf-pages { display: flex; flex-direction: column; align-items: center; gap: 12px; margin: 0 auto; transform-origin: 0 0; width: max-content; min-width: 100%; padding: 0 8px; }
.pdf-page {
  position: relative; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.18); flex: none;
}
.pdf-page canvas { position: absolute; inset: 0; width: 100%; height: 100%; filter: var(--pdf-filter); }
.pdf-page .pnum { position: absolute; bottom: -22px; left: 0; right: 0; text-align: center; font-size: 11px; color: var(--muted); }
:root[data-theme="dark"] .pdf-page, :root[data-theme="black"] .pdf-page { background: #222; }
:root[data-theme="sepia"] .pdf-page { background: #f6ecd6; }
.hl-layer { position: absolute; inset: 0; pointer-events: none; z-index: 2; mix-blend-mode: multiply; }
:root[data-theme="dark"] .hl-layer, :root[data-theme="black"] .hl-layer { mix-blend-mode: screen; opacity: .7; }
.hl-layer div { position: absolute; border-radius: 2px; pointer-events: auto; cursor: pointer; }
.hl-layer div.note::after { content: ""; position: absolute; right: -4px; top: -4px; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.hl-yellow { background: var(--hl-yellow); } .hl-green { background: var(--hl-green); }
.hl-blue { background: var(--hl-blue); } .hl-pink { background: var(--hl-pink); }
.textLayer { position: absolute; text-align: initial; inset: 0; overflow: clip; opacity: 1; line-height: 1; text-size-adjust: none; -webkit-text-size-adjust: none; forced-color-adjust: none; transform-origin: 0 0; z-index: 3; }
.textLayer :is(span, br) { color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%; }
.textLayer span.markedContent { top: 0; height: 0; }
.textLayer ::selection { background: rgba(0, 110, 255, .3); }
.textLayer br::selection { background: transparent; }
.textLayer .endOfContent { display: block; position: absolute; inset: 100% 0 0; z-index: -1; cursor: default; user-select: none; }
.textLayer.selecting .endOfContent { top: 0; }
::highlight(tts) { background-color: var(--tts-hl); }
::highlight(search) { background-color: var(--search-hl); }
.page-placeholder { position: absolute; inset: 0; display: grid; place-items: center; color: #aaa; font-size: 13px; }

/* EPUB */
.epub-area { position: absolute; top: calc(var(--safe-top) + 40px); bottom: calc(var(--safe-bottom) + 38px); left: 24px; right: 24px; }
.epub-area.scrolled { top: calc(var(--safe-top) + 56px); bottom: 0; left: 0; right: 0; }
.run-head, .run-foot { position: absolute; left: 24px; right: 24px; text-align: center; pointer-events: none; opacity: .8; }
.run-head { top: calc(var(--safe-top) + 12px); }
.run-foot { bottom: calc(var(--safe-bottom) + 12px); }

/* Player */
.player {
  position: absolute; left: 50%; transform: translateX(-50%); z-index: 12;
  bottom: calc(var(--safe-bottom) + 112px); width: min(520px, calc(100% - 20px));
  background: var(--surface); border: 1px solid var(--line); border-radius: 22px; box-shadow: var(--shadow);
  padding: 8px 10px 8px; transition: bottom .22s ease;
}
#reader-view.immersive .player { bottom: calc(var(--safe-bottom) + 14px); }
.player-status { text-align: center; padding: 0 10px 4px; min-height: 18px; }
.player-row { display: flex; align-items: center; justify-content: space-between; gap: 2px; }
.play-btn {
  width: 56px; height: 56px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; flex: none;
}
.play-btn.busy { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; } }

/* Selection bar */
.selection-bar {
  position: absolute; z-index: 14; left: 50%; transform: translateX(-50%); top: calc(var(--safe-top) + 64px);
  display: flex; align-items: center; gap: 4px; padding: 6px 8px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.selection-bar .sep { width: 1px; height: 24px; background: var(--line); margin: 0 4px; }
.swatch { width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--line); margin: 0 3px; }
.swatch[data-color="yellow"] { background: #ffd500; } .swatch[data-color="green"] { background: #40c86e; }
.swatch[data-color="blue"] { background: #4696ff; } .swatch[data-color="pink"] { background: #ff5aa0; }
.swatch.on { box-shadow: 0 0 0 2px var(--accent); }

/* Loading */
.loading-veil { position: absolute; inset: 0; z-index: 20; display: grid; place-content: center; justify-items: center; gap: 14px; background: var(--page-bg); }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ───────────── Sheet ───────────── */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 30; animation: fade .18s ease; }
.sheet {
  position: fixed; z-index: 31; left: 0; right: 0; bottom: 0; margin: 0 auto; width: min(640px, 100%);
  max-height: 86vh; display: flex; flex-direction: column;
  background: var(--surface); border-radius: 22px 22px 0 0; box-shadow: var(--shadow);
  padding-bottom: var(--safe-bottom); animation: rise .22s cubic-bezier(.2,.8,.2,1);
}
@media (min-width: 900px) {
  .sheet { bottom: auto; top: 50%; transform: translateY(-50%); border-radius: 22px; max-height: 80vh; animation: fade .18s ease; }
  .sheet-grip { display: none; }
}
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
.sheet-grip { width: 40px; height: 4px; border-radius: 4px; background: var(--line); margin: 8px auto 0; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 10px 2px 20px; }
.sheet-head h2 { font-size: 18px; margin: 0; }
.sheet-body { overflow-y: auto; padding: 6px 20px 20px; overscroll-behavior: contain; }

.tabs { display: flex; gap: 6px; margin: 4px 0 12px; position: sticky; top: 0; background: var(--surface); padding: 4px 0; z-index: 1; }
.tabs button { flex: 1; border: 0; background: var(--surface-2); border-radius: 10px; padding: 8px; font-weight: 600; font-size: 13px; }
.tabs button.on { background: var(--accent); color: var(--accent-ink); }

.list { list-style: none; margin: 0; padding: 0; }
.list li { border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: 0; }
.list .item { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; border: 0; background: none; padding: 12px 4px; }
.list .item:hover { background: var(--surface-2); }
.list .item .grow { flex: 1; min-width: 0; }
.list .item.current { color: var(--accent); font-weight: 700; }
.list .sub { font-size: 12px; color: var(--muted); }
.list .quote { border-left: 3px solid var(--hl-yellow); padding-left: 10px; }
.list .quote.c-green { border-color: #40c86e; } .list .quote.c-blue { border-color: #4696ff; } .list .quote.c-pink { border-color: #ff5aa0; } .list .quote.c-yellow { border-color: #ffd500; }
.list .note-text { margin-top: 4px; font-style: italic; }
.list mark { background: var(--search-hl); color: inherit; border-radius: 3px; }
.empty-small { text-align: center; color: var(--muted); padding: 28px 0; }

.field { margin: 14px 0; }
.field > label, .field-label { display: block; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 8px; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { flex: 1; min-width: 64px; border: 1px solid var(--line); background: var(--surface); border-radius: 10px; padding: 9px 6px; font-weight: 600; font-size: 13px; }
.seg button.on { border-color: var(--accent); background: var(--accent-soft); color: var(--text); box-shadow: inset 0 0 0 1px var(--accent); }
.theme-dot { display: inline-block; width: 16px; height: 16px; border-radius: 50%; vertical-align: -3px; margin-right: 6px; border: 1px solid rgba(128,128,128,.4); }
.stepper { display: flex; align-items: center; gap: 10px; }
.stepper output { flex: 1; text-align: center; font-weight: 700; }
.stepper .icon-btn { border: 1px solid var(--line); }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; }
.switch-row .sub { font-size: 12px; color: var(--muted); }
.switch { appearance: none; width: 46px; height: 28px; border-radius: 999px; background: var(--line); position: relative; flex: none; transition: background .15s; cursor: pointer; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(18px); }
.voice-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.voice {
  display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); background: var(--surface);
  border-radius: 12px; padding: 8px 8px 8px 12px; text-align: left;
}
.voice.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); background: var(--accent-soft); }
.voice .grow { flex: 1; min-width: 0; }
.voice .name { font-weight: 700; font-size: 14px; }
.voice .meta { font-size: 11.5px; color: var(--muted); }
.voice .icon-btn { width: 34px; height: 34px; }
.callout { background: var(--surface-2); border-radius: 12px; padding: 12px 14px; font-size: 13px; }
.callout b { color: var(--text); }
.meter { height: 6px; border-radius: 6px; background: var(--line); overflow: hidden; margin-top: 8px; }
.meter > i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .2s; }
textarea { min-height: 110px; resize: vertical; font: inherit; }
.actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }

/* Toast */
.toast {
  position: fixed; z-index: 60; left: 50%; transform: translateX(-50%); bottom: calc(var(--safe-bottom) + 90px);
  background: #232329; color: #f2f0eb; padding: 11px 18px; border-radius: 12px; font-size: 14px; box-shadow: var(--shadow);
  max-width: calc(100% - 32px); animation: rise .2s ease;
}
.callout svg.i, .list .item > span > svg.i { display: inline-block; vertical-align: -3px; }

/* Dictionary */
.dict-head { display: flex; align-items: flex-start; gap: 10px; margin: 6px 0 4px; }
.dict-head .grow { flex: 1; min-width: 0; }
.dict-word { font-size: 26px; font-weight: 700; letter-spacing: -.01em; }
.dict-pos { margin-top: 14px; }
.dict-senses { margin: 0; padding-left: 22px; }
.dict-senses li { margin-bottom: 12px; }
.dict-ex { color: var(--muted); font-style: italic; margin-top: 3px; }
.dict-syn { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.chip.small { min-width: 0; padding: 4px 10px; font-weight: 500; font-size: 12.5px; }
.btn svg.i { display: inline-block; }
