:root {
  color-scheme: light;
  --bg: #f4f5f7; --surface: #ffffff; --surface-2: #f8f9fb; --line: #dfe3e8; --line-2: #eceef1;
  --text: #16181d; --text-2: #4d535c; --muted: #7a818c;
  --accent: #2a78d6; --accent-ink: #ffffff; --accent-soft: #e6f0fc;
  --good: #0ca30c; --good-soft: #e3f5e3; --warn: #b07800; --warn-soft: #fff4d6;
  --bad: #d03b3b; --bad-soft: #fbe5e5; --serious: #ec835a;
  --g0: #fbeee6; --g1: #fdf7e3; --g2: #eef6ea; --g3: #f1f2f4;
  --bar-track: #e8ebef; --bar-fact: #2a78d6; --bar-plan: #16181d;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121212; --surface: #1a1a19; --surface-2: #222220; --line: #3a3a37; --line-2: #2c2c2a;
    --text: #f2f2f0; --text-2: #c3c2b7; --muted: #8f8e86;
    --accent: #3987e5; --accent-ink: #ffffff; --accent-soft: #1d2d42;
    --good: #2fbf2f; --good-soft: #1b301b; --warn: #fab219; --warn-soft: #3a2f12; --bad: #e66767; --bad-soft: #3d1e1e;
    --g0: #2e2620; --g1: #2d2a1d; --g2: #1f2a1e; --g3: #262624;
    --bar-track: #2f2f2c; --bar-fact: #3987e5; --bar-plan: #f2f2f0;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121212; --surface: #1a1a19; --surface-2: #222220; --line: #3a3a37; --line-2: #2c2c2a;
  --text: #f2f2f0; --text-2: #c3c2b7; --muted: #8f8e86;
  --accent: #3987e5; --accent-ink: #ffffff; --accent-soft: #1d2d42;
  --good: #2fbf2f; --good-soft: #1b301b; --warn: #fab219; --warn-soft: #3a2f12; --bad: #e66767; --bad-soft: #3d1e1e;
  --g0: #2e2620; --g1: #2d2a1d; --g2: #1f2a1e; --g3: #262624;
  --bar-track: #2f2f2c; --bar-fact: #3987e5; --bar-plan: #f2f2f0;
  --shadow: 0 1px 2px rgba(0,0,0,.4);
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea { background: var(--surface); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; min-height: 32px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
button { border: 1px solid var(--line); background: var(--surface); border-radius: 6px; padding: 6px 12px; cursor: pointer; min-height: 32px; white-space: nowrap; }
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.danger { color: var(--bad); }
button.link { border: 0; background: none; color: var(--accent); padding: 2px 4px; min-height: 0; }
button:disabled { opacity: .5; cursor: default; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }

/* вход */
.login { max-width: 360px; margin: 12vh auto; background: var(--surface); padding: 28px; border-radius: 12px; box-shadow: var(--shadow); }
.login h1 { font-size: 20px; margin: 0 0 4px; }
.login label { display: block; margin: 14px 0 4px; color: var(--text-2); }
.login input { width: 100%; }
.login button { width: 100%; margin-top: 18px; }
.err { color: var(--bad); margin-top: 10px; min-height: 1.4em; }

/* каркас */
header.top { position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 12px; padding: 8px 16px; flex-wrap: wrap; }
header.top .brand { font-weight: 700; }
header.top select { max-width: 280px; }
nav.tabs { display: flex; gap: 2px; padding: 0 16px; background: var(--surface); border-bottom: 1px solid var(--line); overflow-x: auto; position: sticky; top: 49px; z-index: 19; }
nav.tabs a { padding: 10px 12px; text-decoration: none; color: var(--text-2); border-bottom: 2px solid transparent; white-space: nowrap; }
nav.tabs a.on { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
main { padding: 16px; max-width: 1800px; margin: 0 auto; }
h2 { font-size: 18px; margin: 4px 0 12px; }
h3 { font-size: 15px; margin: 18px 0 8px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 14px; margin-bottom: 14px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.filters select { max-width: 240px; }
.filters input[type=search] { width: 220px; }
.note { background: var(--accent-soft); border-radius: 8px; padding: 8px 12px; margin-bottom: 10px; color: var(--text-2); }
.warnbox { background: var(--warn-soft); border-radius: 8px; padding: 8px 12px; margin: 8px 0; }

/* таблицы */
.tablewrap { overflow: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); max-height: calc(100vh - 230px); }
table.grid { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 13px; }
table.grid th { position: sticky; top: 0; z-index: 2; background: var(--surface-2); text-align: left; font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; color: var(--text-2); }
table.grid th.sort { cursor: pointer; }
table.grid th.sort:hover { color: var(--accent); }
table.grid td { padding: 5px 8px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
table.grid td.num, table.grid th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
table.grid tr.work:hover td { background: var(--surface-2); }
table.grid tr.clickable { cursor: pointer; }
table.grid tr.g0 td { background: var(--g0); font-weight: 700; }
table.grid tr.g1 td { background: var(--g1); font-weight: 600; }
table.grid tr.g2 td { background: var(--g2); font-weight: 600; }
table.grid tr.g3 td, table.grid tr.g4 td, table.grid tr.g5 td { background: var(--g3); font-weight: 600; }
table.grid tr.dirty td { background: var(--warn-soft); }
table.grid td input.qty { width: 96px; text-align: right; min-height: 28px; padding: 3px 6px; }
table.grid td input.qty.has { border-color: var(--accent); font-weight: 600; }
table.grid .sub { color: var(--muted); font-size: 12px; }
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 10px; font-size: 12px; white-space: nowrap; background: var(--surface-2); color: var(--text-2); }
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.st-done { background: var(--good-soft); color: var(--text); } .st-done::before { background: var(--good); }
.st-behind { background: var(--warn-soft); color: var(--text); } .st-behind::before { background: var(--serious); }
.st-overdue { background: var(--bad-soft); color: var(--text); } .st-overdue::before { background: var(--bad); }
.st-on_track::before { background: var(--accent); }
.st-not_started::before { background: var(--muted); }
.neg { color: var(--bad); }
.pos { color: var(--good); }

/* сохранение */
.savebar { position: sticky; bottom: 0; z-index: 15; background: var(--surface); border-top: 1px solid var(--line); padding: 10px 16px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; box-shadow: 0 -4px 12px rgba(0,0,0,.06); margin: 0 -16px; }
.savebar input.reason { min-width: 320px; flex: 1; }

/* дашборд */
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; margin-bottom: 14px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; }
.kpi .v { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }
.kpi .l { color: var(--text-2); font-size: 12px; }
.dash { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 14px; }
@media (max-width: 1100px) { .dash { grid-template-columns: 1fr; } }
.pbar { position: relative; height: 10px; background: var(--bar-track); border-radius: 5px; min-width: 120px; }
.pbar .f { position: absolute; left: 0; top: 0; bottom: 0; background: var(--bar-fact); border-radius: 5px; }
.pbar .p { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--bar-plan); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-2); margin: 6px 0; flex-wrap: wrap; }
.legend i { display: inline-block; width: 12px; height: 8px; border-radius: 2px; margin-right: 4px; vertical-align: middle; }
.stack { display: flex; height: 22px; border-radius: 6px; overflow: hidden; gap: 2px; background: var(--surface); }
.stack div { height: 100%; }
.chart { position: relative; }
.chart svg { width: 100%; height: 160px; display: block; }
.chart .bar { fill: var(--bar-fact); }
.chart .bar:hover { opacity: .8; }
.chart text { fill: var(--muted); font-size: 10px; }
.tip { position: fixed; z-index: 100; pointer-events: none; background: var(--text); color: var(--bg); padding: 4px 8px; border-radius: 6px; font-size: 12px; }

