/* ==========================================================================
   hexapod-sim.html: the hexapod web sim as a loose plate of the blueprint book.
   A tool page, not a book page: no page turning and no js/book.js. It wears the
   book's look (chapter 1 paper, drafting grid ground, exposure texture, binding
   gutter, the thumb index on the fore-edge) and draws its frames on load.

   The simulation is legacy/simulation.js on the book's three.js r186. It builds the terrain
   toolbar (.tb) inside .viewport, sets #sim-status, toggles .open on the More
   settings panel, .woods on the viewport, and posts {type:'simulation-size'} with
   the height of .sim to the parent window (the book's iframe).

   Embedded in the book as hexapod-sim.html#embed, the #embed span is the :target:
   the desk, sheet, running head, thumb index and title go, the ground is
   transparent (the book page shows through), and .sim keeps its own 14 px of
   padding so every frame line and focus ring stays inside the iframe.

   Tokens are copied from css/book.css (keep the two in step; this page does not
   load book.css). Square corners, no shadows, no gradients but hard 1 px lines.
   ========================================================================== */

/* ------------------------------------------------------------------ 1. TOKENS (from css/book.css) */
:root {
  --desk: #061A33;
  --pp-cover: #0C2950;
  --pp-front: #123A6E;
  --pp-1-open: #143379;     /* 1 Hexapod: ultramarine */
  --pp-1: #1A4290;
  --pp-1b: #183D86;
  --pp-2: #0B3A5A;
  --pp-3: #1F3878;
  --pp-4: #1A5088;
  --pp-end: #0F3060;
  --pp-end-b: #14386C;      /* contact: skills and contact never share a paper */

  --ink: #EEF5FF;           /* text, heavy line */
  --ink-2: #B9D3F3;         /* secondary text, labels (>= 4.8:1 on every paper here) */
  --line: #D8E8FF;          /* frames, rules that matter */
  --line-2: #8EB3E6;        /* construction lines, ticks (non-text only) */

  --f-disp: "Barlow Condensed", "Arial Narrow", "Helvetica Neue", sans-serif;
  --f-body: "IBM Plex Serif", Georgia, "Times New Roman", serif;
  --f-mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
  --t-title: clamp(36px, 4vw, 58px);       /* this page's one heading */
  --t-lede: clamp(18px, 1.45vw, 21px);
  --t-body: 18px;
  --t-cap: 13.5px;
  --t-label: 11.5px;
  --lh: 1.55;

  --lw: 1.5px;
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px; --s7: 48px;
  --pad-x: clamp(18px, 4.4vw, 68px);
  --pad-bot: 56px;
  --tap: 44px;
  --spine: 22px;
  --edge: 30px;

  --ease: cubic-bezier(.65, 0, .35, 1);     /* lines that draw */
  --ease-out: cubic-bezier(.2, .7, .2, 1);  /* things that land */

  /* this page */
  --paper: var(--pp-1);     /* the sheet: a chapter 1 sheet */
  --vp: var(--pp-1-open);   /* inside the 3D viewport and the sticks: the deeper exposure */
  --hover: rgba(216, 232, 255, .1);
  --fog: #CFE0E6;           /* the Woods map's fog colour in legacy/simulation.js (FOGCOL 0xcfe0e6) */

  color-scheme: dark;       /* matches the book, so the iframe stays transparent there */
}

