/*
 * /code thread — port of assistant-ui's Thread element anatomy
 * (docs/adr/0004-port-assistant-ui-elements.md).
 *
 * Loads AFTER code.css and owns everything inside .code-thread-root:
 * the viewport + reading column, welcome state, user bubbles, bare
 * assistant prose, ToolFallback / ToolGroup disclosures, Reasoning,
 * the message footer (action bar + timing), the composer with its rail,
 * suggestion chips, the scroll-to-bottom button and the shimmer utility.
 *
 * Tokens only (tokens.css). assistant-ui → ours:
 *   background → --color-bg      card → --color-bg-2
 *   muted      → --code-muted    muted-foreground → --color-ink-3
 *   border/60  → --code-hairline live (blue-500) → --color-focus
 */

.code-thread-root {
  --thread-max-width: 44rem;
  --composer-bg: var(--color-bg-2);
  --composer-radius: 1.5rem;
  --composer-padding: 8px;
  /* --code-muted / --code-hairline / --code-live / --aui-* come from .code-shell (code.css) */

  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}
/* ─── Stale build notice ──────────────────────────────────────────────── */

.code-stale {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  margin: var(--space-2) auto 0;
  width: calc(100% - 2 * var(--space-4));
  max-width: var(--thread-max-width);
  padding: 6px 6px 6px 14px;
  border: 1px solid var(--code-hairline);
  border-radius: 999px;
  background: var(--color-bg-2);
  color: var(--color-ink-2);
  font-size: var(--text-sm);
  animation: aui-in 200ms var(--ease) both;
  flex-shrink: 0;
}
.code-stale-btn {
  height: 28px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  background: var(--color-ink);
  color: var(--color-bg);
  font: 500 var(--text-sm) var(--font-sans);
  cursor: pointer;
}
.code-stale-btn:hover { opacity: 0.9; }
.code-thread-root.is-loading .code-thread-footer {
  display: none;
}

/* Thread loading overlay: takes over the content area while history loads. */
.code-thread-loading {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: color-mix(in srgb, var(--color-bg) 72%, transparent);
  backdrop-filter: blur(10px) saturate(1.1);
  -webkit-backdrop-filter: blur(10px) saturate(1.1);
  animation: code-thread-loading-in 180ms var(--ease) both;
}
.code-thread-loading-card {
  width: min(420px, 100%);
  padding: 28px 28px 24px;
  border: 1px solid var(--code-hairline);
  border-radius: 24px;
  background: var(--color-bg-2);
  box-shadow: 0 24px 64px oklch(0% 0 0 / 0.22);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
.code-thread-loading-orb {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 24px;
}
.code-thread-loading-orb span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-ink-3);
  animation: code-thread-loading-pulse 1.1s var(--ease) infinite;
}
.code-thread-loading-orb span:nth-child(2) { animation-delay: 150ms; }
.code-thread-loading-orb span:nth-child(3) { animation-delay: 300ms; }
.code-thread-loading-title {
  font: 500 var(--text-sm) var(--font-sans);
  letter-spacing: 0.02em;
  color: var(--color-ink-2);
}
.code-thread-loading-title::after {
  content: "";
  animation: code-thread-loading-dots 1.2s steps(4, end) infinite;
}
.code-thread-loading-skel {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.code-thread-loading-row {
  height: 12px;
  border-radius: 999px;
  background: linear-gradient(
    100deg,
    var(--code-muted) 30%,
    var(--color-border-2) 50%,
    var(--code-muted) 70%
  );
  background-size: 200% 100%;
  animation: code-thread-loading-shimmer 1.4s linear infinite;
}
.code-thread-loading-row.is-a { width: 42%; }
.code-thread-loading-row.is-b { width: 92%; }
.code-thread-loading-row.is-c { width: 76%; }
.code-thread-loading-row.is-d { width: 61%; }
@keyframes code-thread-loading-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes code-thread-loading-pulse {
  0%, 100% { opacity: 0.25; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1.15); }
}
@keyframes code-thread-loading-shimmer {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}
@keyframes code-thread-loading-dots {
  0% { content: ""; }
  25% { content: "."; }
  50% { content: ".."; }
  75% { content: "..."; }
}

/* ─── Viewport ────────────────────────────────────────────────────────── */

