/* Temp Monitor — phone-first PWA.
 *
 * Colour tokens follow the validated data-viz reference palette. The series
 * slots are assigned to ROOMS IN FIXED ORDER and never cycled or re-ranked:
 * a room keeps its hue when you filter the others out, so "the bedroom is the
 * blue line" stays true.
 *
 * Both modes are selected, not flipped: the dark series values are the same
 * eight hues re-stepped for the dark surface, validated against it.
 */

:root {
  color-scheme: light dark;

  --page:        #f9f9f7;
  --surface:     #fcfcfb;
  --ink:         #0b0b0b;
  --ink-2:       #52514e;
  --muted:       #898781;
  --grid:        #e1e0d9;
  --axis:        #c3c2b7;
  --hairline:    rgba(11, 11, 11, 0.10);
  --raised:      rgba(11, 11, 11, 0.04);

  /* Categorical slots — rooms, in fixed order. */
  --series-1: #2a78d6;  /* blue */
  --series-2: #eb6834;  /* orange */
  --series-3: #1baf7a;  /* aqua */
  --series-4: #eda100;  /* yellow */
  --series-5: #e87ba4;  /* magenta */
  --series-6: #008300;  /* green */
  --series-7: #4a3aa7;  /* violet */
  --series-8: #e34948;  /* red */

  /* Outdoor is a REFERENCE series, not a peer: muted ink + a dashed stroke,
     so it never competes with a room for a categorical slot. */
  --outdoor: #898781;

  /* Status — reserved, never reused as a series colour. */
  --good:     #0ca30c;
  --warning:  #fab219;
  --serious:  #ec835a;
  --critical: #d03b3b;

  --radius: 14px;
  --tap: 44px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    --page:     #0d0d0d;
    --surface:  #1a1a19;
    --ink:      #ffffff;
    --ink-2:    #c3c2b7;
    --muted:    #898781;
    --grid:     #2c2c2a;
    --axis:     #383835;
    --hairline: rgba(255, 255, 255, 0.10);
    --raised:   rgba(255, 255, 255, 0.05);

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
  }
}

* { box-sizing: border-box; }

/* Any explicit `display` in a rule below beats the UA default that makes
 * [hidden] disappear — and several of these elements are grid/flex layouts
 * that start hidden (the token form, both panels). Without this they render
 * on top of the app. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--page);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; font-weight: 650; }
h3 { font-size: 0.95rem; margin: 22px 0 8px; color: var(--ink-2); }

/* ───────────────────────────── sign in ───────────────────────────── */

#auth {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: max(24px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
}
.auth-box { width: 100%; max-width: 320px; text-align: center; }
.auth-mark { color: var(--series-1); margin-bottom: 10px; }
.auth-box h1 { font-size: 1.35rem; }
#auth-msg { color: var(--ink-2); margin: 8px 0 22px; font-size: 0.95rem; }
#auth-msg.error { color: var(--critical); }

#pin-display {
  height: 22px;
  letter-spacing: 12px;
  font-size: 1.4rem;
  margin-bottom: 18px;
  color: var(--ink);
}
#pin-keys { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
#pin-keys button {
  height: 62px;
  border: 1px solid var(--hairline);
  border-radius: 50%;
  background: var(--surface);
  font-size: 1.3rem;
}
#pin-keys button:active { background: var(--raised); }
#pin-keys .pin-ok { color: var(--series-1); }

#auth-form { display: grid; gap: 10px; }
#token-input, .panel-body input, .panel-body select {
  padding: 11px 12px;
  border: 1px solid var(--hairline);
  border-radius: 10px;
  background: var(--surface);
  min-width: 0;
}
.btn-primary {
  padding: 12px 16px;
  border: 0;
  border-radius: 10px;
  background: var(--series-1);
  color: #fff;
  font-weight: 600;
  min-height: var(--tap);
}
.btn-primary:disabled { opacity: 0.5; }
.text-btn {
  background: none;
  border: 0;
  color: var(--series-1);
  padding: 10px 4px;
  font-size: 0.92rem;
  min-height: var(--tap);
}
.text-btn.danger { color: var(--critical); }

/* ───────────────────────────── chrome ────────────────────────────── */

#topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--hairline);
  padding-top: env(safe-area-inset-top);
}
.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
}
.bar h1 { font-size: 1.1rem; }
.bar-actions { display: flex; gap: 2px; }
.bar-actions button {
  position: relative;
  width: var(--tap);
  height: var(--tap);
  display: grid;
  place-items: center;
  border: 0;
  background: none;
  color: var(--ink-2);
  border-radius: 10px;
}
.bar-actions button:active { background: var(--raised); }
.dot {
  position: absolute;
  top: 9px;
  right: 9px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--critical);
  /* 2px surface ring so the dot reads as separate from the glyph beneath. */
  box-shadow: 0 0 0 2px var(--page);
}

#banner {
  margin: 0;
  padding: 9px 16px;
  font-size: 0.85rem;
  background: color-mix(in srgb, var(--warning) 22%, var(--page));
  color: var(--ink);
  border-top: 1px solid var(--hairline);
}
#banner.error { background: color-mix(in srgb, var(--critical) 18%, var(--page)); }

main {
  padding: 14px 16px calc(28px + env(safe-area-inset-bottom));
  max-width: 780px;
  margin: 0 auto;
}

/* ─────────────────────────── room tiles ──────────────────────────── */

.tiles { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(158px, 1fr)); }

.tile {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: var(--radius);
  padding: 13px 14px 14px;
  position: relative;
  overflow: hidden;
}
/* The room's own categorical hue, as a thin edge — identity without painting
   the whole card in a saturated block. */
.tile::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 3px;
  background: var(--tile-color, var(--muted));
}
.tile.outdoor::before { background: var(--outdoor); }

.tile-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.tile-name {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--ink-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* Hero figures use proportional digits — tabular-nums makes large numbers
   look loose. Tabular is reserved for the table and the axis ticks. */
.tile-temp { font-size: 2.1rem; font-weight: 640; line-height: 1.05; letter-spacing: -0.02em; }
.tile-temp .deg { font-size: 1.1rem; font-weight: 500; color: var(--ink-2); margin-left: 1px; }
.tile-sub { margin-top: 5px; font-size: 0.82rem; color: var(--ink-2); display: flex; flex-wrap: wrap; gap: 4px 10px; }
.tile-foot { margin-top: 8px; font-size: 0.74rem; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; }

.chip-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 0.72rem;
  border: 1px solid var(--hairline);
}
/* Status colours ship with a word, never colour alone. */
.chip-badge.warn     { background: color-mix(in srgb, var(--warning) 26%, var(--surface)); }
.chip-badge.serious  { background: color-mix(in srgb, var(--serious) 26%, var(--surface)); }
.chip-badge.critical { background: color-mix(in srgb, var(--critical) 22%, var(--surface)); }
.chip-badge.offline  { background: var(--raised); color: var(--ink-2); }

.empty, .status, .hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 10px 2px;
  line-height: 1.45;
}

/* ───────────────────────────── filters ───────────────────────────── */
/* One filter row above everything it scopes — never per-chart controls. */

