/* 夜市貓：台灣陷阱大冒險 — 版面
 * 目標：桌機看起來像街機台，手機單手可玩，畫面永遠等比不變形。 */

:root {
  --bg: #07080f;
  --panel: rgba(14, 17, 26, 0.86);
  --line: rgba(255, 255, 255, 0.14);
  --ink: #e9eef5;
  --dim: #8b93a1;
  --neon: #ffd166;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background:
    radial-gradient(1200px 600px at 50% -10%, #1b1030 0%, transparent 60%),
    radial-gradient(900px 500px at 50% 110%, #2b0f1e 0%, transparent 60%),
    var(--bg);
  color: var(--ink);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", "Heiti TC", sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  gap: 12px;
  padding: 14px;
}

/* ── 舞台 ── */
#stage {
  position: relative;
  width: min(100%, 960px);
  /* 視窗不夠高時縮小舞台，讓下面的工具列與說明也留得住（960/512 = 1.875） */
  width: min(100%, 960px, max(320px, calc((100dvh - 150px) * 1.875)));
  border-radius: 14px;
  overflow: hidden;
  background: #0b1020;
  border: 1px solid var(--line);
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, 0.6),
    0 24px 60px rgba(0, 0, 0, 0.6),
    0 0 90px rgba(255, 100, 120, 0.08);
}

#game {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 960 / 512;
  image-rendering: pixelated;      /* 像素風的靈魂 */
  image-rendering: crisp-edges;
  touch-action: none;
}

/* ── 工具列（暫停／重來／音效）──
   放在畫布外面、文件流裡：畫布上的 HUD 是固定像素座標，按鈕是 CSS 像素，
   疊在一起時只要視窗寬度一變就會壓到右上角的珍奶／計時／一百塊。
   放在外面就永遠不可能重疊，而且手機橫放時也不會擋到遊戲。 */
#toolbar {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  width: min(100%, 960px);
  width: min(100%, 960px, max(320px, calc((100dvh - 150px) * 1.875)));
}

#toolbar button {
  font: 600 13px/1 "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 11px;
  cursor: pointer;
  transition: background 0.14s, transform 0.08s;
}

#toolbar button:hover { background: rgba(40, 46, 66, 0.9); }
#toolbar button:active { transform: translateY(1px); }
#toolbar button:focus-visible { outline: 2px solid var(--neon); outline-offset: 2px; }

/* ── 觸控虛擬按鍵（只有 pointer: coarse 時由 JS 加上 .show） ── */
#touchpad {
  display: none;
  position: absolute;
  inset: auto 0 0 0;
  padding: 10px 12px 14px;
  justify-content: space-between;
  align-items: flex-end;
  pointer-events: none;
  z-index: 6;
}

#touchpad.show { display: flex; }

#touchpad .pad-left,
#touchpad .pad-right {
  display: flex;
  gap: 10px;
  pointer-events: auto;
}

#touchpad button {
  width: 66px;
  height: 66px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.28);
  background: rgba(12, 16, 26, 0.55);
  color: #fff;
  font: 700 20px/1 "Noto Sans TC", sans-serif;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#touchpad button:active { background: rgba(255, 209, 102, 0.35); }
#touchpad #btn-jump { width: 78px; height: 78px; }

