/* ---------------------------------------------------------------- tokens */
:root {
  color-scheme: light;

  --surface-0: #f2f2f0;
  --surface-1: #fcfcfb;
  --surface-2: #ebebe7;
  --border: #dededa;
  --border-strong: #c9c9c3;

  --text-primary: #14140f;
  --text-secondary: #52514e;
  --text-muted: #7b7a75;

  /* Validated categorical palette (light): yellow / blue / red.
     self-consumed = yellow, exported = blue (money in), imported = red (money out). */
  --series-self: #eda100;
  --series-export: #2a78d6;
  --series-import: #e34948;
  --series-line: #4a4a44;

  --good: #1a7f4b;
  --bad: #c0392b;

  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 4px 12px rgba(0, 0, 0, 0.04);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans SC',
    'PingFang SC', 'Microsoft YaHei', Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme='light'])) {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #242422;
    --border: #33332f;
    --border-strong: #4a4a45;
    --text-primary: #f6f6f2;
    --text-secondary: #c3c2b7;
    --text-muted: #99988e;
    --series-self: #c98500;
    --series-export: #3987e5;
    --series-import: #e0455f;
    --series-line: #d8d7cd;
    --good: #3fae72;
    --bad: #e0455f;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}
:root[data-theme='dark'] {
  color-scheme: dark;
  --surface-0: #121211;
  --surface-1: #1a1a19;
  --surface-2: #242422;
  --border: #33332f;
  --border-strong: #4a4a45;
  --text-primary: #f6f6f2;
  --text-secondary: #c3c2b7;
  --text-muted: #99988e;
  --series-self: #c98500;
  --series-export: #3987e5;
  --series-import: #e0455f;
  --series-line: #d8d7cd;
  --good: #3fae72;
  --bad: #e0455f;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--surface-0);
  color: var(--text-primary);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  padding-bottom: env(safe-area-inset-bottom);
  overscroll-behavior-y: contain;
}

/* ---------------------------------------------------------------- chrome */
header.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) 14px 10px;
  background: color-mix(in srgb, var(--surface-1) 88%, transparent);
  backdrop-filter: saturate(160%) blur(12px);
  border-bottom: 1px solid var(--border);
}

.topbar h1 {
  font-size: 17px;
  font-weight: 650;
  margin: 0;
  letter-spacing: -0.01em;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar .sub {
  display: block;
  font-size: 12px;
  font-weight: 450;
  color: var(--text-muted);
}

.icon-btn, .pill {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
  border-radius: 999px;
  font-size: 13px;
  font-weight: 550;
  padding: 7px 12px;
  cursor: pointer;
  font-family: inherit;
  min-height: 34px;
}
.icon-btn:active, .pill:active { transform: scale(0.97); }
.pill[aria-pressed='true'] {
  background: var(--text-primary);
  color: var(--surface-1);
  border-color: var(--text-primary);
}

main { padding: 14px; max-width: 900px; margin: 0 auto; }

/* ------------------------------------------------------------- GST switch */
/* One switch, directly under the header, applying to every screen — rather
   than a control repeated in each toolbar, which made it unclear which basis
   a given figure was on. Wording mirrors Ecotricity's own plan page. */
.gstbar {
  position: sticky;
  top: 0;
  z-index: 15;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  max-width: 900px;
  margin: 0 auto;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  min-height: 34px;
  user-select: none;
  -webkit-user-select: none;
}
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .track {
  position: relative;
  flex: none;
  width: 44px;
  height: 26px;
  border-radius: 999px;
  background: var(--border-strong);
  transition: background 0.18s ease;
}
.switch .knob {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.18s ease;
}
.switch input:checked + .track { background: var(--good); }
.switch input:checked + .track .knob { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--text-primary); outline-offset: 2px; }
.switch-label { font-size: 13px; font-weight: 550; color: var(--text-primary); white-space: nowrap; }

.gstbar-state {
  margin-left: auto;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.gstbar-state.on { color: var(--good); border-color: color-mix(in srgb, var(--good) 40%, var(--border)); }

@media (max-width: 400px) {
  .gstbar { gap: 8px; padding: 8px 12px; }
  .switch-label { font-size: 12px; }
}
@media (max-width: 340px) {
  .gstbar-state { display: none; }
}

.hint {
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
  margin: 2px 0 12px;
}

/* ----------------------------------------------------------------- cards */
.grid { display: grid; gap: 12px; }
@media (min-width: 620px) { .grid.two { grid-template-columns: 1fr 1fr; } }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card > h2 {
  margin: 0;
  padding: 12px 14px 10px;
  font-size: 12px;
  font-weight: 650;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}
.card > h2 .chev { margin-left: auto; color: var(--text-muted); font-weight: 400; }
.card-body { padding: 14px; }
button.card {
  display: block;
  width: 100%;
  text-align: left;
  font: inherit;
  color: inherit;
  cursor: pointer;
  padding: 0;
}
button.card:active { background: var(--surface-2); }

/* ------------------------------------------------------------ stat tiles */
.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 12px;
}
.stat {
  background: var(--surface-2);
  border-radius: 10px;
  padding: 10px 12px;
}
.stat .k {
  font-size: 11px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  display: flex;
  align-items: center;
  gap: 6px;
}
.stat .v {
  font-size: 20px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  margin-top: 3px;
}
.stat .v.pos { color: var(--good); }
.stat .v.neg { color: var(--bad); }
.stat .n { font-size: 11px; color: var(--text-muted); margin-top: 2px; }

.hero {
  font-size: 40px;
  font-weight: 700;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
  line-height: 1.05;
}
.hero.pos { color: var(--good); }
.hero.neg { color: var(--bad); }
.hero-note { font-size: 13px; color: var(--text-secondary); margin-top: 4px; }

