/* ==========================================================================
   Data Privacy and Security (Fall 2025) — shared stylesheet
   Sapienza University of Rome, Prof. Daniele Venturi

   Palette: Flexoki (stephango.com/flexoki). Use the tokens below, never raw hex.
   Designed for offline use over file://. See DPS_INTERACTIVE_COURSE_PRD.md.
   ========================================================================== */

/* ---------------------------------------------------------------- tokens -- */

:root {
  /* Flexoki base — light mode */
  --paper:         #FFFCF0;
  --bg:            #FFFCF0;
  --bg-raised:     #FFFFFF;
  --bg-sunken:     #F2F0E5;
  --bg-inset:      #E6E4D9;
  /* The colour a scroll-fade band paints itself. Every surface that can hold
     a scroller re-declares it, so the band always matches its own ground. */
  --fade:          var(--bg);
  --fade-shadow:   rgba(0,0,0,.10);
  --ink:           #100F0F;
  --ink-soft:      #6F6E69;
  --ink-faint:     #878580;
  --rule:          #DAD8CC;
  --rule-strong:   #CECDC3;

  /* Flexoki hues — 600 in light mode */
  --red:           #AF3029;
  --orange:        #BC5215;
  --yellow:        #AD8301;
  --yellow-text:   #7A5C00;
  --green:         #66800B;
  --cyan:          #24837B;
  --blue:          #205EA6;
  --purple:        #5E409D;
  --magenta:       #A02F6F;

  /* Semantic roles — fixed across all pages. Alice is always blue. */
  --accent:        var(--orange);
  --accent-soft:   #9C4310;
  --alice:         var(--blue);
  --bob:           var(--cyan);
  --eve:           var(--red);
  --key:           var(--yellow);
  --noise:         var(--purple);
  --oracle:        var(--magenta);
  --ok:            var(--green);
  --warn:          var(--yellow);
  --bad:           var(--magenta);

  /* The stage follows the page theme. It reads as a separate instrument
     panel through a tonal step and its own hue set, never by inverting.
     Every colour below clears WCAG AA on both panel and caption bar. */
  --stage-bg:      #F7F4E9;
  --stage-bg-deep: #EDE9DB;
  --stage-ink:     #100F0F;
  --stage-ink-soft:#5F5E59;
  --stage-rule:    #D6D3C4;
  --stage-red:     #AF3029;
  --stage-orange:  #A34310;
  --stage-yellow:  #7A5C00;
  --stage-green:   #54690D;
  --stage-cyan:    #1C6C66;
  --stage-blue:    #205EA6;
  --stage-purple:  #5E409D;
  --stage-magenta: #A02F6F;

  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;

  --radius: 10px;
  --shadow: 0 1px 2px rgba(16,15,15,.05), 0 6px 20px -12px rgba(16,15,15,.18);
  --measure: 62ch;
  /* Type scale. 23 one-off sizes previously sat between .64 and .98rem. */
  --t-3xs: .6875rem; --t-2xs: .75rem;  --t-xs: .8125rem; --t-s: .875rem;
  --t-m: 1rem;       --t-l: 1.25rem;   --t-xl: 1.5rem;
  /* Spacing scale, 4px based. */
  --s1: .25rem; --s2: .5rem; --s3: .75rem; --s4: 1rem; --s5: 1.5rem;
  --s6: 2rem;   --s7: 2.5rem; --s8: 4rem;
  --masthead-h: 3.1rem;
  --sidebar-w: 17rem;
}

