/* Токены — референс-палитра dataviz (light/dark). */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --series-1: #2a78d6;   /* комменты */
  --series-2: #eb6834;   /* сообщения */
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --critical: #d03b3b;
  --accent: #2a78d6;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --series-1: #3987e5;
    --series-2: #d95926;
    --good-text: #0ca30c;
    --accent: #3987e5;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.45;
}
a { color: var(--accent); text-decoration: none; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0 0 6px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }

/* --- шапка --- */
.topbar {
  display: flex; align-items: center; gap: 20px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
  flex-wrap: wrap;
}
.brand { font-weight: 700; white-space: nowrap; }
.brand-sub { font-weight: 400; color: var(--muted); font-size: 12.5px; }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a {
  color: var(--ink-2); padding: 6px 12px; border-radius: 8px;
}
.topbar nav a.active { background: var(--page); color: var(--ink); font-weight: 600; }
.topbar nav a:hover { color: var(--ink); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.user-name { color: var(--ink-2); }
.logout { color: var(--muted); font-size: 13px; }
select, input {
  font: inherit; color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--baseline); border-radius: 8px;
  padding: 6px 10px;
}

main { max-width: 1200px; margin: 0 auto; padding: 20px; }

.page-head { display: flex; align-items: baseline; gap: 16px; margin-bottom: 16px; flex-wrap: wrap; }
.page-meta { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.ghost-btn {
  font: inherit; font-size: 13.5px; color: var(--ink-2);
  background: none; border: 1px solid var(--baseline); border-radius: 8px;
  padding: 5px 12px; cursor: pointer;
}
.ghost-btn:hover { color: var(--ink); border-color: var(--muted); }
.ghost-btn:disabled { opacity: 0.5; cursor: default; }

/* --- KPI --- */
.kpi-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px; margin-bottom: 16px;
}
.kpi {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px 14px;
}
.kpi .v { font-size: 28px; font-weight: 700; }
.kpi .l { color: var(--ink-2); font-size: 12.5px; margin-top: 2px; }
.kpi .s { color: var(--muted); font-size: 12px; }
.kpi .s.good { color: var(--good-text); }

.two-col { display: grid; grid-template-columns: 1fr 1.4fr; gap: 12px; align-items: start; }
@media (max-width: 900px) { .two-col { grid-template-columns: 1fr; } }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 16px;
}

/* --- воронка --- */
.funnel-row { display: grid; grid-template-columns: 130px 1fr 110px; gap: 10px; align-items: center; margin: 7px 0; }
.funnel-label { font-size: 13px; color: var(--ink-2); text-align: right; }
.funnel-track { height: 22px; position: relative; }
.funnel-bar { height: 100%; border-radius: 0 4px 4px 0; min-width: 2px; }
.funnel-val { font-size: 13px; font-variant-numeric: tabular-nums; }
.funnel-val .pct { color: var(--muted); margin-left: 6px; }

/* --- график --- */
.legend { display: flex; gap: 16px; margin: 4px 0 8px; font-size: 13px; color: var(--ink-2); }
.legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: baseline; }
.chart-box { position: relative; width: 100%; }
.chart-box svg { display: block; width: 100%; height: auto; }
.chart-tip {
  position: absolute; pointer-events: none; z-index: 5;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 10px; font-size: 12.5px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
  white-space: nowrap; display: none;
}
.chart-tip .tv { font-variant-numeric: tabular-nums; }

/* --- таблица --- */
.toolbar { display: flex; gap: 12px; align-items: center; margin-bottom: 12px; flex-wrap: wrap; }
#search { flex: 0 1 320px; }
#date-filter { flex: 0 0 auto; color-scheme: inherit; }
#date-clear { flex: 0 0 auto; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  font: inherit; font-size: 13px; color: var(--ink-2);
  background: var(--surface); border: 1px solid var(--baseline);
  border-radius: 999px; padding: 4px 12px; cursor: pointer;
}
.chip:hover { color: var(--ink); }
.chip.on { background: var(--ink); color: var(--surface); border-color: var(--ink); font-weight: 600; }
.chip .n { color: inherit; opacity: 0.65; margin-left: 4px; font-variant-numeric: tabular-nums; }

.table-wrap { padding: 0; overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
thead th {
  text-align: left; font-size: 12px; color: var(--muted); font-weight: 600;
  padding: 10px 12px; border-bottom: 1px solid var(--grid);
  cursor: pointer; user-select: none; white-space: nowrap;
  position: sticky; top: 0; background: var(--surface);
}
thead th.sorted::after { content: " ↓"; }
thead th.sorted.asc::after { content: " ↑"; }
tbody td { padding: 9px 12px; border-bottom: 1px solid var(--grid); vertical-align: top; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--page); }
td.ellip { max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }

