/**
 * Map Isolation CSS - Simplified and Focused
 * 
 * Provides clean CSS isolation for multiple maps on the same page,
 * preventing conflicts between map instances.
 * UPDATED: Simplified rules and better conflict resolution
 */

/* ========================
   BASE ISOLATION RULES
   ======================== */

/* Ensure home map has proper container setup */
.eternal-roam-home-map-container {
    position: relative !important;
    height: 700px !important;
    width: 100% !important;
    z-index: 1 !important;
}

.eternal-roam-home-interactive-map {
    width: 100% !important;
    height: 100% !important;
    z-index: 1 !important;
    background-color: #f8f8f8 !important;
}

/* Journey map containers */
.journey-map-container {
    position: relative !important;
    height: 600px !important;
    max-height: 90vh !important;
    z-index: 2 !important;
}

/* ========================
   MAPREGISTRY INTEGRATION - ENHANCED
   ======================== */

/* FIXED: Enhanced conflict detection and resolution */
.maps-conflict-detected .map-primary-container {
    z-index: 200 !important; /* FIXED: Higher z-index for priority */
    opacity: 1 !important;
    visibility: visible !important;
    pointer-events: auto !important;
}

.maps-conflict-detected .map-primary-container *,
.maps-conflict-detected .map-primary-container .leaflet-control,
.maps-conflict-detected .map-primary-container .map-style-controls {
    pointer-events: auto !important;
    z-index: inherit !important;
}

.maps-conflict-detected .map-secondary-container {
    z-index: 1 !important;
    opacity: 0.3 !important;
    pointer-events: none !important;
    visibility: visible !important; /* Keep visible but non-interactive */
}

.maps-conflict-detected .map-secondary-container *,
.maps-conflict-detected .map-secondary-container .leaflet-control,
.maps-conflict-detected .map-secondary-container .map-style-controls {
    pointer-events: none !important;
}

/* FIXED: Enhanced journey map priority when active */
body.journey-map-active .eternal-roam-home-map-container {
    z-index: 1 !important;
    opacity: 0.3 !important;
    pointer-events: none !important;
    transition: opacity 0.3s ease;
}

body.journey-map-active .eternal-roam-home-map-container *,
body.journey-map-active .eternal-roam-home-map-container .leaflet-control {
    pointer-events: none !important;
}

/* FIXED: Ensure journey map takes complete precedence when active */
body.journey-map-active .journey-map-container {
    z-index: 200 !important;
    opacity: 1 !important;
    visibility: visible !important;
}

body.journey-map-active .journey-map-container *,
body.journey-map-active .journey-map-container .leaflet-control,
body.journey-map-active .journey-map-container .map-style-controls {
    pointer-events: auto !important;
    z-index: inherit !important;
}

/* ========================
   LEAFLET SPECIFIC FIXES - ENHANCED
   ======================== */

/* FIXED: Enhanced z-index layering for Leaflet components */
.leaflet-map-pane {
    z-index: 10 !important;
}

.leaflet-tile-pane {
    z-index: 1 !important;
}

.leaflet-overlay-pane {
    z-index: 20 !important;
}

.leaflet-shadow-pane {
    z-index: 30 !important;
}

.leaflet-marker-pane {
    z-index: 40 !important;
}

.leaflet-tooltip-pane {
    z-index: 50 !important;
}

.leaflet-popup-pane {
    z-index: 60 !important;
}

.leaflet-map-pane canvas,
.leaflet-map-pane svg {
    z-index: 1 !important;
}

/* FIXED: Enhanced control positioning */
.leaflet-top, 
.leaflet-bottom {
    z-index: 1000 !important;
    pointer-events: none; /* Allow click-through except for controls */
}

.leaflet-control {
    z-index: 1001 !important;
    pointer-events: auto !important;
}

/* FIXED: Map style controls get highest priority */
.map-style-controls {
    z-index: 1100 !important;
    pointer-events: auto !important;
}

/* FIXED: Popup and tooltip priority */
.leaflet-popup {
    z-index: 1200 !important;
}

.leaflet-tooltip {
    z-index: 1150 !important;
}

/* ========================
   BUTTON AND CONTROL FIXES
   ======================== */

/* Ensure home map buttons work when not conflicted */
.eternal-roam-home-map-sidebar button,
.eternal-roam-home-map-sidebar .filter-button,
.eternal-roam-home-map-sidebar .dropdown-header,
.eternal-roam-home-map-sidebar input,
.eternal-roam-home-map-sidebar select {
    position: relative !important;
    z-index: 1000 !important;
    pointer-events: auto !important;
}

/* Disable home map interactions when journey map is active */
body.journey-map-active .eternal-roam-home-map-sidebar button,
body.journey-map-active .eternal-roam-home-map-sidebar .filter-button,
body.journey-map-active .eternal-roam-home-map-sidebar .dropdown-header,
body.journey-map-active .eternal-roam-home-map-sidebar input,
body.journey-map-active .eternal-roam-home-map-sidebar select {
    pointer-events: none !important;
    opacity: 0.5 !important;
}

/* Ensure journey map controls work when active */
body.journey-map-active .journey-map-container button,
body.journey-map-active .journey-map-container .leaflet-control-zoom a,
body.journey-map-active .journey-map-container .leaflet-control {
    z-index: 1500 !important;
    pointer-events: auto !important;
}

/* ========================
   ERROR HANDLING
   ======================== */

/* Map error messages */
.map-error-message {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    min-height: 300px;
    background: #f9f9f9;
    border: 2px dashed #ddd;
    border-radius: 8px;
    text-align: center;
    padding: 20px;
}

.map-error-icon {
    font-size: 48px;
    margin-bottom: 15px;
    color: #ff6b6b;
}

.map-error-text {
    font-size: 16px;
    color: #666;
    margin-bottom: 20px;
    max-width: 300px;
}

.map-error-retry {
    background: #8b0000;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.3s ease;
}

.map-error-retry:hover {
    background: #660000;
}

/* Journey planner error messages */
.journey-planner-error {
    background: #fff3cd;
    border: 1px solid #ffeaa7;
    border-radius: 8px;
    padding: 20px;
    margin: 20px 0;
    text-align: center;
}

.journey-planner-error .error-icon {
    font-size: 32px;
    color: #856404;
    margin-bottom: 10px;
}

.journey-planner-error .error-message {
    color: #856404;
    font-size: 16px;
    margin-bottom: 15px;
}

.journey-planner-error .error-retry {
    background: #856404;
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
}

.journey-planner-error .error-retry:hover {
    background: #6c5300;
}

/* ========================
   RESPONSIVE FIXES
   ======================== */

@media (max-width: 768px) {
    /* Cap maps so they never dominate the iPhone viewport */
    .eternal-roam-home-map-container {
        height: min(60vh, 400px) !important;
    }

    .journey-map-container {
        height: min(60vh, 450px) !important;
    }
    
    .map-error-message {
        min-height: 200px;
        padding: 15px;
    }
    
    .map-error-icon {
        font-size: 36px;
    }
    
    .map-error-text {
        font-size: 14px;
    }
}

/* ========================
   WEBKIT FIXES
   ======================== */

.leaflet-container {
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

/* ========================
   LOADING SPINNER STYLES
   ======================== */

.map-loading-spinner {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 1000;
    background: rgba(255, 255, 255, 0.9);
    padding: 20px;
    border-radius: 8px;
    text-align: center;
}

.map-loading-spinner.hidden {
    display: none !important;
}

.map-loading-spinner .spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #8b0000;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 10px auto;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.map-loading-spinner span {
    color: #666;
    font-size: 14px;
}