/* === CSS VARIABLES === */
:root {
    --bg-stone: url('https://firebasestorage.googleapis.com/v0/b/sticky-20bca.appspot.com/o/sgm-asset%2F543b8e43-6949-498a-819e-fd35da91bb0e%2Fe9bb427e-98dc-4ca1-a7c2-6b49f6ea6488.png?alt=media&token=760a4c41-0617-4ae6-a173-04041bb25b89');
    --stroke-color: #192A27;
    --shadow-base: 0px 4px 4px 0px rgba(0, 0, 0, 0.25);
    --btn-primary-bg: #FFB450;
    --btn-primary-dark: #C26314;
    --btn-primary-light: #FFCA84;
    
    /* Button shadow colors */
    --shadow-light: var(--btn-primary-light);
    --shadow-dark: var(--btn-primary-dark);
}

/* === GLOBAL RESET === */
*, *:focus, *:active, button:focus, button:active {
    margin: 0; padding: 0; box-sizing: border-box;
    user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
    outline: none !important; -webkit-tap-highlight-color: transparent !important;
}

input, select, textarea { box-shadow: none !important; }

/* === UTILITY CLASSES === */
.text-stroke { -webkit-text-stroke: var(--stroke-width, 3px) var(--stroke-color); paint-order: stroke fill; }
.text-stroke-sm { --stroke-width: 2px; }
.text-stroke-md { --stroke-width: 3px; }
.text-stroke-lg { --stroke-width: 4px; }
.text-stroke-xl { --stroke-width: 10px; }

