{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ray-marched-anodised-aluminium-air-key-on-black-glass-with-real-travel-a-backlit-legend-and-a-light-ring-that-latches-on","type":"registry:block","title":"Ray-Marched Anodised Aluminium Air Key on Black Glass with Real Travel, a Backlit Legend and a Light Ring That Latches On","description":"Free React toggle button rendered as a thick red-orange anodised key in a WebGL shader: it travels, knocks at the bottom, latches low and lights its legend and ring.","dependencies":[],"files":[{"path":"components/we-inc/ray-marched-anodised-aluminium-air-key-on-black-glass-with-real-travel-a-backlit-legend-and-a-light-ring-that-latches-on.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport type { KeyboardEvent as ReactKeyboardEvent } from \"react\";\n\n// What the key says in each state. `legend` is cut through the anodising and lit from behind.\nconst LEGEND = [\"ON\", \"AIR\"];\nconst PLATE = [\"MARLOW\", \"AIR KEY 02\"]; // the two small engravings on the housing\nconst states = {\n  on: { word: \"On air\", note: \"Studio two is live. Keep the door shut.\" },\n  off: { word: \"Off air\", note: \"Mics muted, lamp out, kettle allowed.\" },\n};\nconst START_ON = true;\nconst TRAVEL = 0.24; // how far the key goes down, in scene units (the key is 2.24 wide)\nconst LATCH = 0.085; // how low it sits while it is on\nconst DYE: [number, number, number] = [0.92, 0.085, 0.012]; // anodising colour of the key (linear RGB)\nconst LAMP: [number, number, number] = [1, 0.8, 0.56]; // legend, light ring and foot line (linear RGB)\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';\n\n// Scene sizes shared by the shader, the engraving and the hit area.\nconst HH = 1.78, HB = 0.42, KHALF = 1.12, KTOP = 0.98;\n\nconst VERT = `ATTR vec2 aPos; void main(){ gl_Position = vec4(aPos, 0.0, 1.0); }`;\n\nconst TONE = `\nvec3 curve(vec3 x){ x *= 0.92; vec3 a = x * (2.51 * x + 0.03); vec3 b = x * (2.43 * x + 0.59) + 0.14; return clamp(a / b, 0.0, 1.0); }\n// half of the curve is applied to the brightest channel alone, so a hot orange stays orange and does not bleach to yellow\nvec3 tone(vec3 x){ float pk = max(max(x.r, x.g), max(x.b, 1e-4)); vec3 keep = curve(vec3(pk)) * (x / pk); return pow(mix(curve(x), keep, 0.55), vec3(1.0 / 2.2)); }\n`;\n\n// The photograph: signed-distance geometry, studio softboxes, one traced bounce.\nconst SCENE = `\nprecision highp float;\nuniform vec2 uRes; uniform vec4 uJit; uniform float uSeed; uniform float uDrop; uniform vec2 uTilt; uniform vec3 uLvl;\nuniform vec3 uCam; uniform vec3 uTgt; uniform float uFoc; uniform vec2 uShift; uniform float uFocus;\nuniform float uKey; uniform vec3 uLamp; uniform vec3 uDye; uniform sampler2D uTex; uniform float uDirect;\nconst float HH = ${HH.toFixed(3)}; const float HB = ${HB.toFixed(3)}; const float HR = 0.62;\nconst float SCREW = 1.425; const float WH = 1.31; const float WR = 0.52; const float RY = 0.385;\nconst float KH = ${KHALF.toFixed(3)}; const float KR = 0.36; const float KT = ${KTOP.toFixed(3)}; const float TAPER = 0.055;\nconst vec3 ALU = vec3(0.913, 0.921, 0.925);\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 h11(float p){ p = fract(p * 0.1031); p *= p + 33.33; p *= p + p; return fract(p); }\nfloat n1(float x){ float i = floor(x); float f = fract(x); f = f * f * (3.0 - 2.0 * f); return mix(h11(i), h11(i + 1.0), f); }\n\n// rounded square in plan\nfloat rb2(vec2 p, float h, float r){ vec2 q = abs(p) - (h - r); return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - r; }\n\nfloat sdHouse(vec3 p){\n  float o = rb2(p.xz, HH, HR);\n  float d = max(o, p.y - HB);\n  d = max(d, (o + p.y - HB + 0.07) * 0.7071);            // bright-cut outer chamfer\n  d = max(d, (o - p.y - 0.012) * 0.7071);                // foot bevel\n  float w = rb2(p.xz, WH, WR);\n  d = max(d, -max(w, RY - p.y));                         // the well the light ring sits in\n  d = max(d, (p.y - HB + 0.035 - w) * 0.7071);           // its polished lip\n  d = max(d, -max(abs(p.y - 0.05) - 0.022, -(o + 0.014))); // the slot for the foot line\n  d = max(d, -max(abs(w - 0.2) - 0.011, HB - 0.012 - p.y)); // a fine turned groove round the well\n  // four socket screws, counterbored into the corners\n  vec2 a = abs(p.xz) - SCREW; float r = length(a);\n  d = max(d, -max(r - 0.1, HB - 0.06 - p.y));\n  float head = max(r - 0.086, p.y - HB + 0.016);\n  head = max(head, (r + p.y - HB + 0.016 - 0.074) * 0.7071);\n  a = vec2(a.x * 0.8776 - a.y * 0.4794, a.x * 0.4794 + a.y * 0.8776);\n  head = max(head, -max(max(abs(a.x) * 0.866 + abs(a.y) * 0.5, abs(a.y)) - 0.04, HB - 0.07 - p.y));\n  return min(d, head);\n}\n// The key's own frame: origin at the middle of its top face, rocked a little under the finger.\nvec3 keyLocal(vec3 p){\n  p.y -= KT - uDrop - 0.5;\n  float c = cos(uTilt.x); float s = sin(uTilt.x); p.yz = vec2(p.y * c - p.z * s, p.y * s + p.z * c);\n  c = cos(uTilt.y); s = sin(uTilt.y); p.xy = vec2(p.x * c - p.y * s, p.x * s + p.y * c);\n  p.y -= 0.5;\n  return p;\n}\nfloat sdKey(vec3 p){\n  vec3 q = keyLocal(p);\n  float o = rb2(q.xz, KH, KR) + q.y * TAPER;\n  float d = max(o, q.y);\n  d = max(d, (o + q.y + 0.06) * 0.7071);                 // bright-cut edge\n  vec2 e2 = q.xz * q.xz; float sq = sqrt(sqrt(dot(e2, e2)));\n  d = max(d, q.y + 0.085 * smoothstep(1.02, 0.3, sq));   // the dish a fingertip sits in\n  return max(d, -q.y - 0.95);\n}\nfloat map(vec3 p){ return min(sdHouse(p), sdKey(p)); }\n\nvec2 bound(vec3 ro, vec3 rd){\n  vec3 d = mix(rd, vec3(1e-5), step(abs(rd), vec3(1e-5)));\n  vec3 t0 = (vec3(-HH, 0.0, -HH) - ro) / d; vec3 t1 = (vec3(HH, KT + 0.03, HH) - ro) / d;\n  vec3 a = min(t0, t1); vec3 b = max(t0, t1);\n  return vec2(max(max(a.x, a.y), max(a.z, 0.0)), min(min(b.x, b.y), b.z));\n}\nfloat march(vec3 ro, vec3 rd){\n  vec2 tb = bound(ro, rd); if (tb.x > tb.y) return -1.0;\n  float t = tb.x;\n  for (int i = 0; i < 84; i++) { float d = map(ro + rd * t); if (d < 0.0006 * (1.0 + t * 0.3)) return t; t += d * 0.8; if (t > tb.y) break; }\n  return -1.0;\n}\nfloat marchLite(vec3 ro, vec3 rd){\n  vec2 tb = bound(ro, rd); if (tb.x > tb.y) return -1.0;\n  float t = tb.x + 0.004;\n  for (int i = 0; i < 40; i++) { float d = map(ro + rd * t); if (d < 0.0018) return t; t += d * 0.85; if (t > tb.y) break; }\n  return -1.0;\n}\nvec3 calcN(vec3 p){\n  vec2 e = vec2(0.0011, -0.0011);\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}\nfloat calcAO(vec3 p, vec3 n){\n  float o = 0.0; float s = 1.0;\n  for (int i = 1; i <= 5; i++) { float h = 0.012 + 0.055 * float(i); o += (h - map(p + n * h)) * s; s *= 0.72; }\n  return clamp(1.0 - 2.4 * o, 0.0, 1.0);\n}\nfloat softShadow(vec3 ro, vec3 rd){\n  float res = 1.0; float t = 0.03;\n  for (int i = 0; i < 22; i++) { float h = map(ro + rd * t); res = min(res, 3.6 * h / t); t += clamp(h, 0.035, 0.3); if (res < 0.01 || t > 4.5) break; }\n  return clamp(res, 0.0, 1.0);\n}\n\n// A rectangular softbox: where the ray meets its plane, with edges that soften as the surface gets rougher.\nfloat rectL(vec3 ro, vec3 rd, vec3 c, vec3 n, vec3 u, vec3 v, 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;\n  vec2 d = abs(vec2(dot(q, u), dot(q, v))) - hs;\n  float cloth = 1.0 - 0.28 * clamp(length(vec2(dot(q, u), dot(q, v)) / hs), 0.0, 1.4);\n  return smoothstep(s, -s, d.x) * smoothstep(s, -s, d.y) * cloth;\n}\nvec3 keyPos(){ return vec3(uKey, 7.3, 9.5); }\nvec3 env(vec3 ro, vec3 rd, float rough){\n  float soft = 0.035 + rough * 0.75;\n  vec3 c = mix(vec3(0.02, 0.021, 0.026), vec3(0.12, 0.125, 0.14), smoothstep(-0.1, 1.0, rd.y));\n  // key: a big scrim high behind the set, leaning over it\n  vec3 kn = normalize(vec3(0.0, -0.42, -1.0));\n  c += vec3(1.0, 0.965, 0.93) * 7.5 * rectL(ro, rd, keyPos(), kn, vec3(1.0, 0.0, 0.0), normalize(cross(kn, vec3(1.0, 0.0, 0.0))), vec2(1.9, 1.35), soft);\n  // overhead box that the chamfers pick up\n  c += vec3(0.93, 0.96, 1.0) * 3.4 * rectL(ro, rd, vec3(uKey * 0.8 - 0.4, 7.5, -4.6), vec3(0.0, -1.0, 0.0), vec3(1.0, 0.0, 0.0), vec3(0.0, 0.0, 1.0), vec2(2.6, 1.7), soft);\n  // cool strip, camera left\n  vec3 ln = normalize(vec3(1.0, -0.05, 0.42));\n  c += vec3(0.82, 0.9, 1.0) * 6.0 * rectL(ro, rd, vec3(-7.6, 2.5, -3.4), ln, normalize(cross(vec3(0.0, 1.0, 0.0), ln)), vec3(0.0, 1.0, 0.0), vec2(1.5, 2.5), soft);\n  // warm strip, camera right\n  vec3 rn = normalize(vec3(-1.0, -0.05, 0.3));\n  c += vec3(1.0, 0.84, 0.66) * 3.4 * rectL(ro, rd, vec3(7.8, 2.8, -2.2), rn, normalize(cross(vec3(0.0, 1.0, 0.0), rn)), vec3(0.0, 1.0, 0.0), vec2(0.9, 2.8), soft);\n  // the cyclorama far behind: it is what the glass shows at the top of the frame\n  float cyc = smoothstep(0.03, 0.2, rd.y) * smoothstep(0.52, 0.2, rd.y) * smoothstep(-0.2, 0.75, rd.z) * (0.55 + 0.45 * smoothstep(-0.9, 0.5, rd.x));\n  c += vec3(0.62, 0.72, 0.9) * 0.36 * cyc;\n  // a fill card behind the camera, so faces turned toward the lens are not left black\n  c += vec3(0.9, 0.93, 1.0) * 0.9 * rectL(ro, rd, vec3(2.0, 3.4, -11.0), vec3(0.0, 0.0, 1.0), vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec2(4.5, 2.6), soft + 0.1);\n  return c;\n}\n// Below the horizon the room is the glass table, which mirrors the lights dimly.\nvec3 envFloor(vec3 ro, vec3 rd, float rough){\n  if (rd.y >= 0.0) return env(ro, rd, rough);\n  float t = -ro.y / rd.y; vec3 gp = ro + rd * t; vec3 r = vec3(rd.x, -rd.y, rd.z);\n  float f = 0.05 + 0.95 * pow(1.0 - clamp(-rd.y, 0.0, 1.0), 5.0);\n  return vec3(0.004, 0.0045, 0.006) + f * env(gp, r, rough + 0.03);\n}\n\n// kind 0: polished, traced. kind 1: glossy plastic. kind 2: bead-blasted metal, traced through a wide lobe.\nstruct Mat { vec3 alb; vec3 f0; float rough; vec3 emi; float kind; float wide; };\n\nvec3 gnTop(vec3 n){ return n; }\nMat getMat(vec3 p, inout vec3 n){\n  Mat m; m.alb = vec3(0.0); m.f0 = ALU; m.rough = 0.045; m.emi = vec3(0.0); m.kind = 0.0; m.wide = 0.03;\n  vec3 grain = vec3(n2(p.xz * 210.0 + p.y * 83.0), n2(p.zy * 210.0 + p.x * 71.0 + 5.0), n2(p.yx * 210.0 + p.z * 67.0 + 11.0)) - 0.5;\n  if (sdKey(p) < sdHouse(p)) {\n    vec3 q = keyLocal(p);\n    float o = rb2(q.xz, KH, KR) + q.y * TAPER;\n    if (o + q.y + 0.06 > -0.007 && q.y > -0.085) {\n      // the edge is cut after anodising, so it is bare bright metal with the cutter's fine lines\n      n = normalize(n + vec3(0.0, 1.0, 0.0) * (n1((o - q.y) * 260.0) - 0.5) * 0.012);\n      return m;\n    }\n    m.f0 = uDye; m.alb = uDye * 0.05; m.rough = 0.17; m.kind = 2.0; m.wide = 0.05;\n    n = normalize(n + grain * 0.05);\n    if (q.y > -0.09 && o < -0.05) {\n      vec2 uv = vec2(q.x, -q.z) / (2.0 * HH) + 0.5; float e = 1.0 / 1024.0;\n      float ink = texture2D(uTex, uv).r;\n      float ix = texture2D(uTex, uv + vec2(e, 0.0)).r - texture2D(uTex, uv - vec2(e, 0.0)).r;\n      float iy = texture2D(uTex, uv + vec2(0.0, e)).r - texture2D(uTex, uv - vec2(0.0, e)).r;\n      n = normalize(n + vec3(ix, 0.0, -iy) * 0.7);\n      // the legend is an opal insert: milky when the lamp is out, burning when it is on\n      m.alb = mix(m.alb, vec3(0.3, 0.285, 0.26), ink); m.f0 = mix(m.f0, vec3(0.045), ink); m.rough = mix(m.rough, 0.2, ink);\n      m.emi = uLamp * ink * (0.012 + 3.4 * uLvl.x);\n    }\n    return m;\n  }\n  float o = rb2(p.xz, HH, HR); float w = rb2(p.xz, WH, WR);\n  if (w < 0.003 && p.y < RY + 0.004) {\n    // the light ring: opal acrylic, with the dark slot the key runs in\n    float gap = rb2(p.xz, KH, KR) - (KT - uDrop - RY) * TAPER;\n    float g = smoothstep(0.035, 0.065, gap);\n    float edge = smoothstep(0.0, -0.035, w);\n    m.alb = vec3(0.56, 0.54, 0.5) * g; m.f0 = vec3(0.045); m.rough = 0.1; m.kind = 1.0;\n    m.emi = uLamp * g * (0.01 + 2.3 * uLvl.y) * (0.62 + 0.38 * edge);\n    return m;\n  }\n  if (abs(p.y - 0.05) < 0.026 && o < -0.006 && o > -0.03) {\n    // the foot line: a strip of the same acrylic let into the base\n    m.alb = vec3(0.4, 0.39, 0.36); m.f0 = vec3(0.045); m.rough = 0.2; m.kind = 1.0;\n    m.emi = uLamp * (0.006 + 4.4 * uLvl.z);\n    return m;\n  }\n  float sr = length(abs(p.xz) - SCREW);\n  if (sr < 0.092 && p.y < HB - 0.008) {\n    // black oxide steel\n    m.f0 = vec3(0.22, 0.225, 0.24); m.alb = vec3(0.012); m.rough = 0.2; m.kind = 2.0; m.wide = 0.06;\n    n = normalize(n + grain * 0.05);\n    return m;\n  }\n  bool outer = o + p.y - HB + 0.07 > -0.007 && p.y > HB - 0.085;\n  bool lip = p.y - HB + 0.035 - w > -0.007 && w < 0.045;\n  bool foot = o - p.y - 0.012 > -0.005 && p.y < 0.02;\n  if (outer || lip || foot) {\n    n = normalize(n + vec3(0.0, 1.0, 0.0) * (n1((o - p.y) * 260.0 + w * 120.0) - 0.5) * 0.012);\n    return m;\n  }\n  // bead-blasted and clear anodised\n  m.f0 = ALU * 0.9; m.alb = vec3(0.03); m.rough = 0.24; m.kind = 2.0; m.wide = 0.1;\n  n = normalize(n + grain * 0.06);\n  if (p.y > HB - 0.004) {\n    // the top is brushed along x after blasting: the lights smear toward the lens\n    float st = n1(p.z * 520.0 + n1(p.x * 3.0) * 2.0) - 0.5; float st2 = n1(p.z * 140.0 + 9.0) - 0.5;\n    n = normalize(gnTop(n) + vec3(0.0, 0.0, 1.0) * (st * 0.05 + st2 * 0.03) + grain * 0.02);\n    m.rough = 0.11 + 0.05 * (st2 + 0.5); m.kind = 3.0; m.wide = 0.04;\n    vec2 uv = vec2(p.x, -p.z) / (2.0 * HH) + 0.5; float e = 1.0 / 1024.0;\n    float ink = texture2D(uTex, uv).r;\n    float ix = texture2D(uTex, uv + vec2(e, 0.0)).r - texture2D(uTex, uv - vec2(e, 0.0)).r;\n    float iy = texture2D(uTex, uv + vec2(0.0, e)).r - texture2D(uTex, uv - vec2(0.0, e)).r;\n    n = normalize(n + vec3(ix, 0.0, -iy) * 0.8);\n    m.alb = mix(m.alb, vec3(0.012), ink); m.f0 = mix(m.f0, vec3(0.04), ink); m.rough = mix(m.rough, 0.45, ink); m.wide *= 1.0 - ink;\n  }\n  return m;\n}\n\nvec3 envAniso(vec3 p, vec3 rd, vec3 n, vec3 b, float spread, float rough){\n  vec3 c = vec3(0.0); float w = 0.0;\n  for (int i = -5; i <= 5; i++) { float f = float(i) / 5.0; float g = exp(-2.6 * f * f); c += g * envFloor(p, reflect(rd, normalize(n + b * f * spread)), rough); w += g; }\n  return c / w;\n}\nvec3 diffuseLight(vec3 p, vec3 n, float sh, float ao){\n  vec3 lk = normalize(keyPos() - p);\n  vec3 c = vec3(1.0, 0.965, 0.93) * 1.5 * max(dot(n, lk), 0.0) * sh;\n  c += vec3(0.93, 0.96, 1.0) * 0.5 * max(n.y, 0.0) * ao;\n  c += vec3(0.82, 0.9, 1.0) * 0.55 * max(dot(n, normalize(vec3(-7.6, 2.5, -3.4) - p)), 0.0) * ao;\n  c += vec3(1.0, 0.84, 0.66) * 0.25 * max(dot(n, normalize(vec3(7.8, 2.8, -2.2) - p)), 0.0) * ao;\n  return c + vec3(0.03) * ao;\n}\n// What the lamp does to things near it: the ring lights the key's flanks and the lip, the foot line lights the glass.\nvec3 lampLight(vec3 p, vec3 n){\n  float w = rb2(p.xz, WH - 0.1, WR - 0.1);\n  vec2 g = vec2(rb2(p.xz + vec2(0.01, 0.0), WH - 0.1, WR - 0.1) - w, rb2(p.xz + vec2(0.0, 0.01), WH - 0.1, WR - 0.1) - w) / 0.01;\n  vec3 to = vec3(-g.x * w, RY - p.y, -g.y * w);\n  float d = length(to) + 0.02;\n  float ring = max(dot(n, to / d), 0.0) * 0.07 / (d * d + 0.03) * step(RY - 0.01, p.y);\n  return uLamp * uLvl.y * ring * 1.2;\n}\nvec3 fres(vec3 f0, float c){ return f0 + (1.0 - f0) * pow(1.0 - clamp(c, 0.0, 1.0), 5.0); }\n// What a mirror sees: shading without a further bounce.\nvec3 shadeLite(vec3 p, vec3 n, vec3 rd){\n  Mat m = getMat(p, n);\n  vec3 r = reflect(rd, n);\n  vec3 s = m.kind == 3.0 ? envAniso(p, rd, n, vec3(0.0, 0.0, 1.0), 0.34, m.rough) : envFloor(p, r, m.rough + 0.04);\n  vec3 dl = diffuseLight(p, n, 0.6, 0.8) + lampLight(p, n);\n  return m.emi + m.alb * dl + fres(m.f0, dot(-rd, n)) * (s + m.wide * dl);\n}\nvec3 groundLite(vec3 gp, vec3 rd){\n  vec3 r = vec3(rd.x, -rd.y, rd.z);\n  float f = 0.05 + 0.95 * pow(1.0 - clamp(-rd.y, 0.0, 1.0), 5.0);\n  float occ = smoothstep(0.0, 0.5, map(gp + vec3(0.0, 0.02, 0.0)));\n  return vec3(0.004, 0.0045, 0.006) * occ + f * env(gp, r, 0.05) * (0.25 + 0.75 * occ);\n}\nvec3 trace(vec3 p, vec3 r, float rough){\n  float t = marchLite(p, r);\n  if (t > 0.0) { vec3 hp = p + r * t; vec3 hn = calcN(hp); return shadeLite(hp, hn, r); }\n  if (r.y < 0.0) return groundLite(p + r * (-p.y / r.y), r);\n  return env(p, r, rough);\n}\nvec3 shadeFull(vec3 p, vec3 n, vec3 rd, vec2 rnd){\n  vec3 gn = n;\n  Mat m = getMat(p, n);\n  float ao = calcAO(p, gn);\n  vec3 lk = normalize(keyPos() - p);\n  float sh = softShadow(p + gn * 0.012, lk);\n  vec3 dl = diffuseLight(p, n, sh, ao) + lampLight(p, gn) * (0.4 + 0.6 * ao);\n  vec3 col = m.emi + m.alb * dl;\n  vec3 F = fres(m.f0, dot(-rd, n));\n  vec3 r = reflect(rd, n);\n  vec3 tx = normalize(cross(r, vec3(0.31, 0.9, 0.3))); vec3 ty = cross(r, tx);\n  vec3 jr = normalize(r + (tx * rnd.x + ty * rnd.y) * m.rough * 0.55);\n  if (dot(jr, gn) < 0.02) jr = r;\n  vec3 s;\n  if (m.kind == 1.0) s = envFloor(p, r, m.rough) * (0.35 + 0.65 * ao);\n  else if (m.kind == 3.0) s = mix(envAniso(p, rd, n, vec3(0.0, 0.0, 1.0), 0.34, m.rough), trace(p + gn * 0.006, jr, m.rough), 0.45) * (0.4 + 0.6 * ao);\n  else s = trace(p + gn * 0.006, jr, m.rough) * (0.4 + 0.6 * ao);\n  return col + F * (s + m.wide * dl);\n}\nvec3 shadeGround(vec3 gp, vec3 rd, vec2 rnd, float dist){\n  float near = step(dot(gp.xz, gp.xz), 60.0);\n  vec3 n = normalize(vec3((n2(gp.xz * 0.7) - 0.5) * 0.0012, 1.0, (n2(gp.zx * 0.7 + 5.0) - 0.5) * 0.0012));\n  vec3 r = reflect(rd, n);\n  r = normalize(r + vec3(rnd.x, 0.0, rnd.y) * 0.016);\n  float f = 0.05 + 0.95 * pow(1.0 - clamp(dot(-rd, n), 0.0, 1.0), 5.0);\n  float occ = 1.0; float sh = 1.0;\n  if (near > 0.5) {\n    occ = smoothstep(-0.02, 0.6, map(gp + vec3(0.0, 0.03, 0.0)));\n    sh = softShadow(gp + vec3(0.0, 0.01, 0.0), normalize(keyPos() - gp));\n  }\n  vec3 refl;\n  float t = near > 0.5 ? marchLite(gp + vec3(0.0, 0.002, 0.0), r) : -1.0;\n  if (t > 0.0) { vec3 hp = gp + r * t; vec3 hn = calcN(hp); refl = shadeLite(hp, hn, r); }\n  else refl = env(gp, r, 0.2) * (0.3 + 0.7 * occ);\n  // the glass: nearly no diffuse, a few specks of dust that catch the key light\n  vec3 lk = normalize(keyPos() - gp);\n  vec2 cell = floor(gp.xz * 19.0); float hsp = h21(cell);\n  vec2 cp = (cell + 0.2 + 0.6 * vec2(h21(cell + 3.1), h21(cell + 7.7))) / 19.0;\n  float speck = step(0.992, hsp) * smoothstep(0.004 + 0.009 * h21(cell + 1.3), 0.0, length(gp.xz - cp));\n  float pool = smoothstep(8.5, 0.8, length(gp.xz - vec2(0.4, 0.2)));\n  vec3 alb = vec3(0.04, 0.042, 0.05) * (0.9 + 0.2 * n2(gp.xz * 3.0)) + speck * 0.25;\n  vec3 col = alb * (vec3(1.0, 0.965, 0.93) * 2.2 * max(lk.y, 0.0) * sh * pool * pool + vec3(0.1) * occ) * occ;\n  // the foot line spills a little onto the dust around the base\n  float o = max(rb2(gp.xz, HH, HR), 0.0);\n  col += uLamp * uLvl.z * (alb * 3.0 + 0.01) * 0.9 / (1.0 + o * o * 90.0) * exp(-o * 2.2);\n  col += f * refl * (0.95 + 0.1 * n2(gp.xz * 0.8 + 3.0));\n  col *= mix(1.0, 0.0, smoothstep(14.0, 46.0, dist));\n  return col;\n}\n\nvoid main(){\n  vec2 fc = gl_FragCoord.xy + uJit.xy;\n  vec2 uv = (fc - 0.5 * uRes) / uRes.y - uShift;\n  vec3 fw = normalize(uTgt - uCam); 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 ign = fract(52.9829189 * fract(dot(gl_FragCoord.xy, vec2(0.06711056, 0.00583715))) + uSeed);\n  float ang = ign * 6.2831853; float rr = sqrt(fract(ign * 7.13 + uSeed * 3.7));\n  vec2 rnd = vec2(cos(ang), sin(ang)) * rr * step(0.001, uSeed);\n  float t = march(ro, rd);\n  float tg = rd.y < 0.0 ? -ro.y / rd.y : 1e9;\n  vec3 col;\n  if (t > 0.0 && t < tg) { vec3 p = ro + rd * t; vec3 n = calcN(p); col = shadeFull(p, n, rd, rnd); }\n  else if (tg < 1e8) col = shadeGround(ro + rd * tg, rd, rnd, tg);\n  else col = env(ro, rd, 0.0);\n  col = min(col, vec3(24.0));\n  if (uDirect > 0.5) col = tone(col);\n  outC = vec4(col, 1.0);\n}\n`;\n\n// Developing the negative: bloom off the hot highlights, filmic curve, vignette, grain.\nconst POST = `\nprecision highp float;\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 = texture2D(uAcc, uv).rgb;\n  vec2 px = 1.0 / uRes;\n  vec3 nb = texture2D(uAcc, uv + vec2(px.x, 0.0)).rgb + texture2D(uAcc, uv - vec2(px.x, 0.0)).rgb + texture2D(uAcc, uv + vec2(0.0, px.y)).rgb + texture2D(uAcc, uv - vec2(0.0, px.y)).rgb;\n  c = max(c + (c - nb * 0.25) * 0.28, 0.0);\n  vec3 b = vec3(0.0);\n  // the spiral is turned by a different amount at every pixel, so the glow is a haze and never a row of ghosts\n  float turn = h21(gl_FragCoord.xy + uGrain * 13.1) * 6.2831853;\n  for (int i = 0; i < 28; i++) {\n    float a = float(i) * 2.39996 + turn; float r = (float(i) + 0.5) / 28.0;\n    vec3 s = texture2D(uAcc, uv + vec2(cos(a), sin(a)) * r * 0.034 * vec2(uRes.y / uRes.x, 1.0)).rgb;\n    b += min(max(s - 1.1, 0.0), vec3(5.0)) * (1.0 - 0.75 * r);\n  }\n  c += b * 0.0085;\n  vec2 q = (uv - uCentre) * vec2(uRes.x / uRes.y, 1.0);\n  c *= 1.0 - 0.42 * smoothstep(0.25, 1.15, length(q));\n  c = tone(c);\n  float g = h21(gl_FragCoord.xy + uGrain * 91.7) - 0.5;\n  c += g * 0.022 * (1.0 - 0.5 * c.g);\n  outC = vec4(c, 1.0);\n}\n`;\n\n// The engraving, drawn once as a top-down plan of the housing: the legend in the middle, two small marks on the rim.\nfunction engraving() {\n  const S = 2048;\n  const cv = document.createElement(\"canvas\");\n  cv.width = S; cv.height = S;\n  const g = cv.getContext(\"2d\");\n  if (!g) return cv;\n  const px = S / (2 * HH);\n  g.fillStyle = \"#000\"; g.fillRect(0, 0, S, S);\n  g.translate(S / 2, S / 2);\n  g.fillStyle = \"#fff\"; g.textAlign = \"center\"; g.textBaseline = \"alphabetic\";\n  const spaced = (text: string, x: number, y: number, size: number, weight: number, track: number, align: \"center\" | \"left\" | \"right\") => {\n    g.font = `${weight} ${size * px}px ${FONT}`;\n    const chars = text.split(\"\");\n    const widths = chars.map((c) => g.measureText(c).width + track * size * px);\n    const total = widths.reduce((a, b) => a + b, 0) - track * size * px;\n    let at = align === \"center\" ? x * px - total / 2 : align === \"left\" ? x * px : x * px - total;\n    g.textAlign = \"left\";\n    chars.forEach((c, i) => { g.fillText(c, at, y * px); at += widths[i]; });\n  };\n  const size = LEGEND.length > 1 ? 0.5 : 0.56, lead = size * 0.98;\n  LEGEND.forEach((line, i) => spaced(line, 0, (i - (LEGEND.length - 1) / 2) * lead + size * 0.36, size, 800, 0.02, \"center\"));\n  spaced(PLATE[0], -1.18, 1.6, 0.092, 700, 0.2, \"left\");\n  spaced(PLATE[1], 1.18, 1.6, 0.092, 700, 0.2, \"right\");\n  return cv;\n}\n\n// No WebGL: the same pose built from stacked CSS slices in a tilted plane.\nfunction Still({ on, down }: { on: boolean; down: boolean }) {\n  const lamp = on ? 1 : 0;\n  const rise = down ? 26 : on ? 44 : 54;\n  const slab = (n: number, from: number, step: number, size: number, radius: number, bg: string) =>\n    Array.from({ length: n }, (_, i) => (\n      <span key={i} className=\"absolute left-1/2 top-1/2 block\" style={{ width: size, height: size, margin: -size / 2, borderRadius: radius, background: bg, transform: `translateZ(${from + i * step}px)` }} />\n    ));\n  return (\n    <div aria-hidden=\"true\" className=\"absolute left-1/2 top-[34%] h-[420px] w-[520px] -translate-x-1/2 -translate-y-1/2 scale-[0.6] sm:scale-90 lg:left-[66.5%] lg:top-[52%] lg:scale-[1.12]\" style={{ perspective: 1700 }}>\n      <div className=\"absolute left-1/2 top-1/2\" style={{ transformStyle: \"preserve-3d\", transform: \"rotateX(63deg) rotateZ(27deg)\" }}>\n        <span className=\"absolute left-1/2 top-1/2 block rounded-[74px] blur-2xl\" style={{ width: 440, height: 440, margin: -220, background: `rgba(255,190,120,${0.12 + lamp * 0.3})`, transform: \"translateZ(-2px)\" }} />\n        <span className=\"absolute left-1/2 top-1/2 block rounded-[66px] blur-md\" style={{ width: 372, height: 372, margin: -186, background: \"#000\", boxShadow: \"0 0 60px 24px rgba(0,0,0,0.85)\" }} />\n        {slab(2, 1, 2, 356, 62, \"#1b1c20\")}\n        {slab(2, 5, 2, 354, 61, on ? \"#ffe6c4\" : \"#3a3835\")}\n        {slab(11, 9, 3, 356, 62, \"linear-gradient(100deg,#2d3036,#8f959d 30%,#55595f 62%,#1c1d21)\")}\n        <span className=\"absolute left-1/2 top-1/2 block rounded-[62px]\" style={{ width: 356, height: 356, margin: -178, transform: \"translateZ(41px)\", background: \"repeating-linear-gradient(90deg, rgba(255,255,255,0.05) 0 1px, rgba(0,0,0,0.04) 1px 3px), linear-gradient(135deg,#e3e6ea 0%,#a9aeb5 34%,#7c8188 62%,#c3c8ce 100%)\", boxShadow: \"inset 0 0 0 5px rgba(255,255,255,0.8), inset 0 0 0 6px rgba(0,0,0,0.25)\" }}>\n          {[[26, 26], [26, 312], [312, 26], [312, 312]].map(([x, y]) => (\n            <span key={`${x}-${y}`} className=\"absolute block h-[18px] w-[18px] rounded-full\" style={{ left: x, top: y, background: \"radial-gradient(circle, #050506 0 28%, #3b3d42 30% 62%, #0c0c0e 66%)\", boxShadow: \"0 1px 0 rgba(255,255,255,0.6)\" }} />\n          ))}\n          <span className=\"absolute left-1/2 top-1/2 block rounded-[60px]\" style={{ width: 290, height: 290, margin: -145, boxShadow: \"inset 0 0 0 1.5px rgba(0,0,0,0.4)\" }} />\n          <span className=\"absolute bottom-[14px] left-[58px] text-[9px] font-semibold uppercase tracking-[0.24em] text-black/75\">{PLATE[0]}</span>\n          <span className=\"absolute bottom-[14px] right-[58px] text-[9px] font-semibold uppercase tracking-[0.24em] text-black/75\">{PLATE[1]}</span>\n          <span className=\"absolute left-1/2 top-1/2 block rounded-[52px]\" style={{ width: 262, height: 262, margin: -131, background: on ? \"#ffe2bd\" : \"#6f6b64\", boxShadow: `0 0 0 3px rgba(255,255,255,0.8), 0 0 ${lamp * 46}px ${lamp * 12}px rgba(255,176,96,0.8), inset 0 0 0 20px rgba(0,0,0,${on ? 0.05 : 0.5})` }} />\n        </span>\n        {slab(Math.max(1, Math.round((rise - 2) / 3)), 43, 3, 226, 37, \"linear-gradient(100deg,#5a1404,#e8480f 34%,#a92a07 66%,#3a0c02)\")}\n        <span className=\"absolute left-1/2 top-1/2 flex flex-col items-center justify-center rounded-[36px]\" style={{ width: 224, height: 224, margin: -112, transform: `translateZ(${41 + rise}px)`, background: \"radial-gradient(circle at 42% 30%, #ff7a3a 0%, #e4440d 46%, #b82f08 100%)\", boxShadow: \"inset 0 0 0 4px rgba(255,255,255,0.85), inset 0 0 0 5px rgba(0,0,0,0.2)\" }}>\n          {LEGEND.map((line) => (\n            <span key={line} className=\"block text-[50px] font-extrabold leading-[0.98] tracking-[0.02em]\" style={{ color: on ? \"#fff6e6\" : \"#b9aa97\", textShadow: on ? \"0 0 18px rgba(255,214,160,0.95), 0 0 4px #fff\" : \"none\" }}>{line}</span>\n          ))}\n        </span>\n      </div>\n    </div>\n  );\n}\n\nexport default function RayMarchedAirKey() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const key = useRef<HTMLButtonElement>(null);\n  const ring = useRef<SVGGElement>(null);\n  const [focused, setFocused] = useState(false);\n  const [on, setOn] = useState(START_ON);\n  const [down, setDown] = useState(false);\n  const [shown, setShown] = useState<\"wait\" | \"gl\" | \"still\">(\"wait\");\n  const api = useRef<{ hold: (h: boolean, x?: number, y?: number) => void; set: (v: boolean) => void; touch: () => void } | null>(null);\n  const flip = useRef<() => void>(() => {});\n  flip.current = () => setOn((v) => !v);\n\n  useEffect(() => { api.current?.set(on); }, [on]);\n\n  useEffect(() => {\n    const box = stage.current, cv = canvas.current, btn = key.current, halo = ring.current;\n    if (!box || !cv || !btn || !halo) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const opts = { antialias: false, alpha: false, powerPreference: \"high-performance\" as const };\n    const gl2 = cv.getContext(\"webgl2\", opts) as WebGL2RenderingContext | null;\n    const gl = (gl2 ?? cv.getContext(\"webgl\", opts)) as WebGLRenderingContext | null;\n    if (!gl) { setShown(\"still\"); return; }\n    const canAcc = !!gl2 && (!!gl2.getExtension(\"EXT_color_buffer_float\") || !!gl2.getExtension(\"EXT_color_buffer_half_float\"));\n    const info = gl.getExtension(\"WEBGL_debug_renderer_info\");\n    const renderer = info ? String(gl.getParameter(info.UNMASKED_RENDERER_WEBGL)) : \"\";\n    const software = /swiftshader|llvmpipe|software|basic render/i.test(renderer);\n    const still = reduce || software; // no idle drift: draw, refine, then rest\n\n    const head = gl2 ? \"#version 300 es\\n#define ATTR in\\n#define texture2D texture\\n\" : \"#define ATTR attribute\\n\";\n    const fhead = gl2 ? \"#version 300 es\\n#define texture2D texture\\nprecision highp float;\\nout vec4 outC;\\n\" : \"#define outC gl_FragColor\\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      return gl.getShaderParameter(sh, gl.COMPILE_STATUS) ? sh : null;\n    };\n    const link = (frag: string) => {\n      const v = compile(gl.VERTEX_SHADER, head + VERT), f = compile(gl.FRAGMENT_SHADER, fhead + 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 U = (p: WebGLProgram, n: string) => gl.getUniformLocation(p, n);\n    const su = Object.fromEntries([\"uRes\", \"uJit\", \"uSeed\", \"uDrop\", \"uTilt\", \"uLvl\", \"uCam\", \"uTgt\", \"uFoc\", \"uShift\", \"uFocus\", \"uKey\", \"uLamp\", \"uDye\", \"uTex\", \"uDirect\"].map((n) => [n, U(scene, n)]));\n    const pu = post ? Object.fromEntries([\"uAcc\", \"uRes\", \"uGrain\", \"uCentre\"].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); gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n\n    const tex = gl.createTexture();\n    gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, tex);\n    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, engraving());\n    gl.generateMipmap(gl.TEXTURE_2D);\n    const aniso = gl.getExtension(\"EXT_texture_filter_anisotropic\");\n    if (aniso) gl.texParameterf(gl.TEXTURE_2D, aniso.TEXTURE_MAX_ANISOTROPY_EXT, 8);\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\n    // The accumulation plate: every frame is one more exposure blended onto it.\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, perDrop = 0, haloY = -1;\n\n    const s = {\n      y: START_ON ? LATCH : 0, vy: 0, held: false, on: START_ON, lv: START_ON ? [1, 1, 1] : [0, 0, 0], fl: [0, 0, 0], onAt: 0, armed: [false, false, false],\n      tx: 0, tz: 0, wx: 0, wz: 0, kick: 0, vk: 0, px: 0, py: 0, sx: 0, sy: 0, key: 3.6,\n      count: 0, fresh: true, frame: 0, slow: 0, visible: true, raf: 0, last: 0, t0: 0,\n      touched: false, demoAt: 0, demoUp: 0, first: true,\n    };\n\n    // The camera, shared by the shader and by the hit area that has to sit over the key.\n    const view = (sx: number, sy: number, kick: number) => {\n      const wide = cssW >= 1024, aspect = cssW / cssH;\n      const el = 0.42, az = -0.5 + sx * 0.07, dist = 9;\n      const tgt = [0, 0.42 - kick, 0];\n      const cam = [tgt[0] + dist * Math.sin(az) * Math.cos(el), 0.42 + dist * Math.sin(el) - sy * 0.25 - kick, tgt[2] - dist * Math.cos(az) * Math.cos(el)];\n      const foc = 1.96 * Math.min(1, aspect / 1.04);\n      const shift = [wide ? 0.142 * aspect : 0, wide ? 0.02 : 0.15];\n      return { wide, cam, tgt, foc, shift };\n    };\n    const place = () => {\n      const v = view(0, 0, 0);\n      const sub = (a: number[], b: number[]) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];\n      const dot = (a: number[], b: number[]) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];\n      const norm = (a: number[]) => { const l = Math.hypot(a[0], a[1], a[2]); return [a[0] / l, a[1] / l, a[2] / l]; };\n      const cross = (a: number[], b: number[]) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];\n      const fw = norm(sub(v.tgt, v.cam)), rt = norm(cross([0, 1, 0], fw)), up = cross(fw, rt);\n      let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9;\n      for (const cx of [-1, 1]) for (const cz of [-1, 1]) for (const cy of [HB, KTOP]) {\n        const d = sub([cx * KHALF * 0.93, cy, cz * KHALF * 0.93], v.cam), z = dot(d, fw);\n        const ux = (dot(d, rt) / z) * v.foc + v.shift[0], uy = (dot(d, up) / z) * v.foc + v.shift[1];\n        const X = cssW / 2 + ux * cssH, Y = cssH / 2 - uy * cssH;\n        x0 = Math.min(x0, X); x1 = Math.max(x1, X); y0 = Math.min(y0, Y); y1 = Math.max(y1, Y);\n      }\n      // the focus ring: the outline of the key's top face, seen through the same lens\n      const pts: string[] = [];\n      const h = KHALF + 0.09, rr = 0.45;\n      for (let i = 0; i < 64; i++) {\n        const a = (i / 64) * Math.PI * 2, cx = Math.cos(a), cz = Math.sin(a);\n        const k = 1 / Math.pow(Math.pow(Math.abs(cx), 2 / rr) + Math.pow(Math.abs(cz), 2 / rr), rr / 2);\n        const d = sub([cx * k * h, KTOP, cz * k * h], v.cam), z = dot(d, fw);\n        pts.push(`${(cssW / 2 + ((dot(d, rt) / z) * v.foc + v.shift[0]) * cssH).toFixed(1)},${(cssH / 2 - ((dot(d, up) / z) * v.foc + v.shift[1]) * cssH).toFixed(1)}`);\n      }\n      halo.querySelectorAll(\"polygon\").forEach((p) => p.setAttribute(\"points\", pts.join(\" \")));\n      perDrop = (up[1] * v.foc * cssH) / dot(sub([0, KTOP, 0], v.cam), fw);\n      btn.style.left = `${x0}px`; btn.style.top = `${y0}px`; btn.style.width = `${x1 - x0}px`; btn.style.height = `${y1 - y0}px`;\n    };\n\n    const size = () => {\n      const r = box.getBoundingClientRect();\n      cssW = Math.max(1, r.width); cssH = Math.max(1, r.height);\n      place();\n      const dpr = Math.min(software ? 1 : 1.5, window.devicePixelRatio || 1) * scale;\n      // never more than about a million and a half pixels of ray marching\n      const k = Math.min(1, Math.sqrt(1.45e6 / (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 (gl2 && acc && fbo) {\n        gl.activeTexture(gl.TEXTURE1); gl.bindTexture(gl.TEXTURE_2D, acc);\n        gl2.texImage2D(gl.TEXTURE_2D, 0, gl2.RGBA16F, W, H, 0, gl.RGBA, gl2.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    const draw = (alpha: number, sample: number) => {\n      const v = view(s.sx, s.sy, s.kick);\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.02 : 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.5 : 0);\n      gl.uniform1f(su.uDrop, s.y); gl.uniform2f(su.uTilt, s.tx, s.tz);\n      gl.uniform3f(su.uLvl, s.lv[0] * (1 + 0.8 * s.fl[0]), s.lv[1] * (1 + 0.8 * s.fl[1]), s.lv[2] * (1 + 0.8 * s.fl[2]));\n      gl.uniform3f(su.uCam, v.cam[0], v.cam[1], v.cam[2]); gl.uniform3f(su.uTgt, v.tgt[0], v.tgt[1], v.tgt[2]);\n      gl.uniform1f(su.uFoc, v.foc);\n      gl.uniform2f(su.uShift, v.shift[0], v.shift[1]);\n      gl.uniform1f(su.uFocus, 8.8);\n      gl.uniform1f(su.uKey, s.key);\n      gl.uniform3f(su.uLamp, LAMP[0], LAMP[1], LAMP[2]); gl.uniform3f(su.uDye, DYE[0], DYE[1], DYE[2]);\n      gl.uniform1i(su.uTex, 0);\n      gl.uniform1f(su.uDirect, canAcc ? 0 : 1);\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, still ? 1 : s.frame % 64);\n        gl.uniform2f(pu.uCentre, v.wide ? 0.5 + 0.17 : 0.5, v.wide ? 0.5 : 0.62);\n        gl.drawArrays(gl.TRIANGLES, 0, 3);\n      }\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\n      // the self-running demo: it opens lit, holds, then presses itself until someone touches it\n      if (!still && !s.touched) {\n        if (!s.held && now > s.demoAt) { s.held = true; s.wx = 0.012; s.wz = -0.01; s.demoUp = now + 190; }\n        else if (s.held && now > s.demoUp) { s.held = false; s.wx = 0; s.wz = 0; flip.current(); s.demoAt = now + (s.on ? 2300 : 3400); }\n      }\n\n      let moving = false;\n      if (reduce) {\n        const rest = s.held ? TRAVEL : s.on ? LATCH : 0;\n        const lv = s.on ? 1 : 0;\n        if (s.y !== rest || s.lv[0] !== lv) { s.y = rest; s.lv = [lv, lv, lv]; s.count = 0; s.fresh = true; }\n      } else {\n        // a sprung plunger with hard stops: it bottoms out with a knock and comes back up past its rest\n        const rest = s.held ? TRAVEL + 0.03 : s.on ? LATCH : 0;\n        const stiff = s.held ? 2100 : 640, damp = s.held ? 38 : 15;\n        const steps = 4, h = dt / steps;\n        for (let i = 0; i < steps; i++) {\n          s.vy += (stiff * (rest - s.y) - damp * s.vy) * h; s.y += s.vy * h;\n          if (s.y > TRAVEL) { s.y = TRAVEL; if (s.vy > 0.25) s.vk += s.vy * 0.022; s.vy *= -0.16; }\n          if (s.y < 0) { s.y = 0; s.vy *= -0.3; }\n          s.vk += (-900 * s.kick - 34 * s.vk) * h; s.kick += s.vk * h;\n        }\n        if (Math.abs(s.vy) > 0.002 || Math.abs(rest - s.y) > 0.0006 && !(s.held && s.y >= TRAVEL) || Math.abs(s.kick) > 0.00005) moving = true;\n        else if (!s.held) { s.y = rest; s.vy = 0; }\n        // the key rocks toward the finger\n        const kt = 1 - Math.exp(-dt * 22);\n        if (Math.abs(s.wx - s.tx) + Math.abs(s.wz - s.tz) > 0.0002) { s.tx += (s.wx - s.tx) * kt; s.tz += (s.wz - s.tz) * kt; moving = true; }\n        // the lamp: it strikes hard, overshoots and settles; when cut it fades like a filament\n        // and it travels: legend first, then the ring, then the line round the foot\n        for (let i = 0; i < 3; i++) {\n          const lit = s.on && now >= s.onAt + i * 75;\n          if (lit && s.armed[i]) { s.armed[i] = false; s.fl[i] = 1; }\n          const want = lit ? 1 : 0, dl = want - s.lv[i];\n          if (Math.abs(dl) > 0.002) { s.lv[i] += dl * (1 - Math.exp(-dt * (lit ? 20 : 9 - i * 2))); moving = true; } else s.lv[i] = want;\n          if (s.fl[i] > 0.004) { s.fl[i] *= Math.exp(-dt * 6); moving = true; } else s.fl[i] = 0;\n          if (s.on && !lit) moving = true;\n        }\n      }\n      if (!still) {\n        s.sx += (s.px - s.sx) * (1 - Math.exp(-dt * 3)); s.sy += (s.py - s.sy) * (1 - Math.exp(-dt * 3));\n        const t = (now - s.t0) / 1000;\n        s.key = 3.6 + Math.sin(t * 0.42 + 0.6) * 1.2 + s.sx * 0.5;\n        moving = true;\n      }\n\n      const fast = Math.abs(s.vy) > 0.1 || Math.abs(s.vk) > 0.02 || s.fl[0] + s.fl[2] > 0.2;\n      if (s.fresh || !canAcc) { draw(1, 0); s.fresh = false; s.count = 1; }\n      else if (moving) { draw(fast ? 0.85 : still ? 0.5 : 0.085, 1 + (s.frame % 64)); s.count = still ? 1 : 6; }\n      else if (s.count < 40) { s.count++; draw(1 / s.count, s.count); }\n      if (s.first) { s.first = false; setShown(\"gl\"); }\n      if (s.y !== haloY) { haloY = s.y; halo.style.transform = `translateY(${(s.y * perDrop).toFixed(2)}px)`; }\n\n      // measured, not guessed: if frames run long for a while, give up some resolution\n      if (!still && dtRaw > 0.026) s.slow++; else s.slow = Math.max(0, s.slow - 2);\n      if (s.slow > 45 && scale > 0.5) { scale *= 0.82; s.slow = 0; size(); }\n\n      if (moving || s.count < 40 || (!still && !s.touched)) s.raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!s.raf && s.visible) { s.last = 0; s.raf = requestAnimationFrame(frame); } };\n\n    api.current = {\n      touch: () => { if (!s.touched) { s.touched = true; if (s.held) { s.held = false; } } },\n      hold: (h, x, y) => {\n        s.held = h;\n        if (h && x !== undefined && y !== undefined) { s.wx = -y * 0.03; s.wz = -x * 0.034; } else { s.wx = 0; s.wz = 0; }\n        wake();\n      },\n      set: (v) => {\n        if (v === s.on) return;\n        s.on = v;\n        if (v) { s.onAt = performance.now(); s.armed = [true, true, true]; }\n        wake();\n      },\n    };\n\n    size();\n    const ro = new ResizeObserver(() => { size(); 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 (!still) wake();\n    };\n    const leave = () => { s.px = 0; s.py = 0; };\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\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);\n      api.current = null;\n    };\n  }, []);\n\n  const hold = (h: boolean, x?: number, y?: number) => { setDown(h); api.current?.touch(); api.current?.hold(h, x, y); };\n  const onKeyDown = (e: ReactKeyboardEvent<HTMLButtonElement>) => {\n    if (e.key !== \" \" && e.key !== \"Enter\") return;\n    if (e.repeat) { e.preventDefault(); return; }\n    hold(true);\n  };\n  const onKeyUp = (e: ReactKeyboardEvent<HTMLButtonElement>) => { if (e.key === \" \" || e.key === \"Enter\") hold(false); };\n\n  const st = on ? states.on : states.off;\n  const still = shown === \"still\";\n\n  return (\n    <section className=\"relative isolate overflow-hidden bg-[#050608] text-white\" style={{ fontFamily: FONT }}>\n      <style>{`\n        @keyframes ray-marched-air-key-rise { from { opacity: 0; transform: translateY(0.16em); filter: blur(7px); } to { opacity: 1; transform: none; filter: blur(0); } }\n        .ray-marched-air-key-word { animation: ray-marched-air-key-rise 480ms cubic-bezier(0.2, 0.9, 0.2, 1) both; }\n        @keyframes ray-marched-air-key-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }\n        .ray-marched-air-key-dot { animation: ray-marched-air-key-pulse 1.6s ease-in-out infinite; }\n        @media (prefers-reduced-motion: reduce) { .ray-marched-air-key-word, .ray-marched-air-key-dot { animation: none; } }\n      `}</style>\n      <div ref={stage} className=\"relative mx-auto h-[720px] w-full max-w-[1600px] lg:h-[760px]\" style={{ background: \"radial-gradient(90% 70% at 62% 30%, #15171c 0%, #090a0d 55%, #050608 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\n        {still && <Still on={on} down={down} />}\n\n        <button\n          ref={key}\n          type=\"button\"\n          aria-pressed={on}\n          aria-label=\"On air\"\n          onClick={() => { api.current?.touch(); setOn((v) => !v); }}\n          onPointerDown={(e) => {\n            const r = e.currentTarget.getBoundingClientRect();\n            hold(true, ((e.clientX - r.left) / r.width) * 2 - 1, ((e.clientY - r.top) / r.height) * 2 - 1);\n          }}\n          onPointerUp={() => hold(false)}\n          onPointerLeave={() => { if (down) hold(false); }}\n          onPointerCancel={() => hold(false)}\n          onKeyDown={onKeyDown}\n          onKeyUp={onKeyUp}\n          onFocus={(e) => setFocused(e.currentTarget.matches(\":focus-visible\"))}\n          onBlur={() => { setFocused(false); if (down) hold(false); }}\n          className={`absolute cursor-pointer touch-manipulation select-none rounded-[22%] bg-transparent outline-none ${still ? \"focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-offset-4 focus-visible:ring-offset-black left-1/2 top-[34%] h-[220px] w-[240px] -translate-x-1/2 -translate-y-1/2 lg:left-[66.5%] lg:top-[50%] lg:h-[300px] lg:w-[330px]\" : \"\"}`}\n          style={{ WebkitTapHighlightColor: \"transparent\" }}\n        />\n\n        <svg aria-hidden=\"true\" className={`pointer-events-none absolute inset-0 h-full w-full transition-opacity duration-150 ${focused && !still ? \"opacity-100\" : \"opacity-0\"}`}>\n          <g ref={ring} fill=\"none\" strokeLinejoin=\"round\">\n            <polygon stroke=\"rgba(0,0,0,0.55)\" strokeWidth=\"7\" />\n            <polygon stroke=\"#ffffff\" strokeWidth=\"2.5\" />\n          </g>\n        </svg>\n\n        <div className=\"pointer-events-none absolute inset-0 flex flex-col justify-end px-6 pb-8 sm:px-10 lg:justify-between lg:px-16 lg:pb-14 lg:pt-14\">\n          <p className=\"hidden text-[11px] font-medium uppercase tracking-[0.34em] text-white/55 lg:block\">Marlow desk <span className=\"mx-2 text-white/25\">/</span> air key</p>\n          <div>\n            <p className=\"mb-3 flex items-center gap-3 text-[11px] font-medium uppercase tracking-[0.3em] text-white/55 lg:mb-5\">\n              <span className={`block h-[7px] w-[7px] rounded-full ${on ? \"ray-marched-air-key-dot bg-[#ff5a1f] shadow-[0_0_10px_2px_rgba(255,90,31,0.7)]\" : \"bg-white/25\"}`} />\n              <span className={on ? \"text-[#ffb37a]\" : \"\"}>Studio 02</span>\n              <span className=\"h-px w-8 bg-white/25\" />\n              <span>{on ? \"Latched\" : \"Released\"}</span>\n            </p>\n            <p aria-live=\"polite\">\n              <span key={st.word} className=\"ray-marched-air-key-word block whitespace-nowrap text-[76px] font-semibold leading-[0.86] tracking-[-0.06em] text-white sm:text-[100px] lg:text-[156px]\">{st.word}</span>\n              <span className=\"mt-4 block w-[25rem] max-w-[82vw] text-[19px] italic leading-snug text-white/70 lg:mt-7 lg:text-[23px]\" style={{ fontFamily: SERIF }}>{st.note}</span>\n            </p>\n            <p className=\"mt-6 flex items-center gap-3 text-[12px] font-medium tracking-[0.02em] text-white/55 lg:mt-9\">\n              <span className=\"hidden rounded-[7px] border border-white/20 bg-white/[0.06] px-2.5 py-1 text-[11px] uppercase tracking-[0.16em] text-white/80 lg:inline-block\">Space</span>\n              <span className=\"hidden lg:inline\">or press the key</span>\n              <span className=\"uppercase tracking-[0.3em] text-[11px] lg:hidden\">Tap the key</span>\n            </p>\n          </div>\n        </div>\n        <p className=\"pointer-events-none absolute bottom-7 right-6 hidden text-[11px] uppercase tracking-[0.3em] text-white/40 sm:right-10 lg:bottom-14 lg:right-16 lg:block\">{TRAVEL * 10} mm travel, latching</p>\n      </div>\n    </section>\n  );\n}\n"}]}
