/**
 * Button primitive (r-39 step c1).
 *
 * Five roles only (ui-design-system.md §13 "Buttons"):
 *   .btn-primary   the main action of a region — one per region
 *   .btn-secondary a normal, bordered action (Cancel, Refresh, a choice)
 *   .btn-ghost     a borderless, low-emphasis action (links, chat-input icons)
 *   .btn-icon      icon-only; always carries title + aria-label
 *   .btn-danger    destructive tone; outlined on its own, solid red when
 *                  combined with .btn-primary (a delete confirmation)
 * Size modifiers: .btn-sm (the smaller "+ Add Item" icon buttons in form rows)
 * and .btn-inline (r-61): a ghost button sized to the line of text it sits
 * in, link-style, e.g. the email header's "Add as contact…".
 * Icon variants (r-39 f3): .btn-step (quantity stepper ▲/▼) and .btn-nav
 * (the date-nav arrows on the page header), both with .btn-icon.
 *
 * Positioning (e.g. .modal-close-btn's absolute corner) stays with the
 * organism in ui_design/css/app/; this file only owns how a button looks.
 *
 * @see docs/architecture/ui-design-system.md §13
 * @see docs/architecture/module-map/buttons.css.md
 */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  /* §9: --space-2 padding + --font-size-sm at --control-line-height + the
     border = --control-height, the shared header-row height. */
  padding: var(--space-2) var(--space-5);
  border: var(--control-border-width) solid transparent;
  border-radius: var(--radius-lg);
  background: transparent;
  color: var(--color-text-primary);
  font-family: inherit;
  font-size: var(--font-size-sm);
  line-height: var(--control-line-height);
  font-weight: var(--font-weight-medium);
  white-space: nowrap;
  cursor: pointer;
  transition: color 0.2s, background 0.2s, border-color 0.2s, opacity 0.2s;
}

/* Tailwind's .hidden is injected at runtime and would otherwise lose to
   .btn's display on source order alone. */
.btn.hidden,
.btn[hidden] {
  display: none;
}

.btn:focus-visible {
  outline: var(--focus-ring-width) solid var(--focus-ring-color);
  outline-offset: 2px;
}

.btn:disabled,
.btn[aria-disabled="true"] {
  opacity: var(--opacity-disabled);
  cursor: not-allowed;
}

/* ── Primary ─────────────────────────────────────────────────────────── */
.btn-primary {
  background: var(--color-action-primary);
  color: var(--color-action-primary-text);
  font-weight: var(--font-weight-bold);
}
.btn-primary:not(:disabled):hover {
  opacity: 0.85;
}

/* Named context rule: inside the teal page header a solid accent fill has
   no visible edge in light mode, so the primary switches to the --color-cta-*
   tokens (white fill, accent text; solid accent in dark mode). §8. */
.section-header .btn-primary {
  background: var(--color-cta-bg);
  color: var(--color-cta-text);
}
.section-header .btn-primary:not(:disabled):hover {
  opacity: 1;
  background: var(--color-cta-bg-hover);
}

/* ── Secondary ───────────────────────────────────────────────────────── */
.btn-secondary {
  background: var(--color-bg-surface);
  border-color: var(--color-border);
  color: var(--color-text-secondary);
}
.btn-secondary:not(:disabled):hover {
  color: var(--color-accent);
  border-color: var(--color-accent);
  background: var(--color-accent-tint-2);
}

/* Named context rule (b-113): on the page header the hover's translucent
   accent tint shows the header's own colour through it, so accent text
   vanished ("Clear all filters", crimson on crimson in Red). There it
   darkens instead and keeps the header's white text. */
.section-header .btn-secondary:not(:disabled):hover {
  color: var(--color-on-header);
  border-color: color-mix(in srgb, var(--color-on-header) 55%, transparent);
  background: var(--color-on-header-sunken);
}

/* Named context rule (r-150 review, N1): an icon-only button on the page
   header (Database and Dashboard "Clear all filters") is transparent, so
   its card text colour was a dark icon on the header's dark end in light
   mode, and its accent hover the header's own hue (b-113 again). On the
   header it takes the header's text colour, and its hover darkens like the
   secondary's. check-contrast measures both. */
.section-header .btn-icon {
  color: var(--color-on-header);
  border-color: color-mix(in srgb, var(--color-on-header) 55%, transparent);
}
.section-header .btn-icon:not(:disabled):hover {
  color: var(--color-on-header);
  border-color: color-mix(in srgb, var(--color-on-header) 55%, transparent);
  background: var(--color-on-header-sunken);
}

/* ── Ghost ───────────────────────────────────────────────────────────── */
.btn-ghost {
  color: var(--color-accent);
}
.btn-ghost:not(:disabled):hover {
  background: var(--color-accent-tint-2);
}

/* Inline (r-61): a .btn-ghost that reads as a link inside a line of text —
   no padding or box, the text's own size, underlined on hover. Peter,
   2026-10-01: "small link style" for the email header's contact actions. */
.btn-inline {
  padding: 0;
  border: 0;
  font-size: inherit;
  line-height: inherit;
  font-weight: var(--font-weight-medium);
}
.btn-inline.btn-ghost:not(:disabled):hover {
  background: transparent;
  text-decoration: underline;
}

