:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e3e2de;
  --grid: #ebeae6;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #4a3aa7;
  --f0: #86b6ef; --f1: #5598e7; --f2: #2a78d6; --f3: #1c5cab; --f4: #104281;
  --b0: #c9c8c2; --b1: #86b6ef; --b2: #3987e5; --b3: #1c5cab; --b4: #0d366b;
  --good: #0ca30c; --good-ink: #006300; --warning: #fab219; --warning-ink: #8a5a00;
  --serious: #ec835a; --critical: #d03b3b; --critical-ink: #b02a2a;
  --good-bg: #e7f5e7; --warning-bg: #fff4dc; --critical-bg: #fbe6e6; --info-bg: #e8f0fb;
  --shadow: 0 1px 2px rgba(0,0,0,.04);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #2e2e2b; --grid: #2a2a28;
    --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #3987e5; --accent-ink: #86b6ef;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #9085e9;
    --f0: #184f95; --f1: #1c5cab; --f2: #2a78d6; --f3: #5598e7; --f4: #9ec5f4;
    --b0: #4a4a46; --b1: #184f95; --b2: #2a78d6; --b3: #6da7ec; --b4: #b7d3f6;
    --good-ink: #5fd05f; --warning-ink: #fab219; --critical-ink: #f08080;
    --good-bg: #16301a; --warning-bg: #352a10; --critical-bg: #3a1a1a; --info-bg: #162538;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #232321; --border: #2e2e2b; --grid: #2a2a28;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #3987e5; --accent-ink: #86b6ef;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #9085e9;
  --f0: #184f95; --f1: #1c5cab; --f2: #2a78d6; --f3: #5598e7; --f4: #9ec5f4;
  --b0: #4a4a46; --b1: #184f95; --b2: #2a78d6; --b3: #6da7ec; --b4: #b7d3f6;
  --good-ink: #5fd05f; --warning-ink: #fab219; --critical-ink: #f08080;
  --good-bg: #16301a; --warning-bg: #352a10; --critical-bg: #3a1a1a; --info-bg: #162538;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent-ink); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 650; }
