* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    position: fixed;
    touch-action: none;
}

body {
    font-family: 'Arial', sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #4ECDC4 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    transition: background 0.5s ease;
    margin: 0;
    padding: 0;
}

.container {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    padding: 2vh 2vw;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    width: 95vw;
    max-width: 600px;
    height: 95vh;
    max-height: 95vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

h1 {
    text-align: center;
    color: #333;
    margin-bottom: 1vh;
    font-size: clamp(1.5em, 1vw, 2.5em);
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
    flex-shrink: 0;
}

.game-info {
    display: flex;
    justify-content: space-around;
    align-items: center;
    margin-bottom: 1vh;
    font-size: clamp(0.9em, 2.5vw, 1.2em);
    font-weight: bold;
    color: #555;
    flex-shrink: 0;
    position: relative;
}

.celebration-gif {
    display: flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    max-width: min(30vw, 200px);
    max-height: min(8vh, 80px);
    min-width: 0;
    opacity: 0;
    transform: scale(0.8);
    transition: opacity 0.3s ease-in-out, transform 0.3s ease-in-out;
}

.celebration-gif.show {
    opacity: 1;
    transform: scale(1);
}

.celebration-gif.hide {
    opacity: 0;
    transform: scale(0.8);
}

.celebration-gif img {
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    pointer-events: none;
}

.game-area {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 1vh;
    background: #f0f0f0;
    padding: 1vh 1vw;
    border-radius: 10px;
    flex: 1;
    min-height: 0;
    overflow: hidden;
    position: relative;
}

#gameCanvas {
    border: 3px solid #333;
    border-radius: 5px;
    background: transparent;
    cursor: pointer;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
    touch-action: none;
    display: block;
    opacity: 0;
    transition: opacity 0.2s ease;
    position: relative;
    z-index: 1;
}

#gameCanvas.ready {
    opacity: 1;
}

#effectCanvas {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    pointer-events: none;
    z-index: 9999;
}

.blocks-preview {
    display: flex;
    justify-content: space-around;
    margin-bottom: 1vh;
    gap: 1vw;
    flex-shrink: 0;
    min-height: 12vh;
}

