/* === SUB Analytics Dashboard — Mono Gray + Color Data === */
:root {
  --bg: #edeef0;
  --bg-card: #ffffff;
  --bg-sidebar: #ffffff;
  --border: #d9dbdf;
  --text: #111111;
  --text-muted: #777777;
  --accent: #333333;
  --accent-hover: #111111;
  --green: #16a34a;
  --red: #dc2626;
  --yellow: #ca8a04;
  --orange: #ea580c;
  --blue: #2563eb;
  --sidebar-w: 200px;
  --topnav-h: 52px;
}

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

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-feature-settings: 'cv01', 'cv02', 'cv03', 'cv04';
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* Tabular numbers in data contexts */
td.num, th.num, .kpi-card .value, .pin-cell, .num {
  font-variant-numeric: tabular-nums;
}

/* ============================================================
   TOP NAVBAR — visible on desktop (>1100px)
   ============================================================ */
.topnav {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--topnav-h);
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
  z-index: 100;
  align-items: center;
  padding: 0 20px;
  gap: 4px;
}

.topnav-logo {
  font-size: 15px;
  font-weight: 800;
  color: var(--text);
  margin-right: 20px;
  white-space: nowrap;
  letter-spacing: -.3px;
}

.topnav-links {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
}

.topnav-links a,
.topnav-links .topnav-drop-trigger {
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 6px 11px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  border-radius: 6px;
  transition: background .12s, color .12s;
  white-space: nowrap;
  cursor: pointer;
}

.topnav-links a:hover,
.topnav-links .topnav-drop-trigger:hover {
  background: rgba(0,0,0,.04);
  color: var(--text);
}

.topnav-links a.active,
.topnav-links .topnav-drop-trigger.active {
  background: rgba(0,0,0,.06);
  color: var(--text);
  font-weight: 600;
}

/* Dropdown menus in topnav */
.topnav-dropdown {
  position: relative;
}

.drop-arrow {
  font-size: 9px;
  opacity: .5;
  margin-left: 2px;
}

.topnav-drop-menu {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  padding: 8px 4px 4px;
  min-width: 160px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,.1);
  z-index: 200;
}

.topnav-dropdown:hover .topnav-drop-menu {
  display: block;
}

.topnav-drop-menu a {
  display: block;
  padding: 8px 12px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 13px;
  border-radius: 6px;
  transition: background .1s;
}

.topnav-drop-menu a:hover {
  background: rgba(0,0,0,.04);
  color: var(--text);
}

.topnav-drop-menu a.active {
  color: var(--text);
  font-weight: 600;
  background: rgba(0,0,0,.05);
}

/* ============================================================
   SIDEBAR — visible on tablet (768–1100px)
   ============================================================ */
.sidebar {
  display: none;
  width: var(--sidebar-w);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  padding: 16px 0;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  overflow-y: auto;
  z-index: 100;
}

.sidebar-logo {
  padding: 0 16px 14px;
  font-size: 15px;
  font-weight: 800;
  color: var(--text);
  letter-spacing: -.3px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 4px;
}

.sidebar nav a {
  display: flex;
  align-items: center;
  min-height: 36px;
  padding: 4px 16px 4px 24px;
  color: var(--text);
  text-decoration: none;
  font-size: 13px;
  font-weight: 450;
  transition: background .12s, color .12s;
  border-left: 3px solid transparent;
}

.sidebar nav a:hover {
  background: rgba(0,0,0,.03);
}

.sidebar nav a.active {
  background: rgba(0,0,0,.05);
  font-weight: 600;
  border-left-color: var(--text);
}

/* Nav group labels (sidebar + mobile) */
.nav-group-label {
  padding: 18px 16px 5px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .8px;
  color: var(--text-muted);
}

.nav-sep {
  height: 1px;
  background: var(--border);
  margin: 12px 16px;
}

/* ============================================================
   MAIN CONTENT
   ============================================================ */
.main {
  flex: 1;
  padding: 16px 20px;
  min-width: 0;
}

