:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --text: #1c1e22;
  --muted: #6b707a;
  --line: #d9d5cc;
  --x: #d9483a;
  --o: #2f7fc7;
  --accent: #1c1e22;
  --accent-text: #ffffff;
  --win: #f2c14e;
  --danger: #b3261e;
  --ok: #2e9e5b;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16181d;
    --surface: #20232a;
    --text: #eceef2;
    --muted: #9aa0ab;
    --line: #353944;
    --x: #f06b5d;
    --o: #5aa7ee;
    --accent: #eceef2;
    --accent-text: #16181d;
    --win: #b8902c;
    --danger: #ff8a80;
    --ok: #57c485;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }

.game {
  width: min(100%, 460px);
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* Placar */
.scoreboard {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
}
.player {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 8px 10px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 2px solid transparent;
  transition: border-color .2s;
}
.player.right { justify-content: flex-end; }
.player.turn { border-color: var(--text); }
.player.me .pname::after { content: " (você)"; color: var(--muted); font-weight: 400; }
.pname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; font-size: 14px; }
.player.right .pname { text-align: right; }
.sym { font-weight: 800; font-size: 18px; }
.sym.x, .cell.x { color: var(--x); }
.sym.o, .cell.o { color: var(--o); }
.pts { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); flex: none; }
.dot.on { background: var(--ok); }
.dot.off { background: var(--danger); }
.round { font-size: 12px; color: var(--muted); text-align: center; line-height: 1.2; max-width: 120px; }
/* Celular: o rótulo da rodada vai para a linha de baixo e os dois jogadores ganham a largura toda. */
@media (max-width: 520px) {
  .scoreboard { grid-template-columns: 1fr 1fr; row-gap: 6px; }
  .round { grid-column: 1 / -1; grid-row: 2; max-width: none; }
  .player { padding: 8px; gap: 6px; }
}

