/* ==========================================================================
   Reader — app stylesheet
   Themes: light · sepia · dark · black (data-theme on <html>)
   ========================================================================== */

:root {
  --bg: #FAFAF8;
  --bg-elevated: #FFFFFF;
  --surface: #F0EEE8;
  --surface-2: #E8E4DB;
  --text: #2C2820;
  --text-2: #5F5952;
  --text-3: #6B645C;
  --border: #E4DED4;
  --accent: #5E51A0;
  --accent-dim: rgba(94, 81, 160, .12);
  --accent-text: #4A3F8C;
  --on-accent: #FFFFFF;
  --danger: #B3261E;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.07);
  --shadow-heavy: 0 10px 40px rgba(0,0,0,.2);
  --radius: 14px;
  --radius-sm: 9px;
  --pill: 999px;
  --ui-font: system-ui, -apple-system, 'Segoe UI', Roboto, 'Atkinson Hyperlegible', sans-serif;
  --book-font: 'Literata', Georgia, serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --tabbar-h: 58px;
  --ease: cubic-bezier(.3, .7, .2, 1);
  color-scheme: light;
}

[data-theme="sepia"] {
  --bg: #F6F0E4; --bg-elevated: #FDF8EE; --surface: #EDE3CF; --surface-2: #E4D7BE;
  --text: #3B2A17; --text-2: #6B5040; --text-3: #765E4B; --border: #DDCFB6;
  --accent: #8C5A2B; --accent-dim: rgba(140, 90, 43, .13); --accent-text: #6E431B;
}
[data-theme="dark"] {
  --bg: #18181B; --bg-elevated: #222226; --surface: #2A2A30; --surface-2: #34343B;
  --text: #ECE8E1; --text-2: #A6A29B; --text-3: #938F88; --border: #36353B;
  --accent: #A497DC; --accent-dim: rgba(164, 151, 220, .16); --accent-text: #BFB5EA; --on-accent: #16141F;
  --danger: #F2B8B5;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 14px rgba(0,0,0,.35);
  --shadow-heavy: 0 10px 40px rgba(0,0,0,.6);
  color-scheme: dark;
}
[data-theme="black"] {
  --bg: #000000; --bg-elevated: #121214; --surface: #1B1B1F; --surface-2: #26262B;
  --text: #D8D4CC; --text-2: #9A968F; --text-3: #8C8881; --border: #26252A;
  --accent: #9D90D6; --accent-dim: rgba(157, 144, 214, .16); --accent-text: #B8ADE6; --on-accent: #111016;
  --danger: #F2B8B5;
  --shadow: none; --shadow-heavy: 0 10px 40px rgba(0,0,0,.8);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html { height: 100%; -webkit-text-size-adjust: 100%; background: var(--bg); }

/* Installed on iOS the status bar text is always white (black-translucent), so give
   light themes a dark strip behind it. Elsewhere this area has zero height. */
@media (display-mode: standalone) {
  :root[data-theme="light"] body::before,
  :root[data-theme="sepia"] body::before {
    content: '';
    position: fixed; top: 0; left: 0; right: 0;
    height: env(safe-area-inset-top, 0px);
    background: rgba(28, 26, 24, .88);
    z-index: 200;
    pointer-events: none;
  }
}
body {
  height: 100%;
  overflow: hidden;
  font-family: var(--ui-font);
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; }
a { color: var(--accent-text); }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
[hidden] { display: none !important; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--text-3); }
.small { font-size: 13px; }
:focus-visible { outline: 2.5px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

#app { position: fixed; inset: 0; overflow: hidden; }

/* ---------- Screens ---------- */
.screen {
  position: absolute;
  inset: 0 0 calc(var(--tabbar-h) + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.scroll {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 0 max(16px, var(--safe-right)) 24px max(16px, var(--safe-left));
}
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(var(--safe-top) + 14px) max(16px, var(--safe-right)) 8px max(16px, var(--safe-left));
  flex-shrink: 0;
}
.topbar h1 { font-size: 30px; font-weight: 750; letter-spacing: -.02em; }
.topbar-actions { display: flex; gap: 2px; }

.icon-btn {
  width: 44px; height: 44px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 12px;
  color: var(--text-2);
  transition: background .15s, color .15s;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface); }
.icon-btn:active { background: var(--surface-2); }
.icon-btn[aria-pressed="true"], .icon-btn.active { color: var(--accent); }
.icon-btn[aria-pressed="true"] svg { fill: currentColor; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px;
  padding: 10px 18px;
  border-radius: var(--pill);
  font-weight: 600;
  font-size: 15px;
  background: var(--surface);
  color: var(--text);
  transition: filter .15s, transform .1s, background .15s;
  text-decoration: none;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: var(--on-accent); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn[disabled] { opacity: .5; pointer-events: none; }
.btn svg { width: 18px; height: 18px; }

/* ---------- Tab bar ---------- */
#tabbar {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--tabbar-h) + var(--safe-bottom));
  padding: 0 var(--safe-right) var(--safe-bottom) var(--safe-left);
  display: flex;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(16px);
  -webkit-backdrop-filter: saturate(1.4) blur(16px);
  border-top: 1px solid var(--border);
  z-index: 20;
}
#tabbar a {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--text-3);
  text-decoration: none;
  font-size: 11px; font-weight: 600;
}
#tabbar a[aria-current="page"] { color: var(--accent); }
#tabbar svg { width: 24px; height: 24px; }

