// Drawing mixins

@function _backdrop_color($c) {
  //
  // adjusts colors for use in the backdrop state
  //
  @if $light {
    @return if($c!=white, lighten($c, 5%), transparentize(white, 0.3));
  }
  @else {
    @if $c==$selected_bg_color or $c==$hb_pathbar_bg {
      @return lighten($c, 5%);
    } @else {
      @return darken($c, 2%);
    }
  }
}
// generic drawing of more complex things

@function _widget_edge($c:$borders_edge) {
// outer highlight "used" on most widgets
  @return 0 1px $c;
}

@mixin _shadows($shadow1, $shadow2:none, $shadow3:none, $shadow4:none) {
//
// Helper function to stack up to 4 box-shadows;
//
  @if $shadow4!=none { box-shadow: $shadow1, $shadow2, $shadow3, $shadow4; }
  @else if $shadow3!=none { box-shadow: $shadow1, $shadow2, $shadow3; }
  @else if $shadow2!=none { box-shadow: $shadow1, $shadow2; }
  @else { box-shadow: $shadow1; }
}

// entries

@function entry_focus_border($fc:$selected_bg_color) {
  @if $variant == 'light' { @return $fc; }
  @else { @return if($fc==$selected_bg_color, $selected_borders_color, darken($fc,35%)); }
}

@function entry_focus_shadow($fc:$selected_bg_color) {
  @return inset 0 0 0 1px $fc;
}

@function entry_gradient($c) {
  @if $variant=='light' { @return linear-gradient(to bottom, mix($borders_color, $c, 35%),
                                                             mix($borders_color, $c, 7%) 3px,
                                                             $c 90%); }
  @else { @return linear-gradient(to bottom, mix($borders_color, $c, 95%),
                                             mix($borders_color, $c, 40%) 3px,
                                             $c 90%); }
}

@mixin entry($t, $fc:$selected_bg_color, $c: $base_color, $tc: $fg_color, $edge: $borders_edge) {
//
// Entries drawing function
//
// $t: entry type
// $fc: focus color
// $edge: set to none to not draw the bottom edge or specify a color to not
//        use the default one
//
// possible $t values:
// normal, focus, insensitive, backdrop, backdrop-insensitive, osd, osd-focus, osd-backdrop;
//

  background-color: transparent;
  background-image: entry_gradient($base_color);
  $_blank_edge: if($edge == none, none, 0 1px transparentize($edge,1));
  $_entry_edge: inset 0 1px 1px 0px rgba(0, 0, 0, if($c==$headerbar_bg_color, .15, 0.05));

  @if $t==normal {
    $_bc: if($c == $headerbar_bg_color, lighten($hb_pathbar_bg, 5%), $c);
    color: if($tc != $fg_color, transparentize($tc, .1), $fg_color);
    border-color: if($c != $base_color, transparent, $borders_color);
    @if $c!=$headerbar_bg_color {
      @if $variant == 'light' {
        border-top-color: darken($_bc, if($c != $base_color, 12%, 20%));
      }
      else {
        border-top-color: darken($_bc, if($c != $base_color, 12%, 20%));
      }
      @include _shadows($_entry_edge);
    }
    @else {
      border-top-color: $_bc;
      box-shadow: none;
    }
    background-color: $_bc;
    // for the transition to work the number of shadows in different states needs to match, hence the transparent shadow here.
  }
  @if $t==focus {
    @include _shadows($_entry_edge);
    border-color: entry_focus_border($fc);
  }
  @if $t==insensitive {
    background-color: if($c != $base_color, mix($c, if($c == $headerbar_bg_color, $headerbar_bg_color, $base_color), 85%), transparent);
    border-color: if($c == $headerbar_bg_color, $hb_pathbar_bg, $insensitive_borders_color);
    box-shadow: none;
    color: if($tc != $text_color, mix($tc, $c, 50%), $insensitive_fg_color);
  }
  @if $t==backdrop {
    $_bg: null;
    $_tc: null;
    $_border_c: null;
    @if $c == $headerbar_bg_color {
        $_bg: $hb_button_bg_hover;
        $_tc: $backdrop_headerbar_text_color;
        $_border_c: $_bg;
    } @else {
        $_bg: if($variant=='light', _backdrop_color($c), $c);
        $_tc: if($tc != $text_color, mix($tc, $c, 80%), $backdrop_text_color);
        $_border_c: $backdrop_borders_color
    }
    background-color: $_bg;
    border-color: $_border_c;
    box-shadow: none;
    color: $_tc;

    .label, & { color: $_tc; }
  }
  @if $t==backdrop-insensitive {
    background-color: transparent;
    border-color: if($c != $base_color, transparent, transparentize($backdrop_borders_color, 0.3));
    box-shadow: none;
    color: if($c != $base_color, mix($tc, $c, 35%), $backdrop_insensitive_color);

    .label, & { color:  $backdrop_fg_color; }
  }
  @if $t==osd {
    color: $osd_text_color;
    border-color: $osd_borders_color;
    background-color: transparentize(opacify($osd_borders_color, 1), 0.5);
    background-clip: padding-box;
    box-shadow: none;
  }
  @if $t==osd-focus {
    color: $osd_text_color;
    border-color: $selected_bg_color;
    background-color: transparentize(opacify($osd_borders_color, 1), 0.5);
    background-clip: padding-box;
  }
  @if $t==osd-insensitive {
    color: $osd_insensitive_fg_color;
    border-color: $osd_borders_color;
    background-color: $osd_insensitive_bg_color;
    background-clip: padding-box;
    box-shadow: none;
  }
  @if $t==osd-backdrop {
    color: $osd_text_color;
    border-color: $osd_borders_color;
    background-color: transparentize(opacify($osd_borders_color, 1), 0.5);
    background-clip: padding-box;
    box-shadow: none;
  }
}

