/**
 * Forms of the website and the internal area: one grid layout (class "form-grid" on the form).
 * One of the stylesheets of the website, loaded in a fixed order by php/include/header.php
 * (list and order: AGENTS.md, "Stylesheets"). In the internal area this file comes after
 * forms-admin.css (its general rules for fields and buttons), so the grid wins over them.
 */

/**
 * @section Labels
 */
/* Info icon for labels with an explanation in the title: Lucide "info" in the label colour */
label[title]::after {
  content: "";
  display: inline-block;
  width: 0.95em;
  height: 0.95em;
  margin-left: 0.3em;
  vertical-align: -0.12em;
  background-color: currentColor;
  mask: url(/img/icons/info.svg) center / contain no-repeat;
}
label.required::after {
  content: " *";
  font-size: 0.8em;
  vertical-align: super;
  position: relative;
}
label.required:focus::after,
label.required:hover::after {
  content: " Pflichtfeld";
  font-variant: small-caps;
}

/* error list of a form sent to the page itself (website forms) */
.errors {
  margin-bottom: 1rem;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--color-error);
  border-radius: var(--border-radius);
  color: var(--color-error);
}
.errors label {
  display: block;
  color: inherit;
  font-weight: normal;
}

/**
  * @section Form grid (class "form-grid")
  * Label column on the left, fields on the right; one column on phones.
  * Markup: <label> followed by exactly one field element (input, select, textarea, editor
  * container, Choices wrapper, button); several elements for one label in <div class="field-group">,
  * radio and checkbox options in <label class="surround">. Parts only the internal area uses
  * (Choices, rights, priority buttons, editor …): forms-admin-widgets.css.
  */
form.form-grid {
  display: grid;
  grid-template-columns: 10rem minmax(0, 1fr);
  column-gap: 1rem;
  row-gap: 1.1rem;
  align-items: start;
  max-width: 52rem;
  margin: 0 0 2rem;
  padding: 0;
}
form.form-grid > * {
  grid-column: 2;
  min-width: 0;
  float: none;
  clear: none;
  margin: 0;
}
/* rows that only apply sometimes (e.g. the cancel reason) are hidden with the hidden attribute */
form.form-grid > [hidden] {
  display: none !important;
}
/* error banner from showApiError() spans both columns */
form.form-grid > .api-error-banner {
  grid-column: 1 / -1;
}
form.form-grid > .field-error {
  margin-top: -0.6rem;
  color: var(--color-error);
  font-size: 0.85rem;
}
form.form-grid > label {
  grid-column: 1;
  width: auto;
  padding: 0.6rem 0 0;
  background: none;
  color: var(--color-darkblue);
  font-size: 0.85rem;
  font-weight: 600;
  line-height: 1.3;
  text-align: right;
}

/* Radio/checkbox with its text in one label (label.surround), e.g. "männlich" / "weiblich":
   one option per row in the field column */
form.form-grid > label.surround {
  grid-column: 2;
  display: flex;
  align-items: center;
  gap: 0.45rem;
  margin: 0;
  padding: 0.2rem 0;
  color: #222;
  font-size: 0.95rem;
  font-weight: normal;
  text-align: left;
  cursor: pointer;
}
form.form-grid > label.surround input {
  position: static;
  float: none;
  flex: none;
  width: 1.15rem;
  height: 1.15rem;
  margin: 0;
}
/* the row gap is meant between fields, not between the options of one group
   (also when a hidden field, e.g. the file of "Eigenes Foto", sits between two options) */
form.form-grid > label.surround + label.surround,
form.form-grid > label.surround + [hidden] + label.surround {
  margin-top: -0.8rem;
}

/* file field: its button is taller than a line of text, so less padding keeps it as high as the
   other fields (and the button centred) */
form.form-grid > input[type="file"]:not([readonly]) {
  padding-top: calc((max(2.4rem, var(--admin-target-size, 0px)) - 1.5rem - 2px) / 2);
  padding-bottom: calc((max(2.4rem, var(--admin-target-size, 0px)) - 1.5rem - 2px) / 2);
}
/* Value with fixed unit plus free text (achievements): [13,23] s  oder [13,23s]
   – the text field is what is saved; without a unit only the text field is shown */
form.form-grid > .value-unit {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}
form.form-grid > .value-unit > [hidden] {
  display: none;
}
.value-unit-part {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}
form.form-grid > .value-unit .value-unit-part input {
  width: 7rem;
}
form.form-grid > .value-unit .value-unit-part input[type="number"] {
  width: 5rem;
  text-align: right;
}
/* unit like the text in the fields: same colour, weight and size */
.value-unit-unit {
  color: inherit;
  font-size: 0.95rem;
  font-weight: normal;
  white-space: nowrap;
}
.value-unit-or {
  color: var(--color-gray);
  font-size: 0.85rem;
}
form.form-grid > .value-unit > input {
  flex: 1 1 10rem;
  width: auto;
}
/* without the margin of the general input rule, so the fields line up with their label */
form.form-grid > .value-unit :is(input, select) {
  margin: 0;
}