/* ---------- Search & chips ---------- */
.search { position: relative; display: block; }
.search svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); width: 18px; height: 18px; color: var(--text-3); pointer-events: none; }
.search input {
  width: 100%;
  min-height: 42px;
  padding: 9px 12px 9px 38px;
  background: var(--surface);
  border: 1.5px solid transparent;
  border-radius: 12px;
  font-size: 16px;
  outline: none;
  transition: border-color .2s, background .2s;
}
.search input::placeholder { color: var(--text-3); }
.search input:focus { border-color: var(--accent); background: var(--bg-elevated); }

.chips { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; padding: 12px 0 4px; margin: 0 -16px; padding-inline: 16px; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0;
  min-height: 34px;
  padding: 6px 14px;
  border-radius: var(--pill);
  font-size: 14px; font-weight: 550;
  background: var(--surface);
  color: var(--text-2);
  border: 1.5px solid transparent;
  white-space: nowrap;
}
.chip.active, .chip[aria-checked="true"] { background: var(--accent-dim); color: var(--accent-text); border-color: var(--accent); }

.segmented { display: flex; background: var(--surface); border-radius: 12px; padding: 3px; gap: 2px; overflow-x: auto; scrollbar-width: none; }
.segmented button {
  flex: 1;
  min-height: 36px;
  padding: 6px 12px;
  border-radius: 9px;
  font-size: 14px; font-weight: 600;
  color: var(--text-2);
  white-space: nowrap;
}
.segmented button[aria-selected="true"], .segmented button[aria-checked="true"] { background: var(--bg-elevated); color: var(--text); box-shadow: var(--shadow); }
.segmented.small button { min-height: 32px; font-size: 13px; }

/* ---------- Library ---------- */
.stats-line { display: flex; align-items: center; gap: 14px; font-size: 13px; color: var(--text-3); margin: -2px 2px 8px; }
.stats-line strong { color: var(--text); font-weight: 650; }
.spark { display: inline-flex; align-items: flex-end; gap: 3px; height: 18px; margin-left: auto; }
.spark i { width: 5px; border-radius: 2px; background: var(--border); }
.spark i.on { background: var(--accent); }
.library-tools { padding: 4px 0 8px; }

.continue {
  display: flex; gap: 16px; align-items: center;
  margin: 6px 0 14px;
  padding: 14px;
  border-radius: 18px;
  background: var(--bg-elevated);
  box-shadow: var(--shadow);
  text-align: left;
  width: 100%;
}
.continue .cover { width: 72px; flex-shrink: 0; }
.continue-meta { min-width: 0; flex: 1; }
.continue-kicker { font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--accent-text); }
.continue-title { font-family: var(--book-font); font-size: 19px; font-weight: 600; line-height: 1.25; margin: 3px 0 2px; }
.continue-sub { font-size: 13px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.continue .bar { margin-top: 10px; }

.banner {
  display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: center;
  padding: 14px 16px;
  margin: 6px 0 12px;
  border-radius: 16px;
  background: var(--accent-dim);
  color: var(--text);
}
.banner p { flex: 1 1 220px; font-size: 14px; line-height: 1.45; }
.banner strong { display: block; font-size: 15px; margin-bottom: 2px; }
.banner .banner-actions { display: flex; gap: 8px; }
.banner .bar { flex-basis: 100%; }
.banner-icon { width: 44px; height: 44px; border-radius: 11px; flex: none; box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.install-lead { font-size: 15px; line-height: 1.5; color: var(--text-2); margin-bottom: 14px; }
.install-steps { counter-reset: step; list-style: none; padding: 0; margin: 0 0 8px; display: grid; gap: 12px; }
.install-steps li { counter-increment: step; position: relative; padding: 2px 0 0 40px; font-size: 16px; line-height: 1.5; }
.install-steps li::before { content: counter(step); position: absolute; left: 0; top: 0; width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; background: var(--accent-dim); color: var(--accent-text); font-weight: 700; font-size: 14px; }
.install-hint { display: block; font-size: 13px; color: var(--text-3); }
.inline-icon { width: 20px; height: 20px; vertical-align: -4px; fill: none; stroke: var(--accent-text); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.bar { height: 4px; border-radius: 2px; background: var(--border); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 2px; transition: width .4s var(--ease); }

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(118px, 1fr));
  gap: 22px 16px;
  padding-top: 8px;
}
@media (min-width: 700px) { .book-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 28px 22px; } }

