/* ==========================================================================
   Portable Photo Gallery — gallery.css
   v3 — unified layout

   One layout mechanism serves all 8 position/overlay combinations:
     - the stage's aspect-ratio always defines the gallery's box
     - the thumbnail strip is always absolutely positioned against that box
     - non-overlay mode reserves space via a margin on the stage
   No flex-direction branching on the container, no per-combination special cases.

   Reskin by overriding the custom properties below; nothing else changes.
   ========================================================================== */

.pg-gallery {
  --pg-version: "v3-unified";

  --pg-bg: #ede7dd;
  --pg-ink: #2b2420;
  --pg-ink-soft: #6b6255;
  --pg-accent: #9c7a44;
  --pg-surface: #f5f1ea;
  --pg-overlay-scrim: rgba(20, 16, 12, 0.55);
  --pg-overlay-strip: rgba(20, 16, 12, 0.35);

  --pg-font-display: "Fraunces", Georgia, serif;
  --pg-font-body: "Inter", -apple-system, sans-serif;

  --pg-radius: 4px;
  --pg-gap: 12px;
  --pg-thumb-cross: 96px;      /* thumbnail size along the strip's short axis */
  --pg-main-ratio: 16 / 9;
  --pg-thumb-ratio: 4 / 3;

  /* Indirection: JS writes --pg-main-ratio / --pg-main-ratio-mobile as inline
     styles, which would outrank any media query. The stylesheet instead picks
     which of them is active, so the breakpoint below can still win. */
  --pg-ratio-active: var(--pg-main-ratio);
  --pg-transition: 320ms cubic-bezier(0.22, 1, 0.36, 1);

  /* total strip thickness, derived — do not set directly */
  --pg-strip: calc(var(--pg-thumb-cross) + 2 * var(--pg-gap));

  position: relative;
  display: flow-root; /* BFC: contains the stage's margin instead of collapsing it */
  width: 100%;
  background: var(--pg-bg);
  color: var(--pg-ink);
  font-family: var(--pg-font-body);
  box-sizing: border-box;
}

.pg-gallery *,
.pg-gallery *::before,
.pg-gallery *::after {
  box-sizing: border-box;
}

.pg-gallery img {
  display: block;
  max-width: 100%;
}

/* ---- stage ------------------------------------------------------------- */

.pg-stage {
  position: relative;
  width: auto;                        /* margins shrink it; ratio drives height */
  aspect-ratio: var(--pg-ratio-active);
  background: #000;
  overflow: hidden;
  touch-action: pan-y;
}

/* Exactly two layers, crossfaded. src is swapped on navigation, so the DOM
   holds 2 <img> elements regardless of collection size. */
.pg-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity var(--pg-transition);
}

.pg-layer.is-visible {
  opacity: 1;
}

/* ---- space reservation (non-overlay) ----------------------------------- */

.pg-gallery[data-overlay="false"][data-position="right"]  .pg-stage { margin-right: var(--pg-strip); }
.pg-gallery[data-overlay="false"][data-position="left"]   .pg-stage { margin-left: var(--pg-strip); }
.pg-gallery[data-overlay="false"][data-position="top"]    .pg-stage { margin-top: var(--pg-strip); }
.pg-gallery[data-overlay="false"][data-position="bottom"] .pg-stage { margin-bottom: var(--pg-strip); }

/* ---- thumbnail strip --------------------------------------------------- */

.pg-thumbs {
  position: absolute;
  display: flex;
  align-items: center;
  gap: var(--pg-gap);
  padding: var(--pg-gap);
  background: var(--pg-surface);
  overflow: hidden;
  z-index: 2;
}

.pg-gallery[data-position="right"] .pg-thumbs,
.pg-gallery[data-position="left"] .pg-thumbs {
  top: 0;
  bottom: 0;
  width: var(--pg-strip);
  flex-direction: column;
}

.pg-gallery[data-position="right"] .pg-thumbs { right: 0; }
.pg-gallery[data-position="left"]  .pg-thumbs { left: 0; }

.pg-gallery[data-position="top"] .pg-thumbs,
.pg-gallery[data-position="bottom"] .pg-thumbs {
  left: 0;
  right: 0;
  height: var(--pg-strip);
  flex-direction: row;
}

.pg-gallery[data-position="top"]    .pg-thumbs { top: 0; }
.pg-gallery[data-position="bottom"] .pg-thumbs { bottom: 0; }

.pg-gallery[data-overlay="true"] .pg-thumbs {
  background: var(--pg-overlay-strip);
  backdrop-filter: blur(6px);
}

/* ---- track ------------------------------------------------------------- */

.pg-thumbs-track {
  display: flex;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  gap: var(--pg-gap);
  overflow: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.pg-thumbs-track::-webkit-scrollbar { display: none; }

.pg-gallery[data-position="right"] .pg-thumbs-track,
.pg-gallery[data-position="left"] .pg-thumbs-track {
  flex-direction: column;
  width: 100%;
}

.pg-gallery[data-position="top"] .pg-thumbs-track,
.pg-gallery[data-position="bottom"] .pg-thumbs-track {
  flex-direction: row;
  height: 100%;
}

/* ---- thumbnails -------------------------------------------------------- */

.pg-thumb {
  position: relative;
  flex: 0 0 auto;
  border: none;
  padding: 0;
  margin: 0;
  background: none;
  cursor: pointer;
  border-radius: var(--pg-radius);
  overflow: hidden;
  aspect-ratio: var(--pg-thumb-ratio);
}

.pg-gallery[data-position="right"] .pg-thumb,
.pg-gallery[data-position="left"] .pg-thumb {
  width: 100%;
  height: auto;
}

.pg-gallery[data-position="top"] .pg-thumb,
.pg-gallery[data-position="bottom"] .pg-thumb {
  height: 100%;
  width: auto;
}

.pg-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: brightness(0.72);
  transition: filter 160ms ease;
}

