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

:root {
    --bg: #0a0a1a;
    --panel: #12122a;
    --border: #2a2a5a;
    --text: #e0e0ff;
    --accent: #ffcc33;
    --accent-dim: #aa8822;
    --red: #ee4444;
    --green: #44dd66;
    --blue: #4488ff;
    --font: 'Press Start 2P', monospace;
}

body {
    background: var(--bg);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    overflow: hidden;
    font-family: var(--font);
    color: var(--text);
    image-rendering: pixelated;
    user-select: none;
    -webkit-user-select: none;
    overscroll-behavior: none;
}

#game-container {
    position: relative;
    width: 800px;
    max-width: 100vw;
    aspect-ratio: 8 / 5;
}

#game-canvas {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    border: 3px solid var(--border);
    border-radius: 4px;
    background: #0d0d20;
    touch-action: manipulation;
}

.hidden {
    display: none !important;
}

/* ---- HUD ---- */
#hud {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 8px 12px;
    pointer-events: none;
    z-index: 5;
}

.hud-side {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 120px;
}

#hud-p2 {
    text-align: right;
    align-items: flex-end;
}

.player-label {
    font-size: 8px;
    color: var(--accent-dim);
    letter-spacing: 2px;
}

.hearts {
    font-size: 12px;
    letter-spacing: 2px;
}

.heart-full {
    color: var(--red);
}

.heart-empty {
    color: #333;
}

.score-row {
    display: flex;
    gap: 6px;
    align-items: baseline;
}

#hud-p2 .score-row {
    flex-direction: row-reverse;
}

.score-label {
    font-size: 6px;
    color: #666;
}

.score-value {
    font-size: 14px;
    color: var(--accent);
}

.streak-row {
    font-size: 7px;
    color: var(--green);
    min-height: 12px;
}

#hud-center {
    display: flex;
    align-items: center;
    padding-top: 4px;
}

#bpm-display {
    font-size: 9px;
    color: #888;
    background: rgba(0, 0, 0, 0.5);
    padding: 3px 8px;
    border-radius: 3px;
}

/* ---- Screens ---- */
#menu-screen,
#lobby-screen,
#results-screen {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: rgba(6, 6, 18, 0.92);
    z-index: 10;
}

.title {
    font-size: 36px;
    color: var(--accent);
    text-shadow: 0 0 20px rgba(255, 204, 50, 0.4), 3px 3px 0 #332200;
    margin-bottom: 4px;
    letter-spacing: 6px;
}

.subtitle {
    font-size: 9px;
    color: #777;
    margin-bottom: 36px;
    letter-spacing: 3px;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 28px;
}

.btn {
    font-family: var(--font);
    font-size: 12px;
    padding: 12px 32px;
    background: var(--panel);
    color: var(--text);
    border: 2px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    letter-spacing: 2px;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
    min-width: 220px;
    text-align: center;
}

.btn:hover {
    background: #1a1a40;
    border-color: var(--accent);
    color: var(--accent);
}

.btn:active {
    background: #222255;
    transform: translateY(1px);
}

.btn-small {
    font-size: 9px;
    padding: 8px 20px;
    min-width: 120px;
}

.controls-hint {
    font-size: 8px;
    color: #555;
    text-align: center;
    line-height: 2;
}

.controls-hint kbd {
    background: #1a1a30;
    border: 1px solid #333;
    padding: 2px 6px;
    border-radius: 3px;
    color: #999;
}

/* ---- Lobby ---- */
.lobby-title {
    font-size: 16px;
    color: var(--accent);
    margin-bottom: 24px;
    letter-spacing: 3px;
}

.lobby-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    margin: 8px 0;
}

.lobby-divider {
    font-size: 10px;
    color: #444;
    margin: 12px 0;
}

.code-input {
    font-family: var(--font);
    font-size: 18px;
    width: 180px;
    padding: 10px 12px;
    background: var(--panel);
    border: 2px solid var(--border);
    border-radius: 4px;
    color: var(--accent);
    text-align: center;
    letter-spacing: 6px;
    text-transform: uppercase;
}

.code-input::placeholder {
    font-size: 9px;
    color: #444;
    letter-spacing: 2px;
}

.code-input:focus {
    outline: none;
    border-color: var(--accent);
}

.room-label {
    font-size: 8px;
    color: #666;
    letter-spacing: 2px;
}

.room-code {
    font-size: 28px;
    color: var(--accent);
    letter-spacing: 8px;
    padding: 8px 0;
    user-select: all;
}

.waiting-text {
    font-size: 8px;
    color: #666;
    animation: blink 1.2s infinite;
}

#lobby-status {
    font-size: 8px;
    color: var(--red);
    margin: 8px 0;
    min-height: 16px;
}

#btn-lobby-back {
    margin-top: 16px;
}

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

/* ---- Results ---- */
.results-title {
    font-size: 20px;
    color: var(--accent);
    margin-bottom: 20px;
    letter-spacing: 3px;
}

#results-body {
    display: flex;
    gap: 40px;
    margin-bottom: 24px;
    font-size: 10px;
    line-height: 2.2;
}

.result-col {
    text-align: center;
    min-width: 140px;
}

.result-col h3 {
    font-size: 11px;
    color: var(--accent-dim);
    margin-bottom: 8px;
}

.result-col .final-score {
    font-size: 22px;
    color: var(--accent);
}

