:root {
  --page: #e7ecea;
  --card: #fbfcfb;
  --field-sunk: #eef2f0;
  --ink: #16232a;
  --muted: #63767c;
  --rule: #d3dcda;
  --shadow: 0 1px 2px rgb(22 35 42 / 0.06), 0 4px 12px rgb(22 35 42 / 0.05);
  --radius: 12px;

  /* Metric hues carry meaning: the same colour marks a value in the strip,
     its button in the switch, and its line in the plot. */
  --temperature: #b4442c;
  --humidity: #26708f;
  --pressure: #40745a;
  --battery: #7a6a46;
  --rssi: #6b6076;
  --movement: #a23b72;

  /* Tag identity in the all-tags chart. Validated as a set for colour-blind
     separation against --card; the order is part of that, so don't cycle or
     reorder. Three light slots sit under 3:1 contrast, which is why lines
     carry name labels as well as colour. */
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;

  --gutter: clamp(1rem, 4vw, 3rem);
}

@media (prefers-color-scheme: dark) {
  :root {
    --page: #0b1113;
    --card: #151d20;
    --field-sunk: #0f1619;
    --ink: #e2e8e7;
    --muted: #8b9c9f;
    --rule: #263236;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 6px 16px rgb(0 0 0 / 0.2);
    --temperature: #e0785c;
    --humidity: #63b0cd;
    --pressure: #78b28f;
    --battery: #bda874;
    --rssi: #a99ab4;
    --movement: #d77fae;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
  }
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-family: "IBM Plex Sans", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

/* ---- masthead ---------------------------------------------------------- */

.masthead {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 1.5rem var(--gutter) 1.25rem;
}

.masthead h1 {
  margin: 0;
  font-size: 1.0625rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.masthead__state {
  margin: 0 0 0 auto;
  color: var(--muted);
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
}

.language { display: flex; gap: 0.125rem; font-size: 0.75rem; }

.language a {
  border: 1px solid transparent;
  border-radius: 3px;
  color: var(--muted);
  letter-spacing: 0.04em;
  padding: 0.125rem 0.375rem;
  text-decoration: none;
}

.language a:hover { color: var(--ink); }

.language a[aria-current="true"] {
  border-color: var(--rule);
  background: var(--field-sunk);
  color: var(--ink);
  font-weight: 500;
}

.logout { margin: 0; }

.logout button {
  appearance: none;
  background: none;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  font-size: 0.75rem;
  padding: 0.125rem 0.25rem;
}

.logout button:hover { color: var(--ink); }

/* ---- login ------------------------------------------------------------- */

.login {
  margin: 2rem auto 3rem;
  max-width: 22rem;
  padding: 1.75rem;
  width: calc(100% - 2 * var(--gutter));
}

.login h2 { font-size: 1.125rem; font-weight: 600; margin: 0 0 1.25rem; }

.login form { display: grid; gap: 0.375rem; }

.login label { color: var(--muted); font-size: 0.8125rem; }

.login input {
  background: var(--field-sunk);
  border: 1px solid var(--rule);
  border-radius: 6px;
  color: var(--ink);
  font: inherit;
  margin-bottom: 0.75rem;
  padding: 0.5rem 0.625rem;
  width: 100%;
}

.login__error { color: var(--temperature); font-size: 0.875rem; margin: 0 0 0.75rem; }

.login button {
  appearance: none;
  background: var(--ink);
  border: 0;
  border-radius: 6px;
  color: var(--card);
  cursor: pointer;
  font: inherit;
  font-weight: 500;
  padding: 0.5rem 1rem;
}

/* ---- reading cards ----------------------------------------------------- */

.strip {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 14rem), 1fr));
  gap: 1rem;
  padding: 0 var(--gutter);
}