// buttons

@function _border_color($c, $darker: false) {
  @if $darker == true {
    @if lightness($c)<40% { @return darken($c, 20%); }
    @else { @return darken($c, 10%); }
  }
  @else {
    @if lightness($c)<40% { @return darken($c, 15%); }
    @else { @return darken($c, 13%); }
  }
}

@function _text_shadow_color ($tc:$fg_color, $bg:$bg_color) {
//
// calculate the color of text shadows
//
// $tc is the text color
// $bg is the background color
//
  $_lbg: lightness($bg)/100%;
  @if lightness($tc)<50% { @return transparentize(white,1-$_lbg/($_lbg*1.3)); }
  @else { @return transparentize(black,$_lbg*0.8); }
}

@function _button_hilight_color($c) {
//
// calculate the right top hilight color for buttons
//
// $c: base color;
//
  @if lightness($c)>95% { @return white; }
  @else if lightness($c)>90% { @return transparentize(white, 0.2); }
  @else if lightness($c)>80% { @return transparentize(white, 0.4); }
  @else if lightness($c)>50% { @return transparentize(white, 0.6); }
  @else if lightness($c)>40% { @return transparentize(white, 0.8); }
  @else { @return transparentize(white, 0.95); }
}

@mixin _button_text_shadow ($tc:$fg_color, $bg:$bg_color) {
//
// helper function for the text emboss effect
//
// $tc is the optional text color, not the shadow color
//
// TODO: this functions needs a way to deal with special cases
//

  $_shadow: _text_shadow_color($tc, $bg);

  @if lightness($tc)<50% {
    text-shadow: 0 1px $_shadow;
    icon-shadow: 0 1px $_shadow;
  }
  @else {
    text-shadow: 0 -1px $_shadow;
    icon-shadow: 0 -1px $_shadow;
  }
}

