/* ==========================================================================
   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 {
    padding: var(--spacing-6); /* structural padding only — visual decoration in components.css */
}

/* 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: var(--size-spinner);
    height: var(--size-spinner);
    border: 3px solid var(--color-white-10);
    border-radius: 50%;
    border-top-color: var(--color-accent-primary);
    animation: spin var(--transition-spin) infinite; /* 2026-04-13: renamed gallery-spin → spin (consolidated to base.css) */
}

/* 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, var(--color-black-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: var(--color-white);
    margin-bottom: var(--spacing-1);
    line-height: var(--leading-tight);
}

.gallery-card__count {
    font-size: var(--text-xs);
    color: var(--color-white-75);
}

/* ==========================================================================
   3b. Model Profile Gallery Grid
   CSS grid layout for the model detail page — replaces masonry flex
   approach used on the index page. Cards are uniform and non-shrinking.

   2026-04-12: created — .gallery-grid (masonry/flex) is not suitable for
   the model profile page because it relies on JS-managed .masonry-column
   children. Without those columns, cards all land in one flex row and
   shrink proportionally. This grid caps at 4 columns and wraps naturally.
   ========================================================================== */

/**
 * Model profile gallery grid — responsive CSS grid, max 4 columns.
 *
 * Uses auto-fill + minmax so cards never shrink below 200px regardless of
 * how many galleries a model has. A max-width on individual cards (via
 * max-width on the grid itself) prevents a single card from spanning the
 * full sidebar-constrained main column width.
 */
.model-galleries-grid {
    display: grid;
    /* 2026-04-12: changed from auto-fill/minmax to explicit 4 columns —
       auto-fill dropped to 3 columns because sidebar reduced available width
       below the 200px threshold for a 4th column. Explicit repeat(4, 1fr)
       matches the /models index page and is predictable regardless of sidebar. */
    grid-template-columns: repeat(4, 1fr);
    gap: var(--spacing-4);
    margin-bottom: var(--spacing-8);
}

/* Mobile: 2 columns, matching main gallery browse page behaviour */
@media (max-width: 768px) {
    .model-galleries-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: var(--spacing-3);
    }
}

/**
 * Scoped overrides for gallery cards on the model profile page.
 *
 * 2026-04-12: restructured to support a persistent below-image label.
 * .gallery-card__thumb wraps the image and hover overlay so overflow:hidden
 * is scoped there — the card itself is no longer overflow:hidden, which lets
 * .gallery-card__label render below without being clipped.
 * Background + shadow now live on .gallery-card to give the full card
 * (image + label) the same floating-card treatment as .model-card.
 */

/* 2026-04-12: min-width:0 — critical grid fix. <img> elements with HTML width/height
   attributes impose their intrinsic width as the min-content size of the column. Without
   min-width:0, CSS grid cannot shrink 1fr columns below that min-content, making columns
   unequal (e.g. 174px vs 190px in a 258px container). min-width:0 allows tracks to shrink
   to zero so 1fr distributes the available space equally regardless of image intrinsic dims. */
.model-galleries-grid .gallery-card {
    min-width: 0;
    overflow: visible;
    background: var(--color-bg-surface);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-md);
    transition: transform var(--transition-base), box-shadow var(--transition-base);
}

.model-galleries-grid .gallery-card:hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow-lg);
}

.model-galleries-grid .gallery-card__link {
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* Image container — overflow:hidden scoped here so the label below is not clipped.
   2026-04-12: switched from aspect-ratio to the padding-top intrinsic-ratio trick.
   aspect-ratio on a flex item inside a column flex + CSS grid causes the browser to
   resolve the height constraint circularly — row height depends on cell content, cell
   content depends on the flex item's aspect-ratio, which depends on the resolved width,
   which depends on the grid column (definite), but the browser sometimes falls back to
   the image's intrinsic height when the chain breaks. Result: page-2 cards render at a
   different height than page-1/3 because the image set on page-2 has different intrinsic
   dimensions that resolve the circularity differently.
   padding-top: 133.33% (= 4/3 × 100%) is always resolved against the containing block's
   WIDTH (never the height), making it completely immune to image content and layout
   algorithm differences. */
.model-galleries-grid .gallery-card__thumb {
    position: relative;
    overflow: hidden;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    width: 100%;
    height: 0;
    padding-top: var(--aspect-ratio-3-4);
    flex-shrink: 0;
}

/* 2026-04-12: height:100% must be set explicitly here to override height:auto from the
   global .gallery-card__image rule.
   2026-04-12: aspect-ratio:unset — HTML width/height attributes on <img> cause the browser
   UA stylesheet to inject aspect-ratio:attr(width)/attr(height). This intrinsic aspect-ratio
   fights height:100% inside a height:0/padding-top container, causing per-gallery height
   variation based on each image's natural dimensions. aspect-ratio:unset disables this so
   height:100% resolves cleanly against the container's padding-box height. */
.model-galleries-grid .gallery-card__image {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    aspect-ratio: unset;
    object-fit: cover;
    object-position: center top;
    display: block;
    border-radius: 0;
    transition: transform var(--transition-base);
}

.model-galleries-grid .gallery-card:hover .gallery-card__image {
    transform: scale(1.04);
}

/* Persistent below-image label — always visible, mirrors .model-card__info */
.model-galleries-grid .gallery-card__label {
    padding: var(--spacing-3) var(--spacing-3) var(--spacing-4);
    display: flex;
    flex-direction: column;
    gap: var(--spacing-1);
}

.model-galleries-grid .gallery-card__label-title {
    font-size: var(--text-sm);
    font-weight: var(--font-semibold);
    color: var(--color-text-primary);
    line-height: var(--leading-snug);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.model-galleries-grid .gallery-card__label-count {
    font-size: var(--text-xs);
    color: var(--color-text-muted);
}

/* ==========================================================================
   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);
    }
}