/* ==========================================================================
   DISEÑO DE JUEGO: EL BOSQUE DE LAS FORMAS
   Estilo Premium, Adaptativo y Lleno de Vida para Niños
   ========================================================================== */

:root {
    --font-primary: 'Fredoka', sans-serif;
    --color-sky-top: #bde0fe;
    --color-sky-bottom: #ffafcc;
    --color-grass-top: #a8e063;
    --color-grass-bottom: #56ab2f;
    --color-mat-bg: rgba(255, 255, 255, 0.45);
    
    /* Colores de las figuras y sus cajas */
    --color-circle: #FF5E7E;
    --color-circle-bg: rgba(255, 94, 126, 0.15);
    --color-square: #2F80ED;
    --color-square-bg: rgba(47, 128, 237, 0.15);
    --color-triangle: #27AE60;
    --color-triangle-bg: rgba(39, 174, 96, 0.15);
    --color-star: #F2C94C;
    --color-star-bg: rgba(242, 201, 76, 0.15);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

body, html {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: var(--font-primary);
    background: linear-gradient(to bottom, var(--color-sky-top), var(--color-sky-bottom));
}

/* ==========================================================================
   PAISAJE DE FONDO ANIMADO
   ========================================================================== */
.sky-background {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    overflow: hidden;
    pointer-events: none;
}

/* Nubes */
.cloud {
    position: absolute;
    background: #ffffff;
    border-radius: 100px;
    filter: drop-shadow(0 8px 16px rgba(0,0,0,0.06));
    opacity: 0.85;
}
.cloud::before, .cloud::after {
    content: '';
    position: absolute;
    background: #ffffff;
    border-radius: 50%;
}
.cloud-1 {
    width: 170px;
    height: 60px;
    top: 15%;
    left: -200px;
    animation: floatCloud 35s linear infinite;
}
.cloud-1::before { width: 80px; height: 80px; top: -40px; left: 25px; }
.cloud-1::after { width: 100px; height: 100px; top: -50px; left: 70px; }

.cloud-2 {
    width: 120px;
    height: 45px;
    top: 8%;
    left: -200px;
    animation: floatCloud 25s linear infinite;
    animation-delay: 8s;
}
.cloud-2::before { width: 50px; height: 50px; top: -25px; left: 20px; }
.cloud-2::after { width: 70px; height: 70px; top: -35px; left: 45px; }

.cloud-3 {
    width: 190px;
    height: 65px;
    top: 25%;
    left: -200px;
    animation: floatCloud 45s linear infinite;
    animation-delay: 15s;
}
.cloud-3::before { width: 90px; height: 90px; top: -45px; left: 30px; }
.cloud-3::after { width: 110px; height: 110px; top: -55px; left: 80px; }

/* El sol */
.sun {
    position: absolute;
    top: 50px;
    right: 80px;
    width: 90px;
    height: 90px;
    background: radial-gradient(circle, #fff7ad 0%, #f7d02c 70%, #f39c12 100%);
    border-radius: 50%;
    box-shadow: 0 0 40px rgba(243, 156, 18, 0.4);
    animation: pulseSun 4s ease-in-out infinite alternate;
}

@keyframes floatCloud {
    0% { transform: translateX(0); }
    100% { transform: translateX(calc(100vw + 300px)); }
}

@keyframes pulseSun {
    0% { transform: scale(1); box-shadow: 0 0 30px rgba(243, 156, 18, 0.3); }
    100% { transform: scale(1.08); box-shadow: 0 0 50px rgba(243, 156, 18, 0.6); }
}

/* ==========================================================================
   CONTENEDORES DEL JUEGO
   ========================================================================== */
.game-container {
    position: relative;
    z-index: 2;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    padding: 15px 25px 25px 25px;
}

/* Encabezado */
.game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    height: 60px;
    flex-shrink: 0;
}

.game-title {
    font-size: 2.2rem;
    font-weight: 700;
    color: #4a4e69;
    text-shadow: 2px 2px 0px rgba(255, 255, 255, 0.8), 4px 4px 10px rgba(0,0,0,0.1);
    letter-spacing: 0.5px;
}

/* Botones de Control */
.controls {
    display: flex;
    gap: 12px;
}

.btn-control {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    border: 3px solid #ffffff;
    background: rgba(255, 255, 255, 0.8);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.btn-control .icon {
    font-size: 1.5rem;
    line-height: 1;
}

.btn-control:hover {
    transform: scale(1.1) translateY(-2px);
    background: #ffffff;
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.15);
}

.btn-control:active {
    transform: scale(0.95) translateY(0);
}

/* Control de Sonido Apagado */
.btn-control:not(.active) .icon {
    filter: grayscale(100%) opacity(0.5);
}

/* Barra de progreso */
.progress-container {
    display: flex;
    align-items: center;
    gap: 15px;
    background: rgba(255, 255, 255, 0.7);
    padding: 8px 20px;
    border-radius: 25px;
    border: 3px solid #ffffff;
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.05);
    flex-grow: 1;
    max-width: 450px;
    margin: 0 20px;
}

