/* Clear pH Masonry Gallery Frontend Styles */

/* Category Filters */
.clearph-gallery-filters {
    display: flex;
    flex-wrap: wrap;
    gap: 40px;
    margin-bottom: 40px;
    padding: 0;
    background: transparent;
}

.clearph-gallery-filters--center {
    justify-content: center;
}

.clearph-gallery-filters--right {
    justify-content: flex-end;
}

.clearph-gallery-filters .filter-btn {
    padding: 10px 0;
    border: none;
    background: transparent;
    color: #999;
    font-size: 16px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    cursor: pointer;
    border-bottom: 3px solid transparent;
    transition: all 0.3s ease;
}

.clearph-gallery-filters .filter-btn:hover {
    color: #333;
}

.clearph-gallery-filters .filter-btn.active {
    color: #333;
    border-bottom-color: #333;
}

.clearph-gallery {
    margin: 20px 0;
    display: grid;
    gap: var(--column-margin, 20px);
    grid-auto-flow: row dense;
    /* Optimize for smooth scrolling */
    contain: layout style;
}

/* Dynamic grid columns - Micro-column system for fractional width support
   Each visual column = 2 micro-columns, allowing 0.5 column increments
   Example: 3-column gallery = 6 micro-columns
            - 1 visual col = span 2 micro-cols (33.33%)
            - 1.5 visual cols = span 3 micro-cols (50%)
            - 2 visual cols = span 4 micro-cols (66.66%)
*/
.clearph-gallery[data-columns="2"] {
    grid-template-columns: repeat(4, 1fr);
    /* 2 visual cols = 4 micro-cols */
}

.clearph-gallery[data-columns="3"] {
    grid-template-columns: repeat(6, 1fr);
    /* 3 visual cols = 6 micro-cols */
}

.clearph-gallery[data-columns="4"] {
    grid-template-columns: repeat(8, 1fr);
    /* 4 visual cols = 8 micro-cols */
}

.clearph-gallery[data-columns="5"] {
    grid-template-columns: repeat(10, 1fr);
    /* 5 visual cols = 10 micro-cols */
}

.clearph-gallery[data-columns="6"] {
    grid-template-columns: repeat(12, 1fr);
    /* 6 visual cols = 12 micro-cols */
}

/* Equal height rows when masonry is disabled */
.clearph-gallery:not(.masonry-enabled) {
    grid-auto-rows: 300px;
}

/* Auto-sizing rows for masonry - 175px base provides better content preservation */
.clearph-gallery.masonry-enabled {
    grid-auto-rows: 175px;
}

.clearph-gallery .gallery-item {
    position: relative;
    overflow: hidden;
    border-radius: var(--border-radius, 0);
    transition: transform 0.3s ease;
    will-change: transform;
    transform: translateZ(0);
}

.clearph-gallery .gallery-item img {
    width: 100%;
    height: 100%;
    display: block;
    transition: transform 0.4s ease, opacity 0.3s ease;
    /* Slower transition for smoother effect */
    object-fit: cover;
    /* Default fallback */
    /* Optimize for smooth scaling */
    will-change: transform;
    transform: translateZ(0);
}


/* Force object-fit based on data attribute */
.clearph-gallery[data-object-fit="cover"] .gallery-item img {
    object-fit: cover;
}

.clearph-gallery[data-object-fit="contain"] .gallery-item img {
    object-fit: contain;
}

.clearph-gallery[data-object-fit="fill"] .gallery-item img {
    object-fit: fill;
}

/* Masonry size classes - updated for micro-column system
   NOTE: These are for backward compatibility with old galleries.
   New galleries use inline styles with data-column-span and data-row-span attributes.
*/

/* Regular: 1 visual column = 2 micro-columns */
.clearph-gallery.masonry-enabled .gallery-item.size-regular {
    grid-row: span 2;
    grid-column: span 2;
    /* 1 visual column */
}

/* Wide: 2 visual columns = 4 micro-columns */
.clearph-gallery.masonry-enabled .gallery-item.size-wide {
    grid-column: span 4;
    /* 2 visual columns */
    grid-row: span 2;
}

/* Tall: 1 visual column, 4 rows */
.clearph-gallery.masonry-enabled .gallery-item.size-tall {
    grid-column: span 2;
    /* 1 visual column */
    grid-row: span 4;
}

