/* ==========================================================================
   LLM Gateway Dashboard — status-driven visual language.
   Color job here is STATUS (state), not identity, so the reserved status
   palette is used throughout and every status is paired with an icon + label
   so meaning never rests on hue alone.
   ========================================================================== */

:root {
  color-scheme: light;

  --surface-1: #fcfcfb;
  --surface-2: #f4f4f1;
  --page: #f9f9f7;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.06), 0 4px 12px rgba(11, 11, 11, 0.04);

  /* Status palette — fixed, never themed, never reused as series colors. */
  --status-good: #0ca30c;
  --status-warning: #fab219;
  --status-serious: #ec835a;
  --status-critical: #d03b3b;
  --status-neutral: #898781;

  --track: #e1e0d9;
  --accent: #2a78d6;

  /* Status → fill mapping, resolved lazily so themed roles (accent) follow the mode. */
  --fill-ok: var(--status-good);
  --fill-warn: var(--status-warning);
  --fill-critical: var(--status-serious);
  --fill-exhausted: var(--status-critical);
  --fill-error: var(--status-critical);
  --fill-unlimited: var(--accent);
  --fill-unknown: var(--status-neutral);
  --fill-disabled: var(--status-neutral);

  --radius-lg: 14px;
  --radius-md: 10px;
  --radius-sm: 6px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-1: #1a1a19;
    --surface-2: #222220;
    --page: #0d0d0d;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 14px rgba(0, 0, 0, 0.3);
    --track: #2c2c2a;
    --accent: #3987e5;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --surface-2: #222220;
  --page: #0d0d0d;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --gridline: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 14px rgba(0, 0, 0, 0.3);
  --track: #2c2c2a;
  --accent: #3987e5;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.wrap {
  max-width: 1500px;
  margin: 0 auto;
  padding: 24px 20px 64px;
}

/* ---------- Header ---------- */

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
  margin-bottom: 22px;
}

.topbar h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.topbar .sub {
  margin: 2px 0 0;
  color: var(--text-secondary);
  font-size: 13px;
}

.topbar .spacer { flex: 1 1 auto; }

.meta {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-secondary);
  font-size: 12.5px;
}

.dot-live {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--status-good);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--status-good) 20%, transparent);
}
.dot-live.stale { background: var(--status-warning); box-shadow: 0 0 0 3px color-mix(in srgb, var(--status-warning) 20%, transparent); }
.dot-live.down { background: var(--status-critical); box-shadow: 0 0 0 3px color-mix(in srgb, var(--status-critical) 20%, transparent); }

button {
  font: inherit;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 12px;
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease;
}
button:hover { background: var(--surface-2); }
button:active { transform: translateY(0.5px); }
button:disabled { opacity: 0.55; cursor: default; }
button:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- KPI tiles ---------- */

.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(168px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.kpi {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}

.kpi .label {
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.kpi .value {
  margin-top: 4px;
  font-size: 28px;
  font-weight: 600;
  line-height: 1.15;
  letter-spacing: -0.02em;
}

.kpi .hint {
  color: var(--text-muted);
  font-size: 12px;
  margin-top: 2px;
}

.kpi.attention .value { color: var(--status-critical); }

/* ---------- Usage chart ---------- */

.chart-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 14px 16px 8px;
  margin-bottom: 20px;
}

.chart-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
}
.chart-head h2 { margin: 0; font-size: 14px; font-weight: 620; }
.chart-head-sub { color: var(--text-muted); font-weight: 500; font-size: 12.5px; }

.chart-toggle {
  margin-left: auto;
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.chart-toggle button {
  border: 0;
  border-radius: 0;
  background: var(--surface-1);
  padding: 5px 12px;
  font-size: 12.5px;
  color: var(--text-secondary);
}
.chart-toggle button:hover { background: var(--surface-2); }
.chart-toggle button.active {
  background: var(--accent);
  color: #fff;
}

.chart-wrap { position: relative; }

.chart-svg { width: 100%; height: auto; display: block; }

.chart-grid { stroke: var(--gridline); stroke-width: 1; }
.chart-axis-label { fill: var(--text-muted); font-size: 10.5px; }
.chart-area { fill: var(--accent); opacity: 0.12; stroke: none; }
.chart-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-dot { fill: var(--accent); stroke: var(--surface-1); stroke-width: 2; }
.chart-crosshair { stroke: var(--text-muted); stroke-width: 1; opacity: 0.5; }

.chart-tooltip {
  position: absolute;
  transform: translate(-50%, -100%) translateY(-10px);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  padding: 6px 10px;
  font-size: 12px;
  white-space: nowrap;
  pointer-events: none;
}
.chart-tooltip .value { font-weight: 620; color: var(--text-primary); }
.chart-tooltip .time { color: var(--text-muted); margin-top: 1px; }

/* ---------- Controls ---------- */

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 18px;
}

