/* 重置與基本設定 */
:root {
  /* 預設暗色主題 (高對比) */
  --bg-color: #000000;
  --text-color: #ffffff;
  --panel-bg: #1a1a24;
  --panel-border: #4a4a65;
  --panel-border-inner: #2b2b3d;
  --btn-bg: #2c3e50;
  --btn-hover: #34495e;
  --btn-border: #1a252f;
  --title-color: #f1c40f;
  --subtitle-color: #ecf0f1;
  --input-bg: #111118;
  --canvas-wall: #1a1a24;
  --canvas-wall-highlight: #2b2b3d;
  --canvas-floor: #0a0a0f;
  --canvas-floor-pattern: #0f0f14;

  /* 搖桿與技能鈕比例變數 (桌面) */
  --joy-size: min(140px, 20vw);
  --joy-bottom: 15vh;
  --btn-size: calc(var(--joy-size) * 0.43);
  --btn-font: calc(var(--btn-size) * 0.33);
  --btn-label: calc(var(--btn-size) * 0.17);
  --btn-right: 5vw;
}

[data-theme="light"] {
  /* 亮色主題 (高對比：木質咖啡配色) */
  --bg-color: #fdfbf7;
  --text-color: #3e2723;
  --panel-bg: #f5f0e6;
  --panel-border: #795548;
  --panel-border-inner: #d7ccc8;
  --btn-bg: #efebe4;
  --btn-hover: #e0d6c8;
  --btn-border: #6d4c41;
  --title-color: #d84315;
  --subtitle-color: #4e342e;
  --input-bg: #ffffff;
  --canvas-wall: #795548;
  --canvas-wall-highlight: #a1887f;
  --canvas-floor: #f5f0e6;
  --canvas-floor-pattern: #ebe3d5;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background-color: var(--bg-color);
  color: var(--text-color);
  font-family: 'DotGothic16', sans-serif;
  overflow: hidden;
  user-select: none;
  position: fixed; /* 防止手機版滾動與彈跳 */
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  touch-action: none; /* 禁止所有觸控預設行為（放大、捲動） */
}

/* 遊戲容器：使用 JS 動態計算的實際可視區域高度 */
#game-container {
  position: relative;
  width: 100vw;
  height: calc(var(--real-vh, 1vh) * 100);
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

/* 遊戲畫布 */
#game-canvas {
  /* Canvas 預設樣式，實際尺寸由 JS 動態設定 */
  display: block;
  image-rendering: pixelated; /* 確保像素不模糊 */
}

/* UI 共用類別 */
.hidden {
  display: none !important;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10;
  overflow-y: auto; /* 內容太長時可滾動 */
}

/* 面板 (選單底板) */
.panel {
  position: relative;
  background-color: var(--panel-bg);
  border: 4px solid var(--panel-border);
  border-radius: 8px;
  padding: 2rem;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.8), inset 0 0 10px rgba(0, 0, 0, 0.1);
  min-width: 300px;
  image-rendering: pixelated;
  /* 簡單像素邊框效果 */
  box-shadow: 
    4px 0 0 var(--panel-border), -4px 0 0 var(--panel-border), 0 -4px 0 var(--panel-border), 0 4px 0 var(--panel-border),
    inset 4px 0 0 var(--panel-border-inner), inset -4px 0 0 var(--bg-color), inset 0 -4px 0 var(--bg-color), inset 0 4px 0 var(--panel-border-inner);
  border: none;
}

.panel-large {
  min-width: 500px;
}

/* 標題字型 (Press Start 2P) */
.title {
  font-family: 'Press Start 2P', cursive;
  font-size: 2rem;
  color: var(--title-color);
  text-shadow: 2px 2px 0 #e67e22, 4px 4px 0 #000;
  margin-bottom: 2rem;
  line-height: 1.5;
}

.subtitle {
  font-family: 'Press Start 2P', cursive;
  font-size: 1.2rem;
  color: var(--subtitle-color);
  text-shadow: 2px 2px 0 rgba(0,0,0,0.5);
  margin-bottom: 1.5rem;
}

