/* Home Energy: phone first. Base styles are for an iPhone in portrait;
   wider screens add columns in the media query at the end. */

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --line: rgba(15, 23, 42, 0.07);
  --line-strong: rgba(15, 23, 42, 0.14);
  --ink: #111827;
  --ink-mute: #6b7280;
  --ink-faint: #a3a9b6;
  --blur-bg: rgba(245, 246, 248, 0.82);
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 6px 24px rgba(15, 23, 42, 0.05);

  --solar: #e0a00e;
  --battery: #16a34a;
  --grid-in: #e5484d;
  --grid-out: #2f7fe0;
  --car: #8b5cf6;
  --warn: #ea8a1a;
  --danger: #e11d48;
  --accent: #2f7fe0;

  --amber-extremely_low: #0891b2;
  --amber-very_low: #059669;
  --amber-low: #65a30d;
  --amber-neutral: #d99a0b;
  --amber-high: #ea580c;
  --amber-spike: #e11d48;

  --radius: 20px;
  --gutter: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, sans-serif;
  --font-display: -apple-system, BlinkMacSystemFont, "SF Pro Display", system-ui, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, monospace;

  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  min-height: 100vh;
  min-height: 100dvh;
  overscroll-behavior-y: contain;
}

button { font: inherit; color: inherit; }

.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 */

.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(var(--safe-top) + 10px) calc(var(--gutter) + var(--safe-right)) 10px calc(var(--gutter) + var(--safe-left));
  background: var(--blur-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
}
.top h1 {
  margin: 0; font-family: var(--font-display);
  font-size: 22px; font-weight: 700; letter-spacing: -0.02em;
}
.top-sub { display: flex; align-items: center; gap: 7px; font-size: 13px; color: var(--ink-mute); }
.live-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-faint); }
body[data-phase="ready"] .live-dot { background: var(--battery); }
body[data-phase="error"] .live-dot { background: var(--danger); }
body[data-stale="true"] .live-dot { background: var(--warn); }

.pill {
  font-size: 11px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase;
  border: 1px solid var(--line-strong); border-radius: 999px; padding: 1px 8px;
}

.icon-btn {
  width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--surface); border: 1px solid var(--line); cursor: pointer;
}
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn:active { transform: scale(0.94); }
.spinning { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Pull to refresh indicator */
.ptr {
  position: fixed; left: 50%; top: calc(var(--safe-top) + 64px); z-index: 11;
  width: 36px; height: 36px; margin-left: -18px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--surface); border: 1px solid var(--line-strong);
  color: var(--accent); opacity: 0; pointer-events: none;
  transform: translateY(calc(var(--pull, 0px) - 50px)) rotate(calc(var(--pull, 0) * 4deg));
}
.ptr svg { width: 18px; height: 18px; }
.ptr[data-armed="true"] { color: var(--battery); }

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

.wrap {
  display: grid; gap: 12px;
  max-width: 1120px; margin: 0 auto;
  padding: 12px calc(var(--gutter) + var(--safe-right)) calc(28px + var(--safe-bottom)) calc(var(--gutter) + var(--safe-left));
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  min-width: 0;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.card-title { font-size: 15px; font-weight: 600; }
.fresh { font-size: 12px; color: var(--ink-mute); }
.fresh[data-stale="true"] { color: var(--warn); }

.label { font-size: 12px; color: var(--ink-mute); }
small { font-size: 0.6em; font-weight: 500; color: var(--ink-mute); margin-left: 3px; }
.v, .price-big, .soc, .ps-soc, dd, .node-val { font-variant-numeric: tabular-nums; }

body[data-phase="loading"] .skel { color: transparent; background: var(--surface-2); border-radius: 8px; }

/* ------------------------------------------------------------------ banner */

.banner {
  display: flex; align-items: flex-start; gap: 10px;
  padding: 12px 14px; border-radius: 16px;
  background: color-mix(in srgb, var(--warn) 14%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent);
}
.banner[data-level="error"] {
  background: color-mix(in srgb, var(--danger) 14%, var(--surface));
  border-color: color-mix(in srgb, var(--danger) 45%, transparent);
}
.b-icon { width: 20px; height: 20px; flex: none; color: var(--warn); margin-top: 1px; }
.banner[data-level="error"] .b-icon { color: var(--danger); }
.b-text { flex: 1; min-width: 0; }
.b-title { font-weight: 600; font-size: 14px; }
.b-detail { font-size: 13px; color: var(--ink-mute); overflow-wrap: anywhere; }
.b-retry {
  flex: none; min-height: 36px; padding: 0 14px; border-radius: 999px;
  border: 1px solid var(--line-strong); background: transparent; font-size: 14px; font-weight: 600;
}

/* -------------------------------------------------------- shared pieces */

.card-head .chip { margin-left: auto; }
.label { font-size: 12px; color: var(--ink-mute); }
.v { font-size: 18px; font-weight: 650; letter-spacing: -0.01em; }
.v small, .money-big small { font-size: 0.62em; }
.bar { height: 6px; border-radius: 4px; background: var(--surface-2); overflow: hidden; margin-top: 6px; }
.bar-fill { height: 100%; width: 0; background: var(--battery); border-radius: 4px; transition: width 0.8s ease; }
.bar.car .bar-fill { background: var(--car); }
@media (prefers-reduced-motion: reduce) { .bar-fill { transition: none; } }
.c-solar { background: var(--solar); } .c-home { background: var(--ink-mute); } .c-ink { background: var(--ink); }
.c-export { background: var(--grid-out); } .c-import { background: var(--grid-in); } .c-batt { background: var(--battery); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--ink-mute); margin-top: 6px; }
.legend i { display: inline-block; width: 10px; height: 3px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }

/* --------------------------------------------------------------------- now */

.now .card-head { margin-bottom: 4px; }
.price-big {
  font-family: var(--font-display);
  font-size: 56px; font-weight: 700; letter-spacing: -0.04em; line-height: 1;
}
.price-big small { font-size: 16px; letter-spacing: 0; font-weight: 500; }
.price-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 14px; margin-top: 10px; font-size: 14px; font-weight: 600; }
.tag { --tint: var(--amber-neutral); display: inline-flex; align-items: center; gap: 7px; color: var(--tint); }
.tag .dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.rate { color: var(--ink-mute); }
.rate[data-kind="earn"] { color: var(--battery); }
.rate[data-kind="cost"] { color: var(--grid-in); }
.summary { margin: 10px 0 16px; font-size: 14px; color: var(--ink-mute); }

.stats {
  display: grid; grid-template-columns: 1fr 1fr 1.3fr; gap: 12px;
  padding-top: 14px; border-top: 1px solid var(--line);
}
.stat-sub { font-size: 12px; color: var(--ink-mute); margin-top: 6px; line-height: 1.45; }
.stat-sub span { display: block; white-space: nowrap; }

.mode-row {
  width: 100%; margin-top: 14px; min-height: 52px;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 12px 8px 14px; border-radius: 14px;
  background: var(--surface-2); border: 1px solid var(--line); text-align: left; cursor: pointer;
}
.mode-row-text { display: grid; }
.mode-current { font-size: 16px; font-weight: 600; }
.chev { width: 20px; height: 20px; color: var(--ink-mute); }

/* -------------------------------------------------------------- power flow */

.sankey-cols {
  display: flex; justify-content: space-between;
  font-size: 11px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--ink-faint); padding: 0 2px 4px;
}
.sankey svg { width: 100%; height: auto; display: block; overflow: visible; }
.sankey .ribbon { opacity: 0.32; }
.sankey .stream { fill: none; stroke-linecap: round; opacity: 0.9; stroke-dasharray: 1.5 9; }
@media (prefers-reduced-motion: no-preference) {
  .sankey .stream { animation: stream var(--speed, 1.4s) linear infinite; }
}
@keyframes stream { to { stroke-dashoffset: -21; } }
.sankey .node { rx: 3; }
.sankey .s-name { font-size: 13px; fill: var(--ink-mute); font-family: var(--font); }
.sankey .s-val { font-size: 17px; font-weight: 700; fill: var(--ink); font-family: var(--font); font-variant-numeric: tabular-nums; }
.sankey .s-flow { font-size: 11px; font-weight: 600; font-family: var(--font); fill: var(--ink); paint-order: stroke; stroke: var(--surface); stroke-width: 4px; stroke-linejoin: round; }
.sankey .idle { font-size: 14px; fill: var(--ink-mute); font-family: var(--font); }
.flow-foot { margin: 8px 0 0; font-size: 13px; color: var(--ink-mute); }

/* ------------------------------------------------------------------- today */

.money { padding-bottom: 16px; margin-bottom: 16px; border-bottom: 1px solid var(--line); }
.money-big { font-family: var(--font-display); font-size: 40px; font-weight: 700; letter-spacing: -0.03em; line-height: 1.1; }
.money-big.pos { color: var(--battery); }
.money-big.neg { color: var(--grid-in); }
.money-sub { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 13px; color: var(--ink-mute); margin-top: 4px; }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 12px; }
.tile .label { display: flex; align-items: center; gap: 6px; }
.tile .label i { width: 8px; height: 8px; border-radius: 2px; display: inline-block; }
.tile .v { font-size: 20px; margin-top: 2px; }
.chart { position: relative; height: 132px; }
.chart.day { height: 110px; margin-top: 16px; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart .fbar { rx: 2.5; }
.chart .axis { font-size: 10px; fill: var(--ink-mute); font-family: var(--font); }
.chart .zero { stroke: var(--line-strong); stroke-width: 1; }
.chart .best { fill: color-mix(in srgb, var(--battery) 14%, transparent); }
.chart .now-line { stroke: var(--ink-faint); stroke-dasharray: 2 3; }
.forecast-foot { margin-top: 10px; font-size: 13px; color: var(--ink-mute); }
.forecast-foot strong { color: var(--ink); }

/* --------------------------------------------------------------- vehicle */

.ps-figrow { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.ps-soc { font-family: var(--font-display); font-size: 32px; font-weight: 700; letter-spacing: -0.03em; }
.ps-range { font-size: 14px; color: var(--ink-mute); }
.ps-status { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--ink-mute); margin-top: 10px; }
.s-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ink-faint); }
.ps-status[data-charging="true"] { color: var(--car); }
.ps-status[data-charging="true"] .s-dot { background: var(--car); }

