@use 'ui-design-system/src/styles/semantic' as *;

.ui-loading-spinner {
  // Core Structure
  display: inline-flex;
  align-items: center;
  justify-content: center;
  position: relative;
  flex-shrink: 0;
  
  // Sizing Variants
  &--sm {
    width: $semantic-sizing-icon-inline;
    height: $semantic-sizing-icon-inline;
  }
  
  &--md {
    width: $semantic-sizing-icon-button;
    height: $semantic-sizing-icon-button;
  }
  
  &--lg {
    width: $semantic-sizing-icon-navigation;
    height: $semantic-sizing-icon-navigation;
  }
  
  // Color Variants
  &--primary {
    color: $semantic-color-brand-primary;
  }
  
  &--secondary {
    color: $semantic-color-brand-secondary;
  }
  
  &--accent {
    color: $semantic-color-brand-accent;
  }
  
  &--success {
    color: $semantic-color-success;
  }
  
  &--warning {
    color: $semantic-color-warning;
  }
  
  &--danger {
    color: $semantic-color-danger;
  }
  
  &--info {
    color: $semantic-color-info;
  }
  
  // Style Variants
  &--spin .ui-loading-spinner__spin {
    border: 2px solid $semantic-color-border-secondary;
    border-top: 2px solid currentColor;
    border-radius: 50%;
    width: 100%;
    height: 100%;
    animation: ui-spinner-spin 1s ease-in-out infinite;
  }
  
  &--dots {
    .ui-loading-spinner__dots {
      display: flex;
      gap: 0.25rem;
      
      .ui-loading-spinner__dot {
        width: 25%;
        height: 25%;
        background-color: currentColor;
        border-radius: 50%;
        animation: ui-spinner-dots 0.6s ease-in-out infinite;
        
        &:nth-child(1) { animation-delay: 0ms; }
        &:nth-child(2) { animation-delay: 150ms; }
        &:nth-child(3) { animation-delay: 300ms; }
      }
    }
  }
  
  &--pulse .ui-loading-spinner__pulse {
    width: 100%;
    height: 100%;
    background-color: currentColor;
    border-radius: 50%;
    animation: ui-spinner-pulse 1s ease-in-out infinite;
  }
  
  &--bars {
    .ui-loading-spinner__bars {
      display: flex;
      align-items: flex-end;
      gap: 20%;
      height: 100%;
      
      .ui-loading-spinner__bar {
        width: 15%;
        background-color: currentColor;
        border-radius: 0.125rem;
        animation: ui-spinner-bars 0.6s ease-in-out infinite;
        
        &:nth-child(1) { animation-delay: 0ms; }
        &:nth-child(2) { animation-delay: 100ms; }
        &:nth-child(3) { animation-delay: 200ms; }
      }
    }
  }
  
  // State Variants
  &--disabled {
    opacity: 0.38;
    pointer-events: none;
  }
  
  // Speed Variants
  &--slow {
    .ui-loading-spinner__spin {
      animation-duration: 2s;
    }
    
    .ui-loading-spinner__dots .ui-loading-spinner__dot {
      animation-duration: 1.2s;
    }
    
    .ui-loading-spinner__pulse {
      animation-duration: 2s;
    }
    
    .ui-loading-spinner__bars .ui-loading-spinner__bar {
      animation-duration: 1.2s;
    }
  }
  
  &--fast {
    .ui-loading-spinner__spin {
      animation-duration: 0.5s;
    }
    
    .ui-loading-spinner__dots .ui-loading-spinner__dot {
      animation-duration: 0.3s;
    }
    
    .ui-loading-spinner__pulse {
      animation-duration: 0.5s;
    }
    
    .ui-loading-spinner__bars .ui-loading-spinner__bar {
      animation-duration: 0.3s;
    }
  }
  
  // Label
  &__label {
    margin-left: 0.5rem;
    color: $semantic-color-text-secondary;
    font-size: 0.875rem;
    font-weight: 500;
  }
  
  // Screen Reader Only Text
  &__sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }
  
  // Accessibility - Respect reduced motion preference
  @media (prefers-reduced-motion: reduce) {
    .ui-loading-spinner__spin,
    .ui-loading-spinner__dots .ui-loading-spinner__dot,
    .ui-loading-spinner__pulse,
    .ui-loading-spinner__bars .ui-loading-spinner__bar {
      animation: none;
    }
    
    .ui-loading-spinner__spin {
      border-top-color: transparent;
      border-right-color: transparent;
      border-bottom-color: transparent;
    }
    
    .ui-loading-spinner__pulse {
      opacity: 0.6;
    }
  }
  
  // High contrast mode
  @media (prefers-contrast: high) {
    .ui-loading-spinner__spin {
      border-width: 3px;
    }
  }
}

// Keyframes
@keyframes ui-spinner-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

@keyframes ui-spinner-dots {
  0%, 20%, 80%, 100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.5);
    opacity: 0.7;
  }
}

@keyframes ui-spinner-pulse {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.5;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes ui-spinner-bars {
  0%, 40%, 100% {
    height: 40%;
  }
  20% {
    height: 100%;
  }
}