/* 按鈕 */
.btn {
  display: block;
  width: 100%;
  padding: 1rem;
  margin-bottom: 1rem;
  background-color: var(--btn-bg);
  color: var(--text-color);
  border: 2px solid var(--btn-border);
  font-family: 'DotGothic16', sans-serif;
  font-size: 1.5rem;
  cursor: pointer;
  transition: all 0.1s;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.3);
}

.btn:hover {
  background-color: var(--btn-hover);
  transform: translateY(-2px);
  box-shadow: inset 0 -6px 0 rgba(0, 0, 0, 0.3);
}

.btn:active {
  transform: translateY(2px);
  box-shadow: inset 0 0px 0 rgba(0, 0, 0, 0.3);
}

.btn-danger {
  background-color: #c0392b;
  border-color: #922b21;
}

.btn-danger:hover {
  background-color: #d64736;
}

.btn-back {
  position: absolute;
  top: 15px;
  left: 15px;
  font-size: 1.5rem;
  background: transparent;
  border: none;
  color: var(--title-color);
  cursor: pointer;
  z-index: 10;
  padding: 5px;
  transition: transform 0.1s;
}
.btn-back:hover {
  transform: scale(1.1);
}

.btn-small {
  display: inline-block;
  padding: 0.5rem 1rem;
  background-color: #34495e;
  color: #fff;
  border: none;
  font-family: 'DotGothic16', sans-serif;
  font-size: 1.2rem;
  cursor: pointer;
}

/* HUD 抬頭顯示器 */
#hud {
  position: absolute;
  top: 20px;
  left: 20px;
  right: 20px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  z-index: 5;
  pointer-events: none; /* 讓點擊穿透到 Canvas，除非點到按鈕 */
}

.hud-item {
  background-color: rgba(0, 0, 0, 0.7);
  padding: 10px 20px;
  border: 2px solid var(--panel-border);
  border-radius: 4px;
  font-size: 1.5rem;
  color: #fff;
  font-family: 'DotGothic16', sans-serif;
}

#btn-pause {
  pointer-events: auto; /* 恢復按鈕的點擊 */
}

/* HUD 技能圖示計數（icon + dot 格子）*/
.hud-icons {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.hud-icon-row {
  display: flex;
  align-items: center;
  gap: 5px;
  background-color: rgba(0, 0, 0, 0.7);
  padding: 6px 12px;
  border: 2px solid var(--panel-border);
  border-radius: 4px;
}

.hud-icon-label {
  font-size: 1.4rem;
  min-width: 1.6rem;
}

.hud-dot-track {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  max-width: 160px;
}

.hud-dot {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  border: 2px solid rgba(255,255,255,0.4);
  background: rgba(255,255,255,0.15);
  transition: background 0.2s;
}

.hud-dot.filled-drill { background: #e74c3c; border-color: #ff7675; }
.hud-dot.filled-hint  { background: #f1c40f; border-color: #ffeaa7; }

/* 無限符號顯示 */
.hud-infinity {
  font-size: 1.6rem;
  color: #f1c40f;
  line-height: 1;
}

@media (max-width: 768px) {
  .hud-icon-label { font-size: 1.2rem; }
  .hud-dot { width: 12px; height: 12px; }
  .hud-dot-track { max-width: 120px; }
}

/* GM HUD */
#gm-hud {
  position: absolute;
  top: 20px;
  right: 20px;
  background-color: rgba(0, 0, 0, 0.85);
  border: 2px solid #e74c3c;
  border-radius: 4px;
  padding: 15px;
  color: #e74c3c;
  font-family: 'DotGothic16', sans-serif;
  z-index: 100;
  pointer-events: none;
}

#gm-hud h3 {
  font-family: 'Press Start 2P', cursive;
  font-size: 0.8rem;
  margin-bottom: 15px;
  color: #f1c40f;
  text-shadow: 1px 1px 0 #000;
}

#gm-hud p {
  margin-bottom: 8px;
  font-size: 1.2rem;
  color: #fff;
}

