* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: #0d1b0f;
    color: #e8f5e9;
    font-family: 'Segoe UI', Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
}

.hidden {
    display: none !important;
}

.screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    padding: 20px;
    width: 100%;
    min-width: 0;
    max-width: 100vw;
}

h1 {
    letter-spacing: 2px;
    color: #4caf50;
}

/* MENU */
.game-selection {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 16px;
    width: min(760px, 100%);
}

.game-select-btn {
    background: #163b1a;
    border: 2px solid #4caf50;
    color: #e8f5e9;
    padding: 16px 24px;
    font-size: 18px;
    font-weight: bold;
    border-radius: 10px;
    cursor: pointer;
    min-width: 260px;
}

.game-select-btn.selected {
    background: #2e7d32;
    border-color: #a5d6a7;
}

#tower-defence-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
}

.difficulty-buttons {
    display: flex;
    gap: 16px;
}

.difficulty-btn {
    background: #163b1a;
    border: 2px solid #4caf50;
    color: #e8f5e9;
    padding: 16px 24px;
    font-size: 16px;
    font-weight: bold;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.difficulty-btn span {
    font-size: 12px;
    font-weight: normal;
    color: #a5d6a7;
}

.difficulty-btn:hover {
    background: #1f5a26;
}

#continue-btn {
    background: #f1c40f;
    border: none;
    color: #201400;
    font-weight: bold;
    padding: 12px 24px;
    border-radius: 8px;
    cursor: pointer;
}

#gameplay-description {
    max-width: 520px;
    text-align: center;
    color: #a5d6a7;
    font-size: 13px;
    line-height: 1.5;
}

#language-flags {
    display: flex;
    gap: 8px;
}

.flag-btn {
    background: #163b1a;
    border: 2px solid #4caf50;
    border-radius: 4px;
    padding: 4px;
    cursor: pointer;
    line-height: 0;
}

.flag-btn img {
    display: block;
    border-radius: 2px;
}

.flag-btn.selected {
    border-color: #f1c40f;
    background: #1f5a26;
}

/* HUD */
#hud {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    width: min(800px, 100%);
    min-width: 0;
    background: #102712;
    border: 2px solid #4caf50;
    border-radius: 6px 6px 0 0;
    padding: 8px 16px;
    font-weight: bold;
    font-size: 14px;
    max-width: 100%;
}

#hud .hud-item {
    min-width: 0;
}

/* GAME AREA */
#game-area {
    position: relative;
    width: min(800px, 100%);
    max-width: 100%;
    min-width: 0;
    aspect-ratio: 4 / 3;
}

canvas {
    background: #1b2e1b;
    display: block;
    width: 100%;
    max-width: 100%;
    height: auto;
    aspect-ratio: 4 / 3;
    border-left: 2px solid #4caf50;
    border-right: 2px solid #4caf50;
    cursor: crosshair;
}

.banner {
    position: absolute;
    top: 16px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(16, 39, 18, 0.95);
    border: 2px solid #f1c40f;
    color: #f1c40f;
    font-weight: bold;
    font-size: 16px;
    padding: 10px 20px;
    border-radius: 8px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.banner.visible {
    opacity: 1;
}

.speed-controls {
    display: flex;
    gap: 4px;
}

.speed-btn {
    background: #163b1a;
    border: 1px solid #4caf50;
    color: #e8f5e9;
    font-weight: bold;
    padding: 4px 10px;
    border-radius: 4px;
    cursor: pointer;
}

.speed-btn.selected {
    background: #4caf50;
    color: #0d1b0f;
}

#restart-round-btn {
    background: #922b21;
    border: 1px solid #e74c3c;
    color: #f5e8e8;
    font-weight: bold;
    font-size: 12px;
    padding: 6px 10px;
    border-radius: 4px;
    cursor: pointer;
}

#sound-btn {
    background: #1f618d;
    border: 1px solid #5dade2;
    color: #eaf4fb;
    font-weight: bold;
    font-size: 12px;
    padding: 6px 10px;
    border-radius: 4px;
    cursor: pointer;
}

#sound-btn.muted {
    background: #424949;
    border-color: #7f8c8d;
    color: #d5d8dc;
}

/* TOWER BAR */
#tower-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    width: min(800px, 100%);
    min-width: 0;
    background: #102712;
    border: 2px solid #4caf50;
    border-radius: 0 0 6px 6px;
    padding: 10px 16px;
}

.tower-btn {
    background: #163b1a;
    border: 2px solid #4caf50;
    color: #e8f5e9;
    border-radius: 6px;
    padding: 8px 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    cursor: pointer;
    min-width: 90px;
}

.tower-btn.selected {
    background: #2e7d32;
    border-color: #a5d6a7;
}

.tower-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.tower-btn-cost {
    font-size: 11px;
    color: #a5d6a7;
}

