/* ==========================================================================
   月玩月开心 —— 翻翻乐样式主文件
   分层：全局重置 → 根容器 → 页面基类 → Loading → 封面 → BGM → 游戏页HUD
         → 卡片网格+3D翻转 → 故事页 → 横屏提示 → 响应式
   兼容：QQ浏览器 / 微信浏览器 iOS & Android / PC 端
   ========================================================================== */

/* =========================================
   1. 全局重置
   ========================================= */
* {
    margin: 0;
    padding: 0;
    -webkit-box-sizing: border-box;
            box-sizing: border-box;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: 'Microsoft YaHei', 'PingFang SC', sans-serif;
    font-size: 16px;
    -webkit-text-size-adjust: none;
       -moz-text-size-adjust: none;
        -ms-text-size-adjust: none;
    -webkit-overflow-scrolling: touch;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
       -moz-user-select: none;
        -ms-user-select: none;
            user-select: none;
}

/* =========================================
   2. 根容器：移动端全屏；桌面端居中显示
   ========================================= */
#app {
    width: 100%;
    max-width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
}

#app.desktop-mode {
    max-width: 420px;
    max-height: 820px;
    margin: 0 auto;
    background: #ECEFF1;
}

/* =========================================
   3. 页面切换基类 .page
   ========================================= */
.page {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

.page.active {
    display: block;
}

/* 所有 page 内的 img 默认不允许拖拽/接收事件 */
.page img {
    -webkit-user-drag: none;
    -khtml-user-drag: none;
            user-drag: none;
    pointer-events: none;
}

/* button 基础：去边框、去背景 */
button {
    border: none;
    background: transparent;
    padding: 0;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}
button:focus { outline: none; }
button > img {
    width: 100%;
    height: 100%;
    display: block;
    pointer-events: none;
}

/* =========================================
   4. BGM 音乐图标（右上角，旋转动画，所有页面固定显示）
   ========================================= */
#bgmToggle {
    position: absolute;
    width: 40px;
    height: 40px;
    top: 15px;
    right: 15px;
    cursor: pointer;
    z-index: 500;
    -webkit-tap-highlight-color: transparent;
    -webkit-animation: bgmRotate 2s linear infinite;
            animation: bgmRotate 2s linear infinite;
}
#bgmToggle img {
    width: 100%;
    height: 100%;
    -webkit-user-drag: none;
            user-drag: none;
}
/* 暂停状态：停止旋转 */
#bgmToggle.paused,
#bgmToggle.paused img {
    -webkit-animation-play-state: paused;
            animation-play-state: paused;
}

@-webkit-keyframes bgmRotate {
    from { -webkit-transform: rotate(0deg); }
    to   { -webkit-transform: rotate(360deg); }
}
@keyframes bgmRotate {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
}

/* =========================================
   5. Loading 预加载页
   ========================================= */
#loadingPage {
    background: #FFF8E1;
    z-index: 100;
}
#loadingPage.active {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
        -webkit-align-items: center;
            -ms-flex-align: center;
                align-items: center;
    -webkit-box-pack: center;
        -webkit-justify-content: center;
            -ms-flex-pack: center;
                justify-content: center;
}

.loading-container {
    width: 75%;
    max-width: 320px;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
        -webkit-flex-direction: column;
            -ms-flex-direction: column;
                flex-direction: column;
    -webkit-box-align: center;
        -webkit-align-items: center;
            -ms-flex-align: center;
                align-items: center;
}

.loading-container > * + * { margin-top: 18px; }

.loading-title {
    font-size: 20px;
    color: #E65100;
    letter-spacing: 2px;
    font-weight: bold;
}

.loading-bar {
    width: 100%;
    height: 14px;
    background: rgba(0,0,0,0.1);
    border-radius: 10px;
    overflow: hidden;
}

