:root {
  --bg: #f5f6f8;
  --card-bg: #ffffff;
  --text: #1b1f23;
  --muted: #6b7280;
  --border: #e2e5ea;
  --accent: #2563eb;
  --accent-soft: #eef2ff;
  --tile-bg: #f2f3f5;
  --computed-accent: #6b7280;
  --danger: #dc2626;
  --home: #9ca3af;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --card-bg: #1d2025;
    --text: #e8eaed;
    --muted: #9aa1ab;
    --border: #2c3036;
    --accent: #5b8cff;
    --accent-soft: #1e2740;
    --tile-bg: #24262b;
    --computed-accent: #9aa1ab;
    --danger: #f87171;
    --home: #6b7280;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 24px 24px 80px;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.45;
}

header { max-width: 1100px; margin: 0 auto 20px; }
header h1 { margin: 0 0 4px; font-size: 1.6rem; }
header .sub { margin: 0; color: var(--muted); font-size: 0.92rem; }
.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.title-block { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.header-row h1 { margin: 0; }
.name-meaning { font-size: 0.8rem; color: var(--muted); font-style: italic; }
.lang-select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card-bg);
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
}

main, #upload-section { max-width: 1100px; margin: 0 auto; }

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 22px;
  margin-bottom: 20px;
}

.card h2 { margin: 0 0 12px; font-size: 1.1rem; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 0 0 14px; }

.file-drop {
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 36px 20px;
  cursor: pointer;
  color: var(--muted);
  font-size: 0.95rem;
  transition: border-color 0.15s, background 0.15s;
}
.file-drop:hover, .file-drop.dragover {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.file-drop input { display: none; }
.error { color: var(--danger); font-size: 0.88rem; margin: 10px 0 0; }

.summary-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.88rem;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.toggle input { width: 16px; height: 16px; cursor: pointer; }

.summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 14px;
  margin-top: 16px;
}

.stat {
  background: var(--tile-bg);
  border-radius: 8px;
  padding: 14px 16px;
}
.calc-badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--computed-accent);
  cursor: help;
  margin-left: 2px;
}
.stat .label { font-size: 0.78rem; color: var(--muted); margin-bottom: 4px; }
.stat .value { font-size: 1.35rem; font-weight: 600; }
.stat .value small { font-size: 0.9rem; font-weight: 500; color: var(--muted); }

