/* ==========================================================================
   MORФEUS Dashboard — оформление

   Палитра задана токенами трижды: светлая на голом :root, тёмная —
   в prefers-color-scheme под защитой :not([data-theme="light"]), и ещё раз
   на [data-theme="dark"], чтобы ручной переключатель побеждал системную
   тему в обе стороны. Ни один цвет не определяется только внутри media.
   ========================================================================== */

:root {
  --bg:            #f4f6f8;
  --surface:       #ffffff;
  --surface-2:     #f8fafc;
  --border:        #e4e8ed;
  --border-strong: #d3d9e0;
  --text:          #11151c;
  --text-muted:    #626d7c;
  --text-faint:    #8a94a2;

  --accent:        #16a34a;
  --accent-soft:   #16a34a1f;
  --positive:      #15803d;
  --negative:      #dc2626;

  /* Ряды графиков. Их читает JS через getComputedStyle, поэтому
     палитра живёт в одном месте и переключается вместе с темой. */
  --series-primary:   #16a34a;
  --series-secondary: #6366f1;
  --series-accent:    #f59e0b;
  --series-compare:   #b4bcc6;
  --series-grid:      #e8ecf0;

  --channel-0: #16a34a;
  --channel-1: #6366f1;
  --channel-2: #f59e0b;
  --channel-3: #a855f7;
  --channel-4: #0ea5e9;
  --channel-5: #ef6c3a;
  --channel-6: #64748b;
  --channel-7: #14b8a6;
  --channel-8: #d946ef;
  --channel-9: #a8b3c1;

  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .05);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:            #0e1116;
    --surface:       #161a21;
    --surface-2:     #1c212a;
    --border:        #262c36;
    --border-strong: #333b47;
    --text:          #e7eaee;
    --text-muted:    #99a3b0;
    --text-faint:    #6d7887;

    --accent:        #4ade80;
    --accent-soft:   #4ade8026;
    --positive:      #4ade80;
    --negative:      #f87171;

    --series-primary:   #4ade80;
    --series-secondary: #818cf8;
    --series-accent:    #fbbf24;
    --series-compare:   #4d5765;
    --series-grid:      #232932;

    --channel-0: #4ade80;
    --channel-1: #818cf8;
    --channel-2: #fbbf24;
    --channel-3: #c084fc;
    --channel-4: #38bdf8;
    --channel-5: #fb8a5c;
    --channel-6: #94a3b8;
    --channel-7: #2dd4bf;
    --channel-8: #e879f9;
    --channel-9: #6b7684;

    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

:root[data-theme="dark"] {
  --bg:            #0e1116;
  --surface:       #161a21;
  --surface-2:     #1c212a;
  --border:        #262c36;
  --border-strong: #333b47;
  --text:          #e7eaee;
  --text-muted:    #99a3b0;
  --text-faint:    #6d7887;

  --accent:        #4ade80;
  --accent-soft:   #4ade8026;
  --positive:      #4ade80;
  --negative:      #f87171;

  --series-primary:   #4ade80;
  --series-secondary: #818cf8;
  --series-accent:    #fbbf24;
  --series-compare:   #4d5765;
  --series-grid:      #232932;

  --channel-0: #4ade80;
  --channel-1: #818cf8;
  --channel-2: #fbbf24;
  --channel-3: #c084fc;
  --channel-4: #38bdf8;
  --channel-5: #fb8a5c;
  --channel-6: #94a3b8;
  --channel-7: #2dd4bf;
  --channel-8: #e879f9;
  --channel-9: #6b7684;

  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
}

/* === Основа === */

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.wrap {
  max-width: 1320px;
  margin: 0 auto;
  padding: 20px 20px 64px;
}

/* Цифры не должны дёргаться при обновлении */
.num { font-variant-numeric: tabular-nums; }

/* === Шапка === */

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  /* Непрозрачный фон вместо backdrop-filter: полупрозрачный липкий
     слой заставляет браузер перерисовывать всё под собой при прокрутке
     и оставляет артефакты. Наглядность важнее эффекта матового стекла. */
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  max-width: 1320px;
  margin: 0 auto;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