i.dot {
  width: 10px; height: 10px; border-radius: 3px; display: inline-block; flex: none;
}

/* ------------------------------------------------------------ power flow */
.flow {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px 8px;
  text-align: center;
}
@media (min-width: 480px) { .flow { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.flow .cell { display: flex; flex-direction: column; align-items: center; gap: 4px; min-width: 0; }
.gauge-wrap { position: relative; width: 96px; height: 96px; max-width: 100%; }
.gauge { display: block; }
.gauge-track { stroke: var(--surface-2); }
.gauge-text {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1px;
}
.gauge-text strong { font-size: 15px; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.gauge-text span { font-size: 10px; color: var(--text-muted); }
.flow .cap { font-size: 11px; color: var(--text-secondary); }

/* ---------------------------------------------------------------- charts */
.plot { position: relative; width: 100%; }
.plot svg { display: block; width: 100%; touch-action: pan-y; }
/* Chart primitives are scoped under .plot svg so they can never collide with
   the .grid layout class — an unscoped `.grid { stroke }` leaks into every SVG
   child by inheritance and outlines each bar. */
.plot svg { stroke: none; }
.plot svg .chart-grid { stroke: var(--border); stroke-width: 1; }
.plot svg .axis-line { stroke: var(--border-strong); stroke-width: 1; }
.plot svg text.axis { fill: var(--text-muted); font-size: 10px; font-family: var(--font); }
.plot svg .crosshair { stroke: var(--border-strong); stroke-width: 1; stroke-dasharray: 3 3; }
.plot svg .cursor-dot { fill: var(--surface-1); stroke: var(--text-primary); stroke-width: 2; }
.plot svg .bar-empty { fill: var(--border); }
.hit.clickable { cursor: pointer; }

.tt {
  position: absolute;
  pointer-events: none;
  background: var(--surface-1);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.5;
  min-width: 132px;
  z-index: 5;
}
.tt .tt-title { font-weight: 650; margin-bottom: 4px; }
.tt .tt-row { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.tt .tt-row b { margin-left: auto; font-variant-numeric: tabular-nums; }

.legend {
  display: flex; flex-wrap: wrap; gap: 6px 14px;
  padding: 10px 2px 0; font-size: 12px; color: var(--text-secondary);
}
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.legend-item i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.legend-item i.as-line { height: 2px; border-radius: 1px; }

p.empty { color: var(--text-muted); font-size: 13px; text-align: center; padding: 32px 0; margin: 0; }

/* ----------------------------------------------------------------- table */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px 10px; text-align: right; border-bottom: 1px solid var(--border); }
th:first-child, td:first-child { text-align: left; }
thead th { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-secondary); font-weight: 600; }
td { font-variant-numeric: tabular-nums; }
tbody tr:last-child td { border-bottom: none; }
tfoot td { font-weight: 650; border-top: 1px solid var(--border-strong); }

/* sortable headers */
table.sortable th { padding: 0; }
.sort-btn {
  appearance: none;
  background: none;
  border: 0;
  font: inherit;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 10px;
  width: 100%;
  text-align: inherit;
  min-height: 40px;           /* comfortable tap target on a phone */
  border-radius: 8px;
}
table.sortable th:first-child .sort-btn { text-align: left; }
table.sortable th:not(:first-child) .sort-btn { text-align: right; }
.sort-btn:hover { background: var(--surface-2); }
.sort-btn.active { color: var(--text-primary); }
.sort-btn .caret { font-size: 10px; opacity: 0.9; }

/* -------------------------------------------------------------- controls */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; }
.toolbar .spacer { flex: 1; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: 2px; }
.seg button {
  appearance: none; border: 0; background: transparent; color: var(--text-secondary);
  font: inherit; font-size: 13px; font-weight: 550; padding: 6px 12px; border-radius: 999px;
  cursor: pointer; min-height: 30px;
}
.seg button[aria-pressed='true'] { background: var(--surface-1); color: var(--text-primary); box-shadow: var(--shadow); }

label.field { display: block; margin-bottom: 12px; font-size: 13px; color: var(--text-secondary); }
label.field span { display: block; margin-bottom: 4px; font-weight: 550; }
input[type='text'], input[type='email'], input[type='password'], input[type='number'], input[type='time'], select {
  width: 100%; font: inherit; font-size: 16px; padding: 9px 11px;
  border: 1px solid var(--border-strong); border-radius: 9px;
  background: var(--surface-1); color: var(--text-primary);
}
.row { display: flex; gap: 8px; align-items: flex-end; }
.row > * { flex: 1; }
.row .shrink { flex: 0 0 auto; }

.band-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(0, 0.8fr) auto;
  gap: 6px;
  align-items: center;
  margin-bottom: 6px;
}
/* grid items default to min-width:auto, which lets the native time pickers
   push the rate field to zero width */
.band-row > * { min-width: 0; }
.band-row input { font-size: 14px; padding: 7px 4px; width: 100%; }
.band-row button.pill { padding: 6px 10px; min-height: 30px; }
.tiny { font-size: 11px; color: var(--text-muted); }
.danger { color: var(--bad); }
.notice { padding: 10px 12px; border-radius: 9px; background: var(--surface-2); font-size: 13px; margin-bottom: 12px; }
.notice.err { background: color-mix(in srgb, var(--bad) 14%, var(--surface-2)); }
.notice.ok { background: color-mix(in srgb, var(--good) 14%, var(--surface-2)); }

.sheet {
  position: fixed; inset: 0; z-index: 50;
  background: var(--surface-0);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.sheet .box { width: 100%; max-width: 320px; }

.spinner {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--border-strong); border-top-color: var(--text-primary);
  animation: spin 0.7s linear infinite; display: inline-block; vertical-align: -3px;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
