/* 以手機直向為主要設計：地圖在上、對白與選單在中、觸控按鈕在下。
   橫向或桌面：對白疊在地圖上，觸控按鈕放右邊。 */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }

body {
  margin: 0;
  height: 100dvh;
  background: #07091a;
  color: #fff;
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

#game {
  position: relative;
  flex: 1;
  min-height: 0;
  width: 100%;
  background: #000;
  overflow: hidden;
  container-type: size;
  --mapH: calc(100cqw * 2 / 3);
}

/* 地圖區域（直向時佔頂部，寬度 3:2） */
#map, #fade {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: var(--mapH);
}

#map { image-rendering: pixelated; }

/* 直向手機：地圖下面成片空位，用場景本身嘅顏色模糊放大做氛圍背景（橫向 3:2 剛好填滿，唔使） */
#ambient {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  filter: blur(14px) saturate(1.4) brightness(0.74);
  transform: scale(1.25);
  display: none;
}
@media (max-aspect-ratio: 4/3) {
  #ambient { display: block; }
  #game #panel { background: linear-gradient(rgba(7, 9, 26, 0.1), rgba(7, 9, 26, 0.9)); }
  #map { box-shadow: 0 6px 22px rgba(0, 0, 0, 0.55); }
}
#fade { background: #000; opacity: 0; pointer-events: none; }

#panel {
  position: absolute;
  left: 0;
  right: 0;
  top: var(--mapH);
  bottom: 0;
  padding: 8px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 14px;
  background: linear-gradient(#0b0f2a, #07091a);
}

/* 勇者鬥惡龍式視窗：深藍漸層、白邊、內框 */
.window {
  background: linear-gradient(180deg, rgba(34, 44, 110, 0.96), rgba(12, 16, 52, 0.96));
  border: 3px solid #f4f1ff;
  border-radius: 8px;
  box-shadow: 0 0 0 2px #0a0c24, inset 0 0 0 2px #3a4a9a, 0 4px 12px rgba(0, 0, 0, 0.5);
  padding: 10px 14px;
  font-size: clamp(15px, 4.6cqw, 20px);
  line-height: 1.55;
}

#dialog {
  position: relative;
  flex: none;
  min-height: 5.4em;
  cursor: pointer;
}

/* 講者名牌：貼住對白框上邊 */
#speaker {
  position: absolute;
  left: 10px;
  top: -0.95em;
  padding: 0 10px;
  font-size: 0.8em;
  line-height: 1.5;
  font-weight: bold;
  color: #2a1a00;
  background: linear-gradient(#ffe89a, #f0b83a);
  border: 2px solid #0a0c24;
  border-radius: 6px;
}
#face {
  display: none;
  position: absolute;
  left: 12px;
  top: 14px;
  width: 3.2em;
  height: 2.4em;
  image-rendering: pixelated;
  background: radial-gradient(#4a5ab8, #1a2060);
  border: 2px solid #f4f1ff;
  border-radius: 4px;
}
#dialog.has-face #face { display: block; }
#dialog.has-face #text { padding-left: 3.6em; min-height: 2.6em; }
#text { white-space: pre-wrap; word-break: break-word; text-shadow: 0 1px 0 #000; }

#next {
  position: absolute;
  right: 12px;
  bottom: 4px;
  color: #ffe27a;
  font-size: 0.8em;
  animation: blink 0.8s steps(2) infinite;
}

@keyframes blink { 50% { opacity: 0; transform: translateY(2px); } }

#menu {
  flex: 0 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.opt {
  position: relative;
  min-height: 44px;           /* 觸控目標至少 44px */
  display: flex;
  align-items: center;
  padding: 4px 6px 4px 28px;
  cursor: pointer;
  border-radius: 4px;
}

