:root {
  --gal-gap: 1.5rem;
  --gal-radius: 8px;
  --lightbox-bg: rgba(0, 0, 0, 0.85);
  --lightbox-z: 1055; /* Sits above BS5 modals */
  --transition-speed: 0.3s;
}

/* =========================================
   1. Base Gallery & Figure Styles
   ========================================= */
.gallery-figure {
  position: relative;
  overflow: hidden;
  border-radius: var(--gal-radius);
  margin-bottom: var(--gal-gap);
  cursor: pointer;
  transition: transform var(--transition-speed) ease;
}

.gallery-figure:hover {
  transform: translateY(-3px);
}

.gallery-figure img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block; /* Removes inline-block bottom gap */
}

.gallery-figure figcaption {
  position: absolute;
  inset: auto 0 0 0; /* Modern shorthand for bottom: 0; left: 0; width: 100%; */
  padding: 1rem;
  background: linear-gradient(to top, rgba(0,0,0,0.8), transparent);
  color: #fff;
  opacity: 0;
  transition: opacity var(--transition-speed) ease;
}

.gallery-figure:hover figcaption {
  opacity: 1;
}

/* =========================================
   2. Layout A: 4-Column Grid (Standard)
   ========================================= */
.layout-grid .gallery-figure {
  aspect-ratio: 1 / 1; /* Forces uniform squares */
}
/* =========================================
   Layout C: Bento Box (Zig-Zag Grid)
   ========================================= */
.layout-bento.row {
  display: grid !important; /* Overrides Bootstrap flexbox */
  grid-template-columns: repeat(4, 1fr);
  grid-auto-rows: 250px;
  gap: var(--gal-gap);
  margin: 0;
  
  /* FIX 1: Prevents Isotope from setting a wrong height, fixing the footer collision */
  height: auto !important; 
  
  /* FIX 2: Forces smaller images to backfill the empty white spaces */
  grid-auto-flow: dense; 
}

/* FIX 3: Strip Isotope's absolute positioning so CSS Grid takes 100% control */
.layout-bento .bento-item {
  position: relative !important;
  left: auto !important;
  top: auto !important;
  width: 100% !important;
  padding: 0 !important;
  margin: 0 !important;
  transform: none !important;
}

/* Base style for the figure to ensure it fills the grid cell */
.layout-bento .gallery-figure {
  height: 100%;
  margin: 0;
}

/* 
   THE ZIG-ZAG LOGIC
   Uses the new PHP classes to anchor the feature images to specific columns 
*/
.layout-bento .bento-item:has(.bento-left) {
  grid-column: 1 / span 2; /* Anchors to the far left */
  grid-row: span 2;
}

.layout-bento .isotopeSelector:has(.bento-right) {
  grid-column: 3 / span 2; /* Anchors to the right side */
  grid-row: span 2;
}

/* Responsive Behavior */
@media (max-width: 991.98px) {
  .layout-bento.row { grid-template-columns: repeat(2, 1fr); }
  
  /* On tablets, the zig-zag stops and all features just span full width */
  .layout-bento .bento-item:has(.bento-left),
  .layout-bento .bento-item:has(.bento-right) {
    grid-column: 1 / span 2; 
    grid-row: span 1; /* Reduce height slightly for tablet */
  }
}

@media (max-width: 575.98px) {
  .layout-bento.row { grid-template-columns: 1fr; }
  .layout-bento .bento-item:has(.bento-left),
  .layout-bento .bento-item:has(.bento-right) {
    grid-column: span 1;
  }
}

/* =========================================
   4. Lightbox Overlay & Controls
   ========================================= */
.custom-lightbox {
  position: fixed;
  inset: 0; /* FIX 1: Replaces width:100vw to prevent mobile horizontal overflow */
  background: var(--lightbox-bg);
  z-index: var(--lightbox-z);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--transition-speed) ease;
}

.custom-lightbox.active {
  opacity: 1;
  visibility: visible;
}

.lightbox-img-wrapper {
  position: relative;
  max-width: 90%;
  max-height: 80vh;
  display: flex;
  justify-content: center;
}

.lightbox-img {
  max-width: 100%;
  max-height: 80vh;
  object-fit: contain;
  user-select: none;
}

.lightbox-caption {
  color: #fff;
  margin-top: 1rem;
  font-size: 1.1rem;
  text-align: center;
  min-height: 1.5rem;
}

