.chart-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: stretch;
    margin-bottom: 0.9rem;
}

.chart-legend__primary {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    flex: 1 1 auto;
}

.chart-legend__item {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.25rem 0.6rem;
    border: 1px solid #ced4da;
    border-radius: 999px;
    background-color: #fff;
    color: #212529;
    font-size: 0.82rem;
    line-height: 1.1;
    transition: background-color 0.2s ease, box-shadow 0.2s ease, color 0.2s ease;
    cursor: pointer;
    max-width: 200px;
}

.chart-legend__item:hover {
    background-color: #f1f3f5;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.chart-legend__item:focus-visible {
    outline: 2px solid #0d6efd;
    outline-offset: 2px;
}

.chart-legend__item--inactive {
    color: #6c757d;
    background-color: #f8f9fa;
    border-color: #dee2e6;
    opacity: 0.7;
}

.chart-legend__color {
    width: 0.65rem;
    height: 0.65rem;
    border-radius: 999px;
    background-color: var(--legend-color, #6c757d);
    flex-shrink: 0;
}

.chart-legend__label {
    display: inline-block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chart-legend__more {
    flex: 1 1 100%;
    position: relative;
    display: flex;
    justify-content: flex-end;
    width: 100%;
}

.chart-legend__more-summary {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.3rem 0.65rem;
    border: 1px solid #ced4da;
    border-radius: 999px;
    background-color: #fff;
    font-size: 0.8rem;
    color: #212529;
    cursor: pointer;
    list-style: none;
}

.chart-legend__more[open] .chart-legend__more-summary {
    background-color: #e9f2ff;
    border-color: #91c4ff;
}

.chart-legend__more-summary::-webkit-details-marker {
    display: none;
}

.chart-legend__more-summary::after {
    content: '\25BE';
    font-size: 0.75rem;
    color: #495057;
}

.chart-legend__more[open] .chart-legend__more-summary::after {
    content: '\25B4';
}

.chart-legend__more-list {
    position: absolute;
    top: calc(100% + 0.6rem);
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 0.5rem;
    padding: 0.85rem 1rem;
    width: min(100%, calc(100vw - 2rem));
    min-width: min(100%, 640px);
    max-width: 1280px;
    background-color: #fff;
    border: 1px solid rgba(0, 0, 0, 0.12);
    border-radius: 0.75rem;
    box-shadow: 0 12px 32px rgba(15, 23, 42, 0.16);
}

.chart-legend__more-list .chart-legend__item {
    width: 100%;
    justify-content: flex-start;
}

.chart-legend__more-list .chart-legend__label {
    white-space: normal;
}

@media (max-width: 575.98px) {
    .chart-legend {
        flex-direction: column;
        gap: 0.75rem;
    }

    .chart-legend__primary {
        width: 100%;
    }

    .chart-legend__item {
        max-width: none;
        width: 100%;
        justify-content: flex-start;
    }

    .chart-legend__more {
        display: block;
    }

    .chart-legend__more-summary {
        width: 100%;
        justify-content: center;
    }

    .chart-legend__more-list {
        position: static;
        transform: none;
        top: auto;
        left: auto;
        width: 100%;
        min-width: 100%;
        max-width: none;
        margin-top: 0.6rem;
        padding: 0.75rem;
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    }
}
