@mixin headerbar_buttons($bg, $fg, $br)
{
  padding: 4px 8px;
  border-style: solid;
  border-width: 1px;
  border-radius: $button_radius;
  color: $fg;
  border-color: $br;
  background-color: $bg;
  background-image: linear-gradient(to bottom, gtkshade($bg, 1.05), gtkshade($bg, 0.81) 50%, gtkshade($bg, 0.77) 51%, gtkshade($bg, 0.72));
  box-shadow: 0 1px gtkalpha($light_shadow, 0.1);

  &:hover {
  }

  &:active, &:checked {
    color: $selected_fg_color;
    border-color: $br;
    background-color: gtkshade($bg, 0.5);
    background-image: none;
    box-shadow: inset 0 2px 3px 1px gtkalpha($dark_shadow, 0.45), inset 0 1px gtkshade($bg_color, 0.35);

    &:backdrop {
      color: $unfocused_fg_color;
      border-color: gtkshade($unfocused_bg_color, 0.85);
      background-color: gtkshade($unfocused_bg_color, 0.85);
      box-shadow: inset 0 2px 3px 1px gtkalpha($dark_shadow, 0.25), inset 0 1px gtkshade($bg_color, 0.55);
    }
    
    &:disabled {
      color: gtkmix($fg, $unfocused_disabled_fg_color, 0.1);
      border-color: gtkmix($fg, $disabled_bg_color, 0.85);
      background-color: gtkshade($disabled_bg_color, 0.9);
      
      &:backdrop {
        color: $unfocused_disabled_fg_color;
        border-color: $unfocused_borders;
        background-color: gtkshade($unfocused_disabled_bg_color, 0.9);
      }
    }
  }

  &:disabled {
    color: gtkmix($fg, $unfocused_disabled_fg_color, 0.5);
    
    &:backdrop {
      color: $unfocused_disabled_fg_color;
    }
  }

  &:backdrop {
    color: $unfocused_fg_color;
    border-color: $unfocused_borders;
    background-image: linear-gradient(to bottom,  gtkshade($unfocused_bg_color, 1.1), gtkshade($unfocused_bg_color, 1.03) 50%, gtkshade($unfocused_base_color, 0.99) 51%, gtkshade($unfocused_base_color, 0.95));
    background-color: $unfocused_base_color;
  }
}

@mixin headerbar() {
  headerbar {
    padding: 4px 8px;
    min-height: 30px;
    color: $titlebar_fg_color;
    background-color: $titlebar_bg_color;
    background-image: linear-gradient(to bottom, gtkshade($titlebar_bg_color, 1.25), gtkshade($titlebar_bg_color, 1.01) 50%, gtkshade($titlebar_bg_color, 0.97) 51%, gtkshade($titlebar_bg_color, 0.92));
    border-bottom: 1px solid gtkshade($titlebar_bg_color, 0.6);

    &:backdrop {
      color: $unfocused_fg_color;
      background-image: linear-gradient(to bottom, gtkshade($unfocused_bg_color, 1.25), gtkshade($unfocused_bg_color, 1.01) 50%, gtkshade($unfocused_bg_color, 0.99) 51%, gtkshade($unfocused_bg_color, 0.95));
      background-color: $unfocused_bg_color;
      border-color: gtkshade($unfocused_bg_color, 0.7);
    }

    .tiled &, .tiled &:backdrop,
    .maximized &, .maximized &:backdrop {
      border-radius: 0;
    }
    
    // Fixes split headerbars
    separator:first-child + &,
    &:first-child {
      &, &:backdrop {
        border-top-left-radius: $csd_radius;

        .maximized &,
        .tiled & { border-radius: 0; }
      }
    }

    &:last-child {
      &, &:backdrop {
        border-top-right-radius: $csd_radius;

        .maximized &,
        .tiled & { border-radius: 0; }
      }
    }

    .title {
      padding-left: 12px;
      padding-right: 12px;
      font-weight: bold;
    }

    .subtitle {
      font-size: smaller;
      padding-left: 12px;
      padding-right: 12px;
      color: gtkalpha(currentColor, 0.7);
    }

    button {
      @include headerbar_buttons($headerbar_bg_color, $headerbar_fg_color, gtkshade($headerbar_bg_color, 0.5));
    }

    switch { border: 1px solid gtkshade($headerbar_bg_color, 0.8);
      &:checked { border-color: gtkshade($headerbar_bg_color, 0.8); }
    }

    entry { padding: 4px 8px; }
    &.titlebar, .titlebar & { border-radius: $csd_radius $csd_radius 0px 0px; }
    
    .titlebutton {
      &.close, &.maximize, &.minimize, &:not(separator) {
        padding: 2px 2px;
        border: none;
        color: transparent;
        box-shadow: none;
        background-color: transparent;
        -gtk-icon-shadow: none;
        
        background-position: center;
        background-repeat: no-repeat;
      }
      
      &.close {
        background: url('assets/close-normal.png') 100% 100%;

        &:hover { background: url('assets/close-hover.png') 100% 100%; }
        &:active { background: url('assets/close-active.png') 100% 100%; }
        &:backdrop { background: url('assets/close-backdrop.png') 100% 100%; }
      }

      &.minimize {
        background: url('assets/minimize-normal.png') 100% 100%;

        &:hover { background: url('assets/minimize-hover.png') 100% 100%; }
        &:active { background: url('assets/minimize-active.png') 100% 100%; }
        &:backdrop { background: url('assets/minimize-backdrop.png') 100% 100%; }
      }

      &.maximize {
        background: url('assets/maximize-normal.png') 100% 100%;

        &:hover { background: url('assets/maximize-hover.png') 100% 100%; }
        &:active { background: url('assets/maximize-active.png') 100% 100%; }
        &:backdrop { background: url('assets/maximize-backdrop.png') 100% 100%; }
      }
    }
  }
  
  window.ssd .titlebar {
    border-bottom: none;
    min-height: 20px;
  }
}