.progress-bar-background {
    flex-grow: 1;
    height: 20px;
    background: #e0e0e0;
    border-radius: 10px;
    overflow: hidden;
    position: relative;
    border: 1px solid rgba(0,0,0,0.05);
    box-shadow: inset 0 2px 5px rgba(0,0,0,0.1);
}

.progress-bar-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, #a8e063 0%, #56ab2f 100%);
    border-radius: 10px;
    transition: width 0.3s cubic-bezier(0.1, 0.8, 0.1, 1);
    position: relative;
    overflow: hidden;
}

.progress-bar-fill::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(
        90deg,
        rgba(255,255,255,0) 0%,
        rgba(255,255,255,0.4) 50%,
        rgba(255,255,255,0) 100%
    );
    animation: shineProgress 2.5s infinite;
}

@keyframes shineProgress {
    0% { transform: translateX(-100%); }
    100% { transform: translateX(100%); }
}

.progress-text {
    font-size: 1.2rem;
    font-weight: 700;
    color: #4a4e69;
    min-width: 80px;
    text-align: right;
}


/* ==========================================================================
   TABLERO PRINCIPAL (ADAPTATIVO)
   ========================================================================== */
.game-board {
    display: grid;
    grid-template-columns: 240px 1fr 240px;
    gap: 20px;
    flex-grow: 1;
    height: calc(100% - 72px);
}

/* Columnas de Cajas */
.boxes-side {
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    gap: 20px;
}

/* Caja Contenedora ("Cajita") */
.box-container {
    background: rgba(255, 255, 255, 0.7);
    border: 4px solid #ffffff;
    border-radius: 30px;
    padding: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.08);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    position: relative;
    cursor: pointer;
    min-height: 180px;
}

/* Colorear bordes y fondo según el tipo de caja */
#box-circle { border-color: var(--color-circle); background: rgba(255, 255, 255, 0.85); }
#box-square { border-color: var(--color-square); background: rgba(255, 255, 255, 0.85); }
#box-triangle { border-color: var(--color-triangle); background: rgba(255, 255, 255, 0.85); }
#box-star { border-color: var(--color-star); background: rgba(255, 255, 255, 0.85); }

/* Etiquetas de las cajas */
.box-label {
    font-size: 1.3rem;
    font-weight: 700;
    margin-bottom: 10px;
    color: #4a4e69;
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Apariencia visual de la caja */
.box-visual {
    width: 100px;
    height: 100px;
    border-radius: 20px;
    background: #f8f9fa;
    border: 3px dashed rgba(0, 0, 0, 0.15);
    display: flex;
    position: relative;
    justify-content: center;
    align-items: center;
    transition: all 0.3s ease;
}

#box-circle .box-visual { background: var(--color-circle-bg); border-color: rgba(255, 94, 126, 0.4); }
#box-square .box-visual { background: var(--color-square-bg); border-color: rgba(47, 128, 237, 0.4); }
#box-triangle .box-visual { background: var(--color-triangle-bg); border-color: rgba(39, 174, 96, 0.4); }
#box-star .box-visual { background: var(--color-star-bg); border-color: rgba(242, 201, 76, 0.4); }

.box-icon {
    width: 65px;
    height: 65px;
    filter: drop-shadow(0 4px 6px rgba(0,0,0,0.1));
    opacity: 0.85;
    transition: all 0.3s ease;
}

/* Efectos al arrastrar encima de la caja (Hover del drag) */
.box-container.drag-over {
    transform: scale(1.08);
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.18);
}
.box-container.drag-over .box-visual {
    background: #ffffff;
    border-style: solid;
    transform: scale(1.1);
}

