/**
 * Advanced Mobile Optimizations
 * iOS Safe Areas, Notch Support, and Cross-Platform Mobile Enhancements
 */

/* ==========================================
   iOS SAFE AREA SUPPORT
   ========================================== */

/* Support for iPhone X, 11, 12, 13, 14 notch and bottom bar */
@supports (padding: max(0px)) {
    body {
        /* Safe area insets for iOS devices with notch */
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
    }
    
    .top-bar,
    .main-header {
        padding-left: max(15px, env(safe-area-inset-left));
        padding-right: max(15px, env(safe-area-inset-right));
    }
    
    .main-header.sticky-top {
        top: env(safe-area-inset-top);
    }
    
    /* Mobile Navigation Safe Areas */
    .mobile-nav {
        padding-top: max(20px, env(safe-area-inset-top));
        padding-bottom: max(20px, env(safe-area-inset-bottom));
        padding-left: max(0px, env(safe-area-inset-left));
        padding-right: max(0px, env(safe-area-inset-right));
    }
    
    /* Footer Safe Areas */
    .main-footer {
        padding-bottom: max(0px, env(safe-area-inset-bottom));
    }
    
    /* Fixed Bottom Elements */
    .floating-buttons {
        bottom: max(20px, calc(20px + env(safe-area-inset-bottom)));
        right: max(20px, calc(20px + env(safe-area-inset-right)));
    }
    
    /* Sticky Cart Button on Product Detail */
    #sticky-cart-mobile {
        padding-bottom: max(15px, calc(15px + env(safe-area-inset-bottom))) !important;
        padding-left: max(15px, calc(15px + env(safe-area-inset-left))) !important;
        padding-right: max(15px, calc(15px + env(safe-area-inset-right))) !important;
    }
}

/* ==========================================
   NOTCH AND DYNAMIC ISLAND SUPPORT (iPhone 14 Pro+)
   ========================================== */

@media only screen and (device-width: 393px) and (device-height: 852px) and (-webkit-device-pixel-ratio: 3),
       only screen and (device-width: 430px) and (device-height: 932px) and (-webkit-device-pixel-ratio: 3) {
    /* iPhone 14 Pro and 14 Pro Max specific adjustments */
    .main-header {
        padding-top: 50px; /* Account for Dynamic Island */
    }
}

/* ==========================================
   ANDROID BOTTOM NAVIGATION BAR
   ========================================== */

@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .footer-bottom {
        padding-bottom: calc(20px + env(safe-area-inset-bottom));
    }
}

/* ==========================================
   TOUCH TARGET OPTIMIZATION (44x44px minimum)
   ========================================== */

@media (max-width: 767.98px) {
    /* Ensure all interactive elements meet accessibility standards */
    a, button, 
    .btn, 
    .header-action-btn,
    .product-action-btn,
    .nav-link,
    input[type="submit"],
    input[type="button"],
    input[type="checkbox"],
    input[type="radio"],
    select {
        min-height: 44px;
        min-width: 44px;
    }
    
    /* Form inputs with proper touch targets */
    .form-control,
    .form-select,
    .glass-input {
        min-height: 48px;
        font-size: 16px; /* Prevent iOS zoom */
        padding: 12px 16px;
    }
    
    textarea.form-control {
        min-height: 120px;
    }
    
    /* Navigation links */
    .nav-link {
        padding: 12px 16px;
    }
    
    /* Product cards touch targets */
    .product-card {
        -webkit-tap-highlight-color: rgba(0, 0, 0, 0.05);
        tap-highlight-color: rgba(0, 0, 0, 0.05);
    }
}

/* ==========================================
   PREVENT TEXT RESIZE ON ORIENTATION CHANGE
   ========================================== */

@media (max-width: 991.98px) {
    html {
        -webkit-text-size-adjust: 100%;
        -moz-text-size-adjust: 100%;
        -ms-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }
}

/* ==========================================
   IMPROVED TOUCH SCROLLING (iOS Momentum)
   ========================================== */

.mobile-nav,
.mobile-nav-body,
.filter-sidebar,
.glass-filter-sidebar,
.dropdown-menu,
.modal-body,
.account-nav {
    -webkit-overflow-scrolling: touch;
    overflow-y: auto;
    overscroll-behavior: contain;
}

/* ==========================================
   DISABLE PULL-TO-REFRESH ON SPECIFIC ELEMENTS
   ========================================== */

body {
    overscroll-behavior-y: contain;
}

.search-bar-container,
.modal,
.mobile-nav {
    overscroll-behavior: none;
}

/* ==========================================
   ENHANCED TAP HIGHLIGHT
   ========================================== */

