/* 背景隨時間動態變化 */
body {
    margin: 0;
    padding: 0;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    color: white;
    text-align: center;
    overflow: hidden;
    animation: bgShift 15s infinite alternate ease-in-out;
}

@keyframes bgShift {
    0% { background-color: #2c3e50; }
    33% { background-color: #6a1b9a; }
    66% { background-color: #00695c; }
    100% { background-color: #b71c1c; }
}

/* 模式切換 */
#modeSelector {
    display: flex;
    justify-content: center;
    gap: 10px;
    padding: 10px 0 5px 0;
}
.mode-btn {
    padding: 8px 18px;
    font-size: 16px;
    font-weight: bold;
    border: 2px solid rgba(255,255,255,0.3);
    background-color: rgba(0,0,0,0.3);
    color: white;
    border-radius: 25px;
    cursor: pointer;
    transition: all 0.2s;
}
.mode-btn:hover { background-color: rgba(255,255,255,0.15); }
.mode-btn.active {
    background-color: #ffea00;
    color: #222;
    border-color: #ffea00;
    box-shadow: 0 0 15px rgba(255,234,0,0.6);
}

/* 頂部資訊區 */
#infoBoard {
    display: flex;
    justify-content: center;
    gap: 40px;
    font-size: 32px;
    font-weight: bold;
    margin-top: 5px;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}

.time-warning { color: #e74c3c; animation: pulse 0.5s infinite alternate; }

#progressContainer {
    width: 80%;
    max-width: 600px;
    height: 12px;
    background-color: rgba(0,0,0,0.5);
    margin: 15px auto 5px auto;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.5);
}

#progressBar {
    width: 100%;
    height: 100%;
    background-color: #00e676;
    transition: width 1s linear, background-color 0.3s;
}

.progress-warning { background-color: #ff1744 !important; }

@keyframes pulse {
    from { transform: scale(1); }
    to { transform: scale(1.1); }
}

#levelInfo { font-size: 16px; color: rgba(255,255,255,0.7); margin-top: 5px; }

/* 點擊遊戲互動區 */
#gameArea {
    position: relative;
    width: 100vw;
    height: 70vh;
    margin-top: 15px;
}

/* 按鈕共用樣式 */
.btn {
    position: absolute;
    padding: 15px 30px;
    font-size: 24px;
    font-weight: bold;
    cursor: pointer;
    border: none;
    color: white;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.8);
    box-shadow: 0 6px 0 rgba(0,0,0,0.4);
    transition: background-color 0.3s, transform 0.1s, box-shadow 0.1s, clip-path 0.3s, border-radius 0.3s;
    user-select: none;
    --s: 1;
    transform: scale(var(--s));
    min-width: 110px;
    min-height: 60px;
}

.btn:active {
    transform: scale(var(--s)) translateY(6px);
    box-shadow: 0 0 0 rgba(0,0,0,0.4);
}

#targetBtn { z-index: 10; }
.trap-btn { z-index: 1; }

.mine-btn {
    background-color: #ffeb3b !important;
    color: #d32f2f !important;
    text-shadow: none;
    font-weight: 900;
    z-index: 5;
    box-shadow: 0 6px 0 #f57f17;
}

/* === 各種隨機形狀 === */
.shape-square    { border-radius: 0; }
.shape-rounded   { border-radius: 20px; }
.shape-pill      { border-radius: 50px; }
.shape-circle    { border-radius: 50%; }
.shape-diamond   { clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%); border-radius: 0; }
.shape-heart     { clip-path: polygon(50% 100%, 0 35%, 15% 0, 50% 25%, 85% 0, 100% 35%); border-radius: 0; }
.shape-star      { clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%); border-radius: 0; }
.shape-triangle  { clip-path: polygon(50% 0%, 0% 100%, 100% 100%); border-radius: 0; }
.shape-hexagon   { clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%); border-radius: 0; }
.shape-arrow     { clip-path: polygon(0% 20%, 60% 20%, 60% 0%, 100% 50%, 60% 100%, 60% 80%, 0% 80%); border-radius: 0; }
.shape-blob      { border-radius: 60% 40% 30% 70% / 60% 30% 70% 40%; }

