/**
 * Responsive Image Fixes for Ahur Lab
 * Fixes image overflow issues on mobile and desktop
 * Created: September 2024
 */

/* ============================================
   Global Image Constraints
   ============================================ */

/* Ensure all images respect container boundaries */
img {
    max-width: 100% !important;
    height: auto !important;
    display: block;
    object-fit: contain;
}

/* Specific fix for images with img-fluid class */
.img-fluid {
    max-width: 100% !important;
    height: auto !important;
}

/* ============================================
   Post List Image Fixes
   ============================================ */

/* Post card image container */
.post-image {
    position: relative;
    overflow: hidden;
    width: 100%;
    max-width: 100%;
    aspect-ratio: 16 / 9;
    background: var(--bg-surface, #f8f9fa);
}

/* Post card images */
.post-image img {
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    object-fit: cover !important;
    display: block;
    transition: transform 0.3s ease;
}

/* Prevent image overflow in post cards */
.post-card {
    overflow: hidden;
    max-width: 100%;
}

.post-card article {
    overflow: hidden;
}

/* ============================================
   Single Post Image Fixes
   ============================================ */

/* Featured image in single posts */
.post-featured-image {
    width: 100%;
    max-width: 100%;
    margin: 1.5rem 0;
    overflow: hidden;
    border-radius: 0.5rem;
}

.post-featured-image img {
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    display: block;
    object-fit: cover;
    border-radius: 0.5rem;
}

/* Content area images */
.post-content img,
article.post img,
.content-area img {
    max-width: 100% !important;
    height: auto !important;
    display: block;
    margin: 1.5rem auto;
    border-radius: 0.25rem;
}

/* ============================================
   Related Posts Image Fixes
   ============================================ */

.related-posts .card-img-top {
    width: 100% !important;
    max-width: 100% !important;
    height: 150px !important;
    object-fit: cover !important;
}

/* ============================================
   Mobile-Specific Fixes
   ============================================ */

@media (max-width: 768px) {
    /* Post images on mobile */
    .post-image {
        aspect-ratio: 16 / 10; /* Slightly taller on mobile */
        margin-bottom: 1rem;
    }

    .post-image img {
        width: 100% !important;
        height: 100% !important;
        object-fit: cover !important;
    }

    /* Single post featured image on mobile */
    .post-featured-image {
        margin: 1rem -15px; /* Full width on mobile */
        border-radius: 0;
        max-width: calc(100% + 30px);
    }

    .post-featured-image img {
        border-radius: 0;
    }

    /* Prevent horizontal scroll */
    .container,
    .container-fluid,
    .row,
    .col,
    [class*="col-"] {
        max-width: 100% !important;
        overflow-x: hidden;
    }

    /* Post card adjustments for mobile */
    .post-card {
        margin-bottom: 1.5rem;
    }

    /* Ensure post grid doesn't overflow */
    .posts-grid,
    .post-list {
        max-width: 100%;
        overflow-x: hidden;
        padding: 0;
    }
}

/* Small mobile devices */
@media (max-width: 576px) {
    .post-image {
        aspect-ratio: 16 / 12; /* Even taller on small screens */
    }

    /* Full-width images on small screens */
    article img,
    .post-content img {
        margin-left: -15px;
        margin-right: -15px;
        max-width: calc(100% + 30px) !important;
        border-radius: 0;
    }
}

/* ============================================
   Desktop-Specific Fixes
   ============================================ */

@media (min-width: 769px) {
    /* Limit max width of featured images on large screens */
    .post-featured-image {
        max-width: 100%;
        margin: 2rem auto;
    }

    .post-featured-image img {
        max-height: 500px;
        object-fit: cover;
    }

    /* Post grid images */
    .post-card .post-image {
        height: 250px;
        aspect-ratio: auto;
    }

    /* Three-column grid image sizing */
    @media (min-width: 992px) {
        .col-lg-4 .post-image {
            height: 200px;
        }
    }

    /* Content images max width */
    .post-content img,
    article.post img {
        max-width: min(100%, 800px) !important;
        margin: 2rem auto;
    }
}

/* ============================================
   Container Width Constraints
   ============================================ */

/* Prevent container overflow */
.container {
    max-width: 100%;
    overflow-x: hidden;
}

.container-fluid {
    max-width: 100%;
    padding-right: 15px;
    padding-left: 15px;
    overflow-x: hidden;
}

/* Row and column constraints */
.row {
    margin-right: -15px;
    margin-left: -15px;
    max-width: calc(100% + 30px);
}

/* ============================================
   Utility Classes
   ============================================ */

/* Force responsive image */
.img-responsive {
    max-width: 100% !important;
    height: auto !important;
    display: block;
}

/* Contain image within bounds */
.img-contain {
    max-width: 100% !important;
    height: auto !important;
    object-fit: contain !important;
}

/* Cover image area */
.img-cover {
    width: 100% !important;
    height: 100% !important;
    object-fit: cover !important;
}

/* ============================================
   Error State Images
   ============================================ */

/* Broken image placeholder */
img[alt]:not([src]),
img[src=""],
img:not([src]) {
    position: relative;
    display: block;
    width: 100%;
    min-height: 200px;
    background: linear-gradient(135deg, #e2e8f0, #cbd5e1);
    border: 2px dashed #94a3b8;
    border-radius: 0.5rem;
}

img[alt]:not([src])::before,
img[src=""]::before {
    content: "🖼️";
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 3rem;
    opacity: 0.5;
}

img[alt]:not([src])::after,
img[src=""]::after {
    content: attr(alt);
    position: absolute;
    bottom: 1rem;
    left: 50%;
    transform: translateX(-50%);
    font-size: 0.875rem;
    color: #64748b;
    text-align: center;
    max-width: 90%;
}

/* ============================================
   Print Styles
   ============================================ */

@media print {
    img {
        max-width: 100% !important;
        page-break-inside: avoid;
    }

    .post-image {
        page-break-inside: avoid;
    }
}

/* ============================================
   High Priority Overrides
   ============================================ */

/* Override any conflicting styles with high specificity */
body .container img,
body .container-fluid img,
body article img,
body .post-card img,
body .post-image img,
body .post-content img,
body .post-featured-image img {
    max-width: 100% !important;
}

/* Prevent images from breaking layout */
* {
    max-width: 100%;
}

/* Exception for specific elements that need to exceed container */
.navbar,
.fixed-top,
.fixed-bottom {
    max-width: 100vw;
}