/* ==========================================================================
   ÁREA CENTRAL (ALFOMBRA DE JUEGO)
   ========================================================================== */
.play-mat {
    background: var(--color-mat-bg);
    border: 4px solid rgba(255, 255, 255, 0.7);
    border-radius: 40px;
    box-shadow: inset 0 6px 15px rgba(0,0,0,0.05), 0 12px 30px rgba(0,0,0,0.07);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    position: relative;
    display: flex;
    flex-direction: column;
    overflow: visible;
    padding: 15px;
}

.mat-label {
    align-self: center;
    background: rgba(255, 255, 255, 0.8);
    padding: 6px 20px;
    border-radius: 20px;
    font-size: 1.1rem;
    font-weight: 600;
    color: #7b809a;
    box-shadow: 0 4px 8px rgba(0,0,0,0.04);
    pointer-events: none;
    z-index: 5;
    margin-bottom: 5px;
    text-align: center;
}

.shapes-spawn-area {
    position: relative;
    flex-grow: 1;
    width: 100%;
    height: 100%;
}

/* ==========================================================================
   FIGURAS JUGABLES (SVGs Dinámicos)
   ========================================================================== */
.shape-item {
    position: absolute;
    width: 90px;
    height: 90px;
    cursor: grab;
    touch-action: none;
    z-index: 10;
    filter: drop-shadow(0 8px 12px rgba(0, 0, 0, 0.15));
    transition: transform 0.1s ease, filter 0.2s ease;
    will-change: transform, left, top;
}

/* Al arrastrar */
.shape-item.dragging {
    cursor: grabbing;
    z-index: 100;
    filter: drop-shadow(0 15px 25px rgba(0, 0, 0, 0.25));
    transform: scale(1.15) !important; /* Fuerza el reescalado sobre rotaciones de JS */
}

/* Estilo Kawaii de las figuras */
.shape-svg {
    width: 100%;
    height: 100%;
    pointer-events: none; /* Asegura que el evento pointer sea capturado por el contenedor de la figura */
}

/* Animación de flotado suave para las figuras en el suelo */
@keyframes shapeFloat {
    0% { transform: translateY(0px) rotate(var(--rand-rot)); }
    50% { transform: translateY(-6px) rotate(var(--rand-rot)); }
    100% { transform: translateY(0px) rotate(var(--rand-rot)); }
}

.shape-item.idle {
    animation: shapeFloat 4s ease-in-out infinite;
}

/* ==========================================================================
   ANIMACIONES DE FEEDBACK
   ========================================================================== */

/* Acierto: Wiggle de felicidad de la caja */
@keyframes successWiggle {
    0% { transform: scale(1); }
    25% { transform: scale(1.1) rotate(-3deg); }
    50% { transform: scale(1.1) rotate(3deg); }
    75% { transform: scale(1.1) rotate(-3deg); }
    100% { transform: scale(1); }
}
.animate-success {
    animation: successWiggle 0.5s ease-in-out;
}

/* Error: Vibración lateral de la caja */
@keyframes errorShake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-8px); }
    40%, 80% { transform: translateX(8px); }
}
.animate-error {
    animation: errorShake 0.4s ease-in-out;
}

/* Desvanecimiento al guardar en caja */
@keyframes shrinkToBox {
    0% { transform: scale(1.1); opacity: 1; }
    100% { transform: scale(0.2); opacity: 0; }
}
.shape-shrinking {
    pointer-events: none;
    animation: shrinkToBox 0.4s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards;
}

/* Retorno elástico al centro */
.shape-returning {
    transition: left 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.2) !important;
    transition-property: left, top !important;
    pointer-events: none;
}

/* ==========================================================================
   PANTALLA DE CELEBRACIÓN (OVERLAY)
   ========================================================================== */
.celebration-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1000;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(15px);
    -webkit-backdrop-filter: blur(15px);
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.5s ease;
}

