> Premium standalone component (part of no design system, works in any React + Tailwind app) from AI Canvas. Index of every component and install setup: https://aicanvas.me/llms.txt

# Agent Cost Flow

What one orchestrated agent run cost, drawn end to end: 178 source strands bundle into a spine, five subagent lanes read in parallel, and every band narrows from what that lane read to what it cost, so the small model that did the bulk reading lands as a sliver beside the expensive one that only judged.

- Category: Widgets
- Built with: Motion, Canvas, Tailwind CSS
- License: Premium (proprietary, not in the public repo)
- Page: https://aicanvas.me/components/agent-cost-flow

## Install

```bash
npx shadcn@latest add @aicanvas/agent-cost-flow
```

Requires Premium. Set your AI Canvas token as AICANVAS_TOKEN and register the @aicanvas registry with it, as described in the Install section of https://aicanvas.me/llms.txt. Without a Premium token the command writes a placeholder file instead of the component.

AI agents can also get its install command from the read-only AI Canvas MCP server (`npx -y @aicanvas/mcp`) and run it. Setup: https://aicanvas.me/mcp

Peer dependencies:

```bash
npm install framer-motion
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `task?` | `string` | `'Evaluate migrating the app to a new framework'` | The one task the orchestrator decomposed. |
| `eyebrow?` | `string` | `'ORCHESTRATOR TRACE'` | The small letter-spaced caps line above the task. |
| `agents?` | `AgentCostFlowAgent[]` | `AGENT_COST_FLOW_AGENTS` | One row per subagent; every total in the graphic is derived from this array. |
| `initialAgent?` | `number \| null` | `null` | Index of the lane lit when the picture opens, or null for the rest state. |
| `onAgentChange?` | `(id: string \| null, index: number \| null) => void` |  | Called with the agent id and index as the lit lane changes, and with nulls when none is lit. |
| `elapsedParallel?` | `string` |  | Overrides the run's wall clock, which is otherwise the longest subagent, since they run at the same time. Printed in the stacked layout only. |
| `elapsedSerial?` | `string` |  | Overrides the serial clock, which is otherwise every subagent's time added up: the same reading done one agent at a time. Printed in the stacked layout only. |
| `showOutcome?` | `boolean` | `true` | Whether the outcome column, one block and label per subagent, is drawn at the right edge. |
| `layout?` | `'auto' \| 'wide' \| 'narrow'` | `'auto'` | Force the wide stage or the stacked layout instead of measuring the host. |
| `paletteDark?` | `Partial<AgentCostFlowPalette>` |  | Colour overrides for the dark theme, merged over AGENT_COST_FLOW_DARK. |
| `paletteLight?` | `Partial<AgentCostFlowPalette>` |  | Colour overrides for the light theme, merged over AGENT_COST_FLOW_LIGHT. |
| `tuning?` | `Partial<AgentCostFlowTuning>` |  | Geometry and timing overrides, merged over AGENT_COST_FLOW_TUNING. The hover reads hoverInMs, hoverOutMs, hoverMs, hoverHoldMs and detailCountMs; the surge a lit lane sends down its strands reads surgeMs, surgeDash, surgeWidth and surgeFade; the lane bands are measured with laneNameRise, outLabelSize, outLabelReach and spineHitPad. |
| `className?` | `string` |  | Extra classes merged onto the root element. |

## About

Agent Cost Flow draws one orchestrator run as a single picture: a hair-thin strand for every source document bundles into five subagent lanes, each lane shows its model, run time and tokens read, and its band sweeps into a stacked bill that totals the cost of the run on the right. Every figure climbs on one 30-second run clock, tokens quickly and money slowly, so the lane bills add up to the total at every moment. Pointing at a lane or its band isolates it and says what it read, returned and cost; below 1000px the picture becomes a stacked list where a click or tap opens a lane's tool calls over a fall of canvas dust. Motion springs the bars, CSS carries the travelling light, a canvas draws the dust, and light and dark themes ship in the same file. Thirteen props take your own agents, task, clocks, layout and palettes.

Use cases: AI Agent Cost Dashboard, Multi-agent Run Report, LLM Token Usage Breakdown.

## Source

The source is Premium and is not published. Preview it at https://aicanvas.me/components/agent-cost-flow.

## Remix prompt

The public part of the remix prompt: 1. Setup and 2. Constants. The rest of the build spec (state, markup, how it works) is part of Premium.

````markdown
# Agent Cost Flow
What one orchestrated agent run cost, drawn left to right from source strands to one bill.

## 1. Setup
// npm install framer-motion
File: components-workspace/agent-cost-flow/index.tsx  ·  'use client'  ·  export default function AgentCostFlow(props: AgentCostFlowProps)
Needs Tailwind CSS v4 with a class-based dark variant, and Manrope loaded by the host app (the root names it with a system-sans fallback). No icon library, no WebGL, no images: SVG marks, absolutely positioned DOM text, one CSS stylesheet the component writes itself, one 2D canvas for the dust at the end of the fans, and the browser's Web Animations API (`element.animate`) for the one-shot surge a lit lane sends down its strands.

```tsx
import {
  memo,
  useCallback,
  useEffect,
  useId,
  useLayoutEffect,
  useMemo,
  useRef,
  useState,
  useSyncExternalStore,
} from 'react'
import type {
  CSSProperties,
  KeyboardEvent as ReactKeyboardEvent,
  PointerEvent as ReactPointerEvent,
  RefObject,
} from 'react'
import { AnimatePresence, MotionConfig, motion, useReducedMotion } from 'framer-motion'
```

## 2. Constants
Everything above the component function, verbatim, except the six helper components, which are split across blocks 3 and 4. The composition IS these numbers, so nothing here is summarised.

```tsx
/**
 * Agent Cost Flow. What one orchestrated run cost, read left to right: a
 * field of hair-thin strands, one per source document, bundles into a spine;
 * five subagent lanes grow out of that spine; each lane sends one band into
 * the bill on the right. A band leaves its bar at the height of what that
 * lane READ and narrows the whole way to a slot sized by what it COST, and
 * the five slots are packed into one short stack, so the picture makes the
 * argument for mixed models by itself: the lane that read the most is not
 * the lane that ends up widest, and the small model that did the bulk
 * reading lands as a sliver next to the expensive one that only judged.
 * Every number is derived from the agents array, so a caller who passes
 * their own trace gets their own bill, their own clock and their own split.
 * The component holds no price table: model prices move, so each lane
 * carries the cost the caller worked out. The only thing that moves is the
 * reading itself: glints run the strands on the left and the slot each lane
 * feeds brightens as its work lands, every lane on a cadence taken from its
 * own tool-call count. The bars and the bands are still on purpose, because
 * a light sweeping a solid shape reads as a thing loading rather than as
 * work being done. Pointing anywhere in a lane's band isolates it and says
 * what it returned, what it cost, what it took and what share of the bill it
 * is; on the wide stage that is the whole of it,
 * and only the stacked layout opens a row, on a click or a tap, to say the
 * same thing. Every mark comes from one bezier
 * formula and one seeded PRNG, so the picture is identical on the server and
 * the client and stays correct at any agent count.
 */

/* ------------------------------------------------------------------ types */

export type AgentCostFlowTool = { name: string; tokens: number }

export type AgentCostFlowAgent = {
  id: string
  name: string
  slice: string
  model: string
  sources: number
  toolCalls: number
  tokensRead: number
  tokensReturned: number
  /** How long this subagent ran, in seconds. The run's wall time is the
      longest of them, because they run at the same time. */
  seconds: number
  /** What this subagent cost to run, in dollars. The component holds no
      price table on purpose: model prices move, and a graphic carrying a
      stale rate is worse than one carrying none, so the caller prices its
      own run and passes the result. */
  costUsd: number
  tools?: AgentCostFlowTool[]
}

export type AgentCostFlowPalette = {
  /** the ground the whole graphic sits on */
  panel: string
  /** primary type */
  text: string
  /** secondary type */
  muted: string
  /** metadata type, the smallest rank */
  faint: string
  /** the spine hairline */
  border: string
  /** the categorical series colours, cycled if there are more than five agents */
  lanes: string[]
}

export type AgentCostFlowTuning = typeof AGENT_COST_FLOW_TUNING

type Pt = { x: number; y: number }

/* ------------------------------------------------------------ sample data */

/**
 * A real orchestration trace: one task, five subagents running at the same
 * time, each with its own context window. Sources, tool calls, tokens and
 * model tier are the only quantities here, because they are the only ones a
 * trace actually has.
 */
