:root {
    --bg-color: #e0f7fa;
    --primary-color: #4a90e2;
    --secondary-color: #ffc107;
    --accent-color: #ff4081;
    --card-bg: #ffffff;
    --text-color: #333333;
    --magic-blue: #40c4ff;
    --magic-green: #69f0ae;
    --magic-yellow: #ffd740;
    --magic-red: #ff5252;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Comic Sans MS', 'Chalkboard SE', 'Comic Neue', sans-serif;
    user-select: none;
    -webkit-user-drag: none;
    -webkit-tap-highlight-color: transparent;
}

body {
    background: linear-gradient(135deg, #e0f7fa 0%, #80deea 100%);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 2rem;
    overflow-x: hidden;
}

/* --- Entry Page Header --- */
header.entry-header {
    text-align: center;
    margin-bottom: 3rem;
    z-index: 10;
}

header.entry-header h1 {
    font-size: clamp(2.5rem, 6vw, 4rem);
    color: var(--primary-color);
    text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
    margin-bottom: 0.5rem;
    font-weight: 900;
}

header.entry-header p.subtitle {
    font-size: 1.5rem;
    color: #555;
    font-weight: 600;
}

/* --- Puzzle Grid Layout --- */
.level-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 2.5rem;
    width: 100%;
    max-width: 1100px;
    z-index: 10;
    margin-bottom: 1rem;
}

.level-card {
    background: var(--card-bg);
    border-radius: 24px;
    padding: 1.8rem;
    box-shadow: 0 12px 30px rgba(0,0,0,0.08);
    display: flex;
    flex-direction: column;
    align-items: center;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    text-decoration: none;
    color: var(--text-color);
    border: 4px solid #fff;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.level-card:hover {
    transform: scale(1.05) translateY(-10px);
    box-shadow: 0 20px 40px rgba(0,0,0,0.15);
    border-color: var(--secondary-color);
}

.level-card.selected {
    border-color: var(--primary-color);
    background: #f0faff;
    box-shadow: 0 15px 35px rgba(74, 144, 226, 0.2);
}

.level-card .thumbnail {
    width: 100%;
    height: 190px;
    background-size: cover;
    background-position: center;
    border-radius: 18px;
    margin-bottom: 1.5rem;
    border: 3px solid #f8f9fa;
}

.level-card h2 {
    font-size: 2rem;
    margin-bottom: 0.5rem;
    color: #2c3e50; /* Strong contrast */
    font-weight: 800;
}

.level-card .description {
    font-size: 1.2rem;
    color: #5d6d7e; /* Stronger contrast than #777 */
    margin-bottom: 1.5rem;
    text-align: center;
    line-height: 1.4;
}

.play-btn {
    background: var(--primary-color);
    color: white;
    padding: 0.9rem 3rem;
    border-radius: 50px;
    font-size: 1.3rem;
    font-weight: 800;
    border: none;
    box-shadow: 0 6px 15px rgba(74, 144, 226, 0.3);
    cursor: pointer;
    transition: all 0.2s;
}

.play-btn:hover {
    transform: scale(1.05) translateY(-2px);
    box-shadow: 0 8px 20px rgba(74, 144, 226, 0.4);
}

