/* Presentation Layer v2 — block renderer styles.
 *
 * Only the block-specific and artifact-panel styles live here. Core message
 * layout (.msg-row, .msg-bubble, .msg-time, .msg-cot, etc.) is shared with
 * the legacy renderer and lives in style.css. Keeping this file lean makes
 * the rollout easy to reason about and the cutover reversible.
 */

/* ── Block container hints ──────────────────────────────────────────────── */
.msg-bubble.is-blocks {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  padding-top: 12px;
}
.msg-row.assistant .msg-bubble.is-blocks::before {
  z-index: 1;
}
.msg-row.assistant .msg-bubble.is-blocks .block-text {
  line-height: 1.5;
}
.msg-row.assistant .msg-bubble.is-blocks .block-text p {
  margin: 0;
}
.msg-row.assistant .msg-bubble.is-blocks .block-text p + p {
  margin-top: 0.35rem;
}
.msg-row.assistant .msg-bubble.is-blocks .block-text ul,
.msg-row.assistant .msg-bubble.is-blocks .block-text ol {
  margin: 0.35rem 0 0.65rem;
  /* Logical inline-start so RTL paragraphs indent on the correct side. */
  padding-inline-start: 1.25rem;
  padding-left: 0;
}
.msg-row.assistant .msg-bubble.is-blocks .block-text li + li {
  margin-top: 0.35rem;
}
.msg-row.assistant .msg-bubble.is-blocks .block-text p + ul,
.msg-row.assistant .msg-bubble.is-blocks .block-text p + ol {
  margin-top: 0.35rem;
}
.msg-row.assistant .msg-bubble.is-blocks .block-text.block-text-error {
  border: 1px solid rgba(220, 38, 38, 0.38);
  background: rgba(220, 38, 38, 0.09);
  color: rgb(185, 28, 28);
  border-radius: 10px;
  padding: 0.7rem 0.85rem;
}
.msg-row.assistant .msg-bubble.is-blocks .block-text.block-text-error a {
  color: rgb(185, 28, 28);
}
.msg-row.assistant .msg-bubble.is-blocks .block-text code:not(pre code) {
  padding: 0 5px;
  border-radius: 6px;
  background: rgba(148, 163, 184, 0.18);
  font-size: 0.84em;
  line-height: 1;
  vertical-align: baseline;
}
.msg-row.assistant .msg-bubble.is-blocks .block-text a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}
/* ── Bidi / RTL hardening ───────────────────────────────────────────────── */
/* Pair with dir="auto" on the block wrapper: `plaintext` makes every
 * paragraph/list-item resolve its own base direction from its first strong
 * character, so a mixed Arabic/Latin answer reads correctly line-by-line
 * regardless of the page's UI language. */
.block-text,
.block-text p,
.block-text li,
.block-text h1,
.block-text h2,
.block-text h3,
.block-text h4,
.block-text blockquote {
  unicode-bidi: plaintext;
}
/* Code, SQL and tool payloads are inherently left-to-right; isolate and pin
 * them so Arabic surrounding text can't reorder identifiers, operators, or
 * punctuation. */
.block-text pre,
.block-text pre code,
.msg-sql-pre,
.msg-sql-pre code,
.block-tool-args,
.block-tool-result,
.block-artifact-preview,
.block-artifact-preview code {
  direction: ltr;
  text-align: left;
  unicode-bidi: isolate;
}
/* Inline code (column/table names, identifiers) stays LTR but isolated so it
 * doesn't flip the direction of the Arabic sentence it sits inside. */
.block-text code:not(pre code),
.block-tool-call code {
  unicode-bidi: isolate;
}

.block { display: block; }
.block-render-error {
  background: rgba(220, 38, 38, 0.08);
  color: rgb(220, 38, 38);
  border: 1px solid rgba(220, 38, 38, 0.3);
  border-radius: 4px;
  padding: 0.25rem 0.5rem;
  font-size: 0.75rem;
}
.block-unknown {
  background: rgba(99, 102, 241, 0.08);
  color: rgb(99, 102, 241);
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  font-size: 0.75rem;
}

/* ── thinking ───────────────────────────────────────────────────────────── */
.block-thinking[open] .block-thinking-reveal {
  animation: bcai-thinking-reveal 180ms ease-out both;
}
@media (prefers-reduced-motion: reduce) {
  .block-thinking[open] .block-thinking-reveal {
    animation: none;
  }
}
@keyframes bcai-thinking-reveal {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}
.block-thinking.is-empty { display: none; }

/* ── tool_call ──────────────────────────────────────────────────────────── */
.block-tool-call {
  border: 1px solid var(--border, rgba(148, 163, 184, 0.25));
  border-radius: 6px;
  padding: 0.25rem 0.5rem;
  font-size: 0.75rem;
}
.block-tool-call.is-error {
  border-color: rgba(220, 38, 38, 0.4);
  background: rgba(220, 38, 38, 0.06);
}
.block-tool-call > summary {
  cursor: pointer;
  list-style: none;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}
.block-tool-call code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.block-tool-args,
.block-tool-result {
  margin: 0.35rem 0 0;
  max-height: 240px;
  overflow: auto;
  background: rgba(15, 23, 42, 0.35);
  color: inherit;
  padding: 0.4rem 0.6rem;
  border-radius: 4px;
  font-size: 0.72rem;
}
.block-tool-ms { color: var(--muted); }

/* ── error ──────────────────────────────────────────────────────────────── */
.block-error {
  border: 1px solid rgba(220, 38, 38, 0.4);
  background: rgba(220, 38, 38, 0.08);
  color: rgb(220, 38, 38);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}
.block-error-body { flex: 1; }
.block-error-retry {
  background: rgba(220, 38, 38, 0.15);
  color: inherit;
  border: 1px solid rgba(220, 38, 38, 0.4);
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-size: 0.75rem;
  cursor: pointer;
}
.block-error-retry:hover { background: rgba(220, 38, 38, 0.22); }