/* 結算畫面 */
#gameOverScreen {
    position: fixed;
    top: 0; left: 0; width: 100vw; height: 100vh;
    background-color: rgba(0, 0, 0, 0.9);
    display: flex; flex-direction: column; justify-content: center; align-items: center;
    z-index: 100; opacity: 0; pointer-events: none; transition: opacity 0.3s;
}

#gameOverScreen.show { opacity: 1; pointer-events: auto; }
#gameOverScreen h2 { font-size: 48px; margin-bottom: 5px; margin-top: 0; }
#gameOverScreen p { font-size: 24px; margin-bottom: 20px; }

.crit-text {
    position: absolute;
    color: #ffea00;
    font-size: 50px;
    font-weight: 900;
    text-shadow: 0 0 10px #ff3d00, 0 0 20px #ff3d00, 0 0 30px #ff3d00;
    pointer-events: none;
    z-index: 50;
    animation: floatCrit 0.8s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

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

#statsArea {
    width: 80%; max-width: 400px; max-height: 200px; overflow-y: auto;
    margin-bottom: 25px; background-color: rgba(255,255,255,0.1); border-radius: 8px; padding: 10px;
}
table { width: 100%; border-collapse: collapse; font-size: 18px; }
th, td { padding: 10px; border-bottom: 1px solid rgba(255,255,255,0.2); }
tr:first-child td { color: #00e676; font-weight: bold; }

#restartBtn { background-color: #2980b9; position: relative; }

.shake { animation: shake-animation 0.3s ease-in-out; }
@keyframes shake-animation {
    0%, 100% { transform: translate(0, 0); }
    20%, 60% { transform: translate(-3px, -3px); }
    40%, 80% { transform: translate(3px, 3px); }
}

.shake-hard { animation: shake-hard-anim 0.4s cubic-bezier(.36,.07,.19,.97); }
@keyframes shake-hard-anim {
    0%, 100% { transform: translate(0, 0) scale(1); }
    10%, 30%, 50%, 70%, 90% { transform: translate(-10px, -15px) scale(1.02); }
    20%, 40%, 60%, 80% { transform: translate(10px, 15px) scale(0.98); }
}

.particle {
    position: absolute; pointer-events: none; font-size: 30px;
    animation: fly 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94) forwards; z-index: 20;
}
@keyframes fly {
    0% { opacity: 1; transform: translate(0, 0) scale(1); }
    100% { opacity: 0; transform: translate(var(--tx), var(--ty)) scale(1.5) rotate(360deg); }
}
.flash-red { background-color: #ff1744 !important; }

/* === 貪食蛇樣式 === */
#snakeContainer {
    display: none;
    flex-direction: column;
    align-items: center;
    margin-top: 10px;
}
#snakeInfo {
    display: flex;
    gap: 30px;
    font-size: 24px;
    font-weight: bold;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
