{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"brass-orrery-schedule-viewer-with-stone-planets-on-geared-arms-that-swing-under-a-due-pointer","type":"registry:component","title":"Brass Orrery Schedule Viewer with Stone Planets on Geared Arms That Swing Under a Due Pointer","description":"Schedule widget drawn as a brass orrery: each repeating job is a stone planet, due when it swings under the pointer. React code and HTML, free to copy.","dependencies":[],"files":[{"path":"components/we-inc/brass-orrery-schedule-viewer-with-stone-planets-on-geared-arms-that-swing-under-a-due-pointer.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport type { KeyboardEvent as ReactKeyboardEvent } from \"react\";\n\n// Each planet is one repeating job. `every` is its cycle in days, `first` is any date it fell due.\n// Stones: \"moonstone\", \"carnelian\", \"malachite\" or \"lapis\". Three or four entries fit the plate.\nconst cycles = [\n  { name: \"Team review\", every: 7, first: \"2026-01-02\", stone: \"moonstone\", dot: \"#e6eaf2\" },\n  { name: \"Sprint demo\", every: 14, first: \"2026-01-08\", stone: \"carnelian\", dot: \"#e2622c\" },\n  { name: \"Payroll run\", every: 28, first: \"2026-01-23\", stone: \"malachite\", dot: \"#35a873\" },\n  { name: \"Board report\", every: 91, first: \"2026-01-19\", stone: \"lapis\", dot: \"#4a72dc\" },\n];\nconst TURN = 7; // days that pass for one full turn of your hand\nconst CRUISE = 1.15; // days per second while the orrery runs by itself\nconst JEWEL = \"#ff3b3b\"; // the stone in the pointer that lights when something falls due\n\nconst SIZE = 600, C = 300;\nconst DOW = [\"Sun\", \"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\"];\nconst MON = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"];\nconst MLEN = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];\nconst DAY = 864e5;\nconst midnight = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate());\nconst fromIso = (iso: string) => { const [y, m, d] = iso.split(\"-\").map(Number); return new Date(y, m - 1, d); };\nconst mod = (a: number, n: number) => ((a % n) + n) % n;\nconst orbit = (i: number) => 100 + i * 34;\nconst ballSize = (i: number) => [12.5, 15.5, 18.5, 15][i] ?? 14;\n\nexport default function BrassOrrerySchedule() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const pad = useRef<HTMLDivElement>(null);\n  const [today] = useState(() => midnight(new Date()));\n  // Days from today until each job is next due.\n  const [due0] = useState(() => cycles.map((c) => mod(Math.round((fromIso(c.first).getTime() - today.getTime()) / DAY), c.every)));\n  const [day, setDay] = useState(0);\n  const dayRef = useRef(0);\n  const api = useRef<{ go: (d: number) => void; aim: () => number } | null>(null);\n\n  useEffect(() => {\n    const cv = canvas.current, el = pad.current;\n    if (!cv || !el) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const dpr = Math.min(2, window.devicePixelRatio || 1);\n    cv.width = SIZE * dpr; cv.height = SIZE * dpr;\n    const TAU = Math.PI * 2, LIGHT = -2.2;\n    const clamp = (v: number) => Math.max(0, Math.min(1, v));\n    const ramp = (stops: number[][]) => (v: number, a = 1) => {\n      const x = clamp(v) * (stops.length - 1), i = Math.min(stops.length - 2, Math.floor(x)), f = x - i;\n      const p = stops[i], q = stops[i + 1];\n      return `rgba(${(p[0] + (q[0] - p[0]) * f) | 0},${(p[1] + (q[1] - p[1]) * f) | 0},${(p[2] + (q[2] - p[2]) * f) | 0},${a})`;\n    };\n    const brass = ramp([[30, 20, 6], [96, 66, 18], [176, 132, 48], [232, 196, 108], [255, 247, 218]]);\n    const silver = ramp([[46, 50, 58], [112, 116, 124], [172, 175, 180], [214, 216, 218], [248, 248, 246]]);\n    const BANDS: [number, number][] = [[0, 1], [0.03, 0.96], [0.085, 0.6], [0.16, 0.3], [0.23, 0.1], [0.31, 0.24], [0.4, 0.52], [0.455, 0.76], [0.5, 0.94], [0.54, 0.72], [0.6, 0.44], [0.69, 0.14], [0.76, 0.3], [0.84, 0.42], [0.92, 0.72], [0.97, 0.95], [1, 1]];\n    // Turned metal: bright and dark wedges that meet at the centre and never rotate with the part.\n    const sheen = (x: CanvasRenderingContext2D, cx: number, cy: number, ang: number, lo: number, hi: number, col: (v: number, a?: number) => string) => {\n      if (typeof x.createConicGradient !== \"function\") return col((lo + hi) / 2);\n      const g = x.createConicGradient(ang, cx, cy);\n      for (const [p, v] of BANDS) g.addColorStop(p, col(lo + (hi - lo) * v));\n      return g;\n    };\n    const layer = (w = SIZE, h = SIZE) => { const c = document.createElement(\"canvas\"); c.width = w * dpr; c.height = h * dpr; const x = c.getContext(\"2d\")!; x.scale(dpr, dpr); return { c, x }; };\n    const disc = (x: CanvasRenderingContext2D, cx: number, cy: number, r: number) => { x.beginPath(); x.arc(cx, cy, r, 0, TAU); };\n    const band = (x: CanvasRenderingContext2D, r0: number, r1: number) => { x.beginPath(); x.arc(C, C, r1, 0, TAU); x.arc(C, C, r0, 0, TAU, true); };\n    // Letters set along a circle, each advanced by its own width. `flip` reads along the bottom of the circle.\n    const arcText = (x: CanvasRenderingContext2D, text: string, r: number, mid: number, track: number, flip = false) => {\n      const ws = [...text].map((ch) => x.measureText(ch).width + track), total = ws.reduce((a, b) => a + b, 0);\n      let at = -total / 2;\n      [...text].forEach((ch, i) => {\n        const a = mid + ((at + ws[i] / 2) / r) * (flip ? -1 : 1); at += ws[i];\n        x.save(); x.translate(C, C); x.rotate(a); x.translate(0, flip ? r : -r); x.fillText(ch, 0, 0); x.restore();\n      });\n    };\n    let seed = 11;\n    const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n\n    // ---- The plate: brass rim, lacquered field, inlaid orbits. Drawn once.\n    const base = layer();\n    {\n      const x = base.x;\n      disc(x, C, C, 290); x.fillStyle = \"#120c03\"; x.fill();\n      band(x, 264, 289); x.fillStyle = sheen(x, C, C, LIGHT, 0.08, 1, brass); x.fill();\n      band(x, 268.5, 284); x.fillStyle = sheen(x, C, C, LIGHT + 0.5, 0.14, 0.9, brass); x.fill();\n      // coin edge\n      for (let k = 0; k < 240; k++) { const a = (k / 240) * TAU, sn = Math.sin(a), cs = Math.cos(a); x.beginPath(); x.moveTo(C + sn * 285.2, C - cs * 285.2); x.lineTo(C + sn * 289, C - cs * 289); x.lineWidth = 1.1; x.strokeStyle = \"rgba(20,12,0,0.5)\"; x.stroke(); }\n      // fine circular brushing on the rim\n      for (let r = 269.5; r < 284; r += 0.7) { const v = rand(); x.beginPath(); x.arc(C, C, r, 0, TAU); x.lineWidth = 0.5; x.strokeStyle = v > 0.5 ? `rgba(255,244,200,${(v - 0.5) * 0.22})` : `rgba(40,24,0,${(0.5 - v) * 0.3})`; x.stroke(); }\n      for (const [r, c] of [[268.5, \"rgba(30,18,0,0.8)\"], [284, \"rgba(30,18,0,0.8)\"], [269.5, \"rgba(255,246,210,0.35)\"], [285, \"rgba(255,246,210,0.55)\"], [289.4, \"rgba(0,0,0,0.9)\"]] as [number, string][]) { x.beginPath(); x.arc(C, C, r, 0, TAU); x.lineWidth = 0.9; x.strokeStyle = c; x.stroke(); }\n      // engraved legend on the rim\n      x.font = \"700 8.5px Georgia, 'Times New Roman', serif\"; x.textAlign = \"center\"; x.textBaseline = \"middle\";\n      const legend = `ONE TURN OF THE HAND  ·  ${TURN} DAYS`;\n      x.fillStyle = \"rgba(255,244,205,0.55)\"; x.save(); x.translate(0.5, 0.7); arcText(x, legend, 276.5, 0, 1.6, true); x.restore();\n      x.fillStyle = \"rgba(34,20,2,0.92)\"; arcText(x, legend, 276.5, 0, 1.6, true);\n      // lacquered field\n      const lac = x.createRadialGradient(C - 70, C - 90, 10, C, C, 240);\n      lac.addColorStop(0, \"#1f3157\"); lac.addColorStop(0.5, \"#131f3b\"); lac.addColorStop(1, \"#090f1f\");\n      disc(x, C, C, 226); x.fillStyle = lac; x.fill();\n      x.save(); disc(x, C, C, 226); x.clip();\n      const gloss = x.createRadialGradient(C - 110, C - 150, 0, C - 110, C - 150, 230);\n      gloss.addColorStop(0, \"rgba(170,200,255,0.2)\"); gloss.addColorStop(0.5, \"rgba(170,200,255,0.05)\"); gloss.addColorStop(1, \"rgba(170,200,255,0)\");\n      x.fillStyle = gloss; x.fillRect(0, 0, SIZE, SIZE);\n      // the raised ring throws a shadow onto the near edge of the field\n      const well = x.createRadialGradient(C + 7, C + 10, 196, C + 7, C + 10, 232);\n      well.addColorStop(0, \"rgba(0,0,0,0)\"); well.addColorStop(1, \"rgba(0,0,0,0.72)\");\n      x.fillStyle = well; x.fillRect(0, 0, SIZE, SIZE);\n      x.restore();\n      // inlaid orbits with one pin for each day (or each week on the long cycles)\n      cycles.forEach((c, i) => {\n        const r = orbit(i), n = c.every <= 28 ? c.every : Math.round(c.every / 7);\n        x.beginPath(); x.arc(C, C, r, 0, TAU); x.lineWidth = 1.1; x.strokeStyle = \"rgba(4,7,16,0.8)\"; x.stroke();\n        x.beginPath(); x.arc(C, C, r - 0.6, 0, TAU); x.lineWidth = 0.8; x.strokeStyle = \"rgba(214,172,86,0.62)\"; x.stroke();\n        for (let k = 0; k < n; k++) {\n          const a = (k / n) * TAU, px = C + Math.sin(a) * r, py = C - Math.cos(a) * r, pr = k === 0 ? 3 : 1.7;\n          disc(x, px + 0.5, py + 0.8, pr + 0.4); x.fillStyle = \"rgba(0,0,0,0.6)\"; x.fill();\n          const g = x.createRadialGradient(px - pr * 0.4, py - pr * 0.5, 0, px, py, pr);\n          g.addColorStop(0, \"#fff3c4\"); g.addColorStop(0.5, \"#d2a548\"); g.addColorStop(1, \"#6a4810\");\n          disc(x, px, py, pr); x.fillStyle = g; x.fill();\n        }\n      });\n      // the due line, inlaid from the hub to the ring\n      x.lineCap = \"round\";\n      x.beginPath(); x.moveTo(C + 0.6, C - 222); x.lineTo(C + 0.6, C - 62); x.lineWidth = 2.2; x.strokeStyle = \"rgba(0,0,0,0.6)\"; x.stroke();\n      const inlay = x.createLinearGradient(0, C - 222, 0, C - 62);\n      inlay.addColorStop(0, \"#f6dc8e\"); inlay.addColorStop(1, \"#a97c26\");\n      x.beginPath(); x.moveTo(C, C - 222); x.lineTo(C, C - 62); x.lineWidth = 1.5; x.strokeStyle = inlay; x.stroke();\n      // step between the field and the calendar ring\n      band(x, 222, 227); x.fillStyle = sheen(x, C, C, LIGHT + 0.9, 0.25, 1, brass); x.fill();\n      x.beginPath(); x.arc(C, C, 222, 0, TAU); x.lineWidth = 1; x.strokeStyle = \"rgba(0,0,0,0.75)\"; x.stroke();\n    }\n\n    // ---- The calendar ring: silvered, engraved with the year. Drawn once, turned every frame.\n    const cal = layer();\n    {\n      const x = cal.x;\n            x.save(); band(x, 227, 264); x.clip();\n      for (let k = 0; k < 900; k++) { // radial graining\n        const a = rand() * TAU, r0 = 227 + rand() * 30, len = 3 + rand() * 9, v = rand();\n        x.beginPath(); x.moveTo(C + Math.sin(a) * r0, C - Math.cos(a) * r0); x.lineTo(C + Math.sin(a) * (r0 + len), C - Math.cos(a) * (r0 + len));\n        x.lineWidth = 0.5; x.strokeStyle = v > 0.5 ? `rgba(255,255,255,${(v - 0.5) * 0.3})` : `rgba(40,44,52,${(0.5 - v) * 0.16})`; x.stroke();\n      }\n      x.restore();\n      x.font = \"700 12.5px Georgia, 'Times New Roman', serif\"; x.textAlign = \"center\"; x.textBaseline = \"middle\";\n      let start = 0;\n      const ink = \"rgba(22,18,12,0.94)\", lite = \"rgba(255,255,255,0.75)\";\n      const tick = (a: number, r0: number, r1: number, w: number) => {\n        const s = Math.sin(a), co = -Math.cos(a);\n        x.lineCap = \"butt\"; x.lineWidth = w;\n        x.strokeStyle = lite; x.beginPath(); x.moveTo(C + s * r0 + 0.5, C + co * r0 + 0.6); x.lineTo(C + s * r1 + 0.5, C + co * r1 + 0.6); x.stroke();\n        x.strokeStyle = ink; x.beginPath(); x.moveTo(C + s * r0, C + co * r0); x.lineTo(C + s * r1, C + co * r1); x.stroke();\n      };\n      MLEN.forEach((len, m) => {\n        for (let d = 0; d < len; d++) {\n          const a = ((start + d) / 365) * TAU;\n          if (d === 0) tick(a, 231, 264, 1.3); else tick(a, d % 10 === 0 ? 252 : d % 5 === 0 ? 255 : 258, 264, d % 10 === 0 ? 0.9 : 0.6);\n        }\n        const mid = ((start + len / 2) / 365) * TAU;\n        x.fillStyle = lite; x.save(); x.translate(0.5, 0.7); arcText(x, MON[m].toUpperCase(), 241, mid, 3); x.restore();\n        x.fillStyle = ink; arcText(x, MON[m].toUpperCase(), 241, mid, 3);\n        start += len;\n      });\n    }\n\n    // ---- Stone balls, drawn once each.\n    const stones = cycles.map((c, i) => {\n      const r = ballSize(i), half = Math.ceil(r + 3), L = layer(half * 2, half * 2), x = L.x;\n      x.translate(half, half);\n      const pal: Record<string, string[]> = {\n        moonstone: [\"#ffffff\", \"#dfe5f0\", \"#9aa6bd\", \"#4a556d\"], carnelian: [\"#ffc59a\", \"#e2622c\", \"#9a2a0c\", \"#3f0e02\"],\n        malachite: [\"#a6f0c8\", \"#2f9e6b\", \"#0f5c3a\", \"#04271a\"], lapis: [\"#9db6ff\", \"#3558c4\", \"#152a78\", \"#070f33\"],\n      };\n      const p = pal[c.stone] ?? pal.lapis;\n      const g = x.createRadialGradient(-r * 0.38, -r * 0.44, r * 0.05, -r * 0.1, -r * 0.12, r * 1.18);\n      g.addColorStop(0, p[0]); g.addColorStop(0.28, p[1]); g.addColorStop(0.72, p[2]); g.addColorStop(1, p[3]);\n      disc(x, 0, 0, r); x.fillStyle = g; x.fill();\n      x.save(); disc(x, 0, 0, r); x.clip();\n      if (c.stone === \"malachite\") for (let k = 1; k < 9; k++) { x.beginPath(); x.arc(r * 0.5, r * 0.7, k * r * 0.24, 0, TAU); x.lineWidth = r * 0.09; x.strokeStyle = k % 2 ? \"rgba(3,40,24,0.38)\" : \"rgba(170,255,210,0.2)\"; x.stroke(); }\n      if (c.stone === \"lapis\") for (let k = 0; k < 16; k++) { const a = rand() * TAU, d = Math.sqrt(rand()) * r * 0.9; disc(x, Math.cos(a) * d, Math.sin(a) * d, 0.35 + rand() * 0.6); x.fillStyle = `rgba(246,214,130,${0.5 + rand() * 0.5})`; x.fill(); }\n      if (c.stone === \"carnelian\") for (let k = 0; k < 3; k++) { x.beginPath(); x.ellipse(-r * 0.2, r * (k * 0.5 - 0.3), r * 1.4, r * 0.12, 0.5, 0, TAU); x.fillStyle = \"rgba(255,214,170,0.16)\"; x.fill(); }\n      if (c.stone === \"moonstone\") { const m = x.createRadialGradient(r * 0.25, r * 0.3, 0, r * 0.25, r * 0.3, r * 0.8); m.addColorStop(0, \"rgba(140,190,255,0.5)\"); m.addColorStop(1, \"rgba(140,190,255,0)\"); x.fillStyle = m; x.fillRect(-r, -r, r * 2, r * 2); }\n      const dark = x.createRadialGradient(-r * 0.3, -r * 0.36, r * 0.5, -r * 0.12, -r * 0.14, r * 1.12);\n      dark.addColorStop(0, \"rgba(0,0,0,0)\"); dark.addColorStop(1, \"rgba(0,0,8,0.62)\");\n      x.fillStyle = dark; x.fillRect(-r, -r, r * 2, r * 2);\n      const bounce = x.createRadialGradient(r * 0.62, r * 0.7, 0, r * 0.62, r * 0.7, r * 0.6);\n      bounce.addColorStop(0, \"rgba(160,190,255,0.3)\"); bounce.addColorStop(1, \"rgba(160,190,255,0)\");\n      x.fillStyle = bounce; x.fillRect(-r, -r, r * 2, r * 2);\n      x.restore();\n      x.save(); x.translate(-r * 0.4, -r * 0.46); x.rotate(-0.7); x.shadowColor = \"rgba(255,255,255,0.9)\"; x.shadowBlur = 3 * dpr;\n      x.beginPath(); x.ellipse(0, 0, r * 0.24, r * 0.13, 0, 0, TAU); x.fillStyle = \"rgba(255,255,255,0.95)\"; x.fill(); x.restore();\n      disc(x, 0, 0, r - 0.3); x.lineWidth = 0.6; x.strokeStyle = \"rgba(0,0,0,0.5)\"; x.stroke();\n      // the brass pin that holds the ball on its arm\n      const pr = Math.max(1.7, r * 0.15);\n      disc(x, 0.3, 0.5, pr + 0.7); x.fillStyle = \"rgba(0,0,0,0.55)\"; x.fill();\n      const pg = x.createRadialGradient(-pr * 0.4, -pr * 0.5, 0, 0, 0, pr);\n      pg.addColorStop(0, \"#fff6cf\"); pg.addColorStop(0.55, \"#cfa046\"); pg.addColorStop(1, \"#5e3f0c\");\n      disc(x, 0, 0, pr); x.fillStyle = pg; x.fill();\n      return { c: L.c, half, r };\n    });\n\n    const s = { day: 0, v: 0, target: null as number | null, drag: false, jewel: 0 };\n    const angleOf = (i: number, d: number) => ((d - due0[i]) / cycles[i].every) * TAU;\n    const base0 = Math.round((today.getTime() - new Date(today.getFullYear(), 0, 1).getTime()) / DAY);\n\n    const shade = (fn: () => void, dx: number, dy: number, blur: number, a: number) => {\n      ctx.save(); ctx.shadowColor = `rgba(0,0,6,${a})`; ctx.shadowBlur = blur * dpr; ctx.shadowOffsetX = (dx + 2000) * dpr; ctx.shadowOffsetY = dy * dpr;\n      ctx.translate(-2000, 0); ctx.fillStyle = \"#000\"; ctx.strokeStyle = \"#000\"; fn(); ctx.restore();\n    };\n    const gearPath = (cx: number, cy: number, rp: number, teeth: number, rot: number, spokes: number) => {\n      const st = TAU / teeth, r0 = rp - 1.7, r1 = rp + 1.9;\n      ctx.beginPath();\n      for (let k = 0; k < teeth; k++) {\n        const a = rot + k * st;\n        for (const [da, rr] of [[-0.29, r0], [-0.13, r1], [0.13, r1], [0.29, r0]] as [number, number][]) ctx.lineTo(cx + Math.cos(a + da * st) * rr, cy + Math.sin(a + da * st) * rr);\n      }\n      ctx.closePath();\n      const w = (TAU / spokes) * 0.36;\n      for (let k = 0; k < spokes; k++) {\n        const a = rot + (k / spokes) * TAU;\n        ctx.moveTo(cx + Math.cos(a - w) * rp * 0.8, cy + Math.sin(a - w) * rp * 0.8);\n        ctx.arc(cx, cy, rp * 0.8, a - w, a + w); ctx.arc(cx, cy, rp * 0.4, a + w * 0.8, a - w * 0.8, true); ctx.closePath();\n      }\n    };\n    const gear = (cx: number, cy: number, rp: number, teeth: number, rot: number, spokes: number, tone: number) => {\n      shade(() => { gearPath(cx, cy, rp, teeth, rot, spokes); ctx.fill(\"evenodd\"); }, 2.4, 3.6, 3.5, 0.75);\n      gearPath(cx + 0.7, cy + 1.1, rp, teeth, rot, spokes); ctx.fillStyle = \"#3a2706\"; ctx.fill(\"evenodd\");\n      gearPath(cx, cy, rp, teeth, rot, spokes); ctx.fillStyle = sheen(ctx, cx, cy, LIGHT + tone, 0.16, 0.94, brass); ctx.fill(\"evenodd\");\n      ctx.lineWidth = 0.5; ctx.strokeStyle = \"rgba(30,18,0,0.7)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(cx, cy, rp * 0.87, 0, TAU); ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(30,18,0,0.55)\"; ctx.stroke();\n      ctx.beginPath(); ctx.arc(cx, cy, rp * 0.87 + 0.9, 0, TAU); ctx.strokeStyle = \"rgba(255,246,206,0.4)\"; ctx.stroke();\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.drawImage(base.c, 0, 0);\n      // the year ring turns so that the date sits under the pointer\n      const ring = -(mod(base0 + s.day, 365) / 365) * TAU;\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      band(ctx, 227, 264); ctx.fillStyle = sheen(ctx, C, C, LIGHT + 0.2, 0.42, 1, silver); ctx.fill();\n      ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.translate(C * dpr, C * dpr); ctx.rotate(ring); ctx.drawImage(cal.c, -C * dpr, -C * dpr);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      const lip = ctx.createRadialGradient(C, C, 227, C, C, 264);\n      lip.addColorStop(0, \"rgba(0,0,0,0.42)\"); lip.addColorStop(0.12, \"rgba(0,0,0,0)\"); lip.addColorStop(0.8, \"rgba(0,0,0,0)\"); lip.addColorStop(1, \"rgba(0,0,0,0.5)\");\n      band(ctx, 227, 264); ctx.fillStyle = lip; ctx.fill();\n\n      // going train under the hub\n      const a0 = angleOf(0, s.day) - Math.PI / 2;\n      gear(C, C, 52, 48, a0, 6, 0.3);\n      for (const phi of [2.5, 5.7]) gear(C + Math.cos(phi) * 68.25, C + Math.sin(phi) * 68.25, 16.25, 15, -(48 / 15) * (a0 - phi) + phi + Math.PI + Math.PI / 15, 5, 1.4);\n\n      // shadows first, so every arm and ball sits above every shadow\n      const pos = cycles.map((_, i) => { const a = angleOf(i, s.day); return { a, x: C + Math.sin(a) * orbit(i), y: C - Math.cos(a) * orbit(i), h: 12 + (cycles.length - 1 - i) * 7 }; });\n      pos.forEach((p, i) => {\n        shade(() => { ctx.lineWidth = 5; ctx.lineCap = \"round\"; ctx.beginPath(); ctx.moveTo(C, C); ctx.lineTo(p.x, p.y); ctx.stroke(); }, p.h * 0.4, p.h * 0.62, 3 + p.h * 0.12, 0.42);\n        const hh = p.h + stones[i].r;\n        shade(() => { disc(ctx, p.x, p.y, stones[i].r * 0.96); ctx.fill(); }, hh * 0.4, hh * 0.62, 4 + hh * 0.14, 0.62);\n      });\n      for (let i = cycles.length - 1; i >= 0; i--) {\n        const p = pos[i], sx = Math.sin(p.a), sy = -Math.cos(p.a), nx = -sy, ny = sx;\n        const facing = nx * -0.56 + ny * -0.83; // which long edge of the bar looks at the lamp\n        const w0 = 3.8, w1 = 2.5, r0 = 10, r1 = orbit(i);\n        const quad = (o: number, k: number) => { ctx.beginPath(); ctx.moveTo(C + sx * r0 + nx * (w0 * k + o), C + sy * r0 + ny * (w0 * k + o)); ctx.lineTo(C + sx * r1 + nx * (w1 * k + o), C + sy * r1 + ny * (w1 * k + o)); ctx.lineTo(C + sx * r1 - nx * (w1 * k - o), C + sy * r1 - ny * (w1 * k - o)); ctx.lineTo(C + sx * r0 - nx * (w0 * k - o), C + sy * r0 - ny * (w0 * k - o)); ctx.closePath(); };\n        quad(0, 1.25); ctx.fillStyle = \"rgba(26,16,2,0.95)\"; ctx.fill();\n        quad(0, 1); ctx.fillStyle = brass(0.5 + 0.12 * Math.abs(facing)); ctx.fill();\n        quad(facing > 0 ? 1.5 : -1.5, 0.42); ctx.fillStyle = brass(0.97, 0.95); ctx.fill();\n        quad(facing > 0 ? -1.8 : 1.8, 0.38); ctx.fillStyle = brass(0.2, 0.9); ctx.fill();\n        // collar where the arm takes the ball\n        disc(ctx, p.x, p.y, stones[i].r * 0.55 + 2); ctx.fillStyle = brass(0.3); ctx.fill();\n        const st = stones[i];\n        ctx.drawImage(st.c, (p.x - st.half), (p.y - st.half), st.half * 2, st.half * 2);\n      }\n\n      // stacked collars and the polished sun\n      for (const [r, off, lo] of [[35, 0, 0.12], [29, 0.8, 0.2], [23.5, 1.7, 0.1]] as [number, number, number][]) {\n        shade(() => { disc(ctx, C, C, r); ctx.fill(); }, 2.2, 3.4, 4, 0.7);\n        disc(ctx, C, C, r); ctx.fillStyle = sheen(ctx, C, C, LIGHT + off, lo, 1, brass); ctx.fill();\n        ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(24,14,0,0.8)\"; ctx.stroke();\n        ctx.beginPath(); ctx.arc(C, C, r - 1.4, 0, TAU); ctx.strokeStyle = \"rgba(255,247,214,0.5)\"; ctx.stroke();\n      }\n      shade(() => { disc(ctx, C, C, 19); ctx.fill(); }, 3.4, 5.2, 4.5, 0.85);\n      const sun = ctx.createRadialGradient(C - 7, C - 8.4, 0.5, C - 1.8, C - 2.4, 23);\n      sun.addColorStop(0, \"#fffbe6\"); sun.addColorStop(0.16, \"#ffe289\"); sun.addColorStop(0.45, \"#c9962c\"); sun.addColorStop(0.78, \"#5d3f08\"); sun.addColorStop(1, \"#231501\");\n      disc(ctx, C, C, 20); ctx.fillStyle = sun; ctx.fill();\n      ctx.save(); disc(ctx, C, C, 20); ctx.clip();\n      const under = ctx.createRadialGradient(C + 11, C + 13, 0, C + 11, C + 13, 13);\n      under.addColorStop(0, \"rgba(255,206,120,0.55)\"); under.addColorStop(1, \"rgba(255,206,120,0)\");\n      ctx.fillStyle = under; ctx.fillRect(C - 24, C - 24, 48, 48);\n      // each stone shows as a small coloured glint in the polished ball\n      pos.forEach((p, i) => { disc(ctx, C + Math.sin(p.a) * 15.4, C - Math.cos(p.a) * 15.4, 1.8 + i * 0.3); ctx.fillStyle = cycles[i].dot; ctx.globalAlpha = 0.42; ctx.fill(); });\n      ctx.restore();\n      ctx.save(); ctx.translate(C - 7.6, C - 8.8); ctx.rotate(-0.7); ctx.shadowColor = \"#fff\"; ctx.shadowBlur = 5 * dpr;\n      ctx.beginPath(); ctx.ellipse(0, 0, 5.4, 3, 0, 0, TAU); ctx.fillStyle = \"rgba(255,255,255,0.96)\"; ctx.fill(); ctx.restore();\n\n      // blued steel pointer, its brass foot and the jewel\n      const tip = () => { ctx.beginPath(); ctx.moveTo(C - 4.2, C - 278); ctx.lineTo(C - 0.9, C - 229); ctx.lineTo(C + 0.9, C - 229); ctx.lineTo(C + 4.2, C - 278); ctx.closePath(); };\n      shade(() => { tip(); ctx.fill(); }, 2, 3, 2.5, 0.6);\n      const blue = ctx.createLinearGradient(C - 4, 0, C + 4, 0);\n      blue.addColorStop(0, \"#5e86e6\"); blue.addColorStop(0.42, \"#22366e\"); blue.addColorStop(0.58, \"#0f1a3c\"); blue.addColorStop(1, \"#2c4489\");\n      tip(); ctx.fillStyle = blue; ctx.fill(); ctx.lineWidth = 0.5; ctx.strokeStyle = \"rgba(0,0,10,0.8)\"; ctx.stroke();\n      const foot = () => { ctx.beginPath(); ctx.moveTo(C - 11, C - 293); ctx.lineTo(C + 11, C - 293); ctx.lineTo(C + 11, C - 277); ctx.quadraticCurveTo(C, C - 268, C - 11, C - 277); ctx.closePath(); };\n      shade(() => { foot(); ctx.fill(); }, 1.6, 2.4, 2.5, 0.7);\n      const fg = ctx.createLinearGradient(C - 11, C - 293, C + 11, C - 270);\n      fg.addColorStop(0, \"#fff0b8\"); fg.addColorStop(0.45, \"#c2932f\"); fg.addColorStop(1, \"#6d4c10\");\n      foot(); ctx.fillStyle = fg; ctx.fill(); ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(24,14,0,0.85)\"; ctx.stroke();\n      const jy = C - 282.5, on = s.jewel;\n      disc(ctx, C, jy, 5.6); ctx.fillStyle = \"#1a0f02\"; ctx.fill();\n      ctx.save(); ctx.shadowColor = JEWEL; ctx.shadowBlur = 18 * on * dpr;\n      const ruby = ctx.createRadialGradient(C - 1.5, jy - 1.8, 0, C, jy, 4.6);\n      ruby.addColorStop(0, on > 0.5 ? \"#ffd0c8\" : \"#d06a6a\"); ruby.addColorStop(0.45, JEWEL); ruby.addColorStop(1, \"#3c0006\");\n      disc(ctx, C, jy, 4.5); ctx.globalAlpha = 0.5 + 0.5 * on; ctx.fillStyle = ruby; ctx.fill(); ctx.restore();\n      disc(ctx, C - 1.4, jy - 1.6, 1); ctx.fillStyle = `rgba(255,255,255,${0.45 + 0.5 * on})`; ctx.fill();\n    };\n\n    let raf = 0, last = 0, seen = true, cruise = CRUISE !== 0, lastAngle = 0; // the cruise ends for good at the first touch\n    const publish = () => { const d = Math.round(s.day); if (d !== dayRef.current) { dayRef.current = d; setDay(d); } };\n    const dueNow = () => cycles.some((c, i) => Math.abs(mod(s.day - due0[i] + c.every / 2, c.every) - c.every / 2) < 0.5);\n    const frame = (now: number) => {\n      raf = 0;\n      if (!last) last = now;\n      const dt = Math.min(0.033, (now - last) / 1000); last = now;\n      if (!s.drag) {\n        if (s.target !== null) {\n          s.v += (64 * (s.target - s.day) - 13 * s.v) * dt; s.day += s.v * dt;\n          if (Math.abs(s.target - s.day) < 0.002 && Math.abs(s.v) < 0.02) { s.day = s.target; s.v = 0; }\n        } else if (cruise) {\n          s.v += (CRUISE - s.v) * Math.min(1, dt * 1.4); s.day += s.v * dt;\n        } else {\n          // free running after a throw: the flywheel slows, then the nearest day catches it\n          s.v *= Math.exp(-1.5 * dt); s.day += s.v * dt;\n          if (Math.abs(s.v) < 1.4) s.target = Math.round(s.day + s.v * 0.3);\n        }\n      }\n      s.jewel += ((dueNow() ? 1 : 0) - s.jewel) * Math.min(1, dt * 9);\n      publish(); draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = 0; raf = requestAnimationFrame(frame); } };\n    const settle = () => { if (s.target !== null) s.day = s.target; s.day = Math.round(s.day); s.v = 0; s.target = null; s.jewel = dueNow() ? 1 : 0; publish(); draw(); };\n    api.current = { go: (d) => { cruise = false; s.target = d; if (reduce) settle(); else wake(); }, aim: () => s.target ?? Math.round(s.day) };\n    if (reduce) settle(); else wake();\n\n    const angleAt = (e: PointerEvent) => { const b = el.getBoundingClientRect(); return Math.atan2(e.clientX - (b.left + b.width / 2), -(e.clientY - (b.top + b.height / 2))); };\n    const onDown = (e: PointerEvent) => { cruise = false; s.drag = true; s.target = null; s.v = 0; lastAngle = angleAt(e); el.setPointerCapture(e.pointerId); wake(); };\n    let lastMove = 0;\n    const onMove = (e: PointerEvent) => {\n      if (!s.drag) return;\n      const now = performance.now(), a = angleAt(e); let d = a - lastAngle; if (d > Math.PI) d -= TAU; if (d < -Math.PI) d += TAU;\n      lastAngle = a;\n      const dd = (d / TAU) * TURN, dt = Math.max(0.008, (now - lastMove) / 1000); lastMove = now;\n      s.day += dd; s.v = Math.max(-45, Math.min(45, s.v * 0.6 + (dd / dt) * 0.4));\n      if (reduce) { publish(); draw(); }\n    };\n    const onUp = () => { if (!s.drag) return; s.drag = false; if (performance.now() - lastMove > 120) s.v = 0; if (reduce) settle(); };\n    el.addEventListener(\"pointerdown\", onDown); el.addEventListener(\"pointermove\", onMove);\n    el.addEventListener(\"pointerup\", onUp); el.addEventListener(\"pointercancel\", onUp);\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      el.removeEventListener(\"pointerdown\", onDown); el.removeEventListener(\"pointermove\", onMove);\n      el.removeEventListener(\"pointerup\", onUp); el.removeEventListener(\"pointercancel\", onUp);\n      io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, [today, due0]);\n\n  const date = new Date(today.getFullYear(), today.getMonth(), today.getDate() + day);\n  const dateText = `${DOW[date.getDay()]} ${date.getDate()} ${MON[date.getMonth()]} ${date.getFullYear()}`;\n  const wait = (i: number) => mod(due0[i] - day, cycles[i].every);\n  const go = (d: number) => api.current?.go(d);\n  const onKey = (e: ReactKeyboardEvent) => {\n    const step = e.key === \"ArrowRight\" || e.key === \"ArrowUp\" ? 1 : e.key === \"ArrowLeft\" || e.key === \"ArrowDown\" ? -1 : e.key === \"PageUp\" ? 7 : e.key === \"PageDown\" ? -7 : 0;\n    if (e.key === \"Home\") { e.preventDefault(); go(0); return; }\n    if (!step) return;\n    e.preventDefault(); go((api.current?.aim() ?? dayRef.current) + step); // steps add up even while the arms are still travelling\n  };\n\n  return (\n    <div className=\"flex min-h-[700px] w-full flex-col items-center justify-center bg-[#090d18] bg-[radial-gradient(110%_75%_at_50%_34%,#1a2440_0%,#090d18_64%)] px-4 py-10 sm:min-h-[800px]\">\n      <div\n        ref={pad}\n        role=\"slider\"\n        tabIndex={0}\n        aria-label=\"Schedule date, in days from today\"\n        aria-valuemin={-3650}\n        aria-valuemax={3650}\n        aria-valuenow={day}\n        aria-valuetext={`${dateText}. ${cycles.map((c, i) => `${c.name} ${wait(i) === 0 ? \"due\" : `in ${wait(i)} d`}`).join(\", \")}`}\n        onKeyDown={onKey}\n        className=\"relative aspect-square w-full max-w-[580px] cursor-grab touch-none rounded-full outline-none focus-visible:ring-2 focus-visible:ring-[#e8c56e] focus-visible:ring-offset-4 focus-visible:ring-offset-[#090d18] active:cursor-grabbing\"\n      >\n        <div aria-hidden=\"true\" className=\"absolute inset-[3%] rounded-full shadow-[0_46px_70px_-18px_rgba(0,0,0,0.9),0_10px_18px_rgba(0,0,0,0.7)]\" />\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n      </div>\n      <p aria-live=\"polite\" className=\"mt-7 text-center font-mono text-[11px] uppercase leading-5 tracking-[0.18em] text-slate-400\">\n        <span className=\"text-slate-100\">{dateText}</span>\n        <span className=\"mx-2 text-slate-600\">/</span>\n        {day === 0 ? \"Today\" : `${Math.abs(day)} ${Math.abs(day) === 1 ? \"day\" : \"days\"} ${day > 0 ? \"ahead\" : \"back\"}`}\n      </p>\n      <ul className=\"mt-4 grid w-full max-w-[640px] grid-cols-2 gap-2 sm:grid-cols-4\">\n        {cycles.map((c, i) => (\n          <li key={c.name}>\n            <button\n              type=\"button\"\n              onClick={() => go(day + (wait(i) === 0 ? c.every : wait(i)))}\n              aria-label={`${c.name}, every ${c.every} days, ${wait(i) === 0 ? \"due on this date\" : `due in ${wait(i)} days`}. Turn to its next date.`}\n              className={`flex min-h-[48px] w-full items-center gap-2.5 rounded-xl border px-3 py-2 text-left outline-none transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-[#e8c56e] motion-reduce:transition-none ${wait(i) === 0 ? \"border-[#e8c56e]/70 bg-[#e8c56e]/10\" : \"border-white/10 bg-white/[0.03] hover:border-white/25\"}`}\n            >\n              <span aria-hidden=\"true\" className=\"h-3 w-3 shrink-0 rounded-full shadow-[inset_-2px_-2px_3px_rgba(0,0,0,0.45)]\" style={{ backgroundColor: c.dot }} />\n              <span className=\"min-w-0\">\n                <span className=\"block truncate text-[13px] font-medium leading-4 text-slate-100\">{c.name}</span>\n                <span className={`block font-mono text-[10px] uppercase leading-4 tracking-[0.14em] ${wait(i) === 0 ? \"text-[#f1d588]\" : \"text-slate-400\"}`}>{wait(i) === 0 ? \"Due\" : `In ${wait(i)} d`}</span>\n              </span>\n            </button>\n          </li>\n        ))}\n      </ul>\n    </div>\n  );\n}\n"}]}
