/* Grid Container */
#grid-container {
    display: inline-grid;
    gap: var(--grid-gap);
    padding: 1rem;
    background: var(--surface);
    border-radius: 12px;
    border: 1px solid var(--border);
    position: relative; /* Required for absolute positioning of player */
    /* Allow browser to handle scrolling naturally */
    touch-action: auto;
}

/* Tile Styles - Medieval Dungeon Theme with Stone Texture */
.tile {
    width: var(--tile-size);
    height: var(--tile-size);
    background: #4a4a4a;
    border-radius: 2px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    transition: all var(--transition-speed);
    font-size: 1.5rem;
    font-weight: 700;
    border: 2px solid #2a2a2a;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3), inset 0 -1px 2px rgba(255, 255, 255, 0.1);
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    image-rendering: crisp-edges;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    /* Allow browser to handle scrolling */
    touch-action: auto;
}

/* Stone texture overlay using pseudo-element */
.tile::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image:
        radial-gradient(circle at 20% 30%, rgba(255, 255, 255, 0.08) 1px, transparent 1px),
        radial-gradient(circle at 60% 70%, rgba(0, 0, 0, 0.1) 1px, transparent 1px),
        radial-gradient(circle at 80% 20%, rgba(255, 255, 255, 0.05) 1px, transparent 1px),
        radial-gradient(circle at 30% 80%, rgba(0, 0, 0, 0.08) 1px, transparent 1px),
        radial-gradient(circle at 90% 60%, rgba(255, 255, 255, 0.06) 1px, transparent 1px);
    background-size: 8px 8px, 10px 10px, 6px 6px, 12px 12px, 7px 7px;
    pointer-events: none;
    border-radius: inherit;
    opacity: 0.6;
}

/* Tile States */
.tile.ground {
    background: #538d4e;
    border-color: #6aaa64;
}

.tile.ice {
    background: #4a8cc7;
    border-color: #5ca3e8;
}

.tile.grass {
    background: #7fad5d;
    border-color: #96c46f;
}

/* Tile Values - 8-bit Dungeon Theme with Vibrant Colors
 * To use image textures instead of solid colors, uncomment the background-image lines
 * and place tile images in /web/assets/tiles/ (e.g., tile5.png, tile4.png, etc.)
 */
.tile[data-value="6"] {
    background: #8b6914;
    /* background-image: url('../assets/tiles/tile6.png'); */
    border: 3px solid #5a4209;
    color: #ffeab3;
    box-shadow: inset 0 -3px 0 #5a4209, 0 3px 0 #000;
}

.tile[data-value="5"] {
    background: #8b4789;
    /* background-image: url('../assets/tiles/tile5.png'); */
    border: 3px solid #5a2d58;
    color: #ffd1ff;
    box-shadow: inset 0 -3px 0 #5a2d58, 0 3px 0 #000;
}

.tile[data-value="4"] {
    background: #c74440;
    /* background-image: url('../assets/tiles/tile4.png'); */
    border: 3px solid #8b2e2b;
    color: #ffe5e5;
    box-shadow: inset 0 -3px 0 #8b2e2b, 0 3px 0 #000;
}

.tile[data-value="3"] {
    background: #d97938;
    /* background-image: url('../assets/tiles/tile3.png'); */
    border: 3px solid #a55525;
    color: #fff5e5;
    box-shadow: inset 0 -3px 0 #a55525, 0 3px 0 #000;
}

.tile[data-value="2"] {
    background: #e6c547;
    /* background-image: url('../assets/tiles/tile2.png'); */
    border: 3px solid #b89a2f;
    color: #fffde5;
    box-shadow: inset 0 -3px 0 #b89a2f, 0 3px 0 #000;
}

.tile[data-value="1"] {
    background: #67b347;
    /* background-image: url('../assets/tiles/tile1.png'); */
    border: 3px solid #4a8332;
    color: #f0ffe5;
    box-shadow: inset 0 -3px 0 #4a8332, 0 3px 0 #000;
}

.tile[data-value="0"] {
    background: #444444;
    /* background-image: url('../assets/tiles/tile0.png'); */
    color: #888888;
    border: 3px solid #222222;
    box-shadow: inset 0 -3px 0 #222222, 0 3px 0 #000;
}

/* Wall Tile - Old dungeon stone wall (8-bit style) */
.tile.wall {
    background: #736357;
    /* background-image: url('../assets/tiles/wall.png'); */
    border: 3px solid #4a3d33;
    box-shadow: inset 0 -3px 0 #4a3d33, 0 3px 0 #000;
    cursor: not-allowed;
    position: relative;
}

