:root {
  --bg: #0e1a12;
  --panel: rgba(20, 30, 22, 0.92);
  --panel-solid: #15201a;
  --fg: #e9efe8;
  --muted: #9fb3a3;
  --accent: #7fb069;
  --accent-strong: #5d9148;
  --warn: #e0a458;
  --danger: #e0524a;
  --ok: #7fb069;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, system-ui, sans-serif;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation;
}

#map {
  position: fixed;
  inset: 0;
  z-index: 0;
  background: #2a3a2e;
}

/* Top-Bar */
.top-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  padding: calc(var(--safe-top) + 8px) calc(var(--safe-right) + 14px) 8px calc(var(--safe-left) + 14px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: linear-gradient(to bottom, rgba(14, 26, 18, 0.85), rgba(14, 26, 18, 0));
  pointer-events: none;
  z-index: 1000;
}
.top-bar .brand {
  font-weight: 700;
  font-size: 1rem;
  letter-spacing: 0.04em;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
  pointer-events: auto;
}
.status {
  display: flex;
  gap: 8px;
  pointer-events: auto;
}
.dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #555;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5);
  transition: background 0.2s;
}
.dot.ok { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--danger); }
.dot.searching {
  background: var(--warn);
  animation: searchPulse 1.3s ease-in-out infinite;
}
@keyframes searchPulse {
  0%, 100% { opacity: 0.45; transform: scale(0.85); }
  50%      { opacity: 1;    transform: scale(1.15); }
}

/* Live-Statistik */
.stats {
  position: fixed;
  top: calc(var(--safe-top) + 44px);
  left: calc(var(--safe-left) + 12px);
  right: calc(var(--safe-right) + 12px);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  background: var(--panel);
  border-radius: 12px;
  padding: 8px 10px;
  z-index: 1000;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: var(--shadow);
}
.stats > div {
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.stats b {
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
}
.stats span {
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* Folge-Button (FAB) */
.fab {
  position: fixed;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: 0;
  background: var(--panel);
  color: var(--fg);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow);
  z-index: 1000;
  cursor: pointer;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.fab svg { width: 22px; height: 22px; }
.fab-follow {
  right: calc(var(--safe-right) + 14px);
  bottom: calc(var(--safe-bot) + 96px);
}
.fab[aria-pressed="true"] { color: var(--accent); }

/* Aktionsleiste unten */
.action-bar {
  position: fixed;
  left: calc(var(--safe-left) + 8px);
  right: calc(var(--safe-right) + 8px);
  bottom: calc(var(--safe-bot) + 8px);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 8px;
  background: var(--panel);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: var(--shadow);
  z-index: 1000;
}
.action {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--fg);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 8px 4px;
  border-radius: 12px;
  font: inherit;
  cursor: pointer;
}
.action:active { background: rgba(255, 255, 255, 0.06); }
.action svg { width: 22px; height: 22px; }
.action .label {
  font-size: 0.7rem;
  color: var(--muted);
  letter-spacing: 0.02em;
}
.action[data-state="recording"] { color: var(--danger); }
.action[data-state="recording"] .label { color: var(--danger); }

/* Rec-Icon */
.rec-icon {
  display: inline-block;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid currentColor;
  position: relative;
}
.action[data-state="idle"] .rec-icon::after {
  content: "";
  position: absolute;
  inset: 3px;
  background: var(--danger);
  border-radius: 50%;
}
.action[data-state="recording"] .rec-icon {
  border-color: var(--danger);
  animation: pulse 1.2s ease-in-out infinite;
}
.action[data-state="recording"] .rec-icon::after {
  content: "";
  position: absolute;
  inset: 4px;
  background: var(--danger);
  border-radius: 2px;
}
.action[data-state="paused"] .rec-icon::after {
  content: "";
  position: absolute;
  inset: 4px;
  background: var(--warn);
  border-radius: 1px;
  clip-path: polygon(0 0, 30% 0, 30% 100%, 0 100%, 0 0, 70% 0, 70% 100%, 100% 100%, 100% 0);
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(224, 82, 74, 0.55); }
  50% { box-shadow: 0 0 0 8px rgba(224, 82, 74, 0); }
}

