/* ==========================================================================
   forms.css: every form control and button
   ==========================================================================

   Uses the tokens in app.css. Plain <input>, <select>, <textarea>, <button>
   and <fieldset> are styled as they are, in both shapes the templates use:
   the citizen Form's .field blocks (label, help, error, control), and the
   admin's <label>Text <input></label>.

   Accessibility: controls are at least 44 px (48 for text inputs and
   primary buttons), borders reach 3:1, a field in error says so in text
   and with aria-invalid, not by colour alone, and focus is always visible.
*/

/* --- Labels and layout -------------------------------------------------- */

main form > * + * { margin-top: 1.25rem; }
main form > input[type="hidden"] + * { margin-top: 0; }

label {
  font-weight: 600;
}

/* <label>Text <input></label>: the text above its control. */
label:has(> input:not([type="checkbox"], [type="radio"]), > select, > textarea) {
  display: flex;
  flex-direction: column;
  gap: .375rem;
  max-width: 42rem;
}

/* <label><input type="checkbox"> Text</label>: the box before its text. */
label:has(> input[type="checkbox"], > input[type="radio"]) {
  display: flex;
  gap: .75rem;
  align-items: flex-start;
  font-weight: 400;
}

/* A line on top with its legend, the fields below it on the page's own left edge. */
fieldset {
  max-width: 42rem;
  padding: .25rem 0 0;
  border: 0;
  border-top: 1px solid var(--line);
}

fieldset > * + * { margin-top: 1rem; }

legend {
  padding: 0 .75rem 0 0;
  font-size: 1.125rem;
  font-weight: 700;
}

/* The citizen Form's fields. */
.field {
  display: flex;
  flex-direction: column;
  gap: .375rem;
  max-width: 42rem;
}

/* A checkbox field: the box, then its label. */
.field:has(> input[type="checkbox"]) {
  flex-direction: row;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: .75rem;
}

.field:has(> input[type="checkbox"]) > label { flex: 1 1 0; font-weight: 400; }
.field:has(> input[type="checkbox"]) > .field-help,
.field:has(> input[type="checkbox"]) > .field-error { flex-basis: 100%; }

.field-help,
.help {
  margin: 0;
  color: var(--muted);
  font-size: .9375rem;
  font-weight: 400;
}

.required { color: var(--err); }

/* A field in error: a red edge down its side, the message in words above the control. (Every
   field has an error slot; only a shown one counts.) */
.field:has(.field-error:not([hidden])) {
  padding-left: 1rem;
  border-left: 4px solid var(--err);
}

.field-error {
  margin: 0;
  color: var(--err);
  font-weight: 600;
}

/* The message sits right under the label, before the control, as screen readers expect. */
.field:has(.field-error:not([hidden])) > .field-error { order: -1; }
.field:has(.field-error:not([hidden])) > label { order: -2; }
.field:has(> input[type="checkbox"]):has(.field-error:not([hidden])) > .field-error { order: 0; }
.field:has(> input[type="checkbox"]):has(.field-error:not([hidden])) > label { order: 0; }

/* The citizen Form's fields: a fieldset only to lock them all while sending, not a frame. */
fieldset.answers {
  max-width: none;
  padding: 0;
  border: 0;
  border-radius: 0;
}

fieldset.answers > * + * { margin-top: 1.5rem; }

/* Radios or boxes to tick under one title: a .field, its legend looking like a label. */
fieldset.choices {
  padding: 0;
  border: 0;
  border-radius: 0;
  gap: .5rem;
}
fieldset.choices > * + * { margin-top: 0; }
/* Its labels are the options: they stay below the message, which sits under the legend. */
fieldset.choices:has(.field-error:not([hidden])) > label { order: 0; }
fieldset.choices > legend {
  padding: 0;
  margin: 0 0 .5rem;
  font-size: inherit;
  font-weight: 600;
}
fieldset.answers > legend + * { margin-top: 0; }
fieldset:disabled .field label { color: var(--muted); }

/* --- Text inputs, selects, text areas ------------------------------------ */