/* ── artifact (inline card + side panel) ────────────────────────────────── */
.artifact-card-inline {
  all: unset;
  display: block;
  box-sizing: border-box;
  position: relative;
  overflow: hidden;
  border: 1px solid var(--border, rgba(148, 163, 184, 0.25));
  background: rgba(15, 23, 42, 0.18);
  padding: 0.75rem 1rem;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.8rem;
  transition: border-color 120ms ease, transform 120ms ease, box-shadow 120ms ease;
}
.artifact-card-inline::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #2563eb, #7c3aed 55%, var(--accent));
  opacity: 0.85;
}
html.light .artifact-card-inline {
  background: var(--card);
  border-color: var(--surface-b);
  box-shadow: var(--shadow-chat-assist, 0 1px 2px rgba(15, 23, 42, 0.06), 0 4px 14px rgba(15, 23, 42, 0.05));
}
.artifact-card-inline:hover {
  border-color: rgba(99, 102, 241, 0.75);
  transform: translateY(-1px);
  box-shadow: 0 8px 16px rgba(15, 23, 42, 0.28);
}
html.light .artifact-card-inline:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--surface-b));
  box-shadow: var(--shadow-chat-assist), 0 10px 24px rgba(15, 23, 42, 0.08);
}
.artifact-card-inline:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.artifact-card-inline:disabled,
.artifact-card-inline[aria-disabled="true"] {
  opacity: 0.5;
  pointer-events: none;
}
@media (prefers-reduced-motion: reduce) {
  .artifact-card-inline {
    transition: border-color 120ms ease, box-shadow 120ms ease;
  }
  .artifact-card-inline:hover {
    transform: none;
  }
}
.block-artifact-header {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  padding-top: 0.15rem;
}
.block-artifact-header svg,
.block-artifact-header .block-artifact-icon {
  flex-shrink: 0;
  width: 1.1rem;
  height: 1.1rem;
  padding: 0.28rem;
  border-radius: 8px;
  color: var(--accent);
  background: rgba(var(--accent-rgb, 37, 99, 235), 0.12);
  border: 1px solid rgba(var(--accent-rgb, 37, 99, 235), 0.2);
}
.block-artifact-title { font-weight: 600; flex: 1; color: var(--text); }
.block-artifact-kind {
  font-size: 0.68rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
html.light .block-artifact-kind {
  background: rgba(37, 99, 235, 0.1);
  color: var(--accent);
  padding: 0.15rem 0.45rem;
  border-radius: 999px;
}
.block-artifact-preview {
  margin: 0.45rem 0 0;
  max-height: 48px;
  overflow: auto;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--code-border);
  border-radius: 10px;
  background: var(--code-theme-bg) !important;
  color: var(--code-theme-fg);
  font-size: 0.74rem;
  line-height: 1.5;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.block-artifact-preview code {
  display: block;
  color: var(--code-theme-fg);
}
.block-artifact-preview code .tok.comment { color: var(--code-theme-comment); font-style: italic; }
.block-artifact-preview code .tok.string { color: var(--code-theme-string); }
.block-artifact-preview code .tok.number { color: var(--code-theme-number); }
.block-artifact-preview code .tok.keyword { color: var(--code-theme-keyword); font-weight: 600; }
.block-artifact-preview code .tok.tag { color: var(--code-theme-tag); }
.block-artifact-preview code .tok.attr-name { color: var(--code-theme-attr); }
.block-artifact-preview code .tok.punct { color: var(--code-theme-punct); }

.msg-row.assistant .msg-bubble.is-blocks pre.block-artifact-preview {
  margin: 0.45rem 0 0;
  padding: 0.55rem 0.65rem;
  border-radius: 10px;
  border: 1px solid var(--code-border);
  background: var(--code-theme-bg) !important;
  box-shadow: none;
}
.block-artifact-cta {
  margin-top: 0.5rem;
  padding: 0.4rem 0.5rem;
  margin-left: -0.5rem;
  margin-right: -0.5rem;
  border-radius: 6px;
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 600;
  transition: background 120ms ease, text-decoration 120ms ease;
}
.artifact-card-inline:hover .block-artifact-cta {
  text-decoration: underline;
  text-underline-offset: 2px;
  background: rgba(37, 99, 235, 0.06);
}
@media (max-width: 640px) {
  .artifact-card-inline {
    padding: 0.85rem 1rem;
    min-height: 44px;
  }
}

/* Side panel — inset-inline-end docks right in LTR, left in RTL (Arabic). */
.artifact-panel {
  position: fixed;
  top: 0;
  inset-inline-end: 0;
  height: 100vh;
  height: 100dvh;
  width: min(820px, 96vw);
  background: var(--card);
  color: var(--text);
  border-inline-start: 1px solid var(--surface-b);
  display: flex;
  flex-direction: column;
  /* Above chat topbar (z-index 220) so the toolbar stays visible. */
  z-index: 260;
  box-shadow: -12px 0 32px rgba(0, 0, 0, 0.18);
  padding-top: env(safe-area-inset-top, 0px);
  box-sizing: border-box;
  overflow: hidden;
}
[dir="rtl"] .artifact-panel {
  box-shadow: 12px 0 32px rgba(0, 0, 0, 0.18);
}
.artifact-panel::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #2563eb, #7c3aed 55%, var(--accent));
  z-index: 4;
  pointer-events: none;
}
html.light .artifact-panel {
  box-shadow: -8px 0 24px rgba(15, 23, 42, 0.06);
}
html.light[dir="rtl"] .artifact-panel {
  box-shadow: 8px 0 24px rgba(15, 23, 42, 0.06);
}
.artifact-panel.hidden { display: none; }

