@mixin flat-connector-before($length: 44px) {
  &::before {
    content: '';
    position: absolute;
    top: 48%;
    left: -$length;
    border-top: 2px solid $border-color;
    width: $length;
    height: 1px;
  }
}

@mixin build-content($border-radius: 30px) {
  display: inline-block;
  padding: 8px 10px 9px;
  width: 100%;
  border: 1px solid $border-color;
  border-radius: $border-radius;
  background-color: $white-light;

  &:hover {
    background-color: $stage-hover-bg;
    border: 1px solid $dropdown-toggle-active-border-color;
    color: $gl-text-color;
  }
}

.pipelines {
  .stage {
    max-width: 90px;
    width: 90px;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .table-holder {
    width: 100%;
  }

  .commit-title {
    margin: 0;
  }

  .ci-table {

    .label {
      margin-bottom: 3px;
    }

    .pipeline-id {
      color: $black;
    }

    .pipelines-time-ago {
      text-align: right;
    }

    .pipeline-actions {
      min-width: 170px; //Guarantees buttons don't break in several lines.

      .btn-default {
        color: $gl-text-color-secondary;
      }

      .btn.btn-retry:hover,
      .btn.btn-retry:focus {
        border-color: $dropdown-toggle-active-border-color;
        background-color: $white-normal;
      }

      svg path {
        fill: $gl-text-color-secondary;
      }

      .dropdown-menu {
        max-height: $dropdown-max-height;
        overflow-y: auto;
      }

      .dropdown-toggle,
      .dropdown-menu {
        color: $gl-text-color-secondary;

        .fa {
          color: $gl-text-color-secondary;
          font-size: 14px;
        }
      }

      .btn-group {
        &.open {
          .btn-default {
            background-color: $white-normal;
            border-color: $border-white-normal;
          }
        }
      }

      .btn .text-center {
        display: inline;
      }

      .tooltip {
        white-space: nowrap;
      }
    }
  }
}

@media (max-width: $screen-md-max) {
  .content-list {
    &.builds-content-list {
      width: 100%;
      overflow: auto;
    }
  }
}

.ci-table {
  .build.retried {
    background-color: $gray-lightest;
  }

  .commit-link {
    a {
      &:focus {
        text-decoration: none;
      }
    }

    a:hover {
      text-decoration: none;
    }
  }

  .avatar {
    margin-left: 0;
    float: none;
  }

  .api {
    @extend .monospace;
  }

  .branch-commit {

    .ref-name {
      font-weight: $gl-font-weight-bold;
      max-width: 120px;
      overflow: hidden;
      display: inline-block;
      white-space: nowrap;
      vertical-align: middle;
      text-overflow: ellipsis;
    }

    svg {
      height: 14px;
      width: 14px;
      vertical-align: middle;
      fill: $gl-text-color-secondary;
    }

    .sprite {
      width: 12px;
      height: 12px;
      fill: $gl-text-color;
    }

    .fa {
      font-size: 12px;
      color: $gl-text-color;
    }

    .commit-sha {
      color: $gl-link-color;
    }

    .label {
      margin-right: 4px;
    }

    .label-container {
      font-size: 0;

      .label {
        margin-top: 5px;
      }
    }
  }

  .icon-container {
    display: inline-block;
    width: 10px;

    &.commit-icon {
      width: 15px;
      text-align: center;
    }
  }

  /**
   * Play button with icon in dropdowns
   */
  .no-btn {
    border: 0;
    background: none;
    outline: none;
    width: 100%;
    text-align: left;

    .icon-play {
      position: relative;
      top: 2px;
      margin-right: 5px;
      height: 13px;
      width: 12px;
    }
  }

  .duration,
  .finished-at {
    color: $gl-text-color-secondary;
    margin: 0;
    white-space: nowrap;

    .fa {
      font-size: 12px;
      margin-right: 4px;
    }

    svg {
      width: 12px;
      height: 12px;
      vertical-align: middle;
      margin-right: 4px;
    }
  }

  .build-link a {
    color: $gl-text-color;
  }

  .btn-group.open .dropdown-toggle {
    box-shadow: none;
  }

  .pipeline-tags .label-container {
    white-space: normal;
  }
}

.stage-cell {
  &.table-section {
    @media (min-width: $screen-md-min) {
      min-width: 160px; /* Hack alert: Without this the mini graph pipeline won't work properly*/
      margin-right: -4px;
    }
  }

  .mini-pipeline-graph-dropdown-toggle svg {
    height: $ci-action-icon-size;
    width: $ci-action-icon-size;
    position: absolute;
    top: -1px;
    left: -1px;
    z-index: 2;
    overflow: visible;
  }

  .stage-container {
    display: inline-block;
    position: relative;
    vertical-align: middle;
    height: $ci-action-icon-size;
    margin: 3px 0;

    + .stage-container {
      margin-left: 6px;
    }

    // Hack to show a button tooltip inline
    button.has-tooltip + .tooltip {
      min-width: 105px;
    }

    // Bootstrap way of showing the content inline for anchors.
    a.has-tooltip {
      white-space: nowrap;
    }

    &:not(:last-child) {
      &::after {
        content: '';
        width: 7px;
        position: absolute;
        right: -7px;
        top: 10px;
        border-bottom: 2px solid $border-color;
      }
    }

    &.has-downstream {
      &::after {
        content: '';
        width: 0;
        border: 0;
      }
    }
  }
}

.admin-builds-table {
  .ci-table td:last-child {
    min-width: 120px;
  }
}

// Pipeline visualization
.pipeline-actions {
  border-bottom: 0;
}

.tab-pane {
  &.builds .ci-table tr {
    height: 71px;
  }
}

.build-failures {
  .build-state {
    padding: 20px 2px;

    .build-name {
      float: right;
      font-weight: $gl-font-weight-normal;
    }

    .ci-status-icon-failed svg {
      vertical-align: middle;
    }

    .stage {
      color: $gl-text-color-secondary;
      font-weight: $gl-font-weight-normal;
      vertical-align: middle;
    }
  }

  .build-log {
    border: 0;
    line-height: initial;
  }
}

// Pipeline graph
.pipeline-graph {
  width: 100%;
  background-color: $gray-light;
  padding: $gl-padding;
  white-space: nowrap;
  transition: max-height 0.3s, padding 0.3s;
  overflow: auto;

  .stage-column-list,
  .builds-container > ul {
    padding: 0;
  }

  a {
    text-decoration: none;
    color: $gl-text-color;
  }

  svg {
    vertical-align: middle;
    margin-right: 3px;
  }

  .stage-column {
    display: inline-block;
    vertical-align: top;

    &:not(:last-child) {
      margin-right: 44px;
    }

    &.left-margin {
      &:not(:first-child) {
        margin-left: 44px;

        .left-connector {
          @include flat-connector-before;
        }
      }
    }

    &.no-margin {
      margin: 0;
    }

    li {
      list-style: none;
    }

    // when downstream pipelines are present, the last stage isn't the last column
    &:last-child:not(.has-downstream) {
      .build {
        // Remove right connecting horizontal line from first build in last stage
        &:first-child {
          &::after {
            border: 0;
          }
        }
        // Remove right curved connectors from all builds in last stage
        &:not(:first-child) {
          &::after {
            border: 0;
          }
        }
        // Remove opposite curve
        .curve {
          &::before {
            display: none;
          }
        }
      }
    }

    // when upstream pipelines are present, the first stage isn't the first column
    &:first-child:not(.has-upstream) {
      .build {
        // Remove left curved connectors from all builds in first stage
        &:not(:first-child) {
          &::before {
            border: 0;
          }
        }
        // Remove opposite curve
        .curve {
          &::after {
            display: none;
          }
        }
      }
    }

    // Curve first child connecting lines in opposite direction
    .curve {
      display: none;

      &::before,
      &::after {
        content: '';
        width: 21px;
        height: 25px;
        position: absolute;
        top: -31px;
        border-top: 2px solid $border-color;
      }

      &::after {
        left: -44px;
        border-right: 2px solid $border-color;
        border-radius: 0 20px;
      }

      &::before {
        right: -44px;
        border-left: 2px solid $border-color;
        border-radius: 20px 0 0;
      }
    }
  }

  .stage-name {
    margin: 0 0 15px 10px;
    font-weight: $gl-font-weight-bold;
    width: 176px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .build {
    position: relative;
    width: 186px;
    margin-bottom: 10px;
    white-space: normal;

    // ensure .build-content has hover style when action-icon is hovered
    .ci-job-dropdown-container:hover .build-content {
      @extend .build-content:hover;
    }

    // Action Icons in big pipeline-graph nodes
    .ci-action-icon-container.ci-action-icon-wrapper {
      position: absolute;
      right: 5px;
      top: 5px;
      height: 30px;
      width: 30px;
      background: $white-light;
      border: 1px solid $border-color;
      border-radius: 100%;
      display: block;

      &:hover {
        background-color: $stage-hover-bg;
        border: 1px solid $dropdown-toggle-active-border-color;

        svg {
          fill: $gl-text-color;
        }
      }

      svg {
        fill: $gl-text-color-secondary;
        position: relative;
        left: 5px;
        top: 2px;
        width: 18px;
        height: 18px;
      }

      &.play {
        svg {
          width: #{$ci-action-icon-size - 8};
          height: #{$ci-action-icon-size - 8};
          left: 8px;
        }
      }
    }

    .ci-status-icon svg {
      height: 20px;
      width: 20px;
    }

    .dropdown-menu-toggle {
      background-color: transparent;
      border: 0;
      padding: 0;

      &:focus {
        outline: none;
      }
    }

    .build-content {
      @include build-content();
      display: inline-block;
      padding: 8px 10px 9px;
      width: 100%;
      border: 1px solid $border-color;
      border-radius: 30px;
      background-color: $white-light;
    }

    a.build-content:hover,
    button.build-content:hover {
      background-color: $stage-hover-bg;
      border: 1px solid $dropdown-toggle-active-border-color;
    }

    // Connect first build in each stage with right horizontal line
    &:first-child {
      &::after {
        content: '';
        position: absolute;
        top: 48%;
        right: -48px;
        border-top: 2px solid $border-color;
        width: 48px;
        height: 1px;
      }
    }

    // Connect each build (except for first) with curved lines
    &:not(:first-child) {
      &::after,
      &::before {
        content: '';
        top: -49px;
        position: absolute;
        border-bottom: 2px solid $border-color;
        width: 25px;
        height: 69px;
      }

      // Right connecting curves
      &::after {
        right: -25px;
        border-right: 2px solid $border-color;
        border-radius: 0 0 20px;
      }

      // Left connecting curves
      &::before {
        left: -25px;
        border-left: 2px solid $border-color;
        border-radius: 0 0 0 20px;
      }
    }

    // Connect second build to first build with smaller curved line
    &:nth-child(2) {
      &::after,
      &::before {
        height: 29px;
        top: -9px;
      }

      .curve {
        display: block;
      }
    }
  }
}

// Triggers the dropdown in the big pipeline graph
.dropdown-counter-badge {
  font-weight: 100;
  font-size: 15px;
  position: absolute;
  right: 13px;
  top: 8px;
}

.ci-status-text {
  max-width: 110px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: bottom;
  display: inline-block;
  position: relative;
  font-weight: $gl-font-weight-normal;
}

@mixin mini-pipeline-graph-color($color-light, $color-main, $color-dark) {
  border-color: $color-main;
  color: $color-main;

  &:hover,
  &:focus,
  &:active {
    background-color: $color-light;
    border-color: $color-dark;
    color: $color-dark;

    svg {
      fill: $color-dark;
    }
  }
}

// Dropdown button in mini pipeline graph
button.mini-pipeline-graph-dropdown-toggle,
a.linked-pipeline-mini-item {
  border-radius: 100px;
  background-color: $white-light;
  border-width: 1px;
  border-style: solid;
  width: $ci-action-icon-size;
  height: $ci-action-icon-size;
  margin: 0;
  padding: 0;
  transition: all 0.2s linear;
  position: relative;
  vertical-align: middle;

  > .fa.fa-caret-down {
    position: absolute;
    left: 20px;
    top: 5px;
    display: inline-block;
    visibility: hidden;
    opacity: 0;
    color: inherit;
    font-size: 12px;
    transition: visibility 0.1s, opacity 0.1s linear;
  }

  &:active,
  &:focus,
  &:hover {
    outline: none;
    width: 35px;

    .fa.fa-caret-down {
      visibility: visible;
      opacity: 1;
    }
  }

  // Dropdown button animation in mini pipeline graph
  &.ci-status-icon-success {
    @include mini-pipeline-graph-color($green-100, $green-500, $green-600);
  }

  &.ci-status-icon-failed {
    @include mini-pipeline-graph-color($red-100, $red-500, $red-600);
  }

  &.ci-status-icon-pending,
  &.ci-status-icon-success_with_warnings {
    @include mini-pipeline-graph-color($orange-100, $orange-500, $orange-600);
  }

  &.ci-status-icon-running {
    @include mini-pipeline-graph-color($blue-100, $blue-400, $blue-600);
  }

  &.ci-status-icon-canceled,
  &.ci-status-icon-disabled,
  &.ci-status-icon-not-found,
  &.ci-status-icon-manual {
    @include mini-pipeline-graph-color(rgba($gl-text-color, 0.1), $gl-text-color, $gl-text-color);
  }

  &.ci-status-icon-created,
  &.ci-status-icon-skipped {
    @include mini-pipeline-graph-color(rgba($gray-darkest, 0.1), $gray-darkest, $gray-darkest);
  }
}

// dropdown content for big and mini pipeline
.big-pipeline-graph-dropdown-menu,
.mini-pipeline-graph-dropdown-menu {
  width: 195px;
  max-width: 195px;

  .scrollable-menu {
    padding: 0;
    max-height: 245px;
    overflow: auto;
  }

  li {
    position: relative;

    // ensure .mini-pipeline-graph-dropdown-item has hover style when action-icon is hovered
    &:hover > .mini-pipeline-graph-dropdown-item,
    &:hover > .ci-job-component > .mini-pipeline-graph-dropdown-item {
      @extend .mini-pipeline-graph-dropdown-item:hover;
    }

    // Action icon on the right
    a.ci-action-icon-wrapper {
      border-radius: 50%;
      border: 1px solid $border-color;
      width: $ci-action-icon-size;
      height: $ci-action-icon-size;
      padding: 2px 0 0 5px;
      font-size: 12px;
      background-color: $white-light;
      position: absolute;
      top: 50%;
      right: $gl-padding;
      margin-top: -#{$ci-action-icon-size / 2};

      &:hover,
      &:focus {
        background-color: $stage-hover-bg;
        border: 1px solid $dropdown-toggle-active-border-color;
      }

      svg {
        fill: $gl-text-color-secondary;
        width: #{$ci-action-icon-size - 6};
        height: #{$ci-action-icon-size - 6};
        left: -3px;
        position: relative;
        top: -2px;

        &.icon-action-stop,
        &.icon-action-cancel {
          width: 12px;
          height: 12px;
          top: 1px;
          left: -1px;
        }

        &.icon-action-play {
          width: 11px;
          height: 11px;
          top: 1px;
          left: 1px;
        }

        &.icon-action-retry {
          width: 16px;
          height: 16px;
          top: 0;
          left: -3px;
        }
      }

      &:hover svg,
      &:focus svg {
        fill: $gl-text-color;
      }

      &.icon-action-retry,
      &.icon-action-play {
        svg {
          width: #{$ci-action-icon-size - 6};
          height: #{$ci-action-icon-size - 6};
          left: 8px;
        }
      }


    }

    // link to the build
    .mini-pipeline-graph-dropdown-item {
      align-items: center;
      clear: both;
      display: flex;
      font-weight: normal;
      line-height: $line-height-base;
      white-space: nowrap;

      .ci-job-name-component {
        align-items: center;
        display: flex;
        flex: 1;
      }

      // build name
      .ci-build-text,
      .ci-status-text {
        font-weight: 200;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        max-width: 70%;
        margin-left: 2px;
        display: inline-block;

        &::after {
          content: '';
          display: block;
        }

        @media (max-width: $screen-xs-max) {
          max-width: 60%;
        }
      }

      .ci-status-icon {
        @extend .append-right-8;

        position: relative;

        > svg {
          width: $pipeline-dropdown-status-icon-size;
          height: $pipeline-dropdown-status-icon-size;
          margin: 3px 0;
          position: relative;
          overflow: visible;
          display: block;
        }
      }

      &:hover,
      &:focus {
        outline: none;
        text-decoration: none;
        background-color: $stage-hover-bg;
      }
    }
  }
}

// Dropdown in the big pipeline graph
.big-pipeline-graph-dropdown-menu {
  width: 195px;
  min-width: 195px;
  left: 100%;
  top: -10px;
  box-shadow: 0 1px 5px $black-transparent;

  /**
   * Top arrow in the dropdown in the big pipeline graph
   */
  &::before,
  &::after {
    content: '';
    display: inline-block;
    position: absolute;
    width: 0;
    height: 0;
    border-color: transparent;
    border-style: solid;
    top: 18px;
  }

  &::before {
    left: -6px;
    margin-top: 3px;
    border-width: 7px 5px 7px 0;
    border-right-color: $border-color;
  }

  &::after {
    left: -5px;
    border-width: 10px 7px 10px 0;
    border-right-color: $white-light;
  }
}

/**
 * Top arrow in the dropdown in the mini pipeline graph
 */
.mini-pipeline-graph-dropdown-menu {
  z-index: 200;

  &::before,
  &::after {
    content: '';
    display: inline-block;
    position: absolute;
    width: 0;
    height: 0;
    border-color: transparent;
    border-style: solid;
    top: -6px;
    left: 50%;
    transform: translate(-50%, 0);
    border-width: 0 5px 6px;

    @media (max-width: $screen-sm-max) {
      left: 100%;
      margin-left: -12px;
    }
  }

  &::before {
    border-width: 0 5px 5px;
    border-bottom-color: $border-color;
  }

  &::after {
    margin-top: 1px;
    border-bottom-color: $white-light;
  }

  /**
   * Center dropdown menu in mini graph
   */
  &.dropdown-menu {
    transform: translate(-80%, 0);
    min-width: 150px;

    @media(min-width: $screen-md-min) {
      transform: translate(-50%, 0);
      right: auto;
      left: 50%;
      min-width: 240px;
    }
  }
}

/**
 * Terminal
 */
.terminal-icon {
  margin-left: 3px;
}

.terminal-container {
  .content-block {
    border-bottom: 0;
  }

  #terminal {
    margin-top: 10px;
    min-height: 450px;
    box-sizing: border-box;

    > div {
      min-height: 450px;
    }
  }
}

.linked-pipeline-mini-list {
  display: inline-block;

  &.is-downstream {
    margin-left: -4px;
    margin-right: 4px;
  }

  .arrow-icon {
    display: inline-block;
    vertical-align: middle;
    margin: -4px 2px 0 0;

    svg {
      fill: $gray-darkest;
    }
  }

  &:hover {
    .linked-pipeline-mini-item {
      margin-left: 0;
    }
  }

  .linked-pipeline-mini-item {
    position: relative;
    display: inline-block;
    vertical-align: middle;
    height: 22px;
    width: 22px;
    transition: margin .2s linear;
    margin: 2px 7px 3px -14px;

    svg {
      height: 22px;
      width: 22px;
      position: absolute;
      top: -1px;
      left: -1px;
      z-index: 2;
      overflow: visible;
    }
    // override dropdown-toggle width expansion
    &:hover {
      width: 22px;
    }

    &:first-of-type:last-of-type {
      margin-right: 1px;
    }

    &:nth-of-type(1) {
      margin-left: 0;
      z-index: 100;
    }

    &:nth-of-type(2):not(.linked-pipelines-counter) {
      z-index: 99;
    }

    &:nth-of-type(3) {
      z-index: 98;
    }

    &:nth-of-type(4) {
      z-index: 97;
    }
  }

  .linked-pipelines-counter {
    position: relative;
    font-size: 12px;
    vertical-align: middle;
    line-height: 20px;
    height: 22px;
    width: 22px;
    padding-left: 1px;
    margin-left: -15px;
    border-radius: 2em;
    background: $gray-darkest;
    color: $white-light;
    z-index: 96;
    text-decoration: none;

    &:hover {
      width: 22px;
      background: darken($gray-darkest, 10%);
    }
  }
}

/**
 * Cross-project pipelines (applied conditionally to pipeline graph)
 */

.has-linked-pipelines.stage-column-list {
  display: inline-block;
}

.linked-pipelines-column.stage-column {
  position: relative;

  > ul {
    padding: 0;
  }

  &.graph-position-left {
    margin-right: 36px;

    .cross-project-triangle {
      right: -42px;
    }
  }

  &.graph-position-right {
    margin-left: 60px;

    .cross-project-triangle {
      left: -64px;
    }
  }

  .linked-pipeline.build {
    height: 40px;

    // apply custom dimensions to connector before and after for triangle arrow
    &.flat-connector-before {
      @include flat-connector-before($linked-project-column-margin);
    }

    &::after {
      right: -$linked-project-column-margin;
      width: $linked-project-column-margin;
    }

    .linked-pipeline-content {
      @include build-content(0);
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}

.stage-column.has-upstream {
  margin-left: 60px;

  .left-connector {
    @include flat-connector-before(60px)
  }

  &.has-only-one-job {
    margin-left: 30px;
    margin-right: 0;

    .left-connector {
      @include flat-connector-before;
    }
  }
}

.stage-column.has-downstream {
  margin-right: $linked-project-column-margin;

  &.has-only-one-job:not(:first-child) {
    margin-right: 36px;

    .left-connector {
      @include flat-connector-before;
    }
  }

  .build {
    &:first-child {
      &::after {
        right: -$linked-project-column-margin;
        width: $linked-project-column-margin;
      }
    }
  }
}

.cross-project-triangle {
  position: absolute;
  top: 48px;
  width: 0;
  height: 0;
  border-bottom: 7px solid transparent;
  border-top: 7px solid transparent;
  border-left: 7px solid $gray-darkest;
  font-size: 0;
  line-height: 0;
  z-index: 10;
}

.project-name-pipeline-id-separator {
  display: inline-block;
  margin: 4px 2px 0;
  font-size: 10px;
  vertical-align: top;
}

.ci-header-container {
  min-height: 55px;

  .text-center {
    padding-top: 12px;
  }

  .header-action-buttons {
    .btn,
    a {
      margin-left: 10px;
    }
  }
}

.pipelines-container .top-area .nav-controls > .btn:last-child {
  float: none;
}

.autodevops-title {
  font-weight: $gl-font-weight-normal;
  line-height: 1.5;
}

.legend-all {
  color: $gl-text-color-secondary;
}

.legend-success {
  color: $green-500;
}
