/* ── Stapel font family ──────────────────────────────────────────────── */
@font-face {
  font-family: 'Stapel';
  src: url('/static/fonts/stapel_regular.otf') format('opentype');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'Stapel';
  src: url('/static/fonts/stapel_medium.otf') format('opentype');
  font-weight: 500;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: 'Stapel';
  src: url('/static/fonts/stapel_bold.otf') format('opentype');
  font-weight: 600 700;
  font-style: normal;
  font-display: swap;
}

/* CSS-переменные */
:root {
  --bg:           #F8F9FA;
  --bg-darker:    #EAECEF;
  --bg-card:      #FFFFFF;
  --accent-red:   #CC2C24;
  --text:         #1A1A1A;
  --text-muted:   #5F6368;
  --status-green:  #2EA043;
  --status-yellow: #D4A017;
  --status-orange: #E06C00;
  --status-red:    #CC2C24;

  --border-color:  rgba(0,0,0,0.08);
  --border-subtle: rgba(0,0,0,0.06);
  --hover-bg:      rgba(0,0,0,0.04);
  --hover-row:     rgba(0,0,0,0.02);
  --btn-bg:        rgba(0,0,0,0.05);
  --btn-hover:     rgba(0,0,0,0.10);
  --topbar-bg:     rgba(234,236,239,0.6);
  --skeleton-bg:   rgba(0,0,0,0.06);
  --shadow:        rgba(0,0,0,0.12);
  --chart-tick:    #5F6368;
  --chart-grid:    #E0E0E0;
}

[data-theme="dark"] {
  --bg:           #0D1117;
  --bg-darker:    #161B22;
  --bg-card:      #1C2128;
  --text:         #E6EDF3;
  --text-muted:   #8B949E;
  --status-yellow: #E3B341;

  --border-color:  rgba(255,255,255,0.08);
  --border-subtle: rgba(255,255,255,0.06);
  --hover-bg:      rgba(255,255,255,0.06);
  --hover-row:     rgba(255,255,255,0.03);
  --btn-bg:        rgba(255,255,255,0.08);
  --btn-hover:     rgba(255,255,255,0.14);
  --topbar-bg:     rgba(13,17,23,0.85);
  --skeleton-bg:   rgba(255,255,255,0.08);
  --shadow:        rgba(0,0,0,0.5);
  --chart-tick:    #8B949E;
  --chart-grid:    #30363D;
}

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

html, body { height: 100%; font-size: 14px; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Stapel', 'Segoe UI', system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ── Layout ─────────────────────────────────────────────────────────── */
#app { display: flex; height: 100vh; overflow: hidden; }

.sidebar {
  width: 224px;
  flex-shrink: 0;
  background: var(--bg-darker);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 4px;
  overflow-y: auto;
  transition: width 0.25s ease, padding 0.25s ease;
}

.sidebar.collapsed {
  width: 0;
  padding: 0;
  overflow: hidden;
}

.sidebar-toggle {
  width: 28px; height: 28px;
  border: none;
  background: var(--btn-bg);
  border-radius: 6px;
  cursor: pointer;
  font-size: 18px;
  color: var(--text-muted);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background 0.1s, color 0.1s;
  line-height: 1;
  padding: 0;
  user-select: none;
}
.sidebar-toggle:hover { background: var(--btn-hover); color: var(--text); }

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px 16px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-logo-icon {
  width: 40px;
  height: 40px;
  object-fit: contain;
  flex-shrink: 0;
  background: #fff;
  border-radius: 8px;
  padding: 3px;
}

.sidebar-logo-title  { font-weight: 600; font-size: 13px; }
.sidebar-logo-sub    { font-size: 11px; color: var(--text-muted); }

.nav-link {
  display: block;
  padding: 7px 12px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--text-muted);
  text-decoration: none;
  border-left: 2px solid transparent;
  transition: background 0.1s, color 0.1s;
  cursor: pointer;
}

.nav-link:hover         { color: var(--text); background: var(--hover-bg); }
.nav-link.active        { color: var(--text); background: var(--hover-bg); border-left-color: var(--accent-red); }

.sidebar-footer { margin-top: auto; padding-top: 16px; font-size: 11px; color: var(--text-muted); }