/* ------------------------------------------------------------------ 2. BASE */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; background: var(--desk); }
body {
  margin: 0;
  padding: 0 var(--edge) 0 var(--spine);
  background: var(--desk);
  color: var(--ink);
  font: 400 var(--t-body)/var(--lh) var(--f-body);
  font-variant-numeric: lining-nums;
}
a { color: inherit; text-decoration-thickness: 1px; text-underline-offset: 3px; }
a:hover { text-decoration-thickness: 2px; }
:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px; }
p { margin: 0; }
button, select { font: inherit; color: inherit; border-radius: 0; }
[hidden] { display: none !important; }
.sr-only { position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.skip { position: fixed; left: 12px; top: -80px; z-index: 200; padding: 12px 16px; background: var(--ink); color: var(--pp-cover); font: 600 14px var(--f-mono); }
.skip:focus { top: 12px; }

/* the page edges of the book this plate came from, in the strips beside it (static) */
body::before, body::after {
  content: "";
  position: fixed;
  top: 8px;
  bottom: 8px;
  z-index: 0;
  pointer-events: none;
  background: repeating-linear-gradient(90deg, rgba(142, 179, 230, .55) 0 1px, transparent 1px 3px);
}
body::before { left: 0; width: calc(var(--spine) * .5); }
body::after { right: 0; width: calc(var(--edge) * .75); }

/* ------------------------------------------------------------------ 3. THE SHEET */
.sheet {
  position: relative;
  z-index: 1;
  isolation: isolate;
  min-height: 100vh;
  min-height: 100svh;
  padding: 12px var(--pad-x) var(--pad-bot);
  background-color: var(--paper);
  /* the book's "grid" ground: 24 px minor, 120 px major */
  background-image:
    linear-gradient(rgba(216, 232, 255, .13) 1px, transparent 1px),
    linear-gradient(90deg, rgba(216, 232, 255, .13) 1px, transparent 1px),
    linear-gradient(rgba(216, 232, 255, .05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(216, 232, 255, .05) 1px, transparent 1px);
  background-size: 120px 120px, 120px 120px, 24px 24px, 24px 24px;
  background-position: -1px -1px;
}
/* cyanotype exposure: paper tooth and slow blotches (the book's .page::before, static) */
.sheet::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background-image:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='300'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .86 0 0 0 0 .93 0 0 0 0 1 1.5 0 0 0 -.74'/%3E%3C/filter%3E%3Crect width='300' height='300' filter='url(%23n)'/%3E%3C/svg%3E"),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='900' height='900'%3E%3Cfilter id='b'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.0045' numOctaves='3' seed='11' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .01 0 0 0 0 .05 0 0 0 0 .16 1.6 0 0 0 -.7'/%3E%3C/filter%3E%3Crect width='900' height='900' filter='url(%23b)'/%3E%3C/svg%3E");
  background-size: 300px 300px, 900px 900px;
  background-position: 0 0, -310px -140px;
  opacity: .55;
}
/* the binding edge (two rules, the inner one dark) */
.sheet::after {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 10px;
  z-index: -1;
  pointer-events: none;
  background: linear-gradient(90deg, rgba(2, 10, 24, .6) 0 1px, transparent 1px 5px, rgba(216, 232, 255, .18) 5px 6px, transparent 6px);
}

/* running head: the way back on the left, the book's head on the right */
.rhead {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s4);
  max-width: 1360px;
  margin: 0 auto var(--s6);
  padding-bottom: 2px;
}
.rhead::after { content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 1px; background: rgba(142, 179, 230, .45); transform-origin: 0 50%; }
.back {
  display: inline-flex;
  align-items: center;
  min-height: var(--tap);
  font: 600 12.5px/1.2 var(--f-mono);
  letter-spacing: .12em;
  text-transform: uppercase;
  text-decoration: none;
}
.back::before { content: "\2190\00a0\00a0"; content: "\2190\00a0\00a0" / ""; }
.back:hover { text-decoration: underline; }
.rh-ch { display: flex; gap: var(--s5); font: 500 var(--t-label)/1.2 var(--f-mono); letter-spacing: .14em; text-transform: uppercase; color: var(--ink-2); }

