:root {
    --bg-color: #f0f4f8;
    --primary-color: #4a90e2;
    --text-color: #333333;
    --tile-face-color: #ffffff;
    --tile-shadow-color: #bbbbbb;
    --tile-shadow-dark: rgba(0, 0, 0, 0.2);
    --glass-bg: rgba(255, 255, 255, 0.85);
    --glass-border: rgba(255, 255, 255, 0.3);

    --tile-width: 60px;
    --tile-height: 80px;
    --tile-depth: 10px;
}

body {
    margin: 0;
    padding: 0;
    background: var(--bg-color);
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    color: var(--text-color);
    overflow: hidden;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

#app-container {
    position: relative;
    width: 100vw;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: radial-gradient(circle at center, #ffffff 0%, #e6e9f0 100%);
}

/* --- HUD Layer --- */
#hud {
    height: 80px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 2rem;
    z-index: 100;
    color: var(--text-color);
    transition: all 0.3s ease;
}

.hud-item {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.2rem;
    font-weight: bold;
}

.title-area {
    flex-direction: column;
    gap: 0;
    align-items: center;
}

.title-area h1 {
    font-size: 1.5rem;
    margin: 0;
    background: linear-gradient(135deg, #333 0%, #666 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

#level-name {
    font-size: 0.9rem;
    opacity: 0.6;
    font-weight: normal;
}

/* --- Game Board Container --- */
#game-board-container {
    flex: 1;
    position: relative;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    perspective: 1000px;
    /* Essential for 3D feel */
}

#game-board {
    position: relative;
    width: 600px;
    height: 600px;
    transform-style: preserve-3d;
    transform: rotateX(10deg);
    /* Slight tilt for depth */
    transition: transform 0.5s ease-out;
}

/* --- Controls Layer --- */
#controls {
    height: 90px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 2rem;
    z-index: 100;
    margin-bottom: 2rem;
}

.icon-btn {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: none;
    background: rgba(255, 255, 255, 0.4);
    font-size: 1.5rem;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
    -webkit-backdrop-filter: blur(5px);
    backdrop-filter: blur(5px);
}

.icon-btn:hover {
    transform: scale(1.1) translateY(-2px);
    background: rgba(255, 255, 255, 0.6);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}

.icon-btn:active {
    transform: scale(0.95);
}

/* --- Tile Styles --- */
/* --- Tile Container (Hitbox) --- */
/* --- Tile Container (Hitbox) --- */
/* --- Tile Container (Hitbox) --- */
.tile {
    position: absolute;
    width: 60px;
    height: 88px;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    overflow: visible;
    z-index: 10;

    /* Container ignores events (Prevents overlapping tiles from blocking each other) */
    pointer-events: none;
}

