/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f1117;
  --bg-surface: #1a1d27;
  --bg-surface-hover: #242836;
  --border: #2a2e3a;
  --text: #e4e4e7;
  --text-muted: #8b8fa3;
  --accent: #4ade80;
  --accent-dim: rgba(74, 222, 128, 0.15);
  --navbar-h: 52px;
  --sidebar-w: 300px;
  --radius: 8px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

/* ===== Navbar ===== */
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--navbar-h);
  background: #111318;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1000;
}

.navbar-brand {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.3px;
  color: var(--text);
}

/* ===== App Container ===== */
.app-container {
  position: fixed;
  top: var(--navbar-h);
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
}

/* ===== Sidebar ===== */
.sidebar {
  position: relative;
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 500;
  transition: margin-left 0.25s ease;
}

.sidebar.collapsed {
  margin-left: calc(-1 * var(--sidebar-w));
}

.sidebar-toggle {
  position: absolute;
  top: 100px;
  right: -44px;
  width: 36px;
  height: 36px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 600;
  transition: background 0.15s, color 0.15s;
}

.sidebar-toggle:hover {
  background: var(--bg-surface-hover);
  color: var(--text);
}

.sidebar-content {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

/* ===== Control Groups ===== */
.control-group {
  margin-bottom: 20px;
}

.control-label {
  display: block;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

/* ===== Search ===== */
.search-wrapper {
  display: flex;
  gap: 6px;
}

.search-input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 12px;
  font-size: 0.85rem;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}

.search-input:focus {
  border-color: var(--accent);
}

.search-input::placeholder {
  color: var(--text-muted);
}

.search-btn {
  width: 36px;
  height: 36px;
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  color: #111;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: opacity 0.15s;
}

.search-btn:hover {
  opacity: 0.85;
}

.search-results {
  list-style: none;
  margin-top: 6px;
}

.search-results li {
  padding: 8px 10px;
  font-size: 0.82rem;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 4px;
  cursor: pointer;
  transition: background 0.15s;
}

.search-results li:hover {
  background: var(--bg-surface-hover);
}

/* ===== Crop Buttons ===== */
.crop-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.crop-btn {
  padding: 6px 12px;
  font-size: 0.8rem;
  font-weight: 500;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
}

.crop-btn:hover {
  border-color: var(--accent);
  color: var(--text);
}

.crop-btn.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}

/* ===== Layer Toggles ===== */
.layer-toggle {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

.layer-toggle:last-child {
  border-bottom: none;
}

.layer-toggle-header {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
}

.layer-toggle input[type="checkbox"] {
  appearance: none;
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  cursor: pointer;
  flex-shrink: 0;
  position: relative;
  transition: all 0.15s;
}

.layer-toggle input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}

.layer-toggle input[type="checkbox"]:checked::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 5px;
  width: 5px;
  height: 9px;
  border: solid #111;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.layer-toggle label {
  font-size: 0.82rem;
  color: var(--text);
  cursor: pointer;
  line-height: 1.3;
  flex: 1;
  min-width: 0;
}

.layer-toggle .layer-tag {
  font-size: 0.65rem;
  padding: 2px 6px;
  border-radius: 10px;
  background: var(--accent-dim);
  color: var(--accent);
  white-space: nowrap;
}

/* Opacity slider */
.layer-opacity-row {
  display: none;
  width: 100%;
  padding: 4px 0 2px 26px;
  align-items: center;
  gap: 8px;
}

.layer-opacity-row.visible {
  display: flex;
}

.layer-opacity-row input[type="range"] {
  flex: 1;
  height: 4px;
  appearance: none;
  background: var(--border);
  border-radius: 2px;
  outline: none;
}

.layer-opacity-row input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

.layer-opacity-row input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: none;
}

.opacity-val {
  font-size: 0.7rem;
  color: var(--text-muted);
  min-width: 30px;
  text-align: right;
}

/* ===== Legend hint ===== */
.legend-hint p {
  font-size: 0.75rem;
  color: var(--text-muted);
  line-height: 1.5;
}

/* ===== Map ===== */
#map {
  flex: 1;
  min-width: 0;
  background: var(--bg);
}

/* Dark map tiles — removed, base layer controls handle styling */

/* Fix leaflet controls on dark bg */
.leaflet-control-zoom a {
  background: var(--bg-surface) !important;
  color: var(--text) !important;
  border-color: var(--border) !important;
}

.leaflet-control-zoom a:hover {
  background: var(--bg-surface-hover) !important;
}

.leaflet-control-attribution {
  background: rgba(15, 17, 23, 0.8) !important;
  color: var(--text-muted) !important;
  font-size: 10px !important;
}

.leaflet-control-attribution a {
  color: var(--text-muted) !important;
}

