@use "config";
@use "mixins";
@use "variables";
@use "sass:color";
@use "sass:map";
@use "sass:math";

fieldset {
  margin: 0;
  padding: 0;
  border: 0;
}

legend {
  box-sizing: border-box;
  color: inherit;
  display: table;
  max-width: 100%;
  padding: 0;
  white-space: normal;
}

.form {
  $error-color: color.adjust(
    map.get(variables.$error-colors, "negative"),
    $lightness: math.percentage(-(config.$hue-threshold * 2))
  );
  $error-background: map.get(variables.$message-colors, "error");

  --disabled-color: #{color.adjust(
      map.get(variables.$greyscale, "light"),
      $lightness: math.percentage(config.$hue-threshold)
    )};

  $self: &;
  color: color.adjust(
    map.get(variables.$greyscale, "base"),
    $lightness: math.percentage(0.05)
  );

  .input-icons {
    left: 0;
    top: 0;
    position: absolute;
    @include mixins.square(1.5rem);

    [class^="pi-"] {
      display: flex;
      line-height: 1;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);

      &:nth-of-type(2) {
        transform: translate(-50%, -50%) scale(0);
        transition: transform var(--transitions) ease;
      }
    }
  }

  input[type="checkbox"],
  input[type="radio"] {
    opacity: 0;
    position: absolute;
    width: auto;

    &:checked + label {
      .input-icons {
        [class^="pi-"]:nth-of-type(2) {
          transform: translate(-50%, -50%) scale(1);
        }
      }
    }

    + label {
      align-items: center;
      color: map.get(variables.$greyscale, "base");
      cursor: pointer;
      display: flex;
      font-weight: 400;
      margin-right: variables.$spacer;
      margin-top: variables.$spacer * 0.75;
      padding-left: map.get(variables.$spacers, 4);
      position: relative;
      vertical-align: baseline;
    }

    &.error {
      + label {
        color: $error-color;
      }
    }
  }

  input[type="radio"] {
    &:checked + label {
      cursor: default;
    }
  }

  &#{$self}--light {
    color: map.get(variables.$greyscale, "white");

    input[type="text"],
    input[type="search"],
    input[type="date"],
    input[type="email"],
    input[type="textarea"],
    input[type="tel"],
    input[type="password"],
    input[type="number"],
    input[type="time"],
    input[type="url"],
    input[type="datetime-local"],
    input[type="month"],
    input[type="week"],
    textarea,
    select {
      border-bottom-color: map.get(variables.$greyscale, "white");
      color: map.get(variables.$greyscale, "white");

      &::placeholder {
        color: map.get(variables.$greyscale, "white");
      }

      &:focus {
        box-shadow: 0 0 2px 1px map.get(variables.$greyscale, "white");
        outline: none;
      }
    }

    ::-webkit-calendar-picker-indicator {
      filter: invert(1);
    }

    &.form--rounded,
    &.form--bordered {
      input[type="text"],
      input[type="search"],
      input[type="date"],
      input[type="email"],
      input[type="textarea"],
      input[type="tel"],
      input[type="password"],
      input[type="number"],
      input[type="time"],
      input[type="url"],
      input[type="datetime-local"],
      input[type="month"],
      input[type="week"],
      textarea,
      select {
        border-color: map.get(variables.$greyscale, "white");
      }
    }

    input[type="checkbox"],
    input[type="radio"] {
      + label {
        color: map.get(variables.$greyscale, "white");

        &:before {
          border: 1px solid;
          border-color: inherit;
        }
      }
    }

    input[type="checkbox"] {
      + label {
        &:after {
          color: map.get(variables.$greyscale, "white");
        }
      }
    }

    textarea {
      border-color: map.get(variables.$greyscale, "white");
    }

    #{ $self }__select-wrapper {
      &:after {
        border-color: map.get(variables.$greyscale, "white");
      }
    }
  }

  &#{$self}--bordered {
    input[type="text"],
    input[type="search"],
    input[type="date"],
    input[type="email"],
    input[type="textarea"],
    input[type="tel"],
    input[type="password"],
    input[type="number"],
    input[type="time"],
    input[type="url"],
    input[type="datetime-local"],
    input[type="month"],
    input[type="week"],
    textarea,
    select {
      border-color: map.get(variables.$greyscale, "light");
      border-radius: map.get(config.$radius, "default");
    }
  }

  &__field {
    display: block;

    input[type="text"],
    input[type="search"],
    input[type="date"],
    input[type="email"],
    input[type="textarea"],
    input[type="tel"],
    input[type="password"],
    input[type="number"],
    input[type="time"],
    input[type="url"],
    input[type="datetime-local"],
    input[type="month"],
    input[type="week"],
    textarea,
    #{ $self }__select-wrapper,
    #{ $self }__option-group {
      margin-top: map.get(variables.$spacers, 2);
      width: 100%;
    }

    &.has-icon {
      position: relative;

      input[type="text"],
      input[type="search"],
      input[type="date"],
      input[type="email"],
      input[type="textarea"],
      input[type="tel"],
      input[type="password"],
      input[type="number"],
      input[type="time"],
      input[type="url"],
      input[type="datetime-local"],
      input[type="month"],
      input[type="week"],
      textarea,
      select {
        padding-left: map.get(variables.$spacers, 5);
      }

      i {
        bottom: 0;
        font-size: map.get(variables.$font-scale, "md");
        left: 0.5rem;
        line-height: 1;
        position: absolute;
        transform: translateY(-50%);
      }

      &--right {
        position: relative;

        i {
          bottom: 0;
          color: map.get(config.$brand-colors, "med-blue");
          font-size: map.get(variables.$font-scale, "md");
          line-height: 1;
          pointer-events: none;
          position: absolute;
          transform: translateY(-50%);
          right: 0.5rem;
        }

        input[type="text"],
        input[type="search"],
        input[type="email"],
        input[type="textarea"],
        input[type="tel"],
        input[type="password"],
        input[type="url"],
        textarea,
        select {
          padding-right: map.get(variables.$spacers, 4);
        }

        input[type="date"],
        input[type="time"] {
          &::-webkit-calendar-picker-indicator,
          &::-webkit-inner-spin-button {
            opacity: 0;
          }
        }
      }

      input[type="date"],
      input[type="time"] {
        &::-webkit-calendar-picker-indicator,
        &::-webkit-inner-spin-button {
          opacity: 0;
        }
      }
    }

    &.error {
      color: $error-color !important;

      input[type="text"],
      input[type="search"],
      input[type="date"],
      input[type="email"],
      input[type="textarea"],
      input[type="tel"],
      input[type="password"],
      input[type="number"],
      input[type="time"],
      input[type="url"],
      input[type="datetime-local"],
      input[type="month"],
      input[type="week"],
      textarea,
      select {
        background-color: $error-background;
        border-color: $error-color !important;
        border-radius: map.get(config.$radius, "default");
        padding-left: map.get(variables.$spacers, 2);
      }

      input[type="checkbox"],
      input[type="radio"] {
        &:not(:checked) + label {
          color: $error-color;

          [class^="pi-"] {
            color: $error-color !important;
          }
        }
      }
    }
  }

  &#{$self}--fancy {
    #{$self}__field {
      position: relative;

      &.error {
        label {
          color: $error-color !important;
          font-size: 80%;
          top: -1rem;
          transform: translate(0) scale(1);
          left: 0;
        }

        input:not([type="checkbox"]),
        input:not([type="radio"]),
        textarea {
          background-color: $error-background;
          border: 1px solid $error-color !important;
          box-shadow: 0 0 2px 0 $error-color !important;
        }

        ::-webkit-input-placeholder,
        :-moz-placeholder {
          opacity: 1;
          transition: inherit;
        }
      }

      input:not([type="checkbox"]),
      input:not([type="radio"]),
      textarea {
        background: transparent;
        border: none;
        border-bottom: 1px solid map.get(variables.$greyscale, "light");
        border-radius: 0;
        color: map.get(variables.$greyscale, "base");
        padding: map.get(variables.$spacers, 1);
        transition: var(--transitions) ease-in;

        &:focus ~ label,
        &:valid ~ label {
          font-size: 80%;
          top: -1rem;
          left: 0;
        }

        &:focus {
          border-bottom-color: map.get(
            config.$brand-colors,
            "med-blue"
          ) !important;
          outline: none !important;
        }
      }

      input::-webkit-datetime-edit {
        color: transparent;
      }

      input::-moz-datetime-edit {
        color: transparent;
      }

      input:focus::-webkit-datetime-edit,
      input:valid::-webkit-datetime-edit {
        color: map.get(variables.$greyscale, "base");
      }

      ::-webkit-input-placeholder {
        /* WebKit browsers */
        color: transparent !important;
      }
      ::-moz-placeholder {
        /* Mozilla Firefox 19+ */
        color: transparent !important;
      }

      input::placeholder {
        color: transparent !important;
      }

      ::-moz-placeholder {
        color: transparent !important;
      }

      input:focus::-webkit-input-placeholder {
        color: map.get(variables.$greyscale, "base") !important;
      }

      input:focus::-moz-placeholder {
        color: map.get(variables.$greyscale, "base") !important;
      }

      input:focus + label {
        transform: translate(0, 0) scale(1);
        cursor: pointer;
      }

      #{ $self }__select-wrapper {
        position: static;
      }

      select {
        padding-left: map.get(variables.$spacers, 1);

        &:focus ~ label,
        &:valid ~ label {
          font-size: 80%;
          top: -1rem;
          left: 0;
        }

        &:focus {
          border-bottom-color: map.get(
            config.$brand-colors,
            "med-blue"
          ) !important;
          outline: none !important;
        }
      }

      label {
        color: map.get(variables.$greyscale, "light");
        pointer-events: none;
        position: absolute;
        left: 0.25rem;
        line-height: 1;
        top: 50%;
        transition: var(--transitions) ease-in;
        transform: translateY(-50%);
      }

      input[type="text"],
      input[type="search"],
      input[type="date"],
      input[type="email"],
      input[type="textarea"],
      input[type="tel"],
      input[type="password"],
      input[type="number"],
      input[type="time"],
      input[type="url"],
      input[type="datetime-local"],
      input[type="month"],
      input[type="week"],
      textarea,
      #{ $self }__select-wrapper,
      #{ $self }__option-group {
        margin-top: map.get(variables.$spacers, 3) + 0.75;
      }
    }
  }

  &__option-group {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    &.option-group--block {
      display: block;
    }
    &.option-group--overflow {
      max-height: 20vh;
      overflow: auto;
    }
  }

  &__button-group {
    align-items: flex-end;
    display: flex;
    justify-content: flex-start;
    margin-left: -0.25rem;
    width: calc(100% + 0.25rem * 2);

    button {
      flex-grow: 1;
      margin: 0.25rem;
      max-width: 14.375rem;
    }
  }

  input[type="text"],
  input[type="search"],
  input[type="date"],
  input[type="email"],
  input[type="textarea"],
  input[type="tel"],
  input[type="password"],
  input[type="number"],
  input[type="time"],
  input[type="url"],
  input[type="datetime-local"],
  input[type="month"],
  input[type="week"],
  textarea,
  select {
    background-color: transparent;
    border: 1px solid transparent;
    border-bottom-color: map.get(variables.$greyscale, "light");
    border-radius: 0; // specify no border-radius to override mobile user agent styles
    color: map.get(variables.$greyscale, "base");
    display: block;
    font-size: 1rem;
    min-height: 2.2rem;
    padding: map.get(variables.$spacers, 1) map.get(variables.$spacers, 2);
    width: 100%;

    &::placeholder {
      color: map.get(variables.$greyscale, "base");
    }

    &:focus {
      outline: none;
      border-bottom-color: map.get(config.$site-colors, "focus");
      box-shadow: 0 0 2px 0 map.get(config.$site-colors, "focus");
      transition: box-shadow var(--transitions),
        border-color var(--transitions) ease-in-out,
        border-width var(--transitions) ease;
    }
  }

  input[type="text"],
  input[type="search"],
  input[type="email"],
  input[type="textarea"],
  input[type="tel"],
  input[type="password"],
  input[type="number"],
  input[type="url"],
  input[type="datetime-local"],
  input[type="month"],
  input[type="week"] {
    &.readonly {
      transition: var(--transitions);

      &[readonly] {
        border-bottom-color: transparent;
        pointer-events: none;
      }
    }
  }

  textarea {
    border-color: map.get(variables.$greyscale, "light");
    padding: map.get(variables.$spacers, 1);
    width: 100%;

    &.readonly {
      transition: var(--transitions);

      &[readonly] {
        border-color: transparent;
        pointer-events: none;
        resize: none;
      }
    }
  }

  &__select-wrapper {
    position: relative;

    &:after {
      border: 1px solid map.get(variables.$greyscale, "base");
      border-width: 0 0 1px 1px;
      content: "";
      height: 0.3rem;
      pointer-events: none;
      position: absolute;
      right: 0.3rem;
      top: 50%;
      transform: rotate(-45deg) translateY(-50%);
      width: 0.3rem;
    }

    .has-icon &,
    .has-icon--right & {
      &:after {
        display: none;
      }
    }
  }

  select {
    -webkit-appearance: none;
    appearance: none;
    border-radius: 0;
    padding: map.get(variables.$spacers, 1) variables.$spacer
      map.get(variables.$spacers, 1) map.get(variables.$spacers, 2);
  }

  input[type="date"],
  input[type="time"],
  input[type="datetime-local"] {
    appearance: textfield;
    display: inline-flex;

    &::-webkit-clear-button {
      margin-right: -0.75rem;
    }
  }

  // form--rounded
  &--rounded {
    input[type="text"],
    input[type="search"],
    input[type="date"],
    input[type="email"],
    input[type="textarea"],
    input[type="tel"],
    input[type="password"],
    input[type="number"],
    input[type="time"],
    input[type="url"],
    input[type="datetime-local"],
    input[type="month"],
    input[type="week"],
    textarea,
    select {
      border-color: map.get(variables.$greyscale, "light");
      border-radius: map.get(config.$radius, "button");
    }

    .has-icon {
      i {
        left: 0.75rem;
      }
    }

    .has-icon--right {
      i {
        right: 0.75rem;
      }
    }

    #{$self}__select-wrapper {
      &:after {
        right: 0.7rem;
      }
    }
  }

  // Disabled styles for all inputs
  input,
  select,
  textarea {
    &:disabled {
      color: var(--disabled-color) !important;
      cursor: not-allowed;

      + i[class*="pi-"] {
        color: var(--disabled-color) !important;
      }
    }
  }

  input[type="checkbox"],
  input[type="radio"] {
    &:disabled {
      + label {
        color: var(--disabled-color) !important;
        cursor: not-allowed;
      }
    }
  }
}