.block-preview {
    flex: 1;
    background: #f5f5f5;
    border: 2px solid #ddd;
    border-radius: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: all 0.3s ease;
    position: relative;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

.block-preview:active {
    transform: scale(0.95);
    border-color: #667eea;
}

.block-preview.selected {
    border-color: #667eea;
    background: #e8e8ff;
    box-shadow: 0 0 20px rgba(102, 126, 234, 0.5);
}

.block-preview.dragging {
    opacity: 0.7;
    transform: scale(1.1);
    z-index: 1000;
}

.block-preview canvas {
    max-width: 100%;
    max-height: 100%;
    pointer-events: none;
}

.controls {
    text-align: center;
    flex-shrink: 0;
}

/* 舊的 restartBtn 樣式已移除，改用 .icon-btn */

/* 拖動時的視覺反饋 */
.drag-ghost {
    position: fixed;
    pointer-events: none;
    z-index: 10000;
    opacity: 0.95;
    transform: translate(-50%, -50%);
    filter: drop-shadow(0 4px 12px rgba(0, 0, 0, 0.4));
    transition: opacity 0.15s ease-out;
    will-change: left, top, opacity;
}

/* 防止雙擊縮放 */
* {
    -webkit-tap-highlight-color: transparent;
}

/* 橫向螢幕優化 */
@media (orientation: landscape) and (max-height: 500px) {
    .container {
        padding: 1vh 2vw;
    }
    
    h1 {
        font-size: clamp(1.2em, 3vw, 1.8em);
        margin-bottom: 0.5vh;
    }
    
    .game-info {
        font-size: clamp(0.8em, 2vw, 1em);
        margin-bottom: 0.5vh;
    }
    
    .blocks-preview {
        min-height: 10vh;
        margin-bottom: 0.5vh;
    }
    
    .icon-btn {
        width: 44px;
        height: 44px;
    }
    
    .icon-btn svg {
        width: 20px;
        height: 20px;
    }
}

/* 頂部按鈕 */
.top-buttons {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1vh;
    padding: 0 1vw;
    flex-shrink: 0;
}

.icon-btn {
    background: linear-gradient(135deg, #667eea 0%, #4ECDC4 100%);
    color: white;
    border: none;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    padding: 0;
}

.icon-btn svg {
    width: 24px;
    height: 24px;
    display: block;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.icon-btn:active {
    transform: scale(0.9);
    box-shadow: 0 2px 10px rgba(102, 126, 234, 0.6);
}

.icon-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

/* 模態框 */
.modal {
    display: none;
    position: fixed;
    z-index: 10000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(5px);
    align-items: center;
    justify-content: center;
    touch-action: none; /* 模態框背景不允許滾動 */
}

.modal.show {
    display: flex;
}

.modal-content {
    background: white;
    padding: 30px;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    max-width: 90%;
    width: 400px;
    max-height: 80vh;
    overflow-y: auto;
    overflow-x: hidden; /* 防止出現左右滑動條 */
    animation: modalSlideIn 0.3s ease;
    touch-action: pan-y; /* 允許垂直滾動 */
    -webkit-overflow-scrolling: touch; /* iOS 平滑滾動 */
    position: relative; /* 確保滾動容器正確 */
}

@keyframes modalSlideIn {
    from {
        transform: translateY(-50px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.modal-content h2 {
    text-align: center;
    color: #333;
    margin-bottom: 20px;
    font-size: 1.8em;
}

.final-score {
    text-align: center;
    font-size: 1.5em;
    font-weight: bold;
    color: #667eea;
    margin-bottom: 20px;
}

.input-group {
    display: flex;
    flex-direction: column;
    gap: 15px;
    margin-bottom: 20px;
}

.input-group input {
    padding: 12px;
    border: 2px solid #ddd;
    border-radius: 10px;
    font-size: 1em;
    text-align: center;
    transition: border-color 0.3s ease;
}

.input-group input:focus {
    outline: none;
    border-color: #667eea;
}

.input-group button {
    background: linear-gradient(135deg, #667eea 0%, #4ECDC4 100%);
    color: white;
    border: none;
    padding: 12px;
    border-radius: 10px;
    font-size: 1em;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.input-group button:active {
    transform: scale(0.95);
}

.close-btn {
    background: #f0f0f0;
    color: #333;
    border: none;
    padding: 10px 20px;
    border-radius: 10px;
    font-size: 1em;
    cursor: pointer;
    width: 100%;
    transition: all 0.3s ease;
}

.close-btn:active {
    background: #e0e0e0;
}

.modal-content.leaderboard-content {
    padding: 20px;
    display: flex;
    flex-direction: column;
    overflow-y: hidden; /* 模態框本身不滾動，由內部列表滾動 */
    overflow-x: hidden;
    max-height: 80vh;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.modal-header h2 {
    margin: 0;
}

.modal-header .close-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    padding: 0;
    font-size: 1.5em;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.leaderboard-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden; /* 防止出現左右滑動條 */
    -webkit-overflow-scrolling: touch; /* iOS 平滑滾動 */
    touch-action: pan-y; /* 允許垂直滾動 */
    position: relative; /* 確保滾動容器正確 */
    /* iOS Safari 需要明確的高度才能滾動 */
    min-height: 0;
    max-height: 100%; /* 確保不超出父容器 */
}

.leaderboard-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px;
    margin-bottom: 10px;
    background: #f5f5f5;
    border-radius: 10px;
    transition: all 0.3s ease;
    min-width: 0; /* 允許 flex 項目縮小 */
    overflow: hidden; /* 防止內容溢出 */
}

.leaderboard-item:hover {
    background: #e8e8ff;
    transform: translateX(5px);
    /* 確保 hover 時不會超出容器 */
    margin-right: -5px;
}

.leaderboard-item.rank-1 {
    background: linear-gradient(135deg, #FFD700 0%, #FFA500 100%);
    color: white;
    font-weight: bold;
}

.leaderboard-item.rank-2 {
    background: linear-gradient(135deg, #C0C0C0 0%, #A0A0A0 100%);
    color: white;
    font-weight: bold;
}

.leaderboard-item.rank-3 {
    background: linear-gradient(135deg, #CD7F32 0%, #8B4513 100%);
    color: white;
    font-weight: bold;
}

.leaderboard-rank {
    font-size: 1.2em;
    font-weight: bold;
    min-width: 30px;
}

.leaderboard-info {
    flex: 1;
    margin-left: 15px;
    min-width: 0; /* 允許 flex 項目縮小 */
    overflow: hidden; /* 防止內容溢出 */
}

.leaderboard-name {
    font-weight: bold;
    margin-bottom: 5px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
}

.leaderboard-time {
    font-size: 0.9em;
    color: #666;
    opacity: 0.8;
}

.leaderboard-score {
    font-size: 1.3em;
    font-weight: bold;
    color: #667eea;
    min-width: 80px;
    text-align: right;
}

.leaderboard-item.player-rank {
    border: 2px solid #667eea;
    box-shadow: 0 0 15px rgba(102, 126, 234, 0.5);
}

.leaderboard-item.player-rank .leaderboard-name {
    font-weight: bold;
    color: #667eea;
}

.leaderboard-item.rank-1 .leaderboard-name,
.leaderboard-item.rank-2 .leaderboard-name,
.leaderboard-item.rank-3 .leaderboard-name {
    color: white;
}

/* 第一名（金色漸層）的時間與分數顏色 */
.leaderboard-item.rank-1 .leaderboard-time {
    color: #8B4513;
    opacity: 1;
    font-weight: 600;
}

.leaderboard-item.rank-1 .leaderboard-score {
    color: #8B4513;
    text-shadow: 0 1px 2px rgba(255, 255, 255, 0.5);
}

/* 第二名（銀色漸層）的時間與分數顏色 */
.leaderboard-item.rank-2 .leaderboard-time {
    color: #2C2C2C;
    opacity: 1;
    font-weight: 600;
}

.leaderboard-item.rank-2 .leaderboard-score {
    color: #1A1A1A;
    text-shadow: 0 1px 2px rgba(255, 255, 255, 0.3);
}

/* 第三名（銅色漸層）的時間與分數顏色 */
.leaderboard-item.rank-3 .leaderboard-time {
    color: #FFF8DC;
    opacity: 1;
    font-weight: 600;
}

.leaderboard-item.rank-3 .leaderboard-score {
    color: #FFFFFF;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}

.loading {
    text-align: center;
    padding: 20px;
    color: #666;
}

.empty-leaderboard {
    text-align: center;
    padding: 40px 20px;
    color: #666;
}

@media (max-width: 600px) {
    .modal-content {
        padding: 20px;
        width: 95%;
    }
    
    .icon-btn {
        width: 44px;
        height: 44px;
    }
    
    .icon-btn svg {
        width: 20px;
        height: 20px;
    }
}

/* 載入中介面 */
.start-game-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10000;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.start-game-screen.show {
    opacity: 1;
    visibility: visible;
}

.start-game-content {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 20px;
    padding: 40px;
    text-align: center;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    max-width: 400px;
    width: 90%;
}

.start-game-content h2 {
    color: #333;
    margin-bottom: 30px;
    font-size: 2em;
}

.start-game-btn {
    background: linear-gradient(135deg, #667eea 0%, #4ECDC4 100%);
    color: white;
    border: none;
    padding: 15px 40px;
    font-size: 1.2em;
    border-radius: 10px;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    font-weight: bold;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.start-game-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}

.start-game-btn:active {
    transform: translateY(0);
}

.start-game-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
}

.audio-loading-progress {
    margin-top: 30px;
}

.progress-bar-container {
    width: 100%;
    height: 8px;
    background: #e0e0e0;
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 10px;
}

.progress-bar {
    height: 100%;
    background: linear-gradient(90deg, #667eea 0%, #4ECDC4 100%);
    width: 0%;
    transition: width 0.3s ease;
    border-radius: 4px;
}

.progress-text {
    color: #666;
    font-size: 0.9em;
    margin-top: 10px;
}

.loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #667eea 0%, #4ECDC4 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10000;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.loading-screen.show {
    opacity: 1;
    visibility: visible;
}

.loading-content {
    text-align: center;
    color: white;
}

.loading-spinner {
    width: 50px;
    height: 50px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 20px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.loading-content p {
    font-size: 1.2em;
    margin: 0;
}

/* 設定模態框樣式 */
.modal-content.settings-content {
    padding: 20px;
    display: flex;
    flex-direction: column;
    max-height: 80vh;
}

.settings-list {
    display: flex;
    flex-direction: column;
    gap: 25px;
}

.setting-item {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.setting-label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 1.1em;
    color: #333;
    cursor: pointer;
}

.setting-label span {
    flex: 1;
}

/* 切換開關樣式 */
.toggle-switch {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 26px;
    cursor: pointer;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: 0.3s;
    border-radius: 26px;
}

.toggle-slider:before {
    position: absolute;
    content: "";
    height: 20px;
    width: 20px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: 0.3s;
    border-radius: 50%;
}

.toggle-switch input:checked + .toggle-slider {
    background-color: #667eea;
}

.toggle-switch input:checked + .toggle-slider:before {
    transform: translateX(24px);
}

/* 滑棒容器 */
.slider-container {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 10px;
}

.slider-track {
    position: relative;
    width: 100%;
    height: 8px;
    background: #e0e0e0;
    border-radius: 4px;
    cursor: pointer;
    touch-action: none;
}

.slider-thumb {
    position: absolute;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 24px;
    height: 24px;
    background: #667eea;
    border-radius: 50%;
    cursor: grab;
    box-shadow: 0 2px 6px rgba(102, 126, 234, 0.4);
    transition: box-shadow 0.2s;
}

.slider-thumb:active {
    cursor: grabbing;
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.6);
}

.slider-labels {
    display: flex;
    justify-content: space-between;
    font-size: 0.85em;
    color: #666;
    margin-top: 5px;
}

.slider-labels span {
    flex: 1;
    text-align: center;
}

.slider-labels span.active {
    color: #667eea;
    font-weight: bold;
}

.slider-value {
    text-align: center;
    font-size: 1.2em;
    font-weight: bold;
    color: #667eea;
    margin-top: 5px;
}