export const AGENT_COST_FLOW_AGENTS: AgentCostFlowAgent[] = [
  {
    id: 'docs',
    name: 'Docs',
    slice: 'Official docs and migration guides',
    model: 'Haiku',
    sources: 38,
    toolCalls: 14,
    tokensRead: 62000,
    tokensReturned: 1200,
    seconds: 62,
    costUsd: 0.05,
  },
  {
    id: 'source',
    name: 'Source',
    slice: 'Our repo and its dependency tree',
    model: 'Sonnet',
    sources: 51,
    toolCalls: 22,
    tokensRead: 104000,
    tokensReturned: 1800,
    seconds: 108,
    costUsd: 0.31,
  },
  {
    id: 'benchmarks',
    name: 'Benchmarks',
    slice: 'Published performance comparisons',
    model: 'Haiku',
    sources: 19,
    toolCalls: 9,
    tokensRead: 31000,
    tokensReturned: 700,
    seconds: 31,
    costUsd: 0.03,
  },
  {
    id: 'signals',
    name: 'Signals',
    slice: 'Issues, changelogs, release notes',
    model: 'Sonnet',
    sources: 44,
    toolCalls: 17,
    tokensRead: 78000,
    tokensReturned: 1400,
    seconds: 74,
    costUsd: 0.23,
  },
  {
    id: 'risks',
    name: 'Risks',
    slice: 'Breaking changes and migration cost',
    model: 'Opus',
    sources: 26,
    toolCalls: 11,
    tokensRead: 47000,
    tokensReturned: 2100,
    seconds: 105,
    costUsd: 0.71,
  },
]

/** The tools a subagent actually has. Each lane starts the cycle further along. */
const TOOL_NAMES = ['Read', 'Grep', 'Glob', 'WebFetch', 'WebSearch', 'Bash'] as const

/* -------------------------------------------------------------- palettes */

export const AGENT_COST_FLOW_DARK: AgentCostFlowPalette = {
  panel: '#06060A',
  text: '#EDEBE8',
  muted: '#9A99A4',
  faint: '#63626C',
  border: '#23232C',
  lanes: ['#F0A33C', '#46B6DA', '#B9D048', '#8C7BE8', '#E8697C'],
}

/**
 * Light is its own design, not the dark one on a white page. Every lane hue is
 * deepened until it clears 3:1 on the light ground, the strand field switches
 * from screen to multiply so density still darkens rather than washes out, and
 * the travelling glint becomes the lane's own colour, because a white glint is
 * invisible here.
 */
export const AGENT_COST_FLOW_LIGHT: AgentCostFlowPalette = {
  panel: '#F6F4F2',
  text: '#17171A',
  muted: '#5C5C66',
  faint: '#82828C',
  border: '#DFDCD8',
  lanes: ['#A85C08', '#0E6F91', '#55680F', '#5442B8', '#A8263D'],
}

/** How many series colours exist; a sixth agent starts the ramp over. */
const LANE_SLOTS = 5
const laneVar = (i: number) => `var(--acf-lane-${(i % LANE_SLOTS) + 1})`

/**
 * The theme is resolved in CSS, not in script. Both palettes travel on the
 * root as -l and -d custom properties, and these rules pick between them, so
 * the very first paint is already in the right theme, no mark ever flashes the
 * wrong colour while script loads, and a preview box that sets its own theme
 * moves with it. Everything below the root reads var(--acf-role) and nothing
 * hard-codes a hex, which is also what lets the Studio retint the component.
 *
 * The hexes themselves live in exactly one place, the two palette objects
 * above, so the palette props merge over them with nothing to keep in step.
 * These class names have to be literal for Tailwind to see them; the values
 * they point at do not.
 */
const THEME_VARS = [
  '[--acf-panel:var(--acf-panel-l)] dark:[--acf-panel:var(--acf-panel-d)]',
  '[--acf-text:var(--acf-text-l)] dark:[--acf-text:var(--acf-text-d)]',
  '[--acf-muted:var(--acf-muted-l)] dark:[--acf-muted:var(--acf-muted-d)]',
  '[--acf-faint:var(--acf-faint-l)] dark:[--acf-faint:var(--acf-faint-d)]',
  '[--acf-border:var(--acf-border-l)] dark:[--acf-border:var(--acf-border-d)]',
  '[--acf-lane-1:var(--acf-lane-1-l)] dark:[--acf-lane-1:var(--acf-lane-1-d)]',
  '[--acf-lane-2:var(--acf-lane-2-l)] dark:[--acf-lane-2:var(--acf-lane-2-d)]',
  '[--acf-lane-3:var(--acf-lane-3-l)] dark:[--acf-lane-3:var(--acf-lane-3-d)]',
  '[--acf-lane-4:var(--acf-lane-4-l)] dark:[--acf-lane-4:var(--acf-lane-4-d)]',
  '[--acf-lane-5:var(--acf-lane-5-l)] dark:[--acf-lane-5:var(--acf-lane-5-d)]',
  // Not colours, so not palette roles: how the strand field accumulates, how
  // much of it shows, and what a glint is made of on each ground.
  '[--acf-blend:multiply] dark:[--acf-blend:screen]',
  '[--acf-strand-op:0.62] dark:[--acf-strand-op:0.85]',
  // Ribbons overlap where they leave the bar, so they are translucent; the
  // light ground eats more of that than the dark one does.
  '[--acf-ribbon-op:0.5] dark:[--acf-ribbon-op:0.42]',
  // A glint is a strand lighting up, not a spark landing on it, so it takes
  // the colour of the lane it is travelling. On the dark ground it used to be
  // knocked to white, which read as a different kind of mark altogether.
  '[--acf-glint:currentColor]',
  '[--acf-glint-op:1]',
  // The packet is light travelling inside a saturated bar, so it is the one
  // mark that stays near-white on both grounds; every lane hue is deep enough
  // in light mode to carry it.
  '[--acf-packet:#FFFFFF]',
  '[--acf-packet-op:0.9] dark:[--acf-packet-op:0.95]',
].join(' ')

/** One palette flattened onto the root under its own suffix. */
function paletteVars(p: AgentCostFlowPalette, suffix: 'l' | 'd'): Record<string, string> {
  const v: Record<string, string> = {
    [`--acf-panel-${suffix}`]: p.panel,
    [`--acf-text-${suffix}`]: p.text,
    [`--acf-muted-${suffix}`]: p.muted,
    [`--acf-faint-${suffix}`]: p.faint,
    [`--acf-border-${suffix}`]: p.border,
  }
  for (let i = 0; i < LANE_SLOTS; i += 1) {
    v[`--acf-lane-${i + 1}-${suffix}`] = p.lanes[i % p.lanes.length]
  }
  return v
}

/* --------------------------------------------------------------- tuning */

/**
 * Every number the picture is drawn from. Exported so the Studio can reach it,
 * and mergeable through the `tuning` prop so a caller can too; the defaults are
 * the tuned composition. Coordinates are stage pixels on a `stageWidth` by
 * `stageHeight` stage that is scaled to fit its host, so the composition never
 * reflows, it only gets smaller.
 *
 * FIVE LANES IS THE TUNED CASE. Past five, three things need a hand:
 * `laneFirstY` and `lanePitch` place the rows and will run past `stageHeight`
 * (with `contentBottom`, `spineBottom` and `strandBottom` below it) at around
 * eight lanes; `outSpan`, `outMinHeight` and `outGap` share one fixed vertical
 * budget for the cost stack, and once the floors add up to more than the span
 * the stack stops making its argument; and the series colours come from
 * `LANE_SLOTS` (5) at the top of this file, so a sixth lane starts the ramp
 * over unless that constant and the theme block beside it are extended.
 */
