/*======================================================================================================================
  THE TUTORIAL'S LOOK -- layout and components. Every colour and face is a token; a style (styles/*.css) only sets
  tokens, a texture and a few frame details, so adding one is a file of its own and changes nothing here.

  Every reader chooses a style (kit/styles.js lists them, the bar's menu switches them); kit.css imports each one's file.

  A page is a SHEET: a drawn border, a title block, and sections where the text runs beside a figure that stays put
  (sticky) while its paragraphs scroll past. On a phone the figure sticks to the top and the text runs under it.

  Theme: light by default, dark under prefers-color-scheme, and an explicit data-theme on <html> wins both ways (the
  toggle, page.js). Each style defines both, on [data-style=...] so a card on the styles page can show a style that is
  not the page's own.
======================================================================================================================*/
@import url("fonts/fonts.css");          /* every style's faces, kept in the repository (fonts/fetch.mjs) */
@import url("styles/notebook.css");
@import url("styles/blueprint.css");
@import url("styles/drafting.css");
@import url("styles/riso.css");
@import url("styles/chalkboard.css");
@import url("styles/drafting-sans.css");
@import url("styles/drafting-fine.css");
@import url("styles/minimal.css");

/* The defaults every style overrides: a plain, readable page if a style sheet failed to load. :where() gives them no
   specificity, so a style's tokens win whatever the order the sheets arrive in. */
:where(:root) {
    --paper: #f7f6f2; --paper-deep: #e9e7e0; --paper-raised: #ffffff;
    --ink: #1c1d22; --ink-soft: #4a4c55; --ink-faint: #8a8c94; --grid: rgba(0, 0, 0, .06); --rule: #1c1d22;
    --accent: #c43c2b; --accent-soft: rgba(196, 60, 43, .12);
    --c-cam: #2457c5; --c-light: #d98a00; --c-shadow: #2d8a4e; --c-blocked: #c43c2b; --c-solid: #1c1d22;
    --c-fill: rgba(28, 29, 34, .10); --c-glass: #2f8fa8; --c-normal: #7a3fb5; --c-hit: #1c1d22; --c-step: rgba(36, 87, 197, .35);
    --code-bg: rgba(0, 0, 0, .04); --code-ink: #1c1d22; --code-comment: #6b6e78; --code-kw: #8a2b8f; --code-num: #b0560f;
    --stamp-measured: #2d8a4e; --stamp-code: #2457c5; --stamp-inferred: #b0560f;
    --f-display: Georgia, 'Times New Roman', serif; --f-body: Georgia, 'Times New Roman', serif;
    --f-hand: 'Comic Sans MS', 'Segoe Print', cursive; --f-mono: ui-monospace, 'SFMono-Regular', Menlo, Consolas, monospace;
    --pen-rough: 0; --pen-double: 0; --pen-width: 1.5; --pen-misreg: 0; --pen-fill: wash;
    --tex: none; --radius: 2px;
    /* what the text calls each role's colour: a word marked data-say="cam" is rewritten to the style's own (page.js
       sayColours), so "blue while they bounce" reads "black" in a drafting sheet, which draws them black */
    --say-cam: "blue"; --say-light: "orange"; --say-shadow: "green"; --say-blocked: "red"; --say-accent: "red"; --say-soft: "grey"; --say-normal: "purple"; --say-glass: "teal";
    color-scheme: light;
}
@media (prefers-color-scheme: dark) {
    :where(:root:not([data-theme="light"])) {
        --paper: #1b1c1f; --paper-deep: #111214; --paper-raised: #232428;
        --ink: #e8e6e1; --ink-soft: #b5b3ad; --ink-faint: #7b7a76; --grid: rgba(255, 255, 255, .05); --rule: #e8e6e1;
        --accent: #ff7a62; --accent-soft: rgba(255, 122, 98, .14);
        --c-cam: #8ab4ff; --c-light: #ffc24d; --c-shadow: #6fd49a; --c-blocked: #ff7a62; --c-solid: #e8e6e1;
        --c-fill: rgba(232, 230, 225, .10); --c-glass: #6fd0e8; --c-normal: #c79bff; --c-hit: #e8e6e1; --c-step: rgba(138, 180, 255, .35);
        --code-bg: rgba(255, 255, 255, .05); --code-ink: #e8e6e1; --code-comment: #9a988f; --code-kw: #e59cff; --code-num: #ffb066;
        --stamp-measured: #6fd49a; --stamp-code: #8ab4ff; --stamp-inferred: #ffb066;
        color-scheme: dark;
    }
}
:where(:root[data-theme="dark"]) {
    --paper: #1b1c1f; --paper-deep: #111214; --paper-raised: #232428;
    --ink: #e8e6e1; --ink-soft: #b5b3ad; --ink-faint: #7b7a76; --grid: rgba(255, 255, 255, .05); --rule: #e8e6e1;
    --accent: #ff7a62; --accent-soft: rgba(255, 122, 98, .14);
    --c-cam: #8ab4ff; --c-light: #ffc24d; --c-shadow: #6fd49a; --c-blocked: #ff7a62; --c-solid: #e8e6e1;
    --c-fill: rgba(232, 230, 225, .10); --c-glass: #6fd0e8; --c-normal: #c79bff; --c-hit: #e8e6e1; --c-step: rgba(138, 180, 255, .35);
    --code-bg: rgba(255, 255, 255, .05); --code-ink: #e8e6e1; --code-comment: #9a988f; --code-kw: #e59cff; --code-num: #ffb066;
    --stamp-measured: #6fd49a; --stamp-code: #8ab4ff; --stamp-inferred: #ffb066;
    color-scheme: dark;
}

