:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #ffffff;
  --surface-2: #eef4fc;
  --blue: #2a78d6;
  --blue-dark: #184f95;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --ink-muted: #898781;
  --grid: #e1e0d9;
  --border: rgba(11, 11, 11, 0.10);
  --good: #0ca30c;
  --bad: #d03b3b;

  --name-font: 16px;
  --value-font: 14px;
  --header-font: 9px;
  --diff-font: 10px;
  --cell-pad-h: 6px;
  --col-otm: 37.4px; /* было 44px, -15% */

  --sf-header-font: 10.5px;
  --sf-value-font: 17px;
  --sf-diff-font: 12px;
  --sf-col-otm: 36px;
}

@media (max-width: 1500px) {
  :root {
    --name-font: 14px;
    --value-font: 12px;
    --header-font: 8px;
    --diff-font: 9px;
    --cell-pad-h: 4px;
    --col-otm: 32.3px; /* было 38px, -15% */

    --sf-header-font: 9px;
    --sf-value-font: 15px;
    --sf-diff-font: 10.5px;
    --sf-col-otm: 32px;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--ink);
}

.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- header ---------- */
.hdr {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 8px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--grid);
  flex: 0 0 auto;
  position: sticky;
  top: 0;
  z-index: 50;
}
.hdr-left h1 {
  margin: 0;
  font-size: clamp(16px, 1.6vw, 22px);
  color: var(--blue-dark);
  font-weight: 700;
}
.hdr-sub {
  font-size: 13px;
  color: var(--ink-2);
  margin-top: 2px;
  text-transform: capitalize;
}
.hdr-right {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-left: auto;
}
.updated {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-muted);
  white-space: nowrap;
}
.refresh-spinner {
  display: inline-block;
  font-size: 15px;
  line-height: 1;
  color: var(--blue);
  animation: otm-spin 0.9s linear infinite;
}
@keyframes otm-spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}
.hidden { display: none !important; }

/* ---------- board: филиалы друг под другом — так таблице ОТМ есть куда
   растянуться по горизонтали и почти не нужна прокрутка ---------- */
.board {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 10px 14px 16px;
}
.loading {
  align-self: center;
  color: var(--ink-muted);
  font-size: 15px;
  padding: 40px 0;
}

.filial {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--grid);
  border-radius: 12px;
  overflow: hidden;
  min-width: 0;
}

.filial-title {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 18px;
  background: var(--blue-dark);
  color: #fff;
  padding: 12px 16px 12px 16px;
  min-width: 0;
}
/* фиксированная ширина (по самому длинному имени филиала — "Северо-восточный"/"Северо-западный"),
   чтобы таблица показателей у всех филиалов начиналась в одном и том же месте по горизонтали */
.filial-title-name {
  font-size: 19px;
  font-weight: 700;
  line-height: 1.15;
  flex: 0 0 200px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.filial-title-resp {
  font-size: var(--sf-diff-font);
  font-weight: 400;
  color: rgba(255, 255, 255, 0.85);
  margin-top: 2px;
}

/* контейнер с горизонтальной прокруткой — у таблицы 8 ОТМ + Итого не помещаются
   в треть экрана, поэтому скроллим саму таблицу, а не жертвуем 3-колоночной раскладкой */
.table-scroll {
  overflow-x: auto;
  min-width: 0;
  scrollbar-width: thin;
  scrollbar-color: var(--ink-muted) transparent;
}
.table-scroll::-webkit-scrollbar { height: 8px; }
.table-scroll::-webkit-scrollbar-track { background: transparent; }
.table-scroll::-webkit-scrollbar-thumb { background: var(--grid); border-radius: 4px; }
.table-scroll::-webkit-scrollbar-thumb:hover { background: var(--ink-muted); }
.filial-title .table-scroll { flex: 1 1 auto; }
.filial-title .table-scroll::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.35); }
.filial-title .table-scroll::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.6); }

/* ---------- общие стили таблиц с группами ОТМ (План/Факт) ---------- */
.ptable, .stable {
  border-collapse: collapse;
  table-layout: fixed;
}
/* table-layout: fixed на обеих таблицах — иначе колонки авто-подстраиваются под контент
   (у .stable из-за более крупного шрифта они иначе становятся шире, чем в .ptable) и колонки
   одного и того же ОТМ у филиала (.stable) и у площадок (.ptable) расходятся по ширине */
.ptable { width: 100%; }
.ptable col.col-name { width: 128px; }
.ptable col.col-otm,
.stable col.col-otm { width: var(--col-otm); }

.otm-code-link {
  border: none;
  background: transparent;
  padding: 0;
  font: inherit;
  font-weight: 700;
  color: inherit;
  cursor: pointer;
  text-decoration: underline;
  text-decoration-style: dotted;
  text-underline-offset: 2px;
}

