.gallery-page {
  padding-block: 2rem 3rem;
}
.gallery-hero {
  margin-bottom: 1.75rem;
  max-width: 40rem;
}
.gallery-hero h1 {
  font-family: var(--font-display);
  font-size: clamp(1.85rem, 4vw, 2.6rem);
  line-height: 1.1;
  margin: 0.35rem 0 0.75rem;
}
.gallery-lead {
  color: var(--muted);
  margin: 0;
  font-size: 1.05rem;
}

/* Always 3 columns — smaller thumbs on phone + desktop */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.55rem;
}
@media (min-width: 640px) {
  .gallery-grid {
    gap: 0.85rem;
  }
}
@media (min-width: 1024px) {
  .gallery-grid {
    gap: 1rem;
  }
}

.gallery-card {
  border: var(--border);
  border-radius: calc(var(--radius) - 0.5rem);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  text-align: left;
  padding: 0;
  font: inherit;
  color: inherit;
  display: flex;
  flex-direction: column;
  width: 100%;
  min-width: 0;
  position: relative;
}
.gallery-card-open {
  display: flex;
  flex-direction: column;
  width: 100%;
  padding: 0;
  margin: 0;
  border: 0;
  background: transparent;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}
.gallery-card-open:hover,
.gallery-card-open:focus-visible {
  outline: 3px solid var(--paper);
  outline-offset: -3px;
}
.gallery-card .thumb-wrap {
  aspect-ratio: 1;
  background: var(--raised);
  overflow: hidden;
}
.gallery-card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.gallery-card .caption {
  padding: 0.4rem 0.45rem 0.55rem;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1.3;
  color: var(--fg);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
@media (min-width: 640px) {
  .gallery-card .caption {
    padding: 0.55rem 0.65rem 0.7rem;
    font-size: 0.85rem;
  }
  .gallery-card {
    border-radius: var(--radius);
  }
}

.gallery-card-admin {
  display: none;
  gap: 0.35rem;
  padding: 0 0.4rem 0.45rem;
}
.gallery-page.is-admin .gallery-card-admin {
  display: flex;
  flex-wrap: nowrap;
}
.gallery-card-admin button {
  font: inherit;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0.25rem 0.5rem;
  border: var(--border);
  border-radius: 999px;
  background: var(--raised);
  cursor: pointer;
  color: var(--fg);
}
.gallery-card-admin button.danger {
  background: #ffd4c8;
}

.gallery-empty,
.gallery-status {
  margin-top: 1.5rem;
  color: var(--muted);
  font-weight: 700;
}
.gallery-sentinel {
  height: 1px;
}

.gallery-lightbox {
  border: var(--border);
  border-radius: var(--radius);
  padding: 0;
  max-width: min(56rem, 96vw);
  /* Fit image + caption in the viewport (dvh for iOS Safari chrome) */
  max-height: min(96dvh, 96vh);
  width: 100%;
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--fg);
  overflow: hidden;
  box-sizing: border-box;
}
/* Only flex when open — author display must not override closed dialog display:none */
.gallery-lightbox[open],
.gallery-lightbox:modal {
  display: flex;
  flex-direction: column;
}
.gallery-lightbox::backdrop {
  background: rgba(42, 33, 24, 0.55);
}
.lightbox-inner {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  max-height: 100%;
  width: 100%;
  gap: 0;
  overflow: hidden;
}
/* Keep the lightbox stacked at every viewport width: media first, copy below. */
.lightbox-media {
  position: relative;
  background: #1a1510;
  /* Shrink so the caption stays visible below tall portraits */
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  touch-action: pan-y;
  user-select: none;
}
.lightbox-media img {
  /* Definite viewport max so tall photos never overflow+crop in the flex media box */
  max-width: 100%;
  max-height: min(100%, calc(96dvh - 12rem), calc(96vh - 12rem));
  width: auto;
  height: auto;
  object-fit: contain;
  object-position: center;
  display: block;
  pointer-events: auto;
  cursor: zoom-in;
  flex: 0 0 auto;
  min-height: 0;
  min-width: 0;
}
@media (max-width: 767.98px) {
  .lightbox-media img {
    max-height: min(100%, calc(96dvh - 11rem), calc(96vh - 11rem));
  }
}

