:root {
  --side: 300px; --bw: 360px;
  --gold: #e9c47e; --gold2: #f7dda3; --ink: #f6ead2; --muted: #c9b392;
  --red: #c8301f; --lacquer: #5a1d12; --wood: #8b5a2b;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none;
  background: #1c0d07 radial-gradient(ellipse at 50% 18%, #5a2a17 0%, #34170d 45%, #170905 100%) fixed;
  color: var(--ink); font-family: system-ui, "Segoe UI", Roboto, Arial, sans-serif;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: manipulation;
}
body::before {   /* hoa văn mây mờ */
  content: ""; position: fixed; inset: 0; pointer-events: none; opacity: .07;
  background-image:
    radial-gradient(circle at 20% 30%, transparent 18px, #e9c47e 19px, transparent 20px),
    radial-gradient(circle at 70% 70%, transparent 18px, #e9c47e 19px, transparent 20px);
  background-size: 90px 90px;
}
button { font-family: inherit; cursor: pointer; color: inherit; }
button:disabled { opacity: .4; cursor: default; }
.hidden { display: none !important; }

#app {
  position: relative; height: 100%; display: grid; gap: 6px;
  grid-template-columns: 1fr; grid-template-rows: auto auto auto; grid-template-areas: "top" "stage" "panel"; align-content: center;
  padding: calc(6px + env(safe-area-inset-top)) calc(7px + env(safe-area-inset-right)) calc(8px + env(safe-area-inset-bottom)) calc(7px + env(safe-area-inset-left));
}
#app > #top, #app > #panel { width: min(100%, max(var(--bw), 340px)); justify-self: center; }
#app.land {
  grid-template-columns: auto var(--side); grid-template-rows: auto 1fr; grid-template-areas: "stage top" "stage panel"; align-content: stretch;
  justify-content: center; column-gap: 20px; row-gap: 10px; padding-top: calc(10px + env(safe-area-inset-top)); padding-bottom: calc(10px + env(safe-area-inset-bottom));
}
#app.land > #top, #app.land > #panel { width: var(--side); }

/* ---------- đầu trang ---------- */
#top { grid-area: top; display: flex; align-items: center; justify-content: space-between; gap: 10px; min-width: 0; }
.brand { display: flex; align-items: center; gap: 9px; min-width: 0; overflow: hidden; }
.logo {
  flex: none; width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center;
  font: bold 21px "KaiTi", "STKaiti", "SimSun", "Microsoft YaHei", serif; color: #b4231a;
  background: radial-gradient(circle at 35% 30%, #fff3d6, #f0cf95 55%, #c99350);
  box-shadow: 0 3px 0 #7d4f22, 0 5px 10px rgba(0, 0, 0, .5), inset 0 0 0 3px #f6dfae, inset 0 0 0 5px #b4231a;
}
#top h1 {
  min-width: 0; overflow: hidden; text-overflow: ellipsis;
  margin: 0; font: 700 26px/1.1 "Times New Roman", Georgia, serif; letter-spacing: .5px; white-space: nowrap;
  color: var(--gold2); text-shadow: 0 2px 0 #3a170b, 0 0 18px rgba(233, 196, 126, .25);
}
.stats { display: flex; gap: 6px; flex: none; }
.stat {
  background: linear-gradient(#3e1d10, #2a120a); border: 1px solid rgba(233, 196, 126, .28); border-radius: 10px;
  padding: 3px 11px; min-width: 64px; text-align: center; box-shadow: inset 0 1px 0 rgba(255, 255, 255, .06);
}
.stat .label { display: block; font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.stat b { font-size: 15px; color: var(--gold2); white-space: nowrap; }

/* ---------- bàn cờ ---------- */
#stage { grid-area: stage; position: relative; display: flex; align-items: center; justify-content: center; min-height: 0; min-width: 0; }
#cv {
  display: block; touch-action: none; border-radius: 10px;
  box-shadow: 0 0 0 4px #4a2312, 0 0 0 6px #b98a4c, 0 0 0 9px #3a1a0c, 0 18px 40px rgba(0, 0, 0, .65);
}
#banner {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%) scale(.4); opacity: 0; pointer-events: none;
  font: 800 clamp(26px, 7vmin, 52px)/1 "Times New Roman", Georgia, serif; letter-spacing: 1px; white-space: nowrap;
  color: #fff4dc; padding: .28em .8em; border-radius: 12px;
  background: linear-gradient(#d8402c, #8f1b10); border: 3px solid #f4cf86;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .55), inset 0 0 0 2px rgba(0, 0, 0, .25);
  text-shadow: 0 2px 0 #5e0d06;
}
#banner.show { animation: stamp 1.35s cubic-bezier(.2, .9, .3, 1.2) forwards; }
@keyframes stamp {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(2.2) rotate(-6deg); }
  14% { opacity: 1; transform: translate(-50%, -50%) scale(.95) rotate(-4deg); }
  22% { transform: translate(-50%, -50%) scale(1.04) rotate(-4deg); }
  75% { opacity: 1; transform: translate(-50%, -50%) scale(1) rotate(-4deg); }
  100% { opacity: 0; transform: translate(-50%, -60%) scale(1) rotate(-4deg); }
}
#toast {
  position: absolute; left: 50%; top: 10px; transform: translate(-50%, -8px); opacity: 0; pointer-events: none;
  background: rgba(30, 12, 6, .92); color: #ffe9c2; border: 1px solid rgba(233, 196, 126, .5);
  padding: 7px 14px; border-radius: 999px; font-size: 13.5px; font-weight: 600; transition: .2s; white-space: nowrap; max-width: 94%;
  overflow: hidden; text-overflow: ellipsis;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- bảng bên ---------- */
