@use "../../styles/mixins";

.MessageList {
  container-type: size;
  overflow-x: hidden;
  overflow-y: scroll;
  flex: 1;

  width: 100%;
  margin-bottom: 0.5rem;

  /* stylelint-disable-next-line plugin/no-low-performance-animation-properties */
  transition: bottom 150ms ease-out, transform var(--layer-transition);

  body.no-page-transitions & {
    transition: none !important;
  }

  .mask-image-enabled & {
    mask-image: linear-gradient(to top, transparent 0, #000 0.5rem);
  }

  body.keyboard-visible & {
    position: relative;
    bottom: 0;

    body.keyboard-visible.no-page-transitions & {
      transition: none !important;
    }

    @media (max-width: 600px) {
      bottom: calc(0px - env(safe-area-inset-bottom));
    }
  }

  &.no-avatars .Message > .Avatar {
    display: none;
  }

  &.with-bottom-snap {
    scroll-snap-type: y proximity;
  }

  .messages-container {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;

    width: 100%;
    max-width: var(--messages-container-width);
    min-height: 100%;
    margin: 0 auto;
    padding: 1rem 1rem 0 1.125rem;

    .force-messages-scroll & {
      margin-top: 100vh !important;
    }

    .fab-trigger {
      scroll-snap-align: end;
    }

    .live-tail {
      overflow-anchor: none;
      min-height: max(0rem, calc(100cqh - var(--middle-header-panes-height) - 2.5rem));

      .message-list-item {
        animation: live-tail-message-mount 0.2s ease-out;

        body.no-message-sending-animations & {
          animation: none;
        }
      }
    }

    @media (max-width: 600px) {
      width: 100vw;
      // Patch for an issue on Android when rotating device
      margin: 0;
      padding: 2.75rem 0.5rem 0;
    }
  }

  .first-message-date-group {
    padding-top: var(--middle-header-panes-height);
  }

  &.no-composer {
    margin-bottom: 0;

    .last-in-list {
      margin-bottom: 1rem;
    }
  }

  &.select-mode-active,
  &.type-pinned,
  &.saved-dialog {
    margin-bottom: 0;

    .last-in-list {
      margin-bottom: 5.125rem;

      &.ActionMessage {
        padding-bottom: 0.125rem;
      }

      @media (max-width: 600px) {
        margin-bottom: 4.25rem;
      }
    }

    &:has(.sponsored-media-preview) {
      margin-bottom: 5.125rem;

      .last-in-list {
        margin-bottom: 1rem;
      }
    }
  }

  .Message,
  .ActionMessage {
    position: relative;
    transform: scale(1);
    opacity: 1;
    transition: opacity 0.2s ease, transform 0.2s ease;

    &::before {
      content: "";

      position: absolute;
      z-index: 0;
      top: -0.1875rem;
      right: -50vw;
      bottom: -0.1875rem;
      left: -50vw;

      opacity: 0;
      background: black;

      transition: opacity var(--select-transition);

      body.no-page-transitions & {
        transition: none !important;
      }
    }

    &:not(.open) {
      transform: scale(0.8);
      opacity: 0;
    }

    body.no-message-sending-animations & {
      transform: none;
      opacity: 1;
      transition: none !important;
    }

    &.is-just-added:not(.own) {
      transform: none;
    }

    // Restore stacking context
    // https://developer.mozilla.org/ru/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context
    &.open.shown {
      transform: none;
    }

    &:not(.last-in-group) {
      &::before {
        bottom: -0.1875rem;
      }
    }

    &:not(.first-in-group) {
      &::before {
        top: -0.1875rem;
      }
    }

    &.focused::before {
      // @optimization
      transition: none !important;
    }

    &.focused,
    &.is-forwarding,
    &.is-selected,
    &.has-menu-open {
      &::before {
        opacity: 0.1;

        .theme-dark & {
          opacity: 0.6;
        }
      }
    }
  }

  html.theme-light &.with-default-bg {
    .Message,
    .ActionMessage {
      &::before {
        @include mixins.action-message-bg;
      }

      &.focused,
      &.is-forwarding,
      &.is-selected,
      &.has-menu-open {
        &::before {
          opacity: 0.55;
        }
      }
    }
  }

  .empty {
    display: flex;
    align-items: center;
    justify-content: center;

    width: 100%;
    height: 100%;

    & > span {
      max-width: 80%;
      text-align: left;
    }
  }
  .sticky-date,
  .local-action-message,
  .ActionMessage,
  .empty {

    --custom-emoji-size: calc(var(--message-text-size, 1rem) + 0.125rem);

    user-select: none;
    font-size: calc(var(--message-text-size, 1rem) - 0.0625rem);
    line-height: 1.25;
    text-align: center;

    > span {
      position: relative;
      z-index: 0;
      border-radius: var(--border-radius-messages);
      color: white;

      @include mixins.action-message-bg;

      body.is-ios &,
      body.is-macos & {
        font-size: calc(var(--message-text-size, 1rem) - 0.0625rem);
        line-height: calc(var(--message-text-size, 1rem) + 0.09375rem);
      }

      .emoji-small {
        width: calc(1.25 * var(--message-text-size, 1rem));
        height: calc(1.25 * var(--message-text-size, 1rem));
        vertical-align: text-bottom;
        background-size: calc(1.25 * var(--message-text-size, 1rem));
      }
    }
  }

  .sticky-date,
  .local-action-message,
  .empty {
    font-weight: var(--font-weight-medium);
    & > span {
      display: inline-block;
      padding: 0.1875rem 0.5rem;
      overflow-wrap: anywhere;
    }
  }

  .sticky-date {
    pointer-events: none;

    z-index: var(--z-sticky-date);

    margin-top: 1rem;
    margin-bottom: 1rem;

    opacity: 1;

    transition: opacity 0.3s ease;

    body:not(.is-scrolling-messages) &.stuck {
      opacity: 0;

      span {
        pointer-events: none;
      }
    }

    body.no-page-transitions & {
      transition: none;
    }

    &.interactive {
      cursor: var(--custom-cursor, pointer);
    }

    span {
      pointer-events: auto;
    }

    @media (max-width: 600px) {
      margin-top: 0.5rem;
      margin-bottom: 0.75rem;
    }
  }

  &.scrolled:not(.is-animating) .sticky-date {
    position: sticky;
    top: calc(0.625rem + var(--middle-header-panes-height));
  }

  &.is-animating .message-select-control {
    display: none !important;
  }

  .local-action-message,
  .ActionMessage {
    margin-top: 0.5rem;
    margin-bottom: 0.5rem;
  }

  .sticky-date + .ActionMessage {
    margin-top: -0.375rem;
  }

  .Transition_slide-from > &,
  .Transition_slide-to > & {
    scrollbar-color: transparent transparent !important;

    &::-webkit-scrollbar-thumb {
      background-color: transparent !important;
    }
  }

  .list-bottom-marker.with-sponsored {
    position: relative;
    top: -1rem; // Prevent overlapping with the sponsored message
  }

  @media (pointer: coarse) {
    user-select: none;

    -webkit-touch-callout: none;

    &.select-mode-active {
      user-select: auto;

      -webkit-touch-callout: default;
    }
  }

  @media (max-width: 600px) {
    &.with-bottom-shift {
      margin-bottom: 0;

      .last-in-list {
        margin-bottom: 4.25rem;

        body:not(.keyboard-visible) & {
          margin-bottom: calc(4.25rem + env(safe-area-inset-bottom));
        }
      }
    }
  }

  @media (min-width: 1276px) {
    transform: translate3d(0, 0, 0);
    width: 100%;
    transition: transform var(--layer-transition);

    .messages-container {
      width: calc(100% - var(--right-column-width));
    }

    body.no-right-column-animations & {
      transition: none;
    }

    #Main.right-column-open & {
      transform: translate3d(calc(var(--right-column-width) / -2), 0, 0);
    }

    #Main.narrow-message-list & {
      width: calc(100% - var(--right-column-width));

      .messages-container {
        width: 100%;
      }
    }
  }

  @media (max-width: 600px) {
    transition: transform var(--layer-transition);

    body.enable-symbol-menu-transforms & {
      transform: translate3d(0, 0, 0);
    }

    body.is-symbol-menu-open & {
      transform: translate3d(0, calc(-1 * (var(--symbol-menu-height))), 0);
    }
  }
}

@keyframes live-tail-message-mount {
  from { transform: translateY(2rem); }
  to { transform: translateY(0); }
}
