> 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 Feedback Loop

An isometric widget of six AI agent stages in a loop. Hover or tap one to lift it apart.

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

## Install

```bash
npx shadcn@latest add @aicanvas/agent-feedback-loop
```

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 |
| --- | --- | --- | --- |
| `stages?` | `AgentFeedbackLoopStage[]` | `AGENT_FEEDBACK_LOOP_STAGES` | The six stage names and descriptions, left to right. The drawings stay tied to position, so entry 0 is always the card fan and entry 5 the tray. |
| `feedbackLabel?` | `string` | `'Feedback'` | The caption under the cube on the loop. |
| `labels?` | `'hover' \| 'always'` | `'hover'` | Whether stage names appear on hover, focus and tap, or stay visible. On narrow screens they are always visible. |
| `ambient?` | `boolean` | `true` | Whether the relay runs after the intro: a comet travels each conduit in turn, the stage it reaches briefly plays its motion and emits the next comet in its own colour, then the loop carries it through the cube back to Discover and the cycle repeats. Hovering a stage, or holding one with activeStage, pauses it. Off, the conduits are still, with a small notch at each entry port showing the direction, as with reduced motion. |
| `playIntro?` | `boolean` | `true` | Whether the stages assemble one by one when the picture first scrolls into view. Off, it opens composed. |
| `onStageClick?` | `(id: string, index: number) => void` |  | Called with the stage id and index on click, Enter or Space, and on a second tap on touch. The Feedback cube is a seventh button and calls it with 'feedback' and 6. |
| `onStageHover?` | `(id: string \| null, index: number) => void` |  | Called with the stage id and index on pointer enter, focus or a first tap, and with null and -1 when it ends. The Feedback cube reports 'feedback' and 6. |
| `activeStage?` | `string` |  | Id of a stage to hold in its hovered state, or 'feedback' for the cube. Hovering another stage still previews it. |
| `paletteDark?` | `Partial<AgentFeedbackLoopPalette>` |  | Colour overrides for the dark theme, merged over AGENT_FEEDBACK_LOOP_DARK. `lanes` (string[]) is the series ramp: Discover and Extract take lane 1, Evaluate to Learn lanes 2 to 5, the Feedback cube lane 1 again; a shorter ramp wraps. Every coloured surface, glow, badge shadow and the travelling light is mixed from it, and glyphs on a lane switch to the dark ground where white would fall under 3:1. |
| `paletteLight?` | `Partial<AgentFeedbackLoopPalette>` |  | Colour overrides for the light theme, merged over AGENT_FEEDBACK_LOOP_LIGHT, `lanes` included. The light lanes are fills on white tiles: keep each deep enough that white glyphs on it clear 3:1 (the default amber does at 3.19:1), or the glyphs switch to the dark ground. |
| `tuning?` | `Partial<AgentFeedbackLoopTuning>` |  | Timing and motion overrides, merged over AGENT_FEEDBACK_LOOP_TUNING. |
| `className?` | `string` |  | Extra classes merged onto the root element. |

## About

Agent Feedback Loop draws an AI agent pipeline as six isometric objects in a row, from Discover through Extract, Evaluate, Review and Activate to Learn, with a thin line carrying the outcome back through a small glass cube. Each stage assembles in turn as it scrolls into view, and hovering, focusing or tapping one lifts it into a named exploded view. A coloured light then relays around the circuit, one hue per stage. Motion drives the assembly and Tailwind CSS switches the light and dark palettes. Twelve props take your own stage names, colours and timing. It suits AI agent product pages, workflow explainers and automation docs.

Use cases: AI Agent Workflow Diagram, Human-in-the-Loop Workflow, AI Automation Landing Page.

## Source

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

## 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 Feedback Loop
Six isometric SVG stages of an agent pipeline, joined by conduits, while a coloured comet relays round a feedback loop.

## 1. Setup
// npm install framer-motion
File: components-workspace/agent-feedback-loop/index.tsx  ·  'use client'  ·  export default function AgentFeedbackLoop(props: AgentFeedbackLoopProps)
Needs Tailwind CSS v4 with a class-based dark variant (`@custom-variant dark (&:where(.dark, .dark *));`), and Manrope loaded by the host app (the root names it with a system-sans fallback). No images, no icon library, no canvas: every object is inline SVG built from one isometric projection, every colour is a CSS custom property, and the lane shades are `color-mix(in srgb, ...)`. One file. Assemble it in this order: block 1, then block 2 with each `/* Name: the component in block 4 ... */` line replaced by that helper (its code block from block 4, with its tree turned back into JSX where it says `return ( ... tree below ... )`), then block 3 followed by block 4's first tree as its `return (`.

```tsx
import {
  createContext,
  useContext,
  useEffect,
  useId,
  useMemo,
  useRef,
  useState,
  useSyncExternalStore,
} from 'react'
import type {
  CSSProperties,
  FocusEvent as ReactFocusEvent,
  KeyboardEvent as ReactKeyboardEvent,
  PointerEvent as ReactPointerEvent,
  ReactNode,
} from 'react'
import {
  MotionConfig,
  animate,
  cubicBezier,
  motion,
  useMotionValue,
  useReducedMotion,
  useTransform,
} from 'framer-motion'
import type { MotionValue, Transition } from 'framer-motion'
```

## 2. Constants
Everything above the component function, verbatim and in source order. The picture IS these numbers: palettes, projection, port positions, footprints, both layouts and every timing, so nothing here is summarised.

