/* Reset default styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* ===== Base Layout ===== */
html, body {
    width: 100%;
    height: 100vh;
    height: 100dvh; /* dynamic viewport height for better iOS handling */
    overflow: hidden; /* Prevent scrolling */
    background-color: #f0f0f0;
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed; /* Ensure no scrolling */
    touch-action: none; /* Prevent unwanted touch behaviors */
}

/* Full-screen container */
.main-container {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    position: relative; /* Important for absolute positioning of panel */
    padding-bottom: 0 !important; /* Reset padding and let bottom-offset handle it */
    overflow: hidden;
}

/* Phaser viewport */
.phaser-viewport {
    flex: 4; /* Using flex like index.html instead of fixed percentage */
    background-color: #33FF57; /* Temporary color */
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative; /* For panel positioning reference */
    transition: flex 0.3s ease;
    overflow: hidden;
    touch-action: none; /* Allow all gestures on the game canvas */
}

/* ===== Input and Chat ===== */
/* Input Container */
.input-container {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 5px;
    justify-content: space-between;
    white-space: nowrap; /* Prevents wrapping */
    width: 100%;
    max-width: 100%; /* Ensures container does not exceed screen width */
    z-index: 15; /* Stay above panel */
    flex: 0 0 auto; /* Don't flex this container */
    position: relative;
    transition: all 0.3s ease;
    transform: scale(1) !important;
    touch-action: manipulation;
}

/* Chat Input */
.chat-input {
    -webkit-appearance: none;
    -webkit-user-select: text;
    user-select: text;
    -webkit-touch-callout: default; /* Allow iOS text selection */
    border-radius: 20px;
    padding: 10px 15px;
    border: 1px solid #ccc;
    font-size: 16px; /* Prevents iOS zoom */
    width: 100%;
    background-color: white;
    z-index: 1000;
    touch-action: manipulation;
}

.chat-input:focus {
    outline: none;
    border-color: #007bff;
    box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}

/* Controller Button */
#controller-toggle {
    width: 40px;
    height: 40px;
    background: none;
    border: none;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    touch-action: manipulation;
}

/* Send Button */
.send-button {
    background-color: #e6388a;
    color: white;
    padding: 10px 20px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    text-decoration: none;
    font-weight: bold;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    text-shadow: 2px 2px 0 #555555; /* Darker grey shadow with 2px offset */
    font-family: 'VT323', monospace; /* Same font as game dialogue */
    font-size: 22px; /* Adjusted for VT323 font */
    transition: all 0.2s ease;
}

.send-button:hover {
    background-color: #c62f73;
}

.send-button:active {
    transform: scale(0.95);
}

/* Send Button Icon */
.send-button img {
    width: 24px;
    height: 24px;
}

/* Microphone Button */
.mic-button {
    background: var(--dark-bg);
    color: white;
    border: 2px solid white;
    padding: 8px 16px;
    cursor: pointer;
    font-size: 14px;
    text-transform: uppercase;
    border-radius: 4px;
    transition: all 0.2s ease;
    margin-left: 5px;
}

.mic-button:hover {
    background: white;
    color: var(--dark-bg);
}

.mic-button:active {
    transform: scale(0.95);
}

.mic-button.active {
    background: #ff4444;
    border-color: #ff4444;
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.05);
    }
    100% {
        transform: scale(1);
    }
}

/* ===== Controller Layout ===== */
/* Controller container */
.controller-container {
    transform: scale(1) !important;
    touch-action: manipulation;
    flex: 4; /* Match flex like in index.html */
    background-image: url('controllerBG.png');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.3s ease-in-out;
    z-index: 10; /* Stay above panel */
    position: relative;
    overflow: hidden; /* Keep overflow hidden for controlled system */
    padding-bottom: 0 !important; /* Reset padding to prevent double padding */
    margin-bottom: 0 !important; /* Reset margin too */
}

