// (C) 2025 GoodData Corporation
@use "variables";
@use "@gooddata/sdk-ui-kit/styles/scss/variables" as kit-variables;

//
// Multi-Layer Legend Styles
// Based on Figma design: https://www.figma.com/design/L2DE4SJXqNxEECwidh88HT/25Q4---Geocharts
//

$legend-border-color: var(--gd-palette-complementary-3);
$legend-divider-color: var(--gd-palette-complementary-3);
$legend-title-color: var(--gd-palette-complementary-8);
$legend-text-color: var(--gd-palette-complementary-7);
$legend-chevron-color: var(--gd-palette-complementary-6);

@mixin legend-focus-outline($offset) {
    outline: 2px solid var(--gd-palette-primary-base);
    outline-offset: $offset;
    border-radius: 2px;
}

@mixin legend-focus-fill {
    border-radius: 2px;
    background-color: var(--gd-palette-primary-base-t80);
}

.gd-geo-multi-layer-legend {
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 3; // Above MapLibre controls
    background-color: kit-variables.$gd-color-white;
    border: 1px solid $legend-border-color;
    border-radius: 3px;
    box-shadow: 0 1px 2px 0 rgba(20, 57, 93, 0.1);
    width: 220px;
    max-height: calc(100% - 20px);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    // Smoothly animate container resizing when sections collapse/expand.
    // Section transition duration is the single source of truth.
    transition: height var(--gd-geo-multi-layer-legend__toggle-duration) ease-out;
    font-family: kit-variables.$gd-font-primary;
    font-size: 12px;
    line-height: 1.4;
    // Section header height, matches js variable COLLAPSED_SECTION_HEIGHT.
    --gd-geo-multi-layer-legend__section-header-height: 34px;
    // Single source of truth for expand/collapse timing.
    --gd-geo-multi-layer-legend__toggle-duration: 0.2s;

    @media (prefers-reduced-motion: reduce) {
        --gd-geo-multi-layer-legend__toggle-duration: 0s;
    }

    // Position modifiers
    &--top-left {
        top: 10px;
        left: 10px;
        right: auto;
        bottom: auto;
    }

    &--top-right {
        top: 10px;
        right: 10px;
        left: auto;
        bottom: auto;
    }

    &--bottom-left {
        bottom: 10px;
        left: 10px;
        top: auto;
        right: auto;
    }

    &--bottom-right {
        bottom: 10px;
        right: 10px;
        top: auto;
        left: auto;
    }

    // ==========================================================================
    // SECTION = One layer's legend
    // ==========================================================================
    &__section {
        display: grid;
        grid-template-rows: var(--gd-geo-multi-layer-legend__section-header-height) 0fr; // header fixed, content 0fr (collapsed)
        align-content: start;
        transition: grid-template-rows var(--gd-geo-multi-layer-legend__toggle-duration) ease-out;
        overflow: hidden;
        min-height: var(--gd-geo-multi-layer-legend__section-header-height);

        // Flex item: by default, take natural height
        flex: 0 0 auto;

        // Expanded: show full section content
        &--expanded {
            grid-template-rows: var(--gd-geo-multi-layer-legend__section-header-height) 1fr;
        }

        // Flexible sections (exceed equal share): grow to fill available space, become scrollable
        &--flexible {
            flex: 1 0 0;
            overflow-y: auto;
            overscroll-behavior: auto;
        }

        // Border between sections (not on first)
        & + & {
            border-top: 1px solid $legend-divider-color;
        }
    }

    // Section header with layer title, chevron, and toggle
    &__section-header {
        display: flex;
        align-items: center;
        gap: 5px;
        height: var(--gd-geo-multi-layer-legend__section-header-height);
        box-sizing: border-box;
        padding: 5px 10px;
        background-color: kit-variables.$gd-color-white;
        user-select: none;
        // Never shrink - header must always be visible
        flex-shrink: 0;
        min-width: 0;

        // Legacy path (enableGeoChartA11yImprovements=false):
        // the header itself is a keyboard-focusable button-like element.
        &[role="button"] {
            cursor: pointer;

            &:focus {
                outline: none;
            }

            &:focus-visible {
                outline: 2px solid var(--gd-palette-primary-base);
                outline-offset: -2px;
            }
        }
    }

    // Native button inside header for expand/collapse
    &__section-header-btn {
        // Button reset
        appearance: none;
        border: none;
        background: none;
        font: inherit;
        color: inherit;
        text-align: left;
        padding: 0;
        margin: 0;
        cursor: pointer;

        // Fill available space next to toggle
        flex: 1;
        min-width: 0;
        display: flex;
        align-items: center;
        gap: 5px;

        &:focus {
            outline: none;
        }

        &:focus-visible {
            outline: 2px solid var(--gd-palette-primary-base);
            outline-offset: -2px;
        }
    }

    // Chevron icon container
    &__section-chevron {
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 16px;
        height: 16px;
    }

    // Chevron icon with rotation animation
    &__chevron-icon {
        display: inline-flex;
        color: $legend-chevron-color;
        transition: transform var(--gd-geo-multi-layer-legend__toggle-duration) ease-out;
        transform: rotate(0deg);

        &--expanded {
            transform: rotate(90deg);
        }
    }

    // Layer title
    &__section-title {
        flex: 1;
        font-weight: 700;
        font-size: 12px;
        line-height: 16px;
        color: $legend-title-color;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    // Toggle switch container
    &__section-toggle {
        flex-shrink: 0;
        display: flex;
        align-items: center;
    }

    &__toggle-switch {
        display: block;
    }

    // Content wrapper for CSS Grid animation (smooth expand/collapse)
    &__section-content-wrapper {
        // The section itself animates (grid-template-rows), so wrapper only needs clipping.
        overflow: hidden;
        min-height: 0;
        height: 100%;
    }

    // Section content (scrollable area for groups)
    // No border between header and content - borders only between sections
    &__section-content {
        display: flex;
        flex-direction: column;
        // Required for CSS Grid 0fr animation to work
        min-height: 0;
        height: 100%;
        overflow-x: hidden;
        overflow-y: hidden; // enabled after expand animation (see --scroll-enabled modifier)
        scrollbar-gutter: stable;

        // Firefox scrollbar
        scrollbar-width: thin;
        scrollbar-color: var(--gd-palette-complementary-4, #ccd8e2) transparent;

        // Hide content when collapsed (prevents visible flicker)
        .gd-geo-multi-layer-legend__section--collapsed & {
            opacity: 0;
            overflow-y: hidden;
            animation: none;
        }

        // Enable scrolling only after expand animation finishes (set by React via --scroll-enabled modifier).
        .gd-geo-multi-layer-legend__section--scroll-enabled & {
            overflow-y: auto;
            opacity: 1;
            overscroll-behavior: auto;
        }

        // Hidden layer: dimmed and desaturated content.
        // Placed after --scroll-enabled so hidden state wins over scroll-enabled's opacity: 1.
        .gd-geo-multi-layer-legend__section--hidden & {
            cursor: default;
            opacity: 0.35;
            filter: grayscale(1);
        }

        // Scrollbar styling
        &::-webkit-scrollbar {
            width: 10px;
        }

        &::-webkit-scrollbar-track {
            background: transparent;
            margin: 4px 0;
        }

        &::-webkit-scrollbar-thumb {
            background-color: var(--gd-palette-complementary-4, #ccd8e2);
            border: 2px solid transparent;
            border-radius: 48px;
            background-clip: padding-box;
        }
    }

    // ==========================================================================
    // GROUP = One data bucket (size, color, etc.)
    // ==========================================================================
    &__group {
        // Figma: pl-31px pr-0 for color/size items
        // No right padding - scrollbar-gutter handles alignment
        padding: 0 0 15px 31px;

        // Space between groups
        & + & {
            margin-top: 10px;
        }

        // Color scale has different padding (Figma: pl-32px pr-10px)
        &--color-scale {
            padding-left: 32px;
            padding-right: 10px;
        }

        &--focusable:focus {
            outline: none;
        }

        &--focusable:focus-visible {
            .gd-geo-multi-layer-legend__group-focus-target {
                box-shadow:
                    inset 0 0 0 2px var(--gd-palette-primary-base),
                    0 0 0 1px var(--gd-palette-complementary-0);
                @include legend-focus-fill;
            }
        }
    }

    &__group-focus-target {
        margin-right: 12px;
        border-radius: 2px;
    }

    // Group title (like "Type:", "30-Day Priority Index")
    &__group-title {
        font-weight: 400;
        font-size: 12px;
        line-height: 20px;
        color: $legend-text-color;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        text-align: left;
    }

    // ==========================================================================
    // SIZE LEGEND
    // ==========================================================================
    &__size-list {
        display: flex;
        flex-direction: row;
        align-items: flex-end;
        gap: 6px;
        padding: 5px 0;
    }

    &__size-anchor {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 4px;
    }

    &__size-circle {
        border: 1px solid variables.$border-color-circle-legend;
        border-radius: 50%;
        box-sizing: content-box;
        background-color: transparent;
    }

    &__size-label {
        font-weight: 400;
        font-size: 12px;
        line-height: 16px;
        color: $legend-text-color;
        white-space: nowrap;
    }

    // ==========================================================================
    // COLOR LEGEND
    // ==========================================================================
    &__color-list {
        display: flex;
        flex-direction: column;

        // Enhanced a11y path (enableGeoChartA11yImprovements=true):
        // focus is visualized on active list item / focused group, not this container.
        &:focus {
            outline: none;
        }

        &:focus-visible {
            outline: none;
        }
    }

    &__color-item {
        display: flex;
        align-items: center;
        gap: 5px;
        min-height: 20px;

        // Clickable item styles
        &--clickable {
            cursor: pointer;
            border-radius: 2px;
            padding: 0 2px;
            margin: 0 -2px;

            // Legacy path (FF off): clickable color items are directly focusable via tabIndex.
            &:focus {
                outline: none;
            }

            &:focus-visible {
                @include legend-focus-outline(1px);
            }
        }

        // Active item indicator (composite widget: focus stays on list container,
        // active item is highlighted when list has focus)
        &--active {
            .gd-geo-multi-layer-legend__color-list:focus-visible & {
                @include legend-focus-outline(-1px);
                @include legend-focus-fill;

                .gd-geo-multi-layer-legend__color-label {
                    font-weight: 700;
                }
            }
        }

        // Disabled item styles (hidden on chart)
        &--disabled {
            .gd-geo-multi-layer-legend__color-swatch {
                opacity: 0.4;
            }

            .gd-geo-multi-layer-legend__color-label {
                opacity: 0.5;
            }

            .gd-geo-multi-layer-legend__color-count {
                opacity: 0.3;
            }
        }
    }

    &__color-swatch {
        flex-shrink: 0;
        width: 10px;
        height: 10px;
        border-radius: 0;
    }

    &__color-label {
        font-weight: 400;
        font-size: 12px;
        line-height: 20px;
        color: $legend-text-color;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    &__color-count {
        font-size: 11px;
        color: $legend-text-color;
        opacity: 0.7;
    }

    // ==========================================================================
    // COLOR SCALE LEGEND (gradient for numeric color)
    // ==========================================================================
    &__color-scale {
        display: flex;
        flex-direction: column;
        gap: 4px;
        padding: 5px 0;
    }

    &__color-scale-bar {
        height: 8px;
        width: 100%;
        border-radius: 2px;
        // Default gradient - light to primary color
        background: linear-gradient(
            to right,
            var(--gd-palette-complementary-2, #ebeff4),
            var(--gd-palette-primary-base, #14b2e2)
        );
    }

    &__color-scale-labels {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    &__color-scale-min,
    &__color-scale-max {
        font-weight: 400;
        font-size: 12px;
        line-height: 16px;
        color: $legend-text-color;
    }
}