[hidden] { display: none !important; }
#run-status { margin: 0; max-width: 96%; color: #9fb3c8; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#run-status:empty { display: none; }
.overlay-panel {
  position: fixed; z-index: 30; inset: 5% max(12px, calc((100% - 720px) / 2));
  overflow: auto; background: #111827; border: 2px solid var(--neon);
  border-radius: 14px; padding: 20px; box-shadow: 0 10px 80px #000;
}
.overlay-panel input { display: block; width: 100%; padding: 12px; margin: 12px 0; font: 18px sans-serif; }
.overlay-panel button { padding: 10px 14px; margin: 4px; cursor: pointer; color: var(--ink); background: #26344c; border: 1px solid #70819c; border-radius: 7px; }
.overlay-panel button:disabled { opacity: 0.4; cursor: default; }
.overlay-panel button:focus-visible { outline: 2px solid var(--neon); }
.overlay-panel table { width: 100%; border-collapse: collapse; font-size: 14px; }
.overlay-panel td, .overlay-panel th { padding: 8px 4px; border-bottom: 1px solid #394152; text-align: left; overflow-wrap: anywhere; }
#board-entry { width: min(100%, 960px); display: flex; justify-content: center; flex: 0 0 auto; padding: 4px 12px 8px; }
#btn-board {
  display: inline-flex; gap: 12px; align-items: center; justify-content: center;
  min-height: 48px; width: min(100%, 380px); padding: 7px 20px;
  border: 2px solid #fff0ad; border-radius: 10px; cursor: pointer;
  background: linear-gradient(135deg, #ffe18a, #ffbb43); color: #35220e;
  box-shadow: 0 3px 0 #956322, 0 4px 18px #ffbf3426; font-family: inherit; font-weight: 800; font-size: 17px; line-height: 1.2;
}
#btn-board img { flex-shrink: 0; image-rendering: pixelated; }
#btn-board span { font-size: 12px; font-weight: 600; opacity: 0.8; }
#btn-board:hover { background: #ffe7a5; }
#btn-board:focus-visible { outline: 3px solid #7dd3fc; outline-offset: 3px; }
body.touch-layout #board-entry { padding: 3px 12px 7px; }
body.touch-layout #btn-board { min-height: 42px; padding: 4px 16px; }
body:has(#board-entry:not([hidden])):not(.touch-layout):not(:fullscreen) #stage,
body:has(#board-entry:not([hidden])):not(.touch-layout):not(:fullscreen) #toolbar {
  width: min(100%, 960px, max(320px, calc((100dvh - 215px) * 1.875)));
}
body:fullscreen:not(.touch-layout):has(#board-entry:not([hidden])) #stage { width: min(100%, calc((100dvh - 110px) * 1.875)); }

/* 英雄榜：固定標題／分頁，只有排名內容捲動；資料文字不插入 HTML。 */
#board-panel { inset: 4% max(12px, calc((100% - 860px) / 2)); display: flex; flex-direction: column; padding: 0; overflow: hidden; border-color: #cda65a; background: #0e1625; }
.board-header { display: flex; justify-content: space-between; align-items: center; padding: 20px 24px 8px; background: linear-gradient(120deg, #293148, #171d2e); flex: 0 0 auto; }
.board-header h2 { margin: 2px 0 0; font-size: 28px; color: #ffe29a; letter-spacing: 2px; }
.board-eyebrow { margin: 0 0 6px; color: #b8c2d6; font-size: 12px; letter-spacing: 2px; }
#board-close { min-width: 40px; min-height: 40px; margin: 0; }
.board-rules { display: flex; flex-wrap: wrap; gap: 6px 14px; margin: 0; padding: 12px 24px; font-size: 12px; color: #b9c6db; border-bottom: 1px solid #344059; }
.board-rules span:first-child { color: #ffe29a; }
.board-notice { padding: 0 24px; flex: 0 0 auto; color: #9ccac4; font-size: 12px; }
#score-status { margin: 8px 0; }
#score-status:empty { display: none; }
#board-list { min-height: 0; flex: 1 1 auto; overflow-y: auto; padding: 18px 24px; overscroll-behavior: contain; scrollbar-color: #66728a #121b2d; }
.board-state { text-align: center; padding: 28px 12px; color: #b6c3d8; }
.board-state img { width: 64px; height: 72px; opacity: 0.55; image-rendering: pixelated; }
.board-state h3 { color: #f4e4bd; margin: 12px 0; }
.podium { display: flex; justify-content: center; align-items: flex-end; gap: 12px; margin: 4px 0 22px; }
.podium-card { flex: 0 1 31%; min-width: 0; padding: 14px 8px; border: 1px solid #52657e; border-radius: 12px; background: linear-gradient(#26344a, #172131); text-align: center; }
.podium-card[data-rank="1"] { order: 2; border-color: #dfb862; background: linear-gradient(#493b24, #201f29); padding-top: 22px; }
.podium-card[data-rank="2"] { order: 1; }
.podium-card[data-rank="3"] { order: 3; border-color: #9e775b; background: linear-gradient(#3b2d28, #1c202d); }
.podium-card img { width: 56px; height: 63px; image-rendering: pixelated; }
.podium-card[data-rank="1"] img { width: 68px; height: 77px; }
.podium-rank { font-size: 11px; letter-spacing: 2px; color: #c1cbdc; margin-bottom: 8px; }
.podium-card h3 { font-size: 16px; color: #f5f2e9; margin: 8px 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.podium-time { color: #ffdf94; margin: 9px 0 4px; font: 700 18px/1.2 Consolas, monospace; font-variant-numeric: tabular-nums; }
.podium-deaths { color: #a9b8cc; font-size: 12px; }
.bill-badge { display: inline-block; padding: 3px 6px; background: #263f36; color: #a6efc7; border: 1px solid #416b57; border-radius: 5px; white-space: nowrap; font-size: 11px; }
.ordinary-clear { color: #899ab2; font-size: 11px; }
#board-panel .board-table { table-layout: fixed; border-collapse: separate; border-spacing: 0 4px; }
.board-table caption { text-align: left; font-size: 12px; color: #91a2bb; margin-bottom: 8px; }
#board-panel .board-table th { font-size: 12px; color: #8fa2bf; border: 0; padding: 6px 10px; }
#board-panel .board-table td { background: #192337; border: 0; padding: 10px; font-variant-numeric: tabular-nums; }
.board-table th:first-child { width: 60px; }
.board-table th:nth-child(3) { width: 96px; }
.board-table th:nth-child(4) { width: 100px; }
.board-table th:nth-child(5) { width: 64px; }
.board-table td:first-child { border-radius: 7px 0 0 7px; color: #ffd879; }
.board-table td:last-child { border-radius: 0 7px 7px 0; }
.rank-cell { display: flex; align-items: center; gap: 4px; }
.rank-cell img { width: 22px; height: 25px; image-rendering: pixelated; }
#board-panel .rank-top-1 td { background: #322d28; }
#board-panel .rank-top-2 td { background: #253044; }
#board-panel .rank-top-3 td { background: #302831; }
.board-pagination { display: flex; justify-content: space-between; align-items: center; padding: 10px 20px; border-top: 1px solid #344059; background: #151f31; flex: 0 0 auto; font-size: 12px; color: #a8b7cd; }
@media (max-width: 520px) {
  .board-header { padding: 14px 14px 8px; }
  .board-header h2 { font-size: 23px; }
  .board-eyebrow { font-size: 10px; letter-spacing: 1px; }
  .board-rules { padding: 10px 14px; gap: 5px 8px; font-size: 10px; }
  .board-notice { padding: 0 14px; }
  #board-list { padding: 12px 10px; }
  .podium { gap: 6px; }
  .podium-card { flex-basis: 33%; padding: 10px 4px; }
  .podium-card h3 { font-size: 12px; }
  .podium-card img { width: 38px; height: 43px; }
  .podium-card[data-rank="1"] img { width: 46px; height: 52px; }
  .podium-time { font-size: 13px; }
  .podium-deaths { font-size: 10px; }
  #board-panel .board-table td, #board-panel .board-table th { padding: 8px 4px; font-size: 11px; }
  .board-table th:first-child { width: 34px; }
  .board-table th:nth-child(3) { width: 60px; }
  .board-table th:nth-child(4) { width: 66px; }
  .board-table th:nth-child(5) { width: 34px; }
  .rank-cell { flex-direction: column; gap: 1px; }
  .bill-badge { font-size: 9px; padding: 3px; }
  .board-pagination { padding: 8px; }
  .board-pagination button { padding: 9px; }
}
@media (max-height: 480px) and (orientation: landscape) {
  .board-header { padding-top: 10px; }
  .board-header h2 { font-size: 23px; }
  .board-eyebrow { display: none; }
  .board-rules { padding-top: 6px; padding-bottom: 6px; }
  .podium-card { display: grid; grid-template-columns: 42px minmax(0, 1fr); gap: 4px 8px; padding: 10px; text-align: left; }
  .podium-card[data-rank="1"] { padding-top: 10px; }
  .podium-rank { grid-column: 1 / -1; margin-bottom: 2px; }
  .podium-card img { grid-column: 1; grid-row: 2 / 5; align-self: center; }
  .podium-card h3 { grid-column: 2; margin: 2px 0; font-size: 14px; }
  .podium-card > span { grid-column: 2; justify-self: start; }
  .podium-time { grid-column: 2; margin: 3px 0; font-size: 15px; }
  .podium-deaths { grid-column: 1 / -1; text-align: right; font-size: 11px; }
  .podium-card img, .podium-card[data-rank="1"] img { width: 36px; height: 41px; }
  .board-pagination { padding-top: 4px; padding-bottom: 4px; }
}
#name-error { color: #ffb3bd; }
body.touch-layout #toolbar { flex-wrap: wrap; flex-basis: auto; min-height: 40px; }
#death-details {
  position: fixed; inset: 8% max(12px, calc((100% - 700px) / 2));
  z-index: 20; overflow: auto; padding: 20px;
  background: #111827; border: 2px solid var(--neon); border-radius: 12px;
  box-shadow: 0 12px 60px #000; max-height: 84dvh;
}
#death-details h2 { margin-top: 0; }
#death-details table { width: 100%; border-collapse: collapse; }
#death-details td, #death-details th { padding: 8px; border-bottom: 1px solid var(--line); text-align: left; }
#death-details button { float: right; padding: 10px 16px; cursor: pointer; }
#touchpad button.held { background: #725326; border-color: var(--neon); }

/* 手機以獨立控制區換取完整視野，所有尺寸包含安全區。 */
body.touch-layout, body:fullscreen {
  height: 100dvh; min-height: 0; overflow: hidden; gap: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  justify-content: center;
}
body.touch-layout #footnote, body:fullscreen #footnote { display: none; }
body.touch-layout #stage {
  width: 100%; flex: 1; min-height: 0; border: 0; border-radius: 0;
  display: grid; grid-template-rows: minmax(0, 1fr) 104px;
  box-shadow: none;
}
body.touch-layout #game { width: var(--game-width, 100%); height: var(--game-height, auto); align-self: center; justify-self: center; }
body.touch-layout #touchpad { position: static; padding: 8px 16px; align-items: center; background: #101523; }
body.touch-layout #toolbar { width: 100%; flex: 0 0 40px; align-items: center; justify-content: center; }
body.touch-layout #toolbar button { padding: 8px; }
body:fullscreen:not(.touch-layout) #stage { width: min(100%, calc((100dvh - 44px) * 1.875)); }
@media (orientation: landscape) {
  body.touch-layout #stage { grid-template-rows: minmax(0, 1fr); padding: 0 92px; }
  body.touch-layout #touchpad { position: absolute; inset: 0; padding: 8px; background: none; }
  body.touch-layout #touchpad .pad-left, body.touch-layout #touchpad .pad-right { flex-direction: column; gap: 14px; }
  body.touch-layout #touchpad button { width: 70px; height: 70px; }
}

/* ── 頁尾說明 ── */
#footnote {
  margin: 0;
  font-size: 13px;
  color: var(--dim);
  text-align: center;
  line-height: 1.7;
}

#footnote .keys {
  display: inline-block;
  padding: 3px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  font-size: 12px;
}

@media (max-width: 700px) {
  body { justify-content: flex-start; padding-top: 8px; }
  #footnote .keys { display: none; }
  #toolbar { justify-content: center; }
  #toolbar button { padding: 6px 9px; font-size: 12px; }
}

/* 尊重使用者的減少動態偏好 */
@media (prefers-reduced-motion: reduce) {
  #toolbar button { transition: none; }
}