.page-title {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 12px;
  color: var(--text);
}

/* === KPI Cards === */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 8px;
  margin-bottom: 14px;
}

.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
}

.kpi-card .label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .4px;
  margin-bottom: 3px;
}

.kpi-card .value {
  font-size: 18px;
  font-weight: 700;
}

.kpi-card .change {
  font-size: 11px;
  margin-top: 2px;
}

.change.up { color: var(--green); }
.change.down { color: var(--red); }

/* === Cards & Sections === */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
  margin-bottom: 12px;
}

.card-title {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 8px;
  color: var(--text);
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.grid-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 12px;
}

/* === Tables === */
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

th {
  text-align: left;
  padding: 5px 8px;
  font-weight: 600;
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .4px;
  border-bottom: 1px solid var(--border);
}

td {
  padding: 5px 8px;
  border-bottom: 1px solid var(--border);
}

tr:hover td { background: rgba(0,0,0,.02); }

td.num, th.num { text-align: right; }

/* === Color helpers for data === */
.c-red { color: var(--red); }
.c-grn { color: var(--green); }
.c-yel { color: var(--yellow); }

/* === ABC colors === */
.abc-a { color: var(--green); font-weight: 600; }
.abc-b { color: var(--yellow); font-weight: 600; }
.abc-c { color: var(--red); font-weight: 600; }
th.sortable:hover { background: rgba(0,0,0,.05); }
th.sort-asc::after { content: " \25B2"; font-size: 10px; opacity: .6; }
th.sort-desc::after { content: " \25BC"; font-size: 10px; opacity: .6; }

/* === Risk levels === */
.risk-high { color: var(--red); font-weight: 600; }
.risk-medium { color: var(--orange); font-weight: 600; }
.risk-low { color: var(--green); font-weight: 600; }

/* === Charts === */
.chart-container {
  position: relative;
  width: 100%;
  height: 320px;
}

.chart-container canvas {
  width: 100% !important;
  height: 100% !important;
}

/* === Retention Heatmap === */
.heatmap-table { overflow-x: auto; display: block; }
.heatmap-table td {
  text-align: center;
  font-size: 12px;
  padding: 6px 8px;
  min-width: 54px;
}

.heat-100 { background: rgba(0,0,0,.14); }
.heat-75  { background: rgba(0,0,0,.10); }
.heat-50  { background: rgba(0,0,0,.06); }
.heat-25  { background: rgba(0,0,0,.03); }
.heat-0   { background: transparent; }

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover { background: var(--accent-hover); }

