/* Understanding AI: shared styles. Mobile first, no build step. One web font, Inter,
   self-hosted in assets/fonts (its @font-face is in the page head, next to the preload).
   Colour tokens (diagrams may use these): --fg --bg --muted --accent --accent-2
   --surface --line --ok --bad. Light is the default; dark comes from the system
   setting unless <html data-theme="light|dark"> overrides it. */

:root {
  color-scheme: light;
  --bg: #ffffff;
  --fg: #1c212b;
  --muted: #515866;
  --surface: #f2f4f7;
  --line: #858c99;
  --accent: #1f5ea8;
  --accent-2: #8a3b8f;
  --on-accent: #ffffff;
  --ok: #17703a;
  --bad: #b02a22;
  --shadow: 0 1px 3px rgb(0 0 0 / 0.12);

  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  --measure: 42rem;

  /* Type scale: body 17px, each step x1.2 on phones rising to x1.25 (major third) by
     1024px wide. The lede sits between body and h3. */
  --step-0: 1.0625rem;                                          /* 17px body, h4 */
  --step-1: clamp(1.275rem, 1.254rem + 0.086vw, 1.3281rem);     /* 20.4 -> 21.25px, h3 */
  --step-2: clamp(1.53rem, 1.45rem + 0.3285vw, 1.6602rem);      /* 24.5 -> 26.6px, h2 */
  --step-3: clamp(1.836rem, 1.689rem + 0.6036vw, 2.0752rem);    /* 29.4 -> 33.2px, h1 */
  --step-lede: calc(var(--step-0) * 1.1);                       /* 18.7px */
  /* Vertical rhythm: block margins are whole multiples of --space. */
  --space: 0.5rem;
  --header-h: 3.5rem;
  --radius: 0.5rem;
  /* Page frame: header, layout and footer share one container, so their content starts at
     the same left edge. Below 960px the container is the reading column; from 960px it is
     the sidebar + gap + reading column, centred as one unit (no empty column on the right). */
  --gutter: 1rem;
  --sidebar: 17rem;
  --sidebar-gap: 3rem;
  --page-max: calc(var(--measure) + 2 * var(--gutter));
}
@media (min-width: 960px) {
  :root {
    --gutter: 1.5rem;
    --page-max: calc(var(--sidebar) + var(--sidebar-gap) + var(--measure) + 2 * var(--gutter));
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #13161c;
  --fg: #e7e9ee;
  --muted: #a8afbc;
  --surface: #1e232c;
  --line: #6e7786;
  --accent: #82b4ff;
  --accent-2: #e2a2e6;
  --on-accent: #0d1117;
  --ok: #6ccf8e;
  --bad: #ff8f86;
  --shadow: 0 1px 3px rgb(0 0 0 / 0.5);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #13161c;
    --fg: #e7e9ee;
    --muted: #a8afbc;
    --surface: #1e232c;
    --line: #6e7786;
    --accent: #82b4ff;
    --accent-2: #e2a2e6;
    --on-accent: #0d1117;
    --ok: #6ccf8e;
    --bad: #ff8f86;
    --shadow: 0 1px 3px rgb(0 0 0 / 0.5);
  }
}

/* ---------- Base ---------- */

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

@media (prefers-reduced-motion: no-preference) {
  html { scroll-behavior: smooth; }
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  font-size: var(--step-0);
  line-height: 1.6;
  overflow-wrap: break-word;
}

h1, h2, h3, h4 { line-height: 1.25; margin: calc(var(--space) * 4) 0 var(--space); text-wrap: balance; }
h1 { font-size: var(--step-3); line-height: 1.15; letter-spacing: -0.01em; margin: calc(var(--space) * 2) 0; }
h2 { font-size: var(--step-2); line-height: 1.2; letter-spacing: -0.005em; margin: calc(var(--space) * 6) 0 calc(var(--space) * 2); scroll-margin-top: calc(var(--header-h) + 1rem); }
h3 { font-size: var(--step-1); line-height: 1.3; }
h4 { font-size: var(--step-0); }
[id] { scroll-margin-top: calc(var(--header-h) + 1rem); }

p, ul, ol { margin: 0 0 calc(var(--space) * 2); }
p, li, figcaption, dd, blockquote { text-wrap: pretty; }
blockquote { margin-block: calc(var(--space) * 3); }
.callout-title { text-wrap: balance; }
li + li { margin-top: 0.3em; }

a { color: var(--accent); text-underline-offset: 0.15em; }
a:hover { text-decoration-thickness: 2px; }

img, video { max-width: 100%; height: auto; }

code, pre, kbd { font-family: var(--mono); font-size: 0.92em; }

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  border-radius: 2px;
}

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute;
  left: 0.5rem; top: -4rem;
  z-index: 100;
  background: var(--accent);
  color: var(--on-accent);
  padding: 0.5rem 1rem;
  border-radius: var(--radius);
  font-weight: 600;
}
.skip-link:focus { top: 0.5rem; }