.loading-bar-inner {
    width: 0%;
    height: 100%;
    background: -webkit-gradient(linear, left top, right top, from(#FFB74D), to(#FF7043));
    background: -webkit-linear-gradient(left, #FFB74D, #FF7043);
    background: linear-gradient(90deg, #FFB74D, #FF7043);
    border-radius: 10px;
    -webkit-transition: width 0.2s linear;
            transition: width 0.2s linear;
}

.loading-percent {
    font-size: 18px;
    color: #6D4C41;
    font-weight: bold;
}

.loading-skip-btn {
    margin-top: 6px;
    padding: 8px 20px;
    border: 1px solid #FF8A65;
    color: #E64A19;
    font-size: 14px;
    border-radius: 20px;
    background: rgba(255,255,255,0.6);
}
.loading-skip-btn:active {
    background: rgba(255,255,255,1);
}

/* =========================================
   6. 封面页
   ========================================= */
#coverPage {
    background: #FFF8E1;
}

/* 背景图铺满 */
.cover-bg-img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    -o-object-fit: cover;
       object-fit: cover;
    display: block;
}

/* 标题图：水平居中、靠上（对照封面设计图 1:1） */
.cover-title-img {
    position: absolute;
    top: 7%;
    left: 15%;
    width: 70%;
    z-index: 2;
    opacity: 0;
    -webkit-transform-origin: center;
            transform-origin: center;
    -webkit-animation: titleZoomIn 0.6s ease-in-out forwards;
            animation: titleZoomIn 0.6s ease-in-out forwards;
}
@-webkit-keyframes titleZoomIn {
    0%   { opacity: 0; -webkit-transform: scale(.3); }
    100% { opacity: 1; }
}
@keyframes titleZoomIn {
    0%   { opacity: 0; transform: scale(.3); }
    100% { opacity: 1; }
}

/* 开始按钮：底部靠下、居中（对照封面设计图 1:1） */
.cover-start-btn {
    position: absolute;
    left: 50%;
    bottom: 11%;
    width: 44%;
    max-width: 240px;
    height: auto;
    z-index: 3;
    overflow: hidden;
    -webkit-transform: translateX(-50%);
            transform: translateX(-50%);
    -webkit-animation: timeBtnBreath 2.6s ease-in-out infinite;
            animation: timeBtnBreath 2.6s ease-in-out infinite;
}
.cover-start-btn::after {
    content: "";
    pointer-events: none;
    will-change: background-position;
    background-image: -webkit-gradient(linear, 115deg, rgba(0, 0, 0, 0) 42%, rgba(255, 255, 255, 0.55) 47%, rgba(255, 255, 255, 0.92) 50%, rgba(255, 255, 255, 0.55) 53%, rgba(0, 0, 0, 0) 58%);
    background-image: -webkit-linear-gradient(115deg, rgba(0, 0, 0, 0) 42%, rgba(255, 255, 255, 0.55) 47%, rgba(255, 255, 255, 0.92) 50%, rgba(255, 255, 255, 0.55) 53%, rgba(0, 0, 0, 0) 58%);
    background-image: linear-gradient(115deg, rgba(0, 0, 0, 0) 42%, rgba(255, 255, 255, 0.55) 47%, rgba(255, 255, 255, 0.92) 50%, rgba(255, 255, 255, 0.55) 53%, rgba(0, 0, 0, 0) 58%);
    background-repeat: no-repeat;
    background-size: 300% 100%;
    position: absolute;
    background-position: 100% 0;
    border-radius: 999px;
    inset: 0;
    /* 与 timeBtnBreath 同周期同缓动：2.6s ease-in-out，保持呼吸→扩张→高光扫到最亮同步 */
    -webkit-animation: timeBtnShimmer 2.6s ease-in-out infinite;
            animation: timeBtnShimmer 2.6s ease-in-out infinite;
}
.cover-start-btn:active { opacity: 0.85; }

@-webkit-keyframes timeBtnBreath {
    0%, 100% { -webkit-transform: translateX(-50%) scale(1); }
    50%      { -webkit-transform: translateX(-50%) scale(1.06); }
}
@keyframes timeBtnBreath {
    0%, 100% { transform: translateX(-50%) scale(1); }
    50%      { transform: translateX(-50%) scale(1.06); }
}

/* 故事页按钮的呼吸动画（无 translateX，因为按钮本身没 translateX 定位） */
@-webkit-keyframes btnBreath {
    0%, 100% { -webkit-transform: scale(1);    transform: scale(1); }
    50%      { -webkit-transform: scale(1.05); transform: scale(1.05); }
}
@keyframes btnBreath {
    0%, 100% { transform: scale(1); }
    50%      { transform: scale(1.05); }
}
@-webkit-keyframes timeBtnShimmer {
    0%, 100%  { background-position: 100% 0; }
    35%       { background-position: 0 0; }
}
@keyframes timeBtnShimmer {
    0%, 100%  { background-position: 100% 0; }
    35%       { background-position: 0 0; }
}

/* =========================================
   7. 游戏页：关卡背景 + HUD + 卡片网格
   ========================================= */
#gamePage {
    background: #1a237e;
}

/* 关卡背景图 */
.level-bg-img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    -o-object-fit: cover;
       object-fit: cover;
    display: block;
    z-index: 0;
}

/* 顶部计时器：居中大号棕色字（参考设计图） */
.game-timer {
    position: absolute;
    top: 4%;
    left: 0;
    right: 0;
    text-align: center;
    z-index: 10;
    font-size: 52px;
    font-weight: 800;
    font-family: 'Impact', 'Arial Black', 'Microsoft YaHei', sans-serif;
    color: #8B4513;
    letter-spacing: 2px;
    text-shadow: 0 2px 4px rgba(0,0,0,0.15);
    line-height: 1;
}

