/**
 * Kore_ProductGallery / kore-product-gallery — Sovereign CUBE CSS.
 * Interactive product gallery with main stage view, thumbnails, photo grid, and fullscreen lightbox.
 * Governed strictly by OKLCH tokens and var(--radius-control) (Zero Pills, Zero Hardcoded Pixels/Hex).
 *
 * @package WP_Kore
 */

.product-gallery {
  container-type: inline-size;
  --local-brand: var(--brand);
  --local-surface: var(--surface);
  --local-surface-border: var(--surface-border);
  --local-border: var(--surface-border);
  --local-radius: var(--radius-card);
  --local-shadow: var(--shadow-sm);
  --local-text: var(--text-main);
  --local-cols: 3;
  --local-display: flex;
  --local-grid-cols: repeat(var(--local-cols), minmax(0, 1fr));
  --local-flex-direction: column;
  --local-align-items: stretch;
  --local-gap: var(--space-sm);
  --local-width: 100%;
  --local-img-fit: cover;
  --local-aspect-ratio: 1 / 1;

  display: var(--local-display);
  grid-template-columns: var(--local-grid-cols);
  flex-direction: var(--local-flex-direction);
  align-items: var(--local-align-items);
  gap: var(--local-gap);
  width: var(--local-width);
  box-sizing: border-box;
  color: var(--local-text);
}

/* ── STAGE VIEW (Main image stage; ratio driven by data-aspect-ratio) ── */
.product-gallery__stage {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  overflow: hidden;
  border-radius: var(--local-radius);
  background-color: var(--local-surface);
  border: var(--border-width) solid var(--local-border);
  box-shadow: var(--local-shadow);
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Aspect Ratio Modifiers */
.product-gallery[data-aspect-ratio="1:1"] .product-gallery__stage { aspect-ratio: 1 / 1; }
.product-gallery[data-aspect-ratio="4:3"] .product-gallery__stage { aspect-ratio: 4 / 3; }
.product-gallery[data-aspect-ratio="16:9"] .product-gallery__stage { aspect-ratio: 16 / 9; }
.product-gallery[data-aspect-ratio="auto"] .product-gallery__stage { aspect-ratio: auto; }

.product-gallery__main-img {
  --local-img-scale: scale(1);
  width: 100%;
  height: 100%;
  min-height: 100%;
  max-height: 100%;
  object-fit: var(--local-img-fit);
  display: block;
  transform: var(--local-img-scale);
  transition: transform var(--motion), opacity var(--motion);
  cursor: zoom-in;
}

/* Image Fit Modifiers */
.product-gallery[data-image-fit="contain"] { --local-img-fit: contain; }
.product-gallery[data-image-fit="cover"] { --local-img-fit: cover; }

.product-gallery__stage:hover .product-gallery__main-img {
  --local-img-scale: scale(1.03);
}

/* Zoom / Lightbox Button */
.product-gallery__zoom-btn {
  --local-zoom-bg: var(--surface-elevated);
  --local-zoom-color: var(--text-main);
  --local-zoom-scale: scale(1);
  position: absolute;
  top: var(--space-xs);
  right: var(--space-xs);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: var(--radius-control);
  border: var(--border-width) solid var(--surface-border);
  background-color: var(--local-zoom-bg);
  color: var(--local-zoom-color);
  cursor: pointer;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  transform: var(--local-zoom-scale);
  transition: transform var(--motion), background-color var(--motion), color var(--motion);
  z-index: 10;
}

.product-gallery__zoom-btn:hover {
  --local-zoom-bg: var(--local-brand);
  --local-zoom-color: var(--brand-on);
  --local-zoom-scale: scale(1.08);
}

/* ── PREVIEW STAGE NAVIGATION ARROWS ── */
.product-gallery__stage-nav {
  --local-stage-nav-bg: var(--surface-control-floating);
  --local-stage-nav-color: var(--surface-control-floating-color);
  --local-stage-nav-border: var(--surface-control-floating-border);
  --local-stage-nav-shadow: var(--surface-control-floating-shadow);
  --local-stage-nav-scale: scale(1);
  --local-stage-nav-opacity: 0.9;

  position: absolute;
  top: 50%;
  transform: translateY(-50%) var(--local-stage-nav-scale);
  width: 2.75rem;
  height: 2.75rem;
  min-width: 2.75rem;
  min-height: 2.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-control);
  border: var(--border-width) solid var(--local-stage-nav-border);
  background-color: var(--local-stage-nav-bg);
  color: var(--local-stage-nav-color);
  box-shadow: var(--local-stage-nav-shadow);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  cursor: pointer;
  z-index: 10;
  opacity: var(--local-stage-nav-opacity);
  transition: transform var(--motion), background-color var(--motion), color var(--motion), opacity var(--motion), box-shadow var(--motion);
}

