/* @canonical/react-ds-global-form — Form component tokens
 *
 * All available custom properties are listed below.
 * Commented-out properties are valid extension points that
 * themes or consumers may set.
 *
 * Terminology:
 *   block  = vertical axis   (block-start = top, block-end = bottom)
 *   inline = horizontal axis (inline-start = left in LTR, right in RTL)
 *
 * Layer: ds.components.global (@canonical/styles README, "Cascade layers").
 */

/* Density & baseline alignment (context × density → control height + baseline).
 *
 * No `layer()` keyword on this import: density.css declares `ds.components.global`
 * itself, like every other sheet here, and `layer()` would nest that declaration
 * one level deeper — `ds.components.global.ds.components.global`, a sublayer that
 * loses to this file's own rules. Measured in Chromium 151. An `@import` here
 * takes `layer(ds.components.global)` only when the sheet it names is not itself
 * wrapped. */
@import url("./density.css");

@layer ds.components.global {
  :root {
    /* ── Form grid layout ───────────────────────────────────────────── */
    /* Forms are subgrids: they inherit columns from a parent .grid      */
    /* context (either .grid.responsive or .grid.intrinsic).             */

    /* Field position within form grid — default: full width */
    --form-field-columns: 1 / -1;

    /* Label and payload column placement — stacked by default */
    --form-label-columns: 1 / -1;
    --form-payload-columns: 1 / -1;

    /* Side layout (set on form wrapper to switch all fields): */
    /* --form-label-columns:   1 / 3;   */ /* label in first 2 cols  */
    /* --form-payload-columns: 3 / -1;  */ /* payload in remaining   */

    /* Per-field column overrides (set via style prop on individual fields): */
    /* --form-field-columns: 1 / 7;  */ /* field spans first 6 columns */
    /* --form-field-columns: 7 / -1; */ /* field spans last 6 columns  */

    /* ── Field layout spacing ───────────────────────────────────────── */
    /* Container-owned: the form/field wrapper controls all spacing.     */

    /* Gaps are split by AXIS so no token spans both directions (a density tier can
       then tune each axis independently, and the names stay honest):
         BLOCK (vertical):  field-block-gap · field-label-gap · group-gap
         INLINE (horizontal): field-inline-gap                                    */
    /* No-density-class defaults — mirror the density matrix's app/comfortable cell
       (F-O4, 15/07 review). The density-scoped block further down re-derives these
       per cell when a context/density class is present. */
    --form-field-block-gap: calc(
      var(--baseline-height) *
      10
    ); /* 40px — block: gap BETWEEN fields */
    --form-field-label-gap: var(
      --space-100
    ); /* 8px — block: gap from a field's LABEL down to its control */
    --form-field-label-description-gap: var(
      --space-050
    ); /* 4px — block: gap from a LABEL down to a leading description */
    --form-group-gap: var(
      --space-100
    ); /* 8px — block: gap from desc/error to the input */
    --form-field-inline-gap: var(
      --container-gap-default
    ); /* 16px — inline: label↔input, prefix/suffix (HORIZONTAL only) */

    /* ── Input chrome ───────────────────────────────────────────────── */
    /* Component-owned: internal padding regardless of context.           */

    --form-input-padding-block: var(
      --component-padding-block
    ); /*  8px — block (vertical)    */

    /* Chrome height snapped to baseline grid — border-box includes border */
    --form-input-height: calc(var(--space-baseline) * 6); /* 40px — 5bU */
    --form-input-padding-inline: var(
      --component-padding-inline
    ); /* 16px — inline (horizontal) */

    /* Per-side input padding overrides: */
    /* --form-input-padding-block-start:  … ; */ /* top padding    */
    /* --form-input-padding-block-end:    … ; */ /* bottom padding */
    /* --form-input-padding-inline-start: … ; */ /* left padding   */
    /* --form-input-padding-inline-end:   … ; */ /* right padding  */

    /* ── Borders ────────────────────────────────────────────────────── */

    --form-input-border-width: var(
      --dimension-stroke-thickness-medium
    ); /* 1px  */
    --form-input-border-color: var(--color-border);
    --form-input-border-radius: var(--dimension-radius-small); /* 0 */

    /* ── Canonical theme: bottom-border-only inputs ──────────────── */
    /* Only the sides that DIFFER from the base are declared.         */
    /* Bottom border inherits --form-input-border-color (no override) */
    --form-input-border-color-top: transparent;
    --form-input-border-color-right: transparent;
    --form-input-border-color-left: transparent;

    /* Per-side border overrides — themes set only sides that differ.
     * When undefined, var() falls through to the base value above.   */
    /* --form-input-border-color-bottom: … ; */ /* bottom border color */
    /* --form-input-border-width-top:    … ; */ /* top border width    */
    /* --form-input-border-width-right:  … ; */ /* right border width  */
    /* --form-input-border-width-bottom: … ; */ /* bottom border width */
    /* --form-input-border-width-left:   … ; */ /* left border width   */

    /* Per-corner radius overrides: */
    /* --form-input-border-radius-top-left:     … ; */
    /* --form-input-border-radius-top-right:    … ; */
    /* --form-input-border-radius-bottom-right: … ; */
    /* --form-input-border-radius-bottom-left:  … ; */

    /* ── Colors ─────────────────────────────────────────────────────── */

    --form-input-background: var(--color-foreground-input);
    --form-input-color: var(--color-text);
    --form-input-font-size: var(--typography-text-primary-font-size);
    --form-input-line-height: var(--typography-text-primary-line-height);

    /* ── Label ──────────────────────────────────────────────────────── */

    /* Field labels use regular paragraph weight, not bold (per design review). */
    --form-label-font-weight: var(--typography-text-primary-font-weight);
    --form-label-font-size: var(--typography-text-primary-font-size);
    --form-label-color: var(--color-text);
    /* --form-label-padding-block:  … ; */ /* block padding for label  */
    /* --form-label-padding-inline: … ; */ /* inline padding for label */
    /* Required marker (shown after the label of required fields in the default
     * "required" indicator mode): */
    /* --form-required-marker:       "*";                    */ /* marker glyph    */
    /* --form-required-marker-gap:   0.25ch;                 */ /* gap after marker*/
    /* --form-required-marker-color: var(--form-label-color);*/ /* marker colour   */

    /* ── Description ────────────────────────────────────────────────── */

    /* Description reads the SECONDARY type ramp (design review): it is subordinate
       supporting text, so it takes the smaller secondary size, not the primary
       body size. Colour is already the muted token. */
    --form-description-font-size: var(--typography-text-secondary-font-size);
    --form-description-color: var(--color-text-muted);

    /* ── Error ──────────────────────────────────────────────────────── */

    --form-error-font-size: var(--typography-text-primary-font-size);
    --form-error-color: var(--modifier-color-text, var(--color-text-error));

    /* ── State colors ───────────────────────────────────────────────── */

    --form-input-border-color-focus: var(--color-focus-ring);
    --form-input-border-color-error: var(--color-border-error);
    --form-input-background-disabled: var(--color-foreground-input-disabled);
    --form-input-color-disabled: var(--disabled--color-text);
    /* Design review (15/07): a disabled control shows no border. Default the
     * disabled border to transparent; a theme wanting a visible disabled border
     * can still override this token (e.g. back to `--disabled--color-border`). */
    --form-input-border-color-disabled: transparent;

    /* ── Focus ring ─────────────────────────────────────────────────── */

    --form-focus-ring-color: var(--color-focus-ring);
    --form-focus-ring-color-error: var(--color-focus-ring-error);
    /* --form-focus-ring-width:  … ; */ /* focus ring thickness (default: 1px via box-shadow) */
    /* --form-focus-ring-offset: … ; */ /* focus ring offset from border */
  }

  /* ── Density → form channel mapping ──────────────────────────────────────────
   * The FORM layer decides which generic density primitive (density.css exposes
   * --density-line-height, --density-padding-inline, and the neutral vertical rungs
   * --density-space-sm/-md/-lg) each of its own tokens follows. Density knows
   * nothing about "field"; this is where the form makes that choice.
   *
   * Scoped to the density classes so a plain form (no density class) keeps the
   * :root defaults above. The var() fallbacks keep values sane if only a CONTEXT
   * class (.app/.site/.docs) is present without a density class.
   *
   *   INLINE (horizontal): control padding + label↔input / prefix-suffix gap
   *     → --density-padding-inline
   *   BLOCK (vertical), by magnitude:
   *     between-fields     → --density-space-lg  (widest structural gap, 40px app)
   *     label → control    → --density-space-md  (label to its own control, 8px app)
   *     desc/error → input → --density-space-sm  (tight annotation gap, 8px)
   *     label → description → --density-space-xs (tightest, 4px)
   *   All rungs are whole baseline multiples, so vertical rhythm holds.
   *   (space-md/-lg were retuned + space-xs added per the 15/07 review — F-O4;
   *    the density matrix now carries Diana's numbers directly, so the form maps
   *    straight onto the rungs rather than pinning literals.) */
  :where(.comfortable, .dense, .app, .site, .docs) {
    --form-input-padding-inline: var(
      --density-padding-inline,
      var(--space-200)
    );
    --form-field-inline-gap: var(--density-padding-inline, var(--space-200));
    --form-field-block-gap: var(
      --density-space-lg,
      calc(var(--baseline-height) * 10)
    );
    --form-field-label-gap: var(
      --density-space-md,
      calc(var(--baseline-height) * 2)
    );
    --form-group-gap: var(--density-space-sm, calc(var(--baseline-height) * 2));
    --form-field-label-description-gap: var(
      --density-space-xs,
      calc(var(--baseline-height) * 1)
    );
  }

  /* ── .ds.input.chrome — shared input chrome ─────────────────────────
   * Applied on native <textarea>/<select> directly, and on the
   * composite .ds.input.text wrapper for <input> + prefix/suffix.
   *
   * Inline padding is deliberately NOT set here. On composite wrappers the
   * text-bearing leaf is the inner <input>, which carries its own
   * `padding-inline`; if the wrapper ALSO had it the text would be inset twice
   * (and prefix/suffix would sit off the edge). So the inline padding lives on
   * the leaf in every case: the inner <input> for composite inputs (text,
   * number, password, phone …), and the element itself for the direct-chrome
   * inputs (date, time, datetime, select, textarea), which set it locally.
   * Only the block padding (baseline-grid height) is shared here.
   */
  .ds.input.chrome {
    display: block;
    box-sizing: border-box;
    width: 100%;
    /* Explicit height snapped to baseline grid — border-box includes border */
    height: var(--form-input-height);
    padding-block: var(--form-input-padding-block);

    /* Base border */
    border: var(--form-input-border-width) solid var(--form-input-border-color);

    /* Per-side overrides — only effective when a theme defines them.
     * When undefined, var() falls through to the base shorthand. */
    border-top-color: var(
      --form-input-border-color-top,
      var(--form-input-border-color)
    );
    border-right-color: var(
      --form-input-border-color-right,
      var(--form-input-border-color)
    );
    border-bottom-color: var(
      --form-input-border-color-bottom,
      var(--form-input-border-color)
    );
    border-left-color: var(
      --form-input-border-color-left,
      var(--form-input-border-color)
    );

    border-top-width: var(
      --form-input-border-width-top,
      var(--form-input-border-width)
    );
    border-right-width: var(
      --form-input-border-width-right,
      var(--form-input-border-width)
    );
    border-bottom-width: var(
      --form-input-border-width-bottom,
      var(--form-input-border-width)
    );
    border-left-width: var(
      --form-input-border-width-left,
      var(--form-input-border-width)
    );

    border-radius: var(--form-input-border-radius);
    background-color: var(--form-input-background);
    color: var(--form-input-color);
    font-family: var(--typography-text-primary-font-family);
    font-size: var(--form-input-font-size);
    line-height: var(--form-input-line-height);
    /* Durations come from the motion token, never from a literal: `@canonical/styles`
     * zeroes the three duration tokens under `prefers-reduced-motion: reduce`, so a
     * literal here would keep animating for a reader who asked their system not to.
     * Every transition in this package reads `--motion-duration-fast` for that
     * reason (motion.css, "Reduced motion"). */
    transition:
      border-color var(--motion-duration-fast) ease,
      box-shadow var(--motion-duration-fast) ease;

    &:hover:not(:disabled, :has(input:disabled)) {
      background-color: var(
        --hover--color-foreground-input,
        var(--color-foreground-input-hover)
      );
    }

    /* Focus — all four borders become visible */
    &:focus-visible,
    &:focus-within {
      outline: none;
      border-color: var(--form-input-border-color-focus);
      border-top-color: var(--form-input-border-color-focus);
      border-right-color: var(--form-input-border-color-focus);
      border-left-color: var(--form-input-border-color-focus);
      box-shadow: 0 0 0 1px var(--form-focus-ring-color);
    }

    /* Disabled — per design review (15/07): a disabled control needs no border.
     * The border BOX is kept (transparent) so the border-box height is unchanged
     * and a disabled control lines up with an enabled one; only the paint is
     * dropped. `--form-input-border-color-disabled` defaults to transparent (see
     * :root) but stays a themeable override point. The background + muted text
     * carry the disabled affordance instead. */
    &:disabled,
    &:has(input:disabled) {
      border-color: var(--form-input-border-color-disabled);
      border-top-color: var(--form-input-border-color-disabled);
      border-right-color: var(--form-input-border-color-disabled);
      border-bottom-color: var(--form-input-border-color-disabled);
      border-left-color: var(--form-input-border-color-disabled);
      background-color: var(--form-input-background-disabled);
      color: var(--form-input-color-disabled);
      cursor: not-allowed;
    }
  }

  /* ── Danger ──────────────────────────────────────────────────────────
   * Per design review (15/07): a validation colour change must be confined to the
   * validation MESSAGE and the input's BOTTOM border — it must not repaint the
   * label or the full box outline. So the general label recolour is gone; the
   * error signal on a normal bordered input is carried by its bottom border alone
   * (below), and the FieldError message carries the error colour in its own text.
   *
   * EXCEPTION — the borderless choices fieldset. A choices group (radios/checkboxes)
   * has no single input border to paint (each option is its own borderless control),
   * so its `<legend>` (a `.ds.field-label`) is the ONLY place an error can be shown
   * without lighting up every option box. Such a group renders its Wrapper label as
   * a `<legend>` (the `mockLabel` path), so the recolour is SCOPED to a legend
   * label — a normal field's `<label>` is untouched. */
  .danger > legend.ds.field-label {
    color: var(--form-error-color);

    /* The required "*" marker recolours WITH the label (it pins its own colour to
     * `--form-label-color` by default, so it must be overridden here). The
     * "(optional)" hint keeps its muted colour — it is subordinate, not an error
     * signal — via its own explicit `color` in the Label styles. */
    &[data-required]::after {
      color: var(--form-error-color);
    }
  }

  /* Input chrome: recolour ONLY the bottom border (the base theme keeps the other
   * three sides transparent). Setting the bottom-border override token — not the
   * four-side `border-color` shorthand — means the error signal cannot bleed onto
   * the top/right/left sides. Focus still lights all four (below). */
  .danger > .payload .ds.input.chrome {
    --form-input-border-color-bottom: var(--form-input-border-color-error);
  }

  .danger > .payload .ds.input.chrome:focus-visible,
  .danger > .payload .ds.input.chrome:focus-within {
    border-color: var(--form-focus-ring-color-error);
    border-top-color: var(--form-focus-ring-color-error);
    border-right-color: var(--form-focus-ring-color-error);
    border-left-color: var(--form-focus-ring-color-error);
    box-shadow: 0 0 0 1px var(--form-focus-ring-color-error);
  }

  /* ── .ds.form — form as subgrid ───────────────────────────────────── */
  .ds.form {
    display: grid;
    grid-template-columns: subgrid;
    grid-column: 1 / -1;
    row-gap: var(--form-field-block-gap);
  }

  /* ── Layout presets ───────────────────────────────────────────────── */

  /* This preset was the one rule in this package already in a layer, its own
   * `@layer ds.components` — which put it ABOVE both component tiers, not
   * between them: a rule written directly into a layer lands in that layer's
   * implicit final sublayer, and that outranks every named sublayer beneath it,
   * `ds.components.app` included. It is in this file's `ds.components.global`
   * wrap now like every other rule here, which demotes it to where it belongs —
   * below an application tier's rules — and leaves one layer for a reader of
   * this file to hold rather than two. Nothing pragma ships is written directly
   * into `ds.components`. */
  /* Side layout: label beside input.
   * Apply on the form or a wrapper to switch all fields to side layout. */
  .form-layout-side {
    --form-label-columns: 1 / 3;
    --form-payload-columns: 3 / -1;
  }
}
