/* ---------- accent palettes ----------
   Each has a deep shade for light mode (white text on it stays readable)
   and a soft shade for dark mode. Sage is the default. */
:root,
:root[data-accent="sage"]     { --accent-l: #4d6b50; --accent-d: #a7c4a4; --on-accent-d: #0f1a10; }
:root[data-accent="ocean"]    { --accent-l: #2d5f8b; --accent-d: #8fbbe3; --on-accent-d: #0b1724; }
:root[data-accent="orchid"]   { --accent-l: #b0279f; --accent-d: #e98bf0; --on-accent-d: #1f0a1f; }
:root[data-accent="rose"]     { --accent-l: #b23a5b; --accent-d: #f0a0b6; --on-accent-d: #23090f; }
:root[data-accent="clay"]     { --accent-l: #a5522f; --accent-d: #eba882; --on-accent-d: #23120a; }
:root[data-accent="graphite"] { --accent-l: #3f4652; --accent-d: #c3cad6; --on-accent-d: #14171c; }

/* ---------- tokens ---------- */
:root {
  --bg: #fafafb;
  --surface: #ffffff;
  --surface-2: #f1f1f4;
  --text: #18181b;
  --muted: #6b6b76;
  --faint: #a1a1ab;
  --border: #e4e4e9;
  --accent: var(--accent-l);
  --accent-soft: color-mix(in srgb, var(--accent-l) 12%, transparent);
  --on-accent: #ffffff;
  --danger: #c0392b;
  --shadow: 0 1px 2px rgba(20, 16, 24, 0.04), 0 8px 24px rgba(20, 16, 24, 0.06);
  --radius: 14px;
  --word-size: 56px;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111013;
    --surface: #1a191d;
    --surface-2: #232127;
    --text: #ecebef;
    --muted: #9b98a3;
    --faint: #5f5c66;
    --border: #2c2a31;
    --accent: var(--accent-d);
    --accent-soft: color-mix(in srgb, var(--accent-d) 15%, transparent);
    --on-accent: var(--on-accent-d);
    --danger: #ff7b6b;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #111013;
  --surface: #1a191d;
  --surface-2: #232127;
  --text: #ecebef;
  --muted: #9b98a3;
  --faint: #5f5c66;
  --border: #2c2a31;
  --accent: var(--accent-d);
  --accent-soft: color-mix(in srgb, var(--accent-d) 15%, transparent);
  --on-accent: var(--on-accent-d);
  --danger: #ff7b6b;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
  color-scheme: dark;
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
/* Stop iPhone/iPad zooming in when a button (like + / − on the speed) is tapped twice quickly. Pinch zoom still works. */
html, body, button, input, select, textarea, label, summary, .stage, .controls, .speed, .wpm { touch-action: manipulation; }
/* …and from zooming in when a text box or menu gets focus (it does that below 16px). */
@media (pointer: coarse) {
  select, textarea, input[type="text"], input[type="search"], input[type="password"] { font-size: 16px !important; }
}
@media (hover: none), (pointer: coarse) { .desktop-only { display: none !important; } }

main {
  flex: 1;
  width: 100%;
  max-width: 880px;
  margin: 0 auto;
  padding: 0 16px 32px;
  display: flex;
  flex-direction: column;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 880px;
  margin: 0 auto;
  padding: 14px 16px;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: 0;
  padding: 4px;
  margin-left: -4px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 650;
  font-size: 17px;
  letter-spacing: -0.01em;
}
.brand .logo { display: block; width: 28px; height: 28px; color: var(--accent); font-family: var(--sans); }
.wordmark { letter-spacing: -0.015em; }
.wordmark .fix { color: var(--accent); }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 40px;
  padding: 0 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 550;
  font-size: 15px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); background: var(--surface-2); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; filter: none; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 22px; height: 22px; }

/* ---------- home ---------- */
.intro { padding: clamp(24px, 6vh, 64px) 0 clamp(28px, 5vh, 44px); max-width: 640px; }
.intro h1 {
  font-size: clamp(22px, 7vw, 46px);
  white-space: nowrap;
  line-height: 1.08;
  letter-spacing: -0.025em;
  margin: 0 0 14px;
  font-weight: 720;
}
.intro h1 .accent { color: var(--accent); }
.link {
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
  color: var(--text);
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: color-mix(in srgb, var(--text) 30%, transparent);
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}
.link:hover { color: var(--accent); text-decoration-color: currentColor; }
.intro h1 { margin-bottom: 16px; }
/* "How it works": a quiet pill under the headline that opens the explainer. */
.pill-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 36px;
  padding: 0 14px 0 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 550;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.pill-btn svg { width: 18px; height: 18px; color: var(--accent); }
@media (hover: hover) { .pill-btn:hover { border-color: color-mix(in srgb, var(--text) 35%, var(--border)); } }

/* How it works (popup) */
.how-body { display: grid; gap: 16px; color: var(--muted); font-size: 15px; text-wrap: pretty; }
.how-body p { margin: 0; }
.how-body strong { color: var(--text); font-weight: 600; }
.steps { display: grid; gap: 10px; margin: 0; padding: 0; list-style: none; counter-reset: step; }
.steps li { position: relative; padding-left: 36px; counter-increment: step; }
.steps li::before {
  content: counter(step);
  position: absolute;
  left: 0;
  top: 0;
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 13px;
  font-weight: 700;
}
.intro-note { font-size: 14px; }


.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/* Source choices: flat tiles, one primary (solid accent), one secondary (outlined) */
.choices { display: grid; grid-template-columns: 2fr 2fr 1.4fr; gap: 12px; }
.choice {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "icon arrow" "title title" "sub sub";
  align-items: start;
  padding: 18px 20px 20px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  text-align: left;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.choice:hover { border-color: color-mix(in srgb, var(--text) 35%, var(--border)); }
.choice-icon { grid-area: icon; width: 24px; height: 24px; margin-bottom: 28px; }
.choice-arrow {
  grid-area: arrow;
  width: 20px;
  height: 20px;
  opacity: 0.45;
  transition: transform 0.2s ease, opacity 0.2s ease;
}
.choice:hover .choice-arrow { transform: translateX(3px); opacity: 1; }
.choice-title { grid-area: title; font-size: 18px; font-weight: 650; letter-spacing: -0.01em; }
.choice-sub { grid-area: sub; margin-top: 2px; font-size: 14px; color: var(--muted); }

.choice--primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.choice--primary:hover { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 90%, var(--text)); }
.choice--primary .choice-sub { color: color-mix(in srgb, var(--on-accent) 72%, transparent); }
.choice--primary .choice-arrow { opacity: 0.7; }

/* Sample: the quiet third option */
.choice--ghost { background: transparent; }
.choice--ghost:hover { background: var(--surface); }

@media (max-width: 760px) {
  .choices { grid-template-columns: 1fr 1fr; }
  .choice--ghost {
    grid-column: 1 / -1;
    grid-template-columns: auto 1fr auto;
    grid-template-areas: "icon title arrow" "icon sub arrow";
    column-gap: 14px;
    align-items: center;
    padding: 14px 20px;
  }
  .choice--ghost .choice-icon { margin: 0; }
}

@media (max-width: 520px) {
  .choices { grid-template-columns: 1fr; gap: 10px; }
  .choice {
    grid-template-columns: auto 1fr auto;
    grid-template-areas: "icon title arrow" "icon sub arrow";
    column-gap: 14px;
    align-items: center;
    padding: 16px 18px;
  }
  .choice-icon { margin: 0; }
}

/* Library */
.library { margin-top: 8px; }

/* "Add a book or text": only shown (as a collapsible row) once there's a library. */
.actions:not(.has-library) > .actions-summary { display: none; }
.actions.has-library { margin: 0 0 12px; }
.actions-summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  font-weight: 600;
  cursor: pointer;
  list-style: none;
  transition: border-color 0.15s;
}
.actions-summary::-webkit-details-marker { display: none; }
.actions-summary:hover { border-color: color-mix(in srgb, var(--text) 35%, var(--border)); }
/* Under the book list: a quieter dashed "add" row. */
.actions.has-library > .actions-summary { border-style: dashed; background: transparent; color: var(--muted); }
.actions.has-library > .actions-summary:hover { color: var(--text); background: var(--surface); }
.actions-label { display: inline-flex; align-items: center; gap: 10px; }
.actions-label svg { width: 20px; height: 20px; color: var(--accent); }
.actions-chevron { width: 20px; height: 20px; color: var(--muted); transition: transform 0.2s ease; }
.actions[open] .actions-chevron { transform: rotate(180deg); }
.actions.has-library[open] > .actions-summary { margin-bottom: 12px; }
.actions[open] .choices { animation: fade-in 0.2s ease; }
/* Returning readers don't need the demo: just Open a file and Paste text, side by side. */
.actions.has-library .choice--ghost { display: none; }
@media (min-width: 521px) {
  .actions.has-library .choices { grid-template-columns: 1fr 1fr; }
}
.library.is-hidden { display: none; }
.section-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; margin: 0 2px 14px; }
.section-title { display: flex; flex-direction: column; gap: 2px; }
.text-btn {
  padding: 6px 10px;
  margin-right: -8px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--accent);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
@media (hover: hover) { .text-btn:hover { background: var(--surface-2); } }

/* Selecting books */
.select-bar {
  position: sticky;
  top: 8px;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 12px;
  padding: 8px 8px 8px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.select-bar .spacer { flex: 1; }
.select-count { color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.select-bar .btn { height: 36px; padding: 0 12px; font-size: 14px; gap: 4px; }
.select-bar .btn svg { width: 16px; height: 16px; }
.select-all { font-size: 14px; font-weight: 550; white-space: nowrap; }
@media (max-width: 520px) { .select-all > span:last-child { display: none; } }

/* Checkboxes (round, accent when ticked) */
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; position: relative; }
.check input { position: absolute; opacity: 0; width: 1px; height: 1px; margin: 0; }
.check-box {
  flex: none;
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border: 1.5px solid var(--faint);
  border-radius: 50%;
  background: var(--surface);
  transition: background 0.15s, border-color 0.15s;
}
.check-box::after {
  content: "";
  width: 10px;
  height: 6px;
  margin-top: -2px;
  border: solid var(--on-accent);
  border-width: 0 0 2px 2px;
  transform: rotate(-45deg);
  opacity: 0;
}
.check input:checked + .check-box { background: var(--accent); border-color: var(--accent); }
.check input:checked + .check-box::after { opacity: 1; }
.check input:indeterminate + .check-box { background: var(--accent); border-color: var(--accent); }
.check input:indeterminate + .check-box::after { opacity: 1; width: 10px; height: 0; margin: 0; border-width: 0 0 2px; transform: none; }
.check input:focus-visible + .check-box { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Options menu */
.menu-wrap { position: relative; }
.menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 10;
  min-width: 210px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: 0 12px 32px rgba(10, 6, 14, 0.18);
  animation: fade-in 0.12s ease;
}
.menu button {
  display: block;
  width: 100%;
  padding: 9px 12px;
  border: 0;
  border-radius: 8px;
  background: none;
  text-align: left;
  font-size: 15px;
  cursor: pointer;
}
.menu button:hover, .menu button:focus-visible { background: var(--surface-2); outline: none; }
.menu button.danger { color: var(--danger); }
.menu hr { margin: 6px 4px; border: 0; border-top: 1px solid var(--border); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger:hover { filter: brightness(1.06); background: var(--danger); }
.section-head h2 { margin: 0; font-size: 20px; font-weight: 680; letter-spacing: -0.015em; }
.section-count { color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
/* Search and sort (more than 3 books) */
.library-tools { display: flex; gap: 10px; margin: 0 0 12px; }
.search {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--muted);
}
.search:focus-within { border-color: var(--accent); }
.search svg { flex: none; width: 18px; height: 18px; }
.search input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  background: none;
  font-size: 15px;
  color: var(--text);
  outline: none;
}
.search input::placeholder { color: var(--faint); }
#library-sort {
  flex: none;
  height: 42px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 15px;
}
.library-empty { margin: 0; padding: 24px 16px; border: 1px dashed var(--border); border-radius: var(--radius); color: var(--muted); text-align: center; font-size: 15px; }

.book-list { list-style: none; margin: 0; padding: 0; overflow: hidden; }
.book { display: flex; align-items: center; transition: background 0.15s; }
.book + .book { border-top: 1px solid var(--border); }
.book:hover { background: var(--surface-2); }
.book-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 8px 16px 20px;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}
.book-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.book-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-meta { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.book-bar { display: block; height: 3px; margin-top: 6px; border-radius: 99px; background: var(--border); overflow: hidden; max-width: 240px; }
.book-bar span { display: block; height: 100%; border-radius: inherit; background: var(--accent); }
.book-cta {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
  transition: color 0.15s;
}
.book-cta svg { width: 16px; height: 16px; transition: transform 0.2s ease; }
.book:hover .book-cta { color: var(--text); }
.book:hover .book-cta svg { transform: translateX(3px); }
/* Selecting: a checkbox at the end of each row, shown on hover (desktop) or while selecting. */
.book-check { flex: none; justify-content: center; width: 44px; height: 44px; margin-right: 6px; }
.book.is-selected { background: var(--accent-soft); }
.library:not(.selecting) .book-check { opacity: 0; pointer-events: none; transition: opacity 0.15s; }
.library.selecting .book-cta { display: none; }
@media (hover: none) { .library:not(.selecting) .book-check { display: none; } }
@media (hover: hover) {
  .library:not(.selecting) .book:hover .book-check,
  .library:not(.selecting) .book-check:focus-within { opacity: 1; pointer-events: auto; }
}
@media (max-width: 520px) {
  .book-cta { display: none; }
  .book-main { padding-left: 16px; }
}

/* Paste panel */
.input { padding: 6px; }
.input:not([hidden]) { animation: fade-in 0.2s ease; }
.input textarea {
  display: block;
  width: 100%;
  min-height: 220px;
  height: 36vh;
  resize: vertical;
  border: 0;
  background: transparent;
  padding: 14px 14px 10px;
  font-size: 15px;
  line-height: 1.6;
  outline: none;
}
.input textarea::placeholder { color: var(--faint); }
.input-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-top: 1px solid var(--border);
}
.input-actions .spacer { flex: 1; }
@media (max-width: 520px) { .input-actions #start-btn { flex: 1; } }


/* ---------- reader ---------- */
#reader { flex: 1; display: flex; flex-direction: column; }
#reader:not([hidden]) { animation: fade-in 0.25s ease; }
@keyframes fade-in { from { opacity: 0; } }

/* Book title with the chapter picker beneath it, both on the left. */
.reader-head {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 10px;
  min-height: 40px;
}
.doc-title {
  align-self: stretch;
  text-align: left;
  max-width: 100%;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#chapter-select {
  width: min(100%, 440px);
  margin-bottom: 8px;
  height: 36px;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 14px;
}

.stage {
  flex: 1;
  min-height: 260px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  outline: none;
}

.reticle { width: 100%; max-width: 760px; overflow-x: clip; }

.guide { position: relative; height: 16px; }
.guide::before {
  content: "";
  position: absolute;
  left: 0; right: 0;
  height: 1.5px;
  background: var(--border);
}
.guide::after {
  content: "";
  position: absolute;
  left: 50%;
  width: 2px;
  height: 12px;
  background: var(--faint);
  transform: translateX(-50%);
}
.guide.top::before { top: 0; }
.guide.top::after { top: 0; }
.guide.bottom::before { bottom: 0; }
.guide.bottom::after { bottom: 0; }

/* The pivot letter sits in the middle column; the equal 1fr columns either
   side keep its centre locked to the centre of the screen. */
.word {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: baseline;
  font-size: var(--word-size);
  line-height: 1.25;
  /* Fixed height (line + padding), so the rail never changes size when a long word is shrunk to fit. */
  height: 1.81em;
  padding: 0.28em 0;
  align-content: center;
  white-space: pre;
  font-family: var(--sans);
  font-weight: 500;
  letter-spacing: 0.005em;
}
.word[data-font="serif"] { font-family: var(--serif); font-weight: 500; }
.word[data-font="mono"] { font-family: var(--mono); font-weight: 450; letter-spacing: 0; }
.word .l { display: flex; justify-content: flex-end; }
.word .r { display: flex; justify-content: flex-start; }
.word .pivot { color: var(--accent); font-weight: 700; }
/* Long words: only the letters shrink (--fit is set per word), the box keeps its size. */
.word > * { font-size: calc(var(--fit, 1) * 1em); }

/* Under the word: the play hint when paused, a chapter countdown while playing. */
.stage-sub { display: grid; margin-top: 22px; min-height: 22px; text-align: center; }
.stage-hint, .stage-time { grid-area: 1 / 1; transition: opacity 0.25s; }
.stage-hint { color: var(--faint); font-size: 14px; }
.stage-time {
  opacity: 0;
  color: var(--muted);
  font-size: 15px;
  font-weight: 550;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.01em;
}
body.is-playing .stage-hint { opacity: 0; }
body.is-playing .stage-time { opacity: 0.7; }

/* Context: the surrounding text, shown while paused, collapsed by default.
   While playing it's invisible but keeps its space, so pausing doesn't shift the layout. */
.disclosure { margin: 0; }
.stats-panel { margin-bottom: 10px; }
body.is-playing .context-panel { visibility: hidden; }
.disclosure summary {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px 6px 6px;
  margin-left: -6px;
  border-radius: 8px;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
  list-style: none;
  user-select: none;
  -webkit-user-select: none;
}
.disclosure summary::-webkit-details-marker { display: none; }
@media (hover: hover) { .disclosure summary:hover { color: var(--text); background: var(--surface-2); } }
.disclosure summary svg { width: 16px; height: 16px; transition: transform 0.2s ease; }
.disclosure[open] summary svg { transform: rotate(90deg); }
.disclosure .when-open, .disclosure[open] .when-closed { display: none; }
.disclosure[open] .when-open { display: inline; }
.disclosure[open] > :not(summary) { animation: fade-in 0.2s ease; }

.context {
  position: relative;
  max-height: 28vh;
  overflow-y: auto;
  padding: 14px 18px;
  margin: 6px 0 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--muted);
  font-size: 15px;
  line-height: 1.7;
  overscroll-behavior: contain;
}
.context p { margin: 0 0 0.8em; }
.context p:last-child { margin-bottom: 0; }
.context span[data-i] { cursor: pointer; border-radius: 4px; }
.context span[data-i]:hover { color: var(--text); background: var(--surface-2); }
.context span.cur { color: var(--text); background: var(--accent-soft); box-shadow: 0 0 0 2px var(--accent-soft); }

.controls { padding-bottom: env(safe-area-inset-bottom); }

#progress {
  width: 100%;
  margin: 0;
  accent-color: var(--accent);
  cursor: pointer;
}
/* Progress (in its accordion): time left above the word count. */
.stats {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 4px 0 4px 22px;
  color: var(--muted);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}
.stat-chapter { color: var(--text); font-weight: 550; }

.buttons {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 14px;
}
.transport { display: flex; align-items: center; gap: 4px; }
.play {
  display: grid;
  place-items: center;
  width: 58px;
  height: 58px;
  margin: 0 6px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  color: var(--on-accent);
  cursor: pointer;
  box-shadow: 0 6px 18px var(--accent-soft);
  transition: transform 0.12s, filter 0.15s;
}
.play:hover { filter: brightness(1.07); }
.play:active { transform: scale(0.96); }
.play svg { width: 26px; height: 26px; }
.play .i-pause { display: none; }
body.is-playing .play .i-play { display: none; }
body.is-playing .play .i-pause { display: block; }

.speed {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
}
.speed .icon-btn { width: 38px; height: 38px; }
.wpm { min-width: 76px; text-align: center; font-variant-numeric: tabular-nums; line-height: 1.1; }
.wpm span { font-weight: 650; font-size: 18px; }
.wpm small { display: block; color: var(--muted); font-size: 11px; letter-spacing: 0.04em; }

.side-controls { display: flex; align-items: center; gap: 8px; }
.ai-download { display: grid; gap: 8px; }
/* ElevenLabs key */
.cloud-setup { display: grid; gap: 10px; }
.field-help { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.5; }
.field-help a { color: var(--text); text-underline-offset: 3px; }
.key-form { display: flex; gap: 8px; }
.key-form input {
  flex: 1;
  min-width: 0;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 15px;
}
.key-form input:focus { outline: none; border-color: var(--accent); }
.field-error { margin: 0; color: var(--danger); font-size: 13px; }
.cloud-key-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; color: var(--muted); font-size: 13px; line-height: 1.45; }
.cloud-key-row .text-btn { flex: none; margin-right: -10px; font-size: 14px; }
.field select { width: 100%; }
.ai-download .btn { width: 100%; height: 44px; }
.ai-download progress { width: 100%; height: 6px; accent-color: var(--accent); }
.aloud-btn[aria-pressed="true"] { color: var(--accent); background: var(--accent-soft); }
.field select {
  height: 40px;
  padding: 0 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 15px;
}