/* Flexoki dark — hues shift to the 400 step for contrast on dark ground. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper:       #100F0F;
    --bg:          #100F0F;
    --bg-raised:   #1C1B1A;
    --bg-sunken:   #1C1B1A;
    --bg-inset:    #282726;
    --fade-shadow: rgba(0,0,0,.5);
    --ink:         #CECDC3;
    --ink-soft:    #878580;
    --ink-faint:   #575653;
    --rule:        #282726;
    --rule-strong: #403E3C;

    --red:         #D14D41;
    --orange:      #DA702C;
    --yellow:      #D0A215;
    --green:       #879A39;
    --cyan:        #3AA99F;
    --blue:        #4385BE;
    --purple:      #8B7EC8;
    --magenta:     #CE5D97;

    --accent-soft: #EC8B49;
    --stage-bg: #1C1B1A;
    --stage-bg-deep: #100F0F;
    --stage-ink: #CECDC3;
    --stage-ink-soft: #9C9A93;
    --stage-rule: #3A3937;
    --stage-red: #D14D41;
    --stage-orange: #DA702C;
    --stage-yellow: #D0A215;
    --stage-green: #A0B535;
    --stage-cyan: #3AA99F;
    --stage-blue: #66A0DA;
    --stage-purple: #A192DC;
    --stage-magenta: #CE5D97;

    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -14px rgba(0,0,0,.8);
  }
}

:root[data-theme="dark"] {
  --paper:       #100F0F;
  --bg:          #100F0F;
  --bg-raised:   #1C1B1A;
  --bg-sunken:   #1C1B1A;
  --bg-inset:    #282726;
  --fade-shadow: rgba(0,0,0,.5);
  --ink:         #CECDC3;
  --ink-soft:    #878580;
  --ink-faint:   #575653;
  --rule:        #282726;
  --rule-strong: #403E3C;

  --red:         #D14D41;
  --orange:      #DA702C;
  --yellow:      #D0A215;
  --green:       #879A39;
  --cyan:        #3AA99F;
  --blue:        #4385BE;
  --purple:      #8B7EC8;
  --magenta:     #CE5D97;

  --accent-soft: #EC8B49;
  --stage-bg: #1C1B1A;
  --stage-bg-deep: #100F0F;
  --stage-ink: #CECDC3;
  --stage-ink-soft: #9C9A93;
  --stage-rule: #3A3937;
  --stage-red: #D14D41;
  --stage-orange: #DA702C;
  --stage-yellow: #D0A215;
  --stage-green: #A0B535;
  --stage-cyan: #3AA99F;
  --stage-blue: #66A0DA;
  --stage-purple: #A192DC;
  --stage-magenta: #CE5D97;

  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -14px rgba(0,0,0,.8);
}

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

html { font-size: 106.25%; scroll-behavior: smooth; scroll-padding-top: var(--masthead-h, 5rem); }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 1rem/1.65 var(--serif);
  -webkit-text-size-adjust: 100%;
  text-rendering: optimizeLegibility;
}

/* ------------------------------------------------------------------ chrome */

.masthead {
  position: sticky; top: 0; z-index: 60;
  display: flex; align-items: center; gap: 1rem;
  padding: .6rem clamp(1rem, 3vw, 2rem);
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: saturate(140%) blur(12px);
  border-bottom: 1px solid var(--rule);
  font-family: var(--sans); font-size: .82rem;
}
.masthead .home { font-weight: 600; color: var(--accent); text-decoration: none; }
.masthead .home:hover { text-decoration: underline; }
.masthead .crumb { color: var(--ink-faint); }
.masthead .spacer { flex: 1; }

.theme-toggle, .sidebar-toggle {
  font: 600 .78rem/1 var(--sans); cursor: pointer;
  background: var(--bg-raised); color: var(--ink-soft);
  border: 1px solid var(--rule-strong); border-radius: 999px;
  padding: .35rem .75rem;
}
.theme-toggle:hover, .sidebar-toggle:hover { border-color: var(--accent); color: var(--ink); }
.sidebar-toggle { display: none; }

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

.layout { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); align-items: start; }

.sidebar {
  position: sticky; top: var(--masthead-h); align-self: start;
  max-height: calc(100vh - var(--masthead-h)); overflow-y: auto;
  border-right: 1px solid var(--rule);
  padding: 1.6rem 1rem 3rem 1.4rem;
  font-family: var(--sans); font-size: .84rem;
}
.sidebar .grp { margin-bottom: 1.4rem; }
.sidebar .grp > .hd {
  font-size: .66rem; font-weight: 700; letter-spacing: .12em; text-transform: uppercase;
  color: var(--ink-faint); margin-bottom: .5rem;
}
.sidebar a { display: block; color: var(--ink-soft); text-decoration: none; padding: .2rem 0; line-height: 1.4; }
.sidebar a:hover { color: var(--accent); }
.sidebar a.cur { color: var(--accent); font-weight: 650; }
.sidebar ol { list-style: none; margin: .3rem 0 0; padding-left: .85rem; border-left: 1px solid var(--rule); }
.sidebar ol a { font-size: .8rem; }

.wrap { max-width: 1120px; margin: 0 auto; padding: 0 clamp(1rem, 4vw, 3rem) 6rem; min-width: 0; }
.prose { max-width: var(--measure); }

@media (max-width: 900px) {
  html { scroll-behavior: auto; }

  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    visibility: hidden;
    position: fixed; inset: var(--masthead-h) auto 0 0; z-index: 55; width: min(20rem, 86vw);
    background: var(--bg-raised); box-shadow: var(--shadow);
    transform: translateX(-102%); transition: transform .22s ease;
  }
  .sidebar[data-open="true"] { transform: none; visibility: visible; }
  .sidebar-toggle { display: inline-block; }
}

/* ------------------------------------------------------------- page header */

