/* --- Variables & Base Styling --- */
:root {
    --bg-dark: #0b0c10;
    --panel-bg: rgba(31, 40, 51, 0.6);
    --neon-cyan: #66fcf1;
    --neon-teal: #45a29e;
    --neon-purple: #c70039; /* Magenta accent */
    --text-main: #c5c6c7;
    --grid-size: 4; /* Overridden by JS */
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none; /* Prevent text selection during drag */
}

body {
    background: radial-gradient(circle at center, #1f2833 0%, var(--bg-dark) 100%);
    color: var(--text-main);
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow-x: hidden;
}

#app-container {
    width: 100%;
    max-width: 1000px;
    padding: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.hidden {
    display: none !important;
}

/* --- Glassmorphism & Neon Aesthetics --- */
.glass-panel {
    background: var(--panel-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid rgba(102, 252, 241, 0.2);
    border-radius: 15px;
    padding: 20px;
    box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
}

.neon-text {
    color: #fff;
    text-shadow: 0 0 5px #fff, 0 0 10px #fff, 0 0 20px var(--neon-cyan), 0 0 40px var(--neon-teal);
    text-align: center;
    margin-bottom: 20px;
    font-size: 2.2rem;
}

.neon-text-sm {
    color: var(--neon-cyan);
    text-shadow: 0 0 5px var(--neon-cyan);
    font-size: 1.2rem;
    margin-bottom: 5px;
}

.neon-box {
    box-shadow: 0 0 10px rgba(102, 252, 241, 0.3), inset 0 0 10px rgba(102, 252, 241, 0.1);
}

/* --- Buttons --- */
.btn {
    background: transparent;
    border: 1px solid var(--neon-teal);
    color: var(--neon-cyan);
    padding: 10px 20px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 1rem;
    font-weight: bold;
    transition: all 0.3s ease;
}

.btn:hover {
    background: rgba(102, 252, 241, 0.1);
    box-shadow: 0 0 15px var(--neon-cyan);
}

.primary-btn {
    display: block;
    width: 100%;
    margin-top: 20px;
    background: rgba(69, 162, 158, 0.2);
    font-size: 1.2rem;
    padding: 15px;
}

.difficulty-selectors {
    display: flex;
    gap: 10px;
    justify-content: center;
    margin: 20px 0;
}

.diff-btn.active {
    background: var(--neon-cyan);
    color: var(--bg-dark);
    box-shadow: 0 0 15px var(--neon-cyan);
}

.icon-btn {
    padding: 5px 10px;
    font-size: 0.9rem;
}

/* --- Start Screen & Previews --- */
#start-screen {
    max-width: 500px;
    width: 100%;
    text-align: center;
}

#preview-container {
    width: 100%;
    height: 250px;
    border: 2px dashed var(--neon-teal);
    border-radius: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    margin-top: 15px;
    position: relative;
}

#meme-preview {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

/* Loading Spinner */
#loading-spinner {
    width: 50px;
    height: 50px;
    border: 5px solid rgba(102, 252, 241, 0.2);
    border-top-color: var(--neon-cyan);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin { 100% { transform: rotate(360deg); } }

/* --- Game Screen Layout --- */
#game-screen {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 20px;
    animation: fadeIn 0.5s ease;
}

@keyframes fadeIn { from { opacity: 0; transform: scale(0.95); } to { opacity: 1; transform: scale(1); } }

.game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
}

.progress-container {
    flex-grow: 1;
    max-width: 300px;
    text-align: center;
}

.progress-bar {
    width: 100%;
    height: 8px;
    background: rgba(255,255,255,0.1);
    border-radius: 4px;
    margin-top: 5px;
    overflow: hidden;
}

#progress-fill {
    height: 100%;
    width: 0%;
    background: var(--neon-cyan);
    box-shadow: 0 0 10px var(--neon-cyan);
    transition: width 0.3s ease;
}

.game-workspace {
    display: flex;
    gap: 20px;
    align-items: flex-start;
}

/* --- Puzzle Board --- */
#puzzle-board-container {
    flex: 1;
    max-width: 500px;
    width: 100%;
    aspect-ratio: 1 / 1;
    margin: 0 auto;
    padding: 5px;
}

#puzzle-board {
    width: 100%;
    height: 100%;
    display: grid;
    /* Grid size injected via inline styles in JS */
    gap: 2px;
    background: rgba(255, 255, 255, 0.05);
}

.board-slot {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    border: 1px dashed rgba(102, 252, 241, 0.3);
    position: relative;
}

.board-slot.drag-over {
    background: rgba(102, 252, 241, 0.2);
    box-shadow: inset 0 0 15px var(--neon-cyan);
}

/* --- Piece Tray --- */
#piece-tray-container {
    flex: 1;
    min-height: 300px;
    display: flex;
    flex-direction: column;
}

#piece-tray-container h3 {
    text-align: center;
    color: var(--neon-teal);
    margin-bottom: 15px;
}

#piece-tray {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
    align-content: flex-start;
    flex-grow: 1;
    padding: 10px;
    overflow-y: auto;
}

/* --- Puzzle Pieces --- */
.puzzle-piece {
    cursor: grab;
    background-repeat: no-repeat;
    /* Background size and position dynamically set via JS mapped to percentages */
    box-shadow: 0 4px 6px rgba(0,0,0,0.5), inset 0 0 8px rgba(255,255,255,0.3);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 4px;
    transition: transform 0.1s ease, box-shadow 0.2s ease;
    /* Fixed small size for tray */
    width: 70px; 
    height: 70px;
}

.puzzle-piece:hover {
    box-shadow: 0 0 15px var(--neon-cyan);
    z-index: 10;
}

.puzzle-piece:active {
    cursor: grabbing;
}

/* When dragging native */
.puzzle-piece.dragging {
    opacity: 0.5;
    transform: scale(1.1);
}

/* When touch dragging */
.puzzle-piece.touch-dragging {
    position: fixed;
    z-index: 9999;
    pointer-events: none; /* Crucial for elementFromPoint to see slots below */
    transform: scale(1.2);
    box-shadow: 0 10px 25px rgba(102, 252, 241, 0.8);
    opacity: 0.9;
}

/* When placed in grid */
.board-slot .puzzle-piece {
    width: 100%;
    height: 100%;
    border-radius: 0;
    border: none;
    box-shadow: none;
    cursor: default;
    transform: none !important;
    animation: snapIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes snapIn {
    0% { transform: scale(1.2); opacity: 0.5; }
    100% { transform: scale(1); opacity: 1; box-shadow: 0 0 20px var(--neon-cyan); }
}

/* --- Win Modal --- */
#win-modal {
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(5px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal-content {
    max-width: 400px;
    width: 90%;
    text-align: center;
    animation: popIn 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes popIn {
    0% { transform: scale(0.5); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

#win-meme-image {
    width: 100%;
    border-radius: 8px;
    margin: 15px 0;
    box-shadow: 0 0 15px rgba(255, 255, 255, 0.2);
}

.stats p {
    margin: 5px 0;
    font-size: 1.1rem;
    color: var(--neon-cyan);
}

#confetti-canvas {
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    pointer-events: none;
    z-index: 999;
}

/* --- Responsiveness --- */
@media (max-width: 768px) {
    .game-workspace {
        flex-direction: column;
    }
    #piece-tray {
        max-height: 250px;
    }
    .puzzle-piece {
        width: 55px; height: 55px; /* Smaller tray pieces for mobile */
    }
}