@use 'sass:map';
@use '../config' as *;

textarea {
  &[cols] {
    height: auto;
  }
}

// Form label
.col-form-label,
.form-label {
  --form-label-required-gap: #{$form-label-required-gap};

  display: block;
  font-weight: var(--font-weight-medium);

  &.required {
    &::after {
      margin-inline-start: var(--form-label-required-gap);
      color: var(--red);
      content: '*';
    }
  }
}

.form-label-description {
  float: inline-end;
  font-weight: var(--font-weight-normal);
  color: $form-secondary-color;
}

// Form select
.form-select {
  &:-moz-focusring {
    color: var(--body-color);
  }
}

// Form control
.form-control {
  &:-webkit-autofill {
    color: var(--body-color);
    box-shadow: 0 0 0 1000px var(--bg-surface-secondary) inset;
    -webkit-text-fill-color: var(--body-color);
  }

  &:disabled,
  &.disabled {
    color: var(--control-disabled-color);
    user-select: none;
  }

  &[size] {
    width: auto;
  }
}

.form-control-light {
  background-color: var(--bg-surface-secondary);
  border-color: transparent;
}

.form-control-dark {
  color: var(--white);
  background-color: color-transparent(var(--black), 0.1);
  border-color: transparent;

  &:focus {
    background-color: color-transparent(var(--black), 0.1);
    border-color: color-transparent(var(--white), 0.24);
    box-shadow: none;
  }

  &::placeholder {
    color: color-transparent(var(--white), 0.6);
  }
}

.form-control-rounded {
  @include border-radius(var(--border-radius-pill));
}

.form-control-flush {
  padding: 0;
  line-height: inherit;
  resize: none;
  background: none !important;
  border-color: transparent !important;
  box-shadow: none !important;
}

.form-footer {
  --form-footer-margin-top: #{$form-footer-margin-top};

  margin-top: var(--form-footer-margin-top);
}

.form-fieldset {
  --form-fieldset-padding: #{$form-fieldset-padding};
  --form-fieldset-margin-bottom: #{$form-fieldset-margin-bottom};

  padding: var(--form-fieldset-padding);
  margin-bottom: var(--form-fieldset-margin-bottom);
  background: var(--bg-surface-secondary);
  border: var(--border-width) var(--border-style) var(--border-color);
  @include border-radius(var(--border-radius));
}

fieldset:empty {
  display: none;
}

// Form help
.form-help {
  --form-help-size: #{$form-help-size};
  --form-help-font-size: #{$form-help-font-size};

  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--form-help-size);
  height: var(--form-help-size);
  // `.form-help` is often a `<button>`: drop the user agent's padding and border
  padding: 0;
  font-size: var(--form-help-font-size);
  font-weight: var(--font-weight-semibold);
  color: $form-secondary-color;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  user-select: none;
  background: var(--bg-surface-tertiary);
  border: 0;
  @include border-radius(var(--border-radius-pill));
  @include transition(background-color $transition-time, color $transition-time);

  &:hover,
  &[aria-describedby] {
    color: var(--primary-fg);
    background: var(--primary);
  }
}

// Input group
.input-group {
  box-shadow: $input-box-shadow;
  @include border-radius(var(--input-group-addon-border-radius));

  .form-control,
  .form-select,
  .btn {
    box-shadow: none;
  }
}

.input-group-link {
  font-size: $h5-font-size;
}

.input-group-flat {
  @include focus-ring-transition();
  &:focus-within {
    --focus-ring-offset: -1px;
    @include focus-ring(true);
    @include border-radius(var(--input-group-addon-border-radius));

    .form-control,
    .input-group-text {
      border-color: $input-focus-border-color !important;
    }
  }

  .form-control {
    &:focus {
      border-color: var(--control-border-color);
      outline: 0;
      box-shadow: none;
    }

    &:not(:last-child) {
      border-inline-end: 0;
    }

    &:not(:first-child) {
      border-inline-start: 0;
    }
  }

  .input-group-text {
    z-index: 10;
    background: #{var-if-default($form-check-input-bg, var(--bg-forms), '--btn-input-bg')};
    @include transition($input-transition);

    &:first-child {
      padding-inline-end: 0;
      border-inline-end: 0;
    }

    &:last-child {
      padding-inline-start: 0;
      border-inline-start: 0;
    }
  }

  @each $state, $data in $form-validation-states {
    $border-color: map.get($data, 'border-color');
    $focus-ring-color: map.get($data, 'focus-ring-color');

    &:has(.form-control.is-#{$state}),
    .was-validated &:has(.form-control:#{$state}) {
      .input-group-text {
        border-color: $border-color;
      }

      &:focus-within {
        outline-color: $focus-ring-color;

        .form-control,
        .input-group-text {
          border-color: $border-color !important;
        }
      }
    }
  }
}

// Upload files
.form-file-button {
  margin-inline-start: 0;
  border-inline-start: 0;
}

// Forms on mobile devices
.form-control,
.form-select {
  --form-control-mobile-font-size: #{$form-control-mobile-font-size};

  @include media-breakpoint-down(sm) {
    font-size: var(--form-control-mobile-font-size);
  }
}
