:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #76756f;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --s1: #2a78d6;
  --s2: #eb6834;
  --good: #0f7b3f;  --good-bg: #e3f3e8;
  --warn: #8a5a00;  --warn-bg: #fdf0d2;
  --bad: #b42323;   --bad-bg: #fbe4e3;
  --wb: #8b2fc9; --ozon: #0059d6;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #33332f;
    --text: #ffffff; --text-2: #c3c2b7; --muted: #95948b;
    --accent: #3987e5; --s1: #3987e5; --s2: #d95926;
    --good: #5fcf8a; --good-bg: #173322; --warn: #f0b84a; --warn-bg: #3a2c0c; --bad: #ff8a85; --bad-bg: #3d1716;
    --wb: #c08ef0; --ozon: #6aa6ff;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #33332f;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #95948b;
  --accent: #3987e5; --s1: #3987e5; --s2: #d95926;
  --good: #5fcf8a; --good-bg: #173322; --warn: #f0b84a; --warn-bg: #3a2c0c; --bad: #ff8a85; --bad-bg: #3d1716;
  --wb: #c08ef0; --ozon: #6aa6ff;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 16px; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.r { text-align: right; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); border: 0; padding: 0; }

/* Каркас */
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.nav { background: var(--surface); border-right: 1px solid var(--border); padding: 16px 12px; display: flex; flex-direction: column; gap: 16px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; }
.logo { display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px; background: var(--text); color: var(--surface); font-size: 12px; font-weight: 700; }
.nav nav { display: flex; flex-direction: column; gap: 2px; }
.nav nav a, .nav nav .soon { padding: 8px 10px; border-radius: 8px; color: var(--text-2); }
.nav nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav nav a.on { background: var(--surface-2); color: var(--text); font-weight: 600; }
.nav nav .soon { color: var(--muted); opacity: .6; cursor: default; }
.nav nav .soon::after { content: " · скоро"; font-size: 11px; }
.nav-foot { margin-top: auto; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 13px; }
.nav-foot .me { color: var(--text-2); flex: 1 1 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.theme-btn, .link { background: none; border: 1px solid var(--border); color: var(--text-2); border-radius: 8px; padding: 4px 10px; cursor: pointer; font: inherit; }
main { padding: 24px 28px 48px; min-width: 0; max-width: 1400px; }
main.auth { display: grid; place-items: center; min-height: 100vh; padding: 16px; max-width: none; }

/* Карточки */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; margin-bottom: 16px; box-shadow: var(--shadow); min-width: 0; }
.card.narrow { max-width: 520px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.card-head h2 { margin: 0; }
.card.empty { text-align: center; padding: 40px 20px; }
.auth-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 12px; }
.auth-card h1 { margin: 8px 0 0; }
.page-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.page-head h1 { margin: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1.4fr; gap: 16px; }
.grid2 > .card { margin-bottom: 0; }
.note { margin: 12px 0 0; }

