:root {
  --bg: #f3ece0;
  --bg2: #fffaf2;
  --card: #ffffff;
  --text: #2b1d14;
  --muted: #8a7766;
  --line: #e2d5c3;
  --primary: #a3311f;
  --primary-2: #7a2e1d;
  --accent: #d79b2f;
  --board: #e9c48a;
  --board-2: #d9a865;
  --grid: #6b3f1d;
  --red: #b3261e;
  --black: #1f2a30;
  --ok: #2f7d4f;
  --shadow: 0 6px 24px rgba(60, 30, 10, .14);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17120e; --bg2: #201913; --card: #261e17; --text: #f1e6d6; --muted: #a8968a;
    --line: #3b3027; --shadow: 0 6px 24px rgba(0, 0, 0, .45); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17120e; --bg2: #201913; --card: #261e17; --text: #f1e6d6; --muted: #a8968a;
  --line: #3b3027; --shadow: 0 6px 24px rgba(0, 0, 0, .45); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
  overscroll-behavior-y: none;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; -webkit-user-select: none; user-select: none; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
code { background: var(--bg2); border: 1px solid var(--line); border-radius: 6px; padding: 1px 5px; font-size: 12px; }

#app { min-height: 100%; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.screen { display: none; min-height: 100vh; min-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)); flex-direction: column; }
.screen.active { display: flex; }
.scroll { flex: 1; overflow: auto; padding: 8px 16px 32px; max-width: 760px; width: 100%; margin: 0 auto; }

/* ---------- Topbar ---------- */
.topbar {
  display: grid; grid-template-columns: 44px 1fr 44px; align-items: center; gap: 8px;
  padding: 8px 12px; background: var(--primary-2); color: #fff4e4;
  position: sticky; top: 0; z-index: 5;
}
.topbar h2 { margin: 0; font-size: 18px; text-align: center; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .muted { color: #f1d9b9; text-align: right; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; border: 0; background: rgba(255,255,255,.12);
  color: inherit; font-size: 22px; line-height: 1; display: grid; place-items: center;
}
.icon-btn:hover { background: rgba(255,255,255,.22); }
.icon-btn.off { opacity: .45; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 18px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--card); box-shadow: 0 1px 0 rgba(0,0,0,.04); font-weight: 600;
}
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-2); }
.btn-big { padding: 16px 18px; font-size: 17px; }
.btn-wide { width: 100%; margin-top: 18px; padding: 15px; font-size: 16px; }

