/*
 * https://github.com/morethanwords/tweb
 * Copyright (C) 2019-2021 Eduard Kuzmenko
 * https://github.com/morethanwords/tweb/blob/master/LICENSE
 */

@mixin dialog-badge-transition-reset() {
  transition: none;
  transform: scale(0);
  opacity: 1;
}

@mixin dialog-badge-transition-visible() {
  transform: scale(1);
}

@mixin dialog-badge-transition-backwards() {
  opacity: 0;
}

@mixin dialog-badge-transition-animating() {
  transition: background-color var(--chatlist-badge-transition-in), transform var(--chatlist-badge-transition-in);
}

@mixin dialog-badge-transition-animating-backwards() {
  transition: background-color var(--chatlist-badge-transition-out), transform var(--chatlist-badge-transition-out), opacity var(--chatlist-badge-transition-out);
}

@mixin dialog-badge-transition($after: false) {
  @include animation-level(2) {
    @if $after {
      &:after {
        @include dialog-badge-transition-reset();
      }

      &.is-visible:not(.backwards):after {
        @include dialog-badge-transition-visible();
      }

      &.backwards:after {
        @include dialog-badge-transition-backwards();
      }

      &.animating:after {
        @include dialog-badge-transition-animating();
      }
      
      &.animating.backwards:after {
        @include dialog-badge-transition-animating-backwards();
      }
    } @else {
      @include dialog-badge-transition-reset();
      
      &.is-visible:not(.backwards) {
        @include dialog-badge-transition-visible();
      }

      &.backwards {
        @include dialog-badge-transition-backwards();
      }

      &.animating {
        @include dialog-badge-transition-animating();
        
        &.backwards {
          @include dialog-badge-transition-animating-backwards();
        }
      }
    }
  }
}

.chatlist-container {
  position: relative;

  .search-group-recent.search-group {
    @include respond-to(handhelds) {
      padding: 2px 0 0;
    }
  }

  .search-group {
    width: 100%;
    padding: 1rem 0 .5rem;
    margin-bottom: 17px;

    @include respond-to(handhelds) {
      margin-bottom: 0;
    }

    &__name {
      color: var(--secondary-text-color);
      padding: 0 23px;
      padding-bottom: 1rem;
      font-weight: var(--font-weight-bold);
      user-select: none;

      @include respond-to(handhelds) {
        padding: 5px 9px 0 16px;
        font-size: 15px;
      }
    }

    &-contacts {
      border-bottom: 1px solid var(--border-color);

      @include respond-to(handhelds) {
        padding: 0px 0 2px;
      }

      // .search-group__name {
      //   padding-bottom: 17px;
        
      //   @include respond-to(handhelds) {
      //     padding-bottom: 0;
      //   }
      // }
    }

    &-people.search-group-contacts {
      padding: 5px 0 5px !important;
    }

    &:last-child {
      border-bottom: none;
    }
  }

  .search-super {
    .search-group {
      margin-bottom: 0px;
      padding: 0 0 .5rem;

      &__name {
        padding-top: 1rem;
        display: flex;
        justify-content: space-between;
      }
    }
  }
}

ul.chatlist {
  padding: 0 .5rem/*  .5rem */;

  @include respond-to(handhelds) {
    // padding: 0 0 .5rem;
    padding: 0;
  }
}