.book-card { display: flex; flex-direction: column; gap: 8px; text-align: left; min-width: 0; position: relative; }
.book-card .cover { transition: transform .18s var(--ease), box-shadow .18s; }
.book-card:hover .cover { transform: translateY(-2px); }
.book-card:active .cover { transform: scale(.97); }
.book-card-title { font-size: 14px; font-weight: 600; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.book-card-sub { font-size: 12px; color: var(--text-3); display: flex; gap: 6px; align-items: center; margin-top: 2px; }
.book-card-sub .dot::before { content: '·'; }
.book-card .bar { height: 3px; }

.cover {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 6px 10px 10px 6px;
  overflow: hidden;
  box-shadow: var(--shadow);
  background: var(--surface);
}
.cover img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.cover-generated {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  padding: 14% 12% 14% 16%;
  background:
    linear-gradient(90deg, rgba(0,0,0,.22) 0 6%, rgba(255,255,255,.12) 6% 7.5%, transparent 7.5%),
    radial-gradient(120% 90% at 30% 10%, rgba(255,255,255,.14), transparent 60%),
    var(--cover);
  color: rgba(255,255,255,.96);
  text-align: center;
  container-type: inline-size;
}
.cover-title { font-family: var(--book-font); font-weight: 650; font-size: clamp(11px, 13cqi, 20px); line-height: 1.18; text-wrap: balance; text-shadow: 0 1px 2px rgba(0,0,0,.25); overflow-wrap: anywhere; }
.cover-rule { width: 28%; height: 1px; background: rgba(255,255,255,.55); }
.cover-author { font-size: clamp(9px, 8.5cqi, 13px); opacity: .85; letter-spacing: .02em; }
.cover-badge {
  position: absolute; right: 6px; top: 6px;
  width: 26px; height: 26px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.45);
  color: #fff;
  backdrop-filter: blur(6px);
}
.cover-badge svg { width: 15px; height: 15px; stroke-width: 2.2; }
.cover-badge.busy { animation: pulse 1.1s ease-in-out infinite; }
.cover-badge.done { background: var(--accent); }
@keyframes pulse { 50% { opacity: .45; } }

.empty { text-align: center; padding: 48px 20px; color: var(--text-3); line-height: 1.5; }
.empty strong { display: block; color: var(--text); font-size: 17px; margin-bottom: 6px; }
.empty .btn { margin-top: 16px; }

/* ---------- Discover ---------- */
.discover-tools { display: flex; flex-direction: column; gap: 10px; padding: 4px 0 10px; position: sticky; top: 0; background: var(--bg); z-index: 2; }
.source-note { font-size: 13px; color: var(--text-3); line-height: 1.45; }
.source-note a { color: var(--accent-text); }
.shelf { margin: 14px 0 22px; }
.shelf h2 { font-size: 19px; font-weight: 700; margin-bottom: 10px; display: flex; justify-content: space-between; align-items: baseline; }
.shelf-row { display: grid; grid-auto-flow: column; grid-auto-columns: 112px; gap: 14px; overflow-x: auto; padding-bottom: 6px; margin: 0 -16px; padding-inline: 16px; scrollbar-width: none; }
.shelf-row::-webkit-scrollbar { display: none; }
.result-list { display: flex; flex-direction: column; }
.result {
  display: flex; gap: 14px; align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  text-align: left;
  width: 100%;
}
.result .cover { width: 56px; flex-shrink: 0; border-radius: 4px 7px 7px 4px; }
.result-meta { flex: 1; min-width: 0; }
.result-title { font-weight: 600; font-size: 15px; line-height: 1.3; }
.result-sub { font-size: 13px; color: var(--text-3); margin-top: 2px; }
.result-state { font-size: 12px; font-weight: 600; color: var(--accent-text); }
.load-more { display: flex; justify-content: center; padding: 18px 0; }
.status-box { padding: 36px 12px; text-align: center; color: var(--text-3); line-height: 1.5; }
.status-box strong { color: var(--text); display: block; margin-bottom: 4px; }
.status-box .btn { margin-top: 14px; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 2.5px solid var(--border); border-top-color: var(--accent); animation: spin .8s linear infinite; margin: 0 auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* OPDS catalogs */
.add-feed { margin: 6px 0 18px; }
.add-feed-row { display: flex; gap: 8px; }
.add-feed-row input { flex: 1; min-width: 0; min-height: 44px; padding: 9px 12px; border-radius: 12px; border: 1.5px solid var(--border); background: var(--bg-elevated); font-size: 16px; }
.list-heading { font-size: 13px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--text-3); margin: 14px 4px 6px; }
.nav-list { display: flex; flex-direction: column; margin-bottom: 14px; }
.nav-row { display: flex; align-items: center; border-bottom: 1px solid var(--border); }
.nav-item { flex: 1; min-width: 0; text-align: left; padding: 13px 4px; display: flex; flex-direction: column; gap: 2px; }
.nav-item strong { font-weight: 600; font-size: 15.5px; }
.nav-item span { font-size: 13px; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.feed-head { display: flex; align-items: center; gap: 6px; margin: 2px -8px 8px; }
.feed-head h2 { font-size: 19px; font-weight: 700; }

/* Book details sheet */
.detail { display: flex; flex-direction: column; gap: 16px; }
.detail-head { display: flex; gap: 16px; align-items: flex-end; }
.detail-head .cover { width: 108px; flex-shrink: 0; }
.detail-title { font-family: var(--book-font); font-size: 22px; font-weight: 650; line-height: 1.2; }
.detail-author { color: var(--text-2); margin-top: 4px; }
.detail-facts { font-size: 13px; color: var(--text-3); margin-top: 6px; display: flex; flex-wrap: wrap; gap: 4px 10px; }
.detail-desc { line-height: 1.55; color: var(--text-2); font-size: 15px; }
.detail-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.detail-actions .btn.primary { flex: 1 1 180px; }
.detail-source { font-size: 12.5px; color: var(--text-3); line-height: 1.5; }

/* ---------- Settings ---------- */
.settings-group { margin: 8px 0 26px; }
.settings-group > h2 { font-size: 13px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--text-3); margin: 0 4px 8px; }
.card { background: var(--bg-elevated); border-radius: 16px; box-shadow: var(--shadow); overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; padding: 13px 16px; min-height: 52px; border-bottom: 1px solid var(--border); }
.row:last-child { border-bottom: 0; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-size: 15.5px; font-weight: 550; }
.row-sub { font-size: 13px; color: var(--text-3); line-height: 1.45; margin-top: 2px; }
.row input[type="url"], .row input[type="text"] {
  width: 100%; margin-top: 8px; padding: 9px 11px; border-radius: 10px; border: 1.5px solid var(--border); background: var(--bg); font-size: 15px;
}
.prose { line-height: 1.6; font-size: 14.5px; color: var(--text-2); padding: 14px 16px; }
.prose p + p { margin-top: 10px; }