/* ---------------------------------------------------------------------------------------------------------------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
    margin: 0; background: var(--paper-deep); color: var(--ink);
    font: 400 1.0625rem/1.62 var(--f-body);
    padding-block: 0;
    padding-left: max(0px, env(safe-area-inset-left, 0px)); padding-right: max(0px, env(safe-area-inset-right, 0px));
}
a { color: inherit; text-decoration-color: var(--accent); text-decoration-thickness: 1.5px; text-underline-offset: 3px; }
a:hover { color: var(--accent); }
:focus-visible { outline: 2px dashed var(--accent); outline-offset: 3px; }
::selection { background: var(--accent-soft); }

/* Shown until page.js runs: a module script does not load from file://, so the page says how to open it. */
.kit-offline { margin: 0; padding: .7rem 1rem; background: var(--accent); color: var(--paper); font: 600 .9rem/1.4 var(--f-mono); }
.kit-js .kit-offline { display: none; }

/* THE SHEET ------------------------------------------------------------------------------------------------------ */
.sheet {
    position: relative; min-height: 100vh; margin: 0 auto; max-width: 1480px;
    background-color: var(--paper); background-image: var(--tex); background-attachment: local;
    padding-block: 4.5rem 3rem;
    padding-left: max(16px, calc(env(safe-area-inset-left, 0px) + 16px), 4vw);
    padding-right: max(16px, calc(env(safe-area-inset-right, 0px) + 16px), 4vw);
}
.sheet::before {                                    /* the drawn frame, each style restyles it */
    content: ""; position: absolute; inset: 10px; pointer-events: none;
    border: 1.5px solid var(--rule); opacity: .7; border-radius: var(--radius);
}
@media (max-width: 640px) { .sheet::before { inset: 5px; } }

.sheet-head { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 1.5rem 3rem; align-items: end; margin-bottom: 3.5rem; }
.sheet-head > * { min-width: 0; }
.eyebrow { margin: 0 0 .4rem; font: 600 .78rem/1.3 var(--f-mono); letter-spacing: .14em; text-transform: uppercase; color: var(--ink-soft); }
h1 { margin: 0; font: 700 clamp(2.4rem, 6vw, 4.6rem)/1.02 var(--f-display); letter-spacing: -.01em; text-wrap: balance; }
.lede { margin: 1rem 0 0; max-width: 60ch; font-size: 1.2rem; line-height: 1.5; color: var(--ink-soft); }
h2 { margin: 0 0 .9rem; font: 700 clamp(1.5rem, 3vw, 2.1rem)/1.15 var(--f-display); text-wrap: balance; }
h3 { margin: 1.6rem 0 .5rem; font: 700 1.15rem/1.3 var(--f-display); }
h2 .n { display: inline-block; min-width: 2.2ch; margin-right: .35em; color: var(--accent); font-family: var(--f-mono); font-weight: 600; font-size: .7em; vertical-align: .18em; }

