/* ── Percival dashboard — refined dark theme, sidebar layout ─────────────────
   Design tokens. Chart series colors (--viz-*) are CVD-validated against the
   card surface (#161a23) — don't reorder or substitute them casually. */
:root {
  --bg: #0b0d12;            /* page plane */
  --panel: #0f121a;         /* sidebar */
  --card: #161a23;          /* cards + chart surface */
  --card-2: #1c2130;        /* nested / hover surfaces */
  --border: #252c3b;
  --border-soft: #1e2431;
  --input-bg: #0d1017;      /* form-field well */
  --text: #e8eaf1;
  --muted: #96a0b5;
  --faint: #6b7488;
  --accent: #7c96ff;        /* links, active states, focus */
  --accent-deep: #4f6af0;   /* filled primary buttons */
  --good: #34c98e;
  --bad: #f26d76;
  --warn: #f0b356;
  /* chart series (dark-surface validated: ΔE 41+, ≥3:1) */
  --viz-1: #3987e5;         /* blue   — auto-sent */
  --viz-2: #199e70;         /* aqua   — sent by human */
  --viz-3: #c98500;         /* yellow — held */
  --viz-grid: #232938;      /* hairline gridlines */
  --radius: 10px;
  --radius-lg: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .25), 0 8px 24px rgba(0, 0, 0, .18);
}

/* Light theme — same token structure, light surfaces. Dark is the default
   (no attribute / data-theme="dark"); [data-theme="light"] flips the palette. */
:root[data-theme="light"] {
  --bg: #f4f6fa;
  --panel: #ffffff;
  --card: #ffffff;
  --card-2: #eef1f6;
  --border: #d3d9e4;
  --border-soft: #e5e9f0;
  --input-bg: #ffffff;      /* form-field well — white on the light card */
  --text: #1b2030;
  --muted: #566074;
  --faint: #8a93a6;
  --accent: #4257d6;        /* links, active states, focus — deeper for light bg */
  --accent-deep: #3f57d8;   /* filled primary buttons (white text) */
  --good: #1a9d69;
  --bad: #d64550;
  --warn: #b9791a;
  --viz-1: #2f6fd0;
  --viz-2: #148a5f;
  --viz-3: #b57d00;
  --viz-grid: #e5e9f0;
  --shadow: 0 1px 2px rgba(20, 30, 60, .06), 0 8px 24px rgba(20, 30, 60, .08);
}

* { box-sizing: border-box; }
html { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
::selection { background: color-mix(in srgb, var(--accent) 35%, transparent); }

h2 { margin: 0 0 6px; font-size: 21px; font-weight: 650; letter-spacing: -.01em; }
h3 { font-size: 15px; font-weight: 600; }
.muted { color: var(--muted); font-size: 13px; }
.small { font-size: 12px; }
code { background: var(--card-2); border: 1px solid var(--border-soft); border-radius: 5px; padding: 1px 5px; font-size: 12px; }

/* ── Shell: fixed sidebar + content ─────────────────────────────────────── */
.shell { display: flex; min-height: 100vh; }

.sidebar {
  position: fixed; inset: 0 auto 0 0; z-index: 40;
  width: 234px; display: flex; flex-direction: column;
  background: var(--panel); border-right: 1px solid var(--border-soft);
  padding: 18px 12px 14px;
}
.side-brand { display: flex; align-items: center; gap: 10px; padding: 2px 10px 16px; }
.side-brand .logo { font-size: 22px; line-height: 1; }
.side-brand-text { font-size: 16px; font-weight: 700; letter-spacing: -.01em; display: flex; flex-direction: column; }
.side-brand-text small { color: var(--faint); font-weight: 500; font-size: 10.5px; letter-spacing: .02em; }

.side-nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
.side-nav .tab {
  display: flex; align-items: center; gap: 11px;
  background: none; border: none; text-align: left; width: 100%;
  color: var(--muted); font-size: 13.5px; font-weight: 500; font-family: inherit;
  padding: 9px 12px; border-radius: 9px; cursor: pointer;
  transition: background .12s, color .12s;
}
.side-nav .tab svg { width: 18px; height: 18px; flex: none; opacity: .8; }
.side-nav .tab:hover { background: var(--card); color: var(--text); }
.side-nav .tab.active {
  background: color-mix(in srgb, var(--accent) 13%, transparent);
  color: var(--text);
}
.side-nav .tab.active svg { color: var(--accent); opacity: 1; }

/* Collapsible sidebar group ("Settings" — children are ordinary tabs) */
.side-group { display: flex; flex-direction: column; gap: 2px; }
.side-group-btn {
  display: flex; align-items: center; gap: 11px;
  background: none; border: none; text-align: left; width: 100%;
  color: var(--muted); font-size: 13.5px; font-weight: 500; font-family: inherit;
  padding: 9px 12px; border-radius: 9px; cursor: pointer;
  transition: background .12s, color .12s;
}
.side-group-btn svg { width: 18px; height: 18px; flex: none; opacity: .8; }
.side-group-btn:hover { background: var(--card); color: var(--text); }
.side-group-btn.child-active { color: var(--text); }
.side-group-btn.child-active > svg:first-child { color: var(--accent); opacity: 1; }
.side-caret { width: 13px !important; height: 13px !important; margin-left: auto; opacity: .6; transition: transform .15s ease; }
.side-group-btn.open .side-caret { transform: rotate(90deg); }
.side-sub {
  display: flex; flex-direction: column; gap: 2px;
  margin-left: 20px; padding-left: 7px; border-left: 1px solid var(--border-soft);
}
.side-sub[hidden] { display: none; }
.side-sub .tab { padding: 7px 10px; font-size: 13px; }
.side-sub .tab svg { width: 16px; height: 16px; }

.side-foot { display: flex; flex-direction: column; gap: 10px; padding: 12px 4px 0; border-top: 1px solid var(--border-soft); }
.theme-toggle {
  display: flex; align-items: center; gap: 8px; width: 100%;
  background: var(--card-2); border: 1px solid var(--border-soft); color: var(--muted);
  border-radius: var(--radius); padding: 7px 10px; font-size: 12.5px; cursor: pointer;
}
.theme-toggle:hover { color: var(--text); border-color: var(--border); }
.theme-toggle .theme-ico { font-size: 14px; line-height: 1; }
.status {
  font-size: 11px; line-height: 1.5; padding: 7px 11px; border-radius: 8px;
  background: var(--card); border: 1px solid var(--border-soft); color: var(--muted);
  word-break: break-word;
}
.status.ok { color: var(--good); }
.status.warn { color: var(--warn); }
.side-user { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 0 4px; }
.user-chip { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ghost-btn { background: transparent; border: 1px solid var(--border); font-size: 12px; padding: 5px 10px; }

main { flex: 1; margin-left: 234px; padding: 30px 38px 60px; max-width: 1260px; width: 100%; }
.panel { display: none; }
.panel.active { display: block; animation: panel-in .18s ease; }
@keyframes panel-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ── Forms & controls ───────────────────────────────────────────────────── */
input, select, textarea {
  background: var(--input-bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 8px 11px; font-size: 13.5px; font-family: inherit;
  transition: border-color .12s, box-shadow .12s;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}
input::placeholder, textarea::placeholder { color: var(--faint); }
textarea { resize: vertical; line-height: 1.5; }

button {
  font-size: 13.5px; font-weight: 500; font-family: inherit;
  border-radius: 8px; padding: 8px 14px; cursor: pointer;
  border: 1px solid var(--border); background: var(--card); color: var(--text);
  transition: background .12s, border-color .12s, filter .12s;
}
button:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); background: var(--card-2); }
button.primary {
  background: var(--accent-deep); border-color: var(--accent-deep); color: #fff; font-weight: 600;
}
button.primary:hover { filter: brightness(1.12); background: var(--accent-deep); }
button:disabled { opacity: .45; cursor: not-allowed; filter: none; }
button.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--border)); }
.link-btn { background: none; border: none; color: var(--accent); cursor: pointer; font-size: 12px; padding: 2px 4px; }
.link-btn:hover { text-decoration: underline; background: none; }

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 22px; }
.grid label, .card label { display: flex; flex-direction: column; font-size: 12.5px; font-weight: 500; gap: 6px; color: var(--muted); }
.grid label.full { grid-column: 1 / -1; }
.grid label.checkbox { flex-direction: row; align-items: center; gap: 9px; align-self: end; min-height: 37px; font-weight: 450; }
.grid input[type="checkbox"], .mode-card input { accent-color: var(--accent-deep); width: 15px; height: 15px; }
.field-hint { font-size: 11px; color: var(--faint); font-weight: 400; line-height: 1.45; margin-top: -2px; }
.actions { display: flex; gap: 8px; align-items: center; }

