$_base_hover_color: transparentize($fg_color, 0.85);
$_base_active_color: transparentize($fg_color, 0.75);
$_titlebutton_height: 24px;

@mixin draw_circle($c, $size:$_titlebutton_height) {
  $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));
}

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

  $_us_shadow_color: if($variant=='light', $silk, $jet);
  $_us_shadow_alpha: 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;
  }

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

  @if ($p == left) {
    box-shadow: inset 2px 0 0 transparentize($_us_shadow_color, $_us_shadow_alpha),
                inset 1px 0 0 transparentize(darken($_us_shadow_color, 20%), $_us_shadow_alpha);
  }
  @else if ($p == right) {
    box-shadow: inset -2px 0 0 transparentize($_us_shadow_color, $_us_shadow_alpha),
                inset -1px 0 0 transparentize(darken($_us_shadow_color, 20%), $_us_shadow_alpha);
  }
  @else if ($p == top) {
    box-shadow: inset 0 2px 0 0 transparentize($_us_shadow_color, $_us_shadow_alpha),
                inset 0 1px 0 0 transparentize(darken($_us_shadow_color, 20%), $_us_shadow_alpha);
  }
  @else if ($p == bottom) {
      box-shadow: inset 0 -2px 0 0 transparentize($_us_shadow_color, $_us_shadow_alpha),
                  inset 0 -1px 0 0 transparentize(darken($_us_shadow_color, 20%), $_us_shadow_alpha);
  }
}

// with a flatter headerbar and buttons, we dont need that heavy shadows, remove this when upstream agrees
headerbar *, button * {
  text-shadow: none;
  -gtk-icon-shadow: none;
}

// blue spinner, white spinner for colored buttons
spinner {
  &:not(:backdrop) {
    color: $blue;
  }
  button.suggested-action &, button.destructive-action & {
    color: $selected_fg_color;
    &:backdrop { color: $backdrop_selected_fg_color; }
  }
}

// titlebutton
button.titlebutton:not(.appmenu) {

  &,
  &:hover,
  &:active {

    headerbar &,
    .titlebar &,
    headerbar.selection-mode & {

      // spec bump otherwise :backdrop  will still have a bg
      &,
      &:backdrop {
        @extend %undecorated_button;
      }
    }
  }

  // Important - otherwise the circle size is wrong
  min-height: $_titlebutton_height;
  min-width: $_titlebutton_height;
  padding: 2px;

  headerbar &,
  .titlebar &,
  headerbar.selection-mode &,
  & {
    &.maximize, &.minimize {

      &, &:backdrop {
        &:hover {
          @include draw_circle(if($ambiance==true, lighten($inkstone, 7%), $_base_hover_color));
        }

        &:active {
          @include draw_circle(if($ambiance==true, lighten($inkstone, 12%), $_base_active_color));
        }
      }
    }

    &.close {
      color: $selected_fg_color;
      @include draw_circle($selected_bg_color);

      &:hover {
        @include draw_circle($selected_bg_color);
        @include draw_circle(lighten($selected_bg_color, 5%));
      }

      &:active {
        @include draw_circle($selected_bg_color);
        @include draw_circle(darken($selected_bg_color, 5%));
      }

      &:backdrop {
        @include draw_circle(lighten($inkstone, if($variant=='light', if($ambiance==true, 15%, 40%), 10%)));
        &:hover { @include draw_circle(lighten($inkstone, if($variant=='light', if($ambiance==true, 20%, 45%), 15%))); }
      }
    }
  }
}

// we prefered our gray hover for menus and popovers, orange is a very strong color
menu,
.menu,
.context-menu {
  menuitem {

    &:hover {
      color: $fg_color;
      background-color: $popover_hover_color;
    }
  }
}

// We don't want menuitems to look like links, we should propose this upstream
menubar,
.menubar {
  > menuitem {
    &:hover {
      color: $fg_color;
      background-color: $popover_hover_color;
    }
  }
}