.page-head { padding: clamp(2rem, 6vw, 4.5rem) 0 1.5rem; }
.page-head .eyebrow {
  font-family: var(--sans); font-size: .72rem; font-weight: 650;
  letter-spacing: .14em; text-transform: uppercase; color: var(--accent); margin: 0 0 .7rem;
}
.page-head h1 {
  font-size: clamp(2rem, 5.5vw, 3.1rem); line-height: 1.08;
  margin: 0 0 .8rem; letter-spacing: -.02em; font-weight: 600;
}
.page-head .standfirst {
  font-size: clamp(1.05rem, 2.2vw, 1.25rem); color: var(--ink-soft);
  max-width: var(--measure); margin: 0;
}

.meta-strip { display: flex; flex-wrap: wrap; gap: .5rem; margin: 1.6rem 0 0; font-family: var(--sans); font-size: .78rem; }
.meta-strip .chip {
  background: var(--bg-sunken); border: 1px solid var(--rule);
  border-radius: 999px; padding: .3rem .8rem; color: var(--ink-soft);
}
.meta-strip .chip b { color: var(--ink); font-weight: 600; }

/* ---------------------------------------------------------------- headings */

h2 {
  font-size: clamp(1.5rem, 3.2vw, 2rem); line-height: 1.2; font-weight: 600;
  letter-spacing: -.015em; margin: 3.4rem 0 .5rem; padding-top: 1.6rem;
  border-top: 2px solid var(--rule);
}
h2 .num {
  display: block; font-family: var(--sans); font-size: .7rem; font-weight: 650;
  letter-spacing: .14em; text-transform: uppercase; color: var(--accent); margin-bottom: .45rem;
}
h3 { font-size: var(--t-l); line-height: 1.3; font-weight: 600; margin: var(--s6) 0 var(--s1); letter-spacing: -.01em; }
h4 { font-family: var(--sans); font-size: var(--t-m); line-height: 1.4; font-weight: 600; margin: var(--s5) 0 var(--s1); color: var(--ink); }

p { margin: 0 0 1.65rem; max-width: var(--measure); }
ul, ol { max-width: var(--measure); padding-left: 1.3rem; margin: 0 0 1.05rem; }
li { margin-bottom: .4rem; }
li > ul, li > ol { margin-top: .4rem; }

a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: .15em; }
a:hover { color: var(--accent-soft); }

strong, b { font-weight: 650; }
code, kbd, samp, .mono { font-family: var(--mono); font-size: .88em; }
:not(pre) > code { background: var(--bg-sunken); border: 1px solid var(--rule); border-radius: 4px; padding: .08em .35em; }
pre {
  --fade: var(--bg-sunken);
  background: var(--bg-sunken); border: 1px solid var(--rule); border-radius: var(--radius);
  padding: 1rem 1.1rem; overflow-x: auto; font-family: var(--mono); font-size: .84rem; line-height: 1.55;
}
hr { border: 0; border-top: 1px solid var(--rule); margin: 2.5rem 0; }

/* ------------------------------------------------------------------- table */

.table-scroll { overflow-x: auto; margin: 1.2rem 0 1.6rem; }
table { border-collapse: collapse; width: 100%; font-size: .92rem; font-family: var(--sans); }
th, td { text-align: left; padding: .55rem .7rem; border-bottom: 1px solid var(--rule); vertical-align: top; }
th { font-weight: 650; font-size: .78rem; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-soft); }
tbody tr:hover { background: var(--bg-sunken); }
td.center, th.center { text-align: center; }

/* ------------------------------------------------------- notes and formals */

.note {
  --fade: var(--bg-raised);
  border-left: 3px solid var(--accent); background: var(--bg-raised);
  border-radius: 0 var(--radius) var(--radius) 0;
  padding: .95rem 1.1rem; margin: 1.4rem 0; max-width: var(--measure); box-shadow: var(--shadow);
}
.note > :last-child { margin-bottom: 0; }
.note .label {
  display: block; font-family: var(--sans); font-size: .7rem; font-weight: 700;
  letter-spacing: .12em; text-transform: uppercase; color: var(--accent); margin-bottom: .4rem;
}
.note.danger { border-left-color: var(--bad); }   .note.danger .label { color: var(--bad); }
.note.good   { border-left-color: var(--ok); }    .note.good .label   { color: var(--ok); }
.note.warn   { border-left-color: var(--warn); }  .note.warn .label   { color: var(--warn); }

