:root {
  --bg: #3b0a0d;
  --panel: #5a1419;
  --line: #85303a;
  --text: #fff4e0;
  --muted: #e4b9a8;
  --accent: #ffd23f;
  --good: #3ddc84;
  --bad: #ff6b6b;
  --felt: #0f6b3f;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(circle at 50% 0%, #8a1a20, var(--bg) 70%) fixed;
  color: var(--text); font: 15px/1.4 "Be Vietnam Pro", "Plus Jakarta Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  user-select: none; -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
.ic { width: 1.15em; height: 1.15em; vertical-align: -0.2em; flex: none; }
button, input { font: inherit; color: inherit; }
button {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px;
  cursor: pointer; white-space: nowrap; display: inline-flex; align-items: center; gap: 4px; justify-content: center;
}
button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--accent); color: #3b1d00; border-color: var(--accent); font-weight: 700; }
input { background: #2a0709; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 24px;
  width: min(380px, calc(100vw - 32px)); display: grid; gap: 12px; text-align: center; box-shadow: 0 12px 40px #0008;
}
.card h1 { margin: 0; display: flex; align-items: center; justify-content: center; gap: 8px; }
.card h1 .logo { width: 40px; height: 40px; }
.card h1 .back { color: var(--muted); display: inline-flex; }
.card label { display: grid; gap: 4px; text-align: left; color: var(--muted); }
.sub, .muted { margin: 0; color: var(--muted); }
.muted { font-size: 12px; }
.join { display: flex; gap: 8px; }
#code { text-transform: uppercase; letter-spacing: 3px; text-align: center; }
#home { min-height: 100%; display: grid; place-items: center; padding: 16px; }

#room { min-height: 100%; display: flex; flex-direction: column; }
header { display: flex; align-items: center; gap: 6px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); }
header .grow { flex: 1; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--bad); flex: none; }
.dot.on { background: var(--good); }
.coins { font-weight: 800; color: var(--accent); white-space: nowrap; font-variant-numeric: tabular-nums; }
@media (max-width: 480px) {
  header { gap: 4px; padding-inline: 8px; }
  header button { padding: 6px 8px; }
  .lbl { display: none; }
}
/* ≤430px: thanh trên đủ chỗ cho cả công tắc Công khai — bỏ chữ cạnh công tắc (vẫn đọc được qua label), gọn khoảng cách. */
@media (max-width: 430px) {
  header { gap: 3px; padding-inline: 6px; }
  header button { padding: 6px 6px; }
  header label.pub-switch { font-size: 0; gap: 0; }
}
/* Thêm nút Cách chơi (i) trên thanh trên: ≤400px thu khoảng cách / đệm nút cho vẫn vừa 360px. */
@media (max-width: 400px) {
  header { gap: 2px; padding-inline: 4px; }
  header button { padding: 6px 4px; }
}
#players { list-style: none; margin: 4px 12px 0; padding: 0; display: flex; gap: 6px; flex-wrap: wrap; }
#players li {
  display: inline-flex; align-items: center; gap: 4px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 3px 10px; font-size: 13px; white-space: nowrap; box-shadow: inset 4px 0 var(--c);
}
#players li.me { border-color: var(--accent); }
#players li.dealer { background: #7a4a00; }
#players b { color: var(--accent); font-variant-numeric: tabular-nums; }
#players .d { font-weight: 800; }
.up { color: var(--good); } .down { color: var(--bad); }