.opt.sel { background: linear-gradient(90deg, rgba(255, 226, 122, 0.22), rgba(255, 226, 122, 0)); }
.opt.sel::before { content: "▶"; position: absolute; left: 7px; color: #ffe27a; animation: nudge 0.8s ease-in-out infinite; }
.opt.disabled { color: #7a80a8; }
@keyframes nudge { 50% { transform: translateX(3px); } }

#menu.grid { display: grid; grid-template-columns: 1fr 1fr; column-gap: 6px; }
#menu.grid .opt { white-space: nowrap; padding-right: 2px; }

#status {
  position: absolute;
  left: 8px;
  top: 8px;
  padding: 5px 9px;
  font-size: clamp(11px, 3.2cqw, 15px);
  line-height: 1.3;
  min-width: 7.5em;
}
.st-name { color: #ffe27a; font-weight: bold; }
.st-row { display: grid; grid-template-columns: auto 1fr; column-gap: 4px; }
.st-label { color: #c8ccf0; }
.st-num { text-align: right; font-variant-numeric: tabular-nums; }
.st-bar { grid-column: 1 / -1; height: 4px; margin: 1px 0 3px; background: #0a0c24; border-radius: 2px; overflow: hidden; }
.st-bar i { display: block; height: 100%; transition: width 0.4s; }
.hp .st-bar i { background: linear-gradient(90deg, #22c55e, #86efac); }
.sp .st-bar i { background: linear-gradient(90deg, #eab308, #fff2a8); }
.self .st-bar i { background: linear-gradient(90deg, #7e22ce, #c084fc); }
.emo .st-bar i { background: linear-gradient(90deg, #db2777, #f9a8d4); }
.st-tag { color: #ffe27a; font-size: 0.9em; }

/* 地點牌：左上角，港鐵站牌風格（色帶＋地名） */
#hint {
  position: absolute;
  left: 6px;
  top: 6px;
  max-width: calc(100% - 12px);
  padding: 2px 8px 2px 10px;
  border-radius: 4px;
  background: rgba(10, 12, 36, 0.78);
  border-left: 4px solid #d8262a;
  font-size: clamp(11px, 3cqw, 14px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}
/* 直向：地點牌放喺地圖下面嘅空位，唔遮住頂行嘅門 */
@media (max-aspect-ratio: 4/3) {
  #hint { top: calc(var(--mapH) + 6px); z-index: 5; }
}
#hint b { font-weight: bold; color: #fff; }
#hint span { color: #aab0d8; font-size: 0.88em; }

/* 入新地方時彈出的大站牌 */
#banner {
  position: absolute;
  left: 50%;
  top: calc(var(--mapH) * 0.32);
  transform: translate(-50%, -8px);
  padding: 6px 22px;
  background: #f4f4f0;
  color: #1b1226;
  border-top: 5px solid #d8262a;
  border-radius: 3px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.55);
  font-size: clamp(16px, 5.2cqw, 26px);
  font-weight: bold;
  letter-spacing: 0.12em;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
}
#banner.show { animation: banner 1.9s ease-out forwards; }
@keyframes banner {
  0% { opacity: 0; transform: translate(-50%, -8px); }
  15%, 75% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, 0); }
}

/* 經文卡對白：金框、經文用襯線體 */
#dialog.verse {
  background: linear-gradient(180deg, rgba(60, 40, 10, 0.97), rgba(24, 16, 6, 0.97));
  border-color: #ffe27a;
  box-shadow: 0 0 0 2px #2a1a00, inset 0 0 0 2px #a07a2a, 0 0 18px rgba(255, 210, 110, 0.35);
}
#dialog.verse #text { font-family: "Noto Serif TC", "PMingLiU", serif; line-height: 1.7; }

/* 詩歌：淺藍框，好似詩歌本一頁 */
#dialog.hymn {
  background: linear-gradient(180deg, rgba(20, 50, 90, 0.97), rgba(8, 20, 44, 0.97));
  border-color: #bfe4ff;
  box-shadow: 0 0 0 2px #04101e, inset 0 0 0 2px #4a7ab0, 0 0 16px rgba(150, 210, 255, 0.3);
}
#dialog.hymn #text { font-family: "Noto Serif TC", "PMingLiU", serif; line-height: 1.7; }

#name-box {
  position: absolute;
  left: 8%;
  right: 8%;
  top: 10%;
  z-index: 5;
}

#name-input {
  width: 100%;
  margin-top: 8px;
  padding: 8px;
  font-size: 18px;            /* 16px 以上，iOS 不會自動放大 */
  background: #0a0c24;
  color: #fff;
  border: 2px solid #f4f1ff;
  border-radius: 4px;
  user-select: text;
  -webkit-user-select: text;
}

/* 觸控按鈕：左邊十字鍵，右邊決定鍵 */
#pad {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px calc(10px + env(safe-area-inset-bottom));
  background: linear-gradient(#07091a, #121634);
}

#dpad {
  display: grid;
  grid-template: repeat(3, 52px) / repeat(3, 52px);
  gap: 2px;
}

#dpad .up { grid-area: 1 / 2; }
#dpad .left { grid-area: 2 / 1; }
#dpad .right { grid-area: 2 / 3; }
#dpad .down { grid-area: 3 / 2; }

#pad button {
  font-size: 18px;
  color: #c8ccf0;
  background: linear-gradient(#3a4280, #1e2350);
  border: 2px solid #0a0c24;
  border-radius: 10px;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.18), 0 3px 0 #05061a;
  touch-action: none;
}

#ok {
  width: 88px;
  height: 88px;
  border-radius: 50% !important;
  font-size: 18px !important;
  font-weight: bold;
  color: #3a2200 !important;
  background: radial-gradient(circle at 40% 30%, #fff2b0, #f0b83a 60%, #b07a10) !important;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.2), 0 4px 0 #05061a !important;
}

#pad { -webkit-touch-callout: none; }
#dpad { touch-action: none; }
#acts { display: flex; flex-direction: column; align-items: center; gap: 10px; }

#menu-btn {
  width: 64px;
  height: 30px;
  font-size: 13px !important;
  border-radius: 15px !important;
  transition: opacity 0.2s;
}
body:not(.exploring) #menu-btn { opacity: 0.35; }

/* 面前有嘢調查：決定鍵發光 */
#ok.ready { animation: ready 1.1s ease-in-out infinite; }
@keyframes ready {
  50% { box-shadow: 0 0 0 4px rgba(255, 226, 122, 0.45), 0 0 18px rgba(255, 226, 122, 0.7), 0 4px 0 #05061a; }
}

#pad button.down-state { transform: translateY(2px); box-shadow: 0 1px 0 #05061a; filter: brightness(1.2); }
#pad button:focus-visible, #name-input:focus-visible { outline: 3px solid #ffd84a; outline-offset: 2px; }

/* 橫向／桌面：地圖維持 3:2 置中，對白疊在地圖下半 */
@media (min-aspect-ratio: 4/3) {
  body { flex-direction: row; align-items: center; justify-content: center; --padw: 0px; }
  #game {
    flex: none;
    width: min(calc(100vw - var(--padw) - 16px), calc((100dvh - 16px) * 3 / 2), 1100px);
    height: auto;
    aspect-ratio: 3 / 2;
    --mapH: 100cqh;
  }
  /* 橫向：對白在左、選單在右並排，唔會遮住戰鬥中的敵人 */
  #panel { top: auto; height: 100%; padding: 2.5cqh 2.5cqw; flex-direction: row; align-items: flex-end; }
  #dialog { flex: 1 1 0; min-width: 0; order: 1; }
  #menu { max-height: 62cqh; flex: 0 1 50%; order: 2; }
  #menu.grid .opt { padding-left: 24px; }
  #menu:has(+ #dialog.hidden) { flex-basis: 100%; }
  .window { font-size: clamp(13px, 2.5cqw, 22px); }
  #dialog { min-height: 24cqh; }
  #menu.grid .opt { min-height: 40px; }
  #banner { top: 22cqh; }
  #status { font-size: clamp(11px, 2cqw, 17px); }
  #panel { background: none; }
  #pad { flex-direction: column; gap: 16px; padding: 12px calc(12px + env(safe-area-inset-right)) 12px 12px; background: none; }
  #dpad { grid-template: repeat(3, 46px) / repeat(3, 46px); }
  #ok { width: 76px; height: 76px; }
}

/* 有滑鼠的桌面不顯示觸控按鈕 */
@media (min-aspect-ratio: 4/3) and (hover: hover) and (pointer: fine) {
  #pad { display: none; }
}

@media (min-aspect-ratio: 4/3) and (pointer: coarse) {
  body { --padw: 180px; }
}

/* 節奏出擊：游標左右掃，喺亮區按決定；金色中心＝完美 */
#timing { flex: none; cursor: pointer; padding: 8px 14px 12px; }
#tm-label { font-size: 0.85em; color: #ffe27a; margin-bottom: 8px; text-align: center; letter-spacing: 0.08em; }
#tm-label.perfect { color: #fff6c0; text-shadow: 0 0 10px #ffd84a; font-weight: bold; }
#tm-label.miss { color: #ff9a9a; }
#tm-track { position: relative; height: 22px; border-radius: 11px; background: #0a0c24; box-shadow: inset 0 0 0 2px #3a4a9a; }
#tm-zone, #tm-perfect, #tm-cur { position: absolute; top: 0; height: 100%; }
#tm-zone { background: rgba(120, 200, 255, 0.35); border-radius: 11px; }
#tm-perfect { background: #ffd84a; border-radius: 11px; box-shadow: 0 0 12px #ffd84a; }
#tm-cur { width: 6px; margin-left: -3px; top: -4px; height: 30px; background: #fff; border-radius: 3px; box-shadow: 0 0 8px #fff; }

/* 有選單時，提示句唔使佔咁高，戰鬥六個指令喺細 mon 都排得落 */
#menu:not(.hidden) + #dialog { min-height: 0; }

.hidden { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

@media (prefers-reduced-motion: reduce) {
  #next, .opt.sel::before, #ok.ready { animation: none; }
  #banner.show { animation-duration: 0.01s; animation-delay: 1.5s; opacity: 1; transform: translate(-50%, 0); }
  .st-bar i { transition: none; }
}