.code-transcript-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.code-transcript {
  /* room the last assistant row leaves free ≈ topbar + anchor + composer */
  --code-anchor-reserve: 360px;

  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  overflow-anchor: none; /* the JS scroller owns anchoring */
  scrollbar-width: thin;
  scrollbar-color: var(--color-border-2) transparent;
  scroll-behavior: auto;
  mask-image: linear-gradient(to bottom, transparent 0, black 20px, black 100%);
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, black 20px, black 100%);
}
.code-transcript::-webkit-scrollbar { width: 8px; }
.code-transcript::-webkit-scrollbar-thumb {
  background: var(--color-border-2);
  border-radius: 4px;
}

.code-transcript-inner {
  width: 100%;
  max-width: var(--thread-max-width);
  margin: 0 auto;
  min-height: 100%;
  padding: var(--space-4) var(--space-4) var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-6);
}

/* Anchoring reserve: the last assistant row keeps near-viewport height so a
   fresh turn can pin its anchor to the top while the reply streams in. */
.code-transcript-inner > .code-row-assistant:last-child {
  min-height: calc(100dvh - var(--code-anchor-reserve, 360px));
}

/* ─── Welcome (new chat) ──────────────────────────────────────────────── */

.code-container.is-empty .code-thread-root {
  justify-content: center;
}
.code-container.is-empty .code-transcript-wrap {
  flex: 0 0 auto;
}
.code-container.is-empty .code-transcript {
  mask-image: none;
  -webkit-mask-image: none;
  overflow: visible;
}
.code-container.is-empty .code-transcript-inner {
  min-height: 0;
  padding-bottom: 0;
}
.code-container.is-empty .code-thread-footer {
  padding-top: var(--space-2);
}

.code-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: var(--space-2);
  padding: 0 var(--space-4) var(--space-4);
}
.code-welcome-title {
  margin: 0;
  font-size: var(--text-xl);
  font-weight: 500;
  letter-spacing: -0.02em;
  color: var(--color-ink);
  animation: aui-in 200ms var(--ease) both;
}
.code-welcome-sub {
  margin: 0;
  font-size: var(--text-sm);
  color: var(--color-ink-3);
  animation: aui-in 200ms var(--ease) both;
  animation-delay: 60ms;
}

/* ─── Rows ────────────────────────────────────────────────────────────── */

.code-row {
  animation: aui-in 150ms var(--ease) both;
  min-width: 0;
}

/* User: two-column grid, bubble in the end column (assistant-ui UserMessage) */
.code-row-user {
  display: grid;
  grid-template-columns: minmax(72px, 1fr) auto;
  align-content: start;
  gap: var(--space-2) 0;
  padding: 0 var(--space-2);
}
.code-row-user > * { grid-column: 2; }
.code-user-bubble {
  min-width: 0;
  max-width: 100%;
  background: var(--code-muted);
  color: var(--color-ink);
  border-radius: 14px;
  padding: var(--space-2) var(--space-4);
  overflow-wrap: anywhere;
}
.code-user-text {
  margin: 0;
  font: 400 var(--text-base) var(--font-sans);
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}
.code-msg-images {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.code-msg-image {
  max-width: 200px;
  max-height: 200px;
  border-radius: 10px;
  border: 1px solid var(--code-hairline);
  object-fit: contain;
  cursor: zoom-in;
}

/* Assistant: bare prose */
.code-row-assistant {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.code-assistant-content {
  padding: 0 var(--space-2);
  color: var(--color-ink);
  line-height: 1.625;
  overflow-wrap: anywhere;
  min-width: 0;
}
.code-parts {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  min-width: 0;
}
.code-parts:empty { display: none; }

/* ─── Thinking indicator ──────────────────────────────────────────────── */

.code-thinking {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: var(--text-sm);
  color: var(--color-ink-3);
  padding: 4px 0;
}
.code-thinking-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--code-live);
  animation: aui-pulse 1.4s var(--ease) infinite;
  flex-shrink: 0;
}
.code-thinking-elapsed {
  font: 400 11px var(--font-mono);
  letter-spacing: -0.01em;
  color: var(--color-ink-4);
  font-variant-numeric: tabular-nums;
}

/* ─── Message footer: action bar + timing ─────────────────────────────── */

.code-msg-footer {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 30px;
  padding: 6px var(--space-2) 0;
  margin-inline-start: -2px;
}
.code-action-bar {
  display: flex;
  align-items: center;
  gap: 2px;
  color: var(--color-ink-3);
  opacity: 0;
  transition: opacity var(--dur) var(--ease);
}
.code-row-assistant.is-last .code-action-bar,
.code-row-assistant:hover .code-action-bar,
.code-row-assistant:focus-within .code-action-bar {
  opacity: 1;
}
.code-turn-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  font: 400 11px var(--font-mono);
  letter-spacing: -0.01em;
  color: var(--color-ink-4);
  min-width: 0;
  font-variant-numeric: tabular-nums;
}
.code-turn-meta > span { white-space: nowrap; }
.code-turn-meta-sep { opacity: 0.6; }

