:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #1c1f24;
  --muted: #646b76;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --ai: #7c3aed;
  --ai-bg: #f1eafe;
  --human: #c2410c;
  --human-bg: #fff1e6;
  --closed: #4b5563;
  --closed-bg: #eceef1;
  --ok: #15803d;
  --ok-bg: #e8f7ee;
  --err: #b91c1c;
  --err-bg: #fdecec;
  --bubble-in: #f0f2f5;
  --bubble-out: #e7f0ff;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115; --surface: #171a20; --surface-2: #1e222a; --border: #2a2f39;
    --text: #e6e8eb; --muted: #9aa2ae; --accent: #4c8dff;
    --ai: #b794ff; --ai-bg: #2a2140; --human: #ff9a5c; --human-bg: #3a2416;
    --closed: #aab1bb; --closed-bg: #262a31; --ok: #4ade80; --ok-bg: #12301f;
    --err: #ff7b7b; --err-bg: #3a1717; --bubble-in: #232832; --bubble-out: #1d2c47;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1115; --surface: #171a20; --surface-2: #1e222a; --border: #2a2f39;
  --text: #e6e8eb; --muted: #9aa2ae; --accent: #4c8dff;
  --ai: #b794ff; --ai-bg: #2a2140; --human: #ff9a5c; --human-bg: #3a2416;
  --closed: #aab1bb; --closed-bg: #262a31; --ok: #4ade80; --ok-bg: #12301f;
  --err: #ff7b7b; --err-bg: #3a1717; --bubble-in: #232832; --bubble-out: #1d2c47;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; word-break: break-all; }
pre { background: var(--surface-2); padding: 12px; border-radius: 8px; overflow: auto; font-size: 12.5px; }

/* ---------- layout ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px;
  padding: 0 20px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 650; letter-spacing: -0.01em; white-space: nowrap; }
.brand span { color: var(--muted); font-weight: 500; }
nav.tabs { display: flex; gap: 4px; }
nav.tabs a {
  text-decoration: none; color: var(--muted); padding: 7px 12px; border-radius: 8px; font-weight: 550;
}
nav.tabs a.active { background: var(--surface-2); color: var(--text); }
.spacer { flex: 1; }
.live { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12.5px; }
.live i { width: 8px; height: 8px; border-radius: 50%; background: var(--err); display: inline-block; }
.live.on i { background: var(--ok); }

main { height: calc(100vh - 56px); }
.view { display: none; height: 100%; }
.view.active { display: block; }

/* ---------- inbox ---------- */
.inbox { display: grid; grid-template-columns: 320px 1fr 300px; height: 100%; }
.pane { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.pane:last-child { border-right: 0; }
.pane-head { padding: 14px 16px; border-bottom: 1px solid var(--border); }
.pane-head h2 { margin: 0; font-size: 15px; }
.pane-body { overflow: auto; flex: 1; min-height: 0; }

.filters { display: flex; gap: 6px; margin-top: 10px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--border); background: var(--surface); border-radius: 999px;
  padding: 3px 10px; cursor: pointer; font-size: 12.5px; color: var(--muted);
}
.chip.active { background: var(--text); color: var(--surface); border-color: var(--text); }

