:root {
  --bg: #0e1116;
  --panel: #161b22;
  --panel-2: #1c232c;
  --line: #273040;
  --text: #e6edf3;
  --muted: #8b98a8;
  --accent: #4aa3ff;
  --ok: #3fb950;
  --bad: #f85149;
  --warn: #d29922;
  --derived: #a371f7;
  --radius: 10px;

  /* Synoptics indication colours, per the interface guide. Dark green has
     to stay legible on a dark panel, so it is lifted well above the
     background rather than being a literal dark green. */
  --alarm: #f85149;
  --clear: #3fb950;
  --running: #56d364;
  --stopped: #2d7d46;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .92em; }
.muted { color: var(--muted); font-size: .85rem; }

/* ------------------------------------------------------------- top bar */

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; flex-wrap: wrap;
  padding: .9rem 1.25rem;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.brand h1 { margin: 0; font-size: 1.1rem; letter-spacing: .2px; }
.brand .muted { display: block; }

.live { display: flex; align-items: center; gap: .5rem; font-size: .85rem; color: var(--muted); }
.live .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.live[data-state="open"] .dot { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.live[data-state="connecting"] .dot { background: var(--warn); }
.live[data-state="closed"] .dot { background: var(--bad); }

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

main { padding: 1.25rem; max-width: 1500px; margin: 0 auto; }

.device {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 1.25rem;
  overflow: hidden;
}
.device-head {
  display: flex; align-items: baseline; justify-content: space-between;
  gap: 1rem; flex-wrap: wrap;
  padding: .85rem 1rem;
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
}
.device-head h2 { margin: 0; font-size: 1rem; }
.device-meta { display: flex; align-items: center; gap: .6rem; }

.pill {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .5px;
  padding: .15rem .5rem; border-radius: 999px; border: 1px solid var(--line);
  color: var(--muted);
}
.pill.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 45%, transparent); }
.pill.bad { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 45%, transparent); }

.group { padding: .5rem 1rem 1rem; }
.group h3 {
  margin: .85rem 0 .55rem;
  font-size: .78rem; font-weight: 600;
  text-transform: uppercase; letter-spacing: .6px; color: var(--muted);
}

.tiles { display: grid; gap: .6rem; grid-template-columns: repeat(auto-fill, minmax(185px, 1fr)); }

/* ---------------------------------------------------------------- tile */

.tile {
  position: relative;
  display: flex; flex-direction: column; gap: .35rem;
  padding: .7rem .8rem;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: inherit; font: inherit; text-align: left;
  cursor: pointer;
  transition: border-color .15s, transform .05s;
}
.tile:hover { border-color: var(--accent); }
.tile:active { transform: translateY(1px); }
.tile.stale { opacity: .45; }
.tile.derived { border-left: 2px solid var(--derived); }

.tile-name { font-size: .8rem; color: var(--muted); }

/* Anything in a tile's top right corner -- the "derived" tag, the alarm
 * marker -- sits outside the flow, so a long name such as "Immersion Heater
 * Energy" runs underneath it. Reserving the space only on the tiles that
 * have a corner marker avoids making every other name wrap early. */