/* ── Cards, rows, tables ────────────────────────────────────────────────── */
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin: 16px 0; }
.card {
  background: var(--card); border: 1px solid var(--border-soft); border-radius: var(--radius-lg);
  padding: 18px; display: flex; flex-direction: column; gap: 10px;
}
.card h3 { margin: 0; }

table { width: 100%; border-collapse: collapse; margin-top: 12px; }
th, td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--border-soft); font-size: 13px; }
th { color: var(--faint); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; }
td { font-variant-numeric: tabular-nums; }
tbody tr:hover td { background: color-mix(in srgb, var(--card) 60%, transparent); }

/* ── Mode cards ─────────────────────────────────────────────────────────── */
.mode-cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin: 18px 0 26px; }
.mode-card {
  border: 1px solid var(--border-soft); border-radius: var(--radius-lg); padding: 16px; cursor: pointer;
  background: var(--card); transition: border-color .15s, background .15s;
}
.mode-card:hover { border-color: color-mix(in srgb, var(--accent) 50%, var(--border)); }
.mode-card.selected {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, var(--card));
  box-shadow: 0 0 0 1px var(--accent) inset;
}
.mode-card h3 { margin: 8px 0 4px; }
.mode-card p { margin: 0; font-size: 12px; color: var(--muted); line-height: 1.45; }

/* ── Stats + badges (shared) ────────────────────────────────────────────── */
.stats { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.stat {
  background: var(--card); border: 1px solid var(--border-soft); border-radius: var(--radius);
  padding: 12px 16px; min-width: 92px;
}
.stat .n { font-size: 22px; font-weight: 700; letter-spacing: -.01em; }
.stat .l { font-size: 10.5px; color: var(--faint); text-transform: uppercase; letter-spacing: .05em; margin-top: 2px; }

.filters { display: flex; gap: 8px; margin: 12px 0; flex-wrap: wrap; }

.badge { font-size: 11px; font-weight: 600; padding: 3px 9px; border-radius: 999px; }
.badge.good { background: color-mix(in srgb, var(--good) 16%, transparent); color: var(--good); }
.badge.bad { background: color-mix(in srgb, var(--bad) 16%, transparent); color: var(--bad); }
.badge.unreviewed { background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--warn); }

.confidence-bar { height: 8px; background: var(--bg); border-radius: 999px; overflow: hidden; margin: 6px 0; }
.confidence-bar > div { height: 100%; background: var(--good); }