export const AGENT_COST_FLOW_TUNING = {
  /* stage */
  /* The stage is wide rather than square because the picture is a journey
     left to right, and a host is usually wider than it is tall: the extra
     width goes into the run between the lanes and the orchestrator, which is
     the part that wants a long, calm curve. */
  stageWidth: 1620,
  stageHeight: 900,
  /* The sheet is bigger than the ink. Nothing is drawn below the floor of the
     strand field or to the right of the bill's own line, so fitting the SHEET
     to the host parked the picture high and to the left with the slack left
     over at the bottom and the right. These are the bounds of what is actually
     drawn, and they are what the host centres, with one margin all round. The
     right edge allows for the line under the bill, which is the widest thing
     on that side and grows with the caller's own figures. */
  contentLeft: 0,
  contentTop: 34,
  contentRight: 1520,
  contentBottom: 790,
  contentMargin: 24,
  /* Below this the picture stacks. The wide stage scaled into a tablet set its
     12px labels at five to seven pixels, so there is no in-between layout:
     under 1000 the stacked one reads at its own size. */
  narrowBreakpoint: 1000,

  /* header */
  headerX: 48,
  eyebrowY: 44,
  taskY: 68,
  subY: 92,
  /* What a pointed lane adds, set as its own block on the centre line rather
     than as another line under the task. Strung out at the left it read as a
     fourth header line and the eye had to leave the lane to follow it; centred
     and stacked it is one thing to look at, and it clears the spine at x=500
     on both sides. It sits under the lanes, in the band between the last lane
     and the floor of the strand field: up at the top it was level with the
     header and read as one more header line, whichever lane it was speaking
     for. It does not name the lane: the row that lit up carries the name, the
     model and the colour already, and repeating them here only said the same
     thing twice. It is centred on the lane column rather than on the stage,
     because the stage's centre counts the bill in and the figures belong to
     the bars; the pitch puts the outer two columns on the ends of the bars and
     keeps every word clear of the spine at x=500. */
  detailCenterX: 712,
  detailLabelY: 726,
  detailValueY: 754,
  detailPitch: 112,

  /* zone A, the strand field */
  strandStartX: 0,
  /* The field is held inside the band the rest of the picture lives in. Left
     to the full height it filled the corners with noise and made the left
     third read as the subject, which it is not. */
  strandTop: 110,
  strandBottom: 790,
  strandJitter: 6,
  strandBundleK: 0.45,
  strandWidth: 0.7,
  strandHotWidth: 1.1,
  strandArriveHalf: 26,

  /* zone B, the spine */
  spineX: 500,
  /* The spine ends where the field ends. Run past it, the hairline carried on
     down into empty stage and read as a divider between the left of the
     picture and the right, which is the opposite of its job: it is the axis
     the strands bundle onto, not a rule between two columns. */
  spineTop: 110,
  spineBottom: 790,
  spineTickLeft: 497,
  spineTickRight: 504,
  /* The hairline is the axis the strands bundle onto, and an axis has no ends
     to announce. Cut square it drew two hard stops in empty stage; carried out
     into nothing at both ends it is only where the field gathers. */
  spineFade: 0.16,
  /* How far either side of the hairline the spine answers a finger or a
     focus ring. A mouse inside the lanes' reach is answered by the lane bands
     instead, so a sweep across the spine never flips the picture to the field
     and back. */
  spineHitPad: 10,

  /* zone C, the lanes */
  laneX: 544,
  laneEndX: 880,
  laneFirstY: 239,
  lanePitch: 98,
  laneHeight: 16,
  laneDotRadius: 2.5,
  laneHitHeight: 56,
  laneNameBaseline: -42,
  laneSliceBaseline: -22,
  /* How far a row's type rises above its baseline, which is where the row
     starts: the lane's band is measured from here, so it holds the lane's name
     as well as its bar. */
  laneNameRise: 10.9,

  /* zone D, the fans */
  /* The band leaves the bar at the bar's own height, so bar and band read as
     one continuous run of work and every pixel the band loses on the way
     right is a pixel of what that lane threw away. */
  fanStartX: 872,
  bandStartHeight: 16,
  /* The hairline is drawn ON the band's own outline, so half of it lands
     outside the shape. The geometry pulls both ends in by that half, which is
     why this width lives here and not only in the stylesheet: change it and
     the band still ends exactly as tall as the bar and as the slot. */
  bandEdgeWidth: 0.75,
  fanK: 0.3,
  fanWidth: 0.7,
  /* Where along the stacked layout's dissolve the stroke has given out and the
     dust is all that is left; the particles are born from this point on. */
  fanFadeFrom: 0.68,

  /* the dissolve: where the stroke gives out, the particles take over */
  fanParticleBirthBias: 1.8,
  fanParticleLifeMinMs: 700,
  fanParticleLifeMaxMs: 2200,
  fanParticleLifeBias: 2.2,
  fanParticleSpeedMin: 0.012,
  fanParticleSpeedMax: 0.03,
  fanParticleDrift: 0.008,
  fanParticleWanderPx: 3,
  fanParticleWanderMs: 1400,
  fanParticleMinSize: 1,
  fanParticleMaxSize: 3,
  fanParticleBigChance: 0.08,
  fanParticleBigSize: 4,
  fanParticleAlpha: 0.9,
  fanParticleWhiten: 0.55,
  fanParticleFalloff: 1.6,
  fanParticleSteps: 12,
  fanParticleBoost: 1.9,
  fanParticleMaxDpr: 2.5,
  sprayDimAlpha: 0.12,
  narrowParticlesPerCurve: 6,

  /* zone E, the orchestrator. Five lanes spread over the whole height of the
     picture arrive inside one short stack, and the stack is the whole answer:
     everything that came back, broken into the five parts that sent it. The
     span is small on purpose, because the point of the picture is that the
     five bands lose most of their height getting here. */
  outLeft: 1276,
  outBlockWidth: 9,
  outBlockRadius: 3,
  outCenterY: 435,
  outSpan: 104,
  outGap: 3,
  outMinHeight: 9,
  /* The readout the stack feeds: one number for the run, set large enough to
     be the thing the eye lands on after crossing the whole picture. */
  hubGap: 28,
  /* Set from the number outwards. The figure is the thing being read, so its
     cap height is centred on the stack it is the sum of, and the label over it
     and the line under it are pushed the same distance off its own edges
     rather than off its baseline, which sits low on a figure this large. */
  hubEyebrowBaseline: -51,
  hubValueBaseline: 25,
  hubValueSize: 64,
  /* The mark is a unit, not a digit, so it stands off the figure. In ems, so
     it holds at whatever size the bill is set in, and wide enough to beat the
     tightened tracking the figure itself carries. */
  hubMarkGap: 0.09,
  hubNoteBaseline: 61,
  /* the small figure a band carries: what that one lane sent back. It rides
     the outer side of its own curve, so it never sits on another band. */
  outLabelU: 0.36,
  outLabelGap: 12,
  outLabelSize: 12,
  /* How far along the band the figure and the share beside it can run. The
     band edges keep clear of that stretch, so pointing at a lane's figure
     lights that lane and never the one whose band it happens to hang over. */
  outLabelReach: 56,

  /* narrow */
  narrowBandHeight: 120,
  narrowSpineY: 112,
  narrowRowHeight: 44,
  narrowRowGap: 8,
  narrowFanHeight: 120,
  narrowFanCap: 10,
  narrowLabelCap: 6,
  narrowPacketLength: 12,
  narrowBarWidth: 4,
  /* the curves stop well short of the band's floor, because everything below
     that line is room for the dust to fall into */
  narrowFanEndFrac: 0.45,

  /* motion */
  strandDrawMs: 900,
  strandSweepMs: 320,
  strandRankMs: 0.4,
  laneStartMs: 620,
  laneStaggerMs: 60,
  laneSpringStiffness: 300,
  laneSpringDamping: 28,
  fanStartMs: 1000,
  fanDrawMs: 420,
  ribbonDrawMs: 720,
  outStartMs: 1620,
  outStaggerMs: 90,
  outDrawMs: 520,
  headerMs: 420,
  glintCount: 14,
  glintBanks: 2,
  glintMinMs: 2600,
  glintStepMs: 130,
  glintDelaySpreadMs: 5200,
  glintBankMs: 6000,
  glintDash: 0.04,
  glintWidth: 0.9,

  /* the chain --------------------------------------------------------------
     One unit of work crosses the whole picture on every cycle: a glint reaches
     the spine, light runs the bar and the band, one fan curve lights, and the
     lane's slot brightens as the work lands. The stage marks below are fractions of one
     cycle, so a lane that runs faster runs every stage of its chain faster and
     no two lanes ever move at the same speed. */
  chainRefMs: 4200,
  chainMinMs: 4200,
  chainMaxMs: 9000,
  chainStartMs: 1400,
  chainGlintEnd: 0.37,
  chainGlintDash: 0.11,
  chainGlintWidth: 1.2,
  chainPacketStart: 0.4,
  chainPacketEnd: 0.64,
  /* The stages are cut to the LENGTH of what the light crosses, so it moves
     at one speed the whole way: roughly 500px of strands, then 330 of bar,
     then 280 of band, then the block. Hand-picked stage marks made it sprint
     the field and crawl the bar. */
  chainFanStart: 0.64,
  chainFanEnd: 0.85,
  chainFanBanks: 3,
  chainOutStart: 0.84,
  /* The last leg. Everything before it answers "what did this lane do";
     this one answers "what did it cost", so it runs from the lane's own
     block into the total and is absorbed there. It starts after the block
     has lit, so the landing is read before the money leaves it. */
  chainBillStart: 0.78,
  billMarkRadius: 2.6,

  /* The run itself, in figures. Nothing on the picture starts at zero: a trace
     is opened on a run already under way, so every number begins at a low
     reading and is carried up by the same half minute. Tokens are READ, so they
     come in fast and flatten; the money is owed for what comes back, so it
     climbs the whole way and only lands at the end. One fraction per kind drives
     every figure of that kind, which is what keeps the five lane bills adding up
     to the total on the right at every moment of the run, not just at the end. */
  runFillMs: 30000,
  runFillFloor: 0.18,
  runTokenPower: 3.2,
  runCostPower: 1.9,

  /* Probing a single mark, in three lengths. Answering the pointer has to
     keep up with the pointer, so the lane that lights comes up on the shortest
     one and the lane it takes over from goes down on the next, both on a curve
     that moves on the first frame: a sweep across the five reads as the light
     travelling with the hand, not as five fades chasing it. Coming back to
     rest has nothing to keep up with, so the field settles over the longest,
     which is still short enough that letting go never reads as stuck. */
  hoverInMs: 90,
  hoverOutMs: 160,
  hoverMs: 180,
  /* Every lane owns a band and the bands meet edge to edge, so going from one
     lane to the next never passes through "nothing is pointed at" and a switch
     needs no grace at all. The hold is only for leaving the chart: long enough
     that a pointer resting on its edge does not flicker, too short to see. */
  hoverHoldMs: 40,
  /* The figures under the lanes roll from one lane's reading to the next
     rather than jumping, and are settled before the eye gets down to them. */
  detailCountMs: 220,
  /* The surge. When a lane lights, one pulse runs every strand that feeds it,
     from the edge of the field into the spine, so the sources that lane read
     are shown being read, once, as the answer to being pointed at. It carries
     no glow: fifty filtered paths in one frame is exactly what a hover must
     not cost. The dash is in path lengths, the width in stage pixels, and the
     fade is the fraction of the pass it takes to come up and to go. */
  surgeMs: 560,
  surgeDash: 0.16,
  surgeWidth: 1.4,
  surgeFade: 0.14,
  focusMs: 420,
  arcStaggerMs: 12,
}

