:root {
  --bg: #e9edf2;
  --card: #ffffff;
  --ink: #f7f9fb;
  --border: #d3dae2;
  --text: #1d2833;
  --muted: #5d6c7b;
  --accent: #2b7de9;
  --accent-ink: #ffffff;
  --them: #e6eaf0;
  --high: #e8504f;
  --medium: #f5a637;
  --low: #34a86c;
  --danger: #d95555;
  --backdrop: rgba(29, 40, 51, 0.4);
  --button-bg: #e7ecf1;
  --bubble-card: #eaf2fd;
}

:root[data-theme="dark"] {
  --bg: #0f1216;
  --card: #171c22;
  --ink: #0c0f13;
  --border: #2a323c;
  --text: #d7dee6;
  --muted: #8b98a5;
  --accent: #4da3ff;
  --accent-ink: #06121f;
  --them: #232b34;
  --high: #ff5d5d;
  --medium: #ffb454;
  --low: #57d78d;
  --danger: #e05c5c;
  --backdrop: rgba(5, 8, 12, 0.65);
  --button-bg: #232b34;
  --bubble-card: #16283e;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.45;
}

.topbar {
  max-width: 960px;
  margin: 0 auto;
  padding: 10px 16px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px;
}
.brand { font-weight: 700; font-size: 1.05rem; text-align: center; }
/* a top-level button would stretch across its 1fr grid column and lean
   into the title — size it to content */
.topbar > button { justify-self: start; align-self: center; }
.topbar-actions { display: flex; gap: 6px; justify-self: end; }
.muted { color: var(--muted); font-weight: 400; }
.hint { color: var(--muted); font-size: 0.8rem; margin: 4px 0; }
.hint code { background: var(--ink); padding: 1px 5px; border-radius: 4px; }

button, .ghost, a.ghost {
  background: var(--button-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 0.82rem;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
}
button:hover, .ghost:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }

/* ---- phone shell ---- */
.phone {
  max-width: 430px;
  margin: 8px auto 16px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 22px;
  display: flex;
  flex-direction: column;
  /* recording-friendly: fills a vertical frame under the centred title */
  height: min(86vh, 900px);
  box-shadow: 0 12px 40px rgba(29, 40, 51, 0.12);
}

.phone-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  border-radius: 22px 22px 0 0;
  background: var(--card);
}
.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
  display: grid;
  place-items: center;
  font-weight: 700;
  flex: 0 0 auto;
}
.contact-col {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
  line-height: 1.2;
}
.contact { font-weight: 600; font-size: 0.8rem; }
.platform {
  font-size: 0.68rem;
  color: var(--muted);
  font-weight: 500;
}
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); flex: 0 0 auto; }
.dot.ok { background: var(--low); box-shadow: 0 0 6px var(--low); }
.dot.down { background: var(--high); box-shadow: 0 0 6px var(--high); }

.thread {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px;
  background: var(--ink);
  border-radius: 0 0 0 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.thread-hint {
  color: var(--muted);
  font-size: 0.78rem;
  text-align: center;
  padding: 8px 12px;
  border: 1px dashed var(--border);
  border-radius: 10px;
  margin: 4px auto 10px;
  max-width: 92%;
}

.msg {
  max-width: 84%;
  padding: 8px 12px;
  border-radius: 16px;
  font-size: 0.92rem;
  white-space: pre-wrap;
  word-break: break-word;
  align-self: flex-start;
  background: var(--them);
}
.msg.me {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-ink);
}
.msg.drafting {
  border: 1px dashed var(--border);
  background: transparent;
  padding: 6px 8px;
}
/* fixed rows ARE normal messages (base .msg look); the who-tag doubles as
   the edit affordance — click to put the turn back into the editor */
