/* CRM «ИИ-сотрудники» — единая дизайн-система.
   Без JavaScript и внешних ресурсов (CSP: style-src 'self'). Motion только на CSS и отключается
   в @media (prefers-reduced-motion: reduce) в самом конце файла. */

/* ───────── 1. Токены ───────── */

:root {
  /* поверхности и текст */
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f9fafb;
  --row-hover: #f3f5fa;
  --text: #14181f;
  --text-2: #3b4452;
  --muted: #586271;
  --line: #e4e7ec;
  --line-strong: #d0d5dd;

  /* акцент и семантика */
  --accent: #3555d4;
  --accent-hover: #2943b0;
  --accent-soft: #eaeffd;
  --on-accent: #ffffff;
  --ok: #15803d;
  --ok-soft: #e6f5ec;
  --warn: #92580a;
  --warn-soft: #fdf3df;
  --error: #b42318;
  --error-soft: #fdecea;
  --focus: #3555d4;

  /* этапы воронки (насыщенный цвет; фон и текст пилюль выводятся из него) */
  --st-new: #5b6472;
  --st-reviewed: #6d4fd6;
  --st-letter_ready: #0e7490;
  --st-verified: #0f766e;
  --st-sent: #1f6fc5;
  --st-replied: #b45309;
  --st-negotiation: #c2410c;
  --st-contract: #a21caf;
  --st-invoiced: #be185d;
  --st-paid: #15803d;
  --st-in_work: #0369a1;
  --st-done: #166534;
  --st-lost: #b91c1c;
  --st-dropped: #6b7280;

  /* шкала отступов (4px) */
  --s1: 4px;
  --s2: 8px;
  --s3: 12px;
  --s4: 16px;
  --s5: 20px;
  --s6: 24px;
  --s8: 32px;
  --s10: 40px;

  /* радиусы */
  --r-sm: 6px;
  --r-md: 8px;
  --r-lg: 12px;
  --r-xl: 16px;

  /* мягкие многослойные тени */
  --shadow-xs: 0 1px 2px rgba(16, 24, 40, 0.05);
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --shadow-md: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 8px -2px rgba(16, 24, 40, 0.08), 0 12px 24px -8px rgba(16, 24, 40, 0.12);

  /* типографика */
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --fs-xs: 12px;
  --fs-sm: 13px;
  --fs-base: 14px;
  --fs-md: 15px;
  --fs-lg: 18px;
  --fs-xl: 22px;
  --lh-tight: 1.25;
  --lh-base: 1.5;

  /* motion */
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --dur: 0.18s;

  --sidebar-w: 248px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1217;
    --surface: #171b21;
    --surface-2: #1b2027;
    --row-hover: #1e242d;
    --text: #e8ebf0;
    --text-2: #c3c9d3;
    --muted: #9aa4b2;
    --line: #272d36;
    --line-strong: #373f4b;

    --accent: #8aa8ff;
    --accent-hover: #a6bcff;
    --accent-soft: #1b2542;
    --on-accent: #0b1226;
    --ok: #5fd394;
    --ok-soft: #12301f;
    --warn: #f2b861;
    --warn-soft: #35280f;
    --error: #ff8f84;
    --error-soft: #3a1a17;
    --focus: #a6bcff;

    --st-new: #9aa3b0;
    --st-reviewed: #b6a4ff;
    --st-letter_ready: #5cd6ee;
    --st-verified: #5eead4;
    --st-sent: #7eb6ff;
    --st-replied: #fbbf24;
    --st-negotiation: #fb923c;
    --st-contract: #e879f9;
    --st-invoiced: #f472b6;
    --st-paid: #4ade80;
    --st-in_work: #38bdf8;
    --st-done: #86efac;
    --st-lost: #ff8a80;
    --st-dropped: #9ca3af;

    --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.35), 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 12px -2px rgba(0, 0, 0, 0.4), 0 16px 28px -8px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

/* цвет этапа: класс stage-<ключ> задаёт --st для пилюли, полосы воронки, колонки канбана */
.stage-new { --st: var(--st-new); }
.stage-reviewed { --st: var(--st-reviewed); }
.stage-letter_ready { --st: var(--st-letter_ready); }
.stage-verified { --st: var(--st-verified); }
.stage-sent { --st: var(--st-sent); }
.stage-replied { --st: var(--st-replied); }
.stage-negotiation { --st: var(--st-negotiation); }
.stage-contract { --st: var(--st-contract); }
.stage-invoiced { --st: var(--st-invoiced); }
.stage-paid { --st: var(--st-paid); }
.stage-in_work { --st: var(--st-in_work); }
.stage-done { --st: var(--st-done); }
.stage-lost { --st: var(--st-lost); }
.stage-dropped { --st: var(--st-dropped); }

/* ───────── 2. База ───────── */

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: var(--fs-base) / var(--lh-base) var(--font);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-wrap: break-word;
}

