:root {
    --bg-color: #0d1117;
    --ui-bg: rgba(22, 27, 34, 0.95);
    --accent-tech: #00f3ff;
    --accent-magic: #bd00ff;
    --text-color: #c9d1d9;
    --gold: #ffd700;
    --danger: #ff4444;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    padding: 0;
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
    user-select: none;
    display: flex;
    flex-direction: column;
}

/* Main Grid Layout */
#app-layout {
    display: grid;
    grid-template-areas:
        "header header header"
        "left   game   right"
        "footer footer footer";
    grid-template-columns: 120px 1fr 220px;
    grid-template-rows: 60px 1fr 80px;
    height: 100%;
    width: 100%;
}

/* Header (Stats) */
#header-panel {
    grid-area: header;
    background: var(--ui-bg);
    border-bottom: 1px solid #30363d;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 40px;
    font-size: 20px;
    font-weight: bold;
    z-index: 10;
}

.stat-box { display: flex; align-items: center; gap: 10px; }
.stat-icon { font-family: 'Courier New', monospace; font-size: 24px; }
.currency { color: var(--gold); }
.lives { color: var(--danger); }
.wave { color: var(--accent-tech); }
.wave.endless { color: #ffd700; text-shadow: 0 0 5px #ffd700; }

/* Left Panel (Abilities) */
#left-panel {
    grid-area: left;
    background: var(--ui-bg);
    border-right: 1px solid #30363d;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 10px;
    gap: 15px;
    z-index: 10;
}

.panel-label {
    font-size: 12px;
    color: #8b949e;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 5px;
    text-align: center;
    width: 100%;
    border-bottom: 1px solid #30363d;
    padding-bottom: 5px;
}

.ability-btn {
    width: 100%;
    height: 80px;
    background: #21262d;
    border: 1px solid #30363d;
    border-radius: 6px;
    cursor: pointer;
    color: white;
    position: relative;
    overflow: hidden;
    transition: 0.2s;
    font-size: 11px;
    font-weight: bold;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 5px;
}
.ability-btn:hover:not(:disabled) { border-color: var(--accent-tech); background: #30363d; }
.ability-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.ability-btn span { z-index: 2; position: relative; }
.key-hint { display: block; font-size: 9px; color: #8b949e; margin-top: 2px; }

.cooldown-overlay {
    position: absolute; bottom: 0; left: 0; width: 100%; height: 0%;
    background: rgba(0, 0, 0, 0.7); transition: height 0.1s linear; z-index: 1;
}

/* Right Panel (Shop) */
#right-panel {
        grid-area: right;
    position: fixed; /* Fix it to the window */
    top: 0;
    right: 0;
    bottom: 0;
    width: 220px; /* Adjust width as needed */
    height: 100vh; /* Full viewport height */
    background: var(--ui-bg);
    border-left: 1px solid #30363d;
    overflow-y: auto; /* Scroll only if screen is very short */
    z-index: 50; /* Below the upgrade popup (100) */
    padding: 10px;
    padding-top: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    box-shadow: -5px 0 15px rgba(0,0,0,0.5);
}


#shop-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding-right: 5px; /* space for scrollbar */
}

.tower-card {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid #444;
    border-radius: 4px;
    padding: 8px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
    gap: 2px;
    position: relative;
    min-height: 60px;
}
.tower-card:hover { border-color: var(--accent-tech); background: rgba(0, 243, 255, 0.1); }
.tower-card.selected { border-color: var(--gold); background: rgba(255, 215, 0, 0.15); box-shadow: 0 0 10px rgba(255, 215, 0, 0.3); }
.tower-card.too-expensive { opacity: 0.5; border-color: #552222; }

.tower-name { font-weight: bold; font-size: 13px; color: #fff; }
.tower-cost { font-size: 12px; color: var(--gold); }
.tower-desc { font-size: 10px; color: #8b949e; }
.hotkey-hint { position: absolute; top: 5px; right: 5px; font-size: 10px; color: #555; font-family: monospace; border: 1px solid #333; padding: 1px 4px; border-radius: 3px; background: rgba(0,0,0,0.5); }

/* Footer (Controls) */
#footer-panel {
    grid-area: footer;
    background: var(--ui-bg);
    border-top: 1px solid #30363d;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 20px;
    z-index: 10;
}

.control-btn {
    padding: 12px 24px;
    background: #21262d;
    border: 1px solid #30363d;
    color: white;
    font-weight: bold;
    font-size: 16px;
    border-radius: 4px;
    cursor: pointer;
    min-width: 120px;
    text-align: center;
    transition: all 0.2s;
}
.control-btn:hover { background: #30363d; border-color: #8b949e; }

#start-wave-btn {
    background: linear-gradient(45deg, var(--accent-tech), var(--accent-magic));
    border: none;
    box-shadow: 0 0 15px rgba(0, 243, 255, 0.3);
}
#start-wave-btn:hover { filter: brightness(1.2); }
.auto-on { background: #238636 !important; border-color: #2ea043 !important; }

/* Game Area */
#game-area {
    grid-area: game;
    position: relative;
    background: #000;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
}

canvas {
    display: block;
    background: radial-gradient(circle at center, #1a1f2e 0%, #0d1117 100%);
    max-width: 100%;
    max-height: 100%;
    object-fit: contain; /* Ensures aspect ratio */
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.5);
}

#status-legend {
    position: absolute;
    left: 14px;
    top: 12px;
    width: 252px;
    padding: 8px 9px;
    border: 1px solid rgba(125, 140, 165, 0.35);
    border-radius: 8px;
    background: rgba(8, 12, 18, 0.78);
    backdrop-filter: blur(2px);
    z-index: 30;
    pointer-events: auto;
}

#status-legend.legend-collapsed {
    width: 142px;
    padding-bottom: 6px;
}

#status-legend.legend-collapsed .legend-row {
    display: none;
}

