// scss-lint:disable QualifyingElement

// Common form controls
//
// Shared size and type resets for form controls. Apply `.form-control` to any
// of the following form controls:
//
// select
// textarea
// input[type="text"]
// input[type="password"]
// input[type="datetime"]
// input[type="datetime-local"]
// input[type="date"]
// input[type="month"]
// input[type="time"]
// input[type="week"]
// input[type="number"]
// input[type="email"]
// input[type="url"]
// input[type="search"]
// input[type="tel"]
// input[type="color"]

.form-control {
  // Customize the `:focus` state to imitate native WebKit styles.
  @include form-control-focus();

  display: block;
  width: 100%;
  height: $input-height-base; // Make inputs at least the height of their button counterpart (base line-height + padding + border)
  padding: $padding-base-vertical $padding-base-horizontal;
  font-size: $font-size-base;
  line-height: $line-height-base-ratio;
  color: $input-color;
  background-color: $input-bg;
  background-image: none; // Reset unusual Firefox-on-Android default style; see https://github.com/necolas/normalize.css/issues/214
  border: 1px solid $input-border;
  transition: border-color ease-in-out .15s;


  // Placeholder
  &::placeholder { color: $input-placeholder-color; }

  // Disabled and read-only inputs
  //
  // HTML5 says that controls under a fieldset > legend:first-child won't be
  // disabled if the fieldset is disabled. Due to implementation difficulty, we
  // don't honor that edge case; we style them as disabled anyway.
  &[disabled],
  fieldset[disabled] & {
    cursor: not-allowed;
    background-color: $input-disabled-bg;
    opacity: 1; // iOS fix for unreadable disabled content
  }

  &[readonly] {
    background-color: $input-disabled-bg;
    cursor: default;
  }

  .has-error & {
    background-color: $input-error-bg;
  }

  // [converter] extracted textarea& to textarea.form-control
}

// Reset height for `textarea`s
textarea.form-control {
  height: auto;
}

// Reset shadow for `select`s
select.form-control {
  box-shadow: none;

  appearance: none;
}

.control-label {
  display: block;
  font-weight: $label-font-weight;
}


// Form groups
//
// Designed to help with the organization and spacing of vertical forms. For
// horizontal forms, use the predefined grid classes.

.form-group {
  margin-bottom: $line-height-base;

  // Character counter wrapper class
  &.with-character-counter {
    position: relative;

    .character-counter {
      position: absolute;
      top: .5em;
      right: 0;
    }
  }

  // Character counter class
  .character-counter {
    font-size: $character-counter-font-size;
    font-weight: $font-weight-base;
    line-height: $character-counter-line-height;
    opacity: $text-muted-opacity;
  }
}


// Checkboxes and radios
//
// Indent the labels to position radios/checkboxes as hanging controls.

.radio,
.checkbox {
  position: relative;
  display: block;
  min-height: $line-height-base; // clear the floating input if there is no label text
  margin-top: 10px;
  margin-bottom: 10px;

  label {
    padding-left: $input-check-padding;
    margin-bottom: 0;
    font-weight: $font-weight-base;
    cursor: pointer;
  }
}

.radio input[type="radio"],
.radio-inline input[type="radio"],
.checkbox input[type="checkbox"],
.checkbox-inline input[type="checkbox"] {
  position: absolute;
  margin-left: -$input-check-padding;
  margin-top: 4px \9;
  cursor: pointer;
}

.radio + .radio,
.checkbox + .checkbox {
  margin-top: -5px; // Move up sibling radios or checkboxes for tighter spacing
}

// Radios and checkboxes on same line
%radio-inline,
.radio-inline,
.checkbox-inline {
  display: inline-block;
  padding-left: $input-check-padding;
  margin-bottom: 0;
  margin-right: $input-check-padding; // space out consecutive inline controls
  vertical-align: middle;
  font-weight: $font-weight-base;
  cursor: pointer;

  &:last-of-type {
    margin-right: 0;
  }
}

// Apply same disabled cursor tweak as for inputs
// Some special care is needed because <label>s don't inherit their parent's `cursor`.
//
// Note: Neither radios nor checkboxes can be readonly.
input[type="radio"],
input[type="checkbox"] {
  &[disabled],
  &.disabled,
  fieldset[disabled] & {
    cursor: not-allowed;
  }
}