.main-area { flex: 1; display: flex; flex-direction: column; min-width: 0; overflow: hidden; }

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 20px;
  border-bottom: 1px solid var(--border-color);
  background: var(--topbar-bg);
  flex-shrink: 0;
}

.topbar-info  { font-size: 13px; color: var(--text-muted); }
.topbar-info span { font-family: 'Stapel', 'Segoe UI', system-ui, sans-serif; color: var(--text); }
.topbar-spacer { flex: 1; }

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

.sync-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--text-muted);
}
.sync-dot.online  { background: var(--status-green); }
.sync-dot.warn    { background: var(--status-yellow); }
.sync-dot.error   { background: var(--status-red); }

.content { flex: 1; padding: 16px 20px; overflow: hidden; display: flex; flex-direction: column; }
#page-content { flex: 1; min-height: 0; overflow-y: auto; }

/* ── Cards & Components ──────────────────────────────────────────────── */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 16px;
}

.kpi-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; flex: 1; grid-auto-rows: 1fr; }

.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 12px 14px;
  display: flex; flex-direction: column; gap: 4px;
  align-items: center; text-align: center;
  justify-content: center;
  height: 100%;
  box-sizing: border-box;
}

.kpi-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.kpi-value {
  font-family: 'Stapel', 'Segoe UI', system-ui, sans-serif;
  font-size: 28px;
  font-weight: 700;
  line-height: 1;
}
.kpi-suffix { font-size: 14px; font-family: inherit; color: var(--text-muted); margin-left: 4px; font-weight: normal; }
.kpi-hint   { font-size: 11px; color: var(--text-muted); }

.tone-neutral { color: var(--text); }
.tone-good    { color: var(--status-green); }
.tone-warn    { color: var(--status-orange); }
.tone-bad     { color: var(--status-red); }