input[type="search"] {
  font: inherit;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 11px;
  min-width: 240px;
}
input[type="search"]::placeholder { color: var(--text-muted); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  font-size: 12.5px;
  cursor: pointer;
}
.chip[aria-pressed="true"] {
  background: var(--text-primary);
  border-color: var(--text-primary);
  color: var(--page);
}
.chip .swatch {
  width: 8px;
  height: 8px;
  border-radius: 2px;
  flex: none;
}
.chip .count { font-variant-numeric: tabular-nums; opacity: 0.75; }

/* ---------- Server section ---------- */

.server {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  margin-bottom: 16px;
  overflow: hidden;
}

.server-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  cursor: pointer;
  background: none;
  border: 0;
  width: 100%;
  text-align: left;
  border-radius: 0;
}
.server-head:hover { background: var(--surface-2); }

.server-head .caret {
  color: var(--text-muted);
  transition: transform 140ms ease;
  flex: none;
  font-size: 11px;
}
.server[data-collapsed="true"] .caret { transform: rotate(-90deg); }
.server[data-collapsed="true"] .server-body { display: none; }

.server-name {
  font-size: 15.5px;
  font-weight: 620;
  letter-spacing: -0.01em;
}

.server-host {
  color: var(--text-muted);
  font-size: 12.5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.server-stats {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 14px;
  color: var(--text-secondary);
  font-size: 12.5px;
}
.server-stats span { white-space: nowrap; }

.server-stats b {
  color: var(--text-primary);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.server-body { padding: 0 16px 16px; }

.server-error {
  margin: 0 16px 16px;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  border: 1px solid color-mix(in srgb, var(--status-critical) 35%, transparent);
  background: color-mix(in srgb, var(--status-critical) 8%, var(--surface-1));
  color: var(--text-primary);
  font-size: 13px;
}

/* ---------- Provider grid ---------- */

.provider-grid {
  display: grid;
  /* auto-fit (not auto-fill): collapses empty tracks so a lone provider card
     stretches across the full row instead of sitting at column width with a
     dead gap beside it. */
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  gap: 12px;
}

.provider {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-1);
  overflow: hidden;
}

.provider-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 0;
  border-bottom: 1px solid var(--border);
  border-radius: 0;
  width: 100%;
  text-align: left;
  cursor: pointer;
}
.provider-head:hover { background: color-mix(in srgb, var(--surface-2) 80%, var(--text-primary) 6%); }

.provider-name {
  font-weight: 600;
  font-size: 13.5px;
  letter-spacing: -0.005em;
}