/* ── Segmented control ──────────────────────────────────────────────────── */
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; background: var(--card); }
.seg button {
  background: transparent; border: none; border-radius: 0; color: var(--muted); padding: 7px 14px;
  cursor: pointer; font-size: 12.5px; border-right: 1px solid var(--border-soft);
}
.seg button:last-child { border-right: none; }
.seg button:hover { color: var(--text); background: var(--card-2); }
.seg button.active { background: var(--accent-deep); color: #fff; font-weight: 600; }

/* ── HOME: page head, KPIs, charts ──────────────────────────────────────── */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head p { margin: 0; }

.kpi-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(168px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi {
  background: var(--card); border: 1px solid var(--border-soft); border-radius: var(--radius-lg);
  padding: 15px 17px 13px; display: flex; flex-direction: column; gap: 3px; min-width: 0;
  position: relative; overflow: hidden;
}
.kpi.clickable { cursor: pointer; transition: border-color .12s; }
.kpi.clickable:hover { border-color: color-mix(in srgb, var(--accent) 50%, var(--border)); }
.kpi-label { font-size: 11px; font-weight: 600; color: var(--faint); text-transform: uppercase; letter-spacing: .06em; }
.kpi-value { font-size: 26px; font-weight: 700; letter-spacing: -.02em; line-height: 1.15; }
.kpi-sub { display: flex; align-items: baseline; gap: 8px; min-height: 17px; }
.kpi-delta { font-size: 12px; font-weight: 600; }
.kpi-delta.up { color: var(--good); }
.kpi-delta.down { color: var(--bad); }
.kpi-delta.flat { color: var(--faint); font-weight: 500; }
.kpi-note { font-size: 11.5px; color: var(--faint); }
.kpi-spark { position: absolute; right: 12px; bottom: 10px; opacity: .9; }

.viz-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.viz-card { gap: 12px; }
.viz-wide { grid-column: 1 / -1; }
.viz-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.viz-head h3 { margin: 0; }
.viz-plot { width: 100%; }
.viz-plot svg { display: block; width: 100%; height: auto; }
.viz-empty { color: var(--faint); font-size: 13px; padding: 26px 0; text-align: center; }

.viz-legend { display: flex; gap: 14px; flex-wrap: wrap; }
.lg-item { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.lg-swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }

/* chart text (SVG) */
.viz-axis { fill: var(--faint); font-size: 10.5px; font-variant-numeric: tabular-nums; }
.viz-val { fill: var(--muted); font-size: 10.5px; font-weight: 600; font-variant-numeric: tabular-nums; }

/* shared hover tooltip */
.viz-tip {
  position: fixed; z-index: 200; pointer-events: none; max-width: 260px;
  background: #1d2331; border: 1px solid var(--border); border-radius: 9px;
  box-shadow: var(--shadow); padding: 9px 12px; font-size: 12px; line-height: 1.55;
}
.viz-tip b { font-weight: 650; }
.viz-tip .tip-row { display: flex; align-items: center; gap: 7px; }
.viz-tip .tip-row .lg-swatch { width: 8px; height: 8px; }
.viz-tip .tip-n { margin-left: auto; font-variant-numeric: tabular-nums; font-weight: 600; padding-left: 14px; }

/* horizontal part-to-whole bar + its row list */
.hbar { display: flex; height: 16px; border-radius: 6px; overflow: hidden; gap: 2px; background: transparent; margin: 4px 0 2px; }
.hbar-seg { min-width: 3px; }
.hbar-seg:first-child { border-radius: 6px 0 0 6px; }
.hbar-seg:last-child { border-radius: 0 6px 6px 0; }
.hbar-seg:only-child { border-radius: 6px; }
.hbar-rows { display: flex; flex-direction: column; gap: 7px; margin-top: 10px; }
.hbar-row { display: flex; align-items: center; gap: 9px; font-size: 12.5px; }
.hb-label { color: var(--muted); }
.hb-count { margin-left: auto; font-weight: 600; font-variant-numeric: tabular-nums; }
.hb-pct { color: var(--faint); font-variant-numeric: tabular-nums; width: 44px; text-align: right; }

/* agent status card */
.home-agent { display: flex; flex-direction: column; gap: 9px; font-size: 13px; }
.ha-row { display: flex; align-items: center; gap: 10px; justify-content: space-between; border-bottom: 1px solid var(--border-soft); padding-bottom: 8px; }
.ha-row:last-child { border-bottom: none; padding-bottom: 0; }
.ha-label { color: var(--faint); font-size: 12px; }
.ha-value { text-align: right; color: var(--text); font-weight: 500; }
.ha-value .badge { margin-left: 6px; }

/* ── Knowledge autocomplete ─────────────────────────────────────────────── */
.kb-autocomplete { position: relative; }
.kb-autocomplete > input { width: 100%; box-sizing: border-box; }
.kb-suggest { position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 20;
  background: var(--card-2); border: 1px solid var(--border); border-radius: 9px; overflow: hidden;
  box-shadow: var(--shadow); max-height: 220px; overflow-y: auto; }
.kb-suggest-item { padding: 8px 11px; font-size: 13px; cursor: pointer; }
.kb-suggest-item:hover, .kb-suggest-item.active { background: var(--accent-deep); color: #fff; }

/* ── Examples (training) ────────────────────────────────────────────────── */
.example { background: var(--card); border: 1px solid var(--border-soft); border-radius: var(--radius-lg); padding: 16px; margin-bottom: 12px; }
.example .meta { font-size: 11px; color: var(--faint); text-transform: uppercase; letter-spacing: .05em; margin-bottom: 10px; }
.example .cust { border-left: 3px solid var(--accent); padding-left: 12px; margin-bottom: 10px; white-space: pre-wrap; line-height: 1.55; }
.example .reply { border-left: 3px solid var(--good); padding-left: 12px; white-space: pre-wrap; line-height: 1.55; }
.example .actions { margin-top: 12px; }

/* ── Reply backlog ──────────────────────────────────────────────────────── */
.backlog-card { margin: 14px 0 18px; border-color: color-mix(in srgb, var(--warn) 30%, var(--border-soft)); }
.backlog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.backlog-head h3 { display: flex; align-items: center; gap: 8px; }
/* `.card label` styles labels as column form-fields — override for these rows. */
.card label.backlog-row, .backlog-row {
  display: flex; flex-direction: row; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 8px; cursor: pointer; font-size: 13px;
  font-weight: 400; color: var(--text);
  overflow: hidden; white-space: nowrap;
}
.backlog-row:hover { background: var(--card-2); }
.backlog-row.ineligible { opacity: .55; cursor: default; }
.backlog-row input { accent-color: var(--accent-deep); flex: none; }
.backlog-row .log-ticket { flex: none; }
.backlog-row .log-email { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.backlog-row .log-conf { flex: none; }
.backlog-row .log-preview { flex: 1 1 40px; min-width: 30px; }
.backlog-row .log-date { flex: 0 1 auto; min-width: 0; margin-left: auto; overflow: hidden; text-overflow: ellipsis; }
.backlog-actions { margin-top: 10px; flex-wrap: wrap; }
.backlog-result { padding: 8px 10px; border-bottom: 1px solid var(--border-soft); font-size: 13px; }
.backlog-result summary { cursor: pointer; list-style: none; display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }
.backlog-result summary::-webkit-details-marker { display: none; }

/* ── Actions (named procedures) ─────────────────────────────────────────── */
.act-howto { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin: 4px 0 18px; }
.act-howto-step {
  background: var(--card); border: 1px solid var(--border-soft); border-radius: var(--radius);
  padding: 12px 14px; display: flex; flex-direction: column; gap: 5px; font-size: 12.5px;
  color: var(--muted); line-height: 1.5;
}
.act-howto-step b { color: var(--text); font-size: 13px; }
@media (max-width: 940px) { .act-howto { grid-template-columns: 1fr; } }
.act-card { display: flex; flex-direction: column; gap: 9px; }
.act-meta { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; font-size: 12.5px; color: var(--muted); line-height: 1.5; }
.act-meta-warn { color: var(--warn); }
.act-k {
  flex: none; font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
  color: var(--faint); min-width: 92px;
}
.act-chip-req { border-color: color-mix(in srgb, var(--accent) 35%, var(--border-soft)); color: var(--text); }
.act-wfref code { font-size: 12px; }
.act-steps-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.act-chip {
  font-size: 12px; color: var(--muted); background: var(--bg);
  border: 1px solid var(--border-soft); border-radius: 999px; padding: 3px 10px;
}
.act-run-row { display: flex; align-items: center; gap: 9px; flex-wrap: wrap; }
.act-run-ticket { width: 90px; }
.act-log { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.act-log-line { display: flex; align-items: flex-start; gap: 9px; font-size: 12.5px; line-height: 1.5; }
.act-log-line .wf-step-num { width: 20px; height: 20px; font-size: 11px; color: #06281b; flex: none; }
.act-log-bad { color: var(--bad); }
.act-history { border-top: 1px solid var(--border-soft); padding-top: 8px; }

/* ── Editable run preview ──────────────────────────────────────────────── */
.act-preview { display: flex; flex-direction: column; gap: 12px; margin-top: 8px; }
.act-prev-head { font-size: 13px; }
.act-prev-req { font-size: 12.5px; }
.act-prev-step { border: 1px solid var(--border-soft); border-radius: var(--radius); background: var(--bg); padding: 10px 12px; display: flex; flex-direction: column; gap: 7px; }
.act-prev-label { display: flex; align-items: center; gap: 9px; font-size: 13px; font-weight: 600; }
.act-prev-label .wf-step-num { width: 20px; height: 20px; font-size: 11px; color: #06281b; flex: none; }
.act-prev-subj { margin-left: 29px; }
.act-prev-warn { color: var(--bad); font-size: 12.5px; background: color-mix(in srgb, var(--bad) 10%, transparent); border-radius: 6px; padding: 6px 9px; }
.act-prev-body { border: 1px solid var(--border); border-radius: var(--radius); background: var(--card); padding: 10px 12px; font-size: 13px; line-height: 1.55; min-height: 60px; outline: none; }
.act-prev-body:focus { border-color: var(--accent); }

/* ── HTML-aware body editor (Preview / HTML toggle, auto-expanding) ────────── */
.act-body-field { display: flex; flex-direction: column; gap: 5px; }
.act-body-lead { font-size: 12.5px; color: var(--muted); }
.act-body-editor { display: flex; flex-direction: column; gap: 0; }
.act-body-tabs { display: flex; align-items: center; gap: 4px; }
.act-body-tab { font-size: 11.5px; padding: 3px 10px; border: 1px solid var(--border); border-bottom: none; border-radius: 6px 6px 0 0; background: var(--bg); color: var(--muted); cursor: pointer; }
.act-body-tab.is-active { background: var(--card); color: var(--fg); font-weight: 600; }
.act-body-tabs .muted { margin-left: 8px; }
/* Both views auto-grow with content — no inner scrollbar. */
.act-body-preview, .act-body-html {
  border: 1px solid var(--border); border-radius: 0 var(--radius) var(--radius) var(--radius);
  background: var(--card); padding: 10px 12px; font-size: 13px; line-height: 1.55; width: 100%;
}
.act-body-preview { min-height: 44px; outline: none; }
.act-body-preview:focus { border-color: var(--accent); }
.act-body-html { font-family: var(--mono, ui-monospace, monospace); font-size: 12px; overflow: hidden; resize: none; white-space: pre-wrap; }
.act-body-html:focus { border-color: var(--accent); outline: none; }

.act-editor { gap: 12px; }
.act-step { border: 1px solid var(--border-soft); border-radius: var(--radius); background: var(--bg); padding: 12px 14px; margin-bottom: 10px; }
.act-step-head { display: flex; align-items: center; gap: 9px; margin-bottom: 10px; }
.act-step-title { font-weight: 600; font-size: 13.5px; }
.act-step-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.act-step-grid label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--muted); }
.act-step-grid label.full, .act-step-grid .full { grid-column: 1 / -1; }
.act-step-grid label.checkbox { flex-direction: row; align-items: center; gap: 8px; align-self: end; }
.act-step-grid label[hidden] { display: none; } /* flex display must not defeat [hidden] */
.act-step-grid select, .act-step-grid input, .act-step-grid textarea { width: 100%; }
.act-attach-opts { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; padding: 8px 0; }
.act-attach-opts .act-attach-lead { flex-basis: 100%; }
.act-attach-opts .checkbox { color: var(--fg); }
.act-fields { display: flex; flex-direction: column; gap: 6px; }
.act-field-row { display: flex; align-items: center; gap: 8px; }
.act-field-row input[data-fk="label"] { flex: 0 1 220px; }
.act-field-row input[data-fk="value"] { flex: 1; }
.act-field-add { align-self: flex-start; }
@media (max-width: 940px) { .act-step-grid { grid-template-columns: 1fr; } }

/* ── House rules ────────────────────────────────────────────────────────── */
.rule-add { margin: 16px 0 20px; }
.rule-add textarea { width: 100%; }
.rule-item { display: flex; flex-direction: column; gap: 8px; }
.rule-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.rule-title { font-weight: 650; font-size: 14px; }
.rule-text { font-size: 13.5px; line-height: 1.55; white-space: pre-wrap; }
.rule-disabled .rule-text, .rule-disabled .rule-title { opacity: .55; }
.rule-edit { display: flex; flex-direction: column; gap: 8px; }
.rule-edit[hidden] { display: none; }
.rule-edit textarea { width: 100%; }
.rule-actions { margin-top: 2px; }
.rule-conflict {
  background: color-mix(in srgb, var(--bad) 8%, transparent);
  border: 1px solid color-mix(in srgb, var(--bad) 35%, transparent);
  border-left: 3px solid var(--bad); border-radius: 8px;
  padding: 10px 12px; font-size: 13px; line-height: 1.5;
}
.rule-conflict-title { color: var(--bad); font-weight: 650; margin-bottom: 4px; }

/* ── Toast ──────────────────────────────────────────────────────────────── */
#toast {
  position: fixed; bottom: 24px; right: 24px; z-index: 300;
  background: var(--card-2); border: 1px solid var(--border);
  padding: 12px 18px; border-radius: 10px; box-shadow: var(--shadow);
  opacity: 0; transform: translateY(10px); transition: .2s; pointer-events: none; font-size: 13px;
}
#toast.show { opacity: 1; transform: translateY(0); }
#toast.error { border-color: var(--bad); }

/* ── Workflow flowchart editor ──────────────────────────────────────────── */
.wf-editor { display: flex; flex-direction: column; gap: 12px; }
.wf-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 12px; }
.wf-title-lbl { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.wf-title { min-width: 200px; }
.wf-toolbar-right { display: flex; align-items: center; gap: 8px; margin-left: auto; flex-wrap: wrap; }
.wf-zoom { display: inline-flex; align-items: center; gap: 2px; border: 1px solid var(--border); border-radius: 8px; padding: 2px; background: var(--card); }
.wf-zoom .link-btn { border: none; padding: 3px 9px; font-size: 14px; min-width: 28px; text-align: center; }
.wf-zoom .link-btn:hover { text-decoration: none; background: var(--card-2); border-radius: 6px; }
.wf-zoom-pct { font-size: 12px; color: var(--muted); min-width: 42px; text-align: center; }
.wf-add-node { background: var(--card); border: 1px dashed var(--border); color: var(--text); padding: 7px 12px; border-radius: 8px; cursor: pointer; font-size: 13px; }
.wf-add-node:hover { border-color: var(--accent); }
.wf-status { align-self: center; }

.wf-canvas {
  position: relative;
  border: 1px solid var(--border-soft); border-radius: var(--radius-lg); background: var(--bg);
  overflow: auto; height: 72vh; min-height: 360px; cursor: grab;
  background-image: radial-gradient(var(--border-soft) 1px, transparent 1px); background-size: 22px 22px;
}
/* Full-screen editing — the editor covers the whole viewport (over the sidebar). */
.wf-editor.wf-full {
  position: fixed; inset: 0; z-index: 300; margin: 0; padding: 14px 18px 16px;
  background: var(--bg); overflow: auto; gap: 10px;
}
.wf-editor.wf-full .wf-canvas { height: auto; flex: 1 1 auto; min-height: 0; }
.wf-editor.wf-full .wf-tip { display: none; }
body.wf-full-open { overflow: hidden; } /* stop the page behind from scrolling */
.wf-canvas.grabbing { cursor: grabbing; }
.wf-chart { position: relative; }
.wf-svg-host { position: absolute; inset: 0; }
.wf-svg { display: block; }
.wf-svg-node { cursor: grab; }
.wf-svg-node:active { cursor: grabbing; }
.wf-svg-num { fill: var(--faint); font-size: 10px; font-weight: 600; }
.wf-svg-text { fill: var(--text); font-size: 12px; }
.wf-svg-edge { fill: var(--muted); font-size: 11px; }
.wf-tip { margin: 8px 0 0; line-height: 1.5; }

.wf-pop {
  position: absolute; z-index: 5; width: 300px; max-width: 90%;
  background: var(--card-2); border: 1px solid var(--accent); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow); max-height: 60vh; overflow: auto;
}
.wf-pop-close { margin-left: 2px; color: var(--muted); font-size: 16px; line-height: 1; }
.wf-insp-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.wf-insp-title { font-weight: 600; font-size: 13px; }
.wf-step-num {
  display: inline-flex; align-items: center; justify-content: center;
  width: 22px; height: 22px; border-radius: 999px; background: var(--accent-deep);
  color: #fff; font-size: 12px; font-weight: 600; flex: none;
}
.wf-node-del { color: var(--bad); margin-left: auto; }
.wf-insp-lbl { font-size: 12px; color: var(--muted); font-weight: 600; margin: 12px 0 4px; }
.wf-node-text { width: 100%; resize: vertical; }
.wf-start-lbl { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); margin-top: 8px; }
.wf-start-lbl input { accent-color: var(--accent-deep); }
.wf-edges { display: flex; flex-direction: column; gap: 8px; margin: 4px 0; }
.wf-edge-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.wf-edge-row .wf-arrow { color: var(--accent); font-weight: 700; }
.wf-edge-label { flex: 1 1 120px; min-width: 90px; }
.wf-edge-to { flex: 1 1 100%; }
.wf-edge-del { color: var(--bad); font-size: 16px; line-height: 1; }
.wf-branch-btns { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }

/* Tool panels: Test / Audit / History */
.wf-tool-btn.active { background: var(--accent-deep); border-color: var(--accent-deep); color: #fff; font-weight: 600; }
.wf-panel { gap: 10px; }
.wf-panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.wf-panel-head h3 { margin: 0; }
.wf-panel textarea { width: 100%; }
.wf-sim-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.wf-sim-ticket { width: 110px; }
.wf-sim-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 8px; }
@media (max-width: 940px) { .wf-sim-cols { grid-template-columns: 1fr; } }
.wf-path { display: flex; flex-direction: column; gap: 6px; }
.wf-path-step {
  display: flex; align-items: flex-start; gap: 9px; text-align: left;
  background: var(--bg); border: 1px solid var(--border-soft); border-radius: 8px;
  padding: 8px 10px; font-size: 12.5px; line-height: 1.45; cursor: pointer;
}
.wf-path-step:hover { border-color: var(--good); background: var(--bg); }
.wf-path-step .wf-step-num { background: var(--good); color: #06281b; width: 20px; height: 20px; font-size: 11px; }
.wf-finding { display: flex; align-items: flex-start; gap: 10px; padding: 10px 4px; border-bottom: 1px solid var(--border-soft); font-size: 13px; }
.wf-finding:last-child { border-bottom: none; }
.wf-finding .badge { flex: none; margin-top: 1px; }
.wf-finding-body { min-width: 0; line-height: 1.5; }
.wf-finding-fix { color: var(--good); font-size: 12.5px; margin-top: 2px; }
.wf-versions { display: flex; flex-direction: column; }
.wf-version-row { display: flex; align-items: center; gap: 12px; padding: 8px 4px; border-bottom: 1px solid var(--border-soft); font-size: 13px; flex-wrap: wrap; }
.wf-version-row:last-child { border-bottom: none; }
.wf-version-date { font-variant-numeric: tabular-nums; color: var(--muted); }
.wf-version-note { font-weight: 550; }
.wf-version-restore { margin-left: auto; padding: 5px 12px; font-size: 12.5px; }

.wf-fieldbar {
  display: flex; align-items: center; flex-wrap: wrap; gap: 8px;
  padding: 6px 10px; border: 1px solid var(--border-soft);
  border-radius: 8px; background: var(--card); font-size: 13px;
}
.wf-fieldbar select { padding: 4px 6px; }
.wf-fieldbar .link-btn { border: 1px solid var(--border); border-radius: 6px; padding: 4px 8px; }
.wf-fieldbar .link-btn:hover { border-color: var(--accent); text-decoration: none; }

/* ── Activity log ───────────────────────────────────────────────────────── */
.log-filters { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.log-search { min-width: 160px; }

.log-entry {
  border: 1px solid var(--border-soft); border-radius: var(--radius-lg); background: var(--card);
  padding: 15px 18px; margin-bottom: 12px; transition: border-color .12s;
}
.log-entry:hover { border-color: var(--border); }
.log-entry > summary.log-head { cursor: pointer; list-style: none; margin-bottom: 0; }
.log-entry > summary.log-head::-webkit-details-marker { display: none; }
.log-entry[open] > summary.log-head { margin-bottom: 14px; }
.log-caret { display: inline-block; color: var(--faint); transition: transform .15s ease; }
.log-entry[open] .log-caret { transform: rotate(90deg); }
.log-preview { flex: 1 1 200px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.log-entry[open] .log-preview { display: none; }

.log-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.log-ticket { font-weight: 650; font-size: 14.5px; }
.log-email { font-size: 11.5px; color: var(--accent); background: color-mix(in srgb, var(--accent) 9%, transparent); border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent); border-radius: 999px; padding: 2px 9px; }
.log-conf { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.log-date { margin-left: auto; font-size: 11.5px; }
/* Ignore-whole-ticket button in the thread header. It (not the date) absorbs the
   free space so the button sits right next to the date; the sibling rule turns the
   date's auto-margin off so the two stay adjacent. */
.log-thread-ignore { margin-left: auto; padding: 2px 10px; font-size: 11.5px; line-height: 1.5; color: var(--muted); background: transparent; border: 1px solid var(--border); border-radius: 6px; cursor: pointer; }
.log-thread-ignore:hover { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 55%, var(--border)); background: color-mix(in srgb, var(--bad) 10%, transparent); }
.log-thread-ignore + .log-date { margin-left: 8px; }
.log-confreason { font-size: 13px; color: var(--muted); margin: 6px 0 2px; font-style: italic; }
.log-confreason .log-label { display: inline; font-style: normal; margin-right: 6px; }

.log-field { margin: 13px 0; }
.log-label { font-size: 12px; color: var(--muted); margin-bottom: 6px; font-weight: 600; }
.log-label .muted { font-weight: 400; }

.log-customer {
  background: var(--bg); border: 1px solid var(--border-soft); border-left: 3px solid var(--muted);
  border-radius: 8px; padding: 10px 12px; font-size: 13px; color: var(--text);
  white-space: pre-wrap; word-break: break-word; max-height: 180px; overflow-y: auto;
}
.log-response {
  background: var(--bg); border: 1px solid var(--border-soft); border-left: 3px solid var(--accent);
  border-radius: 8px; padding: 14px 16px; font-size: 13.5px; line-height: 1.6;
  white-space: pre-wrap; word-break: break-word;
}
/* The human agent's actual reply (shadow-eval ground truth / edited send). */
.log-response.log-human { border-left-color: var(--good); }
.log-tags { font-size: 12px; color: var(--muted); margin: 8px 0; }
.log-tags b { color: var(--text); font-weight: 600; }

.log-thread { display: flex; flex-direction: column; gap: 8px; }
.msg {
  display: flex; gap: 12px; align-items: flex-start;
  background: var(--bg); border: 1px solid var(--border-soft); border-radius: 8px; padding: 9px 12px;
}
.msg-who {
  flex: 0 0 76px; font-size: 10.5px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .05em; color: var(--faint); padding-top: 2px;
}
.msg-text { flex: 1; min-width: 0; font-size: 13px; line-height: 1.55; white-space: pre-wrap; word-break: break-word; }
.msg-customer { border-left: 3px solid var(--accent); }
.msg-agent { border-left: 3px solid var(--good); }
.msg-agent .msg-who { color: var(--good); }
/* Human agents (Anthony/Rhod) in the merged log thread — amber, so they read as
   distinct from both Percival (green) and the customer (blue). */
.msg-agent-human { border-left: 3px solid var(--warn); }
.msg-agent-human .msg-who { color: var(--warn); }

/* One of Percival's responses inside a merged ticket thread. The draft sits in the
   chat bubble; the review controls below it use the original single-entry spacing. */
.log-thread-msgs { gap: 8px; }
.log-resp .msg-text { display: flex; flex-direction: column; gap: 6px; }
.log-resp .resp-meta { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.log-resp .resp-meta .log-date { margin-left: auto; }
/* Controls below the bubble — full width, at the pre-thread spacing. */
.resp-review { margin-top: 4px; }
.resp-review > .log-confreason:first-child { margin-top: 4px; }
/* A response that's already been rated or ignored recedes so unreviewed ones stand out. */
.log-resp.settled { opacity: .72; }
.log-resp.settled:hover { opacity: 1; }

/* "Load more" pager below the ticket list. */
.log-more { display: flex; align-items: center; gap: 12px; justify-content: center; padding: 14px 0 4px; }
.log-more-btn {
  background: var(--card); border: 1px solid var(--border); color: var(--text);
  border-radius: var(--radius); padding: 8px 18px; cursor: pointer; font-weight: 600; font-size: 13px;
  transition: border-color .12s, background .12s;
}
.log-more-btn:hover { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, var(--card)); }

.log-deviation {
  background: color-mix(in srgb, var(--bad) 7%, transparent); border: 1px solid color-mix(in srgb, var(--bad) 30%, transparent);
  border-left: 3px solid var(--bad); border-radius: 8px;
  padding: 10px 12px; margin: 12px 0; font-size: 13px; line-height: 1.55;
  white-space: pre-wrap; word-break: break-word;
}
.log-deviation .log-label { color: var(--bad); }

/* Percival handed the ticket to a human — his reason (dashboard-only, never sent). */
.log-escalation {
  background: color-mix(in srgb, var(--warn) 8%, transparent); border: 1px solid color-mix(in srgb, var(--warn) 32%, transparent);
  border-left: 3px solid var(--warn); border-radius: 8px;
  padding: 10px 12px; margin: 4px 0 10px; font-size: 13px; line-height: 1.55;
  white-space: pre-wrap; word-break: break-word;
}
.log-escalation .log-label { color: var(--warn); }
/* Empty/held draft — show a muted placeholder instead of a blank box. */
.log-response.log-noreply { color: var(--muted); font-style: italic; border-left-color: var(--warn); }

/* ── Advanced Actions ───────────────────────────────────────────────────── */
.adv-danger {
  background: color-mix(in srgb, var(--bad) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--bad) 38%, transparent);
  border-left: 3px solid var(--bad); border-radius: var(--radius);
  padding: 11px 14px; margin: 0 0 16px; font-size: 13px; line-height: 1.5; color: var(--text);
}
.adv-danger code { background: color-mix(in srgb, var(--bad) 14%, transparent); padding: 1px 5px; border-radius: 5px; }
.adv-active { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; margin: 4px 0 6px; cursor: pointer; }
.adv-active input { width: 15px; height: 15px; }
.adv-import-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.adv-import-row input { max-width: 200px; }
.adv-canvas-wrap { margin: 6px 0 14px; }
/* Imported-ticket HEADER box — subject + meta, pinned above the body box. */
.adv-ticket-head {
  background: var(--card); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; margin: 6px 0 8px;
}
.adv-ticket-subject { font-size: 14px; font-weight: 700; color: var(--text); word-break: break-word; }
.adv-ticket-meta { margin-top: 3px; font-size: 12px; color: var(--muted); }
/* Imported-ticket BODY box — the scrollable conversation. */
.adv-canvas {
  max-height: 340px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px;
  background: var(--bg); border: 1px solid var(--border-soft); border-radius: 8px; padding: 12px; margin: 6px 0;
}
.adv-canvas mark { background: color-mix(in srgb, var(--warn) 40%, transparent); color: var(--text); border-radius: 3px; padding: 0 2px; }
/* A message rendered from sanitized Freshdesk HTML — restore normal block flow
   (the .msg-text default is pre-wrap, which would keep the HTML source newlines). */
.adv-msg-html { white-space: normal; }
.adv-msg-html p { margin: 0 0 8px; }
.adv-msg-html p:last-child, .adv-msg-html div:last-child { margin-bottom: 0; }
.adv-msg-html a { color: var(--accent); }
.adv-msg-html ul, .adv-msg-html ol { margin: 4px 0 8px; padding-left: 22px; }
.adv-msg-html blockquote { margin: 6px 0; padding-left: 10px; border-left: 2px solid var(--border); color: var(--muted); }
.adv-msg-html img.adv-real-img { max-width: 100%; height: auto; border-radius: 4px; margin: 4px 0; }
.adv-msg-html table { border-collapse: collapse; max-width: 100%; }
.adv-msg-html td, .adv-msg-html th { border: 1px solid var(--border-soft); padding: 4px 8px; }
/* Placeholder for an image we don't/can't load inline. */
.adv-inline-img {
  display: inline-block; font-size: 11.5px; color: var(--muted);
  background: var(--card); border: 1px dashed var(--border); border-radius: 5px; padding: 1px 7px; margin: 2px 0;
}
/* Attachment chips shown under a message, like Freshdesk's file list. */
.adv-att-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.adv-att-chip {
  display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--text);
  background: var(--card); border: 1px solid var(--border-soft); border-radius: 6px; padding: 3px 9px; word-break: break-all;
}
/* The {{name}} tag rendered inside a marked selection. */
.adv-mark-name {
  font-size: 10px; font-weight: 700; font-family: ui-monospace, monospace;
  color: var(--warn); background: color-mix(in srgb, var(--warn) 18%, transparent);
  border-radius: 3px; padding: 0 3px; margin-left: 3px; vertical-align: baseline;
}
.adv-hl-toolbar { display: flex; align-items: center; gap: 10px; }
.adv-hl-list { display: flex; flex-direction: column; gap: 10px; }
.adv-hl {
  display: grid; grid-template-columns: 1fr auto; gap: 8px 10px; align-items: center;
  background: var(--card); border: 1px solid var(--border-soft); border-radius: 8px; padding: 10px 12px;
}
.adv-hl-quote { grid-column: 1 / -1; font-size: 12.5px; font-style: italic; color: var(--warn); border-left: 3px solid var(--warn); padding-left: 8px; white-space: pre-wrap; word-break: break-word; }
.adv-hl-name-wrap { display: inline-flex; align-items: center; gap: 3px; }
.adv-hl-braces { font-family: ui-monospace, monospace; font-weight: 700; color: var(--muted); }
.adv-hl-name { width: 100%; min-width: 160px; font-family: ui-monospace, monospace; }
.adv-var-hint { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.adv-var-hint code { background: color-mix(in srgb, var(--warn) 14%, transparent); padding: 1px 5px; border-radius: 5px; }
/* Clickable variable tokens — insert {{name}} into the instructions at the cursor. */
.adv-var-chip {
  font-family: ui-monospace, monospace; font-size: 12px; cursor: pointer;
  color: var(--warn); background: color-mix(in srgb, var(--warn) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 32%, transparent); border-radius: 6px; padding: 2px 8px;
  transition: background .12s, border-color .12s;
}
.adv-var-chip:hover { background: color-mix(in srgb, var(--warn) 22%, transparent); border-color: var(--warn); }
.adv-instructions, .adv-name, .adv-match { width: 100%; }
.adv-match, .adv-name { max-width: 420px; }
.adv-instructions { resize: vertical; }
.adv-hl-del { align-self: center; padding: 4px 10px; }
.bad-text { color: var(--bad); }

/* Advanced-action Test: the ordered plan (read + simulated-write steps). */
.adv-plan {
  margin: 10px 0; background: var(--bg); border: 1px solid var(--border-soft); border-radius: 8px; padding: 10px 12px;
}
.adv-plan-head { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--faint); margin-bottom: 6px; }
.adv-plan-head .muted { text-transform: none; letter-spacing: 0; font-weight: 400; }
.adv-step { display: flex; gap: 8px; align-items: baseline; font-size: 13px; line-height: 1.5; padding: 2px 0; word-break: break-word; }
.adv-step-icon { flex: 0 0 auto; font-weight: 700; }
.adv-step-read { color: var(--muted); }
.adv-step-read .adv-step-icon { color: var(--good); }
.adv-step-write { color: var(--text); }
.adv-step-write .adv-step-icon { color: var(--warn); }
/* Live-run outcomes. */
.adv-step-done { color: var(--text); }
.adv-step-done .adv-step-icon { color: var(--good); }
.adv-step-failed .adv-step-icon { color: var(--bad); }
.adv-step-failed { color: var(--bad); }
.adv-step-blocked .adv-step-icon { color: var(--warn); }
.adv-plan-live { border-color: color-mix(in srgb, var(--good) 45%, var(--border)); }
/* Live Run button in the Test modal. */
.adv-live-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 10px; }
.adv-live-btn {
  background: color-mix(in srgb, var(--bad) 16%, transparent); color: var(--text);
  border: 1px solid color-mix(in srgb, var(--bad) 45%, transparent); border-radius: var(--radius);
  padding: 8px 16px; font-weight: 700; cursor: pointer; transition: background .12s, border-color .12s;
}
.adv-live-btn:hover { background: color-mix(in srgb, var(--bad) 28%, transparent); border-color: var(--bad); }

/* Modal (Advanced Actions test) */
.modal-overlay {
  position: fixed; inset: 0; z-index: 1000; display: flex; align-items: flex-start; justify-content: center;
  background: rgba(0, 0, 0, .5); padding: 6vh 16px; overflow-y: auto;
}
.modal {
  width: 100%; max-width: 720px; background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 20px 22px; box-shadow: var(--shadow);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.modal-x { background: none; border: 0; color: var(--muted); font-size: 18px; cursor: pointer; padding: 2px 8px; }
.advt-out { margin-top: 8px; }

/* Credit circuit-breaker banner — pipeline paused for low Anthropic credit. */
.credit-banner {
  background: color-mix(in srgb, var(--bad) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--bad) 35%, transparent);
  border-left: 3px solid var(--bad); border-radius: var(--radius);
  padding: 12px 14px; margin: 0 0 16px; font-size: 13.5px; line-height: 1.5; color: var(--text);
}

button.toggle.active { background: var(--bad); border-color: var(--bad); color: #fff; font-weight: 600; }

.log-field .log-corrected, .log-field .log-feedback { width: 100%; display: block; }
.log-corrected { resize: vertical; min-height: 80px; }
.log-actions { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }

/* ── Auth / login ───────────────────────────────────────────────────────── */
#loginOverlay { display: none; }
body:not(.authed) #loginOverlay { display: flex; }
body:not(.authed) .shell { display: none; }

#loginOverlay {
  position: fixed; inset: 0; z-index: 1000;
  align-items: center; justify-content: center;
  background: radial-gradient(1100px 500px at 50% -10%, color-mix(in srgb, var(--accent-deep) 14%, transparent), transparent 65%), var(--bg);
}
.login-card {
  background: var(--card); border: 1px solid var(--border-soft); border-radius: 16px;
  padding: 30px; width: 330px; max-width: 90vw; display: flex; flex-direction: column; gap: 12px;
  box-shadow: var(--shadow);
}
.login-brand { font-size: 22px; font-weight: 700; letter-spacing: -.01em; }
.login-card p { margin: 0; }
.login-card input { width: 100%; }
.login-error { color: var(--bad); font-size: 13px; min-height: 1.1em; }

/* ── Accounts ───────────────────────────────────────────────────────────── */
.acct-create-fields { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.acct-create-fields input { flex: 1; min-width: 160px; }
.acct-actions { display: flex; gap: 8px; justify-content: flex-end; }

/* ── Playground ─────────────────────────────────────────────────────────── */
.pg-card { gap: 8px; }
.pg-card textarea { width: 100%; }
.pg-card-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 4px; }
.pg-card-foot button { white-space: nowrap; }

/* ── Respond to real tickets ──────────────────────────────────────────────── */
.pg-respond-card { border-left: 3px solid var(--accent); }
.pg-respond-results { display: flex; flex-direction: column; gap: 14px; margin-top: 12px; }
.pg-respond-item { border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; }
.pg-respond-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.pg-respond-thread { max-height: 260px; overflow-y: auto; padding: 4px; }
.pg-respond-draftlbl { font-size: 12.5px; color: var(--muted); }
.pg-respond-draft { width: 100%; font-size: 13px; line-height: 1.5; }
.pg-respond-warn { color: var(--bad); font-size: 13px; background: color-mix(in srgb, var(--bad) 10%, transparent); border-radius: 6px; padding: 8px 10px; }
.pg-respond-foot { display: flex; align-items: center; gap: 10px; }

/* ── Failed Tickets ───────────────────────────────────────────────────────── */
.failed-toolbar { display: flex; align-items: center; gap: 12px; margin: 14px 0; }
.failed-list { display: flex; flex-direction: column; gap: 10px; }
.failed-row { border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); padding: 11px 13px; display: flex; flex-direction: column; gap: 7px; transition: opacity .3s; }
.failed-row.failed-done { opacity: .55; border-color: var(--good); }
.failed-row-head { display: flex; align-items: center; gap: 10px; }
.failed-err { font-size: 12.5px; line-height: 1.5; color: var(--bad); font-family: var(--mono, ui-monospace, monospace); background: color-mix(in srgb, var(--bad) 8%, transparent); border-radius: 6px; padding: 7px 9px; word-break: break-word; }
.failed-err-fresh { outline: 1px solid var(--bad); }
.failed-retry { white-space: nowrap; }

