:root {
  color-scheme: dark;

  --bg: #0f1114;
  --surface: #171a1f;
  --surface-2: #1e2128;
  --border: #2a2e37;
  --text: #f4f4f2;
  --text-dim: #9aa0ab;

  --orange: #ff8a1f;
  --amber: #ffc233;
  --green: #3ddc84;
  --white: #f4f4f2;
  --red: #ff5a4e;

  --c-lifted: var(--green);
  --c-progress: var(--amber);
  --c-queued: #4a4f5a;
  --c-deferred: var(--orange);

  --radius: 10px;
  --gap: 1rem;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

h1, h2 {
  margin: 0;
  font-weight: 600;
}

/* ---------- Top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap);
  padding: 0.75rem 1.5rem;
  background: var(--surface);
  border-bottom: 2px solid var(--orange);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.brand h1 {
  font-size: 1.35rem;
  letter-spacing: 0.02em;
}

.meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1.25rem;
  color: var(--text-dim);
  font-size: 0.9rem;
}

.meta time {
  color: var(--text);
  font-family: var(--mono);
}

.sync {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  font-variant-numeric: tabular-nums;
}

.sync-dot {
  width: 0.6rem;
  height: 0.6rem;
  border-radius: 50%;
  background: var(--text-dim);
}

.sync[data-state="ok"] .sync-dot { background: var(--green); }
.sync[data-state="loading"] .sync-dot { background: var(--amber); animation: pulse 1s ease-in-out infinite; }
.sync[data-state="paused"] .sync-dot { background: var(--text-dim); }
.sync[data-state="error"] { border-color: var(--red); color: var(--red); }
.sync[data-state="error"] .sync-dot { background: var(--red); }
.sync[data-state="updated"] { border-color: var(--orange); color: var(--orange); }
.sync[data-state="updated"] .sync-dot { background: var(--orange); }

@keyframes pulse {
  50% { opacity: 0.3; }
}

/* ---------- Layout ---------- */

main {
  flex: 1;
  width: 100%;
  max-width: 1400px;
  margin: 0 auto;
  padding: 1.5rem;
  display: grid;
  gap: 1.25rem;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  min-width: 0;
}

.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 0.9rem;
}

.panel-head h2 {
  font-size: 1.05rem;
}

.panel-figure {
  font-family: var(--mono);
  font-size: 1.1rem;
  color: var(--green);
}

/* ---------- KPI cards ---------- */

.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--gap);
}

.kpi {
  --accent: var(--white);
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: 4px solid var(--accent);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
}

.kpi h2 {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
}