.celebration-overlay.show {
    opacity: 1;
    pointer-events: auto;
}

#confetti-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1001;
}

.celebration-content {
    background: #ffffff;
    border-radius: 40px;
    padding: 40px 60px;
    text-align: center;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
    border: 8px solid #FFED9E;
    max-width: 90%;
    width: 480px;
    transform: scale(0.7) translateY(50px);
    transition: transform 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    z-index: 1002;
    position: relative;
}

.celebration-overlay.show .celebration-content {
    transform: scale(1) translateY(0);
}

/* Desfile de personajes felices */
.characters-parade {
    display: flex;
    justify-content: center;
    gap: 20px;
    margin-bottom: 25px;
}

.happy-character {
    width: 70px;
    height: 70px;
    background-size: contain;
    background-repeat: no-repeat;
    animation: jumpCharacter 1.2s infinite ease-in-out alternate;
}
.char-circle {
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="45" fill="%23FF5E7E"/><circle cx="35" cy="45" r="5" fill="%23333"/><circle cx="35" cy="43" r="1.5" fill="%23fff"/><circle cx="65" cy="45" r="5" fill="%23333"/><circle cx="65" cy="43" r="1.5" fill="%23fff"/><ellipse cx="30" cy="53" rx="5" ry="2.5" fill="%23ff9999" opacity="0.6"/><ellipse cx="70" cy="53" rx="5" ry="2.5" fill="%23ff9999" opacity="0.6"/><path d="M44,52 Q50,60 56,52" stroke="%23333" stroke-width="4" stroke-linecap="round" fill="none"/></svg>');
    animation-delay: 0s;
}
.char-star {
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><polygon points="50,5 63,33 93,33 69,52 78,82 50,63 22,82 31,52 7,33 37,33" fill="%23F2C94C"/><circle cx="40" cy="42" r="4.5" fill="%23333"/><circle cx="60" cy="42" r="4.5" fill="%23333"/><ellipse cx="34" cy="49" rx="4" ry="2" fill="%23ff9999" opacity="0.6"/><ellipse cx="66" cy="49" rx="4" ry="2" fill="%23ff9999" opacity="0.6"/><path d="M45,48 Q50,55 55,48" stroke="%23333" stroke-width="3" stroke-linecap="round" fill="none"/></svg>');
    animation-delay: 0.2s;
}
.char-square {
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><rect x="10" y="10" width="80" height="80" rx="12" fill="%232F80ED"/><circle cx="35" cy="45" r="5" fill="%23333"/><circle cx="35" cy="43" r="1.5" fill="%23fff"/><circle cx="65" cy="45" r="5" fill="%23333"/><circle cx="65" cy="43" r="1.5" fill="%23fff"/><ellipse cx="28" cy="53" rx="5" ry="2.5" fill="%23ff9999" opacity="0.6"/><ellipse cx="72" cy="53" rx="5" ry="2.5" fill="%23ff9999" opacity="0.6"/><path d="M44,52 Q50,58 56,52" stroke="%23333" stroke-width="4" stroke-linecap="round" fill="none"/></svg>');
    animation-delay: 0.4s;
}

@keyframes jumpCharacter {
    0% { transform: translateY(0) rotate(-5deg); }
    100% { transform: translateY(-20px) rotate(5deg); }
}

.celebration-title {
    font-size: 2.8rem;
    color: #2D9CDB;
    margin-bottom: 10px;
    text-shadow: 2px 2px 0px #fff, 4px 4px 0px rgba(0,0,0,0.05);
}

.celebration-subtitle {
    font-size: 1.4rem;
    color: #4a4e69;
    margin-bottom: 30px;
}

