/* =====================================================================
   Gallery frontend — cards, the three photo layouts, and the lightbox.

   Shared by the single-gallery template, the gallery archive, the
   gallery-display block and the [sfr_gallery] shortcode, so all four render
   identically. Columns and gap arrive as --sfr-gallery-columns / --sfr-gallery-gap
   set inline by the renderer.
   ===================================================================== */

.sfr-gallery-cards,
.sfr-gallery-grid {
    --sfr-gallery-columns: 3;
    --sfr-gallery-gap: 8px;
    --sfr-gallery-radius: 4px;
}

/* ---------------------------------------------------------------------
   Gallery cards (archive / "all galleries")
   --------------------------------------------------------------------- */

.sfr-gallery-cards {
    display: grid;
    grid-template-columns: repeat(var(--sfr-gallery-columns), minmax(0, 1fr));
    gap: var(--sfr-gallery-gap);
    margin: 0;
    padding: 0;
    list-style: none;
}

.sfr-gallery-card {
    margin: 0;
    background: #fff;
    border-radius: var(--sfr-gallery-radius);
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(30, 58, 138, 0.12);
    transition: transform 0.18s ease, box-shadow 0.18s ease;
}

.sfr-gallery-card:hover,
.sfr-gallery-card:focus-within {
    transform: translateY(-2px);
    box-shadow: 0 6px 18px rgba(30, 58, 138, 0.18);
}

.sfr-gallery-card-link {
    display: block;
    text-decoration: none;
    color: inherit;
}

.sfr-gallery-card-media {
    aspect-ratio: 4 / 3;
    overflow: hidden;
    background: #eef2f9;
}

.sfr-gallery-card-media img {
    width: 100%;
    /* Preserve the card crop when a theme gives content images height:auto. */
    aspect-ratio: 4 / 3;
    height: 100%;
    object-fit: cover;
    display: block;
}