/* ── SLA Plan ─────────────────────────────────────────────────────────────── */
.sla-cfg { gap: 14px; margin-bottom: 16px; }
.sla-cfg .grid label { display: flex; flex-direction: column; gap: 5px; }
.sla-cfg input { width: 140px; }
.sla-toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.sla-status { font-size: 12.5px; color: var(--muted); }
.sla-status.ok { color: var(--good); }
/* SLA sub-tabs: Percival can action / Needs a human. */
.sla-subtabs { display: flex; gap: 6px; margin: 14px 0 12px; border-bottom: 1px solid var(--border); }
.sla-subtab {
  background: none; border: none; border-bottom: 2px solid transparent; color: var(--muted);
  padding: 8px 12px; cursor: pointer; font-size: 13px; font-weight: 600; display: inline-flex; align-items: center; gap: 6px;
}
.sla-subtab:hover { color: var(--text); }
.sla-subtab.active { color: var(--text); border-bottom-color: var(--accent); }
.sla-count { font-size: 11px; background: var(--card); border: 1px solid var(--border-soft); border-radius: 10px; padding: 0 7px; color: var(--muted); }
.sla-list { display: flex; flex-direction: column; gap: 10px; }
/* `display: flex` above would otherwise beat the `hidden` attribute's display:none,
   leaving the inactive tab's list visible — this makes hidden win. */
