/* ==========================================================================
   Button
   Colors:    .btn-<color> (btn-green, btn-red …) — plain .btn is neutral gray
   Strengths: .btn-subtle · .btn-soft · (default) · .btn-solid · .btn-vivid · .btn-deep · .btn-outline · .btn-ghost · .btn-link
   Sizes:     .btn-xs · .btn-sm · .btn-lg      Shape: .btn-icon (square)
   States:    :disabled / .disabled · .active · aria-busy="true" (loading)

   <button class="btn btn-red btn-vivid"> — a short <button class="btn-red"> works too.
   ========================================================================== */

:is(/*@btn*/) {
  --mo-btn-height: var(--mo-control-height);
  --mo-btn-padding-x: .875rem;
  --mo-btn-font-size: .875rem;
  --mo-btn-font-weight: var(--mo-font-weight-medium);
  --mo-btn-radius: var(--mo-radius-control);
  --mo-btn-gap: .5rem;
  /* Neutral defaults — variants below override these */
  --mo-btn-color: var(--mo-fg-body);
  --mo-btn-bg: var(--mo-bg-3);
  --mo-btn-border: transparent;
  --mo-btn-hover-color: var(--mo-btn-color);
  --mo-btn-hover-bg: color-mix(in oklab, var(--mo-bg-3), var(--mo-fg-body) 7%);
  --mo-btn-hover-border: var(--mo-btn-border);
  --mo-btn-active-bg: color-mix(in oklab, var(--mo-bg-3), var(--mo-fg-body) 12%);

  position: relative;
  display: inline-flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  gap: var(--mo-btn-gap);
  min-height: var(--mo-btn-height);
  padding: 0 var(--mo-btn-padding-x);
  font-family: inherit;
  font-size: var(--mo-btn-font-size);
  font-weight: var(--mo-btn-font-weight);
  line-height: 1.25;
  color: var(--mo-btn-color);
  text-align: center;
  text-decoration: none;
  white-space: nowrap;
  vertical-align: middle;
  cursor: pointer;
  user-select: none;
  background-color: var(--mo-btn-bg);
  border: var(--mo-border-width) solid var(--mo-btn-border);
  border-radius: var(--mo-btn-radius);
  -webkit-tap-highlight-color: transparent;
}
@media (prefers-reduced-motion: no-preference) {
  :is(/*@btn*/) {
    transition: color var(--mo-duration), background-color var(--mo-duration), border-color var(--mo-duration);
  }
}

:is(/*@btn*/):hover {
  color: var(--mo-btn-hover-color);
  background-color: var(--mo-btn-hover-bg);
  border-color: var(--mo-btn-hover-border);
}
:is(/*@btn*/):is(:active, .active, [aria-pressed="true"]) {
  color: var(--mo-btn-hover-color);
  background-color: var(--mo-btn-active-bg);
  border-color: var(--mo-btn-hover-border);
}
:is(/*@btn*/):focus-visible {
  outline: var(--mo-focus-ring-width) solid var(--mo-theme-focus-ring);
  outline-offset: 1px;
}
:is(/*@btn*/):is(:disabled, .disabled, [aria-disabled="true"]),
fieldset:disabled :is(/*@btn*/) {
  pointer-events: none;
  opacity: .5;
}

/* Icons inside buttons scale with the text — SVG or icon font (.mi) */
:is(/*@btn*/) > svg {
  flex-shrink: 0;
  width: 1.125em;
  height: 1.125em;
}
:is(/*@btn*/) > .mi {
  flex-shrink: 0;
  font-size: 1.125em;
  vertical-align: 0;
}

/* Colors & strengths ------------------------------------------------------------
   <button class="btn btn-green">               the color alone: the soft default fill (*-300)
   <button class="btn btn-green btn-vivid">     + a strength
   Strengths, light to strong:  .btn-subtle · .btn-soft · (default) · .btn-solid · .btn-vivid · .btn-deep
   Without a fill:              .btn-outline · .btn-ghost · .btn-link
   Every fill hovers one shade further and presses two, so all strengths feel alike.
   A strength without a color uses the default color; a plain .btn stays neutral gray. */

