/* Радиусы: панели 12px, кнопки и поля 8px, статусы-чипы pill. */
:root {
  --bg: #eef0f3;
  --surface: #ffffff;
  --surface-2: #f6f7f9;
  --ink: #15181d;
  --muted: #5b6270;
  --line: #d8dce2;
  --accent: #2747d6;
  --accent-ink: #ffffff;
  --accent-soft: #e3e8fd;
  --amber: #8a5300;
  --amber-soft: #fbefd9;
  --green: #1d6b45;
  --green-soft: #ddf1e6;
  --red: #b42318;
  --bubble-user: #ffffff;
  --bubble-bot: #e3e8fd;
  --bubble-op: #2747d6;
  --shadow: 0 1px 2px rgb(21 24 29 / .06), 0 4px 16px rgb(21 24 29 / .05);
  --r-panel: 12px;
  --r-ctl: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1217;
    --surface: #171b22;
    --surface-2: #1d222b;
    --ink: #e8eaee;
    --muted: #9aa2af;
    --line: #2a303b;
    --accent: #8197ff;
    --accent-ink: #0f1217;
    --accent-soft: #232b4d;
    --amber: #f0b45c;
    --amber-soft: #3a2c14;
    --green: #6fd19d;
    --green-soft: #15301f;
    --red: #ff8a80;
    --bubble-user: #1d222b;
    --bubble-bot: #232b4d;
    --bubble-op: #8197ff;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.5 "Onest", system-ui, sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.6rem; line-height: 1.2; font-weight: 700; letter-spacing: -.02em; margin: 0; }
h2 { font-size: 1rem; font-weight: 600; margin: 0 0 .75rem; }
p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- каркас ---------- */
.shell { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: 100dvh; }
.rail {
  position: sticky; top: 0; height: 100dvh; display: flex; flex-direction: column; gap: 2px;
  padding: 20px 12px; border-right: 1px solid var(--line); background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 20px; color: var(--ink); }
.brand:hover { text-decoration: none; }
.brand-mark {
  display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: var(--r-ctl);
  background: var(--ink); color: var(--bg); font-size: 1.15rem;
}
.brand-name { font-weight: 600; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: var(--r-ctl);
  color: var(--muted); font-weight: 500;
}
.nav-item i { font-size: 1.25rem; }
.nav-item:hover { background: var(--surface-2); color: var(--ink); text-decoration: none; }
.nav-item.is-active { background: var(--accent-soft); color: var(--accent); }
.nav-item .badge { margin-left: auto; }
.rail-foot { margin-top: auto; display: grid; gap: 4px; }
.link-btn {
  font: inherit; color: var(--muted); background: none; border: 0; padding: 8px 10px; text-align: left;
  border-radius: var(--r-ctl); cursor: pointer; display: flex; gap: 8px; align-items: center;
}
.link-btn:hover { background: var(--surface-2); color: var(--ink); }
.main { padding: 28px 36px 48px; max-width: 1120px; width: 100%; }

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }

.badge {
  display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px;
  background: var(--accent); color: var(--accent-ink); font-size: .75rem; font-weight: 600;
}
.chip { display: inline-block; padding: 1px 9px; border-radius: 999px; font-size: .8rem; font-weight: 500; white-space: nowrap; }
.chip-new { background: var(--accent-soft); color: var(--accent); }
.chip-work { background: var(--amber-soft); color: var(--amber); }
.chip-done { background: var(--green-soft); color: var(--green); }
.chip-cancel { background: var(--surface-2); color: var(--muted); }

.btn {
  font: inherit; font-weight: 500; display: inline-flex; align-items: center; gap: 6px;
  min-height: 40px; padding: 0 14px; border-radius: var(--r-ctl); border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); cursor: pointer; white-space: nowrap;
  transition: transform .12s ease, background .12s ease;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn[disabled] { opacity: .6; cursor: progress; }

input, textarea, select {
  font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-ctl); padding: 9px 12px; width: 100%;
}
input::placeholder, textarea::placeholder { color: var(--muted); }
textarea { resize: vertical; }
select { width: auto; min-height: 40px; cursor: pointer; }