#panel { grid-area: panel; display: flex; flex-direction: column; gap: 7px; min-height: 0; }
#status {
  display: flex; align-items: center; gap: 9px; padding: 8px 12px; border-radius: 11px; min-height: 38px;
  background: linear-gradient(#3e1d10, #2a120a); border: 1px solid rgba(233, 196, 126, .25); font-size: 14.5px; font-weight: 600;
}
#status .dot {
  flex: none; width: 16px; height: 16px; border-radius: 50%; background: #c8301f;
  box-shadow: inset 0 0 0 2px #f2d39c, 0 0 0 1px rgba(0, 0, 0, .4);
}
#status.black .dot { background: #1f1a17; }
#status.check { border-color: #e0533d; background: linear-gradient(#5a1a10, #3a0f08); }
#status-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#status .dots { display: none; gap: 3px; flex: none; }
#status.thinking .dots { display: inline-flex; }
#status .dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--gold); animation: blink 1s infinite; }
#status .dots i:nth-child(2) { animation-delay: .15s; }
#status .dots i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 100% { opacity: .2; transform: translateY(0); } 50% { opacity: 1; transform: translateY(-3px); } }

#moves {
  list-style: none; margin: 0; padding: 4px 6px; display: flex; gap: 4px; overflow-x: auto; overflow-y: hidden;
  background: rgba(0, 0, 0, .22); border: 1px solid rgba(233, 196, 126, .14); border-radius: 10px; min-height: 32px;
  scrollbar-width: thin; scrollbar-color: rgba(233, 196, 126, .3) transparent; font-size: 13px;
}
#moves li { flex: none; display: flex; align-items: center; gap: 5px; padding: 2px 6px; border-radius: 6px; white-space: nowrap; }
#moves li:last-child { background: rgba(233, 196, 126, .12); }
#moves li i { font-style: normal; color: var(--muted); font-size: 11.5px; }
#moves .r { color: #ff9a85; font-weight: 700; }
#moves .b { color: #e8e2d6; font-weight: 700; }
#moves .chk::after { content: "+"; color: #ffd36b; margin-left: 1px; }
#moves .empty { color: var(--muted); font-size: 12.5px; background: none !important; }

#btns { display: grid; grid-template-columns: repeat(6, 1fr); gap: 5px; }
#btns button {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; min-width: 0;
  padding: 6px 2px 5px; border-radius: 11px; font-size: 11.5px; font-weight: 700; color: var(--gold2);
  background: linear-gradient(#6b3a1c, #4a2410); border: 1px solid rgba(233, 196, 126, .35);
  box-shadow: 0 3px 0 #2a1208, inset 0 1px 0 rgba(255, 255, 255, .12); transition: transform .08s, filter .15s;
}
#btns button:hover:not(:disabled) { filter: brightness(1.15); }
#btns button:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 #2a1208; }
#btns .ic { font-size: 19px; line-height: 1.1; font-family: system-ui, "Segoe UI", Roboto, Arial, "Microsoft YaHei", sans-serif; }
#btns .tx { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

#app.land #panel { gap: 10px; }
#app.land #moves {
  flex: 1; flex-direction: column; flex-wrap: nowrap; overflow-x: hidden; overflow-y: auto; gap: 0; padding: 6px; min-height: 60px;
  display: grid; grid-template-columns: 1fr; align-content: start;
}
#app.land #moves li { display: grid; grid-template-columns: 30px 1fr 1fr; padding: 3px 6px; }
#app.land #moves li:nth-child(odd) { background: rgba(255, 255, 255, .03); }
#app.land #moves li:last-child { background: rgba(233, 196, 126, .14); }
#app.land #btns { grid-template-columns: repeat(3, 1fr); gap: 7px; }
#app.land #btns button { flex-direction: row; gap: 6px; padding: 9px 4px; font-size: 13px; }
#app.land #btns .ic { font-size: 17px; }
#app.land #top h1 { font-size: 28px; }