.provider-meta {
  margin-left: auto;
  color: var(--text-muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.provider-caret {
  color: var(--text-muted);
  font-size: 15px;
  flex: none;
}

/* ---------- Account rows ---------- */

.account {
  padding: 11px 12px;
  border-top: 1px solid var(--gridline);
}
.account:first-child { border-top: 0; }
.account.inactive { opacity: 0.62; }

.account-head {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.account-label {
  font-weight: 550;
  font-size: 13px;
  overflow-wrap: anywhere;
}

.account-plan {
  color: var(--text-muted);
  font-size: 12px;
}

.account-note {
  margin-top: 6px;
  color: var(--text-secondary);
  font-size: 12px;
  overflow-wrap: anywhere;
}

/* Status pill: icon + label, so hue is never the only channel. */
.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px 2px 6px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 560;
  border: 1px solid;
  white-space: nowrap;
}
.pill .glyph { font-size: 10px; line-height: 1; }

.pill.ok        { color: var(--status-good);     border-color: color-mix(in srgb, var(--status-good) 40%, transparent);     background: color-mix(in srgb, var(--status-good) 10%, transparent); }
.pill.warn      { color: var(--status-warning);  border-color: color-mix(in srgb, var(--status-warning) 45%, transparent);  background: color-mix(in srgb, var(--status-warning) 12%, transparent); }
.pill.critical  { color: var(--status-serious);  border-color: color-mix(in srgb, var(--status-serious) 45%, transparent);  background: color-mix(in srgb, var(--status-serious) 12%, transparent); }
.pill.exhausted { color: var(--status-critical); border-color: color-mix(in srgb, var(--status-critical) 45%, transparent); background: color-mix(in srgb, var(--status-critical) 10%, transparent); }
.pill.unlimited { color: var(--accent);          border-color: color-mix(in srgb, var(--accent) 40%, transparent);          background: color-mix(in srgb, var(--accent) 10%, transparent); }
.pill.unknown,
.pill.disabled  { color: var(--text-muted);      border-color: var(--border);                                              background: var(--surface-2); }
.pill.error     { color: var(--status-critical); border-color: color-mix(in srgb, var(--status-critical) 45%, transparent); background: color-mix(in srgb, var(--status-critical) 10%, transparent); }

/* ---------- Quota meters ---------- */

.quotas { margin-top: 9px; display: grid; gap: 9px; }

.quota-top {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 4px;
}

.quota-name {
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: capitalize;
}

.quota-remaining {
  margin-left: auto;
  font-size: 12px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
}

/* Thin mark, 4px rounded data-end anchored to the track's start. */
.meter {
  position: relative;
  height: 8px;
  border-radius: 4px;
  background: var(--track);
  overflow: hidden;
}

.meter .fill {
  position: absolute;
  inset: 0 auto 0 0;
  border-radius: 4px;
  transition: width 400ms cubic-bezier(0.2, 0.7, 0.3, 1);
}

.fill.ok        { background: var(--fill-ok); }
.fill.warn      { background: var(--fill-warn); }
.fill.critical  { background: var(--fill-critical); }
.fill.exhausted { background: var(--fill-exhausted); }
.fill.unlimited { background: var(--fill-unlimited); }
.fill.unknown   { background: var(--fill-unknown); }

.quota-foot {
  margin-top: 4px;
  display: flex;
  gap: 10px;
  color: var(--text-muted);
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
}
.quota-foot .reset { margin-left: auto; white-space: nowrap; }
.quota-foot .reset.soon { color: var(--status-warning); font-weight: 560; }

/* ---------- Empty / loading states ---------- */

.state {
  background: var(--surface-1);
  border: 1px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: 40px 24px;
  text-align: center;
  color: var(--text-secondary);
}
.state h2 { margin: 0 0 6px; font-size: 16px; color: var(--text-primary); }
.state code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  background: var(--surface-2);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 12.5px;
}

.skeleton {
  height: 92px;
  border-radius: var(--radius-lg);
  background: linear-gradient(90deg, var(--surface-1), var(--surface-2), var(--surface-1));
  background-size: 200% 100%;
  animation: shimmer 1.3s linear infinite;
  margin-bottom: 12px;
}
@keyframes shimmer {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}

/* ---------- Recent requests modal ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 50;
}

.modal-panel {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 560px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 {
  margin: 0;
  font-size: 13px;
  font-weight: 650;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
}
.modal-sub { margin: 4px 0 0; font-size: 14px; font-weight: 600; color: var(--text-primary); }
.modal-close { margin-left: auto; padding: 4px 9px; }

.modal-body { padding: 4px 18px 16px; overflow-y: auto; }

.modal-chart { margin: 4px 0 14px; }
.modal-chart-title {
  margin: 0 0 4px;
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}

.requests-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.requests-table th {
  position: sticky;
  top: 0;
  text-align: left;
  color: var(--text-muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
  padding: 8px 6px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--gridline);
}
.requests-table th:last-child { text-align: right; }
.requests-table td { padding: 9px 6px; border-top: 1px solid var(--gridline); vertical-align: middle; }
.requests-table tr:first-child td { border-top: 0; }

.req-model {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px;
}
.req-dot { width: 7px; height: 7px; border-radius: 50%; flex: none; background: var(--status-good); }
.req-dot.error { background: var(--status-critical); }

.req-tokens { font-variant-numeric: tabular-nums; white-space: nowrap; }
.req-in { color: var(--status-serious); }
.req-out { color: var(--status-good); margin-left: 6px; }

.req-when { color: var(--text-muted); white-space: nowrap; text-align: right; }

.modal-hint { margin: 12px 2px 0; color: var(--text-muted); font-size: 11.5px; }

.modal-load-more {
  display: block;
  width: 100%;
  margin-top: 12px;
  padding: 8px;
  font-size: 12.5px;
  text-align: center;
}

.modal-error {
  margin-top: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid color-mix(in srgb, var(--status-critical) 35%, transparent);
  background: color-mix(in srgb, var(--status-critical) 8%, var(--surface-1));
  font-size: 12px;
}

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

@media (max-width: 640px) {
  .wrap { padding: 16px 12px 48px; }
  .provider-grid { grid-template-columns: 1fr; }
  .server-stats { margin-left: 0; width: 100%; }
}
