* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background-image: url('images/background.png');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    display: flex;
    justify-content: center;
    /* 移除 align-items: center，改为顶部对齐避免溢出 */
    padding: 2vh 10px;
    min-height: 100vh;
    box-sizing: border-box;
}

.game-container {
    width: 100%;
    max-width: 500px; /* 减小外框宽度 */
    max-height: 96vh; /* 限制最大高度 */
    background-color: transparent;
    border-radius: 10px;
    box-shadow: none;
    padding: 20px 10px; /* 增加顶部内边距 */
    margin-top: 0px; /* 增加顶部外边距 */
    display: flex;
    flex-direction: column;
    gap: 10px; /* 减少间距 */
    backdrop-filter: none;
    overflow-y: auto; /* 内容溢出时可滚动 */
    box-sizing: border-box;
}

header {
    text-align: center;
    margin-top: 20px; /* 增加标题上方间距 */
    margin-bottom: 15px; /* 增加标题下方间距 */
}

h1 {
    margin-bottom: 5px;
    font-size: 1.6rem;
}

.game-title {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    margin-bottom: 15px;
}

.title-text {
    background: linear-gradient(45deg, #ff6b6b, #ffd93d, #6bcf7f, #4ecdc4, #45b7d1, #96ceb4);
    background-size: 400% 400%;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    font-weight: 900;
    font-size: 2rem;
    letter-spacing: 2px;
    animation: gradientShift 3s ease-in-out infinite, titleBounce 2s ease-in-out infinite alternate;
    position: relative;
}

.deer-emoji {
    font-size: 2.5rem;
    animation: deerDance 2s ease-in-out infinite;
    filter: drop-shadow(2px 2px 4px rgba(0, 0, 0, 0.3));
    transform-origin: center bottom;
}

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

@keyframes titleBounce {
    0% { transform: translateY(0px) scale(1); }
    100% { transform: translateY(-3px) scale(1.02); }
}

@keyframes deerDance {
    0%, 100% { transform: rotate(0deg) scale(1); }
    25% { transform: rotate(-5deg) scale(1.1); }
    50% { transform: rotate(0deg) scale(1.05); }
    75% { transform: rotate(5deg) scale(1.1); }
}

.score-board {
    display: flex;
    justify-content: space-around;
    font-size: 1.1rem;
    font-weight: bold;
    color: #333;
    background: linear-gradient(135deg, rgba(255, 107, 107, 0.85), rgba(255, 217, 61, 0.85));
    border-radius: 20px;
    padding: 12px 20px;
    margin: 0 10px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    border: 2px solid rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(10px);
}

.score, .moves {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    position: relative;
}

.score span, .moves span {
    font-size: 1.3rem;
    font-weight: 900;
    text-shadow: 1px 1px 3px rgba(208, 62, 62, 0.4), 0 0 6px rgba(201, 74, 74, 0.2);
    background: linear-gradient(45deg, #564f4f 0%, #db5704 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.game-board {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    grid-template-rows: repeat(8, 1fr);
    grid-gap: 2px; /* 减少间距避免重叠 */
    background-color: rgba(255, 255, 255, 0.8);
    border-radius: 8px;
    padding: 5px; /* 减少内边距 */
    margin: 0 auto;
    width: min(95%, 450px); /* 让游戏板占用更多空间 */
    height: min(95%, 450px); /* 明确设置高度，保持正方形 */
    max-width: 450px;
    max-height: 70vh; /* 增加最大高度 */
    position: relative;
    overflow: hidden;
    box-sizing: border-box;
}

.candy {
    border-radius: 6px;
    cursor: pointer;
    transition: transform 0.2s ease-out, box-shadow 0.2s ease-out;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 18px; /* 稍微减小字体以适应更小的格子 */
    user-select: none;
    touch-action: none;
    position: relative;
    z-index: 1;
    will-change: transform;
    min-width: 0;
    min-height: 0;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
}

.candy img {
    width: 80%;
    height: 80%;
    object-fit: contain;
    pointer-events: none;
    user-select: none;
    border-radius: 4px;
}

.candy.selected {
    transform: scale(0.8);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
    z-index: 10;
}

.candy.dragging {
    transform: scale(1.1);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
    z-index: 20;
    position: fixed !important;
    pointer-events: none;
    will-change: transform, left, top;
}

.candy.drag-placeholder {
    opacity: 0.3;
    background-color: rgba(200, 200, 200, 0.5) !important;
    border: 2px dashed #ccc;
    transition: opacity 0.2s ease-out;
}

.candy.drag-over {
    background-color: rgba(255, 255, 255, 0.3) !important;
    transform: scale(0.9);
}

.candy.matched {
    animation: epicMatchAnimation 1s forwards;
    z-index: 50;
}

.candy.swapping {
    animation: swapAnimation 0.5s ease-in-out;
}

.candy.falling {
    animation: fallAnimation 0.6s ease-in;
}

.candy.new-candy {
    animation: newCandyAnimation 0.4s ease-out;
}

/* 炫酷的消除特效 */
@keyframes epicMatchAnimation {
    0% { 
        transform: scale(1) rotate(0deg);
        opacity: 1;
        box-shadow: 0 0 0 0 rgba(255, 215, 0, 0.8),
                    0 0 0 0 rgba(255, 69, 0, 0.6),
                    0 0 0 0 rgba(255, 20, 147, 0.4);
        filter: brightness(1) saturate(1);
    }
    15% {
        transform: scale(1.3) rotate(10deg);
        opacity: 1;
        box-shadow: 0 0 20px 5px rgba(255, 215, 0, 0.9),
                    0 0 30px 10px rgba(255, 69, 0, 0.7),
                    0 0 40px 15px rgba(255, 20, 147, 0.5);
        filter: brightness(1.5) saturate(2);
    }
    30% {
        transform: scale(1.5) rotate(-15deg);
        opacity: 0.9;
        box-shadow: 0 0 30px 8px rgba(255, 215, 0, 1),
                    0 0 40px 15px rgba(255, 69, 0, 0.8),
                    0 0 50px 20px rgba(255, 20, 147, 0.6),
                    0 0 60px 25px rgba(138, 43, 226, 0.4);
        filter: brightness(2) saturate(3) hue-rotate(30deg);
    }
    50% {
        transform: scale(1.8) rotate(25deg);
        opacity: 0.7;
        box-shadow: 0 0 40px 12px rgba(255, 215, 0, 0.8),
                    0 0 50px 20px rgba(255, 69, 0, 0.6),
                    0 0 60px 25px rgba(255, 20, 147, 0.4),
                    0 0 70px 30px rgba(138, 43, 226, 0.3),
                    0 0 80px 35px rgba(0, 191, 255, 0.2);
        filter: brightness(2.5) saturate(4) hue-rotate(60deg);
    }
    70% {
        transform: scale(2.2) rotate(-30deg);
        opacity: 0.4;
        box-shadow: 0 0 50px 15px rgba(255, 215, 0, 0.6),
                    0 0 60px 25px rgba(255, 69, 0, 0.4),
                    0 0 70px 30px rgba(255, 20, 147, 0.3),
                    0 0 80px 35px rgba(138, 43, 226, 0.2),
                    0 0 90px 40px rgba(0, 191, 255, 0.1);
        filter: brightness(3) saturate(5) hue-rotate(120deg);
    }
    85% {
        transform: scale(2.5) rotate(45deg);
        opacity: 0.2;
        box-shadow: 0 0 60px 20px rgba(255, 255, 255, 0.8),
                    0 0 80px 35px rgba(255, 215, 0, 0.3),
                    0 0 100px 45px rgba(255, 20, 147, 0.2);
        filter: brightness(4) saturate(6) hue-rotate(180deg);
    }
    100% { 
        transform: scale(0) rotate(360deg);
        opacity: 0;
        box-shadow: 0 0 0 0 transparent;
        filter: brightness(5) saturate(0) hue-rotate(360deg);
    }
}

@keyframes swapAnimation {
    0% { 
        transform: scale(1) rotate(0deg);
        box-shadow: 0 0 0 0 transparent;
    }
    25% { 
        transform: scale(1.15) rotate(5deg);
        box-shadow: 0 0 15px 3px rgba(255, 215, 0, 0.6);
    }
    50% { 
        transform: scale(1.3) rotate(-10deg);
        box-shadow: 0 0 25px 8px rgba(255, 69, 0, 0.7),
                    0 0 35px 12px rgba(255, 20, 147, 0.5);
        filter: brightness(1.3) saturate(1.5);
    }
    75% { 
        transform: scale(1.15) rotate(5deg);
        box-shadow: 0 0 15px 3px rgba(138, 43, 226, 0.6);
    }
    100% { 
        transform: scale(1) rotate(0deg);
        box-shadow: 0 0 0 0 transparent;
        filter: brightness(1) saturate(1);
    }
}

@keyframes fallAnimation {
    0% { 
        transform: translateY(-150%) scale(0.7) rotate(-10deg);
        opacity: 0.5;
        box-shadow: 0 0 10px 2px rgba(0, 191, 255, 0.4);
        filter: blur(2px) brightness(0.8);
    }
    25% {
        transform: translateY(-50%) scale(0.9) rotate(5deg);
        opacity: 0.8;
        box-shadow: 0 0 15px 5px rgba(0, 191, 255, 0.6);
        filter: blur(1px) brightness(1.1);
    }
    60% {
        transform: translateY(10%) scale(1.2) rotate(-5deg);
        opacity: 1;
        box-shadow: 0 0 20px 8px rgba(50, 205, 50, 0.7),
                    0 0 30px 12px rgba(255, 215, 0, 0.4);
        filter: blur(0px) brightness(1.3);
    }
    80% {
        transform: translateY(-5%) scale(1.1) rotate(3deg);
        opacity: 1;
        box-shadow: 0 0 15px 5px rgba(50, 205, 50, 0.5);
        filter: brightness(1.1);
    }
    100% { 
        transform: translateY(0) scale(1) rotate(0deg);
        opacity: 1;
        box-shadow: 0 0 0 0 transparent;
        filter: brightness(1);
    }
}

@keyframes newCandyAnimation {
    0% { 
        transform: scale(0) rotate(180deg);
        opacity: 0;
        box-shadow: 0 0 0 0 transparent;
        filter: blur(5px) brightness(2);
    }
    20% {
        transform: scale(0.5) rotate(90deg);
        opacity: 0.3;
        box-shadow: 0 0 20px 5px rgba(255, 20, 147, 0.6);
        filter: blur(3px) brightness(1.8);
    }
    40% {
        transform: scale(0.8) rotate(45deg);
        opacity: 0.6;
        box-shadow: 0 0 25px 8px rgba(255, 69, 0, 0.7),
                    0 0 35px 12px rgba(255, 215, 0, 0.5);
        filter: blur(2px) brightness(1.5);
    }
    70% {
        transform: scale(1.2) rotate(15deg);
        opacity: 0.9;
        box-shadow: 0 0 30px 10px rgba(138, 43, 226, 0.8),
                    0 0 40px 15px rgba(255, 20, 147, 0.6);
        filter: blur(1px) brightness(1.3);
    }
    85% {
        transform: scale(1.1) rotate(5deg);
        opacity: 1;
        box-shadow: 0 0 15px 5px rgba(50, 205, 50, 0.6);
        filter: blur(0px) brightness(1.1);
    }
    100% { 
        transform: scale(1) rotate(0deg);
        opacity: 1;
        box-shadow: 0 0 0 0 transparent;
        filter: brightness(1);
    }
}

@keyframes bounce {
    0%, 20%, 53%, 80%, 100% {
        transform: translateY(0);
    }
    40%, 43% {
        transform: translateY(-10px);
    }
    70% {
        transform: translateY(-5px);
    }
}

.candy.bounce {
    animation: bounce 0.8s ease-in-out;
}

/* 连击特效 */
.candy.combo {
    animation: comboAnimation 0.8s ease-out forwards;
    z-index: 60;
}

@keyframes comboAnimation {
    0% { 
        transform: scale(1);
        box-shadow: 0 0 0 0 transparent;
        filter: brightness(1);
    }
    20% {
        transform: scale(1.4);
        box-shadow: 0 0 30px 10px rgba(255, 215, 0, 0.9),
                    0 0 50px 20px rgba(255, 20, 147, 0.7);
        filter: brightness(2) saturate(2);
    }
    40% {
        transform: scale(1.6);
        box-shadow: 0 0 40px 15px rgba(255, 69, 0, 1),
                    0 0 60px 25px rgba(138, 43, 226, 0.8),
                    0 0 80px 35px rgba(0, 191, 255, 0.6);
        filter: brightness(3) saturate(3) hue-rotate(60deg);
    }
    60% {
        transform: scale(1.8);
        box-shadow: 0 0 50px 20px rgba(255, 255, 255, 0.9),
                    0 0 70px 30px rgba(255, 215, 0, 0.7),
                    0 0 90px 40px rgba(255, 20, 147, 0.5);
        filter: brightness(4) saturate(4) hue-rotate(120deg);
    }
    80% {
        transform: scale(2);
        opacity: 0.8;
        box-shadow: 0 0 60px 25px rgba(255, 255, 255, 1),
                    0 0 100px 50px rgba(255, 215, 0, 0.6);
        filter: brightness(5) saturate(5) hue-rotate(180deg);
    }
    100% {
        transform: scale(0);
        opacity: 0;
        box-shadow: 0 0 0 0 transparent;
        filter: brightness(6) saturate(0) hue-rotate(360deg);
    }
}

/* 超级连击特效 */
.candy.mega-combo {
    animation: megaComboAnimation 1.2s ease-out forwards;
    z-index: 70;
}

@keyframes megaComboAnimation {
    0% { 
        transform: scale(1) rotate(0deg);
        box-shadow: 0 0 0 0 transparent;
        filter: brightness(1);
    }
    10% {
        transform: scale(1.2) rotate(15deg);
        box-shadow: 0 0 20px 5px rgba(255, 215, 0, 1),
                    0 0 40px 15px rgba(255, 69, 0, 0.8);
        filter: brightness(1.5) saturate(2);
    }
    25% {
        transform: scale(1.6) rotate(-20deg);
        box-shadow: 0 0 40px 15px rgba(255, 20, 147, 1),
                    0 0 60px 25px rgba(138, 43, 226, 0.9),
                    0 0 80px 35px rgba(0, 191, 255, 0.7);
        filter: brightness(2.5) saturate(3) hue-rotate(45deg);
    }
    50% {
        transform: scale(2.2) rotate(30deg);
        box-shadow: 0 0 60px 25px rgba(255, 255, 255, 1),
                    0 0 80px 35px rgba(255, 215, 0, 0.8),
                    0 0 100px 45px rgba(255, 20, 147, 0.6),
                    0 0 120px 55px rgba(138, 43, 226, 0.4);
        filter: brightness(4) saturate(4) hue-rotate(120deg);
    }
    75% {
        transform: scale(2.8) rotate(-45deg);
        opacity: 0.7;
        box-shadow: 0 0 80px 35px rgba(255, 255, 255, 1),
                    0 0 120px 55px rgba(255, 215, 0, 0.7),
                    0 0 160px 75px rgba(255, 20, 147, 0.5),
                    0 0 200px 95px rgba(0, 191, 255, 0.3);
        filter: brightness(6) saturate(5) hue-rotate(240deg);
    }
    90% {
        transform: scale(3.5) rotate(60deg);
        opacity: 0.3;
        box-shadow: 0 0 100px 50px rgba(255, 255, 255, 0.8),
                    0 0 200px 100px rgba(255, 215, 0, 0.4);
        filter: brightness(8) saturate(6) hue-rotate(300deg);
    }
    100% {
        transform: scale(0) rotate(720deg);
        opacity: 0;
        box-shadow: 0 0 0 0 transparent;
        filter: brightness(10) saturate(0) hue-rotate(360deg);
    }
}

.controls {
    display: flex;
    justify-content: center;
    gap: 15px; /* 减少按钮间距 */
    margin-top: 5px; /* 减少顶部边距 */
}

button {
    padding: 8px 16px; /* 减少按钮大小 */
    background-color: #ff6b6b;
    color: white;
    border: none;
    border-radius: 5px;
    font-size: 0.9rem; /* 稍微减小字体 */
    cursor: pointer;
    transition: background-color 0.3s;
}

button:hover {
    background-color: #ff5252;
}

.modal {
    display: none;
    position: fixed;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    z-index: 100;
    justify-content: center;
    align-items: center;
}

.modal-content {
    background-color: white;
    padding: 30px;
    border-radius: 10px;
    text-align: center;
    max-width: 400px;
    width: 90%;
}

.modal h2 {
    color: #ff6b6b;
    margin-bottom: 15px;
}

.modal p {
    margin-bottom: 20px;
}

/* 响应式设计 */
@media (max-width: 600px) {
    body {
        padding: 1vh 5px;
    }
    
    .game-container {
        padding: 8px;
        width: 98%;
        max-width: 98vw;
        gap: 8px;
    }

    .title-text {
        font-size: 1.5rem;
        letter-spacing: 1px;
    }
    
    .deer-emoji {
        font-size: 2rem;
    }

    .score-board {
        font-size: 0.9rem;
        padding: 10px 15px;
        margin: 0 5px;
    }
    
    .score span, .moves span {
        font-size: 1.1rem;
    }

    .game-board {
        grid-gap: 1px;
        padding: 3px;
        width: min(98%, 350px);
        height: min(98%, 350px);
        max-height: 60vh;
    }

    .candy {
        font-size: 14px;
        border-radius: 4px;
    }

    button {
        padding: 6px 12px;
        font-size: 0.8rem;
    }
}

@media (max-width: 400px) {
    .title-text {
        font-size: 1.3rem;
        letter-spacing: 1px;
    }
    
    .deer-emoji {
        font-size: 1.8rem;
    }

    .score-board {
        font-size: 0.8rem;
        padding: 8px 12px;
    }
    
    .score span, .moves span {
        font-size: 1rem;
    }

    .game-board {
        grid-gap: 1px;
        padding: 2px;
        width: min(99%, 300px);
        height: min(99%, 300px);
        max-height: 55vh;
    }

    .candy {
        font-size: 12px;
        border-radius: 3px;
    }
}

/* 确保在低高度屏幕上也能正常显示 */
@media (max-height: 700px) {
    .game-container {
        padding: 5px;
        gap: 5px;
        max-height: 98vh;
    }
    
    .title-text {
        font-size: 1.4rem;
    }
    
    .deer-emoji {
        font-size: 1.8rem;
    }
    
    .game-board {
        width: min(95%, 380px);
        height: min(95%, 380px);
        max-height: 65vh;
        padding: 3px;
        grid-gap: 1px;
    }
    
    .candy {
        font-size: 14px;
    }
}

/* 针对非常小的屏幕进一步优化 */
@media (max-height: 600px) {
    .title-text {
        font-size: 1.2rem;
    }
    
    .deer-emoji {
        font-size: 1.5rem;
    }
    
    .game-board {
        width: min(95%, 320px);
        height: min(95%, 320px);
        max-height: 60vh;
    }
    
    .candy {
        font-size: 12px;
    }
}

/* 屏幕震动效果 */
@keyframes screenShake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-2px); }
    20%, 40%, 60%, 80% { transform: translateX(2px); }
}

