:root {
  --bg: #0d0d0d;
  --surface: #1a1a1a;
  --surface2: #242424;
  --border: #2e2e2e;
  --text: #e8e8e8;
  --text-muted: #888;
  --accent: #e82127;
  --green: #30d158;
  --yellow: #ffd60a;
  --blue: #0a84ff;
  --red: #ff453a;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  min-height: 100vh;
}

/* ── Header ── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-wrap: wrap;
  gap: 8px;
  position: sticky;
  top: 0;
  z-index: 100;
}
header h1 { font-size: 18px; font-weight: 600; letter-spacing: -0.3px; }
header h1 span { color: var(--accent); }
#last-updated { font-size: 12px; color: var(--text-muted); }

/* ── Layout ── */
main { max-width: 1100px; margin: 0 auto; padding: 24px 20px; }
.grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; margin-bottom: 24px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 24px; }
.grid-4 { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; gap: 16px; margin-bottom: 24px; }
@media (max-width: 1050px) { .grid-4 { grid-template-columns: 1fr 1fr; } }
@media (max-width: 820px) { .grid, .grid-2 { grid-template-columns: 1fr; } }
@media (max-width: 500px) { .grid-4 { grid-template-columns: 1fr; } }
.solar-kpi-row { display: flex; gap: 32px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 14px; }

/* ── Cards ── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.card-title {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 14px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.big-value { font-size: 40px; font-weight: 700; letter-spacing: -1px; line-height: 1; }
.big-value .unit { font-size: 18px; font-weight: 400; color: var(--text-muted); margin-left: 2px; }
.sub-value { font-size: 13px; color: var(--text-muted); margin-top: 6px; }

/* ── Pills ── */
.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 12px;
  border-radius: 100px;
  font-size: 13px;
  font-weight: 500;
}
.pill .dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.pill-mini { font-size: 11px; padding: 3px 8px; }
.pill-mini .dot { width: 6px; height: 6px; }
.pill.active { background: rgba(48,209,88,.15); color: var(--green); }
.pill.active .dot { background: var(--green); box-shadow: 0 0 6px var(--green); animation: pulse 1.5s infinite; }
.pill.inactive { background: rgba(136,136,136,.12); color: var(--text-muted); }
.pill.inactive .dot { background: var(--text-muted); }
.pill.warning { background: rgba(255,214,10,.12); color: var(--yellow); }
.pill.warning .dot { background: var(--yellow); }
.pill.error { background: rgba(255,69,58,.12); color: var(--red); }
.pill.error .dot { background: var(--red); }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