.product-gallery__stage-nav:hover {
  --local-stage-nav-bg: var(--surface-elevated);
  --local-stage-nav-color: var(--brand);
  --local-stage-nav-border: var(--brand);
  --local-stage-nav-shadow: var(--surface-control-floating-shadow-hover);
  --local-stage-nav-scale: scale(1.08);
  --local-stage-nav-opacity: 1;
}

.product-gallery__stage-nav:active {
  --local-stage-nav-scale: scale(0.95);
}

.product-gallery__stage-nav--prev {
  left: var(--space-xs);
}

.product-gallery__stage-nav--next {
  right: var(--space-xs);
}

@container (max-width: 30rem) {
  .product-gallery__stage-nav {
    width: 2.5rem;
    height: 2.5rem;
    min-width: 2.5rem;
    min-height: 2.5rem;
  }
}

/* ── THUMBNAILS STRIP (Horizontal / Bottom) ── */
.product-gallery__thumbs-wrap {
  --local-thumbs-flex: 0 0 auto;
  --local-thumbs-wrap-width: 100%;
  --local-thumbs-max-height: none;
  --local-thumbs-overflow-y: visible;
  --local-thumbs-overflow-x: auto;
  --local-thumbs-wrap-padding: var(--space-2xs) 0;

  flex: var(--local-thumbs-flex);
  flex-shrink: 0;
  width: var(--local-thumbs-wrap-width);
  max-height: var(--local-thumbs-max-height);
  overflow-y: var(--local-thumbs-overflow-y);
  overflow-x: var(--local-thumbs-overflow-x);
  scrollbar-width: thin;
  padding: var(--local-thumbs-wrap-padding);
  box-sizing: border-box;
}

.product-gallery__thumbs {
  --local-thumbs-direction: row;
  --local-thumbs-width: auto;

  display: flex;
  gap: var(--space-xs);
  align-items: center;
  flex-direction: var(--local-thumbs-direction);
  width: var(--local-thumbs-width);
  box-sizing: border-box;
}

.product-gallery__thumb {
  --local-thumb-flex: 0 0 5.5rem;
  --local-thumb-width: 5.5rem;
  --local-thumb-opacity: 0.6;
  --local-thumb-border: var(--border-width) solid transparent;
  --local-thumb-transform: translateY(0);

  flex: var(--local-thumb-flex);
  flex-shrink: 0;
  width: var(--local-thumb-width);
  min-width: 5.5rem;
  height: 5.5rem;
  min-height: 5.5rem;
  padding: 0;
  margin: 0;
  border: var(--local-thumb-border);
  border-radius: var(--radius-control);
  overflow: hidden;
  background: var(--local-surface);
  box-shadow: none;
  cursor: pointer;
  opacity: var(--local-thumb-opacity);
  transform: var(--local-thumb-transform);
  transition: opacity var(--motion), transform var(--motion), border-color var(--motion);
  box-sizing: border-box;
}

.product-gallery__thumb img {
  width: 100%;
  height: 100%;
  min-width: 100%;
  min-height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: cover;
  display: block;
  border-radius: inherit;
  box-sizing: border-box;
}

.product-gallery__thumb:hover {
  --local-thumb-opacity: 0.9;
  --local-thumb-transform: translateY(calc(-1 * var(--space-3xs)));
}

