//
// Base styles
//

.fm-group {
  position: relative;
  display: flex;
  flex-wrap: wrap; // For form validation feedback
  align-items: stretch;
  width: 100%;

  > .fm-input,
  > .fm-dropdown,
  > .fm-select,
  > .fm-float {
    position: relative; // For focus state's z-index
    flex: 1 1 auto;
    width: 1%;
    min-width: 0; // https://stackoverflow.com/questions/36247140/why-dont-flex-items-shrink-past-content-size
  }

  // Bring the "active" form control to the top of surrounding elements
  > .fm-input:focus,
  > .fm-float:focus-within {
    z-index: 5;
  }

  // Ensure buttons are always above inputs for more visually pleasing borders.
  // This isn't needed for `.gp-text` since it shares the same border-color
  // as our inputs.
  .btn {
    position: relative;
    z-index: 2;
    border-width: $input-border-width;

    &:focus {
      z-index: 5;
    }
  }

  &.fm-lined {

    > .fm-input,
    > .fm-select,
    > .fm-float,
    > .fm-dropdown > .btn, {
      border-radius: 0 !important;
      border: none;
      padding: $input-padding-y 1px;
      border-bottom: $input-border-width solid $input-border-color;
    }

    > .fm-dropdown > .btn {
      border-bottom: none;
      padding: 0;
      background-position: right 0 center;
    }

    .btn:not(.fm-dropdown > .btn) {
      color: $body-color;
      border-radius: 0 !important;
      border: none;
      border-bottom: $input-border-width solid $input-border-color;
      padding: 1px;

      &:disabled,
      &.disabled {
        opacity: 1;
        color: $input-placeholder-color;
        border-bottom: $input-border-width solid $input-border-color;
      }

      &:hover {
        color: $input-color;
        background-color: $transparent;
      }
    }

    .fm-select {
      background-position: right 1px center;
    }

    .btn:focus {
      box-shadow: none;
    }

    .btn ~ .fm-input,
    .btn ~ .fm-select,
    .btn ~ .fm-dropdown,
    .btn ~ .fm-float {
      padding-left: $btn-padding-x;
    }

    .fm-input ~ .btn,
    .fm-select ~ .btn,
    .fm-dropdown ~ .btn,
    .fm-float ~ .btn {
      padding-left: $btn-padding-x
    }

  }
}


// Textual addons
//
// Serves as a catch-all element for any text or radio/checkbox input you wish
// to prepend or append to an input.

.gp-text {
  display: flex;
  align-items: center;
  padding: $input-group-addon-padding-y $input-group-addon-padding-x;
  @include font-size($input-font-size); // Match inputs
  font-weight: $input-group-addon-font-weight;
  line-height: $input-line-height;
  color: $input-group-addon-color;
  text-align: center;
  white-space: nowrap;
  background-color: $input-group-addon-bg;
  border: $input-border-width solid $input-group-addon-border-color;
  @include border-radius($input-border-radius);
}


// Sizing
//
// Remix the default form control sizing classes into new ones for easier
// manipulation.

.gp-sm > .fm-input,
.gp-sm > .gp-text,
.gp-sm > .btn {
  padding: $input-padding-y-sm $input-padding-x-sm;
  @include font-size($input-font-size-sm);
  @include border-radius($input-border-radius-sm);
}

// Rounded corners
//
// These rulesets must come after the sizing ones to properly override sm and lg
// border-radius values when extending. They're more specific than we'd like
// with the `.fm-group >` part, but without it, we cannot override the sizing.

// stylelint-disable-next-line no-duplicate-selectors
.fm-group {

  &:not(.has-validation) {
    > :not(:last-child):not(.fm-float),
    > .fm-float:not(:last-child) > .fm-input {
      @include border-end-radius(0);
    }
  }

  $validation-messages: "";
  @each $state in map-keys($form-validation-states) {
    $validation-messages: $validation-messages + ":not(." + unquote($state) + "-tooltip)" + ":not(." + unquote($state) + "-feedback)";
  }

  > :not(:first-child)#{$validation-messages} {
    margin-left: -$input-border-width;
    @include border-start-radius(0);
  }

  > .fm-float:not(:first-child) > .fm-input,
  > .fm-float:not(:first-child) > .fm-select {
    @include border-start-radius(0);
  }
}