/* Round ghost icon button (assistant-ui TooltipIconButton / ghostButton) */
.code-icon-btn {
  display: inline-grid;
  place-items: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: color-mix(in oklch, var(--color-ink) 50%, transparent);
  cursor: pointer;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease), scale 150ms var(--ease);
}
.code-icon-btn:hover:not(:disabled) {
  background: var(--code-muted);
  color: var(--color-ink);
}
.code-icon-btn:active:not(:disabled) { scale: 0.96; }
.code-icon-btn:focus-visible {
  outline: none;
  box-shadow: 0 0 0 1px color-mix(in oklch, var(--color-ink) 20%, transparent);
}
.code-icon-btn:disabled { opacity: 0.3; cursor: default; }
.code-icon-btn > svg { grid-area: 1 / 1; }

/* Icon swap (copy ↔ check, arrow ↔ square) */
.code-icon-swap {
  grid-area: 1 / 1;
  transition: opacity var(--aui-dur) var(--ease), scale var(--aui-dur) var(--ease), filter var(--aui-dur) var(--ease);
}
.code-icon-check,
.code-icon-stop {
  opacity: 0;
  scale: 0.25;
  filter: blur(4px);
}
.is-copied .code-icon-copy,
.is-stop .code-icon-send {
  opacity: 0;
  scale: 0.25;
  filter: blur(4px);
}
.is-copied .code-icon-check,
.is-stop .code-icon-stop {
  opacity: 1;
  scale: 1;
  filter: blur(0);
}
.code-icon-btn.is-copied { color: oklch(72% 0.16 145); }

/* ─── Error banner ────────────────────────────────────────────────────── */

.code-msg-error {
  margin-top: var(--space-2);
  padding: var(--space-3);
  border: 1px solid var(--color-error);
  background: color-mix(in oklch, var(--color-error) 10%, transparent);
  color: var(--color-error);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  line-height: 1.5;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

/* ─── Disclosure trigger shared bits (tool, group, reasoning) ─────────── */

.code-chevron {
  flex-shrink: 0;
  width: 16px;
  height: 16px;
  rotate: -90deg;
  transition: rotate var(--aui-dur) var(--aui-ease);
}
.is-open > button > .code-chevron { rotate: 0deg; }

.code-loader {
  flex-shrink: 0;
  animation: aui-spin 0.6s linear infinite;
}

/* ─── ToolFallback ────────────────────────────────────────────────────── */

.code-tool { width: 100%; min-width: 0; }

.code-tool-trigger {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  max-width: 100%;
  min-width: 0;
  padding: 6px 0;
  border: 0;
  background: transparent;
  color: var(--color-ink-3);
  font: 400 var(--text-sm) var(--font-sans);
  text-align: left;
  cursor: pointer;
  transform-origin: left center;
  transition: color var(--dur) var(--ease), scale 150ms var(--ease);
}
.code-tool-trigger:hover { color: var(--color-ink); }
.code-tool-trigger:active { scale: 0.98; }
.code-tool-trigger:focus-visible {
  outline: none;
  border-radius: var(--radius-sm);
  box-shadow: 0 0 0 1px color-mix(in oklch, var(--color-ink) 20%, transparent);
}
.code-tool-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  display: inline-grid;
  place-items: center;
}
.code-tool.is-error .code-tool-icon { color: var(--color-error); }
.code-tool-label {
  flex-shrink: 0;
  line-height: 1.25;
  white-space: nowrap;
}
.code-tool-label b { font-weight: 600; color: inherit; }
.code-tool.is-running .code-tool-label b { color: transparent; }
.code-tool-preview {
  flex: 0 1 auto;
  min-width: 0;
  font: 400 11.5px var(--font-mono);
  letter-spacing: -0.01em;
  color: var(--color-ink-4);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 2px 6px;
  border-radius: 6px;
  background: var(--code-muted);
  max-width: 60%;
}
.code-tool-dur {
  flex-shrink: 0;
  margin-left: auto;
  font-size: var(--text-xs);
  font-variant-numeric: tabular-nums;
  color: var(--color-ink-4);
}
.code-tool-preview + .code-tool-dur,
.code-tool-label + .code-tool-dur { margin-left: 0; }
.code-tool-trigger > .code-chevron { margin-left: auto; }
.code-tool-dur + .code-chevron { margin-left: 0; }

