$avatar-icon-size: 2rem;
$avatar-m-top: 0.5rem;
$avatar-m-ratio: 2;
$avatar-m-left: $avatar-m-top * $avatar-m-ratio;

$system-note-icon-size: 1.5rem;
$system-note-svg-size: 1rem;
$icon-size-diff: $avatar-icon-size - $system-note-icon-size;

$system-note-icon-m-top: $avatar-m-top + $icon-size-diff - 1.3rem;
$system-note-icon-m-left: $avatar-m-left + $icon-size-diff / $avatar-m-ratio;

@mixin vertical-line($left) {
  &::before {
    content: '';
    border-left: 2px solid var(--gray-50, $gray-50);
    position: absolute;
    top: 16px;
    bottom: 0;
    left: calc(#{$left} - 1px);
    height: calc(100% + 20px);
  }
}

@mixin outline-comment() {
  margin: $gl-padding $gl-padding 0;
  border: 1px solid $border-color;
  border-radius: $border-radius-default;
}

.issuable-discussion:not(.incident-timeline-events),
.limited-width-notes {
  .main-notes-list::before,
  .timeline-entry:last-child::before {
    content: '';
    position: absolute;
    width: 2px;
    left: 15px;
    top: 15px;
    height: calc(100% - 15px);
  }

  .main-notes-list::before {
    background: var(--gray-50, $gray-50);
  }

  .timeline-entry:last-child::before {
    background: var(--white);

    .gl-dark & {
      background: var(--gray-10);
    }

    &.note-comment {
      top: 30px;
    }
  }
}

.notes {
  display: block;
  list-style: none;
  margin: 0;
  padding: 0;
  position: relative;

  &.timeline > .timeline-entry {
    margin: $gl-padding 0;

    &.system-note {
      margin-top: $gl-spacing-scale-1;
      margin-bottom: 0;

      .note-header-info {
        padding-left: $gl-spacing-scale-4;
      }
    }

    &.system-note,
    &.note-form {
      border: 0;
    }

    .timeline-avatar {
      height: 2rem;
    }

    .gl-avatar {
      border-color: var(--gray-50, $gray-50);
    }

    &.note-comment,
    &.note-skeleton,
    .draft-note {
      .timeline-avatar {
        margin-top: 5px;
      }

      .timeline-content:not(.flash-container) {
        margin-left: 2.5rem;
        border: 1px solid $border-color;
        border-radius: $gl-border-radius-base;
        background-color: $white;
        padding: $gl-padding-4 $gl-padding-8;
      }

      &.draft-note .timeline-content:not(.flash-container) {
        border: 0;
      }

      .note-header-info {
        min-height: 2rem;
        display: flex;
        align-items: center;
        gap: 0 0.25rem;
        flex-wrap: wrap;
        line-height: $gl-line-height-20;
        padding-top: $gl-padding-6;
        padding-bottom: $gl-padding-6;
      }
    }

    &.note-discussion {
      .timeline-content .discussion-wrapper {
        background-color: transparent;
      }

      .timeline-content {
        ul li {
          &:first-of-type {
            .timeline-avatar {
              margin-top: 5px;
            }

            .timeline-content:not(.flash-container) {
              margin-left: 2.5rem;
              border-left: 1px solid $border-color;
              border-right: 1px solid $border-color;
              border-top: 1px solid $border-color;
              border-top-left-radius: $gl-border-radius-base;
              border-top-right-radius: $gl-border-radius-base;
              background-color: $white;
              padding: $gl-padding-4 $gl-padding-8;
            }
          }

          &.draft-note .timeline-content:not(.flash-container) {
            margin-left: 0;
            border-top-left-radius: 0;
            border-top-right-radius: 0;
          }

          &:not(:first-of-type) .timeline-entry-inner {
            margin-left: 2.5rem;
            border-left: 1px solid $border-color;
            border-right: 1px solid $border-color;
            background-color: $white;

            .timeline-content:not(.flash-container) {
              padding: $gl-padding-8 $gl-padding-8 $gl-padding-8 18px;
            }

            .timeline-discussion-body-footer {
              padding: 0 $gl-padding-8 0;
            }

            .timeline-avatar {
              margin: $gl-padding-8 0 0 $gl-padding;
            }

            .timeline-discussion-body {
              margin-left: 2rem;
            }
          }

          &:last-of-type .timeline-entry-inner {
            border-bottom: 1px solid $border-color;
            border-bottom-left-radius: $gl-border-radius-base;
            border-bottom-right-radius: $gl-border-radius-base;
          }
        }

        .diff-content {
          ul li:first-of-type {
            .timeline-avatar {
              margin-top: 0;
            }

            .timeline-content {
              margin-left: 0;
              border: 0;
              padding: 0;
            }

            .timeline-entry-inner {
              margin-left: 2.5rem;
              border-left: 1px solid $border-color;
              border-right: 1px solid $border-color;
              background-color: $white;

              .timeline-content {
                padding: $gl-padding-8 $gl-padding-8 $gl-padding-8 $gl-padding;
              }

              .timeline-avatar {
                margin: $gl-padding-8 0 0 $gl-padding;
              }

              .timeline-discussion-body {
                margin-left: 2rem;
              }
            }
          }
        }
      }

      .discussion-reply-holder {
        border: 1px solid $border-color;
      }
    }

    &.note-form {
      margin-left: 0;

      .timeline-content {
        margin-left: 0;
      }
    }

    .notes-content {
      border: 0;
      border-top: 1px solid $border-color;
    }
  }

  > .note-discussion {
    .card {
      border: 0;
    }
  }

  .discussion-toggle-replies {
    border-top: 0;
    border-radius: 4px 4px 0 0;

    &.collapsed {
      border-radius: 4px;
    }
  }

  .note-created-ago,
  .note-updated-at {
    white-space: normal;
  }

  .discussion-body {
    .card {
      margin-bottom: 0;
    }

    .note-header-info {
      min-height: 2rem;
      display: flex;
      align-items: center;
      gap: 0 0.25rem;
      flex-wrap: wrap;
      padding-top: $gl-padding-6;
      padding-bottom: $gl-padding-6;
      line-height: $gl-line-height-20;
    }
  }

  .discussion {
    display: block;
    position: relative;

    .timeline-discussion-body {
      overflow-x: auto;
      overflow-y: hidden;
    }

    .diff-content {
      overflow: visible;
      padding: 0;
    }
  }

  > li {
    display: block;
    position: relative;

    &.being-posted {
      pointer-events: none;

      .timeline-entry-inner {
        opacity: 0.5;
      }

      .dummy-avatar {
        background-color: $gray-100;
        border: 1px solid darken($gray-100, 25%);
      }
    }

    .editing-spinner {
      display: none;
    }

    &.is-requesting {
      .note-timestamp {
        display: none;
      }

      .editing-spinner {
        display: inline-block;
      }
    }

    &.is-editing {
      .note-actions,
      .note-text,
      .edited-text {
        display: none;
      }

      .user-avatar-link img {
        margin-top: $gl-padding-8;
      }

      .note-edit-form {
        display: block;
        margin-left: 0;
        margin-top: 0.5rem;

        &.current-note-edit-form + .note-awards {
          display: none;
        }
      }
    }

    .note-body {
      padding: 0 $gl-padding-8 $gl-padding-8;
      overflow-x: auto;
      overflow-y: hidden;

      .note-text {
        word-wrap: break-word;
      }

      .suggestions {
        margin-top: 4px;
      }
    }

    .note-awards {
      .js-awards-block {
        margin-top: 16px;
      }
    }

    .note-emoji-button {
      position: relative;
      line-height: 1;
    }
  }

  .system-note {
    padding: $gl-padding-8 0;
    margin: $gl-padding 0;
    background-color: transparent;
    font-size: $gl-font-size;

    .note-header-info {
      padding-bottom: 0;
      padding-top: 0;
    }

    &.timeline-entry::after {
      clear: none;
    }

    .system-note-message {
      &::first-letter {
        text-transform: lowercase;
      }

      a:not(.gl-link) {
        color: $blue-600;
      }

      p {
        display: inline;
        margin: 0;

        &::first-letter {
          text-transform: lowercase;
        }
      }
    }

    .timeline-content {
      margin-left: 30px;
    }

    .note-header {
      padding-bottom: 0;
    }

    .note-body {
      overflow: hidden;
      padding: 0;

      ul {
        margin: 0.5rem 0;
      }

      p {
        margin-left: 1rem;
      }

      .description-version {
        position: relative;

        .btn.delete-description-history {
          position: absolute;
          top: 18px;
          right: 10px;
        }

        pre {
          max-height: $dropdown-max-height-lg;
          white-space: pre-wrap;
          padding-right: 30px;
        }
      }

      .system-note-commit-list-toggler {
        color: $blue-600;
        padding: 10px 0 0;
        cursor: pointer;
        position: relative;
        z-index: 2;

        &:hover {
          color: $blue-600;
          text-decoration: underline;
        }
      }

      .note-text {
        p:first-child {
          display: none;
        }

        &.system-note-commit-list:not(.hide-shade) {
          max-height: 70px;
          overflow: hidden;
          display: block;

          ul {
            margin: 3px 0 3px 16px !important;
          }

          p:first-child {
            display: none;
          }

          &::after {
            content: '';
            height: 70px;
            position: absolute;
            left: $gl-padding-24;
            right: 0;
            bottom: 0;
            background: linear-gradient(rgba($white, 0.1) -100px, $body-bg 100%);
          }
        }
      }
    }
  }
}

.card .notes {
  .system-note {
    margin: 0;
    padding: 0;
  }

  .timeline-icon {
    margin: 20px 0 0 28px;
  }
}


// Diff code in discussion view
.discussion-body .diff-file {
  .file-title {
    cursor: default;
    border-top: 0;
    border-radius: 0;
    margin-left: 2.5rem;

    &:hover {
      background-color: $gray-light;
    }

    .btn-clipboard {
      top: 10px;
    }
  }

  .line_content {
    white-space: pre-wrap;
  }

  .diff-content {
    margin-left: 2.5rem;

    &.outdated-lines-wrapper {
      margin-left: 0;
    }

    .line_holder td:first-of-type {
      @include gl-border-l;
    }

    .line_holder td:last-of-type {
      @include gl-border-r;
    }

    .discussion-notes {
      margin-left: -2.5rem;

      .notes {
        background-color: transparent;
      }

      .notes-content {
        border: 0;
      }

      .timeline-content {
        border-top: 0 !important;
        border-top-left-radius: 0 !important;
        border-top-right-radius: 0 !important;
      }
    }
  }
}

.tab-pane.notes {
  .diff-file .notes .system-note {
    margin: 0;
  }
}

.tab-pane.diffs {
  .system-note {
    padding: 0 $gl-padding;
    margin-left: 20px;
  }

  .notes > .note-discussion li.note.system-note {
    border-bottom: 0;
    padding: 0;
  }
}

.diff-file {
  .diff-grid-left:hover,
  .diff-grid-right:hover,
  .is-over {
    .add-diff-note {
      display: inline-flex;
      justify-content: center;
      align-items: center;
    }
  }

  .discussion-notes {
    &:not(:last-child) {
      margin-bottom: 0;
    }

    .system-note {
      background-color: transparent;
      padding: 0;
    }
  }

  // Merge request notes in diffs
  // Diff is inline
  .notes-content .note-header .note-headline-light {
    display: inline-block;
    position: relative;
  }

  .notes-content .discussion-notes.diff-discussions {
    border-bottom: 1px solid $border-color;

    &:nth-last-child(1) {
      border-bottom: 0;
    }
  }

  .notes_holder {
    font-family: $regular-font;

    .diff-td,
    td {
      border: 1px solid $border-color;
      border-left: 0;

      &.notes-content {
        border-width: 1px 0;
        padding: 0;
        vertical-align: top;
        white-space: normal;
        background-color: $white;

        &.parallel {
          border-width: 1px;

          &.new {
            border-right-width: 0;
          }

          .note-header {
            flex-wrap: wrap;
            align-items: center;
          }
        }

        .notes {
          background-color: $white;
        }

        a code {
          top: 0;
          margin-right: 0;
        }
      }
    }
  }

  .diff-grid-comments:last-child {
    .notes-content {
      border-bottom-width: 0;
      border-bottom-left-radius: #{$border-radius-default - 1px};
      border-bottom-right-radius: #{$border-radius-default - 1px};
    }
  }
}

.diffs {
  .discussion-notes {
    margin-left: 0;
    border-left: 0;
  }

  .note-wrapper {
    &.system-note {
      border: 0;
      margin-left: 20px;
    }
  }

  .discussion-reply-holder {
    border-top: 0;
    border-radius: 0 0 $gl-border-radius-base $gl-border-radius-base;
    position: relative;

    .discussion-form {
      width: 100%;
      background-color: $gray-light;
      padding: 0;
    }

    .disabled-comment {
      padding: $gl-vert-padding 0;
      width: 100%;
    }
  }
}

.code-commit .notes-content,
.diff-viewer > .image ~ .note-container {
  background-color: $white;

  li.note-comment {
    padding: $gl-padding-8 $gl-padding-8 $gl-padding-8 $gl-padding;

    .avatar {
      margin-right: 0;
    }

    .note-body {
      padding: $gl-padding-4 0 $gl-padding-8;
      margin-left: 2.5rem;
    }
  }
}

.diff-viewer > .image ~ .note-container form.new-note {
  margin-left: 0;
}

.discussion-header,
.note-header-info {
  a {
    color: inherit;

    &:hover,
    &.hover {
      color: $blue-600;
    }

    &:focus,
    &:hover {
      text-decoration: none;

      .note-header-author-name {
        text-decoration: underline;
      }
    }
  }

  .author-link {
    color: $gl-text-color;
  }

  // Prevent flickering of link when hovering between `author-name-link` and `.author-username-link`
  .author-name-link + .author-username .author-username-link {
    position: relative;

    &::before {
      content: '';
      position: absolute;
      right: 100%;
      width: 0.25rem;
      height: 100%;
      top: 0;
      bottom: 0;
    }
  }
}

.discussion-header {
  box-sizing: content-box;

  .note-header-info {
    padding-bottom: 0;
  }

  .timeline-avatar {
    margin-top: 5px;
  }

  .timeline-content {
    overflow-x: auto;
    overflow-y: hidden;
    border-radius: $gl-border-radius-base;
    padding: $gl-padding-8 !important;
    @include gl-border;

    &.expanded {
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
    }
  }

  &.note-wrapper {
    display: flex;
    align-items: center;
    padding-right: $gl-padding;
  }
}

.note-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;

  > .note-header-info,
  > .note-actions {
    flex-grow: 1;
    flex-shrink: 1;
  }
}

