/* Kodza — Loading Button
   A button that shows its work: clicked, its words roll to the second one while a ring comes
   in on the left; when your code says done, the ring closes on a tick and the words roll to
   the third one, then back to the first. The width never moves (the three share one cell).
   From your code: listen to "kodza:loading", then el.kodzaLoading.done() (or .reset()).
   Everything is tuned with the --kz-loading-* variables. */
.kodza-loading-button {
  --kz-loading-bg: #0a0a0a;
  --kz-loading-fg: #ffffff;
  --kz-loading-height: 52px;
  --kz-loading-pad-x: 28px;
  --kz-loading-icon: 18px;
  --kz-loading-gap: 10px;
  --kz-loading-ease: cubic-bezier(0.22, 1, 0.36, 1);

  /* Room for the ring is kept on both sides, so the words can stay centred at rest */
  --kz-loading-room: calc((var(--kz-loading-icon) + var(--kz-loading-gap)) / 2);

  position: relative;
  display: inline-grid;
  align-items: center;
  height: var(--kz-loading-height);
  padding: 0 calc(var(--kz-loading-pad-x) + var(--kz-loading-room));
  border: 0;
  border-radius: 999px;
  background: var(--kz-loading-bg);
  color: var(--kz-loading-fg);
  font: inherit;
  font-weight: 500;
  letter-spacing: -0.01em;
  white-space: nowrap;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.25s var(--kz-loading-ease), background-color 0.3s ease;
}

.kodza-loading-button:active {
  transform: scale(0.97);
}

.kodza-loading-button[aria-busy="true"] {
  cursor: progress;
}

.kodza-loading-button:focus-visible {
  outline: 2px solid var(--kz-loading-bg);
  outline-offset: 3px;
}

.kodza-loading-button--soft {
  --kz-loading-bg: rgba(10, 10, 10, 0.07);
  --kz-loading-fg: #0a0a0a;
}

.kodza-loading-button--soft:focus-visible {
  outline-color: var(--kz-loading-fg);
}

/* The words: three in one cell, the current one in its place, the others above or below */
.kodza-loading-button__label {
  display: inline-grid;
  grid-area: 1 / 1;
  overflow: hidden;
  padding-block: 0.15em;
  transition: transform 0.55s var(--kz-loading-ease);
}

.kodza-loading-button__label > span {
  grid-area: 1 / 1;
  justify-self: center;
}

.kodza-loading-button__label > span:not(:first-child) {
  transform: translateY(110%);
}

/* Busy or done: the words make room for the ring */
.kodza-loading-button:is([data-state="loading"], [data-state="done"]) .kodza-loading-button__label {
  transform: translateX(var(--kz-loading-room));
}

/* The ring, then the tick */
.kodza-loading-button__icon {
  position: absolute;
  top: 50%;
  left: calc(var(--kz-loading-pad-x) - 4px);
  width: var(--kz-loading-icon);
  height: var(--kz-loading-icon);
  margin-top: calc(var(--kz-loading-icon) / -2);
  opacity: 0;
  transform: translateX(-8px) scale(0.6);
  transition: opacity 0.25s ease, transform 0.5s var(--kz-loading-ease);
}

.kodza-loading-button:is([data-state="loading"], [data-state="done"]) .kodza-loading-button__icon {
  opacity: 1;
  transform: none;
}

.kodza-loading-button__icon svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.kodza-loading-button__icon circle:first-child {
  opacity: 0.22;
}

.kodza-loading-button__icon circle:nth-child(2) {
  stroke-dasharray: 28 100;
  transform: rotate(-90deg);
  transform-origin: 50% 50%;
  transition: stroke-dasharray 0.5s cubic-bezier(0.65, 0, 0.35, 1);
}

.kodza-loading-button[data-state="loading"] .kodza-loading-button__icon svg {
  animation: kodza-loading-turn 0.9s linear infinite;
}

@keyframes kodza-loading-turn {
  to {
    transform: rotate(360deg);
  }
}

.kodza-loading-button__icon path {
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  transition: stroke-dashoffset 0.2s ease;
}

/* Done: the arc closes into a full ring, the tick draws itself */
.kodza-loading-button[data-state="done"] .kodza-loading-button__icon circle:nth-child(2) {
  stroke-dasharray: 100 100;
}

.kodza-loading-button[data-state="done"] .kodza-loading-button__icon path {
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 0.4s var(--kz-loading-ease) 0.3s;
}

.kodza-loading-button__sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

@media (prefers-reduced-motion: reduce) {
  .kodza-loading-button[data-state="loading"] .kodza-loading-button__icon svg {
    animation: none;
  }

  .kodza-loading-button__label,
  .kodza-loading-button__icon,
  .kodza-loading-button__icon circle,
  .kodza-loading-button__icon path {
    transition-duration: 0.01ms !important;
    transition-delay: 0s !important;
  }
}

/* Theming example — on a dark background:
.dark-section .kodza-loading-button {
  --kz-loading-bg: #ffffff;
  --kz-loading-fg: #0a0a0a;
}
.dark-section .kodza-loading-button--soft {
  --kz-loading-bg: rgba(255, 255, 255, 0.1);
  --kz-loading-fg: #ffffff;
}
*/