.btn-shadow { box-shadow: var(--shadow-base), 5px 0 0 var(--shadow-light) inset, 0 -6px 0 var(--shadow-dark) inset, 0 5px 0 var(--shadow-light) inset, -4px 0 0 var(--shadow-dark) inset; }
.btn-shadow-wave { --shadow-light: #FECB33; --shadow-dark: #E38018; }
.btn-shadow-easy { --shadow-light: #9BE0A6; --shadow-dark: #3A7944; }
.btn-shadow-medium { --shadow-light: #A7CDFF; --shadow-dark: #386EB4; }
.btn-shadow-hard { --shadow-light: #F7866C; --shadow-dark: #A73F28; }
.btn-shadow-mute { box-shadow: 0 6px 0 #4F5861 inset, var(--shadow-base); }
.bar-shadow-horizontal { box-shadow: 0 -4px 0 #621F28 inset, 0 4px 0 #F37C0E inset; }
.progress-shadow { box-shadow: var(--shadow-base), 0 -4px 0 #2A1509 inset, 0 4px 0 #8B5A1F inset; }
.progress-fill-shadow { box-shadow: 5px 0 0 var(--btn-primary-light) inset, 0 -6px 0 var(--btn-primary-dark) inset, 0 5px 0 var(--btn-primary-light) inset, -4px 0 0 var(--btn-primary-dark) inset; }

/* === BASE LAYOUT === */
body {
    font-family: 'Jersey 20', sans-serif; font-weight: 700;
    background-color: #0a2a0a; color: #f0e6d2; overflow: hidden;
    touch-action: manipulation; position: fixed; width: 100%; height: 100%;
}

button { font-family: 'Jersey 20', sans-serif; }

#gameContainer { position: relative; width: 100%; height: 100%; overflow: hidden; }

canvas {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    background-color: #1a3a1a; image-rendering: pixelated; image-rendering: crisp-edges;
    user-select: none; touch-action: none; cursor: default;
}
canvas.tower-placement { cursor: crosshair; }

#ui {
    position: absolute; top: 0; left: 0; right: 0; bottom: 0; z-index: 100;
    pointer-events: none; display: none;
}
#ui.visible { display: block; }

/* === REUSABLE COMPONENTS === */
.screen-bg { background: var(--bg-stone) repeat; }

.torch {
    position: fixed; width: 128px; height: 128px; bottom: 10%; z-index: 2;
    background: url('https://firebasestorage.googleapis.com/v0/b/sticky-20bca.appspot.com/o/sgm-asset%2F7951ccf7-6e7c-44d9-9e68-0fca97e02c79%2F9455ace0-b25d-4802-949f-1ba5b9bdb2d5.webp?alt=media&token=55a86295-581a-4a01-b7f4-a1b9f27d1f52') no-repeat;
    background-size: contain;
}
.torch.left { left: 0; }
.torch.right { right: 0; }

.torch-light { 
    position: fixed; width: 370px; height: 370px; border-radius: 50%; bottom: 4%;
    pointer-events: none; background: #FF6A00; mix-blend-mode: lighten; filter: blur(70px);
    animation: torch-flicker 1.2s infinite alternate;
}
.torch-light.left { left: -170px; }
.torch-light.right { right: -170px; }

.horizontal-bar {
    width: 100%; height: 10px; border-top: 2px solid #000; border-bottom: 2px solid #000;
    background: #B04D0E; position: absolute; left: 0;
}
.horizontal-bar.top { top: 0; }
.horizontal-bar.bottom { bottom: 0; }

.game-btn {
    font-family: 'Jersey 20', sans-serif; border: 3px solid var(--stroke-color); border-radius: 8px;
    cursor: pointer; transition: all 0.1s ease; text-align: center; display: flex;
    align-items: center; justify-content: center; pointer-events: auto;
    font-style: normal; font-weight: 400; line-height: normal;
}
.game-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.game-btn.primary { background: var(--btn-primary-bg); color: #593817; }

/* === GAME UI === */
#stats-header {
    position: absolute; top: 0; left: 0; right: 0; padding: 25px 80px;
    display: flex; justify-content: space-evenly; pointer-events: auto;
    background: var(--bg-stone) repeat; flex-direction: row; align-items: flex-start;
}
.stats-container { display: flex; align-items: center; align-self: center; }
.live-icon, .gold-icon { 
    width: 28px; height: 28px; flex-shrink: 0; background-size: contain; background-repeat: no-repeat; background-position: center;
}
.live-icon { 
    background-image: url('https://firebasestorage.googleapis.com/v0/b/sticky-20bca.appspot.com/o/sgm-asset%2Fb3bcea50-f8eb-4d00-9650-9ab141b797aa%2Feb787034-806d-47a2-abe2-d8b92c0f4c35.png?alt=media&token=37db268b-18be-4575-be77-96a9fb3b0b81');
    margin-right: 3px;
}
.gold-icon { 
    background-image: url('https://firebasestorage.googleapis.com/v0/b/sticky-20bca.appspot.com/o/sgm-asset%2Fabd2d124-3f1e-44bf-84b0-609869bae4d1%2Fb7d47655-63ef-4f3f-a687-0aafc7dc6e5a.png?alt=media&token=142b32e8-114a-4e19-a91f-fb45f4d30370');
}
.stats-text { color: #D9F5BF; text-align: center; font-size: 28px; }
#lives { color: #FF6C76; }
#gold { color: #FFC06B; }
#fps { position: absolute; bottom: -10px; translate: 0 100%; font-size: 12px; color: #D9F5BF; }

#tower-panel {
    position: absolute; bottom: 0; left: 0; right: 0;
    background: var(--bg-stone) repeat; pointer-events: auto;
}
#tower-controls { position: relative; display: flex; justify-content: center; align-items: center; padding: 20px 0 15px; }
#tower-buttons { display: flex; gap: 15px; align-items: center; justify-content: center; }

#start-wave-btn { 
    width: 110px; margin-left: 10px; flex-shrink: 0; font-size: 28px; background: #FBAF12;
}

@media (min-aspect-ratio: 1/1) {
    #start-wave-btn { position: absolute; top: 50%; transform: translateY(-50%); width: 180px; height: 50px; right: 20px; }
}
@media (max-aspect-ratio: 9/16) {
    #stats-header { padding-left: 30px; padding-right: 30px; }
}

.btn-outline {
    position: absolute;
    top: -4px;
    left: -4px;
    right: -4px;
    bottom: -4px;
    border-radius: 8px;
    border: 2px solid #FFF;
    box-shadow: 0px 3px 3px 0px rgba(0, 0, 0, 0.25);
    z-index: 0;
}

.tower-btn {
    position: relative;
    border: 2px solid #273A35;
    border-radius: 6px;
    cursor: grab;
    color: #f0e6d2;
    font-weight: 700;
    transition: all 0.2s ease;
    width: 71px;
    height: 90px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    text-align: center;
    user-select: none;
}

.tower-btn:active {
    cursor: grabbing;
}

.tower-btn:hover {
    transform: translateY(-2px);
}

.tower-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.tower-btn.selected {
    transform: scale(1.08);
    z-index: 10;
}

.tower-btn.selected .btn-outline {
    border-color: #FFD700;
    box-shadow: 0px 4px 8px 0px rgba(255, 215, 0, 0.4);
}

.tower-icon {
    width: 80%;
    aspect-ratio: 1/2;
    height: auto;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    margin-bottom: 4px;
    z-index: 1;
}

.tower-btn[data-tower="archer"] {
    background: #66AE71;
}

.tower-btn[data-tower="cannon"] {
    background: #C27359;
}

.tower-btn[data-tower="magic"] {
    background: #5D8DA7;
}

.tower-btn[data-tower="archer"] .tower-icon {
    background-image: url('https://firebasestorage.googleapis.com/v0/b/sticky-20bca.appspot.com/o/sgm-asset%2Fedd9561e-4b5c-4069-b5c7-df3bf7686df9%2F9e9f367d-cdca-44af-ad7c-42dc14342446.webp?alt=media&token=667dc1ef-7af4-48a4-a8cf-45c5c670ad7b');
}

.tower-btn[data-tower="cannon"] .tower-icon {
    background-image: url('https://firebasestorage.googleapis.com/v0/b/sticky-20bca.appspot.com/o/sgm-asset%2F68718bbd-4017-4637-ada7-3317e9cabef2%2Fbcdc7272-e2b6-47ef-bacd-658f635f9d7b.webp?alt=media&token=5bc29d26-b176-4967-b269-0a91b22fe377');
}

.tower-btn[data-tower="magic"] .tower-icon {
    background-image: url('https://firebasestorage.googleapis.com/v0/b/sticky-20bca.appspot.com/o/sgm-asset%2F612a5b4c-c345-4cfc-ae02-6c7190fff905%2Fbe28956e-f95f-4d1d-a530-6141e0e83822.webp?alt=media&token=e44a3a4e-a690-48a1-8890-884baf0e55e9');
}

.tower-cost {
    padding-top: 5px;
    padding-bottom: 5px;
    width: 71px;
    border-radius: 4px;
    border: 2px solid #273A35;
    background: #FFB450;
    color: #D9F5BF;
    text-align: center;
    font-size: 18px;
    font-style: normal;
    font-weight: 400;
    line-height: normal;
    z-index: 1;
}

#drag-status {
    text-align: center;
    padding: 8px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 4px;
    font-size: 12px;
}

#drag-status-text {
    color: #f0e6d2;
    text-shadow: 1px 1px 2px #000;
}

/* === DECORATIVE ELEMENTS === */

@keyframes torch-flicker {
    0% { opacity: 0.2; }
    20% { opacity: 0.3; }
    40% { opacity: 0.2; }
    60% { opacity: 0.25; }
    80% { opacity: 0.22; }
    100% { opacity: 0.2; }
}
/* Menu overlay styles */
.menu-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.8);
    z-index: 1000;
}

/* Pre-wave screen styles */
#pre-wave-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none; /* Hidden by default */
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.85);
    z-index: 500;
}

