/* F.U.N. -- mobile-first styling.
 *
 * Dark by default: this gets used at a grill or in a kitchen at night, where a
 * white screen is unpleasant and wrecks night vision. Sizing uses clamp() so
 * the big numbers stay readable from arm's length on a phone but do not become
 * absurd if the page is opened on a desktop.
 */

:root {
  color-scheme: dark;

  --bg: #0f1113;
  --surface: #191d21;
  --surface-2: #22272c;
  --line: #2e343a;

  --text: #e8eaed;
  --muted: #9aa0a6;

  /* Warm for the food interior, cool for the surroundings. The pairing is
   * intuitive, and the two are also distinguished by label and position, so it
   * does not rely on colour perception alone. */
  /* UI text colours for the two sensors. These are large-type readouts, so
   * they are judged on text contrast, not on the categorical-mark rules. */
  --food: #ff8a4c;
  --ambient: #56b6ff;

  /* Chart MARK colours: the documented dark-mode steps of the same two hues.
   * Validated against both chart surfaces with the palette validator:
   * lightness band, chroma floor, CVD separation (worst adjacent dE 26.8
   * protan), normal-vision floor (dE 31.8) and >=3:1 contrast all pass.
   * The brighter UI values above sit at L~0.75, outside the dark band, so they
   * are deliberately NOT reused as mark colours. */
  --series-food: #d95926;
  --series-ambient: #3987e5;
  --chart-surface: #0f1113;

  --live: #4ade80;
  --stale: #fbbf24;
  --offline: #6b7280;
  --danger: #f87171;

  --radius: 14px;
  --tap: 48px; /* minimum comfortable touch target */

  --pad-top: max(12px, env(safe-area-inset-top));
  --pad-bottom: max(16px, env(safe-area-inset-bottom));
  --pad-x: max(14px, env(safe-area-inset-left), env(safe-area-inset-right));
}

* { box-sizing: border-box; }

/* The browser's own [hidden] rule is `display: none` from the UA stylesheet,
 * which ANY author `display` declaration outranks. Several elements here are
 * toggled via the hidden attribute while also carrying a class that sets
 * display -- .chip (inline-flex) and .probes (flex) -- so without this they
 * stay visible when hidden. Needs !important to beat those class rules. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

body {
  /* Removes the 300ms grey flash on tap without disabling focus styles. */
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ---------------------------------------------------------------- topbar */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: var(--pad-top) var(--pad-x) 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}

.brand {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: 0.14em;
}

.topbar-actions { display: flex; gap: 8px; }

.chip {
  min-height: 40px;
  min-width: 52px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  /* Required, not decorative: min-width makes the pill wider than its content
   * for short labels like "?" and "°C", and without this the slack all lands
   * on the right, shoving the glyph left. A button's default
   * `text-align: center` does nothing here, because flex layout ignores it. */
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.chip[aria-pressed="true"] {
  background: color-mix(in srgb, var(--live) 22%, var(--surface));
  border-color: color-mix(in srgb, var(--live) 55%, var(--line));
}
.chip-text { font-size: 0.85rem; }

/* ---------------------------------------------------------------- layout */

#main {
  flex: 1;
  padding: 14px var(--pad-x) var(--pad-bottom);
  display: flex;
  flex-direction: column;
  gap: 14px;
  /* Keep content comfortable if this is opened on a tablet or desktop. */
  width: 100%;
  max-width: 720px;
  margin-inline: auto;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
}

.notice { border-color: color-mix(in srgb, var(--stale) 45%, var(--line)); }
.notice h2 { margin: 0 0 8px; font-size: 1.05rem; }
.notice p { margin: 0 0 10px; color: var(--muted); }

.flag {
  display: block;
  padding: 10px 12px;
  margin-bottom: 10px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 0.82rem;
  /* Long chrome:// URL must not blow out the layout on a narrow screen. */
  overflow-wrap: anywhere;
}

.hint { margin: 8px 0 0; color: var(--muted); font-size: 0.85rem; }

.empty h2 { margin: 0 0 6px; font-size: 1.05rem; }
.empty p { margin: 0; color: var(--muted); font-size: 0.9rem; }

/* ---------------------------------------------------------------- buttons */

.btn {
  min-height: var(--tap);
  padding: 0 18px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn:active { transform: scale(0.985); }

.btn-primary {
  background: var(--text);
  color: #101214;
  border-color: var(--text);
}
.btn-primary[data-scanning="true"] {
  background: color-mix(in srgb, var(--live) 24%, var(--surface));
  color: var(--text);
  border-color: color-mix(in srgb, var(--live) 55%, var(--line));
}

.btn-lg { width: 100%; min-height: 56px; font-size: 1.05rem; }
.btn-ghost { background: transparent; font-weight: 500; }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }

.scan-panel { display: flex; flex-direction: column; gap: 4px; }
.scan-panel .hint { text-align: center; }

/* ---------------------------------------------------------------- probes */

.probes { display: flex; flex-direction: column; gap: 14px; }

.probe { padding: 14px 16px 10px; }
.probe[data-status="offline"] { opacity: 0.62; }
.probe[data-selected="false"] .readings { display: none; }

.probe-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.probe-ident { min-width: 0; }

.probe-name {
  display: block;
  max-width: 100%;
  padding: 0;
  border: 0;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 1.02rem;
  font-weight: 650;
  text-align: left;
  cursor: pointer;
  /* A long nickname must truncate, not reflow the card. */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.probe-name::after { content: " \270E"; color: var(--muted); font-size: 0.8em; }

.probe-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.8rem;
}

.badge {
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: color-mix(in srgb, var(--offline) 30%, transparent);
  color: var(--muted);
}
.probe[data-status="live"] .badge {
  background: color-mix(in srgb, var(--live) 22%, transparent);
  color: var(--live);
}
.probe[data-status="stale"] .badge {
  background: color-mix(in srgb, var(--stale) 22%, transparent);
  color: var(--stale);
}

/* toggle switch -- 48px tall hit area, visually smaller */
.switch { position: relative; display: inline-flex; width: 52px; height: var(--tap); flex: none; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.switch-track {
  position: absolute;
  top: 50%;
  right: 0;
  translate: 0 -50%;
  width: 46px;
  height: 27px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  transition: background 0.15s;
}
.switch-track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 21px;
  height: 21px;
  border-radius: 50%;
  background: var(--muted);
  transition: translate 0.15s, background 0.15s;
}
.switch input:checked + .switch-track {
  background: color-mix(in srgb, var(--live) 30%, var(--surface-2));
  border-color: color-mix(in srgb, var(--live) 50%, var(--line));
}
.switch input:checked + .switch-track::after { translate: 19px 0; background: var(--live); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--text); outline-offset: 2px; }

/* ---------------------------------------------------------------- readings */

.readings {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 12px 0 6px;
}

.reading {
  display: grid;
  grid-template-areas: "label label" "value unit";
  grid-template-columns: auto 1fr;
  align-items: baseline;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid var(--line);
}

.reading-label {
  grid-area: label;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
  margin-bottom: 2px;
}

.reading-value {
  grid-area: value;
  /* Big enough to read at arm's length; tabular so digits do not jitter as the
   * value changes. */
  font-size: clamp(2rem, 12vw, 3.1rem);
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}
.reading-unit { grid-area: unit; padding-left: 4px; color: var(--muted); font-size: 0.9rem; }

.reading-food { border-color: color-mix(in srgb, var(--food) 40%, var(--line)); }
.reading-food .reading-value,
.reading-food .reading-label { color: var(--food); }

.reading-ambient { border-color: color-mix(in srgb, var(--ambient) 35%, var(--line)); }
.reading-ambient .reading-value,
.reading-ambient .reading-label { color: var(--ambient); }

/* ---------------------------------------------------------------- details */

.probe-more { margin-top: 4px; border-top: 1px solid var(--line); }
.probe-more summary {
  min-height: 44px;
  display: flex;
  align-items: center;
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
}

.kv {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  margin: 4px 0 12px;
  font-size: 0.85rem;
}
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }

.probe-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.probe-actions .btn { min-height: 42px; padding: 0 14px; font-size: 0.88rem; }

/* ---------------------------------------------------------------- footer */

.footer {
  padding: 14px var(--pad-x) var(--pad-bottom);
  color: var(--muted);
  font-size: 0.78rem;
  text-align: center;
  border-top: 1px solid var(--line);
}
.footer p { margin: 0; }
.footer a { color: var(--muted); }

/* Single column for the two readings on very narrow screens, so the numbers
 * keep their size instead of shrinking to fit side by side. */
@media (max-width: 340px) {
  .readings { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
  .btn:active { transform: none; }
}

/* Numbered remediation steps inside the notice card. Long paths like
 * "Settings → Apps → Chrome → …" must wrap rather than overflow on a phone. */
.steps {
  margin: 0 0 12px;
  padding-left: 22px;
  color: var(--text);
  font-size: 0.88rem;
}
.steps li { margin-bottom: 6px; overflow-wrap: anywhere; }
.steps li:last-child { margin-bottom: 0; }

/* ---------------------------------------------------------------- setup guide
 *
 * A readiness checklist rather than a wall of prose. Each row shows whether the
 * step is already satisfied, needs action, or cannot be verified from the
 * browser, so nobody re-checks something that is demonstrably fine.
 */

.setup { border-color: color-mix(in srgb, var(--ambient) 30%, var(--line)); }

.setup-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}
.setup-head h2 { margin: 0; font-size: 1.05rem; }

