/* BASE */
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; }
body {
    background: #0f0f23;
    font-family: 'Courier New', monospace;
    touch-action: manipulation;
}
body::after {
    content: '';
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: repeating-linear-gradient(0deg, rgba(0,0,0,0.15) 1px, transparent 1px, transparent 2px);
    pointer-events: none;
    z-index: 9999;
}

/* Pixel BG */
.pixel-bg { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: -2; }
.pixel-bg canvas { width: 100%; height: 100%; opacity: 0.3; }

/* Desktop Layout */
.desktop-game {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    gap: 20px;
    padding: 15px;
    box-sizing: border-box;
}

.game-board-wrapper {
    position: relative;
    border: 4px solid #00fff7;
    box-shadow: 0 0 20px rgba(0, 255, 247, 0.5);
    background: #0a0a1a;
    width: 288px;
    /* 高度与 side-panel 内容高度匹配 */
    min-height: 678px;
    flex-shrink: 0;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* 装饰区域 - 填充 canvas 上下的空白，使用网格背景 */
.board-decoration {
    flex-shrink: 0;
    height: 60px;
    background: 
        linear-gradient(180deg, rgba(0,255,247,0.03) 0%, rgba(0,255,247,0.01) 100%),
        repeating-linear-gradient(0deg, rgba(0,255,247,0.05) 0px, rgba(0,255,247,0.05) 1px, transparent 1px, transparent 28px),
        repeating-linear-gradient(90deg, rgba(0,255,247,0.05) 0px, rgba(0,255,247,0.05) 1px, transparent 1px, transparent 28px);
    display: flex;
    align-items: center;
    justify-content: center;
}

.board-decoration.top {
    border-bottom: 1px solid rgba(0, 255, 247, 0.15);
}

.board-decoration.bottom {
    border-top: 1px solid rgba(0, 255, 247, 0.15);
}

.deco-line, .deco-text {
    display: none;
}

/* 画布容器 - 保持固定比例 */
.canvas-container {
    position: relative;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.2);
    padding: 0;
    margin: 0;
    overflow: hidden;
}

canvas { display: block; }

.side-panel {
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-width: 160px;
    /* 高度由内容决定，与游戏框对齐 */
    max-height: 720px;
    overflow-y: auto;
    overflow-x: hidden;
    /* 添加与游戏框相同的边框设置，确保对齐 */
    border: 4px solid transparent;
    box-sizing: border-box;
}

.panel-box {
    background: #0a0a1a;
    border: 3px solid #ff00ff;
    box-shadow: 0 0 10px rgba(255, 0, 255, 0.3);
    padding: 10px;
    flex-shrink: 0;
}

.panel-box.info-row { display: flex; gap: 10px; }
.panel-box.info-row .info-item { flex: 1; text-align: center; }
.panel-box.info-row .panel-title { margin-bottom: 5px; }
.panel-box.info-row .panel-value { font-size: 22px; }

.panel-title {
    color: #ff00ff;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 2px;
    margin-bottom: 8px;
    text-shadow: 0 0 10px #ff00ff;
}

.panel-value {
    color: #00fff7;
    font-size: 26px;
    font-weight: bold;
    text-shadow: 0 0 15px #00fff7;
}

.next-preview {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 70px;
}

