{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"spirit-level-balance-indicator-with-a-bubble-vial-that-settles-between-two-values","type":"registry:component","title":"Spirit Level Balance Indicator with a Bubble Vial That Settles Between Two Values","description":"Status indicator drawn as a spirit level: the bubble runs uphill when the values drift apart and centres when they match. Free React code and HTML to copy.","dependencies":[],"files":[{"path":"components/we-inc/spirit-level-balance-indicator-with-a-bubble-vial-that-settles-between-two-values.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Everything a visitor is likely to change sits here.\nconst sides = [{ label: \"Opened\", start: 38 }, { label: \"Closed\", start: 26 }]; // left end, right end\nconst CAPTION = \"Support tickets this week\";\nconst demo: [number, number][] = [[32, 32], [29, 36], [33, 33], [38, 26]]; // played in turn until somebody touches a button\nconst DEMO_FIRST = 700, DEMO_EVERY = 3600; // milliseconds\nconst TILT_GAIN = 40; // degrees of lean for a completely one-sided split\nconst MAX_TILT = 5; // the shelf stops here\nconst LEVEL_BAND = 0.075; // share of full lean that still reads as level\nconst ANODISE = [\"#f0633c\", \"#d4441f\", \"#a93014\"]; // top, middle and bottom of the coloured web\nconst SPIRIT = [\"#a9c414\", \"#e4f84a\", \"#cfea2c\", \"#b4d51a\", \"#f6ffa0\", \"#7a9806\"]; // the liquid, top to bottom\n\nconst TAU = Math.PI * 2;\nconst clamp = (v: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, v));\n// From two numbers to a lean between -1 (left end down) and 1 (right end down).\nconst lean = (a: number, b: number) => (a + b === 0 ? 0 : clamp((((b - a) / (a + b)) * TILT_GAIN) / MAX_TILT, -1, 1));\n\nconst layoutFor = (narrow: boolean) =>\n  narrow\n    ? { W: 380, H: 236, PX: 186, PY: 150, PL: 326, PT: 14, BL: 304, BH: 80, rail: 8, cap: 13, VL: 116, VR: 14, BW: 34, port: 22, portAt: 106, holes: [] as number[], holeW: 0 }\n    : { W: 900, H: 300, PX: 446, PY: 196, PL: 800, PT: 18, BL: 660, BH: 100, rail: 10, cap: 18, VL: 156, VR: 18, BW: 46, port: 29, portAt: 268, holes: [-160, 160], holeW: 92 };\n\nexport default function SpiritLevelBalanceIndicator() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [narrow, setNarrow] = useState(false);\n  const [vals, setVals] = useState<[number, number]>([sides[0].start, sides[1].start]);\n  const [touched, setTouched] = useState(false);\n  const live = useRef({ f: lean(sides[0].start, sides[1].start), touched: false });\n  const api = useRef<{ poke: (push?: number) => void } | null>(null);\n\n  const show = (next: [number, number]) => { live.current.f = lean(next[0], next[1]); setVals(next); api.current?.poke(); };\n\n  useEffect(() => {\n    const el = stage.current;\n    if (!el) return;\n    const ro = new ResizeObserver(() => setNarrow(el.clientWidth < 760));\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n\n  // The self-playing demonstration.\n  useEffect(() => {\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    let i = 0, id = 0;\n    const next = () => {\n      if (live.current.touched) return;\n      if (!document.hidden) { show(demo[i % demo.length]); i++; }\n      id = window.setTimeout(next, DEMO_EVERY);\n    };\n    id = window.setTimeout(next, DEMO_FIRST);\n    return () => window.clearTimeout(id);\n  }, []);\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; // canvas pixels per design pixel\n    const PAD = 46, RW = L.PL + PAD * 2, RH = L.BH + L.PT + PAD * 2, OY = PAD + L.BH + L.PT / 2; // the sprite that tilts, and where its pivot sits\n    const bx = -L.BL / 2, by = -(L.PT / 2 + L.BH), vy = by + L.BH / 2; // body corner and the height of the vials\n    const maxPos = L.VL / 2 - L.BW / 2 - 7; // how far the bubble can run from centre\n    const winW = L.VL + 30, winH = L.VR * 2 + 22;\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 steel = (c: CanvasRenderingContext2D, x: number, y: number, r: number) => {\n      const g = c.createLinearGradient(x - r, y - r, x + r, y + r);\n      g.addColorStop(0, \"#fbfbfb\"); g.addColorStop(0.4, \"#a5a8ac\"); g.addColorStop(0.6, \"#dfe1e3\"); g.addColorStop(1, \"#6a6d71\");\n      return g;\n    };\n\n    // The shelf and the level body are painted once into a sprite, and so is the shadow they throw on the wall.\n    const rig = document.createElement(\"canvas\"), shade = document.createElement(\"canvas\");\n    const build = () => {\n      const g = rig.getContext(\"2d\"), sc = shade.getContext(\"2d\");\n      if (!g || !sc) return;\n      rig.width = shade.width = Math.round(RW * k); rig.height = shade.height = Math.round(RH * k);\n      g.setTransform(k, 0, 0, k, (RW / 2) * k, OY * k);\n      let seed = 23;\n      const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n\n      // Oak shelf, seen edge on.\n      const px = -L.PL / 2, py = -L.PT / 2;\n      const oak = g.createLinearGradient(0, py, 0, py + L.PT);\n      oak.addColorStop(0, \"#ecd2a4\"); oak.addColorStop(0.16, \"#d9b884\"); oak.addColorStop(0.7, \"#c39c68\"); oak.addColorStop(1, \"#8e6a3f\");\n      rr(g, px, py, L.PL, L.PT, 2.5); g.fillStyle = oak; g.fill();\n      g.save(); rr(g, px, py, L.PL, L.PT, 2.5); g.clip();\n      for (let i = 0; i < 46; i++) {\n        const y0 = py + rand() * L.PT, x0 = px + rand() * L.PL * 0.9 - 40, len = 80 + rand() * 340, bend = (rand() - 0.5) * 3.4;\n        g.beginPath(); g.moveTo(x0, y0); g.quadraticCurveTo(x0 + len / 2, y0 + bend, x0 + len, y0 + (rand() - 0.5) * 1.6);\n        g.lineWidth = 0.5 + rand() * 0.7; g.strokeStyle = rand() > 0.3 ? `rgba(92,58,22,${0.1 + rand() * 0.2})` : `rgba(255,240,205,${0.16 + rand() * 0.2})`; g.stroke();\n      }\n      g.fillStyle = \"rgba(255,246,222,0.75)\"; g.fillRect(px, py, L.PL, 1);\n      g.fillStyle = \"rgba(50,30,8,0.4)\"; g.fillRect(px, py + L.PT - 1.2, L.PL, 1.2);\n      // Where the level rests it keeps the light off the wood.\n      const rest = g.createLinearGradient(0, py, 0, py + 5);\n      rest.addColorStop(0, \"rgba(40,24,4,0.5)\"); rest.addColorStop(1, \"rgba(40,24,4,0)\");\n      g.fillStyle = rest; g.fillRect(bx + 2, py, L.BL - 4, 5);\n      g.restore();\n\n      // Box-section body: two milled rails and an anodised web between them.\n      g.save(); rr(g, bx, by, L.BL, L.BH, 2.5); g.clip();\n      const web = g.createLinearGradient(0, by, 0, by + L.BH);\n      web.addColorStop(0, ANODISE[0]); web.addColorStop(0.5, ANODISE[1]); web.addColorStop(1, ANODISE[2]);\n      g.fillStyle = web; g.fillRect(bx, by, L.BL, L.BH);\n      for (let y = by; y < by + L.BH; y += 0.5) {\n        const v = rand(), x0 = bx + rand() * L.BL * 0.3, x1 = bx + L.BL - rand() * L.BL * 0.3;\n        g.beginPath(); g.moveTo(x0, y); g.lineTo(x1, y); g.lineWidth = 0.5;\n        g.strokeStyle = v > 0.5 ? `rgba(255,220,200,${(v - 0.5) * 0.3})` : `rgba(70,10,0,${(0.5 - v) * 0.3})`; g.stroke();\n      }\n      // A long soft reflection running the length of the web, as anodised metal gives.\n      const sheen = g.createLinearGradient(0, by + L.rail, 0, by + L.BH - L.rail);\n      sheen.addColorStop(0, \"rgba(40,6,0,0.4)\"); sheen.addColorStop(0.1, \"rgba(40,6,0,0)\"); sheen.addColorStop(0.24, \"rgba(255,226,208,0.3)\"); sheen.addColorStop(0.36, \"rgba(255,226,208,0)\"); sheen.addColorStop(0.88, \"rgba(40,6,0,0)\"); sheen.addColorStop(1, \"rgba(255,190,160,0.35)\");\n      g.fillStyle = sheen; g.fillRect(bx, by + L.rail, L.BL, L.BH - L.rail * 2);\n      for (const top of [true, false]) {\n        const y = top ? by : by + L.BH - L.rail, rail = g.createLinearGradient(0, y, 0, y + L.rail);\n        if (top) { rail.addColorStop(0, \"#ffffff\"); rail.addColorStop(0.2, \"#e6e8ea\"); rail.addColorStop(0.75, \"#b8bcc1\"); rail.addColorStop(1, \"#7f8388\"); }\n        else { rail.addColorStop(0, \"#eceef0\"); rail.addColorStop(0.18, \"#b3b7bc\"); rail.addColorStop(0.8, \"#8b8f95\"); rail.addColorStop(1, \"#5c6065\"); }\n        g.fillStyle = rail; g.fillRect(bx, y, L.BL, L.rail);\n        for (let yy = y; yy < y + L.rail; yy += 0.5) { g.fillStyle = rand() > 0.5 ? \"rgba(255,255,255,0.1)\" : \"rgba(0,0,0,0.06)\"; g.fillRect(bx, yy, L.BL, 0.5); }\n      }\n      // A millimetre scale engraved along the top rail.\n      for (let i = 0, x = bx + L.cap + 6; x < bx + L.BL - L.cap - 5; x += 4, i++) {\n        g.fillStyle = \"rgba(28,30,34,0.72)\"; const th = i % 10 === 0 ? L.rail * 0.72 : i % 5 === 0 ? L.rail * 0.52 : L.rail * 0.32; g.fillRect(x, by + L.rail - th, 0.7, th);\n      }\n      g.restore();\n\n      // Milled openings: a chamfer ring first, then the pocket.\n      const chamfer = (y0: number, y1: number) => { const c = g.createLinearGradient(0, y0, 0, y1); c.addColorStop(0, \"#5a1606\"); c.addColorStop(0.45, \"#a2300f\"); c.addColorStop(1, \"#ffa583\"); return c; };\n      const holeH = L.BH * 0.34, holeY = vy - holeH / 2;\n      for (const hx of L.holes) { rr(g, hx - L.holeW / 2 - 3, holeY - 3, L.holeW + 6, holeH + 6, holeH / 2 + 3); g.fillStyle = chamfer(holeY - 3, holeY + holeH + 3); g.fill(); }\n      rr(g, -winW / 2 - 3.5, vy - winH / 2 - 3.5, winW + 7, winH + 7, 9.5); g.fillStyle = chamfer(vy - winH / 2 - 3.5, vy + winH / 2 + 3.5); g.fill();\n      const pocket = (path: () => void, y0: number, h: number) => {\n        const p = g.createLinearGradient(0, y0, 0, y0 + h);\n        p.addColorStop(0, \"#c9ccbd\"); p.addColorStop(0.3, \"#eef0e4\"); p.addColorStop(1, \"#fbfcf4\");\n        path(); g.fillStyle = p; g.fill();\n        g.save(); path(); g.clip(); g.shadowColor = \"rgba(30,34,10,0.65)\"; g.shadowBlur = 7 * k; g.shadowOffsetX = 2 * k; g.shadowOffsetY = 4 * k;\n        path(); g.lineWidth = 3; g.strokeStyle = \"#20240c\"; g.stroke(); g.restore();\n      };\n      pocket(() => rr(g, -winW / 2, vy - winH / 2, winW, winH, 6), vy - winH / 2, winH);\n      for (const s of [-1, 1]) {\n        const cx = s * L.portAt;\n        g.beginPath(); g.arc(cx, vy, L.port + 3.5, 0, TAU); g.fillStyle = chamfer(vy - L.port - 3.5, vy + L.port + 3.5); g.fill();\n        pocket(() => { g.beginPath(); g.arc(cx, vy, L.port, 0, TAU); }, vy - L.port, L.port * 2);\n      }\n      // Laser-marked legends.\n      g.font = `700 ${narrow ? 6.5 : 7.5}px ui-monospace, SFMono-Regular, Menlo, monospace`; g.textBaseline = \"middle\"; g.fillStyle = \"rgba(255,236,224,0.88)\";\n      if (L.holes.length) {\n        g.textAlign = \"center\";\n        g.fillText(\"600 MM\", L.holes[0], by + L.rail + 11); g.fillText(\"0.5 MM / M\", L.holes[1], by + L.rail + 11);\n        g.fillText(\"BOX SECTION\", L.holes[0], by + L.BH - L.rail - 10); g.fillText(\"MILLED BASE\", L.holes[1], by + L.BH - L.rail - 10);\n      }\n      // Two fine arrows pointing at the centre marks.\n      for (const s of [-1, 1]) { const ax = s * (winW / 2 + 9); g.beginPath(); g.moveTo(ax, vy); g.lineTo(ax + s * 5, vy - 3); g.lineTo(ax + s * 5, vy + 3); g.closePath(); g.fill(); }\n\n      // Rubber end caps with moulded ribs.\n      for (const s of [-1, 1]) {\n        const x = s < 0 ? bx - 1.5 : bx + L.BL - L.cap + 1.5;\n        const rub = g.createLinearGradient(0, by - 2, 0, by + L.BH + 2);\n        rub.addColorStop(0, \"#4b4c4f\"); rub.addColorStop(0.12, \"#2a2b2d\"); rub.addColorStop(0.85, \"#161617\"); rub.addColorStop(1, \"#0a0a0a\");\n        rr(g, x, by - 2, L.cap, L.BH + 3.5, 4); g.fillStyle = rub; g.fill();\n        for (let i = 1; i <= 3; i++) {\n          const rx = x + (L.cap * i) / 4;\n          g.fillStyle = \"rgba(0,0,0,0.6)\"; g.fillRect(rx - 0.9, by + 6, 1.4, L.BH - 12);\n          g.fillStyle = \"rgba(255,255,255,0.13)\"; g.fillRect(rx + 0.5, by + 6, 0.7, L.BH - 12);\n        }\n        g.fillStyle = \"rgba(255,255,255,0.3)\"; g.fillRect(x + 2, by - 1.4, L.cap - 4, 0.8);\n      }\n      // Cut the hand holes clean through, so the wall and the shadow show behind them.\n      g.globalCompositeOperation = \"destination-out\";\n      for (const hx of L.holes) { rr(g, hx - L.holeW / 2, holeY, L.holeW, holeH, holeH / 2); g.fillStyle = \"#000\"; g.fill(); }\n      g.globalCompositeOperation = \"source-over\";\n\n      sc.setTransform(1, 0, 0, 1, 0, 0); sc.clearRect(0, 0, shade.width, shade.height);\n      sc.shadowColor = \"rgba(54,40,18,0.5)\"; sc.shadowBlur = 15 * k; sc.shadowOffsetX = shade.width * 2;\n      sc.drawImage(rig, -shade.width * 2, 0);\n    };\n\n    const fit = () => {\n      const w = cv.clientWidth || L.W;\n      k = (w / L.W) * Math.min(2, window.devicePixelRatio || 1);\n      cv.width = Math.round(L.W * k); cv.height = Math.round(L.H * k);\n      build();\n    };\n\n    // tilt is the shelf's angle in degrees, pos the bubble's distance from centre. Both carry a speed.\n    const s = { tilt: live.current.f * MAX_TILT, tv: 0, pos: -live.current.f * maxPos, pv: 0, light: 0 };\n\n    const vial = (x: number, y: number, upright: boolean, len: number, rad: number, pos: number, bw: number, stretch: number) => {\n      ctx.save(); ctx.translate(x, y); if (upright) ctx.rotate(-Math.PI / 2);\n      const hl = len / 2, r0 = rad * 0.86, cr = rad * 0.62, bulge = 2 * rad - r0;\n      const tube = () => {\n        ctx.beginPath(); ctx.moveTo(-hl + cr, -r0); ctx.quadraticCurveTo(0, -bulge, hl - cr, -r0); ctx.quadraticCurveTo(hl, -r0, hl, -r0 + cr); ctx.lineTo(hl, r0 - cr);\n        ctx.quadraticCurveTo(hl, r0, hl - cr, r0); ctx.quadraticCurveTo(0, bulge, -hl + cr, r0); ctx.quadraticCurveTo(-hl, r0, -hl, r0 - cr); ctx.lineTo(-hl, -r0 + cr); ctx.quadraticCurveTo(-hl, -r0, -hl + cr, -r0); ctx.closePath();\n      };\n      const liquid = ctx.createLinearGradient(0, -rad, 0, rad);\n      [0, 0.13, 0.42, 0.7, 0.86, 1].forEach((o, i) => liquid.addColorStop(o, SPIRIT[i]));\n      // The lit liquid throws a pale green glow on the white pocket behind it.\n      ctx.save(); ctx.shadowColor = \"rgba(92,120,0,0.5)\"; ctx.shadowBlur = 6 * k; ctx.shadowOffsetX = 2 * k; ctx.shadowOffsetY = 4 * k; tube(); ctx.fillStyle = liquid; ctx.fill(); ctx.restore();\n      ctx.save(); tube(); ctx.clip();\n      const ends = ctx.createLinearGradient(-hl, 0, hl, 0);\n      ends.addColorStop(0, \"rgba(50,70,0,0.6)\"); ends.addColorStop(0.1, \"rgba(50,70,0,0.06)\"); ends.addColorStop(0.9, \"rgba(50,70,0,0.06)\"); ends.addColorStop(1, \"rgba(50,70,0,0.6)\");\n      ctx.fillStyle = ends; ctx.fillRect(-hl, -rad, len, rad * 2);\n      if (!upright) {\n        // The two rings the bubble has to sit between.\n        for (const lx of [-(L.BW / 2 + 3.5), L.BW / 2 + 3.5]) {\n          ctx.fillStyle = \"rgba(255,255,220,0.5)\"; ctx.fillRect(lx + 0.9, -rad, 0.8, rad * 2);\n          ctx.fillStyle = \"#15170a\"; ctx.fillRect(lx - 0.8, -rad, 1.6, rad * 2);\n        }\n      }\n      // The bubble: pale inside, a dark refracted rim, and the lamp caught twice on its skin.\n      const w2 = (bw * stretch) / 2, bh = upright ? rad * 1.5 : rad * 1.16 / Math.sqrt(stretch);\n      const wall = -(r0 + (rad - r0) * (1 - (pos / hl) * (pos / hl)));\n      const cy = upright ? 0 : wall + bh / 2 + 1.4;\n      const air = ctx.createRadialGradient(pos - w2 * 0.2, cy - bh * 0.12, 0.5, pos, cy, w2);\n      air.addColorStop(0, \"rgba(253,255,236,0.98)\"); air.addColorStop(0.62, \"rgba(240,252,170,0.95)\"); air.addColorStop(0.84, \"rgba(150,180,24,0.95)\"); air.addColorStop(0.94, \"rgba(66,88,0,0.97)\"); air.addColorStop(1, \"rgba(206,236,70,0.92)\");\n      ctx.save(); ctx.translate(pos, cy); ctx.scale(1, bh / 2 / w2); ctx.translate(-pos, -cy);\n      ctx.beginPath(); ctx.arc(pos, cy, w2, 0, TAU); ctx.fillStyle = air; ctx.fill(); ctx.restore();\n      const hx = pos - w2 * (0.34 - s.light * 0.2);\n      ctx.beginPath(); ctx.ellipse(hx, cy - bh * 0.2, w2 * 0.3, bh * 0.1, -0.12, 0, TAU); ctx.fillStyle = \"rgba(255,255,255,0.96)\"; ctx.fill();\n      ctx.beginPath(); ctx.ellipse(pos + w2 * 0.46, cy + bh * 0.24, w2 * 0.13, bh * 0.07, -0.3, 0, TAU); ctx.fillStyle = \"rgba(255,255,255,0.6)\"; ctx.fill();\n      // The bubble works as a lens and drops a bright patch on the floor of the tube.\n      const lens = ctx.createRadialGradient(pos, rad * 0.62, 0, pos, rad * 0.62, w2 * 0.9);\n      lens.addColorStop(0, \"rgba(255,255,220,0.5)\"); lens.addColorStop(1, \"rgba(255,255,220,0)\");\n      ctx.fillStyle = lens; ctx.fillRect(pos - w2, rad * 0.2, w2 * 2, rad * 0.8);\n      // Glass: one long hard highlight high on the tube, one faint one low.\n      const run = ctx.createLinearGradient(-hl, 0, hl, 0), shift = s.light * 0.12;\n      run.addColorStop(0, \"rgba(255,255,255,0)\"); run.addColorStop(clamp(0.16 + shift, 0.02, 0.4), \"rgba(255,255,255,0.9)\"); run.addColorStop(clamp(0.6 + shift, 0.45, 0.8), \"rgba(255,255,255,0.55)\"); run.addColorStop(1, \"rgba(255,255,255,0)\");\n      rr(ctx, -hl + cr * 0.7, -rad * 0.74, len - cr * 1.4, rad * 0.17, rad * 0.085); ctx.fillStyle = run; ctx.fill();\n      ctx.globalAlpha = 0.34; rr(ctx, -hl + cr, rad * 0.66, len - cr * 2, rad * 0.08, rad * 0.04); ctx.fill(); ctx.globalAlpha = 1;\n      ctx.restore();\n      tube(); ctx.lineWidth = 0.9; ctx.strokeStyle = \"rgba(48,62,0,0.62)\"; ctx.stroke();\n      ctx.restore();\n    };\n\n    const draw = () => {\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.clearRect(0, 0, cv.width, cv.height);\n      ctx.setTransform(k, 0, 0, k, L.PX * k, L.PY * k);\n      const a = (s.tilt * Math.PI) / 180;\n      // Shadow on the wall, pushed down and to the right of the thing that throws it.\n      ctx.save(); ctx.translate(11, 20); ctx.rotate(a); ctx.drawImage(shade, -RW / 2, -OY, RW, RH); ctx.restore();\n      // The bracket the shelf pivots on. It stays put.\n      ctx.save(); ctx.shadowColor = \"rgba(54,40,18,0.45)\"; ctx.shadowBlur = 8 * k; ctx.shadowOffsetX = 5 * k; ctx.shadowOffsetY = 8 * k;\n      const arm = ctx.createLinearGradient(-12, 0, 12, 0);\n      arm.addColorStop(0, \"#5b5e63\"); arm.addColorStop(0.3, \"#888c91\"); arm.addColorStop(0.7, \"#4b4e52\"); arm.addColorStop(1, \"#2f3134\");\n      ctx.beginPath(); ctx.moveTo(-9, -4); ctx.lineTo(9, -4); ctx.lineTo(12, 50); ctx.quadraticCurveTo(12, 58, 4, 58); ctx.lineTo(-4, 58); ctx.quadraticCurveTo(-12, 58, -12, 50); ctx.closePath(); ctx.fillStyle = arm; ctx.fill(); ctx.restore();\n      ctx.beginPath(); ctx.moveTo(-8.2, 10); ctx.lineTo(-11.1, 50); ctx.lineWidth = 0.9; ctx.strokeStyle = \"rgba(255,255,255,0.5)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(0, 44, 4.2, 0, TAU); ctx.fillStyle = steel(ctx, 0, 44, 4.2); ctx.fill();\n      ctx.save(); ctx.translate(0, 44); ctx.rotate(0.6); ctx.fillStyle = \"rgba(10,10,10,0.8)\"; ctx.fillRect(-3.2, -0.6, 6.4, 1.2); ctx.restore();\n\n      ctx.save(); ctx.rotate(a);\n      ctx.drawImage(rig, -RW / 2, -OY, RW, RH);\n      const speed = Math.min(1, Math.abs(s.pv) / 220);\n      vial(0, vy, false, L.VL, L.VR, s.pos, L.BW, 1 + speed * 0.28);\n      for (const side of [-1, 1]) {\n        const vl = L.port * 1.46, vr = L.port * 0.33, bw = vr * 1.62;\n        vial(side * L.portAt, vy, true, vl, vr, vl / 2 - bw / 2 - 2.6 - Math.abs(s.tilt) * 0.3, bw, 1);\n      }\n      // A sheet of clear plastic over the centre window, catching a streak of light.\n      ctx.save(); rr(ctx, -winW / 2, vy - winH / 2, winW, winH, 6); ctx.clip();\n      const sx = -winW * 0.3 + s.light * winW * 0.14;\n      ctx.fillStyle = \"rgba(255,255,255,0.1)\";\n      ctx.beginPath(); ctx.moveTo(sx, vy - winH / 2); ctx.lineTo(sx + winW * 0.16, vy - winH / 2); ctx.lineTo(sx + winW * 0.16 - winH * 0.5, vy + winH / 2); ctx.lineTo(sx - winH * 0.5, vy + winH / 2); ctx.closePath(); ctx.fill();\n      ctx.restore();\n      ctx.restore();\n\n      // Pivot pin through the shelf.\n      ctx.save(); ctx.shadowColor = \"rgba(40,24,4,0.6)\"; ctx.shadowBlur = 3 * k; ctx.shadowOffsetY = 1.5 * k;\n      ctx.beginPath(); ctx.arc(0, 0, L.PT * 0.3, 0, TAU); ctx.fillStyle = steel(ctx, 0, 0, L.PT * 0.3); ctx.fill(); ctx.restore();\n      ctx.beginPath(); ctx.arc(0, 0, L.PT * 0.1, 0, TAU); ctx.fillStyle = \"rgba(20,20,20,0.75)\"; ctx.fill();\n    };\n\n    let raf = 0, last = 0, seen = true, moved = -1e9;\n    const frame = (now: number) => {\n      raf = 0;\n      if (!last) last = now;\n      const dt = Math.min(0.04, (now - last) / 1000); last = now;\n      for (let i = 0; i < 4; i++) {\n        const h = dt / 4;\n        s.tv += (38 * (live.current.f * MAX_TILT - s.tilt) - 4.4 * s.tv) * h; s.tilt += s.tv * h;\n        if (Math.abs(s.tilt) > MAX_TILT + 0.6) { s.tilt = Math.sign(s.tilt) * (MAX_TILT + 0.6); s.tv *= -0.35; }\n        // The bubble runs uphill and overshoots a little before the spirit drags it back.\n        const want = clamp((-s.tilt / MAX_TILT) * maxPos, -maxPos, maxPos);\n        s.pv += (74 * (want - s.pos) - 7.6 * s.pv) * h; s.pos += s.pv * h;\n        if (Math.abs(s.pos) > maxPos + 2) { s.pos = Math.sign(s.pos) * (maxPos + 2); s.pv *= -0.2; }\n      }\n      if (now - moved > 2500) s.light += (0.6 * Math.sin(now / 1900) - s.light) * Math.min(1, dt * 4);\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const still = () => { s.tilt = live.current.f * MAX_TILT; s.tv = 0; s.pos = -live.current.f * maxPos; s.pv = 0; draw(); };\n    const wake = (push?: number) => {\n      if (reduce) { still(); return; }\n      if (push) s.tv += push * 26; // a finger pressing one end of the shelf\n      if (!raf) { last = 0; raf = requestAnimationFrame(frame); }\n    };\n    api.current = { poke: wake };\n    fit(); wake();\n\n    const onLight = (e: PointerEvent) => { const r = cv.getBoundingClientRect(); moved = performance.now(); s.light = clamp(((e.clientX - r.left) / r.width) * 2 - 1, -1, 1); };\n    const onPress = (e: PointerEvent) => { const r = cv.getBoundingClientRect(); wake(e.clientX - r.left < r.width / 2 ? -1 : 1); };\n    cv.addEventListener(\"pointermove\", onLight); cv.addEventListener(\"pointerdown\", onPress);\n    const ro = new ResizeObserver(() => { fit(); if (reduce) still(); else draw(); });\n    ro.observe(cv);\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 () => {\n      cancelAnimationFrame(raf); api.current = null;\n      cv.removeEventListener(\"pointermove\", onLight); cv.removeEventListener(\"pointerdown\", onPress);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, [narrow]);\n\n  const L = layoutFor(narrow);\n  const f = lean(vals[0], vals[1]);\n  const isLevel = Math.abs(f) <= LEVEL_BAND;\n  const diff = Math.abs(vals[0] - vals[1]);\n  const status = isLevel ? \"Level\" : `${sides[vals[0] > vals[1] ? 0 : 1].label} ahead by ${diff}`;\n  const bump = (i: number, by: number) => {\n    live.current.touched = true; setTouched(true);\n    const next: [number, number] = [vals[0], vals[1]];\n    next[i] = clamp(next[i] + by, 0, 999);\n    show(next);\n  };\n  const key = \"flex h-11 w-11 items-center justify-center rounded-full border border-stone-400/70 bg-white/70 text-stone-800 shadow-[0_1px_0_rgba(255,255,255,0.9)_inset,0_1px_2px_rgba(60,45,20,0.18)] outline-none transition-transform duration-150 hover:bg-white active:scale-95 focus-visible:ring-2 focus-visible:ring-[#d4441f] focus-visible:ring-offset-2 focus-visible:ring-offset-[#e9e6df] motion-reduce:transition-none\";\n\n  return (\n    <div ref={stage} className=\"flex min-h-[720px] w-full flex-col items-center justify-center bg-[#e9e6df] bg-[radial-gradient(80%_70%_at_44%_36%,#f6f4ef_0%,#e9e6df_55%,#d6d2c8_100%)] px-4 py-12\">\n      <div className=\"w-full\" style={{ maxWidth: narrow ? L.W : 1100 }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"block w-full touch-manipulation\" style={{ aspectRatio: `${L.W} / ${L.H}` }} />\n      </div>\n      <div className=\"mt-2 grid w-full max-w-[640px] grid-cols-[1fr_auto_1fr] items-start gap-x-3\">\n        {sides.map((side, i) => (\n          <div key={side.label} className={`flex flex-col items-center gap-2 ${i === 1 ? \"order-3\" : \"order-1\"}`}>\n            <p className=\"font-mono text-[11px] uppercase tracking-[0.18em] text-stone-600\">{side.label}</p>\n            <p className=\"text-5xl font-semibold tabular-nums tracking-tight text-stone-900 sm:text-6xl\">{vals[i]}</p>\n            <div className=\"mt-1 flex gap-2\">\n              <button type=\"button\" onClick={() => bump(i, -1)} aria-label={`One fewer ${side.label.toLowerCase()}`} className={key}>\n                <svg aria-hidden=\"true\" viewBox=\"0 0 20 20\" className=\"h-4 w-4\"><path d=\"M4 10h12\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" /></svg>\n              </button>\n              <button type=\"button\" onClick={() => bump(i, 1)} aria-label={`One more ${side.label.toLowerCase()}`} className={key}>\n                <svg aria-hidden=\"true\" viewBox=\"0 0 20 20\" className=\"h-4 w-4\"><path d=\"M4 10h12M10 4v12\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" /></svg>\n              </button>\n            </div>\n          </div>\n        ))}\n        <div className=\"order-2 flex flex-col items-center gap-2 pt-7 text-center\">\n          <p aria-live={touched ? \"polite\" : \"off\"} className={`rounded-full px-3 py-1.5 font-mono text-[11px] uppercase tracking-[0.14em] ${isLevel ? \"bg-[#2c3a05] text-[#eaff8a]\" : \"bg-stone-900/10 text-stone-800\"}`}>{status}</p>\n          <p className=\"max-w-[11rem] font-mono text-[10px] uppercase leading-4 tracking-[0.16em] text-stone-600\">{CAPTION}</p>\n        </div>\n      </div>\n    </div>\n  );\n}\n"}]}
