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

:root {
    --bg-deep: #0d0d1a;
    --bg-main: #1a1a2e;
    --bg-panel: #16213e;
    --green: #00ff41;
    --amber: #ffb000;
    --red: #ff0040;
    --cyan: #00e5ff;
    --bezel-cream: #e8dcc8;
    --bezel-dark: #3a3530;
    --text-dim: #667788;
    --text-main: #c0c8d4;
    --border: #2a3a4e;
}

@font-face {
    font-family: 'Pixel';
    src: local('Press Start 2P');
}

body {
    background: var(--bg-deep);
    color: var(--text-main);
    font-family: 'JetBrains Mono', monospace;
    min-height: 100vh;
    overflow-x: hidden;
}

/* Scanline background */
body::before {
    content: '';
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: repeating-linear-gradient(
        0deg,
        transparent,
        transparent 2px,
        rgba(0,0,0,0.08) 2px,
        rgba(0,0,0,0.08) 4px
    );
    pointer-events: none;
    z-index: 9999;
}

#app-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 10px;
}

/* HEADER */
header {
    text-align: center;
    padding: 20px 10px;
    border-bottom: 2px solid var(--border);
    margin-bottom: 15px;
}

.header-content {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 20px;
    flex-wrap: wrap;
}

.cassette-icon {
    width: 60px;
    height: 40px;
    background: var(--amber);
    border-radius: 4px;
    position: relative;
    border: 2px solid #c89000;
    animation: cassetteWobble 3s ease-in-out infinite;
}

.cassette-icon::before {
    content: '';
    position: absolute;
    top: 10px; left: 12px;
    width: 12px; height: 12px;
    border: 2px solid var(--bg-deep);
    border-radius: 50%;
    box-shadow: 22px 0 0 0 var(--bg-deep), 22px 0 0 2px var(--bg-deep);
}

.cassette-icon::after {
    content: '';
    position: absolute;
    bottom: 4px; left: 8px; right: 8px;
    height: 6px;
    background: #8b6914;
    border-radius: 2px;
}

@keyframes cassetteWobble {
    0%, 100% { transform: rotate(-2deg); }
    50% { transform: rotate(2deg); }
}

header h1 {
    font-family: 'Press Start 2P', monospace;
    font-size: clamp(12px, 2.5vw, 20px);
    color: var(--amber);
    text-shadow: 0 0 20px rgba(255,176,0,0.4);
    line-height: 1.4;
}

.subtitle {
    font-size: clamp(8px, 1.5vw, 12px);
    color: var(--green);
    margin-top: 6px;
    font-family: 'JetBrains Mono', monospace;
}

/* MAIN LAYOUT */
#main-content {
    display: flex;
    gap: 15px;
    align-items: flex-start;
}

#left-panel {
    flex: 1;
    min-width: 0;
}

#right-panel {
    width: 320px;
    flex-shrink: 0;
}

/* DROP ZONE */
.drop-zone {
    border: 3px dashed var(--amber);
    border-radius: 12px;
    padding: 30px 20px;
    text-align: center;
    cursor: pointer;
    transition: all 0.3s;
    background: rgba(255,176,0,0.03);
    position: relative;
}

.drop-zone:hover, .drop-zone.dragover {
    border-color: var(--green);
    background: rgba(0,255,65,0.05);
    box-shadow: 0 0 30px rgba(0,255,65,0.1);
}

.cassette-slot {
    width: 120px;
    height: 8px;
    background: #333;
    margin: 0 auto 15px;
    border-radius: 4px;
    border: 1px solid #555;
}

.insert-label {
    font-family: 'Press Start 2P', monospace;
    font-size: 14px;
    color: var(--amber);
    margin-bottom: 8px;
}

.insert-sub {
    font-size: 11px;
    color: var(--text-dim);
    line-height: 1.6;
}

/* ROM INFO */
.rom-info {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    margin-top: 10px;
    font-size: 11px;
}

.rom-info.hidden { display: none; }

.rom-info-header {
    font-family: 'Press Start 2P', monospace;
    font-size: 10px;
    color: var(--green);
    margin-bottom: 8px;
}

.rom-detail {
    color: var(--text-dim);
    margin-bottom: 4px;
}

.hex-preview {
    background: #0a0a14;
    border: 1px solid var(--border);
    padding: 8px;
    font-size: 10px;
    font-family: 'JetBrains Mono', monospace;
    color: var(--green);
    overflow-x: auto;
    white-space: pre;
    border-radius: 4px;
    margin-top: 8px;
    max-height: 80px;
    overflow-y: auto;
}

