/* ============================================================
   Match-3 Game — 深色主題樣式表（手機優化版）
   ============================================================ */

/* ---------- CSS 自訂屬性 ---------- */
:root {
    --bg-primary: #0a0a1a;
    --bg-secondary: #12122a;
    --bg-card: #1a1a3e;
    --bg-card-hover: #22224e;

    --text-primary: #e8e8f0;
    --text-secondary: #9090b0;
    --text-accent: #c8c8ff;

    --accent-cyan: #00e5ff;
    --accent-magenta: #ff00e5;
    --accent-gold: #ffd700;
    --accent-green: #00ff88;

    --glow-cyan: 0 0 20px rgba(0, 229, 255, 0.4);
    --glow-magenta: 0 0 20px rgba(255, 0, 229, 0.4);
    --glow-gold: 0 0 20px rgba(255, 215, 0, 0.4);

    --border-radius: 12px;
    --transition-speed: 0.3s;

    --font-primary: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;

    /* 手機 safe-area 支援 (iPhone 瀏海/底部手勢列) */
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ---------- Reset & Base ---------- */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    /* 防止觸控長按選取文字 */
    -webkit-user-select: none;
    user-select: none;
    /* 移除 iOS 觸控高亮 */
    -webkit-tap-highlight-color: transparent;
}

html {
    font-size: 16px;
    /* 防止 iOS Safari 字型自動縮放 */
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    font-family: var(--font-primary);
    background: var(--bg-primary);
    color: var(--text-primary);
    /* 使用 dvh (dynamic viewport height) 解決 iOS Safari 位址列問題 */
    min-height: 100dvh;
    min-height: 100vh;
    /* fallback */
    display: flex;
    justify-content: center;
    align-items: flex-start;
    /* 允許縱向捲動，防止畫面被截斷 */
    overflow-x: hidden;
    overflow-y: auto;
    /* 動態漸層背景 */
    background-image:
        radial-gradient(ellipse at 20% 50%, rgba(0, 229, 255, 0.06) 0%, transparent 50%),
        radial-gradient(ellipse at 80% 50%, rgba(255, 0, 229, 0.06) 0%, transparent 50%),
        radial-gradient(ellipse at 50% 100%, rgba(255, 215, 0, 0.04) 0%, transparent 50%);
    background-attachment: fixed;
    /* 支援 iPhone safe-area */
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
}

/* ---------- 遊戲容器 ---------- */
.game-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    /* 使用 CSS var 動態注入，由 JS 控制間距 */
    gap: 10px;
    padding: 12px 12px;
    max-width: 800px;
    width: 100%;
    margin: 0 auto;
}

/* ---------- 標題 ---------- */
.game-header {
    text-align: center;
}

.game-title {
    font-size: 1.6rem;
    /* 縮小一點以容納在兩側按鈕中間 */
    font-weight: 900;
    letter-spacing: -0.02em;
    background: linear-gradient(135deg, var(--accent-cyan), var(--accent-magenta));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    filter: drop-shadow(0 0 30px rgba(0, 229, 255, 0.3));
    line-height: 1;
}

.title-gem {
    -webkit-text-fill-color: initial;
    font-size: 1.7rem;
    display: inline-block;
    animation: gemFloat 2s ease-in-out infinite;
}

@keyframes gemFloat {

    0%,
    100% {
        transform: translateY(0) rotate(0deg);
    }

    50% {
        transform: translateY(-4px) rotate(10deg);
    }
}

.game-subtitle {
    font-size: 0.75rem;
    color: var(--text-secondary);
    letter-spacing: 0.15em;
    text-transform: uppercase;
    margin-top: 2px;
}

/* ---------- 計分板 ---------- */
.scoreboard {
    display: flex;
    gap: 8px;
    width: 100%;
    justify-content: center;
    flex-wrap: nowrap;
    /* 強制單行，避免換行 */
}

.score-item {
    background: var(--bg-card);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 10px;
    padding: 8px 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    flex: 1;
    /* 均分空間，不溢出 */
    min-width: 0;
    /* 允許 flex 子項縮小 */
    transition: background var(--transition-speed) ease;
}

.score-label {
    font-size: 0.62rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    margin-bottom: 2px;
    white-space: nowrap;
}

.score-value {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--accent-cyan);
    text-shadow: var(--glow-cyan);
    transition: all 0.2s ease;
    white-space: nowrap;
}

.score-value.pop {
    transform: scale(1.3);
    color: var(--accent-gold);
    text-shadow: var(--glow-gold);
}