@media (max-width: 560px) {
  .buttons { justify-content: center; }
  .transport { width: 100%; justify-content: center; }
  /* Centre the position and time-left lines over the centred controls. */
  .disclosure { text-align: center; }
  .context-panel .context { text-align: left; }
  .stats { margin-left: 0; align-items: center; }
}

/* Phones: book title on its own line, chapter picker full width beneath it. */
@media (max-width: 640px) {
  .reader-head { gap: 8px; padding-top: 4px; }
  #chapter-select { max-width: none; width: 100%; height: 40px; }
}

/* ---------- settings: modal on desktop, bottom sheet on mobile ---------- */
.topbar-actions { display: flex; align-items: center; gap: 2px; margin-right: -8px; }
body:has(dialog[open]) { overflow: hidden; }

.sheet {
  width: min(460px, calc(100% - 32px));
  max-width: none;
  height: min(640px, calc(100dvh - 48px));
  max-height: none;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 24px 64px rgba(10, 6, 14, 0.28);
  overflow: hidden;
}
.sheet[open] { display: flex; flex-direction: column; animation: modal-in 0.2s ease-out; }
.sheet::backdrop { background: rgba(12, 10, 14, 0.45); backdrop-filter: blur(2px); animation: fade-in 0.2s ease; }
.sheet-inner { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.sheet-handle { display: none; }
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 12px 6px 22px;
}
.sheet-head h2 { margin: 0; font-size: 18px; font-weight: 650; letter-spacing: -0.01em; }
.sheet-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px 22px 22px;
}

