/*
 * Button: primary, plus secondary / tertiary / ghost / text variants and the
 * negative (danger) and inverse (dark-background) modifiers.
 *
 * Colours come from the semantic tokens. Hover/active are color-mixed off the
 * base colour, so recolouring a variant carries its states with it.
 */

.govbb-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--govbb-space-xs);
  padding: var(--govbb-space-s) var(--govbb-space-xm);
  font-family: inherit;
  font-size: var(--govbb-font-size-body);
  line-height: var(--govbb-line-height-base);
  color: var(--govbb-color-surface);
  background: var(--govbb-color-interactive);
  border: 0;
  border-radius: var(--govbb-radius);
  cursor: pointer;
  text-decoration: none; /* survives being rendered as an <a> */
  transition:
    background-color 150ms ease,
    box-shadow 150ms ease,
    color 150ms ease;

  /* pointer-events only blocks the mouse — an [aria-disabled] element is
     still focusable and Enter/Space still fires. Consumers using
     aria-disabled (e.g. on link-buttons) must gate the handler themselves. */
  &:is(:disabled, [aria-disabled='true']) {
    opacity: var(--govbb-opacity-disabled);
    cursor: not-allowed;
    pointer-events: none;
  }

  /* inset shadow = inner border, no layout shift, follows the radius */
  &:hover {
    background: color-mix(in oklab, var(--govbb-color-interactive), white 15%);
    box-shadow: inset 0 0 0 4px var(--govbb-overlay-light-10);
  }

  &:active {
    background: color-mix(in oklab, var(--govbb-color-interactive), black 18%);
    box-shadow: inset 0 0 0 3px var(--govbb-overlay-dark-20);
  }

  /* outline (not box-shadow) to avoid edge artifacts; offset leaves a gap */
  &:focus-visible {
    outline: 4px solid var(--govbb-color-focus);
    outline-offset: 2px;
  }
}

/* Secondary */
.govbb-button--secondary {
  color: var(--govbb-color-ink);
  background: var(--govbb-color-neutral);

  /* already light, so hover only adds the overlay */
  &:hover {
    background: var(--govbb-color-neutral);
    box-shadow: inset 0 0 0 4px var(--govbb-overlay-dark-5);
  }

  &:active {
    background: color-mix(in oklab, var(--govbb-color-neutral), black 18%);
    box-shadow: inset 0 0 0 3px var(--govbb-overlay-dark-10);
  }
}

/* Tertiary — outline via inset shadow, so its height matches the filled ones */
.govbb-button--tertiary {
  color: var(--govbb-color-tertiary);
  background: transparent;
  box-shadow: inset 0 0 0 2px var(--govbb-color-tertiary);

  &:hover {
    background: var(--govbb-green-10);
    box-shadow: inset 0 0 0 2px var(--govbb-color-tertiary);
  }

  &:active {
    color: var(--govbb-green-20);
    background: var(--govbb-color-tertiary);
    box-shadow:
      inset 0 0 0 2px var(--govbb-color-tertiary),
      inset 0 0 0 3px var(--govbb-overlay-dark-10);
  }

  /* ring meets the outline, no gap */
  &:focus-visible {
    outline-offset: 0;
  }
}

/* Ghost and text — a label, not a box. Ghost keeps the button's padding, so
   it lines up in a row of mixed buttons and keeps the full target; text drops
   it to sit flush where space is tight, such as a table cell or a list row. */
.govbb-button--ghost,
.govbb-button--text {
  color: var(--govbb-color-tertiary);
  background: transparent;
  text-decoration: underline;
  text-underline-offset: 0.1em;
  text-decoration-thickness: max(1px, 0.0625em);

  &:is(:hover, :active) {
    color: var(--govbb-color-tertiary);
    background: transparent;
    box-shadow: none;
  }
}

.govbb-button--text {
  padding: 0;
}

/* forced-colors drops box-shadow, so the inset outline needs a real border */
@media (forced-colors: active) {
  .govbb-button--tertiary {
    border: 2px solid ButtonBorder;
  }
}

/* Negative (danger) — only primary, ghost and text use it */
.govbb-button--negative {
  color: var(--govbb-color-surface);
  background: var(--govbb-color-error);

  &:hover {
    background: color-mix(in oklab, var(--govbb-color-error), white 15%);
    box-shadow: inset 0 0 0 4px var(--govbb-overlay-light-10);
  }

  &:active {
    background: color-mix(in oklab, var(--govbb-color-error), black 18%);
    box-shadow: inset 0 0 0 3px var(--govbb-overlay-dark-20);
  }

  &:focus-visible {
    outline-color: var(--govbb-color-focus-danger);
  }

  &:is(.govbb-button--ghost, .govbb-button--text) {
    color: var(--govbb-color-error);
    background: transparent;

    &:is(:hover, :active) {
      color: var(--govbb-color-error);
      background: transparent;
      box-shadow: none;
    }
  }
}

/* Button group — lays out related actions (buttons, or buttons alongside a
   text link) in a row that wraps, 24px apart as in the pattern library's
   Button group. The --vertical modifier stacks them and stretches each to the
   group's width, for narrow or full-width action stacks. */
.govbb-button-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--govbb-space-xm);
}

.govbb-button-group--vertical {
  flex-direction: column;
  align-items: stretch;
}

/* Inverse — for dark backgrounds */
.govbb-button--inverse {
  color: var(--govbb-color-brand);
  background: var(--govbb-color-surface);

  &:hover {
    background: var(--govbb-color-surface);
    box-shadow: inset 0 0 0 4px var(--govbb-overlay-dark-5);
  }

  &:active {
    background: color-mix(in oklab, var(--govbb-color-surface), black 10%);
    box-shadow: inset 0 0 0 3px var(--govbb-overlay-dark-10);
  }

  &.govbb-button--tertiary {
    color: var(--govbb-color-surface);
    background: transparent;
    box-shadow: inset 0 0 0 2px var(--govbb-color-surface);

    &:hover,
    &:active {
      color: var(--govbb-color-surface);
      background: var(--govbb-overlay-light-10);
      box-shadow: inset 0 0 0 2px var(--govbb-color-surface);
    }
  }

  &:is(.govbb-button--ghost, .govbb-button--text) {
    color: var(--govbb-color-surface);
    background: transparent;
  }
}