/* Botón jugar otra vez */
.btn-replay {
    background: linear-gradient(135deg, #FFED9E 0%, #FFA800 100%);
    border: 4px solid #ffffff;
    border-radius: 25px;
    padding: 15px 35px;
    font-family: var(--font-primary);
    font-size: 1.6rem;
    font-weight: 700;
    color: #ffffff;
    text-shadow: 0 2px 4px rgba(0,0,0,0.2);
    cursor: pointer;
    box-shadow: 0 10px 20px rgba(255, 168, 0, 0.4);
    transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    animation: pulseReplay 2s infinite;
}

.btn-replay:hover {
    transform: scale(1.08) translateY(-3px);
    box-shadow: 0 15px 25px rgba(255, 168, 0, 0.5);
}

.btn-replay:active {
    transform: scale(0.96) translateY(1px);
}

@keyframes pulseReplay {
    0% { box-shadow: 0 10px 20px rgba(255, 168, 0, 0.4); }
    50% { box-shadow: 0 10px 30px rgba(255, 168, 0, 0.7); }
    100% { box-shadow: 0 10px 20px rgba(255, 168, 0, 0.4); }
}

/* ==========================================================================
   ADAPTATIVIDAD (MEDIA QUERIES)
   ========================================================================== */

/* Pantallas pequeñas (Móvil en Vertical/Retrato) */
@media (max-width: 768px) and (orientation: portrait) {
    .game-container {
        padding: 10px;
    }
    
    .game-header {
        margin-bottom: 8px;
        height: auto;
        flex-direction: column;
        gap: 8px;
        align-items: center;
    }
    
    .progress-container {
        margin: 0;
        width: 100%;
        max-width: 100%;
        padding: 5px 15px;
        order: 3;
    }
    
    .game-title {
        font-size: 1.5rem;
    }
    
    .btn-control {
        width: 42px;
        height: 42px;
        border-width: 2px;
    }
    
    .btn-control .icon {
        font-size: 1.2rem;
    }

    /* Redistribuir la rejilla en vertical: cajas arriba y abajo */
    .game-board {
        grid-template-columns: 1fr;
        grid-template-rows: auto 1fr auto;
        gap: 10px;
        height: calc(100% - 58px);
    }
    
    .boxes-side {
        flex-direction: row;
        justify-content: space-around;
        gap: 10px;
    }
    
    .left-side {
        grid-row: 1; /* Círculo y Cuadrado arriba */
    }
    
    .right-side {
        grid-row: 3; /* Triángulo y Estrella abajo */
    }

    .play-mat {
        grid-row: 2; /* Área de juego al medio */
        min-height: 200px;
        border-radius: 25px;
    }
    
    .box-container {
        flex: 1;
        min-height: 90px;
        height: 100px;
        padding: 8px;
        border-radius: 20px;
        border-width: 3px;
    }
    
    .box-label {
        font-size: 0.9rem;
        margin-bottom: 4px;
    }
    
    .box-visual {
        width: 50px;
        height: 50px;
        border-radius: 10px;
    }
    
    .box-icon {
        width: 32px;
        height: 32px;
    }
    
    .shape-item {
        width: 60px;
        height: 60px;
    }
}

/* Pantallas pequeñas (Móvil en Horizontal/Paisaje) */
@media (max-height: 500px) and (orientation: landscape) {
    .game-container {
        padding: 5px 10px;
    }
    
    .game-header {
        height: 40px;
        margin-bottom: 5px;
    }
    
    .game-title {
        font-size: 1.4rem;
    }
    
    .btn-control {
        width: 35px;
        height: 35px;
        border-width: 2px;
    }
    
    .btn-control .icon {
        font-size: 1.1rem;
    }
    
    .game-board {
        grid-template-columns: 140px 1fr 140px;
        gap: 10px;
        height: calc(100% - 45px);
    }
    
    .boxes-side {
        gap: 10px;
    }
    
    .box-container {
        min-height: 80px;
        padding: 5px;
        border-radius: 15px;
    }
    
    .box-label {
        font-size: 0.85rem;
        margin-bottom: 2px;
    }
    
    .box-visual {
        width: 50px;
        height: 50px;
        border-radius: 10px;
    }
    
    .box-icon {
        width: 30px;
        height: 30px;
    }
    
    .shape-item {
        width: 55px;
        height: 55px;
    }
    
    .celebration-content {
        padding: 20px;
        width: 380px;
        border-radius: 20px;
    }
    
    .celebration-title {
        font-size: 1.8rem;
    }
    
    .celebration-subtitle {
        font-size: 1rem;
        margin-bottom: 15px;
    }
    
    .btn-replay {
        font-size: 1.2rem;
        padding: 8px 20px;
    }
}