// We don't want the app notifications to look like OSD because they are interactive
.app-notification,
.app-notification.frame {
  padding: 10px;
  margin: 12px;
  color: $text_color;
  border-radius: $menu_radius;
  background: none;
  background-color: $menu_color;
  border: 1px solid $borders_color;
  box-shadow: 0 2px 5px transparentize(black, 0.8);
  text-shadow: none;

  button {
    &, &:dir(ltr), &:dir(rtl) {
      &.text-button, &.image-button, & {
        @include button(normal);
        &.flat { @include button(undecorated); }
        &:hover { @include button(hover); }
        &:active { @include button(active); }
        &:backdrop {
          @include button(backdrop);
          &.flat { @include button(undecorated); }
        }
        &:disabled { @include button(insensitive); }
      }
    }
  }

  &:backdrop {
    background-color: $backdrop_bg_color;
    color: $backdrop_text_color;
    box-shadow: none;
    transition: $backdrop_transition;
  }

  border { border: none; }
}

// 2 pixel transparentized focus rings
// upstream is also interested in this

*:not(button) {
  outline-color: $focus_border_color;
  outline-style: solid;
  outline-offset: -2px;
  outline-width: 2px;
  -gtk-outline-radius: $button-radius;
  :selected & { outline-color: $coloured_focus_border_color; }
}

button {
  outline-color: $focus_border_color;
  outline-style: solid;
  outline-offset: -2px;
  outline-width: 2px;
  -gtk-outline-radius: $button-radius;
  &.suggested-action, &.destructive-action{ &, &:hover, &:active { outline-color: $coloured_focus_border_color; } }
}

switch {
  &, &:hover { slider { outline-color: transparent; } }
  &:focus {
    box-shadow: 0 0 0 3px if($variant=='light', lighten(opacify($focus_border_color, 1), 20%), $focus_border_color);
  }
}

checkbutton,
radiobutton {
  outline-color: $focus_border_color;
  outline-offset: 1px;
  outline-width: 2px;
  row:selected & , treeview:selected & { outline-color: $coloured_focus_border_color; }
}

treeview {
  outline-color: $focus_border_color;
  -gtk-outline-radius: 0px;

  &:selected {
    outline-color: $coloured_focus_border_color;
  }
}

notebook > header {
  &, &:backdrop { background: transparent; }
  tabs {
    &, &:hover, &:backdrop {
      &, &.right, &.left, &.top, &.bottom {
        background-color: transparent;
        tab {
          outline-width: 1px;
          outline-offset: -2px;
          &, &:dir(ltr), &:dir(rtl) {
            &, &:backdrop {
              &, &:checked {
                &, &:hover, &:active {
                  background-color: transparent;
                  border-color: transparent;
                }
              }
              &:hover:not(:checked):not(:backdrop) { color: $insensitive_fg_color; }
            }
          }
        }
      }
    }
  }
}

// With the conservative tab styling scrolled windows need the undershoots

scrolledwindow {
  undershoot {
    &.top { @include undershoot(top); }

    &.bottom { @include undershoot(bottom); }

    &.left { @include undershoot(left); }

    &.right { @include undershoot(right); }
  }
}

// Check/radios in menus do not need a border

check,
radio {
  menu menuitem & {
    &, &:checked, &:indeterminate {
      &, &:hover, &:disabled {
        border-color: transparent;
      }
    }
  }
}
// Fixes selected disabled check/radios leaking selected_bg_color, should move to upstream
treeview.view check,
treeview.view radio {
    &:selected {
      &:focus, & {
        &:disabled { color: $insensitive_fg_color; }
    }
  }
}

// Reducing the amount of the palette's background colors to two

.sidebar { background-color: $bg_color; }

// Treeview items misses a hover, port to upstream

treeview:hover { background: $popover_hover_color; }

scale {
  slider {
    .osd & {
      @include button(osd);
      background-image: image(darken($osd_fg_color, 2%));

      &:hover {
        background-image: image(darken($osd_fg_color, 10%));
      }

      &:active {
        background-image: image(darken($osd_fg_color, 25%));
      }
    }
  }
}

