/* ---------------------------------------------------------------------------
   Токены. Светлая тема — база на голом :root, тёмная объявлена дважды:
   через prefers-color-scheme (системная настройка) и через [data-theme]
   (переключатель в шапке), чтобы переключатель выигрывал в обе стороны.

   Тональность — расходящаяся шкала: негатив и позитив как полюса, нейтрально
   как серая середина. Пара «зелёный–красный» здесь не годится: при дейтеранопии
   ΔE между #0ca30c и #d03b3b — 4,1 при пороге 8, то есть для части читателей
   график становится одноцветным. Взятые оттенки проверены валидатором
   в обеих темах: ΔE 9,9 (дейтеран) между полюсами и 8,5 до серединного серого.
   --------------------------------------------------------------------------- */
:root {
  color-scheme: light;

  --plane:          #f9f9f7;
  --surface:        #fcfcfb;
  --surface-sunken: #f2f1ed;

  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;

  --grid:           #e1e0d9;
  --baseline:       #c3c2b7;
  --border:         rgba(11, 11, 11, .10);
  --hover:          rgba(11, 11, 11, .04);

  /* Тональность. Одни и те же значения в обеих темах: они валидировались
     против светлой и тёмной поверхности разом. */
  --tone-positive:  #1baf7a;
  --tone-neutral:   #7d7b75;
  --tone-negative:  #d03b3b;

  --accent:         #2a78d6;  /* бренд в фокусе на сравнительных графиках */
  --muted-bar:      #c3c2b7;  /* остальные бренды — фон, а не участники */

  --status-warning: #fab219;
  --delta-up:       #006300;
  --delta-down:     #c62f2f;

  /* Последовательная шкала для тепловой карты: светлее → темнее = больше */
  --h0: #cde2fb; --h1: #9ec5f4; --h2: #6da7ec; --h3: #3987e5;
  --h4: #256abf; --h5: #184f95; --h6: #0d366b;
  --h-ink-lo: #0b0b0b;   /* подпись на светлых шагах 0–3 */
  --h-ink-hi: #ffffff;   /* подпись на тёмных шагах 4–6 */

  --radius:   12px;
  --radius-s: 8px;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --plane:          #0d0d0d;
    --surface:        #1a1a19;
    --surface-sunken: #131312;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --grid:           #2c2c2a;
    --baseline:       #383835;
    --border:         rgba(255, 255, 255, .10);
    --hover:          rgba(255, 255, 255, .05);
    --accent:         #3987e5;
    --muted-bar:      #55544f;
    --delta-up:       #0ca30c;
    --delta-down:     #ef8080;
    /* На тёмном фоне «больше» = светлее: та же голубая шкала, развёрнутая */
    --h0: #0d366b; --h1: #184f95; --h2: #256abf; --h3: #3987e5;
    --h4: #6da7ec; --h5: #9ec5f4; --h6: #cde2fb;
    --h-ink-lo: #ffffff;
    --h-ink-hi: #0b0b0b;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --plane:          #0d0d0d;
  --surface:        #1a1a19;
  --surface-sunken: #131312;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --grid:           #2c2c2a;
  --baseline:       #383835;
  --border:         rgba(255, 255, 255, .10);
  --hover:          rgba(255, 255, 255, .05);
  --accent:         #3987e5;
  --muted-bar:      #55544f;
  --delta-up:       #0ca30c;
  --delta-down:     #ef8080;
  --h0: #0d366b; --h1: #184f95; --h2: #256abf; --h3: #3987e5;
  --h4: #6da7ec; --h5: #9ec5f4; --h6: #cde2fb;
  --h-ink-lo: #ffffff;
  --h-ink-hi: #0b0b0b;
}

/* --------------------------------------------------------------------------- */

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0 clamp(12px, 3vw, 32px) 48px;
  background: var(--plane);
  color: var(--text-primary);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

h1, h2 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
h1 { font-size: 20px; }
h2 { font-size: 15px; }
p  { margin: 0; }

a { color: inherit; text-decoration-color: var(--baseline); text-underline-offset: 2px; }
a:hover { text-decoration-color: currentColor; }

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

.skip-link {
  position: absolute; left: -9999px; top: 0; z-index: 20;
  padding: 8px 12px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-s);
}
.skip-link:focus { left: 12px; top: 12px; }

