/*
==============================================
  M4TH5 Resources Theme for Klotski Puzzle - 3D Enhanced
==============================================
*/

/* --- Color Palette --- */
:root {
    --bg-color: #f5f5ff5; /* Light gray background like M4TH5 site */
    --text-color: #333;
    --puzzle-bg: white;
    --tile-bg-default: #6b7280; /* Light gray for regular tiles */
    --tile-bg-special: #dc2626; /* M4TH5 brand red for the main block */
    --target-zone-bg: rgba(220, 38, 38, 0.1); /* Very light red for target */
    --tile-text-color: #ffffff;
    --border-color: #e5e7eb;
    --button-primary: #dc2626;
    --button-secondary: #6b7280;
    --disabled-grey: #9ca3af;
    --shadow-color: rgba(0, 0, 0, 0.1);
    --card-shadow: rgba(0, 0, 0, 0.1);
}

/* --- General Page Layout --- */
body {
    display: flex;
    justify-content: space-around;
    align-items: flex-start;
    flex-wrap: wrap;
    min-height: 100vh;
    background-color: var(--bg-color);
    color: var(--text-color);
    margin: 0;
    padding: 20px;
    box-sizing: border-box;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    overscroll-behavior-y: contain;
}

/* --- Main Puzzle Area --- */
.puzzle-area { 
    margin: 10px;
    background: white;
    border-radius: 12px;
    padding: 25px;
    box-shadow: 0 2px 10px var(--card-shadow);
    position: relative;

    /* FIX 1: Make the puzzle area a flex container to center its contents. */
    /* This ensures the counter, grid, and status are all aligned nicely. */
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* Add subtle branding touch like the connection wall */
.puzzle-area::before {
    content: "";
    position: absolute;
    top: -3px;
    left: 50%;
    transform: translateX(-50%);
    width: 60px;
    height: 4px;
    background: linear-gradient(90deg, #dc2626, #ef4444);
    border-radius: 2px;
}

.puzzle-container {
    display: grid;
    grid-template-columns: repeat(4, 75px);
    grid-template-rows: repeat(5, 75px);
    gap: 8px;
    position: relative;
    border: 2px solid var(--border-color);
    padding: 8px;
    background-color: var(--puzzle-bg);
    touch-action: none;
    margin-bottom: 20px;
    border-radius: 8px;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05);
    /* Add perspective for 3D effect */
    perspective: 1000px;
}

/* --- Target Zone --- */
#target-zone {
    grid-column: 2 / span 2;
    grid-row: 4 / span 2;
    background-color: var(--target-zone-bg);
    border-radius: 6px;
    pointer-events: none;
    z-index: 1;
    border: 2px dashed rgba(220, 38, 38, 0.3);
}

