/* ==========================================================================
   1. VARIABLES DE DISEÑO (mismas que MATEMATICAS)
   ========================================================================== */
:root {
    --bg-main: #fcfbf7;
    --surface: #ffffff;
    --text-dark: #1a1a1a;
    --text-muted: #666;
    --border-soft: #e0dcd3;

    --color-easy: #4caf50;
    --color-medium: #2196f3;
    --color-hard: #f44336;

    --radius-lg: 16px;
    --radius-md: 8px;
    --transition: all 0.3s ease;

    /* Ancho máximo compartido por cabecera y contenido */
    --page-max: 1050px;
    --page-pad: 20px;

    /* Tamaños que escalan con la ALTURA de la ventana para evitar scroll */
    --block-gap: clamp(8px, 1.8vh, 18px);
    --img-h: clamp(40px, 9.5vh, 76px);
}

/* ==========================================================================
   2. BASE
   ========================================================================== */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

[hidden] {
    display: none !important;
}

body {
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--bg-main);
    color: var(--text-dark);
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
}

button {
    font-family: inherit;
    font-weight: 600;
}

button:focus-visible,
a:focus-visible {
    outline: 3px solid var(--color-medium);
    outline-offset: 2px;
}

/* ==========================================================================
   3. CABECERA (igual que MATEMATICAS)
   La banda ocupa todo el ancho; su contenido se centra con un máximo de 1050px.
   ========================================================================== */
.site-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border-soft);
}

.header-inner {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 10px;
    max-width: var(--page-max);
    margin: 0 auto;
    padding: 12px var(--page-pad);
}

.logo {
    justify-self: start;
    font-size: 1.25rem;
    letter-spacing: -0.5px;
}

.nav-link {
    justify-self: center;
    color: var(--color-easy);
    font-size: clamp(0.9rem, 3.8vw, 1.125rem);
    font-weight: 800;
    text-decoration: none;
    white-space: nowrap;
}

.nav-link:hover {
    color: #000;
    text-decoration: underline;
}

.site-header .btn-secondary {
    justify-self: end;
}

/* ==========================================================================
   4. CONTENEDOR Y PANTALLAS
   ========================================================================== */
.game-container {
    flex: 1;
    width: 100%;
    max-width: var(--page-max);
    margin: 0 auto;
    padding: clamp(10px, 2.5vh, 28px) var(--page-pad);
    display: flex;
    flex-direction: column;
}

.screen {
    display: none;
    animation: fadeIn 0.4s ease forwards;
}

.screen.active {
    display: block;
}

/* El tablero apila sus bloques en columna con un único "gap" */
.screen-board.active {
    display: flex;
    flex-direction: column;
    gap: var(--block-gap);
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
}

.footer-note {
    margin-top: auto;
    padding-top: var(--block-gap);
    color: #888;
    text-align: center;
}

/* ==========================================================================
   5. TEXTOS DEL MENÚ
   ========================================================================== */
.main-title {
    margin-bottom: 10px;
    font-size: 2.5rem;
    text-align: center;
}

.subtitle {
    margin-bottom: 30px;
    color: var(--text-muted);
    text-align: center;
}

.section-title {
    margin-bottom: 10px;
    font-size: 0.9rem;
    letter-spacing: 1px;
    text-align: center;
    text-transform: uppercase;
    color: #888;
}

.calm-note {
    margin-top: 24px;
    color: var(--text-muted);
    text-align: center;
}

/* ==========================================================================
   6. SELECCIÓN DE DIFICULTAD
   ========================================================================== */
.difficulty-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 25px;
}

.card-diff {
    width: 100%;
    padding: 12px 20px;
    background: var(--surface);
    border: 2px solid var(--border-soft);
    border-radius: var(--radius-lg);
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    flex-direction: column;
    align-items: center;
    color: inherit;
}

.diff-title {
    margin-bottom: 15px;
    font-size: 1.6rem;
}

.diff-icon {
    display: flex;
    align-items: center;
    height: 80px;
    font-size: 2.6rem;
}

.diff-desc {
    margin-top: 15px;
    color: #555;
}

