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

:root {
    --bg-primary: #0a0a0f;
    --bg-secondary: #12121e;
    --bg-panel: #1a1a2e;
    --bg-elevated: #22223a;
    --text-primary: #e8e8f0;
    --text-secondary: #8888aa;
    --text-dim: #555570;
    --accent-purple: #7b3fa0;
    --accent-purple-light: #9b5fc0;
    --accent-purple-dark: #5C2D91;
    --accent-teal: #00A89D;
    --accent-teal-light: #00d4c6;
    --accent-red: #c0392b;
    --accent-red-dark: #8b1a1a;
    --btn-bg: #1e1e36;
    --btn-hover: #2a2a4a;
    --screen-glow: rgba(123, 63, 160, 0.4);
    --font-pixel: 'Press Start 2P', monospace;
    --font-mono: 'IBM Plex Mono', monospace;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: var(--font-mono);
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    overflow-x: hidden;
    background-image:
        radial-gradient(ellipse at 20% 0%, rgba(92, 45, 145, 0.08) 0%, transparent 60%),
        radial-gradient(ellipse at 80% 100%, rgba(0, 168, 157, 0.06) 0%, transparent 60%);
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg-primary); }
::-webkit-scrollbar-thumb { background: var(--accent-purple-dark); border-radius: 3px; }

/* HEADER */
#header {
    text-align: center;
    padding: 24px 16px 12px;
}

.title {
    font-family: var(--font-pixel);
    font-size: 1.8rem;
    color: var(--accent-teal);
    text-shadow: 0 0 20px rgba(0, 168, 157, 0.5), 0 2px 0 rgba(0,0,0,0.8);
    letter-spacing: 2px;
}

.berry {
    font-size: 1.4rem;
    vertical-align: middle;
}

.subtitle {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--text-secondary);
    margin-top: 6px;
    font-weight: 300;
    letter-spacing: 1px;
}

/* MAIN */
#main {
    max-width: 780px;
    margin: 0 auto;
    padding: 0 16px 20px;
}

/* SHOULDER BUTTONS */
.shoulder-buttons {
    display: flex;
    justify-content: space-between;
    max-width: 720px;
    margin: 0 auto 0;
    padding: 0 20px;
}

.shoulder-btn {
    font-family: var(--font-pixel);
    font-size: 0.55rem;
    background: linear-gradient(180deg, #3a3a50 0%, #28283e 100%);
    color: var(--text-secondary);
    border: 1px solid #444466;
    border-radius: 8px 8px 0 0;
    padding: 6px 24px;
    cursor: pointer;
    transition: all 0.15s;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

.shoulder-btn:active, .shoulder-btn.pressed {
    background: linear-gradient(180deg, var(--accent-purple) 0%, var(--accent-purple-dark) 100%);
    color: white;
    border-color: var(--accent-purple-light);
}

/* BEZEL */
.bezel {
    background: linear-gradient(145deg, #2e2646 0%, #1c1832 40%, #15122a 100%);
    border-radius: 16px;
    padding: 16px 20px 12px;
    border: 2px solid #3a3456;
    box-shadow:
        0 0 40px rgba(92, 45, 145, 0.15),
        inset 0 1px 0 rgba(255,255,255,0.05);
    position: relative;
}

.screen-label {
    font-family: var(--font-pixel);
    font-size: 0.45rem;
    color: var(--text-dim);
    text-align: center;
    margin-bottom: 8px;
    letter-spacing: 3px;
}

.screen-wrapper {
    position: relative;
    background: #0c0c0c;
    border-radius: 4px;
    overflow: hidden;
    border: 3px solid #0a0a14;
    box-shadow:
        0 0 30px var(--screen-glow),
        inset 0 0 20px rgba(0,0,0,0.8);
    aspect-ratio: 240 / 160;
    max-width: 100%;
}

#gba-screen {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    background: #1a1a2e;
}

.power-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
}

.power-led {
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #333;
    transition: all 0.3s;
}

.power-led.on {
    background: #00ff66;
    box-shadow: 0 0 8px #00ff66;
}

.power-text {
    font-family: var(--font-pixel);
    font-size: 0.35rem;
    color: var(--text-dim);
    letter-spacing: 2px;
}

/* Scanline Overlay */
.scanline-overlay {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    pointer-events: none;
    background: repeating-linear-gradient(
        0deg,
        transparent,
        transparent 1px,
        rgba(0, 0, 0, 0.15) 1px,
        rgba(0, 0, 0, 0.15) 2px
    );
    z-index: 2;
}

.lcd-overlay {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    pointer-events: none;
    background:
        repeating-linear-gradient(
            0deg,
            transparent,
            transparent 2px,
            rgba(0,0,0,0.08) 2px,
            rgba(0,0,0,0.08) 3px
        ),
        repeating-linear-gradient(
            90deg,
            transparent,
            transparent 2px,
            rgba(0,0,0,0.06) 2px,
            rgba(0,0,0,0.06) 3px
        );
    z-index: 2;
}

.hidden { display: none !important; }

/* DROP ZONE */
.drop-zone {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 5;
    background: rgba(10, 10, 15, 0.85);
    opacity: 1;
    transition: opacity 0.3s;
    pointer-events: none;
}

.drop-zone.drag-over {
    background: rgba(92, 45, 145, 0.3);
    border: 2px dashed var(--accent-purple-light);
}

.drop-zone.rom-loaded {
    opacity: 0;
    pointer-events: none;
}

.drop-zone-content {
    text-align: center;
}

.drop-icon {
    font-size: 2rem;
    margin-bottom: 8px;
}

.drop-text {
    font-family: var(--font-pixel);
    font-size: 0.5rem;
    color: var(--text-secondary);
    line-height: 1.8;
}

/* ROM INFO */
.rom-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    margin-top: 8px;
    background: var(--bg-secondary);
    border-radius: 6px;
    font-size: 0.7rem;
}

