/* ================================================================
   MOBILE BROWSER COMPATIBILITY FIXES
   Targets: Safari iOS, Chrome Android, Firefox Mobile
   ================================================================ */

/* ----------------------------------------------------------------
   1. FIX: iOS Safari 100vh bug — use dvh (dynamic viewport height)
   ---------------------------------------------------------------- */
html {
    /* Prevent horizontal overflow on all pages */
    overflow-x: hidden;
    /* Use -webkit-fill-available for Safari iOS full height */
    height: -webkit-fill-available;
}

body {
    min-height: 100vh;
    min-height: -webkit-fill-available;
    min-height: 100dvh; /* Modern mobile dynamic viewport height */
    /* Prevent tap highlight on interactive elements (iOS Safari) */
    -webkit-tap-highlight-color: transparent;
    /* Prevent text size adjustment on orientation change (iOS) */
    -webkit-text-size-adjust: 100%;
    -moz-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

/* ----------------------------------------------------------------
   2. FIX: backdrop-filter — ensure -webkit- prefix everywhere
   ---------------------------------------------------------------- */
header,
.product-brand-badge,
.glass-card,
nav {
    -webkit-backdrop-filter: blur(15px);
    backdrop-filter: blur(15px);
}

/* ----------------------------------------------------------------
   3. FIX: Gradient text (background-clip) — Safari compatibility
   ---------------------------------------------------------------- */
.hero-title-home span,
.store-hero h1 span,
.hero-text h2 span {
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    /* Fallback for very old browsers */
    color: var(--primary-red);
}

/* ----------------------------------------------------------------
   4. FIX: Modal overlay — fix scroll & position for iOS Safari
   ---------------------------------------------------------------- */
.modal-overlay {
    /* Fix for iOS Safari — use fixed positioning with full dimensions */
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    /* Prevent body scroll bleed on iOS */
    -webkit-overflow-scrolling: touch;
    /* Ensure it appears above everything */
    z-index: 2000;
}

.modal-content {
    /* Prevent inner scroll from propagating to body on iOS */
    -webkit-overflow-scrolling: touch;
    overflow-y: auto;
    overscroll-behavior: contain; /* Prevent scroll chaining to background */
    /* Ensure border-radius clips children on Safari */
    -webkit-mask-image: -webkit-radial-gradient(white, black);
    mask-image: radial-gradient(white, black);
}

/* ----------------------------------------------------------------
   5. FIX: Cart Drawer — proper touch scrolling on iOS
   ---------------------------------------------------------------- */
.cart-drawer {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain; /* Prevent scroll chaining to background */
}

.cart-items-list {
    -webkit-overflow-scrolling: touch;
    overflow-y: auto;
    overscroll-behavior: contain; /* Prevent scroll chaining to background */
}

/* ----------------------------------------------------------------
   6. FIX: Input/Select elements — iOS Safari adds default styles
   ---------------------------------------------------------------- */
input,
select,
textarea,
button {
    /* Remove iOS Safari default appearance */
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    border-radius: 8px;
    font-family: var(--font-family);
}

/* Restore specific input type styles */
input[type="checkbox"] {
    -webkit-appearance: checkbox;
    appearance: checkbox;
    border-radius: 4px;
}

input[type="radio"] {
    -webkit-appearance: radio;
    appearance: radio;
}

/* Fix select arrow on iOS */
select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%23a0a0ab' stroke-width='2' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: left 12px center;
    padding-left: 36px;
}

/* RTL: move select arrow to right side */
[dir="rtl"] select,
html[dir="rtl"] select {
    background-position: right 12px center;
    padding-right: 36px;
    padding-left: 12px;
}

/* ----------------------------------------------------------------
   7. FIX: Smooth scrolling within scrollable elements on iOS
   ---------------------------------------------------------------- */
.detail-gallery-thumbnails,
.cart-items-list,
.table-responsive,
.admin-tabs {
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
}