/* Large: 2 visual columns, 4 rows */
.clearph-gallery.masonry-enabled .gallery-item.size-large {
    grid-column: span 4;
    /* 2 visual columns */
    grid-row: span 4;
}

/* XL (Extra Large) - spans all columns and 6 rows */
.clearph-gallery.masonry-enabled .gallery-item.size-xl {
    grid-row: span 6;
}

/* XL column spans - full width in micro-columns */
.clearph-gallery.masonry-enabled[data-columns="2"] .gallery-item.size-xl {
    grid-column: span 4;
    /* 2 visual cols = 4 micro-cols */
}

.clearph-gallery.masonry-enabled[data-columns="3"] .gallery-item.size-xl {
    grid-column: span 6;
    /* 3 visual cols = 6 micro-cols */
}

.clearph-gallery.masonry-enabled[data-columns="4"] .gallery-item.size-xl {
    grid-column: span 8;
    /* 4 visual cols = 8 micro-cols */
}

.clearph-gallery.masonry-enabled[data-columns="5"] .gallery-item.size-xl {
    grid-column: span 10;
    /* 5 visual cols = 10 micro-cols */
}

.clearph-gallery.masonry-enabled[data-columns="6"] .gallery-item.size-xl {
    grid-column: span 12;
    /* 6 visual cols = 12 micro-cols */
}

/* When masonry is disabled, all items are regular (1 visual column = 2 micro-columns) */
.clearph-gallery:not(.masonry-enabled) .gallery-item {
    grid-row: span 1;
    grid-column: span 2;
    /* 1 visual column */
}

/* Lightbox styling */
.clearph-gallery .gallery-item.lightbox-clickable {
    cursor: pointer;
}

.clearph-gallery .gallery-item:hover img {
    transform: scale(1.1) translateZ(0);
}

/* Fallback animations when GSAP isn't available */
.clearph-gallery .gallery-item.animate-in {
    opacity: 1;
    transform: translateY(0) scale(1) translateZ(0);
    transition: opacity 0.8s ease, transform 0.8s ease;
}

/* Smooth scrolling optimization */
@media (prefers-reduced-motion: no-preference) {
    .clearph-gallery {
        scroll-behavior: smooth;
    }
}

/* Reduce animations for users who prefer reduced motion */
@media (prefers-reduced-motion: reduce) {
    .clearph-gallery .gallery-item {
        transition: none;
        will-change: auto;
    }

    .clearph-gallery .gallery-item img {
        transition: none;
        will-change: auto;
    }
}

/* Responsive breakpoints - micro-column system */
@media (max-width: 1024px) {

    /* 6 cols → 4 cols, 5 cols → 3 cols */
    .clearph-gallery[data-columns="6"] {
        grid-template-columns: repeat(8, 1fr);
        /* 4 visual cols = 8 micro-cols */
    }

    .clearph-gallery[data-columns="5"] {
        grid-template-columns: repeat(6, 1fr);
        /* 3 visual cols = 6 micro-cols */
    }

    /* Wide and Large collapse to 1 visual column */
    .clearph-gallery.masonry-enabled .gallery-item.size-wide,
    .clearph-gallery.masonry-enabled .gallery-item.size-large {
        grid-column: span 2;
        /* 1 visual column */
    }

    /* XL adjusts to new grid widths */
    .clearph-gallery.masonry-enabled[data-columns="6"] .gallery-item.size-xl {
        grid-column: span 8;
        /* 4 visual cols = 8 micro-cols */
    }

    .clearph-gallery.masonry-enabled[data-columns="5"] .gallery-item.size-xl {
        grid-column: span 6;
        /* 3 visual cols = 6 micro-cols */
    }
}

