/* ---------------------------------------------------------------------------
   Токены те же, что у дашбордов проекта: светлая тема — база на голом :root,
   тёмная объявлена дважды — через prefers-color-scheme (системная настройка) и
   через [data-theme] (переключатель в шапке), чтобы переключатель выигрывал в
   обе стороны. Цвет статуса нигде не единственный носитель смысла: рядом с
   точкой всегда подпись.
   --------------------------------------------------------------------------- */
: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);
  --shadow:         0 1px 2px rgba(11, 11, 11, .06), 0 8px 24px rgba(11, 11, 11, .04);

  --ok:             #17794c;  /* работает */
  --down:           #cc3d33;  /* приостановлен */
  --warn:           #b8770a;  /* в разработке */
  --idle:           #8d8b85;
  --accent:         #2a78d6;

  --ok-soft:        #d6ece0;
  --down-soft:      #fadedb;
  --warn-soft:      #fbeccc;
  --accent-soft:    #dce9f9;


  /* Последовательная шкала тепловых карт: светлее → темнее = больше упоминаний.
     Один тон, светлота монотонна, подпись на каждой ступени читаема. */
  --s0: #eef2f7; --s1: #d7e4f4; --s2: #b9d0ec; --s3: #8fb4e0;
  --s4: #6394d2; --s5: #3d74bd; --s6: #24559b;
  --s-ink-lo: #0b0b0b;   /* подпись на светлых ступенях 0–4 */
  --s-ink-hi: #ffffff;   /* подпись на тёмных ступенях 5–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);
    --shadow:         0 1px 2px rgba(0, 0, 0, .5), 0 8px 24px rgba(0, 0, 0, .35);
    --ok:             #43b27a;
    --down:           #ef6a5e;
    --warn:           #e0a234;
    --idle:           #7c7a75;
    --accent:         #3987e5;
    --ok-soft:        #16321f;
    --down-soft:      #3a1b18;
    --warn-soft:      #33260e;
    --accent-soft:    #14273d;
    --s0: #212326; --s1: #1d3550; --s2: #24486e; --s3: #2d5c8d;
    --s4: #3f78ad; --s5: #5f97c8; --s6: #8fbbe0;
    --s-ink-lo: #ffffff;
    --s-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);
  --shadow:         0 1px 2px rgba(0, 0, 0, .5), 0 8px 24px rgba(0, 0, 0, .35);
  --ok:             #43b27a;
  --down:           #ef6a5e;
  --warn:           #e0a234;
  --idle:           #7c7a75;
  --accent:         #3987e5;
  --ok-soft:        #16321f;
  --down-soft:      #3a1b18;
  --warn-soft:      #33260e;
  --accent-soft:    #14273d;
  --s0: #212326; --s1: #1d3550; --s2: #24486e; --s3: #2d5c8d;
  --s4: #3f78ad; --s5: #5f97c8; --s6: #8fbbe0;
  --s-ink-lo: #ffffff;
  --s-ink-hi: #0b0b0b;

}

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

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

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

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -.01em; }
h1 { font-size: 20px; }
h2 { font-size: 15px; }
h3 { 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; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; margin: -1px;
  clip-path: inset(50%); overflow: hidden; white-space: nowrap;
}

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

.topbar {
  display: flex; flex-wrap: wrap; gap: 12px 16px;
  align-items: flex-end; justify-content: space-between;
  padding: 24px 0 16px;
}
.topbar__title h1 { display: flex; align-items: center; gap: 8px; }
.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; font-variant-numeric: tabular-nums; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 12px; font: inherit; font-size: 13px; color: inherit;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-s); cursor: pointer;
}
.btn:hover { background: var(--hover); }
.btn--icon { padding: 7px 9px; }
.btn[disabled] { opacity: .55; cursor: default; }

/* --- Баннер -------------------------------------------------------------- */

