/* <sg-gallery>: photographs, consent first. Without JavaScript this is the
   quiet look: a grid, hairline frames, and consent-pending slots as drawn
   frames with their captions and the count. Warm adds hand-drawn photo corners
   (drawn by the skin, seeded); playful leans the slots like photos on a table. */

sg-gallery {
  display: grid;
  grid-template-columns: repeat(var(--sg-gallery-cols, 3), minmax(0, 1fr));
  gap: var(--sg-space-4);
  color: var(--sg-text);
  font: var(--sg-step-0) / var(--sg-leading-body) var(--sg-font-body);
}

/* a slot: the photograph, or the drawn frame that stands where it will be */
sg-gallery .sg-gallery-slot {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: var(--sg-space-2);
  margin: 0;
  min-width: 0;
  rotate: 0deg;
  transform-origin: 50% 90%;
}

/* the photograph itself, and the button that opens it */
sg-gallery .sg-gallery-open {
  display: block;
  width: 100%;
  margin: 0;
  padding: 0;
  border: var(--sg-hairline) solid var(--sg-rule-strong);
  background: none;
  color: inherit;
  cursor: zoom-in;
  font: inherit;
  line-height: 0;
}
sg-gallery .sg-gallery-open img {
  display: block;
  width: 100%;
  height: auto;
  object-fit: cover;
}
sg-gallery .sg-gallery-open:focus-visible {
  outline: var(--sg-focus-width) solid var(--sg-focus);
  outline-offset: var(--sg-focus-offset);
}
sg-gallery .sg-gallery-open:hover img { opacity: 0.92; }

sg-gallery figcaption, sg-gallery .sg-gallery-count {
  margin: 0;
  font-size: var(--sg-step--1);
  color: var(--sg-text-soft);
}
sg-gallery figcaption { color: var(--sg-text); }

/* ── consent pending: a drawn frame with photo corners, never the image.
   The frame is decoration (an aria-hidden SVG, drawn by the element and the
   skins); the caption and the count are the real content. The slot is not a
   control: nothing in it takes focus or opens anything. ── */
sg-gallery .sg-gallery-slot[data-consent="pending"] {
  justify-content: center;
  min-height: 11rem;
  padding: var(--sg-space-5) var(--sg-space-4);
  background: var(--sg-surface-sunk);
  border: var(--sg-hairline) solid var(--sg-rule-strong);
}
/* quiet's small photo corners, inked with two rules; warm and playful draw
   theirs by hand in the SVG the element places instead */
sg-gallery:not([data-skin="warm"], [data-skin="playful"])
  .sg-gallery-slot[data-consent="pending"] { border: 0; background: none; }
sg-gallery:not([data-skin="warm"], [data-skin="playful"])
  .sg-gallery-slot[data-consent="pending"]::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  border: var(--sg-hairline) solid var(--sg-rule-strong);
  background:
    linear-gradient(var(--sg-rule-strong) 0 0) top left / 2.2rem var(--sg-hairline),
    linear-gradient(var(--sg-rule-strong) 0 0) top left / var(--sg-hairline) 2.2rem,
    linear-gradient(var(--sg-rule-strong) 0 0) top right / 2.2rem var(--sg-hairline),
    linear-gradient(var(--sg-rule-strong) 0 0) top right / var(--sg-hairline) 2.2rem,
    linear-gradient(var(--sg-rule-strong) 0 0) bottom right / 2.2rem var(--sg-hairline),
    linear-gradient(var(--sg-rule-strong) 0 0) bottom right / var(--sg-hairline) 2.2rem,
    linear-gradient(var(--sg-rule-strong) 0 0) bottom left / 2.2rem var(--sg-hairline),
    linear-gradient(var(--sg-rule-strong) 0 0) bottom left / var(--sg-hairline) 2.2rem,
    var(--sg-surface-sunk);
  background-repeat: no-repeat;
}
/* the drawing is painted by the slot's background layer; keep the words above it */
sg-gallery .sg-gallery-slot[data-consent="pending"] :is(figcaption, .sg-gallery-count) { position: relative; z-index: 1; }
sg-gallery .sg-gallery-slot[data-consent="pending"] .sg-gallery-frame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
  color: var(--sg-text-soft);
}
sg-gallery .sg-gallery-frame path {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.4;
  stroke-linecap: round;
}
/* pending slots sit lower than photographs, so they read as a promise, not a hole */
sg-gallery .sg-gallery-slot[data-consent="pending"] .sg-gallery-count {
  order: 2;
  font-variant-numeric: lining-nums;
}
sg-gallery .sg-gallery-slot[data-consent="pending"] figcaption { order: 1; }