// Rubberband selection shall match with shell's tile-preview
.rubberband,
rubberband {
  background-color: transparentize($ash, 0.8);
  border: 1px solid if($variant == 'light', transparentize($silk, 0.2), transparentize(darken($silk, 10%), 0.4));
}
// Fix for color chose buttons, should be moved to upstream as it is variable
// If the min-height is not set, it stretches compact headerbars
button.color { min-height: $_headerbar_height / 2; }

// Entries drown if drawn on widgets with $base_color
// Fixing this at least for notebooks, since entries on tabs is a common pattern
@if $variant=='dark' {
  notebook entry { background-color: darken($base_color, 2%); }
}

// Make all :not:(.empty) level bars green - should be moved to upstream
levelbar {
  block {
    &.high,
    &:not(.empty) {
      border-color: if($variant == 'light', darken($success_color, 20%), $success_color);
      background-color: $success_color;
      &:backdrop { border-color: $success_color; }
    }
  }
}

// Strengthen the menu shadows to increase visibility
decoration {
  $_wm_border: if($variant=='light', transparentize(black, 0.77), transparentize($borders_color, 0.1));
  .csd.popup & {
    border-radius: $menu_radius;
    box-shadow: 0 1px 2px 1px transparentize(black, 0.7),
                0 0 0 1px $_wm_border;
  }
}

// Fixes for hdy leaflet window and headerbar styling in gtk 3.38+
// Need to investigate how upstream does this without changing _common.scss
window.background.csd.unified {
  &, deck box headerbar { border-radius: $window_radius $window_radius 0 0; }
  decoration { border-radius: $window_radius; }

  headerbar button.radio {
    $_button_bg: if($ambiance, $headerbar_bg_color, darken($bg_color, if($variant=='light', 10%, 7%)));
    background: $_button_bg;
    &:hover {
      background-color: lighten($_button_bg, 4%);
    }
    &:active {
      background-color: darken($_button_bg, 4%);
    }
    &:checked {
      background-color: darken($_button_bg, if($variant=='light', 10%, 5%));
    }
    &:backdrop {
      $_backdrop_button_bg: if($ambiance, $backdrop_headerbar_bg_color,$backdrop_bg_color);
      background-color: $_backdrop_button_bg;
      &:checked {
        background-color: darken($_backdrop_button_bg, 7%);
      }
    }
    min-height: $_headerbar_height; 
    padding-top: 0;
    padding-bottom: 0;
    margin-top: 0;
    margin-bottom: 0;
    border-radius: 0;
    border-left-width: 0;
    border-right-width: 0;
    border-bottom-width: 0;
    border-top-color: if($ambiance, $headerbar_border_color, $top_hilight);
  }
  headerbar button box {
    &.horizontal.wide {
      margin-right: $_headerbar_height / 2.5;
      margin-left: $_headerbar_height / 2.5;
    }
    &.vertical.narrow label {
      margin-left: 10px;
      margin-right: 10px;
    }
  }

  viewswitcherbar {
    box {
      min-height: 0;
      padding-top: 0;
      padding-bottom: 0;
      margin-top: 0;
      margin-bottom: 0;
    }
    
    actionbar {
      button.radio {
        border-radius: 0;
        min-height: $_headerbar_height; 
        padding-top: 0;
        padding-bottom: 0;
        margin-top: 0;
        margin-bottom: 0;
        border-radius: 0;
        border-width: 0;
        border: none;

        $_button_bg: $bg_color;
        background: $_button_bg;
        &:hover {
          background-color: lighten($_button_bg, 4%);
        }
        &:active {
          background-color: darken($_button_bg, 7%);
        }
        &:checked {
          background-color: darken($_button_bg, 9%);
        }
        &:backdrop {
          $_backdrop_button_bg: $backdrop_bg_color;
          background-color: $_backdrop_button_bg;
          &:checked {
            background-color: darken($_backdrop_button_bg, 9%);
          }
        }
      }
    }
  }
}

// Reduce hyper prominent dark theme check switch border in backdrop
switch {
  &:backdrop {
    &:checked {
      &, slider { border-color: if($variant=='light', $checkradio_borders_color, $backdrop_borders_color); }
    }
  }
}