/* Admin console: neutral light theme, dark when the system asks for it.
   Colors are tokens on :root so dark mode only swaps values. */

:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #1b1f24;
  --muted: #5b6573;
  --accent: #2f5bd3;
  --accent-hover: #2449b0;
  --accent-text: #ffffff;
  --accent-soft: #e9effd;
  --ok: #1d7a3a;
  --ok-soft: #e4f4e9;
  --warn: #8a5a00;
  --warn-soft: #fdf2d8;
  --danger: #b42318;
  --danger-soft: #fdeceb;
  --info: #5b3cc4;
  --info-soft: #efeafd;
  --bubble-customer: #f0f2f5;
  --bubble-agent: #e9effd;
  --bubble-staff: #e4f4e9;
  --focus: #2f5bd3;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(16 24 40 / 5%), 0 1px 3px rgb(16 24 40 / 7%);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1f232b;
    --border: #2d333d;
    --text: #e7eaf0;
    --muted: #9aa3b2;
    --accent: #7ea4ff;
    --accent-hover: #a2bdff;
    --accent-text: #0f1115;
    --accent-soft: #1d2840;
    --ok: #74d48f;
    --ok-soft: #15291c;
    --warn: #f0c062;
    --warn-soft: #2e2511;
    --danger: #ff8f85;
    --danger-soft: #3a1a18;
    --info: #bba8ff;
    --info-soft: #251f3d;
    --bubble-customer: #222730;
    --bubble-agent: #1d2840;
    --bubble-staff: #15291c;
    --focus: #7ea4ff;
    --shadow: none;
  }
}

/* ── Base ─────────────────────────────────────────────────────────────── */

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
}

a { color: var(--accent); text-underline-offset: 2px; }
a:hover { color: var(--accent-hover); }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

h1 { font-size: 1.4rem; line-height: 1.3; margin: 0; }
h2 { font-size: 1.05rem; line-height: 1.3; margin: 0 0 12px; }
h3 { font-size: .95rem; margin: 0 0 8px; }
p { margin: 0 0 12px; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: var(--mono); font-size: .85rem; }
.nowrap { white-space: nowrap; }
.loading { color: var(--muted); padding: 24px 0; }

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

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.signed-out .topbar { display: none; }

.topbar-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.brand { font-weight: 650; color: var(--text); text-decoration: none; white-space: nowrap; }
.brand:hover { color: var(--text); }

.app-switch select { width: auto; max-width: 220px; min-height: 34px; padding: 4px 8px; }

.nav {
  display: flex;
  gap: 2px;
  flex: 1 1 auto;
  overflow-x: auto;
  scrollbar-width: none;
}
.nav::-webkit-scrollbar { display: none; }
.nav a {
  padding: 6px 10px;
  border-radius: 8px;
  color: var(--muted);
  font-weight: 550;
  text-decoration: none;
  white-space: nowrap;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a[aria-current="page"] { background: var(--accent-soft); color: var(--accent); }

@media (max-width: 820px) {
  .nav { order: 3; flex-basis: 100%; }
  #logout { margin-left: auto; }
}

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

.main { max-width: 1280px; margin: 0 auto; padding: 20px 16px 64px; }
.main:focus { outline: none; }

.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.page-head p { margin: 4px 0 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
  min-width: 0;
}
.card > :last-child { margin-bottom: 0; }

.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  gap: 16px;
  align-items: start;
}
.grid-3 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 16px;
  align-items: start;
}
.grid-2 > .card, .grid-3 > .card { margin-bottom: 0; }
.grid-2, .grid-3 { margin-bottom: 16px; }

@media (max-width: 420px) {
  .grid-2, .grid-3 { grid-template-columns: minmax(0, 1fr); }
}

.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1 1 auto; }
.row > .grow { flex: 1 1 200px; width: auto; min-width: 0; }

.back { display: inline-block; margin-bottom: 4px; font-size: .9rem; text-decoration: none; }

.empty { text-align: center; color: var(--muted); padding: 32px 16px; }

.callout {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--warn-soft);
  color: var(--text);
  margin-bottom: 12px;
}
.callout strong { color: var(--warn); }

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 550;
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
}
.btn:hover { background: var(--surface-2); color: var(--text); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--accent-text); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); color: var(--danger); }
.btn-ok { color: var(--ok); }
.btn-ok:hover { background: var(--ok-soft); color: var(--ok); }
.btn-small { min-height: 28px; padding: 2px 10px; font-size: .85rem; }

.actions { display: flex; flex-direction: column; gap: 8px; }
.actions .btn { width: 100%; }

/* ── Forms ────────────────────────────────────────────────────────────── */

.field { display: grid; gap: 4px; margin-bottom: 12px; }
.field > span { font-weight: 550; font-size: .9rem; }
.field .hint { font-weight: 400; color: var(--muted); font-size: .82rem; }