/* -------------------------------------------------------------- helpers */

/** Deterministic noise. Math.random at render time is a hydration bug. */
function mulberry32(seed: number): () => number {
  let a = seed >>> 0
  return () => {
    a = (a + 0x6d2b79f5) >>> 0
    let t = Math.imul(a ^ (a >>> 15), 1 | a)
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296
  }
}

/** FNV-1a over the agent rows, so the same trace always draws the same picture. */
function hashSeed(s: string): number {
  let h = 2166136261
  for (let i = 0; i < s.length; i += 1) {
    h ^= s.charCodeAt(i)
    h = Math.imul(h, 16777619)
  }
  return h >>> 0
}

const round1 = (n: number) => Math.round(n * 10) / 10
const clamp = (n: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, n))
/** Eased 0 to 1 with flat ends, so a blend between two rules has no corner. */
const smoothstep = (p: number) => p * p * (3 - 2 * p)
/** Fast off the mark and soft on landing, for a figure rolling to its value. */
const easeOutCubic = (p: number) => 1 - (1 - p) ** 3

/** The arriving curve: fast at the start, so the first frame already moves. */
const EASE_OUT = 'cubic-bezier(0.16,0.84,0.3,1)'

/**
 * The one curve in the component. A cubic with horizontal tangents at both
 * ends: each handle sits a fraction k of the horizontal run inside its end, so
 * the mass leaves and arrives near parallel and reads like combed hair. Around
 * 0.45 combs tight, around 0.30 splays loose.
 */
function bezH(a: Pt, b: Pt, k: number): string {
  const dx = b.x - a.x
  return `M ${round1(a.x)} ${round1(a.y)} C ${round1(a.x + k * dx)} ${round1(a.y)} ${round1(b.x - k * dx)} ${round1(b.y)} ${round1(b.x)} ${round1(b.y)}`
}

/**
 * The same cubic as bezH, drawn as a band. Both edges are that curve offset by
 * a half-width, and each end carries its own, so a band can leave the bar at
 * the bar's own height and open onto the block it came back with.
 */
function ribbonH(a: Pt, b: Pt, halfA: number, halfB: number, k: number): string {
  const dx = b.x - a.x
  const c1 = round1(a.x + k * dx)
  const c2 = round1(b.x - k * dx)
  const ax = round1(a.x)
  const bx = round1(b.x)
  const aT = round1(a.y - halfA)
  const aB = round1(a.y + halfA)
  const bT = round1(b.y - halfB)
  const bB = round1(b.y + halfB)
  return (
    `M ${ax} ${aT} C ${c1} ${aT} ${c2} ${bT} ${bx} ${bT} ` +
    `L ${bx} ${bB} C ${c2} ${bB} ${c1} ${aB} ${ax} ${aB} Z`
  )
}

/**
 * The same cubic as bezH, evaluated rather than written out. The particle
 * layer needs points along the tail of a curve, and they have to come from
 * the identical formula or the spray would leave the line it is dissolving.
 */
function bezHAt(a: Pt, b: Pt, k: number, u: number): Pt {
  const dx = b.x - a.x
  const c1 = { x: a.x + k * dx, y: a.y }
  const c2 = { x: b.x - k * dx, y: b.y }
  const m = 1 - u
  const w0 = m * m * m
  const w1 = 3 * m * m * u
  const w2 = 3 * m * u * u
  const w3 = u * u * u
  return {
    x: w0 * a.x + w1 * c1.x + w2 * c2.x + w3 * b.x,
    y: w0 * a.y + w1 * c1.y + w2 * c2.y + w3 * b.y,
  }
}

/**
 * A slot in the stack: square where the band arrives and rounded where the
 * run ends, because the left edge is not an edge. An SVG rect rounds all four
 * corners or none, so the shape is written out.
 */
function slotD(x: number, y: number, w: number, h: number, radius: number): string {
  const r = clamp(radius, 0, Math.min(w, h / 2))
  return `M${round1(x)} ${round1(y)}H${round1(x + w - r)}A${r} ${r} 0 0 1 ${round1(x + w)} ${round1(y + r)}V${round1(y + h - r)}A${r} ${r} 0 0 1 ${round1(x + w - r)} ${round1(y + h)}H${round1(x)}Z`
}

/** The same curve turned ninety degrees, for the narrow layout's bands. */
function bezV(a: Pt, b: Pt, k: number): string {
  const dy = b.y - a.y
  return `M ${round1(a.x)} ${round1(a.y)} C ${round1(a.x)} ${round1(a.y + k * dy)} ${round1(b.x)} ${round1(b.y - k * dy)} ${round1(b.x)} ${round1(b.y)}`
}

/** bezV evaluated, for the narrow layout's downward spray. */
function bezVAt(a: Pt, b: Pt, k: number, u: number): Pt {
  const dy = b.y - a.y
  const c1 = { x: a.x, y: a.y + k * dy }
  const c2 = { x: b.x, y: b.y - k * dy }
  const m = 1 - u
  const w0 = m * m * m
  const w1 = 3 * m * m * u
  const w2 = 3 * m * u * u
  const w3 = u * u * u
  return {
    x: w0 * a.x + w1 * c1.x + w2 * c2.x + w3 * b.x,
    y: w0 * a.y + w1 * c1.y + w2 * c2.y + w3 * b.y,
  }
}

const TAU = Math.PI * 2

/** #rgb, #rrggbb or rgb()/rgba() to three channels; anything else stays dark. */
function readRgb(raw: string): [number, number, number] {
  const s = raw.trim()
  if (s.startsWith('#')) {
    const h = s.slice(1)
    if (h.length === 3) return [parseInt(h[0] + h[0], 16), parseInt(h[1] + h[1], 16), parseInt(h[2] + h[2], 16)]
    if (h.length >= 6) return [parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16)]
  }
  const m = s.match(/-?\d+(\.\d+)?/g)
  if (m && m.length >= 3) return [Number(m[0]), Number(m[1]), Number(m[2])]
  return [128, 128, 128]
}