#filters { margin: 18px 0 10px; display: grid; gap: 8px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chips button {
  padding: 7px 13px;
  border-radius: 999px;
  border: 1px solid var(--hairline);
  background: var(--surface);
  color: var(--ink-2);
  font-size: 0.86rem;
  min-height: 36px;
}
.chips button[aria-pressed="true"] { background: var(--series-1); border-color: var(--series-1); color: #fff; }
.chips.subtle button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--chip-color, var(--series-1));
  box-shadow: inset 0 0 0 1px var(--chip-color, var(--series-1));
}
.chips.subtle button[aria-pressed="false"] { opacity: 0.5; }
.chips .swatch { display: inline-block; width: 9px; height: 9px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
/* Matches the dashed stroke the outdoor line is drawn with, so the chip and
   the chart use the same visual language for "this is a reference series". */
.chips .swatch.dashed {
  width: 12px;
  height: 3px;
  border-radius: 0;
  background: repeating-linear-gradient(90deg, var(--outdoor) 0 4px, transparent 4px 7px);
}

/* ────────────────────────────── charts ───────────────────────────── */

.chart-card {
  margin: 0 0 12px;
  padding: 12px 12px 8px;
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: var(--radius);
}
/* Title and readout share a row when they fit and stack when they don't —
   two rooms plus a scrub timestamp overflows a 375px phone otherwise. */
.chart-card figcaption {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 2px 10px;
  margin-bottom: 6px;
}
.chart-title { font-size: 0.9rem; font-weight: 600; color: var(--ink-2); }
.readout {
  font-size: 0.78rem;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  margin-left: auto;
}

/* Sized to include the x-axis band, so the card never grows a nested scrollbar. */
.plot { position: relative; height: 190px; touch-action: pan-y; }
.plot canvas { width: 100%; height: 100%; display: block; }
#charts.loading .plot canvas { opacity: 0.45; transition: opacity 120ms; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 2px 2px 0; }
.legend span { display: inline-flex; align-items: center; gap: 6px; font-size: 0.82rem; color: var(--ink-2); }
.legend i { width: 14px; height: 3px; border-radius: 2px; display: inline-block; }
.legend i.dashed { background: repeating-linear-gradient(90deg, var(--outdoor) 0 4px, transparent 4px 7px); }

.chart-actions { display: flex; gap: 12px; }

#table-wrap { overflow-x: auto; margin-top: 6px; }
#data-table { border-collapse: collapse; width: 100%; font-size: 0.8rem; font-variant-numeric: tabular-nums; }
#data-table th, #data-table td { padding: 5px 9px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--hairline); }
#data-table th:first-child, #data-table td:first-child { text-align: left; }
#data-table thead th { color: var(--ink-2); font-weight: 600; position: sticky; top: 0; background: var(--surface); }

/* ────────────────────────────── panels ───────────────────────────── */

#scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 8;
}
.panel {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 9;
  max-height: 88dvh;
  display: flex;
  flex-direction: column;
  background: var(--page);
  border-top-left-radius: 18px;
  border-top-right-radius: 18px;
  border-top: 1px solid var(--hairline);
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--hairline);
}
.panel-head h2 { font-size: 1.05rem; }
.panel-close {
  border: 0;
  background: none;
  font-size: 1.7rem;
  line-height: 1;
  color: var(--ink-2);
  width: var(--tap);
  height: var(--tap);
}
.panel-body {
  overflow-y: auto;
  padding: 6px 16px calc(26px + env(safe-area-inset-bottom));
  -webkit-overflow-scrolling: touch;
}

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 0;
  border-bottom: 1px solid var(--hairline);
  font-size: 0.93rem;
}
.row > span:first-child { color: var(--ink-2); flex: 0 0 auto; }
.row input, .row select { max-width: 55%; text-align: right; }
.row.switch input { width: 46px; height: 28px; }
.unit { color: var(--muted); font-size: 0.85rem; }
.btn-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin: 6px 0; }

/* rules */
.rule { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--hairline); font-size: 0.9rem; }
.rule .rule-text { color: var(--ink); }
.rule .rule-state { font-size: 0.75rem; color: var(--muted); display: block; margin-top: 2px; }
.rule .rule-state.firing { color: var(--critical); }
.rule button { background: none; border: 0; color: var(--critical); font-size: 0.85rem; padding: 8px; }

.rule-form { margin-top: 12px; display: grid; gap: 10px; }
.rule-grid { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.rule-grid select, .rule-grid input { padding: 9px 10px; border: 1px solid var(--hairline); border-radius: 9px; background: var(--surface); }
.rule-grid #rule-threshold { width: 84px; text-align: right; }
.rule-for { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-2); font-size: 0.88rem; }
.rule-for input { width: 62px; text-align: right; }
.rule-unit { color: var(--ink-2); font-size: 0.9rem; }

.log { list-style: none; margin: 0; padding: 0; }
.log li { padding: 9px 0; border-bottom: 1px solid var(--hairline); font-size: 0.86rem; }
.log time { display: block; color: var(--muted); font-size: 0.74rem; margin-top: 2px; }
.log li.fired::before   { content: "▲ "; color: var(--critical); }
.log li.cleared::before { content: "▼ "; color: var(--good); }

/* devices */
.device { padding: 12px 0; border-bottom: 1px solid var(--hairline); }
.device-top { display: flex; align-items: center; gap: 8px; }
.device-top input { flex: 1; min-width: 0; text-align: left; }
.device-meta { font-size: 0.75rem; color: var(--muted); margin-top: 6px; display: flex; flex-wrap: wrap; gap: 4px 12px; }
.device-cal { display: flex; align-items: center; gap: 8px; margin-top: 8px; font-size: 0.82rem; color: var(--ink-2); flex-wrap: wrap; }
.device-cal input { width: 76px; text-align: right; }
.device-del { background: none; border: 0; color: var(--critical); font-size: 0.8rem; padding: 6px 0; }

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