@import url('https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@400;500;700&family=JetBrains+Mono:wght@400;500;700&display=swap');

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

:root {
    --bg: #0f1113;
    --bg-elevated: #171a1d;
    --bg-panel: #1d2126;
    --bg-panel-strong: #232830;
    --line: #2f353d;
    --line-strong: #4b5563;
    --text: #f3efe6;
    --muted: #a7afb8;
    --accent: #84d1c4;
    --accent-strong: #5fb7a8;
    --alert: #e56d61;
    --shadow: 0 18px 48px rgba(0, 0, 0, 0.35);
    --radius: 20px;
    --radius-sm: 14px;
}

html { background: var(--bg); }

body {
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: 'Space Grotesk', sans-serif;
    padding: 24px;
    -webkit-text-size-adjust: 100%;
    overscroll-behavior: none;
}

body::before {
    content: '';
    position: fixed;
    inset: 12px;
    pointer-events: none;
    border: 1px solid rgba(255, 255, 255, 0.03);
    border-radius: 6px;
}

body > * {
    position: relative;
    z-index: 1;
}

.app-shell {
    max-width: 1320px;
    margin: 0 auto;
}

.app-header { margin-bottom: 18px; }

.eyebrow {
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: 0.18em;
    font-size: 0.7rem;
    font-family: 'JetBrains Mono', monospace;
    margin-bottom: 8px;
}

h1 {
    font-size: clamp(1.8rem, 4.5vw, 4rem);
    line-height: 1;
    letter-spacing: -0.04em;
    margin-bottom: 8px;
}

.app-subtitle {
    max-width: 680px;
    color: var(--muted);
    font-size: 0.92rem;
    line-height: 1.6;
}

.warning-banner {
    background: rgba(229, 109, 97, 0.12);
    color: var(--text);
    padding: 12px 16px;
    border: 1px solid rgba(229, 109, 97, 0.35);
    border-radius: var(--radius-sm);
    margin-bottom: 14px;
    line-height: 1.55;
    font-size: 0.9rem;
}

.warning-banner code {
    display: inline-block;
    margin: 5px 0;
    padding: 4px 7px;
    border-radius: 8px;
    background: rgba(15, 17, 19, 0.9);
    border: 1px solid var(--line);
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.8rem;
    word-break: break-all;
}

/* -------------------------------------------------------------
   Compact top bar
   ------------------------------------------------------------- */
.top-bar {
    padding: 14px;
    margin-bottom: 14px;
}

.top-bar-row {
    display: grid;
    grid-template-columns: 1fr 1.4fr auto;
    gap: 12px;
    align-items: end;
}

body.is-gba .top-bar-row,
body.system-gba .top-bar-row {
    grid-template-columns: minmax(160px, 1fr) minmax(220px, 1.35fr) minmax(190px, 1fr) auto;
}

.action-group { flex-shrink: 0; }

.action-buttons {
    display: flex;
    gap: 8px;
}

.icon-btn {
    width: 46px;
    height: 46px;
    padding: 0;
    border-radius: 12px;
    border: 1px solid var(--line-strong);
    background: var(--bg-panel);
    color: var(--text);
    font-size: 1.2rem;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.18s ease;
}

.icon-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.panel {
    background: rgba(23, 26, 29, 0.94);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.field-group { min-width: 0; }
.field-grow { width: 100%; }

.field-group label,
.control-group label {
    display: block;
    margin-bottom: 6px;
    color: var(--text);
    font-weight: 700;
    font-size: 0.85rem;
}

.select-wrap { position: relative; }

select,
input[type="file"] {
    width: 100%;
    min-height: 46px;
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--line-strong);
    background: var(--bg-panel);
    color: var(--text);
    font: inherit;
    font-size: 0.92rem;
    transition: border-color 0.2s ease, background 0.2s ease;
}

select {
    appearance: none;
    padding-right: 38px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23a7afb8' d='M1.41.59 6 5.17 10.59.59 12 2l-6 6-6-6z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
}

select:hover,
input[type="file"]:hover,
select:focus,
input[type="file"]:focus {
    outline: none;
    border-color: var(--accent);
    background-color: var(--bg-panel-strong);
}

input[type="file"] { padding: 8px; }