/* ── Toggle ── */
.control-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.control-row:last-child { margin-bottom: 0; }
.control-label { font-size: 14px; }
.control-sub { font-size: 12px; color: var(--text-muted); }
.toggle { position: relative; width: 50px; height: 28px; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; position: absolute; }
.toggle-track { position: absolute; inset: 0; background: var(--border); border-radius: 100px; cursor: pointer; transition: background .2s; }
.toggle-track::after { content: ''; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; background: #fff; border-radius: 50%; transition: transform .2s; box-shadow: 0 1px 4px rgba(0,0,0,.4); }
.toggle input:checked + .toggle-track { background: var(--green); }
.toggle input:checked + .toggle-track::after { transform: translateX(22px); }

/* ── Buttons ── */
button { border: none; border-radius: 8px; cursor: pointer; font-family: var(--font); font-size: 14px; font-weight: 500; padding: 9px 16px; transition: opacity .15s, transform .1s; }
button:active { transform: scale(.97); }
button:hover { opacity: .85; }
button:disabled { opacity: .4; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-small { padding: 6px 12px; font-size: 13px; }
.btn-sm-green { background: rgba(48,209,88,.12); color: var(--green); border: 1px solid rgba(48,209,88,.25); }
.btn-sm-red { background: rgba(255,69,58,.12); color: var(--red); border: 1px solid rgba(255,69,58,.25); }
.btn-blue { background: var(--blue); color: #fff; }
.btn-row { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }

/* ── Progress bar ── */
.progress-bar { height: 8px; background: var(--surface2); border-radius: 100px; margin-top: 12px; overflow: hidden; }
.progress-bar-fill { height: 100%; border-radius: 100px; background: var(--green); transition: width .5s ease; }

/* ── Dispatch list ── */
.dispatch-list { list-style: none; overflow-x: hidden; }
.dispatch-item { display: flex; align-items: center; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 13px; gap: 8px; }
.dispatch-item:last-child { border-bottom: none; }
.dispatch-time { font-weight: 500; }
.dispatch-meta { color: var(--text-muted); }
.dispatch-kwh { color: var(--green); font-weight: 500; flex-shrink: 0; }
.dispatch-active { background: rgba(48,209,88,.07); border-radius: 8px; padding: 10px; margin: 0 -10px; }
.empty-state { color: var(--text-muted); font-size: 13px; padding: 16px 0; text-align: center; }

/* ── Activity log ── */
.log-list { list-style: none; max-height: 480px; overflow-y: auto; }
.log-range-btns { display: flex; gap: 4px; }
.log-item { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; align-items: start; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.log-item:last-child { border-bottom: none; }
.log-time { color: var(--text-muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
.log-msg { line-height: 1.4; }
.log-info .log-msg { color: var(--text); }
.log-success .log-msg { color: var(--green); }
.log-error .log-msg { color: var(--red); }
.log-warning .log-msg { color: var(--yellow); }

/* ── Settings accordion ── */
details { border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 16px; }
details > summary { padding: 16px 20px; cursor: pointer; font-weight: 600; font-size: 14px; list-style: none; display: flex; align-items: center; justify-content: space-between; user-select: none; }
.summary-label { display: flex; align-items: center; flex: 1; justify-content: space-between; }
details > summary::after { content: '›'; font-size: 20px; color: var(--text-muted); transition: transform .2s; }
details[open] > summary::after { transform: rotate(90deg); }
.settings-body { padding: 0 20px 20px; }
.settings-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 24px; }
@media (max-width: 600px) { .settings-grid { grid-template-columns: 1fr; } }

/* ── Analytics range buttons ── */
.range-btn  { padding: 3px 10px; font-size: 11px; font-weight: 600; border-radius: 6px;
              border: 1px solid var(--border); background: var(--surface2); color: var(--text-muted);
              cursor: pointer; font-family: var(--font); transition: background .15s; }
.range-btn:hover  { color: var(--text); background: var(--card); }
.range-btn.active { background: rgba(10,132,255,.2); border-color: var(--blue); color: var(--blue); }
.range-nav  { padding: 3px 10px; font-size: 11px; border-radius: 6px;
              border: 1px solid var(--border); background: var(--surface2); color: var(--text-muted);
              cursor: pointer; font-family: var(--font); transition: opacity .15s; }
.range-nav:disabled { opacity: .35; cursor: default; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12px; font-weight: 500; color: var(--text-muted); text-transform: uppercase; letter-spacing: .5px; }
.field input, .field select {
  background: var(--surface2); border: 1px solid var(--border); border-radius: 8px;
  color: var(--text); font-family: var(--font); font-size: 14px; padding: 10px 12px;
  width: 100%; transition: border-color .15s;
}
.field input:focus, .field select:focus { outline: none; border-color: var(--blue); }
.field input::placeholder { color: #555; }
.field select option { background: var(--surface2); }
.settings-divider { border: none; border-top: 1px solid var(--border); margin: 20px 0; }
.hint { font-size: 12px; color: var(--text-muted); margin-bottom: 14px; line-height: 1.5; }
.hint code { background: var(--surface2); padding: 1px 5px; border-radius: 4px; font-size: 11px; }

/* ── Test result ── */
.test-row { display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; align-items: center; }
.test-result { font-size: 12px; padding: 6px 10px; border-radius: 6px; display: none; }
.test-result.ok { background: rgba(48,209,88,.12); color: var(--green); display: inline-block; }
.test-result.fail { background: rgba(255,69,58,.12); color: var(--red); display: inline-block; }

/* ── OAuth banner ── */
.oauth-banner { background: rgba(10,132,255,.1); border: 1px solid rgba(10,132,255,.25); border-radius: 10px; padding: 14px 18px; margin-top: 14px; font-size: 13px; line-height: 1.6; }
.oauth-banner a { color: var(--blue); text-decoration: none; }
.oauth-banner a:hover { text-decoration: underline; }

/* ── Site list ── */
.site-item { display: flex; align-items: center; justify-content: space-between; padding: 10px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 8px; font-size: 13px; }
.site-item:last-child { margin-bottom: 0; }

/* ── Session history cards ── */
.sh-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 10px;
  overflow: hidden;
  transition: border-color .15s;
}
.sh-card--active {
  border-color: rgba(48,209,88,.4);
  background: rgba(48,209,88,.03);
}
.sh-card-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; gap: 12px;
}
.sh-card-left  { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.sh-card-right { display: flex; align-items: center; gap: 8px; flex-shrink: 0; flex-wrap: wrap; justify-content: flex-end; }
.sh-card-date  { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .5px; }
.sh-card-time  { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
.sh-card-dur   { font-size: 13px; color: var(--text-muted); }
.sh-card-reserve { display: flex; align-items: center; gap: 4px; }
.sh-card-soc   { display: flex; align-items: center; gap: 4px; }
.sh-segments   { font-size: 11px; color: var(--text-muted); background: var(--surface2); padding: 1px 6px; border-radius: 5px; }
/* SOC pill */
.sh-soc-pill {
  font-size: 12px; font-weight: 600; padding: 2px 7px;
  border-radius: 6px; white-space: nowrap;
  background: rgba(10,132,255,.12); color: var(--blue, #0a84ff);
}
/* Reserve pills */
.sh-res-pill {
  font-size: 12px; font-weight: 600; padding: 2px 7px;
  border-radius: 6px; white-space: nowrap;
}
.sh-res-iog    { background: rgba(255,69,58,.12); color: var(--red); }
.sh-res-normal { background: rgba(48,209,88,.12); color: var(--green); }
.sh-res-pending { opacity: .5; }
.sh-res-arrow  { color: var(--text-muted); font-size: 12px; }
/* Status badge */
.sh-status {
  font-size: 11px; font-weight: 700; padding: 2px 8px;
  border-radius: 6px; display: inline-flex; align-items: center; gap: 4px; flex-shrink: 0;
}
.sh-status--active { background: rgba(48,209,88,.18); color: var(--green); }
.sh-status--done   { background: rgba(48,209,88,.10); color: var(--green); }
.sh-dot {
  width: 6px; height: 6px; border-radius: 50%; background: var(--green);
  animation: pulse 1.5s infinite; flex-shrink: 0;
}

/* ── Energy Flow – Tesla cross layout ── */
.ef-cross { position: relative; width: 100%; max-width: 320px; height: 300px; margin: 8px auto 0; }
.ef-svg   { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
/* inactive channels are invisible */
.ef-line  { fill: none; stroke: none; stroke-width: 2.5; stroke-linecap: round; }
/* active: solid coloured base line – no dasharray */
.ef-line.ef-yellow     { stroke: var(--yellow); }
.ef-line.ef-yellow-rev { stroke: var(--yellow); }
.ef-line.ef-white      { stroke: var(--text);   }
.ef-line.ef-white-rev  { stroke: var(--text);   }
.ef-line.ef-green      { stroke: var(--green);  }
.ef-line.ef-green-rev  { stroke: var(--green);  }
/* moving bright dot overlay – sits on top of the solid line */
.ef-dot { fill: none; stroke: none; stroke-width: 5; stroke-linecap: round; }
@keyframes ef-dot-fwd { from { stroke-dashoffset: 0; } to { stroke-dashoffset: -60; } }
@keyframes ef-dot-rev { from { stroke-dashoffset: 0; } to { stroke-dashoffset:  60; } }
.ef-dot.ef-yellow     { stroke: rgba(255,255,255,0.8); stroke-dasharray: 6 54; animation: ef-dot-fwd 0.9s linear infinite; }
.ef-dot.ef-yellow-rev { stroke: rgba(255,255,255,0.8); stroke-dasharray: 6 54; animation: ef-dot-rev 0.9s linear infinite; }
.ef-dot.ef-white      { stroke: rgba(255,255,255,0.5); stroke-dasharray: 6 54; animation: ef-dot-fwd 0.9s linear infinite; }
.ef-dot.ef-white-rev  { stroke: rgba(255,255,255,0.5); stroke-dasharray: 6 54; animation: ef-dot-rev 0.9s linear infinite; }
.ef-dot.ef-green      { stroke: rgba(255,255,255,0.8); stroke-dasharray: 6 54; animation: ef-dot-fwd 0.9s linear infinite; }
.ef-dot.ef-green-rev  { stroke: rgba(255,255,255,0.8); stroke-dasharray: 6 54; animation: ef-dot-rev 0.9s linear infinite; }
/* node positions */
.ef-node   { position: absolute; display: flex; flex-direction: column; align-items: center; gap: 2px; width: 72px; }
.ef-top    { top: 0;    left: 50%; transform: translateX(-50%); }
.ef-bottom { bottom: 0; left: 50%; transform: translateX(-50%); }
.ef-left   { left: 0;   top: 50%;  transform: translateY(-50%); }
.ef-right  { right: 0;  top: 50%;  transform: translateY(-50%); }
.ef-icon      { font-size: 26px; line-height: 1; }
.ef-icon-solar { color: var(--yellow); }
.ef-icon-grid  { color: var(--text); }
.ef-lbl   { font-size: 10px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); }
.ef-val   { font-size: 13px; font-weight: 600; }
.ef-sub   { font-size: 10px; color: var(--text-muted); }
/* ── Energy Flow tiles (legacy) ── */
.power-flow-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 8px; }
.power-flow-item { display: flex; flex-direction: column; align-items: center; background: var(--surface2); border-radius: 10px; padding: 14px 10px; gap: 4px; }
.power-flow-icon { font-size: 22px; line-height: 1; }
.power-flow-icon.solar { color: var(--yellow); }
.power-flow-icon.pf-grid { color: var(--text); }
.power-flow-icon.battery { color: var(--green); }
.power-flow-icon.home { color: var(--text); }
.power-flow-label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }
.power-flow-value { font-size: 16px; font-weight: 600; }

/* ── Warning banner ── */
#config-warning { background: rgba(255,214,10,.08); border: 1px solid rgba(255,214,10,.25); border-radius: var(--radius); color: var(--yellow); font-size: 14px; padding: 14px 18px; margin-bottom: 24px; display: none; }

/* ── Toast notification ── */
#notify { position: fixed; bottom: 24px; right: 24px; padding: 12px 18px; border-radius: 10px; font-size: 14px; font-weight: 500; opacity: 0; transform: translateY(8px); transition: opacity .25s, transform .25s; pointer-events: none; z-index: 999; max-width: 340px; }
#notify.show { opacity: 1; transform: translateY(0); }
#notify.ok { background: rgba(48,209,88,.2); color: var(--green); border: 1px solid rgba(48,209,88,.3); }
#notify.err { background: rgba(255,69,58,.2); color: var(--red); border: 1px solid rgba(255,69,58,.3); }

/* ── Nav link (settings button in header) ── */
.nav-link { font-size: 13px; color: var(--text-muted); text-decoration: none; padding: 6px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface2); white-space: nowrap; }
.nav-link:hover { color: var(--text); border-color: #444; }
.nav-link.nav-link--active { color: var(--text); border-color: #555; background: var(--surface2); }

/* ── Hamburger nav ── */
.nav-wrap { position: relative; display: flex; align-items: center; gap: 12px; }
.nav-toggle { display: none; background: var(--surface2); border: 1px solid var(--border); color: var(--text); border-radius: 8px; padding: 6px 14px; cursor: pointer; font-size: 15px; white-space: nowrap; }
.nav-links { display: flex; align-items: center; gap: 12px; }
@media (max-width: 600px) {
  .nav-toggle { display: inline-flex; align-items: center; }
  .nav-links {
    display: none;
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    flex-direction: column;
    align-items: stretch;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 8px;
    gap: 6px;
    z-index: 200;
    min-width: 170px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.5);
  }
  .nav-links.open { display: flex; }
  .nav-links .nav-link { text-align: center; }
}

/* ── Solar chart container ── */
.chart-container { position: relative; height: 210px; }