/* ---------- hộp thoại ---------- */
.modal {
  position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  padding: calc(12px + env(safe-area-inset-top)) 12px calc(12px + env(safe-area-inset-bottom));
  background: rgba(15, 6, 3, .62); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); animation: fade .2s;
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.card {
  position: relative; width: min(420px, 100%); max-height: 100%; overflow: auto; text-align: center;
  padding: 18px 18px 16px; border-radius: 18px; color: var(--ink);
  background: linear-gradient(170deg, #4b2212 0%, #2c130a 70%);
  border: 1px solid rgba(233, 196, 126, .45);
  box-shadow: 0 24px 60px rgba(0, 0, 0, .65), inset 0 0 0 4px rgba(0, 0, 0, .18), inset 0 0 0 5px rgba(233, 196, 126, .18);
  animation: pop .28s cubic-bezier(.2, .9, .3, 1.25);
}
@keyframes pop { from { transform: scale(.88); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.card h2 { margin: 4px 0 2px; font: 700 30px/1.15 "Times New Roman", Georgia, serif; color: var(--gold2); text-shadow: 0 2px 0 #2a0f05; }
.card .sub { margin: 0 0 12px; color: var(--muted); font-size: 13.5px; font-style: italic; font-family: "Times New Roman", Georgia, serif; }
.menu-logo { display: flex; justify-content: center; gap: 0; margin-top: 2px; height: 66px; }
.disc {
  width: 62px; height: 62px; border-radius: 50%; display: grid; place-items: center;
  font: bold 34px "KaiTi", "STKaiti", "SimSun", "Microsoft YaHei", serif;
  background: radial-gradient(circle at 35% 30%, #fff3d6, #f0cf95 55%, #c99350);
  box-shadow: 0 4px 0 #7d4f22, 0 8px 16px rgba(0, 0, 0, .5), inset 0 0 0 4px #f6dfae;
}
.disc.r { color: #b4231a; box-shadow: 0 4px 0 #7d4f22, 0 8px 16px rgba(0, 0, 0, .5), inset 0 0 0 4px #f6dfae, inset 0 0 0 6px #b4231a; transform: rotate(-10deg) translateX(8px); animation: bob 3s ease-in-out infinite; }
.disc.b { color: #1f1a17; box-shadow: 0 4px 0 #7d4f22, 0 8px 16px rgba(0, 0, 0, .5), inset 0 0 0 4px #f6dfae, inset 0 0 0 6px #1f1a17; transform: rotate(10deg) translateX(-8px); animation: bob 3s ease-in-out infinite .8s; }
@keyframes bob { 50% { translate: 0 -4px; } }

.opt { margin: 9px 0; text-align: left; transition: opacity .2s; }
.opt.off { opacity: .35; pointer-events: none; }
.opt-label { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; margin: 0 0 5px 2px; font-weight: 700; }
.seg { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; }
.seg.seg4 { grid-template-columns: repeat(4, 1fr); }
.seg button {
  display: flex; align-items: center; justify-content: center; gap: 6px; flex-wrap: wrap; min-width: 0;
  padding: 9px 6px; border-radius: 11px; font-size: 14px; font-weight: 700; color: var(--ink);
  background: rgba(0, 0, 0, .25); border: 1px solid rgba(233, 196, 126, .22); transition: .15s;
}
.seg button:hover { background: rgba(233, 196, 126, .1); }
.seg button.on {
  background: linear-gradient(#f3d394, #d9a65a); color: #3a1608; border-color: #ffe7b3;
  box-shadow: 0 3px 10px rgba(233, 196, 126, .25), inset 0 1px 0 rgba(255, 255, 255, .5);
}
.seg4 button { flex-direction: column; gap: 0; padding: 7px 2px; }
.seg small { font-size: 11px; font-weight: 800; color: #ffd36b; }
.seg button.on small { color: #8c1e0f; }
.seg .e { font-size: 16px; }
.pc {
  font-style: normal; width: 24px; height: 24px; border-radius: 50%; display: inline-grid; place-items: center; flex: none;
  font: bold 14px "KaiTi", "SimSun", "Microsoft YaHei", serif; background: radial-gradient(circle at 35% 30%, #fff3d6, #e6be7d);
  box-shadow: 0 2px 0 #7d4f22, inset 0 0 0 2px #f6dfae;
}
.pc.r { color: #b4231a; box-shadow: 0 2px 0 #7d4f22, inset 0 0 0 2px #f6dfae, inset 0 0 0 3px #b4231a; }
.pc.b { color: #1f1a17; box-shadow: 0 2px 0 #7d4f22, inset 0 0 0 2px #f6dfae, inset 0 0 0 3px #1f1a17; }
.card .hint { margin: 12px 4px 4px; font-size: 12.5px; color: var(--muted); line-height: 1.45; }
.menu-btns, .res-btns { display: flex; gap: 8px; justify-content: center; margin-top: 12px; flex-wrap: wrap; }
.menu-btns button, .res-btns button {
  flex: 1 1 0; min-width: 110px; padding: 12px 16px; border-radius: 13px; font-size: 16px; font-weight: 800;
  border: 1px solid rgba(233, 196, 126, .4); transition: transform .08s, filter .15s;
}
.res-btns button { min-width: 92px; font-size: 14.5px; padding: 11px 10px; }
.primary { background: linear-gradient(#e2432c, #a3200f); color: #fff6e3 !important; border-color: #f4cf86 !important; box-shadow: 0 4px 0 #5e0d06, 0 8px 18px rgba(200, 40, 20, .35); text-shadow: 0 1px 0 #5e0d06; }
.ghost { background: rgba(0, 0, 0, .28); color: var(--gold2) !important; box-shadow: 0 3px 0 rgba(0, 0, 0, .35); }
.menu-btns button:hover, .res-btns button:hover { filter: brightness(1.12); }
.menu-btns button:active, .res-btns button:active { transform: translateY(2px); }
.menu-foot { margin-top: 10px; display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.menu-foot .gnx-btn { margin-top: 0; }

#res-card { padding-top: 26px; }
.res-seal {
  width: 74px; height: 74px; margin: 0 auto 6px; border-radius: 50%; display: grid; place-items: center;
  font: bold 40px "KaiTi", "STKaiti", "SimSun", "Microsoft YaHei", serif; color: #fff3d6;
  background: radial-gradient(circle at 35% 30%, #e8513a, #9b1d0e); border: 3px solid #f4cf86;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .5); animation: seal .5s cubic-bezier(.2, .9, .3, 1.4);
}
@keyframes seal { from { transform: scale(2) rotate(-25deg); opacity: 0; } to { transform: scale(1) rotate(-8deg); opacity: 1; } }
#result.win .res-seal::before { content: "勝"; }
#result.lose .res-seal { background: radial-gradient(circle at 35% 30%, #6f6a66, #2b2622); }
#result.lose .res-seal::before { content: "負"; }
#result.draw .res-seal { background: radial-gradient(circle at 35% 30%, #c79a4c, #7b5320); }
#result.draw .res-seal::before { content: "和"; }
#res-reason { margin: 4px 0 2px; font-size: 15px; color: #ffe0b0; font-weight: 600; }
#res-info { margin: 0 0 6px; font-size: 13px; color: var(--muted); }
#res-pts { margin: 8px 0 2px; font-size: 14px; line-height: 1.5; }
#res-pts b { color: #ffd36b; font-size: 1.1em; }

/* ---------- màn hình nhỏ ---------- */
@media (max-width: 380px) {
  #top h1 { font-size: 22px; }
  .logo { width: 32px; height: 32px; font-size: 18px; }
  .stat { padding: 2px 8px; min-width: 54px; }
  .seg button { font-size: 13px; }
  #btns .tx { font-size: 10.5px; }
}
@media (max-height: 700px) {
  .card { padding: 12px 14px; }
  .menu-logo { height: 48px; }
  .disc { width: 46px; height: 46px; font-size: 25px; }
  .card h2 { font-size: 25px; }
  .card .sub { margin-bottom: 6px; }
  .opt { margin: 6px 0; }
  .seg button { padding: 7px 5px; }
  .card .hint { margin: 8px 4px 2px; font-size: 12px; }
  .menu-btns button { padding: 10px 12px; }
  .menu-foot { margin-top: 8px; }
}
@media (max-height: 520px) {
  .card { padding: 10px 14px; }
  .menu-logo { display: none; }
  .card h2 { font-size: 22px; }
  .card .sub { margin-bottom: 4px; }
  .opt { margin: 5px 0; }
  .seg button { padding: 6px; }
  .card .hint { display: none; }
  .menu-btns { margin-top: 6px; }
  .menu-btns button { padding: 9px; }
  #app.land #top h1 { font-size: 19px; }
  #app.land .logo { display: none; }
  #app.land .stat { min-width: 48px; padding: 1px 7px; }
  #app.land .stat .label { font-size: 9px; }
  #app.land .stat b { font-size: 13px; }
  #app.land #btns button { padding: 6px 3px; font-size: 12px; }
}
@media (prefers-reduced-motion: reduce) {
  .disc.r, .disc.b { animation: none; }
}
