:root {
  color-scheme: light;
  --ink: #14110f;
  --ink-soft: #4a4340;
  --muted: #8a8078;
  --bg: #f7f5f2;
  --card-bg: #ffffff;
  --border: #ece7e1;
  --orange: #f2661f;
  --orange-dark: #d6540f;
  --orange-tint: #fdece0;
  --ok: #1f9d55;
  --ok-tint: #e6f7ec;
  --err: #d6362a;
  --err-tint: #fdeceb;
  --pending-tint: #f1efec;
  --pending-text: #8a8078;
  --shadow: 0 1px 2px rgba(20, 17, 15, 0.04), 0 8px 24px -12px rgba(20, 17, 15, 0.12);
}

* { box-sizing: border-box; }

/*
 * The browser's built-in rule for [hidden] is only `display: none`, which any
 * class that sets `display` (our pills use inline-flex, the chart tooltip uses
 * flex) silently beats. That is why the "No load connected" badge showed even
 * when a load was drawing 4A. Make hiding win outright.
 */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85em;
}

/* ---------- Login page ---------- */

.login-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 36px 32px 32px;
  text-align: center;
}

.login-logo { height: 40px; margin-bottom: 6px; }

.login-subtitle {
  margin: 0 0 28px;
  color: var(--muted);
  font-size: 0.85rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  text-align: left;
}

.login-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--ink-soft);
}

.login-form input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.95rem;
  background: var(--bg);
  color: var(--ink);
}

.login-form input:focus {
  outline: none;
  border-color: var(--orange);
  background: var(--card-bg);
}

.login-form button {
  margin-top: 6px;
  padding: 11px;
  border: none;
  border-radius: 8px;
  background: var(--orange);
  color: #fff;
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  transition: background 0.15s ease;
}

.login-form button:hover { background: var(--orange-dark); }

.login-error {
  background: var(--err-tint);
  color: var(--err);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 0.85rem;
  margin-bottom: 18px;
}

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 28px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar-left { display: flex; align-items: center; gap: 14px; }

.brand-logo { height: 26px; }

.topbar-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--ink-soft);
  border-left: 1px solid var(--border);
  padding-left: 14px;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 0.85rem;
}

.last-updated { color: var(--muted); }

.topbar-link, .logout-link {
  color: var(--ink-soft);
  text-decoration: none;
  font-weight: 600;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 14px;
  white-space: nowrap;
}

.topbar-link:hover, .logout-link:hover { border-color: var(--orange); color: var(--orange-dark); }

/* ---------- Banner ---------- */

.banner {
  margin: 16px 28px 0;
  padding: 11px 14px;
  border-radius: 10px;
  background: var(--err-tint);
  color: var(--err);
  font-size: 0.85rem;
  line-height: 1.45;
  border: 1px solid rgba(214, 54, 42, 0.18);
  max-width: 1400px;
}

/* ---------- Pills / badges ---------- */

.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 11px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  white-space: nowrap;
}

.pill-unknown { background: var(--pending-tint); color: var(--pending-text); }
.pill-ok { background: var(--ok-tint); color: var(--ok); }
.pill-error { background: var(--err-tint); color: var(--err); }
.pill-load { background: var(--orange-tint); color: var(--orange-dark); }

/* ---------- Layout ---------- */

main#device-grid {
  padding: 24px 28px 48px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(440px, 1fr));
  gap: 20px;
  max-width: 1400px;
  margin: 0 auto;
}

.device-panel {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 20px 22px 22px;
  /* Keeps a panel from being stretched by anything inside it. */
  min-width: 0;
}

.panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}

.panel-head h2 { margin: 0 0 3px; font-size: 1.1rem; }

.device-id-line { color: var(--muted); font-size: 0.75rem; }

.panel-head-badges { display: flex; gap: 8px; flex-shrink: 0; flex-wrap: wrap; justify-content: flex-end; }

/* ---------- Stat cards ---------- */

.stat-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 18px;
}

.stat-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.stat-label {
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 700;
}

.stat-value {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

/* ---------- Chart card ---------- */

.chart-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 16px;
}

.chart-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  flex-wrap: wrap;
  gap: 8px;
}

.metric-toggle {
  display: inline-flex;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}

.metric-btn {
  border: none;
  background: transparent;
  padding: 5px 13px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--muted);
  cursor: pointer;
}

.metric-btn.is-active { background: var(--orange); color: #fff; }

.chart-window-note { font-size: 0.72rem; color: var(--muted); }

/*
 * THE fix for the runaway chart.
 *
 * The height is declared once, here, on the wrapper. The chart measures this
 * box and draws inside it; nothing the chart renders can change the box's
 * height, so there is no size feedback loop. `overflow: hidden` is a second
 * belt-and-braces stop, and `contain` tells the browser layout inside this
 * box can never affect anything outside it.
 */
.chart-plot {
  position: relative;
  width: 100%;
  height: 220px;
  overflow: hidden;
  contain: strict;
}

.chart-svg {
  display: block;
  width: 100%;
  height: 100%;
}

.chart-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-area { opacity: 0.12; stroke: none; }
.chart-dot { fill: #fff; stroke-width: 2; }
.chart-grid { stroke: #e4ded7; stroke-width: 1; shape-rendering: crispEdges; }
.chart-cursor { stroke: #b8afa6; stroke-width: 1; stroke-dasharray: 3 3; }
.chart-axis { fill: var(--muted); font-size: 10px; font-family: inherit; }
.chart-empty { fill: var(--muted); font-size: 12px; font-family: inherit; }

.chart-tip {
  position: absolute;
  transform: translate(-50%, -100%);
  background: var(--ink);
  color: #fff;
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 0.75rem;
  line-height: 1.35;
  pointer-events: none;
  white-space: nowrap;
  display: flex;
  flex-direction: column;
  z-index: 2;
}

.chart-tip span { color: #cfc7bf; font-size: 0.68rem; }

/* ---------- Detail grid ---------- */

.detail-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}

.detail-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 11px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  /* Labels that wrap to two lines shouldn't push their value out of line
     with the neighbouring cards. */
  justify-content: space-between;
}

.detail-label {
  font-size: 0.66rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 700;
}

.detail-value {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

.detail-value.is-fault { color: var(--err); }

/* ---------- Sensor cards ---------- */

.sensor-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 10px;
}

.sensor-card {
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 14px;
  background: var(--pending-tint);
  min-width: 0;
}

.sensor-card-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--ink-soft);
  margin-bottom: 6px;
}

.sensor-icon { font-size: 1rem; filter: grayscale(1); opacity: 0.6; }

.sensor-value {
  display: block;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--ink);
  margin-bottom: 6px;
}

