/**
 * CACTUS CLASH - Mobile Touch Controls CSS
 * ==========================================
 * Professional mobile FPS controls styling
 * Hidden on desktop, visible only on touch devices
 */

/* ============================================
   CONTAINER - Hidden on desktop
   ============================================ */
#touch-controls {
    display: none; /* Shown via JS only on mobile */
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 500;
    pointer-events: none;
    /* Safe area insets for notched phones */
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* Show on touch devices (set by JS) */
#touch-controls.active {
    display: block;
}

/* ============================================
   JOYSTICK AREA (Left half of screen)
   ============================================ */
#touch-joystick-area {
    position: absolute;
    left: 0;
    top: 0;
    width: 45%;
    height: 100%;
    pointer-events: auto;
    touch-action: none;
    -ms-touch-action: none;
    z-index: 5;
    /* Debug: uncomment to see area */
    /* background: rgba(255,0,0,0.05); */
}

#touch-joystick-base {
    display: none; /* Shown on touch */
    position: absolute;
    width: 130px;
    height: 130px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(255,255,255,0.08) 0%, rgba(255,255,255,0.03) 70%, transparent 100%);
    border: 2px solid rgba(255,255,255,0.2);
    box-shadow: 0 0 20px rgba(0,0,0,0.3), inset 0 0 20px rgba(255,255,255,0.05);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

#touch-joystick-thumb {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 52px;
    height: 52px;
    margin: -26px 0 0 -26px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(255,255,255,0.35) 0%, rgba(255,255,255,0.15) 100%);
    border: 2px solid rgba(255,255,255,0.4);
    box-shadow: 0 2px 10px rgba(0,0,0,0.4);
    transition: transform 0.05s ease-out;
    will-change: transform;
}

/* ============================================
   AIM AREA (Right half, behind buttons)
   ============================================ */
#touch-aim-area {
    position: absolute;
    right: 0;
    top: 0;
    width: 55%;
    height: 100%;
    pointer-events: auto;
    touch-action: none;
    -ms-touch-action: none;
    z-index: 1; /* LOW — behind buttons */
    /* Debug: uncomment to see area */
    /* background: rgba(0,0,255,0.05); */
}

/* ============================================
   ACTION BUTTONS
   ============================================ */
#touch-buttons {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 55%;
    height: 100%;
    pointer-events: none;
    z-index: 10; /* HIGH — above aim area */
}

.touch-btn {
    position: absolute;
    pointer-events: auto;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: rgba(255, 255, 255, 0.8);
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none; /* CRITICAL: prevents browser gestures */
    -ms-touch-action: none;
    transition: transform 0.08s ease, background 0.08s ease;
    will-change: transform;
    z-index: 20; /* Always above aim area */
}

.touch-btn:active,
.touch-btn.active {
    transform: scale(0.9);
}

.touch-btn svg {
    width: 50%;
    height: 50%;
}

/* --- SHOOT BUTTON (Big, bottom-right) --- */
.touch-btn-shoot {
    right: 20px;
    bottom: 80px;
    width: 80px;
    height: 80px;
    background: radial-gradient(circle, rgba(220, 40, 40, 0.7) 0%, rgba(180, 20, 20, 0.5) 100%);
    border: 2.5px solid rgba(255, 80, 80, 0.6);
    box-shadow: 0 0 15px rgba(255, 50, 50, 0.3), inset 0 0 10px rgba(255,100,100,0.15);
}

.touch-btn-shoot:active,
.touch-btn-shoot.active {
    background: radial-gradient(circle, rgba(255, 50, 50, 0.9) 0%, rgba(220, 30, 30, 0.7) 100%);
    box-shadow: 0 0 25px rgba(255, 50, 50, 0.5), inset 0 0 15px rgba(255,100,100,0.3);
    transform: scale(0.92);
}

.touch-btn-shoot svg {
    width: 45%;
    height: 45%;
    filter: drop-shadow(0 0 4px rgba(255,100,100,0.5));
}