.combo-multiplier {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--accent-magenta);
    text-shadow: var(--glow-magenta);
    opacity: 0;
    transform: translateY(-5px);
    transition: all 0.3s ease;
    display: block;
    min-height: 1em;
}

.combo-multiplier.show {
    opacity: 1;
    transform: translateY(0);
    animation: comboPulse 0.6s ease-out;
}

.combo-multiplier.high {
    color: var(--accent-gold);
    text-shadow: var(--glow-gold), 0 0 10px rgba(255, 215, 0, 0.5);
    animation: comboGlow 0.8s ease-out;
}

.combo-multiplier.amazing {
    color: #ff6b6b;
    text-shadow: 0 0 15px rgba(255, 107, 107, 0.8), 0 0 30px rgba(255, 107, 107, 0.4);
    animation: comboAmazing 1s ease-out;
}

@keyframes comboPulse {
    0% { transform: scale(0.5) translateY(-10px); opacity: 0; }
    50% { transform: scale(1.2) translateY(0); opacity: 1; }
    100% { transform: scale(1) translateY(0); opacity: 1; }
}

@keyframes comboGlow {
    0% { transform: scale(1); filter: brightness(1); }
    50% { transform: scale(1.3); filter: brightness(1.5); }
    100% { transform: scale(1); filter: brightness(1); }
}

@keyframes comboAmazing {
    0% { transform: scale(0.3) rotate(-10deg); opacity: 0; }
    30% { transform: scale(1.5) rotate(5deg); opacity: 1; }
    60% { transform: scale(0.9) rotate(-3deg); }
    80% { transform: scale(1.1) rotate(2deg); }
    100% { transform: scale(1) rotate(0); opacity: 1; }
}

/* ---------- Canvas 包裝器 ---------- */
.canvas-wrapper {
    position: relative;
    border-radius: var(--border-radius);
    overflow: hidden;
    box-shadow:
        0 0 40px rgba(0, 229, 255, 0.1),
        0 0 80px rgba(255, 0, 229, 0.05),
        0 10px 40px rgba(0, 0, 0, 0.5);
    border: 1px solid rgba(255, 255, 255, 0.08);
    /* 讓外框擁有 RWD 能力，自動填滿但保持最大限制與正方形 */
    width: 100%;
    max-width: 600px;
    aspect-ratio: 1 / 1;
    margin: 0 auto;
}

#game-canvas {
    display: block;
    width: 100%;
    height: 100%;
    cursor: pointer;
    background: var(--bg-secondary);
    /* 防止 iOS 觸控選取 */
    -webkit-touch-callout: none;
    touch-action: none;
}

/* ---------- 慶祝影片區 ---------- */
.video-wrapper {
    width: 100%;
    margin-top: 4px;
    border-radius: var(--border-radius);
    overflow: hidden;
    background: var(--bg-card);
    border: 1px solid rgba(255, 255, 255, 0.08);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    display: flex;
    justify-content: center;
    align-items: center;
}

#cheer-video {
    width: 100%;
    height: auto;
    display: block;
    opacity: 0.85;
    transition: opacity 0.3s ease, filter 0.3s ease, transform 0.3s ease;
}

#cheer-video.cheer-active {
    opacity: 1;
    filter: brightness(1.1);
}

/* ---------- 遊戲結束覆蓋層 ---------- */
.overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(10, 10, 26, 0.92);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10;
    animation: fadeIn 0.4s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

.overlay-content {
    text-align: center;
    padding: 24px 20px;
}

.overlay-title {
    font-size: 1.8rem;
    font-weight: 900;
    background: linear-gradient(135deg, var(--accent-gold), var(--accent-magenta));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 12px;
}

.overlay-score {
    font-size: 1rem;
    color: var(--text-secondary);
    margin-bottom: 20px;
}

.overlay-score span {
    color: var(--accent-cyan);
    font-weight: 700;
    font-size: 1.3rem;
    text-shadow: var(--glow-cyan);
}

/* ---------- 控制區 ---------- */
.controls {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
    align-items: center;
}

.mode-selector {
    display: flex;
    gap: 8px;
    width: 100%;
}

.action-buttons {
    display: flex;
    gap: 8px;
    width: 100%;
}

/* ---------- 按鈕（手機觸控最佳化） ---------- */
.btn {
    font-family: var(--font-primary);
    font-size: 0.88rem;
    font-weight: 600;
    /* 手機觸控目標最小高度 44px (Apple HIG) */
    min-height: 44px;
    padding: 10px 16px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    cursor: pointer;
    transition: all var(--transition-speed) ease;
    outline: none;
    letter-spacing: 0.02em;
    /* 觸控回饋 */
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    flex: 1;
    /* 讓按鈕平均分配空間 */
}