.btn-close {
  flex: none;
  width: 34px;
  height: 34px;
  margin: -6px -6px 0 0;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
}

.setup-intro { margin: 4px 0 12px; color: var(--muted); font-size: 0.9rem; }

.checklist {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.check { display: grid; grid-template-columns: 24px 1fr; gap: 10px; align-items: start; }

.check-mark {
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  font-size: 0.8rem;
  font-weight: 800;
  line-height: 1;
}
.check[data-state="ok"] .check-mark {
  background: color-mix(in srgb, var(--live) 22%, transparent);
  color: var(--live);
}
.check[data-state="action"] .check-mark {
  background: color-mix(in srgb, var(--stale) 24%, transparent);
  color: var(--stale);
}
.check[data-state="info"] .check-mark {
  background: var(--surface-2);
  color: var(--muted);
}

.check-body { min-width: 0; }

.check-label { margin: 2px 0 0; font-size: 0.94rem; font-weight: 600; }
/* A satisfied step is de-emphasised: the eye should land on what is left. */
.check[data-state="ok"] .check-label { font-weight: 500; color: var(--muted); }

.check-detail { margin: 4px 0 0; color: var(--muted); font-size: 0.85rem; }

.check-body .flag { margin: 8px 0 8px; }
.check-body .btn { min-height: 40px; padding: 0 14px; font-size: 0.85rem; }

/* Visible to screen readers only: the state glyphs are decorative, so the
 * state is also conveyed as words. */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* ---------------------------------------------------------------- chart
 *
 * Mark specs come from the project's dataviz guidance: 2px lines with round
 * caps, >=8px end dots ringed in the surface colour, hairline solid gridlines,
 * a legend always present for two series, direct end labels in text ink, and a
 * crosshair that snaps to the nearest time. Text never wears a series colour;
 * identity is carried by the coloured key beside it.
 */

.chart-panel { margin: 4px 0 12px; }

.chart-legend {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin: 0 0 8px;
  padding: 0;
  color: var(--muted);
  font-size: 0.8rem;
}
.chart-legend li { display: inline-flex; align-items: center; gap: 6px; }

/* A line key, mirroring the mark: these series are lines, not filled areas. */
.legend-key {
  width: 14px;
  height: 2px;
  border-radius: 1px;
  flex: none;
}
.legend-key-tip { background: var(--series-food); }
.legend-key-ambient { background: var(--series-ambient); }

.chart-wrap { position: relative; }

.chart-canvas {
  display: block;
  width: 100%;
  border-radius: 10px;
  border: 1px solid var(--line);
  /* Let a horizontal scrub drive the crosshair while vertical drags still
   * scroll the page. */
  touch-action: pan-y;
}

.chart-tip {
  position: absolute;
  top: 4px;
  translate: -50% 0;
  min-width: 108px;
  padding: 7px 9px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--surface-2) 94%, transparent);
  border: 1px solid var(--line);
  font-size: 0.78rem;
  pointer-events: none;
  backdrop-filter: blur(4px);
}
.chart-tip-when { color: var(--muted); margin-bottom: 3px; font-size: 0.72rem; }
.chart-tip-row { display: flex; align-items: center; gap: 6px; }
/* Value leads, series name follows. */
.chart-tip-row strong { font-variant-numeric: tabular-nums; }
.chart-tip-row span:last-child { color: var(--muted); }

/* The accessible path to the numbers: a canvas is opaque to assistive tech. */
.chart-table { margin-top: 8px; }
.chart-table summary {
  min-height: 40px;
  display: flex;
  align-items: center;
  color: var(--muted);
  font-size: 0.82rem;
  cursor: pointer;
}
.chart-table table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
}
.chart-table th {
  text-align: right;
  padding: 4px 6px;
  color: var(--muted);
  font-weight: 600;
  border-bottom: 1px solid var(--line);
}
.chart-table th:first-child, .chart-table td:first-child { text-align: left; }
.chart-table td { text-align: right; padding: 4px 6px; }

/* ---------------------------------------------------------------- target + alarm */

.target-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 2px 0 8px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}

.target-display { margin: 0; display: flex; align-items: baseline; gap: 8px; flex: 1 1 auto; }

.target-label {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--muted);
}
.target-value { font-size: 1rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.probe[data-alarm="off"] .target-value { color: var(--text); }

.target-buttons { display: inline-flex; gap: 6px; }
.target-row .btn { min-height: 40px; padding: 0 13px; font-size: 0.85rem; }

.target-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  flex: 1 1 100%;
}

