:root {
    --bg-color: #0f0c29;
    --bg-gradient: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
    --primary-color: #ff007f;
    --secondary-color: #00f0ff;
    --text-color: #ffffff;
    --glass-bg: rgba(255, 255, 255, 0.05);
    --glass-border: rgba(255, 255, 255, 0.1);
    --glass-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
    --font-main: 'Outfit', sans-serif;
    --font-pixel: 'Press Start 2P', cursive;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
}

body {
    background: var(--bg-gradient);
    color: var(--text-color);
    font-family: var(--font-main);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow-x: hidden;
}

/* Typography & Glitch */
.glitch-text {
    font-family: var(--font-pixel);
    font-size: 2.5rem;
    position: relative;
    text-shadow: 2px 2px var(--primary-color), -2px -2px var(--secondary-color);
    margin-bottom: 1rem;
    text-align: center;
}

.subtitle {
    text-align: center;
    font-size: 1.1rem;
    opacity: 0.8;
    margin-bottom: 2rem;
}

/* Glassmorphism */
.glass-panel {
    background: var(--glass-bg);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid var(--glass-border);
    border-radius: 20px;
    padding: 2rem;
    box-shadow: var(--glass-shadow);
}

/* Landing Page */
.landing-container {
    width: 100%;
    max-width: 800px;
    padding: 1rem;
}

.actions {
    display: flex;
    gap: 1.5rem;
    justify-content: center;
    flex-wrap: wrap;
}

.card {
    flex: 1;
    min-width: 250px;
    background: rgba(255,255,255,0.03);
    border: 1px solid var(--glass-border);
    border-radius: 15px;
    padding: 2rem;
    text-align: center;
    cursor: pointer;
    transition: transform 0.3s ease, box-shadow 0.3s ease, background 0.3s ease;
}

.card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 20px rgba(0, 240, 255, 0.2);
    background: rgba(255,255,255,0.08);
}

.card .icon {
    font-size: 4rem;
    margin-bottom: 1rem;
}

.card h2 {
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
    color: var(--secondary-color);
}

/* Host Page */
.host-body {
    align-items: flex-start;
    padding: 2rem;
}

.host-container {
    display: flex;
    gap: 2rem;
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    flex-wrap: wrap;
}

.sidebar {
    flex: 1;
    min-width: 300px;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.room-code-box {
    background: rgba(0,0,0,0.5);
    padding: 1rem;
    border-radius: 10px;
    text-align: center;
    border: 1px solid var(--primary-color);
}

#roomCodeDisplay {
    font-family: var(--font-pixel);
    font-size: 2rem;
    color: var(--secondary-color);
    letter-spacing: 5px;
}

.btn {
    padding: 1rem 2rem;
    border: none;
    border-radius: 10px;
    font-family: var(--font-main);
    font-weight: 800;
    font-size: 1.1rem;
    cursor: pointer;
    transition: all 0.2s;
    text-transform: uppercase;
    text-align: center;
    display: block;
    width: 100%;
}

.primary-btn {
    background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
    color: white;
    box-shadow: 0 4px 15px rgba(255, 0, 127, 0.4);
}

.primary-btn:hover {
    transform: scale(1.02);
    box-shadow: 0 6px 20px rgba(0, 240, 255, 0.6);
}

/* TV Focus Styles */
.tv-focusable:focus {
    outline: 4px solid var(--secondary-color);
    box-shadow: 0 0 20px var(--secondary-color), inset 0 0 10px var(--secondary-color);
    transform: scale(1.05);
    background: var(--primary-color);
    color: white;
}
select.tv-focusable:focus {
    background: #111;
}

