/**
 * Lazy Loading CSS
 * Provides smooth loading transitions and placeholder styles
 */

/* Base lazy image styles */
.lazy-image {
    transition: opacity 0.3s ease-in-out, filter 0.3s ease-in-out;
    will-change: opacity, filter;
}

/* Loading state */
.lazy-loading {
    opacity: 0.7;
    filter: blur(2px);
    background-color: #f8f9fa;
    background-image: linear-gradient(90deg, #f8f9fa 25%, #e9ecef 50%, #f8f9fa 75%);
    background-size: 200% 100%;
    animation: lazy-shimmer 1.5s infinite;
}

/* Loaded state */
.lazy-loaded {
    opacity: 1;
    filter: none;
    animation: none;
}

/* Error state */
.lazy-error {
    opacity: 0.5;
    filter: grayscale(100%);
    background-color: #f8f9fa;
    position: relative;
}

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

/* Picture element styles */
.lazy-picture {
    display: block;
    width: 100%;
    height: auto;
}

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

/* Background image lazy loading */
[data-bg-src] {
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    position: relative;
}

[data-bg-src]:not(.lazy-bg-loaded) {
    background-color: #f8f9fa;
    background-image: linear-gradient(90deg, #f8f9fa 25%, #e9ecef 50%, #f8f9fa 75%);
    background-size: 200% 100%;
    animation: lazy-shimmer 1.5s infinite;
}

[data-bg-src]:not(.lazy-bg-loaded)::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(248, 249, 250, 0.8);
    z-index: 1;
}

.lazy-bg-loaded {
    animation: none;
}

.lazy-bg-loaded::before {
    display: none;
}

.lazy-bg-error {
    background-color: #f8f9fa !important;
    background-image: none !important;
}

.lazy-bg-error::after {
    content: '⚠️ Image failed to load';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: #6c757d;
    font-size: 0.875rem;
    text-align: center;
    z-index: 2;
}

/* Iframe lazy loading */
iframe[data-src] {
    background-color: #f8f9fa;
    background-image: linear-gradient(90deg, #f8f9fa 25%, #e9ecef 50%, #f8f9fa 75%);
    background-size: 200% 100%;
    animation: lazy-shimmer 1.5s infinite;
}

iframe.lazy-loaded {
    animation: none;
    background: none;
}

/* Shimmer animation */
@keyframes lazy-shimmer {
    0% {
        background-position: -200% 0;
    }
    100% {
        background-position: 200% 0;
    }
}

/* Fade in animation */
@keyframes lazy-fade-in {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.lazy-fade-in {
    animation: lazy-fade-in 0.5s ease-out;
}

/* Responsive image containers */
.lazy-container {
    position: relative;
    overflow: hidden;
    background-color: #f8f9fa;
}

.lazy-container::before {
    content: '';
    display: block;
    width: 100%;
    height: 0;
    padding-bottom: 56.25%; /* 16:9 aspect ratio */
}

.lazy-container.aspect-1-1::before {
    padding-bottom: 100%; /* 1:1 aspect ratio */
}

.lazy-container.aspect-4-3::before {
    padding-bottom: 75%; /* 4:3 aspect ratio */
}

.lazy-container.aspect-3-2::before {
    padding-bottom: 66.67%; /* 3:2 aspect ratio */
}

.lazy-container img,
.lazy-container picture {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* Loading spinner for critical images */
.lazy-spinner {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 40px;
    height: 40px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid #007bff;
    border-radius: 50%;
    animation: lazy-spin 1s linear infinite;
    z-index: 2;
}

@keyframes lazy-spin {
    0% { transform: translate(-50%, -50%) rotate(0deg); }
    100% { transform: translate(-50%, -50%) rotate(360deg); }
}

.lazy-loaded + .lazy-spinner {
    display: none;
}

/* Progressive enhancement for modern browsers */
@supports (object-fit: cover) {
    .lazy-image {
        object-fit: cover;
    }
}

/* Reduced motion preferences */
@media (prefers-reduced-motion: reduce) {
    .lazy-image,
    .lazy-loading,
    .lazy-loaded,
    [data-bg-src],
    iframe[data-src] {
        transition: none;
        animation: none;
    }
    
    .lazy-loading {
        opacity: 0.8;
        filter: none;
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .lazy-loading {
        background-color: #000;
        color: #fff;
    }
    
    .lazy-error::after {
        color: #fff;
        background-color: #000;
        padding: 0.5rem;
        border-radius: 0.25rem;
    }
}

/* Print styles */
@media print {
    .lazy-image,
    .lazy-loading,
    [data-bg-src],
    iframe[data-src] {
        opacity: 1 !important;
        filter: none !important;
        animation: none !important;
        background: none !important;
    }
    
    .lazy-spinner {
        display: none !important;
    }
}

/* Mobile optimizations */
@media (max-width: 768px) {
    .lazy-loading {
        filter: blur(1px); /* Less blur on mobile for better performance */
    }
    
    .lazy-container::before {
        padding-bottom: 75%; /* Adjust aspect ratio for mobile */
    }
}

/* Retina display optimizations */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .lazy-image {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}
