/* Button — one component (Storybook's Button + DropdownButton merged).
   Figma source: PR27 Library "Button" (node 16:2578); sizes and the
   neutral / green recipes come from there, the rest extends them.

   Axes (all classes, so plain HTML and the React <Button> render the same):
     size    .btn--s 24 · .btn--m 32 · .btn--l 42
     level   .btn--primary (solid) · .btn--secondary (tint) · .btn--link (text)
     color   .btn--neutral .btn--green .btn--red (app buttons: these three only)
     shape   Regular (radius 6) · .btn--rounded (pill; a circle when icon-only)
     content text · icon + text · text + icon · icon + text + icon · .btn--icon-only
   States: hover, pressed, focus-visible, disabled, loading (aria-busy) —
   plus .is-hover / .is-pressed / .is-focus / .is-disabled to force a look
   on spec boards.

   One rule for every family (Santiago, 2026-09-28): hover keeps the colour
   and lifts (Shadow 1); pressed drops the lift (Shadow 0) and takes the
   family's darker solid (primary) or stronger tint (secondary).
   Content colour — manual exceptions (Santiago, 2026-09-28):
     secondary: n150 text + icon on every family (the tint carries the hue);
     primary: white on every family except yellow (dark, n200). Orange and
     the UFLI colours keep white by choice even though it's under 4.5:1.
     link: the label is neutral on every family (n130, pressed n160); only
     the icons take the family's color (Santiago, 2026-10-08). */

