/* ============================================================
   LLM Monitor — design system (tokens) + UI.
   All sizes, colors, radii, shadows and animation durations live
   in tokens; components only consume tokens (no ad-hoc px).
   Theme-scoped tokens: colors, the chart palette and shadows are
   defined per theme — :root holds dark (default), [data-theme=light]
   (set on <body>) overrides them. No var() indirection across theme
   blocks: a --x: var(--y) alias defined in :root resolves against
   the DARK values there and freezes them, which broke light theme.
   Theme-independent geometry (spacing/radius/type/motion/fonts)
   lives in :root only.
   ============================================================ */
:root {
  /* ---- surfaces / text / states (dark, default) ---- */
  --bg: #0e1116;
  --panel: #161b23;
  --panel-2: #1c222d;
  --border: #262e3b;
  --fg: #e6edf3;
  --muted: #8b97a7;
  --na: #5b6675;
  --accent: #4d9fff;
  --green: #3fb950;
  --amber: #d29922;
  --red: #f85149;
  --chip: #212836;

  /* ---- chart palette (stable per series) ---- */
  --chart-1: #4d9fff;   /* PP · prompt */
  --chart-2: #3fb950;   /* TG · gen */
  --chart-3: #d29922;   /* KV cache */
  --chart-4: #b18cff;   /* MTP acceptance */

  /* ---- elevation ---- */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .25);
  --shadow-md: 0 6px 18px rgba(0, 0, 0, .30);

  /* ---- geometry: spacing (4/8 rhythm), radius, type scale ---- */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 24px;
  --sp-6: 32px;
  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 14px;
  --radius-pill: 999px;
  --fs-xs: 11px;
  --fs-sm: 12px;
  --fs-md: 13px;
  --fs-base: 14px;
  --fs-lg: 16px;
  --fs-xl: 18px;
  --fs-num: 24px;

  /* ---- motion (fast = feedback, med = state change) ---- */
  --dur-fast: 120ms;
  --dur-med: 200ms;
  --ease: cubic-bezier(.2, .7, .3, 1);

  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
[data-theme="light"] {
  /* ---- surfaces / text / states (light) ---- */
  --bg: #f6f8fa;
  --panel: #ffffff;
  --panel-2: #f0f3f6;
  --border: #d0d7de;
  --fg: #1f2328;
  --muted: #656d76;
  --na: #8c959f;
  --accent: #0969da;
  --green: #1a7f37;
  --amber: #9a6700;
  --red: #cf222e;
  --chip: #eaeef2;

  /* ---- chart palette (light) ---- */
  --chart-1: #0969da;
  --chart-2: #1a7f37;
  --chart-3: #9a6700;
  --chart-4: #8250df;

  /* ---- elevation (light) ---- */
  --shadow-sm: 0 1px 2px rgba(31, 35, 40, .08);
  --shadow-md: 0 6px 18px rgba(31, 35, 40, .12);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--fg);
  font-family: var(--sans); font-size: var(--fs-base); line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; font-weight: 600; }
button { font-family: inherit; cursor: pointer; }
.mono { font-family: var(--mono); }
.na { color: var(--na); font-style: italic; }
.muted { color: var(--muted); }
.row { display: flex; align-items: center; gap: var(--sp-2); }
.spread { flex: 1; }

/* ---------- status dots (shared by header, tabs, banners) ---------- */
.dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--red); display: inline-block; flex: none;
}
.dot.on, .dot.online { background: var(--green); box-shadow: 0 0 6px var(--green); }
.dot.connecting, .dot.amber { background: var(--amber); }
.dot.off { background: var(--red); }

