body,
html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: #000; /* Always black background */
    font-family: 'Silkscreen', sans-serif;
    color: #fff;
    touch-action: none;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: none; /* Remove transition effect */
}

/* Added for preloading background images */
.preload-images {
    display: none;
}

#gameContainer {
    position: relative;
    width: 100%;
    height: 100vh; /* Changed: Use full viewport height */
    max-width: 100vh; /* Maintain aspect ratio */
    overflow: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin: 0 auto; /* Center horizontally */
}

#gameCanvas {
    display: block;
    width: 100%;
    height: auto; /* Maintain aspect ratio */
    aspect-ratio: 1/1; /* Force square aspect ratio */
    margin: 0 auto;
    position: relative;
}

.ui-item {
    color: white;
    font-size: 16px;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
    margin: 0 10px;
}

canvas {
    display: block;
    width: 100%;
    height: 100%;
}

#startScreen,
#gameOverScreen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 10;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    box-sizing: border-box;
    text-align: center;
    border-radius: 16px;
}

#cosmicDefenders {
    margin-bottom: 0;
}

#miningDivision {
    margin-top: 0;
    color: rgb(37, 94, 218)
}

#startButton {
    animation: gentleFlash 2s ease-in-out infinite;
    margin-top: 5%;
}

@keyframes gentleFlash {
    0% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }

    100% {
        opacity: 1;
    }
}

#gameOverScreen {
    display: none;
}

h1 {
    font-size: clamp(1.5rem, 8vmin, 3rem);
    color: #ffffff;
    margin: 0.5em 0;
    line-height: 1.2;
    text-shadow: 1px 3px 0px #ff7b00;
    width: 100%;
    text-align: center;
    padding: 0 1rem;
    box-sizing: border-box;
}

h2 {
    font-size: clamp(1rem, 4vmin, 1.5rem);
    color: rgb(255, 255, 255);
    cursor: pointer;
    margin: 0.5em 0;
    line-height: 1.2;
    width: 100%;
    text-align: center;
    padding: 0 1rem;
    box-sizing: border-box;
}

#uiContainer {
    position: absolute;
    top: max(10px, env(safe-area-inset-top));
    left: max(10px, env(safe-area-inset-left));
    z-index: 5;
    background-color: rgba(0, 0, 0, 0.5);
    padding: 10px;
    border-radius: 5px;
    max-width: 45%;
    font-size: clamp(12px, 3vmin, 18px);
    backdrop-filter: blur(3px);
    display: none;
    /* Initially hide the UI container */
}

#score,
#lives,
#shield,
#gems {
    margin-bottom: 5px;
    white-space: nowrap;
}

.action-container {
    position: absolute;
    right: 25px;
    bottom: max(25px, env(safe-area-inset-bottom) + 10px);
    /* Move closer to bottom edge, respecting safe areas */
    pointer-events: auto;
}


@media (orientation: landscape) {
    #touchControls {
        padding: 0 max(20px, env(safe-area-inset-left)) 0 max(20px, env(safe-area-inset-right));
    }
}

@media (orientation: portrait) and (max-height: 600px) {
    .controlBtn {
        width: 35px;
        height: 35px;
        font-size: 16px;
    }

    .dpad-container {
        width: 105px;
        height: 105px;
    }

    #upBtn {
        left: 35px;
    }

    #rightBtn {
        left: 70px;
    }

    #downBtn {
        top: 70px;
        left: 35px;
    }

    #fireBtn {
        width: 60px;
        height: 60px;
        font-size: 24px;
    }

    #joystick-base {
        width: 100px;
        height: 100px;
    }

    #joystick-knob {
        width: 40px;
        height: 40px;
    }
}

@media (orientation: landscape) and (max-height: 450px) {

    #startScreen,
    #gameOverScreen {
        flex-direction: row;
        flex-wrap: wrap;
        align-content: center;
        padding: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left);
    }

    h1 {
        margin: 0.25em 0;
        font-size: clamp(1.2rem, 6vmin, 2.5rem);
    }

    h2 {
        width: auto;
        margin: 0.25em 10px;
        font-size: clamp(0.9rem, 3vmin, 1.3rem);
    }
}