input[type="file"]::file-selector-button {
    margin-right: 10px;
    padding: 8px 12px;
    border-radius: 10px;
    border: 1px solid var(--line-strong);
    background: #252b31;
    color: var(--text);
    font: inherit;
    font-weight: 700;
    font-size: 0.85rem;
    cursor: pointer;
}

.btn-primary,
.btn-secondary {
    min-height: 44px;
    padding: 10px 16px;
    border-radius: var(--radius-sm);
    font: inherit;
    font-weight: 700;
    font-size: 0.9rem;
    cursor: pointer;
    transition: transform 0.15s ease, border-color 0.2s ease, background 0.2s ease;
}

.btn-primary {
    background: var(--accent);
    color: #10201d;
    border: 1px solid transparent;
}

.btn-primary:hover {
    background: var(--accent-strong);
    transform: translateY(-1px);
}

.btn-secondary {
    background: var(--bg-panel);
    color: var(--text);
    border: 1px solid var(--line-strong);
}

.btn-secondary:hover {
    border-color: var(--accent);
    transform: translateY(-1px);
}

/* -------------------------------------------------------------
   Stage panel + screen
   ------------------------------------------------------------- */
.stage-panel { padding: 14px; }

.stage-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}

.stage-header h2 {
    font-size: 1.1rem;
    letter-spacing: -0.02em;
}

.stage-badge {
    padding: 5px 10px;
    border-radius: 999px;
    border: 1px solid var(--line-strong);
    background: #111315;
    color: var(--muted);
    font-size: 0.7rem;
    font-family: 'JetBrains Mono', monospace;
    white-space: nowrap;
}

#screen {
    background: #0b0d0f;
    border-radius: 16px;
    padding: 12px;
    border: 1px solid #2a2f36;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
    /* overflow stays visible so EmulatorJS's menu bar / popups aren't clipped */
}

#canvas {
    display: block;
    width: 100%;
    max-width: 100%;
    height: auto;
    background: #000;
    border-radius: 10px;
    border: 1px solid #343a42;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    margin: 0 auto;
    aspect-ratio: 256 / 240;
    max-height: 70vh;
    object-fit: contain;
}

#canvas.gba { aspect-ratio: 240 / 160; }

#game-container {
    display: none;
    width: 100%;
    max-width: 100%;
    background: #000;
    border-radius: 10px;
    border: 1px solid #343a42;
    /* No overflow:hidden, no strict aspect-ratio — EmulatorJS handles its own
       letterboxing AND needs vertical room for its top menu bar (save state,
       settings, virtual gamepad button, fullscreen). */
    min-height: 360px;
    max-height: 80vh;
    position: relative;
}

#game-container[style*="flex"] {
    display: flex !important;
    align-items: center;
    justify-content: center;
}

#game {
    width: 100%;
    height: 100%;
    background: #000;
    display: flex;
    align-items: center;
    justify-content: center;
}

#game.gba-alt-active {
    flex-direction: column;
    gap: 10px;
    padding: 10px;
    align-items: stretch;
    justify-content: flex-start;
}

.gba-alt-frame {
    width: 100%;
    min-height: 420px;
    height: 100%;
    flex: 1 1 auto;
    border: 0;
    border-radius: 8px;
    background: #000;
}

.gba-alt-note {
    margin: 0;
    color: var(--muted);
    font-size: 0.82rem;
    line-height: 1.45;
    text-align: center;
}

/* Let EmulatorJS size its own internals — only constrain to not overflow */
#game .ejs_player,
#game > div {
    max-width: 100%;
    max-height: 100%;
}

#game canvas {
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

body.is-gba #game,
body.system-gba #game,
body.is-gba #game .ejs_parent,
body.system-gba #game .ejs_parent,
body.is-gba #game .ejs_game,
body.system-gba #game .ejs_game,
body.is-gba #game .ejs_canvas_parent,
body.system-gba #game .ejs_canvas_parent {
    width: 100%;
    height: 100%;
}

body.is-gba #game canvas,
body.system-gba #game canvas {
    width: 100%;
    height: 100%;
}

