/* ========================================
   ENHANCED MEDIA LOADING OPTIMIZATIONS
   ======================================== */

/* Universal Image Loading States */
img {
    background-color: #f0f0f0;
    background-image: linear-gradient(90deg,
            #f0f0f0 0px,
            #e0e0e0 40px,
            #f0f0f0 80px);
    background-size: 600px;
    animation: shimmer 1.5s infinite linear;
}

/* Shimmer animation for loading placeholders */
@keyframes shimmer {
    0% {
        background-position: -600px 0;
    }

    100% {
        background-position: 600px 0;
    }
}

/* Loaded images - remove shimmer and fade in */
img.loaded,
img[data-loaded="true"] {
    animation: none;
    background: transparent;
    opacity: 1;
}

/* Lazy loading images - START VISIBLE, enhance progressively */
/* BFCACHE-SAFE: Images visible by default, blur is enhancement only */
img[loading="lazy"],
img[data-src] {
    /* Start visible - critical for bfcache restore */
    opacity: 1;
    /* Subtle loading state instead of aggressive blur */
    filter: blur(0);
    transition: filter 0.3s ease-out, opacity 0.3s ease-out;
}

/* Only apply blur during ACTIVE loading (JS adds this class) */
img[loading="lazy"].loading,
img[data-src].loading {
    filter: blur(5px);
    opacity: 0.85;
}

/* When lazy image loads - ensure crisp rendering */
img[loading="lazy"].loaded,
img[data-src].loaded,
img[data-loaded="true"] {
    filter: blur(0) !important;
    opacity: 1 !important;
}

/* BFCACHE RESTORATION: Force all images visible on back navigation */
/* This class is applied by JS on pageshow event */
.bfcache-restored img,
.bfcache-restored img[loading="lazy"],
.bfcache-restored img[data-src] {
    filter: none !important;
    opacity: 1 !important;
    animation: none !important;
    background: transparent !important;
}

/* Fade-in animation */
@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

/* Loading skeleton for images */
.img-loading {
    position: relative;
    overflow: hidden;
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading 1.5s infinite;
}

@keyframes loading {
    0% {
        background-position: 200% 0;
    }

    100% {
        background-position: -200% 0;
    }
}

/* Video optimization */
.hero-video {
    background-color: #0f2c4a;
    transition: opacity 0.5s ease-in;
}

.hero-video.loaded {
    opacity: 1;
}

/* Optimize video loading on mobile */
@media (max-width: 768px) {

    /* Reduce video resource usage on mobile */
    .hero-video {
        transform: scale(1.05);
        filter: brightness(0.9);
    }

    /* Ensure lazy-loaded images don't cause layout shift */
    img[loading="lazy"],
    img[data-src] {
        background-color: #f5f5f5;
        min-height: 150px;
    }

    /* Product card images */
    .product-card img[loading="lazy"],
    .product-card img[data-src],
    .card-image[loading="lazy"],
    .card-image[data-src] {
        min-height: 200px;
        background-color: #f8f8f8;
    }
}

/* Product image wrapper loading state */
.img-wrapper,
.card-image-wrapper {
    position: relative;
    background-color: #f5f5f5;
}

.img-wrapper::before,
.card-image-wrapper::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 40px;
    height: 40px;
    border: 3px solid #e0e0e0;
    border-top-color: var(--accent-orange);
    border-radius: 50%;
    animation: spinner 0.8s linear infinite;
    opacity: 0.5;
    z-index: 1;
}

.img-wrapper.loaded::before,
.card-image-wrapper.loaded::before {
    display: none;
}

@keyframes spinner {
    to {
        transform: translate(-50%, -50%) rotate(360deg);
    }
}

/* Progressive image loading - blur up technique */
.progressive-img {
    position: relative;
    overflow: hidden;
}

.progressive-img-placeholder {
    position: absolute;
    inset: 0;
    filter: blur(20px);
    transform: scale(1.1);
    transition: opacity 0.4s ease-out;
}

.progressive-img-placeholder.hidden {
    opacity: 0;
}

/* Preload critical images - remove animations */
img.critical,
img[data-critical="true"] {
    animation: none !important;
    filter: none !important;
    opacity: 1 !important;
}

/* Error state for failed images */
img.error {
    background: #ffe0e0;
    background-image: none;
    animation: none;
    position: relative;
}

/* Accessibility - reduce motion */
@media (prefers-reduced-motion: reduce) {

    img,
    .img-loading,
    .hero-video {
        animation: none !important;
        transition: none !important;
    }

    img[loading="lazy"],
    img[data-src] {
        filter: none !important;
    }
}

/* ================================================
   BFCACHE & SCROLL RESTORATION SAFETY
   ================================================
   IMPORTANT: Do NOT use aggressive CSS containment here.
   contain: layout/style/strict blocks paint on bfcache restore!
*/

/* Product list container - minimal safe styles */
.product-list-container {
    /* Ensure proper height calculation */
    min-height: auto;

    /* Force layout recalculation context */
    position: relative;

    /* Do NOT use contain: layout style - blocks paint! */
}

/* Ensure product items maintain proper visibility state */
.product-item {
    /* Force visibility by default */
    visibility: visible !important;
    opacity: 1 !important;

    /* Ensure proper stacking context */
    position: relative;
    z-index: 1;

    /* Do NOT use contain - blocks descendant paint on bfcache */
}

/* Product card - force repaint on bfcache restore */
.product-card {
    /* Force GPU layer creation for repaint */
    will-change: auto;
    backface-visibility: visible;

    /* NOTE: transform: translateZ(0) can cause stale layers - removed */
}

/* Image wrapper - ensure images paint */
.img-wrapper {
    /* Ensure images don't collapse */
    min-height: 160px;

    /* Prevent blank state on restore */
    background-color: #f5f5f5;

    /* Do NOT use isolation: isolate - creates stacking context that may not repaint */
}

/* Controls bar - simple styles only */
.controls-bar {
    /* Prevent stuck sticky state */
    will-change: auto;
}

/* Force repaint helper class - applied by JS on bfcache restore */
.force-repaint {
    /* Safe repaint trigger - no stale GPU layers */
    backface-visibility: visible;
}

.force-repaint * {
    visibility: visible !important;
    opacity: 1 !important;
    animation: none !important;
    filter: none !important;
}