/* MP Analytics v2 — чистая архитектура.
   Цветовые переменные, компоненты по секциям, никакого глобального бардака. */

:root {
  --bg: #fafbfc;
  --bg-soft: #f5f6f9;
  --card: #ffffff;
  --card-hover: #fafbfc;
  --border: #e6e8ed;
  --text: #0f172a;
  --text-dim: #64748b;
  --accent: #2563eb;
  --green: #059669;
  --red: #dc2626;
  --yellow: #d97706;
  --wb: #7c3aed;
  --ozon: #2563eb;
  --ms: #0d9488;
  --shadow-sm: 0 1px 2px rgba(15,23,42,0.04);
  --shadow-md: 0 4px 16px rgba(15,23,42,0.06);
  --header-bg: rgba(255,255,255,0.92);
  --tip-bg: #1f2937;
  --tip-text: #ffffff;
  --rail-w: 248px;
}
[data-theme="dark"] {
  --bg: #0b1220;
  --bg-soft: #0f1828;
  --card: #131c2e;
  --card-hover: #18223a;
  --border: #243049;
  --text: #e6edf7;
  --text-dim: #93a3bd;
  --accent: #60a5fa;
  --green: #34d399;
  --red: #f87171;
  --yellow: #fbbf24;
  --wb: #a78bfa;
  --ozon: #60a5fa;
  --ms: #2dd4bf;
  --shadow-md: 0 4px 16px rgba(0,0,0,0.3);
  --header-bg: rgba(11,18,32,0.92);
  --tip-bg: #f9fafb;
  --tip-text: #0f172a;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  font-size: 14px;
  line-height: 1.45;
}

/* ─── Header ─────────────────────────────────────────────────────────── */
.app-header {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  background: var(--header-bg);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.hdr-left { display: flex; align-items: center; gap: 8px; }
.hdr-logo { font-weight: 700; font-size: 18px; }
.hdr-version {
  font-size: 11px;
  color: var(--accent);
  background: rgba(37,99,235,0.10);
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
}
.hdr-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: background .15s;
}
.hdr-btn:hover { background: var(--card-hover); }

/* ─── Main + rail ────────────────────────────────────────────────────── */
.app-main {
  max-width: 1400px;
  margin: 0 auto;
  padding: 24px;
  padding-bottom: 80px;
}
@media (min-width: 1281px) {
  body { padding-right: var(--rail-w); }
  .app-header { margin-right: calc(-1 * var(--rail-w)); }
}

/* ─── Filters bar ────────────────────────────────────────────────────── */
.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
  align-items: center;
  margin-bottom: 20px;
  padding: 14px 18px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-sm);
}
.chips, .period-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}
.chip-label, .period-label {
  font-size: 12px;
  color: var(--text-dim);
  font-weight: 600;
  margin-right: 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.chip, .period-chip {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
}
.chip:hover, .period-chip:hover { background: var(--card-hover); }
.chip.active, .period-chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.period-control { display: flex; flex-direction: column; gap: 8px; }
.period-custom {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  font-size: 13px;
}
.period-custom.hidden { display: none; }
.period-custom input[type=date] {
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
}
.period-apply {
  background: var(--accent);
  border: none;
  color: #fff;
  padding: 6px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
}
.period-apply:hover { filter: brightness(1.1); }
.period-error { color: var(--red); font-size: 12px; }

/* ─── Sections ───────────────────────────────────────────────────────── */
.dash-section { margin-bottom: 24px; scroll-margin-top: 80px; }
.dash-section:empty { display: none; }
.section-head {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 12px;
}
.section-head h2 {
  font-size: 18px;
  font-weight: 700;
  margin: 0;
}
.section-sub {
  font-size: 13px;
  color: var(--text-dim);
}

/* ─── KPI cards ──────────────────────────────────────────────────────── */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}
.kpi-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.kpi-label {
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.kpi-value {
  font-size: 24px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 6px;
}
.kpi-sub {
  font-size: 12px;
  color: var(--text-dim);
}
.delta {
  font-size: 12px;
  font-weight: 600;
  margin-left: 6px;
  white-space: nowrap;
}
.delta.delta-up { color: var(--green); }
.delta.delta-down { color: var(--red); }

/* ─── Tooltip — кастомный, заменяет native title ─────────────────────── */
.tip-anchor {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 10px;
  font-weight: 700;
  cursor: help;
  user-select: none;
  transition: background .15s, color .15s, border-color .15s;
}
.tip-anchor:hover { background: var(--accent); color: #fff; border-color: var(--accent); }

.tip-bubble {
  position: fixed;
  z-index: 9999;
  max-width: 320px;
  padding: 10px 12px;
  background: var(--tip-bg);
  color: var(--tip-text);
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.4;
  box-shadow: 0 8px 24px rgba(0,0,0,0.3);
  opacity: 0;
  pointer-events: none;
  transition: opacity .12s;
}
.tip-bubble.visible { opacity: 1; }

/* ─── Sources ────────────────────────────────────────────────────────── */
.src-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}
.src-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
}
.src-card.src-ms { background: rgba(13,148,136,0.10); border-color: rgba(13,148,136,0.3); }
.src-label { font-weight: 600; margin-bottom: 4px; }
.src-tm { font-size: 12px; color: var(--text-dim); margin-bottom: 8px; }
.src-flags { display: flex; gap: 6px; }
.src-flag {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(5,150,105,0.10);
  color: var(--green);
  font-weight: 600;
}
.src-flag.off { background: rgba(220,38,38,0.10); color: var(--red); }

