:root {
  color-scheme: light;
  --bg: #f5f6f8;
  --surface-1: #ffffff;
  --border: #e4e6ea;
  --text-primary: #191919;
  --text-secondary: #666666;
  --grid: #ececec;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --series-1: #257de0;
  --series-2: #f47230;
  --good: #0a7d24;
  --critical: #c62828;
  --unread: #eef4ff;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #111213;
    --surface-1: #191919;
    --border: #2c2d30;
    --text-primary: #efefef;
    --text-secondary: #9c9c9c;
    --grid: #2a2a2a;
    --accent: #3b82f6;
    --series-1: #338ced;
    --series-2: #e76624;
    --good: #3fbf5a;
    --critical: #ef6b6b;
    --unread: #1b2433;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111213;
  --surface-1: #191919;
  --border: #2c2d30;
  --text-primary: #efefef;
  --text-secondary: #9c9c9c;
  --grid: #2a2a2a;
  --accent: #3b82f6;
  --series-1: #338ced;
  --series-2: #e76624;
  --good: #3fbf5a;
  --critical: #ef6b6b;
  --unread: #1b2433;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; font-weight: 600; }
h2 { font-size: 15px; }
.muted { color: var(--text-secondary); }
.small { font-size: 13px; }
.error { color: var(--critical); min-height: 1.2em; margin: 0; font-size: 14px; }

button, select, input { font: inherit; color: inherit; }
select, input {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
}
.btn {
  border: 1px solid var(--border);
  background: var(--surface-1);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  white-space: nowrap;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.danger { color: var(--critical); }
.btn:disabled { opacity: 0.5; cursor: default; }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; font-size: 13px; }
.icon-btn {
  position: relative;
  width: 38px; height: 38px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  cursor: pointer;
  font-size: 17px;
}
.badge {
  position: absolute; top: -4px; right: -4px;
  min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px;
  background: var(--critical); color: #fff;
  font-size: 11px; line-height: 18px; font-weight: 600; text-align: center;
}

/* Login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.login-card {
  width: min(380px, 100%);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  display: grid; gap: 14px;
  box-shadow: var(--shadow);
}
.login-card h1 { font-size: 22px; }
.login-card p { margin: -8px 0 0; }
.login-card label { display: grid; gap: 6px; font-size: 14px; color: var(--text-secondary); }
.login-card input { padding: 10px 12px; color: var(--text-primary); }

/* Shell */
.app { min-height: 100dvh; padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.brand img { border-radius: 7px; }
.filters { display: flex; gap: 8px; flex: 1; min-width: 0; }
.filters select { min-width: 0; flex: 0 1 auto; }
.actions { display: flex; gap: 8px; margin-left: auto; }
.offline-bar { background: #b45309; color: #fff; text-align: center; padding: 6px; font-size: 13px; }
main { max-width: 1280px; margin: 0 auto; padding: 16px; }
.view > h2, .section-head { margin: 4px 0 14px; font-size: 18px; }
.section-head { display: flex; justify-content: space-between; align-items: center; }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow);
}
.kpi .label { color: var(--text-secondary); font-size: 13px; }
.kpi .value { font-size: 26px; font-weight: 650; letter-spacing: -0.01em; margin-top: 2px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12.5px; color: var(--text-secondary); margin-top: 2px; }
.kpi .up { color: var(--good); }
.kpi .down { color: var(--critical); }
.kpi.skeleton .value { background: var(--grid); border-radius: 6px; height: 30px; width: 70%; }

/* Cards */
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.card {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow);
  min-width: 0;
}
.card.wide { grid-column: 1 / -1; }
.card > header { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; }
.chart-box { position: relative; height: 280px; }
.chart-box.tall { height: 300px; }
.card .empty { display: grid; place-items: center; height: 100%; color: var(--text-secondary); font-size: 14px; text-align: center; }
.table-view { max-height: 300px; overflow: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
td.num, th.num { text-align: right; }

/* Lists */
.list { display: grid; }
.item {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 10px 4px; border-bottom: 1px solid var(--border);
  text-decoration: none; color: inherit;
}
.item:last-child { border-bottom: 0; }
.item .main { flex: 1; min-width: 0; }
.item .title { overflow: hidden; text-overflow: ellipsis; }
.item .meta { font-size: 12.5px; color: var(--text-secondary); }
.item .amt { font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 550; }
.pill { font-size: 11.5px; padding: 1px 8px; border-radius: 10px; border: 1px solid var(--border); color: var(--text-secondary); white-space: nowrap; }
#notif-list { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 0 12px; }
#notif-list .item.unread { background: var(--unread); margin: 0 -12px; padding-left: 16px; padding-right: 16px; }
#notif-list .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-top: 7px; flex: none; visibility: hidden; }
#notif-list .unread .dot { visibility: visible; }

/* Settings */
.settings .row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.settings .row p { margin: 2px 0 0; }

/* Tab bar */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; justify-content: center; gap: 4px;
  background: var(--surface-1); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  position: relative; flex: 0 1 140px;
  display: grid; justify-items: center; gap: 1px;
  background: none; border: 0; padding: 8px 0 9px; cursor: pointer;
  font-size: 12px; color: var(--text-secondary);
}
.tabbar button span:first-child { font-size: 19px; filter: grayscale(1); opacity: 0.7; }
.tabbar button.active { color: var(--accent); font-weight: 600; }
.tabbar button.active span:first-child { filter: none; opacity: 1; }
.tabbar .badge { top: 3px; right: calc(50% - 26px); }

/* Toasts */
.toast-stack { position: fixed; right: 16px; top: 72px; z-index: 50; display: grid; gap: 8px; width: min(360px, calc(100% - 32px)); }
.toast {
  background: var(--surface-1); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: 10px; padding: 10px 14px; box-shadow: 0 6px 24px rgba(0,0,0,.15);
  font-size: 14px; animation: slide .2s ease-out; cursor: pointer;
}
.toast strong { display: block; }
.toast.error { border-left-color: var(--critical); }
@keyframes slide { from { transform: translateY(-8px); opacity: 0; } }

.boot { position: fixed; inset: 0; display: grid; place-items: center; color: var(--text-secondary); background: var(--bg); }

@media (max-width: 760px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
  .topbar { gap: 8px; }
  .filters { order: 3; flex-basis: 100%; }
  .filters select { flex: 1; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi .value { font-size: 21px; }
  main { padding: 12px; }
}