.code-tool-content {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding: 4px 0 var(--space-2) 24px;
  font-size: var(--text-sm);
  animation: aui-in 200ms var(--aui-ease) both;
  min-width: 0;
}
.code-tool-section { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.code-tool-section-head {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.code-tool-section-title {
  margin: 0;
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--color-ink-3);
}
.code-tool-copy {
  margin-left: auto;
  padding: 1px 6px;
  font: 500 10px var(--font-sans);
  color: var(--color-ink-4);
  background: transparent;
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  transition: color var(--dur) var(--ease), background var(--dur) var(--ease);
}
.code-tool-copy + .code-tool-copy { margin-left: 0; }
.code-tool-copy:hover { color: var(--color-ink); background: var(--code-muted); }
.code-tool-pre {
  margin: 0;
  padding: 10px;
  font: 400 var(--text-xs) var(--font-mono);
  line-height: 1.55;
  color: color-mix(in oklch, var(--color-ink) 90%, transparent);
  background: color-mix(in oklch, var(--color-ink) 4%, transparent);
  border-radius: var(--radius-md);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 360px;
  overflow: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border-2) transparent;
}
.code-tool-pre.is-error { color: var(--color-error); }
.code-tool-error { display: flex; flex-direction: column; gap: 2px; }
.code-tool-error .code-tool-section-title { font-weight: 600; }
.code-tool-error-text {
  margin: 0;
  color: var(--color-ink-3);
  font-size: var(--text-sm);
  line-height: 1.5;
}

/* ─── ToolGroup (ghost) ───────────────────────────────────────────────── */

.code-tool-group { width: 100%; min-width: 0; }
.code-tool-group-trigger {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: 6px 0;
  border: 0;
  background: transparent;
  color: var(--color-ink-3);
  font: 400 var(--text-xs) var(--font-sans);
  line-height: 1.25;
  cursor: pointer;
  transform-origin: left center;
  transition: color var(--dur) var(--ease), scale 150ms var(--ease);
}
.code-tool-group-trigger:hover { color: var(--color-ink); }
.code-tool-group-trigger:active { scale: 0.98; }
.code-tool-group-trigger:focus-visible {
  outline: none;
  border-radius: var(--radius-sm);
  box-shadow: 0 0 0 1px color-mix(in oklch, var(--color-ink) 20%, transparent);
}
.code-tool-group-trigger > .code-chevron { width: 12px; height: 12px; }
.code-tool-group-content {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 4px;
  padding-left: 8px;
  border-left: 1px solid var(--code-hairline);
  min-width: 0;
}
.code-tool-group-content > * {
  animation: aui-in var(--aui-dur) var(--aui-ease) both;
}
.code-tool-group-content > *:nth-child(2) { animation-delay: 40ms; }
.code-tool-group-content > *:nth-child(3) { animation-delay: 80ms; }
.code-tool-group-content > *:nth-child(4) { animation-delay: 120ms; }
.code-tool-group-content > *:nth-child(n + 5) { animation-delay: 160ms; }

/* ─── Reasoning (ghost) ───────────────────────────────────────────────── */

.code-reasoning { width: 100%; min-width: 0; }
.code-reasoning-trigger {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  max-width: 75%;
  padding: 6px 0;
  border: 0;
  background: transparent;
  color: var(--color-ink-3);
  font: 400 var(--text-sm) var(--font-sans);
  cursor: pointer;
  transform-origin: left center;
  transition: color var(--dur) var(--ease), scale 150ms var(--ease);
}
.code-reasoning-trigger:hover { color: var(--color-ink); }
.code-reasoning-trigger:active { scale: 0.98; }
.code-reasoning-trigger:focus-visible {
  outline: none;
  border-radius: var(--radius-sm);
  box-shadow: 0 0 0 1px color-mix(in oklch, var(--color-ink) 20%, transparent);
}
.code-reasoning-icon { flex-shrink: 0; }
.code-reasoning-label {
  line-height: 1.25;
  font-variant-numeric: tabular-nums;
}
.code-reasoning-content {
  position: relative;
  overflow: hidden;
  color: var(--color-ink-3);
  font-size: var(--text-sm);
}
.code-reasoning-content::before {
  content: "";
  pointer-events: none;
  position: absolute;
  inset: 0 0 auto;
  height: 24px;
  z-index: 1;
  background: linear-gradient(to bottom, var(--color-bg), transparent);
}
.code-reasoning.is-preview .code-reasoning-content::after {
  content: "";
  pointer-events: none;
  position: absolute;
  inset: auto 0 0;
  height: 32px;
  z-index: 1;
  background: linear-gradient(to top, var(--color-bg), transparent);
}
.code-reasoning-text {
  max-height: 16rem;
  overflow-y: auto;
  padding: var(--space-2) 0 var(--space-2) 24px;
  line-height: 1.625;
  white-space: pre-wrap;
  word-break: break-word;
  text-wrap: pretty;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border-2) transparent;
  animation: aui-in-up var(--aui-dur) var(--aui-ease) both;
}

