:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --surface-3: #eef0f3;
  --border: #e2e5ea;
  --border-strong: #cfd4db;
  --text: #121821;
  --text-2: #444d5a;
  --muted: #6b7480;
  --nav: #13263d;
  --nav-text: #c9d4e2;
  --nav-active: #ffffff;
  --accent: #2a78d6;
  --accent-soft: #e6f0fc;
  --accent-text: #1c5cab;
  --s-warm: #2a78d6;
  --s-cold: #eb6834;
  --s-remote: #1baf7a;
  --s-abandon: #eda100;
  --good: #0ca30c;
  --good-text: #006300;
  --good-soft: #e5f5e5;
  --warn: #fab219;
  --warn-text: #8a5a00;
  --warn-soft: #fff4dc;
  --bad: #d03b3b;
  --bad-text: #b02a2a;
  --bad-soft: #fbe9e9;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, .18);
  --radius: 10px;
  --font: "Inter", "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111214; --surface: #1a1a19; --surface-2: #202120; --surface-3: #2a2b2a;
    --border: #2f302e; --border-strong: #42433f; --text: #f0efec; --text-2: #c3c2b7; --muted: #9a998f;
    --nav: #0c0d0e; --nav-text: #a9b0ba; --nav-active: #ffffff;
    --accent: #3987e5; --accent-soft: #1d2b3d; --accent-text: #86b6ef;
    --s-warm: #3987e5; --s-cold: #d95926; --s-remote: #199e70; --s-abandon: #c98500;
    --good-text: #4cc74c; --good-soft: #17301a; --warn-text: #f3c35a; --warn-soft: #3a2e12;
    --bad: #e05555; --bad-text: #f08a8a; --bad-soft: #3b1d1d;
    --shadow: 0 1px 2px rgba(0,0,0,.4); --shadow-lg: 0 12px 32px rgba(0,0,0,.55);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111214; --surface: #1a1a19; --surface-2: #202120; --surface-3: #2a2b2a;
  --border: #2f302e; --border-strong: #42433f; --text: #f0efec; --text-2: #c3c2b7; --muted: #9a998f;
  --nav: #0c0d0e; --nav-text: #a9b0ba; --nav-active: #ffffff;
  --accent: #3987e5; --accent-soft: #1d2b3d; --accent-text: #86b6ef;
  --s-warm: #3987e5; --s-cold: #d95926; --s-remote: #199e70; --s-abandon: #c98500;
  --good-text: #4cc74c; --good-soft: #17301a; --warn-text: #f3c35a; --warn-soft: #3a2e12;
  --bad: #e05555; --bad-text: #f08a8a; --bad-soft: #3b1d1d;
  --shadow: 0 1px 2px rgba(0,0,0,.4); --shadow-lg: 0 12px 32px rgba(0,0,0,.55);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 var(--font); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-text); }
.hidden { display: none !important; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow-lg); padding: 28px; }
.login-card h1 { margin: 12px 0 4px; font-size: 20px; }
.login-card p { margin: 0 0 20px; color: var(--muted); }
.login-card label { display: block; margin: 12px 0 4px; font-weight: 600; font-size: 13px; }
.login-card .btn { width: 100%; margin-top: 18px; justify-content: center; height: 40px; }
.login-error { color: var(--bad-text); margin-top: 10px; min-height: 20px; font-size: 13px; }
.brand-mark { width: 36px; height: 36px; border-radius: 9px; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; }

