html.dafa-gallery-open,
html.dafa-gallery-open body { overflow: hidden; }

.gallery-viewer-trigger { cursor: zoom-in; }
.gallery-viewer-trigger:focus-visible { outline: 2px solid var(--dafa-accent); outline-offset: 3px; }

.gallery-viewer {
  width: min(1180px, calc(100% - 32px));
  height: min(900px, calc(100dvh - 32px));
  max-width: none;
  max-height: none;
  margin: auto;
  padding: 0;
  border: 1px solid var(--dafa-border);
  border-radius: 12px;
  background: var(--dafa-surface);
  color: var(--dafa-text);
  box-shadow: 0 24px 80px rgb(0 0 0 / 28%);
  overflow: hidden;
}

.gallery-viewer[open] { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; }
.gallery-viewer::backdrop { background: rgb(0 0 0 / 74%); }

.gallery-viewer > header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border-bottom: 1px solid var(--dafa-border);
}

.gallery-viewer__counter {
  color: var(--dafa-text-soft);
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.gallery-viewer__zoom,
.gallery-viewer__navigation { display: flex; align-items: center; }
.gallery-viewer__zoom { gap: 2px; background: var(--dafa-surface-muted); border-radius: 8px; }
.gallery-viewer__zoom button:first-child,
.gallery-viewer__zoom button:last-child { font-size: 21px; }
.gallery-viewer__zoom button:nth-child(2) { min-width: 60px; font-variant-numeric: tabular-nums; }
.gallery-viewer .gallery-viewer__close { justify-self: end; font-size: 24px; }

.gallery-viewer :is(button, a) {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: 0 8px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--dafa-text);
  font: inherit;
  font-size: 14px;
  line-height: 1.2;
  text-decoration: none;
  white-space: nowrap;
  touch-action: manipulation;
  cursor: pointer;
}

@media (hover: hover) {
  .gallery-viewer :is(button:not([aria-disabled="true"]), a):hover { background: var(--dafa-surface-muted); }
  .gallery-viewer__zoom button:not([aria-disabled="true"]):hover { background: var(--dafa-surface-elevated); }
}

.gallery-viewer :is(button, a, .gallery-viewer__stage):focus-visible {
  outline: 2px solid var(--dafa-accent);
  outline-offset: -2px;
}

.gallery-viewer button[aria-disabled="true"] { opacity: .45; cursor: default; }
.gallery-viewer [hidden] { display: none; }

.gallery-viewer__stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  box-sizing: border-box;
  border: 12px solid var(--dafa-bg);
  background: var(--dafa-bg);
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-width: none;
  scroll-behavior: auto;
  touch-action: pan-y pinch-zoom;
}

.gallery-viewer__stage::-webkit-scrollbar { display: none; }
.gallery-viewer__stage.is-zoomed { cursor: grab; touch-action: auto; }
.gallery-viewer__stage.is-dragging { cursor: grabbing; }
.gallery-viewer__canvas { display: flex; width: max-content; height: max-content; min-width: 100%; min-height: 100%; }
.gallery-viewer__canvas img { flex: none; width: 0; height: auto; max-width: none; max-height: none; margin: auto; user-select: none; }
.gallery-viewer__stage p { position: absolute; inset: 0; display: grid; place-items: center; margin: 0; }
.gallery-viewer__stage p,
.gallery-viewer__caption { color: var(--dafa-text-soft); font-size: 14px; text-align: center; }

.gallery-viewer > footer { padding: 4px 14px 6px; border-top: 1px solid var(--dafa-border); }
.gallery-viewer__caption { max-height: 3em; margin: 2px 0 0; overflow: auto; overscroll-behavior: contain; overflow-wrap: anywhere; line-height: 1.5; }
.gallery-viewer__navigation { justify-content: center; gap: 16px; }
.gallery-viewer__navigation a { color: var(--dafa-text-soft); }
.gallery-viewer__navigation button { background: var(--dafa-surface-muted); }

@media (max-width: 640px) {
  .gallery-viewer { width: calc(100% - 16px); height: calc(100dvh - 16px); border-radius: 10px; }
  .gallery-viewer > header, .gallery-viewer > footer { padding-inline: 8px; }
  .gallery-viewer__stage { border-width: 6px; }
}