/* --- шапка (группы ОТМ + подзаголовки План/Факт) --- */
.ptable thead th,
.stable thead th {
  background: var(--surface-2);
  color: var(--ink-2);
  font-weight: 700;
  text-transform: uppercase;
  text-align: center;
  line-height: 1.2;
  padding: 3px 2px;
  border-bottom: 1px solid var(--grid);
  white-space: nowrap;
}
.ptable thead th { font-size: var(--header-font); }
.stable thead th { font-size: var(--sf-header-font); padding: 4px 6px; }
.ptable thead th.th-name { text-align: left; padding-left: 8px; vertical-align: bottom; }
.ptable thead th.th-group,
.stable thead th.th-group { border-left: 1px solid var(--grid); }
.ptable thead th.th-sub:nth-child(odd),
.stable thead th.th-sub:nth-child(odd) { border-left: 1px solid var(--grid); }

/* лёгкая серая линия между соседними ОТМ-группами и в шапке, и в теле таблицы */
.ptable tbody td.td-group-start,
.stable tbody td.td-group-start { border-left: 1px solid var(--grid); }
.ptable thead th.th-group.th-total,
.stable thead th.th-group.th-total { border-left: 2px solid var(--blue); }

/* --- строки значений --- */
.ptable tbody td,
.stable tbody td {
  font-variant-numeric: tabular-nums;
  text-align: center;
  white-space: nowrap;
}
/* у площадок все значения (не только "Итого") — увеличенный размер (тот же, что понравился у "Итого") */
.ptable tbody td { font-size: calc(var(--value-font) * 1.15 * 1.15); padding: 8px var(--cell-pad-h); }
.stable tbody td { font-size: var(--sf-value-font); padding: 4px 5px; }
/* "Итого" по филиалам/итого-по-всем — крупнее остальных значений (+15%, потом ещё +15% сверху) */
.stable tbody td.val-total-value { font-size: calc(var(--sf-value-font) * 1.15 * 1.15); }

.ptable tbody tr.row-last td { border-bottom: 1px solid var(--grid); }

/* название площадки закреплено слева при горизонтальной прокрутке таблицы ОТМ */
.ptable th.th-name,
.ptable td.td-name {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--surface);
}
.ptable thead th.th-name { background: var(--surface-2); z-index: 3; }
/* border-bottom тут отдельно: имя площадки теперь растянуто (rowSpan) на строку значений и
   строку расхождений, и общий разделитель ".row-last td" до неё физически не дотягивается —
   его child-строка (row-diff) не включает эту ячейку */
.ptable tbody td.td-name {
  text-align: center;
  padding: 0 8px;
  vertical-align: middle;
  border-bottom: 1px solid var(--grid);
}
.td-name-main {
  font-weight: 700;
  font-size: calc(var(--name-font) * 1.2);
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
}

.ptable tbody tr.row-main:not(.row-last) td { padding-bottom: 3px; }
.ptable tbody tr.row-diff td { padding-top: 0; padding-bottom: 5px; }
.stable tbody tr.row-diff td { padding-top: 0; padding-bottom: 6px; }

td.td-noplan {
  color: var(--ink-muted);
  font-weight: 400;
  font-style: italic;
  font-size: 0.9em;
}
tr.row-total-col td.val-total { font-weight: 700; }
td.val-total { border-left: 2px solid var(--blue); }

td.diff {
  font-weight: 600;
  color: var(--ink-muted);
}
.ptable tbody td.diff { font-size: var(--diff-font); }
.stable tbody td.diff { font-size: var(--sf-diff-font); }
td.diff.diff-total { border-left: 2px solid var(--blue); }
td.diff-empty { padding: 0; }

.val-good { color: var(--good); font-weight: 400; }
.val-good-exceed { color: var(--good); font-weight: 700; }
.val-bad { color: var(--bad); font-weight: 700; }
.val-na { color: var(--ink-muted); font-weight: 400; }

