/* Light theme is the default — chosen for outdoor acroyoga sessions where
   the screen is in direct sunlight. Dark theme is opt-in via body.theme-dark
   (toggled by the toolbar sun/moon button, persisted in localStorage). */
:root {
  --aurora-bg: #FAFAFA;
  --aurora-surface: #FFFFFF;
  --aurora-surface-2: #F2F0EB;
  --aurora-line: rgba(0,0,0,0.10);
  --aurora-text: #1A1A1A;
  --aurora-text-mute: rgba(26,26,26,0.60);
  --aurora-text-dim: rgba(26,26,26,0.40);
  --aurora-signal: oklch(0.62 0.18 50);
  --aurora-signal-soft: oklch(0.62 0.18 50 / 0.15);
  --aurora-friend: oklch(0.55 0.17 290);
  --aurora-friend-soft: oklch(0.55 0.17 290 / 0.15);
  --aurora-mint: oklch(0.65 0.14 165);
  /* Tint helpers — replace inline rgba() so they flip with theme */
  --aurora-hover: rgba(0,0,0,0.05);
  --aurora-overlay-bg: rgba(255,255,255,0.95);
  --aurora-empty-overlay-bg: rgba(250,250,250,0.70);
  --aurora-popup-shadow: 0 4px 20px rgba(0,0,0,0.12);
  --aurora-shadow-soft: 0 4px 12px rgba(0,0,0,0.10);
  --aurora-shadow-strong: 0 6px 16px rgba(0,0,0,0.18);
  --aurora-modal-backdrop: rgba(0,0,0,0.45);
}

body.theme-dark {
  --aurora-bg: #0F1116;
  --aurora-surface: #181B23;
  --aurora-surface-2: #21252F;
  --aurora-line: rgba(255,255,255,0.08);
  --aurora-text: #F5F2EC;
  --aurora-text-mute: rgba(245,242,236,0.55);
  --aurora-text-dim: rgba(245,242,236,0.35);
  --aurora-signal: oklch(0.72 0.17 50);
  --aurora-signal-soft: oklch(0.72 0.17 50 / 0.18);
  --aurora-friend: oklch(0.65 0.16 290);
  --aurora-friend-soft: oklch(0.65 0.16 290 / 0.18);
  --aurora-mint: oklch(0.78 0.13 165);
  --aurora-hover: rgba(245,242,236,0.08);
  --aurora-overlay-bg: rgba(15,17,22,0.95);
  --aurora-empty-overlay-bg: rgba(15,17,22,0.70);
  --aurora-popup-shadow: 0 4px 20px rgba(0,0,0,0.5);
  --aurora-shadow-soft: 0 4px 12px rgba(0,0,0,0.30);
  --aurora-shadow-strong: 0 6px 16px rgba(0,0,0,0.40);
  --aurora-modal-backdrop: rgba(0,0,0,0.78);
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: system-ui, sans-serif; background: var(--aurora-bg); color: var(--aurora-text); height: 100dvh; display: flex; flex-direction: column; overflow-x: hidden; }

#toolbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; background: var(--aurora-surface); border-bottom: 1px solid var(--aurora-line);
  flex-shrink: 0;
}
#toolbar h1 { font-size: 15px; font-weight: 600; letter-spacing: 0.04em; color: var(--aurora-signal); }

