@use 'sass:math';
@use 'uswds-core' as *;

$checkbox-size: 0.875rem;
$input-select-margin-right: 1;

.usa-checkbox__label {
  @include u-line-height($theme-form-font-family, $theme-body-line-height);
  display: inline-block;
  margin-bottom: units(1);
  padding-left: $checkbox-size + units($input-select-margin-right);

  &::before {
    background-color: color('primary-lightest');
    box-shadow: inset 0 0 0 1px color('primary');
    height: $checkbox-size;
    margin-left: 0;
    margin-right: 0;
    margin-top: math.div(
      line-height($theme-form-font-family, $theme-body-line-height) *
        font-size($theme-form-font-family, $theme-body-font-size) - $checkbox-size,
      2
    );
    width: $checkbox-size;
  }
}

.usa-checkbox__input:disabled + .usa-checkbox__label {
  color: color('disabled');
  border-color: color('disabled');
}

.usa-checkbox__input:disabled + .usa-checkbox__label::before {
  box-shadow: inset 0 0 0 1px color('disabled');
}

.usa-checkbox__input:checked:disabled + .usa-checkbox__label::before {
  background-color: color('disabled');
  box-shadow: inset 0 0 0 1px color('disabled');
  background-size: 0.5rem auto;
}

.usa-checkbox__input:checked + .usa-checkbox__label::before {
  box-shadow: inset 0 0 0 1px color('primary');
  background-size: 0.5rem auto;
}

.usa-checkbox__input.usa-focus + .usa-checkbox__label::before,
.usa-checkbox__input:focus + .usa-checkbox__label::before {
  @include focus-outline();
}

.usa-checkbox__input:checked.usa-focus + .usa-checkbox__label::before,
.usa-checkbox__input:checked:focus + .usa-checkbox__label::before {
  @include focus-outline();
}

.usa-checkbox__input--tile + .usa-checkbox__label {
  @include u-text('primary', 'bold');
  @include u-display('block');
  @include u-padding(3);
  padding-left: units(3) + $checkbox-size + $input-select-margin-right;
  border-color: color($theme-input-tile-border-color);
  max-width: units($theme-input-max-width);
  font-size: 1.125rem;

  &::before {
    left: units(3);
    margin-top: math.div(
      line-height($theme-form-font-family, $theme-body-line-height) * 1.125rem - $checkbox-size,
      2
    );
  }
}

.usa-checkbox__input--tile:disabled + .usa-checkbox__label--illustrated .usa-checkbox__image {
  filter: invert(45%) sepia(1%) saturate(0%) hue-rotate(232deg) brightness(100%) contrast(80%);
}

.usa-checkbox__input--tile + .usa-checkbox__label--illustrated {
  align-items: flex-start;

  @include at-media('tablet') {
    display: flex;
  }

  .usa-checkbox__image {
    width: 1.5rem;
    float: left;
    margin-right: 0.5rem;
    @include at-media('tablet') {
      width: 2.625rem;
      padding-top: 0.25rem;
    }
  }

  .usa-checkbox__label--text {
    @include at-media('tablet') {
      flex: 1;
      padding-left: 0.5rem;
    }
  }
}

.usa-checkbox__input--bordered + .usa-checkbox__label {
  @include u-display('table'); // Margin collapse of a "block" while retaining collapsed width.
  border: units($theme-input-bordered-border-width) solid color($theme-input-bordered-border-color);
  border-radius: radius($theme-input-bordered-border-radius);
  margin: units(1) 0;
  max-width: units($theme-input-max-width);
  padding: units(1) units(2) units(1) #{units(2) + $checkbox-size +
    units($input-select-margin-right)};

  &::before {
    left: units(2);
  }
}

.usa-checkbox__input--bordered:checked + .usa-checkbox__label {
  background-color: color($theme-input-bordered-background-color-selected);
  border-color: color($theme-input-bordered-border-color-selected);
}

.usa-checkbox__input--bordered:checked:disabled + .usa-checkbox__label {
  background-color: transparent;
  border-color: color('disabled');
}

.usa-checkbox__input--tile:checked + .usa-checkbox__label {
  background-color: color($theme-input-tile-background-color-selected);
  border-color: color($theme-input-tile-border-color-selected);
}

.usa-checkbox__label-description {
  @include typeset($theme-body-font-family, $theme-body-font-size, $theme-body-line-height);
  @include u-text('ink', 'normal');
}
