/*
 * Lightbox: full-screen modal dialog on the dark overlay surface (design: galerie-grossansicht-*.png). Counter and close
 * button on top; the photo is inset with rounded corners, the caption sits left-aligned right under it; previous/next
 * are round buttons on the dark area beside the photo (desktop). Swipe is handled by the module.
 */
@layer components {
  .lightbox [hidden] {
    display: none;
  }

  .lightbox {
    --lightbox-inset: var(--space-16);
    margin: 0;
    padding: 0;
    border: 0;
    inline-size: 100vw;
    max-inline-size: 100vw;
    block-size: 100dvh;
    max-block-size: 100dvh;
    background-color: var(--color-background-overlay);
    color: var(--color-text-inverse);
    overflow: hidden;
  }

  .lightbox[open] {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .lightbox::backdrop {
    background-color: var(--color-background-overlay);
  }

  /* No page scroll behind the modal. */
  html:has(.lightbox[open]) {
    overflow: hidden;
  }

  .lightbox__bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-16);
    padding: var(--space-16) var(--lightbox-inset) var(--space-12);
  }

  .lightbox__counter {
    font-weight: var(--font-weight-bold);
    font-size: var(--type-label-m-size);
    line-height: var(--type-label-m-line-height);
    font-variant-numeric: tabular-nums;
  }

  /* Download (only when the gallery allows it) and close sit together on the right. */
  .lightbox__tools {
    display: flex;
    align-items: center;
    gap: var(--space-12);
  }

  .lightbox__close {
    flex: none;
  }

  .lightbox__stage {
    position: relative;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    min-block-size: 0;
    padding-inline: var(--lightbox-inset);
    overflow: hidden;
    touch-action: pan-y pinch-zoom;
    user-select: none;
  }

  .lightbox__figure {
    display: flex;
    flex-direction: column;
    gap: var(--space-12);
    max-inline-size: 100%;
    min-block-size: 0;
  }

  /* Contained, never cropped; the figure shrinks to the photo so the caption lines up with its left edge. */
  .lightbox__img {
    inline-size: auto;
    max-inline-size: 100%;
    block-size: auto;
    max-block-size: calc(100dvh - 11rem);
    margin: 0;
    border-radius: var(--radius-m);
    object-fit: contain;
  }

  .lightbox__caption {
    font-size: var(--type-text-small-size);
    line-height: var(--type-text-small-line-height);
    text-wrap: pretty;
  }

  .lightbox__caption:empty {
    display: none;
  }

  /* Phones: buttons overlay the photo's lower corners so the photo keeps the full width. */
  .lightbox__nav {
    position: absolute;
    inset-block-start: calc(var(--lightbox-inset) + 4rem);
    z-index: var(--z-content);
  }

  .lightbox__nav--prev {
    inset-inline-start: var(--space-24);
  }

  .lightbox__nav--next {
    inset-inline-end: var(--space-24);
  }

  .lightbox :focus-visible {
    outline-color: var(--color-yellow-300);
  }

  /* The nav buttons on phones sit on the photo; they keep a visible ring colour on both. */
  @media (min-width: 1024px) {
    .lightbox {
      --lightbox-inset: 100px;
    }

    .lightbox__bar {
      padding-block: var(--space-24) var(--space-16);
      padding-inline: var(--space-24);
    }

    .lightbox__stage {
      align-items: center;
    }

    .lightbox__img {
      max-block-size: calc(100dvh - 12rem);
    }

    .lightbox__nav {
      inset-block-start: 50%;
      translate: 0 -50%;
    }

    .lightbox__nav--prev {
      inset-inline-start: var(--space-24);
    }

    .lightbox__nav--next {
      inset-inline-end: var(--space-24);
    }
  }
}
