/* ============================================
   RSL Gallery Component
   Version: 1.0.0
   Description: Masonry-style image gallery with
   filtering, lightbox, and full accessibility support
   ============================================ */

/* ============================================
   CSS Custom Properties
   ============================================ */
:root {
    --rsl-gallery-gap: 1rem;
    --rsl-gallery-radius: 8px;
    --rsl-gallery-transition: 0.3s ease;
    --rsl-gallery-overlay-bg: linear-gradient(to top, rgba(0, 0, 0, 0.8) 0%, transparent 60%);
    --rsl-gallery-focus-color: #6E7BFF;
    --rsl-gallery-focus-shadow: 0 8px 32px rgba(0, 0, 0, 0.3), 0 0 0 4px rgba(110, 123, 255, 0.3);
}

/* ============================================
   Gallery Filter Buttons
   ============================================ */
.rsl-gallery-filter {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    justify-content: center;
    margin-bottom: 2rem;
}

.rsl-gallery-filter-btn {
    padding: 0.75rem 1.5rem;
    background: transparent;
    border: 2px solid #e0e0e0;
    border-radius: 30px;
    font-size: 0.9rem;
    font-weight: 500;
    color: #666;
    cursor: pointer;
    transition: all var(--rsl-gallery-transition);
}

.rsl-gallery-filter-btn:hover {
    border-color: #333;
    color: #333;
}

.rsl-gallery-filter-btn.active {
    background: #333;
    border-color: #333;
    color: #fff;
}

.rsl-gallery-filter-btn:focus-visible {
    outline: 3px solid var(--rsl-gallery-focus-color);
    outline-offset: 2px;
}

/* ============================================
   Masonry Gallery Grid
   ============================================ */
.rsl-gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: var(--rsl-gallery-gap);
    grid-auto-flow: dense;
}

/* Column-based layouts */
.rsl-gallery[data-cols="2"] {
    grid-template-columns: repeat(2, 1fr);
}

.rsl-gallery[data-cols="3"] {
    grid-template-columns: repeat(3, 1fr);
}

.rsl-gallery[data-cols="4"] {
    grid-template-columns: repeat(4, 1fr);
}

/* ============================================
   Gallery Items
   ============================================ */
.rsl-gallery-item {
    position: relative;
    overflow: hidden;
    border-radius: var(--rsl-gallery-radius);
    aspect-ratio: 4/3;
    background: #f0f0f0;
    transition: transform var(--rsl-gallery-transition), box-shadow var(--rsl-gallery-transition);
}

/* Size variants */
.rsl-gallery-item-tall {
    grid-row: span 2;
    aspect-ratio: 3/4;
}

.rsl-gallery-item-wide {
    grid-column: span 2;
    aspect-ratio: 2/1;
}

.rsl-gallery-item-large {
    grid-column: span 2;
    grid-row: span 2;
    aspect-ratio: 1/1;
}

/* ============================================
   Gallery Link & Image
   ============================================ */
.rsl-gallery-link {
    display: block;
    width: 100%;
    height: 100%;
    text-decoration: none;
}

.rsl-gallery-link:focus-visible {
    outline: none;
}

/* Focus state on item */
.rsl-gallery-item:focus-within {
    border: 3px solid var(--rsl-gallery-focus-color);
    box-shadow: var(--rsl-gallery-focus-shadow);
    z-index: 10;
}

.rsl-gallery-image {
    width: 100%;
    height: 100%;
}

.rsl-gallery-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.6s ease;
}

.rsl-gallery-item:hover .rsl-gallery-image img,
.rsl-gallery-item:focus-within .rsl-gallery-image img {
    transform: scale(1.05);
}

/* ============================================
   Gallery Overlay
   ============================================ */
.rsl-gallery-overlay {
    position: absolute;
    inset: 0;
    background: var(--rsl-gallery-overlay-bg);
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    padding: 1.5rem;
    opacity: 0;
    transition: opacity var(--rsl-gallery-transition);
}

.rsl-gallery-item:hover .rsl-gallery-overlay,
.rsl-gallery-item:focus-within .rsl-gallery-overlay {
    opacity: 1;
}

.rsl-gallery-info {
    color: #fff;
}

.rsl-gallery-title {
    font-size: 1.25rem;
    font-weight: 500;
    margin: 0 0 0.25rem 0;
}

.rsl-gallery-meta {
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.7);
    margin: 0;
}

.rsl-gallery-icon {
    position: absolute;
    top: 1rem;
    right: 1rem;
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    backdrop-filter: blur(4px);
}

/* ============================================
   Lightbox
   ============================================ */
.rsl-lightbox {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.95);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.rsl-lightbox[aria-hidden="false"],
.rsl-lightbox:not([hidden]) {
    opacity: 1;
    visibility: visible;
}

.rsl-lightbox-content {
    position: relative;
    max-width: 90vw;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.rsl-lightbox-image {
    max-width: 100%;
    max-height: 80vh;
    object-fit: contain;
    border-radius: 4px;
}

.rsl-lightbox-caption {
    text-align: center;
    color: #fff;
    margin-top: 1rem;
}

.rsl-lightbox-title {
    font-size: 1.25rem;
    font-weight: 500;
    margin: 0 0 0.25rem 0;
}

.rsl-lightbox-meta {
    font-size: 0.9rem;
    color: rgba(255, 255, 255, 0.7);
    margin: 0;
}

/* Lightbox Controls */
.rsl-lightbox-close,
.rsl-lightbox-prev,
.rsl-lightbox-next {
    position: absolute;
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: #fff;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    transition: background 0.2s ease, transform 0.2s ease;
}

.rsl-lightbox-close:hover,
.rsl-lightbox-prev:hover,
.rsl-lightbox-next:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(1.1);
}