/* ------------------------------------------------------------------ 4. THUMB INDEX (from css/book.css) */
.thumb { position: fixed; z-index: 60; right: 0; top: 50%; transform: translateY(-50%); }
.thumb ol { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 3px; }
.thumb a {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
  width: var(--edge);
  padding: 9px 0 10px;
  border: 1px solid var(--line-2);
  border-right: 0;
  background: var(--tab, var(--pp-cover));
  color: var(--ink);
  text-decoration: none;
  transition: background-color .35s;
}
.thumb a::before { content: ""; position: absolute; top: 0; bottom: 0; right: 0; width: var(--tap); }  /* 44 px hit area */
.thumb b { font: 600 12px/1 var(--f-mono); }
.thumb span { writing-mode: vertical-rl; font: 500 11.5px/1 var(--f-mono); letter-spacing: .08em; text-transform: uppercase; white-space: nowrap; }
.thumb a[data-ch="front"] { --tab: var(--pp-front); }
.thumb a[data-ch="1"] { --tab: var(--pp-1); }
.thumb a[data-ch="2"] { --tab: var(--pp-2); }
.thumb a[data-ch="3"] { --tab: var(--pp-3); }
.thumb a[data-ch="4"] { --tab: var(--pp-4); }
.thumb a[data-ch="end"] { --tab: var(--pp-end); }
.thumb a[href$="#contact"] { --tab: var(--pp-end-b); }
/* a short window (phone landscape, 200% zoom): the tabs keep only their number or letter, so all of
   them fit; the name stays for screen readers */
@media (max-height: 660px) {
  .thumb ol { gap: 2px; }
  .thumb a { padding: 0; min-height: var(--tap); justify-content: center; }
  .thumb span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
}
.thumb a[aria-current] { transform: translateX(-9px); background: var(--ink); color: var(--pp-cover); border-color: var(--ink); }
.thumb a:hover { background-color: color-mix(in srgb, var(--tab, var(--pp-cover)) 80%, white); }
.thumb a[aria-current]:hover { background: var(--ink); }

/* ------------------------------------------------------------------ 5. THE SIM: title, figure left, controls right (7:5) */
.sim {
  display: grid;
  grid-template-columns: minmax(0, 7fr) minmax(0, 5fr);
  gap: var(--s5) clamp(28px, 4.2vw, 64px);
  align-items: start;
  max-width: 1360px;
  margin: 0 auto;
}
.sim:focus { outline: none; }
.sim:focus-visible { outline: 2px solid var(--ink); outline-offset: 8px; }
.col { min-width: 0; }
.right { display: flex; flex-direction: column; gap: var(--s6); }
.sheet-title {
  grid-column: 1 / -1;
  margin: 0;
  font: 700 var(--t-title)/.95 var(--f-disp);
  text-transform: uppercase;
  letter-spacing: .02em;
}

/* the figure: a 3D viewport in a drawn frame, with registration marks and a caption */
.stage { margin: 0; }
.vp-wrap { position: relative; }
.viewport {
  position: relative;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  background: var(--vp);
}
.viewport.woods { background: var(--fog); }
#oc { display: block; width: 100%; height: 100%; }
.viewport :focus-visible { outline-offset: -5px; }   /* inside the button, on its own blue, so it shows on every map */

.fe { position: absolute; z-index: 4; background: var(--line); pointer-events: none; }
.fe.t { top: 0; left: -12px; right: -12px; height: var(--lw); transform-origin: 0 50%; }
.fe.r { right: 0; top: -12px; bottom: -12px; width: var(--lw); transform-origin: 50% 0; }
.fe.b { bottom: 0; left: -12px; right: -12px; height: var(--lw); transform-origin: 100% 50%; }
.fe.l { left: 0; top: -12px; bottom: -12px; width: var(--lw); transform-origin: 50% 100%; }
.reg { position: absolute; z-index: 4; width: 18px; height: 18px; border: 1px solid var(--line-2); border-radius: 50%; pointer-events: none; }
.reg::before, .reg::after { content: ""; position: absolute; background: var(--line-2); }
.reg::before { left: 50%; top: -6px; bottom: -6px; width: 1px; transform: translateX(-.5px); }
.reg::after { top: 50%; left: -6px; right: -6px; height: 1px; transform: translateY(-.5px); }
.reg.r1 { top: -30px; right: -30px; }
.reg.r2 { bottom: -30px; left: -30px; }

