:root {
  --bg: #14161a;
  --panel: #1b1e24;
  --border: #2a2e36;
  --text: #e6e3dc;
  --muted: #8b909a;
  --accent: #e5533d;
  --track: #2a2e36;
  --hover: rgba(255, 255, 255, .04);
  --good: #5fbf8a;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #f4f1ea;
  --panel: #ffffff;
  --border: #ddd8cc;
  --text: #22242a;
  --muted: #6b707a;
  --accent: #c9402b;
  --track: #e3dfd4;
  --hover: rgba(0, 0, 0, .04);
  --good: #2e8b57;
  color-scheme: light;
}

@media (prefers-color-scheme: light) {
  :root[data-theme="system"] {
    --bg: #f4f1ea;
    --panel: #ffffff;
    --border: #ddd8cc;
    --text: #22242a;
    --muted: #6b707a;
    --accent: #c9402b;
    --track: #e3dfd4;
    --hover: rgba(0, 0, 0, .04);
    --good: #2e8b57;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  display: flex;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* Left area */
#sidebar {
  width: 300px;
  flex: none;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
}

#sidebar header { padding: 20px 20px 12px; border-bottom: 1px solid var(--border); }
#sidebar h1 { margin: 0; font-family: Georgia, serif; font-size: 1.6rem; }
#sidebar header p { margin: 2px 0 12px; color: var(--muted); font-size: .9rem; }

.overall { display: flex; align-items: center; gap: 10px; font-size: .8rem; color: var(--muted); }
.overall .bar { flex: 1; }
.overall + .overall { margin-top: 6px; }
.overall .lbl { width: 4.2em; }
.overall span:last-child { white-space: nowrap; }
.overall.quiz span:last-child { text-align: right; }

.bar { height: 4px; background: var(--track); border-radius: 2px; overflow: hidden; }
.bar > div { height: 100%; width: 0; background: var(--accent); }
.bar.quiz > div, .overall.quiz .bar > div { background: var(--good); }

#sidebar .chapter-nav { flex: 1; min-height: 0; overflow-y: auto; }
#chapter-list { list-style: none; margin: 0; padding: 8px 0; }

#chapter-list li button {
  display: block;
  width: 100%;
  padding: 10px 20px;
  background: none;
  border: 0;
  border-left: 3px solid transparent;
  text-align: left;
}
#chapter-list li button:hover { background: var(--hover); }
#chapter-list li.active button { background: rgba(229, 83, 61, .1); border-left-color: var(--accent); }

.ch-head { display: flex; justify-content: space-between; margin-bottom: 6px; font-size: .95rem; }
.ch-pct { color: var(--muted); font-size: .8rem; white-space: nowrap; }
li.done .ch-pct { color: var(--accent); }
.ch-quiz { margin: 6px 0; color: var(--muted); font-size: .75rem; }

.theme {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: .85rem;
}
.theme select {
  padding: 4px 8px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
}

.reset {
  display: flex;
  gap: 8px;
  padding: 0 20px 16px;
}
.reset button {
  flex: 1;
  padding: 6px 8px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--muted);
  font-size: .8rem;
}
.reset button:hover { color: var(--accent); border-color: var(--accent); }

/* Legal navigation and pages */
.legal-links { display: flex; flex-wrap: wrap; gap: 0 16px; }
.legal-links a, .sidebar-links a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  color: var(--muted);
  text-decoration: underline;
  text-underline-offset: .15em;
}
.legal-links a:hover, .legal-links a:focus-visible, .sidebar-links a:hover, .sidebar-links a:focus-visible { color: var(--text); }
.legal-links a:focus-visible, .sidebar-links a:focus-visible, .legal-content a:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
#sidebar > .sidebar-links { flex: none; display: flex; flex-direction: column; align-items: flex-start; padding: 4px 20px; border-top: 1px solid var(--border); font-size: .8rem; }
body.legal-page { display: block; height: auto; min-height: 100%; line-height: 1.6; }
.legal-content { max-width: 70ch; margin: 0 auto; padding: 2rem 1rem; overflow-wrap: anywhere; }
.legal-content h1 { font-family: Georgia, serif; font-size: 2rem; margin: 1rem 0; }
.legal-content h2 { font-size: 1.25rem; margin: 2rem 0 .5rem; }
.legal-content p { margin: .5rem 0 1rem; }
.legal-content address { font-style: normal; }
.legal-content a { color: var(--text); text-decoration: underline; }
.legal-content .legal-links { margin-top: 2rem; padding-top: 1rem; border-top: 1px solid var(--border); }

