:root {
    --bg: #0e0e1a;
    --bg2: #161625;
    --fg: #e0e0f0;
    --fg2: #8888aa;
    --accent: #EE1515;
    --accent2: #FFCB05;
    --accent3: #3B4CCA;
    --shell-green: #5cb85c;
    --shell-green-dark: #3a7a3a;
    --shell-green-light: #8cd98c;
    --shell-purple: #9b59b6;
    --shell-purple-dark: #6a3080;
    --shell-purple-light: #c49dd8;
    --shell-blue: #3498db;
    --shell-blue-dark: #1a6baa;
    --shell-blue-light: #7ec0ee;
    --lcd-bg-classic: #9bbc0f;
    --lcd-fg-classic: #0f380f;
    --lcd-bg-white: #d8dcd0;
    --lcd-fg-white: #1a1a1a;
    --lcd-bg-amber: #f0c060;
    --lcd-fg-amber: #402000;
    --lcd-bg-blue: #90b0d0;
    --lcd-fg-blue: #0a1530;
    --radius: 12px;
    --font-display: 'Bricolage Grotesque', sans-serif;
    --font-mono: 'IBM Plex Mono', monospace;
}

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

body {
    background: var(--bg);
    color: var(--fg);
    font-family: var(--font-display);
    min-height: 100vh;
    overflow-x: hidden;
}

body::before {
    content: '';
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: 
        radial-gradient(ellipse at 20% 50%, rgba(92,184,92,0.06) 0%, transparent 60%),
        radial-gradient(ellipse at 80% 30%, rgba(52,152,219,0.06) 0%, transparent 60%),
        radial-gradient(ellipse at 50% 80%, rgba(155,89,182,0.04) 0%, transparent 50%);
    pointer-events: none;
    z-index: 0;
}

#app {
    position: relative;
    z-index: 1;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

/* Toolbar */
#toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    background: var(--bg2);
    border-bottom: 1px solid rgba(255,255,255,0.06);
    gap: 8px;
    flex-wrap: wrap;
}

.toolbar-left, .toolbar-center, .toolbar-right {
    display: flex;
    align-items: center;
    gap: 6px;
}

.logo {
    display: flex;
    align-items: center;
    gap: 6px;
    font-weight: 800;
    font-size: 1.1rem;
    letter-spacing: -0.5px;
}

.logo-icon {
    color: var(--accent);
    font-size: 1.3rem;
}

.logo-web {
    color: var(--accent2);
    font-weight: 300;
}

.tb-btn {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    padding: 5px 10px;
    border: 1px solid rgba(255,255,255,0.12);
    background: rgba(255,255,255,0.04);
    color: var(--fg);
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
}

.tb-btn:hover { background: rgba(255,255,255,0.1); }
.tb-btn:active { transform: scale(0.95); }
.tb-btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.tb-btn.primary:hover { background: #ff2020; }
.tb-btn.active { background: var(--accent3); border-color: var(--accent3); }

.tb-select {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    padding: 4px 6px;
    background: rgba(255,255,255,0.06);
    border: 1px solid rgba(255,255,255,0.12);
    color: var(--fg);
    border-radius: 6px;
    cursor: pointer;
}

/* Main */
#main-area {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    gap: 16px;
    padding: 20px 16px;
    flex-wrap: wrap;
}

/* Console Shell */
#console-wrapper {
    display: flex;
    justify-content: center;
}

#console-shell {
    position: relative;
    width: 380px;
    padding: 20px 20px 16px;
    border-radius: 40px 40px 30px 30px;
    background: linear-gradient(160deg, var(--shell-green-light) 0%, var(--shell-green) 40%, var(--shell-green-dark) 100%);
    box-shadow: 
        0 8px 32px rgba(0,0,0,0.5),
        inset 0 2px 0 rgba(255,255,255,0.2),
        inset 0 -2px 0 rgba(0,0,0,0.15);
    transition: background 0.4s;
}

#console-shell.shell-green {
    background: linear-gradient(160deg, var(--shell-green-light) 0%, var(--shell-green) 40%, var(--shell-green-dark) 100%);
}
#console-shell.shell-purple {
    background: linear-gradient(160deg, var(--shell-purple-light) 0%, var(--shell-purple) 40%, var(--shell-purple-dark) 100%);
}
#console-shell.shell-blue {
    background: linear-gradient(160deg, var(--shell-blue-light) 0%, var(--shell-blue) 40%, var(--shell-blue-dark) 100%);
}

.shell-top {
    text-align: center;
    margin-bottom: 10px;
}

.shell-label {
    font-family: var(--font-display);
    font-weight: 800;
    font-size: 0.85rem;
    color: rgba(0,0,0,0.45);
    letter-spacing: 2px;
    text-transform: uppercase;
}

