/* ========================================
   alun的方块 - 样式表 (移动端左右布局版)
   ======================================== */

/* CSS Variables */
:root {
    --color-bg: #0f172a;
    --color-bg-light: #1e293b;
    --color-bg-dark: #020617;
    --color-border: #334155;
    --color-text: #e2e8f0;
    --color-text-muted: #94a3b8;

    --color-cyan: #22d3ee;
    --color-yellow: #facc15;
    --color-green: #22c55e;
    --color-purple: #a855f7;
    --color-pink: #ec4899;
    --color-blue: #3b82f6;
    --color-orange: #f97316;
    --color-red: #ef4444;

    --font-digital: 'Orbitron', sans-serif;
    --font-main: 'Noto Sans SC', sans-serif;

    /* 动态格子大小 - 移动端基于屏幕高度 */
    --cell-size: min(4vh, 6vw, 22px);
    --board-gap: 1px;
    --board-padding: 4px;
}

/* 桌面端格子大小 */
@media (min-width: 640px) {
    :root {
        --cell-size: 28px;
        --board-padding: 8px;
    }
}

/* Reset & Base */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    height: 100%;
    height: -webkit-fill-available;
}

body {
    font-family: var(--font-main);
    background: radial-gradient(ellipse at top, var(--color-bg-light), var(--color-bg), var(--color-bg-dark));
    color: var(--color-text);
    min-height: 100vh;
    min-height: -webkit-fill-available;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    touch-action: none;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* Container */
#game-container {
    padding: 8px;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    max-width: 900px;
    display: flex;
    align-items: center;
    justify-content: center;
}

@media (min-width: 640px) {
    #game-container {
        padding: 1rem;
        height: auto;
    }
}

/* ========================================
   移动端：左右布局
   左边：标题 + 游戏板
   右边：分数 | Next | 控制按钮
   ======================================== */

#layout {
    display: flex;
    flex-direction: row;
    align-items: stretch;
    justify-content: center;
    gap: 8px;
    width: 100%;
    height: 100%;
    max-height: 100vh;
    max-height: 100dvh;
}

@media (min-width: 640px) {
    #layout {
        gap: 1.5rem;
        height: auto;
        max-height: none;
        align-items: flex-start;
    }
}

/* ========================================
   Game Section - 左侧区域（标题 + 游戏板）
   ======================================== */
#game-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

@media (min-width: 640px) {
    #game-section {
        gap: 0;
    }
}

/* Mobile Title - 移动端显示的标题 */
#mobile-title {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 4px 8px;
    background: rgba(30, 41, 59, 0.7);
    border-radius: 0.4rem;
    border: 1px solid var(--color-border);
    width: 100%;
}

#mobile-title .icon-gamepad {
    color: var(--color-cyan);
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

#mobile-title span {
    font-family: var(--font-digital);
    font-size: 0.85rem;
    font-weight: 700;
    background: linear-gradient(to right, var(--color-cyan), var(--color-purple));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    white-space: nowrap;
}

@media (min-width: 640px) {
    #mobile-title {
        display: none;
    }
}

/* ========================================
   Left Column - 桌面端显示标题和分数
   ======================================== */
#left-column {
    display: none;
}

@media (min-width: 640px) {
    #left-column {
        display: flex;
        flex-direction: column;
        gap: 1rem;
        width: auto;
    }
}

/* Title */
#title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}

#title .icon-gamepad {
    color: var(--color-cyan);
    width: 32px;
    height: 32px;
}

#title h1 {
    font-family: var(--font-digital);
    font-size: 1.875rem;
    font-weight: 700;
    background: linear-gradient(to right, var(--color-cyan), var(--color-purple));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    letter-spacing: -0.05em;
}

/* Game Stats - Desktop */
#game-stats-desktop {
    display: none;
}

@media (min-width: 640px) {
    #game-stats-desktop {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
    }
}

.stat-box {
    background: rgba(30, 41, 59, 0.7);
    padding: 0.5rem 0.6rem;
    border-radius: 0.4rem;
    border: 1px solid var(--color-border);
    text-align: center;
}

@media (min-width: 640px) {
    .stat-box {
        padding: 0.75rem;
    }
}

