:root {
  --bg: #0d1117;
  --surface: #161b22;
  --surface-2: #1c2128;
  --border: #30363d;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --accent: #8b1a1a;
  --accent-hover: #a52222;
  --accent-light: #c0392b;
  --success: #238636;
  --warning: #d29922;
  --gps: #58a6ff;
  --start: #3fb950;
  --trail: #ff8c42;
  --trail-glow: rgba(255, 140, 66, 0.45);
  --hud-bg: rgba(12, 16, 22, 0.72);
  --hud-blur: blur(14px);
  --pin-first: #e74c3c;
  --pin-heavy: #c0392b;
  --pin-last: #922b21;
  --pin-bedded: #3498db;
  --pin-arrow: #f39c12;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* Map HUD (floating over satellite) */
.map-hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 850;
  pointer-events: none;
  padding: calc(8px + var(--safe-top)) 10px 0;
}

.hud-top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.hud-brand {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 14px;
  background: var(--hud-bg);
  -webkit-backdrop-filter: var(--hud-blur);
  backdrop-filter: var(--hud-blur);
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
  pointer-events: auto;
}

.hud-brand h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  flex: 1;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hud-actions {
  display: flex;
  gap: 6px;
  pointer-events: auto;
}

.hud-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  padding: 0 10px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: var(--hud-bg);
  -webkit-backdrop-filter: var(--hud-blur);
  backdrop-filter: var(--hud-blur);
  color: var(--text);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  text-decoration: none;
}

.hud-btn.hud-help {
  width: 36px;
  padding: 0;
  font-size: 14px;
  color: var(--text-muted);
}

.hud-stats {
  display: flex;
  gap: 6px;
  pointer-events: auto;
}

.panel-hint {
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.45;
  margin: 0 0 12px;
}

.layer-toggle {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  color: var(--text-muted);
  cursor: pointer;
}

.layer-toggle input {
  margin-top: 3px;
  width: 18px;
  height: 18px;
}

.layer-import {
  margin-top: 14px;
}

.import-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
}

.file-input-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.layer-toggle.disabled,
.layer-import.disabled {
  opacity: 0.45;
  pointer-events: none;
}

.draw-bar {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 12px;
  z-index: 860;
  padding: 10px 12px;
  border-radius: 14px;
  background: var(--hud-bg);
  -webkit-backdrop-filter: var(--hud-blur);
  backdrop-filter: var(--hud-blur);
  border: 1px solid rgba(255, 229, 102, 0.35);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
}

.draw-bar-title {
  margin: 0 0 8px;
  font-size: 13px;
  font-weight: 700;
  text-align: center;
  color: #ffe566;
}

.draw-bar-actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

.county-select,
.county-input {
  width: 100%;
  padding: 10px 12px;
  margin-bottom: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
}

.county-links {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 10px 0;
}

.county-link {
  display: block;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  text-align: center;
}

.county-link.primary {
  border-color: var(--trail);
  background: rgba(255, 140, 66, 0.12);
  color: #fff;
}

.county-direct-badge {
  color: #5ee87a !important;
  font-weight: 700;
  margin-bottom: 6px !important;
}

.stat-pill {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 6px 8px;
  background: var(--hud-bg);
  -webkit-backdrop-filter: var(--hud-blur);
  backdrop-filter: var(--hud-blur);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  min-width: 0;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.28);
}

.stat-pill .label {
  font-size: 10px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-pill .value {
  font-size: 14px;
  font-weight: 800;
  color: #7ec8ff;
  letter-spacing: -0.02em;
}

.stat-pill.distance .value { color: #5ee87a; }

.gps-status {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}

.gps-status.active { background: var(--success); box-shadow: 0 0 8px var(--success); }
.gps-status.waiting { background: var(--warning); animation: pulse 1.2s infinite; }
.gps-status.error { background: var(--accent-light); }
.gps-status.explore { background: var(--gps); box-shadow: 0 0 8px rgba(88, 166, 255, 0.6); }

.address-lookup-bar {
  display: flex;
  gap: 6px;
  margin-top: 8px;
  pointer-events: auto;
}

.address-lookup-input {
  flex: 1;
  min-width: 0;
  padding: 9px 12px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: var(--hud-bg);
  -webkit-backdrop-filter: var(--hud-blur);
  backdrop-filter: var(--hud-blur);
  color: var(--text);
  font-size: 14px;
}

.address-lookup-input::placeholder {
  color: var(--text-muted);
}

.address-go-btn {
  padding: 9px 14px;
  border-radius: 12px;
  border: 1px solid rgba(88, 166, 255, 0.45);
  background: rgba(88, 166, 255, 0.2);
  color: #fff;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
  white-space: nowrap;
}

.address-go-btn:active {
  transform: scale(0.97);
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* Map */
.map-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
}

#map {
  width: 100%;
  height: 100%;
  background: #0a0e12;
  z-index: 1;
}

.basemap-bar {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 12px;
  z-index: 840;
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: 14px;
  background: var(--hud-bg);
  -webkit-backdrop-filter: var(--hud-blur);
  backdrop-filter: var(--hud-blur);
  border: 1px solid rgba(255, 255, 255, 0.08);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
  max-width: calc(100% - 80px);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.basemap-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 58px;
  padding: 6px 8px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--text-muted);
  font-size: 10px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}

