/* ==================== Base Styles ==================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    transform: translateZ(0);
    backface-visibility: hidden;
    -webkit-font-smoothing: antialiased;
}

/* ==================== Screen Management ==================== */
.screen {
    display: none;
    width: 100%;
    max-width: 600px;
    padding: 20px;
}

.screen.active {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

.screen.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.8);
    z-index: 1000;
    padding: 20px;
}

.screen-content {
    background: white;
    border-radius: 20px;
    padding: 40px 30px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    text-align: center;
    max-width: 500px;
    width: 100%;
}

.modal {
    animation: modalPop 0.3s ease-out;
}

@keyframes modalPop {
    0% {
        transform: scale(0.8);
        opacity: 0;
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* ==================== Typography ==================== */
.game-title {
    font-size: 2.5em;
    color: #667eea;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.game-subtitle {
    font-size: 1.1em;
    color: #666;
    margin-bottom: 30px;
}

/* ==================== Buttons ==================== */
.btn {
    padding: 15px 30px;
    font-size: 1.1em;
    border: none;
    border-radius: 50px;
    cursor: pointer;
    transition: all 0.3s ease;
    font-weight: bold;
    margin: 8px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

.btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

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

.btn-secondary:hover {
    background: #e0e0e0;
    transform: translateY(-2px);
}

.btn-small {
    padding: 10px 20px;
    font-size: 0.9em;
}

.btn-danger {
    background: #ff6b6b;
    color: white;
}

.btn-danger:hover {
    background: #ff5252;
}

/* ==================== Instructions ==================== */
.instructions {
    margin-top: 30px;
    text-align: left;
    background: #f8f9ff;
    padding: 20px;
    border-radius: 15px;
}

.instructions h3 {
    color: #667eea;
    margin-bottom: 15px;
}

.instructions ul {
    list-style: none;
    padding-left: 0;
}

.instructions li {
    padding: 8px 0;
    color: #555;
    line-height: 1.5;
}

.instructions li:before {
    content: "✓ ";
    color: #667eea;
    font-weight: bold;
    margin-right: 8px;
}

/* ==================== Game Header ==================== */
.game-header {
    display: flex;
    justify-content: space-between;
    width: 100%;
    max-width: 600px;
    margin-bottom: 15px;
    gap: 10px;
}

.header-left, .header-right {
    display: flex;
    gap: 15px;
}

.stat {
    background: white;
    padding: 10px 20px;
    border-radius: 15px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 80px;
}

.stat-label {
    font-size: 0.8em;
    color: #888;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.stat-value {
    font-size: 1.5em;
    font-weight: bold;
    color: #667eea;
    margin-top: 5px;
    transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.timer-stat .stat-value {
    color: #ff6b6b;
}

/* ==================== Progress Bar ==================== */
.progress-container {
    width: 100%;
    max-width: 600px;
    height: 10px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 10px;
    overflow: hidden;
    margin-bottom: 20px;
}

.progress-bar {
    height: 100%;
    background: linear-gradient(90deg, #4ade80 0%, #22c55e 100%);
    border-radius: 10px;
    transition: width 0.5s cubic-bezier(0.4, 0, 0.2, 1);
    width: 0%;
    box-shadow: 0 0 10px rgba(74, 222, 128, 0.5);
}

/* ==================== Game Board ==================== */
.game-board {
    display: grid;
    gap: 5px;
    background: rgba(255, 255, 255, 0.95);
    padding: 15px;
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25), 
                inset 0 0 20px rgba(102, 126, 234, 0.1);
    margin: 0 auto;
    backdrop-filter: blur(10px);
    border: 2px solid rgba(255, 255, 255, 0.3);
    animation: boardEntrance 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
    user-select: none;
    -webkit-user-select: none;
    touch-action: none;
    contain: layout style paint;
    will-change: contents;
}

@keyframes boardEntrance {
    0% {
        transform: scale(0.9) translateY(20px);
        opacity: 0;
    }
    100% {
        transform: scale(1) translateY(0);
        opacity: 1;
    }
}

@keyframes tileEntrance {
    0% {
        transform: scale(0) rotate(-90deg);
        opacity: 0;
    }
    70% {
        transform: scale(1.05) rotate(5deg);
        opacity: 1;
    }
    100% {
        transform: scale(1) rotate(0deg);
        opacity: 1;
    }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-4px); }
    20%, 40%, 60%, 80% { transform: translateX(4px); }
}

.tile.shake {
    animation: shake 0.4s ease-in-out;
}

.tile {
    aspect-ratio: 1;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2em;
    cursor: pointer;
    transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    position: relative;
    background: linear-gradient(135deg, #f0f0f0 0%, #e0e0e0 100%);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    will-change: transform;
    transform: translateZ(0);
    backface-visibility: hidden;
    contain: layout style paint;
    content-visibility: auto;
}

.tile:hover {
    transform: scale(1.08) translateY(-2px) translateZ(0);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
    z-index: 2;
}

.tile:active {
    transform: scale(0.98) translateZ(0);
    transition: transform 0.1s ease;
}

.tile.drag-over {
    transform: scale(1.05) translateZ(0);
    box-shadow: 0 0 0 2px #ffd93d, 0 8px 20px rgba(255, 217, 61, 0.4);
    z-index: 3;
}

.tile.selected {
    transform: scale(1.05) translateZ(0);
    box-shadow: 0 0 0 3px #667eea, 0 8px 20px rgba(102, 126, 234, 0.4);
    animation: pulse 0.6s ease-in-out infinite;
    z-index: 3;
}

.tile.dragging {
    opacity: 0.85;
    cursor: grabbing;
    z-index: 100;
    filter: drop-shadow(0 12px 24px rgba(0, 0, 0, 0.4));
    transition: none !important;
}

@keyframes pulse {
    0%, 100% {
        box-shadow: 0 0 0 3px #667eea, 0 8px 20px rgba(102, 126, 234, 0.4);
        transform: scale(1.05);
    }
    50% {
        box-shadow: 0 0 0 6px #764ba2, 0 10px 30px rgba(118, 75, 162, 0.5);
        transform: scale(1.08);
    }
}

/* Tile Types */
.tile[data-type="0"] { background: linear-gradient(135deg, #ff6b6b 0%, #ee5a52 100%); }
.tile[data-type="1"] { background: linear-gradient(135deg, #4ecdc4 0%, #44a89f 100%); }
.tile[data-type="2"] { background: linear-gradient(135deg, #ffd93d 0%, #f0c020 100%); }
.tile[data-type="3"] { background: linear-gradient(135deg, #95e1d3 0%, #7ac7ba 100%); }
.tile[data-type="4"] { background: linear-gradient(135deg, #a8e6cf 0%, #8fd1b5 100%); }
.tile[data-type="5"] { background: linear-gradient(135deg, #ff9a9e 0%, #fad0c4 100%); }
.tile[data-type="6"] { background: linear-gradient(135deg, #a18cd1 0%, #fbc2eb 100%); }

/* ==================== Animations ==================== */
@keyframes tileMatch {
    0% {
        transform: scale(1) rotate(0deg) translateZ(0);
        opacity: 1;
    }
    50% {
        transform: scale(1.3) rotate(90deg) translateZ(0);
        opacity: 0.8;
    }
    100% {
        transform: scale(0) rotate(180deg) translateZ(0);
        opacity: 0;
    }
}

@keyframes tileFall {
    0% {
        transform: translateY(-15px) scale(0.9) translateZ(0);
        opacity: 0.5;
    }
    70% {
        transform: translateY(0) scale(1.02) translateZ(0);
        opacity: 1;
    }
    100% {
        transform: translateY(0) scale(1) translateZ(0);
        opacity: 1;
    }
}

@keyframes particleExplosion {
    0% {
        transform: translate(0, 0) scale(1);
        opacity: 1;
    }
    100% {
        transform: translate(var(--tx), var(--ty)) scale(0);
        opacity: 0;
    }
}

@keyframes scorePopup {
    0% {
        transform: translateY(0) scale(0.5);
        opacity: 0;
    }
    50% {
        transform: translateY(-30px) scale(1.2);
        opacity: 1;
    }
    100% {
        transform: translateY(-60px) scale(1);
        opacity: 0;
    }
}

.tile.matching {
    animation: tileMatch 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55) forwards;
    pointer-events: none;
    z-index: 10;
    will-change: transform, opacity;
}

.tile.falling {
    animation: tileFall 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
    will-change: transform, opacity;
}

.tile.hidden {
    opacity: 0;
    visibility: hidden;
}

/* Particle effect */
.particle {
    position: fixed;
    font-size: 1.2em;
    pointer-events: none;
    z-index: 9999;
    animation: particleExplosion 0.5s ease-out forwards;
    will-change: transform, opacity;
}

/* Score popup effect */
.score-popup {
    position: fixed;
    font-size: 1.8em;
    font-weight: bold;
    color: #4ade80;
    text-shadow: 0 0 10px rgba(74, 222, 128, 0.8), 2px 2px 4px rgba(0, 0, 0, 0.5);
    pointer-events: none;
    z-index: 9999;
    animation: scorePopup 0.7s ease-out forwards;
    will-change: transform, opacity;
}

/* ==================== Game Footer ==================== */
.game-footer {
    display: flex;
    justify-content: center;
    gap: 15px;
    margin-top: 20px;
}

/* ==================== Level Complete Screen ==================== */
.success-title {
    font-size: 2.5em;
    color: #4ade80;
    margin-bottom: 10px;
}

.success-message {
    font-size: 1.2em;
    color: #666;
    margin-bottom: 25px;
}

.level-stats {
    background: #f8f9ff;
    padding: 20px;
    border-radius: 15px;
    margin: 20px 0;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    padding: 10px 0;
    font-size: 1.1em;
}

.stat-item.total {
    border-top: 2px solid #667eea;
    margin-top: 10px;
    padding-top: 15px;
    font-weight: bold;
    font-size: 1.3em;
    color: #667eea;
}

/* Stars Display */
.stars {
    font-size: 3em;
    margin: 20px 0;
    animation: starsAppear 0.6s ease-out;
}

@keyframes starsAppear {
    0% {
        transform: scale(0) rotate(-180deg);
        opacity: 0;
    }
    50% {
        transform: scale(1.2) rotate(10deg);
    }
    100% {
        transform: scale(1) rotate(0deg);
        opacity: 1;
    }
}

/* ==================== Game Over Screen ==================== */
.game-over-title {
    font-size: 2.5em;
    color: #ff6b6b;
    margin-bottom: 10px;
}

.game-over-message {
    font-size: 1.2em;
    color: #666;
    margin-bottom: 25px;
}

/* ==================== Victory Screen ==================== */
.victory-title {
    font-size: 3em;
    color: #ffd93d;
    margin-bottom: 10px;
    animation: victoryBounce 1s ease-in-out infinite;
}

@keyframes victoryBounce {
    0%, 100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-10px);
    }
}

.victory-message {
    font-size: 1.3em;
    color: #666;
    margin-bottom: 25px;
}

/* ==================== Responsive Design ==================== */
@media (max-width: 768px) {
    .game-title {
        font-size: 2em;
    }

    .game-header {
        flex-direction: column;
        gap: 10px;
    }

    .header-left, .header-right {
        justify-content: center;
        width: 100%;
    }

    .stat {
        flex: 1;
        min-width: 70px;
        padding: 8px 15px;
    }

    .stat-value {
        font-size: 1.2em;
    }

    .tile {
        font-size: 1.5em;
    }

    .btn {
        padding: 12px 25px;
        font-size: 1em;
    }

    .screen-content {
        padding: 30px 20px;
    }

    .success-title, .game-over-title {
        font-size: 2em;
    }

    .victory-title {
        font-size: 2.5em;
    }
}

@media (max-width: 480px) {
    .game-title {
        font-size: 1.5em;
    }

    .tile {
        font-size: 1.2em;
        border-radius: 8px;
    }

    .stat {
        padding: 6px 10px;
        min-width: 60px;
    }

    .stat-label {
        font-size: 0.7em;
    }

    .stat-value {
        font-size: 1em;
    }

    .btn {
        padding: 10px 20px;
        font-size: 0.9em;
    }

    .instructions {
        padding: 15px;
    }

    .instructions li {
        font-size: 0.9em;
    }
}

/* ==================== Loading/Transition Effects ==================== */
@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.fade-in {
    animation: fadeIn 0.3s ease-in;
}

/* ==================== Special Effects ==================== */
.combo-text {
    position: fixed;
    font-size: 2.5em;
    font-weight: bold;
    background: linear-gradient(45deg, #ff6b6b, #ffd93d, #4ecdc4, #667eea);
    background-size: 300% 300%;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    text-shadow: 0 0 20px rgba(255, 215, 61, 0.5);
    pointer-events: none;
    z-index: 9999;
    animation: comboFloat 0.9s ease-out forwards, gradientShift 0.5s ease-in-out infinite;
    filter: drop-shadow(2px 2px 4px rgba(0, 0, 0, 0.3));
    will-change: transform, opacity;
}

@keyframes comboFloat {
    0% {
        transform: translateY(0) scale(0.5) rotate(-10deg);
        opacity: 0;
    }
    20% {
        opacity: 1;
        transform: translateY(-20px) scale(1.2) rotate(5deg);
    }
    100% {
        transform: translateY(-110px) scale(1) rotate(0deg);
        opacity: 0;
    }
}

@keyframes gradientShift {
    0%, 100% {
        background-position: 0% 50%;
    }
    50% {
        background-position: 100% 50%;
    }
}