/* ── Section headers ─────────────────────────────────────────────────── */
.section-title { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
.section-sub   { font-size: 13px; color: var(--text-muted); margin-bottom: 8px; }
.page-title    { font-size: 22px; font-weight: 700; }
.page-sub      { font-size: 13px; color: var(--text-muted); margin-top: 4px; }

.flex-col-gap4  { display: flex; flex-direction: column; gap: 16px; max-width: 1400px; }
.flex-col-gap6  { display: flex; flex-direction: column; gap: 24px; max-width: 1400px; }

/* ── Table ───────────────────────────────────────────────────────────── */
.data-table-wrap { background: var(--bg-card); border: 1px solid var(--border-color); border-radius: 8px; overflow: hidden; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table thead tr { background: var(--bg-darker); }
.data-table th {
  padding: 8px 12px;
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 500;
  white-space: nowrap;
}
.data-table th.right { text-align: right; }
.data-table th.center { text-align: center; }
.data-table td { padding: 8px 12px; border-top: 1px solid var(--border-subtle); }
.data-table td.right { text-align: right; }
.data-table td.center { text-align: center; }
.data-table tr:hover td { background: var(--hover-row); }
.data-table tr.overdue td { background: rgba(204,44,36,0.03); }

/* Аккордеон блокеров (#контроль_срока) */
.data-table tr.row-expandable { cursor: pointer; }
.data-table tr.blocker-row td { background: var(--bg-darker); padding: 10px 16px; }
.data-table tr.blocker-row:hover td { background: var(--bg-darker); }
/* Грид на списке: колонки одинаковы у всех блокеров, как строки таблицы */
.blocker-list { display: grid; grid-template-columns: 1fr max-content max-content max-content; align-items: baseline; gap: 6px 24px; font-size: 13px; }
.blocker-item { display: contents; }
.blocker-resp { color: var(--text-muted); white-space: nowrap; }
.blocker-deadline { white-space: nowrap; text-align: right; }
.blocker-empty { color: var(--text-muted); }

.age-red    { color: var(--status-red);    font-family: 'Stapel', 'Segoe UI', system-ui, sans-serif; }
.age-orange { color: var(--status-orange); font-family: 'Stapel', 'Segoe UI', system-ui, sans-serif; }
.age-bold   { font-weight: 700; }

/* ── Chips ───────────────────────────────────────────────────────────── */
.chip {
  display: inline-flex; align-items: center;
  padding: 2px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 500; white-space: nowrap;
}
.chip-critical, .chip-high   { background: rgba(204,44,36,0.12); color: var(--status-red); }
.chip-medium                  { background: rgba(224,108,0,0.12); color: var(--status-orange); }
.chip-low                     { background: rgba(46,160,67,0.12); color: var(--status-green); }

/* ── Progress bar ────────────────────────────────────────────────────── */
.bar-row { display: flex; align-items: center; gap: 12px; }
.bar-label { width: 200px; font-size: 13px; color: var(--text-muted); flex-shrink: 0; }
.bar-track { flex: 1; height: 22px; background: var(--bg-darker); border-radius: 4px; overflow: hidden; }
.bar-fill  {
  height: 100%;
  background: var(--accent-red);
  display: flex; align-items: center; justify-content: flex-end;
  padding-right: 6px;
  font-size: 11px; font-family: 'Stapel', 'Segoe UI', system-ui, sans-serif;
  color: #fff;
  min-width: 24px;
  transition: width 0.3s;
}
.bar-pct { width: 40px; text-align: right; font-size: 13px; font-family: 'Stapel', 'Segoe UI', system-ui, sans-serif; color: var(--text-muted); }

/* ── Качество: предотвратимость дефектов ─────────────────────────────── */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.stage-list { display: flex; flex-direction: column; gap: 8px; }
/* Раскрытие кейсов этапа — нативный <details>, без JS-аккордеона */
.stage-row > summary { list-style: none; cursor: pointer; }
.stage-row > summary::-webkit-details-marker { display: none; }
.stage-row > summary:hover .bar-label { color: var(--text); }
.stage-items { display: flex; flex-direction: column; gap: 6px; padding: 8px 0 4px 12px; }
.stage-item { display: flex; align-items: baseline; gap: 8px; font-size: 13px; flex-wrap: wrap; }
.stage-item .section-sub { margin-bottom: 0; }
.stage-item-title { flex: 1 1 200px; }
.stage-measure { font-size: 13px; color: var(--text); margin-bottom: 4px; }
.summary-list { margin: 0; padding-left: 20px; font-size: 14px; line-height: 1.7; }
.window-select {
  font-family: inherit; font-size: 13px; color: var(--text);
  padding: 4px 8px; border-radius: 6px;
  background: var(--bg-card); border: 1px solid var(--border-color);
}

/* ── Charts ──────────────────────────────────────────────────────────── */
.chart-wrap { height: 260px; position: relative; }

/* ── Button ──────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 12px; border-radius: 6px;
  font-family: inherit; font-size: 13px; font-weight: 500;
  cursor: pointer; border: none; text-decoration: none;
  transition: background 0.15s;
}
.btn-primary { background: var(--accent-red); color: #fff; }
.btn-primary:hover { background: #a82218; }

/* Theme toggle */
.theme-toggle {
  width: 28px; height: 28px;
  border: none;
  background: var(--btn-bg);
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background 0.1s;
  padding: 0;
  line-height: 1;
}
.theme-toggle:hover { background: var(--btn-hover); }

/* Export dropdown */
.export-wrap { position: relative; }
.export-dropdown {
  position: absolute; right: 0; top: calc(100% + 6px);
  width: 220px;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  box-shadow: 0 4px 16px var(--shadow);
  z-index: 20;
  overflow: hidden;
}
.export-item {
  display: block; width: 100%;
  padding: 12px 16px;
  background: none; border: none; border-top: 1px solid var(--border-subtle);
  text-align: left; cursor: pointer; font-size: 13px;
  transition: background 0.1s;
}
.export-item:first-child { border-top: none; }
.export-item:hover { background: var(--hover-bg); }
.export-item:disabled { opacity: 0.5; cursor: default; }
.export-item-sub { font-size: 11px; color: var(--text-muted); margin-top: 2px; }

/* ── Link ────────────────────────────────────────────────────────────── */
.link-bitrix { font-size: 12px; color: var(--accent-red); text-decoration: none; white-space: nowrap; }
.link-bitrix:hover { text-decoration: underline; }

/* ── Skeleton ────────────────────────────────────────────────────────── */
.skeleton {
  background: var(--skeleton-bg);
  border-radius: 6px;
  animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

/* ── Error ───────────────────────────────────────────────────────────── */
.error-box { background: var(--bg-card); border: 1px solid var(--border-color); border-radius: 8px; padding: 16px; color: var(--status-red); font-size: 13px; }

/* ── Grid helpers ────────────────────────────────────────────────────── */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }

/* ── Overview layout ─────────────────────────────────────────────────── */
/* id в селекторе — чтобы перебить overflow-y:auto правила #page-content */
#page-content.ov-page { overflow: hidden; display: flex; flex-direction: column; }
.ov-outer      { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 12px; overflow: hidden; }
.ov-header     { flex-shrink: 0; }
.ov-top-row    { flex: 1; min-height: 0; display: flex; gap: 20px; overflow: hidden; }
.ov-left-col   { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.ov-bars-col   { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow: hidden; }
.ov-bars-card  { flex: 1; min-height: 0; overflow-y: auto; }
.ov-bars-inner { display: flex; flex-direction: column; gap: 6px; }
.ov-table-row  { flex: 1; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.ov-table-wrap { flex: 1; min-height: 0; overflow-y: auto; }
.kpi-grid-3    { grid-template-columns: repeat(3, 1fr); }

@media (max-width: 900px) {
  .kpi-grid { grid-template-columns: repeat(3, 1fr); }
  .grid-2   { grid-template-columns: 1fr; }

  /* Обзор: KPI и «Распределение по статусам» друг под другом — рядом им
     не хватает ширины (карточки обрезаются, треки баров схлопываются).
     Контент экрана скроллится целиком внутри #page-content. */
  #page-content.ov-page { display: block; overflow-y: auto; }
  .ov-outer, .ov-top-row, .ov-left-col, .ov-bars-col, .ov-table-row {
    display: block; overflow: visible;
  }
  .ov-outer > * + *, .ov-top-row > * + * { margin-top: 16px; }
  .ov-bars-card  { overflow: visible; max-height: none; }
  .ov-table-wrap { overflow: visible; }
  .kpi-grid { grid-auto-rows: auto; }
}

@media print {
  .sidebar, .topbar { display: none !important; }
  #app { display: block; }
  .main-area { overflow: visible; }
  .content { padding: 0; overflow: visible; }
  .chart-wrap { height: 220px; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}

/* ── Режим экспорта (html2canvas): растягиваем контейнеры на всю высоту контента ── */
body.exporting,
body.exporting html { height: auto !important; overflow: visible !important; }
body.exporting #app { height: auto !important; overflow: visible !important; }
body.exporting .main-area { height: auto !important; overflow: visible !important; flex: none !important; }
body.exporting .content { overflow: visible !important; }
body.exporting #page-content { overflow: visible !important; height: auto !important; min-height: 0 !important; }
body.exporting .ov-outer,
body.exporting .ov-table-wrap,
body.exporting .ov-table-row { overflow: visible !important; min-height: 0 !important; }

/* ── Scrollbar (dark theme) ─────────────────────────────────────────── */
[data-theme="dark"] {
  scrollbar-color: #30363D #161B22;
  scrollbar-width: thin;
}
[data-theme="dark"] ::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
[data-theme="dark"] ::-webkit-scrollbar-track {
  background: #161B22;
}
[data-theme="dark"] ::-webkit-scrollbar-thumb {
  background: #30363D;
  border-radius: 6px;
  border: 2px solid #161B22;
}
[data-theme="dark"] ::-webkit-scrollbar-thumb:hover {
  background: #484F58;
}
[data-theme="dark"] ::-webkit-scrollbar-corner {
  background: #161B22;
}

/* ══ Mobile (≤768px) ═════════════════════════════════════════════════════
   Блок в конце файла: при равной специфичности правила побеждают каскадом.
   Desktop-поведение ≥769px не меняется. */
@media (max-width: 768px) {
  /* Оболочка: естественный документный скролл вместо inner-scroll —
     100vh переполняет visual viewport iOS, вложенные скролл-зоны ловят touch */
  html, body { height: auto; }
  body { overflow-x: hidden; }
  #app { display: block; height: auto; min-height: 100vh; min-height: 100dvh; overflow: visible; }
  .main-area { overflow: visible; }
  .content {
    display: block; overflow: visible;
    padding: 12px;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
  #page-content { overflow: visible; }

  .topbar {
    position: sticky; top: 0; z-index: 30;
    background: var(--bg-darker); /* непрозрачный: под sticky просвечивает контент */
    padding: 8px 12px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }
  .topbar-week-wrap { display: none; }

  /* Сайдбар → off-canvas drawer.
     !important нейтрализует desktop-состояние .collapsed (width:0) из localStorage */
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0;
    height: 100vh; height: 100dvh;
    z-index: 40;
    width: min(280px, 82vw) !important;
    padding: 16px !important;
    padding-top: max(16px, env(safe-area-inset-top)) !important;
    overflow-y: auto !important;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 4px 0 24px var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop {
    position: fixed; inset: 0; z-index: 39;
    background: rgba(0, 0, 0, 0.45);
  }
  body.drawer-open { overflow: hidden; }

  /* Тап-таргеты */
  .sidebar-toggle, .theme-toggle { width: 40px; height: 40px; }
  .nav-link { padding: 12px 14px; font-size: 14px; }
  .link-bitrix { display: inline-block; padding: 6px 0; }

  /* Экспорт скрыт: флоу скриншотит экраны на ширине вьюпорта — на телефоне
     получаются 390px-слайды; будущая замена — серверный /api/export/pptx */
  .export-wrap { display: none; }

  /* Таблицы → стековые карточки: Клиент/дни · название · статус/Bitrix */
  .data-table thead { display: none; }
  .data-table, .data-table tbody { display: block; }
  .data-table tr {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 2px 12px;
    padding: 12px 14px;
    border-top: 1px solid var(--border-subtle);
  }
  .data-table tr:first-child { border-top: none; }
  .data-table td { display: block; padding: 0; border: none; }
  .data-table tr:hover td { background: none; }
  .data-table tr.overdue { background: rgba(204, 44, 36, 0.03); }
  .data-table tr.overdue td { background: none; }
  .data-table td.cell-client { grid-column: 1; font-size: 14px; text-align: left; }
  .data-table td.cell-age    { grid-column: 2; grid-row: 1; text-align: right; }
  .data-table td.cell-title  { grid-column: 1 / -1; text-align: left; }
  .data-table td.cell-stage  { grid-column: 1; font-size: 12px; text-align: left; align-self: center; }
  .data-table td.cell-link   { grid-column: 2; grid-row: 3; text-align: right; }

  /* Аккордеон блокеров: строки-карточки — grid, colspan не работает →
     раскрытая строка становится блоком на всю ширину карточки */
  .data-table tr.blocker-row { display: block; padding: 10px 14px; }
  .data-table tr.blocker-row td { display: block; width: 100%; }
  /* На телефоне грид-колонки не влезают → обычный стек с переносом */
  .blocker-list { display: flex; flex-direction: column; gap: 10px; }
  .blocker-item { display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 14px; }
  .blocker-title { flex: 1 1 100%; }

  /* Обзор: стекинг блоков задан в @media 900px; здесь только переход
     на документный скролл вместо внутреннего */
  #page-content.ov-page { overflow: visible; height: auto; }
}

/* ══ Phone (≤480px) ══════════════════════════════════════════════════════ */
@media (max-width: 480px) {
  .topbar-info { display: none; }
  #sync-text   { display: none; } /* остаётся только цветная точка */

  .kpi-grid, .kpi-grid-3 { grid-template-columns: repeat(2, 1fr); }
  .kpi-value  { font-size: 22px; }
  .page-title { font-size: 19px; }

  /* Лейбл бара над треком */
  .bar-row   { flex-wrap: wrap; row-gap: 2px; }
  .bar-label { flex: 1 1 100%; width: auto; }
  .bar-pct   { width: 36px; }

  .chart-wrap       { height: 230px; }
  .chart-wrap-stage { height: 320px; } /* горизонтальному bar нужна высота на 10 стадий */
}