select, button {
  padding: 6px 12px; border-radius: 6px; border: 1px solid var(--aurora-line);
  background: var(--aurora-surface-2); color: var(--aurora-text); font-size: 13px; cursor: pointer;
}
button:hover { background: var(--aurora-hover); }
button.primary { background: var(--aurora-signal); border-color: var(--aurora-signal); color: #FFFFFF; font-weight: 600; }
button.primary:hover { opacity: 0.85; }

#layer-toggle { display: flex; gap: 6px; margin-left: auto; }
.layer-btn { padding: 5px 10px; font-size: 12px; }
.layer-btn.active { background: var(--aurora-signal-soft); border-color: var(--aurora-signal); color: var(--aurora-signal); font-weight: 500; }

#nearby-badge {
  /* Floats over the map at top-right rather than living inside the toolbar.
     Stops the badge's appearance/disappearance from shifting toolbar items
     (was a real flex member competing with #community-select for width).
     z-index sits above Leaflet (400) but below the comm-panel (1500). */
  position: absolute; top: 12px; right: 12px; z-index: 1000;
  font-size: 12px; color: var(--aurora-text-mute); padding: 4px 10px;
  background: var(--aurora-overlay-bg); border: 1px solid var(--aurora-line); border-radius: 999px;
  box-shadow: var(--aurora-shadow-soft);
  pointer-events: none;
}
#user-badge {
  font-size: 12px; color: var(--aurora-text-mute); padding: 4px 10px;
  background: var(--aurora-surface-2); border: 1px solid var(--aurora-line); border-radius: 6px;
}
#logout-btn { font-size: 12px; padding: 5px 10px; color: var(--aurora-text-mute); }

/* Theme toggle and share button — always visible (also on mobile) */
#theme-toggle, #share-btn {
  padding: 5px 10px; font-size: 14px; line-height: 1;
  background: var(--aurora-surface-2); border: 1px solid var(--aurora-line); border-radius: 6px;
  color: var(--aurora-text); cursor: pointer; min-width: 32px;
}
#theme-toggle:hover, #share-btn:hover { background: var(--aurora-hover); }
#theme-toggle-icon { display: inline-block; }

#main-container { display: flex; flex: 1; position: relative; }
#map { flex: 1; }

/* Floating Action Button */
#fab {
  position: fixed; bottom: 24px; right: 24px;
  width: 56px; height: 56px; border-radius: 50%;
  background: var(--aurora-signal); border: none;
  color: #FFFFFF; font-size: 28px; font-weight: 300;
  box-shadow: var(--aurora-shadow-soft);
  cursor: pointer; z-index: 1400; display: none;
  align-items: center; justify-content: center;
  transition: transform 0.2s, box-shadow 0.2s;
}
#fab:hover { transform: scale(1.08); box-shadow: var(--aurora-shadow-strong); }
#fab:active { transform: scale(0.96); }

/* Communities panel – base styles must come BEFORE media queries so they can override */
#comm-panel {
  position: fixed; left: 0; right: 0; top: 0; width: 100vw; height: auto; max-height: 70vh;
  background: var(--aurora-surface); border-bottom: 1px solid var(--aurora-line);
  z-index: 1500; display: none; flex-direction: column;
  box-shadow: var(--aurora-popup-shadow);
  overflow: hidden;
  transform: translateY(-100%);
  transition: transform 0.3s ease-out;
}
#comm-panel.open { display: flex; transform: translateY(0); }

/* Drawer backdrop – mobile and tablet */
#drawer-overlay {
  display: none; position: fixed; inset: 0; background: var(--aurora-modal-backdrop);
  z-index: 1400; /* below drawer (1500), above map */
}
@media (max-width: 1023px) {
  #drawer-overlay.open { display: block; }
}

/* Burger button – only visible on mobile */
#burger-btn {
  display: none;
  padding: 6px 10px; font-size: 18px; line-height: 1;
  background: var(--aurora-surface-2); border: 1px solid var(--aurora-line); border-radius: 6px;
  color: var(--aurora-text); cursor: pointer;
}
#burger-btn:hover { background: var(--aurora-hover); }

