/* AI Relay dashboard. Tokens on :root, dark mode via prefers-color-scheme (and an explicit data-theme hook). No !important. */

:root {
  color-scheme: light;
  --bg: #f5f5f3;
  --surface: #fcfcfb;
  --surface-2: #f0f0ed;
  --border: #e2e2dd;
  --text: #1d1d1b;
  --text-2: #4b4b47;
  --muted: #6f6f69;
  --accent: #2a78d6;
  --accent-text: #ffffff;
  --danger: #b93232;
  --danger-text: #ffffff;
  --ok: #2b7a4b;
  --warn: #8f5c00;
  --err: #b93232;
  --ok-bg: #e4f2ea;
  --warn-bg: #f8eed9;
  --err-bg: #f7e1e1;
  --neutral-bg: #ebebe8;
  --series-1: #2a78d6;
  --grid: #e7e7e3;
  --shadow: 0 8px 24px rgb(0 0 0 / 0.12);
  --radius: 9px;
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #161615;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #33332f;
    --text: #ecebe6;
    --text-2: #c4c3bd;
    --muted: #98978f;
    --accent: #3987e5;
    --accent-text: #0b0b0a;
    --danger: #e5695a;
    --danger-text: #161615;
    --ok: #6cc08f;
    --warn: #e0b04f;
    --err: #ef7a6e;
    --ok-bg: #1f3327;
    --warn-bg: #3a2f17;
    --err-bg: #3a2220;
    --neutral-bg: #2a2a27;
    --series-1: #3987e5;
    --grid: #2c2c29;
    --shadow: 0 8px 24px rgb(0 0 0 / 0.5);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #161615;
  --surface: #1a1a19;
  --surface-2: #232321;
  --border: #33332f;
  --text: #ecebe6;
  --text-2: #c4c3bd;
  --muted: #98978f;
  --accent: #3987e5;
  --accent-text: #0b0b0a;
  --danger: #e5695a;
  --danger-text: #161615;
  --ok: #6cc08f;
  --warn: #e0b04f;
  --err: #ef7a6e;
  --ok-bg: #1f3327;
  --warn-bg: #3a2f17;
  --err-bg: #3a2220;
  --neutral-bg: #2a2a27;
  --series-1: #3987e5;
  --grid: #2c2c29;
  --shadow: 0 8px 24px rgb(0 0 0 / 0.5);
}