.card {
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.reading {
  appearance: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
  padding: 1.125rem 1.25rem 1.25rem;
  transition: border-color 120ms ease, box-shadow 120ms ease, transform 120ms ease;
}

.reading:hover {
  border-color: color-mix(in srgb, var(--temperature) 40%, var(--rule));
  transform: translateY(-1px);
}

/* The selected tag is the one the chart below is drawing. */
.reading[aria-pressed="true"] {
  border-color: var(--temperature);
  box-shadow: var(--shadow), inset 0 0 0 1px var(--temperature);
}

.reading__name {
  color: var(--muted);
  font-size: 0.8125rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* The one place boldness is spent. */
.reading__temp {
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  color: var(--temperature);
}

.reading__temp em {
  font-style: normal;
  font-size: clamp(2rem, 5.5vw, 2.75rem);
  font-weight: 500;
  letter-spacing: -0.02em;
}

.reading__temp--absent { color: var(--muted); opacity: 0.5; }

.reading__temp .unit {
  font-size: 0.875rem;
  margin-left: 0.15em;
  vertical-align: 0.9em;
}

.reading__rest {
  display: flex;
  flex-wrap: wrap;
  gap: 0.0625rem 0.75rem;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
  font-size: 0.75rem;
}

.reading__rest [data-metric="humidity"] { color: var(--humidity); }
.reading__rest [data-metric="pressure"] { color: var(--pressure); }
.reading__rest [data-metric="battery"]  { color: var(--muted); }

.reading--stale { opacity: 0.55; }

/* All-tags view: no card is "the" selected one, and each name gets the dot
   matching its line in the chart. */
.comparing .reading[aria-pressed="true"] {
  border-color: var(--rule);
  box-shadow: var(--shadow);
}

.comparing .reading__name::before {
  content: "";
  display: inline-block;
  width: 0.625rem;
  height: 0.625rem;
  margin-right: 0.4rem;
  border-radius: 50%;
  background: var(--tag-colour, transparent);
  vertical-align: -0.05em;
}

/* ---- history ----------------------------------------------------------- */

.history {
  margin: 1rem var(--gutter) 3rem;
  padding: 1.25rem clamp(1rem, 2.5vw, 1.75rem) 1.5rem;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem 1.5rem;
  margin-bottom: 1.5rem;
}

.switch { display: flex; flex-wrap: wrap; gap: 0.25rem; }

.switch button {
  appearance: none;
  background: none;
  border: 1px solid transparent;
  border-radius: 3px;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  font-size: 0.8125rem;
  padding: 0.25rem 0.625rem;
}

.switch button:hover { color: var(--ink); }

.switch button[aria-pressed="true"] {
  border-color: var(--rule);
  background: var(--field-sunk);
  color: var(--ink);
  font-weight: 500;
}

.switch [data-metric="temperature"][aria-pressed="true"] { color: var(--temperature); }
.switch [data-metric="humidity"][aria-pressed="true"]    { color: var(--humidity); }
.switch [data-metric="pressure"][aria-pressed="true"]     { color: var(--pressure); }
.switch [data-metric="battery"][aria-pressed="true"]      { color: var(--battery); }
.switch [data-metric="rssi"][aria-pressed="true"]         { color: var(--rssi); }
.switch [data-metric="movement"][aria-pressed="true"]     { color: var(--movement); }

.switch--right { font-variant-numeric: tabular-nums; margin-left: auto; }
.switch--right + .switch { font-variant-numeric: tabular-nums; }

.switch [data-resolution][aria-pressed="true"] { color: var(--movement); }

/* ---- electricity ------------------------------------------------------- */

/* The card and its chart borrow the movement hue, so electricity reads as its
   own thing against the temperature-accented tags. */
.reading--electricity .reading__temp { color: var(--movement); }
.reading--electricity:hover {
  border-color: color-mix(in srgb, var(--movement) 40%, var(--rule));
}
.reading--electricity[aria-pressed="true"] {
  border-color: var(--movement);
  box-shadow: var(--shadow), inset 0 0 0 1px var(--movement);
}

/* Each view owns a set of controls: sensor tags get the metric and one/all
   switches; the energy cards (electricity, combined) get the resolution and
   unit switches. The range switch is shared and always shown. */
.controls--energy { display: none; }
body.energy-view .controls--sensor { display: none; }
body.energy-view .controls--energy { display: flex; }

/* The combined card has no single headline number; centre its label. */
.reading--combined { justify-content: center; }
.reading--combined .reading__name { color: var(--ink); font-size: 0.9375rem; }
.reading__combined { color: var(--muted); font-size: 0.8125rem; }
.reading--combined:hover {
  border-color: color-mix(in srgb, var(--movement) 40%, var(--rule));
}
.reading--combined[aria-pressed="true"] {
  border-color: var(--movement);
  box-shadow: var(--shadow), inset 0 0 0 1px var(--movement);
}

.plot { min-height: 320px; }

.plot__empty {
  color: var(--muted);
  font-size: 0.875rem;
  padding: 6rem 0;
  text-align: center;
}

.plot__note {
  color: var(--muted);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
  margin: 0.75rem 0 0;
}

/* uPlot overrides so the chart sits on the same instrument face */
.uplot { font-family: "IBM Plex Sans", system-ui, sans-serif; }
.u-legend {
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.75rem;
}
.u-legend th { font-weight: 500; }

/* ---- empty state ------------------------------------------------------- */

.empty {
  margin: 0 var(--gutter) 3rem;
  max-width: 34rem;
  padding: 2rem 1.75rem 1.5rem;
}

.empty h2 { font-size: 1.125rem; font-weight: 600; margin: 0 0 0.5rem; }
.empty p { margin: 0 0 0.75rem; }
.empty__hint { color: var(--muted); font-size: 0.875rem; }
.empty code {
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.8125em;
  background: var(--field-sunk);
  padding: 0.1em 0.35em;
  border-radius: 2px;
}

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