:root {
  --bg: #EEF2F5;
  --surface: #FFFFFF;
  --surface-2: #F6F8FA;
  --ink: #18232F;
  --ink-2: #4A5866;
  --ink-3: #7B8896;
  --line: #D5DDE5;
  --line-2: #E6EBF0;
  --action: #2756C9;
  --action-ink: #FFFFFF;
  --wait: #D9930D;
  --wait-bg: #FDF3DD;
  --late: #C23B3B;
  --late-bg: #FBE6E6;
  --ok: #23845E;
  --ok-bg: #E1F3EA;
  --rail: #18232F;
  --rail-ink: #B8C4D0;
  --radius: 8px;
  --font: "Golos Text", "Segoe UI", system-ui, -apple-system, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11181F; --surface: #19222B; --surface-2: #1F2933; --ink: #E6ECF1; --ink-2: #AAB6C2; --ink-3: #7D8B98;
    --line: #2E3A46; --line-2: #26313C; --action: #5C86F0; --wait: #F0B03A; --wait-bg: #3A2E14; --late: #F07171;
    --late-bg: #3D1F22; --ok: #4CC08F; --ok-bg: #173327; --rail: #0C1116; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #11181F; --surface: #19222B; --surface-2: #1F2933; --ink: #E6ECF1; --ink-2: #AAB6C2; --ink-3: #7D8B98;
  --line: #2E3A46; --line-2: #26313C; --action: #5C86F0; --wait: #F0B03A; --wait-bg: #3A2E14; --late: #F07171;
  --late-bg: #3D1F22; --ok: #4CC08F; --ok-bg: #173327; --rail: #0C1116; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 var(--font); font-feature-settings: "tnum" 1; }
a { color: var(--action); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--action); outline-offset: 2px; }
h1 { font-size: 26px; line-height: 1.2; font-weight: 600; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 18px; font-weight: 600; margin: 0 0 12px; }
h3 { font-size: 15px; font-weight: 600; margin: 0 0 8px; }
.muted { color: var(--ink-3); }
.small { font-size: 13px; }
.hidden { display: none !important; }