/* ─── Nav rail (desktop) ─────────────────────────────────────────────── */
.nav-rail {
  position: fixed;
  top: 64px;
  right: 0;
  bottom: 0;
  width: var(--rail-w);
  border-left: 1px solid var(--border);
  background: var(--bg);
  padding: 18px 12px;
  overflow-y: auto;
  display: none;
}
@media (min-width: 1281px) {
  .nav-rail { display: block; }
}
.nav-rail-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 0 10px 8px;
}
.nav-rail-list, .nav-sheet-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.nav-item {
  position: relative;
  display: block;
  padding: 8px 12px;
  border-radius: 8px;
  text-decoration: none !important;
  color: var(--text-dim) !important;
  font-size: 13px;
  transition: background .15s, color .15s;
}
.nav-item:link, .nav-item:visited { color: var(--text-dim) !important; }
.nav-item:hover { background: var(--card-hover); color: var(--text) !important; }
.nav-item.active {
  background: rgba(37,99,235,0.10);
  color: var(--accent) !important;
  font-weight: 600;
}
.nav-item.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 6px;
  bottom: 6px;
  width: 3px;
  background: var(--accent);
  border-radius: 2px;
}

/* ─── Nav FAB + sheet (mobile) ───────────────────────────────────────── */
.nav-fab {
  position: fixed;
  right: 18px;
  bottom: 18px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(37,99,235,0.35);
  z-index: 60;
  display: block;
}
@media (min-width: 1281px) { .nav-fab { display: none; } }

.nav-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  z-index: 70;
}
.nav-backdrop.open { opacity: 1; pointer-events: auto; }

.nav-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--card);
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  padding: 18px 14px 28px;
  max-height: 70vh;
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform .22s ease-out;
  z-index: 80;
}
.nav-sheet.open { transform: translateY(0); }
.nav-sheet-title {
  font-weight: 700;
  font-size: 14px;
  margin-bottom: 8px;
  padding-left: 6px;
}
@media (min-width: 1281px) {
  .nav-sheet, .nav-backdrop { display: none !important; }
}

/* ─── Print ──────────────────────────────────────────────────────────── */
@media print {
  body { padding-right: 0 !important; }
  .app-header { position: static; margin-right: 0 !important; }
  .nav-rail, .nav-fab, .nav-sheet, .nav-backdrop { display: none !important; }
  .filters { display: none; }
}

/* ─── Empty state ────────────────────────────────────────────────────── */
.card.empty {
  background: var(--card);
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 24px;
  text-align: center;
  color: var(--text-dim);
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-sm);
  padding: 16px;
}
.kpi-card.kpi-warn .kpi-value { color: var(--yellow); }
.kpi-card.kpi-accent .kpi-value { color: var(--accent); }
.val-positive { color: var(--green); font-weight: 600; }
.val-warn { color: var(--yellow); font-weight: 600; }
.cell-warn { color: var(--red); font-weight: 600; }

/* ─── MP cards ───────────────────────────────────────────────────────── */
.mp-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  gap: 14px;
}
.mp-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
  box-shadow: var(--shadow-sm);
}
.mp-card-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  flex-wrap: wrap;
  gap: 6px;
}
.mp-badge {
  font-size: 11px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.mp-badge.wb   { background: rgba(124,58,237,0.12); color: var(--wb); }
.mp-badge.ozon { background: rgba(37,99,235,0.12); color: var(--ozon); }
.mp-card-meta { font-size: 12px; color: var(--text-dim); }
.mp-metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
}
.mp-metric { padding: 10px 0; }
.mp-m-label {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 4px;
  text-transform: uppercase;
}
.mp-m-value { font-size: 18px; font-weight: 700; margin-bottom: 4px; }
.mp-m-sub { font-size: 12px; color: var(--text-dim); }

.mp-tag {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 6px;
  font-weight: 600;
}
.mp-tag.wb   { background: rgba(124,58,237,0.10); color: var(--wb); }
.mp-tag.ozon { background: rgba(37,99,235,0.10); color: var(--ozon); }