/* ---------- Buttons ---------- */

.btn, .quiz button, .copyblock button {
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1.2;
  min-height: 2.75rem;
  padding: 0.55rem 1rem;
  border-radius: var(--radius);
  border: 2px solid var(--accent);
  background: var(--accent);
  color: var(--on-accent);
  cursor: pointer;
}
.btn-secondary, .quiz .quiz-again, .copyblock button {
  background: transparent;
  color: var(--accent);
}
.btn:hover, .quiz button:hover, .copyblock button:hover { filter: brightness(1.08); }
[aria-disabled="true"] { cursor: default; opacity: 0.75; }

/* ---------- Header ---------- */

.site-header {
  position: sticky;
  top: 0;
  z-index: 40;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.site-header-inner {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-height: var(--header-h);
  max-width: var(--page-max);
  margin: 0 auto;
  padding: 0.35rem var(--gutter);
}
.brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-right: auto;
  min-width: 0;
  color: var(--fg);
  text-decoration: none;
  font-weight: 700;
  font-size: 1.05rem;
}
.brand:hover .brand-title { text-decoration: underline; }
.gmark { width: 2rem; height: 2rem; flex: none; color: var(--fg); fill: currentColor; }
.brand-title { overflow-wrap: anywhere; }

.header-btn {
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 2.75rem;
  min-width: 2.75rem;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--fg);
  cursor: pointer;
  justify-content: center;
}
.header-btn svg { width: 1.25rem; height: 1.25rem; flex: none; }
.header-btn[hidden] { display: none; }

/* On narrow phones the contents button keeps its label for screen readers only. */
@media (max-width: 399.98px) {
  .contents-toggle span {
    position: absolute; width: 1px; height: 1px; margin: -1px;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
  }
}

/* Theme toggle: show the icon for the theme you will switch to. */
.theme-toggle .icon-sun { display: none; }
:root[data-theme="dark"] .theme-toggle .icon-sun { display: block; }
:root[data-theme="dark"] .theme-toggle .icon-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-toggle .icon-sun { display: block; }
  :root:not([data-theme="light"]) .theme-toggle .icon-moon { display: none; }
}

/* ---------- Layout and contents panel ---------- */

.layout {
  max-width: var(--page-max);
  margin: 0 auto;
  padding: 0 var(--gutter);
}
main { max-width: var(--measure); margin: 0 auto; padding: 1rem 0 3rem; }

