:root {
  --bg: #f4f5f7; --surface: #ffffff; --surface-2: #f8f9fb; --border: #e3e6ea; --text: #1d2125; --muted: #66707a;
  --accent: #128c7e; --accent-strong: #0b6e63; --accent-soft: #e2f4f1;
  --bubble-in: #ffffff; --bubble-bot: #e8f0fe; --bubble-agent: #dcf8c6; --chat-bg: #efeae2;
  --ok: #1f8a4c; --ok-soft: #e3f5ea; --warn: #9a6400; --warn-soft: #fff4dc; --bad: #c62828; --bad-soft: #fdecea;
  --read: #34b7f1; --radius: 10px; --shadow: 0 1px 2px rgba(0,0,0,.06), 0 1px 8px rgba(0,0,0,.04);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --shadow-lg: 0 12px 32px rgba(15,30,28,.18);
  --sb-w: 256px; --sb-rail-w: 72px; --bar-h: 0px;
  --sb-bg: linear-gradient(180deg, #0f4a42 0%, #0a332e 100%); --sb-border: rgba(255,255,255,.08);
  --sb-text: #d5ece8; --sb-muted: #8fb7b0; --sb-hover: rgba(255,255,255,.07);
  --sb-active: rgba(255,255,255,.14); --sb-active-text: #ffffff; --sb-glow: #3fe0c5; --sb-ring: #0e423b;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1214; --surface: #171b1e; --surface-2: #1d2226; --border: #2a3035; --text: #e6e8ea; --muted: #97a0a8;
    --accent: #25b39e; --accent-strong: #3fd0ba; --accent-soft: #123a35;
    --bubble-in: #20262a; --bubble-bot: #1d2a40; --bubble-agent: #144d37; --chat-bg: #0b1013;
    --ok: #4cc27f; --ok-soft: #133524; --warn: #f0b443; --warn-soft: #3a2c10; --bad: #ff6b6b; --bad-soft: #3d1717;
    --shadow: 0 1px 2px rgba(0,0,0,.4); --shadow-lg: 0 12px 32px rgba(0,0,0,.55);
    --sb-bg: #111619; --sb-border: #222a2f; --sb-text: #c9d1d6; --sb-muted: #7d8890; --sb-hover: rgba(255,255,255,.05);
    --sb-active: rgba(63,208,186,.14); --sb-active-text: #e9fffb; --sb-ring: #111619;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1214; --surface: #171b1e; --surface-2: #1d2226; --border: #2a3035; --text: #e6e8ea; --muted: #97a0a8;
  --accent: #25b39e; --accent-strong: #3fd0ba; --accent-soft: #123a35;
  --bubble-in: #20262a; --bubble-bot: #1d2a40; --bubble-agent: #144d37; --chat-bg: #0b1013;
  --ok: #4cc27f; --ok-soft: #133524; --warn: #f0b443; --warn-soft: #3a2c10; --bad: #ff6b6b; --bad-soft: #3d1717;
  --shadow: 0 1px 2px rgba(0,0,0,.4); --shadow-lg: 0 12px 32px rgba(0,0,0,.55);
  --sb-bg: #111619; --sb-border: #222a2f; --sb-text: #c9d1d6; --sb-muted: #7d8890; --sb-hover: rgba(255,255,255,.05);
  --sb-active: rgba(63,208,186,.14); --sb-active-text: #e9fffb; --sb-ring: #111619;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 var(--font); }
a { color: var(--accent); }
code, .mono { font-family: var(--mono); font-size: .92em; }
h1 { font-size: 22px; margin: 0 0 6px; }
h2 { font-size: 17px; margin: 0 0 10px; }
h3 { font-size: 15px; margin: 16px 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.hidden { display: none !important; }
.text-bad { color: var(--bad); }
.nowrap { white-space: nowrap; }

/* ---------- controls ---------- */
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 9px; width: 100%; min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
input[type=checkbox] { width: auto; accent-color: var(--accent); }
textarea { resize: vertical; }
label { display: block; font-size: 13px; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: 4px; }
label.chk { display: inline-flex; align-items: center; gap: 6px; color: var(--text); }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; margin: 0; display: flex; flex-wrap: wrap; gap: 4px 14px; }
legend { font-size: 12px; color: var(--muted); padding: 0 4px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; font: inherit; font-weight: 500;
  padding: 7px 14px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-strong); }
.btn.danger { color: var(--bad); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.tiny { padding: 2px 8px; font-size: 12px; }
.btn.block { width: 100%; }

.badge { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2);
  border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.badge.mode-bot { background: var(--bubble-bot); color: var(--text); border-color: transparent; }
.badge.mode-human { background: var(--bubble-agent); color: var(--text); border-color: transparent; }

/* ---------- layout ---------- */
.brand { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text); text-decoration: none; }
.brand.big { font-size: 22px; margin-bottom: 4px; }
.brand-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--accent); }
.flashes { margin: 12px 0 0; padding: 0 32px; }
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 8px; background: var(--surface); border: 1px solid var(--border); }
.flash.success { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.flash.error { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
/* ---------- shared page components (used by every page; macros in templates/_ui.html) ---------- */
/* Pages are full width with flat sections separated by dividers; no boxed containers. */
.page { padding: 28px 32px 50px; }
.page-header { display: flex; justify-content: space-between; align-items: flex-end; flex-wrap: wrap; gap: 18px 24px;
  margin-bottom: 28px; }
.page-title { display: flex; align-items: center; gap: 14px; min-width: 0; }
.page-title h1 { font-size: 27px; font-weight: 750; letter-spacing: -.7px; margin: 0 0 6px; }
.page-title p { margin: 0; color: var(--muted); line-height: 1.6; max-width: 820px; }
.page-icon, .tile-icon { width: 48px; height: 48px; flex-shrink: 0; display: grid; place-items: center; border-radius: 14px;
  background: var(--ok-soft); color: var(--ok); }
.page-icon .icon { width: 24px; height: 24px; }
.tile-icon { width: 44px; height: 44px; border-radius: 12px; font-size: 22px; font-weight: 600; }
.tile-icon .icon { width: 22px; height: 22px; }
.page-actions { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px; }
.page-actions form { margin: 0; }

/* .card is kept as the name for a page section: flat, separated from the previous one by a line */
.card { padding: 26px 0; border-top: 1px solid var(--border); }
.card h2 { font-size: 17px; font-weight: 700; margin: 0 0 6px; }
.card.highlight { margin-bottom: 26px; padding: 18px 20px; border: 1px solid var(--accent); border-radius: var(--radius);
  background: var(--accent-soft); }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px 15px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }
.section-desc { color: var(--muted); font-size: 13px; line-height: 1.6; margin: 6px 0 16px; }
.section-label { display: block; font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase;
  letter-spacing: .07em; margin-bottom: 12px; }
.subsection { margin-top: 18px; padding-top: 18px; border-top: 1px dashed var(--border); }
.subsection-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 12px; }
.subsection-head h3 { margin: 0; }
.empty-note { text-align: center; padding: 26px 15px; color: var(--muted); font-size: 13px; }
.empty-note p { margin: 4px 0; }
.text-ok { color: var(--ok); }
.count-pill { background: var(--surface-2); color: var(--muted); padding: 4px 9px; border-radius: 20px; font-size: 11px;
  font-weight: 700; white-space: nowrap; }

