/*
  Restores the pre-Django 6.1 admin form layout on desktop: labels to the left of
  their inputs, with help text below the input and errors above it.

  Django 6.1 moved labels above inputs for accessibility (ticket #34643), and put
  the help text and errors between the label and the input in the HTML. We prefer
  the denser layout for the ops team, so this brings back 6.0's label rules and
  reorders those elements with flexbox. Narrow screens keep Django's stacked
  layout, as they did before 6.1.
  https://code.djangoproject.com/ticket/34643

  Must load after Django's forms.css, so it's linked from extrahead.
*/

@media (min-width: 768px) {
  /* Django 6.0's label rules, with their original selectors so that our per-field
     overrides in templates/admin/base_site.html still win */
  .aligned legend,
  .aligned label {
    display: block;
    padding: 4px 10px 0 0;
    min-width: 160px;
    width: 160px;
    overflow-wrap: break-word;
  }

  form div.radiolist label {
    width: auto;
  }

  p.datetime label {
    display: inline;
  }

  .aligned fieldset {
    flex-grow: 1;
  }

  fieldset .fieldBox {
    margin-right: 20px;
  }

  /* django-admin-interface makes every .flex-container full width, which in 6.1
     includes each field of a multi-field row */
  .admin-interface .form-multiline > .fieldBox {
    width: auto;
  }

  /* Each field's wrapper holds the label, help, errors, then the widget */
  .aligned .form-row > div.flex-container:not(.form-multiline),
  .aligned .form-row > fieldset > div.flex-container {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 0;
  }

  .aligned .form-row > div.flex-container:not(.form-multiline) > *,
  .aligned .form-row > fieldset > div.flex-container > * {
    order: 1;
  }

  .aligned .form-row > div.flex-container:not(.form-multiline) > label {
    order: 0;
    flex: none;
  }

  /* Keep wide widgets (e.g. rich text editors) beside their label */
  .aligned .form-row > div.flex-container:not(.form-multiline):not(.fieldBox) > :not(label) {
    max-width: calc(100% - 170px);
  }

  /* Errors above the widget, help text below it, both lined up with the widget */
  .aligned .form-row > div.flex-container:not(.form-multiline) > ul.errorlist {
    order: -1;
    flex: 0 0 calc(100% - 170px);
    margin: 0 0 5px 170px;
  }

  form .aligned .form-row > div.flex-container:not(.form-multiline) > div.help {
    order: 2;
    flex: 0 0 calc(100% - 170px);
    margin: 4px 0 0 170px;
  }

  /* Multiple-choice fields: the legend floats left, so their contents need no indent */
  .aligned .form-row > fieldset > div.flex-container > ul.errorlist {
    order: -1;
    flex: 0 0 100%;
    margin-bottom: 5px;
  }

  form .aligned .form-row > fieldset > div.flex-container > div.help {
    order: 2;
    flex: 0 0 100%;
    margin-top: 4px;
  }
}
