﻿* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    background: #1a1a2e;
    color: #e0e0e0;
    font-family: "Segoe UI", system-ui, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    min-height: 100svh;
    overflow: hidden;
    touch-action: none;
    overscroll-behavior: none;
    -webkit-user-select: none;
    user-select: none;
}

body.pseudo-fullscreen {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    min-height: 100dvh;
    overflow: hidden;
}

#emulator-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    max-width: 1180px;
    width: 100%;
    padding: 16px;
}

#emulator-container:fullscreen,
#emulator-container:-webkit-full-screen,
body.pseudo-fullscreen #emulator-container {
    max-width: none;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    min-height: 100dvh;
    padding:
        max(8px, env(safe-area-inset-top))
        max(10px, env(safe-area-inset-right))
        max(8px, env(safe-area-inset-bottom))
        max(10px, env(safe-area-inset-left));
    gap: 4px;
    background: #1a1a2e;
}

#play-layout {
    display: grid;
    grid-template-columns: minmax(150px, 0.8fr) minmax(320px, 2.4fr) minmax(170px, 0.9fr);
    align-items: center;
    gap: 16px;
    width: 100%;
}

#emulator-container:fullscreen #play-layout,
#emulator-container:-webkit-full-screen #play-layout,
body.pseudo-fullscreen #play-layout {
    flex: 1;
    min-height: 0;
    align-items: center;
}

#center-panel {
    min-width: 0;
    display: flex;
    justify-content: center;
}

#emulator-container:fullscreen #center-panel,
#emulator-container:-webkit-full-screen #center-panel,
body.pseudo-fullscreen #center-panel {
    padding-top: 0;
    align-items: center;
}

#screen-wrapper {
    position: relative;
    background: #0f0f23;
    border: 2px solid #333;
    border-radius: 4px;
    overflow: hidden;
    width: 100%;
    /* Crop the NES overscan area where many games leave garbage tiles. */
    aspect-ratio: 240 / 216;
    max-height: 65vh;
}

#emulator-container:fullscreen #screen-wrapper,
#emulator-container:-webkit-full-screen #screen-wrapper,
body.pseudo-fullscreen #screen-wrapper {
    max-height: calc(100dvh - 34px);
}

.touch-zone {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    touch-action: none;
}

#touch-right {
    flex-direction: column;
    gap: 18px;
}

#drop-zone {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 10;
    background: rgba(15, 15, 35, 0.85);
    transition: background 0.2s;
}

#drop-zone.drag-over { background: rgba(233, 69, 96, 0.15); }

.drop-text {
    font-size: 14px;
    color: #888;
    text-align: center;
    pointer-events: none;
}

canvas#nes-screen {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

/* ─── Controller Panel ────────────────────────────── */

#ctrl-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    background: #12122a;
    border: 1px solid #2a2a4a;
    border-radius: 6px;
    padding: 10px 16px;
    width: 100%;
    max-width: 340px;
}

.ctrl-label {
    font-size: 10px;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.ctrl-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
    width: 100%;
    max-width: 240px;
}

.ctrl-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    aspect-ratio: 1;
    background: #1a1a3a;
    border: 1px solid #333;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 600;
    color: #555;
    cursor: default;
    transition: all 0.08s;
    user-select: none;
    min-width: 32px;
}

.ctrl-btn.active {
    background: #e94560;
    border-color: #f06080;
    color: #fff;
    box-shadow: 0 0 8px rgba(233, 69, 96, 0.4);
}

.ctrl-hint {
    font-size: 9px;
    color: #444;
    text-align: center;
    max-width: 280px;
    line-height: 1.4;
}

/* ─── Controls, Buttons, Info ─────────────────────── */

#controls {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: center;
}

button {
    background: #16213e;
    color: #ccc;
    border: 1px solid #333;
    padding: 6px 14px;
    border-radius: 3px;
    cursor: pointer;
    font-size: 12px;
    transition: background 0.15s, color 0.15s;
}

