/* Дашборд аналитики Софии. Раскладка: одна колонка карточек-блоков с шапкой,
   строкой фильтров и строкой показателей наверху; графики — два на ряд.
   Цвета — токены палитры dataviz (проверены валидатором в обеих темах). */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --ring: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --s1: #2a78d6;        /* активные пользователи */
  --s2: #eb6834;        /* открытия чата */
  --ramp-1: #86b6ef;    /* ординальная шкала воронки, светлый конец */
  --ramp-2: #5598e7;
  --ramp-3: #2a78d6;
  --ramp-4: #1c5cab;
  --ramp-5: #104281;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --ring: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --s1: #3987e5;
    --s2: #d95926;
    --ramp-1: #6da7ec;
    --ramp-2: #3987e5;
    --ramp-3: #256abf;
    --ramp-4: #184f95;
    --ramp-5: #184f95;
    --good-text: #0ca30c;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --ring: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --s1: #3987e5;
  --s2: #d95926;
  --ramp-1: #6da7ec;
  --ramp-2: #3987e5;
  --ramp-3: #256abf;
  --ramp-4: #184f95;
  --ramp-5: #184f95;
  --good-text: #0ca30c;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 14px/1.45 var(--font);
  background: var(--page);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; text-wrap: balance; font-weight: 600; }
