/**
 * Form elements
 */

/* Reboot based on :
- normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css
- sanitize.css v13.0.0 | CC0 1.0 Universal | github.com/csstools/sanitize.css
––––––––––––––––––––

1. Change the font styles in all browsers
2. Remove the margin in Firefox and Safari
*/
input,
optgroup,
select,
textarea {
  margin: 0; /* 2 */
  font-size: var(--font-zize); /* 1 */
  line-height: var(--line-height); /* 1 */
  font-family: inherit; /* 1 */
  letter-spacing: inherit; /* 2 */
}

/* Remove the inheritance of text transform in Edge, Firefox, and IE */
select {
  text-transform: none;
}

/* 1. Correct the text wrapping in Edge and IE */
/* 3. Remove the padding so developers are not caught out when they zero out */
/* `fieldset` elements in all browsers */
legend {
  max-width: 100%; /* 1 */
  padding: 0; /* 3 */
  white-space: normal; /* 1 */
}

/* Correct the cursor style of increment and decrement buttons in Safari */
::-webkit-inner-spin-button,
::-webkit-outer-spin-button {
  height: auto;
}

/* 1. Correct the odd appearance in Chrome and Safari */
/* 2. Correct the outline style in Safari */
[type="search"] {
  -webkit-appearance: textfield; /* 1 */
  outline-offset: -2px; /* 2 */
}

/* Remove the inner padding in Chrome and Safari on macOS */
[type="search"]::-webkit-search-decoration {
  -webkit-appearance: none;
}

/* 1. Correct the inability to style clickable types in iOS and Safari */
/* 2. Change font properties to `inherit` in Safari */
::-webkit-file-upload-button {
  -webkit-appearance: button; /* 1 */
  font: inherit; /* 2 */
}

/* Remove the inner border and padding of focus outlines in Firefox */
::-moz-focus-inner {
  padding: 0;
  border-style: none;
}

/* Remove the focus outline in Firefox */
:-moz-focusring {
  outline: none;
}

/* Remove the additional :invalid styles in Firefox */
:-moz-ui-invalid {
  box-shadow: none;
}

/* Remove the border and padding in all browsers (opinionated) */
[type="file"],
[type="range"] {
  padding: 0;
  border-width: 0;
}

/* Fieldset */
fieldset {
  margin: var(--space) 0;
  padding: 0;
  border: 0;
}

fieldset:first-child {
  margin-top: 0;
}

fieldset:last-child {
  margin-bottom: 0;
}

/* Label & legend */
label,
fieldset legend {
  display: block;
  margin-bottom: var(--space-sm);
  font-weight: var(--font-weight);
}

/* Blocks, 100% */
[type="text"],
[type="search"],
[type="number"],
[type="email"],
[type="password"],
[type="datetime-local"],
[type="month"],
[type="tel"],
[type="time"],
[type="url"],
[type="week"],
select,
textarea {
  width: 100%;
}

/* Common input styles */
[type="text"],
[type="search"],
[type="number"],
[type="email"],
[type="password"],
[type="date"],
[type="datetime-local"],
[type="month"],
[type="color"],
[type="tel"],
[type="time"],
[type="url"],
[type="week"],
select,
textarea {
  appearance: none;
  border: solid 1px var(--border-color);
  border-radius: var(--border-radius);
  outline: none;
  background-color: var(--background-color);
  color: var(--color);
  font-weight: var(--font-weight);
  transition: background-color var(--transition), border-color var(--transition), color var(--transition), box-shadow var(--transition);
  padding: var(--space-xs) var(--space);

  &::placeholder {
    opacity: 0.6;
  }
}

/* Textarea */
textarea {
  padding: var(--space-sm) var(--space);
}

/* Active & Focus */
input:not([type="submit"]):not([type="button"]):not([type="reset"]):not([role="switch"]):not([readonly]),
:where(select, textarea) {
  &:is(:active, :focus) {
    border-color: var(--blue);
  }
}

/* Focus */
input:not([type="submit"]):not([type="button"]):not([type="reset"]):not([type="range"]):not([type="file"]):not([readonly]),
select,
textarea {
  &:focus {
    --box-shadow: 0 0 0 var(--space-xs) var(--highlight);
  }
}

/* Disabled */
input:not([type="submit"]):not([type="button"]):not([type="reset"])[disabled],
select[disabled],
textarea[disabled],
:where(fieldset[disabled]) :is(input:not([type="submit"]):not([type="button"]):not([type="reset"]), select, textarea) {
  background-color: var(--grey2);
  border-color: var(--grey2);
  opacity: 0.5;
  pointer-events: none;
}

/* Aria-invalid */
:where(input, select, textarea) {
  &:not([type="checkbox"]):not([type="radio"]) {
    &[aria-invalid] {
      padding-right: calc(var(--form-element-spacing-horizontal) + var(--space-md));
      padding-left: var(--form-element-spacing-horizontal);
      padding-inline-start: var(--form-element-spacing-horizontal);
      padding-inline-end: calc(var(--form-element-spacing-horizontal) + var(--space-md));
      background-position: center right var(--space-sm);
      background-size: var(--space) auto;
      background-repeat: no-repeat;
    }

    &[aria-invalid="false"] {
      background-image: var(--icon-valid);
    }

    &[aria-invalid="true"] {
      background-image: var(--icon-invalid);
    }
  }

  &[aria-invalid="false"] {
    --border-color: var(--form-element-valid-border-color);

    &:is(:active, :focus) {
      --border-color: var(--form-element-valid-active-border-color);
      --box-shadow: 0 0 0 var(--space-xs) var(--form-element-valid-focus-color);
    }
  }

  &[aria-invalid="true"] {
    --border-color: var(--form-element-invalid-border-color);

    &:is(:active, :focus) {
      --border-color: var(--form-element-invalid-active-border-color);
      --box-shadow: 0 0 0 var(--space-xs) var(--form-element-invalid-focus-color);
    }
  }
}

/* Placeholder */
input::placeholder,
input::-webkit-input-placeholder,
textarea::placeholder,
textarea::-webkit-input-placeholder,
select:invalid {
  opacity: 0.6;
}

/* Select */
select {
  background-image: var(--form-icon-arrow-down);
  background-size: var(--space) auto;
  background-position: center right calc(var(--space) * 0.75);
  background-repeat: no-repeat;
  padding-right: calc(var(--space) * 2.5);
}

/* Helper */
:where(input, select, textarea) {
  & + small {
    display: block;
    width: 100%;
    margin-top: var(--space-xs);
    color: var(--muted-color);
  }
}
