/* map.css - Scaled down responsive version */
#map-container {
    top: 5rem;
    position: fixed; /* Changed from absolute to fixed to escape body overflow:hidden */
    left: 0; /* Explicit positioning */
    right: 0; /* Explicit positioning */
    bottom: 0; /* Use full viewport height */
    width: 100vw; /* Use viewport width to escape parent constraints */
    height: calc(100vh - 5rem); /* Use viewport height to escape parent constraints */
    background-color: rgba(0,0,0,0.9);
    z-index: 1000;
    box-sizing: border-box;
    /* Critical: Override any parent overflow or transform constraints */
    transform: none !important;
    clip: none !important;
    overflow: hidden; /* Changed from visible to contain map-display */
    /* Remove flex centering to allow map-display to fill the space */
}

#map-container.hidden {
    display: none !important;
    /* Ensure hidden state takes precedence over all visibility fixes */
    visibility: hidden !important;
    opacity: 0 !important;
    pointer-events: none !important;
}

/* Ensure map container is visible when not hidden - CRITICAL OVERRIDES */
#map-container:not(.hidden) {
    display: block !important;
    opacity: 1 !important;
    visibility: visible !important;
    pointer-events: auto !important;
    /* Force visibility even if parent containers have issues */
    position: fixed !important;
    overflow: visible !important;
    transform: none !important;
    clip: none !important;
    mask: none !important;
    filter: none !important;
    backdrop-filter: none;
    /* Ensure content is not clipped by parent containers */
    contain: none !important;
    isolation: auto !important;
}


#map-display {
    width: 100%;
    height: 100%;
    overflow: scroll;
    overflow-x: scroll;
    overflow-y: scroll;
    background-color: #1a1a1a;
    cursor: default;
    /* Add subtle grid pattern for visual feedback when dragging */
    background-image: 
        linear-gradient(rgba(255,255,255,0.03) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255,255,255,0.03) 1px, transparent 1px);
    background-size: 50px 50px;
    border-radius: 8px;
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
    position: relative;
    z-index: 1001;
    /* Essential visibility without !important interference */
    display: block;
    opacity: 1;
    visibility: visible;
    /* Prevent text selection during panning */
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    /* Removed smooth scrolling to improve pan responsiveness */
    /* scroll-behavior: smooth; */
    /* Ensure pointer events work properly */
    pointer-events: auto;
    /* Allow touch panning on mobile */
    touch-action: pan-x pan-y;
}

/* Cursor states for map panning */
#map-display.grabbable {
    cursor: grab;
    cursor: -webkit-grab;
    cursor: -moz-grab;
}

#map-display.grabbing {
    cursor: grabbing;
    cursor: -webkit-grabbing;
    cursor: -moz-grabbing;
}

.map-grid {
    position: relative;
    transform-origin: center center;
    padding: 100px;
    /* Use CSS custom properties for responsive sizing */
    /* Remove viewport constraints to allow full scrolling */
    width: var(--map-width, 2400px);
    height: var(--map-height, 2400px);
    /* Ensure minimum size for usability */
    min-width: 1200px;
    min-height: 1200px;
    z-index: 1002;
    /* Allow JavaScript transforms for zoom functionality */
    transition: transform 0.2s ease;
    /* Visibility without !important to allow JS control */
    display: block;
    opacity: 1;
    visibility: visible;
}

/* Auto-scale the map to fit the container */
@media (max-width: 768px) {
    #map-display {
        width: 100%; /* Use full width instead of 95% */
        height: 100%; /* Use full height instead of 85% */
        max-width: none;
        max-height: none;
    }

    .map-grid {
        padding: 10px;
        /* Removed fixed scale - zoom controls handle this now */
    }

    /* Keep consistent room sizes - zoom handles scaling */
    .map-room {
        width: 80px;
        height: 80px;
        margin: 8px;
    }

    .map-room-name {
        font-size: 10px;
        padding: 3px 5px;
        max-width: 85%;
    }
}

@media (max-width: 480px) {
    .map-grid {
        /* Removed fixed scale - zoom controls handle this now */
    }

    /* Keep consistent room sizes - zoom handles scaling */
    .map-room {
        width: 70px;
        height: 70px;
        margin: 6px;
    }

    .map-room-name {
        font-size: 10px;
        padding: 3px;
    }
}

.map-room {
    position: absolute;
    width: 100px;
    height: 100px;
    border: 2px solid #666;
    border-radius: 8px;
    background-color: #2a2a2a;
    display: flex !important;
    flex-direction: column !important;
    justify-content: center !important;
    align-items: center !important;
    transition: all 0.2s ease;
    cursor: pointer;
    overflow: visible !important; /* Changed from hidden to visible */
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
    z-index: 1003;
    /* Add margin for better spacing */
    margin: 10px;
    /* Allow hover transforms while maintaining visibility */
    opacity: 1;
    visibility: visible;
    /* Reset cursor for rooms */
    cursor: pointer;
}

