/* Frontend gallery — justified tiled layout */
.tpg-gallery {
    --tpg-gap: 8px;
    --tpg-tile-height: clamp(140px, 22vw, 240px);
    --tpg-radius: 0px;
}

.tpg-row {
    display: flex;
    gap: var(--tpg-gap);
    margin-bottom: var(--tpg-gap);
}

.tpg-item {
    display: block;
    flex: 1 1 0;          /* width share proportional to inline flex-grow */
    min-width: 0;
    overflow: hidden;
    border-radius: var(--tpg-radius);
}

/*
 * Fixed height for every image in a row. Widths are handled by flexbox,
 * so each full row fills the container exactly; object-fit crops the
 * image to fill its slot without distortion.
 * --tpg-tile-height defaults to a responsive clamp() but can be set to a
 * fixed px value via the block's "Tile height" control.
 */
.tpg-item img {
    display: block;
    width: 100%;
    height: var(--tpg-tile-height);
    object-fit: cover;
    transition: transform .3s ease;
}

/* Hover zoom only where hover really exists — on touch screens it would
   otherwise "stick" on the last-tapped tile. */
@media (hover: hover) {
    .tpg-item:hover img { transform: scale(1.05); }
}

/* Touch: no grey tap flash, and no double-tap-zoom delay on tiles. */
.tpg-item {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

/* Short final rows: fixed-width leftovers instead of stretched giants */
.tpg-row--short .tpg-item {
    flex-grow: 0;
    width: var(--tpg-tile-height);
    max-width: 40%;
}

/*
 * Mosaic layout
 *
 * An irregular grid instead of equal-height rows. Each tile's row span is
 * computed server-side from the image's real aspect ratio (and wide images
 * may span two columns), so tiles genuinely differ in height and width.
 *
 * grid-auto-rows is one "unit": a column's width is treated as --tpg-units
 * units tall, so a square tile ends up roughly --tpg-tile-height tall and
 * everything else scales from its true proportions.
 *
 * auto-flow: dense backfills the holes those varied spans leave behind.
 */
.tpg-gallery--mosaic {
    display: grid;
    grid-template-columns: repeat(var(--tpg-cols, 3), 1fr);
    grid-auto-rows: calc(var(--tpg-tile-height) / var(--tpg-units, 12));
    grid-auto-flow: dense;
    gap: var(--tpg-gap);
}

/* Tiles fill their computed grid area rather than a fixed row height. */
.tpg-gallery--mosaic .tpg-item {
    flex: none;
    width: auto;
    height: 100%;
    min-width: 0;
}

.tpg-gallery--mosaic .tpg-item img {
    width: 100%;
    height: 100%;
}

/*
 * On narrow screens collapse to two columns and stop double-width feature
 * tiles from spanning the entire row.
 */
@media (max-width: 600px) {
    .tpg-gallery--mosaic {
        grid-template-columns: repeat(2, 1fr);
    }
}
@media (max-width: 380px) {
    .tpg-gallery--mosaic {
        grid-template-columns: 1fr;
    }
    .tpg-gallery--mosaic .tpg-item {
        grid-column: span 1 !important;
    }
}

/* Lightbox */
.tpg-lightbox {
    position: fixed;
    inset: 0;
    z-index: 99999;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, .9);
    /* The script handles swipe, pinch and pan itself; stop the browser
       from scrolling or zooming the page underneath. */
    touch-action: none;
    overscroll-behavior: contain;
    -webkit-tap-highlight-color: transparent;
    -webkit-user-select: none;
    user-select: none;
}
.tpg-lightbox.is-open { display: flex; }
.tpg-lb-img {
    max-width: 90vw;
    max-height: 78vh;
    max-height: 78dvh;
    object-fit: contain;
    transform-origin: center center;
    will-change: transform;
    -webkit-touch-callout: none;
    -webkit-user-drag: none;
}
.tpg-lb-img.is-animating {
    transition: transform .2s ease, opacity .2s ease;
}
.tpg-lb-close,
.tpg-lb-prev,
.tpg-lb-next {
    position: absolute;
    border: 0;
    background: none;
    color: #fff;
    font-size: 2rem;
    cursor: pointer;
    padding: 1rem;
}
.tpg-lb-close { top: 0; right: 0; }
.tpg-lb-prev { left: 0; top: 50%; transform: translateY(-50%); }
.tpg-lb-next { right: 0; top: 50%; transform: translateY(-50%); }

.tpg-lb-footer {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1.5rem;
    padding: .75rem 1rem;
    padding-bottom: calc(.75rem + env(safe-area-inset-bottom, 0px));
    color: #fff;
    font-size: .9rem;
}

/* Touch devices: bigger tap targets, and swiping replaces the arrows, so
   keep them but make them subtler so they don't cover the photo. */
@media (pointer: coarse) {
    .tpg-lb-close { padding: 1.1rem 1.25rem; }
    .tpg-lb-prev,
    .tpg-lb-next {
        padding: 1.5rem 1rem;
        opacity: .6;
    }
    .tpg-lb-download { padding: .5rem 0; }
}
.tpg-lb-count {
    opacity: .8;
}
.tpg-lb-download {
    color: #fff;
    text-decoration: underline;
}
.tpg-lb-download:hover,
.tpg-lb-download:focus {
    opacity: .8;
}
.tpg-lb-close:focus-visible,
.tpg-lb-prev:focus-visible,
.tpg-lb-next:focus-visible,
.tpg-lb-download:focus-visible {
    outline: 2px solid #fff;
    outline-offset: 2px;
}

/* Announced to screen readers only; not shown visually (the visible
   counter above serves sighted users). */
.tpg-lb-status {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}
