/* Đổi chữ — giao diện “vở ô li”: giấy kẻ ô, mực tím, bút đỏ chấm bài.
 * Xem DESIGN.md cho bảng màu, chữ và quy ước từng màn hình. */

:root {
  --paper: #fdfdf7;
  --grid: #dbe8ee;
  --grid-strong: #a8c6d4;
  --page-line: #dcebf1;      /* kẻ ô của nền trang: nhạt hơn ô chữ để không tranh với nội dung */
  --page-line-fine: #eff6f8;
  --ink: #3b2f9b;        /* mực tím: chữ chính, nút */
  --ink-deep: #231b5e;   /* chữ thân bài */
  --pencil: #5c6070;     /* chữ phụ */
  --red: #cf2f2a;        /* bút đỏ: lỗi, gấp, kết quả */
  --mark: #ffe96b;       /* bút dạ quang: chữ vừa đổi, lượt đang đi */
  --green: #1c7448;
  --cell: 32px;
  --tile: min(88px, calc((100vw - 64px) / 5));
  --hand: "Playwrite VN", "Segoe Script", "Bradley Hand", cursive;
  --sans: "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html { background: var(--paper); }
body {
  margin: 0;
  min-height: 100dvh;
  color: var(--ink-deep);
  font: 400 16px/1.6 var(--sans);
  /* Giấy ô li: ô lớn 32px, mỗi ô chia bốn dòng kẻ mờ. */
  background:
    linear-gradient(var(--page-line) 1px, transparent 1px) 0 0 / var(--cell) var(--cell),
    linear-gradient(90deg, var(--page-line) 1px, transparent 1px) 0 0 / var(--cell) var(--cell),
    linear-gradient(var(--page-line-fine) 1px, transparent 1px) 0 0 / calc(var(--cell) / 4) calc(var(--cell) / 4),
    linear-gradient(90deg, var(--page-line-fine) 1px, transparent 1px) 0 0 / calc(var(--cell) / 4) calc(var(--cell) / 4),
    var(--paper);
  background-attachment: fixed;
}

.page {
  width: min(960px, 100%);
  margin: 0 auto;
  padding: 0 24px 64px;
}
/* Lề đỏ của trang vở. */
@media (min-width: 720px) {
  .page {
    padding-left: 40px;
    border-left: 2px solid rgb(207 47 42 / .45);
    min-height: 100dvh;
  }
}

h1, h2, p { margin: 0; }
h1 {
  font: 400 clamp(24px, 4.2vw, 34px)/1.7 var(--hand);
  color: var(--ink);
}
h1[tabindex="-1"]:focus { outline: none; }
h2 { font: 600 17px/1.4 var(--sans); }
strong { font-weight: 600; }
code { font-size: .9em; background: #fff; padding: 1px 5px; border: 1px solid var(--grid-strong); border-radius: 4px; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}

/* ── Thanh trên ───────────────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 20px;
  padding: 20px 0 12px;
}
.logo {
  font: 400 26px/1.7 var(--hand);
  color: var(--ink);
  text-decoration: none;
  border-bottom: 2px solid var(--red);
}
.logo small { font: 500 12px var(--sans); color: var(--pencil); margin-left: 8px; }
.top-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 18px; }
.user-chip { display: inline-flex; align-items: center; gap: 8px; min-width: 0; }
.user-label { font-size: 13px; color: var(--pencil); overflow-wrap: anywhere; }
.avatar {
  flex: none; display: inline-grid; place-items: center; overflow: hidden;
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--mark); color: var(--ink-deep); border: 1.5px solid var(--grid-strong);
  font: 600 13px/1 var(--sans);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* ── Nút, ô nhập, thông báo ──────────────────────────────────────────────── */
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .5; }

.submit {
  min-height: 48px;
  padding: 0 22px;
  border: 0;
  border-radius: 8px;
  background: var(--ink);
  color: var(--paper);
  font-weight: 600;
  white-space: nowrap;
}
.submit:hover:not(:disabled) { background: var(--ink-deep); }

.text-button {
  padding: 6px 2px;
  border: 0;
  background: none;
  color: var(--ink);
  font-weight: 500;
  font-size: 14px;
  text-decoration: underline;
  text-decoration-color: var(--grid-strong);
  text-decoration-thickness: 2px;
  text-underline-offset: 5px;
}
.text-button:hover:not(:disabled) { text-decoration-color: var(--ink); }
.text-button.danger { color: var(--red); }
.text-button.danger:hover:not(:disabled) { text-decoration-color: var(--red); }

:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; border-radius: 4px; }
.submit:focus-visible { outline-color: var(--red); }

