/* One ownership boundary: isolated class or an opted-in host. Quiet M defaults. */
/* Shipped in a cascade layer: unlayered host CSS wins without !important. */
@layer ds.tokens, ds.base, ds.components;
@layer ds.components {
/* Menu items are buttons with menu roles and their own look (components/menu), as are the data table's sort
   headers and scroll steps (components/data-table). */
:where(.ds-button, [data-ds-ui] button:not([role="menuitem"], [role="menuitemcheckbox"], [role="menuitemradio"], .ds-data-table-sort, .ds-data-table-step)) {
  --_size: var(--ds-control-size-m);
  --_font: var(--ds-type-button-size);
  --_inset: var(--ds-space-button-inset);
  --_icon: var(--ds-icon-size-m);
  --_gap: var(--ds-space-2);
  --_reveal-color: var(--ds-color-text);
  box-sizing: border-box;
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--_gap);
  min-block-size: var(--_size);
  min-inline-size: var(--_size);
  max-inline-size: 100%;
  padding: max(0px, calc((var(--_size) - var(--_font) * 1.4) / 2 - var(--ds-control-border-width))) var(--_inset);
  border: var(--ds-control-border-width) solid var(--ds-color-border);
  border-radius: var(--ds-radius-button);
  background: var(--ds-color-button-fill);
  color: var(--ds-color-text);
  font: var(--ds-type-control-weight) var(--_font)/1.4 var(--ds-font);
  text-align: center;
  text-decoration: none;
  white-space: normal;
  overflow-wrap: anywhere;
  cursor: pointer;
  touch-action: manipulation;

  &[data-size="2xs"] { --_size: var(--ds-control-size-2xs); --_font: var(--ds-type-control-xs); --_inset: var(--ds-space-2); --_icon: var(--ds-icon-size-s); --_gap: var(--ds-space-1); }
  &[data-size="xs"] { --_size: var(--ds-control-size-xs); --_font: var(--ds-type-control-xs); --_inset: var(--ds-space-2); --_icon: var(--ds-icon-size-s); --_gap: var(--ds-control-gap-small); }
  &[data-size="s"] { --_size: var(--ds-control-size-s); --_font: var(--ds-type-button-small); --_inset: var(--ds-space-3); --_icon: var(--ds-icon-size-s); --_gap: var(--ds-control-gap-small); }
  &[data-size="l"] { --_size: var(--ds-control-size-l); --_font: var(--ds-type-control-large); --_inset: var(--ds-space-5); --_icon: var(--ds-icon-size-l); }
  & > :is(svg, [data-ds-button-spinner]) { inline-size: var(--_icon); block-size: var(--_icon); flex: none; pointer-events: none; }
  & > [data-ds-button-label] { min-inline-size: 0; }
  & > [data-ds-button-spinner] {
    display: none;
    box-sizing: border-box;
    border: var(--ds-spinner-stroke) solid currentColor;
    border-inline-end-color: transparent;
    border-radius: 50%;
  }
  /* Tooltips. An icon-only button's label is its bubble: it stays in the DOM as the name. A labelled
     button with a shortcut gets a bubble of its keys. */
  &:is([data-icon-only="true"], [data-ds-shortcut]) {
    position: relative;
    /* Clear of the focus ring, so both stay readable together. */
    --_tooltip-gap: calc(var(--ds-focus-offset) + var(--ds-focus-width) + var(--ds-space-1));
    /* The delay, or none while the surrounding group is warm (below). The fallback keeps the delay in a
       browser without @property. */
    --_tooltip-wait: calc(var(--ds-tooltip-delay) * (1 - var(--_ds-tooltip-warm, 0)));
  }
  &[data-icon-only="true"] { padding: 0; }
  &[data-icon-only="true"] > [data-ds-button-label],
  &:not([data-icon-only="true"]) > [data-ds-button-label] > [data-ds-keys] {
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
    /* Opacity keeps the name in the accessibility tree while the tooltip waits. */
    opacity: 0;
    pointer-events: none;
  }
  &:is([data-icon-only="true"], [data-ds-shortcut]):is(:hover, :focus-visible) { z-index: 1; }
  /* Laid out at once, made visible after --ds-tooltip-delay (a quarter for keyboard focus). An open menu
     replaces its trigger's tooltip, so the menu takes the next Escape; :where keeps the specificity.
     A labelled button's keys wait until a script has said which keyboard is in use (data-ds-platform). */
  &:is(:hover, :focus-visible):not([data-ds-tooltip], :where([aria-haspopup="menu"][aria-expanded="true"])) {
    &[data-icon-only="true"] > [data-ds-button-label],
    &:not([data-icon-only="true"]):is([data-ds-platform], [data-ds-platform] *) > [data-ds-button-label] > [data-ds-keys] {
      inset-block-end: calc(100% + var(--_tooltip-gap) + var(--ds-control-border-width));
      /* Physical centring is direction-neutral. enhanceButton shifts it inside the viewport. */
      left: 50%;
      translate: calc(-50% + var(--_tooltip-shift, 0px)) 0;
      inline-size: max-content;
      /* A readable line length, not a token: 16rem fits short names on one line. */
      max-inline-size: min(16rem, calc(100vw - 2 * var(--ds-space-3)));
      block-size: auto;
      overflow: visible;
      clip-path: none;
      white-space: normal;
      padding: var(--ds-space-1) var(--ds-space-2);
      /* Transparent until forced colours paint it as the tooltip edge. */
      border: var(--ds-control-border-width) solid transparent;
      border-radius: var(--ds-radius-control);
      background: var(--ds-color-text);
      color: var(--ds-color-canvas);
      /* Regular weight sets the name apart from semibold control labels. */
      font: 400 var(--ds-type-control-small)/1.4 var(--ds-font);
      opacity: 1;
      pointer-events: auto;
      /* Half the state timing: a name that arrives, not one that keeps the reader waiting. */
      transition: opacity calc(var(--ds-motion-duration-state) / 2) ease var(--_tooltip-wait), pointer-events 0s var(--_tooltip-wait);
      transition-behavior: allow-discrete;
    }
    /* Bridges the gap so the pointer can move onto the tooltip (WCAG 1.4.13 hoverable). */
    &[data-icon-only="true"] > [data-ds-button-label]::after,
    &:not([data-icon-only="true"]) > [data-ds-button-label] > [data-ds-keys]::after {
      content: "";
      position: absolute;
      inset-inline: 0;
      inset-block-start: 100%;
      block-size: calc(var(--_tooltip-gap) + 2 * var(--ds-control-border-width));
    }
    /* Flipped below when there is no room above (set by enhanceButton). */
    &[data-ds-tooltip-side="below"][data-icon-only="true"] > [data-ds-button-label],
    &[data-ds-tooltip-side="below"]:not([data-icon-only="true"]) > [data-ds-button-label] > [data-ds-keys] {
      inset-block-end: auto;
      inset-block-start: calc(100% + var(--_tooltip-gap) + var(--ds-control-border-width));
      &::after { inset-block-start: auto; inset-block-end: 100%; }
    }
  }
  &[data-icon-only="true"]:focus-visible:not([data-ds-tooltip]) > [data-ds-button-label],
  &:not([data-icon-only="true"])[data-ds-shortcut]:focus-visible:not([data-ds-tooltip]) > [data-ds-button-label] > [data-ds-keys] {
    transition-delay: calc(var(--_tooltip-wait) / 4);
  }
  /* In an icon-only bubble the keys follow the name, a step quieter (still text contrast; full in higher
     contrast). */
  &[data-icon-only="true"] > [data-ds-button-label] > [data-ds-keys]:where([data-ds-platform] *) {
    margin-inline-start: var(--ds-space-2);
    color: color-mix(in srgb, var(--ds-color-canvas) calc(72% + var(--ds-contrast-amount) * .28), var(--ds-color-text));
  }
  /* Touch has no hover: a tap must not leave a bubble behind. Keyboard focus still shows it. */
  @media (hover: none) {
    &[data-icon-only="true"]:hover:not(:focus-visible) > [data-ds-button-label],
    &:not([data-icon-only="true"])[data-ds-shortcut]:hover:not(:focus-visible) > [data-ds-button-label] > [data-ds-keys] { opacity: 0; pointer-events: none; transition: none; }
  }
  &[aria-busy="true"] { cursor: progress; }
  /* A quick answer shows no spinner: the swap waits a beat (PowerLocal's .15s). Time, not motion, so reduced motion keeps it. */
  &[aria-busy="true"] > [data-ds-button-spinner] { display: block; animation: ds-button-busy-wait .15s step-end; }
  /* The glyph holds the place until then, or the button would narrow for the beat. It goes by an animation's end: an
     element already at display: none runs no animation. */
  &[aria-busy="true"]:has(> [data-ds-button-spinner="leading"]) > [data-ds-button-icon="leading"],
  &[aria-busy="true"]:has(> [data-ds-button-spinner="trailing"]) > [data-ds-button-icon="trailing"] { animation: ds-button-busy-swap 1ms .15s forwards; }
  &[data-variant="primary"] {
    --_reveal-color: var(--ds-color-reveal);
    background: var(--ds-color-primary);
    border-color: var(--ds-color-primary);
    color: var(--ds-color-on-primary);
  }
  /* Soft: a neutral fill with no edge, between the filled main action and the outlined default (PowerLocal's
     primary emphasis; our primary is its cta). The words carry the contrast; higher contrast adds the edge. */
  &[data-variant="soft"] { background: var(--ds-color-soft); border-color: color-mix(in srgb, var(--ds-color-border) var(--ds-contrast-amount), transparent); }
  &[data-variant="tertiary"] { background: transparent; border-color: transparent; }
  /* A destructive act (Delete, Discard; PowerLocal's cta with intent danger): the danger colour fills a
     primary and colours a secondary's or tertiary's words. The word says what it destroys; the colour
     repeats it. */
  &[data-tone="danger"]:not([data-variant="primary"]) { color: var(--ds-color-danger); }
  &[data-tone="danger"][data-variant="primary"] {
    --_reveal-color: color-mix(in srgb, var(--ds-color-danger-fill) 70%, var(--ds-color-surface));
    background: var(--ds-color-danger-fill);
    border-color: var(--ds-color-danger-fill);
    color: var(--ds-color-on-danger);
  }
  &[aria-expanded="true"]:not([data-variant="primary"]) { background: var(--ds-color-neutral-pressed); border-color: var(--ds-color-text); }
  &[aria-expanded="true"][data-variant="primary"] { background: var(--ds-color-action-pressed); }
  @media (hover: hover) {
    &:enabled:not([aria-disabled="true"]):hover { background: var(--ds-color-neutral-hover); }
    &[data-variant="soft"]:enabled:not([aria-disabled="true"]):hover { background: color-mix(in srgb, var(--ds-color-soft), var(--ds-color-text) 6%); }
    &[data-variant="tertiary"]:enabled:not([aria-disabled="true"]):hover { background: var(--ds-color-ghost-hover); }
    &[data-variant="primary"]:enabled:not([aria-disabled="true"], [aria-expanded="true"]):hover { background: var(--ds-color-action-hover); }
    &[data-tone="danger"][data-variant="primary"]:enabled:not([aria-disabled="true"], [aria-expanded="true"]):hover { background: color-mix(in srgb, var(--ds-color-danger-fill) 86%, var(--ds-color-text)); }
    /* An open trigger stays pressed under the pointer, whatever its variant: the hovers above would
       otherwise win (a primary arrow went light grey under its white chevron). */
    &[aria-expanded="true"]:not([data-variant="primary"]):enabled:not([aria-disabled="true"]):hover { background: var(--ds-color-neutral-pressed); }
    &[aria-expanded="true"][data-variant="primary"]:enabled:not([aria-disabled="true"]):hover { background: var(--ds-color-action-pressed); }
  }
  &:enabled:not([aria-disabled="true"]):active { background: var(--ds-color-neutral-pressed); }
  &[data-variant="soft"]:enabled:not([aria-disabled="true"]):active { background: color-mix(in srgb, var(--ds-color-soft), var(--ds-color-text) 12%); }
  &[data-variant="tertiary"]:enabled:not([aria-disabled="true"]):active { background: var(--ds-color-ghost-pressed); }
  &[data-variant="primary"]:enabled:not([aria-disabled="true"]):active { background: var(--ds-color-action-pressed); }
  &[data-tone="danger"][data-variant="primary"]:enabled:not([aria-disabled="true"]):active { background: color-mix(in srgb, var(--ds-color-danger-fill) 74%, var(--ds-color-text)); }
  /* Pressed (a toggle that is on): the accent's tint pair, a state rather than an act (visp-sibe's). An
     unavailable toggle never looks on. */
  &[aria-pressed="true"]:enabled:not([aria-disabled="true"]) { background: var(--ds-color-state-tint); border-color: var(--ds-color-state-text); color: var(--ds-color-state-text); }
  @media (hover: hover) { &[aria-pressed="true"]:enabled:not([aria-disabled="true"]):hover { background: color-mix(in srgb, var(--ds-color-state) 18%, var(--ds-color-surface)); } }
  &:focus-visible {
    outline: var(--ds-focus-width) solid var(--ds-color-focus);
    outline-offset: var(--ds-focus-offset);
  }
  &:is(:disabled, [aria-disabled="true"]):not([aria-busy="true"]) {
    background: var(--ds-color-disabled-surface);
    border-color: var(--ds-color-border);
    color: var(--ds-color-disabled-text);
    border-style: dashed;
    cursor: not-allowed;
  }
  &[aria-disabled="true"]:not(:disabled):not([aria-busy="true"]) { cursor: help; }
  /* A pointer-following light, adapted from PowerMail. No wrapper or label mutation. */
  &[data-reveal="true"] { position: relative; isolation: isolate; }
  &[data-reveal="true"]::after {
    content: "";
    position: absolute;
    inset: 0;
    z-index: -1;
    border-radius: inherit;
    pointer-events: none;
    background: radial-gradient(circle var(--ds-reveal-radius) at var(--ds-reveal-x, 50%) var(--ds-reveal-y, 50%), color-mix(in srgb, var(--_reveal-color) var(--ds-reveal-strength), transparent), transparent 70%);
    opacity: 0;
  }
  @media (hover: hover) and (pointer: fine) and (prefers-reduced-motion: no-preference) and (forced-colors: none) {
    &[data-reveal="true"][data-reveal-active="true"]:enabled:not([aria-disabled="true"]):hover::after { opacity: var(--ds-reveal-allowed); }
  }
  @media (prefers-reduced-motion: no-preference) {
    &[aria-busy="true"] > [data-ds-button-spinner] { animation: ds-button-busy-wait .15s step-end, ds-button-spin var(--ds-motion-duration-spin) linear infinite; }
    & {
      transition: background-color var(--ds-motion-duration-state) var(--ds-motion-ease-standard),
        border-color var(--ds-motion-duration-state) var(--ds-motion-ease-standard),
        color var(--ds-motion-duration-state) var(--ds-motion-ease-standard);
    }
    &[data-reveal="true"]::after { transition: opacity var(--ds-motion-duration-state) var(--ds-motion-ease-standard); }
  }
  @media (forced-colors: active) {
    & > [data-ds-button-spinner] { forced-color-adjust: none; }
    & { background: ButtonFace; color: ButtonText; border-color: ButtonText; }
    &:is([data-variant="primary"], [data-variant="soft"], [data-variant="tertiary"]) { background: ButtonFace; color: ButtonText; border-color: ButtonText; }
    &:enabled:not([aria-disabled="true"]):is(:hover, :active),
    &:is([data-variant="primary"], [data-variant="soft"], [data-variant="tertiary"]):enabled:not([aria-disabled="true"]):is(:hover, :active) { background: Highlight; color: HighlightText; border-color: Highlight; forced-color-adjust: none; }
    /* Without that, Chrome paints a Canvas backplate behind the label: white text on white. The tooltip
       keeps the system's own colours. */
    &[data-icon-only="true"] > [data-ds-button-label],
    &:not([data-icon-only="true"]) > [data-ds-button-label] > [data-ds-keys] { forced-color-adjust: auto; }
    &:focus-visible { outline-color: Highlight; }
    &[aria-expanded="true"] { border-color: Highlight; }
    &[aria-pressed="true"]:enabled:not([aria-disabled="true"]) { background: Highlight; color: HighlightText; border-color: Highlight; forced-color-adjust: none; }
    &:is(:disabled, [aria-disabled="true"]):not([aria-busy="true"]) { color: GrayText; border-color: GrayText; background: ButtonFace; }
  }
}

