:root {
    --bg-color: #87CEEB;
    --text-color: #333;
    --font-family: 'Courier New', Courier, monospace;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background-color: #222;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    overflow: hidden;
    font-family: var(--font-family);
    /* Mobile optimizations */
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
    touch-action: none;
    /* Safe area for notched devices (iPhone X+, etc) */
    /* CRITICAL: Applied to body, NOT #game-container to avoid breaking canvas calculations */
    padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
}

#game-container {
    position: relative;
    width: 100%;
    max-width: 1920px;
    aspect-ratio: 16 / 9;
    background-color: #000;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}

canvas {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    -ms-interpolation-mode: nearest-neighbor;
    /* Mobile touch prevention */
    touch-action: none;
}

#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: auto;
}

.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: rgba(255, 255, 255, 0.8);
    transition: opacity 0.5s;
    pointer-events: auto;
}

.hidden {
    opacity: 0;
    pointer-events: none;
}

.active {
    opacity: 1;
    pointer-events: auto;
}

h1 {
    font-size: 4rem;
    margin-bottom: 1rem;
    color: #FFD700;
    text-shadow: 2px 2px #000;
}

h2 {
    font-size: 2.5rem;
    margin-bottom: 1rem;
    color: #FFD700;
    text-shadow: 2px 2px #000;
}

p {
    font-size: 1.5rem;
    margin-bottom: 2rem;
}

button {
    font-size: 2rem;
    padding: 15px 40px;
    min-width: 200px;
    min-height: 60px;
    cursor: pointer;
    background-color: #FF6347;
    color: white;
    border: 4px solid #fff;
    font-family: var(--font-family);
    text-transform: uppercase;
    transition: all 0.2s ease;
}

button:hover {
    background-color: #FF4500;
}

button:active {
    transform: scale(0.95);
    opacity: 0.8;
}

#hud {
    background: none;
    justify-content: flex-start;
    align-items: flex-start;
    padding: 20px;
    flex-direction: row;
}

#hearts {
    font-size: 2rem;
    font-weight: bold;
    color: #fff;
    text-shadow: 2px 2px #000;
}

#score {
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 2rem;
    font-weight: bold;
    color: #fff;
    text-shadow: 2px 2px #000;
}

#sound-toggle {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 2.5rem;
    cursor: pointer;
    pointer-events: auto;
    user-select: none;
    z-index: 10;
    padding: 10px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 8px;
    transition: all 0.2s ease;
}

#sound-toggle:active {
    transform: scale(0.9);
}

/* Orientation Warning */
#orientation-warning {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.95);
    color: white;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    text-align: center;
    padding: 20px;
}

/* Show/hide controls based on device */
.desktop-controls {
    display: block;
}

.mobile-controls {
    display: none;
}

/* Portrait mode warning */
@media screen and (orientation: portrait) and (max-width: 768px) {
    #orientation-warning {
        display: flex;
    }

    #game-canvas {
        display: none;
    }
}

/* Mobile responsive */
@media (max-width: 960px) {

    html,
    body {
        height: 100vh;
        /* Legacy fallback */
        height: 100dvh;
        /* Dynamic viewport height - best for mobile */
    }

    body {
        position: fixed;
        width: 100%;
        background-color: #000;
        /* Black letterboxing for non-16:9 screens */
    }

    #game-container {
        width: 100vw;
        height: 56.25vw;
        /* Pre-calculated: 100vw * (9/16) for 16:9 ratio */
        min-height: 300px;
        /* Defensive: prevent collapse if dvh calculates to 0 */
        max-height: 100vh;
        /* Legacy fallback */
        max-height: 100svh;
        /* Small viewport - ensures visibility with UI */
        max-height: 100dvh;
        /* Dynamic viewport - best experience */
        max-width: none;
        aspect-ratio: 16 / 9;
        border-radius: 0;
        box-shadow: none;
    }

    h1 {
        font-size: 2.5rem;
    }

    h2 {
        font-size: 1.8rem;
    }

    p {
        font-size: 1.2rem;
        margin-bottom: 1.5rem;
    }

    button {
        font-size: 1.5rem;
        padding: 12px 30px;
        min-width: 180px;
        min-height: 50px;
    }

    #sound-toggle {
        font-size: 2rem;
        top: 10px;
        right: 10px;
        padding: 8px;
    }

    .desktop-controls {
        display: none;
    }

    .mobile-controls {
        display: block;
    }
}

/* Tablet optimization - BUILDS ON mobile styles above (768-960px devices get BOTH) */
@media (min-width: 768px) and (max-width: 960px) and (orientation: landscape) {
    #game-container {
        max-width: 768px;
        /* Cap width for better visuals on tablets */
        margin: 0 auto;
        box-shadow: 0 0 30px rgba(0, 0, 0, 0.7);
        /* Restore shadow */
    }
}

/* Fallback for iOS 12-14 and other browsers without aspect-ratio support */
@supports not (aspect-ratio: 16 / 9) {
    @media (max-width: 960px) {
        #game-container {
            height: 56.25vw;
            /* Fallback: 16:9 aspect ratio */
            max-height: 100vh;
        }
    }
}