input[type="text"],
input[type="password"],
input[type="url"],
input[type="number"],
input[type="search"],
select,
textarea {
  width: 100%;
  min-height: 36px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
textarea { resize: vertical; line-height: 1.45; }
textarea.mono { font-family: var(--mono); font-size: .85rem; tab-size: 2; }
textarea[readonly] { background: var(--surface-2); }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline-offset: 0; }

.inline-field { display: inline-flex; align-items: center; gap: 8px; font-size: .9rem; }
.inline-field select, .inline-field input { width: auto; }

.check { display: inline-flex; align-items: center; gap: 6px; font-size: .9rem; cursor: pointer; }
.check input { width: 16px; height: 16px; margin: 0; }

.form-errors {
  margin: 0 0 12px;
  padding: 8px 12px 8px 28px;
  border-radius: 8px;
  background: var(--danger-soft);
  color: var(--danger);
  font-size: .9rem;
}
.form-errors.single { padding-left: 12px; list-style: none; }

.result { margin-top: 12px; font-size: .9rem; }

/* ── Badges ───────────────────────────────────────────────────────────── */

.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 99px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: .75rem;
  font-weight: 600;
  line-height: 1.6;
  white-space: nowrap;
}
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-info { background: var(--info-soft); color: var(--info); }
.badge-accent { background: var(--accent-soft); color: var(--accent); }
.badges { display: inline-flex; gap: 4px; flex-wrap: wrap; }

/* ── Tables ───────────────────────────────────────────────────────────── */

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th {
  color: var(--muted);
  font-size: .78rem;
  font-weight: 600;
  letter-spacing: .02em;
  text-transform: uppercase;
  white-space: nowrap;
}
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.wide { min-width: 220px; }
td select { width: auto; min-height: 30px; padding: 2px 6px; }
tr.highlight td { background: var(--warn-soft); }
.items { margin: 0; padding-left: 1.1em; }
.clamp {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ── Overview ─────────────────────────────────────────────────────────── */

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}
.stat { margin: 0; }
.stat-label { color: var(--muted); font-size: .85rem; }
.stat-value { font-size: 1.6rem; font-weight: 650; line-height: 1.3; font-variant-numeric: tabular-nums; }
.stat-sub { color: var(--muted); font-size: .82rem; }

