

@php
    // Correctly handle the case where 'images' might be null and use 'images_count'.
    $imageCount = $studentLife->images_count ?? 6;
    $images = collect($studentLife->images ?? [])->take($imageCount);
@endphp

@if($images->isNotEmpty())
<div class="gallery-section">
    @foreach ($images as $image)
        <div class="gallery-item">
            <img src="{{ asset('storage/' . $image) }}" alt="Student life gallery image">
        </div>
    @endforeach
</div>
@endif

<style>
    .gallery-section {
        display: grid;
        grid-template-columns: repeat(6, 1fr);
        grid-auto-rows: 250px;
        gap: 1rem;
        padding: 2rem;
        margin: 0 auto;
        max-width: 1200px;
    }

    .gallery-item {
        overflow: hidden;
        border-radius: 12px;
        box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    }

    .gallery-item img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: transform 0.4s ease-in-out;
    }

    .gallery-item img:hover {
        transform: scale(1.08);
    }

    .gallery-item:nth-child(1) {
        grid-column: 1 / span 2;
        grid-row: 1 / span 1;
    }

    .gallery-item:nth-child(2) {
        grid-column: 3 / span 1;
        grid-row: 1 / span 1;
    }

    .gallery-item:nth-child(3) {
        grid-column: 4 / span 3;
        grid-row: 1 / span 2;
    }

    .gallery-item:nth-child(4) {
        grid-column: 1 / span 1;
        grid-row: 2 / span 1;
    }

    .gallery-item:nth-child(5) {
        grid-column: 2 / span 1;
        grid-row: 2 / span 1;
    }

    .gallery-item:nth-child(6) {
        grid-column: 3 / span 1;
        grid-row: 2 / span 1;
    }

    /* Enhanced responsive styles */
    @media (max-width: 1200px) {
        .gallery-section {
            max-width: 1000px;
            grid-auto-rows: 220px;
            gap: 0.9rem;
            padding: 1.8rem;
        }
        .gallery-item {
            border-radius: 10px;
        }
        .gallery-item img:hover {
            transform: scale(1.06);
        }
    }

    @media (max-width: 992px) {
        .gallery-section {
            grid-template-columns: repeat(4, 1fr);
            grid-auto-rows: 200px;
            gap: 0.8rem;
            padding: 1.5rem;
            max-width: 900px;
        }
        .gallery-item:nth-child(3) {
            grid-column: 3 / span 2;
            grid-row: 1 / span 2;
        }
        .gallery-item:nth-child(4) {
            grid-column: 1 / span 2;
            grid-row: 2 / span 1;
        }
        .gallery-item:nth-child(5) {
            grid-column: 1 / span 1;
            grid-row: 3 / span 1;
        }
        .gallery-item:nth-child(6) {
            grid-column: 2 / span 2;
            grid-row: 3 / span 1;
        }
        .gallery-item {
            border-radius: 8px;
        }
        .gallery-item img:hover {
            transform: scale(1.05);
        }
    }

    @media (max-width: 768px) {
        .gallery-section {
            grid-template-columns: repeat(2, 1fr);
            grid-auto-rows: 180px;
            gap: 0.7rem;
            padding: 1.2rem;
            max-width: 700px;
        }
        .gallery-item:nth-child(n) {
            grid-column: auto;
            grid-row: auto;
        }
        .gallery-item:nth-child(3) {
            grid-column: 1 / span 2;
        }
        .gallery-item {
            border-radius: 6px;
        }
        .gallery-item img:hover {
            transform: scale(1.04);
        }
    }

    @media (max-width: 600px) {
        .gallery-section {
            grid-auto-rows: 160px;
            gap: 0.6rem;
            padding: 1rem;
            max-width: 600px;
        }
        .gallery-item {
            border-radius: 5px;
        }
        .gallery-item img:hover {
            transform: scale(1.03);
        }
    }

    @media (max-width: 480px) {
        .gallery-section {
            grid-auto-rows: 140px;
            gap: 0.5rem;
            padding: 0.8rem;
            max-width: 500px;
        }
        .gallery-item {
            border-radius: 4px;
        }
        .gallery-item img:hover {
            transform: scale(1.02);
        }
    }

    @media (max-width: 400px) {
        .gallery-section {
            grid-auto-rows: 120px;
            gap: 0.4rem;
            padding: 0.6rem;
            max-width: 400px;
        }
        .gallery-item {
            border-radius: 3px;
        }
        .gallery-item img:hover {
            transform: scale(1.01);
        }
    }

    @media (max-width: 360px) {
        .gallery-section {
            grid-auto-rows: 100px;
            gap: 0.3rem;
            padding: 0.5rem;
            max-width: 350px;
        }
        .gallery-item {
            border-radius: 2px;
        }
        .gallery-item img:hover {
            transform: scale(1.005);
        }
    }
</style>