a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-color: color-mix(in srgb, var(--accent) 35%, transparent);
  text-underline-offset: 2px;
  transition: color var(--dur) var(--ease), text-decoration-color var(--dur) var(--ease), background-color var(--dur) var(--ease);
}
a:hover { color: var(--accent-hover); text-decoration-color: currentColor; }

code { font: 0.92em var(--mono); background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--r-sm); padding: 1px 5px; }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: var(--r-sm); }

[id] { scroll-margin-top: 72px; }

h1, h2, h3 { margin: 0; line-height: var(--lh-tight); color: var(--text); }
h1 { font-size: var(--fs-xl); font-weight: 650; letter-spacing: -0.015em; }
h2 { font-size: var(--fs-md); font-weight: 600; letter-spacing: -0.005em; }
h3 { font-size: var(--fs-sm); font-weight: 600; color: var(--muted); margin: var(--s5) 0 var(--s2); }
h1 small, h2 small {
  display: inline-block;
  margin-left: var(--s2);
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0;
  vertical-align: middle;
  font-variant-numeric: tabular-nums;
}

.muted { color: var(--muted); }
small { font-size: var(--fs-xs); }
.ico { flex: none; }

/* ───────── 3. Каркас: сайдбар (≥1024) / верхняя панель (мобильные) ───────── */

.top {
  view-transition-name: chrome;
  position: sticky;
  top: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: var(--s2) var(--s3);
  padding: var(--s2) var(--s4) 0;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  box-shadow: var(--shadow-xs);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  color: var(--text);
  text-decoration: none;
}
.brand:hover { color: var(--text); }
.brand-mark {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: var(--r-md);
  background: linear-gradient(135deg, #5b7cff, #3b3fd1);
  color: #fff;
  box-shadow: 0 1px 2px rgba(16, 24, 40, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.25);
}
.brand-name { display: grid; line-height: 1.15; min-width: 0; }
.brand-name b { font-size: var(--fs-md); font-weight: 650; letter-spacing: -0.01em; }
.brand-name small { color: var(--muted); font-size: 11px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; }

.top nav {
  grid-column: 1 / -1;
  display: flex;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
  margin: 0 calc(var(--s4) * -1);
  padding: 0 var(--s4);
}
.top nav::-webkit-scrollbar { display: none; }
.top nav a {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: var(--s2);
  flex: none;
  min-height: 40px;
  padding: 0 var(--s3);
  color: var(--muted);
  font-weight: 500;
  white-space: nowrap;
  text-decoration: none;
  border-radius: var(--r-md);
}
.top nav a:hover { color: var(--text); background: var(--surface-2); }
.top nav a:focus-visible { outline-offset: -2px; }
.top nav a[aria-current="page"] { color: var(--accent); background: var(--accent-soft); }
/* мобильный индикатор активного раздела — линия снизу */
.top nav a[aria-current="page"]::after {
  content: "";
  position: absolute;
  left: var(--s3);
  right: var(--s3);
  bottom: 0;
  height: 2px;
  border-radius: 2px 2px 0 0;
  background: var(--accent);
}

.account { display: flex; align-items: center; gap: var(--s3); min-width: 0; }
.account form { margin: 0; }
.avatar {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: var(--fs-sm);
  font-weight: 650;
}
.who { display: grid; min-width: 0; line-height: 1.25; }
.who b { font-weight: 600; font-size: var(--fs-sm); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who small { color: var(--muted); }

main {
  view-transition-name: page;
  width: 100%;
  max-width: 1600px;
  min-width: 0;
  margin: 0 auto;
  padding: var(--s5) var(--s4) var(--s10);
}

@media (min-width: 1024px) {
  body.app { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); align-items: start; }

  .top {
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: var(--s1);
    padding: var(--s4) var(--s3);
    border-bottom: 0;
    border-right: 1px solid var(--line);
    box-shadow: none;
    overflow-y: auto;
  }
  .brand { padding: var(--s1) var(--s2) var(--s4); }
  .top nav {
    flex: 1;
    flex-direction: column;
    overflow: visible;
    margin: 0;
    padding: 0;
  }
  .top nav a { min-height: 36px; }
  .top nav a[aria-current="page"]::after {
    left: -12px;
    right: auto;
    top: 8px;
    bottom: 8px;
    width: 3px;
    height: auto;
    border-radius: 0 3px 3px 0;
  }
  .account {
    flex-wrap: wrap;
    padding: var(--s3) var(--s2) var(--s1);
    border-top: 1px solid var(--line);
  }
  .who { flex: 1; }
  .account form { width: 100%; }
  .account .ghost { width: 100%; justify-content: flex-start; }
  main { padding: var(--s8) var(--s10) 56px; }
}

@media (max-width: 520px) {
  .who { display: none; }
  .avatar { display: none; }
}

/* ───────── 4. Шапка страницы ───────── */

.page-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--s3) var(--s4);
  margin: 0 0 var(--s5);
}
.page-head > div:first-child { min-width: 0; }
.page-head p { margin: var(--s1) 0 0; font-size: var(--fs-sm); }
.page-head .filters.inline { gap: var(--s2) var(--s3); }

