/* ---------- design tokens ---------- */
:root {
  --fs: 16px;
  --lh: 1.65;
  --content-width: 860px;
  --topbar-h: 52px;

  /* selectable text families */
  --ff-sans: -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial,
    "PT Sans", Georgia, sans-serif;
  --ff-serif: "PT Serif", Georgia, "Times New Roman", "Iowan Old Style",
    serif;
  --ff-mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono",
    monospace;
  --ff: var(--ff-sans);

  --bg: #fbfaf7;
  --bg-alt: #f1efe9;
  --surface: #ffffff;
  --text: #26251f;
  --text-soft: #6b675c;
  --border: #e3e0d7;
  --accent: #8a1f1f;          /* railway dark red */
  --accent-soft: #f3e3e3;
  --link: #26508f;
  --ok: #2e7d43;              /* correct answer green */
  --bad: #b3392e;             /* wrong answer red */
  --shadow: 0 1px 3px rgba(0,0,0,.07), 0 8px 24px rgba(0,0,0,.05);
  color-scheme: light;
}
html[data-theme="dark"] {
  --bg: #191b1e;
  --bg-alt: #202326;
  --surface: #24272b;
  --text: #e4e2da;
  --text-soft: #9b988e;
  --border: #33373c;
  --accent: #d4685f;
  --accent-soft: #3a2725;
  --link: #7fa8e0;
  --ok: #66b57f;
  --bad: #e07a6e;
  --shadow: 0 1px 3px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--ff);
  font-size: var(--fs);
  transition: background-color .25s ease, color .25s ease;
}

/* ---------- topbar ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--topbar-h);
  display: flex; align-items: center; gap: 6px;
  padding: 0 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 40;
}
.spacer { flex: 1; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 38px; height: 38px;
  border: 1px solid transparent; border-radius: 9px;
  background: none; color: var(--text);
  cursor: pointer;
}
.icon-btn:hover { background: var(--bg-alt); border-color: var(--border); }
.fontsize { display: inline-flex; gap: 1px; flex: 0 0 auto; }
.courseselect-wrap { flex: 0 0 auto; min-width: 0; }
.fs-btn { gap: 1px; padding: 0; }
.fs-a { font-size: 15px; }
.fs-mark { font-size: 11px; transform: translateY(-4px); color: var(--text-soft); }

/* font family cycler — glyph rendered in the current family */
.fff-glyph { font-size: 15px; font-weight: 700; line-height: 1; }
.fff-btn.on-sans  #fontGlyph { font-family: var(--ff-sans); }
.fff-btn.on-serif #fontGlyph { font-family: var(--ff-serif); }
.fff-btn.on-mono  #fontGlyph { font-family: var(--ff-mono); }

.courseselect {
  max-width: min(56vw, 480px);
  height: 38px;
  padding: 0 10px;
  font-size: 15px;
  color: var(--text);
  background: var(--bg-alt);
  border: 1px solid var(--border);
  border-radius: 9px;
}

/* ---------- layout ---------- */
.layout { display: flex; padding-top: var(--topbar-h); min-height: 100vh; }

.sidebar {
  position: fixed; top: var(--topbar-h); bottom: 0; left: 0;
  width: 318px;
  overflow-y: auto;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 14px 8px 40px;
  z-index: 30;
}
.sidebar-course {
  padding: 4px 12px 10px;
  font-weight: 700;
  font-size: 15px;
  line-height: 1.35;
  color: var(--accent);
}
#lessonList { list-style: none; margin: 0; padding: 0; }
#lessonList a {
  display: block;
  padding: 8px 12px;
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  font-size: 14.5px;
  line-height: 1.35;
}
#lessonList a:hover { background: var(--bg-alt); }
#lessonList a.active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}
#lessonList li.kind-test > a, #lessonList li.kind-exam > a,
#lessonList li.kind-appendix > a {
  color: var(--text-soft);
  padding-top: 6px; padding-bottom: 6px;
}
#lessonList li { border-top: 1px dashed var(--border); }
#lessonList li:first-child { border-top: 0; }

/* project pages at the bottom of the table of contents */
.sidebar-extra {
  display: flex; flex-direction: column; gap: 2px;
  margin-top: 18px; padding: 10px 12px 0;
  border-top: 1px solid var(--border);
}
.sidebar-extra a {
  color: var(--text-soft);
  font-size: 13.5px;
  text-decoration: none;
  padding: 5px 0;
}
.sidebar-extra a:hover { color: var(--accent); text-decoration: underline; }