.chatlist {
  margin: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  width: 100%;
  background-color: var(--surface-color);

  user-select: none;
  -webkit-user-select: none; /* disable selection/Copy of UIWebView */
  -webkit-touch-callout: none; /* disable the IOS popup when long-press on a link */

  &-chat {
    --background: unset;
    background: var(--background) !important;
    -webkit-user-drag: none;
    // align-items: stretch !important;

    //@include hover-background-effect();
    @include hover(filled-gray, --background, false);

    &.menu-open,
    &.is-forum-open {
      // --background: var(--light-secondary-text-color);
      --background: var(--light-filled-secondary-text-color);
    }

    &.is-muted {
      body:not(.animation-level-0) & {
        &.animating {
          &:not(.backwards) {
            .dialog-muted-icon {
              animation: fade-in-opacity .2s ease-in-out forwards;
            }
          }

          &.backwards .dialog-muted-icon {
            animation: fade-in-backwards-opacity .2s ease-in-out forwards;
          }
        }
      }
    }

    // i {
    //   font-style: normal;
    // }

    .text-highlight {
      color: var(--primary-text-color);
      font-weight: var(--font-weight-bold);
      font-style: normal;
    }

    .premium-icon {
      margin-top: -.125rem;
    }

    .topic-icon {
      flex: 0 0 auto;
      // margin-right: .375rem;
      // display: inline-block;
    }

    .row-title .topic-icon {
      --size: 1.375rem;
      margin-inline-end: .25rem;
    }

    // ! IT IS IMPORTANT, OVERFLOW DOESN'T WORK WITH display: inline-flex;
    .topic-name {
      .topic-icon {
        display: inline;
        line-height: 1 !important;

        // &:before {
        //   content: " ";
          // display: inline-block;
          // width: inherit;
          // height: inherit;
          // min-width: inherit;
          // min-height: inherit;
        // }
  
        &-svg {
          position: relative;
          // top: 0;
          // left: 0;
        }

        &-content {
          left: 0;
          margin-top: -6px;
          position: absolute;
          right: 0;
          top: 0;
        }
      }
    }

    @include respond-to(not-handhelds) {
      &.active {
        --background: var(--primary-color) !important;
        //background: var(--light-secondary-text-color);

        .row-subtitle,
        .row-title,
        .dialog-subtitle-badge-pinned,
        //.user-title:after,
        .user-title,
        .message-status,
        .text-highlight,
        .premium-icon,
        .verified-icon,
        .sending-status-icon,
        .dialog-subtitle-ico {
          color: #fff !important;
        }

        .avatar-stories-svg-circle {
          stroke: #fff !important;
        }

        .badge-fake {
          color: #fff;
          border-color: #fff;
        }

        .primary-text,
        .danger {
          color: #fff !important;
        }

        .dialog-muted-icon {
          color: rgba(255, 255, 255, .7);
        }

        .verified-icon {
          &-background {
            fill: #fff !important;
          }
  
          &-check {
            // fill: var(--primary-color) !important;
            visibility: hidden;
          }
        }

        .dialog-avatar.is-online:after,
        .dialog-group-call-icon {
          background-color: #fff;
          border-color: var(--primary-color);
        }

        .badge.unread, 
        .mention,
        .dialog-subtitle-badge-reaction {
          background-color: #fff !important;
          color: var(--primary-color);
        }

        // .badge.avatar-badge {
        //   border-color: var(--background);
        // }

        .peer-typing-container {
          --color: #fff;
        }
      }
    }
  }

  .peer-typing-container {
    --color: var(--secondary-text-color);
    @include text-overflow(true);

    .peer-typing-text {
      display: inline-flex;
      transform: translateY(-2px);
    }
  }

  .sending-status-icon-premium_lock {
    font-size: .8125rem;
    margin: .1875rem 0 0 .5rem;
    color: var(--secondary-color);
  }

  .dialog-avatar,
  .row-row {
    pointer-events: none;
    position: relative; // for z-index
  }

  .user-title {
    display: flex !important;
    align-items: center;

    .emoji-image {
      vertical-align: top;
      margin-top: 1px;
      width: 18px;
      height: 18px;
    }
  }

  .user-last-message {
    .emoji-image {
      width: 20px;
      height: 20px;
      margin-top: -3px;
    }

    /* span.emoji {
      font-size: 1.2rem;
      margin: 0 .125rem;
      overflow: visible;
      //margin-top: -1.5px;
    } */
  }

  .user-title, 
  .user-last-message {
    flex-grow: 1;
    position: relative; // * for custom emoji
  }

  .message-status {
    margin-inline-end: 0.125rem;
    display: inline-block;
    vertical-align: middle;
    color: var(--chatlist-status-color);
    line-height: 1;
    width: 1.25rem;
    height: 1.25rem;
    font-size: 1.25rem;
    position: relative;
    margin-top: -.0625rem;

    &:before {
      vertical-align: middle;
    }
  }

  .mention {
    padding: 0;
    background-color: var(--chatlist-status-color) !important;

    // html.is-mac & {
    line-height: 1.25rem !important;
    // }
  }

  .mention-badge {
    margin-inline-end: -.125rem;
  }

  .unread, 
  .is-muted.backwards .unread {
    background-color: var(--chatlist-status-color);
  }

  .is-muted .unread,
  .no-unmuted-topic .unread {
    background-color: var(--secondary-color);
  }

  .not-visited {
    --size: .5rem;
    --padding: 0;
    background-color: var(--secondary-color) !important;
  }

  &-parts {
    /* width: 100%;
    min-height: 100%;
    display: flex;
    flex-direction: column; */

    &.with-contacts {
      .chatlist-top:not(.with-placeholder) {
        height: auto;
        // padding-bottom: .5rem;
      }

      .chatlist-top.with-placeholder {
        &.has-contacts {
          height: 24.125rem;
        }
  
        .empty-placeholder-dialogs {
          top: 50%;
        }
      }
    }
  }

  &-top-notification {
    margin: 0 .5rem .5rem;
    // border: 1px solid var(--border-color);
    background-color: var(--light-filled-secondary-text-color);

    &-container {
      height: 0;
      overflow: hidden;

      @include animation-level(2) {
        transition: height var(--transition-standard-in);
      }

      &.is-visible:not(.backwards) {
        height: 4rem;
      }
    }

    .row-icon {
      position: absolute;
      right: .5rem;
      font-size: 1.5rem;
      color: var(--secondary-text-color);
    }
  }

  // do not set position: relative here. will break chatlist slicing.
  &-top {
    // flex: 0 0 auto;
    height: 100%;
  }

  &-bottom {
    // flex: 1 1 auto;
    max-height: 36.375rem;

    .sidebar-left-section {
      padding-bottom: 0;
      margin-bottom: 0 !important;

      /* &-name {
        padding-top: 1rem !important;
        padding-bottom: 1rem !important;
      } */
    }

    .chatlist-new {
      padding: 0;
      // margin-top: -.5rem;
    }
  }
}

