/**
 * Mobile Bug Fixes for DailyMuse Puzzle Page
 * 修复：300ms延迟、点击区域、输入法遮挡、触摸防抖
 */

/* ========== 1. 消除300ms点击延迟 ========== */
.puzzle-cell,
.puzzle-btn,
button[class*="puzzle"],
 input[type="text"],
 input[type="number"],
 select {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

/* ========== 2. 扩大点击区域5px ========== */
.puzzle-cell {
    position: relative;
    z-index: 1;
}

.puzzle-cell::before {
    content: '';
    position: absolute;
    top: -5px;
    left: -5px;
    right: -5px;
    bottom: -5px;
    z-index: -1;
    pointer-events: auto;
}

/* ========== 3. 低性能设备 - 减少动画 ========== */
@media (prefers-reduced-motion: reduce) {
    * {
        animation: none !important;
        transition: none !important;
    }
}

/* ========== 4. 低性能设备 - 无阴影 ========== */
.no-shadows * {
    box-shadow: none !important;
    text-shadow: none !important;
}

/* ========== 5. 小屏自适应 - 数独网格 ========== */
@media (max-width: 640px) {
    /* 数独格子缩小 */
    #sudoku-container > div > div {
        width: 2rem !important;
        height: 2rem !important;
        font-size: 0.75rem !important;
    }
    
    /* 数独数字键盘按钮缩小 */
    #sudoku-container > div > div[class*="w-10"] {
        width: 2rem !important;
        height: 2rem !important;
        font-size: 0.75rem !important;
    }
    
    /* 填字游戏格子缩小 */
    #crossword-container input {
        width: 2rem !important;
        height: 2rem !important;
        font-size: 0.75rem !important;
    }
}

@media (max-width: 480px) {
    /* 极小屏幕 - 数独格子再缩小 */
    #sudoku-container > div > div {
        width: 1.75rem !important;
        height: 1.75rem !important;
        font-size: 0.7rem !important;
    }
    
    /* 数独数字键盘按钮缩小 */
    #sudoku-container > div > div[class*="w-10"] {
        width: 1.75rem !important;
        height: 1.75rem !important;
        font-size: 0.7rem !important;
    }
    
    /* 填字游戏格子缩小 */
    #crossword-container input {
        width: 1.75rem !important;
        height: 1.75rem !important;
        font-size: 0.7rem !important;
    }
}

/* ========== 6. 输入法弹出时防遮挡 ========== */
.keyboard-visible {
    padding-bottom: env(safe-area-inset-bottom, 0);
    transition: padding-bottom 0.3s ease;
}

/* ========== 7. 虚拟键盘样式 ========== */
.virtual-keyboard {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: #F3F4F6;
    border-top: 1px solid #D1D5DB;
    padding: 8px;
    z-index: 9999;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
}

.virtual-key {
    display: inline-block;
    width: 9%;
    margin: 0.5%;
    height: 40px;
    line-height: 40px;
    text-align: center;
    background: #FFFFFF;
    border: 1px solid #D1D5DB;
    border-radius: 4px;
    font-size: 16px;
    font-weight: 500;
    cursor: pointer;
    user-select: none;
    touch-action: manipulation;
}

.virtual-key:active {
    background: #E5E7EB;
}

.virtual-key.backspace {
    background: #EF4444;
    color: #FFFFFF;
}

/* ========== 8. 骨架屏加载动画 ========== */
.skeleton {
    background: linear-gradient(90deg, #F3F4F6 25%, #E5E7EB 50%, #F3F4F6 75%);
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
}

@keyframes skeleton-loading {
    0% {
        background-position: 200% 0;
    }
    100% {
        background-position: -200% 0;
    }
}

.skeleton-cell {
    width: 2.5rem;
    height: 2.5rem;
    border-radius: 4px;
    margin: 1px;
}

.skeleton-text {
    height: 1rem;
    border-radius: 4px;
    margin: 0.5rem 0;
}

/* ========== 9. 记忆匹配 - 移动端优化 ========== */
@media (max-width: 640px) {
    /* 记忆匹配卡片缩小 */
    #memory-container button {
        width: 2.5rem !important;
        height: 2.5rem !important;
        font-size: 1rem !important;
    }
}

@media (max-width: 480px) {
    /* 极小屏幕 - 记忆匹配卡片再缩小 */
    #memory-container button {
        width: 2rem !important;
        height: 2rem !important;
        font-size: 0.875rem !important;
    }
}

/* ========== 10. P0 性能优化 ========== */

/* GPU-accelerated puzzle cells for smooth touch response */
#crossword-container input,
#sudoku-container input,
#memory-container button {
    will-change: transform;
    -webkit-backface-visibility: hidden;
}

/* Defer off-screen rendering */
#crossword-container > div:last-child,
#scramble-container,
#memory-container {
    content-visibility: auto;
    contain-intrinsic-size: auto 300px;
}

/* Prevent text selection during gameplay */
#puzzle-container-card {
    -webkit-user-select: none;
    user-select: none;
}

/* Allow selection in input fields */
#puzzle-container-card input {
    -webkit-user-select: text;
    user-select: text;
}