.meter { height: 8px; margin-top: 8px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.meter > span { display: block; height: 100%; width: 0; background: var(--accent); }
.meter.hot > span { background: var(--danger); }

.legend { display: flex; gap: 16px; font-size: .82rem; color: var(--muted); margin-bottom: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.legend .conv, .bar.conv { background: var(--accent); }
.legend .cost, .bar.cost { background: var(--info); }

.bars { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.bar-row {
  display: grid;
  grid-template-columns: 7.5em minmax(0, 1fr) 3em minmax(0, 1fr) 4.5em;
  gap: 8px;
  align-items: center;
  font-size: .85rem;
  font-variant-numeric: tabular-nums;
}
.bar-day { color: var(--muted); white-space: nowrap; }
.bar-val { text-align: right; white-space: nowrap; }
.bar-track { height: 10px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.bar { display: block; height: 100%; width: 0; border-radius: 99px; }

@media (max-width: 560px) {
  .bar-row { grid-template-columns: 4.6em minmax(0, 1fr) 2.2em minmax(0, 1fr) 3.8em; gap: 6px; font-size: .8rem; }
}

/* ── Conversation list ────────────────────────────────────────────────── */

.conv-list {
  list-style: none;
  margin: 0 0 16px;
  padding: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.conv-list li + li { border-top: 1px solid var(--border); }
.conv-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 4px 16px;
  padding: 12px 16px;
  color: inherit;
  text-decoration: none;
}
.conv-row:hover { background: var(--surface-2); color: inherit; }
.conv-row:focus-visible { outline-offset: -2px; border-radius: 0; }
.conv-first { display: block; font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-contact { display: block; color: var(--muted); font-size: .85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-side { display: grid; justify-items: end; gap: 4px; }
.conv-meta { color: var(--muted); font-size: .82rem; white-space: nowrap; }

@media (max-width: 560px) {
  .conv-row { grid-template-columns: minmax(0, 1fr); }
  .conv-side { justify-items: start; }
}

/* ── Conversation detail ──────────────────────────────────────────────── */

.detail {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 16px;
  align-items: start;
}
@media (max-width: 960px) {
  .detail { grid-template-columns: minmax(0, 1fr); }
}

.chat {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 62vh;
  overflow-y: auto;
  padding: 4px 2px;
  overscroll-behavior: contain;
}
@media (max-width: 560px) {
  .chat { max-height: none; }
}

.msg { max-width: min(82%, 680px); padding: 8px 12px; border-radius: 12px; overflow-wrap: anywhere; }
.msg-customer { align-self: flex-start; background: var(--bubble-customer); border-bottom-left-radius: 4px; }
.msg-agent { align-self: flex-end; background: var(--bubble-agent); border-bottom-right-radius: 4px; }
.msg-staff { align-self: flex-end; background: var(--bubble-staff); border-bottom-right-radius: 4px; }
.msg-notice {
  align-self: center;
  max-width: 92%;
  border: 1px dashed var(--border);
  color: var(--muted);
  font-size: .85rem;
  text-align: center;
}
.msg-meta { color: var(--muted); font-size: .75rem; margin-bottom: 2px; }
.msg-who { font-weight: 600; }
.msg-body p { margin: 0 0 .5em; }
.msg-body p:last-child { margin-bottom: 0; }
.msg-body ul, .msg-body ol { margin: .25em 0 .5em; padding-left: 1.3em; }
@media (max-width: 560px) {
  .msg { max-width: 94%; }
}

.cards { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 4px; font-size: .85rem; }
.cards li { padding: 6px 8px; border-radius: 8px; background: var(--surface); border: 1px solid var(--border); }

.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 4px 12px; margin: 0; font-size: .9rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }

.summary { white-space: pre-wrap; font-size: .9rem; }

/* Agent activity (debug trace) */
.activity > summary { cursor: pointer; font-weight: 600; }
.activity[open] > summary { margin-bottom: 8px; }
.trace-turn { border-top: 1px solid var(--border); padding: 10px 0; }
.trace-head { color: var(--muted); font-size: .82rem; }
.trace-context { color: var(--muted); font-size: .82rem; white-space: pre-wrap; margin: 4px 0 0; }
.tool { margin-top: 8px; border: 1px solid var(--border); border-radius: 8px; }
.tool > summary {
  cursor: pointer;
  padding: 6px 10px;
  font-family: var(--mono);
  font-size: .82rem;
  overflow-wrap: anywhere;
}
.tool > summary .muted { font-family: var(--font); }
.tool-body { padding: 0 10px 10px; }
.tool-body h3 { font-size: .78rem; text-transform: uppercase; letter-spacing: .02em; color: var(--muted); margin: 8px 0 4px; }

pre.code {
  margin: 4px 0;
  padding: 8px 10px;
  max-height: 28em;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  font-family: var(--mono);
  font-size: .8rem;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ── Knowledge base ───────────────────────────────────────────────────── */

.hits { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 8px; }
.hits li { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; }
.hits .hit-body { white-space: pre-wrap; font-size: .85rem; margin-top: 4px; max-height: 12em; overflow: auto; }

dialog {
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / 45%); }

/* ── Settings ─────────────────────────────────────────────────────────── */

.snippet { margin: 0 0 12px; }

/* ── Login ────────────────────────────────────────────────────────────── */

.login { max-width: 380px; margin: 12vh auto 0; }
.login .btn { width: 100%; }

/* ── Toasts ───────────────────────────────────────────────────────────── */

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 100;
  display: grid;
  gap: 8px;
  width: min(420px, calc(100vw - 32px));
}
.toast {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-left-width: 4px;
  border-radius: 10px;
  background: var(--surface);
  box-shadow: 0 6px 24px rgb(0 0 0 / 16%);
  font-size: .9rem;
}
.toast-error { border-left-color: var(--danger); }
.toast-warn { border-left-color: var(--warn); }
.toast-ok { border-left-color: var(--ok); }
.toast p { margin: 0; flex: 1 1 auto; overflow-wrap: anywhere; }
.toast button {
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 1.1rem;
  line-height: 1;
  padding: 0 2px;
  cursor: pointer;
}

/* ── Health ───────────────────────────────────────────────────────────── */

.danger-text { color: var(--danger); }
.legend .err { background: var(--danger); }
td.wrap { white-space: normal; overflow-wrap: anywhere; min-width: 16em; }

.waterfall { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.wf-row {
  display: grid;
  grid-template-columns: minmax(10em, 16em) minmax(0, 1fr) 5.5em;
  gap: 4px 10px;
  align-items: center;
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
  font-size: .85rem;
}
.wf-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wf-track { height: 10px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.wf-bar { display: block; height: 100%; min-width: 2px; border-radius: 3px; background: var(--accent); }
.wf-warn .wf-bar { background: var(--warn); }
.wf-error .wf-bar { background: var(--danger); }
.wf-error .wf-name { color: var(--danger); }
.wf-ms { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.wf-detail { grid-column: 1 / -1; overflow-wrap: anywhere; }
.wf-detail:empty { display: none; }

@media (max-width: 560px) {
  .wf-row { grid-template-columns: minmax(0, 1fr) 4.5em; }
  .wf-track { grid-column: 1 / -1; grid-row: 2; }
}
