:root {
  // global
  --d-button-border-radius: var(--d-border-radius);
  --d-button-transition: none;
  --d-button-border: 1px solid transparent;

  // .btn-default
  --d-button-default-text-color: var(--primary);
  --d-button-default-text-color--hover: var(--secondary);
  --d-button-default-bg-color: var(--primary-low);
  --d-button-default-bg-color--hover: var(--primary-medium);
  --d-button-default-icon-color: var(--primary-high);
  --d-button-default-icon-color--hover: var(--primary-low);

  // .btn-primary
  --d-button-primary-text-color: var(--secondary);
  --d-button-primary-text-color--hover: var(--secondary);
  --d-button-primary-bg-color: var(--tertiary);
  --d-button-primary-bg-color--hover: var(--tertiary-hover);
  --d-button-primary-icon-color: var(--secondary);
  --d-button-primary-icon-color--hover: var(--secondary);

  // .btn-danger and .btn.cancel
  --d-button-danger-text-color: var(--secondary);
  --d-button-danger-text-color--hover: var(--secondary);
  --d-button-danger-bg-color: var(--danger);
  --d-button-danger-bg-color--hover: var(--danger-hover);
  --d-button-danger-icon-color: var(--danger-low);
  --d-button-danger-icon-color--hover: var(--danger-low);

  // .btn-success and .btn.ok
  --d-button-success-text-color: var(--secondary);
  --d-button-success-text-color--hover: var(--secondary);
  --d-button-success-bg-color: var(--success);
  --d-button-success-bg-color--hover: var(--success-hover);
  --d-button-success-icon-color: var(--success-low);
  --d-button-success-icon-color--hover: var(--success-low);

  // .btn-flat
  --d-button-flat-text-color: var(--tertiary);
  --d-button-flat-text-color--hover: var(--primary);
  --d-button-flat-icon-color: var(--primary-low-mid);
  --d-button-flat-icon-color--hover: var(--primary);
  --d-button-flat-bg-color: transparent;
  --d-button-flat-bg-color--hover: var(--d-hover);
  --d-button-flat-bg-color--focus: var(--d-hover);
  --d-button-flat-text-color--disabled: var(--primary);

  // .btn-flat.close
  --d-button-flat-close-text-color: var(--primary-high);
  --d-button-flat-close-bg-color: transparent;
  --d-button-flat-close-icon-color: var(--primary-high);
  --d-button-flat-close-icon-color--hover: var(--primary);

  // .btn-transparent
  --d-button-transparent-text-color: var(--primary);
  --d-button-transparent-text-color--hover: var(--tertiary-hover);
  --d-button-transparent-icon-color: var(--primary-high);
  --d-button-transparent-icon-color--hover: var(--tertiary-hover);
}

/* Base */
@mixin btn(
  $text-color: var(--d-button-default-text-color),
  $bg-color: var(--d-button-default-bg-color),
  $icon-color: var(--d-button-default-icon-color),
  $hover-text-color: var(--d-button-default-text-color--hover),
  $hover-bg-color: var(--d-button-default-bg-color--hover),
  $hover-icon-color: var(--d-button-default-icon-color--hover)
) {
  color: $text-color;
  background-color: $bg-color;
  transition: var(--d-button-transition);
  border: var(--d-button-border);

  &:visited {
    /* covers cases where we add button classes to links */
    color: $text-color;
  }

  .d-icon {
    color: $icon-color;
    margin-right: 0.45em;
    transition: var(--d-button-transition);

    /* For Windows High Contrast (see whcm.scss for more) */
    @media (forced-colors: active) {
      color: ButtonText;
    }
  }

  .d-button-label + .d-icon {
    margin-left: 0.45em;
    margin-right: 0;
  }

  &.no-text {
    .d-icon {
      margin-right: 0;
    }
  }

  @include hover {
    background-color: $hover-bg-color;
    color: $hover-text-color;

    .d-icon {
      color: $hover-icon-color;

      /* For Windows High Contrast (see whcm.scss for more) */
      @media (forced-colors: active) {
        color: Highlight;
      }
    }
  }

  &:focus-visible {
    outline: none;
    background-color: $hover-bg-color;
    color: $hover-text-color;

    .d-icon {
      color: $hover-icon-color;

      @media (forced-colors: active) {
        color: Highlight;
      }
    }
  }

  &[disabled],
  &.disabled {
    cursor: not-allowed;

    &:not(.is-loading) {
      opacity: 0.4;
    }

    &:hover {
      color: $text-color;
      background: $bg-color;

      .d-icon {
        color: $icon-color;
      }
    }
  }

  .loading-container {
    display: none;
    margin: 0 6.75px 0 0;
  }

  &.is-loading {
    &.btn-small {
      .loading-icon {
        font-size: var(--font-down-1);
        margin-right: 0.2em;
      }
    }

    .loading-icon {
      animation:
        rotate-forever 1s infinite linear,
        fadein 1s;
    }
  }
}