/* ─── Markdown prose (assistant-ui markdown-text) ─────────────────────── */

.code-text {
  font: 400 var(--text-base) var(--font-sans);
  color: var(--color-ink);
  line-height: 1.625;
}
.code-text.is-streaming .code-prose > *:last-child::after {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  margin-left: 6px;
  vertical-align: middle;
  background: var(--color-ink-3);
  animation: aui-pulse 1.4s var(--ease) infinite;
}
.code-text.is-streaming .code-prose:empty::after {
  content: "●";
  color: var(--color-ink-3);
  animation: aui-pulse 1.4s var(--ease) infinite;
}

.code-prose > *:first-child { margin-top: 0 !important; }
.code-prose > *:last-child { margin-bottom: 0 !important; }
.code-prose p { margin: 12px 0; line-height: 1.625; }
.code-prose h1 { margin: 20px 0 8px; font-size: var(--text-lg); font-weight: 600; letter-spacing: -0.01em; }
.code-prose h2 { margin: 20px 0 8px; font-size: var(--text-md); font-weight: 600; letter-spacing: -0.01em; }
.code-prose h3 { margin: 16px 0 6px; font-size: var(--text-base); font-weight: 600; }
.code-prose h4 { margin: 14px 0 4px; font-size: var(--text-base); font-weight: 500; }
.code-prose h5,
.code-prose h6 { margin: 12px 0 4px; font-size: var(--text-sm); font-weight: 600; }
.code-prose strong { font-weight: 600; }
.code-prose em { font-style: italic; }
.code-prose a {
  color: var(--color-ink);
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-color: color-mix(in oklch, var(--color-ink) 40%, transparent);
}
.code-prose a:hover { text-decoration-color: currentColor; }
.code-prose blockquote {
  margin: 12px 0;
  padding-inline-start: 16px;
  border-inline-start: 2px solid color-mix(in oklch, var(--color-ink-3) 30%, transparent);
  color: var(--color-ink-3);
}
.code-prose ul,
.code-prose ol {
  margin: 12px 0;
  padding-inline-start: 20px;
}
.code-prose ul { list-style: disc; }
.code-prose ol { list-style: decimal; }
.code-prose li { margin-top: 4px; line-height: 1.625; }
.code-prose li::marker { color: var(--color-ink-3); }
.code-prose li > p { margin: 0; }
.code-prose ul ul,
.code-prose ol ol,
.code-prose ul ol,
.code-prose ol ul { margin: 2px 0; }
.code-prose li input[type="checkbox"] {
  margin: 0 6px 0 -1.3em;
  vertical-align: baseline;
  accent-color: var(--color-ink);
}
.code-prose hr {
  border: 0;
  border-top: 1px solid color-mix(in oklch, var(--color-ink-3) 20%, transparent);
  margin: 12px 0;
}
.code-prose table {
  display: block;
  width: max-content;
  max-width: 100%;
  overflow-x: auto;
  margin: 12px 0;
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--text-sm);
}
.code-prose th {
  background: var(--code-muted);
  padding: 6px 12px;
  text-align: start;
  font-weight: 500;
}
.code-prose th:first-child { border-start-start-radius: var(--radius-lg); }
.code-prose th:last-child { border-start-end-radius: var(--radius-lg); }
.code-prose td {
  padding: 6px 12px;
  border-inline-start: 1px solid color-mix(in oklch, var(--color-ink-3) 20%, transparent);
  border-bottom: 1px solid color-mix(in oklch, var(--color-ink-3) 20%, transparent);
}
.code-prose td:last-child { border-inline-end: 1px solid color-mix(in oklch, var(--color-ink-3) 20%, transparent); }
.code-prose tr:last-child td:first-child { border-end-start-radius: var(--radius-lg); }
.code-prose tr:last-child td:last-child { border-end-end-radius: var(--radius-lg); }
.code-prose code {
  font: 400 0.85em var(--font-mono);
  background: var(--code-muted);
  border-radius: 6px;
  padding: 2px 6px;
}
.code-prose pre code {
  font-size: 13px;
  background: none;
  padding: 0;
  border-radius: 0;
}
/* Fenced blocks: CodeHeader + pre (assistant-ui) */
.code-md-code { margin: 12px 0; min-width: 0; }
.code-md-code-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 6px 4px 14px;
  border: 1px solid color-mix(in oklch, var(--color-ink) 10%, transparent);
  border-bottom: 0;
  border-radius: 12px 12px 0 0;
  background: color-mix(in oklch, var(--color-ink) 5%, transparent);
  font-size: var(--text-xs);
}
.code-md-code-lang {
  color: var(--color-ink-3);
  font-weight: 500;
  text-transform: lowercase;
}
.code-md-copy { width: 24px; height: 24px; }
.code-md-pre {
  margin: 0;
  padding: 14px;
  border: 1px solid color-mix(in oklch, var(--color-ink) 10%, transparent);
  border-top: 0;
  border-radius: 0 0 12px 12px;
  background: color-mix(in oklch, var(--color-ink) 3%, transparent);
  font: 400 13px/1.625 var(--font-mono);
  color: var(--color-ink);
  overflow-x: auto;
  white-space: pre;
  tab-size: 2;
  scrollbar-width: thin;
  scrollbar-color: var(--color-border-2) transparent;
}
/* Bare <pre> (no fence header) */
.code-prose > pre:not(.code-md-pre) {
  margin: 12px 0;
  padding: 14px;
  border: 1px solid color-mix(in oklch, var(--color-ink) 10%, transparent);
  border-radius: 12px;
  background: color-mix(in oklch, var(--color-ink) 3%, transparent);
  font: 400 13px/1.625 var(--font-mono);
  overflow-x: auto;
}
.code-prose img {
  max-width: 100%;
  border-radius: var(--radius-md);
}