button:hover { background: #1a1a4e; color: #fff; }
button:active { background: #e94560; }

.touch-dpad {
    display: grid;
    grid-template-columns: repeat(3, 68px);
    grid-template-rows: repeat(3, 68px);
    justify-content: start;
}

.touch-dpad .touch-btn {
    width: 68px;
    height: 68px;
}

.touch-up { grid-column: 2; grid-row: 1; }
.touch-left { grid-column: 1; grid-row: 2; }
.touch-right { grid-column: 3; grid-row: 2; }
.touch-down { grid-column: 2; grid-row: 3; }

.touch-system {
    display: flex;
    gap: 8px;
    align-items: center;
    justify-content: center;
}

.touch-actions {
    display: flex;
    gap: 14px;
    justify-content: end;
    align-items: center;
}

.touch-btn {
    min-width: 0;
    width: 54px;
    height: 54px;
    padding: 0;
    border-radius: 999px;
    border: 1px solid #3d416a;
    background: #17193d;
    color: #d7d9ef;
    font-weight: 700;
    font-size: 17px;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
}

.touch-small {
    width: 68px;
    height: 34px;
    border-radius: 999px;
    font-size: 10px;
}

.touch-action {
    width: 68px;
    height: 68px;
    background: #2a1730;
    border-color: #6f3353;
}

.touch-btn.active {
    background: #e94560;
    border-color: #ff7a96;
    color: #fff;
    transform: translateY(1px);
}

#info-bar {
    display: flex;
    justify-content: space-between;
    width: 100%;
    font-size: 11px;
    color: #666;
    padding: 0 4px;
}

#emulator-container:fullscreen #controls,
#emulator-container:-webkit-full-screen #controls,
body.pseudo-fullscreen #controls {
    position: fixed;
    top: env(safe-area-inset-top, 0);
    right: env(safe-area-inset-right, 0);
    z-index: 30;
    width: auto;
    max-width: none;
}

#emulator-container:fullscreen #debug-overlay,
#emulator-container:-webkit-full-screen #debug-overlay,
body.pseudo-fullscreen #debug-overlay {
    display: none !important;
}

#emulator-container:fullscreen #controls button:not(#btn-fullscreen),
#emulator-container:-webkit-full-screen #controls button:not(#btn-fullscreen),
body.pseudo-fullscreen #controls button:not(#btn-fullscreen),
#emulator-container:fullscreen #info-bar,
#emulator-container:-webkit-full-screen #info-bar,
body.pseudo-fullscreen #info-bar {
    display: none;
}

#emulator-container:fullscreen #btn-fullscreen,
#emulator-container:-webkit-full-screen #btn-fullscreen,
body.pseudo-fullscreen #btn-fullscreen {
    width: 68px;
    height: 68px;
    padding: 0;
    border-radius: 999px;
    font-size: 16px;
    font-weight: 700;
    opacity: 0.74;
}

#status { color: #888; }
#rom-info { color: #555; }
#fps { color: #4a9; font-variant-numeric: tabular-nums; }

input[type="file"] {
    position: absolute;
    left: -9999px;
    top: -9999px;
    width: 1px;
    height: 1px;
    opacity: 0;
}