.brand { display: flex; flex-direction: column; margin-right: auto; }
.brand h1 { margin: 0; font-size: 17px; font-weight: 650; letter-spacing: -.01em; }
.brand .period { font-size: 12.5px; color: var(--text-muted); }

.controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.presets { display: flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px; padding: 2px; }

.preset {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 13px;
  padding: 5px 11px;
  border-radius: 7px;
  cursor: pointer;
}
.preset:hover { color: var(--text); }
.preset[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.field { display: flex; align-items: center; gap: 6px; }
.field label { font-size: 12px; color: var(--text-muted); }

input[type="date"] {
  font: inherit;
  font-size: 13px;
  padding: 6px 9px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color-scheme: light dark;
}
input[type="date"]:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn {
  font: inherit;
  font-size: 13px;
  font-weight: 550;
  padding: 7px 15px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
.btn:hover { filter: brightness(.94); }
.btn:disabled { opacity: .6; cursor: progress; }

.icon-btn {
  appearance: none;
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
}
.icon-btn:hover { color: var(--text); border-color: var(--border-strong); }
.icon-btn svg { width: 17px; height: 17px; }

/* Иконку темы показываем ту, что соответствует текущему состоянию */
.icon-sun { display: none; }
:root[data-theme="dark"] .icon-sun { display: block; }
:root[data-theme="dark"] .icon-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .icon-sun { display: block; }
  :root:not([data-theme="light"]) .icon-moon { display: none; }
}

/* === Секции === */

.section { margin-top: 28px; }
.section-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 12px; }
.section-head h2 { margin: 0; font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--text-muted); }
.section-head .hint { font-size: 12.5px; color: var(--text-faint); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* === Выводы === */

.insights { padding: 16px 18px; }
.insights-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.insights-head h2 { margin: 0; font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--text-muted); }
.insights-body { font-size: 14.5px; line-height: 1.6; }
.insights-body ul { margin: 8px 0; padding-left: 20px; }
.insights-body li { margin-bottom: 4px; }
.insights-body strong { font-weight: 620; }

.dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent);
}
.dot.pulsing { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: .35; } 50% { opacity: 1; } }

.skeleton-line {
  height: 11px;
  border-radius: 5px;
  background: linear-gradient(90deg, var(--surface-2), var(--border), var(--surface-2));
  background-size: 200% 100%;
  animation: shimmer 1.5s linear infinite;
  margin-bottom: 9px;
}
@keyframes shimmer { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) {
  .skeleton-line, .dot.pulsing { animation: none; }
}

/* === KPI === */

.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 12px;
}

.kpi { padding: 14px 16px; display: flex; flex-direction: column; gap: 2px; overflow: hidden; }
.kpi-label { font-size: 12px; color: var(--text-muted); }
.kpi-value { font-size: 25px; font-weight: 660; letter-spacing: -.02em; line-height: 1.2; }
.kpi-value, .kpi-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kpi-foot { display: flex; align-items: flex-end; justify-content: space-between; gap: 8px; margin-top: 6px; }

.delta { font-size: 12.5px; font-weight: 560; display: inline-flex; align-items: baseline; gap: 3px; white-space: nowrap; }
.delta.up { color: var(--positive); }
.delta.down { color: var(--negative); }
.delta.flat { color: var(--text-faint); }
.delta .caption { color: var(--text-faint); font-weight: 450; }

.spark { height: 30px; flex: 0 1 104px; min-width: 0; }
.spark path { fill: none; stroke: var(--series-primary); stroke-width: 1.6; stroke-linejoin: round; stroke-linecap: round; opacity: .85; }

/* === Водопад === */

.waterfall-head { padding: 14px 16px 0; }
.waterfall-delta { font-size: 20px; font-weight: 650; letter-spacing: -.02em; }

/* === Сетка графиков === */

.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 14px; }
.chart-card { padding: 12px 14px 8px; min-width: 0; }
.chart-title { font-size: 13.5px; font-weight: 600; margin-bottom: 6px; }
.chart-body { width: 100%; height: 300px; }
.chart-card.wide { grid-column: 1 / -1; }
.chart-card.wide .chart-body { height: 340px; }

/* === Таблицы === */

