/* Adapted from Shubin123/azahar_web web/index.html. See THIRD_PARTY.md. */

        * { box-sizing: border-box; margin: 0; padding: 0; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
            background: #0c0f0c;
            color: #e0e0e0;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
            padding: 16px;
            position: relative;
            isolation: isolate;
        }
        body::before {
            content: "";
            position: fixed;
            inset: 0;
            z-index: -1;
            pointer-events: none;
            opacity: .18;
            background-image: repeating-linear-gradient(45deg, rgba(164,214,106,.12) 0 1px, transparent 1px 4px),
                              repeating-linear-gradient(-45deg, rgba(0,0,0,.18) 0 1px, transparent 1px 4px);
            background-size: 4px 4px;
        }
        header {
            text-align: center;
            margin-bottom: 16px;
        }
        header h1 { font-size: 1.5rem; color: #9bf00b; }
        header p { font-size: 0.85rem; color: #888; }

        .main-layout {
            display: flex;
            flex-wrap: wrap;
            gap: var(--widget-gap, 16px);
            max-width: 2400px;
            width: 100%;
            justify-content: center;
            align-items: flex-start;
        }
        .canvas-panel {
            flex: 0 0 804px;
            min-width: 400px;
            /* 800px drawable area plus the 2px border on each side gives the
               native 400px buffer an exact 2x scale on desktop. */
            max-width: 804px;
            width: 100%;
            text-align: center;
            position: relative;
        }
        canvas {
            border: 2px solid #333;
            border-radius: 4px;
            background: #000;
            width: 100%;
            /* Preserve the native 3DS pixels when the 400x480 dual-screen
               image is enlarged. This matches the known-good gameplay capture
               and avoids browser filtering that blurs HUD and control text. */
            image-rendering: pixelated;
        }
        /* Widget grid: as many columns of at least --widget-w as fit. Widgets
           span 1+ columns and keep their own height, so a tall widget never
           stretches its row neighbours; dense packing back-fills the gaps. */
        .controls-panel {
            --widget-w: 260px;
            --widget-gap: 12px;
            flex: 1 1 calc(var(--widget-w) * 2 + var(--widget-gap));
            min-width: 0;
            width: 100%;
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--widget-w)), 1fr));
            grid-auto-flow: row dense;
            /* Masonry: rows are 4px units and each widget spans as many as its
               height needs (set by azahar_layout.js), so short widgets pack
               under each other instead of leaving holes. */
            grid-auto-rows: 4px;
            column-gap: var(--widget-gap);
            row-gap: 0;
            align-items: start;
            align-content: start;
        }
        .main-layout.widgets-below .controls-panel { flex-basis: 100%; }
        @media (max-width: 840px) {
            .canvas-panel {
                flex: 1 1 auto;
                max-width: 100%;
            }
        }
        .card {
            background: #171c17;
            border: 1px solid #2d3c2d;
            border-radius: 8px;
            padding: 16px;
            min-width: 0;
            overflow-wrap: break-word;
            box-shadow: inset 1px 1px 0 rgba(208,243,172,.12),
                        inset -1px -1px 0 rgba(0,0,0,.45), 0 5px 14px rgba(0,0,0,.2);
        }
        /* Reusable raised surface treatment for the app's mixed widgets. */
        .active-download-card, .library-section, .library-table-container,
        .save-entry, .log-box, .renderer-select, .library-select, .library-input,
        .filter-pill, .btn, .file-label, canvas {
            box-shadow: inset 1px 1px 0 rgba(208,243,172,.12),
                        inset -1px -1px 0 rgba(0,0,0,.38);
        }
        .card h2 {
            font-size: 1rem;
            margin-bottom: 10px;
            color: #9bf00b;
            border-bottom: 1px solid #2d3c2d;
            padding-bottom: 6px;
        }

        /* ── Widgets ─────────────────────────────────────────────── */
        .controls-panel > .card { position: relative; display: flex; flex-direction: column; }
        .controls-panel > .card.has-fixed-height { overflow: auto; }
        .controls-panel > .card.is-collapsed { height: auto !important; overflow: hidden; }
        .controls-panel > .card.is-collapsed > :not(h2) { display: none !important; }
        .controls-panel > .card.is-collapsed > h2 { margin-bottom: 0; border-bottom: 0; padding-bottom: 0; }
        .controls-panel > .card > h2 {
            display: flex;
            align-items: center;
            gap: 6px;
            cursor: grab;
            user-select: none;
            flex: none;
        }
        .widget-title { flex: 1 1 auto; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
        .widget-tools { display: flex; gap: 3px; flex: none; opacity: .35; transition: opacity .2s; }
        .card:hover .widget-tools, .widget-tools:focus-within { opacity: 1; }
        @media (pointer: coarse) { .widget-tools { opacity: .8; } }
        .widget-tool {
            width: 22px;
            height: 22px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 0;
            border: 1px solid #2d3c2d;
            border-radius: 4px;
            background: #111611;
            color: #d8e4d2;
            font-size: .72rem;
            line-height: 1;
            cursor: pointer;
        }
        .widget-tool:hover, .widget-tool:focus-visible { border-color: #9bf00b; color: #fff; }
        .widget-resize {
            position: absolute;
            right: 2px;
            bottom: 2px;
            width: 14px;
            height: 14px;
            cursor: nwse-resize;
            touch-action: none;
            opacity: .45;
            background: linear-gradient(135deg, transparent 0 45%, #a4d66a 45% 52%, transparent 52% 65%,
                                        #a4d66a 65% 72%, transparent 72%);
        }
        .widget-resize:hover { opacity: 1; }
        .card.is-collapsed .widget-resize { display: none; }
        .card.is-dragging { opacity: .45; outline: 2px dashed #9bf00b; }
        .card.is-drop-target { outline: 2px solid #a4d66a; outline-offset: 2px; }
        .card.is-resizing { outline: 2px dashed #a4d66a; outline-offset: 2px; }

        /* Hamburger + layout drawer */
        header { position: relative; width: 100%; max-width: 2400px; padding: 0 46px; }
        .menu-toggle {
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 38px;
            height: 34px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            gap: 5px;
            padding: 0 9px;
            border: 1px solid #2d3c2d;
            border-radius: 6px;
            background: #171c17;
            cursor: pointer;
        }
        .menu-toggle span { display: block; height: 2px; background: #e0e0e0; border-radius: 1px; }
        .menu-toggle:hover, .menu-toggle[aria-expanded="true"] { border-color: #9bf00b; }
        .layout-menu {
            position: fixed;
            top: 0;
            left: 0;
            bottom: 0;
            z-index: 900;
            width: min(340px, 100vw);
            overflow-y: auto;
            padding: 16px;
            background: #171c17;
            border-right: 1px solid #2d3c2d;
            box-shadow: 6px 0 24px rgba(0,0,0,.5);
        }
        .layout-menu[hidden], .layout-menu-backdrop[hidden] { display: none; }
        .layout-menu-backdrop { position: fixed; inset: 0; z-index: 899; background: rgba(0,0,0,.45); }
        .layout-menu-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
        .layout-menu-header h2 { font-size: 1rem; color: #9bf00b; }
        .btn.layout-menu-close { width: auto; margin: 0; padding: 4px 10px; }
        .layout-menu-subhead { font-size: .85rem; color: #a4d66a; margin: 14px 0 6px; }
        .layout-widget-list { list-style: none; display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
        .layout-widget-list li {
            display: grid;
            grid-template-columns: auto 1fr auto;
            align-items: center;
            gap: 6px;
            padding: 5px 6px;
            background: #111611;
            border: 1px solid #354735;
            border-radius: 5px;
            font-size: .8rem;
        }
        .layout-widget-list li.is-hidden .layout-widget-name { color: #7d8b76; text-decoration: line-through; }
        .layout-widget-list input[type="checkbox"] { accent-color: #9bf00b; }
        .layout-widget-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
        .layout-widget-actions { display: flex; gap: 3px; }
        .layout-widget-actions select {
            background: #080b08; color: #e0e0e0; border: 1px solid #2d3c2d; border-radius: 4px; font-size: .72rem;
        }
        .btn {
            display: inline-block;
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 0.9rem;
            font-weight: 600;
            text-align: center;
            width: 100%;
            margin-bottom: 6px;
            transition: opacity 0.2s;
        }
        .btn:hover { opacity: 0.85; }
        .btn:disabled { opacity: 0.4; cursor: not-allowed; }
        .btn-primary { background: #9bf00b; color: #fff; }
        .btn-secondary { background: #2d3c2d; color: #e0e0e0; }
        .btn-success { background: #107c10; color: #fff; }

        input[type="file"] {
            display: none;
        }
        .file-label {
            display: block;
            padding: 8px 16px;
            border: 2px dashed #2d3c2d;
            border-radius: 4px;
            cursor: pointer;
            text-align: center;
            font-size: 0.9rem;
            margin-bottom: 6px;
            word-break: break-all;
        }
        .file-label:hover { border-color: #9bf00b; }

        .renderer-select {
            width: 100%;
            margin-bottom: 6px;
            padding: 8px;
            border: 1px solid #2d3c2d;
            border-radius: 4px;
            background: #080b08;
            color: #e0e0e0;
        }
        .mode-help { font-size: 0.72rem; color: #888; line-height: 1.35; }

        .save-toolbar {
            display: grid;
            grid-template-columns: 82px 1fr;
            gap: 6px;
            align-items: start;
        }
        .save-toolbar .renderer-select { margin-bottom: 0; }
        .save-list {
            display: flex;
            flex-direction: column;
            gap: 6px;
            margin-top: 8px;
            max-height: 230px;
            overflow-y: auto;
        }
        .save-empty { color: #777; font-size: 0.75rem; padding: 6px 0; }
        .save-game-group { display: flex; flex-direction: column; gap: 5px; }
        .save-game-title { color: #c2e99a; font-size: 0.76rem; font-weight: 700; overflow-wrap: anywhere; }
        .save-game-id { color: #7d8b76; font-size: 0.62rem; font-family: monospace; }
        .save-entry {
            background: #111611;
            border: 1px solid #354735;
            border-radius: 5px;
            padding: 7px;
        }
        .save-entry-title { font-size: 0.78rem; font-weight: 700; color: #eee; }
        .save-entry-meta {
            color: #a3b19c;
            font-size: 0.67rem;
            line-height: 1.4;
            margin: 3px 0 6px;
            overflow-wrap: anywhere;
        }
        .save-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
        .save-actions .btn { padding: 5px 7px; margin: 0; font-size: 0.72rem; }
        .btn-danger { background: #71313d; color: #fff; }
        #save-state-status { margin-top: 7px; min-height: 1em; }
        .speed-control { margin-top: 10px; }
        .speed-control label {
            display: flex;
            justify-content: space-between;
            color: #aaa;
            font-size: 0.76rem;
            margin-bottom: 4px;
        }
        .speed-control input { width: 100%; accent-color: #9bf00b; }
        /* Fullscreen view: the stage fills the display and the engine draws
           both screens into a canvas of exactly the stage's shape. */
        .screen-stage { position: relative; }
        .screen-stage canvas { display: block; }
        .screen-stage.is-fullscreen {
            width: 100vw;
            height: 100vh;
            height: 100dvh;
            background: #000;
        }
        .screen-stage.is-pseudo-fullscreen {
            position: fixed;
            inset: 0;
            z-index: 1000;
        }
        html.has-pseudo-fullscreen, html.has-pseudo-fullscreen body { overflow: hidden; }
        .screen-stage.is-fullscreen canvas {
            width: 100%;
            height: 100%;
            border: 0;
            border-radius: 0;
            box-shadow: none;
            /* Before the emulator starts the canvas is still 400x480. */
            object-fit: contain;
        }
        /* Sits in the native frame's empty corner beside the bottom screen. */
        .stage-fullscreen-btn {
            position: absolute;
            right: 1.5%;
            bottom: 1.5%;
            width: 7%;
            min-width: 26px;
            max-width: 40px;
            aspect-ratio: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 0;
            border: 1px solid #2d3c2d;
            border-radius: 6px;
            background: rgba(29, 49, 20, .75);
            color: #e0e0e0;
            cursor: pointer;
            opacity: .6;
            transition: opacity .2s;
        }
        .stage-fullscreen-btn svg { width: 62%; height: 62%; }
        .stage-fullscreen-btn:hover, .stage-fullscreen-btn:focus-visible { opacity: 1; }
        .screen-stage.is-fullscreen .stage-fullscreen-btn { display: none; }
        .fullscreen-overlay {
            display: none;
            position: absolute;
            top: 10px;
            right: 10px;
            flex-direction: column;
            align-items: flex-end;
            gap: 6px;
            opacity: .3;
            transition: opacity .2s;
            z-index: 1;
        }
        .screen-stage.is-fullscreen .fullscreen-overlay { display: flex; }
        .fullscreen-overlay:hover, .fullscreen-overlay:focus-within,
        .fullscreen-overlay.is-open { opacity: 1; }
        .fullscreen-overlay-buttons { display: flex; gap: 6px; }
        .fullscreen-overlay-buttons .btn { width: auto; margin: 0; padding: 6px 10px; }
        .fullscreen-overlay-panel {
            width: 260px;
            background: rgba(23, 28, 23, .94);
            border: 1px solid #2d3c2d;
            border-radius: 8px;
            padding: 12px;
            text-align: left;
        }
        .audio-toggle { display: flex; gap: 6px; align-items: flex-start; margin-top: 10px; cursor: pointer; }
        .audio-toggle input { accent-color: #9bf00b; margin-top: 2px; }
        .local-play-row { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
        .local-play-row .btn { margin-bottom: 0; }
        #local-play-card textarea { resize: vertical; min-height: 54px; font: 0.7rem ui-monospace, monospace; word-break: break-all; }
        #local-play-card input[type="text"] { margin-bottom: 6px; }
        #local-play-status { margin: 8px 0 4px; }
        #local-play-status[data-kind="error"] { color: #ff7a8a; }
        #local-play-status[data-kind="ok"], #local-play-status[data-kind="linked"] { color: #8fd6a6; }
        #local-play-peers { margin: 0 0 6px 18px; font-size: 0.78rem; }
        #public-lobby-list { margin: 6px 0; padding: 0; list-style: none; font-size: 0.78rem; }
        #public-lobby-list li { display: flex; gap: 6px; align-items: center; justify-content: space-between; margin-bottom: 4px; }
        #public-lobby-list .btn { width: auto; margin: 0; padding: 4px 10px; }
        #public-lobby-status[data-kind="error"] { color: #ff7a8a; }
        #public-lobby-status[data-kind="ok"] { color: #8fd6a6; }
        #local-play-card details { margin-top: 8px; }
        #local-play-card summary { cursor: pointer; font-size: 0.8rem; margin-bottom: 6px; }
        #audio-status { margin-top: 8px; }

        #status {
            font-size: 0.8rem;
            color: #aaa;
            min-height: 1.2em;
            word-break: break-all;
        }
        #status.error { color: #9bf00b; }
        #status.ok { color: #6bff6b; }

        #progress {
            width: 100%;
            height: 7px;
            margin-top: 6px;
            accent-color: #9bf00b;
        }

        .log-box {
            background: #080b08;
            border: 1px solid #333;
            border-radius: 4px;
            padding: 8px;
            font-family: 'Courier New', monospace;
            font-size: 0.7rem;
            color: #aaa;
            max-height: 120px;
            overflow-y: auto;
            white-space: pre-wrap;
            word-break: break-all;
        }

        /* ── Game Library Section ────────────────────────────────── */
        .library-section {
            max-width: 2400px;
            width: 100%;
            margin-top: 20px;
            margin-bottom: 30px;
        }
        .library-header {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            align-items: baseline;
            gap: 8px;
            margin-bottom: 12px;
            border-bottom: 1px solid #2d3c2d;
            padding-bottom: 8px;
        }
        .library-title-group {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .library-header h2 {
            font-size: 1.15rem;
            color: #9bf00b;
            margin: 0;
            border: none;
            padding: 0;
        }
        .library-badge {
            background: #2d3c2d;
            color: #a4d66a;
            font-size: 0.72rem;
            font-weight: 600;
            padding: 3px 8px;
            border-radius: 12px;
            border: 1px solid #425a36;
        }
        .library-subtitle {
            font-size: 0.75rem;
            color: #888;
            margin-top: 4px;
            width: 100%;
        }

        /* Active download banner */
        .active-download-card {
            background: #182418;
            border: 1px solid #456c2c;
            border-radius: 6px;
            padding: 12px;
            margin-bottom: 14px;
        }
        .active-download-info {
            display: flex;
            justify-content: space-between;
            font-size: 0.82rem;
            font-weight: 600;
            margin-bottom: 6px;
            color: #e0e0e0;
        }
        .active-download-stats {
            color: #6bff6b;
            font-family: monospace;
        }
        .progress-bar-container {
            background: #0d150b;
            border-radius: 4px;
            height: 10px;
            overflow: hidden;
            margin-bottom: 6px;
            border: 1px solid #354e27;
        }
        .progress-bar-fill {
            height: 100%;
            background: linear-gradient(90deg, #9bf00b, #c2ff78);
            width: 0%;
            transition: width 0.15s ease-out;
        }
        .active-download-actions {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 0.72rem;
            color: #a4d66a;
        }

        /* Downloaded games ready to play from the local cache */
        .ready-card {
            background: #0f2b1f;
            border: 1px solid #1a6b44;
            border-radius: 6px;
            padding: 10px 12px;
            margin-bottom: 14px;
        }
        .ready-header { font-size: .82rem; font-weight: 600; color: #6bff6b; margin-bottom: 6px; }
        .ready-list { list-style: none; margin: 0; padding: 0; max-height: 220px; overflow-y: auto; }
        .ready-game {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 6px 0;
            border-top: 1px solid rgba(107,255,107,.12);
        }
        .ready-game:first-child { border-top: none; }
        .ready-game-info { flex: 1; min-width: 0; overflow-wrap: anywhere; }

        /* Toolbar */
        .library-toolbar {
            display: flex;
            flex-direction: column;
            gap: 10px;
            margin-bottom: 14px;
        }
        .library-source-controls {
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            gap: 8px;
            padding: 9px 11px;
            border: 1px solid #2d3c2d;
            border-radius: 6px;
            background: #121912;
            box-shadow: inset 1px 1px 0 rgba(208,243,172,.12), inset -1px -1px 0 rgba(0,0,0,.38);
        }
        .library-source-label { color: #b1c0a8; font-size: .76rem; font-weight: 600; }
        .library-source-link {
            min-width: 0;
            color: #a4d66a;
            font-size: .72rem;
            overflow-wrap: anywhere;
        }
        .library-source-link:hover { color: #d0f3ac; }
        .search-wrap {
            position: relative;
            width: 100%;
        }
        .library-input {
            width: 100%;
            padding: 10px 36px 10px 14px;
            border: 1px solid #2d3c2d;
            border-radius: 6px;
            background: #080b08;
            color: #e0e0e0;
            font-size: 0.88rem;
            outline: none;
            transition: border-color 0.2s;
        }
        .library-input:focus {
            border-color: #9bf00b;
            box-shadow: inset 1px 1px 0 rgba(208,243,172,.14), 0 0 0 2px rgba(155,240,11,.18);
        }
        .search-clear-btn {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            background: none;
            border: none;
            color: #888;
            font-size: 1.1rem;
            cursor: pointer;
            padding: 4px;
            line-height: 1;
        }
        .search-clear-btn:hover { color: #fff; }

        .filter-row {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            align-items: center;
            gap: 10px;
        }
        .region-filters {
            display: flex;
            flex-wrap: wrap;
            gap: 6px;
        }
        .filter-pill {
            background: #2d3c2d;
            color: #ccc;
            border: 1px solid #3d592b;
            border-radius: 14px;
            padding: 4px 12px;
            font-size: 0.75rem;
            cursor: pointer;
            transition: all 0.15s;
        }
        .filter-pill:hover {
            background: #3d592b;
            color: #fff;
        }
        .filter-pill.active {
            background: #9bf00b;
            border-color: #9bf00b;
            color: #fff;
            font-weight: 600;
            box-shadow: inset 1px 1px 0 rgba(255,255,255,.2), inset -1px -1px 0 rgba(15,35,5,.32);
        }
        .sort-controls {
            display: flex;
            align-items: center;
            gap: 6px;
        }
        .sort-label {
            font-size: 0.75rem;
            color: #888;
        }
        .library-select {
            padding: 5px 8px;
            border: 1px solid #2d3c2d;
            border-radius: 4px;
            background: #080b08;
            color: #e0e0e0;
            font-size: 0.75rem;
        }

        /* Table */
        .library-table-container {
            width: 100%;
            overflow-x: auto;
            border: 1px solid #2d3c2d;
            border-radius: 6px;
            background: #111611;
            max-height: 520px;
            overflow-y: auto;
        }
        .library-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 0.8rem;
            text-align: left;
        }
        .library-table th {
            position: sticky;
            top: 0;
            background: #0c120c;
            color: #a3b19c;
            font-weight: 600;
            padding: 9px 12px;
            border-bottom: 1px solid #354735;
            z-index: 1;
        }
        .library-table td {
            padding: 8px 12px;
            border-bottom: 1px solid #283628;
            vertical-align: middle;
        }
        .library-table tr:hover {
            background: #1e2a1a;
        }
        .game-title-col {
            display: flex;
            flex-direction: column;
            gap: 2px;
        }
        .game-name {
            font-weight: 600;
            color: #f0f0f0;
            word-break: break-word;
        }
        .game-filename {
            font-size: 0.68rem;
            color: #8b9b80;
            font-family: monospace;
            word-break: break-all;
        }
        .region-badge {
            display: inline-block;
            padding: 2px 7px;
            border-radius: 4px;
            font-size: 0.68rem;
            font-weight: 600;
            white-space: nowrap;
        }
        .badge-usa { background: #354735; color: #a4d66a; border: 1px solid #456c2c; }
        .badge-europe { background: #123828; color: #81c995; border: 1px solid #1d523b; }
        .badge-japan { background: #3d1c24; color: #f28b82; border: 1px solid #5a2935; }
        .badge-other { background: #262933; color: #bdc1c6; border: 1px solid #3c4043; }

        .size-badge {
            font-family: monospace;
            color: #aab8a0;
            font-size: 0.72rem;
            white-space: nowrap;
        }
        .game-actions-col {
            display: flex;
            gap: 5px;
            justify-content: flex-end;
            align-items: center;
        }
        .btn-sm {
            padding: 4px 9px;
            font-size: 0.72rem;
            border-radius: 4px;
            width: auto;
            margin-bottom: 0;
            text-decoration: none;
            cursor: pointer;
            white-space: nowrap;
        }
        .icon-btn {
            background: #2d3c2d;
            color: #bed3ac;
            border: 1px solid #425a36;
        }
        .icon-btn:hover {
            background: #354c26;
            color: #fff;
        }
        .library-empty {
            text-align: center;
            color: #888;
            padding: 30px;
            font-size: 0.85rem;
        }

        /* Pagination */
        .library-pagination {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            align-items: center;
            gap: 8px;
            margin-top: 10px;
            padding: 4px 0;
        }
        .page-info {
            font-size: 0.75rem;
            color: #aaa;
        }
        .page-size-select {
            margin-left: auto;
        }
        .keymap-dialog {
            margin: auto;
            width: min(560px, calc(100vw - 32px));
            max-height: calc(100vh - 32px);
            overflow-y: auto;
            background: #171c17;
            color: #e0e0e0;
            border: 1px solid #2d3c2d;
            border-radius: 8px;
            padding: 16px;
        }
        .keymap-dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
        .keymap-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            gap: 8px;
            border-bottom: 1px solid #2d3c2d;
            padding-bottom: 6px;
            margin-bottom: 8px;
        }
        .keymap-header h2 { font-size: 1rem; color: #9bf00b; }
        .keymap-close { width: auto; margin: 0; }
        .keymap-table { width: 100%; border-collapse: collapse; font-size: 0.8rem; margin-bottom: 8px; }
        .keymap-table th, .keymap-table td { padding: 4px 6px; text-align: left; }
        .keymap-table thead th { color: #aaa; font-weight: 600; border-bottom: 1px solid #2d3c2d; }
        .keymap-group th { color: #a4d66a; font-size: 0.75rem; padding-top: 10px; }
        .keymap-pad { color: #aaa; }
        .gamepad-status { margin: 2px 0 8px; }
        .keymap-table kbd {
            display: inline-block;
            min-width: 1.6em;
            padding: 1px 6px;
            border: 1px solid #2d3c2d;
            border-radius: 4px;
            background: #0c0f0c;
            font-family: inherit;
            text-align: center;
        }

        /* ── On-screen controls ──────────────────────────────────── */
        /* Sized to fit comfortably beside the bottom screen on phones, wrapping
           over the left and right sides without obscuring the gameplay area. */
        .touch-controls {
            --tc-u: clamp(22px, 6.5vw, 36px);
            display: flex;
            justify-content: space-between;
            gap: 8px;
            margin-top: 8px;
            color: #e0e0e0;
            font-weight: 700;
            line-height: 1;
            -webkit-user-select: none;
            user-select: none;
            -webkit-touch-callout: none;
            -webkit-tap-highlight-color: transparent;
        }
        .touch-controls[hidden] { display: none !important; }
        .touch-half {
            display: flex;
            flex-direction: column;
            align-items: center;
            gap: calc(var(--tc-u) * .22);
            padding: calc(var(--tc-u) * .15);
            touch-action: none;
        }
        .touch-shoulders, .touch-system { display: flex; gap: calc(var(--tc-u) * .25); }
        .touch-btn, .touch-dpad-arm, .touch-dpad-center {
            display: flex;
            align-items: center;
            justify-content: center;
            background: rgba(29, 49, 20, .72);
            border: 1px solid rgba(164, 214, 106, .35);
            box-shadow: inset 1px 1px 0 rgba(208,243,172,.15), inset -1px -1px 0 rgba(0,0,0,.4);
        }
        .touch-btn.is-pressed, .touch-dpad-arm.is-pressed {
            background: #9bf00b;
            border-color: #c2ff78;
            color: #fff;
        }
        .touch-shoulder {
            width: calc(var(--tc-u) * 1.4);
            height: calc(var(--tc-u) * .75);
            border-radius: calc(var(--tc-u) * .38);
            font-size: calc(var(--tc-u) * .34);
        }
        .touch-system-btn {
            min-width: calc(var(--tc-u) * 1.3);
            height: calc(var(--tc-u) * .55);
            padding: 0 calc(var(--tc-u) * .18);
            border-radius: calc(var(--tc-u) * .28);
            font-size: calc(var(--tc-u) * .24);
            letter-spacing: .04em;
        }
        .touch-face, .touch-dpad {
            display: grid;
            grid-template-columns: repeat(3, calc(var(--tc-u) * 1.05));
            grid-template-rows: repeat(3, calc(var(--tc-u) * 1.05));
        }
        .touch-face-btn { border-radius: 50%; font-size: calc(var(--tc-u) * .45); }
        .touch-face-x { grid-area: 1 / 2; }
        .touch-face-y { grid-area: 2 / 1; }
        .touch-face-a { grid-area: 2 / 3; }
        .touch-face-b { grid-area: 3 / 2; }
        .touch-dpad-arm { color: rgba(224, 224, 224, .6); font-size: calc(var(--tc-u) * .32); }
        .touch-dpad-arm.is-pressed { color: #fff; }
        .touch-dpad-up { grid-area: 1 / 2; border-radius: 6px 6px 0 0; border-bottom: 0; }
        .touch-dpad-down { grid-area: 3 / 2; border-radius: 0 0 6px 6px; border-top: 0; }
        .touch-dpad-left { grid-area: 2 / 1; border-radius: 6px 0 0 6px; border-right: 0; }
        .touch-dpad-right { grid-area: 2 / 3; border-radius: 0 6px 6px 0; border-left: 0; }
        .touch-dpad-center { grid-area: 2 / 2; border-width: 1px 0; border-color: transparent; box-shadow: none; }
        .touch-dpad-up::after { content: '▲'; }
        .touch-dpad-down::after { content: '▼'; }
        .touch-dpad-left::after { content: '◀'; }
        .touch-dpad-right::after { content: '▶'; }
        .touch-stick {
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
            width: calc(var(--tc-u) * 2.2);
            height: calc(var(--tc-u) * 2.2);
            border-radius: 50%;
            background: radial-gradient(circle, rgba(29,49,20,.3), rgba(29,49,20,.75));
            border: 1px solid rgba(164, 214, 106, .35);
        }
        .touch-stick-knob {
            width: 48%;
            height: 48%;
            border-radius: 50%;
            background: #456c2c;
            border: 1px solid rgba(208, 243, 172, .5);
            box-shadow: 0 2px 6px rgba(0,0,0,.45);
            pointer-events: none;
        }
        .touch-stick.is-active .touch-stick-knob { background: #9bf00b; border-color: #c2ff78; }
        /* The New 3DS C-Stick is a small nub above and inside the face buttons. */
        .touch-stick-cstick {
            width: calc(var(--tc-u) * 1.5);
            height: calc(var(--tc-u) * 1.5);
            align-self: flex-start;
        }

        /* Mobile touch controls: wrap over the left and right of the bottom screen */
        @media (max-width: 768px), (pointer: coarse) {
            .canvas-panel {
                position: relative;
            }
            .canvas-panel > .touch-controls {
                position: absolute;
                top: 50%;
                bottom: 24px;
                left: 0;
                right: 0;
                margin-top: 0;
                pointer-events: none;
                display: flex;
                justify-content: space-between;
                align-items: center;
                z-index: 5;
            }
            .canvas-panel > .touch-controls .touch-half {
                pointer-events: auto;
            }
        }

        /* Fullscreen: in landscape, 3 columns (left controls, dual screens, right controls).
           In portrait, screens are stacked and the controls wrap over the left and right
           of the bottom screen. */
        .screen-stage.is-fullscreen.has-touch-controls {
            display: grid;
            grid-template: "left screens right" minmax(0, 1fr) / auto minmax(0, 1fr) auto;
        }
        .screen-stage.is-fullscreen.has-touch-controls canvas {
            grid-area: screens;
            min-width: 0;
            min-height: 0;
        }
        .screen-stage.is-fullscreen .touch-controls { display: contents; }
        .screen-stage.is-fullscreen .touch-half {
            justify-content: space-between;
            padding-top: max(calc(var(--tc-u) * .2), env(safe-area-inset-top));
            padding-bottom: max(calc(var(--tc-u) * .2), env(safe-area-inset-bottom));
        }
        .screen-stage.is-fullscreen .touch-left {
            grid-area: left;
            padding-left: max(calc(var(--tc-u) * .2), env(safe-area-inset-left));
        }
        .screen-stage.is-fullscreen .touch-right {
            grid-area: right;
            padding-right: max(calc(var(--tc-u) * .2), env(safe-area-inset-right));
        }
        @media (orientation: portrait) {
            .touch-controls {
                --tc-u: clamp(20px, 6.2vw, 32px);
            }
            .screen-stage.is-fullscreen.has-touch-controls {
                display: block;
                position: relative;
            }
            .screen-stage.is-fullscreen.has-touch-controls canvas {
                width: 100%;
                height: 100%;
                display: block;
            }
            .screen-stage.is-fullscreen.has-touch-controls .touch-controls {
                display: flex;
                position: absolute;
                top: var(--az-bottom-y, 50%);
                height: var(--az-bottom-h, auto);
                min-height: min-content;
                left: 0;
                right: 0;
                pointer-events: none;
                justify-content: space-between;
                align-items: center;
                z-index: 10;
            }
            .screen-stage.is-fullscreen.has-touch-controls .touch-half {
                pointer-events: auto;
                justify-content: center;
                gap: calc(var(--tc-u) * .2);
                padding: calc(var(--tc-u) * .15);
            }
            .screen-stage.is-fullscreen.has-touch-controls .touch-left {
                padding-left: max(calc(var(--tc-u) * .2), env(safe-area-inset-left));
            }
            .screen-stage.is-fullscreen.has-touch-controls .touch-right {
                padding-right: max(calc(var(--tc-u) * .2), env(safe-area-inset-right));
            }
        }
        /* Move the stage fullscreen button to top-right when touch controls are shown
           so it does not collide with the right-hand touch buttons. */
        .screen-stage.has-touch-controls .stage-fullscreen-btn {
            top: 2%;
            bottom: auto;
            right: 2%;
        }
        /* Keep the fullscreen buttons off the right-hand controls. */
        .screen-stage.has-touch-controls .fullscreen-overlay {
            right: 50%;
            transform: translateX(50%);
            align-items: center;
        }
        /* Phones narrower than the native 400px frame. */
        @media (max-width: 460px) {
            .canvas-panel { min-width: 0; }
        }

/* Xbox has one 4:3 display. Keep Azahar's cards and layout, size this stage accordingly. */
header h1{color:#9bf00b}.canvas-panel{flex:0 1 804px;min-width:0;max-width:804px}
.screen-stage{position:relative}.screen-stage canvas{display:block;aspect-ratio:4/3;object-fit:contain}
.screen-stage:fullscreen{background:#000;width:100vw;height:100vh;display:flex;align-items:center;justify-content:center}
.screen-stage:fullscreen canvas{width:100%;height:100%;border:0;object-fit:contain}
.screen-stage:fullscreen .fullscreen-overlay{display:flex;position:absolute;top:12px;right:12px;bottom:auto;z-index:20}
.fullscreen-overlay{display:none}.field-label{display:block;font-size:.75rem;color:#aaa;margin:8px 0 4px}
#status{font-size:.8rem;line-height:1.5;margin-top:8px;overflow-wrap:anywhere}.library-section{width:100%;max-width:2400px;margin-top:16px}
#library{display:table-row-group}#library tr.selected{background:#263b1d}#library td{padding:10px}
#library td:last-child{display:flex;gap:6px;flex-wrap:wrap}.library-action{width:auto;margin:0;font-size:.75rem;padding:5px 10px}
#states .save-entry{display:flex;flex-wrap:wrap;align-items:center;gap:8px;padding:8px;background:#111611}
#states .save-entry span{flex:1;overflow-wrap:anywhere}#states button{width:auto;margin:0;font-size:.75rem}
#logs{white-space:pre-wrap;overflow-wrap:anywhere;max-height:220px;overflow:auto;font:11px/1.5 monospace}
.library-toolbar{display:flex;flex-wrap:wrap;gap:8px}.library-toolbar>*{flex:1;min-width:150px}
.footer{font-size:.7rem;color:#888;text-align:center;margin:20px}.keymap{font-size:.75rem;line-height:1.8}.keymap kbd{background:#111611;padding:2px 5px;border-radius:3px}
.touch-controls{position:absolute!important;inset:auto 8px 8px!important;height:auto!important;display:flex;justify-content:space-between;gap:12px;pointer-events:none;z-index:10}
.touch-controls[hidden]{display:none!important}.touch-cluster{display:flex;gap:5px;flex-wrap:wrap;max-width:42%;align-items:center;pointer-events:auto}
.touch-button{width:38px;height:38px;border:1px solid #a4d66a;border-radius:8px;background:#2d3c2dcc;color:#fff;font-weight:700;touch-action:none;user-select:none}
.touch-button.pressed{background:#9bf00b}.touch-stick{width:72px;height:72px;border:1px solid #a4d66a;border-radius:50%;background:#2d3c2daa;position:relative;touch-action:none}
.touch-stick-knob{position:absolute;left:22px;top:22px;width:28px;height:28px;border-radius:50%;background:#a4d66a;pointer-events:none}
@media(max-width:840px){.canvas-panel{flex-basis:100%;max-width:100%}}@media(max-width:480px){body{padding:8px}.library-table th:nth-child(2),.library-table td:nth-child(2){display:none}.card{padding:12px}}
:focus-visible{outline:2px solid #a4d66a;outline-offset:3px}[hidden]{display:none!important}

/* Xbox accents: bright green on charcoal; darker green for readable button labels. */
:root{color-scheme:dark;accent-color:#9bf00b}
a{color:#9bf00b}a:hover{color:#c2ff78}
.btn-primary{background:#107c10;color:#fff}
.touch-button.pressed,.touch-stick.is-active .touch-stick-knob{background:#107c10;color:#fff}
.filter-pill.active{background:#107c10;color:#fff}

/* Consistent dark-green buttons across the Xbox interface. */
.btn-primary,.btn-secondary,.btn-success,.btn-danger,.library-action,
.widget-tool,.menu-toggle,.stage-fullscreen-btn,.touch-button,.file-label{
    background:#084b16;
    color:#f2f8ef;
    border-color:#26743a;
}
.btn:hover:not(:disabled),.library-action:hover:not(:disabled),
.widget-tool:hover,.menu-toggle:hover,.stage-fullscreen-btn:hover,
.touch-button:hover,.file-label:hover{
    background:#0b6120;
    border-color:#9bf00b;
    opacity:1;
}
.touch-button.pressed{background:#107c10;border-color:#9bf00b}

.loading-panel{margin-top:12px;padding:12px 14px;border:1px solid #26743a;border-radius:6px;background:#111b12;text-align:left}
.loading-heading{display:flex;justify-content:space-between;gap:10px;align-items:center;margin-bottom:8px;font-size:.8rem}
#loading-label{color:#e0f3d5;overflow-wrap:anywhere}
#loading-detail{color:#9bf00b;font-variant-numeric:tabular-nums;flex-shrink:0;font-size:.75rem}
#progress{appearance:none;width:100%;height:12px;display:block;border:1px solid #26743a;border-radius:6px;overflow:hidden;background:#080b08;color:#9bf00b}
#progress::-webkit-progress-bar{background:#080b08;border-radius:6px}
#progress::-webkit-progress-value{background:linear-gradient(90deg,#107c10,#9bf00b);border-radius:6px}
#progress::-moz-progress-bar{background:linear-gradient(90deg,#107c10,#9bf00b);border-radius:6px}
#progress:indeterminate{background:linear-gradient(90deg,#080b08 0%,#107c10 30%,#9bf00b 45%,#107c10 60%,#080b08 100%);background-size:200% 100%;animation:loading-slide 1.4s linear infinite}
#progress:indeterminate::-webkit-progress-bar{background:transparent}
@keyframes loading-slide{from{background-position:100% 0}to{background-position:-100% 0}}
@media(prefers-reduced-motion:reduce){#progress:indeterminate{animation:none;background-position:50% 0}}
@media(max-width:480px){.loading-heading{align-items:flex-start;flex-direction:column;gap:4px}}
/* Keep startup/import progress visible even when the setup cards are scrolled. */
.loading-panel{position:fixed;z-index:1000;bottom:max(16px,env(safe-area-inset-bottom));left:50%;transform:translateX(-50%);width:min(560px,calc(100vw - 32px));margin:0;box-shadow:0 8px 30px #0009}
