/* ==========================================================================
   Wind + Water dashboard
   Design system ported directly from bgt-rd103-dashboard/static/css/dashboard.css
   -- same tokens, same components. Additions: compass widget, info-variant
   banner (below). Colour roles are declared once here and referenced by role
   everywhere else, so light/dark swap in a single place.
   ========================================================================== */

:root {
  color-scheme: light;

  --page:            #f9f9f7;
  --surface-1:       #fcfcfb;
  --surface-2:       #f2f1ed;

  --text-primary:    #0b0b0b;
  --text-secondary:  #52514e;
  --text-muted:      #898781;

  --grid:            #e1e0d9;
  --axis:            #c3c2b7;
  --border:          rgba(11, 11, 11, 0.10);
  --border-strong:   rgba(11, 11, 11, 0.16);
  --wash:            rgba(11, 11, 11, 0.04);

  --series-level:    #2a78d6;
  --series-velocity: #eb6834;
  --series-flow:     #1baf7a;
  --series-wind:     #8b5cf6;

  --status-good:     #0ca30c;
  --status-warning:  #fab219;
  --status-serious:  #ec835a;
  --status-critical: #d03b3b;
  --status-unknown:  #898781;
  --status-info:     #2a78d6;

  --delta-up-good:   #006300;
  --delta-down-bad:  #d03b3b;

  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04),
            0 4px 16px rgba(11, 11, 11, 0.04);

  --radius: 10px;
  --gap: 16px;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;

    --page:            #0d0d0d;
    --surface-1:       #1a1a19;
    --surface-2:       #232322;

    --text-primary:    #ffffff;
    --text-secondary:  #c3c2b7;
    --text-muted:      #898781;

    --grid:            #2c2c2a;
    --axis:            #383835;
    --border:          rgba(255, 255, 255, 0.10);
    --border-strong:   rgba(255, 255, 255, 0.18);
    --wash:            rgba(255, 255, 255, 0.05);

    --series-level:    #3987e5;
    --series-velocity: #d95926;
    --series-flow:     #199e70;
    --series-wind:     #a78bfa;

    --delta-up-good:   #0ca30c;

    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;

  --page:            #0d0d0d;
  --surface-1:       #1a1a19;
  --surface-2:       #232322;

  --text-primary:    #ffffff;
  --text-secondary:  #c3c2b7;
  --text-muted:      #898781;

  --grid:            #2c2c2a;
  --axis:            #383835;
  --border:          rgba(255, 255, 255, 0.10);
  --border-strong:   rgba(255, 255, 255, 0.18);
  --wash:            rgba(255, 255, 255, 0.05);

  --series-level:    #3987e5;
  --series-velocity: #d95926;
  --series-flow:     #199e70;
  --series-wind:     #a78bfa;

  --delta-up-good:   #0ca30c;

  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
}

/* -------------------------------------------------------------------- base */

*, *::before, *::after { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  background: var(--page);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 600; }

button { font: inherit; color: inherit; cursor: pointer; }

.wrap {
  max-width: 1440px;
  margin: 0 auto;
  padding: 0 24px 48px;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ------------------------------------------------------------------ header */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  max-width: 1440px;
  margin: 0 auto;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  gap: var(--gap);
  flex-wrap: wrap;
}

.brand { display: flex; flex-direction: column; gap: 1px; margin-right: auto; }
.brand h1 { font-size: 15px; letter-spacing: -0.01em; }
.brand .site { font-size: 12px; color: var(--text-muted); }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 11px 5px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
}

.pill .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--status-unknown);
  flex: none;
}

.pill[data-state="good"]     .dot { background: var(--status-good); }
.pill[data-state="warning"]  .dot { background: var(--status-warning); }
.pill[data-state="serious"]  .dot { background: var(--status-serious); }
.pill[data-state="critical"] .dot { background: var(--status-critical); }

.pill[data-state="good"] .dot { animation: breathe 2.4s ease-in-out infinite; }

@keyframes breathe { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
@media (prefers-reduced-motion: reduce) {
  .pill .dot { animation: none !important; }
}

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

.controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 18px 0 20px;
}

.segmented {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
  gap: 2px;
}

.segmented button {
  border: 0;
  background: transparent;
  padding: 5px 13px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  transition: background 0.12s, color 0.12s;
}

.segmented button:hover { background: var(--wash); color: var(--text-primary); }

.segmented button[aria-pressed="true"] {
  background: var(--surface-1);
  color: var(--text-primary);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-secondary);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 500;
  text-decoration: none;
  transition: border-color 0.12s, color 0.12s;
}