#gm-minimap {
  margin-top: 15px;
  border: 2px solid #555;
  background: #111;
  display: block;
}

/* 結算文字標記 */
.highlight {
  color: #f1c40f;
  font-weight: bold;
  font-size: 1.2em;
}

.stats-box {
  background: rgba(0,0,0,0.5);
  padding: 15px;
  margin-bottom: 20px;
  border-radius: 4px;
  font-size: 1.2rem;
  line-height: 1.8;
}

/* 排行榜表格 */
#leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1.5rem;
  color: #ecf0f1;
}

#leaderboard-table th, #leaderboard-table td {
  padding: 0.8rem;
  text-align: left;
  border-bottom: 1px solid #5a5a75;
}

#leaderboard-table th {
  font-family: 'Press Start 2P', cursive;
  font-size: 0.8rem;
  color: #bdc3c7;
}

.leaderboard-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  font-size: 1.5rem;
}

.lb-tabs {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.lb-tab {
  padding: 0.5rem 1.2rem;
  background-color: var(--btn-bg);
  color: var(--text-color);
  border: 2px solid var(--btn-border);
  font-family: 'DotGothic16', sans-serif;
  font-size: 1.1rem;
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.1s;
}

.lb-tab.active {
  background-color: var(--title-color);
  color: #000;
  border-color: var(--title-color);
}

.lb-tab:hover {
  opacity: 0.85;
}

/* 輸入框 */
.input-text, .input-select {
  width: 100%;
  padding: 0.8rem;
  margin: 1rem 0 1.5rem;
  background-color: var(--input-bg);
  color: var(--text-color);
  border: 2px solid var(--panel-border);
  font-family: 'DotGothic16', sans-serif;
  font-size: 1.5rem;
  text-align: center;
}

.input-text:focus, .input-select:focus {
  outline: none;
  border-color: var(--title-color);
}

/* 設定群組 */
.setting-group {
  margin-bottom: 1.5rem;
  text-align: left;
}

.setting-group label {
  display: block;
  font-size: 1.2rem;
  margin-bottom: 0.5rem;
  color: var(--text-color);
}

.setting-group select {
  margin: 0;
}

/* =========================================
   手機虛擬搖框與技能鈕
   所有尺寸均由 --joy-size 衍生，依比例縮放
   ========================================= */
#mobile-controls {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 20;
}

@media (max-width: 768px) and (orientation: portrait) {
  #mobile-controls.active {
    display: block;
  }
}

/* 類比搖桿 — 下半部置中 */
#joystick-zone {
  position: absolute;
  left: 50%;
  top: 80%;
  transform: translate(-50%, -50%);
  width: 140px;
  height: 140px;
  pointer-events: auto;
  touch-action: none;
}

#joystick-base {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255,255,255,0.10) 0%, rgba(255,255,255,0.03) 100%);
  border: 2px solid rgba(255,255,255,0.30);
}

#joystick-knob {
  width: 40%;
  height: 40%;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, rgba(255,255,255,0.65), rgba(180,180,180,0.45));
  border: 2px solid rgba(255,255,255,0.55);
  box-shadow: 0 2px 8px rgba(0,0,0,0.4);
  position: absolute;
  top: 30%;
  left: 30%;
  transition: none;
}