.legend-title-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
}

.legend-title {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.9px;
    font-weight: 700;
    color: #d4deeb;
    margin-bottom: 6px;
    border-bottom: 1px solid rgba(120, 145, 180, 0.28);
    padding-bottom: 4px;
    margin: 0;
    flex: 1;
}

#legend-toggle-btn {
    width: 20px;
    height: 20px;
    border: 1px solid rgba(120, 145, 180, 0.5);
    background: rgba(17, 24, 34, 0.92);
    color: #d4deeb;
    border-radius: 4px;
    font-size: 13px;
    line-height: 1;
    font-weight: 700;
    cursor: pointer;
    padding: 0;
}

#legend-toggle-btn:hover {
    border-color: rgba(155, 195, 245, 0.85);
    color: #ffffff;
}

.legend-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 5px;
}

.legend-swatch {
    width: 17px;
    height: 17px;
    border-radius: 50%;
    flex: 0 0 17px;
}

.legend-copy {
    min-width: 0;
}

.legend-name {
    font-size: 11px;
    line-height: 1.1;
    color: #eff6ff;
    font-weight: 700;
}

.legend-desc {
    font-size: 10px;
    line-height: 1.2;
    color: #93a8c0;
}

.swatch-exposed {
    border: 2px solid #b7ff36;
    box-shadow: 0 0 8px rgba(183, 255, 54, 0.5);
    background: radial-gradient(circle at 40% 40%, rgba(255, 255, 255, 0.8), rgba(183, 255, 54, 0.2) 45%, rgba(20, 40, 8, 0.1));
    animation: pulse-exposed 1.3s infinite;
}

.swatch-acid {
    border: 1px solid rgba(192, 255, 94, 0.9);
    background: radial-gradient(circle at 50% 50%, rgba(176, 255, 74, 0.55) 0%, rgba(120, 193, 40, 0.35) 58%, rgba(42, 88, 16, 0.2) 100%);
    box-shadow: inset 0 0 6px rgba(232, 255, 175, 0.35), 0 0 8px rgba(162, 230, 70, 0.42);
    animation: ripple-acid 1.9s infinite;
}

.swatch-chrono {
    border: 1px solid rgba(128, 226, 255, 0.9);
    background:
        radial-gradient(circle, rgba(140, 239, 255, 0.15) 20%, transparent 22%),
        radial-gradient(circle, rgba(140, 239, 255, 0.25) 48%, transparent 50%),
        radial-gradient(circle, rgba(160, 190, 255, 0.35) 72%, transparent 74%);
    box-shadow: 0 0 10px rgba(105, 208, 255, 0.45);
    animation: pulse-chrono 2.2s infinite;
}

@keyframes pulse-exposed {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.08); }
}

@keyframes ripple-acid {
    0%, 100% { box-shadow: inset 0 0 6px rgba(232, 255, 175, 0.35), 0 0 8px rgba(162, 230, 70, 0.42); }
    50% { box-shadow: inset 0 0 10px rgba(232, 255, 175, 0.5), 0 0 13px rgba(162, 230, 70, 0.65); }
}

@keyframes pulse-chrono {
    0%, 100% { transform: scale(1); opacity: 0.9; }
    50% { transform: scale(1.1); opacity: 1; }
}

@media (max-width: 1180px) {
    #status-legend {
        width: 220px;
        left: 10px;
        top: 8px;
    }

    #status-legend.legend-collapsed {
        width: 132px;
    }

    .legend-desc {
        font-size: 9px;
    }
}