a, button, .btn, .nav-link, .dropdown-item {
    -webkit-tap-highlight-color: rgba(44, 95, 45, 0.1);
    tap-highlight-color: rgba(44, 95, 45, 0.1);
}

input:focus, 
textarea:focus, 
select:focus {
    -webkit-tap-highlight-color: transparent;
    tap-highlight-color: transparent;
}

/* ==========================================
   STICKY POSITIONING FIX FOR IOS
   ========================================== */

@supports (-webkit-overflow-scrolling: touch) {
    .sticky-top {
        position: -webkit-sticky;
        position: sticky;
    }
}

/* ==========================================
   LANDSCAPE ORIENTATION OPTIMIZATIONS
   ========================================== */

@media (max-height: 500px) and (orientation: landscape) {
    /* Reduce header height in landscape */
    .main-header {
        padding: 10px 0 !important;
    }
    
    .logo a {
        font-size: 18px !important;
    }
    
    .header-action-btn {
        width: 32px !important;
        height: 32px !important;
        font-size: 14px !important;
    }
    
    /* Optimize mobile nav for landscape */
    .mobile-nav {
        width: 280px !important;
    }
    
    .mobile-nav-header {
        padding: 10px 15px !important;
    }
    
    .nav-link {
        padding: 8px 16px !important;
    }
    
    /* Hide top bar in landscape for space */
    .top-bar,
    .news-ticker-bar {
        display: none !important;
    }
}

/* ==========================================
   FOLDABLE DEVICES SUPPORT
   ========================================== */

/* Samsung Galaxy Z Fold */
@media (min-width: 653px) and (max-width: 884px) {
    .container {
        max-width: 100%;
        padding: 0 20px;
    }
    
    .product-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

/* Dual Screen Devices */
@media (spanning: single-fold-vertical) {
    .main-content {
        flex: 1;
    }
}

/* ==========================================
   REDUCED MOTION SUPPORT
   ========================================== */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
    
    .mobile-nav,
    .filter-sidebar,
    .dropdown-menu {
        transition: none !important;
    }
}

/* ==========================================
   DARK MODE SUPPORT (Android/iOS System)
   ========================================== */

@media (prefers-color-scheme: dark) {
    /* Optional: Add dark mode styles for better mobile experience */
    /* Currently keeping light theme as default, but structure is ready */
}

/* ==========================================
   HIGH PIXEL DENSITY DISPLAYS (Retina)
   ========================================== */

@media only screen and (-webkit-min-device-pixel-ratio: 2),
       only screen and (min-resolution: 192dpi),
       only screen and (min-resolution: 2dppx) {
    /* Optimize for high-DPI displays */
    .logo-image,
    .product-image {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}

/* ==========================================
   ANDROID CHROME SPECIFIC FIXES
   ========================================== */

@supports (-webkit-appearance: none) {
    /* Remove default Android styling */
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="password"],
    input[type="number"],
    select,
    textarea {
        -webkit-appearance: none;
        appearance: none;
    }
}

/* ==========================================
   FOCUS VISIBILITY FOR KEYBOARD NAVIGATION
   ========================================== */

@media (max-width: 991.98px) {
    *:focus-visible {
        outline: 2px solid var(--primary-color);
        outline-offset: 2px;
    }
    
    button:focus-visible,
    a:focus-visible,
    input:focus-visible,
    select:focus-visible,
    textarea:focus-visible {
        outline: 2px solid var(--primary-color);
        outline-offset: 2px;
    }
}

/* ==========================================
   MOBILE HEADER SCROLLED STATE
   ========================================== */

@media (max-width: 991.98px) {
    .main-header.scrolled {
        padding: 10px 0;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    }
    
    .main-header.scrolled .logo a {
        font-size: 18px;
    }
    
    .main-header.scrolled .logo-image {
        height: 28px;
    }
}

/* ==========================================
   MOBILE SEARCH ENHANCEMENTS
   ========================================== */

@media (max-width: 767.98px) {
    .search-bar-container {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.95);
        z-index: 99999;
        display: none;
        padding-top: env(safe-area-inset-top);
    }
    
    .search-bar-container.active {
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        padding-top: calc(60px + env(safe-area-inset-top));
    }
    
    .search-input {
        font-size: 18px;
        border-radius: 12px;
        background: white;
    }
    
    .search-close {
        position: absolute;
        top: calc(15px + env(safe-area-inset-top));
        right: 15px;
        color: white;
        font-size: 24px;
        z-index: 10;
    }
}

/* ==========================================
   MOBILE BOTTOM SHEET STYLE MODALS
   ========================================== */

