:root {
    --bg-gradient: radial-gradient(circle at center, #1a1a2e 0%, #16213e 60%, #0f3460 100%);
    --overlay-bg: rgba(15, 52, 96, 0.9);
    --primary-color: #e94560;
    --accent-color: #0f3460;
    --success-color: #28a745;
    --highlight-color: #00d2ff;
    --text-color: #fff;
    --safe-top: env(safe-area-inset-top);
    --safe-right: env(safe-area-inset-right);
    --safe-bottom: env(safe-area-inset-bottom);
    --safe-left: env(safe-area-inset-left);
}

* {
    box-sizing: border-box;
    /* touch-action: none; Removed to allow button clicks */
    user-select: none;
    /* Prevent text selection */
    -webkit-user-select: none;
}

html,
body {
    margin: 0;
    padding: 0;
    width: 100vw;
    height: 100vh;
    min-height: 100vh;
}

body {
    overflow: hidden;
    background: var(--bg-gradient);
    background-size: 200% 200%;
    animation: gradientBG 20s ease infinite;
    font-family: 'Roboto', sans-serif;
    color: var(--text-color);
    touch-action: pan-y;
    /* Allow vertical scroll for overlays/menu */
    position: relative;
}

@supports (height: 100dvh) {
    body {
        height: 100dvh;
        min-height: 100dvh;
    }
}

/* Ensure game elements define their own touch behavior */
canvas,
.t-btn {
    touch-action: none;
}

@keyframes gradientBG {
    0% {
        background-position: 50% 50%;
    }

    50% {
        background-position: 100% 100%;
    }

    100% {
        background-position: 50% 50%;
    }
}



#game-container {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

canvas {
    box-shadow: 0 0 40px rgba(0, 210, 255, 0.2);
    /* Neon glow */
    background: rgba(15, 52, 96, 0.5);
    max-width: 100%;
    max-height: 100%;
    z-index: 10;
    border: 2px solid rgba(0, 210, 255, 0.1);
    border-radius: 4px;
}

/* Overlays */
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--overlay-bg);
    display: flex;
    z-index: 100;
    transition: opacity 0.3s ease;
    backdrop-filter: blur(5px);
    overflow-y: auto;
    /* Enable vertical scroll */
    -webkit-overflow-scrolling: touch;
    /* Smooth scroll on iOS */
    touch-action: pan-y;
    /* Re-enable vertical scroll gesture */
    padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
    box-sizing: border-box;
}

/* Wrapper to handle centering and scrolling */
.menu-container {
    margin: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 30px 20px;
    width: 100%;
    /* Limit to viewport height so smaller screens can scroll */
    max-height: 100%;
    overflow-y: visible;
}

@supports (height: 100dvh) {
    .menu-container {
        max-height: 100%;
    }
}

.overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.title {
    font-family: 'Roboto', sans-serif;
    font-weight: 700;
    font-size: clamp(3rem, 10vw, 5rem);
    color: var(--highlight-color);
    text-align: center;
    text-shadow: 4px 4px 0px var(--accent-color);
    margin-bottom: 1rem;
    line-height: 1;
}

.subtitle {
    font-size: 0.5em;
    color: #fff;
    text-shadow: 2px 2px 0px var(--accent-color);
}

/* Menu Styles */
.menu-section {
    margin-bottom: 1rem;
    text-align: center;
    width: 100%;
    max-width: 600px;
}

.menu-section h3 {
    font-family: 'Roboto', sans-serif;
    color: #fff;
    font-size: 1.4rem;
    margin-bottom: 0.5rem;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.toggle-group {
    display: flex;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
}

.toggle-btn {
    background: rgba(15, 52, 96, 0.6);
    border: 2px solid rgba(255, 255, 255, 0.5);
    color: #fff;
    padding: 0.6rem 1.2rem;
    font-size: 1.2rem;
    border-radius: 25px;
    cursor: pointer;
    font-family: 'Roboto', sans-serif;
    transition: all 0.2s;
    font-weight: bold;
}

.toggle-btn.selected {
    background: #fff;
    border-color: var(--highlight-color);
    color: var(--accent-color);
    box-shadow: 0 0 15px rgba(0, 210, 255, 0.6);
    transform: scale(1.1);
}

.btn {
    background: rgba(15, 52, 96, 0.6);
    border: 2px solid rgba(255, 255, 255, 0.5);
    color: #fff;
    padding: 0.6rem 1.2rem;
    font-size: 1.2rem;
    border-radius: 25px;
    cursor: pointer;
    font-family: 'Roboto', sans-serif;
    transition: all 0.2s;
    font-weight: bold;
    margin: 0.5rem;
}

.btn:hover {
    background: #fff;
    border-color: var(--highlight-color);
    color: var(--accent-color);
    box-shadow: 0 0 15px rgba(0, 210, 255, 0.6);
}

.btn.secondary {
    background: rgba(255, 255, 255, 0.1);
}

.start-btn {
    margin-top: 0.5rem;
    font-size: 1.8rem;
    padding: 0.8rem 2.5rem;
    width: auto;
}

.controls-hint {
    margin-top: 2rem;
    text-align: center;
    opacity: 0.7;
    font-size: 0.9rem;
}

/* Touch Controls */
#touch-controls {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 50;
    pointer-events: none;
    /* We will use padding in JS logic, but here we just layout the containers */
    padding: var(--safe-top) var(--safe-right) max(20px, var(--safe-bottom)) var(--safe-left);
    box-sizing: border-box;
}

