/* Mobile Responsive Styles for iCarryUP Routing System */

/* ============================================
   Mobile Breakpoints
   ============================================ */
/* Mobile: < 768px */
/* Tablet: 768px - 1024px */
/* Desktop: > 1024px */

/* ============================================
   Desktop: Ensure Normal Layout
   IMPORTANT: This must come FIRST to ensure desktop layout is preserved
   ============================================ */
@media (min-width: 769px) {
    /* Ensure sidebar is NOT fixed on desktop - MUST override mobile rules */
    .main-container .sidebar {
        position: relative !important;
        left: auto !important;
        width: auto !important;
        max-width: none !important;
        height: auto !important;
        z-index: auto !important;
        top: auto !important;
        transition: none !important;
        box-shadow: var(--shadow-md) !important;
        border-right: 1px solid var(--border-color) !important;
    }

    /* Ensure main-container has correct grid on desktop */
    body .main-container {
        grid-template-columns: 450px 1fr !important;
        display: grid !important;
    }

    /* Hide mobile menu toggle on desktop */
    .mobile-menu-toggle {
        display: none !important;
    }

    /* Hide sidebar overlay on desktop */
    .sidebar-overlay {
        display: none !important;
        opacity: 0 !important;
    }

    /* Hide sidebar header on desktop */
    .sidebar-header {
        display: none !important;
    }

    /* Ensure map container is visible on desktop */
    .main-container .map-container {
        position: relative !important;
        height: 100% !important;
        width: 100% !important;
        display: block !important;
    }

    .main-container #map {
        height: 100% !important;
        width: 100% !important;
        display: block !important;
    }
}

/* ============================================
   Hamburger Menu Button
   ============================================ */
.mobile-menu-toggle {
    display: none;
    background: none;
    border: 1px solid var(--border-color);
    padding: 0.5rem;
    border-radius: var(--radius-md);
    cursor: pointer;
    color: var(--text-primary);
    transition: all 0.2s;
    z-index: 1001;
    position: relative;
}

.mobile-menu-toggle:hover {
    background: var(--bg-tertiary);
}

.mobile-menu-toggle .hamburger-icon {
    display: flex;
    flex-direction: column;
    gap: 4px;
    width: 20px;
    height: 16px;
}

.mobile-menu-toggle .hamburger-icon span {
    display: block;
    width: 100%;
    height: 2px;
    background: var(--text-primary);
    border-radius: 2px;
    transition: all 0.3s ease;
}

.mobile-menu-toggle.active .hamburger-icon span:nth-child(1) {
    transform: rotate(45deg) translate(5px, 5px);
}

.mobile-menu-toggle.active .hamburger-icon span:nth-child(2) {
    opacity: 0;
}

.mobile-menu-toggle.active .hamburger-icon span:nth-child(3) {
    transform: rotate(-45deg) translate(7px, -6px);
}

/* ============================================
   Header Actions Layout
   ============================================ */