@media (max-width: 768px) {
    body.is-gba #game,
    body.system-gba #game,
    body.is-gba #game .ejs_game,
    body.system-gba #game .ejs_game {
        display: flex;
        align-items: flex-start;
        justify-content: center;
    }

    body.is-gba #game .ejs_canvas_parent,
    body.system-gba #game .ejs_canvas_parent {
        aspect-ratio: 3 / 2;
        height: auto;
        max-height: calc(100% - 190px);
        flex: 0 0 auto;
    }

    body.is-gba #game canvas,
    body.system-gba #game canvas {
        aspect-ratio: 3 / 2;
        height: 100%;
        object-fit: contain;
    }
}

#status {
    margin-top: 12px;
    padding: 11px 14px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--line);
    background: rgba(15, 17, 19, 0.9);
    color: var(--muted);
    min-height: 42px;
    display: flex;
    align-items: center;
    line-height: 1.5;
    font-size: 0.86rem;
    word-break: break-word;
}

.gba-save-hint {
    margin-top: 12px;
    padding: 11px 14px;
    border-radius: var(--radius-sm);
    border: 1px solid rgba(132, 209, 196, 0.28);
    background: rgba(132, 209, 196, 0.1);
    color: var(--text);
    font-size: 0.86rem;
    line-height: 1.45;
}

.dev-details {
    margin-top: 10px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--line);
    background: #111315;
    overflow: hidden;
}

.dev-details > summary {
    padding: 10px 14px;
    cursor: pointer;
    color: var(--muted);
    font-size: 0.82rem;
    font-family: 'JetBrains Mono', monospace;
    list-style: none;
    user-select: none;
}

.dev-details > summary::-webkit-details-marker { display: none; }

.dev-details > summary::before {
    content: '▸ ';
    color: var(--accent);
    transition: transform 0.2s ease;
    display: inline-block;
}

.dev-details[open] > summary::before {
    transform: rotate(90deg);
}

.dev-details > summary:hover { color: var(--text); }

.debug {
    max-height: 110px;
    overflow-y: auto;
    padding: 10px 14px;
    border-top: 1px solid var(--line);
    color: #8fdccc;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.74rem;
    line-height: 1.55;
    word-break: break-word;
}

.debug::-webkit-scrollbar,
.modal-content::-webkit-scrollbar { width: 8px; }
.debug::-webkit-scrollbar-track,
.modal-content::-webkit-scrollbar-track { background: #111315; }
.debug::-webkit-scrollbar-thumb,
.modal-content::-webkit-scrollbar-thumb { background: #30363f; border-radius: 999px; }

/* -------------------------------------------------------------
   NES Touch Controls — INLINE block, sits below the game screen.
   No absolute positioning, no overlays, can't block any other UI.
   ------------------------------------------------------------- */
.touch-controls {
    display: none; /* set to grid by JS show() when NES + touch device */
    margin-top: 14px;
    padding: 14px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--line);
    background: rgba(15, 17, 19, 0.6);
    grid-template-columns: auto 1fr auto;
    align-items: center;
    justify-items: center;
    gap: 14px;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
}

body.is-gba #touchControls,
body.system-gba #touchControls {
    display: none !important;
    visibility: hidden !important;
    pointer-events: none !important;
}

.touch-dpad {
    position: relative;
    width: 150px;
    height: 150px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 4px;
}

.dpad-up    { grid-column: 2; grid-row: 1; border-radius: 14px 14px 6px 6px; }
.dpad-left  { grid-column: 1; grid-row: 2; border-radius: 14px 6px 6px 14px; }
.dpad-right { grid-column: 3; grid-row: 2; border-radius: 6px 14px 14px 6px; }
.dpad-down  { grid-column: 2; grid-row: 3; border-radius: 6px 6px 14px 14px; }
.dpad-center {
    grid-column: 2; grid-row: 2;
    background: rgba(15, 17, 19, 0.55);
    border-radius: 6px;
    pointer-events: none;
}