.product-gallery__thumb.is-active {
  --local-thumb-opacity: 1;
  --local-thumb-border: var(--border-width) solid var(--local-brand);
}

/* ── THUMBNAIL ASPECT RATIO HARMONIZATION ── */
.product-gallery[data-aspect-ratio="16:9"] .product-gallery__thumb {
  --local-thumb-flex: 0 0 6.75rem;
  --local-thumb-width: 6.75rem;
  width: var(--local-thumb-width);
  min-width: var(--local-thumb-width);
  aspect-ratio: 16 / 9;
  height: auto;
  min-height: unset;
}

.product-gallery[data-aspect-ratio="4:3"] .product-gallery__thumb {
  --local-thumb-flex: 0 0 6rem;
  --local-thumb-width: 6rem;
  width: var(--local-thumb-width);
  min-width: var(--local-thumb-width);
  aspect-ratio: 4 / 3;
  height: auto;
  min-height: unset;
}

/* ── THUMBNAILS POSITION (Left / Right) ── */
:is(.product-gallery[data-thumbs-position="left"], .product-gallery.product-gallery-thumbs-left) {
  --local-flex-direction: row-reverse;
  --local-align-items: flex-start;
}

:is(.product-gallery[data-thumbs-position="right"], .product-gallery.product-gallery-thumbs-right) {
  --local-flex-direction: row;
  --local-align-items: flex-start;
}

:is(.product-gallery[data-thumbs-position="left"], .product-gallery.product-gallery-thumbs-left) .product-gallery__stage,
:is(.product-gallery[data-thumbs-position="right"], .product-gallery.product-gallery-thumbs-right) .product-gallery__stage {
  flex: 1 1 auto;
}

:is(.product-gallery[data-thumbs-position="left"], .product-gallery.product-gallery-thumbs-left) .product-gallery__thumbs-wrap,
:is(.product-gallery[data-thumbs-position="right"], .product-gallery.product-gallery-thumbs-right) .product-gallery__thumbs-wrap {
  --local-thumbs-flex: 0 0 6.5rem;
  --local-thumbs-wrap-width: 6.5rem;
  --local-thumbs-max-height: 100%;
  --local-thumbs-overflow-y: auto;
  --local-thumbs-overflow-x: hidden;
  --local-thumbs-wrap-padding: 0;
}

:is(.product-gallery[data-thumbs-position="left"], .product-gallery.product-gallery-thumbs-left) .product-gallery__thumbs,
:is(.product-gallery[data-thumbs-position="right"], .product-gallery.product-gallery-thumbs-right) .product-gallery__thumbs {
  --local-thumbs-direction: column;
  --local-thumbs-width: 100%;
}

:is(.product-gallery[data-thumbs-position="left"], .product-gallery.product-gallery-thumbs-left) .product-gallery__thumb,
:is(.product-gallery[data-thumbs-position="right"], .product-gallery.product-gallery-thumbs-right) .product-gallery__thumb {
  --local-thumb-width: 100%;
  --local-thumb-flex: 0 0 5.5rem;
}

:is(.product-gallery[data-thumbs-position="left"], .product-gallery.product-gallery-thumbs-left)[data-aspect-ratio="16:9"] .product-gallery__thumb,
:is(.product-gallery[data-thumbs-position="right"], .product-gallery.product-gallery-thumbs-right)[data-aspect-ratio="16:9"] .product-gallery__thumb {
  --local-thumb-flex: 0 0 auto;
  aspect-ratio: 16 / 9;
  height: auto;
  min-height: unset;
}

/* ── LIGHTBOX MODAL FULLSCREEN (<dialog> Reset & Sovereign CUBE Overlay) ── */
html body dialog.product-gallery__lightbox,
html body .product-gallery__lightbox {
  position: fixed;
  inset: 0;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  max-width: 100vw;
  max-height: 100vh;
  padding: 0;
  margin: 0;
  border: none;
  background: var(--lightbox-backdrop);
  backdrop-filter: blur(28px);
  -webkit-backdrop-filter: blur(28px);
  z-index: var(--z-modal);
  display: none;
  align-items: center;
  justify-content: center;
  color: var(--white);
  box-sizing: border-box;
  overflow: hidden;
}