.target-input {
  width: 5.5em;
  min-height: 40px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-variant-numeric: tabular-nums;
}
/* The spinner arrows waste width on a phone; inputmode already gives a keypad. */
.target-input::-webkit-outer-spin-button,
.target-input::-webkit-inner-spin-button { appearance: none; margin: 0; }
.target-input { appearance: textfield; }

.target-unit { color: var(--muted); font-size: 0.9rem; margin-right: 2px; }

/* A fired alarm is a state, not a series, so it wears the reserved status
 * colour and always ships with an icon and words -- never colour alone. */
.alarm-banner {
  display: flex;
  align-items: center;
  gap: 9px;
  margin: 10px 0 2px;
  padding: 10px 12px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--danger) 18%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--danger) 55%, var(--line));
  font-size: 0.9rem;
  font-weight: 600;
}
.alarm-icon {
  flex: none;
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--danger);
  color: #1a0d0d;
  font-weight: 800;
  font-size: 0.85rem;
}
.alarm-banner .btn {
  margin-left: auto;
  min-height: 38px;
  padding: 0 14px;
  background: var(--danger);
  border-color: var(--danger);
  color: #1a0d0d;
  font-weight: 700;
}

.probe[data-alarm="firing"] {
  border-color: color-mix(in srgb, var(--danger) 60%, var(--line));
}
/* Acknowledged but still over target: keep a quiet marker so the card does not
 * look identical to one that never alarmed. */
.probe[data-alarm="acked"] .target-value { color: var(--stale); }

@media (prefers-reduced-motion: no-preference) {
  .probe[data-alarm="firing"] { animation: alarm-pulse 1.6s ease-in-out infinite; }
  @keyframes alarm-pulse {
    0%, 100% { border-color: color-mix(in srgb, var(--danger) 60%, var(--line)); }
    50% { border-color: color-mix(in srgb, var(--danger) 20%, var(--line)); }
  }
}

/* Legend entries double as series toggles: tapping one hides that trace and
 * rescales the axis. Colour stays bound to the entity, so the remaining series
 * is never repainted. */
.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 0 4px;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}
.legend-item[aria-pressed="false"] { opacity: 0.45; }
.legend-item[aria-pressed="false"] span:last-child { text-decoration: line-through; }
.legend-item:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; border-radius: 6px; }

/* ---------------------------------------------------------------- views
 *
 * Exactly one top-level view is on screen. The body starts as data-view
 * ="loading" so neither the app nor the disclaimer flashes before JavaScript
 * decides which is appropriate.
 */

.view { display: none; }
body[data-view="main"] #main,
body[data-view="disclaimer"] #view-disclaimer,
body[data-view="declined"] #view-declined { display: flex; }

/* The topbar controls and footer belong to the app, not to the gate. */
body:not([data-view="main"]) .topbar,
body:not([data-view="main"]) .footer { display: none; }

.noscript {
  margin: 0;
  padding: 16px var(--pad-x);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  color: var(--text);
}

/* ---------------------------------------------------------------- gate */

.gate {
  flex-direction: column;
  min-height: 100dvh;
  padding: max(20px, env(safe-area-inset-top)) var(--pad-x) var(--pad-bottom);
}

.gate-inner {
  width: 100%;
  max-width: 620px;
  margin-inline: auto;
  display: flex;
  flex-direction: column;
}
.gate-inner-narrow { max-width: 420px; margin-top: 12vh; }

.gate-brand {
  margin: 0 0 18px;
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--muted);
}

.gate h2 {
  margin: 0 0 14px;
  font-size: clamp(1.35rem, 6vw, 1.7rem);
  line-height: 1.2;
}

.gate-body > section { margin-bottom: 18px; }

.gate-body h3 {
  margin: 0 0 5px;
  font-size: 0.95rem;
  font-weight: 700;
}

.gate-body p, .gate-body li {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.55;
}
/* Emphasis inside the body must survive the muted body colour, or the
 * genuinely important sentences read as quietly as everything else. */
.gate-body strong { color: var(--text); }

.gate-body ul { margin: 0; padding-left: 20px; }
.gate-body li { margin-bottom: 8px; }

.gate-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 8px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}
.gate-actions .hint { text-align: center; margin: 0; }

/* A disabled accept button must look deliberately not-yet-ready rather than
 * broken, since it is disabled by design for the first few seconds. */
.gate-actions .btn-primary:disabled {
  background: var(--surface-2);
  color: var(--muted);
  border-color: var(--line);
  cursor: default;
}
