/*
 * Copyright (C) 2015-2018 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

.tab-bar {
    position: absolute;
    top: var(--undocked-title-area-height);
    left: 0;
    right: 0;
    height: var(--tab-bar-height);

    display: flex;

    --tab-item-dark-border-color: hsl(0, 0%, 59%);
    --tab-item-medium-border-color: hsl(0, 0%, 65%);
    --tab-item-light-border-color: hsl(0, 0%, 85%);

    --tab-item-medium-border-style: 1px solid var(--tab-item-medium-border-color);

    --tab-bar-border-z-index: 10;
}

body.big-sur .tab-bar {
    --tab-item-light-border-color: hsl(0, 0%, 85%);
    --tab-item-medium-border-color: hsl(0, 0%, 85%);
    --tab-item-dark-border-color: hsl(0, 0%, 75%);
}

body:not(.docked) .tab-bar {
    background: var(--tab-bar-background);
}

body.big-sur:not(.docked) .tab-bar {
    --tab-bar-background: hsl(0, 0%, 90%);
}

body:not(.big-sur):not(.docked) .tab-bar {
    background-size: 100% 200%;
    --tab-bar-background: linear-gradient(to bottom, hsl(0, 0%, 78%), hsl(0, 0%, 72%));
}

body.big-sur:not(.docked).window-inactive .tab-bar,
body:not(.big-sur):not(.docked).window-inactive .tab-bar {
    --tab-bar-background: hsl(0, 0%, 92%);
}

body.docked.window-inactive .tab-bar {
    background-color: var(--background-color-unfocused);
}

.tab-bar > .border {
    position: absolute;
    left: 0;
    right: 0;
    z-index: var(--tab-bar-border-z-index);
    background-color: var(--tab-item-medium-border-color);
}

.tab-bar > .border.top {
    top: 0;
    height: calc(1px / var(--zoom-factor));
}

.tab-bar > .border.bottom {
    bottom: 0;
    height: 1px;
}

body.docked.bottom .tab-bar > .border.top {
    filter: brightness(80%);
}

body.big-sur.docked .tab-bar > .border.bottom {
    background-color: var(--border-color);
}

body.window-inactive .tab-bar > .border {
    background-color: var(--tab-item-light-border-color);
}

.tab-bar > .navigation-bar {
    height: var(--tab-bar-height);
}

.tab-bar > .navigation-bar > .item.group > .item {
    width: auto;
    margin: 0 4px;
    padding: 0;
}

.tab-bar > .navigation-bar > .item.group > .item:nth-child(1 of :not(.hidden)) {
    -webkit-margin-start: 8px;
}

.tab-bar > .navigation-bar > .item.group > .item:nth-last-child(1 of :not(.hidden)) {
    -webkit-margin-end: 8px;
}

.tab-bar > .navigation-bar .item.divider {
    margin-top: 1px;
    background-color: var(--tab-item-medium-border-color);
}

body.window-inactive .tab-bar > .navigation-bar > .item.divider {
    background-color: var(--tab-item-light-border-color);
}

.tab-bar > .tabs {
    display: flex;
    flex-wrap: wrap;
    position: relative;
    width: 100%;
    height: 100%;
}

body.docked .tab-bar .tabs {
    justify-content: space-around;
    padding: 0 4px; /* Keep in sync with `WI.TabBar.horizontalPadding` */
}

body.docked .tab-bar > .tabs > .flexible-space {
    flex-grow: 1;
}

body.docked.bottom .tab-bar > .tabs > .flexible-space {
    cursor: row-resize;
}

.tab-bar > .tabs > .item {
    display: flex;

    position: relative;

    z-index: calc(var(--tab-bar-border-z-index) + 1);

    align-items: center;

    padding: 0 8px;

    height: var(--tab-bar-item-height);
    overflow: hidden;

    line-height: 15px;
    outline: none;

    /* FIXME: These cause noticeable transitions when focusing the window. Fix that with JavaScript? */
    /* transition-property: background-position, border-color; */
    /* transition-duration: 250ms; */
    /* transition-delay: 50ms; */

    --tab-bar-item-height: 100%;
}

body:not(.docked) .tab-bar > .tabs > .item {
    border-top: var(--tab-item-medium-border-style);
    border-bottom: var(--tab-item-medium-border-style);
    background: var(--tab-item-background);
}

body.big-sur:not(.docked) .tab-bar > .tabs > .item {
    --tab-item-background: var(--tab-bar-background);
}