.header-actions {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.header-user-menu {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    position: relative;
}

.header-user-menu-trigger {
    /* visible on all breakpoints */
}

.header-user-account-link:hover {
    opacity: 0.9;
}

/* ============================================
   Mobile Header Adjustments
   ============================================ */
@media (max-width: 768px) {
    .header {
        padding: 0.75rem 1rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .logo {
        font-size: 1rem;
        gap: 0.5rem;
    }

    .logo img {
        height: 24px;
    }

    .logo-text {
        font-size: 0.9rem;
    }

    /* Show hamburger menu */
    .mobile-menu-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .header-user-menu-trigger {
        max-width: calc(100vw - 8rem);
        padding-right: 0.5rem;
    }

    .header-user-menu-trigger-body {
        max-width: min(8.5rem, calc(100vw - 11rem));
    }

    #username-text {
        max-width: 5.5rem;
    }

    .header-tier-chip {
        font-size: 0.62rem;
        padding: 0.12rem 0.4rem;
    }

    .header-user-menu-panel {
        left: 0;
        right: 0;
        min-width: 0;
        max-width: none;
        width: min(320px, calc(100vw - 1.5rem));
    }

    .header-user-menu .header-referral-details {
        width: 100%;
    }

    .header-actions {
        gap: 0.5rem;
        flex-wrap: wrap;
    }

    .header-end-cluster {
        width: 100%;
        justify-content: flex-start;
    }

    /* Stack header elements vertically on very small screens */
    @media (max-width: 480px) {
        .header {
            flex-direction: column;
            align-items: flex-start;
        }

        .header-actions {
            width: 100%;
            justify-content: space-between;
        }

        .header-end-cluster {
            flex-direction: column;
            align-items: stretch;
            gap: 0.5rem;
        }

        .header-tool-buttons {
            align-self: flex-end;
        }
    }
}

/* ============================================
   Sidebar as Mobile Drawer
   ============================================ */
@media (max-width: 768px) {
    /* Ensure body uses flex layout correctly on mobile */
    body {
        height: 100vh !important;
        display: flex !important;
        flex-direction: column !important;
        overflow: hidden !important;
    }

    /* Header should not shrink */
    .header {
        flex-shrink: 0 !important;
    }

    /* Main container should fill remaining space */
    .main-container {
        grid-template-columns: 1fr !important;
        position: relative;
        flex: 1 !important;
        min-height: 0 !important;
        height: 100% !important;
        display: flex !important;
        flex-direction: column !important;
        margin: 0 !important;
        padding: 0 !important;
        gap: 0 !important;
    }

    /* Hide resize handle on mobile - sidebar is drawer */
    .sidebar-resize-handle {
        display: none !important;
    }

    /* Left panel (contains sidebar + wizard) is the drawer on mobile */
    .left-panel {
        position: fixed !important;
        top: 0 !important;
        left: -100% !important;
        width: 100% !important;
        max-width: 400px !important;
        height: 100vh !important;
        z-index: 2000 !important;
        transition: left 0.3s ease !important;
        box-shadow: var(--shadow-lg) !important;
        border-right: none !important;
        display: flex !important;
        flex-direction: column !important;
        min-height: 0 !important;
        overflow: hidden !important;
    }

    .left-panel.open {
        left: 0 !important;
    }

    .left-panel .sidebar {
        position: relative !important;
        left: auto !important;
        flex: 1 1 auto !important;
        min-height: 0 !important;
        height: 100% !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }

    /* Map container should fill all available space - no gaps */
    .map-container {
        width: 100% !important;
        height: 100% !important;
        flex: 1 !important;
        min-height: 0 !important;
        position: relative !important;
        display: flex !important;
        flex-direction: column !important;
        margin: 0 !important;
        padding: 0 !important;
        background: var(--bg-tertiary) !important;
        transition: opacity 0.3s ease, transform 0.3s ease;
    }

    /* When sidebar is open, reduce map visibility/space */
    .map-container.sidebar-open {
        opacity: 0.3 !important;
        pointer-events: none !important;
    }

    /* Map expanded state - full screen */
    .map-container.map-expanded {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        z-index: 3000 !important;
        opacity: 1 !important;
        pointer-events: auto !important;
    }

    .map-container.map-expanded #map {
        height: 100vh !important;
    }

    /* Expand button styling */
    #expand-map {
        background: var(--primary-color) !important;
        color: white !important;
    }

    #expand-map:hover {
        background: var(--primary-dark) !important;
    }

    /* Map expanded state - full screen */
    .map-container.map-expanded {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100vw !important;
        height: 100vh !important;
        z-index: 3000 !important;
        opacity: 1 !important;
        pointer-events: auto !important;
    }

    .map-container.map-expanded #map {
        height: 100vh !important;
    }

    /* Expand button styling */
    #expand-map {
        background: var(--primary-color) !important;
        color: white !important;
    }

    #expand-map:hover {
        background: var(--primary-dark) !important;
    }

    /* Sidebar overlay */
    .sidebar-overlay {
        display: none;
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        z-index: 1999;
        opacity: 0;
        transition: opacity 0.3s ease;
    }

    .sidebar-overlay.active {
        display: block;
        opacity: 1;
    }

    /* Sidebar header with close button */
    .sidebar-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 1rem;
        border-bottom: 1px solid var(--border-color);
        background: var(--bg-primary);
        position: sticky;
        top: 0;
        z-index: 1;
    }

    .sidebar-header h3 {
        margin: 0;
        font-size: 1.1rem;
        color: var(--text-primary);
    }

    .sidebar-close {
        background: none;
        border: none;
        padding: 0.5rem;
        cursor: pointer;
        color: var(--text-secondary);
        font-size: 1.25rem;
        transition: color 0.2s;
    }

    .sidebar-close:hover {
        color: var(--text-primary);
    }
}

/* ============================================
   Form Optimizations for Mobile
   ============================================ */
@media (max-width: 768px) {
    .sidebar-section {
        padding: 1rem;
    }

    .section-title {
        font-size: 0.9rem;
    }

    /* Stack form fields vertically */
    .form-group,
    .upload-area {
        margin-bottom: 1rem;
    }

    /* Full width inputs on mobile */
    .sidebar input,
    .sidebar select,
    .sidebar textarea {
        width: 100%;
        font-size: 16px; /* Prevents zoom on iOS */
    }

    /* Button groups stack vertically */
    .btn-group {
        flex-direction: column;
    }

    .btn-group .btn {
        width: 100%;
    }

    /* Algorithm grid: 1 column on mobile */
    .algorithm-grid {
        grid-template-columns: 1fr;
        gap: 0.5rem;
    }

    .algorithm-option {
        padding: 0.75rem;
        font-size: 0.8rem;
    }

    /* Results grid: 1 column on mobile */
    .results-grid {
        grid-template-columns: 1fr;
    }

    /* Metric cards full width */
    .metric-card {
        margin-bottom: 0.5rem;
    }
}

