/* AI Tutorial — one design system for every page.
   Type scale, colour tokens (light and dark), layout, and the components the
   modules share: demo figures, controls, exercises, self-checks, pager.
   Body text is 18px; gutters are 16px at phone width; one accent colour. */

:root {
  color-scheme: light dark;

  --font-body: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

  /* type scale (1rem = 16px; body is 18px) */
  --text-xs: 0.8125rem;
  --text-sm: 0.9375rem;
  --text-md: 1.125rem;
  --text-lg: 1.375rem;
  --text-xl: 1.75rem;
  --text-2xl: clamp(2rem, 1.4rem + 3vw, 3rem);

  --gutter: 16px;
  --measure: 44rem;
  --wide: 60rem;
  --radius: 6px;

  /* light theme */
  --bg: #faf8f3;
  --surface: #ffffff;
  --sunken: #f1eee6;
  --ink: #1d1c1a;
  --muted: #5c5952;
  --rule: #dcd7cb;
  --accent: #b4400a;
  --accent-ink: #ffffff;
  --accent-soft: #f7e4d7;
  --cool: #1f5f95;          /* second data colour, for the "other" class in plots */
  --good: #256b37;
  --bad: #a3231f;
  --code-kw: #8a2f08;
  --code-str: #256b37;
  --code-num: #1f5f95;
  --code-com: #68645c;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171a;
    --surface: #1f2125;
    --sunken: #101114;
    --ink: #eceae4;
    --muted: #a9a59c;
    --rule: #383a41;
    --accent: #ff9a57;
    --accent-ink: #1d1105;
    --accent-soft: #3b2715;
    --cool: #82b6ea;
    --good: #82cd92;
    --bad: #f39086;
    --code-kw: #ffb07c;
    --code-str: #9ad6a6;
    --code-num: #9cc6ef;
    --code-com: #8f8b83;
  }
}
:root[data-theme="dark"] {
  --bg: #16171a;
  --surface: #1f2125;
  --sunken: #101114;
  --ink: #eceae4;
  --muted: #a9a59c;
  --rule: #383a41;
  --accent: #ff9a57;
  --accent-ink: #1d1105;
  --accent-soft: #3b2715;
  --cool: #82b6ea;
  --good: #82cd92;
  --bad: #f39086;
  --code-kw: #ffb07c;
  --code-str: #9ad6a6;
  --code-num: #9cc6ef;
  --code-com: #8f8b83;
}
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

/* ---- base ---------------------------------------------------------------- */

*, *::before, *::after { box-sizing: border-box; }
html { font-size: 100%; -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

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

img, svg, canvas, video { max-width: 100%; }
[hidden] { display: none !important; }

a { color: var(--accent); text-underline-offset: 0.15em; text-decoration-thickness: 1px; }
a:hover { text-decoration-thickness: 2px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 2px; }

h1, h2, h3 { line-height: 1.2; font-weight: 700; margin: 0; text-wrap: balance; }
h1 { font-size: var(--text-2xl); letter-spacing: -0.01em; }
h2 { font-size: var(--text-xl); margin-top: 3rem; padding-top: 1.25rem; border-top: 1px solid var(--rule); }
h3 { font-size: var(--text-lg); margin-top: 2rem; }
p, ul, ol, pre, figure, table, details, .katex-display { margin: 1rem 0 0; }
ul, ol { padding-left: 1.4rem; }
li + li { margin-top: 0.35rem; }
strong { font-weight: 700; }
hr { border: 0; border-top: 1px solid var(--rule); margin: 2.5rem 0 0; }

code, pre, kbd { font-family: var(--font-mono); font-size: 0.86em; }
code { background: var(--sunken); padding: 0.1em 0.3em; border-radius: 3px; }
pre {
  background: var(--sunken);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  overflow-x: auto;
  line-height: 1.5;
  font-size: var(--text-sm);
  tab-size: 4;
}
pre code { background: none; padding: 0; font-size: inherit; }

.katex { font-size: 1.05em; }
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 0.25rem 0; }
.katex-display > .katex { white-space: nowrap; }

