{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"guarded-toggle-switch-row-with-red-flip-up-safety-covers-you-lift-before-each-switch-will-arm","type":"registry:component","title":"Guarded Toggle Switch Row with Red Flip-Up Safety Covers You Lift Before Each Switch Will Arm","description":"Toggle row styled as a cockpit panel: each switch sits under a red hinged cover you must lift first, so nothing arms by accident. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/guarded-toggle-switch-row-with-red-flip-up-safety-covers-you-lift-before-each-switch-will-arm.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport type { MouseEvent as ReactMouseEvent } from \"react\";\n\n// The switches, left to right. `lamp` is the colour of the legend light above each one.\nconst switches = [\n  { label: \"BATTERY\", note: \"Main bus live\", lamp: \"#63f29a\", on: true },\n  { label: \"FUEL PUMP\", note: \"Boost pump running\", lamp: \"#63f29a\", on: false },\n  { label: \"BEACON\", note: \"Red beacon flashing\", lamp: \"#63f29a\", on: true },\n  { label: \"STARTER\", note: \"Starter armed\", lamp: \"#ffb22e\", on: false },\n];\nconst TITLE = \"ENGINE START\"; // painted in the gap of the white outline\nconst GUARD = [\"#ef4a3a\", \"#c4201a\", \"#6f0a07\"]; // cover paint: lit, body, shade\nconst PANEL = [\"#646b73\", \"#4c5259\", \"#3a3f45\"]; // panel paint, top to bottom\nconst RELAX = 4200; // a lifted cover drops shut after this many milliseconds if the switch is left off\nconst OPEN = (118 * Math.PI) / 180; // how far a cover swings up\n\nconst layoutFor = (narrow: boolean) => {\n  const n = switches.length;\n  if (narrow) { const rows = Math.ceil(n / 2); return { W: 380, H: 60 + rows * 296, units: switches.map((_, i) => ({ x: i % 2 === 0 ? 108 : 272, y: 226 + Math.floor(i / 2) * 296 })) }; }\n  const W = Math.max(480, 84 + n * 156);\n  return { W, H: 396, units: switches.map((_, i) => ({ x: W / 2 + (i - (n - 1) / 2) * 156, y: 244 })) };\n};\n\nexport default function GuardedToggleSwitchRow() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [narrow, setNarrow] = useState(false);\n  const [state, setState] = useState(() => switches.map((s) => ({ up: s.on, on: s.on })));\n  const [said, setSaid] = useState(\"Lift a red cover, then flip the switch under it\");\n  const live = useRef(state.map((s) => ({ ...s, since: 0 })));\n  const api = useRef<{ poke: () => void } | null>(null);\n\n  useEffect(() => {\n    const el = stage.current;\n    if (!el) return;\n    const ro = new ResizeObserver(() => setNarrow(el.clientWidth < 640));\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n\n  // One place that changes a switch, used by clicks, keys and the idle demo alike.\n  const act = (i: number, what: \"step\" | \"shut\", quiet = false) => {\n    const cur = live.current[i], sw = switches[i];\n    let line = \"\";\n    if (what === \"shut\" || (cur.up && cur.on)) { if (!cur.up && !cur.on) return; cur.up = false; cur.on = false; line = `${sw.label} off, cover shut`; }\n    else if (!cur.up) { cur.up = true; line = `${sw.label} cover lifted. Press again to switch on`; }\n    else { cur.on = true; line = `${sw.label} on. ${sw.note}`; }\n    cur.since = performance.now();\n    setState(live.current.map((s) => ({ up: s.up, on: s.on })));\n    if (!quiet) setSaid(line);\n    api.current?.poke();\n  };\n\n  const touched = useRef(-1e9);\n  const actRef = useRef(act);\n  actRef.current = act;\n\n  useEffect(() => {\n    const cv = canvas.current;\n    if (!cv) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const L = layoutFor(narrow);\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let k = 1;\n    const base = document.createElement(\"canvas\");\n    const MONO = \"ui-monospace, SFMono-Regular, Menlo, monospace\", SANS = \"ui-sans-serif, system-ui, 'Helvetica Neue', Arial, sans-serif\";\n    const GW = 48, GL = 96, GD = 30, Z0 = 12, EYE = 560, HINGE = -50, LEVER = 34; // cover width, length, depth; hinge stand-off; viewing distance\n\n    const rr = (c: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, r: number) => {\n      c.beginPath(); c.moveTo(x + r, y); c.arcTo(x + w, y, x + w, y + h, r); c.arcTo(x + w, y + h, x, y + h, r); c.arcTo(x, y + h, x, y, r); c.arcTo(x, y, x + w, y, r); c.closePath();\n    };\n    const hex = (c: CanvasRenderingContext2D, x: number, y: number, r: number, turn: number) => { c.beginPath(); for (let i = 0; i < 6; i++) { const a = turn + (i * Math.PI) / 3; c.lineTo(x + Math.cos(a) * r, y + Math.sin(a) * r); } c.closePath(); };\n    const steel = (c: CanvasRenderingContext2D, x0: number, y0: number, x1: number, y1: number) => {\n      const g = c.createLinearGradient(x0, y0, x1, y1);\n      for (const [p, v] of [[0, \"#fafbfc\"], [0.22, \"#b9bdc3\"], [0.42, \"#6d7177\"], [0.56, \"#e4e6e9\"], [0.78, \"#55585e\"], [1, \"#a5a9af\"]] as [number, string][]) g.addColorStop(p, v);\n      return g;\n    };\n    const legend = (c: CanvasRenderingContext2D, text: string, x: number, y: number, size: number, alpha = 0.92) => {\n      c.font = `700 ${size}px ${SANS}`; c.textAlign = \"center\"; c.textBaseline = \"middle\";\n      c.fillStyle = \"rgba(0,0,0,0.45)\"; c.fillText(text, x, y + 0.9);\n      c.fillStyle = `rgba(240,242,244,${alpha})`; c.fillText(text, x, y);\n    };\n\n    const build = () => {\n      const b = base.getContext(\"2d\");\n      if (!b) return;\n      base.width = cv.width; base.height = cv.height;\n      b.setTransform(k, 0, 0, k, 0, 0);\n      let seed = 97;\n      const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n      const X = 2, Y = 2, PW = L.W - 4, PH = L.H - 4;\n\n      // The panel: sprayed grey paint with a fine orange-peel speckle and a worn bright edge.\n      const paint = b.createLinearGradient(0, Y, 0, Y + PH);\n      paint.addColorStop(0, PANEL[0]); paint.addColorStop(0.35, PANEL[1]); paint.addColorStop(1, PANEL[2]);\n      rr(b, X, Y, PW, PH, 12); b.fillStyle = paint; b.fill();\n      b.save(); rr(b, X, Y, PW, PH, 12); b.clip();\n      const dots = Math.round((L.W * L.H) / 14);\n      for (let i = 0; i < dots; i++) { const v = rand(); b.fillStyle = v > 0.5 ? `rgba(255,255,255,${(v - 0.5) * 0.12})` : `rgba(0,0,0,${(0.5 - v) * 0.16})`; b.fillRect(rand() * L.W, rand() * L.H, 0.9 + rand() * 0.9, 0.9 + rand() * 0.9); }\n      const wash = b.createLinearGradient(X, Y, X + PW, Y + PH);\n      wash.addColorStop(0, \"rgba(255,255,255,0.1)\"); wash.addColorStop(0.4, \"rgba(255,255,255,0)\"); wash.addColorStop(1, \"rgba(0,0,0,0.14)\");\n      b.fillStyle = wash; b.fillRect(X, Y, PW, PH);\n      b.restore();\n      const edge = b.createLinearGradient(0, Y, 0, Y + PH);\n      edge.addColorStop(0, \"rgba(255,255,255,0.7)\"); edge.addColorStop(0.06, \"rgba(255,255,255,0.16)\"); edge.addColorStop(0.92, \"rgba(0,0,0,0.25)\"); edge.addColorStop(1, \"rgba(0,0,0,0.6)\");\n      rr(b, X + 0.7, Y + 0.7, PW - 1.4, PH - 1.4, 11.3); b.lineWidth = 1.4; b.strokeStyle = edge; b.stroke();\n\n      // Quarter-turn fasteners in the corners.\n      [[X + 22, Y + 22, 0.4], [X + PW - 22, Y + 22, 1.9], [X + 22, Y + PH - 22, 2.6], [X + PW - 22, Y + PH - 22, 1.1]].forEach(([x, y, turn]) => {\n        b.beginPath(); b.arc(x, y + 1, 9.6, 0, Math.PI * 2); b.fillStyle = \"rgba(0,0,0,0.5)\"; b.fill();\n        b.beginPath(); b.arc(x, y, 9, 0, Math.PI * 2); b.fillStyle = steel(b, x - 9, y - 9, x + 9, y + 9); b.fill();\n        b.beginPath(); b.arc(x, y, 6.3, 0, Math.PI * 2); b.fillStyle = steel(b, x + 6, y + 6, x - 6, y - 6); b.fill();\n        b.save(); b.translate(x, y); b.rotate(turn); b.fillStyle = \"#121315\"; rr(b, -5.4, -1.3, 10.8, 2.6, 1.2); b.fill(); b.fillStyle = \"rgba(255,255,255,0.5)\"; b.fillRect(-5, 1.3, 10, 0.6); b.restore();\n      });\n\n      // The painted outline that groups the row, broken for its title.\n      const xs = L.units.map((u) => u.x), ys = L.units.map((u) => u.y);\n      const ox = Math.min(...xs) - 62, oy = Math.min(...ys) - 194, ow = Math.max(...xs) - Math.min(...xs) + 124, oh = Math.max(...ys) - Math.min(...ys) + 290;\n      rr(b, ox, oy, ow, oh, 9); b.lineWidth = 1.5; b.strokeStyle = \"rgba(240,242,244,0.78)\"; b.stroke();\n      b.font = `700 9.5px ${SANS}`; const tw = b.measureText(TITLE).width + 22;\n      b.fillStyle = PANEL[0]; b.fillRect(L.W / 2 - tw / 2, oy - 5, tw, 10);\n      b.save(); b.beginPath(); b.rect(L.W / 2 - tw / 2, oy - 5, tw, 10); b.clip();\n      for (let i = 0; i < 160; i++) { const v = rand(); b.fillStyle = v > 0.5 ? `rgba(255,255,255,${(v - 0.5) * 0.12})` : `rgba(0,0,0,${(0.5 - v) * 0.16})`; b.fillRect(L.W / 2 - tw / 2 + rand() * tw, oy - 5 + rand() * 10, 1.2, 1.2); }\n      b.restore();\n      legend(b, TITLE, L.W / 2, oy + 0.5, 9.5);\n\n      L.units.forEach((u, i) => {\n        // Legend light housing.\n        const lx = u.x - 29, ly = u.y - 178;\n        rr(b, lx - 3, ly - 3, 64, 30, 5); b.fillStyle = \"#17181a\"; b.fill();\n        const lb = b.createLinearGradient(0, ly - 3, 0, ly + 27); lb.addColorStop(0, \"rgba(0,0,0,0.9)\"); lb.addColorStop(1, \"rgba(255,255,255,0.45)\");\n        rr(b, lx - 3, ly - 3, 64, 30, 5); b.lineWidth = 1.2; b.strokeStyle = lb; b.stroke();\n        // Label, and the two positions painted either side of the nut.\n        legend(b, switches[i].label, u.x, u.y + 70, 10.5);\n        legend(b, \"ON\", u.x - 41, u.y - 22, 7.5, 0.7); legend(b, \"OFF\", u.x - 41, u.y + 23, 7.5, 0.7);\n        // Hex nut, lock washer and collar of the switch.\n        b.beginPath(); b.arc(u.x, u.y, 21, 0, Math.PI * 2); b.fillStyle = \"rgba(0,0,0,0.28)\"; b.fill();\n        b.beginPath(); b.arc(u.x, u.y, 20, 0, Math.PI * 2); b.fillStyle = steel(b, u.x + 20, u.y - 20, u.x - 20, u.y + 20); b.fill();\n        b.save(); b.shadowColor = \"rgba(0,0,0,0.6)\"; b.shadowBlur = 4 * k; b.shadowOffsetY = 2 * k;\n        hex(b, u.x, u.y, 17, 0.5 + i * 0.37); b.fillStyle = steel(b, u.x - 17, u.y - 17, u.x + 17, u.y + 17); b.fill(); b.restore();\n        hex(b, u.x, u.y, 17, 0.5 + i * 0.37); b.lineWidth = 0.8; b.strokeStyle = \"rgba(0,0,0,0.45)\"; b.stroke();\n        b.beginPath(); b.arc(u.x, u.y, 11, 0, Math.PI * 2); b.fillStyle = steel(b, u.x + 11, u.y + 11, u.x - 11, u.y - 11); b.fill();\n        b.beginPath(); b.arc(u.x, u.y, 7.6, 0, Math.PI * 2); b.fillStyle = \"#070708\"; b.fill();\n        // Hinge bracket riveted to the panel.\n        const hy = u.y + HINGE;\n        b.save(); b.shadowColor = \"rgba(0,0,0,0.55)\"; b.shadowBlur = 4 * k; b.shadowOffsetY = 2 * k;\n        rr(b, u.x - 33, hy - 8, 66, 14, 3); b.fillStyle = steel(b, u.x - 33, hy - 8, u.x + 33, hy + 6); b.fill(); b.restore();\n        for (const rx of [u.x - 28, u.x + 28]) { b.beginPath(); b.arc(rx, hy - 1, 2.6, 0, Math.PI * 2); b.fillStyle = \"#3a3c40\"; b.fill(); b.beginPath(); b.arc(rx - 0.5, hy - 1.6, 1.5, 0, Math.PI * 2); b.fillStyle = \"#e8eaec\"; b.fill(); }\n      });\n    };\n\n    const u0 = live.current;\n    const s = u0.map((v) => ({ th: v.up ? OPEN : 0, thv: 0, ph: v.on ? 0.5 : -0.5, phv: 0, lit: v.on ? 1 : 0 }));\n    const mix = (a: string, c: string, t: number) => { const p = (h: string) => [1, 3, 5].map((j) => parseInt(h.slice(j, j + 2), 16)), x = p(a), y = p(c), q = Math.max(0, Math.min(1, t)); return `rgb(${x.map((v, j) => Math.round(v + (y[j] - v) * q)).join(\",\")})`; };\n\n    const unit = (i: number) => {\n      const u = L.units[i], st = s[i], sw = switches[i], hy = u.y + HINGE;\n      // Legend light.\n      const lx = u.x - 29, ly = u.y - 178, lit = st.lit;\n      const glassG = ctx.createLinearGradient(0, ly, 0, ly + 24); glassG.addColorStop(0, \"#0b0c0d\"); glassG.addColorStop(1, \"#1a1b1d\");\n      rr(ctx, lx, ly, 58, 24, 3); ctx.fillStyle = glassG; ctx.fill();\n      ctx.save(); rr(ctx, lx, ly, 58, 24, 3); ctx.clip();\n      ctx.globalAlpha = lit * 0.3; ctx.fillStyle = sw.lamp; ctx.fillRect(lx, ly, 58, 24);\n      ctx.globalAlpha = 0.14 + 0.86 * lit; ctx.shadowColor = sw.lamp; ctx.shadowBlur = 10 * lit * k;\n      ctx.font = `700 11px ${SANS}`; ctx.textAlign = \"center\"; ctx.textBaseline = \"middle\"; ctx.fillStyle = lit > 0.5 ? mix(sw.lamp, \"#ffffff\", 0.35) : sw.lamp; ctx.fillText(\"ON\", u.x, ly + 12.5);\n      ctx.shadowBlur = 0; ctx.globalAlpha = 1;\n      const gl = ctx.createLinearGradient(lx, ly, lx + 40, ly + 24); gl.addColorStop(0, \"rgba(255,255,255,0.22)\"); gl.addColorStop(0.45, \"rgba(255,255,255,0.03)\"); gl.addColorStop(0.46, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = gl; ctx.fillRect(lx, ly, 58, 24); ctx.restore();\n      if (lit > 0.05) { const halo = ctx.createRadialGradient(u.x, ly + 12, 6, u.x, ly + 12, 54); halo.addColorStop(0, mix(\"#000000\", sw.lamp, 1).replace(\"rgb(\", \"rgba(\").replace(\")\", `,${0.2 * lit})`)); halo.addColorStop(1, \"rgba(0,0,0,0)\"); ctx.fillStyle = halo; ctx.fillRect(u.x - 56, ly - 44, 112, 112); }\n\n      // Bat-handle lever, seen almost end on.\n      const lz = LEVER * Math.cos(st.ph) + 6, sc = EYE / (EYE - lz), ty = u.y - LEVER * Math.sin(st.ph) * sc * 1.25, tr = 9 * sc;\n      const shd = ctx.createRadialGradient(u.x + 7, ty + 13, 2, u.x + 7, ty + 13, 20); shd.addColorStop(0, \"rgba(0,0,0,0.5)\"); shd.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx.fillStyle = shd; ctx.fillRect(u.x - 16, ty - 10, 46, 46);\n      ctx.beginPath(); ctx.moveTo(u.x - 5.4, u.y); ctx.lineTo(u.x - tr, ty); ctx.lineTo(u.x + tr, ty); ctx.lineTo(u.x + 5.4, u.y); ctx.closePath();\n      ctx.fillStyle = steel(ctx, u.x - tr, 0, u.x + tr, 0); ctx.fill();\n      const tip = ctx.createRadialGradient(u.x - tr * 0.35, ty - tr * 0.4, 0.5, u.x, ty, tr);\n      tip.addColorStop(0, \"#ffffff\"); tip.addColorStop(0.3, \"#d5d8dc\"); tip.addColorStop(0.7, \"#8b8f95\"); tip.addColorStop(0.92, \"#4b4e53\"); tip.addColorStop(1, \"#a9adb3\");\n      ctx.beginPath(); ctx.arc(u.x, ty, tr, 0, Math.PI * 2); ctx.fillStyle = tip; ctx.fill();\n      ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(0,0,0,0.45)\"; ctx.stroke();\n\n      // The cover: a pressed channel turning on the hinge pin, projected with a little perspective.\n      const th = st.th, cos = Math.cos(th), sin = Math.sin(th);\n      const P = (a: number, v: number, n: number) => { const y = v * cos - n * sin, z = v * sin + n * cos + Z0, f = EYE / (EYE - z); return { x: u.x + a * f, y: hy + y * f, z }; };\n      const quad = (pts: { x: number; y: number }[]) => { ctx.beginPath(); pts.forEach((p, j) => (j ? ctx.lineTo(p.x, p.y) : ctx.moveTo(p.x, p.y))); ctx.closePath(); };\n      const h = GW / 2, A = P(-h, 0, GD), B = P(h, 0, GD), C = P(h, GL, GD), E = P(-h, GL, GD), a0 = P(-h, 0, 0), b0 = P(h, 0, 0), c0 = P(h, GL, 0), e0 = P(-h, GL, 0);\n      // Its shadow falls where the light, above and in front, would put it.\n      const drop = (p: { x: number; y: number; z: number }) => ({ x: p.x + p.z * 0.1, y: p.y + p.z * 0.42 });\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.5)\"; ctx.shadowBlur = 12 * k; ctx.shadowOffsetX = 4000 * k; quad([A, B, C, E].map(drop).map((p) => ({ x: p.x - 4000, y: p.y }))); ctx.fillStyle = \"#000\"; ctx.fill(); ctx.restore();\n      const outer = cos > 0, light = Math.max(0, 0.6 * sin + 0.8 * cos);\n      const walls = () => {\n        for (const [p, q, r2, t2] of [[a0, A, E, e0], [b0, B, C, c0]]) { quad([p, q, r2, t2]); ctx.fillStyle = mix(GUARD[2], GUARD[1], outer ? 0.2 : 0.55); ctx.fill(); ctx.lineWidth = 1.6; ctx.lineJoin = \"round\"; ctx.strokeStyle = mix(GUARD[2], GUARD[1], outer ? 0.2 : 0.7); ctx.stroke(); }\n      };\n      const plate = () => {\n        const g = ctx.createLinearGradient(0, A.y, 0, E.y);\n        if (outer) {\n          const band = Math.max(0.08, Math.min(0.92, 0.22 + th * 0.75));\n          g.addColorStop(0, mix(GUARD[2], GUARD[1], 0.4 + light * 0.6)); g.addColorStop(Math.max(0, band - 0.2), mix(GUARD[1], GUARD[0], light * 0.7));\n          g.addColorStop(band, mix(GUARD[0], \"#ffd9d2\", 0.25 + light * 0.3)); g.addColorStop(Math.min(1, band + 0.22), mix(GUARD[1], GUARD[0], light * 0.5)); g.addColorStop(1, mix(GUARD[2], GUARD[1], 0.75));\n        } else { g.addColorStop(0, mix(GUARD[2], \"#000000\", 0.45)); g.addColorStop(0.5, mix(GUARD[2], GUARD[1], 0.25)); g.addColorStop(1, mix(GUARD[2], GUARD[1], 0.7)); }\n        ctx.lineJoin = \"round\"; quad([A, B, C, E]); ctx.lineWidth = 5; ctx.strokeStyle = outer ? mix(GUARD[2], GUARD[1], 0.5 + light * 0.4) : mix(GUARD[2], GUARD[1], 0.3); ctx.stroke();\n        quad([A, B, C, E]); ctx.fillStyle = g; ctx.fill();\n        if (outer) {\n          // A pressed rib down the middle and a bright line where the paint rolls over the long edge.\n          const m0 = P(0, GL * 0.16, GD), m1 = P(0, GL * 0.86, GD), wd = (E.x - C.x) * -0.2;\n          ctx.lineCap = \"round\"; ctx.beginPath(); ctx.moveTo(m0.x + 1.2, m0.y); ctx.lineTo(m1.x + 1.2, m1.y); ctx.lineWidth = wd; ctx.strokeStyle = \"rgba(60,0,0,0.35)\"; ctx.stroke();\n          ctx.beginPath(); ctx.moveTo(m0.x - 0.6, m0.y); ctx.lineTo(m1.x - 0.6, m1.y); ctx.lineWidth = wd * 0.72; ctx.strokeStyle = mix(GUARD[1], GUARD[0], 0.4 + light * 0.6); ctx.stroke();\n          ctx.beginPath(); ctx.moveTo(m0.x - wd * 0.18, m0.y + 2); ctx.lineTo(m1.x - wd * 0.18, m1.y - 2); ctx.lineWidth = 1.4; ctx.strokeStyle = `rgba(255,235,230,${0.3 + light * 0.45})`; ctx.stroke();\n          ctx.beginPath(); ctx.moveTo(A.x + 1, A.y + 3); ctx.lineTo(E.x + 1, E.y - 4); ctx.lineWidth = 1.3; ctx.strokeStyle = `rgba(255,225,220,${0.25 + light * 0.5})`; ctx.stroke(); ctx.lineCap = \"butt\";\n          // The finger lip at the free end, rolled outwards.\n          const l0 = P(-h, GL - 9, GD), l1 = P(h, GL - 9, GD);\n          const lip = ctx.createLinearGradient(0, l0.y, 0, E.y); lip.addColorStop(0, \"rgba(60,0,0,0.4)\"); lip.addColorStop(0.25, `rgba(255,220,214,${0.2 + light * 0.4})`); lip.addColorStop(1, \"rgba(60,0,0,0.3)\");\n          quad([l0, l1, C, E]); ctx.fillStyle = lip; ctx.fill();\n        } else {\n          // Looking up into the channel: a lit lip at the far end and the shade of the side walls.\n          ctx.beginPath(); ctx.moveTo(E.x + 2, E.y); ctx.lineTo(C.x - 2, C.y); ctx.lineWidth = 2; ctx.strokeStyle = mix(GUARD[1], GUARD[0], 0.6); ctx.stroke();\n          const sg = ctx.createLinearGradient(E.x, 0, C.x, 0); sg.addColorStop(0, \"rgba(0,0,0,0.4)\"); sg.addColorStop(0.25, \"rgba(0,0,0,0)\"); sg.addColorStop(0.75, \"rgba(0,0,0,0)\"); sg.addColorStop(1, \"rgba(0,0,0,0.4)\");\n          quad([A, B, C, E]); ctx.fillStyle = sg; ctx.fill();\n          const m0 = P(0, GL * 0.16, GD), m1 = P(0, GL * 0.86, GD);\n          ctx.lineCap = \"round\"; ctx.beginPath(); ctx.moveTo(m0.x, m0.y); ctx.lineTo(m1.x, m1.y); ctx.lineWidth = 7; ctx.strokeStyle = \"rgba(0,0,0,0.3)\"; ctx.stroke();\n          ctx.beginPath(); ctx.moveTo(m0.x + 2.6, m0.y); ctx.lineTo(m1.x + 3, m1.y); ctx.lineWidth = 1.2; ctx.strokeStyle = mix(GUARD[1], GUARD[0], 0.3); ctx.stroke(); ctx.lineCap = \"butt\";\n        }\n      };\n      if (outer) { walls(); plate(); } else { plate(); walls(); }\n      // Hinge pin and the coil of its return spring, in front of everything.\n      const pin = ctx.createLinearGradient(0, hy - 5, 0, hy + 5); pin.addColorStop(0, \"#fbfcfc\"); pin.addColorStop(0.35, \"#a9adb3\"); pin.addColorStop(0.7, \"#4b4e53\"); pin.addColorStop(1, \"#8b8f95\");\n      rr(ctx, u.x - 27, hy - 4.5, 54, 9, 4.5); ctx.fillStyle = pin; ctx.fill(); ctx.lineWidth = 0.6; ctx.strokeStyle = \"rgba(0,0,0,0.5)\"; ctx.stroke();\n      for (let c = -18; c <= 18; c += 4) { ctx.beginPath(); ctx.moveTo(u.x + c, hy - 4.2); ctx.lineTo(u.x + c + 1.6, hy + 4.2); ctx.lineWidth = 0.9; ctx.strokeStyle = Math.abs(c) < 9 ? \"rgba(0,0,0,0.4)\" : \"rgba(0,0,0,0)\"; ctx.stroke(); }\n    };\n\n    const draw = () => {\n      ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, cv.width, cv.height); ctx.drawImage(base, 0, 0);\n      ctx.setTransform(k, 0, 0, k, 0, 0);\n      for (let i = 0; i < switches.length; i++) unit(i);\n    };\n\n    let raf = 0, last = 0, seen = true, t0 = 0, cue = 0, lap = 0;\n    // The idle demo, as [seconds, switch, action] on a twenty second loop.\n    const script: [number, number, \"step\" | \"shut\"][] = [[0.5, 1, \"step\"], [1.3, 1, \"step\"], [5, 3, \"step\"], [5.9, 3, \"step\"], [8, 0, \"shut\"], [9.5, 2, \"shut\"], [11, 1, \"shut\"], [12.5, 3, \"shut\"], [14, 0, \"step\"], [14.8, 0, \"step\"], [16, 2, \"step\"], [16.8, 2, \"step\"]];\n    const frame = (now: number) => {\n      raf = 0;\n      if (!t0) { t0 = now; last = now; }\n      const dt = Math.min(0.033, (now - last) / 1000); last = now;\n      const idle = now - touched.current > 8000;\n      const T = (now - t0) / 1000 - lap * 20;\n      while (cue < script.length && script[cue][0] <= T) {\n        const [, i, what] = script[cue]; cue++;\n        if (idle && i < switches.length) { const c = live.current[i]; if (what === \"shut\" ? c.up || c.on : !(c.up && c.on)) actRef.current(i, what, true); }\n      }\n      if (T >= 20) { lap++; cue = 0; }\n      live.current.forEach((c, i) => {\n        const st = s[i];\n        if (!idle && c.up && !c.on && now - c.since > RELAX) actRef.current(i, \"shut\");\n        // Cover: a sprung flap that bounces off both of its stops.\n        st.thv += (150 * ((c.up ? OPEN : 0) - st.th) - 11 * st.thv) * dt; st.th += st.thv * dt;\n        if (st.th < 0) { st.th = 0; st.thv = -st.thv * 0.38; } else if (st.th > OPEN) { st.th = OPEN; st.thv = -st.thv * 0.3; }\n        // Lever: a quick over-centre snap that can only stay up while the cover is clear of it.\n        const want = c.on && st.th > 0.9 ? 0.5 : -0.5;\n        st.phv += (900 * (want - st.ph) - 30 * st.phv) * dt; st.ph += st.phv * dt;\n        st.lit += ((st.ph > 0.1 ? 1 : 0) - st.lit) * Math.min(1, dt * (st.ph > 0.1 ? 22 : 12));\n      });\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    const settle = () => { live.current.forEach((c, i) => { s[i] = { th: c.up ? OPEN : 0, thv: 0, ph: c.on ? 0.5 : -0.5, phv: 0, lit: c.on ? 1 : 0 }; }); draw(); };\n\n    const size = () => {\n      const dpr = Math.min(2, window.devicePixelRatio || 1), wpx = Math.max(200, cv.clientWidth);\n      cv.width = Math.round(wpx * dpr); cv.height = Math.round((wpx * dpr * L.H) / L.W); k = cv.width / L.W;\n      build(); draw();\n    };\n    size();\n    const ro = new ResizeObserver(size); ro.observe(cv);\n    api.current = { poke: () => { if (reduce) settle(); else wake(); } };\n    if (reduce) settle(); else wake();\n    const io = new IntersectionObserver(([entry]) => { seen = entry.isIntersecting; if (seen) wake(); });\n    io.observe(cv);\n    const onVis = () => { if (!document.hidden) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n    return () => { cancelAnimationFrame(raf); api.current = null; ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis); };\n  }, [narrow]);\n\n  const L = layoutFor(narrow);\n  const onClick = (e: ReactMouseEvent<HTMLButtonElement>, i: number) => {\n    touched.current = performance.now();\n    const r = e.currentTarget.getBoundingClientRect();\n    // A pointer click on a raised cover pushes it back down; anywhere else takes the next step.\n    const onCover = e.detail > 0 && live.current[i].up && (e.clientY - r.top) / r.height < 0.48;\n    act(i, onCover ? \"shut\" : \"step\");\n  };\n\n  return (\n    <div ref={stage} className=\"flex min-h-[640px] w-full flex-col items-center justify-center bg-[#d6d2c9] bg-[radial-gradient(100%_80%_at_50%_30%,#efede8_0%,#d6d2c9_70%)] px-4 py-10 sm:min-h-[720px]\">\n      <div className={`relative w-full ${narrow ? \"max-w-[330px]\" : \"max-w-[800px]\"}`} style={{ aspectRatio: `${L.W} / ${L.H}` }}>\n        <div aria-hidden=\"true\" className=\"absolute inset-[3px] rounded-xl shadow-[0_26px_50px_-6px_rgba(46,38,26,0.55),0_6px_12px_rgba(46,38,26,0.3)]\" />\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        <div role=\"group\" aria-label={TITLE.toLowerCase()} className=\"absolute inset-0\">\n          {switches.map((sw, i) => (\n            <button\n              key={sw.label}\n              type=\"button\"\n              role=\"switch\"\n              aria-checked={state[i].on}\n              aria-label={sw.label.toLowerCase()}\n              aria-describedby=\"gtsr-how\"\n              onClick={(e) => onClick(e, i)}\n              onKeyDown={(e) => { if (e.key === \"Escape\") { touched.current = performance.now(); act(i, \"shut\"); } }}\n              style={{ left: `${((L.units[i].x - 54) / L.W) * 100}%`, top: `${((L.units[i].y - 186) / L.H) * 100}%`, width: `${(108 / L.W) * 100}%`, height: `${(272 / L.H) * 100}%` }}\n              className=\"absolute cursor-pointer touch-manipulation rounded-xl outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-offset-2 focus-visible:ring-offset-[#4c5259]\"\n            />\n          ))}\n        </div>\n      </div>\n      <p id=\"gtsr-how\" className=\"sr-only\">Covered switch. The first press lifts the cover, the second switches on, the third switches off and shuts the cover. Escape shuts it at any time.</p>\n      <p aria-live=\"polite\" className=\"mt-9 min-h-[20px] max-w-[340px] text-center font-mono text-[11px] uppercase leading-5 tracking-[0.16em] text-stone-700 sm:max-w-[560px]\">{said}</p>\n    </div>\n  );\n}\n"}]}