html body dialog.product-gallery__lightbox[open],
html body .product-gallery__lightbox[open] {
  display: flex;
}

dialog.product-gallery__lightbox::backdrop {
  background: var(--lightbox-backdrop);
  backdrop-filter: blur(28px);
  -webkit-backdrop-filter: blur(28px);
}

.product-gallery__lightbox-content {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  padding: var(--space-lg);
  box-sizing: border-box;
}

/* Lightbox Top Toolbar */
.product-gallery__lightbox-toolbar {
  position: absolute;
  top: var(--space-md);
  right: var(--space-md);
  display: flex;
  align-items: center;
  gap: var(--space-2xs);
  z-index: 1000000;
}

.product-gallery__lightbox-tool-btn {
  --local-tool-bg: var(--lightbox-control-bg);
  --local-tool-color: var(--lightbox-control-color);
  --local-tool-border: var(--lightbox-control-border);
  --local-tool-scale: scale(1);

  border: var(--border-width) solid var(--local-tool-border);
  background-color: var(--local-tool-bg);
  color: var(--local-tool-color);
  width: 2.75rem;
  height: 2.75rem;
  min-width: 2.75rem;
  min-height: 2.75rem;
  border-radius: var(--radius-control);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 0.5rem 1.5rem oklch(0% 0 0 / 35%);
  transform: var(--local-tool-scale);
  transition: transform var(--motion), background-color var(--motion), color var(--motion), border-color var(--motion);
}

.product-gallery__lightbox-tool-btn:hover {
  --local-tool-bg: var(--lightbox-control-hover-bg);
  --local-tool-color: var(--lightbox-control-hover-color);
  --local-tool-border: transparent;
  --local-tool-scale: scale(1.08);
}

.product-gallery__lightbox-tool-btn:active {
  --local-tool-scale: scale(0.95);
}

.product-gallery__lightbox-zoom-val {
  font-family: var(--font-mono);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-bold);
  letter-spacing: var(--tracking-mono);
}

