:root {
  --bg: #111216;
  --surface: #191B21;
  --raised: #24242C;
  --line: #2E2F39;
  --line-strong: #3D3E4B;
  --accent: #BCA9EB;
  --accent-dim: rgba(188, 169, 235, 0.14);
  --text: #F5F3FA;
  --muted: #93939F;
  --live: #66D2A1;
  --live-dim: rgba(102, 210, 161, 0.13);
  --danger: #EC8E80;
  --danger-dim: rgba(236, 142, 128, 0.13);
  --warn: #E2BC6E;
  --warn-dim: rgba(226, 188, 110, 0.13);
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  color-scheme: dark;
}

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

html { height: 100%; }

body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p, dl, dd, ul, ol { margin: 0; }
h1 { font-size: 22px; font-weight: 600; letter-spacing: -0.01em; line-height: 1.25; }
h2 { font-size: 13px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
h3 { font-size: 14px; font-weight: 600; }

a { color: var(--accent); text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }

code, pre { font-family: var(--mono); font-size: 12.5px; }
code { color: var(--text); }

[hidden] { display: none !important; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
main:focus { outline: none; }

.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.nowrap { white-space: nowrap; }
.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.noscript { padding: 32px; text-align: center; }

.skip-link { position: absolute; left: 8px; top: -48px; z-index: 20; padding: 8px 12px; background: var(--raised); border-radius: 6px; }
.skip-link:focus { top: 8px; }

/* ---------------------------------------------------------------- wordmark */

.wordmark { font-size: 19px; font-weight: 700; letter-spacing: -0.03em; color: var(--text); }
.wordmark-dot { color: var(--accent); }

/* ------------------------------------------------------------------- shell */

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  height: 56px; padding: 0 24px;
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.topbar-label { color: var(--muted); font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; padding-top: 3px; }
.topbar-user { margin-left: auto; display: flex; align-items: center; gap: 12px; min-width: 0; }
.topbar-user .muted { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.layout { display: grid; grid-template-columns: 216px minmax(0, 1fr); min-height: calc(100vh - 56px); }

.sidenav { border-right: 1px solid var(--line); padding: 16px 12px; }
.sidenav ul { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 2px; position: sticky; top: 72px; }
.sidenav a {
  display: block; padding: 7px 12px; border-radius: 7px;
  color: var(--muted); text-decoration: none; font-weight: 500;
}
.sidenav a:hover { color: var(--text); background: var(--surface); }
.sidenav a[aria-current="page"] { color: var(--text); background: var(--raised); box-shadow: inset 2px 0 0 var(--accent); }

#main { padding: 28px 32px 64px; min-width: 0; }
.page { display: flex; flex-direction: column; gap: 20px; max-width: 1480px; }
.stack { display: flex; flex-direction: column; gap: 20px; }

.page-header { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.page-header p { margin-top: 4px; max-width: 72ch; }
.actions, .row-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.row-actions { flex-wrap: nowrap; }
.section-title { margin-top: 12px; }

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

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; display: flex; flex-direction: column; gap: 14px; }
.card-foot { display: flex; gap: 8px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); gap: 12px; }
.tiles-trend { grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.tile { background: var(--raised); border-radius: 8px; padding: 14px 16px; min-width: 0; }
.tile a { text-decoration: none; display: inline-block; border-radius: 4px; }
.tile a:hover .tile-value { text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 4px; }
.tile-label { color: var(--muted); font-size: 12.5px; }
.tile-value { font-size: 26px; font-weight: 600; letter-spacing: -0.02em; line-height: 1.25; font-variant-numeric: tabular-nums; color: var(--text); }
.tile-value-empty { font-size: 15px; font-weight: 500; color: var(--muted); letter-spacing: 0; padding: 7px 0; }
.tile-caption { color: var(--muted); font-size: 12px; }
.tone-live { color: var(--live); }
.tone-warn { color: var(--warn); }

.trend-box { margin-top: 10px; }
.trend { display: block; width: 100%; height: 56px; overflow: visible; touch-action: pan-y; }
.trend-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
.trend-area { fill: var(--accent); opacity: 0.1; }
.trend-base { stroke: var(--line-strong); stroke-width: 1; vector-effect: non-scaling-stroke; }
.trend-cross { stroke: var(--text); stroke-width: 1; opacity: 0.55; vector-effect: non-scaling-stroke; }
.trend-readout { margin-top: 6px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.table-view { margin-top: 4px; }
.table-view summary { cursor: pointer; color: var(--accent); width: fit-content; padding: 2px 4px; border-radius: 4px; }
.table-view[open] summary { margin-bottom: 12px; }

.budget-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.budget-bar { display: block; width: 100%; height: 12px; }
.budget-track { fill: var(--raised); }
.budget-spent { fill: var(--accent); }
.budget-over { fill: var(--danger); }
.budget-mark { stroke: var(--muted); stroke-width: 1; vector-effect: non-scaling-stroke; }

/* ------------------------------------------------------------------- facts */

.facts { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px 24px; }
.facts dt { color: var(--muted); font-size: 12.5px; }
.facts dd { overflow-wrap: anywhere; }
.badge-row { display: inline-flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.plain-list { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 4px; overflow-wrap: anywhere; }

/* ------------------------------------------------------------------ tables */

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.card .table-wrap { border-color: var(--line); background: transparent; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: 8px 12px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--line); }
th { font-size: 12px; font-weight: 600; color: var(--muted); white-space: nowrap; background: var(--raised); }
td { white-space: nowrap; }
td.wide { white-space: normal; min-width: 220px; max-width: 480px; overflow-wrap: anywhere; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: rgba(255, 255, 255, 0.02); }
.detail { white-space: pre-wrap; color: var(--muted); }

.empty { color: var(--muted); padding: 14px 0; }
.card > .empty, .trend-empty { padding: 4px 0; }

/* ------------------------------------------------------------------ badges */

.badge {
  display: inline-block; padding: 1px 8px; border-radius: 999px;
  font-size: 12px; font-weight: 600; line-height: 1.6; white-space: nowrap;
  background: var(--raised); color: var(--text); border: 1px solid var(--line-strong);
}
.badge-live { background: var(--live-dim); color: var(--live); border-color: transparent; }
.badge-warn { background: var(--warn-dim); color: var(--warn); border-color: transparent; }
.badge-danger { background: var(--danger-dim); color: var(--danger); border-color: transparent; }
.badge-accent { background: var(--accent-dim); color: var(--accent); border-color: transparent; }

/* ------------------------------------------------------------ form controls */

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 32px; padding: 5px 12px; border-radius: 7px;
  font: inherit; font-weight: 600; font-size: 13px; white-space: nowrap; text-decoration: none; cursor: pointer;
  background: var(--raised); color: var(--text); border: 1px solid var(--line-strong);
}
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--accent); color: #15131C; border-color: var(--accent); }
.btn-primary:hover { background: #CBBCF1; border-color: #CBBCF1; }
.btn-danger { background: transparent; color: var(--danger); border-color: rgba(236, 142, 128, 0.55); }
.btn-danger:hover { background: var(--danger-dim); border-color: var(--danger); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > label, .field-title { font-size: 12.5px; font-weight: 600; color: var(--text); }
.field-check > label { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 500; cursor: pointer; width: fit-content; }
.hint { font-size: 12px; color: var(--muted); }

input[type="text"], input[type="email"], input[type="password"], input[type="number"], input:not([type]), select, textarea {
  width: 100%; min-height: 34px; padding: 6px 10px; border-radius: 7px;
  background: var(--bg); color: var(--text); border: 1px solid var(--line-strong);
  font: inherit;
}
textarea { resize: vertical; line-height: 1.45; }
input:disabled, select:disabled, textarea:disabled { opacity: 0.6; cursor: not-allowed; }
input[type="checkbox"] { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }
input::placeholder { color: var(--muted); }

.filters { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; }
.filters .field { width: 200px; }
.filters .field:first-child { width: 340px; max-width: 100%; }
.filters .hint { display: none; }

.form-error { color: var(--danger); font-weight: 500; overflow-wrap: anywhere; }
.form-error:empty { display: none; }

.notice { border: 1px solid var(--line-strong); border-radius: 8px; padding: 12px 14px; background: var(--raised); display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.notice-danger { border-color: rgba(236, 142, 128, 0.55); background: var(--danger-dim); }
/* Placeholders for content that has not arrived must not take a gap in a stack. */
div:empty { display: none; }

/* ---------------------------------------------------------------- settings */

.settings { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 16px; align-items: start; }
.setting { gap: 12px; }
.setting-description { color: var(--muted); font-size: 13px; }
.setting-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 2px; }
.object-editor { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; align-items: start; }
.object-editor > .field:has(.ladder) { grid-column: 1 / -1; }
.ladder { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.ladder-step { display: grid; grid-template-columns: minmax(0, 1fr) 88px auto; gap: 8px; align-items: end; }
.value-preview { margin: 8px 0 0; padding: 10px 12px; background: var(--bg); border: 1px solid var(--line); border-radius: 7px; max-height: 240px; overflow: auto; white-space: pre-wrap; overflow-wrap: anywhere; }

/* --------------------------------------------------------------- emergency */

.page-emergency .card { border-color: rgba(236, 142, 128, 0.35); }
.emergency-head { display: flex; align-items: center; gap: 12px; }
.emergency-head h2 { color: var(--text); }
.emergency-action { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding-top: 12px; border-top: 1px solid var(--line); }
.emergency-action p { color: var(--muted); max-width: 72ch; }

/* ----------------------------------------------------------------- support */

.message { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 20px; display: flex; flex-direction: column; gap: 10px; }
.message-head { display: flex; align-items: center; gap: 8px 16px; flex-wrap: wrap; }
.message-head .actions { margin-left: auto; }
.message-body { white-space: pre-wrap; overflow-wrap: anywhere; max-width: 90ch; }

/* ------------------------------------------------------------------ player */

.player { display: flex; flex-direction: column; gap: 8px; }
.player audio { width: 100%; max-width: 560px; height: 40px; }

/* ------------------------------------------------------------------ dialog */

.dialog {
  width: min(520px, calc(100vw - 32px)); max-height: calc(100vh - 48px); overflow: auto;
  padding: 22px 24px; border-radius: 12px;
  background: var(--surface); color: var(--text); border: 1px solid var(--line-strong);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.55);
}
.dialog::backdrop { background: rgba(8, 8, 12, 0.72); }
.dialog-danger { border-color: rgba(236, 142, 128, 0.6); }
.dialog form { display: flex; flex-direction: column; gap: 14px; }
.dialog h2 { font-size: 17px; letter-spacing: 0; text-transform: none; color: var(--text); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* ------------------------------------------------------------------ toasts */

#toasts { position: fixed; right: 20px; bottom: 20px; z-index: 30; display: flex; flex-direction: column; gap: 8px; max-width: min(420px, calc(100vw - 40px)); }
.toast { padding: 10px 14px; border-radius: 8px; background: var(--raised); border: 1px solid var(--line-strong); border-left: 3px solid var(--live); box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4); }
.toast-danger { border-left-color: var(--danger); }

/* -------------------------------------------------------------------- gate */

.gate { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.gate-panel { width: min(400px, 100%); display: flex; flex-direction: column; gap: 14px; padding: 32px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; }
.gate-panel .wordmark { font-size: 24px; }
.gate-kicker { color: var(--muted); font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase; margin-top: -10px; }
.gate-panel h1 { font-size: 18px; margin-top: 8px; }
.gate-panel form { display: flex; flex-direction: column; gap: 14px; }
.gate-actions { display: flex; gap: 8px; align-items: center; }
.qr { align-self: center; background: #fff; padding: 10px; border-radius: 8px; }
.secret { user-select: all; overflow-wrap: anywhere; }

/* --------------------------------------------------------------- narrower */

@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidenav { border-right: 0; border-bottom: 1px solid var(--line); padding: 8px 12px; overflow-x: auto; }
  .sidenav ul { flex-direction: row; position: static; width: max-content; }
  .sidenav a[aria-current="page"] { box-shadow: inset 0 -2px 0 var(--accent); }
  #main { padding: 20px 16px 48px; }
  .topbar { padding: 0 16px; }
  .topbar-user .muted { display: none; }
  .emergency-action { flex-direction: column; align-items: flex-start; }
}

@media (prefers-reduced-motion: no-preference) {
  .btn, .sidenav a { transition: background-color 120ms, border-color 120ms, color 120ms; }
}