/* Controller decoration */
.controller-container::before {
    content: '';
    position: absolute;
    top: 0; /* Changed from -20px to 0 */
    right: 0; /* Changed from -20px to 0 */
    width: 225px; /* Reduced size to fit within container */
    height: 225px; /* Reduced size to fit within container */
    background-image: url('controllerDecor.png');
    background-size: contain;
    background-repeat: no-repeat;
    background-position: top right; /* Align to top right corner */
    z-index: 11; /* Above the background but below other elements */
    pointer-events: none; /* Ensures it doesn't interfere with controls */
}

/* Ensure other controller elements stay above the decoration */
.left, .middle, .right, .button-img, .middle-button, #joystick-container {
    position: relative;
    z-index: 12;
}

/* Three equal sections inside the controller */
.left {
    flex: 1;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.middle {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 15px;
    padding: 2px;
    position: relative;
    width: 60%;
    margin: 0 auto;
}

.middle-container {
    width: 100%;
    background-color: white;
    border-radius: 4px;
    padding: 8px;
}

.button-labels {
    display: flex;
    justify-content: space-around;
    width: 100%;
    font-family: 'PowerClearBold', sans-serif;
    font-size: 12px;
    font-weight: bold;
    color: black;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.button-row {
    display: flex;
    justify-content: space-around;
    width: 100%;
    margin-top: -8px;  /* Pull buttons up closer to the container */
}

.middle-button {
    width: 45px;
    height: 15px;
    font-size: 8px;
    border: none;
    background-color: black;
    border-radius: 2px;
    cursor: pointer;
    text-align: center;
    touch-action: manipulation;
}

.right {
    height: 100%;
    flex: 1;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    flex-direction: row;
}

/* Button Image Styling */
.button-img {
    width: 100px;
    height: 100px;
    image-rendering: pixelated; /* Keeps pixel art look */
    cursor: pointer;
    -webkit-touch-callout: none;
    user-select: none;
    touch-action: manipulation;
}

/* Joystick container */
#joystick-container {
    width: 100px;
    height: 100px;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    touch-action: manipulation;
}

/* Hide controller state (for toggle functionality) */
.controller-container.hidden {
    flex: 0 !important;
    height: 0 !important;
    overflow: hidden;
    padding: 0 !important;
    visibility: hidden;
}

/* ===== Game Panel (Pokémon Style) ===== */
/* Game Panel - Overlay panel - Pokemon Style */
#game-panel {
    position: absolute;
    background-color: white;
    color: black;
    padding: 0;
    display: none; /* Start hidden */
    z-index: 5; /* Above game but below controls */
    border: 2px solid black;
    
    /* Default positioning (will be updated by JS) */
    top: 0;
    right: 0;
    width: 40%; 
    height: auto;
    overflow: hidden; /* No scrolling */
    font-family: monospace, sans-serif; /* System monospace font */
}

/* Panel Open State */
#game-panel.active {
    display: block;
}

/* Pokemon-style menu header */
#game-panel h2 {
    font-size: 18px;
    text-align: center;
    margin: 0;
    padding: 10px 0;
    background-color: white;
    border-bottom: 1px solid black;
    font-weight: bold;
}

/* Menu items container */
.menu-buttons {
    display: flex;
    flex-direction: column;
    width: 100%;
    margin: 0;
}

/* Individual menu buttons - Pokemon style */
.menu-btn {
    background-color: white;
    color: black;
    border: none;
    padding: 10px 25px;
    font-size: 14px;
    cursor: pointer;
    text-align: left;
    transition: none;
    position: relative;
    font-weight: bold;
    border: none;
    border-radius: 0;
}

/* Selected button state */
.menu-btn.active, .menu-btn.selected {
    background-color: #3b66b0; /* Light blue highlight color */
    color: white;
}

/* Exit button special styling */
#logout-btn, #exit-btn {
    background-color: #cc5555; /* Reddish color for exit */
    color: white;
}

#logout-btn.active, #exit-btn.active {
    background-color: #aa3333; /* Darker red when selected */
}

