/* ==========================================================================
   QWT Shell — 3 зоны (sidebar / topbar / cas / content) + bento-утилиты.
   Responsive: desktop 3col → tablet 2col → mobile 1col.
   Все интерактивные элементы ≥44px по высоте строки/кнопки.
   ========================================================================== */

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: var(--text-base);
  line-height: var(--leading-base);
  -webkit-font-smoothing: antialiased;
}

/* Числа — всегда моно + tabular */
.num, td.num, .kpi-value, .lamp-score {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

/* ---------- 3-зонный шелл + CAS-строка ---------- */

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: auto auto 1fr;
  grid-template-areas:
    "sidebar topbar"
    "sidebar cas"
    "sidebar content";
  min-height: 100vh;
}

.sidebar {
  grid-area: sidebar;
  background: var(--bg-panel);
  border-right: var(--bw) solid var(--border);
  display: flex;
  flex-direction: column;
  padding: var(--space-4) var(--space-3);
  gap: var(--space-4);
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: var(--text-md);
  letter-spacing: 0.04em;
  padding: 0 var(--space-2);
  min-height: var(--touch-min);
}

.sidebar-nav { display: flex; flex-direction: column; gap: 2px; }

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: var(--touch-min);
  padding: 0 var(--space-3);
  border-radius: var(--r-sm);
  color: var(--text-secondary);
  text-decoration: none;
  font-size: var(--text-base);
  transition: background var(--dur-input) var(--ease-out),
              color var(--dur-input) var(--ease-out);
}
.sidebar-nav a:hover { background: var(--bg-raised); color: var(--text-primary); }
.sidebar-nav a[aria-current="page"] {
  background: var(--bg-raised);
  color: var(--text-primary);
  box-shadow: inset 2px 0 0 var(--live); /* live-семантика: текущий контекст */
}
.sidebar-nav a:focus-visible,
button:focus-visible, [tabindex]:focus-visible, input:focus-visible {
  outline: 2px solid var(--live);   /* фокус = live-семантика */
  outline-offset: 1px;
}

.sidebar-foot { margin-top: auto; color: var(--text-muted); font-size: var(--text-sm); padding: 0 var(--space-2); }

.main { display: contents; }

.topbar {
  grid-area: topbar;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  align-content: center;
  row-gap: var(--space-1);
  gap: var(--space-3);
  padding: var(--space-1) var(--space-4);
  min-height: var(--topbar-h);
  background: var(--bg-panel);
  border-bottom: var(--bw) solid var(--border);
  position: sticky;
  top: 0;
  z-index: 30;
}

/* ---------- CAS-строка: аварийная зона под топбаром (все экраны).
   Норма = высота 0. Пробой лимита = reverse-video coral, тап → причина. ---------- */
.cas {
  grid-area: cas;
  position: sticky; top: var(--topbar-h); z-index: 29;
  height: 0;
}
.cas[data-active="true"] {
  height: auto; min-height: 32px;
  display: flex; align-items: center;
  padding: var(--space-1) var(--space-4);
  background: var(--alert);  /* reverse-video: фон coral, текст почти чёрный (7:1) */
  color: #06070A;
  font-family: var(--font-mono); font-size: var(--text-sm); font-weight: 700;
  letter-spacing: .02em;
  cursor: pointer;
}
.cas-pop {
  position: absolute; top: calc(100% + 4px); left: var(--space-4);
  display: block; max-width: 480px;
  background: var(--bg-raised); color: var(--text-primary);
  border: var(--bw) solid var(--alert); border-radius: var(--r-sm);
  padding: var(--space-3) var(--space-4);
  font-family: var(--font-ui); font-weight: 400; letter-spacing: 0;
  font-size: var(--text-sm); box-shadow: var(--shadow-2); z-index: 61;
}

.content {
  grid-area: content;
  padding: var(--space-4);
  max-width: var(--content-max);
  width: 100%;
  margin: 0 auto;
  min-width: 0; /* не давать min-content детей (flex-фильтры, таблицы) раздувать grid-колонку */
}

/* ---------- Bento-утилиты (12-колоночный модуль) ---------- */

.bento {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: var(--space-3);
}

.span-2  { grid-column: span 2; }
.span-3  { grid-column: span 3; }
.span-4  { grid-column: span 4; }
.span-6  { grid-column: span 6; }
.span-8  { grid-column: span 8; }
.span-12 { grid-column: span 12; }

