@use 'ui-design-system/src/styles/semantic/index' as *;
.ui-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  position: relative;
  vertical-align: top;
  white-space: nowrap;
  overflow: hidden;
  margin-right: 0.75rem;
  margin-bottom: 0.5rem;
  
  // Prevent text selection
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  
  // Base typography
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-weight: 500;
  text-decoration: none;
  outline: none;
  
  // Transitions
  transition: all $semantic-duration-fast $semantic-easing-standard;
  
  // Size variants
  &--sm {
    height: 24px;
    font-size: 0.75rem;
    line-height: 1;
    
    .chip-label {
      padding: 0 0.5rem;
    }
    
    .chip-leading-icon,
    .chip-trailing-icon {
      width: 18px;
      height: 18px;
      font-size: 0.75rem;
      margin: 0 0.25rem;
    }
    
    .chip-avatar {
      width: 18px;
      height: 18px;
      margin: 0 0.25rem 0 0.125rem;
      
      img {
        width: 18px;
        height: 18px;
        border-radius: 9px;
      }
    }
    
    &.ui-chip--with-leading-icon .chip-label,
    &.ui-chip--with-avatar .chip-label {
      padding-left: 0.25rem;
    }
    
    &.ui-chip--with-trailing-icon .chip-label,
    &.ui-chip--removable .chip-label {
      padding-right: 0.25rem;
    }
  }
  
  &--md {
    height: 32px;
    font-size: 0.875rem;
    line-height: 1;
    
    .chip-label {
      padding: 0 0.75rem;
    }
    
    .chip-leading-icon,
    .chip-trailing-icon {
      width: 24px;
      height: 24px;
      font-size: 1rem;
      margin: 0 0.25rem;
    }
    
    .chip-avatar {
      width: 24px;
      height: 24px;
      margin: 0 0.25rem 0 0.125rem;
      
      img {
        width: 24px;
        height: 24px;
        border-radius: 12px;
      }
    }
    
    &.ui-chip--with-leading-icon .chip-label,
    &.ui-chip--with-avatar .chip-label {
      padding-left: 0.5rem;
    }
    
    &.ui-chip--with-trailing-icon .chip-label,
    &.ui-chip--removable .chip-label {
      padding-right: 0.5rem;
    }
  }
  
  &--lg {
    height: 40px;
    font-size: 1rem;
    line-height: 1;
    
    .chip-label {
      padding: 0 1rem;
    }
    
    .chip-leading-icon,
    .chip-trailing-icon {
      width: 28px;
      height: 28px;
      font-size: 1.125rem;
      margin: 0 0.375rem;
    }
    
    .chip-avatar {
      width: 32px;
      height: 32px;
      margin: 0 0.375rem 0 0.25rem;
      
      img {
        width: 32px;
        height: 32px;
        border-radius: 16px;
      }
    }
    
    &.ui-chip--with-leading-icon .chip-label,
    &.ui-chip--with-avatar .chip-label {
      padding-left: 0.5rem;
    }
    
    &.ui-chip--with-trailing-icon .chip-label,
    &.ui-chip--removable .chip-label {
      padding-right: 0.5rem;
    }
  }
  
  // Radius variants
  &--radius-none {
    border-radius: 0;
  }
  
  &--radius-sm {
    border-radius: 0.25rem;
  }
  
  &--radius-md {
    border-radius: 0.375rem;
  }
  
  &--radius-lg {
    border-radius: 0.5rem;
  }
  
  &--radius-capsule {
    border-radius: 50px;
  }
  
  // Variant styles
  &--filled {
    background-color: $semantic-color-surface-elevated;
    color: $semantic-color-text-primary;
    border: 1px solid transparent;
    
    &:hover:not(.ui-chip--disabled) {
      background-color: $semantic-color-surface-selected;
      transform: translateY(-1px);
    }
    
    &:active:not(.ui-chip--disabled) {
      transform: scale(0.95);
    }
    
    &.ui-chip--selected {
      background-color: $semantic-color-primary;
      color: $semantic-color-text-inverse;
      
      &:hover:not(.ui-chip--disabled) {
        background-color: $semantic-color-primary-hover;
      }
    }
  }
  
  &--outlined {
    background-color: transparent;
    color: $semantic-color-primary;
    border: 1px solid $semantic-color-border-primary;
    
    &:hover:not(.ui-chip--disabled) {
      background-color: $semantic-color-surface-elevated;
      border-color: $semantic-color-primary;
      transform: translateY(-1px);
    }
    
    &:active:not(.ui-chip--disabled) {
      background-color: $semantic-color-surface-primary;
      transform: scale(0.95);
    }
    
    &.ui-chip--selected {
      background-color: $semantic-color-surface-primary;
      border-color: $semantic-color-primary;
      
      &:hover:not(.ui-chip--disabled) {
        background-color: $semantic-color-surface-elevated;
      }
    }
  }
  
  &--elevated {
    background-color: $semantic-color-surface-primary;
    color: $semantic-color-text-primary;
    border: none;
    box-shadow: $semantic-shadow-elevation-1;
    
    &:hover:not(.ui-chip--disabled) {
      box-shadow: $semantic-shadow-elevation-2;
      transform: translateY(-1px);
    }
    
    &:active:not(.ui-chip--disabled) {
      box-shadow: $semantic-shadow-elevation-1;
      transform: scale(0.95);
    }
    
    &.ui-chip--selected {
      background-color: $semantic-color-primary;
      color: $semantic-color-text-inverse;
      box-shadow: $semantic-shadow-elevation-2;
      
      &:hover:not(.ui-chip--disabled) {
        background-color: $semantic-color-primary-hover;
        box-shadow: $semantic-shadow-elevation-3;
      }
    }
  }
  
  // States
  &--clickable {
    cursor: pointer;
    
    &:focus-visible {
      outline: 2px solid $semantic-color-border-focus;
      outline-offset: 2px;
    }
  }
  
  &--disabled {
    opacity: 0.38;
    cursor: not-allowed;
    pointer-events: none;
  }
  
  &--selected {
    // Selected styles are handled in variant sections above
  }
  
  // Icon and avatar styles
  .chip-leading-icon,
  .chip-trailing-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }
  
  .chip-trailing-icon {
    cursor: pointer;
    border-radius: 50%;
    transition: background-color $semantic-duration-fast ease;
    
    &:hover {
      background-color: rgba(0, 0, 0, 0.1);
    }
    
    &:focus-visible {
      outline: 1px solid $semantic-color-border-focus;
      outline-offset: 1px;
    }
  }
  
  .chip-avatar {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    overflow: hidden;
    
    img {
      object-fit: cover;
      border: 1px solid rgba(0, 0, 0, 0.1);
    }
  }
  
  .chip-label {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 1;
    min-width: 0;
    text-align: center;
  }
}