@media (max-width: 767.98px) {
    .modal.modal-bottom-sheet .modal-dialog {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        margin: 0;
        max-width: 100%;
        transform: translateY(100%);
        transition: transform 0.3s ease-out;
    }
    
    .modal.modal-bottom-sheet.show .modal-dialog {
        transform: translateY(0);
    }
    
    .modal.modal-bottom-sheet .modal-content {
        border-radius: 20px 20px 0 0;
        border: none;
        padding-bottom: max(20px, env(safe-area-inset-bottom));
    }
}

/* ==========================================
   MOBILE CART BADGE POSITIONING
   ========================================== */

@media (max-width: 575.98px) {
    .badge-count,
    .cart-count,
    .wishlist-count {
        position: absolute;
        top: -5px;
        right: -8px;
        min-width: 18px;
        height: 18px;
        padding: 0 5px;
        font-size: 11px;
        line-height: 18px;
        border-radius: 9px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

/* ==========================================
   MOBILE PERFORMANCE OPTIMIZATIONS
   ========================================== */

@media (max-width: 767.98px) {
    /* Disable expensive visual effects on mobile */
    .glass-card,
    .glass-product-card,
    .glass-button,
    .glass-header {
        backdrop-filter: none !important;
        -webkit-backdrop-filter: none !important;
        background: rgba(255, 255, 255, 0.98) !important;
    }
    
    /* Simplify shadows */
    .box-shadow-xl {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1) !important;
    }
    
    /* Optimize animations */
    .product-card,
    .hover-lift,
    .hover-scale {
        transition: none !important;
    }
    
    .product-card:hover {
        transform: none !important;
    }
}

/* ==========================================
   MOBILE STICKY ELEMENTS
   ========================================== */

@media (max-width: 767.98px) {
    .sticky-cart-mobile,
    .sticky-checkout-btn {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: white;
        padding: 15px;
        box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.1);
        z-index: 999;
        padding-bottom: max(15px, calc(15px + env(safe-area-inset-bottom)));
    }
}

/* ==========================================
   MOBILE ACCESSIBILITY ENHANCEMENTS
   ========================================== */

@media (max-width: 767.98px) {
    /* Larger text for better readability */
    body {
        font-size: 15px;
        line-height: 1.6;
    }
    
    /* Better spacing for touch */
    .btn + .btn {
        margin-left: 10px;
    }
    
    /* Improved contrast */
    .text-muted {
        color: #666 !important;
    }
    
    /* Skip to main content link for screen readers */
    .skip-to-main {
        position: absolute;
        top: -40px;
        left: 0;
        background: var(--primary-color);
        color: white;
        padding: 8px 16px;
        text-decoration: none;
        z-index: 100000;
    }
    
    .skip-to-main:focus {
        top: 0;
    }
}

/* ==========================================
   MOBILE IMAGE OPTIMIZATION
   ========================================== */

@media (max-width: 767.98px) {
    /* Lazy loading images */
    img[loading="lazy"] {
        background: #f0f0f0;
    }
    
    /* Prevent layout shift */
    img {
        width: 100%;
        height: auto;
    }
    
    /* Product images aspect ratio */
    .product-image-wrapper {
        position: relative;
        padding-bottom: 100%; /* 1:1 aspect ratio */
        overflow: hidden;
    }
    
    .product-image-wrapper img {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        object-fit: cover;
    }
}

/* ==========================================
   MOBILE DROPDOWN IMPROVEMENTS
   ========================================== */

@media (max-width: 991.98px) {
    .dropdown-menu {
        max-height: 70vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .dropdown-item {
        padding: 12px 20px;
        font-size: 15px;
    }
}

/* ==========================================
   MOBILE FORM VALIDATION FEEDBACK
   ========================================== */

@media (max-width: 767.98px) {
    .invalid-feedback,
    .valid-feedback {
        font-size: 14px;
        margin-top: 6px;
        display: block;
    }
    
    .form-control.is-invalid,
    .form-select.is-invalid {
        border-width: 2px;
    }
    
    .form-control.is-valid,
    .form-select.is-valid {
        border-width: 2px;
    }
}

/* ==========================================
   PWA SUPPORT (Add to Homescreen)
   ========================================== */

.pwa-install-prompt {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--primary-color);
    color: white;
    padding: 15px;
    display: none;
    align-items: center;
    justify-content: space-between;
    z-index: 9999;
    padding-bottom: max(15px, calc(15px + env(safe-area-inset-bottom)));
}

.pwa-install-prompt.show {
    display: flex;
}

.pwa-install-prompt button {
    background: white;
    color: var(--primary-color);
    padding: 8px 16px;
    border-radius: 6px;
    font-weight: 600;
}
