/* Leaflet & MarkerCluster Overrides */
.leaflet-marker-icon,
.leaflet-marker-shadow {
    max-width: none !important;
}
.leaflet-popup-content-wrapper {
    padding: 0;
    overflow: hidden;
    border-radius: 0.5rem;
    max-width: calc(100vw - 32px); /* Never wider than viewport */
}
.leaflet-popup-content {
    margin: 0;
    width: auto !important;
    min-width: 220px;
    max-width: calc(100vw - 32px);
}
.leaflet-container a.leaflet-popup-close-button {
    color: white;
    padding: 8px 10px;
    z-index: 1002;
    font-size: 20px;
}
.leaflet-container a.leaflet-popup-close-button:hover {
    color: #ffd0d0;
}

/* Cluster styling */
.marker-cluster-small {
    background-color: rgba(6, 147, 227, 0.6) !important;
}
.marker-cluster-small div {
    background-color: rgba(6, 147, 227, 0.9) !important;
    color: white !important;
}
.marker-cluster-medium {
    background-color: rgba(6, 147, 227, 0.6) !important;
}
.marker-cluster-medium div {
    background-color: rgba(6, 147, 227, 0.9) !important;
    color: white !important;
}
.marker-cluster-large {
    background-color: rgba(6, 147, 227, 0.6) !important;
}
.marker-cluster-large div {
    background-color: rgba(6, 147, 227, 0.9) !important;
    color: white !important;
}

/* Custom Marker styling if using DivIcon */
.unsa-custom-marker {
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    filter: drop-shadow(2px 4px 6px rgba(0,0,0,0.3));
    transition: transform 0.2s ease-in-out;
}
.unsa-custom-marker:hover {
    transform: scale(1.1);
}

/* Results Sidebar highlights */
.unsa-result-item {
    cursor: pointer;
    border-radius: 0.375rem; /* rounded-md */
    transition: background-color 0.2s;
}
.unsa-result-item:hover {
    background-color: #f3f4f6;
}
.dark .unsa-result-item:hover {
    background-color: #1e293b;
}

/* Hide leaflet attribution if needed or style it */
.leaflet-control-attribution {
    font-size: 10px !important;
}

/* Base custom SVG pin support */
.svg-marker path {
    fill: var(--unsa-primary);
}

/* Modern Search Styling */
.unsa-modern-search {
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
}
.unsa-modern-search:focus {
    border-color: var(--unsa-primary) !important;
    box-shadow: 0 0 0 4px rgba(14, 79, 169, 0.1);
    background-color: white;
}
.dark .unsa-modern-search:focus {
    background-color: #0f172a;
}

#unsa-domtom-insets {
    left: 20px !important;
    right: auto !important;
    bottom: 20px !important;
    transform-origin: bottom left !important;
    justify-content: flex-start !important;
}
.leaflet-popup-pane {
    z-index: 2000 !important; /* Above everything including DOM-TOM insets */
}

/* Allow DOM-TOM insets to show popups outside their 80x80 box */
#unsa-domtom-insets {
    pointer-events: none; /* Allow clicks to pass through the wrapper if needed */
}
#unsa-domtom-insets > div,
[id^="unsa-map-inset-"] {
    overflow: visible !important;
    pointer-events: auto; /* Re-enable for the actual maps */
}

.leaflet-container {
    overflow: visible !important;
}
/* But map container itself should clip map data correctly, 
   Leaflet popups are usually in a different pane, let's target the pane */
.leaflet-map-pane {
    z-index: 2;
}
.leaflet-popup-pane {
    z-index: 1001; /* Above everything */
}

/* Fix for DOM-TOM popups - Now follows the "below" rule like others */
.unsa-domtom-popup {
    /* Legacy side positioning removed for consistency */
}

/* ============================================================
   MOBILE RESPONSIVE — Map + Sidebar + Popups
   ============================================================ */