/* Stone brick pattern using pseudo-element */
.tile.wall::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image:
        /* Horizontal mortar lines */
        linear-gradient(to bottom, transparent 45%, #5a4d42 45%, #5a4d42 48%, transparent 48%, transparent 95%, #5a4d42 95%, #5a4d42 98%, transparent 98%),
        /* Vertical mortar lines (staggered) */
        linear-gradient(to right, transparent 48%, #5a4d42 48%, #5a4d42 52%, transparent 52%);
    background-size: 100% 50%, 50% 100%;
    background-position: 0 0, 0 0;
    pointer-events: none;
    border-radius: inherit;
    opacity: 0.7;
}

/* Stone weathering and cracks */
.tile.wall::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image:
        /* Dark spots and weathering */
        radial-gradient(circle at 20% 20%, rgba(0, 0, 0, 0.2) 2px, transparent 2px),
        radial-gradient(circle at 70% 40%, rgba(0, 0, 0, 0.15) 3px, transparent 3px),
        radial-gradient(circle at 40% 75%, rgba(0, 0, 0, 0.18) 2px, transparent 2px),
        radial-gradient(circle at 85% 80%, rgba(0, 0, 0, 0.12) 2px, transparent 2px),
        /* Light spots */
        radial-gradient(circle at 15% 60%, rgba(255, 255, 255, 0.1) 2px, transparent 2px),
        radial-gradient(circle at 60% 25%, rgba(255, 255, 255, 0.08) 2px, transparent 2px);
    background-size: 100% 100%;
    pointer-events: none;
    border-radius: inherit;
    opacity: 0.8;
    z-index: 1;
}

.tile.broken {
    background: repeating-linear-gradient(
        45deg,
        #2a1414,
        #2a1414 10px,
        #1a0a0a 10px,
        #1a0a0a 20px
    );
    border-color: #dc3545;
    animation: shake 0.3s, brokenPulse 1.5s infinite;
    position: relative;
}

/* Broken tiles use ::after for X icon (::before is for stone texture) */
.tile.broken::before {
    display: none; /* Hide stone texture on broken tiles */
}

.tile.broken::after {
    content: "❌";
    font-size: 2rem;
    position: absolute;
    color: #ff4444;
    filter: drop-shadow(0 0 4px #000);
    z-index: 2;
}

@keyframes brokenPulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.7;
    }
}

/* Tile break-apart animation */
@keyframes tileShatter {
    0% {
        transform: scale(1);
        opacity: 1;
    }
    50% {
        transform: scale(1.1);
        opacity: 0.8;
    }
    100% {
        transform: scale(0.9);
        opacity: 1;
    }
}

/* Fragment particles for break animation */
.tile-fragment {
    position: absolute;
    width: 8px;
    height: 8px;
    background: #ff4444;
    border-radius: 2px;
    pointer-events: none;
    z-index: 100;
}

@keyframes fragmentFly1 {
    0% {
        transform: translate(0, 0) rotate(0deg) scale(1);
        opacity: 1;
    }
    100% {
        transform: translate(-30px, -30px) rotate(-180deg) scale(0);
        opacity: 0;
    }
}

@keyframes fragmentFly2 {
    0% {
        transform: translate(0, 0) rotate(0deg) scale(1);
        opacity: 1;
    }
    100% {
        transform: translate(30px, -30px) rotate(180deg) scale(0);
        opacity: 0;
    }
}

@keyframes fragmentFly3 {
    0% {
        transform: translate(0, 0) rotate(0deg) scale(1);
        opacity: 1;
    }
    100% {
        transform: translate(-30px, 30px) rotate(-180deg) scale(0);
        opacity: 0;
    }
}

@keyframes fragmentFly4 {
    0% {
        transform: translate(0, 0) rotate(0deg) scale(1);
        opacity: 1;
    }
    100% {
        transform: translate(30px, 30px) rotate(180deg) scale(0);
        opacity: 0;
    }
}

@keyframes fragmentFly5 {
    0% {
        transform: translate(0, 0) rotate(0deg) scale(1);
        opacity: 1;
    }
    100% {
        transform: translate(0px, -40px) rotate(360deg) scale(0);
        opacity: 0;
    }
}

@keyframes fragmentFly6 {
    0% {
        transform: translate(0, 0) rotate(0deg) scale(1);
        opacity: 1;
    }
    100% {
        transform: translate(40px, 0px) rotate(-360deg) scale(0);
        opacity: 0;
    }
}

@keyframes fragmentFly7 {
    0% {
        transform: translate(0, 0) rotate(0deg) scale(1);
        opacity: 1;
    }
    100% {
        transform: translate(0px, 40px) rotate(360deg) scale(0);
        opacity: 0;
    }
}

@keyframes fragmentFly8 {
    0% {
        transform: translate(0, 0) rotate(0deg) scale(1);
        opacity: 1;
    }
    100% {
        transform: translate(-40px, 0px) rotate(-360deg) scale(0);
        opacity: 0;
    }
}

