:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1c2330;
  --muted: #667085;
  --border: #e3e6eb;
  --primary: #1f6feb;
  --primary-text: #ffffff;
  --ok: #2f9e6e;
  --err: #d9534f;
  --warn-bg: #fff6e0;
  --warn-text: #7a5300;
  --row-hover: #f1f4f8;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f131a;
    --surface: #171c25;
    --text: #e6e9ef;
    --muted: #97a0b0;
    --border: #2a313d;
    --primary: #4c8dff;
    --ok: #3fbf88;
    --err: #ef6b67;
    --warn-bg: #3a2e10;
    --warn-text: #ffd98a;
    --row-hover: #1e2430;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

[hidden] { display: none !important; }

code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: 0.95rem; }

.muted { color: var(--muted); }
.error { color: var(--err); margin: 0; }
.warn { background: var(--warn-bg); color: var(--warn-text); padding: 0.6rem 0.8rem; border-radius: 8px; margin: 0; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 700; letter-spacing: 0.01em; }
.brand span { color: var(--muted); font-weight: 500; }

main, .login { max-width: 1200px; margin: 0 auto; padding: 1rem; }
main { display: flex; flex-direction: column; gap: 1rem; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; flex-wrap: wrap; margin-bottom: 0.75rem; }

.login { margin-top: 10vh; max-width: 420px; display: flex; flex-direction: column; gap: 0.75rem; }
.login form { display: flex; flex-direction: column; gap: 0.75rem; }

label { display: flex; flex-direction: column; gap: 0.3rem; font-weight: 500; }
label.inline { flex-direction: row; align-items: center; gap: 0.5rem; }
label.check { flex-direction: row; align-items: center; gap: 0.5rem; font-weight: 400; }

input {
  font: inherit; color: inherit; background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 0.45rem 0.6rem;
}
input:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
input[type="search"] { min-width: 0; width: 100%; max-width: 320px; }

.btn {
  font: inherit; font-weight: 500; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 0.45rem 0.85rem; border-radius: 8px;
}
.btn:hover { background: var(--row-hover); }
.btn:disabled { opacity: 0.6; cursor: wait; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.danger { background: var(--err); border-color: var(--err); color: #fff; }
.btn.danger-outline { color: var(--err); border-color: var(--err); }
.btn.small { padding: 0.2rem 0.55rem; font-size: 0.85rem; }

.toolbar { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.spacer { flex: 1; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 0.75rem; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 0.85rem 1rem; display: flex; flex-direction: column; }
.kpi-label { color: var(--muted); font-size: 0.85rem; }
.kpi-value { font-size: 1.6rem; font-weight: 650; font-variant-numeric: tabular-nums; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.55rem 0.6rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; font-size: 0.85rem; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover, tbody tr.selected { background: var(--row-hover); }
.empty { text-align: center; padding: 1rem; margin: 0; }
.sub { display: block; color: var(--muted); font-size: 0.8rem; }

.badge { display: inline-block; padding: 0.1rem 0.5rem; border-radius: 999px; font-size: 0.8rem; font-weight: 500; }
.badge.on { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.badge.off { background: color-mix(in srgb, var(--err) 15%, transparent); color: var(--err); }

.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1.5rem; margin-bottom: 1.5rem; }
.stack { display: flex; flex-direction: column; gap: 0.75rem; }
.row { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }

.chart {
  display: flex; align-items: flex-end; gap: 2px; height: 180px; margin-top: 0.75rem;
  border-bottom: 1px solid var(--border); overflow-x: auto;
}
.bar { flex: 1 0 10px; display: flex; flex-direction: column-reverse; min-height: 1px; }
.bar .ok { background: var(--ok); }
.bar .err { background: var(--err); }
.bar:hover { filter: brightness(1.15); }
.legend { display: flex; align-items: center; gap: 0.4rem; margin-top: 0.5rem; color: var(--muted); font-size: 0.85rem; }
.sw { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.sw.ok { background: var(--ok); }
.sw.err { background: var(--err); margin-left: 0.75rem; }

dialog {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
  padding: 1.25rem; width: min(480px, calc(100vw - 2rem));
}
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
.keybox {
  display: block; padding: 0.75rem; border-radius: 8px; background: var(--bg); border: 1px solid var(--border);
  word-break: break-all; user-select: all;
}

.toast {
  position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 0.55rem 1rem; border-radius: 8px; z-index: 20;
}

select {
  font: inherit; color: inherit; background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 0.45rem 0.6rem;
}
.grow { flex: 1; min-width: 0; }
.row > label { flex-direction: column; }
.stack > p { margin: 0; }
.portal-users { margin-bottom: 1.5rem; }