/* Sheet */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 1500;
  animation: fade 0.15s ease;
}
.sheet {
  position: fixed;
  left: 0; right: 0;
  bottom: 0;
  max-height: 86vh;
  max-height: 86dvh;            /* dvh: ignoriert iOS-Safari-Toolbar dynamisch */
  background: var(--panel-solid);
  border-radius: 18px 18px 0 0;
  z-index: 1600;
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-bot);
  box-shadow: 0 -12px 32px rgba(0, 0, 0, 0.5);
  animation: slideUp 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
}
/* Optischer Drag-Handle als zweiter Schließ-Hinweis */
.sheet::before {
  content: "";
  position: absolute;
  top: 8px; left: 50%;
  transform: translateX(-50%);
  width: 44px; height: 4px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.18);
  pointer-events: none;
}
.sheet-head {
  flex: 0 0 auto;               /* nie schrumpfen */
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 18px 14px 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}
.sheet-head h2 { margin: 0; font-size: 1.1rem; flex: 1 1 auto; min-width: 0; }
.sheet-close {
  appearance: none;
  border: 0;
  background: rgba(255, 255, 255, 0.10);
  color: var(--fg);
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  font-size: 1.4rem;
  line-height: 1;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  cursor: pointer;
  transition: background 0.12s ease;
}
.sheet-close:hover, .sheet-close:focus-visible { background: rgba(255, 255, 255, 0.18); }
.sheet-close:active { background: rgba(255, 255, 255, 0.28); }
.sheet-body {
  flex: 1 1 auto;
  min-height: 0;                /* damit overflow-y greift */
  padding: 14px 16px 20px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.sheet-bottom-close {
  margin-top: 24px;
  border-top: 1px solid rgba(255, 255, 255, 0.04);
}

/* Formularelemente */
.form-row { margin-bottom: 14px; }
.form-row label {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 4px;
}
.form-row input[type="text"],
.form-row input[type="number"],
.form-row select,
.form-row textarea {
  width: 100%;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  color: var(--fg);
  font: inherit;
}
.form-row input[type="range"] { width: 100%; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 16px;
  background: var(--accent-strong);
  color: white;
  border: 0;
  border-radius: 12px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-block { display: flex; width: 100%; }
.btn-ghost {
  background: rgba(255, 255, 255, 0.06);
  color: var(--fg);
}
.btn-danger { background: var(--danger); }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { flex: 1; }

/* Listen */
.list { list-style: none; padding: 0; margin: 0; }
.list li {
  padding: 12px 4px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  display: flex;
  align-items: center;
  gap: 10px;
}
.list li:last-child { border-bottom: 0; }
.list .title {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.list .title b { font-size: 0.98rem; }
.list .title small { color: var(--muted); font-size: 0.78rem; }
.list .actions { display: flex; gap: 6px; }
.list .iconbtn {
  appearance: none;
  border: 0;
  background: rgba(255, 255, 255, 0.06);
  color: var(--fg);
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.list .iconbtn svg { width: 18px; height: 18px; }
.muted { color: var(--muted); font-size: 0.85rem; }
.empty {
  text-align: center;
  padding: 28px 8px;
  color: var(--muted);
}

/* Progress */
.progress {
  height: 8px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 4px;
  overflow: hidden;
  margin: 8px 0;
}
.progress > div {
  height: 100%;
  background: var(--accent);
  width: 0;
  transition: width 0.15s linear;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bot) + 96px);
  transform: translateX(-50%);
  background: var(--panel-solid);
  color: var(--fg);
  padding: 10px 14px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 2000;
  max-width: 86vw;
  font-size: 0.9rem;
  animation: fade 0.15s ease;
}

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }

/* Leaflet-Overrides für Dark-Theme */
.leaflet-control-attribution {
  background: rgba(20, 30, 22, 0.7) !important;
  color: var(--muted) !important;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  font-size: 0.66rem !important;
}
.leaflet-control-attribution a { color: var(--accent) !important; }
.leaflet-control-scale-line {
  background: rgba(20, 30, 22, 0.78);
  color: var(--fg);
  border-color: var(--accent) !important;
  font-weight: 600;
  font-size: 0.78rem;
  padding: 2px 6px;
}
.leaflet-bar a, .leaflet-bar a:hover {
  background: var(--panel) !important;
  color: var(--fg) !important;
  border-bottom-color: rgba(255, 255, 255, 0.08) !important;
}
.leaflet-touch .leaflet-bar a {
  width: 40px;
  height: 40px;
  line-height: 40px;
}
.leaflet-control-layers {
  background: var(--panel) !important;
  color: var(--fg) !important;
  border-radius: 10px !important;
  border: 0 !important;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
.leaflet-control-layers-expanded { padding: 8px 12px !important; }
.leaflet-control-layers label { color: var(--fg) !important; }

/* Map-Position für Top-Bar-Versatz: Leaflet-Controls aus Sicherheitsabstand */
.leaflet-top { padding-top: calc(var(--safe-top) + 44px); }
.leaflet-right { padding-right: var(--safe-right); }
.leaflet-bottom { padding-bottom: calc(var(--safe-bot) + 84px); }
.leaflet-left { padding-left: var(--safe-left); }

/* Standort-Marker */
.me-marker {
  width: 18px;
  height: 18px;
  background: #2d8cff;
  border: 3px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.4), 0 0 14px rgba(45, 140, 255, 0.5);
}
.me-marker.recording {
  background: var(--danger);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.4), 0 0 14px rgba(224, 82, 74, 0.5);
}

/* Team-Live-Marker */
.team-marker-wrapper { background: transparent; border: 0; }
.team-marker {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 8px 3px 4px;
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 999px;
  color: var(--fg);
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  white-space: nowrap;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  transform: translate(-10px, -50%);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.team-marker.stale { opacity: 0.45; filter: saturate(0.6); }
.team-dot {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35);
  flex: 0 0 12px;
}
.team-label { display: inline-block; max-width: 160px; overflow: hidden; text-overflow: ellipsis; }

/* Cache-Auswahl-Rechteck */
.cache-rect {
  stroke: var(--accent);
  stroke-width: 2;
  fill: var(--accent);
  fill-opacity: 0.12;
}