/* 卡片网格外层容器：从顶部开始对齐，行数多了自然往下长 */
.card-grid-wrap {
    position: absolute;
    top: 15%;
    left: 0;
    right: 0;
    bottom: 20px;
    z-index: 5;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: flex-start;
        -webkit-align-items: flex-start;
            -ms-flex-align: flex-start;
                align-items: flex-start;
    -webkit-box-pack: center;
        -webkit-justify-content: center;
            -ms-flex-pack: center;
                justify-content: center;
    padding: 0 12px;
}

/* 卡片网格：4 列固定，行数由内容自动生成 */
.card-grid {
    display: -webkit-box;   /* 老浏览器 fallback */
    display: -ms-grid;
    display: grid;
    -ms-grid-columns: (1fr)[4];
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    width: 100%;
    max-width: 420px;
}

/* ========== 卡片本体 ========== */
/* 经典 padding-bottom:100% 方案：所有浏览器通用，撑开正方形占位 */
.card-item {
    position: relative;
    width: 100%;
    height: 0;            /* 让 padding-bottom 的百分比以 width 为基准 */
    padding-bottom: 100%; /* 高度 = 宽度，形成正方形 */
    -webkit-perspective: 800px;
            perspective: 800px;
    cursor: pointer;
}

/* 卡片翻转容器：绝对铺满卡片区域 */
.card-inner {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    -webkit-transform-style: preserve-3d;
            transform-style: preserve-3d;
    -webkit-transition: -webkit-transform 0.45s ease;
            transition: transform 0.45s ease;
}

/* 翻转状态 */
.card-item.flipped .card-inner {
    -webkit-transform: rotateY(180deg);
            transform: rotateY(180deg);
}

/* 配对成功状态：锁定 + 轻微缩放 */
.card-item.matched .card-inner {
    -webkit-transform: rotateY(180deg);
            transform: rotateY(180deg);
}
.card-item.matched {
    pointer-events: none;
}

/* 卡片正面（内容图）和背面 */
.card-face {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden;
            backface-visibility: hidden;
    overflow: hidden;
}

/* 背面：card_back.png */
.card-face-back img {
    width: 100%;
    height: 100%;
    -o-object-fit: cover;
       object-fit: cover;
    display: block;
}

/* 正面：元素卡片 */
.card-face-front {
    -webkit-transform: rotateY(180deg);
            transform: rotateY(180deg);
}
.card-face-front img {
    width: 100%;
    height: 100%;
    -o-object-fit: cover;
       object-fit: cover;
    display: block;
}

/* 翻牌失败时的抖动 */
@-webkit-keyframes cardShake {
    0%, 100% { -webkit-transform: rotateY(180deg) translateX(0); }
    20%      { -webkit-transform: rotateY(180deg) translateX(-4px); }
    40%      { -webkit-transform: rotateY(180deg) translateX(4px); }
    60%      { -webkit-transform: rotateY(180deg) translateX(-3px); }
    80%      { -webkit-transform: rotateY(180deg) translateX(3px); }
}
@keyframes cardShake {
    0%, 100% { transform: rotateY(180deg) translateX(0); }
    20%      { transform: rotateY(180deg) translateX(-4px); }
    40%      { transform: rotateY(180deg) translateX(4px); }
    60%      { transform: rotateY(180deg) translateX(-3px); }
    80%      { transform: rotateY(180deg) translateX(3px); }
}
.card-item.shake .card-inner {
    -webkit-animation: cardShake 0.4s ease-in-out;
            animation: cardShake 0.4s ease-in-out;
}

/* 配对成功时的弹跳动画 */
@-webkit-keyframes cardPop {
    0%   { -webkit-transform: rotateY(180deg) scale(1); }
    40%  { -webkit-transform: rotateY(180deg) scale(1.15); }
    100% { -webkit-transform: rotateY(180deg) scale(1); }
}
@keyframes cardPop {
    0%   { transform: rotateY(180deg) scale(1); }
    40%  { transform: rotateY(180deg) scale(1.15); }
    100% { transform: rotateY(180deg) scale(1); }
}
.card-item.pop .card-inner {
    -webkit-animation: cardPop 0.4s ease-in-out;
            animation: cardPop 0.4s ease-in-out;
}

/* =========================================
   8. 故事页
   ========================================= */
#storyPage {
    background: #000;
}

/* 故事背景图 */
.story-bg-img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    -o-object-fit: cover;
       object-fit: cover;
    display: block;
    z-index: 0;
}