#touch-controls.hidden {
    display: none;
}

.controls-group {
    position: absolute;
    pointer-events: auto;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 20px;
    /* Default for 2P: Sides */
    top: 50%;
    transform: translateY(-50%);
    width: 160px;
}

/* 2 Player Default Positioning */
.p1-controls {
    left: 20px;
}

.p2-controls {
    right: 20px;
}

/* 1 Player Mode Positioning (Bottom) */
.mode-1p .p1-controls {
    top: auto;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    max-width: 500px;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    padding: 0;
}

.mode-1p .p2-controls {
    display: none;
}

.mode-1p .p1-controls .dpad {
    order: 1;
}

.mode-1p .p1-controls .drop {
    order: 2;
}

/* 1 Player Mode: Center controls roughly but maybe to the side?
   Actually, for 1P we might want a standard D-pad left, Action right.
   But to keep it simple, we'll assume a "split" layout feels arcade-like
   even for 1P, or we can adjust via JS classes.
   For now, default is split P1 Left, P2 Right.
*/

.t-btn {
    width: 70px;
    height: 70px;
    background: rgba(255, 255, 255, 0.15);
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    color: white;
    font-size: 24px;
    display: flex;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    transition: background 0.1s;
}

.t-btn:active {
    background: rgba(255, 255, 255, 0.4);
}

.dpad {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr;
    gap: 5px;
}

.dpad .up {
    grid-column: 2;
    grid-row: 1;
}

.dpad .left {
    grid-column: 1;
    grid-row: 2;
}

.dpad .right {
    grid-column: 3;
    grid-row: 2;
}

.dpad .down {
    grid-column: 2;
    grid-row: 2;
}

/* Large Drop Button */
.t-btn.drop {
    width: 90px;
    height: 90px;
    background: rgba(212, 36, 38, 0.3);
    /* Red tint */
    border-color: rgba(212, 36, 38, 0.5);
    font-size: 40px;
}

.t-btn.drop:active {
    background: rgba(212, 36, 38, 0.6);
}

/* Removed old mode-1p overrides as they are now handled in the main block above */

/* Tablet friendly 2P layout: keep controls reachable near bottom corners */
body.tablet-controls:not(.mode-1p) .controls-group {
    top: auto;
    bottom: calc(max(20px, var(--safe-bottom)) + 10px);
    transform: none;
    flex-direction: row;
    width: auto;
    gap: 15px;
}

body.tablet-controls:not(.mode-1p) .p1-controls {
    left: clamp(12px, 4vw, 60px);
    right: auto;
}

body.tablet-controls:not(.mode-1p) .p2-controls {
    right: clamp(12px, 4vw, 60px);
    left: auto;
}

body.tablet-controls:not(.mode-1p) .controls-group .dpad {
    order: 1;
}

body.tablet-controls:not(.mode-1p) .controls-group .drop {
    order: 2;
}

/* Media Queries for iPad/Mobile */
@media (min-width: 768px) {
    .title {
        font-size: 6rem;
    }

    .btn {
        font-size: 1.5rem;
        padding: 1rem 2rem;
    }
}

@media (max-width: 600px) {
    .title {
        font-size: 3rem;
    }

    .t-btn {
        width: 50px;
        height: 50px;
        font-size: 18px;
    }

    .t-btn.drop {
        width: 70px;
        height: 70px;
    }

    .controls-group {
        width: 140px;
    }
}

@media (pointer: coarse) {
    .t-btn {
        width: 80px;
        height: 80px;
        font-size: 26px;
    }

    .t-btn.drop {
        width: 110px;
        height: 110px;
        font-size: 48px;
    }

    body.mode-1p .controls-group {
        gap: 30px;
    }
}