/* ── Bundled reading fonts (work offline) ────────────────────────────────── */
@font-face {
  font-family: 'Lexend';
  src: url('../../vendor/fonts/Lexend.woff2') format('woff2');
  font-weight: 100 900;
  font-display: swap;
}
@font-face {
  font-family: 'Atkinson Hyperlegible';
  src: url('../../vendor/fonts/AtkinsonHyperlegible-400.woff2') format('woff2');
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: 'Atkinson Hyperlegible';
  src: url('../../vendor/fonts/AtkinsonHyperlegible-700.woff2') format('woff2');
  font-weight: 700;
  font-display: swap;
}
@font-face {
  font-family: 'OpenDyslexic';
  src: url('../../vendor/fonts/OpenDyslexic-Regular.woff') format('woff');
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: 'OpenDyslexic';
  src: url('../../vendor/fonts/OpenDyslexic-Bold.woff') format('woff');
  font-weight: 700;
  font-display: swap;
}

/* ── Design tokens ───────────────────────────────────────────────────────── */
:root {
  --accent: #cc2222;
  --accent-dark: #aa1a1a;
  --bg: #f0ede8;
  --card: #fff;
  --text: #333;
  --muted: #6b6b6b;
  --faint: #757575;
  --border: #e4e0da;
  --border-strong: #ccc;
  --hover: #f5f1ec;
  --hover-accent: #fff8f8;
  --input-bg: #fafafa;
  --input-border: #ddd;
  --ok: #2a8a3a;
}

body.dark {
  --accent: #e04848;
  --accent-dark: #c93535;
  --bg: #121214;
  --card: #1d1d22;
  --text: #e4e4ea;
  --muted: #a0a0ab;
  --faint: #8a8a95;
  --border: #2c2c33;
  --border-strong: #3a3a44;
  --hover: #26262d;
  --hover-accent: #2a2025;
  --input-bg: #16161a;
  --input-border: #3a3a44;
  --ok: #5fbf6f;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Lexend', system-ui, -apple-system, 'Segoe UI', sans-serif;
  background: var(--bg);
  min-height: 100vh;
  color: var(--text);
}

/* ── Keyboard focus — always visible ─────────────────────────────────────── */
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
a:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* ── Reduced motion ──────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition: none !important;
    animation: none !important;
    scroll-behavior: auto !important;
  }
}

/* ── Header ──────────────────────────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1.25rem;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  gap: 1rem;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.logo-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
}

.logo-name {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.02em;
}

.logo-tagline {
  font-size: 0.8rem;
  color: var(--muted);
  font-style: italic;
  display: none;
}

@media (min-width: 560px) { .logo-tagline { display: block; } }

.header-actions {
  display: flex;
  align-items: center;
  gap: 14px;
}

.theme-row {
  display: flex;
  align-items: center;
  gap: 7px;
}

.theme-row > span {
  font-size: 12px;
  color: var(--muted);
}

.theme-btn {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2.5px solid var(--border-strong);
  cursor: pointer;
  transition: border-color 0.18s, transform 0.12s;
  padding: 0;
  flex-shrink: 0;
}

.theme-btn.active { border-color: var(--accent); transform: scale(1.12); }
.theme-btn:hover:not(.active) { border-color: var(--muted); }

/* Custom colour swatch — a rainbow ring wrapping a hidden colour input */
.theme-btn-custom {
  position: relative;
  display: inline-block;
  background: conic-gradient(#e66, #ee6, #6e6, #6ee, #66e, #e6e, #e66);
  overflow: hidden;
}

.theme-btn-custom input[type=color] {
  position: absolute;
  inset: -4px;
  opacity: 0;
  cursor: pointer;
}

#darkToggle {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
  background: var(--card);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  transition: background 0.14s;
}

#darkToggle:hover { background: var(--hover); }

/* ── Main layout ─────────────────────────────────────────────────────────── */
main {
  max-width: 800px;
  margin: 0 auto;
  padding: 1.25rem 1rem 2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* ── Hero (dismissable) ──────────────────────────────────────────────────── */
.hero {
  position: relative;
  text-align: center;
  padding: 0.5rem 2.25rem 0.25rem;
}

.hero.hidden { display: none; }

.hero h1 {
  font-size: 1.45rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin-bottom: 0.35rem;
}

.hero p {
  font-size: 0.95rem;
  color: var(--muted);
  line-height: 1.6;
}

.hero a { color: var(--accent); white-space: nowrap; }

.hero-close {
  position: absolute;
  top: -2px;
  right: 0;
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  border-radius: 50%;
  transition: background 0.14s, color 0.14s;
}

.hero-close:hover { background: var(--hover); color: var(--text); }

/* ── Reader card — pinned to the top so the words & play controls
      stay visible while the settings, input, and library scroll below ────── */
.reader-card {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--card);
  border-radius: 14px;
  padding: 1.1rem 1.5rem 1rem;
  box-shadow: 0 2px 16px rgba(0,0,0,0.07);
}

