* { margin: 0; padding: 0; box-sizing: border-box; }
html { font-size: 16px; }
body { 
    font-family: 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif; 
    background: linear-gradient(135deg, #10141f, #16213e, #0f0f23); 
    color: #e0e0e0; 
    min-height: 100vh; 
    /* Se elimina overflow-x: hidden de aquí para un mejor control */
}
h1, h2, h3 { font-family: 'Georgia', serif; }

/* CONTENEDOR PRINCIPAL: Permite el crecimiento y scroll */
#game-container { 
    position: relative; 
    min-height: 100vh; 
    width: 100%; 
    overflow-x: hidden; /* Evita scroll horizontal no deseado */
}

/* PANTALLAS: Se habilita el scroll individual */
.screen { 
    display: none; 
    min-height: 100vh; 
    position: absolute; 
    top: 0; 
    left: 0; 
    width: 100%; 
    z-index: 1; 
    transition: opacity 0.5s ease-in-out; 
    opacity: 0; 
    pointer-events: none;
    overflow-y: auto; /* CLAVE: Permite scroll vertical si el contenido es muy alto */
    padding-bottom: 3rem; /* Espacio al final para que no quede pegado */
}

.screen.active { 
    display: flex; 
    opacity: 1; 
    z-index: 10; 
    pointer-events: auto; 
}
#start-screen { 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    background: radial-gradient(circle at 20% 80%, rgba(78, 205, 196, 0.2), transparent 50%), linear-gradient(135deg, #0c0c0c, #1a1a2e, #16213e); 
}
.mansion-bg { text-align: center; background: rgba(15, 23, 42, 0.8); padding: 3rem; border-radius: 20px; border: 1px solid #334155; box-shadow: 0 0 60px rgba(78, 205, 196, 0.15), inset 0 0 20px rgba(0, 0, 0, 0.5); backdrop-filter: blur(10px); position: relative; max-width: 650px; }
.game-title { font-size: 2.8rem; margin-bottom: 1rem; color: #ffd93d; text-shadow: 0 0 15px rgba(255, 217, 61, 0.6); }
.game-subtitle { font-size: 1.2rem; color: #94a3b8; margin-bottom: 2rem; font-style: italic; }
.story-box { background: rgba(0, 0, 0, 0.6); padding: 1.5rem; border-radius: 10px; margin: 2rem 0; border-left: 4px solid #4ecdc4; line-height: 1.6; }
.warning { color: #ff4757; font-weight: bold; margin-top: 1rem; animation: warningBlink 1.5s infinite; }
@keyframes warningBlink { 0%, 100% { opacity: 1; } 50% { opacity: 0.7; } }
#game-screen { flex-direction: column; align-items: normal; justify-content: flex-start; /* Alinea al inicio */ }
.game-header { background: rgba(15, 23, 42, 0.9); padding: 1rem 2rem; border-bottom: 1px solid #334155; backdrop-filter: blur(5px); z-index: 10; width: 100%; position: sticky; top: 0; }
.status-bar { display: flex; justify-content: space-between; align-items: center; max-width: 1400px; margin: 0 auto; }
.level-info, .lives { font-size: 1.1rem; letter-spacing: 0.5px; }
.ghost-counter { color: #ffc107; }
.lives { color: #e0e0e0; }
.lives span#lives { color: #ff4757; font-size: 1.3rem; vertical-align: middle; margin-left: 0.5rem; }

.header-actions {
    display: flex;
    align-items: center;
    gap: 1.5rem; /* Espacio entre vidas y botón escapar */
    flex-wrap: wrap; /* MODIFICADO: Permitir que los elementos se envuelvan si no caben */
    justify-content: flex-end; /* Alinea los items a la derecha por defecto si hay espacio */
}

.btn-escape {
    background: linear-gradient(45deg, #ff8c00, #ff4500); /* Naranja/Rojo */
    color: white;
    padding: 8px 15px;
    border: none;
    border-radius: 20px;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 2px 10px rgba(255, 100, 0, 0.3);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: bold;
    text-decoration: none;
    display: inline-block;
}

.btn-escape:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 15px rgba(255, 100, 0, 0.5);
}

.game-main { display: grid; grid-template-columns: 1fr 350px; gap: 2rem; padding: 2rem; max-width: 1400px; margin: 0 auto; width: 100%; }
.room-container { background: rgba(15, 23, 42, 0.6); border-radius: 15px; padding: 2.5rem; border: 1px solid #334155; }
.room-desc h2 { color: #ffd93d; font-size: 2rem; text-align: center; margin-bottom: 0.5rem; text-shadow: 0 0 10px rgba(255, 217, 61, 0.5); }
.room-desc p { text-align: center; color: #94a3b8; margin-bottom: 2.5rem; font-style: italic; }
.problem-container { background: #1e293b; border-radius: 10px; padding: 2rem; border: 1px solid #334155; }
.problem-box { margin-bottom: 2rem; padding: 1rem 1.5rem; background: #10141f; border-radius: 8px; border-left: 4px solid #4ecdc4; }
.problem-box h3 { margin-bottom: 1rem; color: #e0e0e0; }
.math-content { font-size: 1.5rem; line-height: 1.8; color: #e8e8e8; margin: 1rem 0; text-align: center; overflow-x: auto; /* Permite scroll si la fórmula es muy ancha */ }
.answer-section { display: flex; flex-direction: column; gap: 1.5rem; }
.input-group { display: flex; flex-direction: column; gap: 0.5rem; }
.input-group label { font-weight: bold; color: #cbd5e1; }
.input-field { background: #334155; border: 1px solid #475569; border-radius: 5px; padding: 12px; color: #e0e0e0; font-size: 1rem; transition: all 0.3s ease; }
.input-field:focus { outline: none; border-color: #4ecdc4; box-shadow: 0 0 10px rgba(78, 205, 196, 0.3); background: #475569; }
.input-field option { background: #1e293b; color: #e0e0e0; }
.btn-submit { background: linear-gradient(45deg, #11998e, #38ef7d); color: white; padding: 12px 30px; border: none; border-radius: 25px; font-size: 1.1rem; cursor: pointer; transition: all 0.3s ease; box-shadow: 0 4px 15px rgba(17, 153, 142, 0.4); text-transform: uppercase; letter-spacing: 1px; font-weight: bold; align-self: center; margin-top: 1rem; }
.btn-submit:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(17, 153, 142, 0.6); }
.help-panel { background: rgba(15, 23, 42, 0.8); border-radius: 15px; padding: 1.5rem; border: 1px solid #334155; height: fit-content; position: sticky; top: 100px; /* Ajustado para el header pegajoso */ backdrop-filter: blur(5px); }
.help-panel h3 { color: #ffd93d; margin-bottom: 1rem; text-align: center; font-size: 1.4rem; }
.help-panel p { color: #94a3b8; text-align: center; margin-bottom: 1.5rem; }
.help-topics-list { list-style: none; padding: 0; display: flex; flex-direction: column; gap: 0.75rem; }
.tooltip-topic { background: transparent; border: 1px solid #475569; border-radius: 8px; padding: 0.75rem 1rem; cursor: help; transition: all 0.2s ease-in-out; text-align: center; font-weight: bold; color: #cbd5e1; }
.tooltip-topic:hover { background-color: rgba(78, 205, 196, 0.1); border-color: #4ecdc4; color: #4ecdc4; }
#game-over-screen, #victory-screen { align-items: center; justify-content: center; text-align: center; flex-direction: column; padding: 1rem; }
.game-over-bg, .victory-bg { padding: 3rem; border-radius: 20px; width: 100%; max-width: 600px; }
.game-over-bg { background: rgba(139, 0, 0, 0.9); border: 2px solid #8b0000; box-shadow: 0 0 50px rgba(139, 0, 0, 0.5); }
.game-over-title { font-size: 2.5rem; color: #ff4757; margin-bottom: 1rem; animation: shake 0.5s ease-in-out infinite alternate; }
.ghost-animation { font-size: 3rem; margin: 1rem 0; animation: meltdown 1s ease-in-out infinite; }
.victory-bg { background: rgba(17, 153, 142, 0.9); border: 2px solid #38ef7d; box-shadow: 0 0 50px rgba(56, 239, 125, 0.5); }
.victory-title { font-size: 2.5rem; color: #8affc1; margin-bottom: 1rem; }
.sparkles { font-size: 1.5rem; font-weight: bold; color: #ffffff; margin: 1rem 0; text-shadow: 0 0 10px #ffffff; }
.score-display { background: rgba(0, 0, 0, 0.5); padding: 1.5rem; border-radius: 10px; margin: 2rem 0; font-size: 1.1rem; border: 1px solid #4ecdc4; text-align: left; }
.score-display h3 { text-align: center; margin-bottom: 1rem; }

.end-game-buttons {
    display: flex;
    justify-content: center;
    gap: 1rem; /* Espacio entre botones */
    margin-top: 1.5rem; /* Espacio sobre los botones */
    flex-wrap: wrap; /* Para que se ajusten en pantallas pequeñas */
}

.btn-primary { 
    background: linear-gradient(45deg, #667eea, #764ba2); 
    color: white; 
    padding: 12px 30px; 
    border: none; 
    border-radius: 25px; 
    font-size: 1.1rem; 
    cursor: pointer; 
    transition: all 0.3s ease; 
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4); 
    text-transform: uppercase; 
    letter-spacing: 1px; 
    font-weight: bold; 
    text-decoration: none; /* Para tags <a> */
    display: inline-block; /* Para que padding funcione bien en <a> */
    text-align: center;
    white-space: nowrap; /* MODIFICADO: Evitar que el texto del botón se parta en líneas */
}
.btn-primary:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6); }

.btn-primary.btn-exit-game {
    background: linear-gradient(45deg, #6c757d, #495057); /* Un color más neutro para salir */
    box-shadow: 0 4px 15px rgba(108, 117, 125, 0.3);
}
.btn-primary.btn-exit-game:hover {
    box-shadow: 0 6px 20px rgba(108, 117, 125, 0.5);
}

@keyframes shake { 0% { transform: translateX(-2px); } 100% { transform: translateX(2px); } }
@keyframes meltdown { 0%, 100% { transform: scale(1) rotate(-5deg); text-shadow: none; } 50% { transform: scale(1.2) rotate(5deg); text-shadow: 0 0 20px #ffc107; } }

/* ======================================= */
/* === ESTILOS DEL BOTÓN DE EMERGENCIA === */
/* ======================================= */
#emergency-button-container { position: relative; width: 280px; height: 100px; background: #333; border-radius: 10px; border: 3px solid #555; box-shadow: inset 0 2px 4px rgba(0,0,0,0.6), 0 5px 10px rgba(0,0,0,0.5); margin: 2rem auto 0 auto; cursor: pointer; overflow: hidden; -webkit-tap-highlight-color: transparent; }
.emergency-cover {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: repeating-linear-gradient(45deg, #ffc107, #ffc107 20px, #000000 20px, #000000 40px);
    z-index: 2;
    transition: transform 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55);
    display: flex;
    align-items: center;
    justify-content: center;
}
.emergency-cover span {
    color: white;
    font-size: 1.2rem;
    font-weight: bold;
    letter-spacing: 2px;
    text-shadow: 0px 0px 8px rgba(0, 0, 0, 1), 0px 0px 3px rgba(255, 255, 255, 0.3);
    transform: skewX(-10deg);
}
#emergency-button-container.cover-lifted .emergency-cover { transform: translateY(-110%); }
#start-btn.btn-emergency-actual { position: relative; width: 80px; height: 80px; background: radial-gradient(#ff4d4d, #c50000); border-radius: 50%; border: 3px solid #8b0000; color: white; font-weight: bold; font-size: 1rem; cursor: pointer; box-shadow: inset 0 3px 5px rgba(255, 255, 255, 0.4), 0 0 20px #ff0000; display: flex; align-items: center; justify-content: center; margin: 8px auto; z-index: 1; animation: pulse-red 1.5s infinite ease-in-out; outline: none; }
#start-btn.btn-emergency-actual .btn-gloss { position: absolute; top: 5px; left: 15px; width: 50px; height: 25px; background: linear-gradient(to bottom, rgba(255,255,255,0.5), rgba(255,255,255,0.1)); border-radius: 50% / 100%; border-bottom-left-radius: 0; border-bottom-right-radius: 0; transform: rotate(-15deg); }
#start-btn.btn-emergency-actual:hover { animation-play-state: paused; box-shadow: inset 0 3px 5px rgba(255, 255, 255, 0.5), 0 0 35px #ff1111, 0 0 50px #ff5555; }
#start-btn.btn-emergency-actual:active { transform: scale(0.95); box-shadow: inset 0 3px 5px rgba(0,0,0,0.5); }
@keyframes pulse-red { 0% { box-shadow: inset 0 3px 5px rgba(255, 255, 255, 0.4), 0 0 15px #ff0000; } 50% { box-shadow: inset 0 3px 5px rgba(255, 255, 255, 0.4), 0 0 30px #ff4848; } 100% { box-shadow: inset 0 3px 5px rgba(255, 255, 255, 0.4), 0 0 15px #ff0000; } }

/* FEEDBACK MODAL */
.feedback-modal { display: none; position: fixed; z-index: 1001; left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgba(0,0,0,0.7); backdrop-filter: blur(5px); align-items: center; justify-content: center; }
.feedback-modal.active { display: flex; animation: fadeIn 0.3s ease; }
.feedback-content { background: #1e293b; border-radius: 15px; padding: 2rem; border: 1px solid #334155; width: 90%; max-width: 550px; text-align: center; box-shadow: 0 0 50px rgba(0,0,0,0.5); }
.feedback-content.correct { border-color: #38ef7d; box-shadow: 0 0 30px rgba(56, 239, 125, 0.4); }
.feedback-content.incorrect { border-color: #ff4757; box-shadow: 0 0 30px rgba(255, 71, 87, 0.4); }
.feedback-content h4 { font-size: 1.8rem; margin-bottom: 1rem; }
.feedback-content.correct h4 { color: #38ef7d; }
.feedback-content.incorrect h4 { color: #ff4757; }
.feedback-content p { margin-bottom: 2rem; }
#feedback-continue-btn { display: block; margin: 0 auto; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* TOOLTIP */
.tooltip { position: fixed; background: rgba(15, 23, 42, 0.95); color: #e0e0e0; padding: 1rem; border-radius: 10px; border: 1px solid #4ecdc4; box-shadow: 0 0 20px rgba(78, 205, 196, 0.5); max-width: 320px; z-index: 1002; display: none; font-size: 0.95rem; line-height: 1.5; pointer-events: none; backdrop-filter: blur(5px); }
.tooltip.show { display: block; animation: fadeIn 0.2s ease-out; }
.tooltip-content h4 { color: #4ecdc4; margin-bottom: 0.75rem; }

/* ================== */
/* = RESPONSIVE CSS = */
/* ================== */
@media (max-width: 800px) {
    html { font-size: 14px; }
    
    /* MEJORA DE RENDIMIENTO: Desactiva filtros costosos */
    .mansion-bg, .help-panel, .tooltip, .game-header, .feedback-modal {
        backdrop-filter: none;
    }

    /* Layout de una sola columna */
    .game-main { 
        grid-template-columns: 1fr; 
        padding: 1rem; 
    }
    .status-bar { 
        flex-direction: column; 
        gap: 0.75rem; 
        text-align: center; 
    }
    .header-actions {
        justify-content: center; /* Centrar vidas y botón escapar en modo columna */
        width: 100%; /* Asegura que tome el ancho completo */
        /* flex-wrap: wrap; ya está añadido arriba globalmente, lo que es bueno */
    }
    .mansion-bg, .game-over-bg, .victory-bg { 
        padding: 1.5rem; 
        margin: 1rem; 
        max-width: 95vw;
    }
    .game-title { font-size: 2.2rem; }
    .room-container { padding: 1.5rem; }
    .problem-container { padding: 1rem; }
    
    /* El panel de ayuda ahora fluye con el contenido */
    .help-panel { 
        position: static; 
        margin-top: 1.5rem; 
    }
    .feedback-content { padding: 1.5rem; }
    .feedback-content h4 { font-size: 1.5rem; }
}