{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"gilt-edged-show-ticket-card-in-webgl-with-hot-foil-that-catches-a-softbox-as-it-tilts-over-blue-lacquer","type":"registry:block","title":"Gilt-Edged Show Ticket Card in WebGL with Hot Foil That Catches a Softbox as It Tilts over Blue Lacquer","description":"Free React 3D card in raw WebGL: a thick show ticket with a fashion photograph, gold foil that lights up under a softbox, gilt edges, a shadow and a mirror floor.","dependencies":["framer-motion"],"files":[{"path":"components/we-inc/gilt-edged-show-ticket-card-in-webgl-with-hot-foil-that-catches-a-softbox-as-it-tilts-over-blue-lacquer.tsx","type":"registry:component","content":"import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { useReducedMotion } from \"framer-motion\";\n\n/** Everything printed on the card. Change the words here and the foil, the emboss and the captions follow. */\nconst card = {\n  house: \"Orvelle\",\n  line: \"AUTUMN SHOW\",\n  admit: \"ADMIT ONE\",\n  number: \"No. 0412\",\n  venue: \"THE GLASSHOUSE, HARROW LANE\",\n  seat: [\n    { label: \"ROW\", value: \"A\" },\n    { label: \"SEAT\", value: \"14\" },\n    { label: \"DOORS\", value: \"19:30\" },\n  ],\n  small: \"ADMITS ONE GUEST  ·  PLEASE BRING THIS CARD\",\n  photo: {\n    src: \"https://images.pexels.com/photos/30755161/pexels-photo-30755161.jpeg?auto=compress&cs=tinysrgb&w=1000\",\n    alt: \"Woman with braided hair built into two tall spheres, hands pressed together, in a pale blue shirt against a cobalt backdrop\",\n    focusY: 0.0, // 0 keeps the top of the photograph, 1 the bottom\n    tone: \"#1b3f97\",\n  },\n};\nconst sides = [\n  { tag: \"Face\", title: \"Admit one\" },\n  { tag: \"Reverse\", title: \"Row A, seat 14\" },\n];\n\nconst TILT = 0.38; // how far the card turns to face the pointer, in radians\nconst SPRING = 70; // pull toward the pose it is asked for\nconst DAMPING = 9.5; // lower for more wobble\nconst FLIP_SPRING = 23;\nconst FLIP_DAMPING = 4.7; // lower for more overshoot when it turns over\nconst HOLD_S = 9; // seconds the card holds its resting pose after load before it starts to sway by itself\nconst SERIF = '\"Didot\", \"Bodoni 72\", \"Bodoni MT\", \"Playfair Display\", \"Libre Bodoni\", Georgia, serif';\nconst GROTESK = '\"Inter Tight\", \"Inter\", ui-sans-serif, system-ui, sans-serif';\n\nconst clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v));\n\n// The card, in metres: 1.0 wide, 1.5 tall, 30 mm thick board with a small rounded arris.\nconst HX = 0.5;\nconst HY = 0.75;\nconst HT = 0.028;\nconst CR = 0.055;\nconst TW = 1024;\nconst TH = 1536;\n// where things sit on the printed face, in texture pixels\nconst PH = { x: 62, y: 62, w: 900, h: 1010 };\nconst SEAL = { x: 820, y: 1076, r: 142 };\nconst toLocal = (x: number, y: number) => [((x / TW) * 2 - 1) * HX, (1 - (y / TH) * 2) * HY];\nconst SEAL_L = toLocal(SEAL.x, SEAL.y);\nconst SEAL_R = (SEAL.r / TW) * 2 * HX;\nconst ROSE = { x: 512, y: 640, r: 300 };\nconst ROSE_L = toLocal(ROSE.x, ROSE.y);\nconst ROSE_R = (ROSE.r / TW) * 2 * HX;\n\nconst GLSL_HEAD = `#version 300 es\nprecision highp float;\nprecision highp sampler2DArray;\nconst vec2 HS = vec2(${HX}, ${HY});\nconst float CR = ${CR};\nconst vec3 KEY = vec3(-2.5, 4.3, 3.1);\nconst vec3 KAIM = vec3(0.25, 0.0, -0.5);\nconst vec3 STRIP = vec3(4.2, 1.5, 0.6);\nconst vec3 TOP = vec3(-0.9, 6.0, 0.5);\nconst vec3 KICK = vec3(3.4, 0.6, -2.8);\nconst vec3 GOLD = vec3(1.0, 0.64, 0.2);\nuniform vec3 uC, uU, uV, uN;\nuniform sampler2DArray uAlb;\nuniform sampler2DArray uMat;\nuniform vec3 uCam, uRt, uUp, uFw;\nuniform float uTan, uAspect, uShift, uFocus, uAperture, uTime;\n\nfloat ign(vec2 p) { return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715)))); }\nfloat hash(vec2 p) { p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 45.32); return fract(p.x * p.y); }\nfloat vnoise(vec2 p) {\n  vec2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f);\n  return mix(mix(hash(i), hash(i + vec2(1, 0)), f.x), mix(hash(i + vec2(0, 1)), hash(i + vec2(1, 1)), f.x), f.y);\n}\nfloat fbm(vec2 p) { return 0.5 * vnoise(p) + 0.25 * vnoise(p * 2.1 + 7.3) + 0.125 * vnoise(p * 4.3 + 1.7) + 0.0625 * vnoise(p * 8.9); }\nfloat sdCard(vec2 p) { vec2 q = abs(p) - HS + CR; return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - CR; }\n\n// one rectangular lamp seen in a mirror, with edges that soften as the mirror gets rougher\nfloat lamp(vec3 rd, vec3 c, vec2 half_, float s) {\n  float d = dot(rd, c);\n  if (d <= 0.02) return 0.0;\n  vec3 rt = normalize(cross(vec3(0.0, 1.0, 0.0), c));\n  vec3 up = cross(c, rt);\n  vec2 e = half_ - abs(vec2(dot(rd, rt), dot(rd, up)) / d);\n  return smoothstep(-s, s, e.x) * smoothstep(-s, s, e.y);\n}\nvec3 floorTint(vec2 xz) {\n  vec2 q = (xz - vec2(0.2, -0.5)) / vec2(3.2, 3.0);\n  return vec3(0.006, 0.026, 0.2) * (0.12 + 2.2 * exp(-dot(q, q) * 1.6));\n}\n// The studio as a mirror sees it: a wide softbox low on the front left, the key above it,\n// a thin strip on the right, a long bank over the back, and the lit blue floor underneath.\nvec3 studio(vec3 ro, vec3 rd, float s) {\n  vec3 c = vec3(0.006, 0.008, 0.014);\n  float front = lamp(rd, normalize(vec3(-0.88, 0.17, 0.44)), vec2(0.21, 0.46), 0.035 + s);\n  vec3 fq = rd - normalize(vec3(-0.88, 0.17, 0.44));\n  float bar = 1.0 - 0.8 * smoothstep(0.022 + s, 0.008, abs(rd.y - 0.32 - 0.25 * fq.x * fq.x));\n  c += vec3(1.0, 0.95, 0.88) * front * bar * (0.55 + 1.9 * exp(-dot(fq, fq) * 16.0));\n  c += vec3(1.0, 0.96, 0.9) * 3.2 * lamp(rd, normalize(KEY - vec3(0.0, 0.9, 0.0)), vec2(0.17, 0.17), 0.02 + s);\n  c += vec3(0.9, 0.94, 1.0) * 2.6 * lamp(rd, normalize(STRIP - vec3(0.0, 0.9, 0.0)), vec2(0.05, 0.62), 0.012 + s);\n  c += vec3(0.9, 0.94, 1.0) * 1.6 * lamp(rd, normalize(KICK - vec3(0.0, 0.5, 0.0)), vec2(0.03, 0.34), 0.012 + s);\n  c += vec3(1.0, 0.97, 0.92) * 4.0 * lamp(rd, normalize(TOP - vec3(0.0, 0.9, 0.0)), vec2(0.1, 0.1), 0.03 + s);\n  c += vec3(0.7, 0.8, 1.0) * 0.5 * lamp(rd, normalize(vec3(0.0, 0.5, -0.86)), vec2(1.1, 0.2), 0.2 + s);\n  c += vec3(0.01, 0.04, 0.26) * exp(-abs(rd.y) * 9.0) * smoothstep(0.3, -0.5, rd.z) * 0.8;\n  if (rd.y < 0.0) {\n    vec3 p = ro + rd * (-ro.y / min(rd.y, -1e-3));\n    c = mix(c, floorTint(p.xz) * 2.2 + vec3(0.004, 0.006, 0.014), smoothstep(0.0, -0.06 - s, rd.y));\n  }\n  return c;\n}\n\n// light landing on one side of the card\nvec3 faceLight(vec3 P, vec3 n) {\n  vec3 lk = KEY - P; float r2 = dot(lk, lk); lk *= inversesqrt(r2);\n  vec3 c = vec3(1.0, 0.94, 0.86) * (32.0 / r2) * max(dot(n, lk), 0.0);\n  c += vec3(0.8, 0.88, 1.0) * 0.22 * max(dot(n, normalize(STRIP - P)), 0.0);\n  c += vec3(1.0, 0.97, 0.92) * 0.2 * max(dot(n, normalize(TOP - P)), 0.0);\n  c += vec3(1.0, 0.95, 0.88) * 0.3 * max(dot(n, normalize(vec3(-0.88, 0.17, 0.44))), 0.0);\n  c += mix(floorTint(P.xz) * 0.9, vec3(0.05, 0.048, 0.045), n.y * 0.5 + 0.5);\n  return c;\n}\n\n// how much of a round lamp at L a point P can see past the card\nfloat lit(vec3 P, vec3 L, float rl) {\n  vec3 d = L - P;\n  float dn = dot(d, uN);\n  if (abs(dn) < 1e-4) return 1.0;\n  float t = dot(uC - P, uN) / dn;\n  if (t <= 0.0 || t >= 1.0) return 1.0;\n  vec3 q = P + d * t - uC;\n  float w = rl * t + 0.003;\n  return smoothstep(-w, w, sdCard(vec2(dot(q, uU), dot(q, uV))));\n}\n\nvec3 tonemap(vec3 x) { x *= 0.9; return clamp((x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14), 0.0, 1.0); }\nvec4 pack(vec3 col, float depth) {\n  float coc = clamp(uAperture * (1.0 - uFocus / max(depth, 0.05)), -1.0, 1.0);\n  return vec4(pow(tonemap(col), vec3(1.0 / 2.2)), 0.5 + 0.5 * coc);\n}\n`;\n\nconst VS_FULL = `#version 300 es\nout vec2 vUv;\nvoid main() { vec2 p = vec2(float((gl_VertexID << 1) & 2), float(gl_VertexID & 2)); vUv = p; gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0); }`;\n\nconst FS_SET = `${GLSL_HEAD}\nin vec2 vUv;\nout vec4 o;\nvoid main() {\n  vec2 ndc = vUv * 2.0 - 1.0;\n  ndc.x -= uShift;\n  vec3 rd = normalize(uFw + uRt * (ndc.x * uTan * uAspect) + uUp * (ndc.y * uTan));\n  if (rd.y > -2e-3) { o = pack(studio(uCam, rd, 0.08), 80.0); return; }\n  float tf = -uCam.y / rd.y;\n  vec3 P = uCam + rd * tf;\n\n  // poured lacquer: a near mirror with slow waves, wiped patches that blur the mirror, and dust\n  float smudge = fbm(P.xz * 1.15 + 3.1);\n  float rough = 0.004 + 0.03 * smudge * smudge + 0.006 * vnoise(P.xz * vec2(2.0, 31.0));\n  vec2 wav = vec2(vnoise(P.xz * 1.9 + 11.0), vnoise(P.xz * 1.9 + 37.0)) - 0.5;\n  vec3 n = normalize(vec3(wav.x * 0.016, 1.0, wav.y * 0.016));\n  vec3 R = reflect(rd, n);\n  float F = 0.085 + 0.915 * pow(1.0 - clamp(dot(-rd, n), 0.0, 1.0), 5.0);\n  vec3 refl = studio(P, R, 0.02 + rough * 3.0);\n\n  // the card in the mirror: one ray to its plane, softened by distance and by the smudges\n  float far = 0.0, seen = 0.0;\n  float dn = dot(R, uN);\n  float t = dot(uC - P, uN) / dn;\n  if (t > 0.0) {\n    vec3 q = P + R * t - uC;\n    vec2 a = vec2(dot(q, uU), dot(q, uV));\n    float w = t * (0.004 + rough * 1.5);\n    seen = smoothstep(w, -w, sdCard(a));\n    if (seen > 0.0) {\n      float side = dn < 0.0 ? 1.0 : -1.0;\n      vec2 uv = clamp(a / HS, -1.0, 1.0) * 0.5 + 0.5;\n      if (side < 0.0) uv.x = 1.0 - uv.x;\n      float layer = side > 0.0 ? 0.0 : 1.0;\n      float lod = log2(1.0 + w * 1100.0);\n      vec3 alb = textureLod(uAlb, vec3(uv, layer), lod).rgb;\n      vec3 m = textureLod(uMat, vec3(uv, layer), lod).rgb;\n      vec3 nn = uN * side;\n      vec3 Pc = P + R * t;\n      vec3 cc = alb * faceLight(Pc, nn);\n      cc = mix(cc, GOLD * studio(Pc, reflect(R, nn), 0.12 + rough * 3.0), m.r);\n      refl = mix(refl, cc, seen);\n      far = t;\n    }\n  }\n\n  // light on the lacquer itself: the key through its cone, the strip, and the bank overhead, each shadowed by the card\n  vec3 lk = KEY - P; float r2 = dot(lk, lk); lk *= inversesqrt(r2);\n  float cone = smoothstep(0.84, 0.975, dot(-lk, normalize(KAIM - KEY)));\n  vec3 dif = vec3(1.0, 0.955, 0.9) * (120.0 * (0.03 + cone) / r2) * lk.y * lit(P, KEY, 0.36);\n  vec3 ls = normalize(STRIP - P);\n  dif += vec3(0.8, 0.88, 1.0) * 0.5 * ls.y * lit(P, STRIP, 0.3);\n  dif += vec3(0.7, 0.8, 1.0) * 0.3 * lit(P, vec3(uC.x, 7.0, uC.z - 1.0), 3.4);\n  vec3 lt = TOP - P;\n  dif += vec3(1.0, 0.97, 0.92) * (26.0 * lt.y / pow(dot(lt, lt), 1.5)) * lit(P, TOP, 0.2);\n  vec3 alb = vec3(0.006, 0.026, 0.2) * (0.94 + 0.12 * vnoise(P.xz * 90.0));\n  vec3 col = alb * dif * (1.0 - F) + refl * F;\n\n  // the foil throws the top lamp back down as a warm patch: mirror the lamp in the card and see whether foil is in the way\n  float kn = dot(TOP - uC, uN);\n  vec3 dm = TOP - 2.0 * kn * uN - P;\n  float tm = dot(uC - P, uN) / dot(dm, uN);\n  if (tm > 0.0 && tm < 1.0 && dot(P - uC, uN) * kn > 0.0) {\n    vec3 qm = P + dm * tm - uC;\n    vec2 am = vec2(dot(qm, uU), dot(qm, uV));\n    float wm = 0.22 * tm + 0.006;\n    float inside = smoothstep(wm, -wm, sdCard(am));\n    vec2 um = clamp(am / HS, -1.0, 1.0) * 0.5 + 0.5;\n    if (kn < 0.0) um.x = 1.0 - um.x;\n    float leaf = textureLod(uMat, vec3(um, kn > 0.0 ? 0.0 : 1.0), log2(1.0 + wm * 1100.0)).r;\n    col += GOLD * (leaf * inside * 42.0 * max(dm.y, 0.0) / pow(dot(dm, dm), 1.5)) * (alb * 0.3 + vec3(0.06, 0.04, 0.014));\n  }\n  float dust = step(0.9992, hash(floor(P.xz * 380.0))) * smoothstep(8.0, 2.0, tf);\n  col += vec3(0.5, 0.6, 0.8) * dust * (0.2 + cone) * 0.3;\n  col = mix(col, vec3(0.003, 0.006, 0.02), 1.0 - exp(-max(tf - 4.6, 0.0) * 0.11));\n  o = pack(col, dot(rd, uFw) * (tf + far * seen * clamp(F * 3.0, 0.0, 1.0)));\n}`;\n\nconst VS_CARD = `#version 300 es\nprecision highp float;\nlayout(location = 0) in vec3 aPos;\nlayout(location = 1) in vec3 aNor;\nuniform vec3 uC, uU, uV, uN, uCam, uRt, uUp, uFw;\nuniform float uTan, uAspect, uShift;\nout vec3 vW; out vec3 vL; out vec3 vNl;\nvoid main() {\n  vW = uC + uU * aPos.x + uV * aPos.y + uN * aPos.z;\n  vL = aPos; vNl = aNor;\n  vec3 d = vW - uCam;\n  float z = dot(d, uFw);\n  gl_Position = vec4(dot(d, uRt) / (uTan * uAspect) + uShift * z, dot(d, uUp) / uTan, (z * 60.1 - 12.0) / 59.9, z);\n}`;\n\nconst FS_CARD = `${GLSL_HEAD}\nfloat glint(vec3 n, vec3 V, vec3 l) { return pow(max(dot(n, normalize(normalize(l) + V)), 0.0), 26.0); }\nconst vec2 SEAL = vec2(${SEAL_L[0].toFixed(5)}, ${SEAL_L[1].toFixed(5)});\nconst float SEAL_R = ${SEAL_R.toFixed(5)};\nconst vec2 ROSE = vec2(${ROSE_L[0].toFixed(5)}, ${ROSE_L[1].toFixed(5)});\nconst float ROSE_R = ${ROSE_R.toFixed(5)};\nin vec3 vW; in vec3 vL; in vec3 vNl;\nout vec4 o;\nvoid main() {\n  vec3 V = normalize(uCam - vW);\n  vec3 nl = normalize(vNl);\n  vec3 Ng = normalize(uU * nl.x + uV * nl.y + uN * nl.z);\n  float face = smoothstep(0.75, 0.98, abs(nl.z));\n  float side = vL.z > 0.0 ? 1.0 : -1.0;\n  vec3 col;\n\n  // the gilt edge: burnished leaf over the plies of the board, streaked along its length\n  float along = abs(nl.x) > abs(nl.y) ? vL.y : vL.x;\n  float streak = vnoise(vec2(vL.z * 520.0, along * 2.5));\n  vec3 ne = normalize(Ng + uN * (streak - 0.5) * 0.35 + (uU + uV) * (vnoise(vec2(along * 40.0, vL.z * 300.0)) - 0.5) * 0.04 + (uU + uV) * (vnoise(vec2(along * 4.0, 3.0)) - 0.5) * 0.3);\n  vec3 Re = reflect(-V, ne);\n  float Fe = pow(1.0 - max(dot(ne, V), 0.0), 5.0);\n  vec3 glints = vec3(1.0, 0.95, 0.88) * (1.5 * glint(ne, V, KEY - vW) + 1.2 * glint(ne, V, vec3(-0.88, 0.17, 0.44)) + 1.1 * glint(ne, V, TOP - vW))\n    + vec3(0.9, 0.94, 1.0) * (1.3 * glint(ne, V, STRIP - vW) + 1.7 * glint(ne, V, KICK - vW));\n  vec3 edge = vec3(1.0, 0.6, 0.17) * (faceLight(vW, ne) * vec3(0.25, 0.2, 0.15) + glints * 1.3) * (0.45 + 1.1 * streak) + mix(GOLD, vec3(1.0), Fe * 0.5) * studio(vW, Re, 0.12) * 0.22;\n  edge *= 0.86 + 0.14 * smoothstep(0.0, 0.25, abs(fract(vL.z / ${(HT * 2).toFixed(4)} * 3.0 + 0.5) - 0.5));\n\n  if (face > 0.0) {\n    vec2 uv = vL.xy / HS * 0.5 + 0.5;\n    if (side < 0.0) uv.x = 1.0 - uv.x;\n    float layer = side > 0.0 ? 0.0 : 1.0;\n    vec3 alb = texture(uAlb, vec3(uv, layer)).rgb;\n    vec3 m = texture(uMat, vec3(uv, layer)).rgb;\n    // relief pressed into the board: foil sunk by the die, the photograph in its plate mark, the seal raised\n    vec2 e = vec2(1.6 / ${TW}.0, 1.6 / ${TH}.0);\n    float hx = texture(uMat, vec3(uv + vec2(e.x, 0.0), layer), 0.7).g - texture(uMat, vec3(uv - vec2(e.x, 0.0), layer), 0.7).g;\n    float hy = texture(uMat, vec3(uv + vec2(0.0, e.y), layer), 0.7).g - texture(uMat, vec3(uv - vec2(0.0, e.y), layer), 0.7).g;\n    vec2 g = vec2(hx, hy) * 5.5 * (1.0 - 0.5 * smoothstep(0.08, 0.6, m.r)) * clamp(1.6 / (fwidth(uv.x) * ${TW}.0), 0.0, 1.0);\n    // cotton paper has a tooth; foil is smooth but never quite flat\n    vec2 pp = vL.xy * vec2(side, 1.0);\n    vec2 tooth = vec2(fbm(pp * 150.0), fbm(pp * 150.0 + 31.0)) - 0.47;\n    vec2 wave = vec2(fbm(pp * 4.0 + 3.0), fbm(pp * 4.0 + 17.0)) - 0.47;\n    float foil = smoothstep(0.08, 0.6, m.r);\n    float gloss = m.b;\n    vec2 tiltP = -g + tooth * mix(0.17, 0.015, max(foil, gloss)) + wave * mix(0.012, 0.03, foil);\n\n    // the seal: a fluted ring of foil, every flute leaning a different way round the circle\n    vec2 sp = pp - (side > 0.0 ? SEAL : ROSE);\n    float sr = length(sp) / (side > 0.0 ? SEAL_R : ROSE_R);\n    float inSeal = smoothstep(0.9, 0.87, sr) * smoothstep(0.37, 0.4, sr) * foil;\n    vec2 rad = sp / max(length(sp), 1e-4);\n    float ang = atan(sp.y, sp.x);\n    float count = side > 0.0 ? 36.0 : 48.0;\n    float flute = sin(ang * count + sr * 9.0) * clamp(6.2832 * length(sp) / (count * 4.5 * fwidth(pp.x)) - 0.3, 0.0, 1.0);\n    tiltP += vec2(-rad.y, rad.x) * flute * 0.3 * inSeal;\n    vec2 tilt = vec2(tiltP.x * side, tiltP.y);\n\n    vec3 N = normalize(Ng + (uU * tilt.x + uV * tilt.y));\n    vec3 R = reflect(-V, N);\n    float nv = max(dot(N, V), 0.0);\n    float fres = pow(1.0 - nv, 5.0);\n\n    // paper and ink\n    vec3 light = faceLight(vW, N);\n    float fibre = 0.92 + 0.16 * fbm(pp * 60.0) - 0.06 * vnoise(pp * 520.0);\n    col = alb * mix(fibre, 1.0, gloss) * light;\n    // the photograph carries a satin varnish; the paper only a faint sheen\n    col += studio(vW, R, mix(0.5, 0.05, gloss)) * (0.04 + 0.96 * fres) * mix(0.2, 0.2, gloss);\n    // hot foil: a metal mirror, warm, with the grain of the paper pressed through it\n    vec3 fc = mix(GOLD, vec3(1.0), fres * 0.6) * studio(vW, R, 0.05 + 0.03 * inSeal) * 0.45;\n    fc += GOLD * GOLD * light * 0.08;\n    fc *= 0.9 + 0.2 * vnoise(pp * 420.0);\n    col = mix(col, fc, foil);\n    col = mix(edge, col, face);\n  } else col = edge;\n  o = pack(col, dot(vW - uCam, uFw));\n}`;\n\nconst FS_LENS = `#version 300 es\nprecision highp float;\nuniform sampler2D uScene;\nuniform vec2 uPx;\nuniform float uMaxR;\nin vec2 vUv;\nout vec4 o;\nfloat ign(vec2 p) { return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715)))); }\nvoid main() {\n  vec4 c0 = texture(uScene, vUv);\n  float s0 = c0.a * 2.0 - 1.0;\n  float coc0 = abs(s0);\n  vec3 lin0 = pow(c0.rgb, vec3(2.2));\n  float w0 = 1.0 / (1.03 - max(lin0.r, max(lin0.g, lin0.b)));\n  vec3 acc = lin0 * w0; float wsum = w0;\n  float rot = 6.2832 * ign(gl_FragCoord.xy);\n  // a disc of taps for the out-of-focus blur, each one only counted if its own blur reaches this pixel\n  for (int k = 1; k < 44; k++) {\n    float r = k < 32 ? sqrt(float(k) / 32.0) * uMaxR : sqrt(float(k - 31) / 12.0) * min(uMaxR, 3.0);\n    float a = float(k) * 2.39996 + rot;\n    vec4 s = texture(uScene, vUv + vec2(cos(a), sin(a)) * r * uPx);\n    float sc = s.a * 2.0 - 1.0;\n    float cs = abs(sc);\n    if (sc > s0) cs = min(cs, coc0 * 1.6 + 0.02);\n    vec3 lin = pow(s.rgb, vec3(2.2));\n    float w = smoothstep(r - 1.0, r + 1.0, cs * uMaxR) / (1.03 - max(lin.r, max(lin.g, lin.b)));\n    acc += lin * w; wsum += w;\n  }\n  o = vec4(pow(acc / wsum, vec3(1.0 / 2.2)), coc0);\n}`;\n\nconst FS_OUT = `#version 300 es\nprecision highp float;\nuniform sampler2D uScene;\nuniform vec2 uPx;\nuniform float uTime;\nin vec2 vUv;\nout vec4 o;\nfloat ign(vec2 p) { return fract(52.9829189 * fract(dot(p, vec2(0.06711056, 0.00583715)))); }\nvoid main() {\n  vec4 c = texture(uScene, vUv);\n  float r = clamp(c.a * 4.0, 0.0, 1.0) * 1.4;\n  vec3 col = c.rgb * 4.0;\n  vec3 glow = vec3(0.0);\n  float rot = 6.2832 * ign(gl_FragCoord.xy);\n  for (int k = 0; k < 8; k++) {\n    float a = float(k) * 0.7854;\n    col += texture(uScene, vUv + vec2(cos(a), sin(a)) * r * (k % 2 == 0 ? 1.0 : 1.9) * uPx).rgb;\n  }\n  col /= 12.0;\n  // halation: only the hottest foil and gilt bleeds a little into its surroundings\n  for (int k = 0; k < 16; k++) {\n    float a = float(k) * 2.39996 + rot;\n    float d = 3.0 + 22.0 * sqrt(float(k) / 16.0);\n    vec3 s = texture(uScene, vUv + vec2(cos(a), sin(a)) * d * uPx).rgb;\n    glow += s * smoothstep(0.86, 1.0, min(s.r, s.g));\n  }\n  col += glow / 16.0 * vec3(0.36, 0.3, 0.2);\n  vec2 q = vUv - 0.5;\n  col *= 1.0 - 0.75 * dot(q, q);\n  col += (ign(gl_FragCoord.xy + fract(uTime) * 61.0) - 0.5) * 0.022;\n  o = vec4(col, 1.0);\n}`;\n\nfunction program(gl: WebGL2RenderingContext, vs: string, fs: string) {\n  const p = gl.createProgram()!;\n  for (const [type, src] of [[gl.VERTEX_SHADER, vs], [gl.FRAGMENT_SHADER, fs]] as const) {\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    gl.attachShader(p, s);\n  }\n  gl.linkProgram(p);\n  if (!gl.getProgramParameter(p, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(p) || \"link\");\n  return p;\n}\n\n/** The board as triangles: two faces, a flat side, and a quarter-round arris between them. Position + normal. */\nfunction cardMesh() {\n  const SEG = 9;\n  const B = 0.0042;\n  const ring = (d: number, z: number, tilt: number, nz: number) => {\n    const out: number[][] = [];\n    for (let k = 0; k < 4; k++) {\n      const cx = (k === 0 || k === 3 ? 1 : -1) * (HX - CR);\n      const cy = (k < 2 ? 1 : -1) * (HY - CR);\n      for (let j = 0; j <= SEG; j++) {\n        const a = ((k + j / SEG) * Math.PI) / 2;\n        const dx = Math.cos(a), dy = Math.sin(a);\n        out.push([cx + (CR - d) * dx, cy + (CR - d) * dy, z, dx * tilt, dy * tilt, nz]);\n      }\n    }\n    return out;\n  };\n  const rings: number[][][] = [];\n  for (const sgn of [1, -1]) {\n    const order = sgn > 0 ? [0, 30, 60, 90] : [90, 60, 30, 0];\n    for (const deg of order) {\n      const p = (deg * Math.PI) / 180;\n      rings.push(ring(B * (1 - Math.sin(p)), sgn * (HT - B * (1 - Math.cos(p))), Math.sin(p), sgn * Math.cos(p)));\n    }\n  }\n  const v: number[] = [];\n  const M = rings[0].length;\n  for (let r = 0; r < rings.length - 1; r++) {\n    for (let i = 0; i < M; i++) {\n      const a = rings[r][i], b = rings[r][(i + 1) % M], c = rings[r + 1][i], d = rings[r + 1][(i + 1) % M];\n      v.push(...a, ...b, ...c, ...b, ...d, ...c);\n    }\n  }\n  for (const [r, z] of [[0, 1], [rings.length - 1, -1]]) {\n    for (let i = 0; i < M; i++) v.push(0, 0, z * HT, 0, 0, z, ...rings[r][i], ...rings[r][(i + 1) % M]);\n  }\n  return new Float32Array(v);\n}\n\ntype Channel = \"alb\" | \"foil\" | \"height\" | \"gloss\";\n\n/** Draw one side of the card into one channel: its printed colour, where the foil is, how high the surface stands, where the varnish is. */\nfunction paintSide(ctx: CanvasRenderingContext2D, side: number, ch: Channel, photo: HTMLImageElement | null) {\n  const PAPER = \"#f4ead6\";\n  const BLUE = \"#122a78\";\n  const INK = \"#1d1b18\";\n  const LEAF = \"#b18a4c\";\n  ctx.globalCompositeOperation = \"source-over\";\n  ctx.fillStyle = { alb: side ? BLUE : PAPER, foil: \"#000\", height: \"#808080\", gloss: \"#000\" }[ch];\n  ctx.fillRect(0, 0, TW, TH);\n  const put = (c: Partial<Record<Channel, string>>, draw: () => void) => {\n    const col = c[ch];\n    if (!col) return;\n    ctx.fillStyle = col;\n    ctx.strokeStyle = col;\n    draw();\n  };\n  const tracked = (s: string, x: number, y: number, font: string, gap: number, align: \"left\" | \"right\" | \"center\" = \"left\") => {\n    ctx.font = font;\n    ctx.textAlign = \"left\";\n    const widths = [...s].map((ch2) => ctx.measureText(ch2).width + gap);\n    const total = widths.reduce((a, b) => a + b, 0) - gap;\n    let cx = align === \"left\" ? x : align === \"right\" ? x - total : x - total / 2;\n    [...s].forEach((ch2, i) => {\n      ctx.fillText(ch2, cx, y);\n      cx += widths[i];\n    });\n  };\n  const foil = { alb: LEAF, foil: \"#fff\", height: \"#6c6c6c\" };\n  const ink = { alb: side ? \"#e6dfcf\" : INK, height: \"#757575\" };\n  // a foil medal: a domed disc with raised rings and the monogram standing in the middle\n  const seal = (cx: number, cy: number, r: number, px: number) => {\n    put({ alb: \"#8b6a36\", foil: \"#fff\", gloss: \"#000\" }, () => {\n      ctx.beginPath();\n      ctx.arc(cx, cy, r, 0, 7);\n      ctx.fill();\n    });\n    if (ch === \"height\") {\n      const g = ctx.createRadialGradient(cx, cy, 0, cx, cy, r);\n      g.addColorStop(0, \"#c4c4c4\");\n      g.addColorStop(0.8, \"#b0b0b0\");\n      g.addColorStop(1, \"#9c9c9c\");\n      ctx.fillStyle = g;\n      ctx.beginPath();\n      ctx.arc(cx, cy, r, 0, 7);\n      ctx.fill();\n      ctx.strokeStyle = \"#dedede\";\n      ctx.lineWidth = Math.max(5, r * 0.03);\n      for (const rr of [r * 0.93, r * 0.37]) {\n        ctx.beginPath();\n        ctx.arc(cx, cy, rr, 0, 7);\n        ctx.stroke();\n      }\n    }\n    put({ alb: \"#5e4620\", height: \"#e8e8e8\" }, () => {\n      ctx.font = `italic ${px}px ${SERIF}`;\n      ctx.textAlign = \"center\";\n      ctx.fillText(card.house[0], cx - px * 0.02, cy + px * 0.33);\n    });\n  };\n  ctx.textBaseline = \"alphabetic\";\n\n  if (side === 0) {\n    // the photograph, tipped into a plate mark\n    put({ alb: card.photo.tone, height: \"#484848\", gloss: \"#fff\" }, () => ctx.fillRect(PH.x, PH.y, PH.w, PH.h));\n    if (ch === \"alb\") {\n      ctx.save();\n      ctx.beginPath();\n      ctx.rect(PH.x, PH.y, PH.w, PH.h);\n      ctx.clip();\n      if (photo) {\n        const s = Math.max(PH.w / photo.naturalWidth, PH.h / photo.naturalHeight);\n        const w = photo.naturalWidth * s, h = photo.naturalHeight * s;\n        ctx.drawImage(photo, PH.x + (PH.w - w) / 2, PH.y + (PH.h - h) * card.photo.focusY, w, h);\n      } else {\n        const g = ctx.createLinearGradient(0, PH.y, 0, PH.y + PH.h);\n        g.addColorStop(0, \"#16337f\");\n        g.addColorStop(1, card.photo.tone);\n        ctx.fillStyle = g;\n        ctx.fillRect(PH.x, PH.y, PH.w, PH.h);\n      }\n      // the wall of the plate mark shades the picture's edge\n      for (let i = 0; i < 5; i++) {\n        ctx.strokeStyle = `rgba(4,8,24,${0.2 - i * 0.035})`;\n        ctx.lineWidth = 2;\n        ctx.strokeRect(PH.x + 1 + i * 2, PH.y + 1 + i * 2, PH.w - 2 - i * 4, PH.h - 2 - i * 4);\n      }\n      ctx.restore();\n    }\n        put(ink, () => tracked(card.line, PH.x + 2, 1148, `600 25px ${GROTESK}`, 8));\n    put(foil, () => {\n      ctx.font = `italic 238px ${SERIF}`;\n      ctx.textAlign = \"left\";\n      ctx.fillText(card.house, PH.x - 12, 1362);\n    });\n    put(foil, () => tracked(card.number, PH.x + PH.w, 1362, `500 30px ${GROTESK}`, 5, \"right\"));\n    put(foil, () => ctx.fillRect(PH.x, 1403, PH.w, 4));\n    put(ink, () => {\n      tracked(card.venue, PH.x + 2, 1462, `600 22px ${GROTESK}`, 6);\n      tracked(card.admit, PH.x + PH.w, 1462, `600 22px ${GROTESK}`, 6, \"right\");\n    });\n    // the seal, stamped half on the photograph and half on the paper\n    seal(SEAL.x, SEAL.y, SEAL.r, 96);\n  } else {\n    // reverse: cobalt stock, a foil keyline, a large fluted medal, and the seat\n    put(foil, () => {\n      ctx.lineWidth = 4;\n      ctx.strokeRect(62, 62, TW - 124, TH - 124);\n    });\n    put(foil, () => tracked(card.number, TW / 2, 172, `500 30px ${GROTESK}`, 6, \"center\"));\n    seal(ROSE.x, ROSE.y, ROSE.r, 210);\n    put(foil, () => {\n      ctx.font = `italic 104px ${SERIF}`;\n      ctx.textAlign = \"center\";\n      ctx.fillText(card.house, TW / 2, 1096);\n    });\n    put(foil, () => ctx.fillRect(140, 1150, TW - 280, 4));\n    card.seat.forEach((s, i) => {\n      const x = 140 + ((TW - 280) / 3) * (i + 0.5);\n      put(ink, () => tracked(s.label, x, 1214, `600 22px ${GROTESK}`, 7, \"center\"));\n      put(foil, () => {\n        ctx.font = `104px ${SERIF}`;\n        ctx.textAlign = \"center\";\n        ctx.fillText(s.value, x, 1330);\n      });\n    });\n    put(foil, () => ctx.fillRect(140, 1370, TW - 280, 4));\n    put(ink, () => tracked(card.small, TW / 2, 1430, `600 19px ${GROTESK}`, 5, \"center\"));\n  }\n}\n\ntype Sim = {\n  yaw: number; vyaw: number; pitch: number; vpitch: number; flip: number; vflip: number; goal: number;\n  px: number; py: number; inside: boolean; press: number; pressGoal: number; focus: number; t0: number; dirty: boolean;\n};\n\nexport default function GiltEdgedShowCard() {\n  const reduce = useReducedMotion();\n  const wrapRef = useRef<HTMLDivElement>(null);\n  const canvasRef = useRef<HTMLCanvasElement>(null);\n  const [side, setSide] = useState(0);\n  const [mode, setMode] = useState<\"pending\" | \"gl\" | \"still\">(\"pending\");\n  const sim = useRef<Sim>({ yaw: 0, vyaw: 0, pitch: 0, vpitch: 0, flip: 0, vflip: 0, goal: 0, px: 0, py: 0, inside: false, press: 0, pressGoal: 0, focus: 4, t0: 0, dirty: true });\n\n  const turn = useCallback(() => {\n    const s = sim.current;\n    s.goal += 1;\n    s.vflip -= 2.4; // a small wind-up the other way before it goes\n    s.dirty = true;\n    setSide(((s.goal % 2) + 2) % 2);\n  }, []);\n\n  useEffect(() => {\n    const wrap = wrapRef.current;\n    const canvas = canvasRef.current;\n    if (!wrap || !canvas) return;\n    const gl = canvas.getContext(\"webgl2\", { antialias: false, alpha: false, powerPreference: \"high-performance\" });\n    if (!gl) {\n      setMode(\"still\");\n      return;\n    }\n    const s = sim.current;\n    let progSet: WebGLProgram, progCard: WebGLProgram, progLens: WebGLProgram, progOut: WebGLProgram;\n    try {\n      progSet = program(gl, VS_FULL, FS_SET);\n      progCard = program(gl, VS_CARD, FS_CARD);\n      progLens = program(gl, VS_FULL, FS_LENS);\n      progOut = program(gl, VS_FULL, FS_OUT);\n    } catch {\n      setMode(\"still\");\n      return;\n    }\n    setMode(\"gl\");\n\n    // two array textures, a layer per side: printed colour (sRGB) and foil / height / varnish masks\n    const levels = Math.floor(Math.log2(TH)) + 1;\n    const aniso = gl.getExtension(\"EXT_texture_filter_anisotropic\");\n    const makeTex = (unit: number, format: number) => {\n      const t = gl.createTexture();\n      gl.activeTexture(unit);\n      gl.bindTexture(gl.TEXTURE_2D_ARRAY, t);\n      gl.texStorage3D(gl.TEXTURE_2D_ARRAY, levels, format, TW, TH, 2);\n      gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);\n      gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n      gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n      if (aniso) gl.texParameterf(gl.TEXTURE_2D_ARRAY, aniso.TEXTURE_MAX_ANISOTROPY_EXT, 8);\n      return t;\n    };\n    const albTex = makeTex(gl.TEXTURE0, gl.SRGB8_ALPHA8);\n    const matTex = makeTex(gl.TEXTURE1, gl.RGBA8);\n    const scratch = document.createElement(\"canvas\");\n    const mix = document.createElement(\"canvas\");\n    scratch.width = mix.width = TW;\n    scratch.height = mix.height = TH;\n    const sctx = scratch.getContext(\"2d\")!;\n    const mctx = mix.getContext(\"2d\")!;\n    gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);\n    const push = (unit: number, tex: WebGLTexture | null, layer: number, src: HTMLCanvasElement) => {\n      gl.activeTexture(unit);\n      gl.bindTexture(gl.TEXTURE_2D_ARRAY, tex);\n      gl.texSubImage3D(gl.TEXTURE_2D_ARRAY, 0, 0, 0, layer, TW, TH, 1, gl.RGBA, gl.UNSIGNED_BYTE, src);\n      gl.generateMipmap(gl.TEXTURE_2D_ARRAY);\n    };\n    const paint = (photo: HTMLImageElement | null, withMasks: boolean) => {\n      for (let layer = 0; layer < 2; layer++) {\n        paintSide(sctx, layer, \"alb\", photo);\n        push(gl.TEXTURE0, albTex, layer, scratch);\n        if (!withMasks) continue;\n        // three grey drawings become the red, green and blue of one mask texture\n        mctx.globalCompositeOperation = \"source-over\";\n        mctx.fillStyle = \"#000\";\n        mctx.fillRect(0, 0, TW, TH);\n        for (const [ch, tint] of [[\"foil\", \"#f00\"], [\"height\", \"#0f0\"], [\"gloss\", \"#00f\"]] as const) {\n          paintSide(sctx, layer, ch, null);\n          sctx.globalCompositeOperation = \"multiply\";\n          sctx.fillStyle = tint;\n          sctx.fillRect(0, 0, TW, TH);\n          mctx.globalCompositeOperation = \"lighter\";\n          mctx.drawImage(scratch, 0, 0);\n        }\n        push(gl.TEXTURE1, matTex, layer, mix);\n      }\n      s.dirty = true;\n    };\n    paint(null, true);\n    let alive = true;\n    const img = new Image();\n    img.crossOrigin = \"anonymous\";\n    img.onload = () => {\n      if (alive) paint(img, false);\n    };\n    img.src = card.photo.src;\n\n    const mesh = cardMesh();\n    const vao = gl.createVertexArray();\n    gl.bindVertexArray(vao);\n    gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());\n    gl.bufferData(gl.ARRAY_BUFFER, mesh, gl.STATIC_DRAW);\n    gl.enableVertexAttribArray(0);\n    gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 24, 0);\n    gl.enableVertexAttribArray(1);\n    gl.vertexAttribPointer(1, 3, gl.FLOAT, false, 24, 12);\n    gl.bindVertexArray(null);\n    const emptyVao = gl.createVertexArray();\n\n    // multisampled scene, resolved to a texture for the lens pass (colour, with blur size in alpha)\n    const msFbo = gl.createFramebuffer();\n    const msColor = gl.createRenderbuffer();\n    const msDepth = gl.createRenderbuffer();\n    const resFbo = gl.createFramebuffer();\n    const resTex = gl.createTexture();\n    const lensFbo = gl.createFramebuffer();\n    const lensTex = gl.createTexture();\n    let W = 0, H = 0, scale = Math.min(window.devicePixelRatio || 1, 2);\n    const samples = Math.min(4, gl.getParameter(gl.MAX_SAMPLES));\n    const resize = () => {\n      const cw = wrap.clientWidth, ch = wrap.clientHeight;\n      let k = scale;\n      if (cw * ch * k * k > 2.1e6) k = Math.sqrt(2.1e6 / (cw * ch));\n      const w = Math.max(2, Math.round(cw * k)), h = Math.max(2, Math.round(ch * k));\n      if (w === W && h === H) return;\n      W = w;\n      H = h;\n      canvas.width = W;\n      canvas.height = H;\n      gl.bindRenderbuffer(gl.RENDERBUFFER, msColor);\n      gl.renderbufferStorageMultisample(gl.RENDERBUFFER, samples, gl.RGBA8, W, H);\n      gl.bindRenderbuffer(gl.RENDERBUFFER, msDepth);\n      gl.renderbufferStorageMultisample(gl.RENDERBUFFER, samples, gl.DEPTH_COMPONENT16, W, H);\n      gl.bindFramebuffer(gl.FRAMEBUFFER, msFbo);\n      gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, msColor);\n      gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, msDepth);\n      for (const [unit, t, f] of [[gl.TEXTURE2, resTex, resFbo], [gl.TEXTURE3, lensTex, lensFbo]] as const) {\n        gl.activeTexture(unit);\n        gl.bindTexture(gl.TEXTURE_2D, t);\n        gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, W, H, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);\n        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_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, f);\n        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, t, 0);\n      }\n      s.dirty = true;\n    };\n    resize();\n    const ro = new ResizeObserver(resize);\n    ro.observe(wrap);\n\n    const U = (p: WebGLProgram, n: string) => gl.getUniformLocation(p, n);\n    const TAN = Math.tan((19 * Math.PI) / 180);\n    const LEAN = 0.36;\n    const REST_YAW = -0.55;\n    const pose = { c: [0, 0, 0], u: [1, 0, 0], v: [0, 1, 0], n: [0, 0, 1] };\n\n    const shared = (p: WebGLProgram, cam: number[], rt: number[], up: number[], fw: number[], shift: number, t: number) => {\n      gl.useProgram(p);\n      gl.uniform3fv(U(p, \"uC\"), pose.c);\n      gl.uniform3fv(U(p, \"uU\"), pose.u);\n      gl.uniform3fv(U(p, \"uV\"), pose.v);\n      gl.uniform3fv(U(p, \"uN\"), pose.n);\n      gl.uniform1i(U(p, \"uAlb\"), 0);\n      gl.uniform1i(U(p, \"uMat\"), 1);\n      gl.uniform3fv(U(p, \"uCam\"), cam);\n      gl.uniform3fv(U(p, \"uRt\"), rt);\n      gl.uniform3fv(U(p, \"uUp\"), up);\n      gl.uniform3fv(U(p, \"uFw\"), fw);\n      gl.uniform1f(U(p, \"uTan\"), TAN);\n      gl.uniform1f(U(p, \"uAspect\"), W / H);\n      gl.uniform1f(U(p, \"uShift\"), shift);\n      gl.uniform1f(U(p, \"uFocus\"), s.focus);\n      gl.uniform1f(U(p, \"uAperture\"), 1.5);\n      gl.uniform1f(U(p, \"uTime\"), t);\n    };\n\n    const draw = (t: number) => {\n      // the card's pose: spin about its own upright, then lean back, with a little roll from the speed of the turn\n      const a = REST_YAW + s.yaw + s.flip * Math.PI;\n      const lean = LEAN - s.pitch;\n      const roll = clamp(-s.vyaw * 0.012 - s.vflip * 0.006, -0.08, 0.08);\n      const rot = (x: number, y: number, z: number) => {\n        const x1 = x * Math.cos(roll) - y * Math.sin(roll), y1 = x * Math.sin(roll) + y * Math.cos(roll);\n        const x2 = x1 * Math.cos(a) + z * Math.sin(a), z2 = -x1 * Math.sin(a) + z * Math.cos(a);\n        return [x2, y1 * Math.cos(lean) + z2 * Math.sin(lean), -y1 * Math.sin(lean) + z2 * Math.cos(lean)];\n      };\n      pose.u = rot(1, 0, 0);\n      pose.v = rot(0, 1, 0);\n      pose.n = rot(0, 0, 1);\n      const spin = Math.sin(clamp(Math.abs(s.flip - Math.round(s.flip)), 0, 0.5) * Math.PI);\n      const bob = reduce ? 0 : 0.012 * Math.sin(Math.max(t - HOLD_S, 0) * 0.9);\n      pose.c = [0, 0.83 + bob + 0.05 * spin * spin - 0.05 * s.press, 0];\n\n      const aspect = W / H;\n      const dist = Math.max(3.5, 0.72 / (TAN * aspect) + 0.6);\n      const el = 0.36;\n      const drift = reduce ? 0 : 1;\n      const cam = [drift * 0.07 * Math.sin(t * 0.27), 0.7 + Math.sin(el) * dist + drift * 0.02 * Math.sin(t * 0.21 + 1), Math.cos(el) * dist];\n      let fw = [0 - cam[0], 0.7 - cam[1], 0 - cam[2]];\n      const fl = Math.hypot(fw[0], fw[1], fw[2]);\n      fw = fw.map((v) => v / fl);\n      let rt = [-fw[2], 0, fw[0]];\n      const rl = Math.hypot(rt[0], rt[2]);\n      rt = rt.map((v) => v / rl);\n      const up = [rt[1] * fw[2] - rt[2] * fw[1], rt[2] * fw[0] - rt[0] * fw[2], rt[0] * fw[1] - rt[1] * fw[0]];\n      s.focus = (pose.c[0] - cam[0]) * fw[0] + (pose.c[1] - cam[1]) * fw[1] + (pose.c[2] - cam[2]) * fw[2];\n      const shift = aspect > 1.25 ? 0.13 : 0;\n\n      gl.bindFramebuffer(gl.FRAMEBUFFER, msFbo);\n      gl.viewport(0, 0, W, H);\n      gl.disable(gl.DEPTH_TEST);\n      gl.depthMask(false);\n      shared(progSet, cam, rt, up, fw, shift, t);\n      gl.bindVertexArray(emptyVao);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n      gl.enable(gl.DEPTH_TEST);\n      gl.depthMask(true);\n      gl.clear(gl.DEPTH_BUFFER_BIT);\n      shared(progCard, cam, rt, up, fw, shift, t);\n      gl.bindVertexArray(vao);\n      gl.drawArrays(gl.TRIANGLES, 0, mesh.length / 6);\n      gl.bindFramebuffer(gl.READ_FRAMEBUFFER, msFbo);\n      gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, resFbo);\n      gl.blitFramebuffer(0, 0, W, H, 0, 0, W, H, gl.COLOR_BUFFER_BIT, gl.NEAREST);\n      gl.bindFramebuffer(gl.FRAMEBUFFER, lensFbo);\n      gl.disable(gl.DEPTH_TEST);\n      gl.useProgram(progLens);\n      gl.uniform1i(U(progLens, \"uScene\"), 2);\n      gl.uniform2f(U(progLens, \"uPx\"), 1 / W, 1 / H);\n      gl.uniform1f(U(progLens, \"uMaxR\"), H * 0.011);\n      gl.bindVertexArray(emptyVao);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n      gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n      gl.useProgram(progOut);\n      gl.uniform1i(U(progOut, \"uScene\"), 3);\n      gl.uniform2f(U(progOut, \"uPx\"), 1 / W, 1 / H);\n      gl.uniform1f(U(progOut, \"uTime\"), reduce ? 0 : t);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n    };\n\n    const physics = (dt: number, t: number) => {\n      // with nobody steering, the card turns slowly through the softbox so the foil comes alight and fades\n      const wake = clamp((t - HOLD_S) / 3, 0, 1);\n      const idle = (s.inside ? 0.25 : 1) * wake * wake * (3 - 2 * wake);\n      const wantYaw = (s.inside ? s.px * TILT : 0) + idle * 0.15 * Math.sin((t - HOLD_S) * 0.74);\n      const wantPitch = (s.inside ? s.py * TILT * 0.5 : 0) + idle * 0.05 * Math.sin((t - HOLD_S) * 0.53);\n      s.vyaw += (SPRING * (wantYaw - s.yaw) - DAMPING * s.vyaw) * dt;\n      s.yaw += s.vyaw * dt;\n      s.vpitch += (SPRING * (wantPitch - s.pitch) - DAMPING * s.vpitch) * dt;\n      s.pitch += s.vpitch * dt;\n      s.vflip += (FLIP_SPRING * (s.goal - s.flip) - FLIP_DAMPING * s.vflip) * dt;\n      s.flip += s.vflip * dt;\n      s.press += (s.pressGoal - s.press) * Math.min(1, dt * 18);\n    };\n\n    let raf = 0, visible = true, last = performance.now(), slow = 0, frames = 0;\n    s.t0 = last;\n    const tick = (now: number) => {\n      const real = now - last;\n      const dt = Math.min(0.05, real / 1000);\n      last = now;\n      const t = (now - s.t0) / 1000;\n      if (reduce) {\n        if (s.flip !== s.goal) s.dirty = true;\n        s.flip = s.goal;\n        s.vflip = 0;\n      } else {\n        const sub = Math.max(1, Math.ceil(dt / 0.006));\n        for (let i = 0; i < sub; i++) physics(dt / sub, t);\n      }\n      if (!reduce || s.dirty) {\n        draw(reduce ? 0 : t);\n        s.dirty = false;\n        // measure, do not guess: if frames run long, render fewer pixels\n        if (!reduce && ++frames > 20) {\n          slow = slow * 0.92 + (real > 24 ? 1 : 0) * 0.08;\n          if (slow > 0.6 && scale > 0.6) {\n            scale *= 0.85;\n            slow = 0;\n            frames = 0;\n            resize();\n          }\n        }\n      }\n      if (visible) raf = requestAnimationFrame(tick);\n    };\n    const io = new IntersectionObserver(([e]) => {\n      const was = visible;\n      visible = e.isIntersecting;\n      if (visible && !was) {\n        last = performance.now();\n        frames = 0;\n        raf = requestAnimationFrame(tick);\n      }\n    });\n    io.observe(wrap);\n    raf = requestAnimationFrame(tick);\n\n    let first: number[] | null = null;\n    const move = (e: PointerEvent) => {\n      const r = wrap.getBoundingClientRect();\n      s.px = clamp(((e.clientX - r.left) / r.width) * 2 - 1.1, -1, 1);\n      s.py = clamp(((e.clientY - r.top) / r.height) * 2 - 1, -1, 1);\n      // a pointer that is merely resting over the stage does not steer: it has to travel first\n      if (first === null) first = [e.clientX, e.clientY];\n      const moved = Math.hypot(e.clientX - first[0], e.clientY - first[1]) > 6;\n      if (moved) first = [-1e6, -1e6];\n      s.inside = moved && (e.pointerType === \"mouse\" || s.pressGoal > 0);\n    };\n    const down = (e: PointerEvent) => {\n      if (e.button !== 0) return;\n      s.pressGoal = 1;\n      move(e);\n    };\n    const up = () => {\n      if (s.pressGoal > 0) turn();\n      s.pressGoal = 0;\n    };\n    const leave = () => {\n      s.inside = false;\n      s.pressGoal = 0;\n    };\n    wrap.addEventListener(\"pointermove\", move);\n    canvas.addEventListener(\"pointerdown\", down);\n    canvas.addEventListener(\"pointerup\", up);\n    wrap.addEventListener(\"pointerleave\", leave);\n    canvas.addEventListener(\"pointercancel\", leave);\n\n    return () => {\n      alive = false;\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      wrap.removeEventListener(\"pointermove\", move);\n      canvas.removeEventListener(\"pointerdown\", down);\n      canvas.removeEventListener(\"pointerup\", up);\n      wrap.removeEventListener(\"pointerleave\", leave);\n      canvas.removeEventListener(\"pointercancel\", leave);\n      gl.getExtension(\"WEBGL_lose_context\")?.loseContext();\n    };\n  }, [reduce, turn]);\n\n  const onKey = (e: React.KeyboardEvent) => {\n    const s = sim.current;\n    const nudge = (dx: number, dy: number) => {\n      e.preventDefault();\n      s.inside = true;\n      s.px = clamp(s.px + dx, -1, 1);\n      s.py = clamp(s.py + dy, -1, 1);\n    };\n    if (e.key === \"ArrowLeft\") nudge(-0.25, 0);\n    else if (e.key === \"ArrowRight\") nudge(0.25, 0);\n    else if (e.key === \"ArrowUp\") nudge(0, -0.25);\n    else if (e.key === \"ArrowDown\") nudge(0, 0.25);\n    else if ((e.key === \"Enter\" || e.key === \" \") && e.target === e.currentTarget) {\n      e.preventDefault();\n      turn();\n    }\n  };\n\n  const gold = \"linear-gradient(100deg, #7a5a22 0%, #f6dc9a 38%, #b88a3a 62%, #f1d28a 100%)\";\n\n  return (\n    <section className=\"relative overflow-hidden bg-[#050b24]\">\n      <div\n        ref={wrapRef}\n        role=\"group\"\n        aria-label=\"A show ticket printed on thick card with gold foil. Arrow keys tilt it under the light, Enter turns it over.\"\n        tabIndex={0}\n        onKeyDown={onKey}\n        onBlur={() => (sim.current.inside = false)}\n        className=\"relative h-[620px] w-full select-none outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-white/70 sm:h-[780px]\"\n      >\n        <canvas ref={canvasRef} aria-hidden=\"true\" className={`absolute inset-0 h-full w-full cursor-pointer touch-pan-y ${mode === \"still\" ? \"hidden\" : \"\"}`} />\n\n        {/* the same card laid out with plain elements, for browsers without WebGL 2 */}\n        {mode === \"still\" && (\n          <div className=\"absolute inset-0\" style={{ background: \"radial-gradient(52% 44% at 56% 58%, #2a55d6 0%, #12297a 42%, #050b24 100%)\", perspective: \"1600px\" }}>\n            <style>{`@keyframes gilt-edged-show-card-sway { from { transform: translateX(-50%) rotateX(8deg) rotateY(-22deg); } to { transform: translateX(-50%) rotateX(5deg) rotateY(-8deg); } } .gilt-edged-show-card-sway { animation: gilt-edged-show-card-sway 5s ease-in-out infinite alternate; } @media (prefers-reduced-motion: reduce) { .gilt-edged-show-card-sway { animation: none; } }`}</style>\n            <div className=\"gilt-edged-show-card-sway absolute left-1/2 top-[13%] h-[62%] sm:left-[56%]\" style={{ aspectRatio: \"2 / 3\", transform: \"translateX(-50%) rotateX(8deg) rotateY(-16deg)\", transformStyle: \"preserve-3d\" }}>\n              <div aria-hidden=\"true\" className=\"absolute -bottom-[16%] left-[18%] h-[14%] w-full rounded-[50%] bg-black/60 blur-2xl\" />\n              <div className=\"absolute inset-0 flex flex-col rounded-[14px] bg-[#f1ebde] p-[6%] shadow-[5px_0_0_#c9a35a,6px_2px_0_#8a6a2c,0_40px_80px_rgba(0,0,0,0.55)]\">\n                {side === 0 ? (\n                  <>\n                    <img src={card.photo.src} alt={card.photo.alt} width={1000} height={1500} className=\"min-h-0 w-full flex-1 object-cover object-top shadow-[inset_0_0_0_1px_rgba(0,0,0,0.3)]\" style={{ background: card.photo.tone }} />\n                    <p className=\"mt-[5%] bg-clip-text text-[clamp(34px,7vw,64px)] italic leading-none text-transparent\" style={{ fontFamily: SERIF, backgroundImage: gold, letterSpacing: \"-0.03em\" }}>\n                      {card.house}\n                    </p>\n                    <p className=\"mt-[4%] flex justify-between border-t border-[#b18a4c] pt-[3%] text-[8px] font-semibold uppercase tracking-[0.24em] text-[#1d1b18] sm:text-[9px]\" style={{ fontFamily: GROTESK }}>\n                      <span>{card.line}</span>\n                      <span>{card.admit}</span>\n                    </p>\n                  </>\n                ) : (\n                  <div className=\"flex h-full flex-col items-center justify-between border border-[#b18a4c] p-[8%] text-center\">\n                    <p className=\"text-[10px] font-medium uppercase tracking-[0.24em] text-[#8a6a2c]\" style={{ fontFamily: GROTESK }}>{card.number}</p>\n                    <p className=\"bg-clip-text text-[clamp(40px,8vw,72px)] italic leading-none text-transparent\" style={{ fontFamily: SERIF, backgroundImage: gold }}>{card.house}</p>\n                    <dl className=\"grid w-full grid-cols-3 border-y border-[#b18a4c] py-[6%]\">\n                      {card.seat.map((x) => (\n                        <div key={x.label}>\n                          <dt className=\"text-[8px] font-semibold uppercase tracking-[0.24em] text-[#1d1b18]\" style={{ fontFamily: GROTESK }}>{x.label}</dt>\n                          <dd className=\"mt-1 text-[clamp(22px,4vw,38px)] leading-none text-[#8a6a2c]\" style={{ fontFamily: SERIF }}>{x.value}</dd>\n                        </div>\n                      ))}\n                    </dl>\n                  </div>\n                )}\n              </div>\n            </div>\n          </div>\n        )}\n\n        <div className=\"pointer-events-none absolute inset-x-0 top-0 flex items-baseline justify-between p-5 text-white sm:p-9\">\n          <p className=\"text-[26px] italic leading-none sm:text-[30px]\" style={{ fontFamily: SERIF, letterSpacing: \"-0.03em\" }}>\n            {card.house}\n          </p>\n          <p className=\"text-[10px] font-medium uppercase tracking-[0.28em] text-white/70 sm:text-[11px]\" style={{ fontFamily: GROTESK }}>\n            <span className=\"hidden sm:inline\">Move to tilt, press to turn</span>\n            <span className=\"sm:hidden\">Tap to turn</span>\n          </p>\n        </div>\n        <div className=\"pointer-events-none absolute inset-x-0 bottom-0 flex items-end justify-between gap-4 p-5 sm:p-9\">\n          <p aria-live=\"polite\" className=\"text-white\">\n            <span className=\"block text-[11px] font-medium uppercase tracking-[0.28em] text-white/75\" style={{ fontFamily: GROTESK }}>\n              <span className=\"tabular-nums text-white\">{`0${side + 1}`}</span>\n              <span className=\"mx-2 text-white/40\">/</span>\n              <span className=\"tabular-nums\">02</span>\n              <span className=\"ml-4\">{sides[side].tag}</span>\n            </span>\n            <span className=\"mt-2 block text-[40px] italic leading-[0.92] sm:text-[84px]\" style={{ fontFamily: SERIF, letterSpacing: \"-0.04em\" }}>\n              {sides[side].title}\n            </span>\n            <span className=\"sr-only\">{side === 0 ? `. ${card.photo.alt}` : `. ${card.seat.map((x) => `${x.label} ${x.value}`).join(\", \")}`}</span>\n          </p>\n          <button\n            type=\"button\"\n            onClick={turn}\n            aria-pressed={side === 1}\n            className=\"pointer-events-auto inline-flex h-11 items-center gap-2.5 rounded-full border border-white/25 bg-black/30 pl-4 pr-5 text-[12px] font-medium uppercase tracking-[0.2em] text-white backdrop-blur-sm transition hover:bg-white/15 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-white\"\n            style={{ fontFamily: GROTESK }}\n          >\n            <svg aria-hidden=\"true\" width=\"18\" height=\"18\" viewBox=\"0 0 20 20\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n              <path d=\"M4 10a6 6 0 0 1 10.6-3.8M16 10a6 6 0 0 1-10.6 3.8M14.8 3v3.4h-3.4M5.2 17v-3.4h3.4\" />\n            </svg>\n            Turn over\n          </button>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
