:root {
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --accent: #10b981;
  --bg-main: #0f172a;
  --bg-card: #1e293b;
  --bg-elevated: #334155;
  --text-primary: #f1f5f9;
  --text-secondary: #cbd5e1;
  --text-muted: #94a3b8;
  --border: #334155;
  --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.5);
  --radius: 12px;
  --radius-lg: 16px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: linear-gradient(180deg, #111827 0%, #162234 100%);
  color: var(--text-primary);
  line-height: 1.6;
  min-height: 100vh;
}

.page {
  min-height: 100vh;
  display: flex;
  flex-direction: row;
  align-items: stretch;
}

.sidebar {
  width: 240px;
  min-height: 100vh;
  background: rgba(15, 23, 42, 0.98);
  border-right: 1px solid var(--border);
  padding: 1.5rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  position: sticky;
  top: 0;
  align-self: flex-start;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.4);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.sidebar-toggle {
  padding: 0.35rem 0.6rem;
}

.brand {
  font-size: 1.5rem;
  font-weight: 700;
  color: #dbe7f5;
}

.nav {
  display: flex;
  gap: 0.5rem;
  align-items: stretch;
  flex-direction: column;
}

.nav a {
  color: var(--text-secondary);
  text-decoration: none;
  padding: 0.6rem 0.9rem;
  border-radius: var(--radius);
  transition: background-color 0.12s ease, color 0.12s ease;
  font-weight: 500;
}

.nav a:hover {
  color: var(--text-primary);
  background: var(--bg-elevated);
}

.nav a[data-nav].active {
  background: var(--primary);
  color: white;
}

.nav-icon {
  display: inline-flex;
  width: 24px;
  justify-content: center;
}

.nav-text {
  white-space: nowrap;
}

.sidebar.collapsed {
  width: 72px;
  padding: 1rem 0.5rem;
  align-items: center;
}

.sidebar.collapsed .sidebar-header {
  justify-content: center;
}

.sidebar.collapsed .brand {
  display: none;
}

.sidebar.collapsed .nav {
  align-items: center;
}

.sidebar.collapsed .nav a {
  justify-content: center;
}

.sidebar.collapsed .nav-text {
  display: none;
}

.sidebar.collapsed [data-logout] .nav-text {
  display: none;
}

.sidebar-build {
  margin-top: auto;
  padding: 0.9rem;
  border-top: 1px solid rgba(148, 163, 184, 0.2);
}

.sidebar-build-product {
  color: var(--text-secondary);
  font-size: 0.8rem;
  font-weight: 600;
}

