/**
 * SOS Media Library – Gallery Frontend
 *
 * Layouts: grid, columns, rows, single, random.
 * Fully responsive. WCAG 2.2 AA compliant.
 *
 * Focus indicators: 3px solid #CC3300 (SOS red) on all interactive elements.
 * Minimum 3:1 contrast against adjacent colours (WCAG 2.4.7 / 2.4.11).
 */

/* ─── Base ──────────────────────────────────────────────────────────── */

.sos-gallery {
    --sos-gal-cols: 3;
    --sos-gal-gap: 12px;
    --sos-gal-radius: 4px;
    --sos-gal-focus: #CC3300;
    margin: 1.5em 0;
    width: 100%;
}

.sos-gallery *,
.sos-gallery *::before,
.sos-gallery *::after {
    box-sizing: border-box;
}

/* ─── Item wrapper ──────────────────────────────────────────────────── */

.sos-gallery__item {
    margin: 0;
    padding: 0;
    position: relative;
    border-radius: var(--sos-gal-radius);
}

/* ─── Image ─────────────────────────────────────────────────────────── */

.sos-gallery__img {
    display: block;
    border-radius: var(--sos-gal-radius);
    transition: transform 0.3s ease, opacity 0.3s ease;
}

/* ─── Link (wraps entire card: image + caption) ─────────────────────── */

.sos-gallery__link {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    text-decoration: none;
    color: inherit;
    border-radius: var(--sos-gal-radius);
}

.sos-gallery__link:focus-visible {
    outline: 3px solid var(--sos-gal-focus);
    outline-offset: 3px;
}

.sos-gallery__link:hover .sos-gallery__img {
    transform: scale(1.03);
}

.sos-gallery__link .sos-gallery__caption {
    text-decoration: none;
}

/* ─── Caption ───────────────────────────────────────────────────────── */

.sos-gallery__caption {
    padding: 8px 4px 4px;
    font-size: var(--sos-gal-cap-size, 14px);
    line-height: 1.4;
    color: #3D3427;
    text-align: center;
    margin-top: auto;
}

/* Caption above: reorder so caption comes first. */
.sos-gallery--cap-above .sos-gallery__link,
.sos-gallery--cap-above .sos-gallery__item {
    flex-direction: column-reverse;
}

.sos-gallery--cap-above .sos-gallery__caption {
    margin-top: 0;
    margin-bottom: auto;
    padding: 4px 4px 8px;
}

/* Caption left: side-by-side, caption on the left. */
.sos-gallery--cap-left .sos-gallery__link,
.sos-gallery--cap-left .sos-gallery__item {
    flex-direction: row-reverse;
    align-items: center;
}

.sos-gallery--cap-left .sos-gallery__caption {
    margin-top: 0;
    padding: 4px 12px 4px 4px;
    text-align: right;
    flex-shrink: 1;
}

/* Caption right: side-by-side, caption on the right. */
.sos-gallery--cap-right .sos-gallery__link,
.sos-gallery--cap-right .sos-gallery__item {
    flex-direction: row;
    align-items: center;
}

.sos-gallery--cap-right .sos-gallery__caption {
    margin-top: 0;
    padding: 4px 4px 4px 12px;
    text-align: left;
    flex-shrink: 1;
}


/* ═══ Layout: Grid ══════════════════════════════════════════════════ */

.sos-gallery--grid {
    display: grid;
    grid-template-columns: repeat(var(--sos-gal-cols), 1fr);
    gap: var(--sos-gal-gap);
    align-items: stretch;
}

.sos-gallery--grid .sos-gallery__item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 0;
}

.sos-gallery--grid .sos-gallery__link {
    width: 100%;
}

.sos-gallery--grid .sos-gallery__img {
    max-width: 100%;
    height: auto;
}


/* ═══ Layout: Columns (masonry-like, CSS columns) ═══════════════════ */

.sos-gallery--columns {
    column-count: var(--sos-gal-cols);
    column-gap: var(--sos-gal-gap);
}

.sos-gallery--columns .sos-gallery__item {
    break-inside: avoid;
    margin-bottom: var(--sos-gal-gap);
    display: inline-block;
    width: 100%;
}

.sos-gallery--columns .sos-gallery__img {
    width: 100% !important;
    height: auto;
    max-width: none;
}


/* ═══ Layout: Rows (horizontal, items keep natural aspect ratio) ═══ */

.sos-gallery--rows {
    display: flex;
    flex-wrap: wrap;
    gap: var(--sos-gal-gap);
    align-items: flex-start;
}

.sos-gallery--rows .sos-gallery__item {
    flex: 0 1 auto;
    max-width: 100%;
}

.sos-gallery--rows .sos-gallery__img {
    height: 260px;
    width: auto;
    max-width: 100%;
    object-fit: cover;
}


