/* ============================================================ tokens */
:root {
  color-scheme: light;
  --page: #f6f6f3;
  --surface: #fcfcfb;
  --surface-2: #f1f0ec;
  --surface-3: #e9e8e3;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #7d7b75;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --border-strong: rgba(11, 11, 11, 0.18);
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a;
  --seq-100: #cde2fb; --seq-300: #6da7ec; --seq-500: #256abf; --seq-700: #0d366b;
  --good: #0ca30c; --good-text: #006300; --good-bg: #e7f5e7;
  --warning: #fab219; --warning-bg: #fdf3dc;
  --serious: #ec835a;
  --critical: #d03b3b; --critical-bg: #fbe9e9;
  --accent: #0a74a6; --accent-bg: #e3f1f8;
  --brand-orange: #f07a00; --brand-blue: #007dad;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04);
  --h-ctl: 36px;
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --surface-3: #2c2c2a;
    --ink: #f0efec; --ink-2: #c3c2b7; --muted: #8f8d86;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10); --border-strong: rgba(255, 255, 255, 0.18);
    --s1: #3987e5; --s2: #d95926; --s3: #199e70;
    --seq-100: #184f95; --seq-300: #256abf; --seq-500: #5598e7; --seq-700: #b7d3f6;
    --good-text: #0ca30c; --good-bg: #13301a; --warning-bg: #3a2e10; --critical-bg: #3a1717;
    --accent: #3aa6d8; --accent-bg: #12303f; --btn-primary: #1479a9;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --surface-3: #2c2c2a;
  --ink: #f0efec; --ink-2: #c3c2b7; --muted: #8f8d86;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10); --border-strong: rgba(255, 255, 255, 0.18);
  --s1: #3987e5; --s2: #d95926; --s3: #199e70;
  --seq-100: #184f95; --seq-300: #256abf; --seq-500: #5598e7; --seq-700: #b7d3f6;
  --good-text: #0ca30c; --good-bg: #13301a; --warning-bg: #3a2e10; --critical-bg: #3a1717;
  --accent: #3aa6d8; --accent-bg: #12303f; --btn-primary: #1479a9;
}

/* ============================================================ base */
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--page); color: var(--ink); }
body { overflow-x: hidden; }
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 24px; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 16px; font-weight: 600; }
h3 { font-size: 12px; font-weight: 700; color: var(--ink-2); text-transform: uppercase; letter-spacing: .05em; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.wrap { width: 100%; max-width: 1480px; margin: 0 auto; padding-left: 24px; padding-right: 24px; }

/* controls */
button, input, select, textarea { font: inherit; color: inherit; }
button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; white-space: nowrap;
  height: var(--h-ctl); padding: 0 14px; border-radius: 8px; cursor: pointer; font-weight: 500;
  border: 1px solid var(--border-strong); background: var(--surface);
}
button:hover { background: var(--surface-2); }
button.small { height: 30px; padding: 0 10px; font-size: 13px; }
button.primary { background: var(--btn-primary, var(--accent)); border-color: transparent; color: #fff; font-weight: 600; }
button.primary:hover { filter: brightness(1.07); }
button.danger { background: var(--critical); border-color: transparent; color: #fff; font-weight: 600; }
button.ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
button.ghost:hover { background: var(--surface-2); color: var(--ink); }
button.link { background: none; border: 0; height: auto; padding: 0; color: var(--accent); }
button:disabled { opacity: .5; cursor: not-allowed; }
input, select, textarea {
  min-height: var(--h-ctl); padding: 6px 10px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface);
}
textarea { min-height: 120px; resize: vertical; width: 100%; }
select[multiple] { padding: 4px; min-width: 240px; }
input[type="checkbox"] { min-height: 0; width: 16px; height: 16px; accent-color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.field { display: grid; gap: 5px; font-size: 13px; color: var(--ink-2); font-weight: 500; }
.field > input, .field > select, .field > textarea { color: var(--ink); font-weight: 400; width: 100%; }
.inline-field { display: inline-flex; align-items: center; gap: 8px; }
.check { display: inline-flex; align-items: center; gap: 8px; color: var(--ink); cursor: pointer; }
.actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.actions.end { justify-content: flex-end; }
.form-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); align-items: end; }