/* Status e relógio */
.status { text-align: center; font-weight: 600; min-height: 1.4em; }
.status.alert { color: var(--danger); }
.timer { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.timer .bar { height: 100%; width: 0; background: var(--text); transition: width .25s linear; }
.timer.low .bar { background: var(--danger); }
.timer.paused .bar { opacity: .35; }
.tolerance { text-align: center; font-size: 13px; color: var(--muted); }
:focus-visible { outline: 3px solid var(--o); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* Tabuleiro */
.board {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  gap: 8px;
  width: 100%;
  aspect-ratio: 1;
  max-height: 60dvh;
  margin: 0 auto;
}
.cell {
  appearance: none;
  border: none;
  border-radius: var(--radius);
  background: var(--surface);
  font: 800 clamp(44px, 15vw, 76px)/1 system-ui, sans-serif;
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
  transition: background .15s, transform .08s;
  box-shadow: 0 1px 0 var(--line);
}
.cell[aria-disabled="true"] { cursor: default; }
.cell.playable:hover { background: color-mix(in srgb, var(--surface), var(--text) 6%); }
.cell.playable:active { transform: scale(.97); }
.cell.pending { opacity: .5; }
.cell.win { background: var(--win); color: #1c1e22; }
.board.dim .cell { opacity: .55; }

/* Ações */
.panel { display: none; flex-direction: column; gap: 10px; }
.panel.show { display: flex; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }
.input {
  font: inherit;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--text);
  min-width: 0;
}
.input.code { text-transform: uppercase; letter-spacing: .2em; font-weight: 700; }
.btn {
  font: inherit;
  font-weight: 700;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-size: 17px; padding: 14px 16px; }
.btn.link { background: none; border: none; color: var(--muted); font-weight: 500; text-decoration: underline; padding: 6px; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }
.hint { margin: 0; text-align: center; color: var(--muted); }
.bigcode { text-align: center; font-size: 34px; font-weight: 800; letter-spacing: .25em; padding-left: .25em; }
.result { margin: 0; text-align: center; font-size: 20px; font-weight: 700; }

.history { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.history li { font-size: 12px; color: var(--muted); background: var(--surface); padding: 4px 8px; border-radius: 999px; }

.conn {
  position: sticky; top: 0; z-index: 5;
  background: var(--danger); color: #fff; text-align: center; font-size: 14px; padding: 6px;
}
.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600;
  max-width: calc(100% - 32px); z-index: 10;
}

@media (min-width: 760px) {
  .game { padding-top: 32px; gap: 16px; }
}

/* Página de privacidade */
.doc { width: min(100%, 760px); margin: 0 auto; padding: 24px 16px 48px; }
.doc h1 { font-size: 28px; margin: 8px 0; }
.doc h2 { font-size: 18px; margin: 24px 0 8px; }
.doc p, .doc li { max-width: 68ch; }
.doc a { color: var(--o); }
.doc .note { color: var(--muted); }
.doc table { width: 100%; border-collapse: collapse; font-size: 14px; background: var(--surface); border-radius: 10px; overflow: hidden; }
.doc th, .doc td { text-align: left; vertical-align: top; padding: 8px 10px; border-bottom: 1px solid var(--line); }
.doc th { color: var(--muted); font-weight: 600; }
@media (max-width: 560px) {
  .doc thead { display: none; }
  .doc table, .doc tbody, .doc tr, .doc td { display: block; width: 100%; }
  .doc tr { border-bottom: 1px solid var(--line); padding: 6px 0; }
  .doc td { border: 0; padding: 2px 10px; }
  .doc td:first-child { font-weight: 600; }
}
.footer { text-align: center; font-size: 13px; }
.footer a { color: var(--muted); }

/* ============================================================
   Fase 2A: navegação, lobby, histórico, ranking e conta
   ============================================================ */
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip { position: absolute; left: -999px; top: 8px; background: var(--text); color: var(--bg); padding: 8px 12px; border-radius: 8px; z-index: 20; }
.skip:focus { left: 8px; }
.small { font-size: 13px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.mt { margin-top: 20px; }
.err { color: var(--danger); min-height: 1.2em; margin: 0; font-size: 14px; }
.note { margin: 0; padding: 10px 12px; border-radius: 10px; background: color-mix(in srgb, var(--o) 10%, var(--surface)); font-size: 14px; }
.note a, .rules a { color: var(--o); }

.topbar {
  position: sticky; top: 0; z-index: 6;
  display: flex; align-items: center; gap: 12px;
  padding: max(8px, env(safe-area-inset-top)) 16px 8px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 6px; font-weight: 800; color: var(--text); text-decoration: none; font-size: 18px; }
.logo { width: 22px; height: 22px; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; fill: none; }
.nav { display: flex; gap: 2px; flex: 1; justify-content: center; }
.nav a, .acct {
  color: var(--muted); text-decoration: none; font-weight: 600; font-size: 15px;
  padding: 8px 12px; border-radius: 10px; min-height: 40px; display: inline-flex; align-items: center;
}
.nav a[aria-current="page"] { color: var(--text); background: var(--surface); box-shadow: 0 1px 0 var(--line); }
.acct { border: 1.5px solid var(--line); color: var(--text); max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.acct[aria-current="page"] { background: var(--surface); }
@media (max-width: 560px) {
  .topbar { gap: 6px; padding-left: 10px; padding-right: 10px; }
  .brand span { display: none; }
  .nav a, .acct { padding: 8px 9px; font-size: 14px; }
  .acct { max-width: 96px; }
}

.view.page { width: min(100%, 1040px); margin: 0 auto; padding: 16px 16px 32px; display: flex; flex-direction: column; gap: 14px; }
.view.page.narrow { width: min(100%, 520px); }
.view.page h1 { font-size: 26px; margin: 4px 0; }
.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; }
.card { background: var(--surface); border-radius: var(--radius); padding: 16px; box-shadow: 0 1px 0 var(--line); display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.card h2 { font-size: 17px; margin: 0; }
.card.empty { color: var(--muted); text-align: center; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 14px; color: var(--muted); font-weight: 600; }
.field .input { color: var(--text); font-weight: 400; }

/* Lobby */
.lobby-grid { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); gap: 14px; align-items: start; }
@media (max-width: 820px) { .lobby-grid { grid-template-columns: minmax(0, 1fr); } }
.resume { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 16px; border-radius: var(--radius); background: var(--accent); color: var(--accent-text); }
.resume .muted { color: color-mix(in srgb, var(--accent-text) 75%, transparent); display: block; font-size: 14px; }
.resume .btn.primary { background: var(--accent-text); color: var(--accent); border-color: var(--accent-text); }
.vis { border: 0; padding: 0; margin: 0; display: grid; gap: 8px; }
.vis legend { font-size: 14px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.opt { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border: 1.5px solid var(--line); border-radius: 12px; cursor: pointer; }
.opt:has(input:checked) { border-color: var(--text); }
.opt input { margin-top: 3px; accent-color: var(--text); width: 18px; height: 18px; }
.opt span { display: flex; flex-direction: column; gap: 2px; }
.opt small { color: var(--muted); font-size: 13px; }
.joinrow { display: flex; gap: 8px; }
.joinrow .input { flex: 1; min-width: 0; }
.list-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.rooms { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.rooms li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px; background: var(--bg); min-height: 52px; }
.rooms li.empty { color: var(--muted); justify-content: center; text-align: center; }
.rooms .who { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.rooms .who strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rooms .occ { font-variant-numeric: tabular-nums; font-size: 13px; color: var(--muted); }
.rooms .btn { padding: 8px 14px; }
.badge { display: inline-block; font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; border: 1px solid currentColor; white-space: nowrap; }
.badge.win { color: var(--ok); } .badge.loss { color: var(--danger); } .badge.none { color: var(--muted); }
.badge.ranked { color: var(--o); }

/* Segmentos (filtros e abas) */
.seg { display: inline-flex; flex-wrap: wrap; gap: 4px; background: var(--surface); padding: 4px; border-radius: 12px; box-shadow: 0 1px 0 var(--line); }
.seg button { font: inherit; font-size: 14px; font-weight: 600; border: 0; background: none; color: var(--muted); padding: 8px 12px; border-radius: 9px; cursor: pointer; min-height: 38px; }
.seg button.on { background: var(--accent); color: var(--accent-text); }

/* Histórico */
.hist { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.hist li { background: var(--surface); border-radius: var(--radius); box-shadow: 0 1px 0 var(--line); }
.hist button.row-btn { all: unset; box-sizing: border-box; width: 100%; cursor: pointer; display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 4px 12px; align-items: center; padding: 12px 14px; border-radius: var(--radius); }
.hist button.row-btn:focus-visible { outline: 3px solid var(--o); }
.hist .score { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; min-width: 64px; text-align: center; }
.hist .main { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.hist .main strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hist .side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.hist .rk { grid-column: 2 / -1; font-size: 13px; color: var(--muted); }
@media (max-width: 520px) { .hist .side { grid-column: 2 / -1; flex-direction: row; justify-content: flex-start; } }
.gate { align-items: flex-start; }
.pager { display: flex; justify-content: center; align-items: center; gap: 12px; }
dialog.detail { width: min(100% - 24px, 640px); max-height: 85dvh; border: 0; border-radius: var(--radius); padding: 16px; background: var(--bg); color: var(--text); }
dialog.detail::backdrop { background: rgb(0 0 0 / .45); }
.detail-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.detail-head h2 { margin: 0; font-size: 18px; }
.boards { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; margin-top: 10px; }
.mini-board { background: var(--surface); border-radius: 12px; padding: 10px; display: flex; flex-direction: column; gap: 6px; }
.mini-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 3px; aspect-ratio: 1; max-width: 120px; }
.mini-grid span { background: var(--bg); border-radius: 5px; display: grid; place-items: center; font-weight: 800; font-size: 18px; }
.mini-grid span.x { color: var(--x); } .mini-grid span.o { color: var(--o); }
.mini-board ol { margin: 0; padding-left: 18px; font-size: 12px; color: var(--muted); }

/* Ranking */
.tablewrap { overflow-x: auto; background: var(--surface); border-radius: var(--radius); box-shadow: 0 1px 0 var(--line); }
table.rank { width: 100%; border-collapse: collapse; font-size: 15px; }
.rank th, .rank td { padding: 10px 12px; text-align: right; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.rank th { font-size: 13px; color: var(--muted); font-weight: 600; }
.rank th:nth-child(2), .rank td:nth-child(2) { text-align: left; width: 100%; }
.rank td:nth-child(2) { font-weight: 600; max-width: 1px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank tr.you td { background: color-mix(in srgb, var(--o) 12%, var(--surface)); }
.rank tr.you td:nth-child(2)::after { content: " (você)"; color: var(--muted); font-weight: 400; }
.rank tr:last-child td { border-bottom: 0; }
@media (max-width: 520px) { .rank th:nth-child(5), .rank td:nth-child(5) { display: none; } .rank th, .rank td { padding: 10px 8px; } }
.me-card { display: grid; grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); gap: 8px; }
.me-card .stat { display: flex; flex-direction: column; }
.me-card .stat b { font-size: 24px; font-variant-numeric: tabular-nums; }
.me-card .stat span { font-size: 13px; color: var(--muted); }
.me-card .title { grid-column: 1 / -1; font-weight: 700; }
.rules { background: var(--surface); border-radius: var(--radius); padding: 12px 16px; box-shadow: 0 1px 0 var(--line); }
.rules summary { cursor: pointer; font-weight: 700; min-height: 32px; display: flex; align-items: center; }
.rules li { margin: 6px 0; max-width: 72ch; }

/* Conta */
.form { gap: 12px; }
.profile { flex-direction: row; align-items: center; gap: 14px; }
.profile div { display: flex; flex-direction: column; min-width: 0; }
.avatar { width: 52px; height: 52px; border-radius: 50%; background: var(--accent); color: var(--accent-text); display: grid; place-items: center; font-size: 22px; font-weight: 800; flex: none; }
#authBox, #profileBox { display: flex; flex-direction: column; gap: 12px; }
#authBox[hidden], #profileBox[hidden] { display: none; }

/* Partida: foco no tabuleiro */
.view.game { padding-top: 12px; }
.footer { padding: 12px 16px max(16px, env(safe-area-inset-bottom)); }
a.btn { text-decoration: none; text-align: center; display: inline-block; }

/* Ajustes após revisão visual */
.card p { margin: 0; }
.mini-grid { grid-template-rows: repeat(3, minmax(0, 1fr)); width: 100%; }
/* Sem estado de partida (sessão encerrada, sala cheia, encerrada): só a mensagem e as ações. */
.view.game.blocked .scoreboard, .view.game.blocked .timer, .view.game.blocked .board,
.view.game.blocked .tolerance, .view.game.blocked .history, .view.game.blocked .status { display: none; }
.view.game.blocked .actions { margin-top: 24px; background: var(--surface); border-radius: var(--radius); padding: 20px 16px; box-shadow: 0 1px 0 var(--line); }
@media (max-width: 820px) {
  /* Celular: com salas públicas disponíveis, elas vêm antes do formulário de criar. */
  .lobby-grid.has-rooms .rooms-card { order: -1; }
}
@media (max-width: 560px) {
  .seg { flex-wrap: nowrap; overflow-x: auto; max-width: 100%; }
  .seg button { flex: none; padding: 8px 10px; }
  .player.me .pname::after { content: ""; }
  .player.me { box-shadow: inset 0 -3px 0 var(--o); }
}

/* Fase 2B: a faixa de conexão fica sobreposta e NÃO desloca o layout (um deslocamento entre
   mirar e clicar fazia cliques caírem fora do botão). */
.conn { position: fixed; left: 0; right: 0; top: 0; z-index: 30; padding-top: max(6px, env(safe-area-inset-top)); }
dialog.confirm { border: 0; border-radius: var(--radius); padding: 20px; width: min(100% - 32px, 380px); background: var(--surface); color: var(--text); }
dialog.confirm::backdrop { background: rgb(0 0 0 / .45); }
dialog.confirm p { margin: 0 0 16px; font-weight: 600; font-size: 17px; }
.btn.danger-fill { background: var(--danger); border-color: var(--danger); color: #fff; }

/* Fase 2B: altura fixa das linhas que mudam de texto durante a partida. Antes, o status alternava
   entre 1 e 2 linhas ("Sua vez" / "Relógio pausado: … está desconectado") e empurrava o tabuleiro e
   o botão Desistir entre o momento de mirar e o de tocar. */
.status { min-height: 2.8em; display: flex; align-items: center; justify-content: center; line-height: 1.4; }
.tolerance { min-height: 1.4em; }
