/* Mobile Touch Controls - Only active on touch devices */

/* Rotate device overlay - shown when in portrait mode on touch devices */
#rotate-device-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: #000;
    z-index: 9999;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    color: #fff;
    font-family: 'Press Start 2P', monospace;
    text-align: center;
    padding: 20px;
}

#rotate-device-overlay .rotate-icon {
    font-size: 48px;
    margin-bottom: 20px;
    animation: rotate-hint 2s ease-in-out infinite;
}

#rotate-device-overlay .rotate-text {
    font-size: 14px;
    line-height: 1.8;
}

@keyframes rotate-hint {
    0%, 100% { transform: rotate(0deg); }
    25% { transform: rotate(-20deg); }
    75% { transform: rotate(20deg); }
}

/* Mobile layout wrapper */
#mobile-game-wrapper {
    display: none;
}

/* Touch controls containers */
#touch-controls-left,
#touch-controls-right {
    display: none;
	height: 110%;
}

/* Padding to account for overflow area in iOS */
#touch-controls-left-pad, #touch-controls-right-pad {
	position: absolute;
	background-color: #000;
	top: 0;
	width: 100px;
	height: 100%;
	z-index: 499;
}
#touch-controls-left-pad {
	left: 0;
}
#touch-controls-right-pad {
	right: 0;
}

/* ==================== TOUCH DEVICE STYLES ==================== */
/* These styles only apply on touch-enabled devices */

.touch-device #mobile-game-wrapper {
    display: flex;
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    background: #000;
    /* Account for safe areas (notch, dynamic island) */
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
}

.touch-device #game-container {
    /* Zoom is applied by JS to fit available space */
    flex: 1;
    min-width: 0;
    /* Prevent text selection and context menus on touch */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    /* Prevent unwanted touch interactions while allowing buttons */
    touch-action: manipulation;
}

/* Explicitly allow buttons to be interactive */
.touch-device #game-container button,
.touch-device #game-container .button,
.touch-device #game-container #menu button {
    -webkit-touch-callout: default;
    -webkit-user-select: auto;
    user-select: auto;
    touch-action: auto;
    pointer-events: auto;
}

/* Remove scale wrapper if it exists from previous approach */
#game-scale-wrapper {
    display: contents;
}

.touch-device #touch-controls-left {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: #000;
    z-index: 500;
    flex-shrink: 0;
    /* Wider to contain d-pad without overlap */
    width: 180px;
    min-width: 160px;
}

.touch-device #touch-controls-right {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: #000;
    z-index: 500;
    flex-shrink: 0;
    /* Narrower since buttons are vertical */
    width: 100px;
    min-width: 90px;
}

/* Extra padding for safe areas */
.touch-device #touch-controls-left {
    /* padding-left: max(10px, env(safe-area-inset-left, 10px)); */
}

.touch-device #touch-controls-right {
    /* padding-right: max(10px, env(safe-area-inset-right, 10px)); */
}

/* ==================== D-PAD STYLES ==================== */

.dpad-container {
    position: relative;
    width: 140px;
    height: 140px;
    /* Prevent text selection and context menus */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    touch-action: none;
}

.dpad-btn {
    position: absolute;
    width: 46px;
    height: 46px;
    background: #222;
    border: 3px solid #444;
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 18px;
    color: #888;
    box-shadow: 
        inset 0 -3px 0 #111,
        0 2px 4px rgba(0,0,0,0.5);
    transition: background-color 0.05s, transform 0.05s;
    /* Prevent text selection and context menus */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    touch-action: none;
}

.dpad-btn.active {
    background: #0a0;
    border-color: #0f0;
    color: #fff;
    box-shadow: 
        inset 0 3px 0 #080,
        0 0 10px rgba(0,255,0,0.5);
    transform: scale(0.95);
}

/* D-pad button positions (plus sign layout) */
.dpad-up {
    top: 0;
    left: 50%;
    transform: translateX(-50%);
}
.dpad-up.active {
    transform: translateX(-50%) scale(0.95);
}

.dpad-down {
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
}
.dpad-down.active {
    transform: translateX(-50%) scale(0.95);
}

.dpad-left {
    left: 0;
    top: 50%;
    transform: translateY(-50%);
}
.dpad-left.active {
    transform: translateY(-50%) scale(0.95);
}

.dpad-right {
    right: 0;
    top: 50%;
    transform: translateY(-50%);
}
.dpad-right.active {
    transform: translateY(-50%) scale(0.95);
}

/* D-pad center piece (decorative) */
.dpad-center {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 44px;
    height: 44px;
    background: #1a1a1a;
    border: 2px solid #333;
    border-radius: 50%;
    pointer-events: none;
}

/* D-pad arrows */
.dpad-btn::after {
    content: '';
    width: 0;
    height: 0;
    border-style: solid;
}

.dpad-up::after {
    border-width: 0 10px 14px 10px;
    border-color: transparent transparent currentColor transparent;
}

.dpad-down::after {
    border-width: 14px 10px 0 10px;
    border-color: currentColor transparent transparent transparent;
}

.dpad-left::after {
    border-width: 10px 14px 10px 0;
    border-color: transparent currentColor transparent transparent;
}

.dpad-right::after {
    border-width: 10px 0 10px 14px;
    border-color: transparent transparent transparent currentColor;
}

/* ==================== ACTION BUTTONS ==================== */

.action-buttons {
    display: flex;
    flex-direction: column;
    gap: 12px;
    /* Prevent text selection and context menus */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    touch-action: none;
}

