:root {
  --bg: #0f1a24;
  --panel: #1a2a3a;
  --text: #f0f6fc;
  --muted: #9bb0c4;
  --accent: #7dd3fc;
  --good: #6ee7b7;
  --warn: #fbbf24;
  --danger: #fca5a5;
  --shadow: rgba(0, 0, 0, 0.35);
  --touch: 64px;
  --pad: 1rem;
  --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;
  --piece-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  height: 100vh;
  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%, #134e4a 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;
  -webkit-user-select: none;
}

img {
  -webkit-user-drag: none;
  user-select: none;
  pointer-events: none;
}

button {
  font: inherit;
  border: none;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

button:active:not(:disabled) {
  transform: scale(0.98);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.app {
  height: 100%;
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.screen {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.screen.hidden,
.overlay.hidden,
.hidden {
  display: none !important;
}

/* —— 首頁 —— */
.top-header {
  text-align: center;
  flex-shrink: 0;
  margin-bottom: 0.45rem;
}

.top-header h1 {
  font-size: clamp(0.95rem, 3.2vw, 1.25rem);
  letter-spacing: 0.04em;
  color: var(--accent);
  line-height: 1.3;
  font-weight: 800;
}

.gentle {
  color: var(--muted);
  font-size: clamp(0.88rem, 2.6vw, 1.05rem);
  line-height: 1.45;
  margin-top: 0.3rem;
  font-weight: 600;
}

.gentle.tiny {
  font-size: 0.85rem;
  margin-top: 0.4rem;
  text-align: center;
}

.panel,
.home-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid rgba(125, 211, 252, 0.15);
  border-radius: var(--radius);
  padding: 0.75rem 0.8rem 0.85rem;
  box-shadow: 0 8px 24px var(--shadow);
}

.section-label {
  font-size: clamp(0.95rem, 2.8vw, 1.1rem);
  font-weight: 800;
  color: var(--warn);
  margin: 0.35rem 0 0.5rem;
  flex-shrink: 0;
}

.section-label.small {
  font-size: 0.85rem;
  margin: 0 0 0.35rem;
  color: var(--muted);
}

.photo-grid {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.75rem;
  padding: 0.15rem 0.1rem 0.5rem;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

@media (min-width: 700px) {
  .photo-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  }
}

.photo-card {
  position: relative;
  border-radius: 14px;
  overflow: hidden;
  border: 3px solid transparent;
  background: rgba(0, 0, 0, 0.25);
  min-height: 120px;
  aspect-ratio: 1;
  padding: 0;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  transition: border-color 0.15s, transform 0.12s, box-shadow 0.15s;
}

.photo-card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}

.photo-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(125, 211, 252, 0.35), 0 8px 20px rgba(0, 0, 0, 0.4);
}

.photo-card.selected::after {
  content: "✓ 已選";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 0.35rem;
  background: linear-gradient(transparent, rgba(12, 25, 41, 0.92));
  color: var(--good);
  font-weight: 800;
  font-size: 0.9rem;
  text-align: center;
}

.diff-bar {
  display: flex;
  gap: 0.55rem;
  flex-wrap: wrap;
  flex-shrink: 0;
  margin-bottom: 0.65rem;
}

.diff-btn {
  flex: 1;
  min-width: 100px;
  min-height: var(--touch);
  padding: 0.65rem 0.5rem;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--muted);
  border: 2px solid rgba(255, 255, 255, 0.1);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15rem;
}

.diff-btn .diff-title {
  font-size: clamp(1rem, 3vw, 1.15rem);
  font-weight: 800;
}

.diff-btn .diff-sub {
  font-size: 0.82rem;
  font-weight: 600;
  opacity: 0.9;
}

.diff-btn.active {
  background: rgba(125, 211, 252, 0.2);
  border-color: rgba(125, 211, 252, 0.55);
  color: var(--accent);
}

