/**
 * button.css — Button ([data-ui~="button"])
 *
 * @layer      variables, components
 * @requires   _layers.css, _variables.css, _reset.css, _properties.css (--_*-resolved, --_focus-shadow), fields.css (--ui-field-* metrics), kbd.css (--ui-kbd-*)
 * @uses       color-mix(), oklch(from …) — variant hover/active tints
 * @uses       text-box — vertical trim (patchy browser support)
 * @uses       corner-shape — superellipse corners; unsupported browsers keep plain
 *             rounded corners
 * @uses       --_touch-min (touch screens) — the default and icon sizes grow to the touch
 *             floor, and each inline side gains half of what the height grew; sm and
 *             icon-sm keep their box and extend an invisible ::after hit area
 * @tokens     --ui-button-* (@layer variables; hooks are named after the utilities they back; metrics default to the --ui-field-* family)
 * @consumedby toggle.css, select.css, button-group.css, autocomplete.css, combobox.css,
 *             command.css, lightbox.css, menu.css, navigation-menu.css, popover.css
 *
 * Every utility-backed declaration reads one chain, highest first:
 *   var(--_<utility>-resolved, var(--_button-<utility>, var(--ui-button-<utility>)))
 * The resolver is set by the utilities layer from the element's `style` gate
 * (inline utilities and their tiers), the private by a variant or size on this
 * element, and the hook by :root, a subtree, or an inline hook.
 *
 * States step one way and active goes further than hover. Unfilled surfaces (the
 * default, outline, ghost) gain the muted role; filled ones fade toward the page;
 * status surfaces darken. Button and badge resolve the same in every variant
 * (badge-button-parity.browser.test.ts).
 */

@layer variables {
  :root {
    /* metrics follow --ui-field-* so buttons match adjacent fields; remap a
       --ui-button-* token to diverge. Colors stay independent: a button is not a
       field surface, and its border doesn't go primary on hover. */
    --ui-button-ring-color: var(--ui-field-ring-color);

    --ui-button-font-family: var(--font-family-body);
    --ui-button-font-size: var(--font-size-sm);
    --ui-button-font-weight: var(--font-weight-strong);
    --ui-button-leading: 1;

    --ui-button-bg: var(--color-background);
    --ui-button-bg--hover: var(--color-muted);
    --ui-button-bg--active: oklch(from var(--ui-button-bg--hover) l c h / calc(alpha * 1.5));

    --ui-button-text: var(--color-foreground);
    --ui-button-text--hover: var(--color-foreground);
    --ui-button-text--active: var(--color-foreground);

    --ui-button-border-width: var(--ui-field-border-width);
    --ui-button-border-style: var(--ui-field-border-style);
    --ui-button-border-color: var(--ui-field-border-color);
    --ui-button-border-color--hover: var(--ui-field-border-color);
    --ui-button-border-color--active: var(--ui-field-border-color);

    /* dual-mode: a scale number or a length */
    --ui-button-gap: var(--spacing);
    --ui-button-py: var(--ui-field-py);
    --ui-button-px: var(--ui-field-px);
    --ui-button-min-h: var(--ui-field-h);
    --ui-button-radius: var(--ui-field-radius);
    --ui-button-corner-shape: var(--ui-field-corner-shape);
    --ui-button-background-clip: border-box;

    --ui-button-icon-size: var(--ui-field-icon-size);

    /* never `none`: the focus ring layers into this box-shadow list */
    --ui-button-shadow: 0 0 transparent;
  }
}

/* Variant privates: non-inheriting so a variant never cascades into a nested
   button. Unset, they are guaranteed-invalid and the chain falls to the hook. */
@property --_button-ring-color {
  syntax: "*";
  inherits: false;
}

@property --_button-font-family {
  syntax: "*";
  inherits: false;
}

@property --_button-font-size {
  syntax: "*";
  inherits: false;
}

@property --_button-font-weight {
  syntax: "*";
  inherits: false;
}

