$tooltip-background: rgba(color(black), .85);
$tooltip-background-light: color(white);

.tooltip {
  position: relative;

  &.form-group {
    display: flex;
  }

  &::before,
  &::after,
  .tooltip-content {
    position: absolute;
    visibility: hidden;
    opacity: 0;
    transition: opacity 250ms linear, visibility 250ms linear, transform 250ms cubic-bezier(0.71, 1.7, 0.77, 1.24);
    transform: translate(0, 0);
  }

  // Base styles for the tooltip's directional arrow
  &::before {
    z-index: 1001;
    border: 8px solid transparent;
    background: transparent;
    content: '';
  }

  &::after {
    z-index: 1002;
    border: 8px solid transparent;
    background: transparent;
    content: '';
  }

  // Base styles for the tooltip's content area
  .tooltip-content {
    @include z-depth(1);
    z-index: 1000;
    padding: ms(-2);
    min-width: 150px;
    border-radius: 2px;
    background-color: $tooltip-background;
    color: color(white);
    text-align: left;
    text-transform: none;
    letter-spacing: normal;
    font-size: ms(-2);
    line-height: 1.2;
  }

  &:hover,
  &:focus {
    &::before,
    .tooltip-content {
      visibility: visible;
      opacity: 1;
    }
  }

  &.tooltip-no-hover:hover {
    &::before,
    .tooltip-content {
      visibility: hidden;
      opacity: 0;
    }

    :focus {
      &::before,
      & ~ .tooltip-content {
        visibility: visible;
        opacity: 1;
      }
    }
  }
}

.tooltip {
  &.tooltip-focus-only {
    &:hover {
      .tooltip-content {
        visibility: hidden;
        opacity: 0;
      }

      &::before,
      &::after {
        display: none;
        visibility: hidden;
      }
    }

    input:focus,
    select:focus {
      & ~ .tooltip-content {
        visibility: visible;
        opacity: 1;
        transform: translate(-50%, 14px);

        &::before,
        &::after {
          position: absolute;
          bottom: 0;
          left: 50%;
          border: 8px solid transparent;
          background: transparent;
          content: '';
          transform: translate(-50%, 6px);
        }

        &::before {
          z-index: 1001;
          margin-bottom: -12px;
          border-top-color: rgba(0, 0, 0, .12);
        }

        &::after {
          z-index: 1002;
          margin-bottom: -9px;
          border-top-color: $tooltip-background-light;
        }
      }
    }
  }
}

// Top (default)
.tooltip,
.tooltip-top {
  &::before,
  &::after,
  .tooltip-content {
    bottom: 100%;
    left: 50%;
    transform: translate(-50%, 6px);
  }

  &::before {
    margin-bottom: -11px;
    border-top-color: $tooltip-background;
  }

  &::after {
    margin-bottom: -9px;
    border-top-color: $tooltip-background-light;
  }

  .tooltip-content {
    margin-bottom: 4px;
  }

  &:hover,
  &:focus {
    &::before,
    &::after,
    .tooltip-content {
      transform: translate(-50%, -6px);
    }
  }
}

.tooltip-left {
  &::before,
  &::after,
  .tooltip-content {
    right: 100%;
    bottom: 50%;
    left: auto;
    transform: translate(6px, 50%);
  }

  &::before {
    margin-right: -11px;
    margin-bottom: 0;
    margin-left: 0;
    border-top-color: transparent;
    border-left-color: $tooltip-background;
  }

  &::after {
    margin-right: -10px;
    margin-bottom: 0;
    margin-left: 0;
    border-top-color: transparent;
    border-left-color: $tooltip-background-light;
  }

  .tooltip-content {
    margin-right: 4px;
    margin-bottom: 0;
  }

  &:hover,
  &:focus {
    &::before,
    &::after,
    .tooltip-content {
      transform: translate(-6px, 50%);
    }
  }
}

.tooltip-right {
  &::before,
  &::after,
  .tooltip-content {
    bottom: 50%;
    left: 100%;
    transform: translate(-6px, 50%);
  }

  &::before {
    margin-bottom: 0;
    margin-left: -11px;
    border-top-color: transparent;
    border-right-color: $tooltip-background;
  }

  &::after {
    margin-bottom: 0;
    margin-left: -10px;
    border-top-color: transparent;
    border-right-color: $tooltip-background-light;
  }

  .tooltip-content {
    margin-bottom: 0;
    margin-left: 4px;
  }

  &:hover,
  &:focus {
    &::before,
    &::after,
    .tooltip-content {
      transform: translate(6px, 50%);
    }
  }
}

.tooltip-bottom {
  &::before,
  &::after,
  .tooltip-content {
    top: 100%;
    bottom: auto;
    left: 50%;
    transform: translate(-50%, -6px);
  }

  &::before {
    margin-top: -11px;
    margin-bottom: 0;
    border-top-color: transparent;
    border-bottom-color: $tooltip-background;
  }

  &::after {
    margin-top: -10px;
    margin-bottom: 0;
    border-top-color: transparent;
    border-bottom-color: $tooltip-background-light;
  }

  .tooltip-content {
    margin-top: 4px;
    margin-bottom: 0;
  }

  &:hover,
  &:focus {
    &::before,
    &::after,
    .tooltip-content {
      transform: translate(-50%, 6px);
    }
  }
}

.tooltip-md {
  .tooltip-content {
    min-width: 326px;
  }
}

.tooltip-lg {
  .tooltip-content {
    min-width: 400px;
  }
}

.tooltip-inverted {
  .tooltip-content {
    border: 1px solid rgba(0, 0, 0, 0.06);
    background-color: $tooltip-background-light;
    color: color(black);
  }

  &::before {
    margin-bottom: -12px;
    border: 8px solid transparent;
    border-top-color: rgba(0, 0, 0, .12);
  }

  &:hover,
  &:focus {
    &::after {
      visibility: visible;
      opacity: 1;
    }
  }

  &.tooltip-left {
    &::before {
      margin: 0;
      margin-right: -12px;
      border-top-color: transparent;
      border-left-color: rgba(0, 0, 0, .1);
    }

    &::after {
      border-top-color: transparent;
    }
  }

  &.tooltip-right {
    &::before {
      margin: 0;
      margin-left: -12px;
      border-top-color: transparent;
      border-right-color: rgba(0, 0, 0, .1);
    }

    &::after {
      border-top-color: transparent;
    }
  }
  &.tooltip-bottom {
    &::before {
      margin: 0;
      margin-top: -12px;
      border-top-color: transparent;
      border-bottom-color: rgba(0, 0, 0, .07);
    }

    &::after {
      border-top-color: transparent;
    }
  }
}