/* status pill with a dot: connected / failed / needs attention / unknown */
.status-badge { display: inline-flex; align-items: center; gap: 7px; padding: 6px 11px; border-radius: 30px; font-size: 11px;
  font-weight: 700; white-space: nowrap; background: var(--surface-2); color: var(--muted); }
.status-badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.status-badge.ok { background: var(--ok-soft); color: var(--ok); }
.status-badge.bad { background: var(--bad-soft); color: var(--bad); }
.status-badge.warn { background: var(--warn-soft); color: var(--warn); }

/* an item (e.g. a WhatsApp number) with its icon, name, subtitle and status */
.entity-head { display: flex; justify-content: space-between; align-items: center; gap: 15px; margin-bottom: 18px; }
.entity { display: flex; align-items: center; gap: 14px; min-width: 0; }
.entity h2 { font-size: 16px; margin: 0 0 4px; }
.entity p { margin: 0; font-size: 12px; color: var(--muted); }

/* label/value pairs in two columns */
.info-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; margin: 0 0 18px; }
.info-grid dt { font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.info-grid dd { margin: 0; font-size: 13px; font-weight: 600; overflow-wrap: anywhere; }

/* collapsible block with a +/- marker */
details.disclosure { margin: 0; padding: 16px 0; border-top: 1px dashed var(--border); }
details.disclosure > summary { list-style: none; display: flex; justify-content: space-between; align-items: center;
  font-weight: 600; padding: 0; }
details.disclosure > summary::-webkit-details-marker { display: none; }
details.disclosure > summary::after { content: "+"; font-size: 20px; line-height: 1; color: var(--muted); }
details.disclosure[open] > summary::after { content: "\2212"; }
.disclosure-body { padding-top: 18px; }

/* figures: a thin rule on the left instead of a box */
.metrics { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 22px 28px; }
.metric { padding: 2px 0 2px 14px; border-left: 3px solid var(--border); min-width: 0; }
.metric-v { font-size: 28px; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; }
.metric-v.metric-v-sm { font-size: 15px; font-weight: 600; padding: 7px 0 4px; }
.metric-l { margin-top: 4px; font-size: 12px; color: var(--muted); }
.metric.bad { border-left-color: var(--bad); }
.metric.bad .metric-v { color: var(--bad); }
.kv.defs { grid-template-columns: minmax(180px, max-content) 1fr; gap: 8px 24px; margin: 0; font-size: 13px; }
.kv.defs dt { font-family: var(--mono); font-size: 12px; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
.actions form { margin: 0; }
details { margin-top: 12px; }
details > summary { cursor: pointer; color: var(--accent); font-weight: 500; padding: 4px 0; }
details.rule { border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; margin-top: 8px; }
details.rule > summary { color: var(--text); font-weight: 400; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
details.rule.inactive { opacity: .65; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 12px; margin-top: 10px; }
.grid-form .span2 { grid-column: 1 / -1; }
.form-actions { grid-column: 1 / -1; display: flex; gap: 8px; }
.inline-form { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; margin: 10px 0; }
.inline-form select, .inline-form input { width: auto; }
.copy-row { display: flex; gap: 8px; }
.copy-row input { font-family: var(--mono); }
.table { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: 13px; display: block; overflow-x: auto; }
.table th, .table td { text-align: left; padding: 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th { color: var(--muted); font-weight: 500; white-space: nowrap; }
.table td form { margin: 0; }
.table tr.muted td { color: var(--muted); }
.pager { display: flex; gap: 8px; margin-top: 12px; }

/* tokens */
.token-status { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; margin-top: 12px; }
.token-box { border: 1px solid var(--border); border-radius: 8px; padding: 12px; background: var(--surface-2); display: grid; gap: 6px; align-content: start; }
.token-box form { margin: 0; }
.token-title { font-weight: 600; }
.token-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; margin-top: 14px; }
.token-form { border: 1px solid var(--border); border-radius: 8px; padding: 14px; display: grid; gap: 10px; align-content: start; }
.token-form h3 { margin: 0; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.token-form p { margin: 0; }

/* login */
body.center-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; padding: 28px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; box-shadow: var(--shadow-lg); }
.login-card form { display: grid; gap: 12px; margin-top: 14px; }

/* ---------- inbox ---------- */
body.inbox-page { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
body.inbox-page .flashes { position: absolute; top: calc(var(--bar-h) + 12px); left: 50%; transform: translateX(-50%); z-index: 20; }
.inbox { flex: 1; min-height: 0; display: grid; grid-template-columns: 340px minmax(0, 1fr) 300px; }
.pane { min-height: 0; display: flex; flex-direction: column; background: var(--surface); }
.list-pane { border-right: 1px solid var(--border); }
.details-pane { border-left: 1px solid var(--border); overflow-y: auto; padding: 16px; }
.list-head { padding: 12px; border-bottom: 1px solid var(--border); display: grid; gap: 8px; }
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.filters select, .filters input { padding: 5px 7px; font-size: 13px; }
.filters label.date { font-size: 12px; }
.filters .span2 { grid-column: 1 / -1; }
.list-meta { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.conn.ok { color: var(--ok); } .conn.bad { color: var(--bad); }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv { padding: 10px 12px; border-bottom: 1px solid var(--border); cursor: pointer; display: grid; gap: 3px; }
.conv:hover, .conv:focus-visible { background: var(--surface-2); outline: none; }
.conv.selected { background: var(--accent-soft); }
.conv-top, .conv-mid { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.conv-name { min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv.unread .conv-name { color: var(--accent-strong); }
.conv-time { font-size: 12px; color: var(--muted); white-space: nowrap; }
.conv-preview { flex: 1; min-width: 0; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.conv.unread .conv-preview { color: var(--text); font-weight: 500; }
.conv-preview.failed { color: var(--bad); }
.unread-badge { background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; border-radius: 999px; min-width: 20px; padding: 1px 6px; text-align: center; }
.conv-bottom { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.conv-phone { font-size: 11px; color: var(--muted); margin-left: auto; }
.chip { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); display: inline-flex; align-items: center; gap: 4px; }
.chip.muted { color: var(--muted); }
.chip.label { background: var(--warn-soft); border-color: transparent; color: var(--text); }
.chip.status-open { color: var(--ok); } .chip.status-pending { color: var(--warn); }
.chip.status-resolved, .chip.status-archived { color: var(--muted); }
.chip-x { border: 0; background: none; color: inherit; cursor: pointer; padding: 0; font-size: 13px; line-height: 1; }
.list-empty { padding: 24px; text-align: center; color: var(--muted); }

.chat-pane { background: var(--chat-bg); }
.empty-state { margin: auto; text-align: center; color: var(--muted); }
.empty-icon { font-size: 42px; }
.chat { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
.chat-head > div { flex: 1; min-width: 0; }
.chat-title { font-weight: 650; font-size: 15px; }
.back-btn, .details-toggle { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 16px 6%; display: flex; flex-direction: column; }
.load-older { align-self: center; margin-bottom: 8px; background: var(--surface); }
#message-list { display: flex; flex-direction: column; gap: 6px; }
.day-sep { align-self: center; font-size: 12px; background: var(--surface); color: var(--muted); padding: 3px 10px; border-radius: 8px; margin: 8px 0; box-shadow: var(--shadow); }
.msg { max-width: min(72%, 560px); padding: 6px 10px 4px; border-radius: 10px; box-shadow: var(--shadow); overflow-wrap: anywhere; }
.msg.inbound { align-self: flex-start; background: var(--bubble-in); border-top-left-radius: 2px; }
.msg.outbound { align-self: flex-end; border-top-right-radius: 2px; }
.msg.sender-bot { background: var(--bubble-bot); }
.msg.sender-agent { background: var(--bubble-agent); }
.msg.sender-system { background: var(--surface-2); align-self: center; }
.msg-sender { font-size: 11px; font-weight: 650; color: var(--accent-strong); margin-bottom: 2px; }
.msg.sender-bot .msg-sender { color: #3b6fd8; }
.msg-text { white-space: pre-wrap; }
.msg-kind { font-size: 12px; color: var(--muted); font-style: italic; }
.msg-image { max-width: 260px; max-height: 260px; border-radius: 6px; display: block; margin: 2px 0; }
.msg-file { display: inline-block; margin: 2px 0; }
.msg-meta { display: flex; justify-content: flex-end; gap: 6px; font-size: 11px; color: var(--muted); margin-top: 2px; }
.msg-status.st-read { color: var(--read); }
.msg-status.st-failed, .msg-status.st-unknown { color: var(--bad); font-weight: 700; }
.msg-error { font-size: 12px; color: var(--bad); background: var(--bad-soft); border-radius: 6px; padding: 4px 6px; margin-top: 4px; }
.window-banner { padding: 8px 16px; font-size: 13px; border-top: 1px solid var(--border); }
.window-banner.ok { background: var(--surface); color: var(--muted); }
.window-banner.warn { background: var(--warn-soft); color: var(--warn); }
.composer { display: flex; gap: 8px; padding: 10px 16px; background: var(--surface); border-top: 1px solid var(--border); align-items: end; }
.composer textarea { flex: 1; max-height: 160px; }
.composer-actions { display: flex; gap: 6px; }
.template-panel { background: var(--surface); border-top: 1px solid var(--border); padding: 12px 16px; display: grid; gap: 8px; }
.tp-head { display: flex; justify-content: space-between; align-items: center; }
#template-params { display: grid; gap: 6px; }
.details-pane h3 { margin: 0; font-size: 17px; }
.details-pane label { margin-top: 12px; }
.details-pane .btn { margin-top: 8px; }
.mode-box { margin-top: 14px; padding: 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.labels { display: flex; flex-wrap: wrap; gap: 4px; margin: 4px 0; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--surface);
  padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); z-index: 50; max-width: calc(100vw - 32px); }
.toast.error { background: var(--bad); color: #fff; }

@media (max-width: 1100px) {
  .inbox { grid-template-columns: 300px minmax(0, 1fr); }
  .details-pane { display: none; }
  .details-toggle { display: inline-flex; }
  .inbox.show-details .details-pane { display: block; position: fixed; right: 0; top: var(--bar-h); bottom: 0; width: min(320px, 100vw);
    z-index: 15; box-shadow: -4px 0 16px rgba(0,0,0,.15); }
}
@media (max-width: 760px) {
  .inbox { grid-template-columns: 1fr; }
  .chat-pane { display: none; }
  .inbox.show-chat .list-pane { display: none; }
  .inbox.show-chat .chat-pane { display: flex; }
  .back-btn { display: inline-flex; }
  .messages { padding: 12px; }
  .msg { max-width: 88%; }
}

/* ---------- app shell: sidebar navigation ---------- */
.shell { display: flex; min-height: 100vh; }
.main { flex: 1; min-width: 0; position: relative; }
body.inbox-page .shell { flex: 1; min-height: 0; }
body.inbox-page .main { display: flex; flex-direction: column; min-height: 0; }

.sidebar { position: sticky; top: 0; height: 100vh; width: var(--sb-w); flex: none; z-index: 30;
  display: flex; flex-direction: column; background: var(--sb-bg); color: var(--sb-text); border-right: 1px solid var(--sb-border); }
body.inbox-page .sidebar { height: 100%; }
.sidebar .icon { width: 20px; height: 20px; flex: none; }
.sidebar :focus-visible { outline: 2px solid var(--sb-glow, var(--accent-strong)); outline-offset: 1px; }

.sb-head { display: flex; align-items: center; gap: 10px; height: 68px; padding: 0 16px; flex: none; }
.sb-brand { display: flex; align-items: center; gap: 11px; min-width: 0; flex: 1; color: inherit; text-decoration: none; }
.brand-mark { width: 36px; height: 36px; border-radius: 11px; flex: none; display: grid; place-items: center; color: #fff;
  background: linear-gradient(135deg, #25d366, #128c7e); box-shadow: 0 4px 14px rgba(37,211,102,.32); }
.sb-brand-text { display: grid; min-width: 0; line-height: 1.2; }
.sb-brand-text strong { color: #fff; font-size: 15px; letter-spacing: .01em; }
.sb-brand-text small { color: var(--sb-muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sb-icon-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 9px; border: 0;
  background: transparent; color: inherit; cursor: pointer; flex: none; }
.sb-icon-btn:hover { background: var(--sb-hover); }
.sb-collapse { position: absolute; top: 21px; right: -13px; width: 26px; height: 26px; border-radius: 50%;
  background: var(--surface); color: var(--muted); border: 1px solid var(--border); box-shadow: var(--shadow); opacity: 0; }
.sb-collapse .icon { width: 14px; height: 14px; }
.sb-collapse:hover { background: var(--surface); color: var(--accent); }
.sidebar:hover .sb-collapse, .sb-collapse:focus-visible { opacity: 1; }
.sb-close, .sb-scrim, .mobile-bar { display: none; }

.sb-nav { flex: 1; overflow-x: hidden; overflow-y: auto; padding: 4px 12px 12px; scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,.18) transparent; }
.nav-section { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .09em; color: var(--sb-muted);
  margin: 18px 12px 6px; white-space: nowrap; }
.nav-section:first-child { margin-top: 6px; }
.nav-item, .nav-group-toggle { position: relative; display: flex; align-items: center; gap: 12px; width: 100%; height: 40px;
  padding: 0 12px; margin: 2px 0; border-radius: 10px; border: 0; background: transparent; color: var(--sb-text);
  font: inherit; font-weight: 500; text-align: left; text-decoration: none; cursor: pointer; white-space: nowrap; }
.nav-item .icon, .nav-group-toggle .icon { opacity: .8; }
.nav-item:hover, .nav-group-toggle:hover { background: var(--sb-hover); color: #fff; }
.nav-item:hover .icon, .nav-group-toggle:hover .icon { opacity: 1; }
.nav-item.active { background: var(--sb-active); color: var(--sb-active-text); font-weight: 600; }
.nav-item.active .icon { opacity: 1; color: var(--sb-glow, var(--accent-strong)); }
.nav-item.active::before { content: ""; position: absolute; left: -12px; top: 10px; bottom: 10px; width: 3px;
  border-radius: 0 3px 3px 0; background: var(--sb-glow, var(--accent-strong)); }
.nav-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.nav-badge { min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px; display: inline-grid; place-items: center;
  background: #25d366; color: #053d2f; font-size: 11px; font-weight: 700; font-variant-numeric: tabular-nums; }

.nav-group-toggle .chevron { width: 16px; height: 16px; opacity: .6; }
.nav-group.open > .nav-group-toggle .chevron { transform: rotate(180deg); }
.nav-group.has-active:not(.open) > .nav-group-toggle { color: #fff; }
.nav-group-body { display: grid; grid-template-rows: 0fr; }
.nav-group.open > .nav-group-body { grid-template-rows: 1fr; }
.nav-group-inner { min-height: 0; overflow: hidden; visibility: hidden; margin-left: 22px; padding-left: 8px;
  border-left: 1px solid var(--sb-border); }
.nav-group.open .nav-group-inner { visibility: visible; }
.nav-group-inner .nav-item { height: 36px; font-size: 13.5px; gap: 10px; padding: 0 10px; }
.nav-group-inner .nav-item .icon { width: 17px; height: 17px; }
.nav-group-inner .nav-item.active::before { left: -9px; top: 8px; bottom: 8px; border-radius: 3px; }

.sb-foot { position: relative; flex: none; padding: 12px; border-top: 1px solid var(--sb-border); }
.user-btn { display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px; border-radius: 12px; border: 0;
  background: var(--sb-hover); color: var(--sb-text); font: inherit; text-align: left; cursor: pointer; }
.user-btn:hover, .user-btn[aria-expanded="true"] { background: rgba(255,255,255,.12); }
.user-btn .chevron { width: 16px; height: 16px; opacity: .6; transform: rotate(180deg); }
.user-btn[aria-expanded="true"] .chevron { transform: none; }
.avatar { width: 34px; height: 34px; border-radius: 50%; flex: none; display: grid; place-items: center; color: #fff;
  font-weight: 650; background: linear-gradient(135deg, #25d366, #128c7e); }
.avatar.lg { width: 40px; height: 40px; font-size: 16px; }
.user-meta { flex: 1; min-width: 0; display: grid; line-height: 1.25; }
.user-name { color: #fff; font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-role { color: var(--sb-muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }

.user-menu { position: absolute; left: 12px; right: 12px; bottom: calc(100% - 4px); z-index: 40; padding: 8px;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow-lg);
  animation: pop-in .14s ease-out; }
.user-menu[hidden] { display: none; }
.user-menu .icon { width: 17px; height: 17px; }
.um-head { display: flex; align-items: center; gap: 10px; padding: 6px 6px 12px; border-bottom: 1px solid var(--border); }
.um-head > div { min-width: 0; }
.um-name { font-weight: 600; }
.um-email { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.um-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 12px 6px 6px; }
.theme-switch { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 4px; border-radius: 10px;
  background: var(--surface-2); border: 1px solid var(--border); }
.theme-switch button { display: grid; justify-items: center; gap: 3px; padding: 7px 4px; border: 0; border-radius: 7px;
  background: transparent; color: var(--muted); font: inherit; font-size: 12px; cursor: pointer; }
.theme-switch button:hover { color: var(--text); }
.theme-switch button[aria-pressed="true"] { background: var(--surface); color: var(--accent-strong); box-shadow: var(--shadow); font-weight: 600; }
.user-menu form { margin: 8px 0 0; padding-top: 6px; border-top: 1px solid var(--border); }
.um-item { display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 8px; border: 0; border-radius: 8px;
  background: transparent; color: var(--text); font: inherit; cursor: pointer; text-align: left; }
.um-item:hover { background: var(--surface-2); }
.um-item.danger { color: var(--bad); }
.user-menu :focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
@keyframes pop-in { from { opacity: 0; transform: translateY(6px) scale(.98); } }

.nav-tip { position: fixed; z-index: 70; transform: translateY(-50%); padding: 5px 10px; border-radius: 7px; pointer-events: none;
  background: var(--text); color: var(--surface); font-size: 12px; font-weight: 500; white-space: nowrap; box-shadow: var(--shadow-lg); }
.nav-tip::before { content: ""; position: absolute; left: -4px; top: 50%; width: 8px; height: 8px; background: inherit;
  transform: translateY(-50%) rotate(45deg); }
.nav-tip[hidden] { display: none; }

/* Transitions only after first paint, so restored state doesn't animate on every page load. */
body.sb-ready .sidebar { transition: width .22s ease, transform .25s ease, visibility .25s; }
body.sb-ready .nav-group-body { transition: grid-template-rows .22s ease; }
body.sb-ready .nav-group-inner { transition: visibility 0s .22s; }
body.sb-ready .nav-group.open .nav-group-inner { transition: none; }
body.sb-ready .chevron, body.sb-ready .sb-collapse { transition: transform .2s ease, opacity .15s; }
body.sb-ready .nav-item, body.sb-ready .nav-group-toggle { transition: background-color .15s, color .15s; }

/* Rail: icon-only sidebar (user choice on desktop, automatic on tablets). */
body.sb-rail .sidebar { width: var(--sb-rail-w); }
body.sb-rail .sb-head { padding: 0; justify-content: center; }
body.sb-rail .sb-brand { flex: none; }
body.sb-rail .sb-brand-text, body.sb-rail .nav-label, body.sb-rail .user-meta, body.sb-rail .user-btn .chevron,
body.sb-rail .nav-group-toggle { display: none; }
body.sb-rail .sb-collapse .icon { transform: rotate(180deg); }
body.sb-forced .sb-collapse { display: none; }
body.sb-rail .nav-section { height: 1px; margin: 10px 8px; overflow: hidden; color: transparent; background: var(--sb-border); }
body.sb-rail .nav-section:first-child { display: none; }
body.sb-rail .nav-item { justify-content: center; padding: 0; height: 44px; }
body.sb-rail .nav-group-body { grid-template-rows: 1fr; }
body.sb-rail .nav-group-inner { visibility: visible; margin: 0; padding: 0; border: 0; }
body.sb-rail .nav-group-inner .nav-item .icon { width: 20px; height: 20px; }
body.sb-rail .nav-group-inner .nav-item.active::before { left: -12px; top: 10px; bottom: 10px; border-radius: 0 3px 3px 0; }
body.sb-rail .nav-badge { position: absolute; top: 4px; right: 4px; min-width: 17px; height: 17px; padding: 0 4px; font-size: 10px;
  box-shadow: 0 0 0 2px var(--sb-ring); }
body.sb-rail .sb-foot { padding: 12px 0; display: flex; justify-content: center; }
body.sb-rail .user-btn { width: auto; padding: 5px; border-radius: 50%; background: transparent; }
body.sb-rail .user-menu { left: calc(100% - 4px); right: auto; bottom: 10px; width: 250px; }

@media (max-width: 760px) {
  :root { --bar-h: 56px; }
  .page { padding: 18px 16px 35px; }
  .flashes { padding: 0 16px; }
  .page-title h1 { font-size: 22px; }
  .page-actions { width: 100%; }
  .info-grid { grid-template-columns: 1fr; gap: 15px; }
  .entity-head { align-items: flex-start; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
  .metric-v { font-size: 24px; }
  .kv.defs { grid-template-columns: 1fr; gap: 2px; }
  .kv.defs dd { margin-bottom: 10px; }
  .mobile-bar { display: flex; align-items: center; gap: 8px; height: var(--bar-h); padding: 0 10px; flex: none;
    position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--border); }
  .mobile-bar .sb-brand { flex: none; gap: 9px; color: var(--text); }
  .mobile-bar .brand-mark { width: 30px; height: 30px; border-radius: 9px; box-shadow: none; }
  .mobile-bar .brand-mark .icon { width: 17px; height: 17px; }
  .mobile-bar .sb-icon-btn:hover { background: var(--surface-2); }
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; height: auto; width: min(290px, 86vw); z-index: 60;
    transform: translateX(-100%); visibility: hidden; box-shadow: var(--shadow-lg); }
  body.inbox-page .sidebar { height: auto; }
  body.sb-open .sidebar { transform: none; visibility: visible; }
  .sb-collapse { display: none; }
  .sb-close { display: inline-grid; }
  .sb-scrim { display: block; position: fixed; inset: 0; z-index: 55; background: rgba(5,15,13,.5); opacity: 0;
    pointer-events: none; transition: opacity .25s; }
  body.sb-open .sb-scrim { opacity: 1; pointer-events: auto; }
  body.sb-open { overflow: hidden; }
}
@media (prefers-reduced-motion: reduce) {
  body.sb-ready .sidebar, body.sb-ready .nav-group-body, body.sb-ready .chevron, .sb-scrim, .user-menu { transition: none; animation: none; }
}

/* AI replies (Chatbot page) */
.ai-box { margin-top: 18px; padding: 16px; border: 1px solid var(--border); border-radius: var(--radius);
  background: linear-gradient(180deg, var(--accent-soft), transparent 140px); }
.ai-box.on { border-color: var(--accent); }
.ai-box h3 { margin: 0; }
.notice { background: var(--warn-soft); color: var(--warn); border-radius: 8px; padding: 8px 10px; }
.ai-test { display: flex; gap: 8px; align-items: end; margin-top: 14px; padding-top: 14px; border-top: 1px dashed var(--border); }
.ai-test label { flex: 1; }
.ai-result { margin-top: 10px; padding: 10px 12px; border-radius: 10px; background: var(--bubble-bot);
  display: grid; gap: 6px; justify-items: start; max-width: 620px; }
.ai-result.bad { background: var(--bad-soft); color: var(--bad); }
@media (max-width: 760px) { .ai-test { flex-direction: column; align-items: stretch; } }

/* AI insights, sentiment, knowledge base, leads */
.badge.sent-positive, .chip.sent-positive { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.sent-neutral, .chip.sent-neutral { background: var(--surface-2); color: var(--muted); }
.badge.sent-negative, .chip.sent-negative { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.sent-frustrated, .chip.sent-frustrated { background: var(--bad-soft); color: var(--bad); border-color: transparent; font-weight: 600; }
.insights { margin-top: 14px; padding: 12px; border-radius: 10px; border: 1px solid var(--border);
  background: linear-gradient(180deg, var(--accent-soft), var(--surface) 90px); }
.insights-head { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.insights-head strong { margin-right: auto; }
.insights-summary { margin: 8px 0 2px; white-space: pre-wrap; }
.details-pane .insights .btn { margin-top: 8px; }
.lead-kv { margin: 10px 0 0; padding-top: 8px; border-top: 1px dashed var(--border); }
.ai-meta { display: flex; flex-wrap: wrap; gap: 6px; }
.hits { margin: 8px 0 0; padding-left: 20px; display: grid; gap: 8px; }
.hits li > div { color: var(--muted); white-space: pre-wrap; }
.sentiment-bars { display: grid; gap: 8px; max-width: 560px; }
.sbar { display: grid; grid-template-columns: 90px 1fr 40px; gap: 10px; align-items: center; }
.sbar progress { width: 100%; height: 10px; appearance: none; -webkit-appearance: none; border: 0; border-radius: 999px;
  overflow: hidden; background: var(--surface-2); color: var(--bar); --bar: var(--muted); }
.sbar progress::-webkit-progress-bar { background: var(--surface-2); border-radius: 999px; }
.sbar progress::-webkit-progress-value { background: var(--bar); border-radius: 999px; }
.sbar progress::-moz-progress-bar { background: var(--bar); border-radius: 999px; }
.sbar progress.sent-positive { --bar: var(--ok); }
.sbar progress.sent-negative { --bar: var(--warn); }
.sbar progress.sent-frustrated { --bar: var(--bad); }
.sbar-n { text-align: right; font-variant-numeric: tabular-nums; }
.kb-add { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px; margin-top: 12px; }
.kb-form { border: 1px solid var(--border); border-radius: 10px; padding: 14px; display: grid; gap: 10px; align-content: start; }
.kb-form h3 { margin: 0; }
.kb-form .btn { justify-self: start; }
.kb-preview { margin-top: 4px; }
.kb-preview > summary { color: var(--muted); }
.table td .btn + form, .table td form + form { margin-top: 4px; }
.lead-list { margin-top: 12px; }
details.lead > summary { gap: 10px; }
.lead-time { margin-left: auto; }
.badge.lead-new { background: var(--accent-soft); color: var(--accent-strong); border-color: transparent; }
.badge.lead-contacted, .badge.lead-qualified { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.lead-won { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.lead-lost { color: var(--muted); }
