:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f0efeb;
  --border: #e1e0d9;
  --border-strong: #c3c2b7;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7973;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e6f0fc;
  --danger: #c0302f;
  --danger-soft: #fbe9e8;
  --ok: #006300;
  --ok-soft: #e5f4e5;
  --warn: #8a5a00;
  --warn-soft: #fdf1d6;
  --info-soft: #e6f0fc;
  /* Chart series: fixed order, validated for both themes (see Chart.tsx). */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.06), 0 8px 24px rgba(11, 11, 11, 0.08);
  --radius: 6px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
  --sans: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #2c2c2a;
    --border-strong: #46463f;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #8f8e86;
    --accent: #3987e5;
    --accent-ink: #ffffff;
    --accent-soft: #16283f;
    --danger: #e66767;
    --danger-soft: #3a1b1b;
    --ok: #3fbf3f;
    --ok-soft: #14301a;
    --warn: #fab219;
    --warn-soft: #3a2c0b;
    --info-soft: #16283f;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --grid: #2c2c2a;
    --axis: #383835;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.5);
  }
}

* {
  box-sizing: border-box;
}

html,
body,
#root {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
}

h1,
h2,
h3,
p {
  margin: 0;
}
h1 {
  font-size: 18px;
  font-weight: 650;
}
h2 {
  font-size: 14px;
  font-weight: 650;
}
h3 {
  font-size: 12px;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-2);
}
a {
  color: var(--accent);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}
code,
.mono {
  font-family: var(--mono);
  font-size: 12px;
}
.muted {
  color: var(--text-2);
}
.faint {
  color: var(--text-3);
}
.nowrap {
  white-space: nowrap;
}
.right {
  text-align: right;
}
.grow {
  flex: 1 1 auto;
  min-width: 0;
}
.row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.row-tight {
  display: flex;
  align-items: center;
  gap: 4px;
}
.stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.spread {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

/* ── shell ─────────────────────────────────────────────────────────────── */

.shell {
  display: grid;
  grid-template-columns: 208px minmax(0, 1fr);
  min-height: 100%;
}
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}
.brand {
  padding: 14px 16px 10px;
  font-weight: 700;
  font-size: 14px;
  display: flex;
  align-items: baseline;
  gap: 6px;
}
.brand small {
  font-weight: 400;
  color: var(--text-3);
  font-size: 11px;
}
.nav {
  display: flex;
  flex-direction: column;
  padding: 4px 8px;
  gap: 1px;
}
.nav a {
  display: block;
  padding: 6px 10px;
  border-radius: var(--radius);
  color: var(--text-2);
}
.nav a:hover {
  background: var(--surface-2);
  text-decoration: none;
  color: var(--text);
}
.nav a.active {
  background: var(--accent-soft);
  color: var(--text);
  font-weight: 600;
}
.nav-group {
  padding: 12px 10px 4px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-3);
}
.sidebar-foot {
  margin-top: auto;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.main {
  padding: 20px 24px 48px;
  min-width: 0;
  max-width: 1500px;
}
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.page-header p {
  margin-top: 2px;
  max-width: 78ch;
}
.crumbs {
  font-size: 12px;
  color: var(--text-3);
  margin-bottom: 4px;
}

@media (max-width: 860px) {
  .shell {
    grid-template-columns: minmax(0, 1fr);
  }
  .sidebar {
    position: static;
    height: auto;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
  .nav {
    flex-direction: row;
    flex-wrap: wrap;
  }
  .nav-group {
    display: none;
  }
  .sidebar-foot {
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
  }
  .main {
    padding: 16px 12px 40px;
  }
}

/* ── cards ─────────────────────────────────────────────────────────────── */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 16px;
  min-width: 0;
}
.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.card-head p {
  margin-top: 2px;
  font-size: 12px;
  max-width: 90ch;
}
.card-body {
  padding: 14px;
}
.card-body.flush {
  padding: 0;
}
.grid-2 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(420px, 100%), 1fr));
  gap: 16px;
}
.grid-2 > .card {
  margin-bottom: 0;
}
.grid-2 {
  margin-bottom: 16px;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 16px;
}
.stat {
  background: var(--surface);
  padding: 10px 14px;
}
.stat-label {
  font-size: 11px;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.stat-value {
  font-size: 20px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
}
.stat-sub {
  font-size: 12px;
  color: var(--text-2);
}

/* ── tables ────────────────────────────────────────────────────────────── */

.table-wrap {
  overflow-x: auto;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}
th,
td {
  padding: 6px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
th {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  white-space: nowrap;
  background: var(--surface);
}
th.sortable {
  cursor: pointer;
  user-select: none;
}
th.sortable:hover {
  color: var(--text);
}
tbody tr:last-child td {
  border-bottom: 0;
}
tbody tr:hover td {
  background: var(--surface-2);
}
tr.clickable {
  cursor: pointer;
}
tr.is-selected td {
  background: var(--accent-soft);
}
td.num,
th.num {
  text-align: right;
  white-space: nowrap;
}
td.actions {
  text-align: right;
  white-space: nowrap;
}
td.actions .btn + .btn {
  margin-left: 4px;
}
td.wrap {
  word-break: break-all;
}
.cell-main {
  font-weight: 600;
}
.cell-sub {
  font-size: 12px;
  color: var(--text-2);
}

/* ── controls ──────────────────────────────────────────────────────────── */

input,
select,
textarea,
button {
  font: inherit;
  color: inherit;
}
input[type='text'],
input[type='password'],
input[type='number'],
input[type='time'],
input[type='search'],
input[type='url'],
select,
textarea {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  padding: 5px 8px;
  min-height: 30px;
  width: 100%;
}
textarea {
  font-family: var(--mono);
  font-size: 12px;
  resize: vertical;
  min-height: 80px;
}
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible,
a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
input:disabled,
select:disabled,
textarea:disabled {
  background: var(--surface-2);
  color: var(--text-3);
  cursor: not-allowed;
}
input[type='checkbox'] {
  width: 15px;
  height: 15px;
  margin: 0;
  accent-color: var(--accent);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 30px;
  padding: 4px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  background: var(--surface);
  cursor: pointer;
  white-space: nowrap;
  font-weight: 550;
}
.btn:hover:not(:disabled) {
  background: var(--surface-2);
}
.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}
.btn-small {
  min-height: 24px;
  padding: 1px 8px;
  font-size: 12px;
}
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}
.btn-primary:hover:not(:disabled) {
  background: var(--accent);
  filter: brightness(1.08);
}
.btn-danger {
  color: var(--danger);
  border-color: var(--danger);
}
.btn-danger:hover:not(:disabled) {
  background: var(--danger-soft);
}
.btn-ghost {
  border-color: transparent;
  background: transparent;
  color: var(--text-2);
}
.icon-btn {
  border: 0;
  background: transparent;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 5px;
  color: var(--text-2);
}
.icon-btn:hover {
  background: var(--surface-2);
}