.crumbs { margin: 0 0 var(--s2); font-size: var(--fs-sm); color: var(--muted); }
.crumbs a { color: var(--muted); text-decoration: none; display: inline-flex; align-items: center; min-height: 24px; }
.crumbs a:hover { color: var(--accent); }

/* ───────── 5. Карточки и сетки ───────── */

.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); gap: var(--s4); margin-bottom: var(--s4); }
.card {
  min-width: 0;
  margin-bottom: var(--s4);
  padding: var(--s5);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
}
.grid .card { margin-bottom: 0; }
.card.wide { grid-column: 1 / -1; }
.card > h2 { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s2); margin-bottom: var(--s4); }
.card > h2 small { margin-left: 0; }
.card > .note:first-of-type { margin-top: calc(var(--s2) * -1); }

.row { display: flex; flex-wrap: wrap; gap: var(--s2); align-items: center; }
.stack { display: grid; gap: var(--s4); }

.note { margin: var(--s3) 0 0; color: var(--muted); font-size: var(--fs-sm); line-height: 1.55; }
.note a { font-weight: 500; }

/* ───────── 6. Кнопки и формы ───────── */

input, select, button, textarea { font: inherit; color: var(--text); }

input, select, textarea {
  min-height: 36px;
  padding: 7px 10px;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-xs);
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease), background-color var(--dur) var(--ease);
}
textarea { resize: vertical; min-height: 84px; line-height: 1.5; }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 1; }
input:hover:not(:disabled), select:hover:not(:disabled), textarea:hover:not(:disabled) { border-color: var(--muted); }
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid transparent;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 28%, transparent);
}
input:disabled, select:disabled, textarea:disabled { background: var(--surface-2); color: var(--muted); cursor: not-allowed; }
input[readonly] { background: var(--surface-2); }
input:user-invalid, select:user-invalid, textarea:user-invalid { border-color: var(--error); }
input[type="checkbox"] { width: 16px; height: 16px; min-height: 0; padding: 0; accent-color: var(--accent); box-shadow: none; }
input[type="number"] { font-variant-numeric: tabular-nums; }

button, .button, a.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  min-height: 36px;
  padding: 0 var(--s4);
  border: 1px solid transparent;
  border-radius: var(--r-md);
  background: var(--accent);
  color: var(--on-accent);
  font-size: var(--fs-base);
  font-weight: 550;
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
  box-shadow: var(--shadow-xs), inset 0 1px 0 rgba(255, 255, 255, 0.14);
  transition: background-color var(--dur) var(--ease), border-color var(--dur) var(--ease), color var(--dur) var(--ease), box-shadow var(--dur) var(--ease), transform var(--dur) var(--ease);
}
button:hover, a.button:hover { background: var(--accent-hover); color: var(--on-accent); box-shadow: var(--shadow-sm), inset 0 1px 0 rgba(255, 255, 255, 0.14); }
button:active, a.button:active { transform: translateY(1px); box-shadow: none; }
button:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }

button.secondary, a.button.secondary, button.danger {
  background: var(--surface);
  color: var(--text);
  border-color: var(--line-strong);
  box-shadow: var(--shadow-xs);
}
button.secondary:hover, a.button.secondary:hover { background: var(--surface-2); border-color: var(--muted); color: var(--text); }
button.danger { color: var(--error); }
button.danger:hover { background: var(--error-soft); border-color: var(--error); color: var(--error); }

button.ghost {
  min-height: 32px;
  padding: 0 var(--s3);
  background: transparent;
  color: var(--muted);
  font-weight: 500;
  box-shadow: none;
}
button.ghost:hover { background: var(--surface-2); color: var(--text); box-shadow: none; }

.filters {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: var(--s3) var(--s4);
  margin-bottom: var(--s4);
  padding: var(--s3) var(--s4);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-xs);
}
.filters > a { display: inline-flex; align-items: center; min-height: 36px; padding: 0 var(--s1); color: var(--muted); }
.filters > a:hover { color: var(--accent); }
.filters label, .stack label, .form-grid label, .grow { display: grid; gap: 6px; font-size: var(--fs-xs); font-weight: 550; color: var(--text-2); min-width: 0; }
.filters label input, .filters label select { font-weight: 400; font-size: var(--fs-base); }
.card .filters, .filters.inline, details .filters {
  padding: 0;
  background: none;
  border: 0;
  box-shadow: none;
  border-radius: 0;
}
.filters.inline { margin: 0; }
.card .filters { margin: var(--s4) 0 0; }
.inline { display: inline-flex; margin: 0; }
.filters.inline { display: flex; }
.grow { flex: 1; min-width: min(100%, 220px); }
.check { display: flex !important; align-items: center; gap: var(--s2); min-height: 28px; color: var(--text) !important; font-weight: 400 !important; font-size: var(--fs-base) !important; }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr)); gap: var(--s4); }
.form-grid input, .form-grid select, .form-grid textarea, .stack input { width: 100%; min-width: 0; }
.form-grid label:has(textarea), .form-grid .wide, .form-actions { grid-column: 1 / -1; }
.form-grid label input, .form-grid label select, .form-grid label textarea, .stack label input { font-weight: 400; font-size: var(--fs-base); }
.form-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s3) var(--s4); padding-top: var(--s2); }
.form-actions a { font-size: var(--fs-sm); }