.note {
  @include notes-media('max', map-get($grid-breakpoints, sm) - 1) {
    .note-header {
      .note-actions > :first-child {
        margin-left: 0;
      }
    }
  }
}

.note-header-info {
  min-width: 0;
  padding-left: $gl-padding-8;

  &.discussion {
    padding-bottom: 0;
  }
}

.system-note .note-header-info {
  padding-bottom: 0;
}

.note-headline-light {
  display: inline;
}

.note-headline-light,
.discussion-headline-light {
  color: $gl-text-color-secondary;
}

.discussion-headline-light {
  a {
    color: $blue-600;
  }
}

.note-headline-meta {
  .note-timestamp {
    white-space: nowrap;
  }

  a:hover {
    text-decoration: underline;
  }

  .gl-label-link:hover {
    text-decoration: none;
    color: inherit;

    .gl-label-text:last-of-type {
      text-decoration: underline;
    }
  }
}

/**
 * Actions for Discussions/Notes
 */

.discussion-actions {
  float: right;
  color: $gray-darkest;

  @include media-breakpoint-down(xs) {
    width: 100%;
    margin: 0 0 $gl-padding-8;
  }

  .btn-group > .discussion-next-btn {
    margin-left: -1px;
  }

  .btn-group > .discussion-create-issue-btn {
    margin-left: -2px;
  }

  svg {
    height: 15px;
  }
}