#rom-name {
    color: var(--text-secondary);
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 70%;
}

.fps-counter {
    color: var(--accent-teal);
    font-family: var(--font-mono);
    font-size: 0.65rem;
    font-weight: 600;
}

/* BUTTONS */
.btn {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 500;
    background: var(--btn-bg);
    color: var(--text-primary);
    border: 1px solid #333355;
    border-radius: 8px;
    padding: 8px 14px;
    cursor: pointer;
    transition: all 0.2s;
    user-select: none;
    -webkit-user-select: none;
    white-space: nowrap;
}

.btn:hover {
    background: var(--btn-hover);
    border-color: var(--accent-purple);
    box-shadow: 0 0 12px rgba(123, 63, 160, 0.2);
}

.btn:active {
    transform: scale(0.96);
}

.btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent-purple) 0%, var(--accent-purple-dark) 100%);
    border-color: var(--accent-purple-light);
    color: white;
    font-weight: 600;
    padding: 10px 20px;
    font-size: 0.8rem;
}

.btn-primary:hover {
    background: linear-gradient(135deg, var(--accent-purple-light) 0%, var(--accent-purple) 100%);
    box-shadow: 0 0 20px rgba(123, 63, 160, 0.4);
}

/* CONTROL BAR */
.control-bar {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
    margin-top: 10px;
    flex-wrap: wrap;
}

.control-group {
    display: flex;
    gap: 6px;
    align-items: center;
}

.rom-loader-btn {
    cursor: pointer;
}

.btn-action { font-size: 1rem; padding: 8px 12px; }
.btn-small { font-size: 0.9rem; padding: 8px 10px; }

/* SAVE BAR */
.save-bar {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 12px;
    margin-top: 10px;
    flex-wrap: wrap;
}

.save-slots {
    display: flex;
    gap: 4px;
}

.btn-slot {
    font-size: 0.6rem;
    padding: 6px 10px;
    border-radius: 6px;
    font-family: var(--font-pixel);
}

.btn-slot.active {
    background: var(--accent-purple-dark);
    border-color: var(--accent-purple);
    color: white;
}

.save-actions {
    display: flex;
    gap: 6px;
}

.btn-save {
    font-size: 0.7rem;
    padding: 6px 12px;
}

/* VOLUME */
.volume-bar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-top: 10px;
}

.volume-icon { font-size: 1rem; }

.volume-slider {
    width: 120px;
    accent-color: var(--accent-teal);
    cursor: pointer;
}

.volume-value {
    font-size: 0.65rem;
    color: var(--text-secondary);
    min-width: 30px;
}

/* GAMEPAD */
.gamepad {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 20px;
    padding: 16px;
    background: var(--bg-secondary);
    border-radius: 16px;
    border: 1px solid #222240;
    touch-action: none;
}

/* D-PAD */
.dpad {
    display: grid;
    grid-template-columns: 40px 40px 40px;
    grid-template-rows: 40px 40px 40px;
    gap: 0;
}

.dpad-btn {
    background: #2a2a42;
    border: 1px solid #3a3a56;
    color: var(--text-dim);
    font-size: 0.8rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    transition: background 0.1s;
}

