:root {
  --blue: #1565c0;
  --blue-dark: #0d2c6b;
  --green: #3d6b35;
  --bg: #fff;
  --panel: #fafbfd;
  --text: #1c2733;
  --muted: #5c6b7a;
  --line: #dde4ec;
  --box: #eef4fb;
  --boxLine: #c9dcf2;
  --warnBox: #fdf3e4;
  --warnLine: #f0d9ae;
  --okBox: #edf7ec;
  --okLine: #c6e3c4;
  --shadow: 0 2px 14px rgba(10, 30, 60, .18);
  --radius: 12px;
  --topbar-h: 52px;
  --kartenFilter: none;
  font-size: 15px;
}

/* Dunkelmodus: eigene Palette, Karte wird nur leicht abgedunkelt, damit sie
   lesbar bleibt – harte Invertierung macht Gewässer und Wald unkenntlich. */
:root[data-theme="dark"] {
  --blue: #5aa2e8;
  --blue-dark: #a9cdf3;
  --green: #8fc07d;
  --bg: #171c22;
  --panel: #1e252d;
  --text: #e6ecf2;
  --muted: #9aa8b6;
  --line: #303a45;
  --box: #1b2734;
  --boxLine: #2c4257;
  --warnBox: #33291a;
  --warnLine: #574421;
  --okBox: #1b2a1c;
  --okLine: #2f4a31;
  --shadow: 0 2px 16px rgba(0, 0, 0, .55);
  --kartenFilter: brightness(0.78) saturate(0.85) contrast(1.05);
  color-scheme: dark;
}

* { box-sizing: border-box; }
/* Muss vor allen ID-Regeln mit display gewinnen, sonst bleiben versteckte Panels sichtbar. */
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font-family: system-ui, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

h4 { margin: 14px 0 6px; font-size: .95rem; }
button { font: inherit; cursor: pointer; }

/* ---------- Kopfleiste ---------- */
#topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--topbar-h);
  display: flex; align-items: center; gap: 8px;
  padding: 0 10px calc(0px + env(safe-area-inset-top, 0px));
  padding-top: env(safe-area-inset-top, 0px);
  background: var(--bg); border-bottom: 1px solid var(--line);
  z-index: 1200;
}
#logo { display: flex; align-items: center; gap: 6px; font-weight: 700; letter-spacing: .5px; color: var(--green); }
#logo span { font-size: .95rem; }
#searchForm { position: relative; flex: 1; display: flex; max-width: 420px; }
#searchInput {
  flex: 1; min-width: 0; padding: 7px 10px; border: 1px solid var(--line);
  border-radius: 8px 0 0 8px; font: inherit; background: var(--panel);
}
#searchForm button {
  border: 1px solid var(--line); border-left: 0; border-radius: 0 8px 8px 0;
  background: var(--panel); padding: 0 10px;
}
/* Kennzahlen des gewählten Damms in der Kopfzeile – hier ist Platz für die
   vollen Werte, in der schmalen Leiste der Ergebnisansicht wurden sie
   abgeschnitten. Leer, solange kein Damm gewählt ist. */
#sheetStatus {
  font-size: .8rem; color: var(--muted); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; margin-left: auto; min-width: 0;
}
#sheetStatus:empty { display: none; }
#searchResults {
  position: absolute; top: 100%; left: 0; right: 0; margin-top: 4px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: var(--shadow); z-index: 1300; overflow: hidden;
}
#searchResults button {
  display: block; width: 100%; text-align: left; padding: 8px 10px;
  border: 0; background: none; border-bottom: 1px solid var(--line);
  font-size: .85rem;
}
#searchResults button:last-child { border-bottom: 0; }
#searchResults button:hover { background: var(--panel); }
#searchResults button.aktiv { background: var(--box); box-shadow: inset 3px 0 0 var(--blue); }
.iconbtn { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 6px 9px; flex: none; }
/* Schmale Geräte: Schriftzug weicht, damit Sprache und Hilfe erreichbar bleiben */
@media (max-width: 480px) {
  #logo span { display: none; }
  #topbar { gap: 6px; padding: 0 8px; }
  #searchInput { padding: 7px 8px; }
}

