:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #1b1f24;
  --muted: #667085;
  --border: #e3e6ea;
  --accent: #0b6bcb;
  --accent-strong: #0b6bcb;
  --cheap: #15803d;
  --mid: #b45309;
  --pricey: #b91c1c;
  --none: #98a2b3;
  --chart-station: #0b6bcb;
  --chart-market: #737d8c;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216;
    --surface: #171b21;
    --text: #e6e9ee;
    --muted: #98a2b3;
    --border: #2a3039;
    --accent: #4ea1ff;
    --accent-strong: #1d64b8;
    --cheap: #4ade80;
    --mid: #fbbf24;
    --pricey: #f87171;
    --none: #667085;
    --chart-station: #3d8fe6;
    --chart-market: #646c7b;
  }
}

* { box-sizing: border-box; }
html, body { height: 100dvh; margin: 0; }
html, body { overflow-x: hidden; }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}
a { color: var(--accent); }

.topbar {
  display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
  /* iPhone-Notch/Dynamic Island: Inhalt nicht darunter verstecken (PWA im Vollbild) */
  padding-top: max(10px, env(safe-area-inset-top));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}
.brand { font-weight: 700; font-size: 18px; }
.search { display: flex; gap: 6px; flex: 1 1 320px; min-width: 0; }
.search input {
  flex: 1; min-width: 0; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text); font: inherit;
  /* mind. 16px, sonst zoomt iOS beim Antippen automatisch rein und bleibt verzoomt hängen */
  font-size: 16px;
}
button, select {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 12px; cursor: pointer;
}
button:hover { border-color: var(--accent); }
.fuel-tabs { display: flex; gap: 4px; }
.fuel-tabs button.active { background: var(--accent-strong); border-color: var(--accent-strong); color: #fff; }

.layout { flex: 1; display: grid; grid-template-columns: 1fr 400px; min-height: 0; }
.map { min-height: 300px; }
.panel {
  display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); border-left: 1px solid var(--border);
}
.panel-head {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
}
.summary { color: var(--muted); font-size: 14px; }
.sort { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.sort select { padding: 4px 8px; font-size: 16px; }

.list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.list li {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.list li:hover, .list li.selected { background: var(--bg); }
.st-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.st-addr, .st-meta { color: var(--muted); font-size: 13px; }
.st-price { grid-row: span 2; text-align: right; font-variant-numeric: tabular-nums; font-weight: 700; font-size: 20px; align-self: center; }
.st-price small { display: block; font-size: 11px; font-weight: 400; color: var(--muted); }
.st-others { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }
.badge {
  display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 6px; font-size: 11px;
  border: 1px solid var(--border); color: var(--text);
}
.st-others .badge:first-child { margin-left: 0; }
.badge.hvo { border-color: var(--cheap); color: var(--cheap); font-weight: 600; }
.popup-prices i { color: #667085; font-style: normal; }
.closed { color: var(--pricey); }
.c-cheap { color: var(--cheap); } .c-mid { color: var(--mid); } .c-pricey { color: var(--pricey); } .c-none { color: var(--none); }

.notice {
  padding: 10px 16px; font-size: 13px; border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--mid) 14%, var(--surface)); color: var(--text);
}
.me-dot {
  display: block; width: 20px; height: 20px; border-radius: 50%; cursor: grab;
  background: #0b6bcb; border: 3px solid #fff; box-shadow: 0 0 0 2px #0b6bcb, 0 1px 6px rgba(0,0,0,.4);
}
.note {
  padding: 10px 16px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--border);
  /* iPhone-Home-Indikator: Text nicht dahinter verstecken */
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}
.empty, .error { padding: 24px 16px; color: var(--muted); text-align: center; }
.error { color: var(--pricey); }

/* Preis-Marker */
.price-pin {
  background: var(--surface); color: var(--text); border: 2px solid var(--none);
  border-radius: 8px; padding: 1px 6px; font-weight: 700; font-size: 12px; white-space: nowrap;
  box-shadow: 0 1px 4px rgba(0,0,0,.3); font-variant-numeric: tabular-nums; transform: translate(-50%, -100%);
  display: inline-block;
}
.price-pin.cheap { border-color: var(--cheap); } .price-pin.mid { border-color: var(--mid); }
.price-pin.pricey { border-color: var(--pricey); } .price-pin.na { color: var(--muted); font-weight: 400; }
.price-pin.selected { outline: 3px solid var(--accent); }
.price-pin.cluster { transform: translate(-50%, -50%); }
.price-pin.cluster em {
  font-style: normal; font-weight: 600; font-size: 10px; margin-left: 5px;
  background: var(--border); color: var(--text); border-radius: 6px; padding: 0 4px;
}
.leaflet-div-icon { background: none; border: none; }
.leaflet-popup-content { font-size: 14px; }
.popup-prices { margin: 6px 0 0; padding: 0; list-style: none; }
.popup-prices li { display: flex; justify-content: space-between; gap: 16px; }

/* Verlauf & Prognose */
.detail-overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .45);
  display: flex; align-items: center; justify-content: center; z-index: 1000; padding: 16px;
}
.detail-overlay[hidden] { display: none; }
.detail-card {
  background: var(--surface); border-radius: 14px; max-width: 600px; width: 100%;
  max-height: 88dvh; overflow-y: auto; overscroll-behavior: contain;
  padding: 20px 24px; position: relative; box-shadow: 0 12px 40px rgba(0, 0, 0, .35);
}
.detail-close {
  position: absolute; top: 10px; right: 10px; border: none; background: var(--bg);
  width: 36px; height: 36px; padding: 0; border-radius: 50%; font-size: 15px; color: var(--muted);
}
.detail-close:hover { color: var(--text); border-color: transparent; }
.sheet-head h2 { margin: 0 44px 2px 0; font-size: 19px; line-height: 1.25; }
.detail-addr { margin: 0 0 12px; color: var(--muted); font-size: 13px; }
.detail-loading, .detail-empty { color: var(--muted); padding: 16px 0; }
.detail-content.is-loading { opacity: .45; pointer-events: none; transition: opacity .15s; }
.detail-disclaimer { margin: 12px 0 0; font-size: 11px; color: var(--muted); }