.note-actions {
  justify-content: flex-end;
  flex-shrink: 1;
  display: inline-flex;
  align-items: center;
  margin-left: $gl-padding-8;
  color: $gray-400;

  @include notes-media('max', map-get($grid-breakpoints, sm) - 1) {
    justify-content: flex-start;
    float: none;

    .note-actions__mobile-spacer {
      flex-grow: 1;
    }
  }
}

.more-actions {
  display: flex;
  align-items: flex-end;

  .tooltip {
    white-space: nowrap;
  }
}

.more-actions-toggle {
  &:hover .icon,
  &:focus .icon {
    color: $blue-600;
  }
}

.more-actions-dropdown {
  width: 180px;
  min-width: 180px;
}

.note-actions-item {
  margin-left: 12px;
  display: flex;
  align-items: center;

  &.more-actions {
    // compensate for narrow icon
    margin-left: 10px;
  }
}

.note-action-button {
  @include emoji-menu-toggle-button;
}

.discussion-toggle-button {
  padding: 0 $gl-padding-8 0 0;
  background-color: transparent;
  border: 0;
  line-height: 20px;
  font-size: 13px;
  transition: color 0.1s linear;

  &:hover {
    color: $blue-600;
  }

  &:focus {
    text-decoration: underline;
    outline: none;
    color: $blue-600;
  }
}

