:root {
  --bg: #1c1611; --panel: #2a2019; --panel2: #352920; --line: rgba(255, 233, 200, .13);
  --text: #f6ecdc; --muted: #c4b294; --gold: #ffcf6b; --gold2: #f0a93a;
  --x: #e5533f; --o: #4b8de6;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; color: var(--text);
  font-family: system-ui, "Segoe UI", Roboto, Arial, sans-serif;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  background: var(--bg);
}
body {
  background:
    radial-gradient(1200px 700px at 20% -10%, rgba(255, 190, 110, .13), transparent 60%),
    radial-gradient(900px 600px at 110% 110%, rgba(120, 70, 30, .25), transparent 60%),
    var(--bg);
}
button { font-family: inherit; cursor: pointer; color: inherit; }
.hidden { display: none !important; }

/* ---------- bố cục: dọc (điện thoại) ---------- */
#app {
  position: fixed; inset: 0; display: grid; touch-action: none;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) auto auto auto auto auto minmax(0, 1fr);
  grid-template-areas: "." "top" "match" "stage" "controls" "tally" ".";
  gap: 6px;
  padding: calc(6px + env(safe-area-inset-top)) calc(8px + env(safe-area-inset-right)) calc(6px + env(safe-area-inset-bottom)) calc(8px + env(safe-area-inset-left));
  max-width: 1240px; margin: 0 auto;
}
#top { grid-area: top; display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.brand h1 { margin: 0; font-size: 22px; letter-spacing: .3px; white-space: nowrap; color: var(--gold); text-shadow: 0 2px 0 rgba(0, 0, 0, .35); }
.logo { position: relative; width: 38px; height: 28px; flex: none; }
.logo .pc { position: absolute; top: 2px; transform: scale(.85); }
.logo .pc.p1 { left: -2px; } .logo .pc.p2 { left: 14px; }
.pts { display: flex; align-items: center; gap: 5px; background: rgba(255, 207, 107, .12); border: 1px solid rgba(255, 207, 107, .35); border-radius: 999px; padding: 4px 12px; font-size: 15px; flex: none; }
.pts .star { color: var(--gold); }
.pts b { font-variant-numeric: tabular-nums; }
.pts.bump { animation: bump .6s ease; }
@keyframes bump { 30% { transform: scale(1.18); background: rgba(255, 207, 107, .3); } }
button.icon { width: 38px; height: 38px; border-radius: 11px; border: 1px solid var(--line); background: rgba(255, 255, 255, .06); font-size: 17px; flex: none; }
button.icon:hover { background: rgba(255, 255, 255, .12); }