// These classes are used directly on <label>s
.radio-inline,
.checkbox-inline {
  &.disabled,
  fieldset[disabled] & {
    cursor: not-allowed;
  }
}

// These classes are used on elements with <label> descendants
.radio,
.checkbox {
  &.disabled,
  fieldset[disabled] & {
    label {
      cursor: not-allowed;
    }
  }
}


// Static form control text
//
// Apply class to a `p` element to make any string of text align with labels in
// a horizontal form layout.

.form-control-static {
  // Size it appropriately next to real form controls
  padding-top: $padding-base-vertical + rem(1px);
  padding-bottom: $padding-base-vertical + rem(1px);
  // Remove default margin from `p`
  margin-bottom: 0;

  &.input-lg,
  &.input-sm {
    padding-left: 0;
    padding-right: 0;
  }
}


// Form control sizing
//
// Build on `.form-control` with modifier classes to decrease or increase the
// height and font-size of form controls.

@include input-size(
  '.input-sm',
  $input-height-small,
  $padding-small-vertical,
  $padding-small-horizontal,
  $font-size-small,
  $line-height-small-ratio
);

@include input-size(
  '.input-lg',
  $input-height-large,
  $padding-large-vertical,
  $padding-large-horizontal,
  $font-size-large,
  $line-height-large-ratio
);

// Special styles for iOS temporal inputs
//
// In Mobile Safari, setting `display: block` on temporal inputs causes the
// text within the input to become vertically misaligned.
// As a workaround, we set a pixel line-height that matches the
// given height of the input. Since this fucks up everything else, we have to
// appropriately reset it for Internet Explorer and the size variations.

input[type="date"].form-control,
input[type="time"].form-control,
input[type="datetime-local"].form-control,
input[type="month"].form-control {
  line-height: $input-height-base;

  &.input-sm {
    line-height: $input-height-small;
  }

  &.input-lg {
    line-height: $input-height-large;
  }
}


// Form control icons
//
// Adds an icon that appears within the input box
.form-control-with-icons {
  position: relative;

  .no-appearance & {
    select.form-control {
      // scss-lint:disable ImportantRule, NestingDepth
      padding-right: inherit !important;

      ~ .icon {
        display: none;
      }
    }
  }

  .icon {
    @include square($input-height-base);

    display: block;
    position: absolute;
    top: 0;
    z-index: 3; // Ensure icon is above input groups

    text-align: center;

    color: $input-icon-color;
    font-size: $font-size-base + $input-icon-font-size-delta;
    line-height: $input-height-base;

    pointer-events: none;
    padding: $padding-base-vertical $padding-base-horizontal;
  }

  .icon:first-child { left: 0; }
  .icon:last-child { right: 0; }

  // Icon on left
  .icon ~ .form-control {
    padding-left:  $input-height-base - .5;
  }

  // Icon on right
  .form-control:not(:last-child) {
    padding-right: $input-height-base - .5;
  }


  &.form-control-with-icons-lg {
    .icon {
      @include square($input-height-large);
      font-size: $font-size-large + $input-icon-font-size-delta;
      line-height: $input-height-large;
    }

    // Icon on left
    .icon ~ .form-control {
      padding-left:  $input-height-large - .5;
    }

    // Icon on right
    .form-control:not(:last-child) {
      padding-right: $input-height-large - .5;
    }
  }


  &.form-control-with-icons-sm {
    .icon {
      @include square($input-height-small);
      font-size: $font-size-small + $input-icon-font-size-delta;
      line-height: $input-height-small;
    }

    // Icon on left
    .icon ~ .form-control {
      padding-left:  $input-height-small - .5;
    }

    // Icon on right
    .form-control:not(:last-child) {
      padding-right: $input-height-small - .5;
    }
  }
}

// Feedback states
.has-success { @include form-control-validation($semantic-color-success, $state-success-bg); }
.has-warning { @include form-control-validation($semantic-color-warning, $state-warning-bg); }
.has-error   { @include form-control-validation($semantic-color-danger,  $state-danger-bg ); }


// Help text
//
// Apply to any element you wish to create light text for placement immediately
// below a form control. Use for general help, formatting, or instructional text.

