/* Tiles that are selected by tapping them, and the bar of actions for the selected ones */
.selectable-tile {
  position: relative;
  min-width: 0;

  .media-tile {
    cursor: pointer;
    /* A finger sliding sideways selects instead of scrolling, and one held on a tile doesn't open the browser's own menu
       or lift its picture to drag it away */
    touch-action: pan-y pinch-zoom;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;

    &, img {
      -webkit-user-drag: none;
    }
  }

  input[type="checkbox"] {
    position: absolute;
    top: var(--space-2);
    left: var(--space-2);
    z-index: 1;
  }

  &:has(:checked) .media-tile {
    outline: 4px solid var(--focus-color);
    outline-offset: -4px;

    img {
      opacity: 0.75;
    }
  }

  &:has(input:focus-visible) .media-tile {
    box-shadow: 0 0 var(--focus-glow-size) var(--focus-shadow-color);
  }

  /* Its menu is open (context_menu_controller). The ring fills the gap between tiles without covering them */
  &.menu-open .media-tile {
    outline: 2px solid var(--text-color);
    outline-offset: 2px;
  }

  /* Its item left the grid while the page showed it, such as moved elsewhere on another device (mark_gone). It's no
     longer selected, even if its box still is. Only its picture dims, so the note on why, shown over its bottom while
     it's hovered, doesn't */
  &.gone {
    --tooltip-top: auto;
    --tooltip-bottom: var(--space-2);

    .media-tile {
      outline: none;
      opacity: 0.3;
      filter: grayscale(1);
    }

    .tile-select {
      display: none;
    }
  }

  .meta {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 0 var(--space-2);
    margin: var(--space-1);
  }
}

/* A grid tile's box, lying over it, so a tap ticks it while the grid is selecting. Like the tile, a finger held on it
   doesn't select text, which phones would otherwise do to the nearest text they find */
.tile-select {
  display: none;

  .selecting & {
    position: absolute;
    inset: 0;
    z-index: 1;
    display: block;
    cursor: pointer;
    touch-action: pan-y pinch-zoom;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }
}

/* Select starts selecting and Cancel stops it. While selecting, the top bar says how many are selected in place of the
   title, and its links make way for Select all. Where tiles are always selectable, it's selecting while any are
   selected, and Select all shows from the start */
.selection-area {
  &:not(.selecting) :is(.select-stop, .selection-count, .selection-bar) {
    display: none;
  }

  &:not(.selecting, .always-selectable) .select-all {
    display: none;
  }

  &.selecting :is(.select-start, .top-bar > h1, .top-bar > .actions > :is(.menu-buttons, .phone-menu)) {
    display: none;
  }
}

/* Floats at the bottom of the screen while its grid is in view */
.selection-bar {
  position: sticky;
  bottom: calc(var(--space-3) + var(--bottom-inset));
  z-index: 10;

  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2) var(--space-4);
  width: fit-content;
  max-width: 100%;
  margin: var(--space-4) auto 0;
  padding: var(--space-2) var(--space-3);

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

  /* Only the icons on narrow screens. A bar with few actions has room for the labels of two */
  @media (max-width: 700px) {
    &:not(.few-actions), &:has(> .actions > :nth-child(3)) {
      /* Big enough and far enough apart to tap the right one */
      > .actions > .button, > .actions > .bar-menu > summary, > .actions > .bar-dialog > .button {
        padding: var(--space-2);
        --tooltip-display: block;

        .label {
          display: none;
        }

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

    /* Closer together on small phones, so a bar of five still fits in one row. Their buttons, the bar's padding and
       the screen's margins take about 17rem */
    > .actions {
      gap: var(--space-2) min(var(--space-5), (100vw - 17rem) / 4);
    }
  }

  /* A grid page's bar is in its top bar. On phones it floats at the bottom of the screen, where the nav was */
  .top-bar > & {
    @media (max-width: 799px) {
      position: fixed;
      right: var(--space-4);
      left: var(--space-4);
      margin: 0 auto;
    }

    /* On wider screens it lies in the top bar beside how many are selected, and its panels and tooltips open
       downwards */
    @media (min-width: 800px) {
      container: selection-bar / inline-size;
      --tooltip-top: initial;
      --tooltip-bottom: initial;
      position: static;
      flex: 1;
      justify-content: flex-start;
      margin: 0;
      padding: 0;
      background: none;
      box-shadow: none;

      &.few-actions {
        container-name: few-actions;
      }

      .bar-menu {
        position: relative;
      }

      .bar-menu-panel {
        top: calc(100% + var(--space-2));
        right: auto;
        bottom: auto;
        left: 0;
      }
    }
  }
}

/* Under More on phones instead (selection_menu) */
.selection-bar .button.wide-only {
  @media (max-width: 799px) {
    display: none;
  }
}

/* A label with a name in it, such as "Untag <person>", can't make the bar much wider */
.selection-bar .label {
  max-width: 12em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* In the top bar, the less used actions (rating and those under More on phones) show only their icons, with their
   labels as tooltips, until there's room for every label. Below that, so do the rest. A person's album's bar is the
   widest, at about 1,450px with the longest labels, and 1,000px with the less used ones' icons only */
@container selection-bar (max-width: 1460px) {
  .menu-buttons .button:has(.icon) {
    padding: var(--space-1);
    --tooltip-display: block;

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

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

@container selection-bar (max-width: 1000px) {
  .button:has(.icon) {
    padding: var(--space-1);
    --tooltip-display: block;

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

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

/* A bar with few actions, such as Review's, has room for all their labels in much less, about 29rem with three */
@container few-actions (max-width: 29rem) {
  .button:has(.icon) {
    padding: var(--space-1);
    --tooltip-display: block;

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

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

/* While an action is being sent, the tiles it's for fade, and nothing can be selected or sent again until it's done */
.sending {
  cursor: progress;

  .selectable-tile, .selection-bar {
    pointer-events: none;
  }

  .selectable-tile:has(:checked) {
    opacity: 0.4;
    transition: opacity 0.2s;
  }
}

/* A button of the bar that opens a panel above the bar, with the choices for its action */
.bar-menu summary {
  list-style: none;

  &::-webkit-details-marker {
    display: none;
  }
}

.bar-menu-panel {
  position: absolute;
  right: 0;
  bottom: calc(100% + var(--space-2));
  z-index: 20;

  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-2);
  width: max-content;
  min-width: 14rem;
  max-width: calc(100vw - 2 * var(--space-4));
  max-height: 50svh;
  overflow-y: auto;
  padding: var(--space-3);

  background-color: var(--card-bg-color);
  border-radius: var(--radius-s);
  box-shadow: 0 4px 12px var(--drop-shadow-color);

  .checkbox-list label:last-child {
    margin-bottom: 0;
  }
}