/* the title block: part, chapter, title, revision, sheet -- the drawing's identity, in its corner */
.titleblock { display: grid; grid-template-columns: auto auto; border: 1.5px solid var(--rule); font: 500 .72rem/1.25 var(--f-mono); text-transform: uppercase; letter-spacing: .06em; min-width: 16rem; background: var(--paper); }
.titleblock > div { padding: .35rem .55rem; border-bottom: 1px solid var(--rule); }
.titleblock > div:nth-child(odd) { color: var(--ink-faint); border-right: 1px solid var(--rule); }
.titleblock > div:nth-last-child(-n+2) { border-bottom: 0; }
.titleblock b { font-weight: 700; color: var(--ink); }
@media (max-width: 760px) { .sheet-head { grid-template-columns: minmax(0, 1fr); } .titleblock { min-width: 0; max-width: 24rem; } }

/* SECTIONS: text beside a figure that stays ------------------------------------------------------------------------ */
.scene, .plain { position: relative; margin: 0 0 5rem; scroll-margin-top: 4rem; }
.scene { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.08fr); gap: 2.5rem 3.5rem; align-items: start; }
.scene > .prose { min-width: 0; }
.scene > .fig { position: sticky; top: 4.2rem; min-width: 0; }
.plain > .prose { max-width: 68ch; }
.plain.wide > .prose { max-width: none; }
.prose > p, .prose > ul, .prose > ol { max-width: 64ch; }
.prose p { margin: 0 0 1rem; }
.prose ul, .prose ol { margin: 0 0 1rem; padding-left: 1.3rem; }
.prose li { margin: .2rem 0; }
.prose strong { font-weight: 700; }
/* a small table of facts in the prose (a type's bytes): the page's own rules, mono numbers */
.prose table.data { border-collapse: collapse; margin: 0 0 1rem; max-width: 64ch; font-size: .92em; display: block; overflow-x: auto; }
.prose table.data th, .prose table.data td { border-bottom: 1px solid var(--grid); padding: .25rem .9rem .25rem 0; text-align: left; vertical-align: top; }
.prose table.data th { font: 600 .78rem/1.3 var(--f-mono); color: var(--ink-soft); border-bottom: 1.5px solid var(--rule); }
.prose table.data td + td { font-family: var(--f-mono); }
.prose em { font-style: italic; }

/* a paragraph that moves the figure: marked while it does */
[data-step] { position: relative; cursor: pointer; transition: opacity .25s; }
.kit-js .scene [data-step] { opacity: .62; }
.kit-js .scene [data-step].on, .kit-js .scene [data-step]:hover { opacity: 1; }
[data-step]::before { content: ""; position: absolute; left: -1.1rem; top: .45em; width: .45rem; height: .45rem; border-radius: 50%; border: 1.5px solid var(--accent); }
[data-step].on::before { background: var(--accent); }
@media (prefers-reduced-motion: reduce) { [data-step] { transition: none; } }

@media (max-width: 860px) {
    .scene { grid-template-columns: minmax(0, 1fr); gap: 1.2rem; }
    .scene > .fig { order: -1; top: 2.6rem; z-index: 3; background: var(--paper); padding-bottom: .4rem; box-shadow: 0 10px 14px -12px rgba(0, 0, 0, .35); }
    .scene > .fig .fig-canvas { max-height: 40vh; }
    [data-step]::before { left: -.85rem; }
}