/* --- Individual Tiles with 3D Effect --- */
.tile {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 18px;
    font-weight: 600;
    background: linear-gradient(135deg, var(--tile-bg-default), #9ca3af);
    border: 2px solid var(--border-color);
    color: var(--tile-text-color);
    cursor: grab;
    position: relative;
    box-sizing: border-box;
    transition: all 0.3s ease-out;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    background-clip: padding-box;
    border-radius: 8px;
    z-index: 10;
    
    /* Enhanced 3D styling */
    box-shadow: 
        /* Main elevated shadow */
        0 4px 8px rgba(0, 0, 0, 0.15),
        0 2px 4px rgba(0, 0, 0, 0.1),
        /* Top highlight for 3D effect */
        inset 0 2px 4px rgba(255, 255, 255, 0.4),
        /* Bottom inner shadow for depth */
        inset 0 -2px 4px rgba(0, 0, 0, 0.15),
        /* Left side highlight */
        inset 2px 0 4px rgba(255, 255, 255, 0.2),
        /* Right side shadow */
        inset -2px 0 4px rgba(0, 0, 0, 0.1);
    
    /* Subtle 3D transform */
    transform: translateZ(0) rotateX(2deg);
    transform-style: preserve-3d;
}

/* Add a subtle pseudo-element for extra depth */
.tile:not(.empty)::before {
    content: '';
    position: absolute;
    top: -2px;
    left: -2px;
    right: -2px;
    bottom: -2px;
    background: linear-gradient(135deg, rgba(255,255,255,0.1), rgba(0,0,0,0.05));
    border-radius: 10px;
    z-index: -1;
    opacity: 0.8;
}

.tile:hover:not(.empty) {
    transform: translateY(-3px) translateZ(5px) rotateX(3deg);
    box-shadow: 
        /* Enhanced hover shadow */
        0 8px 16px rgba(107, 114, 128, 0.25),
        0 4px 8px rgba(107, 114, 128, 0.15),
        /* Stronger 3D inset effects */
        inset 0 3px 6px rgba(255, 255, 255, 0.5),
        inset 0 -3px 6px rgba(0, 0, 0, 0.2),
        inset 3px 0 6px rgba(255, 255, 255, 0.3),
        inset -3px 0 6px rgba(0, 0, 0, 0.15);
}

.tile:active:not(.empty) {
    transform: translateY(-1px) translateZ(2px) rotateX(1deg);
    box-shadow: 
        /* Pressed state shadow */
        0 2px 4px rgba(107, 114, 128, 0.2),
        inset 0 1px 3px rgba(255, 255, 255, 0.3),
        inset 0 -1px 3px rgba(0, 0, 0, 0.2),
        inset 2px 0 3px rgba(255, 255, 255, 0.2),
        inset -2px 0 3px rgba(0, 0, 0, 0.1);
}

/* --- Special Tile: The Red 2x2 Square with Enhanced 3D --- */
.tile-2x2 {
    background: linear-gradient(135deg, var(--tile-bg-special), #ef4444) !important;
    border-color: var(--tile-bg-special) !important;
    font-size: 20px;
    font-weight: 700;
    
    /* Enhanced 3D for the special tile */
    box-shadow: 
        /* Main elevated shadow with red tint */
        0 6px 12px rgba(220, 38, 38, 0.25),
        0 3px 6px rgba(220, 38, 38, 0.15),
        /* Top highlight */
        inset 0 3px 6px rgba(255, 255, 255, 0.4),
        /* Bottom inner shadow */
        inset 0 -3px 6px rgba(139, 0, 0, 0.3),
        /* Left side highlight */
        inset 3px 0 6px rgba(255, 255, 255, 0.25),
        /* Right side shadow */
        inset -3px 0 6px rgba(139, 0, 0, 0.2);
        
    transform: translateZ(2px) rotateX(3deg);
}

.tile-2x2::before {
    background: linear-gradient(135deg, rgba(255,255,255,0.15), rgba(139,0,0,0.1));
}

.tile-2x2:hover {
    transform: translateY(-4px) translateZ(8px) rotateX(4deg);
    box-shadow: 
        /* Enhanced red hover shadow */
        0 10px 20px rgba(220, 38, 38, 0.3),
        0 6px 12px rgba(220, 38, 38, 0.2),
        /* Stronger 3D inset effects */
        inset 0 4px 8px rgba(255, 255, 255, 0.5),
        inset 0 -4px 8px rgba(139, 0, 0, 0.4),
        inset 4px 0 8px rgba(255, 255, 255, 0.3),
        inset -4px 0 8px rgba(139, 0, 0, 0.25) !important;
}

.tile-2x2:active {
    transform: translateY(-2px) translateZ(4px) rotateX(2deg);
    box-shadow: 
        0 4px 8px rgba(220, 38, 38, 0.25),
        inset 0 2px 4px rgba(255, 255, 255, 0.4),
        inset 0 -2px 4px rgba(139, 0, 0, 0.3),
        inset 2px 0 4px rgba(255, 255, 255, 0.25),
        inset -2px 0 4px rgba(139, 0, 0, 0.2);
}

/* --- Empty Tile Styling --- */
.empty {
    background: transparent;
    cursor: default;
    box-shadow: none;
    border: none;
    z-index: 5;
    transform: none;
}

.empty::before {
    display: none;
}

/* --- Winner Animation with Enhanced 3D --- */
@keyframes winnerAnimation {
    0%   { 
        background: linear-gradient(135deg, var(--tile-bg-special), #ef4444);
        transform: translateZ(5px) rotateX(3deg) scale(1);
        box-shadow: 
            0 8px 16px rgba(220, 38, 38, 0.3),
            inset 0 3px 6px rgba(255, 255, 255, 0.4),
            inset 0 -3px 6px rgba(139, 0, 0, 0.3);
    }
    50%  { 
        background: linear-gradient(135deg, #ef4444, var(--tile-bg-special));
        transform: translateZ(8px) rotateX(4deg) scale(1.03);
        box-shadow: 
            0 12px 24px rgba(239, 68, 68, 0.35),
            inset 0 4px 8px rgba(255, 255, 255, 0.5),
            inset 0 -4px 8px rgba(139, 0, 0, 0.4);
    }
    100% { 
        background: linear-gradient(135deg, var(--tile-bg-special), #ef4444);
        transform: translateZ(5px) rotateX(3deg) scale(1);
        box-shadow: 
            0 8px 16px rgba(220, 38, 38, 0.3),
            inset 0 3px 6px rgba(255, 255, 255, 0.4),
            inset 0 -3px 6px rgba(139, 0, 0, 0.3);
    }
}

.winner {
    animation: winnerAnimation 1.2s infinite;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 22px;
    color: var(--tile-text-color);
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.6);
    cursor: default !important;
    font-weight: 700;
}

/* --- Controls & Status Area --- */
.controls-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin: 10px;
    padding: 25px;
    background-color: white;
    border: 2px solid var(--border-color);
    border-radius: 12px;
    box-shadow: 0 2px 10px var(--card-shadow);
    position: relative;
}

.controls-area h1 {
    color: var(--tile-bg-special);
    margin-bottom: 20px;
    font-weight: 700;
    font-size: 1.8em;
    text-align: center;
}

.action-buttons {
    margin-bottom: 25px;
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    justify-content: center;
}

.action-buttons button {
    padding: 12px 20px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    border: 2px solid;
    border-radius: 8px;
    color: white;
    transition: all 0.2s ease;
    box-shadow: 0 2px 4px var(--shadow-color);
}

.action-buttons button:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 4px 8px var(--shadow-color);
}

.action-buttons button:active:not(:disabled) { 
    transform: translateY(0); 
}

/* --- Enhanced button animations --- */
@keyframes flashButton {
    0% { 
        background-color: var(--button-secondary);
        box-shadow: 0 2px 4px var(--shadow-color);
    }
    50% { 
        background-color: #ef4444;
        border-color: #ef4444;
        box-shadow: 0 4px 12px rgba(239, 68, 68, 0.4);
        transform: translateY(-1px);
    }
    100% { 
        background-color: var(--button-secondary);
        box-shadow: 0 2px 4px var(--shadow-color);
    }
}

.flash-button {
    animation: flashButton 1.5s infinite;
}

.flash-button:hover:not(:disabled) {
    animation: none;
    background-color: #4b5563 !important;
    border-color: #4b5563 !important;
}

#solveButton { 
    background-color: var(--button-primary);
    border-color: var(--button-primary);
}

#solveButton:hover:not(:disabled) {
    background-color: #b91c1c;
    border-color: #b91c1c;
}

