:root {
  --bg: #2a2118;
  --panel: #3d3228;
  --text: #f5f0e8;
  --muted: #c4b8a8;
  --accent: #f0d9a8;
  --good: #6ee7b7;
  --warn: #fbbf24;
  --touch: 64px;
  --tile-w: 52px;
  --tile-h: 68px;
  --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);
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  min-height: 100dvh;
  font-family: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  background: #1a1510;
  color: var(--text);
  padding:
    max(0.4rem, var(--safe-t))
    max(0.5rem, var(--safe-r))
    max(0.4rem, var(--safe-b))
    max(0.5rem, var(--safe-l));
  user-select: none;
  touch-action: manipulation;
  overflow: hidden;
}
button { font: inherit; border: none; cursor: pointer; color: inherit; }
button:active:not(:disabled) { transform: scale(0.98); }
button:disabled { opacity: 0.45; }

.app {
  max-width: 900px;
  margin: 0 auto;
  height: calc(100dvh - 0.8rem);
  display: flex;
  flex-direction: column;
}
.screen { flex: 1; display: flex; flex-direction: column; gap: 0.4rem; min-height: 0; }
.hidden { display: none !important; }

.top { text-align: center; flex-shrink: 0; }
.top h1 {
  font-size: clamp(0.95rem, 3.2vw, 1.25rem);
  color: var(--accent);
  font-weight: 800;
}
.gentle { color: var(--muted); font-weight: 600; margin-top: 0.25rem; font-size: 0.95rem; }
.top-actions { display: flex; gap: 0.45rem; justify-content: center; margin-top: 0.4rem; flex-wrap: wrap; }
.btn-chip {
  min-height: 44px; padding: 0.35rem 0.9rem; border-radius: 999px;
  background: rgba(240, 217, 168, 0.12); border: 1px solid rgba(240, 217, 168, 0.4);
  color: var(--accent); font-weight: 800;
}
.panel {
  flex: 1; background: var(--panel); border-radius: 16px; padding: 0.85rem;
  border: 1px solid rgba(240, 217, 168, 0.2); overflow-y: auto;
}
.label { color: var(--warn); font-weight: 800; margin: 0.35rem 0 0.4rem; }
.chips { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.chip, .chips button {
  min-height: var(--touch); min-width: 5rem; padding: 0.5rem 0.85rem; border-radius: 16px;
  background: rgba(0, 0, 0, 0.3); border: 3px solid rgba(196, 184, 168, 0.4); font-weight: 900; font-size: 1.02rem;
}
.chip.active, .chips button.active {
  border-color: var(--warn); background: rgba(251, 191, 36, 0.28); color: #fde68a;
  box-shadow: 0 0 0 3px rgba(251, 191, 36, 0.35);
}
.current { margin-top: 0.45rem; font-weight: 800; font-size: 1.05rem; }
.current span { color: var(--warn); }
.hint { color: var(--muted); font-weight: 600; margin: 0.45rem 0; line-height: 1.45; font-size: 0.95rem; }
.btn-start, .btn.big {
  min-height: 68px; width: 100%; margin-top: 0.6rem; border-radius: 16px;
  font-size: 1.15rem; font-weight: 900;
}
.btn-start, .btn.primary {
  background: linear-gradient(180deg, #e8c48a, #b8860b); color: #2a1a08;
}
.btn.secondary {
  background: linear-gradient(180deg, #7dd3fc, #0ea5e9); color: #082f49;
  width: 100%; min-height: 60px; border-radius: 14px; font-weight: 900; margin-top: 0.35rem;
}
.btn.ghost {
  background: rgba(255, 255, 255, 0.08); width: 100%; min-height: 52px;
  border-radius: 14px; font-weight: 800; margin-top: 0.3rem;
}
.btn {
  min-height: 52px; border-radius: 14px; font-weight: 900; padding: 0 0.65rem;
  background: rgba(255, 255, 255, 0.1);
}

.game-head {
  flex-shrink: 0;
  background: rgba(40, 32, 24, 0.92);
  border-radius: 14px;
  padding: 0.5rem 0.7rem;
  border: 1px solid rgba(240, 217, 168, 0.25);
}
.row-between { display: flex; justify-content: space-between; align-items: center; font-weight: 900; }
#play-player { color: var(--warn); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.3rem; margin-top: 0.35rem; }
.stat { background: rgba(0, 0, 0, 0.25); border-radius: 10px; text-align: center; padding: 0.3rem; }
.sl { display: block; font-size: 0.7rem; color: var(--muted); font-weight: 700; }
.sv { font-weight: 900; font-size: 1rem; }
.toast { min-height: 1.25em; text-align: center; color: var(--good); font-weight: 800; margin-top: 0.25rem; font-size: 0.92rem; }

/* 木桌 */
.table-wrap {
  flex: 1;
  min-height: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 16px;
  border: 3px solid #8b6914;
  box-shadow: inset 0 0 40px rgba(0, 0, 0, 0.25);
}
.table-wood {
  min-width: 100%;
  min-height: 100%;
  background:
    repeating-linear-gradient(
      90deg,
      #d2b48c 0px,
      #c4a574 8px,
      #b8956a 16px,
      #c9ab7a 24px,
      #d4b896 32px
    );
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 1.4rem 1rem 1.7rem;
}
.mj-stage {
  position: relative;
  /* JS 會設定 width/height */
  margin: 0 auto;
}

/* 立體麻將牌 */
.tile {
  position: absolute;
  width: var(--tile-w);
  height: var(--tile-h);
  padding: 0;
  border: none;
  border-radius: 8px;
  background: linear-gradient(165deg, #ffffff 0%, #f8f4ec 50%, #ebe4d4 100%);
  box-shadow:
    inset 0 1px 0 #fff,
    inset 0 -2px 0 rgba(160, 140, 100, 0.25),
    2px 2px 0 #c9b896,
    3px 4px 0 #9a8560,
    4px 6px 10px rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s, filter 0.12s, box-shadow 0.12s;
  -webkit-tap-highlight-color: transparent;
}
.tile::after {
  content: "";
  position: absolute;
  inset: 3px;
  border-radius: 5px;
  border: 1.5px solid rgba(120, 90, 40, 0.35);
  pointer-events: none;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.35);
}
.tile-face {
  position: relative;
  z-index: 1;
  width: 90%;
  height: 92%;
  display: flex;
  align-items: center;
  justify-content: center;
  filter: contrast(1.12) saturate(1.08);
}
.tile-svg {
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
  filter: drop-shadow(0 1px 0 rgba(255,255,255,0.65)) drop-shadow(0 0 0.6px rgba(15,23,42,0.55));
}

.tile.blocked {
  filter: brightness(0.88);
  cursor: default;
}
.tile.free {
  cursor: pointer;
}
.tile.free:active {
  transform: translate(-1px, -2px) scale(1.03);
}
.tile.selected {
  z-index: 500 !important;
  transform: translate(-2px, -6px) scale(1.06);
  box-shadow:
    inset 0 1px 0 #fff,
    0 0 0 4px #1d4ed8,
    0 0 0 7px rgba(251, 191, 36, 0.55),
    5px 10px 16px rgba(0, 0, 0, 0.45);
  filter: brightness(1.05);
}
.tile.hint {
  z-index: 400 !important;
  box-shadow:
    inset 0 1px 0 #fff,
    0 0 0 3px #fbbf24,
    0 0 18px rgba(251, 191, 36, 0.75),
    4px 6px 10px rgba(0, 0, 0, 0.35);
  animation: hint-pulse 1s ease-in-out infinite;
}
.tile.gone {
  display: none !important;
}
@keyframes hint-pulse {
  50% { filter: brightness(1.1); }
}

.actions {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.35rem;
}

.overlay {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(20, 14, 8, 0.78);
  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.65rem, var(--safe-l));
  overflow-y: auto;
}
.card {
  width: min(520px, 100%);
  background: var(--panel);
  border-radius: 18px;
  padding: 1rem;
  border: 1px solid rgba(240, 217, 168, 0.3);
  max-height: 92dvh;
  overflow-y: auto;
}
.card h2 { text-align: center; color: var(--warn); margin-bottom: 0.5rem; }
.big { text-align: center; font-size: 2rem; }
.win-rank { text-align: center; color: var(--good); font-weight: 800; margin: 0.4rem 0; }
.lb-cur { text-align: center; color: var(--muted); font-weight: 700; }
.lb-table { margin: 0.6rem 0; max-height: 40vh; overflow-y: auto; }
.lb-table table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.lb-table th, .lb-table td {
  padding: 0.45rem 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); }
.lb-table tr.me td { color: var(--warn); }
ol { margin: 0.75rem 0 0.75rem 1.2rem; line-height: 1.65; font-weight: 700; }

@media (min-width: 600px) {
  :root { --tile-w: 60px; --tile-h: 78px; }
}
@media (min-width: 900px) {
  :root { --tile-w: 64px; --tile-h: 84px; }
}
@media (max-width: 380px) {
  :root { --tile-w: 44px; --tile-h: 58px; }
  .actions { grid-template-columns: 1fr; }
}


/* batch4 residual — clearer tile faces (no photo textures) */
.tile {
  border: 1px solid rgba(90, 70, 40, 0.28);
}
html.elder-mode .tile-face {
  filter: contrast(1.18) saturate(1.1);
}
