/* common.css — shared chrome across flow / details / control pages.
   Rules here are the header, page nav, connection pill, base card
   hull, and the shared mobile overrides for those elements. Anything
   page-specific (flow diagram, detail rows, control tabs, etc.) stays
   in each page's inline <style> block so the per-page views can
   override as needed. Loaded via <link> BEFORE the inline <style>,
   so inline rules win on tie-breaks. */

:root {
  --bg: #101112; --bg-2: #191a1c;
  --card: #191a1c;
  --card-edge: rgba(255, 255, 255, 0.065);
  --border: rgba(255,255,255,0.055);
  --text: #dedede; --text-dim: #a8aaad; --text-muted: #96999d;
  /* Tesla-adjacent palette: warmer solar, cleaner home blue, neutral grid */
  --solar: #f59e0b; --solar-soft: #fcd34d;
  --grid:  #94a3b8; --grid-soft: #cbd5e1;
  --batt:  #10b981; --batt-soft: #6ee7b7;
  --home:  #3b82f6; --home-soft: #93c5fd;
  --red: #f87171; --red-soft: #fecaca;
}
* { box-sizing: border-box; }

/* Auth-gated UI defaults: hidden until JS proves we have a token. Reveal-
   on-evidence beats hide-on-evidence — without these rules the auth tabs
   and Edit buttons render visible until `_bootstrapApiToken` finishes
   (up to 2 s on cold launch), giving anyone reaching the URL via Funnel
   a window to navigate into write pages they can't actually use.
   `_applyAuthGatedUI` adds .auth-ok / .pywebview-ok once the token
   resolves; the :not() form lets each element's own CSS (inline-flex on
   tabs, etc.) take over without us hardcoding a per-element display
   value here. */
[data-auth-required]:not(.auth-ok) { display: none; }
[data-pywebview-only]:not(.pywebview-ok) { display: none; }
[data-tokenless-only].auth-ok { display: none !important; }
html {
  background-color: #101112;
  background-color: var(--bg);
  min-height: 100%;
  min-height: -webkit-fill-available;
  font-feature-settings: "cv11","ss01";
  font-variant-numeric: tabular-nums;
  overscroll-behavior: none;
  overflow-x: hidden;
  max-width: 100%;
}
body {
  margin: 0; padding: 0;
  background-color: #101112;
  background-color: var(--bg);
  color: var(--text);
  font-family: 'Inter', -apple-system, system-ui, sans-serif; font-size: 14px;
  min-height: 100vh;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  overflow-x: clip;
  max-width: 100%;
  width: 100%;
}
@supports not (overflow: clip) {
  body {
    overflow-x: hidden;
  }
}
/* Responsive media constraints */
canvas, img, svg {
  max-width: 100%;
}
.echart-canvas canvas,
.savings-chart canvas,
.cloud-chart canvas {
  max-width: 100% !important;
}

/* No max-width: content expands to fill the viewport. Horizontal padding
   uses clamp() so the gutter grows with viewport width — readable on a
   phone (16 px), comfortable on a laptop (32 px), airy on a 4K display
   (up to 80 px). Safe area insets ensure notch/island/home-bar clearance. */
.wrap {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  margin: 0 auto;
  padding-top: max(28px, calc(28px + env(safe-area-inset-top)));
  padding-bottom: max(64px, calc(64px + env(safe-area-inset-bottom)));
  padding-left: max(clamp(16px, 3vw, 80px), env(safe-area-inset-left));
  padding-right: max(clamp(16px, 3vw, 80px), env(safe-area-inset-right));
}

/* ================== meter-protection banner ==================
   Driven by /api/meter-state, rendered by common.js across every page.
   Sticky at the top of the viewport so it can't be missed when the
   user scrolls. Safe area insets prevent overlap with camera cutouts/notches.
   The 5 severity variants map from the 12 alert states:
     safe     → routine zone (rows 1-4)
     warning  → other-meter-at-risk standby (rows 5-6)
     urgent   → active meter at risk / stranded / last-stand-flip (rows 7-9, 15)
     lost     → at least one meter has crossed 200u (rows 10-14)
     worst    → both lost (row 16)
   When in safe state with no banner text, the element has the .hidden
   class and is not displayed. */
