/* =============================================================================
   Gallery shortcode — irregular editorial grid
   ============================================================================= */
.gallery {
    margin: clamp(2rem, 4vw, 3.5rem) 0;
    padding: 0;
}

/* Full-bleed break-out: extends the gallery beyond the article column,
   echoing the existing .images-full-width pattern but with a soft inset. */
.gallery--bleed {
    width: min(100vw, 1400px);
    margin-inline: auto;
    position: relative;
    left: 50%;
    transform: translateX(-50%);
    padding-inline: clamp(1rem, 4vw, 2.5rem);
}

/* The grid itself: 12-col base, dense packing so spans pack neatly. */
.gallery-grid {
    display: grid;
    grid-template-columns: repeat(12, 1fr);
    grid-auto-rows: clamp(80px, 11vw, 140px);
    grid-auto-flow: dense;
    gap: clamp(0.5rem, 1vw, 0.85rem);
}

/* Each tile: editorial frame with grayscale → color reveal on hover. */
.gallery-tile {
    position: relative;
    display: block;
    overflow: hidden;
    border: 1px solid var(--border-color);
    border-radius: 2px;
    background-color: var(--bg-secondary);
    transform: translateY(0);
    transition:
        border-color 0.3s ease,
        transform 0.4s cubic-bezier(0.2, 0.7, 0.2, 1),
        box-shadow 0.4s ease;
    /* Default span — overridden by nth-child rules below. */
    grid-column: span 4;
    grid-row: span 2;
}

.gallery-tile:focus-visible {
    outline: 2px solid var(--accent-warm);
    outline-offset: 3px;
}

.gallery-tile:hover {
    border-color: var(--text-secondary);
    transform: translateY(-2px);
    box-shadow: 0 14px 40px -22px rgba(0, 0, 0, 0.55);
}

.gallery-image {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    filter: grayscale(55%) contrast(1.03) brightness(0.97);
    transform: scale(1.005);
    transition:
        filter 0.5s ease,
        transform 0.7s cubic-bezier(0.2, 0.7, 0.2, 1);
}

.gallery-tile:hover .gallery-image {
    filter: grayscale(0%) contrast(1) brightness(1);
    transform: scale(1.04);
}

/* Soft inner border + corner accent. Pure presentation, no JS. */
.gallery-glow {
    position: absolute;
    inset: 0;
    pointer-events: none;
    background:
        linear-gradient(
            to bottom,
            transparent 55%,
            rgba(0, 0, 0, 0.32) 100%
        );
    opacity: 0;
    transition: opacity 0.4s ease;
}

.gallery-tile:hover .gallery-glow {
    opacity: 1;
}

/* Editorial index number — small, mono, top-left corner. */
.gallery-index {
    position: absolute;
    top: clamp(0.55rem, 1vw, 0.85rem);
    left: clamp(0.55rem, 1vw, 0.85rem);
    z-index: 2;
    padding: 0.18rem 0.45rem;
    font-family: var(--font-family-mono);
    font-size: var(--font-size-xs);
    letter-spacing: var(--tracking-wider);
    color: rgba(255, 255, 255, 0.78);
    background: rgba(0, 0, 0, 0.32);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 2px;
    font-variant-numeric: tabular-nums;
    opacity: 0;
    transform: translateY(-4px);
    transition:
        opacity 0.3s ease,
        transform 0.3s ease;
}

.gallery-tile:hover .gallery-index {
    opacity: 1;
    transform: translateY(0);
}

/* ---- Irregular rhythm: 5-tile cycle on a 12-col grid -----------------------
   Pattern (1 cycle = 5 tiles, fills a 12-col strip of ~3 rows):
     #1  hero       — 7 cols × 2 rows
     #2  upper sub  — 5 cols × 1 row
     #3  lower sub  — 5 cols × 1 row
     #4  half       — 6 cols × 1 row
     #5  half       — 6 cols × 1 row
   `grid-auto-flow: dense` smooths any partial tail. */
.gallery-tile:nth-child(5n + 1) {
    grid-column: span 7;
    grid-row: span 2;
}

.gallery-tile:nth-child(5n + 2),
.gallery-tile:nth-child(5n + 3) {
    grid-column: span 5;
    grid-row: span 1;
}

