:root {
  --bg: #171a2e;
  --bg-2: #20244a;
  --panel: #262a52;
  --panel-edge: #3a3f78;
  --ink: #f4f1ff;
  --ink-dim: #a9adda;
  --accent: #ffd166;
  --accent-2: #ff8fab;
  --board: #2a2e58;
  --radius: 18px;
  --cell: 44px; /* 由 game.js 依視窗大小更新 */
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: "Noto Sans TC", "Noto Sans CJK TC", "PingFang TC", "Microsoft JhengHei",
    system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(1200px 700px at 15% -10%, #3a2f6b 0%, transparent 60%),
    radial-gradient(900px 600px at 100% 110%, #173f5a 0%, transparent 60%),
    var(--bg);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

.app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 14px 16px calc(14px + env(safe-area-inset-bottom));
}

/* ---- 標題 ---- */

.masthead {
  text-align: center;
}

.masthead h1 {
  margin: 0;
  font-size: clamp(22px, 3.4vw, 34px);
  font-weight: 900;
  letter-spacing: 0.06em;
  line-height: 1.15;
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.35);
}

.masthead h1 span {
  color: var(--accent);
  margin-left: 0.15em;
}

.tagline {
  margin: 4px 0 0;
  font-size: 13px;
  color: var(--ink-dim);
}

/* ---- 版面 ---- */

.stage {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 16px;
  width: 100%;
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 190px;
  flex: none;
}

.card {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.05), transparent 40%), var(--panel);
  border: 2px solid var(--panel-edge);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.25);
}

.card h2 {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.18em;
  color: var(--ink-dim);
}

/* ---- 角色列表 ---- */

.cast {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.cast li {
  display: flex;
  align-items: center;
  gap: 10px;
}

.cast canvas {
  width: 40px;
  height: 40px;
  flex: none;
}

.cast .who {
  display: flex;
  flex-direction: column;
  line-height: 1.25;
}

.cast .name {
  font-weight: 800;
  font-size: 15px;
}

.cast .org {
  font-size: 12px;
  color: var(--ink-dim);
}

/* ---- 操作說明 ---- */

.keys {
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12.5px;
}

.keys div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.keys dt {
  white-space: nowrap;
  color: var(--ink-dim);
}

.keys dd {
  margin: 0;
  text-align: right;
}

kbd {
  display: inline-block;
  min-width: 22px;
  padding: 1px 6px;
  margin: 0 1px;
  border-radius: 6px;
  border: 1px solid #5a60a8;
  border-bottom-width: 3px;
  background: #343970;
  color: var(--ink);
  font: inherit;
  font-size: 11.5px;
  text-align: center;
}

/* ---- 盤面 ---- */

.board-wrap {
  position: relative;
  flex: none;
  padding: 8px;
  border-radius: 22px;
  background: linear-gradient(180deg, #4a4f95, #33376e);
  box-shadow:
    0 10px 0 rgba(0, 0, 0, 0.3),
    inset 0 2px 0 rgba(255, 255, 255, 0.18);
}

#board {
  display: block;
  width: calc(var(--cell) * 6);
  height: calc(var(--cell) * 12);
  border-radius: 14px;
  background: var(--board);
  touch-action: none;
}

.board-wrap.shake {
  animation: shake 0.32s ease-out;
}

@keyframes shake {
  0% { transform: translate(0, 0); }
  20% { transform: translate(-5px, 3px); }
  40% { transform: translate(5px, -3px); }
  60% { transform: translate(-3px, 2px); }
  80% { transform: translate(2px, -1px); }
  100% { transform: translate(0, 0); }
}

/* ---- 開始／暫停／結束畫面 ---- */

.overlay {
  position: absolute;
  inset: 8px;
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(6px, calc(var(--cell) * 0.28), 14px);
  padding: clamp(8px, calc(var(--cell) * 0.36), 18px);
  overflow: hidden;
  text-align: center;
  background: rgba(18, 20, 44, 0.86);
  backdrop-filter: blur(3px);
}

.overlay[hidden] {
  display: none;
}

.overlay h2 {
  margin: 0;
  font-size: clamp(22px, calc(var(--cell) * 0.66), 32px);
  font-weight: 900;
  letter-spacing: 0.08em;
  color: var(--accent);
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.4);
}

