/* ==================== VARIABLES ==================== */
:root {
    --bg-dark: #0a0a12;
    --bg-panel: rgba(15, 15, 30, 0.92);
    --text-primary: #e8e0d0;
    --text-secondary: #8a8a9a;
    --accent-gold: #f0c040;
    --accent-red: #cc2222;
    --border-color: rgba(255,255,255,0.12);
    --focus-ring-thick: 0 0 0 4px #f0c040, 0 0 0 7px #000;
    --font-title: 'Creepster', cursive;
    --font-ui: 'Press Start 2P', monospace;
    --touch-min: 48px;
    --mobile-btn-size: 64px;
}

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

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

body {
    background: var(--bg-dark);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    min-height: 100dvh;
    font-family: var(--font-ui);
    overflow: hidden;
    color: var(--text-primary);
    -webkit-tap-highlight-color: transparent;
}

/* ==================== SKIP LINK ==================== */
.skip-link {
    position: absolute;
    top: -100px; left: 10px;
    background: var(--accent-gold); color: #000;
    padding: 12px 24px; border-radius: 0 0 8px 8px;
    font-size: 14px; font-weight: bold; z-index: 1000;
    transition: top 0.2s; text-decoration: none;
    box-shadow: 0 2px 8px rgba(0,0,0,0.5);
}
.skip-link:focus { top: 0; outline: none; box-shadow: var(--focus-ring-thick); }

/* ==================== APP CONTAINER ==================== */
#app {
    position: relative;
    width: min(100vw, 1280px);
    aspect-ratio: 16 / 10;
    max-height: 100vh; max-height: 100dvh;
    border-radius: 12px; overflow: hidden;
    box-shadow: 0 0 60px rgba(0,0,0,0.9), 0 0 120px rgba(100,0,150,0.15);
    outline: none;
}

@media (min-width: 1300px) { #app { max-height: 85vh; } }

@media (max-width: 1299px) and (min-width: 769px) {
    #app {
        width: 100vw; height: calc(100vh - 20px); height: calc(100dvh - 20px);
        max-height: none; aspect-ratio: auto; border-radius: 8px; margin: 10px;
    }
}

@media (max-width: 768px) {
    #app { width: 100vw; height: 100vh; height: 100dvh; max-height: none; border-radius: 0; aspect-ratio: auto; }
}

@media (max-width: 768px) and (orientation: landscape) {
    #app { width: 100vw; height: 100vh; height: 100dvh; }
    #mobileControls { bottom: 5px; padding: 0 8px; }
    .joystick-zone { width: 90px !important; height: 90px !important; }
    .action-btn { width: 52px !important; height: 52px !important; font-size: 18px !important; }
}

@media (max-width: 768px) and (orientation: portrait) {
    #app { display: flex; flex-direction: column; }
    canvas { width: 100%; height: auto; flex-shrink: 0; aspect-ratio: 16/10; max-height: 55vh; max-height: 55dvh; }
}

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

/* ==================== UI OVERLAY ==================== */
#uiOverlay { position: absolute; inset: 0; pointer-events: none; z-index: 10; }

/* ==================== HUD (3 columnas, sin superposición) ==================== */
#hud {
    position: absolute;
    top: 8px; left: 8px; right: 8px;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: start;
    gap: 6px;
    font-size: clamp(8px, 1.2vw, 10px);
    text-shadow: 1px 1px 3px rgba(0,0,0,0.9);
    pointer-events: none;
}

#hudLeft  { display: flex; justify-content: flex-start; }
#hudCenter { display: flex; flex-direction: column; align-items: center; gap: 3px; }
#hudRight { display: flex; justify-content: flex-end; }

.hud-item {
    background: var(--bg-panel);
    padding: clamp(6px, 1vw, 8px) clamp(10px, 1.5vw, 14px);
    border-radius: 8px; border: 1px solid var(--border-color);
    backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
    white-space: nowrap;
}

.progress-bar {
    width: clamp(80px, 15vw, 140px); height: 6px;
    background: rgba(255,255,255,0.1); border-radius: 3px; overflow: hidden;
}
.progress-fill {
    height: 100%; background: linear-gradient(90deg, #aa4400, #ff6600, #ffcc00);
    border-radius: 3px; transition: width 0.3s ease;
}

.lives-display { display: flex; gap: 3px; align-items: center; }
.heart { font-size: clamp(12px, 1.8vw, 14px); filter: drop-shadow(0 0 4px rgba(255,0,0,0.5)); }

/* ==================== SCREENS ==================== */
.screen-overlay {
    position: absolute; inset: 0;
    display: flex; flex-direction: column; justify-content: center; align-items: center;
    background: rgba(5, 5, 15, 0.93); pointer-events: auto; z-index: 20;
    padding: clamp(15px, 3vw, 30px); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
    overflow-y: auto; animation: fadeIn 0.4s ease-out;
}

.hidden { display: none !important; }

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
    .screen-overlay { animation: none !important; }
    @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
}