/* --- Tile Inner Visual --- */
.tile-inner {
    width: 100%;
    height: 100%;

    /* Bright Pastel Face */
    background: linear-gradient(to bottom, #FFFEF9 0%, #FFF8E6 100%);
    border-radius: 6px;
    border: 1px solid rgba(180, 160, 140, 0.4);

    /* 3D Thickness (Gradient Layering) */
    box-shadow:
        inset 0 2px 2px rgba(255, 255, 255, 1.0),
        1px 1px 0 #E0D4BC,
        2px 2px 0 #DCCCA4,
        3px 3px 0 #D8C4A4,
        4px 4px 0 #D4BC98,
        5px 5px 0 #D0B48C,
        6px 6px 0 #CCAC80,
        7px 7px 0 #C8A474,
        8px 8px 0 #C49C68,
        8px 10px 16px rgba(96, 80, 64, 0.3);

    display: flex;
    justify-content: center;
    align-items: center;

    /* Visuals ignore events, Hitbox layer takes over */
    pointer-events: none;

    /* Smooth, flicker-free animation */
    transition: transform 0.18s ease-out,
        filter 0.15s ease-out,
        box-shadow 0.18s ease-out;
}

/* Texture */
.tile-inner::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: 1;
    border-radius: 5px;
    filter: url(#paper-texture);
    opacity: 0.1;
    mix-blend-mode: multiply;
    pointer-events: none;
}

/* Corner Accents */
.tile-inner::after {
    content: "";
    position: absolute;
    inset: 3px;
    z-index: 1;
    border-radius: 4px;
    pointer-events: none;
    background:
        linear-gradient(135deg, rgba(210, 190, 170, 0.5) 8px, transparent 8px),
        linear-gradient(-45deg, rgba(210, 190, 170, 0.5) 8px, transparent 8px);
    border: 1px solid rgba(210, 190, 170, 0.2);
}

/* --- Tile Icon (CSS Sprite) --- */
.tile-icon {
    z-index: 2;
    /* Above texture */
    width: 64px;
    height: 64px;
    background-image: url('../assets/images/icons_atlas_healing.png');
    background-size: 400% 300%;
    background-repeat: no-repeat;
    pointer-events: none;

    /* Transparent PNG used */
    opacity: 0.95;

    transform: scale(1.1);
    transform-origin: center center;
    margin: 0;
}

/* Row 1 */
.icon-sun {
    background-position: 0% 0%;
}

.icon-moon {
    background-position: 33.33% 0%;
}

.icon-star {
    background-position: 66.66% 0%;
}

.icon-cloud {
    background-position: 100% 0%;
}

/* Row 2 */
.icon-rain {
    background-position: 0% 50%;
}

.icon-tree {
    background-position: 33.33% 50%;
}

.icon-flower {
    background-position: 66.66% 50%;
}

.icon-leaf {
    background-position: 100% 50%;
}

/* Row 3 */
.icon-stone {
    background-position: 0% 100%;
}

.icon-water {
    background-position: 33.33% 100%;
}

.icon-bird {
    background-position: 66.66% 100%;
}

.icon-fish {
    background-position: 100% 100%;
}

/* Interactions (Stable Light-Only Effect) */
/* NO z-index change, NO transform, NO layout shift */
/* .tile.is-hovered removed (empty rule) */

.tile.is-hovered .tile-inner {
    /* No movement/scale */
    transform: none;

    /* Less aggressive brightness boost */
    filter: brightness(1.1) saturate(1.1);

    /* Distinct Highlighting Border */
    border-color: rgba(255, 255, 255, 0.9);

    /* Enhanced Glow/Shadow without changing size */
    box-shadow:
        inset 0 0 0 1px rgba(255, 255, 255, 0.5),
        /* Inner Glow */
        inset 0 2px 2px rgba(255, 255, 255, 1.0),
        1px 1px 0 #E0D4BC,
        2px 2px 0 #DCCCA4,
        3px 3px 0 #D8C4A4,
        4px 4px 0 #D4BC98,
        5px 5px 0 #D0B48C,
        6px 6px 0 #CCAC80,
        7px 7px 0 #C8A474,
        8px 8px 0 #C49C68,
        8px 10px 16px rgba(96, 80, 64, 0.4);
}

/* --- Selected State (Card Tilt Effect) --- */
/* User Request: "Tilt from bottom like picking up a card" */
.tile.selected {
    z-index: 1000 !important;
}

.tile.selected .tile-inner {
    /* Pivot at bottom to feel like it's rooted/held there */
    transform-origin: center bottom;

    /* Tilt forward AND sideways (Hand-held card feel) */
    transform: perspective(600px) rotateX(-10deg) rotateZ(4deg) scale(1.05) translateY(-10px);

    /* Visual: Card Shadow + Red Glow */
    box-shadow:
        0 30px 40px rgba(0, 0, 0, 0.2),
        /* Deep floating shadow */
        0 0 0 2px var(--primary-color),
        /* Sharp Border */
        0 0 20px var(--primary-color);
    /* Red Glow */

    border-color: var(--primary-color);
    filter: brightness(1.1);
}

/* Old .tile.selected removed */

/* Layer Visualization Logic removed */

/* --- Tile Hitbox Layer (Dedicated Event Area) --- */
/* --- Tile Hitbox Layer (Dedicated Event Area) --- */
.tile-hitbox {
    position: absolute;
    /* Expanded area to bridge gaps */
    inset: -4px;
    z-index: 9999;
    cursor: pointer;

    /* LEVITATION: Move hitbox forward in 3D space to bypass Z-fighting */
    transform: translateZ(50px);

    /* GHOST FIX: Keep physical pixels but hide them */
    background-color: #ffffff;
    opacity: 0;

    /* ONLY this layer receives events */
    pointer-events: auto;
}

/* --- Overlay Layer --- */
#overlay-layer {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    z-index: 2000;
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: auto;
    /* Ensure clicks block game */
}

.overlay-card {
    background: rgba(255, 255, 255, 0.9);
    padding: 3rem;
    border-radius: 24px;
    text-align: center;
    min-width: 320px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
    animation: popIn 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.glass-panel {
    background: rgba(255, 255, 255, 0.65);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid rgba(255, 255, 255, 0.5);
}

.overlay-card h2 {
    font-size: 2rem;
    margin-bottom: 1rem;
    color: var(--primary-color);
}

.primary-btn,
.secondary-btn {
    padding: 1rem 2rem;
    border-radius: 50px;
    border: none;
    font-size: 1.1rem;
    font-weight: bold;
    cursor: pointer;
    margin-top: 1.5rem;
    transition: transform 0.2s;
}

.primary-btn {
    background: linear-gradient(45deg, #4facfe 0%, #00f2fe 100%);
    color: white;
    box-shadow: 0 4px 15px rgba(79, 172, 254, 0.4);
}

.secondary-btn {
    background: #e0e0e0;
    color: #333;
}

.primary-btn:hover,
.secondary-btn:hover {
    transform: scale(1.05);
}

/* Animations */
@keyframes popIn {
    0% {
        transform: scale(0.8);
        opacity: 0;
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

.slide-down {
    animation: slideDown 0.6s ease-out;
}

.slide-up {
    animation: slideUp 0.6s ease-out;
}

@keyframes slideDown {
    from {
        transform: translateY(-100%);
    }

    to {
        transform: translateY(0);
    }
}

@keyframes slideUp {
    from {
        transform: translateY(100%);
    }

    to {
        transform: translateY(0);
    }
}

/* Utility - CRITICAL */
.hidden {
    display: none !important;
    opacity: 0;
    pointer-events: none;
}

/* Texture Filter Definition */
.texture-filter-def {
    position: absolute;
    pointer-events: none;
    opacity: 0;
    width: 0;
    height: 0;
}

/* --- Responsive Design --- */
@media (max-width: 768px) {
    #hud {
        padding: 0 1rem;
        height: 60px;
    }

    .hud-item {
        font-size: 1rem;
    }

    .title-area h1 {
        font-size: 1.2rem;
    }

    #game-board {
        transform: rotateX(10deg) scale(0.65);
        /* Scale down for mobile */
    }

    /* Adjust Controls */
    #controls {
        height: 70px;
        gap: 1rem;
    }

    .icon-btn {
        width: 50px;
        height: 50px;
        font-size: 1.2rem;
    }

    /* Overlay */
    .overlay-card {
        padding: 2rem;
        min-width: 280px;
        width: 80%;
    }

    .overlay-card h2 {
        font-size: 1.5rem;
    }
}