body:not(.big-sur):not(.docked) .tab-bar > .tabs > .item {
    background-size: 100% 200%;

    --tab-item-background: linear-gradient(to bottom, hsl(0, 0%, 78%), hsl(0, 0%, 72%));
}

body:not(.docked) .tab-bar > .tabs:not(.calculate-width) > .item:not(.pinned) {
    flex-grow: 1;
}

body.docked .tab-bar > .tabs > .item {
    margin: var(--tab-bar-item-vertical-margin) 2px 0; /* Keep in sync with `WI.TabBarItem.horizontalMargin` */

    --tab-bar-item-height: calc(var(--tab-bar-height) - (2 * var(--tab-bar-item-vertical-margin)));
    --tab-bar-item-vertical-margin: 4px;
}

.tab-bar > .tabs.calculate-width > .item:not(.pinned) {
    flex: initial;
}

body[dir=ltr]:not(.docked) .tab-bar > .tabs > .item:nth-child(n + 3 of :not(.hidden)),
body[dir=ltr]:not(.docked) .tab-bar > .tabs:not(.hide-border-start) > .item:nth-child(2 of :not(.hidden)),
body[dir=ltr]:not(.docked) .tab-bar > .tabs.dragging-tab > .item.selected,
body[dir=rtl]:not(.docked) .tab-bar > .tabs:not(.hide-border-end) > .item:nth-last-child(1 of :not(.hidden)) {
    border-left: var(--tab-item-medium-border-style);
}

body[dir=ltr]:not(.docked) .tab-bar > .tabs:not(.hide-border-end) > .item:nth-last-child(1 of :not(.hidden)),
body[dir=rtl]:not(.docked) .tab-bar > .tabs > .item:nth-child(n + 3 of :not(.hidden)),
body[dir=rtl]:not(.docked) .tab-bar > .tabs:not(.hide-border-start) > .item:nth-child(2 of :not(.hidden)),
body[dir=rtl]:not(.docked) .tab-bar > .tabs.dragging-tab > .item.selected {
    border-right: var(--tab-item-medium-border-style);
}

.tab-bar > .tabs > .item.pinned {
    flex: none;
    justify-content: center;
    width: var(--tab-bar-height);
    padding: 0;
}

body.docked .tab-bar > .tabs > .item.pinned {
    width: var(--tab-bar-item-height); /* Make pinned tabs square */
}

body.big-sur:not(.docked) .tab-bar > .tabs > .item:not(.disabled).selected {
    border-top-color: var(--tab-item-background);

    --tab-item-background: white;
}

body:not(.big-sur):not(.docked) .tab-bar > .tabs > .item:not(.disabled).selected {
    --tab-item-background: linear-gradient(to bottom, hsl(0, 0%, 87%), hsl(0, 0%, 82%));
    background-size: 100% 100%;
}

body.docked .tab-bar > .tabs > .item:not(.disabled):matches(.selected, :hover) {
    border-radius: var(--tab-bar-item-vertical-margin);
}

body.docked .tab-bar > .tabs > .item:not(.disabled).selected {
    background-color: lightgrey;
}

body.big-sur:not(.docked) .tab-bar > .tabs:not(.animating) > .item:not(.selected, .disabled):hover {
    --tab-item-background: hsl(0, 0%, 84%);
}

body:not(.big-sur):not(.docked) .tab-bar > .tabs:not(.animating) > .item:not(.selected, .disabled):hover {
    --tab-item-background: linear-gradient(to bottom, hsl(0, 0%, 67%), hsl(0, 0%, 64%));
}

body.docked .tab-bar > .tabs:not(.animating) > .item:not(.selected, .disabled):hover {
    background-color: hsl(0, 0%, 95%);
}

body[dir=ltr]:not(.docked) .tab-bar > .tabs:not(.animating) > .item:not(.selected, .disabled):hover,
body[dir=ltr]:not(.docked) .tab-bar > .tabs:not(.animating) > .item:not(.selected, .disabled):hover + .item,
body[dir=rtl]:not(.docked) .tab-bar > .tabs:not(.animating) > .item:last-child:not(.selected, .disabled):hover {
    border-left-color: var(--tab-item-dark-border-color);
}

body[dir=ltr]:not(.docked) .tab-bar > .tabs:not(.animating) > .item:last-child:not(.selected, .disabled):hover,
body[dir=rtl]:not(.docked) .tab-bar > .tabs:not(.animating) > .item:not(.selected, .disabled):hover,
body[dir=rtl]:not(.docked) .tab-bar > .tabs:not(.animating) > .item:not(.selected, .disabled):hover + .item {
    border-right-color: var(--tab-item-dark-border-color);
}

