$mediumButtonSize: componentSize(m);
$smallButtonSize: componentSize(s);
$largeButtonSize: componentSize(l);

@mixin buttonHover($color1, $color2, $percent) {
  &:hover:not([disabled]),
  &:active:not([disabled]) {
    @media (hover: hover) and (pointer: fine) {
      @include sgButtonHoverMix($color1, $color2, $percent);
    }
  }
}

@mixin buttonIconColor($color) {
  & .sg-button__icon {
    color: $color;
  }
}

.sg-button {
  @include applyFocus();
  @include applyPressEffect();

  position: relative;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  background-color: $black;
  height: $mediumButtonSize;
  border-radius: 20px;
  border: none;
  white-space: nowrap;
  text-decoration: none;
  font-size: 15px;
  line-height: 15px;
  font-weight: bold;
  color: $white;
  text-transform: uppercase;
  padding: 0 20px;
  box-sizing: border-box;
  touch-action: manipulation;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  user-select: none;
  cursor: default;

  &[href] {
    cursor: pointer;
  }

  &:disabled {
    @media (forced-colors: active) {
      color: GrayText;
    }
  }

  &__text {
    position: relative;
    display: flex;
    align-items: center;
  }

  &__icon {
    display: inline-flex;
    margin-right: spacing(xs);
    margin-left: -4px;
    position: relative;

    .sg-button--reversed-order & {
      margin-right: -4px;
      margin-left: spacing(xs);
    }

    &--s {
      margin-right: 6px;
      margin-left: -4px;

      .sg-button--reversed-order & {
        margin-right: -4px;
        margin-left: 6px;
      }
    }

    &--l {
      margin-right: 12px;
      margin-left: -6px;

      .sg-button--reversed-order & {
        margin-right: -6px;
        margin-left: 12px;
      }
    }
  }

  &__spinner {
    position: absolute;
  }

  &--m {
    height: $mediumButtonSize;
  }

  &--l {
    border-radius: 28px;
    height: $largeButtonSize;
    padding: 0 28px;
    font-size: 21px;
    line-height: 21px;

    .sg-button__hover-overlay {
      border-radius: 28px;
    }
  }

  &--s {
    border-radius: var(--border-radius-m);
    height: $smallButtonSize;
    padding: 0 spacing(s);
    font-size: 12px;
    line-height: 12px;

    .sg-button__hover-overlay {
      border-radius: var(--border-radius-m);
    }

    // Proxima is broken
    // Added to align text in the center for this font-size
    .sg-button__text {
      top: 1px;
    }
  }

  &--reversed-order {
    flex-direction: row-reverse;
  }

  &--disabled {
    opacity: 0.45;
  }

  &--full-width {
    width: 100%;
  }

  &--loading {
    .sg-button__text {
      opacity: 0;
    }

    .sg-button__icon {
      opacity: 0;
    }
  }

  // BUTTON TYPES
  &--solid {
    @include buttonHover($gray-70, $black, 80%);
    background-color: $black;
  }

  &--solid-inverted {
    @include buttonHover($gray-20, $white, 80%);
    background-color: $white;
    color: $black;
  }

  &--solid-indigo {
    @include buttonHover($indigo-60, $indigo-50, 80%);
    background-color: $indigo-50;
  }

  &--solid-indigo-inverted {
    @include buttonHover($gray-20, $white, 80%);
    background-color: $white;
    color: $indigo-50;
  }

  &--solid-light {
    @include buttonHover($gray-50, $gray-20, 12%);
    background-color: $gray-20;
    color: $black;

    &-toggle-red {
      @include buttonHover($red-40, $red-20, 12%);
      @include buttonIconColor($red-50);
      background-color: $red-20;
    }

    &-toggle-yellow {
      @include buttonHover($yellow-40, $yellow-20, 12%);
      @include buttonIconColor($yellow-50);
      background-color: $yellow-20;
    }
  }

  &--outline {
    @include buttonHover($gray-50, $transparent, 12%);
    background-color: $transparent;
    border: 2px solid $black;
    color: $black;

    &-toggle-red {
      @include buttonHover($red-40, $transparent, 12%);
      @include buttonIconColor($red-50);
      border-color: $red-50;
    }

    &-toggle-yellow {
      @include buttonHover($yellow-40, $transparent, 12%);
      @include buttonIconColor($yellow-50);
      border-color: $yellow-50;
    }
  }

  &--outline-indigo {
    @include buttonHover($indigo-50, $transparent, 12%);
    background-color: $transparent;
    border: 2px solid $indigo-50;
    color: $indigo-50;
  }

  &--outline-inverted {
    @include buttonHover($white, $transparent, 12%);
    background-color: $transparent;
    border: 2px solid $white;
    color: $white;
  }

  &--transparent {
    background-color: $transparent;
    color: $black;

    .sg-button__hover-overlay {
      background-color: mix($gray-50, $transparent, 12%);
    }

    &-toggle-red {
      @include buttonIconColor($red-50);

      .sg-button__hover-overlay {
        background-color: mix($red-40, $transparent, 12%);
      }
    }

    &-toggle-yellow {
      @include buttonIconColor($yellow-50);

      .sg-button__hover-overlay {
        background-color: mix($yellow-40, $transparent, 12%);
      }
    }
  }

  &__hover-overlay {
    height: 100%;
    width: 100%;
    position: absolute;
    left: 0;
    top: 0;
    transform: scale(0.5);
    transition: transform $durationModerate2 cubic-bezier(0.3, 0, 1, 0.8),
      opacity $durationModerate1 linear;
    opacity: 0;
    border-radius: 20px;
  }

  &:hover:not([disabled]),
  &:active:not([disabled]) {
    @media (hover: hover) and (pointer: fine) {
      .sg-button__hover-overlay {
        transform: scale(1);
        transition: transform $durationModerate2 cubic-bezier(0.1, 0, 0, 1);
        opacity: 1;
      }
    }
  }

  &--transparent-light {
    background-color: $transparent;
    color: $gray-50;

    .sg-button__hover-overlay {
      background-color: mix($gray-50, $transparent, 12%);
    }

    &-toggle-red {
      @include buttonIconColor($red-50);

      .sg-button__hover-overlay {
        background-color: mix($red-40, $transparent, 12%);
      }
    }

    &-toggle-yellow {
      @include buttonIconColor($yellow-50);

      .sg-button__hover-overlay {
        background-color: mix($yellow-40, $transparent, 12%);
      }
    }
  }

  &--transparent-inverted {
    background-color: $transparent;
    color: $white;

    .sg-button__hover-overlay {
      background-color: mix($white, $transparent, 12%);
    }
  }

  &--transparent-red {
    @include buttonIconColor($red-50);
    background-color: $transparent;
    color: $red-60;

    .sg-button__hover-overlay {
      background-color: mix($red-40, $transparent, 12%);
    }

    &-toggle-red {
      .sg-button__hover-overlay {
        background-color: mix($red-40, $transparent, 12%);
      }
    }
  }

  &--transparent,
  &--transparent-light,
  &--transparent-inverted,
  &--transparent-red {
    padding: 0 10px;

    &.sg-button--l {
      padding: 0 14px;
    }

    &.sg-button--s {
      padding: 0 spacing(xs);
    }

    &.sg-button--reversed-order {
      .sg-button__icon {
        margin-right: -2px;
        margin-left: spacing(xs);

        &--s {
          margin-right: -2px;
          margin-left: 6px;
        }

        &--l {
          margin-right: -3px;
          margin-left: 12px;
        }
      }
    }

    .sg-button__icon {
      margin-left: -2px;

      &--l {
        margin-left: -3px;
      }
    }
  }

  &--facebook {
    background-color: $buttonPrimaryFbColor;
    color: $white;

    &:hover:not([disabled]),
    &:active:not([disabled]) {
      @media (hover: hover) and (pointer: fine) {
        background-color: $buttonPrimaryFbHoverColor;
        border-color: $buttonPrimaryFbHoverColor;
      }
    }
  }

  &--google {
    @include buttonHover($gray-50, $transparent, 12%);
    background-color: $transparent;
    border: 2px solid $black;
    color: $black;
  }

  &--apple {
    @include buttonHover($gray-70, $black, 80%);
    background-color: $black;
  }

  &--icon-only {
    padding: 0;
    width: $mediumButtonSize;
    --pressScale: 0.92;

    &.sg-button--s {
      width: $smallButtonSize;
    }

    &.sg-button--m {
      width: $mediumButtonSize;
    }

    &.sg-button--l {
      width: $largeButtonSize;
    }

    .sg-button__icon {
      margin: 0;
    }

    .sg-button__text {
      // if defined it's available for screen readers
      position: absolute;
      width: 1px;
      height: 1px;
      padding: 0;
      margin: -1px;
      overflow: hidden;
      clip: rect(0, 0, 0, 0);
      border: none;
    }
  }

  @media (forced-colors: active) {
    border: 2px solid ButtonText;
  }
}
