$input-height: 36px;

.form-group,
fieldset {
  position: relative;
  display: flex;
  flex-direction: column;

  .affixment {
    $border-max-width: 2px;
    position: absolute;
    right: $border-max-width;
    z-index: 1;
    height: 36px - (2 * $border-max-width);
    opacity: 0;
    transition: opacity 200ms linear;
    transform: translateY($border-max-width);

    &.affixed {
      opacity: 1;
    }

    // Reverting for now—this needs to be finessed a bit.
    // &::before {
    //   position: relative;
    //   top: 2px;
    //   left: -36px;
    //   display: block;
    //   width: 36px;
    //   height: calc(100% - 4px);
    //   background-image:
    //     linear-gradient(270deg,
    //     #fff, rgba(#fff, 0.89) 23%,
    //       rgba(#fff, 0.65) 41%,
    //       rgba(#fff, 0.36) 57%,
    //       rgba(#fff, 0.12) 76%,
    //       rgba(#fff, 0)
    //     )
    //   ;
    //   content: '';
    // }
  }

  &:not(:last-child) {
    margin-bottom: margin(2);
  }

  label,
  legend {
    font-size: font-size(0);
    align-self: flex-start;

    &::after {
      color: color(gray);
    }
  }
}

.form-group:not(.inline) {
  .messages {
    display: flex;
    flex-wrap: wrap;
  }
}

.form-group {
  @include vset(--required-text, ' (required)');
  @include vset(--optional-text, ' (optional)');

  label::after {
    font-size: font-size(-1);
  }

  [required] {
    & + label::after {
      content: vget(--required-text);
    }
  }

  :not([required]) {
    & + label::after {
      content: vget(--optional-text);
    }
  }

  .read-only {
    &[disabled] {
      & ~ label::after {
        content: none;
      }
    }
  }

  .optional-suppressed,
  .required-suppressed {
    &[required] {
      & + label::after {
        content: '';
      }
    }

    &:not([required]) {
      & + label::after {
        content: '';
      }
    }
  }

  .helper-text {
    color: color(gray);
  }

  .validation-message {
    color: color(error);
  }

  .helper-text,
  .validation-message {
    font-size: font-size(-1);
  }

  &.inline {
    @include vset(--required-text, '(required)');
    @include vset(--optional-text, '(optional)');

    flex-direction: row;
    align-items: center;

    &:first-child {
      margin-top: margin(2);
    }

    .helper-text,
    label {
      flex: 0 0 auto;
      align-self: center;
    }

    label {
      display: flex;
      flex-direction: column;
      padding-right: ms(-4);
    }

    .helper-text {
      padding-left: ms(-4);
    }

    .messages {
      display: flex;
      flex-direction: column;
      flex: 0 0 auto;
      padding-left: ms(-4);
      order: last;

      .helper-text {
        padding-left: 0;
      }

      .helper-text,
      .validation-message {
        align-self: flex-start;
      }
    }

    :focused {
      overflow: auto;
    }
  }

  &.inline-with-icon {
    input {
      outline: none;
      border: none;

      & ~ * {
        max-height: $input-height;
        border: none;
      }

      & ~ .button {
        &:hover {
          background: transparent;
          box-shadow: none;
        }
      }
    }

    .inline-wrapper {
      display: flex;
      flex: 1;
      border: 1px solid color(gray, plus1);
    }

    .inline-wrapper:focus-within {
      outline: 2px solid color(primary);
      border-color: transparent;

      &.invalid {
        outline-color: color(error);
      }

      input {
        outline: none;
      }
    }

    &.inline-with-icon-stacked {
      flex-wrap: wrap;

      input {
        // Must keep unit on zero-value for flex-basis here to avoid an IE bug.
        // See https://github.com/Pier1/rocketbelt/pull/182.
        flex: 1 0 0px; // sass-lint:disable-line zero-unit
        min-width: 0;
        width: auto;
      }

      label {
        flex-direction: row;
        width: 100%;
        align-items: baseline;

        &::after {
          margin-left: 0.25em;
        }
      }

      .button {
        flex: 0 1 auto;
      }

      .messages {
        padding-left: 0;
        width: 100%;
      }
    }
  }
}

html {
  &.touchevents {
    input,
    select {
      min-height: 44px;
    }
  }
}

input:not([type='checkbox']),
select {
  min-height: $input-height;
}

input {
  &[type='radio'],
  &[type='checkbox'] {
    &:focus {
      ~ label {
        outline: thin dotted color(primary);
      }
    }
  }

  &.rb-checkbox[type='checkbox'],
  &[type='radio'] {
    &[disabled] + label {
      color: color(gray, plus2);
    }
  }
}

input:not([type='radio']),
input:not([type='checkbox']),
select,
textarea {
  display: block;
  padding: 0.2rem 0.4rem;
  width: 100%;
  height: $input-height;
  border: 1px solid color(gray, plus1);
  border-radius: 0;
  font-size: font-size(0);

  & + label {
    order: -1;
  }

  &:focus {
    overflow: hidden;
    outline: 2px solid color(primary);

    &.invalid {
      outline-color: color(error);
    }
  }

  &[disabled] {
    border-color: transparent;
    background-color: color(gray, plus3);
  }

  &.invalid {
    border-width: 2px;
    border-color: color(error);

    & ~ .messages {
      .validation-message {
        display: block;
        visibility: visible;
        color: color(error);

        + .helper-text::before {
          color: color(gray);
          content: unicode(nbsp) + unicode('horizontal-bar') + unicode(nbsp);
        }
      }
    }
  }

  &:valid:not(.invalid) {
    & ~ .messages {
      .validation-message {
        display: none;
        visibility: hidden;
      }
    }
  }

  // Character Based Ems - Good Idea??
  &.field-small {
    width: 8em;
  }

  &.field-medium {
    width: 16em;
  }

  &.field-large {
    width: 24em;
  }

  &.read-only {
    &[disabled] {
      margin-bottom: 0;
      padding: 0;
      border-width: 1px;
      border-color: transparent;
      border-left-width: 0;
      background: transparent;
      color: color(gray);
      font-size: 1rem;
      opacity: 1;
    }
  }
}

[disabled] {
  opacity: 0.6;
  filter: grayscale(0.6);
  transition: opacity 500ms ease;
}

select {
  $fill: color(black);

  padding-right: 25px;
  background-color: color(white);
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg aria-labelledby='asrb-icon-chevron-down-desc asrb-icon-chevron-down-title' xmlns='http://www.w3.org/2000/svg' style='fill: black%3B' viewBox='0 0 16 16'%3E%3Ctitle id='asrb-icon-chevron-down-title'%3EDown Chevron%3C/title%3E%3Cdesc id='asrb-icon-chevron-down-desc'%3EA downward chevron%3C/desc%3E%3Cpath d='M8.023 10.681a.356.356 0 01-.232-.085L2.305 5.953a.36.36 0 01.465-.549l5.252 4.444 5.207-4.443a.36.36 0 01.467.547l-5.44 4.643a.36.36 0 01-.233.086' fill-rule='evenodd'/%3E%3C/svg%3E");
  background-position: right 0.5rem center;
  background-size: 1rem;
  background-repeat: no-repeat;
  appearance: none;

  &.field-auto {
    margin-right: auto;
    padding-right: 2em;
    width: auto;
  }
}

textarea {
  &.read-only {
    &[disabled] {
      resize: none;
    }
  }
}

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

  &[required] {
    legend::after {
      content: ' (required)';
    }
  }

  &:not([required]) {
    legend::after {
      content: ' (optional)';
    }
  }
}