/* Settings live in their own card that scrolls under the pinned reader */
.settings-card {
  background: var(--card);
  border-radius: 14px;
  padding: 1.1rem 1.5rem 1.25rem;
  box-shadow: 0 2px 16px rgba(0,0,0,0.07);
}

/* ── Reader box (rendering lane) ─────────────────────────────────────────── */
#readerBox {
  position: relative;
  width: 100%;
  height: 130px;
  border-radius: 10px;
  overflow: hidden;
  transition: background 0.3s;
  margin-bottom: 0.5rem;
}

/* Each reading theme sets its background and word colours */
.theme-white { background: #fafafa; --word: #363636; --word-ph: #0d0d0d; --dot: #cc2222; }
.theme-cream { background: #f5f0e0; --word: #363636; --word-ph: #0d0d0d; --dot: #cc2222; }
.theme-blue  { background: #e8f2f8; --word: #363636; --word-ph: #0d0d0d; --dot: #cc2222; }
.theme-dark  { background: #16161a; --word: #c9c9d1; --word-ph: #ffffff; --dot: #ff5a5a; }

#wordLayer {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  overflow: hidden;
}

#ghostLeft, #ghostRight, #mainWord {
  position: absolute;
  top: 18px;
  white-space: nowrap;
  font-family: 'Trebuchet MS', 'Helvetica Neue', sans-serif;
  font-size: 56px;
  font-weight: 500;
  line-height: 1;
  color: var(--word);
}

#mainWord span { display: inline-block; font-weight: 500; color: var(--word); }
#mainWord span.ph { font-weight: 900; color: var(--word-ph); }

#ghostLeft, #ghostRight { opacity: 0.22; }

#focalDotEl {
  position: absolute;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--dot, var(--accent));
  z-index: 10;
}

/* ── Sentence view — whole sentence shown, current words highlighted ─────── */
#readerBox.sentence-mode {
  height: auto;
  min-height: 130px;
}

#sentenceLayer {
  display: none;
  position: relative;
  padding: 18px 22px;
  text-align: center;
  line-height: 1.7;
  font-weight: 500;
}

#sentenceLayer .sw { color: var(--word); }

#sentenceLayer .sw.cur {
  color: var(--word-ph);
  font-weight: 700;
  border-bottom: 3px solid var(--dot, var(--accent));
}

/* ── Progress ────────────────────────────────────────────────────────────── */
#speedInfo {
  font-size: 12px;
  color: var(--faint);
  text-align: center;
  min-height: 16px;
  margin-bottom: 0.2rem;
}

#progressText {
  font-size: 13px;
  color: var(--muted);
  text-align: center;
  margin-bottom: 0.6rem;
}

/* Scrubber — drag to seek, or use the Back / Forward / Sentence buttons */
.scrubber {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  height: 12px;
  border-radius: 6px;
  background: var(--border);
  margin-bottom: 1rem;
  cursor: pointer;
}

.scrubber::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid var(--card);
  box-shadow: 0 1px 5px rgba(0,0,0,0.3);
}

.scrubber::-moz-range-thumb {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid var(--card);
}

/* ── Controls — big, touch-friendly, no fine dragging ────────────────────── */
#controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-bottom: 1.1rem;
}

/* Two/three settings side by side on wide screens, stacked on narrow */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 14px;
}

.setting { display: flex; flex-direction: column; gap: 7px; min-width: 0; }

.setting-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

/* Stepper: [ − ] value [ + ] */
.stepper {
  display: flex;
  align-items: stretch;
  height: 48px;
  border: 1.5px solid var(--input-border);
  border-radius: 12px;
  overflow: hidden;
  background: var(--input-bg);
}