/* ---------- header ---------- */
#hdr {
  display: flex; align-items: center; gap: var(--sp-3);
  padding: 10px var(--sp-4); border-bottom: 1px solid var(--border);
  background: var(--panel); position: sticky; top: 0; z-index: 20;
  flex-wrap: wrap;
}
#hdr .logo { font-weight: 700; font-size: var(--fs-lg); letter-spacing: .3px; }
#hdr .logo b { color: var(--accent); }
.hdr-meta { font-size: var(--fs-sm); }
.badge-demo {
  background: var(--amber); color: #111; font-size: var(--fs-xs); font-weight: 700;
  padding: 3px 9px; border-radius: var(--radius-pill); letter-spacing: .5px;
}
.heart { font-size: var(--fs-sm); color: var(--muted); }
.icon-btn {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--fg);
  padding: 6px 10px; border-radius: var(--radius-sm); font-size: var(--fs-md);
  transition: border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.icon-btn:hover { border-color: var(--accent); }
.icon-btn.sm { padding: 3px 8px; font-size: var(--fs-xs); }
.icon-btn.danger { color: var(--red); }

/* ---------- tabs ---------- */
#tabs { display: flex; gap: 6px; padding: var(--sp-3) var(--sp-4) 0; flex-wrap: wrap; align-items: flex-end; }
.tab {
  display: flex; align-items: center; gap: var(--sp-2);
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius-sm) var(--radius-sm) 0 0; padding: 9px var(--sp-4);
  color: var(--muted); font-size: var(--fs-md); position: relative;
  border-bottom: none;
  transition: color var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease);
}
.tab:hover { color: var(--fg); }
.tab.active { background: var(--panel-2); color: var(--fg); }
.tab .rt-type { font-size: var(--fs-xs); color: var(--muted); }
.tab .del { opacity: .35; padding: 0 2px; }
.tab .del:hover { opacity: 1; color: var(--red); }
.tab.add { border-radius: var(--radius-sm); border-bottom: 1px solid var(--border); }
.rt-row { display: flex; gap: var(--sp-2); flex-wrap: wrap; width: 100%; padding-top: var(--sp-1); }
.rt-row-hint { font-size: var(--fs-sm); }
.rt-tab {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 5px 10px;
  color: var(--muted); font-size: var(--fs-sm);
  transition: color var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease);
}
.rt-tab:hover { color: var(--fg); }
.rt-tab.active { background: var(--panel-2); color: var(--fg); border-color: var(--accent); }
.rt-tab .dot { width: 7px; height: 7px; }

/* ---------- layout ---------- */
#main { padding: var(--sp-4) var(--sp-4) 48px; max-width: 1500px; margin: 0 auto; }
.section-title {
  font-size: var(--fs-sm); letter-spacing: .3px;
  color: var(--muted); margin: var(--sp-4) 0 var(--sp-3); font-weight: 600;
}
.section-sub { font-weight: 400; }

/* ---------- stat cards ---------- */
.cards {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: var(--sp-3); margin-top: var(--sp-3);
}
.card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: var(--sp-3) var(--sp-4);
  box-shadow: var(--shadow-sm);
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.card:hover {
  border-color: var(--border);
  border-color: color-mix(in srgb, var(--accent) 40%, var(--border));
}
.card .k { font-size: var(--fs-xs); color: var(--muted); letter-spacing: .3px; }
.card .v {
  font-size: var(--fs-num); font-weight: 700; margin-top: var(--sp-1);
  font-family: var(--mono); letter-spacing: -.5px;
}
.card .v .na { font-size: var(--fs-lg); }
.card .stats {
  display: flex; gap: var(--sp-2); flex-wrap: wrap;
  margin-top: var(--sp-2); font-size: var(--fs-xs); color: var(--muted); font-family: var(--mono);
}
.card.accent .v { color: var(--accent); }
.card.good .v { color: var(--green); }