.banner {
  display: block; padding: 10px 14px; margin-bottom: 16px; font-size: 13px;
  background: var(--warn-soft); border: 1px solid var(--border);
  border-radius: var(--radius-s);
}
.banner[hidden] { display: none; }
.banner--error { background: var(--down-soft); }

/* --- Фокус и фильтры ------------------------------------------------------ */

.focus {
  display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: end;
  padding: 14px 16px; margin-bottom: 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.filters {
  display: flex; flex-wrap: wrap; gap: 10px 12px; align-items: end;
  padding: 14px 16px; margin-bottom: 20px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.field { display: flex; flex-direction: column; gap: 4px; }
.field__label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }
.field input, .field select {
  padding: 7px 10px; font: inherit; font-size: 14px; color: inherit;
  background: var(--plane); border: 1px solid var(--border); border-radius: var(--radius-s);
  min-width: 150px; max-width: 340px;
}
.field--wide select { min-width: 260px; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; padding-bottom: 7px; }
.focus__hint { flex: 1 1 260px; font-size: 12px; color: var(--text-muted); padding-bottom: 6px; }

/* Переключатель метрики: две кнопки, выбранная залита. */
.toggle { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius-s); overflow: hidden; }
.toggle button {
  padding: 6px 11px; font: inherit; font-size: 12px; color: var(--text-secondary);
  background: var(--surface); border: 0; cursor: pointer;
}
.toggle button + button { border-left: 1px solid var(--border); }
.toggle button[aria-pressed="true"] { background: var(--accent); color: #fff; }

/* --- KPI ------------------------------------------------------------------ */

.kpis {
  display: grid; gap: 10px; margin-bottom: 20px;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
}
.kpi {
  padding: 14px 16px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.kpi__value { font-size: 26px; font-weight: 600; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.kpi__label { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }
.kpi__note  { font-size: 11px; color: var(--text-muted); margin-top: 4px; }
.kpi--focus .kpi__value { color: var(--accent); }

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

.cards { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); }
.card {
  padding: 16px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  min-width: 0;
}
.card--wide { grid-column: 1 / -1; }
.card__head {
  display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: baseline;
  justify-content: space-between; margin-bottom: 4px;
}
.card__note { font-size: 12px; color: var(--text-muted); margin-bottom: 14px; }

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

.bars { display: flex; flex-direction: column; gap: 7px; }
.bar { display: grid; grid-template-columns: minmax(90px, 26%) 1fr auto; gap: 10px; align-items: center; }
.bar__name { font-size: 13px; color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* Длинные подписи (запросы) переносятся: обрезка съедала бы весь смысл. */
.bars--wrap .bar { grid-template-columns: minmax(150px, 42%) 1fr auto; align-items: start; }
.bars--wrap .bar__name { white-space: normal; text-overflow: clip; line-height: 1.3; }
.bars--wrap .bar__track { margin-top: 2px; }
.bar--focus .bar__name { color: var(--text-primary); font-weight: 600; }
.bar__track { position: relative; height: 18px; background: var(--surface-sunken); border-radius: 4px; }
.bar__fill {
  position: absolute; inset: 0 auto 0 0; min-width: 2px;
  border-radius: 4px;                       /* 4px скруглённый торец данных */
  background: color-mix(in oklab, var(--accent) 38%, var(--surface-sunken));
}
.bar--focus .bar__fill { background: var(--accent); }
.bar__value { font-size: 12px; font-variant-numeric: tabular-nums; color: var(--text-secondary); min-width: 92px; text-align: right; }
.bar--zero .bar__value { color: var(--text-muted); }

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

.heat { overflow-x: auto; }
.heat table { border-collapse: separate; border-spacing: 2px; font-size: 11px; }
.heat th {
  font-weight: 500; color: var(--text-secondary); text-align: left;
  padding: 2px 6px; white-space: nowrap;
}
.heat thead th { vertical-align: bottom; font-size: 11px; }
.heat tbody th { position: sticky; left: 0; background: var(--surface); z-index: 1; max-width: 210px; overflow: hidden; text-overflow: ellipsis; }
.heat td {
  width: 44px; height: 26px; text-align: center; border-radius: 3px;
  font-variant-numeric: tabular-nums; cursor: default;
}
.heat td.is-empty { background: var(--surface-sunken); color: var(--text-muted); }
.heat .col-focus { outline: 2px solid var(--accent); outline-offset: -2px; }

.legend { display: flex; align-items: center; gap: 8px; margin-top: 12px; font-size: 11px; color: var(--text-muted); }
.legend__steps { display: flex; gap: 2px; }
.legend__step { width: 26px; height: 12px; border-radius: 2px; }

/* --- Столбцы по датам (высота = сколько ответов, заливка = упоминания) ------ */

.timeline { display: flex; align-items: flex-end; gap: 6px; height: 210px; padding-top: 8px; overflow-x: auto; }
.tl { display: flex; flex-direction: column; align-items: center; gap: 8px; flex: 1 1 0; min-width: 30px; }
.tl__col {
  position: relative; width: 100%; background: var(--surface-sunken);
  border-radius: 3px; display: flex; align-items: flex-end;
}
.tl__fill { width: 100%; background: var(--accent); border-radius: 3px; min-height: 2px; }
.tl__col--empty { background: repeating-linear-gradient(45deg, var(--surface-sunken) 0 4px, var(--plane) 4px 8px); }
.tl__label { font-size: 10px; color: var(--text-muted); white-space: nowrap; transform: rotate(-45deg); transform-origin: center; height: 42px; }
/* Запросы во всю ширину: подпись занимает треть, полосе остаётся много места. */
.card--wide .bars--wrap .bar { grid-template-columns: minmax(220px, 32%) 1fr auto; }

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

.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th, table.data td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--grid); vertical-align: top; }
table.data th { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); font-weight: 500; white-space: nowrap; }
table.data tbody tr:hover { background: var(--hover); }
table.data td.nowrap { white-space: nowrap; font-variant-numeric: tabular-nums; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  display: inline-flex; padding: 1px 7px; font-size: 11px; border-radius: 999px;
  background: var(--surface-sunken); border: 1px solid var(--border); color: var(--text-secondary);
}
.chip--focus { background: var(--accent-soft); border-color: transparent; color: var(--accent); font-weight: 600; }
.chip--none { color: var(--text-muted); border-style: dashed; }
.muted { color: var(--text-muted); }