/* модальные окна */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 50; display: flex; align-items: flex-start; justify-content: center; padding: 5vh 16px; overflow: auto; }
.modal { background: var(--surface); border-radius: 12px; box-shadow: var(--shadow); width: 100%; max-width: 760px; padding: 18px 20px; }
.modal.wide { max-width: 1100px; }
.modal h3 { margin-top: 0; }
.form { display: grid; grid-template-columns: 180px 1fr; gap: 8px 12px; align-items: center; }
.form label { color: var(--text-2); }
.form input, .form select, .form textarea { width: 100%; }
.form input[type=checkbox], .form input[type=radio] { width: auto; min-height: 0; }
table.grid td.name { min-width: 240px; max-width: 460px; }
@media (max-width: 640px) { .form { grid-template-columns: 1fr; } }
.actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }
.pw { font: 600 18px/1.4 ui-monospace, Consolas, monospace; background: var(--surface-2); padding: 8px 12px; border-radius: 6px; user-select: all; }
.checks { max-height: 220px; overflow: auto; border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px; }
.checks label { display: block; color: var(--text); }
.subtabs { display: flex; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }
.subtabs button.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.toast { position: fixed; right: 16px; bottom: 16px; z-index: 200; background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; max-width: 420px; box-shadow: var(--shadow); }
.toast.bad { background: var(--bad); color: #fff; }
.kind { font-size: 11px; padding: 0 6px; border-radius: 8px; background: var(--surface-2); color: var(--text-2); }
.kind-group { background: var(--g0); } .kind-agg { background: var(--g1); } .kind-work { background: var(--accent-soft); }
@media (max-width: 640px) {
  main { padding: 10px; }
  header.top { padding: 8px 10px; }
  nav.tabs { padding: 0 6px; top: 0; position: static; }
  .savebar input.reason { min-width: 0; }
  .hm { display: none; }
  table.grid td.name { min-width: 150px; }
  table.grid td input.qty { width: 80px; }
  .filters select, .filters input[type=search] { max-width: 100%; width: 100%; }
}