/* ---------- Karte ---------- */
#map { position: fixed; inset: var(--topbar-h) 0 0 0; z-index: 1; }
.leaflet-container { font: inherit; background: var(--panel); }
/* Nur die Kartenkacheln abdunkeln – Stauflächen und Dämme bleiben unverfälscht */
.leaflet-tile-pane { filter: var(--kartenFilter); }
:root[data-theme="dark"] .leaflet-control-attribution,
:root[data-theme="dark"] .leaflet-bar a {
  background: var(--panel); color: var(--text);
}
:root[data-theme="dark"] .leaflet-bar a { border-bottom-color: var(--line); }
:root[data-theme="dark"] .leaflet-popup-content-wrapper,
:root[data-theme="dark"] .leaflet-popup-tip,
:root[data-theme="dark"] .leaflet-tooltip {
  background: var(--panel); color: var(--text); border-color: var(--line);
}

/* ---------- Schwebeknöpfe ---------- */
#fabs {
  position: fixed; right: 12px; bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  display: flex; flex-direction: column; gap: 10px; z-index: 1100;
  transition: bottom .25s;
}
body.sheet-open #fabs { bottom: calc(50vh + 16px); }
.fab {
  border: 0; border-radius: 24px; padding: 12px 16px; background: var(--bg);
  box-shadow: var(--shadow); font-weight: 600;
}
.fab.primary, .primary { background: var(--blue); color: #fff; }

/* ---------- Auflösungsregler über den Kartenknöpfen ---------- */
/* Feste Höhe: Der Regler darf nicht springen, wenn darunter Text erscheint. */
#resPanel {
  background: var(--bg); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 8px 12px 8px; width: 200px; margin-bottom: 2px;
  display: flex; flex-direction: column;
}
#resHead { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; }
#resHead span { font-size: .72rem; color: var(--muted); }
#resHead b { font-size: .8rem; color: var(--blue-dark); white-space: nowrap; }
#resPanel input[type="range"] { width: 100%; margin: 5px 0 4px; }
#resNote {
  font-size: .68rem; line-height: 1.35;
  min-height: calc(3 * 1.35em);            /* Platz für die längste Meldung */
  display: flex; align-items: flex-start;
}
#resNote.warn { color: #c1791d; }
@media (max-width: 480px) {
  #resPanel { width: 180px; }
  #resNote { min-height: calc(4 * 1.35em); }
}
.fab.lake { background: var(--box); color: var(--blue-dark); border: 2px solid var(--blue); font-weight: 600; }
#damName { flex: 1 1 auto; min-width: 80px; font: inherit; font-weight: 600; border: 1px solid var(--blue); border-radius: 8px; padding: 5px 6px; }
#drawDepth { display: flex; align-items: center; gap: 5px; font-size: .8rem; color: var(--muted); }
#drawDepth input { width: 62px; font: inherit; padding: 6px 6px; border: 1px solid var(--line); border-radius: 8px; }
#layerMenu {
  position: fixed; right: 64px; bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  background: var(--bg); border-radius: var(--radius); box-shadow: var(--shadow);
  z-index: 1150; overflow: hidden;
}
body.sheet-open #layerMenu { bottom: calc(50vh + 16px); }
#layerMenu button { display: block; width: 100%; border: 0; background: none; padding: 10px 16px; text-align: left; }
#layerMenu button:hover, #layerMenu button.active { background: var(--box); }
#layerMenu hr { margin: 0; border: none; border-top: 1px solid var(--line); }

/* ---------- Zeichenleiste ---------- */
#drawbar {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: center;
  background: var(--bg); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 10px 12px; z-index: 1400; max-width: 94vw;
  border: 2px solid var(--blue);
}
@media (min-width: 900px) {
  #drawbar { bottom: auto; top: calc(var(--topbar-h) + 12px); }
}
#drawInfo { display: flex; flex-direction: column; line-height: 1.3; margin-right: 4px; }
#drawInfo b { font-size: .88rem; }
#drawInfo span { font-size: .76rem; color: var(--muted); }
#drawbar button { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 9px 12px; }
#drawbar button.primary { border: 0; background: var(--blue); color: #fff; font-weight: 600; }
#drawbar button.primary:disabled { background: var(--line); color: var(--muted); cursor: not-allowed; }