.vp-label {
  position: absolute;
  top: 0; left: 0;
  z-index: 3;
  padding: 8px 12px 7px;
  font: 500 var(--t-label)/1.2 var(--f-mono);
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--ink);
  background: var(--vp);
  border-right: var(--lw) solid var(--line);
  border-bottom: var(--lw) solid var(--line);
  pointer-events: none;
}
#loading, .nojs {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: var(--s3);
  padding: var(--s5);
  font: 500 13px/1.5 var(--f-mono);
  letter-spacing: .12em;
  text-transform: uppercase;
  text-align: center;
  color: var(--ink-2);
  pointer-events: none;
}
#loading::after { content: ""; width: 132px; height: var(--lw); background: var(--line); transform-origin: 0 50%; }
.nojs { z-index: 3; background: var(--vp); color: var(--ink); }   /* under the frame lines (.fe, 4) */

/* camera buttons: flush in the top-right corner of the frame */
.cam-tools { position: absolute; top: 0; right: 0; z-index: 3; display: flex; flex-direction: column; }
.cam-tools button {
  display: grid;
  place-items: center;
  width: var(--tap);
  height: var(--tap);
  padding: 0;
  border: 0;
  border-left: var(--lw) solid var(--line);
  border-bottom: var(--lw) solid var(--line);
  background: var(--vp);
  color: var(--ink);
  font: 500 22px/1 var(--f-mono);
  cursor: pointer;
}
.cam-tools button svg { display: block; }
.cam-tools button:hover { background: color-mix(in srgb, var(--vp) 82%, var(--line)); }

figcaption {
  margin-top: var(--s3);
  max-width: 46em;
  font: 400 var(--t-cap)/1.5 var(--f-mono);
  color: var(--ink-2);
}
.cap-lbl { margin-right: 8px; font: 600 12px/1 var(--f-mono); letter-spacing: .1em; text-transform: uppercase; color: var(--ink); }
figcaption a { color: var(--ink); }

/* terrain toolbar (built by the script inside .viewport): a tool tray flush along the bottom edge */
.tb { position: absolute; left: 0; right: 0; bottom: 0; z-index: 3; display: flex; flex-wrap: wrap-reverse; align-items: stretch; pointer-events: none; }
.tb > * { pointer-events: auto; }
.tb-btn, .tb-shape, .tb-item { font: 500 12px/1 var(--f-mono); letter-spacing: .08em; text-transform: uppercase; color: var(--ink); }
.tb-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: var(--tap);
  padding: 0 14px;
  border: 0;
  border-top: var(--lw) solid var(--line);
  border-right: var(--lw) solid var(--line);
  background: var(--vp);
  cursor: pointer;
}
.tb-btn:hover:not(:disabled) { background: color-mix(in srgb, var(--vp) 82%, var(--line)); }
.tb-btn:disabled { color: var(--ink-2); opacity: .6; cursor: default; }
.tb-add { background: var(--ink); color: var(--vp); font-weight: 600; }
.tb-add:hover:not(:disabled) { background: var(--line); }
.tb-maps { position: relative; display: flex; }
.tb-maps svg { transition: transform .2s var(--ease-out); }
.tb.open .tb-maps svg { transform: rotate(180deg); }
.tb-menu {
  position: absolute;
  left: 0; bottom: 100%;
  min-width: 190px;
  border: var(--lw) solid var(--line);
  border-left: 0;
  background: var(--vp);
  visibility: hidden;
  opacity: 0;
  transition: opacity .15s, visibility 0s linear .15s;
}
.tb.open .tb-menu { visibility: visible; opacity: 1; transition: opacity .15s, visibility 0s; }
.tb-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  min-height: var(--tap);
  padding: 0 12px;
  border: 0;
  border-top: 1px dashed var(--line-2);
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.tb-item:first-child { border-top: 0; }
.tb-item:hover:not(:disabled) { background: color-mix(in srgb, var(--vp) 82%, var(--line)); }
.tb-item[aria-checked="true"] { text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 4px; }
.tb-item[aria-checked="true"] span::before { content: "\2192\00a0"; }
.tb-item:disabled { color: var(--ink-2); cursor: default; }
.tb-item em { font-style: normal; font-size: 11px; padding: 2px 6px; border: 1px solid var(--line-2); }
.tb-shape { display: inline-flex; align-items: center; height: var(--tap); border-top: var(--lw) solid var(--line); border-right: var(--lw) solid var(--line); background: var(--vp); }
.tb-shape:focus-visible { outline: 2px solid var(--ink); outline-offset: -5px; }
.tb-arrow { display: grid; place-items: center; width: var(--tap); height: var(--tap); padding: 0; border: 0; background: transparent; color: var(--ink); cursor: pointer; }
.tb-arrow:hover { background: color-mix(in srgb, var(--vp) 82%, var(--line)); }
.tb-icon { display: flex; width: 22px; justify-content: center; }
.tb-name { min-width: 64px; text-align: center; }