.gallery-tile:nth-child(5n + 4),
.gallery-tile:nth-child(5n + 5) {
    grid-column: span 6;
    grid-row: span 1;
}

/* Single-image edge case: let it span full width with a calmer aspect. */
.gallery[data-count="1"] .gallery-tile {
    grid-column: span 12;
    grid-row: span 3;
}

/* Two-image edge case: equal halves. */
.gallery[data-count="2"] .gallery-tile {
    grid-column: span 6;
    grid-row: span 2;
}

/* Three-image edge case: hero + two stacked. */
.gallery[data-count="3"] .gallery-tile:nth-child(1) {
    grid-column: span 8;
    grid-row: span 2;
}
.gallery[data-count="3"] .gallery-tile:nth-child(2),
.gallery[data-count="3"] .gallery-tile:nth-child(3) {
    grid-column: span 4;
    grid-row: span 1;
}

/* Tablet — collapse to a 6-col grid with a softer pattern. */
@media (max-width: 900px) {
    .gallery-grid {
        grid-template-columns: repeat(6, 1fr);
        grid-auto-rows: clamp(100px, 18vw, 170px);
    }

    .gallery-tile:nth-child(5n + 1) {
        grid-column: span 6;
        grid-row: span 2;
    }
    .gallery-tile:nth-child(5n + 2),
    .gallery-tile:nth-child(5n + 3) {
        grid-column: span 3;
        grid-row: span 1;
    }
    .gallery-tile:nth-child(5n + 4) {
        grid-column: span 4;
        grid-row: span 1;
    }
    .gallery-tile:nth-child(5n + 5) {
        grid-column: span 2;
        grid-row: span 1;
    }

    .gallery[data-count="1"] .gallery-tile,
    .gallery[data-count="2"] .gallery-tile,
    .gallery[data-count="3"] .gallery-tile {
        grid-column: span 6;
        grid-row: span 2;
    }
}

/* Mobile — single column, varying heights to keep some rhythm. */
@media (max-width: 560px) {
    .gallery--bleed {
        width: 100%;
        left: auto;
        transform: none;
        padding-inline: 0;
    }

    .gallery-grid {
        grid-template-columns: 1fr;
        grid-auto-rows: 220px;
        gap: 0.55rem;
    }

    .gallery-tile,
    .gallery-tile:nth-child(5n + 1),
    .gallery-tile:nth-child(5n + 2),
    .gallery-tile:nth-child(5n + 3),
    .gallery-tile:nth-child(5n + 4),
    .gallery-tile:nth-child(5n + 5),
    .gallery[data-count="1"] .gallery-tile,
    .gallery[data-count="2"] .gallery-tile,
    .gallery[data-count="3"] .gallery-tile {
        grid-column: span 1;
        grid-row: span 1;
    }

    .gallery-tile:nth-child(3n + 1) {
        grid-row: span 2;
        height: auto;
    }
}

/* Caption — minimal editorial footer matching site meta style. */
.gallery-caption {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    margin-top: var(--spacing-md);
    padding: 0 0.25rem;
    font-family: var(--font-family-mono);
    font-size: var(--font-size-xs);
    letter-spacing: var(--tracking-wider);
    text-transform: uppercase;
    color: var(--text-muted);
}

.gallery-caption-rule {
    flex: 0 0 1.75rem;
    height: 1px;
    background: var(--text-muted);
}

.gallery-caption-text {
    color: var(--text-secondary);
    flex: 1;
    min-width: 0;
}

.gallery-caption-count {
    color: var(--text-faint);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

/* Reset post-body img defaults inside the gallery so our cover layout wins. */
.post-content .post-body .gallery img,
.post-content .post-body .gallery-image {
    display: block;
    margin: 0;
    border: none;
    border-radius: 0;
    max-width: none;
}

/* Respect reduced-motion preferences. */
@media (prefers-reduced-motion: reduce) {
    .gallery-tile,
    .gallery-image,
    .gallery-glow,
    .gallery-index {
        transition: none;
    }
    .gallery-tile:hover {
        transform: none;
    }
    .gallery-tile:hover .gallery-image {
        transform: none;
    }
}
