:root {
    --size: min(9.5vw, 4.8vh, 40px); 
}

@media (min-width: 640px) {
    :root { --size: min(48px, 6vh); }
}

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

body {
    margin: 0;
    padding: 0;
    font-family: 'Lora', serif;
    background: radial-gradient(circle at center, #fefce8, #fef3c7, #e0f2fe);
    color: #44403c;
    min-height: 100vh;
    min-height: 100dvh;
}

h1, h2, h3, .font-cinzel {
    font-family: 'Cinzel', serif;
}

/* Tablero mosaico sacro y piezas interactivas */
#grid, .piece, #game-container, .dragging-visual {
    touch-action: none;
}

#grid {
    background: rgba(255, 255, 255, 0.88);
    backdrop-filter: blur(8px);
    box-shadow: 0 8px 25px rgba(217, 119, 6, 0.2);
}

/* Animación fluida Candy Crush para fichas */
@keyframes candy-float {
    0%, 100% { transform: translateY(0) scale(1) rotate(0deg); }
    25% { transform: translateY(-2.5px) scale(1.03) rotate(-1deg); }
    50% { transform: translateY(0) scale(0.99) rotate(0deg); }
    75% { transform: translateY(2px) scale(1.02) rotate(1deg); }
}

/* Piezas base */
.piece {
    width: var(--size);
    height: var(--size);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: calc(var(--size) * 0.65);
    cursor: pointer;
    background: transparent;
    position: relative;
    pointer-events: auto;
    border-radius: 10px;
    transition: transform 0.18s cubic-bezier(0.175, 0.885, 0.32, 1.275), background-color 0.2s;
    animation: candy-float 3s ease-in-out infinite;
}

/* Variación de animación desfasada por fila y columna para movimiento orgánico */
.piece:nth-child(2n) { animation-delay: 0.2s; animation-duration: 3.2s; }
.piece:nth-child(3n) { animation-delay: 0.5s; animation-duration: 2.8s; }
.piece:nth-child(5n) { animation-delay: 0.8s; animation-duration: 3.4s; }
.piece:nth-child(7n) { animation-delay: 1.1s; animation-duration: 2.9s; }
.piece:nth-child(11n) { animation-delay: 1.4s; animation-duration: 3.1s; }

.piece:hover {
    transform: scale(1.22) translateY(-4px) !important;
    z-index: 10;
    filter: drop-shadow(0 6px 12px rgba(245, 158, 11, 0.5));
}

.piece:active {
    transform: scale(0.9) !important;
}

.piece.selected {
    opacity: 0.85;
    transform: scale(0.85) !important;
    background: #fde047;
    box-shadow: 0 0 16px #f59e0b;
    animation: none !important;
}

/* --- PIEZAS ESPECIALES CANDY CRUSH --- */

/* ⚡ Rayo Celestial horizontal (4 en fila) */
.piece-lightning-h, .piece-lightning {
    background: repeating-linear-gradient(
        0deg,
        rgba(255, 251, 180, 0.98),
        rgba(255, 251, 180, 0.98) 5px,
        rgba(234, 179, 8, 0.98) 5px,
        rgba(234, 179, 8, 0.98) 10px
    ) !important;
    border: 2px solid #92400e !important;
    box-shadow: 0 0 18px #facc15, 0 0 6px #fef08a inset;
    animation: lightning-flicker 0.9s infinite alternate !important;
}

/* ⚡ Rayo Celestial vertical (4 en columna) */
.piece-lightning-v {
    background: repeating-linear-gradient(
        90deg,
        rgba(255, 251, 180, 0.98),
        rgba(255, 251, 180, 0.98) 5px,
        rgba(234, 179, 8, 0.98) 5px,
        rgba(234, 179, 8, 0.98) 10px
    ) !important;
    border: 2px solid #92400e !important;
    box-shadow: 0 0 18px #facc15, 0 0 6px #fef08a inset;
    animation: lightning-flicker 0.9s infinite alternate !important;
}