.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { padding: 9px 14px; text-align: right; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; white-space: normal; }
thead th {
  font-size: 11.5px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .05em; color: var(--text-muted);
  border-bottom: 1px solid var(--border);
}
tbody tr + tr td { border-top: 1px solid var(--border); }
tbody tr:hover td { background: var(--surface-2); }

.swatch { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 7px; vertical-align: baseline; }

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

.lead {
  margin: 0 0 14px;
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-muted);
  max-width: 78ch;
}
.lead b { color: var(--text); font-weight: 600; }

.defs {
  margin-top: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 4px 16px 2px;
}
.defs > summary {
  cursor: pointer;
  padding: 10px 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  list-style: none;
}
.defs > summary::-webkit-details-marker { display: none; }
.defs > summary::before {
  content: "▸";
  display: inline-block;
  width: 15px;
  color: var(--text-faint);
  transition: transform .15s;
}
.defs[open] > summary::before { transform: rotate(90deg); }
.defs > summary:hover { color: var(--text); }

.defs dl {
  margin: 4px 0 12px;
  display: grid;
  grid-template-columns: minmax(150px, 200px) 1fr;
  gap: 10px 20px;
  font-size: 13.5px;
  line-height: 1.55;
}
.defs dt { font-weight: 600; color: var(--text); }
.defs dd { margin: 0; color: var(--text-muted); }
.defs dd em { font-style: normal; font-weight: 600; color: var(--text); }
.defs-foot {
  margin: 0 0 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-faint);
  line-height: 1.55;
}

.chart-note {
  margin: 0 0 10px;
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--text-muted);
}

@media (max-width: 700px) {
  .defs dl { grid-template-columns: 1fr; gap: 4px; }
  .defs dt { margin-top: 8px; }
}

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

.cohort-grid { font-size: 12.5px; }
.cohort-grid th { padding: 8px 6px; }
.cohort-grid td { padding: 6px; }
.cohort-grid thead th:nth-child(n+3) { text-align: right; min-width: 40px; }
.cohort-grid tbody tr:hover td { background: none; }

/* Насыщенность клетки задаётся переменной --heat из шаблона: так
   уровень считается на сервере, а цвет остаётся токеном темы. */
.cohort-grid .cell {
  background: color-mix(in srgb, var(--accent) calc(var(--heat, 0) * 62%), transparent);
  border-radius: 4px;
}
.cohort-grid .cell.base { background: none; color: var(--text-faint); }
.cohort-grid .cell.empty { background: none; color: var(--border-strong); }

/* Ссылки-разделы в шапке выглядят как переключатель периодов */
.presets a.preset { text-decoration: none; display: inline-block; }
.presets a.preset[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* === Строка качества === */

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.stat { padding: 12px 15px; }
.stat-label { font-size: 12px; color: var(--text-muted); }
.stat-value { font-size: 19px; font-weight: 620; letter-spacing: -.01em; margin-top: 1px; }
.stat-sub { font-size: 12px; color: var(--text-faint); margin-top: 2px; }

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

.foot { margin-top: 28px; font-size: 12.5px; color: var(--text-faint); display: flex; gap: 14px; flex-wrap: wrap; }

/* === Заставка холодной сборки ===
   Приходит первым куском ответа, пока данных ещё нет. Разметки страницы
   в этот момент не существует, поэтому заставка ни на что не опирается. */

.boot {
  position: fixed; inset: 0; z-index: 100;
  display: grid; place-items: center;
  background: var(--bg);
}
.boot-box { display: grid; gap: 12px; justify-items: center; text-align: center; padding: 24px; }
.boot-text { font-size: 15px; font-weight: 550; }
.boot-hint { font-size: 13px; color: var(--text-muted); }

/* === Загрузка при смене периода === */

.overlay {
  position: fixed; inset: 0; z-index: 90;
  display: none; place-items: center;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
}
.overlay.on { display: grid; }
.overlay-box { text-align: center; display: grid; gap: 12px; justify-items: center; }
.spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid var(--border-strong);
  border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.overlay-text { font-size: 14px; }
.overlay-hint { font-size: 12.5px; color: var(--text-muted); }

@media (max-width: 700px) {
  .charts { grid-template-columns: 1fr; }
  .chart-body, .chart-card.wide .chart-body { height: 260px; }
  .brand { margin-right: 0; width: 100%; }
  .topbar { position: static; }
}