/* Make game container fullscreen on mobile */
@media (max-width: 768px) {
    #gameContainer {
        width: 100%;
        height: 100vh; /* Full viewport height */
        max-width: 100%;
        max-height: 100%;
        border: none;
        border-radius: 0;
    }

    #bottomUI {
        border-radius: 0;
    }
}


/* Add top UI bar for mobile devices - remove orange background */
#topUI {
    position: absolute;
    top: env(safe-area-inset-top, 0px);
    left: 0;
    width: 100%;
    height: 40px;
    background-color: transparent;
    /* Changed from #ff7b00 to transparent */
    display: none;
    flex-direction: row;
    justify-content: space-evenly;
    align-items: center;
    border-bottom: none;
    /* Removed border */
    padding: 5px 0;
    box-sizing: border-box;
    z-index: 6;
}

/* Make the UI items in the top bar more visible without the background */
#topUI .ui-item {
    font-size: clamp(10px, 3vw, 14px);
    margin: 0 2px;
    white-space: nowrap;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
    /* Enhanced text shadow for better visibility */
    background-color: rgba(0, 0, 0, 0.4);
    /* Semi-transparent background for each item */
    padding: 4px 8px;
    /* Add padding for better readability */
    border-radius: 4px;
    /* Rounded corners */
}

/* Add pause button styling */
#pauseBtn {
    font-size: clamp(12px, 3vw, 16px);
    margin: 0 2px;
    cursor: pointer;
    background-color: rgba(0, 0, 0, 0.4);
    padding: 4px 8px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
    width: 30px;
    height: 30px;
}

#pauseBtn:hover,
#pauseBtn:active {
    color: #ff7b00;
    background-color: rgba(0, 0, 0, 0.6);
    /* Darker background on hover */
}

/* Pause menu styling */
#pauseMenu {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

#pauseMenu h2 {
    color: #ff7b00;
    margin-bottom: 20px;
}

#resumeBtn {
    background-color: #ff7b00;
    color: white;
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    margin-top: 10px;
    font-family: 'Silkscreen', sans-serif;
    border: none;
    font-size: 16px;
}


/* Add a class for game-active state */
body.game-active {
    /* Remove background-color change */
    color: #fff;
}

/* Make sure body takes full viewport height on mobile with no centering */
@media (max-width: 768px) {

    body,
    html {
        display: block;
        /* Remove flex centering on mobile */
        height: 100%;
        /* Ensure full height */
        width: 100%;
        margin: 0;
        padding: 0;
    }
}

/* Make sure the top UI respects safe areas on notched devices */
#topUI {
    position: absolute;
    top: env(safe-area-inset-top, 0px);
    /* Use safe area inset if available */
    left: 0;
    width: 100%;
    height: 40px;

}

/* Update the game canvas to properly fill available space accounting for top UI */
@media (max-width: 768px) {
    #gameCanvas {
        width: 100%;
        height: auto; /* Let height auto-adjust based on width */
        aspect-ratio: 1/1; /* Keep square */
        margin-top: env(safe-area-inset-top, 0px);
    }

    /* Override any potential max-width/height limitations */
    #gameContainer {
        width: 100% !important;
        height: 100vh !important; /* Full viewport height */
        max-width: 100vh !important; /* Limit width to viewport height for square */
        max-height: none !important;
        border: none;
        border-radius: 0;
        display: flex;
        justify-content: center;
        align-items: center;
    }
}

/* Audio control styling */
.audio-controls {
    margin-bottom: 20px;
    width: 80%;
    max-width: 300px;
}

.audio-control {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    color: white;
}

.toggle-btn {
    background-color: #666;
    color: white;
    border: none;
    padding: 5px 15px;
    border-radius: 15px;
    cursor: pointer;
    font-family: 'Silkscreen', sans-serif;
    transition: background-color 0.3s;
    width: 60px;
}

.toggle-btn.active {
    background-color: #ff7b00;
}

input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 60%;
    height: 8px;
    background: #666;
    border-radius: 4px;
    outline: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 20px;
    height: 20px;
    background: #ff7b00;
    border-radius: 50%;
    cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    background: #ff7b00;
    border-radius: 50%;
    cursor: pointer;
    border: none;
}

/* Shop menu styling */
#shopMenu {
    position: absolute;
    font-family: 'Silkscreen', sans-serif;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 200;
    padding: 20px;
    padding-top: 60px;
    box-sizing: border-box;
}