```tsx
/**
 * Agent Feedback Loop. Six isometric vector objects walk an AI agent pipeline
 * left to right, Discover to Learn, and a thin line carries the outcome back
 * from Learn to Discover through a small glass cube. Every object stands on one
 * shared isometric projection and is built from the same few primitives (a
 * frosted slab with real thickness, a raised coloured badge, an isometric bar),
 * lit from above and floating just over its own soft floor shadow, so the six
 * read as one illustration rather than six icons.
 *
 * On entering the viewport each object assembles in turn: its shadow spreads,
 * its layers settle in from the depth axis back to front, and its coloured detail
 * pops in last, while the conduit ahead of it draws. The loop then draws as
 * one stroke. Pointing at, focusing or tapping an object lifts it into an
 * exploded view of its own and reveals its name. Each stage carries one hue of
 * a series ramp, and conduits plug each object into the next through small
 * ports. Afterwards a relay runs the circuit: a comet leaves one object, the
 * next one processes it and emits a new comet in its own hue, round the loop
 * through the cube and back into Discover.
 *
 * The theme is resolved in CSS: both palettes travel on the root as -l and -d
 * custom properties and the dark variant picks one, so the first paint is
 * already right. Everything below the root reads var(--fl-role).
 */

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

type CubicEase = [number, number, number, number]
type Pt = { x: number; y: number }

export type AgentFeedbackLoopStage = { id: string; label: string; description?: string }

export type AgentFeedbackLoopPalette = {
  /** page ground, and the centre of its soft radial vignette */
  panel: string
  groundCenter: string
  /** stage labels, the Feedback caption, and the quiet tiers */
  text: string
  muted: string
  faint: string
  /** the conduits, their resting flow and the brace */
  border: string
  /** tile face, top to bottom */
  raised: string
  raisedLow: string
  /** slab thickness, graded from its lit top to its shaded foot */
  slab: string
  slabEdge: string
  /** the 1px bevel along a face's top and left */
  raisedBorder: string
  /** the faint contour of every slab */
  hairline: string
  /** face of the layers that sit further back */
  recede: string
  /** recessed wells: the bar chart well, the tray floor */
  inset: string
  /** the frosted glint in each face's top left */
  frost: string
  /** line glyphs inside the tiles */
  glyph: string
  glyphStrong: string
  glyphSoft: string
  /** the lit 1px ridge along a coloured part's top and left */
  ridge: string
  /**
   * The series ramp, one hue per stage in order: Discover and Extract share
   * the first, Evaluate, Review, Activate and Learn take the next four, and
   * the Feedback cube returns to the first. Every coloured surface, its lit
   * and shaded sides, its glow, its badge shadow and the travelling light are
   * mixed from these in CSS, so an override recolours all of it. A shorter
   * ramp wraps. Glyphs on a lane are white where white clears 3:1, otherwise
   * the palette's darkest ground.
   */
  lanes: string[]
  /** floor and badge shadows */
  shadowCore: string
  /** the keyboard focus ring, one neutral colour for every stage */
  focus: string
}

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

export const AGENT_FEEDBACK_LOOP_DARK: AgentFeedbackLoopPalette = {
  panel: '#0B0B0C',
  groundCenter: '#121214',
  text: '#E8E8E6',
  muted: '#9C9EA1',
  faint: '#6A6C70',
  border: 'rgba(255,255,255,0.28)',
  raised: '#262628',
  raisedLow: '#1A1A1C',
  slab: '#141415',
  slabEdge: '#0E0E0F',
  raisedBorder: 'rgba(255,255,255,0.1)',
  hairline: 'rgba(255,255,255,0.06)',
  recede: '#202022',
  inset: '#151517',
  frost: 'rgba(255,255,255,0.05)',
  glyph: '#B9BBBE',
  glyphStrong: '#D4D5D7',
  glyphSoft: '#7E8084',
  ridge: 'rgba(255,255,255,0.22)',
  lanes: ['#F0A33C', '#46B6DA', '#B9D048', '#8C7BE8', '#E8697C'],
  shadowCore: 'rgba(3,4,2,0.72)',
  focus: '#E8E8E6',
}

/**
 * Light is the reference look: off-white paper, frosted white acrylic tiles
 * and neutral grey glyphs, with the series ramp deepened for a light page. The
 * lanes are fills on white tiles, and each deep lane carries white glyphs at
 * 3:1 or better (amber, the lightest, at 3.19:1; on the bare panel it sits at
 * about 2.85:1). Dark is the same illustration in smoked glass, with the ramp
 * at full brightness.
 */
export const AGENT_FEEDBACK_LOOP_LIGHT: AgentFeedbackLoopPalette = {
  panel: '#F2F2F0',
  groundCenter: '#F7F7F5',
  text: '#2A2B2D',
  muted: '#6B6D70',
  faint: '#9A9C9E',
  border: '#8C8C8C',
  raised: '#FFFFFF',
  raisedLow: '#EDEDEB',
  slab: '#D8D8D5',
  slabEdge: '#C6C6C2',
  raisedBorder: 'rgba(255,255,255,0.95)',
  hairline: 'rgba(0,0,0,0.07)',
  recede: '#F3F3F1',
  inset: '#E9E9E7',
  frost: 'rgba(255,255,255,0.5)',
  glyph: '#55575A',
  glyphStrong: '#3E4043',
  glyphSoft: '#9C9EA0',
  ridge: 'rgba(255,255,255,0.3)',
  lanes: ['#DD7410', '#0E6F91', '#55680F', '#5442B8', '#A8263D'],
  shadowCore: 'rgba(56,58,50,0.2)',
  focus: '#2A2B2D',
}

/** Each palette key and the custom property it travels as. */
const ROLE_VARS: ReadonlyArray<readonly [Exclude<keyof AgentFeedbackLoopPalette, 'lanes'>, string]> = [
  ['panel', 'panel'],
  ['groundCenter', 'ground-center'],
  ['text', 'text'],
  ['muted', 'muted'],
  ['faint', 'faint'],
  ['border', 'border'],
  ['raised', 'raised'],
  ['raisedLow', 'raised-low'],
  ['slab', 'slab'],
  ['slabEdge', 'slab-edge'],
  ['raisedBorder', 'raised-border'],
  ['hairline', 'hairline'],
  ['recede', 'recede'],
  ['inset', 'inset'],
  ['frost', 'frost'],
  ['glyph', 'glyph'],
  ['glyphStrong', 'glyph-strong'],
  ['glyphSoft', 'glyph-soft'],
  ['ridge', 'ridge'],
  ['shadowCore', 'shadow-core'],
  ['focus', 'focus'],
]

/**
 * The class names have to be literal for Tailwind to see them; the values they
 * point at are the two palette objects above, flattened onto the root by
 * paletteVars, so the palette props merge over them with nothing to keep in
 * step and a preview box that sets its own theme moves the whole picture.
 */
const THEME_VARS = [
  '[--fl-panel:var(--fl-panel-l)] dark:[--fl-panel:var(--fl-panel-d)]',
  '[--fl-ground-center:var(--fl-ground-center-l)] dark:[--fl-ground-center:var(--fl-ground-center-d)]',
  '[--fl-text:var(--fl-text-l)] dark:[--fl-text:var(--fl-text-d)]',
  '[--fl-muted:var(--fl-muted-l)] dark:[--fl-muted:var(--fl-muted-d)]',
  '[--fl-faint:var(--fl-faint-l)] dark:[--fl-faint:var(--fl-faint-d)]',
  '[--fl-border:var(--fl-border-l)] dark:[--fl-border:var(--fl-border-d)]',
  '[--fl-raised:var(--fl-raised-l)] dark:[--fl-raised:var(--fl-raised-d)]',
  '[--fl-raised-low:var(--fl-raised-low-l)] dark:[--fl-raised-low:var(--fl-raised-low-d)]',
  '[--fl-slab:var(--fl-slab-l)] dark:[--fl-slab:var(--fl-slab-d)]',
  '[--fl-slab-edge:var(--fl-slab-edge-l)] dark:[--fl-slab-edge:var(--fl-slab-edge-d)]',
  '[--fl-raised-border:var(--fl-raised-border-l)] dark:[--fl-raised-border:var(--fl-raised-border-d)]',
  '[--fl-hairline:var(--fl-hairline-l)] dark:[--fl-hairline:var(--fl-hairline-d)]',
  '[--fl-recede:var(--fl-recede-l)] dark:[--fl-recede:var(--fl-recede-d)]',
  '[--fl-inset:var(--fl-inset-l)] dark:[--fl-inset:var(--fl-inset-d)]',
  '[--fl-frost:var(--fl-frost-l)] dark:[--fl-frost:var(--fl-frost-d)]',
  '[--fl-glyph:var(--fl-glyph-l)] dark:[--fl-glyph:var(--fl-glyph-d)]',
  '[--fl-glyph-strong:var(--fl-glyph-strong-l)] dark:[--fl-glyph-strong:var(--fl-glyph-strong-d)]',
  '[--fl-glyph-soft:var(--fl-glyph-soft-l)] dark:[--fl-glyph-soft:var(--fl-glyph-soft-d)]',
  '[--fl-shadow-core:var(--fl-shadow-core-l)] dark:[--fl-shadow-core:var(--fl-shadow-core-d)]',
  '[--fl-ridge:var(--fl-ridge-l)] dark:[--fl-ridge:var(--fl-ridge-d)]',
  '[--fl-focus:var(--fl-focus-l)] dark:[--fl-focus:var(--fl-focus-d)]',
  '[--fl-lane-0:var(--fl-lane-0-l)] dark:[--fl-lane-0:var(--fl-lane-0-d)]',
  '[--fl-lane-1:var(--fl-lane-1-l)] dark:[--fl-lane-1:var(--fl-lane-1-d)]',
  '[--fl-lane-2:var(--fl-lane-2-l)] dark:[--fl-lane-2:var(--fl-lane-2-d)]',
  '[--fl-lane-3:var(--fl-lane-3-l)] dark:[--fl-lane-3:var(--fl-lane-3-d)]',
  '[--fl-lane-4:var(--fl-lane-4-l)] dark:[--fl-lane-4:var(--fl-lane-4-d)]',
  '[--fl-on-lane-0:var(--fl-on-lane-0-l)] dark:[--fl-on-lane-0:var(--fl-on-lane-0-d)]',
  '[--fl-on-lane-1:var(--fl-on-lane-1-l)] dark:[--fl-on-lane-1:var(--fl-on-lane-1-d)]',
  '[--fl-on-lane-2:var(--fl-on-lane-2-l)] dark:[--fl-on-lane-2:var(--fl-on-lane-2-d)]',
  '[--fl-on-lane-3:var(--fl-on-lane-3-l)] dark:[--fl-on-lane-3:var(--fl-on-lane-3-d)]',
  '[--fl-on-lane-4:var(--fl-on-lane-4-l)] dark:[--fl-on-lane-4:var(--fl-on-lane-4-d)]',
  '[--fl-bounce-op:0] dark:[--fl-bounce-op:1]',
  '[--fl-light:#FFFFFF] [--fl-shade:#000000]',
].join(' ')

/** How many accent slots the drawing uses; a shorter ramp wraps into them. */
const LANE_SLOTS = 5
/** each item's lane slot: Discover and Extract open on the first, the cube returns to it */
const STAGE_LANE = [0, 0, 1, 2, 3, 4, 0] as const

/** relative luminance of a #rgb or #rrggbb colour, or null for anything else */
function luminance(hex: string): number | null {
  const m = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(hex.trim())
  if (!m) return null
  const h = m[1].length === 3 ? m[1].replace(/./g, (ch) => ch + ch) : m[1]
  const ch = [0, 2, 4].map((i) => {
    const c = parseInt(h.slice(i, i + 2), 16) / 255
    return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4
  })
  return 0.2126 * ch[0] + 0.7152 * ch[1] + 0.0722 * ch[2]
}

/** white where it clears 3:1 on the lane, otherwise the palette's darkest ground */
function glyphOn(lane: string, ink: string): string {
  const l = luminance(lane)
  if (l === null) return '#FFFFFF'
  return 1.05 / (l + 0.05) >= 3 ? '#FFFFFF' : ink
}

/** One palette flattened onto the root under its own suffix. */
function paletteVars(p: AgentFeedbackLoopPalette, suffix: 'l' | 'd'): Record<string, string> {
  const v: Record<string, string> = {}
  for (const [key, name] of ROLE_VARS) v[`--fl-${name}-${suffix}`] = p[key]
  const fallback = suffix === 'l' ? AGENT_FEEDBACK_LOOP_LIGHT.lanes : AGENT_FEEDBACK_LOOP_DARK.lanes
  const lanes = p.lanes.length ? p.lanes : fallback
  const lp = luminance(p.panel)
  const lt = luminance(p.text)
  const ink = lp !== null && lt !== null && lt < lp ? p.text : p.panel
  for (let k = 0; k < LANE_SLOTS; k++) {
    const hex = lanes[k % lanes.length]
    v[`--fl-lane-${k}-${suffix}`] = hex
    v[`--fl-on-lane-${k}-${suffix}`] = glyphOn(hex, ink)
  }
  return v
}

const V = (role: string) => `var(--fl-${role})`
const mixCol = (a: string, pct: number, b: string) => `color-mix(in srgb, ${a} ${pct}%, ${b})`
/**
 * One lane and the surfaces mixed from it: a lit top, a shaded side, a soft
 * version sunk toward the tile face for secondary parts, and a glow.
 */
const lane = (k: number) => V(`lane-${k}`)
const laneLit = (k: number) => mixCol(lane(k), 68, V('light'))
const laneDeep = (k: number) => mixCol(lane(k), 78, V('shade'))
const laneSoft = (k: number) => mixCol(lane(k), 50, V('raised'))
const laneSoftLit = (k: number) => mixCol(lane(k), 34, V('raised'))
const laneSoftDeep = (k: number) => mixCol(lane(k), 55, V('slab-edge'))
const laneGlow = (k: number) => mixCol(lane(k), 32, 'transparent')
/** the glyph role on lane k */
const onLane = (k: number) => `on-lane-${k}`
const fill = (role: string): CSSProperties => ({ fill: V(role) })
const stroke = (role: string, width = 2): CSSProperties => ({
  fill: 'none',
  stroke: V(role),
  strokeWidth: width,
  strokeLinecap: 'round',
  strokeLinejoin: 'round',
})

/* ------------------------------------------------------------ stage data */

export const AGENT_FEEDBACK_LOOP_STAGES: AgentFeedbackLoopStage[] = [
  { id: 'discover', label: 'Discover', description: 'Finds sources across the web, docs and files' },
  { id: 'extract', label: 'Extract', description: 'Pulls structured fields out of each document' },
  { id: 'evaluate', label: 'Evaluate', description: 'Scores the results against the goal' },
  { id: 'review', label: 'Review', description: 'A person checks and approves the output' },
  { id: 'activate', label: 'Activate', description: 'Sends the approved result where it is needed' },
  { id: 'learn', label: 'Learn', description: 'Learns from the outcome and improves the next run' },
]

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

/**
 * Every number behind the motion. Exported so the Studio can reach it, and
 * mergeable through the `tuning` prop; the defaults are the tuned piece.
 * Stage timing reads: stage i starts at introDelayMs + i * stageStaggerMs; its
 * shadow spreads first, its layers settle from layerAtMs on, back to front,
 * and its coloured detail pops at badgeAtMs.
 */
export const AGENT_FEEDBACK_LOOP_TUNING = {
  /* layout */
  narrowBreakpoint: 640,

  /* intro */
  introThreshold: 0.35,
  introDelayMs: 200,
  stageStaggerMs: 380,
  shadowMs: 320,
  shadowFrom: 0.6,
  layerAtMs: 120,
  layerStaggerMs: 70,
  /* straight back along the depth axis */
  layerFromX: -12,
  layerFromY: -8,
  fadeMs: 280,
  badgeAtMs: 360,
  popFrom: 0.6,
  barStaggerMs: 80,
  ringInMs: 600,
  ringFromDeg: -120,
  checkDrawMs: 360,
  checkAfterMs: 240,
  stageSettleMs: 1000,
  conduitDrawMs: 240,
  conduitLeadMs: 120,
  loopDrawMs: 1100,
  introTailMs: 400,

  /* springs */
  settleStiffness: 170,
  settleDamping: 24,
  settleMass: 1,
  popStiffness: 320,
  popDamping: 20,
  hoverStiffness: 260,
  hoverDamping: 26,
  calmEase: [0.22, 1, 0.36, 1] as CubicEase,
  drawEase: [0.65, 0, 0.35, 1] as CubicEase,

  /* the relay: a comet runs each conduit, the stage it reaches processes, then emits the next */
  relayTravelMs: 650,
  relayLoopTravelMs: 1400,
  relayProcessMs: 560,
  relayRestMs: 1200,
  /* how far a processing stage plays its hover motion, 0 to 1 */
  relayAmp: 0.5,
  /* the comet: its length in scene units, head and tail widths in screen px per theme */
  cometLength: 34,
  cometHeadDark: 2,
  cometHeadLight: 2.5,
  cometTail: 0.9,
  /* the tint a comet leaves in its conduit, and how long it takes to fade */
  trailOpacity: 0.4,
  trailFadeMs: 1000,
  /* the comet fades out while a stage is hovered, and back in on leave */
  relayPauseFadeMs: 240,
  portFadeMs: 200,
  /* the faint resting flow in every conduit: dash and gap in screen px, and one period */
  flowDashPx: 2,
  flowGapPx: 9,
  flowPeriodMs: 1600,
  glowBlur: 4,

  /* hover */
  hoverLiftPx: 6,
  hoverShadowScale: 1.08,
  hoverShadowOpacity: 0.5,
  hoverFadeMs: 240,
  contactHoverOpacity: 0.35,
  labelInMs: 220,
  labelOutMs: 160,
  labelRisePx: 6,
  dimOpacity: 0.72,
  dimMs: 240,

  /* shadows and depth */
  /* floor shadow: a tight contact layer and a wide ambient one, footprint grown by the spread */
  contactOpacity: 0.9,
  contactSpread: 2,
  contactBlur: 2.5,
  ambientOpacity: 0.6,
  ambientSpread: 8,
  shadowBlurLight: 7,
  shadowBlurDark: 9,
  shadowBlurHoverAdd: 4,
  /* badge shadow on the face behind it */
  raisedBlur: 3,
  raisedContactBlur: 1.25,
  raisedShadowOpacity: 1,
  backLayerOpacity: 0.78,
  deepLayerOpacity: 0.56,

  /* exploded views */
  discoverBackX: -6,
  discoverBackY: -5,
  discoverBackRotate: 0,
  discoverRightX: 5,
  discoverRightY: -3,
  discoverRightRotate: 0,
  discoverFrontX: -2,
  discoverFrontY: 3,
  extractChipX: 8,
  extractStaggerMs: 30,
  extractGlowMs: 240,
  /* depth units each back panel slides away */
  evaluateStep: 6,
  evaluateCheckMs: 320,
  evaluateBarFrom: 0.85,
  reviewBubbleX: 4,
  reviewBubbleY: -4,
  reviewBubbleScale: 1.05,
  activateSendX: 3,
  activateSendY: -3,
  activateSendMs: 180,
  activateRestX: 1.5,
  activateRestY: -1.5,
  activateBackX: -5,
  activateBackY: -3,
  learnBarFrom: 0.4,
  learnBarStaggerMs: 60,
  learnRingMs: 700,
  /* the Feedback cube: its shell lifts, its base settles, its core rises and turns */
  feedbackShellLift: -5,
  feedbackBaseDrop: 2,
  feedbackCoreRise: -4,
  feedbackTurnDeg: 90,
  /* the core drops in from this height at the end of the cube's intro */
  feedbackDropFrom: -12,
}

export type AgentFeedbackLoopTuning = typeof AGENT_FEEDBACK_LOOP_TUNING
type T = AgentFeedbackLoopTuning

const sec = (ms: number) => ms / 1000
const stageAt = (i: number, t: T) => t.introDelayMs + i * t.stageStaggerMs
const loopAt = (t: T) => t.introDelayMs + 6 * t.stageStaggerMs
/** the cube assembles when the loop stroke reaches it */
const cubeIntroAt = (t: T, g: { cubeAt: number }) => loopAt(t) + g.cubeAt * t.loopDrawMs

const settleSpring = (t: T): Transition => ({
  type: 'spring',
  stiffness: t.settleStiffness,
  damping: t.settleDamping,
  mass: t.settleMass,
})
const popSpring = (t: T): Transition => ({ type: 'spring', stiffness: t.popStiffness, damping: t.popDamping })
const hoverSpring = (t: T): Transition => ({ type: 'spring', stiffness: t.hoverStiffness, damping: t.hoverDamping })
const INSTANT: Transition = { duration: 0 }

/* ------------------------------------------------------------- geometry */

/**
 * Both stages share one object drawing, placed per layout. Object coordinates
 * are local to a box of about 144 whose origin is the object's centre; the
 * floor point under that centre sits GROUND below it. The wide stage is one
 * row with the loop underneath, the narrow one a two-column snake. Both are
 * built with their conduits in `layout`, further down, once the ports are known.
 */
type Side = 'l' | 'r'

/**
 * One conduit: it leaves item a's port and plugs into item b's, and the comet
 * running it wears `lane`. `d` is authored in the direction of travel, `tick`
 * is the static notch drawn at the entry port when motion is reduced.
 */
type Hop = {
  a: readonly [number, Side]
  b: readonly [number, Side]
  lane: number
  loop: boolean
  d: string
  tick: string
}

type Geo = {
  vbW: number
  vbH: number
  scale: number
  centres: Pt[]
  /** label top, measured down from each object's centre */
  labelTop: number
  hit: { w: number; h: number; top: number }
  ring: { w: number; h: number; top: number }
  cube: Pt
  caption: Pt & { align: 'center' | 'left' }
  /** how much of the loop's draw time falls before the cube, 0 to 1, and its hit and focus boxes */
  cubeAt: number
  cubeHit: { x: number; y: number; w: number; h: number }
  cubeRing: { x: number; y: number; w: number; h: number }
  /** conduits 0 to 4 join the stages, 5 runs Learn to the cube, 6 the cube back to Discover */
  hops: Hop[]
}

const f = (n: number) => Math.round(n * 100) / 100

const polar = (cx: number, cy: number, r: number, deg: number): Pt => {
  const a = (deg * Math.PI) / 180
  return { x: cx + r * Math.cos(a), y: cy + r * Math.sin(a) }
}

/** an arc running clockwise from a to b degrees, and a solid head at b */
function arcWithHead(cx: number, cy: number, r: number, a: number, b: number): { arc: string; head: string } {
  const p0 = polar(cx, cy, r, a)
  const p1 = polar(cx, cy, r, b)
  const rad = (b * Math.PI) / 180
  const tx = -Math.sin(rad)
  const ty = Math.cos(rad)
  const nx = Math.cos(rad)
  const ny = Math.sin(rad)
  const tip = { x: p1.x + tx * 4, y: p1.y + ty * 4 }
  const l = { x: p1.x - tx * 1.5 + nx * 4, y: p1.y - ty * 1.5 + ny * 4 }
  const rr = { x: p1.x - tx * 1.5 - nx * 4, y: p1.y - ty * 1.5 - ny * 4 }
  return {
    arc: `M${f(p0.x)},${f(p0.y)} A${r},${r} 0 0 1 ${f(p1.x)},${f(p1.y)}`,
    head: `${f(tip.x)},${f(tip.y)} ${f(l.x)},${f(l.y)} ${f(rr.x)},${f(rr.y)}`,
  }
}

/* ----------------------------------------------------------- projection */

/**
 * One orthographic projection for every object and the cube, so all of them
 * share the exact same angle. u runs along the rising axis (right and up), v
 * toward the viewer (right and down), z straight up. The three screen vectors
 * meet the orthographic condition, so a cube drawn with them is a true cube.
 * Each flat part is drawn in its own plane through one matrix, so its glyphs
 * are projected with its face.
 */
const AX = 0.9
const AY = -0.45
const BX = 0.804
const BY = 0.504
/** screen y, measured down from each object's centre, of the floor point under it */
const GROUND = 36

type Mat = readonly [number, number, number, number, number, number]

const iso = (u: number, v: number, z: number): Pt => ({ x: AX * u + BX * v, y: AY * u + BY * v - z + GROUND })
/** the upright plane at depth v: local x runs along u, local y is height measured downward */
const upright = (v: number, g = GROUND): Mat => [AX, AY, 0, 1, BX * v, BY * v + g]
/** the side plane at u, facing down-left: local x runs along v, local y is height measured downward */
const sidePlane = (u: number, g = GROUND): Mat => [BX, BY, 0, 1, AX * u, AY * u + g]
/** the floor plane raised to height z: local x runs along u, local y along v */
const flat = (z: number): Mat => [AX, AY, BX, BY, 0, GROUND - z]
const matrix = (m: Mat) => `matrix(${m.map(f).join(' ')})`
const apply = (m: Mat, p: Pt): Pt => ({ x: m[0] * p.x + m[2] * p.y + m[4], y: m[1] * p.x + m[3] * p.y + m[5] })
/** a depth of d straight back into the picture, as a screen offset */
const backBy = (d: number): Pt => ({ x: -BX * d, y: -BY * d })
/** the same offset in an upright plane's own coordinates */
const backInUpright = (d: number): Pt => {
  const x = (-BX * d) / AX
  return { x, y: -BY * d - AY * x }
}

function roundRectPts(x: number, y: number, w: number, h: number, r: number): Pt[] {
  const rr = Math.min(r, w / 2, h / 2)
  const corners: [number, number, number][] = [
    [x + w - rr, y + rr, -90],
    [x + w - rr, y + h - rr, 0],
    [x + rr, y + h - rr, 90],
    [x + rr, y + rr, 180],
  ]
  const pts: Pt[] = []
  for (const [cx, cy, a0] of corners) for (let k = 0; k <= 6; k++) pts.push(polar(cx, cy, rr, a0 + k * 15))
  return pts
}

const circlePts = (cx: number, cy: number, r: number): Pt[] =>
  Array.from({ length: 36 }, (_, k) => polar(cx, cy, r, k * 10))

function convexHull(pts: Pt[]): Pt[] {
  const p = [...pts].sort((a, b) => a.x - b.x || a.y - b.y)
  const cross = (o: Pt, a: Pt, b: Pt) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x)
  const half = (list: Pt[]) => {
    const out: Pt[] = []
    for (const q of list) {
      while (out.length >= 2 && cross(out[out.length - 2], out[out.length - 1], q) <= 0) out.pop()
      out.push(q)
    }
    return out
  }
  const lower = half(p)
  const upper = half([...p].reverse())
  return lower.slice(0, -1).concat(upper.slice(0, -1))
}

const polyPath = (pts: Pt[]) => `M${pts.map((p) => `${f(p.x)},${f(p.y)}`).join(' L')} Z`

/** the silhouette of a flat shape in plane m swept along the screen offset e */
function sweptHull(m: Mat, local: Pt[], e: Pt): string {
  const face = local.map((p) => apply(m, p))
  return polyPath(convexHull([...face, ...face.map((p) => ({ x: p.x + e.x, y: p.y + e.y }))]))
}

/** the lit 1px bevel along a face's left and top */
function highlightPath(x: number, y: number, w: number, h: number, r: number): string {
  const rr = Math.min(r, w / 2, h / 2)
  const ri = rr - 0.5
  return `M${f(x + 0.5)},${f(y + h - rr)} V${f(y + rr)} A${f(ri)},${f(ri)} 0 0 1 ${f(x + rr)},${f(y + 0.5)} H${f(x + w - rr)}`
}

/* ---------------------------------------------------------- draw context */

type Ids = {
  face: string
  recede: string
  frost: string
  sheen: string
  side: string
  sideFloor: string
  blur: string
  blurHover: string
  blurContact: string
  blurRaised: string
  blurRaisedContact: string
  glow: string
  title: string
  desc: string
}

const IdsContext = createContext<Ids | null>(null)

function useIds(): Ids {
  const ids = useContext(IdsContext)
  if (!ids) throw new Error('AgentFeedbackLoop drawing used outside its svg')
  return ids
}

/**
 * What one object needs to know to move. `built` turns true when the intro
 * begins (every delay is measured from there), `settled` once this stage has
 * finished assembling, and `live` while it is hovered, focused or tapped and
 * allowed to explode.
 */
type ObjCtx = {
  intro: boolean
  built: boolean
  settled: boolean
  live: boolean
  still: boolean
  at: number
  /** the lane slot this item's coloured parts take */
  lane: number
  /** item index, 0 to 6 */
  index: number
  /** true while the relay has this item processing: its hover motion plays at `amp` */
  busy: boolean
  /** live or busy, and how far the motion plays (1 hovered, relayAmp processing) */
  act: boolean
  amp: number
  /** the lane each port is lit in right now, or null */
  ports: Record<Side, number | null>
  t: T
}

type Pose = { x?: number; y?: number; rotate?: number; scale?: number }

/** a pose played at a fraction of its full reach */
const scalePose = (p: Pose, a: number): Pose => ({
  x: (p.x ?? 0) * a,
  y: (p.y ?? 0) * a,
  rotate: (p.rotate ?? 0) * a,
  scale: 1 + ((p.scale ?? 1) - 1) * a,
})

/* Layer: the component in block 4, "### Layer(props)" */

/* ------------------------------------------------------------ primitives */

type Tone = 'face' | 'recede' | 'accent' | 'soft'

/** a flat shape in plane coordinates: a rounded rectangle, a disc, or a polygon */
type Shape = { x: number; y: number; w: number; h: number; r: number; disc: boolean; pts?: Pt[] }

const shapePts = (s: Shape): Pt[] =>
  s.pts ?? (s.disc ? circlePts(s.x + s.w / 2, s.y + s.h / 2, s.w / 2) : roundRectPts(s.x, s.y, s.w, s.h, s.r))

function drawShape(s: Shape, style: CSSProperties, dy = 0, grow = 0, filter?: string, clipPath?: string) {
  if (s.pts) {
    return (
      <polygon
        points={s.pts.map((p) => `${f(p.x)},${f(p.y + dy)}`).join(' ')}
        filter={filter}
        clipPath={clipPath}
        style={style}
      />
    )
  }
  if (s.disc) {
    return (
      <circle
        cx={s.x + s.w / 2}
        cy={s.y + s.h / 2 + dy}
        r={s.w / 2 + grow}
        filter={filter}
        clipPath={clipPath}
        style={style}
      />
    )
  }
  return (
    <rect
      x={s.x - grow}
      y={s.y + dy - grow}
      width={s.w + 2 * grow}
      height={s.h + 2 * grow}
      rx={Math.min(s.r, s.w / 2, s.h / 2) + grow}
      filter={filter}
      clipPath={clipPath}
      style={style}
    />
  )
}

type ShapeProps = { x?: number; y?: number; w?: number; h?: number; r?: number; disc?: boolean; pts?: Pt[] }

/* Slab: the component in block 4, "### Slab(props)" */

/* CastShadow: the component in block 4, "### CastShadow(props)" */

/**
 * Every item's two ports, the sockets its conduits plug into. A card's port
 * is a nub on its left or right edge, in the card's own plane, on the part
 * that stays put in the exploded view where there is one; Learn's sit on its
 * tray's two visible sides, the cube's on its two visible faces. Ports never
 * take part in the exploded view: they lift only with the whole object, and
 * a short plug line keeps each one joined to its conduit while it does. `at` is
 * the plane's depth (v, or u for a side plane), x and y the nub's centre in
 * that plane. `g` is the plane's ground: the cube is drawn about its centre.
 */
type PortDef = { plane: 'up' | 'side'; at: number; x: number; y: number; g?: number }
const PORTS: ReadonlyArray<Record<Side, PortDef>> = [
  { l: { plane: 'up', at: 14, x: -35, y: -44 }, r: { plane: 'up', at: 14, x: 29, y: -22 } },
  { l: { plane: 'up', at: 0, x: -51, y: -50 }, r: { plane: 'up', at: 0, x: 57, y: -15 } },
  { l: { plane: 'up', at: 10, x: -39, y: -50 }, r: { plane: 'up', at: 16, x: 39, y: -18 } },
  { l: { plane: 'up', at: 0, x: -49, y: -50 }, r: { plane: 'up', at: 0, x: 53, y: -10 } },
  { l: { plane: 'up', at: 8, x: -45, y: -51 }, r: { plane: 'up', at: 8, x: 41, y: -14 } },
  { l: { plane: 'side', at: -36, x: -14, y: -7.5 }, r: { plane: 'up', at: 30, x: 24, y: -7.5 } },
  { l: { plane: 'side', at: -11, x: -7, y: 4, g: 0 }, r: { plane: 'up', at: 11, x: 7, y: 4, g: 0 } },
]

const portMat = (p: PortDef): Mat => (p.plane === 'up' ? upright(p.at, p.g) : sidePlane(p.at, p.g))

/** a port's socket in scene space, for an item drawn at `centre` and `scale` */
function portAt(centre: Pt, scale: number, p: PortDef): Pt {
  const q = apply(portMat(p), { x: p.x, y: p.y })
  return { x: centre.x + scale * q.x, y: centre.y + scale * q.y }
}

/* PortNub: the component in block 4, "### PortNub(props)" */

/* Ports: the component in block 4, "### Ports(props)" */

/* Ln: the component in block 4, "### Ln(props)" */

/* Discover: the component in block 4, "### Discover(props)" */

/* Extract: the component in block 4, "### Extract(props)" */

/* Evaluate: the component in block 4, "### Evaluate(props)" */

/* Review: the component in block 4, "### Review(props)" */

/* Activate: the component in block 4, "### Activate(props)" */

type BarTone = 'light' | 'mid' | 'accent'

/** a bar's lit top, lit left side and shaded front, the coloured ones mixed from lane k */
function barFaces(tone: BarTone, k: number): { top: string; left: string; front: string } {
  if (tone === 'accent') return { top: laneLit(k), left: lane(k), front: laneDeep(k) }
  if (tone === 'mid') return { top: laneSoftLit(k), left: laneSoft(k), front: laneSoftDeep(k) }
  return { top: V('raised'), left: V('raised-low'), front: V('slab') }
}

/* IsoBar: the component in block 4, "### IsoBar(props)" */

/* RefreshRing: the component in block 4, "### RefreshRing(props)" */

/* Learn: the component in block 4, "### Learn(props)" */

const OBJECTS = [Discover, Extract, Evaluate, Review, Activate, Learn] as const

/**
 * Each object's footprint on the floor, as [u, v, length along u, depth along v]:
 * `contact` for the parts resting near the floor, `ambient` for everything
 * overhead. The light is straight above, so a shadow is its footprint.
 */
type Foot = readonly [number, number, number, number]
const FEET: ReadonlyArray<{ contact: Foot[]; ambient: Foot[] }> = [
  {
    contact: [[-34, 0, 64, 24]],
    ambient: [
      [-46, -19, 62, 5],
      [20, -5, 40, 5],
      [-34, 0, 64, 24],
    ],
  },
  {
    contact: [
      [-48, -6, 52, 6],
      [26, -9, 28, 9],
    ],
    ambient: [
      [-48, -6, 52, 6],
      [26, -9, 28, 9],
    ],
  },
  {
    contact: [
      [-36, -19, 64, 5],
      [-36, -7, 64, 5],
      [-36, 5, 64, 5],
      [12, 11, 24, 5],
    ],
    ambient: [
      [-36, -19, 64, 29],
      [12, 11, 24, 5],
    ],
  },
  {
    contact: [[-46, -7, 96, 7]],
    ambient: [
      [-46, -7, 96, 7],
      [12, 3, 36, 5],
    ],
  },
  {
    contact: [
      [-46, -20, 80, 4],
      [-46, -8, 80, 4],
      [-42, 2, 80, 6],
    ],
    ambient: [[-46, -20, 84, 28]],
  },
  {
    contact: [[-34, -28, 68, 56]],
    ambient: [[-34, -28, 68, 56]],
  },
]

/** footprints grown by `spread`, rounded, projected onto the floor as one path */
function feetPath(feet: readonly Foot[], spread: number, floorY: number): string {
  const m: Mat = [AX, AY, BX, BY, 0, floorY]
  return feet
    .map(([u, v, w, h]) =>
      polyPath(roundRectPts(u - spread, v - spread, w + 2 * spread, h + 2 * spread, spread + 3).map((p) => apply(m, p))),
    )
    .join(' ')
}

/* ------------------------------------------------------------ conduits */

/** a conduit between two ports at about the same height: a soft S with level ends */
function curve(p: Pt, q: Pt): string {
  if (Math.abs(p.y - q.y) < 0.5) return `M${f(p.x)},${f(p.y)} H${f(q.x)}`
  const mx = (p.x + q.x) / 2
  return `M${f(p.x)},${f(p.y)} C${f(mx)},${f(p.y)} ${f(mx)},${f(q.y)} ${f(q.x)},${f(q.y)}`
}

/** a conduit along a polyline, every corner rounded to at most r */
function ortho(pts: Pt[], r: number): string {
  let d = `M${f(pts[0].x)},${f(pts[0].y)}`
  for (let i = 1; i < pts.length - 1; i++) {
    const a = pts[i - 1]
    const p = pts[i]
    const b = pts[i + 1]
    const la = Math.hypot(p.x - a.x, p.y - a.y) || 1
    const lb = Math.hypot(b.x - p.x, b.y - p.y) || 1
    const rr = Math.min(r, la / 2, lb / 2)
    const s0 = { x: p.x - ((p.x - a.x) / la) * rr, y: p.y - ((p.y - a.y) / la) * rr }
    const s1 = { x: p.x + ((b.x - p.x) / lb) * rr, y: p.y + ((b.y - p.y) / lb) * rr }
    d += ` L${f(s0.x)},${f(s0.y)} Q${f(p.x)},${f(p.y)} ${f(s1.x)},${f(s1.y)}`
  }
  const z = pts[pts.length - 1]
  return `${d} L${f(z.x)},${f(z.y)}`
}

/** the static notch across a conduit just before it enters its port */
function tickAt(q: Pt, prev: Pt): string {
  const len = Math.hypot(q.x - prev.x, q.y - prev.y) || 1
  const dx = (q.x - prev.x) / len
  const dy = (q.y - prev.y) / len
  const c = { x: q.x - dx * 8, y: q.y - dy * 8 }
  return `M${f(c.x - dy * 3.5)},${f(c.y + dx * 3.5)} L${f(c.x + dy * 3.5)},${f(c.y - dx * 3.5)}`
}

/** the lane each conduit's comet wears: the colour of the stage it hands to, rose on into the cube */
const HOP_LANE = [0, 1, 2, 3, 4, 4, 0] as const
/** the comet is drawn as this many tapering pieces plus a soft halo on its head */
const COMET_SEGMENTS = 6

type HopSpec = { a: readonly [number, Side]; b: readonly [number, Side]; via?: (p: Pt, q: Pt) => Pt[] }

function layout(base: Omit<Geo, 'hops'>, specs: HopSpec[], r: number): Geo {
  const at = (i: number) => (i === 6 ? base.cube : base.centres[i])
  const hops = specs.map((h, k): Hop => {
    const p = portAt(at(h.a[0]), base.scale, PORTS[h.a[0]][h.a[1]])
    const q = portAt(at(h.b[0]), base.scale, PORTS[h.b[0]][h.b[1]])
    const pts = h.via?.(p, q)
    return {
      a: h.a,
      b: h.b,
      lane: HOP_LANE[k],
      loop: k >= 5,
      d: pts ? ortho(pts, r) : curve(p, q),
      tick: tickAt(q, pts ? pts[pts.length - 2] : { x: p.x, y: q.y }),
    }
  })
  return { ...base, hops }
}

/**
 * The wide stage is one row: five short conduits left to right, then the
 * loop leaves Learn's tray downward, runs left into the cube, and leaves the
 * cube up the left edge into Discover's front card.
 */
const WIDE = layout(
  {
    vbW: 1100,
    vbH: 388,
    scale: 1.25,
    centres: [100, 286, 472, 658, 844, 1030].map((x) => ({ x, y: 128 })),
    labelTop: 104,
    hit: { w: 180, h: 236, top: -100 },
    ring: { w: 172, h: 228, top: -96 },
    cube: { x: 565, y: 302.5 },
    caption: { x: 565, y: 350, align: 'center' },
    cubeAt: 0.46,
    cubeHit: { x: 523, y: 272, w: 84, h: 112 },
    cubeRing: { x: 535, y: 276, w: 60, h: 104 },
  },
  [
    { a: [0, 'r'], b: [1, 'l'] },
    { a: [1, 'r'], b: [2, 'l'] },
    { a: [2, 'r'], b: [3, 'l'] },
    { a: [3, 'r'], b: [4, 'l'] },
    { a: [4, 'r'], b: [5, 'l'] },
    { a: [5, 'r'], b: [6, 'r'], via: (p, q) => [p, { x: p.x, y: q.y }, q] },
    { a: [6, 'l'], b: [0, 'l'], via: (p, q) => [p, { x: 16, y: p.y }, { x: 16, y: q.y }, q] },
  ],
  32,
)

/**
 * The narrow stage is a two-column snake: row hops run straight across, the
 * row changes run down the outer edge, and the loop leaves Learn downward,
 * runs left along the bottom into the cube, and climbs the left edge into
 * Discover.
 */
const NARROW = layout(
  {
    vbW: 360,
    vbH: 864,
    scale: 1.05,
    centres: [
      { x: 96, y: 110 },
      { x: 292, y: 110 },
      { x: 292, y: 380 },
      { x: 96, y: 380 },
      { x: 96, y: 650 },
      { x: 292, y: 650 },
    ],
    labelTop: 84,
    hit: { w: 170, h: 216, top: -96 },
    ring: { w: 162, h: 208, top: -92 },
    cube: { x: 180, y: 795 },
    caption: { x: 180, y: 830, align: 'center' },
    cubeAt: 0.25,
    cubeHit: { x: 150, y: 772, w: 60, h: 84 },
    cubeRing: { x: 152, y: 774, w: 56, h: 80 },
  },
  [
    { a: [0, 'r'], b: [1, 'l'] },
    { a: [1, 'r'], b: [2, 'r'], via: (p, q) => [p, { x: 354, y: p.y }, { x: 354, y: q.y }, q] },
    { a: [2, 'l'], b: [3, 'r'] },
    { a: [3, 'l'], b: [4, 'l'], via: (p, q) => [p, { x: 26, y: p.y }, { x: 26, y: q.y }, q] },
    { a: [4, 'r'], b: [5, 'l'] },
    { a: [5, 'r'], b: [6, 'r'], via: (p, q) => [p, { x: p.x, y: q.y }, q] },
    { a: [6, 'l'], b: [0, 'l'], via: (p, q) => [p, { x: 12, y: p.y }, { x: 12, y: q.y }, q] },
  ],
  12,
)

/* FloorShadow: the component in block 4, "### FloorShadow(props)" */

/** half size of the glass box, the coloured core and its height, and where the milky band ends */
const CUBE_S = 9
const CORE_H = 5.5
const CORE_Z: readonly [number, number] = [-3, 8]
const BAND_TOP = -1
/** the cube's floor sits this far below its centre, a few units under the box */
const CUBE_FLOOR = CUBE_S + 3
const CUBE_FEET: { contact: Foot[]; ambient: Foot[] } = {
  contact: [[-CUBE_S, -CUBE_S, 2 * CUBE_S, 2 * CUBE_S]],
  ambient: [[-5, -5, 10, 10]],
}

/** a point of the cube, in its own space: origin at the box centre */
const cubeP = (u: number, v: number, z: number) => `${f(AX * u + BX * v)},${f(AY * u + BY * v - z)}`

function boxFaces(h: number, z0: number, z1: number) {
  return {
    top: [cubeP(-h, -h, z1), cubeP(h, -h, z1), cubeP(h, h, z1), cubeP(-h, h, z1)].join(' '),
    left: [cubeP(-h, -h, z0), cubeP(-h, h, z0), cubeP(-h, h, z1), cubeP(-h, -h, z1)].join(' '),
    right: [cubeP(-h, h, z0), cubeP(h, h, z0), cubeP(h, h, z1), cubeP(-h, h, z1)].join(' '),
  }
}

/**
 * The coloured core turned `deg` about its vertical axis. A side shows when its
 * normal faces the viewer (the projection's view direction on the floor is
 * -0.893 u + v), and it is lit by how far it faces the light's side, so the
 * shading reads like the Learn bars at every angle.
 */
function coreGeometry(deg: number) {
  const a = (deg * Math.PI) / 180
  const cs = Math.cos(a)
  const sn = Math.sin(a)
  const c = [
    [-1, -1],
    [1, -1],
    [1, 1],
    [-1, 1],
  ].map(([x, y]) => ({ u: CORE_H * (x * cs - y * sn), v: CORE_H * (x * sn + y * cs) }))
  const [z0, z1] = CORE_Z
  const sides = c.map((p, k) => {
    const q = c[(k + 1) % 4]
    const mu = (p.u + q.u) / 2
    const mv = (p.v + q.v) / 2
    const len = Math.hypot(mu, mv) || 1
    const nu = mu / len
    const nv = mv / len
    const seen = nv - 0.893 * nu > 0.001
    return {
      pts: seen ? [cubeP(p.u, p.v, z0), cubeP(q.u, q.v, z0), cubeP(q.u, q.v, z1), cubeP(p.u, p.v, z1)].join(' ') : '',
      lit: seen ? Math.max(0, -nu) : 0,
    }
  })
  return { sides, top: c.map((p) => cubeP(p.u, p.v, z1)).join(' ') }
}

/* CoreSide: the component in block 4, "### CoreSide(props)" */

/* CoreTop: the component in block 4, "### CoreTop(props)" */

/* Feedback: the component in block 4, "### Feedback(props)" */

/* Defs: the component in block 4, "### Defs(props)" */

/* ----------------------------------------------------------------- props */

/**
 * @typedef {object} AgentFeedbackLoopProps
 * @property {AgentFeedbackLoopStage[]} [stages=AGENT_FEEDBACK_LOOP_STAGES] The six stage names and descriptions, left to right. The drawings stay tied to position, so entry 0 is always the card fan and entry 5 the tray.
 * @property {string} [feedbackLabel='Feedback'] The caption under the cube on the loop.
 * @property {'hover' | 'always'} [labels='hover'] Whether stage names appear on hover, focus and tap, or stay visible. On narrow screens they are always visible.
 * @property {boolean} [ambient=true] Whether the relay runs after the intro: a comet travels each conduit in turn, the stage it reaches briefly plays its motion and emits the next comet in its own colour, then the loop carries it through the cube back to Discover and the cycle repeats. Hovering a stage, or holding one with activeStage, pauses it. Off, the conduits are still, with a small notch at each entry port showing the direction, as with reduced motion.
 * @property {boolean} [playIntro=true] Whether the stages assemble one by one when the picture first scrolls into view. Off, it opens composed.
 * @property {(id: string, index: number) => void} [onStageClick] Called with the stage id and index on click, Enter or Space, and on a second tap on touch. The Feedback cube is a seventh button and calls it with 'feedback' and 6.
 * @property {(id: string | null, index: number) => void} [onStageHover] Called with the stage id and index on pointer enter, focus or a first tap, and with null and -1 when it ends. The Feedback cube reports 'feedback' and 6.
 * @property {string} [activeStage] Id of a stage to hold in its hovered state, or 'feedback' for the cube. Hovering another stage still previews it.
 * @property {Partial<AgentFeedbackLoopPalette>} [paletteDark] Colour overrides for the dark theme, merged over AGENT_FEEDBACK_LOOP_DARK. `lanes` (string[]) is the series ramp: Discover and Extract take lane 1, Evaluate to Learn lanes 2 to 5, the Feedback cube lane 1 again; a shorter ramp wraps. Every coloured surface, glow, badge shadow and the travelling light is mixed from it, and glyphs on a lane switch to the dark ground where white would fall under 3:1.
 * @property {Partial<AgentFeedbackLoopPalette>} [paletteLight] Colour overrides for the light theme, merged over AGENT_FEEDBACK_LOOP_LIGHT, `lanes` included. The light lanes are fills on white tiles: keep each deep enough that white glyphs on it clear 3:1 (the default amber does at 3.19:1), or the glyphs switch to the dark ground.
 * @property {Partial<AgentFeedbackLoopTuning>} [tuning] Timing and motion overrides, merged over AGENT_FEEDBACK_LOOP_TUNING.
 * @property {string} [className] Extra classes merged onto the root element.
 */
export type AgentFeedbackLoopProps = {
  stages?: AgentFeedbackLoopStage[]
  feedbackLabel?: string
  labels?: 'hover' | 'always'
  ambient?: boolean
  playIntro?: boolean
  onStageClick?: (id: string, index: number) => void
  onStageHover?: (id: string | null, index: number) => void
  activeStage?: string
  paletteDark?: Partial<AgentFeedbackLoopPalette>
  paletteLight?: Partial<AgentFeedbackLoopPalette>
  tuning?: Partial<AgentFeedbackLoopTuning>
  className?: string
}

const STYLES = `
.afl-stage { outline: none; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.afl-stage:focus { outline: none; }
.afl-ring { opacity: 0; transition: opacity 120ms linear; }
.afl-stage:focus-visible .afl-ring { opacity: 1; }
`

const noSubscribe = () => () => {}
```
````

## Related

- [Agent Cost Flow](https://aicanvas.me/components/agent-cost-flow.md): 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.
- [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.
- [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.
- [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.
- [Upload Progress](https://aicanvas.me/components/upload-progress.md): A collapsible file upload widget with a shimmer progress bar (indigo while uploading, amber on pause). Expand to see per-file rows with live progress, time remaining, and pause, resume, refresh, and stop controls.
