// (C) 2025 GoodData Corporation

.gd-ui-kit-button-segmented-control {
    display: flex;

    /**
     * Disable transitions for keyboard navigation to avoid "stale border" artifact.
     */
    &:has(:focus-visible) {
        --gd-transition-all: none;
    }

    /**
     * Per-segment defaults (apply to direct children; values are inherited by nested buttons).
     */
    > * {
        --gd-button-borderRadiusTopLeft: 0;
        --gd-button-borderRadiusBottomLeft: 0;
        --gd-button-borderRadiusTopRight: 0;
        --gd-button-borderRadiusBottomRight: 0;

        position: relative;
    }

    /**
     * Collapse borders between segments by overlapping neighboring segments.
     * On hover/focus, raise the segment above its neighbors to ensure its border is visible.
     */
    > * + * {
        margin-left: -1px;
    }

    /**
     * Stacking order: keyboard focus should win over hover.
     *
     * This prevents visual glitches when focus moves between segments (old border "sticking")
     * and avoids hover on a neighboring segment disrupting the currently focused segment.
     */
    > *:hover {
        z-index: 1;
    }

    /**
     * This fixes the situation where if button is active (pressed), then the neighboring button to the right overlaps
     * active button's right border with wrong color (should be the active border color) if the active button is
     * no longer focused. The following fixes that.
     */
    > *.gd-ui-kit-button--isSelected,
    > *.gd-ui-kit-icon-button--active,
    > *:has(.gd-ui-kit-button--isSelected),
    > *:has(.gd-ui-kit-icon-button--active) {
        z-index: 2;
    }

    > *:focus-within {
        z-index: 3;
    }

    /**
     * Rounded corners for the first/last segment.
     */
    > :first-child {
        --gd-button-borderRadiusTopLeft: var(--gd-button-borderRadius);
        --gd-button-borderRadiusBottomLeft: var(--gd-button-borderRadius);
    }

    > :last-child {
        --gd-button-borderRadiusTopRight: var(--gd-button-borderRadius);
        --gd-button-borderRadiusBottomRight: var(--gd-button-borderRadius);
    }

    /**
     * Layout variants.
     */
    &--layout-fill {
        flex: 1;

        > * {
            flex: 1 1 0;

            --gd-button-width: 100%;
        }
    }
}