.kpi-value {
  margin: 0.25rem 0 0;
  font-size: 2.4rem;
  font-weight: 700;
  line-height: 1.1;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.kpi-sub {
  margin: 0.25rem 0 0;
  min-height: 1.2em;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.kpi--lifted { --accent: var(--green); }
.kpi--progress { --accent: var(--amber); }
.kpi--queued { --accent: var(--white); }
.kpi--deferred { --accent: var(--orange); }

.flash {
  animation: flash 1.2s ease-out;
}

@keyframes flash {
  from { background: var(--surface-2); outline: 1px solid var(--orange); }
}

/* ---------- Bars ---------- */

.bar {
  display: flex;
  height: 0.6rem;
  overflow: hidden;
  border-radius: 999px;
  background: var(--surface-2);
}

.bar--lg {
  height: 1.4rem;
  border-radius: 6px;
}

.bar-seg {
  flex: 0 0 0%;
  height: 100%;
  transition: flex-basis 0.6s ease;
}

.bar-seg + .bar-seg {
  border-left: 1px solid var(--bg);
}

.bar-seg--lifted { background: var(--c-lifted); }
.bar-seg--progress { background: var(--c-progress); }
.bar-seg--queued { background: var(--c-queued); }
.bar-seg--deferred { background: var(--c-deferred); }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 1.25rem;
  margin: 0.75rem 0 0;
  padding: 0;
  list-style: none;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.legend li {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.swatch {
  width: 0.8rem;
  height: 0.8rem;
  border-radius: 3px;
}

.swatch--lifted { background: var(--c-lifted); }
.swatch--progress { background: var(--c-progress); }
.swatch--queued { background: var(--c-queued); }
.swatch--deferred { background: var(--c-deferred); }

/* ---------- Subsystems ---------- */

.subsystems {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 0.75rem;
}

.subsystem {
  display: grid;
  gap: 0.5rem;
  padding: 0.75rem 0.9rem;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  text-align: left;
  color: inherit;
  font: inherit;
}

.subsystem:hover,
.subsystem:focus-visible {
  border-color: var(--orange);
  outline: none;
}

.subsystem[aria-pressed="true"] {
  border-color: var(--amber);
  background: #25221a;
}

.subsystem-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
}

.subsystem-name {
  font-family: var(--mono);
  font-weight: 600;
}

.subsystem-pct {
  font-family: var(--mono);
  color: var(--green);
}

.subsystem-pct.is-zero { color: var(--text-dim); }
.subsystem-pct.is-done { color: var(--green); font-weight: 700; }

.subsystem-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 0.9rem;
  font-size: 0.8rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.subsystem-stats b { font-weight: 600; }
.stat-lifted b { color: var(--green); }
.stat-progress b { color: var(--amber); }
.stat-queued b { color: var(--white); }
.stat-deferred b { color: var(--orange); }

/* ---------- Tabs & toolbar ---------- */

.tabs {
  display: flex;
  gap: 0.25rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1rem;
}

.tab {
  padding: 0.6rem 1rem;
  border: 0;
  border-bottom: 3px solid transparent;
  margin-bottom: -1px;
  background: none;
  color: var(--text-dim);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.tab:hover { color: var(--text); }

.tab[aria-selected="true"] {
  color: var(--orange);
  border-bottom-color: var(--orange);
}

.tab-count {
  display: inline-block;
  min-width: 1.8em;
  margin-left: 0.35rem;
  padding: 0 0.4rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.field {
  display: grid;
  gap: 0.25rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}

.field--grow {
  flex: 1 1 240px;
}

.field[hidden] { display: none; }

.field input,
.field select {
  height: 2.2rem;
  padding: 0 0.6rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  text-transform: none;
  letter-spacing: normal;
}

.field input:focus,
.field select:focus {
  outline: none;
  border-color: var(--amber);
}

.result-count {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  color: var(--text-dim);
}

/* ---------- Table ---------- */

.table-wrap {
  max-height: 70vh;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.list {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

.list th,
.list td {
  padding: 0.45rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.list th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  color: var(--text-dim);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  cursor: pointer;
  user-select: none;
}

.list th:hover { color: var(--text); }
.list th[aria-sort="ascending"]::after { content: " \25B2"; color: var(--orange); }
.list th[aria-sort="descending"]::after { content: " \25BC"; color: var(--orange); }

.list th.num,
.list td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.list td.wrap {
  white-space: normal;
  min-width: 16rem;
}

.list tbody tr:hover { background: var(--surface-2); }

.list td.mono {
  font-family: var(--mono);
}

.list tr.is-deferred td:first-child {
  box-shadow: inset 3px 0 0 var(--orange);
}

.badge {
  display: inline-block;
  padding: 0.05rem 0.5rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  border: 1px solid currentColor;
}

.badge--cat-A { color: var(--green); }
.badge--cat-B { color: var(--amber); }
.badge--cat-C { color: var(--orange); }
.badge--to_do { color: var(--text-dim); }
.badge--deferred { color: var(--orange); }

.empty {
  margin: 1rem 0 0;
  color: var(--text-dim);
  text-align: center;
}

/* ---------- Footer ---------- */

.footer {
  padding: 1rem 1.5rem 1.5rem;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
  color: var(--text-dim);
  font-size: 0.8rem;
}

.footer p { margin: 0.25rem 0; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
}