.toc-panel { display: none; }
.toc-panel h2 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 0 0 0.75rem;
}
.toc-panel ol { list-style: none; margin: 0; padding: 0; }
.toc-panel li + li { margin-top: 0.15rem; }
.toc-panel a {
  display: block;
  padding: 0.5rem 0.6rem;
  border-radius: var(--radius);
  border-left: 3px solid transparent;
  color: var(--fg);
  text-decoration: none;
  line-height: 1.35;
}
.toc-panel a:hover { background: var(--surface); text-decoration: underline; }
.toc-panel a[aria-current="true"] { border-left-color: var(--accent); background: var(--surface); }
.toc-close { margin-bottom: 1rem; }
.toc-summary { margin: -0.4rem 0 0.75rem; font-size: 0.85rem; color: var(--muted); }
.toc-part {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  margin: 1.1rem 0 0.35rem;
}
.toc-panel .toc-course > li + li { margin-top: 0.4rem; }
.toc-panel .toc-chapter-link {
  display: flex;
  gap: 0.5rem;
  align-items: baseline;
  font-weight: 600;
}
.toc-num {
  flex: none;
  min-width: 1.4rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.toc-title { flex: 1; }
.toc-done { flex: none; color: var(--ok); font-weight: 700; }
.toc-chapter.is-done .toc-num { color: var(--ok); }
.toc-panel .toc-chapter-link[aria-current="page"] { border-left-color: var(--accent); background: var(--surface); }
.toc-panel .toc-sections { margin: 0.15rem 0 0.35rem 1.9rem; }
.toc-panel .toc-sections a {
  padding: 0.3rem 0.5rem;
  font-size: 0.9rem;
  color: var(--muted);
}
.toc-panel .toc-chapter.is-current .toc-sections a { color: var(--fg); }
.toc-panel .toc-sections a[aria-current="true"] { color: var(--fg); font-weight: 600; }

/* Reading progress ring, bottom right of chapter pages */
.read-progress {
  position: fixed;
  right: max(1rem, env(safe-area-inset-right));
  bottom: max(1rem, env(safe-area-inset-bottom));
  z-index: 50;
  width: 3.25rem;
  height: 3.25rem;
  border-radius: 50%;
  background: var(--bg);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  pointer-events: none;
}
.read-progress svg { position: absolute; inset: 0.2rem; width: calc(100% - 0.4rem); height: calc(100% - 0.4rem); rotate: -90deg; }
.read-progress circle { fill: none; stroke-width: 3; }
.read-progress .rp-track { stroke: var(--surface); }
.read-progress .rp-bar { stroke: var(--accent); stroke-linecap: round; stroke-dasharray: 0 100; }
.read-progress.is-complete .rp-bar { stroke: var(--ok); }
.read-progress .rp-text { position: relative; font-size: 0.72rem; font-weight: 700; color: var(--fg); font-variant-numeric: tabular-nums; }
body.drawer-open .read-progress { display: none; }
@media (prefers-reduced-motion: no-preference) {
  .read-progress .rp-bar { transition: stroke-dasharray 0.15s linear; }
}
/* On phones the ring would cover text, so it becomes a thin bar along the bottom edge of the
   sticky header (drawn over the header's own border, so it never covers page content).
   site.js sets --rp to the percentage; the role and aria values are the same as the ring's. */
@media (max-width: 700px) {
  .read-progress {
    top: calc(var(--header-h) - 3px);
    right: 0;
    bottom: auto;
    left: 0;
    width: auto;
    height: 4px;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    background: linear-gradient(to right, var(--accent) calc(var(--rp, 0) * 1%), transparent 0);
  }
  .read-progress.is-complete { background: linear-gradient(to right, var(--ok) 100%, transparent 0); }
  .read-progress svg, .read-progress .rp-text { display: none; }
}
.toc-backdrop { display: none; }

/* Mobile drawer */
@media (max-width: 959.98px) {
  .toc-panel.is-open {
    display: block;
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 60;
    width: min(20rem, 88vw);
    overflow-y: auto;
    padding: 1rem var(--gutter);
    background: var(--bg);
    border-right: 1px solid var(--line);
    box-shadow: var(--shadow);
  }
  .toc-backdrop.is-open {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 55;
    background: rgb(0 0 0 / 0.45);
  }
  body.drawer-open { overflow: hidden; }
}
@media (max-width: 959.98px) and (prefers-reduced-motion: no-preference) {
  .toc-panel.is-open { transition: translate 0.2s ease-out; }
  @starting-style { .toc-panel.is-open { translate: -100% 0; } }
}

/* Desktop sidebar */
@media (min-width: 960px) {
  .layout {
    display: grid;
    grid-template-columns: var(--sidebar) minmax(0, 1fr);
    gap: var(--sidebar-gap);
  }
  .layout.no-toc { display: block; }
  /* The landing page has no contents sidebar: the header, text and footer share one centred column. */
  body[data-page="index"] { --page-max: calc(var(--measure) + 2 * var(--gutter)); }
  main { margin: 0; padding-top: 1.5rem; }
  .toc-panel {
    display: block;
    position: sticky;
    top: calc(var(--header-h) + 1.5rem);
    align-self: start;
    max-height: calc(100vh - var(--header-h) - 3rem);
    overflow-y: auto;
    padding-top: 1.5rem;
  }
  .toc-panel:empty { display: none; }
  .toc-close, .contents-toggle { display: none !important; }
}

/* Chapter pages from 1280px: Contents | Text | Glossary margin. Both side columns 15rem and
   the text keeps --measure. 3rem gaps would need 81rem (1296px), wider than a 1280px window,
   so the gaps drop to 2rem: 15 + 2 + 42 + 2 + 15 + 2 x 1.5 = 79rem (1264px). The third track
   stays empty until glossary cards are added to it (grid-column 3). Header and footer follow
   --page-max, so they widen with it. */
@media (min-width: 1280px) {
  body[data-page="chapter"] {
    --sidebar: 15rem;
    --sidebar-gap: 2rem;
    --gloss-col: 15rem;
    --page-max: calc(var(--sidebar) + var(--measure) + var(--gloss-col) + 2 * var(--sidebar-gap) + 2 * var(--gutter));
  }
  body[data-page="chapter"] .layout:not(.no-toc) {
    grid-template-columns: var(--sidebar) minmax(0, 1fr) var(--gloss-col);
  }
  body[data-page="chapter"] .layout > .gloss-margin { grid-column: 3; grid-row: 1; }
}

/* ---------- Chapter furniture ---------- */

.eyebrow {
  margin: 0;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.95rem;
}
.lede { font-size: var(--step-lede); }

.stamp {
  display: inline-block;
  margin: 0 0 1rem;
  padding: 0.15rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.875rem;
  font-weight: 600;
}

.callout, .case, .copyblock, .video, .quiz, .chapter-end {
  margin: 1.5rem 0;
  border-radius: var(--radius);
}

.callout {
  padding: 1rem 1.1rem;
  background: var(--surface);
  border-left: 4px solid var(--accent);
}
.callout > :last-child, .case > :last-child { margin-bottom: 0; }
.callout-title { font-weight: 700; margin-bottom: var(--space); }
.callout.warn { border-left-color: var(--bad); }

/* Chapter opener: large number, meta row (built by partials.py), objectives card. */
.chapter-num {
  margin: 0 0 var(--space);
  color: var(--accent);
  font-size: clamp(3rem, 2.5rem + 2.5vw, 4.5rem);
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.03em;
  font-variant-numeric: lining-nums;
}
.chapter-meta {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space) 1.25rem;
  margin: 0 0 calc(var(--space) * 3);
  padding: 0;
  list-style: none;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
}
.chapter-meta li { display: inline-flex; align-items: center; gap: 0.4rem; margin: 0; }
.meta-icon, .callout-icon { flex: none; width: 1.15em; height: 1.15em; }
.callout.objectives {
  padding: 1.1rem 1.25rem 1.2rem;
  background: color-mix(in srgb, var(--accent) 7%, var(--bg));
  border: 1px solid color-mix(in srgb, var(--accent) 45%, transparent);
  border-top: 4px solid var(--accent);
}
.callout.objectives .callout-title {
  color: var(--accent);
  font-size: 0.85rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.callout.objectives ul { padding-left: 0; list-style: none; }
.callout.objectives li { position: relative; padding-left: 1.75rem; }
/* A tick for each objective (decorative; the list is still a list). */
.callout.objectives li::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0.2em;
  width: 1.15em;
  height: 1.15em;
  background: var(--accent);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='m5 12.5 4.5 4.5L19 7.5' fill='none' stroke='%23000' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='m5 12.5 4.5 4.5L19 7.5' fill='none' stroke='%23000' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / contain no-repeat;
}