.basemap-btn .bm-icon { font-size: 16px; line-height: 1; }
.basemap-btn.active {
  background: rgba(255, 140, 66, 0.22);
  color: #fff;
  box-shadow: inset 0 0 0 1px rgba(255, 140, 66, 0.45);
}

.basemap-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}

.basemap-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 12px 6px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.basemap-card .bm-icon { font-size: 20px; }
.basemap-card.active {
  border-color: var(--trail);
  background: rgba(255, 140, 66, 0.12);
  color: var(--text);
}

.panel-section-title {
  margin: 0 0 8px;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.map-tools {
  position: absolute;
  right: 10px;
  top: calc(108px + var(--safe-top));
  z-index: 840;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.map-tool-btn {
  width: 48px;
  min-height: 48px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 14px;
  background: var(--hud-bg);
  -webkit-backdrop-filter: var(--hud-blur);
  backdrop-filter: var(--hud-blur);
  color: var(--text);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.02em;
  cursor: pointer;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.38);
  display: flex;
  align-items: center;
  justify-content: center;
}

.map-tool-btn .basemap-icon { font-size: 22px; line-height: 1; }

.map-tool-btn:active {
  transform: scale(0.96);
  background: rgba(255, 255, 255, 0.08);
}

.map-tool-btn.active {
  border-color: rgba(255, 229, 102, 0.75);
  background: rgba(255, 229, 102, 0.18);
  color: #ffe566;
}

.boundary-dim-wrap {
  background: transparent;
  border: none;
}

.boundary-dim-label {
  display: inline-block;
  padding: 2px 5px;
  border-radius: 4px;
  background: rgba(8, 12, 18, 0.82);
  border: 1px solid rgba(255, 229, 102, 0.55);
  color: #ffe566;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.02em;
  white-space: nowrap;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9);
  transform-origin: center center;
  pointer-events: none;
}

.property-pin-marker {
  width: 28px;
  height: 28px;
  border-radius: 50% 50% 50% 0;
  background: #ffe566;
  border: 3px solid #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.45);
  transform: rotate(-45deg);
}

.property-controls {
  position: absolute;
  left: 10px;
  bottom: 72px;
  z-index: 845;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  max-width: calc(100% - 80px);
}

.property-fab {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 12px 16px;
  border: 2px solid rgba(255, 229, 102, 0.55);
  border-radius: 999px;
  background: linear-gradient(135deg, rgba(139, 26, 26, 0.95), rgba(90, 18, 18, 0.95));
  -webkit-backdrop-filter: var(--hud-blur);
  backdrop-filter: var(--hud-blur);
  color: #fff;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.02em;
  cursor: pointer;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}

.property-fab-icon {
  font-size: 18px;
  line-height: 1;
  color: #ffe566;
}

.property-fab:active {
  transform: scale(0.97);
}

.property-fab:disabled {
  opacity: 0.45;
  pointer-events: none;
}

.property-pin-tool {
  flex-shrink: 0;
}

.property-sheet {
  position: absolute;
  inset: 0;
  z-index: 900;
  display: flex;
  align-items: flex-end;
  padding: 12px;
  background: rgba(0, 0, 0, 0.45);
}

.property-sheet[hidden] {
  display: none !important;
}

.property-sheet-card {
  width: 100%;
  padding: 16px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
}

.property-sheet-title {
  margin: 0 0 6px;
  font-size: 17px;
}

.property-sheet-hint {
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.45;
}

.leaflet-container {
  background: #0a0e12;
  font-family: inherit;
}

.leaflet-control-zoom {
  border: none !important;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.38) !important;
}

.leaflet-control-zoom a {
  width: 40px !important;
  height: 40px !important;
  line-height: 40px !important;
  background: var(--hud-bg) !important;
  -webkit-backdrop-filter: var(--hud-blur);
  backdrop-filter: var(--hud-blur);
  color: var(--text) !important;
  border: 1px solid rgba(255, 255, 255, 0.1) !important;
  border-radius: 12px !important;
  font-size: 18px !important;
  font-weight: 700 !important;
}

.leaflet-control-zoom-in { margin-bottom: 6px !important; }

.leaflet-bottomright .leaflet-control-zoom {
  margin-bottom: calc(58px + var(--safe-bottom)) !important;
  margin-right: 10px !important;
}

.leaflet-control-scale {
  margin-left: 10px !important;
  margin-bottom: calc(58px + var(--safe-bottom)) !important;
}

.leaflet-control-scale-line {
  background: var(--hud-bg) !important;
  -webkit-backdrop-filter: var(--hud-blur);
  backdrop-filter: var(--hud-blur);
  border: 1px solid rgba(255, 255, 255, 0.15) !important;
  border-top: none !important;
  color: var(--text) !important;
  font-size: 10px !important;
  font-weight: 700 !important;
  padding: 2px 6px !important;
}

