/**
 * Image Optimization Styles
 * Lazy loading, WebP support, and performance optimizations
 */

/* Lazy Loading States */
img[data-src],
img[loading="lazy"]:not(.loaded) {
    opacity: 0;
    transition: opacity 0.3s ease-in-out;
}

img.loaded {
    opacity: 1;
}

/* Fade-in animation */
img.fade-in {
    animation: imageFadeIn 0.5s ease-in-out;
}

@keyframes imageFadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Lazy Background */
.lazy-bg {
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    transition: opacity 0.3s ease-in-out;
    position: relative;
}

.lazy-bg:not(.loaded) {
    background-color: #f0f0f0;
}

.lazy-bg.loaded {
    animation: bgFadeIn 0.5s ease-in-out;
}

@keyframes bgFadeIn {
    from {
        opacity: 0.8;
    }
    to {
        opacity: 1;
    }
}

/* Loading placeholder with shimmer effect */
.img-placeholder {
    background: linear-gradient(
        90deg,
        #f0f0f0 0%,
        #f8f8f8 50%,
        #f0f0f0 100%
    );
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
    0% {
        background-position: -200% 0;
    }
    100% {
        background-position: 200% 0;
    }
}

/* Aspect ratio containers */
.aspect-ratio-16-9 {
    position: relative;
    padding-bottom: 56.25%; /* 16:9 */
    height: 0;
    overflow: hidden;
}

.aspect-ratio-4-3 {
    position: relative;
    padding-bottom: 75%; /* 4:3 */
    height: 0;
    overflow: hidden;
}

.aspect-ratio-1-1 {
    position: relative;
    padding-bottom: 100%; /* 1:1 */
    height: 0;
    overflow: hidden;
}

.aspect-ratio-16-9 > *,
.aspect-ratio-4-3 > *,
.aspect-ratio-1-1 > * {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* Responsive images */
picture {
    display: block;
    width: 100%;
}

picture img {
    width: 100%;
    height: auto;
    display: block;
}

/* Error state */
img.error {
    opacity: 0.5;
    background-color: #f0f0f0;
}

img.error::after {
    content: '⚠';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 2rem;
    color: #999;
}

/* Blur-up technique for progressive loading */
.blur-up {
    filter: blur(5px);
    transition: filter 0.3s;
}

.blur-up.loaded {
    filter: blur(0);
}

/* Native lazy loading enhancement */
img[loading="lazy"] {
    background-color: #f0f0f0;
    min-height: 100px;
}

/* Optimize for print */
@media print {
    img[data-src] {
        display: none;
    }
    
    img.loaded {
        display: block;
    }
}

/* High DPI displays */
@media (-webkit-min-device-pixel-ratio: 2),
       (min-resolution: 192dpi) {
    /* Load higher resolution images for retina displays */
    img[data-src-2x] {
        content: attr(data-src-2x);
    }
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    img.fade-in,
    .lazy-bg.loaded {
        animation: none;
    }
    
    img[data-src],
    img[loading="lazy"]:not(.loaded),
    .lazy-bg {
        transition: none;
    }
}