/* ── Danger (tone) ───────────────────────────────────────────────────── */
.btn-danger {
  background: transparent;
  border-color: var(--color-danger-border);
  color: var(--color-danger);
}
.btn-danger:not(:disabled):hover {
  background: var(--color-danger-tint);
  border-color: var(--color-danger);
}

/* A destructive confirmation: the one click that can't be undone looks
   unlike every safe primary (§7). */
.btn-primary.btn-danger {
  background: var(--color-action-danger);
  border-color: var(--color-action-danger);
  color: var(--color-action-danger-text);
}
.btn-primary.btn-danger:not(:disabled):hover {
  opacity: 1;
  background: var(--color-action-danger-hover);
  border-color: var(--color-action-danger-hover);
}

/* ── Icon-only ───────────────────────────────────────────────────────── */
.btn-icon {
  width: 2rem;
  height: 2rem;
  flex-shrink: 0; /* a crowded flex row must squeeze its fields, never the icon */
  padding: 0;
  border-color: var(--color-border);
  /* Primary text colour, so an enabled icon reads clearly against a
     disabled one (--opacity-disabled) right next to it (Peter, 2026-09-28). */
  color: var(--color-text-primary);
  font-size: var(--icon-sm);
}
.btn-icon:not(:disabled):hover,
.btn-icon.active {
  color: var(--color-accent);
  border-color: var(--color-accent);
  background: var(--color-accent-tint-3);
}

/* Icon + ghost: borderless (chat-input icons, the item-row remove ✕). Keeps
   .btn-icon's primary colour at rest (it comes later in this file than
   .btn-ghost) and turns accent on hover. */
.btn-icon.btn-ghost {
  border-color: transparent;
}
.btn-icon.btn-ghost:not(:disabled):hover {
  border-color: transparent;
  background: var(--color-accent-tint-2);
}

.btn-sm.btn-icon {
  width: 1.7rem;
  height: 1.7rem;
}

/* RULE: §9 Header-row control sizing (Peter, 2026-11-02: the clear-filters
   icon was "smaller than the other buttons"). In a header row or toolbar an
   icon-only button is a --control-height square, the height .btn and the
   field primitives already have; elsewhere it stays the 2rem square. The
   small, stepper and date-nav variants keep their own size. */
.toolbar .btn-icon:not(.btn-sm, .btn-step, .btn-nav),
.section-header .btn-icon:not(.btn-sm, .btn-step, .btn-nav) {
  width: var(--control-height);
  height: var(--control-height);
}

/* ── Icon variants (r-39 f3) ─────────────────────────────────────────── */
/* Stepper half-button: the ▲/▼ pair stacked beside a quantity field
   (.qty-stepper, modal-content.css). Square and borderless: the pair's
   wrapper draws the border that joins it to the field. */
.btn-icon.btn-step {
  flex: 1;
  width: 1.3rem;
  height: auto;
  border: none;
  border-radius: 0;
  background: var(--color-bg-field); /* sits inside the field it steps (r-152 review F2) */
  font-size: var(--font-size-xs);
  line-height: 1;
}
.btn-icon.btn-step:not(:disabled):hover {
  background: var(--color-accent-tint-3);
}
.btn-step + .btn-step {
  border-top: 1px solid var(--color-border);
}
/* The shared ring sits outside the button, where the wrapper's
   overflow:hidden and the other half-button clip it (▲ showed nothing, ▼ a
   thin line). Drawn inside the button instead, each half shows a full ring. */
.btn-icon.btn-step:focus-visible {
  outline-offset: calc(-1 * var(--focus-ring-width));
}

/* Date-nav arrow: borderless, on the teal page header (--color-on-header-*). */
.btn-icon.btn-nav {
  width: 1.6rem;
  height: 1.6rem;
  border-color: transparent;
  color: var(--color-on-header-muted);
  font-size: var(--font-size-xs);
}
.btn-icon.btn-nav:not(:disabled):hover {
  border-color: transparent;
  background: transparent;
  color: var(--color-on-header);
}
.btn-icon.btn-nav:disabled {
  opacity: 1;
  color: var(--color-on-header-faint);
}

/* ── Locked for a missing right (r-77, ui-design-system.md §19) ────────
   Any control the signed-in user has no right for (components/permission-gate.js);
   a toggle is locked on its hidden input, so its visible label shows it:
   it keeps its place and looks unavailable, and its reason shows in a
   small pop-up on hover, focus or tap (a manual popover, so it sits above
   an open dialog too, the panel look of .field-lock-reason). */
[data-permission-locked],
.toggle:has(> .toggle-input[data-permission-locked]) {
  opacity: var(--opacity-disabled);
  cursor: not-allowed;
}
.permission-reason {
  position: fixed;
  inset: auto;
  margin: 0;
  width: max-content;
  max-width: 16rem;
  pointer-events: none;
  background: var(--color-bg-surface);
  border: 1px solid var(--color-divider);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-2);
  padding: var(--space-2);
  color: var(--color-text-primary);
}