.step-btn {
  flex: 0 0 52px;
  border: none;
  background: var(--card);
  color: var(--accent);
  font-size: 26px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  transition: background 0.12s;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.step-btn:hover { background: var(--hover); }
.step-btn:active { background: var(--accent); color: #fff; }
.step-btn:disabled { color: var(--faint); opacity: 0.4; cursor: default; background: var(--card); }

.step-val {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  border-left: 1.5px solid var(--input-border);
  border-right: 1.5px solid var(--input-border);
  white-space: nowrap;
}

.step-unit { color: var(--muted); font-weight: 500; font-size: 14px; margin-left: 2px; }

/* Segmented control: a row of connected choice buttons */
.seg {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.seg-btn {
  flex: 1 1 auto;
  min-height: 46px;
  padding: 8px 14px;
  border: 1.5px solid var(--input-border);
  border-radius: 11px;
  background: var(--card);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  touch-action: manipulation;
}

.seg-btn:hover { border-color: var(--accent); }

.seg-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

/* Toggle chips for on/off reading aids */
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  min-height: 44px;
  padding: 8px 16px;
  border: 1.5px solid var(--input-border);
  border-radius: 22px;
  background: var(--card);
  color: var(--muted);
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  touch-action: manipulation;
}

.chip:hover { border-color: var(--accent); }

.chip .chip-check { display: none; }

.chip.on {
  border-color: var(--accent);
  background: var(--hover-accent);
  color: var(--accent);
  font-weight: 600;
}

.chip.on .chip-check { display: inline; }

.chapter-row select {
  font: inherit;
  color: var(--text);
  background: var(--input-bg);
  border: 1.5px solid var(--input-border);
  border-radius: 8px;
  padding: 8px 10px;
  min-height: 44px;
  cursor: pointer;
}

/* ── Play buttons (sit directly under the words, always visible) ─────────── */
#btnRow {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin-top: 0.2rem;
}

#btnRow button {
  min-height: 50px;
  padding: 10px 20px;
  font-size: 15px;
  font-family: inherit;
  border: 1.5px solid var(--border-strong);
  border-radius: 11px;
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  transition: background 0.14s;
  touch-action: manipulation;
}

#btnRow button:hover { background: var(--hover); }

#btnPlay {
  background: var(--accent) !important;
  color: #fff !important;
  border-color: var(--accent) !important;
  min-width: 130px;
  font-weight: 600;
}

#btnPlay:hover { background: var(--accent-dark) !important; }
#btnPlay.playing { background: #555 !important; border-color: #555 !important; }
#btnPlay.playing:hover { background: #333 !important; }

.shortcuts {
  text-align: center;
  font-size: 11px;
  color: var(--faint);
  margin-top: 0.25rem;
}

#statsLine {
  text-align: center;
  font-size: 11.5px;
  color: var(--faint);
  margin-top: 0.4rem;
  min-height: 14px;
}

/* ── Input card ──────────────────────────────────────────────────────────── */
.input-card {
  background: var(--card);
  border-radius: 14px;
  box-shadow: 0 2px 16px rgba(0,0,0,0.07);
  overflow: hidden;
}

.input-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}

.input-tab {
  flex: 1;
  padding: 0.8rem 0.4rem;
  font-size: 13px;
  font-family: inherit;
  background: none;
  border: none;
  border-bottom: 2.5px solid transparent;
  cursor: pointer;
  color: var(--muted);
  transition: color 0.15s, border-color 0.15s;
  text-align: center;
}

.input-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

.input-tab:hover:not(.active) { color: var(--text); }

.input-panel {
  padding: 1.25rem;
  display: none;
}

.input-panel.active { display: block; }

/* ── Paste textarea ──────────────────────────────────────────────────────── */
#pasteTextarea {
  width: 100%;
  height: 110px;
  font-size: 15px;
  padding: 10px;
  border: 1px solid var(--input-border);
  border-radius: 7px;
  resize: vertical;
  font-family: inherit;
  line-height: 1.55;
  color: var(--text);
  background: var(--input-bg);
  transition: border-color 0.18s;
}

#pasteTextarea:focus { outline: none; border-color: var(--accent); }

.panel-hint {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 8px;
}

.start-btn {
  margin-top: 10px;
  padding: 10px 22px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 7px;
  font-size: 14px;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.14s;
}

.start-btn:hover { background: var(--accent-dark); }

/* ── File upload areas ───────────────────────────────────────────────────── */
.file-upload-area {
  display: block;
  border: 2px dashed var(--input-border);
  border-radius: 10px;
  padding: 1.75rem 1rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.18s, background 0.18s;
  margin-bottom: 0.75rem;
}

.file-upload-area:hover,
.file-upload-area:focus-within {
  border-color: var(--accent);
  background: var(--hover-accent);
}

