/* Wrap a Markdown list of linked images in .image-gallery (markdown="1").
   Tile placement repeats automatically; no per-image layout classes are needed. */
.rst-content .image-gallery {
  --gallery-gap: 6px;
  container-type: inline-size;
  margin: 1.5rem 0;
}

.rst-content .image-gallery > ul {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-auto-rows: clamp(5rem, 30cqw, 12rem);
  grid-auto-flow: dense;
  gap: var(--gallery-gap);
  margin: 0;
  padding: 0;
  list-style: none;
}

.rst-content .image-gallery > ul > li {
  min-width: 0;
  min-height: 0;
  margin: 0;
  padding: 0;
  list-style: none;
  background: var(--bg-code);
  break-inside: avoid;
}

/* Support both tight Markdown lists and lists with blank lines between items. */
.rst-content .image-gallery > ul > li > p {
  height: 100%;
  margin: 0;
  padding: 0;
}

.rst-content .image-gallery a {
  position: relative;
  display: block;
  height: 100%;
}

.rst-content .image-gallery a:hover,
.rst-content .image-gallery a:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: -3px;
  z-index: 1;
}

.rst-content .image-gallery img {
  display: block;
  width: 100%;
  max-width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Respond to the content column, including when the docs sidebar is open. */
@container (width < 600px) {
  .rst-content .image-gallery li:nth-child(3n + 1) {
    grid-column: span 2;
    grid-row: span 2;
  }

  .rst-content .image-gallery li:nth-child(3n + 2):last-child {
    grid-column: span 2;
  }
}

@container (min-width: 600px) {
  .rst-content .image-gallery > ul {
    grid-template-columns: minmax(0, 1fr) repeat(2, minmax(0, 1.4fr)) minmax(0, 1fr);
    grid-auto-rows: clamp(5rem, 12cqw, 9rem);
  }

  /* Nine tiles fill four rows: a tall photo, a feature, then smaller tiles. */
  .rst-content .image-gallery li:nth-child(9n + 1),
  .rst-content .image-gallery li:nth-child(9n + 2),
  .rst-content .image-gallery li:nth-child(9n + 6),
  .rst-content .image-gallery li:nth-child(9n + 7),
  .rst-content .image-gallery li:nth-child(9n + 8) {
    grid-row: span 2;
  }

  .rst-content .image-gallery li:nth-child(9n + 2) {
    grid-column: span 2;
  }

  /* Finish partial groups without leaving empty cells. */
  .rst-content .image-gallery li:nth-child(9n + 1):last-child,
  .rst-content .image-gallery li:nth-child(9n + 5):last-child {
    grid-column: 1 / -1;
  }

  .rst-content .image-gallery li:nth-child(9n + 2):last-child {
    grid-column: span 3;
  }

  .rst-content .image-gallery li:nth-child(9n + 3):last-child,
  .rst-content .image-gallery li:nth-child(9n + 5):nth-last-child(2),
  .rst-content .image-gallery li:nth-child(9n + 5):nth-last-child(3),
  .rst-content .image-gallery li:nth-child(9n + 5):nth-last-child(4) {
    grid-row: span 2;
  }

  .rst-content .image-gallery li:nth-child(9n + 5):nth-last-child(2),
  .rst-content .image-gallery li:nth-child(9n + 6):last-child,
  .rst-content .image-gallery li:nth-child(9n + 7):last-child {
    grid-column: span 2;
  }
}

/* The viewer sits in the browser's top layer, outside the docs content. */
html.image-gallery-open,
html.image-gallery-open body {
  overflow: hidden;
}

.image-gallery-dialog {
  width: fit-content;
  max-width: calc(100vw - 2rem);
  max-height: calc(100dvh - 2rem);
  margin: auto;
  padding: 3.5rem 0.75rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--fg);
  box-shadow: 0 16px 64px rgba(0, 0, 0, 0.35);
}

.image-gallery-dialog::backdrop {
  background: rgba(0, 0, 0, 0.8);
}

.image-gallery-dialog__image {
  display: block;
  width: min(72rem, calc(100vw - 3.5rem), calc((100dvh - 6.5rem) * var(--gallery-photo-ratio)));
  max-width: 100%;
  height: auto;
  aspect-ratio: var(--gallery-photo-ratio);
  object-fit: contain;
}

.image-gallery-dialog__close {
  position: absolute;
  top: 0.25rem;
  right: 0.25rem;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--fg);
  font: inherit;
  font-size: 2rem;
  line-height: 1;
  cursor: pointer;
}

.image-gallery-dialog__close:hover {
  background: var(--bg-soft);
}

.image-gallery-dialog__status {
  max-width: 30rem;
  margin: 0.75rem 0 0;
  font-size: 0.875rem;
}

@media print {
  html.image-gallery-open,
  html.image-gallery-open body {
    overflow: visible;
  }

  .image-gallery-dialog,
  .image-gallery-dialog::backdrop {
    display: none !important;
  }

  .rst-content .image-gallery > ul {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-auto-rows: auto;
  }

  .rst-content .image-gallery > ul > li:nth-child(n) {
    /* Override every mosaic span, including the more specific final-tile rules. */
    grid-column: auto !important;
    grid-row: auto !important;
  }

  .rst-content .image-gallery img {
    height: auto;
  }

  /* The theme appends link URLs when printing; these are redundant for photos. */
  .rst-content .image-gallery a::after {
    content: none !important;
  }
}