/* --- ACTION BUTTONS (Jump, Reload, Crouch) --- */
.touch-btn-action {
    width: 56px;
    height: 56px;
    background: radial-gradient(circle, rgba(255,255,255,0.12) 0%, rgba(255,255,255,0.05) 100%);
    border: 2px solid rgba(255,255,255,0.2);
    box-shadow: 0 2px 10px rgba(0,0,0,0.3);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.touch-btn-action:active,
.touch-btn-action.active {
    background: radial-gradient(circle, rgba(255,255,255,0.25) 0%, rgba(255,255,255,0.1) 100%);
    border-color: rgba(255,255,255,0.4);
}

/* Crouch toggled ON state */
.touch-btn-action.toggled {
    background: radial-gradient(circle, rgba(100,200,255,0.3) 0%, rgba(60,150,220,0.15) 100%);
    border-color: rgba(100,200,255,0.5);
    color: rgba(100,200,255,0.9);
}

/* --- SMALL BUTTONS (Weapon switch, Scope) --- */
.touch-btn-small {
    width: 46px;
    height: 46px;
    background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0.04) 100%);
    border: 1.5px solid rgba(255,255,255,0.15);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    font-size: 12px;
    font-weight: 700;
    font-family: 'Segoe UI', Arial, sans-serif;
}

.touch-btn-small:active,
.touch-btn-small.active {
    background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, rgba(255,255,255,0.08) 100%);
}

/* ============================================
   BUTTON POSITIONS - Redesigned for no overlap
   Split: RIGHT = combat, LEFT = utility
   ============================================ */

/* === RIGHT SIDE: Combat cluster === */

/* Shoot: Big, bottom-right corner */
#touch-btn-shoot {
    right: 15px;
    bottom: 25px;
}

/* Jump: Left of shoot, same level */
#touch-btn-jump {
    right: 110px;
    bottom: 30px;
}

/* Crouch: Above shoot */
#touch-btn-crouch {
    right: 15px;
    bottom: 125px;
}

/* Reload: Above jump */
#touch-btn-reload {
    right: 110px;
    bottom: 110px;
}

/* Scope: Top of right column */
#touch-btn-scope {
    right: 15px;
    bottom: 200px;
}

/* Weapon switch: Top-right */
#touch-btn-weapon {
    right: 15px;
    top: 55px;
}

/* Knife: Below weapon switch */
#touch-btn-knife {
    right: 15px;
    top: 105px;
    background: radial-gradient(circle, rgba(255,200,100,0.2) 0%, rgba(200,150,50,0.1) 100%);
    border: 1.5px solid rgba(255,200,100,0.3);
    font-size: 16px;
}

#touch-btn-knife:active,
#touch-btn-knife.active {
    background: radial-gradient(circle, rgba(255,200,100,0.4) 0%, rgba(200,150,50,0.25) 100%);
    border-color: rgba(255,200,100,0.6);
}

/* === TOP-RIGHT: Utility Hub (secondary actions, clears sightline) === */

/* Grenade: Top-right hub, first position */
#touch-btn-grenade {
    right: 68px;
    top: 48px;
    transform: none;
    width: 42px;
    height: 42px;
    font-size: 14px;
    background: radial-gradient(circle, rgba(255,150,50,0.3) 0%, rgba(200,100,20,0.15) 100%);
    border: 1.5px solid rgba(255,150,50,0.4);
}

#touch-btn-grenade:active,
#touch-btn-grenade.active {
    background: radial-gradient(circle, rgba(255,150,50,0.5) 0%, rgba(200,100,20,0.3) 100%);
    border-color: rgba(255,150,50,0.7);
}

/* Emote: Top-right hub, second position */
#touch-btn-emote {
    right: 116px;
    top: 48px;
    width: 42px;
    height: 42px;
    font-size: 14px;
    background: radial-gradient(circle, rgba(100,200,255,0.2) 0%, rgba(60,150,220,0.1) 100%);
    border: 1.5px solid rgba(100,200,255,0.3);
}