#livesDisplay { letter-spacing: 4px; transition: transform 0.2s; }
#livesDisplay.lose { animation: lifeLoss 0.5s ease-out; }
@keyframes lifeLoss {
    0% { transform: scale(1); }
    30% { transform: scale(1.5); filter: drop-shadow(0 0 10px #ff1744); }
    100% { transform: scale(1); }
}
#snakeOptions {
    margin-bottom: 8px;
    font-size: 14px;
    color: rgba(255,255,255,0.9);
    user-select: none;
}
#snakeOptions input { vertical-align: middle; margin-right: 5px; }
#snakeCanvasWrap {
    position: relative;
    display: inline-block;
}
#snakeCanvas {
    background-color: #0d0d1f;
    border: 4px solid rgba(255,255,255,0.3);
    border-radius: 10px;
    box-shadow: 0 0 30px rgba(0,0,0,0.5);
    max-width: 90vw;
    max-height: 60vh;
    touch-action: none;
    transition: box-shadow 0.15s;
}
#snakeCanvas.glow-normal { box-shadow: 0 0 40px #00e676, 0 0 80px rgba(0,230,118,0.4); }
#snakeCanvas.glow-gold   { box-shadow: 0 0 50px #ffd600, 0 0 100px rgba(255,214,0,0.6); }
#snakeCanvas.glow-diamond{ box-shadow: 0 0 60px #00e5ff, 0 0 120px rgba(0,229,255,0.7); }
#snakeCanvas.glow-bad    { box-shadow: 0 0 60px #ff1744, 0 0 120px rgba(255,23,68,0.7); }

#comboDisplay {
    position: absolute;
    top: 10px; right: 10px;
    font-size: 28px;
    font-weight: 900;
    color: #ffea00;
    text-shadow: 0 0 10px #ff3d00, 0 0 20px #ff3d00;
    opacity: 0;
    transition: opacity 0.2s, transform 0.2s;
    pointer-events: none;
}
#comboDisplay.show { opacity: 1; transform: scale(1.2); }
#snakeControls {
    margin-top: 12px;
    font-size: 14px;
    color: rgba(255,255,255,0.8);
}
#snakeStartBtn {
    margin-top: 10px;
    padding: 10px 25px;
    font-size: 18px;
    font-weight: bold;
    background-color: #00c853;
    color: white;
    border: none;
    border-radius: 25px;
    cursor: pointer;
    box-shadow: 0 4px 0 #007a33;
    transition: transform 0.1s;
}
#snakeStartBtn:active { transform: translateY(4px); box-shadow: 0 0 0 #007a33; }

/* 開始 / 復活 按鈕列 */
#snakeBtnRow {
    margin-top: 10px;
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
}
#snakeBtnRow #snakeStartBtn { margin-top: 0; }

/* 復活按鈕：預設隱藏，死亡時 .show 才出現 */
#snakeReviveBtn {
    display: none;
    padding: 10px 22px;
    font-size: 18px;
    font-weight: bold;
    background: linear-gradient(135deg, #ff4081, #f50057);
    color: white;
    border: none;
    border-radius: 25px;
    cursor: pointer;
    box-shadow: 0 4px 0 #a30040;
    animation: revivePulse 0.9s infinite alternate ease-in-out;
}
#snakeReviveBtn.show { display: inline-block; }
#snakeReviveBtn:active { transform: translateY(4px); box-shadow: 0 0 0 #a30040; }