/* Формы */
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-2); }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; min-width: 0; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { resize: vertical; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.btn { font: inherit; cursor: pointer; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 8px; padding: 7px 12px; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.on { border-color: var(--accent); color: var(--accent); }
.stack { display: flex; flex-direction: column; gap: 12px; max-width: 560px; }
.row-form { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.row-form input { flex: 1 1 200px; }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px 12px; display: flex; flex-wrap: wrap; gap: 6px 16px; }
legend { font-size: 13px; color: var(--text-2); padding: 0 4px; }
.chk { flex-direction: row; align-items: center; gap: 6px; color: var(--text); }
.chk input { margin: 0; }
.perm-form { display: flex; flex-wrap: wrap; gap: 4px 14px; align-items: center; }
.perm-form input[type=password] { flex: 1 1 220px; margin-top: 4px; }
.actions, .actions-col { display: flex; gap: 6px; flex-wrap: wrap; }
.actions form { margin: 0; }
.actions-col { flex-direction: column; align-items: stretch; }

/* Фильтры: одна строка над графиками */
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
.filters input[type=search] { flex: 1 1 180px; max-width: 280px; }
.custom { display: inline-flex; gap: 4px; align-items: center; }
.custom input { padding: 6px 8px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { font: inherit; border: 0; background: none; color: var(--text-2); padding: 7px 12px; cursor: pointer; border-right: 1px solid var(--border); white-space: nowrap; }
.seg button:last-child { border-right: 0; }
.seg button.on { background: var(--surface-2); color: var(--text); font-weight: 600; }
.seg.small button { padding: 4px 10px; }
.tabs { display: flex; gap: 4px; margin-bottom: 12px; }
.tabs button { font: inherit; border: 1px solid var(--border); background: none; color: var(--text-2); padding: 6px 14px; border-radius: 8px; cursor: pointer; }
.tabs button.on { background: var(--surface-2); color: var(--text); font-weight: 600; }

/* KPI */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; min-width: 0; }
.kpi-label { color: var(--text-2); font-size: 13px; }
.kpi-value { font-size: 24px; font-weight: 650; letter-spacing: -.02em; margin: 2px 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-sub { color: var(--muted); font-size: 12px; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.small-kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.small-kpis .kpi-value { font-size: 20px; }
@media (max-width: 1100px) { .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.kpi-value.neg, td.neg, .neg { color: var(--bad); }
.pos { color: var(--good); }
.delta { font-weight: 600; color: var(--muted); }
.delta.up { color: var(--good); }
.delta.down { color: var(--bad); }

/* Таблицы */
.scroll-x { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl th { text-align: left; font-weight: 500; color: var(--muted); font-size: 12px; padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: bottom; }
.tbl th.r { text-align: right; }
.tbl td { padding: 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl td.warn { color: var(--bad); font-weight: 600; }
.clip { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mp { display: inline-block; font-size: 10px; font-weight: 700; padding: 1px 5px; border-radius: 4px; border: 1px solid currentColor; vertical-align: 1px; }
.mp-wb { color: var(--wb); }
.mp-ozon { color: var(--ozon); }
.badge { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.badge.ok { background: var(--good-bg); color: var(--good); }
.badge.low { background: var(--warn-bg); color: var(--warn); }
.badge.critical, .badge.out { background: var(--bad-bg); color: var(--bad); }
.badge.critical::before, .badge.out::before { content: "! "; font-weight: 700; }
.badge.dead { background: var(--surface-2); color: var(--muted); }
details.wh summary, details.table-view summary, details.replace-key summary { cursor: pointer; color: var(--accent); font-size: 12px; margin-top: 4px; }
details.wh ul { list-style: none; margin: 6px 0 0; padding: 0; max-width: 360px; }
details.wh li { display: flex; justify-content: space-between; gap: 12px; padding: 2px 0; border-bottom: 1px dashed var(--border); font-size: 12px; }
details.table-view { margin-top: 8px; }
details.table-view .tbl { margin-top: 8px; }

/* Уведомления */
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; background: var(--good-bg); color: var(--good); }
.flash.error { background: var(--bad-bg); color: var(--bad); }

/* График */
.chart { position: relative; width: 100%; min-height: 240px; }
.chart svg { display: block; overflow: visible; touch-action: pan-y; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis { stroke: var(--muted); stroke-width: 1; opacity: .6; }
.chart .tick { fill: var(--muted); font-size: 11px; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .line.s1 { stroke: var(--s1); }
.chart .line.s2 { stroke: var(--s2); }
.chart .bar.s1 { fill: var(--s1); }
.chart .cross { stroke: var(--muted); stroke-dasharray: 3 3; }
.chart .dot { stroke: var(--surface); stroke-width: 2; }
.chart .dot.s1 { fill: var(--s1); }
.chart .dot.s2 { fill: var(--s2); }
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--text-2); margin-bottom: 6px; min-height: 18px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.sw.s1 { background: var(--s1); }
.sw.s2 { background: var(--s2); }
.tip { position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: 0 4px 16px rgba(0,0,0,.12); white-space: nowrap; z-index: 2; }
.tip-h { font-weight: 600; margin-bottom: 4px; }

/* Мобильная версия */
@media (max-width: 900px) {
  .grid2 { grid-template-columns: 1fr; }
  .grid2 > .card { margin-bottom: 16px; }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .nav { position: sticky; top: 0; z-index: 5; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 10px 16px; gap: 8px 12px; border-right: 0; border-bottom: 1px solid var(--border); }
  .nav .brand span:last-child { display: none; }
  .nav nav { flex-direction: row; overflow-x: auto; flex: 1 1 100%; order: 3; min-width: 0; scrollbar-width: none; }
  .nav .brand { flex: 1 1 auto; }
  .nav .brand span:last-child { display: inline !important; }
  .nav nav a, .nav nav .soon { padding: 6px 10px; white-space: nowrap; }
  .nav nav .soon { display: none; }
  .nav-foot { margin: 0; flex-wrap: nowrap; }
  .nav-foot .me { display: none; }
  main { padding: 16px 16px 40px; }
  .kpis, .small-kpis { grid-template-columns: 1fr 1fr; gap: 8px; }
  .custom { flex: 1 1 100%; display: grid; grid-template-columns: minmax(0,1fr) minmax(0,1fr) auto; }
  .custom input { width: 100%; min-width: 0; padding: 6px; }
  .filters select { flex: 1 1 140px; }
  .kpi { padding: 10px 12px; }
  .kpi-value { font-size: 18px; }
  .filters .seg { max-width: 100%; overflow-x: auto; }
  .filters input[type=search] { max-width: none; }
  .card { padding: 14px; }
  .clip { max-width: 200px; }
}

/* Этап 2 */
.params { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.params label { width: 130px; }
.params input { width: 100%; }
.breakdown { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px 16px; margin: 0; }
.breakdown div { display: flex; justify-content: space-between; gap: 8px; border-bottom: 1px dashed var(--border); padding: 4px 0; font-size: 13px; }
.breakdown dt { color: var(--text-2); }
.breakdown dd { margin: 0; font-weight: 600; }
.cost-input { width: 110px; text-align: right; padding: 5px 8px; }
.sticky-save { position: sticky; bottom: 0; background: var(--surface); padding: 10px 0 2px; display: flex; gap: 10px; align-items: center; border-top: 1px solid var(--border); margin-top: 8px; }
.upload-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.steps { margin: 0 0 12px; padding-left: 20px; color: var(--text-2); }
.steps li { margin: 4px 0; }
.status-line { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
tr.urgent td:first-child { box-shadow: inset 3px 0 0 var(--bad); }
pre.diag { margin: 0; max-height: 220px; overflow: auto; font-size: 11px; white-space: pre-wrap; word-break: break-all; background: var(--surface-2); padding: 8px; border-radius: 6px; }
