:root {
  --bg: #1e2c38;          /* mực xanh chàm */
  --bg-2: #263847;
  --line: #3a4f60;
  --text: #efe6d2;        /* giấy dó */
  --muted: #a9b3b8;
  --wood: #ddb06a;
  --wood-line: #5c3a17;
  --red: #b3261e;         /* son */
  --black: #1d2430;
  --jade: #3f9a75;        /* ngọc */
  --gold: #e3b45a;
  --danger: #e0412b;
  --radius: 10px;
  font-family: 'Be Vietnam Pro', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color: var(--text);
  box-sizing: border-box;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
*, *::before, *::after { box-sizing: inherit; }
html, body { height: 100%; margin: 0; }
html {
  background:
    radial-gradient(1200px 700px at 70% -10%, #2c4253 0%, transparent 60%),
    var(--bg);
  background-color: var(--bg);
}
body { -webkit-tap-highlight-color: transparent; }
button, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

.screen { display: none; min-height: 100%; }
.screen.active { display: flex; }

/* ---------- Menu ---------- */
#menu { align-items: center; justify-content: center; padding: 32px 18px; }
.menu-inner { width: 100%; max-width: 520px; }

.brand { display: flex; gap: 20px; align-items: center; margin-bottom: 32px; }
.seal {
  flex: none;
  width: 84px; height: 84px;
  display: grid; place-items: center;
  font-family: 'Noto Serif TC', serif; font-weight: 900;
  font-size: 30px; line-height: 1.02; text-align: center;
  color: #f7e9cf; background: var(--red);
  border-radius: 8px;
  box-shadow: inset 0 0 0 3px #f7e9cf, inset 0 0 0 5px var(--red);
  transform: rotate(-4deg);
}
h1 {
  margin: 0;
  font-family: 'Noto Serif TC', 'Be Vietnam Pro', serif;
  font-weight: 900; font-size: clamp(40px, 9vw, 56px); line-height: 1.05;
  letter-spacing: -0.01em;
}
.tag { margin: 8px 0 0; color: var(--muted); line-height: 1.5; max-width: 34ch; }

.tabs { display: flex; border-bottom: 1px solid var(--line); margin-bottom: 22px; }
.tab {
  flex: 1; background: none; border: 0; padding: 12px 6px 14px;
  font-weight: 600; color: var(--muted); cursor: pointer;
  border-bottom: 3px solid transparent; margin-bottom: -1px;
}
.tab.active { color: var(--text); border-bottom-color: var(--gold); }

.panel { display: none; flex-direction: column; gap: 10px; }
.panel.active { display: flex; }
.field-label { font-size: 14px; color: var(--muted); margin-top: 8px; display: block; }
.hint { color: var(--muted); line-height: 1.55; margin: 4px 0 8px; font-size: 15px; }
.hint:empty { display: none; }

.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button {
  flex: 1 1 auto; padding: 10px 12px; cursor: pointer;
  background: var(--bg-2); border: 1px solid var(--line); border-radius: 8px;
}
.seg button.on { background: var(--text); color: var(--bg); border-color: var(--text); font-weight: 600; }

input {
  width: 100%; padding: 11px 12px; border-radius: 8px;
  background: #15212b; border: 1px solid var(--line);
}
input::placeholder { color: #6f7f8a; }
#join-code { text-transform: uppercase; letter-spacing: .15em; font-weight: 600; }

.split { display: grid; gap: 20px; margin-top: 6px; }
.split > div { display: flex; flex-direction: column; gap: 10px; }
.join-row { display: flex; gap: 8px; }
.join-row .btn { flex: none; margin: 0; }

.btn {
  padding: 11px 16px; border-radius: 8px; cursor: pointer;
  background: var(--bg-2); border: 1px solid var(--line); font-weight: 600;
}
.btn:hover { border-color: #557085; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--red); border-color: var(--red); color: #fff4e4; margin-top: 12px; padding: 13px 16px; }
.btn.primary:hover { background: #c42e25; }
.btn.small { padding: 7px 11px; font-size: 14px; }

/* ---------- Ván cờ ---------- */
#play { gap: 28px; padding: 16px; justify-content: center; align-items: flex-start; }
.board-col { flex: 0 1 auto; display: flex; flex-direction: column; gap: 10px; width: min(100%, 600px, calc((100dvh - 150px) * 0.83)); min-width: 280px; }
.board-wrap {
  border-radius: 14px; padding: 0; overflow: hidden;
  box-shadow: 0 18px 50px rgba(5, 12, 18, .55), 0 0 0 1px rgba(0,0,0,.25);
}
#board { display: block; width: 100%; height: auto; touch-action: manipulation; user-select: none; cursor: pointer; }

.board-bg { fill: var(--wood); }
.grid-line { stroke: var(--wood-line); stroke-width: 2.5; fill: none; stroke-linecap: square; }
.grid-frame { stroke: var(--wood-line); stroke-width: 6; fill: none; }
.river { font-family: 'Noto Serif TC', serif; font-weight: 900; font-size: 54px; fill: var(--wood-line); opacity: .75; letter-spacing: 18px; }
.file-no { font-size: 26px; font-weight: 600; fill: var(--wood-line); opacity: .7; }

.piece .rim { fill: #b98443; }
.piece .face { fill: url(#face); }
.piece .ring { fill: none; stroke-width: 3; }
.piece.red .ring { stroke: var(--red); }
.piece.black .ring { stroke: var(--black); }
.piece .glyph { font-family: 'Noto Serif TC', 'Songti SC', 'SimSun', serif; font-weight: 900; font-size: 50px; }
.piece.red .glyph { fill: var(--red); }
.piece.black .glyph { fill: var(--black); }
.piece .body { transition: transform .16s ease-out; }
.piece.sel .body { transform: translateY(-9px); }
.piece.sel .rim { fill: var(--gold); }
.piece.last .rim { fill: #8f5d24; }
.piece.check .rim { fill: var(--danger); animation: pulse 1s ease-in-out infinite alternate; }
@keyframes pulse { to { fill: #ff8a6a; } }

.from-mark { fill: none; stroke: var(--jade); stroke-width: 4; stroke-dasharray: 8 7; opacity: .9; }
.dot { fill: var(--jade); opacity: .9; }
.cap-ring { fill: none; stroke: var(--jade); stroke-width: 6; }
.hint-mark { fill: none; stroke: #2a6fd6; stroke-width: 6; stroke-dasharray: 10 6; }

.player {
  display: flex; align-items: center; gap: 10px; min-height: 44px;
  padding: 6px 12px; border-radius: var(--radius);
  background: rgba(255,255,255,.03); border: 1px solid transparent;
}
.player.turn { border-color: var(--gold); background: rgba(227,180,90,.08); }
.player .chip { width: 22px; height: 22px; border-radius: 50%; flex: none; box-shadow: inset 0 0 0 3px #f4e4c1; }
.player .chip.r { background: var(--red); }
.player .chip.b { background: var(--black); }
.player .pname { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player .presence { font-size: 13px; color: var(--muted); }
.player .presence.off { color: #e38b70; }
.player .caps { margin-left: auto; font-family: 'Noto Serif TC', serif; font-weight: 900; font-size: 17px; letter-spacing: 1px; text-align: right; line-height: 1.2; }
.player .caps .r { color: #e46a5f; }
.player .caps .b { color: #c9d1d6; }

.side { flex: 0 1 300px; display: flex; flex-direction: column; gap: 16px; padding-top: 54px; min-width: 240px; }
.status { font-size: 20px; font-weight: 600; line-height: 1.35; min-height: 2.7em; }
.status small { display: block; font-size: 14px; font-weight: 400; color: var(--muted); margin-top: 4px; }
.status.alert { color: #ff9b82; }

.room-box[hidden] { display: none; }
.room-box { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px; border: 1px dashed var(--line); border-radius: var(--radius); }
.room-label { display: block; font-size: 13px; color: var(--muted); }
#room-code { font-size: 22px; letter-spacing: .18em; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.actions .btn { width: 100%; }

.moves-wrap h2 { font-size: 15px; color: var(--muted); font-weight: 500; margin: 0 0 8px; }
#moves {
  list-style: none; margin: 0; padding: 0; max-height: 300px; overflow-y: auto;
  display: grid; grid-template-columns: 2.4em 1fr 1fr; row-gap: 2px;
  font-variant-numeric: tabular-nums; font-size: 15px;
}
#moves li { display: contents; }
#moves .n { color: #6f7f8a; }
#moves .mv { padding: 2px 6px; border-radius: 4px; }
#moves .mv.cur { background: rgba(227,180,90,.18); }
#moves .mv.r { color: #f0a49b; }
#moves:empty::before { content: 'Chưa có nước nào.'; grid-column: 1 / -1; color: #6f7f8a; }

/* ---------- Hộp thoại & thông báo ---------- */
.overlay { position: fixed; inset: 0; background: rgba(8, 14, 20, .6); display: grid; place-items: center; padding: 20px; z-index: 20; }
.overlay[hidden] { display: none; }
.sheet {
  width: min(380px, 100%); background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 14px; padding: 24px; box-shadow: 0 24px 60px rgba(0,0,0,.5);
  animation: rise .18s ease-out;
}
.sheet h2 { margin: 0 0 6px; font-family: 'Noto Serif TC', 'Be Vietnam Pro', serif; font-size: 28px; }
.sheet p { margin: 0 0 18px; color: var(--muted); line-height: 1.5; }
.sheet .btns { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.sheet .btns .btn.primary { margin-top: 0; }
@keyframes rise { from { transform: translateY(10px); opacity: 0; } }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); font-weight: 600;
  padding: 10px 16px; border-radius: 8px; transition: .2s; z-index: 30; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Màn hình nhỏ ---------- */
@media (max-width: 860px) {
  #play { flex-direction: column; align-items: center; gap: 14px; padding: 10px; }
  .board-col { width: min(100%, 600px); }
  .side { width: 100%; max-width: 600px; padding-top: 0; flex-basis: auto; }
  .status { min-height: 0; font-size: 18px; }
  #moves { max-height: 180px; }
}
@media (min-width: 520px) {
  .split { grid-template-columns: 1fr 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* Chân trang bản quyền */
html, body { height: auto; }
body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
#menu.screen.active,
#play.screen.active { flex: 1 0 auto; min-height: 0; }
.site-footer {
  flex: none;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--line);
}
.site-footer a { color: var(--gold); text-decoration: none; }
.site-footer a:hover { text-decoration: underline; }