.tile.shattering {
    animation: tileShatter 0.3s ease-out;
}

/* Player - Pixel Art Style */
.player {
    width: var(--tile-size);
    height: var(--tile-size);
    /* Use absolute positioning to overlay on grid */
    position: absolute;
    /* No transition - instant teleport */
    transition: none;
    pointer-events: none;
    z-index: 10;
    transform-origin: center center;
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    image-rendering: crisp-edges;
    /* Center the player within the grid cell */
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Default Character - Pixel Hero (cyan) */
/* Character sprites moved to individual character CSS files */
/* See: styles/characters/hero.css, knight.css, ranger.css, mage.css, champion.css */

/* Base player sprite styling */
.player::before {
    content: "";
    position: absolute;
    width: 8px;
    height: 8px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: transparent;
}

/* Tile value indicator removed - colors indicate tile type */

/* Bouncing animation - fast and snappy */
.player.bouncing {
    animation: bouncyTeleport 0.15s cubic-bezier(0.68, -0.55, 0.27, 1.55);
}

@keyframes bouncyTeleport {
    0% {
        transform: scale(0.85);
    }
    50% {
        transform: scale(1.15);
    }
    100% {
        transform: scale(1);
    }
}

/* Tile Value Label */
.tile-value {
    font-size: 1.25rem;
    font-weight: 700;
    color: inherit;
    z-index: 1;
}

/* Animations */
@keyframes shake {
    0%, 100% {
        transform: translateX(0);
    }
    25% {
        transform: translateX(-4px);
    }
    75% {
        transform: translateX(4px);
    }
}

/* Cleared tiles stay visible */
.tile.cleared {
    opacity: 0.6;
}

/* Tile Hover Effect (desktop only) */
@media (hover: hover) {
    .tile:not(.broken):hover {
        transform: scale(1.05);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    }
}

/* Grid size variations */
.grid-small {
    --tile-size: 48px;
    --grid-gap: 3px;
}

.grid-medium {
    --tile-size: 64px;
    --grid-gap: 4px;
}

.grid-large {
    --tile-size: 80px;
    --grid-gap: 5px;
}

/* Responsive Grid Sizing */
@media (max-width: 640px) {
    #grid-container {
        --tile-size: 56px;
        --grid-gap: 3px;
        padding: 0.75rem;
    }
}

@media (max-width: 480px) {
    #grid-container {
        --tile-size: 48px;
        --grid-gap: 2px;
        padding: 0.5rem;
    }
}

@media (max-width: 400px) {
    #grid-container {
        --tile-size: 40px;
        --grid-gap: 2px;
        padding: 0.5rem;
    }

    .tile-value {
        font-size: 1rem;
    }

    .player::before {
        font-size: 1.5rem;
    }
}

/* Portal Tile - Purple portal theme */
.tile.portal {
    background: radial-gradient(circle at center, #8b4789 0%, #5a2d58 70%);
    border-color: #a65ea3;
    box-shadow: inset 0 -3px 0 #5a2d58, 0 3px 0 #000, 0 0 20px rgba(139, 71, 137, 0.6);
    animation: portalPulse 2s ease-in-out infinite;
}

.tile.portal::before {
    background-image:
        /* Portal swirl effect */
        radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.3) 20%, transparent 21%),
        radial-gradient(circle at 50% 50%, rgba(139, 71, 137, 0.8) 40%, transparent 41%),
        radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.2) 60%, transparent 61%);
    animation: portalSpin 3s linear infinite;
}

@keyframes portalPulse {
    0%, 100% {
        box-shadow: inset 0 -3px 0 #5a2d58, 0 3px 0 #000, 0 0 20px rgba(139, 71, 137, 0.6);
    }
    50% {
        box-shadow: inset 0 -3px 0 #5a2d58, 0 3px 0 #000, 0 0 30px rgba(139, 71, 137, 0.9);
    }
}

@keyframes portalSpin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