.help-block {
  display: block; // account for any element using help-block

  margin-top: 5px;
  margin-bottom: 10px;

  opacity: $help-block-opacity;
  font-size: $help-block-font-size;
}



// Inline forms
//
// Make forms appear inline(-block) by adding the `.form-inline` class. Inline
// forms begin stacked on extra small (mobile) devices and then go inline when
// viewports reach <768px.
//
// Requires wrapping inputs and labels with `.form-group` for proper display of
// default HTML form controls and our custom form controls (e.g., input groups).

.form-inline {

  // Kick in the inline
  @media (min-width: $screen-sm-min) {
    // Inline-block all the things for "inline"
    .form-group,
    .radio,
    .checkbox {
      display: inline-block;
      margin-bottom: 0;
      vertical-align: middle;
    }

    // Remove default margin on radios/checkboxes that were used for stacking, and
    // then undo the floating of radios and checkboxes to match (which also avoids
    // a bug in WebKit: https://github.com/twbs/bootstrap/issues/1969).
    .radio,
    .checkbox {
      margin-top: 0;

      label {
        padding-left: 0;
      }

      input[type="radio"],
      input[type="checkbox"] {
        position: relative;
        margin-left: 0;
      }
    }

    .form-control,
    .form-control-with-icons {
      display: inline-block;
      width: auto; // Prevent labels from stacking above inputs in `.form-group`
      vertical-align: middle;
    }

    .input-group {
      display: inline-table;
      vertical-align: middle;

      .input-group-addon,
      .input-group-btn,
      .form-control {
        width: auto;
      }
    }

    // Input groups need that 100% width though
    .input-group > .form-control {
      width: 100%;
    }

    .control-label {
      margin-bottom: 0;
      vertical-align: middle;
    }
  }
}


// Horizontal forms
//
// Horizontal forms are built on grid classes and allow you to create forms with
// labels on the left and inputs on the right.

.form-horizontal {

  // Consistent vertical alignment of radios and checkboxes
  //
  // Labels also get some reset styles, but that is scoped to a media query below.
  .radio,
  .checkbox,
  .radio-inline,
  .checkbox-inline {
    margin-top: 0;
    margin-bottom: 0;
    padding-top: $padding-base-vertical + rem(1px); // Default padding plus a border
  }
  // Account for padding we're adding to ensure the alignment and of help text
  // and other content below items
  .radio,
  .checkbox {
    min-height: $line-height-base + $padding-base-vertical + rem(1px);
  }

  // Make form groups behave like rows
  .form-group {
    @include make-row(); // DEPRECATED: Remove in ≥ 0.23.0
    margin-bottom: $line-height-base / 2;
  }

  // Reset spacing and right align labels, but scope to media queries so that
  // labels on narrow viewports stack the same as a default form example.
  @media (min-width: $screen-sm-min) {
    .control-label {
      display: block; // Can’t text-align right if display is inline

      padding-top: $padding-base-vertical + rem(1px); // Default padding plus a border
      margin-bottom: 0;

      text-align: right;
    }
  }

  // Reposition the icon because it's now within a grid column and columns have
  // `position: relative;` on them. Also accounts for the grid gutter padding.
  .form-control-with-icons .icon {
    right: $grid-gutter-width / 2;
  }

  // Form group sizes
  //
  // Quick utility class for applying `.input-lg` and `.input-sm` styles to the
  // inputs and labels within a `.form-group`.
  .form-group-lg {
    @media (min-width: $screen-sm-min) {
      .control-label {
        padding-top: $padding-large-vertical * $line-height-large-ratio;
      }
    }

    .form-control {
      // scss-lint:disable PlaceholderInExtend
      @extend .input-lg;
    }
  }

  .form-group-sm {
    @media (min-width: $screen-sm-min) {
      .control-label {
        padding-top: $padding-small-vertical + rem(1px);
      }
    }

    .form-control {
      // scss-lint:disable PlaceholderInExtend
      @extend .input-sm;
    }
  }
}

$currency-width: rem(38px);
.form-group.currency {
  .input-group {
    position: relative;
  }
  .currency-add-on {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    left: 1px;
    width: $currency-width;
    height: 96%;

    display: flex;
    justify-content: center;
    align-items: center;

    background-color: ui-color(u-lighter-gray);
  }

  input.form-control {
    padding-left: $padding-base-horizontal + $currency-width;
  }
}