/* Callout types. Each has an icon and a text label (.callout-label, built by partials.py),
   so they differ by more than colour. Plain .callout is a note. */
.callout-label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0 0 var(--space);
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.callout.tip { border-left-color: var(--ok); }
.callout.tip .callout-label { color: var(--ok); }
.callout.law { border-left: 6px double var(--accent-2); }
.callout.law .callout-label { color: var(--accent-2); }
.callout.watch { border-left-color: var(--bad); }
.callout.watch .callout-label { color: var(--bad); }

/* Key takeaways, at the end of each chapter. */
.takeaways {
  margin: calc(var(--space) * 6) 0 calc(var(--space) * 3);
  padding: 1.1rem 1.25rem 1.25rem;
  border: 2px solid var(--accent);
  border-radius: var(--radius);
}
.takeaways h2 { margin-top: 0; }
.takeaways-list { padding-left: 1.3rem; }
.takeaways-monday {
  margin-top: calc(var(--space) * 2);
  padding: 0.9rem 1.1rem;
  background: var(--surface);
  border-left: 4px solid var(--ok);
  border-radius: var(--radius);
}
.takeaways-monday-label { margin: 0 0 calc(var(--space) / 2); font-weight: 700; }
.takeaways-monday > :last-child { margin-bottom: 0; }

/* Worked example box (the fictional charity, Thornbeck Trust).
   The house-and-heart icon marks every example, here and in the contents sidebar. */