.sensor-value.is-pending {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}

.sensor-note {
  margin: 0;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.45;
}

/* ---------- Errors / debug ---------- */

.device-error {
  margin: 0 0 16px;
  color: var(--err);
  font-size: 0.8rem;
  background: var(--err-tint);
  border-radius: 8px;
  padding: 8px 10px;
  line-height: 1.45;
}

#raw-panel {
  margin: 0 auto 32px;
  padding: 0 28px;
  font-size: 0.8rem;
  color: var(--muted);
  max-width: 1400px;
}

#raw-panel summary { cursor: pointer; font-weight: 600; }

.raw-note { margin: 8px 0; line-height: 1.5; }

#raw-json {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  max-height: 420px;      /* never let the debug dump stretch the page */
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.stat-row-3 { grid-template-columns: repeat(3, 1fr); }

/* A panel whose data has gone stale: the readings are still shown (they are the
   last thing we know) but they must not look live. */
.device-panel.is-stale .stat-value,
.device-panel.is-stale .detail-value,
.device-panel.is-stale .chart-card { opacity: 0.45; }

.range-select {
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--ink-soft);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
}

/* ---------- PayGo page ---------- */

.paygo-page {
  max-width: 900px;
  margin: 0 auto;
  padding: 24px 28px 48px;
}

.paygo-intro {
  color: var(--ink-soft);
  font-size: 0.9rem;
  line-height: 1.55;
  margin: 0 0 20px;
}

.paygo-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 20px 22px 22px;
  margin-bottom: 18px;
}

.paygo-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.paygo-head h2 { margin: 0 0 3px; font-size: 1.05rem; }

.paygo-form {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 4px;
}

.paygo-form label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.paygo-form label.grow { flex: 1 1 220px; }

.paygo-form input {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  font-size: 0.9rem;
  background: var(--bg);
  color: var(--ink);
  text-transform: none;
  letter-spacing: 0;
  font-weight: 500;
}

.paygo-form input:focus { outline: none; border-color: var(--orange); background: var(--card-bg); }

.paygo-form button, .paygo-actions button {
  border: none;
  border-radius: 8px;
  background: var(--orange);
  color: #fff;
  font-weight: 700;
  font-size: 0.85rem;
  padding: 10px 16px;
  cursor: pointer;
}

.paygo-form button:hover, .paygo-actions button:hover { background: var(--orange-dark); }
.paygo-form button:disabled { background: var(--muted); cursor: default; }

.paygo-advanced { margin-top: 16px; font-size: 0.8rem; color: var(--muted); }
.paygo-advanced summary { cursor: pointer; font-weight: 600; }
.paygo-advanced-body p { margin: 10px 0; line-height: 1.5; }
.paygo-advanced-body strong { color: var(--ink-soft); }

.paygo-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.paygo-actions button { background: var(--ink-soft); }
.paygo-actions button:hover { background: var(--ink); }
.paygo-actions button.danger { background: var(--err); }

.token-result {
  margin-top: 16px;
  padding: 16px;
  border-radius: 12px;
  background: var(--orange-tint);
  border: 1px solid rgba(242, 102, 31, 0.25);
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: flex-start;
}

.token-result.is-error { background: var(--err-tint); border-color: rgba(214,54,42,.25); color: var(--err); }

.token-label {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--orange-dark);
}

.token-code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 1.9rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--ink);
  user-select: all;
}

.token-note { font-size: 0.78rem; color: var(--ink-soft); line-height: 1.5; }

.copy-btn {
  border: 1px solid rgba(242, 102, 31, 0.4);
  background: transparent;
  color: var(--orange-dark);
  border-radius: 999px;
  padding: 5px 14px;
  font-size: 0.75rem;
  font-weight: 700;
  cursor: pointer;
}

@media (max-width: 640px) {
  .paygo-page { padding: 16px; }
  .token-code { font-size: 1.5rem; }
  .stat-row-3 { grid-template-columns: 1fr; }
  .stat-row { grid-template-columns: repeat(2, 1fr); }
  .detail-grid { grid-template-columns: repeat(2, 1fr); }
  .sensor-row { grid-template-columns: 1fr; }
  main#device-grid { padding: 16px; grid-template-columns: 1fr; }
  .topbar { padding: 12px 16px; flex-wrap: wrap; gap: 8px; }
  .banner { margin: 12px 16px 0; }
  #raw-panel { padding: 0 16px; }
  .chart-plot { height: 180px; }
}