/* Arrow pointer for selected item */
.menu-btn.active:before {
    content: '▶';
    position: absolute;
    left: 8px;
    top: 50%;
    transform: translateY(-50%);
}

/* ===== Mobile Safe Area Handling ===== */
/* Ensure safe area for mobile devices */
:root {
    --safe-area-inset-top: env(safe-area-inset-top, 0px);
    --safe-area-inset-right: env(safe-area-inset-right, 0px);
    --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-inset-left: env(safe-area-inset-left, 0px);
}

/* Bottom Offset for mobile devices - This is the ONLY element that should have bottom padding */
.bottom-offset {
    width: 100%;
    background: rgb(68, 67, 69);
    flex: 0 0 auto; /* Never grow or shrink */
    position: relative;
    z-index: 5;
}

/* iOS specific adjustments */
.ios-device .bottom-offset {
    height: var(--safe-area-inset-bottom, 0px) !important;
}

/* ===== Responsive Adjustments ===== */
/* Responsive for small screens */
@media (max-width: 400px) {
    .chat-input {
        min-width: 30px; /* Allows text input to shrink more */
    }
    
    .right {
        flex-direction: column; /* Stack buttons */
    }
    
    .button-img {
        width: 80px;
        height: 80px;
    }
}

/* Landscape mode adjustments */
@media screen and (max-width: 768px) and (orientation: landscape) {
    body {
        /* Remove rotation that causes problems */
        transform: none;
        width: 100%;
        height: 100%;
    }
    
    /* Adjust layout for landscape orientation */
    .button-img {
        width: 70px;
        height: 70px;
    }
    
    .middle-button {
        width: 45px;
        height: 20px;
        font-size: 7px;
    }
}

/* ===== FIXED Mobile Keyboard Handling ===== */
/* Base keyboard open state */
.keyboard-open {
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
}

/* When keyboard is open - Phaser viewport takes all available space */
.keyboard-open .phaser-viewport {
    flex: 1 !important; /* Take all available space */
    position: relative;
    transition: all 0.2s ease-out;
}

/* Always hide controller when keyboard is open */
.keyboard-open .controller-container {
    display: none !important;
    flex: 0 !important;
    height: 0 !important;
}

/* Input container positioning */
.keyboard-open .input-container {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 8px 10px;
    background-color: rgba(255, 255, 255, 0.95);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.15);
    z-index: 1000;
}

/* Ensure game panel doesn't interfere with keyboard */
.keyboard-open #game-panel {
    display: none !important;
}

/* Platform-specific adjustments - Android */
@supports not (-webkit-touch-callout: none) {
    .keyboard-open .phaser-viewport {
        /* Let the viewport take all available space */
        height: auto !important;
    }
    
    .keyboard-open .input-container {
        /* Ensure input stays at the bottom */
        position: fixed;
        bottom: 0;
        z-index: 1000;
    }
}

/* Platform-specific adjustments - iOS */
@supports (-webkit-touch-callout: none) {
    .keyboard-open .phaser-viewport {
        /* iOS needs a specific height to account for keyboard overlay */
        height: 70vh !important;
    }
    
    .keyboard-open .input-container {
        /* Add safe area padding for iOS */
        padding-bottom: max(10px, env(safe-area-inset-bottom));
    }
}

/* Ensure canvas fits properly in available space */
canvas {
    object-fit: contain;
    max-width: 100%;
    max-height: 100%;
}

/* Detection element for viewport changes */
.keyboard-measuring {
    position: fixed;
    height: 100vh;
    width: 1px;
    pointer-events: none;
    opacity: 0;
    bottom: 0;
}

#phaser-game {
    flex: 8;
    width: 100%;
    height: 100%;
    background-color: #000000;
    position: relative;
    overflow: hidden;
}

@font-face {
    font-family: 'PowerClearBold';
    src: url('power-clear-bold.ttf') format('truetype');
    font-display: swap;
}