.tabs { display: flex; gap: 4px; overflow-x: auto; margin-bottom: 20px; scrollbar-width: none; }
.tabs.compact { margin: 0; }
.tab {
  padding: 7px 12px; border-radius: 999px; color: var(--muted); font-weight: 500; white-space: nowrap;
  border: 1px solid transparent;
}
.tab:hover { color: var(--ink); text-decoration: none; background: var(--surface); }
.tab.is-active { background: var(--surface); color: var(--ink); border-color: var(--line); }
.tab .num { color: var(--muted); font-weight: 400; margin-left: 2px; }

.empty { text-align: center; padding: 64px 16px; color: var(--muted); max-width: 420px; margin: 0 auto; }
.empty i { font-size: 2.5rem; display: block; margin-bottom: 12px; }
.empty-inline { color: var(--muted); text-align: center; padding: 32px 0; }
.notice { padding: 10px 14px; border-radius: var(--r-ctl); background: var(--amber-soft); color: var(--amber); margin-top: 12px; }
.hint { color: var(--muted); font-size: .85rem; margin-top: 8px; }
.error { color: var(--red); font-size: .9rem; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel); padding: 20px; box-shadow: var(--shadow); }
.section-title { margin-top: 32px; }

/* ---------- заявки ---------- */
.orders { list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; }
.order {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel); padding: 16px 18px;
  box-shadow: var(--shadow); border-left: 4px solid var(--line);
}
.order.status-new { border-left-color: var(--accent); }
.order.status-work { border-left-color: var(--amber); }
.order.status-done { border-left-color: var(--green); }
.order-top { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.order-top time { margin-left: auto; font-size: .85rem; }
.order-id { font-weight: 700; }
.kind { color: var(--muted); font-size: .9rem; }
.order-task { margin: 10px 0 12px; font-size: 1.02rem; max-width: 72ch; white-space: pre-line; }
.order-meta { display: flex; flex-wrap: wrap; gap: 6px 28px; margin: 0 0 14px; }
.order-meta div { display: flex; gap: 6px; }
.order-meta dt { color: var(--muted); }
.order-meta dd { margin: 0; font-weight: 500; }
.order-actions { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.order-actions .btn { max-width: 100%; overflow: hidden; text-overflow: ellipsis; }

/* ---------- лиды ---------- */
.search { position: relative; width: min(320px, 100%); }
.search i { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.search input { padding-left: 36px; }
.leads { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel); overflow: hidden; }
.leads li + li { border-top: 1px solid var(--line); }
.lead-row { display: grid; grid-template-columns: 40px 1fr auto; gap: 12px; align-items: center; padding: 12px 16px; color: var(--ink); }
.lead-row:hover { background: var(--surface-2); text-decoration: none; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-weight: 600;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--line);
}
.has-unread .avatar { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.lead-main { min-width: 0; display: grid; }
.lead-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lead-name .muted { font-weight: 400; }
.lead-last { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .92rem; }
.has-unread .lead-last { color: var(--ink); }
.lead-side { display: grid; justify-items: end; gap: 4px; font-size: .85rem; }
.lead-tags { display: flex; gap: 4px; }

/* ---------- карточка лида ---------- */
.lead-head { justify-content: flex-start; }
.lead-head > div { flex: 1; min-width: 0; }
.lead-head h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { display: grid; place-items: center; width: 40px; height: 40px; border-radius: var(--r-ctl); color: var(--ink); font-size: 1.2rem; }
.back:hover { background: var(--surface); }
.mode { display: flex; align-items: center; gap: 10px; }
.mode-label { color: var(--muted); }
.mode-label b { color: var(--ink); }
.lead-grid { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 20px; align-items: start; }
.chat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel); padding: 16px; box-shadow: var(--shadow); }
.chat-log {
  display: flex; flex-direction: column; gap: 8px; max-height: calc(100dvh - 300px); min-height: 280px;
  overflow-y: auto; padding: 4px 4px 12px; overscroll-behavior: contain;
}
.bubble { max-width: 78%; padding: 8px 12px; border-radius: 14px; border: 1px solid var(--line); }
.bubble p { white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble-meta { display: block; font-size: .75rem; color: var(--muted); margin-top: 2px; text-align: right; }
.from-user { align-self: flex-start; background: var(--bubble-user); border-bottom-left-radius: 4px; }
.from-assistant { align-self: flex-end; background: var(--bubble-bot); border-color: transparent; border-bottom-right-radius: 4px; }
.from-operator { align-self: flex-end; background: var(--bubble-op); color: var(--accent-ink); border-color: transparent; border-bottom-right-radius: 4px; }
.from-operator .bubble-meta { color: inherit; opacity: .75; }
.bubble.is-new { animation: pop .22s cubic-bezier(.16, 1, .3, 1); }
@keyframes pop { from { opacity: 0; transform: translateY(6px); } }
.composer { display: flex; gap: 8px; align-items: flex-end; border-top: 1px solid var(--line); padding-top: 12px; }
.composer textarea { min-height: 44px; max-height: 200px; }
.composer .btn { min-height: 44px; }
.lead-side-panel { display: grid; gap: 16px; }
.lead-side-panel section { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel); padding: 16px; }
.mini-order + .mini-order { border-top: 1px solid var(--line); margin-top: 12px; padding-top: 12px; }
.mini-order p { margin-top: 6px; font-size: .92rem; }
.timeline { list-style: none; margin: 0 0 12px; padding: 0 0 0 14px; border-left: 2px solid var(--line); max-height: 360px; overflow-y: auto; }
.timeline li { position: relative; display: flex; justify-content: space-between; gap: 8px; padding: 4px 0; font-size: .9rem; }
.timeline li::before { content: ""; position: absolute; left: -19px; top: 11px; width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.timeline .ev-order::before { background: var(--accent); }
.timeline .ev-operator_call::before { background: var(--amber); }
.timeline .ev-blocked::before { background: var(--red); }
.timeline time { white-space: nowrap; font-size: .8rem; }

/* ---------- рассылка ---------- */
.broadcast-form { display: grid; gap: 8px; max-width: 680px; }
.broadcast-form label { font-weight: 600; }
.broadcast-form .btn { justify-self: start; margin-top: 8px; }
.bc-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; max-width: 680px; }
.bc-list li { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel); padding: 12px 16px; }
.bc-list p { white-space: pre-line; }
.bc-meta { display: flex; justify-content: space-between; gap: 12px; margin-top: 6px; font-size: .85rem; flex-wrap: wrap; }
.bc-stat { font-weight: 500; }