/* 功能鍵：絕對定位，時鐘方位 */
/* 
  搖桿中心在螢幕中央，距底部 40 + 70 = 110px。
  我們用 calc(50% ± offset) 搭配 bottom 來定位。
  2點鐘 = 右上 (drill)
  10點鐘 = 左上 (hint)
  4點鐘 = 右下 (mark)
  8點鐘 = 左下 (settings)

  搖桿中心 x = 50vw，y = bottom 110px
  各鍵偏移距離 r≈105px（從搖桿中心到按鍵中心）
  2點鐘：sin(60°)=0.866, cos(60°)=0.5  → (+91px, -53px from center)
  10點鐘：sin(-60°), cos(-60°)        → (-91px, -53px)
  4點鐘：sin(120°), cos(120°)         → (+91px, +53px)
  8點鐘：sin(-120°), cos(-120°)       → (-91px, +53px)
  以 bottom 計算：joystick中心 = bottom: 110px
  2點:  right = 50% - 91px - 30px = calc(50% - 121px); bottom = 110 + 53 - 30 = 133px
*/
.action-btn {
  position: absolute;
  right: var(--btn-right);
  width: var(--btn-size);
  height: var(--btn-size);
  background-color: rgba(52, 73, 94, 0.85);
  border: 2px solid rgba(255,255,255,0.6);
  border-radius: 50%;
  color: #fff;
  font-size: var(--btn-font);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  user-select: none;
  touch-action: manipulation;
  pointer-events: auto;
}
.action-btn span { font-size: var(--btn-label); margin-top: 2px; }
.action-btn:active { background-color: rgba(46, 204, 113, 0.85); transform: scale(0.92); }

/* 2點鐘：鑽牆 (+91px右, +53px上) → left = 50% + 91 - 30 = 50% + 61px，bottom = 110 + 53 - 30 = 133px */
.action-btn.drill {
  left: calc(50% + 61px);
  bottom: 133px;
  background-color: rgba(192, 57, 43, 0.85);
}
.action-btn.drill:active { background-color: rgba(231, 76, 60, 0.85); }

/* 10點鐘：提示 (-91px左, +53px上) → left = 50% - 91 - 30 = 50% - 121px，bottom = 133px */
.action-btn.hint {
  left: calc(50% - 121px);
  bottom: 133px;
  background-color: rgba(39, 174, 96, 0.85);
}

/* 4點鐘：記號 (+91px右, -53px下) → left = 50% + 61px，bottom = 110 - 53 - 30 = 27px */
.action-btn.mark {
  left: calc(50% + 61px);
  bottom: 27px;
  background-color: rgba(41, 128, 185, 0.85);
}

/* 8點鐘：設定 (-91px左, -53px下) → left = 50% - 121px，bottom = 27px */
.action-btn.settings {
  left: calc(50% - 121px);
  bottom: 27px;
  background-color: rgba(100, 100, 100, 0.85);
}

/* 按鈕高亮提示動畫 */
@keyframes btn-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(241, 196, 15, 0.7); }
  50% { box-shadow: 0 0 calc(var(--joy-size) * 0.1) calc(var(--joy-size) * 0.05) rgba(241, 196, 15, 0.9); }
}
.action-btn.highlight {
  animation: btn-pulse 0.8s ease-in-out 3;
}

/* 技能提示氣泡 */
.skill-tooltip {
  position: absolute;
  right: calc(var(--btn-right) + var(--btn-size) + var(--joy-size) * 0.08);
  padding: calc(var(--joy-size) * 0.06) calc(var(--joy-size) * 0.1);
  background: rgba(0, 0, 0, 0.85);
  color: #fff;
  font-size: calc(var(--joy-size) * 0.1);
  border-radius: calc(var(--joy-size) * 0.05);
  white-space: nowrap;
  pointer-events: none;
  z-index: 25;
  opacity: 0;
  transform: translateX(10px);
  transition: opacity 0.3s, transform 0.3s;
}
.skill-tooltip.visible {
  opacity: 1;
  transform: translateX(0);
}
/* 右側箭頭 */
.skill-tooltip::after {
  content: '';
  position: absolute;
  right: calc(var(--joy-size) * -0.06);
  top: 50%;
  transform: translateY(-50%);
  border: calc(var(--joy-size) * 0.03) solid transparent;
  border-left-color: rgba(0, 0, 0, 0.85);
}