fieldset { margin: 0; padding: var(--s3) var(--s4) var(--s4); border: 1px solid var(--line); border-radius: var(--r-lg); min-width: 0; }
legend { padding: 0 var(--s2); color: var(--muted); font-size: var(--fs-xs); font-weight: 600; }

details { margin: var(--s3) 0 0; }
summary {
  display: inline-flex;
  align-items: center;
  gap: var(--s2);
  min-height: 28px;
  color: var(--accent);
  font-weight: 550;
  cursor: pointer;
  list-style: none;
  border-radius: var(--r-sm);
}
summary::-webkit-details-marker { display: none; }
summary::before {
  content: "";
  width: 6px;
  height: 6px;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(-45deg);
}
details[open] > summary::before { transform: rotate(45deg); }
details[open] > summary { margin-bottom: var(--s3); }
summary:hover { color: var(--accent-hover); }

pre.letter {
  margin: var(--s2) 0 0;
  padding: var(--s3) var(--s4);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  white-space: pre-wrap;
  font: inherit;
  line-height: 1.6;
}
pre.token {
  margin: var(--s2) 0;
  padding: var(--s3) var(--s4);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--r-md);
  font: var(--fs-sm) / 1.5 var(--mono);
  white-space: pre-wrap;
  word-break: break-all;
  user-select: all;
}

/* ───────── 7. Таблицы ───────── */

.table-wrap {
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  margin-bottom: var(--s4);
}
@media (min-width: 1024px) {
  .table-wrap { max-height: max(360px, calc(100vh - 230px)); max-height: max(360px, calc(100dvh - 230px)); }
}

table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: var(--fs-base); font-variant-numeric: tabular-nums; }
th, td { padding: 10px var(--s4); text-align: left; vertical-align: top; border-bottom: 1px solid var(--line); }
th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-2);
  color: var(--muted);
  font-size: var(--fs-xs);
  font-weight: 600;
  letter-spacing: 0.02em;
  white-space: nowrap;
  border-bottom-color: var(--line-strong);
}
td { color: var(--text-2); transition: background-color var(--dur) var(--ease); }
td:first-child { color: var(--text); font-weight: 500; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--row-hover); }
th.num, td.num { text-align: right; }
td small { display: block; color: var(--muted); }
td .pill + small, td small.muted { display: inline; }
.clip { min-width: 200px; max-width: 420px; }

.card .table-wrap { margin: 0 calc(var(--s5) * -1); border: 0; border-radius: 0; box-shadow: none; border-top: 1px solid var(--line); }
.card .table-wrap:last-child { margin-bottom: calc(var(--s5) * -1); border-radius: 0 0 var(--r-lg) var(--r-lg); }
.card .table-wrap + :is(p, .note) { margin-top: var(--s4); }
.card .table-wrap:not(:last-child) { border-bottom: 1px solid var(--line); }
.card th:first-child, .card td:first-child { padding-left: var(--s5); }
.card th:last-child, .card td:last-child { padding-right: var(--s5); }
.card h3 + .table-wrap { margin-top: 0; }
.card > p:last-child { margin-bottom: 0; }

tr.urgency-5 td:first-child { color: var(--error); font-weight: 600; box-shadow: inset 3px 0 0 var(--error); }
tr.urgency-4 td:first-child { color: var(--warn); font-weight: 600; box-shadow: inset 3px 0 0 var(--warn); }

/* ───────── 8. Пилюли и статусы ───────── */

.pill {
  --st: var(--accent);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 1px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  background: color-mix(in srgb, var(--st) 13%, var(--surface));
  color: var(--accent);
  color: color-mix(in srgb, var(--st) 78%, var(--text));
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--st) 22%, transparent);
  font-size: var(--fs-xs);
  font-weight: 600;
  line-height: 1.6;
  white-space: nowrap;
}
.pill[class*="stage-"]::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--st); }
.pill.ok { --st: var(--ok); }
.pill.warn { --st: var(--warn); }
.pill.muted { --st: var(--muted); }

/* ───────── 9. KPI ───────── */

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 150px), 1fr)); gap: var(--s3); margin-bottom: var(--s4); }
.kpi {
  --tone: var(--accent);
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: start;
  gap: 2px var(--s2);
  padding: var(--s4);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--dur) var(--ease), border-color var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.kpi:hover { box-shadow: var(--shadow-md); border-color: var(--line-strong); transform: translateY(-1px); }