:root {
  --thornbeck-icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M3.5 10.5 12 3.5l8.5 7V20a1 1 0 0 1-1 1h-15a1 1 0 0 1-1-1z' fill='none' stroke='%23000' stroke-width='2' stroke-linejoin='round'/%3E%3Cpath d='M12 18.4s-4.2-2.5-4.2-5.2a2.1 2.1 0 0 1 4.2-.6 2.1 2.1 0 0 1 4.2.6c0 2.7-4.2 5.2-4.2 5.2z'/%3E%3C/svg%3E");
  --case-bg: color-mix(in srgb, var(--accent-2) 7%, var(--surface));
}
.case {
  position: relative;
  padding: 1.1rem 1.2rem 1.2rem;
  background: var(--case-bg);
  border: 1px solid color-mix(in srgb, var(--accent-2) 45%, transparent);
  border-left: 5px solid var(--accent-2);
  scroll-margin-top: calc(var(--header-h) + 1rem);
}
/* A large, faint copy of the icon in the corner. */
.case::after {
  content: "";
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  width: 5.5rem;
  height: 5.5rem;
  background: var(--accent-2);
  opacity: 0.08;
  -webkit-mask: var(--thornbeck-icon) center / contain no-repeat;
  mask: var(--thornbeck-icon) center / contain no-repeat;
  pointer-events: none;
}
.case > * { position: relative; }
.case-label {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  column-gap: 0.6rem;
  overflow-wrap: anywhere;
  align-items: center;
  margin: 0 0 var(--space);
  line-height: 1.25;
}
.case-label .case-icon { grid-row: span 2; }
.case-icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  background: var(--accent-2);
}
.case-icon::before {
  content: "";
  width: 1.2rem;
  height: 1.2rem;
  background: var(--on-accent);
  -webkit-mask: var(--thornbeck-icon) center / contain no-repeat;
  mask: var(--thornbeck-icon) center / contain no-repeat;
}
.case-who {
  color: var(--accent-2);
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.case-kind { color: var(--muted); font-size: 0.85rem; }
.case h3 { margin-top: 0; }
.case :is(ol, ul) { padding-left: 1.3rem; }

/* Worked examples in the contents sidebar */
.toc-panel .toc-examples { list-style: none; margin: 0.1rem 0 0.25rem 0.5rem; padding: 0; }
.toc-panel .toc-example {
  display: flex;
  gap: 0.4rem;
  align-items: baseline;
  padding: 0.25rem 0.5rem;
  font-size: 0.85rem;
  font-style: italic;
  color: var(--muted);
}
.toc-panel .toc-example::before {
  content: "";
  flex: none;
  align-self: center;
  width: 0.95rem;
  height: 0.95rem;
  background: var(--accent-2);
  -webkit-mask: var(--thornbeck-icon) center / contain no-repeat;
  mask: var(--thornbeck-icon) center / contain no-repeat;
}
.toc-panel .toc-chapter.is-current .toc-example { color: var(--fg); }
.toc-panel .toc-example:hover { border-left-color: var(--accent-2); }

/* Figures and diagrams */
.figure { margin: calc(var(--space) * 4) 0; }
.figure svg, .figure img { display: block; width: 100%; max-width: 100%; height: auto; margin: 0 auto; }
/* Portrait or small diagrams: stop them growing past a comfortable size on desktop. */
.figure.is-compact svg, .figure.is-compact img { max-width: 30rem; }
/* The caption of a compact diagram takes the diagram's width and centre, so its text starts
   and ends at the diagram's edges. The text itself stays left-aligned (ragged right), which
   reads better than centred text over several lines. */
.figure.is-compact figcaption { max-width: 30rem; margin-inline: auto; }
.figure figcaption, .video figcaption {
  margin-top: 0.6rem;
  color: var(--muted);
  font-size: 0.95rem;
}
/* Classes for use inside inline SVG diagrams */
.dg-text { fill: var(--fg); font-family: var(--font); }
.dg-muted { fill: var(--muted); font-family: var(--font); }
.dg-bg { fill: var(--bg); }
.dg-surface { fill: var(--surface); }
.dg-line { stroke: var(--line); fill: none; }
.dg-stroke-fg { stroke: var(--fg); fill: none; }
.dg-accent { fill: var(--accent); }
.dg-accent-stroke { stroke: var(--accent); }
.dg-accent-2 { fill: var(--accent-2); }
.dg-accent-2-stroke { stroke: var(--accent-2); }
.dg-ok { fill: var(--ok); }
.dg-bad { fill: var(--bad); }

/* Video with text summary */
.video { padding: 0; }
.video video {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.video-summary {
  margin-top: 0.75rem;
  padding: 0.9rem 1.1rem;
  background: var(--surface);
  border-radius: var(--radius);
}
.video-summary h3 { margin: 0 0 var(--space); font-size: var(--step-0); }
.video-summary > :last-child { margin-bottom: 0; }

/* Template / copy block */
.copyblock { border: 1px solid var(--line); overflow: hidden; }
.copyblock-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.copyblock-title { font-weight: 700; }
.copyblock-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.copyblock pre:focus-visible { outline-offset: -3px; }
.copyblock pre {
  margin: 0;
  padding: 1rem;
  max-height: 28rem;
  overflow: auto;
  white-space: pre-wrap;
  line-height: 1.5;
}
.copy-status { font-size: 0.9rem; color: var(--muted); }
.copy-status:empty { display: none; }

/* ---------- Quiz ---------- */

.quiz-section h2 { margin-bottom: var(--space); }
.quiz { margin-top: 1rem; }
.quiz-q {
  margin: 0 0 1.25rem;
  padding: 1rem 1.1rem 1.1rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  min-width: 0;
}
.quiz-q legend { float: left; width: 100%; padding: 0; margin: 0 0 0.25rem; font-weight: 700; }
.quiz-q legend + * { clear: both; }
.quiz-num { display: block; color: var(--muted); font-size: 0.85rem; font-weight: 600; }
.quiz-scenario {
  margin: 0.25rem 0 0.75rem;
  padding: 0.6rem 0.8rem;
  background: var(--surface);
  border-left: 4px solid var(--accent-2);
  border-radius: 0 var(--radius) var(--radius) 0;
}
.quiz-hint { color: var(--muted); font-size: 0.9rem; margin: 0.25rem 0 0.5rem; }
.quiz-options { list-style: none; margin: 0 0 0.9rem; padding: 0; }
.quiz-options li { margin: 0.35rem 0 0; }
.quiz-options label {
  display: flex;
  gap: 0.65rem;
  align-items: flex-start;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  cursor: pointer;
}
.quiz-options input {
  flex: none;
  width: 1.2rem; height: 1.2rem;
  margin: 0.2rem 0 0;
  accent-color: var(--accent);
}
.quiz-options label:hover { background: var(--surface); }
/* Selected: accent border (2px, drawn as border + inset shadow so the text doesn't move) and
   an accent tint, with or without the pointer, before and after checking. */
.quiz-options { --selected-bg: color-mix(in srgb, var(--accent) 12%, var(--bg)); }
.quiz-options label:has(input:checked) {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
  background: var(--selected-bg);
}
.quiz-options .is-correct { border: 2px solid var(--ok); }
.quiz-options .is-wrong { border: 2px solid var(--bad); }
/* After checking, the verdict colour wins the border; the tint still shows what was picked. */
.quiz-options label.is-correct:has(input:checked) { border-color: var(--ok); box-shadow: none; }
.quiz-options label.is-wrong:has(input:checked) { border-color: var(--bad); box-shadow: none; }
/* Keyboard focus stays the site-wide :focus-visible ring on the input itself (a11y/manual.js
   checks the focused element), so it is distinct from the selected border and tint. */
.quiz-options .opt-text { min-width: 0; overflow-wrap: anywhere; }
.quiz-options .opt-mark { display: block; font-size: 0.85rem; font-weight: 700; }
.quiz-options .is-correct .opt-mark { color: var(--ok); }
.quiz-options .is-wrong .opt-mark { color: var(--bad); }

.quiz-feedback:empty { display: none; }
.quiz-feedback {
  margin-top: 0.9rem;
  padding: 0.75rem 0.9rem;
  background: var(--surface);
  border-radius: var(--radius);
  border-left: 4px solid var(--line);
}
.quiz-feedback.is-correct { border-left-color: var(--ok); }
.quiz-feedback.is-wrong { border-left-color: var(--bad); }
.quiz-feedback .verdict { font-weight: 700; margin: 0 0 0.25rem; }
.quiz-feedback.is-correct .verdict { color: var(--ok); }
.quiz-feedback.is-wrong .verdict { color: var(--bad); }
.quiz-feedback p:last-child { margin-bottom: 0; }

.quiz-result:empty { display: none; }
.quiz-result {
  padding: 1rem 1.1rem;
  background: var(--surface);
  border: 2px solid var(--accent);
  border-radius: var(--radius);
}
.quiz-result p { margin: 0 0 0.75rem; font-weight: 600; }
.quiz-error { color: var(--bad); font-weight: 600; }

/* ---------- End of chapter, chapter nav ---------- */

.chapter-end {
  padding: 1rem 1.1rem;
  border: 1px solid var(--line);
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
}
.chapter-end p { margin: 0; }
.status-note { color: var(--muted); font-size: 0.95rem; }
.status-note:empty { display: none; }

.chapter-nav {
  display: grid;
  gap: 0.75rem;
  margin: 2rem 0 0;
}
@media (min-width: 600px) { .chapter-nav { grid-template-columns: 1fr 1fr; } }
.chapter-nav a {
  display: block;
  min-width: 0;
  overflow-wrap: anywhere;
  padding: 0.75rem 1rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  text-decoration: none;
}
.chapter-nav a:hover { background: var(--surface); }
.chapter-nav .dir { display: block; color: var(--muted); font-size: 0.85rem; font-weight: 600; }
.chapter-nav .title { display: block; font-weight: 700; text-decoration: underline; text-decoration-thickness: 1px; }
.chapter-nav .next { text-align: right; grid-column: -2; }

/* ---------- Index page ---------- */

.course-facts { display: flex; flex-wrap: wrap; gap: 0.5rem; list-style: none; padding: 0; }
.course-facts li {
  margin: 0;
  padding: 0.25rem 0.75rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 0.95rem;
}
.chapter-list { list-style: none; padding: 0; margin: 1rem 0 0; }
.chapter-list li { margin: 0 0 0.75rem; }
.chapter-card {
  display: block;
  padding: 0.85rem 1rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.chapter-card a { display: inline-block; padding: 0.15rem 0; font-weight: 700; font-size: 1.05rem; }
a[download] { display: inline-block; padding: 0.15rem 0; }
.chapter-card .meta { display: block; color: var(--muted); font-size: 0.9rem; margin-top: 0.2rem; }
.ch-status { display: flex; flex-wrap: wrap; gap: 0.4rem; margin-top: 0.4rem; }
.ch-status:empty { display: none; }
.badge {
  display: inline-block;
  padding: 0.05rem 0.55rem;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 700;
  border: 1px solid currentColor;
}
.badge-read { color: var(--ok); }
.badge-score { color: var(--accent); }
.progress-tools { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; }

/* Landing page (item 21): hero, Start/Continue, overall progress, card grid. */
/* No side inset, so the hero text keeps the page's one left edge (item 15). */
.hero {
  margin: 0 0 2.5rem;
  padding: 1.25rem 0 1.75rem;
  border-top: 4px solid var(--accent);
  border-bottom: 1px solid var(--line);
}
.hero h1 { font-size: calc(var(--step-3) * 1.25); margin-top: 0.5rem; }
.hero .course-facts { margin: 1rem 0 0; }
.hero-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem 1.5rem;
  align-items: center;
  margin-top: 1.5rem;
}
.btn-large {
  display: inline-flex;
  align-items: center;
  font-size: 1.05rem;
  min-height: 3rem;
  padding: 0.7rem 1.4rem;
  text-decoration: none;
}
.btn-large::after { content: "→"; margin-left: 0.5rem; }
.course-progress-wrap { flex: 1 1 14rem; min-width: 0; }
.course-progress-label { margin: 0 0 0.35rem; font-size: 0.9rem; font-weight: 600; color: var(--muted); }
.course-progress {
  height: 0.6rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  overflow: hidden;
}
.course-progress-fill { height: 100%; width: 0; background: var(--ok); }

.chapter-list, .extra-list {
  list-style: none;
  padding: 0;
  margin: 1rem 0 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 19rem), 1fr));
  gap: 0.75rem;
}
.chapter-list li, .extra-list li { margin: 0; }
.chapter-list .chapter-card, .extra-list .chapter-card {
  position: relative;
  height: 100%;
  box-sizing: border-box;
  transition: border-color 0.15s;
}
/* The whole card is the chapter link's hit area; the link itself stays plain text in the HTML. */
.chapter-list .chapter-card > a::after, .extra-list .chapter-card > a::after { content: ""; position: absolute; inset: 0; border-radius: inherit; }
.chapter-list .chapter-card:hover, .extra-list .chapter-card:hover { border-color: var(--accent); }
.chapter-list li:has(.badge-read) .chapter-card { border-color: var(--ok); }

