/* Sorting a place's photos & videos one card at a time. It fills the screen: the bar at the top, the cards, and what's
   known about the one on top and what can be done to it below them, within reach of a thumb. The card on top follows a
   finger, turning as it goes, and the one under it waits a little smaller */
.sorting {
  position: fixed;
  inset: 0;
  z-index: 2000;

  display: grid;
  /* As wide as the screen, however long a name in the bar is */
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);

  background-color: #000;
  color: var(--text-color);
}

/* Nothing scrolls behind it, and the grid it opened over stays where it was scrolled to. Two fingers on a trackpad
   don't go back a page while it's open (viewer_drag_controller) */
:root:has(.sorting) {
  overflow: hidden;
  overscroll-behavior-x: none;
  background-color: #000;
}

/* Where it opens over the grid (sort_overlay). While it loads, there or again with other filters, the screen darkens and
   shows that it's loading */
.sort-overlay[busy] {
  &::before, &::after {
    content: "";
    position: fixed;
    z-index: 2001;
  }

  &::before {
    inset: 0;
    background-color: rgb(0 0 0 / 0.85);
  }

  &::after {
    inset: 0;
    width: 2rem;
    height: 2rem;
    margin: auto;

    background-color: var(--meta-text-color);
    mask: url("/assets/icons/progress_activity-b600a072.svg") center / contain no-repeat;
    animation: spin 0.8s linear infinite;
  }
}

/* Its filter menu opens over the cards */
.sort-bar {
  position: relative;
  z-index: 2;

  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-4);
}

/* A long name or note is cut short instead of making the bar taller */
.sort-heading {
  flex: 1;
  min-width: 0;

  > * {
    margin: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  h1 {
    font-size: 1.125rem;
    line-height: 1.3;
  }
}

/* Which library what's kept from the inbox moves to, which opens the choice of it */
.sort-filing > .button {
  max-width: 11rem;

  > span:not(.icon) {
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

/* The cards and, once they're all sorted, what says so take the same place */
.sort-track, .sort-done {
  grid-row: 2;
  grid-column: 1;
}

/* No wider than a card held in a hand would look, on wide screens. Cards are sized to fit in it */
.sort-track {
  position: relative;
  justify-self: center;
  width: min(100% - 2 * var(--space-3), var(--content-max-width));
  container-type: size;
  /* One finger moves the card. Two zoom in on its photo, not the page (viewer_zoom_controller) */
  touch-action: none;

  &.settling .sort-card[data-current] {
    transition: translate 0.25s ease-out, rotate 0.25s ease-out;
  }
}

/* The frame of a card, as big as the track, with the card in the middle. It turns about a point below the card, as a
   card held at the bottom would, and so does the one waiting under it, so it doesn't move as it comes up */
.sort-card {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;

  scale: 0.94;
  transform-origin: 50% 150%;
  transition: scale 0.25s ease-out;

  /* The one waiting is a different shape, so it only shows as the card on top moves off it, either way */
  &:not([data-current]) {
    opacity: max(var(--drag-fraction, 0) * 4, var(--drag-fraction, 0) * -4);
  }

  &[data-current] {
    z-index: 1;
    scale: 1;
    translate: var(--drag-x, 0px) var(--drag-y, 0px);
    rotate: calc(var(--drag-fraction, 0) * 12deg);
  }

  /* Still loading, as the one before it was swiped away */
  &[busy]:not(:has(.sort-card-content))::after {
    content: "";
    width: 2rem;
    height: 2rem;

    background-color: var(--meta-text-color);
    mask: url("/assets/icons/progress_activity-b600a072.svg") center / contain no-repeat;
    animation: spin 0.8s linear infinite;
  }
}

/* The card is the shape of its photo or video (--ratio), as large as fits, so there's no space beside it */
.sort-card-content {
  position: relative;
  width: min(100cqw, 100cqh * var(--ratio, 3 / 4));
  aspect-ratio: var(--ratio, 3 / 4);
  overflow: hidden;

  background-color: var(--card-bg-color);
  border-radius: var(--radius-m);
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.5);

  .viewer-media {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
    object-fit: contain;
  }
}

/* What letting go does, fading in as the card is dragged that way. The one for the left is on the right, ahead of the
   card, and the other way round */
.sort-stamp {
  position: absolute;
  top: var(--space-5);
  z-index: 2;

  padding: var(--space-1) var(--space-3);
  border: 3px solid currentColor;
  border-radius: var(--radius-s);
  background-color: rgb(0 0 0 / 0.4);

  font-size: 1.75rem;
  font-weight: 800;
  letter-spacing: 0.05em;
  line-height: 1.2;
  text-transform: uppercase;

  opacity: 0;
  pointer-events: none;

  &.sort-stamp-left {
    right: var(--space-5);
    color: var(--danger-color);
    rotate: 12deg;

    .sort-card[data-current] & {
      opacity: calc(var(--drag-fraction, 0) * -4);
    }
  }

  &.sort-stamp-right {
    left: var(--space-5);
    color: var(--keep-color);
    rotate: -12deg;

    .sort-card[data-current] & {
      opacity: calc(var(--drag-fraction, 0) * 4);
    }
  }
}

/* Says the card is a contribution awaiting review, along its top, where cards otherwise have nothing. Clear of a
   video's controls, and under the stamps as it's dragged */
.sort-flag {
  position: absolute;
  top: var(--space-3);
  left: 50%;
  translate: -50% 0;
  z-index: 1;

  max-width: calc(100% - 2 * var(--space-3));
  white-space: nowrap;
  pointer-events: none;

  > span:not(.icon) {
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* Hides while zoomed in, so all of the picture shows */
  .sort-card-content:has(.viewer-media.zoomed) & {
    opacity: 0;
  }
}

/* Over the empty track that shares its place */
.sort-done {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  .sort-undo:disabled {
    display: none;
  }
}

/* As tall while the next card loads as with its controls, so the cards don't change size */
.sort-controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  min-height: 14rem;
  padding: var(--space-3) var(--space-4) var(--space-4);

  /* button_to's */
  form.button_to {
    display: contents;
  }
}

/* As tall for most cards, with a caption, who's in it and when it was taken, so the cards keep their size */
.sort-details {
  align-self: stretch;
  min-height: 3.75rem;
  max-height: 7.5rem;
  overflow-y: auto;
  font-size: var(--fs-small);
  text-align: center;

  :is(p, .caption p) {
    margin: 0;
  }

  .caption {
    font-size: var(--fs-body);
  }
}

.sort-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);

  /* Only their icons until there's room for their labels too, which are their tooltips then */
  @media (max-width: 499px) {
    > form > .button, > .sort-dialog > .button {
      padding: var(--space-1);
      --tooltip-display: block;

      > span:not(.icon) {
        display: none;
      }

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

/* Someone's ticked, to tag them once it's kept */
.sort-people:has(input:checked) > .button {
  border-color: var(--link-color);
  color: var(--link-color);
}

.sort-decisions {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-6);
}

/* Round buttons for what swiping does, in its colour, which fills them darker on hover */
.sort-decision {
  --button-bg: var(--card-bg-color);

  justify-content: center;
  width: 4rem;
  height: 4rem;
  padding: 0;
  border: 2px solid currentColor;
  border-radius: 50%;

  &.sort-decision-left {
    --button-fg: var(--danger-color);
    --button-hover-color: color-mix(in srgb, var(--danger-color) 25%, var(--card-bg-color));
  }

  &.sort-decision-right {
    --button-fg: var(--keep-color);
    --button-hover-color: color-mix(in srgb, var(--keep-color) 25%, var(--card-bg-color));
  }

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