/* --- Шапка --------------------------------------------------------------- */

.topbar {
  display: flex; flex-wrap: wrap; gap: 12px 16px;
  align-items: flex-end; justify-content: space-between;
  padding: 24px 0 16px;
}
.topbar__sub { color: var(--text-secondary); font-size: 13px; margin-top: 2px; }
.topbar__actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.status { color: var(--text-muted); font-size: 12px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 32px; padding: 0 12px;
  font: inherit; font-size: 13px; color: var(--text-primary);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-s); cursor: pointer;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: .55; cursor: progress; }
.btn--icon { padding: 0 9px; }
.btn--ghost { background: transparent; }

.banner {
  display: flex; gap: 8px; align-items: flex-start;
  margin-bottom: 16px; padding: 10px 12px;
  font-size: 13px; color: var(--text-secondary);
  background: var(--surface); border: 1px solid var(--border);
  border-left: 3px solid var(--status-warning);
  border-radius: var(--radius-s);
}
.banner[hidden] { display: none; }

/* --- Фильтры ------------------------------------------------------------- */

.filters {
  display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: flex-end;
  margin-bottom: 16px;
}
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field__label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }

.field select {
  min-height: 34px; width: 100%; padding: 0 6px 0 10px;
  font: inherit; font-size: 13px; color: var(--text-primary);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-s);
}

/* --- Карточки ------------------------------------------------------------ */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.card__head { margin-bottom: 14px; }
.card__head--row { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.card__sub { margin-top: 3px; font-size: 12px; color: var(--text-muted); }

.kpis {
  display: grid; gap: 12px; margin-bottom: 16px;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}
.kpi__label { font-size: 12px; color: var(--text-secondary); }
.kpi__value { margin-top: 4px; font-size: 26px; font-weight: 600; letter-spacing: -.02em; }
.kpi__value--hero { font-size: 48px; line-height: 1.05; }
.kpi__hint { margin-top: 2px; font-size: 12px; color: var(--text-muted); }
.kpi--hero { grid-column: span 2; }
@media (max-width: 520px) { .kpi--hero { grid-column: span 1; } }

.delta--up   { color: var(--delta-up); }
.delta--down { color: var(--delta-down); }
.delta__arrow { font-size: .8em; margin-right: 2px; }

.grid {
  display: grid; gap: 12px; margin-bottom: 16px;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
}
.card--wide { grid-column: 1 / -1; }

.chart__body--scroll { overflow-x: auto; }
.chart__plot { position: relative; }
.chart__plot svg { display: block; width: 100%; height: auto; }

.empty { padding: 24px 0; text-align: center; color: var(--text-muted); font-size: 13px; }

/* Перезагрузка данных не роняет кадр: старый рендер держится приглушённым */
main.is-busy { opacity: .55; }

/* --- Колонки (SVG) ------------------------------------------------------- */

.col-chart:focus { outline: none; }
.col-chart:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.col-chart__grid { stroke: var(--grid); stroke-width: 1; }
.col-chart__base { stroke: var(--baseline); stroke-width: 1; }
.col-chart__tick { fill: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.col-chart__xlabel { fill: var(--text-muted); font-size: 11px; }
.col-chart__value { fill: var(--text-primary); font-size: 11px; font-weight: 600; }
.col-chart__band { fill: transparent; }
.col-chart__band.is-active { fill: var(--hover); }
.col-chart__hit { fill: transparent; cursor: default; }
.col-chart__seg--positive { fill: var(--tone-positive); }
.col-chart__seg--neutral  { fill: var(--tone-neutral); }
.col-chart__seg--negative { fill: var(--tone-negative); }

/* --- Стопка «часть-целое» ------------------------------------------------ */

.split { display: flex; gap: 2px; height: 28px; margin-bottom: 14px; }
.split__seg { min-width: 3px; border-radius: 2px; }
.split__seg:first-child { border-radius: 4px 2px 2px 4px; }
.split__seg:last-child  { border-radius: 2px 4px 4px 2px; }
.split__seg--positive { background: var(--tone-positive); }
.split__seg--neutral  { background: var(--tone-neutral); }
.split__seg--negative { background: var(--tone-negative); }

.legend { display: flex; flex-wrap: wrap; gap: 8px 18px; }
.legend__item { display: flex; align-items: baseline; gap: 7px; font-size: 12px; color: var(--text-secondary); }
.legend__swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; transform: translateY(1px); }
.legend__swatch--positive { background: var(--tone-positive); }
.legend__swatch--neutral  { background: var(--tone-neutral); }
.legend__swatch--negative { background: var(--tone-negative); }
.legend__swatch--accent   { background: var(--accent); }
.legend__swatch--muted    { background: var(--muted-bar); }
.legend__value { color: var(--text-primary); font-variant-numeric: tabular-nums; font-weight: 600; }
.legend--top { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--grid); }