#touch-btn-emote:active,
#touch-btn-emote.active {
    background: radial-gradient(circle, rgba(100,200,255,0.4) 0%, rgba(60,150,220,0.25) 100%);
    border-color: rgba(100,200,255,0.6);
}

/* Score: Top-right hub, third position */
#touch-btn-score {
    right: 164px;
    top: 48px;
    width: 42px;
    height: 42px;
    font-size: 13px;
    background: radial-gradient(circle, rgba(200,200,200,0.15) 0%, rgba(150,150,150,0.08) 100%);
    border: 1.5px solid rgba(200,200,200,0.25);
}

#touch-btn-score:active,
#touch-btn-score.active {
    background: radial-gradient(circle, rgba(200,200,200,0.35) 0%, rgba(150,150,150,0.2) 100%);
    border-color: rgba(200,200,200,0.5);
}

/* BUY button: Hub second row, below grenade/emote, only during buy phase */
#touch-btn-buy {
    right: 90px;
    top: 95px;
    left: auto;
    width: 52px;
    height: 52px;
    background: radial-gradient(circle, rgba(50, 200, 50, 0.5) 0%, rgba(30, 150, 30, 0.3) 100%);
    border: 2px solid rgba(50, 255, 50, 0.6);
    box-shadow: 0 0 12px rgba(50, 255, 50, 0.3);
    font-size: 22px;
    font-weight: 900;
    font-family: 'Segoe UI', Arial, sans-serif;
    color: rgba(50, 255, 50, 0.9);
    animation: buy-pulse 1.5s ease-in-out infinite;
}

/* Chat: Top-right hub, fifth position */
#touch-btn-chat {
    right: 212px;
    top: 48px;
    width: 42px;
    height: 42px;
    font-size: 14px;
    background: radial-gradient(circle, rgba(180,160,255,0.2) 0%, rgba(120,100,200,0.1) 100%);
    border: 1.5px solid rgba(180,160,255,0.3);
}

#touch-btn-chat:active,
#touch-btn-chat.active {
    background: radial-gradient(circle, rgba(180,160,255,0.4) 0%, rgba(120,100,200,0.25) 100%);
    border-color: rgba(180,160,255,0.6);
}

#touch-btn-buy:active,
#touch-btn-buy.active {
    background: radial-gradient(circle, rgba(50, 255, 50, 0.7) 0%, rgba(30, 200, 30, 0.5) 100%);
}

@keyframes buy-pulse {
    0%, 100% { box-shadow: 0 0 12px rgba(50, 255, 50, 0.3); }
    50% { box-shadow: 0 0 20px rgba(50, 255, 50, 0.6); }
}

/* PICKUP button: Center screen, only when near weapon */
#touch-btn-pickup {
    left: 50%;
    bottom: 140px;
    transform: translateX(-50%);
    width: 60px;
    height: 60px;
    background: radial-gradient(circle, rgba(255, 200, 50, 0.5) 0%, rgba(200, 150, 30, 0.3) 100%);
    border: 2px solid rgba(255, 220, 50, 0.7);
    box-shadow: 0 0 15px rgba(255, 200, 50, 0.4);
    font-size: 22px;
    font-weight: 900;
    font-family: 'Segoe UI', Arial, sans-serif;
    color: rgba(255, 240, 100, 0.95);
    animation: pickup-pulse 1s ease-in-out infinite;
    z-index: 30;
}

#touch-btn-pickup:active,
#touch-btn-pickup.active {
    background: radial-gradient(circle, rgba(255, 220, 50, 0.8) 0%, rgba(200, 160, 30, 0.5) 100%);
}

@keyframes pickup-pulse {
    0%, 100% { transform: translateX(-50%) scale(1); }
    50% { transform: translateX(-50%) scale(1.08); }
}