.sla-list[hidden] { display: none; }
.sla-row { border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); padding: 11px 13px; display: flex; flex-direction: column; gap: 8px; }
/* Human-required rows read as a queue, tinted amber, no send controls. */
.sla-row.sla-human { border-left: 3px solid var(--warn); }
.sla-metric.sla-wait { color: var(--warn); font-weight: 700; }
.sla-reason {
  font-size: 12.5px; line-height: 1.5; color: var(--text);
  background: color-mix(in srgb, var(--warn) 8%, transparent); border-left: 3px solid var(--warn);
  border-radius: 6px; padding: 8px 10px; white-space: pre-wrap; word-break: break-word;
}
.sla-row-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.sla-metric { font-size: 12px; color: var(--muted); font-family: var(--mono, ui-monospace, monospace); }
.sla-metric.sla-clears { color: var(--good); }
.sla-draft summary { cursor: pointer; font-size: 12.5px; color: var(--muted); }
.sla-draft .log-response { margin-top: 6px; }
.sla-err { font-size: 12.5px; color: var(--bad); font-family: var(--mono, ui-monospace, monospace); word-break: break-word; }
.sla-row-foot { display: flex; gap: 12px; align-items: center; }
.pg-gen-fields { display: flex; gap: 12px; flex-wrap: wrap; }
.pg-gen-fields label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.pg-gen-fields input { width: 110px; }
.pg-threads-title { margin: 22px 0 12px; }