.btn-secondary {
  background: var(--bg-card);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-secondary:hover { background: rgba(0,0,0,.03); }

/* === Status messages === */
.msg {
  padding: 12px 16px;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 13px;
}

.msg-success { background: rgba(22,163,74,.08); color: var(--green); border: 1px solid rgba(22,163,74,.2); }
.msg-error { background: rgba(220,38,38,.08); color: var(--red); border: 1px solid rgba(220,38,38,.2); }

/* === Decomposition bar === */
.decomp-row {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.decomp-item {
  flex: 1;
  text-align: center;
  padding: 8px;
  border-radius: 6px;
  background: rgba(0,0,0,.02);
  border: 1px solid var(--border);
}

.decomp-item .decomp-label {
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.decomp-item .decomp-value {
  font-size: 16px;
  font-weight: 700;
}

/* === Scrollable tables === */
.table-wrap {
  overflow-x: auto;
}

/* === Compact price table === */
.price-scroll {
  overflow: auto;
  max-height: 70vh;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.price-tbl {
  font-size: 11.5px;
  white-space: nowrap;
  border-collapse: separate;
  border-spacing: 0;
}

.price-tbl th,
.price-tbl td {
  padding: 5px 8px;
}

.price-tbl thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--bg-card);
  cursor: pointer;
  user-select: none;
  border-bottom: 2px solid var(--border);
  font-size: 11px;
}

/* Sticky first two cols (index + product) */
.price-tbl .sticky-col {
  position: sticky;
  z-index: 1;
  background: var(--bg-card);
}

.price-tbl thead .sticky-col { z-index: 3; }

.price-tbl .col-idx  { left: 0; width: 28px; min-width: 28px; }
.price-tbl .col-prod { left: 28px; max-width: 200px; overflow: hidden; text-overflow: ellipsis;
  border-right: 1px solid var(--border); }

/* Row hover for sticky cols too */
.price-tbl tr:hover .sticky-col { background: #f5f5f5; }
.price-tbl tr:hover td { background: #f5f5f5; }

/* Helper classes */
.price-tbl .muted { color: var(--text-muted); }
.price-tbl .bold  { font-weight: 600; }
.price-tbl .c-red { color: var(--red); }
.price-tbl .c-grn { color: var(--green); }
.price-tbl .c-yel { color: var(--yellow); }

/* ============================================================
   RESPONSIVE BREAKPOINTS
   ============================================================ */

/* Desktop: >1100px — top navbar, full width content */
@media (min-width: 1101px) {
  .topnav  { display: flex; }
  .sidebar { display: none; }
  .main    { margin-left: 0; margin-top: var(--topnav-h); }
}

/* Tablet: 768–1100px — sidebar */
@media (min-width: 769px) and (max-width: 1100px) {
  .topnav  { display: none; }
  .sidebar { display: block; }
  .main    { margin-left: var(--sidebar-w); margin-top: 0; }
  .grid-2  { grid-template-columns: 1fr; }
  .grid-3  { grid-template-columns: 1fr; }

  /* Inline 3-col grids: 2-col on tablet */
  .dash-alerts { grid-template-columns: 1fr 1fr !important; }
  [style*="grid-template-columns:1fr 1fr 1fr"] { grid-template-columns: 1fr 1fr !important; }

  /* Charts: slightly reduced */
  .chart-container { height: 260px; }
}

/* Export button */
.btn-export {
  display: inline-block;
  padding: 5px 14px;
  font-size: 12px;
  color: var(--text-muted);
  text-decoration: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  cursor: pointer;
  transition: background .15s, color .15s;
}
.btn-export:hover {
  background: var(--bg);
  color: var(--text);
}

/* Loading spinner */
.spinner {
  display: inline-block;
  width: 16px; height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--text);
  border-radius: 50%;
  animation: spin .6s linear infinite;
  vertical-align: middle;
  margin-left: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Search input in navbar */
.search-input {
  padding: 5px 10px;
  font-size: 13px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  width: 160px;
}
.search-input:focus {
  border-color: var(--accent);
}

/* ============================================================
   MOBILE NAV — hamburger + slide-out menu (<768px)
   ============================================================ */
.mobile-toggle {
  display: none;
  position: fixed;
  top: 8px; left: 8px;
  z-index: 200;
  width: 44px; height: 44px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  box-shadow: 0 1px 4px rgba(0,0,0,.1);
}

.mobile-toggle span {
  display: block;
  width: 20px;
  height: 2px;
  background: var(--text);
  border-radius: 1px;
}

.mobile-nav {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 150;
  background: rgba(0,0,0,.3);
  opacity: 0;
  visibility: hidden;
  transition: opacity .2s, visibility .2s;
}

.mobile-nav.open {
  opacity: 1;
  visibility: visible;
}

.mobile-nav-inner {
  position: absolute;
  top: 0; left: 0; bottom: 0;
  width: 280px;
  background: var(--bg-card);
  padding: 20px 16px;
  overflow-y: auto;
  transform: translateX(-100%);
  transition: transform .25s ease;
}

.mobile-nav.open .mobile-nav-inner {
  transform: translateX(0);
}

.mobile-nav-inner a {
  display: block;
  padding: 10px 12px 10px 20px;
  color: var(--text);
  text-decoration: none;
  font-size: 15px;
  font-weight: 450;
  border-radius: 8px;
  border-left: 3px solid transparent;
}

.mobile-nav-inner a:hover {
  background: rgba(0,0,0,.04);
}

.mobile-nav-inner a.active {
  background: rgba(0,0,0,.06);
  font-weight: 600;
  border-left-color: var(--text);
}

.mobile-nav-inner .nav-group-label {
  padding: 18px 12px 5px;
  font-size: 11px;
}

.mobile-nav-inner .nav-sep {
  margin: 10px 0;
}

/* Mobile: <768px — hamburger visible, no other nav */
@media (max-width: 768px) {
  .topnav  { display: none; }
  .sidebar { display: none; }
  .mobile-toggle { display: flex; }
  .mobile-nav { display: block; }
  .main    { margin-left: 0; margin-top: 0; padding: 10px; padding-top: 54px; }
  .grid-2  { grid-template-columns: 1fr !important; }
  .grid-3  { grid-template-columns: 1fr !important; }

  /* Override inline grids on mobile */
  .dash-alerts { grid-template-columns: 1fr !important; }
  .dash-today-grid { grid-template-columns: 1fr !important; }

  /* Touch-friendly nav links */
  .mobile-nav-inner a {
    min-height: 44px;
    display: flex;
    align-items: center;
  }

  /* Responsive tables: horizontal scroll */
  .table-wrap, .card { overflow-x: auto; }

  /* Charts: reduce height on mobile */
  .chart-container { height: 220px; }

  /* Buttons: min touch target */
  .btn, .btn-export, .btn-primary, .btn-secondary {
    min-height: 40px;
    padding: 8px 14px;
  }

  /* Inputs: full width on mobile */
  select, input[type="text"], input[type="date"], input[type="number"] {
    max-width: 100%;
  }
  .search-input { width: 100% !important; }

  /* KPI cards: 2 columns on mobile */
  .kpi-row {
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  /* Price table: allow horizontal scroll, remove sticky on narrow screens */
  .price-tbl .sticky-col {
    position: static;
  }
  .price-tbl { font-size: 11px; }

  /* Hide low-priority columns on mobile */
  .hide-mobile { display: none !important; }

  /* Heatmap: allow scroll */
  .heatmap-table td { min-width: 44px; padding: 4px 2px; }

  /* Charts: cap all inline chart heights */
  .chart-container, [style*="height:320px"], [style*="height:350px"],
  [style*="height:400px"], [style*="height:360px"] {
    max-height: 220px !important;
  }
  [style*="height:300px"] { max-height: 200px !important; }
  [style*="height:280px"] { max-height: 200px !important; }

  /* Form inputs: responsive widths */
  input[type="text"], input[type="number"], input[type="date"], select, textarea {
    max-width: 100% !important;
    min-width: 0 !important;
  }

  /* Images: responsive (not canvas — Chart.js needs explicit size) */
  img { max-width: 100% !important; height: auto !important; }

  /* Inline flex containers: wrap on mobile */
  [style*="display:flex"] { flex-wrap: wrap !important; }

  /* Inline 2-col and 3-col grids: stack */
  [style*="grid-template-columns:1fr 1fr 1fr"] { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns:1fr 1fr"] { grid-template-columns: 1fr !important; }

  /* Decomp row: stack */
  .decomp-row { flex-direction: column; }

  /* Page title: slightly smaller */
  .page-title { font-size: 15px; }
}

/* === Section tabs (Товары и Склад) === */
.section-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 16px;
  border-bottom: 2px solid var(--border);
}

.section-tabs a {
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  text-decoration: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: color .15s, border-color .15s;
}

.section-tabs a:hover {
  color: var(--text);
}

.section-tabs a.active {
  color: var(--text);
  font-weight: 600;
  border-bottom-color: var(--text);
}

/* Print styles */
@media print {
  .topnav, .sidebar, .search-input, .btn-export, .mobile-toggle, .mobile-nav { display: none !important; }
  .main { margin: 0 !important; padding: 10px !important; }
  .card { box-shadow: none !important; border: 1px solid #ddd; break-inside: avoid; }
  .kpi-card { box-shadow: none !important; border: 1px solid #ddd; }
  body { background: #fff; }
}