/* ---------- Home ---------- */
.home { width: 100%; max-width: 420px; margin: auto; padding: 32px 16px; display: flex; flex-direction: column; gap: 24px; }
.logo { text-align: center; }
.logo h1 { margin: 10px 0 2px; font-size: 34px; letter-spacing: .5px; }
.logo .sub { margin: 0; color: var(--muted); }
.logo-piece {
  width: 92px; height: 92px; margin: 0 auto; border-radius: 50%;
  display: grid; place-items: center; font-size: 50px; color: var(--red);
  background: radial-gradient(circle at 35% 30%, #fff3dc, #f0cf98 60%, #d6a35c);
  box-shadow: inset 0 0 0 5px #f7e3bf, inset 0 0 0 7px var(--red), 0 8px 20px rgba(80, 30, 0, .35);
  font-family: "KaiTi", "STKaiti", "Noto Serif SC", "Songti SC", serif;
}
.menu { display: flex; flex-direction: column; gap: 10px; }
.home-foot { display: flex; justify-content: center; gap: 18px; flex-wrap: wrap; color: var(--muted); }
.toggle { display: inline-flex; gap: 6px; align-items: center; cursor: pointer; }

/* ---------- Levels ---------- */
.section-title { margin: 18px 0 10px; font-size: 14px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.level-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.level {
  text-align: left; padding: 14px; border-radius: var(--radius); border: 2px solid var(--line);
  background: var(--card); display: grid; grid-template-columns: 46px 1fr; gap: 4px 12px; align-items: center;
}
.level.on { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(163, 49, 31, .15); }
.level { grid-template-columns: 56px 1fr; gap: 2px 12px; align-items: start; }
.level .lv-face {
  grid-row: span 4; position: relative; width: 56px; height: 56px; border-radius: 50%; display: grid; place-items: center;
  font-size: 30px; line-height: 1; background: color-mix(in srgb, var(--c, var(--primary)) 22%, var(--card));
  box-shadow: inset 0 0 0 3px var(--c, var(--primary));
}
.level .lv-face i { position: absolute; font-style: normal; font-size: 16px; line-height: 1; }
.level .lv-cup { right: -4px; bottom: -2px; }
.level .lv-lock { right: -4px; bottom: -2px; }
.level .lv-name { font-weight: 700; }
.level .lv-rank { font-size: 12px; font-weight: 600; color: var(--primary); }
.level .lv-desc { font-size: 12.5px; color: var(--muted); }
.level .lv-stat { grid-column: 2; font-size: 12px; color: var(--muted); }
.level.locked { opacity: .62; }
.level.locked .lv-face { filter: grayscale(1); }
.level.locked .lv-stat { color: var(--text); font-weight: 600; }

.seg { display: flex; background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 10px 6px; border-radius: 9px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 4px rgba(0,0,0,.12); }

/* ---------- Puzzles ---------- */
.hint-text { color: var(--muted); font-size: 13px; }
.puzzle-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 8px; }
.pz {
  aspect-ratio: 1; border-radius: 12px; border: 1px solid var(--line); background: var(--card);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; position: relative;
}
.pz b { font-size: 18px; }
.pz small { color: var(--accent); font-size: 10px; letter-spacing: -1px; }
.pz.solved { background: #e5f2e8; border-color: #9cc9aa; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .pz.solved { background: #1f3326; border-color: #2f5a3c; } }
.pz.solved::after { content: "✓"; position: absolute; top: 3px; right: 6px; color: var(--ok); font-weight: 800; font-size: 12px; }
.custom-tools { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; align-items: center; }
.custom-tools .hint-text { flex-basis: 100%; }

/* ---------- History ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 8px 0 16px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px; text-align: center; }
.stat b { display: block; font-size: 22px; }
.stat span { font-size: 12px; color: var(--muted); }
.history-list { display: flex; flex-direction: column; gap: 8px; }
.hist-item {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 12px 14px; text-align: left;
  border-radius: 12px; border: 1px solid var(--line); background: var(--card);
}
.hist-item .r { font-weight: 700; }
.hist-item .r.win { color: var(--ok); }
.hist-item .r.lose { color: var(--red); }
.hist-item .meta { color: var(--muted); font-size: 12.5px; }
.empty { text-align: center; color: var(--muted); padding: 32px 0; }

/* ---------- Game layout ---------- */
.game-layout {
  flex: 1; display: grid; gap: 16px; padding: 10px 16px 16px; width: 100%; max-width: 1100px; margin: 0 auto;
  grid-template-columns: minmax(0, 1fr);
}
@media (min-width: 900px) {
  .game-layout { grid-template-columns: minmax(0, 620px) minmax(260px, 1fr); align-items: start; }
}
.board-col { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.side-col { display: flex; flex-direction: column; gap: 10px; min-width: 0; }

.player-bar {
  display: grid; grid-template-columns: 40px minmax(0, 1fr) auto; align-items: center; gap: 10px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 6px 10px; position: relative;
}
.player-bar.turn { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(215, 155, 47, .25); }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; font-size: 18px; color: #fff;
  font-family: "KaiTi", "STKaiti", "Noto Serif SC", serif;
}
.avatar.red { background: var(--red); }
.avatar.black { background: var(--black); }
.avatar.persona {
  font-family: inherit; font-size: 21px; line-height: 1;
  background: color-mix(in srgb, var(--c) 25%, var(--card)); box-shadow: inset 0 0 0 3px var(--c);
}
.avatar.persona.red { outline: 2px solid var(--red); outline-offset: 1px; }
.avatar.persona.black { outline: 2px solid var(--black); outline-offset: 1px; }
.pname { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pdesc { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.captured { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 1px; max-width: 160px; }
.captured .cp {
  width: 18px; height: 18px; border-radius: 50%; font-size: 11px; display: grid; place-items: center;
  background: #f3dcb2; border: 1px solid currentColor; font-family: "KaiTi", "STKaiti", "Noto Serif SC", serif;
}
.captured .cp.r { color: var(--red); }
.captured .cp.b { color: var(--black); }
.thinking { position: absolute; right: 10px; bottom: -4px; display: flex; gap: 3px; }
.thinking span { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: blink 1s infinite; }
.thinking span:nth-child(2) { animation-delay: .2s; }
.thinking span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 100% { opacity: .2; } 50% { opacity: 1; } }

/* ---------- Board ---------- */
.board-wrap { width: 100%; max-width: min(620px, calc((100dvh - 230px) * 0.9)); margin: 0 auto; min-width: 260px; }
#board {
  position: relative; width: 100%; aspect-ratio: 9 / 10; border-radius: 10px;
  background: linear-gradient(135deg, var(--board) 0%, #efcf98 45%, var(--board-2) 100%);
  box-shadow: var(--shadow), inset 0 0 0 4px #b98142;
  user-select: none; touch-action: manipulation;
}
#board-svg { position: absolute; inset: 0; width: 100%; height: 100%; }
#board-svg .ln { stroke: var(--grid); stroke-width: 3; }
#board-svg .frame { stroke: var(--grid); stroke-width: 6; fill: none; }
#board-svg .river { font: 600 46px "KaiTi", "STKaiti", "Noto Serif SC", serif; fill: #7b4a22; opacity: .75; letter-spacing: 10px; }
#board-svg .coord { font: 600 24px system-ui, sans-serif; fill: #7b4a22; opacity: .7; }
#marks, #pieces { position: absolute; inset: 0; }

.piece {
  position: absolute; width: 10.6%; aspect-ratio: 1; transform: translate(-50%, -50%);
  border-radius: 50%; display: grid; place-items: center; cursor: pointer;
  background: radial-gradient(circle at 35% 30%, #fff6e2, #f2d7a5 58%, #d9ab68);
  box-shadow: 0 3px 5px rgba(60, 25, 0, .45), inset 0 -2px 3px rgba(120, 70, 20, .35);
  transition: left .26s ease, top .26s ease, transform .15s ease, opacity .2s;
  z-index: 2;
}
.piece::before {
  content: ""; position: absolute; inset: 8%; border-radius: 50%; border: 2px solid currentColor; opacity: .85;
}
.piece span {
  font-family: "KaiTi", "STKaiti", "Kaiti SC", "Noto Serif SC", "Songti SC", serif; font-weight: 700;
  font-size: min(5.2vw, 34px); line-height: 1; position: relative; top: -1px;
}
.latin .piece span { font-family: system-ui, sans-serif; font-size: min(3.8vw, 22px); font-weight: 800; }
.piece.r { color: var(--red); }
.piece.b { color: var(--black); }
.piece.sel { transform: translate(-50%, -58%) scale(1.06); box-shadow: 0 9px 12px rgba(60, 25, 0, .4), inset 0 -2px 3px rgba(120, 70, 20, .35); z-index: 3; }
.piece.moving { z-index: 4; }
.piece.incheck { box-shadow: 0 0 0 3px #ff3b30, 0 0 18px 4px rgba(255, 59, 48, .8); }
.piece.hiddenp { background: radial-gradient(circle at 35% 30%, #6a4a35, #3e2a1d 65%, #2a1b11); }
.piece.hiddenp::before { border-color: rgba(255, 220, 170, .35); }
.piece.hiddenp span { color: rgba(255, 220, 170, .55); font-size: min(4vw, 26px); }
.piece.hiddenp.r::after, .piece.hiddenp.b::after {
  content: ""; position: absolute; width: 22%; height: 22%; border-radius: 50%; bottom: 10%;
}
.piece.hiddenp.r::after { background: var(--red); }
.piece.hiddenp.b::after { background: #9fb3bf; }
.piece.flip { animation: flip .35s ease; }
@keyframes flip { 0% { transform: translate(-50%, -50%) scaleX(1); } 50% { transform: translate(-50%, -50%) scaleX(0); } 100% { transform: translate(-50%, -50%) scaleX(1); } }

.mark { position: absolute; transform: translate(-50%, -50%); pointer-events: none; }
.mark.dot { width: 3.4%; aspect-ratio: 1; border-radius: 50%; background: rgba(35, 120, 60, .75); box-shadow: 0 0 0 3px rgba(255, 255, 255, .45); }
.mark.ring { width: 11.6%; aspect-ratio: 1; border-radius: 50%; border: 3px solid rgba(35, 120, 60, .85); }
.mark.last-from { width: 6%; aspect-ratio: 1; border-radius: 50%; border: 3px dashed rgba(30, 90, 170, .7); }
.mark.last-to { width: 12.4%; aspect-ratio: 1; border-radius: 50%; box-shadow: 0 0 0 3px rgba(30, 90, 170, .8); }
.mark.hint { width: 12.4%; aspect-ratio: 1; border-radius: 50%; box-shadow: 0 0 0 4px rgba(215, 155, 47, .95), 0 0 16px 4px rgba(215, 155, 47, .8); animation: pulse 1s infinite; }
.mark.hint-from { width: 7%; aspect-ratio: 1; border-radius: 50%; border: 3px solid rgba(215, 155, 47, .95); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .45; } }
.hit { position: absolute; width: 11.1%; height: 10%; transform: translate(-50%, -50%); cursor: pointer; z-index: 1; }

/* ---------- Side panel ---------- */
.status {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; min-height: 44px;
  font-weight: 600; display: flex; align-items: center; gap: 8px;
}
.status.check { color: var(--red); }
.controls { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.ctl {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 9px 4px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--card); font-size: 12.5px; font-weight: 600;
}
.ctl b { font-size: 18px; line-height: 1.1; }
.ctl:hover:not(:disabled) { border-color: var(--accent); }
.ctl:disabled { opacity: .4; cursor: default; }
.move-list {
  list-style: none; margin: 0; padding: 8px; background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  display: grid; grid-template-columns: 34px 1fr 1fr; gap: 2px 6px; max-height: 300px; overflow: auto; font-variant-numeric: tabular-nums;
}
@media (min-width: 900px) { .move-list { max-height: calc(100dvh - 330px); } }
.move-list .no { color: var(--muted); text-align: right; padding: 3px 0; }
.move-list .mv { padding: 3px 6px; border-radius: 6px; cursor: default; }
.move-list .mv.r { color: var(--red); }
.move-list .mv.cur { background: rgba(215, 155, 47, .25); }
.move-list .mv.click { cursor: pointer; }
.move-list:empty::before { content: "Chưa có nước đi"; grid-column: 1 / -1; color: var(--muted); text-align: center; padding: 10px; }

/* ---------- Modal / toast ---------- */
.modal { position: fixed; inset: 0; background: rgba(20, 10, 5, .55); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-card {
  width: 100%; max-width: 380px; background: var(--card); border-radius: 18px; padding: 22px 20px 18px; text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .35); animation: pop .22s ease;
}
@keyframes pop { from { transform: scale(.92); opacity: 0; } }
.modal-card h3 { margin: 4px 0 6px; font-size: 26px; }
.modal-card h3.win { color: var(--ok); }
.modal-card h3.lose { color: var(--red); }
.modal-card p { margin: 0 0 16px; color: var(--muted); }
.modal-card .actions { display: flex; flex-direction: column; gap: 8px; }
.modal-card .emoji { font-size: 42px; }
.modal-card .modal-text { white-space: pre-line; }
.quote {
  display: flex; gap: 10px; align-items: center; text-align: left; margin: -6px 0 16px;
  padding: 10px 12px; border-radius: 12px; background: var(--bg2); border: 1px solid var(--line);
}
.quote .q-face {
  flex: none; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-size: 22px; line-height: 1;
  background: color-mix(in srgb, var(--c) 25%, var(--card)); box-shadow: inset 0 0 0 2px var(--c);
}
.quote .q-body { display: flex; flex-direction: column; font-size: 14px; }
.quote .q-body b { font-size: 13px; }
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translate(-50%, 20px); background: rgba(30, 20, 12, .92); color: #fff;
  padding: 10px 18px; border-radius: 999px; font-weight: 600; opacity: 0; pointer-events: none; transition: .25s; z-index: 60;
  max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