@layer components {

  .btn {
    /* Per-family recipe, set by the colour class below (neutral default). */
    --btn-solid: var(--n140);      --btn-solid-press: var(--n200);   --btn-on-solid: var(--n0);
    --btn-tint: var(--neutralTint25); --btn-tint-press: var(--n35); /* always a tint, never a solid (Santiago 2026-10-01); neutralTint25 is being retuned to read on n15 */ 
    --btn-link: var(--n130);       --btn-link-press: var(--n160);    --btn-link-line: currentColor;
    /* Color rule (color.css): what a part inside can follow (.follows-color). */
    --color: var(--btn-link); --color-tint: var(--btn-tint);

    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--btn-gap, var(--space-4));
    border: none;
    padding-block: 0; /* the browser's 1px button padding was leaking in (inspector, 2026-10-02) */
    border-radius: var(--radius-6);
    /* A label too long for its space wraps to more lines and the Button grows
       taller (WCAG 1.4.10, picked A 2026-11-14). flex-shrink 0 keeps buttons from
       being squeezed in rows: a label only wraps once the Button is as wide as
       its container (max-width), never earlier. */
    white-space: normal;
    text-wrap: balance;
    text-align: center;
    flex-shrink: 0;
    max-width: 100%;
    cursor: pointer;
    text-decoration: none;
    transition: background-color var(--dur-120) var(--ease-fade), color var(--dur-120) var(--ease-fade), box-shadow var(--dur-320) var(--ease-fade), /* shadow changes always animate, --dur-320 */
                scale var(--dur-320) var(--ease-pop); /* press: let go → back with a slight bounce */
  }
  /* In tables a column sizes to its narrowest content, so a wrapping label
     would squeeze the column ("Check / in"). Table buttons stay on one line;
     a wide table scrolls inside its own frame instead. */
  :is(table, .tbl) .btn { white-space: nowrap; }

  /* ── Press (2026-10-19, after animate-ui's button): every button sinks to
     97% while held — quickly down, back up with --ease-pop's slight bounce.
     Not on Link level (text only) and not when disabled. */
  .btn:not(.btn--link):not(:disabled):active, .btn:not(.btn--link).is-pressed {
    scale: 0.97;
    transition: background-color var(--dur-120) var(--ease-fade), color var(--dur-120) var(--ease-fade), box-shadow var(--dur-320) var(--ease-fade),
                scale var(--dur-120) var(--ease-out);
  }

  /* ── Toggle pop (2026-10-19): a toggle button (aria-pressed, Button's
     `pressed` prop) pops with a slight bounce each time it flips. Two
     identical keyframes so the animation restarts on every flip
     (data-pop alternates a / b; unset on first render, so nothing pops
     on load). The icon swaps with IconSwap's pop at the same time. */
  .btn[data-pop="a"] { animation: btn-pop-a var(--dur-320) var(--ease-pop); }
  .btn[data-pop="b"] { animation: btn-pop-b var(--dur-320) var(--ease-pop); }
  @keyframes btn-pop-a { from { scale: 0.9; } }
  @keyframes btn-pop-b { from { scale: 0.9; } }

  /* ── Sizes (Figma: S 24 / M 32 / L 42). Icons 12 / 16 / 16; the side next
     to an icon loses a little padding so the icon doesn't look inset. The
     sides are read from btn-icon--lead / --trail, not :first/:last-child —
     a lone icon is both, which squeezed the label side too (2026-10-01). */
  /* No fixed heights (Corey, 2026-10-06): the height comes from padding +
     content, so the button grows if its text does (zoom, longer labels).
     Content is one line as tall as the icon (12 / 16 / 16), so buttons with
     and without icons match; padding brings them to 24 / 32 / 42, the same
     as Field and Segmented Control. */
  .btn--s { --btn-gap: 4px; --btn-py: var(--space-6); padding-block: var(--btn-py); padding-inline: var(--space-8);  font: var(--title-xs); letter-spacing: var(--title-xs-ls); line-height: 12px; }
  .btn--m { --btn-gap: 6px; --btn-py: var(--space-8); padding-block: var(--btn-py); padding-inline: var(--space-16); font: var(--title-s);  letter-spacing: var(--title-s-ls); line-height: 16px; }
  .btn--l { --btn-gap: 8px; --btn-py: 13px; /* off-scale, keeps L at 42 to match Field L / Segmented L */ padding-block: var(--btn-py); padding-inline: 20px; /* off-scale by decision (snap review 2026-09-30) */ font: var(--title-s);  letter-spacing: var(--title-s-ls); line-height: 16px; }
  .btn--s:has(> .btn-icon--lead) { padding-left: var(--space-6); }   .btn--s:has(> .btn-icon--trail) { padding-right: var(--space-6); }
  .btn--m:has(> .btn-icon--lead) { padding-left: var(--space-12); }  .btn--m:has(> .btn-icon--trail) { padding-right: var(--space-12); }
  .btn--l:has(> .btn-icon--lead) { padding-left: var(--space-16); }  .btn--l:has(> .btn-icon--trail) { padding-right: var(--space-16); }
  .btn-icon { display: inline-flex; flex-shrink: 0; }
  /* Logo (sign in with …): a brand mark keeps its own colors; disabled it goes grey. */
  .btn-logo { display: inline-flex; }
  .btn-logo > svg { width: 100%; height: 100%; }
  .btn:disabled .btn-logo, .btn[aria-disabled="true"] .btn-logo { filter: grayscale(1); opacity: 0.5; }

  /* Icon only: a square (or a circle when rounded). */
  .btn--icon-only { padding: var(--btn-py) !important; } /* !important: beats the icon-side padding rules above */

  /* ── Shape */
  .btn--rounded { border-radius: 999px; }
  /* Round pills need more room at the ends than squared buttons — the
     curve eats into the side padding, most visibly after the label on S
     (2026-10-01): S +4 / M +2 / L +2, and the icon side follows. */
  .btn--rounded.btn--s { padding-inline: var(--space-12); }
  .btn--rounded.btn--m { padding-inline: 18px; }
  .btn--rounded.btn--l { padding-inline: 22px; }
  .btn--rounded.btn--s:has(> .btn-icon--lead) { padding-left: var(--space-8); }   .btn--rounded.btn--s:has(> .btn-icon--trail) { padding-right: var(--space-8); }
  .btn--rounded.btn--m:has(> .btn-icon--lead) { padding-left: 14px; }              .btn--rounded.btn--m:has(> .btn-icon--trail) { padding-right: 14px; }
  .btn--rounded.btn--l:has(> .btn-icon--lead) { padding-left: 18px; }              .btn--rounded.btn--l:has(> .btn-icon--trail) { padding-right: 18px; }

  /* ── Colours: each sets the recipe; the levels below read it. */
  .btn--neutral { }
  .btn--green      { --btn-solid: var(--green100);      --btn-solid-press: var(--green120);      --btn-tint: var(--greenTint25);      --btn-tint-press: var(--greenTint50);           --btn-link: var(--green100);      --btn-link-press: var(--green120); }
  .btn--red        { --btn-solid: var(--red100);        --btn-solid-press: var(--red120);        --btn-tint: var(--redTint25);        --btn-tint-press: var(--redTint50);               --btn-link: var(--red100);        --btn-link-press: var(--red120); }

  /* ── Levels */
  .btn--primary { background-color: var(--btn-solid); color: var(--btn-on-solid); }
  .btn--primary.is-hover { box-shadow: var(--shadow-1); }
  @media (hover: hover) { .btn--primary:hover { box-shadow: var(--shadow-1); } }
  .btn--primary:active, .btn--primary.is-pressed { background-color: var(--btn-solid-press); box-shadow: var(--shadow-0); }

  .btn--secondary { background-color: var(--btn-tint); color: var(--btn-on-tint); }
  /* Hover lightens the tint toward white instead of lifting with a shadow:
     on the n15 page a shadow on a translucent pill read muddy (2026-10-01). */
  .btn--secondary.is-hover { background-color: color-mix(in srgb, var(--btn-tint), var(--n0) 55%); }
  @media (hover: hover) { .btn--secondary:hover { background-color: color-mix(in srgb, var(--btn-tint), var(--n0) 55%); } }
  .btn--secondary:active, .btn--secondary.is-pressed { background-color: var(--btn-tint-press); box-shadow: var(--shadow-0); }

  .btn--link { background: none; color: var(--btn-link); padding-block: 0; padding-inline: 0 !important; text-decoration: underline; text-decoration-color: var(--btn-link-rest, transparent); text-underline-offset: 3px; text-decoration-thickness: 1.5px; }
  .btn--link.is-hover { text-decoration-color: var(--btn-link-line); }
  @media (hover: hover) { .btn--link:hover { text-decoration-color: var(--btn-link-line); } }
  .btn--link:active, .btn--link.is-pressed { color: var(--btn-link-press); }
  /* Label neutral, icons in the family's color (2026-10-08). */
  .btn--link { color: var(--n130); }
  .btn--link > .btn-icon { color: var(--btn-link); }
  .btn--link:active, .btn--link.is-pressed { color: var(--n160); }
  .btn--link:active > .btn-icon, .btn--link.is-pressed > .btn-icon { color: var(--btn-link-press); }
  .btn--link:disabled > .btn-icon, .btn--link.is-disabled > .btn-icon { color: inherit; }

  /* ── Focus: a ring outside the button, whatever the level or colour. */
  .btn:focus-visible, .btn.is-focus { outline: 2px solid transparent; box-shadow: 0 0 0 2px var(--surface, var(--n0)), 0 0 0 4px var(--blue100); }

  /* ── Disabled: universal (Figma) — neutralTint10 / n80. */
  .btn:disabled, .btn.is-disabled { background-color: var(--neutralTint10); color: var(--n80); box-shadow: none; cursor: not-allowed; }
  .btn--link:disabled, .btn--link.is-disabled { background: none; text-decoration-color: transparent; }

  /* ── On a dark surface (.on-dark, color.css; 2026-10-09). Same levels,
     flipped: primary n0 with n150 text, secondary a 12% n0 wash with n0
     text, link label n0 with n35 icons. Neutral only for now. */
  .on-dark .btn--neutral {
    --btn-solid: var(--n0); --btn-solid-press: var(--n25); --btn-on-solid: var(--n150);
    --btn-tint: rgb(from var(--n0) r g b / 12%); --btn-tint-press: rgb(from var(--n0) r g b / 20%); --btn-on-tint: var(--n0);
    --btn-link: var(--n35); --btn-link-press: var(--n0);
  }
  .on-dark .btn--neutral.btn--secondary.is-hover { background-color: rgb(from var(--n0) r g b / 18%); }
  @media (hover: hover) { .on-dark .btn--neutral.btn--secondary:hover { background-color: rgb(from var(--n0) r g b / 18%); } }
  .on-dark .btn--neutral.btn--link { color: var(--n0); }
  .on-dark .btn--neutral.btn--link:active, .on-dark .btn--neutral.btn--link.is-pressed { color: var(--n35); }
  .on-dark .btn:disabled, .on-dark .btn.is-disabled { background-color: rgb(from var(--n0) r g b / 6%); color: var(--n90); }
  .on-dark .btn--link:disabled, .on-dark .btn--link.is-disabled { background: none; }

  /* ── Loading: the spinner takes the leading icon's place; the button
     keeps its size and ignores clicks. */
  .btn[aria-busy="true"] { pointer-events: none; }

  /* ── Dropdown launcher: the chevron turns while its menu is open (Radix
     sets data-state on the trigger). */
  .btn > .btn-icon .icon[data-icon="chevron-down"] { transition: rotate var(--dur-160) var(--ease-fade); }
  /* A forward arrow in the trailing (Icon R) slot nudges 2px on hover —
     animated, never a jump (2026-09-30). A parent with data-nudge (a card
     that is itself the link) triggers the same nudge on its own hover. */
  .btn > .btn-icon--trail .icon[data-icon="arrow-r"] { transition: translate var(--dur-240) var(--ease-out); }
  @media (hover: hover) { .btn:hover > .btn-icon--trail .icon[data-icon="arrow-r"],
  [data-nudge]:hover .btn > .btn-icon--trail .icon[data-icon="arrow-r"] { translate: 2px 0; } }
  .btn[data-state="open"] > .btn-icon .icon[data-icon="chevron-down"] { rotate: 180deg; }

  @media (prefers-reduced-motion: reduce) {
    .btn, .btn > .btn-icon .icon { transition: none; }
    .btn:active, .btn.is-pressed { scale: none; }
    .btn[data-pop] { animation: none; }
  }
}
