:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --ink: #0f1b33;
  --muted: #5f6567;
  --line: #e3e6ea;
  --brand: #f08838;
  --brand-ink: #ffffff;
  --navy: #0f1b33;
  --in: #ffffff;
  --out-ai: #fff1e6;
  --out-human: #e7f0ff;
  --chat-bg: #efeae2;
  --hot: #e05840;
  --warm: #f8a838;
  --cold: #7b8794;
  --ok: #1f8a4c;
  --bad: #c0392b;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1222; --panel: #121b2e; --ink: #e8ecf3; --muted: #9aa4b2; --line: #24304a;
    --in: #1b2740; --out-ai: #3a2a1c; --out-human: #1d2f52; --chat-bg: #0e1626; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1222; --panel: #121b2e; --ink: #e8ecf3; --muted: #9aa4b2; --line: #24304a;
  --in: #1b2740; --out-ai: #3a2a1c; --out-human: #1d2f52; --chat-bg: #0e1626; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; }
textarea { resize: vertical; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; word-break: break-all; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.error { color: var(--bad); min-height: 1.2em; }

button { cursor: pointer; border: 0; border-radius: 8px; padding: 8px 14px; }
button:disabled { opacity: .55; cursor: progress; }
.primary { background: var(--brand); color: var(--brand-ink); font-weight: 600; }
.primary:hover { filter: brightness(.95); }
.ghost { background: transparent; border: 1px solid var(--line); }
.ghost:hover { border-color: var(--brand); }
button.small { padding: 5px 10px; font-size: 13px; }
button.tiny { padding: 2px 8px; font-size: 12px; }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.brand.big { font-size: 20px; }
.logo { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 8px; background: var(--brand); color: #fff; font-weight: 800; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; background: linear-gradient(135deg, var(--navy), #1f3358); }
.login-card { width: min(360px, 100%); background: var(--panel); padding: 28px; border-radius: 14px; display: grid; gap: 14px; box-shadow: 0 20px 50px rgb(0 0 0 / .25); }
.login-card label { display: grid; gap: 6px; font-weight: 600; }

/* Layout */
.app { height: 100%; display: grid; grid-template-rows: auto 1fr; }
.topbar { display: flex; align-items: center; gap: 16px; padding: 8px 16px; background: var(--navy); color: #fff; }
.topbar .ghost { color: #fff; border-color: rgb(255 255 255 / .3); }
.tabs { display: flex; gap: 4px; }
.tab { background: transparent; color: rgb(255 255 255 / .75); }
.tab.active { background: rgb(255 255 255 / .12); color: #fff; }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
main { min-height: 0; }
.view { height: 100%; }

.switch { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.slider { width: 36px; height: 20px; border-radius: 20px; background: #9aa4b2; position: relative; transition: background .15s; }
.slider::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked + .slider { background: var(--ok); }
.switch input:checked + .slider::after { transform: translateX(16px); }
.switch input:focus-visible + .slider { outline: 2px solid var(--brand); }
.switch-label { font-size: 13px; }

/* Inbox */
.inbox { display: grid; grid-template-columns: 340px 1fr auto; min-height: 0; }
.list-pane { border-right: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.list-tools { padding: 10px; display: grid; gap: 8px; border-bottom: 1px solid var(--line); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { padding: 4px 10px; border-radius: 99px; background: var(--bg); border: 1px solid var(--line); font-size: 12.5px; }
.chip.active { background: var(--brand); color: #fff; border-color: var(--brand); }
.contact-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.contact { padding: 10px 12px; border-bottom: 1px solid var(--line); cursor: pointer; display: grid; gap: 3px; }
.contact:hover { background: var(--bg); }
.contact.active { background: var(--out-ai); }
.contact .row { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.contact .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact .preview { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.time { font-size: 12px; flex: none; }
.badge { background: var(--ok); color: #fff; border-radius: 99px; padding: 0 7px; font-size: 12px; font-weight: 700; }
.tags { display: flex; gap: 4px; flex-wrap: wrap; }
.tag { font-size: 11px; padding: 1px 7px; border-radius: 99px; background: var(--line); text-transform: capitalize; }
.tag.hot { background: var(--hot); color: #fff; }
.tag.warm { background: var(--warm); color: #3a2400; }
.tag.cold { background: var(--cold); color: #fff; }
.tag.human { background: #3867d6; color: #fff; text-transform: none; }
.tag.light { background: transparent; border: 1px solid var(--line); text-transform: none; }
.tag.ok { background: var(--ok); color: #fff; text-transform: none; }
.empty { padding: 24px; color: var(--muted); text-align: center; }
.empty.center { display: grid; place-items: center; height: 100%; }

.chat-pane { min-width: 0; min-height: 0; background: var(--chat-bg); }
.chat { display: grid; grid-template-rows: auto 1fr auto auto; height: 100%; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
.chat-head .who { display: grid; flex: 1; min-width: 0; }
.chat-head .who strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { display: none; }
.messages { overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 6px; }
.bubble { max-width: min(560px, 80%); padding: 7px 10px 5px; border-radius: 10px; box-shadow: 0 1px 1px rgb(0 0 0 / .08); white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble.in { align-self: flex-start; background: var(--in); border-top-left-radius: 2px; }
.bubble.out { align-self: flex-end; border-top-right-radius: 2px; }
.bubble.out.ai { background: var(--out-ai); }
.bubble.out.human { background: var(--out-human); }
.bubble .meta { display: flex; gap: 6px; justify-content: flex-end; align-items: center; font-size: 11px; color: var(--muted); margin-top: 2px; }
.bubble .who { font-weight: 700; }
.tick.read { color: #34b7f1; }
.tick.failed { color: var(--bad); }
.msg-error { color: var(--bad); font-size: 12px; }
.note { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); padding: 3px 10px; border-radius: 99px; }
.composer { display: flex; gap: 8px; padding: 10px; background: var(--panel); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { resize: none; max-height: 160px; }
.window-note { margin: 0; padding: 8px 12px; background: #fff4d6; color: #5c4400; font-size: 13px; }

.lead-pane { width: 300px; border-left: 1px solid var(--line); background: var(--panel); padding: 12px 14px; overflow-y: auto; }
.lead-head { display: flex; justify-content: space-between; align-items: center; margin: 4px 0 10px; }
.lead-head h3 { margin: 0; }
.lead-form { display: grid; gap: 8px; }
.lead-form label { display: grid; gap: 3px; font-size: 12.5px; color: var(--muted); }
.lead-score { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }

/* Setup & activity */
.setup, .activity { overflow-y: auto; }
.setup-wrap { max-width: 860px; margin: 0 auto; padding: 20px 16px 80px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin: 14px 0; }
.card h3 { margin: 0 0 8px; }
.fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px; }
.fields label { display: grid; gap: 4px; font-weight: 600; font-size: 13px; }
.fields label.wide { grid-column: 1 / -1; }
.fields textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; font-weight: 400; }
.label-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.copy-row { display: grid; grid-template-columns: 140px 1fr auto; gap: 10px; align-items: center; padding: 6px 0; border-top: 1px solid var(--line); }
.checklist { list-style: none; padding: 0; display: flex; gap: 14px; flex-wrap: wrap; }
.checklist li { background: var(--panel); border: 1px solid var(--line); padding: 6px 12px; border-radius: 99px; }
.save-bar { position: sticky; bottom: 0; display: flex; justify-content: flex-end; align-items: center; gap: 12px; padding: 12px 0; background: var(--bg); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: grid; }
.stat strong { font-size: 24px; }
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; background: var(--panel); }
th, td { text-align: left; padding: 6px 10px; border-bottom: 1px solid var(--line); font-size: 13px; white-space: nowrap; }
.events { list-style: none; padding: 0; font-size: 13px; }
.events li { padding: 6px 0; border-bottom: 1px solid var(--line); overflow-wrap: anywhere; }
.events li.error { color: var(--bad); }
.events li.warn { color: #9a6200; }

.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--navy); color: #fff; padding: 10px 16px; border-radius: 10px; max-width: min(560px, calc(100% - 32px)); box-shadow: 0 8px 24px rgb(0 0 0 / .25); z-index: 10; }
.toast.bad { background: var(--bad); }

@media (max-width: 1100px) {
  .inbox { grid-template-columns: 300px 1fr; }
  .lead-pane { position: fixed; right: 0; top: 52px; bottom: 0; z-index: 5; box-shadow: -8px 0 24px rgb(0 0 0 / .15); }
}
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 12px; }
  .top-right { gap: 8px; }
  .inbox { grid-template-columns: 1fr; }
  .chat-pane { display: none; }
  body.chat-open .list-pane { display: none; }
  body.chat-open .chat-pane { display: block; }
  .back { display: inline-block; }
  .lead-pane { width: 100%; top: 0; }
  .copy-row { grid-template-columns: 1fr auto; }
  .copy-row span { grid-column: 1 / -1; }
  .bubble { max-width: 88%; }
}
.register-row input { width: 140px; }

/* Leads, knowledge, usage */
.tag.bot { background: #6b7280; color: #fff; text-transform: none; }
.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--line)); }
a.button { display: inline-flex; align-items: center; padding: 8px 14px; border-radius: 8px; text-decoration: none; color: inherit; cursor: pointer; white-space: nowrap; }
label.button { display: inline-flex; align-items: center; padding: 8px 14px; border-radius: 8px; cursor: pointer; }
.wide-wrap { max-width: 1400px; margin: 0 auto; padding: 20px 16px 60px; }
.leads-view, .knowledge-view { overflow-y: auto; }
.leads-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.leads-tools { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.leads-tools input { width: 240px; }
.leads-tools select { width: auto; }
#leads-table td { white-space: normal; max-width: 220px; vertical-align: top; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--bg); }
.lead-actions { display: grid; gap: 8px; margin-top: 12px; }
.knowledge-grid { display: grid; grid-template-columns: 340px 1fr; gap: 16px; align-items: start; }
.knowledge-grid .card { margin: 0; }
.doc-list { list-style: none; padding: 0; margin: 10px 0; display: grid; gap: 6px; }
.doc { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; display: grid; gap: 2px; }
.doc:hover { border-color: var(--brand); }
.doc.active { background: var(--out-ai); border-color: var(--brand); }
.doc.off { opacity: .6; }
.upload-box, .kb-test { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.kb-test input { flex: 1; min-width: 160px; }
.kb-results { white-space: pre-wrap; font-size: 12px; background: var(--bg); padding: 10px; border-radius: 8px; max-height: 320px; overflow: auto; }
.doc-editor { display: grid; gap: 10px; }
.doc-editor textarea { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.row-inline { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
label.inline { display: inline-flex; gap: 6px; align-items: center; font-size: 13px; }
label.inline input { width: auto; }
.stat .small { font-size: 12px; }
@media (max-width: 900px) {
  .knowledge-grid { grid-template-columns: 1fr; }
  .leads-tools input { width: 100%; }
  .tabs { overflow-x: auto; max-width: 100%; }
}

.totals { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 12px; }
.total-card { background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--brand); border-radius: var(--radius); padding: 14px 16px; display: grid; gap: 8px; }
.total-label { font-weight: 700; }
.total-row { display: flex; gap: 24px; flex-wrap: wrap; }
.total-row > div { display: grid; }
.total-row strong { font-size: 26px; line-height: 1.1; }
.tag.callback { background: #fff1d6; color: #7a4b00; text-transform: none; }
.tag.callback.due { background: var(--hot); color: #fff; }

.segmented { display: inline-flex; gap: 4px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 4px; margin-bottom: -8px; }
.seg { background: transparent; border-radius: 7px; padding: 6px 12px; display: inline-flex; gap: 6px; align-items: center; }
.seg.active { background: var(--brand); color: #fff; }
.answers-wrap h3 { margin: 20px 0 8px; }
.ia-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(360px, 1fr)); gap: 12px; }
.ia-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; display: grid; gap: 8px; }
.ia-card.pending { border-color: var(--warm); box-shadow: inset 4px 0 0 var(--warm); }
.ia-card input { font-weight: 600; }
.ia-card .actions { margin-top: 0; }
.ia-card details textarea { margin-top: 6px; }
.saved-line { color: var(--ok); font-size: 13px; font-weight: 600; }
@media (max-width: 760px) { .ia-list { grid-template-columns: 1fr; } }

.checklist-box { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; display: grid; gap: 6px; }
.check-head { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.checks { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 2px 10px; font-size: 12.5px; }
.checks .yes { color: var(--ok); }
.checks .no { color: var(--muted); }
.funnel { display: flex; gap: 8px; flex-wrap: wrap; }
.funnel-step { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; display: grid; min-width: 130px; position: relative; }
.funnel-step strong { font-size: 22px; }
.funnel-step.dim { opacity: .7; }