.screen-shake {
    animation: screenShake 0.5s ease-in-out;
}

/* 连击文字效果 */
.combo-text {
    position: fixed;
    top: 30%;
    left: 50%;
    transform: translateX(-50%);
    font-size: 2rem;
    font-weight: 900;
    color: #FFD700;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
    z-index: 1001;
    pointer-events: none;
    animation: comboTextAnimation 1.5s ease-out forwards;
}

@keyframes comboTextAnimation {
    0% {
        opacity: 0;
        transform: translateX(-50%) scale(0.5) translateY(50px);
    }
    20% {
        opacity: 1;
        transform: translateX(-50%) scale(1.2) translateY(0px);
    }
    40% {
        transform: translateX(-50%) scale(1) translateY(-10px);
    }
    100% {
        opacity: 0;
        transform: translateX(-50%) scale(0.8) translateY(-100px);
    }
}

/* 超级连击文字效果 */
.mega-combo-text {
    position: fixed;
    top: 25%;
    left: 50%;
    transform: translateX(-50%);
    font-size: 3rem;
    font-weight: 900;
    background: linear-gradient(45deg, #FF1493, #FFD700, #00BFFF, #FF69B4);
    background-size: 400% 400%;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    text-shadow: 0 0 20px rgba(255, 215, 0, 0.8);
    z-index: 1001;
    pointer-events: none;
    animation: megaComboTextAnimation 2s ease-out forwards, gradientShift 0.5s ease-in-out infinite;
}

@keyframes megaComboTextAnimation {
    0% {
        opacity: 0;
        transform: translateX(-50%) scale(0) rotate(-180deg);
    }
    20% {
        opacity: 1;
        transform: translateX(-50%) scale(1.5) rotate(0deg);
    }
    40% {
        transform: translateX(-50%) scale(1.2) rotate(5deg);
    }
    60% {
        transform: translateX(-50%) scale(1.3) rotate(-5deg);
    }
    80% {
        transform: translateX(-50%) scale(1.1) rotate(3deg);
    }
    100% {
        opacity: 0;
        transform: translateX(-50%) scale(0.5) translateY(-150px) rotate(360deg);
    }
}