.badge { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; white-space: nowrap; }
.badge .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.badge.b-sale .dot { background: var(--good); }
.badge.b-replied .dot { background: var(--series-1); }
.badge.b-messaged .dot { background: var(--series-2); }
.badge.b-added .dot { background: var(--good); }
.badge.b-requested .dot { background: var(--warning); }
.badge.b-commented .dot { background: var(--series-1); }
.badge.b-check .dot { background: var(--warning); }
.badge.b-failed .dot { background: var(--critical); }
.badge.b-refused .dot { background: var(--critical); }
.badge.b-cancelled .dot { background: var(--baseline); }
.badge.b-sale, .badge.b-added { font-weight: 600; }

.table-footer { display: flex; align-items: center; padding: 10px 12px; gap: 12px; }
.pager { margin-left: auto; display: flex; align-items: center; gap: 8px; font-variant-numeric: tabular-nums; }

/* --- модалка --- */
.modal[hidden] { display: none; }
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0, 0, 0, 0.45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 5vh 16px;
}
.modal-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 20px 22px; position: relative;
  width: min(720px, 100%); max-height: 88vh; overflow-y: auto;
}
.modal-close {
  position: absolute; top: 8px; right: 12px;
  font-size: 24px; background: none; border: none; color: var(--muted); cursor: pointer;
}
.modal-close:hover { color: var(--ink); }
.mrow { margin: 10px 0; }
.mrow .ml { font-size: 12px; color: var(--muted); margin-bottom: 2px; }
.mrow .mv { white-space: pre-wrap; word-break: break-word; }
.mrow .mv.hist {
  background: var(--page); border: 1px solid var(--grid); border-radius: 8px;
  padding: 10px 12px; font-size: 13.5px;
}

/* --- логин --- */
.login-body {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; background: var(--page); margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif; color: var(--ink);
}
.login-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 32px 36px; width: min(360px, 92vw); text-align: center;
}
.login-logo { font-size: 40px; }
.login-card h1 { font-size: 20px; margin: 8px 0 2px; }
.login-sub { color: var(--muted); font-size: 13px; margin: 0 0 18px; }
.login-card label { display: block; text-align: left; font-size: 13px; color: var(--ink-2); margin: 12px 0; }
.login-card input { width: 100%; margin-top: 4px; padding: 9px 12px; }
.login-card button {
  width: 100%; margin-top: 16px; padding: 10px;
  font: inherit; font-weight: 600; color: #fff;
  background: var(--accent); border: none; border-radius: 10px; cursor: pointer;
}
.login-error {
  background: color-mix(in srgb, var(--critical) 12%, var(--surface));
  color: var(--critical); border-radius: 8px; padding: 8px; font-size: 13.5px;
}

.load-error {
  margin-top: 16px; padding: 12px 16px; border-radius: 10px;
  background: color-mix(in srgb, var(--critical) 10%, var(--surface));
  color: var(--critical);
}

/* ── CRM: диалоги, указания, уроки ─────────────────────────── */
.crm-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 16px;
}
@media (max-width: 900px) { .crm-grid { grid-template-columns: 1fr; } }
.crm-card h2 { margin: 0 0 4px; font-size: 16px; }
.distilled {
  margin-top: 10px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--page);
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-wrap;
}
.lessons { list-style: none; margin: 10px 0; padding: 0; max-height: 220px; overflow-y: auto; }
.lesson-item {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--grid);
  font-size: 13px;
}
.lesson-item:last-child { border-bottom: 0; }
.lesson-text { flex: 1; }
.lesson-del { padding: 0 8px; line-height: 1.4; }
.lesson-add { display: flex; gap: 8px; align-items: flex-start; }
.lesson-add textarea { flex: 1; }
textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  resize: vertical;
}
textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.primary-btn {
  padding: 7px 14px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}
.primary-btn:hover { filter: brightness(1.08); }
.primary-btn:disabled { opacity: 0.6; cursor: default; }
.dialog-hist {
  max-height: 320px;
  overflow-y: auto;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--page);
  font-size: 13px;
  line-height: 1.5;
}
.hline { padding: 3px 0; }
.hline.hbot { color: var(--series-1); }
.note-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin-top: 8px;
}

/* CRM v2: живой чат и превью реплик */
.chat { display: flex; flex-direction: column; gap: 8px; }
.msg { max-width: 78%; padding: 7px 10px; border-radius: 12px; font-size: 13px; line-height: 1.45; }
.msg.them { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.me { align-self: flex-end; background: color-mix(in srgb, var(--accent) 14%, var(--surface)); border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent); border-bottom-right-radius: 4px; }
.msg-text { white-space: pre-wrap; word-break: break-word; }
.msg-at { margin-top: 2px; text-align: right; font-size: 10px; }
.lastmsg { max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.from-them { color: var(--series-2); }
.from-me { color: var(--series-1); }
.await-row td:first-child { box-shadow: inset 3px 0 0 var(--series-2); }
.who a { font-weight: 600; }
#camp-segment { width: 100%; margin-bottom: 8px; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--ink); font: inherit; font-size: 13px; }

/* Структурный свод правил по стадиям */
.rb-sec { margin-bottom: 8px; }
.rb-sec:last-child { margin-bottom: 0; }
.rb-head { font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--accent); margin-bottom: 2px; }
.rb-rule { padding-left: 4px; }