/* ============================================
   ROTATE DEVICE MESSAGE (Portrait warning)
   ============================================ */
#touch-rotate-msg {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.95);
    color: white;
    font-family: 'Segoe UI', Arial, sans-serif;
    font-size: 20px;
    z-index: 10000;
    align-items: center;
    justify-content: center;
    text-align: center;
}

#touch-rotate-msg div {
    animation: rotate-bounce 2s ease-in-out infinite;
}

@keyframes rotate-bounce {
    0%, 100% { transform: rotate(-5deg); }
    50% { transform: rotate(5deg); }
}

/* ============================================
   MOBILE OVERRIDES (UI adjustments)
   ============================================ */

/* Make HUD elements bigger on mobile */
@media (max-width: 1024px) and (hover: none) {
    /* DO NOT force #touch-controls display here — JS .active class handles it */
    
    /* Hide desktop-only elements */
    #crosshair {
        /* Keep crosshair but make it slightly bigger */
        transform: scale(1.3);
    }
    
    /* === MINIMAP: Shrink to avoid joystick overlap === */
    #minimap-container {
        width: 120px !important;
        height: 120px !important;
        top: 8px !important;
        left: 8px !important;
    }
    
    #minimap {
        width: 120px !important;
        height: 120px !important;
    }
    
    /* === STATS: Smaller on mobile === */
    #stats-container {
        top: 5px !important;
        left: 135px !important;
        font-size: 0.7em !important;
    }
    
    /* === GRENADE HUD: Miniaturize + move to top-right row === */
    #grenade-hud {
        bottom: auto !important;
        top: 8px !important;
        right: 60px !important;
        gap: 4px !important;
    }
    
    .grenade-slot {
        width: 32px !important;
        height: 32px !important;
        border-radius: 5px !important;
    }
    
    .grenade-slot .g-icon {
        font-size: 0.9em !important;
    }
    
    .grenade-slot .g-count {
        font-size: 0.55em !important;
    }
    
    .grenade-slot .g-key {
        display: none !important;
    }
    
    /* === WEAPON SLOTS: Hide on mobile (touch buttons handle switching) === */
    #weapon-slots {
        display: none !important;
    }
    
    /* Kill effects are sized in the bottom section of this media query */
    
    .kill-flash {
        opacity: 0.15 !important;
    }
    
    /* === AMMO DISPLAY: Ensure not hidden by buttons === */
    #ammo-display {
        font-size: 1em !important;
        bottom: 10px !important;
        right: auto !important;
        left: 50% !important;
        transform: translateX(-50%) !important;
    }
    
    /* === MONEY DISPLAY: Compact === */
    #money-display {
        font-size: 0.8em !important;
    }
    
    /* Move kill feed up to avoid button overlap */
    #kill-feed {
        bottom: auto !important;
        top: 50px !important;
        right: 10px !important;
        font-size: 0.75em !important;
        max-width: 200px !important;
    }
    
    /* Hide keyboard hints */
    [data-key-hint],
    .key-hint,
    .controls-hint,
    #pickup-hint kbd {
        display: none !important;
    }
    
    /* === PICKUP HINT: Simplify for mobile === */
    #pickup-hint {
        bottom: 160px !important;
        font-size: 0.85em !important;
    }
    
    /* === BUY MENU: Above touch controls + scrollable + touch-friendly === */
    #buy-menu {
        font-size: 0.85em !important;
        z-index: 600 !important;
        pointer-events: auto !important;
        touch-action: auto !important;
        max-height: 80vh !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        width: 90vw !important;
        max-width: 420px !important;
    }
    
    #buy-menu .buy-item {
        pointer-events: auto !important;
        touch-action: manipulation !important;
        cursor: pointer;
        min-height: 44px;
    }
    
    /* Make scoreboard scrollable + above touch controls */
    #scoreboard {
        font-size: 11px !important;
        z-index: 600 !important;
        pointer-events: auto !important;
        touch-action: auto !important;
        max-height: 70vh !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
    }
    
    .scoreboard-body {
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        touch-action: pan-y !important;
        max-height: 50vh !important;
    }
    
    /* === ROUND ANNOUNCEMENTS: Smaller on mobile === */
    .round-announcement {
        transform: translateX(-50%) scale(0.7) !important;
    }
    
    /* === FLASH GRENADE: Must appear ABOVE touch controls === */
    #flash-overlay {
        z-index: 900 !important;
    }
    
    /* === ROUND INFO / KILLS: Prevent overlap with warmup text === */
    #round-info {
        font-size: 0.7em !important;
        margin-top: 2px !important;
    }
    
    #round-kills {
        font-size: 0.65em !important;
        gap: 15px !important;
        margin-top: 1px !important;
    }
    
    /* === TOUCH BUTTONS: Slightly more transparent when idle === */
    .touch-btn-action,
    .touch-btn-small {
        opacity: 0.7;
    }
    
    .touch-btn-action:active,
    .touch-btn-action.active,
    .touch-btn-small:active,
    .touch-btn-small.active {
        opacity: 1;
    }
    
    /* Prevent text selection everywhere */
    * {
        -webkit-user-select: none;
        user-select: none;
        -webkit-touch-callout: none;
    }
    
    /* Full screen on mobile - but NOT menu body scroll lock */
    body {
        overflow: hidden;
        position: fixed;
        width: 100%;
        height: 100%;
    }
    
    /* === CRITICAL: Enable menu scrolling on mobile === */
    #main-menu {
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        position: fixed !important;
    }
    
    .menu-content,
    .tab-content,
    .loadout-section,
    .shop-content,
    .settings-content,
    .options-content {
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        max-height: 60vh !important;
        touch-action: pan-y !important;
    }
    
    /* === GAME MODE CARDS: Smaller on mobile === */
    .game-mode-card,
    .map-card {
        min-height: 50px !important;
        max-height: 80px !important;
        padding: 8px 12px !important;
        font-size: 0.8em !important;
    }
    
    .game-mode-card h3,
    .map-card h4 {
        font-size: 0.9em !important;
    }
    
    .game-mode-card p,
    .map-card p {
        font-size: 0.7em !important;
        display: none !important;
    }
    
    /* Lobby grid: 2 columns on mobile */
    .mode-grid,
    .map-grid {
        grid-template-columns: 1fr 1fr !important;
        gap: 8px !important;
    }
    
    /* === DEATH SCREEN: Scale down on mobile === */
    #death-screen,
    .death-content {
        font-size: 0.7em !important;
    }
    
    #death-screen h1 {
        font-size: 2em !important;
    }
    
    /* === SPECTATOR/KILLCAM: Miniaturized on mobile === */
    #spectator-ui {
        bottom: 60px !important;
        max-width: 220px !important;
        transform: translateX(-50%) scale(0.75) !important;
        opacity: 0.85 !important;
    }
    
    #spectator-ui > div {
        padding: 8px 14px !important;
        border-radius: 8px !important;
    }
    
    #spec-target-name {
        font-size: 16px !important;
    }
    
    /* Hide desktop hints (LMB/RMB) on mobile */
    #spectator-ui span[style*="LMB"],
    #spectator-ui span[style*="RMB"],
    #spectator-ui div:last-child {
        display: none !important;
    }
    
    /* === CHAT / SERVER MESSAGES: Compact on mobile === */
    #chat-container {
        max-width: 180px !important;
        font-size: 0.65em !important;
        opacity: 0.7 !important;
    }
    
    #chat-messages {
        max-height: 100px !important;
        font-size: 0.8em !important;
    }
    
    #chat-messages div {
        padding: 1px 4px !important;
        line-height: 1.2 !important;
    }
    
    /* === SHOP: Eye/inspect icon always visible (no hover on mobile) === */
    .item-inspect-btn {
        opacity: 1 !important;
        width: 36px !important;
        height: 36px !important;
    }
    
    /* === GRENADE PREVIEW: Smaller text on mobile === */
    #grenade-preview {
        font-size: 0.7em !important;
        padding: 4px 10px !important;
        opacity: 0.8 !important;
    }
    
    /* === CHAT INPUT: Mobile friendly, above keyboard === */
    #chat-input-container {
        z-index: 700 !important;
        pointer-events: auto !important;
        touch-action: auto !important;
    }
    
    #chat-input-container.active {
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        width: 100% !important;
        padding: 8px !important;
        background: rgba(0,0,0,0.9) !important;
    }
    
    #chat-input {
        pointer-events: auto !important;
        touch-action: auto !important;
        font-size: 16px !important; /* Prevents iOS zoom on focus */
    }
    
    /* === KILL EFFECTS: Even smaller (50% of desktop) === */
    .kill-indicator {
        font-size: 16px !important;
        top: 25% !important;
    }
    
    .kill-indicator.headshot {
        font-size: 18px !important;
    }
    
    .kill-indicator.killstreak {
        font-size: 20px !important;
    }
    
    .kill-skull {
        font-size: 36px !important;
        top: 20% !important;
    }
    
    .headshot-indicator {
        font-size: 16px !important;
        top: 20% !important;
    }
    
    /* === MENU: Bigger touch targets for tabs === */
    .main-menu .menu-tabs button,
    .menu-tab {
        min-height: 52px !important;
        padding: 10px 16px !important;
        font-size: 0.75em !important;
    }
    
    /* Settings tabs */
    .settings-tab,
    .opt-tab {
        min-height: 44px !important;
        padding: 8px 14px !important;
    }
    
    /* General buttons bigger */
    .game-mode-card,
    .map-card {
        min-height: 60px !important;
    }
}

