:root {
  --bg: #0f1a24;
  --panel: #1a2a3a;
  --text: #f0f6fc;
  --muted: #b6c7d9;
  --accent: #7dd3fc;
  --good: #6ee7b7;
  --warn: #fbbf24;
  --danger: #fca5a5;
  --shadow: rgba(0, 0, 0, 0.35);
  --touch: 64px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --radius: 16px;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; }
html, body { height: 100%; }
body {
  min-height: 100vh;
  min-height: 100dvh;
  font-family: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  background:
    radial-gradient(ellipse at 15% 0%, #1e3a5f 0%, transparent 50%),
    radial-gradient(ellipse at 85% 100%, #4c1d95 0%, transparent 45%),
    var(--bg);
  color: var(--text);
  padding:
    max(0.5rem, var(--safe-t))
    max(0.65rem, var(--safe-r))
    max(0.5rem, var(--safe-b))
    max(0.65rem, var(--safe-l));
  overscroll-behavior: none;
  touch-action: manipulation;
  user-select: none;
}
button {
  font: inherit; border: none; cursor: pointer; color: inherit;
  touch-action: manipulation;
}
button:active:not(:disabled) { transform: scale(0.98); }
button:disabled { opacity: 0.45; cursor: not-allowed; }

.app {
  max-width: 720px;
  margin: 0 auto;
  min-height: calc(100dvh - 1rem);
  display: flex;
  flex-direction: column;
}
.app.in-game {
  max-width: 900px;
}
.screen {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}
#screen-game.screen {
  gap: 0.38rem;
}
.screen.hidden, .overlay.hidden, .hidden { display: none !important; }

.top-header { text-align: center; flex-shrink: 0; }
.top-header h1 {
  font-size: clamp(1.05rem, 3.6vw, 1.4rem);
  letter-spacing: 0.04em;
  color: var(--accent);
  font-weight: 800;
  line-height: 1.35;
}
.gentle {
  color: var(--muted);
  font-size: clamp(0.92rem, 2.7vw, 1.08rem);
  line-height: 1.45;
  margin-top: 0.3rem;
  font-weight: 600;
}
.top-actions {
  display: flex; justify-content: center; gap: 0.55rem;
  margin-top: 0.55rem; flex-wrap: wrap;
}
.btn-chip {
  min-height: 48px; padding: 0.45rem 1rem; border-radius: 999px;
  background: rgba(125, 211, 252, 0.12);
  border: 1px solid rgba(125, 211, 252, 0.35);
  color: var(--accent); font-weight: 800;
}
.panel, .home-panel {
  flex: 1; display: flex; flex-direction: column;
  background: var(--panel);
  border: 1px solid rgba(125, 211, 252, 0.15);
  border-radius: var(--radius);
  padding: 0.9rem; box-shadow: 0 8px 24px var(--shadow);
  min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.section-label {
  font-size: clamp(0.98rem, 2.9vw, 1.12rem);
  font-weight: 800; color: var(--warn);
  margin: 0.4rem 0 0.5rem;
}
.section-label.small { font-size: 0.95rem; color: var(--muted); font-weight: 700; }
.player-chips, .chip-row {
  display: flex; flex-wrap: wrap; gap: 0.5rem;
}
.player-chips button, .chip {
  min-height: var(--touch); min-width: 5.2rem;
  padding: 0.55rem 0.85rem; border-radius: 14px;
  background: rgba(0,0,0,0.28);
  border: 2px solid rgba(155,176,196,0.25);
  font-weight: 800;
  font-size: clamp(0.95rem, 2.8vw, 1.1rem);
}
.player-chips button.active, .chip.active {
  border-color: var(--warn);
  background: rgba(251,191,36,0.18);
  color: var(--warn);
}
.current-player { margin-top: 0.55rem; font-size: 1.15rem; font-weight: 800; }
.current-player span { color: var(--warn); }
.hint-line {
  margin: 0.45rem 0 0.35rem; color: var(--muted);
  font-size: 1.02rem; line-height: 1.5; font-weight: 700;
}
.btn-start, .btn.big {
  min-height: 72px; width: 100%; margin-top: 0.75rem;
  border-radius: 16px; font-size: 1.25rem; font-weight: 900;
}
.btn-start, .btn-primary {
  background: linear-gradient(180deg, #a78bfa 0%, #7c3aed 100%);
  color: #f5f3ff;
  box-shadow: 0 6px 18px rgba(124, 58, 237, 0.4);
}
.btn-start.alt {
  background: linear-gradient(180deg, #67e8f9 0%, #0891b2 100%);
  color: #083344;
  margin-top: 0.45rem;
}
#btn-more-setup {
  width: 100%;
  margin: 0.55rem 0 0.15rem;
  min-height: 56px;
}
.advanced-setup {
  margin-top: 0.35rem;
  padding-top: 0.25rem;
  border-top: 1px dashed rgba(255,255,255,0.1);
}
.btn-secondary {
  background: linear-gradient(180deg, #7dd3fc 0%, #0ea5e9 100%);
  color: #082f49;
}
.btn-ghost {
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.12);
}
.btn-warn {
  background: linear-gradient(180deg, #fca5a5 0%, #ef4444 100%);
  color: #450a0a;
}

.online-setup { margin-top: 0.35rem; }
.room-list {
  display: flex; flex-direction: column; gap: 0.45rem;
  margin: 0.35rem 0;
}
.room-card {
  text-align: left;
  min-height: 64px;
  padding: 0.75rem 0.9rem;
  border-radius: 14px;
  background: rgba(0,0,0,0.28);
  border: 2px solid rgba(167, 139, 250, 0.35);
  font-weight: 800;
}
.room-card .sub { color: var(--muted); font-size: 0.88rem; margin-top: 0.2rem; }

/* game */
.game-header {
  background: var(--panel);
  border: 1px solid rgba(167, 139, 250, 0.25);
  border-radius: var(--radius);
  padding: 0.35rem 0.6rem 0.4rem;
  flex-shrink: 0;
}
.game-title-row {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 0.2rem;
}
.game-title { font-size: 1.02rem; color: #c4b5fd; font-weight: 900; }
.play-player { font-weight: 900; color: var(--warn); font-size: 0.98rem; }
.stats {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 0.22rem;
}
.stat {
  background: rgba(0,0,0,0.22); border-radius: 10px;
  padding: 0.18rem 0.12rem; text-align: center;
}
.stat-label { display: block; font-size: 0.78rem; color: var(--muted); font-weight: 800; letter-spacing: 0.02em; }
.stat-value { display: block; font-size: 0.98rem; font-weight: 900; margin-top: 0.02rem; }
.toast-line {
  min-height: 1.1em; margin-top: 0.12rem; text-align: center;
  color: var(--good); font-weight: 800; font-size: 0.88rem;
}
.room-banner {
  text-align: center; font-weight: 800; color: #c4b5fd;
  margin-top: 0.12rem; font-size: 0.92rem;
}

.ball-stage {
  display: flex; flex-direction: column; align-items: center;
  flex: 0 2 auto;
  gap: 0.2rem;
}
.ball {
  width: min(118px, 28vw); height: min(118px, 28vw);
  border-radius: 50%;
  background: radial-gradient(circle at 30% 28%, #fff 0%, #fde68a 25%, #f59e0b 70%, #b45309 100%);
  border: 4px solid #fff;
  box-shadow: 0 10px 28px rgba(245, 158, 11, 0.45), inset 0 -8px 16px rgba(0,0,0,0.15);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: #451a03;
  animation: none;
}
.ball.pop { animation: ball-pop 0.45s cubic-bezier(0.34, 1.5, 0.64, 1); }
.ball-label { font-size: 0.88rem; font-weight: 800; opacity: 0.92; }
.ball-num { font-size: clamp(2.45rem, 9vw, 3.4rem); font-weight: 900; line-height: 1; }
.ball-hint { color: var(--muted); font-weight: 700; font-size: 0.95rem; text-align: center; }

@keyframes ball-pop {
  0% { transform: scale(0.6); }
  60% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

.history-strip {
  display: flex; flex-wrap: nowrap; gap: 0.4rem;
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  padding: 0.15rem 0.1rem; min-height: 2.85rem; flex-shrink: 0;
}
.hist-chip {
  flex: 0 0 auto;
  min-width: 2.85rem; min-height: 2.85rem;
  border-radius: 999px;
  background: rgba(167, 139, 250, 0.25);
  border: 1px solid rgba(196, 181, 253, 0.4);
  display: flex; align-items: center; justify-content: center;
  font-weight: 900; font-size: 1.12rem;
}
.hist-chip.latest {
  background: linear-gradient(180deg, #fde68a, #f59e0b);
  color: #451a03; border-color: #fff;
}

.board-wrap {
  flex: 1 1 auto;
  min-height: 240px;
  display: flex; align-items: center; justify-content: center;
}
.bingo-board {
  display: grid;
  gap: 0.45rem;
  /* 自動塞進可視高度，避免內層捲軸裁切 */
  width: min(100%, 560px, 58dvh, 72vw);
  max-width: 100%;
  aspect-ratio: 1;
  max-height: min(58dvh, 560px);
  padding: 0.45rem;
  background:
    linear-gradient(145deg, rgba(91, 33, 182, 0.45), rgba(15, 26, 36, 0.95));
  border: 3px solid rgba(253, 230, 138, 0.55);
  border-radius: 18px;
  box-shadow: 0 12px 32px rgba(0,0,0,0.35);
}
.bingo-board.size-3 { grid-template-columns: repeat(3, 1fr); }
.bingo-board.size-5 { grid-template-columns: repeat(5, 1fr); gap: 0.35rem; }

.cell {
  border-radius: 14px;
  background: #ffffff;
  color: #0b1220;
  font-weight: 900;
  font-size: clamp(1.7rem, 8.8vw, 2.9rem);
  border: 3px solid #1e3a5f;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 3px 0 rgba(0,0,0,0.18);
  transition: transform 0.12s, background 0.15s, border-color 0.15s, box-shadow 0.2s;
  min-height: 72px;
  overflow: hidden;
}
.cell-photo {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0.38;
  pointer-events: none;
}
.cell-n {
  position: relative;
  z-index: 1;
  color: #0b1220;
  font-variant-numeric: tabular-nums;
  text-shadow:
    0 0 6px #fff,
    0 1px 0 #fff,
    0 0 14px #fff,
    0 2px 3px rgba(255,255,255,.95),
    0 2px 8px rgba(0,0,0,.55);
}
.cell.marked .cell-photo { opacity: 0.2; }
.cell.marked .cell-n {
  text-shadow:
    0 0 4px rgba(0,0,0,.9),
    0 1px 3px rgba(0,0,0,.85),
    0 0 10px rgba(0,0,0,.55);
}
.bingo-board.size-3 .cell { min-height: 88px; }
.bingo-board.size-5 .cell {
  font-size: clamp(1.45rem, 6.4vw, 2.15rem);
  border-radius: 10px;
  min-height: 60px;
}
.cell.free {
  background: linear-gradient(180deg, #fde68a, #fbbf24);
  color: #78350f;
}
.cell.marked {
  background: linear-gradient(180deg, #c4b5fd, #7c3aed);
  color: #fff;
  border-color: #fbbf24;
  box-shadow: 0 0 0 2px rgba(251,191,36,0.45);
}
.cell.marked::after {
  content: "蓋";
  position: absolute;
  right: 4px;
  bottom: 3px;
  font-size: 0.62em;
  font-weight: 900;
  color: #fde68a;
  text-shadow: 0 1px 2px rgba(0,0,0,.55);
  pointer-events: none;
}
.cell { position: relative; }
.cell.just { animation: cell-stamp 0.55s ease; }
.cell.stamped { box-shadow: inset 0 0 0 3px #fbbf24, 0 6px 0 rgba(88, 28, 135, 0.45); }
@keyframes cell-stamp {
  0% { transform: scale(1.18) rotate(-4deg); }
  55% { transform: scale(0.94) rotate(1deg); }
  100% { transform: scale(1) rotate(0); }
}
.cell.win-line {
  background: linear-gradient(180deg, #86efac, #059669) !important;
  border-color: #fde68a !important;
  color: #fff !important;
  z-index: 2;
  transform: scale(1.06);
  box-shadow:
    0 0 0 3px #fbbf24,
    0 0 18px 6px rgba(110, 231, 183, 0.9),
    0 0 34px 10px rgba(251, 191, 36, 0.55);
  animation: win-pulse 0.85s ease-in-out 3;
}
.cell.win-line::before {
  content: "✨";
  position: absolute;
  top: 2px;
  right: 3px;
  font-size: 0.55em;
  z-index: 2;
  pointer-events: none;
  filter: drop-shadow(0 1px 2px rgba(0,0,0,.4));
}
.cell.win-line .cell-n {
  text-shadow:
    0 0 6px rgba(0,0,0,.85),
    0 2px 4px rgba(0,0,0,.7);
}
@keyframes cell-flash {
  0%, 100% { transform: scale(1); }
  40% { transform: scale(1.08); }
}
@keyframes win-pulse {
  0%, 100% { transform: scale(1.04); filter: brightness(1); }
  45% { transform: scale(1.12); filter: brightness(1.22); }
}

.game-actions {
  display: grid; grid-template-columns: 1fr 1fr; gap: 0.5rem;
  flex-shrink: 0;
}
.btn.action-btn {
  min-height: 56px; border-radius: 14px; font-weight: 900; font-size: 1.02rem;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.12);
}
.btn.action-btn.primary {
  background: linear-gradient(180deg, #a78bfa, #7c3aed);
  border: none; color: #fff;
}
.btn.action-btn.ghost { background: rgba(0,0,0,0.22); }

/* overlays */
.overlay {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(5, 12, 20, 0.75);
  display: flex; align-items: center; justify-content: center;
  padding:
    max(0.75rem, var(--safe-t))
    max(0.75rem, var(--safe-r))
    max(0.75rem, var(--safe-b))
    max(0.75rem, var(--safe-l));
  overflow-y: auto;
}
.overlay-card {
  width: min(560px, 100%);
  max-height: min(92dvh, 920px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--panel);
  border: 1px solid rgba(167, 139, 250, 0.3);
  border-radius: 18px;
  padding: 1rem;
  box-shadow: 0 16px 40px rgba(0,0,0,0.45);
}
.overlay-card h2 {
  text-align: center; color: var(--warn); font-weight: 900;
  font-size: clamp(1.2rem, 4vw, 1.45rem); margin-bottom: 0.4rem;
}
.win-card h2, #win-title {
  font-size: clamp(2rem, 7.5vw, 2.9rem) !important;
  letter-spacing: 0.04em;
  line-height: 1.2;
  color: #fde68a;
  text-shadow: 0 2px 0 rgba(120,53,15,.45), 0 4px 16px rgba(251,191,36,.55);
}
.overlay:not(.hidden) .win-card {
  border: 2px solid rgba(251, 191, 36, 0.6);
}
.win-emoji { text-align: center; font-size: 2.35rem; }
.win-msg, .win-rank { text-align: center; font-weight: 800; line-height: 1.45; margin: 0.35rem 0; }
.win-rank { color: var(--good); }
.win-label { text-align: center; }
.win-actions, .lb-actions, .confirm-actions {
  display: flex; flex-direction: column; gap: 0.45rem; margin-top: 0.65rem;
}
.help-list {
  margin: 0.75rem 0 0.75rem 1.2rem;
  font-size: 1.05rem; line-height: 1.65; font-weight: 700;
}
.lb-current { text-align: center; font-weight: 800; color: var(--muted); }
.lb-current span { color: var(--warn); }
.lb-rank-banner {
  margin: 0.5rem 0; padding: 0.55rem;
  border-radius: 12px; text-align: center; font-weight: 800; color: var(--good);
  background: rgba(110,231,183,0.1); border: 1px solid rgba(110,231,183,0.25);
}
.lb-table-wrap { max-height: 40vh; overflow-y: auto; margin-top: 0.45rem; }
.lb-empty { text-align: center; color: var(--muted); font-weight: 700; padding: 1rem; }
.lb-table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.lb-table th, .lb-table td {
  padding: 0.5rem 0.3rem; border-bottom: 1px solid rgba(255,255,255,0.08); font-weight: 700;
}
.lb-table th { color: var(--muted); position: sticky; top: 0; background: var(--panel); font-size: 0.9rem; }
.lb-table tr.me td { color: var(--warn); }
.btn-linkish {
  display: block; width: 100%; margin-top: 0.4rem; background: transparent;
  color: var(--muted); font-weight: 700; min-height: 44px; text-decoration: underline;
}
.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0,0,0,0); border: 0;
}

/* dance */
.dance-stage {
  height: clamp(150px, 26vh, 220px);
  margin: 0 auto 0.5rem;
  display: flex; align-items: center; justify-content: center;
  position: relative;
}
.dance-photo-wrap {
  width: min(190px, 48vw); height: 100%;
  display: flex; align-items: center; justify-content: center;
  border-radius: 16px; overflow: hidden;
  background: rgba(0,0,0,0.18);
  filter: drop-shadow(0 10px 18px rgba(0,0,0,0.35));
}
.dance-photo-wrap video {
  max-height: 100%; max-width: 100%; height: 100%; width: auto;
  object-fit: contain; border-radius: 14px; pointer-events: none;
}
.dance-note {
  position: absolute; font-size: 1.2rem;
  animation: note-float 1.1s ease-in-out infinite;
}
.dance-note.n1 { left: 8%; top: 12%; }
.dance-note.n2 { right: 10%; top: 18%; animation-delay: 0.25s; }
@keyframes note-float {
  0%, 100% { transform: translateY(0); opacity: 0.7; }
  50% { transform: translateY(-8px); opacity: 1; }
}
.cele-music-btn {
  display: block; width: 100%; margin: 0 0 0.55rem; min-height: 52px;
  border: 2px solid rgba(251,191,36,0.85); border-radius: 12px;
  background: linear-gradient(180deg, #fbbf24, #f59e0b);
  color: #1a1205; font-weight: 900; font-size: 1.05rem;
  animation: music-pulse 1.2s ease-in-out infinite;
}
.cele-music-btn.playing {
  background: linear-gradient(180deg, #6ee7b7, #34d399);
  border-color: rgba(110,231,183,0.85); color: #052e1a; animation: none;
}
@keyframes music-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.03); }
}

@media (max-width: 380px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .game-actions { grid-template-columns: 1fr; }
}
/* 矮螢幕：先縮小球，格子維持可點大小 */
@media (max-height: 820px) {
  .ball {
    width: min(88px, 22vw);
    height: min(88px, 22vw);
  }
  .ball-num { font-size: clamp(1.85rem, 7vw, 2.5rem); }
  .ball-hint { display: none; }
  .ball-label { font-size: 0.68rem; }
  .game-header { padding: 0.28rem 0.5rem 0.3rem; }
  .bingo-board.size-3 .cell { min-height: 72px; }
  .bingo-board.size-5 .cell { min-height: 56px; }
}
@media (max-height: 640px) {
  .ball {
    width: min(64px, 16vw);
    height: min(64px, 16vw);
    border-width: 3px;
  }
  .ball-num { font-size: clamp(1.45rem, 6vw, 1.9rem); }
  .history-strip { min-height: 2.4rem; }
  .hist-chip { min-width: 2.4rem; min-height: 2.4rem; font-size: 1rem; }
}
@media (prefers-reduced-motion: reduce) {
  .ball.pop, .cell.just, .cell.win-line, .dance-note, .cele-music-btn { animation: none !important; }
}

/* 蓋過共用 elder.css：賓果卡才是主角 */
html.elder-mode .bingo-board.size-3 .cell {
  min-height: 84px !important;
  font-size: clamp(1.9rem, 9.5vw, 3.1rem) !important;
  color: #020617 !important;
}
html.elder-mode .bingo-board.size-5 .cell {
  min-height: 62px !important;
  font-size: clamp(1.55rem, 7vw, 2.25rem) !important;
  color: #020617 !important;
}
@media (max-height: 820px) {
  html.elder-mode .bingo-board.size-3 .cell {
    min-height: auto !important;
    font-size: clamp(1.65rem, 8.2vw, 2.6rem) !important;
  }
  html.elder-mode .bingo-board.size-5 .cell {
    min-height: auto !important;
    font-size: clamp(1.35rem, 6.2vw, 2rem) !important;
  }
}
/* P1.1：單頁捲動 + 賓果卡自適配，不要內層 scrollbar 裁切牌面 */
body.fit-viewport #screen-game.screen:not(.hidden) {
  overflow-x: hidden !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
body.fit-viewport #screen-game.screen:not(.hidden) > .ball-stage {
  flex: 0 0 auto !important;
  min-height: auto !important;
  overflow: visible !important;
}
body.fit-viewport #screen-game.screen:not(.hidden) > .history-strip {
  flex: 0 0 auto;
  flex-shrink: 0;
  min-height: 2.7rem;
  overflow-x: auto;
  overflow-y: hidden;
}
body.fit-viewport #screen-game.screen:not(.hidden) > .board-wrap {
  flex: 0 0 auto !important;
  min-height: 0 !important;
  overflow: visible !important;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.15rem 0;
}
body.fit-viewport #screen-game.screen:not(.hidden) > .board-wrap .bingo-board {
  width: min(100%, 560px, 52dvh, 78vw);
  max-height: min(52dvh, 560px);
}
.board-wrap {
  overflow: visible;
}