/* Invisible left/right hit zones over the image */
.lightbox-hit {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 33.333%;
  z-index: 1;
  border: 0;
  padding: 0;
  margin: 0;
  background: transparent;
  cursor: pointer;
}
.lightbox-hit-prev { left: 0; }
.lightbox-hit-next { right: 0; }
.lightbox-hit:focus-visible {
  outline: 3px solid var(--paper);
  outline-offset: -3px;
}

.lightbox-nav-hint {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 1;
  width: 3rem;
  height: 3.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.9);
  font-weight: 700;
  font-size: 3rem;
  line-height: 1;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.9);
  pointer-events: none;
  opacity: 0.88;
}
.lightbox-nav-hint.prev { left: 0.35rem; }
.lightbox-nav-hint.next { right: 0.35rem; }
@media (max-width: 480px) {
  .lightbox-nav-hint {
    width: 2.5rem;
    height: 3.25rem;
    font-size: 2.55rem;
  }
}

.lightbox-copy {
  padding: 1.25rem 1.35rem 1.5rem;
  min-width: 0;
  flex: 0 0 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  /* Keep one-liner + short comment on-screen; scroll only if admin/long text */
  max-height: min(42dvh, 42vh);
}
.lightbox-one {
  font-family: var(--font-display);
  font-size: 1.35rem;
  font-weight: 700;
  margin: 0 0 0.75rem;
}
.lightbox-comment {
  margin: 0;
  white-space: pre-wrap;
  color: var(--muted);
  line-height: 1.5;
}

.lightbox-admin {
  display: grid;
  margin-top: 1.15rem;
  padding-top: 1rem;
  border-top: 2px dashed var(--faint);
  gap: 0.75rem;
}
.lightbox-admin[hidden] {
  display: none !important;
}
.lightbox-admin label {
  display: grid;
  gap: 0.3rem;
  font-weight: 700;
  font-size: 0.9rem;
}
.lightbox-admin input,
.lightbox-admin textarea {
  font: inherit;
  font-weight: 500;
  border: var(--border);
  border-radius: 0.75rem;
  padding: 0.55rem 0.75rem;
  background: var(--canvas);
  color: var(--fg);
  width: 100%;
  box-sizing: border-box;
}
.lightbox-admin textarea {
  min-height: 4.5rem;
  resize: vertical;
}
.lightbox-admin-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.lightbox-admin-actions button {
  font: inherit;
  font-weight: 700;
  border: var(--border);
  border-radius: 999px;
  padding: 0.45rem 0.95rem;
  cursor: pointer;
  background: var(--paper);
  color: var(--on-paper);
}
.lightbox-admin-actions button.danger {
  background: #ffd4c8;
  color: var(--fg);
}
.lightbox-admin-status {
  margin: 0;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--muted);
  min-height: 1.2em;
}

.lightbox-close {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  z-index: 3;
  width: 2.5rem;
  height: 2.5rem;
  border: var(--border);
  border-radius: 999px;
  background: var(--tape);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: var(--tape-shadow);
}

/* Admin shortcut to Image Uploader — only when session-authed */
.gallery-admin-shortcut {
  display: none;
  margin: 0 0 1.25rem;
}
.gallery-page.is-admin .gallery-admin-shortcut {
  display: block;
}
.gallery-admin-shortcut a {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  font-weight: 700;
  font-size: 0.95rem;
  text-decoration: none;
  color: var(--on-paper);
  background: var(--paper);
  border: var(--border);
  border-radius: 999px;
  padding: 0.5rem 1rem;
  box-shadow: var(--shadow-sm);
}
.gallery-admin-shortcut a:hover,
.gallery-admin-shortcut a:focus-visible {
  outline: 3px solid var(--paper);
  outline-offset: 2px;
}

/* Thumb admin: Edit | Del side-by-side on 3-col grid */
.gallery-card-admin {
  flex-wrap: nowrap;
  justify-content: stretch;
  gap: 0.3rem;
  padding: 0 0.35rem 0.4rem;
}
.gallery-card-admin button {
  flex: 1 1 0;
  min-width: 0;
  font-size: 0.65rem;
  padding: 0.22rem 0.35rem;
  text-align: center;
}
@media (min-width: 640px) {
  .gallery-card-admin button {
    font-size: 0.72rem;
    padding: 0.25rem 0.5rem;
  }
}

