/* Gallery Layout */
.gallery {
  display: flex;
  flex-wrap: wrap;
  margin: -5px; /* Default margin between items */
  width: calc(100% + 10px);
}

.gallery .box {
  width: 100%;
  padding: 5px;
  position: relative;
  box-sizing: border-box;
}

/* Responsive Columns */
@media (min-width: 480px) {
  .gallery .box {
    width: 50%; /* 2 columns */
  }
}

@media (min-width: 768px) {
  .gallery .box {
    width: 33.333%; /* 3 columns */
  }
}

/* Remove 4-column layout for larger screens to keep 3 columns */
/* @media (min-width: 1200px) { ... } removed */

.gallery figure {
  margin: 0;
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1; /* Force square thumbnails ONLY in gallery */
  overflow: hidden;
  background-color: #f0f0f0;
}

/* Standalone images should not be forced to square */
figure:not(.gallery figure) {
  width: auto;
  height: auto;
  aspect-ratio: auto;
}

.gallery figure img {
  width: 100%;
  height: 100%;
  object-fit: cover; /* This crops the non-square thumbnail visually */
  display: block;
  transition: transform 0.3s ease;
}

/* Standalone images natural size */
figure:not(.gallery figure) img {
  width: 100%; /* Responsive width */
  height: auto; /* Maintain aspect ratio */
  object-fit: contain;
}



/* Hover Effect */
.gallery figure a:hover img {
  transform: scale(1.05);
}

/* Caption Styles */
.gallery figcaption {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 8px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 0.85rem;
  text-align: center;
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
}

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

/* Gallery-wide Caption */
.gallery-caption {
  margin-top: 10px;
  text-align: center;
  font-style: italic;
  color: #666;
  width: 100%;
}

/* Standalone Image Captions */
figure:not(.gallery figure) figcaption {
  text-align: center;
  font-style: italic;
  margin-top: 10px;
  color: #666;
  font-size: 0.9em;
}

/* PhotoSwipe Custom Caption */
.pswp__custom-caption {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  padding: 10px;
  text-align: center;
  font-size: 14px;
  line-height: 1.5;
  pointer-events: none; /* Let clicks pass through if needed, though usually text selection is nice. But for now, safe default. */
  z-index: 2000; /* Ensure it's above image but below controls if they are high? PhotoSwipe z-inds are usually high. */
}

.pswp__custom-caption a {
  color: #fff;
  text-decoration: underline;
  pointer-events: auto; /* Allow clicking links */
}

/* B2 Gallery Masonry Layout */
.b2-gallery {
  /* Force column layout */
  -webkit-column-count: 1;
  -moz-column-count: 1;
  column-count: 1;

  -webkit-column-gap: 20px !important;
  -moz-column-gap: 20px !important;
  column-gap: 20px !important;

  width: 100% !important;
  box-sizing: border-box !important;
  display: block !important;
}

/* 2 columns for small tablets (landscape phones) and up */
@media (min-width: 576px) {
  .b2-gallery {
    -webkit-column-count: 2;
    -moz-column-count: 2;
    column-count: 2;
  }
}

/* 3 columns for desktops */
@media (min-width: 992px) {
  .b2-gallery {
    -webkit-column-count: 3;
    -moz-column-count: 3;
    column-count: 3;
    max-width: 1600px;
    margin: 0 auto;
  }
}

.b2-gallery figure {
  /* Force margins and layout */
  margin: 0 0 10px 0 !important;
  padding: 0 !important;

  -webkit-column-break-inside: avoid;
  page-break-inside: avoid;
  break-inside: avoid;

  background: transparent;
  display: inline-block !important;
  /* inline-block is crucial for break-inside to work in some browsers */
  width: 100% !important;
  box-sizing: border-box;
}

.b2-gallery figure img {
  width: 100% !important;
  height: auto !important;
  display: block !important;
  border-radius: 0 !important;
}

/* Custom PhotoSwipe Button Styling */
.pswp__button--open-original {
  background: none;
  padding: 0;
  margin-top: 5px !important;
  /* Moved down */
  margin-right: -10px !important;
  /* Moved right (negative margin pulls it right if flex ordering allows, or pushes neighbours) - actually depends on flex direction. */
  /* If it's in a flex row (default pswp), margin-left pushes it right? No, order handles pos. */
  /* Let's try margin-left to push it away from the previous element, moving IT right. */
  margin-left: 5px !important;

  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.75;
  /* Match typical inactive opacity */
  transition: opacity 0.2s;
}

.pswp__button--open-original:hover {
  opacity: 1;
}

.pswp__button--open-original svg {
  width: 20px;
  height: 20px;
  stroke: #fff;
  /* "Grey outline" simulation using stronger/multiple drop shadows */
  filter: drop-shadow(1px 1px 0px rgba(0, 0, 0, 0.5)) drop-shadow(-1px -1px 0px rgba(0, 0, 0, 0.5)) drop-shadow(1px -1px 0px rgba(0, 0, 0, 0.5)) drop-shadow(-1px 1px 0px rgba(0, 0, 0, 0.5));
}