/* Mobile Controls & Layout Revised */

/* Only show on touch devices */
.mobile-controls {
    display: none;
    pointer-events: none;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
}

@media (pointer: coarse) {

    /* Global Mobile Adjustments */
    .player-bar {
        display: none !important;
    }

    #loader {
        z-index: 200;
    }

    /* --- PORTRAIT MODE: GameBoy Style --- */
    @media (orientation: portrait) {
        #screen {
            position: fixed !important;
            top: 0;
            left: 0;
            width: 100% !important;
            height: 48vh !important;
            background: #000;
            z-index: 50;
            padding: 0;
            border-bottom: 2px solid #333;
        }

        #screen canvas {
            width: 100% !important;
            height: 100% !important;
            object-fit: contain !important;
        }

        .mobile-controls {
            display: block;
            position: fixed;
            top: 48vh;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(180deg, #1a1a1a 0%, #0d0d0d 100%);
            z-index: 100;
            overflow: hidden;
            box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.5);
            padding-bottom: env(safe-area-inset-bottom);
            padding-left: env(safe-area-inset-left);
            padding-right: env(safe-area-inset-right);
        }

        .controls-row {
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: absolute;
            top: 45%;
            transform: translateY(-50%);
            width: 100%;
            padding: 0 10px;
            pointer-events: none;
        }

        .dpad-container {
            position: absolute;
            left: 10px;
            top: 50%;
            transform: translateY(-50%);
            width: 130px;
            height: 130px;
            pointer-events: auto;
        }

        .action-buttons {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            display: flex;
            flex-direction: column;
            gap: 12px;
            pointer-events: auto;
            align-items: flex-end;
        }

        .action-row {
            display: flex;
            gap: 12px;
            justify-content: flex-end;
        }

        .action-btn {
            width: 52px;
            height: 52px;
            font-size: 1.2rem;
        }



        .start-select-container {
            position: absolute;
            bottom: 85px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
            justify-content: center;
            width: 100%;
            pointer-events: none;
            /* Changed from auto */
            z-index: 110;
        }

        .system-controls-container {
            position: absolute;
            bottom: 30px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
            justify-content: center;
            width: 100%;
            pointer-events: none;
            /* Changed from auto */
            z-index: 110;
            padding: 0 10px;
        }

        .start-select-container .ctrl-btn,
        .system-controls-container .ctrl-btn {
            pointer-events: auto;
        }

        .game-controls-row {
            display: flex;
            gap: 15px;
            justify-content: center;
        }

        .game-controls-row .ctrl-btn {
            font-size: 11px;
            padding: 12px 20px;
            min-width: 70px;
        }

        .system-controls-row {
            display: flex;
            gap: 10px;
            justify-content: center;
            flex-wrap: wrap;
        }

        .system-controls-row .ctrl-btn {
            font-size: 0;
            padding: 10px;
            min-width: 44px;
        }
    }

    /* --- LANDSCAPE MODE: Handheld Console Style --- */
    @media (orientation: landscape) {
        #screen {
            position: fixed !important;
            inset: 0;
            width: 100% !important;
            height: 100% !important;
            background: #000;
            z-index: 10;
        }

        #screen canvas {
            width: 100% !important;
            height: 100% !important;
            object-fit: contain !important;
            padding: 0;
        }

        .mobile-controls {
            display: block;
            pointer-events: none;
        }

        .dpad-container {
            position: absolute;
            bottom: 20px;
            left: 20px;
            width: 160px;
            height: 160px;
            pointer-events: auto;
            z-index: 100;
            bottom: calc(20px + env(safe-area-inset-bottom));
            left: calc(20px + env(safe-area-inset-left));
        }

        .action-buttons {
            position: absolute;
            bottom: 20px;
            right: 20px;
            pointer-events: auto;
            gap: 16px;
            z-index: 100;
            bottom: calc(20px + env(safe-area-inset-bottom));
            right: calc(20px + env(safe-area-inset-right));
        }

        .action-buttons .action-btn {
            width: 56px;
            height: 56px;
        }

        .system-controls-container {
            position: absolute;
            top: 20px;
            right: 20px;
            display: flex;
            flex-direction: column;
            pointer-events: none;
            z-index: 100;
            opacity: 0.7;
            transition: opacity 0.2s;
            align-items: flex-end;
        }

        .start-select-container {
            position: absolute;
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
            display: flex;
            justify-content: center;
            pointer-events: none;
            z-index: 100;
            opacity: 0.8;
        }

        .system-controls-container .ctrl-btn,
        .start-select-container .ctrl-btn {
            pointer-events: auto;
        }

        .system-controls-container:active,
        .system-controls-container:hover {
            opacity: 1;
        }

        .game-controls-row {
            display: flex;
            gap: 8px;
            flex-direction: row;
            margin-bottom: 4px;
        }

        .game-controls-row .ctrl-btn {
            font-size: 9px;
            padding: 8px 14px;
            min-width: 60px;
        }

        .system-controls-row {
            display: flex;
            gap: 6px;
            flex-wrap: wrap;
            justify-content: flex-end;
        }

        .system-controls-row .ctrl-btn {
            font-size: 0;
            padding: 8px;
            min-width: 36px;
            width: 36px;
            height: 36px;
        }

        .game-controls-row .ctrl-btn {
            background: rgba(40, 40, 40, 0.6);
            backdrop-filter: blur(4px);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 20px;
            /* Pill shape */
        }

        .system-controls-container .ctrl-btn {
            background: rgba(30, 30, 30, 0.9);
            backdrop-filter: blur(8px);
            border: 1px solid rgba(255, 255, 255, 0.15);
        }

        @media (max-height: 500px) {
            .system-controls-container {
                top: 10px;
                right: 10px;
            }

            .start-select-container {
                bottom: 5px;
            }

            .game-controls-row {
                gap: 6px;
            }

            .game-controls-row .ctrl-btn {
                font-size: 8px;
                padding: 6px 10px;
                min-width: 50px;
            }

            .system-controls-row .ctrl-btn {
                padding: 6px;
                min-width: 32px;
                width: 32px;
                height: 32px;
            }

            .system-controls-row .ctrl-btn svg {
                width: 16px;
                height: 16px;
            }

            .dpad-container {
                width: 140px;
                height: 140px;
                left: 10px;
                bottom: 10px;
            }

            .action-buttons {
                right: 10px;
                bottom: 10px;
            }

            .action-buttons .action-btn {
                width: 50px;
                height: 50px;
            }
        }

        @media (min-width: 800px) and (orientation: landscape) {
            .system-controls-container {
                top: 30px;
                right: 30px;
            }

            .start-select-container {
                bottom: 20px;
                transform: translateX(-50%) scale(1.2);
                /* Slightly larger on big screens */
            }

            .dpad-container {
                left: 30px;
                bottom: 30px;
            }

            .action-buttons {
                right: 30px;
                bottom: 30px;
            }
        }

        @media (max-width: 700px) and (orientation: landscape) {
            .system-controls-container {
                top: 15px;
                right: 15px;
            }

            .game-controls-row .ctrl-btn {
                font-size: 8px;
                padding: 6px 12px;
                min-width: 55px;
            }

            .system-controls-row .ctrl-btn {
                padding: 7px;
                min-width: 34px;
                width: 34px;
                height: 34px;
            }

            .system-controls-row .ctrl-btn svg {
                width: 16px;
                height: 16px;
            }
        }
    }
}

