* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
}
body {
  font-family: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  /* 邊緣安全帶/letterbox 一律純黑:融入手機螢幕邊框,比模擬天空草地的漸層更不搶戲。 */
  background: #000;
  color: #0f172a;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  /* iPhone Safari(非全螢幕)工具列會佔畫面:height:100% 含工具列下方 →
     盤面底部被 Safari 底列遮住(太太回報「版面卡住」)。改 dvh 只吃可視高度,
     工具列收合/展開時 visualViewport resize 會觸發 layout() 重算。 */
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 560px;
  margin: 0 auto;
}

/* ---- Install bar (PWA 安裝提示,只在瀏覽器分頁出現;裝了或按 ✕ 就消失) ---- */
#install-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: max(8px, env(safe-area-inset-top)) 12px 8px;
  background: rgba(15,23,42,.78);
  color: #fff;
  font-size: 15px;
  line-height: 1.35;
}
#install-bar[hidden] { display: none; }
#install-msg { flex: 1 1 auto; min-width: 0; }
#install-go {
  flex: 0 0 auto;
  min-height: 44px;
  padding: 0 18px;
  border: none; cursor: pointer;
  border-radius: 10px;
  font-size: 16px; font-weight: 700;
  color: #0f172a;
  background: #fbbf24;
  box-shadow: 0 3px 0 rgba(0,0,0,.25);
}
#install-go:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0,0,0,.25); }
#install-go[hidden] { display: none; }
#install-close {
  flex: 0 0 auto;
  width: 44px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  border: none; cursor: pointer;
  border-radius: 50%;
  font-size: 18px;
  color: #fff;
  background: rgba(255,255,255,.14);
}

/* ---- Board (fills the app; menu / HUD / game-over are all drawn in-canvas) ---- */
.board-wrap {
  position: relative;
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  /* 邊緣安全帶:底部/左右留一小圈(+瀏海 safe-area)—— 全螢幕時盤面貼死螢幕邊,
     手指掃底排很容易誤觸系統邊緣手勢把整個 app 滑掉。邊帶由 body 天空→草地漸層填色。 */
  padding: env(safe-area-inset-top)
           calc(8px + env(safe-area-inset-right))
           calc(14px + env(safe-area-inset-bottom))
           calc(8px + env(safe-area-inset-left));
  min-height: 0;
}
/* 不加 border-radius / box-shadow:Safari 對持續重繪的 canvas layer 做圓角裁切+陰影
   合成是每幀成本(iPhone 有感);遊戲畫面本來就滿版美術,不需要框飾。 */
#board {
  display: block;
  touch-action: none;
}

/* ---- Overlaid corner buttons (translucent, over the canvas) ----
   back = top-left (aligns with in-canvas pause art); share = bottom-right
   (menu only; audio toggles moved into the in-canvas settings popup). */
.corner-btn {
  position: absolute;
  width: 44px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  border: none; cursor: pointer;
  line-height: 1;
  color: #fff;
  background: rgba(15,23,42,.32);
  border-radius: 50%;
  box-shadow: 0 3px 0 rgba(0,0,0,.18);
  z-index: 5;
}
.corner-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0,0,0,.18); }
.corner-btn[hidden] { display: none; }
.back-btn { top: calc(10px + env(safe-area-inset-top)); left: calc(14px + env(safe-area-inset-left)); font-size: 30px; font-weight: 900; }
.share-btn { bottom: calc(14px + env(safe-area-inset-bottom)); right: calc(14px + env(safe-area-inset-right)); font-size: 22px; }

/* ---- Nickname modal (Part 4 訪客身分:canvas 遊戲沒有原生輸入框 → HTML 疊層問名字) ----
   z-index 高於排行榜(#lb-modal=21):改名可從榜單裡開,輸入框要疊在榜單之上不被蓋住。 */
