/**
 * Image Loading Fix CSS
 * Critical CSS for preventing FOUC (Flash of Unstyled Content)
 * และปรับปรุงการโหลดรูปภาพ
 */

/* 🖼️ Image Loading States */
img {
    /* ป้องกัน layout shift ขณะโหลดรูป */
    max-width: 100%;
    height: auto;
    
    /* Smooth transition เมื่อรูปโหลดเสร็จ */
    transition: opacity 0.3s ease-in-out;
}

/* Loading placeholder สำหรับรูปที่ยังไม่โหลด */
img:not([src]),
img[src=""],
img[src="#"] {
    opacity: 0;
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading-shimmer 1.5s infinite;
}

/* Shimmer animation สำหรับ loading state */
@keyframes loading-shimmer {
    0% {
        background-position: -200% 0;
    }
    100% {
        background-position: 200% 0;
    }
}

/* รูปที่โหลดเสร็จแล้ว */
img.loaded,
img[data-loaded="true"] {
    opacity: 1;
}

/* Lazy loading support */
img[loading="lazy"] {
    opacity: 0;
    transition: opacity 0.3s ease-in-out;
}

img[loading="lazy"].loaded {
    opacity: 1;
}

/* 🎯 Specific image containers */
.property-image,
.listing-image,
.gallery-image {
    position: relative;
    overflow: hidden;
    background: #f8f9fa;
}

.property-image img,
.listing-image img,
.gallery-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s ease, opacity 0.3s ease;
}

/* Loading spinner สำหรับรูปใหญ่ */
.property-image::before,
.listing-image::before,
.gallery-image::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 40px;
    height: 40px;
    margin: -20px 0 0 -20px;
    border: 3px solid #f3f3f3;
    border-top: 3px solid #007bff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.property-image.loading::before,
.listing-image.loading::before,
.gallery-image.loading::before {
    opacity: 1;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* 🚀 Performance optimizations */
.slick-slider img,
.carousel img,
.gallery img {
    /* ป้องกัน reflow/repaint */
    will-change: transform;
    backface-visibility: hidden;
    transform: translateZ(0);
}

/* 📱 Mobile optimizations */
@media (max-width: 768px) {
    img {
        /* ลด quality บนมือถือเพื่อประหยัด bandwidth */
        image-rendering: optimizeSpeed;
    }
    
    .property-image::before,
    .listing-image::before,
    .gallery-image::before {
        width: 30px;
        height: 30px;
        margin: -15px 0 0 -15px;
        border-width: 2px;
    }
}

/* 🎨 Hover effects */
.property-image:hover img,
.listing-image:hover img {
    transform: scale(1.05);
}

/* Error state สำหรับรูปที่โหลดไม่ได้ */
img[alt]:after {
    content: attr(alt);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0,0,0,0.7);
    color: white;
    padding: 10px;
    border-radius: 4px;
    font-size: 12px;
    text-align: center;
    opacity: 0;
    pointer-events: none;
}

img[src=""]:after,
img:not([src]):after {
    opacity: 1;
}

/* 🔧 Critical layout fixes */
.container img,
.row img,
.col img {
    /* ป้องกัน overflow */
    max-width: 100%;
    height: auto;
}

/* Aspect ratio containers */
.aspect-ratio-16-9 {
    position: relative;
    width: 100%;
    height: 0;
    padding-bottom: 56.25%; /* 16:9 */
}

.aspect-ratio-4-3 {
    position: relative;
    width: 100%;
    height: 0;
    padding-bottom: 75%; /* 4:3 */
}

.aspect-ratio-1-1 {
    position: relative;
    width: 100%;
    height: 0;
    padding-bottom: 100%; /* 1:1 */
}

.aspect-ratio-16-9 img,
.aspect-ratio-4-3 img,
.aspect-ratio-1-1 img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* 🎯 Motorcycle gallery specific fixes */
.motorcycle-slick-top img,
.motorcycle-slick-btm img {
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.motorcycle-slick-btm img {
    cursor: pointer;
    transition: all 0.3s ease;
}

.motorcycle-slick-btm .slick-current img {
    border: 2px solid #007bff;
    transform: scale(1.05);
}

/* 🌟 Additional performance hints */
img[data-src] {
    /* Lazy loading placeholder */
    background: linear-gradient(45deg, #f0f0f0 25%, transparent 25%), 
                linear-gradient(-45deg, #f0f0f0 25%, transparent 25%), 
                linear-gradient(45deg, transparent 75%, #f0f0f0 75%), 
                linear-gradient(-45deg, transparent 75%, #f0f0f0 75%);
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
    min-height: 200px;
}

/* Print styles */
@media print {
    img {
        max-width: 100% !important;
        height: auto !important;
        page-break-inside: avoid;
    }
}