.game-selector {
    margin-top: 10px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.game-selector label {
    font-size: 11px;
    color: var(--text-dim);
    white-space: nowrap;
}

.game-selector select {
    flex: 1;
    background: var(--bg-panel);
    color: var(--text-main);
    border: 1px solid var(--border);
    padding: 4px 8px;
    font-family: 'JetBrains Mono', monospace;
    font-size: 11px;
    border-radius: 4px;
}

/* CONSOLE BEZEL */
#emulator-display {
    margin: 15px 0;
}

#console-bezel {
    background: linear-gradient(145deg, #e8dcc8, #d4c8b4);
    border-radius: 16px;
    padding: 12px;
    box-shadow: 
        inset 0 2px 4px rgba(255,255,255,0.3),
        0 8px 24px rgba(0,0,0,0.5),
        0 0 0 3px #3a3530;
    max-width: 700px;
    margin: 0 auto;
}

.bezel-top {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    margin-bottom: 8px;
}

.bezel-stripe {
    flex: 1;
    height: 4px;
    border-radius: 2px;
}

.bezel-stripe.red { background: #cc2233; }
.bezel-stripe.orange { background: #ee7722; }

.bezel-logo {
    font-family: 'Press Start 2P', monospace;
    font-size: clamp(7px, 1.5vw, 11px);
    color: #3a3530;
    white-space: nowrap;
    letter-spacing: 2px;
}

.screen-wrapper {
    position: relative;
    background: #111;
    border-radius: 8px;
    overflow: hidden;
    border: 3px solid #222;
    aspect-ratio: 248/192;
}

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

.crt-overlay {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    pointer-events: none;
    z-index: 2;
}

.crt-overlay.scanlines::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: repeating-linear-gradient(
        0deg,
        transparent,
        transparent 1px,
        rgba(0,0,0,0.25) 1px,
        rgba(0,0,0,0.25) 2px
    );
}

.crt-overlay.glow::after {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: radial-gradient(ellipse at center, transparent 60%, rgba(0,0,0,0.3) 100%);
    box-shadow: inset 0 0 60px rgba(0,255,65,0.05);
}

.crt-overlay.curve {
    border-radius: 20px / 15px;
    box-shadow: inset 0 0 30px rgba(0,0,0,0.5);
}

.screen-off {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: #0a0a0a;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 3;
    transition: opacity 0.5s;
}

.screen-off.hidden {
    opacity: 0;
    pointer-events: none;
}

.screen-off.tv-on {
    animation: tvTurnOn 0.8s ease-out forwards;
}

@keyframes tvTurnOn {
    0% { opacity: 1; clip-path: inset(48% 0 48% 0); }
    30% { clip-path: inset(45% 0 45% 0); background: #222; }
    60% { clip-path: inset(20% 0 20% 0); background: #111; }
    100% { opacity: 0; clip-path: inset(0 0 0 0); }
}

.no-signal {
    font-family: 'Press Start 2P', monospace;
    font-size: 14px;
    color: #444;
    animation: signalBlink 2s ease-in-out infinite;
}

@keyframes signalBlink {
    0%, 100% { opacity: 0.3; }
    50% { opacity: 1; }
}

.bezel-bottom {
    text-align: center;
    padding: 6px;
}

.epoch-label {
    font-family: 'Press Start 2P', monospace;
    font-size: 10px;
    color: #666;
    letter-spacing: 4px;
}

/* CONTROLS PANEL */
#controls-panel {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 15px;
}

.emu-controls {
    display: flex;
    gap: 8px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}

.retro-btn {
    font-family: 'Press Start 2P', monospace;
    font-size: 11px;
    padding: 8px 14px;
    background: linear-gradient(180deg, #444 0%, #2a2a2a 100%);
    color: var(--text-main);
    border: 2px outset #555;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.1s;
    white-space: nowrap;
}

.retro-btn:hover {
    background: linear-gradient(180deg, #555 0%, #3a3a3a 100%);
    box-shadow: 0 0 10px rgba(255,176,0,0.2);
}

.retro-btn:active {
    border-style: inset;
    transform: translateY(1px);
}

.retro-btn.small {
    font-size: 9px;
    padding: 5px 10px;
}

.sliders-row {
    display: flex;
    gap: 20px;
    margin-bottom: 10px;
    flex-wrap: wrap;
}

.slider-group {
    flex: 1;
    min-width: 150px;
}

.slider-group label {
    font-size: 10px;
    color: var(--text-dim);
    display: block;
    margin-bottom: 4px;
}

.slider-group label span {
    color: var(--amber);
}

input[type="range"] {
    -webkit-appearance: none;
    width: 100%;
    height: 6px;
    background: #333;
    border-radius: 3px;
    outline: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    background: var(--amber);
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid #c89000;
}

.crt-toggles {
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
    font-size: 11px;
    align-items: center;
}

.crt-toggles label {
    display: flex;
    align-items: center;
    gap: 4px;
    cursor: pointer;
    color: var(--text-dim);
}

.crt-toggles input[type="checkbox"] {
    accent-color: var(--green);
}

.palette-label select {
    background: var(--bg-deep);
    color: var(--text-main);
    border: 1px solid var(--border);
    padding: 2px 6px;
    font-size: 10px;
    font-family: 'JetBrains Mono', monospace;
    border-radius: 3px;
}

/* CONTROLLER DISPLAY */
#controller-display {
    display: flex;
    gap: 15px;
    margin-bottom: 15px;
}

.controller {
    flex: 1;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    text-align: center;
}

.controller h3 {
    font-family: 'Press Start 2P', monospace;
    font-size: 9px;
    color: var(--amber);
    margin-bottom: 12px;
}

.keys-hint {
    font-size: 9px;
    color: var(--text-dim);
    margin-top: 8px;
}

/* PADDLE */
.paddle-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.paddle-knob {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    background: radial-gradient(circle at 35% 35%, #666, #333);
    border: 3px solid #555;
    cursor: grab;
    position: relative;
    box-shadow: 0 4px 12px rgba(0,0,0,0.5);
    transition: box-shadow 0.2s;
}

.paddle-knob:hover {
    box-shadow: 0 4px 20px rgba(255,176,0,0.3);
}

.knob-marker {
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 4px;
    height: 14px;
    background: var(--amber);
    border-radius: 2px;
}

.paddle-value {
    font-size: 10px;
    color: var(--green);
    font-family: 'JetBrains Mono', monospace;
}

/* DPAD */
.dpad-container {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 20px;
}

.dpad {
    display: grid;
    grid-template-columns: 28px 28px 28px;
    grid-template-rows: 28px 28px 28px;
    gap: 2px;
}

.dpad-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    background: #444;
    color: #888;
    font-size: 12px;
    border-radius: 3px;
    border: 1px solid #555;
    user-select: none;
    transition: all 0.1s;
}

.dpad-btn.active {
    background: var(--green);
    color: #000;
    box-shadow: 0 0 10px rgba(0,255,65,0.5);
}

.dpad-btn.up { grid-column: 2; grid-row: 1; }
.dpad-btn.left { grid-column: 1; grid-row: 2; }
.dpad-center { grid-column: 2; grid-row: 2; background: #2a2a2a; border-radius: 3px; }
.dpad-btn.right { grid-column: 3; grid-row: 2; }
.dpad-btn.down { grid-column: 2; grid-row: 3; }

.action-btns {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.action-btn {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #cc2233;
    color: white;
    font-family: 'Press Start 2P', monospace;
    font-size: 10px;
    border: 2px solid #992233;
    cursor: pointer;
    transition: all 0.1s;
}

.action-btn.active {
    background: #ff4466;
    box-shadow: 0 0 15px rgba(255,0,64,0.5);
}

/* SAVE STATES */
#save-states {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 15px;
}

#save-states h3 {
    font-family: 'Press Start 2P', monospace;
    font-size: 9px;
    color: var(--amber);
    margin-bottom: 10px;
}

.state-slots {
    display: flex;
    gap: 6px;
    margin-bottom: 8px;
}

.state-btn {
    flex: 1;
    font-family: 'JetBrains Mono', monospace;
    font-size: 10px;
    padding: 6px;
    background: #1a1a2e;
    color: var(--text-dim);
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s;
}

.state-btn.selected {
    border-color: var(--amber);
    color: var(--amber);
    box-shadow: 0 0 8px rgba(255,176,0,0.2);
}

.state-btn.has-data {
    color: var(--green);
}

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

/* AUDIO */
#audio-section {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    margin-bottom: 15px;
}

#audio-section h3 {
    font-family: 'Press Start 2P', monospace;
    font-size: 9px;
    color: var(--amber);
    margin-bottom: 10px;
}

.oscilloscope-container {
    background: #050510;
    border: 2px solid #333;
    border-radius: 8px;
    padding: 4px;
    margin-bottom: 10px;
}

#oscilloscope {
    width: 100%;
    height: 80px;
    border-radius: 4px;
}