.kpi-icon {
  grid-column: 2;
  grid-row: 1;
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: var(--r-md);
  background: color-mix(in srgb, var(--tone) 12%, var(--surface));
  color: var(--tone);
}
.kpi-label { grid-column: 1; grid-row: 1; align-self: center; color: var(--muted); font-size: var(--fs-sm); font-weight: 500; }
.kpi-value { grid-column: 1 / -1; margin-top: var(--s2); font-size: 24px; font-weight: 650; line-height: 1.15; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.kpi-note { grid-column: 1 / -1; color: var(--muted); font-size: var(--fs-xs); }
.tone-ok { --tone: var(--ok); }
.tone-warn { --tone: var(--warn); }
.tone-violet { --tone: #7c4dd9; }
.tone-teal { --tone: var(--st-verified); }
.tone-pink { --tone: var(--st-invoiced); }
@media (prefers-color-scheme: dark) { .tone-violet { --tone: #b6a4ff; } }

/* ───────── 10. Воронка (полосы) ───────── */

.bars { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--s1); }
.bars li { display: grid; grid-template-columns: minmax(110px, 1.2fr) 2fr auto; align-items: center; gap: var(--s3); min-height: 30px; }
.bar-label { overflow-wrap: anywhere; font-size: var(--fs-base); }
.bar-label a { color: var(--text); text-decoration: none; }
.bar-label a:hover { color: var(--accent); }
.bar-value { min-width: 36px; text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
progress {
  width: 100%;
  height: 8px;
  appearance: none;
  -webkit-appearance: none;
  border: 0;
  border-radius: 4px;
  overflow: hidden;
  background: color-mix(in srgb, var(--st, var(--accent)) 12%, var(--surface-2));
  color: var(--st, var(--accent));
}
progress::-webkit-progress-bar { background: color-mix(in srgb, var(--st, var(--accent)) 12%, var(--surface-2)); border-radius: 4px; }
progress::-webkit-progress-value { background: var(--st, var(--accent)); border-radius: 4px; }
progress::-moz-progress-bar { background: var(--st, var(--accent)); border-radius: 4px; }

/* ───────── 11. Канбан ───────── */

.board {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(260px, 288px);
  align-items: start;
  gap: var(--s3);
  overflow-x: auto;
  padding: 2px 2px var(--s4);
  scroll-snap-type: x proximity;
}
.column {
  min-width: 0;
  padding: var(--s2);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  scroll-snap-align: start;
}
.column h2 {
  display: flex;
  align-items: center;
  gap: var(--s2);
  margin: 0;
  padding: var(--s2) var(--s2) var(--s3);
  font-size: var(--fs-sm);
  font-weight: 600;
}
.column h2::before { content: ""; flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--st, var(--accent)); }
.column h2 small { margin: 0 0 0 auto; background: var(--surface); }
.column-empty { margin: 0; padding: var(--s4) var(--s2); text-align: center; color: var(--muted); font-size: var(--fs-sm); border: 1px dashed var(--line-strong); border-radius: var(--r-md); }

.tile {
  display: grid;
  gap: 3px;
  margin-bottom: var(--s2);
  padding: 10px var(--s3);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-xs);
  color: var(--text);
  font-size: var(--fs-sm);
  text-decoration: none;
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease), border-color var(--dur) var(--ease), background-color var(--dur) var(--ease);
}
.tile:hover { color: var(--text); border-color: var(--st); box-shadow: var(--shadow-md); transform: translateY(-1px); }
.tile:active { transform: translateY(0); box-shadow: var(--shadow-xs); }
.tile strong { font-size: var(--fs-base); font-weight: 600; overflow-wrap: anywhere; }
.tile span { color: var(--muted); }
.more { display: inline-flex; align-items: center; min-height: 28px; padding: 0 var(--s2); font-size: var(--fs-sm); font-weight: 500; }

/* ───────── 12. ИИ-агенты ───────── */

.agents { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 240px), 1fr)); gap: var(--s3); margin-bottom: var(--s4); }
.agent-card {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 2px var(--s3);
  padding: var(--s4);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  color: var(--text);
  font-size: var(--fs-sm);
  text-decoration: none;
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease), border-color var(--dur) var(--ease), background-color var(--dur) var(--ease);
}
.agent-card:hover { color: var(--text); border-color: var(--accent); box-shadow: var(--shadow-md); transform: translateY(-1px); }
.agent-card:active { transform: translateY(0); }
.agent-card.active { border-color: var(--accent); background: var(--accent-soft); box-shadow: 0 0 0 1px var(--accent), var(--shadow-sm); }
.agent-icon { grid-row: 1 / span 2; display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: var(--r-md); background: var(--accent-soft); color: var(--accent); }
.agent-card.active .agent-icon { background: var(--surface); }
.agent-card strong { align-self: end; font-size: var(--fs-md); font-weight: 600; line-height: 1.2; }
.agent-card .muted { align-self: start; font: var(--fs-xs) var(--mono); }
.agent-stats { grid-column: 1 / -1; margin-top: var(--s2); padding-top: var(--s2); border-top: 1px solid var(--line); color: var(--muted); }

/* ───────── 13. Карточка лида, документы, переписка, история ───────── */