/* ------------------------------------------------------------------ 6. THE RIGHT COLUMN: what it is, how to drive, settings */
.lede { margin-bottom: var(--s5); font-size: var(--t-lede); line-height: 1.45; max-width: 30em; }

/* the key: a small two-column legend, like a title block without the frame */
.keys { margin: 0; border-top: var(--lw) solid var(--line); font: 500 13px/1.35 var(--f-mono); }
.keys > div { position: relative; display: grid; grid-template-columns: 5.2em minmax(0, 1fr); align-items: center; gap: var(--s3); padding: 7px 0; }
.keys > div::after { content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 1px; background: var(--line-2); transform-origin: 0 50%; }
.keys dt { font-size: var(--t-label); letter-spacing: .14em; text-transform: uppercase; color: var(--ink-2); }
.keys dd { margin: 0; color: var(--ink); }
kbd { display: inline-block; min-width: 1.85em; padding: 1px 5px 0; border: 1px solid var(--line-2); font: 500 12.5px/1.45 var(--f-mono); text-align: center; color: var(--ink); }

/* sections size their own insides (container queries), so the same rules fit the page,
   the book's iframe and a phone */
.controls, .tuning { container-type: inline-size; }

.drive { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: var(--s4); }
.stick { display: flex; flex-direction: column; align-items: center; gap: var(--s2); }
.stick > span { font: 500 var(--t-label)/1.2 var(--f-mono); letter-spacing: .14em; text-transform: uppercase; color: var(--ink-2); }
/* the stick's frame is drawn as four background lines, so it can be plotted on load */
.stick canvas {
  display: block;
  width: 132px;
  height: 132px;
  touch-action: none;
  cursor: grab;
  background:
    linear-gradient(var(--line), var(--line)) left top / 100% var(--lw) no-repeat,
    linear-gradient(var(--line), var(--line)) right top / var(--lw) 100% no-repeat,
    linear-gradient(var(--line), var(--line)) right bottom / 100% var(--lw) no-repeat,
    linear-gradient(var(--line), var(--line)) left bottom / var(--lw) 100% no-repeat,
    var(--vp);
}
.stick canvas:active { cursor: grabbing; }
.stick canvas:focus-visible { outline-offset: 4px; }

.drive-options { display: flex; flex-direction: column; gap: var(--s2); min-width: 0; }
.opt-label { font: 500 var(--t-label)/1.3 var(--f-mono); letter-spacing: .14em; text-transform: uppercase; color: var(--ink-2); }
.gaits { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s2); }
.acts { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--s2); margin-top: var(--s1); }
.gait-btn, .stab-btn, .reset-btn {
  min-height: var(--tap);
  padding: 8px 6px;
  border: var(--lw) solid var(--line);
  background: transparent;
  color: var(--ink);
  font: 500 12.5px/1.1 var(--f-mono);
  letter-spacing: .08em;
  text-transform: uppercase;
  cursor: pointer;
  transition: background-color .15s, color .15s;
}
.gait-btn:hover, .stab-btn:hover, .reset-btn:hover { background: var(--hover); }
.gait-btn[aria-checked="true"], .stab-btn[aria-checked="true"] { background: var(--ink); border-color: var(--ink); color: var(--pp-1-open); }
.stab-btn { display: flex; align-items: center; justify-content: center; gap: 9px; }
.stab-btn::before { content: ""; flex: none; width: 10px; height: 10px; border: var(--lw) solid currentColor; }
.stab-btn[aria-checked="true"]::before { background: currentColor; }
.reset-btn { border-color: var(--line-2); }

