USER RULES — APPLY TO THIS PROMPT AND ALL FOLLOW-UP WORK
1. Do not create, play, render, include, or mux any music. Use only nonmusical sound effects where useful. Narration may be used only when I request or supply it, without a musical backing.
2. Do not generate any anti-Islamic content or any content that goes against Islamic values. If my topic or a proposed scene might conflict with this rule, ask me to clarify or choose a compliant alternative before generating it.
END USER RULES

I want a short 3D motion graphic made with three.js in the "Blueprint Build" style. Below is the complete source of a reference animation in this exact style, plus the small rendering kit and sound kit it runs on. Use them as your style guide and rebuild the animation around my content.

MY CONTENT:
[Describe your topic, or paste your script here. If this is left as is, ask me for it before building.]


STORYBOARD FIRST: before building the animation, make a VISUAL storyboard and wait for my OK.
- One HTML page (show it as an artifact if you can) with a grid of panels, one per beat (about 3-6). Each panel is a still of that beat's key frame, rendered with the STAGE kit from a block-out of the scene: simple forms in the reference's materials, lighting, camera angle and grade, so the panels already look like the finished piece. Render the stills one after another on one canvas with S.seek(time) at about 16 samples (enough for depth of field and soft shadows to read) and copy each into an image. Stills only, no animation or sound yet.
- Under each panel: the beat number, its start and end time, the exact on-screen text, what moves or changes to get there from the previous panel, what the camera does, and its key sounds.
- Below the grid, list any facts or numbers you had to assume.
When I approve it (or after my changes), build the full animation exactly as storyboarded, reusing the storyboard's scene code.

WHAT TO BUILD
One self-contained HTML file (show it as an artifact if you can):
- three.js 0.187.0 core only, loaded as an ES module from a CDN with an import map (no addons), then set window.THREE = THREE before running the kit and the style code.
- A 1920x1080 canvas scaled to fit the window, with a small control bar: play/pause, a scrub slider, the time, a sound toggle, loop on by default.
- A new style object, built the same way as the reference (content block first, then build(S) returning update(t)), that tells MY CONTENT in this style. 7-12 seconds unless my content needs more.
- Run it with the kit: const S = await K.run(canvas, style, { samples }). Use a low sample count (2-4) while playing live and the style's full count when rendering frames. Expose window.seek = (t) => S.seek(t) and window.DURATION.
- Play the style's sound(k) score in sync from the player: on play call stopSound = SOUND.live((k) => style.sound(k), currentT, { voiceover: false }), and call stopSound() on pause, on seek and when the sound toggle is turned off.
- Include the STAGE kit and the SOUND kit below unchanged, and the STROKEFONT library.

HOW TO STAY IN THE STYLE
- The reference shows the look, not the story. Its objects, layout and beats belong to its own topic: do not reuse them unless my content calls for them. Work out what my content needs to show first, then build those things in this look.
- Tell my content as an explainer, not a title card: things happen to objects or characters over three or four beats that follow from each other, and the picture does the telling. Words are small captions and labels, never the subject of a shot. End on the result of the process or the end of the story, not on a headline, slogan or logo.
- Keep the reference's materials, lighting, camera language, grade and the way things move. Change the story, the objects and the words; do not change the look.
- update(t) must set the whole scene for time t and nothing may build up between calls: the kit calls it many times per frame to make motion blur, depth of field and soft shadows. No Math.random, Date or timers; use K.rng(seed).
- Build everything in code: no downloaded models, images, HDRIs or textures.
- Simple forms, beautifully lit and finished, beat complicated models that look wrong. Draw my subject accurately and double-check any facts or numbers you show.
- It is video, often watched on a phone: keep every word the story needs large, sharp and on screen long enough to read, keep the subject filling most of the frame, and keep everything that matters inside the frame for the whole camera move. The first frame should already be worth looking at, and the last second should hold.

FLOW BETWEEN BEATS
- A new beat does not automatically mean a new scene. When the next idea builds on the last, stay in the same world: the things that matter stay on screen and move, grow, re-arrange or transform, and the camera travels to the part of the scene where the next idea happens.
- When the story really moves on (a new place or time, a before/after contrast, a reveal), change scene the way this look would: a turn of the set, a page, a passing object that wipes the frame, a change of light. No hard cuts.
- The reference's scene count is not a template: fit the number of beats to my content.

SOUND (no voiceover: a fuller mix)
- A sound starts on the exact frame its event appears: drive picture and sound from the same schedule, as the reference does.
- Score the events that land (things arriving, labels, contacts, transitions, the key number, the result) with sounds this material would make: glass, plastic, paper, metal, motors.
- Use only sparse sound effects where they clarify visible events. Leave small silences around key moments.


If you are Claude Code with a shell: render an MP4 in headless Chrome with the GPU enabled. Load the page, call window.seek(i / 30) for every frame, capture the canvas and pipe the frames into ffmpeg; render the sound with SOUND.renderOffline((k) => style.sound(k), DURATION, 48000, { voiceover: false }) and mux it in at about -15 LUFS. Look at a few rendered frames and fix framing, lighting or legibility problems before you call it done.

STYLE CARD
Name: Blueprint Build
Family: Diagrams with depth
Use: explainer
Look: A real drafting sheet: deep blueprint blue with grain and worn fold creases, a title block, a construction grid, crisp white linework in two weights with hidden edges dashed, dimension arrows and numbered notes in plotted single-stroke capitals. The drawing turns out to be a 3D wireframe, and then a solid object standing on its own plan.
Motion: Lines plot themselves in pen order with a hot tip. As the camera tips over, each part rises out of its own plan, which stays printed on the sheet. The numbered notes peel off the paper, turn to face the lens and are tied to the model with leaders, one by one. Then a hatched section plane climbs through the wireframe and leaves real material under it.
Light: One soft key from behind and to the side, so tops and one flank are lit, the near faces stay in shade and a long soft shadow falls toward the camera across the sheet. A little cool fill. At the end the desk lamp in the model switches on.
Materials: Uncoated blue paper that takes the light, flat white ink. The built object is restrained and real: pale oak, matt white steel, rust wool, off-white plaster, graphite plastic. The white lines stay on it as a faint ghost.
Camera: One continuous crane move with a long lens that opens up: it starts straight above the sheet, where the drawing reads as a flat plan, tips down to a three-quarter view as the drawing gains height, and keeps drifting in until the last frame.
Finish: Almost none: neutral tone curve so the blue and white stay true, light vignette, a trace of bloom on pen tips and the lamp, fine grain. Hidden lines are found by a real depth test, not drawn by hand.
Good for: Plans, set-ups and how-it-is-made: first the layout as a flat drawing, then the same thing as an object, with dimensions and two or three numbered pointers attached to the places they talk about.