body:not(.docked).window-inactive .tab-bar > .tabs > .item {
    border-top-color: var(--tab-item-light-border-color);
    border-bottom-color: var(--tab-item-light-border-color);
    border-right-color: var(--tab-item-light-border-color) !important;
    border-left-color: var(--tab-item-light-border-color) !important;

    transition: none;
}

body.big-sur:not(.docked).window-inactive .tab-bar > .tabs > .item,
body:not(.big-sur):not(.docked).window-inactive .tab-bar > .tabs > .item {
    --tab-item-background: hsl(0, 0%, 92%);
}

body:not(.docked).window-inactive .tab-bar > .tabs > .item:not(.disabled).selected {
    --tab-item-background: hsl(0, 0%, 96%);
}

body.docked .tab-bar > .tabs > .item:not(.disabled):matches(.selected, :hover) {
    box-shadow: 0 1px 2px hsla(0, 0%, 0%, 0.15) inset;
}

body.docked.window-inactive .tab-bar > .tabs > .item:not(.disabled).selected {
    background-color: hsl(0, 0%, 90%);
}

.tab-bar > .tabs > .item > .flex-space {
    display: flex;
    flex: 1;
}

.tab-bar > .tabs > .item > .icon {
    width: 16px;
    height: 16px;

    min-width: 16px;
    min-height: 16px;

    opacity: 0.55; /* Assumes black glyphs. */

    -webkit-user-drag: none;
}

.tab-bar > .tabs > .item.pinned > .icon {
    width: 15px;
    height: 15px;
    min-width: 15px;
    min-height: 15px;
}

.tab-bar > .tabs > .item:not(.selected):hover > .icon {
    opacity: 0.6;
}

.tab-bar > .tabs > .item:not(.disabled).selected > .icon {
    opacity: 0.7;
}

.tab-bar > .tabs > .item.disabled > .icon {
    opacity: 0.35;
}

body.window-inactive .tab-bar > .tabs > .item > .icon {
    opacity: 0.3;
}

body.window-inactive .tab-bar > .tabs > .item:not(.disabled).selected > .icon {
    opacity: 0.4;
}

.tab-bar > .tabs > .item > .name {
    color: hsla(0, 0%, var(--foreground-lightness), 0.6);

    display: flex;

    min-width: 0;
    max-width: 400px;
    -webkit-margin-start: 6px;
}

body.window-inactive .tab-bar > .tabs > .item > .name {
    color: hsla(0, 0%, var(--foreground-lightness), 0.4);
}