/* ---------- controls: segmented window selector ---------- */
.controls { display: flex; align-items: center; gap: var(--sp-3); margin: var(--sp-4) 0 0; flex-wrap: wrap; }
.seg {
  display: inline-flex; align-items: center; gap: 2px;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: var(--radius-pill); padding: 2px;
}
.seg button {
  border: none; background: transparent; color: var(--muted);
  padding: 4px 10px; border-radius: var(--radius-pill); font-size: var(--fs-sm);
  transition: color var(--dur-fast) var(--ease), background var(--dur-fast) var(--ease),
              box-shadow var(--dur-fast) var(--ease);
}
.seg button:hover { color: var(--fg); }
.seg button.active {
  background: var(--panel); color: var(--fg);
  box-shadow: var(--shadow-sm); font-weight: 600;
}
.seg-sm button { padding: 3px 8px; font-size: var(--fs-xs); }
.hint { font-size: var(--fs-xs); color: var(--muted); }
.endpoint { font-size: var(--fs-sm); }

/* ---------- charts: 2×2 grid (1 column on narrow) ---------- */
.charts-grid {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--sp-3); margin: var(--sp-3) 0;
}
@media (max-width: 980px) { .charts-grid { grid-template-columns: 1fr; } }
.chart {
  position: relative;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: var(--sp-3) var(--sp-3) var(--sp-2);
  box-shadow: var(--shadow-sm);
}
.chart-h {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--sp-2); flex-wrap: wrap; margin-bottom: var(--sp-2);
}
.chart-t { font-size: var(--fs-md); font-weight: 600; display: inline-flex; align-items: center; gap: 6px; }
.chart-dotc { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.chart-svg { width: 100%; height: 170px; display: block; cursor: crosshair; }
.chart-range { font-size: var(--fs-xs); text-align: right; margin-top: 2px; }
.chart-empty { padding: 34px 10px; }
/* hover tooltip: fades via opacity only (position is instant — no lag/jitter) */
.chart-tip {
  position: absolute; z-index: 5; pointer-events: none; opacity: 0;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 6px 10px;
  font-size: var(--fs-sm); font-family: var(--mono); white-space: nowrap;
  box-shadow: var(--shadow-md);
  transition: opacity var(--dur-fast) var(--ease);
}
.chart-tip.on { opacity: 1; }
.chart-tip-t { color: var(--muted); font-size: var(--fs-xs); margin-bottom: 3px; }
.chart-tip-row { display: flex; align-items: center; gap: 6px; }
.chart-tip-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
/* line: stroke comes from the per-chart inline style (var(--chart-N));
   fill must be none — otherwise the path renders as a black-filled shape */
.cg-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
/* crosshair + marker: persistent nodes, smooth appear/disappear */
.cg-cross { transition: opacity var(--dur-fast) var(--ease); }
.cg-dot { transition: opacity var(--dur-fast) var(--ease); }
/* one-shot pulse on a NEW sample only (never on historical reloads);
   reduced-motion users get a static chart */
.cg-live { animation: cg-pulse .7s var(--ease) forwards; pointer-events: none; }
@keyframes cg-pulse { 0% { opacity: .9; } 100% { opacity: 0; } }

/* ---------- collapsible sections (spoilers) ---------- */
.spoiler {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); margin-bottom: var(--sp-3); overflow: hidden;
}
.spoiler-h {
  display: flex; align-items: center; gap: var(--sp-2); width: 100%;
  background: transparent; border: none; color: var(--fg);
  font-size: var(--fs-md); font-weight: 600; padding: var(--sp-3); text-align: left;
  transition: background var(--dur-fast) var(--ease);
}
.spoiler-h:hover { background: var(--panel-2); }
.spoiler-arrow { font-size: var(--fs-xs); color: var(--muted); width: 12px; flex: none; }
.spoiler-b { padding: 0 var(--sp-3) var(--sp-3); }
.spoiler-b:empty { padding: 0; }
.spoiler-b .panel { margin-bottom: 0; }
.spoiler-b.open { animation: fade-in var(--dur-med) var(--ease); }
@keyframes fade-in { from { opacity: 0; transform: translateY(-3px); } to { opacity: 1; transform: none; } }