/* ===== Scrollbar ===== */
.sidebar-content::-webkit-scrollbar { width: 6px; }
.sidebar-content::-webkit-scrollbar-track { background: transparent; }
.sidebar-content::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ===== Mobile ===== */
@media (max-width: 768px) {
  :root {
    --sidebar-w: 100vw;
  }

  .sidebar {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 900;
  }

  .sidebar.collapsed {
    margin-left: -100vw;
  }

  .sidebar-toggle {
    top: 100px;
    right: -44px;
  }

  /* Ensure map fills screen on mobile */
  #map {
    width: 100vw;
  }
}

/* ===== Selection ===== */
::selection {
  background: rgba(74, 222, 128, 0.25);
  color: white;
}

/* ===== GPS Locate Button ===== */
.leaflet-control-locate a {
  background: var(--bg-surface) !important;
  color: var(--text) !important;
  border-color: var(--border) !important;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 30px;
  width: 30px;
  height: 30px;
  text-decoration: none;
}

.leaflet-control-locate a:hover {
  background: var(--bg-surface-hover) !important;
}

.leaflet-control-locate a.active {
  color: var(--accent) !important;
}

.gps-marker {
  background: var(--accent);
  border: 3px solid white;
  border-radius: 50%;
  box-shadow: 0 0 8px rgba(74, 222, 128, 0.5);
}

/* ===== Loading Indicator ===== */
.map-loading {
  display: none;
  position: absolute;
  top: calc(var(--navbar-h) + 12px);
  right: 60px;
  z-index: 800;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 14px;
  align-items: center;
  gap: 8px;
  font-size: 0.78rem;
  color: var(--text-muted);
  pointer-events: none;
}

.map-loading.visible {
  display: flex;
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== Legend Container ===== */
.legend-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.legend-item {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  overflow: hidden;
}

.legend-item-title {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.legend-item img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  /* Make legend images visible on dark bg */
  filter: brightness(1.1);
  background: rgba(255, 255, 255, 0.9);
  padding: 2px;
}

/* ===== GetFeatureInfo Popup ===== */
.leaflet-popup-content-wrapper {
  background: var(--bg-surface) !important;
  color: var(--text) !important;
  border: 1px solid var(--border) !important;
  border-radius: var(--radius) !important;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4) !important;
}

.leaflet-popup-tip {
  background: var(--bg-surface) !important;
  border: 1px solid var(--border) !important;
}

.leaflet-popup-close-button {
  color: var(--text-muted) !important;
}

.leaflet-popup-close-button:hover {
  color: var(--text) !important;
}

.feature-info {
  max-height: 250px;
  overflow-y: auto;
  font-size: 0.8rem;
}

.feature-info table {
  width: 100%;
  border-collapse: collapse;
}

.feature-info th,
.feature-info td {
  padding: 4px 8px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 0.78rem;
}

.feature-info th {
  color: var(--accent);
  font-weight: 600;
  white-space: nowrap;
}

.feature-info td {
  color: var(--text);
}

.feature-info-title {
  font-weight: 600;
  font-size: 0.85rem;
  margin-bottom: 6px;
  color: var(--accent);
}

.feature-info-loading {
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 8px 0;
}

.feature-info-empty {
  color: var(--text-muted);
  font-size: 0.8rem;
  font-style: italic;
}

/* ===== Layer Groups (collapsible) ===== */
.layer-group {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 8px;
  overflow: hidden;
  transition: opacity 0.2s;
}

.layer-group-header {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  background: var(--bg);
  border: none;
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  font-family: var(--font);
}

.layer-group-header:hover {
  background: var(--bg-surface-hover);
}

.layer-group-arrow {
  display: inline-block;
  transition: transform 0.2s;
  font-size: 0.7rem;
}

.layer-group.open .layer-group-arrow {
  transform: rotate(90deg);
}

.layer-group-count {
  margin-left: auto;
  font-size: 0.65rem;
  padding: 1px 6px;
  border-radius: 10px;
  background: var(--accent-dim);
  color: var(--accent);
}

.layer-group-body {
  display: none;
  padding: 4px 10px 8px;
}

.layer-group.open .layer-group-body {
  display: block;
}

/* ===== Base Layer Buttons ===== */
.base-layer-buttons {
  display: flex;
  gap: 6px;
}

.base-btn {
  flex: 1;
  padding: 6px 8px;
  font-size: 0.78rem;
  font-weight: 500;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
  font-family: var(--font);
}

.base-btn:hover {
  border-color: var(--accent);
  color: var(--text);
}

.base-btn.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}

/* ===== Floating Legend ===== */
.floating-legend {
  display: none;
  position: absolute;
  bottom: 30px;
  right: 10px;
  z-index: 800;
  max-width: 260px;
}

.floating-legend.visible {
  display: block;
}

.floating-legend-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  margin-left: auto;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  font-size: 0.75rem;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.floating-legend-toggle:hover {
  background: var(--bg-surface-hover);
  color: var(--text);
}

.floating-legend-content {
  display: none;
  margin-top: 6px;
  max-height: 300px;
  overflow-y: auto;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
}

.floating-legend.open .floating-legend-content {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.floating-legend-content::-webkit-scrollbar { width: 5px; }
.floating-legend-content::-webkit-scrollbar-track { background: transparent; }
.floating-legend-content::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
