{
  "$schema": "https://zoblocks.design/schema/registry-item.json",
  "name": "copilot",
  "type": "zoblocks:component",
  "title": "Copilot",
  "description": "Model-agnostic clinical assistant with mode-level scope contracts, inline source attribution, a deterministic crisis interrupt, and FHIR audit output. The engine and the accessibility behaviour ship as npm packages; this item is the Tailwind skin over them.",
  "categories": [
    "patterns",
    "ai"
  ],
  "dependencies": [
    "clsx",
    "tailwind-merge",
    "@zoblocks/copilot-core",
    "@zoblocks/copilot-react"
  ],
  "registryDependencies": [
    "utils",
    "tokens"
  ],
  "files": [
    {
      "path": "registry/zoblocks/copilot/copilot.tsx",
      "type": "zoblocks:component",
      "target": "components/zoblocks/copilot.tsx",
      "content": "\"use client\";\n\n/**\n * Copilot — a floating clinical copilot, as copy-as-source.\n *\n * This is the second skin over `@zoblocks/copilot-react`. The antd skin\n * (`@zoblocks/copilot`) is the first. Both are thin: the engine, the\n * safety pipeline, the streaming announcement strategy, the combobox keyboard\n * model and the citation segmentation all live in the two npm packages, so the\n * two skins cannot drift on any of the parts where drifting would be an\n * accessibility or safety defect.\n *\n * That split is what lets this component stay copyable. A registry item that\n * had to carry the crisis classifier and the audit mapping inline would be\n * thousands of lines nobody would read before pasting, and a safety control\n * nobody reads is not a safety control.\n *\n * Three props are worth reading before you use it:\n *\n *   `suppressed` — removes the component entirely. Set it while the clinician\n *   is administering medication or signing orders. Interruptions during those\n *   tasks measurably increase clinical errors, and the FDA's 2026 CDS guidance\n *   moved time-critical use under the criterion about independent review for\n *   the same reason. The most valuable thing this component does is disappear.\n *\n *   `context` — the resolver. Without one, only modes that read nothing will\n *   run. That is deliberate: reference lookup is the safest first deployment\n *   and needs no BAA covering patient data in the model call.\n *\n *   `provider` — targets *your* endpoint, never a model vendor. There is no\n *   apiKey prop and no way to add one.\n */\n\nimport * as React from \"react\";\nimport {\n  ChevronDownIcon,\n  CloseIcon,\n  CopyIcon,\n  InsertIcon,\n  LockIcon,\n  MicIcon,\n  NewChatIcon,\n  PersonIcon,\n  SendIcon,\n  ShortcutIcon,\n  SparkIcon,\n  StopIcon,\n  ThoughtIcon,\n  ThumbDownIcon,\n  ThumbUpIcon,\n  VerifyIcon,\n  CopilotAnswerRegion,\n  CopilotDockRegion,\n  CopilotLiveRegion,\n  CopilotRoot,\n  citationLabel,\n  modeIcon,\n  segmentAnswer,\n  useCopilot,\n  useShortcutMenu,\n  useSummonShortcut,\n  type CopilotShortcut,\n  type UseCopilotOptions,\n} from \"@zoblocks/copilot-react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface CopilotProps extends UseCopilotOptions {\n  /**\n   * Prompts offered before the reader types. They are starting points, not commands — each\n   * still runs through its mode's contract.\n   */\n  shortcuts?: readonly CopilotShortcut[];\n  /**\n   * ARIA role for the dock. Defaults to `complementary`, so the copilot is findable and\n   * skippable rather than an unnamed div.\n   */\n  role?: string;\n  /** Where the dock sits. `inline` renders it in flow for a surface that has its own layout. */\n  anchor?: \"bottom-center\" | \"bottom-right\" | \"inline\";\n  /** Applied to the dock's outer element. */\n  className?: string;\n  /**\n   * Called when the reader chooses to put an answer into the note they are writing. Receives\n   * text; the host owns where it lands and what provenance it carries.\n   */\n  onInsert?: (text: string) => void;\n  /**\n   * Called when a safety classifier escalates. The host runs its own protocol — this component\n   * never decides what happens next in a crisis.\n   */\n  onRiskProtocol?: () => void;\n}\n\nexport function Copilot({\n  shortcuts = [],\n  role,\n  anchor = \"bottom-center\",\n  className,\n  onInsert,\n  onRiskProtocol,\n  ...options\n}: CopilotProps) {\n  const api = useCopilot(options);\n  const [trayOpen, setTrayOpen] = React.useState(false);\n  const fieldRef = React.useRef<HTMLInputElement>(null);\n\n  const menu = useShortcutMenu({\n    shortcuts,\n    draft: api.draft,\n    ...(role !== undefined ? { role } : {}),\n    onSelect: (shortcut) => {\n      if (shortcut.modeId) api.setMode(shortcut.modeId);\n      api.setDraft(shortcut.question);\n    },\n    onEscape: () => setTrayOpen(false),\n  });\n\n  useSummonShortcut({ onSummon: () => fieldRef.current?.focus() });\n\n  const threadOpen = api.state.messages.length > 0 || api.state.status === \"crisis\";\n\n  // Collapsed, this is one button. A clinician who puts the assistant away has\n  // to find it put away — anything that re-expands on its own teaches them that\n  // dismissing it does not work.\n  if (api.collapsed) {\n    return (\n      <CopilotRoot api={api}>\n        <div\n          data-zb-copilot=\"\"\n          data-zb-collapsed=\"\"\n          className={cn(\n            \"z-[900] flex justify-end\",\n            anchor !== \"inline\" &&\n              \"fixed inset-x-0 bottom-0 px-4 pb-[max(1rem,env(safe-area-inset-bottom))]\",\n            \"pointer-events-none [&>*]:pointer-events-auto\",\n            className,\n          )}\n        >\n          <CopilotLiveRegion message={api.announcer.message} />\n          <button\n            type=\"button\"\n            aria-label=\"Reopen the assistant\"\n            onClick={() => api.setCollapsed(false)}\n            className=\"grid h-12 w-12 place-items-center rounded-full bg-[var(--zb-accent)] text-[var(--zb-text-on-accent)] shadow-lg\"\n          >\n            <SparkIcon />\n          </button>\n        </div>\n      </CopilotRoot>\n    );\n  }\n\n  return (\n    <CopilotRoot api={api}>\n      <div\n        data-zb-copilot=\"\"\n        data-zb-status={api.state.status}\n        className={cn(\n          \"z-[900] flex flex-col gap-2\",\n          // The dock centres; the thread docks to a side. A dock is a summons\n          // bar and belongs where the eye already is, but a thread is something\n          // you read *against* the record — centred, it sits on top of the\n          // chart being discussed, and the first thing anyone does is drag it\n          // out of the way.\n          threadOpen ? \"items-start\" : \"items-center\",\n          anchor !== \"inline\" &&\n            \"fixed inset-x-0 bottom-0 px-4 pb-[max(1rem,env(safe-area-inset-bottom))]\",\n          anchor === \"bottom-right\" && \"items-end\",\n          \"pointer-events-none [&>*]:pointer-events-auto\",\n          className,\n        )}\n      >\n        <CopilotLiveRegion message={api.announcer.message} />\n\n        {threadOpen ? (\n          <CopilotPanel\n            api={api}\n            {...(onInsert ? { onInsert } : {})}\n            {...(onRiskProtocol ? { onRiskProtocol } : {})}\n          />\n        ) : (\n          <CopilotDockRegion className=\"flex w-full max-w-2xl flex-col gap-1.5\">\n            {trayOpen ? (\n              <div className=\"rounded-2xl border border-[var(--zb-border)] bg-[var(--zb-surface)] p-3 shadow-lg\">\n                <div role=\"group\" aria-label=\"Change mode\" className=\"mb-2 flex flex-wrap gap-1.5\">\n                  {api.modes.map((mode) => (\n                    <button\n                      key={mode.id}\n                      type=\"button\"\n                      aria-pressed={mode.id === api.mode.id}\n                      onClick={() => api.setMode(mode.id)}\n                      className={cn(\n                        \"min-h-6 rounded-lg px-2.5 py-1 text-sm\",\n                        mode.id === api.mode.id\n                          ? \"bg-[var(--zb-accent)] text-[var(--zb-text-on-accent)]\"\n                          : \"border border-[var(--zb-border)] text-[var(--zb-text)]\",\n                      )}\n                    >\n                      {mode.label}\n                    </button>\n                  ))}\n                </div>\n                <ul className=\"m-0 list-none p-0\">\n                  {api.mode.suggestions.map((suggestion) => (\n                    <li\n                      key={suggestion.id}\n                      className=\"border-t border-[var(--zb-border)] first:border-t-0\"\n                    >\n                      <button\n                        type=\"button\"\n                        className=\"w-full px-1 py-2 text-start\"\n                        onClick={() => {\n                          api.setDraft(suggestion.question ?? suggestion.label);\n                          setTrayOpen(false);\n                        }}\n                      >\n                        <span className=\"block text-sm text-[var(--zb-text)]\">\n                          {suggestion.label}\n                        </span>\n                        {/* The scope, shown at the point of choice. */}\n                        {suggestion.reads ? (\n                          <span className=\"block text-xs text-[var(--zb-text-muted)]\">\n                            {suggestion.reads}\n                          </span>\n                        ) : null}\n                      </button>\n                    </li>\n                  ))}\n                </ul>\n              </div>\n            ) : null}\n\n            {menu.open ? (\n              <ul\n                {...menu.listProps}\n                aria-label=\"Shortcuts\"\n                className=\"m-0 list-none rounded-2xl border border-[var(--zb-border)] bg-[var(--zb-surface)] p-2 shadow-lg\"\n              >\n                {menu.items.map((item, index) => (\n                  <li\n                    key={item.id}\n                    {...menu.optionProps(index)}\n                    className=\"cursor-pointer rounded-lg px-2 py-1.5 text-sm aria-selected:bg-[color-mix(in_oklab,var(--zb-accent)_12%,transparent)]\"\n                  >\n                    {item.label}\n                  </li>\n                ))}\n              </ul>\n            ) : null}\n\n            <ScopeStrip api={api} onChange={() => setTrayOpen(true)} />\n\n            <div className=\"flex items-center gap-1.5 rounded-2xl border border-[var(--zb-border)] bg-[var(--zb-surface)] px-2 py-1.5 shadow-[0_1px_2px_rgb(0_0_0/0.05),0_12px_32px_-14px_rgb(0_0_0/0.22)]\">\n              {/* The square glyph. Also the \"/\" affordance, so the shortcut\n                menu has a visible way in for anyone who never learns the key. */}\n              <button\n                type=\"button\"\n                aria-label=\"Shortcuts\"\n                onClick={() => api.setDraft(api.draft.startsWith(\"/\") ? api.draft : \"/\")}\n                className=\"grid size-8 shrink-0 place-items-center rounded-[10px] border border-[var(--zb-border)] bg-[var(--zb-bg-subtle)] text-base text-[var(--zb-text-muted)]\"\n              >\n                <ShortcutIcon />\n              </button>\n\n              <input\n                ref={fieldRef}\n                value={api.draft}\n                aria-label=\"Clinical assistant\"\n                placeholder={\n                  api.mode.reads.length > 0 ? \"Ask about this patient…\" : \"Search the evidence…\"\n                }\n                onChange={(event) => api.setDraft(event.target.value)}\n                onKeyDown={(event) => {\n                  menu.inputProps.onKeyDown(event);\n                  if (event.key === \"Enter\" && !menu.open) {\n                    event.preventDefault();\n                    void api.submit();\n                  }\n                }}\n                role={menu.inputProps.role}\n                aria-expanded={menu.inputProps[\"aria-expanded\"]}\n                aria-controls={menu.inputProps[\"aria-controls\"]}\n                aria-autocomplete={menu.inputProps[\"aria-autocomplete\"]}\n                {...(menu.inputProps[\"aria-activedescendant\"]\n                  ? { \"aria-activedescendant\": menu.inputProps[\"aria-activedescendant\"] }\n                  : {})}\n                className=\"min-w-0 flex-1 bg-transparent px-1 text-[var(--zb-text)] outline-none placeholder:text-[var(--zb-text-muted)] [@media(pointer:coarse)]:text-[length:max(16px,1em)]\"\n              />\n\n              {(() => {\n                const ModeGlyph = modeIcon(api.mode.id);\n                return (\n                  <button\n                    type=\"button\"\n                    aria-expanded={trayOpen}\n                    onClick={() => setTrayOpen((open) => !open)}\n                    className=\"inline-flex min-h-8 shrink-0 items-center gap-1.5 rounded-[10px] border border-[var(--zb-border)] bg-[var(--zb-bg-subtle)] px-2.5 py-1 text-sm text-[var(--zb-text)]\"\n                  >\n                    <ModeGlyph className=\"text-[var(--zb-text-muted)]\" />\n                    {api.mode.label}\n                  </button>\n                );\n              })()}\n\n              {api.dictation.active ? (\n                <button\n                  type=\"button\"\n                  aria-label=\"Stop dictation\"\n                  onClick={() => api.dictation.stop()}\n                  className=\"min-h-6 min-w-6 rounded-lg px-2 text-[var(--zb-status-normal)]\"\n                >\n                  <span className=\"inline-flex gap-[2px]\" aria-hidden=\"true\">\n                    <i className=\"block h-3 w-[2px] animate-pulse rounded bg-current\" />\n                    <i className=\"block h-2 w-[2px] animate-pulse rounded bg-current [animation-delay:120ms]\" />\n                    <i className=\"block h-3.5 w-[2px] animate-pulse rounded bg-current [animation-delay:240ms]\" />\n                  </span>\n                  {/* Colour and motion alone would be a 1.4.1 failure, and in a\n                    consulting room it is also a privacy problem. */}\n                  <span className=\"sr-only\">Microphone is live</span>\n                </button>\n              ) : (\n                <button\n                  type=\"button\"\n                  aria-label=\"Start dictation\"\n                  onClick={api.dictation.start}\n                  disabled={api.state.status === \"streaming\" || api.state.status === \"submitting\"}\n                  className=\"grid size-8 shrink-0 place-items-center rounded-[10px] text-base text-[var(--zb-text-muted)] disabled:opacity-40\"\n                >\n                  <MicIcon />\n                </button>\n              )}\n\n              {api.state.status === \"streaming\" || api.state.status === \"submitting\" ? (\n                <button\n                  type=\"button\"\n                  onClick={api.stop}\n                  className=\"grid size-8 shrink-0 place-items-center rounded-[10px] border border-[var(--zb-border)] text-base\"\n                >\n                  <StopIcon />\n                  <span className=\"sr-only\">Stop</span>\n                </button>\n              ) : (\n                <button\n                  type=\"button\"\n                  disabled={!api.canSubmit}\n                  onClick={() => void api.submit()}\n                  className=\"grid size-8 shrink-0 place-items-center rounded-[10px] bg-[var(--zb-accent)] text-base text-[var(--zb-text-on-accent)] disabled:opacity-40\"\n                >\n                  <SendIcon />\n                  <span className=\"sr-only\">Send</span>\n                </button>\n              )}\n            </div>\n\n            {/* Never dismissible. */}\n            <p className=\"m-0 text-center text-xs text-[var(--zb-text-muted)]\">\n              Medical knowledge only. Not for autonomous decision making. Check sources and use your\n              clinical judgement.\n            </p>\n          </CopilotDockRegion>\n        )}\n      </div>\n    </CopilotRoot>\n  );\n}\n\n/**\n * The scope strip.\n *\n * The highest-value element on the surface, and the one that is easiest to\n * leave out. A clinician needs to know what the assistant can see *before*\n * they trust a summary, and — the part everyone omits — what it could not see.\n * A summary that silently excludes a protected record has created a false\n * belief that would not exist if the tool did not exist.\n */\nfunction ScopeStrip({\n  api,\n  onChange,\n}: {\n  api: ReturnType<typeof useCopilot>;\n  onChange: () => void;\n}) {\n  const { scope } = api;\n\n  // \"Change\" opens the mode tray rather than a scope picker of its own. Scope\n  // *is* the mode's `reads` contract, so a second control that edited it\n  // independently would let the two disagree — and the strip is the thing a\n  // clinician is being asked to trust.\n  const change = (\n    <button\n      type=\"button\"\n      onClick={onChange}\n      className=\"rounded-full font-medium text-[var(--zb-accent)] underline underline-offset-2\"\n    >\n      Change\n    </button>\n  );\n\n  // A pill that sizes to its own text, not a full-width bar. The strip is a\n  // statement about *this* answer, and a bar spanning the dock reads as chrome —\n  // which is exactly the thing people stop seeing.\n  const shell =\n    // `rounded-2xl` rather than a full pill: the category list is host-supplied\n    // and a pill that wraps to three lines reads as a rendering fault.\n    \"m-0 mx-auto flex w-fit max-w-full flex-wrap items-center justify-center gap-x-2 gap-y-1 rounded-2xl border border-[var(--zb-border)] bg-[var(--zb-surface)] px-3 py-1 text-xs text-[var(--zb-text-muted)] shadow-[0_1px_2px_rgb(0_0_0/0.04)]\";\n\n  if (scope.categories.length === 0) {\n    return (\n      <p className={shell}>\n        <LockIcon className=\"shrink-0\" />\n        No patient data is being used\n        {change}\n      </p>\n    );\n  }\n\n  return (\n    <p className={shell}>\n      <PersonIcon className=\"shrink-0\" />\n      <span>\n        Reading{\" \"}\n        <strong className=\"font-semibold text-[var(--zb-text)]\">\n          {scope.subject?.display ?? scope.subject?.reference}\n        </strong>\n        {\" · \"}\n        {scope.categories.join(\", \")}\n      </span>\n      {scope.showWithheld ? (\n        <span className=\"inline-flex items-center gap-1 rounded-full bg-[color-mix(in_oklab,var(--zb-status-high)_20%,transparent)] px-2 py-px text-[var(--zb-text)]\">\n          <LockIcon className=\"shrink-0\" />\n          {scope.withheldCount} record{scope.withheldCount === 1 ? \"\" : \"s\"} withheld\n        </span>\n      ) : null}\n      {change}\n    </p>\n  );\n}\n\n/**\n * A retrieved passage with the supporting clause marked.\n *\n * This is the difference between a citation and a verification. A source title\n * tells a clinician where an answer came from; it does not tell them the model\n * read that source correctly, and checking costs a tab, a search and a scan of\n * a forty-page guideline. Nobody pays that during a consultation — which is the\n * mechanism behind automation bias, an effort asymmetry rather than a character\n * flaw.\n *\n * Marking the clause makes the check a glance. `<mark>` rather than a styled\n * span because the semantics are exactly right and it survives forced-colours\n * mode, where a background tint would vanish and take the meaning with it.\n *\n * Offsets are host-supplied and clamped rather than trusted: a provider that\n * returns a stale range should mark the wrong words at worst, never throw away\n * the passage.\n */\nexport interface HighlightedPassageProps {\n  /** The source passage, verbatim. Never re-flowed or truncated — a citation the reader cannot check against the original is not a citation. */\n  text: string;\n  /**\n   * Character offsets of the span the answer drew on, as `[start, end]`.\n   *\n   * Out-of-range offsets highlight nothing rather than throwing: a citation\n   * that points past the end of its own source is a bug in the caller, and\n   * silently marking the wrong sentence would be worse than marking none.\n   */\n  at?: readonly [number, number];\n}\n\nexport function HighlightedPassage({ text, at }: HighlightedPassageProps) {\n  if (!at) return <>{text}</>;\n\n  const start = Math.max(0, Math.min(at[0], text.length));\n  const end = Math.max(start, Math.min(at[1], text.length));\n  if (start === end) return <>{text}</>;\n\n  return (\n    <>\n      {text.slice(0, start)}\n      <mark className=\"rounded bg-[color-mix(in_oklab,var(--zb-status-high)_28%,transparent)] px-0.5 text-[var(--zb-text)]\">\n        {text.slice(start, end)}\n      </mark>\n      {text.slice(end)}\n    </>\n  );\n}\n\nfunction CopilotPanel({\n  api,\n  onInsert,\n  onRiskProtocol,\n}: {\n  api: ReturnType<typeof useCopilot>;\n  onInsert?: (text: string) => void;\n  onRiskProtocol?: () => void;\n}) {\n  const modeSelectRef = React.useRef<HTMLSelectElement>(null);\n\n  return (\n    <section\n      aria-label=\"Assistant conversation\"\n      // A column, not a banner. The record stays readable beside it, which is\n      // the whole reason a clinician opened a thread about that record.\n      className=\"flex max-h-[min(78dvh,44rem)] w-full max-w-md flex-col overflow-hidden rounded-2xl border border-[var(--zb-border)] bg-[var(--zb-surface)] shadow-[0_1px_2px_rgb(0_0_0/0.05),0_16px_40px_-16px_rgb(0_0_0/0.28)]\"\n    >\n      <header className=\"flex items-center justify-between border-b border-[var(--zb-border)] px-3 py-2\">\n        {api.threads.length > 1 ? (\n          <label className=\"flex min-w-0 items-center gap-1\">\n            <span className=\"sr-only\">Switch conversation</span>\n            <select\n              value={api.activeThreadId}\n              onChange={(event) => api.switchThread(event.target.value)}\n              className=\"min-w-0 max-w-[18rem] truncate bg-transparent text-sm font-semibold text-[var(--zb-text)] outline-none\"\n            >\n              {api.threads.map((thread) => (\n                <option key={thread.id} value={thread.id}>\n                  {thread.title}\n                </option>\n              ))}\n            </select>\n            <ChevronDownIcon />\n          </label>\n        ) : (\n          <h2 className=\"m-0 truncate text-sm font-semibold text-[var(--zb-text)]\">\n            {api.threads[0]?.title ?? api.mode.label}\n          </h2>\n        )}\n        <button\n          type=\"button\"\n          onClick={api.newThread}\n          className=\"min-h-6 min-w-6 shrink-0 rounded-lg border border-[var(--zb-border)] px-2 py-0.5 text-xs\"\n        >\n          <NewChatIcon />\n          <span className=\"sr-only\">New chat</span>\n        </button>\n        <button\n          type=\"button\"\n          aria-label=\"Put the assistant away\"\n          onClick={() => api.setCollapsed(true)}\n          className=\"min-h-6 min-w-6 rounded-lg px-1\"\n        >\n          <CloseIcon />\n        </button>\n      </header>\n\n      <div className=\"flex flex-1 flex-col gap-3 overflow-y-auto p-3\">\n        {api.state.messages.map((message) =>\n          message.role === \"clinician\" ? (\n            <p\n              key={message.id}\n              className=\"m-0 max-w-[80%] self-end rounded-xl bg-[color-mix(in_oklab,var(--zb-accent)_12%,transparent)] px-3 py-1.5 text-sm text-[var(--zb-text)]\"\n            >\n              {message.text}\n            </p>\n          ) : (\n            <article key={message.id} className=\"flex flex-col gap-1.5\">\n              {/* Collapsed by design: a visible chain of thought reads as\n                  evidence to a clinician, and it is not evidence. */}\n              {message.answer?.reasoning ? (\n                <details className=\"text-xs text-[var(--zb-text-muted)]\">\n                  <summary className=\"flex cursor-pointer items-center gap-1 text-[0.65rem] uppercase tracking-wide [&::-webkit-details-marker]:hidden\">\n                    <ThoughtIcon /> Reasoning\n                  </summary>\n                  <p className=\"m-0 mt-1 border-s-2 border-[var(--zb-border)] ps-2 leading-relaxed\">\n                    {message.answer.reasoning}\n                  </p>\n                </details>\n              ) : null}\n\n              {message.answer ? (\n                <span\n                  data-register={message.answer.register}\n                  className={cn(\n                    \"self-start rounded px-1.5 py-0.5 text-[0.65rem] uppercase tracking-wide\",\n                    // The status token's own paired ground, not a 15% mix of\n                    // the hue over whatever is behind it: on the docs panel the\n                    // mix measured 4.43:1 for this 10px label. The `-bg` tokens\n                    // are what the token build gates each status colour against.\n                    message.answer.register === \"grounded\" &&\n                      \"bg-[var(--zb-status-normal-bg)] text-[var(--zb-status-normal)]\",\n                    message.answer.register === \"general\" &&\n                      \"bg-[var(--zb-status-high-bg)] text-[var(--zb-status-high)]\",\n                    message.answer.register === \"declined\" &&\n                      \"bg-[var(--zb-bg-subtle)] text-[var(--zb-text-muted)]\",\n                  )}\n                >\n                  {message.answer.register === \"grounded\"\n                    ? `Grounded · ${message.answer.sources.size}`\n                    : message.answer.register === \"general\"\n                      ? \"General knowledge\"\n                      : \"Declined\"}\n                </span>\n              ) : null}\n\n              {message.answer ? (\n                <div\n                  data-register={message.answer.register}\n                  className={cn(\n                    \"text-sm leading-relaxed text-[var(--zb-text)]\",\n                    message.answer.register === \"general\" &&\n                      \"border-s-[3px] border-[var(--zb-status-high)] ps-2.5\",\n                  )}\n                >\n                  {segmentAnswer(message.answer).map((segment, index) => (\n                    <span\n                      key={index}\n                      className={cn(\n                        segment.uncited &&\n                          \"underline decoration-[var(--zb-status-high)] decoration-wavy underline-offset-4\",\n                      )}\n                      {...(segment.uncited\n                        ? {\n                            title:\n                              \"No source was cited for this sentence. Verify it before acting.\",\n                          }\n                        : {})}\n                    >\n                      {segment.text}\n                      {segment.markers.map((marker) => (\n                        <button\n                          key={marker}\n                          type=\"button\"\n                          onClick={() => api.openSourcesFor(message.id)}\n                          aria-label={citationLabel(marker, message.answer?.sources.get(marker))}\n                          className=\"ms-px align-super rounded bg-[color-mix(in_oklab,var(--zb-accent)_15%,transparent)] px-1 text-[0.65em]\"\n                        >\n                          {marker}\n                        </button>\n                      ))}\n                    </span>\n                  ))}\n                </div>\n              ) : null}\n\n              <div className=\"flex flex-wrap items-center gap-2 text-xs\">\n                <button\n                  type=\"button\"\n                  aria-label=\"Helpful\"\n                  onClick={() => api.sendFeedbackFor(message.id, \"up\")}\n                  className=\"min-h-6 min-w-6 px-1\"\n                >\n                  <ThumbUpIcon />\n                </button>\n                <button\n                  type=\"button\"\n                  aria-label=\"Not helpful\"\n                  // No reason picker here, so record the thumbs-down at once.\n                  onClick={() =>\n                    api.sendFeedbackFor(message.id, \"down\", undefined, { askReason: false })\n                  }\n                  className=\"min-h-6 min-w-6 px-1\"\n                >\n                  <ThumbDownIcon />\n                </button>\n                <button\n                  type=\"button\"\n                  aria-label=\"Copy\"\n                  onClick={() => void navigator.clipboard?.writeText(message.text)}\n                  className=\"min-h-6 min-w-6 px-1\"\n                >\n                  <CopyIcon />\n                </button>\n                <button\n                  type=\"button\"\n                  // This answer's sources, not the latest answer's.\n                  onClick={() => api.openSourcesFor(message.id)}\n                  className=\"inline-flex min-h-6 items-center gap-1 rounded-lg border border-[var(--zb-border)] px-2 py-0.5\"\n                >\n                  <VerifyIcon />\n                  Show sources\n                </button>\n                {onInsert ? (\n                  <button\n                    type=\"button\"\n                    aria-label=\"Insert into note\"\n                    onClick={() => onInsert(message.text)}\n                    className=\"min-h-6 min-w-6 px-1\"\n                  >\n                    <InsertIcon />\n                  </button>\n                ) : null}\n              </div>\n            </article>\n          ),\n        )}\n\n        {api.state.status === \"streaming\" && api.state.current ? (\n          <CopilotAnswerRegion busy className=\"text-sm leading-relaxed text-[var(--zb-text)]\">\n            {api.state.current.text}\n          </CopilotAnswerRegion>\n        ) : null}\n\n        {api.state.status === \"crisis\" ? (\n          <section\n            role=\"alert\"\n            className=\"rounded-xl border border-[color-mix(in_oklab,var(--zb-status-critical)_45%,transparent)] border-s-[3px] border-s-[var(--zb-status-critical)] bg-[color-mix(in_oklab,var(--zb-status-critical)_6%,transparent)] p-3\"\n          >\n            <h3 className=\"m-0 mb-1 text-sm font-semibold text-[var(--zb-status-critical)]\">\n              This needs a person, not a model\n            </h3>\n            <p className=\"m-0 mb-2 text-sm text-[var(--zb-text)]\">\n              {api.state.safety.crisis.audience === \"user\"\n                ? \"This assistant is not able to help with this. Please reach a person now.\"\n                : \"This assistant does not answer questions about a patient in immediate danger.\"}\n            </p>\n            <div className=\"flex flex-wrap gap-2\">\n              {onRiskProtocol ? (\n                <button\n                  type=\"button\"\n                  onClick={onRiskProtocol}\n                  className=\"min-h-6 rounded-lg bg-[var(--zb-status-critical)] px-2.5 py-1 text-sm text-[var(--zb-text-on-accent)]\"\n                >\n                  Open risk protocol\n                </button>\n              ) : null}\n              {/* Resolved by locale. 988 works in the United States and nowhere\n                  else, so hardcoding it ships a bug to every other market. */}\n              {api.crisisLines.map((line) =>\n                line.number ? (\n                  <a\n                    key={line.label}\n                    href={`tel:${line.number}`}\n                    className=\"min-h-6 rounded-lg border border-[var(--zb-border)] px-2.5 py-1 text-sm\"\n                  >\n                    {line.label} · {line.number}\n                  </a>\n                ) : (\n                  <span\n                    key={line.label}\n                    className=\"px-2.5 py-1 text-sm text-[var(--zb-text-muted)]\"\n                  >\n                    {line.label}\n                  </span>\n                ),\n              )}\n            </div>\n          </section>\n        ) : null}\n\n        {api.state.status === \"refused\" && api.state.error ? (\n          <div role=\"status\" className=\"rounded-lg bg-[var(--zb-bg-subtle)] p-2.5 text-sm\">\n            <strong className=\"text-[var(--zb-text)]\">Outside what this assistant answers</strong>\n            <p className=\"m-0 text-[var(--zb-text-muted)]\">{api.state.error.message}</p>\n          </div>\n        ) : null}\n\n        {api.state.status === \"error\" && api.state.error ? (\n          <div\n            role=\"alert\"\n            className=\"rounded-lg bg-[color-mix(in_oklab,var(--zb-status-critical)_8%,transparent)] p-2.5 text-sm\"\n          >\n            <strong className=\"text-[var(--zb-text)]\">The assistant could not answer</strong>\n            <p className=\"m-0 text-[var(--zb-text-muted)]\">{api.state.error.message}</p>\n            {api.state.error.retryable && api.canRetry ? (\n              <button\n                type=\"button\"\n                onClick={() => void api.retry()}\n                className=\"min-h-6 rounded-lg border border-[var(--zb-border)] px-2 py-0.5\"\n              >\n                Try again\n              </button>\n            ) : null}\n          </div>\n        ) : null}\n      </div>\n\n      {/* The verification surface. The passage itself, not a link — following a\n          link costs a tab and a search, and people accept rather than pay it. */}\n      {api.sourcesOpen ? (\n        <section\n          aria-label=\"Basis of this answer\"\n          className=\"max-h-72 overflow-y-auto border-t border-[var(--zb-border)] p-3\"\n        >\n          <div className=\"mb-1 flex items-center justify-between\">\n            <strong className=\"text-sm text-[var(--zb-text)]\">Basis of this answer</strong>\n            <button\n              type=\"button\"\n              aria-label=\"Close\"\n              onClick={api.closeSources}\n              className=\"min-h-6 px-1\"\n            >\n              ✕\n            </button>\n          </div>\n          <ol className=\"m-0 list-none p-0\">\n            {/* Numbered by citation marker, so \"2\" here is \"2\" in the answer. */}\n            {api.citations.map(({ marker, source }) => (\n              <li\n                key={source.id}\n                className=\"border-t border-[var(--zb-border)] pt-2 first:border-t-0 first:pt-0\"\n              >\n                <div className=\"flex items-baseline gap-1.5 text-sm\">\n                  <span\n                    aria-hidden=\"true\"\n                    className=\"rounded bg-[color-mix(in_oklab,var(--zb-accent)_15%,transparent)] px-1 text-xs\"\n                  >\n                    {marker}\n                  </span>\n                  {source.url ? (\n                    <a\n                      href={source.url}\n                      target=\"_blank\"\n                      rel=\"noreferrer noopener\"\n                      className=\"text-[var(--zb-text)]\"\n                    >\n                      {source.title}\n                    </a>\n                  ) : (\n                    <strong className=\"text-[var(--zb-text)]\">{source.title}</strong>\n                  )}\n                </div>\n                <blockquote className=\"m-0 mt-1 border-s-2 border-[var(--zb-border)] ps-2 text-xs text-[var(--zb-text-muted)]\">\n                  <HighlightedPassage\n                    text={source.passage}\n                    {...(source.highlight ? { at: source.highlight } : {})}\n                  />\n                </blockquote>\n                <p className=\"m-0 mt-1 text-[0.7rem] tabular-nums text-[var(--zb-text-muted)]\">\n                  Retrieved {source.retrievedAt.slice(0, 10)}\n                  {source.version ? ` · ${source.version}` : \"\"} · {source.kind}\n                  {typeof source.score === \"number\" ? ` · match ${source.score.toFixed(2)}` : \"\"}\n                </p>\n              </li>\n            ))}\n          </ol>\n        </section>\n      ) : null}\n\n      <ScopeStrip api={api} onChange={() => modeSelectRef.current?.focus()} />\n\n      {/* The panel's own composer. Once the thread is open the dock is the wrong\n          place to type: the answer being followed up on is up here, and making\n          someone travel back down to ask about it is the kind of small friction\n          that ends in a copy-paste into a chat window with none of this\n          component's guarantees. */}\n      <div className=\"border-t border-[var(--zb-border)] px-3 py-2\">\n        <input\n          value={api.draft}\n          aria-label=\"Ask a follow-up…\"\n          placeholder=\"Ask a follow-up…\"\n          disabled={api.state.status === \"crisis\"}\n          onChange={(event) => api.setDraft(event.target.value)}\n          onKeyDown={(event) => {\n            if (event.key === \"Enter\") {\n              event.preventDefault();\n              void api.submit();\n            }\n          }}\n          className=\"w-full bg-transparent text-sm text-[var(--zb-text)] outline-none [@media(pointer:coarse)]:text-[length:max(16px,1em)]\"\n        />\n        <div className=\"mt-1 flex items-center gap-1\">\n          <button\n            type=\"button\"\n            aria-label=\"Shortcuts\"\n            onClick={() => api.setDraft(api.draft.startsWith(\"/\") ? api.draft : \"/\")}\n            className=\"min-h-6 min-w-6 rounded-lg px-1\"\n          >\n            <ShortcutIcon />\n          </button>\n\n          {/* Mode has to survive the dock being hidden, or opening a thread\n              would quietly remove the control that decides what the model may\n              read — the one thing that must never be implicit. */}\n          <label className=\"inline-flex items-center gap-1 rounded-lg border border-[var(--zb-border)] px-2 py-0.5 text-xs\">\n            <span className=\"sr-only\">Change mode</span>\n            <select\n              ref={modeSelectRef}\n              value={api.mode.id}\n              onChange={(event) => api.setMode(event.target.value)}\n              className=\"bg-transparent text-xs text-[var(--zb-text)] outline-none\"\n            >\n              {api.modes.map((mode) => (\n                <option key={mode.id} value={mode.id}>\n                  {mode.label}\n                </option>\n              ))}\n            </select>\n          </label>\n\n          <span className=\"flex-1\" />\n\n          {api.dictation.active ? (\n            <button\n              type=\"button\"\n              aria-label=\"Stop dictation\"\n              onClick={() => api.dictation.stop()}\n              className=\"min-h-6 min-w-6 rounded-lg px-1 text-[var(--zb-status-normal)]\"\n            >\n              <MicIcon />\n              <span className=\"sr-only\">Microphone is live</span>\n            </button>\n          ) : (\n            <button\n              type=\"button\"\n              aria-label=\"Start dictation\"\n              onClick={api.dictation.start}\n              // Disabled mid-answer: dictating would leave `streaming` and remove Stop.\n              disabled={api.state.status === \"streaming\" || api.state.status === \"submitting\"}\n              className=\"min-h-6 min-w-6 rounded-lg px-1 disabled:opacity-40\"\n            >\n              <MicIcon />\n            </button>\n          )}\n\n          {api.state.status === \"streaming\" || api.state.status === \"submitting\" ? (\n            <button\n              type=\"button\"\n              aria-label=\"Stop\"\n              onClick={api.stop}\n              className=\"min-h-6 min-w-6 rounded-lg border border-[var(--zb-border)] px-1\"\n            >\n              <StopIcon />\n            </button>\n          ) : (\n            <button\n              type=\"button\"\n              aria-label=\"Send\"\n              disabled={!api.canSubmit}\n              onClick={() => void api.submit()}\n              className=\"min-h-6 min-w-6 rounded-lg bg-[var(--zb-accent)] px-1 text-[var(--zb-text-on-accent)] disabled:opacity-40\"\n            >\n              <SendIcon />\n            </button>\n          )}\n        </div>\n      </div>\n\n      <p className=\"m-0 border-t border-[var(--zb-border)] px-3 py-2 text-center text-xs text-[var(--zb-text-muted)]\">\n        Medical knowledge only. Not for autonomous decision making. Check sources and use your\n        clinical judgement.\n      </p>\n    </section>\n  );\n}\n"
    }
  ]
}