/* Theme selector */
.theme-selector { display: flex; gap: 6px; flex-wrap: wrap; }
.theme-btn {
    width: 24px;
    height: 24px;
    border: 2px solid #333;
    cursor: pointer;
    transition: all 0.2s;
    padding: 0;
}
.theme-btn:hover { transform: scale(1.1); border-color: #fff; }
.theme-btn.active { border-color: #fff; box-shadow: 0 0 8px rgba(255,255,255,0.5); }

/* Combo */
.combo-display {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    font-size: 36px;
    font-weight: bold;
    color: #ffd700;
    text-shadow: 0 0 20px #ffd700, 2px 2px 0 #ff6b35;
    opacity: 0;
    pointer-events: none;
    z-index: 20;
}
.combo-display.show { animation: comboPopup 0.8s ease-out forwards; }
@keyframes comboPopup {
    0% { opacity: 0; transform: translate(-50%, -50%) scale(0.5); }
    20% { opacity: 1; transform: translate(-50%, -50%) scale(1.2); }
    40% { transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(-50%, -80%) scale(1); }
}

/* Perfect display */
.perfect-display {
    position: absolute;
    top: 25%; left: 50%;
    transform: translate(-50%, -50%);
    font-size: 28px;
    font-weight: bold;
    color: #ff00ff;
    text-shadow: 0 0 30px #ff00ff, 0 0 60px #ff00ff, 4px 4px 0 #00fff7;
    opacity: 0;
    pointer-events: none;
    z-index: 25;
    letter-spacing: 4px;
}
.perfect-display.show {
    animation: perfectPopup 1.2s ease-out forwards;
}
@keyframes perfectPopup {
    0% { opacity: 0; transform: translate(-50%, -50%) scale(0.3) rotate(-10deg); }
    15% { opacity: 1; transform: translate(-50%, -50%) scale(1.3) rotate(5deg); }
    30% { transform: translate(-50%, -50%) scale(1) rotate(0deg); }
    50% { transform: translate(-50%, -50%) scale(1.05); }
    70% { transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(-50%, -100%) scale(0.8); }
}

/* Leaderboard */
.leaderboard { max-height: 100px; overflow-y: auto; }
.leaderboard-list { list-style: none; }
.leaderboard-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 4px 0;
    border-bottom: 1px solid #2a2a4a;
    font-size: 12px;
}
.leaderboard-rank { color: #ff00ff; width: 20px; font-weight: bold; }
.leaderboard-rank.top-1 { color: #ffd700; text-shadow: 0 0 10px #ffd700; }
.leaderboard-rank.top-2 { color: #c0c0c0; text-shadow: 0 0 10px #c0c0c0; }
.leaderboard-rank.top-3 { color: #cd7f32; text-shadow: 0 0 10px #cd7f32; }
.leaderboard-name { color: #00fff7; flex: 1; margin: 0 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leaderboard-score { color: #fff; font-weight: bold; }
.leaderboard-empty { color: #666; text-align: center; padding: 15px 0; font-size: 11px; }

.clear-btn {
    width: 100%;
    margin-top: 8px;
    padding: 6px;
    background: transparent;
    border: 2px solid #ff00ff;
    color: #ff00ff;
    font-family: 'Courier New', monospace;
    font-size: 10px;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 1px;
    transition: all 0.2s;
}
.clear-btn:hover { background: #ff00ff; color: #0a0a1a; }

/* Custom Modal */
.custom-modal {
    display: none;
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 9999;
    justify-content: center;
    align-items: center;
}
.custom-modal.show {
    display: flex;
}
.custom-modal-content {
    background: #0a0a1a;
    border: 4px solid #00fff7;
    box-shadow: 0 0 30px rgba(0, 255, 247, 0.5);
    padding: 30px 40px;
    text-align: center;
    max-width: 320px;
}
.custom-modal-text {
    color: #00fff7;
    font-size: 18px;
    margin-bottom: 25px;
    text-shadow: 0 0 10px #00fff7;
}
.custom-modal-buttons {
    display: flex;
    gap: 15px;
    justify-content: center;
}
.custom-modal-btn {
    padding: 10px 25px;
    font-family: 'Courier New', monospace;
    font-size: 14px;
    cursor: pointer;
    border: 2px solid;
    transition: all 0.2s;
    text-transform: uppercase;
    letter-spacing: 1px;
}
.custom-modal-btn.confirm {
    background: transparent;
    border-color: #00fff7;
    color: #00fff7;
}
.custom-modal-btn.confirm:hover {
    background: #00fff7;
    color: #0a0a1a;
}
.custom-modal-btn.cancel {
    background: transparent;
    border-color: #ff00ff;
    color: #ff00ff;
}
.custom-modal-btn.cancel:hover {
    background: #ff00ff;
    color: #0a0a1a;
}

/* Controls */
.controls { font-size: 10px; color: #666; line-height: 1.8; }
.controls span { color: #00fff7; }

/* Overlay screens */
.overlay-screen {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(10, 10, 26, 0.98);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    z-index: 100;
    padding: 20px;
}
.overlay-screen.hidden { display: none !important; }

.title {
    font-size: 28px;
    color: #00fff7;
    text-shadow: 0 0 20px #00fff7, 4px 4px 0 #ff00ff;
    margin-bottom: 20px;
    letter-spacing: 4px;
}
.subtitle { color: #ff00ff; font-size: 12px; margin-bottom: 30px; text-shadow: 0 0 10px #ff00ff; }

.game-btn {
    background: transparent;
    border: 3px solid #00fff7;
    color: #00fff7;
    padding: 12px 30px;
    font-size: 16px;
    font-family: 'Courier New', monospace;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 2px;
    transition: all 0.2s;
    text-shadow: 0 0 10px #00fff7;
    box-shadow: 0 0 20px rgba(0, 255, 247, 0.3);
    margin: 5px;
}
.game-btn:hover { background: #00fff7; color: #0a0a1a; box-shadow: 0 0 40px rgba(0, 255, 247, 0.6); }

.game-over-text { font-size: 24px; color: #ff0044; text-shadow: 0 0 20px #ff0044, 3px 3px 0 #ff00ff; margin-bottom: 15px; letter-spacing: 3px; }
.final-score { color: #00fff7; font-size: 18px; margin-bottom: 12px; }
.name-input-container { margin-bottom: 15px; text-align: center; }
.name-input-label { color: #ff00ff; font-size: 12px; margin-bottom: 8px; display: block; }
.name-input {
    background: #0a0a1a;
    border: 3px solid #00fff7;
    color: #00fff7;
    padding: 8px 12px;
    font-family: 'Courier New', monospace;
    font-size: 14px;
    text-align: center;
    width: 160px;
    outline: none;
}
.name-input:focus { box-shadow: 0 0 20px rgba(0, 255, 247, 0.5); }
.name-input::placeholder { color: #446666; }
.highscore-badge { color: #ffd700; font-size: 12px; margin-top: 6px; text-shadow: 0 0 10px #ffd700; }
.highscore-badge.hidden { display: none; }

.pause-text { font-size: 32px; color: #ff00ff; text-shadow: 0 0 30px #ff00ff; letter-spacing: 5px; animation: pulse 1s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }

/* Mobile Layout */
.mobile-game { display: none; flex-direction: column; width: 100%; height: 100%; }

.mobile-top-bar {
    display: flex;
    justify-content: space-around;
    align-items: center;
    padding: 8px;
    background: rgba(10, 10, 26, 0.9);
    border-bottom: 2px solid #ff00ff;
    flex-shrink: 0;
}
.mobile-stat { display: flex; flex-direction: column; align-items: center; }
.mobile-stat-label { font-size: 10px; color: #ff00ff; }
.mobile-stat-value { font-size: 16px; color: #00fff7; font-weight: bold; }

.mobile-game-area { flex: 1; display: flex; justify-content: center; align-items: center; padding: 0; min-height: 0; overflow: visible; background: #0a0a1a; }
.mobile-board-wrapper { position: relative; border: 3px solid #00fff7; box-shadow: 0 0 15px rgba(0, 255, 247, 0.5); background: #0a0a1a; }
#mobileCanvas { display: block; }
.mobile-next-preview { position: absolute; top: 3px; right: 3px; width: 45px; height: 45px; background: rgba(10,10,26,0.9); border: 2px solid #ff00ff; display: flex; justify-content: center; align-items: center; }

/* Mobile Controls - Redesigned */
.mobile-controls {
    display: flex;
    flex-direction: column;
    gap: 0;
    padding: 8px;
    background: linear-gradient(180deg, rgba(10,10,26,0.98) 0%, rgba(15,15,35,1) 100%);
    border-top: 2px solid rgba(0, 255, 247, 0.3);
    flex-shrink: 0;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

.mobile-controls-main {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    padding: 5px 0;
}

/* D-Pad Section (Left) */
.mobile-dpad {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    justify-content: center;
}

.dpad-center {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 50px;
    height: 50px;
}

.mobile-btn.soft-drop {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    border: 2px solid #00fff7;
    background: rgba(0, 255, 247, 0.1);
    color: #00fff7;
    box-shadow: 0 0 15px rgba(0, 255, 247, 0.3);
    transition: all 0.1s ease;
}

.mobile-btn.soft-drop:active {
    background: rgba(0, 255, 247, 0.3);
    transform: scale(0.9);
    box-shadow: 0 0 25px rgba(0, 255, 247, 0.5);
}

/* Action Buttons Section (Right) */
.mobile-actions {
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: center;
    justify-content: center;
    padding: 5px;
}

/* Base Button Style */
.mobile-btn {
    border: 2px solid;
    background: rgba(0, 0, 0, 0.4);
    color: inherit;
    font-family: 'Courier New', monospace;
    font-weight: bold;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;  /* 关键：禁用默认触摸行为，让 Pointer Events 正常工作 */
    transition: all 0.1s ease;
    position: relative;
    overflow: hidden;
}

.mobile-btn::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.3);
    transform: translate(-50%, -50%);
    transition: width 0.3s, height 0.3s;
}

.mobile-btn:active::before {
    width: 150px;
    height: 150px;
}

.mobile-btn:active {
    transform: scale(0.92);
    opacity: 0.8;
}

/* D-Pad Buttons */
.mobile-btn.dpad-btn {
    width: 70px;
    height: 70px;
    border-radius: 12px;
    border-color: #00fff7;
    color: #00fff7;
    box-shadow: 0 0 15px rgba(0, 255, 247, 0.3), inset 0 0 10px rgba(0, 255, 247, 0.1);
    background: linear-gradient(135deg, rgba(0, 255, 247, 0.1) 0%, rgba(0, 255, 247, 0.05) 100%);
}

.mobile-btn.dpad-btn:active {
    background: linear-gradient(135deg, rgba(0, 255, 247, 0.3) 0%, rgba(0, 255, 247, 0.15) 100%);
    box-shadow: 0 0 25px rgba(0, 255, 247, 0.5), inset 0 0 15px rgba(0, 255, 247, 0.2);
}

/* Rotate Button */
.mobile-btn.rotate {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    border-color: #aa00ff;
    color: #aa00ff;
    box-shadow: 0 0 20px rgba(170, 0, 255, 0.4), inset 0 0 15px rgba(170, 0, 255, 0.1);
    background: linear-gradient(135deg, rgba(170, 0, 255, 0.15) 0%, rgba(170, 0, 255, 0.05) 100%);
}

.mobile-btn.rotate:active {
    background: linear-gradient(135deg, rgba(170, 0, 255, 0.35) 0%, rgba(170, 0, 255, 0.15) 100%);
    box-shadow: 0 0 30px rgba(170, 0, 255, 0.6), inset 0 0 20px rgba(170, 0, 255, 0.2);
    transform: scale(0.9) rotate(90deg);
}

/* Hard Drop Button */
.mobile-btn.hard-drop {
    width: 80px;
    height: 80px;
    border-radius: 50%;
    border-color: #ff6b35;
    color: #ff6b35;
    box-shadow: 0 0 20px rgba(255, 107, 53, 0.4), inset 0 0 15px rgba(255, 107, 53, 0.1);
    background: linear-gradient(135deg, rgba(255, 107, 53, 0.15) 0%, rgba(255, 107, 53, 0.05) 100%);
}

.mobile-btn.hard-drop:active {
    background: linear-gradient(135deg, rgba(255, 107, 53, 0.35) 0%, rgba(255, 107, 53, 0.15) 100%);
    box-shadow: 0 0 30px rgba(255, 107, 53, 0.6), inset 0 0 20px rgba(255, 107, 53, 0.2);
}

/* Pause Button */
.mobile-controls-bottom {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 8px 0 4px;
}

.mobile-btn.pause {
    width: 50px;
    height: 40px;
    border-radius: 8px;
    border-color: #ff00ff;
    color: #ff00ff;
    box-shadow: 0 0 10px rgba(255, 0, 255, 0.3);
    background: rgba(255, 0, 255, 0.08);
}

.mobile-btn.pause:active {
    background: rgba(255, 0, 255, 0.25);
    box-shadow: 0 0 20px rgba(255, 0, 255, 0.5);
}

/* Responsive adjustments */
@media (max-height: 600px) {
    .mobile-btn.dpad-btn {
        width: 60px;
        height: 60px;
    }
    
    .mobile-btn.rotate,
    .mobile-btn.hard-drop {
        width: 70px;
        height: 70px;
    }
    
    .mobile-controls {
        padding: 6px;
    }
}

@media (max-width: 360px) {
    .mobile-btn.dpad-btn {
        width: 60px;
        height: 60px;
    }
    
    .mobile-btn.rotate,
    .mobile-btn.hard-drop {
        width: 70px;
        height: 70px;
    }
}

.fullscreen-prompt {
    display: none;
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: #0a0a1a;
    z-index: 2000;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 30px;
    text-align: center;
}
.fullscreen-prompt.show { display: flex; }
.fullscreen-prompt .hint { color: #666; font-size: 12px; margin-top: 20px; }

/* Responsive */
@media (max-width: 768px) {
    .desktop-game { display: none !important; }
    .mobile-game { display: flex; }
    .fullscreen-prompt.show { display: flex; }
}
@media (min-width: 769px) {
    .mobile-game,
    .fullscreen-prompt:not(.show) { display: none !important; }
}
@media (max-height: 600px) {
    .mobile-btn { height: 42px; font-size: 16px; }
    .mobile-btn.drop { font-size: 12px; }
}