:root {
  color-scheme: dark;
  --bg: #101210;
  --surface: #181b18;
  --surface-2: #202420;
  --text: #f3f0e8;
  --muted: #92988f;
  --faint: #343a34;
  --accent: #e85d4a;
  --accent-contrast: #fff8ef;
  --shadow: rgba(0, 0, 0, 0.38);
  --word-size: clamp(3rem, 8.5vw, 7.5rem);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body { min-height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  transition: background-color 240ms ease, color 240ms ease;
}

body[data-palette="paper"] {
  color-scheme: light;
  --bg: #f3eee4;
  --surface: #fcf9f2;
  --surface-2: #e8dfd1;
  --text: #26231f;
  --muted: #746e64;
  --faint: #d2c8b8;
  --accent: #b84635;
  --accent-contrast: #fffaf2;
  --shadow: rgba(66, 50, 32, 0.18);
}

body[data-palette="moss"] {
  --bg: #11211b;
  --surface: #183027;
  --surface-2: #204034;
  --text: #eef2de;
  --muted: #9aaa91;
  --faint: #315043;
  --accent: #e3b95c;
  --accent-contrast: #172017;
  --shadow: rgba(0, 10, 6, 0.38);
}

body[data-palette="dusk"] {
  --bg: #171625;
  --surface: #211f34;
  --surface-2: #2d2943;
  --text: #f1edf5;
  --muted: #a39caf;
  --faint: #3b3751;
  --accent: #db8eac;
  --accent-contrast: #241724;
  --shadow: rgba(5, 3, 15, 0.42);
}

button, input, textarea, select { font: inherit; }
button { color: inherit; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.topbar {
  position: fixed;
  inset: 0 0 auto;
  z-index: 5;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: 74px;
  padding: 0 32px;
}

.topbar nav { justify-self: end; display: flex; align-items: center; gap: 10px; }
.home-link, .quiet-button { color: var(--muted); font-size: .78rem; letter-spacing: .08em; text-decoration: none; text-transform: uppercase; }
.home-link { justify-self: start; transition: color 160ms ease; }
.home-link:hover, .quiet-button:hover { color: var(--text); }

.brand, .quiet-button, .icon-button, .play-button, .skip-button, .dialog-close {
  border: 0;
  background: none;
  cursor: pointer;
}

.brand { display: flex; align-items: center; gap: 9px; padding: 8px; font-family: Georgia, "Times New Roman", serif; font-size: 1.02rem; letter-spacing: .02em; }
.brand img { width: 26px; height: 34px; }
.brand strong { color: var(--accent); font-weight: 600; }
.quiet-button { padding: 10px; }
.icon-button { display: grid; width: 38px; height: 38px; place-items: center; border: 1px solid var(--faint); border-radius: 50%; }
.icon-button svg { width: 18px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-width: 1.5; }

.reader-shell {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  padding: 15vh 24px 42px;
}

.source-heading { min-height: 64px; text-align: center; }
.source-heading p, .eyebrow { margin: 0 0 8px; color: var(--accent); font-size: .68rem; font-weight: 700; letter-spacing: .18em; text-transform: uppercase; }
.source-heading h1 { margin: 0; font-family: Georgia, "Times New Roman", serif; font-size: clamp(1.05rem, 2vw, 1.35rem); font-weight: 400; }

.word-stage {
  position: relative;
  display: grid;
  width: min(1120px, 100%);
  min-height: clamp(190px, 37vh, 340px);
  place-items: center;
  overflow: hidden;
}

.word {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: baseline;
  width: 100%;
  font-family: Georgia, "Times New Roman", serif;
  font-size: var(--word-size);
  font-variant-ligatures: none;
  letter-spacing: -.01em;
  line-height: 1;
  white-space: pre;
}

.word-before { justify-self: end; }
.word-after { justify-self: start; }
.word-anchor { color: var(--accent); font-weight: 700; }
.word.long-word { font-size: clamp(2.5rem, 7vw, 6rem); }
.word.very-long-word { font-size: clamp(1.85rem, 5.4vw, 4.4rem); }

.focus-guide {
  position: absolute;
  left: 50%;
  width: 1px;
  height: 34px;
  background: var(--accent);
  opacity: .75;
}
.focus-guide::after { position: absolute; left: -3px; width: 7px; height: 1px; background: var(--accent); content: ""; }
.focus-guide-top { top: 12%; }
.focus-guide-top::after { bottom: 0; }
.focus-guide-bottom { bottom: 12%; }
.focus-guide-bottom::after { top: 0; }

.progress-track { width: min(560px, 80vw); height: 2px; overflow: hidden; background: var(--faint); }
.progress-track span { display: block; width: 0; height: 100%; background: var(--accent); transition: width 120ms linear; }
.position { min-height: 18px; margin: 11px 0 25px; color: var(--muted); font-size: .72rem; letter-spacing: .04em; }

.reader-controls { display: flex; align-items: center; gap: 28px; }
.play-button { display: grid; width: 58px; height: 58px; place-items: center; border-radius: 50%; background: var(--text); color: var(--bg); box-shadow: 0 10px 28px var(--shadow); transition: transform 160ms ease; }
.play-button:hover { transform: scale(1.04); }
.play-button svg { grid-area: 1 / 1; width: 22px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.8; }
.play-button .play-icon { margin-left: 2px; fill: currentColor; stroke: none; }
.play-button .pause-icon, .play-button.playing .play-icon { display: none; }
.play-button.playing .pause-icon { display: block; }
.skip-button { position: relative; display: grid; place-items: center; width: 44px; height: 44px; color: var(--muted); }
.skip-button svg { width: 29px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.3; }
.skip-button span { position: absolute; font-size: .56rem; font-weight: 700; }
.skip-button:hover { color: var(--text); }

.speed-control { width: min(380px, 80vw); margin-top: 38px; }
.speed-control label { display: flex; justify-content: space-between; color: var(--muted); font-size: .73rem; letter-spacing: .04em; }
.speed-control output { color: var(--text); font-weight: 650; }
.speed-control input { width: 100%; height: 22px; margin: 8px 0 0; accent-color: var(--accent); cursor: pointer; }
.range-labels { display: flex; justify-content: space-between; color: var(--muted); font-size: .6rem; }

dialog {
  width: min(680px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  padding: 34px;
  overflow: auto;
  border: 1px solid var(--faint);
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 28px 90px var(--shadow);
}
dialog::backdrop { background: color-mix(in srgb, var(--bg) 82%, transparent); backdrop-filter: blur(6px); }
dialog[open] { animation: dialog-in 180ms ease-out; }
@keyframes dialog-in { from { opacity: 0; transform: translateY(8px) scale(.985); } }

.welcome-dialog { width: min(810px, calc(100vw - 32px)); }
.welcome-layout { display: grid; grid-template-columns: 150px 1fr; gap: 40px; align-items: center; }
.welcome-mark { display: grid; min-height: 320px; place-items: center; border-radius: 999px 999px 30px 30px; background: var(--surface-2); }
.welcome-mark img { width: 92px; height: 180px; }
dialog h2 { margin: 0 0 14px; font-family: Georgia, "Times New Roman", serif; font-size: clamp(1.8rem, 5vw, 2.8rem); font-weight: 400; letter-spacing: -.025em; }
dialog h3 { margin: 0 0 8px; font-family: Georgia, "Times New Roman", serif; font-size: 1.3rem; font-weight: 400; }
dialog p { color: var(--muted); line-height: 1.62; }
.welcome-actions, .dialog-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 24px; }
.small-copy { margin: 18px 0 0; font-size: .74rem; }
.primary-button, .secondary-button, .book-button {
  min-height: 43px;
  padding: 0 18px;
  border: 1px solid transparent;
  border-radius: 999px;
  cursor: pointer;
  font-size: .78rem;
  font-weight: 700;
  letter-spacing: .035em;
}
.primary-button { background: var(--accent); color: var(--accent-contrast); }
.secondary-button { border-color: var(--faint); background: transparent; color: var(--text); }
.primary-button:hover, .secondary-button:hover { filter: brightness(1.08); }
.dialog-close { position: absolute; top: 20px; right: 22px; padding: 6px; color: var(--muted); font-family: Arial, sans-serif; font-size: 1.7rem; line-height: 1; }
.dialog-header { display: flex; justify-content: space-between; padding-right: 36px; }

.source-tabs { display: flex; gap: 6px; margin: 10px 0 28px; padding-bottom: 12px; border-bottom: 1px solid var(--faint); }
.source-tab { padding: 8px 13px; border: 0; border-radius: 999px; background: transparent; color: var(--muted); cursor: pointer; font-size: .76rem; }
.source-tab.active { background: var(--surface-2); color: var(--text); }
.source-panel > p { max-width: 540px; margin-top: 0; }
.hidden { display: none !important; }

.source-panel label { display: block; margin: 15px 0 7px; font-size: .75rem; font-weight: 700; letter-spacing: .04em; }
.source-panel label span { color: var(--muted); font-weight: 400; }
.source-panel input, .source-panel textarea, .setting-row select {
  width: 100%;
  border: 1px solid var(--faint);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
}
.source-panel input { height: 43px; padding: 0 12px; }
.source-panel textarea { padding: 12px; resize: vertical; line-height: 1.55; }
.field-footer { display: flex; align-items: center; justify-content: space-between; margin-top: 12px; color: var(--muted); font-size: .72rem; }
.book-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.book-button { min-height: 62px; border-color: var(--faint); border-radius: 10px; background: var(--bg); color: var(--text); text-align: left; }
.book-button:hover { border-color: var(--accent); }
.loading-copy { color: var(--accent); font-size: .75rem; }

fieldset { margin: 0; padding: 0 0 27px; border: 0; border-bottom: 1px solid var(--faint); }
legend { margin-bottom: 13px; font-size: .75rem; font-weight: 700; letter-spacing: .04em; }
.palette-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 9px; }
.palette-option { cursor: pointer; color: var(--muted); font-size: .68rem; text-align: center; }
.palette-option input { position: absolute; opacity: 0; pointer-events: none; }
.palette-option span { display: flex; height: 48px; margin-bottom: 7px; align-items: center; justify-content: center; gap: 3px; border: 2px solid transparent; border-radius: 9px; background: var(--sample-bg); }
.palette-option span i, .palette-option span b, .palette-option span em { width: 8px; height: 15px; background: var(--sample-text); content: ""; }
.palette-option span b { background: var(--sample-accent); }
.palette-option input:checked + span { border-color: var(--sample-accent); }
.palette-ink { --sample-bg: #101210; --sample-text: #f3f0e8; --sample-accent: #e85d4a; }
.palette-paper { --sample-bg: #f3eee4; --sample-text: #26231f; --sample-accent: #b84635; }
.palette-moss { --sample-bg: #11211b; --sample-text: #eef2de; --sample-accent: #e3b95c; }
.palette-dusk { --sample-bg: #171625; --sample-text: #f1edf5; --sample-accent: #db8eac; }
.setting-row { display: flex; min-height: 75px; align-items: center; justify-content: space-between; gap: 20px; border-bottom: 1px solid var(--faint); }
.setting-row label span { display: block; font-size: .8rem; font-weight: 700; }
.setting-row label small { display: block; margin-top: 4px; color: var(--muted); font-size: .68rem; }
.setting-row select { width: auto; min-width: 112px; height: 38px; padding: 0 30px 0 10px; }
.setting-row input[role="switch"] { width: 37px; height: 21px; accent-color: var(--accent); }
#settings-form .dialog-actions { justify-content: flex-end; }

.quiz-heading { align-items: start; }
#quiz-score { color: var(--accent); font-family: Georgia, "Times New Roman", serif; font-size: 1.35rem; }
.question { margin: 22px 0; padding: 0; border: 0; }
.question legend { margin-bottom: 11px; font-family: Georgia, "Times New Roman", serif; font-size: 1rem; font-weight: 400; }
.answer-option { display: flex; margin-top: 7px; padding: 11px 13px; gap: 10px; align-items: center; border: 1px solid var(--faint); border-radius: 9px; cursor: pointer; font-size: .78rem; }
.answer-option:hover { border-color: var(--muted); }
.answer-option.correct { border-color: #6eaa72; background: color-mix(in srgb, #6eaa72 12%, transparent); }
.answer-option.incorrect { border-color: var(--accent); opacity: .72; }
.answer-option input { accent-color: var(--accent); }
.quiz-actions { justify-content: flex-end; }
.finish-dialog { max-width: 470px; text-align: center; }
.finish-dialog .dialog-actions { justify-content: center; }
.finish-symbol { display: grid; width: 55px; height: 55px; margin: 0 auto 20px; place-items: center; border: 1px solid var(--accent); border-radius: 50%; color: var(--accent); font-size: 1.4rem; }

.toast { position: fixed; z-index: 20; bottom: 25px; left: 50%; padding: 10px 16px; border-radius: 999px; background: var(--text); color: var(--bg); font-size: .72rem; opacity: 0; pointer-events: none; transform: translate(-50%, 12px); transition: 180ms ease; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

body.size-small { --word-size: clamp(2.5rem, 7vw, 5.8rem); }
body.size-large { --word-size: clamp(3.6rem, 10vw, 9rem); }

@media (max-width: 640px) {
  .topbar { min-height: 62px; padding: 0 13px; }
  .home-link { font-size: 0; }
  .home-link::first-letter { font-size: 1rem; }
  .brand { font-size: .92rem; }
  .quiet-button { font-size: .68rem; }
  .reader-shell { padding: 13vh 14px 26px; }
  .word-stage { min-height: 34vh; }
  .focus-guide-top { top: 15%; }
  .focus-guide-bottom { bottom: 15%; }
  dialog { padding: 28px 20px 22px; border-radius: 15px; }
  .welcome-layout { display: block; }
  .welcome-mark { display: none; }
  .welcome-actions .primary-button, .welcome-actions .secondary-button { width: 100%; }
  .palette-grid { grid-template-columns: repeat(2, 1fr); }
  .book-grid { grid-template-columns: 1fr; }
}

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