// Checkboxes
//================================================== //
@use 'sass:list';

@mixin checkbox-animation {
  @include animation(check 0.15s);
}

@mixin checkbox-transform-rules {
  @include css3(transform-origin, left top 0);
}

@mixin checkbox-unchecked-before-style {
  background-color: $checkbox-color-unchecked-initial-background;
  border: 1px solid $checkbox-color-unchecked-initial-border;
  border-radius: 2px;
  content: '';
  height: 14px;
  margin-left: -25px;
  margin-top: 0;
  position: absolute;
  width: 14px;
}

@mixin checkbox-unchecked-disabled-before-style {
  background-color: $checkbox-color-unchecked-disabled-background;
  border-color: $checkbox-color-unchecked-disabled-border;
}

@mixin checkbox-checked-before-style {
  background-color: $checkbox-color-checked-initial-background;
  border-color: $checkbox-color-checked-initial-background;
}

@mixin checkbox-checked-disabled-before-style {
  background-color: $checkbox-color-checked-disabled-background;
  border-color: $checkbox-color-checked-disabled-background;
}

@mixin checkbox-checked-after-style {
  @include css3(transform, scaleX(-1) rotate(135deg));

  border-color: $checkbox-color-checked-initial-arrow $checkbox-color-checked-initial-arrow $checkbox-color-checked-initial-arrow transparent;
  border-style: solid;
  border-width: 2px 2px 0 0;
  content: '';
  height: 10px;
  left: 2px;
  position: absolute;
  top: 13px;
  width: 4px;
}

@mixin checkbox-checked-disabled-after-style {
  border-color: $checkbox-color-unchecked-disabled-arrow $checkbox-color-unchecked-disabled-arrow $checkbox-color-unchecked-disabled-arrow transparent;
}

@mixin checkbox-focus-state {
  box-shadow: 0 0 0 3px $input-color-focus-border;
  outline: solid 2px $checkbox-focus-border-color;
}

@mixin checkbox-hover-state {
  @include css3(transition, border-color 300ms ease);

  border-color: $checkbox-color-unchecked-hover-border;
}

@mixin checkbox-partial-after-style {
  border-width: 0 0 2px;
  margin-left: 1px;
  top: 1px;
  width: 10px;

  @include css3(transform, none);
}

input.checkbox,
span.checkbox > input {
  height: 14px;
  left: 0;
  opacity: 0;
  position: absolute;
  top: 0;
  width: 16px;
  z-index: -1;

  &.error {
    + label {
      &::before {
        border: 1px solid $error-color;
      }
    }

    &:focus:not(.hide-focus) {
      + label {
        &::before {
          border-color: $error-color !important;
          box-shadow: 0 0 0 3px $error-color !important;
        }
      }
    }
  }
}

label.inline .checkbox ~ .label-text,
input.checkbox + label,
input.checkbox + input[type='hidden'] + label,
span.checkbox > label {
  @include no-select();

  color: $label-color;
  cursor: pointer;
  display: inline-block;
  font-size: $ids-size-font-base;
  margin-bottom: 0;
  padding-left: 25px;
  position: relative;
}

label.inline .checkbox ~ .label-text::before,
input.checkbox + label::before,
input.checkbox + input[type='hidden'] + label::before,
span.checkbox > label::before {
  @include checkbox-unchecked-before-style;
}

label.inline .checkbox:checked ~ .label-text::before,
input.checkbox:checked + label::before,
input.checkbox:checked + input[type='hidden'] + label::before,
span.checkbox > input:checked + label::before {
  @include checkbox-checked-before-style;
}

label.inline .checkbox:checked ~ .label-text::after,
input.checkbox:checked + label::after,
input.checkbox:checked + input[type='hidden'] + label::after,
span.checkbox > input:checked + label::after {
  @include checkbox-checked-after-style;

  top: 9px;
}

span.checkbox > input + label {
  line-height: 16px;
}

label.inline .checkbox.partial:checked ~ .label-text::after,
input.checkbox.partial:checked + label::after,
input.checkbox.partial:checked + input[type='hidden'] + label::after,
span.checkbox.partial > input:checked + label::after {
  @include checkbox-partial-after-style;

  top: -3px;
}

