{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "habits",
  "type": "registry:block",
  "title": "Habits",
  "description": "A habit tracker that makes ticking things off feel good: checks fill and draw in a beat, counted habits fill like a glass, streaks roll up, and a week grid lets you patch the days you forgot to log.",
  "categories": [
    "screen",
    "Personal"
  ],
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils",
    "https://coss.com/ui/r/button.json",
    "https://coss.com/ui/r/input.json"
  ],
  "files": [
    {
      "path": "registry/devl/habits.tsx",
      "type": "registry:component",
      "content": "\"use client\";\n\nimport {\n  BikeIcon,\n  BookOpenIcon,\n  CarrotIcon,\n  ChevronLeftIcon,\n  DropletsIcon,\n  DumbbellIcon,\n  FlameIcon,\n  Flower2Icon,\n  FootprintsIcon,\n  LanguagesIcon,\n  type LucideIcon,\n  MinusIcon,\n  MoonIcon,\n  MusicIcon,\n  NotebookPenIcon,\n  PersonStandingIcon,\n  PlusIcon,\n  SparklesIcon,\n  SunIcon,\n  XIcon,\n} from \"lucide-react\";\nimport {\n  AnimatePresence,\n  animate,\n  motion,\n  PresenceContext,\n  useMotionValue,\n  useReducedMotion,\n  useTransform,\n} from \"motion/react\";\nimport { type FormEvent, useEffect, useId, useLayoutEffect, useRef, useState } from \"react\";\nimport { cn } from \"@/lib/utils\";\nimport { Button } from \"@/components/ui/button\";\nimport { Input } from \"@/components/ui/input\";\n\nexport type HabitIcon =\n  | \"water\"\n  | \"journal\"\n  | \"stretch\"\n  | \"walk\"\n  | \"read\"\n  | \"sleep\"\n  | \"meditate\"\n  | \"veg\"\n  | \"lift\"\n  | \"music\"\n  | \"sun\"\n  | \"language\"\n  | \"bike\";\nexport type HabitTone = \"sky\" | \"amber\" | \"violet\" | \"teal\" | \"orange\" | \"rose\";\nexport type HabitTime = \"morning\" | \"anytime\" | \"evening\";\n\nexport interface Habit {\n  id: string;\n  name: string;\n  icon: HabitIcon;\n  tone: HabitTone;\n  time: HabitTime;\n  goal?: { count: number; unit: string };\n  carry: number;\n  week: number[];\n  since?: number;\n}\n\nexport interface HabitsProps {\n  className?: string;\n  habits?: Habit[];\n  weekStart?: string;\n  todayIndex?: number;\n  productName?: string;\n}\n\nconst spring = { type: \"spring\", stiffness: 520, damping: 44, mass: 1 } as const;\nconst easeOut = [0.23, 1, 0.32, 1] as const;\nconst press = \"transition-[color,background-color,box-shadow,scale,opacity] duration-150 ease-out active:scale-[0.97]\";\nconst card = \"rounded-2xl border bg-card shadow-xs/5\";\n\nconst icons: Record<HabitIcon, { icon: LucideIcon; label: string }> = {\n  water: { icon: DropletsIcon, label: \"Water\" },\n  journal: { icon: NotebookPenIcon, label: \"Journal\" },\n  stretch: { icon: PersonStandingIcon, label: \"Stretch\" },\n  walk: { icon: FootprintsIcon, label: \"Walk\" },\n  read: { icon: BookOpenIcon, label: \"Read\" },\n  sleep: { icon: MoonIcon, label: \"Sleep\" },\n  meditate: { icon: Flower2Icon, label: \"Meditate\" },\n  veg: { icon: CarrotIcon, label: \"Eat well\" },\n  lift: { icon: DumbbellIcon, label: \"Strength\" },\n  music: { icon: MusicIcon, label: \"Practice\" },\n  sun: { icon: SunIcon, label: \"Outside\" },\n  language: { icon: LanguagesIcon, label: \"Language\" },\n  bike: { icon: BikeIcon, label: \"Ride\" },\n};\n\nconst tones: Record<HabitTone, { tile: string; fill: string; soft: string; on: string; label: string }> = {\n  sky: {\n    tile: \"bg-sky-500/12 text-sky-700 dark:bg-sky-400/14 dark:text-sky-300\",\n    fill: \"bg-sky-500 dark:bg-sky-400\",\n    soft: \"bg-sky-500/30 dark:bg-sky-400/40\",\n    on: \"text-white dark:text-sky-950\",\n    label: \"Sky\",\n  },\n  amber: {\n    tile: \"bg-amber-500/14 text-amber-700 dark:bg-amber-400/14 dark:text-amber-300\",\n    fill: \"bg-amber-500 dark:bg-amber-400\",\n    soft: \"bg-amber-500/30 dark:bg-amber-400/40\",\n    on: \"text-white dark:text-amber-950\",\n    label: \"Amber\",\n  },\n  violet: {\n    tile: \"bg-violet-500/12 text-violet-700 dark:bg-violet-400/14 dark:text-violet-300\",\n    fill: \"bg-violet-500 dark:bg-violet-400\",\n    soft: \"bg-violet-500/30 dark:bg-violet-400/40\",\n    on: \"text-white dark:text-violet-950\",\n    label: \"Violet\",\n  },\n  teal: {\n    tile: \"bg-teal-500/12 text-teal-700 dark:bg-teal-400/14 dark:text-teal-300\",\n    fill: \"bg-teal-500 dark:bg-teal-400\",\n    soft: \"bg-teal-500/30 dark:bg-teal-400/40\",\n    on: \"text-white dark:text-teal-950\",\n    label: \"Teal\",\n  },\n  orange: {\n    tile: \"bg-orange-500/12 text-orange-700 dark:bg-orange-400/14 dark:text-orange-300\",\n    fill: \"bg-orange-500 dark:bg-orange-400\",\n    soft: \"bg-orange-500/30 dark:bg-orange-400/40\",\n    on: \"text-white dark:text-orange-950\",\n    label: \"Orange\",\n  },\n  rose: {\n    tile: \"bg-rose-500/12 text-rose-700 dark:bg-rose-400/14 dark:text-rose-300\",\n    fill: \"bg-rose-500 dark:bg-rose-400\",\n    soft: \"bg-rose-500/30 dark:bg-rose-400/40\",\n    on: \"text-white dark:text-rose-950\",\n    label: \"Rose\",\n  },\n};\n\nconst times: { value: HabitTime; label: string }[] = [\n  { value: \"morning\", label: \"Morning\" },\n  { value: \"anytime\", label: \"Anytime\" },\n  { value: \"evening\", label: \"Evening\" },\n];\n\nconst defaultHabits: Habit[] = [\n  { id: \"h1\", name: \"Morning pages\", icon: \"journal\", tone: \"amber\", time: \"morning\", carry: 37, week: [1, 1, 1, 1, 1, 1, 0] },\n  { id: \"h2\", name: \"Stretch for 10 minutes\", icon: \"stretch\", tone: \"violet\", time: \"morning\", carry: 0, week: [1, 0, 1, 1, 1, 1, 0] },\n  { id: \"h3\", name: \"Drink water\", icon: \"water\", tone: \"sky\", time: \"anytime\", goal: { count: 8, unit: \"glasses\" }, carry: 9, week: [8, 8, 8, 8, 8, 5, 0] },\n  { id: \"h4\", name: \"Walk 8,000 steps\", icon: \"walk\", tone: \"teal\", time: \"anytime\", carry: 4, week: [1, 1, 1, 1, 1, 0, 0] },\n  { id: \"h5\", name: \"Read 20 pages\", icon: \"read\", tone: \"orange\", time: \"evening\", carry: 2, week: [1, 1, 0, 1, 1, 0, 0] },\n  { id: \"h6\", name: \"Lights out by 11\", icon: \"sleep\", tone: \"rose\", time: \"evening\", carry: 0, week: [1, 1, 1, 0, 1, 0, 0] },\n];\n\nconst suggestions: { name: string; icon: HabitIcon; tone: HabitTone; time: HabitTime; goal?: { count: number; unit: string } }[] = [\n  { name: \"Meditate\", icon: \"meditate\", tone: \"violet\", time: \"morning\" },\n  { name: \"Eat a vegetable\", icon: \"veg\", tone: \"orange\", time: \"anytime\", goal: { count: 3, unit: \"servings\" } },\n  { name: \"Practice guitar\", icon: \"music\", tone: \"rose\", time: \"evening\" },\n  { name: \"Get outside\", icon: \"sun\", tone: \"amber\", time: \"anytime\" },\n];\n\nconst isDone = (h: Habit, v: number) => (h.goal ? v >= h.goal.count : v > 0);\n\nfunction streakOf(h: Habit, today: number) {\n  const since = h.since ?? 0;\n  let i = isDone(h, h.week[today] ?? 0) ? today : today - 1;\n  let s = 0;\n  while (i >= since && isDone(h, h.week[i] ?? 0)) {\n    s++;\n    i--;\n  }\n  return i < 0 ? s + h.carry : s;\n}\n\nfunction Tween({ value, format = (n: number) => String(Math.round(n)) }: { value: number; format?: (n: number) => string }) {\n  const reduce = useReducedMotion();\n  const mv = useMotionValue(value);\n  const text = useTransform(mv, format);\n  useEffect(() => {\n    if (reduce) {\n      mv.set(value);\n      return;\n    }\n    const controls = animate(mv, value, { type: \"spring\", stiffness: 200, damping: 28 });\n    return () => controls.stop();\n  }, [value, reduce, mv]);\n  return <motion.span className=\"tabular-nums\">{text}</motion.span>;\n}\n\nfunction Ring({ value, size = 40, stroke = 4, className }: { value: number; size?: number; stroke?: number; className?: string }) {\n  const reduce = useReducedMotion();\n  const r = (size - stroke) / 2;\n  return (\n    <svg aria-hidden className={cn(\"-rotate-90\", className)} height={size} viewBox={`0 0 ${size} ${size}`} width={size}>\n      <circle className=\"stroke-muted\" cx={size / 2} cy={size / 2} fill=\"none\" r={r} strokeWidth={stroke} />\n      <motion.circle\n        animate={{ pathLength: value, opacity: value > 0 ? 1 : 0 }}\n        className=\"stroke-primary\"\n        cx={size / 2}\n        cy={size / 2}\n        fill=\"none\"\n        initial={false}\n        r={r}\n        strokeLinecap=\"round\"\n        strokeWidth={stroke}\n        transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 180, damping: 26 }}\n      />\n    </svg>\n  );\n}\n\nexport function Habits({\n  className,\n  habits: initialHabits = defaultHabits,\n  weekStart = \"2026-10-19\",\n  todayIndex = 5,\n  productName = \"Kindling\",\n}: HabitsProps) {\n  const [habits, setHabits] = useState(initialHabits);\n  const [day, setDay] = useState(todayIndex);\n  const [sheet, setSheet] = useState(false);\n  const [freshId, setFreshId] = useState<string | null>(null);\n  const [wide, setWide] = useState(false);\n  const reduce = useReducedMotion();\n  const uid = useId().replace(/:/g, \"\");\n  const rootRef = useRef<HTMLDivElement>(null);\n  const addRef = useRef<HTMLButtonElement>(null);\n  const counter = useRef(0);\n\n  useLayoutEffect(() => {\n    const el = rootRef.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([e]) => setWide(e.contentRect.width >= 768));\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n\n  useEffect(() => {\n    if (!freshId) return;\n    const timer = setTimeout(() => setFreshId(null), 1600);\n    return () => clearTimeout(timer);\n  }, [freshId]);\n\n  const dates = Array.from({ length: 7 }, (_, i) => {\n    const d = new Date(`${weekStart}T12:00:00`);\n    d.setDate(d.getDate() + i);\n    return d;\n  });\n  const dayName = (i: number, style: \"long\" | \"short\" | \"narrow\" = \"long\") =>\n    dates[i].toLocaleDateString(\"en-US\", { weekday: style });\n\n  const setValue = (id: string, i: number, v: number) =>\n    setHabits((all) => all.map((h) => (h.id === id ? { ...h, week: h.week.map((x, j) => (j === i ? v : x)) } : h)));\n\n  const activeOn = (i: number) => habits.filter((h) => (h.since ?? 0) <= i);\n  const doneOn = (i: number) => activeOn(i).filter((h) => isDone(h, h.week[i] ?? 0)).length;\n  const dayRatio = (i: number) => {\n    const n = activeOn(i).length;\n    return n ? doneOn(i) / n : 0;\n  };\n  const doneToday = doneOn(day);\n  const dayTotal = activeOn(day).length;\n  const allDone = dayTotal > 0 && doneToday === dayTotal;\n  const days = Array.from({ length: todayIndex + 1 }, (_, i) => i);\n  const possible = days.reduce((s, i) => s + activeOn(i).length, 0);\n  const ticked = days.reduce((s, i) => s + doneOn(i), 0);\n  const weekPct = possible ? Math.round((ticked / possible) * 100) : 0;\n  const perfect = days.filter((i) => activeOn(i).length > 0 && doneOn(i) === activeOn(i).length).length;\n  const best = habits.reduce<{ h: Habit | null; s: number }>(\n    (acc, h) => {\n      const s = streakOf(h, todayIndex);\n      return s > acc.s ? { h, s } : acc;\n    },\n    { h: null, s: 0 },\n  );\n\n  const byTime = times\n    .map((t) => {\n      const list = habits.filter((h) => h.time === t.value);\n      let slots = 0;\n      let hit = 0;\n      for (const h of list)\n        for (const i of days)\n          if ((h.since ?? 0) <= i) {\n            slots++;\n            if (isDone(h, h.week[i] ?? 0)) hit++;\n          }\n      return { ...t, slots, pct: slots ? Math.round((hit / slots) * 100) : 0 };\n    })\n    .filter((t) => t.slots > 0);\n  const bestPct = Math.max(0, ...byTime.map((t) => t.pct));\n  const bestTime = byTime.length > 1 ? byTime.find((t) => t.pct === bestPct) : undefined;\n\n  const addHabit = (h: Omit<Habit, \"id\" | \"carry\" | \"week\">) => {\n    counter.current += 1;\n    const id = `new-${counter.current}`;\n    setHabits((all) => [...all, { ...h, id, carry: 0, since: todayIndex, week: [0, 0, 0, 0, 0, 0, 0] }]);\n    setFreshId(id);\n    setDay(todayIndex);\n  };\n\n  const isToday = day === todayIndex;\n\n  return (\n    <div\n      className={cn(\n        \"@container relative isolate flex h-[760px] w-full flex-col overflow-hidden bg-background text-foreground text-sm\",\n        className,\n      )}\n      ref={rootRef}\n    >\n      <header className=\"flex h-14 shrink-0 items-center gap-2.5 border-b px-4 @3xl:px-6\">\n        <span className=\"flex size-8 items-center justify-center rounded-lg bg-primary text-primary-foreground\">\n          <FlameIcon className=\"size-4\" />\n        </span>\n        <span className=\"font-heading font-semibold\">{productName}</span>\n        <Button className={cn(\"ms-auto\", press)} onClick={() => setSheet(true)} ref={addRef} size=\"sm\">\n          <PlusIcon /> New habit\n        </Button>\n      </header>\n\n      <div className=\"min-h-0 flex-1 overflow-y-auto\">\n        <div className=\"mx-auto flex max-w-[1100px] flex-col gap-5 px-4 pt-5 pb-10 @3xl:px-6 @3xl:pt-6 @4xl:grid @4xl:grid-cols-[minmax(0,1fr)_--spacing(95)] @4xl:items-start @4xl:gap-6\">\n          <section aria-labelledby={`${uid}-day`} className=\"flex min-w-0 flex-col gap-4\">\n            <div className=\"flex items-center gap-4\">\n              <div className=\"flex min-w-0 flex-1 flex-col gap-0.5\">\n                <div className=\"flex h-5 items-center\">\n                  <AnimatePresence initial={false} mode=\"popLayout\">\n                    {isToday ? (\n                      <motion.span\n                        animate={{ opacity: 1 }}\n                        className=\"text-muted-foreground text-xs\"\n                        exit={{ opacity: 0 }}\n                        initial={{ opacity: 0 }}\n                        key=\"today\"\n                        transition={{ duration: 0.15 }}\n                      >\n                        Today\n                      </motion.span>\n                    ) : (\n                      <motion.button\n                        animate={{ opacity: 1 }}\n                        className={cn(\n                          \"-ms-1 flex h-6 cursor-pointer items-center gap-0.5 rounded-md pe-1.5 text-muted-foreground text-xs outline-none hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring\",\n                          press,\n                        )}\n                        exit={{ opacity: 0 }}\n                        initial={{ opacity: 0 }}\n                        key=\"back\"\n                        onClick={() => setDay(todayIndex)}\n                        transition={{ duration: 0.15 }}\n                        type=\"button\"\n                      >\n                        <ChevronLeftIcon className=\"size-3.5\" /> Back to today\n                      </motion.button>\n                    )}\n                  </AnimatePresence>\n                </div>\n                <h2 className=\"truncate font-heading font-semibold text-2xl tracking-tight\" id={`${uid}-day`}>\n                  {dayName(day)}, {dates[day].toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\" })}\n                </h2>\n              </div>\n              <div className=\"relative flex size-14 shrink-0 items-center justify-center\">\n                <Ring className=\"absolute inset-0\" size={56} stroke={5} value={dayRatio(day)} />\n                <span className=\"font-medium text-xs tabular-nums\">\n                  <Tween value={doneToday} />/{dayTotal}\n                </span>\n                <span className=\"sr-only\" role=\"status\">\n                  {doneToday} of {dayTotal} habits done\n                </span>\n              </div>\n            </div>\n\n            <div aria-label=\"Pick a day\" className=\"grid grid-cols-7 gap-1\" role=\"group\">\n              {dates.map((d, i) => {\n                const future = i > todayIndex;\n                const active = i === day;\n                return (\n                  <button\n                    aria-label={`${dayName(i)}${i === todayIndex ? \", today\" : \"\"}${future ? \"\" : `, ${doneOn(i)} of ${activeOn(i).length} done`}`}\n                    aria-pressed={active}\n                    className={cn(\n                      \"relative flex cursor-pointer flex-col items-center gap-1.5 rounded-xl py-2 outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:cursor-default\",\n                      press,\n                      !active && !future && \"hover:bg-accent/60\",\n                      future && \"opacity-40\",\n                    )}\n                    disabled={future}\n                    key={d.toISOString()}\n                    onClick={() => setDay(i)}\n                    type=\"button\"\n                  >\n                    {active && (\n                      <motion.span\n                        className=\"absolute inset-0 -z-10 rounded-xl border bg-card shadow-xs/5\"\n                        layoutId={`${uid}-day`}\n                        transition={reduce ? { duration: 0 } : spring}\n                      />\n                    )}\n                    <span className={cn(\"text-xs\", active ? \"font-medium text-foreground\" : \"text-muted-foreground\")}>\n                      {dayName(i, \"short\").slice(0, 2)}\n                    </span>\n                    <span className=\"relative flex size-8 items-center justify-center\">\n                      <Ring className=\"absolute inset-0\" size={32} stroke={2.5} value={future ? 0 : dayRatio(i)} />\n                      <span className={cn(\"font-medium text-xs tabular-nums\", i === todayIndex && \"text-foreground\")}>\n                        {d.getDate()}\n                      </span>\n                    </span>\n                  </button>\n                );\n              })}\n            </div>\n\n            <AnimatePresence initial={false}>\n              {allDone && (\n                <motion.div\n                  animate={{ opacity: 1, height: \"auto\" }}\n                  className=\"overflow-hidden\"\n                  exit={{ opacity: 0, height: 0 }}\n                  initial={{ opacity: 0, height: 0 }}\n                  transition={reduce ? { duration: 0 } : { duration: 0.28, ease: easeOut }}\n                >\n                  <div className=\"flex items-center gap-3 rounded-xl border border-success/24 bg-success/8 p-3 dark:bg-success/12\">\n                    <span className=\"flex size-8 shrink-0 items-center justify-center rounded-full bg-success text-white\">\n                      <SparklesIcon className=\"size-4\" />\n                    </span>\n                    <p className=\"text-pretty\">\n                      <span className=\"font-medium\">A perfect {dayName(day)}.</span>{\" \"}\n                      <span className=\"text-muted-foreground\">All {dayTotal} habits done.</span>\n                    </p>\n                  </div>\n                </motion.div>\n              )}\n            </AnimatePresence>\n\n            {habits.length === 0 ? (\n              <div className={cn(card, \"flex flex-col items-center gap-2 px-6 py-12 text-center\")}>\n                <span className=\"flex size-10 items-center justify-center rounded-full bg-muted text-muted-foreground\">\n                  <FlameIcon className=\"size-4\" />\n                </span>\n                <p className=\"font-medium\">No habits yet</p>\n                <p className=\"text-muted-foreground\">Start with one small thing you want to do every day.</p>\n                <Button className={cn(\"mt-1\", press)} onClick={() => setSheet(true)} size=\"sm\">\n                  <PlusIcon /> New habit\n                </Button>\n              </div>\n            ) : (\n              times.map((t) => {\n                const list = habits.filter((h) => h.time === t.value && (h.since ?? 0) <= day);\n                if (!list.length) return null;\n                const doneHere = list.filter((h) => isDone(h, h.week[day] ?? 0)).length;\n                return (\n                  <div className=\"flex flex-col gap-2\" key={t.value}>\n                    <div className=\"flex items-baseline justify-between px-1 text-xs\">\n                      <h3 className=\"font-medium text-muted-foreground\">{t.label}</h3>\n                      <span className=\"text-muted-foreground tabular-nums\">\n                        {doneHere} of {list.length}\n                      </span>\n                    </div>\n                    <ul className={cn(card, \"divide-y overflow-hidden\")}>\n                      <AnimatePresence initial={false}>\n                        {list.map((h) => (\n                          <HabitRow\n                            day={day}\n                            fresh={h.id === freshId}\n                            habit={h}\n                            key={h.id}\n                            onChange={(v) => setValue(h.id, day, v)}\n                            streak={streakOf(h, todayIndex)}\n                          />\n                        ))}\n                      </AnimatePresence>\n                    </ul>\n                  </div>\n                );\n              })\n            )}\n          </section>\n\n          <aside className=\"flex min-w-0 flex-col gap-4\">\n            <div className={cn(card, \"grid grid-cols-3 divide-x\")}>\n              <Stat label=\"This week\" value={<><Tween value={weekPct} />%</>} />\n              <Stat label=\"Perfect days\" value={<Tween value={perfect} />} />\n              <Stat\n                hint={best.h ? icons[best.h.icon].label : undefined}\n                label=\"Top streak\"\n                value={\n                  <span className=\"flex items-center gap-1\">\n                    <Tween value={best.s} />\n                    <FlameIcon aria-hidden className=\"size-3.5 text-orange-500 dark:text-orange-400\" />\n                  </span>\n                }\n              />\n            </div>\n\n            <section aria-labelledby={`${uid}-grid`} className={cn(card, \"p-4\")}>\n              <div className=\"flex items-baseline justify-between gap-3\">\n                <h3 className=\"font-medium\" id={`${uid}-grid`}>\n                  This week\n                </h3>\n                <span className=\"text-muted-foreground text-xs\">\n                  {dates[0].toLocaleDateString(\"en-US\", { month: \"short\", day: \"numeric\" })} –{\" \"}\n                  {dates[6].toLocaleDateString(\"en-US\", { day: \"numeric\" })}\n                </span>\n              </div>\n              <table className=\"mt-3 w-full border-separate border-spacing-y-1\">\n                <thead>\n                  <tr>\n                    <th className=\"w-full p-0\">\n                      <span className=\"sr-only\">Habit</span>\n                    </th>\n                    {dates.map((d, i) => (\n                      <th\n                        className={cn(\n                          \"w-7 p-0 pb-1 text-center font-normal text-[11px]\",\n                          i === todayIndex ? \"font-medium text-foreground\" : \"text-muted-foreground\",\n                        )}\n                        key={d.toISOString()}\n                        scope=\"col\"\n                      >\n                        <abbr className=\"no-underline\" title={dayName(i)}>\n                          {dayName(i, \"narrow\")}\n                        </abbr>\n                      </th>\n                    ))}\n                    <th className=\"w-10 p-0 pb-1 text-right font-normal text-[11px] text-muted-foreground\" scope=\"col\">\n                      <FlameIcon aria-label=\"Streak\" className=\"ms-auto size-3\" />\n                    </th>\n                  </tr>\n                </thead>\n                <tbody>\n                  {habits.map((h) => {\n                    const Icon = icons[h.icon].icon;\n                    const s = streakOf(h, todayIndex);\n                    return (\n                      <tr key={h.id}>\n                        <th className=\"p-0 pe-2 text-left font-normal\" scope=\"row\">\n                          <span className=\"flex min-w-0 items-center gap-2\">\n                            <span className={cn(\"flex size-6 shrink-0 items-center justify-center rounded-md\", tones[h.tone].tile)}>\n                              <Icon aria-hidden className=\"size-3.5\" />\n                            </span>\n                            <span className=\"sr-only @md:not-sr-only @md:truncate @md:text-xs @4xl:max-w-30\">{h.name}</span>\n                          </span>\n                        </th>\n                        {h.week.map((v, i) => (\n                          <td className=\"p-0 text-center\" key={dates[i].toISOString()}>\n                            <GridCell\n                              day={dayName(i)}\n                              future={i > todayIndex}\n                              before={i < (h.since ?? 0)}\n                              habit={h}\n                              onToggle={() => setValue(h.id, i, isDone(h, v) ? 0 : h.goal?.count ?? 1)}\n                              selected={i === day}\n                              value={v}\n                            />\n                          </td>\n                        ))}\n                        <td className=\"p-0 text-right font-medium text-xs tabular-nums\">\n                          <span className={cn(s === 0 && \"text-muted-foreground\")}>\n                            <Tween value={s} />\n                          </span>\n                        </td>\n                      </tr>\n                    );\n                  })}\n                </tbody>\n              </table>\n            </section>\n\n            <section aria-labelledby={`${uid}-when`} className={cn(card, \"p-4\")}>\n              <h3 className=\"font-medium\" id={`${uid}-when`}>\n                When you show up\n              </h3>\n              <ul className=\"mt-3 flex flex-col gap-2.5\">\n                {byTime.map((t) => (\n                  <li className=\"grid grid-cols-[4.5rem_minmax(0,1fr)_2.5rem] items-center gap-3\" key={t.value}>\n                    <span className=\"text-muted-foreground text-xs\">{t.label}</span>\n                    <span className=\"relative h-2 overflow-hidden rounded-full bg-muted\">\n                      <motion.span\n                        animate={{ scaleX: t.pct / 100 }}\n                        className={cn(\"absolute inset-0 origin-left rounded-full\", t.pct === bestPct ? \"bg-primary\" : \"bg-primary/40\")}\n                        initial={false}\n                        transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 200, damping: 30 }}\n                      />\n                    </span>\n                    <span className=\"text-right font-medium text-xs tabular-nums\">\n                      <Tween value={t.pct} />%\n                    </span>\n                  </li>\n                ))}\n              </ul>\n              {bestTime && (\n                <p className=\"mt-3 text-pretty text-muted-foreground text-xs\">\n                  {bestTime.label} habits stick best this week. Missed one? Tap a square above to fill in an earlier day.\n                </p>\n              )}\n            </section>\n          </aside>\n        </div>\n      </div>\n\n      <AddHabitSheet\n        onAdd={addHabit}\n        onClose={() => {\n          setSheet(false);\n          addRef.current?.focus();\n        }}\n        open={sheet}\n        wide={wide}\n      />\n    </div>\n  );\n}\n\nfunction Stat({ label, value, hint }: { label: string; value: React.ReactNode; hint?: string }) {\n  return (\n    <div className=\"flex min-w-0 flex-col gap-1 px-4 py-3\">\n      <span className=\"truncate text-muted-foreground text-xs\">{label}</span>\n      <span className=\"font-heading font-semibold text-xl tracking-tight\">{value}</span>\n      {hint ? <span className=\"-mt-1 truncate text-muted-foreground text-xs\">{hint}</span> : null}\n    </div>\n  );\n}\n\nfunction GridCell({\n  habit,\n  value,\n  future,\n  selected,\n  day,\n  before,\n  onToggle,\n}: {\n  habit: Habit;\n  value: number;\n  future: boolean;\n  before?: boolean;\n  selected: boolean;\n  day: string;\n  onToggle: () => void;\n}) {\n  const reduce = useReducedMotion();\n  if (before) return <span aria-hidden className=\"mx-auto block size-1 rounded-full bg-border\" />;\n  const done = isDone(habit, value);\n  const partial = habit.goal && !done && value > 0 ? value / habit.goal.count : 0;\n  const t = tones[habit.tone];\n  const state = future ? \"upcoming\" : done ? \"done\" : partial ? `${value} of ${habit.goal?.count}` : \"missed\";\n  return (\n    <button\n      aria-label={`${habit.name}, ${day}: ${state}`}\n      aria-pressed={future ? undefined : done}\n      className={cn(\n        \"relative mx-auto flex size-6 cursor-pointer items-center justify-center overflow-hidden rounded-[min(var(--radius-md),--spacing(2))] outline-none after:absolute after:-inset-1 after:content-[''] focus-visible:ring-2 focus-visible:ring-ring disabled:cursor-default\",\n        press,\n        future ? \"border border-dashed\" : \"bg-muted\",\n        selected && !future && \"ring-1 ring-foreground/40 ring-offset-1 ring-offset-card\",\n      )}\n      disabled={future}\n      onClick={onToggle}\n      type=\"button\"\n    >\n      {partial > 0 && (\n        <motion.span\n          animate={{ scaleY: partial }}\n          className={cn(\"absolute inset-0 origin-bottom\", t.soft)}\n          initial={false}\n          transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 300, damping: 30 }}\n        />\n      )}\n      <motion.span\n        animate={{ opacity: done ? 1 : 0, scale: done ? 1 : 0.6 }}\n        className={cn(\"absolute inset-0 rounded-[inherit]\", t.fill)}\n        initial={false}\n        transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 600, damping: 32 }}\n      />\n    </button>\n  );\n}\n\nfunction HabitRow({\n  habit,\n  day,\n  streak,\n  fresh,\n  onChange,\n}: {\n  habit: Habit;\n  day: number;\n  streak: number;\n  fresh: boolean;\n  onChange: (v: number) => void;\n}) {\n  const reduce = useReducedMotion();\n  const value = habit.week[day] ?? 0;\n  const done = isDone(habit, value);\n  const t = tones[habit.tone];\n  const Icon = icons[habit.icon].icon;\n  const [burst, setBurst] = useState(0);\n\n  const tick = () => {\n    if (habit.goal) {\n      if (value >= habit.goal.count) return;\n      const next = value + 1;\n      onChange(next);\n      if (next === habit.goal.count && !reduce) setBurst((b) => b + 1);\n    } else {\n      onChange(done ? 0 : 1);\n      if (!done && !reduce) setBurst((b) => b + 1);\n    }\n  };\n\n  const sub = habit.goal\n    ? done\n      ? `${habit.goal.count} ${habit.goal.unit} · done`\n      : `${value} of ${habit.goal.count} ${habit.goal.unit}`\n    : done\n      ? \"Done\"\n      : \"Once a day\";\n\n  return (\n    <motion.li\n      animate={{ opacity: 1, y: 0 }}\n      className={cn(\n        \"relative flex items-center gap-3 px-3 py-3 transition-colors duration-300 @md:px-4\",\n        fresh && \"bg-accent/50\",\n      )}\n      exit={{ opacity: 0 }}\n      initial={reduce ? { opacity: 0 } : { opacity: 0, y: 8 }}\n      layout={reduce ? false : \"position\"}\n      transition={spring}\n    >\n      <span className={cn(\"flex size-9 shrink-0 items-center justify-center rounded-lg\", t.tile)}>\n        <Icon aria-hidden className=\"size-4.5\" />\n      </span>\n      <div className=\"flex min-w-0 flex-1 flex-col gap-0.5\">\n        <span className=\"truncate font-medium\">{habit.name}</span>\n        <span className=\"flex items-center gap-2 text-muted-foreground text-xs\">\n          <span className=\"truncate tabular-nums\">{sub}</span>\n          {streak > 0 && (\n            <span className=\"flex shrink-0 items-center gap-0.5 text-foreground\">\n              <FlameIcon aria-hidden className=\"size-3 text-orange-500 dark:text-orange-400\" />\n              <Tween value={streak} />\n              <span className=\"sr-only\"> day streak</span>\n            </span>\n          )}\n        </span>\n      </div>\n      {habit.goal && value > 0 && !done && (\n        <Button\n          aria-label={`Take one off ${habit.name}`}\n          className={press}\n          onClick={() => onChange(value - 1)}\n          size=\"icon-sm\"\n          variant=\"ghost\"\n        >\n          <MinusIcon />\n        </Button>\n      )}\n      <TickButton\n        burst={burst}\n        done={done}\n        goal={habit.goal}\n        label={habit.name}\n        onTick={tick}\n        onUndo={() => onChange(0)}\n        tone={habit.tone}\n        value={value}\n      />\n    </motion.li>\n  );\n}\n\nfunction TickButton({\n  done,\n  value,\n  goal,\n  tone,\n  label,\n  burst,\n  onTick,\n  onUndo,\n}: {\n  done: boolean;\n  value: number;\n  goal?: { count: number; unit: string };\n  tone: HabitTone;\n  label: string;\n  burst: number;\n  onTick: () => void;\n  onUndo: () => void;\n}) {\n  const reduce = useReducedMotion();\n  const t = tones[tone];\n  const level = goal ? Math.min(value / goal.count, 1) : done ? 1 : 0;\n  const quick = reduce ? { duration: 0 } : { type: \"spring\", stiffness: 520, damping: 34 } as const;\n\n  // The row sits in an AnimatePresence with initial={false}; reset that here so the burst and wave still play.\n  return (\n    <PresenceContext.Provider value={null}>\n    <span className=\"relative shrink-0\">\n      <button\n        aria-label={\n          goal\n            ? done\n              ? `${label}: ${goal.count} of ${goal.count} ${goal.unit}. Reset`\n              : `Add one to ${label}, ${value} of ${goal.count} ${goal.unit}`\n            : label\n        }\n        aria-pressed={goal ? undefined : done}\n        className={cn(\n          \"relative flex size-10 cursor-pointer items-center justify-center overflow-hidden rounded-full outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card\",\n          press,\n          \"active:scale-[0.95]\",\n          \"inset-ring-2 inset-ring-border hover:inset-ring-foreground/30\",\n        )}\n        onClick={goal && done ? onUndo : onTick}\n        type=\"button\"\n      >\n        {goal ? (\n          <>\n            <motion.span\n              animate={{ y: `${(1 - level) * 100}%` }}\n              aria-hidden\n              className={cn(\"absolute inset-x-0 bottom-0 h-full\", done ? t.fill : t.soft)}\n              initial={false}\n              transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 160, damping: 20 }}\n            >\n              {!done && level > 0 && (\n                <motion.svg\n                  animate={reduce ? undefined : { x: [\"0%\", \"-50%\"] }}\n                  aria-hidden\n                  className=\"absolute -top-[5px] left-0 h-1.5 w-[200%]\"\n                  key={`w${value}`}\n                  preserveAspectRatio=\"none\"\n                  transition={{ duration: 0.9, ease: \"easeOut\" }}\n                  viewBox=\"0 0 80 6\"\n                >\n                  <path className={waveFill[tone]} d=\"M0 3 Q10 0 20 3 T40 3 T60 3 T80 3 V6 H0 Z\" />\n                </motion.svg>\n              )}\n            </motion.span>\n            {!done && (\n              <span className=\"relative font-medium text-xs tabular-nums\">\n                {value}\n              </span>\n            )}\n          </>\n        ) : (\n          <motion.span\n            animate={{ opacity: done ? 1 : 0, scale: done ? 1 : 0.95 }}\n            aria-hidden\n            className={cn(\"absolute inset-0 rounded-full\", t.fill)}\n            initial={false}\n            transition={quick}\n          />\n        )}\n        <svg aria-hidden className={cn(\"relative size-5\", t.on)} fill=\"none\" viewBox=\"0 0 24 24\">\n          <motion.path\n            animate={{ pathLength: done ? 1 : 0, opacity: done ? 1 : 0 }}\n            d=\"M5.5 12.5 10 17 18.5 7.5\"\n            initial={false}\n            stroke=\"currentColor\"\n            strokeLinecap=\"round\"\n            strokeLinejoin=\"round\"\n            strokeWidth={2.75}\n            transition={reduce ? { duration: 0 } : { duration: 0.2, ease: easeOut, delay: done ? 0.05 : 0 }}\n          />\n        </svg>\n      </button>\n      {burst > 0 && (\n        <span aria-hidden className=\"pointer-events-none absolute inset-0\" key={burst}>\n          {Array.from({ length: 8 }, (_, i) => {\n            const a = (i / 8) * Math.PI * 2;\n            return (\n              <motion.span\n                animate={{ x: Math.cos(a) * 29, y: Math.sin(a) * 29, opacity: 0, scale: 0.5 }}\n                className={cn(\"absolute top-1/2 left-1/2 -mt-[3px] -ml-[3px] size-1.5 rounded-full\", t.fill)}\n                initial={{ x: Math.cos(a) * 19, y: Math.sin(a) * 19, opacity: 1, scale: 1 }}\n                key={i}\n                transition={{ duration: 0.4, ease: easeOut }}\n              />\n            );\n          })}\n        </span>\n      )}\n    </span>\n    </PresenceContext.Provider>\n  );\n}\n\nconst waveFill: Record<HabitTone, string> = {\n  sky: \"fill-sky-500/30 dark:fill-sky-400/40\",\n  amber: \"fill-amber-500/30 dark:fill-amber-400/40\",\n  violet: \"fill-violet-500/30 dark:fill-violet-400/40\",\n  teal: \"fill-teal-500/30 dark:fill-teal-400/40\",\n  orange: \"fill-orange-500/30 dark:fill-orange-400/40\",\n  rose: \"fill-rose-500/30 dark:fill-rose-400/40\",\n};\n\nfunction AddHabitSheet({\n  open,\n  wide,\n  onClose,\n  onAdd,\n}: {\n  open: boolean;\n  wide: boolean;\n  onClose: () => void;\n  onAdd: (h: Omit<Habit, \"id\" | \"carry\" | \"week\">) => void;\n}) {\n  const reduce = useReducedMotion();\n  const uid = useId().replace(/:/g, \"\");\n  const nameRef = useRef<HTMLInputElement>(null);\n  const [name, setName] = useState(\"\");\n  const [icon, setIcon] = useState<HabitIcon>(\"meditate\");\n  const [tone, setTone] = useState<HabitTone>(\"teal\");\n  const [time, setTime] = useState<HabitTime>(\"anytime\");\n  const [counted, setCounted] = useState(false);\n  const [count, setCount] = useState(3);\n  const [unit, setUnit] = useState(\"times\");\n  const [tried, setTried] = useState(false);\n\n  useEffect(() => {\n    if (!open) return;\n    setName(\"\");\n    setIcon(\"meditate\");\n    setTone(\"teal\");\n    setTime(\"anytime\");\n    setCounted(false);\n    setCount(3);\n    setUnit(\"times\");\n    setTried(false);\n    const f = setTimeout(() => nameRef.current?.focus(), 60);\n    return () => clearTimeout(f);\n  }, [open]);\n\n  const invalid = !name.trim();\n  const submit = (e: FormEvent) => {\n    e.preventDefault();\n    setTried(true);\n    if (invalid) {\n      nameRef.current?.focus();\n      return;\n    }\n    onAdd({\n      name: name.trim(),\n      icon,\n      tone,\n      time,\n      goal: counted ? { count, unit: unit.trim() || \"times\" } : undefined,\n    });\n    onClose();\n  };\n\n  const Icon = icons[icon].icon;\n  const t = tones[tone];\n  const motionIn = wide ? { x: 0 } : { y: 0 };\n  const motionOut = wide ? { x: \"100%\" } : { y: \"100%\" };\n\n  return (\n    <AnimatePresence>\n      {open && (\n        <>\n          <motion.div\n            animate={{ opacity: 1 }}\n            aria-hidden\n            className=\"absolute inset-0 z-40 bg-black/32\"\n            exit={{ opacity: 0 }}\n            initial={{ opacity: 0 }}\n            key=\"scrim\"\n            onClick={onClose}\n            transition={{ duration: 0.2, ease: easeOut }}\n          />\n          <motion.form\n            animate={motionIn}\n            aria-labelledby={`${uid}-title`}\n            aria-modal\n            className={cn(\n              \"absolute z-50 flex flex-col bg-popover text-popover-foreground shadow-lg/10 outline-none\",\n              wide\n                ? \"inset-y-0 right-0 w-[400px] border-l\"\n                : \"inset-x-0 bottom-0 max-h-[92%] rounded-t-2xl border-t\",\n            )}\n            exit={reduce ? { opacity: 0 } : motionOut}\n            initial={reduce ? { opacity: 0 } : motionOut}\n            key=\"sheet\"\n            noValidate\n            onKeyDown={(e) => {\n              if (e.key === \"Escape\") {\n                e.stopPropagation();\n                onClose();\n              }\n            }}\n            onSubmit={submit}\n            role=\"dialog\"\n            transition={reduce ? { duration: 0.15 } : { type: \"spring\", stiffness: 440, damping: 42 }}\n          >\n            {!wide && <span aria-hidden className=\"mx-auto mt-2 h-1 w-9 shrink-0 rounded-full bg-border\" />}\n            <div className=\"flex items-center gap-2 px-4 pt-3 pb-2 @3xl:px-5 @3xl:pt-4\">\n              <h2 className=\"flex-1 font-heading font-semibold text-base\" id={`${uid}-title`}>\n                New habit\n              </h2>\n              <Button aria-label=\"Close\" onClick={onClose} size=\"icon-sm\" variant=\"ghost\">\n                <XIcon />\n              </Button>\n            </div>\n\n            <div className=\"flex min-h-0 flex-1 flex-col gap-5 overflow-y-auto px-4 pt-2 pb-4 @3xl:px-5\">\n              <div className=\"flex items-center gap-3 rounded-xl border bg-card p-3\">\n                <motion.span\n                  className={cn(\"flex size-10 shrink-0 items-center justify-center rounded-xl transition-colors duration-200\", t.tile)}\n                  key={icon}\n                  initial={reduce ? false : { scale: 0.9, opacity: 0.6 }}\n                  animate={{ scale: 1, opacity: 1 }}\n                  transition={{ duration: 0.18, ease: easeOut }}\n                >\n                  <Icon aria-hidden className=\"size-5\" />\n                </motion.span>\n                <div className=\"flex min-w-0 flex-1 flex-col gap-0.5\">\n                  <span className={cn(\"truncate font-medium\", !name.trim() && \"text-muted-foreground\")}>\n                    {name.trim() || \"Your new habit\"}\n                  </span>\n                  <span className=\"truncate text-muted-foreground text-xs\">\n                    {times.find((x) => x.value === time)?.label} · {counted ? `${count} ${unit.trim() || \"times\"} a day` : \"Once a day\"}\n                  </span>\n                </div>\n                <span aria-hidden className=\"size-10 shrink-0 rounded-full border-2 border-border\" />\n              </div>\n\n              <div className=\"flex flex-col gap-2\">\n                <label className=\"font-medium\" htmlFor={`${uid}-name`}>\n                  Name\n                </label>\n                <Input\n                  aria-describedby={tried && invalid ? `${uid}-err` : undefined}\n                  aria-invalid={tried && invalid ? true : undefined}\n                  autoComplete=\"off\"\n                  id={`${uid}-name`}\n                  maxLength={40}\n                  onChange={(e) => setName(e.target.value)}\n                  placeholder=\"e.g. Meditate for 5 minutes\"\n                  ref={nameRef}\n                  value={name}\n                />\n                <AnimatePresence initial={false}>\n                  {tried && invalid && (\n                    <motion.p\n                      animate={{ opacity: 1, height: \"auto\" }}\n                      className=\"overflow-hidden text-destructive-foreground text-xs\"\n                      exit={{ opacity: 0, height: 0 }}\n                      id={`${uid}-err`}\n                      initial={{ opacity: 0, height: 0 }}\n                      transition={{ duration: 0.18, ease: easeOut }}\n                    >\n                      Give it a name you’ll recognise at a glance.\n                    </motion.p>\n                  )}\n                </AnimatePresence>\n                <div className=\"flex flex-wrap gap-1.5\">\n                  {suggestions.map((s) => (\n                    <button\n                      className={cn(\n                        \"h-7 cursor-pointer rounded-full border bg-background px-2.5 text-xs outline-none hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring\",\n                        press,\n                      )}\n                      key={s.name}\n                      onClick={() => {\n                        setName(s.name);\n                        setIcon(s.icon);\n                        setTone(s.tone);\n                        setTime(s.time);\n                        setCounted(Boolean(s.goal));\n                        if (s.goal) {\n                          setCount(s.goal.count);\n                          setUnit(s.goal.unit);\n                        }\n                      }}\n                      type=\"button\"\n                    >\n                      {s.name}\n                    </button>\n                  ))}\n                </div>\n              </div>\n\n              <fieldset className=\"flex flex-col gap-2\">\n                <legend className=\"mb-2 font-medium\">Icon</legend>\n                <div className=\"grid grid-cols-7 gap-1.5\">\n                  {(Object.keys(icons) as HabitIcon[]).map((k) => {\n                    const I = icons[k].icon;\n                    const on = k === icon;\n                    return (\n                      <button\n                        aria-label={icons[k].label}\n                        aria-pressed={on}\n                        className={cn(\n                          \"flex aspect-square cursor-pointer items-center justify-center rounded-lg outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n                          press,\n                          on ? t.tile : \"text-muted-foreground hover:bg-accent hover:text-foreground\",\n                          on && \"ring-1 ring-current/30\",\n                        )}\n                        key={k}\n                        onClick={() => setIcon(k)}\n                        type=\"button\"\n                      >\n                        <I className=\"size-4.5\" />\n                      </button>\n                    );\n                  })}\n                </div>\n              </fieldset>\n\n              <fieldset className=\"flex flex-col gap-2\">\n                <legend className=\"mb-2 font-medium\">Colour</legend>\n                <div className=\"flex gap-2.5\">\n                  {(Object.keys(tones) as HabitTone[]).map((k) => (\n                    <button\n                      aria-label={tones[k].label}\n                      aria-pressed={k === tone}\n                      className={cn(\n                        \"relative flex size-8 cursor-pointer items-center justify-center rounded-full outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-popover\",\n                        press,\n                      )}\n                      key={k}\n                      onClick={() => setTone(k)}\n                      type=\"button\"\n                    >\n                      <span className={cn(\"size-6 rounded-full\", tones[k].fill)} />\n                      {k === tone && (\n                        <motion.span\n                          className=\"absolute inset-0 rounded-full ring-2 ring-foreground/70\"\n                          layoutId={`${uid}-swatch`}\n                          transition={reduce ? { duration: 0 } : spring}\n                        />\n                      )}\n                    </button>\n                  ))}\n                </div>\n              </fieldset>\n\n              <Choice\n                label=\"When\"\n                onChange={(v) => setTime(v as HabitTime)}\n                options={times}\n                uid={`${uid}-time`}\n                value={time}\n              />\n\n              <div className=\"flex flex-col gap-3\">\n                <Choice\n                  label=\"Goal\"\n                  onChange={(v) => setCounted(v === \"count\")}\n                  options={[\n                    { value: \"once\", label: \"Once a day\" },\n                    { value: \"count\", label: \"Count it\" },\n                  ]}\n                  uid={`${uid}-goal`}\n                  value={counted ? \"count\" : \"once\"}\n                />\n                <AnimatePresence initial={false}>\n                  {counted && (\n                    <motion.div\n                      animate={{ opacity: 1, height: \"auto\" }}\n                      className=\"overflow-hidden\"\n                      exit={{ opacity: 0, height: 0 }}\n                      initial={{ opacity: 0, height: 0 }}\n                      transition={reduce ? { duration: 0 } : { duration: 0.22, ease: easeOut }}\n                    >\n                      <div className=\"flex items-center gap-2 pt-0.5\">\n                        <div className=\"flex items-center rounded-lg border bg-background\">\n                          <Button\n                            aria-label=\"Fewer\"\n                            disabled={count <= 2}\n                            onClick={() => setCount((c) => Math.max(2, c - 1))}\n                            size=\"icon-sm\"\n                            variant=\"ghost\"\n                          >\n                            <MinusIcon />\n                          </Button>\n                          <span aria-live=\"polite\" className=\"w-7 text-center font-medium tabular-nums\">\n                            {count}\n                          </span>\n                          <Button\n                            aria-label=\"More\"\n                            disabled={count >= 20}\n                            onClick={() => setCount((c) => Math.min(20, c + 1))}\n                            size=\"icon-sm\"\n                            variant=\"ghost\"\n                          >\n                            <PlusIcon />\n                          </Button>\n                        </div>\n                        <Input\n                          aria-label=\"Unit\"\n                          className=\"flex-1\"\n                          maxLength={14}\n                          onChange={(e) => setUnit(e.target.value)}\n                          placeholder=\"glasses, pages…\"\n                          value={unit}\n                        />\n                        <span className=\"shrink-0 text-muted-foreground\">a day</span>\n                      </div>\n                    </motion.div>\n                  )}\n                </AnimatePresence>\n              </div>\n            </div>\n\n            <div className=\"flex gap-2 border-t px-4 py-3 @3xl:px-5\">\n              <Button className={cn(\"flex-1\", press)} onClick={onClose} variant=\"outline\">\n                Cancel\n              </Button>\n              <Button className={cn(\"flex-1\", press)} type=\"submit\">\n                Add habit\n              </Button>\n            </div>\n          </motion.form>\n        </>\n      )}\n    </AnimatePresence>\n  );\n}\n\nfunction Choice({\n  label,\n  value,\n  options,\n  onChange,\n  uid,\n}: {\n  label: string;\n  value: string;\n  options: { value: string; label: string }[];\n  onChange: (v: string) => void;\n  uid: string;\n}) {\n  const reduce = useReducedMotion();\n  return (\n    <fieldset className=\"flex flex-col gap-2\">\n      <legend className=\"mb-2 font-medium\">{label}</legend>\n      <div className=\"flex rounded-lg bg-muted p-0.5\" role=\"radiogroup\">\n        {options.map((o) => {\n          const on = o.value === value;\n          return (\n            <button\n              aria-checked={on}\n              className={cn(\n                \"relative h-8 flex-1 cursor-pointer rounded-md text-sm outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n                press,\n                on ? \"font-medium text-foreground\" : \"text-muted-foreground hover:text-foreground\",\n              )}\n              key={o.value}\n              onClick={() => onChange(o.value)}\n              role=\"radio\"\n              type=\"button\"\n            >\n              {on && (\n                <motion.span\n                  className=\"absolute inset-0 -z-10 rounded-md bg-background shadow-xs/5 dark:bg-input\"\n                  layoutId={uid}\n                  transition={reduce ? { duration: 0 } : spring}\n                />\n              )}\n              {o.label}\n            </button>\n          );\n        })}\n      </div>\n    </fieldset>\n  );\n}\n"
    }
  ]
}