.leaflet-control-attribution {
  background: rgba(8, 10, 14, 0.65) !important;
  color: var(--text-muted) !important;
  font-size: 9px !important;
  max-width: 55%;
}

.leaflet-control-attribution a { color: var(--text-muted) !important; }

/* Pin markers */
.pin-marker {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 3px solid #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 800;
  color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.55), 0 0 0 1px rgba(0, 0, 0, 0.25);
}

.pin-marker.first-blood { background: var(--pin-first); }
.pin-marker.heavy-blood { background: var(--pin-heavy); }
.pin-marker.last-blood { background: var(--pin-last); }
.pin-marker.bedded { background: var(--pin-bedded); }
.pin-marker.arrow { background: var(--pin-arrow); color: #1a1a1a; }

.start-marker {
  width: 30px;
  height: 30px;
  background: var(--start);
  border: 3px solid #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.55), 0 0 0 1px rgba(0, 0, 0, 0.2);
}

.user-marker-wrap {
  position: relative;
  width: 28px;
  height: 28px;
}

.user-marker-pulse {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: rgba(88, 166, 255, 0.35);
  animation: gps-pulse 2s ease-out infinite;
}

.user-marker {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 16px;
  height: 16px;
  margin: -8px 0 0 -8px;
  background: var(--gps);
  border: 3px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 10px var(--gps), 0 2px 6px rgba(0, 0, 0, 0.45);
}

@keyframes gps-pulse {
  0% { transform: scale(0.55); opacity: 0.9; }
  70% { transform: scale(1.35); opacity: 0; }
  100% { transform: scale(1.35); opacity: 0; }
}

/* Controls panel */
.controls {
  flex-shrink: 0;
  background: linear-gradient(180deg, rgba(22, 27, 34, 0.98) 0%, var(--surface) 100%);
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  padding: 10px 12px calc(12px + var(--safe-bottom));
  box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.35);
}

.pin-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 10px;
}

.pin-btn {
  min-height: 56px;
  padding: 10px 6px;
  border: none;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  transition: transform 0.1s, opacity 0.1s;
  -webkit-user-select: none;
  user-select: none;
}

.pin-btn:active { transform: scale(0.96); opacity: 0.9; }
.pin-btn:disabled { opacity: 0.35; cursor: not-allowed; transform: none; }

.pin-btn .icon { font-size: 18px; line-height: 1; }

.pin-btn.first-blood { background: var(--pin-first); }
.pin-btn.heavy-blood { background: var(--pin-heavy); }
.pin-btn.last-blood { background: var(--pin-last); }
.pin-btn.bedded { background: var(--pin-bedded); }
.pin-btn.arrow { background: var(--pin-arrow); color: #1a1a1a; }

.action-row {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

.action-btn {
  min-height: 48px;
  padding: 10px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.action-btn:active { background: var(--border); }
.action-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.action-btn.primary:active { background: var(--accent-hover); }

/* Modals / panels */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  z-index: 2000;
  display: none;
  align-items: flex-end;
  justify-content: center;
}

.overlay.open { display: flex; }

.panel {
  width: 100%;
  max-width: 520px;
  max-height: 85vh;
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  border: 1px solid var(--border);
  border-bottom: none;
  display: flex;
  flex-direction: column;
  animation: slide-up 0.25s ease-out;
}

@keyframes slide-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.panel-header h2 {
  margin: 0;
  font-size: 17px;
}

.panel-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 24px;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
}

.panel-body {
  padding: 16px;
  overflow-y: auto;
  flex: 1;
}

.panel-body input,
.panel-body textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
  margin-bottom: 12px;
}

.panel-body textarea {
  min-height: 100px;
  resize: vertical;
}

.panel-footer {
  padding: 12px 16px calc(12px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
}

.panel-footer button {
  flex: 1;
  min-height: 48px;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.btn-save { background: var(--accent); color: #fff; }
.btn-cancel { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: #6e1a1a; color: #fff; }

/* Track list */
.track-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.track-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 8px;
  cursor: pointer;
  background: var(--surface-2);
  transition: background 0.15s;
}

.track-item:active { background: var(--border); }

.track-item .info { flex: 1; min-width: 0; }
.track-item .name {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.track-item .meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.track-actions {
  display: flex;
  gap: 6px;
}

.track-actions button {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
}

.empty-state {
  text-align: center;
  padding: 32px 16px;
  color: var(--text-muted);
}

.empty-state p { margin: 0 0 8px; }

.toast {
  position: fixed;
  bottom: calc(80px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--surface-2);
  color: var(--text);
  padding: 12px 20px;
  border-radius: 10px;
  border: 1px solid var(--border);
  font-size: 14px;
  font-weight: 500;
  z-index: 3000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  white-space: nowrap;
  max-width: 90vw;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-width: 380px) {
  .basemap-btn .bm-label { display: none; }
  .basemap-btn { min-width: 42px; }
}

@media (min-width: 600px) {
  .pin-grid { grid-template-columns: repeat(5, 1fr); }
  .overlay { align-items: center; }
  .panel { border-radius: 16px; border-bottom: 1px solid var(--border); max-height: 80vh; }
  .map-tools { top: calc(96px + var(--safe-top)); }
}