/* desktop collapse of the whole sidebar */
.sidebar { transition: transform .25s ease; }
body.sidebar-hidden .sidebar {
  transform: translateX(-105%);
  visibility: hidden;
}
body.sidebar-hidden .contentwrap { margin-left: 0; }
.contentwrap { transition: margin-left .25s ease; }

.scrim {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.45); z-index: 25;
  opacity: 0; pointer-events: none; transition: opacity .2s ease;
}

.contentwrap {
  flex: 1;
  min-width: 0;
  margin-left: 318px;
  padding: 26px 20px 90px;
}

/* ---------- typography ---------- */
.typo {
  max-width: var(--content-width);
  margin: 0 auto;
  line-height: var(--lh);
  overflow-wrap: break-word;
}
/* inline figures: centered image with its caption underneath */
.typo figure {
  margin: 1.15em auto;
  max-width: 96%;
  text-align: center;
}
.typo figure img { margin-bottom: .45em; }
.typo figcaption {
  font-size: .86em;
  color: var(--text-soft);
  line-height: 1.45;
  max-width: 56ch;
  margin: 0 auto;
}
.typo h1, .typo h2, .typo h3, .typo h4, .typo h5 {
  line-height: 1.25;
  margin: 1.4em 0 .55em;
  scroll-margin-top: calc(var(--topbar-h) + 12px);
}
.typo h1 { font-size: 1.9em; margin-top: .2em; }
.typo h2 {
  font-size: 1.42em;
  padding-bottom: .25em;
  border-bottom: 2px solid var(--accent-soft);
}
.typo h3 { font-size: 1.18em; color: var(--accent); }
.typo h4 { font-size: 1.04em; }
.typo p { margin: .7em 0; }
.typo ul, .typo ol { margin: .7em 0; padding-left: 1.6em; }
.typo li { margin: .25em 0; }
.typo img {
  max-width: 100%;
  height: auto;
  border-radius: 10px;
  cursor: zoom-in;
  box-shadow: var(--shadow);
  background: var(--surface);
  transition: filter .25s ease;
}
.typo hr {
  border: 0; border-top: 1px solid var(--border);
  margin: 1.6em auto; width: 60%;
}
.typo a { color: var(--link); text-decoration-thickness: 1px; text-underline-offset: 2px; }
.typo table {
  border-collapse: collapse;
  width: 100%;
  margin: 1em 0;
  font-size: .95em;
}
.typo th, .typo td {
  border: 1px solid var(--border);
  padding: 7px 10px;
  vertical-align: top;
  text-align: left;
}
.typo th { background: var(--bg-alt); }
.tablewrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: 8px; }
.pager {
  max-width: var(--content-width); margin: 26px auto 0;
  display: flex; gap: 10px; justify-content: space-between;
}
.pager a {
  flex: 0 1 auto;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  font-weight: 600;
  box-shadow: var(--shadow);
}
.pager a:hover { border-color: var(--accent); color: var(--accent); }
.pager .spacer { flex: 1; }

.reader-error {
  padding: 20px; text-align: center; color: var(--text-soft);
}

