@mixin headerbar_buttons($bg, $fg, $br)
{
  border-color: gtkshade($br, 1.05);
  border-radius: $button_radius;
  color: $fg;
  background-color: gtkshade($bg, 1.05);

  &:hover {
    border-color: gtkshade($br, 1.05);
    color: $fg;
    background-color: gtkshade($bg, 1.05);
  }

  &:active, &:checked {
    border-color: gtkshade($selected_bg_color, 1.15);
    color: $fg_color;
    background-color: gtkshade($selected_bg_color, 1.15);
    
    &:backdrop {
      border-color: $unfocused_selected_bg_color;
      color: $unfocused_selected_fg_color;
      background-color: $unfocused_selected_bg_color;
    }
  }

  &:disabled {
    border-color: gtkshade($br, 1.3);
    color: gtkalpha($fg, 0.7);
    background-color: transparent;
    
    &:backdrop {
      border-color: gtkshade($br, 1.45);
      color: $disabled_fg_color;
      background-color: transparent;
    }
  }

  &:backdrop {
    border-color: gtkshade($br, 1.2);
    color: gtkmix($fg, $unfocused_fg_color, 0.2);
    background-color: transparent;
  }
}

@mixin headerbar() {
  headerbar {
    padding: 4px 8px;
    min-height: 30px;
    color: $titlebar_fg_color;
    background-color: $titlebar_bg_color;
    border-bottom: 1px solid gtkshade($titlebar_bg_color, 0.85);

    &:backdrop {
      color: gtkalpha($titlebar_fg_color, 0.8);
      background-color: gtkmix($titlebar_bg_color, $unfocused_bg_color, 0.1);
      border-bottom-color: gtkshade($titlebar_bg_color, 0.95);
    }

    .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.6));
    }
    
    switch:checked {
      border-color: gtkshade($selected_bg_color, 1.15);
      background-color: gtkshade($selected_bg_color, 1.15);  
    }

    entry { padding: 4px 8px; }
    &.titlebar, .titlebar & { border-radius: $csd_radius $csd_radius 0px 0px; }
    
    .titlebutton:not(separator) {
      padding: 0;
      min-width: 16px;
      min-height: 16px;
      border: none;
      border-radius: 20px;
      -gtk-icon-shadow: none;
      
      &.close {
        color: $selected_fg_color;
        background-color: $selected_bg_color;
        
        &:hover { background-color: gtkshade($selected_bg_color, 1.05); }
        &:active { background-color: gtkshade($selected_bg_color, 0.85); }
      }
      
      &.maximize, &.minimize {
        background-color: gtkmix($titlebar_bg_color, $titlebar_fg_color, 0.3);
        
        &:hover { background-color: gtkmix($titlebar_bg_color, $titlebar_fg_color, 0.2); }
        &:active { background-color: gtkmix($titlebar_bg_color, $titlebar_fg_color, 0.4); }
      }
      
      &:backdrop {
        color: gtkalpha($titlebar_fg_color, 0.8);
        background-color: gtkmix($titlebar_bg_color, $titlebar_fg_color, 0.15);
      }
    }
  }
}