/* "oder" between two alternative fields (e.g. event or athlete): centred in the field column,
   close to both fields */
form.form-grid > .form-or {
  margin: -0.6rem 0;
  color: var(--color-gray);
  font-size: 0.85rem;
  text-align: center;
}

/* Text fields, selects, textareas: one look, full width of the field column */
/* :is()/:not(:is()) keep the specificity low, so the width classes below still win.
   .field-group: several elements for one label (e.g. thumbnail select + preview image) */
form.form-grid > .field-group {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.6rem;
}
/* no extra space above the first element, so it lines up with its label like a direct field */
form.form-grid > .field-group > * {
  margin: 0;
}
/* side by side, e.g. a select with the spinner of its AI suggestion */
form.form-grid > .field-group.field-group-row {
  flex-direction: row;
  align-items: center;
}
/* small spinner next to a field while it is filled in (e.g. the level of a competition) */
.field-spinner {
  flex: none;
  box-sizing: border-box;
  width: 1.1rem;
  height: 1.1rem;
  border: 2px solid var(--color-lightgray);
  border-top-color: var(--color-darkblue);
  border-radius: 50%;
  animation: field-spinner-spin 0.8s linear infinite;
}
.field-spinner[hidden] {
  display: none;
}
@keyframes field-spinner-spin {
  to { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
  .field-spinner { animation-duration: 2.4s; }
}
form.form-grid > :is(input, select, textarea):not(:is([type="checkbox"], [type="radio"], [type="hidden"])),
form.form-grid > .field-group > :is(input, select, textarea):not(:is([type="checkbox"], [type="radio"], [type="hidden"])),
form.form-grid > .value-unit :is(input, select):not(:is([type="checkbox"], [type="radio"], [type="hidden"])) {
  box-sizing: border-box;
  width: 100%;
  /* 44px on touch screens (--admin-target-size, css/admin.css) */
  min-height: max(2.4rem, var(--admin-target-size, 0px));
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--color-lightgray);
  border-radius: var(--border-radius);
  background-color: var(--color-white);
  color: inherit;
  font-family: inherit;
  font-size: 0.95rem;
  line-height: 1.3;
}
/* selects: only the Lucide chevron from the select rule above, not the browser's arrow too */
form.form-grid select:not([multiple]) {
  -webkit-appearance: none;
  appearance: none;
  padding-right: 2rem;
}
/* Read-only values (gallery folder, status, user name, priority number): look like plain text,
   no field border, so it is clear that they cannot be edited */
/* (not the date fields: they are read-only only so that the calendar is used) */
form.form-grid :is(input, textarea)[readonly]:not(.datepicker-field),
form.form-grid :is(input, textarea)[readonly]:not(.datepicker-field):focus {
  border-color: transparent;
  background: transparent;
  box-shadow: none;
  padding-left: 0;
  padding-right: 0;
  cursor: default;
}
form.form-grid > :is(input, select, textarea):not([readonly]):focus,
form.form-grid > .field-group > :is(input, select, textarea):not([readonly]):focus,
form.form-grid > input.datepicker-field:focus {
  outline: none;
  border-color: var(--color-darkblue);
  box-shadow: 0 0 0 2px var(--color-darkblue-transparent-light);
}
/* Short values do not need the full width (as in the old forms): numbers and class "tight"
   (year, position, unit) ≈ 6rem, class "narrow" ≈ 14rem; text fields of class "narrow" (IDs,
   user name, password, LADV-ID) 230px (they were 200px through "label + input.narrow") */