.formal {
  border: 1px solid var(--rule-strong); border-radius: var(--radius);
  --fade: var(--bg-raised); background: var(--bg-raised); padding: 1rem 1.15rem;
  margin: 1.4rem 0; max-width: var(--measure); box-shadow: var(--shadow);
}
.formal > :last-child { margin-bottom: 0; }
.formal .label {
  display: block; font-family: var(--sans); font-size: .74rem; font-weight: 700;
  letter-spacing: .08em; text-transform: uppercase; color: var(--ink-soft); margin-bottom: .45rem;
}
.formal.def { border-left: 4px solid var(--accent); }
.formal.thm { border-left: 4px solid var(--alice); }
.formal.game { border-left: 4px solid var(--eve); }

/* ----------------------------------------------------------------- figures */

figure { margin: 1.8rem 0; }
figure svg { max-width: 100%; height: auto; display: block; margin: 0 auto; }
figcaption {
  font-family: var(--sans); font-size: .82rem; color: var(--ink-soft);
  margin: .7rem auto 0; text-align: center; max-width: var(--measure);
}

/* ==========================================================================
   THE STAGE — full-bleed dark panel for beat-stepped animations
   Dark in both themes by design. See PRD §10.
   ========================================================================== */

.stage {
  margin: 2.4rem 0; border-radius: 14px; overflow: hidden;
  --fade: var(--stage-bg); background: var(--stage-bg); color: var(--stage-ink);
  border: 1px solid var(--stage-rule);
  box-shadow: 0 2px 4px rgba(0,0,0,.2), 0 18px 40px -24px rgba(0,0,0,.55);
  font-family: var(--sans);
}

.stage > .stage-head {
  display: flex; align-items: baseline; gap: .7rem; flex-wrap: wrap;
  padding: .8rem 1.1rem; border-bottom: 1px solid var(--stage-rule);
  background: var(--stage-bg-deep);
}
.stage > .stage-head .kind {
  font-size: .64rem; font-weight: 700; letter-spacing: .14em; text-transform: uppercase;
  color: var(--stage-orange); border: 1px solid currentColor; border-radius: 999px; padding: .12rem .5rem;
}
.stage > .stage-head h3 { margin: 0; font-size: 1.24rem; font-weight: 600; color: var(--stage-ink); font-family: var(--sans); }

/* The viewport: SVG geometry, KaTeX labels pinned over it, canvas beneath. */
.stage .viewport {
  position: relative; width: 100%;
  /* aspect-ratio alone sizes this. A min-height here combines with the ratio
     to imply a min-WIDTH (380 x 16/9 = 676px) and forces the whole page to
     scroll sideways on a phone. */
  background: var(--stage-bg);
  /* Clip, so objects can wait off-frame and slide in. */
  overflow: hidden;
  /* cqh units in the label layer resolve against this box. Without a query
     container they silently fall back to the viewport, so labels get sized
     by the browser window instead of by the stage. */
  container-type: size;
}
.stage .viewport > canvas,
.stage .viewport > svg,
.stage .viewport > .labels {
  position: absolute; inset: 0; width: 100%; height: 100%;
}
.stage .viewport > canvas { z-index: 1; }
.stage .viewport > svg    { z-index: 2; overflow: visible; }
.stage .viewport > .labels { z-index: 3; pointer-events: none; }

/* Labels are absolutely positioned KaTeX, never baked into canvas pixels. */
.stage .labels .lbl {
  position: absolute; transform: translate(-50%, -50%);
  /* 26 logical units in a 900-unit viewBox, matching the SVG text default in
     dps-stage.js, so unsized labels track the artwork like everything else. */
  font-size: clamp(13px, 3.8cqh, 34px); line-height: 1; white-space: nowrap;
  color: var(--stage-ink); transition: opacity .3s, transform .3s;
}
.stage .labels .lbl.fade { opacity: 0; }

/* stage semantic colors */
.stage { --s-alice: var(--stage-blue); --s-bob: var(--stage-cyan); --s-eve: var(--stage-red);
         --s-key: var(--stage-yellow); --s-ok: var(--stage-green); --s-noise: var(--stage-purple);
         --s-oracle: var(--stage-magenta); --s-accent: var(--stage-orange);
         --s-ink: var(--stage-ink); --s-soft: var(--stage-ink-soft); --s-rule: var(--stage-rule); }

/* ---- caption ---- */
.stage .caption .katex,
.stage .stage-foot .katex,
figcaption .katex,
.lab > header .hint .katex,
td .katex, th .katex { font-size: 1.09em; }
/* KaTeX drops script levels to ~70% and ~50%. On a container already below
   body size that lands subscripts near 10px, so hold a floor. */
.stage .caption .katex .sizing,
.stage .caption .katex .mtight { font-size: inherit; }