/* --- COMMON CONTROLS STYLING (Mega Drive Aesthetic) --- */

.dpad-container {
    background: rgba(40, 40, 40, 0.4);
    border-radius: 50%;
    backdrop-filter: blur(2px);
}

.dpad-btn {
    position: absolute;
    background: #151515;
    border: 1px solid #333;
    box-sizing: border-box;
    cursor: pointer;
    overflow: hidden;
}

.dpad-btn:active,
.dpad-btn.active {
    background: #333;
    box-shadow: inset 0 0 10px #000;
    border-color: #555;
}

.dpad-left,
.dpad-right {
    top: 33.33%;
    height: 33.33%;
    width: 33.33%;
}

.dpad-up,
.dpad-down {
    left: 33.33%;
    width: 33.33%;
    height: 33.33%;
}

.dpad-up {
    top: 0;
    border-radius: 6px 6px 0 0;
}

.dpad-down {
    bottom: 0;
    border-radius: 0 0 6px 6px;
}

.dpad-left {
    left: 0;
    border-radius: 6px 0 0 6px;
}

.dpad-right {
    right: 0;
    border-radius: 0 6px 6px 0;
}

.dpad-center {
    position: absolute;
    top: 33.33%;
    left: 33.33%;
    width: 33.33%;
    height: 33.33%;
    background: #111;
    border-radius: 50%;
    transform: scale(0.6);
}

.dpad-up::after {
    content: '▲';
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #555;
    font-size: 14px;
}

.dpad-down::after {
    content: '▼';
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #555;
    font-size: 14px;
}

.dpad-left::after {
    content: '◀';
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #555;
    font-size: 14px;
}

.dpad-right::after {
    content: '▶';
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #555;
    font-size: 14px;
}