.btn-start {
  flex-shrink: 0;
  width: 100%;
  min-height: 72px;
  border-radius: 18px;
  font-size: clamp(1.25rem, 4vw, 1.55rem);
  font-weight: 900;
  letter-spacing: 0.06em;
  background: linear-gradient(180deg, #7dd3fc, #38bdf8);
  color: #0c1929;
  box-shadow: 0 8px 22px rgba(56, 189, 248, 0.35);
}

.btn-start:not(:disabled):active {
  filter: brightness(0.95);
}

.hint-line {
  text-align: center;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
  margin-top: 0.45rem;
  flex-shrink: 0;
  min-height: 1.3em;
}

/* —— 遊戲中 —— */
.game-header {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  margin-bottom: 0.4rem;
}

.game-title {
  font-size: clamp(0.9rem, 2.8vw, 1.1rem);
  color: var(--accent);
  font-weight: 800;
  letter-spacing: 0.03em;
}

.stats {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.stat {
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  padding: 0.35rem 0.65rem;
  min-width: 72px;
  text-align: center;
}

.stat-label {
  display: block;
  font-size: 0.72rem;
  color: var(--muted);
  font-weight: 700;
}

.stat-value {
  display: block;
  font-size: clamp(0.95rem, 2.8vw, 1.15rem);
  font-weight: 900;
  color: var(--good);
  font-variant-numeric: tabular-nums;
}

.game-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  background: var(--panel);
  border: 1px solid rgba(125, 211, 252, 0.15);
  border-radius: var(--radius);
  padding: 0.65rem;
  box-shadow: 0 8px 24px var(--shadow);
  overflow: hidden;
}

.ref-col {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
}

.ref-card {
  align-self: center;
  width: min(160px, 38vw);
  aspect-ratio: 1;
  border-radius: 12px;
  overflow: hidden;
  border: 2px solid rgba(125, 211, 252, 0.35);
  background: rgba(0, 0, 0, 0.3);
  padding: 0;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
}

.ref-card img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.play-col {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.board-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board {
  position: relative;
  width: min(100%, 520px);
  aspect-ratio: 1;
  max-height: 100%;
  background:
    linear-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.03) 1px, transparent 1px),
    rgba(0, 0, 0, 0.28);
  border: 2px solid rgba(125, 211, 252, 0.25);
  border-radius: 12px;
  box-shadow: inset 0 0 24px rgba(0, 0, 0, 0.35);
  overflow: hidden;
  touch-action: none;
}

.slot {
  position: absolute;
  border: 1px dashed rgba(155, 176, 196, 0.22);
  box-sizing: border-box;
  border-radius: 2px;
  transition: background 0.15s, box-shadow 0.15s;
}

.slot.hint-glow {
  background: rgba(251, 191, 36, 0.28);
  box-shadow: inset 0 0 0 3px rgba(251, 191, 36, 0.7);
  z-index: 2;
}

.slot.hover-ok {
  background: rgba(110, 231, 183, 0.18);
  box-shadow: inset 0 0 0 2px rgba(110, 231, 183, 0.55);
}

.piece {
  position: absolute;
  touch-action: none;
  cursor: grab;
  border-radius: 3px;
  box-shadow: var(--piece-shadow);
  background-repeat: no-repeat;
  /* background-size / position set in JS */
  z-index: 5;
  will-change: left, top, transform;
  border: 2px solid rgba(255, 255, 255, 0.35);
}

.piece:active,
.piece.dragging {
  cursor: grabbing;
  z-index: 100;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.55);
  transform: scale(1.04);
  border-color: var(--accent);
}

.piece.locked {
  cursor: default;
  box-shadow: 0 0 0 2px rgba(110, 231, 183, 0.85), 0 2px 8px rgba(0, 0, 0, 0.25);
  border-color: var(--good);
  z-index: 3;
  transform: none;
  animation: lock-pop 0.35s ease-out;
}

.piece.locked.dragging {
  transform: none;
}