#pre-wave-screen.visible {
    display: flex; /* Show when visible */
}

#pre-wave-content {
    text-align: center;
    background: rgba(42, 90, 42, 0.9);
    padding: 40px;
    border-radius: 15px;
    border: 3px solid #c0a080;
    box-shadow: 0 0 20px rgba(192, 160, 128, 0.3);
}

#wave-title {
    font-size: 48px;
    color: #4ecdc4;
    margin-bottom: 30px;
    text-shadow: 2px 2px 4px #000;
    text-transform: uppercase;
    letter-spacing: 2px;
}

.menu-title {
    font-size: 32px;
    margin-bottom: 30px;
    color: #f0e6d2;
    text-align: center;
    text-shadow: 0 0 10px #55aa55;
}

.hidden {
    display: none !important;
}

/* Animation classes for game effects */
@keyframes pulse {
    0% {
        transform: scale(1);
        opacity: 1;
    }
    50% {
        transform: scale(1.1);
        opacity: 0.8;
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

.pulse {
    animation: pulse 1s infinite;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.fade-in {
    animation: fadeIn 0.3s ease-out;
}

/* Mute button styles */
#mute-btn {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 3000;
    width: 40px;
    height: 40px;
    border-radius: 41px;
    border: 3px solid #192028;
    background: #444D55;
    display: flex;
    align-items: center;
    justify-content: center;
}

#mute-icon {
    display: block;
    width: 90%;
    height: 90%;
    padding: 5%;
    box-sizing: border-box;
    object-fit: contain;
    margin: 0 auto;
}

#mute-btn:hover {
    background: #3d444b;
    transform: scale(1.1);
}

