{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "map-city-blocks",
  "type": "registry:component",
  "title": "Map City Blocks",
  "description": "An extruded procedural city on canvas that slowly turns on its own, rotates with drag and inertia, and switches to a night view with lit windows and street lamps.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "maps",
    "map",
    "city",
    "3d",
    "isometric",
    "night mode",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/map-city-blocks",
  "files": [
    {
      "path": "registry/hyperblur/map-city-blocks.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/map-city-blocks.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface MapCityBlocksProps extends Omit<React.HTMLAttributes<HTMLDivElement>, \"children\"> {\n  accent?: string;\n  seed?: number;\n  autoRotate?: boolean;\n  night?: boolean;\n  defaultNight?: boolean;\n  onNightChange?: (night: boolean) => void;\n  label?: string;\n  caption?: string;\n}\n\ntype Pt = [number, number];\ntype C3 = [number, number, number];\ntype Box = { x0: number; y0: number; x1: number; y1: number; h: number; t: number; id: number };\ntype Cell = { i: number; j: number; k: number };\ntype City = { n: number; seed: number; boxes: Box[]; cells: Cell[]; trees: Pt[]; lamps: Pt[] };\ntype View = { k: number; ox: number; oy: number; c: number; cos: number; sin: number };\n\nconst GRID = 12;\nconst RW = 0.4;\nconst SUN_X = Math.cos(0.95);\nconst SUN_Y = Math.sin(0.95);\nconst NORMALS: Pt[] = [\n  [0, -1],\n  [1, 0],\n  [0, 1],\n  [-1, 0],\n];\nconst RM_QUERY = \"(prefers-reduced-motion: reduce)\";\n\nfunction mix32(n: number) {\n  let x = n | 0;\n  x = Math.imul(x ^ (x >>> 16), 0x7feb352d);\n  x = Math.imul(x ^ (x >>> 15), 0x846ca68b);\n  x ^= x >>> 16;\n  return (x >>> 0) / 4294967296;\n}\n\nfunction rnd(seed: number, a: number, b = 0, c = 0) {\n  return mix32(seed * 374761393 + a * 668265263 + b * 2246822519 + c * 3266489917 + 1013904223);\n}\n\nfunction riverY(x: number, n: number, seed: number) {\n  return n * 0.76 + Math.sin(x * 0.5 + seed * 0.9) * 1.1 + Math.sin(x * 1.1 + seed) * 0.25;\n}\n\nfunction buildCity(n: number, seed: number): City {\n  const boxes: Box[] = [];\n  const cells: Cell[] = [];\n  const trees: Pt[] = [];\n  const lamps: Pt[] = [];\n  const pi = 1 + Math.floor(rnd(seed, 1) * 3);\n  const pj = 1 + Math.floor(rnd(seed, 2) * 3);\n  const dcx = n * 0.55;\n  const dcy = n * 0.45;\n  let id = 0;\n  for (let j = 1; j < n; j++) {\n    for (let i = 1; i < n; i++) {\n      if (Math.abs(j - riverY(i, n, seed)) > RW + 0.12) lamps.push([i, j]);\n    }\n  }\n  for (let j = 0; j < n; j++) {\n    for (let i = 0; i < n; i++) {\n      const cx = i + 0.5;\n      const cy = j + 0.5;\n      const dr = Math.abs(cy - riverY(cx, n, seed));\n      if (dr < RW + 0.1) continue;\n      if (dr < 0.95) {\n        cells.push({ i, j, k: 2 });\n        for (let q = 0; q < 3; q++) {\n          const tx = i + 0.2 + rnd(seed, i, j, q * 2 + 40) * 0.6;\n          const ty = j + 0.2 + rnd(seed, i, j, q * 2 + 41) * 0.6;\n          if (Math.abs(ty - riverY(tx, n, seed)) > RW + 0.14) trees.push([tx, ty]);\n        }\n        continue;\n      }\n      if (i >= pi && i < pi + 2 && j >= pj && j < pj + 2) {\n        cells.push({ i, j, k: 1 });\n        for (let q = 0; q < 6; q++) {\n          trees.push([i + 0.2 + rnd(seed, i, j, q * 2 + 60) * 0.6, j + 0.2 + rnd(seed, i, j, q * 2 + 61) * 0.6]);\n        }\n        continue;\n      }\n      cells.push({ i, j, k: 0 });\n      const s = 0.15;\n      const g = 0.05;\n      const x0 = i + s;\n      const y0 = j + s;\n      const x1 = i + 1 - s;\n      const y1 = j + 1 - s;\n      const r = rnd(seed, i, j, 7);\n      const lots: [number, number, number, number][] = [];\n      if (r < 0.36) {\n        lots.push([x0, y0, x1, y1]);\n      } else if (r < 0.74) {\n        const m = 0.4 + rnd(seed, i, j, 8) * 0.2;\n        if (rnd(seed, i, j, 9) < 0.5) {\n          const xm = x0 + (x1 - x0) * m;\n          lots.push([x0, y0, xm - g / 2, y1], [xm + g / 2, y0, x1, y1]);\n        } else {\n          const ym = y0 + (y1 - y0) * m;\n          lots.push([x0, y0, x1, ym - g / 2], [x0, ym + g / 2, x1, y1]);\n        }\n      } else {\n        const xm = (x0 + x1) / 2;\n        const ym = (y0 + y1) / 2;\n        lots.push(\n          [x0, y0, xm - g / 2, ym - g / 2],\n          [xm + g / 2, y0, x1, ym - g / 2],\n          [x0, ym + g / 2, xm - g / 2, y1],\n          [xm + g / 2, ym + g / 2, x1, y1],\n        );\n      }\n      const core = Math.exp(-((cx - dcx) ** 2 + (cy - dcy) ** 2) / 12);\n      lots.forEach((l, q) => {\n        const u = rnd(seed, i, j, 20 + q);\n        let h = 0.1 + core * (0.32 + u * 1.05) + rnd(seed, i, j, 30 + q) * 0.12;\n        if (u > 0.88 && core > 0.35) h *= 1.55;\n        boxes.push({ x0: l[0], y0: l[1], x1: l[2], y1: l[3], h: Math.min(2.9, h), t: rnd(seed, i, j, 50 + q), id: id++ });\n      });\n    }\n  }\n  return { n, seed, boxes, cells, trees, lamps };\n}\n\nfunction proj(v: View, x: number, y: number, z: number): Pt {\n  const dx = x - v.c;\n  const dy = y - v.c;\n  const rx = dx * v.cos - dy * v.sin;\n  const ry = dx * v.sin + dy * v.cos;\n  return [v.ox + (rx - ry) * v.k * 0.866, v.oy + (rx + ry) * v.k * 0.5 - z * v.k * 0.82];\n}\n\nfunction mixc(a: C3, b: C3, t: number): C3 {\n  return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];\n}\n\nfunction shade(a: C3, m: number, b: C3, t: number): C3 {\n  return [Math.min(255, a[0] * m + b[0] * t), Math.min(255, a[1] * m + b[1] * t), Math.min(255, a[2] * m + b[2] * t)];\n}\n\nfunction rgba(c: C3, a = 1) {\n  return `rgba(${Math.round(c[0])},${Math.round(c[1])},${Math.round(c[2])},${a.toFixed(3)})`;\n}\n\nfunction quad(ctx: CanvasRenderingContext2D, p: Pt[]) {\n  ctx.moveTo(p[0][0], p[0][1]);\n  for (let i = 1; i < p.length; i++) ctx.lineTo(p[i][0], p[i][1]);\n  ctx.closePath();\n}\n\nfunction hull(points: Pt[]): Pt[] {\n  const p = points.slice().sort((a, b) => a[0] - b[0] || a[1] - b[1]);\n  const cross = (o: Pt, a: Pt, b: Pt) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);\n  const lower: Pt[] = [];\n  for (const q of p) {\n    while (lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], q) <= 0) lower.pop();\n    lower.push(q);\n  }\n  const upper: Pt[] = [];\n  for (let i = p.length - 1; i >= 0; i--) {\n    const q = p[i];\n    while (upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], q) <= 0) upper.pop();\n    upper.push(q);\n  }\n  return lower.slice(0, -1).concat(upper.slice(0, -1));\n}\n\nfunction parseColor(ctx: CanvasRenderingContext2D, value: string, fb: C3): C3 {\n  ctx.fillStyle = \"#000000\";\n  ctx.fillStyle = value;\n  const v = String(ctx.fillStyle);\n  if (v[0] === \"#\" && v.length === 7) {\n    return [parseInt(v.slice(1, 3), 16), parseInt(v.slice(3, 5), 16), parseInt(v.slice(5, 7), 16)];\n  }\n  const m = v.match(/[\\d.]+/g);\n  return m && m.length >= 3 ? [Number(m[0]), Number(m[1]), Number(m[2])] : fb;\n}\n\nfunction drawScene(ctx: CanvasRenderingContext2D, w: number, h: number, city: City, v: View, nt: number, acc: C3, t: number) {\n  const n = city.n;\n  const P = (x: number, y: number, z = 0) => proj(v, x, y, z);\n  const day = 1 - nt;\n  const sun: C3 = mixc([255, 236, 210], [150, 175, 255], nt);\n  const ux = SUN_X * v.cos - SUN_Y * v.sin;\n  const uy = SUN_X * v.sin + SUN_Y * v.cos;\n\n  const sky = ctx.createLinearGradient(0, 0, 0, h);\n  sky.addColorStop(0, rgba(mixc([20, 26, 40], [5, 7, 15], nt)));\n  sky.addColorStop(1, rgba(mixc([10, 12, 18], [3, 4, 9], nt)));\n  ctx.fillStyle = sky;\n  ctx.fillRect(0, 0, w, h);\n\n  const ground = mixc([24, 28, 38], [10, 12, 19], nt);\n  ctx.beginPath();\n  quad(ctx, [P(-0.3, -0.3), P(n + 0.3, -0.3), P(n + 0.3, n + 0.3), P(-0.3, n + 0.3)]);\n  ctx.fillStyle = rgba(mixc([14, 17, 24], [6, 7, 12], nt));\n  ctx.fill();\n  ctx.beginPath();\n  quad(ctx, [P(0, 0), P(n, 0), P(n, n), P(0, n)]);\n  ctx.fillStyle = rgba(ground);\n  ctx.fill();\n\n  const kinds: C3[] = [\n    mixc([33, 38, 51], [13, 15, 24], nt),\n    mixc([26, 52, 41], [9, 22, 19], nt),\n    mixc([27, 44, 39], [11, 19, 18], nt),\n  ];\n  for (let kind = 0; kind < 3; kind++) {\n    ctx.beginPath();\n    for (const c of city.cells) {\n      if (c.k !== kind) continue;\n      const m = kind === 0 ? 0.1 : 0.06;\n      quad(ctx, [P(c.i + m, c.j + m), P(c.i + 1 - m, c.j + m), P(c.i + 1 - m, c.j + 1 - m), P(c.i + m, c.j + 1 - m)]);\n    }\n    ctx.fillStyle = rgba(kinds[kind]);\n    ctx.fill();\n  }\n\n  const top: Pt[] = [];\n  const bot: Pt[] = [];\n  for (let x = 0; x <= n + 0.001; x += 0.25) {\n    const y = riverY(x, n, city.seed);\n    top.push(P(x, y - RW));\n    bot.push(P(x, y + RW));\n  }\n  ctx.beginPath();\n  quad(ctx, top.concat(bot.slice().reverse()));\n  ctx.fillStyle = rgba(mixc([18, 40, 70], [7, 15, 32], nt));\n  ctx.fill();\n  ctx.beginPath();\n  top.forEach((p, i) => (i ? ctx.lineTo(p[0], p[1]) : ctx.moveTo(p[0], p[1])));\n  bot.forEach((p, i) => (i ? ctx.lineTo(p[0], p[1]) : ctx.moveTo(p[0], p[1])));\n  ctx.strokeStyle = rgba([120, 170, 255], 0.14 + nt * 0.06);\n  ctx.lineWidth = 1;\n  ctx.stroke();\n  ctx.beginPath();\n  for (let i = 1; i < n; i += 2) {\n    const y = riverY(i, n, city.seed);\n    quad(ctx, [P(i - 0.08, y - RW - 0.05), P(i + 0.08, y - RW - 0.05), P(i + 0.08, y + RW + 0.05), P(i - 0.08, y + RW + 0.05)]);\n  }\n  ctx.fillStyle = rgba(mixc(ground, [70, 76, 92], 0.2));\n  ctx.fill();\n\n  const tr = v.k * 0.08;\n  ctx.beginPath();\n  for (const tp of city.trees) {\n    const p = P(tp[0], tp[1], 0.09);\n    ctx.moveTo(p[0] + tr, p[1]);\n    ctx.arc(p[0], p[1], tr, 0, Math.PI * 2);\n  }\n  ctx.fillStyle = rgba(mixc([46, 98, 72], [16, 36, 30], nt));\n  ctx.fill();\n\n  if (day > 0.02) {\n    const len = 0.95;\n    ctx.beginPath();\n    for (const b of city.boxes) {\n      const ox = -SUN_X * len * b.h;\n      const oy = -SUN_Y * len * b.h;\n      quad(\n        ctx,\n        hull([\n          P(b.x0, b.y0),\n          P(b.x1, b.y0),\n          P(b.x1, b.y1),\n          P(b.x0, b.y1),\n          P(b.x0 + ox, b.y0 + oy),\n          P(b.x1 + ox, b.y0 + oy),\n          P(b.x1 + ox, b.y1 + oy),\n          P(b.x0 + ox, b.y1 + oy),\n        ]),\n      );\n    }\n    ctx.fillStyle = `rgba(0,0,0,${(0.3 * day).toFixed(3)})`;\n    ctx.fill();\n  }\n\n  if (nt > 0.02) {\n    ctx.globalCompositeOperation = \"lighter\";\n    const gr = Math.max(3, v.k * 0.16);\n    ctx.beginPath();\n    for (const l of city.lamps) {\n      const p = P(l[0], l[1]);\n      ctx.moveTo(p[0] + gr, p[1]);\n      ctx.arc(p[0], p[1], gr, 0, Math.PI * 2);\n    }\n    ctx.fillStyle = `rgba(255,170,90,${(0.12 * nt).toFixed(3)})`;\n    ctx.fill();\n    ctx.beginPath();\n    for (const l of city.lamps) {\n      const p = P(l[0], l[1]);\n      ctx.moveTo(p[0] + 1.1, p[1]);\n      ctx.arc(p[0], p[1], 1.1, 0, Math.PI * 2);\n    }\n    ctx.fillStyle = `rgba(255,214,160,${(0.85 * nt).toFixed(3)})`;\n    ctx.fill();\n    ctx.globalCompositeOperation = \"source-over\";\n  }\n\n  const rn = NORMALS.map(([x, y]) => [x * v.cos - y * v.sin, x * v.sin + y * v.cos] as Pt);\n  const vis = rn.map((p) => p[0] + p[1] > 0.001);\n  const lam = rn.map((p) => Math.max(0, p[0] * ux + p[1] * uy));\n  const depth = (b: Box) => {\n    const dx = (b.x0 + b.x1) / 2 - v.c;\n    const dy = (b.y0 + b.y1) / 2 - v.c;\n    return dx * (v.cos + v.sin) + dy * (v.cos - v.sin);\n  };\n  const sorted = city.boxes.slice().sort((a, b) => depth(a) - depth(b));\n  const topBase = mixc([48, 56, 74], [22, 26, 40], nt);\n  const sideBase = mixc([27, 32, 45], [12, 15, 26], nt);\n  const blink = 0.5 + 0.5 * Math.sin(t / 420);\n  const winA = (0.9 * nt).toFixed(3);\n  for (const b of sorted) {\n    const c: Pt[] = [\n      [b.x0, b.y0],\n      [b.x1, b.y0],\n      [b.x1, b.y1],\n      [b.x0, b.y1],\n    ];\n    const tone = 0.86 + b.t * 0.26;\n    for (let e = 0; e < 4; e++) {\n      if (!vis[e]) continue;\n      const p = c[e];\n      const q = c[(e + 1) % 4];\n      ctx.beginPath();\n      quad(ctx, [P(p[0], p[1]), P(q[0], q[1]), P(q[0], q[1], b.h), P(p[0], p[1], b.h)]);\n      ctx.fillStyle = rgba(shade(sideBase, tone, sun, lam[e] * (0.34 * day + 0.05 * nt)));\n      ctx.fill();\n      if (nt > 0.04) {\n        const floors = Math.floor((b.h - 0.08) / 0.16);\n        const cols = Math.max(1, Math.round(Math.hypot(q[0] - p[0], q[1] - p[1]) / 0.16));\n        ctx.fillStyle = (b.id & 3) === 0 ? `rgba(170,205,255,${winA})` : `rgba(255,198,122,${winA})`;\n        for (let f = 0; f < floors; f++) {\n          for (let k = 0; k < cols; k++) {\n            if (rnd(city.seed, b.id, e * 37 + f, k) > 0.4) continue;\n            const u = (k + 0.5) / cols;\n            const wp = P(p[0] + (q[0] - p[0]) * u, p[1] + (q[1] - p[1]) * u, 0.12 + f * 0.16);\n            ctx.fillRect(wp[0] - 0.8, wp[1] - 0.7, 1.6, 1.4);\n          }\n        }\n      }\n    }\n    ctx.beginPath();\n    quad(ctx, c.map((p) => P(p[0], p[1], b.h)));\n    ctx.fillStyle = rgba(shade(topBase, tone, sun, 0.14 * day));\n    ctx.fill();\n    ctx.strokeStyle = `rgba(255,255,255,${(0.05 + 0.05 * day).toFixed(3)})`;\n    ctx.lineWidth = 0.6;\n    ctx.stroke();\n    if (b.h > 1.7) {\n      const r = P((b.x0 + b.x1) / 2, (b.y0 + b.y1) / 2, b.h + 0.06);\n      ctx.beginPath();\n      ctx.arc(r[0], r[1], 1.5 + blink * 0.6, 0, Math.PI * 2);\n      ctx.fillStyle = rgba(acc, 0.35 + 0.65 * blink);\n      ctx.fill();\n    }\n  }\n\n  const vg = ctx.createRadialGradient(w / 2, h / 2, Math.min(w, h) * 0.3, w / 2, h / 2, Math.max(w, h) * 0.72);\n  vg.addColorStop(0, \"rgba(4,5,8,0)\");\n  vg.addColorStop(1, `rgba(4,5,8,${(0.45 + nt * 0.2).toFixed(3)})`);\n  ctx.fillStyle = vg;\n  ctx.fillRect(0, 0, w, h);\n}\n\nfunction subscribeMotion(cb: () => void) {\n  const m = window.matchMedia(RM_QUERY);\n  m.addEventListener(\"change\", cb);\n  return () => m.removeEventListener(\"change\", cb);\n}\n\nconst css = `\n.hb-ub2 {\n  --hb-ub2-a: #ff4d4d;\n  position: relative;\n  box-sizing: border-box;\n  width: 100%;\n  aspect-ratio: 16 / 10;\n  min-height: 180px;\n  overflow: hidden;\n  isolation: isolate;\n  border-radius: 14px;\n  border: 1px solid rgba(255, 255, 255, 0.09);\n  background: #07090d;\n  color: #ededed;\n  font-family: inherit;\n  container-type: inline-size;\n}\n.hb-ub2 *, .hb-ub2 *::before, .hb-ub2 *::after { box-sizing: border-box; }\n.hb-ub2-stage {\n  position: absolute;\n  inset: 0;\n  cursor: grab;\n  touch-action: pan-y;\n  outline: none;\n  -webkit-tap-highlight-color: transparent;\n}\n.hb-ub2-stage[data-drag=\"true\"] { cursor: grabbing; }\n.hb-ub2-stage:focus-visible { box-shadow: inset 0 0 0 2px var(--hb-ub2-a); border-radius: 13px; }\n.hb-ub2-cv { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ub2-glass {\n  position: absolute;\n  background: rgba(9, 10, 13, 0.64);\n  border: 1px solid rgba(255, 255, 255, 0.08);\n  -webkit-backdrop-filter: blur(10px);\n  backdrop-filter: blur(10px);\n}\n.hb-ub2-hud {\n  left: 10px;\n  top: 10px;\n  display: flex;\n  align-items: center;\n  gap: 9px;\n  padding: 6px 11px 6px 6px;\n  border-radius: 12px;\n  pointer-events: none;\n}\n.hb-ub2-cmp { flex: none; width: 26px; height: 26px; color: #a1a1a1; }\n.hb-ub2-tx { display: flex; flex-direction: column; gap: 3px; min-width: 0; }\n.hb-ub2-tt { font-size: 12px; font-weight: 600; line-height: 1.1; white-space: nowrap; }\n.hb-ub2-mc {\n  font: 600 8.5px/1 ui-monospace, SFMono-Regular, Menlo, monospace;\n  letter-spacing: 0.14em;\n  text-transform: uppercase;\n  color: #8f8f8f;\n  white-space: nowrap;\n}\n.hb-ub2-mc i { font-style: normal; color: var(--hb-ub2-a); }\n.hb-ub2-tg {\n  right: 10px;\n  top: 10px;\n  display: inline-flex;\n  align-items: center;\n  gap: 6px;\n  height: 30px;\n  margin: 0;\n  padding: 0 11px 0 8px;\n  border-radius: 999px;\n  color: #ededed;\n  font: inherit;\n  font-size: 11.5px;\n  font-weight: 500;\n  cursor: pointer;\n  transition: border-color 0.2s ease, background 0.2s ease;\n}\n.hb-ub2-tg:hover { border-color: rgba(255, 255, 255, 0.2); }\n.hb-ub2-tg:focus-visible { outline: 2px solid var(--hb-ub2-a); outline-offset: 2px; }\n.hb-ub2-sw {\n  position: relative;\n  width: 26px;\n  height: 15px;\n  border-radius: 999px;\n  background: rgba(255, 255, 255, 0.14);\n  transition: background 0.25s ease;\n}\n.hb-ub2-sw::after {\n  content: \"\";\n  position: absolute;\n  top: 2px;\n  left: 2px;\n  width: 11px;\n  height: 11px;\n  border-radius: 50%;\n  background: #ffc27a;\n  box-shadow: 0 0 8px rgba(255, 194, 122, 0.7);\n  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1), background 0.25s ease, box-shadow 0.25s ease;\n}\n.hb-ub2-tg[aria-pressed=\"true\"] .hb-ub2-sw { background: color-mix(in srgb, var(--hb-ub2-a) 30%, rgba(255, 255, 255, 0.08)); }\n.hb-ub2-tg[aria-pressed=\"true\"] .hb-ub2-sw::after { transform: translateX(11px); background: #dfe7ff; box-shadow: 0 0 8px rgba(170, 195, 255, 0.7); }\n.hb-ub2-hint {\n  position: absolute;\n  left: 12px;\n  bottom: 10px;\n  display: flex;\n  align-items: center;\n  gap: 6px;\n  font: 600 8.5px/1 ui-monospace, SFMono-Regular, Menlo, monospace;\n  letter-spacing: 0.14em;\n  text-transform: uppercase;\n  color: rgba(237, 237, 237, 0.55);\n  pointer-events: none;\n}\n.hb-ub2-hint svg { width: 14px; height: 14px; }\n@container (max-width: 300px) {\n  .hb-ub2-mc { display: none; }\n  .hb-ub2-tl { display: none; }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-ub2-sw, .hb-ub2-sw::after, .hb-ub2-tg { transition: none; }\n}\n`;\n\nexport function MapCityBlocks({\n  accent = \"#ff4d4d\",\n  seed = 12,\n  autoRotate = true,\n  night,\n  defaultNight = false,\n  onNightChange,\n  label = \"District 07\",\n  caption = \"Live model\",\n  className = \"\",\n  style,\n  ...rest\n}: MapCityBlocksProps) {\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const stageRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n  const needleRef = React.useRef<SVGGElement>(null);\n  const reduced = React.useSyncExternalStore(subscribeMotion, () => window.matchMedia(RM_QUERY).matches, () => false);\n  const [innerNight, setInnerNight] = React.useState(defaultNight);\n  const isNight = night ?? innerNight;\n  const city = React.useMemo(() => buildCity(GRID, seed), [seed]);\n  const st = React.useRef({ night: isNight, accent, auto: autoRotate, reduced: false, kick: () => {} });\n\n  React.useEffect(() => {\n    const s = st.current;\n    s.night = isNight;\n    s.accent = accent;\n    s.auto = autoRotate;\n    s.reduced = reduced;\n    s.kick();\n  }, [isNight, accent, autoRotate, reduced]);\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    const stage = stageRef.current;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!root || !stage || !canvas || !ctx) return;\n    const s = st.current;\n    let w = 0;\n    let h = 0;\n    let dpr = 1;\n    let view: View | null = null;\n    let angle = 0.62;\n    let vel = 0;\n    let nm = s.night ? 1 : 0;\n    let raf = 0;\n    let last = 0;\n    let visible = true;\n    let dragging = false;\n    let lastX = 0;\n    let lastT = 0;\n    let idleAt = 0;\n    let deg = -1;\n    let rgb: C3 = [255, 77, 77];\n    let rgbFor = \"\";\n\n    const fit = () => {\n      const n = city.n;\n      const k = Math.min(w / (1.732 * n), (h - 16) / (n + 2.6)) * 1.12;\n      view = { k, ox: w / 2, oy: h / 2 + k * 0.75, c: n / 2, cos: Math.cos(angle), sin: Math.sin(angle) };\n    };\n\n    const draw = (t = performance.now()) => {\n      if (!view) return;\n      if (rgbFor !== s.accent) {\n        rgb = parseColor(ctx, s.accent, [255, 77, 77]);\n        rgbFor = s.accent;\n      }\n      view.cos = Math.cos(angle);\n      view.sin = Math.sin(angle);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      drawScene(ctx, w, h, city, view, nm, rgb, s.reduced ? 0 : t);\n      const d = Math.round((((angle * 180) / Math.PI) % 360 + 360) % 360);\n      if (d !== deg) {\n        deg = d;\n        stage.setAttribute(\"aria-valuenow\", String(d));\n        stage.setAttribute(\"aria-valuetext\", `${d} degrees`);\n        needleRef.current?.setAttribute(\"transform\", `rotate(${(-d - 45).toFixed(1)} 13 13)`);\n      }\n    };\n\n    const frame = (t: number) => {\n      raf = 0;\n      const dt = last ? Math.min(0.05, (t - last) / 1000) : 0;\n      last = t;\n      const target = s.night ? 1 : 0;\n      if (s.reduced) {\n        nm = target;\n      } else {\n        nm += (target - nm) * (1 - Math.exp(-dt * 5));\n        if (Math.abs(target - nm) < 0.002) nm = target;\n      }\n      if (!dragging) {\n        if (!s.reduced && s.auto && t - idleAt > 1400) vel += (0.14 - vel) * (1 - Math.exp(-dt * 1.2));\n        else vel *= Math.exp(-dt * (s.reduced ? 30 : 2.6));\n        angle += vel * dt;\n      }\n      draw(t);\n      const busy = dragging || Math.abs(vel) > 0.002 || nm !== target;\n      if (visible && (!s.reduced || busy)) raf = requestAnimationFrame(frame);\n      else last = 0;\n    };\n\n    const kick = () => {\n      if (!visible) return;\n      if (!raf) {\n        last = 0;\n        raf = requestAnimationFrame(frame);\n      }\n    };\n    s.kick = kick;\n\n    const resize = () => {\n      const r = root.getBoundingClientRect();\n      if (r.width < 2 || r.height < 2) return;\n      w = r.width;\n      h = r.height;\n      dpr = Math.min(2, window.devicePixelRatio || 1);\n      canvas.width = Math.round(w * dpr);\n      canvas.height = Math.round(h * dpr);\n      fit();\n      draw();\n    };\n\n    const down = (e: PointerEvent) => {\n      if (e.button !== 0 && e.pointerType === \"mouse\") return;\n      dragging = true;\n      lastX = e.clientX;\n      lastT = e.timeStamp;\n      vel = 0;\n      stage.setPointerCapture(e.pointerId);\n      stage.dataset.drag = \"true\";\n      kick();\n    };\n    const move = (e: PointerEvent) => {\n      if (!dragging) return;\n      const dx = e.clientX - lastX;\n      const dt = Math.max(1, e.timeStamp - lastT) / 1000;\n      const da = dx * (Math.PI / Math.max(240, w));\n      angle += da;\n      vel = vel * 0.6 + (da / dt) * 0.4;\n      lastX = e.clientX;\n      lastT = e.timeStamp;\n      draw();\n    };\n    const up = (e: PointerEvent) => {\n      if (!dragging) return;\n      dragging = false;\n      idleAt = e.timeStamp || performance.now();\n      if (e.timeStamp - lastT > 80) vel = 0;\n      vel = Math.max(-4, Math.min(4, vel));\n      if (stage.hasPointerCapture(e.pointerId)) stage.releasePointerCapture(e.pointerId);\n      stage.dataset.drag = \"false\";\n      kick();\n    };\n    const key = (e: KeyboardEvent) => {\n      if (e.key !== \"ArrowLeft\" && e.key !== \"ArrowRight\") return;\n      e.preventDefault();\n      idleAt = performance.now();\n      const dir = e.key === \"ArrowLeft\" ? -1 : 1;\n      if (s.reduced) {\n        angle += dir * 0.2;\n        draw();\n      } else {\n        vel = dir * 1.6;\n        kick();\n      }\n    };\n\n    stage.addEventListener(\"pointerdown\", down);\n    stage.addEventListener(\"pointermove\", move);\n    stage.addEventListener(\"pointerup\", up);\n    stage.addEventListener(\"pointercancel\", up);\n    stage.addEventListener(\"keydown\", key);\n    s.reduced = window.matchMedia(RM_QUERY).matches;\n    resize();\n    const ro = new ResizeObserver(resize);\n    ro.observe(root);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) kick();\n    });\n    io.observe(root);\n    kick();\n    return () => {\n      stage.removeEventListener(\"pointerdown\", down);\n      stage.removeEventListener(\"pointermove\", move);\n      stage.removeEventListener(\"pointerup\", up);\n      stage.removeEventListener(\"pointercancel\", up);\n      stage.removeEventListener(\"keydown\", key);\n      ro.disconnect();\n      io.disconnect();\n      cancelAnimationFrame(raf);\n      s.kick = () => {};\n    };\n  }, [city]);\n\n  const toggle = () => {\n    const next = !isNight;\n    if (night === undefined) setInnerNight(next);\n    onNightChange?.(next);\n  };\n\n  return (\n    <div\n      ref={rootRef}\n      className={`hb-ub2 ${className}`.trim()}\n      style={{ \"--hb-ub2-a\": accent, ...style } as React.CSSProperties}\n      {...rest}\n    >\n      <style href=\"hb-map-city-blocks\" precedence=\"default\">\n        {css}\n      </style>\n      <div\n        ref={stageRef}\n        className=\"hb-ub2-stage\"\n        role=\"slider\"\n        tabIndex={0}\n        aria-label={`${label} 3D model rotation`}\n        aria-valuemin={0}\n        aria-valuemax={359}\n        aria-valuenow={36}\n        aria-valuetext=\"36 degrees\"\n        data-drag=\"false\"\n      >\n        <canvas ref={canvasRef} className=\"hb-ub2-cv\" aria-hidden=\"true\" />\n      </div>\n      <div className=\"hb-ub2-glass hb-ub2-hud\">\n        <svg className=\"hb-ub2-cmp\" viewBox=\"0 0 26 26\" aria-hidden=\"true\">\n          <circle cx=\"13\" cy=\"13\" r=\"12\" fill=\"none\" stroke=\"currentColor\" strokeOpacity=\"0.35\" />\n          <g ref={needleRef} transform=\"rotate(-81 13 13)\">\n            <path d=\"M13 3.5 15.4 13h-4.8Z\" fill=\"var(--hb-ub2-a)\" />\n            <path d=\"M13 22.5 10.6 13h4.8Z\" fill=\"currentColor\" fillOpacity=\"0.55\" />\n          </g>\n        </svg>\n        <div className=\"hb-ub2-tx\">\n          <span className=\"hb-ub2-tt\">{label}</span>\n          <span className=\"hb-ub2-mc\">\n            <i>●</i> {caption} · {city.boxes.length} bldg\n          </span>\n        </div>\n      </div>\n      <button type=\"button\" className=\"hb-ub2-glass hb-ub2-tg\" aria-pressed={isNight} aria-label=\"Night mode\" onClick={toggle}>\n        <span className=\"hb-ub2-sw\" aria-hidden=\"true\" />\n        <span className=\"hb-ub2-tl\">{isNight ? \"Night\" : \"Day\"}</span>\n      </button>\n      <div className=\"hb-ub2-hint\" aria-hidden=\"true\">\n        <svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\">\n          <path d=\"M3 12a9 4 0 0 0 18 0\" />\n          <path d=\"m17.5 15.2 3.5-3.2 1 4.5\" />\n        </svg>\n        Drag to rotate\n      </div>\n    </div>\n  );\n}\n"
    }
  ]
}