.tone-log {
    max-height: 100px;
    overflow-y: auto;
    background: #0a0a14;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 9px;
}

.tone-log-header {
    font-size: 9px;
    color: var(--text-dim);
    padding: 4px 8px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: #0a0a14;
}

.tone-log-entries {
    padding: 4px 8px;
}

.tone-entry {
    color: var(--cyan);
    margin-bottom: 2px;
}

/* DEBUG PANEL */
.panel-toggle {
    margin-bottom: 8px;
}

.debug-panel {
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px;
    overflow: hidden;
    transition: max-height 0.3s, opacity 0.3s;
}

.debug-panel.collapsed {
    max-height: 0;
    padding: 0 12px;
    opacity: 0;
    border: none;
}

.debug-panel h3 {
    font-family: 'Press Start 2P', monospace;
    font-size: 9px;
    color: var(--amber);
    margin-bottom: 12px;
}

.debug-section {
    margin-bottom: 12px;
}

.debug-section h4 {
    font-size: 10px;
    color: var(--cyan);
    margin-bottom: 6px;
    border-bottom: 1px solid var(--border);
    padding-bottom: 4px;
}

.register-view {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
}

.reg-row {
    display: flex;
    justify-content: space-between;
    padding: 2px 6px;
    background: rgba(0,0,0,0.3);
    border-radius: 2px;
    font-size: 10px;
}

