{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ray-marched-stats-row-of-coloured-card-disc-stacks-on-window-lit-linen-that-deal-themselves-up-to-each-figure","type":"registry:block","title":"Ray-Marched Stats Row of Coloured Card Disc Stacks on Window-Lit Linen That Deal Themselves Up to Each Figure","description":"Free React stats row where three figures stand as piles of coloured card discs on linen, ray marched in WebGL, that drop in or lift away when the period changes.","dependencies":[],"files":[{"path":"components/we-inc/ray-marched-stats-row-of-coloured-card-disc-stacks-on-window-lit-linen-that-deal-themselves-up-to-each-figure.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// DEMO DATA. Three figures for one shop, three periods. Each stack counts in whole discs of `unit`.\nconst stats = [\n  { label: \"visitors\", unit: 500, colour: \"#a9573d\" },\n  { label: \"orders\", unit: 10, colour: \"#3e6079\" },\n  { label: \"repeat buyers\", unit: 5, colour: \"#c99a45\" },\n];\nconst periods = [\n  { key: \"Last 30 days\", values: [18500, 426, 175] },\n  { key: \"Previous 30 days\", values: [14000, 318, 140] },\n  { key: \"A year ago\", values: [9500, 241, 95] },\n];\nconst FONT = '\"Inter Tight\", \"Inter\", ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", sans-serif';\nconst SERIF = '\"Iowan Old Style\", \"Palatino Linotype\", Palatino, \"Book Antiqua\", Georgia, serif';\nconst S = stats.length;\nconst FLY = 14; // discs in the air per stack at most\n// Scene units: card thickness, disc radius, gap between stacks.\nconst T = 0.052, R = 0.6, PITCH = 1.78;\nconst xOf = (i: number) => (i - (S - 1) / 2) * PITCH;\nconst discs = (k: number, i: number) => Math.round(periods[k].values[i] / stats[i].unit);\nconst fmt = (n: number) => n.toLocaleString(\"en-GB\");\nconst lin = (hex: string) => [1, 3, 5].map((o) => Math.pow(parseInt(hex.slice(o, o + 2), 16) / 255, 2.2));\n\nconst VERT = `#version 300 es\nin vec2 aPos; void main(){ gl_Position = vec4(aPos, 0.0, 1.0); }`;\n\nconst TONE = `\nvec3 film(vec3 x){ return clamp(x * (2.51 * x + 0.03) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0); }\nvec3 tone(vec3 x){ x *= 0.9; float l = max(x.r, max(x.g, x.b)) + 1e-4; vec3 keep = x / l * film(vec3(l)).r; return pow(mix(film(x), keep, 0.6), vec3(1.0 / 2.2)); }\n`;\n\n// Each stack is a pile of thin card discs, each one a rounded cylinder nudged a little off true.\n// Resting discs are found from the height of the point, discs in the air are listed one by one.\nconst SCENE = `#version 300 es\nprecision highp float;\nout vec4 outC;\nconst int S = ${S}; const int F = ${FLY};\nuniform vec2 uRes; uniform vec4 uJit; uniform float uSeed; uniform float uDirect;\nuniform vec3 uCam; uniform vec3 uFw; uniform float uFoc; uniform vec2 uShift; uniform float uFocus;\nuniform float uRest[S]; uniform float uTop[S]; uniform float uN[S]; uniform vec3 uFly[S * F]; uniform vec3 uCol[S];\nuniform vec3 uSun;\nconst float T = ${T.toFixed(4)}; const float R = ${R.toFixed(3)}; const float P = ${PITCH.toFixed(3)};\nconst float HH = T * 0.47; const float RR = T * 0.28;\nconst vec3 SUNC = vec3(1.0, 0.86, 0.68);\nconst vec3 SKY = vec3(0.6, 0.66, 0.78);\nconst vec3 HAZE = vec3(0.86, 0.82, 0.76);\nconst vec3 LINEN = vec3(0.79, 0.74, 0.66);\n${TONE}\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); vec2 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 sx(int i){ return (float(i) - float(S - 1) * 0.5) * P; }\n// hand-stacked: every disc sits a hair off centre\nvec2 jit(float k, int i){ float a = h21(vec2(k * 1.37, float(i) * 9.1 + 2.0)) * 6.2831; float r = 0.003 + 0.011 * h21(vec2(k * 3.1, float(i) + 7.0)); return vec2(cos(a), sin(a)) * r; }\nfloat disc(vec3 q){ vec2 d = vec2(length(q.xz) - R + RR, abs(q.y) - HH + RR); return min(max(d.x, d.y), 0.0) + length(max(d, 0.0)) - RR; }\n\nfloat gId; int gSt;\nfloat stack(int i, vec3 p, float best){\n  vec3 q = p - vec3(sx(i), 0.0, 0.0);\n  float bound = max(length(q.xz) - R - 0.05, q.y - uTop[i]);\n  if (bound > best || uTop[i] < 0.001) return best;\n  float rest = uRest[i];\n  if (rest > 0.5) {\n    float k0 = clamp(floor(q.y / T), 0.0, rest - 1.0);\n    for (int j = -1; j <= 1; j++) {\n      float k = clamp(k0 + float(j), 0.0, rest - 1.0);\n      vec2 o = jit(k, i);\n      float d = disc(q - vec3(o.x, k * T + T * 0.5, o.y));\n      if (d < best) { best = d; gId = k; gSt = i; }\n    }\n  }\n  int n = int(uN[i]);\n  for (int j = 0; j < F; j++) {\n    if (j >= n) break;\n    vec3 f = uFly[i * F + j]; vec2 o = jit(f.y, i);\n    vec3 qq = q - vec3(o.x, f.x + T * 0.5, o.y);\n    float c = cos(f.z), s = sin(f.z); qq.xy = mat2(c, -s, s, c) * qq.xy;\n    float d = disc(qq);\n    if (d < best) { best = d; gId = f.y; gSt = i; }\n  }\n  return best;\n}\nfloat map(vec3 p){ float d = 1e9; for (int i = 0; i < S; i++) d = stack(i, p, d); return d; }\nvec3 nrm(vec3 p){ vec2 e = vec2(0.0006, -0.0006);\n  return normalize(e.xyy * map(p + e.xyy) + e.yyx * map(p + e.yyx) + e.yxy * map(p + e.yxy) + e.xxx * map(p + e.xxx)); }\n\n// The window: light comes through four tall panes, so the mullions lay soft bars across the cloth.\nfloat window(vec3 p){\n  vec3 U = normalize(cross(uSun, vec3(0.0, 1.0, 0.0))); vec3 V = cross(U, uSun);\n  vec2 w = vec2(dot(p, U), dot(p, V)) + vec2(1.1, 0.4);\n  float soft = 0.06 + 0.03 * p.y;\n  vec2 c = abs(fract(w / vec2(2.6, 3.4)) - 0.5) * vec2(2.6, 3.4);\n  float bars = mix(1.0, smoothstep(0.03, 0.03 + soft, c.x), 0.85) * mix(1.0, smoothstep(0.025, 0.025 + soft, c.y), 0.7);\n  float frame = smoothstep(5.6, 4.6, abs(w.x - 0.3)) * smoothstep(4.4, 3.4, abs(w.y));\n  return bars * frame;\n}\nfloat shadow(vec3 ro, vec3 rd){\n  float r = 1.0; float t = 0.012;\n  for (int k = 0; k < 40; k++) {\n    float h = map(ro + rd * t);\n    r = min(r, 9.0 * h / t);\n    t += clamp(h, 0.01, 0.25);\n    if (r < 0.002 || t > 5.0) break;\n  }\n  return clamp(r, 0.0, 1.0);\n}\nfloat occl(vec3 p, vec3 n){\n  float o = 0.0; float w = 1.0;\n  for (int k = 1; k <= 5; k++) { float h = 0.018 * float(k * k); o += (h - map(p + n * h)) * w; w *= 0.62; }\n  return clamp(1.0 - 2.2 * o, 0.0, 1.0);\n}\n// linen: a plain weave, slubbed threads, and a long soft fold in the cloth\nvec3 linenN(vec2 g, out float alb){\n  vec2 th = g / 0.034;\n  float wx = 0.5 + 0.5 * sin(th.x * 3.1416 * 2.0); float wz = 0.5 + 0.5 * sin(th.y * 3.1416 * 2.0);\n  float over = step(0.5, fract(floor(th.x) * 0.5 + floor(th.y) * 0.5));\n  float slub = n2(vec2(g.x * 2.2, floor(th.y) * 1.7)) * 0.6 + n2(vec2(floor(th.x) * 1.3, g.y * 2.2)) * 0.4;\n  slub = slub * slub * 1.6;\n  alb = 0.88 + 0.16 * mix(wx, wz, over) + 0.16 * (slub - 0.5) + 0.06 * (n2(g * 2.0) - 0.5);\n  float fold = sin(g.x * 0.9 + g.y * 0.35 + 1.2) * 0.5 + sin(g.x * 2.1 - g.y * 1.3) * 0.18;\n  vec2 grad = vec2(cos(g.x * 0.9 + g.y * 0.35 + 1.2) * 0.9 * 0.5 + cos(g.x * 2.1 - g.y * 1.3) * 2.1 * 0.18, cos(g.x * 0.9 + g.y * 0.35 + 1.2) * 0.35 * 0.5 - cos(g.x * 2.1 - g.y * 1.3) * 1.3 * 0.18) * 0.05;\n  alb *= 1.0 + fold * 0.04;\n  grad += vec2(cos(th.x * 6.2832) * mix(0.0, 1.0, over), cos(th.y * 6.2832) * mix(1.0, 0.0, over)) * 0.09;\n  return normalize(vec3(-grad.x, 1.0, -grad.y));\n}\nvec3 ground(vec3 gp, vec3 rd, float dist){\n  float a; vec3 n = linenN(gp.xz, a);\n  float sh = shadow(gp + vec3(0.0, 0.003, 0.0), uSun);\n  float con = 1.0;\n  for (int i = 0; i < S; i++) {\n    float d = max(length(gp.xz - vec2(sx(i), 0.0)) - R, 0.0);\n    con *= 1.0 - 0.62 * exp(-d / 0.035) - 0.22 * exp(-d / 0.4) * min(uTop[i], 1.0);\n  }\n  float pool = 0.6 + 0.4 * smoothstep(9.0, 1.0, length(gp.xz - vec2(-0.6, 0.4)));\n  vec3 c = LINEN * a * (SUNC * 2.9 * max(dot(n, uSun), 0.0) * sh * window(gp) * pool + SKY * 0.4 * con);\n  // card colour bounced onto the cloth right by each stack\n  for (int i = 0; i < S; i++) {\n    float d = max(length(gp.xz - vec2(sx(i), 0.0)) - R, 0.0);\n    c += LINEN * uCol[i] * 0.35 * exp(-d / 0.18) * min(uTop[i] * 2.0, 1.0);\n  }\n  return mix(c, HAZE, smoothstep(5.0, 20.0, dist));\n}\nvec3 shadeDisc(vec3 p, vec3 rd){\n  float id = gId; int st = gSt;\n  vec3 n = nrm(p);\n  vec3 q = p - vec3(sx(st), 0.0, 0.0);\n  float face = smoothstep(0.6, 0.9, abs(n.y));\n  // through-dyed card: each sheet from a slightly different batch\n  float b = h21(vec2(id * 0.73, float(st) * 4.1));\n  vec3 base = mix(vec3(dot(uCol[st], vec3(0.3, 0.55, 0.15))), uCol[st], 0.82);\n  vec3 alb = base * (0.86 + 0.2 * b) * mix(vec3(1.0), vec3(1.04, 0.98, 0.95), step(0.7, b));\n  float fib = n2(q.xz * 140.0 + id * 13.0) * 0.6 + n2(q.xz * 40.0 + id) * 0.4;\n  float edgeFib = n2(vec2(atan(q.z, q.x) * 160.0, q.y * 900.0));\n  alb *= mix(0.92 + 0.12 * edgeFib, 0.94 + 0.1 * fib, face);\n  // the cut edge of card is a touch paler and rougher than its face\n  alb = mix(alb * 1.08 + 0.015, alb, face);\n  float sh = shadow(p + n * 0.004, uSun);\n  float ao = occl(p, n);\n  float wrap = max((dot(n, uSun) + 0.2) / 1.2, 0.0);\n  vec3 c = alb * SUNC * 2.1 * wrap * sh * window(p);\n  c += alb * SKY * (0.42 + 0.18 * n.y) * ao;\n  // warm light coming back up off the linen\n  c += alb * LINEN * 0.42 * max(-n.y * 0.6 + 0.4, 0.0) * ao * smoothstep(1.4, 0.0, p.y);\n  // paper has only a faint sheen, mostly at grazing angles\n  vec3 h = normalize(uSun - rd);\n  float sp = pow(max(dot(n, h), 0.0), 26.0) * (0.04 + 0.5 * pow(1.0 - max(dot(n, -rd), 0.0), 5.0));\n  c += SUNC * 2.0 * sp * sh * window(p) * face;\n  return c;\n}\nvec3 env(vec3 rd){ return mix(HAZE, HAZE * vec3(0.9, 0.9, 0.93), smoothstep(0.0, 0.5, rd.y)); }\n\nvoid main(){\n  vec2 fc = gl_FragCoord.xy + uJit.xy;\n  vec2 uv = (fc - 0.5 * uRes) / uRes.y - uShift;\n  vec3 fw = uFw; vec3 rt = normalize(cross(vec3(0.0, 1.0, 0.0), fw)); vec3 up = cross(fw, rt);\n  vec3 rd = normalize(fw * uFoc + rt * uv.x + up * uv.y);\n  vec3 ro = uCam;\n  vec3 fp = ro + rd * (uFocus / dot(rd, fw));\n  ro += rt * uJit.z + up * uJit.w; rd = normalize(fp - ro);\n  float tg = rd.y < 0.0 ? -ro.y / rd.y : 1e9;\n  // only march inside the box that holds the stacks\n  vec3 lo = vec3(-P - R - 0.2, 0.0, -R - 0.2); vec3 hi = vec3(P + R + 0.2, 6.0, R + 0.2);\n  vec3 inv = 1.0 / rd; vec3 a = (lo - ro) * inv; vec3 b = (hi - ro) * inv;\n  vec3 mn = min(a, b); vec3 mx = max(a, b);\n  float t0 = max(max(mn.x, mn.y), max(mn.z, 0.0)); float t1 = min(min(mx.x, mx.y), mx.z);\n  vec3 col; bool hit = false;\n  if (t1 > t0) {\n    float t = t0;\n    for (int k = 0; k < 140; k++) {\n      vec3 p = ro + rd * t;\n      float d = map(p);\n      if (d < 0.0004 * t) { hit = true; break; }\n      t += d * 0.9;\n      if (t > t1 || t > tg) break;\n    }\n    if (hit && t < tg) col = shadeDisc(ro + rd * t, rd); else hit = false;\n  }\n  if (!hit) col = tg < 1e8 ? ground(ro + rd * tg, rd, tg) : env(rd);\n  col = min(col, vec3(20.0));\n  if (uDirect > 0.5) col = tone(col);\n  outC = vec4(col, 1.0);\n}\n`;\n\nconst POST = `#version 300 es\nprecision highp float;\nout vec4 outC;\nuniform sampler2D uAcc; uniform vec2 uRes; uniform float uGrain; uniform vec2 uCentre;\n${TONE}\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); }\nvoid main(){\n  vec2 uv = gl_FragCoord.xy / uRes;\n  vec3 c = texture(uAcc, uv).rgb;\n  vec2 px = 1.0 / uRes;\n  vec3 nb = texture(uAcc, uv + vec2(px.x, 0.0)).rgb + texture(uAcc, uv - vec2(px.x, 0.0)).rgb + texture(uAcc, uv + vec2(0.0, px.y)).rgb + texture(uAcc, uv - vec2(0.0, px.y)).rgb;\n  c = max(c + (c - nb * 0.25) * 0.35, 0.0);\n  vec2 q = (uv - uCentre) * vec2(uRes.x / uRes.y, 1.0);\n  c *= 1.0 - 0.2 * smoothstep(0.5, 1.4, length(q));\n  c = tone(c);\n  c = mix(c, c * c * (3.0 - 2.0 * c), 0.18);\n  c += (h21(gl_FragCoord.xy + uGrain * 91.7) - 0.5) * 0.02;\n  outC = vec4(c, 1.0);\n}\n`;\n\n// No WebGL: the same three stacks from the front, in striped gradients.\nfunction Still({ counts }: { counts: number[] }) {\n  return (\n    <div aria-hidden=\"true\" className=\"absolute inset-x-0 top-[16%] mx-auto flex h-[40%] w-[86%] max-w-[620px] items-end justify-between\">\n      {counts.map((c, i) => (\n        <div key={stats[i].label} className=\"relative w-[27%]\" style={{ height: `${(c / 44) * 100}%` }}>\n          <div className=\"absolute -bottom-[6%] left-[30%] h-[12%] w-[120%] rounded-[50%] bg-[#6b5a45]/25 blur-md\" />\n          <div className=\"absolute inset-x-0 bottom-0 top-[6%] rounded-b-[50%/14%]\" style={{ background: `repeating-linear-gradient(180deg, rgba(0,0,0,0.16) 0 1px, transparent 1px 4px), linear-gradient(90deg, rgba(0,0,0,0.05), rgba(255,255,255,0.18) 30%, rgba(0,0,0,0.22) 100%), ${stats[i].colour}` }} />\n          <div className=\"absolute inset-x-0 top-0 h-[12%] rounded-[50%]\" style={{ background: `linear-gradient(160deg, rgba(255,255,255,0.28), rgba(0,0,0,0.04)), ${stats[i].colour}` }} />\n        </div>\n      ))}\n    </div>\n  );\n}\n\ntype Disc = { y: number; v: number; k: number; tilt: number; out: boolean };\n\nexport default function CardDiscStackStatsRow() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const tags = useRef<(HTMLDivElement | null)[]>([]);\n  const [period, setPeriod] = useState(0);\n  const [shown, setShown] = useState<\"wait\" | \"gl\" | \"still\">(\"wait\");\n  const [nums, setNums] = useState<number[]>(periods[0].values);\n  const api = useRef<{ go: (k: number, user: boolean) => void; hop: (i: number) => void } | null>(null);\n\n  useEffect(() => {\n    const box = stage.current, cv = canvas.current;\n    if (!box || !cv) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const gl = cv.getContext(\"webgl2\", { antialias: false, alpha: false, powerPreference: \"high-performance\" }) as WebGL2RenderingContext | null;\n    if (!gl) { setShown(\"still\"); return; }\n    const canAcc = !!gl.getExtension(\"EXT_color_buffer_float\") || !!gl.getExtension(\"EXT_color_buffer_half_float\");\n    const info = gl.getExtension(\"WEBGL_debug_renderer_info\");\n    const software = /swiftshader|llvmpipe|software|basic render/i.test(info ? String(gl.getParameter(info.UNMASKED_RENDERER_WEBGL)) : \"\");\n    const calm = reduce || software;\n\n    const compile = (type: number, src: string) => {\n      const sh = gl.createShader(type);\n      if (!sh) return null;\n      gl.shaderSource(sh, src); gl.compileShader(sh);\n      if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) { console.warn(gl.getShaderInfoLog(sh)); return null; }\n      return sh;\n    };\n    const link = (frag: string) => {\n      const v = compile(gl.VERTEX_SHADER, VERT), f = compile(gl.FRAGMENT_SHADER, frag);\n      const p = gl.createProgram();\n      if (!v || !f || !p) return null;\n      gl.attachShader(p, v); gl.attachShader(p, f); gl.bindAttribLocation(p, 0, \"aPos\"); gl.linkProgram(p);\n      return gl.getProgramParameter(p, gl.LINK_STATUS) ? p : null;\n    };\n    const scene = link(SCENE), post = canAcc ? link(POST) : null;\n    if (!scene || (canAcc && !post)) { setShown(\"still\"); return; }\n    const su = Object.fromEntries([\"uRes\", \"uJit\", \"uSeed\", \"uDirect\", \"uCam\", \"uFw\", \"uFoc\", \"uShift\", \"uFocus\", \"uRest\", \"uTop\", \"uN\", \"uFly\", \"uCol\", \"uSun\"].map((n) => [n, gl.getUniformLocation(scene, n)]));\n    const pu = post ? Object.fromEntries([\"uAcc\", \"uRes\", \"uGrain\", \"uCentre\"].map((n) => [n, gl.getUniformLocation(post, n)])) : {};\n    const cols = new Float32Array(stats.flatMap((s) => lin(s.colour)));\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); gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n\n    const acc = canAcc ? gl.createTexture() : null;\n    const fbo = canAcc ? gl.createFramebuffer() : null;\n    let W = 0, H = 0, scale = 1, cssW = 1, cssH = 1;\n\n    // the piles: discs at rest, discs falling in (bottom first) and discs being dealt away\n    const st = stats.map((_, i) => ({ rest: discs(0, i), fall: [] as Disc[], away: [] as Disc[], target: discs(0, i), spawn: 0, from: periods[0].values[i], to: periods[0].values[i], c0: discs(0, i) }));\n    const s = { k: 0, px: 0, py: 0, sx: 0, sy: 0, count: 0, fresh: true, frame: 0, slow: 0, visible: true, raf: 0, last: 0, t0: 0, touched: false, demoAt: 0, first: true, sent: \"\", pos: \"\", xs: [] as number[][] };\n\n    const size = () => {\n      const r = box.getBoundingClientRect();\n      cssW = Math.max(1, r.width); cssH = Math.max(1, r.height);\n      const dpr = Math.min(software ? 1 : 1.5, window.devicePixelRatio || 1) * scale;\n      const k = Math.min(1, Math.sqrt(1.1e6 / (cssW * cssH * dpr * dpr)));\n      const w = Math.max(2, Math.round(cssW * dpr * k)), h = Math.max(2, Math.round(cssH * dpr * k));\n      if (w === W && h === H) return;\n      W = w; H = h; cv.width = W; cv.height = H;\n      if (acc && fbo) {\n        gl.activeTexture(gl.TEXTURE1); gl.bindTexture(gl.TEXTURE_2D, acc);\n        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA16F, W, H, 0, gl.RGBA, gl.HALF_FLOAT, null);\n        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.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        gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);\n        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, acc, 0);\n        gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n      }\n      s.count = 0; s.fresh = true;\n    };\n    const halton = (i: number, b: number) => { let f = 1, r = 0; while (i > 0) { f /= b; r += f * (i % b); i = Math.floor(i / b); } return r; };\n\n    // A level camera a little above the cloth, the lens shifted so the foot of the stacks sits on a set line.\n    const camera = () => {\n      const aspect = cssW / cssH, wide = cssW >= 760;\n      const el = 0.36 - s.sy * 0.015, az = -0.16 + s.sx * 0.05, dist = 10.5;\n      const cam = [dist * Math.sin(az) * Math.cos(el), 0.9 + dist * Math.sin(el), -dist * Math.cos(az) * Math.cos(el)];\n      const fl = Math.hypot(cam[0], cam[2]);\n      const fw = [-cam[0] / fl, 0, -cam[2] / fl];\n      const rt = [fw[2], 0, -fw[0]];\n      const foc = Math.min(wide ? 1.95 : 1.9, ((wide ? 0.86 : 0.74) * aspect * dist) / (2 * PITCH + 2 * R));\n      const proj = (p: number[], sh: number[]) => {\n        const d = [p[0] - cam[0], p[1] - cam[1], p[2] - cam[2]];\n        const z = d[0] * fw[0] + d[2] * fw[2];\n        return [(foc * (d[0] * rt[0] + d[2] * rt[2])) / z + sh[0], (foc * d[1]) / z + sh[1]];\n      };\n      const raw = proj([0, 0, 0], [0, 0]);\n      const shift = [-raw[0], (wide ? -0.17 : -0.17) - raw[1]];\n      return { cam, fw, foc, shift, proj };\n    };\n\n    const place = () => {\n      const c = camera();\n      const xs = stats.map((_, i) => { const u = c.proj([xOf(i), 0, -R], c.shift); return [cssW / 2 + u[0] * cssH, cssH / 2 - u[1] * cssH]; });\n      const line = Math.max(...xs.map((p) => p[1]));\n      xs.forEach((p) => { p[1] = line; });\n      const key = xs.map((p) => p.map(Math.round).join(\":\")).join(\",\");\n      if (key === s.pos) return;\n      s.pos = key; s.xs = xs;\n      tags.current.forEach((el, i) => { if (el) { el.style.left = `${xs[i][0]}px`; el.style.top = `${xs[i][1] + 22}px`; } });\n    };\n\n    const draw = (alpha: number, sample: number) => {\n      const c = camera();\n      const jx = sample ? halton(sample, 2) - 0.5 : 0, jy = sample ? halton(sample, 3) - 0.5 : 0;\n      const la = halton(sample, 5) * Math.PI * 2, lr = sample ? Math.sqrt(halton(sample, 7)) * 0.05 : 0;\n      gl.useProgram(scene);\n      gl.bindFramebuffer(gl.FRAMEBUFFER, canAcc ? fbo : null);\n      gl.viewport(0, 0, W, H);\n      if (canAcc && alpha < 1) { gl.enable(gl.BLEND); gl.blendColor(0, 0, 0, alpha); gl.blendFunc(gl.CONSTANT_ALPHA, gl.ONE_MINUS_CONSTANT_ALPHA); } else gl.disable(gl.BLEND);\n      gl.uniform2f(su.uRes, W, H);\n      gl.uniform4f(su.uJit, canAcc ? jx : 0, canAcc ? jy : 0, canAcc ? Math.cos(la) * lr : 0, canAcc ? Math.sin(la) * lr : 0);\n      gl.uniform1f(su.uSeed, canAcc && sample ? (sample * 0.61803) % 1 : 0);\n      gl.uniform1f(su.uDirect, canAcc ? 0 : 1);\n      gl.uniform3f(su.uCam, c.cam[0], c.cam[1], c.cam[2]);\n      gl.uniform3f(su.uFw, c.fw[0], c.fw[1], c.fw[2]);\n      gl.uniform1f(su.uFoc, c.foc);\n      gl.uniform2f(su.uShift, c.shift[0], c.shift[1]);\n      gl.uniform1f(su.uFocus, -c.cam[0] * c.fw[0] - c.cam[2] * c.fw[2]);\n      const fly = new Float32Array(S * FLY * 3), n = new Float32Array(S), top = new Float32Array(S), rest = new Float32Array(S);\n      st.forEach((p, i) => {\n        rest[i] = p.rest;\n        let t = p.rest * T, j = 0;\n        for (const d of [...p.fall, ...p.away]) {\n          if (j >= FLY) break;\n          fly.set([d.y, d.k, d.tilt], (i * FLY + j) * 3); j++;\n          t = Math.max(t, d.y + T + Math.abs(d.tilt) * R);\n        }\n        n[i] = j; top[i] = t > 0 ? t + 0.02 : 0;\n      });\n      gl.uniform1fv(su.uRest, rest); gl.uniform1fv(su.uTop, top); gl.uniform1fv(su.uN, n);\n      gl.uniform3fv(su.uFly, fly); gl.uniform3fv(su.uCol, cols);\n      // late afternoon window, low on the left and a little behind the stacks\n      const sa = -1.0 + s.sx * 0.03, se = 0.42;\n      gl.uniform3f(su.uSun, Math.sin(sa) * Math.cos(se), Math.sin(se), Math.cos(sa) * Math.cos(se) * 0.55);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n      if (canAcc && post) {\n        gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n        gl.disable(gl.BLEND);\n        gl.useProgram(post);\n        gl.uniform1i(pu.uAcc, 1); gl.uniform2f(pu.uRes, W, H);\n        gl.uniform1f(pu.uGrain, calm ? 1 : s.frame % 64);\n        gl.uniform2f(pu.uCentre, 0.5, 0.5);\n        gl.drawArrays(gl.TRIANGLES, 0, 3);\n      }\n    };\n\n    const go = (k: number, user: boolean) => {\n      if (user) s.touched = true;\n      if (k === s.k) return;\n      s.k = k; setPeriod(k);\n      const now = performance.now();\n      st.forEach((p, i) => {\n        const shownNow = Math.round(p.from + (p.to - p.from) * prog(p));\n        p.from = shownNow; p.to = periods[k].values[i];\n        p.c0 = p.rest + p.fall.length; p.target = discs(k, i);\n        // the stacks start one after another, left to right\n        p.spawn = now + i * 140;\n        if (reduce) { p.rest = p.target; p.fall = []; p.away = []; p.from = p.to; }\n      });\n      s.count = 0; wake();\n    };\n    // tapping a stack throws its top cards up a little; they fall back in order\n    const hop = (i: number) => {\n      s.touched = true;\n      const p = st[i];\n      if (reduce || p.fall.length || p.rest < 6) return;\n      const m = 7;\n      p.rest -= m;\n      p.fall = Array.from({ length: m }, (_, j) => ({ y: (p.rest + j) * T, v: 1.1 + j * 0.42, k: p.rest + j, tilt: 0, out: false }));\n      s.count = 0; wake();\n    };\n    api.current = { go, hop };\n    const prog = (p: (typeof st)[number]) => {\n      const c = p.rest;\n      if (p.target === p.c0) return p.rest === p.target ? 1 : 0.5;\n      return Math.max(0, Math.min(1, (c - p.c0) / (p.target - p.c0)));\n    };\n\n    const step = (dt: number, now: number) => {\n      let moving = false;\n      st.forEach((p, i) => {\n        const have = p.rest + p.fall.length;\n        if (now >= p.spawn) {\n          if (have < p.target && p.fall.length < FLY - 1 && p.away.length === 0) {\n            const k = have;\n            const lastY = p.fall.length ? p.fall[p.fall.length - 1].y : p.rest * T;\n            p.fall.push({ y: Math.max(k * T + 1.25, lastY + 0.2), v: -0.6, k, tilt: (Math.random() - 0.5) * 0.24, out: false });\n            p.spawn = now + 34;\n          } else if (have > p.target && p.fall.length === 0 && p.away.length < FLY - 1) {\n            p.rest--;\n            p.away.push({ y: p.rest * T, v: 2.6 + Math.random() * 0.6, k: p.rest, tilt: 0, out: true });\n            p.spawn = now + 42;\n          }\n        }\n        // falling cards: gravity, then each lands on the card below it with a small bounce\n        for (let j = 0; j < p.fall.length; j++) {\n          const d = p.fall[j];\n          d.v -= 19 * dt; d.y += d.v * dt;\n          d.tilt *= Math.exp(-dt * (d.y - d.k * T < 0.25 ? 18 : 2));\n          const floor = j === 0 ? p.rest * T : p.fall[j - 1].y + T;\n          if (d.y < floor) {\n            const below = j === 0 ? 0 : p.fall[j - 1].v;\n            d.y = floor; d.tilt *= 0.3;\n            d.v = d.v - below < -0.55 ? below - (d.v - below) * 0.2 : below;\n          }\n        }\n        while (p.fall.length && Math.abs(p.fall[0].y - p.rest * T) < 0.002 && Math.abs(p.fall[0].v) < 0.05) { p.fall.shift(); p.rest++; }\n        // cards dealt away lift off, tip and leave the top of the frame\n        for (const d of p.away) { d.v += 24 * dt; d.y += d.v * dt; d.tilt += dt * (d.k % 2 ? 0.5 : -0.5); }\n        p.away = p.away.filter((d) => d.y < 5.5);\n        if (p.fall.length || p.away.length || p.rest !== p.target) moving = true;\n        if (!moving && p.rest === p.target) p.from = p.to;\n        void i;\n      });\n      return moving;\n    };\n\n    const frame = (now: number) => {\n      s.raf = 0;\n      if (!s.visible) return;\n      const dtRaw = s.last ? (now - s.last) / 1000 : 0.016;\n      const dt = Math.min(0.033, dtRaw);\n      s.last = now; s.frame++;\n      if (!s.t0) { s.t0 = now; s.demoAt = now + 4200; }\n      // opens finished, holds, then walks through the periods until someone takes over\n      if (!calm && !s.touched && now > s.demoAt) { go((s.k + 1) % periods.length, false); s.demoAt = now + 4800; }\n\n      let moving = false;\n      for (let k = 0; k < 2; k++) moving = step(dt / 2, now) || moving;\n      const vals = st.map((p) => (p.rest === p.target && !p.fall.length && !p.away.length ? p.to : Math.round(p.from + (p.to - p.from) * prog(p))));\n      const key = vals.join(\",\");\n      if (key !== s.sent) { s.sent = key; setNums(vals); }\n\n      let drift = false;\n      if (!calm) {\n        const ox = s.sx, oy = s.sy;\n        s.sx += (s.px - s.sx) * (1 - Math.exp(-dt * 3)); s.sy += (s.py - s.sy) * (1 - Math.exp(-dt * 3));\n        drift = Math.abs(s.sx - ox) + Math.abs(s.sy - oy) > 0.0004;\n      }\n      place();\n      if (s.fresh || !canAcc) { draw(1, 0); s.fresh = false; s.count = 1; }\n      else if (moving) { draw(0.92, 1 + (s.frame % 64)); s.count = 2; }\n      else if (drift) { draw(0.5, 1 + (s.frame % 64)); s.count = 3; }\n      else if (s.count < 40) { s.count++; draw(1 / s.count, s.count); }\n      if (s.first) { s.first = false; setShown(\"gl\"); }\n\n      if (!calm && dtRaw > 0.026) s.slow++; else s.slow = Math.max(0, s.slow - 2);\n      if (s.slow > 40 && scale > 0.5) { scale *= 0.82; s.slow = 0; size(); }\n\n      if (moving || drift || s.count < 40 || (!calm && !s.touched)) s.raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!s.raf && s.visible) { s.last = 0; s.raf = requestAnimationFrame(frame); } };\n\n    size();\n    const ro = new ResizeObserver(() => { size(); s.pos = \"\"; wake(); });\n    ro.observe(box);\n    const io = new IntersectionObserver(([e]) => { s.visible = e.isIntersecting && !document.hidden; if (s.visible) wake(); }, { threshold: 0.05 });\n    io.observe(box);\n    const vis = () => { s.visible = !document.hidden; if (s.visible) wake(); };\n    document.addEventListener(\"visibilitychange\", vis);\n    const move = (e: PointerEvent) => {\n      const r = box.getBoundingClientRect();\n      s.px = ((e.clientX - r.left) / r.width) * 2 - 1; s.py = ((e.clientY - r.top) / r.height) * 2 - 1;\n      if (!calm) wake();\n    };\n    const leave = () => { s.px = 0; s.py = 0; wake(); };\n    // pressing a pile itself knocks it too\n    const click = (e: MouseEvent) => {\n      if (e.target !== cv) return;\n      const r = box.getBoundingClientRect(), x = e.clientX - r.left, y = e.clientY - r.top;\n      const gap = s.xs.length > 1 ? (s.xs[1][0] - s.xs[0][0]) / 2 : 100;\n      s.xs.forEach((p, i) => { if (Math.abs(x - p[0]) < gap * 0.7 && y < p[1] && y > p[1] - cssH * 0.6) hop(i); });\n    };\n    cv.addEventListener(\"click\", click);\n    box.addEventListener(\"pointermove\", move);\n    box.addEventListener(\"pointerleave\", leave);\n    const lost = (e: Event) => { e.preventDefault(); setShown(\"still\"); };\n    cv.addEventListener(\"webglcontextlost\", lost);\n    wake();\n    return () => {\n      cancelAnimationFrame(s.raf); ro.disconnect(); io.disconnect();\n      document.removeEventListener(\"visibilitychange\", vis);\n      box.removeEventListener(\"pointermove\", move); box.removeEventListener(\"pointerleave\", leave);\n      cv.removeEventListener(\"webglcontextlost\", lost); cv.removeEventListener(\"click\", click);\n      api.current = null;\n    };\n  }, []);\n\n  const choose = (k: number) => {\n    if (api.current) api.current.go(k, true);\n    else { setPeriod(k); setNums(periods[k].values); }\n  };\n  const counts = stats.map((_, i) => discs(period, i));\n\n  return (\n    <section className=\"relative isolate overflow-hidden bg-[#e7e0d4] text-[#1d1a16]\" style={{ fontFamily: FONT }}>\n      <div ref={stage} className=\"relative mx-auto h-[600px] w-full max-w-[1600px] sm:h-[760px]\" style={{ background: \"radial-gradient(120% 90% at 35% 40%, #efe9df 0%, #e4ddd1 60%, #d6cdbf 100%)\" }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className={`absolute inset-0 h-full w-full transition-opacity duration-700 motion-reduce:transition-none ${shown === \"gl\" ? \"opacity-100\" : \"opacity-0\"}`} />\n        {shown === \"still\" && <Still counts={counts} />}\n\n        <div className=\"pointer-events-none absolute inset-x-0 top-0 flex flex-col gap-4 px-5 pt-6 sm:flex-row sm:items-start sm:justify-between sm:px-10 sm:pt-9 lg:px-14\">\n          <div>\n            <p className=\"text-[11px] font-medium uppercase tracking-[0.3em] text-[#1d1a16]/60\">Ferrow Pottery shop <span className=\"mx-1.5 text-[#1d1a16]/30\">/</span> demo data</p>\n            <h2 className=\"mt-2 text-[26px] font-semibold leading-none tracking-[-0.035em] sm:text-[32px]\">{periods[period].key}</h2>\n          </div>\n          <div role=\"group\" aria-label=\"Period\" className=\"pointer-events-auto inline-flex w-fit rounded-full bg-[#1d1a16]/[0.07] p-1\">\n            {periods.map((p, k) => (\n              <button key={p.key} type=\"button\" aria-pressed={period === k} onClick={() => choose(k)} className={`h-10 whitespace-nowrap rounded-full px-3 text-[12px] font-semibold tracking-[-0.01em] transition-colors duration-200 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[#1d1a16] motion-reduce:transition-none sm:px-4 sm:text-[13px] ${period === k ? \"bg-[#1d1a16] text-[#f3eee6]\" : \"text-[#1d1a16]/70 hover:text-[#1d1a16]\"}`}>{p.key}</button>\n            ))}\n          </div>\n        </div>\n\n        <ul className=\"pointer-events-none absolute inset-0\" aria-label={`${periods[period].key}, demo data`}>\n          {stats.map((st, i) => (\n            <li key={st.label}>\n              <div ref={(el) => { tags.current[i] = el; }} className=\"absolute w-[31%] -translate-x-1/2 text-center sm:w-[26%]\" style={{ left: `${20 + i * 30}%`, top: \"64%\" }}>\n                <button type=\"button\" onClick={() => api.current?.hop(i)} aria-label={`${fmt(nums[i])} ${st.label}. One disc is ${fmt(st.unit)}. Tap to knock the stack.`} className=\"pointer-events-auto w-full rounded-2xl px-1 py-1 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[#1d1a16]\">\n                  <span className=\"block text-[34px] font-medium leading-[0.86] tracking-[-0.06em] tabular-nums sm:text-[64px] lg:text-[88px]\">{fmt(nums[i])}</span>\n                  <span className=\"mt-2 flex items-center justify-center gap-2 text-[15px] italic leading-tight text-[#1d1a16]/80 sm:text-[20px]\" style={{ fontFamily: SERIF }}>\n                    <span className=\"h-2 w-2 shrink-0 rounded-full\" style={{ background: st.colour }} />{st.label}\n                  </span>\n                  <span className=\"mt-1 block text-[10px] font-semibold uppercase tracking-[0.18em] text-[#1d1a16]/55 sm:text-[11px]\">1 disc = {fmt(st.unit)}</span>\n                </button>\n              </div>\n            </li>\n          ))}\n        </ul>\n\n        <table className=\"sr-only\">\n          <caption>Shop figures by period, demo data</caption>\n          <thead><tr><th scope=\"col\">Period</th>{stats.map((s) => <th key={s.label} scope=\"col\">{s.label}</th>)}</tr></thead>\n          <tbody>{periods.map((p) => <tr key={p.key}><th scope=\"row\">{p.key}</th>{p.values.map((v, i) => <td key={stats[i].label}>{fmt(v)}</td>)}</tr>)}</tbody>\n        </table>\n      </div>\n    </section>\n  );\n}\n"}]}