#undoSolveButton { 
    background-color: var(--button-secondary);
    border-color: var(--button-secondary);
}

#undoSolveButton:hover:not(:disabled) {
    background-color: #4b5563;
    border-color: #4b5563;
}

#solveButton:disabled, #undoSolveButton:disabled {
    background-color: var(--disabled-grey);
    border-color: var(--disabled-grey);
    color: #ffffff;
    cursor: not-allowed;
    transform: none;
    box-shadow: 0 1px 2px var(--shadow-color);
}

#moveCounter {
    font-size: 1.3em;
    font-weight: 700;
    margin-bottom: 15px;
    color: var(--text-color);
    background: #fef2f2;
    padding: 10px 20px;
    border-radius: 8px;
    border: 1px solid #fecaca;
    text-align: center;
}

#status {
    min-height: 1.2em;
    font-style: italic;
    color: var(--tile-bg-special);
    font-weight: 600;
    margin-bottom: 20px;
    text-align: center;
    font-size: 1.1em;
    max-width: 340px; 
    margin-left: auto;
    margin-right: auto;
}

.instructions {
    text-align: center;
    line-height: 1.5;
    color: var(--text-color);
}

.objective {
    background: #fef2f2;
    padding: 15px;
    border-radius: 8px;
    border: 1px solid #fecaca;
    margin-bottom: 15px;
}

.objective p {
    margin: 0;
    color: var(--tile-bg-special);
    font-weight: 600;
}

.instructions p {
    margin: 8px 0;
    font-weight: 500;
}

#debug {
    margin-top: 15px;
    font-size: 12px;
    color: #6b7280;
    text-align: center;
    font-style: italic;
}

/* --- Responsive Design --- */
@media (max-width: 768px) {
    body {
        flex-direction: column;
        align-items: center;
        padding: 15px;
    }
    
    .puzzle-area {
        /* FIX 2: Let the container size itself to its content. */
        /* This prevents it from stretching too wide on landscape screens. */
        width: fit-content;
        max-width: 100%; /* Ensure it doesn't overflow the viewport */
        margin: 10px 0;
    }

    .controls-area {
        width: 100%;
        max-width: 400px;
        min-width: 320px; /* Prevent it from getting too squished */
        margin: 10px 0;
    }
}

@media (max-width: 400px) {
    .puzzle-area {
        padding: 15px;
        /* On the smallest screens, allow it to fill the width again */
        width: 100%;
    }

    .puzzle-container {
        /* Define responsive sizes using CSS variables based on viewport width */
        --tile-size: 20vw;
        --gap-size: 2vw;

        /* Apply these variables to the grid */
        grid-template-columns: repeat(4, var(--tile-size));
        grid-template-rows: repeat(5, var(--tile-size));
        gap: var(--gap-size);
        padding: var(--gap-size);
    }
}