.card-diff.easy:hover   { border-color: var(--color-easy);   box-shadow: 0 10px 20px rgba(76, 175, 80, 0.1); }
.card-diff.medium:hover { border-color: var(--color-medium); box-shadow: 0 10px 20px rgba(33, 150, 243, 0.1); }
.card-diff.hard:hover   { border-color: var(--color-hard);   box-shadow: 0 10px 20px rgba(244, 67, 54, 0.1); }

/* ==========================================================================
   7. TABLERO: ESTADÍSTICAS
   ========================================================================== */
.board-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.stat-box {
    padding: 8px 18px;
    background: var(--surface);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-md);
    font-weight: 600;
    white-space: nowrap;
}

.level-name.easy   { color: var(--color-easy); }
.level-name.medium { color: var(--color-medium); }
.level-name.hard   { color: var(--color-hard); }

/* ==========================================================================
   8. TABLERO: PANEL, IMÁGENES Y MENSAJES
   ========================================================================== */
.panel {
    padding: clamp(10px, 2vh, 18px) clamp(12px, 3vw, 26px);
    background: var(--surface);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-lg);
    text-align: center;
}

.panel h2 {
    font-size: clamp(1.15rem, 3vh, 1.5rem);
}

.instruction {
    margin: 2px 0 clamp(6px, 1.4vh, 12px);
    color: var(--text-muted);
}

/* Columnas por defecto: 6. En móvil cambian según el nivel (ver sección 12) */
.items-grid {
    --cols: 6;
    display: grid;
    grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
    gap: clamp(6px, 1.2vh, 10px);
    max-width: 900px;
    margin: 0 auto;
}

.game-item {
    padding: 2px;
    display: grid;
    place-items: center;
    background: var(--surface);
    border: 2px solid var(--border-soft);
    border-radius: 12px;
    cursor: pointer;
    transition: var(--transition);
}

.game-item img {
    display: block;
    width: 100%;
    height: var(--img-h);
    object-fit: contain;
}

.game-item:hover:not(:disabled) {
    transform: translateY(-2px);
    border-color: var(--text-dark);
}

.game-item:disabled {
    cursor: default;
}