main { flex: 1; display: grid; gap: 10px; justify-items: center; align-content: start; padding: 10px 12px max(12px, env(safe-area-inset-bottom)); }
#plate {
  position: relative; width: 190px; height: 110px; margin-top: 20px; border-radius: 50%; display: grid; place-items: center;
  background: radial-gradient(circle at 50% 40%, #fff, #e9dcc6 70%, #b99f7c); box-shadow: 0 8px 24px #0008, inset 0 -6px #0002;
}
#dice { display: flex; gap: 8px; }
.die {
  width: 48px; height: 48px; border-radius: 10px; background: #fff; display: grid; place-items: center; padding: 4px;
  box-shadow: 0 3px 0 #c9b79a, 0 4px 8px #0005; animation: pop .35s both;
}
.die:nth-child(2) { animation-delay: .1s; } .die:nth-child(3) { animation-delay: .2s; }
.die img { width: 100%; height: 100%; border-radius: 50%; }
.pic-fb {
  display: grid; place-items: center; aspect-ratio: 1; border-radius: 50%; overflow: hidden;
  background: #fff6e2; color: #5a0f12; font-weight: 800; font-size: 13px; text-align: center; line-height: 1.1;
}
.die .pic-fb { width: 100%; height: 100%; font-size: 10px; }
@keyframes pop { from { transform: scale(.3) rotate(-40deg); opacity: 0; } }
/* Nắp úp: vòm đỏ viền vàng + núm. */
#bowl {
  position: absolute; left: 22px; right: 22px; top: -14px; bottom: 26px; border-radius: 50% 50% 14px 14px / 100% 100% 14px 14px;
  background: radial-gradient(circle at 35% 25%, #ff8a7a, #c62a2f 55%, #7a1016); border: 4px solid #f3d27a;
  transition: transform .5s cubic-bezier(.5, -0.4, .6, 1), opacity .5s; filter: drop-shadow(0 8px 10px #0007);
}
#bowl::before { content: ''; position: absolute; left: 50%; top: -14px; width: 30px; height: 18px; translate: -50% 0; border-radius: 10px 10px 4px 4px; background: #f3d27a; }
#bowl.open { transform: translate(90px, -70px) rotate(35deg); opacity: 0; }
#bowl.shake { animation: shake .18s infinite; }
@keyframes shake { 0%, 100% { transform: translate(-6px, 0) rotate(-6deg); } 50% { transform: translate(6px, -4px) rotate(6deg); } }
#status { margin: 0; min-height: 1.4em; font-size: 14px; color: var(--muted); text-align: center; }
#status b { color: var(--accent); }

#board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; width: min(560px, 100%); }
.cell {
  position: relative; aspect-ratio: 1 / .9; border-radius: 16px; background: linear-gradient(160deg, #1a8a55, var(--felt));
  border: 3px solid #f3d27a; display: grid; place-items: center; align-content: center; gap: 2px; cursor: pointer;
  box-shadow: 0 4px 0 #0a4029; transition: transform .1s, box-shadow .2s; padding: 4px;
}
.cell:active { transform: translateY(2px); }
.cell .em { width: min(70%, 130px); aspect-ratio: 1; border-radius: 50%; box-shadow: 0 0 0 3px #f3d27a, 0 3px 8px #0006; }
.cell .nm { position: absolute; top: 4px; left: 8px; font-weight: 800; font-size: 13px; letter-spacing: .5px; text-shadow: 0 1px 2px #000a; }
.cell.hit { box-shadow: 0 0 0 4px var(--accent), 0 0 24px 6px #ffd23faa; animation: glow .6s 3; }
.cell.hit .x { position: absolute; top: 4px; right: 6px; background: var(--accent); color: #3b1d00; font-weight: 800; border-radius: 999px; padding: 0 8px; font-size: 13px; }
@keyframes glow { 50% { transform: scale(1.04); } }
.cell.miss { opacity: .55; }
.stack { position: absolute; left: 4px; right: 4px; bottom: 4px; display: flex; flex-wrap: wrap; gap: 2px; justify-content: center; pointer-events: none; }
.stack span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 11px; font-weight: 700; color: #1b1300; background: var(--c); border-radius: 999px; padding: 0 6px; box-shadow: 0 1px 2px #0006; }
.stack span.me { outline: 2px solid #fff; }

#bar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; justify-content: center; }
#chips { display: flex; gap: 6px; }
.chip {
  width: 48px; height: 48px; border-radius: 50%; padding: 0; font-weight: 800; font-size: 13px; color: #fff;
  background: var(--cc); border: 4px dashed #fff8; box-shadow: 0 3px 0 #0006;
}
.chip.on { transform: translateY(-4px); box-shadow: 0 0 0 3px var(--accent), 0 6px 0 #0006; }
#btnRoll { min-width: 110px; font-size: 16px; }
/* Điện thoại xoay ngang: đĩa bên trái, bàn + phỉnh bên phải, vừa một màn không phải cuộn. */
@media (orientation: landscape) and (max-height: 500px) {
  main { grid-template-columns: auto auto; grid-template-areas: "plate board" "status bar"; justify-content: center; align-items: center; column-gap: 24px; }
  #plate { grid-area: plate; margin-top: 0; }
  #status { grid-area: status; }
  #board { grid-area: board; width: min(560px, calc((100dvh - 190px) / .62)); gap: 6px; }
  #bar { grid-area: bar; }
  .chip { width: 42px; height: 42px; }
}
