/* Forms */

/* Fieldsets are used to group related fields and to limit width */

fieldset
{
  max-width: 40rem;
  margin: 0.5rem 0;
}

/* Wrappers for fields and rows containing multiple fields side-by-side */

div.row,
div.field
{
  margin: 0.75rem 0;
}
div.row
{
  display: flex;
  gap: 1rem;
  align-items: flex-end;
}
div.row > div.field
{
  flex: 1 1 auto;
  margin: 0
}
div.field.short
{
  width: 25vw;
  min-width: 2rem;
  max-width: 8rem;
}

/* Basic styling for all labels and label paragraphs */

label,
p.label
{
  display: block;
  width: fit-content;
  word-break: break-all;
}

/* Label above text input element or label paragraph above group of radio buttons or checkboxe(s) */

label:has(+ input),
label:has(+ textarea),
p.label
{
  font-size: var(--size-small);
  font-weight: bold;
  color: var(--secondary);
}
div.field:has(.error) label:has(+ input),
div.field:has(.error) label:has(+ textarea),
div.field:has(.error) p.label
{
  color: var(--error) !important;
}

/* Text input elements */

input[type=email],
input[type=search],
input[type=text],
input[type=url],
textarea
{
  margin: 0.1875rem 0;
  padding: 0.375rem 0.5rem;
  background: var(--inverse);
  border-radius: 0.25rem;
  border: 1px solid var(--border);
  font: inherit;
}
div.field input[type=email],
div.field input[type=search],
div.field input[type=text],
div.field input[type=url],
div.field textarea
{
  width: 100%;
}
div.field textarea
{
  height: 6rem
}
div.field:has(.error) input[type=email],
div.field:has(.error) input[type=search],
div.field:has(.error) input[type=text],
div.field:has(.error) input[type=url],
div.field:has(.error) textarea
{
  border-color: var(--error);
  background: var(--error-background);
}

/* Checkboxes and radio buttons */

label:has(input[type=checkbox]),
label:has(input[type=radio])
{
  display: flex;
  margin: 0.1875rem 0;
  align-items: center;
  gap: 0.5rem;
  -webkit-user-select: none;
  user-select: none;
}
label:has(input[type=checkbox]) input,
label:has(input[type=radio]) input
{
  display: block;
  position: relative;
  width: 1.25rem;
  height: 1.25rem;
  margin: -0.125rem;
  -webkit-appearance: none;
  appearance: none;
}
label:has(input[type=checkbox]) input,
label:has(input[type=radio]) input:not(:checked)
{
  cursor: pointer;
}
label:has(input[type=checkbox]) input:before,
label:has(input[type=checkbox]) input:after,
label:has(input[type=radio]) input:before,
label:has(input[type=radio]) input:after
{
  content: "";
  position: absolute;
  inset: 0;
  mask: center / contain;
}
label:has(input[type=checkbox]) input:before,
label:has(input[type=radio]) input:before
{
  background-color: var(--inverse);
}
label:has(input[type=checkbox]) input:before
{
  mask-image: var(--checkbox-background-svg);
}
label:has(input[type=radio]) input:before
{
  mask-image: var(--radio-background-svg);
}
label:has(input[type=checkbox]) input:after,
label:has(input[type=radio]) input:after
{
  background-color: var(--control);
}
label:has(input[type=checkbox]) input:active:after,
label:has(input[type=radio]) input:not(:checked):active:after
{
  background-color: var(--control-active);
}
label:has(input[type=checkbox]) input:after,
label:has(input[type=checkbox]) input:after
{
  mask-image: var(--checkbox-unchecked-svg);
}
label:has(input[type=radio]) input:after
{
  mask-image: var(--radio-unchecked-svg);
}
label:has(input[type=checkbox]) input:checked:after,
label:has(input[type=checkbox]) input:not(:checked):active:after
{
  mask-image: var(--checkbox-checked-svg);
}
label:has(input[type=radio]) input:checked:after,
label:has(input[type=radio]) input:active:after
{
  mask-image: var(--radio-checked-svg);
}

/* Prevent mask image from “flashing” between `:active` and `:selected` */

label:has(input[type=checkbox]) input:after,
label:has(input[type=radio]) input:after
{
  transition: mask-image 0ms allow-discrete 50ms;
}
label:has(input[type=checkbox]) input:active:after,
label:has(input[type=radio]) input:active:after
{
  transition-delay: 0ms;
}

/* When there’s no label paragraph above a group of radio buttons or checkboxe(s), apply the error
   highlight color to them directly. */

div.field:has(.error):not(:has(p.label)) label:has(input[type=checkbox]),
div.field:has(.error):not(:has(p.label)) label:has(input[type=radio])
{
  color: var(--error);
}
div.field:has(.error):not(:has(p.label)) label:has(input[type=checkbox]) input:before,
div.field:has(.error):not(:has(p.label)) label:has(input[type=radio]) input:before
{
  background: var(--error-background);
}
div.field:has(.error):not(:has(p.label)) label:has(input[type=checkbox]) input:after,
div.field:has(.error):not(:has(p.label)) label:has(input[type=radio]) input:after
{
  background: var(--error);
}

/* Select */

div.select  /* Wrapper needed because :before and :after pseudo-elements don’t work on select. */
{
  position: relative;
  width: fit-content;
  margin: 0.1875rem 0;
}
div.select:after
{
  content: "";
  position: absolute;
  right: 0.125rem;
  top: 50%;
  width: 1.5rem;
  height: 1.5rem;
  margin-top: -0.75rem;
  background-color: var(--control);
  mask: center / contain;
  mask-image: var(--select-svg);
  pointer-events: none;
}
div.select select
{
  appearance: none;
  padding: 0.375rem 1.625rem 0.375rem 0.5rem;
  background: var(--inverse);
  border-radius: 0.25rem;
  border: 1px solid var(--control);
  font: inherit;
}
div.select:has(select:active):after
{
  background-color: var(--control-acive);
}
div.select select:active
{
  border-color: var(--control-acive);
}