.sheet--fit { height: fit-content; max-height: min(760px, calc(100dvh - 48px)); }
/* Every popup has the same top bar: title, close button and a dividing line
   (in Settings, the line sits under the tabs instead). */
.sheet:not(:has(.tabs)) .sheet-head { padding-bottom: 12px; border-bottom: 1px solid var(--border); }
.sheet:not(:has(.tabs)) .sheet-body { padding-top: 18px; }

.tabs {
  flex: none;
  display: flex;
  gap: 4px;
  padding: 6px 16px 0;
  border-bottom: 1px solid var(--border);
}
.tab {
  position: relative;
  padding: 10px 8px 12px;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 15px;
  font-weight: 550;
  cursor: pointer;
}
.tab + .tab { margin-left: 8px; }
.tab:hover { color: var(--text); }
.tab[aria-selected="true"] { color: var(--text); }
.tab[aria-selected="true"]::after {
  content: "";
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: -1px;
  height: 2px;
  border-radius: 2px;
  background: var(--accent);
}
.tab:focus-visible { outline-offset: -2px; border-radius: 8px; }

@keyframes modal-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
@keyframes sheet-up { from { transform: translateY(100%); } }

@media (max-width: 640px) {
  .sheet {
    width: 100%;
    height: min(88dvh, 620px);
    margin: auto 0 0;
    border-radius: 20px 20px 0 0;
    border-width: 1px 0 0;
  }
  .sheet[open] { animation: sheet-up 0.28s cubic-bezier(0.2, 0.8, 0.2, 1); }
  .sheet-handle {
    display: block;
    flex: none;
    width: 38px;
    height: 5px;
    margin: 8px auto 0;
    border-radius: 99px;
    background: var(--border);
  }
  .sheet-head { padding-top: 4px; }
  .sheet--fit { height: fit-content; max-height: 88dvh; }
  .tabs { padding: 0 12px; }
  .tab { flex: 1; }
  .tab + .tab { margin-left: 0; }
  .sheet-body { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
}

/* Live preview of the reading style */
.preview {
  margin: 16px 0 4px;
  padding: 8px 0;
  border-radius: 12px;
  background: var(--bg);
  overflow: hidden;
}
.preview .guide::before { left: 18px; right: 18px; }
.preview .word { padding: 0.18em 0; height: 1.61em; }

.group { display: grid; gap: 20px; padding: 18px 0 4px; }
.field { display: flex; flex-direction: column; gap: 8px; font-size: 15px; }
.field-row { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.field output { color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
.field input[type="range"] { width: 100%; height: 24px; margin: 0; accent-color: var(--accent); cursor: pointer; }

/* Switch */
.toggle { display: flex; align-items: center; justify-content: space-between; gap: 16px; cursor: pointer; }
.toggle-text { display: flex; flex-direction: column; gap: 2px; font-size: 15px; }
.toggle-text small { color: var(--muted); font-size: 13px; line-height: 1.4; }
.toggle input {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 44px;
  height: 26px;
  margin: 0;
  border-radius: 999px;
  background: var(--faint);
  cursor: pointer;
  transition: background 0.15s;
}
.toggle input::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
  transition: transform 0.15s;
}
.toggle input:checked { background: var(--accent); }
.toggle input:checked::before { transform: translateX(18px); }

/* Accent swatches */
.swatches { display: flex; flex-wrap: wrap; gap: 10px; }
.swatch { position: relative; cursor: pointer; }
.swatch input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }
.swatch span {
  display: block;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--sw-l);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
  transition: transform 0.15s;
}
.swatch:hover span { transform: scale(1.08); }
.swatch input:checked + span { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--sw-l); }
.swatch input:focus-visible + span { outline: 2px solid var(--text); outline-offset: 4px; }
:root[data-theme="dark"] .swatch span { --sw-l: var(--sw-d); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .swatch span { --sw-l: var(--sw-d); }
}