/* ---------- воронка ---------- */
.funnel { display: grid; gap: 0; max-width: 760px; }
.step { position: relative; min-height: 64px; display: flex; align-items: center; }
.step-bar {
  position: absolute; inset: 0 auto 0 0; width: var(--w); border-radius: var(--r-ctl);
  background: var(--accent); transform-origin: left; animation: grow .7s cubic-bezier(.16, 1, .3, 1) both;
}
.step-bar { animation-delay: calc(var(--i) * 80ms); opacity: calc(1 - var(--i) * .22); }
@keyframes grow { from { transform: scaleX(0); } }
.step-text {
  position: relative; display: flex; align-items: baseline; gap: 12px; padding: 0 16px;
  color: var(--ink); background: var(--surface); border-radius: var(--r-ctl); margin-left: 8px; padding: 6px 12px;
}
.step-n { font-size: 1.7rem; font-weight: 700; letter-spacing: -.02em; }
.step-name { color: var(--muted); font-weight: 500; }
.drop { display: flex; gap: 16px; padding: 8px 0 8px 24px; font-size: .9rem; border-left: 2px dashed var(--line); margin-left: 20px; }
.funnel-note { margin-top: 16px; max-width: 72ch; font-size: .92rem; }
.chart-legend { display: flex; gap: 16px; font-size: .85rem; color: var(--muted); margin-bottom: 8px; }
.lg::before { content: ""; display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.lg-users::before { background: var(--muted); opacity: .35; }
.lg-orders::before { background: var(--accent); }
.chart {
  display: grid; grid-auto-flow: column; grid-auto-columns: minmax(8px, 1fr); gap: 3px; align-items: end;
  height: 200px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel);
  padding: 16px 12px 28px; overflow-x: auto;
}
.col { position: relative; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; }
.bars { position: relative; flex: 1; display: flex; align-items: flex-end; gap: 1px; }
.bar { flex: 1; height: max(var(--h), 1px); border-radius: 3px 3px 0 0; }
.bar-users { background: var(--muted); opacity: .35; }
.bar-orders { background: var(--accent); }
.col-label { position: absolute; bottom: -22px; left: 50%; transform: translateX(-50%); font-size: .7rem; color: var(--muted); white-space: nowrap; visibility: hidden; }
.col-label.show { visibility: visible; }