/* Right area */
#reader {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  gap: 28px;
}

#chapter-title { margin: 0; font-family: Georgia, serif; font-weight: normal; color: var(--muted); letter-spacing: .08em; text-transform: uppercase; font-size: 1rem; }

#display {
  position: relative;
  width: min(100%, 760px);
  padding: 34px 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.guide {
  position: absolute;
  left: 50%;
  width: 2px;
  height: 14px;
  background: var(--accent);
  transform: translateX(-50%);
}
.guide.top { top: 0; }
.guide.bottom { bottom: 0; }

#word {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  font-family: "Courier New", ui-monospace, monospace;
  font-size: clamp(2rem, 6vw, 3.4rem);
  line-height: 1.3;
  white-space: pre;
}
#w-before { text-align: right; }
#w-pivot { color: var(--accent); }
#w-after { text-align: left; }

#context {
  width: min(100%, 760px);
  min-height: 4.5em;
  margin: 0;
  color: var(--muted);
  font-family: Georgia, serif;
  line-height: 1.5;
  text-align: center;
}
#context b { color: var(--text); font-weight: normal; background: rgba(229, 83, 61, .25); border-radius: 3px; }

#controls { width: min(100%, 760px); display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; align-items: center; justify-content: center; gap: 12px; }

#controls button {
  min-width: 44px;
  height: 44px;
  padding: 0 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
}
#controls button:hover { border-color: var(--muted); }
#btn-play { min-width: 110px; background: var(--accent); border-color: var(--accent); font-weight: 600; }

input[type=range] { flex: 1; accent-color: var(--accent); }
#seek-text, #speed-text, #chapter-eta { min-width: 5.5em; color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; }

.hint { margin: 0; text-align: center; color: var(--muted); font-size: .8rem; }

/* Comprehension quiz */
#quiz {
  width: min(92vw, 560px);
  padding: 24px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
}
#quiz::backdrop { background: rgba(0, 0, 0, .6); }
#quiz h3 { margin: 0 0 4px; padding-right: 32px; font-family: Georgia, serif; font-weight: normal; }
#quiz #quiz-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px;
  height: 32px;
  padding: 0;
  background: none;
  border: 0;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  text-align: center;
}
#quiz #quiz-close:hover { color: var(--text); }
#quiz-step { margin: 0 0 16px; color: var(--muted); font-size: .85rem; }
.q-text { margin: 0 0 12px; font-family: Georgia, serif; font-size: 1.05rem; line-height: 1.4; }
.q-options { display: flex; flex-direction: column; gap: 8px; }
#quiz button {
  padding: 10px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  text-align: left;
}
#quiz button:hover:not(:disabled) { border-color: var(--muted); }
#quiz button:disabled { cursor: default; color: var(--text); opacity: .6; }
#quiz button.correct { opacity: 1; border-color: var(--good); background: color-mix(in srgb, var(--good) 18%, transparent); }
#quiz button.wrong { opacity: 1; border-color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, transparent); }
#quiz button.unsure { opacity: 1; border-color: var(--muted); background: color-mix(in srgb, var(--muted) 15%, transparent); }
#quiz button.primary { background: var(--accent); border-color: var(--accent); font-weight: 600; text-align: center; }
#quiz-feedback { min-height: 1.5em; margin: 12px 0 0; font-size: .9rem; }
.good { color: var(--good); }
.bad { color: var(--accent); }
.neutral { color: var(--muted); }
.review { margin-bottom: 14px; }
.review p { margin: 0 0 4px; font-size: .9rem; }
.review .q-text { color: var(--text); font-size: 1rem; }
#quiz-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
#quiz-actions button { text-align: center; }

@media (max-width: 760px) {
  body { flex-direction: column; }
  #sidebar { width: 100%; height: 35%; min-height: 360px; border-right: 0; border-bottom: 1px solid var(--border); }
  #reader { padding: 16px; gap: 18px; }
}