.overlay p {
  margin: 0;
  font-size: 13.5px;
  line-height: 1.7;
  color: var(--ink);
}

.overlay .dim {
  color: var(--ink-dim);
  font-size: 12px;
}

.overlay .big {
  font-size: 30px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.overlay .record {
  color: var(--accent-2);
  font-weight: 800;
}

.overlay .trio {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
}

.overlay .trio canvas {
  /* 跟著盤面縮放，六個才排得下 */
  width: min(42px, calc(var(--cell) * 0.75));
  height: min(42px, calc(var(--cell) * 0.75));
  animation: bob 1.4s ease-in-out infinite;
}

.overlay .trio canvas:nth-child(2) {
  animation-delay: 0.18s;
}

.overlay .trio canvas:nth-child(3) {
  animation-delay: 0.36s;
}

.overlay .trio canvas:nth-child(4) {
  animation-delay: 0.54s;
}

.overlay .trio canvas:nth-child(5) {
  animation-delay: 0.72s;
}

.overlay .trio canvas:nth-child(6) {
  animation-delay: 0.9s;
}

/* ---- 難度切換 ---- */

.picker {
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: 14px;
  border: 1px solid var(--panel-edge);
  background: rgba(0, 0, 0, 0.28);
}

.pick {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 6px 12px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--ink-dim);
  font: inherit;
  line-height: 1.25;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.pick-label {
  font-size: 14px;
  font-weight: 800;
}

.pick-sub {
  font-size: 11px;
}

.pick.on {
  background: var(--panel-edge);
  color: var(--ink);
  box-shadow: inset 0 0 0 2px var(--accent);
}

.pick:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

@keyframes bob {
  0%, 100% { transform: translateY(0) scale(1, 1); }
  45% { transform: translateY(-7px) scale(0.97, 1.04); }
  60% { transform: translateY(0) scale(1.06, 0.93); }
}

/* ---- 按鈕 ---- */

.btn,
.btn-primary {
  font: inherit;
  font-weight: 800;
  color: var(--ink);
  cursor: pointer;
  border-radius: 14px;
  border: 2px solid var(--panel-edge);
  border-bottom-width: 5px;
  background: var(--panel);
  padding: 8px 12px;
  transition: transform 0.06s, border-bottom-width 0.06s;
  -webkit-tap-highlight-color: transparent;
}

.btn:active,
.btn-primary:active {
  transform: translateY(3px);
  border-bottom-width: 2px;
}

.btn:focus-visible,
.btn-primary:focus-visible,
.touch button:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.btn-primary {
  font-size: 17px;
  letter-spacing: 0.12em;
  padding: 11px 26px;
  color: #3a2500;
  background: var(--accent);
  border-color: #c9962c;
}

.buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ---- 資訊欄 ---- */

.card-next {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.card-next h2 {
  align-self: flex-start;
}

#next {
  display: block;
}

.card-stats {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stat {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.stat .label {
  font-size: 12px;
  letter-spacing: 0.1em;
  white-space: nowrap;
  color: var(--ink-dim);
}

.stat .value {
  font-size: 18px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.stat-score {
  flex-direction: column;
  align-items: flex-start;
  gap: 0;
  padding-bottom: 8px;
  border-bottom: 1px dashed var(--panel-edge);
}

.stat-score .value {
  font-size: 28px;
  color: var(--accent);
  line-height: 1.15;
}

/* ---- 觸控按鈕（預設隱藏，觸控裝置或窄螢幕才出現） ---- */

.touch {
  display: none;
  grid-template-columns: repeat(6, 1fr);
  gap: 8px;
  width: min(100%, 420px);
  user-select: none;
  -webkit-user-select: none;
}

.touch button {
  font: inherit;
  font-size: 22px;
  font-weight: 800;
  color: var(--ink);
  height: 56px;
  border-radius: 16px;
  border: 2px solid var(--panel-edge);
  border-bottom-width: 5px;
  background: var(--panel);
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}

.touch button.held,
.touch button:active {
  transform: translateY(3px);
  border-bottom-width: 2px;
  background: #343970;
}

.footnote {
  max-width: 640px;
  margin-top: auto;
  font-size: 11px;
  line-height: 1.6;
  text-align: center;
  color: #7f84b8;
}

@media (hover: none) and (pointer: coarse) {
  .touch {
    display: grid;
  }

  /* 觸控裝置沒有鍵盤，鍵盤提示就不用佔位置 */
  .kbd-hint {
    display: none;
  }
}

/* ---- 窄螢幕：資訊欄移到盤面上方，隱藏鍵盤說明 ---- */

@media (max-width: 760px) {
  .app {
    gap: 8px;
    padding-top: 8px;
  }

  .tagline {
    display: none;
  }

  .stage {
    flex-direction: column;
    align-items: center;
    gap: 8px;
  }

  .panel-left {
    display: none;
  }

  .panel-right {
    order: -1;
    flex-direction: row;
    align-items: stretch;
    width: min(100%, 420px);
    gap: 8px;
  }

  .panel-right .card {
    padding: 8px 10px;
    border-radius: 14px;
  }

  .card-next {
    flex: none;
  }

  .card-stats {
    flex: 1;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2px 12px;
    align-content: center;
  }

  .stat-score {
    grid-column: 1 / -1;
    flex-direction: row;
    align-items: baseline;
    border-bottom: 0;
    padding-bottom: 0;
  }

  .stat-score .value {
    font-size: 22px;
  }

  .stat .value {
    font-size: 15px;
  }

  .stat .label {
    font-size: 11px;
    letter-spacing: 0;
  }

  .kbd-hint {
    display: none;
  }

  .buttons {
    flex: none;
    justify-content: center;
  }

  .buttons .btn {
    padding: 6px 8px;
    font-size: 12px;
  }

  .touch {
    display: grid;
  }

  .footnote {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .board-wrap.shake,
  .overlay .trio canvas {
    animation: none;
  }
}

/* ---- Jensen 障礙：開關與倒數 ---- */

.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px 5px 6px;
  border-radius: 14px;
  border: 1px solid var(--panel-edge);
  background: rgba(0, 0, 0, 0.28);
  color: var(--ink-dim);
  font: inherit;
  line-height: 1.25;
  text-align: left;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.toggle canvas {
  width: 30px;
  height: 30px;
  flex: none;
  filter: grayscale(1);
  opacity: 0.55;
}

.toggle-text {
  display: flex;
  flex-direction: column;
}

.toggle-state {
  min-width: 30px;
  padding: 2px 8px;
  border-radius: 999px;
  background: #3a3f63;
  font-size: 12px;
  font-weight: 800;
  text-align: center;
}

.toggle.on {
  color: var(--ink);
}

.toggle.on canvas {
  filter: none;
  opacity: 1;
}

.toggle.on .toggle-state {
  background: #a4e02a;
  color: #1c2a00;
}

.toggle:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.stat[hidden] {
  display: none;
}

.stat-jensen .value {
  font-size: 14px;
  color: #b8f03c;
  white-space: nowrap;
}

.stat-jensen.warn .value {
  color: var(--accent-2);
  animation: warn-blink 0.5s steps(2, jump-none) infinite;
}

@keyframes warn-blink {
  from { opacity: 1; }
  to { opacity: 0.45; }
}

/* ---- 盤面很小時的精簡版覆蓋畫面 ---- */

.overlay {
  /* 內容真的放不下時可以捲動，而且不會把頂端裁掉 */
  justify-content: safe center;
  overflow: hidden auto;
}

.overlay.compact .trio,
.overlay.compact .pick-sub,
.overlay.compact .kbd-hint {
  display: none;
}

.overlay.compact .big {
  font-size: 24px;
  line-height: 1.2;
}

.overlay.compact .toggle canvas {
  width: 24px;
  height: 24px;
}

/* ---- 視窗很矮時：角色列表只留頭像，免得左欄比盤面還高 ---- */

@media (max-height: 740px) and (min-width: 761px) {
  .cast {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 6px;
  }

  .cast .who {
    display: none;
  }

  .cast canvas {
    width: 34px;
    height: 34px;
  }

  .keys {
    gap: 4px;
    font-size: 12px;
  }

  .tagline {
    display: none;
  }

  .card {
    padding: 9px 12px;
  }

  .card-stats {
    gap: 5px;
  }

  .buttons {
    flex-direction: row;
  }

  .buttons .btn {
    flex: 1;
    padding: 6px 4px;
    font-size: 13px;
  }
}

@media (max-width: 760px) {
  .stat-jensen .value {
    font-size: 13px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .stat-jensen.warn .value {
    animation: none;
  }
}