.artifact-panel-toolbar {
  position: sticky;
  top: 0;
  z-index: 2;
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 0.75rem;
  min-height: 48px;
  padding: 0.55rem 0.85rem;
  background: var(--card);
  border-bottom: 1px solid var(--surface-b);
  box-shadow: 0 1px 0 color-mix(in srgb, #fff 4%, transparent);
}
html.light .artifact-panel-toolbar {
  box-shadow: 0 1px 0 rgba(15, 23, 42, 0.04);
}
.ap-zone-left {
  min-width: 0;
}
.ap-title-wrap {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  min-width: 0;
}
.ap-title {
  font-weight: 600;
  font-size: 0.92rem;
  line-height: 1.35;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ap-badge {
  flex-shrink: 0;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 0.15rem 0.45rem;
  border-radius: 6px;
  color: var(--accent);
  background: rgba(var(--accent-rgb, 37, 99, 235), 0.12);
  border: 1px solid rgba(var(--accent-rgb, 37, 99, 235), 0.22);
}
.ap-zone-center {
  justify-self: center;
}
.ap-zone-right {
  justify-self: end;
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
}

.ap-segmented {
  position: relative;
  display: inline-flex;
  padding: 3px;
  background: var(--toggle-bg);
  border: 1px solid var(--toggle-b);
  border-radius: 999px;
}
.ap-seg {
  position: relative;
  z-index: 1;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.35rem 0.8rem;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  border-radius: 999px;
  transition: color 200ms ease;
}
.ap-seg.is-active {
  color: var(--text);
}
.ap-seg-ico {
  width: 0.85rem;
  height: 0.85rem;
  flex-shrink: 0;
}
.ap-seg-indicator {
  position: absolute;
  top: 3px;
  bottom: 3px;
  inset-inline-start: 3px;
  width: calc(50% - 3px);
  background: var(--card);
  border: 1px solid var(--surface-b);
  border-radius: 999px;
  transition: transform 200ms cubic-bezier(0.4, 0, 0.2, 1);
  pointer-events: none;
}
.ap-segmented[data-active="source"] .ap-seg-indicator {
  transform: translateX(100%);
}

.ap-icon-btn {
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 8px;
  color: var(--muted);
  cursor: pointer;
  transition: background 140ms ease, color 140ms ease, transform 140ms ease, border-color 140ms ease;
}
.ap-icon-btn svg {
  width: 0.95rem;
  height: 0.95rem;
}
.ap-icon-btn:hover {
  color: var(--text);
  background: rgba(var(--accent-rgb, 37, 99, 235), 0.1);
  transform: scale(1.05);
}
.ap-icon-btn:active {
  transform: scale(0.96);
}
.ap-icon-btn:focus-visible {
  outline: 2px solid rgba(var(--accent-rgb, 37, 99, 235), 0.55);
  outline-offset: 2px;
}
.ap-icon-close:hover {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.1);
}
.ap-sep {
  width: 1px;
  height: 18px;
  background: var(--surface-b);
  margin: 0 0.25rem;
}

.artifact-panel-body {
  flex: 1 1 0;
  overflow: hidden;
  position: relative;
  min-height: 0;
}
.ap-pane {
  position: absolute;
  inset: 0;
  overflow: auto;
  scroll-behavior: smooth;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
}
.ap-pane.is-active {
  opacity: 1;
  pointer-events: auto;
}
/* HTML preview: outer pane does not scroll — iframe fills height and scrolls internally */
.ap-pane.artifact-preview {
  overflow: hidden;
}
.ap-pane::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
.ap-pane::-webkit-scrollbar-thumb {
  background: var(--surface-b);
  border-radius: 999px;
  border: 2px solid var(--card);
}

/* Preview pane: stretch iframe to fill space below toolbar (flex fixes % height chain). */
.artifact-panel-body > .artifact-preview.ap-pane {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  background:
    radial-gradient(circle at 1px 1px, color-mix(in srgb, var(--surface-b) 38%, transparent) 1px, transparent 0)
    0 0 / 18px 18px;
}
html.light .artifact-panel-body > .artifact-preview.ap-pane {
  background:
    radial-gradient(circle at 1px 1px, rgba(148, 163, 184, 0.22) 1px, transparent 0)
    0 0 / 18px 18px;
}
.artifact-panel-body > .artifact-preview.ap-pane > .ap-html-shell {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 0.85rem;
}
.artifact-panel-body > .artifact-preview.ap-pane > .ap-html-shell > .ap-html-frame,
.artifact-panel-body > .artifact-preview.ap-pane > iframe {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  border: 0;
  background: #fff;
  border-radius: 12px;
  box-shadow:
    0 0 0 1px rgba(15, 23, 42, 0.06),
    0 12px 32px rgba(15, 23, 42, 0.12);
}
.artifact-panel-body > .artifact-preview.ap-pane > .artifact-fallback,
.artifact-panel-body > .artifact-preview.ap-pane > .artifact-mermaid {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  margin: 0.85rem;
  border-radius: 12px;
  border: 1px solid var(--surface-b);
  background: var(--card);
}
html.light .artifact-panel-body > .artifact-preview.ap-pane > .ap-html-shell > .ap-html-frame,
html.light .artifact-panel-body > .artifact-preview.ap-pane > iframe {
  background: #fafafa;
  box-shadow:
    0 0 0 1px rgba(15, 23, 42, 0.05),
    0 10px 28px rgba(15, 23, 42, 0.08);
}

.ap-toast {
  position: absolute;
  top: 56px;
  inset-inline-end: 14px;
  z-index: 3;
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.35rem 0.6rem;
  border-radius: 8px;
  color: #fff;
  background: rgb(34 197 94 / 0.95);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2);
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 160ms ease, transform 160ms ease;
}
.ap-toast.is-show {
  opacity: 1;
  transform: translateY(0);
}

.artifact-panel-body .artifact-source pre {
  margin: 1rem;
  padding: 1.1rem 1.2rem;
  font-size: 0.82rem;
  line-height: 1.65;
  overflow: visible;
  border: 1px solid var(--code-border);
  border-radius: 12px;
  background: var(--code-theme-bg) !important;
  color: var(--code-theme-fg);
  font-family: ui-monospace, "JetBrains Mono", "Fira Code", SFMono-Regular, Menlo, monospace;
}

.artifact-panel-body .artifact-source pre code {
  display: block;
  color: var(--code-theme-fg);
}
.artifact-panel-body .artifact-source pre code .tok.comment { color: var(--code-theme-comment); font-style: italic; }
.artifact-panel-body .artifact-source pre code .tok.string { color: var(--code-theme-string); }
.artifact-panel-body .artifact-source pre code .tok.number { color: var(--code-theme-number); }
.artifact-panel-body .artifact-source pre code .tok.keyword { color: var(--code-theme-keyword); font-weight: 600; }
.artifact-panel-body .artifact-source pre code .tok.tag { color: var(--code-theme-tag); }
.artifact-panel-body .artifact-source pre code .tok.attr-name { color: var(--code-theme-attr); }
.artifact-panel-body .artifact-source pre code .tok.punct { color: var(--code-theme-punct); }

@media (prefers-reduced-motion: reduce) {
  .ap-seg,
  .ap-seg-indicator,
  .ap-pane,
  .ap-icon-btn,
  .ap-toast {
    transition: none;
  }
  .ap-icon-btn:hover,
  .ap-icon-btn:active {
    transform: none;
  }
}
.artifact-fallback {
  margin: 1rem;
  padding: 1rem 1.05rem;
  border: 1px solid var(--code-border);
  border-radius: 12px;
  background: var(--code-theme-bg) !important;
  color: var(--code-theme-fg);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.82rem;
  line-height: 1.6;
  white-space: pre-wrap;
  overflow: auto;
}
.artifact-mermaid {
  display: block;
  padding: 1rem;
  min-height: 100%;
  min-width: max-content;
}

/* ── Topic divider (v2 only) ─────────────────────────────────────────────
 * Inserted between consecutive user messages whose topic_id differs (set
 * server-side by the time-gap heuristic). Visually segments long sessions
 * — useful for ERP investigations like month-end where the user comes
 * back to the same chat next morning. */
.msg-topic-divider {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 1rem 0 0.5rem;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.msg-topic-divider::before,
.msg-topic-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border, rgba(148, 163, 184, 0.2));
}
.msg-topic-label {
  flex: 0 0 auto;
  padding: 0.15rem 0.7rem;
  border: 1px solid var(--border, rgba(148, 163, 184, 0.25));
  background: rgba(99, 102, 241, 0.08);
  color: var(--accent);
  border-radius: 999px;
  white-space: nowrap;
  max-width: min(60ch, 70%);
  overflow: hidden;
  text-overflow: ellipsis;
  text-transform: none;
  letter-spacing: 0.01em;
  font-weight: 500;
}