// Dark mode support
@media (prefers-color-scheme: dark) {
  .ui-chip {
    &--filled {
      background-color: hsl(279, 14%, 25%);
      color: hsl(0, 0%, 90%);
      
      &:hover:not(.ui-chip--disabled) {
        background-color: hsl(279, 14%, 30%);
      }
      
      &.ui-chip--selected {
        background-color: hsl(258, 100%, 47%);
        color: white;
        
        &:hover:not(.ui-chip--disabled) {
          background-color: hsl(258, 100%, 52%);
        }
      }
    }
    
    &--outlined {
      background-color: transparent;
      color: hsl(258, 100%, 67%);
      border-color: hsl(279, 14%, 45%);
      
      &:hover:not(.ui-chip--disabled) {
        background-color: hsl(279, 14%, 15%);
        border-color: hsl(258, 100%, 67%);
      }
      
      &:active:not(.ui-chip--disabled) {
        background-color: hsl(279, 14%, 20%);
      }
      
      &.ui-chip--selected {
        background-color: hsl(279, 14%, 20%);
        border-color: hsl(258, 100%, 67%);
        
        &:hover:not(.ui-chip--disabled) {
          background-color: hsl(279, 14%, 25%);
        }
      }
    }
    
    &--elevated {
      background-color: hsl(279, 14%, 15%);
      color: hsl(0, 0%, 90%);
      
      &:hover:not(.ui-chip--disabled) {
        background-color: hsl(279, 14%, 20%);
      }
      
      &.ui-chip--selected {
        background-color: hsl(258, 100%, 47%);
        color: white;
        
        &:hover:not(.ui-chip--disabled) {
          background-color: hsl(258, 100%, 52%);
        }
      }
    }
    
    .chip-trailing-icon:hover {
      background-color: rgba(255, 255, 255, 0.1);
    }
    
    .chip-avatar img {
      border-color: rgba(255, 255, 255, 0.2);
    }
  }
}

// Accessibility
@media (prefers-reduced-motion: reduce) {
  .ui-chip {
    transition: none;
    
    &:hover,
    &:active {
      transform: none;
    }
  }
}

// Chip group/set container utility
.ui-chip-set {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: flex-start;
  
  &--row {
    flex-wrap: nowrap;
    overflow-x: auto;
    scroll-padding-left: 1rem;
    scroll-snap-type: x mandatory;
    
    .ui-chip {
      scroll-snap-align: center;
      flex-shrink: 0;
    }
    
    &::-webkit-scrollbar {
      height: 0;
      background: transparent;
    }
    
    scrollbar-width: none;
    -ms-overflow-style: none;
  }
  
  &--no-scroll {
    overflow-x: visible;
  }
}

// Responsive
@media (max-width: 768px) {
  .ui-chip {
    margin-right: 0.5rem;
    margin-bottom: 0.375rem;
    
    &--lg {
      height: 36px;
      font-size: 0.875rem;
      
      .chip-label {
        padding: 0 0.75rem;
      }
    }
  }
}