.legend--scale { margin-top: 14px; align-items: center; gap: 6px; }
.legend--scale .legend__caption { font-size: 11px; color: var(--text-muted); margin-right: 2px; }
.scale-step { display: flex; align-items: center; gap: 5px; font-size: 11px; color: var(--text-muted); }
.scale-step__chip { width: 18px; height: 10px; border-radius: 2px; border: 1px solid var(--border); }

/* --- Горизонтальные бары по брендам -------------------------------------- */

.bars { display: flex; flex-direction: column; }

.bar-row {
  display: grid;
  grid-template-columns: minmax(86px, 132px) 1fr minmax(58px, auto);
  align-items: center; gap: 10px;
  min-height: 28px; padding: 0 4px; margin: 0 -4px;
  border-radius: 4px;
}
.bar-row:hover { background: var(--hover); }
.bar-row__label {
  display: flex; align-items: center; gap: 6px; min-width: 0;
  font-size: 13px; color: var(--text-secondary);
}
.bar-row__labeltext { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-row--focus .bar-row__labeltext { color: var(--text-primary); font-weight: 600; }
.bar-row__track {
  position: relative; height: 16px;
  background: var(--surface-sunken); border-radius: 0 4px 4px 0;
}
.bar-row__fill {
  height: 100%; min-width: 2px;
  background: var(--muted-bar);
  border-radius: 0 4px 4px 0;
}
.bar-row--focus .bar-row__fill { background: var(--accent); }
.bar-row__value {
  font-size: 13px; color: var(--text-primary);
  font-variant-numeric: tabular-nums; text-align: right;
}
.bar-row--absent .bar-row__value { color: var(--text-muted); }

/* --- Парные бары «период к периоду» -------------------------------------- */

.pairs { display: flex; flex-direction: column; gap: 12px; }
.pair {
  display: grid;
  grid-template-columns: minmax(90px, 150px) 1fr minmax(76px, auto);
  gap: 2px 10px; align-items: center;
}
/* Позиции проставлены явно: у двух дорожек одна колонка и разные строки,
   на автопотоке они разъезжаются по соседним колонкам. */
.pair__label { grid-column: 1; grid-row: 1 / 3; display: flex; align-items: center; gap: 6px; min-width: 0; font-size: 13px; color: var(--text-secondary); }
.pair__labeltext { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pair__track { grid-column: 2; position: relative; height: 12px; background: var(--surface-sunken); border-radius: 0 3px 3px 0; }
.pair__track--now { grid-row: 1; }
.pair__track--was { grid-row: 2; }
.pair__fill { height: 100%; min-width: 2px; border-radius: 0 3px 3px 0; }
.pair__fill--was { background: var(--muted-bar); }
.pair__fill--now.tone--positive { background: var(--tone-positive); }
.pair__fill--now.tone--neutral  { background: var(--tone-neutral); }
.pair__fill--now.tone--negative { background: var(--tone-negative); }
.pair__value { grid-column: 3; grid-row: 1 / 3; text-align: right; font-size: 13px; font-variant-numeric: tabular-nums; }
.pair__delta { display: block; font-size: 12px; font-weight: 600; }
.pair:hover { background: var(--hover); border-radius: 4px; }

/* --- Тепловая карта ------------------------------------------------------ */

.heat { border-collapse: separate; border-spacing: 2px; font-size: 12px; }
.heat th {
  font-weight: 500; color: var(--text-secondary); text-align: left;
  padding: 4px 6px; white-space: nowrap;
}
.heat thead th { font-size: 11px; color: var(--text-muted); text-align: center; vertical-align: bottom; }
.heat tbody th { position: sticky; left: 0; background: var(--surface); z-index: 1; }
.heat tbody tr.is-focus th { color: var(--text-primary); font-weight: 600; }
.heat td {
  width: 64px; height: 30px; padding: 0;
  text-align: center; vertical-align: middle;
  font-variant-numeric: tabular-nums;
  border-radius: 3px;
}
.heat td.is-empty { background: transparent; box-shadow: inset 0 0 0 1px var(--grid); color: var(--text-muted); }
.heat td.l0 { background: var(--h0); color: var(--h-ink-lo); }
.heat td.l1 { background: var(--h1); color: var(--h-ink-lo); }
.heat td.l2 { background: var(--h2); color: var(--h-ink-lo); }
.heat td.l3 { background: var(--h3); color: var(--h-ink-lo); }
.heat td.l4 { background: var(--h4); color: var(--h-ink-hi); }
.heat td.l5 { background: var(--h5); color: var(--h-ink-hi); }
.heat td.l6 { background: var(--h6); color: var(--h-ink-hi); }
.heat td.total { background: var(--surface-sunken); color: var(--text-primary); font-weight: 600; }
.heat td:hover, .heat td:focus-visible { box-shadow: inset 0 0 0 2px var(--text-primary); }

/* --- Таблица ------------------------------------------------------------- */

.table-card { padding-bottom: 8px; }
.table-wrap { max-height: 560px; overflow: auto; border-radius: var(--radius-s); }

.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td {
  padding: 8px 10px; text-align: left; vertical-align: top;
  border-bottom: 1px solid var(--grid);
}
.data-table thead th {
  position: sticky; top: 0; z-index: 2;
  background: var(--surface); color: var(--text-secondary);
  font-weight: 500; font-size: 12px; white-space: nowrap;
  cursor: pointer; user-select: none;
  border-bottom: 1px solid var(--baseline);
}
.data-table thead th::after { content: ""; display: inline-block; width: 12px; color: var(--text-muted); }
.data-table thead th[aria-sort="ascending"]::after  { content: " ↑"; }
.data-table thead th[aria-sort="descending"]::after { content: " ↓"; }
.data-table tbody tr:hover { background: var(--hover); }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.data-table .period { white-space: nowrap; }
.data-table .zero { color: var(--text-muted); }
.data-table .head-dot { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 5px; }
.data-table .head-dot--positive { background: var(--tone-positive); }
.data-table .head-dot--neutral  { background: var(--tone-neutral); }
.data-table .head-dot--negative { background: var(--tone-negative); }

/* --- Подвал и тултип ----------------------------------------------------- */

.pagefoot { margin-top: 24px; font-size: 12px; color: var(--text-muted); display: flex; flex-direction: column; gap: 2px; }
.pagefoot code { font-size: 11px; background: var(--surface-sunken); padding: 1px 5px; border-radius: 4px; }

.tooltip {
  position: fixed; z-index: 30; max-width: 300px;
  padding: 8px 10px; pointer-events: none; opacity: 0;
  transition: opacity .09s ease-out;
  background: var(--surface); color: var(--text-primary);
  border: 1px solid var(--border); border-radius: var(--radius-s);
  box-shadow: 0 6px 20px rgba(0, 0, 0, .14);
  font-size: 12px; line-height: 1.4;
}
.tooltip.is-visible { opacity: 1; }
.tooltip__title { color: var(--text-secondary); margin-bottom: 4px; }
.tooltip__value { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
.tooltip__label { color: var(--text-secondary); margin-top: 1px; }
.tooltip__meta  { color: var(--text-muted); margin-top: 3px; }
.tooltip__rows { display: grid; grid-template-columns: auto 1fr auto; gap: 2px 8px; align-items: center; }
.tooltip__key { width: 12px; height: 2px; border-radius: 1px; }
.tooltip__key--positive { background: var(--tone-positive); }
.tooltip__key--neutral  { background: var(--tone-neutral); }
.tooltip__key--negative { background: var(--tone-negative); }
.tooltip__name { color: var(--text-secondary); }
.tooltip__num { font-variant-numeric: tabular-nums; font-weight: 600; text-align: right; }
.tooltip__total { margin-top: 5px; padding-top: 5px; border-top: 1px solid var(--grid); display: flex; justify-content: space-between; gap: 12px; }

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
