:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #1c1f24;
  --ink-2: #4b5563;
  --ink-3: #6b7280;
  --line: #e3e6eb;
  --line-2: #eef0f3;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --user-bubble: #2563eb;
  --user-ink: #ffffff;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --warn: #b45309;
  --ok: #047857;
  --rail-channel: #f59e0b;
  --rail-article: #2563eb;
  --rail-match: #059669;
  --rail-error: #dc2626;
  --rail-system: #94a3b8;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 4px 12px rgba(15, 23, 42, 0.04);
  --gap: 12px;
  --fs-sm: 12.5px;
  --fs-md: 14px;
  --fs-lg: 16px;
  --font-sys: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-sys);
  font-size: var(--fs-md);
  line-height: 1.45;
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
}

/* ─── Top bar ─────────────────────────────────────────────────────────── */

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.topbar h1 {
  font-size: 15px;
  margin: 0 8px 0 0;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.uid-field {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: var(--fs-sm);
  color: var(--ink-2);
}
.uid-field input {
  font: inherit;
  font-size: 13px;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg);
  width: 180px;
}
.uid-field input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: transparent;
}

/* ─── Buttons ─────────────────────────────────────────────────────────── */

.btn {
  font: inherit;
  font-size: 13px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}
.btn:hover:not(:disabled) { background: var(--bg); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { background: #1d4ed8; }
.btn-danger { border-color: #fecaca; color: var(--danger); background: var(--panel); }
.btn-danger:hover:not(:disabled) { background: var(--danger-soft); }
.btn-ghost { border-color: transparent; color: var(--ink-2); background: transparent; }
.btn-ghost:hover:not(:disabled) { background: var(--bg); }
.btn-sm { padding: 3px 8px; font-size: 12px; }
.topbar .btn-ghost[aria-pressed="true"] { background: var(--bg); }

/* ─── Layout ──────────────────────────────────────────────────────────── */

.layout {
  display: grid;
  grid-template-columns: 1fr 360px;
  min-height: 0;
  transition: grid-template-columns 0.18s ease;
}
.layout[data-dev="off"] { grid-template-columns: 1fr 0; }
.layout[data-dev="off"] .dev { display: none; }

/* ─── Chat column ─────────────────────────────────────────────────────── */

.chat {
  overflow-y: auto;
  padding: 18px 16px 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.chat:empty::before {
  content: "Send a message to start.";
  color: var(--ink-3);
  font-size: var(--fs-sm);
  margin: auto;
}

.turn { display: flex; flex-direction: column; gap: 8px; max-width: 100%; }
.turn.user { align-items: flex-end; }
.turn.assistant, .turn.system { align-items: flex-start; }

.bubble {
  max-width: min(720px, 78%);
  padding: 10px 14px;
  border-radius: 14px;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.turn.user .bubble {
  background: var(--user-bubble);
  color: var(--user-ink);
  border-bottom-right-radius: 4px;
}
.turn.assistant .bubble {
  background: var(--panel);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow);
  max-width: min(780px, 92%);
}
.turn.assistant .bubble:empty {
  display: none;
}
.turn.system .bubble {
  background: transparent;
  color: var(--ink-3);
  font-size: var(--fs-sm);
  font-style: italic;
  padding: 4px 6px;
  border-left: 3px solid var(--rail-system);
  border-radius: 0;
}
.turn.system.error .bubble {
  color: var(--danger);
  border-left-color: var(--rail-error);
  font-style: normal;
}

/* assistant turn metadata strip (below the text, holds chips/cards/status) */
.assistant-extras {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: min(780px, 92%);
}
.assistant-extras:empty { display: none; }

/* ─── Channel chip ────────────────────────────────────────────────────── */

.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px;
  border-radius: 999px;
  background: #fff7ed;
  color: #9a3412;
  border: 1px solid #fed7aa;
  font-size: var(--fs-sm);
  width: fit-content;
  max-width: 100%;
}
.chip .chip-label { font-weight: 600; }
.chip .chip-reason { color: #b45309; font-weight: 400; }

/* ─── Cards (article, match) ──────────────────────────────────────────── */

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-left: 3px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  box-shadow: var(--shadow);
}
.card-article { border-left-color: var(--rail-article); }
.card-match { border-left-color: var(--rail-match); }

.card-title {
  font-weight: 600;
  font-size: var(--fs-md);
  margin: 0 0 4px;
  line-height: 1.3;
}
.card-body {
  font-size: var(--fs-sm);
  color: var(--ink-2);
  margin: 0 0 6px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.card-meta {
  font-size: 11.5px;
  color: var(--ink-3);
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  align-items: center;
}
.card-meta a {
  color: var(--accent);
  text-decoration: none;
}
.card-meta a:hover { text-decoration: underline; }

.match-name { font-weight: 600; color: var(--ink); }
.match-score {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 11.5px;
  background: #ecfdf5;
  color: var(--ok);
  border: 1px solid #a7f3d0;
  padding: 1px 6px;
  border-radius: 4px;
}

/* ─── Status line under live assistant turn ───────────────────────────── */

.status-line {
  font-size: 12px;
  color: var(--ink-3);
  font-style: italic;
  padding-left: 4px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.status-line .dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.1s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 0.25; transform: scale(0.9); }
  50% { opacity: 1; transform: scale(1.15); }
}
.turn[data-status="done"] .status-line,
.turn[data-status="error"] .status-line,
.turn[data-status="aborted"] .status-line { display: none; }

/* ─── Error row ───────────────────────────────────────────────────────── */

.error-row {
  font-size: var(--fs-sm);
  color: var(--danger);
  background: var(--danger-soft);
  border: 1px solid #fecaca;
  border-left: 3px solid var(--rail-error);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-family: var(--font-mono);
}
.aborted-tag {
  font-size: 12px;
  color: var(--warn);
  padding-left: 4px;
  font-style: italic;
}

/* ─── Dev panel ───────────────────────────────────────────────────────── */

.dev {
  background: var(--panel);
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.dev-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
}
.dev-head h2 {
  margin: 0;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-3);
  font-weight: 600;
}
.rawlog {
  list-style: none;
  margin: 0;
  padding: 8px 0;
  overflow-y: auto;
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.5;
  flex: 1 1 auto;
  min-height: 0;
}
.rawlog li {
  padding: 2px 14px;
  display: flex;
  gap: 8px;
  align-items: baseline;
  border-left: 3px solid transparent;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.rawlog .ts { color: var(--ink-3); flex-shrink: 0; }
.rawlog .ev { font-weight: 600; flex-shrink: 0; min-width: 56px; }
.rawlog .pl { color: var(--ink-2); flex: 1; }
.rawlog li.ev-meta { border-left-color: #94a3b8; }
.rawlog li.ev-status { border-left-color: #0ea5e9; }
.rawlog li.ev-partial { border-left-color: #a78bfa; }
.rawlog li.ev-text { border-left-color: #2563eb; }
.rawlog li.ev-matches { border-left-color: var(--rail-match); }
.rawlog li.ev-articles { border-left-color: var(--rail-article); }
.rawlog li.ev-channel { border-left-color: var(--rail-channel); }
.rawlog li.ev-logs { border-left-color: #94a3b8; }
.rawlog li.ev-error { border-left-color: var(--rail-error); background: var(--danger-soft); }
.rawlog li.ev-done { border-left-color: var(--ink-3); color: var(--ink-3); }

/* ─── Composer ────────────────────────────────────────────────────────── */

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  gap: 8px 12px;
  padding: 12px 16px;
  background: var(--panel);
  border-top: 1px solid var(--line);
  position: sticky;
  bottom: 0;
}
.composer textarea {
  grid-column: 1;
  grid-row: 1;
  font: inherit;
  font-size: var(--fs-md);
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg);
  resize: vertical;
  min-height: 44px;
  max-height: 200px;
  field-sizing: content;
}
.composer textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: transparent;
  background: var(--panel);
}
.composer #send {
  grid-column: 2;
  grid-row: 1;
  align-self: end;
  min-width: 80px;
  padding: 10px 16px;
}
.composer .status-bar {
  grid-column: 1 / -1;
  grid-row: 2;
  font-size: 11.5px;
  color: var(--ink-3);
  font-family: var(--font-mono);
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}
.composer .status-bar .sep { color: var(--line); }

/* ─── Small screen tweaks ─────────────────────────────────────────────── */

@media (max-width: 880px) {
  .layout { grid-template-columns: 1fr; }
  .layout[data-dev="on"] .dev {
    position: fixed;
    inset: 56px 0 84px 0;
    border-left: none;
    border-top: 1px solid var(--line);
    background: var(--panel);
    z-index: 4;
  }
  .uid-field input { width: 130px; }
  .topbar h1 { display: none; }
}

/* ─── View switch ─────────────────────────────────────────────────────── */

.view-switch { display: inline-flex; gap: 2px; }

/* Chat-only controls hide in the triggers view; layout + composer collapse so
   the triggers section takes the 1fr body row. */
body[data-view="triggers"] .layout,
body[data-view="triggers"] .composer,
body[data-view="triggers"] .chat-only { display: none; }

/* ─── Trigger console ─────────────────────────────────────────────────── */

.triggers {
  display: none;
  grid-template-columns: minmax(0, 380px) minmax(0, 1fr);
  min-height: 0;
  overflow: hidden;
}
body[data-view="triggers"] .triggers { display: grid; }

.trigger-form {
  padding: 18px 18px 24px;
  border-right: 1px solid var(--line);
  background: var(--panel);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.tg-h {
  margin: 0;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-3);
  font-weight: 600;
}
.trigger-result { padding: 18px 18px 24px; overflow-y: auto; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field > label { font-size: var(--fs-sm); color: var(--ink-2); font-weight: 500; }
.field .hint { color: var(--ink-3); font-weight: 400; }
.field select,
.field input,
.field textarea {
  font: inherit;
  font-size: 13px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--ink);
  width: 100%;
}
.field textarea { resize: vertical; font-family: var(--font-mono); font-size: 12.5px; min-height: 44px; }
.field select:focus,
.field input:focus,
.field textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: transparent;
  background: var(--panel);
}
.param-group { display: flex; flex-direction: column; gap: 14px; }
.param-group[hidden] { display: none; }
.field[hidden] { display: none; }

.tg-flags {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 6px 12px 10px;
  display: flex;
  gap: 18px;
  margin: 0;
}
.tg-flags legend { font-size: var(--fs-sm); color: var(--ink-2); padding: 0 4px; font-weight: 500; }
.tg-flags label { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }

.tg-actions { display: flex; gap: 8px; }
.tg-actions .btn { flex: 1; padding: 9px 12px; }
.tg-note { font-size: 11.5px; color: var(--ink-3); margin: 0; }

.tg-empty { color: var(--ink-3); font-size: var(--fs-sm); }
.tg-out { display: flex; flex-direction: column; gap: 12px; }
.tg-status { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.tg-reason { font-size: var(--fs-sm); color: var(--ink-2); font-family: var(--font-mono); }

.tg-badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 10px;
  border-radius: 999px;
}
.tg-badge-ok { background: #ecfdf5; color: var(--ok); border: 1px solid #a7f3d0; }
.tg-badge-info { background: #eff6ff; color: var(--accent); border: 1px solid #bfdbfe; }
.tg-badge-err { background: var(--danger-soft); color: var(--danger); border: 1px solid #fecaca; }
.tg-badge-muted { background: var(--line-2); color: var(--ink-2); border: 1px solid var(--line); }

.tg-rows { display: flex; flex-wrap: wrap; gap: 4px 16px; }
.tg-kv { display: flex; gap: 6px; font-size: var(--fs-sm); }
.tg-kv-k { color: var(--ink-3); }
.tg-kv-v { color: var(--ink); font-family: var(--font-mono); font-size: 12.5px; overflow-wrap: anywhere; }

.tg-card { display: flex; flex-direction: column; gap: 8px; }
.tg-chat-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.tg-mono { font-family: var(--font-mono); font-size: 13px; font-weight: 600; }
.tg-pill {
  font-size: 11.5px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 1px 6px;
  color: var(--ink-2);
}
.tg-would {
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  margin: 0;
}
.tg-preview { display: flex; flex-direction: column; gap: 3px; }
.tg-preview-label {
  font-size: 11px;
  color: var(--ink-3);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.tg-preview-body {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--bg);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 220px;
  overflow-y: auto;
}

@media (max-width: 880px) {
  .triggers { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .trigger-form { border-right: none; border-bottom: 1px solid var(--line); }
}