/* ---------- каркас ---------- */
.shell { display: grid; grid-template-columns: 216px 1fr; min-height: 100vh; }
.rail { background: var(--rail); color: var(--rail-ink); padding: 20px 12px; display: flex; flex-direction: column; gap: 2px; position: sticky; top: 0; height: 100vh; }
.brand { color: #fff; font-weight: 700; font-size: 17px; padding: 0 10px 18px; display: flex; align-items: center; gap: 8px; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--wait); box-shadow: 0 0 0 4px rgba(217,147,13,.25); }
.nav-link { display: flex; align-items: center; justify-content: space-between; padding: 8px 10px; border-radius: 6px; color: var(--rail-ink); }
.nav-link:hover { background: rgba(255,255,255,.06); text-decoration: none; color: #fff; }
.nav-link.active { background: rgba(255,255,255,.1); color: #fff; }
.nav-count { font-size: 12px; min-width: 22px; text-align: center; padding: 0 6px; border-radius: 10px; background: var(--late); color: #fff; font-weight: 600; }
.rail-foot { margin-top: auto; padding: 10px; font-size: 13px; border-top: 1px solid rgba(255,255,255,.08); }
.rail-foot button { background: none; border: 0; color: var(--rail-ink); padding: 0; cursor: pointer; text-decoration: underline; }
.main { padding: 24px 32px 48px; min-width: 0; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }

/* ---------- элементы ---------- */
.btn { border: 1px solid var(--line); background: var(--surface); padding: 7px 14px; border-radius: 6px; cursor: pointer; line-height: 1.3; }
.btn:hover { border-color: var(--ink-3); }
.btn.primary { background: var(--action); border-color: var(--action); color: var(--action-ink); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: none; padding: 6px 12px; cursor: pointer; color: var(--ink-2); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--ink); color: var(--surface); }
input.field, select.field, textarea.field { border: 1px solid var(--line); background: var(--surface); border-radius: 6px; padding: 7px 10px; width: 100%; }
textarea.field { resize: vertical; min-height: 64px; }
label.lbl { display: block; font-size: 13px; color: var(--ink-2); margin: 10px 0 4px; }
.panel { background: var(--surface); border: 1px solid var(--line-2); border-radius: var(--radius); padding: 20px; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 10px; background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--line-2); white-space: nowrap; }
.tag.late { background: var(--late-bg); color: var(--late); border-color: transparent; }
.tag.wait { background: var(--wait-bg); color: var(--wait); border-color: transparent; }
.tag.ok { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
.ch { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-2); }
.ch i { width: 8px; height: 8px; border-radius: 2px; display: inline-block; }
.ch-phone i { background: #6B7C8F; } .ch-avito i { background: #00AAFF; } .ch-whatsapp i { background: #25B35A; }
.ch-telegram i { background: #2AA3DF; } .ch-max i { background: #7A5AF8; } .ch-vk i { background: #2D6FD6; }
.empty { padding: 40px 20px; text-align: center; color: var(--ink-3); }
.toast { position: fixed; right: 20px; bottom: 20px; background: var(--ink); color: var(--surface); padding: 10px 16px; border-radius: 6px; z-index: 50; max-width: 420px; }
.toast.err { background: var(--late); color: #fff; }

/* ---------- ряд ключевых цифр ---------- */
.figures { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); background: var(--surface); border: 1px solid var(--line-2); border-radius: var(--radius); margin-bottom: 28px; }
.fig { padding: 14px 18px; border-left: 1px solid var(--line-2); }
.fig:first-child { border-left: 0; }
.fig b { display: block; font-size: 26px; font-weight: 600; line-height: 1.15; }
.fig span { font-size: 13px; color: var(--ink-2); }
.fig.alarm b { color: var(--late); }

/* ---------- очередь ожидания (главный элемент) ---------- */
.queue { display: flex; flex-direction: column; gap: 6px; }
.q-row { display: grid; grid-template-columns: 200px 1fr 150px 120px; gap: 16px; align-items: center; background: var(--surface); border: 1px solid var(--line-2); border-radius: var(--radius); padding: 12px 16px; position: relative; overflow: hidden; }
.q-row.overdue { border-color: color-mix(in srgb, var(--late) 45%, transparent); }
.q-clock { display: flex; flex-direction: column; gap: 6px; }
.q-time { font-size: 24px; font-weight: 600; line-height: 1; letter-spacing: -0.01em; }
.q-row.overdue .q-time { color: var(--late); }
.q-row.waiting .q-time { color: var(--wait); }
.q-bar { height: 6px; background: var(--line-2); border-radius: 3px; position: relative; }
.q-bar em { position: absolute; inset: 0 auto 0 0; border-radius: 3px; background: var(--wait); transition: width .6s linear; }
.q-row.overdue .q-bar em { background: var(--late); }
.q-bar s { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--ink-3); }
.q-limit { font-size: 12px; color: var(--ink-3); }
.q-who b { font-weight: 600; }
.q-who p { margin: 2px 0 0; color: var(--ink-2); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.q-mgr { font-size: 13px; color: var(--ink-2); }
.q-act { text-align: right; }
@media (prefers-reduced-motion: reduce) { .q-bar em { transition: none; } }

/* ---------- таблицы ---------- */
.tbl-wrap { background: var(--surface); border: 1px solid var(--line-2); border-radius: var(--radius); overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th { text-align: left; font-weight: 500; color: var(--ink-2); font-size: 13px; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; background: var(--surface-2); }
.tbl td { padding: 9px 12px; border-bottom: 1px solid var(--line-2); vertical-align: middle; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl tr.click { cursor: pointer; }
.tbl tr.click:hover td { background: var(--surface-2); }
.tbl .num { text-align: right; white-space: nowrap; }
.tbl th.num { text-align: right; }
.bad { color: var(--late); font-weight: 600; }
.good { color: var(--ok); }
.score { display: inline-block; min-width: 34px; text-align: center; border-radius: 4px; padding: 0 6px; font-weight: 600; font-size: 13px; }
.score.hi { background: var(--ok-bg); color: var(--ok); } .score.mid { background: var(--wait-bg); color: var(--wait); } .score.lo { background: var(--late-bg); color: var(--late); }

/* ---------- переписки ---------- */
.inbox { display: grid; grid-template-columns: 340px 1fr; gap: 0; background: var(--surface); border: 1px solid var(--line-2); border-radius: var(--radius); height: calc(100vh - 140px); overflow: hidden; }
.inbox-list { border-right: 1px solid var(--line-2); overflow-y: auto; }
.inbox-filters { padding: 10px; border-bottom: 1px solid var(--line-2); display: flex; flex-direction: column; gap: 8px; position: sticky; top: 0; background: var(--surface); z-index: 1; }
.conv { padding: 10px 14px; border-bottom: 1px solid var(--line-2); cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.conv:hover { background: var(--surface-2); }
.conv.on { background: color-mix(in srgb, var(--action) 9%, var(--surface)); }
.conv b { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv p { margin: 0; font-size: 13px; color: var(--ink-2); grid-column: 1 / -1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread { display: flex; flex-direction: column; min-width: 0; }
.thread-head { padding: 12px 18px; border-bottom: 1px solid var(--line-2); display: flex; justify-content: space-between; gap: 10px; align-items: center; flex-wrap: wrap; }
.msgs { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 8px; background: var(--surface-2); }
.msg { max-width: 68%; padding: 8px 12px; border-radius: 10px; background: var(--surface); border: 1px solid var(--line-2); align-self: flex-start; white-space: pre-wrap; word-wrap: break-word; }
.msg.out { align-self: flex-end; background: color-mix(in srgb, var(--action) 10%, var(--surface)); border-color: transparent; }
.msg small { display: block; font-size: 12px; color: var(--ink-3); margin-top: 3px; }
.msg small .late { color: var(--late); font-weight: 600; }
.reply { border-top: 1px solid var(--line-2); padding: 12px; display: flex; gap: 10px; }
.handoff-note { margin: 0; padding: 8px 14px; font-size: 13px; background: var(--late-bg); color: var(--late); border-top: 1px solid var(--line-2); }
.reply textarea { flex: 1; min-height: 44px; }

/* ---------- звонок / клиент ---------- */
.drawer-bg { position: fixed; inset: 0; background: rgba(10,16,22,.4); z-index: 30; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(620px, 100%); background: var(--surface); z-index: 31; overflow-y: auto; padding: 24px; box-shadow: -8px 0 30px rgba(0,0,0,.15); }
.check { display: grid; grid-template-columns: 22px 1fr; gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--line-2); }
.check .mark { font-weight: 700; }
.check .yes { color: var(--ok); } .check .no { color: var(--late); }
.transcript { white-space: pre-wrap; font-size: 14px; background: var(--surface-2); padding: 12px; border-radius: 6px; max-height: 320px; overflow-y: auto; }
.kv { display: grid; grid-template-columns: 150px 1fr; gap: 6px 12px; font-size: 14px; margin: 12px 0 20px; }
.kv dt { color: var(--ink-3); } .kv dd { margin: 0; }
.timeline { display: flex; flex-direction: column; }
.tl { display: grid; grid-template-columns: 110px 22px 1fr; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line-2); font-size: 14px; }
.tl time { color: var(--ink-3); font-size: 13px; }
.tl.tl-alert { grid-template-columns: 90px 1fr auto; align-items: start; }

/* ---------- сделки ---------- */
.board { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(230px, 1fr); gap: 12px; overflow-x: auto; padding-bottom: 8px; }
.col { background: var(--surface-2); border: 1px solid var(--line-2); border-radius: var(--radius); padding: 10px; min-height: 300px; }
.col h3 { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; }
.col.drop { outline: 2px dashed var(--action); }
.deal { background: var(--surface); border: 1px solid var(--line-2); border-radius: 6px; padding: 10px; margin-bottom: 8px; cursor: grab; font-size: 14px; }
.deal b { font-weight: 600; display: block; }

/* ---------- графики ---------- */
.bars { display: grid; grid-template-columns: repeat(24, 1fr); gap: 3px; align-items: end; height: 140px; }
.bars div { background: var(--action); border-radius: 2px 2px 0 0; position: relative; min-height: 1px; }
.bars div i { position: absolute; left: 0; right: 0; bottom: 0; background: var(--late); border-radius: 2px 2px 0 0; }
.bars-x { display: grid; grid-template-columns: repeat(24, 1fr); gap: 3px; font-size: 11px; color: var(--ink-3); text-align: center; margin-top: 4px; }
.meter { height: 6px; background: var(--line-2); border-radius: 3px; overflow: hidden; min-width: 60px; }
.meter i { display: block; height: 100%; background: var(--ok); }

/* ---------- вход ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: min(360px, 100%); }
.login h1 { margin-bottom: 4px; }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  /* телефон: меню — одна строка с прокруткой, чтобы очереди и переписке хватало экрана */
  .rail { position: sticky; top: 0; z-index: 20; height: auto; flex-direction: row; align-items: center; padding: 6px 8px; overflow-x: auto; scrollbar-width: none; }
  .brand { display: none; }
  .nav-link { flex: none; gap: 6px; white-space: nowrap; }
  .rail-foot { flex: none; margin: 0 0 0 auto; padding: 8px 10px; border-top: 0; font-size: 0; }
  .rail-foot br { display: none; }
  .rail-foot button { font-size: 13px; }
  .main { padding: 12px; }
  .q-row { grid-template-columns: 92px 1fr auto; grid-template-areas: "clock who who" "clock mgr act"; gap: 6px 12px; padding: 10px 12px; }
  .q-clock { grid-area: clock; }
  .q-who { grid-area: who; min-width: 0; }
  .q-mgr { grid-area: mgr; align-self: center; }
  .q-act { grid-area: act; }
  .q-time { font-size: 20px; }
  .inbox { grid-template-columns: 1fr; height: auto; }
  .inbox:not(.has-thread) .thread { display: none; }
  .inbox.has-thread .inbox-list { display: none; }
  .main:has(.inbox.has-thread) > .page-head { display: none; }
  .inbox.has-thread { height: calc(100dvh - 76px); }
  .reply { padding: 8px; }
  .reply textarea { font-size: 16px; }   /* меньше 16px — iPhone увеличивает страницу при вводе */
  .grid2 { grid-template-columns: 1fr; }
  .msg { max-width: 88%; }
  .tl.tl-alert { grid-template-columns: 1fr auto; }
  .tl.tl-alert time { grid-column: 1 / -1; }
  /* показатели компактно в 3 столбца — очередь «ждут реакции» должна быть видна без прокрутки */
  .figures { grid-template-columns: repeat(3, 1fr); margin-bottom: 16px; }
  .fig { padding: 8px 10px; border-top: 1px solid var(--line-2); }
  .fig:nth-child(3n+1) { border-left: 0; }
  .fig:nth-child(-n+3) { border-top: 0; }
  .fig b { font-size: 19px; }
  .fig span { font-size: 11px; line-height: 1.25; display: block; }
  .kv { grid-template-columns: 1fr; }
}