/* ---------- вход ---------- */
.login-body { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login {
  width: min(360px, 100%); display: grid; gap: 10px; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-panel); padding: 28px; box-shadow: var(--shadow);
}
.login h1 { font-size: 1.3rem; margin-bottom: 8px; }
.login label { font-weight: 500; }
.login .btn { justify-content: center; margin-top: 6px; }

.toast {
  position: fixed; right: 20px; bottom: 20px; z-index: 30; max-width: 340px; padding: 12px 16px;
  background: var(--ink); color: var(--bg); border-radius: var(--r-ctl); box-shadow: 0 8px 24px rgb(0 0 0 / .2);
  animation: pop .22s cubic-bezier(.16, 1, .3, 1);
}
.toast a { color: inherit; text-decoration: underline; }

/* ---------- планшет и телефон ---------- */
@media (max-width: 1000px) {
  .lead-grid { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  body { font-size: 16px; }
  .shell { grid-template-columns: minmax(0, 1fr); }
  .rail {
    position: fixed; inset: auto 0 0 0; top: auto; height: auto; z-index: 20; flex-direction: row; justify-content: space-around;
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); border-right: 0; border-top: 1px solid var(--line);
  }
  .brand, .rail-foot { display: none; }
  .nav-item { flex-direction: column; gap: 2px; font-size: .72rem; padding: 6px 10px; position: relative; flex: 1; min-height: 52px; justify-content: center; }
  .nav-item .badge { position: absolute; top: 2px; left: 50%; margin-left: 6px; min-width: 18px; height: 18px; font-size: .7rem; }
  .main { padding: 16px 16px calc(88px + env(safe-area-inset-bottom)); }
  h1 { font-size: 1.35rem; }
  .search { width: 100%; }
  .order { padding: 14px; }
  .order-actions { flex-direction: column; align-items: stretch; }
  .order-actions select { width: 100%; }
  .lead-head { flex-wrap: wrap; }
  .mode { width: 100%; justify-content: space-between; }
  .chat { padding: 10px; }
  .chat-log { max-height: 60dvh; }
  .bubble { max-width: 88%; }
  .composer .btn span { display: none; }
  .lead-row { padding: 12px; }
  .lead-tags .chip { display: none; }
  .toast { left: 16px; right: 16px; bottom: calc(76px + env(safe-area-inset-bottom)); max-width: none; }
  .step-n { font-size: 1.4rem; }
}
@media (hover: none) {
  .btn:hover, .nav-item:hover, .lead-row:hover, .tab:hover { background: inherit; }
  .nav-item.is-active { background: var(--accent-soft); }
  .btn-primary:hover { background: var(--accent); filter: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- настройки ---------- */
.status-row { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 20px; }
.status-row .chip { white-space: normal; }
.settings { display: grid; gap: 16px; max-width: 760px; }
.settings-group { display: grid; gap: 18px; }
.settings-group h2 { margin: 0; }
.field { display: grid; gap: 6px; scroll-margin-top: 16px; }
.field-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.field label { font-weight: 500; }
.field .hint { margin-top: 0; }
.link-btn.small { font-size: .85rem; padding: 4px 8px; }
textarea.mono-ish { font-size: .92rem; line-height: 1.55; }
.save-bar {
  position: sticky; bottom: 0; z-index: 5; display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 12px 0; background: var(--bg); border-top: 1px solid var(--line);
}
@media (max-width: 760px) {
  .save-bar { bottom: calc(64px + env(safe-area-inset-bottom)); flex-wrap: wrap; }
  .save-bar .btn { width: 100%; justify-content: center; }
}

/* Источники лидов (код из t.me/<bot>?start=<код>) */
.chip-ref { background: var(--surface-2); color: var(--ink); border: 1px solid var(--line); font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: .76rem; }
.ref-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-panel); overflow-x: auto; }
.ref-table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.ref-table th, .ref-table td { padding: 10px 14px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
.ref-table th:first-child, .ref-table td:first-child { text-align: left; }
.ref-table th { font-weight: 500; color: var(--muted); font-size: .82rem; }
.ref-table tr:last-child td { border-bottom: 0; }
.ref-table code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: .85rem; }
.order-note { margin: 6px 0 0; color: var(--muted); }
.order-top + .order-meta { margin-top: 8px; }
.order-meta dt { white-space: nowrap; }