/* ---------- per-page footer: source, timestamps, disclaimer ---------- */
.pgfoot {
  max-width: var(--content-width);
  margin: 34px auto 0;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--text-soft);
}
.pgfoot a { color: var(--text-soft); text-decoration: underline; text-underline-offset: 2px; }
.pgfoot a:hover { color: var(--accent); }
.pgfoot .pf-src { margin-bottom: 2px; }
.pgfoot .pf-upd { opacity: .9; }
.pgfoot .pf-legal { margin-top: 2px; opacity: .9; }
/* ---------- interactive test ---------- */
.quiz { max-width: var(--content-width); margin: 0 auto; }
.quiz-intro {
  color: var(--text-soft);
  font-size: .92em;
  line-height: 1.5;
  margin: .4em 0 1.2em;
}
.quiz-toolbar {
  display: flex; flex-wrap: wrap; gap: 8px;
  position: sticky; top: calc(var(--topbar-h) + 8px);
  z-index: 5;
  padding: 8px; margin: 0 0 14px -8px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(6px);
  border: 1px solid var(--border);
  justify-content: center;
}
.quiz-toolbar button {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-family: var(--ff);
  cursor: pointer;
  box-shadow: var(--shadow);
}
.quiz-toolbar button:hover { border-color: var(--accent); color: var(--accent); }
.quiz-toolbar button.quiz-primary {
  background: var(--accent); border-color: var(--accent); color: #fff;
  font-weight: 600;
}
.quiz-toolbar button.quiz-primary:hover { filter: brightness(1.1); color: #fff; }
.quiz-toolbar button.quiz-primary[disabled] { opacity: .45; cursor: not-allowed; }
.quiz-toolbar button.quiz-active { border-color: var(--accent); color: var(--accent); font-weight: 600; }

.quiz-list { list-style: none; margin: 0; padding: 0; }
.quiz-q {
  padding: 14px 0 4px;
  border-top: 1px solid var(--border);
  margin: 0;
}
.quiz-q:first-child { border-top: 0; }
.quiz-q.missing { outline: 2px dashed var(--bad); outline-offset: 6px; border-radius: 8px; }
.quiz-question { font-weight: 700; line-height: 1.45; margin: .2em 0 .7em; }
.quiz-question .quiz-num {
  color: var(--accent); font-variant-numeric: tabular-nums;
  margin-right: .5em;
}
.quiz-opt {
  display: flex; gap: 10px; align-items: baseline;
  padding: 7px 12px;
  margin: 4px 0;
  border: 1px solid transparent;
  border-radius: 9px;
  cursor: pointer;
  line-height: 1.45;
}
.quiz-opt:hover { background: var(--bg-alt); border-color: var(--border); }
.quiz-opt .quiz-letter {
  flex: 0 0 auto;
  width: 1.7em; height: 1.7em;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  color: var(--text-soft);
  font-size: .82em; font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center;
  align-self: flex-start;
  transform: translateY(2px);
}
.quiz-opt.quiz-picked { border-color: var(--accent); background: var(--accent-soft); }
.quiz-opt.quiz-picked .quiz-letter {
  border-color: var(--accent); background: var(--accent); color: #fff;
}
/* right/wrong marks after grading or in show-answers mode */
.quiz:not(.quiz-checked):not(.quiz-showing) .quiz-opt { cursor: pointer; }
.quiz.quiz-showing .quiz-opt.quiz-correct,
.quiz.quiz-checked .quiz-opt.quiz-correct {
  border-color: var(--ok);
  background: color-mix(in srgb, var(--ok) 12%, transparent);
}
.quiz.quiz-showing .quiz-opt.quiz-correct .quiz-letter,
.quiz.quiz-checked .quiz-opt.quiz-correct .quiz-letter {
  border-color: var(--ok); background: var(--ok); color: #fff;
}
.quiz.quiz-checked .quiz-opt.quiz-wrong {
  border-color: var(--bad);
  background: color-mix(in srgb, var(--bad) 12%, transparent);
}
.quiz.quiz-checked .quiz-opt.quiz-wrong .quiz-letter {
  border-color: var(--bad); background: var(--bad); color: #fff;
}
.quiz-opt .quiz-mark {
  margin-left: auto; align-self: flex-start;
  font-size: .78em; font-weight: 700; white-space: nowrap;
  transform: translateY(3px);
}
.quiz-showing .quiz-opt.quiz-correct .quiz-mark,
.quiz-checked .quiz-opt.quiz-correct .quiz-mark { color: var(--ok); }
.quiz-checked .quiz-opt.quiz-wrong .quiz-mark { color: var(--bad); }

.quiz-score {
  margin: 22px 0 10px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: center;
}
.quiz-score .quiz-grade {
  font-size: 1.25em; font-weight: 800; margin-top: .2em;
}
.quiz-score .quiz-grade.g-exc { color: var(--ok); }
.quiz-score .quiz-grade.g-good { color: var(--ok); }
.quiz-score .quiz-grade.g-sat { color: #c07f00; }
.quiz-score .quiz-grade.g-uns { color: var(--bad); }
.quiz-score[hidden] { display: none; }

/* ---------- lightbox ---------- */
.lightbox {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(12,12,14,0);
  display: flex; flex-direction: column;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;          /* invisibly transparent to clicks */
  transition: opacity .22s ease, visibility .22s ease, background .22s ease;
}
.lightbox.open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  background: rgba(12,12,14,.93);
}
.lightbox[hidden] { display: none; }
.lb-bar {
  display: flex; gap: 8px; padding: 10px 12px;
  justify-content: center;
}
.lb-btn {
  min-width: 42px; height: 42px;
  border: 1px solid rgba(255,255,255,.25); border-radius: 10px;
  background: rgba(255,255,255,.08);
  color: #fff; font-size: 17px; cursor: pointer;
}
.lb-btn:hover { background: rgba(255,255,255,.18); }
.lb-sep {
  width: 1px; height: 26px;
  background: rgba(255,255,255,.25);
  margin: 0 4px;
}
.lb-pos {
  align-self: center;
  min-width: 56px; text-align: center;
  color: #e8e8e4; font-size: 14px;
  font-variant-numeric: tabular-nums;
  user-select: none;
}
.lb-zoompct {
  min-width: 66px;
  font-variant-numeric: tabular-nums;
  font-size: 14px;
  color: #ffd9a8;
}
.lb-btn[disabled] { opacity: .3; cursor: default; }
.lb-stage {
  flex: 1; overflow: hidden; position: relative;
  display: flex; align-items: center; justify-content: center;
}
#lbImg {
  max-width: calc(100vw - 30px);
  max-height: calc(100vh - 130px);
  user-select: none; -webkit-user-drag: none;
  transform-origin: center center;
  will-change: transform;
  border-radius: 4px;
  box-shadow: 0 10px 60px rgba(0,0,0,.5);
  background: var(--surface);
  cursor: zoom-out;               /* click at zoom=1 closes */
  transition: filter .25s ease;
}
#lbImg.morphing { transition: transform .36s cubic-bezier(.25,.85,.3,1); }
#lbImg.dragging { cursor: grabbing; }
#lbImg.zoomed { cursor: grab; }
.lb-caption {
  padding: 8px 16px 14px;
  text-align: center; color: #d8d8d4; font-size: 14px;
  min-height: 34px;
}

