/* === Enshrouded Theme Colors (CSS Custom Properties) === */
:root {
  --bg-dark: #08060E;
  --bg-surface: #0E0B16;
  --bg-surface-variant: #151020;
  --bg-card: #12101C;
  --border: #2E2848;
  --border-variant: #1A1528;
  --accent: #D4A024;
  --accent-amber: #CC7A00;
  --accent-ember: #B85C1E;
  --accent-bright: #FFB830;
  --accent-white: #FFF3E0;
  --text-primary: #EAE4D8;
  --text-warm: #D0C2A8;
  --text-subtle: #908098;
  --text-muted: #605670;
  --fog: #3A3358;
  --fog-light: #4D4570;
  --shroud-mid: #251840;
  --shroud-purple: #1A1028;
}

/* === Reset & Base === */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--text-primary);
  background: var(--bg-dark);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* === App Layout === */
#app {
  display: flex;
  height: 100%;
  width: 100%;
}

/* === Sidebar === */
.sidebar {
  width: 320px;
  min-width: 320px;
  height: 100%;
  background: var(--bg-surface);
  display: flex;
  flex-direction: column;
  z-index: 1000;
  border-right: 1px solid var(--border);
}

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

.sidebar-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 1.5px;
  text-transform: uppercase;
}

.sidebar-close {
  display: none;
  background: none;
  border: none;
  color: var(--text-subtle);
  font-size: 24px;
  cursor: pointer;
  padding: 8px 12px;
  line-height: 1;
  min-width: 44px;
  min-height: 44px;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  touch-action: manipulation;
}

.sidebar-close:hover,
.sidebar-close:active {
  color: var(--text-primary);
  background: rgba(212, 160, 36, 0.1);
}

/* === Search === */
.search-container {
  padding: 12px 16px;
  position: relative;
  border-bottom: 1px solid var(--border);
}

#search-input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-dark);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  -webkit-appearance: none;
}

#search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(212, 160, 36, 0.15);
}

#search-input::placeholder {
  color: var(--text-muted);
}

.search-results {
  display: none;
  position: absolute;
  top: 100%;
  left: 16px;
  right: 16px;
  background: var(--bg-dark);
  border: 1px solid var(--border);
  border-radius: 0 0 8px 8px;
  max-height: 400px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  z-index: 1001;
  box-shadow: 0 4px 16px rgba(0,0,0,0.6);
}

.search-results.visible {
  display: block;
}

.search-result-item {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  gap: 10px;
  transition: background 0.15s;
  min-height: 44px;
  touch-action: manipulation;
}

.search-result-item:hover,
.search-result-item:active {
  background: var(--shroud-mid);
}

.search-result-item:last-child {
  border-bottom: none;
}

.search-result-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.search-result-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.search-result-title {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-warm);
}

.search-result-category {
  font-size: 11px;
  color: var(--text-subtle);
}

.search-no-results {
  padding: 16px;
  text-align: center;
  color: var(--text-muted);
}

/* === Sidebar Content === */
.sidebar-content {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 0;
  overscroll-behavior: contain;
}

.sidebar-content::-webkit-scrollbar {
  width: 6px;
}

.sidebar-content::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar-content::-webkit-scrollbar-thumb {
  background: var(--fog);
  border-radius: 3px;
}

/* === Bulk Actions === */
.sidebar-bulk-actions {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--fog);
}

.bulk-action-btn {
  flex: 1;
  padding: 6px 0;
  border: 1px solid var(--fog);
  border-radius: 4px;
  background: var(--bg-surface);
  color: var(--text-primary);
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.bulk-action-btn:hover {
  background: rgba(212, 160, 36, 0.1);
  border-color: var(--accent);
}

.bulk-action-btn.bulk-action-active {
  background: rgba(212, 160, 36, 0.15);
  border-color: var(--accent);
  color: var(--accent);
}

/* === Category List === */
.category-list-flat {
  padding: 4px 0;
}

.category-item {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  cursor: pointer;
  gap: 8px;
  transition: background 0.15s;
  min-height: 40px;
  touch-action: manipulation;
}

.category-item:hover,
.category-item:active {
  background: rgba(212, 160, 36, 0.04);
}

.category-checkbox {
  accent-color: var(--accent);
  cursor: pointer;
  width: 16px;
  height: 16px;
  touch-action: manipulation;
}

.category-icon-wrapper {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.category-name {
  font-size: 13px;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-warm);
}

.category-count {
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* === Map Container === */
#map-container {
  flex: 1;
  position: relative;
  min-width: 0;
}

#map {
  width: 100%;
  height: 100%;
  background: var(--bg-dark);
}