/* ---------- Panel / Sheet ---------- */
/* Ein einziges Attribut steuert die Sichtbarkeit – so können sich Zustände nicht überschreiben. */
#sheet {
  position: fixed; z-index: 1150; background: var(--bg);
  box-shadow: 0 -4px 20px rgba(10,30,60,.22);
  display: flex; flex-direction: column;
  left: 0; right: 0; bottom: 0; height: 52vh;
  border-radius: 16px 16px 0 0;
  transition: transform .25s;
}
#sheet[data-state="closed"] { transform: translateY(110%); }
#sheet[data-state="peek"]   { transform: translateY(calc(100% - 116px)); }
#sheet[data-state="open"]   { transform: none; }
#sheetHandle { display: flex; justify-content: center; padding: 8px 0 2px; cursor: pointer; }
.grip { width: 52px; height: 5px; border-radius: 3px; background: var(--line); }
#sheetHead { display: flex; align-items: center; gap: 8px; padding: 4px 12px 6px; }
#damSelect { flex: 0 1 auto; max-width: 46%; font: inherit; font-weight: 600; border: 1px solid var(--line); border-radius: 8px; padding: 5px 6px; background: var(--panel); }
/* Platzhalter: schiebt die Knöpfe rechts an den Rand, seit die Kennzahlen
   in der Kopfzeile stehen (dort ist Platz für die vollen Werte). */
#sheetHeadSpacer { flex: 1; }
#tabs { display: flex; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; }
#tabs button {
  flex: 1; border: 0; background: none; padding: 9px 8px; white-space: nowrap;
  color: var(--muted); border-bottom: 2px solid transparent; font-size: .85rem;
}
#tabs button.active { color: var(--blue); border-bottom-color: var(--blue); font-weight: 600; }
#panels { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.panel { display: none; padding: 12px 14px calc(20px + env(safe-area-inset-bottom, 0px)); }
.panel.active { display: block; }

/* Desktop: Panel rechts */
@media (min-width: 900px) {
  #sheet {
    left: auto; top: var(--topbar-h); height: auto; width: 400px;
    border-radius: 0; box-shadow: -4px 0 20px rgba(10,30,60,.15);
  }
  #sheet[data-state="closed"] { transform: translateX(110%); }
  #sheet[data-state="peek"], #sheet[data-state="open"] { transform: none; }
  #sheetHandle { display: none; }
  body.sheet-open #fabs, body.sheet-open #layerMenu { bottom: calc(24px + env(safe-area-inset-bottom, 0px)); right: 412px; }
  body.sheet-open #layerMenu { right: 464px; }
}

/* ---------- Inhalte ---------- */
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.stat b { display: block; font-size: 1.05rem; color: var(--blue-dark); }
.stat span { font-size: .72rem; color: var(--muted); }
.stat small { display: block; font-size: .72rem; color: var(--muted); }

.sliderRow { margin-top: 4px; }
.sliderRow label { display: flex; justify-content: space-between; font-size: .85rem; margin-bottom: 4px; }
input[type="range"] { width: 100%; accent-color: var(--blue); }
#levelRow {
  display: flex; align-items: center; gap: 8px; margin-top: 8px;
  font-size: .78rem; color: var(--muted);
}
#levelRow input { width: 108px; font: inherit; padding: 5px 7px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--text); }

.formGrid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; margin: 8px 0; }
.formGrid label { display: flex; flex-direction: column; gap: 3px; font-size: .78rem; color: var(--muted); }
.formGrid input, .formGrid select, textarea {
  font: inherit; padding: 7px 8px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--text); width: 100%;
}
textarea { resize: vertical; margin-top: 6px; }