.pg-thumb:hover img,
.pg-thumb:focus-visible img { filter: brightness(0.95); }
.pg-thumb[aria-selected="true"] img { filter: brightness(1); }

.pg-thumb[aria-selected="true"] {
  box-shadow: inset 0 0 0 2px var(--pg-accent);
}

.pg-thumb[aria-selected="true"]::after {
  content: "";
  position: absolute;
  inset: 4px;
  border: 1px solid rgba(245, 241, 234, 0.6);
  pointer-events: none;
}

.pg-thumb:focus-visible {
  outline: 2px solid var(--pg-accent);
  outline-offset: 2px;
}

/* ---- bottom control cluster ------------------------------------------- */
/* [prev] [caption panel] [next] — always horizontally centred at the bottom
   of the stage. With captions off the panel is hidden and the two arrows
   collapse together, still centred. */

.pg-controls {
  position: absolute;
  left: 50%;
  bottom: clamp(16px, 3%, 32px);
  transform: translateX(-50%);
  z-index: 3;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(12px, 2vw, 28px);
  max-width: calc(100% - 32px);
}

/* keep the cluster clear of an overlaid strip */
.pg-gallery[data-overlay="true"][data-position="bottom"] .pg-controls {
  bottom: calc(var(--pg-strip) + clamp(16px, 3%, 32px));
}

.pg-gallery[data-overlay="true"][data-position="left"] .pg-controls,
.pg-gallery[data-overlay="true"][data-position="right"] .pg-controls {
  max-width: calc(100% - (2 * var(--pg-strip)) - 32px);
}

/* scrim for caption legibility; only needed when captions are showing */
.pg-stage::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 55%;
  background: linear-gradient(to top, var(--pg-overlay-scrim) 0%, rgba(20, 16, 12, 0) 100%);
  pointer-events: none;
  opacity: 0;
  transition: opacity 200ms ease;
  z-index: 1;
}

.pg-gallery[data-captions="true"] .pg-stage::after { opacity: 1; }

/* ---- caption panel ----------------------------------------------------- */

.pg-caption-panel {
  min-width: 0;
  max-width: 34rem;
  text-align: center;
  color: #f5f1ea;
}

.pg-gallery[data-captions="false"] .pg-caption-panel { display: none; }

.pg-index {
  font-family: var(--pg-font-display);
  font-style: italic;
  font-size: 13px;
  letter-spacing: 0.04em;
  opacity: 0.85;
  margin: 0 0 6px;
}

.pg-title {
  font-family: var(--pg-font-display);
  font-weight: 500;
  font-size: clamp(20px, 2.6vw, 32px);
  line-height: 1.15;
  margin: 0 0 8px;
}

.pg-caption {
  font-size: 14px;
  line-height: 1.5;
  color: rgba(245, 241, 234, 0.85);
  margin: 0 0 12px;
}

.pg-link {
  display: inline-block;
  font-size: 13px;
  letter-spacing: 0.02em;
  color: var(--pg-ink);
  background: #f5f1ea;
  border-radius: 999px;
  padding: 9px 18px;
  text-decoration: none;
}

/* ---- arrows ------------------------------------------------------------ */

.pg-arrow {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: rgba(20, 16, 12, 0.4);
  backdrop-filter: blur(4px);
  color: #f5f1ea;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  transition: background 160ms ease;
}

.pg-arrow:hover { background: rgba(20, 16, 12, 0.65); }

.pg-arrow:focus-visible {
  outline: 2px solid var(--pg-accent);
  outline-offset: 2px;
}

/* ---- mobile: always a bottom, non-overlay strip ------------------------ */

@media (max-width: 720px) {
  /* falls back to the desktop ratio when no mobile ratio is configured */
  .pg-gallery {
    --pg-ratio-active: var(--pg-main-ratio-mobile, var(--pg-main-ratio));
  }

  .pg-gallery .pg-stage {
    margin: 0 0 var(--pg-strip) 0 !important;
  }

  .pg-gallery .pg-thumbs {
    top: auto !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: auto !important;
    height: var(--pg-strip) !important;
    flex-direction: row !important;
    background: var(--pg-surface) !important;
    backdrop-filter: none !important;
  }

  .pg-gallery .pg-thumbs-track {
    flex-direction: row !important;
    width: auto !important;
    height: 100% !important;
  }

  .pg-gallery .pg-thumb {
    height: 100% !important;
    width: auto !important;
  }

  .pg-gallery .pg-arrow {
    width: 34px;
    height: 34px;
  }

  .pg-gallery .pg-controls {
    bottom: clamp(12px, 3%, 20px) !important;
    max-width: calc(100% - 20px) !important;
    gap: 12px;
  }

  .pg-caption-panel {
    max-width: 100%;
  }
}

/* ---- reduced motion ---------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  .pg-layer { transition: none; }
}
