.photo-gallery {
  --photo-gallery-gap: 0.75rem;
  --photo-gallery-thumb-ratio: 4 / 3;
  --photo-gallery-thumb-radius: 0.5rem;
  --photo-gallery-border: rgb(128 128 128 / 38%);
  --photo-gallery-surface: transparent;
  --photo-gallery-dialog-surface: Canvas;
  --photo-gallery-dialog-border: currentColor;
  --photo-gallery-focus: Highlight;
  color: inherit;
  font: inherit;
}

.photo-gallery__grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 132px), 1fr));
  gap: var(--photo-gallery-gap);
}

.photo-gallery__thumb {
  display: block;
  width: 100%;
  aspect-ratio: var(--photo-gallery-thumb-ratio);
  overflow: hidden;
  padding: 0;
  border: 1px solid var(--photo-gallery-border);
  border-radius: var(--photo-gallery-thumb-radius);
  background: var(--photo-gallery-surface);
  color: inherit;
  cursor: zoom-in;
}

.photo-gallery__thumb img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.photo-gallery__thumb:hover {
  border-color: var(--photo-gallery-accent, currentColor);
}

.photo-gallery[data-photo-gallery-layout="carousel"] .photo-gallery__grid {
  grid-template-columns: minmax(0, 1fr);
}

.photo-gallery[data-photo-gallery-layout="carousel"] .photo-gallery__thumb {
  width: min(100%, 900px);
  margin-inline: auto;
  aspect-ratio: 16 / 9;
}

.photo-gallery__carousel-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  margin-top: 0.8rem;
}

.photo-gallery__carousel-controls[hidden] {
  display: none;
}

.photo-gallery__carousel-control {
  min-height: 38px;
  padding: 7px 12px;
  border: 1px solid var(--photo-gallery-border);
  border-radius: var(--photo-gallery-control-radius, 0.4rem);
  background: var(--photo-gallery-control-surface, transparent);
  color: inherit;
  font: inherit;
  cursor: pointer;
}

.photo-gallery__carousel-control:hover:not(:disabled) {
  border-color: var(--photo-gallery-accent, currentColor);
}

.photo-gallery__carousel-control:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.photo-gallery__carousel-control:focus-visible {
  outline: 2px solid var(--photo-gallery-focus);
  outline-offset: 3px;
}

.photo-gallery__carousel-count {
  min-width: 84px;
  color: var(--photo-gallery-muted, inherit);
  font-size: 0.8125rem;
  text-align: center;
}

.photo-gallery__thumb:focus-visible,
.photo-gallery__close:focus-visible {
  outline: 2px solid var(--photo-gallery-focus);
  outline-offset: 3px;
}

/* Keep layout overrides off the closed native dialog so its hidden state remains effective. */
.photo-gallery__dialog[open] {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: fixed;
  inset: 0;
  box-sizing: border-box;
  width: fit-content;
  max-width: calc(100vw - 24px);
  max-height: calc(100vh - 24px);
  margin: auto;
  padding: 10px;
  overflow: auto;
  border: 1px solid var(--photo-gallery-dialog-border);
  border-radius: var(--photo-gallery-dialog-radius, 0.75rem);
  background: var(--photo-gallery-dialog-surface);
  color: inherit;
  box-shadow: var(--photo-gallery-dialog-shadow, 0 12px 40px rgb(0 0 0 / 55%));
}

.photo-gallery__dialog::backdrop {
  background: rgb(0 0 0 / 82%);
  backdrop-filter: blur(3px);
}

.photo-gallery__toolbar {
  align-self: stretch;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.25rem;
  margin-bottom: 0.5rem;
}

.photo-gallery__count {
  margin: 0;
  color: var(--photo-gallery-muted, inherit);
  font-size: 0.8125rem;
}

.photo-gallery__close {
  min-height: 36px;
  padding: 6px 12px;
  border: 1px solid var(--photo-gallery-border);
  border-radius: var(--photo-gallery-control-radius, 0.4rem);
  background: var(--photo-gallery-control-surface, transparent);
  color: inherit;
  font: inherit;
  cursor: pointer;
}

.photo-gallery__close:hover {
  border-color: var(--photo-gallery-accent, currentColor);
}

.photo-gallery__image {
  display: block;
  width: auto;
  height: auto;
  max-width: min(calc(100vw - 48px), 1100px);
  max-height: calc(100vh - 92px);
  object-fit: contain;
  margin-inline: auto;
}

@supports (height: 100dvh) {
  .photo-gallery__dialog[open] {
    max-height: calc(100dvh - 24px);
  }

  .photo-gallery__image {
    max-height: calc(100dvh - 92px);
  }
}

@media (max-width: 750px) {
  .photo-gallery {
    --photo-gallery-gap: 0.5rem;
  }

  .photo-gallery__grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .photo-gallery__image {
    max-width: calc(100vw - 44px);
    max-height: calc(100vh - 84px);
  }
}

@media (max-width: 360px) {
  .photo-gallery__grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