.rowBtns { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
button.small { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 11px; font-size: .82rem; }
button.small.primary { background: var(--blue); color: #fff; border: 0; }

.resultBox {
  background: var(--box); border: 1px solid var(--boxLine); border-radius: 10px;
  padding: 10px 12px; margin: 8px 0; font-size: .86rem; line-height: 1.45;
}
.resultBox.warn { background: var(--warnBox); border-color: var(--warnLine); }
.resultBox.ok { background: var(--okBox); border-color: var(--okLine); }

.hint { font-size: .78rem; color: var(--muted); line-height: 1.5; }
details { margin: 10px 0; border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; }
details summary { cursor: pointer; font-weight: 600; font-size: .88rem; }
details[open] summary { margin-bottom: 6px; }

.chart { width: 100%; overflow: hidden; }
.chart svg { display: block; width: 100%; height: auto; }

/* Schematische Zeichnungen (Fischpass, Dammquerschnitt) */
.fishFig, .damFig {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 6px; margin: 8px 0;
}
.fishFig svg, .damFig svg { display: block; width: 100%; height: auto; }

/* Beschriftung des Damms: schmale Hochkantbox statt breiter Zeile */
.leaflet-tooltip.damTip {
  max-width: 150px; white-space: normal; line-height: 1.35;
  font-size: .78rem; padding: 6px 8px; text-align: left;
}
.leaflet-tooltip.damTip b { display: block; margin-bottom: 2px; }

/* Dammbauwerk auf der Karte: Materialoptik */
.damBody { stroke-linejoin: round; }
.damBody.mat-beaver { fill: url(#teichPat-beaver); }
.damBody.mat-earth { fill: url(#teichPat-earth); }
.damBody.mat-rock { fill: url(#teichPat-rock); }
.damBody.mat-concrete { fill: url(#teichPat-concrete); }

table.mats { width: 100%; table-layout: fixed; border-collapse: collapse; font-size: .82rem; margin: 8px 0; }
table.mats td, table.mats th {
  padding: 5px 6px; border-bottom: 1px solid var(--line); text-align: left;
  overflow-wrap: anywhere;
}
table.mats td:first-child, table.mats th:first-child { width: 44%; }
table.mats td:last-child, table.mats th:last-child { text-align: right; }
/* Panelinhalt darf nie seitlich überlaufen */
.panel { overflow-x: hidden; }

/* ---------- Begrüßung, Toast, Loader ---------- */
#welcome {
  position: fixed; inset: 0; z-index: 2000; background: rgba(12, 30, 50, .55);
  display: flex; align-items: center; justify-content: center; padding: 18px;
}
#welcome.hidden, #welcome[hidden] { display: none; }
#welcome .card {
  background: var(--bg); border-radius: 16px; padding: 22px; max-width: 460px;
  box-shadow: var(--shadow); max-height: 86vh; overflow-y: auto;
}
#welcome h2 { margin: 0 0 8px; }
#welcome ol { padding-left: 20px; line-height: 1.7; }
#welcome button { width: 100%; border: 0; border-radius: 10px; padding: 12px; font-weight: 600; margin-top: 8px; }

/* ---------- Erklärungen zu Fachwörtern ---------- */
i.term {
  display: inline-block; cursor: help; font-style: normal; font-weight: 700;
  font-size: .62rem; line-height: 1.05; vertical-align: super; margin-left: 3px;
  color: var(--blue); border: 1px solid currentColor; border-radius: 50%;
  width: 1.15em; height: 1.15em; text-align: center; flex: none;
}
i.term:hover, i.term:focus { background: var(--blue); color: var(--bg); outline: none; }
.formGrid label { position: relative; }
.formGrid label > i.term { position: absolute; top: 0; left: auto; right: 0; }
#termPop {
  position: fixed; z-index: 2200; max-width: 270px;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--boxLine); border-radius: 10px;
  box-shadow: var(--shadow); padding: 10px 12px;
}
#termPop b { display: block; font-size: .86rem; margin-bottom: 4px; color: var(--blue-dark); }
#termPop p { margin: 0; font-size: .78rem; line-height: 1.5; }
#termFormel {
  display: block; margin-top: 8px; padding: 6px 8px; border-radius: 6px;
  background: var(--box); border: 1px solid var(--boxLine);
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: .72rem; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere;
}

/* ---------- 3D-Ansicht ---------- */
#v3Modal {
  position: fixed; inset: 0; z-index: 2050; background: var(--bg);
  display: flex; flex-direction: column;
}
#v3Head {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  border-bottom: 1px solid var(--line); flex: none;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
}
#v3Head b { font-size: .95rem; }
#v3Info { flex: 1; font-size: .78rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#v3Canvas { flex: 1; width: 100%; min-height: 0; display: block; touch-action: none; cursor: grab; }
#v3Canvas:active { cursor: grabbing; }
#v3Bar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--line); flex: none;
}
#v3ExLabel { display: flex; align-items: center; gap: 6px; font-size: .78rem; color: var(--muted); }
#v3ExLabel input { width: 100px; accent-color: var(--blue); }
#v3Bar .hint { flex: 1; text-align: right; min-width: 120px; }

/* ---------- Biber-Abschnitt ---------- */
.beaverCheck { list-style: none; margin: 8px 0 0; padding: 0; }
.beaverCheck li { padding: 4px 0 4px 2px; font-size: .82rem; line-height: 1.45; }
.beaverCheck li.ja { color: var(--green); }
.beaverCheck li.nein { color: #d4674a; }
.beaverCheck li.evtl { color: #c1791d; }
.beaverBox { border-color: var(--okLine); background: var(--okBox); }
.beaverBox .srcStep { background: var(--bg); border-color: var(--okLine); }

/* ---------- Fenster für die Bezugsquellen ---------- */
#demModal {
  position: fixed; inset: 0; z-index: 2050; background: rgba(12, 30, 50, .55);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
#demModal .card {
  position: relative; background: var(--bg); border-radius: 16px; padding: 20px;
  max-width: 640px; width: 100%; max-height: 88vh; overflow-y: auto; box-shadow: var(--shadow);
}
#demModal h3 { margin: 0 0 10px; font-size: 1.05rem; padding-right: 32px; }
#demModal h4 { margin: 16px 0 6px; font-size: .92rem; }
.modalClose { position: absolute; top: 12px; right: 12px; }
.srcStep {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; margin: 8px 0; font-size: .84rem; line-height: 1.5;
}
.srcStep b { color: var(--blue-dark); }
.srcStep ol { margin: 6px 0 0; padding-left: 20px; }
.srcStep code {
  background: var(--panel); border: 1px solid var(--line); border-radius: 4px;
  padding: 1px 5px; font-size: .82em; word-break: break-all;
}
.srcList { list-style: none; margin: 6px 0; padding: 0; }
.srcList li {
  padding: 7px 0; border-bottom: 1px solid var(--line); font-size: .84rem;
  display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap;
}
.srcList li:last-child { border-bottom: 0; }
.srcList a { color: var(--blue); }
.srcList .res { color: var(--muted); font-size: .78rem; }
.srcGroup { margin: 10px 0; }
.srcGroup > b { font-size: .88rem; }
.srcHere {
  background: var(--okBox); border: 1px solid var(--okLine); border-radius: 10px;
  padding: 8px 12px; margin: 8px 0;
}

#toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(90px + env(safe-area-inset-bottom, 0px));
  background: var(--text); color: var(--bg); border-radius: 10px; padding: 10px 16px;
  font-size: .85rem; z-index: 2100; max-width: 90vw; text-align: center;
  box-shadow: var(--shadow);
}
body.sheet-open #toast { bottom: calc(52vh + 12px); }
@media (min-width: 900px) { body.sheet-open #toast { bottom: 90px; } }

#loader {
  position: fixed; top: calc(var(--topbar-h) + 12px); left: 12px; z-index: 1250;
  background: var(--bg); border-radius: 10px; box-shadow: var(--shadow);
  display: flex; align-items: center; gap: 8px; padding: 8px 14px; font-size: .82rem;
}
.spin {
  width: 16px; height: 16px; border: 3px solid var(--line); border-top-color: var(--blue);
  border-radius: 50%; animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Damm-Bearbeitung auf der Karte */
.dam-vertex { cursor: grab; }
.dam-vertex:active { cursor: grabbing; }
.dam-vertex div {
  width: 16px; height: 16px; background: #fff; border: 3px solid #8d5524;
  border-radius: 4px; box-shadow: 0 1px 4px rgba(0,0,0,.35);
}
.dam-mid div {
  width: 14px; height: 14px; border-radius: 50%;
  background: #8d5524; border: 2px solid #fff; opacity: .75;
  color: #fff; font-size: 11px; line-height: 10px; text-align: center; font-weight: 700;
  box-shadow: 0 1px 4px rgba(0,0,0,.3);
}
.dam-mid:hover div { opacity: 1; transform: scale(1.15); }
.vertexPopup { display: flex; gap: 6px; align-items: center; font-size: .8rem; }
.vertexPopup button { border: 1px solid var(--line); background: var(--panel); border-radius: 6px; padding: 4px 8px; }
.leaflet-popup-content { margin: 8px 10px; }

/* Bearbeitungshinweis */
#editHint {
  position: fixed; left: 50%; transform: translateX(-50%);
  top: calc(var(--topbar-h) + 10px);
  background: rgba(28,39,51,.9); color: #fff; border-radius: 20px;
  padding: 7px 14px; font-size: .78rem; z-index: 1120; pointer-events: none;
  max-width: 92vw; text-align: center;
}