.btn:hover { border-color: var(--border-strong); color: var(--text-primary); }
.btn[aria-pressed="true"] {
  border-color: var(--border-strong);
  color: var(--text-primary);
  background: var(--surface-2);
}

.spacer { margin-left: auto; }

:focus-visible {
  outline: 2px solid var(--series-level);
  outline-offset: 2px;
  border-radius: 4px;
}

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

.grid {
  display: grid;
  gap: var(--gap);
  grid-template-columns: repeat(12, 1fr);
}

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px 16px;
  min-width: 0;
}

.card-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 4px;
  flex-wrap: wrap;
}

.card-head h2 {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.card-head .unit {
  font-size: 12px;
  color: var(--text-muted);
}

.card-sub {
  font-size: 12px;
  color: var(--text-muted);
  margin: 0 0 12px;
}

.card-sub code {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 4px;
}

.span-3  { grid-column: span 3; }
.span-4  { grid-column: span 4; }
.span-5  { grid-column: span 5; }
.span-6  { grid-column: span 6; }
.span-7  { grid-column: span 7; }
.span-12 { grid-column: span 12; }

/* ------------------------------------------------------------- hero figure */

.hero { display: flex; flex-direction: column; height: 100%; }

.hero-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}

.hero-value {
  font-size: 60px;
  font-weight: 600;
  line-height: 1.05;
  letter-spacing: -0.03em;
  margin: 10px 0 2px;
}

.hero-value .suffix {
  font-size: 20px;
  font-weight: 500;
  color: var(--text-muted);
  letter-spacing: 0;
  margin-left: 4px;
}

.hero-meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 16px;
}

.meter { margin-top: auto; }

.meter-track {
  position: relative;
  height: 10px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}

.meter-fill {
  height: 100%;
  border-radius: 999px;
  background: var(--status-good);
  transition: width 0.4s ease, background 0.3s ease;
}

.meter-fill[data-state="warning"]  { background: var(--status-warning); }
.meter-fill[data-state="serious"]  { background: var(--status-serious); }
.meter-fill[data-state="critical"] { background: var(--status-critical); }
.meter-fill[data-state="unknown"]  { background: var(--status-unknown); }

.meter-ticks {
  position: relative;
  height: 16px;
  margin-top: 5px;
  font-size: 11px;
  color: var(--text-muted);
}

.meter-ticks span {
  position: absolute;
  transform: translateX(-50%);
  white-space: nowrap;
}

.meter-legend {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  margin-top: 12px;
  font-size: 11px;
  color: var(--text-secondary);
}

.meter-legend i {
  display: inline-block;
  width: 8px; height: 8px;
  border-radius: 2px;
  margin-right: 5px;
  vertical-align: 0;
}

/* -------------------------------------------------------------- stat tiles */

.tiles {
  display: grid;
  gap: var(--gap);
  grid-template-columns: repeat(2, minmax(0, 1fr));
  height: 100%;
}

.tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px 12px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.tile-label {
  font-size: 12px;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 6px;
}

.tile-label .key {
  width: 8px; height: 8px; border-radius: 2px; flex: none;
}

.tile-value {
  font-size: 27px;
  font-weight: 600;
  letter-spacing: -0.02em;
  margin: 6px 0 1px;
  line-height: 1.15;
}

.tile-value .suffix {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  letter-spacing: 0;
  margin-left: 3px;
}

.tile-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: auto;
  padding-top: 8px;
  font-size: 11px;
  color: var(--text-muted);
}

.delta { font-weight: 600; white-space: nowrap; }
.delta[data-dir="up-good"]   { color: var(--delta-up-good); }
.delta[data-dir="down-bad"]  { color: var(--delta-down-bad); }
.delta[data-dir="flat"]      { color: var(--text-muted); }

.spark { display: block; width: 72px; height: 22px; flex: none; }

/* ---------------------------------------------------------------- compass */

.compass-host {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 6px 0 4px;
}

.compass-host svg { width: 130px; height: 130px; }

.compass-ring { fill: var(--surface-2); stroke: var(--border-strong); stroke-width: 1.5; }
.compass-tick { fill: var(--text-muted); font-size: 9px; font-weight: 600; }
.compass-needle {
  stroke: var(--series-wind);
  stroke-width: 3;
  stroke-linecap: round;
  transform-origin: 60px 60px;
  transition: transform 0.4s ease;
}
.compass-hub { fill: var(--series-wind); }

/* ------------------------------------------------------------ chart canvas */

.chart-host {
  position: relative;
  width: 100%;
  height: 220px;
}

.chart-host.tall { height: 300px; }

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

