action-text-attachment {
  display: contents;

  img {
    display: initial;

    &.external-inline {
      max-width: 100%;

      &:not([height]) {
        height: auto;
      }
    }
  }
}

.attachment {
  display: inline-block;
  padding: 0;

  a,
  a:hover,
  a:visited:hover {
    color: inherit;
    text-decoration: none;
  }
}

.attachment--preview:not(.attachment--remote) {
  width: 100%;
  text-align: center;
  margin: 0.625em 0;

  img {
    border: 1px solid var(--color-border);
    vertical-align: middle;
    width: auto;
    max-width: calc(100% - 2px);
    max-height: 64rem;
  }
}

.attachment--lightboxable img {
  cursor: zoom-in;
}

.attachment.attachment--remote {
  display: inline;

  .attachment__caption {
    display: none;
  }
}

.attachment--file {
  &.attachment--linked,
  &.attachment--downloadable,
  action-text-attachment & {
    background-color: var(--color-bg--surface-glint);
    border-radius: 10px;
    position: relative;
    width: calc(100% / 4 - 10px);
    border: 0;
    padding: 0 !important;
    margin: 0 6px 6px 0 !important;
    min-width: 150px; /* Uses pixels to avoid font scaling factor breakage */
    max-width: 200px;

    .attachment__name {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      display: block;
    }

    &::before {
      content: "";
      display: block;
      padding-top: 45%;
    }

    &::after {
      background: url("/assets/icons/attachments/text-files-3de18e47.svg") center 1.5em / 25% no-repeat;
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      z-index: 0;
    }

    [data-color-scheme="dark"] & {
      &::after {
        background-image: url("/assets/icons/attachments/text-files-reverse-98788575.svg");
      }
    }

    &.attachment--jpg,
    &.attachment--jpeg,
    &.attachment--png,
    &.attachment--gif,
    &.attachment--psd {
      &::after { background-image: url("/assets/icons/attachments/images-1346058e.svg"); }

      [data-color-scheme="dark"] & {
        &::after { background-image: url("/assets/icons/attachments/images-reverse-6850a029.svg"); }
      }
    }

    &.attachment--pdf {
      &::after { background-image: url("/assets/icons/attachments/pdfs-770c85af.svg"); }

      [data-color-scheme="dark"] & {
        &::after { background-image: url("/assets/icons/attachments/pdfs-reverse-2044372c.svg"); }
      }
    }

    &.attachment--doc,
    &.attachment--docx,
    &.attachment--pages,
    &.attachment--txt,
    &.attachment--odt {
      &::after { background-image: url("/assets/icons/attachments/documents-8d1a51da.svg"); }

      [data-color-scheme="dark"] & {
        &::after { background-image: url("/assets/icons/attachments/documents-reverse-9d480bc8.svg"); }
      }
    }

    &.attachment--csv,
    &.attachment--xls,
    &.attachment--xlsx,
    &.attachment--numbers,
    &.attachment--ods {
      &::after { background-image: url("/assets/icons/attachments/spreadsheets-d281f477.svg"); }

      [data-color-scheme="dark"] & {
        &::after { background-image: url("/assets/icons/attachments/spreadsheets-reverse-b7f87f5a.svg"); }
      }
    }

    &.attachment--ppt,
    &.attachment--pptx,
    &.attachment--keynote,
    &.attachment--odp {
      &::after { background-image: url("/assets/icons/attachments/presentations-2b1f45a9.svg"); }

      [data-color-scheme="dark"] & {
        &::after { background-image: url("/assets/icons/attachments/presentations-reverse-7001fa33.svg"); }
      }
    }

    &.attachment--mp4,
    &.attachment--mov,
    &.attachment--avi,
    &.attachment--mp3,
    &.attachment--m4v {
      &::after { background-image: url("/assets/icons/attachments/media-515be8c6.svg"); }

      [data-color-scheme="dark"] & {
        &::after { background-image: url("/assets/icons/attachments/media-reverse-c78b46f9.svg"); }
      }
    }

    &.attachment--zip,
    &.attachment--rar {
      &::after { background-image: url("/assets/icons/attachments/zip-files-600aa33c.svg"); }

      [data-color-scheme="dark"] & {
        &::after { background-image: url("/assets/icons/attachments/zip-files-reverse-df04b400.svg"); }
      }
    }

    &.attachment--ics {
      &::after { background-image: url("/assets/icons/attachments/calendar-invites-ee74786b.svg"); }

      [data-color-scheme="dark"] & {
        &::after { background-image: url("/assets/icons/attachments/calendar-invites-reverse-9150369e.svg"); }
      }
    }
  }
}

.attachment__caption {
  color: var(--color--txt);
  margin: 0.625em 1.25em;
  text-align: center;
  font-weight: 500;
  font-size: 0.875em;
  line-height: 1.2;

  @media (min-width: 40em) {
    font-size: 0.75em;
    line-height: 1.3;
  }

  .attachment__name,
  .attachment__source {
    display: block;
  }

  .attachment__name {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .attachment__source {
    font-weight: normal;
    max-height: 2.6em;
    padding-bottom: 0.2em;
    overflow-y: hidden;
  }

  .attachment__size {
    color: var(--color-txt--subtle-on-message-content);
    font-weight: normal;
  }

  .attachment--preview & {
    .attachment__name { display: inline; white-space: normal; }
    .attachment__name + .attachment__size::before { content: ' · '; }
  }

  .attachment--file & {
    .attachment__name + .attachment__size::before { content: ''; }
  }
}

.attachment__file-link {
  inset: 0;
  position: absolute;
  text-indent: -9999em;
  z-index: 1;
}

.attachment-gallery {
  display: flex;
  flex-wrap: wrap;
  position: relative;
  gap: 4px;

  .attachment {
    flex: 1 0 33%;
    margin: 0 0 1em;
    max-width: calc(33% - 10px);
  }

  &.attachment-gallery--2,
  &.attachment-gallery--4 {
    .attachment {
      flex-basis: 50%;
      max-width: calc(50% - 10px);
    }
  }
}
