/* Square thumbnails filling the row. Three across on phones, more as the screen widens */
.media-grid {
  --tile-min-size: 110px;

  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--tile-min-size), 1fr));
  gap: var(--space-1);
  margin: var(--space-4) 0;

  @media (min-width: 600px) {
    --tile-min-size: 160px;
  }

  /* On wider screens, as many in a row as the size slider says (grid_size_controller) */
  @media (min-width: 800px) {
    :root[data-grid-columns] & {
      grid-template-columns: repeat(var(--grid-columns), minmax(0, 1fr));
    }
  }

  /* Each lazily loaded page frame lays its tiles out in the parent grid */
  .media-page {
    display: contents;

    /* Until then it needs a box to be seen scrolling into view. It reaches up over the last rows, out of the way, so
       the next page starts loading before the end is reached. Only while it's empty, since Turbo marks it complete a
       few frames after its tiles are in */
    &:empty {
      display: block;
      grid-column: 1 / -1;
      height: 100vh;
      margin-top: -100vh;
      pointer-events: none;
    }
  }
}

/* Thumbnails are square wherever they're shown */
.media-tile img {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1;
  object-fit: cover;
  background-color: var(--card-bg-color);
}

/* A thumbnail that fails to load shows an icon on a plain square while it's tried again (thumb_controller), wherever
   it's shown */
img.thumb {
  overflow: hidden;

  /* Browsers other than Safari draw this only in a broken image. It pushes the broken image's icon and text out of
     sight, before the controller swaps it for a blank one */
  &::before {
    content: "";
    display: block;
    height: 100%;
  }

  &.thumb-failed {
    background-color: var(--card-bg-color);
  }
}

.media-tile {
  position: relative;
  display: block;
  margin: 0;
  overflow: hidden;
  border-radius: var(--radius-s);

  .tile-badges {
    position: absolute;
    right: var(--space-1);
    bottom: var(--space-1);
    left: var(--space-1);
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: var(--space-1);
  }

  .item-badges {
    display: contents;
  }

  .flag, .duration, .tile-rating {
    padding: 0 var(--space-1);
    border-radius: var(--radius-s);
    background-color: rgba(0, 0, 0, 0.6);
    color: #fff;
    font-size: var(--fs-small);
  }

  /* How many stars it's rated, at the top right. Nothing while it's unrated */
  .tile-rating {
    position: absolute;
    top: var(--space-1);
    right: var(--space-1);
    display: flex;
    align-items: center;
    gap: 2px;
    font-weight: 600;

    &:empty {
      display: none;
    }

    .icon svg {
      width: 0.875rem;
      height: 0.875rem;
      fill: var(--star-color);
    }
  }

  /* New to the user, apart from the other badges at the left */
  .new-flag {
    margin-right: auto;
  }

  &:focus-visible {
    box-shadow: 0 0 var(--focus-glow-size) var(--focus-shadow-color);
  }
}

/* An item in Recently Deleted, which only opens a menu to restore it */
.trashed-item {
  /* How long it has left, over the bottom of its thumbnail. The shade keeps it readable on light pictures */
  .days-left {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    padding: var(--space-5) var(--space-2) var(--space-1);

    background: linear-gradient(transparent, rgba(0, 0, 0, 0.65));
    color: #fff;
    font-size: var(--fs-small);
    pointer-events: none;
  }
}
