{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ray-marched-fan-of-painted-colour-chips-on-a-brass-rivet-that-washes-a-board-model-of-a-homepage-in-the-colour-you-pick","type":"registry:block","title":"Ray Marched Fan of Painted Colour Chips on a Brass Rivet That Washes a Board Model of a Homepage in the Colour You Pick","description":"Free React and WebGL colour picker. Swing a painted chip out of a riveted fan and its colour spreads like wet paint over a sunlit model of a homepage.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/ray-marched-fan-of-painted-colour-chips-on-a-brass-rivet-that-washes-a-board-model-of-a-homepage-in-the-colour-you-pick.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"framer-motion\";\n\n/** The painted chips on the rivet: an invented name, a short code printed on the chip, and the paint colour. The first one is out on load. */\nconst CHIPS = [\n  { name: \"Morning Blue\", code: \"No. 01\", hex: \"#4F98FB\", note: \"Clear, calm and easy to trust.\" },\n  { name: \"Kiln Ember\", code: \"No. 02\", hex: \"#E0582E\", note: \"Warm, loud and hard to miss.\" },\n  { name: \"Fig Leaf\", code: \"No. 03\", hex: \"#3B8A5A\", note: \"Green from the shady side of the garden.\" },\n  { name: \"Saffron Hour\", code: \"No. 04\", hex: \"#EFAE35\", note: \"Late sun on a whitewashed wall.\" },\n  { name: \"Plum Dusk\", code: \"No. 05\", hex: \"#7A3C76\", note: \"Deep, soft and a little theatrical.\" },\n  { name: \"Late Ink\", code: \"No. 06\", hex: \"#24304A\", note: \"Almost black, with the blue left in.\" },\n];\n\nconst BLUE = \"#4F98FB\";\nconst INK = \"#0B0D12\";\nconst FONT = '\"Inter Tight\", \"Inter\", ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", sans-serif';\nconst MONO = 'ui-monospace, \"SF Mono\", Menlo, Consolas, monospace';\n/** Seconds the self-playing demo rests on each colour before taking the next chip. */\nconst HOLD = 5.4;\n/** Seconds the wet paint takes to spread across the whole model. */\nconst SPREAD = 2.4;\n/** Lowest and highest render scale (canvas pixels per CSS pixel). The loop steps down when frames run slow. */\nconst MIN_SCALE = 0.5;\nconst MAX_SCALE = 1.5;\n/** Most pixels ever sent through the ray marcher in one frame. */\nconst PIXEL_BUDGET = 1.6e6;\n\n/* ---------- the parts of the homepage model that stand proud of the board ----------\n   x, y: centre on the board face (the board is 1.72 wide and 1.24 tall, y from its foot);\n   w, h: half sizes; r: corner radius; lift: how far it stands out; paint: true for the parts the colour washes over. */\ntype Part = { x: number; y: number; w: number; h: number; r: number; lift: number; paint: boolean };\nconst PARTS: Part[] = [\n  // menu row\n  { x: -0.73, y: 1.12, w: 0.035, h: 0.035, r: 0.035, lift: 0.016, paint: false },\n  { x: 0.08, y: 1.12, w: 0.06, h: 0.011, r: 0.011, lift: 0.008, paint: false },\n  { x: 0.25, y: 1.12, w: 0.06, h: 0.011, r: 0.011, lift: 0.008, paint: false },\n  { x: 0.42, y: 1.12, w: 0.05, h: 0.011, r: 0.011, lift: 0.008, paint: false },\n  { x: 0.67, y: 1.12, w: 0.12, h: 0.036, r: 0.036, lift: 0.02, paint: true },\n  // headline band, two headline lines on it and a picture block\n  { x: 0, y: 0.8, w: 0.79, h: 0.215, r: 0.022, lift: 0.012, paint: true },\n  { x: -0.33, y: 0.88, w: 0.4, h: 0.044, r: 0.012, lift: 0.032, paint: false },\n  { x: -0.42, y: 0.765, w: 0.31, h: 0.044, r: 0.012, lift: 0.032, paint: false },\n  { x: 0.47, y: 0.8, w: 0.25, h: 0.175, r: 0.014, lift: 0.05, paint: false },\n  // the two buttons\n  { x: -0.6, y: 0.47, w: 0.17, h: 0.046, r: 0.046, lift: 0.026, paint: true },\n  { x: -0.22, y: 0.47, w: 0.15, h: 0.046, r: 0.046, lift: 0.014, paint: false },\n  // three cards with painted tops\n  { x: -0.54, y: 0.2, w: 0.245, h: 0.135, r: 0.016, lift: 0.018, paint: false },\n  { x: 0, y: 0.2, w: 0.245, h: 0.135, r: 0.016, lift: 0.018, paint: false },\n  { x: 0.54, y: 0.2, w: 0.245, h: 0.135, r: 0.016, lift: 0.018, paint: false },\n  { x: -0.54, y: 0.3, w: 0.245, h: 0.032, r: 0.016, lift: 0.026, paint: true },\n  { x: 0, y: 0.3, w: 0.245, h: 0.032, r: 0.016, lift: 0.026, paint: true },\n  { x: 0.54, y: 0.3, w: 0.245, h: 0.032, r: 0.016, lift: 0.026, paint: true },\n];\n\n/* ---------- small helpers ---------- */\n\nconst f = (n: number) => n.toFixed(5);\nconst linear = (hex: string) => [1, 3, 5].map((i) => Math.pow(parseInt(hex.slice(i, i + 2), 16) / 255, 2.2));\n\n/** The fan: the angle of each chip round the rivet while it is in the deck, and the angle of the one that is out. */\nconst FAN = CHIPS.map((_, i) => 1.02 + i * 0.15);\nconst OUT = 0.3;\n/** Where the rivet and the middle of the model's base sit on the table (x, z), wide screens then phones. */\nconst PLACE = { wide: { rivet: [-1.5, 0.92], model: [0.66, -0.46] }, phone: { rivet: [-0.85, 1.3], model: [0.3, -0.5] } };\n/** How far each chip tips down toward the table from the rivet: in the deck, and once it is out. */\nconst LIE_DECK = 0.6;\nconst LIE_OUT = 0.9;\n\nconst partsGLSL = (paint: boolean) =>\n  PARTS.filter((p) => p.paint === paint)\n    .map((p) => `d = min(d, ext(rb2(b.xy - vec2(${f(p.x)}, ${f(p.y)}), vec2(${f(p.w)}, ${f(p.h)}), ${f(p.r)}), b.z - ${f(0.02 + p.lift / 2)}, ${f(p.lift / 2)}, 0.0035));`)\n    .join(\"\\n  \");\n\nconst VERT = `#version 300 es\nin vec2 a; out vec2 vUv;\nvoid main(){ vUv = a * 0.5 + 0.5; gl_Position = vec4(a, 0.0, 1.0); }`;\n\nconst SCENE = `#version 300 es\nprecision highp float;\nin vec2 vUv; out vec4 o;\nuniform vec2 uRes;\nuniform vec3 uCam, uTarget;\nuniform float uFov, uTime;\nuniform vec4 uChip[6];   // angle round the rivet, lift, how far it lies down onto the table, layer in the stack\nuniform vec3 uCol[6];    // paint colours, linear\nuniform vec3 uOld, uNew; // colour on the model before and after the wash\nuniform float uR, uDry;  // how far the wash has spread, and how dry it is\nuniform sampler2D uLab;  // printed names on the chips\n\nconst vec3 L = vec3(-0.7010, 0.5842, 0.4089);\nconst vec3 SUNC = vec3(1.0, 0.93, 0.82);\nconst float SUNI = 2.15;\nuniform vec3 R0;   // the rivet on the table\nuniform vec3 BP;   // the middle of the model's base\nconst float LEAN = 0.2;\nconst vec3 TABLE = vec3(0.86, 0.85, 0.82);\nconst vec3 CARD = vec3(0.93, 0.925, 0.91);\nconst float WALLZ = -3.6;\n\nfloat gPix, gDist;\nint gChip;\n\nfloat h21(vec2 p){ vec3 q = fract(vec3(p.xyx) * 0.1031); q += dot(q, q.yzx + 33.33); return fract((q.x + q.y) * q.z); }\nfloat n2(vec2 x){ vec2 i = floor(x), f = fract(x); f = f * f * (3.0 - 2.0 * f);\n  return mix(mix(h21(i), h21(i + vec2(1.0, 0.0)), f.x), mix(h21(i + vec2(0.0, 1.0)), h21(i + vec2(1.0, 1.0)), f.x), f.y); }\nfloat fbm(vec2 p){ float a = 0.5, s = 0.0; for (int i = 0; i < 4; i++){ s += a * n2(p); p = p * 2.03 + 7.1; a *= 0.5; } return s; }\nvec3 tone(vec3 x){ vec3 c = 1.0 - exp(-max(x, 0.0) * 0.82); c = mix(c, c * c * (3.0 - 2.0 * c), 0.34); return pow(c, vec3(1.0 / 2.2)); }\n\nfloat rb2(vec2 p, vec2 h, float r){ vec2 d = abs(p) - h + r; return length(max(d, 0.0)) + min(max(d.x, d.y), 0.0) - r; }\nfloat ext(float d2, float y, float h, float bev){ vec2 w = vec2(d2, abs(y) - h) + bev; return min(max(w.x, w.y), 0.0) + length(max(w, 0.0)) - bev; }\n\n/* ---- the chips ---- */\nfloat layerY(float k){ return 0.0118 + k * 0.0236; }\n// position in the chip's own frame: x along its length from the rivet, y up from its middle, z across\nvec3 chipLocal(vec3 p, int i){\n  vec4 c = uChip[i];\n  vec3 q = p - R0;\n  float ca = cos(c.x), sa = sin(c.x);\n  float lx = ca * q.x - sa * q.z;\n  float lz = sa * q.x + ca * q.z;\n  float t = clamp(lx / 1.5, 0.0, 1.0);\n  float y0 = layerY(c.w) * (1.0 - c.z * t) + c.y * t;  // a chip is lifted by its far end\n  // a card bends to meet the table instead of folding at it\n  float k = 0.02; y0 = 0.0112 + 0.5 * ((y0 - 0.0112) + sqrt((y0 - 0.0112) * (y0 - 0.0112) + k * k)) - 0.5 * k;\n  return vec3(lx, q.y - y0, lz);\n}\nfloat sdChip(vec3 p, int i){\n  vec3 l = chipLocal(p, i);\n  float d2 = max(rb2(vec2(l.x - 0.67, l.z), vec2(0.8, 0.205), 0.034), 0.05 - length(l.xz));\n  return ext(d2, l.y, 0.0102, 0.0034) * 0.85;\n}\nfloat sdRivet(vec3 p){\n  vec3 q = p - R0;\n  float top = layerY(5.0) + 0.012;\n  float shaft = max(length(q.xz) - 0.038, abs(q.y - top * 0.5) - top * 0.5);\n  vec3 e = (q - vec3(0.0, top, 0.0)) / vec3(0.074, 0.03, 0.074);\n  float cap = (length(e) - 1.0) * 0.03;\n  return min(shaft, max(cap, top - 0.004 - q.y));\n}\n\n/* ---- the board model ---- */\nvec3 toB(vec3 p){ vec3 q = p - vec3(BP.x, 0.085, BP.z); float c = cos(LEAN), s = sin(LEAN); return vec3(q.x, c * q.y - s * q.z, s * q.y + c * q.z); }\nfloat sdBase(vec3 p){ return ext(rb2(p.xz - BP.xz, vec2(0.98, 0.18), 0.03), p.y - 0.0425, 0.0425, 0.01); }\nfloat partsW(vec3 b){ float d = 1e5;\n  ${partsGLSL(false)}\n  return d; }\nfloat partsP(vec3 b){ float d = 1e5;\n  ${partsGLSL(true)}\n  return d; }\n\nvec2 map(vec3 p){\n  vec2 r = vec2(1e5, 0.0);\n  // chips and rivet\n  float bc = length(p.xz - R0.xz + vec2(-0.5, 0.25)) - 1.35;\n  if (p.y > 0.26 || bc > 0.25){ r = vec2(max(bc, p.y - 0.24), 0.0); }\n  else {\n    for (int i = 0; i < 6; i++){\n      float d = sdChip(p, i);\n      if (d < r.x){ r = vec2(d, 1.0); gChip = i; }\n    }\n    float d = sdRivet(p); if (d < r.x) r = vec2(d, 2.0);\n  }\n  // board on its base\n  vec3 b = toB(p);\n  float bb = length(b - vec3(0.0, 0.6, 0.0)) - 1.15;\n  if (bb > 0.25){ if (bb < r.x) r = vec2(bb, 0.0); return r; }\n  float d = sdBase(p); if (d < r.x) r = vec2(d, 3.0);\n  d = min(ext(rb2(vec2(b.x, b.y - 0.62), vec2(0.86, 0.62), 0.024), b.z, 0.02, 0.006), partsW(b));\n  if (d < r.x) r = vec2(d, 4.0);\n  d = partsP(b); if (d < r.x) r = vec2(d, 5.0);\n  return r;\n}\n// the board, its base and the rivet: what the table needs to march for shadow (the chips are done exactly below)\nfloat mapB(vec3 p){\n  float d = sdRivet(p);\n  vec3 b = toB(p);\n  float bb = length(b - vec3(0.0, 0.6, 0.0)) - 1.15;\n  if (bb > 0.25) return min(d, bb);\n  d = min(d, sdBase(p));\n  d = min(d, min(ext(rb2(vec2(b.x, b.y - 0.62), vec2(0.86, 0.62), 0.024), b.z, 0.02, 0.006), min(partsW(b), partsP(b))));\n  return d;\n}\nfloat shadowB(vec3 ro, vec3 rd){\n  float res = 1.0, t = 0.01;\n  for (int i = 0; i < 32; i++){\n    float h = mapB(ro + rd * t);\n    res = min(res, 9.0 * h / t);\n    t += clamp(h, 0.01 + t * 0.02, 0.25);\n    if (res < 0.003 || t > 4.0) break;\n  }\n  res = clamp(res, 0.0, 1.0);\n  return res * res * (3.0 - 2.0 * res);\n}\n// Each chip is a flat card a few millimetres up: slide the table point toward the sun to the chip's height and ask whether it lands on the card.\nfloat chipShadow(vec3 p, float above){\n  float s = 1.0;\n  for (int i = 0; i < 6; i++){\n    vec4 c = uChip[i];\n    if (c.w <= above) continue;\n    float hgt = layerY(c.w) * (1.0 - 0.5 * c.z) + c.y * 0.5 + 0.01 - p.y;\n    if (hgt <= 0.0) continue;\n    vec3 l = chipLocal(p + L * (hgt / L.y), i);\n    float d = max(rb2(vec2(l.x - 0.67, l.z), vec2(0.8, 0.205), 0.034), 0.05 - length(l.xz));\n    float pen = 0.003 + hgt * 0.35;\n    s *= mix(0.08, 1.0, smoothstep(-pen, pen, d));\n  }\n  return s;\n}\nvec3 normal(vec3 p){\n  vec2 e = vec2(1.0, -1.0) * 0.0009;\n  return normalize(e.xyy * map(p + e.xyy).x + e.yyx * map(p + e.yyx).x + e.yxy * map(p + e.yxy).x + e.xxx * map(p + e.xxx).x);\n}\nvec2 march(vec3 ro, vec3 rd, float far){\n  float t = 0.0; vec2 h = vec2(0.0);\n  for (int i = 0; i < 110; i++){\n    h = map(ro + rd * t);\n    if (h.x < 0.00025 * (1.0 + t * 0.4) || t > far) break;\n    t += h.x;\n  }\n  return vec2(t, (t > far || h.x > 0.01) ? 0.0 : h.y);\n}\nfloat shadow(vec3 ro, vec3 rd){\n  float res = 1.0, t = 0.006;\n  for (int i = 0; i < 36; i++){\n    float h = map(ro + rd * t).x;\n    res = min(res, 11.0 * h / t);\n    t += clamp(h, 0.004 + t * 0.03, 0.25);\n    if (res < 0.003 || t > 4.0) break;\n  }\n  res = clamp(res, 0.0, 1.0);\n  return res * res * (3.0 - 2.0 * res);\n}\nfloat occ(vec3 p, vec3 n){\n  float o = 0.0, s = 1.0;\n  for (int i = 0; i < 5; i++){\n    float h = 0.004 + 0.06 * float(i) / 4.0;\n    o += max(h - map(p + n * h).x, 0.0) * s;\n    s *= 0.75;\n  }\n  return clamp(1.0 - 4.0 * o, 0.0, 1.0);\n}\n\n// Leaves outside the window break the sunlight up at the edges of the table; the middle stays in full sun.\nfloat leaves(vec3 p){\n  vec2 pp = p.xz - L.xz / L.y * p.y;\n  float sway = sin(uTime * 0.45) * 0.035;\n  float g = fbm(pp * 1.25 + vec2(sway, sway * 0.6) + 3.0) + 0.25 * n2(pp * 5.0 + sway * 3.0);\n  float leafy = smoothstep(0.52, 0.7, g);\n  float reach = smoothstep(1.4, 3.0, length((p.xz - vec2(-0.2, -0.05)) * vec2(0.8, 1.15)));\n  return 1.0 - 0.62 * leafy * reach;\n}\nvec3 fillL(vec3 n){ return mix(vec3(0.62, 0.6, 0.58), vec3(0.72, 0.78, 0.9), 0.5 + 0.5 * n.y); }\nfloat rectL(vec3 ro, vec3 rd, vec3 c, vec3 n, vec2 hs, float soft){\n  float dn = dot(rd, n); if (dn > -1e-4) return 0.0;\n  float t = dot(c - ro, n) / dn; if (t < 0.0) return 0.0;\n  vec3 q = ro + rd * t - c; float s = soft * t + 0.02;\n  vec3 u = normalize(cross(vec3(0.0, 1.0, 0.0), n)), v = cross(n, u);\n  vec2 d = abs(vec2(dot(q, u), dot(q, v))) - hs;\n  return smoothstep(s, -s, d.x) * smoothstep(s, -s, d.y);\n}\n// The bright room around the table: what gloss, brass and wet paint reflect.\nvec3 env(vec3 ro, vec3 rd, float rough){\n  float soft = 0.03 + rough * 0.8;\n  vec3 c = mix(vec3(1.25, 1.2, 1.12), vec3(1.5, 1.5, 1.52), smoothstep(-0.3 - rough * 0.3, 0.6 + rough * 0.3, rd.y));\n  c *= 0.75 + 0.25 * smoothstep(-0.6, 0.4, rd.y);\n  c += SUNC * 9.0 * rectL(ro, rd, L * 8.0, -L, vec2(1.8, 2.6), soft);\n  c += vec3(0.95, 0.97, 1.0) * 2.4 * rectL(ro, rd, vec3(-5.0, 3.8, 6.0), normalize(vec3(5.0, -3.0, -6.0)), vec2(2.2, 1.6), soft + 0.05);\n  return c;\n}\n\nvec3 sunlight(vec3 p, vec3 n, bool hq){\n  float ndl = max(dot(n, L), 0.0);\n  if (ndl <= 0.0) return vec3(0.0);\n  float sh = hq ? shadow(p + n * 0.003, L) : 1.0;\n  return SUNC * SUNI * ndl * sh * leaves(p);\n}\nfloat spec(vec3 n, vec3 rd, float rough){\n  vec3 h = normalize(L - rd);\n  float a = rough * rough;\n  float nh = max(dot(n, h), 0.0);\n  float dd = nh * nh * (a * a - 1.0) + 1.0;\n  return a * a / (3.14159 * dd * dd) * max(dot(n, L), 0.0);\n}\nfloat fres(vec3 n, vec3 rd, float f0){ return f0 + (1.0 - f0) * pow(1.0 - max(dot(n, -rd), 0.0), 5.0); }\n\n/* ---- materials ---- */\nvec3 shadeChip(vec3 p, vec3 n, int i, vec3 rd, bool hq){\n  vec3 l = chipLocal(p, i);\n  vec4 c = uChip[i];\n  float ca = cos(c.x), sa = sin(c.x);\n  vec3 across = vec3(sa, 0.0, ca);\n  vec3 along = vec3(ca, 0.0, -sa);\n  bool top = n.y > 0.75 && l.y > 0.003;\n  bool label = top && l.x > 1.0 && l.x < 1.43 && abs(l.z) < 0.168;\n  vec3 alb; float rough = 0.62; float f0 = 0.035;\n  if (label){\n    vec2 uv = vec2((l.x - 1.0) / 0.43, (float(i) + (l.z + 0.168) / 0.336) / 6.0);\n    alb = pow(texture(uLab, uv).rgb, vec3(2.2));\n    rough = 0.5;\n  } else if (top || l.y > 0.0045){\n    // brushed paint: streaks run along the chip, with a little ridge where the bristles dragged\n    float seed = float(i) * 13.1;\n    vec2 sp = vec2(l.x * 2.2 + seed, l.z * 95.0);\n    float s1 = n2(sp) * 0.62 + n2(sp * vec2(2.6, 2.7) + 4.0) * 0.38;\n    float s2 = n2(sp + vec2(0.0, 0.18)) * 0.62 + n2((sp + vec2(0.0, 0.18)) * vec2(2.6, 2.7) + 4.0) * 0.38;\n    float blot = n2(l.xz * vec2(9.0, 14.0) + seed);\n    alb = uCol[i] * (0.9 + 0.17 * s1 + 0.06 * (blot - 0.5));\n    if (top) n = normalize(n + across * (s2 - s1) * 0.9 + along * (blot - 0.5) * 0.04);\n    rough = 0.55 - 0.1 * s1;\n  } else {\n    // the card core shows on the cut edges\n    alb = CARD * (0.86 + 0.06 * n2(l.xz * 300.0));\n    rough = 0.85;\n  }\n  float ao = hq ? occ(p, n) : 1.0;\n  float sh = hq ? shadowB(p + n * 0.003, L) * chipShadow(p, c.w) : 1.0;\n  vec3 col = alb * (SUNC * SUNI * max(dot(n, L), 0.0) * sh * leaves(p) + fillL(n) * ao);\n  col += spec(n, rd, rough) * SUNC * SUNI * 0.25 * sh;\n  col += fres(n, rd, f0) * env(p, reflect(rd, n), rough) * ao * 0.5;\n  return col;\n}\nvec3 shadeRivet(vec3 p, vec3 n, vec3 rd, bool hq){\n  vec3 brass = vec3(0.93, 0.66, 0.32);\n  float ao = hq ? occ(p, n) : 1.0;\n  vec3 r = reflect(rd, n);\n  vec3 F = brass + (1.0 - brass) * pow(1.0 - max(dot(n, -rd), 0.0), 5.0);\n  vec3 col = F * env(p, r, 0.22) * (0.35 + 0.65 * ao);\n  col += F * spec(n, rd, 0.22) * SUNC * SUNI * (hq ? shadow(p + n * 0.003, L) : 1.0);\n  col += brass * 0.08 * sunlight(p, n, false);\n  return col;\n}\n// how far the wash has reached at a point on the board, and how wet the paint is there\nvec3 wash(vec2 b){\n  float d = length((b - vec2(-0.95, -0.05)) * vec2(1.0, 1.15));\n  d += (fbm(b * 3.2 + 1.7) - 0.5) * 0.42 + (n2(b * 15.0 + 4.0) - 0.5) * 0.07;\n  float k = smoothstep(uR + 0.012, uR - 0.012, d);\n  float age = max(uR - d, 0.0);\n  float wet = k * exp(-age * 1.3) * (1.0 - uDry);\n  float rim = exp(-pow((d - uR + 0.035) / 0.04, 2.0)) * (1.0 - uDry) * step(0.02, uR);\n  return vec3(k, wet, rim);\n}\nvec3 shadeBoard(vec3 p, vec3 n, bool painted, vec3 rd, bool hq){\n  vec3 b = toB(p);\n  float ao = hq ? occ(p, n) : 1.0;\n  vec3 alb; float rough = 0.8; float wet = 0.0;\n  if (painted){\n    vec3 w = wash(b.xy);\n    alb = mix(uOld, uNew, w.x);\n    alb *= 1.0 - 0.3 * w.z;               // the bead of paint at the edge is thicker and darker\n    alb *= 1.0 - 0.32 * w.y;               // wet paint reads deeper\n    alb *= 0.97 + 0.06 * n2(b.xy * 140.0);\n    wet = clamp(w.y + w.z, 0.0, 1.0);\n    rough = mix(0.5, 0.07, wet);\n  } else {\n    alb = CARD * (0.97 + 0.03 * n2(b.xy * 220.0));\n    // printed lines on the cards and in the picture block\n    bool face = b.z > 0.035;\n    if (face && b.y < 0.33 && b.y > 0.07){\n      float cx = mod(b.x + 0.81, 0.54) - 0.27;\n      float row = b.y - 0.13;\n      float lines = step(abs(fract(row / 0.04) - 0.5), 0.17) * step(-0.25, cx) * step(cx, 0.15 - 0.14 * step(row, 0.05)) * step(row, 0.11) * step(-0.02, row);\n      alb *= 1.0 - 0.32 * lines;\n    }\n    if (b.z > 0.065 && abs(b.x - 0.47) < 0.23 && abs(b.y - 0.8) < 0.16){\n      // the picture block on the model is a soft grey hill under a pale sky\n      float hill = smoothstep(0.0, 0.01, (b.y - 0.72) - 0.05 * sin(b.x * 9.0 + 1.0));\n      alb = mix(vec3(0.55, 0.56, 0.55), vec3(0.83, 0.84, 0.85), hill);\n    }\n  }\n  vec3 col = alb * (sunlight(p, n, hq) + fillL(n) * ao);\n  // colour from the painted band reaches the white card round it\n  if (!painted){\n    vec3 w = wash(b.xy);\n    vec3 nearC = mix(uOld, uNew, w.x);\n    float near = smoothstep(0.32, 0.0, abs(b.y - 0.8) - 0.2) * step(abs(b.x), 0.82);\n    col += alb * nearC * near * 0.12 * SUNI * (b.z > 0.03 ? 1.0 : 0.3);\n  }\n  float sh = hq ? shadow(p + n * 0.003, L) : 1.0;\n  col += spec(n, rd, rough) * SUNC * SUNI * mix(0.15, 2.4, wet) * sh;\n  col += fres(n, rd, mix(0.03, 0.05, wet)) * env(p, reflect(rd, n), rough) * ao * mix(0.4, 1.6, wet);\n  if (painted){\n    // wet paint holds a soft sheen of the bright room, broken where the film is thin\n    float film = smoothstep(0.5, 0.78, fbm(b.xy * vec2(4.0, 11.0) + 2.0));\n    col += vec3(0.95, 0.97, 1.0) * wet * film * 0.3 * (0.5 + 0.5 * sh);\n  }\n  return col;\n}\nvec3 shade(vec3 p, vec3 n, float id, vec3 rd, bool hq){\n  if (id < 1.5) return shadeChip(p, n, gChip, rd, hq);\n  if (id < 2.5) return shadeRivet(p, n, rd, hq);\n  if (id < 3.5){\n    float ao = hq ? occ(p, n) : 1.0;\n    return CARD * 0.97 * (sunlight(p, n, hq) + fillL(n) * ao) + fres(n, rd, 0.03) * env(p, reflect(rd, n), 0.7) * 0.4;\n  }\n  return shadeBoard(p, n, id > 4.5, rd, hq);\n}\n\n/* ---- the table and the wall behind it ---- */\nvec3 shadeTable(vec3 p, vec3 rd, float t){\n  vec3 n = vec3(0.0, 1.0, 0.0);\n  // painted oak: grain under a white finish\n  float grain = n2(vec2(p.x * 1.6, p.z * 30.0 + n2(p.xz * 2.0) * 3.0));\n  vec3 alb = TABLE * (0.975 + 0.04 * grain + 0.015 * (n2(p.xz * 60.0) - 0.5));\n  float near = step(t, 12.0);\n  float sh = near > 0.5 ? shadowB(p + n * 0.002, L) * chipShadow(p, -1.0) : 1.0;\n  float ao = near > 0.5 ? occ(p, n) : 1.0;\n  vec3 col = alb * (SUNC * SUNI * max(L.y, 0.0) * sh * leaves(p) + fillL(n) * ao);\n  // coloured light thrown down by the painted model and by the chips\n  float wf = clamp(uR / 2.3, 0.0, 1.0);\n  vec3 bc = mix(uOld, uNew, wf);\n  float dz = p.z - (BP.z + 0.16);\n  float fr = step(0.0, dz) * exp(-dz * 2.6) * smoothstep(1.25, 0.55, abs(p.x - BP.x));\n  col += alb * bc * fr * 1.3 * (0.4 + 0.6 * sh);\n  for (int i = 0; i < 6; i++){\n    vec3 l = chipLocal(p, i);\n    float d = rb2(vec2(l.x - 0.67, l.z), vec2(0.8, 0.205), 0.034);\n    col += alb * uCol[i] * exp(-max(d, 0.0) * 22.0) * step(0.0, d) * 0.4 * (0.3 + 0.7 * sh);\n  }\n  // a satin finish: a soft, faint mirror of the room\n  float f = fres(n, rd, 0.02) * 0.5;\n  col = mix(col, env(p, reflect(rd, n), 0.45), f);\n  return col;\n}\nvec3 shadeWall(vec3 p, vec3 rd){\n  vec3 n = vec3(0.0, 0.0, 1.0);\n  vec3 alb = vec3(0.9, 0.885, 0.86) * (0.98 + 0.04 * n2(p.xy * 9.0));\n  float ndl = max(dot(n, L), 0.0);\n  return alb * (SUNC * SUNI * ndl * leaves(p) * 0.85 + fillL(n));\n}\n\nvoid main(){\n  gPix = 2.0 * tan(uFov * 0.5) / uRes.y;\n  vec2 uv = (gl_FragCoord.xy - 0.5 * uRes) / uRes.y;\n  vec3 fw = normalize(uTarget - uCam), rgt = normalize(cross(fw, vec3(0.0, 1.0, 0.0))), up = cross(rgt, fw);\n  vec3 rd = normalize(fw + (uv.x * rgt + uv.y * up) * 2.0 * tan(uFov * 0.5));\n  vec3 ro = uCam;\n  float tt = rd.y < -1e-5 ? -ro.y / rd.y : 1e9;\n  float tw = rd.z < -1e-5 ? (WALLZ - ro.z) / rd.z : 1e9;\n  bool tableFirst = tt < tw;\n  float ts = min(tt, tw);\n  vec2 hit = march(ro, rd, min(ts, 30.0));\n  float t; vec3 col;\n  if (hit.y > 0.5 && hit.x < ts){\n    t = hit.x; vec3 p = ro + rd * t; gDist = t;\n    float id = hit.y; int ci = gChip;\n    vec3 n = normal(p); gChip = ci;\n    col = shade(p, n, id, rd, true);\n  } else if (ts < 1e8){\n    t = ts; vec3 p = ro + rd * ts;\n    col = tableFirst ? shadeTable(p, rd, ts) : shadeWall(p, rd);\n  } else { t = 30.0; col = env(ro, rd, 0.3); }\n  o = vec4(tone(col), clamp(t / 20.0, 0.0, 1.0));\n}`;\n\nconst POST = `#version 300 es\nprecision highp float;\nin vec2 vUv; out vec4 o;\nuniform sampler2D uSrc;\nuniform vec2 uRes;\nuniform float uFocus, uTime;\nfloat hash(vec2 p){ return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453); }\nvoid main(){\n  vec4 c0 = texture(uSrc, vUv);\n  float z = c0.a * 20.0;\n  {\n    vec2 e = 1.0 / uRes; vec3 Lm = vec3(0.299, 0.587, 0.114);\n    float nw = dot(textureLod(uSrc, vUv + vec2(-e.x, -e.y), 0.0).rgb, Lm), ne = dot(textureLod(uSrc, vUv + vec2(e.x, -e.y), 0.0).rgb, Lm);\n    float sw = dot(textureLod(uSrc, vUv + vec2(-e.x, e.y), 0.0).rgb, Lm), se = dot(textureLod(uSrc, vUv + e, 0.0).rgb, Lm), lm = dot(c0.rgb, Lm);\n    float lo = min(lm, min(min(nw, ne), min(sw, se))), hi = max(lm, max(max(nw, ne), max(sw, se)));\n    vec2 dir = vec2(-((nw + ne) - (sw + se)), (nw + sw) - (ne + se));\n    float rcp = 1.0 / (min(abs(dir.x), abs(dir.y)) + max((nw + ne + sw + se) * 0.03125, 0.0078));\n    dir = clamp(dir * rcp, -8.0, 8.0) * e;\n    vec3 a = 0.5 * (textureLod(uSrc, vUv + dir * (1.0 / 3.0 - 0.5), 0.0).rgb + textureLod(uSrc, vUv + dir * (2.0 / 3.0 - 0.5), 0.0).rgb);\n    vec3 b = a * 0.5 + 0.25 * (textureLod(uSrc, vUv - dir * 0.5, 0.0).rgb + textureLod(uSrc, vUv + dir * 0.5, 0.0).rgb);\n    float lb = dot(b, Lm);\n    c0.rgb = (lb < lo || lb > hi) ? a : b;\n  }\n  float px = uRes.y / 720.0;\n  float coc = clamp(abs(z - uFocus) / max(z, 0.1) * (z < uFocus ? 9.0 : 6.5) - 0.35, 0.0, 7.0) * px;\n  vec3 acc = c0.rgb; float ws = 1.0;\n  float j = hash(gl_FragCoord.xy) * 6.283;\n  float lod = log2(max(coc / 3.0, 1.0));\n  for (int i = 0; i < 14; i++){\n    float fi = (float(i) + 0.5) / 14.0;\n    float a = float(i) * 2.39996 + j;\n    vec2 off = vec2(cos(a), sin(a)) * sqrt(fi) * coc / uRes;\n    acc += textureLod(uSrc, vUv + off, lod).rgb; ws += 1.0;\n  }\n  vec3 col = acc / ws;\n  vec3 bl = textureLod(uSrc, vUv, 4.0).rgb * 0.5 + textureLod(uSrc, vUv, 6.0).rgb * 0.5;\n  col += bl * bl * 0.03;\n  vec2 v = vUv - 0.5;\n  col *= 1.0 - dot(v, v) * 0.16;\n  float g = hash(gl_FragCoord.xy + fract(uTime) * 91.7) - 0.5;\n  col += g * 0.018 * (1.0 - 0.4 * col);\n  o = vec4(col, 1.0);\n}`;\n\n/* ---------- motion ---------- */\n\ntype Spring = { x: number; v: number; t: number; k: number; c: number };\nconst spring = (x: number, k: number, c: number): Spring => ({ x, v: 0, t: x, k, c });\nconst stepSpring = (s: Spring, dt: number) => {\n  s.v += (-s.k * (s.x - s.t) - s.c * s.v) * dt;\n  s.x += s.v * dt;\n};\n\n/** Draw the printed name strip of every chip into one strip of labels, one row per chip. */\nfunction printLabels(cv: HTMLCanvasElement) {\n  const c = cv.getContext(\"2d\");\n  if (!c) return;\n  const W = cv.width;\n  const H = cv.height / CHIPS.length;\n  CHIPS.forEach((chip, i) => {\n    const y = i * H;\n    c.fillStyle = \"#EFEBE3\";\n    c.fillRect(0, y, W, H);\n    c.fillStyle = chip.hex;\n    c.fillRect(0, y, 10, H);\n    c.fillStyle = \"#1A1C22\";\n    c.textBaseline = \"alphabetic\";\n    c.font = `800 62px ${FONT}`;\n    (c as unknown as { letterSpacing: string }).letterSpacing = \"-2px\";\n    const [a, b] = chip.name.split(\" \");\n    c.fillText(a, 34, y + 86);\n    c.fillText(b ?? \"\", 34, y + 146);\n    c.font = `600 30px ${MONO}`;\n    (c as unknown as { letterSpacing: string }).letterSpacing = \"0px\";\n    c.fillStyle = \"#4A4E58\";\n    c.fillText(chip.code, 36, y + H - 26);\n  });\n}\n\nexport default function PaintChipFanColourWash() {\n  const reduce = useReducedMotion();\n  const [pick, setPick] = useState(0);\n  const [ready, setReady] = useState(false);\n  const rootRef = useRef<HTMLElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const want = useRef(0);\n  const api = useRef<{ choose: (i: number) => void; at: (x: number, y: number) => number } | null>(null);\n\n  useEffect(() => {\n    const root = rootRef.current;\n    const canvas = canvasRef.current;\n    if (!root || !canvas) return;\n    const gl = canvas.getContext(\"webgl2\", { antialias: false, alpha: false, powerPreference: \"high-performance\" });\n    if (!gl) return;\n    const compile = (type: number, src: string) => {\n      const s = gl.createShader(type)!;\n      gl.shaderSource(s, src);\n      gl.compileShader(s);\n      if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(s) || \"shader\");\n      return s;\n    };\n    const link = (fs: string) => {\n      const p = gl.createProgram()!;\n      gl.attachShader(p, compile(gl.VERTEX_SHADER, VERT));\n      gl.attachShader(p, compile(gl.FRAGMENT_SHADER, fs));\n      gl.bindAttribLocation(p, 0, \"a\");\n      gl.linkProgram(p);\n      if (!gl.getProgramParameter(p, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(p) || \"link\");\n      return p;\n    };\n    let scene: WebGLProgram;\n    let post: WebGLProgram;\n    try {\n      scene = link(SCENE);\n      post = link(POST);\n    } catch (e) {\n      console.error(e);\n      return;\n    }\n    const U = (p: WebGLProgram, n: string) => gl.getUniformLocation(p, n);\n    const su = Object.fromEntries([\"uRes\", \"uCam\", \"uTarget\", \"uFov\", \"uTime\", \"uChip\", \"uCol\", \"uOld\", \"uNew\", \"uR\", \"uDry\", \"uLab\", \"R0\", \"BP\"].map((n) => [n, U(scene, n)]));\n    const pu = Object.fromEntries([\"uSrc\", \"uRes\", \"uFocus\", \"uTime\"].map((n) => [n, U(post, n)]));\n\n    const quad = gl.createBuffer();\n    gl.bindBuffer(gl.ARRAY_BUFFER, quad);\n    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);\n    gl.enableVertexAttribArray(0);\n    gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n\n    // printed names\n    const labCanvas = document.createElement(\"canvas\");\n    labCanvas.width = 512;\n    labCanvas.height = 256 * CHIPS.length;\n    printLabels(labCanvas);\n    const labTex = gl.createTexture()!;\n    gl.bindTexture(gl.TEXTURE_2D, labTex);\n    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, labCanvas);\n    gl.generateMipmap(gl.TEXTURE_2D);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n\n    const COLS = new Float32Array(CHIPS.flatMap((c) => linear(c.hex)));\n    // chip 0 is on top of the stack\n    const layer = CHIPS.map((_, i) => CHIPS.length - 1 - i);\n    let chosen = 0;\n    const ang = CHIPS.map((_, i) => spring(i === 0 ? OUT : FAN[i], 70, 10.5));\n    const lift = CHIPS.map(() => spring(0, 120, 13));\n    const lie = CHIPS.map((_, i) => spring(i === 0 ? LIE_OUT : LIE_DECK, 60, 14));\n    const old = linear(CHIPS[0].hex);\n    const nxt = linear(CHIPS[0].hex);\n    let spread = 9;\n    let spreading = false;\n    let washAge = 99;\n    const knock = spring(0, 90, 10);\n    const cam = { ax: 0, ay: 0, tx: 0, ty: 0 };\n    let demo = !reduce;\n    let clock = 0;\n    let idleAt = HOLD;\n    let wake = Infinity;\n    let cues: { at: number; fn: () => void }[] = [];\n    let dirty = true;\n    const at = (delay: number, fn: () => void) => {\n      cues.push({ at: clock + delay, fn });\n      cues.sort((a, b) => a.at - b.at);\n    };\n    // the colour on the model right now, mixed by how far the wash has gone\n    const currentMix = () => {\n      const w = Math.min(1, Math.max(0, spread / 2.3));\n      return old.map((o, k) => o + (nxt[k] - o) * w);\n    };\n    const startWash = (j: number) => {\n      const now = currentMix();\n      old.splice(0, 3, ...now);\n      nxt.splice(0, 3, ...linear(CHIPS[j].hex));\n      spread = 0;\n      spreading = true;\n      washAge = 0;\n      knock.v += 1.4;\n      setPick(j);\n    };\n    const choose = (j: number) => {\n      if (j === chosen) return;\n      const prev = chosen;\n      chosen = j;\n      want.current = j;\n      if (reduce) {\n        ang.forEach((s, i) => (s.x = s.t = i === j ? OUT : FAN[i]));\n        lie.forEach((s, i) => (s.x = s.t = i === j ? LIE_OUT : LIE_DECK));\n        old.splice(0, 3, ...linear(CHIPS[j].hex));\n        nxt.splice(0, 3, ...linear(CHIPS[j].hex));\n        spread = 9;\n        washAge = 99;\n        setPick(j);\n        dirty = true;\n        return;\n      }\n      cues = [];\n      // the old chip goes home; the new one lifts, hangs back a beat, then swings out and lies down\n      ang[prev].t = FAN[prev];\n      lie[prev].t = LIE_DECK;\n      lift[prev].t = 0.02;\n      at(0.35, () => (lift[prev].t = 0));\n      // high enough at the tip to ride over every chip above it in the stack\n      lift[j].t = 0.05 + (CHIPS.length - 1 - layer[j]) * 0.03;\n      ang[j].t = FAN[j] + 0.06;\n      at(0.2, () => (ang[j].t = OUT));\n      at(0.62, () => {\n        lift[j].t = 0;\n        lie[j].t = LIE_OUT;\n      });\n      at(0.78, () => startWash(j));\n    };\n    const quiet = () => {\n      demo = false;\n      idleAt = Infinity;\n      wake = clock + 14;\n    };\n\n    // render target\n    const fbo = gl.createFramebuffer();\n    const fbTex = gl.createTexture()!;\n    gl.bindTexture(gl.TEXTURE_2D, fbTex);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n    let scale = Math.min(MAX_SCALE, Math.max(MIN_SCALE, window.devicePixelRatio || 1));\n    let cssW = 1;\n    let cssH = 1;\n    let phone = false;\n    let bw = 0;\n    let bh = 0;\n    const resize = () => {\n      const r = canvas.getBoundingClientRect();\n      cssW = Math.max(1, r.width);\n      cssH = Math.max(1, r.height);\n      phone = cssW < 768;\n      const s = Math.min(scale, Math.sqrt(PIXEL_BUDGET / (cssW * cssH)));\n      const w = Math.round(cssW * s);\n      const h = Math.round(cssH * s);\n      if (w !== bw || h !== bh) {\n        bw = w;\n        bh = h;\n        canvas.width = w;\n        canvas.height = h;\n        gl.bindTexture(gl.TEXTURE_2D, fbTex);\n        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, w, h, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);\n        gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);\n        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, fbTex, 0);\n      }\n      dirty = true;\n    };\n    resize();\n\n    const CH = new Float32Array(24);\n    let camState = { pos: [0, 0, 0], tgt: [0, 0, 0], fov: 0.5 };\n    let time = 0;\n    const draw = () => {\n      const wide = cssW / cssH;\n      const fov = phone ? 0.5 : 0.44;\n      const tgt = phone ? [0.02, 0.26, 0.32] : [-0.22, 0.4, 0.02];\n      const az = (phone ? -0.06 : -0.16) + cam.ax * 0.06 + Math.sin(time * 0.17) * 0.012;\n      const el = (phone ? 0.98 : 0.56) - cam.ay * 0.03 + Math.sin(time * 0.23 + 1) * 0.006;\n      const dist = (phone ? 5.0 / Math.max(0.42, wide) : 7.0 * Math.max(1, 1.6 / wide)) * (1 - 0.008 * knock.x);\n      const pos = [tgt[0] + dist * Math.sin(az) * Math.cos(el), tgt[1] + dist * Math.sin(el), tgt[2] + dist * Math.cos(az) * Math.cos(el)];\n      camState = { pos, tgt, fov };\n      // focus between the model and the chip that is out\n      const fpt = phone ? [0.0, 0.25, 0.2] : [-0.1, 0.25, -0.1];\n      const focus = Math.hypot(fpt[0] - pos[0], fpt[1] - pos[1], fpt[2] - pos[2]);\n      for (let i = 0; i < CHIPS.length; i++) {\n        CH[i * 4] = ang[i].x;\n        CH[i * 4 + 1] = Math.max(0, lift[i].x);\n        CH[i * 4 + 2] = Math.min(1, Math.max(0, lie[i].x));\n        CH[i * 4 + 3] = layer[i];\n      }\n      gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);\n      gl.viewport(0, 0, bw, bh);\n      gl.useProgram(scene);\n      gl.uniform2f(su.uRes, bw, bh);\n      gl.uniform3f(su.uCam, pos[0], pos[1], pos[2]);\n      gl.uniform3f(su.uTarget, tgt[0], tgt[1], tgt[2]);\n      gl.uniform1f(su.uFov, fov);\n      gl.uniform1f(su.uTime, reduce ? 0 : time);\n      gl.uniform4fv(su.uChip, CH);\n      gl.uniform3fv(su.uCol, COLS);\n      const place = phone ? PLACE.phone : PLACE.wide;\n      gl.uniform3f(su.R0, place.rivet[0], 0, place.rivet[1]);\n      gl.uniform3f(su.BP, place.model[0], 0, place.model[1]);\n      gl.uniform3f(su.uOld, old[0], old[1], old[2]);\n      gl.uniform3f(su.uNew, nxt[0], nxt[1], nxt[2]);\n      gl.uniform1f(su.uR, spread);\n      gl.uniform1f(su.uDry, Math.min(1, Math.max(0, (washAge - SPREAD) / 1.6)));\n      gl.activeTexture(gl.TEXTURE0);\n      gl.bindTexture(gl.TEXTURE_2D, labTex);\n      gl.uniform1i(su.uLab, 0);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n\n      gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n      gl.viewport(0, 0, bw, bh);\n      gl.useProgram(post);\n      gl.activeTexture(gl.TEXTURE0);\n      gl.bindTexture(gl.TEXTURE_2D, fbTex);\n      gl.generateMipmap(gl.TEXTURE_2D);\n      gl.uniform1i(pu.uSrc, 0);\n      gl.uniform2f(pu.uRes, bw, bh);\n      gl.uniform1f(pu.uFocus, focus);\n      gl.uniform1f(pu.uTime, reduce ? 0 : time);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    };\n\n    // which chip is under a point on screen: project the middle of each painted face and take the nearest\n    const project = (x: number, y: number, z: number) => {\n      const { pos, tgt, fov } = camState;\n      const fw = [tgt[0] - pos[0], tgt[1] - pos[1], tgt[2] - pos[2]];\n      const fl = Math.hypot(fw[0], fw[1], fw[2]);\n      fw[0] /= fl; fw[1] /= fl; fw[2] /= fl;\n      const rl = Math.hypot(fw[2], fw[0]);\n      const rg = [-fw[2] / rl, 0, fw[0] / rl];\n      const up = [rg[1] * fw[2] - rg[2] * fw[1], rg[2] * fw[0] - rg[0] * fw[2], rg[0] * fw[1] - rg[1] * fw[0]];\n      const v = [x - pos[0], y - pos[1], z - pos[2]];\n      const depth = v[0] * fw[0] + v[1] * fw[1] + v[2] * fw[2];\n      const k = cssH / (depth * 2 * Math.tan(fov / 2));\n      return [cssW / 2 + (v[0] * rg[0] + v[2] * rg[2]) * k, cssH / 2 - (v[0] * up[0] + v[1] * up[1] + v[2] * up[2]) * k];\n    };\n    const chipAt = (sx: number, sy: number) => {\n      let best = -1;\n      let bd = 1e9;\n      CHIPS.forEach((_, i) => {\n        for (const along of [0.45, 0.85, 1.25]) {\n          const a = ang[i].x;\n          const rv = (phone ? PLACE.phone : PLACE.wide).rivet;\n          const [px, py] = project(rv[0] + Math.cos(a) * along, 0.06, rv[1] - Math.sin(a) * along);\n          const d = Math.hypot(px - sx, py - sy) - layer[i] * 2;\n          if (d < bd) {\n            bd = d;\n            best = i;\n          }\n        }\n      });\n      return bd < 70 ? best : -1;\n    };\n\n    api.current = {\n      choose: (j) => {\n        quiet();\n        choose(j);\n        kick();\n      },\n      at: chipAt,\n    };\n\n    let raf = 0;\n    let last = 0;\n    let onScreen = true;\n    let slow = 0;\n    let first = true;\n    const frame = (now: number) => {\n      raf = 0;\n      const raw = last ? (now - last) / 1000 : 0.016;\n      last = now;\n      const dt = Math.min(raw, 0.1);\n      if (reduce) {\n        if (dirty) {\n          dirty = false;\n          draw();\n        }\n      } else {\n        clock += dt;\n        time += dt;\n        while (cues.length && cues[0].at <= clock) cues.shift()!.fn();\n        if (clock > wake) {\n          wake = Infinity;\n          demo = true;\n          idleAt = clock + 1.2;\n        }\n        if (demo && clock > idleAt) {\n          choose((chosen + 1) % CHIPS.length);\n          idleAt = clock + HOLD;\n        }\n        if (spreading) {\n          washAge += dt;\n          const u = Math.min(1, washAge / SPREAD);\n          spread = 2.6 * (1 - Math.pow(1 - u, 1.6));\n          if (u >= 1) spreading = false;\n        } else washAge += dt;\n        for (let n = 0; n < 4; n++) {\n          const h = dt / 4;\n          ang.forEach((s) => stepSpring(s, h));\n          lift.forEach((s) => stepSpring(s, h));\n          lie.forEach((s) => stepSpring(s, h));\n          stepSpring(knock, h);\n        }\n        const kc = 1 - Math.exp(-dt * 3);\n        cam.ax += (cam.tx - cam.ax) * kc;\n        cam.ay += (cam.ty - cam.ay) * kc;\n        draw();\n        if (raw > 0.03) slow++;\n        else slow = Math.max(0, slow - 1);\n        if (slow > 24 && scale > MIN_SCALE) {\n          scale = Math.max(MIN_SCALE, scale * 0.84);\n          slow = 0;\n          resize();\n        }\n      }\n      if (first) {\n        first = false;\n        setReady(true);\n      }\n      if (onScreen && !document.hidden && (!reduce || dirty)) raf = requestAnimationFrame(frame);\n    };\n    function kick() {\n      dirty = true;\n      if (!raf && onScreen && !document.hidden) {\n        last = 0;\n        raf = requestAnimationFrame(frame);\n      }\n    }\n    const io = new IntersectionObserver(([e]) => {\n      onScreen = e.isIntersecting;\n      kick();\n    });\n    io.observe(root);\n    const ro = new ResizeObserver(() => {\n      resize();\n      kick();\n    });\n    ro.observe(canvas);\n    const onVis = () => kick();\n    document.addEventListener(\"visibilitychange\", onVis);\n    const onMove = (e: PointerEvent) => {\n      if (reduce) return;\n      const r = root.getBoundingClientRect();\n      cam.tx = Math.max(-1, Math.min(1, ((e.clientX - r.left) / r.width) * 2 - 1));\n      cam.ty = Math.max(-1, Math.min(1, ((e.clientY - r.top) / r.height) * 2 - 1));\n      const r2 = canvas.getBoundingClientRect();\n      canvas.style.cursor = chipAt(e.clientX - r2.left, e.clientY - r2.top) >= 0 ? \"pointer\" : \"\";\n    };\n    const onLeave = () => {\n      cam.tx = 0;\n      cam.ty = 0;\n    };\n    root.addEventListener(\"pointermove\", onMove);\n    root.addEventListener(\"pointerleave\", onLeave);\n    const onLost = (e: Event) => {\n      e.preventDefault();\n      setReady(false);\n      cancelAnimationFrame(raf);\n      api.current = null;\n    };\n    canvas.addEventListener(\"webglcontextlost\", onLost);\n    kick();\n\n    return () => {\n      cancelAnimationFrame(raf);\n      io.disconnect();\n      ro.disconnect();\n      document.removeEventListener(\"visibilitychange\", onVis);\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      canvas.removeEventListener(\"webglcontextlost\", onLost);\n      api.current = null;\n      gl.deleteTexture(labTex);\n      gl.deleteTexture(fbTex);\n      gl.deleteFramebuffer(fbo);\n      gl.deleteBuffer(quad);\n      gl.deleteProgram(scene);\n      gl.deleteProgram(post);\n    };\n  }, [reduce]);\n\n  const choose = (i: number) => {\n    want.current = i;\n    if (api.current) api.current.choose(i);\n    else setPick(i);\n  };\n  const onCanvasClick = (e: React.MouseEvent<HTMLCanvasElement>) => {\n    const r = e.currentTarget.getBoundingClientRect();\n    const i = api.current?.at(e.clientX - r.left, e.clientY - r.top) ?? -1;\n    if (i >= 0) choose(i);\n  };\n  const onKeys = (e: React.KeyboardEvent<HTMLDivElement>) => {\n    const step = e.key === \"ArrowRight\" || e.key === \"ArrowDown\" ? 1 : e.key === \"ArrowLeft\" || e.key === \"ArrowUp\" ? -1 : 0;\n    if (!step) return;\n    e.preventDefault();\n    const next = (want.current + step + CHIPS.length) % CHIPS.length;\n    choose(next);\n    (e.currentTarget.querySelectorAll(\"button\")[next] as HTMLElement | undefined)?.focus();\n  };\n  const chip = CHIPS[pick];\n\n  return (\n    <section ref={rootRef} className=\"relative isolate h-[720px] w-full overflow-hidden md:h-[740px]\" style={{ backgroundColor: \"#E9E6E0\", color: INK, fontFamily: FONT }}>\n      {/* The still: a fan of chips and the model in the chosen colour. Shown before the renderer starts and wherever it cannot run. */}\n      <div aria-hidden=\"true\" className={`absolute inset-0 transition-opacity duration-700 ${ready ? \"pointer-events-none opacity-0\" : \"opacity-100\"}`} style={{ background: \"radial-gradient(120% 90% at 62% 40%, #F7F5F0 0%, #ECE9E3 55%, #DCD8D0 100%)\" }}>\n        <div className=\"absolute left-1/2 top-[44%] h-[300px] w-[340px] -translate-x-[8%] -translate-y-1/2 md:left-[58%] md:top-[46%] md:h-[340px] md:w-[460px] md:-translate-x-[10%]\">\n          <div className=\"absolute inset-0 rounded-[6px] bg-[#F3F1EC]\" style={{ transform: \"perspective(900px) rotateX(14deg)\", boxShadow: \"0 1px 0 #fff inset, 30px 46px 60px -26px rgba(60,52,40,0.45), 6px 10px 16px -6px rgba(60,52,40,0.3)\" }}>\n            <div className=\"absolute left-[6%] right-[6%] top-[5%] flex items-center gap-[4%]\">\n              <span className=\"h-3 w-3 rounded-full bg-white shadow\" />\n              <span className=\"ml-auto h-1.5 w-8 rounded-full bg-[#DAD6CE]\" />\n              <span className=\"h-1.5 w-8 rounded-full bg-[#DAD6CE]\" />\n              <span className=\"h-4 w-14 rounded-full transition-colors duration-700 motion-reduce:transition-none\" style={{ backgroundColor: chip.hex }} />\n            </div>\n            <div className=\"absolute left-[6%] right-[6%] top-[15%] h-[34%] rounded-[3px] transition-colors duration-700 motion-reduce:transition-none\" style={{ backgroundColor: chip.hex, boxShadow: \"0 3px 6px -2px rgba(0,0,0,0.25)\" }}>\n              <span className=\"absolute left-[6%] top-[26%] h-[16%] w-[46%] rounded-sm bg-white shadow\" />\n              <span className=\"absolute left-[6%] top-[52%] h-[16%] w-[34%] rounded-sm bg-white shadow\" />\n              <span className=\"absolute right-[4%] top-[10%] h-[80%] w-[30%] rounded-sm bg-[#C9CBCB] shadow-md\" />\n            </div>\n            <div className=\"absolute left-[6%] top-[55%] flex gap-2\">\n              <span className=\"h-5 w-20 rounded-full transition-colors duration-700 motion-reduce:transition-none\" style={{ backgroundColor: chip.hex }} />\n              <span className=\"h-5 w-16 rounded-full bg-white shadow\" />\n            </div>\n            <div className=\"absolute bottom-[8%] left-[6%] right-[6%] grid grid-cols-3 gap-[4%]\">\n              {[0, 1, 2].map((k) => (\n                <span key={k} className=\"h-14 overflow-hidden rounded-[3px] bg-white shadow\">\n                  <span className=\"block h-3 transition-colors duration-700 motion-reduce:transition-none\" style={{ backgroundColor: chip.hex }} />\n                </span>\n              ))}\n            </div>\n          </div>\n        </div>\n        <div className=\"absolute bottom-[26%] left-[8%] h-[40px] w-[40px] md:bottom-[18%] md:left-[16%]\">\n          {CHIPS.map((c, i) => (\n            <span\n              key={c.name}\n              className=\"absolute left-[20px] top-[20px] h-[46px] w-[200px] origin-left rounded-[8px] md:h-[54px] md:w-[250px]\"\n              style={{ backgroundColor: c.hex, transform: `translateY(-50%) rotate(${i === pick ? -14 : -98 + i * 9}deg)`, boxShadow: \"0 1px 0 rgba(255,255,255,0.35) inset, 6px 10px 14px -6px rgba(40,30,20,0.45)\", zIndex: CHIPS.length - i }}\n            />\n          ))}\n          <span className=\"absolute left-[8px] top-[8px] z-10 h-6 w-6 rounded-full\" style={{ background: \"radial-gradient(circle at 35% 30%, #FBE3A6, #C08A3E 55%, #7A5422)\", boxShadow: \"0 2px 4px rgba(0,0,0,0.35)\" }} />\n        </div>\n      </div>\n\n      <canvas ref={canvasRef} aria-hidden=\"true\" onClick={onCanvasClick} className={`absolute inset-0 h-full w-full touch-manipulation transition-opacity duration-700 ${ready ? \"opacity-100\" : \"opacity-0\"}`} />\n\n      <div className=\"pointer-events-none absolute left-5 right-5 top-5 md:left-10 md:right-auto md:top-9\">\n        <p className=\"flex items-center gap-2.5 text-[10px] font-semibold uppercase tracking-[0.22em] text-[#0B0D12]/60 md:text-[11px]\">\n          <span className=\"h-2 w-2 rounded-full\" style={{ backgroundColor: BLUE }} />\n          Colour <span className=\"text-[#0B0D12]/30\">/</span> one move\n        </p>\n        <div className=\"relative mt-1.5 h-[52px] overflow-hidden md:mt-2 md:h-[96px] md:w-[620px]\">\n          <AnimatePresence initial={false} mode=\"popLayout\">\n            <motion.h2\n              key={chip.name}\n              initial={reduce ? { opacity: 0 } : { y: \"75%\", opacity: 0 }}\n              animate={{ y: \"0%\", opacity: 1 }}\n              exit={reduce ? { opacity: 0 } : { y: \"-55%\", opacity: 0 }}\n              transition={reduce ? { duration: 0.01 } : { y: { type: \"spring\", stiffness: 200, damping: 22 }, opacity: { duration: 0.26 } }}\n              className=\"absolute left-0 top-0 whitespace-nowrap text-[46px] font-extrabold leading-[1] tracking-[-0.055em] md:text-[92px]\"\n            >\n              {chip.name}\n            </motion.h2>\n          </AnimatePresence>\n        </div>\n        <p className=\"mt-1 flex items-center gap-3 text-[13px] font-medium tracking-tight text-[#0B0D12]/65 md:mt-2 md:text-[16px]\">\n          <span style={{ fontFamily: MONO }} className=\"text-[12px] tracking-[0.08em] md:text-[13px]\">{chip.hex.toUpperCase()}</span>\n          <span>{chip.note}</span>\n        </p>\n      </div>\n\n      <div className=\"absolute inset-x-3 bottom-4 z-10 flex flex-col items-center gap-2 md:inset-x-auto md:bottom-9 md:right-10 md:items-end md:gap-3\">\n        <span className=\"text-[10px] font-semibold uppercase tracking-[0.22em] text-[#0B0D12]/55 md:text-[11px]\">Take a chip</span>\n        <div\n          role=\"radiogroup\"\n          aria-label=\"Paint colour for the homepage model\"\n          onKeyDown={onKeys}\n          className=\"grid grid-cols-3 gap-1 rounded-[22px] p-1 md:flex md:rounded-full\"\n          style={{ backgroundColor: \"rgba(250,249,246,0.78)\", boxShadow: \"0 0 0 1px rgba(11,13,18,0.08), 0 14px 30px -14px rgba(40,34,24,0.4)\", backdropFilter: \"blur(10px)\" }}\n        >\n          {CHIPS.map((c, i) => (\n            <button\n              key={c.name}\n              type=\"button\"\n              role=\"radio\"\n              aria-checked={i === pick}\n              tabIndex={i === pick ? 0 : -1}\n              onClick={() => choose(i)}\n              className=\"relative flex h-11 items-center gap-2 whitespace-nowrap rounded-full pl-2.5 pr-3.5 text-[13px] font-bold tracking-tight focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[#0B0D12] focus-visible:ring-offset-2 md:text-[14px]\"\n              style={{ color: INK }}\n            >\n              {i === pick && (\n                <motion.span layoutId=\"paint-chip-fan-pill\" className=\"absolute inset-0 rounded-full bg-white\" style={{ boxShadow: \"0 0 0 1px rgba(11,13,18,0.1), 0 6px 14px -6px rgba(40,34,24,0.45)\" }} transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 380, damping: 32 }} />\n              )}\n              <span className=\"relative h-4 w-4 shrink-0 rounded-full\" style={{ backgroundColor: c.hex, boxShadow: \"inset 0 0 0 1px rgba(0,0,0,0.12)\" }} />\n              <span className=\"relative\">{c.name}</span>\n            </button>\n          ))}\n        </div>\n      </div>\n      <p className=\"sr-only\" aria-live=\"polite\">\n        {chip.name} paint, {chip.hex}, now on the buttons, headline band and card tops of the homepage model. {chip.note}\n      </p>\n    </section>\n  );\n}\n"}]}
