:root { color-scheme: light; --ink: #172e42; --muted: #617285; --line: #e0e7ed; --green: #18744c; --amber: #99620b; --red: #b3323a; }
* { box-sizing: border-box; }
body { margin: 0; background: #f2f5f8; color: var(--ink); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; line-height: 1.5; }
.page-header { background: #12283b; color: #fff; padding: 36px 24px 40px; }
.header-inner, .container { max-width: 1080px; margin: 0 auto; }
.header-inner { display: flex; align-items: center; justify-content: space-between; gap: 24px; }
.eyebrow { color: #94bdce; font-size: 11px; font-weight: 700; letter-spacing: .18em; margin: 0 0 6px; }
h1 { font-size: clamp(26px, 4vw, 34px); letter-spacing: -.035em; line-height: 1.2; margin: 0; }
.header-description { color: #b7c8d5; font-size: 14px; margin: 10px 0 0; }
.connection-panel { display: flex; flex-direction: column; align-items: flex-end; gap: 9px; font-size: 12px; color: #b7c8d5; }
.connection-badge { display: inline-flex; align-items: center; gap: 8px; padding: 6px 12px; border-radius: 24px; background: #263d50; color: #e0e9f0; font-weight: 600; font-size: 12px; }
.connection-badge::before { content: ""; width: 7px; height: 7px; background: currentColor; border-radius: 50%; }
.connection-badge.connected { color: #98e6c0; background: #1b443e; }
.connection-badge.disconnected { color: #ffd6a3; background: #4a3c2d; }
.connection-badge.error { color: #ffc0c6; background: #513039; }
.container { padding: 28px 24px 0; }
.section-heading { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin-bottom: 18px; }
h2 { font-size: 18px; font-weight: 650; letter-spacing: -.025em; margin: 0; }
.status-indicator { display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; }
.dot { width: 9px; height: 9px; background: #8a98a5; border-radius: 50%; flex-shrink: 0; }
.data-notice { background: #e8eef4; color: #475e73; border-radius: 8px; padding: 10px 14px; font-size: 13px; margin: 0 0 16px; }
[hidden] { display: none !important; }
.overview-grid { display: grid; grid-template-columns: 1fr 1.3fr; gap: 18px; }
.tile { background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 24px; min-width: 0; box-shadow: 0 3px 12px #172e4203; }
.tile-label { font-size: 13px; font-weight: 500; color: var(--muted); margin: 0 0 10px; }
.tile-value { font-size: clamp(25px, 3vw, 34px); font-weight: 650; line-height: 1.25; letter-spacing: -.04em; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.unit { font-size: .48em; color: var(--muted); font-weight: 500; letter-spacing: 0; white-space: nowrap; }
.charge-card { border-top: 3px solid #238263; }
.charge-value { font-size: 52px; }
.charge-track { background: #e8f0ec; border-radius: 8px; height: 10px; overflow: hidden; margin-top: 20px; }
.charge-fill { background: #8a98a5; width: 0; height: 100%; border-radius: inherit; transition: width .4s ease, background-color .2s ease; }
.charge-fill.bg-blue { background: #3384c1; }
.charge-fill.bg-green { background: #238263; }
.charge-fill.bg-orange { background: #c88215; }
.charge-fill.bg-red { background: #c13944; }
.tile-note { color: var(--muted); font-size: 12px; margin: 12px 0 0; }
.metric-delta { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 4px 10px; width: 100%; margin-top: 12px; font-size: 12px; color: var(--muted); }
.metric-delta strong { color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.charge-card .metric-delta strong { margin-left: auto; max-width: 100%; white-space: normal; text-align: right; }
.time-estimate { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; border-top: 1px solid var(--line); margin-top: 16px; padding-top: 12px; }
.estimate-label { font-size: 12px; color: var(--muted); }
.estimate-value { margin-left: auto; font-size: 17px; font-variant-numeric: tabular-nums; }
.estimate-note { flex-basis: 100%; font-size: 11px; color: var(--muted); }
.flow-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.flow-grid .tile { display: flex; flex-direction: column; justify-content: center; }
.support-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; margin-top: 18px; }
.compact-tile { padding: 18px 22px; }
.compact-tile .tile-value { font-size: 25px; }
.compact-tile .tile-label { margin-bottom: 6px; }
.mains-control { margin-top: 24px; }
.mains-control .section-heading { margin-bottom: 8px; }
.control-button { min-height: 44px; padding: 9px 14px; border: 1px solid #bccbd7; border-radius: 8px; background: #f5f8fa; color: var(--ink); font: inherit; font-size: 13px; font-weight: 600; cursor: pointer; }
.control-button:hover:enabled { background: #e5eef5; }
.control-button.danger { color: var(--red); border-color: #e5bfc3; }
.control-button:disabled { opacity: .45; cursor: not-allowed; }
.control-button:focus-visible, #bypassPin:focus-visible { outline: 3px solid #3287bd; outline-offset: 2px; }
.mains-actions { display: flex; flex-wrap: wrap; gap: 10px; margin: 18px 0 12px; }
.command-result { font-size: 13px; margin: 16px 0; }
.gateway-footer { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; color: var(--muted); font-size: 12px; }
#bypassPinDialog { width: min(420px, calc(100% - 32px)); padding: 24px; border: 1px solid var(--line); border-radius: 14px; color: var(--ink); box-shadow: 0 16px 60px #12283b40; }
#bypassPinDialog::backdrop { background: #12283b99; }
.pin-label { display: block; font-size: 13px; margin: 20px 0 6px; }
#bypassPin { width: 100%; min-height: 48px; padding: 10px 12px; font: inherit; font-size: 20px; letter-spacing: .3em; border: 1px solid #bccbd7; border-radius: 8px; }
.pin-error { min-height: 20px; margin: 6px 0; color: var(--red); font-size: 13px; }
#bypassPinDialog .mains-actions { justify-content: flex-end; }
.battery-section { margin-top: 34px; }
.section-description { margin: 4px 0 0; font-size: 13px; color: var(--muted); }
.section-count { font-size: 12px; color: var(--muted); white-space: nowrap; }
.battery-grid { display: grid; grid-template-columns: 1fr 1fr; align-items: start; gap: 18px; }
.battery-card { background: #fff; border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
summary { padding: 22px; cursor: pointer; list-style: none; }
summary::-webkit-details-marker { display: none; }
summary:hover { background: #f9fbfc; }
summary:focus-visible { outline: 3px solid #3287bd; outline-offset: -4px; border-radius: 12px; }
.battery-heading { display: flex; align-items: center; gap: 12px; }
.battery-id { display: flex; width: 38px; height: 38px; align-items: center; justify-content: center; background: #edf3f7; color: #527083; font-size: 13px; font-weight: 700; border-radius: 10px; }
.battery-title { display: block; font-size: 15px; font-weight: 650; }
.battery-role { display: block; font-size: 12px; color: var(--muted); }
.expand-icon { margin-left: auto; font-size: 23px; color: var(--muted); transition: transform .2s ease; }
details[open] .expand-icon { transform: rotate(45deg); }
.battery-summary { display: grid; grid-template-columns: .8fr 1fr 1.1fr; gap: 10px; margin-top: 20px; }
.summary-label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.summary-value { display: block; font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.details-content { padding: 0 22px 22px; }
.detail-group { border-top: 1px solid var(--line); padding-top: 16px; margin-top: 8px; }
.detail-group h3 { font-size: 10px; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); margin: 0 0 8px; }
.detail-row { display: flex; justify-content: space-between; align-items: baseline; gap: 16px; padding: 8px 0; font-size: 13px; }
.detail-label { color: var(--muted); }
.detail-val { font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }
.state-pill { display: inline-block; font-size: 10px; font-weight: 650; letter-spacing: .03em; background: #edf2f5; padding: 3px 8px; border-radius: 5px; }
.state-pill.text-green { background: #e8f5ee; }
.state-pill.text-red { background: #fbecee; }
.empty-state { font-size: 13px; color: var(--muted); }
.text-green { color: var(--green); }
.text-red { color: var(--red); }
.bg-blue { background-color: #3384c1; }
.bg-green { background-color: #238263; }
.bg-orange { background-color: #c88215; }
.bg-red { background-color: #c13944; }
footer { font-size: 11px; color: var(--muted); padding: 28px 0; display: flex; gap: 8px; }
.history-section { margin-top: 32px; }
.history-range { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); white-space: nowrap; }
.history-range select { min-height: 44px; border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; background: #fff; color: var(--ink); font: inherit; cursor: pointer; }
.history-range select:focus-visible { outline: 3px solid #3287bd; outline-offset: 2px; }
.history-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.chart-card { min-width: 0; padding: 20px; border: 1px solid var(--line); border-radius: 14px; background: #fff; }
.chart-card h3 { font-size: 15px; margin: 0 0 4px; }
.chart-description, .history-note { font-size: 11px; color: var(--muted); margin: 0; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 8px 16px; margin-top: 16px; font-size: 11px; color: var(--muted); }
.chart-legend > span { display: inline-flex; align-items: center; gap: 6px; }
.chart-legend > span::before { content: ""; width: 14px; height: 3px; border-radius: 2px; }
.legend-average::before { background: #172e42; }
.legend-bms1::before { background: #238263; }
.legend-bms2::before { background: #8059c7; }
.legend-charging::before { background: #238263; }
.legend-discharging::before { background: #c13944; }
.legend-neutral::before { background: #8a98a5; }
.history-chart { display: block; width: 100%; height: 230px; margin-top: 8px; overflow: hidden; touch-action: pan-y; }
.chart-reading { min-height: 36px; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; margin: 8px 0 0; }
.history-note { margin-top: 10px; }
/* Mobile and tablet overrides; desktop rules above stay unchanged. */
@media (max-width: 760px) {
  .history-section { margin-top: 24px; }
  .history-grid { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .chart-card { padding: 16px; border-radius: 12px; }
  .history-chart { height: 210px; }
  .history-section > .section-heading { flex-wrap: wrap; }
  .page-header { padding: 22px 20px; }
  .header-inner { align-items: flex-start; gap: 16px; }
  .header-description { margin-top: 6px; font-size: 12px; }
  .connection-panel { max-width: 46%; font-size: 11px; }
  #lastReceived { overflow-wrap: anywhere; }
  .container { padding: 20px 18px 0; }
  .section-heading { margin-bottom: 12px; gap: 12px; }
  h2 { font-size: 17px; }
  .status-indicator { font-size: 12px; text-align: right; }
  .data-notice { font-size: 12px; padding: 10px 12px; margin-bottom: 12px; }
  .overview-grid { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .tile { border-radius: 12px; padding: 18px; }
  .charge-card { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; column-gap: 16px; row-gap: 8px; }
  .charge-card .tile-label { margin: 0; }
  .charge-card .charge-value { grid-column: 2; grid-row: 1 / 3; font-size: 42px; white-space: nowrap; }
  .charge-card .tile-note { grid-column: 1; grid-row: 2; margin: 0; font-size: 11px; }
  .charge-track { grid-column: 1 / -1; margin-top: 4px; height: 8px; }
  .time-estimate { grid-column: 1 / -1; margin-top: 4px; padding-top: 10px; }
  .charge-card .metric-delta { grid-column: 1 / -1; margin-top: 2px; }
  .flow-grid .metric-delta { font-size: 11px; margin-top: 10px; }
  .flow-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .flow-grid .tile { padding: 16px 18px; }
  .flow-grid .tile-label { margin-bottom: 6px; }
  .flow-grid .tile-note { display: none; }
  .flow-grid .tile-value { font-size: clamp(24px, 5vw, 32px); }
  .support-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin-top: 12px; }
  .compact-tile { padding: 14px 12px; }
  .compact-tile .tile-label { font-size: 11px; margin-bottom: 6px; }
  .compact-tile .tile-value { font-size: 21px; }
  .battery-section { margin-top: 24px; }
  .section-description { font-size: 12px; }
  .section-count { display: none; }
  .battery-grid { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .battery-card { border-radius: 12px; }
  summary { padding: 16px; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
  .battery-heading { gap: 10px; min-height: 44px; }
  .battery-id { width: 34px; height: 34px; flex-shrink: 0; }
  .expand-icon { display: flex; align-items: center; justify-content: center; width: 44px; height: 44px; flex-shrink: 0; }
  .battery-summary { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
  .battery-summary > span { min-width: 0; }
  .summary-value { font-size: 16px; }
  .details-content { padding: 0 16px 16px; }
  .detail-group { padding-top: 12px; }
  .detail-group h3 { font-size: 11px; }
  .detail-row { padding: 10px 0; gap: 12px; font-size: 14px; }
  .detail-label, .detail-val { min-width: 0; overflow-wrap: anywhere; }
  .state-pill { font-size: 11px; }
  footer { flex-wrap: wrap; padding: 20px 0 max(20px, env(safe-area-inset-bottom)); }
}
@media (max-width: 480px) {
  .page-header { padding: max(18px, env(safe-area-inset-top)) 16px 16px; }
  .header-inner { flex-direction: column; gap: 14px; }
  .eyebrow { font-size: 10px; margin-bottom: 4px; }
  h1 { font-size: 26px; }
  .header-description { display: none; }
  .connection-panel { flex-direction: row; align-items: center; justify-content: space-between; max-width: none; width: 100%; gap: 12px; }
  .connection-badge { padding: 5px 9px; font-size: 11px; flex-shrink: 0; }
  #lastReceived { text-align: right; font-size: 10px; max-width: 57%; }
  .container { padding: 18px 12px 0; }
  .section-heading { flex-wrap: wrap; }
  .status-indicator { margin-left: auto; }
  .charge-card { padding: 14px 16px; column-gap: 10px; }
  .charge-card .charge-value { font-size: 38px; }
  .flow-grid { gap: 10px; }
  .flow-grid .tile { padding: 14px; }
  .support-grid { gap: 8px; margin-top: 10px; }
  .compact-tile { padding: 12px 10px; }
  .compact-tile .tile-value { font-size: 20px; }
  .unit { font-size: .55em; }
}
@media (max-width: 359px) {
  .charge-card .charge-value { font-size: 32px; }
  .charge-card .tile-note { max-width: 115px; }
  .support-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .support-grid .compact-tile:last-child { grid-column: 1 / -1; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
  .support-grid .compact-tile:last-child .tile-label { margin: 0; }
  .summary-value { font-size: 14px; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { transition: none !important; } }

.raw-readings { display: grid; grid-template-columns: minmax(7rem, 1fr) minmax(0, 2fr); gap: .5rem 1rem; margin: 0; }
.raw-readings dt { font-weight: 600; }
.raw-readings dd { margin: 0; overflow-wrap: anywhere; }

.analysis-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.analysis-grid .chart-card { min-width: 0; }
.analysis-controls { display: flex; flex-wrap: wrap; gap: .5rem 1rem; margin-block: .75rem; }
.analysis-controls label { display: grid; gap: .25rem; font-size: .85rem; }
.analysis-controls select, .analysis-controls input { font: inherit; color: inherit; border: 1px solid #cad6df; border-radius: .35rem; padding: .35rem; background: #fff; min-height: 2.25rem; }
.analysis-controls button { align-self: end; }
.analysis-controls[hidden] { display: none; }
.analysis-plot { display: block; width: 100%; height: 230px; }
.readings-card { grid-column: 1 / -1; }
.readings-card .analysis-table th, .readings-card .analysis-table td { text-align: left; vertical-align: top; }
.readings-card .analysis-table td:last-child { white-space: normal; width: 100%; }
.readings-card .analysis-table summary { margin: 0; padding: 0; }
.readings-card pre { white-space: pre-wrap; overflow-wrap: anywhere; }
.energy-totals { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .5rem; margin-block: .75rem; }
.energy-totals > div { background: #f2f6f9; border-radius: .35rem; padding: .5rem .65rem; }
.energy-totals dt { color: var(--muted); font-size: .75rem; }
.energy-totals dd { margin: .15rem 0 0; font-weight: 650; font-variant-numeric: tabular-nums; }
.analysis-table { overflow: auto; max-height: 20rem; }
.analysis-table table { border-collapse: collapse; width: 100%; font-size: .85rem; }
.analysis-table th, .analysis-table td { text-align: right; white-space: nowrap; padding: .4rem; border-bottom: 1px solid #dce6ed; }
.analysis-table th:first-child, .analysis-table td:first-child { text-align: left; }
.analysis-grid summary { cursor: pointer; margin-top: .8rem; }
.energy-scroll { overflow-x: auto; }
.energy-plot { display: grid; grid-template-columns: repeat(var(--energy-days, 7), minmax(2.5rem, 1fr)); gap: .2rem; height: 194px; }
.energy-day { display: grid; grid-template-rows: 1fr auto; gap: .25rem; text-align: center; font-size: .7rem; }
.energy-bars { display: flex; align-items: end; justify-content: center; gap: .15rem; border-bottom: 1px solid #cad6df; }
.energy-bar { width: 32%; min-width: .25rem; border-radius: .2rem .2rem 0 0; background: #238263; }
.energy-bar.negative { background: #c15a36; }
.energy-day.no-data .energy-bars { background: repeating-linear-gradient(135deg, transparent 0 5px, #edf1f5 5px 8px); }
.analysis-grid .section-description { min-height: 2.4em; }
@media (max-width: 820px) { .analysis-grid { grid-template-columns: minmax(0, 1fr); } }