/** 178 stays "178", 62000 becomes "62k", 1200 becomes "1.2k". */
function fmtK(n: number): { num: string; unit: string } {
  if (n < 1000) return { num: String(Math.round(n)), unit: '' }
  const v = n / 1000
  return { num: v < 10 ? v.toFixed(1) : String(Math.round(v)), unit: 'k' }
}
const fmtKs = (n: number) => {
  const f = fmtK(n)
  return f.num + f.unit
}

/** Money is always two places, because a run that cost four cents is a real
    answer and "$0" is not. */
const fmtUsd = (n: number) => `$${n.toFixed(2)}`

/** The bill's own figure: the dollar sign beside it is drawn on its own. */
const fmtCents = (n: number) => n.toFixed(2)

/** A plain figure at a fixed scale, for the blocks that carry their own unit. */
const fmtFixed = (scale: number, decimals: number) => (v: number) => (v / scale).toFixed(decimals)
/* Made once, so a figure can tell a roll to a new value in the same unit from
   a change of unit, which is set at once rather than rolled. */
const FMT_COUNT = fmtFixed(1, 0)
const FMT_MONEY = fmtFixed(1, 2)
const FMT_THOUSANDS = fmtFixed(1000, 0)
const FMT_THOUSANDS_1 = fmtFixed(1000, 1)

/** 62 stays "62s", 108 becomes "1m 48s", 120 becomes "2m". */
function fmtDur(total: number): string {
  const n = Math.max(0, Math.round(total))
  if (n < 60) return `${n}s`
  const m = Math.floor(n / 60)
  const r = n % 60
  return r === 0 ? `${m}m` : `${m}m ${r}s`
}

/** Split a total across n calls with seeded variation, landing exactly on the total. */
function distribute(total: number, n: number, rnd: () => number): number[] {
  if (n <= 0) return []
  const w = Array.from({ length: n }, () => 0.6 + rnd() * 0.8)
  const sum = w.reduce((s, v) => s + v, 0)
  const out = w.map((v) => Math.max(1, Math.round((total * v) / sum)))
  let diff = total - out.reduce((s, v) => s + v, 0)
  let i = 0
  while (diff !== 0 && i < n * 8) {
    const k = i % n
    const step = diff > 0 ? 1 : -1
    if (out[k] + step >= 1) {
      out[k] += step
      diff -= step
    }
    i += 1
  }
  return out
}

/** useLayoutEffect warns during server rendering; the count-up only matters in a browser. */
const useIsoLayout = typeof window === 'undefined' ? useEffect : useLayoutEffect

/* ----------------------------------------------------------- the model */

type Strand = { d: string; delay: number; startY: number; endY: number }
/** One tool call: what it was, and what it read. */
type Call = { name: string; tokens: number }

/**
 * What a lane came back with: one block in the outcome column, the band that
 * flows into it, and the path its cost rides from the band into the total.
 */
type Outcome = { y: number; h: number; ribbonD: string; billD: string; label: Pt; labelAbove: boolean }

/** A lane's own cadence: how often one unit of work crosses the picture. */
type Chain = { cycleMs: number; phaseMs: number; glintD: string; fanPicks: number[] }

type Lane = {
  agent: AgentCostFlowAgent
  index: number
  y: number
  strands: Strand[]
  calls: Call[]
  out: Outcome
  chain: Chain
}

type Model = {
  lanes: Lane[]
  totals: {
    sources: number
    toolCalls: number
    tokensRead: number
    tokensReturned: number
    /** what the whole run cost */
    cost: number
    /** the longest lane, because they ran at the same time */
    wall: number
    /** every lane added up: the same reading one agent at a time */
    serial: number
  }
}

function buildModel(agents: AgentCostFlowAgent[], t: AgentCostFlowTuning): Model {
  const L = agents.length
  const rnd = mulberry32(hashSeed(agents.map((a) => `${a.id}:${a.sources}:${a.toolCalls}`).join('|')))
  const totalSources = agents.reduce((s, a) => s + a.sources, 0)
  const n = Math.max(1, totalSources)

  /* Which lane owns each strand, interleaved round robin by share, so the left
     edge reads as one mixed field and only the spine reads as sorted groups. */
  const share = agents.map((a) => a.sources / n)
  const acc = share.slice()
  const owner: number[] = []
  for (let i = 0; i < totalSources; i += 1) {
    let best = 0
    for (let j = 1; j < L; j += 1) if (acc[j] > acc[best]) best = j
    owner.push(best)
    acc[best] -= 1
    for (let j = 0; j < L; j += 1) acc[j] += share[j]
  }

  const laneY = (i: number) => t.laneFirstY + i * t.lanePitch
  const seen = agents.map(() => 0)
  const strandsByLane: Strand[][] = agents.map(() => [])
  const spanY = t.strandBottom - t.strandTop

  owner.forEach((k, i) => {
    const y0 = t.strandTop + (spanY * (i + 0.5)) / n + (rnd() * 2 - 1) * t.strandJitter
    const m = seen[k]
    seen[k] += 1
    const half = t.strandArriveHalf
    const y1 = laneY(k) - half + (2 * half * (m + 0.5)) / Math.max(1, agents[k].sources)
    strandsByLane[k].push({
      d: bezH({ x: t.strandStartX, y: y0 }, { x: t.spineX, y: y1 }, t.strandBundleK),
      delay: (y0 / t.stageHeight) * t.strandSweepMs + i * t.strandRankMs,
      startY: y0,
      endY: y1,
    })
  })

  /* Tool calls. One curve each, and every fan gets the identical half-spread
     and the identical arc, so the only thing that differs between the five is
     how many curves are packed into the same shape. */
  const rawCalls = agents.map((a, k) => {
    const count = Math.max(0, Math.round(a.toolCalls))
    const tokens = distribute(a.tokensRead, count, rnd)
    return Array.from({ length: count }, (_, i) => {
      const given = a.tools?.[i]
      return {
        name: given?.name ?? TOOL_NAMES[(i + k) % TOOL_NAMES.length],
        tokens: given?.tokens ?? tokens[i],
      }
    })
  })
  /* The busiest lane sets the reference cadence and every other lane's cycle
     is that reference divided by its share of the tool calls, so the rate a
     lane sends work at is its own number, never a hand-picked one. */
  const maxCalls = Math.max(1, ...agents.map((a) => a.toolCalls))

  const lanes: Lane[] = agents.map((a, k) => {
    const y = laneY(k)
    const calls = rawCalls[k]
    const count = calls.length
    const cycleMs = Math.round(
      clamp((t.chainRefMs * maxCalls) / Math.max(1, a.toolCalls), t.chainMinMs, t.chainMaxMs),
    )
    const phaseMs = Math.round(rnd() * cycleMs)
    const built: Call[] = calls.map((c) => ({ name: c.name, tokens: c.tokens }))

    /* Which strand carries this lane's chain glint, and which fan curves take
       turns lighting; one per cycle, rotating, so the fan never looks scripted. */
    const strands = strandsByLane[k]
    const banks = Math.max(1, Math.round(t.chainFanBanks))
    const chain: Chain = {
      cycleMs,
      phaseMs,
      glintD: strands.length ? strands[Math.floor(rnd() * strands.length)].d : '',
      fanPicks: Array.from({ length: banks }, (_, b) =>
        count > 0 ? Math.min(count - 1, Math.floor(((b + 0.5) / banks) * count)) : 0,
      ),
    }
    // the band and the block are laid out once all five heights are known
    const out: Outcome = { y, h: 0, ribbonD: '', billD: '', label: { x: 0, y: 0 }, labelAbove: true }
    return { agent: a, index: k, y, strands, calls: built, out, chain }
  })

  /* The cost stack, sized by what each lane cost and centred as one block,
     so five lanes spread over the height of the picture arrive inside
     something far shorter. Each band leaves its bar at the height of what
     that lane READ and narrows to the slot of what it COST, which is the
     whole argument for mixed models drawn rather than described: the lane
     that read the most is not the lane that ends up widest. */
  const spend = Math.max(1e-9, agents.reduce((sum, a) => sum + a.costUsd, 0))
  const room = Math.max(1, t.outSpan - Math.max(0, lanes.length - 1) * t.outGap)
  /* Every slot gets a floor first, so the quietest lane is still a mark and
     not a line, and what is left over is split by share. */
  const floor = Math.min(t.outMinHeight, room / Math.max(1, lanes.length))
  const spare = room - floor * lanes.length
  const heights = lanes.map((l) => round1(floor + spare * (l.agent.costUsd / spend)))
  const stackH = heights.reduce((s, h) => s + h, 0) + Math.max(0, lanes.length - 1) * t.outGap
  let top = t.outCenterY - stackH / 2
  lanes.forEach((l, k) => {
    const h = heights[k]
    const cy = round1(top + h / 2)
    const a: Pt = { x: t.fanStartX, y: l.y }
    const b: Pt = { x: t.outLeft, y: cy }
    /* Half the hairline sits outside the path, so both ends are pulled in by
       it: painted, the band leaves at exactly the bar's height and lands at
       exactly the slot's, instead of overhanging both by a hair. */
    const edge = t.bandEdgeWidth / 2
    const startHalf = Math.max(0, t.bandStartHeight / 2 - edge)
    const endHalf = Math.max(0, h / 2 - edge)
    const u = clamp(t.outLabelU, 0, 1)
    const mid = bezHAt(a, b, t.fanK, u)
    /* Above when the band is on its way down, below when it is on its way up:
       the figure always sits on the outside of the bend, never on a neighbour. */
    const above = cy >= l.y
    const halfAt = startHalf + (endHalf - startHalf) * u + edge
    /* The last leg: the lane's cost leaves its own figure on the band, rides
       the band's centre line down to the stack, and gives out as it lands.
       Sampled from the same bezier the band is drawn from, so the mark is
       inside the shape it is travelling for every frame of it and stops where
       the shape does. Carried on to the number it once read as a mark loose on
       the stage, which is the one place it does not belong. */
    const steps = 12
    const runPts: string[] = []
    for (let i = 0; i <= steps; i += 1) {
      const uu = u + (1 - u) * (i / steps)
      const pt = bezHAt(a, b, t.fanK, uu)
      runPts.push(`${round1(pt.x)} ${round1(pt.y)}`)
    }
    l.out = {
      y: cy,
      h,
      ribbonD: ribbonH(a, b, startHalf, endHalf, t.fanK),
      billD: `M ${runPts.join(' L ')}`,
      label: {
        x: round1(mid.x),
        y: round1(mid.y + (above ? -1 : 1) * (halfAt + t.outLabelGap)),
      },
      labelAbove: above,
    }
    top += h + t.outGap
  })

  const tokensRead = agents.reduce((s, a) => s + a.tokensRead, 0)
  const tokensReturned = agents.reduce((s, a) => s + a.tokensReturned, 0)
  return {
    lanes,
    totals: {
      sources: totalSources,
      toolCalls: agents.reduce((s, a) => s + a.toolCalls, 0),
      tokensRead,
      tokensReturned,
      cost: agents.reduce((s, a) => s + a.costUsd, 0),
      wall: agents.reduce((s, a) => Math.max(s, a.seconds), 0),
      serial: agents.reduce((s, a) => s + a.seconds, 0),
    },
  }
}