#screen-bezel {
    background: #222;
    border-radius: 8px;
    padding: 8px;
    box-shadow: inset 0 2px 8px rgba(0,0,0,0.6);
}

#screen-frame {
    position: relative;
    border-radius: 4px;
    overflow: hidden;
    background: var(--lcd-bg-classic);
    line-height: 0;
}

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

#lcd-overlay {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    pointer-events: none;
    background: repeating-linear-gradient(
        0deg,
        transparent,
        transparent 3px,
        rgba(0,0,0,0.06) 3px,
        rgba(0,0,0,0.06) 4px
    );
    mix-blend-mode: multiply;
}

#lcd-overlay.hidden { display: none; }

#drop-zone {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0,0,0,0.5);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
    border-radius: 4px;
}

#drop-zone.active {
    opacity: 1;
    pointer-events: auto;
    cursor: pointer;
}

#drop-zone.dragover {
    opacity: 1;
    pointer-events: auto;
    background: rgba(238,21,21,0.3);
}

.drop-content {
    text-align: center;
    color: #fff;
}

.cartridge-icon {
    font-size: 2rem;
    margin-bottom: 4px;
    animation: cartBounce 2s ease-in-out infinite;
}

@keyframes cartBounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-5px); }
}

.drop-text {
    font-family: var(--font-mono);
    font-size: 0.85rem;
    font-weight: 600;
}

.drop-sub {
    font-family: var(--font-mono);
    font-size: 0.65rem;
    opacity: 0.6;
    margin-top: 2px;
}

#rom-info {
    text-align: center;
    font-family: var(--font-mono);
    font-size: 0.65rem;
    color: rgba(0,0,0,0.5);
    margin: 6px 0 2px;
    min-height: 1em;
}

/* Controls Panel */
#controls-panel {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 4px 4px;
    gap: 8px;
}

#dpad {
    display: grid;
    grid-template-columns: 32px 32px 32px;
    grid-template-rows: 32px 32px 32px;
    gap: 2px;
}

.dpad-btn {
    background: rgba(0,0,0,0.35);
    color: rgba(255,255,255,0.8);
    border: none;
    border-radius: 4px;
    font-size: 0.7rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.1s;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

.dpad-up { grid-column: 2; grid-row: 1; border-radius: 4px 4px 0 0; }
.dpad-left { grid-column: 1; grid-row: 2; border-radius: 4px 0 0 4px; }
.dpad-center { grid-column: 2; grid-row: 2; background: rgba(0,0,0,0.45); border-radius: 0; }
.dpad-right { grid-column: 3; grid-row: 2; border-radius: 0 4px 4px 0; }
.dpad-down { grid-column: 2; grid-row: 3; border-radius: 0 0 4px 4px; }

.dpad-btn:active, .dpad-btn.pressed {
    background: rgba(0,0,0,0.6);
    transform: scale(0.92);
}

.controls-middle {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.ctrl-btn {
    font-family: var(--font-mono);
    font-size: 0.55rem;
    font-weight: 600;
    padding: 4px 10px;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    background: rgba(0,0,0,0.3);
    color: rgba(255,255,255,0.7);
    transition: all 0.1s;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

.ctrl-btn:active, .ctrl-btn.pressed {
    transform: scale(0.92);
    background: rgba(0,0,0,0.5);
}

.controls-right {
    display: flex;
    flex-direction: column;
    gap: 6px;
    align-items: center;
}

.action-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: none;
    font-family: var(--font-display);
    font-weight: 800;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.1s;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    box-shadow: 0 3px 0 rgba(0,0,0,0.3);
}

.action-btn:active, .action-btn.pressed {
    transform: translateY(2px) scale(0.95);
    box-shadow: 0 1px 0 rgba(0,0,0,0.3);
}

.btn-a { background: var(--accent); color: #fff; }
.btn-b { background: var(--accent3); color: #fff; }
.btn-c { background: var(--accent2); color: #333; width: 32px; height: 32px; font-size: 0.65rem; }

.shell-bottom {
    display: flex;
    justify-content: center;
    margin-top: 8px;
}

.shell-color-dots {
    display: flex;
    gap: 8px;
}

.dot {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    cursor: pointer;
    border: 2px solid rgba(0,0,0,0.2);
    transition: all 0.2s;
}

.dot:hover { transform: scale(1.15); }
.dot.active { border-color: #fff; box-shadow: 0 0 6px rgba(255,255,255,0.4); }

.dot-green { background: var(--shell-green); }
.dot-purple { background: var(--shell-purple); }
.dot-blue { background: var(--shell-blue); }

/* Debug Panel */
#debug-panel {
    width: 360px;
    max-height: 600px;
    background: var(--bg2);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: var(--radius);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

#debug-panel.hidden { display: none; }

.debug-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    background: rgba(255,255,255,0.04);
    border-bottom: 1px solid rgba(255,255,255,0.06);
    font-family: var(--font-mono);
    font-size: 0.8rem;
    font-weight: 600;
}

.debug-header button {
    background: none;
    border: none;
    color: var(--fg2);
    cursor: pointer;
    font-size: 1rem;
}

.debug-body {
    overflow-y: auto;
    padding: 8px 12px;
    flex: 1;
}

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

.debug-section h3 {
    font-family: var(--font-mono);
    font-size: 0.65rem;
    color: var(--accent2);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 4px;
}

.debug-section pre {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--fg2);
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-all;
}

.debug-controls {
    display: flex;
    gap: 6px;
    padding-top: 8px;
    border-top: 1px solid rgba(255,255,255,0.06);
    align-items: center;
}

#debug-fps {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--accent2);
    margin-left: auto;
}

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

.modal.hidden { display: none; }

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

.modal-content {
    position: relative;
    background: var(--bg2);
    border: 1px solid rgba(255,255,255,0.1);
    border-radius: var(--radius);
    width: 90%;
    max-width: 440px;
    max-height: 80vh;
    overflow-y: auto;
}

.modal-small { max-width: 340px; }

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid rgba(255,255,255,0.06);
}

.modal-header h2 {
    font-family: var(--font-display);
    font-size: 1.1rem;
    font-weight: 800;
}

.modal-header button {
    background: none;
    border: none;
    color: var(--fg2);
    cursor: pointer;
    font-size: 1.2rem;
}

.modal-body {
    padding: 16px;
}

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

.setting-group h3 {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--accent2);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 8px;
}

.setting-group label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.8rem;
    margin-bottom: 6px;
    color: var(--fg2);
}

.setting-group select, .setting-group input[type="range"] {
    flex: 1;
    max-width: 180px;
}

.setting-group select {
    background: rgba(255,255,255,0.06);
    border: 1px solid rgba(255,255,255,0.12);
    color: var(--fg);
    padding: 4px 8px;
    border-radius: 6px;
    font-family: var(--font-mono);
    font-size: 0.75rem;
}

.checkbox-label input { width: auto; flex: none; }

.shell-picker {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.shell-opt {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    padding: 6px 12px;
    border: 2px solid transparent;
    border-radius: 8px;
    cursor: pointer;
    color: #fff;
    transition: all 0.15s;
}

.shell-opt:hover { transform: scale(1.05); }
.shell-opt-green { background: var(--shell-green); }
.shell-opt-purple { background: var(--shell-purple); }
.shell-opt-blue { background: var(--shell-blue); }

.key-help {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--fg2);
    line-height: 1.8;
}

/* Save state slots */
.save-slot {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    margin-bottom: 6px;
    background: rgba(255,255,255,0.04);
    border-radius: 8px;
    border: 1px solid rgba(255,255,255,0.06);
}

.save-slot-info {
    font-family: var(--font-mono);
    font-size: 0.7rem;
}

.save-slot-info .slot-name {
    color: var(--fg);
    font-weight: 600;
}

.save-slot-info .slot-detail {
    color: var(--fg2);
    font-size: 0.6rem;
}

.save-slot button {
    font-family: var(--font-mono);
    font-size: 0.65rem;
    padding: 4px 8px;
    border: 1px solid rgba(255,255,255,0.12);
    background: rgba(255,255,255,0.06);
    color: var(--fg);
    border-radius: 4px;
    cursor: pointer;
    margin-left: 4px;
}

.save-slot button:hover { background: rgba(255,255,255,0.12); }

/* Footer */
#footer {
    text-align: center;
    padding: 16px;
    font-family: var(--font-mono);
    font-size: 0.65rem;
    color: var(--fg2);
    border-top: 1px solid rgba(255,255,255,0.04);
}

#footer a {
    color: var(--accent2);
    text-decoration: none;
    margin-left: 12px;
}

#footer a:hover { text-decoration: underline; }

/* Responsive */
@media (max-width: 800px) {
    #main-area {
        flex-direction: column;
        align-items: center;
    }
    #debug-panel {
        width: 100%;
        max-width: 380px;
    }
    #console-shell {
        width: 340px;
    }
    .toolbar-left .logo-text {
        font-size: 0.9rem;
    }
}

@media (max-width: 400px) {
    #console-shell {
        width: 300px;
        padding: 14px 14px 10px;
    }
    #toolbar {
        padding: 6px 8px;
    }
    .tb-btn { padding: 4px 6px; font-size: 0.65rem; }
}

/* Cartridge animation */
@keyframes cartridgeInsert {
    0% { transform: translateY(-30px); opacity: 0; }
    60% { transform: translateY(4px); opacity: 1; }
    100% { transform: translateY(0); opacity: 1; }
}

.cart-inserting #rom-info {
    animation: cartridgeInsert 0.5s ease-out;
}