.stage .caption {
  padding: .95rem 1.15rem; border-top: 1px solid var(--stage-rule);
  background: var(--stage-bg-deep); font-size: 1.2rem; color: var(--stage-ink);
  /* Reserve 3 lines (line-height * font-size * 3 + vertical padding) so beat
     changes never reflow the stage; dps-stage.js refines this per stage to
     the tallest of its captions. Text is top-aligned so it stays put too. */
  min-height: calc(3.2 * 1.55 * 1.2rem + 2 * .95rem);
  display: flex; align-items: flex-start; gap: .6rem;
}
.stage .caption .stepno {
  flex: none; font-size: .68rem; font-weight: 700; letter-spacing: .1em; text-transform: uppercase;
  color: var(--stage-ink-soft);
}
.stage .caption .text { flex: 1; display: flex; flex-direction: column; gap: .3rem; }
.stage .caption .lead {
  font-family: var(--sans); font-weight: 650; font-size: 1.02em;
  letter-spacing: -.005em; color: var(--stage-ink); line-height: 1.25;
}
.stage .caption .body { color: var(--stage-ink-soft); line-height: 1.55; max-width: 74ch; }

/* ---- controls: stepper + scrubber ---- */
.stage .stage-controls {
  display: flex; align-items: center; gap: .8rem; flex-wrap: wrap;
  padding: .75rem 1.1rem; background: var(--stage-bg-deep); border-top: 1px solid var(--stage-rule);
}
.stage .stage-controls button {
  font: 600 .8rem/1 var(--sans); cursor: pointer;
  background: transparent; color: var(--stage-ink);
  border: 1px solid var(--stage-rule); border-radius: 7px; padding: .45rem .8rem;
}
.stage .stage-controls button:hover:not(:disabled) { border-color: var(--stage-orange); color: var(--stage-orange); }
.stage .stage-controls button:disabled { opacity: .3; cursor: not-allowed; }
.stage .stage-controls button:focus-visible { outline: 2px solid var(--stage-orange); outline-offset: 2px; }

/* beat dots double as direct jumps */
.stage .beats { display: flex; align-items: center; gap: 0; flex: 1 1 8rem; min-width: 6rem; }
.stage .beats .dot {
  --d: .62rem;
  width: var(--d); height: var(--d); border-radius: 50%; flex: none;
  /* 24px hit area around a 10px dot, per WCAG 2.5.8. */
  padding: .45rem; background-clip: content-box; box-sizing: content-box;
  background: var(--stage-ink-soft); border: 0; cursor: pointer;
  transition: background .2s, transform .2s;
}
.stage .beats .dot:hover { transform: scale(1.35); background: var(--stage-ink-soft); }
.stage .beats .dot[aria-current="true"] { background: var(--stage-orange); transform: scale(1.35);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--stage-orange) 28%, transparent); }
.stage .beats .dot.done { background: var(--stage-ink-soft); }
.stage .beats .link { flex: 1 1 auto; height: 1px; background: var(--stage-rule); min-width: .5rem; }

/* free scrubber over the whole animation */
.stage .scrub { flex: 1 1 12rem; min-width: 8rem; display: flex; align-items: center; gap: .55rem; }
.stage .scrub input[type="range"] { flex: 1; accent-color: var(--stage-orange); background: transparent; }
.stage .scrub .pos {
  font-family: var(--mono); font-size: .72rem; color: var(--stage-ink-soft);
  flex: none; min-width: 3.2rem; text-align: right;
}

.stage .stage-foot {
  padding: .7rem 1.15rem; font-size: .92rem; color: var(--stage-ink-soft);
  border-top: 1px solid var(--stage-rule); background: var(--stage-bg-deep);
}

/* KaTeX inside the stage must not inherit the paper ink color. */
.stage .katex { color: inherit; }

/* ------------------------------------------------------------ lab / demo -- */

.lab {
  border: 1px solid var(--rule-strong); border-radius: var(--radius);
  --fade: var(--bg-raised);
  background: var(--bg-raised); box-shadow: var(--shadow); margin: 2rem 0; overflow: hidden;
}
.lab > header {
  display: flex; align-items: baseline; gap: .6rem; flex-wrap: wrap;
  padding: .8rem 1.1rem; --fade: var(--bg-sunken); background: var(--bg-sunken);
  border-bottom: 1px solid var(--rule); font-family: var(--sans);
}
.lab > header .kind {
  font-size: .68rem; font-weight: 700; letter-spacing: .12em; text-transform: uppercase;
  color: var(--accent); border: 1px solid currentColor; border-radius: 999px; padding: .12rem .5rem;
}
.lab > header h3 { margin: 0; font-size: 1rem; font-family: var(--sans); font-weight: 650; }
.lab > header .hint { font-size: .8rem; color: var(--ink-faint); flex-basis: 100%; margin: 0; }
.lab .body { padding: 1.1rem; }
.lab .body > :last-child { margin-bottom: 0; }
.lab .body p { max-width: var(--measure); }
.lab .body > .controls, .lab .body > .readout { max-width: none; }

