/* ===================================
   FORM FIELD MOLECULE - Molecular Design
   ===================================

   Combines Label + Input + Help/Error atoms into cohesive form fields.
   Uses atomic design tokens and atomic components.
*/

/* Base form field styles */
.form-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1_5);
}

/* Form field label */
.form-label {
  display: block;
  font-size: var(--text-sm);
  font-weight: var(--font-weight-medium);
  color: var(--text-primary);
  line-height: var(--line-height-normal);
  margin: 0;
  user-select: none;
}

/* Required field indicator */
.required-indicator {
  color: var(--color-error);
  margin-left: var(--space-0_5);
  font-weight: var(--font-weight-bold);
}

/* Form field message (help/error) */
.form-message {
  display: flex;
  align-items: flex-start;
  gap: var(--space-1_5);
  font-size: var(--text-xs);
  line-height: var(--line-height-relaxed);
  margin: 0;
}

.form-message-help {
  color: var(--text-muted);
}

.form-message-error {
  color: var(--color-error-hover);
}

/* Form field states */
.form-field-error .form-label {
  color: var(--color-error-hover);
}

.form-field-required .form-label {
  font-weight: var(--font-weight-semibold);
}

.form-field-disabled {
  opacity: 0.6;
  pointer-events: none;
}

.form-field-disabled .form-label {
  color: var(--text-muted);
}

/* Form field groups */
.form-field-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.form-field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--space-4);
  align-items: start;
}

.form-field-row .form-field {
  margin: 0;
}

/* Form field with inline label */
.form-field-inline {
  flex-direction: row;
  align-items: center;
  gap: var(--space-3);
}

.form-field-inline .form-label {
  flex-shrink: 0;
  min-width: var(--space-30);
  margin: 0;
}

.form-field-inline .input-wrapper {
  flex: 1;
}

/* Reverse layout: control before label (switches, checkboxes, radios).

   HOUSE RULE -- a control and the text that belongs to it share ONE ROW, in reading
   order: control, then its label, then its help text. A switch stacked above its own
   label reads as two unrelated things; beside it, it reads as one sentence you can
   flip. This is the DEFAULT for every reverse field, so a module never has to
   remember to ask for it -- `inline: true` is not required and adds nothing here. */
.form-field--reverse {
  flex-direction: row;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2);
}

/* The control is whatever is neither the label nor the message. NOT :first-child --
   the DOM order is label / control / message and only `order` reverses it visually. */
.form-field--reverse > :not(.form-label):not(.form-message) {
  order: 0;
  flex: 0 0 auto;  /* the control keeps its size; the help text gives way instead */
}

.form-field--reverse .form-label {
  order: 1;
  flex: 0 0 auto;
}

.form-field--reverse .form-message {
  order: 2;
  flex: 1 1 auto;
  min-width: 0;  /* long help wraps inside the row rather than forcing it wider */
}

/* Responsive adjustments */
@media (max-width: 768px) {
  .form-field-row {
    grid-template-columns: 1fr;
    gap: var(--space-3);
  }

  .form-field-inline {
    flex-direction: column;
    align-items: flex-start;
    gap: var(--space-1_5);
  }

  .form-field-inline .form-label {
    min-width: auto;
  }

  /* A control and its text stay on one row at EVERY width -- the house rule above is
     not a desktop-only affordance, and a switch beside its label fits any screen.
     Only the label's inline min-width is dropped, so the row can close up.

     No caller should pass `inline` on a reverse field any more, but three did until
     2026-09-03, so this guard stays: a stray `inline` must not silently break the
     house row on a narrow screen. */
  .form-field-inline.form-field--reverse {
    flex-direction: row;
    align-items: center;
  }

  .form-field-inline.form-field--reverse .form-label {
    min-width: auto;
  }
}

@media (max-width: 640px) {
  .form-field {
    gap: var(--space-1);
  }

  .form-message {
    gap: var(--space-1);
  }
}