.table-wrap { overflow-x: auto; margin-top: 1rem; }
table { border-collapse: collapse; width: 100%; font-family: var(--font-ui); font-size: var(--text-sm); margin: 0; }
th, td { text-align: left; padding: 0.5rem 0.75rem 0.5rem 0; border-bottom: 1px solid var(--rule); vertical-align: top; }
th { color: var(--muted); font-weight: 600; }

.skip { position: absolute; left: -999px; top: 0; background: var(--accent); color: var(--accent-ink); padding: 0.5rem 1rem; z-index: 10; font-family: var(--font-ui); }
.skip:focus { left: 0; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* ---- layout -------------------------------------------------------------- */

.site-header, main, .site-footer { padding-inline: var(--gutter); }
.site-header > *, main > *, .site-footer > * { max-width: var(--measure); margin-inline: auto; }
main > .wide { max-width: var(--wide); }
main { padding-bottom: 4rem; }

.site-header { border-bottom: 1px solid var(--rule); font-family: var(--font-ui); font-size: var(--text-sm); }
.site-header .bar { display: flex; flex-wrap: wrap; align-items: center; gap: 0.25rem 1.25rem; padding-block: 0.75rem; }
.brand { font-weight: 700; color: var(--ink); text-decoration: none; margin-right: auto; letter-spacing: 0.01em; }
.brand span { color: var(--accent); }
.site-header nav { display: flex; flex-wrap: wrap; align-items: center; gap: 0.25rem 1.1rem; }
.site-header nav a { color: var(--muted); text-decoration: none; }
.site-header nav a:hover, .site-header nav a[aria-current] { color: var(--ink); text-decoration: underline; }

.site-footer { border-top: 1px solid var(--rule); font-family: var(--font-ui); font-size: var(--text-xs); color: var(--muted); padding-block: 1.5rem 2.5rem; }
.site-footer p { margin-top: 0; }

/* ---- buttons and controls ------------------------------------------------ */

button, input, select, textarea { font: inherit; color: inherit; }
.btn {
  font-family: var(--font-ui); font-size: var(--text-sm); font-weight: 600; line-height: 1.2;
  padding: 0.55rem 0.9rem; min-height: 2.5rem;
  border: 1px solid var(--rule); border-radius: var(--radius);
  background: var(--surface); color: var(--ink); cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { color: var(--accent-ink); filter: brightness(1.08); }
.btn-quiet { border-color: transparent; background: none; color: var(--muted); padding-inline: 0.4rem; }
.btn[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--ink); }

.controls { display: flex; flex-wrap: wrap; gap: 0.6rem 1rem; align-items: end; margin-top: 0.9rem; font-family: var(--font-ui); font-size: var(--text-sm); }
.controls label { display: grid; gap: 0.15rem; color: var(--muted); min-width: 0; }
.controls label output { color: var(--ink); font-family: var(--font-mono); font-size: var(--text-xs); }
.controls label .row { display: flex; justify-content: space-between; gap: 0.5rem; }
.controls input[type="range"] { width: 9.5rem; max-width: 100%; accent-color: var(--accent); margin: 0; height: 1.5rem; }
.controls select { padding: 0.4rem 0.5rem; min-height: 2.5rem; border: 1px solid var(--rule); border-radius: var(--radius); background: var(--surface); }
.controls .group { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.segmented { display: inline-flex; border: 1px solid var(--rule); border-radius: var(--radius); overflow: hidden; }
.segmented .btn { border: 0; border-radius: 0; }
.segmented .btn + .btn { border-left: 1px solid var(--rule); }

.readout { font-family: var(--font-mono); font-size: var(--text-sm); color: var(--muted); margin-top: 0.75rem; display: flex; flex-wrap: wrap; gap: 0.25rem 1.25rem; }
.readout b { color: var(--ink); font-weight: 600; }

/* ---- module page --------------------------------------------------------- */

.kicker { font-family: var(--font-ui); font-size: var(--text-sm); font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--accent); margin-top: 2.5rem; }
.kicker + h1 { margin-top: 0.4rem; }
.lede { font-size: var(--text-lg); line-height: 1.5; color: var(--ink); }
.meta { font-family: var(--font-ui); font-size: var(--text-sm); color: var(--muted); }

.progress { display: flex; flex-wrap: wrap; gap: 4px; list-style: none; padding: 0 0 0.75rem; margin: 0 auto; }
.progress li { margin: 0; }
.progress a, .progress span {
  display: grid; place-items: center; width: 1.5rem; height: 1.5rem;
  font-family: var(--font-mono); font-size: 0.6875rem; text-decoration: none;
  border: 1px solid var(--rule); border-radius: 3px; color: var(--muted); background: var(--surface);
}
.progress span { border-style: dashed; background: none; }
.progress a:hover { border-color: var(--accent); color: var(--accent); }
.progress .passed { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.progress [aria-current] { border-color: var(--ink); color: var(--ink); box-shadow: 0 0 0 1px var(--ink); }

figure.demo {
  background: var(--surface); border: 1px solid var(--rule); border-radius: var(--radius);
  padding: 1rem; margin-top: 1.5rem;
}
figure.demo > figcaption { font-family: var(--font-ui); font-size: var(--text-sm); color: var(--muted); margin-top: 0.75rem; }
figure.demo .title { font-family: var(--font-ui); font-weight: 700; font-size: var(--text-sm); letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); margin: 0; }
.panels { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 17rem), 1fr)); margin-top: 0.75rem; }
.panel { min-width: 0; }
.panel-title { font-family: var(--font-ui); font-size: var(--text-xs); font-weight: 600; color: var(--muted); margin: 0 0 0.35rem; }
.panel svg, .panel canvas { display: block; width: 100%; height: auto; background: var(--bg); border: 1px solid var(--rule); border-radius: 4px; }
.plot text { font-family: var(--font-ui); font-size: 11px; fill: var(--muted); }
.plot .axis { stroke: var(--rule); stroke-width: 1; fill: none; }
.plot .handle { cursor: grab; touch-action: none; }
.plot .handle:active { cursor: grabbing; }
.note { font-family: var(--font-ui); font-size: var(--text-sm); color: var(--muted); }