/* ----------------------------------------------------------------
   8. FIX: Touch target sizes — minimum 44px for accessibility
   ---------------------------------------------------------------- */
.btn-icon-badge,
.btn-qty,
.modal-close,
.btn-close-cart,
.btn-action-table,
.menu-toggle,
.btn-add-cart-small {
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ----------------------------------------------------------------
   9. FIX: Prevent animation jank on mobile (use GPU layer)
   ---------------------------------------------------------------- */
.product-card,
.brand-card,
.brand-home-card,
.feature-card,
.goal-card,
.floating-badge,
.hero-logo-big {
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    will-change: transform;
}

/* Reduce motion for accessibility & performance */
@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;
    }
}

/* ----------------------------------------------------------------
   10. FIX: Prevent text overflow on small screens
   ---------------------------------------------------------------- */
.product-name,
.cart-item-name,
.detail-title {
    word-break: break-word;
    overflow-wrap: break-word;
}

/* ----------------------------------------------------------------
   11. MOBILE BREAKPOINTS — Comprehensive responsive fixes
   ---------------------------------------------------------------- */

/* Large mobile / small tablet (≤ 768px) */
@media (max-width: 768px) {
    /* Prevent iOS auto-zoom on input focus */
    input,
    select,
    textarea,
    .filter-box input,
    .filter-box select {
        font-size: 16px !important;
    }

    /* Header adjustments */
    .nav-container {
        height: 64px !important;
    }

    header {
        height: 64px !important;
    }

    /* Fix body top padding for shorter header */
    body {
        padding-top: 64px !important;
    }

    /* Hero section viewport fix */
    .hero-home {
        min-height: 100vh !important;
        min-height: 100dvh !important;
    }

    /* Logo text — hide subtitle on very small screens */
    .logo-text span {
        display: none;
    }

    /* Hero section — stack columns, reduce padding */
    .hero-home {
        padding: 80px 0 50px !important;
    }

    .hero-content-home {
        grid-template-columns: 1fr !important;
        text-align: center !important;
    }

    .hero-visual {
        display: none !important;
    }

    .hero-title-home {
        font-size: 32px !important;
        letter-spacing: -0.5px !important;
    }

    .hero-subtitle-home {
        font-size: 15px !important;
        margin: 0 auto 28px !important;
    }

    .hero-cta-group {
        justify-content: center !important;
        flex-direction: column !important;
        align-items: center !important;
        gap: 12px !important;
    }

    .btn-cta-primary,
    .btn-cta-secondary {
        width: 100% !important;
        max-width: 300px !important;
        justify-content: center !important;
    }

    /* Stats grid — 2 columns on mobile */
    .stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* About section — single column */
    .about-grid-home {
        grid-template-columns: 1fr !important;
        gap: 30px !important;
    }

    .section-heading {
        font-size: 28px !important;
    }

    /* Features — single column */
    .features-grid {
        grid-template-columns: 1fr !important;
        gap: 16px !important;
    }

    /* Goals — single column */
    .goals-grid {
        grid-template-columns: 1fr !important;
        gap: 16px !important;
    }

    /* Brands home — 3 columns on tablet, 2 on mobile */
    .brands-home-grid {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 12px !important;
    }

    /* CTA box */
    .cta-box {
        padding: 40px 20px !important;
        border-radius: 16px !important;
    }

    .cta-box h2 {
        font-size: 26px !important;
    }

    .cta-box p {
        font-size: 14px !important;
    }

    .cta-buttons {
        flex-direction: column !important;
        align-items: center !important;
    }

    /* Footer */
    .footer-grid {
        grid-template-columns: 1fr 1fr !important;
        gap: 24px !important;
    }

    .footer-bottom {
        flex-direction: column !important;
        gap: 10px !important;
        text-align: center !important;
    }

    /* Navigation — mobile drawer */
    nav {
        position: fixed !important;
        top: 64px !important;
        right: -100% !important;
        width: 80% !important;
        max-width: 300px !important;
        height: calc(100vh - 64px) !important;
        height: calc(100dvh - 64px) !important;
        background-color: var(--bg-card) !important;
        border-left: 1px solid var(--border-color) !important;
        transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
        padding: 30px 20px !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        overscroll-behavior: contain !important; /* Prevent background scroll chaining */
        z-index: 999 !important;
    }

    nav.active {
        right: 0 !important;
        display: block !important;
    }

    nav ul {
        flex-direction: column !important;
        gap: 8px !important;
    }

    nav ul li a,
    nav a {
        display: block !important;
        padding: 12px 16px !important;
        border-radius: 10px !important;
        font-size: 16px !important;
    }

    .menu-toggle {
        display: flex !important;
    }

    /* Products grid — 2 columns minimum */
    .products-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 12px !important;
    }

    /* Product card tweaks */
    .product-img-wrapper {
        height: 160px !important;
    }

    .product-details {
        padding: 12px !important;
    }

    .product-name {
        font-size: 13px !important;
        height: auto !important;
        min-height: 36px !important;
    }

    .price-iqd {
        font-size: 14px !important;
    }

    .price-usd {
        font-size: 10px !important;
    }

    .btn-add-cart-small {
        width: 38px !important;
        height: 38px !important;
    }

    /* Detail modal — full screen style on mobile */
    .modal-overlay {
        padding: 0 !important;
        align-items: flex-end !important;
    }

    .modal-content {
        border-radius: 20px 20px 0 0 !important;
        max-height: 92vh !important;
        max-height: 92dvh !important;
        max-width: 100% !important;
        width: 100% !important;
    }

    .detail-modal-body,
    .checkout-modal-body {
        padding: 20px !important;
    }

    .detail-header {
        grid-template-columns: 1fr !important;
        gap: 16px !important;
    }

    .detail-img-container {
        height: 200px !important;
    }

    .detail-title {
        font-size: 18px !important;
    }

    .detail-pricing .iqd {
        font-size: 20px !important;
    }

    .detail-actions {
        flex-direction: column !important;
        gap: 10px !important;
    }

    .detail-actions .btn-primary {
        width: 100% !important;
    }

    /* Cart drawer — full width on mobile */
    .cart-drawer {
        max-width: 100% !important;
        width: 100% !important;
        right: -100% !important;
    }

    .cart-drawer.active {
        right: 0 !important;
    }

    /* Checkout form */
    .form-row {
        grid-template-columns: 1fr !important;
    }

    /* Store brand cards grid */
    .brands-grid {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 12px !important;
    }

    /* Filter controls */
    .filter-row-top {
        grid-template-columns: 1fr !important;
    }

    .filter-row-bottom {
        grid-template-columns: 1fr 1fr !important;
    }

    /* Admin tabs — scrollable row */
    .admin-tabs {
        flex-direction: row !important;
        overflow-x: auto !important;
        flex-wrap: nowrap !important;
        gap: 8px !important;
        padding-bottom: 4px !important;
        -webkit-overflow-scrolling: touch !important;
    }

    .admin-tabs li {
        flex-shrink: 0 !important;
    }

    .admin-tab-btn {
        white-space: nowrap !important;
        font-size: 13px !important;
        padding: 8px 14px !important;
    }

    /* Admin grid — single column */
    .admin-grid {
        grid-template-columns: 1fr !important;
    }
}