#map {
  height: 460px;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.provider-pricing {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.provider-row {
  display: grid;
  grid-template-columns: 1fr 140px auto;
  gap: 10px;
  align-items: center;
  padding-left: 12px;
  border-left: 4px solid transparent;
}
.provider-row .name { font-weight: 500; }
.provider-row .meta { color: var(--muted); font-size: 0.82rem; }
.provider-row input {
  width: 100%;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
}
.provider-row button {
  padding: 7px 12px;
  border-radius: 6px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: white;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
}
.provider-row button:hover { opacity: 0.9; }

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

table { width: 100%; border-collapse: collapse; font-size: 0.87rem; min-width: 560px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.02em; }
tbody tr:hover { background: var(--accent-soft); }
tr.day-total-row td { font-weight: 600; background: var(--tile-bg); }
tr.day-total-row { cursor: pointer; }
tr.day-total-row:hover td { background: var(--accent-soft); }
.trip-chevron { display: inline-block; width: 1em; color: var(--muted); font-weight: 400; }

td input[type="number"] {
  width: 80px;
  padding: 5px 7px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.87rem;
}
td input[type="text"] {
  width: 150px;
  padding: 5px 7px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.87rem;
}
td input[type="number"].kwh-input { width: 70px; }
.gps-link { color: var(--accent); text-decoration: none; white-space: nowrap; }
.gps-link:hover { text-decoration: underline; }

td.ignore-cell { text-align: center; }
tr.ignored-row { opacity: 0.6; }
tr.ignored-row.hidden-row { display: none; }
.warn-badge { cursor: help; }
td select {
  padding: 5px 7px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.87rem;
}
.provider-select { min-width: 150px; }
.provider-custom-input { margin-top: 6px; }
.provider-cell { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

.subheading { margin: 20px 0 10px; font-size: 0.95rem; }

.daily-chart-row {
  display: flex;
  gap: 28px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.chart-stack {
  display: flex;
  flex-direction: column;
  gap: 26px;
}
.daily-chart-block { flex: 1 1 240px; min-width: 200px; }
.daily-chart-heading { font-size: 0.8rem; color: var(--muted); font-weight: 600; margin: 0 0 8px; }
.bar-chart {
  display: flex;
  align-items: stretch;
  height: 110px;
  gap: 6px;
}
.bar-chart-tall { height: 140px; gap: 4px; }
.bar-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  cursor: pointer;
}
.bar-fill {
  width: 100%;
  min-height: 2px;
  border-radius: 4px 4px 0 0;
  transition: opacity 0.1s;
}
.bar-col:hover .bar-fill { opacity: 0.75; }
.bar-col-day-start { border-left: 1px dashed var(--border); padding-left: 4px; margin-left: 3px; }
.bar-labels {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}
.bar-label {
  flex: 1;
  text-align: center;
  font-size: 0.66rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
}
.bar-label-day-start { font-weight: 600; color: var(--text); white-space: normal; overflow: visible; line-height: 1.3; }

.line-chart-svg { width: 100%; height: 130px; display: block; overflow: visible; }
.hover-capture { cursor: crosshair; }
.hover-indicator { pointer-events: none; }

.chart-legend-inline { display: flex; gap: 16px; margin-bottom: 8px; flex-wrap: wrap; }
.legend-row-inline { display: inline-flex; align-items: center; gap: 6px; font-size: 0.76rem; color: var(--muted); }
.legend-row-inline .legend-swatch { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.legend-row-inline .legend-swatch-line { border-radius: 2px; height: 3px; }
.combo-chart-wrap { position: relative; }
.combo-line-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
.combo-marker { pointer-events: none; }
.line-chart-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 4px;
  font-size: 0.66rem;
  color: var(--muted);
}

.provider-chart { margin-bottom: 18px; }
.chart-layout {
  display: flex;
  gap: 28px;
  align-items: center;
  flex-wrap: wrap;
}
.chart-svg { flex-shrink: 0; }
.chart-legend {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 200px;
  flex: 1;
}
.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.86rem;
}
.legend-swatch {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
}
.legend-name { flex: 1; }
.legend-rate { color: var(--muted); white-space: nowrap; font-size: 0.8rem; }
.legend-value { color: var(--muted); white-space: nowrap; }
.legend-row:hover { color: var(--text); cursor: default; }

.donut-slice { cursor: pointer; transition: opacity 0.1s; }
.donut-slice:hover { opacity: 1 !important; }

.chart-tooltip {
  position: fixed;
  pointer-events: none;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 0.82rem;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.18);
  z-index: 1000;
  max-width: 220px;
}
.chart-tooltip .tt-name { font-weight: 600; margin-bottom: 2px; }
.chart-tooltip .tt-row { color: var(--muted); }
.fuel-inputs {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.fuel-inputs label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.82rem;
  color: var(--muted);
}
.fuel-inputs input {
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
  width: 140px;
}

.kwh-marker { background: transparent; border: none; }
.leaflet-popup-content-wrapper { border-radius: 8px; }
.popup-title { font-weight: 600; margin-bottom: 4px; }
.popup-row { font-size: 0.85rem; margin: 2px 0; }
.popup-ignore-btn {
  margin-top: 8px;
  padding: 5px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  cursor: pointer;
  font-size: 0.8rem;
}

.toolbar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 20px;
}
.toolbar button {
  padding: 9px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--text);
  font-size: 0.88rem;
  cursor: pointer;
}
.toolbar button:hover { background: var(--accent-soft); border-color: var(--accent); }
.toolbar input[type="file"] { display: none; }

.stat-group { margin-bottom: 18px; }
.stat-group:last-child { margin-bottom: 0; }
.stat-group-heading {
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--muted);
  margin: 0 0 10px;
}

