/* ====== ARCADE STICK MOBILE CONTROLLER (shared) ======
   Used by:
     - emulator.html (host + player flows)
     - neogeo-arcade.html (host + player flows)

   Hidden by default; visible on touch devices and small viewports. */

.arcade-pad {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 5000;
    background: linear-gradient(180deg, #1a1a2e 0%, #0d0d1a 50%, #0a0a14 100%);
    border-top: 3px solid #2a2a40;
    box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.04), 0 -4px 20px rgba(0, 0, 0, 0.5);
    padding: 6px 8px;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
}

.arcade-pad::before {
    content: '';
    position: absolute;
    inset: 0;
    background:
        radial-gradient(circle at 20% 50%, rgba(233, 69, 96, 0.03) 0%, transparent 50%),
        radial-gradient(circle at 80% 50%, rgba(99, 102, 241, 0.03) 0%, transparent 50%);
    pointer-events: none;
}

@media (pointer: coarse), (max-width: 900px) {
    .arcade-pad { display: block; }

    /* emulator.html uses the arcade-player shell for every console.
       Only real arcade-shaped systems get the arcade stick; SNES/GBA/etc.
       keep the per-console #mobileControls gamepad. */
    body.emulator-page:not(.og-arcade-only) .arcade-pad {
        display: none !important;
        visibility: hidden !important;
        pointer-events: none !important;
    }

    body.emulator-page.og-arcade-only .arcade-pad {
        display: block !important;
        visibility: visible !important;
        pointer-events: auto !important;
    }
}
@media (hover: hover) and (pointer: fine) and (min-width: 901px) {
    .arcade-pad { display: none !important; }
}

.arcade-pad .arcade-surface {
    display: flex;
    align-items: center;
    justify-content: space-between;
    max-width: 600px;
    margin: 0 auto;
    position: relative;
    height: 130px;
    gap: 12px;
    padding: 0 6px;
}

/* === Joystick === */
.arcade-stick-zone {
    position: relative;
    width: 130px;
    height: 130px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    pointer-events: auto;
}
.arcade-stick-base {
    position: relative;
    width: 110px;
    height: 110px;
    border-radius: 50%;
    background: radial-gradient(circle, rgba(34, 33, 56, 0.95) 0%, rgba(20, 18, 36, 0.95) 60%, rgba(10, 8, 22, 0.98) 100%);
    border: 2px solid rgba(233, 69, 96, 0.32);
    box-shadow: inset 0 2px 14px rgba(0, 0, 0, 0.6), 0 4px 12px rgba(0, 0, 0, 0.45);
}
.arcade-stick-gate {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 80px;
    height: 80px;
    border-radius: 50%;
    border: 1px dashed rgba(255, 255, 255, 0.1);
}
.arcade-stick-knob {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 30%, #ff486e, #b81d3a 70%, #6a0e22 100%);
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.55), 0 0 0 2px rgba(255, 255, 255, 0.04);
    transition: transform 0.05s linear;
    pointer-events: none;
}
.arcade-stick-knob.active {
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.55), 0 0 18px rgba(233, 69, 96, 0.55);
}

/* === Coin / Start meta column === */
.arcade-meta {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-self: center;
    min-width: 90px;
    pointer-events: auto;
}
.arcade-meta-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 7px 12px;
    background: linear-gradient(160deg, rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0.02));
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 10px;
    color: #fff;
    font-family: 'Russo One', sans-serif;
    font-size: 0.72rem;
    letter-spacing: 0.5px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