/* ---------- layout */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar { background: var(--nav); color: var(--nav-text); padding: 18px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 4px; }
.sidebar .brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 20px; color: #fff; font-weight: 650; line-height: 1.2; }
.sidebar .brand small { display: block; color: var(--nav-text); font-weight: 400; font-size: 12px; }
.nav-link { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px; color: var(--nav-text); text-decoration: none; font-weight: 500; }
.nav-link:hover { background: rgba(255,255,255,.06); color: #fff; }
.nav-link.active { background: rgba(255,255,255,.12); color: var(--nav-active); }
.nav-link svg { width: 18px; height: 18px; flex: none; }
.nav-link .count { margin-left: auto; background: rgba(255,255,255,.14); border-radius: 10px; padding: 0 7px; font-size: 11px; }
.sidebar .spacer { flex: 1; }
.sidebar .me { font-size: 12px; padding: 8px 10px; border-top: 1px solid rgba(255,255,255,.08); display: flex; align-items: center; gap: 8px; }
.sidebar .me button { margin-left: auto; background: none; border: 0; color: var(--nav-text); cursor: pointer; padding: 4px; border-radius: 6px; }
.sidebar .me button:hover { color: #fff; background: rgba(255,255,255,.08); }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--border); padding: 12px 24px; display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.topbar h2 { margin: 0 12px 0 0; font-size: 18px; }
.topbar .filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; flex: 1; }
.topbar .actions { display: flex; gap: 8px; }
.content { padding: 20px 24px 48px; display: flex; flex-direction: column; gap: 16px; max-width: 1600px; width: 100%; }

/* ---------- controls */
.select, .input { height: 34px; border: 1px solid var(--border-strong); background: var(--surface); border-radius: 8px; padding: 0 10px; min-width: 0; }
.select { padding-right: 28px; appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 15px) 15px, calc(100% - 10px) 15px; background-size: 5px 5px; background-repeat: no-repeat; }
.input:focus, .select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea.input { height: auto; min-height: 88px; padding: 8px 10px; resize: vertical; width: 100%; line-height: 1.45; }
.btn { display: inline-flex; align-items: center; gap: 6px; height: 34px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--border-strong); background: var(--surface); cursor: pointer; font-weight: 550; white-space: nowrap; }
.btn:hover { background: var(--surface-3); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn:disabled { opacity: .55; cursor: default; }
.btn svg { width: 16px; height: 16px; }
.icon-btn { width: 34px; padding: 0; justify-content: center; }
.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: none; padding: 0 12px; height: 32px; cursor: pointer; border-right: 1px solid var(--border); font-weight: 500; }
.seg button:last-child { border-right: 0; }
.seg button.on { background: var(--accent-soft); color: var(--accent-text); font-weight: 650; }
.seg.big button { height: 38px; padding: 0 16px; }
.seg button.on.good { background: var(--good-soft); color: var(--good-text); }
.seg button.on.warn { background: var(--warn-soft); color: var(--warn-text); }
.seg button.on.bad { background: var(--bad-soft); color: var(--bad-text); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--border-strong); background: var(--surface); border-radius: 999px; padding: 5px 12px; cursor: pointer; font-weight: 500; }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip kbd { font-size: 10px; opacity: .6; margin-left: 4px; }
.field-label { font-size: 12px; font-weight: 650; color: var(--text-2); margin: 14px 0 6px; text-transform: uppercase; letter-spacing: .03em; }
.check { display: flex; align-items: center; gap: 8px; cursor: pointer; }