.tile.derived .tile-name { padding-right: 3.4rem; }
.tile[data-level="alarm"] .tile-name { padding-right: 1.2rem; }
.tile-value { display: flex; align-items: baseline; gap: .25rem; }
.tile-value .v { font-size: 1.45rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.tile-value .u { font-size: .85rem; color: var(--muted); }

.tile .tag {
  position: absolute; top: .45rem; right: .5rem;
  font-size: .6rem; text-transform: uppercase; letter-spacing: .5px;
  color: var(--derived);
}

/* Brief highlight when a value changes, so activity is visible at a glance. */
@keyframes flash { from { background: color-mix(in srgb, var(--accent) 22%, transparent); } }
.tile.updated { animation: flash .7s ease-out; }

/* ------------------------------------------------ synoptics indications */

/* The guide assigns a colour per state:
 *   alarm    1 = on/alarm    red
 *   clear    0 = no alarm    green
 *   running  1 = on/running  light green
 *   stopped  0 = off/stopped dark green
 *
 * Every one keeps its text, so colour reinforces the wording instead of
 * replacing it. That matters for red/green confusion, and it is why the
 * alarm state also carries a marker in the corner rather than relying on
 * hue alone.
 *
 * These rules follow the tile block deliberately: .tile.derived also sets
 * border-left at the same specificity, and would otherwise win and paint a
 * running derived sensor purple instead of green.
 */
.tile[data-level] { border-left: 3px solid transparent; }

.tile[data-level="alarm"] {
  border-left-color: var(--alarm);
  background: color-mix(in srgb, var(--alarm) 16%, var(--panel-2));
}
.tile[data-level="alarm"] .v { color: var(--alarm); font-weight: 600; }
.tile[data-level="alarm"]::after {
  content: "!";
  position: absolute; bottom: .35rem; right: .5rem;
  font-size: .8rem; font-weight: 700; color: var(--alarm);
}

.tile[data-level="clear"] { border-left-color: var(--clear); }
.tile[data-level="clear"] .v { color: var(--clear); }

.tile[data-level="running"] {
  border-left-color: var(--running);
  background: color-mix(in srgb, var(--running) 10%, var(--panel-2));
}
.tile[data-level="running"] .v { color: var(--running); }

.tile[data-level="stopped"] { border-left-color: var(--stopped); }
.tile[data-level="stopped"] .v { color: var(--stopped); }

/* A stale reading is untrustworthy whatever its colour, so grey wins. It
   would be worse to show a confident green on a value that stopped
   arriving an hour ago than to show nothing. */
.tile.stale[data-level] { border-left-color: var(--line); background: var(--panel-2); }
.tile.stale[data-level] .v { color: var(--muted); }
.tile.stale[data-level="alarm"]::after { color: var(--muted); }

/* States read as words, not measurements, so they do not need the large
   tabular figures that a temperature does. */
.tile[data-level] .tile-value .v { font-size: 1.1rem; }
.tile[data-level=""] .tile-value .v { font-size: 1.45rem; }

/* ------------------------------------------------------------ controls */

.controls { display: grid; gap: .7rem; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); }
.control {
  padding: .75rem .85rem;
  background: var(--panel-2);
  border: 1px dashed var(--line);
  border-radius: var(--radius);
}
.control.disabled { opacity: .72; }
.control-head { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; }
.control-name { font-size: .9rem; font-weight: 600; }
.control-body { display: flex; align-items: center; gap: .5rem; margin: .6rem 0 .45rem; }
.control-body input[type="number"] {
  width: 6rem; padding: .35rem .5rem;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 6px;
  font: inherit; font-variant-numeric: tabular-nums;
}
.control-body .u { color: var(--muted); font-size: .85rem; }
.control-body .apply {
  margin-left: auto; padding: .35rem .8rem;
  background: var(--bg); color: var(--muted);
  border: 1px solid var(--line); border-radius: 6px;
  font: inherit; cursor: not-allowed;
}
.control-foot { font-size: .78rem; }

.switch { position: relative; display: inline-block; width: 42px; height: 23px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; inset: 0; background: var(--line);
  border-radius: 999px; transition: .2s;
}
.slider::before {
  content: ""; position: absolute; height: 17px; width: 17px;
  left: 3px; top: 3px; background: var(--muted);
  border-radius: 50%; transition: .2s;
}

.note {
  margin: 1rem 0 0; padding: .7rem .85rem;
  background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius);
  color: var(--muted); font-size: .82rem;
}

/* --------------------------------------------------------------- modal */

.modal {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  background: rgba(0, 0, 0, .65);
  padding: 1rem;
}
.modal[hidden] { display: none; }
.modal-card {
  width: min(960px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem 1.1rem 1.2rem;
}
.modal-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; }
.modal-head h3 { margin: 0 0 .15rem; font-size: 1rem; }
.close {
  background: none; border: none; color: var(--muted);
  font-size: 1.6rem; line-height: 1; cursor: pointer; padding: 0 .2rem;
}
.close:hover { color: var(--text); }

.ranges { display: flex; gap: .35rem; margin: .8rem 0; flex-wrap: wrap; }
.ranges button {
  padding: .3rem .7rem;
  background: var(--panel-2); color: var(--muted);
  border: 1px solid var(--line); border-radius: 6px;
  font: inherit; font-size: .82rem; cursor: pointer;
}
.ranges button:hover { color: var(--text); }
.ranges button.active { background: var(--accent); color: #08121f; border-color: var(--accent); }

#chart-canvas { width: 100%; height: auto; display: block; border-radius: 6px; background: var(--bg); }

.chart-stats {
  display: flex; gap: 1.25rem; flex-wrap: wrap;
  margin-top: .8rem; font-size: .85rem; color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.chart-stats strong { color: var(--text); font-weight: 600; }

@media (max-width: 600px) {
  main { padding: .75rem; }
  .tiles { grid-template-columns: repeat(auto-fill, minmax(145px, 1fr)); }
}
