:root {
  color-scheme: dark;
  --bg: #0a0a0c;
  --fg: #e8e8ec;
  --dim: #9b9ba3;
  --good: #7bd88f;
  --warn: #f2c14e;
  --bad:  #f06a6a;
  --panel: #15151a;
  --line: #2a2a32;
  --accent: #6ea8ff;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; background: var(--bg); color: var(--fg);
  font-family: ui-sans-serif, -apple-system, "Segoe UI", Roboto, sans-serif; }

#status-bar {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel);
  position: sticky; top: 0; z-index: 10;
}
#status-dot {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--warn); box-shadow: 0 0 8px currentColor;
}
#status-dot.ok { background: var(--good); }
#status-dot.warn { background: var(--warn); }
#status-dot.bad { background: var(--bad); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: 0.4; } }

#app-name { font-weight: 700; color: var(--accent); letter-spacing: 0.5px; }
#device-label { font-weight: 600; color: var(--dim); font-size: 12px; }
#last-update {
  color: var(--dim); font-size: 0.9em; margin-left: auto; margin-right: 16px;
  font-variant-numeric: tabular-nums; padding: 4px 10px; border-radius: 999px;
  border: 1px solid transparent; transition: color 0.2s, background 0.2s;
}
#last-update.warn { color: var(--warn); border-color: rgba(242,193,78,0.4); background: rgba(242,193,78,0.06); }
#last-update.bad  { color: var(--bad);  border-color: rgba(240,106,106,0.5); background: rgba(240,106,106,0.10); font-weight: 600; animation: blink 1.5s infinite; }
#preset-nav { display: flex; gap: 6px; align-items: center; }
#preset-nav button {
  background: transparent; color: var(--fg); border: 1px solid var(--line);
  padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 14px;
}
#preset-nav button.active { border-color: var(--accent); color: var(--accent); }
#preset-nav button:hover { background: rgba(255,255,255,0.04); }
#logout-btn { color: var(--dim); margin-left: 12px; }
#logout-btn:hover { color: var(--bad); border-color: var(--bad); }

#reconnect-banner {
  background: var(--bad); color: #fff; padding: 10px 16px;
  display: flex; align-items: center; justify-content: center; gap: 16px;
  font-weight: 600; position: sticky; top: 48px; z-index: 9;
}
#reconnect-banner[hidden] { display: none; }
#reconnect-banner button {
  background: rgba(0,0,0,0.25); color: #fff; border: 1px solid rgba(255,255,255,0.4);
  padding: 4px 12px; border-radius: 6px; cursor: pointer; font-weight: 600;
}
#reconnect-banner button:hover { background: rgba(0,0,0,0.4); }

#dashboard { padding: 16px; min-height: calc(100vh - 60px); }

/* ============ TV NIGHT ============ */
.preset.tv-night {
  display: grid; grid-template-rows: 1fr 1fr auto; gap: 24px;
  height: calc(100vh - 90px); background: #000; padding: 24px; border-radius: 12px;
}
.big-stat { display: flex; flex-direction: column; align-items: center; justify-content: center; }
.big-stat .label { font-size: clamp(20px, 3vw, 32px); color: var(--dim); }
.big-stat .value {
  font-size: clamp(96px, 22vw, 280px); font-weight: 800; line-height: 1;
  font-variant-numeric: tabular-nums;
}
.big-stat .unit { font-size: clamp(20px, 3vw, 36px); color: var(--dim); }
.big-stat.ox .value { color: var(--good); }
.big-stat.ox .value.warn { color: var(--warn); }
.big-stat.ox .value.bad { color: var(--bad); }
.big-stat.hr .value { color: var(--accent); }
.big-stat.hr .value.warn { color: var(--warn); }
.big-stat.hr .value.bad { color: var(--bad); }

.status-strip { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.chip {
  background: #1a1a22; border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 14px; font-size: clamp(14px, 1.4vw, 20px);
}

/* ============ TV DAY ============ */
.preset.tv-day {
  display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 2fr 1fr;
  gap: 16px; height: calc(100vh - 90px);
}
.chart-cell, .stat-cell {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px; display: flex; flex-direction: column;
}
.cell-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 12px; margin-bottom: 6px; flex-wrap: wrap;
}
.cell-title { font-size: 14px; color: var(--dim); }
.cell-value { font-variant-numeric: tabular-nums; }
.cell-value b {
  font-size: clamp(22px, 2.4vw, 32px); font-weight: 700; color: var(--fg);
  margin-right: 4px;
}
.cell-value span { color: var(--dim); font-size: 13px; }
.cell-value.ox b { color: var(--good); }
.cell-value.ox b.warn { color: var(--warn); }
.cell-value.ox b.bad  { color: var(--bad); }
.cell-value.hr b { color: var(--accent); }
.cell-value.hr b.warn { color: var(--warn); }
.cell-value.hr b.bad  { color: var(--bad); }
.cell-value.mv b { color: var(--warn); }
.cell-sub { color: var(--dim); font-size: 13px; margin-top: 4px; }
.chart { flex: 1; min-height: 100px; }
.stat-cell { align-items: center; justify-content: center; }
.value-xl { font-size: clamp(72px, 12vw, 160px); font-weight: 800; color: var(--good); line-height: 1; }
.value-xl.warn { color: var(--warn); }
.value-xl.bad { color: var(--bad); }
.unit-xl { color: var(--dim); font-size: 24px; margin-top: 4px; }

/* ============ DEBUG ============ */
.preset.debug {
  display: grid; grid-template-columns: 1fr 1fr; gap: 12px;
}
.preset.debug .chart-cell { height: 200px; }
.debug-grid {
  grid-column: 1 / -1;
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px;
  padding: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
}
.debug-grid > div { display: flex; align-items: baseline; gap: 6px; }
.debug-grid .k { color: var(--dim); font-size: 12px; min-width: 80px; }
.debug-grid b { font-variant-numeric: tabular-nums; font-size: 18px; }
.debug-grid .u { color: var(--dim); font-size: 12px; }

/* ============ mobile ============ */
@media (max-width: 700px) {
  /* Header wraps to two rows: row 1 = dot + Ivy + freshness pill (right),
     row 2 = preset switcher + logout (full width). DSN is dropped -- it's
     a long opaque string and we only ever have one device. */
  #status-bar {
    flex-wrap: wrap;
    gap: 8px 10px;
    padding: 8px 12px;
  }
  #device-label { display: none; }
  #last-update { margin-left: auto; margin-right: 0; font-size: 0.85em; padding: 3px 9px; }
  #preset-nav {
    flex-basis: 100%;
    order: 99;
    justify-content: flex-start;
    gap: 6px;
  }
  #preset-nav button {
    padding: 5px 10px;
    font-size: 13px;
    white-space: nowrap;
  }
  #logout-btn { margin-left: auto; }
  /* Header is taller on mobile (~92px) -- bump the banner so it doesn't slide
     under the navbar when active. */
  #reconnect-banner { top: 92px; }

  /* On phones we scroll the page vertically, so the strict 2x2 layout is
     replaced with one column and each cell gets its own explicit height -- a
     bare `flex: 1` inside `height: auto` collapses to zero. */
  .preset.tv-day {
    grid-template-columns: 1fr;
    grid-template-rows: none;
    height: auto;
  }
  .preset.tv-day > .chart-cell { height: 260px; }
  .preset.tv-day > .stat-cell  { height: 200px; }

  .preset.debug { grid-template-columns: 1fr; }
  .preset.debug > .chart-cell { height: 200px; }
}

/* uPlot dark tweaks */
.u-legend { color: var(--dim); }
.u-axis { color: var(--dim); }