.result-col .winner-badge {
    font-size: 9px;
    color: var(--green);
    margin-top: 4px;
}

.result-col .stat {
    color: #888;
}

/* ---- Responsive ---- */
@media (max-width: 820px) {
    #game-container {
        width: 100vw;
        border-radius: 0;
    }
    #game-canvas {
        border: none;
        border-radius: 0;
    }
    .title { font-size: 24px; }
    .subtitle { font-size: 7px; margin-bottom: 24px; }
    .btn { font-size: 10px; padding: 10px 20px; min-width: 180px; }
    .controls-hint { font-size: 7px; }
    .player-label { font-size: 7px; }
    .hearts { font-size: 10px; }
    .score-value { font-size: 12px; }
    .streak-row { font-size: 6px; }
    #bpm-display { font-size: 7px; }
    .results-title { font-size: 16px; }
    #results-body { gap: 20px; font-size: 9px; }
    .result-col .final-score { font-size: 18px; }
}

@media (max-width: 420px) {
    .title { font-size: 18px; letter-spacing: 4px; }
    .subtitle { font-size: 6px; margin-bottom: 18px; }
    .btn { font-size: 9px; padding: 8px 16px; min-width: 150px; }
    .menu-buttons { gap: 8px; margin-bottom: 18px; }
    #hud { padding: 4px 6px; }
    .hud-side { min-width: 80px; }
    .score-value { font-size: 10px; }
    .hearts { font-size: 9px; }
    .lobby-title { font-size: 13px; }
    .code-input { font-size: 14px; width: 150px; padding: 8px 10px; }
    .room-code { font-size: 22px; }
    #results-body { gap: 12px; font-size: 8px; }
    .result-col { min-width: 100px; }
}

@media (max-height: 500px) {
    .title { font-size: 16px; margin-bottom: 2px; }
    .subtitle { font-size: 6px; margin-bottom: 10px; }
    .menu-buttons { gap: 6px; margin-bottom: 10px; }
    .btn { padding: 6px 16px; }
    #hud { padding: 3px 6px; }
    .hearts { font-size: 9px; }
    .score-value { font-size: 10px; }
    .results-title { font-size: 14px; margin-bottom: 10px; }
    #results-body { margin-bottom: 12px; }
}

/* ---- Debug mode ---- */
.btn-debug-menu {
    background: rgba(140, 30, 30, 0.6);
    border-color: #993333;
    color: #ff9999;
}

.btn-debug-menu:hover {
    background: rgba(180, 40, 40, 0.8);
    border-color: #cc4444;
    color: #ffcccc;
}

#debug-hud {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 170px;
    z-index: 15;
    background: rgba(8, 8, 20, 0.92);
    border-left: 2px solid #993333;
    padding: 10px 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: auto;
}

.debug-hud-title {
    font-size: 9px;
    color: #cc4444;
    letter-spacing: 3px;
    text-align: center;
    padding-bottom: 4px;
    border-bottom: 1px solid #442222;
}

.debug-row {
    display: flex;
    align-items: center;
    gap: 6px;
}

.debug-row label {
    font-size: 7px;
    color: #999;
    min-width: 46px;
    letter-spacing: 1px;
}

.debug-row input[type="range"] {
    flex: 1;
    accent-color: var(--accent);
    height: 12px;
}

.debug-row span {
    font-size: 8px;
    color: var(--accent);
    min-width: 24px;
    text-align: right;
}

.btn-debug {
    font-family: var(--font);
    font-size: 7px;
    padding: 5px 8px;
    background: #1a1a30;
    color: #ccc;
    border: 1px solid #444;
    border-radius: 3px;
    cursor: pointer;
    min-width: auto;
    letter-spacing: 1px;
}

.btn-debug:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.btn-debug-quit {
    border-color: #993333;
    color: #cc6666;
}

.btn-debug-quit:hover {
    border-color: #cc4444;
    color: #ff8888;
}

.debug-cfg-title {
    font-size: 7px;
    color: #996644;
    letter-spacing: 2px;
    text-align: center;
    margin-top: 4px;
    padding-top: 6px;
    border-top: 1px solid #442222;
}

.debug-cfg-scroll {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 5px;
    min-height: 0;
    max-height: 200px;
    padding-right: 2px;
}

.debug-cfg-scroll::-webkit-scrollbar {
    width: 4px;
}

.debug-cfg-scroll::-webkit-scrollbar-thumb {
    background: #444;
    border-radius: 2px;
}

.debug-cfg-row {
    display: flex;
    align-items: center;
    gap: 4px;
}

.debug-cfg-row label {
    font-size: 6px;
    color: #888;
    min-width: 62px;
    letter-spacing: 0.5px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
}

.debug-cfg-row input {
    font-family: var(--font);
    font-size: 7px;
    width: 56px;
    padding: 3px 4px;
    background: #111122;
    border: 1px solid #444;
    border-radius: 2px;
    color: var(--accent);
    text-align: right;
}

.debug-cfg-row input:focus {
    outline: none;
    border-color: var(--accent);
}

.debug-btns {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding-top: 8px;
    border-top: 1px solid #442222;
}

/* Touch device hints */
@media (pointer: coarse) {
    .controls-hint-keyboard { display: none; }
    .controls-hint-touch { display: block; }
}
@media (pointer: fine) {
    .controls-hint-touch { display: none; }
}