/* ============================================================ login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 32px; display: grid; gap: 16px; box-shadow: var(--shadow); }
.login-card input { width: 100%; }
.error { color: var(--critical); min-height: 1em; font-size: 13px; }

/* ============================================================ shell */
.brand-logo { height: 30px; width: auto; display: block; }
.brand-sub { font-weight: 600; color: var(--ink-2); padding-left: 12px; margin-left: 2px; border-left: 1px solid var(--border-strong); font-size: 13px; }
a.brand:hover { text-decoration: none; }
.login-logo { width: 220px; height: auto; margin-bottom: 4px; }
.dark-only { display: none !important; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .dark-only { display: block !important; } :root:not([data-theme="light"]) .light-only { display: none !important; } }
:root[data-theme="dark"] .dark-only { display: block !important; }
:root[data-theme="dark"] .light-only { display: none !important; }
.topbar { box-shadow: inset 0 -3px 0 0 transparent; }
.topbar::before { content: ""; display: block; height: 3px; background: linear-gradient(90deg, var(--brand-orange) 0 72%, var(--surface) 72% 73.5%, var(--brand-blue) 73.5% 100%); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; white-space: nowrap; }
.logo { width: 24px; height: 24px; border-radius: 6px; flex: none;
  background: var(--s1) linear-gradient(135deg, transparent 44%, #fff 44%, #fff 56%, transparent 56%); }
.topbar { position: sticky; top: 0; z-index: 10; background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; height: 56px; }
.user { display: flex; align-items: center; gap: 14px; white-space: nowrap; }
.user-name { font-weight: 600; }
.nav { display: flex; flex-wrap: wrap; gap: 2px 4px; padding-bottom: 8px; min-width: 0; }
.nav a { padding: 6px 12px; border-radius: 8px; color: var(--ink-2); white-space: nowrap; font-weight: 500; }
.nav a:hover { background: var(--surface-2); color: var(--ink); text-decoration: none; }
.nav a[aria-current="page"] { background: var(--accent-bg); color: var(--accent); font-weight: 600; }
.nav .nav-sep { width: 1px; background: var(--border); margin: 6px 6px; }

.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 16px; padding: 28px 0 8px; }
.page-title { display: grid; gap: 4px; min-width: 260px; }
.page-title p { font-size: 14px; }
.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.custom-range { display: inline-flex; align-items: center; gap: 6px; }
.period-label { min-height: 18px; padding-bottom: 8px; }
.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 9px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; border-radius: 0; height: 34px; background: transparent; font-weight: 500; color: var(--ink-2); }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--accent-bg); color: var(--accent); font-weight: 600; }
.view { display: grid; gap: 20px; padding-bottom: 48px; }
.view:focus { outline: none; }