/* ── Per-message actions bar (v2 only) ──────────────────────────────────── */
.msg-actions-bar {
  display: inline-flex;
  flex-wrap: nowrap;
  gap: 1px;
  margin-inline-start: 0;
  align-items: center;
  flex-shrink: 0;
  padding: 2px 3px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--surface) 62%, transparent);
  border: 1px solid color-mix(in srgb, var(--card-b) 50%, transparent);
  box-shadow: inset 0 1px 0 color-mix(in srgb, #fff 4%, transparent);
  opacity: 0;
  transition: opacity 120ms ease;
}
.msg-row:hover .msg-actions-bar,
.msg-row:focus-within .msg-actions-bar {
  opacity: 1;
}
@media (max-width: 768px) {
  /* On mobile there's no hover, so always show. */
  .msg-actions-bar { opacity: 1; }
}
.msg-action-btn {
  background: transparent;
  border: 1px solid transparent;
  border-radius: 5px;
  padding: 0;
  width: 1.5rem;
  height: 1.5rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: color-mix(in srgb, var(--muted) 88%, var(--text));
  transition: background 120ms ease, color 120ms ease, border-color 120ms ease;
}
.msg-action-btn:hover {
  color: var(--text);
  background: color-mix(in srgb, var(--surface-b) 75%, transparent);
  border-color: color-mix(in srgb, var(--card-b) 65%, transparent);
}
.msg-action-btn:focus-visible {
  outline: 2px solid rgba(var(--accent-rgb, 59, 130, 246), 0.45);
  outline-offset: 1px;
}
.msg-action-btn:disabled { opacity: 0.5; cursor: progress; }
.msg-action-btn.is-active {
  color: rgb(250, 204, 21);
  background: rgba(250, 204, 21, 0.1);
  border-color: rgba(250, 204, 21, 0.4);
}
.msg-action-btn.is-flashed {
  background: rgba(34, 197, 94, 0.15);
  border-color: rgba(34, 197, 94, 0.4);
  color: rgb(34, 197, 94);
}
.msg-action-btn svg {
  width: 0.85rem;
  height: 0.85rem;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.msg-action-btn[data-action="bookmark"].is-active svg {
  fill: currentColor;
}

/* ── Feedback (thumbs / issue-report) action variants ──────────────────── */
.msg-action-btn[data-action="thumb-up"].is-active {
  color: rgb(34, 197, 94);
  background: rgba(34, 197, 94, 0.12);
  border-color: rgba(34, 197, 94, 0.4);
}
.msg-action-btn[data-action="thumb-up"].is-active svg { fill: currentColor; }
.msg-action-btn[data-action="thumb-down"].is-active {
  color: rgb(239, 68, 68);
  background: rgba(239, 68, 68, 0.12);
  border-color: rgba(239, 68, 68, 0.4);
}
.msg-action-btn[data-action="thumb-down"].is-active svg { fill: currentColor; }
.msg-action-btn[data-action="issue-report"].is-active {
  color: rgb(245, 158, 11);
  background: rgba(245, 158, 11, 0.12);
  border-color: rgba(245, 158, 11, 0.4);
}
.msg-action-btn[data-action="issue-report"].is-active svg { fill: currentColor; }

/* ── Reason-tag popover (shared by thumbs & issue-report) ──────────────── */
.msg-feedback-popover {
  position: absolute;
  z-index: 80;
  min-width: 270px;
  max-width: min(360px, calc(100vw - 1rem));
  padding: 0.9rem;
  border-radius: 14px;
  background:
    linear-gradient(180deg, rgba(148, 163, 184, 0.12) 0%, rgba(148, 163, 184, 0.04) 22%, transparent 40%),
    var(--surface);
  border: 1px solid color-mix(in srgb, var(--border, rgba(148, 163, 184, 0.35)) 82%, transparent);
  box-shadow: 0 22px 50px rgba(0, 0, 0, 0.35), 0 8px 18px rgba(0, 0, 0, 0.2);
  color: var(--text);
  font-size: 0.84rem;
  line-height: 1.4;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}
.msg-feedback-pop-title {
  font-weight: 700;
  font-size: 0.9rem;
  letter-spacing: 0.01em;
}
.msg-feedback-popover.is-issue-report .msg-feedback-pop-title {
  color: #f8fafc;
}
html.light .msg-feedback-popover.is-issue-report .msg-feedback-pop-title {
  color: var(--text);
}
.msg-feedback-pop-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.38rem;
}
.msg-feedback-pop-tag {
  display: inline-flex;
  align-items: center;
  gap: 0.38rem;
  padding: 0.32rem 0.62rem;
  border-radius: 999px;
  border: 1px solid rgba(148, 163, 184, 0.32);
  background: rgba(148, 163, 184, 0.09);
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  font-size: 0.76rem;
  font-weight: 500;
  transition: border-color 130ms ease, background 130ms ease, color 130ms ease;
}
.msg-feedback-pop-tag:hover {
  border-color: rgba(99, 102, 241, 0.55);
  background: rgba(99, 102, 241, 0.16);
}
.msg-feedback-pop-tag input {
  margin: 0;
  accent-color: var(--accent);
}
.msg-feedback-pop-tag input:checked + span {
  color: #fff;
}
.msg-feedback-pop-tag:has(input:checked) {
  border-color: rgba(99, 102, 241, 0.8);
  background: linear-gradient(140deg, rgba(59, 130, 246, 0.34) 0%, rgba(99, 102, 241, 0.4) 100%);
}
.msg-feedback-pop-textarea {
  width: 100%;
  resize: vertical;
  min-height: 74px;
  max-height: 190px;
  padding: 0.55rem 0.65rem;
  border-radius: 10px;
  border: 1px solid var(--border, rgba(148, 163, 184, 0.3));
  background: rgba(15, 23, 42, 0.45);
  color: inherit;
  font-family: inherit;
  font-size: 0.8rem;
  line-height: 1.4;
  transition: border-color 120ms ease, box-shadow 120ms ease, background 120ms ease;
}
.msg-feedback-pop-textarea::placeholder {
  color: color-mix(in srgb, var(--muted) 88%, transparent);
}
.msg-feedback-pop-textarea:focus-visible {
  outline: none;
  border-color: rgba(99, 102, 241, 0.72);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.25);
  background: rgba(15, 23, 42, 0.62);
}
.msg-feedback-pop-textarea.is-required {
  border-color: rgba(239, 68, 68, 0.9);
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.18);
}
.msg-feedback-pop-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.15rem;
}
.msg-feedback-pop-skip,
.msg-feedback-pop-submit {
  padding: 0.43rem 0.82rem;
  border-radius: 9px;
  border: 1px solid var(--border, rgba(148, 163, 184, 0.3));
  background: transparent;
  color: inherit;
  cursor: pointer;
  font-size: 0.79rem;
  font-weight: 600;
  transition: transform 110ms ease, filter 110ms ease, background 110ms ease, border-color 110ms ease;
}
.msg-feedback-pop-submit {
  background: linear-gradient(140deg, #3b82f6 0%, #6366f1 100%);
  border-color: transparent;
  color: #fff;
}
.msg-feedback-pop-submit:hover { filter: brightness(1.08); transform: translateY(-1px); }
.msg-feedback-pop-skip:hover {
  background: rgba(148, 163, 184, 0.14);
  border-color: rgba(148, 163, 184, 0.5);
}
.msg-feedback-pop-submit:active,
.msg-feedback-pop-skip:active {
  transform: translateY(0);
}
.msg-feedback-pop-submit:focus-visible,
.msg-feedback-pop-skip:focus-visible {
  outline: 2px solid rgba(99, 102, 241, 0.55);
  outline-offset: 2px;
}
html.light .msg-feedback-popover {
  background:
    linear-gradient(180deg, rgba(37, 99, 235, 0.1) 0%, rgba(37, 99, 235, 0.03) 24%, transparent 42%),
    var(--card);
  border-color: rgba(148, 163, 184, 0.35);
  color: var(--text);
  box-shadow: 0 14px 34px rgba(15, 23, 42, 0.13), 0 4px 10px rgba(15, 23, 42, 0.09);
}
html.light .msg-feedback-pop-tag {
  background: rgba(148, 163, 184, 0.1);
}
html.light .msg-feedback-pop-tag:hover {
  background: rgba(37, 99, 235, 0.11);
}
html.light .msg-feedback-pop-tag input:checked + span {
  color: #0f172a;
}
html.light .msg-feedback-pop-tag:has(input:checked) {
  border-color: rgba(37, 99, 235, 0.6);
  background: linear-gradient(140deg, rgba(147, 197, 253, 0.72) 0%, rgba(191, 219, 254, 0.95) 100%);
}
html.light .msg-feedback-pop-textarea {
  background: rgba(248, 250, 252, 0.9);
}
html.light .msg-feedback-pop-textarea:focus-visible {
  background: #fff;
}
@media (max-width: 520px) {
  .msg-feedback-popover {
    min-width: min(280px, calc(100vw - 1rem));
    max-width: calc(100vw - 1rem);
    padding: 0.78rem;
  }
}

/* "Remember this" popover — same visual language as the feedback popover. */
.msg-remember-popover {
  margin: 0.4rem 0 0.6rem;
  padding: 0.9rem;
  min-width: 280px;
  max-width: min(420px, calc(100vw - 1rem));
  border-radius: 12px;
  background:
    linear-gradient(180deg, rgba(168, 85, 247, 0.12) 0%, rgba(168, 85, 247, 0.04) 22%, transparent 40%),
    var(--surface);
  border: 1px solid color-mix(in srgb, var(--border, rgba(148, 163, 184, 0.35)) 82%, transparent);
  box-shadow: 0 14px 34px rgba(15, 23, 42, 0.2), 0 4px 10px rgba(15, 23, 42, 0.12);
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  font-size: 0.85rem;
  color: var(--text);
}
html.light .msg-remember-popover {
  background:
    linear-gradient(180deg, rgba(168, 85, 247, 0.08) 0%, rgba(168, 85, 247, 0.02) 22%, transparent 40%),
    var(--card);
  color: var(--text);
}
.msg-remember-head {
  font-weight: 600;
  font-size: 0.9rem;
}
.msg-remember-label {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  font-size: 0.78rem;
}
.msg-remember-label > span {
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.msg-remember-cat,
.msg-remember-text {
  padding: 0.42rem 0.55rem;
  border-radius: 8px;
  border: 1px solid var(--border, rgba(148, 163, 184, 0.35));
  background: rgba(15, 23, 42, 0.25);
  color: inherit;
  font-size: 0.85rem;
  font-family: inherit;
}
html.light .msg-remember-cat,
html.light .msg-remember-text {
  background: rgba(248, 250, 252, 0.7);
}
.msg-remember-text {
  resize: vertical;
  min-height: 3.2em;
}
.msg-remember-error {
  font-size: 0.78rem;
  color: #f87171;
}
.msg-remember-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.45rem;
}
.msg-remember-cancel,
.msg-remember-save {
  padding: 0.4rem 0.85rem;
  border-radius: 7px;
  border: 1px solid var(--border, rgba(148, 163, 184, 0.35));
  background: transparent;
  color: inherit;
  font-size: 0.82rem;
  cursor: pointer;
}
.msg-remember-save {
  background: var(--accent);
  color: #ffffff;
  border-color: transparent;
}
.msg-remember-save:disabled {
  opacity: 0.6;
  cursor: progress;
}
/* Save-as-widget success link reuses the .msg-remember-save button styling. */
.msg-widget-view {
  display: inline-block;
  text-align: center;
  text-decoration: none;
}

/* Inline edit form replacing the user bubble while editing. */
.msg-row.is-editing .msg-bubble { display: none; }
.msg-edit-form {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  width: 100%;
  margin: 0.25rem 0 0.5rem;
}
.msg-edit-textarea {
  width: 100%;
  resize: vertical;
  min-height: 4em;
  padding: 0.55rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border, rgba(148, 163, 184, 0.3));
  background: rgba(15, 23, 42, 0.25);
  color: inherit;
  font-family: inherit;
  font-size: 0.88rem;
  line-height: 1.45;
}
.msg-edit-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}
.msg-edit-save,
.msg-edit-cancel {
  font-size: 0.78rem;
  padding: 0.3rem 0.7rem;
  border-radius: 6px;
  border: 1px solid var(--border, rgba(148, 163, 184, 0.3));
  background: transparent;
  color: inherit;
  cursor: pointer;
}
.msg-edit-save {
  background: rgba(99, 102, 241, 0.18);
  border-color: rgba(99, 102, 241, 0.45);
}
.msg-edit-save:hover { background: rgba(99, 102, 241, 0.28); }
.msg-edit-msg { font-size: 0.75rem; color: var(--muted); }
.artifact-mermaid svg {
  max-width: none;
  height: auto;
  display: block;
}
.artifact-mermaid-error {
  margin: 0.5rem 1rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid rgba(239, 68, 68, 0.35);
  background: rgba(239, 68, 68, 0.08);
  color: #fca5a5;
  border-radius: 8px;
  font-size: 0.8rem;
}