.piece-lightning-h::after, .piece-lightning-v::after, .piece-lightning::after {
    content: "⚡";
    position: absolute;
    top: -5px;
    right: -5px;
    font-size: 14px;
    background: #fef08a;
    border: 1.5px solid #b45309;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 0 8px #fbbf24, 0 2px 4px rgba(0,0,0,0.25);
    animation: badge-pulse 0.9s infinite alternate;
}

@keyframes lightning-flicker {
    0%   { filter: brightness(1)    drop-shadow(0 0  8px #facc15); transform: scale(1);    }
    40%  { filter: brightness(1.35) drop-shadow(0 0 18px #fbbf24); transform: scale(1.05); }
    70%  { filter: brightness(1.1)  drop-shadow(0 0  6px #facc15); transform: scale(1.02); }
    100% { filter: brightness(1.5)  drop-shadow(0 0 22px #fef08a); transform: scale(1.07); }
}

@keyframes badge-pulse {
    0%   { box-shadow: 0 0  4px #fbbf24; transform: scale(1);    }
    100% { box-shadow: 0 0 12px #facc15; transform: scale(1.15); }
}

/* Barrido de luz cuando el rayo se activa */
.lightning-beam-h {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: linear-gradient(90deg,
        transparent 0%,
        rgba(255, 250, 150, 0.85) 40%,
        rgba(255, 255, 255, 0.95) 50%,
        rgba(255, 250, 150, 0.85) 60%,
        transparent 100%);
    pointer-events: none;
    z-index: 25;
    animation: beam-sweep-h 0.4s ease-out forwards;
    border-radius: 4px;
}

.lightning-beam-v {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: linear-gradient(180deg,
        transparent 0%,
        rgba(255, 250, 150, 0.85) 40%,
        rgba(255, 255, 255, 0.95) 50%,
        rgba(255, 250, 150, 0.85) 60%,
        transparent 100%);
    pointer-events: none;
    z-index: 25;
    animation: beam-sweep-v 0.4s ease-out forwards;
    border-radius: 4px;
}

@keyframes beam-sweep-h {
    0%   { opacity: 0; transform: scaleX(0.1); }
    20%  { opacity: 1; transform: scaleX(1.5); }
    60%  { opacity: 0.8; }
    100% { opacity: 0; transform: scaleX(1); }
}

@keyframes beam-sweep-v {
    0%   { opacity: 0; transform: scaleY(0.1); }
    20%  { opacity: 1; transform: scaleY(1.5); }
    60%  { opacity: 0.8; }
    100% { opacity: 0; transform: scaleY(1); }
}

/* Popup especial del rayo */
.floating-popup.lightning {
    color: #92400e;
    text-shadow: 0 0 10px #fbbf24, 0 2px 4px rgba(0,0,0,0.35);
    font-size: 14px !important;
    background: linear-gradient(135deg, #fef9c3, #fde047);
    border: 2px solid #ca8a04;
    border-radius: 20px;
    padding: 3px 8px;
    box-shadow: 0 0 14px #facc15, 0 3px 8px rgba(0,0,0,0.25);
}

/* 💣 Incienso Bendito (5 en L o T - Wrapped Candy) */
.piece-bomb {
    background: radial-gradient(circle, #ffedd5, #fdba74) !important;
    border: 3px double #ea580c !important;
    box-shadow: 0 0 15px #f97316;
    animation: bomb-bounce 1s infinite alternate !important;
}

.piece-bomb::after {
    content: "💣";
    position: absolute;
    top: -4px;
    right: -4px;
    font-size: 13px;
    background: #ffedd5;
    border: 1px solid #ea580c;
    border-radius: 50%;
    width: 18px;
    height: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

/* ☀️ Hostia Divina (5 en línea - Color Bomb) */
.piece-star {
    background: radial-gradient(circle, #ffffff, #fef08a, #facc15) !important;
    border: 2px solid #b45309 !important;
    box-shadow: 0 0 18px #f59e0b, inset 0 0 10px #ffffff;
    animation: holy-spin 3.5s linear infinite !important;
}

.piece-star::after {
    content: "✨";
    position: absolute;
    top: -4px;
    right: -4px;
    font-size: 13px;
    background: #fef9c3;
    border: 1px solid #b45309;
    border-radius: 50%;
    width: 18px;
    height: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

@keyframes pulse-glow {
    0% { filter: brightness(1) scale(1); }
    100% { filter: brightness(1.3) drop-shadow(0 0 10px #f59e0b) scale(1.08); }
}

@keyframes bomb-bounce {
    0% { transform: scale(1); }
    100% { transform: scale(1.1) rotate(3deg); }
}

@keyframes holy-spin {
    0% { transform: rotate(0deg) scale(1); }
    50% { transform: rotate(180deg) scale(1.08); }
    100% { transform: rotate(360deg) scale(1); }
}

/* Arrastre visual */
.dragging-visual {
    position: fixed;
    pointer-events: none;
    z-index: 9999;
    font-size: calc(var(--size) * 1.15);
    transform: translate(-50%, -50%);
    filter: drop-shadow(0 8px 16px rgba(245, 158, 11, 0.6));
    transition: opacity 0.1s;
}

/* Animación de Swap Invalido (Rollback) */
@keyframes swap-rollback {
    0% { transform: scale(1); }
    50% { transform: scale(1.15) rotate(6deg); }
    100% { transform: scale(1); }
}

.swap-invalid {
    animation: swap-rollback 0.3s ease-in-out !important;
}

/* Animación de Match: Estallido de luz dorada */
@keyframes holy-match-glow {
    0% { transform: scale(1); filter: brightness(1); }
    50% { transform: scale(1.4); filter: brightness(2) drop-shadow(0 0 20px #fbbf24); opacity: 0.95; }
    100% { transform: scale(0); opacity: 0; }
}

.match-blink {
    animation: holy-match-glow 0.35s ease-out forwards !important;
}

/* Animación de Holy Rush */
@keyframes rush-pop {
    0% { transform: scale(0.8); filter: brightness(1); }
    50% { transform: scale(1.4); filter: brightness(2) drop-shadow(0 0 20px #f59e0b); }
    100% { transform: scale(1); filter: brightness(1); }
}

.rush-active {
    animation: rush-pop 0.3s ease-out !important;
}

/* Popups Flotantes de Puntos y Combos */
.floating-popup {
    position: absolute;
    pointer-events: none;
    font-family: 'Cinzel', serif;
    font-weight: 900;
    text-shadow: 0 2px 4px rgba(0,0,0,0.3), 0 0 10px rgba(251, 191, 36, 0.8);
    transform: translate(-50%, -50%);
    animation: floatUpAndFade 1.3s cubic-bezier(0.215, 0.61, 0.355, 1) forwards;
    z-index: 30;
}

@keyframes floatUpAndFade {
    0% {
        opacity: 0;
        transform: translate(-50%, -20%) scale(0.5);
    }
    20% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1.25);
    }
    40% {
        transform: translate(-50%, -70%) scale(1);
    }
    100% {
        opacity: 0;
        transform: translate(-50%, -120%) scale(0.85);
    }
}

/* Confeti de Celebración */
.confetti-particle {
    position: absolute;
    pointer-events: none;
    font-size: 20px;
    animation: fallAndRotate 2.5s cubic-bezier(0.25, 0.46, 0.45, 0.94) forwards;
}

@keyframes fallAndRotate {
    0% {
        opacity: 1;
        transform: translateY(-20px) rotate(0deg) scale(0.8);
    }
    100% {
        opacity: 0;
        transform: translateY(100vh) rotate(720deg) scale(1.2);
    }
}

/* Scroll y estética para el modal del mapa en celular y escritorio */
#map-modal, #reset-modal, #reset-confirm-modal, #status-modal {
    touch-action: auto;
}

#levels-grid {
    touch-action: pan-y !important;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: #f59e0b #fef3c7;
}

#levels-grid::-webkit-scrollbar {
    width: 6px;
}

#levels-grid::-webkit-scrollbar-track {
    background: #fef3c7;
    border-radius: 4px;
}

#levels-grid::-webkit-scrollbar-thumb {
    background: #f59e0b;
    border-radius: 4px;
}