/* ─── Footer: scroll-to-bottom + composer + suggestions ───────────────── */

.code-thread-footer {
  position: relative;
  width: 100%;
  max-width: var(--thread-max-width);
  margin: 0 auto;
  padding: var(--space-2) var(--space-4) var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  background: var(--color-bg);
  border-radius: var(--composer-radius) var(--composer-radius) 0 0;
  flex-shrink: 0;
}

.code-scroll-bottom {
  position: absolute;
  top: -44px;
  left: 50%;
  translate: -50% 0;
  z-index: 3;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--color-border-2);
  border-radius: 999px;
  background: var(--color-bg);
  color: var(--color-ink-2);
  cursor: pointer;
  box-shadow: 0 2px 8px oklch(0% 0 0 / 0.12);
  animation: aui-in 160ms var(--ease) both;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease);
}
.code-scroll-bottom:hover { background: var(--color-bg-3); color: var(--color-ink); }
.code-scroll-bottom:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px color-mix(in oklch, var(--color-ink) 20%, transparent);
}
.code-scroll-bottom.is-live::after {
  content: "";
  position: absolute;
  top: 2px;
  right: 2px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--code-live);
  box-shadow: 0 0 0 2px var(--color-bg);
  animation: aui-pulse 1.4s var(--ease) infinite;
}

/* Composer shell (assistant-ui Composer) */
.code-composer {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  width: 100%;
  padding: var(--composer-padding);
  border: 1px solid var(--code-hairline);
  border-radius: var(--composer-radius);
  background: var(--composer-bg);
  cursor: text;
  transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease);
}
.code-composer:focus-within {
  border-color: color-mix(in oklch, var(--color-ink) 24%, transparent);
}
.code-composer.is-drag {
  border-style: dashed;
  border-color: var(--code-live);
  background: color-mix(in oklch, var(--code-live) 8%, var(--composer-bg));
}
.code-composer-input {
  width: 100%;
  min-height: 40px;
  max-height: 192px;
  resize: none;
  background: transparent;
  color: var(--color-ink);
  border: 0;
  outline: none;
  padding: 4px 10px;
  font: 400 var(--text-base) var(--font-sans);
  line-height: 24px;
  caret-color: var(--color-ink);
}
.code-composer-input::placeholder {
  color: color-mix(in oklch, var(--color-ink-3) 60%, transparent);
}

.code-composer-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  min-width: 0;
}
.code-composer-rail {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1 1 auto;
  min-width: 0;
  flex-wrap: wrap;
}
.code-composer-end {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  align-self: flex-end;
}
.code-composer-attach { width: 28px; height: 28px; }