.sfr-gallery-card-placeholder {
    display: block;
    width: 100%;
    height: 100%;
    /* A tinted panel rather than a broken-image icon for a gallery with no
       photos yet. */
    background: linear-gradient(135deg, #1e3a8a, #3b82f6);
    opacity: 0.15;
}

.sfr-gallery-card-info {
    padding: 12px 14px 14px;
}

.sfr-gallery-card > .sfr-gallery-card-link > .sfr-gallery-card-info > .sfr-gallery-card-title {
    margin: 0 0 4px;
    font-size: 1.05rem;
    line-height: 1.3;
    color: #1e3a8a;
}

.sfr-gallery-card > .sfr-gallery-card-link > .sfr-gallery-card-info > .sfr-gallery-card-meta {
    margin: 0;
    font-size: 0.82rem;
    color: #64748b;
    line-height: 1.45;
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

@media (prefers-reduced-motion: reduce) {
    .sfr-gallery-card {
        transition: none;
    }

    .sfr-gallery-card:hover,
    .sfr-gallery-card:focus-within {
        transform: none;
    }
}

@media (max-width: 900px) {
    .sfr-gallery-cards {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 600px) {
    .sfr-gallery-cards {
        grid-template-columns: 1fr;
    }
}

/* ---------------------------------------------------------------------
   Photo layouts
   --------------------------------------------------------------------- */

.sfr-gallery-grid {
    margin: 1.5rem 0;
    padding: 0;
    list-style: none;
}

.sfr-gallery-item {
    margin: 0;
    position: relative;
}

.sfr-gallery-thumb {
    display: block;
    overflow: hidden;
    border-radius: var(--sfr-gallery-radius);
    background: #eef2f9;
    line-height: 0;
}

.sfr-gallery-thumb img,
.sfr-gallery-item > img {
    display: block;
    width: 100%;
    height: auto;
    transition: transform 0.3s ease;
}

.sfr-gallery-thumb:hover img {
    transform: scale(1.04);
}

.sfr-gallery-thumb:focus-visible {
    outline: 3px solid #3b82f6;
    outline-offset: 2px;
}

.sfr-gallery-caption {
    margin: 6px 0 0;
    font-size: 0.82rem;
    line-height: 1.4;
    color: #64748b;
}

@media (prefers-reduced-motion: reduce) {
    .sfr-gallery-thumb img,
    .sfr-gallery-item > img {
        transition: none;
    }

    .sfr-gallery-thumb:hover img {
        transform: none;
    }
}

/* Grid — equal square tiles. */

.sfr-gallery-grid--grid {
    display: grid;
    grid-template-columns: repeat(var(--sfr-gallery-columns), minmax(0, 1fr));
    gap: var(--sfr-gallery-gap);
}

.sfr-gallery-grid--grid .sfr-gallery-thumb {
    aspect-ratio: 1;
}

.sfr-gallery-grid--grid .sfr-gallery-thumb img,
.sfr-gallery-grid--grid .sfr-gallery-item > img {
    /* Keep the image square even when a theme gives content images height:auto. */
    aspect-ratio: 1;
    height: 100%;
    object-fit: cover;
}

/* Masonry — CSS multi-column, so photos keep their shape and the column fills
   vertically. No JavaScript, at the cost of reading order going down each
   column rather than across rows. */

.sfr-gallery-grid--masonry {
    column-count: var(--sfr-gallery-columns);
    column-gap: var(--sfr-gallery-gap);
}

.sfr-gallery-grid--masonry .sfr-gallery-item {
    margin-bottom: var(--sfr-gallery-gap);
    /* Stop a photo being split across a column break. */
    break-inside: avoid;
    -webkit-column-break-inside: avoid;
}

/* Justified rows — each item is given a flex-basis proportional to its aspect
   ratio (--sfr-item-ratio, set inline per photo), so a row of mixed shapes
   lands on a common height. flex-grow then fills the row exactly; the last row
   is left ragged rather than stretched. */

.sfr-gallery-grid--justified {
    display: flex;
    flex-wrap: wrap;
    gap: var(--sfr-gallery-gap);
}

.sfr-gallery-grid--justified .sfr-gallery-item {
    /* 220px is the target row height. */
    flex: calc(var(--sfr-item-ratio, 1) * 220) 1 calc(var(--sfr-item-ratio, 1) * 220px);
    min-width: 120px;
}

.sfr-gallery-grid--justified .sfr-gallery-thumb {
    height: 100%;
}

.sfr-gallery-grid--justified .sfr-gallery-thumb img {
    height: 100%;
    object-fit: cover;
}

/* Narrow screens: no layout survives its author's column count on a phone. */
@media (max-width: 900px) {
    .sfr-gallery-grid--grid {
        grid-template-columns: repeat(min(var(--sfr-gallery-columns), 3), minmax(0, 1fr));
    }

    .sfr-gallery-grid--masonry {
        column-count: min(var(--sfr-gallery-columns), 3);
    }
}

@media (max-width: 600px) {
    .sfr-gallery-grid--grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .sfr-gallery-grid--masonry {
        column-count: 2;
    }

    .sfr-gallery-grid--justified .sfr-gallery-item {
        flex-basis: calc(50% - var(--sfr-gallery-gap));
    }
}

/* ---------------------------------------------------------------------
   Single gallery / empty states
   --------------------------------------------------------------------- */

.sfr-gallery-single-meta {
    margin: 0 0 1rem;
    color: #64748b;
    font-size: 0.9rem;
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.sfr-gallery-empty {
    padding: 2rem;
    text-align: center;
    color: #64748b;
    background: #f8fafc;
    border-radius: var(--sfr-gallery-radius, 4px);
}

/* ---------------------------------------------------------------------
   Lightbox
   --------------------------------------------------------------------- */

/* Locks the page behind the viewer. */
.sfr-lightbox-open {
    overflow: hidden;
}

.sfr-lightbox {
    position: fixed;
    inset: 0;
    z-index: 100000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.sfr-lightbox[hidden] {
    display: none;
}

.sfr-lightbox-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(9, 14, 30, 0.94);
    border: none;
    padding: 0;
    cursor: zoom-out;
}

.sfr-lightbox-bar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    /* Clear of a notch when the phone is held on its side. */
    padding: max(12px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) 12px max(16px, env(safe-area-inset-left, 0px));
    z-index: 2;
}

.sfr-lightbox-counter {
    margin: 0;
    color: rgba(255, 255, 255, 0.75);
    font-size: 0.85rem;
    font-variant-numeric: tabular-nums;
}

.sfr-lightbox-btn {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    color: #fff;
    cursor: pointer;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: background 0.15s ease;
}

.sfr-lightbox-btn:hover {
    background: rgba(255, 255, 255, 0.22);
}

.sfr-lightbox-btn:focus-visible {
    outline: 3px solid #3b82f6;
    outline-offset: 2px;
}

.sfr-lightbox-btn[hidden] {
    display: none;
}

.sfr-lightbox-btn svg {
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.sfr-lightbox-prev,
.sfr-lightbox-next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 2;
}

.sfr-lightbox-prev {
    left: 16px;
}

.sfr-lightbox-next {
    right: 16px;
}

.sfr-lightbox-figure {
    position: relative;
    margin: 0;
    z-index: 1;
    max-width: min(92vw, 1600px);
    /* dvh: on a phone the address bar collapses, and a viewer measured
       against the tall viewport pushes its caption off the screen. */
    max-height: 88vh;
    max-height: 88dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    /* Leave room for the bar and the arrows. */
    padding: 56px 72px 24px;
    pointer-events: none;
}

.sfr-lightbox-stage {
    position: relative;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.sfr-lightbox-img {
    max-width: 100%;
    max-height: 76vh;
    max-height: 76dvh;
    width: auto;
    height: auto;
    display: block;
    border-radius: 3px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    pointer-events: auto;
    opacity: 1;
    transition: opacity 0.2s ease;
}

.sfr-lightbox.is-loading .sfr-lightbox-img {
    opacity: 0.25;
}

.sfr-lightbox-spinner {
    position: absolute;
    width: 34px;
    height: 34px;
    border: 3px solid rgba(255, 255, 255, 0.25);
    border-top-color: #fff;
    border-radius: 50%;
    opacity: 0;
    animation: sfr-lightbox-spin 0.8s linear infinite;
}

.sfr-lightbox.is-loading .sfr-lightbox-spinner {
    opacity: 1;
}

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

.sfr-lightbox-caption {
    margin: 0;
    color: rgba(255, 255, 255, 0.85);
    font-size: 0.9rem;
    line-height: 1.45;
    text-align: center;
    max-width: 60ch;
    pointer-events: auto;
}

.sfr-lightbox-caption[hidden] {
    display: none;
}

@media (prefers-reduced-motion: reduce) {
    .sfr-lightbox-img,
    .sfr-lightbox-btn {
        transition: none;
    }

    .sfr-lightbox-spinner {
        animation: none;
    }
}

/* On a phone the arrows would sit on top of the photo; move them to the bottom
   and give the image the full width. */
@media (max-width: 600px) {
    .sfr-lightbox-figure {
        padding: 52px 12px 76px;
        max-width: 100vw;
    }

    .sfr-lightbox-img {
        max-height: 66vh;
        max-height: 66dvh;
    }

    .sfr-lightbox-prev,
    .sfr-lightbox-next {
        top: auto;
        bottom: max(16px, env(safe-area-inset-bottom, 0px));
        transform: none;
    }

    .sfr-lightbox-prev {
        left: 25%;
    }

    .sfr-lightbox-next {
        right: 25%;
    }
}

/* ---------------------------------------------------------------------------
   Gallery filter and paging
   --------------------------------------------------------------------------- */

.sfr-gallery-filter {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 12px;
    margin: 0 0 20px;
    padding: 16px;
    background: #f6f8fc;
    border: 1px solid #e3e8f2;
    border-radius: 8px;
}

.sfr-gallery-filter-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 0.85rem;
    font-weight: 600;
    color: #1e3a8a;
}

.sfr-gallery-filter-field select,
.sfr-gallery-filter-field input[type="search"] {
    font: inherit;
    font-weight: 400;
    color: #1d2327;
    padding: 7px 10px;
    border: 1px solid #c9d2e3;
    border-radius: 6px;
    background: #fff;
    min-height: 40px;
}

.sfr-gallery-filter-search {
    flex: 1 1 220px;
}

.sfr-gallery-filter-search input[type="search"] {
    width: 100%;
}

.sfr-gallery-filter-submit {
    font: inherit;
    min-height: 40px;
    padding: 8px 18px;
    border: 0;
    border-radius: 6px;
    background: #1e40af;
    color: #fff;
    cursor: pointer;
}

.sfr-gallery-filter-submit:hover,
.sfr-gallery-filter-submit:focus {
    background: #1e3a8a;
}

.sfr-gallery-filter-clear {
    align-self: center;
    font-size: 0.9rem;
    color: #3b82f6;
}

.sfr-gallery-filter-count {
    flex: 1 0 100%;
    margin: 0;
    font-size: 0.85rem;
    color: #5b6472;
}

.sfr-gallery-pagination {
    display: flex;
    justify-content: center;
    gap: 6px;
    flex-wrap: wrap;
    margin-top: 28px;
}

.sfr-gallery-pagination .page-numbers {
    display: inline-block;
    min-width: 40px;
    padding: 8px 12px;
    text-align: center;
    border: 1px solid #dbe2ee;
    border-radius: 6px;
    text-decoration: none;
    color: #1e3a8a;
}

.sfr-gallery-pagination .page-numbers:hover {
    border-color: #1e40af;
    background: #f6f8fc;
}

.sfr-gallery-pagination .page-numbers.current {
    background: #1e40af;
    border-color: #1e40af;
    color: #fff;
}

.sfr-gallery-pagination .page-numbers.dots {
    border-color: transparent;
}

@media (max-width: 600px) {
    .sfr-gallery-filter {
        gap: 10px;
    }

    .sfr-gallery-filter-field {
        flex: 1 1 100%;
    }
}

/* ---------------------------------------------------------------------
   Content images
   Anchors the content-lightbox filter wraps around images in post and page
   content. They are links only as a mechanism, so they carry none of a
   link's usual decoration.
   --------------------------------------------------------------------- */

.sfr-content-lightbox,
.sfr-content-lightbox:hover,
.sfr-content-lightbox:focus {
    display: inline-block;
    max-width: 100%;
    border: 0;
    border-bottom: 0;
    box-shadow: none;
    text-decoration: none;
    cursor: zoom-in;
}

.sfr-content-lightbox img {
    display: block;
    max-width: 100%;
    height: auto;
    transition: opacity 0.2s ease;
}

.sfr-content-lightbox:hover img {
    opacity: 0.92;
}

.sfr-content-lightbox:focus-visible {
    outline: 2px solid #1e40af;
    outline-offset: 3px;
}

@media (prefers-reduced-motion: reduce) {
    .sfr-content-lightbox img {
        transition: none;
    }
}