.reg-name { color: var(--text-dim); }
.reg-val { color: var(--green); font-weight: bold; }

.disassembly {
    background: #0a0a14;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 6px;
    max-height: 200px;
    overflow-y: auto;
    font-size: 10px;
    font-family: 'JetBrains Mono', monospace;
}

.disasm-line {
    padding: 1px 4px;
    color: var(--text-dim);
    cursor: pointer;
    border-radius: 2px;
}

.disasm-line:hover { background: rgba(255,176,0,0.1); }
.disasm-line.current { background: rgba(0,255,65,0.15); color: var(--green); }
.disasm-line.breakpoint { background: rgba(255,0,64,0.2); color: var(--red); }
.disasm-line .addr { color: var(--amber); }
.disasm-line .opcode { color: var(--text-dim); }
.disasm-line .mnemonic { color: var(--cyan); }

.memory-view {
    background: #0a0a14;
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 6px;
    max-height: 150px;
    overflow-y: auto;
    font-size: 10px;
    font-family: 'JetBrains Mono', monospace;
    color: var(--green);
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 2px 8px;
}

.mem-addr { color: var(--amber); }
.mem-row { letter-spacing: 2px; }

.mem-cell {
    cursor: pointer;
    padding: 0 1px;
    border-radius: 1px;
    transition: background 0.2s;
}

.mem-cell:hover {
    background: rgba(255,176,0,0.3);
}

.debug-controls {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

/* STATUS BAR */
#status-bar {
    display: flex;
    gap: 20px;
    padding: 8px 14px;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 10px;
    font-family: 'JetBrains Mono', monospace;
    color: var(--text-dim);
    margin-top: 10px;
    cursor: default;
    flex-wrap: wrap;
}

.state-stopped { color: var(--red); }
.state-running { color: var(--green); }
.state-paused { color: var(--amber); }

/* FOOTER */
footer {
    text-align: center;
    padding: 20px;
    font-size: 11px;
    color: var(--text-dim);
    border-top: 1px solid var(--border);
    margin-top: 15px;
}

footer a {
    color: var(--amber);
    text-decoration: none;
    margin-left: 15px;
}

footer a:hover {
    text-decoration: underline;
    color: var(--green);
}

/* HELP */
.help-btn {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--amber);
    color: var(--bg-deep);
    font-family: 'Press Start 2P', monospace;
    font-size: 16px;
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(255,176,0,0.4);
    z-index: 100;
    transition: transform 0.2s;
}

.help-btn:hover { transform: scale(1.1); }

.modal {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.8);
    z-index: 200;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal.hidden { display: none; }

.modal-content {
    background: #f5ecd7;
    color: #2a2520;
    border-radius: 12px;
    padding: 30px;
    max-width: 600px;
    width: 100%;
    max-height: 80vh;
    overflow-y: auto;
    font-family: 'JetBrains Mono', monospace;
    font-size: 12px;
    line-height: 1.7;
    position: relative;
    box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

.modal-content h2 {
    font-family: 'Press Start 2P', monospace;
    font-size: 14px;
    color: #cc2233;
    margin-bottom: 16px;
}

.modal-content h3 {
    font-family: 'Press Start 2P', monospace;
    font-size: 10px;
    color: #886611;
    margin: 16px 0 8px;
}

.modal-content ul {
    padding-left: 20px;
    margin-bottom: 10px;
}

.modal-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 10px 0;
}

.modal-content td {
    padding: 4px 8px;
    border: 1px solid #ccc;
    font-size: 11px;
}

.close-btn {
    position: absolute;
    top: 10px; right: 14px;
    font-size: 24px;
    background: none;
    border: none;
    cursor: pointer;
    color: #888;
}

.close-btn:hover { color: #333; }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: rgba(0,0,0,0.2); }
::-webkit-scrollbar-thumb { background: #444; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #555; }

/* RESPONSIVE */
@media (max-width: 900px) {
    #main-content {
        flex-direction: column;
    }
    #right-panel {
        width: 100%;
    }
    #controller-display {
        flex-direction: column;
    }
    .register-view {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 500px) {
    .emu-controls {
        justify-content: center;
    }
    .bezel-logo {
        font-size: 7px !important;
        letter-spacing: 1px;
    }
}