/* ---------- tables ---------- */
table { width: 100%; border-collapse: collapse; font-size: var(--fs-md); }
th, td { text-align: left; padding: var(--sp-2) var(--sp-3); border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-size: var(--fs-xs); letter-spacing: .4px; font-weight: 600; }
tbody tr { transition: background var(--dur-fast) var(--ease); }
tbody tr:hover { background: var(--panel-2); }
td.mono { font-family: var(--mono); }
.td-sm { font-size: var(--fs-xs); }
.status-pill { padding: 2px 9px; border-radius: var(--radius-pill); font-size: var(--fs-xs); font-weight: 600; }
.status-pill.online { background: rgba(63,185,80,.15); color: var(--green); }
.status-pill.offline { background: rgba(248,81,73,.15); color: var(--red); }
.status-pill.connecting { background: rgba(210,153,34,.15); color: var(--amber); }
.status-pill.unknown { background: var(--chip); color: var(--muted); }

/* ---------- panels ---------- */
.panel {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; margin-bottom: var(--sp-3);
  box-shadow: var(--shadow-sm);
}
.panel .ph {
  padding: var(--sp-2) var(--sp-3); background: var(--panel-2);
  font-size: var(--fs-md); font-weight: 600;
  display: flex; align-items: center;
}
.panel .pb { padding: var(--sp-3); }
.sys-row { flex-wrap: wrap; gap: var(--sp-3) var(--sp-4); font-size: var(--fs-sm); font-family: var(--mono); }

/* ---------- banners / states ---------- */
.offline-panel {
  background: var(--panel); border: 1px solid var(--red);
  border-radius: var(--radius); padding: var(--sp-5); text-align: center;
  box-shadow: var(--shadow-sm); margin-bottom: var(--sp-3);
}
.offline-panel .big { font-size: var(--fs-lg); font-weight: 700; color: var(--red); }
.offline-panel .sub { color: var(--muted); margin-top: var(--sp-2); font-size: var(--fs-md); }
.no-runtime { text-align: center; margin-top: var(--sp-3); }
.no-runtime .big { font-size: var(--fs-lg); font-weight: 700; color: var(--amber); }
.no-runtime .sub { color: var(--muted); font-size: var(--fs-md); margin-top: var(--sp-2); }
.spin {
  display: inline-block; width: 14px; height: 14px;
  border: 2px solid var(--red); border-top-color: transparent; border-radius: 50%;
  animation: s .8s linear infinite; vertical-align: -2px; margin-right: var(--sp-2);
}
@keyframes s { to { transform: rotate(360deg); } }
.empty { color: var(--muted); font-size: var(--fs-md); padding: 20px; text-align: center; }

/* ---------- events ---------- */
.ev { display: flex; gap: var(--sp-2); padding: 7px 0; border-bottom: 1px solid var(--border); font-size: var(--fs-md); }
.ev:last-child { border-bottom: none; }
.ev .t { color: var(--muted); font-family: var(--mono); font-size: var(--fs-sm); min-width: 70px; }
.ev .lv { font-size: 10px; font-weight: 700; padding: 1px 7px; border-radius: var(--radius-sm); height: fit-content; }
.ev .lv.INFO { background: var(--chip); color: var(--muted); }
.ev .lv.WARN { background: rgba(210,153,34,.2); color: var(--amber); }
.ev .lv.ERROR { background: rgba(248,81,73,.2); color: var(--red); }
.ev-rt { margin-left: auto; }

/* ---------- auth ---------- */
.auth { min-height: 70vh; display: flex; align-items: center; justify-content: center; }
.auth-card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: var(--sp-6);
  width: 380px; max-width: 92vw; box-shadow: var(--shadow-md);
}
.auth-logo { margin-bottom: var(--sp-4); font-size: var(--fs-xl); font-weight: 700; }
.auth-logo b { color: var(--accent); }
.auth-title { font-size: var(--fs-lg); margin-bottom: var(--sp-1); }
.auth-sub { font-size: var(--fs-md); margin-bottom: var(--sp-4); }
.auth-err { color: var(--red); font-size: var(--fs-sm); margin-top: var(--sp-3); }
.auth-btn { width: 100%; margin-top: var(--sp-2); }

