{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "spending-by-category-chart",
  "title": "Spending by Category Chart",
  "description": "A personal finance spending breakdown: month total with budget left and change vs last month, a gapped donut with a center readout, ranked category bars with budget markers and over-budget flags, compare-with-last-month ghost bars, a table view, month stepping and a merchant drill-down per category.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/spending-by-category-chart/spending-by-category-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport interface CategoryMonth {\n  month: string;\n  values: Record<string, number>;\n}\nexport interface Category {\n  id: string;\n  label: string;\n  budget: number;\n  merchants: Record<string, [string, number][]>;\n}\n\nexport interface SpendingByCategoryChartProps {\n  categories?: Category[];\n  months?: CategoryMonth[];\n  currency?: string;\n  theme?: \"dark\" | \"light\";\n  className?: string;\n}\n\n// Categorical slots in fixed order, validated per mode (CVD + contrast). Color follows the category.\nconst SERIES = {\n  dark: [\"#3987e5\", \"#d95926\", \"#199e70\", \"#c98500\", \"#d55181\", \"#9085e9\"],\n  light: [\"#2a78d6\", \"#eb6834\", \"#1baf7a\", \"#eda100\", \"#e87ba4\", \"#6250d6\"],\n};\nconst PALETTES = {\n  dark: { bg: \"#0A0A0A\", text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.64)\", faint: \"rgba(245,244,241,0.38)\", card: \"#121212\", well: \"rgba(255,255,255,0.04)\", border: \"rgba(255,255,255,0.08)\", track: \"rgba(255,255,255,0.07)\", on: \"#F5F4F1\", onText: \"#0A0A0A\", over: \"#F87171\", good: \"#4ADE80\", ring: \"rgba(245,244,241,0.6)\" },\n  light: { bg: \"#FAFAF8\", text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.64)\", faint: \"rgba(10,10,10,0.42)\", card: \"#FFFFFF\", well: \"rgba(10,10,10,0.03)\", border: \"rgba(10,10,10,0.09)\", track: \"rgba(10,10,10,0.07)\", on: \"#0A0A0A\", onText: \"#FAFAF8\", over: \"#DC2626\", good: \"#15803D\", ring: \"rgba(10,10,10,0.5)\" },\n};\nconst CATEGORIES: Category[] = [\n  { id: \"housing\", label: \"Housing\", budget: 1100, merchants: { Nov: [[\"Rent · Altbau Living\", 980], [\"Utilities · Stadtwerk\", 112]], Oct: [[\"Rent · Altbau Living\", 980], [\"Utilities · Stadtwerk\", 98]], Sep: [[\"Rent · Altbau Living\", 980], [\"Utilities · Stadtwerk\", 91]] } },\n  { id: \"food\", label: \"Groceries & dining\", budget: 450, merchants: { Nov: [[\"Marktplatz Grocer\", 214], [\"Kornblume Bakery\", 61], [\"Osteria Lupo\", 148], [\"Noodle Bar 88\", 96]], Oct: [[\"Marktplatz Grocer\", 196], [\"Osteria Lupo\", 92], [\"Kornblume Bakery\", 54]], Sep: [[\"Marktplatz Grocer\", 205], [\"Noodle Bar 88\", 64], [\"Kornblume Bakery\", 48]] } },\n  { id: \"transport\", label: \"Transport\", budget: 180, merchants: { Nov: [[\"City transit pass\", 86], [\"Velo Rides\", 42], [\"Fuel · Nordtank\", 38]], Oct: [[\"City transit pass\", 86], [\"Velo Rides\", 31]], Sep: [[\"City transit pass\", 86], [\"Skyline Air\", 212]] } },\n  { id: \"leisure\", label: \"Leisure\", budget: 200, merchants: { Nov: [[\"Lumen Cinema\", 48], [\"Climbing Hall\", 65], [\"Bookshop Seiten\", 34]], Oct: [[\"Climbing Hall\", 65], [\"Lumen Cinema\", 24]], Sep: [[\"Harbor Hotel\", 386], [\"Climbing Hall\", 65]] } },\n  { id: \"subs\", label: \"Subscriptions\", budget: 40, merchants: { Nov: [[\"Wavelength Music\", 11], [\"Cloudnest Storage\", 3], [\"Newsline Daily\", 8], [\"Fitloop\", 20]], Oct: [[\"Wavelength Music\", 11], [\"Cloudnest Storage\", 3], [\"Newsline Daily\", 8]], Sep: [[\"Wavelength Music\", 11], [\"Cloudnest Storage\", 3], [\"Newsline Daily\", 8]] } },\n  { id: \"health\", label: \"Health\", budget: 80, merchants: { Nov: [[\"Apotheke am Park\", 27], [\"Physio Klinik\", 45]], Oct: [[\"Apotheke am Park\", 18]], Sep: [[\"Dental care\", 120], [\"Apotheke am Park\", 14]] } },\n];\nconst sum = (l: [string, number][] = []) => l.reduce((a, [, v]) => a + v, 0);\nconst MONTHS: CategoryMonth[] = [\"Sep\", \"Oct\", \"Nov\"].map((m) => ({ month: m, values: Object.fromEntries(CATEGORIES.map((c) => [c.id, sum(c.merchants[m])])) }));\nconst LONG: Record<string, string> = { Sep: \"September\", Oct: \"October\", Nov: \"November\" };\n\nexport function SpendingByCategoryChart({ categories = CATEGORIES, months = MONTHS, currency = \"€\", theme = \"dark\", className }: SpendingByCategoryChartProps) {\n  const p = PALETTES[theme];\n  const colors = SERIES[theme];\n  const reduce = !!useReducedMotion();\n  const uid = React.useId();\n  const [mi, setMi] = React.useState(months.length - 1);\n  const [hover, setHover] = React.useState<string | null>(null);\n  const [picked, setPicked] = React.useState<string | null>(null);\n  const [compare, setCompare] = React.useState(false);\n  const [view, setView] = React.useState<\"chart\" | \"table\">(\"chart\");\n\n  const month = months[mi];\n  const prev = months[mi - 1];\n  const total = categories.reduce((a, c) => a + (month.values[c.id] ?? 0), 0);\n  const prevTotal = prev ? categories.reduce((a, c) => a + (prev.values[c.id] ?? 0), 0) : null;\n  const budgetTotal = categories.reduce((a, c) => a + c.budget, 0);\n  const money = (n: number) => `${currency}${Math.round(n).toLocaleString(\"en-US\")}`;\n  const colorOf = (id: string) => colors[categories.findIndex((c) => c.id === id) % colors.length];\n  const focusId = hover ?? picked;\n  const focusCat = categories.find((c) => c.id === focusId) ?? null;\n  const ranked = [...categories].sort((a, b) => (month.values[b.id] ?? 0) - (month.values[a.id] ?? 0));\n  const maxBar = Math.max(...categories.map((c) => Math.max(month.values[c.id] ?? 0, c.budget, compare && prev ? (prev.values[c.id] ?? 0) : 0)));\n\n  // Donut: one circle per category, 2px surface gap between segments.\n  const R = 70;\n  const C = 2 * Math.PI * R;\n  const GAP = 2.5;\n  const lens = categories.map((c) => (total ? ((month.values[c.id] ?? 0) / total) * C : 0));\n  const arcs = categories.map((c, i) => ({ id: c.id, start: lens.slice(0, i).reduce((a, b) => a + b, 0), len: Math.max(0, lens[i] - GAP) }));\n\n  const ring = { [\"--tw-ring-color\" as string]: p.ring, [\"--tw-ring-offset-color\" as string]: p.card } as React.CSSProperties;\n  const focus = \"outline-none focus-visible:ring-2 focus-visible:ring-offset-2\";\n  const delta = prevTotal ? (total - prevTotal) / prevTotal : 0;\n\n  return (\n    <section className={cn(\"@container w-full\", className)} style={{ background: p.bg, color: p.text }}>\n      <div className=\"mx-auto max-w-5xl px-4 py-10 @[640px]:px-6\">\n        <div className=\"rounded-2xl border p-5 @[640px]:p-6\" style={{ background: p.card, borderColor: p.border }}>\n          {/* Header + filters in one row */}\n          <div className=\"flex flex-wrap items-start justify-between gap-3\">\n            <div>\n              <h2 className=\"m-0 text-[15px] font-semibold\">Spending by category</h2>\n              <p className=\"m-0 mt-2 text-[30px] leading-none font-semibold tracking-[-0.02em] tabular-nums\">{money(total)}</p>\n              <p className=\"m-0 mt-1.5 text-[12.5px]\" style={{ color: p.muted }}>\n                {LONG[month.month] ?? month.month} · {money(Math.abs(budgetTotal - total))} {total <= budgetTotal ? \"left in budget\" : \"over budget\"}\n                {prevTotal !== null && (\n                  <span>\n                    {\" \"}\n                    · {delta >= 0 ? \"▲\" : \"▼\"} {Math.abs(Math.round(delta * 100))}% vs {prev!.month}\n                  </span>\n                )}\n              </p>\n            </div>\n            <div className=\"flex flex-wrap items-center gap-2\">\n              <div className=\"flex items-center rounded-lg border\" style={{ borderColor: p.border }}>\n                <button type=\"button\" onClick={() => (setMi((i) => i - 1), setPicked(null))} disabled={mi === 0} aria-label=\"Previous month\" className={cn(\"grid h-8 w-8 cursor-pointer place-items-center border-0 bg-transparent disabled:cursor-not-allowed disabled:opacity-30\", focus)} style={{ color: p.text, ...ring }}>\n                  ‹\n                </button>\n                <span className=\"w-10 text-center text-[12.5px] font-medium\" aria-live=\"polite\">\n                  {month.month}\n                </span>\n                <button type=\"button\" onClick={() => (setMi((i) => i + 1), setPicked(null))} disabled={mi === months.length - 1} aria-label=\"Next month\" className={cn(\"grid h-8 w-8 cursor-pointer place-items-center border-0 bg-transparent disabled:cursor-not-allowed disabled:opacity-30\", focus)} style={{ color: p.text, ...ring }}>\n                  ›\n                </button>\n              </div>\n              <button type=\"button\" aria-pressed={compare} disabled={!prev} onClick={() => setCompare((c) => !c)} className={cn(\"h-8 cursor-pointer rounded-lg border px-2.5 text-[12px] disabled:cursor-not-allowed disabled:opacity-40\", focus)} style={{ borderColor: compare ? p.ring : p.border, background: compare ? p.well : \"transparent\", color: compare ? p.text : p.muted, ...ring }}>\n                Compare {prev ? prev.month : \"\"}\n              </button>\n              <div role=\"tablist\" aria-label=\"View\" className=\"flex rounded-lg border p-0.5\" style={{ borderColor: p.border }}>\n                {([\"chart\", \"table\"] as const).map((v) => (\n                  <button key={v} type=\"button\" role=\"tab\" aria-selected={view === v} onClick={() => setView(v)} className={cn(\"h-7 cursor-pointer rounded-md border-0 px-2.5 text-[12px] capitalize\", focus)} style={{ background: view === v ? p.well : \"transparent\", color: view === v ? p.text : p.muted, ...ring }}>\n                    {v}\n                  </button>\n                ))}\n              </div>\n            </div>\n          </div>\n\n          {view === \"table\" ? (\n            <table className=\"mt-5 w-full border-collapse text-[13px]\">\n              <caption className=\"sr-only\">\n                Spending by category, {LONG[month.month]}\n              </caption>\n              <thead>\n                <tr className=\"border-b text-left text-[11.5px]\" style={{ borderColor: p.border, color: p.faint }}>\n                  <th scope=\"col\" className=\"py-2 font-medium\">\n                    Category\n                  </th>\n                  <th scope=\"col\" className=\"py-2 text-right font-medium\">\n                    Spent\n                  </th>\n                  <th scope=\"col\" className=\"py-2 text-right font-medium\">\n                    Budget\n                  </th>\n                  <th scope=\"col\" className=\"py-2 text-right font-medium\">\n                    Share\n                  </th>\n                  {prev && (\n                    <th scope=\"col\" className=\"py-2 text-right font-medium\">\n                      {prev.month}\n                    </th>\n                  )}\n                </tr>\n              </thead>\n              <tbody>\n                {ranked.map((c) => (\n                  <tr key={c.id} className=\"border-b last:border-b-0\" style={{ borderColor: p.border }}>\n                    <th scope=\"row\" className=\"py-2.5 text-left font-normal\">\n                      <span className=\"flex items-center gap-2\">\n                        <span className=\"h-2.5 w-2.5 rounded-sm\" style={{ background: colorOf(c.id) }} />\n                        {c.label}\n                      </span>\n                    </th>\n                    <td className=\"py-2.5 text-right tabular-nums\">{money(month.values[c.id] ?? 0)}</td>\n                    <td className=\"py-2.5 text-right tabular-nums\" style={{ color: p.muted }}>\n                      {money(c.budget)}\n                    </td>\n                    <td className=\"py-2.5 text-right tabular-nums\" style={{ color: p.muted }}>\n                      {total ? Math.round(((month.values[c.id] ?? 0) / total) * 100) : 0}%\n                    </td>\n                    {prev && (\n                      <td className=\"py-2.5 text-right tabular-nums\" style={{ color: p.muted }}>\n                        {money(prev.values[c.id] ?? 0)}\n                      </td>\n                    )}\n                  </tr>\n                ))}\n              </tbody>\n            </table>\n          ) : (\n            <div className=\"mt-5 grid grid-cols-1 items-center gap-6 @[700px]:grid-cols-[220px_minmax(0,1fr)]\">\n              {/* Donut */}\n              <div className=\"relative mx-auto h-[200px] w-[200px]\">\n                <svg viewBox=\"0 0 200 200\" className=\"h-full w-full -rotate-90\" role=\"img\" aria-label={`Share of ${money(total)}: ${ranked.map((c) => `${c.label} ${money(month.values[c.id] ?? 0)}`).join(\", \")}`}>\n                  <circle cx=\"100\" cy=\"100\" r={R} fill=\"none\" stroke={total ? \"transparent\" : p.track} strokeWidth=\"22\" />\n                  {arcs.map((a, i) => {\n                    const dim = focusId && focusId !== a.id;\n                    return (\n                      <motion.circle\n                        key={a.id}\n                        cx=\"100\"\n                        cy=\"100\"\n                        r={R}\n                        fill=\"none\"\n                        stroke={colorOf(a.id)}\n                        strokeLinecap=\"butt\"\n                        initial={reduce ? false : { strokeDasharray: `0 ${C}`, strokeDashoffset: -a.start }}\n                        animate={{ strokeDasharray: `${a.len} ${C}`, strokeDashoffset: -a.start, opacity: dim ? 0.3 : 1, strokeWidth: focusId === a.id ? 28 : 22 }}\n                        transition={{ duration: reduce ? 0 : 0.6, delay: reduce ? 0 : i * 0.05, ease: [0.22, 1, 0.36, 1] }}\n                        onMouseEnter={() => setHover(a.id)}\n                        onMouseLeave={() => setHover(null)}\n                        onClick={() => setPicked((x) => (x === a.id ? null : a.id))}\n                        className=\"cursor-pointer\"\n                      />\n                    );\n                  })}\n                </svg>\n                <div className=\"pointer-events-none absolute inset-0 grid place-items-center text-center\" aria-live=\"polite\">\n                  <AnimatePresence mode=\"popLayout\" initial={false}>\n                    <motion.div key={focusCat?.id ?? \"total\"} initial={reduce ? false : { opacity: 0, y: 4 }} animate={{ opacity: 1, y: 0 }} exit={reduce ? undefined : { opacity: 0, y: -4 }} transition={{ duration: 0.18 }}>\n                      <p className=\"m-0 max-w-[110px] text-[11.5px] leading-tight\" style={{ color: p.muted }}>\n                        {focusCat ? focusCat.label : \"Total spent\"}\n                      </p>\n                      <p className=\"m-0 mt-1 text-[19px] font-semibold tabular-nums\">{money(focusCat ? (month.values[focusCat.id] ?? 0) : total)}</p>\n                      <p className=\"m-0 text-[11px] tabular-nums\" style={{ color: p.faint }}>\n                        {focusCat ? `${Math.round(((month.values[focusCat.id] ?? 0) / total) * 100)}% of spend` : `${categories.length} categories`}\n                      </p>\n                    </motion.div>\n                  </AnimatePresence>\n                </div>\n              </div>\n\n              {/* Bars vs budget (legend + direct labels) */}\n              <ul className=\"m-0 flex list-none flex-col gap-1 p-0\" aria-label=\"Categories\">\n                {ranked.map((c) => {\n                  const v = month.values[c.id] ?? 0;\n                  const pv = prev?.values[c.id] ?? 0;\n                  const over = v > c.budget;\n                  const isFocus = focusId === c.id;\n                  return (\n                    <li key={c.id}>\n                      <button type=\"button\" aria-pressed={picked === c.id} onMouseEnter={() => setHover(c.id)} onMouseLeave={() => setHover(null)} onFocus={() => setHover(c.id)} onBlur={() => setHover(null)} onClick={() => setPicked((x) => (x === c.id ? null : c.id))} className={cn(\"w-full cursor-pointer rounded-lg border-0 px-2.5 py-2 text-left transition-colors\", focus)} style={{ background: isFocus ? p.well : \"transparent\", color: p.text, opacity: focusId && !isFocus ? 0.55 : 1, ...ring }}>\n                        <span className=\"flex items-center gap-2 text-[12.5px]\">\n                          <span className=\"h-2.5 w-2.5 shrink-0 rounded-sm\" style={{ background: colorOf(c.id) }} />\n                          <span className=\"min-w-0 flex-1 truncate\">{c.label}</span>\n                          {over && (\n                            <span className=\"flex items-center gap-1 text-[11px] font-medium\" style={{ color: p.over }}>\n                              ▲ {money(v - c.budget)} over\n                            </span>\n                          )}\n                          <span className=\"font-medium tabular-nums\">{money(v)}</span>\n                          <span className=\"w-16 text-right text-[11.5px] tabular-nums\" style={{ color: p.faint }}>\n                            of {money(c.budget)}\n                          </span>\n                        </span>\n                        <span className=\"relative mt-1.5 block h-2\">\n                          <span className=\"absolute inset-0 rounded\" style={{ background: p.track }} />\n                          {compare && prev && <motion.span className=\"absolute top-0 left-0 h-[3px] rounded\" initial={reduce ? false : { width: 0 }} animate={{ width: `${(pv / maxBar) * 100}%` }} transition={{ duration: 0.4 }} style={{ background: p.faint }} />}\n                          <motion.span className=\"absolute bottom-0 left-0 rounded-r\" initial={reduce ? false : { width: 0 }} animate={{ width: `${(v / maxBar) * 100}%`, height: compare && prev ? 5 : 8 }} transition={{ duration: reduce ? 0 : 0.55, ease: [0.22, 1, 0.36, 1] }} style={{ background: colorOf(c.id) }} />\n                          <span className=\"absolute -top-0.5 -bottom-0.5 w-0.5 rounded\" style={{ left: `calc(${(c.budget / maxBar) * 100}% - 1px)`, background: p.text, opacity: 0.55 }} title={`Budget ${money(c.budget)}`} />\n                        </span>\n                      </button>\n                    </li>\n                  );\n                })}\n                <li className=\"mt-1 flex flex-wrap gap-x-4 gap-y-1 px-2.5 text-[11px]\" style={{ color: p.faint }}>\n                  <span className=\"flex items-center gap-1.5\">\n                    <span className=\"h-2.5 w-0.5 rounded\" style={{ background: p.text, opacity: 0.55 }} /> Budget\n                  </span>\n                  {compare && prev && (\n                    <span className=\"flex items-center gap-1.5\">\n                      <span className=\"h-[3px] w-3 rounded\" style={{ background: p.faint }} /> {prev.month}\n                    </span>\n                  )}\n                </li>\n              </ul>\n            </div>\n          )}\n\n          {/* Drill-down */}\n          <AnimatePresence initial={false}>\n            {picked && view === \"chart\" && (\n              <motion.div key={`${picked}-${month.month}`} initial={reduce ? false : { height: 0, opacity: 0 }} animate={{ height: \"auto\", opacity: 1 }} exit={reduce ? undefined : { height: 0, opacity: 0 }} className=\"overflow-hidden\" id={`${uid}-drill`}>\n                <div className=\"mt-5 border-t pt-4\" style={{ borderColor: p.border }}>\n                  <p className=\"m-0 flex items-center gap-2 text-[13px] font-medium\">\n                    <span className=\"h-2.5 w-2.5 rounded-sm\" style={{ background: colorOf(picked) }} />\n                    {categories.find((c) => c.id === picked)?.label} in {LONG[month.month]}\n                  </p>\n                  <ul className=\"m-0 mt-2 grid list-none grid-cols-1 gap-x-6 p-0 @[560px]:grid-cols-2\">\n                    {(categories.find((c) => c.id === picked)?.merchants[month.month] ?? []).map(([name, amt], i) => (\n                      <motion.li key={name} initial={reduce ? false : { opacity: 0, y: 4 }} animate={{ opacity: 1, y: 0 }} transition={{ delay: reduce ? 0 : i * 0.04 }} className=\"flex justify-between gap-3 border-b py-2 text-[12.5px]\" style={{ borderColor: p.border }}>\n                        <span className=\"truncate\" style={{ color: p.muted }}>\n                          {name}\n                        </span>\n                        <span className=\"tabular-nums\">{money(amt)}</span>\n                      </motion.li>\n                    ))}\n                  </ul>\n                </div>\n              </motion.div>\n            )}\n          </AnimatePresence>\n        </div>\n      </div>\n    </section>\n  );\n}\n\nexport default SpendingByCategoryChart;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}