.spinner {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -1px;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
@media (prefers-reduced-motion: reduce) {
  .spinner {
    animation-duration: 2s;
  }
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px 16px;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}
.field-wide {
  grid-column: 1 / -1;
}
.field-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2);
}
.field-hint {
  font-size: 12px;
  color: var(--text-3);
}
.form-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 14px;
  flex-wrap: wrap;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  position: relative;
}
.toggle input {
  position: absolute;
  opacity: 0;
  width: 30px;
  height: 18px;
  margin: 0;
  cursor: inherit;
}
.toggle-track {
  width: 30px;
  height: 18px;
  border-radius: 9px;
  background: var(--border-strong);
  position: relative;
  transition: background 0.12s;
  flex: none;
}
.toggle-track::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.12s;
}
.toggle input:checked + .toggle-track {
  background: var(--accent);
}
.toggle input:checked + .toggle-track::after {
  transform: translateX(12px);
}
.toggle input:focus-visible + .toggle-track {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.toggle.is-disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.unit-input {
  display: inline-flex;
  width: 100%;
  min-width: 0;
}
.unit-input input {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
  border-right: 0;
  min-width: 0;
}
.unit-input select {
  width: auto;
  flex: none;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  background: var(--surface-2);
}
.unit-input.compact {
  width: 132px;
}
.unit-input.compact input,
.unit-input.compact select {
  min-height: 26px;
  padding: 2px 6px;
}
select.compact,
input.compact {
  min-height: 26px;
  padding: 2px 6px;
  width: auto;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.toolbar input[type='search'],
.toolbar input[type='text'] {
  width: 220px;
}
.toolbar select {
  width: auto;
}
.bulk {
  background: var(--accent-soft);
}

/* ── feedback ──────────────────────────────────────────────────────────── */

.note {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--info-soft);
  margin-bottom: 12px;
}
.note:last-child {
  margin-bottom: 0;
}
.note-icon {
  flex: none;
  width: 16px;
  height: 16px;
  margin-top: 1px;
  border-radius: 50%;
  font-size: 10px;
  font-weight: 700;
  line-height: 16px;
  text-align: center;
  background: var(--accent);
  color: #fff;
}
.note-warn {
  background: var(--warn-soft);
}
.note-warn .note-icon {
  background: var(--warn);
  color: var(--surface);
}
.note-error {
  background: var(--danger-soft);
  border-color: var(--danger);
}
.note-error .note-icon {
  background: var(--danger);
  color: var(--surface);
}
.note-ok {
  background: var(--ok-soft);
}
.note-ok .note-icon {
  background: var(--ok);
  color: var(--surface);
}

.badge {
  display: inline-block;
  padding: 0 6px;
  border-radius: 9px;
  font-size: 11px;
  font-weight: 600;
  line-height: 18px;
  background: var(--surface-2);
  color: var(--text-2);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.badge-ok {
  background: var(--ok-soft);
  color: var(--ok);
  border-color: transparent;
}
.badge-warn {
  background: var(--warn-soft);
  color: var(--warn);
  border-color: transparent;
}
.badge-bad {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: transparent;
}
.badge-info {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: transparent;
}

.empty {
  padding: 28px 16px;
  text-align: center;
  color: var(--text-2);
}

.meter {
  min-width: 120px;
}
.meter-track {
  height: 6px;
  border-radius: 3px;
  background: var(--surface-2);
  overflow: hidden;
}
.meter-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
}
.meter-warn {
  background: var(--warn);
}
.meter-bad {
  background: var(--danger);
}
.meter-label {
  font-size: 12px;
  color: var(--text-2);
  margin-top: 3px;
}

.tabs {
  display: flex;
  gap: 2px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.tab {
  border: 0;
  background: transparent;
  padding: 8px 12px;
  cursor: pointer;
  color: var(--text-2);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  font-weight: 550;
}
.tab:hover {
  color: var(--text);
}
.tab.active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(11, 11, 11, 0.45);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 8vh 16px 16px;
  z-index: 50;
  overflow-y: auto;
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 520px;
}
.modal-wide {
  max-width: 920px;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
.modal-body {
  padding: 14px;
}
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
}

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 60;
}
.toast {
  padding: 8px 12px;
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow);
  max-width: 420px;
}
.toast-error {
  background: var(--danger);
  color: #fff;
}