/* ---------- fields & buttons ---------- */
.field { margin-bottom: var(--sp-3); }
.field label { display: block; font-size: var(--fs-sm); color: var(--muted); margin-bottom: var(--sp-1); }
.field input, .field select {
  width: 100%; background: var(--panel-2); border: 1px solid var(--border);
  color: var(--fg); padding: 9px 11px; border-radius: var(--radius-sm); font-size: var(--fs-md);
  transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.field input:focus, .field select:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
}
.btn {
  background: var(--accent); color: #fff; border: none;
  padding: 9px var(--sp-4); border-radius: var(--radius-sm);
  font-size: var(--fs-md); font-weight: 600;
  transition: filter var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.btn:hover { filter: brightness(1.08); box-shadow: var(--shadow-sm); }
.btn.ghost { background: var(--panel-2); color: var(--fg); border: 1px solid var(--border); }
.btn.ghost:hover { border-color: var(--accent); filter: none; }
.btn:disabled { opacity: .5; cursor: default; }

/* ---------- modal ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 50;
  display: flex; align-items: center; justify-content: center;
}
.modal {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius-lg); width: 420px; max-width: 92vw;
  box-shadow: var(--shadow-md);
}
.modal .mh { padding: var(--sp-4) 18px; border-bottom: 1px solid var(--border); font-weight: 700; font-size: var(--fs-lg); }
.mh-sub { font-weight: 400; font-size: var(--fs-sm); }
.modal .mb { padding: 18px; }
.modal .mf { padding: 0 18px 18px; display: flex; gap: var(--sp-2); justify-content: flex-end; align-items: center; }
.mf-hint { margin-right: auto; font-size: var(--fs-xs); }
.testline { font-size: var(--fs-sm); margin-top: var(--sp-2); min-height: 16px; }
.testline-top { margin-bottom: var(--sp-3); }
.testline.ok { color: var(--green); }
.testline.err { color: var(--red); }
.testline.busy { color: var(--amber); }

/* ---------- users panel ---------- */
.users-modal { width: 640px; max-height: 85vh; overflow: auto; }
.u-form { display: flex; gap: var(--sp-2); flex-wrap: wrap; }
.u-form input, .u-form select {
  flex: 1; min-width: 120px; background: var(--panel-2);
  border: 1px solid var(--border); color: var(--fg);
  padding: 8px 10px; border-radius: var(--radius-sm); font-size: var(--fs-md);
  transition: border-color var(--dur-fast) var(--ease);
}
.u-form input:focus { outline: none; border-color: var(--accent); }
.u-actions { white-space: nowrap; }

/* ---------- setup ---------- */
.setup { text-align: center; padding: var(--sp-6) 0; }
.setup .big { font-size: var(--fs-xl); font-weight: 700; }
.setup .sub { color: var(--muted); font-size: var(--fs-md); margin-top: var(--sp-2); max-width: 520px; margin-left: auto; margin-right: auto; }
.setup-btn { margin-top: var(--sp-4); }

/* ---------- error boundary ---------- */
#err {
  position: fixed; inset: 0; background: var(--bg); z-index: 99;
  display: flex; align-items: center; justify-content: center; flex-direction: column; gap: var(--sp-2);
}
#err .big { font-size: var(--fs-lg); font-weight: 700; }
.fatal-msg { max-width: 70%; }

/* ---------- responsive ---------- */
@media (max-width: 640px) {
  #hdr { padding: 8px var(--sp-3); }
  #tabs { padding: var(--sp-3) var(--sp-3) 0; }
  #main { padding: var(--sp-3); }
}

/* ---------- motion preferences ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
  .cg-live { display: none; }
}

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 6px; }
