/* =====================================================================
   InPhase MQTT Portal
   Colour roles come from a validated data-viz palette: the three series
   slots pass CVD separation, normal-vision separation and lightness-band
   checks in BOTH modes on the all-pairs list. Status colours are a fixed,
   separate set so a state can never impersonate a data series.
   Dark mode is a selected set of steps, not an automatic inversion.
   ===================================================================== */

:root {
  color-scheme: light;

  --plane:        #f9f9f7;
  --surface:      #fcfcfb;
  --surface-2:    #f2f1ed;
  --ink:          #0b0b0b;
  --ink-2:        #52514e;
  --ink-muted:    #898781;
  --grid:         #e1e0d9;
  --baseline:     #c3c2b7;
  --border:       rgba(11,11,11,0.10);
  --shadow:       0 1px 2px rgba(11,11,11,0.06), 0 4px 12px rgba(11,11,11,0.04);

  --series-1:     #2a78d6;
  --series-2:     #eb6834;
  --series-3:     #1baf7a;
  --band:         rgba(42,120,214,0.14);

  --good:         #0ca30c;
  --warning:      #fab219;
  --serious:      #ec835a;
  --critical:     #d03b3b;
  --good-text:    #006300;

  --r:            10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --plane: #0d0d0d;  --surface: #1a1a19;  --surface-2: #232322;
    --ink: #ffffff;    --ink-2: #c3c2b7;    --ink-muted: #898781;
    --grid: #2c2c2a;   --baseline: #383835; --border: rgba(255,255,255,0.10);
    --shadow: none;
    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70;
    --band: rgba(57,135,229,0.18);
    --good-text: #0ca30c;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --plane: #0d0d0d;  --surface: #1a1a19;  --surface-2: #232322;
  --ink: #ffffff;    --ink-2: #c3c2b7;    --ink-muted: #898781;
  --grid: #2c2c2a;   --baseline: #383835; --border: rgba(255,255,255,0.10);
  --shadow: none;
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70;
  --band: rgba(57,135,229,0.18);
  --good-text: #0ca30c;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--plane); color: var(--ink);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--series-1); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 2px; letter-spacing: -0.01em; }
h2 { font-size: 15px; margin: 0; letter-spacing: -0.005em; }
code { font-family: ui-monospace, "Cascadia Mono", Menlo, monospace; font-size: 0.92em; }

/* ---------------- top bar ---------------- */
.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 0 20px; height: 56px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; color: var(--ink); font-size: 16px; }
.brand:hover { text-decoration: none; }
.brand-mark {
  width: 14px; height: 14px; border-radius: 4px;
  background: linear-gradient(135deg, var(--series-1), var(--series-3));
}
.brand-sub { color: var(--ink-muted); font-weight: 500; }
.nav { display: flex; gap: 4px; }
.nav a {
  padding: 6px 12px; border-radius: 7px; color: var(--ink-2);
  font-weight: 500; font-size: 14px;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav a.on { background: var(--surface-2); color: var(--ink); font-weight: 600; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; font-size: 13px; }
.who { color: var(--ink-2); }
.role {
  display: inline-block; margin-left: 4px; padding: 1px 7px; border-radius: 999px;
  background: var(--surface-2); color: var(--ink-muted);
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; font-weight: 600;
}
.btn-icon {
  background: none; border: 1px solid var(--border); color: var(--ink-2);
  width: 30px; height: 30px; border-radius: 8px; cursor: pointer; font-size: 14px;
}
.btn-icon:hover { background: var(--surface-2); }
.btn-link { color: var(--ink-2); }

.wrap { max-width: 1360px; margin: 0 auto; padding: 24px 20px 64px; }

/* ---------------- page furniture ---------------- */
.page-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 16px; margin-bottom: 18px; flex-wrap: wrap;
}
.page-sub { margin: 2px 0 0; color: var(--ink-2); font-size: 13px; }
.page-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.crumb { font-size: 13px; color: var(--ink-muted); }
.hint { font-size: 12px; color: var(--ink-muted); }

.panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r); box-shadow: var(--shadow); margin-top: 20px; overflow: hidden;
}
.panel-head { padding: 14px 16px 10px; }
.panel-sub { margin: 3px 0 0; font-size: 12.5px; color: var(--ink-muted); max-width: 70ch; }

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
  border: 1px solid var(--border);
}
.badge-muted { color: var(--ink-2); background: var(--surface-2); }
.badge-good  { color: var(--good-text); border-color: color-mix(in oklab, var(--good) 40%, transparent); }
.badge-crit  { color: var(--critical); border-color: color-mix(in oklab, var(--critical) 45%, transparent); }

.switch { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-2); cursor: pointer; }

.btn {
  font: inherit; font-size: 14px; font-weight: 550;
  padding: 8px 14px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--ink);
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--series-1); border-color: var(--series-1); color: #fff; }
.btn-primary:hover { filter: brightness(1.06); }
.btn-ghost { background: transparent; }
.btn-block { width: 100%; margin-top: 6px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg-btn {
  font: inherit; font-size: 13px; padding: 6px 12px; cursor: pointer;
  background: var(--surface); color: var(--ink-2); border: 0;
  border-right: 1px solid var(--border);
}
.seg-btn:last-child { border-right: 0; }
.seg-btn.on { background: var(--surface-2); color: var(--ink); font-weight: 600; }

.alert {
  display: flex; gap: 9px; align-items: flex-start;
  padding: 11px 14px; border-radius: 9px; margin-bottom: 16px;
  font-size: 13.5px; border: 1px solid var(--border); background: var(--surface);
}
.alert-error { border-color: color-mix(in oklab, var(--critical) 45%, transparent); color: var(--critical); }
.alert-warn  { border-color: color-mix(in oklab, var(--warning) 55%, transparent); }
.alert-ok    { border-color: color-mix(in oklab, var(--good) 45%, transparent); color: var(--good-text); }

/* ---------------- device cards ---------------- */
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r); box-shadow: var(--shadow); padding: 14px 15px;
  display: flex; flex-direction: column; gap: 11px;
}
/* Telemetry is not retained by design, so a dead device would otherwise show
   its last reading forever. Fading it is the visual half of that guarantee. */