.touch-system {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.system-select,
.system-start {
    min-width: 84px;
    height: 32px;
    border-radius: 999px;
    font-size: 0.7rem;
    letter-spacing: 0.08em;
}

.touch-actions {
    position: relative;
    width: 150px;
    height: 120px;
}

.action-a {
    position: absolute;
    width: 70px;
    height: 70px;
    border-radius: 50%;
    top: 0;
    right: 0;
    font-size: 1.5rem;
}

.action-b {
    position: absolute;
    width: 70px;
    height: 70px;
    border-radius: 50%;
    bottom: 0;
    left: 0;
    font-size: 1.5rem;
}

.touch-btn {
    background: rgba(255, 255, 255, 0.14);
    color: var(--text);
    border: 1px solid rgba(255, 255, 255, 0.22);
    font-family: 'JetBrains Mono', monospace;
    font-weight: 700;
    font-size: 1rem;
    cursor: pointer;
    transition: background 0.08s ease, transform 0.08s ease, border-color 0.08s ease;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
    display: flex;
    align-items: center;
    justify-content: center;
}

.touch-btn.is-pressed {
    background: var(--accent);
    color: #10201d;
    border-color: var(--accent);
    transform: scale(0.95);
    box-shadow: 0 0 16px rgba(132, 209, 196, 0.6);
}

/* -------------------------------------------------------------
   Info grid
   ------------------------------------------------------------- */
.info-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px;
    margin-top: 16px;
}

.info { padding: 16px; }

.info strong {
    display: block;
    margin-bottom: 8px;
    color: var(--text);
    font-size: 0.92rem;
}

.info p {
    color: var(--muted);
    line-height: 1.55;
    font-size: 0.85rem;
}

.control-summary { display: grid; gap: 10px; }

.control-summary-item {
    padding: 10px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--line);
    background: rgba(15, 17, 19, 0.9);
}

.system-chip {
    display: inline-flex;
    margin-bottom: 6px;
    padding: 4px 9px;
    border-radius: 999px;
    background: rgba(132, 209, 196, 0.12);
    border: 1px solid rgba(132, 209, 196, 0.3);
    color: var(--accent);
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.7rem;
}

/* -------------------------------------------------------------
   Modal
   ------------------------------------------------------------- */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1000;
    background: rgba(8, 10, 12, 0.85);
    align-items: center;
    justify-content: center;
    padding: 16px;
}

.modal-content {
    width: min(700px, 100%);
    max-height: min(90vh, 920px);
    overflow-y: auto;
    background: var(--bg-elevated);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: 0 28px 70px rgba(0, 0, 0, 0.45);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 14px;
    padding: 16px 20px;
    border-bottom: 1px solid var(--line);
}

.modal-header h2 {
    font-size: 1.25rem;
    letter-spacing: -0.02em;
}

.close-btn {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    border: 1px solid var(--line);
    background: #111315;
    color: var(--muted);
    font-size: 1.4rem;
    line-height: 1;
    cursor: pointer;
}

.close-btn:hover {
    color: var(--text);
    border-color: var(--accent);
}

.modal-body { padding: 20px; }

.control-group { margin-bottom: 16px; }

.control-mapping { display: grid; gap: 10px; }

.control-item {
    display: flex;
    justify-content: space-between;
    gap: 14px;
    align-items: flex-start;
    padding: 12px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--line);
    background: #111315;
}

.control-label { flex: 0 0 130px; }
.control-name { font-weight: 700; font-size: 0.9rem; }

.control-keys {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    flex: 1;
}

.key-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    border-radius: 10px;
    border: 1px solid rgba(132, 209, 196, 0.25);
    background: rgba(132, 209, 196, 0.1);
    color: var(--text);
    font-size: 0.8rem;
    font-family: 'JetBrains Mono', monospace;
}

.key-badge .remove-key {
    width: 20px;
    height: 20px;
    border: 0;
    border-radius: 6px;
    background: rgba(229, 109, 97, 0.14);
    color: var(--alert);
    cursor: pointer;
    font-size: 0.95rem;
    line-height: 1;
}

.add-key-btn {
    padding: 6px 10px;
    border-radius: 10px;
    border: 1px dashed var(--line-strong);
    background: transparent;
    color: var(--accent);
    font: inherit;
    font-weight: 700;
    font-size: 0.8rem;
    cursor: pointer;
}

.add-key-btn:hover,
.add-key-btn.waiting {
    border-color: var(--accent);
    background: rgba(132, 209, 196, 0.08);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    margin-top: 18px;
    padding-top: 16px;
    border-top: 1px solid var(--line);
}