/* sliders: each one a dimension line with extension ticks at both ends and a square cursor */
.sliders { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: var(--s4) var(--s5); }
.sliders label { display: grid; grid-template-columns: minmax(0, 1fr) auto auto; align-items: baseline; min-width: 0; }
.sliders label > span:first-child { padding-right: 8px; font: 500 var(--t-label)/1.4 var(--f-mono); letter-spacing: .1em; text-transform: uppercase; color: var(--ink-2); }
.sliders output { grid-row: 1; grid-column: 2; min-width: 34px; text-align: right; font: 600 14px/1.3 var(--f-mono); font-variant-numeric: tabular-nums; color: var(--ink); }
.sliders .unit { grid-row: 1; grid-column: 3; padding-left: 4px; font: 500 var(--t-label)/1.3 var(--f-mono); color: var(--ink-2); }
.sliders input {
  grid-row: 2;
  grid-column: 1 / -1;
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: var(--tap);
  margin: -6px 0 -4px;
  background:
    linear-gradient(var(--line), var(--line)) left center / 100% var(--lw) no-repeat,
    linear-gradient(var(--line-2), var(--line-2)) left center / 1px 14px no-repeat,
    linear-gradient(var(--line-2), var(--line-2)) right center / 1px 14px no-repeat;
  cursor: pointer;
}
.sliders input::-webkit-slider-runnable-track { height: 2px; background: transparent; border: 0; }
.sliders input::-moz-range-track { height: 2px; background: transparent; border: 0; }
.sliders input::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 12px; height: 22px; margin-top: -10px; border: 2px solid var(--ink); border-radius: 0; background: var(--paper); }
.sliders input::-moz-range-thumb { width: 8px; height: 18px; border: 2px solid var(--ink); border-radius: 0; background: var(--paper); }
.sliders input:active::-webkit-slider-thumb { background: var(--ink); }
.sliders input:active::-moz-range-thumb { background: var(--ink); }
.sliders input:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

.more-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: var(--tap);
  margin-top: var(--s2);
  padding: 0 2px;
  border: 0;
  background: transparent;
  color: var(--ink);
  font: 500 12.5px/1 var(--f-mono);
  letter-spacing: .08em;
  text-transform: uppercase;
  text-decoration: underline;
  text-underline-offset: 4px;
  cursor: pointer;
}
.more-toggle:hover { text-decoration-thickness: 2px; }
.more-toggle svg { transition: transform .3s var(--ease-out); }
.more-toggle.open svg { transform: rotate(180deg); }
/* folded away, behind a dashed (hidden) line */
.more-panel {
  display: grid;
  grid-template-rows: 0fr;
  visibility: hidden;
  border-top: 1px dashed var(--line-2);
  opacity: 0;
  transition: grid-template-rows .4s var(--ease), opacity .3s, visibility 0s linear .4s;
}
.more-panel.open { grid-template-rows: 1fr; visibility: visible; opacity: 1; transition: grid-template-rows .4s var(--ease), opacity .35s, visibility 0s; }
.more-inner { overflow: hidden; min-height: 0; }
.more-inner .sliders { grid-template-columns: repeat(2, minmax(0, 1fr)); padding: var(--s4) 0 var(--s2); }
.tune-note { margin-top: var(--s2); font: 400 12.5px/1.45 var(--f-mono); color: var(--ink-2); }