.lead-item { padding: 12px 16px; border-bottom: 1px solid var(--border); cursor: pointer; }
.lead-item:hover { background: var(--surface-2); }
.lead-item.active { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--accent); }
.lead-top { display: flex; align-items: center; gap: 8px; }
.lead-name { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lead-time { color: var(--muted); font-size: 12px; }
.lead-preview { color: var(--muted); font-size: 13px; margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lead-meta { display: flex; gap: 6px; margin-top: 6px; align-items: center; }

.badge { font-size: 11.5px; font-weight: 600; padding: 1px 8px; border-radius: 999px; white-space: nowrap; }
.badge.ai { color: var(--ai); background: var(--ai-bg); }
.badge.human { color: var(--human); background: var(--human-bg); }
.badge.closed { color: var(--closed); background: var(--closed-bg); }
.badge.plat { color: var(--muted); background: var(--surface-2); font-weight: 500; }

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

.convo-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.convo-head h2 { flex: 1; min-width: 0; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.lead-links { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 4px; font-size: 12.5px; font-weight: 500; }
.lead-links a { text-decoration: none; }
.lead-links a:hover { text-decoration: underline; }

.thread { padding: 16px 20px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 72%; display: flex; flex-direction: column; gap: 3px; }
.msg.in { align-self: flex-start; }
.msg.out { align-self: flex-end; align-items: flex-end; }
.msg.note { align-self: center; max-width: 90%; }
.bubble { padding: 9px 12px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; }
.msg.in .bubble { background: var(--bubble-in); border-bottom-left-radius: 4px; }
.msg.out .bubble { background: var(--bubble-out); border-bottom-right-radius: 4px; }
.msg.note .bubble { background: none; color: var(--muted); font-size: 12.5px; padding: 2px; text-align: center; }
.msg.err .bubble { background: var(--err-bg); }
.msg-meta { font-size: 11.5px; color: var(--muted); }
.msg-err { font-size: 12px; color: var(--err); max-width: 100%; }

.composer { border-top: 1px solid var(--border); padding: 12px 16px; display: flex; flex-direction: column; gap: 8px; }
.composer-row { display: flex; gap: 8px; }
.composer textarea { flex: 1; resize: none; min-height: 42px; max-height: 140px; }
.composer .hint { font-size: 12px; color: var(--muted); }
.sim { border-top: 1px dashed var(--border); padding: 10px 16px; background: var(--surface-2); }
.sim .hint { font-size: 12px; color: var(--muted); margin-bottom: 6px; }

.details { padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.field input { width: 100%; }
.progress { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); }

/* ---------- forms / buttons ---------- */
input[type=text], input[type=password], textarea, select {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }
.btn {
  border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 7px 12px;
  cursor: pointer; font-weight: 550; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--err); }
.btn.sm { padding: 4px 9px; font-size: 12.5px; }
.btn:disabled { opacity: .55; cursor: default; }

/* ---------- pages ---------- */
.page { max-width: 920px; margin: 0 auto; padding: 24px 20px 60px; overflow: auto; height: 100%; }
.page h1 { font-size: 22px; margin: 0 0 4px; letter-spacing: -0.01em; }
.page > p.sub { color: var(--muted); margin: 0 0 20px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; box-shadow: var(--shadow); }
.card h3 { margin: 0 0 6px; font-size: 15px; }
.card p { margin: 6px 0; color: var(--muted); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }
.form-grid .full { grid-column: 1 / -1; }
.form-grid input, .form-grid textarea, .form-grid select { width: 100%; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; }

.kv { display: grid; grid-template-columns: 180px 1fr; gap: 8px 12px; align-items: center; }
.kv div:nth-child(odd) { color: var(--muted); }
.ok { color: var(--ok); } .bad { color: var(--err); }

.banner { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; }
.banner.err { background: var(--err-bg); color: var(--err); }
.banner.ok { background: var(--ok-bg); color: var(--ok); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--surface); padding: 9px 16px; border-radius: 8px; z-index: 50;
  box-shadow: 0 6px 20px rgba(0,0,0,.2); max-width: calc(100% - 32px);
}

/* ---------- responsive ---------- */
.back { display: none; }
@media (max-width: 1100px) {
  .inbox { grid-template-columns: 280px 1fr; }
  .inbox .pane.side { display: none; }
}
@media (max-width: 720px) {
  .topbar { padding: 0 16px; gap: 10px; }
  .brand span, .live span { display: none; }
  .inbox { grid-template-columns: 1fr; }
  .inbox .pane.convo { display: none; }
  .inbox.open .pane.list { display: none; }
  .inbox.open .pane.convo { display: flex; }
  .back { display: inline-block; }
  .msg { max-width: 88%; }
  .form-grid { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 1fr; }
  .page { padding: 20px 16px 48px; }
}
