:root{
  --bg:#f3f5f7; --panel:#fff; --border:#dfe3e8; --border2:#eceff3;
  --text:#2d3a4a; --muted:#8793a3; --muted2:#5f6b7a;
  --accent:#2e86de; --accent-d:#2574c4; --accent-soft:#e8f2fc;
  --side:#26354a; --side-d:#1d2a3b; --side-text:#b9c4d3;
  --green:#2fa36b; --red:#e5484d; --amber:#e8a317; --chat-in:#fff; --chat-out:#dcf1c8;
  --radius:8px; --shadow:0 1px 2px rgba(20,30,45,.06),0 6px 20px rgba(20,30,45,.08);
  --side-w:72px;
}
*{box-sizing:border-box}
html,body{height:100%;margin:0}
body{background:var(--bg);color:var(--text);font:14px/1.45 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Inter,Arial,sans-serif;-webkit-font-smoothing:antialiased}
button,input,select,textarea{font:inherit;color:inherit}
button{cursor:pointer}
a{color:var(--accent);text-decoration:none}
a:hover{text-decoration:underline}
h1,h2,h3{margin:0}
.hidden{display:none!important}
.muted{color:var(--muted)}
.right{margin-left:auto}
.row{display:flex;align-items:center;gap:8px}
.grow{flex:1;min-width:0}
.ell{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
:focus-visible{outline:2px solid var(--accent);outline-offset:1px}

/* ---------- login ---------- */
.login-wrap{min-height:100%;display:flex;align-items:center;justify-content:center;padding:24px;background:linear-gradient(160deg,#26354a,#1d2a3b)}
.login-box{background:#fff;border-radius:14px;padding:32px 28px;width:100%;max-width:360px;box-shadow:var(--shadow)}
.login-box h1{font-size:22px;margin-bottom:4px}
.login-box p{margin:0 0 20px;color:var(--muted)}
.logo{display:flex;align-items:center;gap:10px;font-weight:700;font-size:18px;margin-bottom:18px}
.logo i{width:30px;height:30px;border-radius:8px;background:var(--accent);display:grid;place-items:center}

/* ---------- каркас ---------- */
.app{display:flex;height:100%;min-height:100vh}
.side{width:var(--side-w);background:var(--side);display:flex;flex-direction:column;align-items:stretch;padding:10px 0;flex:0 0 var(--side-w);position:sticky;top:0;height:100vh}
.side .brand{display:grid;place-items:center;height:44px;margin-bottom:8px}
.side .brand i{width:32px;height:32px;border-radius:9px;background:var(--accent);display:grid;place-items:center}
.nav-item{display:flex;flex-direction:column;align-items:center;gap:3px;padding:10px 4px;color:var(--side-text);font-size:10.5px;border-left:3px solid transparent;position:relative;text-decoration:none!important;background:none;border-top:0;border-right:0;border-bottom:0}
.nav-item:hover{background:var(--side-d);color:#fff}
.nav-item.active{background:var(--side-d);color:#fff;border-left-color:var(--accent)}
.nav-item .badge{position:absolute;top:4px;right:12px;background:var(--red);color:#fff;border-radius:10px;font-size:10px;line-height:1;padding:3px 5px;font-weight:700}
.side .spacer{flex:1}
.main{flex:1;min-width:0;display:flex;flex-direction:column;height:100vh}
.topbar{background:var(--panel);border-bottom:1px solid var(--border);padding:8px 16px;display:flex;align-items:center;gap:12px;flex:0 0 auto}
.topbar h2{font-size:16px;font-weight:600}
.gsearch{position:relative;flex:1;max-width:420px}
.gsearch input{width:100%;padding:8px 10px 8px 34px;border:1px solid var(--border);border-radius:var(--radius);background:var(--bg)}
.gsearch .ic{position:absolute;left:9px;top:50%;transform:translateY(-50%);color:var(--muted);display:flex}
.gresults{position:absolute;top:100%;left:0;right:0;background:#fff;border:1px solid var(--border);border-radius:var(--radius);box-shadow:var(--shadow);margin-top:4px;max-height:60vh;overflow:auto;z-index:30}
.gresults h4{margin:0;padding:8px 12px 4px;font-size:11px;color:var(--muted);text-transform:uppercase;letter-spacing:.04em}
.gresults button{display:block;width:100%;text-align:left;padding:7px 12px;background:none;border:0}
.gresults button:hover{background:var(--accent-soft)}
.userbox{display:flex;align-items:center;gap:8px;position:relative}
.userbox .uname{font-weight:600;max-width:140px}
.userbox .urole{font-size:11px;color:var(--muted);line-height:1.1}
.content{flex:1;min-height:0;overflow:auto;position:relative}
.page{padding:16px;max-width:1500px}
.page-fill{height:100%;display:flex;flex-direction:column;padding:0}

/* ---------- элементы ---------- */
.btn{display:inline-flex;align-items:center;gap:6px;background:var(--accent);color:#fff;border:1px solid var(--accent);border-radius:var(--radius);padding:7px 14px;font-weight:600;white-space:nowrap}
.btn:hover{background:var(--accent-d);text-decoration:none}
.btn:disabled{opacity:.55;cursor:default}
.btn.sec{background:#fff;color:var(--text);border-color:var(--border)}
.btn.sec:hover{background:var(--bg)}
.btn.danger{background:#fff;color:var(--red);border-color:#f3c3c5}
.btn.danger:hover{background:#fdeeee}
.btn.sm{padding:4px 10px;font-size:12.5px}
.btn.icon{padding:7px 9px}
.link-btn{background:none;border:0;color:var(--accent);padding:0}
.link-btn:hover{text-decoration:underline}
input[type=text],input[type=password],input[type=number],input[type=email],input[type=tel],input[type=date],input[type=datetime-local],select,textarea{width:100%;padding:7px 10px;border:1px solid var(--border);border-radius:var(--radius);background:#fff;outline:none}
input:focus,select:focus,textarea:focus{border-color:var(--accent);box-shadow:0 0 0 3px var(--accent-soft)}
textarea{resize:vertical;min-height:70px}
label.f{display:block;font-size:12px;color:var(--muted2);font-weight:600;margin:0 0 4px}
.field{margin-bottom:12px}
.grid2{display:grid;grid-template-columns:1fr 1fr;gap:12px}
.card{background:var(--panel);border:1px solid var(--border);border-radius:var(--radius)}
.pad{padding:16px}
.chip{display:inline-block;background:var(--accent-soft);color:var(--accent-d);border-radius:12px;padding:1px 9px;font-size:12px}
.pill{display:inline-block;border-radius:12px;padding:1px 9px;font-size:12px;color:#34404f}
.avatar{display:inline-grid;place-items:center;width:26px;height:26px;border-radius:50%;color:#fff;font-size:11px;font-weight:700;flex:0 0 auto}
.avatar.lg{width:34px;height:34px;font-size:13px}
.err{color:var(--red);font-size:13px;margin:8px 0 0}
.empty{padding:40px 16px;text-align:center;color:var(--muted)}
table.t{width:100%;border-collapse:collapse;background:#fff}
table.t th{text-align:left;font-size:12px;color:var(--muted);font-weight:600;padding:9px 12px;border-bottom:1px solid var(--border);background:#fafbfc;position:sticky;top:0;z-index:1;white-space:nowrap}
table.t td{padding:9px 12px;border-bottom:1px solid var(--border2);vertical-align:middle}
table.t tr.click{cursor:pointer}
table.t tr.click:hover td{background:#f7fafd}
.table-wrap{background:#fff;border:1px solid var(--border);border-radius:var(--radius);overflow:auto}
.toolbar{display:flex;align-items:center;gap:8px;flex-wrap:wrap;padding:10px 16px;background:var(--panel);border-bottom:1px solid var(--border);flex:0 0 auto}
.toolbar .sel{width:auto;min-width:150px}
.toolbar input[type=text]{width:220px}
.seg{display:inline-flex;border:1px solid var(--border);border-radius:var(--radius);overflow:hidden}
.seg button{background:#fff;border:0;padding:6px 12px;color:var(--muted2)}
.seg button.on{background:var(--accent);color:#fff}
.tabs{display:flex;gap:2px;border-bottom:1px solid var(--border);margin-bottom:14px;overflow:auto}
.tabs button{background:none;border:0;border-bottom:2px solid transparent;padding:9px 14px;color:var(--muted2);font-weight:600;white-space:nowrap}
.tabs button.on{color:var(--accent);border-bottom-color:var(--accent)}

/* ---------- канбан ---------- */
.board{flex:1;min-height:0;overflow:auto;display:flex;gap:10px;padding:12px 16px;align-items:flex-start}
.col{flex:0 0 268px;width:268px;background:#e9edf1;border-radius:var(--radius);display:flex;flex-direction:column;max-height:100%}
.col.over{outline:2px dashed var(--accent);outline-offset:-2px;background:#dfeaf5}
.col-head{padding:10px 12px 8px;border-top:4px solid var(--c,#ccc);border-radius:var(--radius) var(--radius) 0 0;background:#f6f8fa}
.col-head .nm{font-weight:700;font-size:12.5px;text-transform:uppercase;letter-spacing:.02em}
.col-head .sub{font-size:12px;color:var(--muted)}
.col-body{padding:8px;overflow:auto;display:flex;flex-direction:column;gap:8px;min-height:60px}
.kcard{background:#fff;border:1px solid var(--border);border-radius:7px;padding:9px 10px;cursor:grab;box-shadow:0 1px 1px rgba(20,30,45,.04)}
.kcard:hover{border-color:#bcd3ec}
.kcard.drag{opacity:.4}
.kcard .nm{font-weight:600;color:#1f5f9e;word-break:break-word}
.kcard .pr{font-weight:700;margin:3px 0}
.kcard .meta{font-size:12px;color:var(--muted2)}
.kcard .foot{display:flex;align-items:center;gap:6px;margin-top:7px}
.kcard .tags{display:flex;gap:4px;flex-wrap:wrap;margin-top:5px}
.dot{display:inline-flex;align-items:center;gap:4px;font-size:11.5px;color:var(--muted2)}
.dot::before{content:"";width:8px;height:8px;border-radius:50%;background:var(--muted)}
.dot.red{color:var(--red)} .dot.red::before{background:var(--red)}
.dot.amber::before{background:var(--amber)}
.dot.green::before{background:var(--green)}
.unread{background:var(--green);color:#fff;border-radius:10px;font-size:11px;font-weight:700;padding:1px 7px;display:inline-flex;align-items:center;gap:3px}
.quick{margin:0 8px 8px;padding:7px;border:1px dashed #b9c4d3;border-radius:7px;background:none;color:var(--muted2);text-align:center}
.quick:hover{border-color:var(--accent);color:var(--accent)}

/* ---------- модальные окна ---------- */
.overlay{position:fixed;inset:0;background:rgba(18,28,42,.5);z-index:100;display:flex;align-items:flex-start;justify-content:center;padding:4vh 12px;overflow:auto}
.modal{background:#fff;border-radius:12px;width:100%;max-width:480px;box-shadow:var(--shadow);margin:auto 0}
.modal.wide{max-width:1020px}
.modal.mid{max-width:640px}
.modal-h{display:flex;align-items:center;gap:10px;padding:14px 18px;border-bottom:1px solid var(--border2)}
.modal-h h3{font-size:16px;flex:1}
.modal-b{padding:16px 18px}
.modal-f{padding:12px 18px;border-top:1px solid var(--border2);display:flex;gap:8px;justify-content:flex-end;flex-wrap:wrap}
.x{background:none;border:0;color:var(--muted);padding:4px;border-radius:6px;display:flex}
.x:hover{background:var(--bg);color:var(--text)}

/* ---------- карточка сделки ---------- */
.lead-head{padding:14px 18px 10px;border-bottom:1px solid var(--border2)}
.lead-title{font-size:18px;font-weight:700;border:1px solid transparent;border-radius:6px;padding:3px 6px;width:100%;background:none}
.lead-title:hover{border-color:var(--border)}
.stagebar{display:flex;gap:3px;margin-top:10px;overflow:auto}
.stagebar button{flex:1 0 auto;min-width:90px;border:0;padding:6px 14px 6px 18px;font-size:12px;font-weight:600;color:#34404f;background:var(--c);opacity:.5;clip-path:polygon(0 0,calc(100% - 10px) 0,100% 50%,calc(100% - 10px) 100%,0 100%,10px 50%);white-space:nowrap}
.stagebar button:first-child{clip-path:polygon(0 0,calc(100% - 10px) 0,100% 50%,calc(100% - 10px) 100%,0 100%)}
.stagebar button.on{opacity:1}
.stagebar button:hover{opacity:.85}
.lead-body{display:grid;grid-template-columns:320px 1fr;min-height:420px}
.lead-side{padding:14px 18px;border-right:1px solid var(--border2);background:#fafbfc;border-radius:0 0 0 12px}
.lead-main{padding:0;display:flex;flex-direction:column;min-width:0}
.sec-title{font-size:11px;text-transform:uppercase;color:var(--muted);letter-spacing:.05em;font-weight:700;margin:14px 0 6px}
.sec-title:first-child{margin-top:0}
.kv{display:flex;flex-direction:column;margin-bottom:10px}
.kv .k{font-size:12px;color:var(--muted)}
.feed{flex:1;overflow:auto;padding:14px 18px;display:flex;flex-direction:column;gap:8px;max-height:48vh;min-height:220px;background:#f6f8fa}
.ev{font-size:12.5px;color:var(--muted2);display:flex;gap:8px;align-items:baseline}
.ev time{color:var(--muted);font-size:11.5px;white-space:nowrap}
.note{background:#fff8dc;border:1px solid #f0e2a1;border-radius:8px;padding:8px 10px;align-self:stretch}
.note .who,.msg .who{font-size:11.5px;color:var(--muted)}
.msg{max-width:78%;border-radius:10px;padding:7px 10px;position:relative;word-break:break-word;white-space:pre-wrap;box-shadow:0 1px 1px rgba(20,30,45,.07)}
.msg.in{background:var(--chat-in);align-self:flex-start;border-top-left-radius:2px}
.msg.out{background:var(--chat-out);align-self:flex-end;border-top-right-radius:2px}
.msg .tm{display:block;font-size:11px;color:var(--muted);text-align:right;margin-top:2px}
.composer{border-top:1px solid var(--border);padding:10px 14px;background:#fff;border-radius:0 0 12px 0}
.composer .ctabs{display:flex;gap:4px;margin-bottom:8px}
.composer .ctabs button{background:none;border:0;padding:3px 10px;border-radius:12px;color:var(--muted2);font-weight:600}
.composer .ctabs button.on{background:var(--accent-soft);color:var(--accent-d)}
.composer textarea{min-height:56px}
.task-row{display:flex;align-items:flex-start;gap:8px;padding:6px 0;border-bottom:1px solid var(--border2)}
.task-row:last-child{border:0}
.task-row.done .tx{text-decoration:line-through;color:var(--muted)}
.task-row .due{font-size:12px;color:var(--muted)} .task-row .due.over{color:var(--red);font-weight:600}
.file-row{display:flex;align-items:center;gap:8px;padding:4px 0}

/* ---------- чаты ---------- */
.chats{display:grid;grid-template-columns:340px 1fr;height:100%;background:#fff}
.chat-list{border-right:1px solid var(--border);display:flex;flex-direction:column;min-height:0}
.chat-list .items{overflow:auto;flex:1}
.chat-item{display:flex;gap:10px;padding:10px 14px;border-bottom:1px solid var(--border2);cursor:pointer;width:100%;background:none;border-left:0;border-right:0;border-top:0;text-align:left}
.chat-item:hover{background:#f7fafd}
.chat-item.on{background:var(--accent-soft)}
.chat-item .top{display:flex;gap:6px;align-items:baseline}
.chat-item .nm{font-weight:600}
.chat-item .tm{font-size:11.5px;color:var(--muted);margin-left:auto;white-space:nowrap}
.chat-item .last{font-size:12.5px;color:var(--muted2)}
.chat-view{display:flex;flex-direction:column;min-height:0;min-width:0;background:#eef1f4}
.chat-head{background:#fff;border-bottom:1px solid var(--border);padding:10px 16px;display:flex;align-items:center;gap:10px}
.chat-msgs{flex:1;overflow:auto;padding:14px 16px;display:flex;flex-direction:column;gap:6px}
.chat-input{background:#fff;border-top:1px solid var(--border);padding:10px 14px;display:flex;gap:8px;align-items:flex-end}
.chat-input textarea{min-height:42px;max-height:140px}
.banner{background:#fff4d6;border-bottom:1px solid #f0dfa2;color:#6b5200;padding:8px 16px;font-size:13px}
.back{display:none}

/* ---------- аналитика ---------- */
.kpis{display:grid;grid-template-columns:repeat(auto-fit,minmax(170px,1fr));gap:12px;margin-bottom:16px}
.kpi{padding:14px 16px}
.kpi .v{font-size:24px;font-weight:700;margin-top:2px}
.kpi .l{font-size:12px;color:var(--muted)}
.bar-row{display:grid;grid-template-columns:170px 1fr 110px;gap:10px;align-items:center;padding:5px 0}
.bar-track{background:#eef1f4;border-radius:5px;height:20px;overflow:hidden}
.bar-fill{height:100%;border-radius:5px;min-width:2px}
.chart{width:100%;height:170px;display:block}
.cols2{display:grid;grid-template-columns:1fr 1fr;gap:16px}

/* ---------- прочее ---------- */
#toasts{position:fixed;right:14px;bottom:14px;z-index:300;display:flex;flex-direction:column;gap:8px;max-width:340px}
.toast{background:#26354a;color:#fff;padding:10px 14px;border-radius:9px;box-shadow:var(--shadow);cursor:pointer;animation:tin .2s ease}
.toast.err{background:#b4373b}
.toast.ok{background:#217a50}
@keyframes tin{from{opacity:0;transform:translateY(8px)}to{opacity:1;transform:none}}
.qr{width:260px;height:260px;border:1px solid var(--border);border-radius:10px;display:block;margin:10px 0}
.status-dot{display:inline-block;width:9px;height:9px;border-radius:50%;margin-right:6px;background:var(--muted)}
.status-dot.green{background:var(--green)} .status-dot.amber{background:var(--amber)}
.stage-edit{display:flex;gap:8px;align-items:center;padding:6px 8px;background:#fff;border:1px solid var(--border);border-radius:7px;margin-bottom:6px}
.stage-edit input[type=color]{width:30px;height:26px;padding:0;border:0;background:none}
.stage-edit input[type=text]{border:0;padding:3px 4px;box-shadow:none}
.filters{background:#fafbfc;border-bottom:1px solid var(--border);padding:10px 16px;display:grid;grid-template-columns:repeat(auto-fill,minmax(170px,1fr));gap:10px;flex:0 0 auto}

/* ---------- мобильная версия ---------- */
@media (max-width:860px){
  .app{flex-direction:column-reverse}
  .side{flex-direction:row;width:100%;height:auto;flex:0 0 auto;position:fixed;bottom:0;left:0;right:0;z-index:50;padding:0;padding-bottom:env(safe-area-inset-bottom);overflow-x:auto}
  .side .brand,.side .spacer{display:none}
  .nav-item{flex:1 0 64px;border-left:0;border-top:3px solid transparent;padding:7px 2px}
  .nav-item.active{border-left-color:transparent;border-top-color:var(--accent)}
  .main{height:100vh;padding-bottom:62px}
  .topbar .uname,.topbar .urole{display:none}
  .lead-body{grid-template-columns:1fr}
  .lead-side{border-right:0;border-bottom:1px solid var(--border2);border-radius:0}
  .composer{border-radius:0 0 12px 12px}
  .chats{grid-template-columns:1fr}
  .chats .chat-view{display:none}
  .chats.open .chat-list{display:none}
  .chats.open .chat-view{display:flex}
  .back{display:inline-flex}
  .cols2,.grid2{grid-template-columns:1fr}
  .bar-row{grid-template-columns:110px 1fr 80px}
  .toolbar input[type=text]{width:100%}
  .overlay{padding:0}
  .modal,.modal.wide,.modal.mid{max-width:none;border-radius:0;min-height:100%}
}