@media (max-width: 768px) {
    .clearph-gallery {
        gap: calc(var(--column-margin, 20px) * 0.75);
        grid-auto-rows: 220px;
    }

    .clearph-gallery.masonry-enabled {
        grid-auto-rows: 160px;
    }

    /* All galleries collapse to 2 visual columns = 4 micro-columns */
    .clearph-gallery[data-columns="6"],
    .clearph-gallery[data-columns="5"],
    .clearph-gallery[data-columns="4"],
    .clearph-gallery[data-columns="3"],
    .clearph-gallery[data-columns="2"] {
        grid-template-columns: repeat(4, 1fr);
        /* 2 visual cols = 4 micro-cols */
    }

    .clearph-gallery.masonry-enabled .gallery-item.size-regular {
        grid-row: span 2;
    }

    .clearph-gallery.masonry-enabled .gallery-item.size-tall,
    .clearph-gallery.masonry-enabled .gallery-item.size-large {
        grid-row: span 3;
        /* Reduced on mobile */
    }

    .clearph-gallery.masonry-enabled .gallery-item.size-xl {
        grid-row: span 4;
        /* Reduced on mobile */
    }

    /* Wide and Large collapse to 1 visual column on tablet */
    .clearph-gallery.masonry-enabled .gallery-item.size-wide,
    .clearph-gallery.masonry-enabled .gallery-item.size-large {
        grid-column: span 2;
        /* 1 visual column */
    }

    /* XL spans full width (2 visual cols = 4 micro-cols) on tablet */
    .clearph-gallery.masonry-enabled .gallery-item.size-xl {
        grid-column: span 4;
        /* 2 visual cols = 4 micro-cols */
    }

    /* Reduce animations on mobile for better performance */
    .clearph-gallery .gallery-item {
        will-change: auto;
    }

    .clearph-gallery .gallery-item img {
        will-change: auto;
        transition: none;
    }
}

@media (max-width: 480px) {
    .clearph-gallery {
        gap: calc(var(--column-margin, 20px) * 0.5);
        /* 1 visual column = 2 micro-columns for consistency */
        grid-template-columns: repeat(2, 1fr) !important;
        grid-auto-rows: 280px;
    }

    .clearph-gallery.masonry-enabled {
        grid-auto-rows: 220px;
    }

    /* All items span full width (2 micro-cols) and single row on mobile */
    .clearph-gallery .gallery-item {
        grid-column: span 2 !important;
        /* Full width = 2 micro-cols */
        grid-row: span 1 !important;
    }

}

/* Video items */
.clearph-gallery .gallery-item--video {
    cursor: pointer;
}

.clearph-gallery .gallery-item--video video {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover;
}

.clearph-gallery .gallery-video-badge {
    position: absolute;
    bottom: 12px;
    left: 12px;
    width: 36px;
    height: 36px;
    background: rgba(0, 0, 0, 0.6);
    color: #fff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    pointer-events: none;
    transition: opacity 0.3s ease;
    z-index: 2;
    padding-left: 2px;
}

.clearph-gallery .gallery-item--video:hover .gallery-video-badge {
    opacity: 0;
}

/* Video items should not get the image hover scale */
.clearph-gallery .gallery-item--video:hover video {
    transform: none;
}

/* YouTube items */
.clearph-gallery .gallery-item--youtube {
    cursor: pointer;
}

.clearph-gallery .gallery-item--youtube img {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover;
}

.clearph-gallery .gallery-youtube-badge {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.3s ease;
    z-index: 2;
    opacity: 0.9;
}

.clearph-gallery .gallery-item--youtube:hover .gallery-youtube-badge {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1.1);
}

/* YouTube items get the same hover zoom as images */
.clearph-gallery .gallery-item--youtube:hover img {
    transform: scale(1.1) translateZ(0);
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    .clearph-gallery .gallery-item.lightbox-clickable:hover::after {
        background: rgba(255, 255, 255, 0.1);
    }
}

/* ===== Image Labels ===== */
.clearph-gallery-label {
    position: absolute;
    margin: 0 !important;
    pointer-events: none;
    z-index: 2;
    line-height: 1.3;
}

/* Hidden by default — require data-label-show or data-label-hover to display */
.clearph-gallery .clearph-gallery-label {
    display: none;
}

/* Show always */
.clearph-gallery[data-label-show="1"] .clearph-gallery-label {
    display: block;
}