.map-room:hover {
    transform: scale(1.1); /* Slightly more hover effect */
    box-shadow: 0 4px 8px rgba(0,0,0,0.4);
    z-index: 10;
    border-color: #666;
}

.map-room.current {
    border: 2px solid #f66;
    background-color: #332222;
    box-shadow: 0 0 10px #f66; /* Smaller glow */
}

.map-room-name {
    font-size: 12px;
    color: #ffffff;
    text-align: center;
    padding: 4px 6px;
    font-weight: 600;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.9);
    line-height: 1.2;
    word-wrap: break-word;
    width: auto;
    max-width: 90%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    background-color: rgba(0,0,0,0.85);
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: 4px;
    position: relative;
    margin: 0 auto;
    z-index: 1004;
    pointer-events: none;
    /* Essential visibility */
    display: block;
    opacity: 1;
    visibility: visible;
}

/* Remove exit indicators since we're only showing names and lines */
.map-exit {
    display: none; /* Hide exit indicators completely */
}

#map-controls {
    position: absolute;
    top: 80px; /* Move below menu bar (was 60px) */
    right: 10px;
    display: flex;
    gap: 10px;
    z-index: 1010;
}

/* Removed - positioning moved to main #exit-map rule below */

.map-control-btn {
    background-color: rgba(0,0,0,0.8);
    color: white;
    border: 1px solid #666;
    border-radius: 6px;
    padding: 8px 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    font-size: 16px;
    font-weight: bold;
    min-width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    /* Ensure buttons stay on top but don't block map interactions */
    pointer-events: auto;
    user-select: none;
}

.map-control-btn:hover {
    background-color: rgba(255,100,100,0.2);
    border-color: #f66;
}

#exit-map {
    position: absolute;
    top: 80px; /* Move below menu bar (was 60px) */
    left: 10px;
    z-index: 1010;
    background-color: rgba(0,0,0,0.7);
    color: white;
    border: 1px solid #666;
    border-radius: 4px;
    padding: 6px 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    font-size: 12px;
}

#exit-map:hover {
    background-color: rgba(255,100,100,0.2);
    border-color: #f66;
}

/* Ensure the map scrolls smoothly */
#map-display::-webkit-scrollbar {
    width: 6px;  /* Thinner scrollbar */
    height: 6px;
}

#map-display::-webkit-scrollbar-track {
    background: #333;
    border-radius: 3px;
}

#map-display::-webkit-scrollbar-thumb {
    background: #666;
    border-radius: 3px;
}

#map-display::-webkit-scrollbar-thumb:hover {
    background: #888;
}

/* Ensure map elements maintain their layout properties */
#map-container,
#map-display,
.map-grid {
    /* Clean slate - remove any inherited constraints */
    contain: none;
    isolation: auto;
    /* No clipping or masking */
    clip: none;
    clip-path: none;
    mask: none;
    mask-image: none;
}

/* Ensure rooms display as flex containers for proper centering */
.map-room {
    display: flex;
}

/* CRITICAL FIX: Override body overflow:hidden when map is visible */
body:has(#map-container:not(.hidden)) {
    overflow: visible !important;
}

/* Fallback for browsers without :has() support */
body.map-visible {
    overflow: visible !important;
}


/* EMERGENCY VISIBILITY FIX - Force elements to be within viewport */
#map-container:not(.hidden) {
    /* Ensure map is always within viewport bounds */
    max-width: 100vw !important;
    max-height: 100vh !important;
    /* Force it to be visible even if parent containers clip content */
    position: fixed !important;
    inset: 0 !important; /* Modern shorthand for top:0, right:0, bottom:0, left:0 */
    /* Override any negative margins or transforms */
    margin: 0 !important;
    transform: none !important;
    /* Ensure it's on top of everything */
    z-index: 1000;
}

/* Ensure the map display uses the full container */
#map-container:not(.hidden) #map-display {
    position: absolute !important;
    top: 4rem !important; /* Account for header */
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: auto !important;
    height: auto !important;
}

/* Make sure map grid is visible */
#map-container:not(.hidden) .map-grid {
    /* Add a minimum width/height to ensure it exists */
    min-width: 100px !important;
    min-height: 100px !important;
}

/* Ensure map displays correctly on mobile */
@media (max-width: 768px) {
    #map-container {
        top: 5rem !important; /* Account for mobile nav bar */
        height: 100vh !important; /* Use full viewport height */
        padding-top: 4rem; /* Add padding instead of top positioning */
        box-sizing: border-box;
    }
    
    #map-display {
        /* Ensure full area usage on mobile */
        margin: 0 !important;
        padding: 0 !important;
        left: 0 !important;
        top: 0 !important;
        position: relative !important;
    }
}