.arcade-meta-btn:active,
.arcade-meta-btn.active { background: rgba(233, 69, 96, 0.22); }
.arcade-meta-btn .meta-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    box-shadow: 0 0 8px currentColor;
}
.arcade-meta-btn .coin-dot { background: #fbbf24; color: #fbbf24; }
.arcade-meta-btn .start-dot { background: #22c55e; color: #22c55e; }

/* === 4 action buttons === */
.arcade-buttons {
    display: grid;
    grid-template-columns: repeat(2, 56px);
    grid-template-rows: repeat(2, 56px);
    gap: 8px;
    align-content: center;
    flex-shrink: 0;
    pointer-events: auto;
}
.arcade-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.12);
    color: #fff;
    font-family: 'Russo One', sans-serif;
    font-size: 1rem;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.05s, box-shadow 0.05s;
}
.arcade-btn:active,
.arcade-btn.active {
    transform: scale(0.92);
    box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.35);
}
.arcade-btn.btn-a { background: radial-gradient(circle at 35% 30%, #5dd5ff, #1d8dc7); }
.arcade-btn.btn-b { background: radial-gradient(circle at 35% 30%, #ffd66e, #c98a17); }
.arcade-btn.btn-c { background: radial-gradient(circle at 35% 30%, #6affb1, #178c4c); }
.arcade-btn.btn-d { background: radial-gradient(circle at 35% 30%, #ff7a90, #b8243f); }

/* === Compact phones === */
@media (max-width: 480px) {
    .arcade-pad { padding: 8px 8px max(8px, env(safe-area-inset-bottom)); }
    .arcade-pad .arcade-surface { height: 116px; gap: 6px; }
    .arcade-stick-zone { width: 110px; height: 110px; }
    .arcade-stick-base { width: 96px; height: 96px; }
    .arcade-stick-gate { width: 66px; height: 66px; }
    .arcade-stick-knob { width: 44px; height: 44px; }
    .arcade-buttons { grid-template-columns: repeat(2, 48px); grid-template-rows: repeat(2, 48px); gap: 6px; }
    .arcade-btn { font-size: 0.92rem; }
    .arcade-meta { min-width: 78px; gap: 6px; }
    .arcade-meta-btn { font-size: 0.66rem; padding: 5px 8px; }
}

/* Reserve space at bottom of game viewport so the pad never covers the canvas/video */
body.arcade-player-ui #game-container,
body.mp-host-mode #mp-host-emulator-slot,
body .mp-game-container .mp-main-content,
body.arcade-player-ui .game-main {
    padding-bottom: 152px;
}
@media (max-width: 480px) {
    body.arcade-player-ui #game-container,
    body.mp-host-mode #mp-host-emulator-slot,
    body .mp-game-container .mp-main-content,
    body.arcade-player-ui .game-main {
        padding-bottom: 132px;
    }
}
@media (hover: hover) and (pointer: fine) and (min-width: 901px) {
    body.arcade-player-ui #game-container,
    body.mp-host-mode #mp-host-emulator-slot,
    body .mp-game-container .mp-main-content,
    body.arcade-player-ui .game-main {
        padding-bottom: 0;
    }
}


/* ════════════════════════════════════════════════════════════════
 * ORIENTATION-AWARE LAYOUT (atomic via [data-orientation])
 * ════════════════════════════════════════════════════════════════
 * arcade-orientation.js sets <body data-orientation="portrait|landscape">
 * synchronously on every rotation so we can switch layouts atomically.
 * This avoids the brittle @media rule timing where the old portrait pad
 * and the new landscape pad could overlap mid-rotation.
 */

/* ── Portrait (default — pad along the bottom) ───────────────── */
body[data-orientation="portrait"] .arcade-pad {
    background: linear-gradient(180deg, #1a1a2e 0%, #0d0d1a 50%, #0a0a14 100%);
    border-top: 3px solid #2a2a40;
    box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.04), 0 -4px 20px rgba(0, 0, 0, 0.5);
}

/* ── Landscape on phones (overlay layout) ────────────────────── */
@media (max-height: 500px) {
    body[data-orientation="landscape"] .arcade-pad {
        background: transparent;
        border-top: none;
        box-shadow: none;
        padding: 0;
        height: 100vh;
        height: 100dvh;
        pointer-events: none;
    }
    body[data-orientation="landscape"] .arcade-pad::before { display: none; }
    body[data-orientation="landscape"] .arcade-pad .arcade-surface {
        height: 100%;
        max-width: none;
        width: 100%;
        padding: 0 env(safe-area-inset-right, 8px) 0 env(safe-area-inset-left, 8px);
        box-sizing: border-box;
        pointer-events: none;
    }
    /* Joystick on the left edge with translucent base */
    body[data-orientation="landscape"] .arcade-pad .arcade-stick-zone {
        pointer-events: auto;
        position: absolute;
        left: max(20px, env(safe-area-inset-left));
        bottom: 16px;
        width: 130px;
        height: 130px;
        opacity: 0.7;
    }
    body[data-orientation="landscape"] .arcade-pad .arcade-stick-zone:active,
    body[data-orientation="landscape"] .arcade-pad .arcade-stick-zone.touching {
        opacity: 1;
    }
    body[data-orientation="landscape"] .arcade-pad .arcade-stick-base {
        background: radial-gradient(circle, rgba(30,30,48,0.85) 0%, rgba(20,20,36,0.92) 60%, rgba(14,14,28,0.96) 100%);
    }
    /* Buttons cluster on the right edge */
    body[data-orientation="landscape"] .arcade-pad .arcade-buttons {
        pointer-events: auto;
        position: absolute;
        right: max(16px, env(safe-area-inset-right));
        bottom: 16px;
        opacity: 0.78;
    }
    body[data-orientation="landscape"] .arcade-pad .arcade-buttons:active,
    body[data-orientation="landscape"] .arcade-pad .arcade-buttons.touching {
        opacity: 1;
    }
    /* Coin/Start small at top center */
    body[data-orientation="landscape"] .arcade-pad .arcade-meta {
        pointer-events: auto;
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        flex-direction: row;
        gap: 12px;
        opacity: 0.55;
    }
    body[data-orientation="landscape"] .arcade-pad .arcade-meta:active,
    body[data-orientation="landscape"] .arcade-pad .arcade-meta.touching {
        opacity: 0.95;
    }
    body[data-orientation="landscape"] .arcade-pad .arcade-meta-btn {
        padding: 4px 12px;
        font-size: 0.62rem;
        background: rgba(0,0,0,0.55);
        border-color: rgba(255,255,255,0.14);
    }
    /* In landscape we need the canvas to use the full screen */
    body[data-orientation="landscape"].arcade-player-ui #game-container,
    body[data-orientation="landscape"].mp-host-mode #mp-host-emulator-slot,
    body[data-orientation="landscape"].arcade-player-ui .game-main,
    body[data-orientation="landscape"] .game-wrapper {
        padding-bottom: 0 !important;
        max-height: 100dvh !important;
    }
}

/* ── Tablet landscape (taller screens) ──────────────────────── */
@media (min-height: 501px) and (max-height: 760px) {
    body[data-orientation="landscape"] .arcade-pad {
        padding: 6px 12px max(6px, env(safe-area-inset-bottom));
    }
    body[data-orientation="landscape"] .arcade-pad .arcade-surface {
        height: 110px;
        max-width: 720px;
    }
}

/* ── Hide all touch overlays on hover-capable wide screens ──── */
@media (hover: hover) and (pointer: fine) and (min-width: 901px) {
    body .arcade-pad,
    body .mobile-gamepad,
    body #mobileControls {
        display: none !important;
    }
}

/* ── Defensive: guarantee only one pad is shown when arcade UI is on ── */
body.arcade-player-ui.og-arcade-only .mobile-gamepad,
body.arcade-player-ui.og-arcade-only #mobileControls {
    display: none !important;
}


/* ════════════════════════════════════════════════════════════════
 *  v4 MOBILE GAMEPAD — Mobile + Landscape polish
 * ════════════════════════════════════════════════════════════════
 *  These rules sit in the SHARED stylesheet so emulator.html and
 *  neogeo-arcade.html benefit equally. They override the page-local
 *  rules that left buttons cropped or overlapping in landscape.
 */

/* ── Portrait, mobile: keep the v4 pad full-width, properly spaced ── */
@media (pointer: coarse), (max-width: 900px) {
    body[data-orientation="portrait"] .mobile-gamepad,
    body:not([data-orientation]) .mobile-gamepad {
        padding: 8px 12px;
        padding-bottom: max(10px, env(safe-area-inset-bottom));
        z-index: 6000;
    }
    body[data-orientation="portrait"] .mobile-gamepad .gpad-main,
    body:not([data-orientation]) .mobile-gamepad .gpad-main {
        gap: 6px;
        padding: 0 4px;
    }
    body[data-orientation="portrait"] .mobile-gamepad .gpad-dpad,
    body[data-orientation="portrait"] .mobile-gamepad .gpad-abxy,
    body:not([data-orientation]) .mobile-gamepad .gpad-dpad,
    body:not([data-orientation]) .mobile-gamepad .gpad-abxy {
        width: 132px;
        height: 132px;
    }
    /* On really narrow phones, shrink the diamond pad so nothing clips */
}
@media (pointer: coarse) and (max-width: 360px) {
    .mobile-gamepad .gpad-dpad,
    .mobile-gamepad .gpad-abxy { width: 116px !important; height: 116px !important; }
    .mobile-gamepad .gpad-dir,
    .mobile-gamepad .gpad-action { width: 40px !important; height: 40px !important; font-size: 13px !important; }
    .mobile-gamepad .gpad-shoulder { padding: 5px 14px !important; font-size: 11px !important; }
}

/* ── Landscape, phone-class screens: put d-pad LEFT and buttons RIGHT ── */
@media (max-height: 500px) and (orientation: landscape),
       (max-width: 950px) and (orientation: landscape) {
    body[data-orientation="landscape"] .mobile-gamepad {
        position: fixed;
        inset: 0;
        background: transparent !important;
        border-top: 0 !important;
        padding: 0 !important;
        pointer-events: none;
        z-index: 6000;
    }
    body[data-orientation="landscape"] .mobile-gamepad .gpad-shoulders {
        position: absolute;
        top: max(6px, env(safe-area-inset-top, 6px));
        left: max(6px, env(safe-area-inset-left));
        right: max(6px, env(safe-area-inset-right));
        margin: 0;
        pointer-events: auto;
        opacity: 0.85;
    }
    body[data-orientation="landscape"] .mobile-gamepad .gpad-shoulder {
        background: rgba(0,0,0,0.55);
        border-color: rgba(255,255,255,0.18);
        padding: 6px 18px;
    }
    body[data-orientation="landscape"] .mobile-gamepad .gpad-main {
        position: absolute;
        bottom: max(8px, env(safe-area-inset-bottom));
        left: 0;
        right: 0;
        display: block;     /* override flex so we can absolute-position */
        padding: 0;
        pointer-events: none;
    }
    /* D-pad LEFT */
    body[data-orientation="landscape"] .mobile-gamepad .gpad-dpad {
        position: absolute;
        left: max(14px, env(safe-area-inset-left));
        bottom: 8px;
        pointer-events: auto;
        opacity: 0.78;
        width: 130px !important;
        height: 130px !important;
    }
    body[data-orientation="landscape"] .mobile-gamepad .gpad-dpad:active,
    body[data-orientation="landscape"] .mobile-gamepad .gpad-dpad.touching {
        opacity: 1;
    }
    body[data-orientation="landscape"] .mobile-gamepad .gpad-dpad .gpad-dir {
        background: rgba(0,0,0,0.55);
        border-color: rgba(255,255,255,0.22);
        backdrop-filter: blur(2px);
        -webkit-backdrop-filter: blur(2px);
    }
    /* Action buttons RIGHT */
    body[data-orientation="landscape"] .mobile-gamepad .gpad-abxy {
        position: absolute;
        right: max(14px, env(safe-area-inset-right));
        bottom: 8px;
        pointer-events: auto;
        opacity: 0.78;
        width: 130px !important;
        height: 130px !important;
    }
    body[data-orientation="landscape"] .mobile-gamepad .gpad-abxy:active,
    body[data-orientation="landscape"] .mobile-gamepad .gpad-abxy.touching {
        opacity: 1;
    }
    /* SELECT/START small at top center, low opacity */
    body[data-orientation="landscape"] .mobile-gamepad .gpad-menu {
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        flex-direction: row;
        gap: 8px;
        pointer-events: auto;
        opacity: 0.55;
    }
    body[data-orientation="landscape"] .mobile-gamepad .gpad-menu:active,
    body[data-orientation="landscape"] .mobile-gamepad .gpad-menu.touching {
        opacity: 1;
    }
    body[data-orientation="landscape"] .mobile-gamepad .gpad-menu-btn {
        background: rgba(0,0,0,0.55);
        border-color: rgba(255,255,255,0.18);
        padding: 4px 12px;
        font-size: 9px;
    }

    /* In landscape we want the canvas to fill the whole screen */
    body[data-orientation="landscape"] #game-display-area,
    body[data-orientation="landscape"] #game,
    body[data-orientation="landscape"] #game-container,
    body[data-orientation="landscape"] .game-wrapper,
    body[data-orientation="landscape"] #emulator-wrapper,
    body[data-orientation="landscape"] #canvas {
        max-height: 100dvh !important;
        height: 100dvh !important;
        padding-bottom: 0 !important;
    }
}

/* ── For the per-console v4 gamepad on neogeo/arcade in portrait,
       guarantee the 6-button cluster fits entirely on screen ── */
@media (pointer: coarse), (max-width: 900px) {
    .mobile-gamepad[data-console="neogeo"] .gpad-abxy,
    .mobile-gamepad[data-console="arcade"] .gpad-abxy,
    .mobile-gamepad[data-console="genesis"] .gpad-abxy {
        width: 152px !important;
        height: 100px !important;
    }
    .mobile-gamepad[data-console="neogeo"] .gpad-action,
    .mobile-gamepad[data-console="arcade"] .gpad-action,
    .mobile-gamepad[data-console="genesis"] .gpad-action {
        width: 44px !important;
        height: 44px !important;
        font-size: 13px !important;
    }
}