#shopMenu h2 {
    color: #ff7b00;
    margin-bottom: 5px;
}

#shopGemCount {
    color: #1f771f;
    font-size: 1.2em;
    margin-bottom: 20px;
}

.shop-items {
    width: 80%;
    max-width: 400px;
    display: flex;
    flex-direction: column;
    gap: 15px;
    margin-bottom: 20px;
}

.shop-item {
    background-color: rgba(0, 0, 0, 0.669);
    border: 1px solid rgb(255, 255, 255);
    border-radius: 5px;
    padding: 10px;
    display: flex;
    flex-direction: column;
}

.item-name {
    color: white;
    font-size: 1.1em;
    margin-bottom: 5px;
}

.item-description {
    color: #ccc;
    font-size: 0.9em;
    margin-bottom: 10px;
}

.shop-menu-btn {
    color: white;
    border: white 1px solid;
    background-color: rgba(9, 255, 0, 0.67);
    border-radius: 5px;
    padding: 8px;
    cursor: pointer;
    font-family: 'Silkscreen', sans-serif;
    font-size: 0.9em;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.shop-menu-btn.disabled {
    color: white;
    border: white 1px solid;
    background-color: rgba(9, 255, 0, 0.67);
    border-radius: 5px;
    padding: 8px;
    cursor: pointer;
    font-family: 'Silkscreen', sans-serif;
    font-size: 0.9em;
    display: flex;
    justify-content: space-between;
    align-items: center;
}


.shop-menu-btn.disabled:hover {
    background-color: #860808;
    opacity: 0.7;
    cursor: not-allowed;
}

.item-status {
    color: #00ff00;
}

#closeShopBtn {
    background-color: #444;
    color: white;
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    font-family: 'Silkscreen', sans-serif;
    border: none;
    font-size: 16px;
    margin-top: 10px;
}

/* Tracer rocket button styles */
#tracerBtn {
    background-color: rgba(255, 69, 0, 0.6);
    width: 50px;
    height: 50px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    user-select: none;
    -webkit-touch-callout: none;
    font-size: 20px;
    color: white;
    border: 3px solid rgba(255, 255, 255, 0.6);
    margin-top: 10px;
    position: relative;
}

#tracersMobile {
    position: absolute;
    top: 0;
    right: 0;
    background: rgba(0,0,0,0.5);
    border-radius: 50%;
    width: 20px;
    height: 20px;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Shop button in top UI */
#shopBtn {
    font-size: clamp(12px, 3vw, 16px);
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    width: 30px;
    height: 30px;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    position: relative;
    z-index: 10;  /* Ensure it's above other elements */
}

/* Make shop button more visible when active/hovered */
#shopBtn:hover, #shopBtn:active {
    color: rgba(255, 123, 0, 1);
}

/* Mobile UI adjustments */
@media (max-width: 768px) {
    /* Change back to flex */
    body, html {
        display: flex;
        justify-content: center;
        align-items: center;
    }
    
    .shop-items {
        width: 90%;
        gap: 10px;
    }
    
    .shop-item {
        padding: 8px;
    }
    
    .item-name {
        font-size: 1em;
    }
    
    .item-description {
        font-size: 0.8em;
    }
}

/* Mobile unsupported message styling */
#mobileUnsupportedMessage {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #000;
    color: #fff;
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 2000; /* Higher than any other element */
    text-align: center;
    padding: 20px;
    box-sizing: border-box;
    font-family: 'Silkscreen', sans-serif;
}

#mobileUnsupportedMessage h1 {
    color: #ff7b00;
    margin-bottom: 20px;
    font-size: clamp(1.5rem, 8vmin, 3rem);
}

#mobileUnsupportedMessage p {
    font-size: clamp(1rem, 4vmin, 1.5rem);
    margin: 10px 0;
    max-width: 80%;
    color: white;
}

/* Make sure mobile message respects safe areas */
@supports (padding: max(0px)) {
    #mobileUnsupportedMessage {
        padding: max(20px, env(safe-area-inset-top))
                max(20px, env(safe-area-inset-right))
                max(20px, env(safe-area-inset-bottom))
                max(20px, env(safe-area-inset-left));
    }
}