pre.code {
  margin: 0;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: var(--mono);
  font-size: 12px;
  overflow: auto;
  max-height: 420px;
  white-space: pre-wrap;
  word-break: break-word;
}
.kv {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 4px 16px;
}
.kv dt {
  color: var(--text-2);
}
.kv dd {
  margin: 0;
  word-break: break-word;
}
.secret {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface-2);
  font-family: var(--mono);
  font-size: 12px;
  word-break: break-all;
}

/* ── login ─────────────────────────────────────────────────────────────── */

.login {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.login .card {
  width: 100%;
  max-width: 340px;
  margin: 0;
}
.login h1 {
  margin-bottom: 2px;
}

/* ── chart ─────────────────────────────────────────────────────────────── */

.chart {
  position: relative;
}
.chart svg {
  display: block;
  max-width: 100%;
  overflow: visible;
}
.chart text {
  font-size: 11px;
  fill: var(--text-3);
  font-variant-numeric: tabular-nums;
}
.chart-legend {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--text-2);
  margin-bottom: 6px;
}
.chart-legend b {
  color: var(--text);
  font-weight: 600;
}
.swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  margin-right: 6px;
  vertical-align: -1px;
}
.chart-tip {
  position: absolute;
  top: 0;
  pointer-events: none;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px 8px;
  font-size: 12px;
  white-space: nowrap;
  z-index: 2;
}
.chart-tip table {
  width: auto;
}
.chart-tip td {
  border: 0;
  padding: 0 0 0 10px;
}
.chart-tip td:first-child {
  padding-left: 0;
}

/* ── feed ──────────────────────────────────────────────────────────────── */

.feed {
  max-height: 560px;
  overflow-y: auto;
}
.feed-item {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
.feed-item:last-child {
  border-bottom: 0;
}
.feed-item.is-new {
  animation: flash 1.2s ease-out;
}
@keyframes flash {
  from {
    background: var(--accent-soft);
  }
  to {
    background: transparent;
  }
}
.feed-item pre {
  margin: 4px 0 0;
  font-family: var(--mono);
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 180px;
  overflow: auto;
}
.live-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-3);
  margin-right: 6px;
}
.live-dot.on {
  background: var(--ok);
}

/* ── late additions ────────────────────────────────────────────────────── */

a.btn {
  color: inherit;
}
a.btn:hover {
  text-decoration: none;
}
.grid-side {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 3fr);
  gap: 16px;
  align-items: start;
}
@media (max-width: 1100px) {
  .grid-side {
    grid-template-columns: minmax(0, 1fr);
  }
}