// Disabled State
label.inline .checkbox:disabled ~ .label-text,
input.checkbox:disabled + label,
input.checkbox:disabled + input[type='hidden'] + label,
span.checkbox > input:disabled + label {
  color: $checkbox-color-disabled-font;
  cursor: default;
}

label.inline .checkbox:disabled:not(:checked) ~ .label-text::before,
input.checkbox:disabled:not(:checked) + label::before,
input.checkbox:disabled:not(:checked) + input[type='hidden'] + label::before,
span.checkbox > input:disabled:not(:checked) + label::before {
  @include checkbox-unchecked-disabled-before-style;
}

label.inline .checkbox:disabled:checked ~ .label-text::before,
input.checkbox:disabled:checked + label::before,
input.checkbox:disabled:checked + input[type='hidden'] + label::before,
span.checkbox > input:disabled:checked + label::before {
  @include checkbox-checked-disabled-before-style;
}

label.inline .checkbox:disabled:checked ~ .label-text::after,
input.checkbox:disabled:checked + label::after,
input.checkbox:disabled:checked + input[type='hidden'] + label::after,
span.checkbox > input:disabled:checked + label::after {
  @include checkbox-checked-disabled-after-style;
}

// Focus State
label.inline .checkbox:focus:not(.hide-focus) ~ .label-text::before,
input.checkbox:focus:not(.hide-focus) + label::before,
input.checkbox:focus:not(.hide-focus) + input[type='hidden'] + label::before,
span.checkbox > input:focus:not(.hide-focus) + label::before {
  @include checkbox-focus-state;
}

// Hover State
label.inline .checkbox:not(:checked):not(.error):not(:disabled):hover ~ .label-text::before,
input.checkbox:not(:checked):not(.error):not(:disabled):hover + label::before,
input.checkbox:not(:checked):not(.error):not(:disabled):hover + input[type='hidden'] + label::before,
span.checkbox > input:not(:checked):not(.error):not(:disabled):hover + label::before {
  @include checkbox-hover-state;
}

// Checkboxes with hitboxes
.field-checkbox.field-hitbox {
  span.dirty-checkbox.is-checked {
    left: 13px;
    top: 14px !important;
    position: absolute;
  }
}

input.checkbox ~ label.checkbox-label {
  &.hitbox {
    display: inline-flex;
    align-items: center;
    vertical-align: middle;
    min-width: 44px;
    height: 44px;

    &.hitbox-padding {
      padding-right: 14px;
    }

    &::before {
      margin-left: -12px;
      margin-right: 12px;
      position: relative;
    }

    &::after {
      left: 15px;
      top: 50%;
    }
  }
}

input.checkbox.partial:checked + label.checkbox-label {
  &.hitbox {
    &::after {
      top: 10px;
    }
  }
}

// Labels
label.inline .checkbox ~ .label-text,
.checkbox-label {
  display: inline-block;
  line-height: 17px;
  margin-bottom: 13px;
}

.is-firefox {
  label.inline .checkbox ~ .label-text,
  .checkbox-label {
    display: inline-block;
    line-height: 17px;
  }
}

.ios,
.is-firefox {
  .field-checkbox .icon-dirty {
    left: 2px;
    top: 1px;
  }
}

@include keyframes(check) {
  0% {
    height: 0;
    width: 0;
  }

  50% {
    height: 0;
    width: 4px;
  }

  100% {
    height: 10px;
    width: 4px;
  }
}

label.inline .checkbox ~ .label-text::after,
span.checkbox > input:checked + label::after,
.checkbox ~ label::after {
  @include checkbox-transform-rules;
}

label.inline .checkbox:not(.partial):checked ~ .label-text::after,
span.checkbox > input:checked + label::after,
.checkbox:not(.partial):checked ~ label::after {
  @include checkbox-animation;
}

.checkbox-group-label {
  @include antialiased();

  display: block;
  font-size: $ids-size-font-sm;
  margin-bottom: 12px;

  ~ .field {
    margin-bottom: 12px;
  }

  ~ .field-short,
  .form-layout-compact .field,
  .form-layout-compact .field-checkbox {
    margin-bottom: 3px;
  }

  + .compound-field {
    margin-top: -8px;
  }
}