.cell {
  background: var(--bg-panel);
  border: var(--bw) solid var(--border);
  border-radius: var(--r-md);
  padding: var(--space-4);
  box-shadow: var(--shadow-1);
  min-width: 0; /* чтобы svg/таблицы не раздували грид */
}

.cell-title {
  margin: 0 0 var(--space-3);
  font-size: var(--text-sm);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
}

/* Переключатель layout-пресетов — жёсткая сегментация, не свободный драг */
.presets {
  display: flex;
  border: var(--bw) solid var(--border);
  border-radius: var(--r-sm);
  overflow: hidden;
}
.presets button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: var(--text-sm);
  min-height: var(--touch-min);
  padding: 0 var(--space-4);
  cursor: pointer;
  transition: background var(--dur-input) var(--ease-out),
              color var(--dur-input) var(--ease-out);
}
.presets button + button { border-left: var(--bw) solid var(--border); }
.presets button[aria-pressed="true"] {
  background: var(--bg-raised);
  color: var(--text-primary);
}

/* Кнопка AI-оверлея в топбаре (AI = слой, не роут) */
.ai-btn {
  appearance: none; display: inline-flex; align-items: center; gap: var(--space-2);
  min-height: var(--touch-min); padding: 0 var(--space-3);
  background: transparent; border: var(--bw) solid var(--border);
  border-radius: var(--r-sm); color: var(--text-secondary);
  font: inherit; font-size: var(--text-sm); cursor: pointer;
}
.ai-btn[aria-pressed="true"] { background: var(--bg-raised); color: var(--text-primary); }
.ai-btn kbd {
  font-family: var(--font-mono); font-size: var(--text-xs);
  border: var(--bw) solid var(--border-strong); border-radius: 4px;
  padding: 1px 5px; color: var(--text-secondary);
}

/* ---------- Responsive: 3col → 2col → 1col ---------- */

@media (max-width: 1100px) {
  .bento { grid-template-columns: repeat(6, 1fr); }
  .span-2, .span-3 { grid-column: span 3; }
  .span-4, .span-6 { grid-column: span 6; }
  .span-8, .span-12 { grid-column: span 6; }
}

/* Иконки навигации */
.sidebar-nav .ic { width: 16px; height: 16px; flex: none; }

/* ---------- Планшет 721–1200px: сайдбар → icon-rail 60px ---------- */

@media (min-width: 721px) and (max-width: 1200px) {
  :root { --sidebar-w: 60px; }
  .sidebar { padding: var(--space-3) var(--space-2); align-items: center; }
  .sidebar-logo { padding: 0; justify-content: center; }
  .sidebar-logo .lb, .sidebar-nav a .lb { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .sidebar-nav { align-items: center; }
  .sidebar-nav a { justify-content: center; padding: 0; width: var(--touch-min); }
  .sidebar-foot { display: none; }
}

/* ---------- Мобайл ≤720px: bottom tab bar (5 пунктов, прибит к низу) ---------- */

@media (max-width: 720px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto 1fr;
    grid-template-areas: "topbar" "cas" "content";
  }
  .sidebar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
    flex-direction: row; align-items: stretch;
    border-right: 0;
    border-top: var(--bw) solid var(--border);
    padding: 0 0 env(safe-area-inset-bottom, 0px);
  }
  .sidebar-logo, .sidebar-foot { display: none; }
  .sidebar-nav { flex-direction: row; width: 100%; justify-content: space-around; }
  .sidebar-nav a {
    flex: 1; flex-direction: column; justify-content: center; align-items: center;
    gap: 2px; padding: var(--space-1) var(--space-2);
    min-height: 56px; font-size: var(--text-xs);
  }
  /* 07.08 (consilium): жёсткий display:none на 6-м табе (Настройки) убран —
     табы доступны все; запасной вход в Настройки — ⚙ в топбаре (виден ≤720px) */
  .bento { grid-template-columns: 1fr; }
  .span-2, .span-3, .span-4, .span-6, .span-8, .span-12 { grid-column: span 1; }
  .content {
    padding: var(--space-3);
    padding-bottom: calc(56px + var(--space-5) + env(safe-area-inset-bottom, 0px));
  }
  .topbar { flex-wrap: wrap; height: auto; min-height: var(--topbar-h); padding: var(--space-2) var(--space-3); }
  .cas { position: static; } /* топбар на мобайле резиновый — CAS просто строкой в потоке */
}

/* ---------- Reduced motion: grid-boot и хром-переходы отключаются ---------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