.sidebar-build-id {
  margin-top: 0.35rem;
  color: var(--text-muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 0.72rem;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.sidebar.collapsed .sidebar-build {
  display: none;
}

.btn {
  padding: 0.5rem 1.25rem;
  border-radius: var(--radius);
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease, border-color 0.12s ease, box-shadow 0.12s ease;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  border: 2px solid var(--border);
}

.btn.ghost:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.content {
  flex: 1;
  padding: 2rem;
  max-width: 1600px;
  width: 100%;
  margin: 0 auto;
}

/* ====== Stat cards ====== */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.stat {
  position: relative;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.1rem 1.2rem 0;
  box-shadow: var(--shadow-md);
  transition: border-color 0.15s, transform 0.15s;
  overflow: hidden;
  min-height: 168px;
  display: flex;
  flex-direction: column;
}
.stat:hover { border-color: rgba(96, 165, 250, 0.28); }
.stat::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  background: var(--accent-color, transparent);
}
.stat[data-stat="solar"]       { --accent-color: var(--flow-pv); }
.stat[data-stat="grid-limit"]  { --accent-color: #60a5fa; }
.stat[data-stat="grid"]        { --accent-color: var(--flow-grid); }
.stat[data-stat="grid"][data-state="import"] { --accent-color: var(--flow-import); }
.stat[data-stat="grid"][data-state="export"] { --accent-color: var(--flow-export); }
.stat[data-stat="battery"]     { --accent-color: var(--flow-battery); }
.stat[data-stat="consumption"] { --accent-color: var(--flow-load); }
.stat[data-stat="weather"]     { --accent-color: #6c8aa4; }

.stat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}
.stat-title {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
}
.stat-icon {
  width: 22px;
  height: 22px;
  color: var(--accent-color, var(--text-muted));
  opacity: 0.95;
}
.stat-icon svg { width: 100%; height: 100%; }
.icon-stroke { fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.stat-value {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-top: 0.65rem;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.stat-sub {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.25rem;
  font-size: 0.78rem;
  color: var(--text-muted);
}
.delta {
  display: inline-flex;
  align-items: center;
  gap: 0.18rem;
  padding: 0.08rem 0.4rem;
  border-radius: var(--radius-pill);
  font-weight: 600;
  font-size: 0.74rem;
}
.delta.up   { color: #5fdfa0; background: rgba(46, 196, 137, 0.1); }
.delta.down { color: #ff8a93; background: rgba(230, 57, 70, 0.1); }
.delta.neut { color: var(--text-muted); background: rgba(255,255,255,0.04); }

.stat-spark {
  margin-top: auto;
  margin-left: -1.2rem;
  margin-right: -1.2rem;
  height: 42px;
  width: calc(100% + 2.4rem);
  pointer-events: none;
}
.stat-spark svg {
  display: block;
  width: 100%;
  height: 100%;
}

/* Battery extra meta */
.battery-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.25rem 0.85rem;
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 0.5rem;
}
.battery-meta b {
  color: var(--text-primary);
  font-weight: 600;
  margin-left: 0.3rem;
  font-variant-numeric: tabular-nums;
}

/* Weather extras */
.weather-extras {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  margin-top: 0.4rem;
  padding-top: 0.5rem;
  border-top: 1px dashed var(--border-subtle);
  font-size: 0.78rem;
  color: var(--text-muted);
}
.weather-extras b {
  color: var(--text-primary);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* ====== Chart panel ====== */
.panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}
.panel-title { font-size: 1rem; font-weight: 700; letter-spacing: -0.01em; }
.panel-meta { color: var(--text-muted); font-size: 0.78rem; margin-top: 0.2rem; font-variant-numeric: tabular-nums; }

.range-bar { display: flex; align-items: center; gap: 0.65rem; }
.segmented {
  display: inline-flex;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-pill);
  padding: 3px;
}
.segmented button {
  border: 0;
  background: transparent;
  color: var(--text-secondary);
  padding: 0.32rem 0.85rem;
  font: 600 0.78rem var(--font-sans);
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.segmented button:hover { color: var(--text-primary); }
.segmented button.active { background: var(--primary); color: #fff; box-shadow: 0 1px 3px rgba(0,0,0,0.5); }
.reset-btn {
  background: transparent;
  border: 0;
  color: var(--text-muted);
  font: 500 0.78rem var(--font-sans);
  cursor: pointer;
  padding: 0.32rem 0.55rem;
  border-radius: var(--radius);
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}
.reset-btn:hover { color: var(--text-primary); background: rgba(255,255,255,0.04); }

/* Legend chips */
.legend { display: flex; flex-wrap: wrap; gap: 0.45rem; margin: 0.75rem 0 0.5rem; }
.legend-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.32rem 0.7rem;
  border-radius: var(--radius-pill);
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  color: var(--text-secondary);
  font: 500 0.8rem var(--font-sans);
  cursor: pointer;
  user-select: none;
  transition: border-color 0.12s, background 0.12s, color 0.12s;
}
.legend-chip:hover { color: var(--text-primary); border-color: rgba(96,165,250,0.35); }
.legend-chip input { position: absolute; opacity: 0; width: 0; height: 0; }
.legend-chip:has(input:not(:checked)) { opacity: 0.45; }
.legend-chip:has(input:not(:checked)) .swatch-row { filter: grayscale(0.9); }
.swatch-row { display: inline-flex; align-items: center; gap: 3px; }
.swatch { width: 12px; height: 3px; border-radius: 2px; background: var(--c, #fff); }
.swatch.dashed { background: repeating-linear-gradient(to right, var(--c, #fff) 0 4px, transparent 4px 7px); }

/* ====== Chart (uPlot) ====== */
.chart-container {
  min-height: 410px;
  margin-top: 1rem;
  position: relative;
  overflow: hidden;
  min-width: 0;
}

.uplot-host { width: 100%; height: 380px; min-width: 0; }
.uplot { font-family: inherit; max-width: 100%; }
.uplot,
.uplot .u-title,
.uplot .u-label,
.uplot .u-value { color: var(--text-secondary); }
.uplot .u-legend {
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-secondary);
}
.uplot .u-select { background: rgba(59, 130, 246, 0.18); }

.chart-tooltip {
  position: absolute;
  z-index: 20;
  pointer-events: none;
  min-width: 180px;
  max-width: min(320px, calc(100% - 16px));
  padding: 0.75rem 0.9rem;
  border-radius: 12px;
  border: 1px solid rgba(148, 163, 184, 0.28);
  background: rgba(15, 23, 42, 0.96);
  box-shadow: 0 14px 30px rgba(0, 0, 0, 0.45);
  color: var(--text-primary);
  opacity: 0;
  transform: translate3d(0, 0, 0);
  transition: opacity 0.12s ease;
}
.chart-tooltip.visible { opacity: 1; }
.chart-tooltip-time { font-size: 0.78rem; color: var(--text-muted); margin-bottom: 0.5rem; }
.chart-tooltip-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.25rem 0.75rem;
  font-size: 0.83rem;
}
.chart-tooltip-label { display: inline-flex; align-items: center; gap: 0.45rem; color: var(--text-secondary); }
.chart-tooltip-swatch { width: 0.6rem; height: 0.6rem; border-radius: 999px; flex: 0 0 auto; }
.chart-tooltip-value { color: var(--text-primary); font-weight: 600; text-align: right; }

canvas { max-width: 100%; }

/* ====== Footer ====== */
.footer {
  background: rgba(30, 41, 59, 0.6);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--border);
  padding: 1.25rem 2rem;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: 1.5rem;
}
.footer-brand { color: var(--text-primary); font-weight: 600; }
.footer-build {
  margin-top: 0.35rem;
  color: var(--text-muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 0.8rem;
  overflow-wrap: anywhere;
}

body.pv-layout-pending [data-pv-ui],
body.no-pv-site [data-pv-ui] {
  display: none !important;
}
body:not(.no-pv-site) [data-no-pv-ui],
body.pv-layout-pending [data-no-pv-ui] {
  display: none !important;
}

@media (max-width: 1280px) {
  .stats-grid { grid-template-columns: repeat(3, 1fr); }
}
@media (max-width: 768px) {
  .page { flex-direction: column; }
  .sidebar {
    width: 100%;
    min-height: auto;
    position: relative;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
  .nav {
    flex-direction: row;
    align-items: center;
    flex-wrap: wrap;
    justify-content: flex-end;
  }
  .sidebar.collapsed { width: 100%; padding: 1rem; }
  .sidebar.collapsed .brand { display: block; }
  .sidebar.collapsed .nav-text { display: inline; }
  .content { padding: 1rem; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .topology-wrap { min-height: auto; }
}