/* FIGURES ---------------------------------------------------------------------------------------------------------- */
.sheet { counter-reset: fig; }
.fig { margin: 0; counter-increment: fig; }
.fig-canvas { position: relative; width: 100%; max-width: 100%; }
.fig-canvas canvas { display: block; width: 100%; height: 100%; touch-action: pan-y; outline-offset: 4px; }
.fig figcaption { margin-top: .5rem; font: 400 .92rem/1.45 var(--f-body); color: var(--ink-soft); max-width: 62ch; }
.fig figcaption::before { content: "Fig. " counter(fig) "  "; font: 700 .8rem/1 var(--f-mono); letter-spacing: .08em; color: var(--ink); text-transform: uppercase; margin-right: .4rem; }
.fig-readout { margin: .35rem 0 0; min-height: 1.4em; font: 500 .85rem/1.4 var(--f-mono); color: var(--ink); font-variant-numeric: tabular-nums; }
.fig-noscript { padding: 1rem; border: 1px dashed var(--ink-faint); color: var(--ink-soft); font-size: .9rem; }

/* controls: a strip under the figure */
.fig-controls { display: flex; flex-wrap: wrap; gap: .5rem 1.1rem; align-items: center; margin-top: .6rem; font: 500 .82rem/1.2 var(--f-mono); }
.fig-controls label { display: inline-flex; align-items: center; gap: .45rem; }
.fig-controls output { min-width: 4.5ch; font-variant-numeric: tabular-nums; color: var(--ink); }
.fig-controls input[type=range] { width: 8.5rem; accent-color: var(--accent); }
.kbtn, .seg button {
    font: 600 .78rem/1 var(--f-mono); letter-spacing: .04em; color: var(--ink); background: transparent;
    border: 1.5px solid var(--rule); border-radius: var(--radius); padding: .42rem .6rem; cursor: pointer;
}
.kbtn:hover, .seg button:hover { background: var(--accent-soft); }
.kbtn[aria-pressed="true"], .seg button[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
.seg { display: inline-flex; }
.seg button + button { border-left: 0; }

/* hidden means hidden: a figure's control shown only for some states (controls.js `when`) is a label the rules above
   make inline-flex, which beat the hidden attribute's own display: none and left a dead slider on the page */
[hidden] { display: none !important; }

/* NOTES, MATHS, STAMPS ---------------------------------------------------------------------------------------------- */
.note { margin: 1.2rem 0; padding: .7rem .9rem; border-left: 3px solid var(--accent); background: var(--accent-soft); font-size: .95rem; max-width: 62ch; }
.note > :last-child { margin-bottom: 0; }
.note .tag, details.maths > summary .tag { font: 700 .72rem/1 var(--f-mono); letter-spacing: .1em; text-transform: uppercase; color: var(--accent); margin-right: .5rem; }

/* A READING BOX: outside sources for the idea beside it (kit/reading.js; page.js writes each title, author and link).
   A slip of card left in the book, not a note in its margin: dashed, raised, the reason last and quietest. */
.refs { margin: 1.4rem 0; padding: .7rem .95rem .75rem; max-width: 62ch; border: 1.5px dashed var(--ink-faint); border-radius: var(--radius); background: var(--paper-raised); font-size: .93rem; }
.refs > .tag { display: block; margin-bottom: .55rem; font: 700 .72rem/1 var(--f-mono); letter-spacing: .1em; text-transform: uppercase; color: var(--ink-soft); }
.refs > ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .7rem; }
.refs li { margin: 0; }
.refs .src-t { font-weight: 700; }
.refs a.src-t::after { content: "\2009↗"; font-weight: 400; color: var(--ink-faint); }
.refs .by { display: block; margin-top: .12rem; font: 500 .74rem/1.35 var(--f-mono); letter-spacing: .02em; color: var(--ink-faint); }
.refs .why { display: block; margin-top: .2rem; color: var(--ink-soft); line-height: 1.45; }
.refs .why cite { font-style: normal; }