input:not([type="checkbox"], [type="radio"], [type="file"], [type="hidden"], [type="submit"]),
select,
textarea {
  width: 100%;
  max-width: 42rem;
  min-height: 48px;
  box-sizing: border-box;
  padding: .625rem .875rem;
  border: 2px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--ground);
  color: var(--ink);
  font: inherit;
  font-weight: 400;
  line-height: 1.4;
}

input[type="number"] { max-width: 12rem; }

select {
  appearance: none;
  padding-right: 2.75rem;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--ink) 50%),
    linear-gradient(135deg, var(--ink) 50%, transparent 50%);
  background-position: calc(100% - 1.25rem) 50%, calc(100% - .9rem) 50%;
  background-size: .375rem .375rem;
  background-repeat: no-repeat;
}

textarea { min-height: 7rem; resize: vertical; }

input:hover, select:hover, textarea:hover { border-color: var(--ink); }

input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 0;
  box-shadow: 0 0 0 6px var(--focus);
}

[aria-invalid="true"] { border-color: var(--err) !important; }

input:disabled, select:disabled, textarea:disabled {
  border-color: #c4ccd4;
  background: var(--neutral-soft);
  color: #5f6b78;
  cursor: not-allowed;
}

::placeholder { color: #5f6b78; opacity: 1; }

/* --- Checkboxes and radios --------------------------------------------- */

input[type="checkbox"],
input[type="radio"] {
  flex: 0 0 auto;
  width: 1.5rem;
  height: 1.5rem;
  margin: .1rem 0 0;
  accent-color: var(--accent);
  cursor: pointer;
}

/* --- Buttons ----------------------------------------------------------- */

/* Every button, and a link that does a button's job (class="button"). */
button,
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  min-height: 48px;
  padding: .5rem 1.5rem;
  box-sizing: border-box;
  border: 2px solid var(--accent);
  border-radius: var(--radius);
  background: var(--accent);
  color: #ffffff;
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
}

button:hover,
.button:hover {
  border-color: var(--accent-dark);
  background: var(--accent-dark);
  color: #ffffff;
}

button:focus-visible,
.button:focus-visible {
  outline: none;
  border-color: var(--ink);
  background: var(--focus);
  color: var(--ink);
  box-shadow: 0 4px 0 var(--ink);
}

/* Second choices: an outline. */
.button-secondary,
.session-discard button,
.pagination .button,
.header-login form[method="get"] button,
.keep-receipt button,
.download-warning button {
  background: var(--ground);
  color: var(--accent);
}

.button-secondary:hover,
.session-discard button:hover,
.pagination .button:hover,
.header-login form[method="get"] button:hover,
.keep-receipt button:hover,
.download-warning button:hover {
  background: var(--accent-soft);
  color: var(--accent-dark);
}

/* Destructive: red outline, so it is never the obvious next step. */
.button-danger,
.review-reject button,
td > form button {
  border-color: var(--err);
  background: var(--ground);
  color: var(--err);
}

.button-danger:hover,
.review-reject button:hover,
td > form button:hover {
  border-color: var(--err);
  background: var(--err-soft);
  color: var(--err);
}

/* Small buttons in a table row, still 44 px tall. */
td > form button {
  min-height: 44px;
  padding: .25rem 1rem;
  font-size: .9375rem;
}

button:disabled,
button:disabled:hover {
  border-color: #c4ccd4;
  background: var(--neutral-soft);
  color: #5f6b78;
  box-shadow: none;
  cursor: not-allowed;
}

/* A phone gets full-width main actions. */
@media (max-width: 30em) {
  main form > button[type="submit"],
  .finalize button,
  .session-discard button { width: 100%; }
}

/* --- The form builder (form-builder.js) ---------------------------------- */

.form-builder > * + * { margin-top: 1rem; }
.builder-elements { margin: 0; padding: 0; list-style: none; border: 1px solid var(--line); border-radius: var(--radius-lg); }
.builder-elements:empty { display: none; }
.builder-elements > li + li { border-top: 1px solid var(--line); }
.builder-elements > li:has(.builder-editor) { padding: .5rem; }

.builder-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0 .75rem;
  padding: .5rem .5rem .5rem .75rem;
}