.tab-bar > .tabs > .item > .name > .content {
    min-width: 0;
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.tab-bar > .tabs > .item:not(.selected):hover > .name {
    color: hsla(0, 0%, var(--foreground-lightness), 0.7);
}

.tab-bar > .tabs > .item:not(.disabled).selected > .name {
    color: hsla(0, 0%, var(--foreground-lightness), 0.8);
}

body.window-inactive .tab-bar > .tabs > .item:not(.disabled).selected > .name {
    color: hsla(0, 0%, var(--foreground-lightness), 0.5);
}

.tab-bar > .tabs.static-layout {
    position: relative;
}

.tab-bar > .tabs.static-layout > :matches(.flexible-space, .item) {
    position: absolute;
    top: 0;
}

.tab-bar > .tabs.animating.closing-tab > .item {
    transition-property: left;
    transition-duration: 250ms;
    transition-timing-function: ease-in-out;
}

.tab-bar > .tabs.animating:matches(.expanding-tabs, .inserting-tab) > .item {
    transition-property: left, width;
    transition-duration: 250ms;
    transition-timing-function: ease-in-out;
}

.tab-bar > .tabs.animating.inserting-tab > .item.being-inserted {
    z-index: calc(var(--tab-bar-border-z-index) + 2);
    padding: 0;
}

body:not(.docked) .tab-bar > .tabs.dragging-tab > .item:not(.disabled).selected,
body:not(.docked) .tab-bar > .tabs.static-layout:not(.animating.inserting-tab):not(.dragging-tab) > .item:nth-last-child(1 of :not(.pinned)),
body:not(.docked) .tab-bar > .tabs.animating.closing-tab > .item:not(.disabled).selected {
    border-right: var(--tab-item-medium-border-style);
}

body:not(.docked).window-inactive .tab-bar > .tabs.dragging-tab > .item:not(.disabled).selected,
body:not(.docked).window-inactive .tab-bar > .tabs.static-layout:not(.animating.inserting-tab):not(.dragging-tab) > .item:nth-last-child(1 of :not(.pinned)),
body:not(.docked).window-inactive .tab-bar > .tabs.animating.closing-tab > .item:not(.disabled).selected {
    border-right-color: var(--tab-item-light-border-color);
}

.tab-bar > .tabs.dragging-tab > .item:not(.disabled).selected {
    z-index: calc(var(--tab-bar-border-z-index) + 2);
    pointer-events: none;
}

@media (prefers-color-scheme: dark) {
    body.big-sur .tab-bar,
    body:not(.big-sur) .tab-bar {
        /* FIXME: <https://webkit.org/b/189769> Dark Mode: use semantic color names */
        --tab-item-dark-border-color: var(--tab-item-border-color);
        --tab-item-medium-border-color: var(--tab-item-border-color);
        --tab-item-light-border-color: var(--tab-item-border-color);
        --tab-item-border-color: hsl(0, 0%, 36%);
    }

    body.big-sur:not(.docked) .tab-bar {
        --tab-bar-background: hsl(0, 0%, 8%);
    }

    body:not(.big-sur):not(.docked) .tab-bar {
        background-image: linear-gradient(to bottom, hsl(0, 0%, 12%), hsl(0, 0%, 10%));
    }

    body.docked .tab-bar {
        background-color: hsl(0, 0%, 10%);
    }

    body.docked.bottom .tab-bar > .border.top {
        filter: brightness(120%);
    }

    body:not(.big-sur):not(.docked) .tab-bar > .tabs > .item {
        --tab-item-background: linear-gradient(to bottom, hsl(0, 0%, 12%), hsl(0, 0%, 10%));
    }

    body.big-sur:not(.docked) .tab-bar > .tabs > .item:not(.disabled).selected {
        --tab-item-background: hsl(0, 0%, 19%);
    }

    body:not(.big-sur):not(.docked) .tab-bar > .tabs > .item:not(.disabled).selected {
        --tab-item-background: linear-gradient(to bottom, hsl(0, 0%, 23%), hsl(0, 0%, 21%));
    }

    body.big-sur:not(.docked) .tab-bar > .tabs:not(.animating) > .item:not(.selected, .disabled):hover {
        --tab-item-background: hsl(0, 0%, 7%);
    }

    body:not(.big-sur):not(.docked) .tab-bar > .tabs:not(.animating) > .item:not(.selected, .disabled):hover {
        --tab-item-background: linear-gradient(to bottom, hsl(0, 0%, 11%), hsl(0, 0%, 9%));
    }

    body.docked .tab-bar > .tabs:not(.animating) > .item:not(.selected, .disabled):hover {
        background-color: hsl(0, 0%, 15%);
    }

    body.docked .tab-bar > .tabs > .item:not(.disabled).selected {
        background-color: var(--background-color);
    }

    body.docked.window-inactive .tab-bar > .tabs > .item:not(.disabled).selected {
        background-color: hsl(0, 0%, 20%);
    }

    .tab-bar > .tabs > .item > .icon {
        filter: var(--filter-invert);
    }

    body:not(.docked) .tab-bar > .tabs:not(.animating) > .item:not(.selected, .disabled):hover > .icon {
        opacity: 0.6;
    }

    body:not(.docked).window-inactive .tab-bar {
        --tab-item-border-color: hsl(0, 0%, 34%);
    }

    body.big-sur:not(.docked).window-inactive .tab-bar,
    body:not(.big-sur):not(.docked).window-inactive .tab-bar {
        --tab-bar-background: hsl(0, 0%, 7%);
    }

    body.big-sur:not(.docked).window-inactive .tab-bar > .tabs > .item {
        --tab-item-background: hsl(0, 0%, 7%);
    }

    body:not(.big-sur):not(.docked).window-inactive .tab-bar > .tabs > .item {
        --tab-item-background: hsl(0, 0%, 13%);
    }

    body.big-sur:not(.docked).window-inactive .tab-bar > .tabs > .item:not(.disabled).selected {
        --tab-item-background: hsl(0, 0%, 11%);
    }

    body:not(.big-sur):not(.docked).window-inactive .tab-bar > .tabs > .item:not(.disabled).selected {
        --tab-item-background: hsl(0, 0%, 19%);
    }
}