/* ============================================
   LANDSCAPE OPTIMIZATIONS
   ============================================ */
@media (max-height: 450px) {
    /* Very short screens (phones in landscape) */
    #touch-btn-shoot {
        bottom: 15px;
        width: 70px;
        height: 70px;
    }
    
    #touch-btn-jump {
        bottom: 18px;
        right: 100px;
        width: 50px;
        height: 50px;
    }
    
    #touch-btn-reload {
        bottom: 85px;
        right: 100px;
        width: 44px;
        height: 44px;
    }
    
    #touch-btn-crouch {
        bottom: 100px;
        right: 15px;
        width: 44px;
        height: 44px;
    }
    
    #touch-btn-scope {
        bottom: 170px;
        right: 15px;
        width: 40px;
        height: 40px;
    }
    
    /* Shrink utility hub buttons for short screens */
    #touch-btn-grenade,
    #touch-btn-emote,
    #touch-btn-score,
    #touch-btn-chat {
        width: 36px !important;
        height: 36px !important;
        font-size: 10px !important;
        top: 42px !important;
    }
    
    #touch-btn-buy {
        width: 42px !important;
        height: 42px !important;
        top: 82px !important;
        right: 80px !important;
    }
    
    /* Knife: shrink for short screens */
    #touch-btn-knife {
        top: 95px;
        width: 38px !important;
        height: 38px !important;
        font-size: 13px !important;
    }
}

/* ============================================
   TABLET OPTIMIZATION (Bigger buttons)
   ============================================ */
@media (min-width: 768px) and (max-width: 1024px) and (hover: none) {
    .touch-btn-shoot {
        width: 90px !important;
        height: 90px !important;
    }
    
    .touch-btn-action {
        width: 64px !important;
        height: 64px !important;
    }
    
    #touch-joystick-base {
        width: 150px;
        height: 150px;
    }
    
    #touch-joystick-thumb {
        width: 60px;
        height: 60px;
        margin: -30px 0 0 -30px;
    }
}