/* three sliders need about 520 px before a label wraps */
@container (max-width: 520px) {
  .sliders { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
/* a narrow column (a laptop, the book's iframe, a phone): sticks side by side, the gait block under them */
@container (max-width: 470px) {
  .drive { grid-template-columns: 1fr 1fr; justify-items: center; row-gap: var(--s4); }
  .s-move { grid-column: 1; grid-row: 1; }
  .s-turn { grid-column: 2; grid-row: 1; }
  .drive-options { grid-column: 1 / -1; grid-row: 2; width: 100%; }
  .gaits { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .acts { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .sliders { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@container (max-width: 380px) {
  .sliders, .more-inner .sliders { grid-template-columns: minmax(0, 1fr); gap: var(--s3); }
  .stick canvas { width: 112px; height: 112px; }
}
@container (max-width: 300px) {
  .gaits { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ------------------------------------------------------------------ 7. NARROW WINDOWS */
/* one column: title, what it is, the figure, the controls, the key, the settings */
@media (max-width: 900px) {
  .sim { grid-template-columns: minmax(0, 1fr); row-gap: var(--s5); }
  .col, .right, .intro { display: contents; }
  .sheet-title { order: 0; }
  .lede { order: 1; margin-bottom: var(--s2); }
  .stage { order: 2; }
  .controls { order: 3; margin-top: var(--s3); }
  .keys { order: 4; }
  .tuning { order: 5; }
}
@media (max-width: 760px) {
  :root { --spine: 7px; --edge: 24px; --pad-x: 16px; --pad-bot: 40px; --t-body: 17px; }
  .rh-ch span:first-child { display: none; }
  .rh-ch { font-size: 10.5px; letter-spacing: .1em; }
  .back { font-size: 12px; letter-spacing: .08em; }
  .rhead { margin-bottom: var(--s5); }
  .thumb a { gap: 5px; padding: 7px 0 8px; }
  .thumb b { font-size: 11px; }
  .thumb span { font-size: 11px; letter-spacing: .06em; }
  .thumb a[aria-current] { transform: translateX(-6px); }
  .reg { width: 12px; height: 12px; }
  .reg.r1 { top: -16px; right: -9px; }
  .reg.r2 { bottom: -16px; left: -9px; }
  .fe.t, .fe.b { left: -7px; right: -7px; }
  .fe.r, .fe.l { top: -7px; bottom: -7px; }
  figcaption { font-size: 12.5px; }
  .tb-btn { padding: 0 10px; }
  .tb-icon { display: none; }
  .tb-name { min-width: 52px; }
}
@media (max-width: 360px) {
  .rh-ch { display: none; }   /* the thumb index already marks chapter 1; the way back keeps one line */
}
/* a phone: a square view, and the terrain tray moves under it (still inside the frame) as a
   two-by-two strip, so it never covers the robot's feet. The renderer sizes itself to the
   canvas (cv.clientWidth/Height), so the canvas carries the aspect ratio. */
@media (max-width: 560px) {
  .viewport { aspect-ratio: auto; display: flex; flex-direction: column; }
  #oc { height: auto; aspect-ratio: 1 / 1; }
  .tb { position: relative; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); background: var(--vp); }
  .tb-maps .tb-btn, .tb > .tb-btn { width: 100%; justify-content: center; }
  .tb-shape { justify-content: space-between; }
  .tb-name { flex: 1; min-width: 0; }
  .tb-btn, .tb-shape, .tb-item { font-size: 12px; letter-spacing: .06em; }
}

/* without JavaScript nothing here can move: the viewport says so, and the dead controls go */
@media (scripting: none) {
  .controls, .tuning, .keys, .cam-tools, .vp-label { display: none; }
}

/* ------------------------------------------------------------------ 8. EMBEDDED IN THE BOOK (#embed) */
html:has(#embed:target),
body:has(#embed:target) { padding: 0; background: transparent; }
body:has(#embed:target)::before,
body:has(#embed:target)::after,
body:has(#embed:target) .sheet::before,
body:has(#embed:target) .sheet::after,
body:has(#embed:target) .skip,
body:has(#embed:target) .thumb,
body:has(#embed:target) .rhead,
body:has(#embed:target) .reg { display: none; }
body:has(#embed:target) .sheet { min-height: 0; padding: 0; background: none; }
body:has(#embed:target) .sim { max-width: none; padding: 14px; }
body:has(#embed:target) .sim:focus-visible { outline-offset: -4px; }
/* the book's viewport already frames the iframe: the inner frame closes at its corners */
body:has(#embed:target) .fe.t, body:has(#embed:target) .fe.b { left: 0; right: 0; }
body:has(#embed:target) .fe.r, body:has(#embed:target) .fe.l { top: 0; bottom: 0; }
/* the book's sheet around the iframe already says what this is, how to drive it and that it is
   browser only (chapters/10-hexapod.html, sheet 1.6), so the iframe holds only the instrument */
body:has(#embed:target) .sheet-title { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
body:has(#embed:target) .intro,
body:has(#embed:target) .stage figcaption { display: none; }
@media (max-width: 560px) {
  body:has(#embed:target) .sim { padding: 10px; }
}

/* ------------------------------------------------------------------ 9. MOTION: things that are drawn, draw themselves on load */
/* Text never animates; it is simply there. The frame, its registration marks, the rules, the
   stick frames and the slider scales plot in, then the script swings the camera in (it skips
   that under reduced motion). Pure CSS keyframes, so nothing waits on the 1.6 MB of script. */
@media (prefers-reduced-motion: no-preference) {
  .rhead::after { animation: draw-x .9s var(--ease) .05s both; }
  .fe.t { animation: draw-x .3s var(--ease) .2s both; }
  .fe.r { animation: draw-y .3s var(--ease) .38s both; }
  .fe.b { animation: draw-x .3s var(--ease) .56s both; }
  .fe.l { animation: draw-y .3s var(--ease) .74s both; }
  .reg { animation: reg-in .5s var(--ease-out) .9s both; }
  .keys > div::after { animation: draw-x .5s var(--ease) both; }
  .keys > div:nth-child(1)::after { animation-delay: .35s; }
  .keys > div:nth-child(2)::after { animation-delay: .45s; }
  .keys > div:nth-child(3)::after { animation-delay: .55s; }
  .stick canvas { animation: frame-in .8s var(--ease) both; }
  .s-move canvas { animation-delay: .5s; }
  .s-turn canvas { animation-delay: .7s; }
  .sliders input { animation: scale-in .6s var(--ease) both; }
  .sliders label:nth-child(1) input { animation-delay: .8s; }
  .sliders label:nth-child(2) input { animation-delay: .9s; }
  .sliders label:nth-child(3) input { animation-delay: 1s; }
  .sliders label:nth-child(4) input { animation-delay: 1.1s; }
  /* while the script loads, a pen line runs under the words */
  #loading::after { animation: plotting 1.1s var(--ease) infinite alternate; }
}
@keyframes draw-x { from { transform: scaleX(0); } }
@keyframes draw-y { from { transform: scaleY(0); } }
@keyframes reg-in { from { opacity: 0; transform: rotate(-120deg) scale(.4); } }
@keyframes plotting { from { transform: scaleX(.04); } to { transform: scaleX(1); } }
/* the four sides of a stick's frame, one after another, clockwise from the top left */
@keyframes frame-in {
  0%   { background-size: 0 var(--lw), var(--lw) 0, 0 var(--lw), var(--lw) 0; }
  25%  { background-size: 100% var(--lw), var(--lw) 0, 0 var(--lw), var(--lw) 0; }
  50%  { background-size: 100% var(--lw), var(--lw) 100%, 0 var(--lw), var(--lw) 0; }
  75%  { background-size: 100% var(--lw), var(--lw) 100%, 100% var(--lw), var(--lw) 0; }
  100% { background-size: 100% var(--lw), var(--lw) 100%, 100% var(--lw), var(--lw) 100%; }
}
/* a slider's scale: the line runs out from the left, the end ticks rise */
@keyframes scale-in {
  0%   { background-size: 0 var(--lw), 1px 0, 1px 0; }
  70%  { background-size: 100% var(--lw), 1px 14px, 1px 0; }
  100% { background-size: 100% var(--lw), 1px 14px, 1px 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition-duration: 0s !important; transition-delay: 0s !important; }
}