#match { grid-area: match; display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); align-items: center; gap: 6px; }
.pl { display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-radius: 13px; border: 1px solid var(--line); background: rgba(255, 255, 255, .04); min-width: 0; transition: background .25s, border-color .25s, box-shadow .25s, opacity .25s; opacity: .7; }
#pl2 { flex-direction: row-reverse; text-align: right; }
.pl.active { opacity: 1; background: rgba(255, 207, 107, .12); border-color: rgba(255, 207, 107, .55); box-shadow: 0 0 0 3px rgba(255, 207, 107, .1), 0 6px 18px rgba(0, 0, 0, .25); }
.pl.winner { opacity: 1; background: rgba(120, 220, 120, .14); border-color: rgba(140, 230, 140, .6); }
.pl-t { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.pl-t b { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pl-t small { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pl.thinking .pc { animation: think 1s ease-in-out infinite; }
@keyframes think { 50% { transform: translateY(-3px) scale(1.08); } }
#status { text-align: center; font-size: 13.5px; font-weight: 700; color: var(--gold); padding: 0 4px; white-space: nowrap; }
#status.win { color: #8fe08f; } #status.lose { color: #ff8a7a; }
.dots::after { content: ''; display: inline-block; width: 1.2em; text-align: left; animation: dots 1.2s steps(1) infinite; }
@keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }

/* biểu tượng quân (X/O hoặc đá đen/trắng) */
.pc { position: relative; display: inline-block; width: 26px; height: 26px; flex: none; }
.pc::before, .pc::after { content: ''; position: absolute; left: 50%; top: 50%; }
.paper .pc.p1::before, .paper .pc.p1::after { width: 24px; height: 4.5px; background: var(--x); border-radius: 3px; transform: translate(-50%, -50%) rotate(45deg); }
.paper .pc.p1::after { transform: translate(-50%, -50%) rotate(-45deg); }
.paper .pc.p2::before { width: 19px; height: 19px; border: 4.5px solid var(--o); border-radius: 50%; transform: translate(-50%, -50%); box-sizing: border-box; }
.paper .pc.p2::after { display: none; }
.wood .pc::after { display: none; }
.wood .pc::before { width: 22px; height: 22px; border-radius: 50%; transform: translate(-50%, -50%); box-shadow: 1px 2px 3px rgba(0, 0, 0, .45); }
.wood .pc.p1::before { background: radial-gradient(circle at 35% 32%, #777 0, #222 45%, #000 100%); }
.wood .pc.p2::before { background: radial-gradient(circle at 35% 32%, #fff 0, #ececec 50%, #b9b9b9 100%); }

/* ---------- bàn cờ ---------- */
#stage { grid-area: stage; position: relative; min-height: 0; min-width: 0; margin: 4px 0; }
#board-wrap { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); }
#cv { display: block; border-radius: 12px; touch-action: none; box-shadow: 0 14px 40px rgba(0, 0, 0, .5), 0 2px 0 rgba(255, 255, 255, .06) inset; cursor: pointer; }
#toast {
  position: absolute; left: 50%; top: 12px; transform: translate(-50%, -8px); opacity: 0; pointer-events: none;
  background: rgba(30, 22, 16, .92); color: #fff; border: 1px solid rgba(255, 207, 107, .45); border-radius: 999px;
  padding: 7px 16px; font-size: 14px; font-weight: 600; white-space: nowrap; transition: opacity .2s, transform .2s; box-shadow: 0 6px 20px rgba(0, 0, 0, .4);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- nút ---------- */
#controls { grid-area: controls; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
#controls button {
  position: relative; display: flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; padding: 6px 4px;
  border-radius: 12px; border: 1px solid var(--line); background: linear-gradient(180deg, rgba(255, 255, 255, .09), rgba(255, 255, 255, .03));
  font-size: 14px; font-weight: 700; white-space: nowrap; transition: transform .08s, background .2s;
}
#controls button:hover { background: rgba(255, 255, 255, .13); }
#controls button:active { transform: scale(.96); }
#controls button:disabled { opacity: .4; cursor: default; }
#controls .ic { font-size: 18px; line-height: 1; }
#controls em { font-style: normal; font-size: 11px; min-width: 18px; height: 18px; line-height: 18px; border-radius: 9px; background: var(--gold); color: #2a1800; padding: 0 4px; }
#btn-new.pulse { border-color: rgba(255, 207, 107, .7); animation: glow 1.4s ease-in-out infinite; }
@keyframes glow { 50% { box-shadow: 0 0 0 4px rgba(255, 207, 107, .18); } }
#tally { grid-area: tally; text-align: center; font-size: 12.5px; color: var(--muted); display: flex; flex-direction: column; gap: 2px; min-height: 16px; }
#tally .keys { display: none; opacity: .65; font-size: 11.5px; }
@media (hover: hover) and (pointer: fine) { #tally .keys { display: block; } }

@media (max-width: 380px) {
  .brand h1 { font-size: 19px; }
  #controls button { font-size: 12.5px; gap: 4px; }
  #controls .ic { font-size: 16px; }
  .pl { padding: 5px 7px; gap: 6px; }
  .pl-t b { font-size: 13.5px; }
  #status { font-size: 12px; }
}

/* ---------- bố cục ngang (máy tính / điện thoại xoay ngang) ---------- */
@media (min-aspect-ratio: 11/10) and (min-width: 600px) {
  #app {
    grid-template-columns: minmax(0, 1fr) minmax(250px, 330px);
    grid-template-rows: 1fr auto auto auto auto 1fr;
    grid-template-areas: "stage ." "stage top" "stage match" "stage controls" "stage tally" "stage .";
    align-content: stretch;
    column-gap: 18px; row-gap: 10px;
  }
  #match { grid-template-columns: minmax(0, 1fr); }
  #pl2 { flex-direction: row; text-align: left; }
  #status { order: 3; font-size: 15px; padding: 4px 0; }
  #controls { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .brand h1 { font-size: 26px; }
}
@media (min-aspect-ratio: 11/10) and (min-width: 600px) and (max-height: 430px) {
  #app { row-gap: 6px; }
  .brand h1 { font-size: 20px; }
  .pl { padding: 4px 8px; }
  #controls button { min-height: 38px; }
  #status { padding: 0; font-size: 13.5px; }
}

/* ---------- hộp thoại ---------- */
.modal {
  position: fixed; inset: 0; z-index: 20; 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(12, 9, 6, .62); backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px); animation: fade .25s ease;
}
@keyframes fade { from { opacity: 0; } }
.card {
  position: relative; width: 100%; max-width: 430px; max-height: 100%; overflow-y: auto; touch-action: pan-y; overscroll-behavior: contain;
  background: linear-gradient(180deg, #392c22, #2a2019); border: 1px solid rgba(255, 220, 170, .2); border-radius: 20px;
  padding: 18px 18px 16px; box-shadow: 0 24px 60px rgba(0, 0, 0, .55); animation: pop .3s cubic-bezier(.2, 1.4, .4, 1);
}
@keyframes pop { from { transform: scale(.9); opacity: 0; } }
.x-close { position: absolute; right: 10px; top: 10px; width: 34px; height: 34px; border-radius: 10px; border: 1px solid var(--line); background: rgba(255, 255, 255, .06); font-size: 15px; }
.menu-head { text-align: center; margin-bottom: 10px; }
.menu-head h2 { margin: 4px 0 2px; font-size: 30px; color: var(--gold); letter-spacing: .5px; }
.menu-head p { margin: 0; font-size: 13.5px; color: var(--muted); line-height: 1.4; }
.big-logo { display: flex; justify-content: center; gap: 4px; padding: 10px 14px; margin: 0 auto; width: max-content; border-radius: 12px; background: #fbf6e8; background-image: linear-gradient(#a9c6e8 1px, transparent 1px), linear-gradient(90deg, #a9c6e8 1px, transparent 1px); background-size: 34px 34px; background-position: -1px -1px; }
.big-logo .pc { width: 34px; height: 34px; transform: scale(1.15); }
.wood .big-logo { background: linear-gradient(135deg, #e9c07c, #c98f4a); background-image: none; background-color: #d9a75f; }
.opt { margin: 8px 0; }
.opt-l { display: block; font-size: 12px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); margin: 0 0 5px 2px; font-weight: 700; }
.seg { display: flex; gap: 3px; padding: 3px; border-radius: 13px; background: rgba(0, 0, 0, .28); border: 1px solid rgba(255, 255, 255, .05); }
.seg button { flex: 1; min-width: 0; min-height: 40px; padding: 7px 6px; border: 0; border-radius: 10px; background: none; color: var(--muted); font-size: 14px; font-weight: 700; transition: background .2s, color .2s; }
.seg button:hover { color: var(--text); background: rgba(255, 255, 255, .05); }
.seg button.on { background: linear-gradient(180deg, #ffd98a, var(--gold2)); color: #2a1800; box-shadow: 0 3px 10px rgba(240, 169, 58, .3); }
.tog { display: flex; align-items: center; gap: 12px; padding: 7px 4px; cursor: pointer; }
.tog input { position: absolute; opacity: 0; pointer-events: none; }
.sw { position: relative; width: 44px; height: 26px; border-radius: 13px; background: rgba(255, 255, 255, .15); flex: none; transition: background .2s; }
.sw::after { content: ''; position: absolute; left: 3px; top: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 3px rgba(0, 0, 0, .4); }
.tog input:checked + .sw { background: var(--gold2); }
.tog input:checked + .sw::after { transform: translateX(18px); }
.tog input:focus-visible + .sw { outline: 2px solid var(--gold); outline-offset: 2px; }
.tog-t { display: flex; flex-direction: column; line-height: 1.3; }
.tog-t b { font-size: 14.5px; }
.tog-t small { font-size: 12px; color: var(--muted); }
.menu-stats { margin: 8px 0 2px; padding: 8px 10px; border-radius: 12px; background: rgba(255, 207, 107, .08); font-size: 13px; text-align: center; color: var(--muted); line-height: 1.5; }
.menu-stats b { color: var(--gold); }
.menu-btns, .res-btns { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.menu-btns button, .res-btns button { flex: 1 1 auto; min-height: 48px; border-radius: 13px; font-size: 16px; font-weight: 800; padding: 10px 14px; }
button.main { border: 0; background: linear-gradient(180deg, #ffd98a, var(--gold2)); color: #2a1800; box-shadow: 0 6px 18px rgba(240, 169, 58, .35); }
button.main:hover { filter: brightness(1.06); }
button.sec { border: 1px solid var(--line); background: rgba(255, 255, 255, .07); }
button.sec:hover { background: rgba(255, 255, 255, .13); }
.res-btns .main { flex-basis: 100%; }

.res-card { text-align: center; max-width: 400px; }
.res-badge { width: 74px; height: 74px; margin: 2px auto 6px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 38px; background: radial-gradient(circle at 35% 30%, #ffe7a8, #f0a93a); box-shadow: 0 0 0 6px rgba(255, 207, 107, .15), 0 10px 26px rgba(240, 169, 58, .35); animation: badge .7s cubic-bezier(.2, 1.6, .4, 1); }
.res-badge.lose { background: radial-gradient(circle at 35% 30%, #d7c9b8, #7c6a58); box-shadow: 0 0 0 6px rgba(200, 180, 160, .12); }
.res-badge.draw { background: radial-gradient(circle at 35% 30%, #cfe1ff, #5e86c4); box-shadow: 0 0 0 6px rgba(120, 160, 230, .15); }
@keyframes badge { from { transform: scale(.3) rotate(-30deg); opacity: 0; } }
.res-card h2 { margin: 4px 0 4px; font-size: 28px; color: var(--gold); }
.res-card h2.lose { color: #ffb3a6; } .res-card h2.draw { color: #bcd4ff; }
.res-card p { margin: 0 0 6px; color: var(--muted); font-size: 14px; }
.res-points { margin: 8px 0; line-height: 1.5; font-size: 14px; }
.res-points .gain { display: block; font-size: 30px; font-weight: 900; color: var(--gold); text-shadow: 0 2px 12px rgba(255, 207, 107, .35); animation: badge .6s .15s both; }
.res-points small { display: block; color: var(--muted); font-size: 12.5px; }
.res-points .tot b { color: var(--gold); }

/* màn hình thấp (điện thoại xoay ngang) */
@media (max-height: 500px) {
  .card { padding: 12px 14px; border-radius: 16px; }
  .res-badge { width: 50px; height: 50px; font-size: 26px; margin-bottom: 2px; }
  .res-card h2 { font-size: 22px; margin: 2px 0; }
  .res-points { margin: 4px 0; }
  .res-points .gain { font-size: 24px; }
  .menu-btns, .res-btns { margin-top: 8px; }
  .menu-btns button, .res-btns button { min-height: 42px; font-size: 15px; padding: 8px 12px; }
  .res-btns .main { flex-basis: auto; }
  .menu-head h2 { font-size: 24px; }
  .big-logo { display: none; }
  .opt { margin: 7px 0; }
  .seg button { min-height: 36px; }
  .tog { padding: 5px 4px; }
}
@media (max-height: 500px) and (min-width: 600px) {
  .menu-card { max-width: 720px; }
  .menu-opts { display: grid; grid-template-columns: 1fr 1fr; column-gap: 16px; align-items: center; }
  .menu-head { margin-bottom: 2px; }
  .menu-head p, .menu-stats, .tog-t small { display: none; }
  .menu-head h2 { font-size: 21px; margin: 0; }
  .opt { margin: 5px 0; }
  .opt-l { margin-bottom: 3px; }
}