.switch { position: relative; width: 50px; height: 30px; flex-shrink: 0; }
.switch input { position: absolute; inset: 0; opacity: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; border-radius: 15px; background: var(--surface-2); transition: background .2s; }
.switch span::after { content: ''; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .2s var(--ease); }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(20px); }
.switch input:focus-visible + span { outline: 2.5px solid var(--accent); outline-offset: 2px; }

/* ---------- Reading settings (type sheet) ---------- */
.type-section { margin-bottom: 22px; }
.type-label { font-size: 12px; font-weight: 700; letter-spacing: .07em; text-transform: uppercase; color: var(--text-3); margin-bottom: 9px; display: flex; justify-content: space-between; }
.type-label output { text-transform: none; letter-spacing: 0; font-weight: 600; color: var(--text-2); }
.stepper { display: flex; align-items: center; gap: 12px; }
.stepper button { width: 48px; height: 44px; border-radius: 12px; background: var(--surface); font-family: var(--book-font); font-weight: 600; font-size: 15px; }
.stepper button:last-child { font-size: 21px; }
.stepper input { flex: 1; }
.options { display: grid; grid-template-columns: repeat(auto-fit, minmax(92px, 1fr)); gap: 8px; }
.option {
  min-height: 44px; padding: 8px 10px;
  border-radius: 12px;
  border: 1.5px solid var(--border);
  font-size: 14px; color: var(--text-2);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
}
.option[aria-checked="true"] { border-color: var(--accent); background: var(--accent-dim); color: var(--accent-text); font-weight: 600; }
.theme-dot { width: 30px; height: 20px; border-radius: 6px; border: 1px solid rgba(128,128,128,.35); }
input[type="range"] { accent-color: var(--accent); width: 100%; min-height: 32px; }

/* ---------- Sheets ---------- */
#sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.38); opacity: 0; pointer-events: none; transition: opacity .3s var(--ease); z-index: 60; }
#sheet-backdrop.visible { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  margin: 0 auto;
  max-width: 640px;
  max-height: min(88dvh, 820px);
  display: flex; flex-direction: column;
  background: var(--bg-elevated);
  border-radius: 22px 22px 0 0;
  box-shadow: var(--shadow-heavy);
  z-index: 70;
  transform: translateY(105%);
  visibility: hidden;
  transition: transform .34s var(--ease), visibility 0s linear .34s;
  padding-bottom: var(--safe-bottom);
  outline: none;
}
.sheet.open { transform: none; visibility: visible; transition: transform .34s var(--ease); }
@media (min-width: 700px) {
  .sheet { bottom: auto; top: 50%; border-radius: 22px; transform: translateY(-40%) scale(.97); opacity: 0; transition: transform .25s var(--ease), opacity .25s, visibility 0s linear .25s; max-height: 80dvh; width: min(560px, 92vw); }
  .sheet.open { transform: translateY(-50%); opacity: 1; transition: transform .25s var(--ease), opacity .25s; }
}
.sheet-handle { width: 38px; height: 5px; border-radius: 3px; background: var(--border); margin: 9px auto 0; flex-shrink: 0; }
.sheet-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 12px 8px 20px; flex-shrink: 0; }
.sheet-title { font-size: 18px; font-weight: 700; }
.sheet-body { overflow-y: auto; padding: 4px 20px 24px; outline: none; overscroll-behavior: contain; }