.players-list ul {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.players-list li {
    background: rgba(0,0,0,0.3);
    padding: 1rem;
    border-radius: 8px;
    display: flex;
    justify-content: space-between;
    font-weight: 600;
}

.players-list li.online {
    border-left: 4px solid #00ff00;
    color: #fff;
}
.players-list li.offline {
    border-left: 4px solid #555;
    color: #888;
}

.main-screen {
    flex: 3;
    min-width: 300px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.tv-frame {
    background: #111;
    padding: 2rem;
    border-radius: 20px;
    border: 4px solid #333;
    box-shadow: 0 20px 50px rgba(0,0,0,0.8), inset 0 0 20px rgba(0,0,0,1);
    position: relative;
    width: 100%;
    max-width: 800px;
    aspect-ratio: 4/3;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Fullscreen Button */
.fullscreen-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    background: rgba(0, 0, 0, 0.5);
    color: white;
    border: 1px solid var(--glass-border);
    border-radius: 5px;
    width: 40px;
    height: 40px;
    font-size: 1.5rem;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10;
    transition: all 0.2s ease;
}
.fullscreen-btn:hover {
    background: rgba(0, 0, 0, 0.8);
    color: var(--secondary-color);
}
.fullscreen-btn:active {
    transform: scale(0.9);
}

/* Fix tv-frame when fullscreen */
.tv-frame:fullscreen {
    padding: 0;
    border: none;
    border-radius: 0;
    max-width: none;
    aspect-ratio: auto;
}
.tv-frame:fullscreen .fullscreen-btn {
    display: none;
}

canvas#emulatorCanvas {
    width: 100%;
    height: 100%;
    background: #000;
    image-rendering: pixelated;
    border-radius: 5px;
}

.controls-hint {
    background: var(--glass-bg);
    padding: 1rem;
    border-radius: 10px;
    text-align: center;
    font-size: 0.9rem;
    width: 100%;
}

/* Player Controller Page */
.player-body {
    padding: 0;
    margin: 0;
    overflow: hidden;
    touch-action: none; /* Prevent scroll/zoom on mobile */
    background: #000;
}

.join-container {
    width: 90%;
    max-width: 400px;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    text-align: center;
}

.join-container input {
    padding: 1rem;
    border-radius: 10px;
    border: 1px solid var(--glass-border);
    background: rgba(0,0,0,0.5);
    color: white;
    font-family: var(--font-pixel);
    font-size: 1.5rem;
    text-align: center;
    letter-spacing: 5px;
}
.join-container input:focus {
    outline: none;
    border-color: var(--secondary-color);
}

.error-text {
    color: #ff4444;
    font-weight: 600;
    min-height: 1.5rem;
}

/* Gamepad Layout */
.controller-container {
    width: 100vw;
    height: 100vh;
    display: flex;
    flex-direction: column;
    background: radial-gradient(circle at center, #2a2d34 0%, #111317 100%);
    box-shadow: inset 0 0 50px rgba(0,0,0,0.8);
}

.top-bar {
    height: 12vh;
    min-height: 50px;
    background: linear-gradient(180deg, #1a1a1a 0%, #0a0a0a 100%);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 max(1rem, env(safe-area-inset-right)) 0 max(1rem, env(safe-area-inset-left));
    border-bottom: 2px solid #333;
    box-shadow: 0 4px 10px rgba(0,0,0,0.5);
    z-index: 20;
}

.player-badge {
    font-family: var(--font-pixel);
    font-size: 0.8rem;
    color: var(--secondary-color);
    text-shadow: 0 0 5px var(--secondary-color);
    background: rgba(0, 240, 255, 0.1);
    padding: 5px 10px;
    border-radius: 5px;
    border: 1px solid rgba(0, 240, 255, 0.3);
}

.room-indicator {
    font-size: 0.9rem;
    color: #aaa;
    display: flex;
    flex-direction: column;
    align-items: center;
}
#currentRoomCode {
    font-family: var(--font-pixel);
    color: #fff;
    font-size: 1rem;
    margin-top: 2px;
}

select#playerRomSelect {
    background: #111;
    color: var(--secondary-color);
    border: 1px solid #444;
    border-radius: 8px;
    font-family: var(--font-main);
    font-weight: 600;
    padding: 5px 10px;
    font-size: 0.8rem;
    max-width: 130px;
    outline: none;
    cursor: pointer;
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.5);
}
select#playerRomSelect:focus {
    border-color: var(--secondary-color);
}

.top-controls button {
    background: #222;
    border: 1px solid #444;
    border-radius: 8px;
    color: #fff;
    width: 35px;
    height: 35px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2rem;
}

.gamepad {
    flex: 1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem max(3rem, env(safe-area-inset-right)) 2rem max(3rem, env(safe-area-inset-left));
    position: relative;
    overflow: hidden;
}

/* D-PAD */
.dpad {
    position: relative;
    width: 160px;
    height: 160px;
    filter: drop-shadow(0 10px 15px rgba(0,0,0,0.6));
}