/* Mobile first: < 500px */
@media (max-width: 499px) {
  body { display: flex; flex-direction: column; }
  #toolbar { flex-shrink: 0; gap: 8px; }
  #main-container { flex: 1; flex-direction: column; }
  /* Drawer from top on mobile */
  #comm-panel {
    display: flex !important;
    position: fixed; left: 0; right: 0; top: 0; width: 100vw; height: auto; max-height: 70vh;
    z-index: 1500;
    border-bottom: 1px solid var(--aurora-line);
    border-left: none;
    overflow: hidden;
  }
  #fab { display: flex; }
  #map { position: relative; }
  /* hide text button, show burger icon instead */
  #comm-btn { display: none !important; }
  #burger-btn { display: flex; align-items: center; justify-content: center; }
  /* Show community select, hide layer controls on mobile */
  #community-select { display: block; flex: 1; min-width: 0; }
  #layer-toggle { display: none; }
  #toolbar button:not(#burger-btn):not(#theme-toggle):not(#share-btn) { display: none; }
  #user-badge { display: none !important; }
  #logout-btn { display: none !important; }
  #mobile-profile-section { display: block !important; }
}

/* Tablet: 500px - 1023px */
@media (min-width: 500px) and (max-width: 1023px) {
  #main-container { flex-direction: column; }
  /* Drawer from top on tablet */
  #comm-panel {
    position: fixed; left: 0; right: 0; top: 0; width: 100vw; height: auto; max-height: 60vh;
    z-index: 1500;
    border-bottom: 1px solid var(--aurora-line);
    border-left: none;
    overflow: hidden;
  }
  #comm-panel.open { transform: translateY(0); }
  #comm-close { display: none; }
  #fab { display: flex; }
}

/* Desktop: 1024px+ */
@media (min-width: 1024px) {
  #main-container { flex-direction: row; }
  /* Show sidebar on desktop */
  #comm-panel {
    position: relative !important; left: auto !important; right: auto !important; top: auto !important;
    width: 300px; height: auto; max-height: 100vh;
    border-right: 1px solid var(--aurora-line); border-bottom: none;
    z-index: auto;
    transform: none !important;
    display: flex !important;
  }
  #fab { display: flex; }
  #comm-btn { display: none !important; }
  .comm-close { display: none; }
}

#status {
  position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%);
  background: var(--aurora-overlay-bg); border: 1px solid var(--aurora-line);
  padding: 6px 14px; border-radius: 20px; font-size: 12px; color: var(--aurora-text-mute);
  pointer-events: none; z-index: 1000; white-space: nowrap;
}