.file-upload-area input[type=file] {
  /* visually hidden but still focusable for keyboard users */
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.file-upload-icon { font-size: 2rem; margin-bottom: 0.4rem; line-height: 1; }
.file-upload-text { font-size: 14px; color: var(--text); }
.file-upload-hint { font-size: 12px; color: var(--muted); margin-top: 4px; }

.file-status {
  font-size: 13px;
  color: var(--muted);
  min-height: 20px;
  text-align: center;
}

.chapter-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-top: 0.6rem;
  font-size: 13px;
  color: var(--muted);
}

.chapter-row select { max-width: 70%; }

.status-loading { color: var(--muted); }
.status-ok      { color: var(--ok); font-weight: 500; }
.status-error   { color: var(--accent); }

/* ── Library card ────────────────────────────────────────────────────────── */
.library-card {
  background: var(--card);
  border-radius: 14px;
  box-shadow: 0 2px 16px rgba(0,0,0,0.07);
  overflow: hidden;
}

.library-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 1rem 1.25rem;
  cursor: pointer;
  user-select: none;
  transition: background 0.14s;
  background: none;
  border: none;
  font-family: inherit;
  color: var(--text);
}

.library-header:hover { background: var(--hover); }

.library-title { font-size: 14px; font-weight: 600; }
.library-toggle { font-size: 12px; color: var(--muted); }

.library-list { padding: 0 1.25rem 1rem; display: none; }
.library-list.open { display: block; }

.lib-empty { font-size: 13px; color: var(--muted); padding: 0.5rem 0; }

.library-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0;
  border-top: 1px solid var(--border);
}

.library-item-info { flex: 1; min-width: 0; }

.library-item-title {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.library-item-meta { font-size: 12px; color: var(--muted); margin-top: 2px; }

.library-item-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.lib-btn {
  padding: 6px 12px;
  font-size: 12px;
  border-radius: 5px;
  border: 1px solid var(--input-border);
  background: var(--card);
  cursor: pointer;
  color: var(--text);
  transition: background 0.13s;
  font-family: inherit;
}

.lib-btn:hover { background: var(--hover); }

.lib-btn-primary {
  border-color: var(--accent);
  color: var(--accent);
}

.lib-btn-primary:hover { background: var(--hover-accent); }

.lib-btn-del {
  color: var(--muted);
  border-color: var(--border);
  padding: 6px 9px;
}

.lib-btn-del:hover { color: var(--accent); border-color: var(--accent); background: var(--hover-accent); }

/* ── Footer ──────────────────────────────────────────────────────────────── */
footer {
  max-width: 800px;
  margin: 0 auto;
  padding: 0 1rem 2.5rem;
  text-align: center;
  font-size: 12.5px;
  color: var(--muted);
}

footer a { color: var(--accent); }

.footer-nav {
  margin-bottom: 6px;
  font-size: 13px;
}

/* ── Text pages (How it works, About, Accessibility, Privacy) ────────────── */
.doc-card {
  background: var(--card);
  border-radius: 14px;
  padding: 1.5rem;
  box-shadow: 0 2px 16px rgba(0,0,0,0.07);
  line-height: 1.7;
  font-size: 15px;
}

.doc-card h1 { font-size: 1.4rem; margin-bottom: 1rem; }
.doc-card h2 { font-size: 1.05rem; margin: 1.4rem 0 0.5rem; }
.doc-card p + p { margin-top: 0.6rem; }
.doc-card ul { padding-left: 1.4rem; margin-top: 0.4rem; }
.doc-card li { margin-bottom: 0.35rem; }
.doc-card .back-link { display: inline-block; margin-bottom: 1rem; color: var(--accent); }

.doc-illus {
  display: block;
  margin: 0.75rem auto 1rem;
  max-width: 100%;
  border-radius: 10px;
}

/* ── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 520px) {
  header { padding: 0.7rem 0.9rem; }
  main { padding: 0.75rem 0.75rem 2rem; gap: 0.75rem; }
  .reader-card { padding: 0.75rem 0.85rem 0.85rem; box-shadow: 0 4px 14px rgba(0,0,0,0.12); }
  .settings-card { padding: 1rem 1rem 1.1rem; }
  #readerBox { height: 96px; }
  #speedInfo { display: none; }
  #btnRow button { flex: 1 1 40%; padding: 10px 12px; }
  .settings-grid { grid-template-columns: 1fr; }
  .theme-btn { width: 30px; height: 30px; }
  .input-tab { font-size: 12px; padding: 0.7rem 0.25rem; }
  .input-panel { padding: 1rem; }
  .library-item { flex-wrap: wrap; }
  .library-item-actions { width: 100%; }
}
