/* Photo gallery slider (figure.blk-gallery > .blk-gallery__carousel), used in .article-body and in the lifted position
   .c-article__gallery. Native scroll + scroll snap on the track; js/gallery.js adds buttons, counter, enlarge button
   and the thumbnail strip. The old grid markup (.blk-gallery__items) is styled in blocks.css.

   The region is a one-column grid: row 1 is the stage (track, buttons, counter and enlarge button overlap in it),
   row 2 the thumbnail strip. */
@layer components {
  .blk-gallery__carousel {
    --stage-ratio: 4 / 3;
    --strip-h: 4.75rem;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    row-gap: var(--space-2);
    min-width: 0;
  }
  @media (min-width: 30em) {
    .blk-gallery__carousel { --stage-ratio: 3 / 2; }
  }

  /* Layout shift: the strip is added by the script below the stage. Where scripting is on, the strip's row is reserved
     up front (only for 2+ photos), so nothing below the gallery moves when the script runs. */
  @media (scripting: enabled) {
    .blk-gallery:not([data-count="1"]) .blk-gallery__carousel:not(.is-enhanced)::after { content: ""; grid-row: 2; block-size: var(--strip-h); }
  }

  /* Stage: fixed aspect ratio, photos shown whole on the dark background. */
  .blk-gallery__track {
    grid-area: 1 / 1;
    display: flex;
    min-width: 0;
    aspect-ratio: var(--stage-ratio);
    overflow-x: auto;
    /* mandatory: the track always rests on a slide, which the settled-scroll status relies on */
    scroll-snap-type: x mandatory;
    /* x only, so the page still scrolls vertically over the gallery */
    overscroll-behavior-x: contain;
    touch-action: manipulation;
    border-radius: var(--radius-md);
    background: var(--surface-inverse);
  }
  /* Without JS there are no buttons, so the native scrollbar stays as the cue. */
  .blk-gallery__carousel.is-enhanced .blk-gallery__track { scrollbar-width: none; }
  .blk-gallery__track:focus-visible { outline: var(--focus-width) solid var(--focus-on-dark); outline-offset: calc(-1 * var(--focus-width)); }
  @media (prefers-reduced-motion: no-preference) {
    .blk-gallery__track, .blk-gallery__strip { scroll-behavior: smooth; }
  }

  /* Looping: the script keeps --current (index of the current slide) on the track and parks an end slide next to it.
     --children-count is only set by the script where sibling-count() is unsupported.
     Unlike the plain formulas of the carousel guide, these also work for exactly 3 slides: there, the middle slide has
     both ends one step away, and parking both would reverse the sequence (3, 2, 1). With 3 slides the middle slide
     therefore parks nothing: max(count, 4) shifts the first slide's threshold, min(2, count - 2) the last slide's. */
  .blk-gallery__track > :first-child { order: clamp(0, var(--current) - (max(var(--children-count, sibling-count()), 4) - 3), 1); }
  .blk-gallery__track > :last-child { order: calc(-1 * clamp(0, min(2, var(--children-count, sibling-count()) - 2) - var(--current), 1)); }

  .blk-gallery__slide { flex: 0 0 100%; min-width: 0; scroll-snap-align: center; }
  .blk-gallery__slide a { display: block; inline-size: 100%; block-size: 100%; cursor: zoom-in; }
  .blk-gallery__slide a:focus-visible { outline: var(--focus-width) solid var(--focus-on-dark); outline-offset: calc(-1 * var(--focus-width)); }
  .blk-gallery__slide img { display: block; inline-size: 100%; block-size: 100%; object-fit: contain; }

  /* Buttons over the stage edges, before the track in DOM order. Pointer events pass through the container. */
  .blk-gallery__controls { grid-area: 1 / 1; z-index: 2; display: flex; align-items: center; justify-content: space-between; padding-inline: var(--space-2); pointer-events: none; }
  .blk-gallery__btn, .blk-gallery__zoom {
    display: grid; place-items: center; inline-size: var(--tap); block-size: var(--tap); padding: 0;
    border: 2px solid rgba(255, 255, 255, 0.85); border-radius: 50%; background: rgba(36, 26, 19, 0.78); color: var(--on-inverse);
    touch-action: manipulation; pointer-events: auto; cursor: pointer; transition: background-color var(--dur-1);
  }
  .blk-gallery__btn:hover, .blk-gallery__zoom:hover { background: var(--surface-inverse); }
  .blk-gallery__btn:focus-visible, .blk-gallery__zoom:focus-visible { outline: var(--focus-width) solid var(--focus-on-dark); outline-offset: var(--focus-offset); }
  .blk-gallery__btn:disabled { opacity: 0.45; cursor: default; }

  .blk-gallery__zoom { grid-area: 1 / 1; z-index: 2; align-self: start; justify-self: end; margin: var(--space-2); }
  .blk-gallery__counter {
    grid-area: 1 / 1; z-index: 2; align-self: end; justify-self: start; margin: var(--space-2); padding: var(--space-1) var(--space-3);
    border-radius: 999px; background: rgba(36, 26, 19, 0.78); color: var(--on-inverse);
    font-family: var(--font-sans); font-size: var(--fs-sm); line-height: var(--lh-snug); font-variant-numeric: tabular-nums; pointer-events: none;
  }

  /* Thumbnail strip: one 3:2 marker per photo. The active one has a thick ring and full opacity, the others are dimmed
     (not colour alone). Padding leaves room for the ring and the focus outline inside the scroller. */
  .blk-gallery__strip {
    grid-area: 2 / 1; position: relative; display: flex; align-items: center; gap: var(--space-2);
    block-size: var(--strip-h); padding: var(--space-2) var(--space-3);
    overflow-x: auto; overscroll-behavior-x: contain; touch-action: manipulation; scrollbar-width: thin;
  }
  .blk-gallery__thumb {
    flex: none; inline-size: 4.5rem; aspect-ratio: 3 / 2; padding: 0; border: 0; border-radius: var(--radius-sm);
    background: var(--surface-alt); opacity: 0.55; cursor: pointer; touch-action: manipulation;
    transition: opacity var(--dur-2), box-shadow var(--dur-2);
  }
  .blk-gallery__thumb img { display: block; inline-size: 100%; block-size: 100%; object-fit: cover; border-radius: inherit; }
  .blk-gallery__thumb:hover { opacity: 0.85; }
  .blk-gallery__thumb[aria-current="true"] { opacity: 1; box-shadow: 0 0 0 3px var(--accent-strong); }
  .blk-gallery__thumb:focus-visible { outline: var(--focus-width) solid var(--focus); outline-offset: 5px; }
}