/* Small phones (≤ 480px) */
@media (max-width: 480px) {
    .hero-title-home {
        font-size: 28px !important;
    }

    /* Brands home — 2 columns */
    .brands-home-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* Stats — 2 columns remain */
    .stat-box {
        padding: 14px 10px !important;
    }

    .stat-box .stat-num {
        font-size: 28px !important;
    }

    /* Footer — single column */
    .footer-grid {
        grid-template-columns: 1fr !important;
        gap: 20px !important;
    }

    /* Brand cards on store — 2 columns */
    .brands-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    /* Product badges — smaller */
    .product-badges {
        top: 6px !important;
        right: 6px !important;
        gap: 3px !important;
    }

    .card-badge {
        font-size: 9px !important;
        padding: 2px 6px !important;
    }

    /* Filter bottom — single column */
    .filter-row-bottom {
        grid-template-columns: 1fr !important;
    }

    /* About features */
    .about-features {
        grid-template-columns: 1fr !important;
    }

    /* Admin header direction */
    .admin-header {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 16px !important;
    }

    /* Admin tabs — keep scrollable */
    .admin-tabs {
        width: 100% !important;
    }
}

/* ----------------------------------------------------------------
   12. FIX: Scrollbar hide on mobile (clean look)
   ---------------------------------------------------------------- */
