// Desktop Layout and JavaScript Fixes
document.addEventListener('DOMContentLoaded', function() {
    
    // Fix potential querySelector errors by ensuring selectors exist
    function safeQuerySelector(selector) {
        if (!selector || selector.trim() === '' || selector === '#') {
            return null;
        }
        try {
            return document.querySelector(selector);
        } catch (e) {
            console.warn('Invalid selector:', selector);
            return null;
        }
    }
    
    // Override querySelector to prevent errors
    const originalQuerySelector = document.querySelector;
    document.querySelector = function(selector) {
        if (!selector || selector.trim() === '' || selector === '#') {
            console.warn('Empty or invalid selector prevented:', selector);
            return null;
        }
        return originalQuerySelector.call(this, selector);
    };
    
    // Ensure proper header display on desktop
    function fixDesktopLayout() {
        const isDesktop = window.innerWidth >= 768;
        const mobileHeader = document.querySelector('.modern-header');
        const desktopNav = document.querySelector('.navbar');
        
        if (isDesktop) {
            // Hide mobile header completely
            if (mobileHeader) {
                mobileHeader.style.display = 'none';
            }
            // Show desktop navigation
            if (desktopNav) {
                desktopNav.style.display = 'block';
            }
        } else {
            // Show mobile header on mobile
            if (mobileHeader) {
                mobileHeader.style.display = 'block';
            }
            // Hide desktop navigation on mobile
            if (desktopNav) {
                desktopNav.style.display = 'none';
            }
        }
    }
    
    // Apply fixes on load
    fixDesktopLayout();
    
    // Apply fixes on resize
    window.addEventListener('resize', fixDesktopLayout);
    
    // Fix empty href attributes that might cause selector issues
    const links = document.querySelectorAll('a[href="#"]');
    links.forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();
        });
    });
    
    // Ensure smooth scrolling works properly
    if (window.innerWidth >= 768) {
        document.documentElement.style.scrollBehavior = 'smooth';
    }
    
    // Desktop-specific enhancements
    if (window.innerWidth >= 992) {
        // Add hover effects to product cards
        const productCards = document.querySelectorAll('.product-card, .clearance-card, .latest-card');
        productCards.forEach(card => {
            card.addEventListener('mouseenter', function() {
                this.style.transform = 'translateY(-8px)';
                this.style.boxShadow = '0 12px 40px rgba(255, 107, 53, 0.15)';
            });
            
            card.addEventListener('mouseleave', function() {
                this.style.transform = 'translateY(0)';
                this.style.boxShadow = '0 4px 20px rgba(0,0,0,0.08)';
            });
        });
        
        // Enhanced button hover effects
        const heroButtons = document.querySelectorAll('.btn-hero');
        heroButtons.forEach(button => {
            button.addEventListener('mouseenter', function() {
                this.style.transform = 'translateY(-3px)';
            });
            
            button.addEventListener('mouseleave', function() {
                this.style.transform = 'translateY(0)';
            });
        });
    }
});