.grid-line   { stroke: var(--grid); stroke-width: 1; }
.axis-line   { stroke: var(--axis); stroke-width: 1; }
.tick-label  { fill: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.axis-title  { fill: var(--text-muted); font-size: 11px; }

.series-line {
  fill: none;
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
}

.series-area { stroke: none; opacity: 0.10; }

.end-dot { stroke: var(--surface-1); stroke-width: 2; }

.bar-mark { transition: opacity 0.12s; }

.threshold-line {
  stroke-width: 1;
  stroke-dasharray: 3 4;
  fill: none;
}

.threshold-label {
  font-size: 10px;
  font-weight: 600;
  fill: var(--text-secondary);
}

.crosshair {
  stroke: var(--axis);
  stroke-width: 1;
  pointer-events: none;
}

.hover-dot { stroke: var(--surface-1); stroke-width: 2; pointer-events: none; }

.empty-note[hidden] { display: none; }

.empty-note {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 13px;
  color: var(--text-muted);
  text-align: center;
  padding: 20px;
}

.refetching .chart-host svg,
.refetching .tile-value,
.refetching .hero-value { opacity: 0.45; transition: opacity 0.2s; }

/* --------------------------------------------------------------- tooltip */

.tooltip {
  position: absolute;
  z-index: 10;
  min-width: 130px;
  padding: 9px 11px;
  background: var(--surface-1);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.16);
  font-size: 12px;
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, -100%);
  transition: opacity 0.1s;
}

.tooltip.visible { opacity: 1; }

.tooltip .tip-time {
  color: var(--text-muted);
  font-size: 11px;
  margin-bottom: 5px;
}

.tooltip .tip-row {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text-secondary);
}

.tooltip .tip-row .key {
  width: 8px; height: 8px; border-radius: 2px; flex: none;
}

.tooltip .tip-row .val {
  margin-left: auto;
  font-weight: 600;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

.tooltip .tip-note {
  margin-top: 5px;
  color: var(--text-muted);
  font-size: 11px;
}

/* ----------------------------------------------------------------- legend */

.legend {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-secondary);
}

.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 14px; height: 2px; border-radius: 1px; }

/* ------------------------------------------------------------ table view */

.table-wrap { overflow-x: auto; }

table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

table.data th,
table.data td {
  padding: 7px 12px 7px 0;
  text-align: right;
  border-bottom: 1px solid var(--grid);
  white-space: nowrap;
}

table.data th {
  text-align: right;
  color: var(--text-muted);
  font-weight: 500;
  position: sticky;
  top: 0;
  background: var(--surface-1);
}

table.data th:first-child,
table.data td:first-child { text-align: left; }

.table-scroll { max-height: 420px; overflow-y: auto; }

/* ------------------------------------------------------------------- misc */

.banner {
  display: none;
  align-items: flex-start;
  gap: 10px;
  padding: 11px 14px;
  border-radius: 8px;
  border: 1px solid var(--status-critical);
  background: var(--surface-1);
  font-size: 13px;
  margin-bottom: var(--gap);
  color: var(--text-primary);
}

.banner.show { display: flex; }
.banner .icon { flex: none; font-weight: 700; color: var(--status-critical); }

/* Informational variant -- not an alarm, always visible while the fault
   stands, styled distinctly from the (hidden-until-triggered) alarm banner. */
.banner.sensor-note {
  display: flex;
  border-color: var(--status-info);
}
.banner.sensor-note .icon {
  color: var(--status-info);
  font-style: italic;
  font-weight: 700;
}

footer.foot {
  margin-top: 28px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-muted);
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

/* ------------------------------------------------------------------ login */

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 340px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px;
}

.login-card h1 { font-size: 16px; margin-bottom: 4px; }
.login-card p  { color: var(--text-muted); font-size: 12px; margin: 0 0 18px; }

.login-card input {
  width: 100%;
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--page);
  color: var(--text-primary);
  font: inherit;
  margin-bottom: 12px;
}

.login-card button {
  width: 100%;
  padding: 9px;
  border-radius: 8px;
  border: 0;
  background: var(--series-level);
  color: #fff;
  font-weight: 600;
}

.login-error { color: var(--status-critical); font-size: 12px; margin-bottom: 10px; }

/* ------------------------------------------------------------ responsive */

@media (max-width: 1080px) {
  .span-3, .span-4, .span-5, .span-6, .span-7 { grid-column: span 12; }
  .tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

@media (max-width: 720px) {
  .wrap, .topbar-inner { padding-left: 14px; padding-right: 14px; }
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .hero-value { font-size: 48px; }
  .controls { gap: 8px; }
  .spacer { margin-left: 0; }
}