@mixin button($t, $c:$button_bg_color, $tc:$fg_color, $flat:false) {
  // Button drawing function
  //
  // $t:    button type,
  // $c:    base button color for colored* types
  // $tc:   optional text color for colored* types
  // $flat:  true/false
  //
  // possible $t values:
  // normal, hover, active, insensitive, insensitive-active,
  // backdrop, backdrop-hover, backdrop-active, backdrop-insensitive, backdrop-insensitive-active,
  // osd, osd-hover, osd-active, osd-insensitive, osd-backdrop, undecorated

  $_pressed: if(lightness($c)<15%, transparentize(black, 0.4), transparentize(black, 0.8));
  $shadow: null;
  @if $variant == "dark" or $c != $button_bg_color { $shadow: 0px 2px 2px -1px $borders_edge, inset 0 -1px _button_hilight_color($c); }
  @else { $shadow: 0px 2px 2px -1px $borders_edge; }

  $_border: if($c == $button_bg_color, $borders_color, darken($c, 10%));

  @if $t==normal {
    // normal button
    background-color: $c;
    background-image: none;
    color: $tc;

    @if $flat {
      border-color: $c;
      box-shadow: none;
    } @else {
      border-color: $_border;
      box-shadow: 0 1px transparentize(black, 0.9);
    }
    text-shadow: none;
    icon-shadow: none;
  }

  @else if $t==hover {
    // hovered button
    -gtk-image-effect: highlight;

    $_bg: null;

    @if $c==$headerbar_bg_color { $_bg: lighten($c, 15%); }
    @else if $c==$success_color or $c==$destructive_color or $c==$warning_color or $c==$neutral_color or $c==$purple { $_bg: lighten($c, 5%); }
    @else if $c==$button_bg_color {
      @if $variant=='light' { $_bg: lighten($c, 25%); }
      @else { $_bg: lighten($c, 1%); }
    }

    background-color: $_bg;
    background-image: none;
    color: $tc;

    @if $flat {
      border-color: $_bg;
      box-shadow: none;
    } @else {
      border-color: $_border;
      border-bottom-color: darken($_border, if($c == $headerbar_bg_color or $c == $button_bg_color, 15%, 12%));
      box-shadow: 0 1px transparentize(black, 0.85);
    }
    text-shadow: none;
    icon-shadow: none;
  }

  @else if $t==active {
    $_bg: null;
    $_border_top_color: null;
    $_box_shadow: none;

    @if $c==$headerbar_bg_color {
      $_bg: lighten($c, 10%);
      $_border: $_bg;
    }
    @else if $c==$button_bg_color{
      @if $variant=='light' {
        $_bg: darken($c, 7%);
        $_border: $borders_color;
        $_border_top_color: darken($_border, 25%);
      }
      @else {
        $_bg: darken($button_bg_color,10%);
        $_border: $borders_color;
        $_border_top_color: darken($_border, 8%);
      }
      $_box_shadow: inset 0 1px 1px 0px rgba(0, 0, 0, 0.05);
    }
    @else if $c==$success_color or $c==$destructive_color or $c==$warning_color or $c==$neutral_color or $c==$purple {
      $_bg: darken($c, 10%);
      $_border: darken($c, 15%);
      $_border_top_color: darken($_border, 5%);
      @if $flat {
        $_bg: darken($c, 10%);
        $_border: $_bg;
        $_border_top_color: $_bg;
        $_box_shadow: none;
      }
    }

    box-shadow: $_box_shadow;
    border-color: $_border;
    border-top-color: $_border_top_color;
    background-color: $_bg;
    background-image: none;
    text-shadow: none;
    icon-shadow: none;
  }

  @else if $t==active-hover {
    $_bg: none;

    @if $c == $headerbar_bg_color {
      $_bg: lighten($c, 20%);
      border-color: $_bg;
      box-shadow: none;
    } @else {
      @if $flat == true { $c: darken($c, 5%); }
      $_bg: if(lightness($c)<35%, lighten($c, 18%), darken($c, 7%));
      $_bg: transparentize($_bg, 0.4);
    }

    background-color: $_bg;
    background-image: none;
    @if $variant=='dark' and $c != $headerbar_bg_color { background-color: darken($button_bg_color,9%); }
    text-shadow: none;
    icon-shadow: none;
  }

  @else if $t==insensitive {
    // insensitive button
    $_bg: null;
    @if $c == $button_bg_color {
        $_bg: $bg_color;
    } @else if $c == $headerbar_bg_color {
        $_bg: $c;
    } @else if $c == $red or $green or $yellow or $blue or $purple {
        $_bg: transparentize($c, .1);
    }
      @else {
        $_bg: darken($c, 20%);
    }

    background-color: $_bg;
    background-image: none;

    @if $flat {
      border-color: $_bg;
      box-shadow: none;
    } @else {
      border-color: if($c!=$button_bg_color, $c, $insensitive_borders_color);
      box-shadow: 0 1px transparent;
    }

    .label, & {
      $_label_alpha: if($tc == $headerbar_fg_color, 0.25, 0);
      $_label_color: none;

      @if $c == $success_color {
        $_label_color: $porcelain;
        $_label_alpha: 0.45;
      } @else if $tc == $fg_color {
        $_label_color: $insensitive_fg_color;
        $_label_alpha: 0;
      } @else {
        $_label_color: mix($tc, $_bg, 50%);
        $_label_alpha: 0.25;
      }

      color: transparentize($_label_color, $_label_alpha);
    }
  }

  @else if $t==insensitive-active {
    // insensitive pushed button
    $_from: $c;
    $_to: if($c==$headerbar_bg_color, $headerbar_bg_color, $base_color);
    $_mix: mix($_from, $_to, 85%);

    $_bg: darken($_mix, 8%);
    background-color: $_bg;
    background-image: none;

    @if $flat {
      border-color: $_bg;
      box-shadow: none;
    } @else {
      border-color: $insensitive_borders_color;
      border-top-color: darken($insensitive_borders_color, 10%);
      box-shadow: inset 0 1px 1px 0px rgba(0, 0, 0, if($c==$headerbar_bg_color, .15, 0.05));
    }

    .label, & { color: if($c != $button_bg_color, mix($tc, $_bg, 60%), $insensitive_fg_color); }
  }

  @else if $t == backdrop {
    // backdrop button
    $_bc: if($c != $button_bg_color, _backdrop_color(_border_color($c)), $backdrop_borders_color);

    $_bg: null;
    @if $c == $headerbar_bg_color {
      $_bg: $backdrop_headerbar_bg_color;
    } @else if $c == $button_bg_color {
      $_bg: $button_backdrop_bg_color;
    } @else {
      $_bg: _backdrop_color($c);
    }

    -gtk-image-effect: dim;
    background-color: $_bg;
    background-image: none;
    border-color: if($flat==true, $_bg, $_bc);
    box-shadow: none;

    .label, & { color: if($tc != $fg_color, mix($tc, $_bg, 60%), $backdrop_text_color); }
  }

  @else if $t==backdrop-hover {
    $_bg: null;
    @if $c==$headerbar_bg_color { $_bg: lighten($backdrop_headerbar_bg_color, 6%); }
    @else if $c==$success_color or $c==$destructive_color or $c==$neutral_color or $c==$purple {
      $_c: _backdrop_color($c);
      $_bg: lighten($_c, 5%);
    } @else if $c==$button_bg_color {
      $_bg: if($light, lighten($button_backdrop_bg_color, 25%), lighten($button_backdrop_bg_color, 1%) );
    }

    color: $tc;

    @if $flat {
      background-color: $_bg;
      background-image: none;
      border-color: $_bg;
      box-shadow: none;
    } @else {
      border-color: $_border;
      border-bottom-color: darken($_border, if($c == $headerbar_bg_color or $c == $button_bg_color, 15%, 12%));
      box-shadow: 0 1px transparentize(black, 0.85);
    }
  }
  @else if $t==backdrop-active {
    // backdrop pushed button

    $_bg: null;
    $_bc: if($c != $button_bg_color, _backdrop_color(_border_color($c)), $backdrop_borders_color);

    @if $c==$headerbar_bg_color {
      $_bg: lighten($backdrop_headerbar_bg_color, 5%);
    } @else if $c!=$headerbar_bg_color and $variant=='light'{
      $_bg: darken($c, 12%);
    }
    @else if $c!=$headerbar_bg_color and $variant=='dark'{
      $_bg: darken($c, 7%);
    }

    $_border_top_color: null;
    @if $c==$headerbar_bg_color { $_border_top_color: $_bg;}
    @else if $c==$button_bg_color {
      @if $variant=='light' { $_border_top_color: darken($_border,10%);}
      @else { $_border_top_color: darken($_border,2%);}
    }

    background-color: $_bg;
    background-image: none;
    border-color: if($flat==true, $_bg, $_bc);
    border-top-color: $_border_top_color;
    box-shadow: none;

    .label, & { color: if($tc != $fg_color, mix($tc, $_bg, 80%), $backdrop_fg_color); }
  }

  @else if $t==backdrop-insensitive {
    // backdrop insensitive button

    $_bc: if($c != $button_bg_color, _backdrop_color(_border_color($c)), transparentize($backdrop_borders_color, 0.3));
    $_bg: null;
    @if $c == $button_bg_color {
        $_bg: $backdrop_bg_color;
    } @else if $c == $headerbar_bg_color {
        $_bg: transparent;
        background-image: none;
        text-shadow: none;
        icon-shadow: none;
    }
    @else if $c == $red or $green or $yellow or $blue or $purple {
        $_bg: transparentize($c, .1);
    }
    @else {
        $_bg: darken($c, 20%);
    }

    background-color: $_bg;
    background-image: none;
    border-color: if($flat==true, $_bg, $_bc);
    box-shadow: none;

    .label, & { color: if($c != $button_bg_color, mix($tc, $_bg, 35%), transparentize($backdrop_text_color, 0.3)); }
  }

  @else if $t==backdrop-insensitive-active {
    // backdrop insensitive pushed button
    $_bg: null;
    $_bc: if($c != $button_bg_color, _backdrop_color(_border_color($c)), $backdrop_borders_color);
    @if $c == $headerbar_bg_color {
      $_bg: mix($c, $headerbar_bg_color, 85%);
    }
    @else if $c!=$headerbar_bg_color and $variant=='light'{
      $_bg: darken($c, 15%);
    }
    @else if $c!=$headerbar_bg_color and $variant=='dark'{
      $_bg: darken($c, 5%);
    }

    background-color: $_bg;
    background-image: none;
    border-color: if($flat==true, $_bg, $_bc);
    $_border_top_color: null;
    @if $c==$headerbar_bg_color { $_border_top_color: darken($_border,15%);}
    @else if $c==$button_bg_color {
      @if $variant=='light' { $_border_top_color: darken($_border,15%);}
      @else { $_border_top_color: darken($_border,1%);}
    }
    border-top-color: $_border_top_color;

    .label { color: if($c != $button_bg_color, mix($tc, $_bg, 35%), transparentize($backdrop_text_color, 0.5)); }
  }

  @else if $t==osd {
  //
  // normal osd button
  //
    $_bg: if($c != $button_bg_color, transparentize($c, 0.5), $osd_button_bg_color);

    color: $osd_fg_color;
    border-color: _border_color($_bg);
    background-color: $_bg;
    background-image: none;
    background-clip: padding-box;
    box-shadow: inset 0 -1px 1px _button_hilight_color($_bg);
  }

  @else if $t==osd-hover {
  //
  // active osd button
  //
    $_bg: if($c != $button_bg_color, transparentize($c, 0.3), lighten($osd_button_bg_color, 12%));

    color: white;
    border-color: _border_color($_bg);
    background-color: $_bg;
    background-image: none;
    background-clip: padding-box;
    box-shadow: inset 0 -1px 1px _button_hilight_color($_bg);
  }

  @else if $t==osd-active {
  //
  // active osd button
  //
    $_bg: if($c != $button_bg_color, $c, $osd_button_bg_color);

    color: white;
    border-color: _border_color($_bg);
    background-color: $_bg;
    background-image: none;
    background-clip: padding-box;
    box-shadow: inset 0 2px 3px -1px $_pressed;
  }

  @else if $t==osd-insensitive {
  //
  // insensitive osd button
  //
    color: $osd_insensitive_fg_color;
    background-color: $osd_insensitive_bg_color;
    background-image: none;
    border-color: _border_color($osd_insensitive_bg_color);
    background-clip: padding-box;
    box-shadow: none;
  }

  @else if $t==osd-backdrop {
  //
  // backdrop osd button
  //
    $_bg: if($c != $button_bg_color, transparentize($c, 0.5), $osd_bg_color);

    color: $osd_fg_color;
    border-color: _border_color($_bg);
    background-color: $_bg;
    background-image: none;
    background-clip: padding-box;
    box-shadow: none;
  }

  @else if $t==undecorated {
  //
  // reset
  //
    border-color: transparent;
    background-color: transparent;
    background-image: none;
    box-shadow: none;
    text-shadow: none;
    icon-shadow: none;
  }
}