.controls { display: flex; flex-wrap: wrap; gap: .7rem; align-items: center; margin: 0 0 1rem; font-family: var(--sans); font-size: .86rem; }
.controls label { display: inline-flex; align-items: center; gap: .4rem; color: var(--ink-soft); }
.controls .grow { flex: 1 1 14rem; }

button, .btn {
  font: 600 .84rem/1 var(--sans); cursor: pointer;
  background: var(--accent); color: var(--paper); border: 1px solid transparent;
  border-radius: 7px; padding: .5rem .9rem;
}
button:hover, .btn:hover { background: var(--accent-soft); }
button:active { transform: translateY(1px); }
button.ghost { background: transparent; color: var(--ink-soft); border-color: var(--rule-strong); }
button.ghost:hover { background: var(--bg-sunken); color: var(--ink); }
button.ghost[aria-pressed="true"] { background: var(--accent); color: var(--paper); border-color: transparent; }
button:disabled { opacity: .45; cursor: not-allowed; }
button:focus-visible, input:focus-visible, select:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

input[type="text"], input[type="number"], select {
  font: inherit; font-family: var(--sans); font-size: .86rem;
  background: var(--bg); color: var(--ink);
  border: 1px solid var(--rule-strong); border-radius: 7px; padding: .42rem .6rem; min-width: 0;
}
input[type="text"].mono { font-family: var(--mono); }
input[type="range"] { accent-color: var(--accent); vertical-align: middle; }
input[type="checkbox"] { accent-color: var(--accent); }

.segmented { display: inline-flex; border: 1px solid var(--rule-strong); border-radius: 7px; overflow: hidden; }
.segmented button {
  border-radius: 0; border: 0; border-right: 1px solid var(--rule-strong);
  background: transparent; color: var(--ink-soft); padding: .45rem .75rem;
}
.segmented button:last-child { border-right: 0; }
.segmented button[aria-pressed="true"] { background: var(--accent); color: var(--paper); }

.readout {
  font-family: var(--mono); font-size: .8rem; line-height: 1.7;
  --fade: var(--bg-sunken);
  background: var(--bg-sunken); border: 1px solid var(--rule);
  border-radius: var(--radius); padding: .8rem .9rem; overflow-x: auto;
  white-space: pre; margin: .9rem 0 0;
}
.readout .k { color: var(--key); } .readout .m { color: var(--alice); }
.readout .c { color: var(--eve); } .readout .dim { color: var(--ink-faint); }
.readout b { color: var(--ink); }

.metric-row { display: flex; flex-wrap: wrap; gap: 1.4rem; margin: 1rem 0 0; font-family: var(--sans); }
.metric { min-width: 7rem; }
.metric .v { display: block; font-size: 1.5rem; font-weight: 600; font-family: var(--serif); line-height: 1.1; }
.metric .l { font-size: .72rem; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-faint); }

canvas { max-width: 100%; display: block; border-radius: 8px; }

/* bit strips */
.bits { display: flex; flex-wrap: wrap; gap: 2px; font-family: var(--mono); font-size: .74rem; margin: .3rem 0 .9rem; }
.bits .bit {
  width: 1.45rem; height: 1.45rem; display: grid; place-items: center; border-radius: 3px;
  background: var(--bg-sunken); border: 1px solid var(--rule); transition: background .18s, color .18s;
}
.bits .bit.one { background: var(--accent); color: var(--paper); border-color: transparent; }
.bits.key .bit.one { background: var(--key); }
.bits.msg .bit.one { background: var(--alice); }
.bits.ct  .bit.one { background: var(--eve); }
.bit-legend {
  font-family: var(--sans); font-size: .74rem; color: var(--ink-faint);
  margin: 0 0 .2rem; letter-spacing: .05em; text-transform: uppercase; font-weight: 650;
}

/* AES-style state grid */
.state-grid { display: grid; grid-template-columns: repeat(4, 2.9rem); gap: 4px; font-family: var(--mono); font-size: .8rem; }
.state-grid .cell {
  aspect-ratio: 1; display: grid; place-items: center; border-radius: 5px;
  background: var(--bg-sunken); border: 1px solid var(--rule);
  transition: background .25s, transform .25s, color .25s;
}
.state-grid .cell.hot { background: var(--accent); color: var(--paper); border-color: transparent; }
.state-grid .cell.moved { background: var(--alice); color: var(--paper); border-color: transparent; }

/* --------------------------------------------------------------- exercises */