.action-buttons {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.action-row {
    display: flex;
    gap: 16px;
    justify-content: center;
}

.action-btn {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: #1a1a1a;
    border: 2px solid #444;
    color: #ddd;
    font-weight: 700;
    font-size: 1.4rem;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 5px 0 #000;
    transition: transform 0.05s, box-shadow 0.05s;
}

.action-btn:active,
.action-btn.active {
    transform: translateY(5px);
    box-shadow: 0 0 0 #000;
    background: #444;
    color: #fff;
    border-color: #888;
}

.btn-a {
    color: rgba(255, 255, 255, 0.9);
    background: radial-gradient(circle at 30% 30%, #3b82f6, #1d4ed8);
    border-color: #1e40af;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.btn-b {
    color: rgba(255, 255, 255, 0.9);
    background: radial-gradient(circle at 30% 30%, #ef4444, #b91c1c);
    border-color: #991b1b;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.btn-c {
    color: rgba(255, 255, 255, 0.9);
    background: radial-gradient(circle at 30% 30%, #22c55e, #15803d);
    border-color: #166534;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}



.ctrl-btn {
    background: #333;
    border: none;
    color: #ccc;
    padding: 12px 18px;
    border-radius: 20px;
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 1px;
    text-transform: uppercase;
    box-shadow: 0 3px 0 #000;
    transition: all 0.05s;
    min-width: 60px;
}

.start-btn {
    background: #444;
    color: #fff;
}

.menu-btn,
.back-btn {
    background: #882222;
    color: #fff;
}

.back-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px !important;
    min-width: 44px;
    border: 2px solid rgba(255, 255, 255, 0.3) !important;
    box-shadow: 0 2px 8px rgba(136, 34, 34, 0.4) !important;
}

.back-btn svg {
    width: 18px;
    height: 18px;
}

.back-btn:active,
.back-btn.active {
    background: rgba(136, 34, 34, 0.95) !important;
    border-color: rgba(255, 255, 255, 0.5) !important;
    box-shadow: 0 0 0 rgba(136, 34, 34, 0.4) !important;
}

.ctrl-btn:active,
.ctrl-btn.active {
    transform: translateY(3px);
    box-shadow: 0 0 0 #000;
    filter: brightness(1.3);
}

.save-btn,
.load-btn,
.full-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px !important;
    min-width: 44px;
    background: rgba(40, 40, 40, 0.9) !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    color: #ddd !important;
    font-size: 0 !important;
    line-height: 0;
}

.save-btn svg,
.load-btn svg,
.full-btn svg {
    width: 18px;
    height: 18px;
    stroke-width: 2.5;
    flex-shrink: 0;
}

.save-btn:active,
.load-btn:active,
.full-btn:active,
.back-btn:active,
.save-btn.active,
.load-btn.active,
.full-btn.active,
.back-btn.active {
    background: rgba(60, 60, 60, 0.95) !important;
    transform: translateY(2px);
    box-shadow: 0 0 0 #000 !important;
    filter: brightness(1.2);
}

.save-btn.active {
    background: rgba(50, 100, 50, 0.9) !important;
}

.load-btn.active {
    background: rgba(50, 50, 100, 0.9) !important;
}

.back-btn:active,
.back-btn.active {
    background: rgba(136, 34, 34, 0.95) !important;
}

.full-btn:active,
.full-btn.active {
    background: rgba(60, 60, 60, 0.95) !important;
}

@media (pointer: coarse) {
    @media (orientation: portrait) {

        .save-btn,
        .load-btn,
        .full-btn,
        .back-btn {
            min-width: 50px;
            padding: 12px !important;
        }

        .save-btn svg,
        .load-btn svg,
        .full-btn svg,
        .back-btn svg {
            width: 20px;
            height: 20px;
        }
    }
}

body.is-fullscreen .mobile-controls {
    transition: opacity 0.3s ease, transform 0.3s ease;
}

body.is-fullscreen .mobile-controls.fullscreen-mode {
    opacity: 0.3;
}

body.is-fullscreen .mobile-controls.fullscreen-mode:active {
    opacity: 1;
}

@media (pointer: coarse) {
    @media (orientation: landscape) {
        body.is-fullscreen .mobile-controls.fullscreen-mode .system-controls-container {
            opacity: 0.7;
        }

        body.is-fullscreen .mobile-controls.fullscreen-mode .system-controls-container:active,
        body.is-fullscreen .mobile-controls.fullscreen-mode .system-controls-container:hover {
            opacity: 1;
        }

        body.is-fullscreen .mobile-controls.fullscreen-mode .dpad-container,
        body.is-fullscreen .mobile-controls.fullscreen-mode .action-buttons,
        body.is-fullscreen .mobile-controls.fullscreen-mode .start-select-container {
            opacity: 0.8;
            transition: opacity 0.3s ease;
        }

        body.is-fullscreen .mobile-controls.fullscreen-mode .dpad-container:active,
        body.is-fullscreen .mobile-controls.fullscreen-mode .action-buttons:active,
        body.is-fullscreen .mobile-controls.fullscreen-mode .start-select-container:active,
        body.is-fullscreen .mobile-controls.fullscreen-mode:active .dpad-container,
        body.is-fullscreen .mobile-controls.fullscreen-mode:active .action-buttons,
        body.is-fullscreen .mobile-controls.fullscreen-mode:active .start-select-container {
            opacity: 1;
        }
    }

    @media (orientation: portrait) {
        body.is-fullscreen .mobile-controls.fullscreen-mode {
            opacity: 0.7;
        }

        body.is-fullscreen .mobile-controls.fullscreen-mode:active {
            opacity: 1;
        }
    }
}