/***************************
 * Check and Radio buttons *
 ***************************/

@mixin check($t, $c:white, $tc:white) {
  // Check/Radio drawing function
  //
  // $t:    check/radio type,
  // $c:    base button color for colored* types
  // $tc:   optional text color for colored* types
  //
  // possible $t values:
  // normal, hover, active, insensitive, backdrop, backdrop-insensitive

  $_checked: if($c==$success_color, true, false);
  $_border_color: if($_checked, $c, if($light, $borders_color, white));
  $_dim_border_color: transparentize($_border_color, if($light, 0.3, 0.7));
  $_dim_background: if($light, $dark_fill, $slate);

  @if $t==normal  {
    background-color: if($_checked, $c, if($variant=='light', $c, transparent));
    border-color: $_border_color;
    box-shadow: 0 1px transparentize(black, 0.95);
    color: $tc;
  }

  @if $t==hover {
    -gtk-image-effect: highlight;
    background-color: if($c==white, darken($c, 5%), lighten($c, 5%));
  }

  @if $t==active {
    box-shadow: inset 0 1px 1px 0px if($light, rgba(0, 0, 0, 0.2), black);
  }

  @if $t==insensitive {
    $_bg: if($_checked, $_dim_background , transparent);
    background-color: $_bg;
    border-color: if($_checked, $_bg, $_dim_border_color);
    box-shadow: none;
    color: transparentize($tc, 0.3);
  }

  @if $t==backdrop {
    -gtk-image-effect: dim;

    background-color: if($_checked, _backdrop_color($c), $bg_color);
    border-color: $_dim_border_color;
    box-shadow: 0 1px transparentize(black, 0.95);
    color: $tc;
  }

  @if $t==backdrop-hover {
    -gtk-image-effect: highlight;
    background-color: if($c!=white, lighten($c, 9%), darken($c, 7%));
  }

  @if $t==backdrop-insensitive {
    -gtk-image-effect: dim;

    $_bg: if($_checked, $_dim_background, transparent);
    background-color: $_bg;
    border-color: if($_checked, $_bg, $_dim_border_color);
    box-shadow: none;
    color: transparentize($tc, 0.6);
  }
}