.builder-row:hover { background: var(--surface-alt); }
.builder-row-error, .builder-row-error:hover { background: var(--err-soft); box-shadow: inset 4px 0 var(--err); }
/* The problem as plain red text inside the red row, not the boxed .error banner. */
.builder-row > .error { grid-column: 2 / -1; margin: 0; padding: 0 0 .25rem; border: 0; background: none; color: var(--err); font-size: .9375rem; }
.builder-tile { width: 2.25rem; height: 2.25rem; border-radius: var(--radius); background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; }
.builder-tile-snippet { background: var(--neutral-soft); color: var(--muted); }
.builder-icon { flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.builder-main { min-width: 0; }
.builder-title { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.builder-meta { display: flex; flex-wrap: wrap; align-items: center; gap: .125rem .5rem; color: var(--muted); font-size: .875rem; }
.builder-pill { flex: none; white-space: nowrap; font-size: .6875rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; background: var(--ok-soft); color: var(--ok); border-radius: 999px; padding: .05rem .5rem; }
.builder-pill-off { background: var(--neutral-soft); color: var(--muted); }
.builder-name { font-family: var(--mono); font-size: .8125rem; background: var(--surface); padding: 0 .3rem; border-radius: 3px; overflow-wrap: anywhere; }
.builder-kind { color: var(--muted); font-size: .9375rem; display: inline-flex; align-items: center; gap: .375rem; }
.builder-actions { display: flex; }

.builder-tool {
  min-height: 44px;
  min-width: 44px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: none;
  color: var(--accent);
}

.builder-tool:hover:not(:disabled) { background: var(--accent-soft); }
.builder-tool-danger { color: var(--err); }
.builder-tool-danger:hover:not(:disabled) { background: var(--err-soft); }
.builder-tool:disabled { opacity: .35; cursor: default; background: none; }

@media (max-width: 40rem) {
  .builder-row { grid-template-columns: auto 1fr; }
  .builder-actions { grid-column: 2; margin-left: -.75rem; }
}

.builder-editor {
  padding: 1rem 1.25rem 1.25rem;
  border: 2px solid var(--accent);
  border-radius: var(--radius-lg);
  background: var(--ground);
}

.builder-editor > * + * { margin-top: 1rem; }
.builder-editor > .builder-kind { margin: 0; font-weight: 600; color: var(--accent); }
.builder-editor .error p { margin: 0; }
.builder-more > summary { font-weight: 600; cursor: pointer; }
.builder-more > * + * { margin-top: 1rem; }
.builder-buttons { display: flex; flex-wrap: wrap; gap: .5rem; }
.builder-add { display: flex; flex-wrap: wrap; align-items: flex-end; gap: .75rem; }
.builder-empty { color: var(--muted); }

/* The Forms list (forms_list.html) and a Form's files (_form_advanced.html) borrow the builder's rows. */
.forms-list { max-width: 56rem; }
.forms-list > li, .files-list > li { max-width: none; }
.files-list .builder-tile { font-size: .8125rem; font-weight: 700; }
/* A preview of the image itself, on white so a transparent logo shows as it will. */
.builder-tile-thumb { background: var(--ground); border: 1px solid var(--line); overflow: hidden; }
.builder-tile-thumb img { width: 100%; height: 100%; object-fit: contain; }
.builder-icon-filled { fill: currentColor; }
.forms-list .builder-actions form { margin: 0; }

/* --- A Form's page: five sections, one under the other (docs/adr/0028) ------------------ */

/* Each starts with a line and its heading, as wide as the fields. */
.form-part { max-width: 42rem; border-top: 1px solid var(--line); padding-top: 1.25rem; }
main form > .form-part { margin-top: 2.5rem; }
main form > .form-part:first-child { margin-top: 1.5rem; }
.form-part > * + * { margin-top: 1rem; }
.form-part > h2 { margin: 0; }
.form-part > h3 { margin-top: 1.5rem; font-size: 1.125rem; }
.form-part > fieldset { margin-top: 1.5rem; }
.form-part > .template-source { min-height: 7rem; }
.form-version { color: var(--muted); }
main form > .form-buttons { margin-top: 2rem; }
.form-buttons { display: flex; flex-wrap: wrap; gap: .75rem; }