@property --_button-leading {
  syntax: "*";
  inherits: false;
}

@property --_button-bg {
  syntax: "*";
  inherits: false;
}

@property --_button-bg--hover {
  syntax: "*";
  inherits: false;
}

@property --_button-bg--active {
  syntax: "*";
  inherits: false;
}

@property --_button-text {
  syntax: "*";
  inherits: false;
}

@property --_button-text--hover {
  syntax: "*";
  inherits: false;
}

@property --_button-text--active {
  syntax: "*";
  inherits: false;
}

@property --_button-border-width {
  syntax: "*";
  inherits: false;
}

@property --_button-border-style {
  syntax: "*";
  inherits: false;
}

@property --_button-border-color {
  syntax: "*";
  inherits: false;
}

@property --_button-border-color--hover {
  syntax: "*";
  inherits: false;
}

@property --_button-border-color--active {
  syntax: "*";
  inherits: false;
}

@property --_button-gap {
  syntax: "*";
  inherits: false;
}

@property --_button-py {
  syntax: "*";
  inherits: false;
}

@property --_button-px {
  syntax: "*";
  inherits: false;
}

@property --_button-min-h {
  syntax: "*";
  inherits: false;
}

@property --_button-radius {
  syntax: "*";
  inherits: false;
}

@property --_button-corner-shape {
  syntax: "*";
  inherits: false;
}

@property --_button-background-clip {
  syntax: "*";
  inherits: false;
}

@property --_button-icon-size {
  syntax: "*";
  inherits: false;
}

@property --_button-shadow {
  syntax: "*";
  inherits: false;
}