/************
 * Switches *
 ************/
@mixin switch($bg:$dark_fill, $alt:$success_color) {
  -GtkSwitch-slider-width: 18px;
  -GtkSwitch-slider-height: 18px;

  font-weight: normal;
  font-size: 0;
  outline-color: $focus_color;
  outline-offset: -2px;
  outline-radius: $small_radius;
  box-shadow: inset 0 1px 1px 0 if($bg==$dark_fill, transparentize($bg, 0.2), rgba(0, 0, 0, 0.15));

  $_insensitive_border_color: null;
  @if $bg == $dark_fill {
    $_insensitive_border_color: if($variant=='light', $borders_edge, lighten($inkstone, 5%));
  } @else {
    $_insensitive_border_color: $borders_edge;
  }

  &.trough {
    // similar to the .scale
    border: 1px solid darken($bg, 5%);
    border-top-color: darken($bg, 20%);
    border-radius: 5px;
    color: transparent;
    background-color: $bg;
    background-image: none;
    text-shadow: none;
    &:hover {
      -gtk-image-effect: highlight;
      background-color: lighten($bg, 5%);
    }
    &:active {
      background-color: $alt;
      background-image: none;
      color: transparent;
      border-color: darken($alt, 5%);
      border-top-color: darken($alt, 20%);
      box-shadow: inset 0 1px 1px 0 transparentize($alt, 0.2);
      text-shadow: none;
      &:hover {
        -gtk-image-effect: highlight;
        background-color: lighten($alt, 5%);
      }
    }
    &:insensitive {
      color: transparent;
      border-color: $_insensitive_border_color;
      background-image: none;
      background-color: if($bg==$dark_fill, transparent, $ash);
      box-shadow: none;
      text-shadow: none;
      &:active {
        background-color: transparentize($dark_fill, if($variant=='light', 0.6, 0.9));
      }
    }
    &:backdrop {
      color: transparent;
      border-color: if($bg==$dark_fill, if($variant=='light', $borders_edge, $bg), transparent);
      border-top-color: darken($bg, if($variant=='light', 10%, 5%));
      @if $bg!=$dark_fill {
        background-color: _backdrop_color(transparentize($bg, 0.3));
      }
      @else {
        @if $variant='light' {
          background-color: _backdrop_color($bg);
        }
        @else {
          background-color: lighten($bg, 2%);
        }
      }
      background-image: none;
      box-shadow: none;
      text-shadow: none;
      transition: $backdrop_transition;
      &:hover {
        background-color: $bg;

        &:active {
          background-color: $alt;
        }
      }
      &:active {
        color: transparent;
        border-color: darken($alt, 5%);
        border-top-color: darken($alt, 10%);
        background-color: _backdrop_color($alt);
        background-image: none;
        box-shadow: none;
        &:insensitive {
          $_bg: transparentize($dark_fill, if($variant=='light', 0.7, 0.9));
          background-color: if($bg==$dark_fill, $_bg, _backdrop_color($ash));;
        }
      }
      &:insensitive {
        color: transparent;
        border-color: if($bg==$dark_fill, $_insensitive_border_color, transparent);
        background-image: none;
        background-color: if($bg==$dark_fill, transparent, _backdrop_color($ash));
        box-shadow: none;
      }
    }
  }
  &.slider {
    &, &:hover, &:active, &:insensitive, &:backdrop, &:backdrop:insensitive {
      background-color: if($bg==$dark_fill, white, $headerbar_fg_color);
      background-image: none;
      background-clip: if($variant == "light", border-box, padding-box);
      border-radius: $small_radius;
      border: 1px solid transparent;
      box-shadow: 0 1px 1px if($bg==$dark_fill, darken($bg, 7%), rgba(0, 0, 0, 0.15));
    }
    padding: 3px;
    transition: $button_transition;
    &:active {
      box-shadow: 0 1px 1px darken($alt, 7%);
    }
    &:insensitive {
      background-color: if($bg==$dark_fill, transparent, $ash);
      border: 1px solid if($bg==$dark_fill, $_insensitive_border_color, $slate);;
      box-shadow: none;
    }
    &:backdrop {
      transition: $backdrop_transition;
      @if $bg==$dark_fill {
        @if $variant=='light' {
          background-color: $backdrop_bg_color;
        } @else {
          background-color: lighten($backdrop_fg_color, 20%);
        }
      } @else {
        background-color: $headerbar_fg_color;
      }
      &:active{
        border-color: transparent;
      }
      &:insensitive {
        background-color: if($bg==$dark_fill, transparent, _backdrop_color($ash));
        transition: $backdrop_transition;
      }
    }
  }
  .list-row:selected & {
    @if $variant == 'light' {
      box-shadow: none;
      border-color: $selected_borders_color;
      &:backdrop { border-color: $selected_borders_color; }
      &.slider:dir(rtl) { border-left-color: $borders_color; }
      &.slider:dir(ltr) { border-right-color: $borders_color; }
      &.slider,
      &.slider:active { border-color: $selected_borders_color; }
    }
  }
}