.notification {
    position: fixed;
    right: 16px;
    bottom: 16px;
    z-index: 2000;
    padding: 12px 16px;
    border-radius: 12px;
    background: var(--accent);
    color: #10201d;
    font-weight: 700;
    font-size: 0.9rem;
    box-shadow: var(--shadow);
    opacity: 0;
    transform: translateY(16px);
    transition: opacity 0.25s ease, transform 0.25s ease;
}

.notification.show { opacity: 1; transform: translateY(0); }

/* =============================================================
   RESPONSIVE BREAKPOINTS
   ============================================================= */

@media (max-width: 1080px) {
    #canvas { max-height: 60vh; }
    #game-container { max-height: 70vh; min-height: 340px; }
}

@media (max-width: 768px) {
    body { padding: 12px; }
    body::before { inset: 6px; }

    .app-header { margin-bottom: 12px; }

    .top-bar { padding: 12px; }

    .top-bar-row {
        grid-template-columns: 1fr 1fr;
    }

    body.is-gba .top-bar-row,
    body.system-gba .top-bar-row {
        grid-template-columns: 1fr 1fr;
    }

    .top-bar-row .action-group {
        grid-column: 1 / -1;
    }

    .action-buttons { justify-content: flex-end; }

    .stage-panel, .info, .modal-body { padding: 12px; }

    .modal-actions { flex-direction: column; align-items: stretch; }

    .stage-header { gap: 8px; }
    .stage-header h2 { font-size: 1rem; }
    .stage-badge { font-size: 0.66rem; padding: 4px 8px; }

    .info-grid {
        grid-template-columns: 1fr;
        gap: 10px;
        margin-top: 12px;
    }

    .control-item { flex-direction: column; }
    .control-label { flex-basis: auto; }

    #screen { padding: 8px; }
    #canvas { max-height: 50vh; }
    #game-container { max-height: 65vh; min-height: 320px; }
    body.is-gba #game-container,
    body.system-gba #game-container {
        height: clamp(520px, 76dvh, 760px);
        min-height: 520px;
        max-height: none;
    }

    .modal-header { padding: 14px 16px; }
    .modal-header h2 { font-size: 1.1rem; }
    .modal-body { padding: 14px; }

    .touch-controls { padding: 12px; gap: 10px; }
    .touch-dpad { width: 130px; height: 130px; }
    .touch-actions { width: 130px; height: 100px; }
    .action-a, .action-b { width: 60px; height: 60px; font-size: 1.3rem; }
    .system-select, .system-start { min-width: 72px; font-size: 0.66rem; }
}

@media (max-width: 480px) {
    .hide-on-mobile { display: none !important; }

    body { padding: 8px; }
    body::before { display: none; }

    .app-header { margin-bottom: 10px; }
    .eyebrow { font-size: 0.64rem; margin-bottom: 4px; }
    h1 { font-size: 1.55rem; margin-bottom: 4px; }
    .app-subtitle { font-size: 0.82rem; line-height: 1.45; }

    .top-bar { padding: 10px; margin-bottom: 10px; }
    .top-bar-row { gap: 8px; }

    .panel { border-radius: 14px; }

    .stage-panel, .info { padding: 10px; }

    .stage-header { margin-bottom: 8px; }
    .stage-header h2 { font-size: 0.95rem; }

    #screen { padding: 6px; border-radius: 12px; }
    #canvas, #game-container { border-radius: 8px; }
    #canvas { max-height: 45vh; }
    #game-container { max-height: 60vh; min-height: 300px; }
    body.is-gba #game-container,
    body.system-gba #game-container {
        height: clamp(520px, calc(100dvh - 220px), 680px);
        min-height: 520px;
        max-height: none;
    }

    #status { font-size: 0.8rem; padding: 9px 12px; min-height: 38px; }

    .info p { font-size: 0.8rem; }
    .info strong { font-size: 0.88rem; }

    .notification { left: 8px; right: 8px; bottom: 8px; text-align: center; }

    .touch-controls {
        grid-template-columns: auto 1fr auto;
        padding: 10px;
        gap: 8px;
    }
    .touch-dpad { width: 118px; height: 118px; }
    .touch-actions { width: 118px; height: 92px; }
    .action-a, .action-b { width: 55px; height: 55px; font-size: 1.2rem; }
    .system-select, .system-start { min-width: 64px; height: 28px; font-size: 0.62rem; }
}