/* ═══ Layout: Single ════════════════════════════════════════════════ */

.sos-gallery--single {
    display: flex;
    justify-content: center;
}

.sos-gallery--single .sos-gallery__item {
    max-width: 100%;
}

.sos-gallery--single .sos-gallery__img {
    max-width: 100%;
    height: auto;
}


/* ═══ Layout: Random (same as grid but re-shuffled by PHP) ═══════ */

.sos-gallery--random {
    display: grid;
    grid-template-columns: repeat(var(--sos-gal-cols), 1fr);
    gap: var(--sos-gal-gap);
    align-items: stretch;
}

.sos-gallery--random .sos-gallery__item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 0;
}

.sos-gallery--random .sos-gallery__link {
    width: 100%;
}

.sos-gallery--random .sos-gallery__img {
    max-width: 100%;
    height: auto;
}


/* ═══ Responsive ════════════════════════════════════════════════════ */

@media (max-width: 900px) {
    .sos-gallery--grid,
    .sos-gallery--random {
        grid-template-columns: repeat(
            clamp(1, var(--sos-gal-cols) - 1, 4),
            1fr
        );
    }
    .sos-gallery--columns {
        column-count: clamp(1, calc(var(--sos-gal-cols) - 1), 4);
    }
}

@media (max-width: 600px) {
    .sos-gallery--grid,
    .sos-gallery--random {
        grid-template-columns: repeat(
            clamp(1, var(--sos-gal-cols) - 2, 2),
            1fr
        );
    }
    .sos-gallery--columns {
        column-count: clamp(1, calc(var(--sos-gal-cols) - 2), 2);
    }
    .sos-gallery--rows .sos-gallery__img {
        height: 180px;
    }
}

@media (max-width: 400px) {
    .sos-gallery--grid,
    .sos-gallery--random {
        grid-template-columns: 1fr;
    }
    .sos-gallery--columns {
        column-count: 1;
    }
}


/* ═══ Lightbox ══════════════════════════════════════════════════════ */

.sos-lightbox-overlay {
    position: fixed;
    inset: 0;
    z-index: 999999;
    background: rgba(0, 0, 0, 0.92);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.sos-lightbox-overlay.is-active {
    opacity: 1;
    visibility: visible;
}

.sos-lightbox-content {
    position: relative;
    max-width: 92vw;
    max-height: 90vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.sos-lightbox-img {
    max-width: 92vw;
    max-height: 88vh;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
}

/* Navigation buttons — minimum 44×44px touch target (WCAG 2.5.8) */

.sos-lightbox-btn {
    position: absolute;
    z-index: 2;
    background: rgba(255, 255, 255, 0.15);
    border: 2px solid rgba(255, 255, 255, 0.4);
    color: #fff;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s ease, border-color 0.2s ease;
    padding: 0;
    line-height: 1;
}

.sos-lightbox-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    border-color: rgba(255, 255, 255, 0.7);
}

.sos-lightbox-btn:focus-visible {
    outline: 3px solid var(--sos-gal-focus, #CC3300);
    outline-offset: 3px;
}

.sos-lightbox-btn svg {
    width: 20px;
    height: 20px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2.5;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.sos-lightbox-close {
    position: fixed;
    top: 16px;
    right: 16px;
}

.sos-lightbox-prev {
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    position: fixed;
}

.sos-lightbox-next {
    right: 16px;
    top: 50%;
    transform: translateY(-50%);
    position: fixed;
}

/* Counter */

.sos-lightbox-counter {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    color: rgba(255, 255, 255, 0.7);
    font-size: 14px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* Alt text caption in lightbox */

.sos-lightbox-alt {
    position: fixed;
    bottom: 44px;
    left: 50%;
    transform: translateX(-50%);
    color: rgba(255, 255, 255, 0.85);
    font-size: 14px;
    max-width: 70vw;
    text-align: center;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}


/* ═══ Reduced motion ════════════════════════════════════════════════ */

@media (prefers-reduced-motion: reduce) {
    .sos-gallery__img {
        transition: none;
    }
    .sos-lightbox-overlay {
        transition: none;
    }
    .sos-gallery__link:hover .sos-gallery__img {
        transform: none;
    }
}


/* ═══ High contrast / forced-colors mode ═══════════════════════════ */

@media (forced-colors: active) {
    .sos-gallery__link:focus-visible {
        outline: 3px solid LinkText;
    }
    .sos-lightbox-btn {
        border-color: ButtonText;
        color: ButtonText;
    }
    .sos-lightbox-btn:focus-visible {
        outline: 3px solid LinkText;
    }
    .sos-lightbox-overlay {
        background: Canvas;
    }
    .sos-gallery__caption {
        color: CanvasText;
    }
}