.site-footer {
  max-width: 1100px;
  margin: 30px auto 0;
  padding: 16px 4px 0;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.6;
}
.site-footer p { margin: 2px 0; }
.site-footer a { color: var(--accent); text-decoration: none; }
.site-footer a:hover { text-decoration: underline; }

@media (max-width: 640px) {
  body { padding: 14px 14px 60px; }
  .card { padding: 16px; }
  .header-row { flex-wrap: wrap; }
  header h1 { font-size: 1.35rem; }
  .provider-row {
    grid-template-columns: 1fr;
    align-items: stretch;
  }
  .provider-row button { width: 100%; }
  .fuel-inputs label, .fuel-inputs input { width: 100%; }
  .chart-layout { justify-content: center; }
  .toolbar button { flex: 1 1 auto; text-align: center; }
  #map { height: 320px; }
}

/* ---------- Druckansicht (so viele A4-Seiten wie nötig, natürlicher Seitenumbruch) ---------- */

.print-only { display: none; }

@media print {
  @page { size: A4; margin: 14mm; }

  body * { visibility: hidden; }
  .print-only, .print-only * { visibility: visible; }
  .print-only {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    color: #111;
    background: #fff;
  }

  /* Bewusst KEINE page-break-before/-after-Regeln: erzwungene Umbrüche sind die häufigste
     Ursache für leere Seiten am Druck-Ende. Der Inhalt fließt natürlich über so viele Seiten
     wie nötig; nur kleine Einheiten (einzelne Kachel/Zeile) bekommen break-inside:avoid,
     nie ganze Tabellen oder Abschnitte, damit nichts unnötig auf eine neue Seite springt. */

  .print-only h1 { font-size: 20pt; margin: 0 0 1mm; }
  .print-meta { font-size: 9pt; color: #444; margin: 0 0 5mm; }
  .print-only h2 {
    font-size: 12pt;
    margin: 6mm 0 2mm;
    border-bottom: 0.5pt solid #999;
    padding-bottom: 1mm;
    break-after: avoid;
  }
  .print-subheading {
    font-size: 9.5pt;
    color: #555;
    margin: 3mm 0 1.5mm;
    break-after: avoid;
  }
  .print-hint { font-size: 8.5pt; color: #666; margin: 0 0 3mm; }
  .print-stats {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 2.5mm;
    margin-bottom: 3mm;
  }
  .print-stat {
    border: 0.5pt solid #ccc;
    border-radius: 1.5mm;
    padding: 1.5mm 2.5mm;
    break-inside: avoid;
  }
  .print-stat-label { display: block; font-size: 7pt; color: #666; }
  .print-stat-value { display: block; font-size: 10.5pt; font-weight: 700; }
  /* table-layout:fixed statt auto: erzwingt, dass sich Spalten die verfügbare Breite teilen
     und Inhalte notfalls umbrechen, statt die Tabelle über den Papierrand hinaus zu schieben
     (mit auto ragte die 9-spaltige Ladevorgänge-Tabelle nachweislich über die Seitenbreite). */
  .print-table { width: 100%; table-layout: fixed; border-collapse: collapse; font-size: 8.5pt; margin-bottom: 4mm; }
  .print-table-wide { font-size: 7.3pt; }
  .print-table th, .print-table td {
    border-bottom: 0.5pt solid #ddd;
    padding: 0.8mm 1.5mm;
    text-align: left;
    white-space: normal; /* überschreibt die globale th,td{white-space:nowrap}-Regel der Live-Tabellen */
    overflow-wrap: break-word;
    word-break: break-word;
  }
  .print-table th { color: #555; font-weight: 600; }
  .print-table tr { break-inside: avoid; }
  .print-table thead { display: table-header-group; } /* Spaltenköpfe wiederholen sich auf Folgeseiten */
  .print-total-row td { font-weight: 700; background: #f3f3f3; }
  .print-ignored-row td { color: #999; }
  .print-footer { font-size: 7pt; color: #888; margin-top: 5mm; }
}