/* Base */
*, *::before, *::after { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14.5px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
#app { min-height: 100vh; }
h1, h2 { color: var(--text); line-height: 1.25; }
h1 { margin: 0; font-size: 22px; font-weight: 650; letter-spacing: -0.01em; }
h2 { margin: 0; font-size: 15px; font-weight: 600; }
p { margin: 0; }
a { color: var(--accent); text-underline-offset: 2px; }
code { font-family: var(--mono); font-size: 0.92em; }
label { font-size: 13.5px; font-weight: 550; color: var(--text-2); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.muted { color: var(--muted); }
.mono { font-family: var(--mono); overflow-wrap: anywhere; }
.small { font-size: 12.5px; }
.right { text-align: right; }
.spacer { flex: 1 1 auto; }

/* Layout */
.shell { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh; }
.sidebar {
  position: sticky;
  top: 0;
  align-self: start;
  height: 100vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-5) var(--space-3);
  background: var(--surface);
  border-right: 1px solid var(--border);
}
.brand { padding: 0 10px; font-size: 16px; font-weight: 650; letter-spacing: -0.01em; color: var(--text); text-decoration: none; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-link {
  display: block;
  padding: 7px 10px;
  border-radius: 7px;
  color: var(--text-2);
  text-decoration: none;
  transition: background-color 0.15s ease, color 0.15s ease;
}
.nav-link:hover { background: var(--surface-2); color: var(--text); }
.nav-link.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.nav-section {
  margin: var(--space-3) 10px 0;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.sidebar-foot { padding: 0 10px; color: var(--muted); font-size: 12.5px; overflow-wrap: anywhere; }

.content {
  min-width: 0;
  width: 100%;
  max-width: 1180px;
  padding: var(--space-5) var(--space-6) var(--space-6);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-3); }
.stack { display: flex; flex-direction: column; gap: var(--space-4); min-width: 0; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); min-width: 0; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: var(--space-4); }
.grid > * { min-width: 0; }

.card, .stat {
  min-width: 0;
  padding: var(--space-5);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.stat-label { font-size: 13px; color: var(--muted); }
.stat-value { margin: var(--space-1) 0; font-size: 26px; font-weight: 650; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stat-sub { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }

.facts { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: var(--space-2) var(--space-4); margin: 0; font-size: 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }

.empty {
  padding: var(--space-6) var(--space-5);
  text-align: center;
  color: var(--muted);
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}
.empty strong { display: block; margin-bottom: var(--space-1); color: var(--text); }

/* Forms */
form.form { display: flex; flex-direction: column; gap: var(--space-4); max-width: 440px; min-width: 0; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field-inline { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
.check { display: flex; align-items: center; gap: var(--space-2); }
.check input[type="checkbox"] { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }
.hint { font-size: 12.5px; color: var(--muted); }
.error-text { font-size: 13.5px; color: var(--danger); overflow-wrap: anywhere; }
input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
select,
textarea {
  font: inherit;
  font-size: 14px;
  min-height: 36px;
  min-width: 0;
  max-width: 100%;
  padding: 7px 10px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 7px;
}
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 1; }
textarea { min-height: 90px; resize: vertical; }
input:disabled, select:disabled, textarea:disabled { opacity: 0.6; }

/* Buttons */
button, .btn {
  font: inherit;
  font-size: 14px;
  font-weight: 550;
  line-height: 1.2;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 7px 14px;
  white-space: nowrap;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { color: var(--accent-text); background: var(--accent); border-color: var(--accent); }
.btn-primary:hover:not(:disabled) { border-color: var(--accent); opacity: 0.92; }
.btn-danger { color: var(--danger-text); background: var(--danger); border-color: var(--danger); }
.btn-danger:hover:not(:disabled) { border-color: var(--danger); opacity: 0.92; }
.btn-ghost { color: var(--text-2); background: transparent; border-color: transparent; }
.btn-ghost:hover:not(:disabled) { color: var(--text); background: var(--surface-2); border-color: transparent; }
.btn-small { min-height: 28px; padding: 3px 9px; font-size: 13px; }

/* Tables */
.table-wrap {
  max-width: 100%;
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
table.data { width: 100%; border-collapse: collapse; font-size: 13.5px; font-variant-numeric: tabular-nums; }
table.data th,
table.data td { padding: 9px 12px; text-align: left; vertical-align: top; border-bottom: 1px solid var(--border); }
table.data tr:last-child td { border-bottom: 0; }
table.data th { font-size: 12.5px; font-weight: 550; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
table.data td { color: var(--text); overflow-wrap: break-word; }
table.data .num { text-align: right; white-space: nowrap; }

/* Badges */
.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  line-height: 1.6;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  color: var(--text-2);
  background: var(--neutral-bg);
}
.badge.ok { color: var(--ok); background: var(--ok-bg); }
.badge.warn { color: var(--warn); background: var(--warn-bg); }
.badge.err { color: var(--err); background: var(--err-bg); }
.badge.neutral { color: var(--text-2); background: var(--neutral-bg); }

/* Dialog */
.dialog {
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  overflow: auto;
  padding: var(--space-5);
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgb(0 0 0 / 0.45); }
.dialog h2 { font-size: 17px; }
.dialog-body { display: flex; flex-direction: column; gap: var(--space-3); margin-top: var(--space-3); }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: var(--space-2); margin-top: var(--space-5); }

/* Toasts */
.toast-host {
  position: fixed;
  right: var(--space-4);
  bottom: var(--space-4);
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: var(--space-2);
  max-width: min(380px, calc(100vw - 32px));
  pointer-events: none;
}
.toast {
  max-width: 100%;
  padding: 10px 14px;
  font-size: 13.5px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--muted);
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow-wrap: anywhere;
  pointer-events: auto;
  animation: toast-in 0.18s ease-out;
}
.toast.info { border-left-color: var(--accent); }
.toast.success { border-left-color: var(--ok); }
.toast.error { border-left-color: var(--danger); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

/* Charts */
.chart { position: relative; margin: 0; min-width: 0; }
.chart figcaption { margin-bottom: var(--space-2); font-size: 13.5px; font-weight: 600; color: var(--text-2); }
.chart-svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart-svg .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart-svg .bar { fill: var(--series-1); }
.chart-svg .axis { fill: var(--muted); font-family: var(--font); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart-svg .col { fill: transparent; outline: none; }
.chart-svg .col:hover { fill: var(--series-1); fill-opacity: 0.08; }
.chart-svg .col:focus-visible { fill: var(--series-1); fill-opacity: 0.08; stroke: var(--accent); stroke-width: 1.5; }
.chart-tip {
  position: absolute;
  z-index: 2;
  transform: translate(-50%, calc(-100% - 8px));
  max-width: 240px;
  padding: 4px 8px;
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  color: var(--surface);
  background: var(--text);
  border-radius: 6px;
  box-shadow: var(--shadow);
  pointer-events: none;
}
.chart-table { margin-top: var(--space-3); font-size: 13px; }
.chart-table summary { cursor: pointer; color: var(--muted); }
.chart-table summary:hover { color: var(--text); }
.chart-table .table-wrap { margin-top: var(--space-2); max-height: 260px; }

/* Meters */
.meter { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.meter-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: var(--space-2); font-size: 13.5px; font-variant-numeric: tabular-nums; }
.meter-head span:first-child { color: var(--text-2); font-weight: 550; }
.meter meter { display: block; width: 100%; height: 10px; }
.meter meter::-webkit-meter-bar { background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; }
.meter meter::-webkit-meter-optimum-value { background: var(--accent); border-radius: 999px; }
.meter meter::-webkit-meter-suboptimal-value { background: var(--warn); border-radius: 999px; }
.meter meter::-webkit-meter-even-less-good-value { background: var(--danger); border-radius: 999px; }
.meter meter::-moz-meter-bar { background: var(--accent); border-radius: 999px; }
.meter meter:-moz-meter-suboptimal::-moz-meter-bar { background: var(--warn); }
.meter meter:-moz-meter-sub-sub-optimum::-moz-meter-bar { background: var(--danger); }

/* Key box and code */
.keybox {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-3);
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 13.5px;
  overflow-wrap: anywhere;
  word-break: break-all;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.code {
  margin: 0;
  padding: 12px 14px;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.55;
  white-space: pre;
  tab-size: 2;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow-x: auto;
}

/* Auth pages */
.auth {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: var(--space-4);
  box-sizing: border-box;
}
.auth > .card { width: min(400px, 100%); }
.auth form.form { max-width: none; }

/* Phone and narrow tablet: sidebar becomes a top bar */
@media (max-width: 759px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: sticky;
    top: 0;
    z-index: 10;
    height: auto;
    overflow: visible;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-1) var(--space-2);
    padding: var(--space-2) var(--space-4);
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
  .brand { padding: 0 var(--space-2) 0 0; }
  .nav { flex-direction: row; flex-wrap: wrap; gap: 2px; }
  .nav-link { padding: 6px 8px; }
  .nav-section { margin: 0 var(--space-1) 0 var(--space-2); }
  .sidebar-foot { display: none; }
  .content { padding: var(--space-4); gap: var(--space-4); }
  .card, .stat { padding: var(--space-4); }
  .auth { padding: var(--space-4); }
}

@media (prefers-reduced-motion: reduce) {
  .toast { animation: none; }
  .nav-link, button { transition: none; }
}