.note-role {
  margin: 0 8px;
}

/**
 * Line note button on the side of diffs
 */

.diff-grid-left:hover,
.diff-grid-right:hover,
.line_holder .is-over:not(.no-comment-btn) {
  .add-diff-note {
    opacity: 1;
    z-index: 101;
  }
}

.tooltip-wrapper.add-diff-note {
  margin-left: -52px;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 10;
}

.note-button.add-diff-note {
  @include btn-comment-icon;
  opacity: 0;
  will-change: opacity;

  &[disabled] {
    background: $white;
    border-color: $gray-200;
    color: $gray-300;
    cursor: not-allowed;
  }
}

.unified-diff-components-diff-note-button {
  &::before {
    background-color: $blue-500;
    mask-image: asset_url('icons-stacked.svg#comment');
    mask-repeat: no-repeat;
    mask-size: cover;
    mask-position: center;
    content: '';
    width: 12px;
    height: 12px;
  }

  &:hover:not([disabled]),
  &.inverted {
    &::before {
      background-color: $white;
    }
  }
}

.disabled-comment {
  background-color: $gray-light;
  border-radius: $border-radius-base;
  border: 1px solid $border-gray-normal;
  color: $note-disabled-comment-color;
  padding: $gl-padding-8 0;

  &.discussion-locked {
    border: 0;
    background-color: $white;
  }

  a:not(.learn-more) {
    color: $blue-600;
  }
}