/* the dialog: the photograph large, its caption, and a way out */
sg-gallery .sg-gallery-dialog {
  max-width: min(92vw, 56rem);
  padding: var(--sg-space-4);
  border: var(--sg-hairline) solid var(--sg-rule-strong);
  background: var(--sg-surface-raised);
  color: var(--sg-text);
  font: var(--sg-step-0) / var(--sg-leading-body) var(--sg-font-body);
}
sg-gallery .sg-gallery-dialog::backdrop { background: color-mix(in oklch, var(--sg-shadow) 62%, transparent); }
sg-gallery .sg-gallery-dialog figure { margin: 0; display: grid; gap: var(--sg-space-2); }
sg-gallery .sg-gallery-dialog img { display: block; width: 100%; height: auto; max-height: 72vh; object-fit: contain; }
sg-gallery .sg-gallery-dialog figcaption { color: var(--sg-text-soft); }
sg-gallery .sg-gallery-close {
  margin-top: var(--sg-space-2);
  justify-self: end;
  min-height: 2.5rem;
  padding: 0 var(--sg-space-3);
  border: var(--sg-hairline) solid var(--sg-rule-strong);
  border-radius: var(--sg-radius-2);
  background: transparent;
  color: var(--sg-text);
  font: inherit;
  cursor: pointer;
}
sg-gallery .sg-gallery-close:hover { background: var(--sg-surface-sunk); }
sg-gallery .sg-gallery-close:focus-visible {
  outline: var(--sg-focus-width) solid var(--sg-focus);
  outline-offset: var(--sg-focus-offset);
}

/* playful: a slot lifts a hair under the pointer and settles, once. Nothing
   under reduced motion, and nothing that loops. */
@media (prefers-reduced-motion: no-preference) {
  sg-gallery[data-skin="playful"][data-motion="full"] .sg-gallery-slot {
    rotate: var(--sg-gallery-tilt, 0deg);
    transition: rotate var(--sg-dur-slow) var(--sg-ease-spring), translate var(--sg-dur-slow) var(--sg-ease-spring);
  }
  sg-gallery[data-skin="playful"][data-motion="full"] .sg-gallery-open:hover { translate: 0 -3px; }
}

@media (forced-colors: active) {
  sg-gallery .sg-gallery-open, sg-gallery .sg-gallery-dialog { border: 1px solid CanvasText; }
}

/* ── arrivals: the tala stagger ── An item that has not been seen plays its
   arrival once, on the teental cycle: sixteen beats of --sg-tala-beat, nothing
   landing on the three khali beats (8 to 10). This is talaDelay() in tokens.js
   written in CSS (arrive.test.js keeps the two agreeing); where sibling-index()
   is missing, core/arrive.js hands each item the same delay. Only the first five
   items have a beat of their own (the ones whose beat is inside the 600 ms cap);
   every later item takes the fifth's (520 ms), so a long list cascades top to
   bottom, the tail lands with the fifth, and nothing waits longer than 520 ms. Only arrivals play: arrive.js marks an item
   data-seen when it has, so a sort or a re-append never does. Quiet gets none
   (data-arrive is absent), and neither does reduced motion. */