/* Lightbox Buttons */
.lb-ctrl {
  position: absolute;
  background: transparent;
  border: none;
  color: #fff;
  font-size: 2rem;
  cursor: pointer;
  padding: 1rem;
  z-index: calc(var(--lightbox-z) + 1);
  transition: transform 0.2s ease-in-out;
}

.lb-close { 
  top: 1rem; 
  right: 1rem; 
}

.lb-close:hover { 
  transform: scale(1.2); 
}

.lb-prev, .lb-next {
    top: 50%; 
    height: 60%;
    width: 25%;
    cursor: pointer;
    display: block;
    transform: translateY(-50%); 
}

.lb-prev { 
  left: 1rem; 
}

.lb-next { 
  right: 1rem; 
}

.lb-prev:hover,
.lb-next:hover { 
  transform: translateY(-50%) scale(1.2); 
}

/* =========================================
   5. Filter Animations
   ========================================= */
.fade-in {
    animation: galFadeIn 0.4s ease forwards;
}

.fade-out {
    animation: galFadeOut 0.3s ease forwards;
}

@keyframes galFadeIn {
    from { opacity: 0; transform: scale(0.95); }
    to { opacity: 1; transform: scale(1); }
}

@keyframes galFadeOut {
    from { opacity: 1; transform: scale(1); }
    to { opacity: 0; transform: scale(0.95); }
}

/* =========================================
   6. Responsive Filter Navigation
   ========================================= */

.gallery-filter-nav {
  list-style: none;
  margin: 0;
  padding: 0;
}

.gallery-filter-nav .nav-item {
  margin: 0;
}

.gallery-filter-nav .btn {
  white-space: nowrap;
}

/* Desktop: Center aligned */
@media (min-width: 992px) {
  .gallery-filter-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    justify-content: center;
  }
  .filter-scroll-btn {
    display: none !important;
  }
}

/* Tablet & Mobile: Horizontal scrollable with scroll indicators */
@media (max-width: 991.98px) {
  .gallery-filter-nav {
    position: relative;
    display: flex;
    flex-wrap: nowrap;
    gap: 0.5rem;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
    padding: 0.25rem 1rem;
    justify-content: flex-start;
    align-items: center;
  }

  .gallery-filter-nav::-webkit-scrollbar {
    display: none;
  }

  /* Removed static pseudo-elements for scroll arrows */
  .filter-nav-wrapper {
    position: relative;
    display: flex;
    align-items: center;
  }

  .filter-scroll-btn {
    background: transparent;
    border: none;
    font-size: 28px;
    line-height: 1;
    color: #666;
    padding: 0 10px;
    z-index: 5;
    transition: opacity 0.2s;
  }
  
  .filter-scroll-btn:focus {
    outline: none;
  }

  .gallery-filter-nav .nav-item {
    flex-shrink: 0;
    margin: 0;
  }

  .gallery-filter-nav .btn {
    font-size: 0.9rem;
  }
}

/* Small Mobile: Adjust font size */
@media (max-width: 575.98px) {
  .gallery-filter-nav .btn {
    font-size: 0.85rem;
  }
}

    /* Gallery item animations */
    .bento-item {
        transition: opacity 0.35s ease, transform 0.35s ease;
    }

    /* Initial page load animation - staggered entrance */
    .bento-item.gal-initial {
        animation: galleryItemLoad 0.5s ease-out forwards;
        animation-delay: calc(var(--item-index, 0) * 50ms);
        opacity: 0;
        transform: scale(0.9);
    }

    @keyframes galleryItemLoad {
        to {
            opacity: 1;
            transform: scale(1);
        }
    }

    /* Section Re-render animation */
    #main-gallery-wrapper {
        transition: opacity 0.3s ease, transform 0.3s ease;
    }
    
    #main-gallery-wrapper.re-rendering {
        opacity: 0;
        transform: translateY(10px);
    }

    /* Filter animation - when showing items */
    .bento-item.gal-showing {
        opacity: 1;
        transform: scale(1);
    }

    /* Filter button subtle click feedback */
    .gallery-filter-nav .btn {
        transition: opacity 0.15s ease;
    }
    .gallery-filter-nav .btn.filter-btn-active {
        animation: filterBtnBlink 0.3s ease;
    }

    @keyframes filterBtnBlink {
        0%, 100% { opacity: 1; }
        50% { opacity: 0.7; }
    }