:root {
  --bg: #f6f3ef;
  --surface: #ffffff;
  --surface-2: #faf8f5;
  --line: #e7e1d9;
  --text: #2a241f;
  --muted: #7b7168;
  --accent: #8a5a34;
  --accent-soft: #f3e8dd;
  --accent-ink: #ffffff;
  --ok: #2f7d4f;
  --ok-soft: #e3f2e8;
  --warn: #a8661b;
  --warn-soft: #fbefdc;
  --bad: #b23b3b;
  --bad-soft: #f9e3e3;
  --tg: #2a9fd8;
  --vk: #2b6fd6;
  --max: #7a4be0;
  --test: #8a7f74;
  --bubble-client: #f1ede8;
  --bubble-bot: #f3e8dd;
  --bubble-op: #e3eefb;
  --shadow: 0 1px 2px rgba(42, 36, 31, .05), 0 4px 16px rgba(42, 36, 31, .05);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171412; --surface: #211d1a; --surface-2: #1c1916; --line: #342e29; --text: #eee7df; --muted: #a39789;
    --accent: #d39a68; --accent-soft: #3a2b1f; --accent-ink: #1b140e;
    --ok: #6cc28f; --ok-soft: #1e3427; --warn: #e3a95a; --warn-soft: #3a2d19; --bad: #ef8080; --bad-soft: #3d2222;
    --bubble-client: #2b2622; --bubble-bot: #3a2b1f; --bubble-op: #1f2d3d; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #171412; --surface: #211d1a; --surface-2: #1c1916; --line: #342e29; --text: #eee7df; --muted: #a39789;
  --accent: #d39a68; --accent-soft: #3a2b1f; --accent-ink: #1b140e;
  --ok: #6cc28f; --ok-soft: #1e3427; --warn: #e3a95a; --warn-soft: #3a2d19; --bad: #ef8080; --bad-soft: #3d2222;
  --bubble-client: #2b2622; --bubble-bot: #3a2b1f; --bubble-op: #1f2d3d; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.5 Manrope, system-ui, -apple-system, "Segoe UI", sans-serif; -webkit-font-smoothing: antialiased; }
a { color: var(--accent); }
button, input, textarea, select { font: inherit; color: inherit; }
h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.spacer { flex: 1; }
.stack > * + * { margin-top: 12px; }
.hidden { display: none !important; }

/* ---------- Layout ---------- */
.layout { display: grid; grid-template-columns: 232px 1fr; height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 18px 12px; gap: 4px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 700; font-size: 16px; }
.brand-logo { width: 32px; height: 32px; border-radius: 9px; background: var(--accent); display: grid; place-items: center; color: var(--accent-ink); }
.brand small { display: block; font-weight: 500; color: var(--muted); font-size: 11px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 9px; text-decoration: none; color: var(--text); font-weight: 500; }
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav svg { width: 18px; height: 18px; flex: none; }
.nav .count { margin-left: auto; background: var(--accent); color: var(--accent-ink); border-radius: 99px; font-size: 11px; padding: 0 7px; font-weight: 700; }
.sidebar-foot { margin-top: auto; padding: 10px; display: grid; gap: 8px; }
.main { overflow: auto; min-width: 0; }
.page { padding: 28px 32px 48px; max-width: 1240px; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head p { margin: 2px 0 0; color: var(--muted); }

/* ---------- Components ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.stat .num { font-size: 28px; font-weight: 700; letter-spacing: -.02em; }
.stat .lbl { color: var(--muted); font-size: 13px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line); background: var(--surface); padding: 8px 14px; border-radius: 9px; cursor: pointer; font-weight: 600; white-space: nowrap; text-decoration: none; color: var(--text); }
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 5px 10px; font-size: 12px; border-radius: 7px; }
.btn svg { width: 16px; height: 16px; }

.input, textarea.input, select.input { width: 100%; border: 1px solid var(--line); background: var(--surface); border-radius: 9px; padding: 9px 12px; outline: none; }
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.input { resize: vertical; min-height: 90px; }
label.field { display: block; }
label.field > span { display: block; font-weight: 600; font-size: 13px; margin-bottom: 5px; }
label.field > small { display: block; color: var(--muted); margin-top: 4px; font-size: 12px; }
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.switch input { appearance: none; width: 36px; height: 20px; border-radius: 99px; background: var(--line); position: relative; cursor: pointer; transition: .15s; margin: 0; flex: none; }
.switch input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch input:checked { background: var(--ok); }
.switch input:checked::after { left: 18px; }

.badge { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 99px; font-size: 11px; font-weight: 700; background: var(--surface-2); border: 1px solid var(--line); 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.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.ch { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 6px; color: #fff; font-size: 10px; font-weight: 800; flex: none; }
.ch.telegram { background: var(--tg); } .ch.vk { background: var(--vk); } .ch.max { background: var(--max); } .ch.test { background: var(--test); }
.ch.lg { width: 36px; height: 36px; border-radius: 10px; font-size: 13px; }

.tabs { display: inline-flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.tabs button { border: 0; background: transparent; padding: 6px 12px; border-radius: 7px; cursor: pointer; font-weight: 600; color: var(--muted); }
.tabs button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.notice { padding: 12px 14px; border-radius: 10px; background: var(--warn-soft); color: var(--text); border: 1px solid transparent; }
.notice.info { background: var(--accent-soft); }
.notice b { color: var(--warn); }
.empty { text-align: center; padding: 40px 16px; color: var(--muted); }

.toast-wrap { position: fixed; right: 20px; bottom: 20px; display: grid; gap: 8px; z-index: 100; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.2); max-width: 380px; animation: pop .2s; }
.toast.bad { background: var(--bad); color: #fff; }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }

.modal-bg { position: fixed; inset: 0; background: rgba(20, 16, 12, .45); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { background: var(--surface); border-radius: 14px; width: min(640px, 100%); max-height: 90vh; overflow: auto; padding: 22px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal.wide { width: min(900px, 100%); }

/* ---------- Dialogs ---------- */
.inbox { display: grid; grid-template-columns: 340px 1fr 280px; height: 100vh; }
.inbox-list { border-right: 1px solid var(--line); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.inbox-list .head { padding: 18px 16px 12px; border-bottom: 1px solid var(--line); display: grid; gap: 10px; }
.conv-items { overflow: auto; flex: 1; }
.conv { display: flex; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); cursor: pointer; text-decoration: none; color: inherit; }
.conv:hover { background: var(--surface-2); }
.conv.active { background: var(--accent-soft); }
.conv .who { font-weight: 700; display: flex; gap: 6px; align-items: center; }
.conv .last { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .time { font-size: 11px; color: var(--muted); margin-left: auto; white-space: nowrap; }
.unread-dot { background: var(--accent); color: var(--accent-ink); border-radius: 99px; font-size: 10px; font-weight: 800; padding: 0 6px; }

.thread { display: flex; flex-direction: column; min-height: 0; min-width: 0; background: var(--bg); }
.thread-head { padding: 14px 20px; border-bottom: 1px solid var(--line); background: var(--surface); display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.msgs { flex: 1; overflow: auto; padding: 20px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: min(560px, 80%); display: grid; gap: 3px; }
.msg .bubble { padding: 10px 13px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; background: var(--bubble-client); }
.msg.client { align-self: flex-start; }
.msg.client .bubble { border-bottom-left-radius: 4px; }
.msg.bot, .msg.operator { align-self: flex-end; }
.msg.bot .bubble { background: var(--bubble-bot); border-bottom-right-radius: 4px; }
.msg.operator .bubble { background: var(--bubble-op); border-bottom-right-radius: 4px; }
.msg .info { font-size: 11px; color: var(--muted); display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.msg.bot .info, .msg.operator .info { justify-content: flex-end; }
.day-sep { align-self: center; font-size: 11px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); border-radius: 99px; padding: 1px 10px; }
.composer { border-top: 1px solid var(--line); background: var(--surface); padding: 12px 16px; display: grid; gap: 8px; }
.composer textarea { min-height: 56px; max-height: 200px; }
.sidepanel { border-left: 1px solid var(--line); background: var(--surface); padding: 18px; overflow: auto; display: grid; align-content: start; gap: 16px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; font-size: 13px; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; word-break: break-word; }

.sources { border-top: 1px dashed var(--line); margin-top: 4px; padding-top: 6px; font-size: 12px; }
.sources summary { cursor: pointer; color: var(--muted); }
.sources .src { padding: 6px 8px; margin-top: 6px; border-radius: 8px; background: var(--surface); border: 1px solid var(--line); }

/* ---------- KB ---------- */
.drop { display: block; border: 2px dashed var(--line); border-radius: var(--radius); padding: 26px; text-align: center; cursor: pointer; transition: .15s; background: var(--surface); }
.drop.over, .drop:hover { border-color: var(--accent); background: var(--accent-soft); }
.drop svg { width: 30px; height: 30px; color: var(--accent); }
table.tbl { width: 100%; border-collapse: collapse; }
.tbl th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.tbl td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl tr.off td { opacity: .5; }
.doc-icon { width: 34px; height: 34px; border-radius: 8px; display: grid; place-items: center; font-size: 10px; font-weight: 800; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); flex: none; }
.search-hit { padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); }
.search-hit mark { background: var(--warn-soft); color: inherit; }

/* ---------- Test chat ---------- */
.lab { display: grid; grid-template-columns: 1fr 360px; gap: 16px; height: calc(100vh - 130px); min-height: 480px; }
.phone { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; overflow: hidden; box-shadow: var(--shadow); min-height: 0; }
.phone .msgs { background: var(--bg); }
.typing { display: inline-flex; gap: 4px; padding: 12px 14px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 50% { opacity: .2; } }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 99px; padding: 4px 10px; font-size: 12px; cursor: pointer; }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.rate { display: inline-flex; gap: 2px; }
.rate button { border: 0; background: transparent; cursor: pointer; padding: 2px 4px; border-radius: 5px; color: var(--muted); font-size: 13px; }
.rate button:hover, .rate button.on { background: var(--surface-2); color: var(--text); }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: min(380px, 100%); padding: 28px; }

/* ---------- Responsive ---------- */
@media (max-width: 1180px) {
  .inbox { grid-template-columns: 300px 1fr; }
  .sidepanel { display: none; }
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: auto 1fr; height: auto; min-height: 100vh; }
  .sidebar { flex-direction: row; overflow-x: auto; padding: 8px; gap: 2px; border-right: 0; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 10; }
  .brand, .sidebar-foot { display: none; }
  .nav { display: flex; gap: 2px; }
  .nav a { padding: 8px 10px; white-space: nowrap; font-size: 13px; }
  .page { padding: 18px 16px 40px; }
  .grid-3, .grid-2 { grid-template-columns: 1fr; }
  .inbox { grid-template-columns: 1fr; height: calc(100vh - 54px); }
  .inbox.has-thread .inbox-list { display: none; }
  .inbox:not(.has-thread) .thread { display: none; }
  .lab { grid-template-columns: 1fr; height: auto; }
  .lab .phone { height: 70vh; }
  .msg { max-width: 90%; }
  .tbl .hide-sm { display: none; }
}
.composer .chips { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 2px; scrollbar-width: thin; }
.chip { white-space: nowrap; }