.toggle {
  border: 1px solid map.get(variables.$greyscale, "light");
  border-radius: map.get(config.$radius, "button");
  display: flex;
  justify-content: space-between;
  margin-top: map.get(variables.$spacers, 2);
  min-height: 2.2rem;
  padding: map.get(variables.$spacers, 1);

  &__content {
    display: flex;
    flex: 1 1 auto;
  }

  input[type="checkbox"],
  input[type="radio"] {
    opacity: 0;
    position: absolute;
    width: auto;

    &:checked + label {
      .input-icons {
        [class^="pi-"]:nth-of-type(2) {
          transform: translate(-50%, -50%) scale(1);
        }
      }
    }

    + label {
      align-items: center;
      border-radius: map.get(config.$radius, "button");
      color: map.get(variables.$greyscale, "base");
      display: flex;
      flex: 1 1 auto;
      justify-content: center;
      margin: 0;
      padding: 0 map.get(variables.$spacers, 2);
      padding-left: variables.$spacer * 1.3;
      position: relative;

      [class^="pi-"] {
        display: flex;
        height: auto;
        left: 2px;
        opacity: 0;
        position: absolute;
        top: 50%;
        transform: translate(30%, -50%);
        visibility: hidden;
        width: auto;
      }
    }

    &:checked + label {
      background-color: map.get(config.$site-colors, "link");
      color: map.get(variables.$greyscale, "white");
      font-weight: inherit;

      [class^="pi-"] {
        opacity: 1;
        visibility: visible;
      }
    }
  }
}

