@use '../variables' as v;
@use '../mixins' as m;
@use 'sass:color';

// ─────────────────────────────────────────────
// Base button
// ─────────────────────────────────────────────
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 16px;
  height: 34px;
  font-size: v.$font-size-sm;
  font-weight: 500;
  font-family: v.$font-family;
  border-radius: v.$radius;
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  transition: background-color v.$transition-base, border-color v.$transition-base,
    color v.$transition-base, box-shadow v.$transition-base, opacity v.$transition-base;
  white-space: nowrap;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;

  &:focus {
    outline: none;
  }

  svg {
    width: 15px;
    height: 15px;
    flex-shrink: 0;
  }

  &:focus-visible {
    @include m.focus-ring;
  }

  &:disabled,
  &.is-disabled {
    opacity: 0.5;
    cursor: not-allowed;
    pointer-events: none;
  }

  &.is-loading {
    position: relative;
    pointer-events: none;
    opacity: 0.8;
  }
}

// ─────────────────────────────────────────────
// Variants
// ─────────────────────────────────────────────

.btn-primary {
  background: linear-gradient(180deg, #323742 0%, #1f232b 100%);
  border: 1px solid #14171d;
  color: v.$white;
  font-size: v.$font-size-xs;
  font-weight: 500;
  border-radius: 8px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 2px 4px rgba(0, 0, 0, 0.35);
  transition: border-color 0.15s ease, box-shadow 0.15s ease, color 0.15s ease;

  &:hover {
    background: linear-gradient(180deg, #3d4350 0%, #262b35 100%);
    border-color: #0d0f13;
    color: v.$white;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.3), 0 4px 8px rgba(0, 0, 0, 0.45);
  }

  &:focus {
    outline: none;
    background: linear-gradient(180deg, #3d4350 0%, #262b35 100%);
    color: v.$white;
  }

  &:active,
  &:focus:active {
    background: linear-gradient(180deg, #191c23 0%, #14171d 100%);
    border-color: #0d0f13;
    color: v.$white;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.5);
  }
}





.btn-outline {
  background: var(--bg-surface);
  border-color: var(--border-color);
  color: var(--text-secondary);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 2px 4px rgba(0, 0, 0, 0.35);

  &:hover {
    background: var(--bg-hover);
    border-color: var(--border-color);
    color: var(--text-primary);
  }
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-secondary);

  &:hover {
    background: var(--bg-hover-strong);
    color: var(--text-primary);
  }
}

.btn-danger {
  background: v.$danger;
  border-color: v.$danger;
  color: v.$white;

  &:hover {
    background: color.adjust(v.$danger, $lightness: -6%);
    color: v.$white;
  }
}

.btn-danger-outline {
  background: var(--bg-surface);
  border-color: v.$danger-border;
  color: v.$danger;

  &:hover {
    background: v.$danger-light;
  }
}

.btn-success {
  background: v.$success;
  border-color: v.$success;
  color: v.$white;

  &:hover {
    background: color.adjust(v.$success, $lightness: -6%);
    color: v.$white;
  }
}

// ─────────────────────────────────────────────
// Sizes
// ─────────────────────────────────────────────
.btn-sm {
  height: 30px;
  padding: 0 12px;
  font-size: v.$font-size-xs;
  border-radius: v.$radius-sm;

  svg {
    width: 13px;
    height: 13px;
  }
}

.btn-lg {
  height: 42px;
  padding: 0 22px;
  font-size: v.$font-size-base;

  svg {
    width: 17px;
    height: 17px;
  }
}

.btn-xl {
  height: 48px;
  padding: 0 28px;
  font-size: v.$font-size-md;
}

// ─────────────────────────────────────────────
// Icon-only button
// ─────────────────────────────────────────────
.btn-icon {
  width: 36px;
  height: 36px;
  padding: 0;

  &.btn-sm {
    width: 30px;
    height: 30px;
  }

  &.btn-lg {
    width: 42px;
    height: 42px;
  }
}

// ─────────────────────────────────────────────
// Button group
// ─────────────────────────────────────────────
.btn-group {
  display: inline-flex;

  .btn {
    border-radius: 0;

    &:first-child {
      border-radius: v.$radius 0 0 v.$radius;
    }

    &:last-child {
      border-radius: 0 v.$radius v.$radius 0;
    }

    &:not(:first-child) {
      margin-left: -1px;
    }
  }
}

// ─────────────────────────────────────────────
// Table row action buttons & title links (Automation design pattern)
// ─────────────────────────────────────────────
.re-action-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 6px;
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
  flex-shrink: 0;

  &:hover {
    background: var(--bg-hover);
    border-color: var(--border-color);
  }

  &.is-disabled,
  &:disabled {
    opacity: 0.45;
    cursor: not-allowed !important;
    pointer-events: none;
    background: transparent !important;
    border-color: transparent !important;
  }

  svg {
    width: 17px;
    height: 17px;
    flex-shrink: 0;
    shape-rendering: geometricPrecision;
    transform: translateZ(0);
    transition: transform 0.22s cubic-bezier(0.34, 1.4, 0.64, 1), stroke 0.2s ease;
    transform-origin: center bottom;

    path {
      stroke-linecap: round;
      stroke-linejoin: round;
    }
  }

  /* Trash Delete Icon Hover Animation */
  &[title="Delete"]:hover svg,
  &[title="delete"]:hover svg,
  &[title*="Delete"]:hover svg,
  &[title*="delete"]:hover svg,
  &:hover svg[stroke="#ef4444"] {
    animation: trashWiggle 0.32s cubic-bezier(0.34, 1.4, 0.64, 1) forwards;
  }
}