/* Send / stop: ink round button with an icon swap */
.code-composer-send {
  display: inline-grid;
  place-items: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: var(--color-ink);
  color: var(--color-bg);
  cursor: pointer;
  transition: opacity 150ms var(--ease), scale 150ms var(--ease), background var(--dur) var(--ease);
}
.code-composer-send > svg { grid-area: 1 / 1; }
.code-composer-send:hover:not(:disabled) { opacity: 0.9; }
.code-composer-send:active:not(:disabled) { scale: 0.96; }
.code-composer-send:disabled {
  background: var(--code-muted-strong);
  color: color-mix(in oklch, var(--color-ink) 30%, transparent);
  cursor: default;
}
.code-composer-send.is-stop { background: var(--color-ink); }
.code-composer-send:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--color-bg), 0 0 0 3px color-mix(in oklch, var(--color-ink) 40%, transparent);
}

/* Attachment previews */
.code-attachments {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding: 2px 4px 0;
}
.code-attachment {
  position: relative;
  width: 56px;
  height: 56px;
  border-radius: 12px;
  overflow: hidden;
  background: var(--code-muted);
  flex-shrink: 0;
}
.code-attachment img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.code-attachment-remove {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 18px;
  height: 18px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: oklch(0% 0 0 / 0.65);
  color: #fff;
  cursor: pointer;
  display: inline-grid;
  place-items: center;
}
.code-attachment-remove:hover { background: oklch(0% 0 0 / 0.9); }

/* Welcome suggestions (assistant-ui ThreadSuggestions) */
.code-suggestions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: 0 var(--space-4);
}
.code-suggestion {
  padding: 6px 14px;
  border: 1px solid var(--code-hairline);
  border-radius: 999px;
  background: transparent;
  color: var(--color-ink);
  font: 400 var(--text-sm) var(--font-sans);
  white-space: nowrap;
  cursor: pointer;
  animation: aui-in-2 200ms var(--ease) both;
  transition: background var(--dur) var(--ease), translate 150ms var(--ease), scale 150ms var(--ease);
}
.code-suggestion:hover { background: var(--code-muted); translate: 0 -1px; }
.code-suggestion:active { scale: 0.96; }
.code-suggestion:focus-visible {
  outline: none;
  box-shadow: 0 0 0 1px color-mix(in oklch, var(--color-ink) 20%, transparent);
}

/* ─── Question card ───────────────────────────────────────────────────────
 * The agent has paused the Turn to ask something. Unlike a tool row, this
 * one is meant to stop the eye: it is the only transcript element that
 * wants an action from the reader. Styled like assistant-ui's approval
 * card: paper surface, hairline, rounded 16px.
 * ───────────────────────────────────────────────────────────────────────── */

.code-question {
  margin: var(--space-2) 0;
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--code-hairline);
  border-radius: 16px;
  background: var(--color-bg-2);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  box-shadow: 0 0 0 1px color-mix(in oklch, var(--code-live) 30%, transparent);
}
.code-question-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
}
.code-question-tag {
  font: 500 11px var(--font-mono);
  letter-spacing: -0.01em;
  color: var(--code-live);
}
.code-question-timer {
  font: 400 11px var(--font-mono);
  color: var(--color-ink-4);
  font-variant-numeric: tabular-nums;
}
.code-question-text {
  font-size: var(--text-base);
  color: var(--color-ink);
  line-height: 1.5;
}
.code-question-options {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.code-question-opt {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--code-hairline);
  border-radius: 12px;
  background: var(--color-bg);
  color: var(--color-ink);
  text-align: left;
  cursor: pointer;
  min-height: 44px;
  transition: border-color var(--dur) var(--ease), background var(--dur) var(--ease), scale 150ms var(--ease);
}
.code-question-opt:hover:not(:disabled) { background: var(--code-muted); }
.code-question-opt:active:not(:disabled) { scale: 0.98; }
.code-question-opt.is-picked { border-color: var(--code-live); }
.code-question-opt:disabled { opacity: 0.5; cursor: default; }
.code-question-opt-label { font-size: var(--text-sm); font-weight: 500; }
.code-question-opt-desc { font-size: var(--text-xs); color: var(--color-ink-3); line-height: 1.4; }
.code-question-other { display: flex; gap: var(--space-2); }
.code-question-input {
  flex: 1;
  min-width: 0;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--code-hairline);
  border-radius: 999px;
  background: var(--color-bg);
  color: var(--color-ink);
  font: 400 var(--text-sm) var(--font-sans);
}
.code-question-input:focus { outline: none; border-color: var(--code-live); }
.code-question-send {
  padding: var(--space-2) var(--space-4);
  border: 0;
  border-radius: 999px;
  background: var(--color-ink);
  color: var(--color-bg);
  font: 500 var(--text-sm) var(--font-sans);
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 150ms var(--ease), scale 150ms var(--ease);
}
.code-question-send:hover:not(:disabled) { opacity: 0.9; }
.code-question-send:active:not(:disabled) { scale: 0.96; }
.code-question-send:disabled { opacity: 0.3; cursor: default; }