.game-item.correct   { background: #e8f5e9; border-color: var(--color-easy); }
.game-item.incorrect { background: #ffebee; border-color: var(--color-hard); }

/* Un único espacio para pista y resultado: reserva su altura para que nada "salte" */
.message {
    min-height: 42px;
    margin-top: var(--block-gap);
    padding: 8px 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid transparent;
    border-radius: var(--radius-md);
    font-size: 0.95rem;
    font-weight: 600;
}

.message.is-hint    { background: #eef5ff; color: #234c76; border-color: #bfd6f4; }
.message.is-success { background: #e8f5e9; color: #2e7d32; border-color: #c8e6c9; }
.message.is-error   { background: #ffebee; color: #c62828; border-color: #ffcdd2; }

.game-actions {
    margin-top: var(--block-gap);
    display: flex;
    gap: 10px;
}

.game-actions button {
    flex: 1;
    scroll-margin-bottom: 24px; /* al hacer scroll automático, no queda pegado al borde */
}

/* ==========================================================================
   9. RESULTADOS
   ========================================================================== */
.result-panel {
    max-width: 650px;
    margin: 0 auto;
    padding: 24px 30px;
}

.result-icon {
    display: block;
    font-size: 3rem;
}

.result-panel h2 {
    margin: 4px 0;
}

.result-score {
    font-size: 1.25rem;
    font-weight: 600;
}

.result-message {
    color: var(--text-muted);
}

.result-summary {
    margin: 16px 0;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.result-summary > div {
    padding: 12px;
    background: var(--bg-main);
    border: 1px solid var(--border-soft);
    border-radius: var(--radius-md);
}

.result-summary span {
    display: block;
    font-size: 1.45rem;
}

.result-summary strong {
    display: block;
    font-size: 1.3rem;
}

.result-summary small {
    color: var(--text-muted);
}

.result-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
}

/* ==========================================================================
   10. BOTONES
   ========================================================================== */
.btn-primary,
.btn-secondary {
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: var(--transition);
}

.btn-primary   { padding: 10px 20px; background: var(--text-dark); color: #fff; border: none; font-size: 1rem; }
.btn-secondary { padding: 8px 16px; background: transparent; color: var(--text-dark); border: 1px solid var(--text-dark); font-size: 0.9rem; }

button:hover:not(:disabled) {
    opacity: 0.9;
}

/* Hover de los botones de contorno (Irten Jokotik, Nola jokatu?...) */
.btn-secondary:hover:not(:disabled) {
    background: var(--text-dark);
    color: #fff;
    opacity: 1;
}

.btn-secondary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ==========================================================================
   11. MODAL DE REGLAS (igual que MATEMATICAS)
   ========================================================================== */
.modal {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 100;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.4);
}

.modal.active {
    display: flex;
}

.modal-content {
    width: 90%;
    max-width: 500px;
    padding: 30px;
    background: var(--surface);
    border-radius: var(--radius-lg);
}

.modal-content h2 { margin-bottom: 15px; }
.modal-content ul { margin: 0 0 25px 20px; color: #555; }
.modal-content li { margin-bottom: 8px; }

/* ==========================================================================
   12. PANTALLAS BAJAS (portátiles con ventana de poca altura)
   ========================================================================== */
@media (max-height: 700px) {
    .footer-note {
        display: none;
    }
}

@media (min-width: 769px) and (max-height: 620px) {
    .instruction {
        display: none;
    }
}

/* ==========================================================================
   13. MÓVIL
   Las fichas van de 3 en 3 en todos los niveles (2, 4 y 6 filas). En el nivel
   difícil habrá algo de scroll: es intencionado, así las imágenes se ven grandes.
   ========================================================================== */
@media (max-width: 768px) {
    :root {
        --page-pad: 12px;
    }

    .header-inner {
        grid-template-columns: auto 1fr auto;
        padding: 1px var(--page-pad);
    }

    .site-header .btn-secondary {
        padding: 6px 10px;
        font-size: 0.8rem;
    }

    /* Menú compacto */
    .main-title {
        font-size: 2rem;
    }

    .subtitle {
        margin-bottom: 16px;
    }

    .difficulty-grid {
        gap: 12px;
    }

    .card-diff {
        padding: 10px 16px;
    }

    .diff-title {
        margin-bottom: 4px;
        font-size: 1.4rem;
    }

    .diff-icon {
        height: auto;
        font-size: 2rem;
    }

    .diff-desc {
        margin-top: 4px;
    }

    .calm-note {
        margin-top: 14px;
    }

    /* Estadísticas en matriz 2x2 con letra grande (niños y personas mayores) */
    .board-header {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    .stat-box {
        padding: 4px 6px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.0rem;
        line-height: 1.2;
        text-align: center;
        text-wrap: balance;
        white-space: normal;
    }

    .board-header .btn-secondary {
        width: 100%;
        padding: 4px 6px;
        font-size: 1.0rem;
    }

    .panel {
        padding: 10px;
    }

    /* Fichas cuadradas, 3 por fila */
    .items-grid {
        --cols: 3;
        gap: 6px;
        max-width: 420px;
    }

    .game-item {
        aspect-ratio: 1;
        padding: 4px;
    }

    .game-item img {
        height: 100%;
    }

    .message {
        min-height: 52px;
        font-size: 0.9rem;
    }

    .game-actions button {
        padding-left: 8px;
        padding-right: 8px;
        font-size: 0.9rem;
    }

    .result-panel {
        padding: 18px 14px;
    }
}

/* Móviles bajos: tarjetas del menú en horizontal para que quepan sin scroll */
@media (max-width: 768px) and (max-height: 620px) {
    .main-title {
        margin-bottom: 4px;
        font-size: 1.5rem;
    }

    .subtitle {
        margin-bottom: 10px;
        font-size: 0.9rem;
    }

    .section-title,
    .calm-note {
        display: none;
    }

    .difficulty-grid {
        gap: 8px;
    }

    .card-diff {
        padding: 6px 14px;
        display: grid;
        grid-template-columns: auto 1fr;
        grid-template-areas:
            "icon title"
            "icon desc";
        column-gap: 14px;
        align-items: center;
        text-align: left;
    }

    .diff-icon  { grid-area: icon; font-size: 1.8rem; }
    .diff-title { grid-area: title; margin: 0; font-size: 1.2rem; }
    .diff-desc  { grid-area: desc; margin: 0; font-size: 0.85rem; }
}