form.form-grid > input[type="number"],
form.form-grid > input.tight,
form.form-grid > .field-group > input.tight {
  width: 6rem;
}
/* in a row: the short field keeps its width, the other one takes the rest (postcode and town) */
form.form-grid > .field-group-row > input.tight {
  flex: none;
}
form.form-grid > .field-group-row > input:not(.tight) {
  flex: 1;
  min-width: 0;
}
form.form-grid > input.narrow,
form.form-grid > select.narrow,
form.form-grid > .field-group > select.narrow,
form.form-grid > input.datepicker-date,
form.form-grid > input.datepicker-datetime {
  width: 14rem;
  max-width: 100%;
}
form.form-grid > input.narrow:not(.datepicker-field) {
  width: 230px;
}
/* a period ("24.12.2025 bis 25.12.2025", bus booking) needs a little more */
form.form-grid > input.datepicker-range {
  width: 17rem;
  max-width: 100%;
}
/* required fields turn red only after the user has touched them */
form.form-grid > input:invalid,
form.form-grid > textarea:invalid {
  border-color: var(--color-lightgray);
  color: inherit;
}
form.form-grid > input:user-invalid,
form.form-grid > textarea:user-invalid,
form.form-grid > .input-error {
  border-color: var(--color-error);
}
form.form-grid > input[type="checkbox"],
form.form-grid > input[type="radio"] {
  justify-self: start;
  width: 1.15rem;
  height: 1.15rem;
  margin-top: 0.6rem;
}
/* a checkbox with a note next to it (.field-inline): as large as the others, centred by the row */
form.form-grid > .field-inline > input[type="checkbox"] {
  flex: none;
  width: 1.15rem;
  height: 1.15rem;
}
form.form-grid > .choices,
form.form-grid > .editor-field {
  width: 100%;
  min-width: 0;
}
/* Form buttons: shape of .admin-button (css/admin.css) – icon + text, 32px (44px on touch),
   thin border – but filled, because they are the main action of the page:
   dark blue for save/add, red for delete (.button-delete, faded while disabled) */
form.form-grid > button {
  justify-self: start;
  cursor: pointer;
  font-family: inherit;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  box-sizing: border-box;
  width: auto;
  min-width: 8rem;
  min-height: var(--admin-target-size, 32px);
  margin-top: 0.4rem;
  padding: 0 1rem;
  border: 1px solid var(--color-darkblue);
  border-radius: var(--border-radius);
  background: var(--color-darkblue);
  color: var(--color-white);
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1;
}
form.form-grid > button:hover:not(:disabled),
form.form-grid > button:focus-visible {
  border-color: var(--color-darkblue-toned);
  background: var(--color-darkblue-toned);
  box-shadow: none;
  transform: none;
}
form.form-grid > button .icon {
  margin: 0;
}
/* disabled until a confirmation is ticked (e.g. "Ausführen"): faded, like the delete button */
form.form-grid > button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
form.form-grid > button.button-delete {
  border-color: var(--color-error);
  background: var(--color-error);
}
form.form-grid > button.button-delete:hover:not(:disabled),
form.form-grid > button.button-delete:focus-visible {
  border-color: var(--color-error-toned, #cc0000);
  background: var(--color-error-toned, #cc0000);
}

/* One column in narrow places (e.g. the bus booking dialog): labels above their field */
form.form-grid.form-stacked {
  grid-template-columns: minmax(0, 1fr);
  row-gap: 0.35rem;
}
form.form-grid.form-stacked > * {
  grid-column: 1;
}
form.form-grid.form-stacked > label {
  padding-top: 0.6rem;
  text-align: left;
}
form.form-grid.form-stacked > label:first-child {
  padding-top: 0;
}

/* Phones: labels above their field, buttons over the full width */
@media (max-width: 640px) {
  form.form-grid {
    grid-template-columns: minmax(0, 1fr);
    row-gap: 0.35rem;
  }
  form.form-grid > *,
  form.form-grid > label {
    grid-column: 1;
  }
  form.form-grid > label {
    padding: 0.6rem 0 0;
    text-align: left;
  }
  form.form-grid > label:first-child {
    padding-top: 0;
  }
  form.form-grid > label.surround {
    grid-column: 1;
    min-height: 2.25rem;
  }
  form.form-grid > label.surround + label.surround {
    margin-top: -0.35rem;
  }
  /* the empty label in front of a button only adds space */
  form.form-grid > label:has(+ button) {
    display: none;
  }
  form.form-grid > button {
    justify-self: stretch;
    width: 100%;
    margin-top: 1rem;
  }
  form.form-grid > input[type="checkbox"],
  form.form-grid > input[type="radio"] {
    width: 1.5rem;
    height: 1.5rem;
    margin-top: 0.2rem;
  }
  form.form-grid > .field-inline > input[type="checkbox"] {
    width: 1.5rem;
    height: 1.5rem;
  }
}

/* headings and texts inside a form span both columns */
form.form-grid > :is(h2, h3, h4, p, .errors) {
  grid-column: 1 / -1;
}

/* arrow: Lucide chevron-down (path inlined, so it can be grey and, on focus, blue) */
form.form-grid select {
  background: var(--color-white) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23808080' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 0.6em center;
  background-size: 18px 18px;
  cursor: pointer;
  padding-right: 2.5em;
}

form.form-grid select:focus {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23005193' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
}

form.form-grid select[multiple] {
  background-image: none;
  height: auto;
  padding-right: 0.6em;
}