/* ============================================
   Map Optimizations for Mobile
   ============================================ */
@media (max-width: 768px) {
    /* Map container already set above, but ensure it fills space */
    .map-container {
        height: 100% !important;
        flex: 1 !important;
        min-height: 0 !important;
        position: relative !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    #map {
        height: 100% !important;
        width: 100% !important;
        flex: 1 !important;
        min-height: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    /* Map controls optimized for touch */
    .map-controls {
        top: 0.5rem;
        right: 0.5rem;
        gap: 0.375rem;
    }

    .map-control-btn {
        padding: 0.875rem;
        font-size: 1rem;
        min-width: 44px; /* Minimum touch target size */
        min-height: 44px;
    }

    /* Leaflet controls optimized for mobile */
    .leaflet-control-zoom {
        margin-top: 0.5rem !important;
        margin-left: 0.5rem !important;
    }

    .leaflet-control-zoom a {
        width: 40px !important;
        height: 40px !important;
        line-height: 40px !important;
        font-size: 18px !important;
    }

    /* Popup optimized for mobile */
    .leaflet-popup-content-wrapper {
        max-width: 90vw !important;
    }

    .leaflet-popup-content {
        font-size: 14px !important;
        margin: 10px 12px !important;
    }
}

/* ============================================
   Notification Adjustments
   ============================================ */
@media (max-width: 768px) {
    .notification {
        top: 1rem;
        right: 1rem;
        left: 1rem;
        min-width: auto;
        max-width: calc(100% - 2rem);
        transform: translateY(-100px);
    }

    .notification.show {
        transform: translateY(0);
    }
}

/* ============================================
   Driver Links Section
   ============================================ */
@media (max-width: 768px) {
    .driver-link {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }

    .driver-actions {
        width: 100%;
        justify-content: flex-end;
    }

    .btn-link {
        flex: 1;
        text-align: center;
    }
}

/* ============================================
   Tablet Adjustments (768px - 1024px)
   ============================================ */
@media (min-width: 769px) and (max-width: 1024px) {
    .main-container {
        grid-template-columns: 380px 1fr;
    }

    .header {
        padding: 0.75rem 1.5rem;
    }

    .algorithm-grid {
        grid-template-columns: 1fr;
    }
}

/* ============================================
   Utility Classes for Mobile
   ============================================ */
.mobile-only {
    display: none;
}

.desktop-only {
    display: block;
}

@media (max-width: 768px) {
    .mobile-only {
        display: block;
    }

    .desktop-only {
        display: none;
    }
}

/* ============================================
   Touch Optimizations
   ============================================ */
@media (max-width: 768px) {
    /* Larger touch targets */
    .btn {
        min-height: 44px;
        padding: 0.625rem 1rem;
    }

    /* Prevent text selection on interactive elements */
    .btn,
    .sidebar-section.collapsible,
    .algorithm-option {
        -webkit-tap-highlight-color: transparent;
        user-select: none;
    }

    /* Smooth scrolling */
    .sidebar {
        -webkit-overflow-scrolling: touch;
    }
}

/* ============================================
   Landscape Mobile Adjustments
   ============================================ */
@media (max-width: 768px) and (orientation: landscape) {
    .header {
        padding: 0.5rem 1rem;
    }

    .sidebar {
        max-width: 50%;
    }

    .map-container {
        height: calc(100vh - 60px);
    }
}

/* ============================================
   Final Desktop Override - Highest Priority
   This ensures desktop layout is always correct
   ============================================ */
@media (min-width: 769px) {
    /* Force sidebar to be in normal flow on desktop */
    body .main-container > .sidebar {
        position: relative !important;
        left: 0 !important;
        transform: none !important;
    }

    /* Force main-container grid layout on desktop */
    body .main-container {
        grid-template-columns: 450px 1fr !important;
        display: grid !important;
    }

    /* Force map container to be visible on desktop */
    body .main-container > .map-container {
        position: relative !important;
        display: block !important;
        visibility: visible !important;
        opacity: 1 !important;
    }

    body .main-container > .map-container.map-container--rlp-dock {
        display: flex !important;
        flex-direction: column !important;
        min-height: 0 !important;
    }

    /* Force map to be visible on desktop */
    body .main-container #map {
        display: block !important;
        visibility: visible !important;
        opacity: 1 !important;
    }
}