/* Modals */
#upgrade-modal {
    position: absolute;
    z-index: 100;
    
    /* Remove fixed positioning */
    bottom: auto; 
    right: auto;
    
    /* These will be set by JS, but good to have defaults */
    top: 0;
    left: 0;

    /* Keep the sizing/style from before */
    display: none; 
    flex-direction: column;
    width: 260px;
    padding: 10px;
    background: rgba(10, 15, 20, 0.95);
    border: 1px solid #444;
    border-radius: 8px;
    color: #fff;
    font-family: 'Courier New', Courier, monospace;
    font-size: 12px;
    box-shadow: 0 0 10px rgba(0,0,0,0.5);
}

/* Make the title smaller and tighter */
#upgrade-modal h3 {
    margin: 0 0 8px 0;
    font-size: 14px;
    color: #00f3ff;
    text-transform: uppercase;
    border-bottom: 1px solid #333;
    padding-bottom: 5px;
}

/* Compact the rows */
.upgrade-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6px;
    background: rgba(255, 255, 255, 0.05);
    padding: 5px;
    border-radius: 4px;
}

/* Style the buttons to be small */
#upgrade-modal button {
    padding: 4px 8px;
    font-size: 11px;
    background: #333;
    border: 1px solid #555;
    color: #fff;
    cursor: pointer;
    transition: all 0.2s;
}

#upgrade-modal button:hover:not(:disabled) {
    background: #00f3ff;
    color: #000;
}

#upgrade-modal button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Style the close button */
#close-upgrade-btn {
    position: absolute;
    top: 5px;
    right: 8px;
    background: none;
    border: none;
    color: #ff4444;
    font-weight: bold;
    font-size: 16px;
    cursor: pointer;
}

.upgrade-header { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #444; padding-bottom: 5px; }
.upgrade-paths { display: flex; gap: 10px; }
.path-column { flex: 1; background: rgba(0, 0, 0, 0.3); padding: 10px; border-radius: 4px; border: 1px solid #333; position: relative; }
.path-tech { border-color: rgba(0, 243, 255, 0.3); }
.path-magic { border-color: rgba(189, 0, 255, 0.3); }
.path-title { font-size: 12px; text-transform: uppercase; margin-bottom: 8px; text-align: center; }
.path-tech .path-title { color: var(--accent-tech); }
.path-magic .path-title { color: var(--accent-magic); }
.path-key-hint { position: absolute; top: 5px; right: 5px; font-size: 10px; color: #666; font-weight: bold; border: 1px solid #444; padding: 1px 3px; border-radius: 3px; }

.upgrade-btn {
    width: 100%; padding: 8px; background: #21262d; border: 1px solid #30363d;
    color: white; cursor: pointer; border-radius: 4px; margin-top: 5px; font-size: 12px;
}
.upgrade-btn:hover:not(:disabled) { background: #30363d; }
.upgrade-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.sell-btn { background: #3d1c1c; border: 1px solid #ff4444; color: #ffaaaa; margin-top: 10px; }

#priority-btn {
    background: #238636; border: 1px solid #2ea043; margin-left: 10px;
    font-size: 11px; padding: 4px 8px; border-radius: 4px; color: white; cursor: pointer;
}

#dps-display { color: #f0f6fc; font-size: 12px; margin-left: 10px; }

/* Fullscreen Overlays */
#map-screen, #game-over, #level-complete {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(13, 17, 23, 0.95);
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    z-index: 200;
}
#game-over { display: none; }
#level-complete { display: none; }

.map-grid { display: flex; gap: 20px; flex-wrap: wrap; justify-content: center; max-width: 900px; }
.map-card {
    width: 200px; height: 160px; background: #161b22; border: 1px solid #30363d;
    border-radius: 8px; cursor: pointer; padding: 15px;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    transition: 0.3s; position: relative;
}
.map-card:hover:not(.locked) { transform: translateY(-5px); border-color: var(--accent-tech); box-shadow: 0 5px 15px rgba(0, 243, 255, 0.2); }
.map-card.locked { opacity: 0.5; cursor: not-allowed; border-color: #3d1c1c; }
.map-name { font-size: 18px; font-weight: bold; margin-bottom: 5px; color: #fff; text-align: center; }
.map-diff { font-size: 12px; color: #8b949e; margin-bottom: 5px; }
.map-preview { width: 100%; height: 60px; background: #0d1117; margin-top: 10px; border-radius: 4px; opacity: 0.5; }
.lock-icon { font-size: 24px; color: #8b949e; margin-top: 10px; }

h1 { margin: 0 0 20px 0; text-shadow: 0 0 10px var(--accent-magic); font-size: 32px; }
.modal-buttons { display: flex; gap: 20px; margin-top: 20px; }