h3 { font-size: 13px; margin: 16px 0 8px; font-weight: 650; color: var(--text-2); }
.muted { color: var(--muted); }
.num, td.n, th.n { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

/* шапка */
.top { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.top-in { max-width: 1320px; margin: 0 auto; padding: 0 20px; display: flex; align-items: center; gap: 20px; min-height: 54px; }
.brand { font-weight: 700; white-space: nowrap; color: var(--text); }
.brand span { color: var(--muted); font-weight: 500; }
.nav { display: flex; gap: 2px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.nav a { padding: 16px 10px 14px; color: var(--text-2); border-bottom: 2px solid transparent; white-space: nowrap; }
.nav a:hover { color: var(--text); text-decoration: none; }
.nav a.on { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.who { display: flex; align-items: center; gap: 10px; white-space: nowrap; color: var(--text-2); }
.who form { margin: 0; }

.page { max-width: 1320px; margin: 0 auto; padding: 20px; }
.head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.head .sub { color: var(--muted); margin-top: 4px; }

/* выбор периода */
.periods { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.chip { border: 1px solid var(--border); background: var(--surface); color: var(--text-2); border-radius: 999px;
  padding: 5px 11px; font-size: 13px; cursor: pointer; }
.chip:hover { text-decoration: none; border-color: var(--muted); color: var(--text); }
.chip.on { background: var(--text); color: var(--surface); border-color: var(--text); }
.periods form { display: flex; gap: 6px; align-items: center; margin: 0; }
input, select, button, textarea { font: inherit; color: var(--text); }
input[type=date], input[type=text], input[type=password], input[type=number], input[type=month], select, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 7px; padding: 5px 8px; }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.btn { background: var(--accent); color: #fff; border: 0; border-radius: 7px; padding: 7px 14px; cursor: pointer; font-weight: 600; display: inline-block; }
.btn:hover { text-decoration: none; filter: brightness(1.07); }
.btn.ghost { background: transparent; color: var(--text-2); border: 1px solid var(--border); font-weight: 500; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.linkbtn { background: none; border: 0; color: var(--accent-ink); cursor: pointer; padding: 0; }

/* сетка */
.grid { display: grid; gap: 14px; }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.span2 { grid-column: span 2; }
@media (max-width: 1000px) { .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .g3 { grid-template-columns: 1fr 1fr; } }
@media (max-width: 680px) { .g3, .g2 { grid-template-columns: 1fr; } .g4 { grid-template-columns: 1fr 1fr; gap: 10px; } .span2 { grid-column: auto; } }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.card + .card, .grid + .card, .card + .grid, .grid + .grid { margin-top: 14px; }
.grid > .card { margin-top: 0; }
.card-h { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; margin-bottom: 10px; }
.card-h h2 { margin: 0; }
.card-h .muted { font-size: 12px; }

/* плитки показателей */
.tile .label { color: var(--text-2); font-size: 13px; }
.tile .value { font-size: 26px; font-weight: 650; margin-top: 4px; letter-spacing: -.01em; font-variant-numeric: tabular-nums; }
.tile .foot { display: flex; gap: 8px; align-items: baseline; margin-top: 4px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.delta { font-weight: 600; font-variant-numeric: tabular-nums; }
.delta.good { color: var(--good-ink); }
.delta.bad { color: var(--critical-ink); }
.delta.flat { color: var(--muted); }
.hero .value { font-size: 40px; }

/* отделы на обзоре */
.dept-card .dept-h { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.dept-card .dept-h a { font-weight: 650; color: var(--text); font-size: 15px; }
.dept-card .big { font-size: 28px; font-weight: 650; font-variant-numeric: tabular-nums; }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; margin-top: 10px; font-size: 13px; }
.kv dt { color: var(--text-2); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; font-weight: 550; }

/* сигналы */
.signals { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.signals li { padding: 9px 12px; border-radius: 8px; display: flex; gap: 10px; align-items: flex-start; }
.signals li.critical { background: var(--critical-bg); }
.signals li.warning { background: var(--warning-bg); }
.signals li.info { background: var(--info-bg); }
.ico { flex: none; width: 18px; height: 18px; border-radius: 50%; display: inline-grid; place-items: center; font-size: 11px; font-weight: 800; color: #fff; }
.ico.critical { background: var(--critical); }
.ico.warning { background: var(--warning); color: #3a2800; }
.ico.info, .ico.none { background: var(--muted); }
.ico.good { background: var(--good); }

/* статусы KPI: цвет всегда с иконкой и подписью */
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 2px 9px 2px 3px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge.good { background: var(--good-bg); color: var(--good-ink); }
.badge.warning { background: var(--warning-bg); color: var(--warning-ink); }
.badge.critical { background: var(--critical-bg); color: var(--critical-ink); }
.badge.none { background: var(--surface-2); color: var(--muted); padding-left: 9px; }
.meter { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; min-width: 60px; }
.meter i { display: block; height: 100%; border-radius: 3px; }
.meter i.good { background: var(--good); } .meter i.warning { background: var(--warning); } .meter i.critical { background: var(--critical); }
.score { font-size: 32px; font-weight: 700; font-variant-numeric: tabular-nums; }

/* таблицы */
.tbl-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; font-weight: 600; color: var(--text-2); font-size: 12px; padding: 7px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 7px 8px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--surface-2); }
td.total, tr.total td { font-weight: 650; }
tr.sub td { color: var(--text-2); }
.neg { color: var(--critical-ink); }

/* графики */
svg.chart { width: 100%; height: auto; display: block; overflow: visible; }
svg.chart .grid { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
svg.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
svg.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; }
svg.chart .area { stroke: none; opacity: .10; }
svg.chart .dot { stroke: var(--surface); stroke-width: 2; }
svg.chart .hit { fill: transparent; }
svg.chart .hit:hover { fill: var(--text); fill-opacity: .05; }
.line.s1 { stroke: var(--s1); } .line.s2 { stroke: var(--s2); } .line.s3 { stroke: var(--s3); } .line.s4 { stroke: var(--s4); }
.area.s1, .dot.s1, .bar.s1 { fill: var(--s1); } .area.s2, .dot.s2, .bar.s2 { fill: var(--s2); }
.area.s3, .dot.s3, .bar.s3 { fill: var(--s3); } .dot.s4, .bar.s4 { fill: var(--s4); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12px; color: var(--text-2); margin: 8px 0 4px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.key { display: inline-block; width: 10px; height: 10px; border-radius: 3px; }
.key.s1 { background: var(--s1); } .key.s2 { background: var(--s2); } .key.s3 { background: var(--s3); } .key.s4 { background: var(--s4); }

.hbars { display: grid; gap: 7px; }
.hb-row { display: grid; grid-template-columns: minmax(90px, 34%) 1fr; gap: 10px; align-items: center; font-size: 13px; }
.hb-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text-2); }
.hb-track { display: flex; align-items: center; gap: 8px; min-width: 0; }
.hb-bar { height: 14px; border-radius: 0 4px 4px 0; flex: none; max-width: calc(100% - 72px); }
.hb-bar.s1 { background: var(--s1); } .hb-bar.s2 { background: var(--s2); } .hb-bar.s3 { background: var(--s3); }
.hb-val { font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 550; }

.funnel { display: grid; gap: 8px; }
.fn-row { display: grid; grid-template-columns: 130px 1fr 150px; gap: 10px; align-items: center; }
.fn-label { color: var(--text-2); font-size: 13px; }
.fn-track { background: var(--surface-2); border-radius: 4px; height: 22px; overflow: hidden; }
.fn-bar { height: 100%; border-radius: 0 4px 4px 0; }
.fn-bar.f0 { background: var(--f0); } .fn-bar.f1 { background: var(--f1); } .fn-bar.f2 { background: var(--f2); }
.fn-bar.f3 { background: var(--f3); } .fn-bar.f4 { background: var(--f4); }
.fn-val { font-variant-numeric: tabular-nums; font-size: 13px; }
@media (max-width: 680px) { .fn-row { grid-template-columns: 90px 1fr 90px; } .fn-val .muted { display: none; } }

.stack { display: flex; gap: 2px; height: 22px; border-radius: 4px; overflow: hidden; background: var(--surface); }
.seg { min-width: 3px; }
.seg.b0, .key.b0 { background: var(--b0); } .seg.b1, .key.b1 { background: var(--b1); } .seg.b2, .key.b2 { background: var(--b2); }
.seg.b3, .key.b3 { background: var(--b3); } .seg.b4, .key.b4 { background: var(--b4); }

details.tableview { margin-top: 8px; }
details.tableview summary { cursor: pointer; color: var(--muted); font-size: 12px; }

/* формы */
.form-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.form-row label { display: grid; gap: 4px; font-size: 12px; color: var(--text-2); }
.note { background: var(--info-bg); border-radius: 8px; padding: 10px 12px; font-size: 13px; }
.note.ok { background: var(--good-bg); }
.note.err { background: var(--critical-bg); }
.plan-tbl input[type=text] { width: 120px; text-align: right; }
.plan-tbl input.w { width: 56px; }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
pre { background: var(--surface-2); padding: 10px 12px; border-radius: 8px; overflow-x: auto; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 14px; }
.method p, .method li { max-width: 820px; }
.method h2 { margin-top: 26px; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: 100%; max-width: 360px; }
.login form { display: grid; gap: 12px; }
.login label { display: grid; gap: 5px; font-size: 13px; color: var(--text-2); }
.login input { padding: 9px 10px; }

@media (max-width: 680px) {
  .page { padding: 16px; }
  .top-in { padding: 0 16px; gap: 0 10px; flex-wrap: wrap; }
  .brand { flex: 1; padding: 12px 0 4px; }
  .nav { order: 3; flex-basis: 100%; }
  .nav a { padding: 8px 10px 10px; }
  .tile { padding: 12px; }
  .grid.g4.depts { grid-template-columns: 1fr; }
  .periods form { flex-wrap: wrap; }
  .periods input[type=date] { flex: 1; min-width: 0; width: 40%; }
  .tile .foot span:not(.delta) { display: none; }
  .hero .value, .tile .value { font-size: 21px; }
  .who .uname { display: none; }
}
@media print {
  .top, .periods, .no-print { display: none !important; }
  body { background: #fff; }
  .card { box-shadow: none; break-inside: avoid; }
}
.stack-form { display: grid; gap: 10px; }
.stack-form label { display: grid; gap: 4px; font-size: 12px; color: var(--text-2); }
.stack-form .form-row label { flex: 1; min-width: 120px; }
.stack-form input[type=text], .stack-form select { width: 100%; }
.stack-form .btn { justify-self: start; }
[hidden] { display: none !important; }
details.cancel summary { cursor: pointer; color: var(--accent-ink); font-size: 12px; list-style: none; white-space: nowrap; }
details.cancel summary::-webkit-details-marker { display: none; }
details.cancel[open] summary { color: var(--muted); }