.product-gallery__lightbox-stage {
  position: relative;
  max-width: 90vw;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.product-gallery__lightbox-viewport {
  position: relative;
  max-width: 100%;
  max-height: 75vh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border-radius: var(--radius-card);
  cursor: zoom-in;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.product-gallery__lightbox-viewport.is-zoomed {
  cursor: grab;
}

.product-gallery__lightbox-viewport.is-panning {
  cursor: grabbing;
}

.product-gallery__lightbox-img {
  max-width: 100%;
  max-height: 75vh;
  object-fit: contain;
  border-radius: var(--radius-card);
  box-shadow: 0 1.5rem 3.5rem -0.5rem oklch(0% 0 0 / 60%), 0 0 0 1px oklch(100% 0 0 / 14%);
  border: none;
  transform-origin: center center;
  transition: transform 0.22s cubic-bezier(0.16, 1, 0.3, 1);
  will-change: transform;
  pointer-events: auto;
}

.product-gallery__lightbox-viewport.is-panning .product-gallery__lightbox-img {
  transition: none;
}

.product-gallery__lightbox-footer {
  margin-top: var(--space-sm);
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

.product-gallery__lightbox-counter {
  font-family: var(--font-mono);
  font-size: var(--font-size-sm);
  letter-spacing: var(--tracking-mono);
  text-transform: uppercase;
  color: var(--white);
  background: var(--lightbox-control-bg);
  padding: var(--space-3xs) var(--space-xs);
  border-radius: var(--radius-control);
  border: var(--border-width) solid var(--lightbox-control-border);
  box-shadow: 0 0.25rem 0.75rem oklch(0% 0 0 / 25%);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

.product-gallery__lightbox-hint {
  font-family: var(--font-mono);
  font-size: var(--font-size-sm);
  letter-spacing: var(--tracking-mono);
  color: oklch(100% 0 0 / 65%);
  text-transform: uppercase;
}

@container (max-width: 40rem) {
  .product-gallery__lightbox-hint {
    display: none;
  }
}

.product-gallery__lightbox-nav {
  --local-nav-bg: var(--lightbox-control-bg);
  --local-nav-color: var(--lightbox-control-color);
  --local-nav-border: var(--lightbox-control-border);
  --local-nav-scale: scale(1);
  position: absolute;
  top: 50%;
  transform: translateY(-50%) var(--local-nav-scale);
  border: var(--border-width) solid var(--local-nav-border);
  background-color: var(--local-nav-bg);
  color: var(--local-nav-color);
  width: 3rem;
  height: 3rem;
  min-width: 3rem;
  min-height: 3rem;
  border-radius: var(--radius-control);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 0.5rem 1.5rem oklch(0% 0 0 / 35%);
  transition: transform var(--motion), background-color var(--motion), color var(--motion), border-color var(--motion);
  z-index: 1000000;
}

.product-gallery__lightbox-nav:hover {
  --local-nav-bg: var(--lightbox-control-hover-bg);
  --local-nav-color: var(--lightbox-control-hover-color);
  --local-nav-border: transparent;
  --local-nav-scale: scale(1.08);
}

.product-gallery__lightbox-nav--prev {
  left: var(--space-md);
}

.product-gallery__lightbox-nav--next {
  right: var(--space-md);
}

@container (min-width: 48rem) {
  .product-gallery__lightbox-nav--prev {
    left: var(--space-xl);
  }

  .product-gallery__lightbox-nav--next {
    right: var(--space-xl);
  }
}

/* ── PRESETS ── */
.product-gallery.product-gallery-stage {
  --local-display: flex;
}

.product-gallery.product-gallery-grid {
  --local-display: grid;
}

.product-gallery.product-gallery-grid .product-gallery__thumbs-wrap {
  display: none;
}

.product-gallery[data-columns="2"] { --local-cols: 2; }
.product-gallery[data-columns="3"] { --local-cols: 3; }
.product-gallery[data-columns="4"] { --local-cols: 4; }
.product-gallery[data-columns="5"] { --local-cols: 5; }

/* ── CANONICAL INTENTS ── */
.product-gallery.product-gallery-canvas {
  --local-brand: var(--brand);
  --local-surface: var(--canvas);
  --local-border: var(--surface-border);
  --local-text: var(--text-main);
}

.product-gallery.product-gallery-surface {
  --local-brand: var(--brand);
  --local-surface: var(--surface);
  --local-border: var(--surface-border);
  --local-text: var(--text-main);
}

.product-gallery.product-gallery-brand {
  --local-brand: var(--brand);
  --local-surface: var(--surface-elevated);
  --local-border: var(--brand);
  --local-text: var(--text-main);
}

.product-gallery.product-gallery-accent {
  --local-brand: var(--brand-accent);
  --local-surface: var(--surface-elevated);
  --local-border: var(--brand-accent);
  --local-text: var(--text-main);
}

.product-gallery.product-gallery-inverse {
  --local-brand: var(--brand-accent);
  --local-surface: var(--bg-dark-surface);
  --local-border: var(--bg-dark-border);
  --local-text: var(--bg-dark-on);
  --local-shadow: none;
  --local-zoom-bg: var(--bg-dark-elevated);
  --local-zoom-color: var(--bg-dark-on);
}

/* ── DISABLED STATE ── */
.product-gallery[data-state="disabled"],
.product-gallery:disabled {
  cursor: not-allowed;
  pointer-events: none;
  opacity: 0.5;
}

/* ── REDUCED MOTION ACCESSIBILITY GUARD ── */
@media (prefers-reduced-motion: reduce) {
  .product-gallery,
  .product-gallery *,
  .product-gallery *::before,
  .product-gallery *::after {
    animation-duration: 0.01ms;
    animation-iteration-count: 1;
    transition-duration: 0.01ms;
  }
}
