:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ef;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --accent: #2a78d6;
  --up: #006300;
  --down: #d03b3b;
  --other: #b5b3ab;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --seq-0: #f0efec; --seq-1: #cde2fb; --seq-2: #86b6ef; --seq-3: #3987e5; --seq-4: #1c5cab; --seq-5: #0d366b;
  --font: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232322;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --accent: #3987e5;
    --up: #0ca30c;
    --down: #e66767;
    --other: #5c5b57;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --seq-0: #232322; --seq-1: #104281; --seq-2: #1c5cab; --seq-3: #2a78d6; --seq-4: #5598e7; --seq-5: #9ec5f4;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232322;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --accent: #3987e5;
  --up: #0ca30c;
  --down: #e66767;
  --other: #5c5b57;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --seq-0: #232322; --seq-1: #104281; --seq-2: #1c5cab; --seq-3: #2a78d6; --seq-4: #5598e7; --seq-5: #9ec5f4;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--ink);
  font: 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--down); margin: 0; }
code { font-size: 0.92em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

button, select, input {
  font: inherit;
  color: inherit;
}
button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 5px 12px;
}
button:hover { background: var(--surface-2); }
button.ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
button.ghost:hover { background: var(--surface-2); }
button:disabled { cursor: default; opacity: 0.55; }

/* layout */
main { max-width: 1360px; margin: 0 auto; padding: 16px 24px 40px; display: grid; gap: 16px; }
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px;
  position: sticky; top: 0; z-index: 5;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(8px);
  padding: 8px 0;
}
.brand { display: flex; align-items: baseline; gap: 10px; }
.brand strong { font-size: 18px; letter-spacing: -0.01em; }
.brand .home { color: inherit; text-decoration: none; }
a.ghost { color: var(--ink-2); text-decoration: none; border-radius: 8px; padding: 5px 10px; }
a.ghost:hover { background: var(--surface-2); }
.user { display: inline-flex; align-items: center; gap: 8px; }
.user img { width: 24px; height: 24px; border-radius: 50%; background: var(--surface-2); }
.access-help { color: var(--ink-2); margin: 0 0 12px; max-width: 880px; }
.token-created {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  background: var(--surface-2); border-radius: 10px; padding: 10px 12px; margin-bottom: 12px;
}
.token-created span { color: var(--ink-2); font-size: 13px; }
.token-created code { font-size: 13px; padding: 4px 8px; word-break: break-all; background: var(--surface); }
/* loading, freshness and errors */
.boot { min-height: 60vh; display: flex; align-items: center; justify-content: center; gap: 10px; padding: 16px; }
.boot .error { max-width: 560px; }
.spinner { width: 18px; height: 18px; border: 2px solid var(--grid); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
#refresh.spinning { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.progress { position: absolute; left: 0; right: 0; bottom: 0; height: 2px; overflow: hidden; opacity: 0; transition: opacity 0.2s; pointer-events: none; }
.progress::before { content: ""; position: absolute; top: 0; bottom: 0; left: -30%; width: 30%; border-radius: 2px; background: var(--accent); animation: progress 1.1s ease-in-out infinite; }
@keyframes progress { to { left: 100%; } }
main.loading .progress { opacity: 1; }
/* Dim stale content, but only once a load takes long enough to notice. */
.kpis, .card { transition: opacity 0.2s; }
main.loading .kpis, main.loading .card { opacity: 0.55; transition-delay: 0.15s; }
.banner {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px;
  padding: 8px 8px 8px 14px; border-radius: 10px; color: var(--down);
  border: 1px solid color-mix(in srgb, var(--down) 35%, transparent);
  background: color-mix(in srgb, var(--down) 8%, var(--surface));
}
.banner button { color: var(--ink); }
@media (prefers-reduced-motion: reduce) {
  .spinner, #refresh.spinning { animation: none; }
  .progress::before { animation: none; left: 0; width: 100%; opacity: 0.6; }
}

.controls, .card-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 2px; }
.seg button { border: 0; background: transparent; padding: 4px 10px; border-radius: 7px; color: var(--ink-2); }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 0 0 1px var(--border); font-weight: 600; }
.seg.small button { padding: 2px 8px; font-size: 12px; }

