/* SolarAI dashboard — light card design language.
 *
 * Motion policy: every transition/animation below is switched off by
 * html[data-motion="reduced"], which ui.js sets from prefers-reduced-motion
 * (or the Settings override). The plain @media query is kept as well so the
 * page still respects the preference before any JavaScript runs.
 */
:root {
  --bg: #F5F7FA;
  --surface: #FFFFFF;
  --ink: #16202A;
  --muted: #64748B;
  --amber: #E2A63B;
  --amber-soft: #FBF0DC;
  --teal: #0F766E;
  --teal-soft: #DFF3F0;
  --danger: #DC4C4C;
  --danger-soft: #FBE4E4;
  --border: #E6EAF0;
  --radius: 16px;
  --shadow-sm: 0 1px 2px rgba(16, 32, 42, .04);
  --shadow-md: 0 6px 24px rgba(16, 32, 42, .08);
  --font-display: 'Space Grotesk', system-ui, sans-serif;
  --font-body: 'IBM Plex Sans', system-ui, sans-serif;
  --font-mono: 'IBM Plex Mono', 'SFMono-Regular', Consolas, monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; }
button:focus-visible, .toggle:focus-visible, a:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

/* ── Top bar ─────────────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 24px;
  flex-wrap: wrap;
}

.brand {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  background: linear-gradient(135deg, var(--amber), var(--teal));
  display: inline-block;
}

/* ── Navigation ──────────────────────────────────────────────────────── */
.nav { display: flex; gap: 4px; }
.nav a {
  text-decoration: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  padding: 7px 14px;
  border-radius: 999px;
  transition: background .15s ease, color .15s ease;
}
.nav a:hover { background: var(--surface); color: var(--ink); }
.nav a.active { background: var(--surface); color: var(--ink); box-shadow: var(--shadow-sm); }

.nav-progress {
  position: fixed; top: 0; left: 0; height: 2px; width: 0;
  background: linear-gradient(90deg, var(--amber), var(--teal));
  z-index: 60; opacity: 0;
  transition: width .5s ease, opacity .2s ease;
}
.nav-progress.on { width: 80%; opacity: 1; }

/* ── Simulation badge — shown only when telemetry reports fw == "sim" ──── */
.sim-badge {
  display: inline-flex;
  align-items: center;
  padding: 6px 14px;
  border-radius: 999px;
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .12em;
  color: #fff;
  background: var(--danger);
  border: 1px solid var(--danger);
  cursor: help;
}
.sim-badge[hidden] { display: none; }
/* A hairline across the whole viewport, so the mode is obvious even in a
   screenshot that crops the top bar. */