/* Generic btn */
.btn {
  @include btn;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  font-weight: normal;
  background-image: linear-gradient(
    to bottom,
    rgb(0, 0, 0, 0),
    rgb(0, 0, 0, 0)
  );
  box-sizing: border-box;
  padding: $vpad $hpad;
  cursor: pointer;
  line-height: normal;
}

/* link button */
.btn-link {
  background: transparent;
  border: 0;
  padding: 0;
  color: var(--tertiary);

  @include hover {
    background-color: transparent;
    color: var(--tertiary-hover);

    .d-icon {
      color: var(--tertiary-hover);

      /* For Windows High Contrast (see whcm.scss for more) */
      @media (forced-colors: active) {
        color: Highlight;
      }
    }
  }

  &:focus-visible {
    color: var(--tertiary);
    background: transparent;

    @include default-focus;
  }

  &[disabled],
  &.disabled {
    color: var(--d-button-transparent-text-color);
    cursor: not-allowed;

    &:not(.is-loading) {
      opacity: 0.4;
    }

    &:hover {
      background: transparent;

      .d-icon {
        color: inherit;
      }
    }
  }
}

/* Default button */
.btn-default {
  border-radius: var(--d-button-border-radius);
}

/* Primary button */
.btn-primary {
  @include btn(
    $text-color: var(--d-button-primary-text-color),
    $bg-color: var(--d-button-primary-bg-color),
    $icon-color: var(--d-button-primary-icon-color),
    $hover-text-color: var(--d-button-primary-text-color--hover),
    $hover-bg-color: var(--d-button-primary-bg-color--hover),
    $hover-icon-color: var(--d-button-primary-icon-color--hover)
  );
  border-radius: var(--d-button-border-radius);
}

/* Danger button */
.btn-danger,
.btn.cancel {
  @include btn(
    $text-color: var(--d-button-danger-text-color),
    $bg-color: var(--d-button-danger-bg-color),
    $icon-color: var(--d-button-danger-icon-color),
    $hover-text-color: var(--d-button-danger-text-color--hover),
    $hover-bg-color: var(--d-button-danger-bg-color--hover),
    $hover-icon-color: var(--d-button-danger-icon-color--hover)
  );
  border-radius: var(--d-button-border-radius);
}

/* Success button */
.btn-success,
.btn.ok {
  @include btn(
    $text-color: var(--d-button-success-text-color),
    $bg-color: var(--d-button-success-bg-color),
    $icon-color: var(--d-button-success-icon-color),
    $hover-text-color: var(--d-button-success-text-color--hover),
    $hover-bg-color: var(--d-button-success-bg-color--hover),
    $hover-icon-color: var(--d-button-success-icon-color--hover)
  );
  border-radius: var(--d-button-border-radius);
}

