:root {
  --article-image-max-width: 960px;
  --article-image-max-height: 480px;
}

.markdown-section img {
  width: auto;
  height: auto;
  max-width: min(100%, var(--article-image-max-width));
  max-height: var(--article-image-max-height);
  object-fit: contain;
}

.markdown-section img[data-image-viewer] {
  cursor: zoom-in;
}

.markdown-section img[data-image-viewer]:focus-visible {
  outline: 2px solid var(--theme-color);
  outline-offset: 4px;
}

/* Lock the document while the viewer is open. scrollbar-gutter keeps the
   reserved track when the vertical scrollbar hides so the page behind does
   not jump sideways. */
html.image-viewer-open {
  overflow: hidden;
  overscroll-behavior: none;
  scrollbar-gutter: stable;
}

/* Docsify's viewport-height body would become a separate scroll container
   when html is locked, collapsing the root scroll range back to one screen. */
html.image-viewer-open body {
  overflow: visible;
}

/* No display property here on purpose: the closed <dialog> must keep its
   native display:none until showModal() opens it. */
.image-viewer {
  position: fixed;
  inset: 0;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  width: 100dvw;
  height: 100dvh;
  max-width: none;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

.image-viewer::backdrop {
  background: rgb(0 0 0 / 85%);
}

/* The inset area around the stage belongs to the dialog itself, so taps and
   clicks there always land on the dialog and can close it — even when the
   image is zoomed past the stage bounds and clipped by overflow:hidden. */
.image-viewer-stage {
  position: absolute;
  inset: 64px 16px 16px;
  inset:
    calc(64px + env(safe-area-inset-top, 0px))
    max(calc(16px + env(safe-area-inset-right, 0px)), 4vmin)
    max(calc(16px + env(safe-area-inset-bottom, 0px)), 4vmin)
    max(calc(16px + env(safe-area-inset-left, 0px)), 4vmin);
  overflow: hidden;
  overscroll-behavior: contain;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  cursor: grab;
}

.image-viewer-stage.is-dragging {
  cursor: grabbing;
}

/* JS positions the image exclusively through transform:
   translate3d(x, y, 0) scale(s) in stage coordinates. No transitions or
   max-size limits may interfere with that. */
.image-viewer-stage > img {
  position: absolute;
  top: 0;
  left: 0;
  width: auto;
  height: auto;
  max-width: none;
  max-height: none;
  margin: 0;
  transform-origin: 0 0;
  transition: none;
  -webkit-user-drag: none;
  will-change: transform;
}

/* Two classes beat docsify theme `button` rules (the old white pill look)
   without needing !important. Content is a single "×" set by JS. */
.image-viewer .image-viewer-close {
  position: fixed;
  top: 12px;
  right: 12px;
  top: calc(12px + env(safe-area-inset-top, 0px));
  right: calc(12px + env(safe-area-inset-right, 0px));
  z-index: 1;
  box-sizing: border-box;
  appearance: none;
  -webkit-appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid rgb(255 255 255 / 30%);
  border-radius: 50%;
  background: rgb(0 0 0 / 55%);
  box-shadow: none;
  color: #fff;
  font: inherit;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

@media (hover: hover) {
  .image-viewer .image-viewer-close:hover {
    background: rgb(0 0 0 / 75%);
    border-color: rgb(255 255 255 / 55%);
  }
}

.image-viewer .image-viewer-close:focus-visible {
  outline: 2px solid #fff;
  outline-offset: 2px;
}

.image-viewer .image-viewer-close:active {
  background: rgb(0 0 0 / 80%);
}

@media (pointer: coarse) {
  .image-viewer .image-viewer-close {
    width: 44px;
    height: 44px;
  }
}
