/**
 * Map Legend Control
 * Custom Leaflet layer control component
 * Follows site design system with CSS custom properties
 */

/* ==========================================================================
   CONTROL CONTAINER
   ========================================================================== */

.map-legend-control {
    position: absolute;
    top: 16px;
    right: 16px;
    z-index: var(--z-dropdown, 1000);
    pointer-events: auto;
}

.map-legend {
    width: 300px;
    background: var(--surface-2, #ffffff);
    border: 1px solid var(--border-subtle, rgba(15, 23, 42, 0.12));
    border-radius: var(--radius-lg, 0.75rem);
    box-shadow: var(--shadow-soft, 0 6px 18px rgba(0, 0, 0, 0.14));
    overflow: hidden;
    transition: all var(--transition-normal, 250ms ease-in-out);
}

/* ==========================================================================
   HEADER
   ========================================================================== */

.map-legend__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-4, 1rem);
    background: var(--surface-3, #eef2f6);
    border-bottom: 1px solid var(--border-subtle, rgba(15, 23, 42, 0.12));
}

.map-legend__title {
    font-weight: var(--font-weight-semibold, 600);
    color: var(--text-primary, #212529);
    font-size: var(--font-size-base, 1rem);
    margin: 0;
}

.map-legend__collapse {
    background: transparent;
    border: none;
    color: var(--text-secondary, #6c757d);
    cursor: pointer;
    padding: var(--space-2, 0.5rem);
    transition: all var(--transition-fast, 150ms ease-in-out);
    border-radius: var(--radius-sm, 0.25rem);
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
}

.map-legend__collapse:hover {
    color: var(--text-primary, #212529);
    background: var(--nav-hover, #f8f9fa);
}

.map-legend__collapse:focus {
    outline: 2px solid var(--accent, #1a4d73);
    outline-offset: 2px;
}

/* ==========================================================================
   BODY
   ========================================================================== */

.map-legend__body {
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0;
    max-height: 420px;
    -webkit-overflow-scrolling: touch;
}

/* Custom scrollbar styling */
.map-legend__body::-webkit-scrollbar {
    width: 6px;
}

.map-legend__body::-webkit-scrollbar-track {
    background: transparent;
}

.map-legend__body::-webkit-scrollbar-thumb {
    background: var(--border-strong, rgba(15, 23, 42, 0.2));
    border-radius: var(--radius-full, 9999px);
}

.map-legend__body::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary, #6c757d);
}

/* ==========================================================================
   SECTIONS
   ========================================================================== */

.map-legend__section {
    padding: var(--space-4, 1rem);
    border-bottom: 1px solid var(--border-subtle, rgba(15, 23, 42, 0.12));
}

.map-legend__section:last-child {
    border-bottom: none;
}

.map-legend__section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--space-3, 0.75rem);
}

.map-legend__section-title {
    font-size: var(--font-size-xs, 0.75rem);
    font-weight: var(--font-weight-semibold, 600);
    color: var(--text-secondary, #6c757d);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin: 0 0 var(--space-3, 0.75rem) 0;
}

/* ==========================================================================
   ACTION BUTTONS
   ========================================================================== */

.map-legend__actions {
    display: flex;
    gap: var(--space-2, 0.5rem);
}

.map-legend__action-btn {
    padding: var(--space-1, 0.25rem) var(--space-2, 0.5rem);
    font-size: var(--font-size-xs, 0.75rem);
    background: transparent;
    border: 1px solid var(--border-subtle, rgba(15, 23, 42, 0.12));
    border-radius: var(--radius-sm, 0.25rem);
    color: var(--text-secondary, #6c757d);
    cursor: pointer;
    transition: all var(--transition-fast, 150ms ease-in-out);
    font-weight: var(--font-weight-medium, 500);
}

.map-legend__action-btn:hover {
    background: var(--nav-hover, #f8f9fa);
    color: var(--text-primary, #212529);
    border-color: var(--border-strong, rgba(15, 23, 42, 0.2));
}

.map-legend__action-btn:focus {
    outline: 2px solid var(--accent, #1a4d73);
    outline-offset: 2px;
}

/* ==========================================================================
   OPTIONS (RADIO & CHECKBOX ROWS)
   ========================================================================== */

.map-legend__options {
    display: flex;
    flex-direction: column;
    gap: var(--space-2, 0.5rem);
}

/* Base map selector: 2x2 grid layout */
.map-legend__options[role='radiogroup'] {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-2, 0.5rem);
}

.map-legend__option {
    display: flex;
    align-items: center;
    gap: var(--space-3, 0.75rem);
    padding: var(--space-3, 0.75rem);
    background: var(--surface-1, #f8f9fa);
    border: 1px solid var(--border-subtle, rgba(15, 23, 42, 0.12));
    border-radius: var(--radius-md, 0.5rem);
    cursor: pointer;
    transition: all var(--transition-fast, 150ms ease-in-out);
    min-height: 44px;
    user-select: none;
}

.map-legend__option:hover {
    background: var(--nav-hover, #f8f9fa);
    border-color: var(--border-strong, rgba(15, 23, 42, 0.2));
    transform: translateX(2px);
}

.map-legend__option:focus-within {
    outline: 2px solid var(--accent, #1a4d73);
    outline-offset: 2px;
}

.map-legend__option--selected,
.map-legend__option--checked {
    background: var(--accent-soft, rgba(26, 77, 115, 0.14));
    border-color: var(--accent, #1a4d73);
}

.map-legend__option--selected:hover,
.map-legend__option--checked:hover {
    background: var(--accent-soft, rgba(26, 77, 115, 0.14));
    border-color: var(--accent-strong, #2a5f8a);
}

/* ==========================================================================
   NATIVE INPUTS (HIDDEN)
   ========================================================================== */

.map-legend__radio,
.map-legend__checkbox {
    position: absolute;
    opacity: 0;
    pointer-events: none;
    width: 1px;
    height: 1px;
}

/* ==========================================================================
   CUSTOM RADIO VISUAL
   ========================================================================== */

.map-legend__radio-visual {
    width: 20px;
    height: 20px;
    border: 2px solid var(--border-strong, rgba(15, 23, 42, 0.2));
    border-radius: var(--radius-full, 9999px);
    position: relative;
    flex-shrink: 0;
    transition: all var(--transition-fast, 150ms ease-in-out);
    background: var(--surface-2, #ffffff);
}

.map-legend__radio:checked + .map-legend__radio-visual {
    border-color: var(--accent, #1a4d73);
}

.map-legend__radio:checked + .map-legend__radio-visual::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 10px;
    height: 10px;
    background: var(--accent, #1a4d73);
    border-radius: var(--radius-full, 9999px);
}

.map-legend__radio:focus + .map-legend__radio-visual {
    outline: 2px solid var(--accent, #1a4d73);
    outline-offset: 2px;
}

/* ==========================================================================
   CUSTOM CHECKBOX VISUAL
   ========================================================================== */

.map-legend__checkbox-visual {
    width: 20px;
    height: 20px;
    border: 2px solid var(--border-strong, rgba(15, 23, 42, 0.2));
    border-radius: var(--radius-sm, 0.25rem);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all var(--transition-fast, 150ms ease-in-out);
    background: var(--surface-2, #ffffff);
}

.map-legend__checkbox:checked + .map-legend__checkbox-visual {
    background: var(--accent, #1a4d73);
    border-color: var(--accent, #1a4d73);
}

.map-legend__checkbox-visual i {
    color: var(--color-white, #ffffff);
    font-size: 12px;
    opacity: 0;
    transition: opacity var(--transition-fast, 150ms ease-in-out);
}

.map-legend__checkbox:checked + .map-legend__checkbox-visual i {
    opacity: 1;
}

.map-legend__checkbox:focus + .map-legend__checkbox-visual {
    outline: 2px solid var(--accent, #1a4d73);
    outline-offset: 2px;
}

/* ==========================================================================
   LAYER ICON
   ========================================================================== */

.map-legend__icon {
    font-size: var(--font-size-lg, 1.125rem);
    flex-shrink: 0;
    width: 18px;
    text-align: center;
}

/* ==========================================================================
   LABELS
   ========================================================================== */

.map-legend__label {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: var(--space-1, 0.25rem);
    min-width: 0;
}

.map-legend__label-primary {
    font-weight: var(--font-weight-medium, 500);
    color: var(--text-primary, #212529);
    font-size: var(--font-size-sm, 0.875rem);
    line-height: 1.3;
}

.map-legend__label-secondary {
    font-size: var(--font-size-xs, 0.75rem);
    color: var(--text-secondary, #6c757d);
    line-height: 1.3;
}

/* Base map grid options: compact toggle-button style */
.map-legend__options[role='radiogroup'] .map-legend__option {
    justify-content: center;
    padding: var(--space-2, 0.5rem);
    min-height: 36px;
    gap: var(--space-2, 0.5rem);
}

.map-legend__options[role='radiogroup'] .map-legend__radio-visual {
    display: none;
}

.map-legend__options[role='radiogroup'] .map-legend__label-primary {
    font-size: var(--font-size-xs, 0.75rem);
    text-align: center;
}

/* ==========================================================================
   COLLAPSED STATE
   ========================================================================== */

.map-legend--collapsed .map-legend__body {
    display: none;
}

.map-legend--collapsed .map-legend__collapse i {
    transform: rotate(180deg);
}

/* ==========================================================================
   RESPONSIVE ADJUSTMENTS
   ========================================================================== */

@media (max-width: 768px) {
    .map-legend {
        width: 250px;
    }

    .map-legend-control {
        top: 12px;
        right: 12px;
    }

    .map-legend__header {
        padding: 0.625rem 0.75rem;
    }

    .map-legend__title {
        font-size: var(--font-size-sm, 0.875rem);
    }

    .map-legend__body {
        max-height: 320px;
    }

    .map-legend__section {
        padding: 0.625rem 0.75rem;
    }

    .map-legend__option {
        padding: var(--space-2, 0.5rem);
        gap: var(--space-2, 0.5rem);
        min-height: 38px;
    }

    .map-legend__label-primary {
        font-size: var(--font-size-xs, 0.75rem);
    }

    .map-legend__label-secondary {
        font-size: 0.625rem;
    }

    .map-legend__icon {
        font-size: var(--font-size-sm, 0.875rem);
        width: 16px;
    }

    .map-legend__checkbox-visual {
        width: 18px;
        height: 18px;
    }

    .map-legend__checkbox-visual i {
        font-size: 10px;
    }
}

@media (max-width: 480px) {
    .map-legend {
        width: calc(100vw - 24px);
        max-width: 280px;
    }

    .map-legend-control {
        top: 8px;
        right: 8px;
    }

    .map-legend__body {
        max-height: 240px;
    }

    .map-legend__option {
        padding: 0.375rem 0.5rem;
        min-height: 34px;
    }

    .map-legend__options[role='radiogroup'] .map-legend__option {
        min-height: 30px;
        padding: 0.25rem 0.375rem;
    }
}

/* ==========================================================================
   DARK MODE OVERRIDES
   ========================================================================== */

[data-theme='dark'] .map-legend {
    box-shadow: var(--shadow-lg, 0 10px 15px -3px rgba(0, 0, 0, 0.1));
    background: var(--surface-2, #161a1f);
    border-color: var(--border-subtle, rgba(255, 255, 255, 0.06));
}

[data-theme='dark'] .map-legend__header {
    background: var(--surface-3, #232b36);
    border-bottom-color: var(--border-subtle, rgba(255, 255, 255, 0.06));
}

[data-theme='dark'] .map-legend__title {
    color: var(--text-primary, #e7edf3);
}

[data-theme='dark'] .map-legend__collapse {
    color: var(--text-secondary, #94a3b8);
}

[data-theme='dark'] .map-legend__collapse:hover {
    color: var(--text-primary, #e7edf3);
    background: var(--nav-hover, #232b36);
}

[data-theme='dark'] .map-legend__section {
    border-bottom-color: var(--border-subtle, rgba(255, 255, 255, 0.06));
}

[data-theme='dark'] .map-legend__section-title {
    color: var(--text-secondary, #94a3b8);
}

[data-theme='dark'] .map-legend__action-btn {
    border-color: var(--border-subtle, rgba(255, 255, 255, 0.06));
    color: var(--text-secondary, #94a3b8);
}

[data-theme='dark'] .map-legend__action-btn:hover {
    background: var(--nav-hover, #232b36);
    color: var(--text-primary, #e7edf3);
    border-color: var(--border-strong, rgba(255, 255, 255, 0.12));
}

[data-theme='dark'] .map-legend__option {
    background: var(--surface-2, #161a1f);
    border-color: var(--border-subtle, rgba(255, 255, 255, 0.06));
}

[data-theme='dark'] .map-legend__option:hover {
    background: var(--surface-3, #232b36);
    border-color: var(--border-strong, rgba(255, 255, 255, 0.12));
}

[data-theme='dark'] .map-legend__option--selected,
[data-theme='dark'] .map-legend__option--checked {
    background: var(--accent-soft, rgba(43, 110, 168, 0.18));
    border-color: var(--accent, #2b6ea8);
}

[data-theme='dark'] .map-legend__radio-visual,
[data-theme='dark'] .map-legend__checkbox-visual {
    background: var(--surface-3, #232b36);
    border-color: var(--border-strong, rgba(255, 255, 255, 0.12));
}

[data-theme='dark'] .map-legend__radio:checked + .map-legend__radio-visual {
    border-color: var(--accent, #2b6ea8);
}

[data-theme='dark'] .map-legend__radio:checked + .map-legend__radio-visual::after {
    background: var(--accent, #2b6ea8);
}

[data-theme='dark'] .map-legend__checkbox:checked + .map-legend__checkbox-visual {
    background: var(--accent, #2b6ea8);
    border-color: var(--accent, #2b6ea8);
}

[data-theme='dark'] .map-legend__label-primary {
    color: var(--text-primary, #e7edf3);
}

[data-theme='dark'] .map-legend__label-secondary {
    color: var(--text-secondary, #94a3b8);
}

[data-theme='dark'] .map-legend__body::-webkit-scrollbar-thumb {
    background: var(--border-strong, rgba(255, 255, 255, 0.12));
}

[data-theme='dark'] .map-legend__body::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary, #94a3b8);
}

/* ==========================================================================
   ACCESSIBILITY - REDUCED MOTION
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    .map-legend,
    .map-legend__option,
    .map-legend__collapse,
    .map-legend__action-btn,
    .map-legend__radio-visual,
    .map-legend__checkbox-visual,
    .map-legend__checkbox-visual i {
        transition-duration: 0.01ms !important;
    }

    .map-legend__option:hover {
        transform: none;
    }
}

/* ==========================================================================
   PRINT STYLES
   ========================================================================== */

@media print {
    .map-legend-control {
        display: none;
    }
}
