/**
 * WhenBRRR Mobile UX Fixes - Production Ready
 * Fixes for iPhone 12-15 (390-430px viewport)
 * 
 * FIXES:
 * 1. Top navigation bar - compress height, fix account dropdown
 * 2. Left sidebar - ensure menu items display properly
 * 3. Bottom navigation bar - fix Allocate button routing
 * 4. Floating "+" (FAB) button - fix positioning and click handling
 */

/* ===================================================================
   1. TOP NAVIGATION BAR FIXES
   =================================================================== */

@media (max-width: 768px) {
    /* Compress header height - reduce from 80px+ to compact 56px */
    .app-header {
        min-height: 56px !important;
        height: 56px !important;
        padding: 8px 12px !important; /* Reduced from 20px 32px */
        position: fixed !important;
        top: 0;
        left: 0;
        right: 0;
        z-index: 1000 !important;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1) !important;
    }

    /* Compress left section */
    .app-header__left {
        gap: 8px !important; /* Reduced from 20px */
        flex: 1;
        min-width: 0; /* Allow text truncation */
    }

    /* Page heading - compact single line */
    .page-heading__title {
        font-size: 16px !important; /* Reduced from 1.5rem */
        font-weight: 600 !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        line-height: 1.2;
    }

    /* Hide subtitle on mobile to save space */
    .page-heading__subtitle {
        display: none !important;
    }

    /* Hide search on mobile - not essential for small screens */
    .header-search {
        display: none !important;
    }

    /* Compress actions section */
    .app-header__actions {
        gap: 6px !important; /* Reduced from 12px */
        flex-shrink: 0; /* Prevent compression */
    }

    /* Make icon buttons more compact */
    .icon-button {
        padding: 6px 8px !important; /* Reduced from 10px 14px */
        min-width: 36px;
        min-height: 36px;
    }

    .icon-button span {
        display: none !important; /* Hide text labels on mobile */
    }

    .icon-button svg {
        width: 20px !important;
        height: 20px !important;
    }

    /* CRITICAL FIX: Account menu dropdown behavior */
    .account-menu__trigger {
        padding: 4px 8px !important; /* More compact */
        gap: 4px !important;
        min-width: 36px;
        min-height: 36px;
    }

    .account-menu__trigger span {
        display: none !important; /* Hide "Ops Team" text */
    }

    .account-menu__trigger svg {
        width: 16px !important;
        height: 16px !important;
    }

    .account-menu__trigger-avatar {
        width: 24px !important; /* Smaller avatar */
        height: 24px !important;
        font-size: 10px !important;
    }

    /* FIX: Account dropdown panel - hidden by default */
    .account-menu__panel {
        /* Reset any permanent visibility */
        display: none !important;
        position: absolute;
        top: calc(100% + 8px);
        right: 0;
        min-width: 180px;
        border-radius: 8px;
        border: 1px solid var(--color-border, #e5e7eb);
        background: var(--color-surface, #ffffff);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        padding: 8px 0;
        z-index: 1001;
        opacity: 0;
        transform: translateY(-10px);
        transition: opacity 0.2s ease, transform 0.2s ease;
        pointer-events: none;
    }

    /* FIX: Show dropdown when [hidden] attribute removed */
    .account-menu__panel:not([hidden]) {
        display: flex !important;
        flex-direction: column;
        opacity: 1;
        transform: translateY(0);
        pointer-events: auto;
    }

    .account-menu__panel a {
        padding: 10px 16px !important;
        font-size: 14px !important;
        color: var(--color-text, #1f2937);
        text-decoration: none;
        white-space: nowrap;
        display: block;
        transition: background 0.15s ease;
    }

    .account-menu__panel a:hover,
    .account-menu__panel a:active {
        background: var(--color-surface-alt, #f9fafb);
    }

    /* Adjust main content for fixed header */
    .app-main {
        margin-top: 56px !important; /* Match header height */
    }

    .app-content {
        padding: 16px !important; /* Reduced from 32px */
    }
}

/* ===================================================================
   2. LEFT SIDEBAR FIXES - Ensure menu items display
   =================================================================== */

@media (max-width: 768px) {
    /* FIX: Sidebar must be hidden off-screen by default */
    .sidebar,
    #app-sidebar {
        position: fixed;
        top: 0;
        left: -100%; /* Hidden off-screen */
        width: 85%;
        max-width: 320px;
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height for mobile browsers */
        background: var(--color-surface, #ffffff);
        box-shadow: 2px 0 12px rgba(0, 0, 0, 0.15);
        z-index: 1002; /* Above header */
        transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        display: flex !important; /* Force display */
        flex-direction: column;
    }

    /* FIX: Show sidebar when open */
    .sidebar.is-open,
    #app-sidebar.is-open {
        left: 0 !important;
    }

    /* Ensure sidebar brand is visible */
    .sidebar__brand {
        display: flex !important;
        align-items: center;
        justify-content: space-between;
        padding: 16px;
        border-bottom: 1px solid var(--color-border, #e5e7eb);
        flex-shrink: 0; /* Don't compress */
        background: var(--color-surface, #ffffff);
        position: sticky;
        top: 0;
        z-index: 1;
    }

    .sidebar__brand-icon {
        font-size: 24px;
    }

    .sidebar__brand-text {
        flex: 1;
        margin-left: 12px;
        font-weight: 600;
        font-size: 16px;
    }

    /* Close button */
    .sidebar__close {
        display: flex !important;
        align-items: center;
        justify-content: center;
        width: 36px;
        height: 36px;
        padding: 0;
        background: none;
        border: none;
        cursor: pointer;
        border-radius: 6px;
        color: var(--color-text, #1f2937);
        transition: background 0.15s ease;
    }

    .sidebar__close:hover,
    .sidebar__close:active {
        background: var(--color-surface-alt, #f9fafb);
    }

    .sidebar__close svg {
        width: 20px;
        height: 20px;
    }

    /* FIX: Ensure sidebar sections container is visible */
    .sidebar__sections {
        flex: 1;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        padding: 0; /* Remove padding to maximize content area */
        display: block !important; /* Force visibility */
    }

    /* FIX: Section titles visible */
    .sidebar__section-title {
        display: block !important;
        padding: 16px 16px 8px;
        font-size: 11px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--color-text-muted, #6b7280);
    }

    /* FIX: Navigation sections visible */
    .sidebar__section,
    nav.sidebar__section {
        display: block !important;
        margin-bottom: 0;
    }

    /* FIX: Navigation list visible */
    .nav-list {
        display: block !important;
        padding: 0;
        margin: 0;
        list-style: none;
    }

    /* FIX: Navigation links fully visible and tappable */
    .nav-link,
    .sidebar-item,
    a[data-nav] {
        display: flex !important; /* Force display */
        align-items: center;
        gap: 12px;
        padding: 14px 16px !important;
        color: var(--color-text, #1f2937);
        text-decoration: none;
        font-size: 15px !important;
        font-weight: 500;
        border-left: 3px solid transparent;
        transition: all 0.15s ease;
        min-height: 48px; /* Touch-friendly */
        cursor: pointer;
        background: var(--color-surface, #ffffff);
    }

    .nav-link:hover,
    .nav-link:active,
    .sidebar-item:hover,
    .sidebar-item:active,
    a[data-nav]:hover,
    a[data-nav]:active {
        background: var(--color-surface-alt, #f9fafb);
    }

    .nav-link.nav-link--active,
    .sidebar-item.active,
    a[data-nav].nav-link--active {
        background: rgba(102, 126, 234, 0.08);
        border-left-color: #667eea;
        color: #667eea;
    }

    /* FIX: Icons visible */
    .nav-link svg,
    .sidebar-item svg,
    a[data-nav] svg {
        width: 20px !important;
        height: 20px !important;
        flex-shrink: 0;
        display: block !important;
        stroke-width: 1.5;
    }

    /* FIX: Labels visible */
    .nav-link span,
    .sidebar-item span,
    a[data-nav] span {
        flex: 1;
        display: block !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* Sidebar footer */
    .sidebar__footer {
        display: flex !important;
        flex-direction: column;
        gap: 8px;
        padding: 16px;
        border-top: 1px solid var(--color-border, #e5e7eb);
        background: var(--color-surface-alt, #f9fafb);
        flex-shrink: 0;
    }

    .sidebar__footer strong {
        font-size: 13px;
        font-weight: 600;
        color: var(--color-text, #1f2937);
    }

    .sidebar__footer span {
        font-size: 12px;
        color: var(--color-text-muted, #6b7280);
        line-height: 1.4;
    }

    /* Hide collapse button on mobile */
    .sidebar__collapse {
        display: none !important;
    }
}

/* ===================================================================
   3. BOTTOM NAVIGATION BAR FIXES
   =================================================================== */

@media (max-width: 768px) {
    /* Ensure bottom nav is visible */
    .hs-bottom-nav {
        position: fixed !important;
        bottom: 0;
        left: 0;
        right: 0;
        height: 56px;
        background: var(--color-surface, #ffffff);
        border-top: 1px solid var(--color-border, #e5e7eb);
        display: flex !important;
        justify-content: space-around;
        align-items: center;
        padding: 0 8px;
        box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.08);
        z-index: 999;
        /* Safe area for iPhone notch */
        padding-bottom: max(0px, env(safe-area-inset-bottom));
    }

    /* FIX: Navigation items properly styled */
    .hs-nav-item {
        display: flex !important;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: 2px;
        padding: 6px 8px;
        min-width: 60px;
        min-height: 48px;
        color: var(--color-text-muted, #6b7280);
        text-decoration: none !important;
        font-size: 11px;
        font-weight: 500;
        transition: color 0.15s ease;
        cursor: pointer;
        position: relative;
        /* Remove any tap highlights */
        -webkit-tap-highlight-color: transparent;
        user-select: none;
    }

    /* FIX: Allocate button - ensure it's clickable */
    .hs-nav-item[href="/allocate"],
    a[href="/allocate"].hs-nav-item {
        pointer-events: auto !important; /* Critical fix */
        cursor: pointer !important;
        position: relative;
        z-index: 1;
    }

    /* Active state */
    .hs-nav-item.active {
        color: #667eea !important;
    }

    /* Tap feedback */
    .hs-nav-item:active {
        transform: scale(0.95);
        opacity: 0.7;
    }

    /* Icons */
    .hs-nav-item svg {
        width: 24px !important;
        height: 24px !important;
        stroke-width: 1.5;
        display: block !important;
    }

    /* Labels */
    .hs-nav-item span {
        display: block !important;
        font-size: 11px;
        font-weight: 500;
        white-space: nowrap;
    }

    /* Badge (for notification count) */
    .hs-nav-badge {
        position: absolute;
        top: 2px;
        right: 8px;
        min-width: 18px;
        height: 18px;
        padding: 0 4px;
        background: #ef4444;
        color: #ffffff;
        border-radius: 9px;
        font-size: 10px;
        font-weight: 600;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    /* Adjust main content for bottom nav */
    .app-content,
    .hs-mobile-container {
        margin-bottom: calc(56px + env(safe-area-inset-bottom)) !important;
    }
}

/* ===================================================================
   4. FLOATING "+" (FAB) BUTTON FIXES
   =================================================================== */

@media (max-width: 768px) {
    /* FIX: Floating action button positioning and functionality */
    .hs-fab {
        position: fixed !important;
        bottom: calc(56px + 16px + env(safe-area-inset-bottom)) !important; /* Above bottom nav */
        right: 16px !important;
        width: 56px !important;
        height: 56px !important;
        border-radius: 50% !important;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
        color: #ffffff !important;
        border: none !important;
        box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4) !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        cursor: pointer !important;
        z-index: 1000 !important; /* Above bottom nav (999) */
        transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1) !important;
        /* Ensure it's interactive */
        pointer-events: auto !important;
        -webkit-tap-highlight-color: transparent;
        user-select: none;
    }

    .hs-fab:hover {
        box-shadow: 0 6px 16px rgba(102, 126, 234, 0.5) !important;
        transform: translateY(-2px) !important;
    }

    .hs-fab:active {
        transform: scale(0.92) !important;
        box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3) !important;
    }

    .hs-fab svg {
        width: 24px !important;
        height: 24px !important;
        display: block !important;
        fill: #ffffff !important;
    }

    /* Hide FAB on desktop */
    @media (min-width: 769px) {
        .hs-fab {
            display: none !important;
        }
    }
}

/* ===================================================================
   5. OVERLAY FIXES - Click outside to close
   =================================================================== */

@media (max-width: 768px) {
    /* Mobile overlay for sidebar and dropdowns */
    .app-overlay,
    .mobile-overlay,
    #mobile-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 1001; /* Between header (1000) and sidebar (1002) */
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
        transition: opacity 0.3s ease, visibility 0.3s ease;
    }

    /* Show overlay when sidebar/menu is open */
    .app-overlay.is-visible,
    .mobile-overlay.is-active,
    #mobile-overlay.is-visible,
    body.is-nav-open .app-overlay,
    body.is-nav-open .mobile-overlay,
    body.is-nav-open #mobile-overlay {
        opacity: 1 !important;
        visibility: visible !important;
        pointer-events: auto !important;
    }

    /* Prevent body scroll when menu is open */
    body.is-nav-open,
    body.menu-is-open {
        overflow: hidden !important;
        position: fixed;
        width: 100%;
    }
}

/* ===================================================================
   6. HAMBURGER MENU FIXES
   =================================================================== */

@media (max-width: 768px) {
    /* Show hamburger on mobile */
    .hamburger,
    #menu-toggle {
        display: flex !important;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        padding: 0;
        background: none;
        border: 1px solid var(--color-border, #e5e7eb);
        border-radius: 6px;
        cursor: pointer;
        flex-direction: column;
        gap: 4px;
        position: relative;
        z-index: 1003; /* Above sidebar */
        transition: all 0.2s ease;
    }

    .hamburger:hover,
    .hamburger:active,
    #menu-toggle:hover,
    #menu-toggle:active {
        background: var(--color-surface-alt, #f9fafb);
        border-color: var(--color-primary, #667eea);
    }

    /* Hamburger lines */
    .hamburger span,
    #menu-toggle span {
        display: block !important;
        width: 18px;
        height: 2px;
        background: var(--color-text, #1f2937);
        border-radius: 2px;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        position: relative;
    }

    /* Animated X when open */
    body.is-nav-open .hamburger span:nth-child(1),
    body.is-nav-open #menu-toggle span:nth-child(1),
    body.menu-is-open .hamburger span:nth-child(1),
    body.menu-is-open #menu-toggle span:nth-child(1) {
        transform: translateY(6px) rotate(45deg);
    }

    body.is-nav-open .hamburger span:nth-child(2),
    body.is-nav-open #menu-toggle span:nth-child(2),
    body.menu-is-open .hamburger span:nth-child(2),
    body.menu-is-open #menu-toggle span:nth-child(2) {
        opacity: 0;
        transform: translateX(-10px);
    }

    body.is-nav-open .hamburger span:nth-child(3),
    body.is-nav-open #menu-toggle span:nth-child(3),
    body.menu-is-open .hamburger span:nth-child(3),
    body.menu-is-open #menu-toggle span:nth-child(3) {
        transform: translateY(-6px) rotate(-45deg);
    }
}

/* ===================================================================
   7. DARK MODE SUPPORT
   =================================================================== */

@media (max-width: 768px) {
    [data-theme="dark"] .app-header,
    [data-theme="dark"] .hs-bottom-nav,
    [data-theme="dark"] .sidebar,
    [data-theme="dark"] #app-sidebar {
        background: #1f2937;
        border-color: #374151;
    }

    [data-theme="dark"] .account-menu__panel {
        background: #1f2937;
        border-color: #374151;
    }

    [data-theme="dark"] .account-menu__panel a:hover {
        background: #374151;
    }

    [data-theme="dark"] .nav-link,
    [data-theme="dark"] .sidebar-item {
        color: #f9fafb;
        background: #1f2937;
    }

    [data-theme="dark"] .nav-link:hover,
    [data-theme="dark"] .sidebar-item:hover {
        background: #374151;
    }

    [data-theme="dark"] .hamburger,
    [data-theme="dark"] #menu-toggle {
        border-color: #374151;
    }

    [data-theme="dark"] .hamburger span,
    [data-theme="dark"] #menu-toggle span {
        background: #f9fafb;
    }
}

/* ===================================================================
   8. PERFORMANCE OPTIMIZATIONS
   =================================================================== */

@media (max-width: 768px) {
    /* GPU acceleration for smooth animations */
    .sidebar,
    #app-sidebar,
    .app-overlay,
    .mobile-overlay,
    #mobile-overlay,
    .account-menu__panel,
    .hs-fab {
        will-change: transform, opacity;
        backface-visibility: hidden;
        -webkit-backface-visibility: hidden;
        transform: translateZ(0);
        -webkit-transform: translateZ(0);
    }

    /* Optimize scrolling */
    .sidebar__sections,
    .app-content {
        -webkit-overflow-scrolling: touch;
        scroll-behavior: smooth;
    }

    /* Reduce motion for users who prefer it */
    @media (prefers-reduced-motion: reduce) {
        .sidebar,
        #app-sidebar,
        .app-overlay,
        .hamburger span,
        #menu-toggle span,
        .account-menu__panel,
        .hs-fab,
        .hs-nav-item {
            transition-duration: 0.01ms !important;
            animation-duration: 0.01ms !important;
        }
    }
}

/* ===================================================================
   9. ACCESSIBILITY ENHANCEMENTS
   =================================================================== */

@media (max-width: 768px) {
    /* Focus states for keyboard navigation */
    .hamburger:focus,
    #menu-toggle:focus,
    .nav-link:focus,
    .hs-nav-item:focus,
    .hs-fab:focus,
    .account-menu__trigger:focus {
        outline: 2px solid #667eea !important;
        outline-offset: 2px !important;
    }

    /* High contrast mode support */
    @media (prefers-contrast: high) {
        .app-header,
        .hs-bottom-nav,
        .sidebar {
            border-width: 2px !important;
        }

        .hamburger,
        #menu-toggle {
            border-width: 2px !important;
        }
    }
}