details.maths { margin: 1.4rem 0; max-width: 64ch; border: 1.5px solid var(--rule); border-radius: var(--radius); background: var(--paper); }
details.maths > summary { list-style: none; cursor: pointer; padding: .6rem .9rem; font: 600 .95rem/1.3 var(--f-display); display: flex; gap: .5rem; align-items: baseline; }
details.maths > summary::-webkit-details-marker { display: none; }
details.maths > summary::after { content: "+"; margin-left: auto; font: 700 1.1rem/1 var(--f-mono); color: var(--accent); }
details.maths[open] > summary::after { content: "−"; }
details.maths > .body { padding: 0 .9rem .9rem; border-top: 1px dashed var(--ink-faint); }
details.maths > .body > :first-child { margin-top: .8rem; }
.eq { font: 400 1.08rem/1.6 var(--f-body); font-style: normal; }
p.eq { margin: .8rem 0; padding: .5rem .8rem; text-align: center; background: var(--code-bg); border-radius: var(--radius); overflow-x: auto; }
.eq var { font-style: italic; }
.frac { display: inline-flex; flex-direction: column; vertical-align: middle; text-align: center; line-height: 1.15; margin: 0 .15em; }
.frac > span:first-child { border-bottom: 1px solid currentColor; padding: 0 .2em .05em; }
.frac > span:last-child { padding: .05em .2em 0; }
dl.terms { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: .3rem 1rem; margin: .6rem 0; font-size: .95rem; }
dl.terms dt { font-family: var(--f-mono); font-size: .88rem; }
dl.terms dd { margin: 0; color: var(--ink-soft); }

/* a claim stamped with what it rests on (CLAUDE.md): measured, read in code, or inferred */
[data-claim] { position: relative; margin: 1rem 0; padding: .7rem .9rem .7rem 1rem; border: 1.5px solid var(--stamp-c); border-radius: var(--radius); max-width: 64ch; }
[data-claim]::before, [data-claim] > .stamp {
    content: var(--stamp-t); position: absolute; top: -.62rem; left: .7rem; padding: 0 .4rem; background: var(--paper);
    font: 700 .66rem/1.2 var(--f-mono); letter-spacing: .14em; color: var(--stamp-c);
}
/* with the page's script, the stamp is a real link (page.js stamps) and the drawn label steps aside */
.kit-js [data-claim]::before { content: none; }
[data-claim] > .stamp { text-decoration: none; cursor: help; }
[data-claim] > .stamp:hover, [data-claim] > .stamp:focus-visible { text-decoration: underline dotted; color: var(--stamp-c); }
[data-claim="measured"] { --stamp-c: var(--stamp-measured); --stamp-t: "MEASURED"; }
[data-claim="code"] { --stamp-c: var(--stamp-code); --stamp-t: "READ IN CODE"; }
[data-claim="inferred"] { --stamp-c: var(--stamp-inferred); --stamp-t: "INFERRED"; border-style: dashed; }
[data-claim] > :last-child { margin-bottom: 0; }

/* GLOSSARY LINKS AND CARDS ---------------------------------------------------------------------------------------- */
a[data-g] { text-decoration-style: dotted; text-decoration-color: var(--ink-faint); cursor: help; }
a[data-g]:hover, a[data-g]:focus-visible { text-decoration-color: var(--accent); color: inherit; }
a[data-ch] { text-decoration-style: solid; }
a[data-ch].planned { text-decoration-style: dashed; }
.gcard {
    position: absolute; z-index: 50; width: min(21rem, calc(100vw - 32px)); padding: .75rem .9rem .8rem;
    background: var(--paper-raised); color: var(--ink); border: 1.5px solid var(--rule); border-radius: var(--radius);
    box-shadow: 0 10px 28px -12px rgba(0, 0, 0, .45); font: 400 .9rem/1.45 var(--f-body);
}
.gcard b { display: block; font: 700 1rem/1.2 var(--f-display); margin-bottom: .25rem; }
.gcard p { margin: 0 0 .45rem; }
.gcard a { font: 600 .78rem/1.2 var(--f-mono); }