/* === Sidebar Toggle Button === */
.sidebar-toggle {
  display: none;
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 999;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--accent);
  font-size: 20px;
  padding: 10px 14px;
  border-radius: 8px;
  cursor: pointer;
  line-height: 1;
  box-shadow: 0 2px 12px rgba(0,0,0,0.5);
  min-width: 44px;
  min-height: 44px;
  touch-action: manipulation;
}

.sidebar-toggle:hover,
.sidebar-toggle:active {
  background: var(--bg-surface-variant);
  border-color: var(--accent);
}

/* === Sidebar Backdrop (mobile) === */
.sidebar-backdrop {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(8, 6, 14, 0.7);
  z-index: 999;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.sidebar-backdrop.visible {
  display: block;
  opacity: 1;
}

/* === Marker Icons === */
.marker-icon {
  background: none;
  border: none;
}

.marker-icon img {
  display: block;
  filter: drop-shadow(0 1px 3px rgba(0,0,0,0.5));
}

/* === Search Highlight Animation === */
.marker-search-highlight {
  animation: marker-bounce 0.5s ease 2, marker-pulse 1.5s ease-out 1;
}

@keyframes marker-bounce {
  0%, 100% { transform: translateY(0); }
  40% { transform: translateY(-12px); }
  60% { transform: translateY(-4px); }
}

@keyframes marker-pulse {
  0% { filter: drop-shadow(0 0 0 rgba(212, 160, 36, 0.8)); }
  50% { filter: drop-shadow(0 0 12px rgba(212, 160, 36, 0.6)); }
  100% { filter: drop-shadow(0 0 0 rgba(212, 160, 36, 0)); }
}

/* === Cluster Icons === */
.marker-cluster-custom {
  background: none !important;
}

.cluster-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(0,0,0,0.5);
  border: 2px solid rgba(255,243,224,0.3);
}

.cluster-small { width: 32px; height: 32px; font-size: 11px; }
.cluster-medium { width: 38px; height: 38px; font-size: 13px; }
.cluster-large { width: 44px; height: 44px; font-size: 14px; }

/* === Popups === */
.embervale-popup .leaflet-popup-content-wrapper {
  background: var(--bg-dark);
  color: var(--text-primary);
  border-radius: 10px;
  border: 1px solid var(--border);
  box-shadow: 0 4px 20px rgba(0,0,0,0.6);
}

.embervale-popup .leaflet-popup-tip {
  background: var(--bg-dark);
  border: 1px solid var(--border);
  border-top: none;
  border-left: none;
}

.embervale-popup .leaflet-popup-close-btn {
  color: var(--text-subtle);
  font-size: 20px;
  padding: 8px;
  width: 32px;
  height: 32px;
}

.embervale-popup .leaflet-popup-close-btn:hover {
  color: var(--text-primary);
}

.popup-content {
  max-width: 320px;
}

.popup-header {
  padding: 10px 12px;
  margin-bottom: 8px;
}

.popup-header-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.popup-header-text {
  flex: 1;
  min-width: 0;
}

.popup-share-btn {
  flex-shrink: 0;
  background: none;
  border: 1px solid var(--border);
  color: var(--text-subtle);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
  margin-top: 2px;
}

.popup-share-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
  background: rgba(212, 160, 36, 0.1);
}

.popup-share-btn.copied {
  color: var(--accent);
  border-color: var(--accent);
}