@keyframes revivePulse {
    from { box-shadow: 0 4px 0 #a30040, 0 0 8px rgba(245,0,87,0.4); }
    to   { box-shadow: 0 4px 0 #a30040, 0 0 22px rgba(245,0,87,0.9); }
}

/* 3D 復活按鈕：螢幕置中浮層，桌機/手機皆可用 */
#s3dReviveBtn {
    display: none;
    position: fixed;
    left: 50%; top: 50%;
    transform: translate(-50%, -50%);
    padding: 16px 34px;
    font-size: 22px;
    font-weight: bold;
    background: linear-gradient(135deg, #ff4081, #f50057);
    color: white;
    border: none;
    border-radius: 30px;
    cursor: pointer;
    box-shadow: 0 6px 0 #a30040, 0 0 30px rgba(245,0,87,0.6);
    z-index: 66;
    white-space: nowrap;
    animation: revivePulse3d 0.9s infinite alternate ease-in-out;
}
#s3dReviveBtn.show { display: block; }
#s3dReviveBtn:active { transform: translate(-50%, -50%) translateY(4px); }

@keyframes revivePulse3d {
    from { box-shadow: 0 6px 0 #a30040, 0 0 20px rgba(245,0,87,0.5); }
    to   { box-shadow: 0 6px 0 #a30040, 0 0 40px rgba(245,0,87,1); }
}

#dpad {
    margin-top: 15px;
    display: grid;
    grid-template-columns: repeat(3, 60px);
    grid-template-rows: repeat(3, 60px);
    gap: 5px;
}
.dpad-btn {
    background-color: rgba(255,255,255,0.15);
    border: 2px solid rgba(255,255,255,0.3);
    border-radius: 10px;
    color: white;
    font-size: 24px;
    font-weight: bold;
    cursor: pointer;
    user-select: none;
}
.dpad-btn:active { background-color: rgba(255,234,0,0.4); }
#dpadUp    { grid-column: 2; grid-row: 1; }
#dpadLeft  { grid-column: 1; grid-row: 2; }
#dpadRight { grid-column: 3; grid-row: 2; }
#dpadDown  { grid-column: 2; grid-row: 3; }

/* ===== 3D 貪食蛇 ===== */
#snake3dContainer {
    display: none;
    position: fixed;
    top: 0; left: 0;
    width: 100vw;
    height: 100vh;
    background: radial-gradient(1200px 800px at 50% 25%, #0f172a 0%, #0b0f14 45%, #05060a 100%);
    z-index: 50;
}
#snake3dCanvas { display: block; width: 100vw; height: 100vh; }
#snake3dHud {
    position: fixed; left: 12px; top: 60px;
    padding: 10px 14px; border-radius: 12px;
    background: rgba(10,12,18,.55);
    border: 1px solid rgba(255,255,255,.10);
    backdrop-filter: blur(10px);
    color: rgba(235,245,255,.92);
    font-size: 14px;
    user-select: none;
    z-index: 60;
}
#snake3dHud .row3d { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; margin-bottom: 6px; }
#snake3dHud .sep3d { opacity: .5; }
#snake3dHud .hint3d { opacity: .85; font-size: 12px; }
.kbd3d {
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 11px;
    padding: 2px 6px;
    border: 1px solid rgba(255,255,255,0.16);
    border-bottom-width: 2px;
    border-radius: 6px;
    background: rgba(15,23,42,.55);
    color: rgba(255,255,255,0.92);
}
#s3d_status {
    position: fixed;
    left: 50%; bottom: calc(20px + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    padding: 8px 18px;
    border-radius: 25px;
    background: rgba(10,12,18,.6);
    border: 1px solid rgba(255,255,255,.15);
    color: #ffea00;
    font-size: 16px;
    font-weight: bold;
    z-index: 60;
    max-width: 80vw;
    transition: opacity 0.3s;
}
/* 3D 模式時把上方按鈕拉高 z-index（整列置中、滿版寬以正常換行） */
body.mode-snake3d #modeSelector { position: fixed; top: 0; left: 0; right: 0; transform: none; z-index: 70; }
body.mode-snake3d { animation: none; }

/* ===================================================================== */
/* ============== 手機 / 觸控優化 (Mobile & Touch) ===================== */
/* ===================================================================== */

/* 移除點擊延遲與點擊高亮，避免快速連點時誤觸縮放 */
* { -webkit-tap-highlight-color: transparent; }
button { touch-action: manipulation; }
html, body { overscroll-behavior: none; }

/* 安全區域：避開瀏海 / 底部手勢條 */
#modeSelector { padding-top: max(10px, env(safe-area-inset-top)); }

/* desktop-hint / touch-hint 預設：桌機顯示鍵盤提示、隱藏觸控提示 */
.touch-hint { display: none; }
.desktop-hint { display: inline; }

/* ===== 3D 觸控控制盤（預設隱藏，僅觸控裝置顯示）===== */
#snake3dTouch { display: none; }
#s3dPad {
    position: fixed;
    left: max(12px, env(safe-area-inset-left));
    bottom: calc(16px + env(safe-area-inset-bottom));
    display: grid;
    grid-template-columns: repeat(3, 54px);
    grid-template-rows: repeat(3, 54px);
    gap: 6px;
    z-index: 65;
}
.s3d-pad-btn {
    background: rgba(10,12,18,.5);
    border: 1px solid rgba(255,255,255,.18);
    border-radius: 12px;
    color: rgba(235,245,255,.92);
    font-size: 22px;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}
.s3d-pad-btn:active { background: rgba(103,215,255,.4); }
#s3dPad [data-dir="up"]    { grid-column: 2; grid-row: 1; }
#s3dPad [data-dir="left"]  { grid-column: 1; grid-row: 2; }
#s3dPad [data-dir="right"] { grid-column: 3; grid-row: 2; }
#s3dPad [data-dir="down"]  { grid-column: 2; grid-row: 3; }
#s3dActions {
    position: fixed;
    right: max(12px, env(safe-area-inset-right));
    bottom: calc(16px + env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    gap: 10px;
    z-index: 65;
}
.s3d-act-btn {
    width: 56px; height: 56px;
    border-radius: 50%;
    background: rgba(10,12,18,.5);
    border: 1px solid rgba(255,255,255,.18);
    color: rgba(235,245,255,.95);
    font-size: 22px;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}
.s3d-act-btn:active { background: rgba(255,234,0,.35); }

/* 觸控裝置：顯示觸控提示與控制盤，隱藏鍵盤提示
   觸控判定用 JS 加上的 body.is-touch，並以 media query 作為後備 */
body.is-touch .touch-hint { display: inline; }
body.is-touch .desktop-hint { display: none; }
body.is-touch.mode-snake3d #snake3dTouch { display: block; }

@media (hover: none) and (pointer: coarse) {
    .touch-hint { display: inline; }
    .desktop-hint { display: none; }
    body.mode-snake3d #snake3dTouch { display: block; }
}

/* ===== 小螢幕（手機）版面調整 ===== */
@media (max-width: 600px) {
    /* 模式切換鈕：換行、縮小 */
    #modeSelector { flex-wrap: wrap; gap: 6px; padding-left: 6px; padding-right: 6px; }
    .mode-btn { padding: 8px 12px; font-size: 14px; }

    /* 點擊遊戲資訊列 */
    #infoBoard { font-size: 22px; gap: 20px; }
    #progressContainer { width: 92%; }
    #levelInfo { font-size: 13px; }

    /* 點擊遊戲按鈕縮小，確保不超出畫面 */
    #gameArea { height: 72vh; margin-top: 10px; }
    .btn { font-size: 18px; padding: 12px 18px; min-width: 90px; min-height: 52px; }

    /* 貪食蛇 */
    #snakeInfo { font-size: 18px; gap: 16px; }
    #snakeOptions { font-size: 12px; }
    #snakeControls { font-size: 12px; }
    #dpad { grid-template-columns: repeat(3, 64px); grid-template-rows: repeat(3, 64px); }
    .dpad-btn { font-size: 26px; }

    /* 結算畫面 */
    #gameOverScreen h2 { font-size: 32px; }
    #gameOverScreen p { font-size: 18px; }

    /* 3D HUD 縮小並下移，避開上方換行的模式列 */
    #snake3dHud { font-size: 12px; left: 8px; right: 8px; top: 100px; padding: 8px 10px; }
    #snake3dHud .hint3d { font-size: 11px; }
    /* 狀態列上移到方向盤上方，避免被控制盤蓋住 */
    #s3d_status { bottom: calc(200px + env(safe-area-inset-bottom)); font-size: 14px; }
}

/* 用動態視窗高度修正行動瀏覽器網址列造成的高度誤差 */
@supports (height: 100dvh) {
    #snake3dContainer, #snake3dCanvas { height: 100dvh; }
}
