:root {
  color-scheme: light dark;
  --focus-color: #3266b4;
  --artwork-size: 400px;
  --gap: 48px;
  --outer-padding: 24px;
  --logo-size: 96px;
  --logo-gap: 40px;
  --control-top: max(var(--outer-padding), env(safe-area-inset-top));
  --control-right: max(var(--outer-padding), env(safe-area-inset-right));
  --control-left: max(var(--outer-padding), env(safe-area-inset-left));
  color: var(--c-fg-1);
  background: var(--c-bg-1);
  /* Reflow must preserve the scroll offset, not anchor a particular artwork. */
  overflow-anchor: none;
  scrollbar-gutter: stable;
}

:root[data-theme='dark'] {
  --focus-color: var(--c-fg-2);
  background-color: var(--grey-100);
}

/* Keep this gallery document-sized and leave smooth scrolling to Back to Top. */
html:focus-within { scroll-behavior: auto; }
body {
  width: auto;
  height: auto;
  text-rendering: auto;
  -webkit-font-smoothing: auto;
  line-height: normal;
}
.image-trigger, .back-to-top { color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
button:focus-visible, a:focus-visible { outline: 3px solid var(--focus-color); outline-offset: 4px; }
[hidden] { display: none !important; }

.logo {
  --logo-foreground: var(--c-fg-2);
  --logo-background: var(--c-bg-2);
  position: fixed;
  z-index: 10;
  top: var(--control-top);
  left: var(--control-left);
  width: var(--logo-size);
  cursor: pointer;
  user-select: none;
}
.logo svg { display: block; width: 100%; height: auto; }

/* COMPOSITION */
.gallery-header {
  display: grid;
  align-items: center;
  min-height: calc(var(--control-top) + var(--logo-size));
  padding: var(--control-top) var(--control-right) 0 calc(var(--control-left) + var(--logo-size) + var(--control-left));
}

.gallery {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-content: start;
  min-height: 100vh;
  min-height: 100dvh;
  gap: var(--gap);
  padding: var(--logo-gap) var(--control-right) max(48px, env(safe-area-inset-bottom)) var(--control-left);
  overflow-anchor: none;
}

/* BLOCKS */
.gallery-title {
  font-family: "Special Gothic Expanded One", sans-serif;
  /* Scale from 2rem at 375px to 3rem at 640px (16px root). */
  font-size: clamp(2rem, calc(0.5849rem + 6.03774vw), 3rem);
  font-weight: 400;
  line-height: 1.1;
  overflow-wrap: anywhere;
}

.artwork {
  aspect-ratio: 1;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  min-width: 0;
}
.image-trigger {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  padding: 0;
  background: transparent;
  cursor: zoom-in;
}
.image-trigger picture { display: block; height: 100%; }
.image-trigger img { display: block; width: 100%; height: 100%; object-fit: contain; object-position: center bottom; }
.embed {
  --embed-ratio: 4 / 3;
  display: block;
  width: min(100%, calc(100% * (var(--embed-ratio))));
  height: auto;
  max-height: 100%;
  aspect-ratio: var(--embed-ratio);
  border: 0;
}

.back-to-top {
  position: fixed;
  z-index: 10;
  right: var(--control-right);
  bottom: max(var(--outer-padding), env(safe-area-inset-bottom));
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: var(--c-bg-2);
  background: color-mix(in srgb, var(--c-bg-2) 50%, transparent);
  box-shadow: 0 2px 12px rgb(0 0 0 / 7%);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.back-to-top svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.back-to-top:hover {
  background: var(--c-bg-3);
  background: color-mix(in srgb, var(--c-bg-3) 50%, transparent);
}

/* Native modal dialog supplies focus containment and makes the gallery inert. */
.viewer, .viewer::backdrop { cursor: zoom-out; }
.viewer-caption a { cursor: pointer; }
.viewer {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  max-width: none;
  max-height: none;
  margin: 0;
  border: 0;
  padding: max(24px, env(safe-area-inset-top)) var(--control-right) max(24px, env(safe-area-inset-bottom)) var(--control-left);
  background: transparent;
  color: inherit;
}
.viewer::backdrop {
  background: var(--c-bg-1);
  background: color-mix(in srgb, var(--c-bg-1) 90%, transparent);
}
.viewer-content { display: flex; flex-direction: column; width: 100%; height: 100%; gap: 16px; pointer-events: none; }
.viewer-stage { flex: 1; min-height: 0; display: flex; justify-content: center; align-items: center; }
.viewer-image { display: block; width: auto; height: auto; max-width: 100%; max-height: 100%; object-fit: contain; pointer-events: auto; }
.viewer-caption { --flow-space: 4px; flex: 0 0 auto; max-height: 30%; overflow: auto; text-align: center; font-size: 14px; line-height: 1.5; pointer-events: auto; overflow-wrap: anywhere; }
.viewer-caption p { overflow-wrap: inherit; }
.viewer-caption a { color: inherit; text-underline-offset: 3px; }

/* EXCEPTIONS */
:root[data-viewer-open] { overflow: hidden; }

@media (min-width: 768px) {
  /* Allow two narrower columns until there is room for two full-size tracks. */
  .gallery { grid-template-columns: repeat(auto-fill, minmax(min(var(--artwork-size), calc((100% - var(--gap)) / 2)), 1fr)); }
}

@media (width < 768px) {
  .artwork { aspect-ratio: auto; }
  .image-trigger,
  .image-trigger picture,
  .image-trigger img { height: auto; }
  .embed { width: 100%; }
}

@media (max-width: 600px) {
  :root {
    --outer-padding: 12px;
    --logo-size: 72px;
    --logo-gap: 32px;
  }
}