@media (pointer: coarse), (max-width: 760px) {
    body {
        align-items: flex-start;
    }

    #emulator-container {
        display: grid;
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        grid-template-areas:
            "screen screen"
            "controls controls"
            "info info"
            "left right";
        grid-template-rows: auto auto auto minmax(0, 1fr);
        align-items: start;
        min-height: 100vh;
        min-height: 100dvh;
        height: 100vh;
        height: 100dvh;
        padding:
            max(8px, env(safe-area-inset-top))
            8px
            max(8px, env(safe-area-inset-bottom));
        gap: 8px 10px;
    }

    #screen-wrapper {
        max-height: 43vh;
        max-height: 43dvh;
    }

    #ctrl-panel,
    #debug-overlay {
        display: none !important;
    }

    #controls {
        grid-area: controls;
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 6px;
        width: 100%;
        margin: 0;
    }

    #controls button {
        min-height: 34px;
        padding: 5px 4px;
        font-size: 12px;
        white-space: nowrap;
    }

    #info-bar {
        grid-area: info;
        display: flex;
        align-items: center;
        gap: 6px;
        font-size: 10px;
        min-height: 14px;
        overflow: hidden;
        opacity: 0.82;
    }

    #status,
    #rom-info {
        min-width: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    #status {
        flex: 0 1 26%;
    }

    #rom-info {
        flex: 1 1 auto;
        text-align: center;
    }

    #fps {
        flex: 0 0 auto;
    }

    #play-layout {
        display: contents;
    }

    #center-panel {
        grid-area: screen;
        width: 100%;
    }

    #touch-left {
        grid-area: left;
        align-self: start;
        justify-content: flex-start;
        padding-top: 2px;
    }

    #touch-right {
        grid-area: right;
        align-self: start;
        justify-content: flex-start;
        align-items: center;
        gap: 24px;
        padding-top: 8px;
    }

    #touch-right .touch-actions {
        width: 100%;
        justify-content: center;
        gap: 22px;
    }

    #touch-right .touch-system {
        width: 100%;
        justify-content: center;
        gap: 16px;
    }
}

@media (pointer: coarse) and (orientation: landscape), (min-width: 900px) {
    body {
        align-items: flex-start;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    #emulator-container {
        display: flex;
        max-width: none;
        min-height: 100svh;
        height: auto;
        padding: 18px 14px 18px;
        gap: 5px;
    }

    #play-layout {
        display: grid;
        flex: 1;
        grid-template-columns: minmax(150px, 0.75fr) minmax(360px, 2.6fr) minmax(170px, 0.85fr);
        grid-template-areas: "left screen right";
        gap: 16px;
        align-items: start;
    }

    #center-panel {
        grid-area: screen;
        padding-top: 8px;
    }
    #touch-left { grid-area: left; }
    #touch-right {
        grid-area: right;
        padding-top: 44px;
    }

    #emulator-container:fullscreen #touch-right,
    #emulator-container:-webkit-full-screen #touch-right,
    body.pseudo-fullscreen #touch-right {
        padding-top: max(72px, calc(env(safe-area-inset-top, 0px) + 54px));
    }

    #emulator-container:fullscreen #controls,
    #emulator-container:-webkit-full-screen #controls,
    body.pseudo-fullscreen #controls {
        right: max(18px, calc(env(safe-area-inset-right, 0px) + 18px));
    }

    #screen-wrapper {
        width: min(100%, 72vw);
        max-height: 72vh;
        max-height: 72svh;
    }

    #controls {
        max-width: 720px;
    }

    #info-bar {
        max-width: 720px;
    }
}

@media (pointer: coarse) and (orientation: portrait) {
    #touch-right {
        gap: 26px;
    }

    #touch-right .touch-system {
        margin-top: 0;
    }
}

@media (pointer: coarse) and (max-height: 520px) {
    #screen-wrapper {
        max-height: 58vh;
    }

    .touch-dpad {
        grid-template-columns: repeat(3, 56px);
        grid-template-rows: repeat(3, 56px);
    }

    .touch-btn {
        width: 56px;
        height: 56px;
    }

    .touch-dpad .touch-btn {
        width: 56px;
        height: 56px;
    }

    .touch-action {
        width: 58px;
        height: 58px;
    }
}

.patch-toggle {
    color: #aaa;
    font-size: 12px;
    display: flex;
    align-items: center;
    gap: 4px;
    cursor: pointer;
    user-select: none;
    margin-left: 8px;
}
.patch-toggle input { cursor: pointer; }