/* 搖桿提示 (顯示在搖桿上方) */
.joystick-tooltip {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--joy-bottom) + var(--joy-size) + var(--joy-size) * 0.1);
  padding: calc(var(--joy-size) * 0.06) calc(var(--joy-size) * 0.1);
  background: rgba(0, 0, 0, 0.85);
  color: #fff;
  font-size: calc(var(--joy-size) * 0.1);
  border-radius: calc(var(--joy-size) * 0.05);
  white-space: nowrap;
  pointer-events: none;
  z-index: 25;
  opacity: 0;
  transition: opacity 0.3s;
}
.joystick-tooltip.visible { opacity: 1; }
/* 下箭頭 */
.joystick-tooltip::after {
  content: '';
  position: absolute;
  bottom: calc(var(--joy-size) * -0.06);
  left: 50%;
  transform: translateX(-50%);
  border: calc(var(--joy-size) * 0.03) solid transparent;
  border-top-color: rgba(0, 0, 0, 0.85);
}

/* =========================================
   RWD 響應式網頁設計 (手機/小螢幕適配)
   ========================================= */
@media (max-width: 768px) {
  /* 面板與字體縮放 */
  .panel {
    min-width: 80vw;
    padding: 1rem;
    max-height: 90vh;
    overflow-y: auto;
  }
  
  .panel-large {
    min-width: 90vw;
  }
  
  .title {
    font-size: 1.5rem;
    margin-bottom: 1rem;
  }
  
  .subtitle {
    font-size: 1rem;
    margin-bottom: 1rem;
  }
  
  .btn {
    font-size: 1.2rem;
    padding: 0.8rem;
    margin-bottom: 0.5rem;
  }
  
  /* 表格/統計字體 */
  .stats-box {
    font-size: 0.9rem;
    padding: 10px;
  }
  
  th, td {
    padding: 6px;
  }
  
  /* HUD 調整 */
  #hud {
    padding: 5px 10px;
    font-size: 0.9rem;
  }
  
  .hud-item {
    margin-right: 10px;
  }
  
  .btn-small {
    font-size: 0.8rem;
    padding: 4px 8px;
  }

  /* 操作說明文字太長時稍微縮小 */
  #menu-help .stats-box p {
    font-size: 0.8rem;
    margin-bottom: 8px;
  }

  /* === 手機底部控制區：左搖桿 + 右按鈕橫排 ===
     底部控制區高度固定為 120px
     搖桿：左側 120×120px
     按鈕：右側橫列 64px 圓鐲，中間间距 10px  */

  /* 搖桿區：下半部置中，適合左右慣用手且不擋畫面 */
  #joystick-zone {
    position: fixed;
    left: 50%;
    top: 80%;
    transform: translate(-50%, -50%);
    width: 120px;
    height: 120px;
  }

  /* 按鈕共用樣式：固定下方橫排 */
  .action-btn {
    position: fixed;
    bottom: 10px;
    width: 64px;
    height: 64px;
    font-size: 24px;
    /* 左內距由 JS/CSS 個別設定，這裡設預設 */
    left: auto;
    right: auto;
  }
  .action-btn span { font-size: 9px; }

  /* 按鈕橫列：從右一內距 16px 依序排列 */
  /* drill (1個鈕)→ right:16  hint (2個鈕)→ right:90  mark (3個鈕)→ right:164 */
  .action-btn.drill    { right: 16px;  left: auto; bottom: 10px; }
  .action-btn.hint     { right: 90px;  left: auto; bottom: 10px; }
  .action-btn.mark     { right: 164px; left: auto; bottom: 10px; }
  .action-btn.settings { display: none; } /* settings button is hidden on mobile */
}

/* 教學提示元件 */
#tutorial-hint {
  position: fixed;
  top: 80px; /* 避開 HUD */
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 20px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.85);
  border: 2px solid var(--title-color);
  color: #fff;
  font-family: 'DotGothic16', sans-serif;
  font-size: 1.1rem;
  box-shadow: 0 4px 15px rgba(0,0,0,0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s ease, transform .4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  z-index: 10000;
  text-align: center;
  min-width: 250px;
}

#tutorial-hint.show {
  opacity: 0.6;
  transform: translateX(-50%) translateY(10px);
}

/* 按鈕閃爍動畫 */
.flash {
  animation: actionFlash .25s ease-in-out 4;
}

@keyframes actionFlash {
  0%, 100% { transform: scale(1); filter: brightness(1); }
  50% { transform: scale(1.15); filter: brightness(1.6); }
}
