*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  font-family: system-ui;
  margin: 0;
}

:root {
  --sq-light: #f0d9b5;
  --sq-dark: #b58863;
  --wood: #dcb35c;
  --wood-line: #5b3a12;
  --hl-last: rgba(255, 235, 59, 0.45);
  --hl-selected: rgba(76, 175, 80, 0.55);
  --hl-target: rgba(0, 0, 0, 0.28);
  --hl-win: #fbc02d;
}

.board {
  display: block;
  width: 100%;
  height: auto;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.board .sq-light {
  fill: var(--sq-light);
}

.board .sq-dark {
  fill: var(--sq-dark);
}

.board .wood {
  fill: var(--wood);
}

.board .grid-line {
  stroke: var(--wood-line);
  stroke-width: 0.04;
}

.board .hl-last {
  fill: var(--hl-last);
}

.board .hl-selected {
  fill: var(--hl-selected);
}

.board .hl-target {
  fill: var(--hl-target);
}

.board .hl-win {
  fill: none;
  stroke: var(--hl-win);
  stroke-width: 0.1;
}

.board .piece-x {
  stroke: #d32f2f;
  stroke-width: 0.09;
  stroke-linecap: round;
}

.board .piece-o {
  fill: none;
  stroke: #1565c0;
  stroke-width: 0.09;
}

.board .dragging {
  pointer-events: none;
}

.board .hl-target-capture {
  fill: none;
  stroke: var(--hl-target);
  stroke-width: 0.1;
  pointer-events: none;
}

.board .hl-target {
  pointer-events: none;
}

/* Trang chủ */

button {
  font: inherit;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.home {
  max-width: 560px;
  margin: 0 auto;
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 88px;
  padding: 12px;
  border: 2px solid #c9c9c9;
  border-radius: 8px;
  background: #fff;
}

.card-name {
  font-size: 1.1rem;
  font-weight: 600;
}

.card.active,
.mode.active,
.size-picker button.active {
  border-color: #2e7d32;
  background: #e8f5e9;
}

.badge {
  font-size: 0.75rem;
  color: #666;
}

.modes {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.mode,
.size-picker button {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border: 2px solid #c9c9c9;
  border-radius: 8px;
  background: #fff;
}

.options {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
}

.size-picker {
  display: flex;
  gap: 8px;
}

.start {
  padding: 12px;
  border: none;
  border-radius: 8px;
  background: #2e7d32;
  color: #fff;
  font-weight: 600;
}

/* Màn hình chơi */

.play {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 24px;
  padding: 16px;
}

.board-wrap {
  width: min(92vw, 80vh);
  flex: none;
}

.panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 260px;
  max-height: min(92vw, 80vh);
}

.panel h2 {
  margin: 0;
  font-size: 1rem;
}

.home-link {
  color: #1565c0;
}

.turn {
  font-weight: 600;
}

.banner {
  padding: 8px 12px;
  border-radius: 8px;
  background: #fff3cd;
  font-weight: 600;
}

.buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.buttons button {
  padding: 6px 12px;
  border: 1px solid #c9c9c9;
  border-radius: 6px;
  background: #fff;
}

.moves {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow-y: auto;
  flex: 1;
  min-height: 80px;
  font-variant-numeric: tabular-nums;
}

.moves li {
  padding: 2px 0;
}

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
}

.popup {
  padding: 16px;
  border-radius: 10px;
  background: #fff;
}

.popup-title {
  margin-bottom: 8px;
  font-weight: 600;
  text-align: center;
}

.popup-pieces {
  display: flex;
  gap: 8px;
}

.promo-piece {
  width: 64px;
  height: 64px;
  padding: 0;
  border: 2px solid #c9c9c9;
  border-radius: 8px;
  background: var(--sq-light);
}

.promo-piece img {
  width: 100%;
  height: 100%;
}

.not-found {
  padding: 24px 16px;
  text-align: center;
  font-size: 1.2rem;
}

/* Trang chủ: tùy chọn Online */

.online-heading {
  margin: 0;
  font-size: 1.05rem;
}

.size-picker {
  flex-wrap: wrap;
}

.custom-tc {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.number-field,
.nickname-field {
  display: flex;
  align-items: center;
  gap: 8px;
}

.number-field input {
  width: 5em;
}

.nickname-field input {
  flex: 1;
}

.nickname-field input,
.invite-link {
  min-width: 0;
  padding: 8px 10px;
  border: 2px solid #c9c9c9;
  border-radius: 8px;
  font: inherit;
}

.error {
  margin: 0;
  color: #c62828;
  font-weight: 600;
}

.waking {
  margin: 0;
  color: #555;
}

/* Phòng online */

.room-message,
.room-waiting {
  max-width: 560px;
  margin: 0 auto;
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  text-align: center;
  font-size: 1.1rem;
}

.room-message p {
  margin: 0;
}

.join-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.room-code-line {
  display: flex;
  justify-content: center;
  align-items: baseline;
  gap: 8px;
}

.room-code {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  font-variant-numeric: tabular-nums;
}

.waiting {
  margin: 0;
  color: #555;
}

.reconnecting {
  padding: 6px 12px;
  background: #fff3cd;
  text-align: center;
  font-weight: 600;
}

.board-col {
  /* Trừ chỗ cho hai thẻ người chơi để cả cột vừa màn hình. */
  width: min(92vw, calc(80vh - 96px));
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.board-col .board-wrap {
  width: 100%;
}

.player-card {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 42px;
  padding: 6px 10px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  background: #fff;
}

.player-card.to-move {
  border-color: #2e7d32;
}

.conn-dot {
  flex: none;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #9e9e9e;
}

.conn-dot.conn-on {
  background: #43a047;
}

.player-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}

.player-side {
  color: #666;
  font-size: 0.9rem;
}

.clock {
  padding: 2px 8px;
  border-radius: 6px;
  background: #eee;
  font-size: 1.15rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.player-card.to-move .clock {
  background: #2e7d32;
  color: #fff;
}

.spectating {
  align-self: flex-start;
  padding: 2px 8px;
  border-radius: 6px;
  background: #e3f2fd;
  color: #1565c0;
}

.draw-offer {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  border-radius: 8px;
  background: #e8f5e9;
}

.draw-offered,
.rematch-requested {
  font-weight: 600;
}

.buttons button.resign.confirm {
  border-color: #c62828;
  background: #c62828;
  color: #fff;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 8px;
  background: rgba(33, 33, 33, 0.92);
  color: #fff;
  z-index: 10;
}

@media (max-width: 700px) {
  .play {
    flex-direction: column;
    align-items: center;
    padding: 8px 0;
  }

  .board-wrap {
    /* 100% (không phải 100vw) để thanh cuộn không gây tràn ngang; 80vh để màn ngang vẫn thấy bảng. */
    width: min(100%, 80vh);
  }

  .panel {
    width: 100%;
    max-height: none;
    padding: 0 16px;
  }

  .board-col {
    /* Bàn rộng hết chiều ngang; thẻ người chơi (có đồng hồ) nằm ngay trên và dưới bàn. */
    width: min(100%, calc(80vh - 96px));
  }

  .player-card {
    margin: 0 8px;
  }
}