.stat-label {
    font-size: 0.6rem;
    color: var(--color-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

@media (min-width: 640px) {
    .stat-label {
        font-size: 0.75rem;
        letter-spacing: 0.1em;
    }
}

.stat-value {
    font-family: var(--font-digital);
    font-size: 0.9rem;
}

@media (min-width: 640px) {
    .stat-value {
        font-size: 1.5rem;
    }
}

.stat-value.cyan {
    color: var(--color-cyan);
}

.stat-value.yellow {
    color: var(--color-yellow);
}

.stat-value.green {
    color: var(--color-green);
}

/* ========================================
   Center - Game Board (移动端在左侧)
   ======================================== */
#board-wrapper {
    position: relative;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

#game-board {
    display: grid;
    grid-template-columns: repeat(10, var(--cell-size));
    grid-template-rows: repeat(20, var(--cell-size));
    gap: var(--board-gap);
    background: rgba(2, 6, 23, 0.7);
    padding: var(--board-padding);
    border-radius: 0.5rem;
    border: 2px solid var(--color-border);
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
}

/* Cells */
.cell {
    width: 100%;
    height: 100%;
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 2px;
    transition: background-color 75ms ease;
}

.cell.empty {
    background: rgba(30, 41, 59, 0.5);
}

.cell.filled {
    position: relative;
}

.cell.filled::after {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(255, 255, 255, 0.1);
}

/* Tetromino Colors */
.cell.I {
    background: var(--color-cyan);
    box-shadow: 0 0 6px rgba(34, 211, 238, 0.8);
    border-color: #a5f3fc;
}

.cell.J {
    background: var(--color-blue);
    box-shadow: 0 0 6px rgba(59, 130, 246, 0.8);
    border-color: #93c5fd;
}

.cell.L {
    background: var(--color-orange);
    box-shadow: 0 0 6px rgba(249, 115, 22, 0.8);
    border-color: #fdba74;
}

.cell.O {
    background: var(--color-yellow);
    box-shadow: 0 0 6px rgba(250, 204, 21, 0.8);
    border-color: #fef08a;
}

.cell.S {
    background: var(--color-green);
    box-shadow: 0 0 6px rgba(34, 197, 94, 0.8);
    border-color: #86efac;
}

.cell.T {
    background: var(--color-purple);
    box-shadow: 0 0 6px rgba(168, 85, 247, 0.8);
    border-color: #d8b4fe;
}

.cell.Z {
    background: var(--color-red);
    box-shadow: 0 0 6px rgba(239, 68, 68, 0.8);
    border-color: #fca5a5;
}

/* Overlay */
#overlay {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    z-index: 10;
    border-radius: 0.5rem;
}

#overlay.hidden {
    display: none;
}

#overlay-content {
    text-align: center;
    padding: 1rem;
    background: var(--color-bg-light);
    border: 1px solid var(--color-border);
    border-radius: 0.75rem;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
}

#overlay-title {
    font-family: var(--font-digital);
    font-size: 1.25rem;
    color: var(--color-cyan);
    margin-bottom: 0.5rem;
    text-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
}

@media (min-width: 640px) {
    #overlay-title {
        font-size: 2.25rem;
    }
}

#overlay-score {
    color: var(--color-text-muted);
    font-size: 0.8rem;
}

@media (min-width: 640px) {
    #overlay-score {
        font-size: 1rem;
    }
}

/* ========================================
   Right Column - 移动端纵向布局：分数 | Next | 控制
   ======================================== */
#right-column {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
    max-width: 140px;
    align-self: flex-start;
    justify-content: flex-start;
}

@media (min-width: 640px) {
    #right-column {
        width: 12rem;
        max-width: none;
        gap: 1rem;
        align-self: flex-start;
        justify-content: flex-start;
    }
}

/* Mobile Stats - 右上区域 */
#game-stats-mobile {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 4px;
}

@media (min-width: 640px) {
    #game-stats-mobile {
        display: none;
    }
}

#game-stats-mobile .stat-box {
    flex: 1;
    min-width: 40px;
    padding: 0.35rem 0.4rem;
}

#game-stats-mobile .stat-box.full-width {
    flex-basis: 100%;
}

/* Next Piece - 右中区域 */
#next-piece-container {
    background: rgba(30, 41, 59, 0.7);
    padding: 0.5rem;
    border-radius: 0.5rem;
    border: 1px solid var(--color-border);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

@media (min-width: 640px) {
    #next-piece-container {
        padding: 1rem;
        aspect-ratio: 1;
        width: 100%;
    }
}

.next-label {
    font-size: 0.55rem;
    color: var(--color-text-muted);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    margin-bottom: 0.25rem;
}

@media (min-width: 640px) {
    .next-label {
        font-size: 0.75rem;
        margin-bottom: 0.5rem;
    }
}

#next-piece {
    display: grid;
    gap: var(--board-gap);
}

#next-piece .cell {
    width: 14px;
    height: 14px;
}

@media (min-width: 640px) {
    #next-piece .cell {
        width: 20px;
        height: 20px;
    }
}

/* AI Section - 移动端隐藏 */
#ai-section {
    display: none;
}

@media (min-width: 640px) {
    #ai-section {
        display: block;
        background: rgba(30, 41, 59, 0.8);
        border: 1px solid rgba(168, 85, 247, 0.3);
        padding: 1rem;
        border-radius: 0.5rem;
        box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
        position: relative;
        overflow: hidden;
        animation: fadeIn 0.3s ease;
    }
}

#ai-section.hidden {
    display: none !important;
}

#ai-section::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
    background: linear-gradient(to right, var(--color-purple), var(--color-pink));
}

.ai-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
    color: #d8b4fe;
    font-size: 0.875rem;
    font-weight: 700;
}

.ai-header svg {
    flex-shrink: 0;
}

#ai-comment {
    font-size: 0.875rem;
    color: var(--color-text);
    min-height: 40px;
}

.ai-loading {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--color-text-muted);
}

.ai-loading svg {
    animation: spin 1s linear infinite;
}