/* Wrap-up checklists (item 22). */
.checklist { list-style: none; padding: 0; }
.checklist li {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 0.1rem 0.75rem;
  align-items: start;
  padding: 0.75rem 0.9rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.checklist li + li { margin-top: 0.6rem; }
.checklist input[type="checkbox"] { width: 1.5rem; height: 1.5rem; margin: 0.15rem 0 0; accent-color: var(--ok); }
.checklist label { cursor: pointer; overflow-wrap: anywhere; }
.checklist li:has(input:checked) { border-color: var(--ok); background: color-mix(in srgb, var(--ok) 7%, var(--bg)); }

/* Glossary definitions on first use (item 24): the term reads as text with a dotted underline. */
.gloss-term {
  font: inherit; line-height: inherit; color: inherit;
  background: none; border: 0; padding: 0; margin: 0;
  text-decoration: underline dotted; text-decoration-thickness: 1.5px; text-underline-offset: 0.2em;
  cursor: pointer;
  max-width: 100%; text-align: start; overflow-wrap: anywhere;
}
button.gloss-term[aria-expanded="true"] { color: var(--accent); text-decoration-style: solid; }
.gloss-def:not([hidden]) {
  display: block;
  margin: 0.4rem 0 0.6rem;
  padding: 0.6rem 0.8rem;
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  background: var(--surface);
  font-size: 0.95em;
  font-weight: normal;
  font-style: normal;
}
.gloss-link { white-space: nowrap; }
/* The card head (term + close button) is only shown when the definition is a margin card. */
.gloss-card-head, .gloss-card-close { display: none; }
/* Margin cards (>=1280px chapter pages; site.js moves the open .gloss-def into .gloss-margin and
   sets its top so it is level with the term's line; later cards are pushed below earlier ones).
   z-index sits above the reading-progress ring and below the sticky header. */
.layout:has(> .gloss-margin) { position: relative; }
.gloss-margin { position: relative; min-width: 0; }
.gloss-margin[hidden] { display: none; }
.gloss-margin .gloss-def.gloss-card:not([hidden]) {
  position: absolute; left: 0; right: 0; z-index: 35;
  margin: 0;
  padding: 0.55rem 0.75rem 0.65rem;
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 0.9rem;
  line-height: 1.45;
  overflow-wrap: anywhere;
}
.gloss-margin .gloss-def.is-orphan { display: none !important; }
.gloss-margin .gloss-def.is-hot { border-color: var(--accent); }
.gloss-margin .gloss-card-head { display: block; margin-bottom: 0.25rem; padding-right: 1.6rem; }
.gloss-card-title { font-weight: 700; color: var(--fg); }
.gloss-margin .gloss-card-close {
  display: block; position: absolute; top: 0.35rem; right: 0.35rem;
  font: inherit; font-size: 1.1rem; line-height: 1;
  width: 1.75rem; height: 1.75rem; margin: 0; padding: 0;
  border: 1px solid transparent; border-radius: var(--radius);
  background: none; color: var(--muted); cursor: pointer;
}
.gloss-margin .gloss-card-close:hover { color: var(--fg); border-color: var(--line); background: var(--bg); }
.gloss-margin .gloss-text { display: block; }
.gloss-margin .gloss-link { display: inline-block; margin-top: 0.3rem; }
/* Connector lines: decorative, drawn over .layout, never take pointer events. Faint by default,
   strong (accent) for the card or term being hovered or focused. */
.gloss-lines { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; z-index: 34; }
.gloss-line { fill: none; stroke: var(--line); stroke-width: 1; opacity: 0.55; }
.gloss-line.is-hot { stroke: var(--accent); stroke-width: 2; opacity: 1; }
button.gloss-term.is-hot { background: color-mix(in srgb, var(--accent) 12%, transparent); border-radius: 2px; }
@media (min-width: 1280px) {
  /* The ring would otherwise sit on top of cards in column 3 at 1280-1330px. */
  body[data-page="chapter"] .read-progress { z-index: 30; }
}
@media print {
  .gloss-margin, .gloss-lines { display: none !important; }
}
/* partials.py wraps a term and the punctuation touching it, so "(" never ends a line alone. */
.gloss-nb { white-space: nowrap; }
.gloss-nb .gloss-term, .gloss-nb .gloss-def { white-space: normal; }

/* Glossary page. */
.glossary-list { margin: 0 0 1rem; }
.glossary-list dt { font-weight: 700; font-size: var(--step-1); margin-top: 1.5rem; scroll-margin-top: calc(var(--header-h) + 1rem); }
.glossary-list dd { margin: 0.25rem 0 0; }
.glossary-list dd p { margin: 0.25rem 0; }
.glossary-list .gloss-source { color: var(--muted); font-size: 0.95rem; }

/* Completion certificate (item 25). */
.cert-notice { padding: 0.75rem 1rem; border-left: 3px solid var(--accent-2); background: var(--surface); border-radius: 0 var(--radius) var(--radius) 0; }
.cert-form-row { display: flex; flex-wrap: wrap; align-items: end; gap: 0.5rem 0.75rem; }
.cert-form-row label { flex-basis: 100%; font-weight: 600; }
.cert-form-row input {
  font: inherit; min-height: 2.75rem; padding: 0.4rem 0.7rem; flex: 1 1 14rem; min-width: 0;
  color: var(--fg); background: var(--bg); border: 2px solid var(--line); border-radius: var(--radius);
}
.cert-status:empty { display: none; }
.certificate:not([hidden]) {
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  gap: 0.5rem; margin: 2rem 0 1rem; padding: 2.5rem 1.5rem; aspect-ratio: 297 / 210;
  border: 3px double var(--accent); border-radius: var(--radius); background: var(--bg);
}
.certificate p { margin: 0; text-wrap: balance; }
.cert-brand { display: flex; align-items: center; gap: 0.6rem; font-weight: 700; font-size: var(--step-1); }
.cert-brand .gmark { width: 2.5rem; height: 2.5rem; fill: currentColor; }
.cert-kicker { font-size: var(--step-3); font-weight: 700; }
.cert-unread a { display: block; padding: 0.25rem 0; }
.cert-name { font-size: var(--step-3); font-weight: 700; color: var(--accent); border-bottom: 1px solid var(--line); padding: 0 1.5rem 0.25rem; overflow-wrap: anywhere; }
.cert-foot { margin-top: 1rem; color: var(--muted); font-size: 0.95rem; }
@media (max-width: 40rem) { .certificate:not([hidden]) { aspect-ratio: auto; } }
@media print {
  body[data-page="certificate"] { background: #fff; color: #000; }
  body[data-page="certificate"] .site-footer, body[data-page="certificate"] .skip-link,
  body[data-page="certificate"] main > :not(.certificate) { display: none !important; }
  body[data-page="certificate"] .layout, body[data-page="certificate"] main { display: block; margin: 0; padding: 0; max-width: none; }
  body[data-page="certificate"] .certificate:not([hidden]) {
    box-sizing: border-box; width: 100%; height: 185mm; margin: 0; aspect-ratio: auto;
    overflow: hidden; break-inside: avoid; border-color: #1f5ea8; color: #000; background: #fff;
  }
  body[data-page="certificate"] .cert-name { color: #1f5ea8; }
  body[data-page="certificate"] .cert-foot { color: #333; }
}

/* ---------- Footer ---------- */

.site-footer {
  border-top: 1px solid var(--line);
  margin-top: 2rem;
  padding: 1.25rem 0 2rem;
  color: var(--muted);
  font-size: 0.95rem;
}
.site-footer-inner {
  max-width: var(--page-max);
  margin: 0 auto;
  padding: 0 var(--gutter);
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1.5rem;
  justify-content: space-between;
}
.site-footer p { margin: 0; }
.site-footer .stamp { margin: 0; }

@media print {
  .site-header, .toc-panel, .toc-backdrop, .chapter-nav, .quiz, .chapter-end, .read-progress { display: none !important; }
}
