:root {
  color-scheme: light;
  --bg: #f4f6f3;
  --surface: #ffffff;
  --surface-2: #eef4f0;
  --text: #16211d;
  --muted: #65736d;
  --line: #dce5df;
  --pv: #f5b642;
  --house: #247c8f;
  --battery: #6f63d9;
  --good: #26864b;
  --shadow: 0 18px 40px rgba(32, 50, 43, 0.1);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.dashboard {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: 28px 0 42px;
}

.topbar,
.panel-heading,
.legend,
.chart-actions,
.day-controls,
.averages {
  display: flex;
  align-items: center;
}

.topbar {
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 22px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(2rem, 5vw, 4rem);
  line-height: 1;
}

h2 {
  font-size: 1.15rem;
}

.status {
  min-width: 180px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.9rem;
  text-align: center;
}

.status.ok {
  color: var(--good);
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 14px;
  margin-bottom: 14px;
}

.metric,
.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.metric[data-view] {
  cursor: pointer;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.metric[data-view]:hover {
  border-color: var(--pv);
  box-shadow: 0 18px 40px rgba(32, 50, 43, 0.16);
}

.metric[data-view].active {
  border-color: var(--pv);
  box-shadow: 0 0 0 2px var(--pv), var(--shadow);
}

#batteryCard.active {
  border-color: var(--battery);
  box-shadow: 0 0 0 2px var(--battery), var(--shadow);
}

#batteryCard:hover {
  border-color: var(--battery);
}

.view-hidden {
  display: none;
}

.metric {
  min-height: 138px;
  padding: 18px;
  display: grid;
  align-content: space-between;
}

.metric.primary {
  background: #20322b;
  color: #fff;
}

.metric span,
.metric small,
.averages span,
.bar-label {
  color: var(--muted);
  font-size: 0.86rem;
}

.metric.primary span,
.metric.primary small {
  color: rgba(255, 255, 255, 0.72);
}

.metric small a {
  color: inherit;
  font-weight: 700;
}

.metric strong {
  display: block;
  margin: 8px 0;
  font-size: clamp(1.55rem, 3vw, 2.3rem);
  line-height: 1.05;
  letter-spacing: 0;
  overflow-wrap: anywhere;
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.65fr) minmax(320px, 0.8fr);
  gap: 14px;
}

.panel {
  min-width: 0;
  padding: 18px;
}

.chart-panel {
  min-height: 470px;
}

.battery-panel {
  min-height: 360px;
}

.panel-heading {
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.legend {
  gap: 14px;
  color: var(--muted);
  font-size: 0.86rem;
}

.chart-actions {
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 12px;
}

.day-controls {
  gap: 6px;
}

.day-controls button,
.day-controls input,
.text-link {
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.day-controls button {
  width: 36px;
  padding: 0;
  cursor: pointer;
  font-size: 1.35rem;
  line-height: 1;
}

.day-controls input {
  width: 148px;
  padding: 0 9px;
}

.text-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 12px;
  color: var(--text);
  font-weight: 700;
  text-decoration: none;
}

.legend i {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 6px;
  border-radius: 50%;
}

.legend .pv {
  background: var(--pv);
}

.legend .house {
  background: var(--house);
}

.legend .battery {
  background: var(--battery);
}

.chart-wrap {
  position: relative;
}

canvas {
  display: block;
  width: 100%;
  height: 380px;
}

#batteryChart {
  height: 270px;
}

#meterChart {
  height: 300px;
}

.chart-tooltip {
  position: absolute;
  z-index: 4;
  min-width: 172px;
  padding: 10px 12px;
  border: 1px solid rgba(22, 33, 29, 0.14);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.97);
  box-shadow: 0 14px 30px rgba(32, 50, 43, 0.16);
  color: var(--text);
  pointer-events: none;
  transform: translate(-50%, calc(-100% - 12px));
}

.tooltip-title {
  margin-bottom: 7px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}

.tooltip-row {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  font-size: 0.9rem;
}

.tooltip-row + .tooltip-row {
  margin-top: 4px;
}

.tooltip-row strong {
  font-size: 0.95rem;
}

.bars {
  display: grid;
  gap: 12px;
}

.bar-row {
  display: grid;
  grid-template-columns: 86px 1fr 72px;
  align-items: center;
  gap: 10px;
}

.bar-track {
  height: 16px;
  overflow: hidden;
  border-radius: 8px;
  background: var(--surface-2);
}

.bar-fill {
  height: 100%;
  min-width: 2px;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--good), var(--pv));
}

.bar-value {
  font-weight: 750;
  text-align: right;
}

.averages {
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
  gap: 12px;
}

.averages > div {
  flex: 1;
  padding: 14px;
  border-radius: 8px;
  background: var(--surface-2);
}

.averages strong {
  display: block;
  margin-top: 6px;
  font-size: 1.3rem;
}

.panel-note {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 0.82rem;
}

.history-panel {
  margin-top: 14px;
}

.history-filters {
  display: flex;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 10px;
}

.history-filters label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.history-filters input,
.history-filters button {
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.history-filters input {
  width: 150px;
  padding: 0 9px;
}

.history-filters button {
  padding: 0 14px;
  background: #20322b;
  color: #fff;
  cursor: pointer;
  font-weight: 750;
}

.table-wrap {
  overflow-x: auto;
}

.meter-table-wrap {
  margin-top: 18px;
}

.history-table {
  width: 100%;
  min-width: 620px;
  border-collapse: collapse;
}

.history-table th,
.history-table td {
  padding: 13px 12px;
  border-bottom: 1px solid var(--line);
  text-align: right;
  white-space: nowrap;
}

.history-table th:first-child,
.history-table td:first-child {
  text-align: left;
}

.history-table th {
  color: var(--muted);
  font-size: 0.78rem;
  letter-spacing: 0;
  text-transform: uppercase;
}

.history-table tbody tr:hover {
  background: var(--surface-2);
}

@media (max-width: 900px) {
  .metric-grid,
  .content-grid {
    grid-template-columns: 1fr 1fr;
  }

  .chart-panel {
    grid-column: 1 / -1;
  }
}

@media (max-width: 640px) {
  .dashboard {
    width: min(100% - 20px, 1180px);
    padding-top: 18px;
  }

  .topbar,
  .panel-heading,
  .chart-actions,
  .averages {
    align-items: flex-start;
    flex-direction: column;
  }

  .status {
    width: 100%;
  }

  .metric-grid,
  .content-grid {
    grid-template-columns: 1fr;
  }

  canvas {
    height: 300px;
  }

  .day-controls {
    width: 100%;
  }

  .day-controls input {
    flex: 1;
    min-width: 0;
  }

  .bar-row {
    grid-template-columns: 74px 1fr 66px;
  }

  .history-filters,
  .history-filters label,
  .history-filters input,
  .history-filters button {
    width: 100%;
  }
}