/* Specific colors for cards */
.level-card:nth-child(1) { border-bottom: 10px solid #4a90e2; }
.level-card:nth-child(2) { border-bottom: 10px solid #ffc107; }
.level-card:nth-child(3) { border-bottom: 10px solid #2ecc71; }

.level-card:nth-child(1) .play-btn { background: #4a90e2; }
.level-card:nth-child(2) .play-btn { background: #ffc107; }
.level-card:nth-child(3) .play-btn { background: #2ecc71; }

.level-card.locked {
    cursor: not-allowed;
}

.level-card.locked .thumbnail {
    filter: blur(2px) grayscale(0.5);
    opacity: 0.6;
}

.lock-icon {
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 4rem;
    background: rgba(255,255,255,0.4);
    backdrop-filter: blur(3px); /* Reduced blur */
    z-index: 5;
}

/* --- The Selection UI Container --- */
.menu-overlay {
    width: 100%;
    max-width: 1200px;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 1rem 1rem 5rem 1rem;
    animation: fadeIn 0.6s ease-out;
}

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

.selection-footer {
    width: 100%;
    max-width: 900px;
    margin-top: 2rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
    z-index: 10;
}

/* --- Difficulty Selector --- */
.difficulty-selector {
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(10px);
    padding: 2.5rem;
    border-radius: 40px;
    width: 100%;
    box-shadow: 0 10px 40px rgba(0,0,0,0.06);
    border: 2px solid #fff;
}

.difficulty-selector h3 {
    font-size: 1.8rem;
    color: #2c3e50;
    margin-bottom: 1.5rem;
    font-weight: 800;
}

.diff-buttons {
    display: flex;
    gap: 1.5rem;
}

.diff-btn {
    flex: 1;
    background: #fff;
    border: 4px solid transparent;
    border-radius: 25px;
    padding: 1.2rem;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    box-shadow: 0 6px 15px rgba(0,0,0,0.04);
}

.diff-btn:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
}

.diff-btn.selected {
    border-color: var(--primary-color);
    background: #f0faff;
    transform: scale(1.05);
}

.diff-btn small {
    display: block;
    font-size: 0.9rem;
    opacity: 0.7;
    margin-top: 5px;
    font-weight: 600;
}

.btn-large {
    padding: 1.5rem 6rem;
    font-size: 2.2rem;
    margin-top: 1rem;
    box-shadow: 0 10px 30px rgba(74, 144, 226, 0.4);
}


/* Back to Hub Button */
.btn-back-hub {
    position: fixed;
    top: 20px;
    right: 20px;
    background: white;
    color: var(--primary-color);
    padding: 12px 24px;
    border-radius: 50px;
    text-decoration: none;
    font-weight: bold;
    font-size: 1.1rem;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    z-index: 1000;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    border: 3px solid var(--primary-color);
    display: flex;
    align-items: center;
    gap: 10px;
}

.btn-back-hub:hover {
    transform: scale(1.05) translateY(-3px);
    box-shadow: 0 8px 25px rgba(0,0,0,0.2);
    background: var(--primary-color);
    color: white;
}

/* System Controls (Speaker/Music) */
.system-controls {
    position: fixed;
    top: 20px;
    left: 20px;
    display: flex;
    gap: 12px;
    z-index: 1000;
}

.speaker-btn, .music-toggle {
    width: 55px;
    height: 55px;
    border-radius: 50%;
    border: 3px solid var(--primary-color);
    background: white;
    font-size: 1.5rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 10px rgba(0,0,0,0.1);
    transition: all 0.3s ease;
}

.speaker-btn:hover, .music-toggle:hover {
    transform: scale(1.1);
    background: #f0f7ff;
    box-shadow: 0 6px 15px rgba(0,0,0,0.15);
}

/* --- Mobile Mastery --- */
@media (max-width: 600px) {
    .menu-content {
        padding: 30px 15px;
        border-radius: 40px;
        border-width: 8px;
        margin: 10px;
    }

    h1 { font-size: 2.8rem; }
    h2 { font-size: 1.1rem; margin-bottom: 20px; }

    .thumbnails { grid-template-columns: repeat(2, 1fr); gap: 10px; }
    .thumbnail-wrapper { border-radius: 25px; }

    .difficulty-selector { padding: 15px; }
    .diff-buttons { flex-direction: column; }
    .diff-btn { padding: 15px; }

    .btn-start {
        width: 100%;
        padding: 20px 10px;
        font-size: 1.8rem;
        box-shadow: 0 10px 0 #1b5e20;
    }

    .btn-back-hub {
        top: 15px;
        right: 15px;
        padding: 10px 20px;
        font-size: 0.9rem;
    }
}

/* Gameplay Overrides - Restoring Critical Layout */
.game-container {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    padding: 15px;
    z-index: 10;
    max-width: 1200px;
}

header {
    background: var(--panel-bg);
    padding: 15px 30px;
    border-radius: 30px;
    margin-bottom: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 10px 0 rgba(0,0,0,0.05);
    border: 6px solid #fff;
    flex-shrink: 0;
}

header h1 {
    font-size: 1.8rem;
    margin-bottom: 0;
    filter: none;
    -webkit-text-fill-color: initial;
    background: none;
    color: var(--magic-blue);
}

.stats-panel {
    display: flex;
    gap: 15px;
    align-items: center;
}

.stat-box {
    background: #f5f5f5;
    padding: 8px 18px;
    border-radius: 20px;
    font-weight: 800;
    text-align: center;
}

.stat-label { font-size: 0.8rem; opacity: 0.6; }
.stat-value { color: var(--magic-red); font-size: 1.3rem; }

.progress-bar-container {
    flex-grow: 1;
    margin: 0 20px;
    background: #eee;
    height: 18px;
    border-radius: 50px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--magic-green), #00c853);
    border-radius: 50px;
    transition: width 0.3s ease;
}

.game-board {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    gap: 15px;
    min-height: 0;
    position: relative;
}

/* Puzzle Grid Area */
.puzzle-grid {
    flex-shrink: 0;
    margin: 0 auto;
    background: rgba(255, 255, 255, 0.4);
    border: 4px dashed rgba(255, 255, 255, 0.6);
    border-radius: 24px;
    position: relative;
    backdrop-filter: blur(5px);
    overflow: visible;
}

.puzzle-slot {
    position: absolute;
    box-sizing: border-box;
    border: 2px dashed rgba(255, 255, 255, 0.4);
    pointer-events: none;
}

.pieces-tray {
    flex-grow: 1;
    background: rgba(255, 255, 255, 0.4);
    border-radius: 40px;
    padding: 15px;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-content: flex-start;
    gap: 15px;
    overflow-y: auto;
    border: 4px dashed rgba(255, 255, 255, 0.8);
    min-height: 120px;
}

/* Draggable Piece Base */
.puzzle-piece {
    position: relative;
    cursor: grab;
    background-repeat: no-repeat;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    touch-action: none;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    transform: translateZ(0);
    z-index: 10;
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275), box-shadow 0.2s;
    /* Do NOT transition left/top to avoid lag during drag */
}

.puzzle-piece.placed {
    cursor: default;
    box-shadow: none;
    border-radius: 0;
    z-index: 1;
    transition: none;
}

/* Squishy Dragging Piece */
.puzzle-piece.dragging {
    cursor: grabbing;
    transform: scale(1.15) rotate(-3deg) !important;
    box-shadow: 0 20px 40px rgba(0,0,0,0.3) !important;
    z-index: 100000;
}
/* Confetti Celebration */
.confetti {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 999999;
}

.confetti.hidden { display: none; }


/* Visibility Utilities */
.hidden { display: none !important; }

/* Modal System Restoration */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10000;
    opacity: 0;
    visibility: hidden;
    transition: all 0.4s ease;
}

.modal.active {
    opacity: 1;
    visibility: visible;
}

.modal-content {
    background: #fff;
    padding: 40px;
    border-radius: 50px;
    text-align: center;
    box-shadow: 0 30px 60px rgba(0,0,0,0.3);
    max-width: 500px;
    width: 90%;
    transform: scale(0.8) translateY(20px);
    transition: transform 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.modal.active .modal-content {
    transform: scale(1) translateY(0);
}

.modal-title {
    font-size: 2.5rem;
    color: var(--magic-blue);
    margin-bottom: 20px;
    font-weight: 900;
}

.modal-text {
    font-size: 1.4rem;
    margin-bottom: 30px;
    line-height: 1.4;
}

.modal-actions {
    display: flex;
    flex-direction: column;
    gap: 15px;
    align-items: center;
}

.btn-modal {
    padding: 18px 45px;
    border-radius: 100px;
    border: none;
    font-size: 1.4rem;
    font-weight: 900;
    cursor: pointer;
    transition: var(--transition-bounce);
    text-decoration: none;
    display: inline-block;
}

.btn-modal-primary {
    background: var(--magic-green);
    color: #1b5e20;
    box-shadow: 0 8px 0 #388e3c;
}

.btn-modal-secondary {
    background: #fff;
    color: var(--text-color);
    box-shadow: 0 8px 0 #e0e0e0;
}

.btn-modal-danger {
    background: var(--magic-red);
    color: #fff;
    box-shadow: 0 8px 0 #b71c1c;
}

.btn-modal:hover { transform: translateY(-5px); }
.btn-modal:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(0,0,0,0.1); }


/* Responsive Design */
@media (max-width: 768px) {
    body { padding: 1rem; }
    
    header.entry-header h1 { font-size: 2.2rem; }
    header.entry-header p.subtitle { font-size: 1.1rem; }
    
    .level-grid { grid-template-columns: 1fr; gap: 1.5rem; }
    
    .btn-back-hub {
        top: 10px;
        right: 10px;
        width: 50px;
        height: 50px;
        padding: 0;
        justify-content: center;
        border-radius: 50%;
    }

    .btn-back-hub .text { display: none; }
    .btn-back-hub .emoji { font-size: 1.5rem; }

    .system-controls {
        top: 10px;
        left: 10px;
        gap: 8px;
    }

    .speaker-btn, .music-toggle {
        width: 50px;
        height: 50px;
        font-size: 1.2rem;
    }

    /* Gameplay Specific Layout */
    .game-container { padding: 5px; }
    
    .game-container header {
        padding: 10px 15px;
        border-radius: 20px;
        margin-bottom: 10px;
        flex-direction: column;
        gap: 8px;
    }

    .game-container header h1 { 
        font-size: 1.3rem;
        order: 1;
    }

    .stats-panel { 
        width: 100%;
        justify-content: center;
        order: 3;
    }

    .progress-bar-container { 
        width: 100%;
        margin: 5px 0;
        order: 2;
    }

    #menu-btn { display: none; } /* Using the hub button instead */

    .pieces-tray {
        padding: 10px;
        gap: 10px;
        border-radius: 25px;
    }
}

/* Snapping Fix Patch - KEEP THIS */
.puzzle-slot {
    border: 3px dashed rgba(255, 255, 255, 0.6) !important;
    background: rgba(255, 255, 255, 0.1) !important;
}

.pieces-tray {
    transform: none !important;
    z-index: 10;
}
