.popover {
  // Our parent element can be arbitrary since tooltips are by default inserted as a sibling of their target element.
  // So reset our font and text properties to avoid inheriting weird values.
  @include reset-text();
  @include font-size($popover-font-size);
  @include border-radius($popover-border-radius);
  @include box-shadow($popover-box-shadow);
  background-clip: padding-box;
  background-color: $popover-bg;
  border: $popover-border-width solid $popover-border-color;
  display: block;
  left: 0;
  max-width: $popover-max-width;
  position: absolute;
  top: 0;
  // Allow breaking very long words so they don't overflow the popover's bounds
  word-wrap: break-word;
  z-index: $zindex-popover;

  .arrow {
    display: block;
    height: $popover-arrow-height;
    margin: 0 $popover-border-radius;
    position: absolute;
    width: $popover-arrow-width;

    &::before,
    &::after {
      border-color: transparent;
      border-style: solid;
      content: "";
      display: block;
      position: absolute;
    }
  }
}

.bs-popover-top {
  margin-bottom: $popover-arrow-height;

  > .arrow {
    bottom: subtract(-$popover-arrow-height, $popover-border-width);

    &::before {
      border-top-color: $popover-arrow-outer-color;
      border-width: $popover-arrow-height ($popover-arrow-width / 2) 0;
      bottom: 0;
    }

    &::after {
      border-top-color: $popover-arrow-color;
      border-width: $popover-arrow-height ($popover-arrow-width / 2) 0;
      bottom: $popover-border-width;
    }
  }
}

.bs-popover-right {
  margin-left: $popover-arrow-height;

  > .arrow {
    height: $popover-arrow-width;
    left: subtract(-$popover-arrow-height, $popover-border-width);
    margin: $popover-border-radius 0; // make sure the arrow does not touch the popover's rounded corners
    width: $popover-arrow-height;

    &::before {
      border-right-color: $popover-arrow-outer-color;
      border-width: ($popover-arrow-width / 2) $popover-arrow-height ($popover-arrow-width / 2) 0;
      left: 0;
    }

    &::after {
      border-right-color: $popover-arrow-color;
      border-width: ($popover-arrow-width / 2) $popover-arrow-height ($popover-arrow-width / 2) 0;
      left: $popover-border-width;
    }
  }
}

.bs-popover-bottom {
  margin-top: $popover-arrow-height;

  > .arrow {
    top: subtract(-$popover-arrow-height, $popover-border-width);

    &::before {
      border-bottom-color: $popover-arrow-outer-color;
      border-width: 0 ($popover-arrow-width / 2) $popover-arrow-height ($popover-arrow-width / 2);
      top: 0;
    }

    &::after {
      border-bottom-color: $popover-arrow-color;
      border-width: 0 ($popover-arrow-width / 2) $popover-arrow-height ($popover-arrow-width / 2);
      top: $popover-border-width;
    }
  }

  // This will remove the popover-header's border just below the arrow
  .popover-header::before {
    border-bottom: $popover-border-width solid $popover-header-bg;
    content: "";
    display: block;
    left: 50%;
    margin-left: -$popover-arrow-width / 2;
    position: absolute;
    top: 0;
    width: $popover-arrow-width;
  }
}

.bs-popover-left {
  margin-right: $popover-arrow-height;

  > .arrow {
    height: $popover-arrow-width;
    margin: $popover-border-radius 0; // make sure the arrow does not touch the popover's rounded corners
    right: subtract(-$popover-arrow-height, $popover-border-width);
    width: $popover-arrow-height;

    &::before {
      border-left-color: $popover-arrow-outer-color;
      border-width: ($popover-arrow-width / 2) 0 ($popover-arrow-width / 2) $popover-arrow-height;
      right: 0;
    }

    &::after {
      border-left-color: $popover-arrow-color;
      border-width: ($popover-arrow-width / 2) 0 ($popover-arrow-width / 2) $popover-arrow-height;
      right: $popover-border-width;
    }
  }
}

.bs-popover-auto {
  &[x-placement^="top"] {
    @extend .bs-popover-top;
  }

  &[x-placement^="right"] {
    @extend .bs-popover-right;
  }

  &[x-placement^="bottom"] {
    @extend .bs-popover-bottom;
  }

  &[x-placement^="left"] {
    @extend .bs-popover-left;
  }
}

// Offset the popover to account for the popover arrow
.popover-header {
  @include font-size($font-size-base);
  @include border-top-radius($popover-inner-border-radius);
  background-color: $popover-header-bg;
  border-bottom: $popover-border-width solid darken-color(--athens, 5%);
  color: $popover-header-color;
  margin-bottom: 0; // Reset the default from Reboot
  padding: $popover-header-padding-y $popover-header-padding-x;

  &:empty {
    display: none;
  }
}

.popover-body {
  color: $popover-body-color;
  padding: $popover-body-padding-y $popover-body-padding-x;
}