.pg-tabs { display: flex; flex-wrap: wrap; gap: 10px; }
.pg-tab {
  display: flex; flex-direction: column; align-items: flex-start; gap: 6px;
  text-align: left; width: 224px; padding: 13px 15px; border-radius: var(--radius);
  border: 1px solid var(--border-soft); background: var(--card);
}
.pg-tab:hover { border-color: color-mix(in srgb, var(--accent) 50%, var(--border)); background: var(--card); }
.pg-tab.is-done { border-color: color-mix(in srgb, var(--good) 40%, var(--border-soft)); }
.pg-tab-origin { font-size: 10.5px; color: var(--faint); text-transform: uppercase; letter-spacing: .05em; }
.pg-tab-title { font-size: 13px; font-weight: 600; color: var(--text); line-height: 1.35;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.pg-status { font-size: 11px; font-weight: 600; padding: 2px 9px; border-radius: 999px; }
.pg-status.done { background: color-mix(in srgb, var(--good) 18%, transparent); color: var(--good); }
.pg-status.part { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.pg-status.none { background: var(--card-2); color: var(--muted); }

.pg-thread-view { margin-top: 4px; }
.pg-thread-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.pg-thread-meta { display: flex; align-items: center; gap: 10px; }
.pg-back { background: transparent; border-color: transparent; color: var(--accent); padding-left: 0; }
.pg-back:hover { background: transparent; border-color: transparent; text-decoration: underline; }
.pg-delete { color: var(--bad); border-color: var(--border); }
.pg-thread-title { margin: 10px 0 16px; font-size: 17px; }
.pg-thread-body { display: flex; flex-direction: column; gap: 14px; }

.pg-context {
  background: var(--card); border: 1px solid var(--border-soft); border-left: 3px solid var(--accent);
  border-radius: 8px; padding: 10px 14px; margin: 0 0 18px; font-size: 13px; line-height: 1.5; color: var(--muted);
}
.pg-context-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; color: var(--accent); margin-bottom: 4px; }

.pg-atts {
  margin-top: 2px; max-width: 88%; display: flex; flex-direction: column; gap: 6px;
  background: color-mix(in srgb, var(--warn) 7%, var(--card));
  border: 1px solid color-mix(in srgb, var(--warn) 28%, var(--border-soft));
  border-radius: var(--radius); padding: 10px 12px;
}
.pg-atts-label { font-size: 11px; font-weight: 600; color: var(--warn); text-transform: uppercase; letter-spacing: .03em; }
.pg-att { font-size: 13px; line-height: 1.5; }
.pg-att-name { font-weight: 600; }
.pg-att-content { color: var(--muted); white-space: pre-wrap; }

.pg-msg { display: flex; flex-direction: column; gap: 4px; max-width: 88%; }
.pg-msg.customer { align-self: flex-start; }
.pg-msg.agent { align-self: flex-end; align-items: flex-end; }
.pg-msg-role { font-size: 11px; color: var(--faint); }
.pg-bubble { padding: 11px 14px; border-radius: 13px; line-height: 1.5; white-space: pre-wrap; font-size: 13.5px; }
.pg-msg.customer .pg-bubble { background: var(--card); border: 1px solid var(--border-soft); border-top-left-radius: 4px; }
.pg-msg.agent .pg-bubble { background: color-mix(in srgb, var(--accent) 14%, var(--card)); border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--border)); border-top-right-radius: 4px; }

