/**
 * Project Map Page Styles
 * Handles fullscreen mode, responsive layout, and fixes sidebar overlap
 */

/* ================================
   CSS Custom Properties
   ================================ */

:root {
    --vh: 1vh; /* Will be set by JavaScript for iOS */
    --sat: env(safe-area-inset-top, 0px);
    --sab: env(safe-area-inset-bottom, 0px);
}

/* ================================
   Map Container Base Styles
   ================================ */

#map-page-container {
    width: 100%;
    position: relative;
    overflow-x: hidden;
}

#map-content-wrapper {
    width: 100%;
    position: relative;
}

#map-main-container {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

#map-container {
    position: relative;
    width: 100%;
    margin-top: 1rem;
}

#map-paper {
    position: relative;
    overflow: hidden;
    border-radius: 8px;
}

/* Ensure map fills container properly */
#project-map {
    width: 100% !important;
    height: 100% !important;
}

/* Fix for Leaflet container */
.leaflet-container {
    width: 100% !important;
    height: 100% !important;
    z-index: 1;
    border-radius: 8px;
}

/* ================================
   Fullscreen Mode
   ================================ */

/* Native fullscreen mode */
#map-content-wrapper[style*="position: fixed"] {
    background-color: #fff;
    overflow: auto;
}

#map-content-wrapper[style*="position: fixed"] #map-main-container {
    padding: 0 !important;
    max-width: 100% !important;
}

#map-content-wrapper[style*="position: fixed"] #map-container {
    margin: 0;
}

/* Pseudo-fullscreen mode for iOS/mobile - critical for mobile support */
.leaflet-pseudo-fullscreen {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100% !important;
    height: 100vh !important;
    height: calc(var(--vh, 1vh) * 100) !important; /* iOS fix */
    z-index: 99999 !important;
    margin: 0 !important;
    padding: 0 !important;
    background-color: #fff;
}

/* Ensure leaflet container fills pseudo-fullscreen properly */
.leaflet-pseudo-fullscreen .leaflet-container {
    width: 100% !important;
    height: 100vh !important;
    height: calc(var(--vh, 1vh) * 100) !important; /* iOS fix */
    position: absolute !important;
    top: 0 !important;
    left: 0 !important;
    border-radius: 0 !important;
}

/* Hide header and other elements in pseudo-fullscreen on mobile */
.leaflet-pseudo-fullscreen ~ * {
    display: none !important;
}

/* Mobile viewport adjustments for iOS Safari */
@supports (-webkit-touch-callout: none) {
    /* iOS-specific fullscreen fixes */
    .leaflet-pseudo-fullscreen {
        height: 100vh !important;
        height: -webkit-fill-available !important;
        height: calc(var(--vh, 1vh) * 100) !important;
        overflow: hidden !important;
        /* Account for safe areas on iPhone notch */
        padding-top: var(--sat) !important;
        padding-bottom: var(--sab) !important;
    }
    
    .leaflet-pseudo-fullscreen .leaflet-container {
        height: 100vh !important;
        height: -webkit-fill-available !important;
        height: calc(var(--vh, 1vh) * 100) !important;
        /* Adjust for safe area padding */
        height: calc(var(--vh, 1vh) * 100 - var(--sat) - var(--sab)) !important;
    }
}

/* Fullscreen toggle button - always visible */
#fullscreen-toggle {
    display: flex !important;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15) !important;
    transition: all 0.2s ease;
    background-color: #228be6 !important;
    cursor: pointer;
    position: absolute !important;
    top: 10px !important;
    right: 10px !important;
    z-index: 1000 !important;
}

#fullscreen-toggle:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2) !important;
    background-color: #1c7ed6 !important;
}

#fullscreen-toggle svg {
    color: white;
}

/* ================================
   Sidebar Overlap Fix
   ================================ */

/* Ensure map doesn't overlap with sidebar on larger screens */
@media (min-width: 768px) {
    #map-page-container {
        position: relative;
        z-index: 1;
        width: 100%;
        overflow-x: hidden;
    }
    
    /* Prevent content from sliding under sidebar */
    #map-content-wrapper:not([style*="position: fixed"]) {
        margin-left: 0;
        width: 100%;
        padding-left: 0;
    }
    
    /* Ensure map container respects sidebar space */
    #map-main-container {
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }
}

/* When sidebar is expanded (220px) */
@media (min-width: 901px) {
    body:not(.sidebar-collapsed) #map-page-container {
        padding-left: 0;
    }
    
    body:not(.sidebar-collapsed) #map-main-container {
        max-width: 100%;
    }
}

/* When sidebar is collapsed (48px) */
@media (min-width: 901px) {
    body.sidebar-collapsed #map-page-container {
        padding-left: 0;
    }
}

/* ================================
   Mobile Responsive Styles
   ================================ */