// Print Support
@media print {
  label.inline .checkbox ~ .label-text,
  .checkbox + label::before {
    content: none;
  }

  input.checkbox,
  span.checkbox > input {
    left: auto;
    position: static;
    width: auto;
  }

  label.inline .checkbox ~ .label-text,
  input.checkbox + label,
  input.checkbox + input[type='hidden'] + label,
  span.checkbox > label {
    padding-left: 8px;
  }

  label.inline .checkbox ~ .label-text::before,
  input.checkbox + label::before,
  input.checkbox + input[type='hidden'] + label::before,
  span.checkbox > label::before {
    content: none;
    margin-left: 0;
  }
}

label.inline:not(.checkbox-label) .checkbox ~ .label-text {
  margin: 0 auto 3px -5px;
}

// RTL Styles
html[dir='rtl'] {
  label.inline .checkbox:checked ~ .label-text::after,
  input.checkbox:checked + label::after,
  input.checkbox:checked + input[type='hidden'] + label::after,
  span.checkbox > input:checked + label::after {
    left: auto;
    right: 13px;
  }

  label.inline .checkbox ~ .label-text,
  input.checkbox + label,
  input.checkbox + input[type='hidden'] + label,
  span.checkbox > label {
    padding-left: inherit;
    padding-right: 32px;
  }

  label.inline .checkbox ~ .label-text::before,
  input.checkbox + label::before,
  input.checkbox + input[type='hidden'] + label::before,
  span.checkbox > label::before {
    margin-left: inherit;
    margin-right: -27px;
  }

  label.inline .checkbox.partial:checked ~ .label-text::after,
  input.checkbox.partial:checked + label::after,
  input.checkbox.partial:checked + input[type='hidden'] + label::after,
  span.checkbox.partial > input:checked + label::after {
    right: 8px;
  }

  .dirty-checkbox {
    inset-inline-end: 0;
  }

  .field-short,
  .form-layout-compact .field,
  .form-layout-compact .field-checkbox {
    .dirty-checkbox {
      right: 6px;
    }

    label.inline .checkbox ~ .label-text,
    input.checkbox + label,
    input.checkbox + input[type='hidden'] + label,
    span.checkbox > label {
      padding-right: 21px;
    }

    label.inline .checkbox ~ .label-text::before,
    input.checkbox + label::before,
    input.checkbox + input[type='hidden'] + label::before,
    span.checkbox > label::before {
      margin-right: -21px;
    }

    label.inline .checkbox:checked ~ .label-text::after,
    input.checkbox:checked + label::after,
    input.checkbox:checked + input[type='hidden'] + label::after,
    span.checkbox > input:checked + label::after {
      right: 8px;
    }
  }

  input.checkbox,
  span.checkbox > input {
    left: auto;
    right: -99999px;

    + .checkbox-label {
      span.required {
        margin-right: 5px;
      }
    }

  }
}

// Short Fields
.field-short,
.form-layout-compact .field,
.form-layout-compact .field-checkbox {
  // Reduce Checkbox Size
  .checkbox-label,
  .checkbox > label {
    font-size: $input-size-compact-font-size;
    padding-left: 21px;
  }

  label.inline .checkbox ~ .label-text::before,
  input.checkbox + label::before,
  input.checkbox + input[type='hidden'] + label::before,
  span.checkbox > label::before {
    margin-left: -21px;
    margin-top: -1px;
  }

  input.checkbox + label::before {
    top: 1px;
  }

  input.checkbox {
    padding: 0;
  }

  label.inline .checkbox ~ .label-text,
  .checkbox-label,
  input.checkbox + label,
  input.checkbox + input[type='hidden'] + label,
  span.checkbox > label {
    padding-top: 0;
    width: auto;
  }

  label.inline .checkbox:checked ~ .label-text::after,
  input.checkbox:checked + label::after,
  input.checkbox:checked + input[type='hidden'] + label::after,
  span.checkbox > input:checked + label::after {
    left: 2px;
    top: 8px;
  }

  .icon-dirty.dirty-checkbox {
    left: auto;
    margin: 1px 0 0 1px;
  }
}

// Colored Checkboxes
@each $name in $color-names {
  $i: list.index($color-names, $name);
  $color: list.nth($color-vars, $i);

  input.checkbox.#{$name} + label::before {
    background-color: $color;
    border-color: $color;
  }
}

// Dirty Checkbox
.compound-field {
  > .field.field-checkbox {
    .dirty-checkbox {
      margin: 10px 0 0 -1px;
    }
  }
}