.rsl-lightbox-close:focus-visible,
.rsl-lightbox-prev:focus-visible,
.rsl-lightbox-next:focus-visible {
    outline: 3px solid var(--rsl-gallery-focus-color);
    outline-offset: 2px;
}

.rsl-lightbox-close {
    top: 1rem;
    right: 1rem;
}

.rsl-lightbox-prev {
    left: 1rem;
    top: 50%;
    transform: translateY(-50%);
}

.rsl-lightbox-next {
    right: 1rem;
    top: 50%;
    transform: translateY(-50%);
}

.rsl-lightbox-prev:hover,
.rsl-lightbox-next:hover {
    transform: translateY(-50%) scale(1.1);
}

.rsl-lightbox-counter {
    position: absolute;
    bottom: 1rem;
    left: 50%;
    transform: translateX(-50%);
    color: rgba(255, 255, 255, 0.7);
    font-size: 0.9rem;
}

/* ============================================
   Live Region for Announcements
   ============================================ */
.rsl-gallery-status {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ============================================
   Filter Animation
   ============================================ */
@keyframes rsl-gallery-fadeIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes rsl-gallery-fadeOut {
    from {
        opacity: 1;
        transform: scale(1);
    }
    to {
        opacity: 0;
        transform: scale(0.95);
    }
}

.rsl-gallery-item.rsl-filtering-in {
    animation: rsl-gallery-fadeIn 0.4s ease forwards;
}

.rsl-gallery-item.rsl-filtering-out {
    animation: rsl-gallery-fadeOut 0.3s ease forwards;
}

/* ============================================
   Dark Mode
   ============================================ */
[data-theme="dark"] .rsl-gallery-filter-btn {
    border-color: #444;
    color: #aaa;
}

[data-theme="dark"] .rsl-gallery-filter-btn:hover {
    border-color: #888;
    color: #fff;
}

[data-theme="dark"] .rsl-gallery-filter-btn.active {
    background: #fff;
    border-color: #fff;
    color: #111;
}

[data-theme="dark"] .rsl-gallery-item {
    background: #2a2a2a;
}

[data-theme="dark"] .rsl-lightbox {
    background: rgba(0, 0, 0, 0.98);
}

/* ============================================
   Reduced Motion
   ============================================ */
@media (prefers-reduced-motion: reduce) {
    .rsl-gallery-image img,
    .rsl-gallery-overlay,
    .rsl-lightbox,
    .rsl-gallery-item {
        transition: none;
    }

    .rsl-gallery-item.rsl-filtering-in,
    .rsl-gallery-item.rsl-filtering-out {
        animation: none;
    }

    .rsl-gallery-item:hover .rsl-gallery-image img,
    .rsl-gallery-item:focus-within .rsl-gallery-image img {
        transform: none;
    }
}

/* ============================================
   Responsive Breakpoints
   ============================================ */
@media (max-width: 1024px) {
    .rsl-gallery[data-cols="4"] {
        grid-template-columns: repeat(3, 1fr);
    }

    .rsl-gallery-item-wide {
        grid-column: span 1;
        aspect-ratio: 4/3;
    }
}

@media (max-width: 768px) {
    .rsl-gallery {
        grid-template-columns: repeat(2, 1fr);
    }

    .rsl-gallery[data-cols="3"],
    .rsl-gallery[data-cols="4"] {
        grid-template-columns: repeat(2, 1fr);
    }

    .rsl-gallery-item-tall {
        grid-row: span 1;
        aspect-ratio: 4/3;
    }

    .rsl-gallery-item-large {
        grid-column: span 2;
        grid-row: span 1;
    }

    .rsl-lightbox-prev,
    .rsl-lightbox-next {
        width: 40px;
        height: 40px;
        font-size: 1rem;
    }
}

@media (max-width: 480px) {
    .rsl-gallery {
        grid-template-columns: 1fr;
    }

    .rsl-gallery[data-cols="2"],
    .rsl-gallery[data-cols="3"],
    .rsl-gallery[data-cols="4"] {
        grid-template-columns: 1fr;
    }

    .rsl-gallery-item-wide,
    .rsl-gallery-item-large {
        grid-column: span 1;
    }

    .rsl-gallery-filter {
        flex-direction: column;
        align-items: stretch;
    }

    .rsl-gallery-filter-btn {
        text-align: center;
    }
}

/* ============================================
   FilterBus Integration
   ============================================ */

/* Hide gallery items filtered out by FilterBus */
.rsl-gallery-item.rsl-filterbus-hidden {
    display: none !important;
}

/* Hide gallery items filtered out by date range */
.rsl-gallery-item.rsl-date-hidden {
    display: none !important;
}

/* Smooth transitions for FilterBus visibility changes */
.rsl-gallery-initialized .rsl-gallery-item {
    transition: opacity 0.3s ease, transform 0.3s ease;
}

/* Prevent flash on page load before FilterBus initializes */
.rsl-gallery:not(.rsl-gallery-initialized) .rsl-gallery-item[data-filter-subscribe] {
    opacity: 1;
}

@media (prefers-reduced-motion: reduce) {
    .rsl-gallery-initialized .rsl-gallery-item {
        transition: none;
    }
}
