:root {
  --bg: #111418;
  --housing: #1c2026;
  --housing-edge: #2a3038;
  --text: #e8ebef;
  --muted: #8a929c;
  --red: #ff3b30;
  --yellow: #ffcc00;
  --green: #34c759;
  --off: #2b2f35;
  --hist-h: 24vh;
}

/* Als Farbe registriert, damit der Glow beim Ampelwechsel weich überblendet */
@property --tint { syntax: '<color>'; inherits: true; initial-value: transparent; }
@property --edge { syntax: '<color>'; inherits: true; initial-value: transparent; }
@property --wash { syntax: '<color>'; inherits: true; initial-value: transparent; }
@property --accent { syntax: '<color>'; inherits: true; initial-value: #3a414b; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
}
html { background: var(--bg); }

body {
  display: grid;
  grid-template-rows: 1fr auto;
  height: 100dvh;
  transition: --tint 0.6s ease, --edge 0.6s ease, --wash 0.6s ease, --accent 0.6s ease;
}

/* Die ganze App färbt sich ein: flächige Grundtönung + kräftiger Farbschein + leuchtende Ränder */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background:
    radial-gradient(ellipse 95% 85% at 50% 40%, var(--tint) 0%, transparent 80%),
    linear-gradient(var(--wash), var(--wash));
  box-shadow: inset 0 0 28vmin var(--edge);
}
body[data-level="green"]  { --tint: rgba(52, 199, 89, 0.5);  --edge: rgba(52, 199, 89, 0.75); --wash: rgba(52, 199, 89, 0.16); --accent: var(--green); }
body[data-level="yellow"] { --tint: rgba(255, 204, 0, 0.45); --edge: rgba(255, 204, 0, 0.75); --wash: rgba(255, 204, 0, 0.15); --accent: var(--yellow); }
body[data-level="red"]    { --tint: rgba(255, 59, 48, 0.6);  --edge: rgba(255, 59, 48, 0.9);  --wash: rgba(255, 59, 48, 0.22); --accent: var(--red); }
body[data-state="inactive"], body[data-state="error"] {
  --tint: transparent; --edge: transparent; --wash: transparent; --accent: #3a414b;
}


body.no-history { --hist-h: 0px; }
body.no-history #history { display: none; }

/* ---------- Ampel ---------- */
#stage {
  position: relative;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 16px 8px;
}

/* Lampendurchmesser D; Gehäuse = 1.24·D breit, 3.48·D lang (Padding + Abstände je 0.12·D) */
.housing {
  --d: min((100dvh - var(--hist-h) - 56px) / 3.48, 90vw / 1.24);
  display: flex;
  flex-direction: column;
  gap: calc(var(--d) * 0.12);
  padding: calc(var(--d) * 0.12);
  border-radius: calc(var(--d) * 0.62);
  background: linear-gradient(180deg, var(--housing-edge), var(--housing));
  box-shadow: 0 2vh 6vh rgba(0, 0, 0, 0.6), inset 0 0 0 2px rgba(255, 255, 255, 0.04);
}

/* Querformat: Ampel liegt quer (Rot rechts) */
@media (orientation: landscape) {
  .housing {
    --d: min(90vw / 3.48, (100dvh - var(--hist-h) - 56px) / 1.24);
    flex-direction: row-reverse;
  }
}