/* ------------------------------------------------------------ the bands */

/**
 * Where the pointer is, answered by rule rather than by whatever shape happens
 * to be under it. Every lane owns a band across the whole chart and the bands
 * meet edge to edge, so there is no gap between two lanes for a sweep to fall
 * into and no mark too thin to find. On the left a band is the lane's row,
 * name and bar together, and the sector of the field that bundles into it; on
 * the right it follows the lane's own band down into its slot in the stack,
 * so pointing at a band lights that band however far it has curved from its
 * row. Each edge between two lanes runs halfway across the empty space between
 * what the two of them paint, cost figure included, so whatever the pointer is
 * on belongs to the lane it belongs to in the picture.
 */
type BandRow = { y: number; slotY: number; slotH: number; labelX: number; labelY: number; labelAbove: boolean }
type Bands = { left: number; right: number; top: number; bottom: number; rows: BandRow[] }

function buildBands(lanes: Lane[], t: AgentCostFlowTuning): Bands {
  const nameTop = t.laneNameBaseline - t.laneNameRise
  const barHalf = t.laneHeight / 2
  /* The outer two bands reach past the picture by as much as the inner ones
     reach into the gap between two rows, so the first and the last lane are
     exactly as easy to find as the three between them. */
  const pad = Math.max(0, (t.lanePitch - (barHalf - nameTop)) / 2)
  const first = lanes[0]
  const end = lanes[lanes.length - 1]
  return {
    left: t.strandStartX,
    /* Halfway from the stack to the bill. The total is every lane at once, so
       pointing at it is pointing at none of them, and the picture rests. */
    right: t.outLeft + t.outBlockWidth + t.hubGap / 2,
    top: first ? Math.min(first.y + nameTop, first.out.y - first.out.h / 2) - pad : 0,
    bottom: end ? Math.max(end.y + barHalf, end.out.y + end.out.h / 2) + pad : 0,
    rows: lanes.map((l) => ({
      y: l.y,
      slotY: l.out.y,
      slotH: l.out.h,
      labelX: l.out.label.x,
      labelY: l.out.label.y,
      labelAbove: l.out.labelAbove,
    })),
  }
}

/** The fan's curve with y running from 0 at the bars to 1 at the stack: at any
    x, y is how far every band has travelled from its row to its slot. */
const fanAt = (t: AgentCostFlowTuning, u: number) => bezHAt({ x: t.fanStartX, y: 0 }, { x: t.outLeft, y: 1 }, t.fanK, u)

/** Enough halvings to place u well inside a stage pixel on any fan width. */
const FAN_HALVINGS = 14

/** Every band shares the same horizontal handles, so one solve for u at the
    pointer's x places it on all of them at once. */
function fanU(t: AgentCostFlowTuning, x: number): number {
  let lo = 0
  let hi = 1
  for (let i = 0; i < FAN_HALVINGS; i += 1) {
    const m = (lo + hi) / 2
    if (fanAt(t, m).x < x) lo = m
    else hi = m
  }
  return (lo + hi) / 2
}

/** The lane whose band holds a stage point, or null outside the chart. */
function bandAt(b: Bands, t: AgentCostFlowTuning, x: number, y: number): number | null {
  const rows = b.rows
  if (rows.length === 0 || x < b.left || x > b.right || y < b.top || y > b.bottom) return null
  const nameTop = t.laneNameBaseline - t.laneNameRise
  const barHalf = t.laneHeight / 2
  // how far a row's name stands above its bar
  const rise = Math.max(0, -nameTop - barHalf)
  const open = t.bandStartHeight / 2
  const zone = x <= t.fanStartX ? 'row' : x >= t.outLeft ? 'slot' : 'fan'
  let late = 0
  let ramp = 0
  if (zone === 'fan') {
    const u = fanU(t, x)
    late = fanAt(t, u).y
    /* The row's name stands over the start of its band, then hands over to
       the band's own figure by the point that figure rides at, with no corner
       in between. */
    ramp = t.outLabelU > 0 ? smoothstep(clamp(u / t.outLabelU, 0, 1)) : 1
  }
  const clear = (labelX: number) => Math.max(0, Math.abs(x - labelX) - t.outLabelReach)
  for (let k = 0; k < rows.length - 1; k += 1) {
    const a = rows[k]
    const c = rows[k + 1]
    let edge: number
    if (zone === 'row') edge = (a.y + barHalf + c.y + nameTop) / 2
    else if (zone === 'slot') edge = (a.slotY + a.slotH / 2 + c.slotY - c.slotH / 2) / 2
    else {
      // the painted underside of the upper band and top of the lower one
      const under = (1 - late) * (a.y + open) + late * (a.slotY + a.slotH / 2)
      const over = (1 - late) * (c.y - open) + late * (c.slotY - c.slotH / 2) - rise * (1 - ramp)
      /* A figure riding outside its band pushes the edge away from itself, but
         never past the neighbour's own band: a shape outranks a label. */
      const low = a.labelAbove ? under : Math.max(under, Math.min(over, a.labelY + t.outLabelSize - clear(a.labelX)))
      const high = c.labelAbove ? Math.min(over, Math.max(under, c.labelY - t.outLabelSize + clear(c.labelX))) : over
      edge = (low + high) / 2
    }
    if (y <= edge) return k
  }
  return rows.length - 1
}

/** The lit lane is one attribute on the stage, written straight to the node. */
function paintLit(stage: HTMLElement | null, k: number | null) {
  if (!stage) return
  if (k === null) stage.removeAttribute('data-hot')
  else stage.setAttribute('data-hot', String(k))
}

