* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html, body {
    width: 100%;
    height: 100%;
    height: 100dvh;
    min-height: -webkit-fill-available;
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-overscroll-behavior: none;
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    font-family: 'Courier New', Courier, monospace;
    background: #070712;
    color: #00ff88;
}

#game-container {
    position: relative;
    width: 100%;
    height: 100%;
    height: 100dvh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    touch-action: none;
}

#game-canvas {
    display: block;
    background: #1a1a2e;
    image-rendering: pixelated;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.mobile-hidden {
    display: none !important;
}

/* Mobile controls container: hidden by default for desktop and laptop */
#mobile-controls {
    position: fixed;
    bottom: max(14px, env(safe-area-inset-bottom, 14px));
    left: 0;
    right: 0;
    display: none;
    justify-content: space-between;
    align-items: flex-end;
    padding: 0 max(16px, env(safe-area-inset-right, 16px)) 0 max(16px, env(safe-area-inset-left, 16px));
    pointer-events: none;
    z-index: 30;
}

/* Shown strictly on mobile and tablet devices during gameplay */
#mobile-controls.visible {
    display: flex !important;
}

/* 3x3 Cross D-Pad */
.d-pad {
    position: relative;
    display: grid;
    grid-template-columns: repeat(3, clamp(44px, 12vw, 56px));
    grid-template-rows: repeat(3, clamp(44px, 12vw, 56px));
    gap: 4px;
    pointer-events: auto;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

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

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

.dpad-center {
    grid-column: 2;
    grid-row: 2;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    border: 1px dashed rgba(0, 255, 136, 0.25);
    background: radial-gradient(circle, rgba(0, 255, 136, 0.08) 0%, rgba(0, 0, 0, 0) 70%);
    pointer-events: none;
}

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

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

.dpad-btn {
    width: 100%;
    height: 100%;
    border: 2px solid rgba(0, 255, 136, 0.85);
    border-radius: 50%;
    background: radial-gradient(circle, rgba(0, 255, 136, 0.22) 0%, rgba(0, 30, 15, 0.7) 100%);
    color: #00ff88;
    font-size: clamp(16px, 4vw, 22px);
    font-family: 'Courier New', monospace;
    font-weight: bold;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
    box-shadow: 0 0 10px rgba(0, 255, 136, 0.25), inset 0 0 8px rgba(0, 255, 136, 0.15);
    transition: transform 0.06s ease, background 0.06s ease, border-color 0.06s ease;
    pointer-events: auto;
}

.dpad-btn:active,
.dpad-btn.active {
    background: radial-gradient(circle, rgba(0, 255, 136, 0.75) 0%, rgba(0, 255, 136, 0.4) 100%);
    color: #ffffff;
    border-color: #ffffff;
    box-shadow: 0 0 18px rgba(0, 255, 136, 0.85), inset 0 0 12px rgba(255, 255, 255, 0.5);
    transform: scale(0.92);
}

/* Action Button Cluster */
.action-cluster {
    display: flex;
    flex-direction: column;
    align-items: center;
    pointer-events: auto;
    margin-bottom: 4px;
}

#action-btn {
    width: clamp(66px, 17vw, 82px);
    height: clamp(66px, 17vw, 82px);
    border: 2px solid rgba(255, 215, 0, 0.85);
    border-radius: 50%;
    background: radial-gradient(circle, rgba(255, 215, 0, 0.25) 0%, rgba(50, 40, 0, 0.7) 100%);
    color: #ffd700;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
    box-shadow: 0 0 14px rgba(255, 215, 0, 0.35), inset 0 0 10px rgba(255, 215, 0, 0.18);
    transition: transform 0.06s ease, background 0.06s ease, border-color 0.06s ease;
    pointer-events: auto;
}

#action-btn:active,
#action-btn.active {
    background: radial-gradient(circle, rgba(255, 215, 0, 0.75) 0%, rgba(255, 215, 0, 0.4) 100%);
    color: #ffffff;
    border-color: #ffffff;
    box-shadow: 0 0 22px rgba(255, 215, 0, 0.9), inset 0 0 14px rgba(255, 255, 255, 0.5);
    transform: scale(0.92);
}

#action-btn.pulse {
    animation: actionPulse 1.1s infinite alternate ease-in-out;
}

@keyframes actionPulse {
    0% {
        box-shadow: 0 0 10px rgba(255, 215, 0, 0.3), inset 0 0 8px rgba(255, 215, 0, 0.15);
        border-color: rgba(255, 215, 0, 0.7);
    }
    100% {
        box-shadow: 0 0 24px rgba(255, 215, 0, 0.9), inset 0 0 14px rgba(255, 255, 255, 0.4);
        border-color: #ffffff;
    }
}

.action-primary {
    font-size: clamp(12px, 3.4vw, 15px);
    font-weight: bold;
    font-family: 'Courier New', monospace;
    letter-spacing: 1px;
    line-height: 1.1;
}

.action-sub {
    font-size: clamp(8px, 2.2vw, 10px);
    opacity: 0.85;
    font-family: 'Courier New', monospace;
    margin-top: 2px;
}

/* Landscape adaptations on phones (low vertical space) */
@media (orientation: landscape) and (max-height: 520px) {
    #mobile-controls {
        bottom: max(8px, env(safe-area-inset-bottom, 8px));
        padding: 0 max(16px, env(safe-area-inset-right, 16px)) 0 max(16px, env(safe-area-inset-left, 16px));
    }
    .d-pad {
        grid-template-columns: repeat(3, clamp(38px, 10vh, 48px));
        grid-template-rows: repeat(3, clamp(38px, 10vh, 48px));
        gap: 3px;
    }
    #action-btn {
        width: clamp(56px, 15vh, 70px);
        height: clamp(56px, 15vh, 70px);
    }
}

/* Portrait adaptations on phones */
@media (orientation: portrait) {
    #mobile-controls {
        bottom: max(20px, env(safe-area-inset-bottom, 20px));
    }
    .d-pad {
        grid-template-columns: repeat(3, clamp(48px, 13vw, 62px));
        grid-template-rows: repeat(3, clamp(48px, 13vw, 62px));
        gap: 5px;
    }
    #action-btn {
        width: clamp(70px, 19vw, 86px);
        height: clamp(70px, 19vw, 86px);
    }
}

/* CRT screen treatment */
#crt {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 5;
    background:
        repeating-linear-gradient(
            0deg,
            rgba(0, 0, 0, 0.22) 0px,
            rgba(0, 0, 0, 0.22) 1px,
            transparent 1px,
            transparent 3px
        ),
        radial-gradient(ellipse at center, rgba(0, 0, 0, 0) 58%, rgba(0, 0, 0, 0.5) 100%);
    opacity: 0.9;
    animation: crtFlicker 4s infinite;
}

@keyframes crtFlicker {
    0%, 100% { opacity: 0.9; }
    92% { opacity: 0.9; }
    93% { opacity: 0.75; }
    94% { opacity: 0.9; }
    97% { opacity: 0.85; }
}
