/* ==========================================================================
   Shadow — one stylesheet, mobile first.
   Order: tokens → reset → base type → layout → components → preferences
   Three adjectives that every choice must fit: quiet · mysterious · precise
   ========================================================================== */

/* ---------- 1. Tokens (lecture 6 + 8): named by role, not by value -------- */
:root {
  color-scheme: light dark;              /* follows the visitor's system */

  /* colour: one dominant (night indigo), one accent (low-sun amber), tinted neutrals */
  --c-bg:        light-dark(#f5f3ee, #14131a);
  --c-surface:   light-dark(#ffffff, #1e1c27);
  --c-sunken:    light-dark(#ebe8f0, #0e0d13);
  --c-text:      light-dark(#1d1b26, #ebe9f2);
  --c-muted:     light-dark(#55516a, #b3afc4);
  --c-brand:     light-dark(#2f2a6b, #b8b1f5);
  --c-accent:    light-dark(#8f4700, #f2a65a);
  --c-border:    light-dark(#cfcadb, #3a3748);
  --c-focus:     light-dark(#b35300, #ffb768);
  --c-on-brand:  light-dark(#ffffff, #14131a);
  --c-shadow:    light-dark(rgb(29 27 38 / .14), rgb(0 0 0 / .55));

  /* type: system fonts only (lecture 5) — nothing to download */
  --f-body: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --f-head: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --f-mono: ui-monospace, "Cascadia Code", Consolas, "Liberation Mono", monospace;

  /* type scale: ratio 1.25 per step; headings flow with clamp(), rem keeps zoom working */
  --step--1: 0.875rem;
  --step-0:  1rem;
  --step-1:  clamp(1.15rem, 1.08rem + 0.35vw, 1.25rem);
  --step-2:  clamp(1.35rem, 1.2rem + 0.75vw, 1.563rem);
  --step-3:  clamp(1.6rem, 1.3rem + 1.4vw, 1.953rem);
  --step-4:  clamp(2rem, 1.5rem + 2.6vw, 3.05rem);

  /* spacing: the same 1.5 ratio everywhere */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 1rem;
  --space-4: 1.5rem;
  --space-5: 2.25rem;
  --space-6: 3.5rem;

  --radius: 0.5rem;
  --measure: 65ch;
  --wide: 72rem;

  /* elevation: shadows are the subject, so they are tokens too */
  --shadow-1: 0 1px 2px var(--c-shadow);
  --shadow-2: 0 2px 4px var(--c-shadow), 0 8px 24px var(--c-shadow);
}

/* the theme toggle (js/app.js) only switches color-scheme: light-dark() does the rest */
:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"]  { color-scheme: dark; }

/* ---------- 2. Reset --------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
body, h1, h2, h3, h4, p, figure, blockquote, dl, dd { margin: 0; }
img, svg, video, picture { display: block; max-width: 100%; height: auto; }
input, button, select { font: inherit; }
[hidden] { display: none !important; }

/* ---------- 3. Base ---------------------------------------------------- */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: var(--f-body);
  font-size: var(--step-0);
  line-height: 1.5;
  color: var(--c-text);
  background: var(--c-bg);
}

h1, h2, h3, h4 {
  font-family: var(--f-head);
  line-height: 1.15;
  font-weight: 700;
  color: var(--c-brand);
  text-wrap: balance;
}
h1 { font-size: var(--step-4); letter-spacing: -0.01em; }
h2 { font-size: var(--step-3); }
h3 { font-size: var(--step-2); }
h4 { font-size: var(--step-1); }

p, li, dd { max-width: var(--measure); text-wrap: pretty; }
p { hyphens: auto; }

a { color: var(--c-brand); text-decoration-thickness: 0.08em; text-underline-offset: 0.18em; }
a:hover { color: var(--c-accent); text-decoration-thickness: 0.14em; }

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

abbr[title] { text-decoration: underline dotted; cursor: help; }
code, kbd { font-family: var(--f-mono); font-size: 0.92em; }
code { background: var(--c-sunken); padding: 0.1em 0.3em; border-radius: 0.25em; font-variant-ligatures: none; }
pre { background: var(--c-sunken); padding: var(--space-3); border-radius: var(--radius); overflow-x: auto; max-width: 100%; line-height: 1.4; }
pre code { background: none; padding: 0; }

.lede { font-size: var(--step-1); color: var(--c-muted); }
.small { font-size: var(--step--1); }
.muted { color: var(--c-muted); }
.caps { text-transform: uppercase; letter-spacing: 0.08em; font-size: var(--step--1); font-weight: 600; }

.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;
}

/* ---------- 4. Page frame ---------------------------------------------- */
.skip-link {
  position: absolute; left: var(--space-3); top: -10rem; z-index: 10;
  padding: var(--space-2) var(--space-3);
  background: var(--c-brand); color: var(--c-on-brand); font-weight: 600;
  border-radius: var(--radius);
}
.skip-link:focus { top: var(--space-3); color: var(--c-on-brand); }

.wrap { width: min(100% - 2 * var(--space-3), var(--wide)); margin-inline: auto; }

.site-header {
  border-bottom: 1px solid var(--c-border);
  background: var(--c-surface);
}
.site-header .wrap {
  display: flex; flex-wrap: wrap; align-items: center;
  gap: var(--space-2) var(--space-3);
  padding-block: var(--space-2);
}
.brand {
  display: inline-flex; align-items: center; gap: var(--space-2);
  font-family: var(--f-head); font-size: var(--step-2); font-weight: 700;
  color: var(--c-text); text-decoration: none;
  margin-inline-end: auto;
  min-height: 44px;
}
.brand svg { width: 1.6em; height: 1.6em; color: var(--c-brand); }
.brand:hover { color: var(--c-brand); }

.header-tools { display: flex; gap: var(--space-2); }

/* navigation: without JavaScript every link is visible and simply wraps */
.site-nav { flex-basis: 100%; }
.nav-list {
  list-style: none; margin: 0; padding: 0;
  display: flex; flex-wrap: wrap; gap: var(--space-1) var(--space-2);
}
.nav-list a {
  display: block; padding: var(--space-2) var(--space-2);
  min-height: 44px; /* target size, WCAG 2.5.8 asks for at least 24px */
  text-decoration: none; color: var(--c-text); border-radius: var(--radius);
}
.nav-list a:hover { background: var(--c-sunken); color: var(--c-brand); }
.nav-list a[aria-current="page"] {
  color: var(--c-brand); font-weight: 700;
  text-decoration: underline; text-decoration-thickness: 0.15em; text-underline-offset: 0.35em;
}

/* two links only: always visible, no menu button needed (lecture 4: "few links: always visible") */
:root:not(.js) .theme-toggle { display: none; }

main { flex: 1; }
main:focus { outline: none; }

.site-footer {
  margin-top: var(--space-6);
  padding-block: var(--space-5);
  background: var(--c-sunken);
  color: var(--c-muted);
  font-size: var(--step--1);
}
.site-footer .wrap { display: grid; gap: var(--space-3); }
.site-footer ul { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); }
.site-footer a { color: var(--c-text); }

/* ---------- 5. Content layout ------------------------------------------ */
.page-head { padding-block: var(--space-5) var(--space-4); }
.page-head p { margin-top: var(--space-3); }

.flow > * + * { margin-top: var(--space-3); }
.flow > h2 { margin-top: var(--space-6); }
.flow > h3 { margin-top: var(--space-5); }
.flow > h2 + *, .flow > h3 + * { margin-top: var(--space-3); }

/* article with an optional side column ("column drop" pattern, lecture 4) */
.article { display: grid; gap: var(--space-5); }
.article > *, .lab > * { min-width: 0; } /* let tables and code scroll inside, not the page */
.toc {
  align-self: start;
  padding: var(--space-3) var(--space-4);
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
}
.toc h2 { font-size: var(--step-1); margin-bottom: var(--space-2); }
.toc ol { margin: 0; padding-inline-start: 1.25rem; }
.toc li + li { margin-top: var(--space-1); }

/* cards: as many columns as fit, no media query (lecture 4) */
.cards {
  display: grid; gap: var(--space-4);
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
  padding: 0; list-style: none;
}
.card-host { container-type: inline-size; }

.card {
  position: relative;
  display: flex; flex-direction: column; gap: var(--space-2);
  height: 100%;
  padding: var(--space-4);
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-1);
  transition: box-shadow .2s ease, transform .2s ease;
}
.card h3 { font-size: var(--step-1); }
.card p { color: var(--c-muted); }
.card .card-link { text-decoration: none; color: var(--c-brand); }
.card .card-link::after { content: ""; position: absolute; inset: 0; border-radius: var(--radius); }
.card:hover { box-shadow: var(--shadow-2); transform: translateY(-2px); }
.card:has(.card-link:focus-visible) { outline: 3px solid var(--c-focus); outline-offset: 3px; }
.card .card-link:focus-visible { outline: none; }
.card-icon { width: 2.5rem; height: 2.5rem; color: var(--c-accent); }

/* the same card, wider: icon beside text (container query, lecture 1 + 4) */
@container (min-width: 26rem) {
  .card { flex-direction: row; align-items: flex-start; gap: var(--space-3); }
  .card-icon { flex: none; }
}

/* facts: numbered "did you know" list */
.facts { counter-reset: fact; list-style: none; padding: 0; display: grid; gap: var(--space-3); }
.facts li {
  counter-increment: fact;
  display: grid; grid-template-columns: auto 1fr; gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--c-surface);
  border-inline-start: 4px solid var(--c-accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  max-width: none;
}
.facts li::before {
  content: counter(fact, decimal-leading-zero);
  font-family: var(--f-head); font-size: var(--step-2); font-weight: 700; color: var(--c-accent);
  line-height: 1.1;
}
.facts strong { color: var(--c-text); }

/* hero with art-directed picture */
.hero { display: grid; gap: var(--space-4); padding-block: var(--space-4) var(--space-5); }
.hero-media { border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow-2); background: #3b2c3f; }
.hero-media img { width: 100%; aspect-ratio: 4 / 5; object-fit: cover; }
.hero-text h1 { font-size: clamp(2.6rem, 1.6rem + 6vw, 5.5rem); line-height: 1; }
.hero-text .pron { margin-top: var(--space-2); color: var(--c-muted); font-family: var(--f-head); font-size: var(--step-1); }
.hero-text .message { margin-top: var(--space-3); font-size: var(--step-1); }

/* figures and diagrams */
figure { margin: 0; }
figcaption { margin-top: var(--space-2); font-size: var(--step--1); color: var(--c-muted); max-width: var(--measure); }
.diagram {
  padding: var(--space-3);
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: var(--radius);
}
.diagram svg { width: 100%; height: auto; color: var(--c-text); }
/* diagrams of real light stay in "daylight" in both themes: shadows must be darker than the ground */
.diagram-day { background: #f7f1e6; color-scheme: light; }
.diagram-day svg, .diagram-day figcaption { color: #1d1b26; }

/* definition lists for meanings */
.defs { display: grid; gap: var(--space-3); }
.defs dt { font-weight: 700; color: var(--c-brand); }
.defs dd { margin-inline-start: 0; }
.defs dd + dt { margin-top: var(--space-2); }

/* tables of words */
.table-wrap { overflow-x: auto; max-width: 100%; }
.table-wrap:focus-visible, pre:focus-visible { outline: 3px solid var(--c-focus); outline-offset: 2px; }
table { border-collapse: collapse; width: 100%; min-width: 17rem; font-size: var(--step--1); }
caption { text-align: start; font-weight: 700; padding-bottom: var(--space-2); color: var(--c-text); }
th, td { text-align: start; vertical-align: top; padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--c-border); }
thead th { color: var(--c-muted); font-weight: 600; }
td:first-child, th[scope="row"] { font-weight: 600; }
td[lang], span[lang] { font-style: italic; }

blockquote {
  padding: var(--space-3) var(--space-4);
  border-inline-start: 4px solid var(--c-brand);
  background: var(--c-surface);
  font-family: var(--f-head); font-size: var(--step-1);
}
blockquote footer { margin-top: var(--space-2); font-family: var(--f-body); font-size: var(--step--1); color: var(--c-muted); }

.note {
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--c-border);
  border-inline-start: 4px solid var(--c-accent);
  background: var(--c-surface);
  border-radius: 0 var(--radius) var(--radius) 0;
}
.note > :first-child { margin-top: 0; }

.sources { font-size: var(--step--1); color: var(--c-muted); }
.sources li + li { margin-top: var(--space-1); }

.pager {
  display: flex; flex-wrap: wrap; justify-content: space-between; gap: var(--space-3);
  margin-top: var(--space-6); padding-top: var(--space-4); border-top: 1px solid var(--c-border);
}
.pager a { display: inline-flex; flex-direction: column; text-decoration: none; padding: var(--space-2) 0; }
.pager a span { color: var(--c-muted); font-size: var(--step--1); }
.pager .next { margin-inline-start: auto; text-align: end; }

/* ---------- 6. Components (see styleguide.html for all states) -------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: 44px; padding: var(--space-2) var(--space-4);
  font-weight: 600; line-height: 1.2; text-decoration: none;
  color: var(--c-on-brand); background: var(--c-brand);
  border: 2px solid var(--c-brand); border-radius: var(--radius);
  box-shadow: var(--shadow-1);
  cursor: pointer;
  transition: background-color .15s ease, box-shadow .15s ease, transform .15s ease;
}
.btn:hover { background: color-mix(in oklch, var(--c-brand), var(--c-text) 25%); color: var(--c-on-brand); box-shadow: var(--shadow-2); }
.btn:active { transform: translateY(1px); box-shadow: none; }
.btn:disabled, .btn[aria-disabled="true"] {
  cursor: not-allowed; color: var(--c-muted); background: var(--c-sunken); border-color: var(--c-border); box-shadow: none; transform: none;
}
.btn-ghost { color: var(--c-brand); background: transparent; box-shadow: none; }
.btn-ghost:hover { background: var(--c-sunken); color: var(--c-brand); box-shadow: none; }
.btn svg { width: 1.25em; height: 1.25em; flex: none; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-width: 44px; min-height: 44px; padding: var(--space-1) var(--space-3);
  color: var(--c-text); background: var(--c-surface);
  border: 1px solid var(--c-border); border-radius: var(--radius);
  cursor: pointer;
}
.icon-btn:hover { background: var(--c-sunken); color: var(--c-brand); }
.icon-btn svg { width: 1.25rem; height: 1.25rem; }

.field { display: grid; gap: var(--space-1); max-width: 24rem; }
.field label { font-weight: 600; }
.field input {
  min-height: 44px; padding: var(--space-2) var(--space-3);
  color: var(--c-text); background: var(--c-surface);
  border: 2px solid var(--c-border); border-radius: var(--radius);
}
.field input:hover { border-color: var(--c-muted); }
.field input:focus-visible { border-color: var(--c-focus); outline: 3px solid var(--c-focus); outline-offset: 1px; }
.field .hint { color: var(--c-muted); font-size: var(--step--1); }
.field .error { display: flex; gap: var(--space-1); align-items: center; color: light-dark(#a1121e, #ff9aa2); font-size: var(--step--1); font-weight: 600; }
.field input[aria-invalid="true"] { border-color: light-dark(#a1121e, #ff9aa2); }
.field input:disabled { background: var(--c-sunken); color: var(--c-muted); cursor: not-allowed; }

.tag {
  display: inline-block; padding: 0.15em 0.6em;
  font-size: var(--step--1); font-weight: 600;
  color: var(--c-brand); background: color-mix(in oklch, var(--c-brand) 12%, transparent);
  border: 1px solid color-mix(in oklch, var(--c-brand) 35%, transparent);
  border-radius: 999px;
}

/* ---------- 7. Demos (web.html) ---------------------------------------- */
.demo-grid { display: grid; gap: var(--space-4); grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr)); }
.demo-box {
  display: grid; place-items: center; min-height: 9rem; padding: var(--space-4);
  background: var(--c-surface); border-radius: var(--radius); text-align: center;
}
.demo-box.s-hard { box-shadow: 6px 6px 0 var(--c-brand); }
.demo-box.s-soft { box-shadow: 0 10px 30px var(--c-shadow); }
.demo-box.s-layers { box-shadow: 0 1px 1px var(--c-shadow), 0 2px 2px var(--c-shadow), 0 4px 4px var(--c-shadow), 0 8px 8px var(--c-shadow), 0 16px 16px var(--c-shadow); }
.demo-box.s-inset { box-shadow: inset 0 2px 10px var(--c-shadow); background: var(--c-sunken); }
.demo-text { font-family: var(--f-head); font-size: var(--step-3); font-weight: 700; color: var(--c-brand); text-shadow: 3px 3px 0 color-mix(in oklch, var(--c-accent) 55%, transparent); }
.demo-drop { color: var(--c-brand); filter: drop-shadow(5px 6px 2px var(--c-shadow)); width: 6rem; }

.lab {
  display: grid; gap: var(--space-4);
  padding: var(--space-4);
  background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius);
}
.lab-stage {
  --angle: 45deg; --dist: 14px; --blur: 8px;
  display: grid; place-items: center; min-height: 14rem;
  background: var(--c-sunken); border-radius: var(--radius); overflow: hidden;
}
.lab-object {
  width: 7rem; aspect-ratio: 1; border-radius: var(--radius);
  background: var(--c-brand);
  /* the shadow offset is computed from an angle and a distance: trigonometry in CSS */
  box-shadow: calc(cos(var(--angle)) * var(--dist)) calc(sin(var(--angle)) * var(--dist)) var(--blur) var(--c-shadow);
}
.lab-controls { display: grid; gap: var(--space-3); }
.lab-row { display: grid; grid-template-columns: 1fr auto; gap: var(--space-1) var(--space-2); align-items: baseline; }
.lab-row[hidden] { display: none; }
.lab-row label { font-weight: 600; }
.lab-row input { grid-column: 1 / -1; }
.lab-controls output { font-family: var(--f-mono); font-weight: 400; color: var(--c-muted); }
.lab-controls input[type="range"] { width: 100%; min-height: 44px; accent-color: var(--c-accent); }
.lab-code { margin: 0; }

/* sundial on the home page — the gnomon's shadow follows the visitor's clock */
.sundial svg { max-width: 16rem; margin-inline: auto; }
.sundial .gnomon-shadow { transform-origin: 100px 100px; transform: rotate(var(--sun-rot, 0deg)); transition: transform 1s ease; }

/* ---------- 8. Style guide page --------------------------------------- */
.swatches { display: grid; gap: var(--space-3); grid-template-columns: repeat(auto-fill, minmax(min(100%, 11rem), 1fr)); list-style: none; padding: 0; }
.swatch { border: 1px solid var(--c-border); border-radius: var(--radius); overflow: hidden; background: var(--c-surface); }
.swatch-chip { height: 4rem; }
.swatch p { padding: var(--space-2) var(--space-3); font-size: var(--step--1); }
.swatch code { background: none; padding: 0; }
.specimen > * + * { margin-top: var(--space-2); }
.states { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: flex-start; }
.states figure { display: grid; gap: var(--space-2); justify-items: start; }
.is-hover.btn { background: color-mix(in oklch, var(--c-brand), var(--c-text) 25%); box-shadow: var(--shadow-2); }
.is-focus { outline: 3px solid var(--c-focus); outline-offset: 3px; }
.is-active.btn { transform: translateY(1px); box-shadow: none; }

/* ---------- 9. Wider screens: breakpoints where the content breaks ---- */
@media (min-width: 40em) {
  .hero-media img { aspect-ratio: 16 / 9; }
}

@media (min-width: 48em) {
  .site-nav { flex-basis: auto; }
  .site-header .wrap { flex-wrap: nowrap; }
  .site-footer .wrap { grid-template-columns: 2fr 1fr; align-items: start; }
}

@media (min-width: 64em) {
  .hero { grid-template-columns: 5fr 7fr; align-items: center; gap: var(--space-5); }
  .hero-text { order: -1; }
  .article { grid-template-columns: minmax(0, 1fr) 15rem; }
  .article .toc { position: sticky; top: var(--space-4); order: 2; }
  .lab { grid-template-columns: 1fr 1fr; align-items: center; }
}

/* ---------- 10. Visitor preferences (lecture 1, 6) -------------------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
  .card:hover { transform: none; }
}

@media (prefers-contrast: more) {
  :root {
    --c-text: light-dark(#000, #fff);
    --c-muted: light-dark(#1d1b26, #e6e4ef);
    --c-border: light-dark(#1d1b26, #e6e4ef);
  }
  a { text-decoration-thickness: 0.15em; }
}

@media (forced-colors: active) {
  .card, .btn, .icon-btn, .toc, .note, .facts li, .demo-box { border: 1px solid CanvasText; }
  .card-icon, .brand svg { color: CanvasText; }
}

/* ---------- 11. Print: a shadowless, ink-friendly page ----------------- */
@media print {
  :root { color-scheme: light; }
  .site-header .header-tools, .site-nav, .skip-link, .pager, .lab, .site-footer ul { display: none !important; }
  .card, .hero-media, .facts li { box-shadow: none; }
  a[href^="http"]::after { content: " (" attr(href) ")"; font-size: 0.8em; color: #555; }
}

/* ---------- 12. Small fixes ---------------------------------------------- */
:where(p, li, td, dd, figcaption) code { white-space: nowrap; }   /* never split --shadow-1 at its hyphens */
.lab-code { white-space: pre-wrap; overflow-wrap: anywhere; }