.toc, .toc ol { list-style: none; }
.toc-link {
  display: block; width: 100%;
  text-align: left;
  padding: 11px 8px 11px calc(8px + var(--depth, 0) * 18px);
  border-radius: 10px;
  font-size: 15px; line-height: 1.35;
  color: var(--text);
}
.toc ol .toc-link { color: var(--text-2); font-size: 14.5px; }
.toc-link:hover { background: var(--surface); }
.toc-link.current { color: var(--accent-text); background: var(--accent-dim); font-weight: 650; }

.mark {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.mark-open { flex: 1; text-align: left; min-width: 0; }
.mark-label { font-size: 13px; font-weight: 650; color: var(--accent-text); }
.mark-text { font-family: var(--book-font); font-size: 15px; line-height: 1.45; color: var(--text); margin-top: 3px; }
.mark-meta { font-size: 12px; color: var(--text-3); margin-top: 4px; }

.search-results { list-style: none; margin-top: 12px; }
.search-results h3 { font-size: 12px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; color: var(--text-3); margin: 14px 0 4px; }
.search-hit { display: block; width: 100%; text-align: left; padding: 10px 6px; border-bottom: 1px solid var(--border); font-family: var(--book-font); font-size: 15px; line-height: 1.45; color: var(--text-2); }
.search-hit mark { background: var(--accent-dim); color: var(--text); border-radius: 3px; padding: 0 2px; }

/* ---------- Reader ---------- */
.reader {
  position: fixed; inset: 0;
  z-index: 40;
  background: var(--bg);
}
#reader-stage {
  position: absolute;
  inset: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}
#reader-stage foliate-view { display: block; width: 100%; height: 100%; }
.reader[data-flow="scrolled"] #reader-stage { top: calc(var(--safe-top) + 4px); }

