:root {
  --bg: #0f172a;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --muted-strong: #cbd5e1;
  --panel: #1e293b;
  --panel-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
  --subpanel: #162033;
  --tab-bg: #1e293b;
  --tab-text: #ffffff;
  --tab-active-bg: #38bdf8;
  --tab-active-text: #0f172a;
  --tab-active-shadow: 0 10px 24px rgba(56, 189, 248, 0.35);
  --stat-surface: linear-gradient(145deg, rgba(56, 189, 248, 0.12), rgba(15, 23, 42, 0.96));
  --stat-border: rgba(148, 163, 184, 0.18);
  --stat-shadow: 0 18px 36px rgba(2, 6, 23, 0.35);
  --stat-value: #f8fafc;
  --unit-bg: rgba(15, 23, 42, 0.65);
  --unit-border: rgba(148, 163, 184, 0.14);
  --grid-line: rgba(148, 163, 184, 0.15);
  --select-bg: #0f172a;
  --select-border: rgba(148, 163, 184, 0.25);
}

body[data-theme="light"] {
  --bg: #eef4fb;
  --text: #10213a;
  --muted: #5f738d;
  --muted-strong: #42556f;
  --panel: #ffffff;
  --panel-shadow: 0 12px 28px rgba(15, 23, 42, 0.08);
  --subpanel: #f4f8fc;
  --tab-bg: #dbe7f3;
  --tab-text: #18324f;
  --tab-active-bg: #0ea5e9;
  --tab-active-text: #ffffff;
  --tab-active-shadow: 0 12px 24px rgba(14, 165, 233, 0.22);
  --stat-surface: linear-gradient(145deg, rgba(255, 255, 255, 1), rgba(228, 238, 249, 0.96));
  --stat-border: rgba(148, 163, 184, 0.25);
  --stat-shadow: 0 14px 32px rgba(15, 23, 42, 0.1);
  --stat-value: #0f172a;
  --unit-bg: rgba(255, 255, 255, 0.82);
  --unit-border: rgba(148, 163, 184, 0.2);
  --grid-line: rgba(148, 163, 184, 0.2);
  --select-bg: #ffffff;
  --select-border: rgba(148, 163, 184, 0.35);
}

body {
  font-family: Arial;
  background: var(--bg);
  color: var(--text);
  margin: 0;
  padding: 10px;
  transition: background 0.25s ease, color 0.25s ease;
}

h2 {
  text-align: center;
  margin-bottom: 8px;
}

.header {
  position: relative;
  padding-top: 6px;
  margin-bottom: 10px;
}

.topbar {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
}

.tabs {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.tabs button {
  padding: 10px;
  margin: 0;
  border: none;
  background: var(--tab-bg);
  color: var(--tab-text);
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease;
}

.tabs button.active {
  background: var(--tab-active-bg);
  color: var(--tab-active-text);
  box-shadow: var(--tab-active-shadow);
  transform: translateY(-1px);
  font-weight: 700;
}

.tab-status {
  text-align: center;
  color: var(--muted);
  font-size: 0.95rem;
  margin: 0 0 12px;
}

.theme-toggle {
  position: absolute;
  top: 0;
  right: 0;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 5px 9px;
  border-radius: 999px;
  background: var(--panel);
  box-shadow: var(--panel-shadow);
  color: var(--text);
}

.theme-toggle span {
  font-size: 0.76rem;
  font-weight: 700;
  color: var(--muted-strong);
}

.theme-toggle input {
  display: none;
}

.theme-toggle-slider {
  position: relative;
  width: 40px;
  height: 22px;
  border-radius: 999px;
  background: var(--tab-bg);
  border: 1px solid var(--select-border);
  transition: background 0.2s ease, border-color 0.2s ease;
}

.theme-toggle-slider::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: linear-gradient(145deg, #ffffff, #dbeafe);
  box-shadow: 0 4px 10px rgba(15, 23, 42, 0.18);
  transition: transform 0.2s ease;
}

.theme-toggle input:checked + .theme-toggle-slider {
  background: var(--tab-active-bg);
  border-color: transparent;
}

.theme-toggle input:checked + .theme-toggle-slider::after {
  transform: translateX(18px);
  background: linear-gradient(145deg, #ffffff, #fef3c7);
}

.card {
  background: var(--panel);
  padding: 15px;
  border-radius: 12px;
  margin-bottom: 15px;
  box-shadow: var(--panel-shadow);
  transition: background 0.25s ease, box-shadow 0.25s ease;
}

.chart-shell {
  position: relative;
  width: 100%;
  height: 340px;
  margin-top: 12px;
}

.live-chart-grid {
  display: grid;
  gap: 12px;
  margin-top: 12px;
}

.live-chart-shell {
  position: relative;
  width: 100%;
  height: 180px;
  background: var(--subpanel);
  border-radius: 10px;
  padding: 10px;
  box-sizing: border-box;
  transition: background 0.25s ease;
}

canvas {
  display: block;
}

select {
  margin-right: 10px;
  background: var(--select-bg);
  color: var(--text);
  border: 1px solid var(--select-border);
  border-radius: 10px;
  padding: 8px 10px;
}

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

.stat {
  --accent: #38bdf8;
  position: relative;
  overflow: hidden;
  padding: 18px 20px;
  border-radius: 18px;
  text-align: left;
  background: var(--stat-surface);
  border: 1px solid var(--stat-border);
  box-shadow: var(--stat-shadow);
  transition: background 0.25s ease, border-color 0.25s ease, box-shadow 0.25s ease;
}

.stat-voltage {
  --accent: #38bdf8;
}

.stat-current {
  --accent: #f97316;
}

.stat-power {
  --accent: #22c55e;
}

.stat-energy {
  --accent: #eab308;
}

.stat-frequency {
  --accent: #a855f7;
}

.stat-pf {
  --accent: #ef4444;
}

.stat::before {
  content: "";
  position: absolute;
  inset: 0 auto auto 0;
  width: 100%;
  height: 4px;
  background: var(--accent, #38bdf8);
  opacity: 0.95;
}

.stat::after {
  content: "";
  position: absolute;
  top: -24px;
  right: -12px;
  width: 96px;
  height: 96px;
  border-radius: 50%;
  background: radial-gradient(circle, color-mix(in srgb, var(--accent, #38bdf8) 28%, transparent), transparent 68%);
  pointer-events: none;
}

.stat-label {
  display: block;
  font-size: 0.88rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 8px;
}

.stat-value {
  display: block;
  font-size: clamp(1.6rem, 2vw, 2.2rem);
  font-weight: 800;
  line-height: 1;
  color: var(--stat-value);
}

.stat-unit {
  display: inline-block;
  margin-top: 10px;
  padding: 5px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--accent, #38bdf8);
  background: var(--unit-bg);
  border: 1px solid var(--unit-border);
}

.chart-note {
  color: var(--muted);
  font-size: 0.95rem;
  margin: 0;
}

.chart-title {
  margin: 0 0 8px;
  font-size: 0.95rem;
  color: var(--muted-strong);
}

@media (max-width: 680px) {
  .header {
    padding-top: 40px;
  }

  .topbar {
    flex-direction: column;
  }

  .theme-toggle {
    right: 50%;
    transform: translateX(50%);
  }
}