body.simulating { box-shadow: inset 0 0 0 3px var(--danger); min-height: 100vh; }

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px 6px 14px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
  transition: background .25s ease, color .25s ease, border-color .25s ease;
}
.status-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.status-pill.up { color: var(--teal); border-color: var(--teal-soft); background: var(--teal-soft); }
.status-pill.up .dot { background: var(--teal); box-shadow: 0 0 0 3px var(--teal-soft); }
.status-pill.warn { color: #A26B12; border-color: var(--amber-soft); background: var(--amber-soft); }
.status-pill.warn .dot { background: var(--amber); }
.status-pill.down { color: var(--danger); border-color: var(--danger-soft); background: var(--danger-soft); }
.status-pill.down .dot { background: var(--danger); }

/* Which live transport is actually carrying the data. */
.rt-chip {
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 600;
  letter-spacing: .08em;
  padding: 2px 6px;
  border-radius: 6px;
  background: rgba(22, 32, 42, .07);
  color: inherit;
  opacity: .75;
}
.rt-chip.off { text-decoration: line-through; opacity: .5; }

.mode-switch {
  margin-left: auto;
  display: flex;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}
.mode-switch.inline { margin-left: 0; align-self: flex-start; }
.mode-switch button {
  border: none;
  background: transparent;
  padding: 7px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  transition: background .15s ease, color .15s ease;
}
.mode-switch button.active { background: var(--ink); color: #fff; }
.mode-switch button:disabled { opacity: .4; cursor: not-allowed; }
.mode-switch button.busy { opacity: .6; }

/* ── Banner ──────────────────────────────────────────────────────────── */
.banner {
  margin: 0 24px 16px;
  padding: 12px 18px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  animation: slide-down .28s ease both;
}
.banner.danger { background: var(--danger-soft); color: #9C2E2E; }
.banner.warn   { background: var(--amber-soft); color: #8A5A0E; }
@keyframes slide-down { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }

/* ── Layout ──────────────────────────────────────────────────────────── */
main { padding: 0 24px 40px; max-width: 960px; margin: 0 auto; }
main.swapping { animation: view-fade .22s ease both; }
@keyframes view-fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.hero {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 32px;
  display: flex;
  align-items: center;
  gap: 40px;
  flex-wrap: wrap;
  box-shadow: var(--shadow-sm);
}

/* ── Gauge ───────────────────────────────────────────────────────────── */
.gauge-wrap { position: relative; width: 200px; height: 200px; flex-shrink: 0; margin: 0 auto; }
.gauge { width: 100%; height: 100%; transform: rotate(-90deg); }
.gauge-track {
  fill: none; stroke: var(--bg); stroke-width: 14;
}
.gauge-fill {
  fill: none; stroke: var(--teal); stroke-width: 14; stroke-linecap: round;
  transition: stroke-dashoffset .9s cubic-bezier(.22, .61, .36, 1), stroke .4s ease;
}
.gauge-fill.charging { stroke: var(--amber); }
.gauge-fill.danger { stroke: var(--danger); }

.gauge-center {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.gauge-pct {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 44px;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.gauge-pct .unit { font-size: 20px; font-weight: 600; margin-left: 2px; color: var(--muted); }
.gauge-label { margin-top: 6px; font-size: 13px; color: var(--muted); font-weight: 500; }

/* ── Readouts ────────────────────────────────────────────────────────── */
.readouts {
  display: grid;
  grid-template-columns: repeat(2, minmax(120px, 1fr));
  gap: 20px;
  flex: 1;
  min-width: 240px;
}
.readout { display: flex; flex-direction: column; gap: 4px; }
.readout-label { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.readout-val { font-family: var(--font-mono); font-size: 26px; font-weight: 600; font-variant-numeric: tabular-nums; }
.readout-unit { font-size: 13px; color: var(--muted); margin-left: 2px; }
.mono { font-family: var(--font-mono); }

/* ── Relay cards ─────────────────────────────────────────────────────── */
.relays {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 14px;
  margin-top: 20px;
}
.relay-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: var(--shadow-sm);
  transition: border-color .25s ease, background .25s ease, box-shadow .25s ease, transform .25s ease;
}
.relay-card.on { border-color: var(--amber); background: var(--amber-soft); }
.relay-card.faulted { border-color: var(--danger); background: var(--danger-soft); opacity: .85; }
.relay-card.pending { opacity: .7; }
.relay-card.flash { animation: card-flash .6s ease; }
@keyframes card-flash {
  0% { transform: scale(1); box-shadow: var(--shadow-sm); }
  35% { transform: scale(1.025); box-shadow: var(--shadow-md); }
  100% { transform: scale(1); box-shadow: var(--shadow-sm); }
}

.relay-head { display: flex; flex-direction: column; gap: 3px; }
.relay-name { font-weight: 600; font-size: 14px; }
.relay-meta { font-family: var(--font-mono); font-size: 11px; color: var(--muted); letter-spacing: .02em; }
.relay-row { display: flex; align-items: center; gap: 10px; }
.relay-state { font-family: var(--font-mono); font-size: 12px; color: var(--muted); letter-spacing: .04em; }
.relay-card.on .relay-state { color: #8A5A0E; }
.relay-override {
  font-size: 11px; font-weight: 600; color: #8A5A0E;
  background: var(--amber-soft); border-radius: 6px; padding: 3px 8px; align-self: flex-start;
}
.relay-card.on .relay-override { background: rgba(255, 255, 255, .6); }
.relay-override[hidden] { display: none; }

/* Command status — what we ASKED for and how it went, kept visually distinct
   from .relay-state, which is the physical state reported by the rig.
   A dwell block is styled teal, not red: it is a protection layer working as
   designed, and dressing it as an error trains you to ignore real ones. */
.relay-cmd {
  font-size: 11px; font-weight: 600; line-height: 1.35;
  border-radius: 6px; padding: 4px 8px; align-self: flex-start;
  color: var(--muted); background: rgba(100, 116, 139, .10);
}
.relay-cmd[hidden] { display: none; }
.relay-cmd.is-pending { color: var(--muted); background: rgba(100, 116, 139, .10); }
.relay-cmd.is-blocked { color: var(--teal); background: var(--teal-soft); }
.relay-cmd.is-danger  { color: #9C2E2E; background: var(--danger-soft); }
.relay-cmd.is-unknown { color: #8A5A0E; background: var(--amber-soft); }

/* Firmware acked a change telemetry does not reflect. Deliberately loud, and
   deliberately NOT reverted to the old value — see relay_state.py. */
.relay-card.unconfirmed { border-color: var(--danger); border-style: dashed; }

.toggle:disabled { opacity: .45; cursor: not-allowed; }

.dim-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.dim-btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
  border-radius: 8px; padding: 5px 0; font-size: 11px; font-weight: 600; cursor: pointer;
  transition: border-color .15s ease, color .15s ease;
}
.dim-btn:hover { border-color: var(--teal); color: var(--teal); }
.dim-btn.busy, .toggle.busy, .ghost-btn.busy { opacity: .5; pointer-events: none; }

.toggle {
  width: 46px; height: 26px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  padding: 2px;
  cursor: pointer;
  display: flex;
  align-items: center;
  flex: none;
  transition: background .2s ease, border-color .2s ease;
}
.toggle .toggle-knob {
  width: 20px; height: 20px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
  transition: transform .22s cubic-bezier(.34, 1.4, .64, 1);
}
.toggle[aria-pressed="true"] { background: var(--amber); border-color: var(--amber); }
.toggle[aria-pressed="true"] .toggle-knob { transform: translateX(20px); }
.toggle:disabled { cursor: not-allowed; opacity: .5; }

/* ── Panels / activity feed ──────────────────────────────────────────── */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 22px 24px;
  margin-top: 20px;
  box-shadow: var(--shadow-sm);
}
.panel-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }

.activity { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.activity-item {
  display: flex; gap: 12px; align-items: baseline;
  padding: 9px 0; border-top: 1px solid var(--border);
  font-size: 13px;
}
.activity-item:first-child { border-top: none; }
.activity-item.fresh { animation: row-in .35s ease both; }
@keyframes row-in { from { opacity: 0; transform: translateY(-5px); } to { opacity: 1; transform: none; } }
.activity-time { color: var(--muted); font-size: 11px; flex: none; min-width: 62px; }
.activity-text { color: var(--ink); }
.activity-item.kind-fault .activity-text { color: #9C2E2E; }
.activity-item.kind-fault.sev-ok .activity-text { color: var(--teal); }
.activity-item.kind-model .activity-text { color: var(--teal); }
.activity-empty { color: var(--muted); font-size: 13px; padding: 6px 0; }

/* ── Toasts ──────────────────────────────────────────────────────────── */
.toasts {
  position: fixed; right: 20px; bottom: 20px; z-index: 70;
  display: flex; flex-direction: column; gap: 10px;
  width: min(360px, calc(100vw - 40px));
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  display: flex; align-items: flex-start; gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--muted);
  border-radius: 12px;
  padding: 12px 14px;
  box-shadow: var(--shadow-md);
  opacity: 0; transform: translateY(10px);
  transition: opacity .24s ease, transform .24s cubic-bezier(.22,.61,.36,1);
}
.toast.in { opacity: 1; transform: none; }
.toast.out { opacity: 0; transform: translateY(6px); }
.toast-info   { border-left-color: var(--teal); }
.toast-ok     { border-left-color: var(--teal); background: var(--teal-soft); }
.toast-warn   { border-left-color: var(--amber); background: var(--amber-soft); }
.toast-danger { border-left-color: var(--danger); background: var(--danger-soft); }
.toast-body { flex: 1; min-width: 0; }
.toast-title { font-weight: 600; font-size: 13px; }
.toast-text { font-size: 12.5px; color: var(--muted); margin-top: 2px; overflow-wrap: anywhere; }
.toast-danger .toast-text, .toast-warn .toast-text { color: inherit; opacity: .85; }
.toast-close {
  border: none; background: transparent; color: var(--muted);
  font-size: 18px; line-height: 1; cursor: pointer; padding: 0 2px;
}

/* ── Footer ──────────────────────────────────────────────────────────── */
.foot {
  margin-top: 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: border-color .15s ease, color .15s ease, background .15s ease;
}
.ghost-btn:hover { border-color: var(--teal); color: var(--teal); background: var(--surface); }
.fw-tag { font-family: var(--font-mono); font-size: 12px; color: var(--muted); }

/* ── Responsive ──────────────────────────────────────────────────────── */
@media (max-width: 640px) {
  .topbar { padding: 14px 16px; gap: 10px; }
  main { padding: 0 16px 32px; }
  .hero { padding: 22px; gap: 24px; flex-direction: column; }
  .readouts { grid-template-columns: repeat(2, 1fr); width: 100%; }
  .mode-switch { margin-left: 0; order: 4; width: 100%; justify-content: center; }
  .nav { order: 3; width: 100%; }
  .nav a { flex: 1; text-align: center; }
  .toasts { right: 12px; left: 12px; bottom: 12px; width: auto; }
  .page-section { padding: 22px 18px; }
}

/* ── Motion policy ───────────────────────────────────────────────────── */
/* Pre-JavaScript: honour the OS setting on its own. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}
/* Post-JavaScript: ui.js sets data-motion, so the Settings override wins in
   both directions — "reduced" kills motion even when the OS allows it, and
   "full" restores it even when the OS asks to reduce. */
html[data-motion="reduced"] *,
html[data-motion="reduced"] *::before,
html[data-motion="reduced"] *::after {
  animation-duration: .001ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: .001ms !important;
  scroll-behavior: auto !important;
}
html[data-motion="reduced"]::view-transition-group(*),
html[data-motion="reduced"]::view-transition-old(*),
html[data-motion="reduced"]::view-transition-new(*) { animation: none !important; }

/* ── View transitions (Chromium; ignored elsewhere) ──────────────────── */
::view-transition-old(root) { animation: vt-out .18s ease both; }
::view-transition-new(root) { animation: vt-in .26s ease both; }
@keyframes vt-out { to { opacity: 0; transform: translateY(-4px); } }
@keyframes vt-in { from { opacity: 0; transform: translateY(6px); } }

/* ── Boot overlay — real status only, no fabricated stages ─────────────── */
.boot-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  transition: opacity .4s ease;
}
.boot-overlay.hidden { opacity: 0; pointer-events: none; }
.boot-mark {
  width: 28px; height: 28px; border-radius: 8px;
  background: linear-gradient(135deg, var(--amber), var(--teal));
  animation: boot-pulse 1.4s ease-in-out infinite;
}
@keyframes boot-pulse { 0%,100% { transform: scale(1); opacity: 1; } 50% { transform: scale(1.15); opacity: .7; } }
.boot-title { font-family: var(--font-display); font-weight: 700; font-size: 22px; }
.boot-status { font-size: 13px; color: var(--muted); }

.nav-link { text-decoration: none; }

/* ── Reusable page sections (training / settings) ───────────────────────── */
.page-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 28px 32px;
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: var(--shadow-sm);
}
.page-section:first-child { margin-top: 0; }
.section-title { font-family: var(--font-display); font-size: 18px; font-weight: 700; margin: 0; }
.hint { font-size: 13px; color: var(--muted); margin: 0; }
.step-list { font-size: 14px; line-height: 1.7; padding-left: 20px; margin: 0; color: var(--ink); }
.form-row { display: flex; flex-direction: column; gap: 10px; max-width: 420px; }
.form-row input[type="text"], .form-row input[type="file"], .form-row input[type="password"] {
  font-family: var(--font-body);
  font-size: 14px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--ink);
}
.version-log { font-family: var(--font-mono); font-size: 12px; color: var(--muted); line-height: 1.8; }

/* ── Auth: sign-out button, login page, password form ────────────────────
   Same tokens as everything above — this is the dashboard's own design
   language, not a bolted-on login screen. */

.logout-form { display: flex; margin-left: 8px; }
.logout-form .ghost-btn { padding: 7px 14px; font-size: 13px; }
.logout-form .ghost-btn:hover { border-color: var(--danger); color: var(--danger); }

/* The primary action button — the login page's only filled control. */
.primary-btn {
  border: 1px solid var(--teal);
  background: var(--teal);
  color: #fff;
  padding: 12px 20px;
  border-radius: 999px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: filter .15s ease, opacity .15s ease;
}
.primary-btn:hover { filter: brightness(1.08); }
.primary-btn:disabled, .primary-btn.busy { opacity: .55; cursor: not-allowed; }

/* Labelled fields, shared by the login page and the Settings password form. */
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label {
  font-size: 12px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); font-weight: 600;
}
.field-input {
  font-family: var(--font-body);
  font-size: 15px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--ink);
  width: 100%;
  transition: border-color .15s ease, background .15s ease;
}
.field-input:focus { outline: none; border-color: var(--teal); background: var(--surface); }
.field-input:disabled { opacity: .55; cursor: not-allowed; }
.field-hint { font-size: 12px; color: var(--muted); margin: 0; }

/* ── Login page ──────────────────────────────────────────────────────── */
.login-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  /* A whisper of the brand gradient, so the login page is recognisably the
     same product as the dashboard without shouting about it. */
  background:
    radial-gradient(1100px 520px at 50% -12%, var(--amber-soft), transparent 62%),
    var(--bg);
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 34px 32px;
  box-shadow: var(--shadow-md);
  display: flex;
  flex-direction: column;
  gap: 16px;
  animation: login-in .35s ease both;
}
@keyframes login-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.login-brand {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 18px;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
}
.login-title { font-family: var(--font-display); font-size: 24px; font-weight: 700; margin: 0; }

.login-form { display: flex; flex-direction: column; gap: 16px; margin-top: 4px; }
.login-form .primary-btn { margin-top: 2px; }
.login-countdown { font-variant-numeric: tabular-nums; }

.login-error {
  background: var(--danger-soft);
  color: #9C2E2E;
  border-radius: 12px;
  padding: 11px 14px;
  font-size: 13px;
  font-weight: 500;
}

.login-foot {
  font-size: 12px;
  color: var(--muted);
  margin: 0;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  line-height: 1.6;
}
.login-foot .mono, .hint .mono { font-family: var(--font-mono); font-size: .95em; }

@media (max-width: 640px) {
  .login-card { padding: 26px 22px; border-radius: 20px; }
  .logout-form { order: 2; margin-left: auto; }
}

/* ── Change-password form (Settings) ─────────────────────────────────── */
.auth-form { gap: 14px; }
.auth-form .btn-row { margin-top: 2px; }
.auth-msg.bad { color: #9C2E2E; }
.auth-msg.good { color: var(--teal); }

.table-wrap { overflow-x: auto; }
.dtable { width: 100%; border-collapse: collapse; font-size: 13px; }
.dtable th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .06em;
  color: var(--muted); font-weight: 600; padding: 6px 10px 6px 0;
}
.dtable td { padding: 8px 10px 8px 0; border-top: 1px solid var(--border); white-space: nowrap; }
