{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "delivery-tracker",
  "type": "registry:block",
  "title": "Delivery Tracker",
  "description": "Live order tracking on a hand-drawn city map: the courier rides the route as the ETA counts down, you can zoom in to follow them, message with quick replies, and rate and tip once it lands.",
  "categories": [
    "screen",
    "Commerce"
  ],
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils",
    "https://coss.com/ui/r/avatar.json",
    "https://coss.com/ui/r/button.json",
    "https://coss.com/ui/r/input.json",
    "https://coss.com/ui/r/menu.json",
    "https://coss.com/ui/r/textarea.json"
  ],
  "files": [
    {
      "path": "registry/devl/delivery-tracker.tsx",
      "type": "registry:component",
      "content": "\"use client\";\n\nimport {\n  BikeIcon,\n  CheckIcon,\n  ChevronLeftIcon,\n  CircleHelpIcon,\n  HouseIcon,\n  MessageCircleIcon,\n  MinusIcon,\n  PencilIcon,\n  PhoneIcon,\n  PlusIcon,\n  RotateCcwIcon,\n  SendIcon,\n  StarIcon,\n  StoreIcon,\n  XCircleIcon,\n} from \"lucide-react\";\nimport {\n  AnimatePresence,\n  animate,\n  motion,\n  useMotionValue,\n  useMotionValueEvent,\n  useReducedMotion,\n  useTransform,\n} from \"motion/react\";\nimport { type FormEvent, useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from \"react\";\nimport { cn } from \"@/lib/utils\";\nimport { Avatar, AvatarFallback } from \"@/components/ui/avatar\";\nimport { Button } from \"@/components/ui/button\";\nimport { Input } from \"@/components/ui/input\";\nimport { Menu, MenuItem, MenuLinkItem, MenuPopup, MenuSeparator, MenuTrigger } from \"@/components/ui/menu\";\nimport { Textarea } from \"@/components/ui/textarea\";\n\nexport interface DeliveryItem {\n  id: string;\n  name: string;\n  detail: string;\n  price: number;\n  qty: number;\n  art: \"bread\" | \"milk\" | \"fruit\" | \"flowers\";\n}\n\nexport interface DeliveryCourier {\n  name: string;\n  rating: number;\n  trips: number;\n  vehicle: string;\n  phone: string;\n}\n\nexport interface DeliveryTrackerProps {\n  className?: string;\n  storeName?: string;\n  orderId?: string;\n  address?: string;\n  courier?: DeliveryCourier;\n  items?: DeliveryItem[];\n  routeMinutes?: number;\n  startAt?: number;\n  simulateSeconds?: number;\n  now?: string;\n  onBack?: () => void;\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]\";\n\nconst defaultCourier: DeliveryCourier = {\n  name: \"Tomás Reyes\",\n  rating: 4.9,\n  trips: 1284,\n  vehicle: \"Green e-bike\",\n  phone: \"+15555550142\",\n};\n\nconst defaultItems: DeliveryItem[] = [\n  { id: \"d1\", name: \"Country sourdough\", detail: \"800 g, sliced\", price: 7.5, qty: 1, art: \"bread\" },\n  { id: \"d2\", name: \"Barista oat milk\", detail: \"1 L\", price: 3.2, qty: 2, art: \"milk\" },\n  { id: \"d3\", name: \"Clementines\", detail: \"1 kg net\", price: 4.8, qty: 1, art: \"fruit\" },\n  { id: \"d4\", name: \"Seasonal stems\", detail: \"Small bunch\", price: 12, qty: 1, art: \"flowers\" },\n];\n\nconst W = 800;\nconst H = 600;\nconst ROUTE = \"M180 500 H384 Q400 500 400 484 V376 Q400 360 416 360 H544 Q560 360 560 344 V236 Q560 220 576 220 H624 Q640 220 640 204 V150\";\nconst START = { x: 180, y: 500 };\nconst END = { x: 640, y: 150 };\nconst xs = [0, 100, 240, 400, 560, 720, 800];\nconst ys = [0, 90, 220, 360, 500, 600];\n\nconst stages = [\"Confirmed\", \"Packed\", \"On the way\", \"Delivered\"] as const;\n\nfunction phaseText(p: number, first: string) {\n  if (p >= 1) return `${first} is at your door`;\n  if (p > 0.93) return `${first} is pulling up outside`;\n  if (p > 0.8) return `${first} is turning onto Alder Row`;\n  if (p > 0.62) return `${first} is crossing Calder Bridge`;\n  if (p > 0.45) return `${first} is riding along Quay Road`;\n  if (p > 0.27) return `${first} is heading up Linden Avenue`;\n  return `${first} is leaving Harbor Street`;\n}\n\nfunction addMinutes(hhmm: string, mins: number) {\n  const [h, m] = hhmm.split(\":\").map(Number);\n  const total = h * 60 + m + Math.round(mins);\n  const hh = Math.floor(total / 60) % 24;\n  const mm = total % 60;\n  const suffix = hh >= 12 ? \"pm\" : \"am\";\n  return `${hh % 12 || 12}:${String(mm).padStart(2, \"0\")} ${suffix}`;\n}\n\nfunction initials(name: string) {\n  return name\n    .split(/\\s+/)\n    .map((p) => p[0])\n    .join(\"\")\n    .slice(0, 2)\n    .toUpperCase();\n}\n\nfunction ItemArt({ art }: { art: DeliveryItem[\"art\"] }) {\n  const tile = {\n    bread: \"bg-amber-500/14 dark:bg-amber-400/12\",\n    milk: \"bg-sky-500/12 dark:bg-sky-400/12\",\n    fruit: \"bg-orange-500/14 dark:bg-orange-400/12\",\n    flowers: \"bg-rose-500/12 dark:bg-rose-400/12\",\n  }[art];\n  return (\n    <span aria-hidden className={cn(\"flex size-10 shrink-0 items-center justify-center rounded-lg\", tile)}>\n      <svg className=\"size-6\" viewBox=\"0 0 32 32\">\n        {art === \"bread\" && (\n          <>\n            <path className=\"fill-amber-600 dark:fill-amber-500\" d=\"M5 18c0-6 5-10 11-10s11 4 11 10v4a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2z\" />\n            <path className=\"fill-none stroke-amber-200/80\" d=\"M11 13l2 3M16 12l1 3.5M21 13l-1 3\" strokeLinecap=\"round\" strokeWidth=\"1.5\" />\n          </>\n        )}\n        {art === \"milk\" && (\n          <>\n            <path className=\"fill-white dark:fill-sky-100\" d=\"M11 9h10v4l2 3v11a1 1 0 0 1-1 1H10a1 1 0 0 1-1-1V16l2-3z\" />\n            <rect className=\"fill-sky-500 dark:fill-sky-400\" height=\"5\" rx=\"1\" width=\"14\" x=\"9\" y=\"18\" />\n            <rect className=\"fill-sky-700/70 dark:fill-sky-600\" height=\"3\" rx=\"0.5\" width=\"10\" x=\"11\" y=\"5\" />\n          </>\n        )}\n        {art === \"fruit\" && (\n          <>\n            <circle className=\"fill-orange-500 dark:fill-orange-400\" cx=\"12\" cy=\"19\" r=\"6.5\" />\n            <circle className=\"fill-orange-400 dark:fill-orange-300\" cx=\"20.5\" cy=\"17\" r=\"6\" />\n            <path className=\"fill-emerald-600 dark:fill-emerald-500\" d=\"M20 11c1-3 4-4 6-3-1 2-3 3.5-6 3z\" />\n          </>\n        )}\n        {art === \"flowers\" && (\n          <>\n            <path className=\"fill-none stroke-emerald-600 dark:stroke-emerald-500\" d=\"M16 28V15M16 22l-5-6M16 20l5-5\" strokeLinecap=\"round\" strokeWidth=\"1.6\" />\n            <circle className=\"fill-rose-500 dark:fill-rose-400\" cx=\"16\" cy=\"10\" r=\"4\" />\n            <circle className=\"fill-rose-400 dark:fill-rose-300\" cx=\"10\" cy=\"14\" r=\"3\" />\n            <circle className=\"fill-amber-400 dark:fill-amber-300\" cx=\"22\" cy=\"13\" r=\"3\" />\n          </>\n        )}\n      </svg>\n    </span>\n  );\n}\n\nfunction ItemArtStore() {\n  return (\n    <span aria-hidden className=\"flex size-8 shrink-0 items-center justify-center rounded-lg bg-muted text-muted-foreground\">\n      <StoreIcon className=\"size-4\" />\n    </span>\n  );\n}\n\nfunction Tween({ value }: { value: number }) {\n  const reduce = useReducedMotion();\n  const mv = useMotionValue(value);\n  const text = useTransform(mv, (n) => String(Math.round(n)));\n  useEffect(() => {\n    if (reduce) {\n      mv.set(value);\n      return;\n    }\n    const controls = animate(mv, value, { duration: 0.4, ease: easeOut });\n    return () => controls.stop();\n  }, [value, reduce, mv]);\n  return <motion.span className=\"tabular-nums\">{text}</motion.span>;\n}\n\nconst ZOOMS = [1, 1.5, 2.2];\n\nexport function DeliveryTracker({\n  className,\n  storeName = \"Larder & Co.\",\n  orderId = \"LC-20931\",\n  address = \"Flat 3, 18 Alder Row\",\n  courier = defaultCourier,\n  items = defaultItems,\n  routeMinutes = 18,\n  startAt = 0.3,\n  simulateSeconds = 40,\n  now = \"19:24\",\n  onBack,\n}: DeliveryTrackerProps) {\n  const reduce = useReducedMotion();\n  const uid = useId().replace(/:/g, \"\");\n  const first = courier.name.split(\" \")[0];\n  const progress = useMotionValue(startAt);\n  const zoomMv = useMotionValue(1);\n  const [zoomIdx, setZoomIdx] = useState(0);\n  const [minutes, setMinutes] = useState(Math.ceil(routeMinutes * (1 - startAt)));\n  const [phase, setPhase] = useState(() => phaseText(startAt, first));\n  const [status, setStatus] = useState<\"moving\" | \"delivered\">(\"moving\");\n  const [run, setRun] = useState(0);\n  const [chat, setChat] = useState(false);\n  const [rating, setRating] = useState(0);\n  const [tip, setTip] = useState<number | null>(null);\n  const [note, setNote] = useState(\"Leave with the concierge if I’m not in.\");\n  const [editing, setEditing] = useState(false);\n  const [draft, setDraft] = useState(note);\n  const routeRef = useRef<SVGPathElement>(null);\n  const cameraRef = useRef<SVGGElement>(null);\n  const courierRef = useRef<SVGGElement>(null);\n  const pinsRef = useRef<(SVGGElement | null)[]>([]);\n  const chatBtnRef = useRef<HTMLButtonElement>(null);\n  const svgRef = useRef<SVGSVGElement>(null);\n  const renderScale = useRef(1);\n  const measured = useRef(false);\n  const arrival = addMinutes(now, routeMinutes * (1 - startAt));\n\n  const draw = useCallback(() => {\n    const path = routeRef.current;\n    if (!path) return;\n    const len = path.getTotalLength();\n    const pt = path.getPointAtLength(len * Math.min(Math.max(progress.get(), 0), 1));\n    const z = zoomMv.get();\n    const m = (1.1 / (z * renderScale.current)).toFixed(4);\n    let tx = W / 2 - pt.x * z;\n    let ty = H / 2 - pt.y * z;\n    tx = Math.min(0, Math.max(W - W * z, tx));\n    ty = Math.min(0, Math.max(H - H * z, ty));\n    cameraRef.current?.setAttribute(\"transform\", `translate(${tx.toFixed(2)} ${ty.toFixed(2)}) scale(${z.toFixed(4)})`);\n    courierRef.current?.setAttribute(\"transform\", `translate(${pt.x.toFixed(2)} ${pt.y.toFixed(2)}) scale(${m})`);\n    const pins = [START, END];\n    pinsRef.current.forEach((g, i) => {\n      g?.setAttribute(\"transform\", `translate(${pins[i].x} ${pins[i].y}) scale(${m})`);\n    });\n  }, [progress, zoomMv]);\n\n  useMotionValueEvent(progress, \"change\", (p) => {\n    draw();\n    setMinutes(Math.max(1, Math.ceil(routeMinutes * (1 - p))));\n    setPhase(phaseText(p, first));\n  });\n  useMotionValueEvent(zoomMv, \"change\", draw);\n\n  useLayoutEffect(() => {\n    const el = svgRef.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([e]) => {\n      const { width, height } = e.contentRect;\n      renderScale.current = Math.max(width / W, height / H) || 1;\n      if (!measured.current) {\n        measured.current = true;\n        if (renderScale.current < 0.75) {\n          zoomMv.set(ZOOMS[1]);\n          setZoomIdx(1);\n        }\n      }\n      draw();\n    });\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [draw, zoomMv]);\n\n  useEffect(() => {\n    progress.set(startAt);\n    setStatus(\"moving\");\n    setRating(0);\n    setTip(null);\n    draw();\n    const controls = animate(progress, 1, {\n      duration: simulateSeconds,\n      ease: \"linear\",\n      onComplete: () => setStatus(\"delivered\"),\n    });\n    return () => controls.stop();\n  }, [run, progress, startAt, simulateSeconds, draw]);\n\n  useEffect(() => {\n    const controls = animate(zoomMv, ZOOMS[zoomIdx], reduce ? { duration: 0 } : { type: \"spring\", stiffness: 260, damping: 34 });\n    return () => controls.stop();\n  }, [zoomIdx, zoomMv, reduce]);\n\n  const delivered = status === \"delivered\";\n  const stage = delivered ? 3 : 2;\n  const itemsTotal = items.reduce((s, i) => s + i.price * i.qty, 0);\n  const fee = 2.99;\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    >\n      <header className=\"flex h-14 shrink-0 items-center gap-2.5 border-b px-3 @3xl:px-5\">\n        {onBack ? (\n          <Button aria-label=\"Back to orders\" className={press} onClick={onBack} size=\"icon-sm\" variant=\"ghost\">\n            <ChevronLeftIcon />\n          </Button>\n        ) : (\n          <ItemArtStore />\n        )}\n        <div className=\"flex min-w-0 flex-col\">\n          <span className=\"truncate font-medium\">Order from {storeName}</span>\n          <span className=\"font-mono text-muted-foreground text-xs\">#{orderId}</span>\n        </div>\n        <Menu>\n          <MenuTrigger\n            render={<Button aria-label=\"Help with this order\" className={cn(\"ms-auto\", press)} size=\"sm\" variant=\"ghost\" />}\n          >\n            <CircleHelpIcon /> <span className=\"hidden @md:inline\">Help</span>\n          </MenuTrigger>\n          <MenuPopup align=\"end\" className=\"w-60\">\n            {delivered ? (\n              <MenuItem onClick={() => setRun((r) => r + 1)}>\n                <RotateCcwIcon /> Watch the delivery again\n              </MenuItem>\n            ) : (\n              <>\n                <MenuItem onClick={() => setChat(true)}>\n                  <MessageCircleIcon /> Message {first}\n                </MenuItem>\n                <MenuLinkItem href={`tel:${courier.phone}`}>\n                  <PhoneIcon /> Call {first}\n                </MenuLinkItem>\n                <MenuItem\n                  onClick={() => {\n                    setDraft(note);\n                    setEditing(true);\n                  }}\n                >\n                  <PencilIcon /> Change drop-off note\n                </MenuItem>\n              </>\n            )}\n            <MenuSeparator />\n            <MenuItem disabled>\n              <XCircleIcon /> Cancel order\n              <span className=\"ms-auto text-muted-foreground text-xs\">{delivered ? \"Delivered\" : \"On its way\"}</span>\n            </MenuItem>\n          </MenuPopup>\n        </Menu>\n      </header>\n\n      <div className=\"flex min-h-0 flex-1 flex-col @3xl:flex-row\">\n        <div className=\"relative h-[280px] shrink-0 overflow-hidden bg-muted @3xl:h-auto @3xl:min-w-0 @3xl:flex-1\">\n          <svg\n            aria-label={`Map: ${phase}`}\n            className=\"absolute inset-0 size-full\"\n            preserveAspectRatio=\"xMidYMid slice\"\n            ref={svgRef}\n            role=\"img\"\n            viewBox={`0 0 ${W} ${H}`}\n          >\n            <g ref={cameraRef}>\n              <rect className=\"fill-muted\" height={H} width={W} />\n              {xs.slice(0, -1).flatMap((x0, i) =>\n                ys.slice(0, -1).map((y0, j) => {\n                  const x1 = xs[i + 1];\n                  const y1 = ys[j + 1];\n                  const park = i === 2 && j === 1;\n                  return (\n                    <rect\n                      className={park ? \"fill-emerald-500/18 dark:fill-emerald-400/10\" : \"fill-foreground/[0.025]\"}\n                      height={y1 - y0 - 22}\n                      key={`${x0}-${y0}`}\n                      rx={park ? 14 : 5}\n                      width={x1 - x0 - 22}\n                      x={x0 + 11}\n                      y={y0 + 11}\n                    />\n                  );\n                }),\n              )}\n              {[\n                [118, 108, 50, 38],\n                [178, 108, 44, 60],\n                [418, 108, 60, 44],\n                [490, 108, 52, 30],\n                [578, 238, 40, 40],\n                [658, 238, 46, 30],\n                [118, 378, 56, 46],\n                [258, 378, 70, 36],\n                [418, 378, 40, 50],\n                [470, 378, 72, 30],\n                [578, 378, 60, 44],\n                [258, 518, 60, 40],\n                [578, 518, 56, 44],\n                [418, 518, 44, 50],\n                [738, 108, 46, 70],\n              ].map(([x, y, w, h]) => (\n                <rect className=\"fill-foreground/[0.045]\" height={h} key={`${x}-${y}`} rx={3} width={w} x={x} y={y} />\n              ))}\n              {[\n                [282, 130],\n                [310, 150],\n                [350, 128],\n                [336, 182],\n                [270, 180],\n                [372, 168],\n              ].map(([x, y]) => (\n                <circle className=\"fill-emerald-500/30 dark:fill-emerald-400/20\" cx={x} cy={y} key={`${x}-${y}`} r={9} />\n              ))}\n              <path\n                className=\"fill-none stroke-sky-400/35 dark:stroke-sky-400/18\"\n                d=\"M-20 310 C 120 270, 220 330, 360 296 S 600 262, 820 300\"\n                strokeLinecap=\"round\"\n                strokeWidth={34}\n              />\n              <g className=\"fill-none stroke-background\" strokeLinecap=\"round\">\n                {xs.slice(1, -1).map((x) => (\n                  <line key={`v${x}`} strokeWidth={x === 400 || x === 560 ? 20 : 13} x1={x} x2={x} y1={-10} y2={H + 10} />\n                ))}\n                {ys.slice(1, -1).map((y) => (\n                  <line key={`h${y}`} strokeWidth={y === 500 || y === 360 || y === 220 ? 20 : 13} x1={-10} x2={W + 10} y1={y} y2={y} />\n                ))}\n                <line strokeWidth={11} x1={640} x2={640} y1={220} y2={128} />\n                <g strokeWidth={6}>\n                  <line x1={484} x2={484} y1={90} y2={220} />\n                  <line x1={650} x2={650} y1={360} y2={500} />\n                  <line x1={200} x2={200} y1={360} y2={500} />\n                  <line x1={100} x2={240} y1={446} y2={446} />\n                  <line x1={560} x2={720} y1={160} y2={160} />\n                  <line x1={240} x2={400} y1={574} y2={574} />\n                </g>\n              </g>\n              <g className=\"fill-muted-foreground font-medium\" fontSize={11} letterSpacing={0.4}>\n                <text x={18} y={492}>\n                  HARBOR ST\n                </text>\n                <text transform=\"translate(392 590) rotate(-90)\">LINDEN AVE</text>\n                <text x={436} y={212}>\n                  ALDER ROW\n                </text>\n                <text x={420} y={352}>\n                  QUAY RD\n                </text>\n                <text className=\"fill-emerald-700/70 dark:fill-emerald-300/60\" fontSize={10} x={262} y={208}>\n                  FERNWOOD PARK\n                </text>\n                <text className=\"fill-sky-700/60 italic dark:fill-sky-300/50\" fontSize={11} x={60} y={296}>\n                  Calder River\n                </text>\n              </g>\n              <path className=\"fill-none stroke-background\" d={ROUTE} strokeLinecap=\"round\" strokeLinejoin=\"round\" strokeWidth={13} />\n              <path\n                className=\"fill-none stroke-info\"\n                d={ROUTE}\n                ref={routeRef}\n                strokeLinecap=\"round\"\n                strokeLinejoin=\"round\"\n                strokeWidth={6}\n              />\n              <motion.path\n                className=\"fill-none stroke-muted-foreground\"\n                d={ROUTE}\n                strokeLinecap=\"round\"\n                strokeLinejoin=\"round\"\n                strokeWidth={6}\n                style={{ pathLength: progress }}\n              />\n              <g\n                ref={(el) => {\n                  pinsRef.current[0] = el;\n                }}\n                transform={`translate(${START.x} ${START.y})`}\n              >\n                <circle className=\"fill-card stroke-border\" r={15} strokeWidth={1.5} />\n                <StoreIcon className=\"text-foreground\" height={14} width={14} x={-7} y={-7} />\n              </g>\n              <g\n                ref={(el) => {\n                  pinsRef.current[1] = el;\n                }}\n                transform={`translate(${END.x} ${END.y})`}\n              >\n                {!delivered && (\n                  <motion.circle\n                    animate={{ scale: [1, 2], opacity: [0.35, 0] }}\n                    className=\"fill-foreground motion-reduce:hidden\"\n                    r={16}\n                    transition={{ duration: 2, repeat: Number.POSITIVE_INFINITY, ease: \"easeOut\" }}\n                  />\n                )}\n                <circle className=\"fill-foreground stroke-background\" r={16} strokeWidth={3} />\n                <HouseIcon className=\"text-background\" height={15} width={15} x={-7.5} y={-7.5} />\n              </g>\n              <g ref={courierRef}>\n                <circle className=\"fill-info/20\" r={24} />\n                <circle className=\"fill-info stroke-background\" r={16} strokeWidth={3} />\n                <BikeIcon className=\"text-white\" height={16} width={16} x={-8} y={-8} />\n              </g>\n            </g>\n          </svg>\n\n          <div className=\"pointer-events-none absolute inset-x-3 top-3 flex items-start justify-between gap-2\">\n            <span className=\"flex h-7 items-center gap-2 rounded-full border bg-popover/90 px-2.5 text-xs shadow-xs/5 backdrop-blur-sm\">\n              <span className=\"relative flex size-2\">\n                {!delivered && (\n                  <span className=\"absolute inset-0 animate-ping motion-reduce:hidden rounded-full bg-success opacity-60 [animation-duration:1.6s]\" />\n                )}\n                <span className={cn(\"relative size-2 rounded-full\", delivered ? \"bg-muted-foreground\" : \"bg-success\")} />\n              </span>\n              {delivered ? \"Delivered\" : \"Live\"}\n            </span>\n          </div>\n          <div className=\"absolute right-3 bottom-3 flex flex-col overflow-hidden rounded-xl border bg-popover shadow-xs/5\">\n            <button\n              aria-label=\"Zoom in on courier\"\n              className=\"flex size-9 cursor-pointer items-center justify-center text-foreground outline-none transition-colors hover:bg-accent focus-visible:bg-accent disabled:cursor-default disabled:opacity-40\"\n              disabled={zoomIdx === ZOOMS.length - 1}\n              onClick={() => setZoomIdx((z) => Math.min(ZOOMS.length - 1, z + 1))}\n              type=\"button\"\n            >\n              <PlusIcon className=\"size-4\" />\n            </button>\n            <span className=\"h-px bg-border\" />\n            <button\n              aria-label=\"Zoom out\"\n              className=\"flex size-9 cursor-pointer items-center justify-center text-foreground outline-none transition-colors hover:bg-accent focus-visible:bg-accent disabled:cursor-default disabled:opacity-40\"\n              disabled={zoomIdx === 0}\n              onClick={() => setZoomIdx((z) => Math.max(0, z - 1))}\n              type=\"button\"\n            >\n              <MinusIcon className=\"size-4\" />\n            </button>\n          </div>\n        </div>\n\n        <div className=\"relative min-h-0 flex-1 overflow-y-auto border-t @3xl:w-[380px] @3xl:flex-none @3xl:border-t-0 @3xl:border-l\">\n          <div className=\"flex flex-col divide-y\">\n            <section aria-live=\"polite\" className=\"flex flex-col gap-4 px-5 py-5\">\n              <AnimatePresence initial={false} mode=\"popLayout\">\n                {delivered ? (\n                  <motion.div\n                    animate={{ opacity: 1, y: 0 }}\n                    className=\"flex flex-col gap-1\"\n                    initial={reduce ? { opacity: 0 } : { opacity: 0, y: 6 }}\n                    key=\"done\"\n                    transition={{ duration: 0.25, ease: easeOut }}\n                  >\n                    <span className=\"text-muted-foreground text-xs\">Delivered at {arrival}</span>\n                    <span className=\"flex items-center gap-2 font-heading font-semibold text-3xl tracking-tight\">\n                      Enjoy your order\n                      <span className=\"flex size-6 items-center justify-center rounded-full bg-success text-white\">\n                        <CheckIcon className=\"size-4\" />\n                      </span>\n                    </span>\n                    <span className=\"text-muted-foreground\">{note ? \"Left with the concierge.\" : \"Handed over at the door.\"}</span>\n                  </motion.div>\n                ) : (\n                  <motion.div\n                    animate={{ opacity: 1, y: 0 }}\n                    className=\"flex flex-col gap-1\"\n                    exit={{ opacity: 0, y: -6 }}\n                    initial={{ opacity: 0 }}\n                    key=\"eta\"\n                    transition={{ duration: 0.2, ease: easeOut }}\n                  >\n                    <span className=\"text-muted-foreground text-xs\">Arriving by {arrival}</span>\n                    <span className=\"font-heading font-semibold text-3xl tracking-tight\">\n                      <Tween value={minutes} /> min\n                    </span>\n                    <AnimatePresence initial={false} mode=\"popLayout\">\n                      <motion.span\n                        animate={{ opacity: 1, filter: \"blur(0px)\" }}\n                        className=\"text-muted-foreground\"\n                        exit={{ opacity: 0, filter: \"blur(2px)\" }}\n                        initial={{ opacity: 0, filter: \"blur(2px)\" }}\n                        key={phase}\n                        transition={{ duration: 0.2 }}\n                      >\n                        {phase}\n                      </motion.span>\n                    </AnimatePresence>\n                  </motion.div>\n                )}\n              </AnimatePresence>\n              <ol aria-label=\"Order progress\" className=\"grid grid-cols-4 gap-1\">\n                {stages.map((s, i) => (\n                  <li className=\"flex flex-col gap-2\" key={s}>\n                    <span className=\"relative h-1 overflow-hidden rounded-full bg-muted\">\n                      <motion.span\n                        animate={{ scaleX: i < stage ? 1 : i === stage ? (delivered ? 1 : 0.6) : 0 }}\n                        className={cn(\"absolute inset-0 origin-left rounded-full\", delivered ? \"bg-success\" : \"bg-info\")}\n                        initial={false}\n                        transition={reduce ? { duration: 0 } : { duration: 0.4, ease: easeOut }}\n                      />\n                    </span>\n                    <span\n                      aria-current={i === stage ? \"step\" : undefined}\n                      className={cn(\n                        \"truncate text-xs\",\n                        i === stage ? \"font-medium text-foreground\" : i < stage ? \"text-foreground\" : \"text-muted-foreground\",\n                      )}\n                    >\n                      {s}\n                    </span>\n                  </li>\n                ))}\n              </ol>\n            </section>\n\n            <section aria-label=\"Your courier\" className=\"flex flex-col gap-4 px-5 py-4\">\n              <div className=\"flex items-center gap-3\">\n                <Avatar className=\"[--avatar-size:--spacing(10)]\">\n                  <AvatarFallback className=\"bg-gradient-to-br from-teal-400 to-sky-500 font-semibold text-white\">\n                    {initials(courier.name)}\n                  </AvatarFallback>\n                </Avatar>\n                <div className=\"flex min-w-0 flex-1 flex-col gap-0.5\">\n                  <span className=\"truncate font-medium\">{courier.name}</span>\n                  <span className=\"flex items-center gap-1 truncate text-muted-foreground text-xs\">\n                    <StarIcon aria-hidden className=\"size-3 fill-amber-400 text-amber-400\" />\n                    <span className=\"text-foreground tabular-nums\">{courier.rating.toFixed(1)}</span>\n                    <span aria-hidden>·</span>\n                    <span className=\"truncate\">{courier.vehicle}</span>\n                  </span>\n                </div>\n                <AnimatePresence initial={false}>\n                  {!delivered && (\n                    <motion.div\n                      animate={{ opacity: 1 }}\n                      className=\"flex shrink-0 gap-2\"\n                      exit={{ opacity: 0 }}\n                      initial={{ opacity: 0 }}\n                      transition={{ duration: 0.15 }}\n                    >\n                      <Button\n                        aria-label={`Message ${first}`}\n                        className={cn(\"rounded-full\", press)}\n                        onClick={() => setChat(true)}\n                        ref={chatBtnRef}\n                        size=\"icon\"\n                        variant=\"outline\"\n                      >\n                        <MessageCircleIcon />\n                      </Button>\n                      <Button\n                        aria-label={`Call ${first}`}\n                        className={cn(\"rounded-full\", press)}\n                        render={<a href={`tel:${courier.phone}`} />}\n                        size=\"icon\"\n                        variant=\"outline\"\n                      >\n                        <PhoneIcon />\n                      </Button>\n                    </motion.div>\n                  )}\n                </AnimatePresence>\n              </div>\n              <AnimatePresence initial={false}>\n                {delivered && (\n                  <motion.div\n                    animate={{ opacity: 1, y: 0 }}\n                    className=\"flex flex-col gap-3 rounded-xl bg-muted/60 p-3\"\n                    initial={reduce ? { opacity: 0 } : { opacity: 0, y: 6 }}\n                    key=\"rate\"\n                    transition={{ duration: 0.25, ease: easeOut, delay: 0.1 }}\n                  >\n                    <div className=\"flex items-center justify-between gap-2\">\n                      <span className=\"font-medium\">How was {first}?</span>\n                      <div aria-label=\"Rate your courier\" className=\"-me-1.5 flex\" role=\"radiogroup\">\n                        {[1, 2, 3, 4, 5].map((n) => (\n                          <button\n                            aria-checked={rating === n}\n                            aria-label={`${n} star${n > 1 ? \"s\" : \"\"}`}\n                            className={cn(\n                              \"flex size-8 cursor-pointer items-center justify-center rounded-md outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n                              press,\n                            )}\n                            key={n}\n                            onClick={() => setRating(n)}\n                            role=\"radio\"\n                            type=\"button\"\n                          >\n                            <motion.span\n                              animate={{ scale: rating >= n ? 1 : 0.9 }}\n                              transition={{ type: \"spring\", stiffness: 600, damping: 20, delay: rating >= n ? (n - 1) * 0.03 : 0 }}\n                            >\n                              <StarIcon\n                                className={cn(\n                                  \"size-5 transition-colors duration-150\",\n                                  rating >= n ? \"fill-amber-400 text-amber-400\" : \"text-muted-foreground/60\",\n                                )}\n                              />\n                            </motion.span>\n                          </button>\n                        ))}\n                      </div>\n                    </div>\n                    <div className=\"flex items-center gap-2\">\n                      <span className=\"text-muted-foreground text-xs\">Add a tip</span>\n                      <div className=\"ms-auto flex gap-1.5\">\n                        {[2, 3, 5].map((amt) => (\n                          <button\n                            aria-pressed={tip === amt}\n                            className={cn(\n                              \"h-8 min-w-12 cursor-pointer rounded-full border px-3 font-medium text-xs tabular-nums outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n                              press,\n                              tip === amt ? \"border-primary bg-primary text-primary-foreground\" : \"bg-background hover:bg-accent\",\n                            )}\n                            key={amt}\n                            onClick={() => setTip((t) => (t === amt ? null : amt))}\n                            type=\"button\"\n                          >\n                            ${amt}\n                          </button>\n                        ))}\n                      </div>\n                    </div>\n                    <AnimatePresence initial={false}>\n                      {(rating > 0 || tip) && (\n                        <motion.p\n                          animate={{ opacity: 1, height: \"auto\" }}\n                          className=\"overflow-hidden text-success-foreground text-xs\"\n                          exit={{ opacity: 0, height: 0 }}\n                          initial={{ opacity: 0, height: 0 }}\n                          transition={{ duration: 0.2, ease: easeOut }}\n                        >\n                          Thanks. {tip ? `All $${tip} goes to ${first}.` : `${first} will see your rating.`}\n                        </motion.p>\n                      )}\n                    </AnimatePresence>\n                  </motion.div>\n                )}\n              </AnimatePresence>\n            </section>\n\n            <section aria-labelledby={`${uid}-drop`} className=\"flex flex-col gap-1.5 px-5 py-4\">\n              <div className=\"flex h-6 items-center justify-between gap-2\">\n                <h3 className=\"text-muted-foreground text-xs\" id={`${uid}-drop`}>\n                  Drop-off\n                </h3>\n                {!editing && !delivered && (\n                  <Button\n                    className={cn(\"-me-2\", press)}\n                    onClick={() => {\n                      setDraft(note);\n                      setEditing(true);\n                    }}\n                    size=\"xs\"\n                    variant=\"ghost\"\n                  >\n                    <PencilIcon /> Edit note\n                  </Button>\n                )}\n              </div>\n              <span className=\"flex items-center gap-2 font-medium\">\n                <HouseIcon aria-hidden className=\"size-4 shrink-0 text-muted-foreground\" />\n                <span className=\"truncate\">{address}</span>\n              </span>\n              <AnimatePresence initial={false} mode=\"popLayout\">\n                {editing ? (\n                  <motion.form\n                    animate={{ opacity: 1 }}\n                    className=\"mt-1.5 flex flex-col gap-2\"\n                    exit={{ opacity: 0 }}\n                    initial={{ opacity: 0 }}\n                    key=\"edit\"\n                    onKeyDown={(e) => {\n                      if (e.key === \"Escape\") setEditing(false);\n                    }}\n                    onSubmit={(e: FormEvent) => {\n                      e.preventDefault();\n                      setNote(draft.trim());\n                      setEditing(false);\n                    }}\n                    transition={{ duration: 0.15 }}\n                  >\n                    <Textarea\n                      aria-label=\"Note for the courier\"\n                      autoFocus\n                      maxLength={140}\n                      onChange={(e) => setDraft(e.target.value)}\n                      placeholder=\"Gate code, where to leave it…\"\n                      rows={2}\n                      value={draft}\n                    />\n                    <div className=\"flex justify-end gap-2\">\n                      <Button onClick={() => setEditing(false)} size=\"sm\" variant=\"ghost\">\n                        Cancel\n                      </Button>\n                      <Button className={press} size=\"sm\" type=\"submit\">\n                        Save note\n                      </Button>\n                    </div>\n                  </motion.form>\n                ) : (\n                  <motion.p\n                    animate={{ opacity: 1 }}\n                    className=\"ps-6 text-muted-foreground text-pretty\"\n                    initial={{ opacity: 0 }}\n                    key=\"note\"\n                    transition={{ duration: 0.15 }}\n                  >\n                    {note ? `“${note}”` : \"No note for the courier.\"}\n                  </motion.p>\n                )}\n              </AnimatePresence>\n            </section>\n\n            <section aria-labelledby={`${uid}-items`} className=\"flex flex-col px-5 py-4\">\n              <div className=\"flex items-baseline justify-between gap-2\">\n                <h3 className=\"text-muted-foreground text-xs\" id={`${uid}-items`}>\n                  Your order\n                </h3>\n                <span className=\"text-muted-foreground text-xs tabular-nums\">\n                  {items.reduce((s, i) => s + i.qty, 0)} items · {storeName}\n                </span>\n              </div>\n              <ul className=\"mt-3 flex flex-col gap-3\">\n                {items.map((i) => (\n                  <li className=\"flex items-center gap-3\" key={i.id}>\n                    <ItemArt art={i.art} />\n                    <div className=\"flex min-w-0 flex-1 flex-col\">\n                      <span className=\"truncate font-medium\">\n                        {i.qty > 1 && <span className=\"me-1 text-muted-foreground tabular-nums\">{i.qty}×</span>}\n                        {i.name}\n                      </span>\n                      <span className=\"truncate text-muted-foreground text-xs\">{i.detail}</span>\n                    </div>\n                    <span className=\"tabular-nums\">${(i.price * i.qty).toFixed(2)}</span>\n                  </li>\n                ))}\n              </ul>\n              <dl className=\"mt-4 flex flex-col gap-1.5 border-t border-dashed pt-3 text-xs\">\n                <div className=\"flex justify-between\">\n                  <dt className=\"text-muted-foreground\">Items</dt>\n                  <dd className=\"tabular-nums\">${itemsTotal.toFixed(2)}</dd>\n                </div>\n                <div className=\"flex justify-between\">\n                  <dt className=\"text-muted-foreground\">Delivery</dt>\n                  <dd className=\"tabular-nums\">${fee.toFixed(2)}</dd>\n                </div>\n                <AnimatePresence initial={false}>\n                  {tip ? (\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={{ duration: 0.2, ease: easeOut }}\n                    >\n                      <div className=\"flex justify-between\">\n                        <dt className=\"text-muted-foreground\">Tip for {first}</dt>\n                        <dd className=\"tabular-nums\">${tip.toFixed(2)}</dd>\n                      </div>\n                    </motion.div>\n                  ) : null}\n                </AnimatePresence>\n                <div className=\"mt-1 flex justify-between font-medium text-sm\">\n                  <dt>Paid</dt>\n                  <dd className=\"tabular-nums\">${(itemsTotal + fee + (tip ?? 0)).toFixed(2)}</dd>\n                </div>\n              </dl>\n            </section>\n\n            {delivered && (\n              <div className=\"flex justify-center px-5 py-3\">\n                <Button className={press} onClick={() => setRun((r) => r + 1)} size=\"sm\" variant=\"ghost\">\n                  <RotateCcwIcon /> Watch the delivery again\n                </Button>\n              </div>\n            )}\n          </div>\n        </div>\n      </div>\n\n      <CourierChat\n        courier={courier}\n        onClose={() => {\n          setChat(false);\n          chatBtnRef.current?.focus();\n        }}\n        open={chat && !delivered}\n        storeName={storeName}\n      />\n    </div>\n  );\n}\n\ntype Msg = { id: number; from: \"me\" | \"them\"; text: string; time: string };\n\nfunction CourierChat({\n  open,\n  courier,\n  storeName,\n  onClose,\n}: {\n  open: boolean;\n  courier: DeliveryCourier;\n  storeName: string;\n  onClose: () => void;\n}) {\n  const reduce = useReducedMotion();\n  const first = courier.name.split(\" \")[0];\n  const [msgs, setMsgs] = useState<Msg[]>([\n    { id: 1, from: \"them\", text: `Hi! Picked up your order from ${storeName} and I’m on my way.`, time: \"7:26 pm\" },\n  ]);\n  const [text, setText] = useState(\"\");\n  const [typing, setTyping] = useState(false);\n  const timers = useRef<ReturnType<typeof setTimeout>[]>([]);\n  const listRef = useRef<HTMLDivElement>(null);\n  const inputRef = useRef<HTMLInputElement>(null);\n  const nextId = useRef(2);\n\n  useEffect(() => () => timers.current.forEach(clearTimeout), []);\n  useEffect(() => {\n    if (open) {\n      const f = setTimeout(() => inputRef.current?.focus(), 80);\n      return () => clearTimeout(f);\n    }\n  }, [open]);\n  useEffect(() => {\n    const el = listRef.current;\n    if (el) el.scrollTo({ top: el.scrollHeight, behavior: reduce ? \"auto\" : \"smooth\" });\n  }, [msgs, typing, reduce]);\n\n  const reply = (q: string) => {\n    const s = q.toLowerCase();\n    if (s.includes(\"door\")) return \"No problem, I’ll leave it by the door and send a photo.\";\n    if (s.includes(\"bell\") || s.includes(\"ring\")) return \"Will do, I’ll ring when I’m outside.\";\n    if (s.includes(\"down\") || s.includes(\"coming\")) return \"Great, see you downstairs in a few minutes.\";\n    return \"Got it, thanks!\";\n  };\n\n  const send = (raw: string) => {\n    const t = raw.trim();\n    if (!t) return;\n    setMsgs((m) => [...m, { id: nextId.current++, from: \"me\", text: t, time: \"Now\" }]);\n    setText(\"\");\n    timers.current.push(setTimeout(() => setTyping(true), 500));\n    timers.current.push(\n      setTimeout(() => {\n        setTyping(false);\n        setMsgs((m) => [...m, { id: nextId.current++, from: \"them\", text: reply(t), time: \"Now\" }]);\n      }, 1700),\n    );\n  };\n\n  const quick = [\"Leave it at the door\", \"Please ring the bell\", \"I’m coming down\"];\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            onClick={onClose}\n            transition={{ duration: 0.2, ease: easeOut }}\n          />\n          <motion.div\n            animate={{ y: 0 }}\n            aria-label={`Chat with ${first}`}\n            aria-modal\n            className=\"absolute inset-x-0 top-16 bottom-0 z-50 flex flex-col rounded-t-2xl border-t bg-popover text-popover-foreground shadow-lg/10 @3xl:top-14 @3xl:left-auto @3xl:w-[380px] @3xl:rounded-none @3xl:border-t-0 @3xl:border-l\"\n            exit={reduce ? { opacity: 0 } : { y: \"100%\" }}\n            initial={reduce ? { opacity: 0 } : { y: \"100%\" }}\n            onKeyDown={(e) => {\n              if (e.key === \"Escape\") onClose();\n            }}\n            role=\"dialog\"\n            transition={reduce ? { duration: 0.15 } : { type: \"spring\", stiffness: 440, damping: 42 }}\n          >\n            <div className=\"flex items-center gap-3 border-b px-4 py-3\">\n              <Avatar className=\"[--avatar-size:--spacing(8)]\">\n                <AvatarFallback className=\"bg-gradient-to-br from-teal-400 to-sky-500 font-semibold text-white text-xs\">\n                  {initials(courier.name)}\n                </AvatarFallback>\n              </Avatar>\n              <div className=\"flex min-w-0 flex-1 flex-col\">\n                <span className=\"truncate font-medium\">{first}</span>\n                <span className=\"text-muted-foreground text-xs\">Your courier · replies fast</span>\n              </div>\n              <Button onClick={onClose} size=\"sm\" variant=\"ghost\">\n                Done\n              </Button>\n            </div>\n            <div className=\"flex min-h-0 flex-1 flex-col gap-2 overflow-y-auto px-4 py-4\" ref={listRef}>\n              <p className=\"mb-1 text-center text-muted-foreground text-xs\">Messages go through the app. Your number stays private.</p>\n              <AnimatePresence initial={false}>\n                {msgs.map((m) => (\n                  <motion.div\n                    animate={{ opacity: 1, y: 0, scale: 1 }}\n                    className={cn(\"flex max-w-[80%] flex-col gap-0.5\", m.from === \"me\" ? \"items-end self-end\" : \"items-start self-start\")}\n                    initial={reduce ? { opacity: 0 } : { opacity: 0, y: 6, scale: 0.98 }}\n                    key={m.id}\n                    style={{ transformOrigin: m.from === \"me\" ? \"100% 100%\" : \"0% 100%\" }}\n                    transition={spring}\n                  >\n                    <span\n                      className={cn(\n                        \"rounded-2xl px-3 py-2 text-pretty\",\n                        m.from === \"me\" ? \"rounded-br-md bg-primary text-primary-foreground\" : \"rounded-bl-md bg-muted\",\n                      )}\n                    >\n                      {m.text}\n                    </span>\n                    <span className=\"px-1 text-[11px] text-muted-foreground\">{m.time}</span>\n                  </motion.div>\n                ))}\n                {typing && (\n                  <motion.div\n                    animate={{ opacity: 1 }}\n                    aria-label={`${first} is typing`}\n                    className=\"flex h-9 w-14 items-center justify-center gap-1 self-start rounded-2xl rounded-bl-md bg-muted\"\n                    exit={{ opacity: 0 }}\n                    initial={{ opacity: 0 }}\n                    key=\"typing\"\n                    role=\"status\"\n                  >\n                    {[0, 1, 2].map((i) => (\n                      <motion.span\n                        animate={reduce ? undefined : { opacity: [0.3, 1, 0.3] }}\n                        className=\"size-1.5 rounded-full bg-muted-foreground\"\n                        key={i}\n                        transition={{ duration: 0.9, repeat: Number.POSITIVE_INFINITY, delay: i * 0.15, ease: \"linear\" }}\n                      />\n                    ))}\n                  </motion.div>\n                )}\n              </AnimatePresence>\n            </div>\n            <div className=\"flex flex-col gap-2 border-t p-3\">\n              <div className=\"-mx-3 flex gap-1.5 overflow-x-auto px-3 [scrollbar-width:none]\">\n                {quick.map((q) => (\n                  <button\n                    className={cn(\n                      \"h-8 shrink-0 cursor-pointer rounded-full border bg-background px-3 text-xs outline-none hover:bg-accent focus-visible:ring-2 focus-visible:ring-ring\",\n                      press,\n                    )}\n                    key={q}\n                    onClick={() => send(q)}\n                    type=\"button\"\n                  >\n                    {q}\n                  </button>\n                ))}\n              </div>\n              <form\n                className=\"flex gap-2\"\n                onSubmit={(e) => {\n                  e.preventDefault();\n                  send(text);\n                }}\n              >\n                <Input\n                  aria-label={`Message ${first}`}\n                  className=\"flex-1\"\n                  onChange={(e) => setText(e.target.value)}\n                  placeholder={`Message ${first}`}\n                  ref={inputRef}\n                  value={text}\n                />\n                <Button aria-label=\"Send\" className={press} disabled={!text.trim()} size=\"icon\" type=\"submit\">\n                  <SendIcon />\n                </Button>\n              </form>\n            </div>\n          </motion.div>\n        </>\n      )}\n    </AnimatePresence>\n  );\n}\n"
    }
  ]
}