@keyframes lock-pop {
  0% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

.tray {
  flex: 0 0 auto;
  min-height: 88px;
  max-height: 28vh;
  overflow-x: auto;
  overflow-y: hidden;
  display: flex;
  flex-wrap: nowrap;
  gap: 0.55rem;
  align-items: center;
  padding: 0.45rem 0.35rem;
  background: rgba(0, 0, 0, 0.22);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  -webkit-overflow-scrolling: touch;
  position: relative;
  touch-action: pan-x;
}

.tray .piece.in-tray {
  position: relative;
  left: auto !important;
  top: auto !important;
  flex: 0 0 auto;
  /* size set by JS via width/height */
}

.tray-empty-msg {
  width: 100%;
  text-align: center;
  color: var(--good);
  font-weight: 800;
  font-size: 0.95rem;
  padding: 0.5rem;
}

.game-actions {
  flex-shrink: 0;
  display: flex;
  gap: 0.5rem;
  margin-top: 0.5rem;
  flex-wrap: wrap;
}

.btn,
.action-btn {
  flex: 1;
  min-width: 100px;
  min-height: var(--touch);
  border-radius: 14px;
  font-weight: 800;
  font-size: clamp(0.95rem, 2.8vw, 1.05rem);
  padding: 0.55rem 0.65rem;
  background: linear-gradient(180deg, #7dd3fc, #38bdf8);
  color: #0c1929;
}

.action-btn.ghost,
.btn-ghost {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  border: 2px solid rgba(255, 255, 255, 0.12);
}

.btn-primary {
  background: linear-gradient(180deg, #7dd3fc, #38bdf8);
  color: #0c1929;
}

.btn-secondary {
  background: linear-gradient(180deg, #6ee7b7, #34d399);
  color: #0c1929;
}

.btn-warn {
  background: linear-gradient(180deg, #fbbf24, #f59e0b);
  color: #1a1200;
}

.btn.big {
  min-height: 68px;
  font-size: 1.15rem;
  border-radius: 16px;
  width: 100%;
}

/* —— 橫式 iPad：參考圖在側邊 —— */
@media (orientation: landscape) and (min-width: 700px) {
  .game-body {
    flex-direction: row;
    align-items: stretch;
  }

  .ref-col {
    width: 160px;
    flex: 0 0 160px;
  }

  .ref-card {
    width: 100%;
  }

  .play-col {
    flex: 1;
    min-width: 0;
  }

  .tray {
    max-height: 22vh;
  }
}

/* 小螢幕直式：參考圖更小橫排 */
@media (orientation: portrait) {
  .ref-col {
    flex-direction: row;
    align-items: center;
    gap: 0.65rem;
  }

  .ref-col .section-label {
    display: none;
  }

  .ref-card {
    width: 72px;
    min-width: 72px;
  }

  .gentle.tiny {
    flex: 1;
    text-align: left;
    margin-top: 0;
  }
}

/* —— Overlay —— */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(8, 14, 22, 0.78);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(1rem, var(--safe-t))
    max(1rem, var(--safe-r))
    max(1rem, var(--safe-b))
    max(1rem, var(--safe-l));
}

.overlay-card {
  width: min(480px, 100%);
  max-height: 100%;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid rgba(125, 211, 252, 0.25);
  border-radius: 20px;
  padding: 1.25rem 1.15rem 1.2rem;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
  text-align: center;
}

.win-card h2 {
  font-size: clamp(1.4rem, 5vw, 1.85rem);
  color: var(--good);
  font-weight: 900;
  margin-bottom: 0.4rem;
}

.win-emoji {
  font-size: 2.2rem;
  margin-bottom: 0.35rem;
  letter-spacing: 0.15em;
}

.win-msg {
  font-size: clamp(1.05rem, 3.5vw, 1.25rem);
  font-weight: 800;
  color: var(--accent);
  margin: 0.35rem 0 0.5rem;
  line-height: 1.4;
}

.win-photo {
  width: min(280px, 72vw);
  aspect-ratio: 1;
  object-fit: cover;
  border-radius: 14px;
  border: 3px solid rgba(125, 211, 252, 0.4);
  margin: 0.75rem auto;
  display: block;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  pointer-events: none;
}

.win-actions,
.confirm-actions {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  margin-top: 0.85rem;
}

.confirm-card h2 {
  font-size: 1.35rem;
  color: var(--warn);
  font-weight: 900;
  margin-bottom: 0.4rem;
}

.confirm-actions {
  flex-direction: row;
  gap: 0.65rem;
}

.confirm-actions .btn {
  flex: 1;
}

.ref-zoom-card img {
  width: 100%;
  max-height: min(70vh, 560px);
  object-fit: contain;
  border-radius: 12px;
  margin-bottom: 0.85rem;
  background: rgba(0, 0, 0, 0.3);
  pointer-events: none;
}

/* 拖曳時的幽靈層由 piece 本身移動 */
.drag-layer {
  position: fixed;
  inset: 0;
  z-index: 150;
  pointer-events: none;
}

/* 完成時拼圖板微光 */
.board.complete {
  box-shadow:
    inset 0 0 0 3px rgba(110, 231, 183, 0.7),
    0 0 28px rgba(110, 231, 183, 0.35);
}

/* —— 姓名大按鈕 / 排行榜 —— */
.top-actions {
  margin-top: 0.45rem;
  display: flex;
  justify-content: center;
  gap: 0.5rem;
}

.btn-chip {
  min-height: 48px;
  padding: 0.45rem 1rem;
  border-radius: 999px;
  font-weight: 800;
  font-size: 0.95rem;
  background: rgba(251, 191, 36, 0.15);
  color: var(--warn);
  border: 2px solid rgba(251, 191, 36, 0.45);
}

.player-chips,
.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.35rem;
}

.player-chips button,
.chip {
  min-height: var(--touch);
  min-width: 88px;
  flex: 1 1 auto;
  padding: 0.55rem 0.75rem;
  border-radius: 14px;
  font-weight: 800;
  font-size: clamp(0.95rem, 2.8vw, 1.08rem);
  background: rgba(255, 255, 255, 0.06);
  color: var(--muted);
  border: 2px solid rgba(255, 255, 255, 0.1);
}

.player-chips button.active,
.chip.active {
  background: rgba(125, 211, 252, 0.22);
  border-color: rgba(125, 211, 252, 0.55);
  color: var(--accent);
}

.current-player,
.lb-current {
  font-size: 0.95rem;
  font-weight: 800;
  color: var(--muted);
  margin: 0.15rem 0 0.55rem;
}

.current-player span,
.lb-current span,
#lb-current-name {
  color: var(--warn);
  font-weight: 900;
}

.game-title-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.play-player {
  font-size: 0.85rem;
  font-weight: 800;
  color: var(--warn);
  background: rgba(251, 191, 36, 0.12);
  border: 1px solid rgba(251, 191, 36, 0.35);
  border-radius: 999px;
  padding: 0.2rem 0.55rem;
}

.win-label {
  margin-top: 0.65rem;
  text-align: left;
}

.win-rank {
  font-size: clamp(1rem, 3.2vw, 1.15rem);
  font-weight: 800;
  color: var(--good);
  line-height: 1.45;
  margin: 0.35rem 0 0.25rem;
  min-height: 1.4em;
}

.win-card .player-chips {
  justify-content: center;
  margin-bottom: 0.5rem;
}

/* 排行榜卡片 */
.lb-card {
  width: min(560px, 100%);
  max-height: min(92dvh, 900px);
  display: flex;
  flex-direction: column;
  text-align: left;
  padding: 1rem 0.9rem 0.9rem;
}

.lb-card h2 {
  text-align: center;
  color: var(--warn);
  font-size: clamp(1.2rem, 4vw, 1.5rem);
  font-weight: 900;
  margin-bottom: 0.35rem;
}

.lb-current {
  text-align: center;
}

.lb-rank-banner {
  display: none;
  margin: 0.35rem 0 0.55rem;
  padding: 0.65rem 0.75rem;
  border-radius: 12px;
  background: rgba(110, 231, 183, 0.12);
  border: 2px solid rgba(110, 231, 183, 0.4);
  color: var(--text);
  font-weight: 800;
  font-size: 0.95rem;
  line-height: 1.45;
  text-align: center;
}

.lb-rank-banner.show {
  display: block;
}

.lb-rank-banner strong {
  color: var(--good);
  font-size: 1.2em;
}

.lb-table-wrap {
  flex: 1;
  min-height: 120px;
  max-height: 38vh;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0.45rem 0 0.55rem;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.lb-empty {
  text-align: center;
  color: var(--muted);
  font-weight: 700;
  padding: 1.25rem 0.75rem;
  font-size: 0.95rem;
  line-height: 1.45;
}

.lb-table {
  width: 100%;
  border-collapse: collapse;
  font-size: clamp(0.82rem, 2.4vw, 0.95rem);
}

.lb-table th,
.lb-table td {
  padding: 0.55rem 0.4rem;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.lb-table th {
  position: sticky;
  top: 0;
  background: #152535;
  color: var(--accent);
  font-weight: 800;
  z-index: 1;
}

.lb-table th.lb-sort-th {
  cursor: pointer;
  user-select: none;
}

.lb-table th.lb-sort-th.active {
  color: var(--warn);
}

.lb-sort-mark {
  margin-left: 0.15rem;
  font-size: 0.75em;
}

.lb-table tr.highlight-row td {
  background: rgba(125, 211, 252, 0.15);
  color: var(--accent);
  font-weight: 900;
}

.lb-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.lb-actions .action-btn {
  flex: 1 1 30%;
  min-height: 52px;
  font-size: 0.92rem;
}

.lb-actions .btn-primary {
  flex: 1 1 100%;
  margin-top: 0.15rem;
}

.btn-linkish {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  background: transparent;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 700;
  min-height: 40px;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

body.lb-open {
  overflow: hidden;
}
