/* Phông chữ Noto Sans (giống Lichess; SIL Open Font License, xem fonts/OFL-LICENSE.txt), lưu ngay trong game –
   không tải từ dịch vụ ngoài. Đủ dấu tiếng Việt và chữ Nga; chữ Hán dùng phông Noto Sans SC / PingFang của máy.
   Trình duyệt chỉ tải phần chữ cần dùng (unicode-range): trang tiếng Việt ~49 KB. */
@font-face {
  font-family: 'Noto Sans';
  font-style: normal;
  font-display: swap;
  font-weight: 100 900;
  src: url('fonts/noto-sans-cyrillic-ext-wght-normal.woff2') format('woff2-variations'), url('fonts/noto-sans-cyrillic-ext-wght-normal.woff2') format('woff2');
  unicode-range: U+0460-052F,U+1C80-1C8A,U+20B4,U+2DE0-2DFF,U+A640-A69F,U+FE2E-FE2F;
}
@font-face {
  font-family: 'Noto Sans';
  font-style: normal;
  font-display: swap;
  font-weight: 100 900;
  src: url('fonts/noto-sans-cyrillic-wght-normal.woff2') format('woff2-variations'), url('fonts/noto-sans-cyrillic-wght-normal.woff2') format('woff2');
  unicode-range: U+0301,U+0400-045F,U+0490-0491,U+04B0-04B1,U+2116;
}
@font-face {
  font-family: 'Noto Sans';
  font-style: normal;
  font-display: swap;
  font-weight: 100 900;
  src: url('fonts/noto-sans-vietnamese-wght-normal.woff2') format('woff2-variations'), url('fonts/noto-sans-vietnamese-wght-normal.woff2') format('woff2');
  unicode-range: U+0102-0103,U+0110-0111,U+0128-0129,U+0168-0169,U+01A0-01A1,U+01AF-01B0,U+0300-0301,U+0303-0304,U+0308-0309,U+0323,U+0329,U+1EA0-1EF9,U+20AB;
}
@font-face {
  font-family: 'Noto Sans';
  font-style: normal;
  font-display: swap;
  font-weight: 100 900;
  src: url('fonts/noto-sans-latin-ext-wght-normal.woff2') format('woff2-variations'), url('fonts/noto-sans-latin-ext-wght-normal.woff2') format('woff2');
  unicode-range: U+0100-0101,U+0104-010F,U+0112-0127,U+012A-0167,U+016A-019F,U+01A2-01AE,U+01B1-02BA,U+02BD-02C5,U+02C7-02CC,U+02CE-02D7,U+02DD-02FF,U+1D00-1DBF,U+1E00-1E9F,U+1EFA-1EFF,U+2020,U+20A0-20AA,U+20AD-20C0,U+2113,U+2C60-2C7F,U+A720-A7FF;
}
@font-face {
  font-family: 'Noto Sans';
  font-style: normal;
  font-display: swap;
  font-weight: 100 900;
  src: url('fonts/noto-sans-latin-wght-normal.woff2') format('woff2-variations'), url('fonts/noto-sans-latin-wght-normal.woff2') format('woff2');
  unicode-range: U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2000-206F,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD;
}