/* Shortcut keys, written by CSS from the PHP-rendered words once a script has said which keyboard is in
   use (data-ds-platform on the element or an ancestor; enhanceButton and enhanceMenuButton set it). The
   span is aria-hidden: aria-keyshortcuts carries the keys to assistive technology. Keys never break
   across lines and read left to right in either direction (Ctrl+↑ stays Ctrl+↑); the span itself keeps
   the page's direction, so its logical margins fall on the right side. */
:where([data-ds-platform]) [data-ds-keys]::before {
  content: attr(data-ds-keys);
  white-space: nowrap;
  direction: ltr;
  unicode-bidi: isolate;
  font-variant-numeric: tabular-nums;
}
:where([data-ds-platform="apple"]) [data-ds-keys]::before { content: attr(data-ds-keys-apple); }

/* Warm window: once a bubble has shown in a button group or toolbar, the next one there shows at once,
   also when the pointer moves straight across (visp-sibe's opens only after a hide). The group cools 350ms
   (visp-sibe's WARM_MS) after its last bubble trigger is left. A registered number switches after a
   transition delay, so no script keeps time. A toolbar's own transition replaces this one. */
@property --_ds-tooltip-warm { syntax: "<number>"; inherits: true; initial-value: 0; }
:where(.ds-button-group:not([role="toolbar"] *), [role="toolbar"]):where(:has(:is([data-icon-only="true"], [data-ds-shortcut]))) {
  --_ds-tooltip-warm: 0;
  transition: --_ds-tooltip-warm 0s 350ms;
}
:where(.ds-button-group:not([role="toolbar"] *), [role="toolbar"]):where(:has(:is([data-icon-only="true"], [data-ds-shortcut]):hover)) {
  --_ds-tooltip-warm: 1;
  transition-delay: var(--ds-tooltip-delay);
}
:where(.ds-button-group:not([role="toolbar"] *), [role="toolbar"]):where(:has(:is([data-icon-only="true"], [data-ds-shortcut]):focus-visible)) {
  --_ds-tooltip-warm: 1;
  transition-delay: calc(var(--ds-tooltip-delay) / 4);
}

/* One bubble at a time is enhanceButton's (data-ds-tooltip): a bubble under the pointer replaces a
   keyboard-focus bubble elsewhere until the pointer leaves, and a keyboard focus that comes later wins.
   It was a :root:has(:hover) rule here: every pointer that entered or left a tooltip button restyled
   each button label on the page (3 900 elements, 18 to 25 ms on the 1 000-button stress page). */
@keyframes ds-button-spin { to { transform: rotate(1turn); } }
@keyframes ds-button-busy-wait { from, to { display: none; } }
@keyframes ds-button-busy-swap { to { display: none; } }
/* The copy button's status (copy-button.js): read, never seen; the button's own words show the same. */
:where(.ds-button-status) { position: absolute; inline-size: 1px; block-size: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
}
