/* ============================================
   Universal Image Switcher (UIS)
   Vanilla JS - Zero Dependency
   ============================================ */

/* --- FOUC Prevention --- */
.uis-gallery {
  visibility: hidden;
}

.uis-gallery.is-initialized {
  visibility: visible;
}

/* --- Layout (Desktop: thumbs left, main right) --- */
.uis-gallery {
  display: flex;
  gap: 12px;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  position: relative;
}

/* --- Main Image Area (viewport / clip container) --- */
.uis-main {
  position: relative;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  background: #fff;
  -webkit-touch-callout: none;
  user-select: none;
  cursor: zoom-in;
}

/* --- Slide Strip (flex row, all images side by side) --- */
.uis-strip {
  display: flex;
  will-change: transform;
}

.uis-strip.is-animating {
  transition: transform var(--uis-slide-speed, 200ms) var(--uis-easing, cubic-bezier(0.25, 1, 0.5, 1));
}

/* --- Individual Slide --- */
.uis-slide {
  flex-shrink: 0;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.uis-slide img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0 auto;
  -webkit-user-select: none;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
}

/* --- Thumbnail Strip --- */
.uis-thumbs {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
  width: 80px;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  order: -1;
}

.uis-thumbs::-webkit-scrollbar {
  width: 4px;
}

.uis-thumbs::-webkit-scrollbar-thumb {
  background: #ccc;
  border-radius: 2px;
}

/* --- Thumbnail Button --- */
.uis-thumb {
  appearance: none;
  -webkit-appearance: none;
  display: block;
  padding: 0;
  margin: 0;
  background: none;
  border: 2px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
  flex-shrink: 0;
  transition: border-color 200ms ease, opacity 200ms ease;
  opacity: 0.6;
}

.uis-thumb:hover {
  opacity: 0.85;
}

.uis-thumb.is-active {
  border-color: #0066cc;
  opacity: 1;
}

.uis-thumb:focus-visible {
  outline: 2px solid #0066cc;
  outline-offset: 2px;
}

.uis-thumb img {
  display: block;
  width: 100%;
  height: auto;
  pointer-events: none;
}

/* --- Navigation Arrows --- */
.uis-arrow {
  appearance: none;
  -webkit-appearance: none;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 2;
  width: 36px;
  height: 36px;
  padding: 0;
  margin: 0;
  background: rgba(255, 255, 255, 0.85);
  border: 1px solid #ddd;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 200ms ease, background 200ms ease;
  opacity: 0;
}

.uis-gallery:hover .uis-arrow,
.uis-arrow:focus-visible {
  opacity: 1;
}

.uis-arrow:hover {
  background: rgba(255, 255, 255, 1);
}

.uis-arrow:focus-visible {
  outline: 2px solid #0066cc;
  outline-offset: 2px;
}

.uis-arrow.is-hidden {
  display: none;
}

.uis-arrow--prev {
  left: 8px;
}

.uis-arrow--next {
  right: 8px;
}

.uis-arrow svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: #333;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* --- Lazy Loading Placeholder --- */
.uis-thumb img[data-src] {
  background: #e8e8e8;
  min-height: 60px;
}

.uis-slide img[data-src] {
  background: #f0f0f0;
}

/* --- Modifier: Thumbs always at bottom --- */
.uis-gallery--thumbs-bottom {
  flex-direction: column;
  gap: 8px;
}

.uis-gallery--thumbs-bottom .uis-thumbs {
  flex-direction: row;
  width: auto;
  height: auto;
  overflow: hidden;
  order: 1;
  touch-action: pan-y;
}

.uis-gallery--thumbs-bottom .uis-thumbs.is-dragging {
  cursor: grabbing;
}

.uis-gallery--thumbs-bottom .uis-thumbs.is-dragging .uis-thumb {
  pointer-events: none;
}

.uis-thumbs-inner {
  display: flex;
  gap: 8px;
}

.uis-gallery--thumbs-bottom .uis-thumb {
  width: 64px;
  flex-shrink: 0;
}

/* --- Responsive (Mobile: thumbs bottom) --- */
@media (max-width: 767px) {
  .uis-gallery {
    flex-direction: column;
    gap: 6px;
  }

  .uis-thumbs {
    flex-direction: row;
    width: auto;
    height: auto;
    overflow: hidden;
    order: 1;
  }

  .uis-thumbs.is-dragging {
    cursor: grabbing;
  }

  .uis-thumbs.is-dragging .uis-thumb {
    pointer-events: none;
  }

  .uis-thumb {
    width: 48px;
    flex-shrink: 0;
  }

  .uis-main {
    cursor: default;
  }

  .uis-arrow {
    display: none !important;
  }
}

/* --- Screen Reader Only --- */
.uis-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* --- Reduced Motion --- */
@media (prefers-reduced-motion: reduce) {
  .uis-strip.is-animating {
    transition: none;
  }

  .uis-thumb {
    transition: none;
  }
}

/* --- Lightbox Modal --- */
.uis-lightbox {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9999;
}

.uis-lightbox__overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
}

/* White panel container — thumbs left, image right */
.uis-lightbox__container {
  position: absolute;
  z-index: 1;
  top: 5vh;
  bottom: 10vh;
  left: 50%;
  transform: translateX(-50%);
  width: 90vw;
  max-width: 1000px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: row;
  overflow: hidden;
}

/* Close button — inside panel top-right */
.uis-lightbox__close {
  position: absolute;
  top: 8px;
  right: 8px;
  appearance: none;
  background: rgba(0, 0, 0, 0.05);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  padding: 8px;
  width: 36px;
  height: 36px;
  z-index: 2;
  transition: background 200ms ease;
}

.uis-lightbox__close:hover {
  background: rgba(0, 0, 0, 0.1);
}