:root {
  /* ---- Chữ: 1 phông, 6 cỡ, 4 độ đậm – dùng cho toàn bộ game.
     Cỡ chữ co giãn theo bề rộng màn hình (điện thoại nhỏ 360px → máy tính), tính bằng rem nên vẫn theo
     cỡ chữ người dùng đặt trong trình duyệt / điện thoại. Không ghi cỡ chữ bằng px ở chỗ khác. */
  --font: 'Noto Sans', 'Noto Sans SC', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --fs-2xs: clamp(0.6875rem, 0.66rem + 0.1vw, 0.75rem);   /* 11→12px: số trên huy hiệu, giờ tin nhắn, hạt giống */
  --fs-xs: clamp(0.75rem, 0.72rem + 0.12vw, 0.8125rem);    /* 12→13px: nhãn nút dưới, nhãn nhóm, thông tin phụ */
  --fs-sm: clamp(0.8125rem, 0.78rem + 0.18vw, 0.9375rem);  /* 13→15px: gợi ý, thông báo, lựa chọn, nút nhỏ */
  --fs-md: clamp(0.9375rem, 0.9rem + 0.2vw, 1.0625rem);    /* 15→17px: nội dung chính, nút, tên */
  --fs-lg: clamp(1.125rem, 1.05rem + 0.35vw, 1.375rem);    /* 18→22px: tiêu đề hộp thoại */
  --fs-xl: clamp(1.5rem, 1.35rem + 0.75vw, 2rem);          /* 24→32px: số lớn (mã phòng), biểu tượng lớn */
  --fs-input: max(16px, var(--fs-md));                     /* ô nhập ≥16px: iOS không tự phóng to */
  --fw-regular: 400;
  --fw-medium: 500;
  --fw-semibold: 600;
  --fw-bold: 700;
  --lh: 1.5;
  --radius: 14px;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);

  --bg: #f6f3ec;
  --grid: #d9d3c4;
  --grid-strong: #bfb7a4;
  --panel: #fffdf8;
  --panel-border: rgba(0, 0, 0, 0.08);
  --text: #23201a;
  --muted: #6f6857;
  --x: #d8342c;
  --o: #1f5fd1;
  --last: rgba(255, 196, 0, 0.28);
  --win: rgba(40, 180, 90, 0.9);
  --accent: #1f5fd1;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
  --seg-on: var(--panel); /* lựa chọn đang bật trong nút phân đoạn */
  color-scheme: light;
  /* Màu bàn cờ: mặc định theo giao diện; đổi trong Cài đặt → Bàn cờ (boardstyle.js) */
  --board: var(--bg);
  --board-grid: var(--grid);
  --board-grid-strong: var(--grid-strong);
}
:root[data-board="wood"] { --board: #dcb67f; --board-grid: #b48a52; --board-grid-strong: #8f6a38; }
:root[data-board="green"] { --board: #cfe3c4; --board-grid: #9dbf8d; --board-grid-strong: #78a066; }
:root[data-board="blue"] { --board: #d6e4f2; --board-grid: #a6bfd8; --board-grid-strong: #7f9fbf; }
:root[data-board="night"] { --board: #1d2430; --board-grid: #2e3a4c; --board-grid-strong: #44546b; }

/* Tối: theo hệ thống (khi không chọn Sáng) hoặc chọn Tối trong Cài đặt */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161512;
    --grid: #2b2925;
    --grid-strong: #3c3934;
    --panel: #262421;
    --panel-border: rgba(255, 255, 255, 0.08);
    --text: #e6e4e1;
    --muted: #9b9893;
    --x: #ff5d52;
    --o: #57a0ff;
    --last: rgba(255, 196, 0, 0.18);
    --win: rgba(60, 220, 120, 0.9);
    --accent: #57a0ff;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
    --seg-on: #403d39;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #161512;
  --grid: #2b2925;
  --grid-strong: #3c3934;
  --panel: #262421;
  --panel-border: rgba(255, 255, 255, 0.08);
  --text: #e6e4e1;
  --muted: #9b9893;
  --x: #ff5d52;
  --o: #57a0ff;
  --last: rgba(255, 196, 0, 0.18);
  --win: rgba(60, 220, 120, 0.9);
  --accent: #57a0ff;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  --seg-on: #403d39;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* Biểu tượng SVG (index.html: #i-…): nét 2px, theo màu chữ */
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
svg.ico {
  width: 1.25em; height: 1.25em; flex: none; display: block;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--fs-md);
  line-height: var(--lh);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

#board {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
}
#board.grabbing { cursor: grabbing; }

.bar {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  max-width: calc(100% - 16px);
}
.bar.top {
  top: calc(8px + env(safe-area-inset-top));
  padding: 8px 14px;
  gap: 16px;
  font-size: var(--fs-md);
}
.bar.bottom { bottom: calc(10px + env(safe-area-inset-bottom)); }

.turn { display: flex; align-items: center; gap: 8px; font-weight: var(--fw-semibold); white-space: nowrap; }
.turn .dot { font-size: var(--fs-lg); font-weight: var(--fw-bold); line-height: 1; }
.turn .thinking { color: var(--muted); font-weight: var(--fw-regular); font-size: var(--fs-sm); }
.score { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.x { color: var(--x); }
.o { color: var(--o); }

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: transparent;
  border-radius: 12px;
  cursor: pointer;
  touch-action: manipulation;
}
.bar.bottom button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-width: 68px;
  min-height: 52px; /* vùng chạm đủ lớn cho ngón tay */
  padding: 6px 10px;
  font-size: var(--fs-xs);
  font-weight: var(--fw-medium);
  color: var(--muted);
  white-space: nowrap;
  transition: background-color 0.15s, transform 0.1s;
}
.bar.bottom button .ico { width: calc(var(--fs-lg) * 1.1); height: calc(var(--fs-lg) * 1.1); color: var(--text); }
@media (hover: hover) { .bar.bottom button:hover { background: rgba(127, 127, 127, 0.12); } }
.bar.bottom button:active { transform: scale(0.94); background: rgba(127, 127, 127, 0.16); }
.bar.bottom button:disabled { opacity: 0.55; pointer-events: none; }
.bar.bottom button:disabled .ico { color: var(--muted); }

.primary {
  background: var(--accent);
  color: #fff;
  padding: 10px 18px;
  font-weight: var(--fw-semibold);
  transition: filter 0.15s, transform 0.1s;
}
@media (hover: hover) { .primary:hover { filter: brightness(1.08); } }
.primary:active, .ghost:active { transform: scale(0.97); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.ghost {
  padding: 10px 14px;
  color: var(--muted);
}
.ghost:hover { background: rgba(127, 127, 127, 0.12); }

.banner {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(86px + env(safe-area-inset-bottom));
  display: flex;
  justify-content: center;
  pointer-events: none;
  padding: 0 8px;
}
.banner[hidden] { display: none; }
.banner-card {
  pointer-events: auto;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 14px 16px;
  text-align: center;
  animation: pop 0.25s ease-out;
}
.banner-title { font-size: var(--fs-lg); font-weight: var(--fw-bold); margin-bottom: 10px; letter-spacing: -0.01em; display: flex; align-items: center; justify-content: center; gap: 10px; }
.banner-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
@keyframes pop { from { transform: translateY(12px) scale(0.96); opacity: 0; } }

dialog {
  border: 1px solid var(--panel-border);
  border-radius: 18px;
  padding: 0;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
  width: min(420px, calc(100% - 24px));
  max-height: calc(100% - 24px);
}
dialog::backdrop { background: rgba(10, 12, 16, 0.38); }
dialog[open] { animation: dlg-in 0.22s var(--ease); }
dialog[open]::backdrop { animation: fade-in 0.22s ease-out; }
@keyframes dlg-in { from { opacity: 0; transform: translateY(10px) scale(0.98); } }
@keyframes fade-in { from { opacity: 0; } }
dialog form { padding: 18px; }
dialog h2 { margin: 0 0 14px; font-size: var(--fs-lg); font-weight: var(--fw-bold); letter-spacing: -0.01em; line-height: 1.25; }
fieldset { border: 0; margin: 0 0 14px; padding: 0; min-width: 0; }
legend { font-size: var(--fs-sm); font-weight: var(--fw-semibold); color: var(--text); margin-bottom: 6px; padding: 0; }
fieldset label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0 12px 6px 0;
  font-size: var(--fs-md);
  cursor: pointer;
}
fieldset label.switch { display: flex; }
input[type="radio"], input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }
.rules { font-size: var(--fs-sm); color: var(--muted); margin: 4px 0 14px; }
.rules summary { cursor: pointer; color: var(--text); }
.rules ul { padding-left: 18px; margin: 8px 0 0; }
.rules li { margin-bottom: 4px; }
.dialog-actions { display: flex; justify-content: space-between; gap: 8px; }
form.pvp .ai-only { display: none; }

@media (max-width: 380px) {
  .bar.bottom button { min-width: 58px; padding: 6px 6px; }
  .bar.top { gap: 10px; font-size: var(--fs-sm); }
}