/* Auth modal */
#auth-modal {
  position: fixed; inset: 0;
  background: var(--aurora-modal-backdrop);
  z-index: 2000;
  display: flex; align-items: center; justify-content: center;
}
.auth-box {
  background: var(--aurora-surface); border: 1px solid var(--aurora-line); border-radius: 12px;
  padding: 28px 24px; width: 90%; max-width: 340px;
}
.auth-logo { font-size: 18px; font-weight: 700; color: var(--aurora-signal); text-align: center; margin-bottom: 22px; letter-spacing: 0.05em; }
.auth-tabs { display: flex; border-bottom: 1px solid var(--aurora-line); margin-bottom: 20px; }
.auth-tab {
  flex: 1; padding: 8px; font-size: 13px; text-align: center; cursor: pointer;
  background: none; border: none; border-radius: 0; color: var(--aurora-text-dim);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.auth-tab:hover { background: none; color: var(--aurora-signal); }
.auth-tab.active { color: var(--aurora-signal); border-bottom-color: var(--aurora-signal); }
.auth-input {
  width: 100%; padding: 8px 10px; margin-bottom: 10px;
  background: var(--aurora-surface-2); border: 1px solid var(--aurora-line); border-radius: 6px;
  color: var(--aurora-text); font-size: 13px;
}
.auth-input:focus { outline: none; border-color: var(--aurora-signal); }
.auth-error { color: #f87171; font-size: 12px; margin-bottom: 10px; min-height: 16px; }
.auth-submit { width: 100%; padding: 9px; font-size: 13px; }

.comm-panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--aurora-line);
  flex-shrink: 0;
}
#comm-panel > div[style] {
  width: 100vw;
  max-width: 100%;
  overflow: hidden !important;
  overflow-y: auto !important;
}
.comm-panel-header {
  width: 100vw;
  max-width: 100%;
  box-sizing: border-box;
  padding-right: 16px;
}
.comm-panel-header h2 { font-size: 14px; font-weight: 600; color: var(--aurora-signal); }
.comm-close { background: none; border: none; color: var(--aurora-text-dim); font-size: 18px; padding: 0 4px; flex-shrink: 0; }
.comm-close:hover { background: none; color: var(--aurora-text); }
#comm-panel > div { min-width: 0; width: 100vw; max-width: 100%; overflow: hidden !important; box-sizing: border-box; }
.comm-section {
  width: 100vw;
  max-width: 100%;
  padding: 12px 16px;
  border-bottom: 1px solid var(--aurora-line);
  overflow: hidden;
  box-sizing: border-box;
}
.comm-section h3 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--aurora-text-dim); margin-bottom: 10px; }
.comm-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 0;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  min-width: 0;
  width: 100%;
  overflow: hidden;
}
.comm-item:last-child { border-bottom: none; }
.comm-item-name {
  font-size: 13px;
  min-width: 0;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.comm-item-meta {
  font-size: 11px;
  color: var(--aurora-text-dim);
  min-width: 0;
  width: 100%;
  overflow: hidden;
}
.comm-btn {
  font-size: 11px;
  padding: 6px 10px;
  border-radius: 4px;
  width: 100%;
  min-width: 0;
}
.comm-btn-join { background: var(--aurora-signal-soft); border-color: var(--aurora-signal); color: var(--aurora-signal); font-weight: 500; }
.comm-btn-join:hover { background: var(--aurora-signal); color: var(--aurora-bg); }
.comm-btn-leave { background: var(--aurora-surface-2); border-color: var(--aurora-line); color: var(--aurora-text-mute); }
.comm-btn-leave:hover { background: var(--aurora-hover); color: var(--aurora-text); }
.comm-empty { font-size: 12px; color: var(--aurora-text-dim); text-align: center; padding: 16px 0; }

/* Themed popup — surface + text follow the active theme */
.leaflet-popup-content-wrapper {
  background: var(--aurora-surface); border: 1px solid var(--aurora-line); border-radius: 8px;
  box-shadow: var(--aurora-popup-shadow); color: var(--aurora-text);
}
.leaflet-popup-tip { background: var(--aurora-surface); }
.leaflet-popup-content { margin: 12px 14px; }

/* Halo animation for active beacons */
@keyframes beacon-pulse {
  0% { transform: scale(1); opacity: 0.5; }
  100% { transform: scale(2.5); opacity: 0; }
}
.beacon-marker {
  width: 44px !important;
  height: 44px !important;
  display: block;
}
.beacon-pin {
  width: 44px; height: 44px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 700; color: white;
  box-shadow: inset 0 0 0 3px rgba(0,0,0,0.3), var(--aurora-shadow-soft);
  position: relative;
}
.beacon-pin.with-halo::before,
.beacon-pin.with-halo::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 50%;
  background: var(--beacon-halo-color, white);
  animation: beacon-pulse 2.4s ease-out infinite;
  pointer-events: none;
}
.beacon-pin.with-halo::after {
  animation-delay: 1.2s;
}

/* Respect user's motion preferences */
@media (prefers-reduced-motion: reduce) {
  .beacon-pin.with-halo::before,
  .beacon-pin.with-halo::after {
    animation: none;
    opacity: 0.3;
    transform: scale(1.3);
  }
}

/* Create-beacon form inside popup */
.bc-form { min-width: 210px; }
.bc-form h3 { font-size: 13px; font-weight: 600; color: var(--aurora-signal); margin-bottom: 10px; }
.bc-form input, .bc-form select {
  width: 100%; padding: 6px 8px; margin-bottom: 7px;
  background: var(--aurora-surface-2); border: 1px solid var(--aurora-line); border-radius: 5px;
  color: var(--aurora-text); font-size: 12px;
}