/* When artifact panel is open, give the chat some breathing room on wide
 * screens only — on narrow screens the panel already covers everything. */
@media (min-width: 1100px) {
  body.has-artifact-panel .chat-layout,
  body.has-artifact-panel #messages {
    padding-inline-end: min(760px, 56vw);
    transition: padding-inline-end 160ms ease;
  }
}

@media (max-width: 768px) {
  .artifact-panel {
    width: 100vw;
  }
  .artifact-panel-toolbar {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "apTitle apActions"
      "apTabs apTabs";
    row-gap: 0.5rem;
    padding: 0.55rem 0.7rem;
  }
  .ap-zone-left {
    grid-area: apTitle;
  }
  .ap-zone-center {
    grid-area: apTabs;
    justify-self: stretch;
  }
  .ap-zone-right {
    grid-area: apActions;
  }
  .ap-segmented {
    width: 100%;
  }
  .ap-seg {
    flex: 1;
    justify-content: center;
  }
  .artifact-panel-body .artifact-source pre {
    margin: 0.7rem;
    padding: 0.8rem;
    font-size: 0.77rem;
  }
}

/* ── Stream status strip ───────────────────────────────────────────────────
 * Top-of-turn meter shown above the streaming assistant bubble: live token
 * meter, output-token + tool-call chips, current activity, and Inspect/Stop
 * controls. Fed by run_event frames. */