/* Just a color — .btn-green, or .theme-green on the button itself */
:is(/*@colors btn*/), .btn:where(/*@themes*/) {
  --mo-btn-color: var(--mo-theme-fill-text);
  --mo-btn-bg: var(--mo-theme-fill);
  --mo-btn-hover-bg: var(--mo-theme-fill-hover);
  --mo-btn-active-bg: var(--mo-theme-fill-active);
}
.btn-subtle { --mo-btn-color: var(--mo-theme-subtle-text); --mo-btn-bg: var(--mo-theme-subtle); --mo-btn-hover-bg: var(--mo-theme-subtle-hover); --mo-btn-active-bg: var(--mo-theme-subtle-active); }
.btn-soft   { --mo-btn-color: var(--mo-theme-soft-text);   --mo-btn-bg: var(--mo-theme-soft);   --mo-btn-hover-bg: var(--mo-theme-soft-hover);   --mo-btn-active-bg: var(--mo-theme-soft-active); }
.btn-solid  { --mo-btn-color: var(--mo-theme-solid-text);  --mo-btn-bg: var(--mo-theme-solid);  --mo-btn-hover-bg: var(--mo-theme-solid-hover);  --mo-btn-active-bg: var(--mo-theme-solid-active); }
.btn-vivid  { --mo-btn-color: var(--mo-theme-vivid-text);  --mo-btn-bg: var(--mo-theme-vivid);  --mo-btn-hover-bg: var(--mo-theme-vivid-hover);  --mo-btn-active-bg: var(--mo-theme-vivid-active); }
.btn-deep   { --mo-btn-color: var(--mo-theme-deep-text);   --mo-btn-bg: var(--mo-theme-deep);   --mo-btn-hover-bg: var(--mo-theme-deep-hover);   --mo-btn-active-bg: var(--mo-theme-deep-active); }

/* Outline — the one variant with a border (by design). Hover: a see-through tint and a
   stronger border; text stays the color. Outline & ghost tints are see-through, so they
   read on ANY background — white cards, the gray page, a colored section. */
.btn-outline {
  --mo-btn-color: var(--mo-theme-fg);
  --mo-btn-bg: transparent;
  --mo-btn-border: color-mix(in oklab, var(--mo-theme-fg) 45%, transparent);
  --mo-btn-hover-color: var(--mo-theme-fg-emphasis);
  --mo-btn-hover-bg: color-mix(in oklab, var(--mo-theme-base) 9%, transparent);
  --mo-btn-hover-border: var(--mo-theme-fg);
  --mo-btn-active-bg: color-mix(in oklab, var(--mo-theme-base) 16%, transparent);
}

/* Ghost — invisible until hover. Great for toolbars and icon buttons. */
.btn-ghost {
  --mo-btn-color: var(--mo-theme-fg);
  --mo-btn-bg: transparent;
  --mo-btn-hover-bg: color-mix(in oklab, var(--mo-theme-base) 10%, transparent);
  --mo-btn-active-bg: color-mix(in oklab, var(--mo-theme-base) 17%, transparent);
}

/* Link — looks like a link, sized like a button */
.btn-link {
  --mo-btn-color: var(--mo-theme-fg);
  --mo-btn-bg: transparent;
  --mo-btn-hover-color: var(--mo-theme-fg-emphasis);
  --mo-btn-hover-bg: transparent;
  --mo-btn-active-bg: transparent;
  --mo-btn-padding-x: .25rem;
  text-underline-offset: .2em;
}
.btn-link:hover { text-decoration: underline; }

/* Sizes ------------------------------------------------------------------- */
.btn-xs,
.btn-group-xs > * {
  --mo-btn-height: 1.75rem;
  --mo-btn-padding-x: .5rem;
  --mo-btn-font-size: .75rem;
  --mo-btn-gap: .25rem;
  --mo-btn-radius: calc(var(--mo-radius-control) * .65);
}
.btn-sm,
.btn-group-sm > * {
  --mo-btn-height: var(--mo-control-height-sm);
  --mo-btn-padding-x: .75rem;
  --mo-btn-font-size: .8125rem;
  --mo-btn-gap: .375rem;
}
.btn-lg,
.btn-group-lg > * {
  --mo-btn-height: var(--mo-control-height-lg);
  --mo-btn-padding-x: 1.25rem;
  --mo-btn-font-size: .9375rem;
  --mo-btn-radius: calc(var(--mo-radius-control) * 1.2);
}

/* Square icon-only button. Always give it an aria-label. */
.btn-icon {
  width: var(--mo-btn-height);
  padding: 0;
}

/* Loading — <button class="btn-solid" aria-busy="true"> */
:is(/*@btn*/):is([aria-busy="true"], .is-loading) {
  color: transparent;
  pointer-events: none;
}
:is(/*@btn*/):is([aria-busy="true"], .is-loading)::after {
  position: absolute;
  inset: 0;
  width: 1.1em;
  height: 1.1em;
  margin: auto;
  content: "";
  border: 2px solid var(--mo-btn-color);
  border-inline-end-color: transparent;
  border-radius: 50%;
  animation: mo-spin .7s linear infinite;
}

@keyframes mo-spin { to { rotate: 360deg; } }
