/* ── Justified rows ─────────────────────────────────────────────────── */

.gallery {
  --row: 220px;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 6px;
}

.gallery-empty {
  font-family: var(--mono);
  font-size: 13px;
  color: var(--muted);
}

.gallery-empty .prompt {
  color: var(--accent);
  margin-right: 0.4ch;
}

/* ── Toolbar ────────────────────────────────────────────────────────── */

.gallery-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5em 1em;
  margin: 0 0 0.8em;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.01em;
}

.gallery-bar .sep {
  color: var(--rule);
  margin: 0 0.4ch;
}

.gallery-filters {
  display: flex;
  gap: 0.15em;
}

.gallery-filters button {
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.2;
  color: var(--fg);
  background-color: transparent;
  padding: 0.2em 0.7em;
  border: 1px solid transparent;
  border-radius: 2px;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.gallery-filters button:hover {
  background-color: var(--accent-bg-soft);
  border-color: var(--rule);
}

.gallery-filters button.current {
  background-color: var(--accent-bg);
  border-color: #c0d4a8;
}

/* ── Tiles ──────────────────────────────────────────────────────────── */

/* grow and basis are both proportional to --r, so every tile in a row
   resolves to the same height with no cropping */
.tile {
  position: relative;
  display: block;
  min-width: 0;
  flex-grow: var(--r);
  flex-shrink: 1;
  flex-basis: calc(var(--r) * var(--row-m, var(--row)));
  aspect-ratio: var(--r);
  overflow: hidden;
  cursor: pointer;
  text-decoration: none;
  background-color: var(--warm-bg);
  box-shadow: inset 0 0 0 1px var(--rule);
}

.tile[hidden] {
  display: none;
}

/* last row fills small gaps but a lone tile stays near its natural size */
.tile.tail {
  max-width: calc(var(--r) * var(--row-m, var(--row)) * 1.4);
}

.tile:hover {
  text-decoration: none;
}

.tile img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.tile img.loaded {
  opacity: 1;
}

.tile-tag {
  position: absolute;
  left: 4px;
  bottom: 4px;
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1;
  padding: 0.25em 0.45em;
  border-radius: 2px;
  color: var(--bg);
  background-color: rgba(26, 26, 26, 0.75);
  pointer-events: none;
}

.tile-num {
  position: absolute;
  top: 4px;
  right: 4px;
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1;
  padding: 0.25em 0.45em;
  border-radius: 2px;
  color: var(--muted);
  background-color: var(--bg);
  opacity: 0;
  transition: opacity 0.15s ease;
  pointer-events: none;
}

@media (hover: hover) {
  .tile:hover {
    box-shadow: inset 0 0 0 1px var(--rule), 0 0 0 1px var(--warm-border);
  }

  .tile:hover .tile-num {
    opacity: 1;
  }
}

@media (max-width: 600px) {
  .gallery {
    --row-m: 150px;
  }
}

/* ── Lightbox ───────────────────────────────────────────────────────── */

html:has(dialog[open]) {
  overflow: hidden;
}

.lightbox {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  box-sizing: border-box;
  overflow: hidden;
  color: var(--fg);
  background-color: transparent;
}

.lightbox[open] {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  opacity: 1;
  transition: opacity 0.2s ease;
}

@starting-style {
  .lightbox[open] {
    opacity: 0;
  }
}

.lightbox::backdrop {
  background-color: rgba(251, 250, 245, 0.96);
}

.lb-figure {
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  max-width: 100%;
}

.lb-figure img,
.lb-figure video {
  display: block;
  width: auto;
  height: auto;
  max-width: min(92vw, 1400px);
  max-height: calc(100vh - 7em);
  max-height: calc(100dvh - 7em);
  object-fit: contain;
  box-shadow: 0 0 0 1px var(--rule);
}

.lb-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 1em;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  letter-spacing: 0.01em;
}

.lb-bar .prompt {
  color: var(--accent);
  margin-right: 0.4ch;
}

.lb-bar .sep {
  color: var(--rule);
  margin: 0 0.4ch;
}

.lb-close {
  position: absolute;
  top: 1em;
  right: 1em;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.2;
  color: var(--muted);
  background-color: var(--bg);
  border: 1px solid var(--rule);
  border-radius: 2px;
  padding: 0.2em 0.6em;
  cursor: pointer;
}

.lb-prev,
.lb-next {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  font-family: var(--mono);
  font-size: 28px;
  line-height: 1;
  color: var(--muted);
  background-color: transparent;
  border: 0;
  cursor: pointer;
}

.lb-prev {
  left: 1em;
}

.lb-next {
  right: 1em;
}

.lb-close:hover,
.lb-prev:hover,
.lb-next:hover {
  color: var(--fg);
}

/* arrows drop into the bottom bar row so they never cover the media */
@media (max-width: 600px) {
  .lightbox[open] {
    display: grid;
    grid-template-columns: 44px 1fr 44px;
    grid-template-rows: 1fr auto;
    align-items: center;
    justify-items: center;
    padding-bottom: 0.6em;
  }

  .lb-figure {
    grid-area: 1 / 1 / 2 / 4;
    min-height: 0;
  }

  .lb-prev,
  .lb-next {
    position: static;
    transform: none;
  }

  .lb-prev {
    grid-area: 2 / 1 / 3 / 2;
  }

  .lb-bar {
    grid-area: 2 / 2 / 3 / 3;
    margin-top: 0;
  }

  .lb-next {
    grid-area: 2 / 3 / 3 / 4;
  }
}
