:root {
    --bg: #0f172a;
    --panel: #1e293b;
    --text: #f1f5f9;
    --muted: #cbd5e1;
    --line: #334155;
    --accent: #60a5fa;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
}

.skip-link {
    position: absolute;
    left: -9999px;
    top: auto;
    width: 1px;
    height: 1px;
    overflow: hidden;
}

.skip-link:focus-visible {
    left: 1rem;
    top: 1rem;
    width: auto;
    height: auto;
    padding: 0.5rem 0.75rem;
    background: var(--panel);
    color: var(--text);
    border: 2px solid var(--accent);
    border-radius: 6px;
    z-index: 1000;
}

.visually-hidden {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.page {
    max-width: 1200px;
    margin: 0 auto;
    padding: 1rem;
}

.header {
    margin-bottom: 1rem;
}

.header-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
}

.back-link {
    color: var(--accent);
    text-decoration: none;
    font-weight: 600;
}

.back-link:hover {
    text-decoration: underline;
}

.study-link {
    color: #10b981;
    text-decoration: none;
    font-weight: 600;
    font-size: 0.9rem;
}

.study-link:hover {
    text-decoration: underline;
}

a:focus-visible,
input:focus-visible,
button:focus-visible,
.tab-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.layout {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
}

.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 1rem;
}

h1,
h2 {
    margin: 0 0 0.75rem;
}

p {
    margin: 0.25rem 0 0.75rem;
    color: var(--muted);
}

.daily-status {
    font-size: 0.92rem;
}

.hint {
    font-size: 0.92rem;
}

.error {
    color: #fca5a5;
}

.success {
    color: #86efac;
}

form {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.5rem;
    align-items: center;
}

input,
button {
    font: inherit;
    padding: 0.65rem 0.75rem;
    border-radius: 6px;
    border: 1px solid var(--line);
}

input {
    background: #0b1220;
    color: var(--text);
}

button {
    background: var(--accent);
    color: #0b1220;
    font-weight: 700;
    border: none;
    cursor: pointer;
    white-space: nowrap;
}

