/*--------------------------
Inspired by https://github.com/Akifyss/obsidian-border
---------------------------*/

body.anp-border-layout {
    --card-background-color: rgb(var(--ctp-crust));
    --card-foreground-color: var(--background-primary);
    --border-border-style: 1px solid var(--tab-outline-color);
    --anp-border-bottom-padding: var(--anp-border-padding, 20px);

    .horizontal-main-container {
        background-color: var(--card-background-color);
        --titlebar-background: var(--card-background-color);
        --ribbon-background: var(--card-background-color);
        --ribbon-background-collapsed: var(--card-background-color);
    }

    &.anp-fixed-status-bar {
        --anp-border-bottom-padding: calc(var(--anp-border-padding, 20px) - 18px);
    }

    .workspace {

        & .workspace-tab-container {
            border-bottom: var(--border-border-style);
        }

        // LEFT SIDE rules when the left sidedock is closed
        &:not(.is-left-sidedock-open) {
            >.workspace-split.mod-vertical.mod-root {

                >.workspace-tabs:nth-child(2).workspace-tabs.mod-top-left-space,
                // the following 2 selectors don't make sense but they are here because obsidan let's you create a vertical split inside a vertical split in certain conditions
                // ideally this would be fixed in a future release but for now this is a workaround
                // please remove the 2 selectors for other elements if you remove this one
                >.workspace-split.mod-vertical>.workspace-tabs:nth-child(2).mod-top-left-space,
                >.workspace-split.mod-vertical>.workspace-split.mod-vertical>.workspace-tabs:nth-child(2).mod-top-left-space {
                    >.workspace-tab-container {
                        border-bottom-left-radius: var(--anp-border-radius, var(--radius-xl));
                    }
                }

                & .workspace-tabs.mod-top-left-space {
                    .workspace-tab-container {
                        border-top-left-radius: var(--anp-border-radius, var(--radius-xl));
                    }
                }

                >.workspace-split.mod-horizontal:nth-child(2),
                >.workspace-split.mod-vertical>.workspace-split.mod-horizontal:nth-child(2),
                >.workspace-split.mod-vertical>.workspace-split.mod-vertical>.workspace-split.mod-horizontal:nth-child(2) {
                    >.workspace-tabs:last-child {
                        .workspace-tab-container {
                            border-bottom-left-radius: var(--anp-border-radius, var(--radius-xl));
                        }
                    }

                    >.workspace-split.mod-vertical:last-child {
                        >.workspace-tabs:nth-child(2) {
                            .workspace-tab-container {
                                border-bottom-left-radius: var(--anp-border-radius, var(--radius-xl));
                            }
                        }
                    }
                }

                // selecting all tabs that are on the extreme left for the border
                & .workspace-tabs.mod-top-left-space {
                    >.workspace-tab-container {
                        border-left: var(--border-border-style);
                    }
                }

                >.workspace-split.mod-horizontal:nth-child(2),
                >.workspace-split.mod-vertical>.workspace-split.mod-horizontal:nth-child(2),
                >.workspace-split.mod-vertical>.workspace-split.mod-vertical>.workspace-split.mod-horizontal:nth-child(2) {
                    >.workspace-tabs {
                        >.workspace-tab-container {
                            border-left: var(--border-border-style);
                        }
                    }

                    >.workspace-tabs:not(.mod-top-left-space) {
                        >.workspace-tab-header-container {
                            border-left: var(--border-border-style);
                        }
                    }

                    >.workspace-split.mod-vertical {
                        >.workspace-tabs:nth-child(2) {
                            >.workspace-tab-container {
                                border-left: var(--border-border-style);
                            }
                        }

                        >.workspace-tabs:nth-child(2):not(.mod-top-left-space) {
                            >.workspace-tab-header-container {
                                border-left: var(--border-border-style);
                            }
                        }
                    }
                }
            }
        }

        // RIGHT SIDE rules when the right sidedock is closed (please make sure it similar in structure to the left side rules above)
        &:not(.is-right-sidedock-open) {
            >.workspace-split.mod-vertical.mod-root {

                >.workspace-tabs:last-child.mod-top-right-space,
                // the following 2 selectors don't make sense but they are here because obsidan let's you create a vertical split inside a vertical split in certain conditions
                // ideally this would be fixed in a future release but for now this is a workaround
                // please remove the 2 selectors for other elements if you remove this one
                >.workspace-split.mod-vertical>.workspace-tabs:last-child.mod-top-right-space,
                >.workspace-split.mod-vertical>.workspace-split.mod-vertical>.workspace-tabs:last-child.mod-top-right-space {
                    >.workspace-tab-container {
                        border-bottom-right-radius: var(--anp-border-radius, var(--radius-xl));
                    }
                }

                & .workspace-tabs.mod-top-right-space {
                    .workspace-tab-container {
                        border-top-right-radius: var(--anp-border-radius, var(--radius-xl));
                    }
                }

                >.workspace-split.mod-horizontal:last-child,
                >.workspace-split.mod-vertical>.workspace-split.mod-horizontal:last-child,
                >.workspace-split.mod-vertical>.workspace-split.mod-vertical>.workspace-split.mod-horizontal:last-child {
                    >.workspace-tabs:last-child {
                        .workspace-tab-container {
                            border-bottom-right-radius: var(--anp-border-radius, var(--radius-xl));
                        }
                    }

                    >.workspace-split.mod-vertical:last-child {
                        >.workspace-tabs:last-child {
                            .workspace-tab-container {
                                border-bottom-right-radius: var(--anp-border-radius, var(--radius-xl));
                            }
                        }
                    }
                }

                // selecting all tabs that are on the extreme right for the border
                & .workspace-tabs.mod-top-right-space {
                    >.workspace-tab-container {
                        border-right: var(--border-border-style);
                    }
                }

                >.workspace-split.mod-horizontal:last-child,
                >.workspace-split.mod-vertical>.workspace-split.mod-horizontal:last-child,
                >.workspace-split.mod-vertical>.workspace-split.mod-vertical>.workspace-split.mod-horizontal:last-child {
                    >.workspace-tabs {
                        >.workspace-tab-container {
                            border-right: var(--border-border-style);
                        }
                    }

                    >.workspace-tabs:not(.mod-top-right-space) {
                        >.workspace-tab-header-container {
                            border-right: var(--border-border-style);
                        }
                    }

                    >.workspace-split.mod-vertical {
                        >.workspace-tabs:last-child {
                            >.workspace-tab-container {
                                border-right: var(--border-border-style);
                            }
                        }

                        >.workspace-tabs:last-child:not(.mod-top-right-space) {
                            >.workspace-tab-header-container {
                                border-right: var(--border-border-style);
                            }
                        }
                    }
                }
            }
        }

        .workspace-split.mod-left-split {
            background-color: var(--card-background-color);
            padding-left: calc(var(--anp-border-padding, 20px) - 20px);

            .workspace-tabs {
                background-color: var(--card-background-color);

                .workspace-tab-container {
                    .workspace-leaf {
                        background-color: var(--card-foreground-color);
                    }

                    border-left: var(--border-border-style);
                }

                &:not(.mod-top) {
                    .workspace-tab-header-container {
                        border-left: var(--border-border-style);
                    }
                }

                &:last-child {
                    .workspace-tab-container {
                        border-bottom: var(--border-border-style);
                    }
                }

                &.mod-top-left-space {
                    .workspace-tab-container {
                        border-top: var(--border-border-style);
                        border-left: var(--border-border-style);
                    }
                }
            }

            .workspace-sidedock-vault-profile {
                border-top: none;
                background-color: var(--card-foreground-color);
                border-left: var(--border-border-style);
                border-bottom: var(--border-border-style);
            }
        }

        .workspace-split.mod-right-split {
            background-color: var(--card-background-color);

            .workspace-tabs {
                background-color: var(--card-background-color);

                .workspace-tab-container {
                    .workspace-leaf {
                        background-color: var(--card-foreground-color);
                    }

                    border-right: var(--border-border-style);
                }

                &:not(.mod-top) {
                    .workspace-tab-header-container {
                        border-right: var(--border-border-style);
                    }
                }

                &:last-child {
                    .workspace-tab-container {
                        border-bottom: var(--border-border-style);
                    }
                }

                &.mod-top-right-space {
                    .workspace-tab-container {
                        border-top: var(--border-border-style);
                        border-right: var(--border-border-style);
                    }
                }
            }
        }

        .workspace-split:not(.mod-right-split):not(.mod-left-split) {
            >.workspace-split {
                padding-bottom: 0;
            }

            background-color: var(--card-background-color);

            .workspace-tabs {
                background-color: var(--card-background-color);

                &:last-child {
                    .workspace-tab-container {
                        border-bottom: var(--border-border-style);
                    }
                }

                &.mod-top {
                    .workspace-tab-container {
                        border-top: var(--border-border-style);
                    }
                }
            }
        }

        .workspace-tabs:not(.mod-top) {
            .workspace-tab-header-container {
                background-color: var(--card-foreground-color);
            }
        }

        .workspace-tabs.mod-top {
            .workspace-tab-header-container {
                border-bottom: none;
            }
        }
    }

    .workspace-split.mod-left-split>.workspace-leaf-resize-handle,
    .workspace-split.mod-right-split>.workspace-leaf-resize-handle {
        height: calc(100% + -1*var(--header-height) + -1*var(--anp-border-bottom-padding, 20px));
        bottom: var(--anp-border-bottom-padding, 20px);
    }

    .workspace-split {
        --divider-vertical-height: calc(100% + -1*var(--header-height));
    }

    .workspace-ribbon.mod-left:before {
        border-bottom: 1px solid var(--card-background-color);
    }

    .workspace-ribbon.mod-left,
    .workspace-ribbon.mod-left.is-focused {
        border-right-color: var(--card-background-color);
    }

    &.is-popout-window {
        .workspace {
            margin-left: var(--anp-border-padding, 20px);
        }
    }
}