.select {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
  padding: 0 4px 0 10px; height: 32px;
}
.select span { color: var(--muted); font-size: 12px; white-space: nowrap; }
.select select { border: 0; background: transparent; padding: 4px 2px; max-width: 180px; outline: none; }
.select select option { background: var(--surface); color: var(--ink); }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: 1.6fr repeat(5, 1fr); gap: 12px; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px 16px; display: grid; gap: 2px; min-width: 0;
}
.kpi .label { color: var(--ink-2); font-size: 13px; }
.kpi .value { font-size: 26px; font-weight: 600; letter-spacing: -0.02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi.hero .value { font-size: 48px; line-height: 1.1; }
.kpi .sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.delta { font-size: 12px; font-weight: 600; }
.delta.up { color: var(--up); }
.delta.down { color: var(--down); }
.delta.flat { color: var(--muted); }

/* cards */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 14px 16px; min-width: 0;
}
.card-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.card-head h2 { font-size: 15px; margin: 0; font-weight: 600; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.chart { width: 100%; height: 300px; }
.chart.tall { height: 380px; }
#chart-heatmap { height: 280px; }

/* tables */
.table-wrap { overflow: auto; max-height: 560px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 7px 10px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--grid); }
th { color: var(--muted); font-weight: 500; font-size: 12px; position: sticky; top: 0; z-index: 1; background: var(--surface); }
th:first-child, td:first-child { text-align: left; }
td { font-variant-numeric: tabular-nums; }
td.name { max-width: 320px; overflow: hidden; text-overflow: ellipsis; }
tr:hover td { background: var(--surface-2); }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 8px; vertical-align: -1px; }
.bar-cell { position: relative; min-width: 120px; }
.bar-cell i { position: absolute; left: 10px; top: 50%; height: 6px; transform: translateY(-50%); border-radius: 3px; background: var(--accent); opacity: 0.25; }
.bar-cell span { position: relative; }
.tag { display: inline-block; font-size: 11px; padding: 0 6px; border-radius: 4px; background: var(--surface-2); color: var(--ink-2); margin-left: 6px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.dot.ok { background: #0ca30c; }
.dot.stale { background: #fab219; }
.dot.off { background: var(--muted); }
td button.danger { padding: 1px 8px; font-size: 12px; color: var(--down); }
td button { padding: 1px 8px; font-size: 12px; }
td .actions { display: inline-flex; gap: 6px; }
td .err { color: var(--down); display: inline-block; max-width: 260px; overflow: hidden; text-overflow: ellipsis; vertical-align: bottom; }
.tag.done { background: color-mix(in srgb, var(--up) 14%, transparent); color: var(--up); }
.tag.blocked { background: color-mix(in srgb, #eda100 18%, transparent); color: #a86f00; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .tag.blocked { color: #f0b429; } }
:root[data-theme="dark"] .tag.blocked { color: #f0b429; }

/* notification channels */
.access-help a { color: var(--accent); }
.channel-form { background: var(--surface-2); border-radius: 10px; padding: 14px; margin-bottom: 12px; display: grid; gap: 10px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
.field { display: grid; gap: 4px; min-width: 0; margin: 0; padding: 0; border: 0; }
.field.wide { grid-column: 1 / -1; }
.field > span, .field > legend { color: var(--ink-2); font-size: 12px; padding: 0; margin-bottom: 4px; }
.field input:not([type=checkbox]), .field select {
  height: 34px; padding: 0 10px; min-width: 0; width: 100%;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface); outline: none;
}
.field input:focus, .field select:focus { border-color: var(--accent); }
.check { display: inline-flex; align-items: center; gap: 6px; margin-right: 18px; cursor: pointer; }
.check input { width: 15px; height: 15px; margin: 0; accent-color: var(--accent); }
.form-help { margin: 0; max-width: 880px; }
.field-note { margin: 0; align-self: end; padding-bottom: 8px; }
.form-actions { display: flex; gap: 8px; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: color-mix(in srgb, var(--accent) 85%, #000); }
.sub-head { font-size: 13px; font-weight: 600; color: var(--ink-2); margin: 16px 0 4px; }
.table-wrap.short { max-height: 320px; }
.switch { cursor: pointer; width: 15px; height: 15px; accent-color: var(--accent); vertical-align: -2px; }

footer { text-align: center; padding-top: 8px; }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(3, 1fr); }
  .kpi.hero { grid-column: span 3; }
}
@media (max-width: 760px) {
  main { padding: 8px 16px 32px; }
  .grid-2 { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .kpi.hero { grid-column: span 2; }
  .kpi.hero .value { font-size: 40px; }
  .topbar { position: relative; }
  .chart.tall { height: 320px; }
  .select select { max-width: 120px; }
  .form-grid { grid-template-columns: 1fr; }
}