.card.stale { opacity: 0.5; }
.card-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.card-name { font-weight: 620; font-size: 14.5px; }
.card-key  { font-size: 11.5px; color: var(--ink-muted); font-family: ui-monospace, monospace; }
.card-metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 9px 6px; }
.metric-label { font-size: 10.5px; color: var(--ink-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.metric-value { font-size: 17px; font-weight: 600; letter-spacing: -0.015em; }
.metric-unit { font-size: 11px; color: var(--ink-muted); font-weight: 500; margin-left: 2px; }
.card-foot { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--ink-muted); }

/* Status: icon + label + colour. Never colour alone. */
.status { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 620; white-space: nowrap; }
.status .ico { font-size: 11px; line-height: 1; }
.st-good    { color: var(--good-text); }
.st-warn    { color: var(--ink-2); }
.st-warn .ico { color: var(--warning); }
.st-idle    { color: var(--ink-muted); }
.st-crit    { color: var(--critical); }
.st-off     { color: var(--ink-muted); }
.st-unknown { color: var(--ink-muted); }

/* ---------------- stat row ---------------- */
.stat-row { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); margin-bottom: 18px; }
.stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r); padding: 12px 14px;
}
.stat-label { font-size: 11px; color: var(--ink-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.stat-value { font-size: 26px; font-weight: 600; letter-spacing: -0.02em; margin-top: 3px; }
.stat-unit { font-size: 13px; color: var(--ink-muted); font-weight: 500; margin-left: 3px; }

/* ---------------- charts ---------------- */
.chart-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 6px 0 14px; flex-wrap: wrap; }
.chart-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); }
.chart-card {
  margin: 0; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--r); box-shadow: var(--shadow); padding: 14px 15px 10px;
}
.chart-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.chart-title { font-size: 13.5px; font-weight: 620; }
.chart-latest { font-size: 13px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.chart-body { position: relative; height: 190px; }
.chart-body svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart-foot { font-size: 11px; color: var(--ink-muted); margin-top: 6px; }
.chart-table { margin-top: 10px; max-height: 220px; overflow: auto; }

.ax-line   { stroke: var(--baseline); stroke-width: 1; }
.grid-line { stroke: var(--grid); stroke-width: 1; }
.ax-text   { fill: var(--ink-muted); font-size: 10.5px; font-variant-numeric: tabular-nums; }
.series-line { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.series-band { fill: var(--band); stroke: none; }
.crosshair { stroke: var(--ink-muted); stroke-width: 1; stroke-dasharray: 3 3; }
.focus-dot { fill: var(--series-1); stroke: var(--surface); stroke-width: 2; }
.direct-label { fill: var(--ink); font-size: 11px; font-weight: 620; font-variant-numeric: tabular-nums; }

.tip {
  position: absolute; pointer-events: none; z-index: 5;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 10px; font-size: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.14); white-space: nowrap;
}
.tip b { font-variant-numeric: tabular-nums; }
.tip-time { color: var(--ink-muted); font-size: 11px; }

/* ---------------- tables ---------------- */
.table-wrap { overflow: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data th, table.data td { padding: 8px 12px; text-align: left; border-bottom: 1px solid var(--border); }
table.data th { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-muted); font-weight: 600; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data tr.stale td { opacity: 0.5; }
.empty { text-align: center; color: var(--ink-muted); padding: 26px; font-size: 13px; }

.meter { display: inline-block; width: 60px; height: 6px; border-radius: 3px; background: var(--surface-2); vertical-align: middle; position: relative; overflow: hidden; }
.meter::after { content: ""; position: absolute; inset: 0 auto 0 0; width: var(--pct, 0%); background: var(--series-1); border-radius: 3px; }
.meter-val { font-size: 11px; color: var(--ink-muted); margin-left: 6px; }

/* ---------------- forms ---------------- */
.form-row { display: flex; gap: 14px; align-items: flex-end; flex-wrap: wrap; padding: 0 16px 16px; }
.form-row label { display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: var(--ink-2); }
input[type=text], input[type=password], input[type=date], select {
  font: inherit; font-size: 14px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--ink);
}
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--series-1); outline-offset: 2px;
}

/* ---------------- login ---------------- */
.login-body { display: grid; place-items: center; min-height: 100vh; background: var(--plane); }
.login-card {
  width: min(370px, 92vw); background: var(--surface);
  border: 1px solid var(--border); border-radius: 14px;
  box-shadow: var(--shadow); padding: 30px 28px;
  display: flex; flex-direction: column; gap: 6px;
}
.login-brand { display: flex; align-items: center; gap: 8px; font-size: 19px; font-weight: 650; }
.login-sub { margin: 0 0 14px; color: var(--ink-muted); font-size: 13px; }
.login-card label { font-size: 12px; color: var(--ink-2); margin-top: 8px; }
.login-foot { margin: 16px 0 0; font-size: 11.5px; color: var(--ink-muted); line-height: 1.5; }

/* ---------------- footer ---------------- */
.foot {
  position: fixed; bottom: 0; left: 0; right: 0; height: 30px;
  display: flex; align-items: center; gap: 16px; padding: 0 20px;
  background: var(--surface); border-top: 1px solid var(--border);
  font-size: 11.5px; color: var(--ink-muted);
}
.dot-idle::before, .dot-ok::before, .dot-bad::before {
  content: "●"; margin-right: 5px;
}
.dot-ok::before  { color: var(--good); }
.dot-bad::before { color: var(--critical); }
.dot-idle::before{ color: var(--ink-muted); }

@media (max-width: 720px) {
  .nav { display: none; }
  .chart-grid { grid-template-columns: 1fr; }
  .who { display: none; }
}

[hidden] { display: none !important; }
