=selector-index-append($target-index, $appending-selector)
  @if $target-index == 0
    @error '$target-index cannot be `0`'
  $new-selectors: ''
  @each $selector in &
    $local-index: $target-index
    @while $local-index < 0
      $local-index: $local-index + length($selector) + 1
    $i: 0
    $new-selector: ''
    @each $sel in $selector
      $i: $i + 1
      @if $i == $local-index
        $sel: selector-append($sel, $appending-selector)
      $new-selector: $new-selector + $sel + ' '
    @if $new-selector != $selector
      $new-selectors: $new-selectors + $new-selector + ', '
  @at-root #{$new-selectors}
    @content

=selector-index-append($target-index, $appending-selector)
  @if $target-index == 0
    @error '$target-index cannot be `0`'
  $new-selectors: ''
  @each $selector in &
    $local-index: $target-index
    @while $local-index < 0
      $local-index: $local-index + length($selector) + 1
    $i: 0
    $new-selector: ''
    @each $sel in $selector
      $i: $i + 1
      @if $i == $local-index
        $sel: selector-replace($sel, $sel, $sel + $appending-selector)
      $new-selector: $new-selector + $sel + ' '
    @if $new-selector != $selector
      $new-selectors: $new-selectors + $new-selector + ', '
  @at-root #{$new-selectors}
    @content

Change function to bring class selectors ahead of pseudo-element selectors
Change function to bring class selectors ahead of pseudo-element selectors