/* Settled: collapses to one quiet line, like a finished tool row. */
.code-question.is-settled {
  flex-direction: row;
  align-items: baseline;
  gap: var(--space-2);
  margin: 0;
  padding: 6px 0;
  border: 0;
  border-radius: 0;
  background: none;
  box-shadow: none;
  font-size: var(--text-sm);
}
.code-question-glyph { color: var(--color-ink-3); }
.code-question-settled-q { color: var(--color-ink-3); }
.code-question-settled-a { color: var(--color-ink); font-weight: 500; }
.code-question.is-expired .code-question-settled-a {
  color: var(--color-ink-4);
  font-weight: 400;
  font-style: italic;
}

/* ─── Shimmer + keyframes ─────────────────────────────────────────────── */

.shimmer {
  display: inline-block;
  padding-bottom: 1px;
  color: transparent;
  background-image: linear-gradient(
    100deg,
    var(--color-ink-4) 40%,
    var(--color-ink) 50%,
    var(--color-ink-4) 60%
  );
  background-size: 200% 100%;
  background-clip: text;
  -webkit-background-clip: text;
  animation: code-shimmer 2s linear infinite;
}
@keyframes code-shimmer {
  from { background-position: 120% 0; }
  to   { background-position: -80% 0; }
}
@keyframes aui-in {
  from { opacity: 0; translate: 0 4px; }
  to   { opacity: 1; translate: 0 0; }
}
@keyframes aui-in-2 {
  from { opacity: 0; translate: 0 8px; }
  to   { opacity: 1; translate: 0 0; }
}
@keyframes aui-in-up {
  from { opacity: 0; translate: 0 -8px; filter: blur(2px); }
  to   { opacity: 1; translate: 0 0; filter: blur(0); }
}
@keyframes aui-pulse {
  0%, 100% { opacity: 0.35; }
  50%      { opacity: 1; }
}
@keyframes aui-spin { to { rotate: 360deg; } }

/* ─── Reduced motion ──────────────────────────────────────────────────── */

@media (prefers-reduced-motion: reduce) {
  .shimmer {
    animation: none;
    background: none;
    color: var(--color-ink-3);
  }
  .code-tool.is-running .code-tool-label b { color: inherit; }
  .code-row,
  .code-welcome-title,
  .code-welcome-sub,
  .code-suggestion,
  .code-scroll-bottom,
  .code-tool-content,
  .code-tool-group-content > *,
  .code-reasoning-text { animation: none; }
  .code-thinking-dot,
  .code-scroll-bottom.is-live::after { animation: none; opacity: 0.8; }
  .code-chevron,
  .code-icon-swap,
  .code-icon-btn,
  .code-suggestion,
  .code-tool-trigger,
  .code-tool-group-trigger,
  .code-reasoning-trigger { transition: none; }
  .code-transcript { scroll-behavior: auto; }
}

/* ─── Narrow screens ──────────────────────────────────────────────────── */

@media (max-width: 720px) {
  .code-transcript { --code-anchor-reserve: 320px; }
  .code-transcript-inner {
    gap: var(--space-5);
    padding: var(--space-3) var(--space-3) var(--space-5);
  }
  /* Mobile browsers (Chrome on iOS in particular) lay the page out under
     their bottom toolbar and report no safe-area inset for it, so
     the composer was clipped. 76px, measured on an iPhone, leaves ~14px
     of air above Chrome iOS's bar; applied on every narrow screen. */
  .code-thread-footer {
    padding: var(--space-2) var(--space-3) calc(76px + env(safe-area-inset-bottom));
    gap: var(--space-3);
  }
  .code-row-user { grid-template-columns: minmax(32px, 1fr) auto; padding: 0; }
  .code-assistant-content,
  .code-msg-footer { padding-left: 0; padding-right: 0; }
  .code-tool-preview { max-width: 45%; }
  .code-tool-dur { display: none; }
  .code-reasoning-trigger { max-width: 100%; }
  .code-composer-input { font-size: 16px; } /* no iOS zoom */
  .code-question { padding: var(--space-3); }
  .code-question.is-settled { flex-wrap: wrap; }
  /* Action bar is a hover affordance; on touch it stays visible. */
  .code-action-bar { opacity: 1; }
}