/* Segmented control */
.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 3px;
  padding: 3px;
  border-radius: 11px;
  background: var(--surface-2);
}
.segmented label { position: relative; }
.segmented input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }
.segmented span {
  display: block;
  padding: 8px;
  border-radius: 8px;
  text-align: center;
  font-size: 14px;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
}
.segmented label:hover span { color: var(--text); }
.segmented input:checked + span {
  background: var(--surface);
  color: var(--text);
  font-weight: 550;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 0 0 1px var(--border);
}
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Locked (DRM) book */
.drm-body { display: grid; gap: 14px; }
.drm-lead { margin: 0; color: var(--muted); font-size: 15px; text-wrap: pretty; }
.drm-options { display: grid; gap: 10px; margin: 0; padding: 0; list-style: none; counter-reset: drm; }
.drm-options li {
  position: relative;
  display: grid;
  gap: 2px;
  padding: 12px 14px 12px 50px;
  border: 1px solid var(--border);
  border-radius: 12px;
  counter-increment: drm;
  font-size: 14px;
  color: var(--muted);
}
.drm-options li::before {
  content: counter(drm);
  position: absolute;
  left: 14px;
  top: 12px;
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 13px;
  font-weight: 700;
}
.drm-options strong { color: var(--text); font-size: 15px; font-weight: 600; }
.drm-options a { color: var(--text); text-underline-offset: 3px; }
.drm-options a:hover { color: var(--accent); }
.drm-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
@media (max-width: 520px) { .drm-actions .btn { flex: 1; } }