/* --- Mobile: map fills viewport, sidebar becomes a top overlay --- */
@media (max-width: 767px) {
    /* Main container: full viewport height, column layout */
    #unsa-map-container {
        height: 100svh !important;      /* svh = small viewport height, respects browser chrome */
        height: 100vh !important;       /* fallback for older browsers */
        min-height: 480px !important;
        flex-direction: column !important;
        overflow: hidden !important;
        border-radius: 0 !important;    /* Edge-to-edge on mobile */
    }

    /* Maps wrapper takes all remaining space */
    #unsa-map-container > div:last-child {
        flex: 1 1 auto !important;
        min-height: 0 !important;
        order: 0 !important;            /* Map first */
    }

    /* Sidebar becomes a floating overlay bar at the top */
    #unsa-map-sidebar {
        position: absolute;
        top: 12px;
        left: 12px;
        right: 12px;
        width: auto;
        height: auto;
        flex: none !important;
        background: transparent !important;
        border: none !important;
        pointer-events: none;
        z-index: 2100;
    }
    #unsa-map-sidebar > div:first-child {
        pointer-events: auto;
        padding: 0 !important;
        background: transparent !important;
        border: none !important;
    }
    .unsa-modern-search {
        box-shadow: 0 4px 16px rgba(0,0,0,0.18) !important;
        background: white !important;
    }
    .dark .unsa-modern-search {
        background: #1e293b !important;
    }
    /* Results dropdown */
    #unsa-map-results {
        display: none;
        position: absolute;
        top: 60px;
        left: 0;
        right: 0;
        max-height: 55vh;
        background: white !important;
        box-shadow: 0 10px 25px rgba(0,0,0,0.15) !important;
        border-radius: 0 0 14px 14px !important;
        z-index: 2100;
        overflow-y: auto;
        border: 1px solid #e2e8f0 !important;
        pointer-events: auto;
    }
    #unsa-map-results.is-focused,
    #unsa-map-results.has-query {
        display: block;
    }
    .dark #unsa-map-results {
        background: #1e293b !important;
        border-color: #334155 !important;
    }
    #unsa-map-results:empty {
        display: none;
    }
    .unsa-no-results {
        padding: 20px;
        text-align: center;
        color: #64748b;
        font-size: 0.9rem;
        font-style: italic;
    }

    /* DOM-TOM insets: shrink and stay bottom-left */
    #unsa-domtom-insets {
        bottom: 56px !important;        /* Above zoom controls */
        left: 8px !important;
        right: auto !important;
        max-width: 160px !important;
        transform: scale(0.80);
        transform-origin: bottom left;
        z-index: 1000 !important;
    }

    /* Leaflet zoom controls: keep at bottom-right, above insets */
    .leaflet-bottom.leaflet-right {
        bottom: 8px !important;
    }
}

/* Mobile popup refinements */
@media (max-width: 640px) {
    .leaflet-popup-content {
        min-width: 160px !important;
        max-width: calc(100vw - 32px) !important;
        width: auto !important;
    }
    .leaflet-popup-content-wrapper {
        max-width: calc(100vw - 24px) !important;
    }
    .unsa-popup h3 {
        font-size: 0.9rem !important;
    }
    .unsa-popup p {
        font-size: 0.75rem !important;
    }
    .unsa-popup .p-4 {
        padding: 0.65rem !important;
    }
    .unsa-popup .space-y-3 > * + * {
        margin-top: 0.4rem !important;
    }
    /* Inner min-width override inside popup div */
    .unsa-popup {
        min-width: 0 !important;
    }
    .unsa-popup [style*="min-width"] {
        min-width: 0 !important;
    }
}

/* ============================================================
   POPUP MODAL OVERLAY — Universal (mobile + desktop)
   Opens on every marker / sidebar click.
   ============================================================ */
#unsa-domtom-modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 99999;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    align-items: center;
    justify-content: center;
    padding: 16px;
    box-sizing: border-box;
}
#unsa-domtom-modal-overlay.is-open {
    display: flex;
    animation: unsaOverlayFadeIn 0.2s ease-out;
}
@keyframes unsaOverlayFadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}
#unsa-domtom-modal-box {
    background: white;
    border-radius: 20px;
    overflow: hidden;
    width: 100%;
    max-width: 420px;       /* Comfortable on desktop too */
    max-height: 90vh;
    overflow-y: auto;
    box-shadow:
        0 25px 60px rgba(0, 0, 0, 0.25),
        0 0 0 1px rgba(255,255,255,0.08);
    position: relative;
    animation: unsaModalSlideUp 0.28s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes unsaModalSlideUp {
    from { opacity: 0; transform: translateY(32px) scale(0.95); }
    to   { opacity: 1; transform: translateY(0)    scale(1); }
}
#unsa-domtom-modal-close {
    position: absolute;
    top: 10px;
    right: 12px;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: rgba(255,255,255,0.22);
    border: none;
    color: white;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    transition: background 0.2s, transform 0.15s;
    font-family: inherit;
}
#unsa-domtom-modal-close:hover {
    background: rgba(255,255,255,0.38);
    transform: scale(1.12);
}
.dark #unsa-domtom-modal-box {
    background: #1e293b;
    color: white;
}

/* On desktop, widen the modal slightly for more breathing room */
@media (min-width: 768px) {
    #unsa-domtom-modal-box {
        max-width: 460px;
        border-radius: 24px;
    }
    #unsa-domtom-modal-overlay {
        padding: 24px;
    }
}

/* Force popups below markers */
.leaflet-popup.unsa-popup-below {
    bottom: auto !important;
    top: 0 !important; /* Start exactly at the anchor point */
}

.unsa-popup-below .leaflet-popup-content-wrapper {
    border-radius: 12px !important;
    margin-top: 20px !important; /* Space for the tip above the bubble */
}

.unsa-popup-below .leaflet-popup-tip-container {
    top: 0 !important;
    bottom: auto !important;
    transform: rotate(180deg) !important;
    margin-top: 1px !important; /* Slight overlap to avoid gap with wrapper */
}

/* Ensure the popup container has a shadow that includes the tip */
.leaflet-popup.unsa-popup-below {
    filter: drop-shadow(0 10px 25px rgba(0,0,0,0.15)) !important;
}

/* Ensure the popup doesn't overlap the marker if it's very tall */
.unsa-popup-below {
    filter: drop-shadow(0 10px 25px rgba(0,0,0,0.15));
}