@mixin progressbar_fill($d:horizontal) {
  $dir: if($d==vertical,right,bottom);
  background-image: linear-gradient(to $dir, $selected_bg_color 2px,
                                             lighten($selected_bg_color,6%));
}

@mixin overshoot($p, $t:normal, $c:$fg_color) {
//
// overshoot
//
// $p: position
// $t: type
// $c: base color
//
// possible $p values:
// top, bottom, right, left
//
// possible $t values:
// normal, backdrop
//

  $_small_gradient_length: 5%;
  $_big_gradient_length: 100%;

  $_position: center top;
  $_small_gradient_size: 100% $_small_gradient_length;
  $_big_gradient_size: 100% $_big_gradient_length;

  @if $p==bottom {
    $_position: center bottom;
    $_linear_gradient_direction: to top;
  }

  @else if $p==right {
    $_position: right center;
    $_small_gradient_size: $_small_gradient_length 100%;
    $_big_gradient_size: $_big_gradient_length 100%;
  }

  @else if $p==left {
    $_position: left center;
    $_small_gradient_size: $_small_gradient_length 100%;
    $_big_gradient_size: $_big_gradient_length 100%;
  }

  $_small_gradient_color: $c;
  $_big_gradient_color: $c;

  @if $c==$fg_color {
    $_small_gradient_color: darken($borders_color, 10%);
    $_big_gradient_color: $fg_color;

    @if $t==backdrop { $_small_gradient_color: $backdrop_borders_color; }
  }

  $_small_gradient: -gtk-gradient(radial,
                                  $_position, 0,
                                  $_position, 0.5,
                                  to($_small_gradient_color),
                                  to(transparentize($_small_gradient_color, 1)));

  $_big_gradient: -gtk-gradient(radial,
                                $_position, 0,
                                $_position, 0.6,
                                from(transparentize($_big_gradient_color, 0.93)),
                                to(transparentize($_big_gradient_color, 1)));

  @if $t==normal {
    background-image: $_small_gradient, $_big_gradient;
    background-size: $_small_gradient_size, $_big_gradient_size;
  }

  @else if $t==backdrop {
    background-image: $_small_gradient;
    background-size: $_small_gradient_size;
  }

  background-repeat: no-repeat;
  background-position: $_position;

  background-color: transparent; // reset some properties to be sure to not inherit them somehow
  border: none;                  //
  box-shadow: none;              //
}

