{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "billing-overview",
  "type": "registry:block",
  "title": "Billing Overview",
  "description": "A billing page in one glance: plan and seats, the next invoice with its cycle progress, a payment card that holds its colours in both themes, usage bars that warn near the limit, and invoices with a one-click retry.",
  "categories": [
    "block",
    "Dashboards"
  ],
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils",
    "https://coss.com/ui/r/button.json",
    "https://coss.com/ui/r/spinner.json"
  ],
  "files": [
    {
      "path": "registry/devl/billing-overview.tsx",
      "type": "registry:component",
      "content": "\"use client\";\n\nimport { ArrowUpRightIcon, CheckIcon, DownloadIcon, PlusIcon, RotateCwIcon, TriangleAlertIcon } from \"lucide-react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { type ReactNode, useState } from \"react\";\nimport { Button } from \"@/components/ui/button\";\nimport { Spinner } from \"@/components/ui/spinner\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface BillingPlan {\n  name: string;\n  price: number;\n  interval: \"month\" | \"year\";\n  billed: string;\n  renewsOn: string;\n  seatsUsed: number;\n  seats: number;\n  includes?: string[];\n}\n\nexport interface BillingLine {\n  label: string;\n  amount: number;\n}\n\nexport interface BillingInvoicePreview {\n  dueOn: string;\n  lines: BillingLine[];\n  cycleDay: number;\n  cycleLength: number;\n}\n\nexport interface BillingCard {\n  holder: string;\n  last4: string;\n  expires: string;\n  kind?: string;\n}\n\nexport interface BillingUsage {\n  label: string;\n  used: number;\n  limit: number;\n  unit?: string;\n  format?: (n: number) => string;\n}\n\nexport type InvoiceStatus = \"paid\" | \"failed\" | \"refunded\" | \"open\";\n\nexport interface BillingInvoice {\n  id: string;\n  date: string;\n  amount: number;\n  status: InvoiceStatus;\n}\n\nexport interface BillingOverviewProps {\n  title?: string;\n  subtitle?: string;\n  plan: BillingPlan;\n  next: BillingInvoicePreview;\n  card: BillingCard;\n  billingEmail?: string;\n  usage: BillingUsage[];\n  invoices: BillingInvoice[];\n  currency?: string;\n  onChangePlan?: () => void;\n  onUpdateCard?: () => void;\n  onAddSeats?: () => void;\n  onExport?: () => void;\n  onDownload?: (invoice: BillingInvoice) => void;\n  onRetry?: (invoice: BillingInvoice) => Promise<boolean>;\n  className?: string;\n}\n\nconst ease = [0.23, 1, 0.32, 1] as const;\nconst press = \"transition-[box-shadow,scale,background-color] duration-150 active:scale-[0.97]\";\n\nconst STATUS: Record<InvoiceStatus, { label: string; className: string }> = {\n  paid: { label: \"Paid\", className: \"bg-success/10 text-success-foreground\" },\n  failed: { label: \"Failed\", className: \"bg-destructive/10 text-destructive-foreground\" },\n  refunded: { label: \"Refunded\", className: \"bg-muted text-muted-foreground\" },\n  open: { label: \"Open\", className: \"bg-info/10 text-info-foreground\" },\n};\n\nfunction money(n: number, currency: string) {\n  return `${currency}${n.toLocaleString(\"en-US\", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`;\n}\n\nfunction CardLabel({ children, action }: { children: string; action?: ReactNode }) {\n  return (\n    <div className=\"flex min-h-7 items-center justify-between gap-2\">\n      <h3 className=\"font-medium text-muted-foreground text-sm\">{children}</h3>\n      {action}\n    </div>\n  );\n}\n\nfunction PaymentCard({ card }: { card: BillingCard }) {\n  return (\n    <div className=\"relative isolate aspect-[1.586] w-full max-w-[300px] overflow-hidden rounded-xl bg-linear-135 from-indigo-500 via-violet-600 to-fuchsia-600 p-4 text-white shadow-sm/10 ring-1 ring-black/5 ring-inset\">\n      <svg aria-hidden className=\"absolute inset-0 -z-10 size-full opacity-30\" preserveAspectRatio=\"none\" viewBox=\"0 0 300 190\">\n        <path d=\"M-20 150 C 60 90, 140 200, 320 80\" fill=\"none\" stroke=\"white\" strokeOpacity=\".35\" strokeWidth=\"1\" />\n        <path d=\"M-20 170 C 80 110, 160 220, 320 100\" fill=\"none\" stroke=\"white\" strokeOpacity=\".25\" strokeWidth=\"1\" />\n        <circle cx=\"260\" cy=\"20\" fill=\"white\" fillOpacity=\".12\" r=\"70\" />\n      </svg>\n      <div className=\"flex h-full flex-col justify-between\">\n        <div className=\"flex items-start justify-between\">\n          <svg aria-hidden className=\"h-7 w-9\" viewBox=\"0 0 36 28\">\n            <rect fill=\"white\" fillOpacity=\".85\" height=\"26\" rx=\"5\" width=\"34\" x=\"1\" y=\"1\" />\n            <path d=\"M1 10h11m12 0h11M1 18h11m12 0h11M12 1v26m12-26v26\" stroke=\"rgb(0 0 0 / .18)\" strokeWidth=\"1\" />\n          </svg>\n          <span className=\"font-medium text-[0.6875rem] text-white/80 uppercase tracking-[0.14em]\">{card.kind ?? \"Business debit\"}</span>\n        </div>\n        <div>\n          <p className=\"font-mono text-base tracking-[0.18em] sm:text-lg\">\n            <span className=\"text-white/70\">•••• •••• ••••</span> {card.last4}\n          </p>\n          <div className=\"mt-2 flex items-end justify-between text-[0.6875rem] uppercase tracking-wider\">\n            <span className=\"truncate\">{card.holder}</span>\n            <span className=\"tabular-nums\">{card.expires}</span>\n          </div>\n        </div>\n      </div>\n    </div>\n  );\n}\n\nfunction UsageRow({ item, index }: { item: BillingUsage; index: number }) {\n  const reduce = useReducedMotion();\n  const pct = Math.min(1, item.used / item.limit);\n  const tone = pct >= 0.9 ? \"warning\" : \"default\";\n  const fmt = item.format ?? ((n: number) => n.toLocaleString(\"en-US\"));\n  return (\n    <li>\n      <div className=\"flex items-baseline justify-between gap-3 text-sm\">\n        <span className=\"flex items-center gap-2 font-medium\">\n          {item.label}\n          {tone === \"warning\" && (\n            <span className=\"inline-flex items-center gap-1 rounded-full bg-warning/12 px-1.5 py-px font-medium text-[0.6875rem] text-warning-foreground\">\n              <TriangleAlertIcon className=\"size-3\" />\n              Near limit\n            </span>\n          )}\n        </span>\n        <span className=\"shrink-0 text-muted-foreground text-xs tabular-nums\">\n          <span className=\"text-foreground\">{fmt(item.used)}</span> / {fmt(item.limit)}\n          {item.unit ? ` ${item.unit}` : \"\"}\n        </span>\n      </div>\n      <div\n        aria-label={`${item.label}: ${Math.round(pct * 100)}% used`}\n        aria-valuemax={100}\n        aria-valuemin={0}\n        aria-valuenow={Math.round(pct * 100)}\n        className=\"mt-2 h-2 overflow-hidden rounded-full bg-muted\"\n        role=\"meter\"\n      >\n        <motion.div\n          animate={{ scaleX: pct }}\n          className={cn(\"h-full origin-left rounded-full\", tone === \"warning\" ? \"bg-warning\" : \"bg-primary/80\")}\n          initial={{ scaleX: 0 }}\n          transition={reduce ? { duration: 0 } : { duration: 0.7, ease, delay: 0.15 + index * 0.05 }}\n        />\n      </div>\n    </li>\n  );\n}\n\nfunction InvoiceRow({\n  invoice,\n  currency,\n  onDownload,\n  onRetry,\n  onPaid,\n}: {\n  invoice: BillingInvoice;\n  currency: string;\n  onDownload?: (invoice: BillingInvoice) => void;\n  onRetry?: (invoice: BillingInvoice) => Promise<boolean>;\n  onPaid?: (invoice: BillingInvoice) => void;\n}) {\n  const [status, setStatus] = useState(invoice.status);\n  const [retrying, setRetrying] = useState(false);\n  const s = STATUS[status];\n  const retry = async () => {\n    if (!onRetry) return;\n    setRetrying(true);\n    const ok = await onRetry(invoice);\n    setRetrying(false);\n    if (ok) {\n      setStatus(\"paid\");\n      onPaid?.(invoice);\n    }\n  };\n  return (\n    <li className=\"grid grid-cols-[1fr_auto_76px] items-center gap-3 px-5 py-3 text-sm transition-colors duration-150 hover:bg-accent/40 sm:grid-cols-[1.1fr_1fr_88px_92px_28px]\">\n      <div className=\"min-w-0\">\n        <p className=\"font-medium\">{invoice.date}</p>\n        <p className=\"truncate font-mono text-muted-foreground text-xs sm:hidden\">{invoice.id}</p>\n      </div>\n      <p className=\"hidden truncate font-mono text-muted-foreground text-xs sm:block\">{invoice.id}</p>\n      <p className=\"text-end tabular-nums\">{money(invoice.amount, currency)}</p>\n      <div className=\"flex justify-end\">\n        <AnimatePresence initial={false} mode=\"popLayout\">\n          {status === \"failed\" && onRetry ? (\n            <motion.span\n              animate={{ opacity: 1, scale: 1 }}\n              exit={{ opacity: 0, scale: 0.9 }}\n              initial={{ opacity: 0, scale: 0.9 }}\n              key=\"retry\"\n              transition={{ duration: 0.16, ease }}\n            >\n              <Button className={cn(\"text-destructive-foreground\", press)} disabled={retrying} onClick={retry} size=\"xs\" variant=\"outline\">\n                {retrying ? <Spinner /> : <RotateCwIcon />}\n                Retry\n              </Button>\n            </motion.span>\n          ) : (\n            <motion.span\n              animate={{ opacity: 1, scale: 1 }}\n              className={cn(\"inline-flex items-center gap-1 rounded-full px-2 py-0.5 font-medium text-xs\", s.className)}\n              exit={{ opacity: 0, scale: 0.9 }}\n              initial={{ opacity: 0, scale: 0.9 }}\n              key={status}\n              transition={{ duration: 0.16, ease }}\n            >\n              {status === \"paid\" && invoice.status === \"failed\" && <CheckIcon className=\"size-3\" strokeWidth={3} />}\n              {s.label}\n            </motion.span>\n          )}\n        </AnimatePresence>\n      </div>\n      <Button\n        aria-label={`Download invoice ${invoice.id}`}\n        className={cn(\"hidden text-muted-foreground sm:inline-flex\", press)}\n        onClick={() => onDownload?.(invoice)}\n        size=\"icon-sm\"\n        variant=\"ghost\"\n      >\n        <DownloadIcon />\n      </Button>\n    </li>\n  );\n}\n\nexport function BillingOverview({\n  title = \"Billing\",\n  subtitle = \"Your plan, what you’ve used this cycle, and every invoice.\",\n  plan,\n  next,\n  card,\n  billingEmail,\n  usage,\n  invoices,\n  currency = \"$\",\n  onChangePlan,\n  onUpdateCard,\n  onAddSeats,\n  onExport,\n  onDownload,\n  onRetry,\n  className,\n}: BillingOverviewProps) {\n  const reduce = useReducedMotion();\n  const total = next.lines.reduce((sum, l) => sum + l.amount, 0);\n  const cycle = next.cycleDay / next.cycleLength;\n  const [recovered, setRecovered] = useState<string[]>([]);\n  const failed = invoices.find((i) => i.status === \"failed\" && !recovered.includes(i.id));\n\n  return (\n    <section className={cn(\"mx-auto w-full max-w-[1100px] space-y-4\", className)}>\n      <header className=\"flex flex-wrap items-end justify-between gap-3\">\n        <div>\n          <h2 className=\"text-balance font-semibold text-xl tracking-tight\">{title}</h2>\n          <p className=\"mt-0.5 text-pretty text-muted-foreground text-sm\">{subtitle}</p>\n        </div>\n        <Button className={press} onClick={onChangePlan} variant=\"outline\">\n          Compare plans\n          <ArrowUpRightIcon />\n        </Button>\n      </header>\n\n      <AnimatePresence initial={false}>\n        {failed && (\n          <motion.div\n            animate={{ height: \"auto\", opacity: 1 }}\n            className=\"-mb-4 overflow-hidden\"\n            exit={{ height: 0, opacity: 0 }}\n            initial={{ height: 0, opacity: 0 }}\n            key={failed.id}\n            transition={reduce ? { duration: 0 } : { duration: 0.24, ease }}\n          >\n            <div className=\"pb-4\">\n        <div role=\"alert\" className=\"flex gap-3 rounded-2xl border border-destructive/20 bg-destructive/6 p-4 text-sm dark:bg-destructive/10\">\n          <TriangleAlertIcon className=\"mt-0.5 size-4 shrink-0 text-destructive-foreground sm:mt-2\" />\n          <div className=\"flex min-w-0 flex-1 flex-col gap-3 sm:flex-row sm:items-center\">\n            <p className=\"flex-1 text-pretty\">\n              <span className=\"font-medium\">Your {failed.date} payment didn’t go through.</span>{\" \"}\n              <span className=\"text-muted-foreground\">We’ll try again in 3 days, or retry it from the list below.</span>\n            </p>\n            <Button className={cn(\"self-start sm:self-auto\", press)} onClick={onUpdateCard} size=\"sm\" variant=\"outline\">\n              Update card\n            </Button>\n          </div>\n        </div>\n            </div>\n          </motion.div>\n        )}\n      </AnimatePresence>\n\n      <div className=\"grid gap-4 md:grid-cols-2 lg:grid-cols-3\">\n        <article className=\"flex flex-col rounded-2xl border bg-card p-5 shadow-xs/5\">\n          <CardLabel>Current plan</CardLabel>\n          <div className=\"mt-2 flex items-center gap-2\">\n            <p className=\"font-semibold text-2xl tracking-tight\">{plan.name}</p>\n            <span className=\"rounded-full border px-2 py-0.5 font-medium text-muted-foreground text-xs\">{plan.billed}</span>\n          </div>\n          <p className=\"mt-1 text-muted-foreground text-sm\">\n            <span className=\"font-medium text-foreground tabular-nums\">{money(plan.price, currency)}</span> per {plan.interval} · renews{\" \"}\n            {plan.renewsOn}\n          </p>\n          <div className=\"mt-5\">\n            <div className=\"flex items-center justify-between text-xs\">\n              <span className=\"text-muted-foreground\">Seats</span>\n              <span className=\"tabular-nums\">\n                {plan.seatsUsed} of {plan.seats} used\n              </span>\n            </div>\n            <div aria-hidden className=\"mt-2 flex gap-1\">\n              {Array.from({ length: plan.seats }, (_, i) => (\n                <motion.span\n                  animate={{ opacity: 1, scaleY: 1 }}\n                  className={cn(\"h-5 flex-1 origin-bottom rounded-[calc(var(--radius)*0.4)]\", i < plan.seatsUsed ? \"bg-primary/80\" : \"bg-muted\")}\n                  initial={{ opacity: 0, scaleY: 0.4 }}\n                  key={i}\n                  transition={reduce ? { duration: 0 } : { duration: 0.3, ease, delay: 0.1 + i * 0.025 }}\n                />\n              ))}\n            </div>\n          </div>\n          {plan.includes && plan.includes.length > 0 && (\n            <ul className=\"mt-4 space-y-1.5 text-sm\">\n              {plan.includes.map((f) => (\n                <li className=\"flex items-center gap-2\" key={f}>\n                  <CheckIcon className=\"size-3.5 shrink-0 text-muted-foreground\" strokeWidth={2.5} />\n                  <span className=\"truncate\">{f}</span>\n                </li>\n              ))}\n            </ul>\n          )}\n          <div className=\"mt-auto flex gap-2 pt-5\">\n            <Button className={cn(\"flex-1\", press)} onClick={onChangePlan}>\n              Change plan\n            </Button>\n            <Button className={cn(\"flex-1\", press)} onClick={onAddSeats} variant=\"outline\">\n              <PlusIcon />\n              Add seats\n            </Button>\n          </div>\n        </article>\n\n        <article className=\"flex flex-col rounded-2xl border bg-card p-5 shadow-xs/5\">\n          <CardLabel>Next invoice</CardLabel>\n          <p className=\"mt-2 font-semibold text-2xl tabular-nums tracking-tight\">{money(total, currency)}</p>\n          <p className=\"mt-1 text-muted-foreground text-sm\">Estimated · charged {next.dueOn}</p>\n          <dl className=\"mt-4 space-y-1.5 text-sm\">\n            {next.lines.map((l) => (\n              <div className=\"flex justify-between gap-3\" key={l.label}>\n                <dt className=\"truncate text-muted-foreground\">{l.label}</dt>\n                <dd className=\"tabular-nums\">{money(l.amount, currency)}</dd>\n              </div>\n            ))}\n          </dl>\n          <div className=\"mt-auto pt-5\">\n            <div className=\"flex justify-between text-muted-foreground text-xs\">\n              <span>Billing cycle</span>\n              <span className=\"tabular-nums\">\n                Day {next.cycleDay} of {next.cycleLength}\n              </span>\n            </div>\n            <div className=\"relative mt-2 h-2 rounded-full bg-muted\">\n              <motion.div\n                animate={{ scaleX: cycle }}\n                className=\"absolute inset-y-0 left-0 w-full origin-left rounded-full bg-info\"\n                initial={{ scaleX: 0 }}\n                transition={reduce ? { duration: 0 } : { duration: 0.8, ease, delay: 0.2 }}\n              />\n            </div>\n          </div>\n        </article>\n\n        <article className=\"flex flex-col rounded-2xl border bg-card p-5 shadow-xs/5 md:col-span-2 lg:col-span-1\">\n          <CardLabel\n            action={\n              <Button className={cn(\"-me-2 text-muted-foreground\", press)} onClick={onUpdateCard} size=\"sm\" variant=\"ghost\">\n                Update\n              </Button>\n            }\n          >\n            Payment method\n          </CardLabel>\n          <div className=\"mt-3 flex flex-1 flex-col gap-4 sm:flex-row sm:items-center lg:flex-col lg:items-stretch\">\n            <PaymentCard card={card} />\n            <div className=\"space-y-1.5 text-sm\">\n              <p className=\"flex items-center justify-between gap-3\">\n                <span className=\"text-muted-foreground\">Default</span>\n                <span className=\"font-mono text-xs\">•••• {card.last4}</span>\n              </p>\n              {billingEmail && (\n                <p className=\"flex items-center justify-between gap-3\">\n                  <span className=\"text-muted-foreground\">Billing email</span>\n                  <span className=\"truncate\">{billingEmail}</span>\n                </p>\n              )}\n            </div>\n          </div>\n        </article>\n      </div>\n\n      <div className=\"grid gap-4 lg:grid-cols-[1fr_1.35fr]\">\n        <article className=\"rounded-2xl border bg-card p-5 shadow-xs/5\">\n          <CardLabel>Usage this cycle</CardLabel>\n          <ul className=\"mt-4 space-y-5\">\n            {usage.map((u, i) => (\n              <UsageRow index={i} item={u} key={u.label} />\n            ))}\n          </ul>\n          <p className=\"mt-5 text-pretty text-muted-foreground text-xs\">\n            Overage is billed at the end of the cycle. Usage resets {plan.renewsOn.split(\",\")[0]}.\n          </p>\n        </article>\n\n        <article className=\"overflow-hidden rounded-2xl border bg-card shadow-xs/5\">\n          <div className=\"flex items-center justify-between px-5 pt-5 pb-3\">\n            <h3 className=\"font-medium text-muted-foreground text-sm\">Invoices</h3>\n            <Button className={cn(\"-me-2 text-muted-foreground\", press)} onClick={onExport} size=\"sm\" variant=\"ghost\">\n              <DownloadIcon />\n              Export CSV\n            </Button>\n          </div>\n          <div className=\"hidden grid-cols-[1.1fr_1fr_88px_92px_28px] gap-3 border-y bg-muted/40 px-5 py-2 text-muted-foreground text-xs sm:grid\">\n            <span>Date</span>\n            <span>Invoice</span>\n            <span className=\"text-end\">Amount</span>\n            <span className=\"text-end\">Status</span>\n            <span />\n          </div>\n          <ul className=\"divide-y border-t sm:border-t-0\">\n            {invoices.map((inv) => (\n              <InvoiceRow\n                currency={currency}\n                invoice={inv}\n                key={inv.id}\n                onDownload={onDownload}\n                onPaid={(i) => setRecovered((ids) => [...ids, i.id])}\n                onRetry={onRetry}\n              />\n            ))}\n          </ul>\n        </article>\n      </div>\n    </section>\n  );\n}\n"
    }
  ]
}