.meter-banner {
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  z-index: 100;
  box-sizing: border-box;
  max-width: 100vw;
  margin: -28px clamp(-80px, -3vw, -16px) 16px;   /* pull out into the wrap padding */
  padding: max(10px, calc(10px + env(safe-area-inset-top))) clamp(16px, 3vw, 80px) 10px;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  text-align: center;
  letter-spacing: 0.01em;
  box-shadow: 0 2px 12px rgba(0,0,0,0.35);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.meter-banner.hidden { display: none; }
.meter-banner.severity-safe    { background: rgba(16,185,129,0.85);  color: #04140d; }
.meter-banner.severity-warning { background: rgba(245,158,11,0.90);  color: #1a0e00; }
.meter-banner.severity-urgent  { background: rgba(239,68,68,0.92);   color: #fff; }
.meter-banner.severity-lost    { background: rgba(220,38,38,0.95);   color: #fff; }
.meter-banner.severity-worst   { background: rgba(0,0,0,0.92);       color: #fca5a5; }

.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark {
  width: 42px; height: 42px; border-radius: 12px;
  background: #202224;
  border: 1px solid var(--card-edge);
  display: grid; place-items: center;
  box-shadow: none;
}
.brand-mark svg { width: 22px; height: 22px; stroke: #b7bdbc; }
.title { font-size: 22px; font-weight: 700; letter-spacing: -0.015em; line-height: 1.15; }
.subtitle { font-size: 12px; color: var(--text-dim); margin-top: 3px; letter-spacing: 0.01em; }
.back-link {
  font-size: 13px; font-weight: 600; color: var(--text); text-decoration: none;
  padding: 9px 16px; border-radius: 10px;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--card-edge);
  transition: all 160ms ease;
  display: inline-flex; align-items: center; gap: 6px;
}
.back-link:hover { background: rgba(255,255,255,0.08); border-color: rgba(255,255,255,0.18); }

.card {
  background: var(--card);
  border: 1px solid var(--card-edge);
  border-radius: 18px; padding: 24px;
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  box-shadow: none;
}
.card-head {
  font-size: 10.5px; font-weight: 700; letter-spacing: 0.18em;
  color: var(--text-dim); text-transform: uppercase; margin-bottom: 18px;
}

/* Icon power button — red=connected (click disconnects), green=disconnected
   (click connects). Same visual as the details view. */
.icon-btn {
  width: 40px; height: 40px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 10px; padding: 0;
  background: rgba(248, 113, 113, 0.08);
  border: 1px solid rgba(248, 113, 113, 0.35);
  color: #fca5a5; cursor: pointer;
  transition: all 140ms ease;
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn:hover { background: rgba(248, 113, 113, 0.18); border-color: rgba(248, 113, 113, 0.6); }
.icon-btn:active { transform: translateY(1px); }
.icon-btn.disconnected {
  background: rgba(52, 211, 153, 0.08);
  border-color: rgba(52, 211, 153, 0.35);
  color: #6ee7b7;
}
.icon-btn.disconnected:hover { background: rgba(52, 211, 153, 0.18); border-color: rgba(52, 211, 153, 0.6); }
.icon-btn:disabled { opacity: 0.55; cursor: not-allowed; }
.header-actions { display: inline-flex; gap: 10px; align-items: center; }

/* ---------- Unified connection pill ----------
   Same widget on flow + details + control so the connection status reads
   identically across pages. Combines the status indicator (dot + text)
   with the connect/disconnect action (click toggles). Replaces the old
   mix of "red power icon when connected" + "green status dot" which
   encoded the same state twice with opposite colors. */
.conn-pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 14px; border-radius: 10px;
  background: rgba(52, 211, 153, 0.1);
  border: 1px solid rgba(52, 211, 153, 0.3);
  color: #6ee7b7;
  font-size: 12px; font-weight: 600;
  cursor: pointer; transition: all 160ms ease;
  font-family: inherit; letter-spacing: 0.02em;
  white-space: nowrap;
}
.conn-pill:hover:not(:disabled) {
  background: rgba(52, 211, 153, 0.18);
  border-color: rgba(52, 211, 153, 0.55);
}
.conn-pill:active { transform: translateY(1px); }
.conn-pill:disabled { cursor: wait; }
.conn-pill .conn-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: currentColor;
  box-shadow: 0 0 10px currentColor;
  flex-shrink: 0;
}
.conn-pill.connecting, .conn-pill.disconnecting {
  background: rgba(251, 191, 36, 0.1);
  border-color: rgba(251, 191, 36, 0.35);
  color: #fcd34d;
}
.conn-pill.connecting .conn-dot,
.conn-pill.disconnecting .conn-dot {
  animation: conn-pulse 1s ease-in-out infinite;
}
@keyframes conn-pulse {
  0%, 100% { box-shadow: 0 0 8px currentColor; }
  50%      { box-shadow: 0 0 18px currentColor; }
}
.conn-pill.disconnected {
  background: rgba(136, 146, 168, 0.08);
  border-color: rgba(136, 146, 168, 0.24);
  color: #94a3b8;
}
.conn-pill.error {
  background: rgba(248, 113, 113, 0.08);
  border-color: rgba(248, 113, 113, 0.3);
  color: #fca5a5;
}

/* ---------- Page nav (segmented tabs) ----------
   Shared pattern across flow / details / control so the three pages read
   as one app. Each page marks its own tab `.active`. Visually mirrors
   control.html's in-page `.tabs` so the user learns one idiom. */
.page-nav {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  display: flex; gap: 4px; margin-bottom: 22px;
  padding: 4px; border-radius: 14px;
  background: #171819;
  border: 1px solid var(--card-edge);
  backdrop-filter: blur(8px);
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -ms-overflow-style: none;
  -webkit-overflow-scrolling: touch;
}
.page-nav::-webkit-scrollbar { display: none; }
.page-tab {
  flex: 1 0 auto; text-align: center;
  white-space: nowrap;
  padding: 9px 13px; border-radius: 10px;
  text-decoration: none; font-weight: 600; font-size: 13px;
  color: var(--text-dim);
  border: 1px solid transparent;
  transition: all 180ms ease;
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.page-tab:hover:not(.active) { color: var(--text); background: rgba(255,255,255,0.03); }
.page-tab.active {
  background: #2b2d30;
  border-color: rgba(255,255,255,.12);
  color: var(--text);
  box-shadow: none;
}

/* ---------- Mobile overrides ----------
   Shared mobile adjustments for the chrome above. Each page may add its
   own `@media (max-width: 640px)` block with page-specific overrides;
   those inline rules come later in cascade and still win on ties. */
@media (max-width: 640px) {
  .wrap {
    padding-top: max(20px, calc(20px + env(safe-area-inset-top)));
    padding-bottom: max(48px, calc(48px + env(safe-area-inset-bottom)));
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
  }
  .meter-banner {
    margin-top: calc(-1 * max(20px, calc(20px + env(safe-area-inset-top))));
    margin-left: calc(-1 * max(14px, env(safe-area-inset-left)));
    margin-right: calc(-1 * max(14px, env(safe-area-inset-right)));
    padding-top: max(10px, calc(10px + env(safe-area-inset-top)));
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
    margin-bottom: 14px;
    max-width: 100vw;
  }
  .title { font-size: 20px; }
  .brand-mark { width: 38px; height: 38px; }
  .brand-mark svg { width: 20px; height: 20px; }
  .page-nav { margin-bottom: 18px; }
  .page-tab { font-size: 12.5px; padding: 8px 10px; min-height: 36px; }
  .conn-pill { padding: 7px 11px; font-size: 11.5px; gap: 7px; }
}

/* All content cards share equal edge insets; disclosure actions stay in flow. */
html body .card { padding: 12px; box-sizing: border-box; max-width: 100%; }
.card.collapsible-card { position: relative; }
.card > .card-disclosure-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; min-width: 0; width: 100%; box-sizing: border-box;
  margin-bottom: 16px; grid-column: 1 / -1;
}
.card .card-disclosure-header > .card-collapse-heading,
.card .card-disclosure-header > .card-collapsed-title {
  margin: 0 !important; padding: 0; min-width: 0; flex: 1;
}
.card .card-collapse-toggle {
  flex: 0 0 44px; width: 44px; height: 44px; display: grid; place-items: center;
  padding: 0; border: 0; border-radius: 10px;
  background: transparent; color: var(--text-dim); cursor: pointer;
}
.card .card-collapse-toggle:hover { color: var(--text); background: rgba(255,255,255,.06); }
.card .card-collapse-toggle:focus-visible { outline: 2px solid #60a5fa; outline-offset: 2px; }
.card .card-collapsed-title {
  color: var(--text-dim); font-size: 10.5px; font-weight: 700;
  letter-spacing: .18em; text-transform: uppercase;
}
.card.collapsible-card.card-collapsed { display: block; min-height: 0; height: auto; width: 100%; box-sizing: border-box; align-self: start; }
.card.card-collapsed > :not(.card-disclosure-header) { display: none !important; }
.card.card-collapsed > .card-disclosure-header { margin-bottom: 0; }
.card.card-collapsed .card-collapse-toggle svg { transform: rotate(-90deg); }
.card.donut-card.collapsible-card { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0 36px; }
.card.donut-card.card-collapsed { display: block; }
@media (max-width: 720px) {
  .card.donut-card.collapsible-card { grid-template-columns: minmax(0, 1fr); row-gap: 16px; }
}
@media (max-width: 640px) {
  html body .card { padding: 8px; }
}

/* Low-glare neutral surfaces shared across every page. */
html body .card {
  background: var(--card);
  border-color: var(--card-edge);
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
  box-shadow: none;
}
.conn-pill .conn-dot { box-shadow: none; }
.conn-pill.connecting .conn-dot,
.conn-pill.disconnecting .conn-dot { animation: none; }
.page-tab:focus-visible { outline: 2px solid #aeb4ba; outline-offset: 2px; }

/* Native menus follow the same neutral dark palette as custom pickers. */
select { color-scheme: dark; color: var(--text); background-color: #222426; border-color: var(--card-edge); }
select option, select optgroup { background: #222426; color: var(--text); }
select:focus-visible { outline: 2px solid #aeb4ba; outline-offset: 2px; }

input[type="date"], input[type="datetime-local"] { color-scheme: dark; }

/* One header layout on all dashboard pages. */
html body header .header-actions { display: flex; align-items: center; flex-direction: row; flex-wrap: wrap; justify-content: flex-end; gap: 10px; }
.header-freshness { color: var(--text-dim); font-size: 11px; white-space: nowrap; font-variant-numeric: tabular-nums; }
html body header .icon-btn { width: 44px; height: 44px; padding: 0; background: #222426; color: var(--text-dim); border: 1px solid var(--card-edge); border-radius: 9px; box-shadow: none; }
html body header .conn-pill { height: 44px; padding: 0 14px; display: inline-flex; align-items: center; justify-content: center; border-radius: 9px; }
html body header .icon-btn:hover { background: #303235; color: var(--text); }
@media (max-width: 640px) {
  html body header { flex-wrap: wrap; gap: 14px; }
  html body header .header-actions { width: 100%; justify-content: flex-end; }
  .header-freshness { margin-right: auto; }
}

html body button.copy-btn, html body button.btn-copy {
  width: 44px; height: 44px; padding: 0; display: inline-flex;
  align-items: center; justify-content: center; flex-shrink: 0;
  color: var(--text-dim); background: #222426; border: 1px solid var(--card-edge);
}
button.copy-btn svg, button.btn-copy svg { width: 18px; height: 18px; }
button .copy-check { display: none; }
button.copied .copy-symbol { display: none; }
button.copied .copy-check { display: block; color: #9ac8ad; }
button.copy-btn:focus-visible, button.btn-copy:focus-visible { outline: 2px solid #aeb4ba; outline-offset: 2px; }

/* Prevent rubber-band overscroll in the native viewer and browser pages. */
html, body { overscroll-behavior: none; background-color: var(--bg); }