.keys { display: grid; gap: 10px; margin: 0; font-size: 14px; color: var(--muted); }
.keys div { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.keys dt, .keys dd { margin: 0; }

kbd {
  display: inline-block;
  min-width: 22px;
  padding: 1px 6px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--surface);
  font: 12px/1.5 var(--sans);
  text-align: center;
  color: var(--text);
}

/* Focus mode: fade the interface while words are flashing. */
.chrome { transition: opacity 0.35s ease; }
body.is-playing .chrome { opacity: 0.08; }
body.is-playing .chrome:has(:focus-visible) { opacity: 1; }
@media (hover: hover) and (pointer: fine) {
  body.is-playing .chrome:hover { opacity: 1; }
}
body.is-playing .stage { cursor: none; }

/* Natural voice loader: covers the reader while chapter audio is prepared */
.voice-loader {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 24px;
  background: color-mix(in srgb, var(--bg) 94%, transparent);
  backdrop-filter: blur(4px);
  animation: fade-in 0.2s ease;
}
.voice-loader-card { width: min(100%, 320px); display: grid; justify-items: center; gap: 14px; text-align: center; }
.voice-loader-spinner {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.voice-loader-text { margin: 0; font-weight: 600; }
.voice-loader-bar { width: 100%; height: 6px; border-radius: 99px; background: var(--border); overflow: hidden; }
.voice-loader-bar span { display: block; height: 100%; width: var(--p, 0%); border-radius: inherit; background: var(--accent); transition: width 0.3s ease; }
.voice-loader-bar.indeterminate span { width: 35%; animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(290%); } }
.voice-loader-pct { margin: -6px 0 0; min-height: 20px; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }

/* Drag and drop */
.drop-overlay {
  position: fixed;
  inset: 12px;
  display: grid;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: 20px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  color: var(--accent);
  font-size: 22px;
  font-weight: 650;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
  z-index: 10;
}
body.dragging .drop-overlay { opacity: 1; }

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