.msg.fixed .who-toggle {
  background: none;
  border: none;
  padding: 0 0 2px;
  font-size: 0.68rem;
  color: var(--muted);
  opacity: 0.7;
  cursor: pointer;
  display: block;
}
.msg.fixed .who-toggle:hover { opacity: 1; text-decoration: underline; }
.msg.fixed .fixed-text { white-space: pre-wrap; word-break: break-word; }
.msg.drafting .draft-tools {
  display: flex;
  gap: 6px;
  margin-bottom: 6px;
  align-items: center;
}
.msg.drafting textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  font: inherit;
  padding: 6px 8px;
  resize: vertical;
  min-height: 2.2em;
}
.msg .who-toggle {
  font-size: 0.72rem;
  padding: 2px 8px;
  border-radius: 999px;
}
.who-toggle.from-them { background: var(--them); color: var(--text); border-color: var(--border); }
.who-toggle.from-me { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* FINCHI verdict card (system bubble) */
.verdict {
  align-self: center;
  max-width: 94%;
  background: var(--bubble-card);
  border: 1px solid var(--accent);
  border-radius: 14px;
  padding: 10px 12px;
  font-size: 0.88rem;
}
.verdict .vhead {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 6px;
  font-size: 0.76rem;
  color: var(--muted);
}
.verdict .vlabel { text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700; }
.badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.76rem;
}
.badge.risk-HIGH { background: var(--high); color: #2b0707; }
.badge.risk-MEDIUM { background: var(--medium); color: #2b1c02; }
.badge.risk-LOW { background: var(--low); color: #05230f; }
.badge.risk-UNKNOWN, .badge.risk-FAILED { background: var(--high); color: #2b0707; }
.badge.risk-QUEUED, .badge.risk-PROCESSING { background: var(--them); color: var(--muted); }

.verdict .vfield { margin: 4px 0; }
/* section labels must NOT blend into the body: accent colour + weight,
   with air above so each section reads as its own heading */
.verdict .vfield .k {
  color: var(--accent);
  font-weight: 700;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  display: block;
  margin: 8px 0 2px;
}
.verdict .vfield:first-of-type .k { margin-top: 4px; }
.verdict ul.actions { margin: 4px 0 0; padding-left: 18px; }
.verdict details { margin-top: 8px; }
.verdict summary, dialog summary { cursor: pointer; color: var(--muted); font-size: 0.78rem; }
.verdict pre {
  background: var(--ink);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  overflow: auto;
  font-size: 0.72rem;
  max-height: 240px;
}
.msg.typing { color: var(--muted); font-style: italic; }
/* animated dots + a live elapsed timer: a fast Jev pass reads as instant,
   a full-pipeline turn reads as *working*, never stuck */
.typing-dots i {
  display: inline-block;
  width: 4px; height: 4px;
  margin-left: 3px;
  border-radius: 50%;
  background: var(--muted);
  animation: typing-blink 1.2s infinite;
}
.typing-dots i:nth-child(2) { animation-delay: 0.2s; }
.typing-dots i:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing-blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .typing-dots i { animation: none; opacity: 0.6; }
}
.msg.errorb { border: 1px solid var(--danger); color: var(--danger); background: var(--card); }

/* ---- composer ---- */
.composer { border-top: 1px solid var(--border); padding: 8px 10px 10px; }
.drafts-bar {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 6px;
  flex-wrap: wrap;
}
.drafts-bar .ghost { font-size: 0.76rem; padding: 3px 10px; }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.paperclip span {
  display: inline-block;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 0.78rem;
  color: var(--muted);
  cursor: pointer;
  background: var(--button-bg);
}
#message {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--ink);
  color: var(--text);
  font: inherit;
  font-size: 0.92rem;
  padding: 8px 10px;
  resize: none;
  max-height: 120px;
}
.send {
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: 12px;
  padding: 9px 16px;
  font-weight: 600;
  font-size: 0.86rem;
}
.analyse {
  background: var(--card);
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 12px;
  padding: 8px 14px;
  font-weight: 600;
  font-size: 0.86rem;
}
.analyse:disabled, .send:disabled { opacity: 0.5; cursor: wait; }
.attach-note { font-size: 0.74rem; margin-top: 4px; }

/* ---- drawers ---- */
.drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(92vw, 400px);
  background: var(--card);
  border-left: 1px solid var(--border);
  box-shadow: -12px 0 40px rgba(29, 40, 51, 0.18);
  padding: 14px 16px;
  overflow-y: auto;
  z-index: 30;
}
/* guest mode: the settings drawer trims to the demo-facing fields
   (safe person, contact, platform, audience profile + presets) — the
   host-only blocks (endpoint, key, log tail, playground link, raw
   profile JSON, theme switch) drop out via the body.guest class */
body.guest .host-only { display: none; }
/* the scenario replay panel opens from the LEFT so it never covers the
   results/settings drawers, and reads as a different kind of surface */
.drawer.drawer-left {
  right: auto;
  left: 0;
  border-left: none;
  border-right: 1px solid var(--border);
  box-shadow: 12px 0 40px rgba(29, 40, 51, 0.18);
}
.scenario-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin: 10px 0;
  background: var(--bg);
}
.scenario-card h3 { font-size: 0.88rem; margin: 0 0 4px; }
.scenario-card p { font-size: 0.8rem; margin: 4px 0; }
.scenario-card .scenario-expect { color: var(--muted); font-size: 0.74rem; }
.scenario-card .replay {
  width: 100%;
  margin-top: 8px;
}
/* the LIVE-API note bubble a replay emits before the message lands */
.msg.sysnote {
  color: var(--muted);
  font-size: 0.74rem;
  background: var(--card);
  border: 1px dashed var(--border);
}
.drawer h2 { font-size: 1rem; margin: 8px 0; }
.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  z-index: 20;
}
.drawer label { display: flex; flex-direction: column; gap: 3px; font-size: 0.78rem; color: var(--muted); margin-bottom: 8px; }
.drawer input, .drawer select, .drawer textarea {
  background: var(--ink);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 7px 9px;
  font: inherit;
  font-size: 0.85rem;
}
.drawer .profile-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }
.drawer .profile-grid .check { flex-direction: row; align-items: center; gap: 6px; margin: 0; }
.drawer .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 8px 0; }
.ping-out { font-size: 0.78rem; margin-bottom: 10px; white-space: pre-wrap; }