#mute-icon.on {
    content: url('https://firebasestorage.googleapis.com/v0/b/sticky-20bca.appspot.com/o/sgm-asset%2Fb70b3b5c-16e7-47e5-a686-00202af9e928%2F8961f353-285c-42b3-8e62-09dca2db6962.png?alt=media&token=c7a4c542-f66d-41f4-b30d-76a50563e8cf');
}

#mute-icon.off {
    content: url('https://firebasestorage.googleapis.com/v0/b/sticky-20bca.appspot.com/o/sgm-asset%2Faafd22d7-c1d0-44b5-9f41-92b452f790e2%2F61a87051-680b-4980-9139-b08dab69e3fa.png?alt=media&token=084ff565-eeae-4c3b-a8d3-69a7a3db1369');
}

/* Start screen styles */
#start-screen {
    position: fixed;
    z-index: 2000;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: var(--bg-stone) repeat;
    color: #fff;
    text-align: center;
}

/* Game Over screen styles */
#game-over-screen {
    position: fixed;
    z-index: 2000;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: var(--bg-stone) repeat;
    color: #fff;
    text-align: center;
}

#game-over-screen.hidden {
    display: none !important;
}

#game-over-slot {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1001;
}

#game-over-screen h1 {
    display: flex;
    width: 375px;
    height: 110px;
    flex-direction: column;
    justify-content: center;
    flex-shrink: 0;
    color: #FF6C76;
    text-align: center;
    text-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
    font-size: 62px;
    font-style: normal;
    font-weight: 400;
    line-height: normal; 
    transform: translateY(-50px);
    margin-bottom: 32px;
}

#final-stats {
    margin-bottom: 24px;
    color: #D9F5BF;
    font-size: 24px;
}

#final-stats p {
    margin: 8px 0;
}

#game-over-buttons {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
    justify-content: center;
}

#play-again-btn,
#main-menu-btn {
    width: 190px;
    height: 65px;
    flex-shrink: 0;
    padding: 16px 40px;
    font-size: 24px;
    flex-direction: column;
    justify-content: center;
}
#start-slot {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1001;
}

#start-screen h1 {
    display: flex;
    width: 375px;
    height: 110px;
    flex-direction: column;
    justify-content: center;
    flex-shrink: 0;
    color: #D9F5BF;
    text-align: center;
    text-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
    font-size: 62px;
    font-style: normal;
    font-weight: 400;
    line-height: normal; 
    transform: translateY(-50px);
    margin-bottom: 32px;
}

#start-game-btn {
    width: 256px;
    height: 65px;
    flex-shrink: 0;
    padding: 16px 40px;
    font-size: 28px;
    flex-direction: column;
    justify-content: center;
    margin-bottom: 24px;
    transition: all 0.3s ease;
}

#start-game-btn:active {
    transform: translateY(0);
}

/* Difficulty screen styles */
#difficulty-screen {
    position: fixed;
    z-index: 2000;
    display: none;
    inset: 0;
    background: var(--bg-stone) repeat;
    color: #fff;
}

#difficulty-slot {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -65%);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1001;
}

#back-btn {
    position: absolute;
    top: 24px;
    left: 24px;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: #222;
    color: #fff;
    border: none;
    box-shadow: 0 2px 8px #0006;
    font-size: 2rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    z-index: 1001; /* Ensure it's above other elements */
}

#back-btn:hover {
    background: #333;
    transform: scale(1.1);
}

#difficulty-screen h2 {
    display: flex;
    width: 375px;
    height: 110px;
    flex-direction: column;
    justify-content: center;
    flex-shrink: 0;
    color: #D9F5BF;
    text-align: center;
    text-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
    font-size: 32px;
    font-style: normal;
    font-weight: 400;
    line-height: normal; 
    margin-bottom: 32px;
}

#difficulty-options {
    display: flex;
    gap: 24px;
    margin-bottom: 24px;
    flex-wrap: wrap;
    justify-content: center;
}

.difficulty-btn {
    width: 190px;
    height: 65px;
    flex-shrink: 0;
    padding: 16px 40px;
    left: 24px;
    right: 24px;
    font-size: 28px;
    flex-direction: column;
    justify-content: center;
    color: #D9F5BF;
}