.try { background: var(--accent-soft); border-radius: var(--radius); padding: 1rem 1.1rem; margin-top: 1.5rem; }
.try h3 { margin-top: 0; font-family: var(--font-ui); font-size: var(--text-sm); letter-spacing: 0.06em; text-transform: uppercase; }
.try ol { margin-top: 0.5rem; }

.callout { border-left: 3px solid var(--accent); padding: 0.1rem 0 0.1rem 1rem; margin-top: 1.25rem; color: var(--muted); }
.callout p:first-child { margin-top: 0; }

details.check { border: 1px solid var(--rule); border-radius: var(--radius); background: var(--surface); padding: 0.75rem 1rem; margin-top: 0.75rem; }
details.check summary { cursor: pointer; font-weight: 700; }
details.check[open] summary { margin-bottom: 0.25rem; }
details.check p:first-of-type { margin-top: 0.5rem; }

/* ---- exercise ------------------------------------------------------------ */

.exercise { background: var(--surface); border: 1px solid var(--rule); border-top: 3px solid var(--accent); border-radius: var(--radius); padding: 1rem; margin-top: 1.5rem; }
.exercise > h3 { margin-top: 0; }
.ex-status { font-family: var(--font-ui); font-size: var(--text-sm); color: var(--muted); margin-top: 0.25rem; }
.ex-status.passed { color: var(--good); font-weight: 600; }
.ex-editor { margin-top: 1rem; border: 1px solid var(--rule); border-radius: var(--radius); overflow: hidden; background: var(--bg); }
.ex-editor textarea {
  display: block; width: 100%; min-height: 20rem; border: 0; resize: vertical; padding: 0.75rem;
  font-family: var(--font-mono); font-size: 0.875rem; line-height: 1.5; background: var(--bg); color: var(--ink);
  white-space: pre; overflow: auto; tab-size: 4;
}
.ex-editor .cm-editor { font-size: 0.875rem; }
.ex-editor .cm-editor.cm-focused { outline: 2px solid var(--accent); outline-offset: -2px; }
.ex-editor .cm-scroller { font-family: var(--font-mono); line-height: 1.5; }
.ex-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.75rem; }
.ex-output { margin-top: 0.75rem; font-family: var(--font-ui); font-size: var(--text-sm); }
.ex-output:empty { display: none; }
.ex-summary { font-weight: 700; margin: 0; }
.ex-summary.ok { color: var(--good); }
.ex-summary.fail { color: var(--bad); }
.ex-results { list-style: none; padding: 0; margin-top: 0.5rem; }
.ex-results li { padding: 0.4rem 0 0.4rem 1.6rem; position: relative; border-top: 1px solid var(--rule); margin: 0; }
.ex-results li::before { position: absolute; left: 0.1rem; font-weight: 700; }
.ex-results li.ok::before { content: "✓"; color: var(--good); }
.ex-results li.fail::before { content: "✗"; color: var(--bad); }
.ex-results .msg { display: block; font-family: var(--font-mono); font-size: var(--text-xs); color: var(--muted); white-space: pre-wrap; margin-top: 0.15rem; }
.ex-stdout { margin-top: 0.75rem; max-height: 14rem; }
.ex-hint, .ex-solution, .ex-provided { margin-top: 0.75rem; border-left: 3px solid var(--rule); padding-left: 0.9rem; font-size: var(--text-md); }
.ex-hint > :first-child, .ex-solution > :first-child { margin-top: 0; }
.ex-provided summary { cursor: pointer; font-family: var(--font-ui); font-size: var(--text-sm); color: var(--muted); }

/* ---- pager --------------------------------------------------------------- */

.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; margin-top: 3rem; font-family: var(--font-ui); }
.pager a, .pager .soon { display: block; padding: 0.8rem 1rem; border: 1px solid var(--rule); border-radius: var(--radius); text-decoration: none; color: var(--ink); background: var(--surface); min-width: 0; }
.pager a:hover { border-color: var(--accent); }
.pager .dir { display: block; font-size: var(--text-xs); color: var(--muted); letter-spacing: 0.06em; text-transform: uppercase; }
.pager .next { text-align: right; grid-column: 2; }
.pager .soon { color: var(--muted); border-style: dashed; }
@media (max-width: 30rem) { .pager { grid-template-columns: 1fr; } .pager .next { grid-column: 1; text-align: left; } }

/* ---- course map (index) -------------------------------------------------- */

.hero { padding-top: 3rem; }
.hero .lede { margin-top: 1.25rem; }
.hero .actions { display: flex; flex-wrap: wrap; gap: 0.75rem; margin-top: 1.5rem; }
.hero .btn { text-decoration: none; display: inline-flex; align-items: center; }
.facts { list-style: none; padding: 0; display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 13rem), 1fr)); margin-top: 2rem; }
.facts li { margin: 0; border-top: 2px solid var(--ink); padding-top: 0.6rem; font-size: var(--text-sm); font-family: var(--font-ui); color: var(--muted); }
.facts b { display: block; color: var(--ink); font-size: var(--text-md); font-family: var(--font-body); }

.course-map { list-style: none; padding: 0; margin-top: 1.5rem; counter-reset: none; }
.course-map > li { margin: 0; display: grid; grid-template-columns: 2.75rem 1fr; gap: 0 0.75rem; padding: 1rem 0; border-top: 1px solid var(--rule); }
.course-map .num { font-family: var(--font-mono); font-size: var(--text-lg); color: var(--accent); line-height: 1.3; }
.course-map h3 { margin: 0; font-size: var(--text-lg); }
.course-map h3 a { color: var(--ink); text-decoration: none; }
.course-map h3 a:hover { color: var(--accent); text-decoration: underline; }
.course-map p { margin-top: 0.3rem; font-size: var(--text-sm); font-family: var(--font-ui); color: var(--muted); }
.course-map p b { color: var(--ink); font-weight: 600; }
.course-map .soon h3 { color: var(--muted); }
.tag { display: inline-block; font-family: var(--font-ui); font-size: var(--text-xs); font-weight: 600; padding: 0.1rem 0.5rem; border-radius: 99px; border: 1px solid var(--rule); color: var(--muted); vertical-align: middle; margin-left: 0.4rem; white-space: nowrap; }
.tag.passed { border-color: var(--good); color: var(--good); }