.history-row {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  margin-bottom: 6px;
  display: flex;
  gap: 8px;
  align-items: center;
  cursor: pointer;
  font-size: 0.82rem;
  background: var(--ink);
}
.history-row:hover { border-color: var(--accent); }
.history-row .mono { color: var(--muted); font-family: ui-monospace, Menlo, monospace; font-size: 0.74rem; }

dialog#detailModal {
  width: min(860px, 94vw);
  max-height: 87vh;
  overflow: auto;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
}
dialog#detailModal::backdrop { background: var(--backdrop); }
dialog .summary-grid { display: grid; grid-template-columns: 110px 1fr; gap: 6px 12px; font-size: 0.88rem; }
dialog .summary-grid .k { color: var(--muted); }
dialog .summary-grid .explanation { grid-column: 1 / -1; margin-top: 6px; }
dialog .summary-grid .explanation .label { color: var(--muted); font-size: 0.76rem; }
dialog .mono { font-family: ui-monospace, Menlo, monospace; }
.prewrap { white-space: pre-wrap; word-break: break-word; font-size: 0.8rem; }
.local-only-note { color: var(--muted); font-size: 0.8rem; margin: 0 0 10px; }

.minihead { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--accent); margin: 14px 0 2px; }
.msg.lowchip {
  font-size: 0.78rem;
  color: var(--text);
  background: var(--low);
  border-radius: 999px;
  padding: 4px 10px;
  align-self: flex-start;
  display: inline-block;
  width: auto;
  max-width: 94%;
}

/* ---- verdict risk borders + icons ---- */
.verdict.risk-border-LOW { border-color: var(--low); }
.verdict.risk-border-MEDIUM { border-color: var(--medium); }
.verdict.risk-border-HIGH { border-color: var(--high); }
.verdict.risk-border-UNKNOWN, .verdict.risk-border-FAILED { border-color: var(--danger); }
.vicon { font-size: 1rem; margin-right: 4px; flex-shrink: 0; }
.vicon-LOW { color: var(--low); }
.vicon-MEDIUM { color: var(--medium); }
.vicon-HIGH { color: var(--high); }