.uis-lightbox__close svg {
  width: 20px;
  height: 20px;
  stroke: #333;
  stroke-width: 2;
  fill: none;
}

.uis-lightbox__close:focus-visible {
  outline: 2px solid #0066cc;
  outline-offset: 2px;
}

/* Thumbnails — left column, vertical, 2-column grid if many */
.uis-lightbox__thumbs {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  padding: 5px;
  width: 140px;
  box-sizing: border-box;
  overflow-y: auto;
  flex-shrink: 0;
  align-content: start;
  border-right: 1px solid #eee;
  scrollbar-width: thin;
}

.uis-lightbox__thumb {
  appearance: none;
  width: 56px;
  height: 56px;
  padding: 5px;
  background: none;
  border: 2px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
  flex-shrink: 0;
  opacity: 0.5;
  transition: opacity 200ms ease, border-color 200ms ease;
}

.uis-lightbox__thumb:hover { opacity: 0.8; }
.uis-lightbox__thumb.is-active { border-color: #0066cc; opacity: 1; }
.uis-lightbox__thumb:focus-visible { outline: 2px solid #0066cc; outline-offset: 2px; }

.uis-lightbox__thumb img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

/* Stage — main image area, right side */
.uis-lightbox__stage {
  position: relative;
  overflow: hidden;
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
}

.uis-lightbox__strip {
  display: flex;
  will-change: transform;
  align-items: center;
}

.uis-lightbox__strip.is-animating {
  transition: transform 300ms cubic-bezier(0.25, 1, 0.5, 1);
}

.uis-lightbox__slide {
  flex-shrink: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

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

/* Arrows — inside stage */
.uis-lightbox__arrow {
  appearance: none;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 2;
  width: 40px;
  height: 40px;
  padding: 0;
  background: rgba(0, 0, 0, 0.04);
  border: 1px solid #ddd;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 200ms ease;
}

.uis-lightbox__arrow:hover {
  background: rgba(0, 0, 0, 0.08);
}

.uis-lightbox__arrow:focus-visible {
  outline: 2px solid #0066cc;
  outline-offset: 2px;
}

.uis-lightbox__arrow.is-hidden {
  display: none;
}

.uis-lightbox__arrow--prev { left: 8px; }
.uis-lightbox__arrow--next { right: 8px; }

.uis-lightbox__arrow svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: #333;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Affiliate buy bar — bottom of stage */
.uis-lightbox__buybar {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 3;
}

.uis-lightbox__buybar-label {
  position: absolute;
  right: 100%;
  margin-right: 10px;
  font-size: 13px;
  color: #666;
  white-space: nowrap;
}

.uis-lightbox__buy {
  display: inline-block;
  padding: 8px 24px;
  border-radius: 4px;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
  background: #f0ad4e;
  color: #fff;
  transition: opacity 200ms ease;
}

.uis-lightbox__buy + .uis-lightbox__buy {
  background: #5bc0de;
}

.uis-lightbox__buy:hover {
  opacity: 0.85;
  color: #fff;
  text-decoration: none;
}

@media (prefers-reduced-motion: reduce) {
  .uis-lightbox__strip.is-animating { transition: none; }
}

/* --- Progressive Image Loading --- */
.uis-lightbox__stage {
  cursor: zoom-in;
}

.uis-lightbox__stage * {
  cursor: inherit;
}

.uis-lightbox__arrow {
  cursor: pointer;
}


.uis-lightbox__stage.is-zoomed {
  cursor: zoom-out;
}

.uis-lightbox__stage.is-zoomed.is-panning {
  cursor: grabbing;
}

.uis-lightbox__stage.is-zoomed.is-max-zoom {
  cursor: zoom-out;
}

.uis-lightbox__stage.is-zoomed.is-max-zoom.is-panning {
  cursor: grabbing;
}

.uis-lightbox__stage.is-zoomed .uis-lightbox__img {
  will-change: transform;
}

.uis-lightbox__img.is-zoom-entering {
  transition: transform 400ms cubic-bezier(0.2, 0, 0, 1);
}

.uis-lightbox__stage.is-zoomed .uis-lightbox__slide.is-pulsing .uis-lightbox__img.is-active {
  animation: none;
}

.uis-lightbox__slide {
  position: relative;
}

/* Crossfade: two stacked image layers per slide */
.uis-lightbox__img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: calc(100% - 48px);
  max-height: calc(80vh - 48px);
  object-fit: contain;
  opacity: 0;
  transition: opacity 250ms ease;
}

.uis-lightbox__img.is-active {
  opacity: 1;
}

.uis-lightbox__spinner {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 40px;
  height: 40px;
  margin: -20px 0 0 -20px;
  border: 3px solid rgba(0, 0, 0, 0.1);
  border-top-color: #0066cc;
  border-radius: 50%;
  opacity: 0;
  pointer-events: none;
  z-index: 2;
  transition: opacity 150ms ease;
}

.uis-lightbox__spinner.is-visible {
  opacity: 1;
  animation: uis-spin 700ms linear infinite;
}

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

@keyframes uis-pulse {
  0%   { transform: translate(-50%, -50%) scale(1); }
  50%  { transform: translate(-50%, -50%) scale(1.03); }
  100% { transform: translate(-50%, -50%) scale(1); }
}

.uis-lightbox__slide.is-pulsing .uis-lightbox__img.is-active {
  animation: uis-pulse 260ms ease-out;
}

@media (prefers-reduced-motion: reduce) {
  .uis-lightbox__spinner.is-visible { animation: none; }
  .uis-lightbox__img { transition: none; }
  .uis-lightbox__slide.is-pulsing .uis-lightbox__img.is-active { animation: none; }
}