.dpad-btn {
    position: absolute;
    width: 54px;
    height: 54px;
    background: linear-gradient(145deg, #3a3d45, #25272d);
    border: 1px solid #111;
    color: #888;
    font-size: 1.5rem;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: inset 2px 2px 5px rgba(255,255,255,0.1), inset -2px -2px 5px rgba(0,0,0,0.5);
    z-index: 2;
}

.dpad-btn:active, .dpad-btn.pressed {
    background: linear-gradient(145deg, #25272d, #3a3d45);
    box-shadow: inset 3px 3px 8px rgba(0,0,0,0.8);
    color: var(--secondary-color);
}

.dpad-btn.up { top: 0; left: 53px; border-radius: 12px 12px 0 0; border-bottom: none; }
.dpad-btn.right { top: 53px; right: 0; border-radius: 0 12px 12px 0; border-left: none; }
.dpad-btn.down { bottom: 0; left: 53px; border-radius: 0 0 12px 12px; border-top: none; }
.dpad-btn.left { top: 53px; left: 0; border-radius: 12px 0 0 12px; border-right: none; }

.dpad-center {
    position: absolute;
    top: 53px; left: 53px;
    width: 54px; height: 54px;
    background: #2f3239;
    border-radius: 5px;
    z-index: 1;
    box-shadow: inset 0 0 10px rgba(0,0,0,0.5);
}
.dpad-center::after {
    content: '';
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    width: 20px; height: 20px;
    background: #222;
    border-radius: 50%;
    box-shadow: inset 1px 1px 3px rgba(0,0,0,0.8);
}

/* Action Buttons (SNES Layout) */
.action-buttons-snes {
    position: relative;
    width: 170px;
    height: 170px;
    transform: rotate(-15deg);
    background: rgba(0,0,0,0.1);
    border-radius: 50%;
    box-shadow: inset 0 5px 15px rgba(0,0,0,0.3), 0 10px 20px rgba(0,0,0,0.4);
}

.action-btn {
    border: none;
    outline: none;
    color: rgba(255,255,255,0.7);
    font-family: var(--font-main);
    font-weight: 800;
}

.round-btn {
    position: absolute;
    width: 58px;
    height: 58px;
    border-radius: 50%;
    font-size: 1.4rem;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 8px 15px rgba(0,0,0,0.6), inset 0 4px 8px rgba(255,255,255,0.4), inset 0 -4px 8px rgba(0,0,0,0.3);
    border: 2px solid #111;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.8);
}

/* SNES colors with 3D gradients */
.btn-x { top: 5px; left: 56px; background: radial-gradient(circle at 40% 30%, #0088e0, #003366); }
.btn-y { top: 56px; left: 5px; background: radial-gradient(circle at 40% 30%, #5cd60e, #225503); }
.btn-a { top: 56px; right: 5px; background: radial-gradient(circle at 40% 30%, #ff1a33, #80000d); }
.btn-b { bottom: 5px; left: 56px; background: radial-gradient(circle at 40% 30%, #ffcc00, #806600); }

.round-btn:active, .round-btn.pressed {
    box-shadow: 0 2px 5px rgba(0,0,0,0.8), inset 0 6px 12px rgba(0,0,0,0.6);
    transform: translateY(4px);
    color: white;
}

/* Shoulder Buttons */
.shoulder-buttons {
    position: absolute;
    top: 15px; 
    width: 100%;
    display: flex;
    justify-content: space-between;
    padding: 0 max(2rem, env(safe-area-inset-right)) 0 max(2rem, env(safe-area-inset-left));
    z-index: 10;
}

.shoulder-btn {
    width: 120px;
    height: 35px;
    border-radius: 20px;
    background: linear-gradient(180deg, #555, #222);
    border: 2px solid #111;
    color: #ccc;
    font-size: 1rem;
    box-shadow: 0 5px 10px rgba(0,0,0,0.5), inset 0 2px 5px rgba(255,255,255,0.2);
}
.shoulder-btn:active, .shoulder-btn.pressed {
    background: #222;
    box-shadow: inset 0 5px 10px rgba(0,0,0,0.8);
    transform: translateY(2px);
    color: white;
}

/* Center Buttons (Start, Select) */
.center-buttons {
    position: absolute;
    bottom: 15%;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 1.5rem;
    background: rgba(0,0,0,0.2);
    padding: 10px 20px;
    border-radius: 30px;
    box-shadow: inset 0 2px 10px rgba(0,0,0,0.5);
}

.pill-btn {
    width: 65px;
    height: 22px;
    border-radius: 11px;
    background: linear-gradient(180deg, #444, #111);
    border: 1px solid #000;
    font-size: 0.65rem;
    color: #aaa;
    text-transform: uppercase;
    transform: rotate(-15deg);
    box-shadow: 0 4px 6px rgba(0,0,0,0.6), inset 0 1px 2px rgba(255,255,255,0.2);
}
.pill-btn:active, .pill-btn.pressed {
    background: #111;
    box-shadow: inset 0 4px 8px rgba(0,0,0,0.8);
    transform: rotate(-15deg) translateY(2px);
    color: white;
}

/* Analog Stick */
.analog-stick {
    position: absolute;
    bottom: 15%;
    left: 10%;
    width: 160px;
    height: 160px;
}
.analog-base {
    width: 100%;
    height: 100%;
    background: radial-gradient(circle, #2a2d34, #111317);
    border-radius: 50%;
    border: 4px solid #0a0a0c;
    box-shadow: inset 0 10px 20px rgba(0,0,0,0.8), 0 5px 15px rgba(0,0,0,0.5);
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
}
.analog-thumb {
    width: 70px;
    height: 70px;
    background: radial-gradient(circle at 40% 40%, #5a5d64, #2a2d34);
    border-radius: 50%;
    box-shadow: 0 10px 20px rgba(0,0,0,0.6), inset 0 2px 5px rgba(255,255,255,0.3);
    position: absolute;
    transition: transform 0.05s linear;
}
.analog-thumb::after {
    content: '';
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    width: 80%; height: 80%;
    border-radius: 50%;
    border: 2px solid rgba(0,0,0,0.2);
    background: repeating-radial-gradient(circle, transparent, transparent 4px, rgba(0,0,0,0.1) 4px, rgba(0,0,0,0.1) 5px);
}
.analog-thumb:active {
    background: radial-gradient(circle at 40% 40%, #4a4d54, #1a1d24);
    box-shadow: 0 5px 10px rgba(0,0,0,0.8), inset 0 2px 5px rgba(255,255,255,0.1);
}

/* Edit Mode Layout */
.edit-overlay {
    position: absolute;
    top: 12vh;
    left: 0;
    width: 100vw;
    height: 88vh;
    background: transparent; /* Removed blur and dark background to see controls clearly */
    z-index: 100;
    display: flex;
    flex-direction: column;
    align-items: center;
    pointer-events: none;
}

.edit-controls {
    display: flex;
    gap: 1rem;
    background: rgba(20, 20, 25, 0.95);
    padding: 0.8rem 1.5rem;
    border-radius: 0 0 20px 20px;
    border: 1px solid var(--secondary-color);
    border-top: none;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    pointer-events: auto;
    box-shadow: 0 10px 30px rgba(0,0,0,0.8);
    max-width: 90%;
}

.edit-controls .btn {
    width: auto;
    display: inline-block;
    padding: 0.5rem 1rem;
    margin: 0;
}

.slider-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    font-size: 0.9rem;
    color: var(--secondary-color);
    font-weight: 600;
}
.slider-group input[type=range] {
    accent-color: var(--primary-color);
}

.edit-hint {
    margin-top: auto;
    margin-bottom: 3rem;
    font-size: 1.2rem;
    font-weight: 800;
    letter-spacing: 2px;
    color: var(--secondary-color);
    text-shadow: 0 0 10px var(--secondary-color);
    pointer-events: none;
    animation: pulse 1.5s infinite;
}

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

.draggable {
    transition: transform 0.1s ease;
}

.edit-mode .draggable {
    border: 2px dashed var(--secondary-color);
    background: rgba(0, 240, 255, 0.05);
    border-radius: 20px;
    box-shadow: 0 0 20px rgba(0, 240, 255, 0.3);
}

.edit-mode .action-btn, .edit-mode .dpad-btn {
    pointer-events: none;
}

/* Responsive constraints for smaller phones */
@media (max-width: 600px) {
    .dpad { width: 130px; height: 130px; }
    .dpad-btn { width: 44px; height: 44px; }
    .dpad-btn.up { left: 43px; }
    .dpad-btn.right { top: 43px; }
    .dpad-btn.down { left: 43px; }
    .dpad-btn.left { top: 43px; }
    .dpad-center { top: 43px; left: 43px; width: 44px; height: 44px; }
    
    .action-buttons-snes { width: 140px; height: 140px; }
    .round-btn { width: 48px; height: 48px; font-size: 1.2rem; }
    .btn-x { left: 46px; }
    .btn-y { top: 46px; }
    .btn-a { top: 46px; }
    .btn-b { left: 46px; }
    
    .player-badge { font-size: 0.6rem; padding: 4px 8px; }
    .room-indicator { font-size: 0.7rem; }
    #currentRoomCode { font-size: 0.8rem; }
    select#playerRomSelect { max-width: 100px; font-size: 0.7rem; }
}
