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 "Painted" 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.
- Load these Google Fonts before building (the kit awaits the specs in style.fonts): Caveat

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 a story, 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: Painted
Family: Filters over 3D
Use: story
Look: A filter, not a set: any lit 3D scene comes out as an oil painting on canvas, with thick directional brush dabs, broken colour, ridges of paint that catch a raking light, linen weave showing through the thin darks and a ragged unpainted edge. Here it carries a golden-hour countryside: a wheat field, a windmill, a bakery cottage.
Motion: Things happen inside the painting and each one causes the next: a gust runs through the wheat as a wave of bent stalks, reaches the sails and spins them up, the millstone turns with them, flour pours into a sack and puffs, a loaf is slid onto a sill. Brush dabs are fixed to the things they describe, so they travel with stalks, sails and sacks instead of boiling; the canvas weave stays still. Each caption is brushed on in under half a second when its beat starts and stays until the next one.
Light: One low golden sun from behind and to the side, long shadows, cool blue-violet sky light in every shadow, and small warm lamps (a doorway, a window, a lantern) wherever the story needs the eye. Warm light against cool shadow is the whole palette.
Materials: Rough simple shapes with no detail of their own, each big enough to survive a brush nine pixels wide. Every surface carries a field of brush dabs that varies its colour, tilts its shading and adds bristle streaks; the painterly filter turns that into strokes.
Camera: One continuous shot at walking height, like a picture book without page turns: it travels with the wind from the field toward the mill, cranes down to the door and drifts slowly across it while the stone works, then glides after the flour dust along the track and pushes in on the window sill, where it stops and holds on the thing that was made. Everything sharp, like a canvas. Each stop is composed like a painting, with the subject large in frame.
Finish: Anisotropic Kuwahara brush filter, then a paint-relief and canvas-weave pass lit from the upper left, which also letters the caption on a bare corner of the linen. Soft bloom on the sun and the lamps before the filter, so the glow is painted too. No grain, no vignette.
Good for: Stories, journeys and processes that happen in a place, told as a picture book: how something is made, where something comes from, a day in a life, a chain of cause and effect. Each beat is one stop in the landscape where one thing visibly happens and sets off the next; something that travels (wind, a road, a river, a character) leads the camera from stop to stop; one short caption per beat sits in the bare corner of the canvas and names what is happening. The filter carries any 3D scene, so the world can be a farm, a city, a workshop or a room, as long as each subject is a big simple shape.

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 };
  };
})();
```

REFERENCE STYLE SOURCE (styles/painted.js; its sample topic was "From wind to bread: a gust crosses a wheat field and turns the windmill, the millstone grinds flour, and a loaf ends up on the bakery sill")
```js
{
  // ---------------------------------------------------------------- content (change this for a new topic)
  // A picture-book journey: one slow camera move through a landscape, four beats that cause each other, and one short
  // caption per beat lettered in the bare corner of the canvas. Here: from wind to bread.
  const DURATION = 12;
  const BEATS = [                   // `at` = when the beat starts and its caption is brushed on (the first is there on frame one)
    { at: 0.0, text: 'Wind crosses the wheat.' },
    { at: 2.4, text: 'The same wind turns the sails.' },
    { at: 4.9, text: 'The stone grinds grain into flour.' },
    { at: 8.45, text: 'And the flour becomes bread.' },
  ];
  const WRITE = 0.45;               // seconds to brush a whole caption on; it then stays up until the next beat starts
  // the wind is the thread: each gust is a front that travels across the land at GUST_SPEED. `at` = the moment it
  // reaches the mill; wheat, grass, trees, sails, flour dust and smoke all read the same gusts.
  const GUSTS = [{ at: 2.5, amp: 1.0 }, { at: 6.7, amp: 0.6 }];
  const GUST_SPEED = 5;             // world units per second
  const SAILS = { idle: 0.2, run: 1.45, spinUp: 1.5, phase: 0.35 };   // rad/s before and after the first gust arrives
  const STONE_GEAR = 3;             // the millstone is geared to the sails, so it speeds up with them
  const DOOR_AT = 5.1, DOOR_LEAVE = 6.9;   // the camera reaches the mill door, drifts slowly across it while the stone and the flour do their part, and moves on
  const STOP = 11.1;                // the camera reaches the window sill and holds on the loaf until the end
  const FLOUR_ON = 4.4;             // flour starts to run from the spout into the open sack
  const SACK_FULL = 6.5;            // the sack is full: a puff of flour dust, which the second gust carries toward the bakery
  const OVEN_ON = 8.3;              // the bakery window warms up
  const LOAF_OUT = 9.8, LOAF_DOWN = 10.5;   // a baker's peel slides the loaf out of the window and leaves it on the sill

  // one schedule for picture and sound
  const sailAngle = (t) => {        // closed form, so any frame can be rendered on its own
    const u = (t - GUSTS[0].at) / SAILS.spinUp, ramp = u <= 0 ? 0 : u < 1 ? u * u * u - u * u * u * u / 2 : u - 0.5;   // integral of a smooth step
    return SAILS.phase + SAILS.idle * t + (SAILS.run - SAILS.idle) * SAILS.spinUp * ramp;
  };
  function sailPasses() {           // the moments a sail points straight down
    const out = []; let last = Math.floor(sailAngle(0) / (Math.PI / 2));
    for (let t = 0; t < DURATION; t += 0.005) { const n = Math.floor(sailAngle(t) / (Math.PI / 2)); if (n !== last) { out.push(t); last = n; } }
    return out;
  }
  function lettering() {            // every caption word is one pull of a small brush: { beat, word, k, t0, dur }
    const out = [];
    BEATS.forEach((b, beat) => { let t = b.at; b.text.split(' ').forEach((word, k) => { const dur = WRITE * (word.length + 1) / (b.text.length + 1); out.push({ beat, word, k, t0: beat ? t : -1, dur: dur * 0.85 }); t += dur; }); });
    return out;
  }
  const STONE_NEAR = [DOOR_AT - 0.4, DOOR_LEAVE + 0.3];   // while the camera is at the mill door (for the sound of the stones)
  function cogKnocks() {            // every tooth of the cog wheel on the stone's shaft, while we are close enough to hear it
    const out = [], tooth = Math.PI / 5; let last = Math.floor(STONE_GEAR * sailAngle(STONE_NEAR[0] - 0.5) / tooth);
    for (let t = STONE_NEAR[0] - 0.5; t < STONE_NEAR[1] + 0.9; t += 0.004) { const n = Math.floor(STONE_GEAR * sailAngle(t) / tooth); if (n !== last) { out.push(t); last = n; } }
    return out;
  }
  const BIRD_CALLS = [0.4, 0.62, 1.7, 3.6, 3.78];

  // ---------------------------------------------------------------- the paint: two full-screen passes over the finished frame
  // Pass 1, the brush: an anisotropic Kuwahara filter. Around every pixel it looks at eight overlapping pie slices of
  // an ellipse stretched along the local direction of the picture, and keeps the mean colour of the calmest slices.
  // Flat areas become flat dabs, edges stay crisp and strokes run along forms. It has no randomness, so strokes cannot
  // boil: they only change when the picture underneath changes.
  // Alpha carries out the fine detail the brush removed (the bristle streaks painted into the scene's materials), so
  // the second pass can turn it into relief.
  // The filter eats anything thinner than the brush, so the story's subjects have to be big, simple shapes with a clear
  // difference in tone from what is behind them.
  const BRUSH = `
    uniform float radius;      // brush size in pixels
    vec3 px(vec2 o){ return texture2D(tex, vUv + o / res).rgb; }
    void main(){
      // which way does the picture run here? (structure tensor, averaged over the brush footprint)
      vec3 T = vec3(0.0), mean = vec3(0.0); float wsum = 0.0;
      for (int j = -2; j <= 2; j++) for (int i = -2; i <= 2; i++) {
        vec2 o = vec2(float(i), float(j)) * 3.0; float w = exp(-dot(o, o) / 44.0);
        vec3 a = px(o + vec2(1.5, 0.0)), b = px(o - vec2(1.5, 0.0)), c = px(o + vec2(0.0, 1.5)), d = px(o - vec2(0.0, 1.5));
        vec3 gx = a - b, gy = c - d;
        T += w * vec3(dot(gx, gx), dot(gy, gy), dot(gx, gy)); mean += w * (a + b + c + d) * 0.25; wsum += w;
      }
      T /= wsum; mean /= wsum;
      float root = sqrt((T.x - T.y) * (T.x - T.y) + 4.0 * T.z * T.z), l1 = 0.5 * (T.x + T.y + root), l2 = 0.5 * (T.x + T.y - root);
      vec2 dir = vec2(l1 - T.x, -T.z); dir = length(dir) > 1e-6 ? normalize(dir) : vec2(1.0, 0.0);   // along the edge
      float aniso = l1 + l2 > 1e-7 ? (l1 - l2) / (l1 + l2) : 0.0;                                   // 0 = no direction, 1 = a clean line
      float ra = radius * clamp(1.0 + aniso, 0.1, 2.0), rb = radius * clamp(1.0 / (1.0 + aniso), 0.1, 2.0);
      int mx = int(sqrt(ra * ra * dir.x * dir.x + rb * rb * dir.y * dir.y)), my = int(sqrt(ra * ra * dir.y * dir.y + rb * rb * dir.x * dir.x));
      float zeta = 2.0 / radius, eta = (zeta + cos(0.58)) / (sin(0.58) * sin(0.58));
      vec4 m[8]; vec3 s[8];
      for (int k = 0; k < 8; k++) { m[k] = vec4(0.0); s[k] = vec3(0.0); }
      for (int y = -my; y <= my; y++) for (int x = -mx; x <= mx; x++) {
        vec2 o = vec2(float(x), float(y));
        vec2 v = 0.5 * vec2(dot(o, dir) / ra, dot(o, vec2(-dir.y, dir.x)) / rb);
        if (dot(v, v) > 0.25) continue;
        vec3 c = px(o);
        float w[8]; float z, vxx = zeta - eta * v.x * v.x, vyy = zeta - eta * v.y * v.y, sum = 0.0;   // smooth weights for the eight slices
        z = max(0.0, v.y + vxx); w[0] = z * z; z = max(0.0, -v.x + vyy); w[2] = z * z; z = max(0.0, -v.y + vxx); w[4] = z * z; z = max(0.0, v.x + vyy); w[6] = z * z;
        vec2 q = 0.70710678 * vec2(v.x - v.y, v.x + v.y); vxx = zeta - eta * q.x * q.x; vyy = zeta - eta * q.y * q.y;
        z = max(0.0, q.y + vxx); w[1] = z * z; z = max(0.0, -q.x + vyy); w[3] = z * z; z = max(0.0, -q.y + vxx); w[5] = z * z; z = max(0.0, q.x + vyy); w[7] = z * z;
        for (int k = 0; k < 8; k++) sum += w[k];
        float g = exp(-3.125 * dot(v, v)) / sum;
        for (int k = 0; k < 8; k++) { float wk = w[k] * g; m[k] += vec4(c * wk, wk); s[k] += c * c * wk; }
      }
      vec4 acc = vec4(0.0);
      for (int k = 0; k < 8; k++) {
        vec3 mk = m[k].rgb / m[k].w, sd = abs(s[k] / m[k].w - mk * mk);
        float w = 1.0 / (1.0 + pow(8000.0 * (sd.r + sd.g + sd.b), 4.0));                             // calm slices win
        acc += vec4(mk * w, w);
      }
      float lm = dot(mean, vec3(0.3, 0.55, 0.15)), detail = (dot(px(vec2(0.0)), vec3(0.3, 0.55, 0.15)) - lm) / (lm + 0.08);
      gl_FragColor = vec4(acc.rgb / acc.w, 0.5 + clamp(detail, -0.16, 0.16) * 2.5);                  // clamped: bristle streaks count, hard edges do not
    }`;
  // Pass 2, the surface: paint gets thickness (light passages are laid on thick, darks stay thin), the canvas weave
  // shows through where it is thin, and one raking studio light picks out the ridges. The weave and the ragged
  // unpainted edge belong to the canvas, so they stay fixed on screen while the painting moves.
  // The caption lives here too: the painter left the lower left corner of the linen bare and letters one short line on
  // it in dark paint with a small brush. It is added after the big brush, so the words stay sharp, but it still gets
  // the relief and the weave like any other paint.
  const SURFACE = `
    uniform sampler2D uCap;    // all caption lines, one per row (white on black)
    uniform vec4 uCapBox;      // where a line sits on the canvas: x, y of its lower left corner, width, height (px)
    uniform vec2 uCapLine;     // which row is showing, and how many rows there are
    uniform vec2 uCapPatch;    // size of the bare corner (px)
    uniform float uCapShow, uCapAlpha;   // how far the brush has got along the line (px), and the fade when a line is wiped off
    float lum(vec3 c){ return dot(c, vec3(0.3, 0.55, 0.15)); }
    float h21(vec2 p){ p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 45.32); return fract(p.x * p.y); }
    float vnoise(vec2 p){ vec2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f); return mix(mix(h21(i), h21(i + vec2(1, 0)), f.x), mix(h21(i + vec2(0, 1)), h21(i + vec2(1, 1)), f.x), f.y); }
    float weave(vec2 p){       // plain weave: threads go over and under each other in a checker pattern
      vec2 q = p / 4.0, id = floor(q), f = fract(q) - 0.5; float over = mod(id.x + id.y, 2.0);
      float warp = cos(f.x * 3.14159) * (0.55 + 0.45 * cos(f.y * 6.28318)), weft = cos(f.y * 3.14159) * (0.55 + 0.45 * cos(f.x * 6.28318));
      return mix(warp, weft, over) * (0.8 + 0.4 * h21(id * (over > 0.5 ? vec2(0.0, 1.0) : vec2(1.0, 0.0))));
    }
    float ink(vec2 p){         // the caption: wiped in from the left behind a bristly edge, like one pull of a loaded brush
      vec2 q = (p - uCapBox.xy) / uCapBox.zw;
      if (q.x < 0.0 || q.x > 1.0 || q.y < 0.0 || q.y > 1.0) return 0.0;
      float wipe = smoothstep(0.0, 5.0, uCapShow - (p.x - uCapBox.x) - 14.0 * h21(vec2(floor(p.y / 3.0), 7.0)));
      return texture2D(uCap, vec2(q.x, 1.0 - (uCapLine.x + 1.0 - q.y) / uCapLine.y)).r * wipe * uCapAlpha;
    }
    float thickness(vec2 o){ vec4 s = texture2D(tex, vUv + o / res); return 0.25 * lum(s.rgb) + 0.8 * (s.a - 0.5); }
    void main(){
      vec2 p = vUv * res; vec3 c = texture2D(tex, vUv).rgb;
      // the painting stops short of the canvas edge, raggedly, the way a loaded brush runs out
      float edge = min(min(p.x, res.x - p.x), min(p.y, res.y - p.y));
      float painted = smoothstep(0.0, 3.0, edge - 13.0 - 17.0 * vnoise(p * vec2(0.012, 0.11)) - 7.0 * vnoise(p * 0.07));
      // ... and around the bare corner kept for the caption
      float corner = length(max(p - (uCapPatch - 30.0), 0.0)) - 30.0 + 9.0 * vnoise(p * vec2(0.05, 0.012)) + 6.0 * vnoise(p * 0.09);
      painted *= smoothstep(0.0, 3.0, corner);
      float k = ink(p), body = max(painted * smoothstep(0.04, 0.8, lum(c)), k);
      vec2 g = vec2(thickness(vec2(1.5, 0.0)) - thickness(vec2(-1.5, 0.0)), thickness(vec2(0.0, 1.5)) - thickness(vec2(0.0, -1.5))) * painted;
      g += vec2(ink(p + vec2(1.5, 0.0)) - ink(p - vec2(1.5, 0.0)), ink(p + vec2(0.0, 1.5)) - ink(p - vec2(0.0, 1.5))) * 0.22;
      vec2 gw = vec2(weave(p + vec2(0.8, 0.0)) - weave(p - vec2(0.8, 0.0)), weave(p + vec2(0.0, 0.8)) - weave(p - vec2(0.0, 0.8)));
      vec3 n = normalize(vec3(-(g * 1.6 + gw * mix(0.3, 0.07, body)), 1.0));                         // relief of paint plus canvas
      vec3 L = normalize(vec3(-0.5, 0.62, 0.6));                                                    // studio light from the upper left
      float diff = dot(n, L) / L.z, spec = pow(max(dot(n, normalize(L + vec3(0.0, 0.0, 1.0))), 0.0), 48.0);
      c = mix(vec3(0.90, 0.86, 0.77) * (0.97 + 0.05 * vnoise(p * 0.02)), c, painted);                // primed linen outside the picture
      c = mix(c, vec3(0.17, 0.11, 0.24) * (0.9 + 0.3 * vnoise(p * vec2(0.5, 0.08))), k);             // caption paint: the violet of the shadows
      c = c * mix(1.0, diff, 0.5) + spec * 0.12 * body;
      c *= 1.0 - 0.07 * (1.0 - body) * (1.0 - weave(p));                                             // thin paint sinks into the weave
      gl_FragColor = vec4(c, 1.0);
    }`;
  const CAP = { font: '700 64px "Caveat"', x: 50, y: 34, row: 96, width: 1024 };   // caption size and place on the 1920 x 1080 canvas

  registerStyle({
    id: 'painted',
    name: 'Painted',
    family: 'Filters over 3D',
    look: 'A filter, not a set: any lit 3D scene comes out as an oil painting on canvas, with thick directional brush dabs, broken colour, ridges of paint that catch a raking light, linen weave showing through the thin darks and a ragged unpainted edge. Here it carries a golden-hour countryside: a wheat field, a windmill, a bakery cottage.',
    motion: 'Things happen inside the painting and each one causes the next: a gust runs through the wheat as a wave of bent stalks, reaches the sails and spins them up, the millstone turns with them, flour pours into a sack and puffs, a loaf is slid onto a sill. Brush dabs are fixed to the things they describe, so they travel with stalks, sails and sacks instead of boiling; the canvas weave stays still. Each caption is brushed on in under half a second when its beat starts and stays until the next one.',
    light: 'One low golden sun from behind and to the side, long shadows, cool blue-violet sky light in every shadow, and small warm lamps (a doorway, a window, a lantern) wherever the story needs the eye. Warm light against cool shadow is the whole palette.',
    materials: 'Rough simple shapes with no detail of their own, each big enough to survive a brush nine pixels wide. Every surface carries a field of brush dabs that varies its colour, tilts its shading and adds bristle streaks; the painterly filter turns that into strokes.',
    camera: 'One continuous shot at walking height, like a picture book without page turns: it travels with the wind from the field toward the mill, cranes down to the door and drifts slowly across it while the stone works, then glides after the flour dust along the track and pushes in on the window sill, where it stops and holds on the thing that was made. Everything sharp, like a canvas. Each stop is composed like a painting, with the subject large in frame.',
    post: 'Anisotropic Kuwahara brush filter, then a paint-relief and canvas-weave pass lit from the upper left, which also letters the caption on a bare corner of the linen. Soft bloom on the sun and the lamps before the filter, so the glow is painted too. No grain, no vignette.',
    explains: 'Stories, journeys and processes that happen in a place, told as a picture book: how something is made, where something comes from, a day in a life, a chain of cause and effect. Each beat is one stop in the landscape where one thing visibly happens and sets off the next; something that travels (wind, a road, a river, a character) leads the camera from stop to stop; one short caption per beat sits in the bare corner of the canvas and names what is happening. The filter carries any 3D scene, so the world can be a farm, a city, a workshop or a room, as long as each subject is a big simple shape.',
    topic: 'From wind to bread: a gust crosses a wheat field and turns the windmill, the millstone grinds flour, and a loaf ends up on the bakery sill',
    duration: DURATION,
    fonts: [CAP.font],
    render: { samples: 24, shutter: 0.5, tone: 'neutral', exposure: 1.0, bloom: 0.5, bloomThreshold: 1.05, vignette: 0, grain: 0, saturation: 1.12, contrast: 1.04, fov: 34, far: 500, background: '#f0a878' },

    build(S) {
      const { THREE, K, scene, camera } = S;
      const V = (x, y, z) => new THREE.Vector3(x, y, z), lin = (hex) => new THREE.Color(hex);
      const rnd = K.rng(21), nz = K.noise(3);
      S.addFilter(BRUSH, { radius: { value: 9 } });

      // ---- the caption corner: all lines are drawn once into one texture, the surface pass shows one row at a time
      const words = lettering(), meas = document.createElement('canvas').getContext('2d'); meas.font = CAP.font;
      let capWidth = 0;
      BEATS.forEach((b, i) => {
        capWidth = Math.max(capWidth, meas.measureText(b.text).width);
        words.filter((w) => w.beat === i).forEach((w) => { const before = b.text.split(' ').slice(0, w.k).join(' ') + (w.k ? ' ' : ''); w.x0 = meas.measureText(before).width; w.x1 = w.x0 + meas.measureText(w.word).width; });
      });
      const capTex = K.canvasTex(CAP.width, CAP.row * BEATS.length, (ctx, w, h) => {
        ctx.fillStyle = '#000'; ctx.fillRect(0, 0, w, h); ctx.font = CAP.font; ctx.textBaseline = 'middle'; ctx.fillStyle = ctx.strokeStyle = '#fff'; ctx.lineWidth = 1.5; ctx.lineJoin = 'round';
        BEATS.forEach((b, i) => { ctx.fillText(b.text, 8, CAP.row * (i + 0.52)); ctx.strokeText(b.text, 8, CAP.row * (i + 0.52)); });   // fill plus outline: fattened, a brush and not a pen
      }, { data: true });
      const capU = { uCap: { value: capTex }, uCapBox: { value: new THREE.Vector4(CAP.x - 8, CAP.y, CAP.width, CAP.row) }, uCapLine: { value: new THREE.Vector2(0, BEATS.length) },
        uCapPatch: { value: new THREE.Vector2(CAP.x + capWidth + 44, CAP.y + CAP.row + 8) }, uCapShow: { value: 0 }, uCapAlpha: { value: 1 } };
      S.addFilter(SURFACE, capU);
      const caption = (t) => {     // which line, how much of it is brushed on, and how much is left after the wipe
        let i = 0; while (i < BEATS.length - 1 && t >= BEATS[i + 1].at - 0.02) i++;
        let show = 0;
        for (const w of words) if (w.beat === i && t >= w.t0) show = Math.max(show, K.lerp(w.x0, w.x1 + 30, K.seg(t, w.t0, w.t0 + w.dur, K.ease.inOut)));
        capU.uCapLine.value.x = i; capU.uCapShow.value = show + 8;
        capU.uCapAlpha.value = i < BEATS.length - 1 ? 1 - K.seg(t, BEATS[i + 1].at - 0.2, BEATS[i + 1].at - 0.04) : 1;   // the old line is wiped just before the next beat
      };

      // ---- the land: one height function for the mesh and for standing things on it. The story runs left to right,
      // the way the wind blows: wheat field, mill, a track past a cart, cottage.
      const WIND = [0.954, -0.3];                                                                    // unit vector on the ground (x, z)
      const MILL = [4, -6], MILL_YAW = -0.3, COT = [12, -3.0], COT_YAW = -0.52;
      const bump = (dx, dz, sx, sz) => Math.exp(-(dx * dx) / (sx * sx) - (dz * dz) / (sz * sz));
      const riverX = (z) => -3.3 + 0.277 * z - 12.5 * K.smooth(-30, -6, z) + 1.2 * Math.sin(z * 0.33 + 1.0);   // the river passes left of the field, then runs straight toward the sun
      const ground = (x, z) => {
        const dr = x - riverX(z);
        let h = 0.2 + 0.24 * nz.fbm(x * 0.09, z * 0.09, 0, 3);
        h += 0.7 * bump(x - 4, z + 6.6, 6.5, 5) + 0.4 * bump(x - 12.4, z + 3.8, 5, 4);                     // mill knoll, cottage rise
        const gap = 1 - 0.85 * Math.exp(-dr * dr / 110);                                             // the river has cut a gap through the three far ridges
        h += gap * (5.5 * Math.exp(-(((z + 40) / 9) ** 2)) * (0.65 + 0.5 * nz.n2(x * 0.045, 1.7)) + 13 * Math.exp(-(((z + 82) / 16) ** 2)) * (0.65 + 0.5 * nz.n2(x * 0.03, 8.3)) + 21 * Math.exp(-(((z + 150) / 22) ** 2)) * (0.6 + 0.7 * nz.n2(x * 0.016, 4.1)));
        return h - 1.25 * Math.exp(-dr * dr / (2.6 + Math.max(0, -z) * 0.5));                         // river bed, wider with distance
      };
      const G = (x, y, z) => [x, ground(x, z) + y, z];                                               // a point y above the ground
      const local = (o, yaw) => (x, y, z) => [o[0] + x * Math.cos(yaw) + z * Math.sin(yaw), ground(o[0], o[1]) + y, o[1] - x * Math.sin(yaw) + z * Math.cos(yaw)];
      const millPt = local(MILL, MILL_YAW), cotPt = local(COT, COT_YAW);                             // points given in the mill's and the cottage's own axes (door and window face +z)
      const TRACK = [[-1.8, 13], [0.1, 8.8], [1.6, 4.6], [2.3, 0], [3.4, -4.1], [5.8, -3.4], [8.4, -3.5], [10.6, -2.2], [12.2, -1.3]];   // field corner, mill door, cottage door
      const trackDist = (x, z) => {
        let d = 1e9;
        for (let i = 1; i < TRACK.length; i++) {
          const ax = TRACK[i - 1][0], az = TRACK[i - 1][1], bx = TRACK[i][0] - ax, bz = TRACK[i][1] - az, k = K.clamp(((x - ax) * bx + (z - az) * bz) / (bx * bx + bz * bz));
          d = Math.min(d, Math.hypot(x - ax - bx * k, z - az - bz * k));
        }
        return d;
      };
      const inField = (x, z) => 1 - Math.hypot(((x + 4.4) / 6.6) ** 2, ((z - 2.2) / 6.3) ** 2) ** 0.5 - 0.1 * nz.n2(x * 0.4, z * 0.4) - K.smooth(1.5, 0.7, trackDist(x, z));   // > 0 inside the wheat

      // ---- camera: one slow continuous shot. It travels with the gust from the field to the mill door, drifts across
      // the door while the stone and the flour do their part, then glides after the flour dust along the track to the
      // window sill and stops. Keys are where the camera is and what it looks at when each beat is on; K.path flows
      // through them. Two paths joined at the door, so the push-in settles before the sideways drift begins.
      const HEAD = [MILL[0], ground(MILL[0], MILL[1]) + 5.0, MILL[1]];             // the hub of the sails, high enough that they clear the door
      const DOOR = { pos: millPt(-0.85, 1.95, 5.5), aim: millPt(-0.65, 1.0, 1.0) }, SILL = { pos: cotPt(-0.52, 1.22, 3.15), aim: cotPt(-0.5, 1.01, 1.4) };
      const leg1 = {
        pos: K.path([[-2.2, G(-1.9, 1.25, 10.9)], [0, G(-0.6, 1.3, 9.6)], [2.2, G(0.4, 1.6, 7.4)], [3.6, G(1.4, 2.1, 3.8)], [DOOR_AT, DOOR.pos]]),
        aim: K.path([[-2.2, G(-0.2, 2.6, -6)], [0, G(0.5, 2.8, -6)], [2.2, G(1.6, 3.0, -6)], [3.6, [HEAD[0], HEAD[1] - 0.5, HEAD[2]]], [DOOR_AT, DOOR.aim]]),
      };
      const leg2 = {
        pos: K.path([[DOOR_AT, DOOR.pos], [DOOR_LEAVE, millPt(-0.2, 1.9, 5.65)], [DOOR_LEAVE + 0.6, millPt(0.6, 1.85, 5.9)], [9.1, G(6.4, 1.9, 1.25)], [STOP, SILL.pos]]),
        aim: K.path([[DOOR_AT, DOOR.aim], [DOOR_LEAVE, millPt(-0.7, 0.95, 1.2)], [DOOR_LEAVE + 0.6, millPt(0.1, 1.1, 1.4)], [9.1, cotPt(-0.3, 2.7, 1.0)], [9.5, cotPt(-0.5, 1.3, 1.35)], [STOP, SILL.aim]]),
      };
      const cam = { pos: (t) => (t < DOOR_AT ? leg1 : leg2).pos(t), aim: (t) => (t < DOOR_AT ? leg1 : leg2).aim(t) };

      // ---- stations: the views the brush dabs are laid out in. Dabs are projected onto the world from a station, so
      // they have an even size on screen there but stay attached to the surfaces. A journey needs one station per stop:
      // every object names the stop it is the subject of, and the ground blends from one to the next.
      // eye, aim = the direction it is seen from; seen = how far away the camera is when it is the subject.
      const STATIONS = {
        field: { eye: [-3, 4.5, 22], aim: [-3, 0.8, 2], seen: 9 },
        mill: { eye: [3, 4, 24], aim: [4, 3.5, -6], seen: 13 },                                      // also the sky and the far land
        yard: { eye: [2.2, 2.6, 15], aim: [3.4, 1.2, -4.6], seen: 5.2 },
        cottage: { eye: cotPt(-1, 2, 24), aim: cotPt(0, 1.2, 1.2), seen: 6.5 },
        sill: { eye: cotPt(-0.7, 1.4, 12), aim: cotPt(-0.6, 1.1, 1.3), seen: 2.1 },
      };
      const REF = {};
      for (const [name, s] of Object.entries(STATIONS)) {
        const d = Math.hypot(s.eye[0] - s.aim[0], s.eye[1] - s.aim[1], s.eye[2] - s.aim[2]);
        const c = new THREE.PerspectiveCamera(2 * Math.atan(Math.tan(17 * Math.PI / 180) * s.seen / d) * 180 / Math.PI, S.W / S.H, 0.1, 900);   // a long lens from far back: same dab size, no ground behind it
        K.look(c, s.eye, s.aim); c.updateMatrixWorld();
        REF[name] = new THREE.Matrix4().multiplyMatrices(c.projectionMatrix, c.matrixWorldInverse);
      }
      const GROUND_REFS = [REF.field, REF.mill, REF.yard, REF.cottage];

      // ---- brush-dab field: thousands of overlapping strokes rasterised into one tiling texture.
      // R,G = a random tilt per dab (each dab catches the light a little differently), B = a random tone per dab
      // (broken colour), A = bristle ridges along the dab, with ragged ends.
      function dabField(seed, flow) {
        const N = 1024, r = K.rng(seed), data = new Uint8Array(N * N * 4).fill(128), LEN = 84, WID = 22;
        for (let n = Math.round(N * N / (LEN * WID) * 3.4); n > 0; n--) {
          const cx = Math.floor(r() * N), cy = Math.floor(r() * N), len = LEN * r.range(0.55, 1.35), wid = WID * r.range(0.6, 1.25);
          const a = flow(cx / N, cy / N) + r.gauss() * 0.16, ca = Math.cos(a), sa = Math.sin(a), bend = r.range(-0.35, 0.35) / len;
          const tx = r() * 255, ty = r() * 255, tone = r() * 255, hairs = Math.max(3, Math.round(wid / 3.2)), ext = Math.ceil(len / 2 + wid);
          const from = [], to = [], ridge = [];
          for (let h = 0; h < hairs; h++) { from.push(-len / 2 + r() * len * 0.14); to.push(len / 2 - r() * len * 0.32); ridge.push(r.range(-0.3, 0.3)); }
          for (let dy = -ext; dy <= ext; dy++) for (let dx = -ext; dx <= ext; dx++) {
            const u = dx * ca + dy * sa, across = (-dx * sa + dy * ca - bend * u * u) / wid + 0.5;
            if (across < 0 || across >= 1) continue;
            const h = Math.floor(across * hairs);
            if (u < from[h] || u > to[h]) continue;
            const i = ((((cy + dy) % N) + N) % N * N + (((cx + dx) % N) + N) % N) * 4;
            data[i] = tx; data[i + 1] = ty; data[i + 2] = tone;
            data[i + 3] = 255 * K.clamp(0.5 + ridge[h] + 0.25 * Math.pow(Math.abs(across - 0.5) * 2, 3) - 0.15 * (u / len + 0.5));   // paint piles up at the sides of a stroke and thins toward its end
          }
        }
        const tex = new THREE.DataTexture(data, N, N);
        tex.wrapS = tex.wrapT = THREE.RepeatWrapping; tex.magFilter = THREE.LinearFilter; tex.minFilter = THREE.LinearMipmapLinearFilter; tex.generateMipmaps = true; tex.anisotropy = 8; tex.needsUpdate = true;
        return tex;
      }
      const DABS = {
        sweep: dabField(1, (u, v) => 0.3 * Math.sin(6.283 * (u + 2 * v))),               // long level strokes: sky, water, fields
        upright: dabField(2, (u, v) => 1.45 + 0.35 * Math.sin(6.283 * (2 * u + v))),     // short standing strokes: trees, walls, grass
      };
      const dabScale = (size) => new THREE.Vector2(S.W, S.H).multiplyScalar(0.5 / (1024 * size));    // size 1 = a dab of about 84 x 22 px on screen

      // ---- wind: the same gust fronts in GLSL (for everything that bends: wheat, grass, flowers) and in JS (for
      // everything that is moved from update(): sails, trees, smoke, dust)
      const MILL_S = MILL[0] * WIND[0] + MILL[1] * WIND[1], f3 = (x) => x.toFixed(3);
      const gustAt = (x, z, t) => {
        const s = x * WIND[0] + z * WIND[1], c = x * WIND[1] - z * WIND[0]; let g = 0;
        GUSTS.forEach((q, i) => { const d = s - (MILL_S + GUST_SPEED * (t - q.at)) + 1.3 * Math.sin(c * 0.42 + i * 2.1); g += q.amp * Math.exp(-d * d / (d > 0 ? 2.2 : 14)); });   // a sharp front and a longer tail
        return g;
      };
      const GUST_GLSL = `
        const vec2 WIND = vec2(${f3(WIND[0])}, ${f3(WIND[1])});
        float gustAt(vec2 p, float t){
          float s = dot(p, WIND), c = p.x * WIND.y - p.y * WIND.x, g = 0.0, d;
          ${GUSTS.map((q, i) => `d = s - (${f3(MILL_S)} + ${f3(GUST_SPEED)} * (t - ${f3(q.at)})) + 1.3 * sin(c * 0.42 + ${f3(i * 2.1)}); g += ${f3(q.amp)} * exp(-d * d / (d > 0.0 ? 2.2 : 14.0));`).join('\n          ')}
          return g;
        }`;
      const uTime = { value: 0 };

      // ---- paint material: a normal lit material (so real light, shadow and fog still work) whose colour and shading
      // are broken up by the dab field. o: a, b = the two colours dabs vary between; c = accent colour for a few dabs;
      // dabs = 'sweep' | 'upright'; size = stroke size; tilt = how much dabs facet the shading; at = its station;
      // moving = the object moves, so its dabs are fixed in its rest pose with bake(); blend = the ground (mixes
      // stations per vertex); wind = 'blade' (instances rooted in the ground bend from the root) | 'head' (instances
      // on a stem `stem` long sway as a whole); plus any MeshStandardMaterial option.
      const paint = (o = {}) => {
        const { a = '#ffffff', b = a, c = b, dabs = 'sweep', size = 1, tilt = 0.5, at = 'mill', moving = false, blend = false, wind = null, stem = 0.4, ...std } = o;
        const m = new THREE.MeshStandardMaterial(Object.assign({ roughness: 1, metalness: 0 }, std));
        const u = { uDabs: { value: DABS[dabs] }, uRef: { value: REF[at] }, uRefs: { value: GROUND_REFS }, uA: { value: lin(a) }, uB: { value: lin(b) }, uC: { value: lin(c) }, uTilt: { value: tilt },
          uScale: { value: dabScale(size) }, uOff: { value: new THREE.Vector2(rnd(), rnd()) }, uTime, uStem: { value: stem } };
        m.defines = {};
        if (moving) m.defines.DAB_BAKED = '';
        if (blend) m.defines.DAB_BLEND = '';
        if (wind) m.defines[wind === 'head' ? 'WIND_HEAD' : 'WIND_BLADE'] = '';
        m.onBeforeCompile = (sh) => {
          Object.assign(sh.uniforms, u);
          sh.vertexShader = sh.vertexShader.replace('#include <common>', `#include <common>
            uniform mat4 uRef, uRefs[4]; uniform float uTime, uStem; attribute vec3 dabClip; attribute vec4 dabMix; varying vec3 vDab, vDab1, vDab2, vDab3; varying vec4 vDabMix; varying float vGust;
            ${GUST_GLSL}`)
            .replace('#include <project_vertex>', `
              vec4 mvPosition = vec4(transformed, 1.0);
              #ifdef USE_INSTANCING
                mvPosition = instanceMatrix * mvPosition;
              #endif
              // dabs are looked up where the thing stands at rest, before the wind moves it
              #if defined(DAB_BAKED)
                vDab = dabClip;
              #elif defined(DAB_BLEND)
                vec4 dW = modelMatrix * mvPosition;
                vDab = (uRefs[0] * dW).xyw; vDab1 = (uRefs[1] * dW).xyw; vDab2 = (uRefs[2] * dW).xyw; vDab3 = (uRefs[3] * dW).xyw; vDabMix = dabMix;
              #else
                vDab = (uRef * modelMatrix * mvPosition).xyw;
              #endif
              vGust = 0.0;
              #if defined(USE_INSTANCING) && (defined(WIND_BLADE) || defined(WIND_HEAD))
                vec3 root = instanceMatrix[3].xyz;
                vGust = gustAt(root.xz, uTime);
                float lean = 0.12 + 0.11 * sin(uTime * 3.2 - dot(root.xz, WIND) * 1.1 + 0.6 * sin(root.z * 0.7 + root.x * 0.3)) + vGust * (0.6 + 0.1 * sin(uTime * 13.0 + root.x * 3.0 + root.z * 2.0));   // ripples run downwind all the time; a gust leans everything hard and makes it flutter
                #ifdef WIND_BLADE
                  float tall = mvPosition.y - root.y, bendAt = lean * clamp(position.y, 0.0, 1.2);    // the stalk curves: no lean at the root, all of it at the tip
                  mvPosition.xz += WIND * sin(bendAt) * tall; mvPosition.y = root.y + tall * cos(bendAt);
                #else
                  mvPosition.xz += WIND * sin(lean) * uStem; mvPosition.y -= (1.0 - cos(lean)) * uStem;
                #endif
              #endif
              mvPosition = modelViewMatrix * mvPosition;
              gl_Position = projectionMatrix * mvPosition;`);
          sh.fragmentShader = sh.fragmentShader.replace('#include <common>', '#include <common>\nuniform sampler2D uDabs; uniform vec2 uScale, uOff; uniform vec3 uA, uB, uC; uniform float uTilt; varying vec3 vDab, vDab1, vDab2, vDab3; varying vec4 vDabMix; varying float vGust;')
            .replace('#include <map_fragment>', `
              #ifdef DAB_BLEND
                vec4 dab = vDabMix.x * texture2D(uDabs, vDab.xy / vDab.z * uScale + uOff) + vDabMix.y * texture2D(uDabs, vDab1.xy / vDab1.z * uScale + uOff + 0.31)
                  + vDabMix.z * texture2D(uDabs, vDab2.xy / vDab2.z * uScale + uOff + 0.57) + vDabMix.w * texture2D(uDabs, vDab3.xy / vDab3.z * uScale + uOff + 0.83);
              #else
                vec4 dab = texture2D(uDabs, vDab.xy / vDab.z * uScale + uOff);
              #endif
              vec3 dabCol = mix(uA, uB, smoothstep(0.1, 0.9, dab.b));
              dabCol = mix(dabCol, uC, step(0.86, fract(dab.b * 5.3 + dab.r * 3.1)));
              diffuseColor.rgb *= dabCol * (1.0 + (dab.a - 0.5) * 0.4) * (1.0 + 0.5 * vGust);`)   // bent stalks show their pale sides: the gust is a bright band
            .replace('#include <normal_fragment_maps>', '#include <normal_fragment_maps>\nnormal = normalize(normal + vec3(dab.rg - 0.5, 0.0) * uTilt);')
            .replace('#include <fog_fragment>', '#include <fog_fragment>\ngl_FragColor.rgb *= 1.0 + (dab.b - 0.5) * 0.3 + (dab.a - 0.5) * 0.14;');   // haze is painted too: far things keep their strokes
        };
        return m;
      };
      // project an object's vertices from its station once, in the pose it has now, and keep that with the geometry
      const bake = (obj, at) => {
        obj.updateWorldMatrix(true, true);
        obj.traverse((m) => {
          if (!m.isMesh) return;
          const g = (m.geometry = m.geometry.clone()), p = g.attributes.position, out = new Float32Array(p.count * 3), v = new THREE.Vector4();
          for (let i = 0; i < p.count; i++) { v.set(p.getX(i), p.getY(i), p.getZ(i), 1).applyMatrix4(m.matrixWorld).applyMatrix4(REF[at]); out.set([v.x, v.y, v.w], i * 3); }
          g.setAttribute('dabClip', new THREE.BufferAttribute(out, 3));
        });
        return obj;
      };
      const add = (geo, mat, pos = [0, 0, 0], o = {}) => {
        const m = new THREE.Mesh(geo, mat); m.position.set(pos[0], pos[1], pos[2]);
        if (o.rot) m.rotation.set(o.rot[0], o.rot[1], o.rot[2]);
        if (o.scale) m.scale.set(o.scale[0], o.scale[1], o.scale[2]);
        m.castShadow = o.cast !== false; m.receiveShadow = true; (o.parent || scene).add(m); return m;
      };
      const group = (pos, parent = scene, yaw = 0) => { const g = new THREE.Group(); g.position.set(pos[0], pos[1], pos[2]); g.rotation.y = yaw; parent.add(g); return g; };
      const box = (w, h, d) => new THREE.BoxGeometry(w, h, d);

      // ---- light: a low sun from behind-left, blue sky in every shadow, a little warm bounce from the ground
      const sunDir = (el, az) => V(-Math.sin(az) * Math.cos(el), Math.sin(el), -Math.cos(az) * Math.cos(el));
      const lightElev = K.track([[0, 0.27], [DURATION, 0.22, 'linear']]);         // the light sinks during the clip, so shadows lengthen
      const discElev = K.track([[0, 0.068], [DURATION, 0.05, 'linear']]);        // the painted sun sits lower and slips behind the ridge
      K.env(S, { top: '#3f72c0', horizon: '#c9a0b4', bottom: '#3a4a2a', intensity: 0.85 });
      const sun = K.sun(S, { color: '#ffc27c', power: 5.6, size: 30, soft: 1.0, mapSize: 4096, far: 140, normalBias: 0.06 });
      const bounce = new THREE.DirectionalLight('#ff9a6a', 0.7); bounce.position.set(-6, -10, -8); scene.add(bounce);
      scene.fog = new THREE.FogExp2('#d9a59e', 0.0078);                    // dusty rose haze: distance cools and pales everything
      const lampLight = (parent, pos, power, reach) => { const l = new THREE.PointLight('#ffa23c', power, reach, 2); l.position.set(pos[0], pos[1], pos[2]); parent.add(l); return l; };

      // ---- sky and water share one sky function, so the river reflects the sunset it sits under
      const SKY = `
        uniform vec3 uSun; uniform float uDusk, uTime;
        float h21(vec2 p){ p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 45.32); return fract(p.x * p.y); }
        float vnoise(vec2 p){ vec2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f); return mix(mix(h21(i), h21(i + vec2(1, 0)), f.x), mix(h21(i + vec2(0, 1)), h21(i + vec2(1, 1)), f.x), f.y); }
        float fbm(vec2 p){ float s = 0.0, a = 0.5; for (int i = 0; i < 4; i++) { s += a * vnoise(p); p = p * 2.03 + 17.0; a *= 0.5; } return s; }
        vec3 skyColor(vec3 d){
          float y = clamp(d.y, 0.0, 1.0), s = max(dot(d, uSun), 0.0);
          vec3 c = mix(vec3(1.0, 0.52, 0.20), vec3(0.93, 0.58, 0.42), smoothstep(0.0, 0.07, y));       // ember at the horizon, then rose
          c = mix(c, vec3(0.17, 0.28, 0.44), smoothstep(0.04, 0.14, y));                              // slate blue
          c = mix(c, vec3(0.035, 0.09, 0.25), smoothstep(0.11, 0.30, y));                             // deep blue overhead
          c *= 1.0 - 0.18 * uDusk;
          vec3 off = d - uSun;
          c += vec3(1.0, 0.46, 0.14) * exp(-dot(off.xz, off.xz) * 3.5 - off.y * off.y * 60.0) * 0.8 + vec3(1.0, 0.8, 0.45) * pow(s, 150.0);   // a glow that hugs the horizon, and a hot core
          // clouds: a flat deck seen in perspective, stretched along the wind, drifting. An edge that thins out toward
          // the sun is lit salmon, the body stays mauve.
          vec2 q = d.xz / (y + 0.1) * vec2(0.5, 1.7) + vec2(uTime * 0.035, 0.0);
          float body = fbm(q), toSun = fbm(q + normalize(uSun.xz) * vec2(0.14, 0.45));
          float cover = smoothstep(0.47, 0.62, body) * smoothstep(0.015, 0.05, y) * mix(0.12, 1.0, smoothstep(0.16, 0.09, y));
          vec3 cloud = mix(vec3(0.30, 0.24, 0.42), vec3(1.25, 0.60, 0.36), clamp(0.45 + (body - toSun) * 7.0, 0.0, 1.0)) + vec3(0.7, 0.35, 0.1) * pow(s, 5.0);
          c = mix(c, cloud, cover * 0.92);
          return c + vec3(9.0, 6.4, 3.0) * smoothstep(0.99962, 0.99975, s);                           // the disc
        }`;
      const SKY_VS = 'uniform mat4 uRef; varying vec3 vDab, vWorld; void main(){ vec4 w = modelMatrix * vec4(position, 1.0); vWorld = w.xyz; vec4 c = uRef * w; vDab = c.xyw; gl_Position = projectionMatrix * viewMatrix * w; }';
      const skyU = { uSun: { value: V(0, 0, -1) }, uDusk: { value: 0 }, uTime, uRef: { value: REF.mill }, uDabs: { value: DABS.sweep }, uScale: { value: dabScale(1) } };
      add(new THREE.SphereGeometry(400, 48, 24), new THREE.ShaderMaterial({ side: THREE.BackSide, depthWrite: false, uniforms: skyU, vertexShader: SKY_VS,
        fragmentShader: `uniform sampler2D uDabs; uniform vec2 uScale; varying vec3 vDab, vWorld; ${SKY}
          void main(){
            vec4 dab = texture2D(uDabs, vDab.xy / vDab.z * uScale / 2.1);                             // big sweeping strokes
            vec3 c = skyColor(normalize(vWorld - cameraPosition));
            c *= 1.0 + (dab.b - 0.5) * 0.34 + (dab.a - 0.5) * 0.2; c += (dab.r - 0.5) * vec3(0.12, 0.0, -0.1) * (0.2 + c.g);   // broken colour: each dab leans warm or cool
            gl_FragColor = vec4(c, 1.0);
          }` }), [0, 0, 0], { cast: false });
      add(new THREE.PlaneGeometry(400, 400), new THREE.ShaderMaterial({ uniforms: skyU, vertexShader: SKY_VS,
        fragmentShader: `uniform sampler2D uDabs; uniform vec2 uScale; varying vec3 vDab, vWorld; ${SKY}
          void main(){
            vec4 dab = texture2D(uDabs, vDab.xy / vDab.z * uScale / 1.3 + vec2(uTime * 0.008, 0.0));   // on water the dabs are ripples, so they are allowed to drift
            vec3 r = reflect(normalize(vWorld - cameraPosition), normalize(vec3((dab.r - 0.5) * 0.12, 1.0, (dab.g - 0.5) * 0.22)));
            r.y = abs(r.y);
            gl_FragColor = vec4(skyColor(r) * vec3(0.78, 0.84, 0.9) + vec3(0.0, 0.02, 0.03), 1.0);
          }` }), [0, -0.3, -100], { rot: [-Math.PI / 2, 0, 0], cast: false });

      // ---- the ground mesh: local colour per vertex, dabs blended between stations per vertex
      const LAND = { meadow: lin('#5f8a36'), lush: lin('#2f6b3c'), sunny: lin('#a3a83a'), wheat: lin('#dba13a'), straw: lin('#efd07a'), earth: lin('#a5532f'), lavender: lin('#7a5aa6'), sand: lin('#e6c79c'), reed: lin('#3c5a2e'), ridge: lin('#3f6658'), blue: lin('#4c5f90'), violet: lin('#6c68a4') };
      const FIELDS = [LAND.wheat, LAND.meadow, LAND.straw, LAND.lavender, LAND.lush, LAND.earth, LAND.wheat, LAND.sunny];
      const landColor = (x, z, out) => {
        const n = nz.fbm(x * 0.2, z * 0.2, 5, 3), dr = Math.abs(x - riverX(z));
        out.copy(LAND.meadow).lerp(LAND.lush, K.smooth(-0.1, 0.35, n)).lerp(LAND.sunny, K.smooth(0.1, 0.4, -n));
        if (z < -11) {                                                                                // the valley floor is a patchwork of strip fields
          const strip = Math.floor((z + 0.35 * x + 3 * nz.n2(x * 0.05, z * 0.05)) / (2.5 + Math.max(0, -z) * 0.09)), block = Math.floor((x + 40) / 13 + K.hash(strip) * 3);
          out.lerp(FIELDS[Math.floor(K.hash(strip, block) * FIELDS.length)], 0.85 * K.smooth(-11, -15, z));
        }
        out.lerp(LAND.wheat, K.smooth(-0.12, 0.05, inField(x, z)));                                   // stubble gold under the wheat
        out.lerp(LAND.reed, K.smooth(2.6, 1.0, dr / (1 + Math.max(0, -z) * 0.03)));
        out.lerp(LAND.ridge, K.smooth(-27, -36, z)).lerp(LAND.blue, K.smooth(-55, -70, z)).lerp(LAND.violet, K.smooth(-105, -125, z));   // each ridge further back is bluer
        return out.lerp(LAND.sand, K.smooth(0.55, 0.3, trackDist(x, z)));
      };
      {
        const NX = 440, NZ = 320, g = new THREE.PlaneGeometry(1, 1, NX, NZ), p = g.attributes.position, uv = g.attributes.uv, col = new Float32Array(p.count * 3), mix = new Float32Array(p.count * 4), c = new THREE.Color();
        for (let i = 0; i < p.count; i++) {
          // a grid that is fine where the story happens and coarse far away
          const u = uv.getX(i) * 2 - 1, au = Math.abs(u), v = uv.getY(i);
          const x = 4 + Math.sign(u) * (au < 0.5 ? 32 * au : 16 + 160 * Math.pow((au - 0.5) * 2, 2.2)), z = v < 0.45 ? 13 - 60 * v : -14 - 190 * Math.pow((v - 0.45) / 0.55, 1.7);
          p.setXYZ(i, x, ground(x, z), z); landColor(x, z, c); col.set([c.r, c.g, c.b], i * 3);
          const far = K.smooth(-9, -15, z), a = K.smooth(0.2, 2.6, x), b = K.smooth(7.4, 9.4, x);     // which station's dabs: field | yard | cottage near the lens, the mill view far away
          mix.set([(1 - a) * (1 - far), far, a * (1 - b) * (1 - far), b * (1 - far)], i * 4);
        }
        g.setAttribute('color', new THREE.BufferAttribute(col, 3)); g.setAttribute('dabMix', new THREE.BufferAttribute(mix, 4)); g.computeVertexNormals();
        add(g, paint({ a: '#aeb9d6', b: '#fff2c4', vertexColors: true, size: 1.15, tilt: 0.22, blend: true }));    // dabs push the local colour cool or warm
      }

      // ---- BEAT 1, the wheat: thousands of chunky stalks and ears that bend in the vertex shader when a gust passes
      {
        const r = K.rng(5), pts = [];
        while (pts.length < 11000) { const x = r.range(-11.5, 3.5), z = r.range(-4.6, 9), m = inField(x, z); if (m > 0 && r() < 0.35 + 0.65 * K.smooth(-4, 8, z)) pts.push([x, z, m]); }   // thicker toward the lens
        const stalk = new THREE.CylinderGeometry(0.012, 0.02, 0.9, 4, 4); stalk.translate(0, 0.45, 0);
        const ear = new THREE.LatheGeometry([[0, 0.8], [0.045, 0.84], [0.062, 0.94], [0.05, 1.06], [0, 1.16]].map((q) => new THREE.Vector2(q[0], q[1])), 5);
        const stalks = new THREE.InstancedMesh(stalk, paint({ a: '#7f7a2c', b: '#d6ab45', c: '#5f8a36', dabs: 'upright', size: 0.6, tilt: 0.5, at: 'field', wind: 'blade' }), pts.length);
        const ears = new THREE.InstancedMesh(ear, paint({ a: '#d08f24', b: '#ffd565', c: '#fff0aa', dabs: 'upright', size: 0.6, tilt: 0.6, at: 'field', wind: 'blade', emissive: '#ff8a20', emissiveIntensity: 0.16 }), pts.length);   // backlit ears glow a little
        pts.forEach(([x, z, m], i) => {
          const tall = r.range(1.0, 1.35) * (0.8 + 0.2 * K.smooth(0, 0.12, m)), rot = [r.range(-0.1, 0.1), r() * 6.28, r.range(-0.1, 0.1)], y = ground(x, z) - 0.02, fat = r.range(0.9, 1.35);
          K.place(stalks, i, [x, y, z], rot, [1, tall, 1]); K.place(ears, i, [x, y, z], rot, [fat, tall, fat]);
        });
        for (const m of [stalks, ears]) { m.receiveShadow = true; m.frustumCulled = false; scene.add(m); }
      }

      // ---- grass, poppies and fence posts beside the track, so the journey has something to slide past
      {
        const r = K.rng(4), blade = new THREE.ConeGeometry(0.035, 1, 3, 3); blade.translate(0, 0.5, 0);
        const spots = { field: [], yard: [], cottage: [] };
        const footprint = (x, z) => Math.min(Math.hypot(x - MILL[0], z - MILL[1]) - 3.3, Math.hypot(x - COT[0], z - COT[1]) - 2.6, Math.hypot(x - 7.6, z + 5.0) - 1.5);   // keep clear of the mill yard, the cottage and the cart
        while (spots.field.length + spots.yard.length + spots.cottage.length < 5200) {
          const x = r.range(-5, 19), z = r.range(-9, 11.5), d = trackDist(x, z);
          if (inField(x, z) > -0.03 || d < 0.6 || footprint(x, z) < 0 || r() > 0.25 + 0.75 * K.smooth(4.5, 0.8, d)) continue;   // tufts gather along the track
          spots[x < 1.4 ? 'field' : x < 8.4 ? 'yard' : 'cottage'].push([x, z]);
        }
        for (const [at, list] of Object.entries(spots)) {
          const grass = new THREE.InstancedMesh(blade, paint({ a: '#1f4a2c', b: '#7f9a34', c: '#d9a83a', dabs: 'upright', size: 0.5, tilt: 0.6, at, wind: 'blade' }), list.length);
          const poppies = new THREE.InstancedMesh(new THREE.IcosahedronGeometry(1, 0), paint({ a: '#c8241c', b: '#ff5a2a', size: 0.4, tilt: 0.6, at, wind: 'head', stem: 0.35, emissive: '#a01808', emissiveIntensity: 0.25 }), Math.floor(list.length / 22));
          list.forEach(([x, z], i) => {
            K.place(grass, i, [x, ground(x, z) - 0.02, z], [r.range(-0.25, 0.25), r() * 6, r.range(-0.3, 0.3)], [1.5, r.range(0.2, 0.62), 1.5]);
            if (i < poppies.count) K.place(poppies, i, [x + r.range(-0.2, 0.2), ground(x, z) + r.range(0.3, 0.5), z + r.range(-0.2, 0.2)], [r(), r(), r()], [0.085, 0.06, 0.085]);
          });
          for (const m of [grass, poppies]) { m.receiveShadow = true; m.frustumCulled = false; scene.add(m); }
        }
        const posts = new THREE.InstancedMesh(box(0.09, 0.8, 0.09), paint({ a: '#33262a', b: '#6a4c3c', dabs: 'upright', size: 0.5, at: 'field' }), 9);
        for (let i = 0; i < posts.count; i++) { const z = K.lerp(10.5, -3, i / (posts.count - 1)), x = K.lerp(1.6, 2.6, i / (posts.count - 1)) + 1.2 - 0.4 * Math.sin(i * 0.5); K.place(posts, i, [x, ground(x, z) + 0.34, z], [r.range(-0.08, 0.08), r(), r.range(-0.08, 0.08)]); }
        posts.castShadow = posts.receiveShadow = true; scene.add(posts);
      }

      // ---- trees and hedges: blobs, nothing more. The brush does the leaves. Trees lean when a gust reaches them.
      const leaf = paint({ a: '#1d4a3c', b: '#5d8f2e', c: '#c9862e', dabs: 'upright', size: 0.6, tilt: 0.7, flatShading: true, moving: true });
      const bark = paint({ a: '#4a2f2a', b: '#7a4a32', dabs: 'upright', size: 0.5, moving: true });
      const blob = new THREE.IcosahedronGeometry(1, 1);
      const trees = [[-1.6, -9, 'round', 1.7, 'mill'], [0.6, -10.5, 'poplar', 1.5, 'mill'], [-11.8, -5.5, 'round', 1.9, 'mill'], [-10.6, -11.5, 'poplar', 1.7, 'mill'], [-11.7, -12.3, 'poplar', 1.35, 'mill'], [8.6, -9.5, 'round', 1.6, 'mill'],
        [14.4, -5.0, 'poplar', 1.6, 'cottage'], [15.3, -4.0, 'poplar', 1.25, 'cottage'], [9.6, -7.4, 'round', 1.3, 'cottage'], [18.5, -7.5, 'round', 1.8, 'cottage'], [16.8, 0.6, 'round', 1.2, 'cottage']].map(([x, z, kind, s, at]) => {
        const g = group([x, ground(x, z), z]);
        add(new THREE.CylinderGeometry(0.05 * s, 0.09 * s, 1.0 * s, 5), bark, [0, 0.4 * s, 0], { parent: g });
        if (kind === 'poplar') add(blob, leaf, [0, 2.0 * s, 0], { parent: g, scale: [0.46 * s, 1.75 * s, 0.46 * s], rot: [0, x, 0] });
        else for (const [ox, oy, oz, rr] of [[0, 1.35, 0, 0.8], [-0.55, 1.0, 0.2, 0.55], [0.5, 1.05, -0.15, 0.6], [0.1, 1.8, 0.1, 0.5]]) add(blob, leaf, [ox * s, oy * s, oz * s], { parent: g, scale: [rr * s, rr * s * 0.85, rr * s], rot: [ox, oy * 3, oz] });
        bake(g, at);
        return { g, x, z, bend: kind === 'poplar' ? 0.1 : 0.035 };
      });
      {
        const hedge = new THREE.InstancedMesh(blob, paint({ a: '#1c4440', b: '#4c7a36', c: '#9a7a30', dabs: 'upright', size: 0.6, tilt: 0.6, flatShading: true }), 170), r = K.rng(8);
        for (let i = 0; i < hedge.count; i++) {
          let x, z; do { x = r.range(-48, 48); z = r.range(-52, -13); } while (Math.abs(x - riverX(z)) < 3.2 - z * 0.06);
          if (i % 3) z = Math.round(z / 5) * 5 + r.range(-0.4, 0.4);                                   // most of them line up as hedgerows along field edges
          const s = r.range(0.32, 0.62) * (1 - z * 0.014);
          K.place(hedge, i, [x, ground(x, z) + s * 0.55, z], [0, r() * 6, 0], [s * r.range(0.9, 1.5), s * r.range(0.8, 1.5), s]);
        }
        hedge.castShadow = hedge.receiveShadow = true; scene.add(hedge);
      }

      // ---- shared props: flour sacks (a lathe shape with a tied neck)
      const linen = (at, moving) => paint({ a: '#c9b48a', b: '#fff0cc', c: '#e2c08c', dabs: 'upright', size: 0.5, tilt: 0.45, at, moving, emissive: '#b08a58', emissiveIntensity: 0.3 });   // a little glow of their own keeps them cream in the shade
      const stripe = (at, moving) => paint({ a: '#34557e', b: '#4f77a0', size: 0.4, at, moving });
      const cord = (at) => paint({ a: '#5a3a2a', b: '#7a5038', size: 0.4, at });
      const sackGeo = new THREE.LatheGeometry([[0, 0], [0.25, 0], [0.33, 0.1], [0.35, 0.32], [0.3, 0.54], [0.17, 0.68], [0.11, 0.73], [0.19, 0.84], [0, 0.82]].map((q) => new THREE.Vector2(q[0], q[1])), 12);
      const sack = (parent, at, pos, o = {}) => {
        const g = group(pos, parent, o.yaw || 0); g.rotation.z = o.lie || 0; g.scale.setScalar(o.s || 1);
        add(sackGeo, linen(at), [0, 0, 0], { parent: g }); add(new THREE.TorusGeometry(0.12, 0.022, 5, 10), cord(at), [0, 0.73, 0], { parent: g, rot: [Math.PI / 2, 0, 0] });
        for (const y of [0.24, 0.36]) add(new THREE.CylinderGeometry(0.357, 0.357, 0.05, 12, 1, true), stripe(at), [0, y, 0], { parent: g, cast: false });   // two blue stripes: a flour sack, not a boulder
        return g;
      };

      // ---- BEAT 2, the windmill: an eight-sided tower whose ground floor has one face left open as the door, a cap
      // and four sails; inside, a lamp and the stones
      const mill = group(G(MILL[0], -0.05, MILL[1]), scene, MILL_YAW);
      const stone = (at, o = {}) => paint(Object.assign({ a: '#cdbba6', b: '#fff0d4', c: '#e9b98c', dabs: 'upright', size: 0.75, tilt: 0.35, flatShading: true, at }, o));
      const wood = (at, o = {}) => paint(Object.assign({ a: '#4a2a26', b: '#8a4a30', dabs: 'upright', size: 0.55, tilt: 0.5, flatShading: true, at }, o));
      const glow = (hex, k) => new THREE.MeshStandardMaterial({ color: '#2a1a12', emissive: hex, emissiveIntensity: k, roughness: 1 });
      const P8 = Math.PI / 8;
      add(new THREE.CylinderGeometry(1.34, 1.62, 2.1, 8, 1, true, P8, Math.PI * 2 - 2 * P8), stone('yard', { side: THREE.DoubleSide, size: 0.9 }), [0, 1.05, 0], { parent: mill });   // seven faces: the eighth is the doorway
      add(new THREE.CircleGeometry(1.5, 8), wood('yard'), [0, 0.03, 0], { parent: mill, rot: [-Math.PI / 2, 0, 0], cast: false });                   // floor
      add(new THREE.CylinderGeometry(1.46, 1.46, 0.16, 8, 1, false, P8), wood('mill'), [0, 2.16, 0], { parent: mill });                              // the beam ring between the floors
      add(new THREE.CylinderGeometry(0.8, 1.34, 2.7, 8, 1, false, P8), stone('mill'), [0, 3.59, 0], { parent: mill });
      add(new THREE.ConeGeometry(0.98, 1.1, 8, 1, false, P8), wood('mill'), [0, 5.47, 0], { parent: mill });
      for (const x of [-0.6, 0.6]) add(box(0.13, 2.1, 0.16), wood('yard'), [x, 1.05, 1.44], { parent: mill });                                        // door posts and lintel
      add(box(1.5, 0.17, 0.22), wood('yard'), [0, 2.1, 1.42], { parent: mill });
      add(box(0.07, 1.9, 1.0), wood('yard', { a: '#6a4a3a', b: '#b08258' }), [0.86, 0.98, 1.86], { parent: mill, rot: [0, -0.75, 0] });              // the door leaf, swung open
      add(box(0.3, 0.36, 0.06), glow('#ffa028', 1.6), [0, 3.5, 1.02], { parent: mill, rot: [-0.18, 0, 0], cast: false });                             // the miller's window
      lampLight(mill, [0, 1.75, 0.55], 3.2, 7);
      // the stones: a fixed bed stone on a timber frame, and the runner stone turning on it with its shaft and cog wheel
      add(box(1.4, 0.6, 1.4), wood('yard'), [0, 0.3, 0.1], { parent: mill });
      add(new THREE.CylinderGeometry(0.66, 0.66, 0.2, 14), stone('yard', { a: '#8f867f', b: '#c9bfb2', size: 0.5 }), [0, 0.7, 0.1], { parent: mill });
      const runner = group([0, 0.82, 0.1], mill);
      const grit = stone('yard', { a: '#a89c92', b: '#f1e6d6', c: '#7d7470', size: 0.45, tilt: 0.6, moving: true }), turning = wood('yard', { moving: true, a: '#3a2420', b: '#7a4a30' });
      add(new THREE.CylinderGeometry(0.62, 0.62, 0.3, 14), grit, [0, 0.15, 0], { parent: runner });
      add(new THREE.CylinderGeometry(0.635, 0.635, 0.06, 14), turning, [0, 0.15, 0], { parent: runner });                                             // iron band
      add(box(1.16, 0.06, 0.16), turning, [0, 0.32, 0], { parent: runner });                                                                          // the bar that drives the stone: it shows the turning
      for (let i = 0; i < 4; i++) add(box(0.14, 0.2, 0.12), turning, [0.66 * Math.cos(i * Math.PI / 2 + 0.6), 0.15, 0.66 * Math.sin(i * Math.PI / 2 + 0.6)], { parent: runner, rot: [0, -i * Math.PI / 2 - 0.6, 0] });
      add(box(0.12, 0.98, 0.12), turning, [0, 0.79, 0], { parent: runner });                                                                          // shaft
      add(new THREE.CylinderGeometry(0.5, 0.5, 0.08, 16), turning, [0, 1.06, 0], { parent: runner });                                                 // cog wheel
      for (let i = 0; i < 10; i++) add(box(0.07, 0.16, 0.07), turning, [0.44 * Math.cos(i * 0.6283), 0.96, 0.44 * Math.sin(i * 0.6283)], { parent: runner });
      bake(runner, 'yard');
      // cap and sails face into the wind (and, a painter's cheat, a little toward us)
      const head = group([HEAD[0], HEAD[1], HEAD[2]], scene, -0.5);
      const rotor = group([0, 0, 1.0], head);
      rotor.rotation.x = -0.14;   // the windshaft is tilted, as on a real mill: the tower widens downwards, and upright sails would cut into its wall each time one points down
      const sailMat = paint({ a: '#e2c79c', b: '#fff3d2', dabs: 'upright', size: 0.6, tilt: 0.4, moving: true, side: THREE.DoubleSide, emissive: '#ff9a4a', emissiveIntensity: 0.22 });   // sailcloth lets some sun through
      const sparMat = paint({ a: '#4a2a26', b: '#7a4530', dabs: 'upright', size: 0.5, moving: true });
      add(new THREE.CylinderGeometry(0.13, 0.13, 0.5, 8), sparMat, [0, 0, -0.15], { parent: rotor, rot: [Math.PI / 2, 0, 0] });
      for (let k = 0; k < 4; k++) {
        const arm = new THREE.Group(); arm.rotation.z = k * Math.PI / 2; rotor.add(arm);
        add(box(0.1, 2.65, 0.08), sparMat, [0, 1.32, 0], { parent: arm });
        add(new THREE.PlaneGeometry(0.62, 1.95), sailMat, [0.36, 1.6, 0.02], { parent: arm, rot: [0, -0.12, 0] });
        for (let j = 0; j < 5; j++) add(box(0.66, 0.04, 0.04), sparMat, [0.36, 0.7 + j * 0.45, 0.05], { parent: arm });
      }
      bake(rotor, 'mill');

      // ---- BEAT 3, flour: a spout from the stones, an open sack under it that fills, full sacks by the door, a cart
      const chute = group([-1.17, 1.05, 1.17], mill, -Math.PI / 4);                                                                                   // the meal spout comes out through the wall beside the door
      add(box(0.26, 0.05, 0.95), wood('yard', { a: '#7a5236', b: '#c08a56' }), [0, 0, 0], { parent: chute, rot: [0.3, 0, 0] });
      for (const x of [-0.13, 0.13]) add(box(0.035, 0.13, 0.95), wood('yard', { a: '#7a5236', b: '#c08a56' }), [x, 0.04, 0], { parent: chute, rot: [0.3, 0, 0] });
      const SPOUT = [-1.47, 0.9, 1.47];                                                                                                               // its mouth, in the mill's axes
      const openSack = group([SPOUT[0] - 0.03, -0.06, SPOUT[2] + 0.05], mill);
      add(new THREE.LatheGeometry([[0, 0], [0.27, 0], [0.36, 0.1], [0.38, 0.3], [0.35, 0.46], [0.37, 0.52], [0.32, 0.54]].map((q) => new THREE.Vector2(q[0], q[1])), 12), linen('yard', true), [0, 0, 0], { parent: openSack });
      for (const y of [0.2, 0.3]) add(new THREE.CylinderGeometry(0.387, 0.387, 0.045, 12, 1, true), stripe('yard', true), [0, y, 0], { parent: openSack, cast: false });
      const mound = add(new THREE.SphereGeometry(0.31, 12, 6, 0, Math.PI * 2, 0, Math.PI / 2), new THREE.MeshStandardMaterial({ color: '#fffaf0', roughness: 1, emissive: '#f4e6cc', emissiveIntensity: 0.75 }), [0, 0.4, 0], { parent: openSack, cast: false });
      bake(openSack, 'yard');
      const stream = add(new THREE.CylinderGeometry(0.04, 0.055, 1, 6), new THREE.MeshBasicMaterial({ color: '#fff8ea', transparent: true }), [0, 0, 0], { parent: mill, cast: false });
      sack(mill, 'yard', [-2.35, -0.08, 0.8], { yaw: 0.4 }); sack(mill, 'yard', [-2.8, -0.1, 0.15], { yaw: 1.3, s: 1.08 }); sack(mill, 'yard', [-2.55, 0.52, 0.5], { yaw: 0.9, lie: 1.45, s: 0.95 });
      // flour dust: soft round sprites. Each one bursts out of the sack, then rides the wind toward the cottage and thins out.
      const puffTex = K.canvasTex(128, 128, (ctx) => { const g = ctx.createRadialGradient(64, 64, 2, 64, 64, 62); g.addColorStop(0, 'rgba(255,255,255,0.9)'); g.addColorStop(0.35, 'rgba(255,255,255,0.5)'); g.addColorStop(0.7, 'rgba(255,255,255,0.15)'); g.addColorStop(1, 'rgba(255,255,255,0)'); ctx.fillStyle = g; ctx.fillRect(0, 0, 128, 128); });
      const MOUTH = V(...millPt(SPOUT[0], 0.5, SPOUT[2] + 0.05));
      const puffs = Array.from({ length: 26 }, (_, i) => {
        const s = new THREE.Sprite(new THREE.SpriteMaterial({ map: puffTex, color: '#fff1dc', transparent: true, depthWrite: false })); scene.add(s);
        const a = K.hash(i, 5) * 6.28, up = 0.5 + 0.9 * K.hash(i, 6);
        return { s, born: SACK_FULL + 0.22 * K.hash(i, 7), burst: V(Math.cos(a) * 0.75, up * 1.1, Math.sin(a) * 0.4 + 0.25), speed: 2.2 + 1.3 * K.hash(i, 8), size: 0.3 + 0.35 * K.hash(i, 9), rise: 0.1 + 0.4 * K.hash(i, 10) };
      });
      // the cart on the track, loaded for the bakery
      {
        const cart = group(G(7.6, 0.06, -5.3), scene, 0.12), w = wood('yard', { a: '#8a5a34', b: '#d69a5a' });
        add(box(1.7, 0.1, 0.95), w, [0, 0.62, 0], { parent: cart });
        for (const z of [-0.47, 0.47]) add(box(1.7, 0.3, 0.06), w, [0, 0.8, z], { parent: cart });
        for (const x of [-0.85, 0.85]) add(box(0.06, 0.3, 0.95), w, [x, 0.8, 0], { parent: cart });
        for (const z of [-0.36, 0.36]) add(box(1.5, 0.07, 0.07), w, [1.5, 0.4, z], { parent: cart, rot: [0, 0, -0.33] });                             // shafts resting on the ground
        for (const z of [-0.58, 0.58]) {
          const wheel = group([-0.1, 0.5, z], cart);
          add(new THREE.TorusGeometry(0.5, 0.07, 6, 18), w, [0, 0, 0], { parent: wheel });
          for (let i = 0; i < 4; i++) add(box(0.98, 0.07, 0.05), w, [0, 0, 0], { parent: wheel, rot: [0, 0, i * Math.PI / 4] });
        }
        sack(cart, 'yard', [-0.42, 0.67, 0.02], { yaw: 0.3 }); sack(cart, 'yard', [0.3, 0.67, -0.05], { yaw: 1.9, s: 1.05 }); sack(cart, 'yard', [-0.02, 1.2, 0], { yaw: 0.6, lie: 1.5, s: 0.9 });
      }

      // ---- BEAT 4, the bakery: a cottage with real walls, so the window is a hole with a lit room and an oven behind
      // it, and the loaf can come out of it
      const cot = group(G(COT[0], -0.08, COT[1]), scene, COT_YAW);
      const plaster = (at, o = {}) => paint(Object.assign({ a: '#9c9092', b: '#dccdbb', dabs: 'upright', size: 0.6, tilt: 0.3, at }, o));
      const roofMat = paint({ a: '#8a3028', b: '#c85a30', c: '#e07a3a', size: 0.55, tilt: 0.5, at: 'cottage' });
      const teal = (at) => paint({ a: '#1f4f5f', b: '#2f6f78', dabs: 'upright', size: 0.5, at });
      const room = paint({ a: '#8a4a26', b: '#c8763a', dabs: 'upright', size: 0.6, at: 'sill', emissive: '#ff7a20', emissiveIntensity: 0 });          // the walls inside glow once the oven is lit
      add(box(3.4, 1.9, 0.16), room, [0, 0.95, -1.17], { parent: cot }); add(box(3.42, 1.9, 0.04), plaster('cottage'), [0, 0.95, -1.27], { parent: cot });
      for (const x of [-1.62, 1.62]) { add(box(0.16, 1.9, 2.5), plaster('cottage'), [x, 0.95, 0], { parent: cot }); add(box(0.04, 1.86, 2.2), room, [x * 0.94, 0.95, 0], { parent: cot, cast: false }); }
      for (const [x, y, w, h] of [[-1.375, 0.95, 0.65, 1.9], [0.775, 0.95, 1.85, 1.9], [-0.6, 0.4, 0.9, 0.8], [-0.6, 1.75, 0.9, 0.3]]) add(box(w, h, 0.16), plaster('sill', { size: 1.3 }), [x, y, 1.17], { parent: cot });   // front wall around the window
      const gable = new THREE.Shape(); gable.moveTo(-1.5, 0); gable.lineTo(1.5, 0); gable.lineTo(0, 1.3); gable.closePath();
      add(new THREE.ExtrudeGeometry(gable, { depth: 3.9, bevelEnabled: false }), roofMat, [-1.95, 1.88, 0], { parent: cot, rot: [0, Math.PI / 2, 0] });
      add(box(0.48, 1.3, 0.48), plaster('cottage'), [-1.25, 3.05, 0.3], { parent: cot });                                                               // chimney
      add(box(0.64, 1.36, 0.06), teal('cottage'), [1.0, 0.68, 1.27], { parent: cot });                                                                // door
      add(box(0.9, 0.1, 0.5), plaster('cottage', { a: '#77706e', b: '#a89f96' }), [1.0, 0.03, 1.5], { parent: cot });                                 // doorstep
      const oven = glow('#ff8418', 0);
      add(new THREE.CircleGeometry(0.34, 16, 0, Math.PI), oven, [-1.02, 0.95, -1.08], { parent: cot, cast: false }); add(box(0.68, 0.3, 0.02), oven, [-1.02, 0.8, -1.08], { parent: cot, cast: false });   // the oven mouth
      const roomLamp = lampLight(cot, [-0.4, 1.45, 0.1], 0, 6);
      // window: frame, sill, shutters, lantern
      const cream = paint({ a: '#bfae96', b: '#f2e4c8', dabs: 'upright', size: 0.5, at: 'sill' });
      for (const [x, y, w, h] of [[-1.08, 1.2, 0.07, 0.94], [-0.12, 1.2, 0.07, 0.94], [-0.6, 1.635, 1.03, 0.07]]) add(box(w, h, 0.08), cream, [x, y, 1.27], { parent: cot });
      add(box(1.2, 0.07, 0.46), cream, [-0.6, 0.78, 1.36], { parent: cot });
      const SILL_Y = 0.815;
      for (const sx of [-1, 1]) {
        add(box(0.44, 0.84, 0.05), teal('sill'), [-0.6 + sx * 0.72, 1.2, 1.28], { parent: cot });
        for (const y of [0.95, 1.45]) add(box(0.46, 0.07, 0.03), teal('sill'), [-0.6 + sx * 0.72, y, 1.315], { parent: cot, cast: false });
      }
      add(box(0.05, 0.05, 0.3), wood('sill'), [-1.42, 1.86, 1.38], { parent: cot }); add(box(0.14, 0.2, 0.14), glow('#ffb850', 2.4), [-1.42, 1.72, 1.5], { parent: cot, cast: false });
      const lantern = lampLight(cot, [-1.15, 1.6, 1.85], 1.5, 4.5);
      sack(cot, 'cottage', [1.72, 0, 1.6], { yaw: 0.8 }); sack(cot, 'cottage', [2.25, 0, 1.45], { yaw: 2.2, s: 0.92 });                               // the flour has arrived
      // the loaf, and the baker's peel that brings it out
      const loaf = group([-0.48, SILL_Y, 1.4], cot, 0.3);
      const crust = paint({ a: '#a9591c', b: '#eba445', c: '#c97a2a', size: 0.45, tilt: 0.6, at: 'sill', moving: true });
      add(new THREE.SphereGeometry(1, 20, 10, 0, Math.PI * 2, 0, Math.PI * 0.62), crust, [0, 0.055, 0], { parent: loaf, scale: [0.31, 0.17, 0.2] });
      for (const x of [-0.15, 0, 0.15]) add(new THREE.SphereGeometry(1, 8, 5), paint({ a: '#f3dba6', b: '#fff3d0', size: 0.4, at: 'sill', moving: true, emissive: '#e8c890', emissiveIntensity: 0.35 }), [x, 0.052 + 0.165 * Math.sqrt(1 - (x / 0.31) ** 2), 0.01], { parent: loaf, scale: [0.03, 0.02, 0.125], rot: [0, 0.6, 0], cast: false });   // the baker's cuts
      bake(loaf, 'sill');
      const peel = group([-0.48, SILL_Y + 0.005, 1.4], cot, 0.0);
      const peelWood = wood('sill', { a: '#8a5a34', b: '#d69a5a', moving: true });
      add(box(0.5, 0.02, 0.5), peelWood, [0, 0, 0], { parent: peel }); add(box(0.05, 0.03, 1.5), peelWood, [0, 0, -1.0], { parent: peel });
      bake(peel, 'sill');

      // ---- smoke and steam: soft ribbons of pale paint that rise and lean with the wind, rebuilt for every frame
      // from a formula of time
      const plume = (o) => {
        const N = o.n || 26, g = new THREE.PlaneGeometry(1, 1, 1, N - 1), fade = new Float32Array(N * 8), p = g.attributes.position;
        for (let i = 0; i < N * 2; i++) fade.set([1, 1, 1, Math.pow(1 - Math.floor(i / 2) / (N - 1), 1.2)], i * 4);
        g.setAttribute('color', new THREE.BufferAttribute(fade, 4));
        const mat = new THREE.MeshBasicMaterial({ color: o.color, vertexColors: true, transparent: true, depthWrite: false, side: THREE.DoubleSide });
        add(g, mat, [0, 0, 0], { cast: false }).frustumCulled = false;
        return (t, base, lean, show) => {                                                            // puffs travel up along it
          mat.opacity = o.alpha * show;
          for (let i = 0; i < N; i++) {
            const k = i / (N - 1), along = o.h * (lean * Math.pow(k, 1.5) + 0.07 * k * Math.sin(7 * k - 2.4 * t + o.seed)), y = o.h * Math.pow(k, 0.75);
            const w = o.w * (0.15 + Math.pow(k, 0.8)) * (1 + 0.28 * Math.sin(15 * k - 5 * t + o.seed)), a = Math.atan2(0.75 * Math.pow(k + 0.02, -0.25), 1.5 * lean * Math.sqrt(k));
            for (const side of [-1, 1]) p.setXYZ(i * 2 + (side + 1) / 2, base.x + (along - side * w * Math.sin(a)) * WIND[0], base.y + y + side * w * Math.cos(a), base.z + (along - side * w * Math.sin(a)) * WIND[1]);
          }
          p.needsUpdate = true;
        };
      };
      const smoke = plume({ color: '#f0dfe0', alpha: 0.62, h: 3.0, w: 0.55, seed: 0 }), CHIMNEY = V(...cotPt(-1.25, 3.62, 0.3));
      const steam = [plume({ color: '#fff4e4', alpha: 0.45, h: 0.7, w: 0.1, n: 18, seed: 1 }), plume({ color: '#fff4e4', alpha: 0.32, h: 0.42, w: 0.06, n: 14, seed: 4 })];

      // ---- birds: two bent wings and a body each, rising out of the valley and over the mill
      const wing = new THREE.BufferGeometry().setAttribute('position', new THREE.Float32BufferAttribute([0, 0, 0.2, 0, 0, -0.2, 0.5, 0.16, 0.22, 0, 0, -0.2, 0.5, 0.16, -0.12, 0.5, 0.16, 0.22, 0.5, 0.16, 0.22, 0.5, 0.16, -0.12, 1.08, 0.02, -0.26], 3));
      const birdMat = new THREE.MeshBasicMaterial({ color: '#33203a', side: THREE.DoubleSide, fog: false });
      const birds = Array.from({ length: 7 }, (_, i) => {
        const g = new THREE.Group(), L = new THREE.Mesh(wing, birdMat), R = new THREE.Mesh(wing, birdMat), body = new THREE.Mesh(new THREE.OctahedronGeometry(0.2), birdMat);
        R.scale.x = -1; body.scale.set(0.8, 0.8, 2.2); g.add(L, R, body); g.scale.setScalar(0.85 + 0.2 * K.hash(i, 3)); scene.add(g);
        const row = Math.ceil(i / 2), side = i % 2 ? 1 : -1;                                          // a loose V behind the leader
        return { g, L, R, off: V(-row * 0.8 + side * row * 0.9, K.hash(i, 1) * 0.7 - row * 0.2, -row * 1.1 - K.hash(i) * 0.5), phase: K.hash(i, 2) * 6 };
      });
      const flight = (t) => V(-9.5 + 2.4 * t, 8.6 + 0.4 * t + 0.25 * Math.sin(t * 0.9), -30 + 2.6 * t);

      const loafRest = loaf.position.clone(), peelRest = peel.position.clone(), LOAF = V(...cotPt(loafRest.x, SILL_Y + 0.2, loafRest.z));
      return {
        update(t) {
          uTime.value = t;
          // light: the sun sinks a little during the journey
          sun.target.position.set(5, 0, -2); sun.position.copy(sun.target.position).addScaledVector(sunDir(lightElev(t), 0.8), 70);   // the light comes from further to the side than the painted sun: a painter's cheat that shows more form
          sun.intensity = 5.6 * (1 - 0.1 * t / DURATION);
          skyU.uSun.value.copy(sunDir(discElev(t), 0.27)); skyU.uDusk.value = t / DURATION;

          // beat 1 -> 2: the gust that bent the wheat (in the vertex shader) reaches the trees and the sails
          for (const tr of trees) tr.g.rotation.z = -tr.bend * (0.3 * Math.sin(t * 1.7 + tr.x) + 2.2 * gustAt(tr.x, tr.z, t));
          rotor.rotation.z = -sailAngle(t);
          runner.rotation.y = STONE_GEAR * sailAngle(t - 0.15);

          // beat 3: flour runs while the stone is up to speed; the sack swells; when it is full it puffs
          const flow = K.seg(t, FLOUR_ON, FLOUR_ON + 0.5), fill = K.seg(t, FLOUR_ON, SACK_FULL, K.ease.out), plump = fill * (1 + 0.12 * (K.spring(t - SACK_FULL, 3, 0.4) - (t > SACK_FULL ? 1 : 0)));
          openSack.scale.set(0.88 + 0.12 * plump, 0.55 + 0.45 * plump, 0.88 + 0.12 * plump);
          mound.scale.set(0.7 + 0.3 * fill, 0.25 + 0.3 * fill, 0.7 + 0.3 * fill); mound.position.y = 0.3 + 0.2 * fill;
          const top = (mound.position.y + 0.25 * mound.scale.y) * openSack.scale.y - 0.06, len = Math.max(0.02, SPOUT[1] - top);
          stream.position.set(SPOUT[0], top + len / 2, SPOUT[2]); stream.scale.set(1 + 0.25 * Math.sin(t * 31), len, 1 + 0.25 * Math.sin(t * 23 + 1)); stream.material.opacity = flow; stream.visible = flow > 0;
          for (const q of puffs) {
            const age = t - q.born, m = q.s.material;
            q.s.visible = age > 0 && age < 3;
            if (!q.s.visible) continue;
            const drift = q.speed * (age - (1 - Math.exp(-1.6 * age)) / 1.6), out = 1 - Math.exp(-4 * age);   // a quick burst, then it picks up the speed of the wind
            q.s.position.set(MOUTH.x + q.burst.x * out + WIND[0] * drift, MOUTH.y + q.burst.y * out * 0.7 + q.rise * age, MOUTH.z + q.burst.z * out + WIND[1] * drift * 0.5);
            q.s.scale.setScalar(q.size * (0.6 + 1.3 * Math.pow(age, 0.6))); m.opacity = 0.34 * K.smooth(0, 0.1, age) * (1 - K.smooth(0.9, 3, age));
          }

          // beat 4: the oven is lit, smoke leans with the gust, the peel brings the loaf out and leaves it on the sill
          const warm = t < OVEN_ON ? 0.25 : 0.25 + 0.75 * K.spring(t - OVEN_ON, 2.4, 0.55) * (0.98 + 0.02 * Math.sin(t * 23) * Math.sin(t * 7.1));
          oven.emissiveIntensity = 1.7 * warm; room.emissiveIntensity = 0.3 * warm; roomLamp.intensity = 2.4 * warm;
          lantern.intensity = 1.5 * (0.985 + 0.015 * Math.sin(t * 17) * Math.sin(t * 5.3));
          smoke(t, CHIMNEY, 0.75 + 0.5 * gustAt(COT[0], COT[1], t), 1);
          const out = K.seg(t, LOAF_OUT, LOAF_DOWN, K.ease.inOut), back = K.seg(t, LOAF_DOWN + 0.08, LOAF_DOWN + 0.6, K.ease.inOut), set = K.seg(t, LOAF_DOWN + 0.05, LOAF_DOWN + 0.2, K.ease.in);
          peel.position.set(peelRest.x, peelRest.y - 0.65 * back * back, peelRest.z - 1.35 * (1 - out) - 1.2 * back);   // then it is drawn back and lowered out of sight
          loaf.position.set(loafRest.x, loafRest.y + 0.022 * (1 - set), loafRest.z - 1.35 * (1 - out));
          const settle = t > LOAF_DOWN + 0.2 ? 1 - 0.05 * Math.exp(-(t - LOAF_DOWN - 0.2) * 9) * Math.cos((t - LOAF_DOWN - 0.2) * 30) : 1;   // it lands with a tiny squash
          loaf.scale.set(1, settle, 1);
          steam.forEach((s, i) => s(t, V(LOAF.x + (i ? 0.17 : -0.07) * Math.cos(COT_YAW), LOAF.y - (i ? 0.06 : 0.03), LOAF.z - (i ? 0.17 : -0.07) * Math.sin(COT_YAW)), (i ? 0.15 : 0.4) + 0.2 * Math.sin(t * 1.3 + i * 2), K.seg(t, LOAF_DOWN - 0.3 + i * 0.3, LOAF_DOWN + 0.5 + i * 0.3)));

          const lead = flight(t), heading = flight(t + 0.1).sub(lead);
          birds.forEach((b) => {
            b.g.position.copy(lead).add(b.off); b.g.position.y += 0.12 * Math.sin(t * 2.2 + b.phase);
            b.g.lookAt(heading.clone().add(b.g.position));
            const flap = 0.5 + 0.42 * Math.sin(t * 8.5 + b.phase);                                     // wings stay above level, so a bird never turns edge-on to the lens
            b.L.rotation.z = flap; b.R.rotation.z = -flap;
          });

          K.look(camera, cam.pos(t), cam.aim(t));
          caption(t);
        },
      };
    },

    sound(k) {
      // a windy evening told in sound: the gust, the sails, the stones, flour, the oven, and one warm chord under it
      k.bed('room', 0, DURATION, { gain: 0.9 });
      k.noise(0, { dur: DURATION, color: 'pink', filter: 'bandpass', f: 520, q: 0.7, gain: 0.03, a: 1.0, hold: DURATION - 2, bus: 'amb' });   // the steady breeze
      k.withMinor(() => {
        // each gust: wind rising and passing left to right, with the dry hiss of stalks under the first one
        GUSTS.forEach((g, i) => {
          const t0 = Math.max(0, g.at - 2.6), d = g.at + 1 - t0;
          k.noise(t0, { dur: d, color: 'pink', filter: 'bandpass', f: [[0, 380], [d * 0.6, 900], [d, 420]], q: 0.8, gain: 0.2 * g.amp, a: d * 0.55, pan: -0.5 + 0.9 * i, verb: 0.2 });
          if (!i) k.noise(t0 + 0.4, { dur: 2.6, color: 'white', filter: 'highpass', f: [[0, 3200], [1.6, 5200], [2.6, 3600]], gain: 0.07, a: 1.3, pan: -0.3 });
        });
        for (const t of sailPasses()) {   // canvas pushing air, and the wooden shaft taking the load; loudest while the mill fills the frame
          const near = 0.35 + 0.65 * K.smooth(1.2, 3.0, t) * (1 - 0.85 * K.smooth(STONE_NEAR[1], STONE_NEAR[1] + 1.5, t));
          k.noise(Math.max(0, t - 0.3), { dur: 0.7, color: 'brown', filter: 'lowpass', f: [[0, 160], [0.3, 460], [0.7, 150]], gain: 0.26 * near, a: 0.3, pan: 0.3 });
          k.tone(t, { f: [[0, 96], [0.25, 78]], type: 'triangle', dur: 0.3, gain: 0.06 * near, a: 0.05, pan: 0.3 });
        }
        for (const t of BIRD_CALLS) k.tone(t, { f: [[0, 3300], [0.05, 4300], [0.11, 3600]], dur: 0.13, gain: 0.03, a: 0.01, pan: -0.5 + t * 0.12, verb: 0.5 });
        for (const w of lettering()) if (w.beat) k.play('stroke', w.t0, { dur: w.dur, material: 'brush', gain: 1.1, pan: -0.6 });
      });
      // the stones: a low rumble that comes up as the camera reaches the door, with the cog knocking on every tooth
      k.noise(STONE_NEAR[0] - 1, { dur: STONE_NEAR[1] - STONE_NEAR[0] + 2, color: 'brown', filter: 'lowpass', f: [[0, 120], [1.2, 240], [STONE_NEAR[1] - STONE_NEAR[0] + 2, 140]], gain: 0.34, a: 1.2, hold: STONE_NEAR[1] - STONE_NEAR[0] - 0.6, pan: 0.1 });
      k.withMinor(() => { for (const t of cogKnocks()) k.play('tock', t, { f: 240 + 30 * (Math.round(t * 100) % 2), gain: 0.2 * K.smooth(STONE_NEAR[0] - 0.5, STONE_NEAR[0], t) * (1 - K.smooth(STONE_NEAR[1], STONE_NEAR[1] + 0.9, t)), pan: 0.15 }); });
      k.noise(FLOUR_ON, { dur: SACK_FULL - FLOUR_ON, color: 'white', filter: 'highpass', f: 6000, gain: 0.018, a: 0.5, hold: SACK_FULL - FLOUR_ON - 1, pan: -0.15 });   // flour running
      k.play('thud', SACK_FULL, { gain: 0.28, f: 90, pan: -0.15 });                                   // the sack is full
      k.noise(SACK_FULL, { dur: 0.9, color: 'pink', filter: 'bandpass', f: [[0, 1800], [0.9, 700]], q: 0.6, gain: 0.12, a: 0.03, pan: -0.1, verb: 0.3 });
      // the bakery: the oven catches, wood slides on stone, the loaf is set down, and the chord resolves
      k.noise(OVEN_ON, { dur: 1.4, color: 'brown', filter: 'lowpass', f: [[0, 200], [0.3, 700], [1.4, 260]], gain: 0.2, a: 0.25, pan: 0.35 });
      k.tone(OVEN_ON + 0.05, { f: 'F#4', dur: 1.8, gain: 0.05, a: 0.1, pan: 0.35, verb: 0.6 });
      k.noise(LOAF_OUT, { dur: LOAF_DOWN - LOAF_OUT, color: 'pink', filter: 'bandpass', f: [[0, 900], [LOAF_DOWN - LOAF_OUT, 1500]], q: 1.4, gain: 0.07, a: 0.2, pan: 0.1 });
      k.play('tock', LOAF_DOWN + 0.2, { f: 190, gain: 0.5, pan: 0.05 });
      k.tone(LOAF_DOWN + 0.25, { f: 'D5', dur: 2.2, gain: 0.06, a: 0.02, verb: 0.7 }); k.tone(LOAF_DOWN + 0.25, { f: 'A5', dur: 2.0, gain: 0.03, a: 0.02, verb: 0.7 }); k.tone(LOAF_DOWN + 0.25, { f: 'F#5', dur: 2.0, gain: 0.03, a: 0.02, verb: 0.7 });
    },
  });
}
```