/* Mobile portrait */
@media (max-width: 767px) {
    #map-main-container {
        padding: 0.5rem !important;
    }
    
    #map-header-section {
        gap: 0.5rem !important;
    }
    
    /* Stack title and badges vertically on mobile */
    #map-header-section > div:first-child {
        flex-direction: column;
        align-items: flex-start !important;
    }
    
    /* Adjust map height for mobile */
    #map-paper:not([style*="100vh"]) {
        height: 60vh !important;
        min-height: 400px !important;
    }
    
    /* Smaller badges on mobile */
    .mantine-Badge-root {
        font-size: 0.75rem;
    }
    
    /* Adjust fullscreen button position on mobile */
    #fullscreen-toggle {
        top: 60px !important;
        right: 10px !important;
        z-index: 1001 !important;
    }
    
    /* Legend adjustments for mobile */
    #map-header-section .mantine-Paper-root {
        padding: 0.5rem !important;
    }
    
    #map-header-section .mantine-Group-root {
        gap: 0.5rem !important;
        font-size: 0.8rem;
    }
    
    /* Ensure pseudo-fullscreen map fills entire mobile viewport */
    .leaflet-pseudo-fullscreen {
        padding: 0 !important;
        margin: 0 !important;
    }
    
    /* Position fullscreen button within bounds on mobile fullscreen */
    .leaflet-pseudo-fullscreen .leaflet-control-fullscreen {
        top: 10px !important;
        right: 10px !important;
    }
    
    /* Hide Safari UI bars in fullscreen */
    .leaflet-pseudo-fullscreen {
        /* iOS Safari viewport height fix */
        min-height: 100vh;
        min-height: -webkit-fill-available;
    }
}

/* Mobile landscape and small tablets */
@media (min-width: 768px) and (max-width: 1024px) {
    #map-paper:not([style*="100vh"]) {
        height: 70vh !important;
        min-height: 500px !important;
    }
    
    #fullscreen-toggle {
        top: 10px !important;
    }
    
    /* iPad-specific fullscreen fixes */
    .leaflet-pseudo-fullscreen {
        /* Account for iPad notch and UI elements */
        padding: env(safe-area-inset-top, 0) 
                 env(safe-area-inset-right, 0) 
                 env(safe-area-inset-bottom, 0) 
                 env(safe-area-inset-left, 0) !important;
        height: 100vh !important;
        height: -webkit-fill-available !important;
    }
    
    .leaflet-pseudo-fullscreen .leaflet-container {
        /* Ensure map container accounts for safe areas */
        height: calc(100vh - env(safe-area-inset-top, 0) - env(safe-area-inset-bottom, 0)) !important;
        height: calc(-webkit-fill-available - env(safe-area-inset-top, 0) - env(safe-area-inset-bottom, 0)) !important;
    }
    
    /* Position controls within safe area on iPad */
    .leaflet-pseudo-fullscreen .leaflet-top {
        top: calc(10px + env(safe-area-inset-top, 0)) !important;
    }
    
    .leaflet-pseudo-fullscreen .leaflet-bottom {
        bottom: calc(10px + env(safe-area-inset-bottom, 0)) !important;
    }
}

/* Desktop */
@media (min-width: 1025px) {
    #map-paper:not([style*="100vh"]) {
        height: 75vh !important;
        min-height: 600px !important;
    }
}

/* ================================
   Map Controls Positioning
   ================================ */

/* Leaflet zoom controls - adjust position to avoid overlap */
.leaflet-top.leaflet-left {
    top: 60px;
    left: 10px;
}

/* Move attribution to bottom right */
.leaflet-bottom.leaflet-right {
    right: 10px;
    bottom: 10px;
}

/* ================================
   Map Popup and Tooltip Styles
   ================================ */

.leaflet-popup-content-wrapper {
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.leaflet-popup-content {
    margin: 12px;
    line-height: 1.5;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.leaflet-tooltip {
    background-color: rgba(0, 0, 0, 0.8);
    border: none;
    border-radius: 4px;
    color: white;
    padding: 6px 10px;
    font-size: 0.875rem;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

/* ================================
   Layer Control Styles
   ================================ */

.leaflet-control-layers {
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

/* ================================
   Loading State
   ================================ */

#map-container.loading {
    opacity: 0.6;
    pointer-events: none;
}

#map-container.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 50px;
    height: 50px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #3498db;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    z-index: 1000;
}

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

/* ================================
   Print Styles
   ================================ */

@media print {
    #map-header-section,
    #fullscreen-toggle,
    .leaflet-control-zoom,
    .leaflet-control-layers {
        display: none !important;
    }
    
    #map-paper {
        height: 100vh !important;
        border: none !important;
    }
}

/* ================================
   Accessibility Improvements
   ================================ */

/* Focus styles for keyboard navigation */
#fullscreen-toggle:focus-visible {
    outline: 3px solid #228be6;
    outline-offset: 2px;
}

.leaflet-interactive:focus {
    outline: 2px solid #228be6;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    #map-paper {
        border: 2px solid #000 !important;
    }
    
    #fullscreen-toggle {
        border: 2px solid #000;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    #fullscreen-toggle,
    .leaflet-zoom-animated,
    .leaflet-pan-anim {
        transition: none !important;
        animation: none !important;
    }
}

/* ================================
   Performance Optimizations
   ================================ */

/* Hardware acceleration for smooth animations */
#map-content-wrapper,
#map-paper,
#fullscreen-toggle {
    transform: translateZ(0);
    will-change: transform;
}

/* Optimize rendering for large datasets */
.leaflet-overlay-pane svg {
    shape-rendering: optimizeSpeed;
}

/* ================================
   Dark Mode Support (Future)
   ================================ */

@media (prefers-color-scheme: dark) {
    #map-content-wrapper[style*="position: fixed"] {
        background-color: #1a1b1e;
    }
    
    .leaflet-popup-content-wrapper {
        background-color: #25262b;
        color: #c1c2c5;
    }
    
    .leaflet-container {
        background-color: #1a1b1e;
    }
}
