/* 紅白反應遊戲：深藍灰底，純紅與純白提示，品牌色只當點綴 */
:root {
  --bg: #0f1b26;
  --bg-raised: #172a3b;
  --line: #2b4256;
  --text: #fffef5;          /* 品牌米白 */
  --muted: #a9bac8;
  --red: #ff1a1a;           /* 飽和紅 */
  --white: #ffffff;
  --ok: #2ecc71;
  --bad: #ff5a5f;
  --brand-teal: #004d40;
  --brand-orange: #ff8a00;
  --font: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", "Heiti TC", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 20px;
  line-height: 1.5;
}

body { overflow: hidden; }

button { font: inherit; cursor: pointer; }
:focus-visible { outline: 4px solid var(--brand-orange); outline-offset: 3px; }
[hidden] { display: none !important; }

/* 遊戲進行中隱藏游標（遮罩與按鈕仍可點） */
body[data-screen="countdown"],
body[data-screen="round"],
body[data-screen="flag"] { cursor: none; }
body[data-screen="countdown"] .overlay,
body[data-screen="round"] .overlay,
body[data-screen="flag"] .overlay { cursor: default; }

#app { height: 100%; display: flex; flex-direction: column; }
main { flex: 1; min-height: 0; position: relative; }

/* ---- 上方狀態列 ---- */
.topbar {
  position: fixed;
  top: 12px;
  right: 16px;
  z-index: 30;
  display: flex;
  gap: 10px;
  align-items: center;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  border-radius: 999px;
  background: var(--bg-raised);
  border: 2px solid var(--line);
  font-size: 18px;
  font-weight: 700;
}
.chip .dot { width: 14px; height: 14px; border-radius: 50%; background: #7a8794; }
.chip[data-state="connected"] { border-color: var(--ok); }
.chip[data-state="connected"] .dot { background: var(--ok); }
.chip[data-state="sim"] { border-color: var(--brand-orange); background: #3a2605; color: #ffd9a0; }
.chip[data-state="sim"] .dot { background: var(--brand-orange); }
.chip[data-state="lost"] { border-color: var(--bad); }
.chip[data-state="lost"] .dot { background: var(--bad); }

.tool-btn {
  padding: 8px 16px;
  border-radius: 10px;
  border: 2px solid var(--line);
  background: var(--bg-raised);
  color: var(--text);
  font-size: 18px;
}
.tool-btn:hover { border-color: var(--muted); }

/* ---- 畫面通用 ---- */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 64px 32px 32px;
  text-align: center;
}
.screen-scroll { justify-content: flex-start; overflow-y: auto; }
.screen-scroll > * { flex-shrink: 0; }
/* 內容夠矮時垂直置中，太高時從頂端開始、可往下捲（避免 justify-content: center 把上方內容切掉） */
.screen-safe { overflow-y: auto; justify-content: flex-start; }
.screen-safe > * { flex-shrink: 0; }
.screen-safe > :first-child { margin-top: auto; }
.screen-safe > :last-child { margin-bottom: auto; }

.heading { margin: 0; font-size: 40px; }

.btn-row { display: flex; gap: 20px; flex-wrap: wrap; justify-content: center; }

.btn {
  min-width: 260px;
  min-height: 72px;
  padding: 12px 36px;
  border-radius: 16px;
  border: 3px solid transparent;
  font-size: 30px;
  font-weight: 700;
}
.btn-primary { background: var(--brand-orange); color: #1a1000; }
.btn-primary:hover:not(:disabled) { background: #ffa033; }
.btn-secondary { background: var(--brand-teal); color: var(--text); border-color: #1d7d6d; }
.btn-secondary:hover:not(:disabled) { background: #00695a; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-link {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 20px;
  text-decoration: underline;
  padding: 8px 16px;
}

.note { margin: 0; color: var(--muted); max-width: 40em; min-height: 1.5em; }
.note.small { font-size: 16px; }

/* ---- 首頁 ---- */
.title {
  margin: 0;
  font-size: clamp(56px, 9vw, 120px);
  line-height: 1.1;
  letter-spacing: 0.04em;
  padding-bottom: 14px;
  border-bottom: 8px solid var(--brand-orange);
}
.lead { margin: 0; font-size: 28px; }

/* ---- 規則 ---- */
.pad-row { display: flex; gap: 80px; justify-content: center; }
.pad { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.pad-disc { width: 150px; height: 150px; border-radius: 50%; border: 6px solid #3a566e; }
.pad-red { background: var(--red); }
.pad-white { background: var(--white); }
.pad-name { font-size: 32px; font-weight: 700; }
.pad-where { color: var(--muted); }
.pad-key {
  margin-top: 4px;
  padding: 4px 16px;
  border-radius: 8px;
  background: #3a2605;
  color: #ffd9a0;
  font-weight: 700;
}
.rule-text { margin: 0; font-size: 26px; max-width: 30em; }

/* ---- 倒數 ---- */
.countdown { margin: 0; font-size: clamp(160px, 30vw, 360px); font-weight: 900; line-height: 1; font-variant-numeric: tabular-nums; }

/* ---- 回合 ---- */
.round-label { position: absolute; top: 20px; left: 0; right: 0; margin: 0; font-size: 28px; color: var(--muted); }
#stage { position: relative; width: 100%; flex: 1; display: flex; align-items: center; justify-content: center; }

.cross { position: absolute; width: 64px; height: 64px; }
.cross::before, .cross::after { content: ""; position: absolute; background: var(--muted); border-radius: 3px; }
.cross::before { left: 29px; top: 0; width: 6px; height: 64px; }
.cross::after { top: 29px; left: 0; height: 6px; width: 64px; }

/* 提示色塊：沒有任何過場動畫，瞬間出現 */
.cue { width: 60vw; height: 60vh; border-radius: 12px; }
.cue[data-color="RED"] { background: var(--red); }
.cue[data-color="WHITE"] { background: var(--white); }

.feedback { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.fb-icon { width: 120px; height: 120px; }
.fb-main { margin: 0; font-size: clamp(72px, 12vw, 160px); font-weight: 900; line-height: 1.1; font-variant-numeric: tabular-nums; }
.fb-sub { margin: 0; font-size: 28px; color: var(--muted); min-height: 1.5em; }
.feedback[data-ok="true"] .fb-main { color: var(--ok); }
.feedback[data-ok="true"] #fb-icon-bad,
.feedback[data-ok="false"] #fb-icon-ok { display: none; }

#stage .cross, #stage .cue, #stage .feedback { display: none; }
#stage[data-phase="wait"] .cross { display: block; }
#stage[data-phase="cue"] .cue { display: block; }
#stage[data-phase="feedback"] .feedback { display: flex; }

/* ---- 結算 ---- */
.big-stats { display: flex; gap: 20px 48px; flex-wrap: wrap; justify-content: center; }
.big-stat { display: flex; flex-direction: column; align-items: center; }
.big-label { color: var(--muted); font-size: 22px; }
.big-num { font-size: clamp(64px, 6.5vw, 96px); font-weight: 900; line-height: 1.15; font-variant-numeric: tabular-nums; white-space: nowrap; }
#stat-accuracy, #fr-accuracy { color: var(--brand-orange); }
.big-sub { color: var(--muted); font-size: 22px; font-variant-numeric: tabular-nums; }
.counts { margin: 0; font-size: 22px; color: var(--muted); }

.bars {
  display: flex;
  align-items: flex-end;
  gap: 14px;
  height: 140px;
  width: min(720px, 90%);
  border-bottom: 3px solid var(--line);
  padding: 0 8px;
}
.bar { flex: 1; min-height: 8px; border-radius: 6px 6px 0 0; background: #7a8794; }
.bar[data-result="correct"] { background: var(--ok); }
.bar[data-result="wrong"], .bar[data-result="both"] { background: var(--bad); }
.bar[data-result="early"], .bar[data-result="timeout"] { background: #7a8794; }

.rounds-table { border-collapse: collapse; font-size: 20px; width: min(720px, 100%); }
.rounds-table th, .rounds-table td { padding: 6px 12px; border-bottom: 1px solid var(--line); text-align: center; font-variant-numeric: tabular-nums; }
.rounds-table th { color: var(--muted); font-weight: 500; }
.swatch { display: inline-block; width: 16px; height: 16px; border-radius: 50%; margin-right: 8px; vertical-align: -2px; border: 2px solid #3a566e; }
.swatch-RED { background: var(--red); }
.swatch-WHITE { background: var(--white); }
td.r-correct { color: var(--ok); font-weight: 700; }
td.r-wrong, td.r-both { color: var(--bad); font-weight: 700; }
td.r-early, td.r-timeout { color: var(--muted); font-weight: 700; }

/* ---- 首頁：選遊戲、選難度 ---- */
.step-label { margin: 0; color: var(--muted); font-size: 22px; font-weight: 700; letter-spacing: 0.05em; }
.pick-row { display: flex; gap: 20px; flex-wrap: wrap; justify-content: center; }
.pick {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
  min-width: 260px;
  max-width: 340px;
  padding: 16px 24px;
  border-radius: 16px;
  border: 3px solid var(--line);
  background: var(--bg-raised);
  color: var(--text);
}
.pick:hover { border-color: var(--muted); }
.pick[aria-checked="true"] { border-color: var(--brand-orange); background: #3a2605; }
.pick-name { font-size: 34px; font-weight: 900; }
.pick-desc { font-size: 18px; color: var(--muted); }
.pick[aria-checked="true"] .pick-desc { color: #ffd9a0; }
.diff-row { display: flex; gap: 14px; align-items: center; justify-content: center; flex-wrap: wrap; }
.diff-label { font-size: 24px; font-weight: 700; }
.pick-small { min-width: 110px; padding: 8px 20px; font-size: 28px; font-weight: 700; border-radius: 12px; }

/* ---- 規則頁：紅白旗 ---- */
.rules-block { display: flex; flex-direction: column; align-items: center; gap: 20px; }
.pad-where { max-width: 12em; }
#rules-flag .pad-where { max-width: 20em; }   /* 「在左邊，按住舉起，放開放下」不要斷成孤字 */
.voice-row { display: flex; gap: 16px; align-items: center; justify-content: center; flex-wrap: wrap; }
.btn-small { min-width: 0; min-height: 56px; font-size: 24px; padding: 8px 24px; }
.voice-row .note { min-height: 0; }

/* ---- 紅白旗畫面 ---- */
.screen-flag { justify-content: flex-start; gap: 12px; padding-top: 56px; }
.flag-cue {
  margin: 0;
  min-height: 1.25em;
  font-size: clamp(48px, 11vh, 120px);
  font-weight: 900;
  line-height: 1.2;
  letter-spacing: 0.04em;
}
.flag-cue[data-warn="true"] { color: var(--brand-orange); }

.flag-stage {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: stretch;
  gap: clamp(32px, 14vw, 220px);
}
.flag { display: flex; flex-direction: column; align-items: center; gap: 8px; min-height: 0; }
.flag-pole {
  position: relative;
  flex: 1;
  min-height: 120px;
  width: clamp(170px, 20vw, 300px);
}
/* 旗桿 */
.flag-pole::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 12px;
  border-radius: 6px;
  background: #8aa0b3;
}
/* 旗面：舉著貼頂端，放下貼底部。過場很短，不拖慢操作 */
.flag-cloth {
  position: absolute;
  left: 12px;
  top: 0;
  width: 100%;
  height: clamp(64px, 38%, 130px);
  border-radius: 4px 14px 14px 4px;
  border: 4px solid #3a566e;
  transition: top 90ms ease-out, opacity 90ms ease-out;
}
.flag[data-color="red"] .flag-cloth { background: var(--red); }
.flag[data-color="white"] .flag-cloth { background: var(--white); }
.flag[data-up="false"] .flag-cloth { top: calc(100% - clamp(64px, 38%, 130px)); opacity: 0.55; }
.flag[data-up="true"] .flag-pole::before { background: var(--brand-orange); }
.flag-name { margin: 0; font-size: clamp(24px, 4.6vh, 36px); font-weight: 700; white-space: nowrap; }
.flag-state {
  display: inline-block;
  min-width: 3.4em;
  padding: 0 10px;
  border-radius: 999px;
  background: #2b4256;
  color: var(--muted);
  text-align: center;
}
.flag[data-up="true"] .flag-state { background: var(--ok); color: #0b1620; }

.flag-feedback {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
  min-height: clamp(84px, 16vh, 150px);
  visibility: hidden;
}
.flag-feedback[data-show="true"] { visibility: visible; }
.flag-feedback .fb-icon { width: clamp(64px, 12vh, 110px); height: clamp(64px, 12vh, 110px); }
.flag-feedback[data-ok="true"] .fb-icon-bad,
.flag-feedback[data-ok="false"] .fb-icon-ok { display: none; }
.flag-fb-text { text-align: left; }
.flag-fb-main { margin: 0; font-size: clamp(48px, 10vh, 100px); font-weight: 900; line-height: 1.1; font-variant-numeric: tabular-nums; }
.flag-fb-sub { margin: 0; font-size: 24px; color: var(--muted); min-height: 1.5em; }
.flag-feedback[data-ok="true"] .flag-fb-main { color: var(--ok); }

/* 紅白旗結算 */
.flag-table td:nth-child(2) { text-align: left; }
.flag-table td:nth-child(3) { color: var(--muted); }
.trap-tag { margin-left: 8px; padding: 0 8px; border-radius: 6px; background: #3a2605; color: #ffd9a0; font-size: 16px; }
td.r-held { color: var(--ok); font-weight: 700; }

/* ---- 暫停遮罩 ---- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: #0b1620;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  padding: 32px;
  text-align: center;
}
.overlay-title { margin: 0; font-size: 56px; }
.overlay-text { margin: 0; font-size: 28px; color: var(--muted); max-width: 30em; }

/* ---- 除錯面板 ---- */
.debug {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  max-height: 45vh;
  background: rgba(5, 10, 16, 0.96);
  border-top: 2px solid var(--brand-orange);
  font-size: 14px;
  padding: 8px 16px;
  cursor: default;
  overflow-y: auto;
}
.debug-head { display: flex; gap: 20px; align-items: center; flex-wrap: wrap; }
.debug-sound { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 6px; }
.debug .tool-btn { font-size: 14px; padding: 2px 10px; }
.debug-log { margin: 6px 0 0; height: 22em; max-height: 30vh; overflow-y: auto; font-family: ui-monospace, Consolas, monospace; font-size: 13px; line-height: 1.4; white-space: pre-wrap; word-break: break-all; color: #cfe3f2; }

/* ---- 手機說明 ---- */
.mobile-block { position: fixed; inset: 0; z-index: 100; background: var(--bg); padding: 32px; display: flex; flex-direction: column; justify-content: center; text-align: center; gap: 12px; }
.mobile-block h1 { margin: 0; font-size: 32px; }
.mobile-block p { margin: 0; color: var(--muted); }

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
  .flag-cloth { transition: none; }
}

/* ---- 中等高度（例如 1366x768）：紅白旗規則頁縮一點，「回首頁」不用捲動就看得到 ---- */
@media (max-height: 820px) {
  .rule-text { font-size: 22px; max-width: 40em; }
  #rules-flag .pad-disc { width: 96px; height: 96px; }
}

/* ---- 矮螢幕（例如 1024x600）：縮小規則頁與首頁的間距，讓「開始」不用捲動就看得到 ---- */
@media (max-height: 700px) {
  .screen { gap: 12px; padding: 56px 24px 16px; }
  .heading { font-size: 32px; }
  .pad-disc { width: 96px; height: 96px; border-width: 5px; }
  .pad-row { gap: 56px; }
  .pad-name { font-size: 26px; }
  .btn { min-height: 64px; }
  .pick { padding: 10px 20px; }
  .pick-name { font-size: 28px; }
  .step-label { font-size: 18px; }
  .title { font-size: clamp(44px, 7vw, 72px); padding-bottom: 8px; }
  .rules-block { gap: 12px; }
  .rule-text { font-size: 22px; max-width: 40em; }
  #rules-flag .pad-disc { width: 76px; height: 76px; }
  .screen-flag { padding-top: 48px; }
}
