/* Form */

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

fieldset
{
  max-width: 48rem;
  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;
}

/* Fields without a size class fill the available width in the row equally */

div.row > div.field
{
  flex: 1 1 0;
  margin: 0;
}

/* Fields with a size class limit their width */

div.field.small
{
  min-width: 4rem;
  max-width: min(calc(25% - 0.5rem), 8rem);
}
div.field.medium
{
  min-width: 8rem;
  max-width: min(calc(33.3% - 0.5rem), 16rem);
}

/* Fields containing an input element with a limited width only take up the space they need */

div.row > div.field:has(input[type=date]),
div.row > div.field:has(select)
{
  flex: 0 1 auto;
}

/* Pivot rows on narrower viewports */

@media (max-width: 24rem)
{
  div.row
  {
    flex-direction: column;
    align-items: flex-start;
  }
  div.field
  {
    width: 100%;
  }
  div.field.small
  {
    max-width: min(calc(50% - 0.5rem), 8rem);
  }
  div.field.medium
  {
    max-width: 100%;
  }
}

/* Composite controls with secondary controls or labels before or after an input element */

div.field div.composite
{
  display: flex;
  align-items: center;
  gap: 0.375rem;
  color: var(--text-secondary);
}

/* Composite for a group of radio button or checkbox options */

div.field div.options div.composite
{
  gap: 0.5rem;
}

/* Pivot composite for a group of radio button or checkbox options on narrower viewports */

@media (max-width: 24rem)
{
  div.field div.options div.composite
  {
    flex-direction: column;
    align-items: flex-start;
  }
  div.field div.options div.composite + div.composite
  {
    margin-top: 0.5rem;
  }
  div.field div.options div.composite > label:not(:first-of-type)
  {
    margin-left: 1.5rem;
  }
}

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

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

/* Label above text input or select, or label paragraph above radio buttons or checkboxe(s) */

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

/* Text input elements */

input[type=email],
input[type=number],
input[type=search],
input[type=tel],
input[type=text],
input[type=url],
textarea
{
  padding: 0.375rem 0.5rem;
  background: var(--inverse);
  border-radius: 0.25rem;
  border: 1px solid var(--border);
  font: inherit;
}
input[type=email]:focus,
input[type=number]:focus,
input[type=search]:focus,
input[type=tel]:focus,
input[type=text]:focus,
input[type=url]:focus,
textarea:focus
{
  border-color: var(--control-active);
}
div.field input[type=email],
div.field input[type=number],
div.field input[type=search],
div.field input[type=tel],
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=number],
div.field:has(.error) input[type=search],
div.field:has(.error) input[type=tel],
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;
  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;
  flex: 0 0 auto;
  position: relative;
  width: 1.25rem;
  height: 1.25rem;
  margin: -0.125rem;
  -webkit-appearance: none;
  appearance: none;
  cursor: inherit;
}
label:has(input[type=checkbox]:not(:disabled)),
label:has(input[type=radio]:not(:disabled))
{
  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 no-repeat;
}

/* Background behind custom checkbox or radio button shape */

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]).tag input:before
{
  mask-image: var(--star-filled-svg);
  scale: 0.75;
}

/* Custom checkbox or radio button shape */

label:has(input[type=checkbox]) input:after,
label:has(input[type=radio]) input:after
{
  background-color: var(--control);
}
label:has(input[type=checkbox]) input:not(:disabled):active:after,
label:has(input[type=radio]) input:not(:disabled):not(:checked):active:after
{
  background-color: var(--control-active);
}
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]).tag input:after
{
  mask-image: var(--star-svg);
}
label:has(input[type=checkbox]) input:not(:disabled):checked:after,
label:has(input[type=checkbox]) input:not(:disabled):not(:checked):active:after
{
  mask-image: var(--checkbox-checked-svg);
}
label:has(input[type=radio]) input:not(:disabled):checked:after,
label:has(input[type=radio]) input:not(:disabled):active:after
{
  mask-image: var(--radio-checked-svg);
}
label:has(input[type=checkbox]).tag input:not(:disabled):checked:after
{
  mask-image: var(--star-filled-svg);
}
label:has(input[type=checkbox]).tag input:not(:disabled):not(:checked):active:after
{
  mask-image: var(--star-filled-shine-svg);
}

/* Prevent “flashing” between `:active` and `:checked` */

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

/* Wrapper for a group of radio button or checkbox options */