h1.screen-title {
    font-family: var(--font-title);
    font-size: clamp(24px, 5vw, 56px); color: #cc3333;
    text-shadow: 0 0 20px rgba(200,0,0,0.5), 3px 3px 0 #3a0a0a;
    margin-bottom: 8px; text-align: center; line-height: 1.2; letter-spacing: 2px;
}

h2.screen-subtitle {
    font-size: clamp(9px, 1.6vw, 16px); color: var(--text-secondary);
    margin-bottom: clamp(15px, 3vw, 25px); text-align: center; line-height: 1.6;
}

/* ==================== BUTTONS ==================== */
.btn {
    font-family: var(--font-ui); font-size: clamp(10px, 1.4vw, 13px);
    padding: clamp(12px, 2vw, 14px) clamp(24px, 4vw, 32px); margin: 5px;
    background: linear-gradient(180deg, #cc3333, #881111); color: #fff;
    border: 2px solid #aa2222; border-radius: 8px; cursor: pointer; pointer-events: auto;
    transition: all 0.15s; text-shadow: 1px 1px 0 rgba(0,0,0,0.5);
    box-shadow: 0 4px 0 #550000, 0 6px 15px rgba(0,0,0,0.4);
    min-width: 48px; min-height: var(--touch-min);
    display: flex; align-items: center; justify-content: center; gap: 8px;
}
.btn:hover { transform: translateY(-2px); box-shadow: 0 6px 0 #550000, 0 8px 20px rgba(0,0,0,0.5); background: linear-gradient(180deg, #dd4444, #992222); }
.btn:focus-visible { outline: none; box-shadow: var(--focus-ring-thick); }
.btn:active { transform: translateY(2px); box-shadow: 0 2px 0 #550000; }

.btn-secondary {
    background: linear-gradient(180deg, #3a3a4a, #2a2a3a); border-color: #5a5a6a;
    box-shadow: 0 4px 0 #1a1a2a;
}
.btn-secondary:hover { background: linear-gradient(180deg, #4a4a5a, #3a3a4a); }
.btn-secondary:focus-visible { box-shadow: var(--focus-ring-thick); }

.controls-grid {
    display: grid; grid-template-columns: auto auto;
    gap: 6px clamp(12px, 3vw, 20px); font-size: clamp(8px, 1.1vw, 9px);
    color: var(--text-secondary); margin-top: clamp(12px, 2vw, 20px);
    text-align: left; line-height: 1.8;
}

.key-badge {
    background: rgba(255,255,255,0.1); padding: 2px 8px; border-radius: 4px;
    border: 1px solid rgba(255,255,255,0.2); font-family: monospace; color: var(--accent-gold);
}

.score-display { font-size: clamp(14px, 2.5vw, 22px); color: var(--accent-gold); margin: 12px 0; text-shadow: 0 0 10px rgba(240,192,64,0.3); }
.level-name-display { font-size: clamp(10px, 1.5vw, 14px); color: #888; margin-bottom: 5px; }

/* ==================== GAME CTRL BUTTONS ==================== */
.game-ctrl-btn {
    position: absolute; z-index: 30;
    background: var(--bg-panel); border: 1px solid var(--border-color);
    border-radius: 8px; padding: clamp(6px, 1vw, 8px) clamp(10px, 1.5vw, 14px);
    color: var(--text-primary); cursor: pointer; font-size: clamp(14px, 2vw, 18px);
    pointer-events: auto; min-width: var(--touch-min); min-height: var(--touch-min);
    display: flex; align-items: center; justify-content: center;
    backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
    transition: background 0.15s;
}
.game-ctrl-btn:hover { background: rgba(30,30,60,0.98); }
.game-ctrl-btn:focus-visible { outline: none; box-shadow: var(--focus-ring-thick); }

/* Audio botón: top-right. Pausa: top-right junto a audio */
#audioToggle { top: 8px; right: 8px; }
#pauseBtn { top: 8px; right: 62px; font-family: var(--font-ui); font-size: clamp(7px, 1vw, 9px); }

/* ==================== MOBILE CONTROLS ==================== */
#mobileControls {
    position: absolute; bottom: 0; left: 0; right: 0;
    display: none; justify-content: space-between; align-items: flex-end;
    padding: clamp(8px, 2vw, 15px) clamp(10px, 2vw, 20px);
    padding-bottom: max(clamp(8px, 2vw, 15px), env(safe-area-inset-bottom));
    pointer-events: auto; z-index: 15;
    background: linear-gradient(0deg, rgba(0,0,0,0.5) 0%, transparent 100%);
}

@media (hover: none) and (pointer: coarse) { #mobileControls { display: flex; } }

@media (max-width: 768px) and (orientation: portrait) {
    #mobileControls { position: relative; background: rgba(10,10,18,0.95); padding: 15px 20px; padding-bottom: max(15px, env(safe-area-inset-bottom)); }
}

/* Joystick */
.joystick-zone {
    position: relative; width: 120px; height: 120px; touch-action: none; flex-shrink: 0;
}
.joystick-base {
    position: absolute; inset: 0; border-radius: 50%;
    background: rgba(255,255,255,0.05); border: 2px solid rgba(255,255,255,0.2);
    box-shadow: inset 0 0 18px rgba(0,0,0,0.4);
}
.joystick-base::before { content: ''; position: absolute; inset: 18%; border-radius: 50%; border: 1px solid rgba(255,255,255,0.08); }
.joystick-cross { position: absolute; inset: 0; pointer-events: none; }
.joystick-cross::before { content: ''; position: absolute; top: 50%; left: 10%; right: 10%; height: 1px; transform: translateY(-50%); background: rgba(255,255,255,0.1); }
.joystick-cross::after  { content: ''; position: absolute; left: 50%; top: 10%; bottom: 10%; width: 1px; transform: translateX(-50%); background: rgba(255,255,255,0.1); }
.joystick-knob {
    position: absolute; width: 44px; height: 44px; border-radius: 50%;
    background: radial-gradient(circle at 35% 30%, rgba(220,180,255,0.7), rgba(80,0,140,0.85));
    border: 2px solid rgba(200,150,255,0.4);
    box-shadow: 0 3px 10px rgba(0,0,0,0.5), inset 0 1px 2px rgba(255,255,255,0.2);
    top: 50%; left: 50%; transform: translate(-50%,-50%);
    pointer-events: none; transition: box-shadow 0.1s;
}
.joystick-knob.active { box-shadow: 0 2px 6px rgba(0,0,0,0.6), 0 0 14px rgba(160,80,255,0.6); }

/* Action Buttons */
.action-btns { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.action-btn-row { display: flex; gap: 10px; align-items: center; }
.action-btn-wrap { position: relative; display: flex; align-items: center; justify-content: center; }

.action-btn {
    width: 64px; height: 64px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 22px; cursor: pointer; user-select: none; -webkit-user-select: none;
    touch-action: none; -webkit-touch-callout: none; pointer-events: auto;
    border: 2px solid rgba(255,255,255,0.22); background: rgba(255,255,255,0.09);
    box-shadow: 0 4px 10px rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.12);
    transition: background 0.08s, transform 0.08s;
    min-width: var(--touch-min); min-height: var(--touch-min);
}
.action-btn[aria-pressed="true"], .action-btn:active {
    background: rgba(255,255,255,0.28); border-color: rgba(255,255,255,0.5);
    transform: scale(0.92); box-shadow: 0 2px 6px rgba(0,0,0,0.6), 0 0 14px rgba(200,150,255,0.4);
}
.action-btn:focus-visible { outline: none; box-shadow: var(--focus-ring-thick); }

.btn-jump  { background: radial-gradient(circle at 40% 30%, rgba(200,50,50,0.55), rgba(100,0,0,0.75)); border-color: rgba(220,80,80,0.45); }
.btn-crouch{ background: radial-gradient(circle at 40% 30%, rgba(80,80,200,0.55), rgba(20,20,100,0.75)); border-color: rgba(100,100,220,0.45); width: 54px; height: 54px; font-size: 18px; }
.btn-pause-m { background: rgba(255,255,255,0.05); border-color: rgba(255,255,255,0.12); width: 40px; height: 40px; font-size: 14px; }

.btn-label { position: absolute; bottom: -14px; left: 50%; transform: translateX(-50%); font-size: 7px; color: rgba(255,255,255,0.35); white-space: nowrap; font-family: var(--font-ui); pointer-events: none; }

/* ==================== ACCESSIBILITY ==================== */
@media (prefers-contrast: high) {
    :root { --border-color: rgba(255,255,255,0.5); --bg-panel: rgba(0,0,0,0.95); }
    .btn { border-width: 3px; }
    .action-btn { border-width: 3px; }
    .hud-item { border-width: 2px; }
}
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border-width: 0; }

.a11y-indicator { position: absolute; bottom: 8px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,0.7); color: #aaa; font-size: 8px; padding: 3px 8px; border-radius: 4px; z-index: 25; pointer-events: none; opacity: 0; transition: opacity 0.3s; }
.a11y-indicator.visible { opacity: 1; }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }
