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

:root {
    --bg-dark: #07070a;
    --bg-panel: #12121a;
    --border: #232333;
    
    --text-main: #f0f0f5;
    --text-dim: #808099;
    
    /* Default Green Theme */
    --neon: #00ff66;
    --neon-dim: rgba(0, 255, 102, 0.15);
    
    --food: #ffcc00;
    --danger: #ff3366;
    
    --font: 'JetBrains Mono', monospace;
}

body.theme-blue { --neon: #00ccff; --neon-dim: rgba(0, 204, 255, 0.15); }
body.theme-orange { --neon: #ff8800; --neon-dim: rgba(255, 136, 0, 0.15); }
body.theme-pink { --neon: #ff00aa; --neon-dim: rgba(255, 0, 170, 0.15); }

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

body {
    background-color: var(--bg-dark);
    color: var(--text-main);
    font-family: var(--font);
    overflow: hidden; /* Prevent pull-to-refresh on mobile */
    overscroll-behavior: none;
    -webkit-user-select: none;
    user-select: none;
    touch-action: none; /* Disable double-tap zoom */
}

.game-app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh; /* Mobile browser bottom bar fix */
    max-width: 1200px;
    margin: 0 auto;
}

/* --- Top HUD (Mobile First) --- */
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 16px;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.topbar__stat { display: flex; flex-direction: column; }
.topbar__label { font-size: 10px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 1px; }
.topbar__val { font-size: 20px; font-weight: 800; color: var(--text-main); }
.topbar__val--hi { color: var(--food); }

.topbar__center { text-align: center; display: flex; flex-direction: column; align-items: center; }
.topbar__title { font-size: 16px; font-weight: 800; letter-spacing: 2px; color: var(--neon); text-shadow: 0 0 10px var(--neon-dim); }
.topbar__lvl { font-size: 10px; color: var(--text-dim); background: rgba(255,255,255,0.05); padding: 2px 6px; border-radius: 4px; margin-top: 2px; }

/* --- Main Area --- */
.main-area {
    display: flex;
    flex: 1;
    overflow: hidden;
    padding: 10px;
    gap: 16px;
    justify-content: center;
}

/* Hide sidebars on mobile */
.sidebar { display: none; width: 220px; flex-direction: column; gap: 16px; }

/* --- Canvas Area --- */
.canvas-col {
    display: flex;
    flex-direction: column;
    flex: 1;
    max-width: 600px;
    gap: 16px;
}

.canvas-frame {
    position: relative;
    width: 100%;
    aspect-ratio: 1/1; /* Keep square */
    background: #000;
    border: 2px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 10px 30px rgba(0,0,0,0.5);
    flex-shrink: 0;
}

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

/* --- Overlays --- */
.ov {
    position: absolute;
    inset: 0;
    background: rgba(7, 7, 10, 0.85);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
}
.ov--visible { opacity: 1; pointer-events: auto; }

.ov__box { text-align: center; padding: 20px; display: flex; flex-direction: column; gap: 12px; align-items: center; }
.ov__icon { font-size: 40px; margin-bottom: -10px; }
.ov__title { font-size: 32px; font-weight: 800; color: var(--neon); letter-spacing: 2px; }
.ov__title--sm { font-size: 24px; color: var(--text-main); }
.ov__title--red { color: var(--danger); }
.ov__desc { font-size: 12px; color: var(--text-dim); line-height: 1.5; }

.ov__keys {
    display: none; /* Hide on mobile */
    font-size: 11px; color: var(--text-dim); flex-direction: column; gap: 6px; margin: 10px 0;
}
.ov__keys kbd {
    background: rgba(255,255,255,0.1); border: 1px solid var(--border); border-radius: 4px; padding: 2px 6px; color: #fff;
}

.ov__score { font-size: 16px; color: var(--text-main); }
.ov__score strong { font-size: 24px; color: var(--food); }

/* Highscore input */
.ov__hs { display: none; background: rgba(255, 204, 0, 0.05); border: 1px solid rgba(255, 204, 0, 0.2); border-radius: 8px; padding: 12px; margin-top: 10px; }
.ov__hs--show { display: block; }
.ov__hs-label { font-size: 10px; color: var(--food); margin-bottom: 8px; }
.ov__hs-row { display: flex; gap: 8px; }
.ov__hs-input {
    flex: 1; background: #000; border: 1px solid var(--border); border-radius: 4px; color: #fff; font-family: var(--font); font-size: 14px; text-align: center; text-transform: uppercase; padding: 8px; width: 120px;
}
.ov__hs-input:focus { outline: none; border-color: var(--food); }

/* --- Buttons --- */
.btn {
    font-family: var(--font);
    font-size: 16px; font-weight: 700;
    background: var(--text-main); color: #000;
    border: none; border-radius: 8px;
    padding: 12px 32px;
    cursor: pointer;
    transition: transform 0.1s, background 0.2s;
    -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: scale(0.95); }
.btn--sm { font-size: 12px; padding: 8px 16px; background: var(--food); }

/* --- Mobile D-Pad (Zero-delay setup) --- */
.controls {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding-bottom: 20px;
}

.dpad {
    display: grid;
    grid-template-areas: 
        ".    up    ."
        "left mid   right"
        ".    down  .";
    gap: 12px;
    /* Big enough for thumbs */
    grid-template-columns: repeat(3, 70px);
    grid-template-rows: repeat(3, 70px);
}

.dpad__b {
    background: linear-gradient(145deg, #1f1f2e, #12121a);
    border: none;
    border-radius: 12px;
    box-shadow: 
        inset 1px 1px 2px rgba(255,255,255,0.15),
        inset -1px -1px 4px rgba(0,0,0,0.8),
        0 6px 0 #050508,
        0 10px 15px rgba(0,0,0,0.9);
    position: relative;
    top: 0;
    color: var(--text-dim);
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: all 0.05s ease;
    outline: none;
}
/* Visual feedback only, logic handled instantly in JS */
.dpad__b:active, .dpad__b.active-visual {
    background: linear-gradient(145deg, #12121a, #1a1a26);
    box-shadow: 
        inset 2px 2px 8px rgba(0,0,0,0.9),
        0 1px 0 #050508,
        0 3px 6px rgba(0,0,0,0.8);
    top: 5px;
    color: var(--neon);
}

.dpad__b--u { grid-area: up; border-radius: 16px 16px 8px 8px; }
.dpad__b--d { grid-area: down; border-radius: 8px 8px 16px 16px; }
.dpad__b--l { grid-area: left; border-radius: 16px 8px 8px 16px; }
.dpad__b--r { grid-area: right; border-radius: 8px 16px 16px 8px; }
.dpad__b--pause { grid-area: mid; border-radius: 50%; border-color: transparent; background: transparent; }

/* --- Desktop Specific --- */
@media (min-width: 850px) {
    .main-area { padding: 20px; gap: 30px; }
    .sidebar { display: flex; }
    .controls { display: none; } /* Hide D-Pad on desktop */
    .ov__keys { display: flex; }
    
    .sb-section { background: var(--bg-panel); border: 1px solid var(--border); border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
    .sb-head { font-size: 11px; font-weight: 800; color: var(--text-dim); letter-spacing: 1px; border-bottom: 1px solid var(--border); padding-bottom: 8px; }
    
    .sb-stats { display: flex; flex-direction: column; gap: 8px; }
    .sb-stat { display: flex; justify-content: space-between; font-size: 12px; }
    .sb-stat__k { color: var(--text-dim); }
    .sb-stat__v { color: var(--neon); font-weight: 700; }
    
    .sb-console-section { flex: 1; }
    .sb-console { flex: 1; overflow-y: auto; font-size: 10px; color: var(--text-dim); display: flex; flex-direction: column; gap: 4px; line-height: 1.4; max-height: 250px; }
    .log-hi { color: var(--neon); }
    .log-err { color: var(--danger); }
    
    .lb-table { width: 100%; font-size: 12px; border-collapse: collapse; }
    .lb-table th { color: var(--text-dim); text-align: left; padding-bottom: 8px; font-weight: 400; font-size: 10px;}
    .lb-table td { padding: 4px 0; border-bottom: 1px solid rgba(255,255,255,0.05); }
    .lb-table td:last-child { text-align: right; color: var(--food); font-weight: 700; }
    
    .cfg-row { display: flex; justify-content: space-between; align-items: center; font-size: 12px; color: var(--text-main); }
    .cfg-sel { background: #000; border: 1px solid var(--border); color: #fff; font-family: var(--font); padding: 4px 8px; border-radius: 4px; font-size: 11px; outline: none; }
    
    .cfg-toggle { display: flex; justify-content: space-between; align-items: center; font-size: 12px; cursor: pointer; }
    .cfg-toggle input { display: none; }
    .cfg-sw { width: 32px; height: 18px; background: #000; border: 1px solid var(--border); border-radius: 10px; position: relative; transition: 0.2s; }
    .cfg-sw::after { content: ''; position: absolute; left: 2px; top: 2px; width: 12px; height: 12px; background: var(--text-dim); border-radius: 50%; transition: 0.2s; }
    .cfg-toggle input:checked + .cfg-sw { background: var(--neon-dim); border-color: var(--neon); }
    .cfg-toggle input:checked + .cfg-sw::after { transform: translateX(14px); background: var(--neon); }
}

/* Fix for tall mobile screens */
@media (min-height: 800px) and (max-width: 849px) {
    .dpad { grid-template-columns: repeat(3, 80px); grid-template-rows: repeat(3, 80px); }
    .canvas-col { max-width: 100%; padding: 0 10px; }
}

/* --- CRT Effects & Boot Screen --- */
.crt-overlay {
    position: fixed;
    top: 0; left: 0; width: 100vw; height: 100vh;
    background: linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.25) 50%), linear-gradient(90deg, rgba(255, 0, 0, 0.06), rgba(0, 255, 0, 0.02), rgba(0, 0, 255, 0.06));
    background-size: 100% 2px, 3px 100%;
    pointer-events: none;
    z-index: 9998;
    opacity: 0.6;
}
@keyframes flicker {
  0% { opacity: 0.95; }
  5% { opacity: 0.85; }
  10% { opacity: 0.95; }
  15% { opacity: 1; }
  100% { opacity: 1; }
}
body {
    animation: flicker 0.15s infinite;
}

#boot-screen {
    position: fixed;
    inset: 0;
    background: #000;
    color: #00ff66;
    font-family: 'JetBrains Mono', monospace;
    z-index: 10000;
    padding: 20px;
    display: flex;
    flex-direction: column;
    font-size: 16px;
    text-shadow: 0 0 8px #00ff66;
    pointer-events: auto;
}
#boot-screen.hidden {
    display: none;
}
.boot-line {
    margin-bottom: 6px;
}
.cursor {
    display: inline-block;
    width: 10px;
    height: 16px;
    background: #00ff66;
    animation: blink 1s step-end infinite;
    vertical-align: text-bottom;
}
@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}