@keyframes trashWiggle {
  0% {
    transform: scale(1) rotate(0deg);
  }

  35% {
    transform: scale(1.05) rotate(-7deg);
  }

  70% {
    transform: scale(1.03) rotate(4deg);
  }

  100% {
    transform: scale(1.04) rotate(-2deg);
  }
}

.automation-title-link {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  text-decoration: none;
  transition: color 0.15s ease;

  &:hover {
    color: var(--info, #2563eb);
    text-decoration: underline;
  }
}

.automation-title-sub {
  font-size: 11px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 2px;

  svg {
    width: 12px;
    height: 12px;
    color: var(--text-muted);
  }
}

// ─────────────────────────────────────────────
// Brand Spinner (Project Logo Palette Colors: Blue, Green, Yellow, Red)
// ─────────────────────────────────────────────
.brand-spinner {
  width: 13px;
  height: 13px;
  border: 2px solid rgba(66, 133, 244, 0.2);
  border-top-color: #4285F4;
  border-right-color: #34A853;
  border-bottom-color: #FBBC05;
  border-left-color: #EA4335;
  border-radius: 50%;
  display: inline-block;
  animation: brandSpin 0.75s linear infinite;
  vertical-align: middle;
  flex-shrink: 0;
}

@keyframes brandSpin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

.btn-view-action {
  min-width: 52px;
  justify-content: center;
  transition: all 0.15s ease;

  &.is-loading {
    opacity: 0.85;
    pointer-events: none;
    cursor: wait;
    background: var(--bg-hover);
    border-color: var(--border-color);
  }
}

// ── Responsive Icon-Only Button Helper ──
@media (max-width: 768px) {
  .btn-icon-mobile {
    padding: 0 !important;
    width: 36px !important;
    height: 36px !important;
    min-width: 36px !important;
    justify-content: center !important;
    align-items: center !important;
    gap: 0 !important;
    flex-shrink: 0 !important;

    .btn-text,
    span:not(.badge):not(.color-dot):not(.color-swatch-sm) {
      display: none !important;
    }

    svg {
      margin: 0 !important;
      width: 16px !important;
      height: 16px !important;
    }
  }
}