.lamp {
  --c: var(--off);
  position: relative;
  overflow: hidden;
  width: var(--d);
  height: var(--d);
  flex: none;
  border-radius: 50%;
  background: var(--off);
  box-shadow: inset 0 0.8vh 2vh rgba(0, 0, 0, 0.55);
  transition: background 0.3s ease, box-shadow 0.3s ease, filter 0.3s ease;
  display: grid;
  place-items: center;
}
.lamp.red    { --c: var(--red);    --hi: #ff8a80; }
.lamp.yellow { --c: var(--yellow); --hi: #fff1a8; }
.lamp.green  { --c: var(--green);  --hi: #a6f0b8; }

/* Füllstand: wie weit der Pegel innerhalb des Ampelbereichs liegt */
.fill {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--fill, 0%);
  background: linear-gradient(180deg, var(--hi), var(--c) 40%);
  box-shadow: 0 -1px 0 rgba(255, 255, 255, 0.35);
  transition: height 0.15s linear;
  display: none;
}

/* aktive Lampe: leicht gedämpfte Grundfarbe + heller Füllstand + Glow.
   Kontrast bewusst gering, damit die ganze Lampe klar als "an" lesbar bleibt. */
body[data-level="red"] .lamp.red,
body[data-level="yellow"] .lamp.yellow,
body[data-level="green"] .lamp.green {
  background: color-mix(in srgb, var(--c) 78%, #000);
  box-shadow: 0 0 calc(var(--d) * 0.35) var(--c), inset 0 -1vh 3vh rgba(0, 0, 0, 0.2);
}
body[data-level="red"] .lamp.red .fill,
body[data-level="yellow"] .lamp.yellow .fill,
body[data-level="green"] .lamp.green .fill {
  display: block;
}

/* numerischer Pegel außerhalb der Ampel, unten rechts im Ampelbereich */
#readout {
  position: absolute;
  right: max(16px, env(safe-area-inset-right));
  bottom: 8px;
  font-size: clamp(22px, 4vmin, 40px);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  padding: 4px 12px;
  background: rgba(17, 20, 24, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
}
#readout small { font-size: 0.5em; font-weight: 500; color: var(--muted); }

#offLabel {
  position: absolute;
  margin: 0;
  font-size: clamp(28px, 8vmin, 80px);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text);
  background: rgba(17, 20, 24, 0.85);
  padding: 0.3em 0.8em;
  border-radius: 0.4em;
  display: none;
}
body[data-state="inactive"] #offLabel,
body[data-state="error"] #offLabel { display: block; }
body[data-state="error"] #offLabel { font-size: clamp(18px, 4vmin, 36px); text-transform: none; letter-spacing: 0; cursor: pointer; }
body[data-state="inactive"] .lamp { filter: grayscale(1) brightness(0.6); }

/* ---------- Verlauf ---------- */
#history {
  height: var(--hist-h);
  min-height: 120px;
  display: flex;
  flex-direction: column;
  padding: 0 max(16px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.history-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 4px;
}
.history-head small { margin-left: 6px; }
.ranges { display: flex; gap: 4px; }
.ranges button {
  min-height: 28px;
  padding: 3px 10px;
  font-size: 12px;
  border-radius: 7px;
}
.ranges button.on { background: #2f6fed; border-color: #2f6fed; } /* wird unten durch Akzentfarbe ersetzt */

.chart-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  background: rgba(17, 20, 24, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
}
#chart { position: absolute; inset: 0; width: 100%; height: 100%; cursor: crosshair; }
#tooltip {
  position: absolute;
  top: 6px;
  pointer-events: none;
  background: rgba(28, 32, 38, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  transform: translateX(-50%);
}

/* ---------- Bedienelemente ---------- */
#controls {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  display: flex;
  gap: 8px;
  opacity: 0.4;
  transition: opacity 0.2s;
}
#controls:hover, #controls:focus-within { opacity: 1; }

button {
  font: inherit;
  color: var(--text);
  background: rgba(40, 45, 52, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  padding: 10px 14px;
  cursor: pointer;
  min-height: 44px;
}
button:hover { background: rgba(60, 66, 75, 0.95); }
button.primary { background: #2f6fed; border-color: #2f6fed; }
button.primary:hover { background: #4a82f0; }
body[data-state="inactive"] #toggleBtn { background: #2f6fed; border-color: #2f6fed; }

/* ---------- Dialog ---------- */
.card {
  background: #1c2026;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 16px;
  padding: 24px;
  max-width: 440px;
  width: 100%;
}
.card h1, .card h2 { margin: 0 0 12px; }
.card p { color: var(--muted); line-height: 1.45; }
.error { color: var(--red) !important; }

dialog {
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text);
  width: min(440px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
dialog label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 10px 0;
}
dialog input {
  width: 80px;
  margin-left: auto;
  font: inherit;
  color: var(--text);
  background: #111418;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  padding: 6px 8px;
}
dialog label[hidden] { display: none; }
dialog select {
  flex: 1;
  min-width: 0;
  max-width: 260px;
  margin-left: auto;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: #111418;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 8px;
  padding: 6px 8px;
}
.hint { font-size: 13px; }
.row { display: flex; justify-content: space-between; gap: 8px; margin-top: 16px; flex-wrap: wrap; }

/* ---------- Akzentfarbe = aktuelle Ampelfarbe (am Ende, damit sie gewinnt) ---------- */
.housing { outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 4px; }
#controls button, #readout, .chart-wrap { border-color: color-mix(in srgb, var(--accent) 60%, transparent); }
#readout #dbValue { color: color-mix(in srgb, var(--accent) 35%, var(--text)); }
.ranges button.on {
  background: color-mix(in srgb, var(--accent) 35%, #1c2026);
  border-color: var(--accent);
}

/* ---------- Kalibrierungs-Wizard ---------- */
#calib h2 { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
#calib .step-no { font-size: 13px; font-weight: 500; color: var(--muted); }
#calib section[hidden], #calib [hidden] { display: none; }
#calib section p { margin: 10px 0; }
#calib b { color: var(--text); }
.cal-live {
  font-size: 22px;
  font-variant-numeric: tabular-nums;
  text-align: center;
  padding: 8px;
  margin: 12px 0 0;
  border-radius: 10px;
  background: #111418;
}
.row .spacer { flex: 1; }
button:disabled { opacity: 0.45; cursor: not-allowed; }