#nick-modal {
  position: absolute; inset: 0; z-index: 30;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background: rgba(15,23,42,.55);
}
#nick-modal[hidden] { display: none; }
.nick-card {
  width: 100%; max-width: 340px;
  display: flex; flex-direction: column; gap: 14px;
  padding: 22px 20px;
  border-radius: 20px;
  background: #fff7ea;
  box-shadow: 0 14px 40px rgba(20,40,60,.45);
}
.nick-title { font-size: 20px; font-weight: 800; color: #5a3b1c; text-align: center; }
.nick-input {
  width: 100%; min-height: 52px;
  padding: 0 16px;
  border: 3px solid #e0cfa8; border-radius: 14px;
  font-size: 20px; color: #3d2410; background: #fffdf8;
  text-align: center; outline: none;
}
.nick-input:focus { border-color: #f5b642; }
.nick-err { min-height: 18px; font-size: 14px; color: #d1533c; text-align: center; }
.nick-actions { display: flex; gap: 12px; }
.nick-actions button {
  flex: 1 1 0; min-height: 50px;
  border: none; cursor: pointer;
  border-radius: 14px;
  font-size: 17px; font-weight: 800;
}
.nick-skip { color: #6b5a44; background: #ece0c9; }
.nick-ok { color: #fff; background: #6fb93c; box-shadow: 0 3px 0 rgba(0,0,0,.2); }
.nick-ok:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0,0,0,.2); }

/* ---- Profile panel (本地身分:暱稱 + 你的 ID;可複製、可換機貼碼還原;登入日後接這下面) ----
   z-index 25 = 疊在排行榜(21)之上,但在暱稱彈窗(30)之下 → 從面板裡開改名,輸入框仍浮在最上。 */
#pf-modal {
  position: absolute; inset: 0; z-index: 25;
  display: flex; align-items: center; justify-content: center;
  padding: 22px;
  background: rgba(15,23,42,.58);
}
#pf-modal[hidden] { display: none; }
.pf-card {
  width: 100%; max-width: 360px;
  display: flex; flex-direction: column; gap: 16px;
  padding: 18px 18px 20px;
  border-radius: 20px;
  background: #fff7ea;
  box-shadow: 0 14px 40px rgba(20,40,60,.45);
}
.pf-head { display: flex; align-items: center; justify-content: space-between; }
.pf-title { font-size: 21px; font-weight: 800; color: #5a3b1c; }
.pf-close {
  width: 38px; height: 38px; border: none; cursor: pointer;
  border-radius: 50%; background: #ece0c9; color: #6b5a44;
  font-size: 20px; font-weight: 800; line-height: 1;
}
.pf-section { display: flex; flex-direction: column; gap: 8px; }
.pf-label { font-size: 15px; font-weight: 800; color: #8a6a3c; }
.pf-hint { font-size: 13px; line-height: 1.4; color: #9a8a6c; }
.pf-row { display: flex; align-items: center; gap: 10px; }
.pf-val {
  flex: 1 1 auto; min-height: 44px;
  display: flex; align-items: center; padding: 0 14px;
  border-radius: 12px; background: #fffdf8;
  font-size: 17px; font-weight: 700; color: #3d2410;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.pf-id {
  flex: 1 1 auto; min-width: 0; min-height: 44px;
  display: flex; align-items: center; padding: 0 12px;
  border-radius: 12px; background: #f4ead4;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 13px; color: #5a4326;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.pf-btn {
  flex: 0 0 auto; min-height: 44px; padding: 0 16px;
  border: none; cursor: pointer; border-radius: 12px;
  font-family: inherit; font-size: 15px; font-weight: 800;
  color: #fff; background: #f2a33c; box-shadow: 0 2px 0 rgba(0,0,0,.18);
}
.pf-btn:active { transform: translateY(2px); box-shadow: 0 0 0 rgba(0,0,0,.18); }
.pf-input {
  flex: 1 1 auto; min-width: 0; min-height: 44px;
  padding: 0 12px;
  border: 2px solid #e0cfa8; border-radius: 12px;
  font-size: 14px; color: #3d2410; background: #fffdf8; outline: none;
}
.pf-input:focus { border-color: #f5b642; }
.pf-msg { min-height: 18px; font-size: 13px; font-weight: 700; color: #c0532f; text-align: center; }

/* ---- Leaderboard modal (排行榜:Worker+D1;canvas 遊戲外掛 HTML 疊層,原生捲動) ---- */
#lb-modal {
  position: absolute; inset: 0; z-index: 21;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  background: rgba(15,23,42,.6);
}
#lb-modal[hidden] { display: none; }
.lb-card {
  width: 100%; max-width: 400px; max-height: 82vh;
  display: flex; flex-direction: column; gap: 12px;
  padding: 18px 16px 16px;
  border-radius: 20px;
  background: #fff7ea;
  box-shadow: 0 14px 40px rgba(20,40,60,.45);
}
.lb-head { display: flex; align-items: center; justify-content: space-between; }
.lb-title { font-size: 21px; font-weight: 800; color: #5a3b1c; }
.lb-close {
  width: 38px; height: 38px; border: none; cursor: pointer;
  border-radius: 50%; background: #ece0c9; color: #6b5a44;
  font-size: 20px; font-weight: 800; line-height: 1;
}
/* 「你」列:榜單內改名入口(暱稱本來只在 game-over 能設,這裡是選單側找得到的地方)。 */
.lb-you {
  display: flex; align-items: center; justify-content: center; gap: 4px;
  width: 100%; min-height: 44px; padding: 8px 14px;
  border: none; cursor: pointer;
  border-radius: 12px;
  background: #fde6c4; color: #6b4a1e;
  font-family: inherit; font-size: 15px; font-weight: 800;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.lb-you:active { transform: translateY(1px); }
.lb-tabs { display: flex; gap: 8px; }
.lb-tab {
  flex: 1 1 0; min-height: 42px; border: none; cursor: pointer;
  border-radius: 12px; font-size: 15px; font-weight: 800;
  color: #6b5a44; background: #ece0c9;
}
.lb-tab.on { color: #fff; background: #f2a33c; box-shadow: 0 2px 0 rgba(0,0,0,.18); }
.lb-list {
  flex: 1 1 auto; min-height: 120px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 4px;
  -webkit-overflow-scrolling: touch;
}
.lb-row {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: 11px; background: #fffdf8;
}
.lb-row.me { background: #fff0c4; box-shadow: inset 0 0 0 2px #f2a33c; }
.lb-rank { flex: 0 0 40px; font-size: 16px; font-weight: 800; color: #b07d2c; text-align: right; }
.lb-name { flex: 1 1 auto; font-size: 16px; color: #3d2410; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-score { flex: 0 0 auto; font-size: 16px; font-weight: 800; color: #4a2c14; }
.lb-empty { padding: 30px 12px; text-align: center; font-size: 15px; color: #9a8a6c; }
.lb-me {
  flex: 0 0 auto; padding: 9px 12px; border-radius: 11px;
  background: #fff0c4; box-shadow: inset 0 0 0 2px #f2a33c;
  display: flex; align-items: center; gap: 10px;
}