.stream-status-strip {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0 0 0.4rem 0;
  padding: 0.3rem 0.6rem;
  border: 1px solid var(--surface-b);
  border-radius: 10px;
  background: var(--toggle-bg);
  font-size: 0.74rem;
  color: var(--muted);
}
.ss-activity {
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 16rem;
}
.ss-spacer { flex: 1 1 auto; }
.ss-meter {
  position: relative;
  width: 64px;
  height: 6px;
  border-radius: 999px;
  background: var(--surface-b);
  overflow: hidden;
  display: inline-flex;
  align-items: center;
}
.ss-meter-fill {
  position: absolute;
  inset-inline-start: 0; top: 0; bottom: 0;
  width: 0%;
  background: var(--accent);
  transition: width 200ms ease;
}
.ss-meter-fill.is-high { background: #d97706; }
.ss-meter-fill.is-critical { background: var(--danger); }
.ss-meter-pct {
  font-variant-numeric: tabular-nums;
  min-width: 2.2rem;
}
.ss-chip {
  font-variant-numeric: tabular-nums;
  padding: 0.1rem 0.35rem;
  border-radius: 6px;
  background: var(--surface-b);
  white-space: nowrap;
}
.ss-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.22rem 0.5rem;
  border: 1px solid var(--surface-b);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 150ms ease, border-color 150ms ease;
}
.ss-btn:hover { border-color: var(--accent); }
.ss-btn svg { width: 0.85rem; height: 0.85rem; }
.ss-stop { color: #f87171; }
.ss-stop svg { width: 0.7rem; height: 0.7rem; }

/* ── Run Inspector dock ────────────────────────────────────────────────────
 * Mirrors .artifact-panel; one step above so it can stack on top when both open. */
.run-inspector {
  position: fixed;
  top: 0;
  inset-inline-end: 0;
  height: 100vh;
  height: 100dvh;
  width: min(560px, 96vw);
  background: color-mix(in srgb, var(--card) 96%, var(--surface));
  color: var(--text);
  border-inline-start: 1px solid var(--surface-b);
  display: flex;
  flex-direction: column;
  /* Above chat topbar (220) and artifact panel (260) when both are open. */
  z-index: 261;
  box-shadow: -12px 0 32px rgba(0, 0, 0, 0.18);
  padding-top: env(safe-area-inset-top, 0px);
  box-sizing: border-box;
  overflow: hidden;
}
[dir="rtl"] .run-inspector { box-shadow: 12px 0 32px rgba(0, 0, 0, 0.18); }
html.light .run-inspector { box-shadow: -8px 0 24px rgba(15, 23, 42, 0.06); }
html.light[dir="rtl"] .run-inspector { box-shadow: 8px 0 24px rgba(15, 23, 42, 0.06); }
.run-inspector.hidden { display: none; }
.ri-accent {
  position: absolute;
  inset: 0 0 auto;
  height: 3px;
  background: linear-gradient(90deg, rgba(37, 99, 235, 0.48), rgba(16, 185, 129, 0.44), rgba(124, 58, 237, 0.38));
  pointer-events: none;
  z-index: 3;
}

.ri-toolbar {
  position: sticky;
  top: 0;
  z-index: 2;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-height: 52px;
  padding: 0.65rem 0.85rem 0.55rem;
  background: color-mix(in srgb, var(--card) 94%, var(--surface));
  border-bottom: 1px solid color-mix(in srgb, var(--surface-b) 82%, transparent);
  box-shadow: 0 1px 0 color-mix(in srgb, #fff 4%, transparent);
}
html.light .ri-toolbar {
  box-shadow: 0 1px 0 rgba(15, 23, 42, 0.04);
}
.ri-zone-left { flex: 1 1 auto; min-width: 0; }
.ri-zone-right { display: inline-flex; align-items: center; gap: 0.25rem; flex-shrink: 0; }
.ri-title-wrap { display: flex; align-items: center; gap: 0.55rem; min-width: 0; }
.ri-head-icon {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--accent);
  background: linear-gradient(135deg, rgba(16, 185, 129, 0.14), rgba(37, 99, 235, 0.12));
  border: 1px solid color-mix(in srgb, var(--accent) 22%, var(--surface-b));
  box-shadow: 0 6px 16px rgba(16, 185, 129, 0.1);
}
html.dark .ri-head-icon { color: #6ee7b7; }
.ri-head-icon svg { width: 16px; height: 16px; }
.ri-title-block { min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.ri-title { font-weight: 650; font-size: 0.9rem; letter-spacing: -0.02em; white-space: nowrap; }
.ri-status {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  align-self: flex-start;
  font-size: 0.68rem;
  font-weight: 600;
  padding: 0.12rem 0.45rem;
  border-radius: 999px;
  background: color-mix(in srgb, var(--surface) 55%, var(--card));
  border: 1px solid color-mix(in srgb, var(--surface-b) 80%, transparent);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.ri-status-main { color: inherit; }
.ri-status-sep { opacity: 0.45; }
.ri-status-steps { color: var(--muted); font-weight: 500; }
.ri-status.is-running { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 28%, var(--surface-b)); background: color-mix(in srgb, var(--accent) 8%, var(--card)); }
.ri-status.is-ok { color: var(--accent); border-color: color-mix(in srgb, #34d399 30%, var(--surface-b)); background: color-mix(in srgb, #34d399 8%, var(--card)); }
.ri-status.is-error { color: var(--danger); border-color: color-mix(in srgb, #f87171 30%, var(--surface-b)); background: color-mix(in srgb, #f87171 8%, var(--card)); }
.ri-status.is-stopped { color: #d97706; border-color: color-mix(in srgb, #fbbf24 30%, var(--surface-b)); background: color-mix(in srgb, #fbbf24 8%, var(--card)); }
html.dark .ri-status.is-ok { color: #34d399; }

.ri-icon-btn {
  width: 30px; height: 30px;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid transparent; background: transparent; cursor: pointer;
  color: var(--muted); border-radius: 8px;
}
.ri-icon-btn:hover { background: var(--surface-b); color: var(--text); border-color: color-mix(in srgb, var(--surface-b) 70%, transparent); }
.ri-icon-btn svg { width: 1rem; height: 1rem; }
.ri-sep { width: 1px; height: 18px; background: var(--surface-b); margin: 0 0.2rem; }

/* ── run inspector: id chips ─────────────────────────────────────────────── */
.ri-idbar {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  padding: 0.5rem 0.65rem;
  border-bottom: 1px solid var(--surface-b);
}
.ri-id-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.2rem 0.5rem;
  border: 1px solid var(--surface-b);
  border-radius: 999px;
  background: var(--card);
  color: var(--text);
  font-size: 0.7rem;
  cursor: pointer;
  transition: border-color 120ms ease, background 120ms ease;
}
.ri-id-chip:hover {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--surface-b));
  background: color-mix(in srgb, var(--accent) 8%, var(--card));
}
.ri-id-label {
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  font-size: 0.62rem;
}
.ri-id-val {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--text);
}
.ri-id-copy { width: 0.85rem; height: 0.85rem; color: var(--muted); }
.ri-id-chip:hover .ri-id-copy { color: var(--accent); }

.ri-body { flex: 1 1 auto; overflow-y: auto; padding: 0.55rem 0.65rem 0.75rem; }
.ri-legend {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin: 0 0 0.55rem;
  padding: 0.35rem 0.55rem;
  border-radius: 8px;
  font-size: 0.68rem;
  color: var(--muted);
  background: color-mix(in srgb, var(--surface) 50%, transparent);
  border: 1px dashed color-mix(in srgb, var(--surface-b) 75%, transparent);
}
.ri-legend-swatch {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: color-mix(in srgb, var(--muted) 18%, var(--card));
  border: 1px solid color-mix(in srgb, var(--surface-b) 85%, transparent);
  flex-shrink: 0;
}
.ri-empty {
  color: var(--muted);
  font-size: 0.82rem;
  padding: 2rem 0.75rem;
  text-align: center;
  border: 1px dashed color-mix(in srgb, var(--surface-b) 80%, transparent);
  border-radius: 12px;
  background: color-mix(in srgb, var(--surface) 45%, transparent);
}

/* Run group — root has no header; sub-agents indent + show a header bar. */
.ri-run.is-sub {
  margin: 0.45rem 0;
  border-inline-start: 2px solid color-mix(in srgb, var(--accent) 45%, transparent);
  padding-inline-start: 0.6rem;
}
.ri-run-nodes { display: flex; flex-direction: column; gap: 2px; }
.ri-run-head {
  display: flex; align-items: center; gap: 0.4rem;
  padding: 0.3rem 0.45rem; font-size: 0.76rem; font-weight: 600;
  border-radius: 8px;
  background: color-mix(in srgb, var(--accent) 6%, var(--card));
}
.ri-run-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ri-node-spacer { flex: 1 1 auto; min-width: 0; }
.ri-run-badge {
  font-size: 0.58rem; font-weight: 700; letter-spacing: 0.06em;
  padding: 0.08rem 0.38rem; border-radius: 999px;
  color: var(--accent);
  background: rgba(var(--accent-rgb, 37, 99, 235), 0.12);
  border: 1px solid rgba(var(--accent-rgb, 37, 99, 235), 0.22);
}

/* Node — one step in a run. Flat list rows (GitHub-Actions style): no border
 * at rest; a card materialises when the row is expanded or errored. */
.ri-node {
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  transition: background 120ms ease, border-color 120ms ease;
}
.ri-node.is-open {
  border-color: color-mix(in srgb, var(--surface-b) 72%, transparent);
  background: color-mix(in srgb, var(--card) 88%, var(--surface));
}
.ri-node.is-internal {
  background: color-mix(in srgb, var(--surface) 42%, transparent);
}
.ri-node.is-error {
  border-color: color-mix(in srgb, #f87171 32%, transparent);
  background: color-mix(in srgb, #f87171 7%, transparent);
}
.ri-node.is-warn {
  border-color: color-mix(in srgb, var(--warn) 30%, transparent);
  background: color-mix(in srgb, var(--warn) 6%, transparent);
}
.ri-node.is-internal .ri-node-label { color: var(--muted); font-weight: 500; }
.ri-node-head {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.45rem;
  padding: 0.38rem 0.45rem;
  border-radius: 9px;
  font-size: 0.76rem;
}
.ri-node-head.is-expandable { cursor: pointer; }
.ri-node-head:hover { background: color-mix(in srgb, var(--toggle-bg) 65%, transparent); }
.ri-node-leading {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
}
.ri-node-ico { width: 0.8rem; height: 0.8rem; flex-shrink: 0; }
/* Small tinted tile that anchors each row's kind icon. */
.ri-node-ico-tile {
  width: 22px;
  height: 22px;
  border-radius: 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--muted);
  background: color-mix(in srgb, var(--surface) 62%, var(--card));
  border: 1px solid color-mix(in srgb, var(--surface-b) 52%, transparent);
}
.ri-node-ico-tile.is-tool {
  color: var(--accent);
  background: rgba(var(--accent-rgb, 37, 99, 235), 0.09);
  border-color: rgba(var(--accent-rgb, 37, 99, 235), 0.18);
}
.ri-node-ico-tile.is-model {
  color: #7c3aed;
  background: rgba(124, 58, 237, 0.09);
  border-color: rgba(124, 58, 237, 0.18);
}
html.dark .ri-node-ico-tile.is-model { color: #a78bfa; }
.ri-node.is-error .ri-node-ico-tile {
  color: var(--danger);
  background: rgba(248, 113, 113, 0.1);
  border-color: rgba(248, 113, 113, 0.24);
}
html.dark .ri-node.is-error .ri-node-ico-tile { color: #f87171; }
.ri-node.is-warn .ri-node-ico-tile {
  color: #b45309;
  background: rgba(245, 158, 11, 0.1);
  border-color: rgba(245, 158, 11, 0.24);
}
html.dark .ri-node.is-warn .ri-node-ico-tile { color: #fbbf24; }
.ri-node-label {
  color: var(--text);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ri-node.is-tool .ri-node-label {
  font-weight: 600;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.72rem;
}
.ri-node-meta {
  display: inline-grid;
  grid-template-columns: auto 3.8rem 4.2rem 1.65rem 0.85rem;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
  justify-items: end;
}
.ri-node-dur, .ri-node-t, .ri-tok {
  font-size: 0.64rem;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  white-space: nowrap;
}
/* Duration as a subtle chip so it reads apart from the @timeline position. */
.ri-node-dur {
  padding: 0.05rem 0.38rem;
  border-radius: 999px;
  background: color-mix(in srgb, var(--surface) 72%, transparent);
  border: 1px solid color-mix(in srgb, var(--surface-b) 50%, transparent);
  color: color-mix(in srgb, var(--text) 72%, var(--muted));
  font-weight: 600;
}
.ri-node-dur--empty { visibility: hidden; }
.ri-tok {
  color: var(--accent);
  font-weight: 600;
  justify-self: end;
  min-width: 4.5rem;
  text-align: end;
}
html.dark .ri-tok { color: #34d399; }
.ri-node-t { opacity: 0.72; font-size: 0.62rem; }
.ri-dot { width: 7px; height: 7px; border-radius: 50%; flex-shrink: 0; }
.ri-dot.is-ok { background: #34d399; }
.ri-dot.is-error { background: #f87171; }
.ri-dot.is-skipped { background: var(--muted); }
/* Leading per-step status glyph: check (ok) / cross (error) / dash (skipped). */
.ri-status-glyph { display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; }
.ri-status-glyph svg { width: 0.9rem; height: 0.9rem; }
.ri-status-glyph.is-ok { color: var(--accent); }
.ri-status-glyph.is-error { color: var(--danger); }
.ri-status-glyph.is-warn { color: #b45309; }
.ri-status-glyph.is-skipped { color: var(--muted); }
html.dark .ri-status-glyph.is-ok { color: #34d399; }
html.dark .ri-status-glyph.is-error { color: #f87171; }
html.dark .ri-status-glyph.is-warn { color: #fbbf24; }
.ri-node.is-internal .ri-status-glyph.is-ok { opacity: 0.5; }
.ri-chevron {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  transition: transform 150ms ease;
}
.ri-node.is-open .ri-chevron { transform: rotate(90deg); }
.ri-raw-btn {
  border: 1px solid color-mix(in srgb, var(--surface-b) 85%, transparent);
  background: color-mix(in srgb, var(--surface) 40%, transparent);
  color: var(--muted);
  border-radius: 5px;
  cursor: pointer;
  font-size: 0.58rem;
  font-family: ui-monospace, monospace;
  padding: 0 0.28rem;
  line-height: 1.45;
  min-width: 1.45rem;
  /* Revealed on row hover/focus to keep resting rows quiet. */
  opacity: 0;
  transition: opacity 120ms ease;
}
.ri-node-head:hover .ri-raw-btn,
.ri-node.is-open .ri-raw-btn,
.ri-raw-btn:focus-visible { opacity: 1; }
@media (hover: none) { .ri-raw-btn { opacity: 1; } }
.ri-raw-btn:hover { color: var(--accent); border-color: var(--accent); }
.ri-raw-slot, .ri-chevron-slot { width: 1.45rem; height: 1rem; }

.ri-node-detail {
  display: none;
  padding: 0.15rem 0.55rem 0.55rem;
  padding-inline-start: 3.05rem;
  border-top: 1px solid color-mix(in srgb, var(--surface-b) 65%, transparent);
  background: color-mix(in srgb, var(--surface) 35%, transparent);
}
.ri-node.is-open .ri-node-detail { display: block; }
.ri-kv { display: flex; gap: 0.5rem; font-size: 0.7rem; padding: 0.12rem 0; }
.ri-k { color: var(--muted); min-width: 6.5rem; flex-shrink: 0; }
.ri-v { color: var(--text); font-variant-numeric: tabular-nums; word-break: break-word; min-width: 0; flex: 1; }
/* A kv row that carries a code block (tool args / result) stacks vertically. */
.ri-kv:has(.ri-pre) { display: block; }
.ri-kv:has(.ri-pre) .ri-k { display: block; margin-bottom: 0.2rem; }
.ri-pre {
  margin: 0;
  padding: 0.4rem 0.55rem;
  max-height: 240px;
  overflow: auto;
  background: var(--code-bg, #0f172a);
  border: 1px solid var(--code-border, rgba(148, 163, 184, 0.22));
  border-radius: 7px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.7rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--text);
  direction: ltr;
  text-align: left;
  unicode-bidi: isolate;
}

/* Replay transport — play/pause/restart/end + scrubber + speed. */
.ri-transport {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.55rem 0.85rem;
  border-top: 1px solid color-mix(in srgb, var(--surface-b) 82%, transparent);
  background: color-mix(in srgb, var(--toggle-bg) 88%, var(--card));
}
.ri-transport[hidden] { display: none; }
.ri-tp-btn {
  width: 30px; height: 30px; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid color-mix(in srgb, var(--surface-b) 85%, transparent);
  border-radius: 8px;
  background: var(--card);
  color: var(--text);
  cursor: pointer;
}
.ri-tp-btn:hover { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 6%, var(--card)); }
.ri-tp-play { color: var(--accent); }
.ri-tp-btn svg { width: 0.8rem; height: 0.8rem; }
.ri-tp-range {
  flex: 1 1 auto; min-width: 0; height: 5px; cursor: pointer;
  accent-color: var(--accent);
  border-radius: 999px;
}
.ri-tp-time {
  font-size: 0.64rem; font-variant-numeric: tabular-nums;
  color: var(--muted); white-space: nowrap; min-width: 6.8rem; text-align: end;
}
.ri-tp-speed {
  font-size: 0.66rem; padding: 0.2rem 0.4rem; border-radius: 999px;
  border: 1px solid color-mix(in srgb, var(--surface-b) 85%, transparent);
  background: var(--card); color: var(--text); cursor: pointer;
}

.ri-footer {
  flex-shrink: 0;
  padding: 0.55rem 0.85rem;
  border-top: 1px solid color-mix(in srgb, var(--surface-b) 82%, transparent);
  background: color-mix(in srgb, var(--card) 94%, var(--surface));
}
.ri-foot-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.ri-foot-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 24px;
  padding: 0 0.5rem;
  border-radius: 999px;
  font-size: 0.66rem;
  font-weight: 600;
  color: var(--text);
  background: color-mix(in srgb, var(--surface) 55%, var(--card));
  border: 1px solid color-mix(in srgb, var(--surface-b) 80%, transparent);
  font-variant-numeric: tabular-nums;
}
.ri-foot-pill-k {
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted);
}
.ri-foot-pill.is-error {
  color: var(--danger);
  border-color: color-mix(in srgb, #f87171 35%, var(--surface-b));
  background: color-mix(in srgb, #f87171 8%, var(--card));
}
html.dark .ri-foot-pill.is-error { color: #f87171; }

.ri-toast {
  position: absolute; bottom: 3.2rem; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--card);
  padding: 0.35rem 0.75rem; border-radius: 999px; font-size: 0.72rem; font-weight: 600;
  opacity: 0; transition: opacity 150ms ease; pointer-events: none;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.18);
}
.ri-toast.is-show { opacity: 1; }

@media (max-width: 520px) {
  .ri-node-meta {
    grid-template-columns: auto 2.8rem 3.6rem 1.45rem 0.75rem;
    gap: 0.25rem;
  }
  .ri-tok { min-width: 0; font-size: 0.58rem; }
}

@media (min-width: 1100px) {
  body.has-run-inspector .chat-layout,
  body.has-run-inspector #messages {
    padding-inline-end: min(560px, 42vw);
    transition: padding-inline-end 160ms ease;
  }
}
@media (max-width: 768px) {
  .run-inspector { width: 100vw; }
}