.tabs {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  gap: var(--s1);
  overflow-x: auto;
  scrollbar-width: none;
  margin: 0 0 var(--s4);
  padding: var(--s1);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-xs);
}
.tabs::-webkit-scrollbar { display: none; }
.tabs a {
  flex: none;
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 0 var(--s3);
  border-radius: var(--r-md);
  color: var(--muted);
  font-size: var(--fs-sm);
  font-weight: 550;
  text-decoration: none;
}
.tabs a:hover { background: var(--accent-soft); color: var(--accent); }
.top ~ main .tabs { top: 0; }
@media (max-width: 1023.98px) { .tabs { position: static; } }

.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 190px), 1fr)); gap: var(--s3); margin: 0 0 var(--s4); }
.facts div { min-width: 0; padding: var(--s3) var(--s4); background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); box-shadow: var(--shadow-xs); }
.facts dt { color: var(--muted); font-size: var(--fs-xs); font-weight: 600; }
.facts dd { margin: 2px 0 0; overflow-wrap: anywhere; }

.request { margin-bottom: var(--s4); padding-bottom: var(--s4); border-bottom: 1px solid var(--line); }
.request:last-child { margin-bottom: 0; padding-bottom: 0; border-bottom: 0; }
.request .facts { margin-bottom: var(--s3); }

.doc {
  margin-bottom: var(--s4);
  padding: var(--s6);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
  overflow-wrap: anywhere;
  line-height: 1.65;
}
.doc > header { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: var(--s2) var(--s4); margin-bottom: var(--s4); padding-bottom: var(--s3); border-bottom: 1px solid var(--line); }
.doc > header h1, .doc > header h2 { margin: 0; }
.doc > header .muted { font-size: var(--fs-sm); }
.doc :is(h1, h2, h3, h4) { margin: var(--s5) 0 var(--s2); }
.doc > header + :is(h1, h2, h3, h4) { margin-top: 0; }
.doc h1 { font-size: var(--fs-lg); }
.doc h2 { font-size: var(--fs-md); }
.doc :is(p, ul, ol) { margin: 0 0 var(--s3); }
.doc :is(p, li) { max-width: 80ch; }
.doc table { display: block; overflow-x: auto; margin: 0 0 var(--s3); }
.doc pre { overflow-x: auto; margin: 0 0 var(--s3); padding: var(--s3) var(--s4); background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--r-md); }
.doc blockquote { margin: 0 0 var(--s3); padding-left: var(--s4); border-left: 3px solid var(--line-strong); color: var(--muted); }
.doc hr { border: 0; border-top: 1px solid var(--line); margin: var(--s5) 0; }
.img-alt { color: var(--muted); }

.timeline, .thread { list-style: none; margin: var(--s4) 0 0; padding: 0; display: grid; gap: var(--s3); }
.timeline li { position: relative; padding-left: var(--s5); font-size: var(--fs-base); }
.timeline li::before { content: ""; position: absolute; left: 0; top: 0.5em; width: 8px; height: 8px; border-radius: 50%; background: var(--surface); border: 2px solid var(--line-strong); }
.timeline li::after { content: ""; position: absolute; left: 3px; top: calc(0.5em + 10px); bottom: calc(var(--s3) * -1 + 2px); width: 2px; background: var(--line); }
.timeline li:last-child::after { display: none; }
.timeline time { margin-right: var(--s2); color: var(--muted); font-size: var(--fs-sm); font-variant-numeric: tabular-nums; }
.card > .timeline:first-child { margin-top: 0; }

.thread li { padding: var(--s3) var(--s4); background: var(--surface); border: 1px solid var(--line); border-left-width: 3px; border-radius: var(--r-md); }
.thread li.in { border-left-color: var(--ok); background: color-mix(in srgb, var(--ok) 5%, var(--surface)); }
.thread li.out { border-left-color: var(--accent); }
.thread .meta { margin-bottom: var(--s1); color: var(--muted); font-size: var(--fs-xs); }
.thread pre.letter { margin-top: var(--s2); }

/* ───────── 14. Сообщения и пустые состояния ───────── */

.flash {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin: 0 0 var(--s4);
  padding: var(--s3) var(--s4);
  border: 1px solid transparent;
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-xs);
  color: var(--text);
  line-height: 1.5;
}
.flash > svg { margin-top: 2px; }
.flash > span { min-width: 0; }
.flash p { margin: var(--s1) 0; }
.flash-ok { background: var(--ok-soft); border-color: color-mix(in srgb, var(--ok) 35%, transparent); }
.flash-ok > svg { color: var(--ok); }
.flash-error { background: var(--error-soft); border-color: color-mix(in srgb, var(--error) 35%, transparent); }
.flash-error > svg { color: var(--error); }
div.flash { display: block; }

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--s2);
  margin: 0 0 var(--s4);
  padding: var(--s8) var(--s4);
  text-align: center;
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--line-strong);
  border-radius: var(--r-lg);
}
.empty .ico { color: var(--muted); opacity: 0.8; }
.card .empty { background: var(--surface-2); margin-bottom: 0; }

/* ───────── 15. Вход, регистрация, ошибки ───────── */