/* ---------- cards & KPI */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-h { display: flex; align-items: center; gap: 10px; padding: 14px 16px 6px; }
.card-h h3 { margin: 0; font-size: 14px; font-weight: 650; }
.card-h .sub { color: var(--muted); font-size: 12px; }
.card-h .right { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.card-b { padding: 8px 16px 16px; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: 2fr 1fr; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.kpi { padding: 14px 16px; }
.kpi .label { color: var(--muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.kpi .value { font-size: 26px; font-weight: 700; margin-top: 4px; letter-spacing: -.01em; }
.kpi .meta { font-size: 12px; color: var(--muted); margin-top: 2px; display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.delta { font-weight: 650; }
.delta.up-bad, .delta.down-bad { color: var(--bad-text); }
.delta.up-good, .delta.down-good { color: var(--good-text); }
.progress { height: 6px; border-radius: 3px; background: var(--surface-3); overflow: hidden; margin-top: 8px; }
.progress > span { display: block; height: 100%; background: var(--accent); border-radius: 3px; }

/* ---------- charts */
.legend { display: flex; flex-wrap: wrap; gap: 14px; font-size: 12px; color: var(--text-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.cols { display: flex; align-items: stretch; gap: 10px; height: 230px; padding-top: 18px; border-bottom: 1px solid var(--border-strong); position: relative; }
.cols .gridline { position: absolute; left: 0; right: 0; border-top: 1px dashed var(--border); pointer-events: none; }
.cols .gridline span { position: absolute; right: 0; top: -16px; font-size: 11px; color: var(--muted); }
.col { flex: 1; min-width: 18px; max-width: 64px; display: flex; flex-direction: column; justify-content: flex-end; align-items: stretch; position: relative; cursor: pointer; z-index: 1; }
.col .stack { display: flex; flex-direction: column-reverse; gap: 2px; }
.col .seg-bar { min-height: 0; }
.col .stack .seg-bar:last-child { border-radius: 4px 4px 0 0; }
.col .total { text-align: center; font-size: 11px; color: var(--text-2); font-weight: 600; margin-bottom: 3px; }
.col.sel .total { color: var(--text); }
.col.dim { opacity: .45; }
.col:hover { opacity: 1; }
.col-labels { display: flex; gap: 10px; margin-top: 6px; }
.col-labels span { flex: 1; min-width: 18px; max-width: 64px; text-align: center; font-size: 11px; color: var(--muted); white-space: nowrap; }
.col-labels span.sel { color: var(--text); font-weight: 700; }
.hbars { display: flex; flex-direction: column; gap: 7px; }
.hbar { display: grid; grid-template-columns: minmax(90px, 180px) 1fr 64px; align-items: center; gap: 10px; cursor: default; border-radius: 6px; padding: 1px 4px; }
.hbar.link { cursor: pointer; }
.hbar.link:hover { background: var(--surface-2); }
.hbar .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.hbar .track { height: 14px; position: relative; }
.hbar .fill { height: 100%; border-radius: 0 4px 4px 0; background: var(--accent); min-width: 2px; }
.hbar .val { text-align: right; font-size: 12px; font-weight: 600; font-variant-numeric: tabular-nums; }
.hbar .avg { position: absolute; top: -3px; bottom: -3px; border-left: 2px dashed var(--text-2); opacity: .55; }
.splitbar { display: flex; gap: 2px; height: 22px; border-radius: 5px; overflow: hidden; margin: 8px 0 10px; }
.splitbar span { display: block; height: 100%; }
.status-list { display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.status-list div { display: flex; align-items: center; gap: 8px; }
.status-list b { margin-left: auto; font-variant-numeric: tabular-nums; }
.empty { padding: 28px; text-align: center; color: var(--muted); }
.tooltip { position: fixed; z-index: 100; pointer-events: none; background: var(--text); color: var(--bg); padding: 7px 10px; border-radius: 7px; font-size: 12px; line-height: 1.5; box-shadow: var(--shadow-lg); max-width: 260px; }
.tooltip b { font-weight: 700; }
.tooltip i { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 5px; }

/* ---------- tables */
.table-wrap { overflow: auto; }
table.data { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 13px; }
table.data th { position: sticky; top: 0; background: var(--surface-2); text-align: left; font-weight: 650; color: var(--text-2); font-size: 12px; padding: 9px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; user-select: none; }
table.data th.sortable { cursor: pointer; }
table.data th.sortable:hover { color: var(--text); }
table.data th .arrow { opacity: .6; font-size: 10px; }
table.data td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.data tr.row { cursor: pointer; }
table.data tr.row:hover td { background: var(--surface-2); }
table.data tr.row.active td { background: var(--accent-soft); }
table.data td.r, table.data th.r { text-align: right; font-variant-numeric: tabular-nums; }
table.data td.nowrap { white-space: nowrap; }
table.data td.clip { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ratebar { display: inline-block; width: 60px; height: 6px; border-radius: 3px; background: var(--surface-3); vertical-align: middle; margin-left: 8px; overflow: hidden; }
.ratebar span { display: block; height: 100%; background: var(--accent); }
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--surface-3); color: var(--text-2); }
.badge i { width: 7px; height: 7px; border-radius: 50%; display: inline-block; }
.badge.good { background: var(--good-soft); color: var(--good-text); }
.badge.warn { background: var(--warn-soft); color: var(--warn-text); }
.badge.bad { background: var(--bad-soft); color: var(--bad-text); }
.badge.info { background: var(--accent-soft); color: var(--accent-text); }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.toolbar .grow { flex: 1; min-width: 160px; }
.more { padding: 12px; text-align: center; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); padding: 0 16px; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: 10px 10px; cursor: pointer; font-weight: 600; color: var(--muted); }
.tabs button.on { color: var(--text); border-bottom-color: var(--accent); }

/* ---------- drawer & modal */
.backdrop { position: fixed; inset: 0; background: rgba(10, 14, 20, .35); z-index: 40; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(520px, 100vw); background: var(--surface); z-index: 50; box-shadow: var(--shadow-lg); display: flex; flex-direction: column; }
.drawer-h { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; align-items: flex-start; gap: 10px; }
.drawer-h h3 { margin: 0; font-size: 16px; }
.drawer-h .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.drawer-b { padding: 4px 20px 20px; overflow: auto; flex: 1; }
.drawer-f { padding: 12px 20px; border-top: 1px solid var(--border); display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.drawer-f .hint { font-size: 11px; color: var(--muted); flex-basis: 100%; }
.drawer-f .btn.primary { flex: 1; justify-content: center; }
.facts { display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px; font-size: 13px; margin-top: 12px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; word-break: break-word; }
.modal { position: fixed; z-index: 50; top: 50%; left: 50%; transform: translate(-50%, -50%); width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 32px); overflow: auto; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow-lg); }
.modal .drawer-h, .modal .drawer-f { position: sticky; background: var(--surface); }
.modal .drawer-h { top: 0; } .modal .drawer-f { bottom: 0; }
.export-opt { display: flex; gap: 12px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px; cursor: pointer; margin-top: 8px; align-items: flex-start; }
.export-opt:hover { background: var(--surface-2); }
.export-opt.on { border-color: var(--accent); background: var(--accent-soft); }
.export-opt input { margin-top: 3px; }
.export-opt b { display: block; }
.export-opt span { color: var(--muted); font-size: 12px; }
.ac-list { border: 1px solid var(--border); border-radius: 8px; margin-top: 4px; max-height: 220px; overflow: auto; }
.ac-list button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 7px 10px; cursor: pointer; border-bottom: 1px solid var(--border); }
.ac-list button:hover { background: var(--surface-2); }
.ac-list small { color: var(--muted); display: block; }
.grid-2f { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid-2f label { font-size: 12px; color: var(--muted); display: block; margin-bottom: 3px; }
.grid-2f .input { width: 100%; }

/* ---------- import */
.drop { border: 2px dashed var(--border-strong); border-radius: 12px; padding: 36px 16px; text-align: center; cursor: pointer; background: var(--surface-2); transition: .15s; }
.drop:hover, .drop.over { border-color: var(--accent); background: var(--accent-soft); }
.drop svg { width: 36px; height: 36px; color: var(--accent); }
.drop h4 { margin: 8px 0 4px; font-size: 15px; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 9px; z-index: 200; box-shadow: var(--shadow-lg); font-weight: 550; max-width: calc(100vw - 32px); }
.toast.err { background: var(--bad); color: #fff; }
.spinner { width: 28px; height: 28px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; margin: 60px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- responsive */
.mobile-nav { display: none; }
@media (max-width: 1100px) {
  .g3, .g2 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .mobile-nav { display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 30; background: var(--nav); justify-content: space-around; padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); }
  .mobile-nav a { color: var(--nav-text); text-decoration: none; font-size: 10px; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px 6px; border-radius: 6px; }
  .mobile-nav a.active { color: #fff; background: rgba(255,255,255,.1); }
  .mobile-nav svg { width: 20px; height: 20px; }
  .topbar { padding: 10px 16px; position: static; }
  .topbar h2 { flex-basis: 100%; }
  .topbar .actions { width: 100%; }
  .topbar .actions .btn { flex: 1; justify-content: center; }
  .card-h { flex-wrap: wrap; }
  .card-h .right { margin-left: 0; }
  .content { padding: 16px 16px 90px; }
  .topbar .filters .select { flex: 1 1 140px; }
  .hbar { grid-template-columns: minmax(70px, 120px) 1fr 52px; }
  .facts { grid-template-columns: 110px 1fr; }
  .kpi .value { font-size: 22px; }
}
@media print {
  .sidebar, .topbar .actions, .mobile-nav { display: none; }
  .app { grid-template-columns: 1fr; }
}