/* Fullscreen control (distinct from close) */
.lightbox-fullscreen {
  position: absolute;
  top: 0.5rem;
  right: 3.25rem;
  z-index: 3;
  width: 2.5rem;
  height: 2.5rem;
  border: var(--border);
  border-radius: 999px;
  background: var(--tape);
  box-shadow: var(--tape-shadow);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  color: var(--fg);
}
.lightbox-fullscreen svg {
  width: 1.15rem;
  height: 1.15rem;
  display: block;
}
.lightbox-fullscreen:focus-visible {
  outline: 3px solid var(--paper);
  outline-offset: 2px;
}

/* Fullscreen control exit button (hidden until FS) */
.lightbox-fs-exit {
  display: none;
  position: absolute;
  top: max(0.5rem, env(safe-area-inset-top));
  right: max(0.5rem, env(safe-area-inset-right));
  z-index: 5;
  width: 2.5rem;
  height: 2.5rem;
  border: var(--border);
  border-radius: 999px;
  background: var(--tape);
  box-shadow: var(--tape-shadow);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  font-size: 1.35rem;
  line-height: 1;
  padding: 0;
  color: var(--fg);
  pointer-events: auto;
}

/* Body lock while image fullscreen (native or pseudo) */
body.is-lightbox-fs {
  overflow: hidden !important;
  overscroll-behavior: none;
  touch-action: none;
}

/* --- Native Fullscreen API stage --- */
.lightbox-media:fullscreen,
.lightbox-media:-webkit-full-screen {
  background: #000;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  touch-action: none;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}
.lightbox-media:fullscreen img,
.lightbox-media:-webkit-full-screen img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  pointer-events: auto;
  touch-action: none;
  transform-origin: center center;
  will-change: transform;
}
.lightbox-media:fullscreen .lightbox-hit,
.lightbox-media:-webkit-full-screen .lightbox-hit,
.lightbox-media:fullscreen .lightbox-nav-hint,
.lightbox-media:-webkit-full-screen .lightbox-nav-hint {
  display: none !important;
  pointer-events: none !important;
}
.lightbox-media:fullscreen .lightbox-fs-exit,
.lightbox-media:-webkit-full-screen .lightbox-fs-exit {
  display: flex;
}
.lightbox-media:fullscreen .lightbox-fullscreen,
.lightbox-media:-webkit-full-screen .lightbox-fullscreen {
  display: none;
}

/*
 * iOS / no-Fullscreen-API fallback:
 * Expand the already-top-layer <dialog> to the viewport so the stage captures
 * all touches (body-level fixed layers sit UNDER dialog top layer).
 */
.gallery-lightbox.is-pseudo-fullscreen,
.gallery-lightbox.is-pseudo-fullscreen:modal {
  position: fixed !important;
  inset: 0 !important;
  max-width: none !important;
  max-height: none !important;
  width: 100vw !important;
  height: 100vh !important;
  width: 100dvw !important;
  height: 100dvh !important;
  margin: 0 !important;
  padding: 0 !important;
  border: 0 !important;
  border-radius: 0 !important;
  background: #000 !important;
  overflow: hidden !important;
  box-shadow: none !important;
}
.gallery-lightbox.is-pseudo-fullscreen .lightbox-inner {
  flex-direction: column !important;
  height: 100%;
  max-height: none;
  width: 100%;
  overflow: hidden;
}
.gallery-lightbox.is-pseudo-fullscreen .lightbox-copy,
.gallery-lightbox.is-pseudo-fullscreen .lightbox-close {
  display: none !important;
  pointer-events: none !important;
}

.lightbox-media.is-pseudo-fullscreen {
  position: relative !important;
  inset: auto;
  z-index: 1;
  flex: 1 1 auto !important;
  width: 100% !important;
  height: 100% !important;
  max-height: none !important;
  min-height: 0;
  margin: 0;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto !important;
  touch-action: none;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}
.lightbox-media.is-pseudo-fullscreen img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  pointer-events: auto;
  touch-action: none;
  transform-origin: center center;
  will-change: transform;
}
.lightbox-media.is-pseudo-fullscreen .lightbox-hit,
.lightbox-media.is-pseudo-fullscreen .lightbox-nav-hint {
  display: none !important;
  pointer-events: none !important;
}
.lightbox-media.is-pseudo-fullscreen .lightbox-fs-exit {
  display: flex;
  position: fixed;
  top: max(0.5rem, env(safe-area-inset-top));
  right: max(0.5rem, env(safe-area-inset-right));
  z-index: 6;
}
.lightbox-media.is-pseudo-fullscreen .lightbox-fullscreen {
  display: none;
}