/* -------------------------------------------------------------- styles */

/**
 * The component's own stylesheet. Two jobs, both of them things React must not
 * do here. The idle motion is pure CSS driven by per-element custom
 * properties, so nothing re-renders per frame. The dimming reads one attribute
 * on the stage, so pointing at a lane changes one string and never touches the
 * 178 strand nodes underneath it.
 */
function styleSheet(laneCount: number, t: AgentCostFlowTuning): string {
  /* Stage marks are fractions of one cycle, so one set of keyframes serves
     every lane and only the duration and the phase change per element. */
  const pc = (f: number) => `${round1(clamp(f, 0, 1) * 100)}%`

  /** A bright dash that runs one path between two fractions of the cycle. */
  const glide = (name: string, from: number, to: number) => {
    const span = Math.max(0.02, to - from)
    const stops: string[] = [`0%{stroke-dashoffset:var(--acf-g0);opacity:0}`]
    if (from > 0.001) stops.push(`${pc(from)}{stroke-dashoffset:var(--acf-g0);opacity:0}`)
    stops.push(
      `${pc(from + span * 0.1)}{opacity:1}`,
      `${pc(to - span * 0.16)}{opacity:1}`,
      `${pc(to)}{stroke-dashoffset:var(--acf-g1);opacity:0}`,
      `100%{stroke-dashoffset:var(--acf-g1);opacity:0}`,
    )
    return `@keyframes ${name}{${stops.join('')}}`
  }

  const A = t.chainPacketStart
  const B = t.chainPacketEnd
  const dim: string[] = []
  const lift: string[] = []
  for (let i = 0; i < laneCount; i += 1) {
    const on = `.acf-stage[data-hot='${i}']`
    const own = `[data-lane='${i}']`
    dim.push(
      `${on} .acf-gl:not(${own}){opacity:.12}`,
      `${on} .acf-sg${own} .acf-strand{stroke-width:var(--acf-strand-hot)}`,
      /* A dimmed shape reads as background; a dimmed moving mark still reads
         as a second thing happening. So the marks that TRAVEL - the ambient
         glints, each lane's chain glint and the cost it carries to the bill -
         fade out while one lane is pointed at, and only that lane's own are
         left running. Faded, never display:none: display would restart the
         very animation that drives them, so every return from a hover popped.
         The bill marks fade on a group around them, because their own opacity
         belongs to the keyframes that run them, and a running animation
         outranks any rule written here. */
      `${on} .acf-bank:not(${own}){opacity:0}`,
      `${on} .acf-cgg:not(${own}){opacity:0}`,
      `${on} .acf-bmg:not(${own}){opacity:0}`,
      // the two things only a lit lane shows: its surge and its share
      `${on} .acf-surge${own}{opacity:1}`,
      `${on} ${own} .acf-share{opacity:1}`,
    )
    lift.push(`${on} ${own},${on} ${own} .acf-strand,${on} ${own} .acf-share{transition-duration:var(--acf-hover-in-ms)}`)
  }

  return `
.acf-strands{mix-blend-mode:var(--acf-blend);opacity:var(--acf-strand-op)}
.acf-strand{fill:none;stroke-width:var(--acf-strand-w);stroke-dasharray:1}
/* The band is translucent so the bands behind it stay readable where they
   crowd together, and it carries a hairline of its own colour so the edge
   stays drawn instead of dissolving into the one next to it. */
.acf-ribbon{fill-opacity:var(--acf-ribbon-op);stroke-width:${t.bandEdgeWidth};stroke-opacity:calc(var(--acf-ribbon-op) * 1.25);transition:fill-opacity var(--acf-hover-ms) var(--acf-hover-ease)}
.acf-fanline{fill:none;stroke-width:var(--acf-fan-w);stroke-dasharray:1;stroke-opacity:0}
.acf-glint{fill:none;stroke:var(--acf-glint);opacity:var(--acf-glint-op);stroke-linecap:round}
/* The hover is one opacity cross-fade and nothing else. It used to drop a
   saturate() filter on the dimmed lanes at the same time: easing that filter
   over a group this deep re-rasterises the whole field on every frame, and
   landing it at once put a step in the middle of a fade. Without it the lanes
   keep their own colour as they go faint, and the whole change is one curve.
   That curve is its own ease, not the stage's: the display ease starts fast
   and is right for something arriving, wrong for a field settling. */
.acf-gl{transition:opacity var(--acf-hover-ms) var(--acf-hover-ease)}
.acf-bank,.acf-bmg,.acf-surge,.acf-share{transition:opacity var(--acf-hover-ms) var(--acf-hover-ease)}
.acf-strand{transition:stroke-width var(--acf-hover-ms) var(--acf-hover-ease)}
/* The surge is a copy of each lane's strands that only shows while its pulse
   runs, and the share is a figure only a lit lane prints. */
.acf-surge,.acf-share{opacity:0}
.acf-surge path{fill:none;stroke-linecap:round;visibility:hidden}
${dim.join('\n')}

/* While a lane is pointed at, every change is an answer to the pointer, so
   all of it runs on the arriving curve, the one that moves on the first
   frame. What lets go runs on the out length; the lane that lights runs on
   the in length, which is shorter, so on a switch the new lane is already up
   while the old one is still going and the two never meet at half. The
   settle, the field coming back up once the pointer has gone, is left on the
   hover length and its own ease. */
.acf-stage[data-hot] .acf-gl,
.acf-stage[data-hot] .acf-bank,
.acf-stage[data-hot] .acf-bmg,
.acf-stage[data-hot] .acf-surge,
.acf-stage[data-hot] .acf-share,
.acf-stage[data-hot] .acf-strand{transition-duration:var(--acf-hover-out-ms);transition-timing-function:${EASE_OUT}}
${lift.join('\n')}

/* the spine reads the whole field at once */
.acf-stage[data-spine] .acf-strand{stroke-width:var(--acf-strand-hot)}
.acf-stage[data-spine] .acf-tg{opacity:1}

.acf-out{animation:acf-fadein var(--acf-out-ms) ${EASE_OUT} var(--acf-d) both,acf-land var(--acf-cycle) linear var(--acf-phase) infinite}
.acf-outlabel{animation:acf-fadein var(--acf-out-ms) ${EASE_OUT} var(--acf-d) both}
/* the two blocks that only ever arrive: no exit, so no JS */
.acf-fadein{animation:acf-fadein var(--acf-fade-ms) ${EASE_OUT} var(--acf-d) both}

/* the packet of work travelling a lane, used by the narrow layout */
.acf-packet{border-radius:9999px;background:var(--acf-packet);opacity:0;pointer-events:none;will-change:transform}

@keyframes acf-draw{from{stroke-dashoffset:1}to{stroke-dashoffset:0}}
@keyframes acf-glide{from{stroke-dashoffset:var(--acf-g0)}to{stroke-dashoffset:var(--acf-g1)}}
@keyframes acf-bank{0%{visibility:visible}50%{visibility:hidden}100%{visibility:hidden}}
@keyframes acf-fadein{from{opacity:0}to{opacity:1}}
/* the band is extruded from the bar's tip rather than faded in, which is the
   same direction everything else in the picture travels */
@keyframes acf-ribbonin{from{transform:scaleX(0);opacity:0}to{transform:scaleX(1);opacity:1}}
${glide('acf-cglide', 0, t.chainGlintEnd)}
@keyframes acf-pktv-move{0%{transform:translateY(0)}${pc(A)}{transform:translateY(0);animation-timing-function:${EASE_OUT}}${pc(B)}{transform:translateY(var(--acf-run))}100%{transform:translateY(var(--acf-run))}}
@keyframes acf-pkt-fade{0%{opacity:0}${pc(A)}{opacity:0}${pc(A + (B - A) * 0.12)}{opacity:var(--acf-packet-op)}${pc(B - (B - A) * 0.18)}{opacity:var(--acf-packet-op)}${pc(B)}{opacity:0}100%{opacity:0}}
/* the landing: the block brightens as the glint in its band reaches it,
   which is the only motion in the column once it is on screen */
@keyframes acf-land{0%{fill-opacity:.7}${pc(t.chainOutStart)}{fill-opacity:.7}${pc(t.chainOutStart + 0.03)}{fill-opacity:1}${pc(Math.min(1, t.chainOutStart + 0.14))}{fill-opacity:.7}100%{fill-opacity:.7}}

.acf-strand{animation:acf-draw var(--acf-draw-ms) cubic-bezier(0.22,1,0.36,1) var(--acf-d) both}
.acf-fanline{animation:acf-draw var(--acf-fan-ms) cubic-bezier(0.22,1,0.36,1) var(--acf-d) both}
.acf-ribbon{transform-box:fill-box;transform-origin:left center;animation:acf-ribbonin var(--acf-ribbon-ms) ${EASE_OUT} var(--acf-d) both}
.acf-glint{animation:acf-glide var(--acf-gd) linear var(--acf-gdelay) infinite}
.acf-bank{animation:acf-bank var(--acf-bank-ms) step-end var(--acf-bank-delay) infinite}
.acf-cglint{fill:none;stroke-linecap:round;opacity:0;animation:acf-cglide var(--acf-cycle) linear var(--acf-phase) infinite}
/* the lane's cost travelling the last gap into the total. One shared
   keyframe; each lane carries its own delta, so five different distances
   converge on one point and arrive on their own cycles rather than
   together, which is what five agents billing separately looks like. */
@keyframes acf-billrun{0%{opacity:0;offset-distance:0%}${pc(t.chainBillStart)}{opacity:0;offset-distance:0%}${pc(t.chainBillStart + 0.03)}{opacity:1}${pc(0.93)}{opacity:1}100%{opacity:0;offset-distance:100%}}
.acf-bm{offset-path:var(--acf-run);offset-rotate:0deg;offset-distance:0%;opacity:0;animation:acf-billrun var(--acf-cycle) linear var(--acf-phase) infinite;filter:drop-shadow(0 0 3px var(--acf-glint))}
/* the glow is what turns a short bright segment into a light rather than a
   dash; it costs one filter on the handful of marks that carry the chain */
.acf-glint,.acf-cglint{filter:drop-shadow(0 0 3px var(--acf-glint))}
/* No fade on the packet: it is already invisible before it arrives and after it
   leaves, and a fade on top of that is what made the hand-off blink. */
.acf-packet-v{animation:acf-pktv-move var(--acf-cycle) linear var(--acf-phase) infinite,acf-pkt-fade var(--acf-cycle) linear var(--acf-phase) infinite}

[data-acf-off] *{animation-play-state:paused}

@media (prefers-reduced-motion: reduce){
.acf-strand,.acf-fanline{animation:none;stroke-dashoffset:0}
.acf-glint,.acf-bank,.acf-cglint,.acf-packet,.acf-packet-v,.acf-bm,.acf-surge{display:none}
.acf-out,.acf-outlabel,.acf-fadein{animation:none;opacity:1}
.acf-ribbon{animation:none;transform:none}
.acf-gl,.acf-strand,.acf-out,.acf-fanline,.acf-ribbon,.acf-bmg,.acf-share{transition:none}
}
`
}