/* ---------- 模式導覽 (頂部兩角) ---------- */
.mode-selector.top-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    /* 垂直置中 */
    width: 100%;
    margin-bottom: 6px;
    position: relative;
    z-index: 5;
}

.mode-selector.top-nav .game-header {
    flex: 1;
    text-align: center;
    margin: 0 5px;
}

.mode-selector.top-nav .btn-mode {
    flex: 0 0 auto;
    min-width: 140px;
    font-size: 1rem;
    font-weight: 800;
    padding: 12px 20px;
    min-height: 52px;
    background: rgba(26, 26, 62, 0.7);
    border: 2px solid rgba(0, 229, 255, 0.2);
    border-radius: 14px;
    color: var(--accent-cyan);
    /* 均使用青色字體 */
    text-shadow: 0 0 10px rgba(0, 229, 255, 0.3);
}

.btn-mode.active {
    background: linear-gradient(135deg, rgba(0, 229, 255, 0.15), rgba(255, 0, 229, 0.15));
    color: var(--accent-cyan);
    border-color: var(--accent-cyan);
    box-shadow: var(--glow-cyan);
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent-cyan), var(--accent-magenta));
    color: #fff;
    border: none;
    font-size: 1rem;
    min-height: 48px;
    padding: 12px 32px;
    border-radius: var(--border-radius);
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.btn-primary:active {
    filter: brightness(0.9);
    transform: scale(0.97);
}

.btn-secondary {
    background: var(--bg-card);
    color: var(--text-secondary);
}

.btn-secondary.active {
    color: var(--accent-cyan);
    border-color: rgba(0, 229, 255, 0.3);
    background: rgba(0, 229, 255, 0.05);
}

/* ---------- 桌面端 hover 效果（手機無 hover） ---------- */
@media (hover: hover) {
    .score-item:hover {
        background: var(--bg-card-hover);
        border-color: rgba(255, 255, 255, 0.1);
    }

    .btn-mode:hover {
        background: var(--bg-card-hover);
        color: var(--text-primary);
        border-color: rgba(255, 255, 255, 0.15);
    }

    .btn-primary:hover {
        filter: brightness(1.15);
        box-shadow: 0 0 30px rgba(0, 229, 255, 0.3);
        transform: translateY(-2px);
    }

    .btn-secondary:hover {
        background: var(--bg-card-hover);
        color: var(--text-primary);
        border-color: rgba(255, 255, 255, 0.15);
    }
}

/* ---------- 手機直向（Portrait）主要斷點 ---------- */
@media (max-width: 480px) {
    .game-container {
        gap: 8px;
        padding: 8px 10px;
    }

    .game-title {
        font-size: 1.6rem;
    }

    .title-gem {
        font-size: 1.4rem;
    }

    .score-value {
        font-size: 1.1rem;
    }

    .score-label {
        font-size: 0.58rem;
    }

    .btn {
        font-size: 0.82rem;
        padding: 10px 10px;
    }
}

/* ---------- 極小螢幕（≤ 360px） ---------- */
@media (max-width: 360px) {
    .game-title {
        font-size: 1.4rem;
    }

    .score-item {
        padding: 6px 6px;
    }

    .score-value {
        font-size: 1rem;
    }

    .btn {
        font-size: 0.76rem;
    }
}

/* ---------- 橫向（Landscape）模式 ---------- */
@media (orientation: landscape) and (max-height: 500px) {
    .game-container {
        flex-direction: row;
        flex-wrap: wrap;
        align-items: flex-start;
        max-width: 100%;
        gap: 8px;
        padding: 6px 12px;
    }

    .game-header {
        width: 100%;
        display: flex;
        align-items: center;
        gap: 12px;
        justify-content: center;
    }

    .game-subtitle {
        margin-top: 0;
    }

    .scoreboard {
        width: auto;
        flex: 0 0 auto;
        flex-direction: column;
        gap: 6px;
    }

    .score-item {
        flex: 0 0 auto;
        flex-direction: row;
        gap: 6px;
        padding: 6px 10px;
        min-height: 36px;
    }

    .score-label {
        margin-bottom: 0;
    }

    .canvas-wrapper {
        flex: 1;
        min-width: 0;
    }

    .controls {
        width: auto;
        flex: 0 0 auto;
        gap: 6px;
    }

    .mode-selector,
    .action-buttons {
        flex-direction: column;
        width: auto;
    }

    .btn {
        min-height: 36px;
        font-size: 0.78rem;
        white-space: nowrap;
    }

    .video-wrapper {
        width: 150px;
        margin-top: 0;
    }
}