/* Social buttons */
.btn-social {
  color: #000;
  background: #fff;
  border-radius: var(--d-border-radius);

  &:focus-visible {
    outline: 1px solid #000;
  }

  &[href] {
    color: var(--secondary);
  }

  &::before {
    margin-right: 9px;
    font-size: var(--font-0);
  }

  .d-icon,
  &.btn:hover .d-icon {
    color: #000;
  }

  &.cas {
    .d-icon {
      color: var(--cas);
    }

    &:hover {
      .d-icon {
        color: var(--cas);
      }
    }
  }

  &.twitter {
    .d-icon {
      color: var(--twitter);
    }

    &:hover {
      .d-icon {
        color: var(--twitter);
      }
    }
  }

  &.github {
    .d-icon {
      color: var(--github);
    }

    &:hover {
      .d-icon {
        color: var(--github);
      }
    }
  }

  &.discord {
    .d-icon {
      color: var(--discord);
    }

    &:hover {
      .d-icon {
        color: var(--discord);
      }
    }
  }

  &.discourse_id {
    background-color: var(--discourse_id-background);
    color: var(--discourse_id-text);

    .d-icon {
      color: var(--discourse_id-icon);
    }

    &:hover {
      background-color: var(--discourse_id-background-hover);
      color: var(--discourse_id-text-hover);

      .d-icon {
        color: var(--discourse_id-icon-hover);
      }
    }
  }

  /* https://developers.facebook.com/docs/facebook-login/userexperience/#buttondesign
  if you are unable to use Facebook blue, revert to black and white. */
}

/* Button Sizes */

/* Small */
.btn-small {
  font-size: var(--font-down-1);
  min-height: 20px;
}

/* Large */
.btn-large {
  font-size: var(--font-up-1);
}

/* Bonus Buttons */
.btn-flat {
  @include btn(
    $text-color: var(--d-button-default-text-color),
    $bg-color: var(--d-button-flat-bg-color),
    $icon-color: var(--d-button-flat-icon-color),
    $hover-text-color: var(--d-button-flat-text-color--hover),
    $hover-bg-color: var(--d-button-flat-bg-color--hover),
    $hover-icon-color: var(--d-button-flat-icon-color--hover)
  );
  border-radius: var(--d-button-border-radius);

  .discourse-no-touch & {
    &:focus-visible {
      background: var(--d-button-flat-bg-color--focus);
    }
  }

  &.close {
    background: var(--d-button-flat-close-bg-color);

    .d-icon {
      color: var(--d-button-flat-close-icon-color);
    }

    .discourse-no-touch & {
      &:hover,
      &:focus-visible {
        background: var(--d-button-flat-close-bg-color);

        .d-icon {
          color: var(--d-button-flat-close-icon-color--hover);
        }
      }
    }

    &:focus-visible {
      background: var(--d-button-flat-close-bg-color);

      .d-icon {
        color: var(--d-button-flat-close-icon-color--hover);
      }
    }
  }

  &:focus-visible {
    outline: none;
    background: var(--primary-low);

    .d-icon {
      color: var(--primary);
    }
  }

  &.back-button {
    margin-bottom: 1em;
  }
}

@mixin btn-colors($btn-type) {
  color: var(--d-button-#{$btn-type}-bg-color);

  .d-icon {
    color: currentcolor;
  }

  &:focus-visible {
    color: var(--d-button-#{$btn-type}-bg-color--hover);
  }

  .discourse-no-touch & {
    &:hover {
      color: var(--d-button-#{$btn-type}-bg-color--hover);
    }
  }
}

.btn-transparent {
  &,
  &.btn-default, // preferably NOT used in conjuction with btn-default
  &.btn-icon,
  &.no-text {
    @include btn(
      $text-color: var(--d-button-transparent-text-color),
      $bg-color: transparent,
      $icon-color: var(--d-button-transparent-icon-color),
      $hover-text-color: var(--d-button-transparent-text-color--hover),
      $hover-bg-color: transparent,
      $hover-icon-color: currentcolor
    );
    border: 0;

    &:focus-visible {
      background: transparent;
      color: var(--d-button-transparent-text-color--hover);

      .d-icon {
        color: currentcolor;
      }
    }
  }

  &.btn-primary {
    @include btn-colors("primary");
  }

  &.btn-danger {
    @include btn-colors("danger");
  }

  &.btn-success {
    @include btn-colors("success");
  }
}

.btn-mini-toggle {
  border-radius: var(--d-border-radius);
  padding: 0.4em 0.467em;

  .d-icon {
    color: var(--primary-medium);
  }

  @include hover {
    .d-icon {
      color: var(--primary);
    }
  }
}