@mixin undershoot($p) {
//
// undershoot
//
// $p: position
//
// possible $p values:
// top, bottom, right, left
//

  $_undershoot_color_dark: transparentize(black, 0.8);
  $_undershoot_color_light: transparentize(white, 0.8);

  $_gradient_dir: left;
  $_dash_bg_size: 10px 1px;
  $_gradient_repeat: repeat-x;
  $_bg_pos: center $p;

  background-color: transparent; // shouldn't be needed, but better to be sure;

  @if ($p == left) or ($p == right) {
    $_gradient_dir: top;
    $_dash_bg_size: 1px 10px;
    $_gradient_repeat: repeat-y;
    $_bg_pos: $p center;
  }

  background-image: linear-gradient(to $_gradient_dir, // this is the dashed line
                                    $_undershoot_color_light 50%,
                                    $_undershoot_color_dark 50%);

  padding-#{$p}: 1px;
  background-size: $_dash_bg_size;
  background-repeat: $_gradient_repeat;
  background-origin: content-box;
  background-position: $_bg_pos;
}

@mixin draw_circle($c, $close:false, $size:24px) {
    $button_size: $size + 2px * 2;
    $circle_size: 20px / $button_size / 2;

    background-image: -gtk-gradient(radial,
                                    center center, 0,
                                    center center, $circle_size,
                                    to($c),
                                    to(transparent));

    &:hover {
        $_bg: if(lightness($c)<35%, lighten($c, 25%), lighten($c, 5%));
        background-image: -gtk-gradient(radial,
                                        center center, 0,
                                        center center, $circle_size,
                                        to($_bg),
                                        to(transparent));
        &:backdrop {
            $_bg: lighten($backdrop_headerbar_bg_color, 8%);
            background-image: -gtk-gradient(radial,
                                        center center, 0,
                                        center center, $circle_size,
                                        to($_bg),
                                        to(transparent));
        }
    }

    &:active {
        $_bg: if(lightness($c)<35%, lighten($c, 18%), darken($c, 7%));
        background-image: -gtk-gradient(radial,
                                        center center, 0,
                                        center center, $circle_size,
                                        to($_bg),
                                        to(transparent));
    }

    @if $close == true {
        &:backdrop {
            color: $backdrop_headerbar_fg_color;
        }
    }
}