@keyframes sg-arrive { from { opacity: 0; translate: 0 var(--sg-arrive-rise, 0.5rem); scale: var(--sg-arrive-scale, 1); } }
@media (prefers-reduced-motion: no-preference) {
  sg-gallery[data-arrive] .sg-gallery-slot:not([data-seen]) {
    animation: sg-arrive var(--sg-dur-calm) var(--sg-ease-out) backwards;
    animation-delay: var(--sg-arrive-delay, 0ms);
  }
  sg-gallery[data-arrive="full"] .sg-gallery-slot:not([data-seen]) {
    --sg-arrive-rise: 0.9rem; --sg-arrive-scale: 0.96;
    animation-duration: var(--sg-dur-slow); animation-timing-function: var(--sg-ease-spring);
  }
  @supports (width: calc(sibling-index() * 1px)) {
    sg-gallery[data-arrive] .sg-gallery-slot:not([data-seen]) {
      --sg-arrive-n: calc(sibling-index() - 1);
      --sg-arrive-k: min(var(--sg-arrive-n), 4);
      --sg-arrive-m: mod(var(--sg-arrive-k), 13);
      animation-delay: var(--sg-arrive-delay, calc(var(--sg-tala-beat) * (16 * round(down, var(--sg-arrive-k) / 13, 1) + var(--sg-arrive-m) + 3 * round(down, var(--sg-arrive-m) / 8, 1))));
    }
  }
}

/* ── the touch contract: a press answers within a frame ── No transition on the
   way in, so :active lands on the next frame; the way out eases back in under
   150 ms. Quiet settles 1 px, warm shades the colour toward the ink, playful
   gives a little and is let go on the spring. Reduced motion keeps the colour
   and drops the movement; a key held down does not move anything. */
sg-gallery :is(.sg-gallery-close):not([aria-disabled="true"], :disabled) {
  transition: translate 120ms var(--sg-ease-out), scale 120ms var(--sg-ease-out), background-color 120ms var(--sg-ease-out);
}
sg-gallery[data-skin="playful"] :is(.sg-gallery-close):not([aria-disabled="true"], :disabled) { transition-timing-function: var(--sg-ease-spring); }
sg-gallery :is(.sg-gallery-close) { --press-base: transparent; }
sg-gallery :is(.sg-gallery-close):not([aria-disabled="true"], :disabled):active {
  transition: none; translate: 0 1px;
  background-color: color-mix(in oklab, var(--press-base, transparent), var(--sg-text) 10%);
}
sg-gallery[data-skin="warm"] :is(.sg-gallery-close):not([aria-disabled="true"], :disabled):active { translate: none; background-color: color-mix(in oklab, var(--press-base, transparent), var(--sg-text) 25%); }
sg-gallery[data-skin="playful"] :is(.sg-gallery-close):not([aria-disabled="true"], :disabled):active {
  translate: none; scale: 0.97;
  background-color: color-mix(in oklab, var(--press-base, transparent), var(--sg-text) 14%);
}
@media (prefers-reduced-motion: reduce) {
  sg-gallery :is(.sg-gallery-close):active { translate: none !important; scale: none !important; transition: none; }
}
:root[data-sg-input="key"] sg-gallery :is(.sg-gallery-close):active { translate: none !important; scale: none !important; }

/* a thumbnail is a photograph: pressing it dims the picture, and moves it as the others do */
sg-gallery .sg-gallery-open { transition: translate 120ms var(--sg-ease-out), scale 120ms var(--sg-ease-out); }
sg-gallery .sg-gallery-open img { transition: opacity 120ms var(--sg-ease-out); }
sg-gallery .sg-gallery-open:active { transition: none; translate: 0 1px; }
sg-gallery .sg-gallery-open:active img { transition: none; opacity: 0.84; }
sg-gallery[data-skin="warm"] .sg-gallery-open:active { translate: none; }
sg-gallery[data-skin="warm"] .sg-gallery-open:active img { opacity: 0.74; }
sg-gallery[data-skin="playful"] .sg-gallery-open { transition-timing-function: var(--sg-ease-spring); }
sg-gallery[data-skin="playful"] .sg-gallery-open:active { translate: none; scale: 0.97; }
/* playful's hover lifts the photograph 3 px; a press must beat it, so it carries the same selectors and comes later */
@media (prefers-reduced-motion: no-preference) {
  sg-gallery[data-skin="playful"][data-motion="full"] .sg-gallery-open:active { translate: none; scale: 0.97; }
}
@media (prefers-reduced-motion: reduce) {
  sg-gallery .sg-gallery-open:active { translate: none !important; scale: none !important; }
}
:root[data-sg-input="key"] sg-gallery .sg-gallery-open:active { translate: none !important; scale: none !important; }