/* CODE: an excerpt of the real file --------------------------------------------------------------------------------- */
.excerpt { margin: 1.4rem 0; max-width: 100%; border: 1.5px solid var(--rule); border-radius: var(--radius); background: var(--paper); }
.excerpt > figcaption { display: flex; flex-wrap: wrap; gap: .3rem .9rem; align-items: baseline; padding: .45rem .75rem; border-bottom: 1px solid var(--rule); font: 600 .78rem/1.3 var(--f-mono); }
.excerpt > figcaption .lang { color: var(--ink-faint); font-weight: 500; }
.excerpt > figcaption .why { flex-basis: 100%; font: 400 .88rem/1.4 var(--f-body); color: var(--ink-soft); }
.excerpt .drift { color: var(--accent); }
.excerpt .scroll { overflow-x: auto; max-height: 34rem; }
.excerpt pre { margin: 0; padding: .6rem 0; font: 400 .8rem/1.55 var(--f-mono); color: var(--code-ink); background: var(--code-bg); tab-size: 4; }
.excerpt code { display: grid; grid-template-columns: max-content minmax(0, 1fr); min-width: max-content; }
.excerpt .ln { padding: 0 .7rem 0 .75rem; text-align: right; color: var(--ink-faint); user-select: none; border-right: 1px solid var(--grid); }
.excerpt .lc { padding: 0 1rem 0 .8rem; white-space: pre; }
.excerpt .c { color: var(--code-comment); font-style: italic; }
.excerpt .k { color: var(--code-kw); font-weight: 600; }
.excerpt .num { color: var(--code-num); }
.excerpt .tpl { background: var(--accent-soft); border-radius: 2px; }
.excerpt.failed pre { padding: .8rem; color: var(--accent); white-space: pre-wrap; }
pre[data-src] { font: 400 .8rem/1.5 var(--f-mono); padding: .8rem; border: 1px dashed var(--ink-faint); white-space: pre-wrap; }
cite[data-src] { font: 500 .85em/1 var(--f-mono); font-style: normal; }

/* WORDS FROM THIS CHAPTER, PREV / NEXT ---------------------------------------------------------------------------- */
.words { display: flex; flex-wrap: wrap; gap: .45rem; margin: .8rem 0 0; padding: 0; list-style: none; }
.words a { display: inline-block; padding: .25rem .6rem; border: 1.5px solid var(--rule); border-radius: 999px; font: 500 .82rem/1.2 var(--f-mono); text-decoration: none; }
.words a:hover { background: var(--accent-soft); }
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; margin-top: 4rem; }
.tabs a { display: block; padding: 1rem 1.2rem; border: 1.5px solid var(--rule); border-radius: var(--radius); text-decoration: none; min-width: 0; }
.tabs a:hover { background: var(--accent-soft); color: inherit; }
.tabs a small { display: block; font: 600 .72rem/1.3 var(--f-mono); letter-spacing: .1em; text-transform: uppercase; color: var(--ink-faint); }
.tabs a span { font: 700 1.15rem/1.25 var(--f-display); }
.tabs .next { text-align: right; grid-column: 2; }

/* THE BAR: contents, the style by name, the theme -------------------------------------------------------------------- */
.kit-bar {
    position: fixed; z-index: 60; top: calc(env(safe-area-inset-top, 0px) + 14px); right: calc(env(safe-area-inset-right, 0px) + 18px);
    display: flex; flex-wrap: wrap; justify-content: flex-end; gap: .4rem; align-items: center; max-width: calc(100vw - 36px);
    font: 600 .75rem/1 var(--f-mono);
}
.kit-bar > * { background: var(--paper-raised); }
.kit-bar a.kbtn { text-decoration: none; display: inline-block; }
.kit-bar .stylepick { display: inline-flex; align-items: center; gap: .1rem; border: 1.5px solid var(--rule); border-radius: var(--radius); padding: 0 .1rem 0 .55rem; }
.kit-bar .stylesall { text-decoration: none; color: var(--ink-faint); }
.kit-bar .stylesall:hover { color: var(--accent); }
.kit-bar select { font: 600 .75rem/1 var(--f-mono); color: var(--ink); background: transparent; border: 0; padding: .42rem .3rem; cursor: pointer; max-width: 11rem; }
.kit-bar select option { background: var(--paper-raised); color: var(--ink); }
@media (max-width: 640px) { .kit-bar { top: calc(env(safe-area-inset-top, 0px) + 8px); right: calc(env(safe-area-inset-right, 0px) + 10px); } .kit-bar .stylesall { display: none; } }

/* small screens: sizes down a step */
@media (max-width: 640px) {
    body { font-size: 1rem; }
    .sheet { padding-top: 4.2rem; }
    .scene, .plain { margin-bottom: 3.5rem; }
    .lede { font-size: 1.08rem; }
}