.narrow { width: 100%; max-width: 420px; margin: var(--s10) auto; }
.wide-form { max-width: 720px; }
.narrow h1 { font-size: var(--fs-xl); margin-bottom: var(--s4); }
.narrow > p { margin: 0 0 var(--s3); }
.auth-brand { display: flex; align-items: center; gap: 10px; margin-bottom: var(--s5); font-weight: 650; letter-spacing: -0.01em; }
.auth-brand .brand-mark { width: 36px; height: 36px; }
.auth-icon { display: inline-grid; place-items: center; width: 44px; height: 44px; margin-bottom: var(--s3); border-radius: 50%; background: var(--ok-soft); color: var(--ok); }

body.auth main { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 100vh; min-height: 100dvh; padding: var(--s6) var(--s4); }
body.auth .narrow { margin: 0; box-shadow: var(--shadow-md); padding: var(--s8); }
body.auth .flash { width: 100%; max-width: 420px; }
body.auth { background: radial-gradient(1200px 500px at 50% -10%, color-mix(in srgb, var(--accent) 10%, transparent), transparent 70%), var(--bg); }
body.auth .narrow .stack button, body.auth .form-actions button { min-height: 40px; }
body.auth .stack button { width: 100%; }

/* ───────── 16. Motion ───────── */

@media (prefers-reduced-motion: no-preference) {
  html { scroll-behavior: smooth; }
}

/* переходы между страницами (cross-document View Transitions) */
@view-transition { navigation: auto; }

::view-transition-group(root) { animation-duration: 0s; }
::view-transition-old(root), ::view-transition-new(root) { animation: none; mix-blend-mode: normal; }
::view-transition-group(chrome) { animation-duration: 0.2s; animation-timing-function: var(--ease); }
::view-transition-old(chrome), ::view-transition-new(chrome) { mix-blend-mode: normal; }
::view-transition-group(page) { animation-duration: 0s; }
::view-transition-old(page) { animation: vt-out 0.12s var(--ease) both; mix-blend-mode: normal; }
::view-transition-new(page) { animation: vt-in 0.22s var(--ease) both; mix-blend-mode: normal; }

@keyframes vt-out { to { opacity: 0; transform: translateY(-3px); } }
@keyframes vt-in { from { opacity: 0; transform: translateY(8px); } }

/* появление карточек, плиток и строк; задержка = --i * 35 мс (не больше ~0.3 с суммарно) */
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }
@keyframes reveal { from { clip-path: inset(0 100% 0 0 round 4px); } to { clip-path: inset(0 0 0 0 round 4px); } }

.kpi, .grid > .card, main > .card, main > .doc, .column, .agent-card, .flash, .tabs, .filters, tbody tr:nth-child(-n + 14) {
  animation: rise 0.32s var(--ease) backwards;
  animation-delay: calc(var(--i, 0) * 35ms);
}
.tile { animation: rise 0.3s var(--ease) backwards; animation-delay: calc(var(--i, 0) * 35ms + 60ms); }
.bars progress { animation: reveal 0.5s var(--ease) backwards; animation-delay: calc(var(--i, 0) * 40ms + 100ms); }

:is(.kpis, .grid, .agents, .board) > :nth-child(1), tbody tr:nth-child(1), .bars li:nth-child(1) { --i: 0; }
:is(.kpis, .grid, .agents, .board) > :nth-child(2), tbody tr:nth-child(2), .bars li:nth-child(2) { --i: 1; }
:is(.kpis, .grid, .agents, .board) > :nth-child(3), tbody tr:nth-child(3), .bars li:nth-child(3) { --i: 2; }
:is(.kpis, .grid, .agents, .board) > :nth-child(4), tbody tr:nth-child(4), .bars li:nth-child(4) { --i: 3; }
:is(.kpis, .grid, .agents, .board) > :nth-child(5), tbody tr:nth-child(5), .bars li:nth-child(5) { --i: 4; }
:is(.kpis, .grid, .agents, .board) > :nth-child(6), tbody tr:nth-child(6), .bars li:nth-child(6) { --i: 5; }
:is(.kpis, .grid, .agents, .board) > :nth-child(7), tbody tr:nth-child(7), .bars li:nth-child(7) { --i: 6; }
:is(.kpis, .grid, .agents, .board) > :nth-child(n + 8), tbody tr:nth-child(n + 8), .bars li:nth-child(n + 8) { --i: 7; }

/* ───────── 17. Адаптив ───────── */

@media (max-width: 640px) {
  main { padding: var(--s4) var(--s4) var(--s8); }
  h1 { font-size: 20px; }
  .card { padding: var(--s4); }
  .card .table-wrap { margin-left: calc(var(--s4) * -1); margin-right: calc(var(--s4) * -1); }
  .card .table-wrap:last-child { margin-bottom: calc(var(--s4) * -1); }
  .card th:first-child, .card td:first-child { padding-left: var(--s4); }
  .card th:last-child, .card td:last-child { padding-right: var(--s4); }
  .doc { padding: var(--s4); }
  .board { grid-auto-columns: 82%; }
  .page-head .filters { width: 100%; }
  .filters { padding: var(--s3); }
  .filters label { flex: 1 1 140px; }
  .bars li { grid-template-columns: minmax(0, 1fr) auto; row-gap: 2px; }
  .bars progress { grid-column: 1 / -1; grid-row: 2; }
  .kpi { padding: var(--s3); }
  .kpi-value { font-size: 20px; }
  body.auth .narrow { padding: var(--s5); }
  /* 16px в полях — iOS не увеличивает страницу при фокусе */
  input, select, textarea { font-size: 16px; }
}