div.field div.options:not(.columns) > label,
div.field div.options:not(.columns) > div.composite,
div.field div.options.columns > div > label,
div.field div.options.columns > div > div.composite
{
  margin: 0.1875rem 0;
}
@media (min-width: 48rem)
{
  div.field div.options.columns
  {
    display: flex;
    gap: 1rem;
  }
  div.field div.options.columns > div
  {
    flex: 1 1 0;
    margin: -0.1875rem 0;
  }
}

/* Tag-style toggle control */

label:has(input[type=checkbox]).tag
{
  position: relative;
  margin: -0.1875rem 0;
  padding: 0.0625rem 0.375rem 0.0625rem 0.625rem;
  gap: 0.25rem;
  font-size: var(--size-tiny);
  background: var(--toggle-background);
  --border-width: 1px;
  --arrow-length: calc(1.0625rem / 2 + var(--border-width)); /* 45° angle for single-line label */
  clip-path: polygon(
    var(--arrow-length) 100%, 0% 50%, var(--arrow-length) 0%, 100% 0%, 100% 0%, 100% 100%
  );
  color: var(--control);
  border: var(--border-width) solid var(--toggle-border);
}
label:has(input[type=checkbox]).tag:after
{
  content: '';
  position: absolute;
  left: calc(var(--border-width) * -1);
  top: calc(var(--border-width) * -1);
  bottom: calc(var(--border-width) * -1);
  --cutout-offset: calc(var(--border-width) * 1.4); /* Shift for arrow cutout to match border */
  width: calc(var(--arrow-length) + var(--cutout-offset));
  background-color: var(--toggle-border);
  clip-path: polygon(
    evenodd, /* The first line below draws a square, the second line cuts out an arrowhead shape */
    100% 0, 100% 100%, 0 100%, 0 0, 100% 0,
    var(--cutout-offset) 50%, 100% 100%
  );
}
label:has(input[type=checkbox]:not(:disabled):checked).tag
{
  background: var(--toggle-background-checked);
  border-color: var(--toggle-border-checked);
}
label:has(input[type=checkbox]:not(:disabled):checked).tag:after
{
  background-color: var(--toggle-border-checked);
}
label:has(input[type=checkbox]:not(:disabled):active).tag
{
  background: var(--toggle-background-active);
  border-color: var(--toggle-border-active);
}
label:has(input[type=checkbox]:not(:disabled):active).tag:after
{
  background-color: var(--toggle-border-active);
}
label:has(input[type=checkbox]).tag input
{
  width: 1rem;
  height: 1rem;
}

/* 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);
}

/* Date input */

input[type=date]
{
  padding: 0.375rem 0.5rem;
  background: var(--inverse);
  border-radius: 0.25rem;
  border: 1px solid var(--border);
  font: inherit;
}
input[type=date]:focus
{
  border-color: var(--control-active);
}
div.field:has(.error) input[type=date]
{
  border-color: var(--error);
  background: var(--error-background);
}

/* Control height of shadow elements inside the date input. */

input[type=date]::-webkit-datetime-edit,
input[type=date]::-webkit-datetime-edit-month-field,
input[type=date]::-webkit-datetime-edit-day-field,
input[type=date]::-webkit-datetime-edit-year-field
{
  height: 1.1875rem;
  padding-top: 0;
}

/* Select */

div.select  /* Wrapper needed because :before and :after pseudo-elements don’t work on select. */
{
  position: relative;
  width: fit-content;
  max-width: 100%;
}
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;
  width: 100%;
  max-width: 48rem;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  background: var(--inverse);
  border-radius: 0.25rem;
  border: 1px solid var(--border);
  font: inherit;
}
div.select:has(select:active):after
{
  background-color: var(--control-active);
}
div.select select:focus
{
  border-color: var(--control-active);
}
div.field:has(.error) div.select:after
{
  background-color: var(--error);
}
div.field:has(.error) div.select select
{
  border-color: var(--error);
  background: var(--error-background);
}

/* Disabled fields */

input[type=email]:disabled,
input[type=number]:disabled,
input[type=search]:disabled,
input[type=tel]:disabled,
input[type=text]:disabled,
input[type=url]:disabled,
textarea:disabled,
input[type=date]:disabled,
div.select:disabled,
label:has(input[type=checkbox]:disabled),
label:has(input[type=radio]:disabled)
{
  cursor: default;
  opacity: 0.4;
}
