*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background: #1a1a2e;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100vw;
    height: 100svh;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    overflow: hidden;
    touch-action: none;
}

#game-container {
    position: relative;
    /* Always maintain 2:3 portrait ratio, fit inside viewport */
    aspect-ratio: 2 / 3;
    height: min(600px, 100svh);
    width: min(400px, calc(100svh * 2 / 3), 100vw);
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 24px 64px rgba(0, 0, 0, 0.6), 0 0 0 2px rgba(255, 255, 255, 0.08);
}

canvas {
    display: block;
    width: 100%;
    height: 100%;
}

.screen {
    position: absolute;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(4px);
    z-index: 10;
    animation: fadeIn 0.2s ease;
}

.screen.active {
    display: flex;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

.screen-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    padding: 36px 40px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 20px;
    backdrop-filter: blur(8px);
    text-align: center;
    min-width: 260px;
    animation: slideUp 0.25s ease;
    color: #fff;
}

@keyframes slideUp {
    from {
        transform: translateY(20px);
        opacity: 0;
    }

    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.game-title {
    font-size: 2.8rem;
    font-weight: 800;
    color: #fff;
    letter-spacing: -1px;
    text-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
    line-height: 1;
}

.gameover-title {
    font-size: 2rem;
    font-weight: 700;
    color: #ff6b6b;
    text-shadow: 0 2px 8px rgba(255, 107, 107, 0.4);
}

.section-title {
    font-size: 1.8rem;
    font-weight: 700;
    color: #fff;
}

.best-score-display {
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    padding: 8px 20px;
}

.score-panel {
    width: 100%;
    background: rgba(0, 0, 0, 0.25);
    border-radius: 12px;
    padding: 16px 24px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.score-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.label {
    font-size: 0.85rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: rgba(255, 255, 255, 0.6);
}

#start-best-score,
#final-score,
#gameover-best-score {
    font-size: 1.5rem;
    font-weight: 800;
    color: #f9c74f;
}

.btn-primary {
    padding: 14px 40px;
    font-size: 1rem;
    font-weight: 700;
    color: #1a1a2e;
    background: linear-gradient(135deg, #f9c74f, #f4a261);
    border: none;
    border-radius: 50px;
    cursor: pointer;
    transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
    box-shadow: 0 4px 16px rgba(249, 199, 79, 0.4);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(249, 199, 79, 0.5);
}

.btn-primary:active {
    transform: translateY(1px);
    filter: brightness(0.9);
}

.hint {
    font-size: 0.8rem;
    color: rgba(255, 255, 255, 0.45);
}

#hud {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    padding: 14px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    z-index: 5;
    pointer-events: none;
}

#hud.hidden {
    display: none;
}

#score-display {
    font-size: 2.2rem;
    font-weight: 900;
    color: #fff;
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5), 0 0 0 2px rgba(0, 0, 0, 0.3);
    letter-spacing: -1px;
    line-height: 1;
}

.hud-controls {
    display: flex;
    gap: 8px;
    pointer-events: all;
}

.icon-btn {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.35);
    backdrop-filter: blur(4px);
    cursor: pointer;
    font-size: 0.9rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s ease, transform 0.1s ease;
    color: #fff;
}

.icon-btn:hover {
    background: rgba(0, 0, 0, 0.55);
    transform: scale(1.1);
}

.icon-btn:active {
    transform: scale(0.95);
}

.loader {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-top-color: #f9c74f;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

#loading-screen p {
    color: rgba(255, 255, 255, 0.6);
    font-size: 0.9rem;
}

/* ── Responsive ── */

/* Small phones – full screen, no chrome */
@media (max-width: 440px) {
    #game-container {
        width: 100vw;
        height: 100svh;
        border-radius: 0;
        box-shadow: none;
    }
}

/* Landscape on phones – shrink to fit height */
@media (orientation: landscape) and (max-height: 500px) {
    #game-container {
        height: 100svh;
        width: calc(100svh * 2 / 3);
        border-radius: 0;
        box-shadow: none;
    }
}