/* THE GALLERY  —  [gallery id="N"]
   ════════════════════════════════════════════════════════════════════════════

   WHY THIS IS ITS OWN FILE, AND NOT PART OF theme.css.

   The gallery editor's Layout and Columns controls ask the owner to choose a
   look, so the editor must preview that look. Drawn in `.media-grid--library`,
   the library's fixed 160px track, its tiles would ignore the settings behind
   `.cms-gallery--grid|masonry` and `--gallery-columns` on the public page:
   Columns set to 3 would render four across in the editor and three on the
   site.

   So the editor's tile area IS the gallery — same classes, same custom
   property, same layout rules — and there is nothing left that can drift.
   That needs ONE set of rules reachable from two surfaces:

     · the public page, through admin/templates/base.php
     · the gallery editor, through admin/admin/templates/galleries/edit.php

   theme.css cannot be the shared file: it opens with `:root` and `body` blocks
   that carry the public palette and page background, and linking it into the
   admin would repaint the admin with them. Copying the rules into admin.css is
   the other option, and it fails in a known way — a second renderer that
   agrees on the day it is written and diverges quietly after.

   Both surfaces reach it with `Asset::site()`, so it is one file on disk with
   no mirror to check and no second copy to forget.

   ── WHAT IT DEPENDS ON ──────────────────────────────────────────────────────
   Tokens only, all of which both surfaces already define: --sp-3, --sp-5,
   --r-none, --surface-2. Nothing here assumes the admin's chrome or the public
   theme's typography, which is what lets one file serve both.

   ── THE TWO LAYOUTS ARE GENUINELY DIFFERENT ─────────────────────────────────
   Which is what makes the Layout control real rather than decorative: grid
   crops to a uniform tile so the rows line up, masonry keeps every image's own
   aspect ratio and lets the columns run ragged.

   `--gallery-columns` arrives inline on the container — from the shortcode on
   the public page, from gallery-images.js on the editor. The fallback of 3
   matches Shortcode::renderGallery()'s own default, so a gallery saved before
   that setting existed still lays out.                                        */

.cms-gallery { margin: var(--sp-5) 0; }

.cms-gallery--grid {
  display: grid;
  grid-template-columns: repeat(var(--gallery-columns, 3), minmax(0, 1fr));
  gap: var(--sp-3);
}
.cms-gallery--grid .cms-gallery__item img {
  width: 100%;
  /* AUTO, NOT 100%. The tile is a grid item stretched to its row, so 100% was
     the whole tile: once an item had a caption (migration 091) its picture took
     the caption's room too and the caption ran out under the tile, where the
     next row painted over it — in the editor and on a page without the
     lightbox (measured). With the 4:3 ratio, auto is the same box it was. */
  height: auto;
  aspect-ratio: 4 / 3;
  object-fit: cover;
}

.cms-gallery--masonry {
  columns: var(--gallery-columns, 3);
  column-gap: var(--sp-3);
}
.cms-gallery--masonry .cms-gallery__item {
  break-inside: avoid;
  margin-bottom: var(--sp-3);
}

.cms-gallery__item img {
  width: 100%;
  border-radius: var(--r-none);
  background: var(--surface-2);
}

/* The lightbox setting wraps each image in a link to the full-size file — that
   is exactly what its label in the gallery editor promises and all it does.
   There is no modal viewer in this package; the link navigates. */
.cms-gallery__item a { display: block; }

/* AN ITEM'S CAPTION (migration 091). Under
   its picture, as words — on the page and in the editor's preview alike,
   so a captioned tile is the same shape in both. IN THE PAGE'S OWN TYPE: a
   size relative to it, its colour, its links underlined — a gallery is drawn
   inside a site's own theme as often as inside this one, and a site theme
   has none of this one's type tokens. That keeps a site's existing look, so
   an update does not restyle its galleries. With the lightbox on the caption
   carries the item's link, so a link inside it is text, not a block. */
.cms-gallery__caption {
  margin-top: var(--sp-2);
  font-size: 0.875em;
  line-height: 1.4;
}
.cms-gallery__caption a { display: inline; text-decoration: underline; }
.cms-gallery__item a:hover img { opacity: 0.9; }

/* Two columns is the widest a gallery reads at this width whatever the owner
   picked, so the setting is clamped rather than overridden — it is a ceiling,
   not an override. Both breakpoints are on the sanctioned four (560 / 720 /
   1080 / 1280). */
@media (max-width: 720px) {
  .cms-gallery--grid {
    grid-template-columns: repeat(min(var(--gallery-columns, 3), 2), minmax(0, 1fr));
  }
  .cms-gallery--masonry { columns: min(var(--gallery-columns, 3), 2); }
}

@media (max-width: 560px) {
  .cms-gallery--grid { grid-template-columns: minmax(0, 1fr); }
  .cms-gallery--masonry { columns: 1; }
}
