/* Dark by default (a monitor reads well dark); light via prefers-color-scheme. */
:root {
  --bg: #0d1117;
  --surface: #161b22;
  --surface-2: #1c222b;
  --border: #2a313c;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #4a9eff;
  --track: #30363d;
  --warning: #d29922;
  --critical: #f85149;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  --radius: 0.7rem;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  /* Everything below is sized in rem against this, so the whole interface scales with the display
     rather than staying at a size chosen for a 1080p screen. On a 4K panel at 100% browser zoom a
     fixed 14px base is physically tiny — this is what the user would otherwise fix by zooming to
     150%, done once here instead.

     13px at phone widths, 14px at 1280, 16px at 1920, 18px at 2560, 22px at 3840. */
  font-size: clamp(13px, 0.3125vw + 10px, 22px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f8fa;
    --surface: #ffffff;
    --surface-2: #f6f8fa;
    --border: #d0d7de;
    --text: #1f2328;
    --muted: #656d76;
    --accent: #0969da;
    --track: #eaeef2;
    --warning: #9a6700;
    --critical: #cf222e;
    --shadow: 0 1px 2px rgba(31, 35, 40, .08);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  font-size: 1.4rem;
  line-height: 1.4;
}

/* ---- top bar ---- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.14rem;
  padding: 0.86rem 1.43rem;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 0.71rem; font-size: 1.07rem; letter-spacing: .2px; }
.brand strong { font-weight: 650; }
.brand .dot {
  width: 0.64rem; height: 0.64rem; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 0.21rem color-mix(in srgb, var(--accent) 22%, transparent);
}

.controls { display: flex; gap: 0.57rem; }
.controls input {
  width: min(42vw, 24rem);
  padding: 0.5rem 0.71rem;
  font: 0.93rem/1.2 var(--mono);
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.57rem;
}
.controls input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.controls button {
  padding: 0.5rem 1rem;
  font: 600 0.93rem var(--sans);
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 0.57rem;
  cursor: pointer;
}
.controls button:hover { filter: brightness(1.08); }

/* ---- source buttons ---- */
.sources { display: flex; gap: 0.43rem; }
.controls .src {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.86rem;
  font: 600 0.93rem var(--sans);
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 0.57rem;
  cursor: pointer;
}
.controls .src:hover { filter: brightness(1.12); }
.controls .src.active {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}
.controls .src-quiet { color: var(--muted); font-weight: 500; }
.controls .src-quiet.active { color: #fff; }

/* Secondary buttons that are not data sources — deliberately a different class, so the source
   picker's selector cannot pick them up. */
.controls .ghost, .rails-actions .ghost {
  display: inline-flex;
  align-items: center;
  gap: 0.36rem;
  padding: 0.5rem 0.86rem;
  font: 500 0.93rem var(--sans);
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 0.57rem;
  cursor: pointer;
}
.controls .ghost:hover, .rails-actions .ghost:hover { color: var(--text); }
.controls .ghost.active {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

/* Reachability lamp. Grey until probed, so "unknown" never masquerades as "down". */
.lamp {
  width: 0.57rem; height: 0.57rem;
  border-radius: 50%;
  background: var(--muted);
  flex: none;
}
.src[data-live="yes"] .lamp {
  background: #3fb950;
  box-shadow: 0 0 0 0.21rem color-mix(in srgb, #3fb950 25%, transparent);
}
.src[data-live="no"] .lamp { background: var(--critical); opacity: .55; }
.src.active .lamp { box-shadow: none; }
.src.active[data-live="yes"] .lamp { background: #fff; }

/* The gear reads as the affordance on its own, so on a narrow bar the word can go and the button
   stays a recognisable target rather than wrapping the source picker onto another line. */
.gear { font-size: 1.07em; line-height: 1; }
.rails-head .gear { margin-right: 0.36rem; color: var(--muted); }

/* ---- inline configuration editor ---- */
.rails {
  padding: 0.86rem 1.43rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.rails[hidden] { display: none; }
.rails-head {
  display: flex;
  align-items: baseline;
  gap: 0.71rem;
  flex-wrap: wrap;
  margin-bottom: 0.57rem;
}
.rails-hint { flex: 1; min-width: 15.7rem; font-size: 0.86rem; color: var(--muted); }
.rails textarea {
  width: 100%;
  padding: 0.71rem 0.86rem;
  font: 0.86rem/1.55 var(--mono);
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.57rem;
  resize: vertical;
  tab-size: 2;
}
.rails textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.rails-errors {
  margin: 0.57rem 0 0;
  padding: 0.57rem 0.71rem;
  font: 0.86rem/1.5 var(--mono);
  white-space: pre-wrap;
  color: var(--critical);
  background: color-mix(in srgb, var(--critical) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--critical) 35%, transparent);
  border-radius: 0.57rem;
}
.rails-errors[hidden] { display: none; }
.rails-actions {
  display: flex;
  align-items: center;
  gap: 0.57rem;
  margin-top: 0.57rem;
  flex-wrap: wrap;
}
.rails-actions button {
  padding: 0.43rem 1rem;
  font: 600 0.93rem var(--sans);
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 0.57rem;
  cursor: pointer;
}
.rails-status { font-size: 0.86rem; color: var(--muted); }

/* ---- status bar ---- */
.statusbar {
  display: flex;
  align-items: center;
  gap: 0.86rem;
  padding: 0.71rem 1.43rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.machine-name { font: 600 1rem var(--mono); }
.spacer { flex: 1; }
.age { color: var(--muted); font-size: 0.86rem; }
.age.stale { color: var(--warning); }

.badge {
  padding: 0.14rem 0.57rem;
  font-size: 0.79rem;
  font-weight: 600;
  color: var(--muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.conn {
  display: inline-flex;
  align-items: center;
  gap: 0.43rem;
  font-size: 0.86rem;
  font-weight: 600;
  color: var(--muted);
}
.conn::before {
  content: "";
  width: 0.57rem; height: 0.57rem; border-radius: 50%;
  background: currentColor;
}
.conn[data-state="live"] { color: #3fb950; }
.conn[data-state="offline"] { color: var(--critical); }
.conn[data-state="waiting"] { color: var(--warning); }
.conn[data-state="connecting"] { color: var(--muted); }

/* ---- grid of cards ----
   Explicit columns filled shortest-first by app.js, rather than CSS grid or multi-column. A grid
   row is as tall as its tallest cell, so short cards leave dead space beneath them; multi-column
   balances to equal heights, so a few cards fill a few columns and leave the rest of the width
   empty. Flex columns each take an equal share of the full width, always.

   The column *count* is computed in app.js from a minimum width in em, so it follows the root font
   size — a bigger display gets wider columns, not merely more of them. */
.grid {
  display: flex;
  align-items: flex-start;
  gap: 0.86rem;
  padding: 1rem 1.14rem;
}

.col {
  flex: 1 1 0;
  /* Without this a wide card would push its column past its share and unbalance the row. */
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.86rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  width: 100%;
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.57rem;
  padding: 0.57rem 0.86rem;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}
.card-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge.kind { text-transform: uppercase; letter-spacing: .4px; flex: none; }

.group { padding: 0.14rem 0.86rem 0.43rem; }
.group-label {
  display: block;
  margin: 0.43rem 0 0.14rem;
  font-size: 1.3rem;
  font-weight: 700;
  letter-spacing: .6px;
  text-transform: uppercase;
  color: var(--muted);
  /* border-style: dotted; */

}

.sensor {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.57rem;
  padding: 0.07rem 0.29rem;
  margin: 0 -0.29rem;
  border-radius: 0.36rem;
}
.sensor.is-clickable { cursor: pointer; }
.sensor.is-clickable:hover { background: var(--surface-2); }
.sensor.is-clickable:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
/* The charted row stays marked while its chart is open, so the panel below is never orphaned. */
.sensor.is-charted { background: var(--surface-2); }
.sensor.is-charted .s-name { color: var(--accent); font-weight: 600; }

/* A derived rail, indented under the raw channel it is computed from. Its value is styled as a
   derivation rather than a reading — it is inferred from a divider ratio, not measured. */
.sensor-rail { padding-top: 0; }
.sensor-rail .s-name { padding-left: 0.14rem; }
.rail-arrow { color: var(--border); }
.rail-alias { font-weight: 600; }
.rail-tag {
  margin-left: 0.43rem;
  padding: 0 0.36rem;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: .4px;
  text-transform: uppercase;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 0.29rem;
}
.sensor-rail .s-val { color: var(--accent); }

/* An alias on a channel that has no divider maths — a rename, so it sits beside the channel rather
   than generating a second row that would repeat the same number. */
.rail-alias-inline {
  margin-left: 0.43rem;
  font-weight: 600;
  color: var(--accent);
}

/* Column captions for the value block, once per group. Sentence case at a readable size — the
   previous 10px uppercase tracking was decorative and genuinely hard to read. */
.sensor-head {
  font-size: 0.79rem;
  color: var(--muted);
  padding-bottom: 0;
  pointer-events: none;
}
.sensor-head .s-val, .sensor-head .s-range {
  font-family: var(--sans);
  font-size: 1rem;
  font-weight: 600;
}

/* Three columns and no more: name, current value, range. The name takes whatever is left, because
   it is the column you scan. */
.s-name {
  flex: 1 1 auto;
  min-width: 0;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.s-right { display: inline-flex; align-items: center; gap: 0.57rem; flex: none; }
/* Sized for the widest reading these columns carry — "11.980 V" — so voltages at three decimals
   do not shunt the range cell around as values change. */
.s-val {
  font: 0.93rem var(--mono);
  font-variant-numeric: tabular-nums;
  min-width: 4.9rem;
  text-align: right;
}
.s-val.is-warning { color: var(--warning); }
.s-val.is-critical { color: var(--critical); font-weight: 700; }

/* Min and max share one cell, recessive, so "now" stays the thing you read first. */
.s-range {
  font: 1rem var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  min-width: 7.15rem;
  text-align: right;
}

/* percentage meter */
.meter {
  width: 4.6rem; height: 0.43rem;
  background: var(--track);
  border-radius: 999px;
  overflow: hidden;
}
.meter i {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  transition: width .3s ease;
}
.meter i.is-high { background: var(--warning); }

/* ---- states ---- */
.empty, .empty-row {
  color: var(--muted);
  font-size: 0.93rem;
}
.empty {
  flex: 1;
  padding: 2.86rem 1.43rem;
  text-align: center;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}
.empty-row { padding: 0.57rem 1rem 0.86rem; }

/* ---- footer ---- */
.footer {
  display: flex;
  justify-content: space-between;
  gap: 0.86rem;
  padding: 1rem 1.43rem 1.71rem;
  color: var(--muted);
  font-size: 0.86rem;
}
.footer code { font-family: var(--mono); color: var(--text); }

/* ---- 1-minute chart ----
   Inline, directly under the sensor it belongs to, rather than a modal: the rest of the dashboard
   stays visible and live, which is the point of watching one value in context. It inherits the
   card's width, so it is responsive for free. */
.chart-inline {
  margin: 0.29rem 0 0.57rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.57rem;
  overflow: hidden;
  cursor: default;
}

.chart-head {
  display: flex;
  align-items: center;
  gap: 0.57rem;
  padding: 0.36rem 0.57rem;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}
.chart-window {
  font-size: 0.71rem;
  letter-spacing: .3px;
  text-transform: uppercase;
  color: var(--muted);
  flex: none;
}
.chart-stats {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: right;
  font: 0.79rem var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--text);
}
.chart-close {
  flex: none;
  padding: 0.07rem 0.43rem;
  font-size: 0.86rem;
  line-height: 1.3;
  color: var(--muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 0.43rem;
  cursor: pointer;
}
.chart-close:hover { color: var(--text); }

.chart-body { position: relative; padding: 0.57rem 0.57rem 0.14rem; }

/* The row's tooltip text, made permanent while the chart is open. pre-line keeps the line breaks
   without needing markup per line. */
.chart-details {
  margin: 0;
  padding: 0.5rem 0.71rem 0.64rem;
  font-size: 0.79rem;
  line-height: 1.5;
  white-space: pre-line;
  color: var(--muted);
  border-top: 1px solid var(--border);
}
.chart-details:empty { display: none; }
.chart-svg { display: block; width: 100%; height: 9.4rem; overflow: visible; }

/* Recessive axes and grid; the data line is the only assertive mark. */
.chart-grid { stroke: var(--border); stroke-width: 1; }
.chart-axis {
  fill: var(--muted);
  font: 10px var(--mono);
}
.chart-axis-y { text-anchor: end; }
.chart-axis-end { text-anchor: end; }
.chart-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
}
.chart-cross { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart-dot {
  fill: var(--accent);
  /* A surface-coloured ring keeps the marker legible where it sits on the line. */
  stroke: var(--surface);
  stroke-width: 2;
}
.chart-tip {
  position: absolute;
  transform: translate(-50%, -160%);
  padding: 0.21rem 0.57rem;
  font: 0.79rem var(--mono);
  white-space: nowrap;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 0.43rem;
  pointer-events: none;
}

/* ---- responsive ----
   The top bar is the part that breaks first: a fixed-width machine-id field beside three source
   buttons overflows well before phone width. Below 860px it becomes rows that wrap; below 560px the
   controls go full width so nothing is clipped or unreachable.

   Text size is not adjusted here — the fluid root font-size above already covers it. */
@media (max-width: 860px) {
  .topbar {
    flex-wrap: wrap;
    gap: 0.71rem;
    padding: 0.71rem 1rem;
  }
  .controls {
    flex-wrap: wrap;
    flex: 1 1 100%;
    gap: 0.43rem;
  }
  .controls input { flex: 1 1 13rem; width: auto; min-width: 0; }
  .sources { flex: 1 1 auto; }
  .grid { padding: 0.86rem; }
}

@media (max-width: 560px) {
  /* The column count itself is computed in app.js from the available width, so a phone gets one
     column without needing a rule here. */
  .grid { padding: 0.71rem; }
  .sources { display: grid; grid-template-columns: 1fr 1fr; gap: 0.43rem; }
  /* HTTP spans the row under the two WebSocket sources, which are the primary pair. */
  .controls .src-quiet { grid-column: 1 / -1; }
  .controls .src { justify-content: center; }
  .controls button, .controls .src { padding: 0.57rem 0.71rem; }
  #railsToggle .label { display: none; }
  .statusbar { flex-wrap: wrap; gap: 0.57rem 0.71rem; padding: 0.57rem 0.86rem; }
  .statusbar .spacer { flex-basis: 100%; height: 0; }
  .footer { flex-direction: column; gap: 0.29rem; padding: 0.86rem 0.86rem 1.43rem; }
  .chart-svg { height: 10.7rem; }
}