@media (pointer: coarse) {
  input:not([type="checkbox"]), select, button, a.button { min-height: 44px; }
  .top nav a { min-height: 44px; }
  .tabs a { min-height: 40px; }
  button.ghost { min-height: 40px; }
}

/* ───────── 18. Reduced motion ───────── */

@media (prefers-reduced-motion: reduce) {
  @view-transition { navigation: none; }

  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
  ::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none !important; }
  .tile:hover, .agent-card:hover, .kpi:hover, button:active, a.button:active { transform: none; }
}

/* Канбан занимает всю ширину рабочей области: колонок много, их нужно видеть сразу */
main:has(> .board) { max-width: none; }

/* ───────── Ниши ───────── */

.niches { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr)); gap: var(--s3); }
.niche-card {
  display: grid;
  gap: var(--s2);
  padding: var(--s4);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-lg, 12px);
  box-shadow: var(--shadow-sm, none);
  color: var(--text);
  text-decoration: none;
  animation: rise 0.32s var(--ease) backwards;
  transition: border-color 0.18s var(--ease), box-shadow 0.18s var(--ease), transform 0.18s var(--ease);
}
.niche-card:hover { border-color: var(--accent); box-shadow: var(--shadow-md, none); transform: translateY(-1px); }
.niche-head { display: flex; justify-content: space-between; align-items: center; gap: var(--s2); }
.niche-head strong { font-size: var(--fs-md, 16px); overflow-wrap: anywhere; }
.niche-stats { display: flex; flex-wrap: wrap; gap: 4px 14px; color: var(--muted); font-size: var(--fs-sm); }
.niche-stats b { color: var(--text); font-variant-numeric: tabular-nums; }
.niche-paid { color: var(--ok); font-weight: 600; font-size: var(--fs-sm); }
.niches > :nth-child(2) { animation-delay: 35ms; }
.niches > :nth-child(3) { animation-delay: 70ms; }
.niches > :nth-child(4) { animation-delay: 105ms; }
.niches > :nth-child(n + 5) { animation-delay: 140ms; }

.doc-marks { white-space: nowrap; }
.doc-mark {
  display: inline-block;
  margin: 0 4px 2px 0;
  padding: 0 6px;
  border-radius: 6px;
  font-size: 12px;
  color: var(--muted);
  background: var(--surface-2);
  text-decoration: line-through;
  text-decoration-color: color-mix(in srgb, var(--muted) 50%, transparent);
}
.doc-mark.on { color: var(--ok); background: var(--ok-soft); text-decoration: none; }

@media (prefers-reduced-motion: reduce) {
  .niche-card { animation: none; transition: none; }
  .niche-card:hover { transform: none; }
}

/* ───────── Адаптивные таблицы ─────────
   Документы агентов: таблица прокручивается внутри, слова не рвутся по буквам. */
.doc table { width: max-content; max-width: 100%; }
.doc th, .doc td { overflow-wrap: normal; word-break: normal; min-width: 7.5em; max-width: 28em; }

/* Узкий экран: строка таблицы — карточка «подпись: значение» (подписи проставляет сервер). */
@media (max-width: 640px) {
  .table-wrap { max-height: none; overflow: visible; border: 0; background: transparent; box-shadow: none; }
  .table-wrap table, .doc table, .card > table { display: block; width: 100%; min-width: 0; max-width: 100%; }
  .table-wrap thead, .doc thead, .card > table thead {
    position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
  }
  .table-wrap tbody, .doc tbody, .card > table tbody { display: grid; gap: var(--s2); }
  .table-wrap tr, .doc tbody tr, .card > table tr {
    display: grid; gap: 6px; padding: var(--s3);
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-md, 8px);
  }
  .table-wrap td, .doc td, .card > table td {
    display: grid; grid-template-columns: minmax(84px, 38%) minmax(0, 1fr); gap: var(--s2); align-items: start;
    padding: 0; border: 0; min-width: 0; max-width: none; text-align: left; white-space: normal;
    overflow-wrap: break-word; word-break: normal;
  }
  .table-wrap td[data-label]::before, .doc td[data-label]::before, .card > table td[data-label]::before {
    content: attr(data-label); color: var(--muted); font-size: var(--fs-sm); font-weight: 500;
  }
  .table-wrap td:not([data-label]), .doc td:not([data-label]) { grid-template-columns: 1fr; }
  .table-wrap td.num, .card > table td.num { text-align: left; }
  .table-wrap tr:hover { background: var(--surface); }
}
