/**
 * Lazy Loading Styles
 * 
 * Provides visual feedback during image loading states:
 * - Placeholder state (before load)
 * - Loading state (during load)
 * - Loaded state (after load)
 * - Error state (failed to load)
 */

/* Base lazy image styles */
.lazy-image,
img[data-src],
img[loading="lazy"] {
    background-color: var(--color-bg-secondary, #1a1a2e);
    transition: opacity 0.3s ease-in-out;
}

/* Placeholder state */
.lazy-placeholder {
    opacity: 1;
    background: linear-gradient(
        135deg,
        var(--color-bg-secondary, #1a1a2e) 0%,
        var(--color-bg-tertiary, #252540) 50%,
        var(--color-bg-secondary, #1a1a2e) 100%
    );
    background-size: 200% 200%;
    animation: lazyShimmer 1.5s ease-in-out infinite;
}

/* Loading state - shimmer animation */
.lazy-loading {
    opacity: 0.7;
    animation: lazyShimmer 1s ease-in-out infinite;
}

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

/* Error state */
.lazy-error {
    opacity: 0.5;
    filter: grayscale(100%);
}

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

/* Fade in animation */
@keyframes lazyFadeIn {
    from {
        opacity: 0;
        transform: scale(0.98);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* Apply fade-in to newly loaded images */
.lazy-loaded {
    animation: lazyFadeIn 0.3s ease-out forwards;
}

/* ============================================================================
   Card Header Images - Specific optimizations
   ============================================================================ */

.card-header-image {
    /* Ensure images don't cause layout shift */
    aspect-ratio: 16 / 9;
    object-fit: cover;
    width: 100%;
    
    /* Smooth loading */
    background-color: var(--color-bg-secondary, #1a1a2e);
    
    /* Hardware acceleration for smooth animations */
    will-change: opacity;
    transform: translateZ(0);
}

.card-header-media {
    /* Prevent layout shift */
    min-height: 140px;
    position: relative;
    overflow: hidden;
    border-radius: var(--radius-md, 8px);
    background-color: var(--color-bg-secondary, #1a1a2e);
    transition: background-color 0.3s ease;
}

/* Hide placeholder background when image is loaded */
.card-header-media:has(.lazy-loaded) {
    background-color: transparent;
}

/* Loading indicator for card images */
.card-header-media::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(
        90deg,
        transparent,
        rgba(255, 255, 255, 0.05),
        transparent
    );
    animation: cardImageShimmer 1.5s infinite;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.card-header-media:not(:has(.lazy-loaded))::before {
    opacity: 1;
}

@keyframes cardImageShimmer {
    0% {
        transform: translateX(-100%);
    }
    100% {
        transform: translateX(100%);
    }
}

/* ============================================================================
   Index Header Images
   ============================================================================ */

.index-header-image {
    /* Prevent layout shift */
    aspect-ratio: 21 / 9;
    object-fit: cover;
    width: 100%;
    max-height: 400px;
    
    /* Smooth loading */
    background-color: var(--color-bg-secondary, #1a1a2e);
    border-radius: var(--radius-lg, 12px);
}

/* ============================================================================
   Search Result Previews
   ============================================================================ */

.search-result-preview {
    /* Fixed size to prevent layout shift */
    width: 48px;
    height: 48px;
    object-fit: cover;
    border-radius: var(--radius-md, 8px);
    background-color: var(--color-bg-secondary, #1a1a2e);
    flex-shrink: 0;
}

/* ============================================================================
   Avatar Images
   ============================================================================ */

.profile-avatar img,
.avatar-preview-container img,
#avatarImage,
#avatarPreview {
    background-color: var(--color-bg-secondary, #1a1a2e);
    object-fit: cover;
}

/* ============================================================================
   GIF-specific optimizations
   ============================================================================ */

/* Pause GIF animations when not in viewport (via JS) */
.gif-paused {
    animation-play-state: paused !important;
}

/* Indicator for GIF content */
.card-header-media[data-media-type="gif"]::after {
    content: 'GIF';
    position: absolute;
    bottom: 8px;
    right: 8px;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    font-size: 10px;
    font-weight: 600;
    padding: 2px 6px;
    border-radius: 4px;
    letter-spacing: 0.5px;
    z-index: 10; /* Ensure GIF label appears above the image */
}

/* ============================================================================
   Video elements (converted from GIF)
   ============================================================================ */

/* Videos that replace GIFs should behave like images */
.gif-video,
video[data-src] {
    /* Same styling as lazy images */
    background-color: var(--color-bg-secondary, #1a1a2e);
    transition: opacity 0.3s ease-in-out;
    
    /* Ensure video fills container */
    width: 100%;
    height: 100%;
    object-fit: cover;
    
    /* Remove video controls styling */
    outline: none;
}

/* Loading state for videos */
video.lazy-loading {
    opacity: 0.7;
}

/* Loaded state for videos */
video.lazy-loaded {
    opacity: 1;
    animation: lazyFadeIn 0.3s ease-out forwards;
}

/* Video in card headers */
.card-header-media video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* Indicator for video content (converted from GIF) */
.card-header-media[data-media-type="video"]::after {
    content: '▶';
    position: absolute;
    bottom: 8px;
    right: 8px;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    font-size: 10px;
    font-weight: 600;
    padding: 2px 6px;
    border-radius: 4px;
    z-index: 10; /* Ensure video label appears above the video */
}

/* ============================================================================
   Responsive images (srcset)
   ============================================================================ */

/* Images with srcset should maintain aspect ratio */
img[srcset],
img[data-srcset] {
    width: 100%;
    height: auto;
    max-width: 100%;
}

/* Ensure proper sizing hints are respected */
img[sizes] {
    width: 100%;
}

/* ============================================================================
   Responsive adjustments
   ============================================================================ */

@media (max-width: 768px) {
    .card-header-image {
        aspect-ratio: 4 / 3;
    }
    
    .index-header-image {
        aspect-ratio: 16 / 9;
        max-height: 250px;
    }
}

/* ============================================================================
   Print styles - load all images
   ============================================================================ */

@media print {
    .lazy-image,
    img[data-src] {
        opacity: 1 !important;
    }
}

/* ============================================================================
   Reduced motion preference
   ============================================================================ */

@media (prefers-reduced-motion: reduce) {
    .lazy-placeholder,
    .lazy-loading,
    .card-header-media::before {
        animation: none;
    }
    
    .lazy-loaded {
        animation: none;
        opacity: 1;
    }
}

