/* messages.css — message bubbles, work container, tools, typing indicator */

.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

  /* Jump-to-latest pill — floats over the message list, anchored to the top
     of the composer so it tracks the textarea's height. */
  .scroll-pill {
    position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%);
    z-index: 6;
    padding: 7px 14px; border-radius: 999px;
    background: var(--surface2); color: var(--text-dim);
    border: 1px solid var(--border); box-shadow: var(--shadow-md);
    font-size: calc(13px * var(--font-scale)); font-weight: 600; font-family: inherit;
    cursor: pointer; transition: all 0.15s ease;
  }
  .scroll-pill:hover { color: var(--accent); border-color: var(--accent); }

  /* Messages */
  .messages { flex: 1; overflow-y: auto; padding: 24px; display: flex; flex-direction: column; gap: 16px; }
  .msg { max-width: 78%; padding: 12px 18px; border-radius: 18px; font-size: calc(16px * var(--font-scale)); line-height: 1.65; word-break: break-word; }
  .msg.user { align-self: flex-end; background: var(--user-msg-bg); color: var(--user-msg-text); border: 1px solid var(--user-msg-border); border-bottom-right-radius: 4px; white-space: pre-wrap; box-shadow: var(--shadow-sm); font-weight: 600; }
  .msg.assistant { align-self: flex-start; background: var(--msg-assistant-bg); border-bottom-left-radius: 4px; box-shadow: var(--shadow-sm), var(--glass-highlight); border: 1px solid var(--glass-border); }
  .msg.error { align-self: center; background: transparent; color: var(--error); font-size: calc(14px * var(--font-scale)); border-radius: 8px; }
  .msg.welcome { background: var(--surface); border: none; border-top: 3px solid var(--accent); box-shadow: var(--shadow-md); max-width: min(900px, 90%); margin: 2rem auto; align-self: center; padding: 2rem 2.5rem; border-radius: var(--radius-lg); }
  .msg.welcome ul, .msg.welcome ol { padding-left: 2rem; }
  .msg.assistant code { background: var(--bg); padding: 2px 5px; border-radius: 3px; font-size: calc(14px * var(--font-scale)); font-family: 'SF Mono', 'Fira Code', 'JetBrains Mono', monospace; }
  .msg.assistant pre { background: var(--bg); padding: 16px; border-radius: var(--radius-md); overflow-x: auto; margin: 10px 0; border: 1px solid var(--border); box-shadow: inset 0 1px 3px rgba(0,0,0,0.04); }
  .msg.assistant .work-container > .work-content > .thinking-block pre { background: none; padding: 6px 8px; border: none; margin: 0; }
  .msg.assistant pre code { background: none; padding: 0; border: none; font-size: calc(14px * var(--font-scale)); font-family: 'SF Mono', 'Fira Code', 'JetBrains Mono', monospace; }
  .msg.assistant a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
  .msg.assistant strong { color: var(--strong); }
  .msg.assistant em { color: var(--em); }
  .msg.assistant h1, .msg.assistant h2, .msg.assistant h3, .msg.assistant h4 { color: var(--accent); margin: 12px 0 6px; font-size: calc(16px * var(--font-scale)); }
  .msg.assistant h1 { font-size: calc(20px * var(--font-scale)); }
  .msg.assistant h2 { font-size: calc(18px * var(--font-scale)); }
  .msg.assistant ul, .msg.assistant ol { padding-left: 20px; margin: 6px 0; }
  .msg.assistant li { margin: 3px 0; }
  .msg.assistant blockquote { border-left: 3px solid var(--accent); padding-left: 10px; margin: 6px 0; color: var(--text-dim); }
  .msg.assistant table { border-collapse: separate; border-spacing: 0; margin: 12px 0; width: 100%; font-size: calc(14.5px * var(--font-scale)); border-radius: var(--radius-md); overflow: hidden; box-shadow: var(--shadow-sm); border: 1px solid var(--border); }
  .msg.assistant th, .msg.assistant td { border: none; border-bottom: 1px solid var(--border); padding: 10px 14px; text-align: left; }
  .msg.assistant th { background: var(--accent-subtle); color: var(--accent); font-size: calc(12px * var(--font-scale)); font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; border-bottom: 2px solid var(--border); }
  .msg.assistant tr:nth-child(even) td { background: var(--surface2); }
  .msg.assistant tbody tr:last-child td { border-bottom: none; }
  .msg.assistant tbody tr:hover td { background: var(--chip-bg-hover); }
  .msg.assistant hr { border: none; border-top: 1px solid var(--border); margin: 10px 0; }
  .msg.assistant p { margin: 6px 0; }
  .msg.assistant p:first-child { margin-top: 0; }
  .msg.assistant p:last-child { margin-bottom: 0; }
  .msg.assistant { position: relative; padding-bottom: 28px; }
  @keyframes msg-enter { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
  .msg-group { display: flex; flex-direction: column; max-width: 78%; animation: msg-enter 0.3s ease-out; }
  .msg-group.user { align-self: flex-end; }
  .msg-group.assistant { align-self: flex-start; }
  .msg-group.error { align-self: center; }
  .msg-group > .msg { max-width: 100%; align-self: auto; }
  /* ===== Tool panel =====
     One panel above the answer ("Searched N sources · N calls · time") with a
     status dot: pulsing while the answer is produced, green once it is done.
     It opens into one line per call (the source's logo, the tool, what it
     was asked for), the thinking and any subagents; a call with a command or
     arguments opens them under its line. */
  .work-container {
      margin: 0;
      border: 1px solid var(--glass-border);
      border-radius: var(--radius-lg);
      background: var(--glass-bg);
      box-shadow: var(--shadow-sm), var(--glass-highlight);
      -webkit-backdrop-filter: var(--glass-blur);
      backdrop-filter: var(--glass-blur);
      overflow: hidden;
      transition: border-color 0.3s ease, box-shadow 0.3s ease;
  }
  .work-container.streaming { border-color: var(--tone-violet-ring); box-shadow: 0 0 0 3px var(--tone-violet-bg), var(--shadow-sm); }
  .work-container summary {
      display: flex; align-items: center; gap: 10px;
      min-height: 52px;
      padding: 12px 18px;
      font-size: calc(14.5px * var(--font-scale));
      font-weight: 600;
      color: var(--text-secondary);
      font-variant-numeric: tabular-nums;
      cursor: pointer;
      user-select: none;
      list-style: none;
  }
  .work-container summary::-webkit-details-marker { display: none; }
  .work-container summary:hover { background: var(--chip-bg); }
  .work-container summary:focus-visible { outline: 2px solid var(--field-focus-border); outline-offset: -2px; }
  /* The status dot */
  .work-container summary::before {
      content: ''; flex: none; width: 9px; height: 9px; border-radius: 50%;
      background: var(--success);
      box-shadow: 0 0 10px var(--tone-success-ring);
  }
  .work-container.streaming summary::before {
      background: var(--tone-violet-text);
      box-shadow: 0 0 0 0 var(--tone-violet-ring);
      animation: work-pulse 1.8s ease-out infinite;
  }
  @keyframes work-pulse { 0% { box-shadow: 0 0 0 0 var(--tone-violet-ring); } 70%, 100% { box-shadow: 0 0 0 8px transparent; } }
  @keyframes work-live { 0%, 100% { opacity: 0.25; } 50% { opacity: 1; } }
  /* The chevron, turned down while the panel is open */
  .work-container summary::after {
      content: ''; flex: none; margin-left: auto;
      width: 8px; height: 8px;
      border-right: 2px solid var(--text-dim); border-bottom: 2px solid var(--text-dim);
      transform: rotate(-45deg);
      transition: transform 0.2s ease;
  }
  .work-container[open] summary::after { transform: rotate(45deg); }
  .work-summary-text { min-width: 0; }
  .work-summary-lead { color: var(--text); font-weight: 800; }
  .work-container[open] summary { border-bottom: 1px solid var(--glass-border); }
  .work-container .work-content { padding: 4px 0; }
  @keyframes slide-in { from { opacity: 0; } to { opacity: 1; } }
  .work-content > .thinking-block,
  .work-content > .tool-call,
  .work-content > .subagent-section { animation: slide-in 0.3s ease-out; }

  .tool-call {
      padding: 8px 18px;
      font-size: calc(13.5px * var(--font-scale));
  }
  .work-content > .tool-call + .tool-call { border-top: 1px solid var(--glass-border); }
  .tool-call.active { background: var(--chip-bg); }
  .tool-call.active .tool-icon { display: inline-block; animation: tool-spin 1s linear infinite; color: var(--tone-violet-text); }
  @keyframes tool-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
  .tool-call.completed .tool-icon { color: var(--success); }
  .tool-call-header { display: flex; align-items: center; gap: 10px; min-width: 0; white-space: nowrap; }
  .tool-call-header .tool-icon { order: 9; flex: none; margin-left: auto; width: 16px; font-size: 13px; text-align: center; color: var(--text-dim); }
  .tool-call-header .tool-name { flex: none; font-weight: 700; color: var(--text); }
  .tool-call-header .tool-detail {
      flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis;
      color: var(--text-dim);
      font-family: 'SF Mono', 'JetBrains Mono', 'Fira Code', monospace; font-size: calc(12.5px * var(--font-scale));
  }
  /* The source's logo (tool-logos.js), tinted with one of the theme's tones */
  .src-logo {
      --logo-bg: var(--tone-neutral-bg); --logo-ring: var(--tone-neutral-ring); --logo-text: var(--tone-neutral-text);
      flex: none;
      display: inline-grid; place-items: center;
      width: 26px; height: 26px; border-radius: 8px;
      background: var(--logo-bg); color: var(--logo-text);
      box-shadow: inset 0 0 0 1px var(--logo-ring);
      font-family: 'SF Mono', 'JetBrains Mono', monospace; font-size: 9.5px; font-weight: 800; letter-spacing: -0.02em;
  }
  .src-logo.tone-accent { --logo-bg: var(--tone-accent-bg); --logo-ring: var(--tone-accent-ring); --logo-text: var(--tone-accent-text); }
  .src-logo.tone-success { --logo-bg: var(--tone-success-bg); --logo-ring: var(--tone-success-ring); --logo-text: var(--tone-success-text); }
  .src-logo.tone-warning { --logo-bg: var(--tone-warning-bg); --logo-ring: var(--tone-warning-ring); --logo-text: var(--tone-warning-text); }
  .src-logo.tone-info { --logo-bg: var(--tone-info-bg); --logo-ring: var(--tone-info-ring); --logo-text: var(--tone-info-text); }
  .src-logo.tone-violet { --logo-bg: var(--tone-violet-bg); --logo-ring: var(--tone-violet-ring); --logo-text: var(--tone-violet-text); }
  .tool-call.has-body > .tool-call-header { cursor: pointer; border-radius: 4px; }
  .tool-call.has-body > .tool-call-header:hover .tool-name { text-decoration: underline; }
  .tool-call.has-body > .tool-call-header:focus-visible { outline: 2px solid var(--field-focus-border); outline-offset: 2px; }
  .tool-call.has-body > .tool-call-header::after { content: '\25B8'; order: 10; flex: none; color: var(--text-dim); font-size: 11px; }
  .tool-call.has-body.expanded > .tool-call-header::after { content: '\25BE'; }
  .tool-call:not(.expanded) > .tool-call-body { display: none; }
  .tool-call-body { margin: 6px 0 2px 36px; }
  .tool-call-body pre { margin: 0; padding: 8px 10px; background: var(--field-bg); border: 1px solid var(--glass-border); border-radius: var(--radius-sm); font-size: 12px; overflow-x: auto; position: relative; }
  .tool-call-body pre code { font-family: 'SF Mono', 'Fira Code', 'JetBrains Mono', monospace; white-space: pre-wrap; word-break: break-all; }
  .tool-call-body .tool-copy-btn { position: absolute; top: 4px; right: 4px; border: 1px solid var(--border); border-radius: 4px; padding: 1px 6px; font-size: 11px; cursor: pointer; opacity: 0; transition: opacity 0.15s; }
  .tool-call-body pre:hover .tool-copy-btn, .tool-call-body .tool-copy-btn:focus-visible { opacity: 0.8; }
  .tool-call-body .tool-copy-btn:hover { opacity: 1; }
  .tool-call-body .tool-copy-btn.copied { opacity: 1; color: var(--success); border-color: var(--success); }

  .thinking-block {
      padding: 8px 18px;
      color: var(--text-dim);
      font-style: italic;
      font-size: calc(14.5px * var(--font-scale));
      line-height: 1.6;
      white-space: pre-wrap;
  }
  .subagent-section {
      margin: 4px 18px 4px 30px;
      padding-left: 12px;
      border-left: 2px solid var(--tone-violet-ring);
  }
  .subagent-section > .tool-call { padding-inline: 4px; }
  .subagent-title {
      font-size: 12px;
      color: var(--text-dim);
      margin-bottom: 4px;
      font-weight: 600;
  }
  .subagent-section.collapsed > :not(.subagent-title) { display: none; }

  .turn-separator { border: none; border-top: 1px solid var(--glass-border); margin: 4px 18px; }
  .output-files { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border); }
  .output-file-chip { display: inline-flex; align-items: center; gap: 4px; background: var(--chip-bg); border-radius: var(--radius-sm); padding: 4px 10px; font-size: 13px; color: var(--accent); text-decoration: none; border: 1px solid var(--border); transition: all 0.2s ease; }
  .output-file-chip:hover { background: var(--chip-bg-hover); border-color: var(--accent); transform: translateY(-1px); }
  .output-file-chip .file-icon { font-size: 13px; }
  .output-file-chip .file-size { color: var(--text-dim); font-size: 12px; }
  /* Download widget */
  .download-widget { margin-top: 10px; padding: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius, 8px); }
  .download-widget-header { font-size: 13px; font-weight: 600; color: var(--text-dim); margin-bottom: 8px; }
  .download-widget-files { display: flex; gap: 6px; flex-wrap: wrap; }
  .msg.assistant .download-all-btn { display: inline-flex; align-items: center; gap: 6px; margin-top: 8px; padding: 6px 14px; background: var(--chip-bg); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius-sm, 4px); font-size: 13px; font-weight: 500; text-decoration: none; transition: background 0.15s; cursor: pointer; }
  .msg.assistant .download-all-btn:hover { background: var(--chip-bg-hover); }
  .download-widget-expired .download-widget-header { color: var(--text-dim); }
  .output-file-chip-expired { color: var(--text-dim); cursor: not-allowed; opacity: 0.55; text-decoration: line-through; }
  .output-file-chip-expired:hover { background: var(--chip-bg); border-color: var(--border); transform: none; }
  .msg.assistant .download-all-btn-expired { color: var(--text-dim); cursor: not-allowed; opacity: 0.55; text-decoration: line-through; }
  .msg.assistant .download-all-btn-expired:hover { background: var(--chip-bg); }
  .msg.assistant img { max-width: 100%; border-radius: 4px; }
  .msg.assistant details:not(.work-container) { margin: 6px 0; border: 1px solid var(--border); border-radius: var(--radius-md); overflow: hidden; }
  .msg.assistant details:not(.work-container) summary { padding: 8px 12px; cursor: pointer; font-size: 14px; background: var(--bg); user-select: none; }
  .msg.assistant details:not(.work-container) summary:hover { background: var(--surface); }
  .msg.assistant details:not(.work-container) summary b { color: var(--accent); }
  .msg.assistant details:not(.work-container) summary em { color: var(--error); }
  .msg.assistant details:not(.work-container) > pre, .msg.assistant details:not(.work-container) > p { margin: 0; border-top: 1px solid var(--border); }
  .msg.assistant details:not(.work-container) > pre { border-radius: 0; border: none; }
  .msg.assistant details:not(.work-container)[open] summary { border-bottom: 1px solid var(--border); }

  /* Mermaid diagrams */
  .mermaid { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-md); padding: 16px; margin: 8px 0; overflow-x: auto; }
  .mermaid svg { max-width: 100% !important; width: 100% !important; height: auto !important; }
  .msg.assistant:has(.mermaid), .msg.assistant:has(.drawio-container) { max-width: 100%; width: 100%; }
  /* Chart.js */
  .chart-container { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-md); padding: 12px; margin: 8px 0; max-width: 600px; }
  .chart-error { color: var(--error); font-size: 14px; padding: 8px; background: rgba(255,167,38,0.1); border-radius: 4px; margin: 8px 0; }
  /* Sortable table headers */
  .msg.assistant th[data-sortable] { cursor: pointer; user-select: none; }
  .msg.assistant th[data-sortable]:hover { background: var(--surface2); }
  .msg.assistant th[data-sortable]::after { content: ' \2195'; opacity: 0.4; font-size: 10px; }
  .msg.assistant th[data-sortable].sort-asc::after { content: ' \25B2'; opacity: 0.8; }
  .msg.assistant th[data-sortable].sort-desc::after { content: ' \25BC'; opacity: 0.8; }
  /* Interactive markdown tables */
  .md-table-wrap { margin: 12px 0; }
  .md-table-toolbar { display: flex; justify-content: flex-end; gap: 6px; margin-bottom: 4px; }
  .md-table-btn { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 3px 10px; font-family: inherit; font-size: 12px; color: var(--text-dim); cursor: pointer; opacity: 0.75; transition: all 0.2s ease; }
  .md-table-btn:hover { opacity: 1; border-color: var(--accent); color: var(--accent); }
  .md-table-btn.done { opacity: 1; border-color: var(--success); color: var(--success); }
  .md-table-scroll { overflow-x: auto; border-radius: var(--radius-md); }
  .md-table-scroll > table { margin: 0 !important; }
  .md-table-modal { position: fixed; inset: 0; z-index: 1000; display: none; align-items: center; justify-content: center; }
  .md-table-modal.open { display: flex; }
  .md-table-modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.6); backdrop-filter: blur(2px); }
  .md-table-modal-content { position: relative; width: 92vw; height: 92vh; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-md); box-shadow: 0 20px 60px rgba(0,0,0,0.4); display: flex; flex-direction: column; overflow: hidden; }
  .md-table-modal-header { display: flex; justify-content: flex-end; align-items: center; gap: 6px; padding: 10px 12px; border-bottom: 1px solid var(--border); background: var(--surface2); }
  .md-table-modal-close { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 3px 10px; font-family: inherit; font-size: 14px; color: var(--text-dim); cursor: pointer; }
  .md-table-modal-close:hover { color: var(--accent); border-color: var(--accent); }
  .md-table-modal-body { flex: 1; overflow: auto; padding: 16px 20px; }
  .md-table-modal-body table { width: 100%; }
  /* Draw.io diagrams */
  .drawio-container { position: relative; background: #f0f0f0; border: 1px solid var(--border); border-radius: var(--radius-md); padding: 16px; margin: 8px 0; overflow-x: auto; min-height: 300px; }
  .drawio-container .geDiagramContainer { background: transparent !important; min-width: 600px !important; min-height: 400px !important; }
  img.geAdaptiveAsset[style*="position: fixed"] { display: none !important; }
  .drawio-download { position: absolute; top: 8px; right: 8px; background: var(--surface2); color: var(--text-dim); border: 1px solid var(--border); border-radius: 4px; padding: 3px 8px; font-family: inherit; font-size: 13px; cursor: pointer; z-index: 1; }
  .drawio-download:hover { color: var(--text); border-color: var(--accent); }

  /* Marp slide viewer */
  .msg.assistant:has(.marp-viewer) { max-width: 100%; width: 100%; }
  .marp-viewer { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-md); margin: 8px 0; overflow: hidden; outline: none; }
  .marp-slide-host { position: relative; width: 100%; aspect-ratio: 16/9; overflow: hidden; background: #fff; }
  .marp-controls { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 8px; border-top: 1px solid var(--border); background: var(--surface); }
  .marp-nav-btn { background: var(--surface2); color: var(--text-dim); border: 1px solid var(--border); border-radius: 4px; padding: 4px 12px; font-size: 14px; cursor: pointer; font-family: inherit; }
  .marp-nav-btn:hover { color: var(--text); border-color: var(--accent); }
  .marp-nav-btn:disabled { opacity: 0.3; cursor: default; }
  .marp-counter { font-size: 13px; color: var(--text-dim); font-variant-numeric: tabular-nums; min-width: 50px; text-align: center; }
  .marp-fullscreen-btn { background: var(--surface2); color: var(--text-dim); border: 1px solid var(--border); border-radius: 4px; padding: 4px 8px; font-size: 14px; cursor: pointer; margin-left: auto; }
  .marp-fullscreen-btn:hover { color: var(--text); border-color: var(--accent); }
  .marp-viewer:fullscreen { background: #000; }
  .marp-viewer:fullscreen .marp-slide-host { height: calc(100vh - 44px); aspect-ratio: unset; }
  .marp-viewer:fullscreen .marp-controls { background: #111; border-color: #333; }
  .marp-viewer:fullscreen .marp-nav-btn,
  .marp-viewer:fullscreen .marp-fullscreen-btn { background: #222; color: #aaa; border-color: #444; }
  .marp-viewer:fullscreen .marp-counter { color: #aaa; }

  .typing { align-self: flex-start; font-size: 14px; padding: 8px 14px; display: flex; flex-direction: column; gap: 4px; }
  .typing .typing-row { display: flex; align-items: center; gap: 6px; }
  .typing .bars { display: flex; gap: 0; }
  .typing .activity-text { color: var(--text-dim); font-size: 12px; white-space: nowrap; transition: opacity 0.15s; }
  .typing .elapsed-timer { color: var(--text-dim); font-size: 11px; font-variant-numeric: tabular-nums; font-family: monospace; }

  .typing .bars span { display: inline-block; width: 8px; height: 14px; background: var(--accent); border-radius: 0; opacity: 0.1; }

  /* Wave — classic sequential pulse */
  .typing.anim-wave .bars span { animation: pulse 1.4s ease-in-out infinite; }
  .typing.anim-wave .bars span:nth-child(2) { animation-delay: 0.15s; }
  .typing.anim-wave .bars span:nth-child(3) { animation-delay: 0.3s; }
  .typing.anim-wave .bars span:nth-child(4) { animation-delay: 0.45s; }
  .typing.anim-wave .bars span:nth-child(5) { animation-delay: 0.6s; }
  @keyframes pulse { 0%, 80%, 100% { opacity: 0.15; } 40% { opacity: 0.8; } }

  /* Random — JS-driven random opacity per bar */
  .typing.anim-random .bars span { transition: opacity 0.3s ease-in-out; }

  /* Scanner — a bright glow sweeps back and forth across bars */
  .typing.anim-scanner .bars span { opacity: 0.12; animation: scanner 1.6s ease-in-out infinite; }
  .typing.anim-scanner .bars span:nth-child(2) { animation-delay: 0.15s; }
  .typing.anim-scanner .bars span:nth-child(3) { animation-delay: 0.3s; }
  .typing.anim-scanner .bars span:nth-child(4) { animation-delay: 0.45s; }
  .typing.anim-scanner .bars span:nth-child(5) { animation-delay: 0.6s; }
  @keyframes scanner { 0%, 100% { opacity: 0.12; } 20% { opacity: 0.9; } 40% { opacity: 0.12; } }

  /* Seesaw — odd bars up while even bars down, alternating */
  .typing.anim-seesaw .bars span { opacity: 0.7; animation: seesawUp 1s ease-in-out infinite; }
  .typing.anim-seesaw .bars span:nth-child(2n) { animation-name: seesawDown; }
  @keyframes seesawUp { 0%, 100% { transform: translateY(-4px); } 50% { transform: translateY(4px); } }
  @keyframes seesawDown { 0%, 100% { transform: translateY(4px); } 50% { transform: translateY(-4px); } }

  /* Bounce — bars jump up and down at staggered times */
  .typing.anim-bounce .bars span { opacity: 0.7; animation: bounce 0.8s ease-in-out infinite; }
  .typing.anim-bounce .bars span:nth-child(2) { animation-delay: 0.1s; }
  .typing.anim-bounce .bars span:nth-child(3) { animation-delay: 0.2s; }
  .typing.anim-bounce .bars span:nth-child(4) { animation-delay: 0.3s; }
  .typing.anim-bounce .bars span:nth-child(5) { animation-delay: 0.4s; }
  @keyframes bounce { 0%, 100% { transform: translateY(0); } 40% { transform: translateY(-8px); } }

  /* Equalizer — wavy height animation like a music visualizer */
  .typing.anim-equalizer .bars { align-items: flex-end; height: 14px; }
  .typing.anim-equalizer .bars span { opacity: 0.75; animation: eq 1.8s ease-in-out infinite; }
  .typing.anim-equalizer .bars span:nth-child(2) { animation-delay: 0.25s; }
  .typing.anim-equalizer .bars span:nth-child(3) { animation-delay: 0.5s; }
  .typing.anim-equalizer .bars span:nth-child(4) { animation-delay: 0.75s; }
  .typing.anim-equalizer .bars span:nth-child(5) { animation-delay: 1.0s; }
  @keyframes eq { 0%, 100% { height: 3px; } 50% { height: 14px; } }

  /* Popcorn — JS-driven, bars randomly pop with scale */
  .typing.anim-popcorn .bars span { opacity: 0.3; transition: transform 0.15s ease-out, opacity 0.15s ease-out; }

  /* Morse — rhythmic dot-dash pattern, each bar offset */
  .typing.anim-morse .bars span { animation: morse 2s step-end infinite; }
  .typing.anim-morse .bars span:nth-child(2) { animation-delay: 0.25s; }
  .typing.anim-morse .bars span:nth-child(3) { animation-delay: 0.6s; }
  .typing.anim-morse .bars span:nth-child(4) { animation-delay: 0.9s; }
  .typing.anim-morse .bars span:nth-child(5) { animation-delay: 1.3s; }
  @keyframes morse { 0% { opacity: 0.8; } 8% { opacity: 0.1; } 16% { opacity: 0.8; } 24% { opacity: 0.1; } 40% { opacity: 0.8; } 60% { opacity: 0.1; } 100% { opacity: 0.1; } }

  /* Domino — bars tip over one by one then reset */
  .typing.anim-domino .bars span { opacity: 0.7; animation: domino 2s ease-in-out infinite; transform-origin: bottom right; }
  .typing.anim-domino .bars span:nth-child(2) { animation-delay: 0.2s; }
  .typing.anim-domino .bars span:nth-child(3) { animation-delay: 0.4s; }
  .typing.anim-domino .bars span:nth-child(4) { animation-delay: 0.6s; }
  .typing.anim-domino .bars span:nth-child(5) { animation-delay: 0.8s; }
  @keyframes domino { 0%, 30% { transform: rotate(0deg); opacity: 0.7; } 15% { transform: rotate(90deg); opacity: 0.15; } }

  /* Awaiting interactive input — runner is parked on a TUI picker. */
  .msg.assistant.awaiting-input { border-color: var(--tone-warning-ring); box-shadow: 0 0 0 1px var(--tone-warning-ring); }
  .awaiting-input-label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--tone-warning-text); margin-bottom: 6px; font-weight: 600; }
  .awaiting-input-body { line-height: 1.5; }

  /* ===== Conversation view: page header, question, tool panel, answer panel, composer =====
     One reading column of about 900px of text, centred on wide screens; the
     composer lines up with it. */
  .messages {
      --chat-column: 960px;
      padding: 32px max(24px, calc((100% - var(--chat-column)) / 2)) 64px;
      gap: 28px;
      overscroll-behavior: contain;
      /* The conversation fades out under the composer. */
      -webkit-mask-image: linear-gradient(to bottom, #000 calc(100% - 40px), transparent);
      mask-image: linear-gradient(to bottom, #000 calc(100% - 40px), transparent);
  }
  .messages > * { flex: none; }

  /* The page header (chat-head.js): the title on one line, a quiet row under
     it (status, agent, start time, message count) with Copy link on its
     right, then a thin divider. Shown once the conversation holds a
     question. */
  .chat-head {
      display: flex; flex-direction: column; gap: 10px;
      padding-bottom: 16px;
      border-bottom: 1px solid var(--border);
  }
  .messages:not(:has(.msg.user)) > .chat-head { display: none; }
  .chat-title {
      margin: 0;
      color: var(--text);
      font-size: calc(22px * var(--font-scale)); font-weight: 700; line-height: 1.3; letter-spacing: -0.01em;
      white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .chat-meta {
      display: flex; flex-wrap: wrap; align-items: center; gap: 6px 16px;
      color: var(--text-dim);
      font-size: calc(13px * var(--font-scale)); font-weight: 600; line-height: 1.4;
  }
  .chat-meta-item { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
  .chat-meta-item[hidden] { display: none; }
  .chat-agent .agent-logo { width: 20px; height: 20px; border-radius: 6px; }
  .chat-agent .agent-logo svg { width: 13px; height: 13px; }
  .chat-agent-name { color: var(--text); font-family: 'SF Mono', 'JetBrains Mono', 'Fira Code', monospace; }
  .chat-copy-link.btn-secondary {
      flex: none; margin-left: auto;
      gap: 6px; padding: 6px 12px; border-radius: var(--radius-md);
      font-size: calc(13px * var(--font-scale)); font-weight: 700;
  }
  .chat-copy-link:disabled { cursor: default; }
  /* The status: a dot and a word in its tone, no chip. */
  .chat-status::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
  .chat-status[data-status="running"] { color: var(--tone-violet-text); }
  .chat-status[data-status="awaiting"], .chat-status[data-status="interrupted"] { color: var(--tone-warning-text); }
  .chat-status[data-status="done"] { color: var(--tone-success-text); }
  .chat-status[data-status="failed"] { color: var(--tone-danger-text); }
  .chat-status[data-status="running"]::before { animation: work-live 1.2s ease-in-out infinite; }

  /* The question: a bubble on the right with its time inside. */
  .msg-group.user, .messages > .msg.user { max-width: min(80%, 760px); }
  .msg.user {
      padding: 14px 20px;
      border-radius: 22px 22px 6px 22px;
      line-height: 1.55;
      font-weight: 600;
  }
  .msg.user[data-time]::after,
  .msg.awaiting-input[data-time]::after {
      content: attr(data-time);
      display: block; margin-top: 6px;
      color: var(--text-dim);
      font-size: calc(12px * var(--font-scale)); font-weight: 600;
      text-align: right; white-space: normal;
  }

  /* A turn: the tool panel above the answer panel. */
  .msg-group.assistant, .messages > .msg.assistant { align-self: stretch; max-width: 100%; }
  .msg-group.assistant { gap: 16px; }

  /* The answer: a panel as wide as the column, larger text, headings in the
     accent colour. */
  .msg.assistant:not(.welcome) {
      padding: 26px 30px 18px;
      border-radius: 22px;
      background: var(--glass-bg);
      border: 1px solid var(--glass-border);
      box-shadow: var(--shadow-md), var(--glass-highlight);
      font-size: calc(17px * var(--font-scale));
      line-height: 1.7;
  }
  .messages .msg.assistant.awaiting-input {
      padding-bottom: 18px;
      border-color: var(--tone-warning-ring);
      box-shadow: 0 0 0 1px var(--tone-warning-ring), var(--shadow-md);
  }
  .msg.assistant p { margin: 0 0 12px; }
  .msg.assistant li { margin: 5px 0; }
  .msg.assistant ul, .msg.assistant ol { margin: 0 0 12px; padding-left: 24px; }
  .msg.assistant h1, .msg.assistant h2, .msg.assistant h3, .msg.assistant h4 {
      color: var(--accent); font-weight: 800; line-height: 1.3; letter-spacing: -0.01em; margin: 28px 0 10px;
  }
  .msg.assistant h1 { font-size: calc(23px * var(--font-scale)); }
  .msg.assistant h2 { font-size: calc(20px * var(--font-scale)); }
  .msg.assistant h3 { font-size: calc(17.5px * var(--font-scale)); }
  .msg.assistant h4 { font-size: calc(14px * var(--font-scale)); text-transform: uppercase; letter-spacing: 0.06em; }
  .msg.assistant > :is(h1, h2, h3, h4):first-child,
  .msg.assistant .stream-text-content > :is(h1, h2, h3, h4):first-child { margin-top: 0; }
  .msg.assistant hr { border-top-color: var(--glass-border); margin: 20px 0; }

  /* Links and code */
  .msg.assistant a {
      color: var(--accent); font-weight: 600;
      text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 3px;
      text-decoration-color: color-mix(in srgb, var(--accent) 45%, transparent);
      overflow-wrap: anywhere;
  }
  .msg.assistant a:hover { text-decoration-color: currentColor; }
  .msg.assistant :not(pre) > code {
      padding: 1px 6px;
      border-radius: 6px;
      background: var(--chip-bg); color: var(--text);
      box-shadow: inset 0 0 0 1px var(--glass-border);
      font-size: 0.84em;
      overflow-wrap: anywhere;
  }
  .code-block {
      margin: 14px 0;
      border: 1px solid var(--glass-border);
      border-radius: var(--radius-md);
      background: var(--field-bg);
      overflow: hidden;
  }
  .code-head {
      display: flex; align-items: center; justify-content: space-between; gap: 8px;
      padding: 5px 6px 5px 14px;
      border-bottom: 1px solid var(--glass-border);
      background: var(--chip-bg);
  }
  .code-lang { color: var(--text-dim); font-size: 11px; font-weight: 800; letter-spacing: 0.12em; text-transform: uppercase; }
  .msg.assistant .code-block > pre { margin: 0; padding: 14px 16px; border: 0; border-radius: 0; background: transparent; box-shadow: none; }
  .msg.assistant pre code { font-size: calc(13.5px * var(--font-scale)); line-height: 1.6; }

  /* Small icon buttons: code and table headers. The label shows while the
     button reports what it did ("Copied"). */
  .code-copy, .md-table-btn {
      display: inline-flex; align-items: center; justify-content: center; gap: 6px;
      min-width: 32px; height: 30px; padding: 0 8px;
      border: 1px solid var(--btn-secondary-border); border-radius: 9px;
      background: var(--btn-secondary-bg); color: var(--btn-secondary-text);
      font: inherit; font-size: 12px; font-weight: 700;
      cursor: pointer; opacity: 1;
  }
  .code-copy:hover, .md-table-btn:hover { border-color: var(--accent); color: var(--btn-secondary-text); opacity: 1; }
  .code-copy:focus-visible, .md-table-btn:focus-visible { outline: 2px solid var(--field-focus-border); outline-offset: 1px; }
  .md-table-btn .btn-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .md-table-btn.done .btn-label, .md-table-btn.failed .btn-label { position: static; width: auto; height: auto; clip: auto; }
  .code-copy.done, .md-table-btn.done { border-color: var(--tone-success-ring); color: var(--tone-success-text); }
  .code-copy.failed, .md-table-btn.failed { border-color: var(--tone-danger-ring); color: var(--tone-danger-text); }

  /* Warning and note blocks (chat.js decorateCallouts), as the mockup's caveats. */
  .callout {
      --callout-bg: var(--tone-info-bg); --callout-ring: var(--tone-info-ring); --callout-text: var(--tone-info-text);
      position: relative;
      margin: 12px 0; padding: 12px 16px 12px 48px;
      background: var(--callout-bg);
      border: 0;
      border-radius: var(--radius-md);
      box-shadow: inset 0 0 0 1px var(--callout-ring);
      font-size: 0.94em;
  }
  .callout::before {
      content: 'i';
      position: absolute; left: 14px; top: 13px;
      display: grid; place-items: center;
      width: 22px; height: 22px; border-radius: 50%;
      box-shadow: inset 0 0 0 1.5px var(--callout-text);
      color: var(--callout-text);
      font-size: 12px; font-weight: 900; line-height: 1;
  }
  .callout-warning { --callout-bg: var(--tone-warning-bg); --callout-ring: var(--tone-warning-ring); --callout-text: var(--tone-warning-text); }
  .callout-caution { --callout-bg: var(--tone-danger-bg); --callout-ring: var(--tone-danger-ring); --callout-text: var(--tone-danger-text); }
  .callout-important { --callout-bg: var(--tone-violet-bg); --callout-ring: var(--tone-violet-ring); --callout-text: var(--tone-violet-text); }
  .callout-tip { --callout-bg: var(--tone-success-bg); --callout-ring: var(--tone-success-ring); --callout-text: var(--tone-success-text); }
  .callout-warning::before, .callout-caution::before, .callout-important::before { content: '!'; }
  .callout-tip::before { content: '\2713'; }
  .callout-title {
      margin-bottom: 2px; color: var(--callout-text);
      font-size: calc(12px * var(--font-scale)); font-weight: 800; text-transform: uppercase; letter-spacing: 0.06em;
  }
  .msg.assistant .callout p { margin: 4px 0; }
  .msg.assistant .callout > :last-child { margin-bottom: 0; }
  .msg.assistant .callout strong { color: var(--callout-text); }

  /* Tables: a bordered card; its row count, Copy, Download CSV and Fullscreen
     in a header row. */
  .md-table-wrap {
      margin: 14px 0;
      border: 1px solid var(--glass-border);
      border-radius: var(--radius-md);
      overflow: hidden;
      background: var(--glass-bg);
      box-shadow: none;
  }
  .md-table-toolbar {
      display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: 6px;
      margin: 0; padding: 6px 6px 6px 14px;
      background: var(--chip-bg);
      border-bottom: 1px solid var(--glass-border);
  }
  .md-table-info {
      margin-right: auto;
      color: var(--text-dim);
      font-size: 11px; font-weight: 800; letter-spacing: 0.12em; text-transform: uppercase;
  }
  .md-table-scroll { border-radius: 0; }
  .msg.assistant .md-table-scroll > table { margin: 0; border: none; border-radius: 0; box-shadow: none; font-size: calc(15px * var(--font-scale)); }
  .msg.assistant .md-table-wrap th {
      background: transparent; color: var(--text-dim);
      font-size: calc(11.5px * var(--font-scale)); letter-spacing: 0.08em;
      border-bottom: 1px solid var(--glass-border);
  }
  .msg.assistant .md-table-wrap td { border-bottom-color: var(--glass-border); }
  .msg.assistant .md-table-wrap tr:nth-child(even) td { background: transparent; }
  .msg.assistant .md-table-wrap tbody tr:hover td { background: var(--chip-bg); }

  /* The footer under an answer: Copy, Publish privately, then when, how long
     and by which agent. */
  .answer-foot {
      display: flex; align-items: center; flex-wrap: wrap; gap: 8px;
      margin-top: 20px; padding-top: 14px;
      border-top: 1px solid var(--glass-border);
      font-size: 13px; line-height: 1.4;
  }
  .answer-btn {
      display: inline-flex; align-items: center; gap: 6px;
      padding: 6px 12px;
      border: 1px solid var(--btn-secondary-border); border-radius: 10px;
      background: var(--btn-secondary-bg); color: var(--btn-secondary-text);
      box-shadow: var(--glass-highlight);
      font: inherit; font-size: 13px; font-weight: 700;
      cursor: pointer;
  }
  .answer-btn:hover:not(:disabled) { border-color: var(--accent); }
  .answer-btn:focus-visible { outline: 2px solid var(--field-focus-border); outline-offset: 1px; }
  .answer-btn:disabled { cursor: default; opacity: 0.7; }
  .answer-btn.done { border-color: var(--tone-success-ring); color: var(--tone-success-text); }
  .answer-btn.failed { border-color: var(--tone-danger-ring); color: var(--tone-danger-text); }
  .answer-meta { margin-left: auto; color: var(--text-dim); font-size: 12.5px; font-weight: 600; text-align: right; }

  /* Working state while the answer is produced: the panel shows once text
     arrives, rings softly and ends in a blinking caret. */
  .messages:has(> .typing) .msg.assistant.answer-live:has(> .stream-text-content:empty):not(:has(> .stream-gap)) { display: none; }
  .messages:has(> .typing) .msg.assistant.answer-live {
      border-color: var(--tone-violet-ring);
      box-shadow: 0 0 0 3px var(--tone-violet-bg), var(--shadow-md);
  }
  .messages:has(> .typing) .msg.assistant.answer-live .stream-text-content > :is(p, h1, h2, h3, h4):last-child::after,
  .messages:has(> .typing) .msg.assistant.answer-live .stream-text-content > :is(ul, ol):last-child > li:last-child::after {
      content: '';
      display: inline-block; width: 0.5em; height: 1.05em; margin-left: 3px; vertical-align: -0.18em;
      border-radius: 2px;
      background: var(--tone-violet-text);
      animation: answer-caret 1s steps(2, jump-none) infinite;
  }
  @keyframes answer-caret { 0% { opacity: 1; } 100% { opacity: 0; } }
  .typing { padding: 2px 4px; color: var(--text-dim); }
  .typing .bars span { background: var(--tone-violet-text); }

  /* Errors: a panel with the message's formatting rendered and Retry. */
  .msg-group.error {
      position: relative;
      align-self: stretch; max-width: 100%;
      flex-direction: row; flex-wrap: wrap; align-items: center; gap: 10px 16px;
      padding: 14px 16px 14px 52px;
      border-radius: var(--radius-lg);
      background: var(--tone-danger-bg);
      box-shadow: inset 0 0 0 1px var(--tone-danger-ring);
  }
  .msg-group.error::before {
      content: '!';
      position: absolute; left: 16px; top: 15px;
      display: grid; place-items: center;
      width: 22px; height: 22px; border-radius: 50%;
      box-shadow: inset 0 0 0 1.5px var(--tone-danger-text);
      color: var(--tone-danger-text);
      font-size: 12px; font-weight: 900; line-height: 1;
  }
  .msg-group.error > .msg.error {
      flex: 1 1 260px; min-width: 0;
      padding: 0;
      background: none; border-radius: 0;
      color: var(--text);
      font-size: calc(15px * var(--font-scale)); line-height: 1.55;
  }
  .msg.error p { margin: 0 0 6px; }
  .msg.error > :last-child { margin-bottom: 0; }
  .msg.error :not(pre) > code { padding: 1px 6px; border-radius: 6px; background: var(--chip-bg); font-size: 0.88em; font-family: 'SF Mono', 'JetBrains Mono', monospace; overflow-wrap: anywhere; }
  .msg.error pre { margin: 6px 0; padding: 8px 10px; border-radius: var(--radius-sm); background: var(--field-bg); overflow-x: auto; font-size: 13px; }
  .msg.error a { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }
  .msg.error strong { color: var(--tone-danger-text); }
  .error-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 12px; }
  .error-retry.btn-secondary { padding: 7px 16px; font-size: 14px; font-weight: 700; }
  .error-note:empty { display: none; }
  .error-note { flex: 1 1 200px; color: var(--text-secondary); font-size: 13px; }

  /* The composer: as on Home, a glass box with a gradient edge holding the
     field, then attach and Send. It stays at the bottom over the conversation's
     fade (the message list's mask above). */
  #inputArea {
      flex-direction: column;
      position: sticky; bottom: 0;
      margin-top: -40px;
      padding: 0 max(24px, calc((100% - 960px) / 2)) max(18px, env(safe-area-inset-bottom));
      background: transparent;
      border: 0;
      box-shadow: none;
  }
  .input-area > .file-previews, .input-area > .input-row { width: 100%; max-width: 960px; margin-inline: auto; }
  #inputArea .input-row {
      position: relative;
      flex-direction: column; gap: 2px;
      padding: 8px;
      border-radius: 24px;
      background: var(--glass-bg-strong);
      box-shadow: var(--shadow-lg), var(--glass-highlight);
      -webkit-backdrop-filter: var(--glass-blur);
      backdrop-filter: var(--glass-blur);
  }
  #inputArea .input-row::before {
      content: '';
      position: absolute; inset: 0;
      padding: 1px; border-radius: inherit;
      background: linear-gradient(120deg, var(--tone-accent-ring), var(--tone-violet-ring), var(--tone-info-ring));
      -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
      -webkit-mask-composite: xor;
      mask-composite: exclude;
      pointer-events: none;
  }
  #inputArea .input-row:focus-within::before { padding: 1.5px; background: linear-gradient(120deg, var(--accent), var(--tone-violet-text), var(--tone-info-text)); }
  #inputArea .input-row textarea {
      flex: none;
      width: 100%;
      min-height: 52px; max-height: 200px;
      padding: 10px 12px 4px;
      border: 0; border-radius: 16px;
      background: transparent; color: var(--text);
      box-shadow: none;
      font-family: inherit; font-size: calc(16px * var(--font-scale)); font-weight: 500; line-height: 1.5;
  }
  #inputArea .input-row textarea:hover, #inputArea .input-row textarea:focus { border: 0; background: transparent; box-shadow: none; outline: none; }
  .input-bar { display: flex; align-items: center; gap: 8px; padding: 2px; }
  #inputArea .attach-btn { width: 40px; height: 40px; border-radius: 12px; align-self: auto; }
  #inputArea .input-row button.send {
      margin-left: auto; align-self: auto;
      display: inline-flex; align-items: center; gap: 8px;
      height: 42px; padding: 0 20px; border-radius: 14px;
      font-size: 15px;
  }
  #sendBtn:not(.stop):not(.stopping):not(.awaiting)::after { content: '\2192'; font-size: 16px; line-height: 1; }

  /* Very narrow screens: the column uses the full width. Placed after the
     base .msg/.msg-group rules so it wins on source order. The centered
     welcome card keeps its own width via higher specificity. */
  @media (max-width: 520px) {
    .messages { padding: 18px 12px 56px; gap: 20px; }
    .msg-group,
    .msg,
    .msg-group.user,
    .messages > .msg.user { max-width: 100%; }
    .chat-title { font-size: calc(20px * var(--font-scale)); }
    .msg.assistant:not(.welcome) { padding: 18px 16px 14px; border-radius: 18px; font-size: calc(16px * var(--font-scale)); }
    .work-container summary { padding: 10px 14px; }
    .tool-call { padding-inline: 14px; }
    .tool-call-body { margin-left: 0; }
    .answer-meta { margin-left: 0; flex-basis: 100%; text-align: left; }
    #inputArea { padding: 0 10px max(10px, env(safe-area-inset-bottom)); }
    #inputArea .input-row { border-radius: 20px; }
  }