input[type=text], input[type=password], input[type=email], input:not([type]), select {
  width: 100%;
  min-height: 48px;
  padding: 10px 14px;
  border: 1.5px solid var(--ink);
  border-radius: 8px;
  background: #fff;
}
input::placeholder { color: #8589a0; }
input:disabled { border-color: var(--grid-strong); background: rgb(255 255 255 / .6); }
label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 6px; }
label span { font-weight: 400; color: var(--pencil); margin-left: 6px; }

.input-row { display: flex; gap: 10px; }
.input-row input { flex: 1; min-width: 0; font-size: 18px; }

.message { min-height: 1.6em; margin-top: 10px; font-size: 14px; color: var(--pencil); }
.message:empty { min-height: 0; margin-top: 0; }
.message.error { color: var(--red); font-weight: 500; }
.message.success { color: var(--green); font-weight: 500; }
.hint { font-size: 13px; color: var(--pencil); }

.panel {
  padding: 22px;
  background: #fff;
  border: 1.5px solid var(--grid-strong);
  border-radius: 12px;
}

/* ── Khung màn hình ──────────────────────────────────────────────────────── */
.view { padding-top: 24px; }
.view--narrow { max-width: 640px; }
.view-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

/* ── Ô chữ (dùng chung cho ví dụ ở trang đầu và bàn chơi) ─────────────────── */
.tiles { display: flex; justify-content: center; gap: 8px; }
.tile {
  display: grid;
  place-items: center;
  width: var(--tile);
  height: var(--tile);
  font: 600 calc(var(--tile) * .58)/1 var(--sans);
  color: var(--ink);
  /* Một ô li: viền đậm, bên trong chia bốn. */
  background:
    linear-gradient(var(--grid) 1px, transparent 1px) 0 -1px / 25% 25%,
    linear-gradient(90deg, var(--grid) 1px, transparent 1px) -1px 0 / 25% 25%,
    #fff;
  border: 1.5px solid var(--grid-strong);
  border-radius: 4px;
}
.tile.changed {
  background: var(--mark);
  border-color: var(--ink);
  box-shadow: 0 4px 0 var(--red);
}
@media (prefers-reduced-motion: no-preference) {
  /* Chỉ chạy khi có nước đi mới; vẽ lại cùng trạng thái không làm ô nháy lại. */
  .tile.changed.pop { animation: ink-in .32s ease-in-out both; }
  @keyframes ink-in {
    from { transform: translateY(-6px); background-color: #fff; }
    to { transform: none; background-color: var(--mark); }
  }
}

/* ── Trang đầu: thử một từ + đăng nhập ───────────────────────────────────── */
.try-word { max-width: 560px; margin: 24px auto 0; }
.try-word .tiles { margin-bottom: 28px; }
.try-word h1 { text-align: center; }
.lead { margin: 10px 0 22px; text-align: center; color: var(--pencil); }
.gate {
  max-width: 560px;
  margin: 36px auto 0;
  padding-top: 28px;
  border-top: 1.5px dashed var(--grid-strong);
  text-align: center;
}
.gate-heading { font-size: 20px; }
.gate-lead { margin: 6px 0 18px; color: var(--pencil); }
.gate .submit { width: min(320px, 100%); }
.gate.error { text-align: left; }

/* ── Sảnh ────────────────────────────────────────────────────────────────── */
.lobby-body { display: grid; gap: 28px; margin-top: 20px; }
@media (min-width: 800px) {
  .lobby-body { grid-template-columns: minmax(0, 1fr) 320px; align-items: start; }
}
@media (max-width: 799px) {
  .lobby-side { order: -1; }
}
.lobby-list-wrap h2 { margin-bottom: 12px; }
.empty {
  padding: 16px 18px;
  border: 1.5px dashed var(--grid-strong);
  border-radius: 12px;
  background: rgb(255 255 255 / .7);
  color: var(--pencil);
}
.lobby-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.lobby-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px 16px;
  padding: 14px 18px;
  background: #fff;
  border: 1.5px solid var(--grid-strong);
  border-radius: 12px;
}
.lobby-list li strong { display: block; font-size: 17px; overflow-wrap: anywhere; }
.lobby-list li span { font-size: 13px; color: var(--pencil); }
.lobby-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 10px 18px; }
.join-inline { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.join-password { width: 170px !important; }
.join-inline-error { flex: 1 1 100%; margin: 0; }

.lobby-create { display: grid; gap: 4px; }
.lobby-create h2 { margin-bottom: 10px; }
.lobby-create label { margin-top: 8px; }
.lobby-create .submit { margin-top: 16px; }
.field-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.my-room { border-color: var(--ink); }
.room-name { font: 400 22px/1.7 var(--hand); color: var(--ink); overflow-wrap: anywhere; margin: 4px 0 8px; }
.room-meta { color: var(--pencil); font-size: 14px; }
.my-room .lobby-actions { margin: 16px 0 14px; }

/* ── Phòng chờ ───────────────────────────────────────────────────────────── */
.waiting-count { margin-top: 20px; font-size: 20px; font-weight: 600; }
.player-cards {
  list-style: none; margin: 14px 0 0; padding: 0;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px;
}
.player-card {
  display: flex; align-items: center; gap: 12px; min-width: 0;
  padding: 12px 14px; background: #fff; border: 1.5px solid var(--grid-strong); border-radius: 12px;
}
.player-card.me { border-color: var(--ink); }
.player-card .avatar { width: 44px; height: 44px; font-size: 18px; }
.player-card div { min-width: 0; }
.player-card strong { display: block; font-size: 16px; overflow-wrap: anywhere; }
.player-card span:not(.avatar) { font-size: 13px; color: var(--pencil); }
#room-waiting .lobby-actions { margin-top: 16px; }
.rules { margin-top: 36px; padding-top: 22px; border-top: 1.5px dashed var(--grid-strong); }
.rules ul { margin: 10px 0 0; padding-left: 20px; }
.rules li { margin-bottom: 6px; }

/* ── Bàn chơi ────────────────────────────────────────────────────────────── */
.roster { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.player {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 6px 12px;
  background: #fff;
  border: 1.5px solid var(--grid-strong);
  border-radius: 8px;
  font-size: 14px;
}
.player strong { font-weight: 500; }
.player.me strong { font-weight: 700; }
.turn-badge { font-size: 12px; color: var(--pencil); }
.turn-badge:empty { display: none; }
.player.active { background: var(--mark); border-color: var(--ink); }
.player.active .turn-badge { color: var(--ink-deep); font-weight: 600; }
.player.eliminated { background: transparent; border-style: dashed; color: var(--pencil); }
.player.eliminated strong { text-decoration: line-through; }
.player.winner { border: 2px solid var(--red); }
.player.winner .turn-badge { color: var(--red); font-weight: 700; }

.board {
  margin-top: 16px;
  padding: 20px 22px 22px;
  background: #fff;
  border: 1.5px solid var(--grid-strong);
  border-radius: 12px;
}
.my-turn .board { border-color: var(--ink); box-shadow: 0 0 0 3px rgb(59 47 155 / .12); }
.turn-line { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; }
.turn-status { font-weight: 600; font-size: 17px; }
.my-turn .turn-status { color: var(--ink); }
.timer { display: flex; align-items: baseline; gap: 5px; color: var(--ink-deep); }
.timer strong { font: 700 34px/1 var(--sans); font-variant-numeric: tabular-nums; }
.timer span { font-size: 13px; color: var(--pencil); }
.timer-track { height: 6px; margin-top: 8px; background: var(--grid); border-radius: 3px; overflow: hidden; }
.timer-bar { height: 100%; width: 100%; background: var(--ink); transition: width .2s linear; }
.urgent .timer, .urgent .timer span { color: var(--red); }
.urgent .timer-bar { background: var(--red); }

.board .tiles { margin: 28px 0 8px; }
.word-meaning { min-height: 1.6em; text-align: center; font-size: 14px; color: var(--pencil); }
#move-form { margin-top: 18px; }
#move-form .submit { min-width: 108px; }

.result { margin-top: 18px; text-align: center; }
.result h2 { font: 400 clamp(24px, 5vw, 32px)/1.7 var(--hand); color: var(--red); }
.result p { margin: 4px 0 18px; }
.result p:empty { margin: 0 0 18px; }

.history { margin-top: 24px; }
.history h2 { display: flex; align-items: baseline; gap: 8px; }
.count { font-weight: 400; font-size: 14px; color: var(--pencil); }
.history-list {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 0;
  font-size: 17px;
  color: var(--ink);
}
.history-list li + li::before { content: "→"; margin: 0 8px; color: var(--grid-strong); }
.history-list li:last-child { font-weight: 700; }
.history-list li[title] { cursor: help; }
.board-foot { margin-top: 28px; }

/* ── Màn hình hẹp ────────────────────────────────────────────────────────── */
@media (max-width: 520px) {
  .page { padding: 0 16px 48px; }
  .board { padding: 16px 14px 18px; }
  .tiles { gap: 6px; }
  .input-row input { font-size: 16px; }
  #move-form .submit { min-width: 0; padding: 0 16px; }
  .lobby-list li { align-items: stretch; flex-direction: column; }
}