@layer ui.components {
  :where([data-ui~="button"]) {
    font-family: var(
      --_font-family-resolved,
      var(--_button-font-family, var(--ui-button-font-family))
    );
    font-size: var(--_font-size-resolved, var(--_button-font-size, var(--ui-button-font-size)));
    font-weight: var(
      --_font-weight-resolved,
      var(--_button-font-weight, var(--ui-button-font-weight))
    );
    line-height: var(--_leading-resolved, var(--_button-leading, var(--ui-button-leading)));

    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;

    --_gap-len: var(--_gap-resolved, var(--_button-gap, var(--ui-button-gap)));
    --_gap-num: var(--_gap-resolved, var(--_button-gap, var(--ui-button-gap)));
    column-gap: calc(var(--_gap-len) + var(--_gap-num) * var(--spacing));

    --_min-h-len: var(--_min-h-resolved, var(--_button-min-h, var(--ui-button-min-h)));
    --_min-h-num: var(--_min-h-resolved, var(--_button-min-h, var(--ui-button-min-h)));
    /* the touch floor: 0 on a mouse; a variant or size that must stay small sets it to 0px */
    --_touch-floor: var(--_touch-min);
    min-block-size: max(
      calc(var(--_min-h-len) + var(--_min-h-num) * var(--spacing)),
      var(--_touch-floor)
    );
    min-inline-size: max-content;

    --_py-len: var(--_py-resolved, var(--_button-py, var(--ui-button-py)));
    --_py-num: var(--_py-resolved, var(--_button-py, var(--ui-button-py)));
    padding-block: calc(var(--_py-len) + var(--_py-num) * var(--spacing));

    --_px-len: var(--_px-resolved, var(--_button-px, var(--ui-button-px)));
    --_px-num: var(--_px-resolved, var(--_button-px, var(--ui-button-px)));
    /* each inline side gains half of what the touch floor added to the height, so the
       label's margins grow evenly; the kbd ends below add it too */
    --_touch-px: max(
      0px,
      (var(--_touch-floor) - calc(var(--_min-h-len) + var(--_min-h-num) * var(--spacing))) / 2
    );
    padding-inline: calc(var(--_px-len) + var(--_px-num) * var(--spacing) + var(--_touch-px));

    border-width: var(
      --_border-width-resolved,
      var(--_button-border-width, var(--ui-button-border-width))
    );
    border-style: var(
      --_border-style-resolved,
      var(--_button-border-style, var(--ui-button-border-style))
    );
    border-color: var(
      --_border-color-resolved,
      var(--_button-border-color, var(--ui-button-border-color))
    );
    --_radius-len: var(--_radius-resolved, var(--_button-radius, var(--ui-button-radius)));
    --_radius-num: var(--_radius-resolved, var(--_button-radius, var(--ui-button-radius)));
    border-radius: calc(
      var(--_radius-len) + var(--_radius-num) * var(--spacing) * var(--radius-multiplier)
    );
    corner-shape: var(
      --_corner-shape-resolved,
      var(--_button-corner-shape, var(--ui-button-corner-shape))
    );

    color: var(--_text-resolved, var(--_button-text, var(--ui-button-text)));
    background-color: var(--_bg-resolved, var(--_button-bg, var(--ui-button-bg)));
    background-clip: var(
      --_bg-clip-resolved,
      var(--_button-background-clip, var(--ui-button-background-clip))
    );
    white-space-collapse: collapse;

    /* The ring is published to --_focus-shadow so the utilities layer's
       box-shadow emission keeps it above a --shadow or --ring utility; the ring
       widths grow from 0 on :focus-visible so the ring eases in. */
    --_ring-offset-width: 0px;
    --_ring-width: 0px;
    --_focus-in: max(0, -1 * sign(var(--_focus-ring-w, 1px)));
    --_ring-fill: color-mix(
      in oklch,
      var(--_ring-color-resolved, var(--_button-ring-color, var(--ui-button-ring-color)))
        var(--ring-opacity),
      transparent
    );
    --_focus-shadow:
      0 0 0 calc(var(--_ring-offset-width) * (1 - var(--_focus-in))) var(--ring-offset-shadow-color),
      0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * (1 - var(--_focus-in)))
        var(--_ring-fill, var(--ring-shadow-color)),
      inset 0 0 0 calc(var(--_ring-offset-width) * var(--_focus-in)) var(--ring-offset-shadow-color),
      inset 0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * var(--_focus-in))
        var(--_ring-fill, var(--ring-shadow-color));
    box-shadow:
      var(--_focus-shadow, 0 0 #0000),
      var(--_shadow-resolved, var(--_button-shadow, var(--ui-button-shadow)));

    cursor: default;
    text-decoration: none;

    /* transparent twin of the ring for forced-colors / high-contrast modes */
    outline: abs(var(--_focus-ring-w, var(--outline-width))) var(--outline-style) transparent;
    outline-offset: calc(
      var(--outline-offset) - var(--_focus-in) *
        (2 * var(--outline-offset) + abs(var(--_focus-ring-w, var(--outline-width))))
    );

    transition: var(--default-transition);

    user-select: none;
    text-box: trim-both ex alphabetic;
  }

  a[href] :where([data-ui~="button"]):not(:disabled, [aria-disabled="true"]),
  a[href]:where([data-ui~="button"]):not(:disabled, [aria-disabled="true"]) {
    cursor: pointer;
  }

  /* ===========================================================================
  BUTTON STATES
  =========================================================================== */

  @media (hover: hover) {
    :where([data-ui~="button"]):hover:not(:disabled, [aria-disabled="true"]) {
      color: var(--_text-resolved, var(--_button-text--hover, var(--ui-button-text--hover)));
      background-color: var(--_bg-resolved, var(--_button-bg--hover, var(--ui-button-bg--hover)));
      border-color: var(
        --_border-color-resolved,
        var(--_button-border-color--hover, var(--ui-button-border-color--hover))
      );
    }
  }

  :where([data-ui~="button"]):active:not(:disabled, [aria-disabled="true"]) {
    color: var(--_text-resolved, var(--_button-text--active, var(--ui-button-text--active)));
    background-color: var(--_bg-resolved, var(--_button-bg--active, var(--ui-button-bg--active)));
    border-color: var(
      --_border-color-resolved,
      var(--_button-border-color--active, var(--ui-button-border-color--active))
    );
  }

  :where([data-ui~="button"]):focus-visible:not(:disabled, [aria-disabled="true"]) {
    --_ring-offset-width: var(--ring-offset-width);
    --_ring-width: abs(var(--_focus-ring-w, var(--ring-width)));

    outline-color: transparent;
  }

  /* before the variants at the same specificity: a disabled primary keeps its color; only the default surface goes muted */
  :where([data-ui~="button"]:is(:disabled, [aria-disabled="true"])) {
    --_button-text: var(--color-muted-foreground);
    --_button-bg: var(--color-muted);
    --_button-shadow: 0 0 transparent;
    --decoration-line: none;

    cursor: default;
    opacity: 0.6;
    outline: none;
  }

  :where([data-ui~="button"])[aria-busy="true"] {
    cursor: progress;
  }

  /* ===========================================================================
  BUTTON ICONS
  =========================================================================== */

  :where([data-ui~="button"]) > svg {
    inline-size: var(--ui-button-icon-size);
    block-size: var(--ui-button-icon-size);
    pointer-events: none;
    justify-content: center;
    align-items: center;
    display: flex;
    padding: 0;
  }

  /* ===========================================================================
  BUTTON VARIANTS — data-button-variant writes the privates
  =========================================================================== */
  :where([data-ui~="button"][data-button-variant="outline"]) {
    --_button-bg: transparent;

    --_button-shadow: 0 0 transparent;
  }

  :where([data-ui~="button"][data-button-variant="primary"]) {
    --_button-bg: var(--color-primary);
    --_button-bg--hover: light-dark(
      oklch(from var(--color-primary) calc(l + (1 - l) * 0.2) c h),
      oklch(from var(--color-primary) calc(l * 0.8) c h)
    );
    --_button-bg--active: light-dark(
      oklch(from var(--color-primary) calc(l + (1 - l) * 0.3) c h),
      oklch(from var(--color-primary) calc(l * 0.7) c h)
    );

    --_button-text: var(--color-primary-foreground);
    --_button-text--hover: var(--color-primary-foreground);
    --_button-text--active: var(--color-primary-foreground);

    --_button-border-color: transparent;
    --_button-border-color--hover: transparent;
    --_button-border-color--active: transparent;

    --_button-shadow: 0 0 transparent;
  }

  :where([data-ui~="button"][data-button-variant="secondary"]) {
    --_button-bg: var(--color-secondary);
    --_button-bg--hover: light-dark(
      oklch(from var(--color-secondary) calc(l + (1 - l) * 0.2) c h),
      oklch(from var(--color-secondary) calc(l * 0.8) c h)
    );
    --_button-bg--active: light-dark(
      oklch(from var(--color-secondary) calc(l + (1 - l) * 0.3) c h),
      oklch(from var(--color-secondary) calc(l * 0.7) c h)
    );

    --_button-text: var(--color-secondary-foreground);
    --_button-text--hover: var(--color-secondary-foreground);
    --_button-text--active: var(--color-secondary-foreground);

    --_button-border-color: transparent;
    --_button-border-color--hover: transparent;
    --_button-border-color--active: transparent;

    --_button-shadow: 0 0 transparent;
  }

  :where([data-ui~="button"][data-button-variant="tertiary"]) {
    --_button-bg: var(--color-tertiary);
    --_button-bg--hover: light-dark(
      oklch(from var(--color-tertiary) calc(l + (1 - l) * 0.2) c h),
      oklch(from var(--color-tertiary) calc(l * 0.8) c h)
    );
    --_button-bg--active: light-dark(
      oklch(from var(--color-tertiary) calc(l + (1 - l) * 0.3) c h),
      oklch(from var(--color-tertiary) calc(l * 0.7) c h)
    );

    --_button-text: var(--color-tertiary-foreground);
    --_button-text--hover: var(--color-tertiary-foreground);
    --_button-text--active: var(--color-tertiary-foreground);

    --_button-border-color: transparent;
    --_button-border-color--hover: transparent;
    --_button-border-color--active: transparent;

    --_button-shadow: 0 0 transparent;
  }

  :where([data-ui~="button"][data-button-variant="muted"]) {
    /* the translucent muted role: fades toward the surface beneath */
    --_button-bg: var(--color-muted);
    --_button-bg--hover: oklch(from var(--_button-bg) l c h / calc(alpha * 0.66));
    --_button-bg--active: oklch(from var(--_button-bg) l c h / calc(alpha * 0.33));

    --_button-text: var(--color-foreground);
    --_button-text--hover: var(--color-foreground);
    --_button-text--active: var(--color-foreground);

    --_button-border-color: transparent;
    --_button-border-color--hover: transparent;
    --_button-border-color--active: transparent;

    --_button-shadow: 0 0 transparent;
  }

  :where([data-ui~="button"][data-button-variant="ghost"]) {
    --_button-bg: transparent;
    /* the translucent muted role: shows the surface beneath and follows a retheme */
    --_button-bg--hover: var(--color-muted);
    --_button-bg--active: oklch(from var(--_button-bg--hover) l c h / calc(alpha * 1.5));

    --_button-text: var(--color-foreground);
    --_button-text--hover: var(--color-foreground);
    --_button-text--active: var(--color-foreground);

    --_button-border-color: transparent;
    --_button-border-color--hover: transparent;
    --_button-border-color--active: transparent;

    --_button-shadow: 0 0 transparent;
  }

  /* Status variants (info, success, warning, destructive) pair the role's
     surface with its -foreground. No border. One rule derives the states from
     the surface; each variant names only its role. status-contrast.browser.test.ts
     checks 4.5:1 at rest, hover, and active. */
  :where(
    [data-ui~="button"]:is(
      [data-button-variant="info"],
      [data-button-variant="success"],
      [data-button-variant="warning"],
      [data-button-variant="destructive"]
    )
  ) {
    --_button-bg--hover: oklch(from var(--_button-bg) calc(l - 0.025) c h);
    --_button-bg--active: oklch(from var(--_button-bg) calc(l - 0.05) c h);

    --_button-text--hover: var(--_button-text);
    --_button-text--active: var(--_button-text);

    --_button-border-color: transparent;
    --_button-border-color--hover: transparent;
    --_button-border-color--active: transparent;

    --_button-shadow: 0 0 transparent;
  }

  :where([data-ui~="button"][data-button-variant="info"]) {
    --_button-ring-color: var(--color-info-foreground);
    --_button-bg: var(--color-info);
    --_button-text: var(--color-info-foreground);
  }

  :where([data-ui~="button"][data-button-variant="success"]) {
    --_button-ring-color: var(--color-success-foreground);
    --_button-bg: var(--color-success);
    --_button-text: var(--color-success-foreground);
  }

  :where([data-ui~="button"][data-button-variant="warning"]) {
    --_button-ring-color: var(--color-warning-foreground);
    --_button-bg: var(--color-warning);
    --_button-text: var(--color-warning-foreground);
  }

  :where([data-ui~="button"][data-button-variant="destructive"]) {
    --_button-ring-color: var(--color-destructive-foreground);
    --_button-bg: var(--color-destructive);
    --_button-text: var(--color-destructive-foreground);
  }

  :where([data-ui~="button"][data-button-variant="link"]) {
    --_button-bg: transparent;
    --_button-bg--hover: transparent;
    --_button-bg--active: transparent;

    --_button-text: var(--color-foreground);
    --_button-text--hover: var(--color-muted-foreground);
    --_button-text--active: var(--color-muted-foreground);

    --_button-border-style: none;

    --_button-min-h: 0;
    --_button-px: 0;
    --_button-radius: 0;
    /* inline text: no touch floor (it would break line flow) */
    --_touch-floor: 0px;

    --_button-shadow: 0 0 transparent;
  }

  /* ===========================================================================
  BUTTON SIZES — data-button-size
  =========================================================================== */

  :where([data-ui~="button"][data-button-size="sm"]) {
    --_button-min-h: calc(var(--spacing) * 6);
    /* a 16px icon, this, and the border fit the 24px box */
    --_button-py: calc(var(--spacing) * 0.75);
    --_button-px: calc(var(--spacing) * 2);
    --_button-radius: var(--radius-sm);
    --_button-font-size: var(--font-size-xs);
    /* dense by choice: the box stays small and ::after takes the touch floor */
    --_touch-floor: 0px;
    position: relative;
  }

  :where(
    :is(ui-button-group, [data-ui~="button-group"]):has([data-ui~="button"][data-button-size="sm"])
  ) {
    --_button-group-radius: var(--radius-sm);
  }

  :where([data-ui~="button"][data-button-size="icon"]) {
    block-size: max(
      calc(var(--_min-h-len) + var(--_min-h-num) * var(--spacing)),
      var(--_touch-floor)
    );
    inline-size: max(
      calc(var(--_min-h-len) + var(--_min-h-num) * var(--spacing)),
      var(--_touch-floor)
    );
    padding: 0;
    aspect-ratio: 1 / 1;
  }

  :where([data-ui~="button"][data-button-size="icon-sm"]) {
    --_button-min-h: calc(var(--spacing) * 6);
    --_button-px: calc(var(--spacing) * 2);
    --_button-radius: var(--radius-sm);
    /* the hook, not a private: it must inherit to reach the icon */
    --ui-button-icon-size: calc(var(--spacing) * 3.5);
    --_touch-floor: 0px;
    position: relative;

    block-size: calc(var(--_min-h-len) + var(--_min-h-num) * var(--spacing));
    inline-size: calc(var(--_min-h-len) + var(--_min-h-num) * var(--spacing));
    padding: 0;
    aspect-ratio: 1 / 1;
  }

  /* touch: sm and icon-sm keep their box; a transparent ::after centred on the button
     is at least --_touch-min square (it collapses to the box on a mouse) */
  :where([data-ui~="button"]:is([data-button-size="sm"], [data-button-size="icon-sm"]))::after {
    content: "";
    position: absolute;
    inset: min(0px, calc((100% - var(--_touch-min)) / 2));
  }

  /* ===========================================================================
  BUTTON KBD
  =========================================================================== */

  /* Descendant (not child) so keys wrapped in a ui-kbd-group still resize */
  :where([data-ui~="button"]) :where(kbd) {
    --ui-kbd-min-w: calc(var(--spacing) * 4.5);
    --ui-kbd-min-h: calc(var(--spacing) * 4.5);
    --ui-kbd-icon-size: calc(var(--spacing) * 3);
  }

  :where([data-ui~="button"]):has(
    > :where(kbd, ui-kbd-group, [data-ui~="kbd-group"]):last-child:not(:only-child)
  ) {
    padding-inline-end: calc(var(--spacing) * 1.5 + var(--_touch-px));
  }

  :where([data-ui~="button"]):has(
    > :where(kbd, ui-kbd-group, [data-ui~="kbd-group"]):first-child:not(:only-child)
  ) {
    padding-inline-start: calc(var(--spacing) * 1.5 + var(--_touch-px));
  }

  :where([data-ui~="button"][data-button-size="sm"]):has(
    > :where(kbd, ui-kbd-group, [data-ui~="kbd-group"]):last-child:not(:only-child)
  ) {
    padding-inline-end: calc(var(--spacing) * 0.5 + var(--_touch-px));
  }

  :where([data-ui~="button"][data-button-size="sm"]):has(
    > :where(kbd, ui-kbd-group, [data-ui~="kbd-group"]):first-child:not(:only-child)
  ) {
    padding-inline-start: calc(var(--spacing) * 0.5 + var(--_touch-px));
  }
}
