:root {
  --bg: #f5f6fa; --panel: #fff; --line: #e6e8ef; --line2: #eef0f5;
  --text: #14172b; --muted: #6b7088; --faint: #9aa0b4;
  --accent: #5b5bf6; --accent-2: #4646e0; --accent-soft: #ecebff;
  --in: #fff; --out: #5b5bf6; --out-text: #fff;
  --ok: #16a34a; --warn: #f59e0b; --bad: #e5484d;
  --s-new: #5b5bf6; --s-talk: #0ea5e9; --s-offer: #f59e0b; --s-deal: #8b5cf6; --s-paid: #16a34a; --s-lost: #9aa0b4;
  --r: 14px; --shadow: 0 1px 2px rgba(20,23,43,.04), 0 4px 16px rgba(20,23,43,.06);
  font-family: Inter, "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.45; -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* buttons & inputs */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 38px; padding: 0 16px; border-radius: 10px;
  border: 1px solid var(--line); background: #fff; cursor: pointer; font-weight: 600; white-space: nowrap; transition: .15s; }
.btn:hover { border-color: #cfd3e0; background: #fafbfd; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.sm { height: 32px; padding: 0 12px; font-size: 13px; }
.icon-btn { width: 38px; height: 38px; border-radius: 10px; border: 0; background: transparent; display: inline-grid; place-items: center; cursor: pointer; color: var(--muted); }
.icon-btn:hover { background: var(--line2); color: var(--text); }
input[type=text], input[type=password], input[type=number], input[type=search], input[type=datetime-local], input[type=tel], textarea, select {
  width: 100%; border: 1px solid var(--line); border-radius: 10px; background: #fff; padding: 9px 12px; outline: none; transition: .15s; }
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; }
label.f { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin: 14px 0 6px; text-transform: uppercase; letter-spacing: .03em; }

/* auth */
.auth { min-height: 100%; display: grid; place-items: center; padding: 20px; }
.auth-card { width: 100%; max-width: 380px; background: #fff; border-radius: 20px; padding: 32px; box-shadow: var(--shadow); display: grid; gap: 12px; }
.auth-card h1 { margin: 10px 0 0; font-size: 22px; }
.auth-err { color: var(--bad); min-height: 20px; font-weight: 600; }

/* top bar */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 16px; letter-spacing: -.01em; }
.brand.big { font-size: 18px; }
.logo { width: 30px; height: 30px; border-radius: 9px; background: var(--accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M9 11h14M9 16h10M9 21h6' stroke='white' stroke-width='3' stroke-linecap='round'/%3E%3C/svg%3E") center/100% no-repeat; flex: none; }
.top { height: 60px; display: flex; align-items: center; gap: 20px; padding: 0 16px; background: #fff; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.tabs { display: flex; gap: 4px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.tabs a { padding: 8px 14px; border-radius: 10px; color: var(--muted); font-weight: 600; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; }
.tabs a:hover { background: var(--line2); color: var(--text); }
.tabs a.on { background: var(--accent-soft); color: var(--accent); }
.badge { background: var(--bad); color: #fff; font-size: 11px; min-width: 18px; height: 18px; border-radius: 9px; padding: 0 5px; display: inline-grid; place-items: center; }
.banner { background: #fff7e6; color: #8a5a00; border-bottom: 1px solid #fde3a7; padding: 10px 16px; font-weight: 600; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.banner.bad { background: #fff0f0; color: #a3262a; border-color: #f7c9cb; }

/* toast */
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #14172b; color: #fff; padding: 12px 18px; border-radius: 12px; font-weight: 600; z-index: 100; box-shadow: var(--shadow); max-width: calc(100vw - 32px); }
.toast.bad { background: var(--bad); }

#app { height: calc(100vh - 60px); height: calc(100dvh - 60px); }
body:has(.banner:not([hidden])) #app { height: calc(100dvh - 60px - 42px); }

/* ---------- chats layout ---------- */
.chats { display: grid; grid-template-columns: 340px 1fr 320px; height: 100%; }
.list { border-right: 1px solid var(--line); background: #fff; display: flex; flex-direction: column; min-height: 0; }
.list-head { padding: 12px; border-bottom: 1px solid var(--line2); display: grid; gap: 10px; }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 20px; padding: 4px 10px; font-size: 12px; font-weight: 600; color: var(--muted); cursor: pointer; white-space: nowrap; }
.chip.on { background: var(--text); border-color: var(--text); color: #fff; }
.chip i { font-style: normal; opacity: .7; margin-left: 4px; }
.list-items { overflow-y: auto; flex: 1; }
.ci { display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; padding: 12px; border-bottom: 1px solid var(--line2); cursor: pointer; position: relative; }
.ci:hover { background: #fafbfd; }
.ci.on { background: var(--accent-soft); }
.ci .nm { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci .pv { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ci .pv.unr { color: var(--text); font-weight: 600; }
.ci .meta { text-align: right; display: grid; gap: 4px; justify-items: end; align-content: start; }
.ci .tm { font-size: 12px; color: var(--faint); }
.ci .sub { display: flex; gap: 6px; align-items: center; margin-top: 3px; min-width: 0; }
.av { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; font-weight: 800; color: #fff; font-size: 15px; background-size: cover; background-position: center; flex: none; }
.av.sm { width: 34px; height: 34px; font-size: 13px; }
.pill { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 10px; white-space: nowrap; color: #fff; }
.demo-tag { font-size: 10px; font-weight: 800; color: var(--warn); border: 1px solid #fde3a7; border-radius: 6px; padding: 0 5px; }
.empty { padding: 40px 24px; text-align: center; color: var(--muted); }
.empty b { display: block; color: var(--text); font-size: 16px; margin-bottom: 6px; }

.thread { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }
.th-head { background: #fff; border-bottom: 1px solid var(--line); padding: 10px 16px; display: flex; gap: 12px; align-items: center; min-height: 64px; }
.th-head .t { min-width: 0; flex: 1; }
.th-head .t b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px; }
.th-head .t span { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.back, .lead-toggle { display: none; }
.msgs { flex: 1; overflow-y: auto; padding: 18px 16px; display: flex; flex-direction: column; gap: 6px; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: #e9ebf2; border-radius: 10px; padding: 2px 10px; margin: 10px 0 4px; }
.m { max-width: min(560px, 82%); padding: 9px 12px 6px; border-radius: 16px; background: var(--in); box-shadow: 0 1px 1px rgba(20,23,43,.06); white-space: pre-wrap; word-wrap: break-word; position: relative; }
.m.in { align-self: flex-start; border-bottom-left-radius: 6px; }
.m.out { align-self: flex-end; background: var(--out); color: var(--out-text); border-bottom-right-radius: 6px; }
.m.sys { align-self: center; background: #fff7e6; color: #8a5a00; font-size: 13px; max-width: 90%; }
.m.deleted { opacity: .6; font-style: italic; }
.m .mt { font-size: 11px; opacity: .6; text-align: right; margin-top: 2px; }
.m img { display: block; max-width: 100%; max-height: 320px; border-radius: 10px; margin-bottom: 4px; cursor: zoom-in; }
.m audio { width: 260px; max-width: 100%; }
.m a { color: inherit; text-decoration: underline; }
.composer { background: #fff; border-top: 1px solid var(--line); padding: 10px 12px; display: grid; grid-template-columns: auto auto 1fr auto; gap: 8px; align-items: end; position: relative; }
.composer textarea { min-height: 42px; max-height: 180px; resize: none; padding: 10px 12px; }
.composer .cnt { position: absolute; right: 70px; bottom: 2px; font-size: 11px; color: var(--faint); }
.composer .cnt.over { color: var(--bad); font-weight: 700; }
.tpl-pop { position: absolute; bottom: calc(100% + 6px); left: 12px; width: min(420px, calc(100vw - 24px)); max-height: 360px; overflow-y: auto; background: #fff; border: 1px solid var(--line); border-radius: 14px; box-shadow: 0 12px 40px rgba(20,23,43,.16); padding: 6px; z-index: 5; }
.tpl-pop button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 10px 12px; border-radius: 10px; cursor: pointer; }
.tpl-pop button:hover { background: var(--line2); }
.tpl-pop b { display: block; }
.tpl-pop span { color: var(--muted); font-size: 12px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.item-card { display: flex; gap: 10px; align-items: center; background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 8px; margin: 0 16px 0; }
.item-card img { width: 56px; height: 42px; object-fit: cover; border-radius: 8px; background: var(--line2); }

.lead { border-left: 1px solid var(--line); background: #fff; overflow-y: auto; padding: 16px; }
.lead h3 { margin: 0 0 4px; font-size: 16px; }
.stages { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 6px; }
.stages button { border: 1px solid var(--line); background: #fff; border-radius: 10px; padding: 8px; font-weight: 600; font-size: 13px; cursor: pointer; display: flex; align-items: center; gap: 6px; }
.stages button i { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.stages button.on { border-color: currentColor; box-shadow: inset 0 0 0 1px currentColor; }
.saved { font-size: 12px; color: var(--ok); font-weight: 700; min-height: 18px; margin-top: 10px; }
.quick-remind { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.links { display: grid; gap: 6px; margin-top: 16px; font-size: 13px; }

.placeholder { display: grid; place-items: center; height: 100%; color: var(--muted); text-align: center; padding: 24px; }

/* ---------- funnel ---------- */
.funnel { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(250px, 1fr); gap: 12px; padding: 16px; height: 100%; overflow-x: auto; }
.col { background: #eceef4; border-radius: var(--r); display: flex; flex-direction: column; min-height: 0; }
.col-h { padding: 12px 14px; font-weight: 800; display: flex; align-items: center; gap: 8px; }
.col-h i { width: 10px; height: 10px; border-radius: 50%; }
.col-h small { margin-left: auto; color: var(--muted); font-weight: 600; }
.col-b { padding: 0 8px 8px; overflow-y: auto; flex: 1; display: grid; align-content: start; gap: 8px; min-height: 60px; border-radius: var(--r); transition: background .15s; }
.col-b.drop { background: var(--accent-soft); }
.card { background: #fff; border-radius: 12px; padding: 10px 12px; box-shadow: 0 1px 2px rgba(20,23,43,.06); cursor: grab; display: grid; gap: 4px; }
.card:active { cursor: grabbing; }
.card b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .pv { color: var(--muted); font-size: 12px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card .row { display: flex; justify-content: space-between; font-size: 12px; color: var(--faint); gap: 8px; }
.card .sum { color: var(--text); font-weight: 700; }

/* ---------- stats ---------- */
.page { padding: 20px; overflow-y: auto; height: 100%; }
.page-in { max-width: 1100px; margin: 0 auto; display: grid; gap: 16px; }
.page h2 { margin: 0; font-size: 22px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.kpi { background: #fff; border-radius: var(--r); padding: 16px; box-shadow: var(--shadow); }
.kpi small { color: var(--muted); font-weight: 600; font-size: 12px; }
.kpi b { display: block; font-size: 26px; letter-spacing: -.02em; margin-top: 4px; }
.box { background: #fff; border-radius: var(--r); padding: 18px; box-shadow: var(--shadow); }
.box h3 { margin: 0 0 12px; font-size: 15px; }
.bars { display: flex; align-items: flex-end; gap: 3px; height: 160px; }
.bars div { flex: 1; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 2px; position: relative; opacity: .85; }
.bars div:hover { opacity: 1; }
.bars-x { display: flex; justify-content: space-between; color: var(--faint); font-size: 11px; margin-top: 6px; }
.fbar { display: grid; grid-template-columns: 130px 1fr 90px; gap: 10px; align-items: center; margin: 8px 0; font-size: 13px; }
.fbar .tr { height: 12px; background: var(--line2); border-radius: 6px; overflow: hidden; }
.fbar .tr i { display: block; height: 100%; border-radius: 6px; }
.seg { display: inline-flex; background: var(--line2); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; font-weight: 600; cursor: pointer; color: var(--muted); }
.seg button.on { background: #fff; color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

/* templates & settings */
.tpl { display: grid; grid-template-columns: 1fr auto; gap: 8px; padding: 14px 0; border-bottom: 1px solid var(--line2); }
.tpl:last-child { border: 0; }
.tpl p { margin: 4px 0 0; white-space: pre-wrap; color: var(--muted); font-size: 13px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.code { font-family: ui-monospace, Consolas, monospace; font-size: 12px; background: var(--line2); border-radius: 8px; padding: 8px 10px; word-break: break-all; }
.status { display: inline-flex; align-items: center; gap: 6px; font-weight: 700; font-size: 13px; }
.status i { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); }
.status.ok i { background: var(--ok); }
.steps { margin: 0; padding-left: 20px; color: var(--muted); }
.steps li { margin: 4px 0; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }

.lightbox { position: fixed; inset: 0; background: rgba(10,12,25,.85); display: grid; place-items: center; z-index: 200; cursor: zoom-out; padding: 20px; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 10px; }

/* ---------- responsive ---------- */
@media (max-width: 1200px) {
  .chats { grid-template-columns: 300px 1fr; }
  .lead { display: none; position: fixed; right: 0; top: 60px; bottom: 0; width: min(360px, 100vw); z-index: 30; box-shadow: -10px 0 40px rgba(20,23,43,.15); }
  .chats.show-lead .lead { display: block; }
  .lead-toggle { display: inline-grid; }
}
@media (max-width: 800px) {
  .top { gap: 8px; padding: 0 8px; }
  .brand-t { display: none; }
  .tabs a { padding: 8px 10px; }
  .chats { grid-template-columns: 1fr; }
  .chats .thread { display: none; }
  .chats.open .list { display: none; }
  .chats.open .thread { display: flex; }
  .back { display: inline-grid; }
  .grid2 { grid-template-columns: 1fr; }
  .fbar { grid-template-columns: 100px 1fr 70px; }
  .page { padding: 14px; }
}
