:root {
  --bg: #f4efe6;
  --card: #fffdf8;
  --ink: #2b2118;
  --muted: #7b6d5f;
  --line: #e4d9c7;
  --accent: #7a2418;
  --accent-2: #a8382a;
  --red: #c0281c;
  --black: #1f1f1f;
  --wood: #e9c58f;
  --wood-line: #6b4321;
  --ok: #2f8a4b;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(60, 40, 20, .06), 0 6px 20px rgba(60, 40, 20, .07);
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--ink); line-height: 1.45;
  padding-bottom: env(safe-area-inset-bottom);
}
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.2; }
h2 { font-size: 1.15rem; }
h3 { font-size: .95rem; color: var(--muted); font-weight: 600; margin-top: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; justify-content: center; }
.error { color: var(--red); min-height: 1.2em; margin: .4rem 0; font-size: .9rem; }

/* Thanh trên cùng */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
  padding: .7rem 1.2rem; padding-top: max(.7rem, env(safe-area-inset-top));
  background: var(--accent); color: #fff;
}
.brand { color: #fff; font-weight: 700; font-size: 1.1rem; display: flex; align-items: center; gap: .5rem; }
.brand-mark {
  display: inline-grid; place-items: center; width: 32px; height: 32px; border-radius: 50%;
  background: #fff4e0; color: var(--red); border: 2px solid #f1c27d; font-size: 1rem;
}
nav { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; }
nav a, nav .link { color: #fde9d6; font-size: .95rem; }
nav a.active { color: #fff; font-weight: 600; }
.link { background: none; border: 0; padding: 0; cursor: pointer; font: inherit; }

main { max-width: 1180px; margin: 0 auto; padding: 1.2rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.2rem; box-shadow: var(--shadow); }
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(270px, 1fr)); }

/* Nút & ô nhập */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .6rem 1rem; border-radius: 10px; border: 1px solid var(--line);
  background: #fff; color: var(--ink); font: inherit; font-weight: 600; cursor: pointer;
  transition: background .15s, transform .05s;
}
.btn:hover { background: #faf4ea; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.danger { color: var(--red); }
.btn.small { padding: .35rem .7rem; font-size: .85rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
input:not([type=checkbox]) {
  width: 100%; padding: .65rem .8rem; border-radius: 10px; border: 1px solid var(--line);
  font: inherit; background: #fff; margin-top: .25rem;
}
input:focus { outline: 2px solid #e8b48c; border-color: transparent; }
label { display: block; font-size: .9rem; font-weight: 600; margin-bottom: .8rem; }
label.check { display: flex; gap: .5rem; align-items: center; font-weight: 500; }
.row { display: flex; gap: .5rem; align-items: center; }
.row input { margin: 0; }

/* Đăng nhập */
.auth-card { max-width: 400px; margin: 2rem auto; text-align: center; }
.auth-card form { text-align: left; margin-top: 1rem; }
.auth-hero { display: flex; justify-content: center; gap: .6rem; margin-bottom: .8rem; }
.hero-piece {
  width: 64px; height: 64px; border-radius: 50%; display: grid; place-items: center; font-size: 1.9rem;
  background: radial-gradient(circle at 35% 30%, #fff8ea, #f0d6a8); border: 3px solid #b58a4c;
  box-shadow: 0 3px 8px rgba(0,0,0,.2); font-family: "Noto Serif SC", "Songti SC", "SimSun", serif;
}
.hero-piece.red { color: var(--red); } .hero-piece.black { color: var(--black); }
.tabs { display: flex; background: #f3eadc; border-radius: 10px; padding: 4px; margin-top: 1rem; }
.tab { flex: 1; border: 0; background: none; padding: .5rem; border-radius: 8px; font: inherit; font-weight: 600; color: var(--muted); cursor: pointer; }
.tab.active { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.08); }

/* Thẻ người chơi & hạng */
.badge {
  display: inline-flex; align-items: center; gap: .3rem; padding: .15rem .55rem; border-radius: 999px;
  font-size: .78rem; font-weight: 700; color: #fff; white-space: nowrap;
}
.me-card .elo { font-size: 2.4rem; font-weight: 800; line-height: 1; margin: .4rem 0; }
.progress { height: 8px; background: #efe5d5; border-radius: 99px; overflow: hidden; margin: .5rem 0 .3rem; }
.progress > div { height: 100%; border-radius: 99px; }
.stats { display: flex; gap: 1rem; margin-top: .8rem; }
.stats div { flex: 1; text-align: center; background: #f8f1e5; border-radius: 10px; padding: .5rem; }
.stats b { display: block; font-size: 1.15rem; }
.stats span { font-size: .75rem; color: var(--muted); }

.tc-picker { display: flex; gap: .4rem; margin: .8rem 0; }
.tc-picker button { flex: 1; padding: .5rem 0; border-radius: 10px; border: 1px solid var(--line); background: #fff; font: inherit; font-weight: 600; cursor: pointer; }
.tc-picker button.active { border-color: var(--accent); background: #fbeee6; color: var(--accent); }

.waiting { text-align: center; padding: .6rem 0; display: grid; gap: .7rem; justify-items: center; }
.spinner { width: 36px; height: 36px; border-radius: 50%; border: 4px solid #eadfcd; border-top-color: var(--accent); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.room-code { font-size: 2rem; font-weight: 800; letter-spacing: .25em; font-family: ui-monospace, Menlo, monospace; color: var(--accent); }
.divider { text-align: center; margin: .9rem 0; color: var(--muted); font-size: .85rem; position: relative; }
.divider::before { content: ""; position: absolute; left: 0; right: 0; top: 50%; border-top: 1px solid var(--line); }
.divider span { background: var(--card); padding: 0 .6rem; position: relative; }
.more { display: inline-block; margin-top: .7rem; font-size: .9rem; font-weight: 600; }

/* Bảng */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { padding: .55rem .5rem; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; }
.table th { color: var(--muted); font-weight: 600; font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; }
.table td.num, .table th.num { text-align: right; }
.table tr.me td { background: #fbefe4; }
.pos { font-weight: 800; color: var(--muted); }
.pos.top1 { color: #c99a06; } .pos.top2 { color: #8a96a3; } .pos.top3 { color: #a8662f; }
.rank-legend { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: 1rem; }
.mini-row { display: flex; align-items: center; gap: .6rem; padding: .4rem 0; border-bottom: 1px dashed var(--line); }
.mini-row:last-child { border: 0; }
.mini-row .name { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.win { color: var(--ok); font-weight: 700; } .loss { color: var(--red); font-weight: 700; } .draw { color: var(--muted); font-weight: 700; }

/* Ván cờ */
.game-layout { display: grid; grid-template-columns: minmax(0, 620px) minmax(260px, 1fr); gap: 1.2rem; align-items: start; justify-content: center; }
.board-col { display: grid; gap: .6rem; }
.board-wrap { background: var(--wood); border-radius: 12px; box-shadow: var(--shadow), inset 0 0 0 3px #b98a4f; padding: 4px; }
#board { display: block; width: 100%; height: auto; user-select: none; -webkit-user-select: none; touch-action: manipulation; }
.player-bar { display: flex; align-items: center; gap: .7rem; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: .55rem .8rem; }
.player-bar .dot { width: 14px; height: 14px; border-radius: 50%; flex: none; }
.player-bar .dot.r { background: var(--red); } .player-bar .dot.b { background: var(--black); }
.player-bar .pname { font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-bar .pelo { color: var(--muted); font-weight: 500; font-size: .9rem; }
.clock { font-family: ui-monospace, Menlo, monospace; font-size: 1.25rem; font-weight: 700; padding: .25rem .6rem; border-radius: 8px; background: #f1e8da; min-width: 5.2ch; text-align: center; }
.clock.running { background: var(--accent); color: #fff; }
.clock.low { background: var(--red); color: #fff; }
.side-col { position: sticky; top: 1rem; }
.status { font-weight: 700; font-size: 1.05rem; padding: .7rem .8rem; border-radius: 10px; background: #f6eee1; margin-bottom: .8rem; }
.status.mine { background: #e7f3ea; color: #1f6a37; }
.status.check { background: #fde4e1; color: var(--red); }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.actions .btn { flex: 1; }
.draw-offer { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; background: #fff3d6; border: 1px solid #f0d48a; border-radius: 10px; padding: .6rem .7rem; margin-bottom: .8rem; font-weight: 600; }
.draw-offer span { flex: 1; }
.moves { list-style: none; padding: 0; margin: 0; max-height: 340px; overflow-y: auto; display: grid; grid-template-columns: 2.4em 1fr 1fr; font-family: ui-monospace, Menlo, monospace; font-size: .9rem; }
.moves li { display: contents; }
.moves span { padding: .25rem .3rem; border-bottom: 1px solid #f1e9dc; }
.moves .n { color: var(--muted); }
.moves .last { background: #fbe6d4; border-radius: 4px; }

/* Quân cờ trên SVG */
.piece { cursor: pointer; }
.piece circle.outer { fill: #f6dfb4; stroke: #8a5a2b; stroke-width: 3; }
.piece circle.inner { fill: none; stroke-width: 2.5; }
.piece text { font-family: "Noto Serif SC", "Songti SC", "STSong", "SimSun", "PingFang SC", serif; font-size: 50px; font-weight: 700; text-anchor: middle; dominant-baseline: central; pointer-events: none; }
.piece.r text { fill: var(--red); } .piece.r circle.inner { stroke: var(--red); }
.piece.b text { fill: var(--black); } .piece.b circle.inner { stroke: var(--black); }
.piece.sel circle.outer { stroke: #2f7de1; stroke-width: 6; }
.piece.check circle.outer { stroke: #e0301e; stroke-width: 7; }
.hint { fill: rgba(47, 125, 225, .55); pointer-events: none; }
.hint-cap { fill: none; stroke: rgba(47, 125, 225, .8); stroke-width: 6; pointer-events: none; }
.lastmark { fill: none; stroke: #e2883a; stroke-width: 4; stroke-dasharray: 10 8; pointer-events: none; }

/* Modal & toast */
.modal { position: fixed; inset: 0; background: rgba(30, 20, 10, .45); display: grid; place-items: center; padding: 1rem; z-index: 50; }
.modal-box { max-width: 380px; width: 100%; text-align: center; }
.modal-box .big { font-size: 1.6rem; font-weight: 800; margin: .3rem 0; }
.delta { font-size: 1.3rem; font-weight: 800; }
.toast { position: fixed; left: 50%; bottom: calc(1.2rem + env(safe-area-inset-bottom)); transform: translateX(-50%); background: #2b2118; color: #fff; padding: .7rem 1.1rem; border-radius: 10px; z-index: 60; font-weight: 600; max-width: 90vw; }

@media (max-width: 860px) {
  .game-layout { grid-template-columns: 1fr; }
  .side-col { position: static; }
  main { padding: .7rem; }
  .moves { max-height: 200px; }
}
@media (max-width: 520px) {
  nav { gap: .7rem; font-size: .9rem; }
  .topbar { padding: .6rem .8rem; }
}

/* Vừa khít chiều cao màn hình trên máy tính */
@media (min-width: 861px) {
  .game-layout { grid-template-columns: auto minmax(260px, 340px); }
  .board-col { width: min(620px, calc((100vh - 230px) * 0.9)); min-width: 380px; }
}
.btn, .tc-picker button { white-space: nowrap; }
.tc-picker button { font-size: .88rem; padding: .5rem .2rem; }
#btn-flip { flex: 0 0 auto; }

/* Tài khoản máy & Google */
.badge.bot { background: #5b6472; }
.google-btn { display: flex; justify-content: center; margin-top: 1rem; min-height: 44px; }

/* Thông báo & quản trị */
.announce { background: #fff3d6; border: 1px solid #f0d48a; border-radius: var(--radius); padding: .8rem 1rem; margin-bottom: 1rem; font-weight: 600; white-space: pre-line; }
.admin-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.admin-head h2 { margin: 0; }
.admin-tabs { margin-top: 0; flex-wrap: wrap; }
.admin-tabs .tab { flex: none; padding: .45rem .8rem; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .7rem; margin-bottom: 1.2rem; }
.stat { background: #f8f1e5; border-radius: 12px; padding: .8rem 1rem; }
.stat b { display: block; font-size: 1.6rem; line-height: 1.2; }
.stat span { color: var(--muted); font-size: .82rem; }
.toolbar { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-bottom: .8rem; }
.toolbar input, .toolbar select { width: auto; flex: 1; min-width: 140px; margin: 0; }
.table .acts { display: flex; gap: .3rem; flex-wrap: wrap; }
.table .acts .btn { padding: .25rem .55rem; font-size: .8rem; }
.table select { padding: .3rem; border-radius: 8px; border: 1px solid var(--line); font: inherit; }
.tag { display: inline-block; font-size: .72rem; font-weight: 700; padding: .05rem .45rem; border-radius: 6px; background: #eee3d2; color: var(--muted); margin-left: .3rem; }
.tag.red { background: #fde4e1; color: var(--red); } .tag.green { background: #e2f2e6; color: #1f6a37; } .tag.blue { background: #e2ecfb; color: #2a5bb5; }
.settings-form { max-width: 560px; }
.settings-form .hint { font-weight: 400; color: var(--muted); font-size: .82rem; margin-top: .25rem; }
.settings-form textarea { width: 100%; min-height: 80px; padding: .6rem .8rem; border-radius: 10px; border: 1px solid var(--line); font: inherit; margin-top: .25rem; }
.settings-form select { width: 100%; padding: .6rem; border-radius: 10px; border: 1px solid var(--line); font: inherit; margin-top: .25rem; }
.toolbar select { padding: .65rem .6rem; border-radius: 10px; border: 1px solid var(--line); font: inherit; background: #fff; flex: 0 0 auto; }

/* Trang nội dung */
.page-content { max-width: 820px; margin: 0 auto; padding: 1.6rem 1.8rem; line-height: 1.65; }
.md h1 { font-size: 1.6rem; margin: 0 0 1rem; }
.md h2 { font-size: 1.25rem; margin: 1.6rem 0 .6rem; color: var(--accent); }
.md h3 { font-size: 1.05rem; margin: 1.2rem 0 .5rem; color: var(--ink); }
.md p { margin: 0 0 .9rem; }
.md ul, .md ol { margin: 0 0 1rem; padding-left: 1.4rem; }
.md li { margin: .25rem 0; }
.md table { border-collapse: collapse; width: 100%; margin: 0 0 1rem; font-size: .93rem; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--line); padding: .45rem .65rem; text-align: left; }
.md th { background: #f6eee1; }
.md code { background: #f3eadc; padding: .1rem .35rem; border-radius: 5px; font-size: .9em; }
.md blockquote { margin: 0 0 1rem; padding: .6rem 1rem; border-left: 4px solid #e8b48c; background: #fbf4ea; border-radius: 0 8px 8px 0; }
.md img { max-width: 100%; border-radius: 10px; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 1.4rem 0; }
.site-footer { max-width: 1180px; margin: 1rem auto 2rem; padding: 0 1.2rem; text-align: center; color: var(--muted); font-size: .88rem; }
.site-footer a { margin: 0 .55rem; font-weight: 600; }
.page-editor { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.page-editor textarea { width: 100%; min-height: 460px; font-family: inherit; font-size: .92rem; line-height: 1.5; padding: .7rem; border-radius: 10px; border: 1px solid var(--line); }
.page-editor .preview { border: 1px dashed var(--line); border-radius: 10px; padding: 1rem 1.2rem; max-height: 520px; overflow-y: auto; background: #fff; }
.page-meta { display: grid; grid-template-columns: 2fr 1.4fr .6fr; gap: .8rem; }
.page-meta label { margin-bottom: .6rem; }
@media (max-width: 860px) { .page-editor, .page-meta { grid-template-columns: 1fr; } .page-content { padding: 1.1rem; } }

/* Chữ trên sông & bộ quân */
.river-text { font-family: "Trebuchet MS", system-ui, sans-serif; font-size: 50px; font-weight: 700; letter-spacing: 8px; fill: #6b4321; opacity: .8; }
.piece.vi text { font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif; font-weight: 800; }
.piece.img circle.outer { fill: none; stroke: transparent; }
.piece.img.sel circle.outer { stroke: #2f7de1; stroke-width: 6; }
.piece.img.check circle.outer { stroke: #e0301e; stroke-width: 7; }