// Apply margins & border radius if on PC
// Does NOT applies on phone and tablets
// todo: needs some fixes to enable & look great on tablets
body.anp-border-layout:not(.is-mobile) {
    .workspace {
        margin-right: var(--anp-border-padding, 20px);

        .workspace-split.mod-left-split {
            padding-bottom: var(--anp-border-bottom-padding, 20px);

            .workspace-tabs {
                &.mod-top-left-space {
                    .workspace-tab-container {
                        border-top-left-radius: var(--anp-border-radius, var(--radius-xl));
                    }
                }
            }

            .workspace-sidedock-vault-profile {
                border-bottom-left-radius: var(--anp-border-radius, var(--radius-xl));
            }
        }

        .workspace-split.mod-right-split {
            padding-bottom: var(--anp-border-bottom-padding, 20px);

            .workspace-tabs {
                &:last-child {
                    .workspace-tab-container {
                        border-bottom-right-radius: var(--anp-border-radius, var(--radius-xl));
                    }
                }

                &.mod-top-right-space {
                    .workspace-tab-container {
                        border-top-right-radius: var(--anp-border-radius, var(--radius-xl));
                    }
                }
            }
        }

        .workspace-split.mod-root:not(.mod-right-split):not(.mod-left-split) {
            padding-bottom: var(--anp-border-bottom-padding, 20px);
        }
    }
}