.action-btn {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background: #222;
    border: 3px solid #444;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Press Start 2P', monospace;
    font-size: 14px;
    color: #FFF;
    box-shadow: 
        inset 0 -4px 0 #111,
        0 3px 6px rgba(0,0,0,0.5);
    transition: background-color 0.05s, transform 0.05s;
    /* Prevent text selection and context menus */
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    touch-action: none;
}

.action-btn.active {
    background: #0a0;
    border-color: #0f0;
    color: #fff;
    box-shadow: 
        inset 0 4px 0 #080,
        0 0 15px rgba(0,255,0,0.5);
    transform: scale(0.92);
}

/* Individual button colors when active */
.action-btn-z.active {
    background: #a00;
    border-color: #f00;
    box-shadow: 
        inset 0 4px 0 #800,
        0 0 15px rgba(255,0,0,0.5);
}

.action-btn-x.active {
    background: #00a;
    border-color: #00f;
    box-shadow: 
        inset 0 4px 0 #008,
        0 0 15px rgba(0,0,255,0.5);
}

.action-btn-a.active {
    background: #a80;
    border-color: #fa0;
    box-shadow: 
        inset 0 4px 0 #850,
        0 0 15px rgba(255,170,0,0.5);
}

/* ==================== PORTRAIT MODE DETECTION ==================== */

/* Show rotate overlay in portrait on touch devices */
@media (orientation: portrait) {
    .touch-device #rotate-device-overlay {
        display: flex;
    }
    
    .touch-device #mobile-game-wrapper {
        display: none;
    }
}

/* ==================== RESPONSIVE SCALING ==================== */

/* 
 * On mobile, CSS scaling doesn't work well because the game JS uses
 * window.innerWidth/innerHeight for positioning. Instead, we adjust
 * the camera lookAhead in JS to center the player better on small screens.
 */

/* Small phones (iPhone SE size ~667px landscape width) - control sizing */
@media (max-height: 400px) and (orientation: landscape) {
    .touch-device #touch-controls-left {
        width: 160px;
        min-width: 140px;
        height: 110%;
    }
    
    .touch-device #touch-controls-right {
        width: 90px;
        min-width: 80px;
        height: 110%;
    }
    
    .dpad-container {
        width: 120px;
        height: 120px;
    }
    
    .dpad-btn {
        width: 40px;
        height: 40px;
    }
    
    .dpad-center {
        width: 38px;
        height: 38px;
    }
    
    .action-btn {
        width: 48px;
        height: 48px;
        font-size: 12px;
    }
    
    .action-buttons {
        gap: 8px;
    }
}

/* Medium phones */
@media (min-height: 401px) and (max-height: 500px) and (orientation: landscape) {
    .touch-device #touch-controls-left {
        width: 170px;
    }
    
    .touch-device #touch-controls-right {
        width: 95px;
    }
}

/* Larger phones and small tablets */
@media (min-height: 501px) and (orientation: landscape) {
    .touch-device #touch-controls-left {
        width: 180px;
    }
    
    .touch-device #touch-controls-right {
        width: 100px;
    }
    
    .dpad-container {
        width: 150px;
        height: 150px;
    }
    
    .dpad-btn {
        width: 50px;
        height: 50px;
    }
    
    .dpad-center {
        width: 48px;
        height: 48px;
    }
    
    .action-btn {
        width: 60px;
        height: 60px;
        font-size: 16px;
    }
}

/* Large tablets (iPad Pro etc) - may not need controls shown as prominently */
@media (min-width: 1024px) and (orientation: landscape) {
    .touch-device #touch-controls-left {
        width: 190px;
    }
    
    .touch-device #touch-controls-right {
        width: 110px;
    }
    
    .dpad-container {
        width: 160px;
        height: 160px;
    }
    
    .dpad-btn {
        width: 54px;
        height: 54px;
    }
    
    .action-btn {
        width: 64px;
        height: 64px;
    }
}

/* ==================== HUD ADJUSTMENTS FOR MOBILE ==================== */

.touch-device #hud {
    /* Scale down HUD on small screens */
    transform-origin: top left;
}

@media (max-height: 400px) and (orientation: landscape) {
    .touch-device #hud {
        transform: scale(0.8);
        padding: 10px;
    }
    
    .touch-device .health-bar,
    .touch-device .cooldown-bar {
        width: 150px;
        height: 16px;
    }
    
    .touch-device #boss-health {
        top: 10px;
        right: 10px;
    }
}

/* ==================== MENU ADJUSTMENTS FOR MOBILE ==================== */

/* Hide desktop-only text on touch devices, show touch-only text */
.touch-device .desktop-only {
    display: none;
}

.touch-device .touch-only {
    display: inline;
}

/* Hide touch-only text on desktop */
.desktop-only {
    display: inline;
}

.touch-only {
    display: none;
}

.touch-device #menu {
    max-width: 90%;
    padding: 20px;
}

.touch-device #menu img {
    max-width: 100%;
    height: auto;
}

.touch-device #menu button {
    font-size: 18px;
    padding: 12px 30px;
}

.touch-device #menu .controls {
    font-size: 11px;
}

@media (max-height: 400px) and (orientation: landscape) {
    .touch-device #menu {
        padding: 15px;
        max-height: 90vh;
        overflow-y: auto;
    }
    
    .touch-device #menu img {
        width: 200px;
    }
    
    .touch-device #menu button {
        font-size: 16px;
        padding: 10px 25px;
        margin: 10px 0;
    }
    
    .touch-device #menu .controls {
        font-size: 10px;
        margin-top: 15px;
    }
}
