@import "../../base";
@import "../../overrides/all";

@include govuk-exports("idsk/component/button") {
  $idsk-button-colour: govuk-colour("green", $legacy: #00823b); // sass-lint:disable no-color-literals
  $idsk-button-hover-colour: govuk-colour("dark-green");
  $idsk-button-shadow-colour: govuk-shade($idsk-button-colour, 60%);
  $idsk-button-text-colour: govuk-colour("white");

  // Secondary button variables
  $idsk-secondary-button-colour: govuk-colour("lighter-grey", $legacy: "grey-4");
  $idsk-secondary-button-hover-colour: govuk-colour("light-grey");
  $idsk-secondary-button-shadow-colour: govuk-shade($idsk-secondary-button-colour, 40%);
  $idsk-secondary-button-text-colour: govuk-colour("black");

  // Warning button variables
  $idsk-warning-button-colour: govuk-colour("red");
  $idsk-warning-button-hover-colour: govuk-colour("dark-red");
  $idsk-warning-button-shadow-colour: govuk-shade($idsk-warning-button-colour, 60%);
  $idsk-warning-button-text-colour: govuk-colour("white");

  // Because the shadow (s0) is visually 'part of' the button, we need to reduce
  // the height of the button to compensate by adjusting its padding (s1) and
  // increase the bottom margin to include it (s2).
  $button-shadow-size: $govuk-border-width-form-element;

  .idsk-button {
    @include govuk-font($size: 19, $line-height: 19px);

    box-sizing: border-box;
    display: inline-block;
    position: relative;
    width: 100%;
    margin-top: 0;
    @include govuk-responsive-margin(6, "bottom", $adjustment: $button-shadow-size); // s2
    padding: (govuk-spacing(2) - $govuk-border-width-form-element) govuk-spacing(2)
      (govuk-spacing(2) - $govuk-border-width-form-element - ($button-shadow-size / 2)); // s1
    border: $govuk-border-width-form-element solid transparent;
    border-radius: 0;
    color: $idsk-button-text-colour;
    background-color: $idsk-button-colour;
    box-shadow: 0 $button-shadow-size 0 $idsk-button-shadow-colour; // s0
    text-align: center;
    vertical-align: top;
    cursor: pointer;
    -webkit-appearance: none;

    @include govuk-if-ie8 {
      border-bottom: $button-shadow-size solid $idsk-button-shadow-colour;
    }

    @include govuk-media-query($from: tablet) {
      width: auto;
    }

    // Ensure that any global link styles are overridden
    &:link,
    &:visited,
    &:active,
    &:hover {
      color: $idsk-button-text-colour;
      text-decoration: none;
    }

    // Fix unwanted button padding in Firefox
    &::-moz-focus-inner {
      padding: 0;
      border: 0;
    }

    &:hover {
      background-color: $idsk-button-hover-colour;
    }

    &:active {
      // Bump the button down so it looks like its being pressed in
      top: $button-shadow-size;

      @include govuk-if-ie8 {
        border-bottom-width: 0;
      }
    }

    &:focus {
      border-color: $idsk-focus-colour;
      // When colours are overridden, for example when users have a dark mode,
      // backgrounds and box-shadows disappear, so we need to ensure there's a
      // transparent outline which will be set to a visible colour.
      // Since Internet Explorer 8 does not support box-shadow, we want to force the user-agent outlines
      @include govuk-not-ie8 {
        outline: $govuk-focus-width solid transparent;
      }
      // Since Internet Explorer does not support `:not()` we set a clearer focus style to match user-agent outlines.
      @include govuk-if-ie8 {
        color: $idsk-focus-text-colour;
        background-color: $idsk-focus-colour;

        // If there is a svg icon inside the button
        svg,
        svg path {
          fill: $govuk-focus-text-colour;
        }
      }
      box-shadow: inset 0 0 0 1px $idsk-focus-colour;
    }

    &:focus:not(:active):not(:hover) {
      border-color: $idsk-focus-colour;
      color: $idsk-focus-text-colour;
      background-color: $idsk-focus-colour;
      box-shadow: 0 2px 0 $idsk-focus-text-colour;

      // If there is a svg icon inside the button
      svg,
      svg path {
        fill: $govuk-focus-text-colour;
      }
    }

    // The following adjustments do not work for <input type="button"> as
    // non-container elements cannot include pseudo elements (i.e. ::before).

    // Use a pseudo element to expand the click target area to include the
    // button's shadow as well, in case users try to click it.
    &::before {
      content: "";
      display: block;

      position: absolute;

      top: -$govuk-border-width-form-element;
      right: -$govuk-border-width-form-element;
      bottom: -($govuk-border-width-form-element + $button-shadow-size);
      left: -$govuk-border-width-form-element;

      background: transparent;
    }

    // When the button is active it is shifted down by $button-shadow-size to
    // denote a 'pressed' state. If the user happened to click at the very top
    // of the button, their mouse is no longer over the button (because it has
    // 'moved beneath them') and so the click event is not fired.
    //
    // This corrects that by shifting the top of the pseudo element so that it
    // continues to cover the area that the user originally clicked, which means
    // the click event is still fired.
    //
    // 🎉
    &:active::before {
      top: -($govuk-border-width-form-element + $button-shadow-size);
    }
  }

  .idsk-button--disabled,
  .idsk-button[disabled="disabled"],
  .idsk-button[disabled] {
    opacity: (0.5);

    &:hover {
      background-color: $idsk-button-colour;
      cursor: default;
    }

    &:focus {
      outline: none;
    }

    &:active {
      top: 0;
      box-shadow: 0 $button-shadow-size 0 $idsk-button-shadow-colour; // s0
      @include govuk-if-ie8 {
        border-bottom: $button-shadow-size solid $idsk-button-shadow-colour; // s0
      }
    }
  }

  .idsk-button--secondary {
    background-color: $idsk-secondary-button-colour;
    box-shadow: 0 $button-shadow-size 0 $idsk-secondary-button-shadow-colour;

    &,
    &:link,
    &:visited,
    &:active,
    &:hover {
      color: $idsk-secondary-button-text-colour;
    }

    // alphagov/govuk_template includes a specific a:link:focus selector
    // designed to make unvisited links a slightly darker blue when focussed, so
    // we need to override the text colour for that combination of selectors so
    // so that unvisited links styled as buttons do not end up with dark blue
    // text when focussed.
    @include govuk-compatibility(govuk_template) {
      &:link:focus {
        color: $idsk-secondary-button-text-colour;
      }
    }

    &:hover {
      background-color: $idsk-secondary-button-hover-colour;

      &[disabled] {
        background-color: $idsk-secondary-button-colour;
      }
    }
  }

  .idsk-button--warning {
    background-color: $idsk-warning-button-colour;
    box-shadow: 0 $button-shadow-size 0 $idsk-warning-button-shadow-colour;

    &,
    &:link,
    &:visited,
    &:active,
    &:hover {
      color: $idsk-warning-button-text-colour;
    }

    // alphagov/govuk_template includes a specific a:link:focus selector
    // designed to make unvisited links a slightly darker blue when focussed, so
    // we need to override the text colour for that combination of selectors so
    // so that unvisited links styled as buttons do not end up with dark blue
    // text when focussed.
    @include govuk-compatibility(govuk_template) {
      &:link:focus {
        color: $idsk-warning-button-text-colour;
      }
    }

    &:hover {
      background-color: $idsk-warning-button-hover-colour;

      &[disabled] {
        background-color: $idsk-warning-button-colour;
      }
    }
  }

  .idsk-button--start {
    @include govuk-typography-weight-bold;
    @include govuk-typography-responsive($size: 24, $override-line-height: 1);

    display: -webkit-inline-box;

    display: -ms-inline-flexbox;

    display: inline-flex;
    min-height: auto;

    -webkit-box-pack: center;

    -ms-flex-pack: center;

    justify-content: center;
  }

  .idsk-button__start-icon {
    margin-left: govuk-spacing(1);

    @include govuk-media-query($from: desktop) {
      margin-left: govuk-spacing(2);
    }
    vertical-align: middle;
    -ms-flex-negative: 0;
    flex-shrink: 0;
    -ms-flex-item-align: center;
    align-self: center;
  }

  @if $govuk-use-legacy-font {
    // Begin adjustments for font baseline offset when using v1 of nta
    $offset: 2;

    .idsk-button {
      padding-top: (govuk-spacing(2) - $govuk-border-width-form-element - ($button-shadow-size / 2) + $offset); // s1
      padding-bottom: (
        govuk-spacing(2) - $govuk-border-width-form-element - ($button-shadow-size / 2) - $offset + 1
      ); // s1
    }

    .idsk-button__start-icon {
      margin-top: -3px;
    }
  }
}