@mixin toggle-disabled {
  pointer-events: none;

  input[type="checkbox"],
  input[type="radio"] {
    &:checked + label {
      background-color: var(--disabled-color) !important;
      border-color: var(--disabled-color) !important;
      color: map.get(variables.$greyscale, "white") !important;
    }
  }
}

fieldset:disabled {
  cursor: not-allowed;

  legend {
    cursor: default;
  }

  .toggle {
    @include toggle-disabled;
  }

  input[type="checkbox"],
  input[type="radio"] {
    + label {
      color: var(--disabled-color) !important;
      cursor: not-allowed;
    }
  }
}

// Switch
.switch {
  input[type="checkbox"],
  input[type="radio"] {
    opacity: 0;
    position: absolute;
    width: auto;
  }

  label {
    background-color: var(--lighter);
    border: 1px solid
      color.adjust(
        map.get(variables.$greyscale, "light"),
        $lightness: math.percentage(config.$hue-threshold * 2)
      );
    border-radius: 5rem;
    display: flex;
    height: 1rem;
    padding: 0;
    position: relative;
    transition: var(--transitions) ease-in-out;
    transition-property: background-color, border-color;
    width: 2rem;

    &:before {
      background-color: var(--lighter);
      border: 1px solid
        color.adjust(
          map.get(variables.$greyscale, "light"),
          $lightness: math.percentage(config.$hue-threshold * 2)
        );
      border-radius: 50%;
      box-shadow: 0 0 2px 0
        color.adjust(
          map.get(variables.$greyscale, "light"),
          $lightness: math.percentage(config.$hue-threshold * 2)
        );
      content: "";
      height: 1.25rem;
      width: 1.25rem;
      left: -20%;
      top: 50%;
      transform: translateY(-50%);
      transition: var(--transitions) ease-in-out;
      position: absolute;
      z-index: 1;
    }
  }

  input:checked {
    + label {
      background-color: var(--skyblue);
      border-color: transparent;

      &:before {
        background-color: var(--med-blue);
        border-color: transparent;
        box-shadow: 0 0 2px 0 var(--med-blue);
        left: -10%;
        transform: translate(100%, -50%);
      }
    }
  }
}