/* Visibility chip group */
.bc-visibility-chips {
  display: flex; gap: 6px; margin-bottom: 7px;
}
.bc-visibility-chip {
  flex: 1; padding: 6px 8px; font-size: 12px; border-radius: 5px;
  background: var(--aurora-surface-2); border: 1px solid var(--aurora-line);
  color: var(--aurora-text); cursor: pointer; transition: all 0.2s;
}
.bc-visibility-chip:hover { background: var(--aurora-hover); }
.bc-visibility-chip.active {
  background: var(--aurora-signal-soft); border-color: var(--aurora-signal);
  color: var(--aurora-signal); font-weight: 600;
}

.bc-form .bc-actions { display: flex; gap: 6px; margin-top: 2px; }
.bc-form .bc-actions button {
  flex: 1; padding: 6px; font-size: 12px; border-radius: 5px; cursor: pointer;
}
.bc-form .bc-post { background: var(--aurora-signal); border-color: var(--aurora-signal); color: #FFFFFF; font-weight: 600; }
.bc-form .bc-post:hover { opacity: 0.85; }
.bc-form .bc-cancel { background: var(--aurora-surface-2); border-color: var(--aurora-line); color: var(--aurora-text-mute); }

/* Error banner */
#error-banner {
  position: fixed; top: 0; left: 0; right: 0; z-index: 1600;
  background: rgba(239, 68, 68, 0.95); color: white;
  padding: 12px 16px; font-size: 13px; display: none;
  flex-direction: row; align-items: center; justify-content: space-between;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  backdrop-filter: blur(4px);
}
#error-banner.show { display: flex; animation: slideDown 0.3s ease-out; }
@keyframes slideDown {
  from { transform: translateY(-100%); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
#error-banner-close {
  background: none; border: none; color: white; cursor: pointer; padding: 0;
  font-size: 16px; font-weight: 300;
}
#error-banner-close:hover { opacity: 0.8; }

/* Empty map message */
#empty-map-overlay {
  position: fixed; top: 48px; bottom: 0; left: 0; right: 0;
  background: var(--aurora-empty-overlay-bg); z-index: 1300;
  display: none; align-items: center; justify-content: center;
  flex-direction: column; gap: 16px; color: var(--aurora-text-dim);
  font-size: 14px; text-align: center; pointer-events: none;
}
#empty-map-overlay.show { display: flex; }

/* Anonymous mode CTA banner — bottom of viewport, invites login.
   Visible only when body.anonymous (set by enterAnonymousMode in app.js). */
#anon-cta {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1500;
  display: none; align-items: center; justify-content: center; gap: 14px;
  padding: 12px 16px;
  background: var(--aurora-surface); border-top: 1px solid var(--aurora-line);
  box-shadow: var(--aurora-popup-shadow);
}
body.anonymous #anon-cta { display: flex; }
#anon-cta .cta-text { font-size: 13px; color: var(--aurora-text-mute); }
#anon-cta .cta-btn { padding: 7px 16px; font-size: 13px; }
@media (max-width: 499px) {
  #anon-cta { flex-direction: column; gap: 8px; padding: 10px 14px; }
  #anon-cta .cta-text { font-size: 12px; text-align: center; }
  #anon-cta .cta-btn { width: 100%; }
}

/* Anonymous mode hides authenticated-only UI (FAB, badges, comm/burger, logout).
   These selectors win against the responsive #fab { display: flex } rules. */
body.anonymous #fab,
body.anonymous #user-badge,
body.anonymous #logout-btn,
body.anonymous #comm-btn,
body.anonymous #burger-btn,
body.anonymous #nearby-badge { display: none !important; }
/* Auto-leave a bottom gap so the CTA doesn't cover the map's bottom edge */
body.anonymous #map { padding-bottom: 56px; }
