/* Photos & videos filling the screen, one at a time, with the ones before and after waiting beside it. Its controls
   go over the top while they're shown */
.viewer {
  position: fixed;
  inset: 0;
  /* Over the header */
  z-index: 2000;
  overflow: hidden;

  /* Fades as the photo is pulled down to close it */
  background-color: rgb(0 0 0 / calc(1 - var(--pull, 0)));
  color: var(--text-color);
  /* One finger moves through the photos and pulls them away. Two zoom in on the photo, not the page
     (viewer_zoom_controller) */
  touch-action: none;

  &:not(.controls-shown) {
    cursor: none;

    .viewer-media {
      cursor: none;
    }
  }

  /* Hidden controls. Zoomed in, a contribution's banner hides too, so all of the picture shows */
  &:not(.controls-shown) :is(.viewer-controls, .viewer-step, .viewer-details, .viewer-review:not(.decidable)),
  &:has(.viewer-media.zoomed) .viewer-review {
    opacity: 0;

    &, * {
      pointer-events: none;
    }
  }

  /* It takes the focus when it opens, so keys go to it */
  &:focus {
    outline: none;
  }

  &.settling {
    transition: background-color 0.25s ease-out;

    .viewer-track, .viewer-slide[data-current] .viewer-stage {
      transition: transform 0.25s ease-out;
    }
  }
}

/* Nothing scrolls behind it. The scrollbar's space stays, so the page doesn't change width, but it's black too. Two
   fingers on a trackpad don't go back a page while it's open (viewer_drag_controller) */
:root.viewing {
  overflow: hidden;
  overscroll-behavior-x: none;
  background-color: #000;
}

/* All the slides move together, following a finger */
.viewer-track {
  position: absolute;
  inset: 0;
  transform: translateX(var(--drag-x, 0px));
}

.viewer-slide {
  position: absolute;
  inset: 0;
  display: block;
  transform: translateX(calc(var(--slot, 0) * (100% + 16px)));
}

.viewer-stage {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);

  .viewer-slide[data-current] & {
    transform: translateY(var(--drag-y, 0px)) scale(calc(1 - var(--pull, 0) * 0.4));
  }
}

.viewer-media {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  /* Shows where the picture goes while it's still loading */
  background-color: rgb(255 255 255 / 0.06);
  /* No menu for saving the picture from a held finger, since it would save this smaller copy */
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;

  /* Zoomed in, in the viewer or on a card while sorting (lib/zoom). A mouse moves it around by dragging it */
  &:is(img) {
    transform: translate(var(--zoom-x, 0px), var(--zoom-y, 0px)) scale(var(--zoom, 1));

    &.zoomed {
      cursor: grab;
    }

    &.grabbed {
      cursor: grabbing;
    }

    .settling [data-current] & {
      transition: transform 0.25s ease-out;
    }
  }

  /* A video's own size is often smaller than the screen */
  &:is(video) {
    width: 100%;
    height: 100%;
  }
}

.viewer-missing {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  color: var(--meta-text-color);
}

.viewer-controls {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;

  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: calc(var(--space-3) + env(safe-area-inset-top)) calc(var(--space-4) + env(safe-area-inset-right))
    var(--space-6) calc(var(--space-4) + env(safe-area-inset-left));

  background: linear-gradient(rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0.5) 70%, transparent);
  transition: opacity 0.2s ease-out;
}

/* A touch screen swipes instead. Hidden as a group, as the buttons' own display is set by .button */
.viewer-steps {
  display: contents;

  @media (max-width: 799px) {
    display: none;
  }
}

/* Moves to the item before or after. Near the screen's edges, as the picture's edges move with its shape */
.viewer-step {
  position: absolute;
  top: 50%;
  translate: 0 -50%;

  transition: opacity 0.2s ease-out,
              transform 0.2s ease-out,
              box-shadow 0.2s ease-out,
              background-color 0.2s ease-out;

  /* Bigger than the bar's buttons, to stand out from them */
  &.icon-only {
    padding: var(--space-2);

    .icon svg {
      width: 2rem;
      height: 2rem;
    }
  }

  &.previous {
    left: calc(var(--space-4) + env(safe-area-inset-left));
  }

  &.next {
    right: calc(var(--space-4) + env(safe-area-inset-right));
  }
}

.viewer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-2) var(--space-4);
}

/* Deciding on a contribution, near the top of the picture as on a card while sorting, but clear of the bar. Who can
   decide on it sees it while the other controls are hidden too. Taps beside it go to the picture */
.viewer-review {
  --below-bar: calc(env(safe-area-inset-top) + 4.25rem);

  position: absolute;
  top: var(--below-bar);
  right: 0;
  left: 0;

  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  padding: 0 var(--space-4);

  text-align: center;
  pointer-events: none;
  transition: opacity 0.2s ease-out;

  > * {
    pointer-events: auto;
  }

  /* Centered on the picture, even when it's narrower than the screen */
  @supports (anchor-name: --viewer-media) {
    position-anchor: --viewer-media;
    top: max(var(--below-bar), anchor(top, 0px) + var(--space-3));
    right: anchor(right, 0px);
    left: anchor(left, 0px);
  }

  .actions {
    justify-content: center;
  }
}

.viewer-slide[data-current] .viewer-media {
  anchor-name: --viewer-media;
}

/* What's known about the item, along the bottom of the screen, where phones mostly have room. Over a video, it's above
   the video's own controls, and taps beside it go through to them */
.viewer-details {
  --clearance: 0px;

  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;

  padding: var(--space-6) calc(var(--space-4) + env(safe-area-inset-right))
    calc(var(--space-4) + env(safe-area-inset-bottom) + var(--clearance)) calc(var(--space-4) + env(safe-area-inset-left));

  background: linear-gradient(transparent, rgba(0, 0, 0, 0.5) 30%, rgba(0, 0, 0, 0.75));
  pointer-events: none;
  transition: opacity 0.2s ease-out;

  &.over-video {
    --clearance: 4.5rem;
  }

  > * {
    pointer-events: auto;
  }

  .meta {
    margin: 0;
  }

  /* A long one scrolls, instead of covering the picture */
  .caption {
    max-height: 30svh;
    overflow-y: auto;

    p {
      margin: 0 0 var(--space-1);
    }
  }
}

/* Looks like the caption it edits, so it's edited where it shows (captions/_caption) */
.caption-form {
  margin: 0 0 var(--space-1);

  textarea {
    display: block;
    min-height: 1lh;
    padding: 0;
    border: none;
    border-radius: 0;
    background: none;
    color: inherit;
    line-height: inherit;
    text-align: inherit;
    resize: none;
    field-sizing: content;

    &:focus {
      box-shadow: none;
    }

    &::placeholder {
      color: var(--meta-text-color);
    }
  }
}

::view-transition-group(viewer-media) {
  animation-duration: 0.3s;
}