/* Парсер аудитории */
#parse-link { width: 100%; margin-bottom: 8px; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--ink); font: inherit; font-size: 13px; }
.parse-params { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; font-size: 12px; color: var(--muted); }
.parse-params label { display: inline-flex; align-items: center; gap: 4px; }
.parse-params input { width: 66px; padding: 4px 6px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--ink); font: inherit; font-size: 12px; }

/* Списки аудитории */
.list-active { background: color-mix(in srgb, var(--accent) 8%, transparent); border-radius: 6px; padding-left: 6px; }
#parse-target { flex: 1; padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--ink); font: inherit; font-size: 12px; }
#parse-list-name { width: 100%; margin-top: 6px; padding: 6px 9px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--ink); font: inherit; font-size: 13px; }

/* Редактор промптов */
.prompt-card { margin-bottom: 16px; }
.prompt-card h2 { display: flex; align-items: center; gap: 8px; }
.prompt-text { width: 100%; margin: 8px 0 0; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; line-height: 1.5; }
.badge-edited { font-size: 11px; font-weight: 500; padding: 2px 7px; border-radius: 10px; background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }
.badge-empty { font-size: 11px; font-weight: 500; padding: 2px 7px; border-radius: 10px; background: color-mix(in srgb, var(--muted) 20%, transparent); color: var(--muted); }
#period { padding: 5px 9px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--ink); font: inherit; font-size: 12px; }

/* Подключение аккаунта */
.step-card { margin-bottom: 16px; }
.step-num { display: inline-flex; align-items: center; justify-content: center; width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 14px; margin-right: 6px; }
#qr-box { margin-top: 12px; }
#qr-img { border: 1px solid var(--border); border-radius: 10px; padding: 8px; background: #fff; }
#token-url { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--ink); font: inherit; font-size: 12.5px; }
a.primary-btn { text-decoration: none; display: inline-block; }
.login-ok { background: color-mix(in srgb, var(--good) 14%, transparent); color: var(--good-text); padding: 10px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 12px; }
#users-table td { vertical-align: top; }

/* ---------- пейволл ---------- */
.paywall-banner {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  margin: 0 0 18px; padding: 18px 20px; border-radius: 14px;
  background: linear-gradient(135deg, #fff3cd, #ffe08a);
  border: 1px solid #e0a800; color: #4a3600;
  box-shadow: 0 2px 12px rgba(224, 168, 0, .18);
}
.paywall-banner.soft {
  background: linear-gradient(135deg, #e7f1ff, #cfe2ff);
  border-color: #7aa7e9; color: #16345e;
  box-shadow: 0 2px 12px rgba(122, 167, 233, .18);
}
.paywall-ico { font-size: 26px; line-height: 1; }
.paywall-text { display: flex; flex-direction: column; gap: 3px; flex: 1 1 320px; }
.paywall-text b { font-size: 16px; }
.paywall-text span { font-size: 13px; opacity: .85; }
.paywall-btn { margin-left: auto; white-space: nowrap; }

/* Шаги подключения до оплаты: видны, но неактивны */
.step-card.locked { opacity: .55; filter: grayscale(.35); position: relative; }
.step-card.locked::after { content: ; position: absolute; inset: 0; cursor: not-allowed; }

/* таблица тарифов */
.tariff-table { width: 100%; border-collapse: collapse; margin-top: 12px; }
.tariff-table th, .tariff-table td {
  text-align: left; padding: 10px 12px; border-bottom: 1px solid rgba(128,128,128,.18);
}
.tariff-table th { font-size: 12px; text-transform: uppercase; letter-spacing: .4px; opacity: .65; }
.tariff-table tbody tr:last-child td { border-bottom: none; }
.save-cell { color: #0f9d58; font-weight: 600; }

/* ---------- помощник по промптам ---------- */
.wizard-invite { display: flex; align-items: center; gap: 18px; flex-wrap: wrap; }
.wizard-invite-text { display: flex; flex-direction: column; gap: 4px; flex: 1 1 340px; }
.wizard-invite-text b { font-size: 16px; }
.wizard-chat {
  max-height: 420px; overflow-y: auto; padding: 12px 4px;
  display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px;
}
.wz-msg {
  max-width: 78%; padding: 10px 14px; border-radius: 14px;
  font-size: 14px; line-height: 1.5; white-space: pre-wrap;
}
.wz-bot { align-self: flex-start; background: rgba(128,128,128,.14); border-bottom-left-radius: 4px; }
.wz-me  { align-self: flex-end;   background: #2563eb; color: #fff; border-bottom-right-radius: 4px; }
.wz-loading { opacity: .7; font-style: italic; }
#wizard-answer { width: 100%; resize: vertical; }
.wz-draft-block { margin: 14px 0; }
.wz-draft-block h4 { margin: 0 0 6px; font-size: 15px; }
.wz-draft-block textarea { width: 100%; resize: vertical; font-size: 13px; }