/* tabs (config sub-pages) */
.tabs { display: flex; flex-wrap: wrap; gap: 4px; border-bottom: 1px solid var(--border); }
.tabs button { border: 0; border-radius: 8px 8px 0 0; background: transparent; color: var(--ink-2); height: 40px;
  border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs button:hover { background: var(--surface-2); }
.tabs button[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

/* ============================================================ cards & grids */
.grid { display: grid; gap: 20px; min-width: 0; }
.stack { display: grid; gap: 12px; min-width: 0; }
.grid > button, .stack > button, .grid > .actions, .stack > .actions { justify-self: start; }
.stack > .actions[style*='space-between'] { justify-self: stretch; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; }
.cols-2 { grid-template-columns: repeat(auto-fit, minmax(min(460px, 100%), 1fr)); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px;
  min-width: 0; box-shadow: var(--shadow); }
.card > header { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.card > header .card-sub { width: 100%; color: var(--muted); font-size: 13px; margin-top: -8px; }
.kpi { padding: 18px 20px; }
.kpi .label { color: var(--ink-2); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 30px; font-weight: 650; margin-top: 8px; line-height: 1.1; }
.kpi .sub { display: flex; gap: 14px; margin-top: 8px; color: var(--ink-2); flex-wrap: wrap; font-size: 13px; }
.kpi .sub b { color: var(--ink); font-weight: 600; }
.delta-up { color: var(--good-text); }
.delta-down { color: var(--critical); }
.chart-box { position: relative; height: 270px; }
.chart-box.tall { height: 340px; }
.empty { color: var(--muted); padding: 28px 0; text-align: center; }
.note { background: var(--surface-2); border-radius: 10px; padding: 12px 14px; color: var(--ink-2); font-size: 13px; }
.note.warn { background: var(--warning-bg); color: var(--ink); }

/* progress */
.progress { position: relative; height: 12px; border-radius: 6px; background: var(--surface-2); margin: 14px 0 8px; }
.progress .bar { position: absolute; inset: 0 auto 0 0; border-radius: 6px; background: var(--s1); max-width: 100%; }
.progress .time { position: absolute; top: -5px; bottom: -5px; width: 2px; background: var(--ink-2); }
.legend { display: flex; flex-wrap: wrap; gap: 16px; color: var(--ink-2); font-size: 12px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

/* ============================================================ tables */
.table-wrap { overflow: auto; max-height: 560px; border: 1px solid var(--border); border-radius: 10px; }
table { border-collapse: collapse; width: 100%; font-size: 13px; }
th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--grid); white-space: nowrap; vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
th { position: sticky; top: 0; z-index: 1; background: var(--surface-2); font-weight: 600; color: var(--ink-2); cursor: pointer; user-select: none; }
th[aria-sort="ascending"]::after { content: " ▲"; font-size: 9px; }
th[aria-sort="descending"]::after { content: " ▼"; font-size: 9px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.wrap-text { white-space: normal; min-width: 260px; }
tbody tr:hover { background: var(--surface-2); }
.table-tools { display: flex; gap: 8px; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.heat { text-align: right; font-variant-numeric: tabular-nums; }
.targets td input { width: 84px; text-align: right; min-height: 32px; padding: 4px 8px; }
.targets th, .targets td { padding: 6px 6px; }

/* chips & badges */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 10px; border-radius: 999px;
  font-size: 12px; background: var(--surface-2); color: var(--ink-2); white-space: nowrap; border: 1px solid transparent; }
button.chip { cursor: pointer; font-weight: 500; }
.chip.on { border-color: var(--accent); background: var(--accent-bg); color: var(--accent); }
.badge { display: inline-flex; align-items: center; height: 22px; padding: 0 8px; border-radius: 6px; font-size: 12px; font-weight: 600;
  background: var(--surface-2); color: var(--ink-2); vertical-align: middle; }
.badge.test { background: var(--warning-bg); color: #8a5a00; }
.badge.live { background: var(--good-bg); color: var(--good-text); }
.badge.off { background: var(--surface-3); color: var(--muted); }
.badge.fail { background: var(--critical-bg); color: var(--critical); }
:root[data-theme="dark"] .badge.test { color: var(--warning); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .badge.test { color: var(--warning); } }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }
.toast { position: fixed; bottom: 20px; right: 20px; z-index: 50; background: var(--ink); color: var(--page);
  padding: 12px 16px; border-radius: 10px; box-shadow: 0 6px 24px rgba(0,0,0,.2); max-width: 420px; }

/* ============================================================ automations */
.banner { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 16px;
  border-left: 4px solid var(--axis); }
.banner.on { border-left-color: var(--good); }
.banner .state { display: grid; gap: 4px; }
.banner .state strong { font-size: 16px; }
.rule-card { display: grid; gap: 14px; }
.rule-card.disabled { opacity: .6; }
.rule-head { display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: 12px; }
.rule-title { display: grid; gap: 4px; }
.rule-title h2 { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.rule-logic { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr)); gap: 12px; }
.rule-block { background: var(--surface-2); border-radius: 10px; padding: 12px 14px; display: grid; gap: 6px; align-content: start; }
.rule-block ul { margin: 0; padding-left: 18px; display: grid; gap: 3px; }
.rule-foot { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px;
  border-top: 1px solid var(--border); padding-top: 12px; }
.stat-line { display: flex; flex-wrap: wrap; gap: 16px; color: var(--ink-2); font-size: 13px; }
.stat-line b { color: var(--ink); }

.editor { border: 2px solid var(--accent); }
.builder { display: grid; gap: 8px; }
.rule-row { display: grid; gap: 6px; background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.rule-row-main { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 10px; }
.rule-row-main > select:first-child { min-width: 240px; font-weight: 500; }
.rule-row-main > .remove { margin-left: auto; }
.params { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.rule-row .help { color: var(--muted); font-size: 12px; }
.section-label { display: flex; align-items: center; gap: 10px; }
.section-label::after { content: ""; flex: 1; height: 1px; background: var(--border); }

/* ============================================================ responsive */
@media (max-width: 900px) {
  .wrap { padding-left: 16px; padding-right: 16px; }
  .topbar-row { height: 52px; }
  #sync-info { display: none; }
  .nav { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .page-head { padding-top: 20px; }
  .kpi .value { font-size: 26px; }
  .card { padding: 16px; }
  .seg { flex-wrap: wrap; }
}

/* reports */
.report-card { display: grid; grid-template-columns: 48px 1fr; gap: 6px 16px; align-items: start; }
.report-icon { width: 48px; height: 48px; border-radius: 12px; background: var(--accent-bg); display: grid; place-items: center; color: var(--accent); }
.report-actions { grid-column: 2; margin-top: 8px; }
/* client state bars */
.state-bars { display: grid; gap: 10px; }
.state-bar { display: grid; grid-template-columns: 170px 1fr 48px; align-items: center; gap: 12px; }
.state-name { display: inline-flex; align-items: center; gap: 8px; color: var(--ink-2); text-transform: capitalize; }
.state-track { height: 10px; border-radius: 5px; background: var(--surface-2); overflow: hidden; }
.state-track > span { display: block; height: 100%; border-radius: 5px; }
.state-n { text-align: right; font-variant-numeric: tabular-nums; }
@media (max-width: 900px) {
  .form-grid { grid-template-columns: 1fr !important; }
  .brand-sub, .user-name { display: none; }
  .page-head { align-items: flex-start; }
  .filters { width: 100%; }
  .state-bar { grid-template-columns: 120px 1fr 36px; }
  .report-card { grid-template-columns: 1fr; }
  .report-icon { display: none !important; }
  .report-actions { grid-column: 1; }
  select[multiple] { min-width: 0; width: 100%; }
  .rule-row-main > select:first-child { min-width: 0; width: 100%; }
  .params { width: 100%; }
  .params > input[style*="min-width"] { min-width: 0 !important; width: 100%; }
}
select { max-width: 100%; }