/* --- Всплывающая подсказка -------------------------------------------------- */

.tip {
  position: fixed; z-index: 40; pointer-events: none; max-width: 280px;
  padding: 7px 10px; font-size: 12px; line-height: 1.35;
  background: var(--surface); color: var(--text-primary);
  border: 1px solid var(--border); border-radius: var(--radius-s);
  box-shadow: 0 4px 16px rgba(0,0,0,.16);
}
.tip[hidden] { display: none; }
.tip b { font-weight: 600; }

/* --- Текст ответа ---------------------------------------------------------- */

.modal {
  position: fixed; inset: 0; z-index: 50; display: grid; place-items: center;
  padding: 20px; background: rgba(0,0,0,.45);
}
.modal[hidden] { display: none; }
.modal__box {
  width: min(760px, 100%); max-height: 86vh; display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.modal__head {
  display: flex; gap: 12px; align-items: flex-start; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--grid);
}
.modal__title { font-size: 13px; color: var(--text-secondary); }
.modal__title b { display: block; font-size: 15px; color: var(--text-primary); margin-bottom: 2px; }
.modal__body { padding: 16px; overflow: auto; font-size: 14px; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere; }

/* --- Подвал --------------------------------------------------------------- */

.foot {
  display: flex; flex-wrap: wrap; gap: 6px 16px;
  margin-top: 28px; padding-top: 16px; border-top: 1px solid var(--grid);
  font-size: 12px; color: var(--text-muted);
}

