/* ═══════════════ MAP LEGEND — gd-nation-legend ═══════════════ */
.map-legend {
    position: absolute;
    bottom: 54px; left: 10px;
    background: rgba(10,14,23,0.93);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px var(--sp-3);
    z-index: 50;
    max-height: 50vh;
    overflow-y: auto;
    min-width: 165px;
    backdrop-filter: blur(8px);
    font-size: var(--fs-sm);
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
    touch-action: pan-y;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}
.map-legend::-webkit-scrollbar { width: 4px; }
.map-legend::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
.legend-header {
    display: flex; align-items: center; justify-content: space-between;
    margin-bottom: var(--sp-2);
    padding-bottom: var(--sp-1);
    border-bottom: 1px solid var(--border-subtle);
    flex-shrink: 0;
}
.legend-title {
    font-family: var(--font-title);
    font-size: var(--fs-xs);
    color: var(--accent);
    letter-spacing: 1.5px;
    text-transform: uppercase;
    font-weight: bold;
}
.legend-toggle {
    display: inline-flex;
    background: none; border: 1px solid var(--border); color: var(--text-dim);
    font-size: 0.7rem; width: 30px; height: 30px; border-radius: 5px;
    cursor: pointer; align-items: center; justify-content: center;
    transition: color 0.2s, border-color 0.2s;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}
.legend-toggle:hover { color: var(--accent); border-color: var(--accent); }
.map-legend.legend-collapsed .legend-item { display: none !important; }
.map-legend.legend-collapsed { max-height: none !important; overflow: hidden; }
.legend-item {
    display: flex; align-items: center; gap: var(--sp-2);
    padding: var(--sp-1) var(--sp-2); margin-bottom: 1px; border-radius: 5px;
    cursor: pointer; transition: background 0.15s;
}
.legend-item:hover { background: rgba(255,255,255,0.08); }
.legend-item.legend-player { background: rgba(0,229,255,0.1); }
.legend-item.legend-player:hover { background: rgba(0,229,255,0.2); }
.legend-item.legend-enemy { background: rgba(255,23,68,0.08); }
.legend-item.legend-enemy:hover { background: rgba(255,23,68,0.15); }
.legend-item.legend-ally { background: rgba(0,230,118,0.08); }
.legend-item.legend-ally:hover { background: rgba(0,230,118,0.15); }
.legend-item.legend-dead { opacity: 0.4; text-decoration: line-through; cursor: default; }
.legend-item.legend-minor { border-top: 1px solid var(--border-subtle, rgba(255,255,255,0.06)); margin-top: 2px; }
.legend-item.legend-minor:hover { background: rgba(255,255,255,0.06); }
.legend-minor-arrow { transition: transform 0.2s; }
.legend-item-minor { padding-left: var(--sp-4, 16px); font-size: 0.72rem; opacity: 0.85; }
.legend-item-minor:hover { background: rgba(255,255,255,0.08); opacity: 1; }
.legend-flag-img {
    width: 16px;
    height: 12px;
    object-fit: cover;
    border-radius: 2px;
    flex-shrink: 0;
    box-shadow: 0 0 0 1px rgba(255,255,255,0.14);
}
.legend-name {
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: 500;
}
.legend-count {
    font-family: var(--font-mono);
    color: var(--text-dim);
    font-size: var(--fs-xs);
    min-width: 16px; text-align: right;
}
.legend-pct {
    font-family: var(--font-mono);
    color: var(--text-muted);
    font-size: 0.6rem;
    min-width: 30px;
    text-align: right;
    opacity: 0.7;
}

/* Keep nations legend below autoplay/spectator toolbar (same behavior as events) */
#spectator-banner:not(.hidden) ~ #map-container #map-legend,
body.spectator-active #map-legend {
    transform: translateY(36px);
}

/* ── Mobile (≤768px) ── */
@media (max-width: 768px) {
    #spectator-banner:not(.hidden) ~ #map-container #map-legend,
    body.spectator-active #map-legend {
        top: 34px !important;
        transform: none;
    }

    .map-legend {
        top: 6px; bottom: 0; left: 0;
        max-height: none;
        min-width: 170px;
        max-width: min(30vw, 200px);
        padding: 10px 12px;
        border-radius: 0;
        border-left: none;
        border-top: none;
        border-bottom: none;
    }
    .legend-toggle { display: inline-flex; }
    .legend-title {
        font-size: 0.72rem;
        letter-spacing: 1.8px;
    }
    .legend-item {
        gap: 8px;
        padding: 7px 10px;
        min-height: 38px;
        border-radius: 8px;
    }
    .legend-flag-img { width: 22px; height: 16px; border-radius: 3px; }
    .legend-name { }
    .legend-count { min-width: 20px; }
}

@media (max-width: 480px) {
    #spectator-banner:not(.hidden) ~ #map-container #map-legend,
    body.spectator-active #map-legend {
        top: 38px !important;
    }
}

/* ── Landscape mobile ── */
@media (hover: none) and (pointer: coarse) and (orientation: landscape) and (max-height: 500px) {
    .map-legend {
        top: 0; bottom: 0; left: 0;
        min-width: 160px;
        max-width: min(25vw, 200px);
        max-height: none;
        padding: 10px 10px;
        border-radius: 0;
        border-left: none; border-top: none; border-bottom: none;
        border-radius: 0 12px 12px 0;

    }
    .legend-toggle { display: inline-flex; }
    .legend-title {
        font-size: 0.78rem;
        letter-spacing: 2px;
    }
    .legend-item {
        gap: 9px;
        padding: 8px 10px;
        min-height: 40px;
    }
    .legend-flag-img { width: 24px; height: 18px; }
    .legend-count { min-width: 20px; }
}
