:root {
  --bg: #f4f5fa;
  --panel: #ffffff;
  --panel-2: #fafbff;
  --text: #16181f;
  --muted: #6b7280;
  --border: #e6e8f1;
  --accent: #4f46e5;
  --accent-2: #6366f1;
  --accent-soft: #eef0ff;
  --green: #0f9d58;
  --green-soft: #e6f6ee;
  --red: #e5484d;
  --red-soft: #fdecec;
  --amber: #d97706;
  --amber-soft: #fdf3e2;
  --blue: #2563eb;
  --shadow: 0 1px 2px rgba(16, 20, 40, .04), 0 8px 24px -12px rgba(16, 20, 40, .18);
  --radius: 16px;
  --nav-w: 232px;
  --rail-w: 68px;
  --tabbar-h: 58px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}
[data-theme="dark"] {
  --bg: #0f1117;
  --panel: #161923;
  --panel-2: #1b1f2b;
  --text: #e8eaf1;
  --muted: #969db0;
  --border: #262b39;
  --accent: #7c83f5;
  --accent-2: #8b91f7;
  --accent-soft: #232744;
  --green: #34d399;
  --green-soft: #16302a;
  --red: #f87171;
  --red-soft: #341f22;
  --amber: #fbbf24;
  --amber-soft: #322a16;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 12px 28px -16px rgba(0,0,0,.7);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 "Inter", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
a { color: var(--accent); text-decoration: none; -webkit-tap-highlight-color: transparent; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, h4 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 24px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.tiny { font-size: 11px; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.center { text-align: center; }
.grow { flex: 1; }
.row { display: flex; align-items: center; gap: 10px; }
.col { display: flex; flex-direction: column; gap: 10px; }
.wrap { flex-wrap: wrap; }
.hidden { display: none !important; }

/* ---------- каркас ---------- */
.shell { display: grid; grid-template-columns: var(--nav-w) minmax(0, 1fr); min-height: 100vh; min-height: 100dvh; }
.sidebar {
  background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; height: 100dvh;
  z-index: 40;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 18px 14px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 9px; background: linear-gradient(135deg, #5b57e8, #8b5cf6);
  display: grid; place-items: center; color: #fff; font-weight: 800; font-size: 15px; flex: none;
}
.brand-name { font-weight: 750; font-size: 16px; letter-spacing: -.02em; }
.brand-sub { font-size: 10.5px; color: var(--muted); letter-spacing: .06em; text-transform: uppercase; }

.nav { padding: 6px 10px; display: flex; flex-direction: column; gap: 2px; overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 10px;
  color: var(--text); font-weight: 500; font-size: 13.5px; min-height: 40px;
}
.nav a:hover { background: var(--panel-2); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 650; }
.nav .ico { width: 18px; text-align: center; opacity: .8; flex: none; }
.nav .badge { margin-left: auto; font-size: 10.5px; background: var(--red); color: #fff; border-radius: 999px; padding: 1px 6px; }
.nav-sep { margin: 10px 8px 4px; font-size: 10.5px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); }

.side-foot { padding: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
.plan-chip {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 12px; padding: 9px 11px;
}
.plan-chip b { font-size: 12.5px; }

.main { min-width: 0; display: flex; flex-direction: column; }

/* шапка для мобильных */
.mheader {
  display: none; align-items: center; gap: 10px; padding: calc(8px + var(--safe-t)) 12px 8px;
  background: var(--panel); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 30;
}
.mheader .brand-name { font-size: 15px; }
.icon-btn {
  width: 40px; height: 40px; display: grid; place-items: center; border-radius: 10px;
  border: 1px solid var(--border); background: var(--panel); cursor: pointer; font-size: 16px;
  flex: none;
}

.topbar {
  position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--border);
  padding: 10px 22px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.content { padding: 20px 22px 60px; display: flex; flex-direction: column; gap: 16px; max-width: 1680px; width: 100%; min-width: 0; }
/* без этого длинные таблицы растягивают страницу вместо своей прокрутки */
.content > *, .grid > *, .card, .card-body, .col > * { min-width: 0; }
.table-wrap { max-width: 100%; }

/* затемнение под меню */
.scrim {
  position: fixed; inset: 0; background: rgba(10, 12, 20, .45); z-index: 35;
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
body.nav-open .scrim { opacity: 1; pointer-events: auto; }

/* нижняя навигация */
.tabbar {
  display: none; position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  background: color-mix(in srgb, var(--panel) 96%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-b);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  padding: 7px 2px; color: var(--muted); font-size: 10.5px; font-weight: 550; min-height: var(--tabbar-h);
  text-align: center;
}
.tabbar a.active { color: var(--accent); }
.tabbar .ico { font-size: 17px; line-height: 1; }

/* ---------- элементы форм ---------- */
.seg { display: inline-flex; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 2px; }
.seg button {
  border: 0; background: transparent; padding: 7px 11px; border-radius: 8px; cursor: pointer;
  font-size: 12.5px; font-weight: 550; color: var(--muted); white-space: nowrap; min-height: 32px;
}
.seg button.active { background: var(--accent-soft); color: var(--accent); }
.seg button:hover:not(.active) { color: var(--text); }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--border); background: var(--panel); border-radius: 999px;
  padding: 4px 11px; font-size: 12.5px; cursor: pointer; color: var(--muted); font-weight: 550;
  white-space: nowrap; min-height: 30px;
  /* названия магазинов показываем целиком: никакой обрезки и многоточий */
  max-width: none; overflow: visible; text-overflow: clip;
}
.chip .dot {
  width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .45;
  display: inline-block; margin-right: 6px; vertical-align: 1px;
}
.chip.active { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }

input[type=text], input[type=email], input[type=password], input[type=date], input[type=number], select, textarea {
  border: 1px solid var(--border); background: var(--panel); border-radius: 9px; padding: 8px 10px;
  outline: none; font-size: 14px; color: var(--text); min-width: 0; max-width: 100%;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
label.field > span { font-weight: 550; }

.btn {
  border: 1px solid var(--border); background: var(--panel); border-radius: 9px; padding: 8px 13px;
  cursor: pointer; font-size: 13px; font-weight: 550; display: inline-flex; align-items: center; gap: 7px;
  transition: .15s; min-height: 38px; justify-content: center;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); color: #fff; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--panel-2); color: var(--accent); }
.btn.sm { padding: 5px 10px; font-size: 12px; min-height: 32px; }
.btn.danger { color: var(--red); border-color: var(--red-soft); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.filters-btn { display: none; }

/* ---------- карточки ---------- */
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { padding: 14px 16px 0; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.card-head h2 { font-size: 15.5px; }
.card-body { padding: 14px 16px 16px; }
.grid { display: grid; gap: 14px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.g23 { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }
.g32 { grid-template-columns: minmax(0, 3fr) minmax(0, 1.2fr); }

/* KPI-плитки */
.kpi { padding: 14px 16px; position: relative; overflow: hidden; }
.kpi .label { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 5px; }
.kpi .value { font-size: 26px; font-weight: 700; letter-spacing: -.02em; margin-top: 4px; }
.kpi .value small { font-size: 14px; font-weight: 600; color: var(--muted); margin-left: 3px; }
.kpi .delta { display: inline-flex; align-items: center; gap: 3px; font-size: 11.5px; font-weight: 650; padding: 1px 7px; border-radius: 999px; margin-top: 6px; }
.delta.up { color: var(--green); background: var(--green-soft); }
.delta.down { color: var(--red); background: var(--red-soft); }
.delta.flat { color: var(--muted); background: var(--panel-2); }
.kpi .hint { font-size: 11px; color: var(--muted); margin-top: 3px; }
.kpi.accent { background: linear-gradient(160deg, var(--accent) 0%, #7c5cf0 100%); color: #fff; border: 0; }
.kpi.accent .label, .kpi.accent .hint { color: rgba(255,255,255,.82); }
.kpi.accent .delta { background: rgba(255,255,255,.18); color: #fff; }

/* таблицы */
.table-wrap { overflow: auto; border-radius: 12px; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 13px; }
th {
  text-align: left; font-weight: 600; font-size: 11.5px; text-transform: uppercase; letter-spacing: .03em;
  color: var(--muted); padding: 9px 10px; background: var(--panel-2); position: sticky; top: 0; z-index: 1;
  white-space: nowrap; border-bottom: 1px solid var(--border); cursor: default;
}
th.sortable { cursor: pointer; }
th.sortable:hover { color: var(--accent); }
td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:hover td { background: var(--panel-2); }
tbody tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
/* первая колонка закреплена: на телефоне видно, к какому товару относится строка */
.table-wrap th:first-child, .table-wrap td:first-child {
  position: sticky; left: 0; z-index: 2; background: var(--panel);
}
.table-wrap th:first-child { z-index: 3; background: var(--panel-2); }
.table-wrap tbody tr:hover td:first-child { background: var(--panel-2); }
.table-wrap th:first-child::after, .table-wrap td:first-child::after {
  content: ''; position: absolute; top: 0; right: 0; bottom: -1px; width: 1px; background: var(--border);
}
.name-cell { max-width: 460px; min-width: 250px; }
.name-cell .t {
  font-weight: 550; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; white-space: normal; word-break: break-word;
}
.name-cell .s { font-size: 11px; color: var(--muted); display: block; white-space: normal; word-break: break-word; }
.prod { display: flex; align-items: center; gap: 10px; min-width: 0; }
.thumb {
  width: 38px; height: 38px; border-radius: 9px; overflow: hidden; flex: none;
  background: var(--panel-2); border: 1px solid var(--border); display: grid; place-items: center;
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb span { font-size: 13px; font-weight: 700; color: var(--muted); }

.tag { display: inline-block; padding: 1px 7px; border-radius: 6px; font-size: 11px; font-weight: 650; white-space: nowrap; }
.tag.wb { background: #f3e8ff; color: #7e22ce; }
.tag.ozon { background: #dbeafe; color: #1d4ed8; }
[data-theme="dark"] .tag.wb { background: #341a4d; color: #d8b4fe; }
[data-theme="dark"] .tag.ozon { background: #172b52; color: #93c5fd; }
.tag.a { background: var(--green-soft); color: var(--green); }
.tag.b { background: var(--amber-soft); color: var(--amber); }
.tag.c { background: var(--panel-2); color: var(--muted); }
.tag.good { background: var(--green-soft); color: var(--green); }
.tag.bad { background: var(--red-soft); color: var(--red); }
.tag.warn { background: var(--amber-soft); color: var(--amber); }

.bar { height: 6px; border-radius: 3px; background: var(--border); overflow: hidden; min-width: 40px; flex: 1; }
.bar > i { display: block; height: 100%; border-radius: 3px; background: var(--accent); }
.bar.green > i { background: var(--green); }
.bar.red > i { background: var(--red); }

/* инсайты */
.insight { display: flex; gap: 12px; padding: 13px 15px; border-radius: 13px; border: 1px solid var(--border); background: var(--panel); }
.insight .ico { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; flex: none; font-size: 15px; }
.insight.critical .ico { background: var(--red-soft); color: var(--red); }
.insight.warn .ico { background: var(--amber-soft); color: var(--amber); }
.insight.info .ico { background: var(--accent-soft); color: var(--accent); }
.insight .t { font-weight: 650; font-size: 13.5px; }
.insight .d { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.insight .a { font-size: 12px; color: var(--accent); margin-top: 5px; }

/* воронка */
.funnel-row { display: flex; align-items: center; gap: 12px; margin-bottom: 9px; }
.funnel-label { width: 88px; font-size: 12.5px; color: var(--muted); flex: none; }
.funnel-bar { height: 30px; border-radius: 8px; background: linear-gradient(90deg, var(--accent), var(--accent-2)); opacity: .9; min-width: 4px; }
.funnel-val { font-weight: 650; font-variant-numeric: tabular-nums; }

/* графики */
.chart { width: 100%; height: 260px; display: block; overflow: visible; }
.chart .grid-line { stroke: var(--border); stroke-width: 1; stroke-dasharray: 3 4; }
.chart .axis { fill: var(--muted); font-size: 10.5px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.legend i { width: 9px; height: 9px; border-radius: 3px; display: inline-block; margin-right: 5px; }

/* тарифы */
.plan-card { padding: 16px; display: flex; flex-direction: column; gap: 8px; position: relative; }
.plan-card.popular { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.plan-card .price { font-size: 26px; font-weight: 750; letter-spacing: -.02em; }
.plan-card ul { margin: 4px 0 0; padding-left: 16px; font-size: 12.5px; color: var(--muted); }
.plan-card li { margin: 3px 0; }

/* вход */
.auth-wrap { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 20px;
  background: radial-gradient(1200px 500px at 20% -10%, #e8e7ff 0%, transparent 60%), var(--bg); }
[data-theme="dark"] .auth-wrap { background: radial-gradient(1200px 500px at 20% -10%, #1e1b4b 0%, transparent 60%), var(--bg); }
.auth-card { width: 100%; max-width: 420px; padding: 26px; }
.auth-card h1 { font-size: 22px; margin-bottom: 4px; }

/* прочее */
.toast {
  position: fixed; right: 20px; bottom: calc(20px + var(--safe-b)); z-index: 100;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 13px;
  box-shadow: var(--shadow); max-width: 420px;
}
.empty { padding: 40px; text-align: center; color: var(--muted); }
.spinner { width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading { display: flex; align-items: center; gap: 10px; padding: 30px; justify-content: center; color: var(--muted); }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; font-size: 13px; }
.kv > div:nth-child(even) { font-variant-numeric: tabular-nums; text-align: right; font-weight: 550; }
.hr { height: 1px; background: var(--border); margin: 10px 0; }
.note { font-size: 12px; color: var(--muted); background: var(--panel-2); border: 1px dashed var(--border); border-radius: 10px; padding: 10px 12px; }

/* ================= планшет ================= */
@media (max-width: 1199px) {
  :root { --nav-w: var(--rail-w); }
  .brand { padding: 16px 0 12px; justify-content: center; }
  .brand > div:not(.brand-mark) { display: none; }
  .nav { padding: 6px 8px; }
  .nav a { justify-content: center; padding: 10px 0; }
  .nav a > span:not(.ico):not(.badge) { display: none; }
  .nav-sep { display: none; }
  .side-foot .plan-chip > div { display: none; }
  .side-foot .plan-chip { flex-direction: column; padding: 6px; }
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g3, .g23, .g32 { grid-template-columns: 1fr; }
  .content { padding: 16px 16px 40px; }
  .topbar { padding: 10px 16px; }
}

/* ================= телефон ================= */
@media (max-width: 899px) {
  :root { --nav-w: 0px; }
  .shell { grid-template-columns: minmax(0, 1fr); }
  .mheader { display: flex; }
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; height: 100dvh; width: 272px;
    transform: translateX(-100%); transition: transform .22s ease; box-shadow: var(--shadow);
    padding-top: var(--safe-t);
  }
  body.nav-open .sidebar { transform: none; }
  .sidebar .brand { padding: 16px; justify-content: flex-start; }
  .sidebar .brand > div:not(.brand-mark) { display: block; }
  .nav a { justify-content: flex-start; padding: 11px 12px; }
  .nav a > span:not(.ico):not(.badge) { display: inline; }
  .nav-sep { display: block; }
  .side-foot .plan-chip { flex-direction: row; padding: 9px 11px; }
  .side-foot .plan-chip > div { display: block; }
  .tabbar { display: flex; }
  .content { padding: 12px 12px calc(var(--tabbar-h) + 24px + var(--safe-b)); gap: 12px; }
  .topbar {
    position: static; padding: 8px 12px 10px; gap: 8px; border-bottom: 0;
    background: transparent; backdrop-filter: none;
    display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center;
  }
  .topbar .seg { grid-column: 1; width: 100%; overflow-x: auto; }
  .topbar .seg button { flex: 1 0 auto; }
  .topbar .filters-btn { grid-column: 2; display: inline-flex; }
  /* даты, маркетплейсы и магазины — только после нажатия «Фильтры» */
  .topbar .dates, .topbar .chips.filters, .topbar #refresh { display: none; grid-column: 1 / -1; }
  body.filters-open .topbar .dates { display: flex; gap: 6px; width: 100%; }
  body.filters-open .topbar .dates input { flex: 1; min-width: 0; }
  body.filters-open .topbar .chips.filters {
    display: flex; width: 100%; flex-wrap: nowrap; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none;
  }
  body.filters-open .topbar #refresh { display: inline-flex; width: 100%; }
  .topbar .chips::-webkit-scrollbar { display: none; }
  .topbar .chip { flex: 0 0 auto; }
  .topbar .chip-divider { width: 1px; align-self: stretch; background: var(--border); flex: none; margin: 2px 2px; }
  .filters-btn.active { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
  .topbar .grow { display: none; }
  h1 { font-size: 20px; }
  .kpi { padding: 12px 13px; }
  .kpi .value { font-size: 21px; }
  .card-head { padding: 12px 13px 0; }
  .card-body { padding: 12px 13px 14px; }
  .table-wrap { margin: 0 -4px; }
  table { font-size: 12.5px; }
  th, td { padding: 9px 8px; }
  .name-cell { max-width: 230px; min-width: 200px; }
  .name-cell .t { -webkit-line-clamp: 3; }
  .prod { gap: 8px; }
  .thumb { width: 32px; height: 32px; border-radius: 8px; }
  .chart { height: 200px; }
  .toast { left: 12px; right: 12px; bottom: calc(var(--tabbar-h) + 12px + var(--safe-b)); max-width: none; }
  .funnel-label { width: 74px; font-size: 11.5px; }
  .funnel-bar { height: 24px; }
  .plan-card .price { font-size: 22px; }
  /* iOS зумит страницу, если поле мельче 16 px */
  input[type=text], input[type=email], input[type=password], input[type=date], input[type=number], select, textarea {
    font-size: 16px;
  }
}

@media (max-width: 560px) {
  .g2 { grid-template-columns: minmax(0, 1fr); }
  .kpi .value { font-size: 19px; }
  .insight .ico { width: 26px; height: 26px; }
}

/* горизонтальная ориентация телефона: меню-рейка не нужна, но и не мешает */
@media (max-height: 480px) and (max-width: 899px) {
  .tabbar { display: none; }
  .content { padding-bottom: 20px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation-duration: .01ms !important; }
}

/* печать отчёта «на бумагу» */
@media print {
  .sidebar, .topbar, .mheader, .tabbar, .scrim, .toast { display: none !important; }
  .shell { grid-template-columns: 1fr; }
  .card { box-shadow: none; border-color: #ccc; break-inside: avoid; }
}