/* ------------------------------------------------------- the dissolve */

/** One curve the spray comes off, in stage coordinates. */
type SprayCurve = { a: Pt; b: Pt; lane: number; call: number }
/** What the canvas needs to know about a lane's chain, to flare with it. */
type SprayChain = { cycleMs: number; phaseMs: number; picks: number[] }
type SprayBox = { left: number; top: number; width: number; height: number }

type Particle = {
  lane: number
  call: number
  bx: number
  by: number
  vx: number
  vy: number
  life: number
  age: number
  size: number
  phase: number
  amp: number
}

/* ------------------------------------------------------------- run fill */

/** The three speeds the figures move at: what was read, what it cost, how long. */
type RunFill = { tokens: number; cost: number; time: number }
const RUN_FILL_DONE: RunFill = { tokens: 1, cost: 1, time: 1 }
type RunSubscribe = (fn: (f: RunFill) => void) => () => void

/* ------------------------------------------------------------ component */

/**
 * @typedef {object} AgentCostFlowProps
 * @property {string} [task='Evaluate migrating the app to a new framework'] The one task the orchestrator decomposed.
 * @property {string} [eyebrow='ORCHESTRATOR TRACE'] The small letter-spaced caps line above the task.
 * @property {AgentCostFlowAgent[]} [agents=AGENT_COST_FLOW_AGENTS] One row per subagent; every total in the graphic is derived from this array.
 * @property {number | null} [initialAgent=null] Index of the lane lit when the picture opens, or null for the rest state.
 * @property {(id: string | null, index: number | null) => void} [onAgentChange] Called with the agent id and index as the lit lane changes, and with nulls when none is lit.
 * @property {string} [elapsedParallel] Overrides the run's wall clock, which is otherwise the longest subagent, since they run at the same time. Printed in the stacked layout only.
 * @property {string} [elapsedSerial] Overrides the serial clock, which is otherwise every subagent's time added up: the same reading done one agent at a time. Printed in the stacked layout only.
 * @property {boolean} [showOutcome=true] Whether the outcome column, one block and label per subagent, is drawn at the right edge.
 * @property {'auto' | 'wide' | 'narrow'} [layout='auto'] Force the wide stage or the stacked layout instead of measuring the host.
 * @property {Partial<AgentCostFlowPalette>} [paletteDark] Colour overrides for the dark theme, merged over AGENT_COST_FLOW_DARK.
 * @property {Partial<AgentCostFlowPalette>} [paletteLight] Colour overrides for the light theme, merged over AGENT_COST_FLOW_LIGHT.
 * @property {Partial<AgentCostFlowTuning>} [tuning] Geometry and timing overrides, merged over AGENT_COST_FLOW_TUNING. The hover reads hoverInMs, hoverOutMs, hoverMs, hoverHoldMs and detailCountMs; the surge a lit lane sends down its strands reads surgeMs, surgeDash, surgeWidth and surgeFade; the lane bands are measured with laneNameRise, outLabelSize, outLabelReach and spineHitPad.
 * @property {string} [className] Extra classes merged onto the root element.
 */
export type AgentCostFlowProps = {
  task?: string
  eyebrow?: string
  agents?: AgentCostFlowAgent[]
  initialAgent?: number | null
  onAgentChange?: (id: string | null, index: number | null) => void
  elapsedParallel?: string
  elapsedSerial?: string
  showOutcome?: boolean
  layout?: 'auto' | 'wide' | 'narrow'
  paletteDark?: Partial<AgentCostFlowPalette>
  paletteLight?: Partial<AgentCostFlowPalette>
  tuning?: Partial<AgentCostFlowTuning>
  className?: string
}

/** One mark the visitor is pointing at, tabbed to, or has tapped. */
const noSubscribe = () => () => {}
const CAPS = (tracking: string): CSSProperties => ({
  textTransform: 'uppercase',
  letterSpacing: tracking,
  fontWeight: 600,
})

/* absolute type placement --------------------------------------------------
   The spec is written in baselines; a DOM span is placed by its top, and
   with a line height of one the baseline sits about 0.78 of the size down. */
const at = (x: number, baseline: number, size: number, extra?: CSSProperties): CSSProperties => ({
  position: 'absolute',
  left: x,
  top: baseline - size * 0.78,
  fontSize: size,
  lineHeight: 1,
  whiteSpace: 'nowrap',
  ...extra,
})

/** The lit lane as the rest of the component reads it: a ref and a set of
    listeners, so a hover re-renders only what subscribes to it. */
type LitStore = { subscribe: (fn: () => void) => () => void; get: () => number | null }

function useLit(store: LitStore, count: number): number | null {
  const lit = useSyncExternalStore(store.subscribe, store.get, store.get)
  return lit === null || count === 0 ? null : Math.min(lit, count - 1)
}
```
````

## Related

- [AI Knowledge Map](https://aicanvas.me/components/ai-knowledge-map.md): A dotted AI orb sits above a confidence indicator and wires to five scored category cards. Clicking one fans its results out along a curved arc.
- [Signature Pad](https://aicanvas.me/components/signature-pad.md): A signature pad widget: pill button morphs into a canvas to draw with mouse or touch.
- [Sticker Wall](https://aicanvas.me/components/sticker-wall.md): A draggable sticker card wall. Drop notes, toss emoji, pile them up with real 2D physics.
- [Agent Feedback Loop](https://aicanvas.me/components/agent-feedback-loop.md): An isometric widget of six AI agent stages in a loop. Hover or tap one to lift it apart.
- [Crypto Swap](https://aicanvas.me/components/crypto-swap.md): A crypto token-swap widget with live exchange rates, price impact, and an animated swap button.