@media (max-width: 768px) {
    .detail-gallery-thumbnails::-webkit-scrollbar,
    .admin-tabs::-webkit-scrollbar {
        display: none;
    }

    .detail-gallery-thumbnails,
    .admin-tabs {
        scrollbar-width: none;
        -ms-overflow-style: none;
    }
}

/* ----------------------------------------------------------------
   13. FIX: Safe area insets for iPhone notch / home bar (iOS)
   ---------------------------------------------------------------- */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .cart-footer,
    .modal-overlay,
    .cart-drawer {
        padding-bottom: calc(16px + env(safe-area-inset-bottom));
    }

    header {
        padding-top: env(safe-area-inset-top);
    }
}

/* ----------------------------------------------------------------
   14. FIX: Image rendering on retina / high-DPI screens
   ---------------------------------------------------------------- */
img {
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    image-rendering: -webkit-optimize-contrast;
}

.brand-card img,
.brand-home-card img,
.logo-img,
.product-img,
.detail-img {
    image-rendering: auto;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

/* ----------------------------------------------------------------
   15. FIX: Hover states — disable on touch devices to prevent
       sticky hover effect on mobile Safari
   ---------------------------------------------------------------- */
@media (hover: none) and (pointer: coarse) {
    .product-card:hover {
        transform: none !important;
        box-shadow: none !important;
    }

    .brand-card:hover,
    .brand-home-card:hover {
        transform: none !important;
    }

    .feature-card:hover {
        transform: none !important;
        box-shadow: none !important;
    }

    .btn-primary:hover,
    .btn-cta-primary:hover {
        transform: none !important;
    }

    .social-btn:hover {
        transform: none !important;
    }
}

/* ----------------------------------------------------------------
   16. FIX: Table horizontal scroll on mobile — touch-friendly
   ---------------------------------------------------------------- */
.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    /* Show scrollbar hint */
    scroll-snap-type: x mandatory;
}

/* ----------------------------------------------------------------
   17. FIX: Store page filter row on mobile
   ---------------------------------------------------------------- */
@media (max-width: 768px) {
    .store-hero h1 {
        font-size: 28px !important;
    }

    .store-hero p {
        font-size: 13px !important;
    }

    /* VIN section */
    #vin-input {
        font-size: 16px !important; /* Prevent iOS zoom */
    }
}

/* ----------------------------------------------------------------
   18. FIX: Notification toast — position for mobile
   ---------------------------------------------------------------- */
.notification-toast {
    bottom: calc(20px + env(safe-area-inset-bottom, 0px)) !important;
    left: 16px !important;
    right: 16px !important;
    max-width: calc(100% - 32px) !important;
}

@media (max-width: 480px) {
    .notification-toast {
        font-size: 13px !important;
        padding: 12px 16px !important;
    }
}

/* ----------------------------------------------------------------
   19. FIX: Nav overlay for mobile — close nav when tapping outside
   ---------------------------------------------------------------- */
.nav-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 998;
}

.nav-overlay.active {
    display: block;
}

/* ----------------------------------------------------------------
   20. FIX: Thumb gallery — bigger touch targets on mobile
   ---------------------------------------------------------------- */
@media (max-width: 768px) {
    .thumb-wrapper {
        width: 56px !important;
        height: 56px !important;
    }
}