.exercises { margin: 1.6rem 0; max-width: var(--measure); }
details.ex { --fade: var(--bg-raised); border: 1px solid var(--rule); border-radius: var(--radius); background: var(--bg-raised); margin: 0 0 .7rem; overflow: hidden; }
details.ex[open] { border-color: var(--rule-strong); box-shadow: var(--shadow); }
details.ex > summary {
  cursor: pointer; padding: .8rem 1rem .8rem 2.6rem; position: relative;
  font-family: var(--sans); font-size: .92rem; font-weight: 550; list-style: none;
}
details.ex > summary::-webkit-details-marker { display: none; }
details.ex > summary::before { content: "▸"; position: absolute; left: 1rem; color: var(--accent); transition: transform .18s; display: inline-block; }
details.ex[open] > summary::before { transform: rotate(90deg); }
details.ex > summary .tag {
  font-size: .66rem; font-weight: 700; letter-spacing: .1em; text-transform: uppercase;
  color: var(--ink-faint); border: 1px solid var(--rule-strong); border-radius: 999px;
  padding: .1rem .45rem; margin-left: .5rem; white-space: nowrap;
}
details.ex .answer { padding: .9rem 1rem 1rem 2.6rem; border-top: 1px dashed var(--rule); font-size: .96rem; }
details.ex .answer > :last-child { margin-bottom: 0; }
details.ex .answer p, details.ex .answer ul, details.ex .answer ol { max-width: none; }

/* ------------------------------------------------------------ card indexes */

.card-grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fill, minmax(15.5rem, 1fr)); margin: 1.6rem 0; }
.card {
  display: flex; flex-direction: column; gap: .45rem;
  border: 1px solid var(--rule); border-radius: var(--radius);
  --fade: var(--bg-raised); background: var(--bg-raised); padding: 1rem 1.1rem;
  text-decoration: none; color: inherit; box-shadow: var(--shadow);
  transition: transform .16s, border-color .16s;
}
.card:hover { transform: translateY(-2px); border-color: var(--accent); }
.card .ch { font-family: var(--sans); font-size: .68rem; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; color: var(--accent); }
.card .t { font-size: 1.08rem; font-weight: 600; line-height: 1.25; }
.card .d { font-size: .88rem; color: var(--ink-soft); font-family: var(--sans); line-height: 1.5; }
.card .lx { font-family: var(--sans); font-size: .72rem; color: var(--ink-faint); margin-top: auto; padding-top: .5rem; }

/* ------------------------------------------------------------------- toc */

.toc {
  --fade: var(--bg-raised);
  border: 1px solid var(--rule); border-radius: var(--radius); background: var(--bg-raised);
  padding: 1rem 1.2rem; margin: 2rem 0; font-family: var(--sans); font-size: .89rem; max-width: var(--measure);
}
.toc .label { font-size: .7rem; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; color: var(--ink-faint); margin-bottom: .6rem; }
.toc ol { margin: 0; padding-left: 1.2rem; }
.toc li { margin-bottom: .3rem; }
.toc a { text-decoration: none; }
.toc a:hover { text-decoration: underline; }

/* ------------------------------------------------------------------ search */

.search { position: relative; font-family: var(--sans); }
.search input {
  width: 100%; font-size: .84rem; padding: .45rem .7rem;
  background: var(--bg-sunken); border: 1px solid var(--rule-strong); border-radius: 7px; color: var(--ink);
}
.search-results {
  position: absolute; z-index: 70; top: calc(100% + .35rem); left: 0; right: 0;
  max-height: 60vh; overflow-y: auto;
  background: var(--bg-raised); border: 1px solid var(--rule-strong);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: .35rem;
}
.search-results:empty { display: none; }
.search-results a { display: block; padding: .45rem .6rem; border-radius: 6px; text-decoration: none; color: var(--ink); font-size: .84rem; }
.search-results a:hover, .search-results a.sel { background: var(--bg-sunken); }
.search-results a .where { display: block; font-size: .7rem; color: var(--ink-faint); }
.search-results mark { background: color-mix(in srgb, var(--key) 35%, transparent); color: inherit; border-radius: 2px; }

/* ------------------------------------------------------------------ footer */

.page-foot {
  border-top: 1px solid var(--rule); margin-top: 4rem; padding-top: 1.5rem;
  font-family: var(--sans); font-size: .8rem; color: var(--ink-faint);
  display: flex; flex-wrap: wrap; gap: 1rem; justify-content: space-between;
}
.page-foot a { color: var(--ink-soft); }