/* 听故事按钮：绝对定位到背景图标题和文字中间（参考图位置） */
.story-listen-btn {
    position: absolute;
    left: 35%;
    top: 23%;        /* 背景图标题下方、文字上方 */
    width: 30%;
    z-index: 5;
    -webkit-animation: btnBreath 2.6s ease-in-out infinite;
            animation: btnBreath 2.6s ease-in-out infinite;
}
.story-listen-btn:active { opacity: 0.85; }

/* 故事页按钮区（下一关 / end.png）：上提回填 listen 按钮留下的空位 */
.story-btn-wrap {
    position: absolute;
    bottom: 18%;
    left: 0;
    right: 0;
    z-index: 5;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
        -webkit-justify-content: center;
            -ms-flex-pack: center;
                justify-content: center;
    padding: 0 20px;
}

/* 下一关按钮（前两关） */
.story-next-btn {
    width: 52%;
    max-width: 240px;
    display: none;
    -webkit-animation: btnBreath 2.6s ease-in-out infinite;
            animation: btnBreath 2.6s ease-in-out infinite;
}
.story-next-btn.show {
    display: block;
    -webkit-animation: popIn 0.4s ease-out both;
            animation: popIn 0.4s ease-out both;
}
.story-next-btn:active { opacity: 0.85; }

/* 通关结束图（第三关） */
.story-end-img {
    width: 94%;
    max-width: 380px;
    display: none;
}
.story-end-img.show {
    display: block;
    -webkit-animation: popIn 0.4s ease-out both;
            animation: popIn 0.4s ease-out both;
}

@-webkit-keyframes popIn {
    0%   { -webkit-transform: scale(0);   opacity: 0; }
    60%  { -webkit-transform: scale(1.08); }
    100% { -webkit-transform: scale(1);   opacity: 1; }
}
@keyframes popIn {
    0%   { transform: scale(0);   opacity: 0; }
    60%  { transform: scale(1.08); }
    100% { transform: scale(1);   opacity: 1; }
}

/* 播放故事时：按钮停止呼吸动画，保持静止不动 */
.story-listen-btn.playing {
    -webkit-animation: none;
            animation: none;
}

/* =========================================
   9. 横屏提示
   ========================================= */
.rotate-tip-layer {
    position: fixed;
    top: 0; left: 0;
    width: 100vw;
    height: 100vh;
    background: #FFF8E1;
    z-index: 9999;
    display: none;
    -webkit-box-align: center;
        -webkit-align-items: center;
            -ms-flex-align: center;
                align-items: center;
    -webkit-box-pack: center;
        -webkit-justify-content: center;
            -ms-flex-pack: center;
                justify-content: center;
}
.rotate-tip-layer.show {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
}

.rotate-tip-img {
    width: 36%;
    max-width: 280px;
    height: auto;
    -o-object-fit: contain;
       object-fit: contain;
    display: block;
}

/* =========================================
   10. 响应式适配
   ========================================= */
@media screen and (min-width: 320px) { html { font-size: 12px; } }
@media screen and (min-width: 360px) { html { font-size: 13px; } }
@media screen and (min-width: 375px) { html { font-size: 14px; } }
@media screen and (min-width: 414px) { html { font-size: 15px; } }
@media screen and (min-width: 480px) { html { font-size: 16px; } }
@media screen and (min-width: 640px) { html { font-size: 18px; } }

/* 小屏微调 */
@media (max-width: 360px) {
    .card-grid { gap: 6px; }
    .card-grid-wrap { top: 14%; padding: 0 10px; }
    .game-timer { font-size: 44px; }
}

/* 矮屏微调 */
@media (max-height: 560px) {
    .cover-title-img { top: 8%; width: 75%; }
    .cover-start-btn { bottom: 14%; width: 48%; }
    .card-grid { gap: 6px; }
    .card-grid-wrap { top: 15%; bottom: 10px; }
    .game-timer { font-size: 42px; }
}

/* =========================================
   11. 故事页入场动画
   ========================================= */
#storyPage.active {
    -webkit-animation: pageZoomIn 0.48s cubic-bezier(0.18, 0.89, 0.32, 1.28) both;
            animation: pageZoomIn 0.48s cubic-bezier(0.18, 0.89, 0.32, 1.28) both;
}
@-webkit-keyframes pageZoomIn {
    0%   { opacity: 0; -webkit-transform: scale(0.86); }
    55%  { opacity: 1; -webkit-transform: scale(1.03); }
    100% { opacity: 1; -webkit-transform: scale(1); }
}
@keyframes pageZoomIn {
    0%   { opacity: 0; transform: scale(0.86); }
    55%  { opacity: 1; transform: scale(1.03); }
    100% { opacity: 1; transform: scale(1); }
}