@media (max-width: 720px) {
  .cards { grid-template-columns: 1fr; }
  .bar { grid-template-columns: minmax(80px, 34%) 1fr auto; }
  .bar__value { min-width: 74px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* ---------------------------------------------------------------------------
   Трафик: серии, графики, легенда.

   Пять источников — пять слотов категориальной палитры проекта, в фиксированном
   порядке. Порядок не зависит от того, сколько серий сейчас на экране: цвет
   закреплён за источником, а не за его местом в стопке.

   В светлой теме три из пяти оттенков не дотягивают до контраста 3:1 к фону —
   поэтому цвет нигде не единственный носитель смысла: у каждой серии в легенде
   стоит подпись со значением, а под графиком лежит та же таблица числами.
   --------------------------------------------------------------------------- */
:root {
  --series-1: #2a78d6;  /* поисковые системы */
  --series-2: #eb6834;  /* прямые заходы */
  --series-3: #1baf7a;  /* реклама */
  --series-4: #eda100;  /* ссылки на сайтах */
  --series-5: #e87ba4;  /* внутренние переходы */
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
  }
}

:root[data-theme="dark"] {
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
}

.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart__grid { stroke: var(--grid); stroke-width: 1; }
.chart__axis { fill: var(--text-muted); font-size: 11px; }
.chart__baseline { stroke: var(--baseline); stroke-width: 1; }
.chart__crosshair { stroke: var(--baseline); stroke-width: 1; stroke-dasharray: 3 3; pointer-events: none; }
.chart__hit { fill: transparent; }
.chart__hit:hover { fill: var(--hover); }

.legend { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 14px; }
.legend__item { display: flex; align-items: baseline; gap: 8px; font-size: 13px; color: var(--text-secondary); }
.legend__dot { width: 10px; height: 10px; border-radius: 3px; flex: none; transform: translateY(1px); }
.legend__value { color: var(--text-primary); font-variant-numeric: tabular-nums; font-weight: 600; }

.tip {
  position: fixed; z-index: 30; max-width: 280px; padding: 8px 10px;
  background: var(--surface); color: var(--text-primary);
  border: 1px solid var(--border); border-radius: var(--radius-s);
  box-shadow: var(--shadow); font-size: 12.5px; line-height: 1.5;
  pointer-events: none; opacity: 0; transition: opacity .12s;
}
.tip[data-open="1"] { opacity: 1; }
.tip__head { font-weight: 600; margin-bottom: 4px; }
.tip__row { display: flex; align-items: baseline; gap: 8px; justify-content: space-between; }
.tip__row span:last-child { font-variant-numeric: tabular-nums; }
.tip__total { border-top: 1px solid var(--border); margin-top: 4px; padding-top: 4px; }

.rows { display: flex; flex-direction: column; gap: 10px; }
.row2 { display: grid; grid-template-columns: minmax(150px, 1.2fr) 2fr auto; gap: 12px; align-items: center; font-size: 13px; }
.row2__name { color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row2__track { background: var(--surface-sunken); border-radius: 4px; height: 12px; overflow: hidden; }
.row2__fill { height: 100%; border-radius: 4px; }
.row2__value { color: var(--text-primary); font-variant-numeric: tabular-nums; font-weight: 600; min-width: 5.5em; text-align: right; }
.row2__share { color: var(--text-muted); font-weight: 400; }

.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td { padding: 7px 10px; border-bottom: 1px solid var(--border); text-align: right; }
.data-table th:first-child, .data-table td:first-child { text-align: left; }
.data-table th { color: var(--text-muted); font-weight: 600; font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; white-space: nowrap; }
.data-table td { font-variant-numeric: tabular-nums; color: var(--text-primary); }
.data-table tbody tr:hover { background: var(--hover); }
.scroll-x { overflow-x: auto; }

.card__hint { font-size: 12px; color: var(--text-muted); margin: -2px 0 14px; }