h1 { font-size: 1.25rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: 0.95rem; margin-top: 1.5rem; margin-bottom: .5rem; }
.muted { color: var(--muted); }
.eyebrow { font-size: .72rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.error { color: var(--critical); }
.error.banner { padding: .6rem .9rem; border: 1px solid var(--critical); border-radius: var(--radius); background: var(--surface); }
a { color: var(--accent); }

input, select, button { font: inherit; color: inherit; }
input, select {
  background: var(--surface); border: 1px solid var(--axis); border-radius: 8px;
  padding: .45rem .65rem; min-width: 0;
}
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button {
  background: var(--surface); border: 1px solid var(--axis); border-radius: 8px;
  padding: .45rem .8rem; cursor: pointer; color: var(--ink);
}
button:hover { border-color: var(--ink-2); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
button.primary:hover { filter: brightness(1.08); }
button.ghost { background: none; border-color: transparent; color: var(--ink-2); }
button.ghost:hover { color: var(--ink); background: var(--surface); }
button.small { padding: .25rem .6rem; font-size: .85em; }
button.selected { background: var(--ink); color: var(--page); border-color: var(--ink); }
button[disabled] { opacity: .5; cursor: default; }

/* Вход */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-card {
  width: min(360px, 100%); display: flex; flex-direction: column; gap: .5rem;
  background: var(--surface); border: 1px solid var(--ring); border-radius: var(--radius); padding: 1.5rem;
}
.login-card h1 { margin-bottom: .25rem; }
.login-card p { margin: 0 0 .5rem; }
.login-card label { font-size: .85rem; color: var(--ink-2); }
.login-card button { margin-top: .5rem; }

/* Каркас */
.app {
  max-width: 1200px; margin: 0 auto;
  padding-block: 1rem 3rem;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: .75rem 1.5rem;
  padding-bottom: .75rem; border-bottom: 1px solid var(--grid); margin-bottom: 1rem;
}
.brand { flex: 1 1 auto; min-width: 0; }
.views { display: flex; gap: .25rem; flex-wrap: wrap; }
.view-btn { border-color: transparent; background: none; color: var(--ink-2); }
.view-btn.active { background: var(--surface); color: var(--ink); border-color: var(--ring); }
.session { display: flex; align-items: center; gap: .25rem; }
.view { display: flex; flex-direction: column; gap: 1.25rem; }

/* Фильтры: одна строка над всем, что они задают */
.filters { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem 1rem; }
.presets { display: flex; flex-wrap: wrap; gap: .25rem; }
.range { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; }
.range label { display: inline-flex; align-items: center; gap: .4rem; color: var(--ink-2); }
.hint { flex-basis: 100%; font-size: .85rem; }

/* Пометки полноты */
.flags { display: flex; flex-wrap: wrap; gap: .4rem; min-height: 1.6rem; }
.flags:empty { display: none; }
.chip {
  display: inline-flex; align-items: center; gap: .4rem;
  border: 1px solid var(--ring); border-radius: 999px; padding: .2rem .7rem .2rem .5rem;
  background: var(--surface); font-size: .85rem;
}
.chip i { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.chip.ok i { background: var(--good); }
.chip.warn i { background: var(--warning); }
.chip.serious i { background: var(--serious); }
.chip.info i { background: var(--accent); }

/* Плитки показателей */
.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: .75rem; }
.tile {
  background: var(--surface); border: 1px solid var(--ring); border-radius: var(--radius);
  padding: .8rem .95rem; display: flex; flex-direction: column; gap: .15rem; min-width: 0;
}
.tile .label { font-size: .82rem; color: var(--ink-2); }
.tile .value { font-size: 1.75rem; font-weight: 600; line-height: 1.15; }
.tile .sub { font-size: .78rem; color: var(--muted); }
.tile-row { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.tiles-col { display: flex; flex-direction: column; gap: .75rem; }

/* Блоки */
.block { background: var(--surface); border: 1px solid var(--ring); border-radius: var(--radius); padding: 1rem 1.1rem 1.1rem; }
.block-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .5rem; margin-bottom: .75rem; }
.block-tools { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.num { color: var(--muted); font-weight: 500; margin-right: .35rem; font-variant-numeric: tabular-nums; }
.charts-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.funnel-grid { display: grid; grid-template-columns: minmax(0, 3fr) minmax(220px, 2fr); gap: 1rem; }
figure.chart { margin: 0; min-width: 0; }
figcaption { font-size: .85rem; color: var(--ink-2); margin-bottom: .4rem; }
.plot { position: relative; width: 100%; }
.plot svg { display: block; width: 100%; height: auto; overflow: visible; }
.plot.loading { opacity: .5; }
.legend { display: inline-flex; gap: 1rem; font-size: .82rem; color: var(--ink-2); }
.key { display: inline-flex; align-items: center; gap: .4rem; }
.swatch { display: inline-block; }
.swatch.line { width: 16px; height: 2px; border-radius: 1px; }
.swatch.rect { width: 10px; height: 10px; border-radius: 2px; }
.swatch.s1 { background: var(--s1); }
.swatch.s2 { background: var(--s2); }
.foot { font-size: .8rem; max-width: 70ch; }

/* Графики (SVG) */
.gridline { stroke: var(--grid); stroke-width: 1; }
.baseline { stroke: var(--axis); stroke-width: 1; }
.tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.line { fill: none; stroke: var(--s1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.area { fill: var(--s1); opacity: .10; }
.dot { fill: var(--s1); stroke: var(--surface); stroke-width: 2; }
.bar { fill: var(--s2); }
.bar.hover, .fbar.hover { filter: brightness(1.15); }
.hit { fill: transparent; }
.crosshair { stroke: var(--axis); stroke-width: 1; }
.end-label { fill: var(--ink); font-size: 11px; font-weight: 600; }
.flabel { fill: var(--ink-2); font-size: 12px; }
.fvalue { fill: var(--ink); font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
.fpct { fill: var(--muted); font-size: 11px; }
.empty { fill: var(--muted); font-size: 12px; }

/* Подсказка */
.tooltip {
  position: fixed; z-index: 20; pointer-events: none;
  background: var(--surface); color: var(--ink); border: 1px solid var(--ring);
  border-radius: 8px; padding: .4rem .6rem; font-size: .82rem; box-shadow: 0 4px 14px rgba(0,0,0,.12);
  max-width: 260px;
}
.tooltip .t-head { color: var(--ink-2); margin-bottom: .2rem; }
.tooltip .t-row { display: flex; align-items: center; gap: .4rem; }
.tooltip .t-key { width: 12px; height: 2px; }
.tooltip .t-val { font-weight: 600; font-variant-numeric: tabular-nums; }

/* Таблицы */
.table-wrap { overflow-x: auto; margin-top: .75rem; }
table { border-collapse: collapse; width: 100%; font-size: .88rem; }
th, td { text-align: left; padding: .4rem .6rem; border-bottom: 1px solid var(--grid); vertical-align: top; }
th { color: var(--ink-2); font-weight: 500; white-space: nowrap; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--page); }
.state { display: inline-flex; align-items: center; gap: .35rem; white-space: nowrap; }
.state i { width: 8px; height: 8px; border-radius: 50%; }
.state.ok i { background: var(--good); }
.state.warn i { background: var(--warning); }
.state.bad i { background: var(--critical); }
.state.muted i { background: var(--muted); }

/* Формы админки */
.stack-form { display: flex; flex-direction: column; gap: .35rem; max-width: 520px; }
.stack-form.narrow { max-width: 400px; }
.stack-form label { font-size: .85rem; color: var(--ink-2); margin-top: .35rem; }
.row { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; margin-top: .5rem; }
.inline-actions { display: flex; gap: .3rem; flex-wrap: wrap; }
.block-head .row { margin-top: 0; }

/* Модальное окно: отступы у формы, чтобы клик по самому dialog означал подложку */
.modal {
  padding: 0; width: min(440px, calc(100vw - 32px));
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--ring); border-radius: var(--radius);
}
.modal::backdrop { background: rgba(0, 0, 0, .45); }
.modal .stack-form { padding: 1.25rem 1.25rem 1.1rem; max-width: none; }
.modal h3 { margin-top: 0; }
.modal-actions { justify-content: flex-end; }
#user-create-result:empty { display: none; }
#user-create-result { color: var(--critical); }

@media (max-width: 900px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .charts-2, .funnel-grid { grid-template-columns: 1fr; }
}
@media (max-width: 480px) {
  .kpis { grid-template-columns: 1fr; }
  .tile-row { grid-template-columns: 1fr; }
  .tile .value { font-size: 1.5rem; }
}
@media (prefers-reduced-motion: no-preference) {
  .plot { transition: opacity .15s; }
}
