* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
}

body {
    background-color: #111;
    color: white;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    overflow: hidden; /* Prevent scrolling */
    touch-action: manipulation; /* Allow only basic touch interactions, prevent zoom/swipe */
}

/* CSS Variables for responsive design */
:root {
    --scale-factor: 1;
    --is-mobile: 0;
    --is-portrait: 0;
    --hud-font-size: 18px;
    --stat-padding: 10px 20px;
    --hud-gap: 20px;
    --upgrade-font-size: 16px;
}

#gameCanvas {
    display: block;
    width: 100%;
    height: 100%;
    isolation: isolate; /* Creates separate stacking context to prevent UI interference */
}

#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    padding: 20px;
    isolation: isolate; /* Creates separate stacking context */
    z-index: 10; /* Ensure it stays above canvas */
}

.hud-top {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    width: 100%;
    font-size: var(--hud-font-size);
    font-weight: bold;
    text-shadow: 0 2px 4px rgba(0,0,0,0.5);
    gap: calc(var(--hud-gap) * 0.5);
    margin-bottom: var(--hud-gap);
}

.stat-box {
    background: rgba(0, 0, 0, 0.5);
    padding: var(--stat-padding);
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    font-size: inherit; /* Use parent font size */
    min-width: 0; /* Allow shrinking */
    flex: 1; /* Allow flexing */
    text-align: center;
}

.upgrades-panel {
    display: flex;
    justify-content: center;
    gap: var(--hud-gap);
    padding: 10px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    flex-wrap: wrap; /* Allow wrapping on small screens */
}

.upgrade-stat {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: var(--upgrade-font-size);
    font-weight: bold;
    text-shadow: 0 2px 4px rgba(0,0,0,0.5);
    background: rgba(0, 0, 0, 0.4);
    padding: 8px 15px;
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    transition: transform 0.2s, box-shadow 0.2s;
    min-width: 0; /* Allow shrinking */
}

.upgrade-stat:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.upgrade-icon {
    font-size: 20px;
}

.upgrade-label {
    color: #ccc;
}

.upgrade-value {
    color: #4ade80;
    font-weight: bold;
    min-width: 20px;
    text-align: center;
}

#start-screen, #game-over-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
    z-index: 10;
    backdrop-filter: blur(5px);
}

h1 {
    font-size: calc(48px * var(--scale-factor));
    margin-bottom: 10px;
    color: #4ade80;
    text-transform: uppercase;
    letter-spacing: 2px;
    text-align: center;
    max-width: 90vw; /* Prevent overflow on small screens */
}

p {
    font-size: calc(18px * var(--scale-factor));
    color: #ccc;
    margin-bottom: 30px;
    max-width: 600px;
    text-align: center;
    line-height: 1.5;
    padding: 0 20px; /* Add padding for mobile */
}

button {
    background: linear-gradient(135deg, #4ade80, #22c55e);
    border: none;
    padding: calc(15px * var(--scale-factor)) calc(40px * var(--scale-factor));
    font-size: calc(24px * var(--scale-factor));
    color: #003300;
    font-weight: bold;
    border-radius: 50px;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(74, 222, 128, 0.4);
    transition: transform 0.1s, box-shadow 0.1s;
    max-width: 90vw; /* Prevent overflow on small screens */
    /* Mobile-specific improvements */
    touch-action: manipulation; /* Ensure button is touchable */
    -webkit-tap-highlight-color: transparent; /* Remove default tap highlight */
    position: relative; /* Ensure proper stacking */
    z-index: 20; /* Higher z-index for buttons */
}

button:active {
    transform: scale(0.95);
    box-shadow: 0 2px 8px rgba(74, 222, 128, 0.2);
}

/* Mobile-specific button feedback */
@media (pointer: coarse) {
    button:hover {
        transform: none; /* Disable hover on touch devices */
    }
    
    button:active, button:focus {
        outline: 2px solid #fff; /* Add focus indicator for accessibility */
        outline-offset: 2px;
    }
}

.hidden {
    display: none !important;
}

.floating-text {
    position: absolute;
    font-weight: bold;
    pointer-events: none;
    animation: floatUp 1s ease-out forwards;
    text-shadow: 0 2px 2px rgba(0,0,0,0.8);
}

@keyframes floatUp {
    0% { opacity: 1; transform: translateY(0) scale(1); }
    100% { opacity: 0; transform: translateY(-50px) scale(1.2); }
}

/* Ad Container Styles */
.ad-container {
    margin: 20px 0;
    padding: 10px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    max-width: 300px;
    width: 100%;
    text-align: center;
    /* Ensure ads don't interfere with touch events */
    pointer-events: auto;
    position: relative;
    z-index: 5;
}

.title-ad {
    margin: 20px auto 30px auto;
}

.game-over-ad {
    margin: 20px auto 30px auto;
}

/* Mobile-specific styles */
@media (max-width: 768px) {
    #ui-layer {
        padding: 10px;
    }
    
    .hud-top {
        font-size: 14px;
        gap: 5px;
    }
    
    .stat-box {
        padding: 6px 10px;
        font-size: 12px;
        min-width: 60px;
    }
    
    .upgrades-panel {
        gap: 8px;
        padding: 8px;
    }
    
    .upgrade-stat {
        font-size: 11px;
        padding: 4px 8px;
        gap: 4px;
    }
    
    .upgrade-icon {
        font-size: 14px;
    }
    
    h1 {
        font-size: 32px;
    }
    
    p {
        font-size: 16px;
        padding: 0 15px;
    }
    
    button {
        font-size: 20px;
        padding: 12px 30px;
        min-height: 48px; /* Ensure minimum touch target size */
        min-width: 120px; /* Ensure minimum touch target size */
    }
    
    .ad-container {
        max-width: 250px;
        margin: 15px auto;
    }
}

@media (max-width: 480px) {
    #ui-layer {
        padding: 8px;
    }
    
    .hud-top {
        font-size: 12px;
        gap: 3px;
    }
    
    .stat-box {
        padding: 4px 8px;
        font-size: 10px;
        min-width: 50px;
    }
    
    .upgrades-panel {
        gap: 5px;
        padding: 6px;
    }
    
    .upgrade-stat {
        font-size: 10px;
        padding: 3px 6px;
        gap: 3px;
    }
    
    .upgrade-icon {
        font-size: 12px;
    }
    
    h1 {
        font-size: 28px;
    }
    
    p {
        font-size: 14px;
        padding: 0 10px;
    }
    
    button {
        font-size: 18px;
        padding: 10px 25px;
        min-height: 44px; /* Minimum touch target size for small screens */
        min-width: 100px; /* Minimum touch target size for small screens */
    }
    
    .ad-container {
        max-width: 200px;
        margin: 10px auto;
        padding: 8px;
    }
}

/* Portrait mode specific adjustments */
@media (max-height: 600px) and (orientation: portrait) {
    #ui-layer {
        padding: 8px;
    }
    
    .hud-top {
        gap: 3px;
    }
    
    .upgrades-panel {
        padding: 4px;
    }
    
    .stat-box, .upgrade-stat {
        padding: 3px 6px;
    }
}

/* Landscape mode specific adjustments */
@media (orientation: landscape) and (max-height: 500px) {
    #ui-layer {
        padding: 10px;
    }
    
    .hud-top {
        font-size: 12px;
        margin-bottom: 5px;
    }
    
    .upgrades-panel {
        padding: 6px;
    }
}