/* ─── Funnel ─────────────────────────────────────────────────────────── */
.funnel-card { padding: 18px; }
.funnel-row { margin-bottom: 16px; }
.funnel-row-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
  font-size: 13px;
}
.funnel-row-value { font-weight: 700; }
.funnel-hint { font-size: 11px; color: var(--text-dim); font-weight: 500; }
.funnel-bar, .funnel-split {
  display: flex;
  height: 36px;
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg-soft);
  border: 1px solid var(--border);
}
.funnel-seg {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 8px;
}
.funnel-seg.realized { background: linear-gradient(90deg, #475569, #334155); width: 100%; }
.funnel-seg.withhold { background: var(--yellow); }
.funnel-seg.payout   { background: var(--green); }
.funnel-seg.other    { background: #94a3b8; }
.funnel-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.funnel-legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}
.funnel-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}
.funnel-dot.withhold { background: var(--yellow); }
.funnel-dot.payout   { background: var(--green); }
.funnel-dot.other    { background: #94a3b8; }
.funnel-legend-label { color: var(--text); }
.funnel-legend-pct   { font-weight: 600; color: var(--text-dim); }
.funnel-legend-sum   { color: var(--text-dim); }

/* ─── Account fin ────────────────────────────────────────────────────── */
.acc-fin-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 14px;
}
.acc-fin-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  box-shadow: var(--shadow-sm);
}
.acc-fin-title {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.acc-fin-mp { margin-bottom: 12px; }
.acc-fin-mp:last-child { margin-bottom: 0; }
.acc-fin-mp-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.acc-fin-period { font-size: 11px; color: var(--text-dim); }
.acc-fin-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  font-size: 13px;
  border-bottom: 1px dashed var(--border);
}
.acc-fin-row:last-child { border-bottom: none; }
.acc-fin-row span:first-child { color: var(--text-dim); }

/* ─── Tables ─────────────────────────────────────────────────────────── */
.table-card { padding: 0; overflow-x: auto; }
.table-title {
  font-size: 13px;
  font-weight: 700;
  margin: 0;
  padding: 14px 16px 8px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.data-table th, .data-table td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.data-table th {
  background: var(--bg-soft);
  color: var(--text-dim);
  font-weight: 600;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.data-table td.num, .data-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table tbody tr:hover { background: var(--card-hover); }
.table-foot { padding: 10px 14px; font-size: 12px; color: var(--text-dim); text-align: center; }

/* ─── ABC ────────────────────────────────────────────────────────────── */
.abc-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
.abc-card {
  padding: 18px;
  border-radius: 12px;
  border: 1px solid var(--border);
  text-align: center;
  background: var(--card);
}
.abc-letter { font-size: 32px; font-weight: 800; }
.abc-count { font-size: 12px; color: var(--text-dim); margin: 4px 0; }
.abc-rev { font-size: 18px; font-weight: 700; }
.abc-share { font-size: 11px; color: var(--text-dim); }
.abc-card.abc-a { background: rgba(5,150,105,0.08); border-color: rgba(5,150,105,0.3); }
.abc-card.abc-a .abc-letter { color: var(--green); }
.abc-card.abc-b { background: rgba(217,119,6,0.08); border-color: rgba(217,119,6,0.3); }
.abc-card.abc-b .abc-letter { color: var(--yellow); }
.abc-card.abc-c { background: rgba(107,114,128,0.10); border-color: rgba(107,114,128,0.3); }
.abc-card.abc-c .abc-letter { color: var(--text-dim); }
.abc-badge {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
}
.abc-badge.abc-a { background: rgba(5,150,105,0.15); color: var(--green); }
.abc-badge.abc-b { background: rgba(217,119,6,0.15); color: var(--yellow); }
.abc-badge.abc-c { background: rgba(107,114,128,0.15); color: var(--text-dim); }

/* ─── Chart ──────────────────────────────────────────────────────────── */
.chart-card { padding: 14px; }
.chart-wrap { position: relative; height: 320px; }

/* ─── Rhythm ─────────────────────────────────────────────────────────── */
.rhythm-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 8px;
}
@media (max-width: 720px) { .rhythm-grid { grid-template-columns: repeat(2, 1fr); } }
.rhythm-cell {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  text-align: center;
}
.rhythm-day { font-weight: 700; font-size: 12px; }
.rhythm-bar {
  height: 6px;
  background: var(--bg-soft);
  border-radius: 3px;
  margin: 6px 0;
  overflow: hidden;
}
.rhythm-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
  transition: width .3s;
}
.rhythm-val { font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
.rhythm-sub { font-size: 11px; color: var(--text-dim); }

/* ─── Tags / urgency ─────────────────────────────────────────────────── */
.urg-critical { color: var(--red); font-weight: 600; }
.urg-high     { color: var(--yellow); font-weight: 600; }
.urg-medium   { color: var(--text-dim); }
.flag-not_in_ms  { color: var(--red); font-weight: 600; }
.flag-stock_diff { color: var(--yellow); }
.flag-not_on_mp  { color: var(--text-dim); }