/* --- вариант "stable" внутри синей шапки филиала: светлый текст на синем фоне --- */
.stable-filial thead th { color: rgba(255, 255, 255, 0.72); background: transparent; border-bottom-color: rgba(255,255,255,0.25); }
.stable-filial thead th.th-group,
.stable-filial thead th.th-sub:nth-child(odd) { border-left-color: rgba(255,255,255,0.25); }
.stable-filial tbody td.td-group-start { border-left-color: rgba(255,255,255,0.25); }
.stable-filial tbody td { color: #fff; font-weight: 700; }
.stable-filial tbody td.diff { color: rgba(255, 255, 255, 0.85); font-weight: 600; }
.stable-filial tbody td.td-noplan { color: rgba(255,255,255,0.55); }
.stable-filial tbody td.val-good { color: #17c93f; }
.stable-filial tbody td.val-good-exceed { color: #17c93f; }
.stable-filial tbody td.val-bad { color: #ff6b6b; }
.stable-filial td.val-total,
.stable-filial thead th.th-group.th-total,
.stable-filial thead th.th-sub.th-total-sub:nth-child(odd) { border-left-color: rgba(255,255,255,0.55); }

/* ---------- "итого" строка вверху страницы ---------- */
.grand-total {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 10px 16px;
  padding: 12px 16px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--grid);
  min-width: 0;
}
.grand-total-label {
  display: flex;
  flex-direction: column;
  white-space: nowrap;
  flex: 0 0 auto;
}
.grand-total-label-main,
.grand-total-label-sub {
  font-size: 17px;
  font-weight: 800;
  color: var(--blue-dark);
  line-height: 1.15;
}
.grand-total .table-scroll { flex: 1 1 auto; min-width: 0; }

.gt-card-chart {
  flex: 1 1 260px;
  min-width: 260px;
  max-width: 900px;
  display: flex;
  flex-direction: column;
  background: var(--surface-2);
  border-radius: 8px;
  padding: 5px 12px 7px;
}
.gt-chart-body { flex: 1 1 auto; min-width: 0; }
.gt-card-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  text-align: center;
  color: var(--ink-2);
}
.gt-title-plan { color: var(--blue-dark); }
.gt-title-fact { color: var(--blue); }
.gt-chart { display: block; width: 100%; height: 92px; }
.gt-chart-axis { font-size: 9px; font-weight: 600; fill: var(--ink-muted); }
.gt-chart-grid { stroke: var(--grid); stroke-width: 1; opacity: 0.8; vector-effect: non-scaling-stroke; }
.gt-chart-line-plan {
  fill: none; stroke: var(--blue-dark); stroke-width: 1.6; stroke-dasharray: 4 3; vector-effect: non-scaling-stroke;
}
.gt-chart-line-fact { fill: none; stroke: var(--blue); stroke-width: 1.8; vector-effect: non-scaling-stroke; }
.gt-chart-empty {
  flex: 1; display: flex; align-items: center; justify-content: center; font-size: 11px; color: var(--ink-muted);
}

/* ---------- footer legend ---------- */
.legend {
  flex: 0 0 auto;
  display: flex;
  gap: 24px;
  justify-content: flex-start;
  padding: 6px 20px 10px;
  font-size: 12px;
  color: var(--ink-2);
}
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.good { background: var(--good); }
.dot.bad { background: var(--bad); }

.info-trigger {
  border: none;
  background: transparent;
  color: var(--ink-muted);
  font: inherit;
  font-size: 12px;
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
  padding: 0;
}
.info-trigger:hover { color: var(--blue-dark); }

/* ---------- справка "Справка по ОТМ" — модальная панель ---------- */
.info-overlay {
  position: fixed;
  inset: 0;
  z-index: 199;
  background: rgba(11, 11, 11, 0.35);
}
.info-panel {
  position: fixed;
  z-index: 200;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(600px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--grid);
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(11, 11, 11, 0.24);
  padding: 16px 18px;
}
.info-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  position: sticky;
  top: -16px;
  background: var(--surface);
  padding-top: 2px;
}
.info-panel-title { font-size: 16px; font-weight: 700; color: var(--ink); }
.info-panel-close {
  width: 28px; height: 28px; border: none; border-radius: 6px;
  background: var(--surface-2); color: var(--blue-dark); font-size: 14px; line-height: 1; cursor: pointer;
}
.info-panel-close:hover { background: #e2edfb; }
.info-panel-body { font-size: 14px; line-height: 1.5; color: var(--ink-2); }
.otm-entry {
  margin: 0 0 12px;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--surface-2);
  transition: background 0.4s ease;
}
.otm-entry.flash { background: #cfe3fa; }
.otm-entry:last-child { margin-bottom: 0; }
.otm-entry-code { font-weight: 700; color: var(--ink); display: block; margin-bottom: 2px; }
.otm-entry-full { color: var(--ink-2); white-space: pre-line; }

/* ---------- мобильные телефоны ---------- */
@media (max-width: 768px) {
  .hdr { flex-wrap: wrap; padding: 8px 10px; gap: 8px; }
  .board { padding: 8px; gap: 10px; }
  .grand-total { flex-direction: column; align-items: stretch; padding: 8px 10px; }
  .grand-total .table-scroll { width: 100%; }
  .gt-card-chart { width: 100%; }
  .filial-title { flex-wrap: wrap; padding: 8px 10px; }
}
