:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --fg: #1a1a1a;
  --muted: #666;
  --border: #ddd;
  --accent: #2563eb;
  --accent-fg: #ffffff;
  --card-bg: #f7f7f8;
  --danger: #b91c1c;
  --success: #15803d;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --fg: #e7e7e7;
    --muted: #9a9a9a;
    --border: #333;
    --card-bg: #1e2126;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.5;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1.5rem;
  border-bottom: 1px solid var(--border);
}

.topbar .brand { font-weight: 700; text-decoration: none; color: var(--fg); }
.topbar nav { display: flex; align-items: center; gap: 1rem; }
.topbar .who { color: var(--muted); }

.link-button {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  padding: 0;
  font: inherit;
  text-decoration: underline;
}

.inline-form { display: inline; }

.content { max-width: 900px; margin: 0 auto; padding: 1.5rem; }

.flashes { list-style: none; padding: 0; margin: 0 0 1rem; }
.flash { padding: 0.6rem 0.9rem; border-radius: 6px; margin-bottom: 0.5rem; }
.flash-error { background: #fee2e2; color: var(--danger); }
.flash-info, .flash-message { background: var(--card-bg); }
.flash-success { background: #dcfce7; color: var(--success); }

.auth-form { display: flex; flex-direction: column; gap: 0.75rem; max-width: 320px; }
.auth-form label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.9rem; }
.auth-form input { padding: 0.5rem; border: 1px solid var(--border); border-radius: 6px; background: var(--bg); color: var(--fg); }

button, .btn {
  padding: 0.5rem 1rem;
  border-radius: 6px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-fg);
  cursor: pointer;
  font: inherit;
  text-decoration: none;
  display: inline-block;
}
button.secondary, .btn.secondary { background: transparent; color: var(--accent); }

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.25rem;
  margin-bottom: 1rem;
}

.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.5rem; border-bottom: 1px solid var(--border); }

.muted { color: var(--muted); }
.badge { display: inline-block; padding: 0.15rem 0.5rem; border-radius: 999px; background: var(--card-bg); border: 1px solid var(--border); font-size: 0.8rem; }
.tabs { display: flex; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.tabs a { padding: 0.4rem 0.8rem; border-radius: 6px; text-decoration: none; color: var(--fg); border: 1px solid var(--border); }
.tabs a.active { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }

.choice-list { list-style: none; padding: 0; }
.choice-list li { padding: 0.5rem; border: 1px solid var(--border); border-radius: 6px; margin-bottom: 0.4rem; }
.choice-list li.correct { border-color: var(--success); }
.choice-list li.incorrect { border-color: var(--danger); }

.grade-buttons { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 1rem; }

ul.toc, ul.toc ul { list-style: none; padding-left: 1rem; margin: 0; }
ul.toc { padding-left: 0; }
ul.toc a { text-decoration: none; color: var(--fg); display: block; padding: 0.15rem 0; }
ul.toc a.current { font-weight: 700; color: var(--accent); }

/* Book navigator: sidebar + reading pane both scroll internally within a height fixed to the
   viewport, so the TOC and the Mark Read / Prev/Next controls stay in place while flipping
   through chapters -- only the chapter text itself scrolls. */
.nav-layout {
  display: flex;
  gap: 1.5rem;
  margin-top: 1rem;
  height: calc(100vh - 15rem);
  min-height: 320px;
}
.nav-sidebar {
  flex: 0 0 260px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem;
}
.nav-article {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}
.nav-article-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.25rem;
}
.nav-article-actions {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  padding: 0.75rem 1.25rem;
  background: var(--card-bg);
}
.nav-article-actions .action-row { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.nav-article-actions .prev-next-row { display: flex; justify-content: space-between; margin-top: 0.6rem; }

.exam-grid { display: flex; flex-wrap: wrap; gap: 2px; margin: 0.5rem 0 1rem; }
.exam-grid button { width: 2.4rem; padding: 0.3rem 0; font-size: 0.85rem; }
.exam-grid button.answered { background: var(--success); border-color: var(--success); }
.exam-grid button.flagged { outline: 2px solid #b45309; }
.exam-grid button.current { outline: 3px solid var(--accent); }

.timer { font-weight: 700; font-size: 1.1rem; }
.timer.low { color: var(--danger); }

table.book-progress { width: 100%; }
table.book-progress td { border-bottom: 1px solid var(--border); padding: 0.6rem 0.5rem; vertical-align: middle; }
table.book-progress tr:last-child td { border-bottom: none; }
.book-title { width: 40%; }
.book-pct-cell { display: flex; flex-direction: column; gap: 0.25rem; min-width: 160px; }
.progress-bar { background: var(--border); border-radius: 999px; height: 8px; width: 100%; overflow: hidden; }
.progress-bar-fill { background: var(--accent); height: 100%; border-radius: 999px; }