.pg-rate { display: flex; align-items: center; gap: 8px; margin-top: 4px; flex-wrap: wrap; justify-content: flex-end; }
.pg-rate-btn { padding: 6px 12px; }
.pg-rate-btn.sel-good { background: var(--good); border-color: var(--good); color: #06281b; font-weight: 600; }
.pg-rate-btn.sel-bad { background: var(--bad); border-color: var(--bad); color: #2a0b0b; font-weight: 600; }
.pg-note { flex: 1; min-width: 220px; }

/* ── Responsive: sidebar collapses to a top bar ─────────────────────────── */
@media (max-width: 940px) {
  .sidebar {
    position: static; width: 100%; flex-direction: column; padding: 12px 14px;
    border-right: none; border-bottom: 1px solid var(--border-soft);
  }
  .shell { flex-direction: column; }
  .side-nav { flex-direction: row; overflow-x: auto; padding-bottom: 4px; }
  .side-nav .tab { white-space: nowrap; width: auto; }
  /* Flatten the Settings group into the horizontal bar: hide the toggle, always show children. */
  .side-group { flex-direction: row; }
  .side-group-btn { display: none; }
  .side-nav .side-sub, .side-nav .side-sub[hidden] { display: flex; flex-direction: row; margin: 0; padding: 0; border-left: none; }
  .side-foot { flex-direction: row; align-items: center; justify-content: space-between; }
  main { margin-left: 0; padding: 20px 16px 48px; }
  .viz-grid { grid-template-columns: 1fr; }
  .grid, .row { grid-template-columns: 1fr; }
  .mode-cards { grid-template-columns: 1fr; }
}
