:root {
  --bg: #151923; --panel: #242c3a; --cell: #303a4b; --grid: #6c7a90;
  --text: #eef3fb; --muted: #aeb9c9; --x: #ff6559; --o: #55a7ff;
  --ghost: #8f8f8f; --accent: #f2c94c; --ok: #75d69b; --danger: #e05656;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; justify-content: center;
}
#app { width: 100%; max-width: 480px; padding: 12px 16px 24px; }
#topbar { display: flex; justify-content: space-between; align-items: center; padding: 4px 0 12px; }
#game-title { font-weight: 800; letter-spacing: 2px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.hidden { display: none !important; }
.card { background: var(--panel); border-radius: 12px; padding: 20px; text-align: center; }
.stat { display: flex; justify-content: space-between; padding: 6px 2px; font-size: 18px; }
.label { color: var(--muted); }
.big { font-size: 28px; font-weight: 700; }
button {
  background: var(--cell); color: var(--text); border: 1px solid var(--grid);
  border-radius: 10px; padding: 12px 18px; font-size: 16px; margin-top: 12px;
  width: 100%; cursor: pointer;
}
button.primary { background: var(--accent); color: #151923; font-weight: 700; border: none; }
button.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
button.challenge { background: transparent; color: var(--accent); border-color: var(--accent); }
button.okbtn { background: transparent; color: var(--ok); border-color: var(--ok); }
button.small { width: auto; padding: 8px 14px; font-size: 14px; margin: 6px 4px 0; }
button:disabled { opacity: 0.45; cursor: default; }
#scorebar { display: flex; justify-content: space-between; align-items: center; margin: 4px 0; }
.side { display: flex; flex-direction: column; min-width: 90px; }
.side.right { text-align: right; }
.rating { color: var(--muted); font-size: 13px; }
.dots { color: var(--accent); font-size: 15px; letter-spacing: 2px; }
#board { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 12px 0; }
.cell {
  aspect-ratio: 1; background: var(--cell); border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 52px; font-weight: 800; cursor: pointer; user-select: none;
}
.cell.x { color: var(--x); }
.cell.o { color: var(--o); }
/* Fading mark: blinks until its owner moves, stays fully playable. */
.cell.fading { animation: fading-blink 0.9s ease-in-out infinite; }
@keyframes fading-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}
.cell.locked { cursor: default; }
.cell.win { outline: 3px solid var(--accent); background: #4a4430; }
.won-text { color: var(--ok); font-weight: 700; }
.lost-text { color: var(--danger); font-weight: 700; }
.miniboard {
  display: grid; grid-template-columns: repeat(3, 22px);
  gap: 2px; margin: 8px 0; justify-content: start;
}
.minicell {
  width: 22px; height: 22px; background: var(--cell); border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 700;
}
.minicell.win { outline: 2px solid var(--accent); }
#game-status { min-height: 20px; }
/* Turn highlight: my turn bold green, opponent's turn yellow. */
#game-status.my-turn { color: var(--ok); font-weight: 800; }
#game-status.opp-turn { color: var(--accent); font-weight: 700; }
/* Move timer: last 10s warn that the server forfeits the game on timeout. */
#move-timer.warn { color: var(--danger); font-weight: 800; }
.history-item {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--cell); border-radius: 10px; padding: 10px 12px;
  margin: 6px 0; cursor: pointer; font-size: 15px;
}
.history-item.won { border-left: 4px solid var(--ok); }
.history-item.lost { border-left: 4px solid var(--danger); }
.history-item.live { border-left: 4px solid var(--accent); }
.history-item.void { border-left: 4px solid var(--grid); cursor: default; opacity: 0.75; }
/* History rows: 3-column flex per spec (left/center/right). */
.hrow { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; }
.hrow .hdur { color: var(--text); font-weight: 700; white-space: nowrap; flex: 0 0 auto; }
.hrow.hsub { color: var(--text); font-weight: 700; margin-top: 2px; }
.hrow.hsub span:nth-child(2) { font-size: 17px; flex: 0 0 auto; }
/* Narrow phones: shrink, never wrap mid-token, ellipsis on nicks. */
.history-item { font-size: 14px; }
.history-item .hrow > span { min-width: 0; }
.history-item .hrow > span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-item .hsub span:first-child,
.history-item .hsub span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 38%; }
.history-item .muted.small { white-space: nowrap; }
#history-search {
  width: 100%; padding: 10px 12px; font-size: 15px; border-radius: 10px;
  border: 1px solid var(--grid); background: var(--cell); color: var(--text);
  margin-bottom: 8px;
}
.history-detail { text-align: left; font-size: 14px; }
.history-detail .row { display: flex; justify-content: space-between; padding: 3px 0; }
/* Friends: tappable rows with inline context actions (thumb-friendly). */
.friend-row {
  background: var(--cell); border-radius: 10px; padding: 10px 12px;
  margin: 6px 0; cursor: pointer; font-size: 15px; text-align: left;
}
.friend-actions { display: flex; gap: 4px; margin-top: 6px; }
/* Center modal for invites/challenges (PC + phones). */
#modal-overlay {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
#modal-card {
  background: var(--panel); border-radius: 12px; padding: 20px;
  max-width: 320px; width: calc(100% - 48px); text-align: center;
}
#modal-buttons { display: flex; gap: 8px; }
#modal-buttons button { width: auto; flex: 1; }
#private-code {
  font-size: 34px; font-weight: 800; letter-spacing: 6px; margin: 8px 0;
}
#private-join-input {
  width: 100%; padding: 12px; font-size: 18px; text-align: center;
  letter-spacing: 4px; text-transform: uppercase; border-radius: 10px;
  border: 1px solid var(--grid); background: var(--cell); color: var(--text);
  margin-top: 12px;
}
/* Accept squares: empty → green ✓ (accepted) / red ✗ (declined). */
#accept-row { display: flex; gap: 8px; align-items: center; justify-content: center; margin: 6px 0; }
.acc-box {
  width: 30px; height: 30px; border-radius: 8px; border: 2px solid var(--grid);
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 800; color: transparent; background: var(--cell);
}
.acc-box.ok { border-color: var(--ok); color: var(--ok); }
.acc-box.no { border-color: var(--danger); color: var(--danger); }
/* Sticky back buttons (History/Friends): long lists scroll, the way back
   stays pinned to the viewport bottom while its card is visible. */
#btn-history-back, #btn-friends-back {
  position: sticky; bottom: 12px; z-index: 5;
  background: var(--cell);
  box-shadow: 0 -6px 14px rgba(0, 0, 0, 0.45);
}