/* Show on hover only */
.clearph-gallery[data-label-hover="1"] .clearph-gallery-label {
    display: block;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.clearph-gallery[data-label-hover="1"] .gallery-item:hover .clearph-gallery-label {
    opacity: 1;
}

/* If both show + hover are set, always visible (show wins) */
.clearph-gallery[data-label-show="1"][data-label-hover="1"] .clearph-gallery-label {
    opacity: 1;
    transition: none;
}

/* Placement — scoped under .clearph-gallery for specificity over extra theme classes */
.clearph-gallery .clearph-gallery-label--bottom-left   { bottom: 40px; left: 20px; right: auto; top: auto; text-align: left; transform: none; }
.clearph-gallery .clearph-gallery-label--bottom-center { bottom: 40px; left: 20px; right: 20px; top: auto; text-align: center; transform: none; }
.clearph-gallery .clearph-gallery-label--bottom-right  { bottom: 40px; right: 20px; left: auto; top: auto; text-align: right; transform: none; }

.clearph-gallery .clearph-gallery-label--middle-left   { top: 50%; left: 20px; right: auto; bottom: auto; transform: translateY(-50%); text-align: left; }
.clearph-gallery .clearph-gallery-label--middle-center { top: 50%; left: 20px; right: 20px; bottom: auto; transform: translateY(-50%); text-align: center; }
.clearph-gallery .clearph-gallery-label--middle-right  { top: 50%; right: 20px; left: auto; bottom: auto; transform: translateY(-50%); text-align: right; }

.clearph-gallery .clearph-gallery-label--top-left   { top: 40px; left: 20px; right: auto; bottom: auto; text-align: left; transform: none; }
.clearph-gallery .clearph-gallery-label--top-center { top: 40px; left: 20px; right: 20px; bottom: auto; text-align: center; transform: none; }
.clearph-gallery .clearph-gallery-label--top-right  { top: 40px; right: 20px; left: auto; bottom: auto; text-align: right; transform: none; }

/* ===== Lightbox Caption Opt-In ===== */
/* When a gallery opts into lightbox captions (Show labels on lightbox image),
   FancyBox's container gets the .clearph-fancybox-captioned baseClass.
   This rule re-reveals the caption in case a host theme has hidden it. */
.clearph-fancybox-captioned .fancybox-caption,
.clearph-fancybox-captioned .fancybox-caption--separate {
    opacity: 1 !important;
    visibility: visible !important;
}

/* "Hide lightbox caption" setting: keep the caption text in the DOM (for SEO /
   accessibility) but hide it visually. Placed after the captioned rule so it
   wins if a gallery somehow has both states. */
.clearph-fancybox-caption-hidden .fancybox-caption,
.clearph-fancybox-caption-hidden .fancybox-caption--separate {
    opacity: 0 !important;
    visibility: hidden !important;
    pointer-events: none !important;
}

/* ===== Lightbox "Resize to Next Image" transition ===== */
/* The image box animates through other shapes on its way to the next image's
   aspect ratio; cover keeps the picture undistorted mid-flight (at rest the
   box matches the image exactly, so nothing is cropped). */
.clearph-fancybox-resize .fancybox-slide--image .fancybox-image {
    object-fit: cover;
}

.clearph-fancybox-resize .fancybox-content.clearph-is-resizing,
.clearph-fancybox-ghost {
    transition-property: transform, width, height, opacity;
    transition-timing-function: cubic-bezier(.5, 0, .14, 1);
}

/* Copy of the previous image, fading out over the new one as both resize */
.clearph-fancybox-ghost {
    position: absolute;
    top: 0;
    left: 0;
    object-fit: cover;
    pointer-events: none;
    transform-origin: top left;
    z-index: 99996;
}

/* Tablet spans: at 481-768px galleries drop to 4 tracks, so items carry
   tablet-safe spans (set inline by class-frontend.php grid_span_style()).
   !important beats the desktop inline spans. Skipped while a category filter
   is active, which applies its own uniform spans. */
@media (min-width: 481px) and (max-width: 768px) {
    .clearph-gallery.masonry-enabled:not(.is-filtered) .gallery-item[style*="--clearph-tablet-col"] {
        grid-column: var(--clearph-tablet-col) !important;
        grid-row: var(--clearph-tablet-row) !important;
    }
}

/* Phones, proportional galleries: best-fit row count per item instead of the
   one-row-for-everything rule above (rows are track-width tall here, so a
   single row would crop portraits hard). */
@media (max-width: 480px) {
    .clearph-gallery.masonry-enabled[data-row-height="proportional"]:not(.is-filtered) .gallery-item[style*="--clearph-phone-row"] {
        grid-row: var(--clearph-phone-row) !important;
    }
}

/* Print styles */
@media print {
    .clearph-gallery {
        display: block !important;
    }

    .clearph-gallery .gallery-item {
        display: inline-block;
        width: 48%;
        margin: 1%;
        break-inside: avoid;
    }
}