[role="radio"],
[role="checkbox"] {
  position: relative;
  transition: var(--transitions);
  width: 100%;

  i {
    left: 0.5rem;
    position: absolute;
    transform: scale(0);
    transition: transform var(--transitions) ease;
  }

  &[aria-checked="true"] {
    i {
      transform: scale(1);
    }
  }
}

input[type="checkbox"].pill-filter,
input[type="radio"].pill-filter {
  + label {
    background-color: map.get(variables.$greyscale, "white");
    border-radius: map.get(config.$radius, "button");
    color: map.get(config.$site-colors, "link");
    border: 1px solid map.get(config.$site-colors, "link");
    height: auto;
    justify-content: center;
    margin: 0.25rem;
    padding: map.get(variables.$spacers, 2) 1.5rem;
    transition: var(--transitions);
    i {
      font-size: map.get(variables.$font-scale, "xs");
    }

    &:before {
      display: none;
    }

    &:after {
      color: map.get(variables.$greyscale, "white");
      left: 0.5rem;
      opacity: 0;
      top: 50%;
      transform: translateY(-50%);
      transition: visibility var(--transitions) ease,
        opacity var(--transitions) ease;
      visibility: hidden;
    }
  }

  &:checked + label {
    background-color: map.get(config.$site-colors, "link");
    font-weight: 400;
    color: map.get(variables.$greyscale, "white");

    &:after {
      opacity: 1;
      visibility: visible;
    }
  }
}

// range
.slidecontainer {
  margin-bottom: 1rem;
  width: 100%;
  @supports not (-ms-high-contrast: none) {
    // targeting averything *but* IE //

    $border-radius: 3rem;
    .slider {
      appearance: none;
      background-color: map.get(config.$brand-colors, "med-blue");
      border-radius: $border-radius;
      width: 100%;
      height: 0.5rem;
      margin: 0;
      outline: none;
      transition: var(--transitions);

      &:hover {
        opacity: 0.7;
      }
      &::-webkit-slider-thumb {
        appearance: none;
        border: 1px solid;
        border-color: map.get(config.$brand-colors, "med-blue");
        border-radius: $border-radius;
        background-color: map.get(variables.$greyscale, "white");
        cursor: pointer;
        @include mixins.square(1.3rem);
      }
      &::-moz-range-thumb {
        background-color: map.get(variables.$greyscale, "white");
        cursor: pointer;
        @include mixins.square(25px);
      }
    }
  }
}
