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

.Chat {
  --background-color: var(--color-background);
  --thumbs-background: var(--background-color);

  --z-forum-indicator: 2;
  --z-badge: 4;
  --z-status: 8; // Avatar stories require a higher z-index than the ripple to work

  position: absolute;
  top: 0;
  left: 0;

  width: 100%;
  margin: 0;

  -webkit-touch-callout: none;

  body.is-ios &,
  body.is-macos & {
    --color-text-meta: var(--color-text-meta-apple);
  }

  &.standalone {
    position: static;

    .LastMessageMeta {
      margin-inline-end: 0;
    }
  }

  &.animate-opacity {
    will-change: opacity;
    transition: opacity 0.2s ease-out;
  }

  &.animate-transform {
    will-change: transform;
    transition: transform 0.2s ease-out;
  }

  &:hover,
  &.ListItem.has-menu-open {
    .avatar-badge {
      --_color-outline: var(--color-chat-hover);
    }

    .avatar-badge-wrapper {
      --outline-color: var(--color-chat-hover);
    }

    .ChatCallStatus {
      border-color: var(--color-chat-hover);
    }
  }

  &:not(.standalone):last-of-type {
    border-bottom: 0.5rem solid transparent; // `margin` does not help, and `padding` affects forum indicator height
  }

  &.selected-forum {
    &::before {
      transform: translateX(0) scaleY(1) !important;
    }
  }

  body.is-ios &,
  body.is-android & {
    .ListItem-button {
      border-radius: 0 !important;
    }
  }

  &.forum {
    --color-chat-username: var(--color-text);

    &::before {
      content: '';

      position: absolute;
      z-index: var(--z-forum-indicator);
      top: 0.625rem;
      bottom: 0.625rem;
      left: -0.4375rem; // `.chat-list` left padding
      transform: translateX(-0.375rem) scaleY(0.5);

      width: 0.3125rem;
      border-start-end-radius: var(--border-radius-default);
      border-end-end-radius: var(--border-radius-default);

      background: var(--color-primary);

      transition: transform var(--layer-transition);

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

    .ListItem-button {
      padding-top: 0.3125rem;
      padding-bottom: 0.3125rem;
    }

    .title {
      line-height: 1.375rem;
    }

    .status {
      background-color: var(--background-color);
    }

    @media (max-width: 600px) {
      &::before {
        left: 0;
      }
    }
  }

  .ripple-container {
    z-index: var(--z-chat-ripple);
  }

  .status {
    position: relative;
    z-index: var(--z-status);

    overflow: visible !important;
    display: flex;
    align-items: center;
    align-self: stretch;
  }

  .avatar-badge-wrapper {

    --outline-color: var(--color-background);

    position: absolute;
    z-index: var(--z-badge);
    right: 0.5rem;
    bottom: 0;

    .ChatBadge {
      box-shadow: 0 0 0 2px var(--outline-color);
    }

    .ChatBadge-transition {
      position: relative;
      transition: opacity var(--layer-transition), transform var(--layer-transition);

      body.no-page-transitions & {
        transition: opacity var(--layer-transition);
      }
    }
  }

  .avatar-badge {
    --_color-outline: var(--color-background);

    position: absolute;
    right: 0.0625rem;
    bottom: 0.0625rem;
    flex-shrink: 0;
  }

  .avatar-subscription {
    font-size: 0.875rem;

    @include mixins.filter-outline(1px, var(--_color-outline));
  }

  .avatar-online {
    transform: scale(0);

    width: 0.875rem;
    height: 0.875rem;
    border: 2px solid var(--_color-outline);
    border-radius: 50%;

    opacity: 0.5;
    background-color: #0ac630;

    transition: opacity 200ms, transform 200ms;

    &.avatar-online-shown {
      transform: scale(1);
      opacity: 1;
    }
  }

  .info {
    transition: opacity 300ms ease, transform var(--layer-transition);

    body.no-page-transitions & {
      transition: opacity 300ms ease;
    }

    .title .custom-emoji {
      color: var(--color-primary);
    }

    .icon-muted {
      margin-left: 0.25rem;
      font-size: 1.125rem;
      color: var(--color-list-icon);

      body.is-ios & {
        margin-top: 0;
        margin-right: 0.5rem;
      }
    }

    .general-forum-icon {
      font-size: 1.25rem;
      color: var(--color-text-secondary);
    }

    .LastMessageMeta {
      body.is-ios & {
        margin-right: 0;
        font-size: 0.875rem;
      }
    }

    .last-message,
    .typing-status {
      unicode-bidi: plaintext;
      flex-grow: 1;
      padding-right: 0.25rem;
      color: var(--color-text-secondary);

      &[dir="ltr"] {
        text-align: left;
      }

      .sender-name {
        color: var(--color-chat-username);

        body.is-macos &,
        body.is-ios & {
          color: var(--color-text);
        }
      }

      .draft {
        color: var(--color-error);
      }
    }

    .last-message {
      overflow: hidden;
      display: flex;
      &-summary {
        overflow: hidden;
        flex: unset;

        max-width: none;

        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .draft {
        margin-inline-end: 0.5ch;
      }

      .colon, .chat-prefix-icon {
        margin-inline-end: 0.1875rem;
      }

      .chat-prefix-icon {
        display: inline-block;
        align-self: center;
        color: var(--color-list-icon);
      }

      .media-preview-spoiler {
        filter: blur(1px);
      }

      .media-preview--image {
        flex-shrink: 0;

        width: 1.25rem;
        height: 1.25rem;
        margin-inline-start: 0.125rem;
        margin-inline-end: 0.25rem;
        border-radius: 0.125rem;

        vertical-align: -0.25rem;

        object-fit: cover;

        body.is-ios & {
          width: 1.125rem;
          height: 1.125rem;
          vertical-align: -0.1875rem;
        }

        &.round {
          border-radius: 0.625rem;
        }
      }

      .emoji-small {
        width: 1rem;
        height: 1rem;
        vertical-align: -0.125rem;
      }

      .icon-play {
        position: relative;

        display: inline-block;

        margin-inline-start: -1.25rem;
        margin-inline-end: 0.5rem;

        font-size: 0.75rem;
        color: #fff;

        body.is-ios & {
          bottom: 0;
          margin-inline-start: -1.125rem;
        }
      }
    }

    .star-amount-icon {
      margin-inline: 0;
      line-height: initial;
      vertical-align: text-top;
    }

    &.has-tags {
      line-height: 1.375rem;

      .ChatBadge {
        min-width: 1.375rem;
        height: 1.375rem;
        line-height: 1.5rem;

        .is-ios &,
        .is-macos & {
          line-height: 1.375rem;
        }
      }
    }
  }

  &[dir="rtl"] {
    .info {
      .LastMessageMeta {
        margin-right: auto;
        margin-left: 0;
      }

      .title,
      .subtitle {
        padding-right: 0;
        padding-left: 0.125rem;
      }

      .icon-muted {
        margin-right: 0.25rem;
        margin-left: 0;
      }

      .last-message,
      .typing-status {
        unicode-bidi: plaintext;
        padding-right: 0;
        padding-left: 0.5rem;
        text-align: right;
      }
    }
  }

  // Super specific selector to override the same in `ListItem`
  @media (min-width: 600px) {
    &:not(.has-ripple):not(.is-static),
    body.no-page-transitions & {
      .ListItem-button:active {
        --background-color: var(--color-chat-hover) !important;
      }
    }
  }

  @media (max-width: 600px) {
    &.selected {
      --background-color: var(--color-chat-hover) !important;

      .avatar-badge {
        --_color-outline: var(--color-chat-hover);
      }

      .ChatCallStatus {
        border-color: var(--color-chat-hover);
      }
    }
  }

  @media (min-width: 600px) {
    &.selected:not(.forum),
    &.selected:not(.forum):hover {
      --background-color: var(--color-chat-active) !important;

      .title .custom-emoji {
        color: var(--color-white);
      }

      .VerifiedIcon, .StarIcon {
        --color-fill: #fff;
        --color-checkmark: var(--color-primary);
      }

      .avatar-badge {
        --_color-outline: var(--color-chat-active) !important;
      }

      .avatar-online {
        background-color: var(--color-white);
      }

      .ChatCallStatus {
        border-color: var(--color-chat-active) !important;
      }

      .ListItem-button {
        --background-color: var(--color-chat-active) !important;
        --color-text: var(--color-white);
        --color-text-meta-colored: var(--color-white);
        --color-text-meta: var(--color-white);
        --color-text-secondary: var(--color-white);
        --color-error: var(--color-white);
        --color-list-icon: var(--color-white);
        --color-chat-username: var(--color-white);
      }

      .icon-muted {
        color: var(--color-white) !important;
      }

      .general-forum-icon {
        color: var(--color-white) !important;
      }

      .ChatBadge:not(.pinned):not(.muted) {
        color: var(--color-chat-active);
        background: var(--color-white);
      }

      .ChatBadge:not(.pinned).muted {
        color: var(--color-white);
        background: #FFFFFF33;
      }

      .avatar-badge-wrapper .ChatBadge:not(.pinned) {
        --outline-color: transparent;
      }

      .avatar-badge-wrapper .ChatBadge:not(.pinned).muted {
        background: var(--color-gray);
      }

      .monoforum-badge {
        color: var(--color-chat-active);
        background-color: #fff;
      }

      .ChatTags {
        color: var(--color-chat-active);
        background-color: #ffffffab;
      }
    }
  }

  @media (max-width: 600px) {
    .ListItem-button {
      border-radius: 0 !important;
    }
  }
}