.pager { display: flex; gap: 1rem; flex-wrap: wrap; margin: 3rem 0 0; font-family: var(--sans); font-size: .9rem; }
.pager a { flex: 1 1 12rem; border: 1px solid var(--rule); border-radius: var(--radius); padding: .8rem 1rem; text-decoration: none; background: var(--bg-raised); }
.pager a:hover { border-color: var(--accent); }
.pager a .dir { display: block; font-size: .68rem; letter-spacing: .12em; text-transform: uppercase; color: var(--ink-faint); margin-bottom: .2rem; }
.pager a .ttl { color: var(--ink); font-weight: 550; }
.pager a.next { text-align: right; }

/* -------------------------------------------------------------- utilities */

.small { font-size: .86rem; color: var(--ink-soft); }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.cols-2 { display: grid; gap: 1.4rem; grid-template-columns: repeat(auto-fit, minmax(17rem, 1fr)); margin: 1.2rem 0; }
.cols-2 > * { min-width: 0; }
.cols-2 p, .cols-2 ul, .cols-2 ol { max-width: none; }

.tick { color: var(--ok); font-weight: 700; }
.cross { color: var(--bad); font-weight: 700; }
.tilde { color: var(--warn); font-weight: 700; }

/* KaTeX display blocks should scroll rather than overflow the page. */
.katex-display { overflow-x: auto; overflow-y: hidden; padding: .2rem 0; }
/* Inline KaTeX is white-space: nowrap and unbreakable. Long runs pushed the
   page wider than the viewport until this. */
/* Inline maths is one unbreakable box. overflow-x has no effect on an inline
   element, so it must be inline-block first. Without this a single long
   formula sets a min-width on the whole document and the page scrolls
   sideways on a phone. */
.prose .katex, main .katex, .stage .caption .katex {
  display: inline-block; max-width: 100%;
  overflow-x: auto; overflow-y: hidden; vertical-align: bottom;
}
/* pre and readouts keep their own scroller and must not push the page. */
pre, .readout { max-width: 100%; overflow-x: auto; }
/* Belt and braces: nothing inside the content column may exceed it. */
/* The content column takes its width from the grid track and never from its
   contents. A single long formula or wide figure used to set a min-width on
   the whole document, so the page scrolled sideways on every phone: 686px of
   document in a 500px viewport. Anything genuinely too wide now scrolls
   inside its own container. */
/* The content column must never exceed the viewport. A single long formula
   or wide figure used to set a min-width on the whole document, giving 686px
   of document in a 500px viewport and a sideways scroll on every phone.
   Anything genuinely too wide now scrolls inside its own container. */
main.wrap { max-width: 100vw; overflow-x: clip; }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

@media print {
  .masthead, .sidebar, .stage .stage-controls, .theme-toggle, .pager, .search { display: none; }
  .layout { grid-template-columns: 1fr; }
  body { background: #fff; color: #000; }
  details.ex { break-inside: avoid; }
  details.ex[open] .answer { display: block; }
  .stage { break-inside: avoid; }
}

/* ------------------------------------------------------- accessibility */
/* The stage owns the arrow-key handler, so it needs a visible focus state. */
.stage:focus-visible { outline: 3px solid var(--stage-orange); outline-offset: 2px; }

/* Bypass the ~35 sidebar links that precede main on every page (WCAG 2.4.1). */
.skip {
  position: absolute; left: .6rem; top: -3rem; z-index: 200;
  background: var(--accent); color: var(--paper);
  font: 600 .9rem/1 var(--sans); padding: .7rem 1rem; border-radius: 6px;
  transition: top .15s;
}
.skip:focus { top: .6rem; }

/* Scrollable regions must be reachable by keyboard. */
.table-scroll:focus-visible, .readout:focus-visible, pre:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
/* A scroll shadow, so a cut-off table looks cut off. The fade band paints
   itself in --fade, which every surface sets to its own background: a fixed
   colour here left a bright fringe on every ground but the one it named. */
.table-scroll, pre, .readout {
  background-image:
    linear-gradient(to right, var(--fade) 30%, transparent),
    linear-gradient(to left,  var(--fade) 30%, transparent),
    linear-gradient(to right, var(--fade-shadow), transparent 12px),
    linear-gradient(to left,  var(--fade-shadow), transparent 12px);
  background-position: left center, right center, left center, right center;
  background-repeat: no-repeat;
  background-size: 32px 100%, 32px 100%, 12px 100%, 12px 100%;
  background-attachment: local, local, scroll, scroll;
}
/* P2-28 the masthead height was hardcoded in three places and was wrong. */
.masthead .crumb { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 480px) { .masthead .crumb { display: none; } }
/* P2-26 disabled controls vanished instead of reading as disabled. */
.stage .stage-controls button:disabled { opacity: 1; color: var(--stage-ink-soft);
  border-color: var(--stage-rule); cursor: not-allowed; }