.dpad-up { grid-column: 2; grid-row: 1; border-radius: 6px 6px 0 0; }
.dpad-left { grid-column: 1; grid-row: 2; border-radius: 6px 0 0 6px; }
.dpad-center { grid-column: 2; grid-row: 2; background: #2a2a42; border: 1px solid #3a3a56; }
.dpad-right { grid-column: 3; grid-row: 2; border-radius: 0 6px 6px 0; }
.dpad-down { grid-column: 2; grid-row: 3; border-radius: 0 0 6px 6px; }

.dpad-btn:active, .dpad-btn.pressed {
    background: var(--accent-purple);
    color: white;
    border-color: var(--accent-purple-light);
}

/* CENTER BUTTONS */
.gamepad-center {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-items: center;
}

.meta-btn {
    font-family: var(--font-pixel);
    font-size: 0.4rem;
    background: #333352;
    color: var(--text-dim);
    border: 1px solid #444466;
    border-radius: 12px;
    padding: 6px 16px;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    transition: all 0.1s;
}

.meta-btn:active, .meta-btn.pressed {
    background: var(--accent-purple);
    color: white;
}

/* A/B BUTTONS */
.gamepad-right {
    display: flex;
    gap: 8px;
    align-items: center;
}

.ab-btn {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    font-family: var(--font-pixel);
    font-size: 0.7rem;
    cursor: pointer;
    border: 2px solid;
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    transition: all 0.1s;
}

.btn-a {
    background: linear-gradient(135deg, #c0392b 0%, #8b1a1a 100%);
    border-color: #e74c3c;
    color: white;
    margin-top: -16px;
}

.btn-b {
    background: linear-gradient(135deg, #a33226 0%, #6b1010 100%);
    border-color: #c0392b;
    color: #ffcccc;
    margin-top: 16px;
}

.ab-btn:active, .ab-btn.pressed {
    transform: scale(0.9);
    filter: brightness(1.3);
}

/* MODALS */
.modal {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-backdrop {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.7);
    backdrop-filter: blur(4px);
}

.modal-content {
    position: relative;
    background: var(--bg-panel);
    border: 1px solid #333355;
    border-radius: 16px;
    width: 90%;
    max-width: 420px;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid #333355;
}

.modal-header h2 {
    font-family: var(--font-pixel);
    font-size: 0.7rem;
    color: var(--accent-teal);
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 4px;
}

.modal-close:hover { color: white; }

.modal-body {
    padding: 16px 20px;
}

/* SETTINGS */
.setting-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
    border-bottom: 1px solid #222240;
}

.setting-row:last-child { border-bottom: none; }

.setting-row label {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.setting-row select,
.setting-row input[type="checkbox"] {
    background: var(--bg-elevated);
    color: var(--text-primary);
    border: 1px solid #444466;
    border-radius: 6px;
    padding: 4px 8px;
    font-family: var(--font-mono);
    font-size: 0.7rem;
}

.setting-row input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--accent-teal);
}

/* CONTROLS TABLE */
.controls-table {
    width: 100%;
    border-collapse: collapse;
}

.controls-table th {
    font-family: var(--font-pixel);
    font-size: 0.5rem;
    color: var(--accent-teal);
    text-align: left;
    padding: 8px;
    border-bottom: 2px solid #333355;
}

.controls-table td {
    font-size: 0.7rem;
    padding: 8px;
    border-bottom: 1px solid #222240;
    color: var(--text-secondary);
}

.controls-table td:last-child {
    font-family: var(--font-mono);
    color: var(--accent-purple-light);
    font-weight: 600;
}

.disclaimer {
    font-size: 0.65rem;
    color: var(--accent-red);
    margin-top: 16px;
    padding: 10px;
    background: rgba(192, 57, 43, 0.1);
    border-radius: 8px;
    text-align: center;
    font-family: var(--font-pixel);
    line-height: 1.6;
}

/* FOOTER */
#footer {
    text-align: center;
    padding: 24px 16px;
    border-top: 1px solid #1a1a2e;
    margin-top: 20px;
}

#footer p {
    font-family: var(--font-pixel);
    font-size: 0.5rem;
    color: var(--text-dim);
    line-height: 2;
}

.footer-sub {
    font-family: var(--font-mono) !important;
    font-size: 0.6rem !important;
}

.footer-sub a {
    color: var(--accent-teal);
    text-decoration: none;
    transition: color 0.2s;
}

.footer-sub a:hover {
    color: var(--accent-teal-light);
    text-decoration: underline;
}

/* Desktop: hide gamepad */
@media (min-width: 769px) {
    .gamepad { display: none; }
}

/* Show gamepad on touch or small screens */
@media (max-width: 768px) {
    .title { font-size: 1.2rem; }
    .subtitle { font-size: 0.55rem; }
    .bezel { padding: 10px 12px 8px; border-radius: 12px; }
    .control-bar { gap: 4px; }
    .btn { padding: 6px 10px; font-size: 0.65rem; }
    .btn-primary { padding: 8px 14px; font-size: 0.7rem; }
    .btn-action { padding: 6px 10px; }
    .gamepad { display: flex; }
    .save-bar { gap: 6px; }
}

@media (hover: none) and (pointer: coarse) {
    .gamepad { display: flex !important; }
}

/* Welcome screen animation */
@keyframes pulse-glow {
    0%, 100% { opacity: 0.6; }
    50% { opacity: 1; }
}

.welcome-text {
    animation: pulse-glow 2s ease-in-out infinite;
}