.popup-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--accent-white);
  margin-bottom: 2px;
}

.popup-category {
  font-size: 12px;
  color: var(--text-subtle);
}

.popup-description {
  padding: 0 12px 8px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--text-warm);
}

.popup-description strong {
  color: var(--text-primary);
}

.popup-description a {
  color: var(--accent);
  text-decoration: none;
}

.popup-description a:hover {
  text-decoration: underline;
  color: var(--accent-bright);
}

.popup-description ul {
  margin: 4px 0 4px 16px;
  padding: 0;
}

.popup-description li {
  margin: 2px 0;
}

.popup-media {
  padding: 0 12px 8px;
}

.popup-image {
  width: 100%;
  max-height: 200px;
  object-fit: cover;
  border-radius: 6px;
  margin-top: 4px;
}

.popup-progress {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--border);
  cursor: pointer;
  font-size: 13px;
  color: var(--text-warm);
  transition: background 0.15s;
}

.popup-progress:hover {
  background: rgba(212, 160, 36, 0.06);
}

.popup-progress input[type="checkbox"] {
  accent-color: var(--accent);
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.popup-id {
  padding: 4px 12px 8px;
  font-size: 10px;
  color: var(--text-muted);
}

/* === Long-press Share Popup === */
.longpress-content {
  text-align: center;
  padding: 4px 0;
}

.longpress-coords {
  font-size: 12px;
  color: var(--text-subtle);
  margin-bottom: 8px;
  font-family: monospace;
}

.longpress-share-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: rgba(212, 160, 36, 0.1);
  border: 1px solid var(--accent);
  color: var(--accent);
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.15s;
}

.longpress-share-btn:hover {
  background: rgba(212, 160, 36, 0.2);
}

.longpress-share-btn.copied {
  background: rgba(212, 160, 36, 0.25);
  color: var(--accent-bright);
}

/* === Shared Pin Marker === */
.shared-pin-marker {
  background: none !important;
  border: none !important;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.5));
  cursor: pointer;
}

/* === Loading Overlay === */
.loading-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: var(--bg-dark);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  transition: opacity 0.3s ease;
}

.loading-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.loading-overlay p {
  margin-top: 16px;
  color: var(--text-warm);
  font-size: 16px;
  letter-spacing: 0.5px;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* === Leaflet Overrides === */
.leaflet-control-zoom a {
  background: var(--bg-surface) !important;
  color: var(--text-primary) !important;
  border-color: var(--border) !important;
}

.leaflet-control-zoom a:hover {
  background: var(--bg-surface-variant) !important;
}

/* === Responsive — Mobile First === */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 85%;
    max-width: 320px;
    height: 100%;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    box-shadow: 4px 0 24px rgba(0,0,0,0.7);
    padding-top: env(safe-area-inset-top, 0);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-close {
    display: flex;
  }

  .sidebar-toggle {
    display: block;
    top: calc(12px + env(safe-area-inset-top, 0));
    left: calc(12px + env(safe-area-inset-left, 0));
  }

  #map-container {
    width: 100%;
  }

  /* Larger popup close button on mobile */
  .embervale-popup .leaflet-popup-close-btn {
    font-size: 24px;
    padding: 10px;
    width: 40px;
    height: 40px;
  }

  .popup-content {
    max-width: 280px;
  }

  /* Move zoom controls to bottom-right so they don't overlap the menu button */
  .leaflet-top.leaflet-left .leaflet-control-zoom {
    position: fixed;
    bottom: calc(20px + env(safe-area-inset-bottom, 0));
    right: calc(12px + env(safe-area-inset-right, 0));
    top: auto;
    left: auto;
    margin: 0 !important;
    z-index: 999;
  }
}

@media (min-width: 769px) {
  .sidebar-toggle {
    display: none;
  }

  .sidebar {
    display: flex;
  }

  .sidebar-backdrop {
    display: none !important;
  }
}

/* Small phones */
@media (max-width: 380px) {
  .sidebar {
    width: 90%;
  }

  .sidebar-title {
    font-size: 17px;
  }
}