/* Segment-Schalter (Sorte, Zeitraum) */
.seg { display: flex; gap: 2px; padding: 3px; background: var(--bg); border-radius: 10px; margin-bottom: 12px; }
.seg button {
  flex: 1; border: none; background: none; border-radius: 8px; padding: 8px 6px; min-height: 36px;
  font-size: 14px; color: var(--muted); font-weight: 500;
}
.seg button[aria-selected="true"] { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.seg-sm { margin-bottom: 0; }
.seg-sm button { padding: 5px 6px; min-height: 32px; font-size: 13px; }

/* Kennzahlen */
.stats { display: grid; grid-template-columns: 1fr auto auto; gap: 4px 18px; align-items: end; margin-bottom: 14px; }
.stat { display: flex; flex-direction: column; min-width: 0; }
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 30px; font-weight: 700; line-height: 1.1; }
.stat-value.sm { font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums; }
.stat-delta { font-size: 12px; margin-top: 2px; }
.stat-delta.good { color: var(--cheap); }
.stat-delta.bad { color: var(--pricey); }

/* Diagramm */
.chart-card { border: 1px solid var(--border); border-radius: 12px; padding: 10px 10px 8px; }
.chart-top { margin-bottom: 8px; }
.readout { display: flex; align-items: baseline; gap: 8px; min-height: 24px; padding: 0 4px; flex-wrap: wrap; }
.readout b { font-size: 20px; font-variant-numeric: tabular-nums; }
.readout span { font-size: 12px; color: var(--muted); }
.chart-wrap { width: 100%; touch-action: pan-y; user-select: none; -webkit-user-select: none; }
.chart { display: block; overflow: visible; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.chart .price-line { fill: none; stroke: var(--chart-station); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .price-area { fill: var(--chart-station); opacity: .1; }
.chart .market-line { fill: none; stroke: var(--chart-market); stroke-width: 2; stroke-linejoin: round; }
.chart .forecast-line { stroke: var(--chart-station); stroke-width: 2; stroke-dasharray: 5 4; stroke-linecap: round; }
.chart .now-line { stroke: var(--border); stroke-width: 1; }
.chart .dot { fill: var(--chart-station); stroke: var(--surface); stroke-width: 2; }
.chart .dot-forecast { fill: var(--surface); stroke: var(--chart-station); stroke-width: 2; }
.chart .cursor-line { stroke: var(--muted); stroke-width: 1; }
.chart .hit { fill: transparent; cursor: crosshair; outline: none; }
.chart .hit:focus-visible { stroke: var(--chart-station); stroke-width: 1; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--muted); padding: 6px 4px 0; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.key { display: inline-block; width: 16px; height: 0; border-top: 2px solid; }
.key-station { border-color: var(--chart-station); }
.key-market { border-color: var(--chart-market); }
.key-forecast { border-color: var(--chart-station); border-top-style: dashed; }

/* Prognose */
.forecast { margin-top: 12px; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border); }
.forecast-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; font-size: 15px; }
.forecast-row b { font-size: 20px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.forecast-meta { margin-top: 4px; font-size: 12px; color: var(--muted); }
.forecast small.up, .changes .up { color: var(--pricey); }
.forecast small.down, .changes .down { color: var(--cheap); }
.forecast-medium { border-color: var(--accent); }
.forecast-none { color: var(--muted); }

/* Tabelle aller Änderungen */
.changes { margin-top: 12px; font-size: 13px; }
.changes summary { cursor: pointer; color: var(--accent); padding: 6px 0; }
.changes table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.changes th { text-align: left; font-weight: 500; color: var(--muted); font-size: 12px; padding: 4px 0; }
.changes td { padding: 6px 0; border-top: 1px solid var(--border); }
.changes th:not(:first-child), .changes td:not(:first-child) { text-align: right; }

/* Handy: Bottom-Sheet über die volle Breite, Daumen-erreichbar */
@media (max-width: 600px) {
  .detail-overlay { align-items: flex-end; padding: 0; }
  .detail-card {
    max-width: none; border-radius: 18px 18px 0 0; max-height: 92dvh;
    padding: 22px 16px max(16px, env(safe-area-inset-bottom));
    animation: sheet-up .22s ease-out;
  }
  .detail-card::before {
    content: ""; position: absolute; top: 7px; left: 50%; width: 38px; height: 4px;
    margin-left: -19px; border-radius: 2px; background: var(--border);
  }
  .stat-value { font-size: 26px; }
  .chart-card { padding: 10px 6px 8px; margin: 0 -6px; }
  .stats { gap: 4px 14px; }
}
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .detail-card { animation: none; } }
body.sheet-open { overflow: hidden; }

@media (max-width: 800px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 45vh 1fr; }
  .panel { border-left: 0; border-top: 1px solid var(--border); }
  .topbar { padding: 8px 16px; }
  .search { flex-basis: 100%; }
  .fuel-tabs { width: 100%; }
  .fuel-tabs button { flex: 1; }
}