.btn-secondary {
    background: var(--bg-tertiary, #334155);
    color: var(--text);
    border: 1px solid var(--line);
    font-weight: 600;
}

.btn-secondary:hover {
    background: var(--border-light, #475569);
}

.actions {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.75rem;
}

.actions button {
    flex: 1;
    padding: 0.5rem;
    font-size: 0.85rem;
}

.map-container {
    width: 100%;
    height: auto;
    min-height: 400px;
    background: #0b1220;
    border: 1px solid var(--line);
    border-radius: 6px;
    box-shadow: 0 10px 25px rgba(2, 6, 23, 0.35);
}

.municipality {
    fill: var(--bg-tertiary, #334155);
    stroke: var(--line);
    stroke-width: 0.5;
    transition: fill 0.25s ease, stroke 0.25s ease;
}

.municipality:hover {
    fill: var(--border-light, #475569);
}

.municipality.guessed {
    stroke-width: 1;
    animation: guessReveal 250ms ease-out;
}

.municipality.correct {
    fill: #166534;
    stroke: #22c55e;
}

.municipality.very-near {
    fill: #22c55e;
}

.municipality.near {
    fill: #65a30d;
}

.municipality.warm {
    fill: #eab308;
}

.municipality.cool {
    fill: #f59e0b;
}

.municipality.far {
    fill: #f97316;
}

.municipality.very-far {
    fill: #ea580c;
}

.municipality.distant {
    fill: #ef4444;
}

.municipality.extreme {
    fill: #7f1d1d;
}

@keyframes guessReveal {
    from {
        transform: scale(0.995);
        opacity: 0.85;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

.legend {
    margin-top: 1rem;
    padding: 0.55rem 0.6rem;
    background: var(--bg-tertiary, #334155);
    border-radius: 6px;
    border: 1px solid var(--line);
}

.legend-title {
    font-size: 0.82rem;
    font-weight: 600;
    margin-bottom: 0.35rem;
    color: var(--text);
}

.legend-items {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.28rem 0.5rem;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    min-width: 0;
}

.legend-color {
    width: 14px;
    height: 10px;
    border-radius: 3px;
    border: 1px solid var(--line);
    flex: 0 0 auto;
}

.legend-color.correct {
    background: #166534;
}

.legend-color.very-near {
    background: #22c55e;
}

.legend-color.near {
    background: #65a30d;
}

.legend-color.warm {
    background: #eab308;
}

.legend-color.cool {
    background: #f59e0b;
}

.legend-color.far {
    background: #f97316;
}

.legend-color.very-far {
    background: #ea580c;
}

.legend-color.distant {
    background: #ef4444;
}

.legend-color.extreme {
    background: #7f1d1d;
}

.legend-label {
    font-size: 0.74rem;
    line-height: 1.1;
    color: var(--muted);
}

.guess-list {
    margin: 0;
    padding-left: 1.25rem;
}

/* Stats section */
.stats-panel {
    background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
    border: 1px solid var(--accent);
}

.stats-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}

.stats-header h2 {
    margin: 0;
}

.reset-stats-btn {
    background: none;
    border: 1px solid var(--muted);
    color: var(--muted);
    cursor: pointer;
    padding: 0.4rem 0.6rem;
    font-size: 1rem;
    border-radius: 4px;
    transition: all 0.2s;
}

.reset-stats-btn:hover {
    background: rgba(255, 255, 255, 0.05);
    color: var(--accent);
    border-color: var(--accent);
}

.reset-stats-btn:active {
    transform: scale(0.95);
}

.stats-container {
    display: flex;
    justify-content: space-around;
    align-items: center;
    gap: 1rem;
}

.stat-item {
    text-align: center;
    flex: 1;
}

.stat-value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--accent);
    margin-bottom: 0.25rem;
}

.stat-label {
    font-size: 0.85rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Share button */
.btn-share {
    background: #10b981;
    color: white;
    font-weight: 600;
}

.btn-share:hover {
    background: #059669;
}

/* Shared game indicator */
.shared-indicator {
    margin-top: 0.5rem;
    padding: 0.5rem 1rem;
    background: #8b5cf6;
    color: white;
    border-radius: 6px;
    text-align: center;
    font-weight: 600;
    font-size: 0.9rem;
}

/* Leaderboard section */
.leaderboard-panel {
    background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
    border: 1px solid #10b981;
}

.leaderboard-tabs {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
    border-bottom: 2px solid var(--line);
    padding-bottom: 0.5rem;
}

.tab-btn {
    flex: 1;
    padding: 0.5rem 0.75rem;
    background: transparent;
    color: var(--muted);
    border: none;
    border-radius: 6px 6px 0 0;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s ease;
}

.tab-btn:hover {
    background: #1e293b;
    color: var(--text);
}

.tab-btn.active {
    background: #10b981;
    color: white;
    font-weight: 600;
}

.leaderboard-content {
    min-height: 200px;
    max-height: 400px;
    overflow-y: auto;
}

.leaderboard-content:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 3px;
    border-radius: 6px;
}

.leaderboard-loading {
    text-align: center;
    padding: 2rem;
    color: var(--muted);
}

.leaderboard-table {
    width: 100%;
    border-collapse: collapse;
}

.leaderboard-table th {
    text-align: left;
    padding: 0.5rem;
    border-bottom: 2px solid var(--line);
    color: var(--accent);
    font-size: 0.85rem;
    font-weight: 600;
}

.leaderboard-table td {
    padding: 0.65rem 0.5rem;
    border-bottom: 1px solid var(--line);
    font-size: 0.9rem;
}

.leaderboard-table tr:hover {
    background: #1e293b;
}

.leaderboard-table .rank {
    width: 40px;
    text-align: center;
    font-weight: 700;
    color: var(--accent);
}

.leaderboard-table .player {
    font-weight: 600;
}

.leaderboard-table .score {
    text-align: right;
    color: #10b981;
}

.leaderboard-table .detail {
    font-size: 0.8rem;
    color: var(--muted);
}

.leaderboard-empty {
    text-align: center;
    padding: 2rem;
    color: var(--muted);
}

.name-prompt {
    background: #1e3a5f;
    border: 2px solid var(--accent);
    border-radius: 8px;
    padding: 1.5rem;
    margin-top: 1rem;
}

.name-prompt h3 {
    margin: 0 0 1rem;
    color: var(--accent);
}

.name-prompt input {
    width: 100%;
    margin-bottom: 0.75rem;
}

.name-prompt-actions {
    display: flex;
    gap: 0.5rem;
}

.name-prompt-actions button {
    flex: 1;
}

@media (min-width: 980px) {
    .layout {
        grid-template-columns: 340px 1fr;
        grid-template-rows: auto 1fr auto;
        grid-template-areas:
            'guess map'
            'list map'
            'stats leaderboard';
    }

    .layout .panel:nth-child(1) {
        grid-area: stats;
    }

    .layout .panel:nth-child(2) {
        grid-area: leaderboard;
    }

    /* Guess panel: shrink to fit its content */
    .layout .panel:nth-child(3) {
        grid-area: guess;
        align-self: start;
    }

    .layout .panel:nth-child(4) {
        grid-area: map;
    }

    /* List panel: fill remaining vertical space and scroll internally */
    .layout .panel:nth-child(5) {
        grid-area: list;
        display: flex;
        flex-direction: column;
        min-height: 0;
    }

    .layout .panel:nth-child(5) h2 {
        flex-shrink: 0;
    }

    .layout .panel:nth-child(5) .guess-list {
        flex: 1;
        overflow-y: auto;
        min-height: 0;
    }
}

@media (max-width: 979px) {

    /* Mobile-first flow: gameplay first, meta panels after */
    .layout .panel:nth-child(3) {
        order: 1;
    }

    .layout .panel:nth-child(4) {
        order: 2;
    }

    .layout .panel:nth-child(5) {
        order: 3;
    }

    .layout .panel:nth-child(1) {
        order: 4;
    }

    .layout .panel:nth-child(2) {
        order: 5;
    }

    .legend-items {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.3rem 0.45rem;
    }

    .legend-item {
        min-width: 0;
    }

    .map-container {
        min-height: 300px;
    }

    .actions button {
        font-size: 0.8rem;
        padding: 0.5rem 0.4rem;
    }
}