/* Gallery Grid Setup */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 1rem;
  max-width: 1200px;
  margin: 1.5rem auto;
}

.gallery-item {
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  aspect-ratio: 600 / 430;
  cursor: pointer;
  background-color: #1a1a1a;
}

.gallery-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
  display: block;
  
  /* Protection on Grid Thumbnails */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-user-drag: none;
}

.gallery-item:hover img {
  transform: scale(1.04);
}

/* Lightbox Overlay */
.lightbox {
  position: fixed;
  inset: 0;
  background-color: #3a6c85;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  z-index: 9999;
}

.lightbox.active {
  opacity: 1;
  pointer-events: auto;
}

.lightbox-stage {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  overflow: hidden;
}

.lightbox-img {
  max-width: 90vw;
  max-height: 80vh;
  object-fit: contain;
  border-radius: 4px;
  transform-origin: center center;
  transition: transform 0.1s ease-out, opacity 0.2s ease;
  opacity: 0; /* Hidden until loaded to reveal cleanly */

  /* Download Prevention Rules */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-user-drag: none;
}

.lightbox-img.loaded {
  opacity: 1;
}

/* CSS Spinner Loader */
.lightbox-spinner {
  position: absolute;
  width: 44px;
  height: 44px;
  border: 3px solid rgba(255, 255, 255, 0.15);
  border-top-color: #ffffff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  z-index: 5;
  display: none;
}

.lightbox-spinner.active {
  display: block;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Open Sans Image Counter */
.lightbox-counter {
  position: absolute;
  top: 1.5rem;
  left: 1.5rem;
  font-family: 'Open Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: rgba(255, 255, 255, 0.9);
  background: #2a3439;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  padding: 0.4rem 0.8rem;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  pointer-events: none;
  z-index: 10;
}

/* Open Sans Caption Bar */
.lightbox-caption {
  position: absolute;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%);
  
  font-family: 'Open Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  font-size: 0.875rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: #ffffff;
  
  background: #2a3439;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  padding: 0.5rem 1.25rem;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  
  text-align: center;
  max-width: 80vw;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  
  pointer-events: none;
  transition: opacity 0.2s ease;
  z-index: 10;
}

.lightbox-caption:empty {
  display: none;
}

/* Controls */
.lightbox-btn {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: #fff;
  font-size: 1.5rem;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease;
  z-index: 10;
}

.lightbox-btn:hover {
  background: rgba(255, 255, 255, 0.25);
}

.close-btn { top: 1.5rem; right: 1.5rem; }
.fullscreen-btn { top: 1.5rem; right: 5rem; }
.prev-btn { left: 1.5rem; }
.next-btn { right: 1.5rem; }

@media (max-width: 768px) {
  /* Flexbox layout to keep image and caption anchored together on mobile */
  .lightbox-stage {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
  }

  .lightbox-img {
    max-height: 70vh; /* Leaves comfortable vertical space for caption box */
  }

  .lightbox-caption {
    position: relative;
    bottom: auto;
    left: auto;
    transform: none;
    margin-top: 0.25rem;
    font-size: 0.8rem;
    max-width: 90vw;
    padding: 0.4rem 0.8rem;
  }

  .prev-btn, .next-btn { display: none; }
  .fullscreen-btn { right: 4.5rem; }
  .lightbox-counter {
    top: 1.5rem;
    left: 1rem;
    font-size: 0.75rem;
    padding: 0.3rem 0.6rem;
  }
}