.line-resolve-btn {
  position: relative;
  top: 0;
  padding: 0;
  background-color: transparent;
  border: 0;
  outline: 0;
  transition: color $general-hover-transition-duration $general-hover-transition-curve;

  &[disabled] {
    padding: 0 8px !important;
    box-shadow: none !important;

    .gl-button-loading-indicator {
      margin-right: 0 !important;
    }
  }

  &.is-disabled {
    cursor: default;
  }

  &:not(.is-disabled) {
    &:hover,
    &:focus {
      svg {
        color: $green-600;
      }
    }
  }

  &.is-active {
    svg {
      @include gl-text-green-500;
    }

    &:hover,
    &:focus {
      svg {
        color: $green-700;
      }
    }
  }

  .loading {
    margin: 0;
    height: auto;
  }
}

// Vue refactored diff discussion adjustments
.files {
  .diff-discussions {
    .note-discussion.timeline-entry {
      padding-left: 0;

      ul.notes li.note-wrapper {
        .timeline-content:not(.flash-container) {
          padding: $gl-padding-8 $gl-padding-8 $gl-padding-8 $gl-padding;
        }

        .timeline-avatar {
          margin: $gl-padding-8 0 0 $gl-padding;
        }
      }

      ul.notes {
        li.toggle-replies-widget {
          margin-left: 0;
          border-left: 0;
          border-right: 0;
        }

        .discussion-reply-holder {
          margin-left: 0;
        }
      }

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

      > .timeline-entry-inner {
        padding: 0;

        > .timeline-content {
          margin-left: 0;
        }

        > .timeline-icon {
          display: none;
        }
      }

      .discussion-body {
        padding-top: 0;

        .discussion-wrapper {
          border: 0;
        }
      }
    }
  }

  .draft-note-component.draft-note.timeline-entry {
    .timeline-content:not(.flash-container) {
      padding: $gl-padding-8 $gl-padding-8 $gl-padding-8 $gl-padding;
    }

    .timeline-avatar {
      margin: $avatar-m-top 0 0 $avatar-m-left;
    }
  }

  .diff-comment-form {
    display: block;
  }
}

.discussion-filter-container {
  .dropdown-menu {
    margin-bottom: $gl-padding-4;
  }
}

//This needs to be deleted when Snippet/Commit comments are convered to Vue
// See https://gitlab.com/gitlab-org/gitlab-foss/issues/53918#note_117038785
.unstyled-comments {
  .discussion-header {
    padding: $gl-padding 0;
  }

  .discussion-form-container {
    padding: $gl-padding;
  }
}
