/* ==========================================================================
   Frontend Layout — Gallery Page Structure
   Layer 3 — requires tokens.css, base.css
   /css/frontend/layout.css

   Structural rules only: gallery layout, responsive grid,
   container sizing, and the main content/sidebar layout.
   All component styles live in components.css (Layer 4).

   Sections:
    1.  Container & Layout Shell
    2.  Gallery Main Layout
    3.  Gallery Grid (Index Page)
    4.  Responsive Breakpoints
   ========================================================================== */

/* ==========================================================================
   1. Container & Layout Shell
   ========================================================================== */

.container {
    max-width: var(--width-container);
    margin: 0 auto;
    padding: 0 var(--spacing-4);
    width: 100%;
}

.gallery-container {
    max-width: var(--width-container);
    margin: 0 auto;
    padding: 0 var(--spacing-4);
    width: 100%;
}

.gallery-layout {
    display: flex;
    gap: var(--spacing-8);
    min-height: 100vh;
}

.gallery-main {
    flex: 1;
    min-width: 0;
}

.gallery-sidebar {
    width: var(--width-sidebar);
    flex-shrink: 0;
}

/* ==========================================================================
   2. Gallery Main Layout
   ========================================================================== */

.gallery-content {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-6);
}

.gallery-info-panel {
    background: var(--color-bg-surface); /* background color */
    border-radius: var(--radius-xl); /* rounded corners  */
    padding: var(--spacing-6); /* padding */
}

/* Masonry thumbnail grid — CSS columns approach */
/* 2026-04-01: changed from grid to columns for natural aspect ratio masonry */
.gallery-thumbnails {
    columns: 3;
    column-gap: var(--spacing-3);
}

.thumbnail-item {
    break-inside: avoid;
    margin-bottom: var(--spacing-3);
    width: 100%;
    cursor: pointer;
    overflow: hidden;
    border-radius: var(--radius-xl);
}

.thumbnail-image {
    width: 100%;
    height: auto;
    display: block;
    transition: opacity var(--transition-fast);
}

/* ==========================================================================
   3. Gallery Grid (Index Page) — explicit flex masonry columns
   ========================================================================== */

/* 2026-04-07: spacers for DOM recycling scroll restoration (kept for legacy safety) */
.grid-spacer-top,
.grid-spacer-bottom {
    width: 100%;
    pointer-events: none;
    clear: both;
}

/* 2026-04-07: Loading spinner for infinite scroll */
.gallery-loader {
    display: flex;
    justify-content: center;
    padding: var(--spacing-8) 0;
    width: 100%;
}

.gallery-loader[hidden] {
    display: none;
}

.gallery-loader__spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    border-top-color: var(--color-accent-primary);
    animation: gallery-spin 0.8s linear infinite;
}

@keyframes gallery-spin {
    to { transform: rotate(360deg); }
}

/* 2026-04-11: changed from CSS columns to explicit flex masonry columns.
   Reason: CSS columns reflows ALL items whenever any image loads or a new
   batch is appended — the browser recalculates the column split point and
   redistributes items, causing visible card-jumping on mobile (right column
   rearranges continuously as images load). Explicit JS-placed flex columns
   are stable: cards are appended once and never move. */
.gallery-grid {
    display: flex;
    gap: var(--spacing-4);
    align-items: flex-start;
    margin-bottom: var(--spacing-8);
}

/* Each column grows equally; JS manages how many exist (2 / 3 / 4). */
.masonry-column {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-4);
}

.gallery-card {
    /* 2026-04-11: removed break-inside: avoid (no longer in CSS columns context)
       removed content-visibility: auto + contain-intrinsic-size — was causing
       iOS Safari to defer rendering of cards it deemed off-screen, resulting
       in an empty grid on initial iPhone page load. */
    position: relative;
    overflow: hidden;
    border-radius: var(--radius-lg);
    cursor: pointer;
}

.gallery-card__link {
    display: block;
    text-decoration: none;
    color: inherit;
}

.gallery-card__image {
    width: 100%;
    height: auto;
    display: block;
    border-radius: var(--radius-lg);
    /* 2026-04-11: removed opacity fade-in (was 0→1 on is-loaded).
       Images have explicit width/height so no layout shift occurs.
       Fading caused constant flicker when DOM recycling rebuilt cards. */
    transition: transform var(--transition-base);
    background: var(--color-bg-elevated);
}

.gallery-card:hover .gallery-card__image {
    transform: scale(1.03);
}

/* Overlay with title/count at bottom of image */
.gallery-card__content {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: var(--spacing-6) var(--spacing-3) var(--spacing-3);
    background: linear-gradient(to top, rgba(0, 0, 0, 0.75) 0%, transparent 100%);
    border-radius: 0 0 var(--radius-lg) var(--radius-lg);
    opacity: 0;
    transition: opacity var(--transition-base);
}

.gallery-card:hover .gallery-card__content {
    opacity: 1;
}

.gallery-card__title {
    font-size: var(--text-sm);
    font-weight: var(--font-semibold);
    color: white;
    margin-bottom: var(--spacing-1);
    line-height: var(--leading-tight);
}

.gallery-card__count {
    font-size: var(--text-xs);
    color: rgba(255, 255, 255, 0.75);
}

/* ==========================================================================
   4. Responsive Breakpoints
   ========================================================================== */

/* Tablet */
@media (max-width: 1024px) {
    /* 2026-04-11: removed gallery-grid columns: 3 — column count now managed by JS */
    .gallery-layout {
        flex-direction: column;
    }

    .gallery-sidebar {
        width: 100%;
        order: 2;
    }

    .gallery-main {
        order: 1;
    }
}

/* Mobile */
@media (max-width: 768px) {
    /* 2026-04-11: removed gallery-grid columns: 2 — column count now managed by JS */
    .gallery-grid {
        gap: var(--spacing-3);
    }

    .masonry-column {
        gap: var(--spacing-3);
    }

    .gallery-container {
        padding: 0 var(--spacing-3);
    }

    .gallery-info-panel {
        padding: var(--spacing-4);
    }

    .gallery-thumbnails {
        columns: 2;
    }

    .gallery-layout {
        gap: var(--spacing-6);
    }

    .gallery-content {
        gap: var(--spacing-4);
    }
}

/* Small mobile */
@media (max-width: 480px) {
    .gallery-info-panel {
        padding: var(--spacing-3);
    }

    .gallery-container {
        padding: 0 var(--spacing-2);
    }
}