.reader-bar {
  position: absolute; left: 0; right: 0;
  z-index: 5;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(1.3) blur(14px);
  -webkit-backdrop-filter: saturate(1.3) blur(14px);
  transition: transform .28s var(--ease), opacity .28s var(--ease);
}
.reader-top {
  top: 0;
  display: flex; align-items: center; gap: 2px;
  padding: calc(var(--safe-top) + 4px) max(6px, var(--safe-right)) 4px max(6px, var(--safe-left));
  border-bottom: 1px solid var(--border);
}
.reader-titles { flex: 1; min-width: 0; padding: 0 6px; }
.reader-title { font-size: 14.5px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader-chapter { font-size: 12px; color: var(--text-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader-bottom {
  bottom: 0;
  padding: 8px max(18px, var(--safe-right)) calc(var(--safe-bottom) + 10px) max(18px, var(--safe-left));
  border-top: 1px solid var(--border);
}
.reader-controls { display: flex; align-items: center; gap: 6px; margin: -4px -8px 2px; }
.reader-controls input { flex: 1; }
.reader-status { display: flex; justify-content: space-between; gap: 12px; font-size: 12.5px; color: var(--text-3); font-variant-numeric: tabular-nums; }
.reader-status span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reader-status span:last-child { flex-shrink: 0; }
.reader.chrome-hidden .reader-top { transform: translateY(-105%); opacity: 0; pointer-events: none; }
.reader.chrome-hidden .reader-bottom { transform: translateY(105%); opacity: 0; pointer-events: none; }

.reader-mini {
  position: absolute; left: 0; right: 0; bottom: calc(var(--safe-bottom) + 6px);
  display: flex; justify-content: space-between;
  padding: 0 max(22px, var(--safe-right)) 0 max(22px, var(--safe-left));
  font-size: 11px; color: var(--text-2);
  pointer-events: none;
  opacity: 0; transition: opacity .3s;
  font-variant-numeric: tabular-nums;
}
.reader.chrome-hidden[data-flow="paginated"] .reader-mini { opacity: 1; }
.reader-mini span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 65%; }

.reader-loading { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: var(--bg); z-index: 8; transition: opacity .25s; }
.reader-loading.done { opacity: 0; pointer-events: none; }

.tts-bar {
  position: absolute; left: 50%; bottom: calc(var(--safe-bottom) + 76px);
  transform: translateX(-50%);
  z-index: 6;
  display: flex; align-items: center; gap: 2px;
  padding: 4px 6px;
  border-radius: var(--pill);
  background: var(--bg-elevated);
  box-shadow: var(--shadow-heavy);
  transition: bottom .28s var(--ease);
}
.reader.chrome-hidden .tts-bar { bottom: calc(var(--safe-bottom) + 28px); }
.tts-bar .tts-play { color: var(--accent); }
.tts-bar .i-pause, .tts-bar.playing .i-play { display: none; }
.tts-bar.playing .i-pause { display: block; }
.voice-list { list-style: none; margin: 0 0 14px; padding: 0; display: grid; gap: 2px; }
.voice-list button { width: 100%; display: flex; align-items: center; gap: 12px; padding: 11px 12px; border-radius: 12px; text-align: left; color: var(--text); }
.voice-list button:hover { background: var(--accent-dim); }
.voice-list button[aria-checked="true"] { background: var(--accent-dim); }
.voice-list .voice-main { flex: 1; min-width: 0; }
.voice-list .voice-name { display: block; font-weight: 600; font-size: 16px; }
.voice-list .voice-sub { display: block; font-size: 13px; color: var(--text-3); }
.voice-badge { flex: none; font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; padding: 3px 7px; border-radius: 6px; background: var(--accent); color: var(--on-accent); }
.voice-check { flex: none; width: 20px; color: var(--accent-text); font-weight: 700; visibility: hidden; }
.voice-list button[aria-checked="true"] .voice-check { visibility: visible; }
.voice-tip { font-size: 14px; line-height: 1.5; color: var(--text-2); padding: 12px 14px; border-radius: 12px; background: var(--surface-2, var(--accent-dim)); }
.tts-rate { min-width: 44px; height: 36px; border-radius: 10px; font-weight: 650; font-size: 14px; color: var(--text-2); font-variant-numeric: tabular-nums; }

.audio-bar {
  position: absolute; left: 50%; bottom: calc(var(--safe-bottom) + 76px);
  transform: translateX(-50%);
  width: min(440px, calc(100% - 24px));
  z-index: 6;
  display: flex; flex-direction: column; gap: 2px;
  padding: 8px 8px 4px;
  border-radius: 20px;
  background: var(--bg-elevated);
  box-shadow: var(--shadow-heavy);
  transition: bottom .28s var(--ease);
}
.reader.chrome-hidden .audio-bar { bottom: calc(var(--safe-bottom) + 28px); }
.audio-meta { display: flex; justify-content: space-between; gap: 12px; padding: 0 8px; font-size: 12px; color: var(--text-2); }
.audio-track { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; color: var(--text); }
.audio-time { flex: none; font-variant-numeric: tabular-nums; }
.audio-meta { align-items: center; }
.audio-track { flex: 1; min-width: 0; }
.audio-align { flex: none; width: 30px; height: 30px; margin: -6px -4px -6px 0; display: grid; place-items: center; border-radius: 8px; color: var(--text-2); }
.audio-align svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; }
.audio-align[aria-pressed="true"] { color: var(--on-accent); background: var(--accent); }
.audio-link { flex: none; font-size: 12px; font-weight: 600; color: var(--accent-text, var(--accent)); text-decoration: underline; }
.audio-bar.aligning .audio-track { color: var(--accent-text, var(--accent)); }
.reader.audio-aligning #reader-stage { cursor: crosshair; }
#audio-seek { width: auto; margin: 2px 8px; accent-color: var(--accent); }
.audio-buttons { display: flex; align-items: center; justify-content: space-between; }
.audio-bar .audio-play { color: var(--accent); }
.audio-bar .i-pause, .audio-bar.playing .i-play { display: none; }
.audio-bar.playing .i-pause { display: block; }
#audio-save.busy { color: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
.audio-sync {
  position: absolute; left: 50%; top: -40px; transform: translateX(-50%);
  padding: 7px 14px; border-radius: var(--pill);
  background: var(--accent); color: var(--on-accent, #fff);
  font-size: 13px; font-weight: 600; white-space: nowrap;
  box-shadow: var(--shadow-heavy);
}
.audio-sync[hidden] { display: none; }
@media (max-width: 360px) { #audio-back, #audio-fwd { display: none; } }

.hl-pop {
  position: absolute; left: 0; top: 0;
  z-index: 9;
  display: flex; align-items: center; gap: 2px;
  padding: 5px;
  border-radius: 14px;
  background: var(--bg-elevated);
  box-shadow: var(--shadow-heavy);
  border: 1px solid var(--border);
}
.hl-pop.docked {
  left: 12px; right: 12px; margin-inline: auto; width: max-content; max-width: calc(100% - 24px);
  top: auto; bottom: calc(var(--safe-bottom) + 14px);
  overflow-x: auto; scrollbar-width: none;
}
.hl-pop.docked.dock-top { top: calc(var(--safe-top) + 12px); bottom: auto; }
.hl-pop button { width: 40px; height: 40px; border-radius: 10px; display: flex; align-items: center; justify-content: center; color: var(--text-2); }
.hl-pop button:hover { background: var(--surface); }
.hl-pop i { width: 22px; height: 22px; border-radius: 50%; background: var(--c); box-shadow: inset 0 0 0 1px rgba(0,0,0,.12); }
.hl-pop [aria-pressed="true"] i { box-shadow: 0 0 0 2.5px var(--bg-elevated), 0 0 0 4.5px var(--c); }
.hl-pop svg { width: 20px; height: 20px; }
.hl-sep { width: 1px; height: 24px; background: var(--border); margin: 0 3px; }
.hl-pop:not(.existing) .hl-del, .hl-pop:not(.word) .hl-define { display: none; }
.share-preview { display: flex; justify-content: center; background: var(--surface); border-radius: 14px; padding: 12px; margin-bottom: 12px; }
.share-preview img { max-width: 100%; max-height: 42dvh; border-radius: 6px; box-shadow: var(--shadow); }
.share-options { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.landing-quote { font-family: var(--book-font); font-size: 21px; line-height: 1.5; padding: 4px 0 4px 16px; border-left: 3px solid var(--accent); margin: 4px 0 14px; }
.landing-cite { color: var(--text-2); line-height: 1.45; margin-bottom: 16px; font-family: var(--book-font); }
.footnote-text { font-family: var(--book-font); font-size: 16px; line-height: 1.55; margin-bottom: 8px; }
.footnote-text p + p { margin-top: .6em; }
.define-word { font-family: var(--book-font); font-size: 24px; font-weight: 650; margin-bottom: 8px; }
.define-entry { margin: 10px 0 14px; }
.define-pos { font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--accent-text); margin-bottom: 4px; }
.define-entry ol { padding-left: 20px; line-height: 1.5; color: var(--text); }
.define-entry li + li { margin-top: 4px; }
.define-example { font-family: var(--book-font); font-style: italic; color: var(--text-2); margin-top: 6px; }
.note-quote { font-family: var(--book-font); font-size: 15px; line-height: 1.5; color: var(--text-2); border-left: 3px solid var(--accent); padding: 2px 0 2px 12px; margin-bottom: 12px; max-height: 8.5em; overflow: auto; }
#note-text { width: 100%; padding: 12px; border-radius: 12px; border: 1.5px solid var(--border); background: var(--bg); font-size: 16px; line-height: 1.5; resize: vertical; }
#note-text:focus { outline: none; border-color: var(--accent); }
.mark-swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.mark-note { font-size: 14px; color: var(--text-2); margin-top: 6px; line-height: 1.45; white-space: pre-wrap; }
.marks-toolbar { display: flex; justify-content: flex-end; gap: 8px; margin: 2px 0 6px; }

/* ---------- Autopilot ---------- */
.auto-ui { position: absolute; inset: 0; pointer-events: none; z-index: 6; }
.auto-track { position: absolute; left: 0; right: 0; bottom: var(--safe-bottom); height: 4px; background: color-mix(in srgb, var(--text) 8%, transparent); z-index: 6; }
#auto-fill { height: 100%; background: var(--accent); transform-origin: left; transform: scaleX(0); }
.auto-ui[data-mode="scroll"] .auto-track { display: none; }
.auto-ui.held #auto-fill { background: #D97706; animation: pulse 1s ease-in-out infinite; }
.auto-ui.paused #auto-fill { opacity: .45; }
/* The pill and status chip live in the empty margin bands above and below the
   text, so they never cover what you're reading (paginated has 44px bands built
   in; scroll mode reserves matching bands while Autopilot runs). */
.auto-pill {
  position: absolute; left: 12px; right: 12px; bottom: calc(var(--safe-bottom) + 5px);
  margin-inline: auto; width: max-content;
  height: 36px;
  display: flex; align-items: center; gap: 0;
  padding: 0 4px;
  border-radius: 18px;
  background: var(--bg-elevated);
  box-shadow: 0 1px 3px rgba(0,0,0,.12), 0 0 0 1px var(--border);
  opacity: 0; pointer-events: none;
  transition: opacity .25s;
  z-index: 7;
}
.auto-pill.visible { opacity: 1; pointer-events: auto; }
.auto-pill .icon-btn { width: 36px; height: 36px; }
.auto-pill .icon-btn svg { width: 18px; height: 18px; }
.auto-pill .i-play, .auto-ui.paused .auto-pill .i-pause { display: none; }
.auto-ui.paused .auto-pill .i-play { display: block; }
#auto-toggle { color: var(--accent); }
.auto-step { width: 34px; height: 34px; border-radius: 10px; font-size: 20px; line-height: 1; color: var(--text-2); }
.auto-step:hover { background: var(--surface); }
.auto-wpm { min-width: 72px; text-align: center; font-weight: 650; font-size: 14.5px; font-variant-numeric: tabular-nums; }
.auto-status {
  position: absolute; left: 12px; right: 12px; top: calc(var(--safe-top) + 8px);
  margin-inline: auto; width: max-content; max-width: calc(100% - 24px);
  height: 28px; line-height: 28px; padding: 0 12px;
  border-radius: 14px;
  background: var(--bg-elevated);
  box-shadow: 0 0 0 1px var(--border);
  font-size: 12.5px; font-weight: 600; color: var(--text-2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.auto-status:empty { display: none; }
.auto-ui.held .auto-status { color: #B45309; box-shadow: 0 0 0 1.5px #D97706; }
/* the pill replaces the chapter/percent line while it's showing */
.reader.auto-pill-on .reader-mini { opacity: 0 !important; }
.reader.autopilot[data-flow="scrolled"] #reader-stage { top: calc(var(--safe-top) + 44px); bottom: calc(var(--safe-bottom) + 44px); }

/* ---------- RSVP ---------- */
.rsvp {
  position: fixed; inset: 0;
  z-index: 90;
  background: var(--bg);
  display: flex; flex-direction: column;
  transform: translateY(100%);
  visibility: hidden;
  transition: transform .3s var(--ease), visibility 0s linear .3s;
}
.rsvp.open { transform: none; visibility: visible; transition: transform .3s var(--ease); }
.rsvp-top { display: flex; align-items: center; justify-content: space-between; padding: calc(var(--safe-top) + 8px) 12px 8px; }
.spacer-40 { width: 44px; }
.rsvp-speed { display: flex; align-items: center; gap: 6px; background: var(--surface); border-radius: var(--pill); padding: 4px; }
.rsvp-speed button { width: 36px; height: 36px; border-radius: 50%; font-size: 22px; color: var(--text-2); }
.rsvp-speed span { min-width: 76px; text-align: center; font-weight: 650; font-variant-numeric: tabular-nums; }
.rsvp-stage { flex: 1; display: flex; flex-direction: column; justify-content: center; position: relative; padding: 0 16px; min-height: 0; cursor: pointer; }
.rsvp-line { position: relative; height: 1px; background: var(--border); margin: 0 -16px; }
.rsvp-line.top::after { content: ''; position: absolute; left: 50%; top: 0; width: 2px; height: 10px; background: var(--accent); transform: translateX(-1px); }
.rsvp-line.bottom::after { content: ''; position: absolute; left: 50%; bottom: 0; width: 2px; height: 10px; background: var(--accent); transform: translateX(-1px); }
.rsvp-word {
  display: grid; grid-template-columns: 1fr auto 1fr;
  font-family: var(--book-font);
  font-size: clamp(30px, 8.5vw, 52px);
  line-height: 1.5;
  padding: .35em 0;
  white-space: pre;
}
#rsvp-left { text-align: right; }
#rsvp-pivot { color: var(--accent); font-weight: 700; }
#rsvp-right { text-align: left; }
.rsvp-context {
  position: absolute; left: 16px; right: 16px; bottom: 12px;
  text-align: center;
  font-family: var(--book-font); font-size: 15px; line-height: 1.5; color: var(--text-3);
  transition: opacity .2s;
}
.rsvp-context mark { background: var(--accent-dim); color: var(--text); border-radius: 3px; padding: 0 2px; }
.rsvp.playing .rsvp-context { opacity: 0; }
.rsvp-progress { padding: 0 24px 8px; }
.rsvp-progress .track { height: 3px; background: var(--border); border-radius: 2px; overflow: hidden; }
#rsvp-fill { height: 100%; background: var(--accent); width: 0; }
#rsvp-label { text-align: center; font-size: 12px; color: var(--text-3); margin-top: 6px; font-variant-numeric: tabular-nums; }
.rsvp-controls { display: flex; justify-content: center; align-items: center; gap: 10px; padding: 6px 12px; }
.rsvp-controls button { width: 52px; height: 52px; border-radius: 50%; background: var(--surface); color: var(--text-2); display: flex; align-items: center; justify-content: center; }
.rsvp-controls button.play { width: 68px; height: 68px; background: var(--accent); color: var(--on-accent); }
.rsvp .i-pause, .rsvp.playing .i-play { display: none; }
.rsvp.playing .i-pause { display: block; }
.rsvp-presets { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; padding: 10px 16px calc(var(--safe-bottom) + 18px); }
.rsvp-preset { padding: 7px 14px; border-radius: var(--pill); border: 1.5px solid var(--border); font-size: 14px; color: var(--text-2); min-height: 36px; }
.rsvp-preset.active { border-color: var(--accent); background: var(--accent-dim); color: var(--accent-text); font-weight: 600; }

/* ---------- Toast, drop, update ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px);
  transform: translate(-50%, 20px);
  display: flex; align-items: center; gap: 14px;
  max-width: min(92vw, 520px);
  padding: 11px 18px;
  border-radius: 14px;
  background: var(--text);
  color: var(--bg);
  font-size: 14.5px; font-weight: 500;
  box-shadow: var(--shadow-heavy);
  opacity: 0; pointer-events: none;
  transition: opacity .25s, transform .25s var(--ease);
  z-index: 120;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#toast button { color: inherit; font-weight: 700; text-decoration: underline; }
body.reading #toast { bottom: calc(var(--safe-bottom) + 90px); }
body.reading .reader.autopilot ~ #toast, body.reading:has(.reader.autopilot) #toast { bottom: calc(var(--safe-bottom) + 150px); }

#drop-overlay { position: fixed; inset: 0; z-index: 130; display: none; align-items: center; justify-content: center; background: color-mix(in srgb, var(--accent) 22%, transparent); backdrop-filter: blur(4px); }
#drop-overlay div { padding: 28px 36px; border-radius: 20px; border: 2.5px dashed var(--accent); background: var(--bg-elevated); font-weight: 650; font-size: 17px; }
body.dragging #drop-overlay { display: flex; }

#update-banner {
  position: fixed; left: 50%; top: calc(var(--safe-top) + 10px); transform: translateX(-50%);
  display: flex; gap: 14px; align-items: center;
  padding: 10px 12px 10px 18px;
  border-radius: 14px;
  background: var(--bg-elevated); box-shadow: var(--shadow-heavy);
  font-size: 14px; z-index: 125;
}
#update-banner button { padding: 7px 14px; border-radius: var(--pill); background: var(--accent); color: var(--on-accent); font-weight: 650; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