.difficulty-btn[data-diff="easy"] {
    background: #66AE71;
}

.difficulty-btn[data-diff="medium"] {
    background: #67A0EA;
}

.difficulty-btn[data-diff="hard"] {
    background: #CA563C;
}

.difficulty-btn.selected {
    transform: scale(1.1);
    border-color: #fff;
}

#real-start-btn {
    width: 256px;
    height: 65px;
    flex-shrink: 0;
    padding: 16px 40px;
    font-size: 28px;
    flex-direction: column;
    justify-content: center;
    margin-top: 24px;
}

#real-start-btn.enabled {
    opacity: 1;
    pointer-events: auto;
}

#real-start-btn.enabled:hover {
    background: #3a8cff;
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(42, 124, 255, 0.5);
}

/* Tower Info Popup Styles */
#tower-info-popup {
    position: absolute;
    min-width: 180px;
    max-width: 240px;
    background: rgba(20, 30, 20, 0.98);
    border: 2px solid #4a5a4a;
    border-radius: 10px;
    box-shadow: 0 4px 24px #000a;
    z-index: 2000;
    pointer-events: auto;
    padding: 18px 18px 12px 18px;
    display: none;
    color: #f0e6d2;
    font-size: 16px;
    transition: opacity 0.15s;
}
#tower-info-popup:not(.hidden) {
    display: block;
    opacity: 1;
}
#tower-info-content {
    display: flex;
    flex-direction: column;
    gap: 12px;
}
#tower-info-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0;
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 0;
}
#tower-info-name {
    font-size: 20px;
    font-weight: bold;
    color: #ffd700;
    text-align: center;
}
#tower-info-level {
    font-size: 13px;
    font-weight: 400;
    color: #aaa;
    margin: 2px 0 0 0;
    position: static;
    text-align: center;
}
#tower-info-actions {
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: center;
    justify-content: center;
    margin-top: 16px;
    text-align: center;
}
/* Button styles for popup and other UI elements */
.btn {
    padding: 10px 20px;
    background: #2a5a2a;
    color: #f0e6d2;
    border: 2px solid #c0a080;
    border-radius: 6px;
    font-weight: 700;
    font-size: 14px;
    cursor: pointer;
    margin: 5px;
    text-shadow: 0 0 5px #55aa55;
    transition: transform 0.2s, box-shadow 0.2s;
}

.btn:hover {
    box-shadow: 0 0 10px rgba(85, 170, 85, 0.7);
}

.btn:focus {
    outline: none;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    background: #444 !important;
    border-color: #888 !important;
    color: #ccc !important;
    box-shadow: none !important;
}

#tower-upgrade-btn:hover, #tower-sell-btn:hover {
    background: #3a6a3a;
    border-color: #ffd700;
}

#tower-upgrade-btn, #tower-sell-btn {
    width: 120px;
    height: 40px;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#tower-sell-btn {
    background: #5a2a2a;
    border-color: #c08080;
}

#tower-sell-btn:hover {
    background: #7a3a3a;
    border-color: #ff6b6b;
}

.selected-tower-outline {
    outline: none !important;
    box-shadow: none !important;
}



/* Loading overlay matching game's medieval theme */
#loading-overlay {
    position: fixed;
    z-index: 3000;
    inset: 0;
    background: var(--bg-stone) repeat;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.3s ease-in-out;
}

#loading-overlay.hidden {
    display: none !important;
}

.loading-progress-container {
    width: 400px;
    max-width: 80vw;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.loading-progress-bar {
    width: 100%;
    height: 40px;
    position: relative;
    border: 3px solid #192A27;
    border-radius: 8px;
    background: #593817;
    overflow: hidden;
}

.loading-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #FFB450 0%, #FFCA84 50%, #FFB450 100%);
    width: 0%;
    transition: width 0.4s ease;
    border-radius: 4px;
    position: relative;
}

.loading-progress-fill::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(90deg, transparent 0%, rgba(255, 255, 255, 0.3) 50%, transparent 100%);
    background-size: 200% 100%;
    animation: loading-shimmer 2s linear infinite;
}

.loading-progress-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: #D9F5BF;
    font-size: 18px;
    font-weight: 400;
    z-index: 1;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
}

@keyframes loading-shimmer {
    0% { background-position: -200% 0; }
    100% { background-position: 200% 0; }
}