/* ------------------------------------------------------------------- sheet */

.sheet {
  position: fixed; inset: auto 0 0 0; margin: 0 auto;
  width: 100%; max-width: 520px; max-height: 88dvh;
  border: none; border-radius: 24px 24px 0 0;
  padding: 8px 16px calc(16px + var(--safe-bottom));
  background: var(--surface); color: var(--ink);
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.35);
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.45); }
.sheet-grip { width: 38px; height: 5px; border-radius: 3px; background: var(--line-strong); margin: 0 auto 12px; }
.sheet h2 { margin: 0; font-size: 20px; font-family: var(--font-display); }
.sheet-note { margin: 4px 0 14px; color: var(--ink-mute); font-size: 14px; }
.sheet-options { display: grid; gap: 8px; }
.opt {
  min-height: 52px; display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 0 14px; border-radius: 14px; cursor: pointer;
  background: var(--surface-2); border: 1.5px solid transparent; text-align: left; font-size: 16px; font-weight: 500;
}
.opt[aria-checked="true"] { border-color: var(--accent); }
.opt .cur { font-size: 12px; color: var(--ink-mute); }
.sheet-actions { display: grid; grid-template-columns: 1fr 1.4fr; gap: 10px; margin-top: 16px; }
.btn { min-height: 50px; border-radius: 14px; border: 1px solid var(--line-strong); font-size: 16px; font-weight: 600; cursor: pointer; background: transparent; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn:disabled { opacity: 0.45; cursor: default; }

/* ------------------------------------------------------------------ footer */

footer { font-size: 12px; color: var(--ink-faint); display: grid; gap: 6px; text-align: center; }
.sources {
  display: grid; text-align: left; font-size: 13px;
  background: var(--surface); box-shadow: var(--shadow); border-radius: 16px; padding: 4px 16px;
}
.conn { display: grid; grid-template-columns: 10px auto 1fr; align-items: center; gap: 10px; padding: 9px 0; }
.conn + .conn { border-top: 1px solid var(--line); }
.conn-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ink-faint); }
.conn-name { color: var(--ink); font-weight: 500; }
.conn-info { color: var(--ink-mute); text-align: right; overflow-wrap: anywhere; }
.conn[data-state="ok"] .conn-dot { background: var(--battery); }
.conn[data-state="stale"] .conn-dot { background: var(--warn); }
.conn[data-state="stale"] .conn-info { color: var(--warn); }
.conn[data-state="error"] .conn-dot { background: var(--danger); }
.conn[data-state="error"] .conn-info { color: var(--danger); }
.conn[data-state="sample"] .conn-dot { background: var(--accent); }

/* ------------------------------------------------------------------- toast */

.toast {
  position: fixed; left: 50%; bottom: calc(20px + var(--safe-bottom)); z-index: 30;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  background: var(--surface-2); border: 1px solid var(--line-strong);
  padding: 10px 16px; border-radius: 14px; font-size: 14px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  max-width: min(calc(100vw - 32px), 460px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast[data-level="error"] { border-color: color-mix(in srgb, var(--danger) 55%, transparent); }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ------------------------------------- larger screens: two even columns */

@media (min-width: 820px) {
  :root { --gutter: 24px; }
  /* Cards interlock so both columns end level: Now and Today on the left,
     Power flow and Next hours on the right, each stretched to fill its area. */
  /* Cards interlock so both columns end level: Now and Today on the left,
     Power flow and Next hours on the right, each stretched to fill. Rows are
     explicit so nothing auto places into a hidden banner's empty row. */
  .wrap {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px; max-width: 1080px; align-items: stretch;
  }
  .banner { grid-column: 1 / -1; grid-row: 1; }
  .now { grid-column: 1; grid-row: 2; }
  .flow-card { grid-column: 2; grid-row: 2 / 4; }
  .today-card { grid-column: 1; grid-row: 3 / 5; }
  #forecastCard { grid-column: 2; grid-row: 4; }
  #vehicleCard { grid-column: 1 / -1; grid-row: 5; }
  footer { grid-column: 1 / -1; grid-row: 6; }
  .card { display: flex; flex-direction: column; }
  .card[hidden] { display: none; }
  .now .stats { margin-top: auto; }
  .flow-card .sankey { flex: 1; display: flex; align-items: center; }
  .flow-card .sankey svg { max-height: 300px; }
  #forecastCard .chart { flex: 1; min-height: 132px; }
  .today-card .chart.day { flex: 1; min-height: 110px; }
  .price-big { font-size: 68px; }
  .tiles { grid-template-columns: repeat(3, 1fr); }
}