.virtual-chatlist {
  position: relative;
  margin: 0 0.5rem !important;
  padding: 0 !important;
  width: auto !important;
}

.dialog {
  &-title {
    &-details {
      font-size: .75rem;
      padding: 1px 0px 0px 0px;
      flex: 0 0 auto;
      margin-inline-start: .5rem !important;
    }
  }

  &-subtitle {
    &-flex {
      display: flex;

      .has-only-pinned-badge:not(.animating) & {
        margin-inline-end: 1.5rem;
      }
    }

    &-span {
      flex: 0 0 auto;
      // flex: 0 1 auto;
      max-width: 100%;

      &-overflow {
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: pre;
      }

      &-last {
        flex: unset;
        max-width: none;
        // flex: 1;
        white-space: nowrap;
      }
    }

    &-badge {
      display: block !important;
      margin-inline-start: .5rem;
      flex: 0 0 auto;
      position: relative;
      z-index: 1;

      &.avatar-badge {
        // --padding: .375rem;
        --size: 1.625rem;
        margin: 0;
        position: absolute;
        right: calc(100% - 4.0625rem);
        top: 2.4375rem;
        // border: 2px solid var(--surface-color);
        border: 2px solid var(--background, var(--surface-color));
        pointer-events: none;
        line-height: 23px !important;
      }

      &-reaction {
        padding: 0;
        background-color: var(--danger-color) !important;
        display: flex !important;
        align-items: center;
        justify-content: center;
        order: 1;
      }

      &-mention {
        order: 2;
      }

      &-unread {
        order: 3;
      }

      &-pinned {
        .has-multiple-badges & {
          margin-inline-start: -1.375rem;
        }

        color: var(--chatlist-pinned-color);
        order: 4;
        z-index: 0;
      }
    }

    &-media {
      width: 1.25rem;
      height: 1.25rem;
      line-height: 1.25rem;
      position: relative;
      flex: 0 0 auto;
      border-radius: .25rem;
      margin-inline-end: 0.375rem;
      display: inline-block;

      &:before {
        content: " ";
        display: inline-block;
        width: inherit;
        height: inherit;
        min-width: inherit;
        min-height: inherit;
      }

      &.is-round {
        border-radius: 50%;

        .dialog-subtitle-media-play {
          font-size: .875rem;
        }
      }

      &-play {
        position: absolute;
        z-index: 1;
        color: #fff;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        line-height: 1;
        font-size: 1rem;
      }

      .media-photo {
        width: inherit;
        height: inherit;
        object-fit: cover;
        border-radius: inherit;
        top: auto;
        bottom: auto;
      }
    }

    &-ico {
      display: inline-block;
      vertical-align: middle;
      width: 20px;
      height: 20px;
      line-height: 18px;
      color: var(--chatlist-pinned-color);

      &-storyreply {
        font-size: 18px;
        margin-inline-end: .125rem;
      }
    }
  }

  &-group-call-icon {
    position: absolute;
    inset-inline-start: 45px;
    top: 45px;
    border: 2px solid var(--surface-color);
    overflow: hidden;
    border-radius: 50%;
    background-color: var(--avatar-online-color);
    width: 20px;
    height: 20px;
    z-index: 2;
  }

  &-subtitle-badge, 
  &-group-call-icon {
    @include dialog-badge-transition();
  }

  &-avatar {
    flex: 0 0 auto;
    @include dialog-badge-transition(true);
  }

  &-muted-icon {
    flex: 0 0 auto;
    color: var(--chatlist-pinned-color);
    font-size: 1.125rem;
    margin-inline-start: .125rem;
  }
}