/* ---------- responsive ---------- */
/* dark-theme image comfort:
 * ink-on-paper drawings become white-on-black; photos are dimmed */
html[data-theme="dark"] img.img-tone-invert {
  filter: invert(1) hue-rotate(180deg) brightness(.96);
}
html[data-theme="dark"] img.img-tone-dim {
  filter: brightness(.82) contrast(1.04) saturate(.85);
}
@media (max-width: 960px) {
  .sidebar {
    transform: translateX(-105%);
    transition: transform .25s ease;
    box-shadow: 12px 0 40px rgba(0,0,0,.25);
    width: min(86vw, 340px);
    border-right: 0;
  }
  .sidebar.open { transform: translateX(0); }
  body.sidebar-hidden .sidebar { transform: translateX(-105%); }
  .scrim.show { opacity: 1; pointer-events: auto; display: block; }
  .contentwrap { margin-left: 0; padding: 18px 14px 70px; }
  .courseselect { max-width: 44vw; font-size: 14px; }
  .typo h1 { font-size: 1.55em; }
  .typo h2 { font-size: 1.28em; }
  .pager a { padding: 8px 10px; font-size: .92em; }
  .pgfoot { font-size: 11.5px; margin-top: 22px; }
}
@media (max-width: 560px) {
  /* compact topbar: everything on one row, select takes the leftover width */
  .topbar { gap: 3px; padding: 0 5px; flex-wrap: nowrap; }
  .icon-btn { min-width: 33px; height: 36px; }
  .fs-btn { min-width: 31px; }
  .courseselect-wrap { flex: 1 1 0; }
  .courseselect {
    max-width: none; width: 100%; height: 36px;
    padding: 0 5px; font-size: 13.5px;
  }
  .fs-a { font-size: 14px; }
  .fs-mark { font-size: 10px; transform: translateY(-3px); }
  .fff-glyph { font-size: 13.5px; }
  .lb-bar { gap: 4px; padding: 8px 6px; }
  .lb-btn { min-width: 36px; height: 36px; font-size: 15px; }
  .lb-pos { font-size: 12px; min-width: 44px; }
  .lb-zoompct { min-width: 52px; font-size: 12.5px; }
  .lb-sep { display: none; }
  #lbImg { max-width: calc(100vw - 12px); max-height: calc(100vh - 110px); }
  .pgfoot { font-size: 11px; margin-top: 18px; }
  .quiz-toolbar { gap: 6px; padding: 6px; }
  .quiz-toolbar button { padding: 7px 10px; font-size: 13px; }
  .quiz-opt { padding: 8px 8px; }
}
@media print {
  .topbar, .sidebar, .scrim, .pager, #lightbox { display: none !important; }
  .contentwrap { margin: 0; padding: 0; }
}