@keyframes spin {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ========================================
   Controls - 右下区域
   ======================================== */
#controls {
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1;
    min-height: 0;
    user-select: none;
}

@media (min-width: 640px) {
    #controls {
        width: 100%;
        max-width: 300px;
        margin-top: 0.5rem;
        gap: 1rem;
        flex: none;
    }
}

/* Action Buttons */
#action-buttons {
    display: flex;
    justify-content: center;
    gap: 4px;
    flex-shrink: 0;
}

@media (min-width: 640px) {
    #action-buttons {
        gap: 1rem;
        margin-bottom: 0.5rem;
    }
}

.action-btn {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.3rem 0.5rem;
    background: var(--color-bg-light);
    border: 1px solid var(--color-border);
    border-radius: 0.4rem;
    color: var(--color-text-muted);
    font-size: 0.65rem;
    cursor: pointer;
    transition: all 0.15s ease;
    font-family: var(--font-main);
}

@media (min-width: 640px) {
    .action-btn {
        padding: 0.5rem 1rem;
        font-size: 0.875rem;
        gap: 0.5rem;
        border-radius: 0.5rem;
    }
}

.action-btn:hover {
    background: var(--color-border);
}

.action-btn:active {
    background: var(--color-cyan);
    color: var(--color-bg);
}

.action-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.action-btn svg {
    width: 12px;
    height: 12px;
}

@media (min-width: 640px) {
    .action-btn svg {
        width: 16px;
        height: 16px;
    }
}

.action-btn svg.hidden {
    display: none;
}

/* D-Pad - 移动端控制按钮 */
#dpad {
    display: flex;
    flex-direction: column;
    gap: 3px;
    flex: 1;
    justify-content: center;
    align-items: center;
}

@media (min-width: 640px) {
    #dpad {
        display: none;
    }
}

.dpad-row {
    display: flex;
    justify-content: center;
    gap: 3px;
}

.dpad-spacer {
    width: 40px;
    height: 40px;
}

.dpad-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(51, 65, 85, 0.9);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.15);
    color: white;
    cursor: pointer;
    transition: all 0.1s ease;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.dpad-btn svg {
    width: 18px;
    height: 18px;
    pointer-events: none;
}

.dpad-btn:active {
    background: rgba(6, 182, 212, 0.9);
    transform: scale(0.95);
    box-shadow: 0 2px 8px rgba(6, 182, 212, 0.4);
}

.dpad-btn.action {
    background: linear-gradient(135deg, rgba(6, 182, 212, 0.9), rgba(59, 130, 246, 0.9));
    width: 44px;
    height: 44px;
    box-shadow: 0 4px 16px rgba(6, 182, 212, 0.3);
    border: 2px solid rgba(255, 255, 255, 0.25);
}

.dpad-btn.action svg {
    width: 22px;
    height: 22px;
}

.dpad-btn.action:active {
    background: linear-gradient(135deg, rgba(6, 182, 212, 1), rgba(59, 130, 246, 1));
    transform: scale(0.92);
}

/* PC Instructions */
#pc-instructions {
    display: none;
    text-align: center;
    color: var(--color-text-muted);
    font-size: 0.75rem;
    margin-top: 1rem;
}

@media (min-width: 640px) {
    #pc-instructions {
        display: block;
    }
}

#pc-instructions p {
    margin-bottom: 0.25rem;
}

/* Utility */
.hidden {
    display: none !important;
}

/* ========================================
   小屏幕适配 (iPhone SE 等)
   ======================================== */
@media (max-width: 375px) {
    :root {
        --cell-size: min(3.8vh, 5.5vw, 20px);
    }

    .dpad-btn {
        width: 36px;
        height: 36px;
    }

    .dpad-spacer {
        width: 36px;
        height: 36px;
    }

    .dpad-btn.action {
        width: 40px;
        height: 40px;
    }

    .dpad-btn svg {
        width: 16px;
        height: 16px;
    }

    .dpad-btn.action svg {
        width: 20px;
        height: 20px;
    }
}

/* 超小屏幕高度适配 */
@media (max-height: 600px) {
    :root {
        --cell-size: min(3.5vh, 5vw, 18px);
    }

    .dpad-btn {
        width: 34px;
        height: 34px;
    }

    .dpad-spacer {
        width: 34px;
        height: 34px;
    }

    .dpad-btn.action {
        width: 38px;
        height: 38px;
    }
}

/* 横屏模式 */
@media (max-height: 500px) and (orientation: landscape) {
    :root {
        --cell-size: min(4vh, 3vw, 22px);
    }

    #right-column {
        max-width: 160px;
    }

    .dpad-btn {
        width: 38px;
        height: 38px;
    }

    .dpad-spacer {
        width: 38px;
        height: 38px;
    }
}

/* 安全区域适配 (iPhone X 等刘海屏) */
@supports (padding: env(safe-area-inset-bottom)) {
    #game-container {
        padding-bottom: calc(8px + env(safe-area-inset-bottom));
        padding-left: calc(8px + env(safe-area-inset-left));
        padding-right: calc(8px + env(safe-area-inset-right));
    }
}