#start-wave-btn {
    margin-left: auto;
    background: #ff9800;
    border: none;
    color: #201400;
    font-weight: bold;
    padding: 12px 20px;
    border-radius: 6px;
    cursor: pointer;
}

#start-wave-btn:disabled {
    background: #6d4c1f;
    color: #b8a98a;
    cursor: not-allowed;
}

#auto-wave-btn {
    background: #163b1a;
    border: 2px solid #4caf50;
    color: #e8f5e9;
    font-weight: bold;
    padding: 12px 16px;
    border-radius: 6px;
    cursor: pointer;
}

#auto-wave-btn.selected {
    background: #4caf50;
    color: #0d1b0f;
}

#save-game-btn {
    background: #2471a3;
    border: 2px solid #5dade2;
    color: #eaf4fb;
    font-weight: bold;
    padding: 12px 16px;
    border-radius: 6px;
    cursor: pointer;
}

/* TOWER INFO PANEL */
.panel {
    position: absolute;
    top: 10px;
    right: 10px;
    background: rgba(16, 39, 18, 0.95);
    border: 2px solid #4caf50;
    border-radius: 6px;
    padding: 12px 16px;
    min-width: 180px;
}

.panel h3 {
    margin: 0 0 6px 0;
    color: #4caf50;
}

.panel-actions {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}

.panel-actions button {
    flex: 1;
    background: #163b1a;
    border: 1px solid #4caf50;
    color: #e8f5e9;
    padding: 6px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
}

.panel-actions button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

#panel-evolution-actions {
    flex-wrap: wrap;
}

#panel-evolve-frost-btn {
    background: #2471a3;
    border-color: #5dade2;
    color: #eaf4fb;
}

#panel-evolve-venom-btn {
    background: #218c48;
    border-color: #58d68d;
    color: #eafaf1;
}

/* GAME OVER / VICTORY */
#game-over-screen h1 {
    color: #e74c3c;
}

#victory-screen h1 {
    color: #f1c40f;
}

@media (max-width: 700px) {
    body {
        align-items: flex-start;
        min-height: 100dvh;
        width: 100vw;
        max-width: 100vw;
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
        padding-bottom: env(safe-area-inset-bottom);
    }

    .screen {
        gap: 10px;
        padding: 10px;
        overflow: hidden;
    }

    #game-screen {
        align-items: stretch;
        width: 100vw;
        max-width: 100vw;
        padding-left: 4px;
        padding-right: 4px;
    }

    h1 {
        margin: 8px 0;
        font-size: 22px;
        text-align: center;
    }

    .game-selection {
        gap: 10px;
    }

    .game-select-btn {
        min-width: 0;
        width: 100%;
        padding: 12px 16px;
        font-size: 15px;
    }

    .difficulty-buttons {
        width: 100%;
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px;
    }

    .difficulty-btn {
        flex: 1 1 90px;
        min-width: 90px;
        padding: 12px 8px;
        font-size: 13px;
    }

    #hud {
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px 12px;
        padding: 8px;
        font-size: 12px;
        width: 100%;
        max-width: 100%;
        overflow: hidden;
    }

    #game-area,
    #tower-bar {
        width: calc(100vw - 8px);
        max-width: calc(100vw - 8px);
    }

    canvas {
        width: 100%;
        height: auto;
    }

    #restart-round-btn {
        padding: 5px 8px;
    }

    #tower-bar {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
        padding: 8px;
    }

    .tower-btn,
    #start-wave-btn,
    #auto-wave-btn,
    #save-game-btn {
        width: 100%;
        min-width: 0;
        margin: 0;
        padding: 9px 6px;
        font-size: 12px;
    }

    .panel {
        top: 6px;
        right: 6px;
        min-width: 155px;
        padding: 8px 10px;
        font-size: 12px;
    }

    .banner {
        top: 8px;
        max-width: calc(100% - 16px);
        padding: 7px 10px;
        font-size: 12px;
        text-align: center;
    }
}

/* iPhone Chrome can expose a wider legacy layout viewport; use device width
   as a fallback so the game still switches to the mobile layout. */
@media (max-device-width: 700px) {
    body,
    .screen,
    #game-screen,
    #hud,
    #game-area,
    #tower-bar {
        width: 100%;
        max-width: 100%;
        min-width: 0;
    }

    #game-screen {
        align-items: stretch;
        padding-left: 4px;
        padding-right: 4px;
    }

    #hud {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        justify-content: initial;
        gap: 7px;
        overflow: hidden;
    }

    #hud .hud-item,
    #hud .speed-controls,
    #hud button {
        min-width: 0;
        max-width: 100%;
    }

    #hud .speed-controls {
        justify-content: center;
    }

    #game-area {
        aspect-ratio: 4 / 3;
    }

    #game-area canvas {
        width: 100%;
        max-width: 100%;
        height: auto;
    }

    #tower-bar {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
    }

    #tower-bar button {
        min-width: 0;
        max-width: 100%;
        margin: 0;
    }
}