/* Ice Tile - Blue icy theme with enhanced styling */
/* Base ice style (will be overridden by value-specific styles) */
.tile.ice {
    background: linear-gradient(135deg, #5ca3e8 0%, #4a8cc7 50%, #3d7ab8 100%);
    border-color: #7bb8f0;
    box-shadow: inset 0 -3px 0 #3d7ab8, 0 3px 0 #000, inset 0 0 20px rgba(255, 255, 255, 0.3);
}

/* Ice tiles with different values - darker for higher values */
.tile.ice[data-value="5"] {
    background: linear-gradient(135deg, #0f2438 0%, #0a1a28 50%, #05111a 100%);
    border-color: #1f3448;
    box-shadow: inset 0 -3px 0 #05111a, 0 3px 0 #000, inset 0 0 20px rgba(255, 255, 255, 0.15);
}

.tile.ice[data-value="4"] {
    background: linear-gradient(135deg, #1a3d5e 0%, #143050 50%, #0e2338 100%);
    border-color: #2a4d6e;
    box-shadow: inset 0 -3px 0 #0e2338, 0 3px 0 #000, inset 0 0 20px rgba(255, 255, 255, 0.18);
}

.tile.ice[data-value="3"] {
    background: linear-gradient(135deg, #285580 0%, #1f4468 50%, #183352 100%);
    border-color: #385d90;
    box-shadow: inset 0 -3px 0 #183352, 0 3px 0 #000, inset 0 0 20px rgba(255, 255, 255, 0.22);
}

.tile.ice[data-value="2"] {
    background: linear-gradient(135deg, #3a6d9e 0%, #2e5682 50%, #234268 100%);
    border-color: #4a7dae;
    box-shadow: inset 0 -3px 0 #234268, 0 3px 0 #000, inset 0 0 20px rgba(255, 255, 255, 0.28);
}

.tile.ice[data-value="1"] {
    background: linear-gradient(135deg, #4a8cc7 0%, #3d7ab8 50%, #2f5d8a 100%);
    border-color: #5a9cd7;
    box-shadow: inset 0 -3px 0 #2f5d8a, 0 3px 0 #000, inset 0 0 20px rgba(255, 255, 255, 0.35);
}

.tile.ice[data-value="0"] {
    background: linear-gradient(135deg, #5ca3e8 0%, #4a8cc7 50%, #3d7ab8 100%);
    border-color: #6cb3f8;
    box-shadow: inset 0 -3px 0 #3d7ab8, 0 3px 0 #000, inset 0 0 20px rgba(255, 255, 255, 0.42);
}

.tile.ice::before {
    background-image:
        /* Ice crystals */
        radial-gradient(circle at 30% 30%, rgba(255, 255, 255, 0.6) 1px, transparent 1px),
        radial-gradient(circle at 70% 50%, rgba(255, 255, 255, 0.4) 1px, transparent 1px),
        radial-gradient(circle at 50% 80%, rgba(255, 255, 255, 0.5) 1px, transparent 1px),
        radial-gradient(circle at 20% 70%, rgba(255, 255, 255, 0.3) 1px, transparent 1px),
        /* Ice shine effect */
        linear-gradient(45deg, transparent 30%, rgba(255, 255, 255, 0.2) 50%, transparent 70%);
    background-size: 15px 15px, 20px 20px, 18px 18px, 12px 12px, 100% 100%;
    animation: iceShimmer 3s ease-in-out infinite;
}

@keyframes iceShimmer {
    0%, 100% {
        opacity: 0.7;
    }
    50% {
        opacity: 1;
    }
}

/* Player Teleporting Animation */
.player.teleporting {
    animation: teleportEffect 0.4s ease-in-out;
}

@keyframes teleportEffect {
    0% {
        transform: scale(1);
        opacity: 1;
        filter: blur(0px);
    }
    50% {
        transform: scale(0.5);
        opacity: 0.3;
        filter: blur(4px);
    }
    100% {
        transform: scale(1);
        opacity: 1;
        filter: blur(0px);
    }
}

/* Player Sliding Animation */
.player.sliding {
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Indestructible Tile (Knight's Shield) */
.tile.indestructible {
    background: linear-gradient(135deg, #4a7dae 0%, #3d6b96 50%, #2e5682 100%) !important;
    border: 3px solid #6cb3f8 !important;
    box-shadow:
        inset 0 -3px 0 #2e5682,
        0 3px 0 #000,
        0 0 20px rgba(108, 179, 248, 0.6),
        inset 0 0 30px rgba(255, 255, 255, 0.3) !important;
    animation: shieldPulse 2s ease-in-out infinite;
}

.tile.indestructible::before {
    content: '🛡️';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 2rem;
    opacity: 0.8;
    z-index: 1;
}

.tile.indestructible .tile-value {
    z-index: 2;
    position: relative;
    text-shadow: 0 0 10px rgba(255, 255, 255, 0.8);
}

@keyframes shieldPulse {
    0%, 100% {
        box-shadow:
            inset 0 -3px 0 #2e5682,
            0 3px 0 #000,
            0 0 15px rgba(108, 179, 248, 0.5),
            inset 0 0 30px rgba(255, 255, 255, 0.3);
    }
    50% {
        box-shadow:
            inset 0 -3px 0 #2e5682,
            0 3px 0 #000,
            0 0 30px rgba(108, 179, 248, 0.8),
            inset 0 0 30px rgba(255, 255, 255, 0.5);
    }
}
