// ============================================================
// Buttons Component
// .cyber-btn — cut-corner neon button with a sweep fill on hover.
// States: hover, :focus-visible, :active, disabled (`disabled`,
// `aria-disabled="true"`), loading (`.cyber-btn--loading` or
// `aria-busy="true"`), pressed (`aria-pressed="true"`).
// ============================================================

@use "kit";

// Reduced motion: no sweep, no travel; the busy ring breathes instead of spinning
@mixin -reduced-motion {
  .cyber-btn {
    transition: none;

    &:active {
      transform: none;
    }

    &--loading::after,
    &[aria-busy="true"]::after {
      animation: cyber-btn-breathe 1.6s ease-in-out infinite;
      border-right-color: var(--btn-color);
    }
  }
}

@layer components {
  .cyber-btn {
    // Public theming API
    --btn-color: var(--color-accent, var(--cyber-cyan-500));
    --btn-on-color: var(--color-text-on-accent, var(--cyber-void-500));
    --btn-bg: transparent;

    // Hover halo, in box-shadow syntax. A 0.3.0 hook: takes any shadow list
    // (e.g. var(--glow-magenta)) or `none`. Follows --btn-color by default.
    --btn-glow: 0 0 0.75rem
      color-mix(in srgb, var(--btn-color) calc(40% * var(--glow-intensity, 1)), transparent);
    --btn-height: var(--control-height-md, 2.75rem);
    --btn-padding: var(--space-md);
    --btn-cut: 10px;

    // Frame wiring
    --frame-cut: var(--btn-cut);
    --frame-border-width: var(--border-medium);
    --frame-border-color: var(--btn-color);
    --frame-bg: var(--btn-bg);
    --frame-bg-image: linear-gradient(var(--btn-color), var(--btn-color));
    --frame-bg-size: 0% 100%;

    @include kit.cut-frame;
    @include kit.label-text(var(--text-sm), 0.1em);

    display: inline-flex;
    gap: var(--space-2xs);
    align-items: center;
    justify-content: center;
    min-height: var(--btn-height);
    padding: var(--space-3xs) var(--btn-padding);
    border: 0;
    line-height: 1.1;
    text-align: center;
    text-decoration: none;
    white-space: nowrap;
    color: var(--btn-color);
    background: none;
    transition:
      color var(--duration-fast) var(--ease-cyber),
      box-shadow var(--duration-fast) ease,
      filter var(--duration-fast) ease,
      transform var(--duration-fast) ease;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;

    // Hover: fill sweeps in, label flips to the dark on-accent colour
    @include kit.hover {
      --frame-bg-size: 100% 100%;

      color: var(--btn-on-color);
      box-shadow: var(--btn-glow);

      // Edge light that follows the cut corners, which a box-shadow cannot
      filter: drop-shadow(
        0 0 0.3rem
          color-mix(in srgb, var(--btn-color) calc(55% * var(--glow-intensity, 1)), transparent)
      );
    }

    svg {
      flex: none;
      width: 1.15em;
      height: 1.15em;
    }

    &:focus-visible {
      @include kit.focus-ring(var(--btn-color));
    }

    // Pressed gives touch devices the same fill feedback hover gives a mouse
    &:active,
    &[aria-pressed="true"] {
      --frame-bg-size: 100% 100%;

      color: var(--btn-on-color);
    }

    &:active {
      transform: translateY(1px);
    }

    &:disabled,
    &[aria-disabled="true"],
    &--disabled {
      --frame-border-style: dashed;

      opacity: 50%;
      cursor: not-allowed;
      pointer-events: none;
      filter: saturate(40%);
    }

    // --------------------------------------------------------
    // Colour variants
    // --------------------------------------------------------

    &--cyan {
      --btn-color: var(--cyber-cyan-500);
    }

    &--magenta {
      --btn-color: var(--cyber-magenta-500);
    }

    &--yellow {
      --btn-color: var(--cyber-yellow-500);
    }

    &--green {
      --btn-color: var(--cyber-green-500);
    }

    // Neutral, for secondary actions next to a coloured primary
    &--chrome {
      --btn-color: var(--cyber-chrome-200);
    }

    // --------------------------------------------------------
    // Style variants (`.cyber-btn--outline` is the default look)
    // --------------------------------------------------------

    &--ghost {
      --frame-border-style: dashed;
    }

    // Solid primary. Hover sweeps the dark fill back in.
    &--filled {
      --btn-bg: var(--btn-color);
      --frame-bg-image: linear-gradient(var(--btn-on-color), var(--btn-on-color));

      color: var(--btn-on-color);

      @include kit.hover {
        color: var(--btn-color);
      }

      &:active,
      &[aria-pressed="true"] {
        color: var(--btn-color);
      }
    }

    // --------------------------------------------------------
    // Sizes
    // --------------------------------------------------------

    &--sm {
      --btn-height: var(--control-height-sm, 2rem);
      --btn-padding: var(--space-sm);
      --btn-cut: 7px;

      font-size: kit.$text-xs;
    }

    &--lg {
      --btn-height: var(--control-height-lg, 3.25rem);
      --btn-padding: var(--space-lg);
      --btn-cut: 14px;

      font-size: var(--text-base);
    }

    // Square, for a single icon. Needs an `aria-label`.
    &--icon {
      width: var(--btn-height);
      padding: 0;
    }

    &--block {
      display: flex;
      width: 100%;
    }

    // --------------------------------------------------------
    // Loading: hides the label, keeps the width, shows a ring
    // --------------------------------------------------------

    &--loading,
    &[aria-busy="true"] {
      color: transparent;
      pointer-events: none;

      &::after {
        content: "";
        position: absolute;
        top: 50%;
        left: 50%;
        width: 1.1em;
        height: 1.1em;
        margin: -0.55em 0 0 -0.55em;
        border: 2px solid var(--btn-color);
        border-radius: 50%;
        animation: cyber-btn-spin 0.7s linear infinite;
        border-right-color: transparent;
      }
    }

    &--filled.cyber-btn--loading::after,
    &--filled[aria-busy="true"]::after {
      border-color: var(--btn-on-color);
      border-right-color: transparent;
    }
  }

  // --------------------------------------------------------
  // Button group: shared edges, one row
  // --------------------------------------------------------

  .cyber-btn-group {
    display: inline-flex;
    flex-wrap: wrap;
    gap: var(--space-2xs);
    align-items: center;

    &--attached {
      flex-wrap: nowrap;
      gap: 0;

      .cyber-btn {
        --btn-cut: 0;

        &:not(:first-child) {
          margin-inline-start: calc(var(--border-medium) * -1);
        }

        &:first-child,
        &:last-child {
          --btn-cut: 10px;
        }

        &:hover,
        &:focus-visible {
          z-index: 1;
        }
      }
    }
  }

  @keyframes cyber-btn-spin {
    to {
      transform: rotate(360deg);
    }
  }

  @keyframes cyber-btn-breathe {
    50% {
      opacity: 35%;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    @include -reduced-motion;
  }

  [data-motion="reduce"] {
    @include -reduced-motion;
  }

  @media (prefers-contrast: more) {
    .cyber-btn {
      --frame-border-width: var(--border-thick);

      &:disabled,
      &[aria-disabled="true"],
      &--disabled {
        opacity: 70%;
      }
    }
  }

  // Forced colours: the frame mixin already drops the painted sweep
  @media (forced-colors: active) {
    .cyber-btn {
      // Text colours are forced, so the label stays readable while busy
      &--loading::after,
      &[aria-busy="true"]::after {
        display: none;
      }

      &:disabled,
      &[aria-disabled="true"],
      &--disabled {
        color: GrayText;
        opacity: 100%;
      }
    }
  }
}