STAGE KIT (lib/stage.js)
```js
// STAGE: a small film-style rendering kit for three.js motion graphics. Classic script; needs a global THREE (core only,
// no addons). It gives every style the same pipeline:
//   - one clock: stage.seek(t) builds the whole frame for time t, so frames can be rendered alone and in any order
//   - accumulation rendering: each frame is the average of N sub-renders, which gives real motion blur, anti-aliasing,
//     true depth of field (the camera moves across a lens aperture) and soft area-light shadows (lights move across
//     their own surface), all without post-process tricks
//   - HDR bloom, tone mapping, vignette, grain, chromatic aberration and a crisp 2D overlay for captions
//   - code-built lighting environments (no HDRI files), a real mirror floor, canvas textures, text planes, easing,
//     seeded random and noise
// Globals: K (helpers) and registerStyle(). A style is { id, name, ..., render: {...}, build(S) -> { update(t), overlay? } }.
(function (root) {
  const THREE = root.THREE;
  const K = {};
  const STYLES = [];
  root.STYLES = STYLES;
  root.registerStyle = (s) => { STYLES.push(s); return s; };

  // ---------------------------------------------------------------- maths
  K.clamp = (x, a = 0, b = 1) => Math.min(b, Math.max(a, x));
  K.lerp = (a, b, t) => a + (b - a) * t;
  K.map = (x, a, b, c, d) => c + (d - c) * K.clamp((x - a) / (b - a));
  K.smooth = (a, b, x) => { const t = K.clamp((x - a) / (b - a)); return t * t * (3 - 2 * t); };
  // progress of time t through the window [a, b], 0..1, optionally eased: K.seg(t, 1, 2.5, K.ease.out)
  K.seg = (t, a, b, ease) => { const u = K.clamp((t - a) / (b - a)); return ease ? ease(u) : u; };
  K.ease = {
    linear: (t) => t,
    in: (t) => t * t * t,
    out: (t) => 1 - Math.pow(1 - t, 3),
    inOut: (t) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2),
    outQuint: (t) => 1 - Math.pow(1 - t, 5),
    inOutQuint: (t) => (t < 0.5 ? 16 * t * t * t * t * t : 1 - Math.pow(-2 * t + 2, 5) / 2),
    outExpo: (t) => (t >= 1 ? 1 : 1 - Math.pow(2, -10 * t)),
    inExpo: (t) => (t <= 0 ? 0 : Math.pow(2, 10 * t - 10)),
    outBack: (t, s = 1.70158) => 1 + (s + 1) * Math.pow(t - 1, 3) + s * Math.pow(t - 1, 2),
    inBack: (t, s = 1.70158) => (s + 1) * t * t * t - s * t * t,
    outElastic: (t) => (t <= 0 ? 0 : t >= 1 ? 1 : Math.pow(2, -10 * t) * Math.sin((t * 10 - 0.75) * (2 * Math.PI) / 3) + 1),
    outBounce: (t) => { const n = 7.5625, d = 2.75; if (t < 1 / d) return n * t * t; if (t < 2 / d) return n * (t -= 1.5 / d) * t + 0.75; if (t < 2.5 / d) return n * (t -= 2.25 / d) * t + 0.9375; return n * (t -= 2.625 / d) * t + 0.984375; },
  };
  // damped spring from 0 to 1 starting at time 0 (t in seconds). freq in Hz, damp 0..1 (1 = no overshoot)
  K.spring = (t, freq = 2.2, damp = 0.45) => {
    if (t <= 0) return 0;
    const w = 2 * Math.PI * freq;
    if (damp >= 1) return 1 - Math.exp(-w * t) * (1 + w * t);
    const wd = w * Math.sqrt(1 - damp * damp);
    return 1 - Math.exp(-damp * w * t) * (Math.cos(wd * t) + (damp * w / wd) * Math.sin(wd * t));
  };
  // keyframe track: K.track([[0, [0,2,8]], [3, [2,2,6], 'inOut'], [6, 5, K.ease.out]]) -> f(t). Values are numbers or
  // arrays; the ease on a key shapes the move INTO that key.
  K.track = (keys) => (t) => {
    if (t <= keys[0][0]) return keys[0][1];
    for (let i = 1; i < keys.length; i++) {
      if (t <= keys[i][0]) {
        const [t0, v0] = keys[i - 1], [t1, v1, e] = keys[i];
        const fn = typeof e === 'function' ? e : K.ease[e || 'inOut'];
        const u = fn((t - t0) / (t1 - t0));
        return Array.isArray(v0) ? v0.map((a, k) => a + (v1[k] - a) * u) : v0 + (v1 - v0) * u;
      }
    }
    return keys[keys.length - 1][1];
  };

  // like K.track, but the value flows smoothly THROUGH the middle keys without stopping at them (it only eases in at
  // the first key and out at the last). Use it for camera moves that should feel like one continuous shot.
  K.path = (keys) => {
    const n = keys.length, arr = Array.isArray(keys[0][1]), val = (i) => (arr ? keys[i][1] : [keys[i][1]]), dim = val(0).length;
    const m = keys.map((_, i) => (i === 0 || i === n - 1 ? new Array(dim).fill(0) : val(i).map((_, k) => (val(i + 1)[k] - val(i - 1)[k]) / (keys[i + 1][0] - keys[i - 1][0]))));
    return (t) => {
      if (t <= keys[0][0]) return keys[0][1];
      if (t >= keys[n - 1][0]) return keys[n - 1][1];
      let i = 1; while (keys[i][0] < t) i++;
      const t0 = keys[i - 1][0], h = keys[i][0] - t0, u = (t - t0) / h, u2 = u * u, u3 = u2 * u;
      const a = val(i - 1), b = val(i), out = a.map((_, k) => (2 * u3 - 3 * u2 + 1) * a[k] + (u3 - 2 * u2 + u) * h * m[i - 1][k] + (-2 * u3 + 3 * u2) * b[k] + (u3 - u2) * h * m[i][k]);
      return arr ? out : out[0];
    };
  };

  // ---------------------------------------------------------------- seeded random and noise
  K.rng = (seed) => {
    let a = (typeof seed === 'string' ? [...seed].reduce((h, c) => Math.imul(h ^ c.charCodeAt(0), 16777619), 2166136261) : seed * 2654435761) >>> 0;
    const r = () => { a = (a + 0x6d2b79f5) | 0; let t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
    r.range = (lo, hi) => lo + (hi - lo) * r();
    r.int = (lo, hi) => Math.floor(lo + (hi - lo + 1) * r());
    r.pick = (arr) => arr[Math.floor(r() * arr.length)];
    r.chance = (p) => r() < p;
    r.gauss = () => { let u = 0, v = 0; while (u === 0) u = r(); v = r(); return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v); };
    r.sign = () => (r() < 0.5 ? -1 : 1);
    return r;
  };
  // a stable 0..1 hash of up to three numbers (handy for per-instance variation without storing arrays)
  K.hash = (x, y = 0, z = 0) => { let h = Math.sin(x * 127.1 + y * 311.7 + z * 74.7) * 43758.5453123; return h - Math.floor(h); };
  K.noise = (seed = 1) => {
    const r = K.rng(seed), p = new Uint8Array(512), perm = Array.from({ length: 256 }, (_, i) => i);
    for (let i = 255; i > 0; i--) { const j = Math.floor(r() * (i + 1)); [perm[i], perm[j]] = [perm[j], perm[i]]; }
    for (let i = 0; i < 512; i++) p[i] = perm[i & 255];
    const fade = (t) => t * t * t * (t * (t * 6 - 15) + 10);
    const grad = (h, x, y, z) => { const u = (h & 15) < 8 ? x : y, v = (h & 15) < 4 ? y : (h & 15) === 12 || (h & 15) === 14 ? x : z; return ((h & 1) ? -u : u) + ((h & 2) ? -v : v); };
    const n3 = (x, y, z) => {
      const X = Math.floor(x) & 255, Y = Math.floor(y) & 255, Z = Math.floor(z) & 255;
      x -= Math.floor(x); y -= Math.floor(y); z -= Math.floor(z);
      const u = fade(x), v = fade(y), w = fade(z), A = p[X] + Y, AA = p[A] + Z, AB = p[A + 1] + Z, B = p[X + 1] + Y, BA = p[B] + Z, BB = p[B + 1] + Z;
      const L = K.lerp;
      return L(L(L(grad(p[AA], x, y, z), grad(p[BA], x - 1, y, z), u), L(grad(p[AB], x, y - 1, z), grad(p[BB], x - 1, y - 1, z), u), v),
        L(L(grad(p[AA + 1], x, y, z - 1), grad(p[BA + 1], x - 1, y, z - 1), u), L(grad(p[AB + 1], x, y - 1, z - 1), grad(p[BB + 1], x - 1, y - 1, z - 1), u), v), w);
    };
    const fbm = (x, y, z = 0, oct = 4) => { let s = 0, a = 0.5, f = 1; for (let i = 0; i < oct; i++) { s += a * n3(x * f, y * f, z * f); a *= 0.5; f *= 2; } return s; };
    return { n3, n2: (x, y) => n3(x, y, 0), fbm };
  };
  const halton = (i, b) => { let f = 1, r = 0; while (i > 0) { f /= b; r += f * (i % b); i = Math.floor(i / b); } return r; };
  K.halton = halton;

  // ---------------------------------------------------------------- textures, text, geometry
  // K.canvasTex(1024, 512, (ctx, w, h) => {...}) -> THREE.CanvasTexture (sRGB, mipmapped). Redraw with tex.redraw(fn).
  K.canvasTex = (w, h, draw, opts = {}) => {
    const cv = document.createElement('canvas'); cv.width = w; cv.height = h;
    const ctx = cv.getContext('2d');
    if (draw) draw(ctx, w, h);
    const tex = new THREE.CanvasTexture(cv);
    tex.colorSpace = opts.data ? THREE.NoColorSpace : THREE.SRGBColorSpace;
    tex.anisotropy = 8; tex.generateMipmaps = true; tex.minFilter = THREE.LinearMipmapLinearFilter;
    if (opts.repeat) { tex.wrapS = tex.wrapT = THREE.RepeatWrapping; tex.repeat.set(opts.repeat[0], opts.repeat[1]); }
    tex.redraw = (fn) => { ctx.clearRect(0, 0, w, h); fn(ctx, w, h); tex.needsUpdate = true; };
    tex.canvas2d = cv; tex.ctx = ctx;
    return tex;
  };
  // A flat piece of text living in the 3D world. K.textPlane('Hello', { font: '800 160px "Inter"', color: '#fff',
  // height: 0.5 }) -> Mesh whose world height is `height` units per line. opts: align, lineHeight, glow (emissive
  // multiplier for bloom), lit (use a lit material so it takes shadows), maxWidthPx, letterSpacing (px), pad (px).
  K.textPlane = (text, o = {}) => {
    const font = o.font || '700 160px "Inter"', px = +(/(\d+(?:\.\d+)?)px/.exec(font) || [0, 160])[1];
    const lines = String(text).split('\n'), lh = (o.lineHeight || 1.12) * px, pad = o.pad == null ? Math.ceil(px * 0.25) : o.pad;
    const m = document.createElement('canvas').getContext('2d'); m.font = font;
    if (o.letterSpacing) m.letterSpacing = o.letterSpacing + 'px';
    const wpx = Math.ceil(Math.max(...lines.map((l) => m.measureText(l).width))) + pad * 2, hpx = Math.ceil(lh * lines.length) + pad * 2;
    const tex = K.canvasTex(wpx, hpx, (ctx) => {
      ctx.font = font; if (o.letterSpacing) ctx.letterSpacing = o.letterSpacing + 'px';
      ctx.textBaseline = 'middle'; ctx.textAlign = o.align || 'center'; ctx.fillStyle = o.color || '#ffffff';
      const x = o.align === 'left' ? pad : o.align === 'right' ? wpx - pad : wpx / 2;
      lines.forEach((l, i) => ctx.fillText(l, x, pad + lh * (i + 0.5)));
    });
    const unit = (o.height || 1) / px, hWorld = hpx * unit, wWorld = wpx * unit; // `height` = world size of the font's px size
    const mat = o.lit
      ? new THREE.MeshStandardMaterial({ map: tex, transparent: true, alphaTest: 0.02, roughness: o.roughness == null ? 0.8 : o.roughness, metalness: 0 })
      : new THREE.MeshBasicMaterial({ map: tex, transparent: true, depthWrite: false });
    if (o.glow && !o.lit) mat.color.setScalar(o.glow);
    const mesh = new THREE.Mesh(new THREE.PlaneGeometry(wWorld, hWorld), mat);
    mesh.userData.size = [wWorld, hWorld]; mesh.userData.tex = tex;
    return mesh;
  };
  // Box with rounded edges, centred on the origin. r = edge radius, seg = roundness.
  K.roundedBox = (w, h, d, r = 0.05, seg = 6) => {
    r = Math.max(1e-4, Math.min(r, w / 2, h / 2, d / 2));
    const N = 2 * seg + 1, g = new THREE.BoxGeometry(2, 2, 2, N, N, N), p = g.attributes.position, nrm = g.attributes.normal;
    const half = [w / 2 - r, h / 2 - r, d / 2 - r], v = new THREE.Vector3();
    for (let i = 0; i < p.count; i++) {
      const c = [p.getX(i), p.getY(i), p.getZ(i)], inner = [0, 0, 0], a = [0, 0, 0];
      for (let k = 0; k < 3; k++) {
        const idx = Math.round((c[k] + 1) * N / 2), side = idx <= seg ? -1 : 1;
        a[k] = idx <= seg ? -(1 - idx / seg) : (idx - seg - 1) / seg; inner[k] = side * half[k];
      }
      v.set(Math.tan(a[0] * Math.PI / 4), Math.tan(a[1] * Math.PI / 4), Math.tan(a[2] * Math.PI / 4)).normalize();
      p.setXYZ(i, inner[0] + v.x * r, inner[1] + v.y * r, inner[2] + v.z * r); nrm.setXYZ(i, v.x, v.y, v.z);
    }
    // UVs: each of the six sides is mapped flat, 0..1 across the whole side, so textures are not stretched at the corners
    const uv = g.attributes.uv, per = (N + 1) * (N + 1), sz = [w, h, d];
    const ax = [[2, 1, -1], [2, 1, 1], [0, 2, 1, -1], [0, 2, 1, 1], [0, 1, 1], [0, 1, -1]];   // [u axis, v axis, u sign, v sign] for +x -x +y -y +z -z
    for (let i = 0; i < p.count; i++) {
      const f = ax[Math.min(5, Math.floor(i / per))], c = [p.getX(i), p.getY(i), p.getZ(i)];
      uv.setXY(i, f[2] * c[f[0]] / sz[f[0]] + 0.5, (f[3] || 1) * c[f[1]] / sz[f[1]] + 0.5);
    }
    g.computeBoundingBox(); g.computeBoundingSphere();
    return g;
  };
  // Flat rounded rectangle (a card, a screen, a tile), in the XY plane facing +Z, with UVs 0..1 across it.
  K.roundedRect = (w, h, r = 0.05, seg = 8) => {
    const s = new THREE.Shape(), x = w / 2, y = h / 2; r = Math.min(r, x, y);
    s.moveTo(-x + r, -y); s.lineTo(x - r, -y); s.absarc(x - r, -y + r, r, -Math.PI / 2, 0); s.lineTo(x, y - r); s.absarc(x - r, y - r, r, 0, Math.PI / 2);
    s.lineTo(-x + r, y); s.absarc(-x + r, y - r, r, Math.PI / 2, Math.PI); s.lineTo(-x, -y + r); s.absarc(-x + r, -y + r, r, Math.PI, Math.PI * 1.5);
    const g = new THREE.ShapeGeometry(s, seg), p = g.attributes.position, uv = g.attributes.uv;
    for (let i = 0; i < p.count; i++) uv.setXY(i, p.getX(i) / w + 0.5, p.getY(i) / h + 0.5);
    return g;
  };
  // Tube along a list of [x,y,z] points (smooth Catmull-Rom). closed loops allowed.
  K.tube = (pts, radius = 0.05, o = {}) => {
    const curve = new THREE.CatmullRomCurve3(pts.map((p) => new THREE.Vector3(p[0], p[1], p[2] || 0)), !!o.closed, 'centripetal');
    const g = new THREE.TubeGeometry(curve, o.segments || Math.max(8, pts.length * 3), radius, o.radial || 12, !!o.closed);
    g.userData.curve = curve;
    return g;
  };
  // Set instance i of an InstancedMesh: K.place(mesh, i, [x,y,z], [rx,ry,rz], scale | [sx,sy,sz]). Call
  // mesh.instanceMatrix.needsUpdate = true once after a batch.
  const _o = new THREE.Object3D();
  K.place = (mesh, i, p, r, s) => {
    _o.position.set(p[0], p[1], p[2]); if (r) _o.rotation.set(r[0], r[1], r[2]); else _o.rotation.set(0, 0, 0);
    if (s == null) _o.scale.set(1, 1, 1); else if (typeof s === 'number') _o.scale.set(s, s, s); else _o.scale.set(s[0], s[1], s[2]);
    _o.updateMatrix(); mesh.setMatrixAt(i, _o.matrix);
  };
  // Camera: position, look-at target, optional fov and roll. K.focus sets the lens: focus distance (or a point to
  // focus on) and aperture radius in world units (0 = everything sharp).
  const _v = new THREE.Vector3();
  K.look = (cam, pos, target, fov, roll) => {
    cam.position.set(pos[0], pos[1], pos[2]); cam.up.set(0, 1, 0); cam.lookAt(target[0], target[1], target[2]);
    if (roll) cam.rotateZ(roll);
    if (fov && fov !== cam.fov) { cam.fov = fov; cam.updateProjectionMatrix(); }
  };
  K.focus = (cam, at, aperture) => {
    cam.userData.focus = typeof at === 'number' ? at : _v.set(at[0], at[1], at[2]).distanceTo(cam.position);
    if (aperture != null) cam.userData.aperture = aperture;
  };

  // ---------------------------------------------------------------- lighting
  // Code-built lighting environment for reflections and soft fill (replaces an HDRI). K.env(S, { top, horizon,
  // bottom: sky gradient colours; panels: [{ pos:[x,y,z], size:[w,h], color, power }] bright rectangles that face the
  // origin (softboxes, windows, a sun); intensity; background: true to also show it behind the scene; blur }).
  // Metal and glass can only shine if the environment has something bright in it.
  K.env = (S, o = {}) => {
    const es = new THREE.Scene();
    const sky = new THREE.Mesh(new THREE.SphereGeometry(60, 48, 24), new THREE.ShaderMaterial({
      side: THREE.BackSide, depthWrite: false,
      uniforms: { top: { value: new THREE.Color(o.top || '#0b0d12') }, hor: { value: new THREE.Color(o.horizon || o.top || '#14171f') }, bot: { value: new THREE.Color(o.bottom || o.horizon || '#050506') }, gain: { value: o.skyPower == null ? 1 : o.skyPower } },
      vertexShader: 'varying vec3 vD; void main(){ vD = normalize(position); gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }',
      fragmentShader: 'varying vec3 vD; uniform vec3 top, hor, bot; uniform float gain; void main(){ float y = normalize(vD).y; vec3 c = y > 0.0 ? mix(hor, top, pow(y, 0.6)) : mix(hor, bot, pow(-y, 0.5)); gl_FragColor = vec4(c * gain, 1.0); }',
    }));
    es.add(sky);
    for (const p of o.panels || []) {
      const c = new THREE.Color(p.color || '#ffffff').multiplyScalar(p.power == null ? 5 : p.power);
      const m = new THREE.Mesh(p.round ? new THREE.CircleGeometry((p.size ? p.size[0] : 4) / 2, 40) : new THREE.PlaneGeometry(p.size ? p.size[0] : 4, p.size ? p.size[1] : 4), new THREE.MeshBasicMaterial({ color: c, side: THREE.DoubleSide }));
      m.position.set(p.pos[0], p.pos[1], p.pos[2]); m.lookAt(0, p.aimY || 0, 0); es.add(m);
    }
    const pm = new THREE.PMREMGenerator(S.renderer);
    const rt = pm.fromScene(es, o.blur == null ? 0.02 : o.blur, 0.1, 200);
    pm.dispose();
    S.scene.environment = rt.texture;
    S.scene.environmentIntensity = o.intensity == null ? 1 : o.intensity;
    if (o.background) { S.scene.background = rt.texture; S.scene.backgroundBlurriness = o.backgroundBlur == null ? 0.5 : o.backgroundBlur; S.scene.backgroundIntensity = o.backgroundIntensity == null ? 1 : o.backgroundIntensity; }
    return rt.texture;
  };
  // A shadow-casting spot light with a real size: `soft` is the radius of the light's surface in world units, which
  // gives true soft-edged shadows (bigger = softer). o: pos, target, color, power, angle, penumbra, soft, mapSize, near, far.
  K.spot = (S, o = {}) => {
    const l = new THREE.SpotLight(o.color || '#ffffff', o.power == null ? 80 : o.power, o.distance || 0, o.angle == null ? 0.5 : o.angle, o.penumbra == null ? 0.6 : o.penumbra, o.decay == null ? 2 : o.decay);
    l.position.set(...(o.pos || [3, 6, 4])); l.target.position.set(...(o.target || [0, 0, 0]));
    l.castShadow = o.shadow !== false; l.shadow.mapSize.setScalar(o.mapSize || 2048); l.shadow.bias = o.bias == null ? -0.0004 : o.bias; l.shadow.normalBias = o.normalBias == null ? 0.02 : o.normalBias;
    l.shadow.camera.near = o.near || 0.5; l.shadow.camera.far = o.far || 60;
    S.scene.add(l, l.target);
    if (o.soft) S.softLights.push({ light: l, radius: o.soft });
    return l;
  };
  // A sun: parallel light with shadows. `size` is the half-width of the area that gets shadows, `soft` the blur of
  // shadow edges in world units at the light (0.3-1.5 looks natural). o: pos, target, color, power, size, soft, mapSize.
  K.sun = (S, o = {}) => {
    const l = new THREE.DirectionalLight(o.color || '#ffffff', o.power == null ? 3 : o.power);
    l.position.set(...(o.pos || [6, 10, 5])); l.target.position.set(...(o.target || [0, 0, 0]));
    const s = o.size || 8, c = l.shadow.camera;
    l.castShadow = o.shadow !== false; c.left = -s; c.right = s; c.top = s; c.bottom = -s; c.near = 0.5; c.far = o.far || 80;
    l.shadow.mapSize.setScalar(o.mapSize || 2048); l.shadow.bias = o.bias == null ? -0.0004 : o.bias; l.shadow.normalBias = o.normalBias == null ? 0.02 : o.normalBias;
    S.scene.add(l, l.target);
    if (o.soft) S.softLights.push({ light: l, radius: o.soft });
    return l;
  };
  // Sky light with real occlusion: light arrives from the whole dome and is blocked by objects, so creases, contact
  // points and the ground under things darken softly (ambient occlusion, but physically made). o: color, power,
  // size (half-width covered), center, mapSize, spread (1 = full dome, lower = more from above).
  K.skyLight = (S, o = {}) => {
    const l = new THREE.DirectionalLight(o.color || '#ffffff', o.power == null ? 1.5 : o.power);
    const s = o.size || 8, c = l.shadow.camera;
    l.castShadow = true; c.left = -s; c.right = s; c.top = s; c.bottom = -s; c.near = 0.5; c.far = (o.dist || 30) * 2;
    l.shadow.mapSize.setScalar(o.mapSize || 1024); l.shadow.bias = o.bias == null ? -0.0006 : o.bias; l.shadow.normalBias = o.normalBias == null ? 0.03 : o.normalBias;
    l.target.position.set(...(o.center || [0, 0, 0]));
    S.scene.add(l, l.target);
    S.skyLights.push({ light: l, dist: o.dist || 30, spread: o.spread == null ? 1 : o.spread });
    return l;
  };
  // A real mirror floor at height y: reflects the scene, and still takes light and shadows like a normal material.
  // o: y, size, color, roughness (0 = perfect mirror, 0.3 = soft blurred reflection), strength (0..1), metalness,
  // fresnel (true = reflects more at grazing angles, like polished stone or wet ground), map/normalMap etc via `material`.
  K.mirrorFloor = (S, o = {}) => {
    const y = o.y || 0, W = S.W, H = S.H, scale = o.resolution || 1;
    const rt = new THREE.WebGLRenderTarget(Math.round(W * scale), Math.round(H * scale), { type: THREE.HalfFloatType, samples: S.msaa, generateMipmaps: true, minFilter: THREE.LinearMipmapLinearFilter });
    const mat = new THREE.MeshStandardMaterial(Object.assign({ color: o.color || '#111114', roughness: o.surfaceRoughness == null ? 0.35 : o.surfaceRoughness, metalness: o.metalness || 0 }, o.material || {}));
    const u = { tMirror: { value: rt.texture }, uMirrorMatrix: { value: new THREE.Matrix4() }, uMirrorStrength: { value: o.strength == null ? 0.6 : o.strength }, uMirrorFresnel: { value: o.fresnel === false ? 0 : 1 }, uMirrorFade: { value: o.fade || 0 }, uMirrorBlur: { value: (o.roughness || 0) * 7 } };
    mat.onBeforeCompile = (sh) => {
      Object.assign(sh.uniforms, u);
      sh.vertexShader = sh.vertexShader.replace('#include <common>', '#include <common>\nuniform mat4 uMirrorMatrix; varying vec4 vMirrorCoord; varying vec3 vMirrorWorld;')
        .replace('#include <begin_vertex>', '#include <begin_vertex>\nvec4 mw = modelMatrix * vec4(transformed, 1.0); vMirrorWorld = mw.xyz; vMirrorCoord = uMirrorMatrix * mw;');
      sh.fragmentShader = sh.fragmentShader.replace('#include <common>', '#include <common>\nuniform sampler2D tMirror; uniform float uMirrorStrength, uMirrorFresnel, uMirrorFade, uMirrorBlur; varying vec4 vMirrorCoord; varying vec3 vMirrorWorld;')
        .replace('#include <opaque_fragment>', `vec3 mRefl = min(texture2DProj(tMirror, vMirrorCoord, uMirrorBlur).rgb, vec3(12.0));
          float mF = pow(1.0 - saturate(dot(normalize(vViewPosition), normal)), 5.0);
          float mK = uMirrorStrength * mix(1.0, 0.06 + 0.94 * mF, uMirrorFresnel);
          if (uMirrorFade > 0.0) mK *= exp(-length(vMirrorWorld.xz) / uMirrorFade);
          outgoingLight += mRefl * mK;
          #include <opaque_fragment>`);
    };
    const mesh = new THREE.Mesh(new THREE.PlaneGeometry(o.size || 80, o.size || 80), mat);
    mesh.rotation.x = -Math.PI / 2; mesh.position.y = y; mesh.receiveShadow = true;
    S.scene.add(mesh);
    S.mirrors.push({ mesh, rt, u, y, rough: o.roughness || 0, cam: new THREE.PerspectiveCamera() });
    return mesh;
  };

  // ---------------------------------------------------------------- the stage
  const QUAD_VS = 'varying vec2 vUv; void main(){ vUv = uv; gl_Position = vec4(position.xy, 0.0, 1.0); }';
  const TONE = `
    vec3 tmFilmic(vec3 c){ // ACES fitted (Stephen Hill)
      mat3 a = mat3(0.59719,0.07600,0.02840, 0.35458,0.90834,0.13383, 0.04823,0.01566,0.83777);
      mat3 b = mat3(1.60475,-0.10208,-0.00327, -0.53108,1.10813,-0.07276, -0.07367,-0.00605,1.07602);
      c = a * (c / 0.6); vec3 x = c * (c + 0.0245786) - 0.000090537; vec3 y = c * (0.983729 * c + 0.4329510) + 0.238081;
      return clamp(b * (x / y), 0.0, 1.0);
    }
    vec3 tmNeutral(vec3 c){ // Khronos PBR neutral: keeps colours true, only compresses highlights
      float x = min(c.r, min(c.g, c.b)); float off = x < 0.08 ? x - 6.25 * x * x : 0.04; c -= off;
      float peak = max(c.r, max(c.g, c.b)); if (peak < 0.76) return c;
      float d = 1.0 - 0.76; float np = 1.0 - d * d / (peak + d - 0.76); c *= np / peak;
      float g = 1.0 - 1.0 / (0.15 * (peak - np) + 1.0); return mix(c, vec3(np), g);
    }
    vec3 tmAgx(vec3 c){ // AgX (minimal, Benjamin Wrensch): soft, filmic, desaturates bright colours gracefully
      mat3 m = mat3(0.842479062253094,0.0423282422610123,0.0423756549057051, 0.0784335999999992,0.878468636469772,0.0784336, 0.0792237451477643,0.0791661274605434,0.879142973793104);
      mat3 mi = mat3(1.19687900512017,-0.0528968517574562,-0.0529716355144438, -0.0980208811401368,1.15190312990417,-0.0980434501171241, -0.0990297440797205,-0.0989611768448433,1.15107367264116);
      c = m * c; c = clamp((log2(max(c, 1e-10)) + 12.47393) / 16.5, 0.0, 1.0);
      vec3 x2 = c * c, x4 = x2 * x2;
      c = 15.5 * x4 * x2 - 40.14 * x4 * c + 31.96 * x4 - 6.868 * x2 * c + 0.4298 * x2 + 0.1191 * c - 0.00232;
      c = mi * c; return pow(max(c, 0.0), vec3(2.2));
    }`;

  function pass(frag, uniforms) {
    return new THREE.ShaderMaterial({ vertexShader: QUAD_VS, fragmentShader: 'precision highp float; varying vec2 vUv;\n' + frag, uniforms, depthTest: false, depthWrite: false });
  }

  // createStage(canvas, style, { samples }) -> S. S.seek(t) renders one finished frame.
  K.createStage = (canvas, style, opts = {}) => {
    const R = Object.assign({ samples: 32, preview: 3, shutter: 0.5, fps: 30, exposure: 1, tone: 'filmic', bloom: 0.25, bloomThreshold: 1.0, bloomRadius: 1, vignette: 0.25, grain: 0.03, ca: 0, saturation: 1, contrast: 1, lift: 0, fov: 35, near: 0.1, far: 200, background: '#000000', shadows: true }, style.render || {});
    const W = opts.width || 1920, H = opts.height || 1080;
    const N = Math.max(1, opts.samples || R.samples);
    const renderer = new THREE.WebGLRenderer({ canvas, antialias: false, alpha: false, preserveDrawingBuffer: true, powerPreference: 'high-performance' });
    renderer.setPixelRatio(1); renderer.setSize(W, H, false);
    renderer.toneMapping = THREE.NoToneMapping;
    renderer.shadowMap.enabled = !!R.shadows; renderer.shadowMap.type = R.shadowType === 'vsm' ? THREE.VSMShadowMap : THREE.PCFShadowMap;
    const scene = new THREE.Scene(); scene.background = new THREE.Color(R.background);
    const camera = new THREE.PerspectiveCamera(R.fov, W / H, R.near, R.far);
    camera.position.set(0, 1.5, 8); camera.userData.aperture = 0; camera.userData.focus = 8;
    const msaa = N >= 8 ? 0 : 4;
    const S = { THREE, K, renderer, scene, camera, W, H, style, R, samples: N, msaa, softLights: [], skyLights: [], mirrors: [], duration: style.duration || 6 };

    const HF = { type: THREE.HalfFloatType, depthBuffer: false };
    const sceneRT = new THREE.WebGLRenderTarget(W, H, { type: THREE.HalfFloatType, samples: msaa });
    const accRT = new THREE.WebGLRenderTarget(W, H, HF);
    const quad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2));
    const qScene = new THREE.Scene(); qScene.add(quad);
    const qCam = new THREE.Camera();
    const draw = (mat, target) => { quad.material = mat; renderer.setRenderTarget(target); renderer.render(qScene, qCam); };

    const mAcc = pass('uniform sampler2D tex; uniform float w; void main(){ gl_FragColor = vec4(min(texture2D(tex, vUv).rgb, vec3(64.0)) * w, w); }', { tex: { value: sceneRT.texture }, w: { value: 1 } });
    mAcc.blending = THREE.CustomBlending; mAcc.blendEquation = THREE.AddEquation; mAcc.blendSrc = THREE.OneFactor; mAcc.blendDst = THREE.OneFactor; mAcc.transparent = true;

    // bloom: threshold, then a chain of down-samples and smooth up-samples (wide, soft, no blockiness)
    const LV = 7, bl = [];
    for (let i = 0, w = W >> 1, h = H >> 1; i < LV && w > 4 && h > 4; i++, w >>= 1, h >>= 1) bl.push({ d: new THREE.WebGLRenderTarget(w, h, HF), u: new THREE.WebGLRenderTarget(w, h, HF), w, h });
    const mPre = pass('uniform sampler2D tex; uniform float thr; void main(){ vec3 c = min(texture2D(tex, vUv).rgb, vec3(60.0)); float l = max(c.r, max(c.g, c.b)); float k = clamp((l - thr) / max(thr * 0.5, 1e-3), 0.0, 1.0); gl_FragColor = vec4(c * k * k * (3.0 - 2.0 * k), 1.0); }', { tex: { value: accRT.texture }, thr: { value: R.bloomThreshold } });
    const mDown = pass(`uniform sampler2D tex; uniform vec2 px;
      void main(){ vec3 a = texture2D(tex, vUv + px * vec2(-2, 2)).rgb, b = texture2D(tex, vUv + px * vec2(0, 2)).rgb, c = texture2D(tex, vUv + px * vec2(2, 2)).rgb,
        d = texture2D(tex, vUv + px * vec2(-2, 0)).rgb, e = texture2D(tex, vUv).rgb, f = texture2D(tex, vUv + px * vec2(2, 0)).rgb,
        g = texture2D(tex, vUv + px * vec2(-2, -2)).rgb, h = texture2D(tex, vUv + px * vec2(0, -2)).rgb, i = texture2D(tex, vUv + px * vec2(2, -2)).rgb,
        j = texture2D(tex, vUv + px * vec2(-1, 1)).rgb, k = texture2D(tex, vUv + px * vec2(1, 1)).rgb, l = texture2D(tex, vUv + px * vec2(-1, -1)).rgb, m = texture2D(tex, vUv + px * vec2(1, -1)).rgb;
        gl_FragColor = vec4(e * 0.125 + (a + c + g + i) * 0.03125 + (b + d + f + h) * 0.0625 + (j + k + l + m) * 0.125, 1.0); }`, { tex: { value: null }, px: { value: new THREE.Vector2() } });
    const mUp = pass(`uniform sampler2D tex, add; uniform vec2 px; uniform float r;
      void main(){ vec2 o = px * r; vec3 s = texture2D(tex, vUv).rgb * 4.0 + (texture2D(tex, vUv + vec2(o.x, 0)).rgb + texture2D(tex, vUv - vec2(o.x, 0)).rgb + texture2D(tex, vUv + vec2(0, o.y)).rgb + texture2D(tex, vUv - vec2(0, o.y)).rgb) * 2.0
        + texture2D(tex, vUv + o).rgb + texture2D(tex, vUv - o).rgb + texture2D(tex, vUv + vec2(o.x, -o.y)).rgb + texture2D(tex, vUv + vec2(-o.x, o.y)).rgb;
        gl_FragColor = vec4(s / 16.0 + texture2D(add, vUv).rgb, 1.0); }`, { tex: { value: null }, add: { value: null }, px: { value: new THREE.Vector2() }, r: { value: R.bloomRadius } });

    const ovCanvas = document.createElement('canvas'); ovCanvas.width = W; ovCanvas.height = H;
    const ovCtx = ovCanvas.getContext('2d');
    const ovTex = new THREE.CanvasTexture(ovCanvas); ovTex.colorSpace = THREE.NoColorSpace; ovTex.premultiplyAlpha = true; ovTex.generateMipmaps = false; ovTex.minFilter = THREE.LinearFilter;
    const mOut = pass(TONE + `
      uniform sampler2D tex, bloom; uniform float exposure, bloomK, vig, ca, sat, con, lift, levels; uniform int tone; uniform vec2 res;
      void main(){
        vec2 d = vUv - 0.5; float r2 = dot(d, d);
        vec3 c;
        if (ca > 0.0) { c.r = texture2D(tex, 0.5 + d * (1.0 + ca * r2)).r; c.g = texture2D(tex, vUv).g; c.b = texture2D(tex, 0.5 + d * (1.0 - ca * r2)).b; }
        else c = texture2D(tex, vUv).rgb;
        c += texture2D(bloom, vUv).rgb * bloomK / levels;
        c = max(c * exposure, 0.0);
        c = tone == 0 ? tmFilmic(c) : tone == 1 ? tmNeutral(c) : tone == 2 ? tmAgx(c) : clamp(c, 0.0, 1.0);
        float l = dot(c, vec3(0.2126, 0.7152, 0.0722));
        c = mix(vec3(l), c, sat); c = (c - 0.5) * con + 0.5; c = c + lift * (1.0 - c);
        c *= 1.0 - vig * smoothstep(0.22, 0.8, length(d * vec2(1.0, 0.82)));
        c = clamp(c, 0.0, 1.0);
        c = mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, step(0.0031308, c));
        gl_FragColor = vec4(c, 1.0);
      }`, { tex: { value: accRT.texture }, bloom: { value: null }, exposure: { value: R.exposure }, bloomK: { value: R.bloom }, vig: { value: R.vignette }, ca: { value: R.ca }, sat: { value: R.saturation }, con: { value: R.contrast }, lift: { value: R.lift }, levels: { value: 1 }, tone: { value: { filmic: 0, neutral: 1, agx: 2, linear: 3 }[R.tone] ?? 0 }, res: { value: new THREE.Vector2(W, H) } });

    // after grading the picture is 8-bit display colour: optional style filters run here (painterly, dither, halftone...),
    // then grain and the 2D overlay go on top
    const LD = { depthBuffer: false, minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter };
    const ldA = new THREE.WebGLRenderTarget(W, H, LD), ldB = new THREE.WebGLRenderTarget(W, H, LD);
    const mFin = pass(`uniform sampler2D tex, ov; uniform float grain, seed, useOv;
      float h21(vec2 p){ p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 45.32); return fract(p.x * p.y); }
      void main(){ vec3 c = texture2D(tex, vUv).rgb; float l = dot(c, vec3(0.2126, 0.7152, 0.0722));
        float g = (h21(gl_FragCoord.xy + seed) + h21(gl_FragCoord.xy * 1.37 + seed * 1.7) - 1.0);
        c += g * grain * (0.55 + 0.45 * (1.0 - l));
        if (useOv > 0.5) { vec4 o = texture2D(ov, vUv); c = o.rgb + c * (1.0 - o.a); }
        gl_FragColor = vec4(c, 1.0); }`, { tex: { value: ldA.texture }, ov: { value: ovTex }, grain: { value: R.grain }, seed: { value: 0 }, useOv: { value: 0 } });
    const filters = [];
    // S.addFilter(fragmentShaderBody, uniforms): a full-screen pass over the graded picture. The shader gets
    // `uniform sampler2D tex; uniform vec2 res; uniform float time; varying vec2 vUv;` and must set gl_FragColor.
    S.addFilter = (frag, uniforms = {}) => { const m = pass('uniform sampler2D tex; uniform vec2 res; uniform float time;\n' + frag, Object.assign({ tex: { value: null }, res: { value: new THREE.Vector2(W, H) }, time: { value: 0 } }, uniforms)); filters.push(m); return m; };

    let api = null;
    const cp = new THREE.Vector3(), right = new THREE.Vector3(), up = new THREE.Vector3(), tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(), nrm = new THREE.Vector3();
    const rot = new THREE.Matrix4(), plane = new THREE.Plane(), clip = new THREE.Vector4(), q = new THREE.Vector4(), saved = [];

    function renderMirrors(i) {
      for (const m of S.mirrors) {
        const cam = m.cam;
        // roughness: tilt the mirror plane a little differently on each sub-render, which blurs the reflection
        const a = m.rough * 0.12, th = 2 * Math.PI * halton(i + 1, 7), rr = Math.sqrt(halton(i + 1, 11)) * a;
        nrm.set(Math.cos(th) * rr, 1, Math.sin(th) * rr).normalize();
        const origin = tmp2.set(0, m.y, 0);
        camera.getWorldPosition(cp);
        const view = tmp.copy(origin).sub(cp);
        if (view.dot(nrm) > 0) continue;
        view.reflect(nrm).negate().add(origin);
        rot.extractRotation(camera.matrixWorld);
        const look = new THREE.Vector3(0, 0, -1).applyMatrix4(rot).add(cp);
        const target = origin.clone().sub(look).reflect(nrm).negate().add(origin);
        cam.position.copy(view); cam.up.set(0, 1, 0).applyMatrix4(rot).reflect(nrm); cam.lookAt(target);
        cam.near = camera.near; cam.far = camera.far; cam.updateMatrixWorld(); cam.projectionMatrix.copy(camera.projectionMatrix);
        m.u.uMirrorMatrix.value.set(0.5, 0, 0, 0.5, 0, 0.5, 0, 0.5, 0, 0, 0.5, 0.5, 0, 0, 0, 1).multiply(cam.projectionMatrix).multiply(cam.matrixWorldInverse);
        // clip everything below the mirror (oblique near plane)
        plane.setFromNormalAndCoplanarPoint(nrm, origin).applyMatrix4(cam.matrixWorldInverse);
        clip.set(plane.normal.x, plane.normal.y, plane.normal.z, plane.constant);
        const pm = cam.projectionMatrix.elements;
        q.x = (Math.sign(clip.x) + pm[8]) / pm[0]; q.y = (Math.sign(clip.y) + pm[9]) / pm[5]; q.z = -1; q.w = (1 + pm[10]) / pm[14];
        clip.multiplyScalar(2 / clip.dot(q));
        pm[2] = clip.x; pm[6] = clip.y; pm[10] = clip.z + 1 - 0.003; pm[14] = clip.w;
        m.mesh.visible = false;
        renderer.setRenderTarget(m.rt); renderer.render(scene, cam);
        m.mesh.visible = true;
      }
    }

    function subRender(t, i, n) {
      api.update(t);
      saved.length = 0;
      // lights with a size: sample a different point on the light each sub-render
      S.softLights.forEach((s, k) => {
        const l = s.light; saved.push([l, l.position.clone()]);
        if (n === 1) return;
        const u = (halton(i + 1, 3) + k * 0.37) % 1, v = (halton(i + 1, 5) + k * 0.61) % 1, r = Math.sqrt(u) * s.radius, a = 2 * Math.PI * v;
        tmp.copy(l.position).sub(l.target ? l.target.position : tmp2.set(0, 0, 0)).normalize();
        right.set(0, 1, 0).cross(tmp); if (right.lengthSq() < 1e-6) right.set(1, 0, 0); right.normalize(); up.crossVectors(tmp, right);
        l.position.addScaledVector(right, Math.cos(a) * r).addScaledVector(up, Math.sin(a) * r);
      });
      S.skyLights.forEach((s, k) => {
        const u = (halton(i + 1, 2) + k * 0.31) % 1, v = (halton(i + 1, 3) + k * 0.53) % 1;
        const sinT = Math.sqrt(u) * s.spread, cosT = Math.sqrt(Math.max(0, 1 - sinT * sinT)), a = 2 * Math.PI * v;
        const c = s.light.target.position;
        if (n === 1) s.light.position.set(c.x + 0.15 * s.dist, c.y + s.dist, c.z + 0.1 * s.dist);
        else s.light.position.set(c.x + Math.cos(a) * sinT * s.dist, c.y + cosT * s.dist, c.z + Math.sin(a) * sinT * s.dist);
      });
      // lens: move the camera across the aperture and shear the view so the focus plane stays put (true depth of field)
      camera.updateMatrixWorld(); camera.updateProjectionMatrix();
      const camPos = camera.position.clone(), e = camera.projectionMatrix.elements;
      if (n > 1) {
        const ap = camera.userData.aperture || 0, fd = Math.max(0.05, camera.userData.focus || 8);
        if (ap > 0) {
          const r = Math.sqrt(halton(i + 1, 2)) * ap, a = 2 * Math.PI * halton(i + 1, 3), dx = Math.cos(a) * r, dy = Math.sin(a) * r;
          right.setFromMatrixColumn(camera.matrixWorld, 0); up.setFromMatrixColumn(camera.matrixWorld, 1);
          camera.position.addScaledVector(right, dx).addScaledVector(up, dy); camera.updateMatrixWorld();
          e[8] -= e[0] * dx / fd; e[9] -= e[5] * dy / fd;
        }
        e[8] += (halton(i + 1, 5) - 0.5) * 2 / W; e[9] += (halton(i + 1, 7) - 0.5) * 2 / H;
        camera.projectionMatrixInverse.copy(camera.projectionMatrix).invert();
      }
      if (S.mirrors.length) renderMirrors(i);
      renderer.setRenderTarget(sceneRT); renderer.render(scene, camera);
      camera.position.copy(camPos); camera.updateMatrixWorld(); camera.updateProjectionMatrix();
      for (const [l, p] of saved) l.position.copy(p);
    }

    S.seek = (t, sampleOverride) => {
      const n = sampleOverride || S.samples;
      renderer.setRenderTarget(accRT); renderer.setClearColor(0x000000, 0); renderer.clear();
      mAcc.uniforms.w.value = 1 / n;
      for (let i = 0; i < n; i++) {
        const ts = n > 1 ? t + ((i + 0.5) / n - 0.5) * R.shutter / R.fps : t;
        subRender(K.clamp(ts, 0, S.duration), i, n);
        renderer.autoClear = false; draw(mAcc, accRT); renderer.autoClear = true;
      }
      api.update(t); // leave the scene at the exact frame time (for the overlay and anything that reads it)
      let bloomTex = null;
      if (R.bloom > 0) {
        mPre.uniforms.thr.value = R.bloomThreshold; draw(mPre, bl[0].d);
        for (let i = 1; i < bl.length; i++) { mDown.uniforms.tex.value = bl[i - 1].d.texture; mDown.uniforms.px.value.set(1 / bl[i - 1].w, 1 / bl[i - 1].h); draw(mDown, bl[i].d); }
        let prev = bl[bl.length - 1].d;
        for (let i = bl.length - 2; i >= 0; i--) { mUp.uniforms.tex.value = prev.texture; mUp.uniforms.add.value = bl[i].d.texture; mUp.uniforms.px.value.set(1 / prev.width, 1 / prev.height); draw(mUp, bl[i].u); prev = bl[i].u; }
        bloomTex = prev.texture;
      }
      const o = mOut.uniforms;
      o.bloom.value = bloomTex; o.bloomK.value = bloomTex ? R.bloom : 0; o.levels.value = bl.length; o.exposure.value = R.exposure; o.vig.value = R.vignette; o.ca.value = R.ca; o.sat.value = R.saturation; o.con.value = R.contrast; o.lift.value = R.lift;
      draw(mOut, ldA);
      let src = ldA, dst = ldB;
      for (const f of filters) { f.uniforms.tex.value = src.texture; f.uniforms.time.value = t; draw(f, dst); [src, dst] = [dst, src]; }
      const fu = mFin.uniforms;
      fu.tex.value = src.texture; fu.grain.value = R.grain; fu.seed.value = Math.round(t * R.fps) * 17.0 % 1000;
      if (api.overlay) { ovCtx.clearRect(0, 0, W, H); ovCtx.save(); api.overlay(ovCtx, t, W, H); ovCtx.restore(); ovTex.needsUpdate = true; fu.useOv.value = 1; } else fu.useOv.value = 0;
      draw(mFin, null);
    };
    S.setApi = (a) => { api = typeof a === 'function' ? { update: a } : a; };
    return S;
  };

  // Load fonts, build the style and return its stage. `K.run(canvas, style, { samples })`.
  K.run = async (canvas, style, opts) => {
    if (style.fonts && document.fonts) await Promise.all(style.fonts.map((f) => document.fonts.load(f).catch(() => null)));
    const S = K.createStage(canvas, style, opts);
    S.setApi(await style.build(S));
    return S;
  };

  root.K = K;
})(typeof window !== 'undefined' ? window : globalThis);
```

SOUND KIT (lib/sound.js)
```js
// sound.js: a small synthesized sound kit for the style explainers. Plain Web Audio, no audio files.
// (play(..., { minor: true }), tone/noise { minor: true }, or scheduled inside k.withMinor(() => ...)) at about 60%
// volume instead of full, and sits all effects a little lower, so the narration stays on top while the piece still
// sounds alive. In the full mix, minor sounds play at full volume. Scores can read k.vo to branch further.
// A style adds   explainerSound(k) { k.bed('room', 0, D); k.play('pop', 1.25); ... }   and the same
// call schedules into a live AudioContext (the player) or an OfflineAudioContext (the video render).
// Times are seconds on the explainer's own clock. Randomness is seeded, so every render is identical.
(function () {
  'use strict';
  const SOUND = (window.SOUND = {});
  const TINY = 1e-4;

  // seeded random in [0,1), keyed by any string
  function rand(key) {
    let h = 2166136261;
    for (let i = 0; i < key.length; i++) { h ^= key.charCodeAt(i); h = Math.imul(h, 16777619); }
    let a = h >>> 0 || 1;
    return () => { a = (a + 0x6d2b79f5) | 0; let t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
  }

  // note names to Hz: 'A4' = 440, 'C#5', 'Eb3'
  const STEP = { C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11 };
  function hz(n) {
    if (typeof n === 'number') return n;
    const m = /^([A-G])([#b]?)(-?\d)$/.exec(n);
    const semis = STEP[m[1]] + (m[2] === '#' ? 1 : m[2] === 'b' ? -1 : 0) + (Number(m[3]) - 4) * 12 - 9;
    return 440 * Math.pow(2, semis / 12);
  }
  SOUND.hz = hz;

  SOUND.kit = function (ctx, opts) {
    const o = opts || {};
    const VO = !!o.voiceover;
    const MINOR = 0.6; // voiceover-mode volume for minor sounds
    let inMinor = false;
    const gainOf = (p, d) => (p.gain == null ? d : p.gain) * (VO && (p.minor || inMinor) ? MINOR : 1);
    const T0 = (o.start || 0) - 0; // explainer time that maps to ctx time `base`
    const base = ctx.currentTime + (o.delay || 0);
    // Start times snap to the sample grid (+ a quarter sample): in Chrome a source started a hair off the grid can
    // spike to about twice its set level.
    const SR = ctx.sampleRate;
    const at = (t) => (Math.round((base + (t - T0)) * SR) + 0.25) / SR;

    // ---- effect and ambience buses feed the master output
    const master = ctx.createGain(); master.gain.value = o.master == null ? 0.9 : o.master;
    const comp = ctx.createDynamicsCompressor();
    comp.threshold.value = -16; comp.knee.value = 8; comp.ratio.value = 3; comp.attack.value = 0.004; comp.release.value = 0.18;
    comp.connect(master); master.connect(o.dest || ctx.destination);
    const bus = {};
    for (const [name, g] of Object.entries({ sfx: VO ? 0.7 : 0.85, amb: 0.5 })) { bus[name] = ctx.createGain(); bus[name].gain.value = g; bus[name].connect(comp); }

    // generated room reverb: decaying stereo noise, darker toward the tail
    const verb = ctx.createConvolver();
    {
      const secs = 1.6, n = Math.floor(secs * ctx.sampleRate), ir = ctx.createBuffer(2, n, ctx.sampleRate);
      for (let c = 0; c < 2; c++) {
        const r = rand('verb' + c), d = ir.getChannelData(c); let lp = 0;
        for (let i = 0; i < n; i++) { const u = i / n; lp += (0.6 - 0.45 * u) * ((r() * 2 - 1) - lp); d[i] = lp * Math.pow(1 - u, 3); }
      }
      verb.buffer = ir;
    }
    const verbIn = ctx.createGain(); verbIn.gain.value = 0.9; verbIn.connect(verb); verb.connect(comp);

    // seeded noise buffers: white, pink-ish and brown
    const noiseBuf = {};
    {
      const n = Math.floor(2.5 * ctx.sampleRate);
      for (const kind of ['white', 'pink', 'brown']) {
        const b = ctx.createBuffer(1, n, ctx.sampleRate), d = b.getChannelData(0), r = rand('noise-' + kind);
        let p0 = 0, p1 = 0, p2 = 0, br = 0;
        for (let i = 0; i < n; i++) {
          const w = r() * 2 - 1;
          if (kind === 'white') d[i] = w;
          else if (kind === 'pink') { p0 = 0.997 * p0 + 0.029 * w; p1 = 0.985 * p1 + 0.032 * w; p2 = 0.95 * p2 + 0.048 * w; d[i] = (p0 + p1 + p2 + w * 0.05) * 2.2; }
          else { br = (br + 0.02 * w) / 1.02; d[i] = br * 3.2; }
        }
        noiseBuf[kind] = b;
      }
    }

    // ---- automation helpers
    // a value or [[dt, value], ...] from the voice start; ramps are exponential (frequencies)
    function sweep(param, v, t) {
      if (!Array.isArray(v)) { param.setValueAtTime(v, t); return; }
      param.setValueAtTime(v[0][1], t + v[0][0]);
      for (let i = 1; i < v.length; i++) param.exponentialRampToValueAtTime(Math.max(v[i][1], 1), t + v[i][0]);
    }
    // amplitude: linear attack to `gain`, optional hold, exponential release to silence by `dur`
    function shape(param, t, gain, dur, a, hold) {
      a = Math.min(a == null ? 0.004 : a, dur * 0.9);
      hold = Math.min(hold || 0, dur - a);
      param.setValueAtTime(0, t);
      param.linearRampToValueAtTime(gain, t + a);
      if (hold > 0) param.setValueAtTime(gain, t + a + hold);
      param.exponentialRampToValueAtTime(TINY, t + dur);
      param.setValueAtTime(0, t + dur + 0.001);
    }
    function out(node, p) {
      const pan = ctx.createStereoPanner(); pan.pan.value = Math.max(-1, Math.min(1, p.pan || 0));
      node.connect(pan); pan.connect(bus[p.bus || 'sfx']);
      if (p.verb) { const s = ctx.createGain(); s.gain.value = p.verb; pan.connect(s); s.connect(verbIn); }
    }
    function filterNode(p, t) {
      if (!p.filter) return null;
      const f = ctx.createBiquadFilter(); f.type = p.filter; f.Q.value = p.q == null ? 1 : p.q;
      sweep(f.frequency, p.f == null ? 1000 : p.f, t);
      return f;
    }

    // Live playback from the middle of a piece: a long sound (a bed, a pad) that began before that point has a start
    // time in the past, which Web Audio refuses. Pick it up now, at its held level, for the time it has left.
    function late(t, dur, p) {
      if (t >= ctx.currentTime) return null;   // never true in an offline render, so rendered sound is unchanged
      const now = ctx.currentTime + 0.01;
      const cut = now - t, left = dur - cut;
      if (left < 0.08) return { skip: true };
      const a = Math.min(0.05, left / 2);
      return { t: now, dur: left, p: Object.assign({}, p, { a, hold: Math.max(0, Math.min(left - a - 0.02, (p.a == null ? 0.004 : p.a) + (p.hold || 0) - cut)) }) };
    }

    // ---- primitives
    // tone(t, { f, type, dur, gain, a, hold, pan, verb, bus, filter, f2 (filter freq), q, detune })
    function tone(time, p) {
      let t = at(time), dur = p.dur || 0.2;
      if (t + dur < ctx.currentTime || (time < T0 - 0.001 && dur < 1.5)) return;
      const lt = late(t, dur, p); if (lt) { if (lt.skip) return; t = lt.t; dur = lt.dur; p = lt.p; }
      const osc = ctx.createOscillator(); osc.type = p.type || 'sine';
      sweep(osc.frequency, Array.isArray(p.f) ? p.f.map(([d, v]) => [d, hz(v)]) : hz(p.f || 440), t);
      if (p.detune) osc.detune.value = p.detune;
      const g = ctx.createGain(); shape(g.gain, t, gainOf(p, 0.3), dur, p.a, p.hold);
      let node = osc;
      if (p.filter) { const f = ctx.createBiquadFilter(); f.type = p.filter; f.Q.value = p.q == null ? 0.8 : p.q; sweep(f.frequency, p.f2 || 2000, t); osc.connect(f); node = f; }
      node.connect(g); out(g, p);
      osc.start(t); osc.stop(t + dur + 0.02);
    }
    // noise(t, { dur, color, filter, f, q, gain, a, hold, pan, verb, bus, rate })
    function noise(time, p) {
      let t = at(time), dur = p.dur || 0.2;
      if (t + dur < ctx.currentTime || (time < T0 - 0.001 && dur < 1.5)) return;
      const lt = late(t, dur, p); if (lt) { if (lt.skip) return; t = lt.t; dur = lt.dur; p = lt.p; }
      const src = ctx.createBufferSource(); src.buffer = noiseBuf[p.color || 'white']; src.loop = true;
      if (p.rate) src.playbackRate.value = p.rate;
      const g = ctx.createGain(); shape(g.gain, t, gainOf(p, 0.3), dur, p.a, p.hold);
      const f = filterNode(p, t);
      if (f) { src.connect(f); f.connect(g); } else src.connect(g);
      out(g, p);
      const off = rand('off' + time + (p.f || ''))() * 2;
      src.start(t, off); src.stop(t + dur + 0.02);
    }

    // ---- named sounds. play(name, t, opts): opts.gain scales the whole sound, opts.pan places it.
    const S = {
      // UI / motion graphics
      pop: (t, o) => { tone(t, { f: [[0, o.f || 620], [0.07, (o.f || 620) * 0.45]], dur: 0.1, gain: 0.32 * o.g, pan: o.pan }); noise(t, { dur: 0.012, filter: 'highpass', f: 3000, gain: 0.12 * o.g, pan: o.pan }); },
      tick: (t, o) => noise(t, { dur: 0.018, filter: 'bandpass', f: o.f || 4200, q: 2.5, gain: 0.3 * o.g, pan: o.pan }),
      click: (t, o) => { tone(t, { f: o.f || 1900, type: 'square', dur: 0.02, gain: 0.08 * o.g, filter: 'lowpass', f2: 3000, pan: o.pan }); noise(t, { dur: 0.01, filter: 'highpass', f: 2500, gain: 0.15 * o.g, pan: o.pan }); },
      tock: (t, o) => { tone(t, { f: o.f || 820, dur: 0.07, gain: 0.25 * o.g, pan: o.pan }); noise(t, { dur: 0.05, filter: 'bandpass', f: o.f || 820, q: 9, gain: 0.35 * o.g, pan: o.pan }); },
      blip: (t, o) => tone(t, { f: o.f || 'E6', type: o.type || 'square', dur: o.dur || 0.07, gain: 0.07 * o.g, a: 0.002, hold: (o.dur || 0.07) * 0.6, pan: o.pan }),
      whoosh: (t, o) => { const d = o.dur || 0.45; noise(t, { dur: d, color: 'pink', filter: 'bandpass', f: [[0, 350], [d * 0.55, o.top || 2600], [d, 700]], q: 0.9, gain: 0.32 * o.g, a: d * 0.5, pan: o.pan, verb: 0.15 }); },
      swish: (t, o) => { const d = o.dur || 0.18; noise(t, { dur: d, filter: 'highpass', f: [[0, 1800], [d, 5200]], q: 0.7, gain: 0.22 * o.g, a: d * 0.35, pan: o.pan }); },
      riser: (t, o) => { const d = o.dur || 0.8; noise(t, { dur: d, color: 'pink', filter: 'bandpass', f: [[0, 300], [d, 4000]], q: 1.2, gain: 0.25 * o.g, a: d * 0.95, pan: o.pan, verb: 0.2 }); tone(t, { f: [[0, 180], [d, 720]], type: 'triangle', dur: d, gain: 0.06 * o.g, a: d * 0.9 }); },
      thud: (t, o) => { tone(t, { f: [[0, o.f || 130], [0.16, 42]], dur: 0.26, gain: 0.55 * o.g, pan: o.pan }); noise(t, { dur: 0.09, color: 'brown', filter: 'lowpass', f: 500, gain: 0.35 * o.g, pan: o.pan }); },
      impact: (t, o) => { S.thud(t, o); noise(t, { dur: 0.7, color: 'pink', filter: 'lowpass', f: [[0, 2400], [0.6, 300]], gain: 0.3 * o.g, verb: 0.35, pan: o.pan }); },
      stamp: (t, o) => { S.thud(t, { ...o, f: 160 }); noise(t, { dur: 0.05, filter: 'bandpass', f: 1300, q: 1.2, gain: 0.4 * o.g, pan: o.pan }); noise(t + 0.004, { dur: 0.012, filter: 'highpass', f: 3500, gain: 0.25 * o.g, pan: o.pan }); },
      ding: (t, o) => { const f = hz(o.f || 'E6'); tone(t, { f, dur: 1.3, gain: 0.2 * o.g, verb: 0.3, pan: o.pan }); tone(t, { f: f * 2.76, dur: 0.5, gain: 0.05 * o.g, verb: 0.3, pan: o.pan }); },
      success: (t, o) => { S.ding(t, { ...o, f: o.f || 'C6' }); S.ding(t + 0.09, { ...o, f: hz(o.f || 'C6') * 1.5, g: o.g * 0.9 }); },
      error: (t, o) => { tone(t, { f: o.f || 'A3', type: 'square', dur: 0.13, gain: 0.09 * o.g, hold: 0.08, filter: 'lowpass', f2: 1400, pan: o.pan }); tone(t + 0.15, { f: hz(o.f || 'A3') * 0.8, type: 'square', dur: 0.22, gain: 0.09 * o.g, hold: 0.14, filter: 'lowpass', f2: 1200, pan: o.pan }); },
      buzz: (t, o) => { const d = o.dur || 0.3; tone(t, { f: o.f || 110, type: 'sawtooth', dur: d, gain: 0.12 * o.g, hold: d * 0.7, filter: 'lowpass', f2: 900, pan: o.pan }); tone(t, { f: (o.f || 110) * 1.01, type: 'sawtooth', dur: d, gain: 0.1 * o.g, hold: d * 0.7, filter: 'lowpass', f2: 900, pan: o.pan }); },
      sparkle: (t, o) => { const r = rand('sparkle' + t), notes = ['C6', 'D6', 'E6', 'G6', 'A6', 'C7']; for (let i = 0; i < (o.n || 6); i++) { const ti = t + i * 0.04 + r() * 0.01, f = hz(notes[Math.floor(r() * notes.length)]), pan = r() * 1.2 - 0.6; tone(ti, { f, dur: 0.6, gain: 0.04 * o.g, a: 0.002, verb: 0.35, pan }); tone(ti, { f: f * 2.76, dur: 0.2, gain: 0.012 * o.g, a: 0.002, pan }); } },
      glitch: (t, o) => { const r = rand('glitch' + t); for (let i = 0; i < 7; i++) tone(t + i * 0.022, { f: 200 + r() * 1800, type: 'square', dur: 0.02, gain: 0.07 * o.g, a: 0.001, hold: 0.015, pan: r() * 1.4 - 0.7 }); noise(t, { dur: 0.16, filter: 'bandpass', f: 2500, q: 0.6, gain: 0.12 * o.g }); },
      shutter: (t, o) => { S.click(t, o); noise(t + 0.06, { dur: 0.04, filter: 'bandpass', f: 2200, q: 1, gain: 0.2 * o.g, pan: o.pan }); },
      count: (t, o) => S.tick(t, { ...o, f: o.f || 5200, g: o.g * 0.7 }),

      // hand-made media
      // stroke: a pen/marker/chalk/pencil stroke over `dur` seconds. material sets the surface sound.
      stroke: (t, o) => {
        const d = o.dur || 0.4, m = o.material || 'marker', r = rand('stroke' + m + t);
        const M = {
          marker: { f: 2900, q: 2.2, g: 0.16, grain: 0.05 },
          chalk: { f: 1700, q: 0.6, g: 0.22, grain: 0.028 },
          pencil: { f: 4600, q: 0.8, g: 0.16, grain: 0.04, filter: 'highpass' },
          pen: { f: 4200, q: 1.6, g: 0.08, grain: 0.06 },
          brush: { f: 900, q: 0.5, g: 0.1, grain: 0.09, color: 'pink' },
          sharpie: { f: 2400, q: 1.4, g: 0.2, grain: 0.045 },
        }[m];
        for (let x = 0; x < d; x += M.grain * (0.7 + r() * 0.6)) {
          const gd = M.grain * (0.8 + r() * 0.9);
          noise(t + x, { dur: gd, color: M.color, filter: M.filter || 'bandpass', f: M.f * (0.8 + r() * 0.4), q: M.q, gain: M.g * o.g * (0.6 + r() * 0.5), a: gd * 0.3, pan: (o.pan || 0) + (r() - 0.5) * 0.2 });
        }
      },
      // paper: picking up / slapping down / turning a sheet
      paper: (t, o) => { const d = o.dur || 0.35, r = rand('paper' + t); for (let x = 0; x < d; x += 0.025 + r() * 0.03) noise(t + x, { dur: 0.05, filter: 'bandpass', f: 1200 + r() * 2600, q: 0.9, gain: 0.12 * o.g * (1 - x / d * 0.5), pan: o.pan }); },
      slap: (t, o) => { noise(t, { dur: 0.07, filter: 'highpass', f: 900, gain: 0.45 * o.g, a: 0.001, pan: o.pan }); tone(t, { f: [[0, 180], [0.06, 70]], dur: 0.1, gain: 0.25 * o.g, pan: o.pan }); },
      tape: (t, o) => { noise(t, { dur: o.dur || 0.5, filter: 'bandpass', f: [[0, 900], [o.dur || 0.5, 2200]], q: 1.8, gain: 0.18 * o.g, a: 0.02, pan: o.pan }); },
      press: (t, o) => { S.thud(t, { ...o, f: 95, g: o.g * 0.9 }); noise(t + 0.01, { dur: 0.25, color: 'brown', filter: 'lowpass', f: 700, gain: 0.25 * o.g, pan: o.pan }); },
      roller: (t, o) => { const d = o.dur || 0.6; noise(t, { dur: d, color: 'brown', filter: 'bandpass', f: 380, q: 1.5, gain: 0.3 * o.g, a: 0.05, hold: d * 0.6, pan: o.pan }); for (let x = 0; x < d; x += 0.07) S.tock(t + x, { f: 300, g: o.g * 0.15, pan: o.pan }); },
      erase: (t, o) => { const d = o.dur || 0.6, r = rand('erase' + t); for (let x = 0; x < d; x += 0.06 + r() * 0.04) noise(t + x, { dur: 0.08, color: 'pink', filter: 'bandpass', f: 700 + r() * 500, q: 0.8, gain: 0.22 * o.g, a: 0.03, pan: (r() - 0.5) * 0.5 }); },

      // machines and screens
      key: (t, o) => { const r = rand('key' + t); noise(t, { dur: 0.025, filter: 'bandpass', f: 2600 + r() * 1200, q: 2, gain: 0.25 * o.g, a: 0.001, pan: o.pan }); tone(t, { f: 160 + r() * 40, dur: 0.04, gain: 0.12 * o.g, pan: o.pan }); },
      typing: (t, o) => { const r = rand('typing' + t), n = o.n || 8, rate = o.rate || 0.09; for (let i = 0; i < n; i++) S.key(t + i * rate * (0.75 + r() * 0.5), { g: o.g * (0.7 + r() * 0.4), pan: o.pan }); },
      typebar: (t, o) => { S.key(t, o); noise(t + 0.008, { dur: 0.03, filter: 'bandpass', f: 1100, q: 4, gain: 0.35 * o.g, pan: o.pan }); },
      bell: (t, o) => S.ding(t, { ...o, f: o.f || 'A6' }),
      boot: (t, o) => { tone(t, { f: [[0, 80], [0.35, 1600]], type: 'sine', dur: 0.5, gain: 0.08 * o.g, a: 0.3 }); S.blip(t + 0.45, { ...o, f: 'C6', dur: 0.12 }); },
      crtOn: (t, o) => { noise(t, { dur: 0.25, filter: 'highpass', f: 2000, gain: 0.2 * o.g, a: 0.001 }); tone(t, { f: 15700, dur: 1.5, gain: 0.02 * o.g, a: 0.1, hold: 1 }); S.thud(t, { ...o, f: 90, g: o.g * 0.5 }); },
      neon: (t, o) => { const d = o.dur || 0.25, r = rand('neon' + t); for (let x = 0; x < d; x += 0.035 + r() * 0.04) tone(t + x, { f: 120, type: 'sawtooth', dur: 0.03, gain: 0.3 * o.g * (0.5 + r() * 0.5), filter: 'bandpass', f2: 1200, q: 1.2, pan: o.pan }); },
      // chip (8-bit console) sounds: raw square/triangle, no filtering
      coin: (t, o) => { tone(t, { f: 'B5', type: 'square', dur: 0.07, gain: 0.07 * o.g, a: 0.001, hold: 0.06 }); tone(t + 0.07, { f: 'E6', type: 'square', dur: 0.3, gain: 0.07 * o.g, a: 0.001, hold: 0.1 }); },
      jump: (t, o) => tone(t, { f: [[0, 260], [0.16, 820]], type: 'square', dur: 0.18, gain: 0.07 * o.g, a: 0.001, hold: 0.12 }),
      hurt: (t, o) => { tone(t, { f: [[0, 600], [0.25, 90]], type: 'square', dur: 0.28, gain: 0.08 * o.g, a: 0.001, hold: 0.2 }); noise(t, { dur: 0.2, filter: 'lowpass', f: 1800, gain: 0.15 * o.g, rate: 0.25 }); },
      powerup: (t, o) => ['C5', 'E5', 'G5', 'C6', 'E6', 'G6', 'C7'].forEach((n, i) => tone(t + i * 0.045, { f: n, type: 'square', dur: 0.06, gain: 0.06 * o.g, a: 0.001, hold: 0.05 })),
      select: (t, o) => { S.blip(t, { ...o, f: 'A5', dur: 0.04 }); S.blip(t + 0.05, { ...o, f: 'E6', dur: 0.05 }); },
      text: (t, o) => { const n = o.n || 10, rate = o.rate || 0.05; for (let i = 0; i < n; i++) tone(t + i * rate, { f: i % 2 ? 'C6' : 'G5', type: 'square', dur: 0.025, gain: 0.04 * o.g, a: 0.001, hold: 0.015 }); },
    };

    function play(name, t, opts) {
      const fn = S[name];
      if (!fn) throw new Error('unknown sound: ' + name);
      const o2 = Object.assign({}, opts); o2.g = o2.gain == null ? 1 : o2.gain;
      if (opts && opts.minor) withMinor(() => fn(t, o2)); else fn(t, o2);
    }

    // everything scheduled inside fn counts as minor (quieter in voiceover mode)
    function withMinor(fn) { const was = inMinor; inMinor = true; try { fn(); } finally { inMinor = was; } }

    // ---- beds: continuous sound under a stretch of time. bed(name, t0, t1, opts)
    function bed(name, t0, t1, p) {
      if (VO) return;
      p = p || {}; const g = p.gain == null ? 1 : p.gain, d = t1 - t0, fade = Math.min(0.4, d / 4);
      const long = (q) => Object.assign({ dur: d, a: fade, hold: d - fade * 2, bus: 'amb' }, q);
      if (name === 'room') noise(t0, long({ color: 'brown', filter: 'lowpass', f: 320, gain: 0.12 * g }));
      else if (name === 'hiss') noise(t0, long({ color: 'pink', filter: 'highpass', f: 5000, gain: 0.02 * g }));
      else if (name === 'hum') { tone(t0, long({ f: 60, gain: 0.05 * g })); tone(t0, long({ f: 120, gain: 0.025 * g })); tone(t0, long({ f: 180, type: 'triangle', gain: 0.012 * g })); }
      else if (name === 'rain') { noise(t0, long({ color: 'pink', filter: 'bandpass', f: 2500, q: 0.4, gain: 0.08 * g })); const r = rand('rain'); for (let x = 0; x < d; x += 0.04 + r() * 0.08) noise(t0 + x, { dur: 0.01, filter: 'highpass', f: 4000, gain: 0.04 * g * r(), bus: 'amb', pan: r() * 1.6 - 0.8 }); }
      else if (name === 'projector') { noise(t0, long({ color: 'pink', filter: 'highpass', f: 3000, gain: 0.03 * g })); for (let x = 0; x < d; x += 1 / 24) S.tock(t0 + x, { f: 700, g: 0.04 * g, pan: 0.3 }); }
      else if (name === 'vinyl') { noise(t0, long({ color: 'pink', filter: 'bandpass', f: 1500, q: 0.5, gain: 0.025 * g })); const r = rand('vinyl'); for (let x = 0; x < d; x += 0.08 + r() * 0.4) noise(t0 + x, { dur: 0.006, filter: 'highpass', f: 2500, gain: 0.08 * g * r(), bus: 'amb' }); }
      else throw new Error('unknown bed: ' + name);
    }

    return { play, bed, tone, noise, withMinor, hz, vo: VO, sounds: Object.keys(S) };
  };

  // Live playback for a player: SOUND.live(scoreFn, fromT, { voiceover, master }) starts the score from explainer time
  // fromT right now and returns stop(). Call stop() on pause or seek, and SOUND.live again on play.
  SOUND.live = function (fn, fromT, opts) {
    const ctx = new (window.AudioContext || window.webkitAudioContext)();
    fn(SOUND.kit(ctx, Object.assign({}, opts, { start: fromT || 0, delay: 0.03 })));
    return () => { try { ctx.close(); } catch (e) {} };
  };

  // Render a style's explainerSound into an OfflineAudioContext; returns interleaved float stereo.
  SOUND.renderOffline = async function (fn, duration, sampleRate, kitOpts) {
    const sr = sampleRate || 48000, len = Math.ceil(duration * sr);
    const ctx = new OfflineAudioContext(2, len, sr);
    fn(SOUND.kit(ctx, kitOpts || {}));
    const buf = await ctx.startRendering();
    const L = buf.getChannelData(0), R = buf.getChannelData(1), inter = new Float32Array(len * 2);
    let peak = 0;
    for (let i = 0; i < len; i++) { inter[2 * i] = L[i]; inter[2 * i + 1] = R[i]; peak = Math.max(peak, Math.abs(L[i]), Math.abs(R[i])); }
    return { inter, peak, frames: len, sr };
  };
})();
```

STROKEFONT (lib/strokefont.js)
```js
/*
 * strokefont.js - a single-stroke (centre-line) rounded sans for tube lettering.
 *
 * Classic script, no dependencies. Sets a global `STROKEFONT` (browser and Node).
 *
 *   STROKEFONT.layout(text, opts) -> { strokes, width, height, glyphs }
 *     opts.size     cap height in output units (1)
 *     opts.spacing  extra gap between letters, fraction of size (0.18)
 *     opts.lineGap  gap between lines, fraction of size (0.45)
 *     opts.align    'left' | 'center' | 'right' ('center'); x = 0 is the left
 *                   edge, the centre or the right edge of the block. The block
 *                   is always centred on y = 0. y is up.
 *     opts.round    corner fillet radius, fraction of size (0.09)
 *     opts.step     max distance between output points, fraction of size (0.03)
 *     strokes[i] = { points: [[x, y], ...], closed, glyph, char }
 *                   points are centre-line samples, evenly spaced by arc length
 *                   between corner tips, corners already filleted. A closed
 *                   stroke does not repeat its first point. `glyph` is the
 *                   index of the character in `text`.
 *     glyphs[i]  = { char, x, y, w, strokeIndexes }  one per character of
 *                   `text` (line breaks included, so glyphs[stroke.glyph]
 *                   works); x, y = left end of the baseline, w = ink width.
 *     width, height = size of the block of centre lines (add the tube width).
 *   STROKEFONT.length(stroke) -> arc length of a stroke (or of a points array)
 *   STROKEFONT.glyphs -> { 'A': { d, w, strokes }, ... }  d = path in the DSL
 *                   below, w = ink width, strokes = number of strokes
 *
 * Characters: A-Z (lowercase maps to uppercase), 0-9, . , ! ? - + : ' / & % # @ =
 * and space. Anything else leaves a space.
 *
 * Glyphs live on a grid where the cap height is 1 and the baseline is y = 0
 * (y up). Every glyph is a few centre-line strokes written in a small DSL:
 *
 *   M x y                 start a new stroke at a point
 *   L x y                 line to a point (the corner there gets filleted)
 *   A cx cy rx ry a0 a1   elliptical arc, degrees, a1 > a0 = counter-clockwise
 *   CW  cx cy rx ry       wrap around an ellipse clockwise / counter-clockwise,
 *   CCW cx cy rx ry       like a belt round a pulley: it is entered and left
 *                         along tangents worked out from its neighbours
 *   FROM a / TO a         start / end a wrapped ellipse at a fixed angle
 *   X x y                 join two wrapped ellipses at their crossing nearest
 *                         (x, y) instead of along a tangent (the waist of a 3)
 *   R k                   scale the fillet of the corner just drawn (0 = sharp)
 *   Z                     close the stroke
 *   ;                     end the stroke (the next arc starts a new one)
 */
(function (root) {
  'use strict';

  var DEG = Math.PI / 180, TAU = Math.PI * 2;

  // Letter fitting, in cap heights. `target` is the gap between two straight
  // stems before the caller's `spacing` is added.
  var FIT = { target: 0.27, depth: 0.36, min: 0.19, bands: 21, band: 0.06, space: 0.46, round: 0.09 };

  var DEFS = {
    'A': 'M 0 0 L .41 1.03 L .82 0  M .119 .3 L .701 .3',
    'B': 'M 0 .53 CW .37 .265 .25 .265 L 0 0 L 0 1 CW .335 .765 .225 .235 TO -90',
    'C': 'A .44 .5 .44 .512 46 314',
    'D': 'M 0 .5 L 0 1 CW .30 .5 .46 .5 L 0 0 Z',
    'E': 'M .56 1 L 0 1 L 0 0 L .56 0  M 0 .52 L .47 .52',
    'F': 'M .54 1 L 0 1 L 0 0  M 0 .5 L .44 .5',
    'G': 'A .44 .5 .44 .512 46 356 R .4 L .46 .464',
    'H': 'M 0 0 L 0 1  M .70 0 L .70 1  M 0 .51 L .70 .51',
    'I': 'M 0 0 L 0 1',
    'J': 'M .44 1 CW .215 .258 .225 .27 TO 200',
    'K': 'M 0 0 L 0 1  M .60 1 L 0 .47 L .66 0',
    'L': 'M 0 1 L 0 0 L .52 0',
    'M': 'M 0 0 L 0 1.03 L .46 .18 L .92 1.03 L .92 0',
    'N': 'M 0 0 L 0 1.03 L .74 -.03 L .74 1',
    'O': 'A .44 .5 .44 .512 90 450 Z',
    'P': 'M 0 0 L 0 1 CW .345 .735 .255 .265 L 0 .47',
    'Q': 'A .44 .5 .44 .512 90 450 Z  M .54 .27 L .88 -.07',
    'R': 'M 0 0 L 0 1 CW .345 .745 .255 .255 L 0 .49  M .30 .49 L .62 0',
    'S': 'CCW .30 .786 .262 .226 FROM 38 CW .30 .238 .30 .25 TO 218',
    'T': 'M 0 1 L .72 1  M .36 1 L .36 0',
    'U': 'M 0 1 CCW .35 .338 .35 .35 L .70 1',
    'V': 'M 0 1 L .40 -.03 L .80 1',
    'W': 'M 0 1 L .30 -.03 L .60 1.03 L .90 -.03 L 1.20 1',
    'X': 'M 0 0 L .72 1  M 0 1 L .72 0',
    'Y': 'M 0 1 L .38 .44 L .76 1  M .38 .44 L .38 0',
    'Z': 'M 0 1 L .68 1 L 0 0 L .68 0',
    '0': 'A .31 .5 .31 .512 90 450 Z',
    '1': 'M 0 .77 L .24 1.01 L .24 0',
    '2': 'CW .29 .715 .29 .297 FROM 155 L 0 0 L .60 0',
    '3': 'CW .28 .765 .26 .247 FROM 150 X .2 .52 CW .29 .263 .30 .275 TO 210',
    '4': 'M .50 0 L .50 1.03 L 0 .3 L .70 .3',
    '5': 'M .55 1 L .085 1 L .071 .538 CW .29 .31 .31 .322 TO 210',
    '6': 'A .35 .312 .35 .70 48 180 A .31 .312 .31 .324 180 540',
    '7': 'M 0 1 L .60 1 L .20 0',
    '8': 'A .30 .771 .25 .241 -90 270 A .30 .259 .30 .271 90 -270 Z',
    '9': 'A .27 .688 .35 .70 228 360 A .31 .688 .31 .324 0 360',
    '.': 'A .03 .03 .03 .03 0 360 Z',
    ',': 'M .08 .05 L 0 -.16',
    '!': 'M .03 1 L .03 .36 ; A .03 .03 .03 .03 0 360 Z',
    '?': 'CW .245 .767 .245 .245 FROM 160 CCW .395 .40 .15 .15 L .245 .37 ; A .245 .03 .03 .03 0 360 Z',
    '-': 'M 0 .44 L .34 .44',
    '+': 'M 0 .46 L .50 .46  M .25 .71 L .25 .21',
    ':': 'A .03 .03 .03 .03 0 360 Z  A .03 .60 .03 .03 0 360 Z',
    '\'': 'M 0 1 L 0 .76',
    '/': 'M 0 0 L .38 1',
    '&': 'M .72 0 CW .31 .812 .17 .20 CCW .29 .258 .29 .27 L .70 .50',
    '%': 'A .155 .815 .155 .185 90 450 Z  A .895 .185 .155 .185 90 450 Z  M .31 0 L .74 1',
    '#': 'M .17 0 L .29 1  M .53 0 L .65 1  M .03 .33 L .75 .33  M .07 .67 L .79 .67',
    '@': 'M .6796 .69 L .6796 .5 A .4896 .5 .19 .19 0 -360 L .6796 .33 A .8296 .33 .15 .15 180 332 A .51 .5 .512 .512 -28 300',
    '=': 'M 0 .64 L .50 .64  M 0 .30 L .50 .30'
  };

  var ALIAS = { '’': '\'', '‘': '\'', '–': '-', '—': '-', '−': '-', '×': 'X' };

  // ---------------------------------------------------------------- parser

  function parse(d) {
    var tok = d.trim().split(/[\s,]+/), i = 0, strokes = [], cur = null, last = null;
    function num() { return parseFloat(tok[i++]); }
    function add(node) {
      if (!cur) { cur = { nodes: [], closed: false }; strokes.push(cur); }
      node.k = 1; cur.nodes.push(node); last = node;
    }
    while (i < tok.length) {
      var c = tok[i++];
      if (c === 'M') { cur = null; add({ t: 'P', x: num(), y: num() }); }
      else if (c === 'L') add({ t: 'P', x: num(), y: num() });
      else if (c === 'A') add({ t: 'A', cx: num(), cy: num(), rx: num(), ry: num(), a0: num() * DEG, a1: num() * DEG });
      else if (c === 'CW' || c === 'CCW') add({ t: 'B', cx: num(), cy: num(), rx: num(), ry: num(), dir: c === 'CCW' ? 1 : -1 });
      else if (c === 'FROM') last.from = num() * DEG;
      else if (c === 'TO') last.to = num() * DEG;
      else if (c === 'X') last.cusp = [num(), num()];
      else if (c === 'R') last.k = num();
      else if (c === 'Z') { cur.closed = true; cur = null; }
      else if (c === ';') cur = null;
      else if (c !== '') throw new Error('strokefont: bad token "' + c + '" in "' + d + '"');
    }
    return strokes;
  }

  // -------------------------------------------------------------- geometry

  function ept(e, a) { return [e.cx + e.rx * Math.cos(a), e.cy + e.ry * Math.sin(a)]; }

  // Angle at which a tangent through point p touches ellipse e, for a path that
  // runs round e in direction dir and is arriving from p (entering) or leaving
  // towards p.
  function tangentAngle(e, p, dir, entering) {
    var ux = (p[0] - e.cx) / e.rx, uy = (p[1] - e.cy) / e.ry;
    var D = Math.sqrt(ux * ux + uy * uy), beta = D > 1 ? Math.acos(1 / D) : 0;
    return Math.atan2(uy, ux) + (entering ? dir : -dir) * beta;
  }

  // Common tangent from ellipse e1 (leaving) to ellipse e2 (arriving).
  function mutual(e1, e2) {
    var a2 = Math.atan2(e1.cy - e2.cy, e1.cx - e2.cx), a1 = 0;
    for (var i = 0; i < 80; i++) {
      a1 = tangentAngle(e1, ept(e2, a2), e1.dir, false);
      a2 = tangentAngle(e2, ept(e1, a1), e2.dir, true);
    }
    return [a1, a2];
  }

  // Crossing of two ellipses nearest to a hint point, as an angle on each.
  function crossing(e1, e2, hint) {
    function f(a) {
      var p = ept(e1, a), u = (p[0] - e2.cx) / e2.rx, v = (p[1] - e2.cy) / e2.ry;
      return u * u + v * v - 1;
    }
    var N = 720, best = null, bestD = Infinity;
    for (var i = 0; i < N; i++) {
      var lo = i * TAU / N, hi = (i + 1) * TAU / N, flo = f(lo);
      if (flo * f(hi) > 0) continue;
      for (var j = 0; j < 50; j++) {
        var mid = (lo + hi) / 2;
        if (flo * f(mid) <= 0) hi = mid; else { lo = mid; flo = f(lo); }
      }
      var p = ept(e1, lo), d = Math.pow(p[0] - hint[0], 2) + Math.pow(p[1] - hint[1], 2);
      if (d < bestD) { bestD = d; best = [lo, Math.atan2((p[1] - e2.cy) / e2.ry, (p[0] - e2.cx) / e2.rx)]; }
    }
    if (!best) throw new Error('strokefont: ellipses do not cross');
    return best;
  }

  function nodeEnd(n) { return n.t === 'P' ? [n.x, n.y] : ept(n, n.a1); }
  function nodeStart(n) { return n.t === 'P' ? [n.x, n.y] : ept(n, n.a0); }

  // Turn one parsed stroke into line / arc segments.
  function segments(stroke) {
    var nodes = stroke.nodes, n = nodes.length, closed = stroke.closed, i, nd;
    for (i = 0; i < n; i++) {
      nd = nodes[i];
      if (nd.t !== 'B') continue;
      var prev = i > 0 ? nodes[i - 1] : (closed ? nodes[n - 1] : null);
      var next = i < n - 1 ? nodes[i + 1] : (closed ? nodes[0] : null);
      var a0, a1;
      if (nd.from != null) a0 = nd.from;
      else if (prev.t === 'B') a0 = (prev.cusp ? crossing(prev, nd, prev.cusp) : mutual(prev, nd))[1];
      else a0 = tangentAngle(nd, nodeEnd(prev), nd.dir, true);
      if (nd.to != null) a1 = nd.to;
      else if (next.t === 'B') a1 = (nd.cusp ? crossing(nd, next, nd.cusp) : mutual(nd, next))[0];
      else a1 = tangentAngle(nd, next.t === 'P' ? [next.x, next.y] : ept(next, next.a0), nd.dir, false);
      if (nd.dir > 0) { while (a1 < a0 - 1e-9) a1 += TAU; while (a1 > a0 + TAU + 1e-9) a1 -= TAU; }
      else { while (a1 > a0 + 1e-9) a1 -= TAU; while (a1 < a0 - TAU - 1e-9) a1 += TAU; }
      nd.a0 = a0; nd.a1 = a1;
    }
    var segs = [], pen = null, start = null, prevK = 1;
    function line(p, q, k) {
      if (Math.abs(p[0] - q[0]) + Math.abs(p[1] - q[1]) > 2e-3) segs.push({ p0: p, p1: q, k: k });
      else if (segs.length) segs[segs.length - 1].k = k;
    }
    for (i = 0; i < n; i++) {
      nd = nodes[i];
      if (nd.t === 'P') {
        var p = [nd.x, nd.y];
        if (pen) line(pen, p, nd.k); else start = p;
        pen = p;
      } else {
        var s = nodeStart(nd);
        if (pen) line(pen, s, prevK); else start = s;
        segs.push({ e: nd, a0: nd.a0, a1: nd.a1, k: nd.k });
        pen = nodeEnd(nd);
      }
      prevK = nd.k;
    }
    if (closed) line(pen, start, nodes[0].k);
    return segs;
  }

  function segTangent(s, atEnd) {
    var x, y;
    if (s.e) {
      var a = atEnd ? s.a1 : s.a0, sg = s.a1 >= s.a0 ? 1 : -1;
      x = -s.e.rx * Math.sin(a) * sg; y = s.e.ry * Math.cos(a) * sg;
    } else { x = s.p1[0] - s.p0[0]; y = s.p1[1] - s.p0[1]; }
    var l = Math.sqrt(x * x + y * y) || 1;
    return [x / l, y / l];
  }

  // Segments -> fine polyline plus the list of corners that need a fillet.
  function flatten(segs, closed) {
    var pts = [], corners = [];
    function push(p) {
      var q = pts[pts.length - 1];
      if (!q || Math.abs(q[0] - p[0]) + Math.abs(q[1] - p[1]) > 1e-7) pts.push(p);
    }
    for (var j = 0; j < segs.length; j++) {
      var s = segs[j];
      if (s.e) {
        var sweep = s.a1 - s.a0, m = Math.max(4, Math.ceil(Math.abs(sweep) / (1.5 * DEG)));
        for (var i = 0; i <= m; i++) push(ept(s.e, s.a0 + sweep * i / m));
      } else { push(s.p0); push(s.p1); }
      var nx = j < segs.length - 1 ? segs[j + 1] : (closed ? segs[0] : null);
      if (!nx) continue;
      var din = segTangent(s, true), dout = segTangent(nx, false);
      var dot = Math.max(-1, Math.min(1, din[0] * dout[0] + din[1] * dout[1]));
      if (Math.acos(dot) > 1.5 * DEG) corners.push({ i: j < segs.length - 1 ? pts.length - 1 : 0, k: s.k, phi: Math.acos(dot) });
    }
    if (closed && pts.length > 1) {
      var a = pts[0], b = pts[pts.length - 1];
      if (Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]) < 1e-7) pts.pop();
    }
    corners.sort(function (p, q) { return p.i - q.i; });
    return { pts: pts, corners: corners };
  }

  // Round the corners of a polyline. Right-angle and open corners get radius
  // `round`; sharper ones get a smaller radius chosen so the tip never pulls
  // back further than a right-angle corner does, which keeps A, V, M, N crisp.
  function fillet(pts, corners, round, closed) {
    var i, anchors = [];
    if (closed) {
      if (corners.length && corners[0].i === 0) {          // never start on a corner
        var mid = [(pts[0][0] + pts[1][0]) / 2, (pts[0][1] + pts[1][1]) / 2];
        pts = [mid].concat(pts.slice(1), [pts[0]]);
        corners = corners.map(function (c) { return { i: c.i === 0 ? pts.length - 1 : c.i, k: c.k, phi: c.phi }; });
        corners.sort(function (a, b) { return a.i - b.i; });
      }
      pts = pts.concat([pts[0]]);
    }
    var n = pts.length, S = [0];
    for (i = 1; i < n; i++) S.push(S[i - 1] + Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]));
    function at(s, before) {
      var j = 1;
      while (j < n - 1 && (before ? S[j] < s - 1e-12 : S[j] <= s + 1e-12)) j++;
      var l = S[j] - S[j - 1] || 1, u = (s - S[j - 1]) / l;
      var a = pts[j - 1], b = pts[j];
      return { p: [a[0] + (b[0] - a[0]) * u, a[1] + (b[1] - a[1]) * u], d: [(b[0] - a[0]) / l, (b[1] - a[1]) / l] };
    }
    var out = [];
    function push(p) {
      var q = out[out.length - 1];
      if (!q || Math.abs(q[0] - p[0]) + Math.abs(q[1] - p[1]) > 1e-7) out.push(p);
    }
    function copy(s0, s1) {
      push(at(s0, false).p);
      for (var j = 0; j < n; j++) if (S[j] > s0 + 1e-9 && S[j] < s1 - 1e-9) push(pts[j]);
      push(at(s1, true).p);
    }
    var cursor = 0;
    for (var c = 0; c < corners.length; c++) {
      var cr = corners[c], sc = S[cr.i];
      var before = sc - (c > 0 ? S[corners[c - 1].i] : 0), after = (c < corners.length - 1 ? S[corners[c + 1].i] : S[n - 1]) - sc;
      var limIn = c > 0 ? before / 2 : before * (closed ? 1 : 0.9);
      var limOut = c < corners.length - 1 ? after / 2 : after * (closed ? 1 : 0.9);
      var theta = Math.PI - cr.phi, r = round * cr.k, t = 0;
      if (r > 0) {
        if (theta < Math.PI / 2) r = Math.min(r, r * (Math.SQRT2 - 1) / (1 / Math.sin(theta / 2) - 1));
        t = Math.min(r / Math.tan(theta / 2), limIn, limOut);
      }
      if (t < 1e-4) { copy(cursor, sc); anchors.push(out.length - 1); cursor = sc; continue; }
      var A = at(sc - t, true), B = at(sc + t, false);
      copy(cursor, sc - t);
      var cross = A.d[0] * B.d[1] - A.d[1] * B.d[0], X = pts[cr.i];
      if (Math.abs(cross) > 1e-6) {
        var u = ((B.p[0] - A.p[0]) * B.d[1] - (B.p[1] - A.p[1]) * B.d[0]) / cross;
        if (u > 0) X = [A.p[0] + A.d[0] * u, A.p[1] + A.d[1] * u];
      }
      var psi = Math.acos(Math.max(-1, Math.min(1, A.d[0] * B.d[0] + A.d[1] * B.d[1])));
      var w = Math.cos(psi / 2), m = 2 * Math.ceil(psi / (8 * DEG)) + 1;
      for (i = 1; i <= m; i++) {
        var v = i / (m + 1), b0 = (1 - v) * (1 - v), b1 = 2 * v * (1 - v) * w, b2 = v * v, q = b0 + b1 + b2;
        push([(b0 * A.p[0] + b1 * X[0] + b2 * B.p[0]) / q, (b0 * A.p[1] + b1 * X[1] + b2 * B.p[1]) / q]);
        if (i === (m + 1) / 2) anchors.push(out.length - 1);
      }
      cursor = sc + t;
    }
    copy(cursor, S[n - 1]);
    if (closed) out.pop();
    return { pts: out, anchors: anchors, closed: closed };
  }

  // Evenly spaced points by arc length, never more than `step` apart. Corner
  // tips (anchors) are always kept, so spacing is even between corners.
  function resample(st, step) {
    var P = st.closed ? st.pts.concat([st.pts[0]]) : st.pts, last = P.length - 1;
    var bounds = [0].concat(st.anchors.filter(function (a) { return a > 0 && a < last; }), [last]), res = [];
    for (var b = 0; b < bounds.length - 1; b++) {
      var i0 = bounds[b], i1 = bounds[b + 1], S = [0], i;
      for (i = i0 + 1; i <= i1; i++) S.push(S[S.length - 1] + Math.hypot(P[i][0] - P[i - 1][0], P[i][1] - P[i - 1][1]));
      var len = S[S.length - 1];
      if (len < 1e-9) continue;
      var cnt = Math.max(1, Math.ceil(len / step - 1e-9)), j = 1;
      for (i = 0; i < cnt; i++) {
        var s = len * i / cnt;
        while (j < S.length - 1 && S[j] < s) j++;
        var u = (s - S[j - 1]) / (S[j] - S[j - 1] || 1), a = P[i0 + j - 1], c = P[i0 + j];
        res.push([a[0] + (c[0] - a[0]) * u, a[1] + (c[1] - a[1]) * u]);
      }
    }
    if (!st.closed) res.push(P[last].slice());
    return res;
  }

  // ---------------------------------------------------------- glyph caches

  var parsed = {}, built = {}, sampled = {}, profiles = {}, pairs = {};

  function buildGlyph(ch, round) {
    var key = ch + '|' + round;
    if (built[key]) return built[key];
    if (!parsed[ch]) parsed[ch] = parse(DEFS[ch]);
    var strokes = parsed[ch].map(function (st) {
      var f = flatten(segments(st), st.closed);
      return fillet(f.pts, f.corners, round, st.closed);
    });
    // ink box comes from the unrounded outline so metrics do not move with `round`
    var x0 = Infinity, x1 = -Infinity;
    parsed[ch].forEach(function (st) {
      flatten(segments(st), st.closed).pts.forEach(function (p) { if (p[0] < x0) x0 = p[0]; if (p[0] > x1) x1 = p[0]; });
    });
    strokes.forEach(function (st) { st.pts.forEach(function (p) { p[0] -= x0; }); });
    return (built[key] = { strokes: strokes, w: x1 - x0 });
  }

  function sampleGlyph(ch, round, step) {
    var key = ch + '|' + round + '|' + step;
    if (sampled[key]) return sampled[key];
    var g = buildGlyph(ch, round);
    return (sampled[key] = { w: g.w, strokes: g.strokes.map(function (st) { return { pts: resample(st, step), closed: st.closed }; }) });
  }

  // How far the ink sits in from the glyph's left and right edges, per height band.
  function profile(ch) {
    if (profiles[ch]) return profiles[ch];
    var g = sampleGlyph(ch, FIT.round, 0.01), n = FIT.bands, L = [], R = [], i;
    for (i = 0; i < n; i++) { L.push(Infinity); R.push(Infinity); }
    g.strokes.forEach(function (st) {
      st.pts.forEach(function (p) {
        var y = Math.max(0, Math.min(1, p[1]));
        for (i = 0; i < n; i++) {
          if (Math.abs(y - i / (n - 1)) > FIT.band) continue;
          if (p[0] < L[i]) L[i] = p[0];
          if (g.w - p[0] < R[i]) R[i] = g.w - p[0];
        }
      });
    });
    return (profiles[ch] = { L: L, R: R });
  }

  // Gap between the ink boxes of glyph a and the glyph b that follows it.
  // Open shapes (A V, L T, T .) are drawn together by averaging how deep the
  // white space runs between the pair; a clearance oval around every point of
  // the centre lines then stops anything getting closer than FIT.min sideways
  // or twice that vertically.
  function pairGap(a, b) {
    var key = a + b;
    if (pairs[key] != null) return pairs[key];
    var A = profile(a).R, B = profile(b).L, sum = 0, i, j;
    for (i = 0; i < FIT.bands; i++) sum += Math.min(A[i] + B[i], FIT.depth);
    var d = FIT.target - sum / FIT.bands;
    var ga = sampleGlyph(a, FIT.round, 0.02), gb = sampleGlyph(b, FIT.round, 0.02), pa = [], pb = [];
    ga.strokes.forEach(function (st) { st.pts.forEach(function (p) { pa.push(p); }); });
    gb.strokes.forEach(function (st) { st.pts.forEach(function (p) { pb.push(p); }); });
    var m = FIT.min, my = FIT.min * 2;
    for (i = 0; i < pa.length; i++) {
      for (j = 0; j < pb.length; j++) {
        var dy = (pa[i][1] - pb[j][1]) / my;
        if (dy > -1 && dy < 1) {
          var need = m * Math.sqrt(1 - dy * dy) - (ga.w - pa[i][0]) - pb[j][0];
          if (need > d) d = need;
        }
      }
    }
    return (pairs[key] = d);
  }

  function glyphKey(c) {
    if (ALIAS[c]) c = ALIAS[c];
    c = c.toUpperCase();
    return DEFS.hasOwnProperty(c) ? c : null;
  }

  // ------------------------------------------------------------------ API

  function layout(text, opts) {
    opts = opts || {};
    text = String(text == null ? '' : text);
    var size = opts.size != null ? opts.size : 1;
    var spacing = opts.spacing != null ? opts.spacing : 0.18;
    var lineGap = opts.lineGap != null ? opts.lineGap : 0.45;
    var align = opts.align || 'center';
    var round = Math.max(0, opts.round != null ? opts.round : 0.09);
    var step = Math.max(1e-3, opts.step != null ? opts.step : 0.03);

    // pass 1: place every character on its line, in cap heights
    var lines = [], line = { items: [], width: 0 }, prev = null, x = 0, i;
    lines.push(line);
    var chars = [];
    for (i = 0; i < text.length; i++) {
      var raw = text[i];
      if (raw === '\n') {
        chars.push({ char: '\n', nl: true, line: lines.length - 1 });
        line = { items: [], width: 0 }; lines.push(line); prev = null; x = 0;
        continue;
      }
      var key = glyphKey(raw), w = key ? buildGlyph(key, round).w : FIT.space;
      if (line.items.length) x += (key && prev ? pairGap(prev, key) : 0) + spacing;
      var item = { char: key || ' ', key: key, x: x, w: w, line: lines.length - 1 };
      chars.push(item); line.items.push(item);
      x += w; line.width = x; prev = key;
    }
    var width = 0;
    lines.forEach(function (l) { if (l.width > width) width = l.width; });
    var height = lines.length + (lines.length - 1) * lineGap;

    // pass 2: emit strokes
    var strokes = [], glyphs = [];
    for (i = 0; i < chars.length; i++) {
      var c = chars[i];
      var ln = lines[c.line];
      var ox = align === 'left' ? 0 : align === 'right' ? -ln.width : -ln.width / 2;
      var oy = height / 2 - 1 - c.line * (1 + lineGap);
      if (c.nl) {
        glyphs.push({ char: '\n', x: (ox + ln.width) * size, y: oy * size, w: 0, strokeIndexes: [] });
        continue;
      }
      var rec = { char: c.char, x: (ox + c.x) * size, y: oy * size, w: c.w * size, strokeIndexes: [] };
      if (c.key) {
        var g = sampleGlyph(c.key, round, step);
        for (var s = 0; s < g.strokes.length; s++) {
          var src = g.strokes[s].pts, pts = new Array(src.length);
          for (var p = 0; p < src.length; p++) pts[p] = [(ox + c.x + src[p][0]) * size, (oy + src[p][1]) * size];
          rec.strokeIndexes.push(strokes.length);
          strokes.push({ points: pts, closed: g.strokes[s].closed, glyph: i, char: c.char });
        }
      }
      glyphs.push(rec);
    }
    return { strokes: strokes, width: width * size, height: height * size, glyphs: glyphs };
  }

  function length(stroke) {
    var pts = stroke && stroke.points ? stroke.points : stroke, len = 0;
    if (!pts || pts.length < 2) return 0;
    for (var i = 1; i < pts.length; i++) len += Math.hypot(pts[i][0] - pts[i - 1][0], pts[i][1] - pts[i - 1][1]);
    if (stroke.closed) len += Math.hypot(pts[0][0] - pts[pts.length - 1][0], pts[0][1] - pts[pts.length - 1][1]);
    return len;
  }

  var glyphs = { ' ': { d: '', w: FIT.space, strokes: 0 } };
  Object.keys(DEFS).forEach(function (ch) {
    var g = buildGlyph(ch, FIT.round);
    glyphs[ch] = { d: DEFS[ch], w: Math.round(g.w * 1e4) / 1e4, strokes: g.strokes.length };
  });

  var STROKEFONT = { glyphs: glyphs, layout: layout, length: length };

  root.STROKEFONT = STROKEFONT;
  if (typeof module !== 'undefined' && module.exports) module.exports = STROKEFONT;
})(typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : this);
```

REFERENCE STYLE SOURCE (styles/blueprint-build.js; its sample topic was "An ergonomic desk set-up: screen top at eye level, elbows at about 90 degrees, feet flat on the floor")
```js
{
  // ---------------------------------------------------------------- content (change this block for a new subject)
  const TITLE = ['ERGONOMIC', 'DESK SET-UP'];
  const BLOCK = ['DRG  A-101', 'SCALE 1:10'];                                  // small print in the title block
  // the three pointers: [two short lines of lettering]. Their marks and anchors are set where the model is built.
  const NOTES = [['SCREEN TOP', 'AT EYE LEVEL'], ['ELBOWS', 'AT ABOUT 90°'], ['FEET FLAT', 'ON THE FLOOR']];

  // ---------------------------------------------------------------- the beats, in seconds (picture and sound share them)
  // 1 the plan is plotted flat on the sheet   2 the camera tips over and every part rises out of its own plan
  // 3 the three notes leave their column on the sheet and are tied to the model, one by one
  // 4 a section plane climbs through the wireframe and leaves it solid; the lamp in the model comes on
  const T = {
    draw: { desk: [-2.2, -1.1], items: [-1.3, -0.2], chair: [-0.5, 0.6], figure: [0.45, 1.45], lamp: [1.2, 1.75] },
    lift: 1.85, liftGap: 0.13, liftDur: 0.9,      // desk, chair, desk items, lamp, figure, in that order
    notes: [3.0, 3.2, 3.4], fly: 0.85,            // the notes leave the sheet one after another and fly to their places beside the model
    marks: [3.85, 4.25, 4.65],                    // then each one is tied to the model: a leader, and its mark drawn on the part
    fill: [5.2, 6.6],
    lamp: 6.7,
  };
  const LIFT_ORDER = { desk: 0, chair: 1, items: 2, lamp: 3, figure: 4 };
  const liftAt = (name) => T.lift + LIFT_ORDER[name] * T.liftGap;

  const INK = '#f1f6ff', PAPER = '#1f52ad';
  const HEAVY = 3.6, LIGHT = 2.0, FINE = 1.2;   // line weights in pixels: outlines, construction and dimensions, grid

  // ================================================================ the blueprint kit (reusable: nothing below knows about desks)
  //   ink(list, opts)           draws line records { a, b, w, alpha, at, dur, ... } with a constant width on screen. Every
  //                             line draws itself on from `at` over `dur`. opts.hidden draws only the parts that are
  //                             BEHIND a surface, dashed (the hidden-line convention); opts.onTop ignores depth.
  //   edgesOf(geometry)         the hard edges of any mesh as line records
  //   penOrder(list, t0, t1)    sorts line records so the pen travels as little as possible and gives them times
  //   letter(text, opts)        single-stroke drafting capitals made of the same lines, plotted letter by letter
  //   dimension(a, b, opts)     witness lines, a dimension line with arrowheads and its figure, anywhere in 3D
  //   buildable(material)       gives a material a section plane: below it the real material, at it a hatched cut
  //                             face, above it nothing. depthOnly() keeps the unbuilt part hiding lines behind it.
  //   sheet(opts)               the paper: blueprint blue with grain, uneven exposure, fibres and worn fold creases
  function blueprintKit(S) {
    const { THREE, K, scene } = S;
    const U = {
      uTime: { value: 0 }, uRes: { value: new THREE.Vector2(S.W, S.H) },
      uLift: { value: new Array(12).fill(1) },                               // height scale of each lift group (0 = flat on the sheet)
      uInk: { value: new THREE.Color(INK) }, uInk2: { value: new THREE.Color(PAPER).multiplyScalar(0.55) },
      uFill: { value: -10 }, uFillN: { value: new THREE.Vector3(0.22, 1, -0.16) },   // the section plane: dot(uFillN, p) = uFill
      uPool: { value: new THREE.Vector4(0, 0, 3, 9) },                       // ink dims with the light pool on the sheet
    };
    const QUAD = new THREE.PlaneGeometry(1, 2).translate(0.5, 0, 0);          // x 0..1 along the line, y -1..1 across it

    const LINE_VS = `
      attribute vec3 aA, aB;
      attribute vec4 aTime;    // start, duration, cap at a, cap at b (caps in half line widths)
      attribute vec4 aStyle;   // width in px, alpha, taper (width multiplier at b: 0 makes an arrowhead), ink (0 white, 1 paper)
      attribute vec3 aKind;    // lift group, dash period in world units (0 = solid), dash style (1 dashes, 2 dash-dot)
      uniform float uTime, uBias, uFlat, uWidth;
      uniform vec2 uRes;
      uniform float uLift[12];
      varying float vAcross, vAlong, vAlpha, vW, vTip, vInk;
      varying vec2 vDash;
      varying vec3 vWorld;
      void main() {
        float lift = uFlat > 0.5 ? 0.0 : uLift[int(aKind.x + 0.5)];
        vec3 a = aA, b = aB; a.y *= lift; b.y *= lift;
        float p = clamp((uTime - aTime.x) / max(aTime.y, 1e-4), 0.0, 1.0);
        p = 1.0 - (1.0 - p) * (1.0 - p);
        vec3 h = mix(a, b, p);                                              // where the pen is now
        vec4 wa = modelMatrix * vec4(a, 1.0), wh = modelMatrix * vec4(h, 1.0);
        vec4 va = viewMatrix * wa, vh = viewMatrix * wh;
        vec4 ca = projectionMatrix * va, ch = projectionMatrix * vh;
        vec2 d = (ch.xy / ch.w - ca.xy / ca.w) * uRes * 0.5;
        float L = length(d), u = position.x, v = position.y;
        vec2 dir = L > 1e-4 ? d / L : vec2(1.0, 0.0), nrm = vec2(-dir.y, dir.x);
        float w0 = aStyle.x * uWidth, w = w0 * mix(1.0, aStyle.z, u);
        float ext = (u < 0.5 ? -aTime.z : aTime.w) * w0 * 0.5;
        vec4 c = mix(ca, ch, u);
        c.xy += (nrm * v * w * 0.5 + dir * ext) / (uRes * 0.5) * c.w;       // widen the line in screen space
        vec4 vb = mix(va, vh, u); vb.xyz *= 1.0 - uBias;                     // and pull it a little toward the lens, so it sits on its surface
        vec4 cz = projectionMatrix * vb; c.z = cz.z / cz.w * c.w;
        float wl = distance(wa.xyz, wh.xyz);
        if (p <= 0.0 || aStyle.y <= 0.0 || wl < 1e-5) c = vec4(2.0, 2.0, 2.0, 1.0);
        gl_Position = c;
        vAcross = v; vW = max(w, 0.6); vAlpha = aStyle.y; vInk = aStyle.w; vDash = aKind.yz;
        vAlong = u * wl; vWorld = mix(wa.xyz, wh.xyz, u);
        vTip = p < 1.0 ? (1.0 - u) * L : 1e4;
      }`;
    const LINE_FS = `
      uniform vec3 uInk, uInk2, uFillN;
      uniform float uHidden, uGhost, uOpacity, uFill, uTipGlow;
      uniform vec4 uPool;
      varying float vAcross, vAlong, vAlpha, vW, vTip, vInk;
      varying vec2 vDash;
      varying vec3 vWorld;
      void main() {
        float a = vAlpha * uOpacity * (1.0 - smoothstep(max(0.0, 1.0 - 1.4 / vW), 1.0, abs(vAcross)));
        float period = vDash.x, style = vDash.y;
        if (uHidden > 0.5) { period = 0.075; style = 1.0; }                 // hidden edges are always dashed
        if (period > 0.0) {
          float f = fract(vAlong / period);
          if (!(style < 1.5 ? f < 0.56 : (f < 0.56 || (f > 0.73 && f < 0.83)))) discard;
        }
        a *= mix(1.0, uGhost, smoothstep(0.0, 0.12, uFill - dot(uFillN, vWorld)));   // once the solid has passed, the line is a ghost
        vec3 col = mix(uInk, uInk2, vInk);
        col *= 1.0 + uTipGlow * exp(-vTip / 6.0);                           // the pen tip is hot while a line is being drawn
        col *= mix(1.0, 0.25, smoothstep(uPool.z, uPool.w, length(vWorld.xz - uPool.xy)));
        if (a < 0.004) discard;
        gl_FragColor = vec4(col, a);
      }`;

    const rec = (a, b, p = {}) => Object.assign({ a, b }, p);
    function ink(list, p = {}) {
      const n = list.length, g = new THREE.InstancedBufferGeometry();
      g.setIndex(QUAD.index); g.setAttribute('position', QUAD.attributes.position);
      const A = new Float32Array(n * 3), B = new Float32Array(n * 3), Tm = new Float32Array(n * 4), St = new Float32Array(n * 4), Kd = new Float32Array(n * 3);
      list.forEach((l, i) => {
        A.set(l.a, i * 3); B.set(l.b, i * 3);
        Tm.set([l.at == null ? -10 : l.at, l.dur == null ? 0.25 : l.dur, l.capA == null ? 1 : l.capA, l.capB == null ? 1 : l.capB], i * 4);
        St.set([l.w || LIGHT, l.alpha == null ? 1 : l.alpha, l.taper == null ? 1 : l.taper, l.ink || 0], i * 4);
        Kd.set([l.group || 0, l.dash || 0, l.dashStyle || 1], i * 3);
      });
      for (const [name, arr, size] of [['aA', A, 3], ['aB', B, 3], ['aTime', Tm, 4], ['aStyle', St, 4], ['aKind', Kd, 3]]) g.setAttribute(name, new THREE.InstancedBufferAttribute(arr, size));
      g.instanceCount = n;
      return inkMesh(g, p);
    }
    // a second drawing of the same lines (another pass, or the flat copy left on the sheet): inkMesh(first.geometry, opts)
    function inkMesh(g, p = {}) {
      const mat = new THREE.ShaderMaterial({
        uniforms: Object.assign({}, U, { uOpacity: { value: p.opacity == null ? 1 : p.opacity }, uGhost: { value: 1 }, uHidden: { value: p.hidden ? 1 : 0 }, uFlat: { value: p.flat ? 1 : 0 },
          uBias: { value: p.bias == null ? 0.0022 : p.bias }, uWidth: { value: p.width || 1 }, uTipGlow: { value: p.tip == null ? 1.4 : p.tip } }),
        vertexShader: LINE_VS, fragmentShader: LINE_FS, transparent: true, depthWrite: false, depthTest: !p.onTop,
        depthFunc: p.hidden ? THREE.GreaterDepth : THREE.LessEqualDepth,
      });
      const mesh = new THREE.Mesh(g, mat); mesh.frustumCulled = false; mesh.renderOrder = p.order == null ? 4 : p.order;
      (p.parent || scene).add(mesh);
      return mesh;
    }

    function edgesOf(geo, angle = 25) {
      const p = new THREE.EdgesGeometry(geo, angle).attributes.position, out = [];
      for (let i = 0; i < p.count; i += 2) out.push(rec([p.getX(i), p.getY(i), p.getZ(i)], [p.getX(i + 1), p.getY(i + 1), p.getZ(i + 1)]));
      return out;
    }
    // Plotter order: start at one corner, always go on to the nearest free end, and hand out times along the way.
    // Distances are measured in plan (y counts little), because that is how the drawing is first seen.
    function penOrder(list, t0, t1, dur = 0.28) {
      const d2 = (p, q) => (p[0] - q[0]) ** 2 + (p[2] - q[2]) ** 2 + 0.05 * (p[1] - q[1]) ** 2, left = list.slice(), out = [];
      left.sort((p, q) => (p.a[0] + p.a[2]) - (q.a[0] + q.a[2]));
      let cur = left.shift(); out.push(cur);
      while (left.length) {
        let bi = 0, bd = Infinity, flip = false;
        for (let i = 0; i < left.length; i++) { const da = d2(cur.b, left[i].a), db = d2(cur.b, left[i].b); if (da < bd) { bd = da; bi = i; flip = false; } if (db < bd) { bd = db; bi = i; flip = true; } }
        cur = left.splice(bi, 1)[0]; if (flip) [cur.a, cur.b] = [cur.b, cur.a]; out.push(cur);
      }
      const len = out.map((l) => 0.02 + Math.hypot(l.b[0] - l.a[0], l.b[2] - l.a[2])), total = len.reduce((s, x) => s + x, 0); let run = 0;
      out.forEach((l, i) => { l.at = t0 + (t1 - t0 - dur) * run / total; l.dur = dur; run += len[i]; });
      return out;
    }

    // Drafting capitals from lib/strokefont.js. p: size (cap height), align, map(x, y) -> [x, y, z] (what puts the words on
    // the sheet, on a wall or in a label), at, per (seconds per letter), w, alpha, ink, squeeze (letter width)
    function letter(text, p = {}) {
      const size = p.size || 0.1, sq = p.squeeze || 0.9, map = p.map || ((x, y) => [x, y, 0]), per = p.per == null ? 0.028 : p.per, out = [];
      const lay = STROKEFONT.layout(text.replace(/°/g, ' '), { size, align: p.align || 'left', spacing: 0.2, step: p.step || 0.075 });
      const seg = (x0, y0, x1, y1, at, capA, capB) => out.push(rec(map(x0 * sq, y0), map(x1 * sq, y1), { at, dur: per * 1.2, w: p.w || LIGHT, alpha: p.alpha, ink: p.ink, capA, capB }));
      let n = 0;
      lay.glyphs.forEach((gl, i) => {
        const at = (p.at == null ? -10 : p.at) + n * per;
        if (text[i] === '°') { const r = 0.15 * size, cx = gl.x + 0.2 * size, cy = gl.y + size - r; for (let k = 0; k < 10; k++) { const a0 = k / 10 * 6.2832, a1 = (k + 1) / 10 * 6.2832; seg(cx + Math.cos(a0) * r, cy + Math.sin(a0) * r, cx + Math.cos(a1) * r, cy + Math.sin(a1) * r, at, 0.2, 0.2); } n++; return; }
        if (gl.strokeIndexes.length) n++;
        gl.strokeIndexes.forEach((si) => {
          const st = lay.strokes[si], pts = st.closed ? st.points.concat([st.points[0]]) : st.points, m = pts.length - 1;
          for (let k = 0; k < m; k++) seg(pts[k][0], pts[k][1], pts[k + 1][0], pts[k + 1][1], at + per * 0.9 * k / m, k === 0 && !st.closed ? 1 : 0.2, k === m - 1 && !st.closed ? 1 : 0.2);
        });
      });
      out.width = lay.width * sq; out.end = (p.at == null ? -10 : p.at) + n * per + per * 1.2;
      return out;
    }

    const add = (p, q, k = 1) => [p[0] + q[0] * k, p[1] + q[1] * k, p[2] + q[2] * k], sub = (p, q) => [p[0] - q[0], p[1] - q[1], p[2] - q[2]];
    const unit = (p) => { const l = Math.hypot(p[0], p[1], p[2]) || 1; return [p[0] / l, p[1] / l, p[2] / l]; };
    const arrow = (tip, dir, p = {}) => rec(add(tip, dir, -(p.len || 0.075)), tip, { w: p.head || 11, taper: 0, capA: 0, capB: 0, at: p.at, dur: p.dur || 0.12 });
    // the same lines with a dark edge under them, so they stay readable where they cross a light surface
    const cased = (list, extra = 3.6) => list.map((l) => Object.assign({}, l, { ink: 1, w: (l.w || LIGHT) + extra, alpha: 0.8 })).concat(list);
    // A dimension between two points. off = the vector from the measured points out to the dimension line (it also sets
    // which way is "up" for the figure; flip puts the figure on the inner side). The figure reads along a -> b.
    function dimension(a, b, p = {}) {
      const off = p.off, on = unit(off), up = p.flip ? on.map((x) => -x) : on, dir = unit(sub(b, a)), A1 = add(a, off), B1 = add(b, off), at = p.at == null ? -10 : p.at, mid = add(add(A1, B1), [0, 0, 0]).map((x) => x / 2);
      const out = [
        rec(add(a, on, 0.04), add(A1, on, 0.05), { at, dur: 0.2 }), rec(add(b, on, 0.04), add(B1, on, 0.05), { at: at + 0.05, dur: 0.2 }),
        rec(mid, A1, { at: at + 0.15, dur: 0.3, capA: 0 }), rec(mid, B1, { at: at + 0.15, dur: 0.3, capA: 0 }),
        arrow(A1, dir.map((x) => -x), { at: at + 0.38 }), arrow(B1, dir, { at: at + 0.38 }),
      ];
      const size = p.size || 0.06, w = letter(p.text, { size }).width;
      return out.concat(letter(p.text, { size, at: at + 0.3, per: 0.05, w: p.w || 2.2, map: (x, y) => add(add(mid, dir, x - w / 2), up, y + size * 0.5 + (p.gap || 0.03)) }));
    }

    // ---- solids that can be built up. The section plane is shared with the lines (uFill, uFillN).
    const FILL = { uFill: U.uFill, uFillN: U.uFillN, uCutA: { value: new THREE.Color('#dbe8ff') }, uCutB: { value: new THREE.Color(PAPER).multiplyScalar(0.8) }, uTint: { value: new THREE.Color('#5d97ff') }, uBand: { value: 0.16 } };
    const CUT_VS = ['#include <common>', '#include <common>\nvarying vec3 vBW;', '#include <begin_vertex>', '#include <begin_vertex>\nvBW = (modelMatrix * vec4(transformed, 1.0)).xyz;'];
    function buildable(mat) {
      mat.side = THREE.DoubleSide;     // the inside of the shell, seen through the cut, is painted as the cut face
      mat.onBeforeCompile = (sh) => {
        Object.assign(sh.uniforms, FILL);
        sh.vertexShader = sh.vertexShader.replace(CUT_VS[0], CUT_VS[1]).replace(CUT_VS[2], CUT_VS[3]);
        sh.fragmentShader = sh.fragmentShader.replace('#include <common>', '#include <common>\nvarying vec3 vBW; uniform float uFill, uBand; uniform vec3 uFillN, uCutA, uCutB, uTint;')
          .replace('#include <clipping_planes_fragment>', '#include <clipping_planes_fragment>\nfloat bd = uFill - dot(uFillN, vBW); if (bd < 0.0) discard;')
          .replace('#include <opaque_fragment>', `#include <opaque_fragment>
            if (!gl_FrontFacing) {
              // where the eye's ray meets the section plane: hatch that point at 45 degrees, as a draughtsman hatches cut material
              vec3 rd = normalize(vBW - cameraPosition);
              vec3 pc = cameraPosition + rd * (uFill - dot(uFillN, cameraPosition)) / dot(uFillN, rd);
              float hs = abs(fract((pc.x + pc.z) / 0.034) - 0.5) * 2.0, aa = fwidth((pc.x + pc.z) / 0.034) * 2.0;
              gl_FragColor.rgb = mix(uCutA, uCutB, smoothstep(0.62 - aa, 0.62 + aa, hs));
            } else {
              // just under the plane the new surface is still a blue-toned drawing of itself, then it takes its real colour
              float l = dot(gl_FragColor.rgb, vec3(0.2126, 0.7152, 0.0722));
              gl_FragColor.rgb = mix(uTint * (0.22 + 1.5 * l), gl_FragColor.rgb, smoothstep(0.0, uBand, bd));
              gl_FragColor.rgb = mix(uCutA, gl_FragColor.rgb, smoothstep(0.004, 0.011, bd));
            }`);
      };
      return mat;
    }
    const cutShadow = new THREE.MeshDepthMaterial({ side: THREE.DoubleSide });   // only the built part throws a shadow
    cutShadow.onBeforeCompile = (sh) => {
      Object.assign(sh.uniforms, FILL);
      sh.vertexShader = sh.vertexShader.replace(CUT_VS[0], CUT_VS[1]).replace(CUT_VS[2], CUT_VS[3]);
      sh.fragmentShader = sh.fragmentShader.replace('#include <common>', '#include <common>\nvarying vec3 vBW; uniform float uFill; uniform vec3 uFillN;')
        .replace('#include <clipping_planes_fragment>', '#include <clipping_planes_fragment>\nif (uFill - dot(uFillN, vBW) < 0.0) discard;');
    };
    // writes depth and no colour: the unbuilt object still hides what is behind it, so hidden edges can be told apart
    const depthOnly = new THREE.MeshBasicMaterial({ colorWrite: false, transparent: true });

    // ---- the paper
    function sheet(o) {
      const N = 512, grain = K.canvasTex(N, N, (ctx) => {
        const r = K.rng(7), layer = (draw) => { const c = document.createElement('canvas'); c.width = c.height = N; const x = c.getContext('2d'); x.fillStyle = '#000'; x.fillRect(0, 0, N, N); draw(x); return x.getImageData(0, 0, N, N).data; };
        const wrap = (x, fn) => { for (const dx of [-N, 0, N]) for (const dy of [-N, 0, N]) { x.save(); x.translate(dx, dy); fn(); x.restore(); } };
        const blot = layer((x) => { x.fillStyle = '#808080'; x.fillRect(0, 0, N, N); for (let i = 0; i < 420; i++) { const px = r() * N, py = r() * N, rad = r.range(6, 46), v = r() < 0.5 ? 255 : 0; const g = x.createRadialGradient(px, py, 0, px, py, rad); g.addColorStop(0, `rgba(${v},${v},${v},0.16)`); g.addColorStop(1, `rgba(${v},${v},${v},0)`); x.fillStyle = g; wrap(x, () => x.fillRect(px - rad, py - rad, rad * 2, rad * 2)); } });
        const fibre = layer((x) => { x.lineCap = 'round'; for (let i = 0; i < 900; i++) { const px = r() * N, py = r() * N, a = r() * 6.28, l = r.range(4, 22), b = r.range(-0.5, 0.5); x.strokeStyle = `rgba(255,255,255,${r.range(0.15, 0.8)})`; x.lineWidth = r.range(0.5, 1.3); wrap(x, () => { x.beginPath(); x.moveTo(px, py); x.quadraticCurveTo(px + Math.cos(a + b) * l * 0.5, py + Math.sin(a + b) * l * 0.5, px + Math.cos(a) * l, py + Math.sin(a) * l); x.stroke(); }); } });
        const img = ctx.createImageData(N, N);
        for (let i = 0; i < N * N; i++) { img.data[i * 4] = 255 * r(); img.data[i * 4 + 1] = blot[i * 4]; img.data[i * 4 + 2] = fibre[i * 4]; img.data[i * 4 + 3] = 255; }
        ctx.putImageData(img, 0, 0);     // R fine tooth, G soft blotches, B fibres
      }, { data: true, repeat: [1, 1] });
      const mat = new THREE.MeshStandardMaterial({ color: o.color, roughness: 0.86, metalness: 0 });
      mat.onBeforeCompile = (sh) => {
        Object.assign(sh.uniforms, { uGrain: { value: grain }, uFold: { value: new THREE.Vector4(...o.fold) }, uWear: { value: new THREE.Color(o.wear || '#7fa6ee') } });
        sh.vertexShader = sh.vertexShader.replace(CUT_VS[0], CUT_VS[1]).replace(CUT_VS[2], CUT_VS[3]);
        sh.fragmentShader = sh.fragmentShader.replace('#include <common>', '#include <common>\nvarying vec3 vBW; uniform sampler2D uGrain; uniform vec4 uFold; uniform vec3 uWear;')
          .replace('#include <map_fragment>', `#include <map_fragment>
            vec2 sp = vBW.xz;
            vec3 g1 = texture2D(uGrain, sp * 1.9).rgb, g2 = texture2D(uGrain, sp * 0.21 + 0.31).rgb, g3 = texture2D(uGrain, sp * 0.045 + 0.7).rgb;
            diffuseColor.rgb *= 1.0 + (g1.r - 0.5) * 0.20 + (g1.g - 0.5) * 0.22 + (g2.g - 0.5) * 0.34 + (g3.g - 0.5) * 0.5;   // tooth, and exposure that never came out even
            diffuseColor.rgb += vec3(0.035, 0.06, 0.10) * smoothstep(0.3, 0.9, g1.b);                                       // pale fibres in the stock
            vec2 fq = (sp - uFold.xy) / uFold.zw, fs = (fract(fq + 0.5) - 0.5) * uFold.zw;                                  // signed distance to the nearest fold, each way
            float wear = max(1.0 - smoothstep(0.0, 0.008, abs(fs.x)), 1.0 - smoothstep(0.0, 0.008, abs(fs.y)));
            wear = max(wear * (0.25 + 0.75 * smoothstep(0.25, 0.6, g2.r)), 0.35 * max(1.0 - smoothstep(0.0, 0.05, abs(fs.x)), 1.0 - smoothstep(0.0, 0.05, abs(fs.y))) * g2.g);
            diffuseColor.rgb = mix(diffuseColor.rgb, uWear, wear * 0.6);                                                    // ink rubbed off along the folds`)
          .replace('#include <normal_fragment_maps>', `#include <normal_fragment_maps>
            vec2 par = mod(floor(fq + 0.5), 2.0) * 2.0 - 1.0;                                                               // mountain and valley folds alternate
            vec2 tilt = par * sign(fs) * exp(-abs(fs) / 0.11) * 0.2 + (g3.rb - 0.5) * 0.03;
            normal = normalize((viewMatrix * vec4(normalize(vec3(tilt.x, 1.0, tilt.y)), 0.0)).xyz);`);
      };
      const [x0, z0, x1, z1] = o.rect, mesh = new THREE.Mesh(new THREE.PlaneGeometry(x1 - x0, z1 - z0).rotateX(-Math.PI / 2).translate((x0 + x1) / 2, 0, (z0 + z1) / 2), mat);
      mesh.receiveShadow = true; scene.add(mesh);
      return mesh;
    }

    return { U, rec, ink, inkMesh, edgesOf, penOrder, letter, dimension, arrow, cased, buildable, cutShadow, depthOnly, sheet, add, sub, unit };
  }

  registerStyle({
    id: 'blueprint-build',
    name: 'Blueprint Build',
    family: 'Diagrams with depth',
    look: 'A real drafting sheet: deep blueprint blue with grain and worn fold creases, a title block, a construction grid, crisp white linework in two weights with hidden edges dashed, dimension arrows and numbered notes in plotted single-stroke capitals. The drawing turns out to be a 3D wireframe, and then a solid object standing on its own plan.',
    motion: 'Lines plot themselves in pen order with a hot tip. As the camera tips over, each part rises out of its own plan, which stays printed on the sheet. The numbered notes peel off the paper, turn to face the lens and are tied to the model with leaders, one by one. Then a hatched section plane climbs through the wireframe and leaves real material under it.',
    light: 'One soft key from behind and to the side, so tops and one flank are lit, the near faces stay in shade and a long soft shadow falls toward the camera across the sheet. A little cool fill. At the end the desk lamp in the model switches on.',
    materials: 'Uncoated blue paper that takes the light, flat white ink. The built object is restrained and real: pale oak, matt white steel, rust wool, off-white plaster, graphite plastic. The white lines stay on it as a faint ghost.',
    camera: 'One continuous crane move with a long lens that opens up: it starts straight above the sheet, where the drawing reads as a flat plan, tips down to a three-quarter view as the drawing gains height, and keeps drifting in until the last frame.',
    post: 'Almost none: neutral tone curve so the blue and white stay true, light vignette, a trace of bloom on pen tips and the lamp, fine grain. Hidden lines are found by a real depth test, not drawn by hand.',
    explains: 'Plans, set-ups and how-it-is-made: first the layout as a flat drawing, then the same thing as an object, with dimensions and two or three numbered pointers attached to the places they talk about.',
    topic: 'An ergonomic desk set-up: screen top at eye level, elbows at about 90 degrees, feet flat on the floor',
    duration: 8,
    fonts: [],
    render: { samples: 28, shutter: 0.5, tone: 'neutral', exposure: 1.0, bloom: 0.3, bloomThreshold: 1.05, vignette: 0.34, grain: 0.016, fov: 20, near: 0.5, far: 120, background: '#070a12' },

    build(S) {
      const { THREE, K, scene, camera } = S;
      const B = blueprintKit(S), { U, rec, ink, inkMesh, letter, dimension, arrow } = B;

      // ---------------------------------------------------------------- the subject, from simple forms
      // Everything is modelled in metres with the sitter facing +u, then turned to face west on the sheet (x = CX - u).
      // A part is a solid, the edges that get drawn for it, a lift group and a line weight.
      const CX = 0.36, PLACE = new THREE.Matrix4().makeRotationY(Math.PI).setPosition(CX, 0, 0);
      const W = (u, y, z) => [CX - u, y, -z];                       // model point -> sheet point (for dimensions and notes)
      const GROUPS = ['paper', 'desk', 'chair', 'items', 'lamp', 'figure'], gid = (name) => GROUPS.indexOf(name);
      const parts = [], Y = new THREE.Vector3(0, 1, 0), v3 = (p) => new THREE.Vector3(p[0], p[1], p[2]);
      const part = (group, mat, geo, wire, m, w) => {
        const full = PLACE.clone().multiply(m); geo.applyMatrix4(full);
        wire.forEach((l) => { l.a = v3(l.a).applyMatrix4(full).toArray(); l.b = v3(l.b).applyMatrix4(full).toArray(); l.group = gid(group); l.w = w || HEAVY; });
        parts.push({ group, mat, geo, wire });
      };
      const pose = (c, rot) => new THREE.Matrix4().compose(v3(c), new THREE.Quaternion().setFromEuler(new THREE.Euler(...(rot || [0, 0, 0]))), new THREE.Vector3(1, 1, 1));
      // box(group, material, centre, size, { r: edge radius, rot, w: line weight }): drawn as its twelve sharp edges
      const box = (group, mat, c, s, o = {}) => part(group, mat, K.roundedBox(s[0], s[1], s[2], o.r == null ? 0.008 : o.r, 3), B.edgesOf(new THREE.BoxGeometry(s[0], s[1], s[2])), pose(c, o.rot), o.w);
      const ring = (r, y, n = 28) => Array.from({ length: n }, (_, i) => rec([Math.cos(i / n * 6.2832) * r, y, Math.sin(i / n * 6.2832) * r], [Math.cos((i + 1) / n * 6.2832) * r, y, Math.sin((i + 1) / n * 6.2832) * r], { capA: 0.2, capB: 0.2 }));
      // rod(group, material, from, to, r0, r1, { oval: [sx, sz] }): a round or oval bar, drawn as its two end rings and four generator lines
      const rod = (group, mat, a, b, r0, r1 = r0, o = {}) => {
        const A = v3(a), Bv = v3(b), len = A.distanceTo(Bv), ov = o.oval || [1, 1];
        const m = new THREE.Matrix4().compose(A.clone().add(Bv).multiplyScalar(0.5), new THREE.Quaternion().setFromUnitVectors(Y, Bv.clone().sub(A).normalize()), new THREE.Vector3(ov[0], 1, ov[1]));
        const wire = ring(r0, -len / 2).concat(ring(r1, len / 2), [0, 1, 2, 3].map((k) => rec([Math.cos(k * 1.5708) * r0, -len / 2, Math.sin(k * 1.5708) * r0], [Math.cos(k * 1.5708) * r1, len / 2, Math.sin(k * 1.5708) * r1], { capA: 0, capB: 0 })));
        part(group, mat, new THREE.CylinderGeometry(r1, r0, len, 40, 1), wire, m, o.w);
      };
      // ball(group, material, centre, r, { scale }): drawn the way a draughtsman draws a sphere, as three great circles
      const ball = (group, mat, c, r, o = {}) => {
        const g = ring(r, 0), turn = (l, f) => rec(f(l.a), f(l.b), { capA: 0.2, capB: 0.2 });
        const wire = g.concat(g.map((l) => turn(l, (p) => [p[0], p[2], 0])), g.map((l) => turn(l, (p) => [0, p[2], p[0]])));
        part(group, mat, new THREE.SphereGeometry(r, 40, 26), wire, new THREE.Matrix4().compose(v3(c), new THREE.Quaternion(), new THREE.Vector3(...(o.scale || [1, 1, 1]))), o.w);
      };

      // restrained, real materials. buildable() gives each one the section plane.
      const woodTex = (base, dark, seed) => K.canvasTex(512, 1024, (ctx, w, h) => {
        const r = K.rng(seed); ctx.fillStyle = base; ctx.fillRect(0, 0, w, h);
        for (let i = 0; i < 260; i++) { const x = r() * w, a = r.range(0.03, 0.16), wob = r.range(2, 14), ph = r() * 6; ctx.strokeStyle = r() < 0.75 ? `rgba(${dark},${a})` : `rgba(255,236,200,${a * 0.7})`; ctx.lineWidth = r.range(0.6, 3.2); ctx.beginPath(); for (let y = 0; y <= h; y += 24) ctx.lineTo(x + Math.sin(y / 170 + ph) * wob, y); ctx.stroke(); }
      });
      const std = (color, roughness, o = {}) => B.buildable(new THREE.MeshStandardMaterial(Object.assign({ color, roughness, metalness: 0 }, o)));
      const MAT = {
        oak: std('#ffffff', 0.5, { map: woodTex('#cda671', '120,78,36', 3) }),
        steel: std('#e9e8e4', 0.42, { metalness: 0.15 }),
        wool: std('#bb5a31', 0.95),
        shell: std('#25282e', 0.5),
        alu: std('#b4b8bf', 0.36, { metalness: 0.9 }),
        plaster: std('#ebe6dc', 0.72),
        graphite: std('#1d2025', 0.42),
        key: std('#d9d8d3', 0.6),
        cream: std('#efe9dc', 0.5),
        paperWhite: std('#f3f1ea', 0.8),
        mug: std('#d9643a', 0.35),
        glow: B.buildable(new THREE.MeshStandardMaterial({ color: '#2a2622', emissive: '#ffcf8a', emissiveIntensity: 0 })),
      };

      // desk: 1300 x 700, top at 740
      box('desk', MAT.oak, [0.55, 0.725, 0], [0.7, 0.03, 1.3], { r: 0.006 });
      for (const u of [0.25, 0.85]) for (const z of [-0.6, 0.6]) box('desk', MAT.steel, [u, 0.355, z], [0.045, 0.71, 0.045], { r: 0.006, w: LIGHT });
      for (const z of [-0.6, 0.6]) box('desk', MAT.steel, [0.55, 0.685, z], [0.555, 0.05, 0.025], { r: 0.004, w: LIGHT });
      box('desk', MAT.steel, [0.85, 0.685, 0], [0.025, 0.05, 1.155], { r: 0.004, w: LIGHT });

      // on the desk: monitor (top edge at 1260 = eye level), keyboard, mouse, notebook, mug
      const EYE = 1.26;
      box('items', MAT.graphite, [0.7, EYE - 0.165, 0], [0.028, 0.33, 0.58], { r: 0.008 });
      box('items', MAT.alu, [0.735, 0.9, 0], [0.022, 0.3, 0.07], { r: 0.006, rot: [0, 0, -0.1], w: LIGHT });
      box('items', MAT.alu, [0.745, 0.746, 0], [0.19, 0.012, 0.27], { r: 0.005, w: LIGHT });
      box('items', MAT.key, [0.345, 0.749, 0], [0.13, 0.018, 0.4], { r: 0.006, w: LIGHT });
      ball('items', MAT.key, [0.35, 0.752, -0.33], 0.034, { scale: [1.5, 0.55, 1], w: LIGHT });
      box('items', MAT.paperWhite, [0.52, 0.748, -0.44], [0.21, 0.016, 0.15], { r: 0.004, rot: [0, 0.14, 0], w: LIGHT });
      rod('items', MAT.mug, [0.5, 0.74, 0.3], [0.5, 0.83, 0.3], 0.036, 0.04, { w: LIGHT });

      // chair: seat at 500, five-star base
      box('chair', MAT.wool, [0.14, 0.465, 0], [0.46, 0.07, 0.46], { r: 0.03 });
      box('chair', MAT.wool, [-0.185, 0.83, 0], [0.055, 0.5, 0.42], { r: 0.026, rot: [0, 0, 0.13] });
      box('chair', MAT.shell, [0.1, 0.415, 0], [0.22, 0.035, 0.2], { r: 0.008, w: LIGHT });
      rod('chair', MAT.shell, [-0.02, 0.41, 0], [-0.17, 0.47, 0], 0.02, 0.02, { w: LIGHT });
      rod('chair', MAT.shell, [-0.17, 0.47, 0], [-0.165, 0.66, 0], 0.02, 0.02, { w: LIGHT });
      rod('chair', MAT.alu, [0.12, 0.11, 0], [0.12, 0.4, 0], 0.03, 0.022, { w: LIGHT });
      rod('chair', MAT.shell, [0.12, 0.07, 0], [0.12, 0.135, 0], 0.05, 0.045, { w: LIGHT });
      for (let k = 0; k < 5; k++) {
        const a = k * 1.2566 + 0.31, cu = 0.12 + Math.cos(a) * 0.3, cz = Math.sin(a) * 0.3;
        rod('chair', MAT.alu, [0.12 + Math.cos(a) * 0.04, 0.105, Math.sin(a) * 0.04], [cu, 0.07, cz], 0.022, 0.016, { w: LIGHT });
        ball('chair', MAT.shell, [cu, 0.03, cz], 0.03, { w: LIGHT });
      }

      // lamp, at the far corner
      rod('lamp', MAT.cream, [0.76, 0.74, 0.5], [0.76, 0.762, 0.5], 0.085, 0.08, { w: LIGHT });
      rod('lamp', MAT.cream, [0.76, 0.76, 0.5], [0.85, 1.07, 0.48], 0.011, 0.011, { w: LIGHT });
      ball('lamp', MAT.alu, [0.85, 1.07, 0.48], 0.02, { w: LIGHT });
      rod('lamp', MAT.cream, [0.85, 1.07, 0.48], [0.6, 1.29, 0.4], 0.011, 0.011, { w: LIGHT });
      const LAMP_A = [0.61, 1.3, 0.405], LAMP_B = [0.5, 1.2, 0.34];
      rod('lamp', MAT.cream, LAMP_A, LAMP_B, 0.03, 0.09);
      ball('lamp', MAT.glow, [0.52, 1.218, 0.352], 0.034, { w: FINE });

      // the sitter: a drafting mannequin of bars and ball joints, posed the way the three notes say
      const ELBOW = [0.02, 0.79], KNEE = [0.43, 0.57];
      ball('figure', MAT.plaster, [0.025, EYE + 0.005, 0], 0.105, { scale: [1, 1.1, 0.92] });
      ball('figure', MAT.plaster, [0.128, EYE - 0.02, 0], 0.022, { w: LIGHT });                                 // a nose, so the head has a front
      rod('figure', MAT.plaster, [0, 1.07, 0], [0.012, 1.17, 0], 0.045, 0.042, { w: LIGHT });
      rod('figure', MAT.plaster, [0, 0.6, 0], [0, 1.07, 0], 0.1, 0.108, { oval: [1, 1.6] });
      ball('figure', MAT.plaster, [0, 0.585, 0], 0.115, { scale: [1.05, 0.8, 1.45], w: LIGHT });
      for (const z of [-1, 1]) {
        ball('figure', MAT.plaster, [0, 1.035, z * 0.215], 0.056, { w: LIGHT });
        rod('figure', MAT.plaster, [0, 1.035, z * 0.215], [ELBOW[0], ELBOW[1], z * 0.215], 0.046, 0.04);
        ball('figure', MAT.plaster, [ELBOW[0], ELBOW[1], z * 0.215], 0.046, { w: LIGHT });
        rod('figure', MAT.plaster, [ELBOW[0], ELBOW[1], z * 0.215], [0.275, 0.79, z * 0.2], 0.04, 0.031);
        box('figure', MAT.plaster, [0.335, 0.786, z * 0.195], [0.105, 0.034, 0.072], { r: 0.015, w: LIGHT });
        rod('figure', MAT.plaster, [0.02, 0.57, z * 0.09], [KNEE[0], KNEE[1], z * 0.09], 0.07, 0.058);
        ball('figure', MAT.plaster, [KNEE[0], KNEE[1], z * 0.09], 0.06, { w: LIGHT });
        rod('figure', MAT.plaster, [KNEE[0], KNEE[1], z * 0.09], [0.43, 0.095, z * 0.09], 0.055, 0.04);
        ball('figure', MAT.plaster, [0.43, 0.085, z * 0.09], 0.044, { w: LIGHT });
        box('figure', MAT.plaster, [0.49, 0.04, z * 0.09], [0.25, 0.075, 0.095], { r: 0.03 });
      }

      // ---------------------------------------------------------------- lift groups: solids, depth shells and edges
      const rig = {};
      for (const name of GROUPS.slice(1)) { rig[name] = new THREE.Group(); scene.add(rig[name]); }
      for (const p of parts) {
        const solid = new THREE.Mesh(p.geo, p.mat); solid.castShadow = solid.receiveShadow = true; solid.customDepthMaterial = B.cutShadow; solid.userData.solid = true;
        const shell = new THREE.Mesh(p.geo, B.depthOnly); shell.renderOrder = 2;
        rig[p.group].add(solid, shell);
      }
      // the monitor's picture: a quiet page, lit from behind
      const screenTex = K.canvasTex(1024, 560, (ctx, w, h) => {
        ctx.fillStyle = '#e9eef5'; ctx.fillRect(0, 0, w, h); ctx.fillStyle = '#c9d3e2'; ctx.fillRect(0, 0, w, 54); ctx.fillStyle = '#f8fafc'; ctx.fillRect(250, 90, 524, 470);
        ctx.fillStyle = '#9fb0c8'; ctx.fillRect(300, 140, 300, 26); ctx.fillStyle = '#c3cedd'; for (let i = 0; i < 9; i++) ctx.fillRect(300, 200 + i * 38, i % 4 === 3 ? 260 : 424, 14);
        ctx.fillStyle = '#b9c6d8'; for (let i = 0; i < 6; i++) ctx.fillRect(40, 100 + i * 46, 150, 18); ctx.fillStyle = '#e07a45'; ctx.fillRect(40, 400, 110, 30);
      });
      const screen = new THREE.Mesh(new THREE.PlaneGeometry(0.555, 0.305).rotateY(-Math.PI / 2).translate(0.7 - 0.0148, EYE - 0.165, 0).applyMatrix4(PLACE), B.buildable(new THREE.MeshBasicMaterial({ map: screenTex, color: new THREE.Color(0.7, 0.72, 0.76) })));
      rig.items.add(screen);

      const edges = {};
      for (const name of GROUPS.slice(1)) edges[name] = B.penOrder(parts.filter((p) => p.group === name).flatMap((p) => p.wire), T.draw[name][0], T.draw[name][1]);
      const allEdges = Object.values(edges).flat();
      const wireMesh = ink(allEdges, { order: 4 });                                        // edges in front of every surface
      const hiddenMesh = inkMesh(wireMesh.geometry, { hidden: true, order: 3, opacity: 0.5, width: 0.62, tip: 0 });   // the same edges where something is in front of them
      const planMesh = inkMesh(wireMesh.geometry, { flat: true, order: 1, opacity: 0, bias: 0.006, width: 0.7, tip: 0 });   // and their plan, left behind on the sheet

      // ---------------------------------------------------------------- the sheet and everything printed on it
      const EAST = 1.84, SOUTH = 0.97, BX = 1.77, BZ = 0.92;                                   // paper edges, border
      B.sheet({ color: PAPER, rect: [-13, -14, EAST, SOUTH], fold: [EAST, SOUTH, 1.32, 1.33] });
      const table = new THREE.Mesh(new THREE.PlaneGeometry(60, 60).rotateX(-Math.PI / 2).translate(0, -0.006, 0), new THREE.MeshStandardMaterial({ map: woodTex('#3a2414', '10,5,2', 9), roughness: 0.55 }));
      table.material.map.repeat.set(6, 3); table.material.map.wrapS = table.material.map.wrapT = THREE.RepeatWrapping; table.receiveShadow = true; scene.add(table);
      // the sheet's own thin shadow on the table, along its two visible edges
      const edgeShade = (alongX) => { const t = K.canvasTex(64, 64, (ctx, w, h) => { const g = alongX ? ctx.createLinearGradient(0, 0, 0, h) : ctx.createLinearGradient(0, 0, w, 0); g.addColorStop(0, 'rgba(0,0,0,0.6)'); g.addColorStop(1, 'rgba(0,0,0,0)'); ctx.fillStyle = g; ctx.fillRect(0, 0, w, h); });
        const m = new THREE.Mesh((alongX ? new THREE.PlaneGeometry(16, 0.07).translate(EAST - 8 + 0.07, -0.035, 0) : new THREE.PlaneGeometry(0.07, 16).translate(0.035, 8, 0)).rotateX(-Math.PI / 2), new THREE.MeshBasicMaterial({ map: t, transparent: true, depthWrite: false }));
        m.position.set(alongX ? 0 : EAST, -0.003, SOUTH); scene.add(m); };
      edgeShade(true); edgeShade(false);

      const onSheet = (x0, z0) => (x, y) => [x0 + x, 0, z0 - y];                            // lettering that reads from the south
      const print = [];
      for (let x = BX - 0.25; x > -12; x -= 0.25) print.push(rec([x, 0, BZ], [x, 0, -13], { w: FINE, alpha: Math.abs(Math.round(x) - x) < 0.01 ? 0.36 : 0.16 }));
      for (let z = BZ - 0.25; z > -13; z -= 0.25) print.push(rec([BX, 0, z], [-12, 0, z], { w: FINE, alpha: Math.abs(Math.round(z) - z) < 0.01 ? 0.36 : 0.16 }));
      print.push(rec([BX, 0, -13], [BX, 0, BZ], { w: HEAVY }), rec([BX, 0, BZ], [-12, 0, BZ], { w: HEAVY }));
      // title block, hard in the corner of the border as on any drawing
      const TB = [0.9, 0.57], RULE = 0.825, MID = 1.335, TITLE_Z = 0.64, TITLE_GAP = 0.122;
      print.push(rec([TB[0], 0, BZ], [TB[0], 0, TB[1]], { w: HEAVY }), rec([TB[0], 0, TB[1]], [BX, 0, TB[1]], { w: HEAVY }), rec([TB[0], 0, RULE], [BX, 0, RULE]), rec([MID, 0, RULE], [MID, 0, BZ]));
      TITLE.forEach((s, i) => print.push(...letter(s, { size: 0.082, w: 4.2, map: onSheet(TB[0] + 0.055, TITLE_Z + i * TITLE_GAP) })));
      BLOCK.forEach((s, i) => print.push(...letter(s, { size: 0.032, w: 1.7, alpha: 0.9, step: 0.14, map: onSheet((i ? MID : TB[0]) + 0.045, (RULE + BZ) / 2) })));
      // plan dimensions and the centre line through sitter and screen
      print.push(...dimension([CX - 0.9, 0, 0.65], [CX - 0.9, 0, -0.65], { off: [-0.2, 0, 0], text: '1300', at: -1.0 }));
      print.push(...dimension([CX - 0.9, 0, 0.65], [CX - 0.2, 0, 0.65], { off: [0, 0, 0.13], flip: true, text: '700', at: 0.15 }));
      print.push(rec([CX - 1.02, 0, 0], [CX + 0.42, 0, 0], { dash: 0.11, dashStyle: 2, alpha: 0.75, at: 1.0, dur: 0.6 }));
      print.push(...letter('DESK', { size: 0.045, w: 1.9, at: -0.9, per: 0.06, map: onSheet(CX - 0.78, 0.5) }));
      const printMesh = ink(print, { order: 1, bias: 0.006 });

      // ---------------------------------------------------------------- the three notes: a mark on the model, a leader, a numbered balloon, two lines of lettering
      const marks = [], [n1, n2, n3] = T.marks.map((t) => t - 0.2);
      // 1 a level line from the eye to the top edge of the screen
      marks.push(rec(W(0.13, EYE, 0), W(0.82, EYE, 0), { w: 2.6, dash: 0.1, dashStyle: 2, at: n1 + 0.3, dur: 0.5 }), arrow(W(0.13, EYE, 0), [1, 0, 0], { at: n1 + 0.7 }), arrow(W(0.686, EYE, 0), [-1, 0, 0], { at: n1 + 0.7 }));
      marks.push(rec(W(0.686, EYE - 0.07, 0), W(0.686, EYE + 0.07, 0), { w: 2.2, at: n1 + 0.65, dur: 0.15 }));
      // 2 the right angle at the elbow, drawn just outside the near arm
      const EZ = -0.265, R = 0.2;
      marks.push(rec(W(ELBOW[0], ELBOW[1], EZ), W(ELBOW[0], ELBOW[1] + 0.36, EZ), { w: 2.2, dash: 0.08, dashStyle: 2, at: n2 + 0.3, dur: 0.3 }), rec(W(ELBOW[0], ELBOW[1], EZ), W(ELBOW[0] + 0.4, ELBOW[1], EZ), { w: 2.2, dash: 0.08, dashStyle: 2, at: n2 + 0.3, dur: 0.3 }));
      for (let k = 0; k < 14; k++) { const a0 = k / 14 * 1.5708, a1 = (k + 1) / 14 * 1.5708; marks.push(rec(W(ELBOW[0] + Math.sin(a0) * R, ELBOW[1] + Math.cos(a0) * R, EZ), W(ELBOW[0] + Math.sin(a1) * R, ELBOW[1] + Math.cos(a1) * R, EZ), { w: HEAVY, capA: 0.2, capB: 0.2, at: n2 + 0.5 + k * 0.02, dur: 0.05 })); }
      marks.push(rec(W(ELBOW[0] + 0.055, ELBOW[1], EZ), W(ELBOW[0] + 0.055, ELBOW[1] + 0.055, EZ), { w: 2, at: n2 + 0.8, dur: 0.1 }), rec(W(ELBOW[0] + 0.055, ELBOW[1] + 0.055, EZ), W(ELBOW[0], ELBOW[1] + 0.055, EZ), { w: 2, at: n2 + 0.85, dur: 0.1 }));
      // 3 the contact patch under each foot, hatched on the sheet
      for (const z of [-1, 1]) {
        const u0 = 0.35, u1 = 0.63, z0 = z * 0.09 - 0.06, z1 = z * 0.09 + 0.06, y = 0.004;
        [[u0, z0, u1, z0], [u1, z0, u1, z1], [u1, z1, u0, z1], [u0, z1, u0, z0]].forEach((q, i) => marks.push(rec(W(q[0], y, q[1]), W(q[2], y, q[3]), { w: 2.6, at: n3 + 0.3 + i * 0.05, dur: 0.12 })));
        for (let k = 1; k < 8; k++) { const s = u0 + k * 0.05; marks.push(rec(W(Math.min(s, u1), y, z0 + Math.max(0, s - u1)), W(Math.max(s - 0.12, u0), y, z0 + Math.min(0.12, s - u0)), { w: 1.5, at: n3 + 0.5 + k * 0.03, dur: 0.1 })); }
      }
      // one dimension that stands up in space: the height of the desk, set out square to the final view
      marks.push(...dimension([CX - 0.9, 0, 0.65], [CX - 0.9, 0.74, 0.65], { off: [-0.175, 0, 0.097], text: '740', at: T.lift + 1.35 }));
      const NOTE_AT = [[0.74, 1.42, -0.34], [0.84, 0.95, -0.4], [0.82, 0.3, 0.1]];       // where each balloon floats
      const NOTE_TO = [W(0.3, EYE, 0), W(ELBOW[0] + 0.145, ELBOW[1] + 0.145, EZ), W(0.4, 0.004, -0.15)];
      NOTE_TO.forEach((p, i) => { const d = B.unit(B.sub(p, NOTE_AT[i])), at = T.marks[i]; marks.push(rec(NOTE_AT[i], p, { at, dur: 0.3, capA: 0 }), arrow(p, d, { at: at + 0.24, len: 0.06, head: 9 })); });
      ink(B.cased(marks), { order: 6, onTop: true });
      // the angle itself, as a pale quarter disc
      const wedge = new THREE.Mesh(new THREE.BufferGeometry().setFromPoints([W(ELBOW[0], ELBOW[1], EZ)].concat(Array.from({ length: 17 }, (_, k) => W(ELBOW[0] + Math.sin(k / 16 * 1.5708) * R, ELBOW[1] + Math.cos(k / 16 * 1.5708) * R, EZ))).map(v3)),
        new THREE.MeshBasicMaterial({ color: new THREE.Color(INK), transparent: true, opacity: 0, depthTest: false, side: THREE.DoubleSide }));
      wedge.geometry.setIndex(Array.from({ length: 16 }, (_, k) => [0, k + 1, k + 2]).flat()); wedge.renderOrder = 5; scene.add(wedge);

      // Lettering that leaves the sheet. A flyer is a small group that starts printed flat on the paper and is carried
      // to a place in space while it turns to face the lens, so the same strokes serve as print and as a label.
      const FLAT = new THREE.Quaternion().setFromEuler(new THREE.Euler(-Math.PI / 2, 0, 0)), flyers = [];
      const flyer = (home, away, at, dur, strokes) => { const g = new THREE.Group(); scene.add(g); ink(strokes, { parent: g, order: 8, onTop: true }); flyers.push({ g, home: v3(home), away: v3(away), at, dur, pulse: T.marks[flyers.length] }); return g; };
      const SIZE = 0.086, RB = 0.085;
      NOTES.forEach((lines, i) => {
        const txt = letter(String(i + 1), { size: 0.09, w: 4.8, ink: 1, align: 'center' });
        lines.forEach((s, k) => txt.push(...letter(s, { size: SIZE, squeeze: 0.84, w: 4.2, map: (x, y) => [RB + 0.05 + x, y + (0.5 - k) * SIZE * 1.6, 0] })));
        const g = flyer([0.75, 0.004, -0.54 + i * 0.36], NOTE_AT[i], T.notes[i], T.fly, txt);
        const disc = new THREE.Mesh(new THREE.CircleGeometry(RB, 48), new THREE.MeshBasicMaterial({ color: new THREE.Color(INK), depthTest: false, transparent: true })); disc.renderOrder = 7; g.add(disc);
      });


      // ---------------------------------------------------------------- light
      K.env(S, { top: '#9aa6bd', horizon: '#7f8aa3', bottom: '#1c2c4d', intensity: 0.3, panels: [{ pos: [9, 9, -7], size: [9, 9], color: '#fff2df', power: 3.5 }, { pos: [-8, 5, 9], size: [8, 6], color: '#cfe0ff', power: 1.2 }] });
      K.spot(S, { pos: [5.2, 8.6, -4.6], target: [0, 0.3, 0.1], color: '#fff3e2', power: 420, angle: 0.36, penumbra: 1, soft: 0.55, mapSize: 4096, near: 4, far: 30 });
      K.spot(S, { pos: [-5, 6, 9], target: [0, 0.5, 0], color: '#dfe9ff', power: 60, angle: 0.5, penumbra: 1, shadow: false });
      const bulbAt = v3([0.5, 1.19, 0.34]).applyMatrix4(PLACE), bulb = new THREE.PointLight('#ffc98a', 0, 3, 2); bulb.position.copy(bulbAt); scene.add(bulb);

      // ---------------------------------------------------------------- camera: one crane move. [target x, y, z, elevation, azimuth, frame height at the target, lens (tan of half the fov)]
      const CAM = K.path([
        [0, [0.36, 0, 0.1, 89.6, 0, 1.7, 0.085]],
        [1.75, [0.36, 0.03, 0.1, 79, 3, 1.74, 0.095]],
        [2.85, [0.44, 0.46, 0.08, 52, 14, 2.12, 0.14]],
        [3.9, [0.4, 0.62, -0.1, 31, 24, 2.12, 0.18]],
        [8, [0.4, 0.6, -0.12, 25, 29, 2.0, 0.2]],
      ]);
      const RAD = Math.PI / 180, wedgeInk = new THREE.Color(INK), wedgeBlue = new THREE.Color('#2f6fe0');
      const liftEase = (x) => (x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2);

      return {
        update(t) {
          U.uTime.value = t;
          // each part rises out of its plan
          for (const name of GROUPS.slice(1)) { const k = Math.max(1e-4, liftEase(K.seg(t, liftAt(name), liftAt(name) + T.liftDur))); U.uLift.value[gid(name)] = k; rig[name].scale.y = k; }
          // the section plane climbs through the model
          // (its time is snapped to the frame, so the cut face is not smeared by motion blur and its hatching stays crisp)
          const tf = Math.round(t * 30) / 30, f = K.seg(tf, T.fill[0], T.fill[1], (x) => x * x * (3 - 2 * x)), fill = K.lerp(-0.25, 1.75, f);
          U.uFill.value = t < T.fill[0] ? -10 : fill;
          planMesh.material.uniforms.uOpacity.value = 0.34 * K.seg(t, T.lift, T.lift + 0.25) * K.lerp(1, 0.55, f);
          for (const name of GROUPS.slice(1)) rig[name].children.forEach((m) => { if (m.userData.solid) m.visible = t >= T.fill[0]; });
          screen.visible = t >= T.fill[0];
          wireMesh.material.uniforms.uGhost.value = 0.34; hiddenMesh.material.uniforms.uGhost.value = 0.1;
          printMesh.material.uniforms.uOpacity.value = K.lerp(1, 0.6, K.seg(t, T.lift, T.lift + 1.5));
          // the lamp comes on
          const on = K.seg(t, T.lamp, T.lamp + 0.5, K.ease.out);
          MAT.glow.emissiveIntensity = 5 * on; bulb.intensity = 0.9 * on;

          const [tx, ty, tz, el, az, H, lens] = CAM(t), e = Math.min(el, 89.6) * RAD, a = az * RAD, d = H / (2 * lens);
          K.look(camera, [tx + d * Math.sin(a) * Math.cos(e), ty + d * Math.sin(e), tz + d * Math.cos(a) * Math.cos(e)], [tx, ty, tz], 2 * Math.atan(lens) / RAD);
          camera.updateMatrixWorld();
          wedge.material.opacity = K.lerp(0.3, 0.5, f) * K.seg(t, T.marks[1] + 0.3, T.marks[1] + 0.6); wedge.material.color.copy(wedgeInk).lerp(wedgeBlue, f);   // white on the drawing, blue on the plaster arm
          for (const n of flyers) { const k = K.smooth(n.at, n.at + n.dur, t); n.g.position.lerpVectors(n.home, n.away, k); n.g.quaternion.copy(FLAT).slerp(camera.quaternion, k); n.g.scale.setScalar(1 + 0.1 * Math.sin(Math.PI * k) + 0.14 * Math.sin(Math.PI * K.seg(t, n.pulse, n.pulse + 0.4))); }
        },
      };
    },

    sound(k) {
      k.bed('room', 0, 8, { gain: 0.9 });
      // the plotter: pen on paper while a part is drawn, a short servo chirp as the pen moves on to the next part
      for (const [t0, t1] of Object.values(T.draw)) {
        if (t1 <= 0.1) continue;
        const a = Math.max(0, t0);
        k.play('stroke', a, { dur: t1 - a, material: 'pen', gain: 1.1, pan: -0.25 });
        if (t0 > 0) k.withMinor(() => k.tone(t0, { f: [[0, 520], [0.09, 780]], type: 'triangle', dur: 0.1, gain: 0.05, pan: -0.25 }));
      }
      k.play('tick', 0.53, { gain: 0.5, pan: -0.4 }); k.play('tick', 0.58, { gain: 0.4, pan: -0.2 });      // the arrowheads of the 700 dimension
      // the sheet tips over and each part rises out of its plan: a click as it lets go, a soft rising tone under it
      k.play('whoosh', T.lift - 0.15, { gain: 0.3 });
      Object.keys(LIFT_ORDER).forEach((name, i) => {
        const t = liftAt(name);
        k.play('click', t, { gain: 0.5, pan: -0.3 + i * 0.1 });
        k.tone(t, { f: [[0, 196 * Math.pow(1.122, i)], [T.liftDur, 392 * Math.pow(1.122, i)]], type: 'sine', dur: T.liftDur, gain: 0.03, a: 0.35, verb: 0.3 });
      });
      // the notes peel off the paper one after another; then each is tied to the model with a pop and a pen stroke
      T.notes.forEach((t, i) => k.play('paper', t, { dur: 0.3, gain: 0.6, pan: 0.45 }));
      T.marks.forEach((t, i) => { k.play('pop', t, { gain: 0.8, f: 500 + i * 90, pan: 0.45 }); k.play('stroke', t + 0.02, { dur: 0.36, material: 'pen', gain: 1.0, pan: 0.1 }); });
      // the section plane climbs: a narrow band of noise rising with it, and a low tone that lands as the last part closes
      const [f0, f1] = T.fill, fd = f1 - f0;
      k.noise(f0, { dur: fd, color: 'pink', filter: 'bandpass', f: [[0, 350], [fd, 2400]], q: 3.5, gain: 0.2, a: 0.35, verb: 0.25 });
      k.tone(f0, { f: [[0, 98], [fd, 196]], type: 'triangle', dur: fd, gain: 0.08, a: 0.5 });
      k.play('thud', f1 - 0.06, { gain: 0.2 });
      // the lamp
      k.play('click', T.lamp, { gain: 1.0, pan: -0.3 });
      k.play('ding', T.lamp + 0.04, { gain: 0.4, f: 'A5', pan: -0.3 });
    },
  });
}
```