/* ---------- Design tokens ---------- */
:root {
  --bg: #0b0d16;
  --bg-elevated: #131628;
  --bg-card: #171b30;
  --border: #262b47;
  --text: #f2f3fb;
  --text-dim: #9096b8;
  --accent: #6d5bff;
  --accent-2: #00e5c7;
  --x-color: #6d9bff;
  --o-color: #ff7a9c;
  --win: #ffd35a;
  --danger: #ff5470;
  --success: #2ee6a6;
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --tg-safe-top: env(safe-area-inset-top, 0px);
  --tg-safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  user-select: none;
  overflow-x: hidden;
}

img, svg { -webkit-user-drag: none; user-drag: none; }

#app {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 520px;
  margin: 0 auto;
  position: relative;
  background: radial-gradient(circle at 50% -10%, #1b1f3a 0%, var(--bg) 55%);
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(14px + var(--tg-safe-top)) 16px 14px;
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(11, 13, 22, 0.85);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.2px;
}

.coin-pill {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  padding: 5px 11px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 13px;
  transition: transform 0.15s ease;
}
.coin-pill.bump { animation: coinBump 0.35s ease; }
@keyframes coinBump {
  0% { transform: scale(1); }
  40% { transform: scale(1.18); }
  100% { transform: scale(1); }
}

.icon-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  width: 38px;
  height: 38px;
  border-radius: 12px;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.2s ease;
}
.icon-btn:active { transform: scale(0.9); background: var(--bg-elevated); }

/* ---------- Screens ---------- */
#screens {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.screen {
  position: absolute;
  inset: 0;
  padding: 16px 16px calc(18px + var(--tg-safe-bottom));
  overflow-y: auto;
  display: none;
  flex-direction: column;
  opacity: 0;
}

.screen.active {
  display: flex;
  animation: screenIn 0.28s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

.screen.leaving {
  display: flex;
  animation: screenOut 0.2s ease forwards;
}

@keyframes screenIn {
  from { opacity: 0; transform: translateY(14px) scale(0.99); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes screenOut {
  from { opacity: 1; transform: translateY(0) scale(1); }
  to { opacity: 0; transform: translateY(-10px) scale(0.99); }
}

.screen-title {
  font-size: 19px;
  font-weight: 800;
  margin: 2px 0 14px;
}

.hint-text {
  color: var(--text-dim);
  font-size: 13px;
  margin: 0 0 14px;
}

/* ---------- Home ---------- */
.home-hero {
  display: flex;
  justify-content: center;
  margin: 4px 0 20px;
}

.hero-board {
  display: grid;
  grid-template-columns: repeat(3, 38px);
  grid-template-rows: repeat(3, 38px);
  gap: 7px;
  padding: 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
}
.hero-board span {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 17px;
  border-radius: 9px;
  background: var(--bg-elevated);
  animation: heroPulse 3.2s ease-in-out infinite;
}
.hero-board span:nth-child(odd) { color: var(--x-color); }
.hero-board span:nth-child(even) { color: var(--o-color); animation-delay: 0.3s; }
@keyframes heroPulse {
  0%, 100% { opacity: 0.55; transform: scale(0.96); }
  50% { opacity: 1; transform: scale(1); }
}

.menu-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.menu-btn {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  padding: 14px 16px;
  font-size: 14.5px;
  font-weight: 700;
  color: var(--text);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: transform 0.12s ease, background 0.2s ease, box-shadow 0.2s ease;
}
.menu-btn:active { transform: scale(0.97); }
.menu-btn.primary {
  background: linear-gradient(135deg, var(--accent), #4a3bd6);
  border-color: transparent;
  box-shadow: 0 10px 24px rgba(109, 91, 255, 0.35);
}
.menu-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.menu-icon { font-size: 18px; }

.or-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-dim);
  font-size: 12px;
  margin: 16px 0 12px;
}
.or-divider::before, .or-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ---------- Find player ---------- */
.search-row {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}
.search-row input {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 11px 13px;
  border-radius: var(--radius-sm);
  font-size: 14px;
}
.search-row input:focus { outline: none; border-color: var(--accent); }

.player-list { display: flex; flex-direction: column; gap: 10px; }

.player-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  animation: rowIn 0.25s ease both;
}
@keyframes rowIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.avatar {
  position: relative;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 14px;
  flex-shrink: 0;
  color: #0b0d16;
}

.online-dot {
  position: absolute;
  bottom: -1px;
  right: -1px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--success);
  border: 2px solid var(--bg-card);
}

.online-label {
  display: inline-block;
  font-size: 9.5px;
  font-weight: 800;
  color: var(--success);
  background: rgba(46, 230, 166, 0.12);
  padding: 1px 6px;
  border-radius: 999px;
  vertical-align: middle;
  margin-left: 2px;
}

.player-info { flex: 1; min-width: 0; }
.player-info .name { font-weight: 700; font-size: 14px; }
.player-info .meta { color: var(--text-dim); font-size: 11.5px; margin-top: 2px; }

.challenge-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 8px 13px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 12.5px;
  cursor: pointer;
  transition: transform 0.12s ease;
}
.challenge-btn:active { transform: scale(0.92); }
.challenge-btn:disabled {
  background: var(--border);
  color: var(--text-dim);
  cursor: not-allowed;
}

.empty-state {
  color: var(--text-dim);
  text-align: center;
  margin-top: 40px;
  font-size: 14px;
}

/* ---------- Wallet ---------- */
.wallet-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.wallet-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 18px 10px;
  text-align: center;
}
.wallet-icon { font-size: 22px; margin-bottom: 6px; }
.wallet-value { font-size: 22px; font-weight: 800; }
.wallet-label { color: var(--text-dim); font-size: 11px; margin-top: 4px; }

/* ---------- Buy Coins (shop) ---------- */
.buy-coins-btn {
  margin-top: 16px;
  background: linear-gradient(135deg, #ffb238, #ff7a3d);
  border-color: transparent;
  box-shadow: 0 10px 24px rgba(255, 140, 56, 0.3);
}

.shop-list { display: flex; flex-direction: column; gap: 10px; }

.shop-pack {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  animation: rowIn 0.25s ease both;
}
.shop-pack .pack-icon { font-size: 22px; flex-shrink: 0; }
.shop-pack .pack-info { flex: 1; min-width: 0; }
.shop-pack .pack-coins { font-weight: 800; font-size: 15px; }
.shop-pack .pack-stars { color: var(--text-dim); font-size: 12px; margin-top: 2px; }
.shop-pack .pack-buy-btn {
  background: linear-gradient(135deg, #ffb238, #ff7a3d);
  color: #1a1200;
  border: none;
  padding: 9px 16px;
  border-radius: 999px;
  font-weight: 800;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: transform 0.12s ease;
}
.shop-pack .pack-buy-btn:active { transform: scale(0.92); }
.shop-pack .pack-buy-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Leaderboard ---------- */
.leaderboard-list { display: flex; flex-direction: column; gap: 8px; }
.leaderboard-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  animation: rowIn 0.25s ease both;
}
.leaderboard-row .rank {
  width: 26px;
  font-weight: 800;
  color: var(--text-dim);
  text-align: center;
}
.leaderboard-row.top1 .rank { color: #ffd35a; }
.leaderboard-row.top2 .rank { color: #cfd4e6; }
.leaderboard-row.top3 .rank { color: #d18a52; }
.leaderboard-row .lb-name { flex: 1; font-weight: 700; font-size: 13px; }
.leaderboard-row .lb-wins { font-weight: 800; font-size: 13px; color: var(--success); }

/* ---------- Waiting ---------- */
.screen-waiting-center, .waiting-box {
  margin: auto;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}
.spinner {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Game screen ----------
   The whole screen is a fixed-height flex column (header / turn text /
   board / footer). The board sits in a flexible middle region that eats
   whatever vertical space is left, and the board itself is sized from
   that available height (not just viewport width), so on short screens
   it shrinks to fit rather than pushing content below the fold. */
#screen-game {
  justify-content: flex-start;
  overflow: hidden;
  height: 100%;
  gap: 6px;
}

.game-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex: 0 0 auto;
}
.player-chip {
  display: flex;
  align-items: center;
  gap: 7px;
  flex: 1;
  min-width: 0;
}
.player-chip.opp { flex-direction: row-reverse; text-align: right; }
.player-chip .avatar { width: 32px; height: 32px; font-size: 13px; }
.player-name {
  font-weight: 700;
  font-size: 12.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.vs-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex: 0 0 auto;
}
.vs { font-weight: 800; color: var(--text-dim); font-size: 11px; }
.quit-btn {
  width: 28px;
  height: 28px;
  font-size: 13px;
  background: rgba(255, 84, 112, 0.12);
  border-color: rgba(255, 84, 112, 0.3);
}
.quit-btn:active { background: rgba(255, 84, 112, 0.25); }

.turn-indicator {
  text-align: center;
  font-weight: 700;
  font-size: 13px;
  color: var(--accent-2);
  flex: 0 0 auto;
  min-height: 16px;
  transition: opacity 0.15s ease;
}

.board-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 0;
}

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: min(2.5vmin, 10px);
  height: 100%;
  width: auto;
  aspect-ratio: 1 / 1;
  max-width: min(92vw, 400px);
  max-height: 100%;
}

.cell {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: clamp(20px, 8vmin, 40px);
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: transparent;
  position: relative;
  overflow: hidden;
  min-width: 0;
  min-height: 0;
}
.cell:active:not(.filled) { transform: scale(0.94); }
.cell.filled { cursor: default; }

.cell.mark-x { color: var(--x-color); animation: markPop 0.28s cubic-bezier(0.34, 1.56, 0.64, 1); }
.cell.mark-o { color: var(--o-color); animation: markPop 0.28s cubic-bezier(0.34, 1.56, 0.64, 1); }
@keyframes markPop {
  0% { transform: scale(0.2) rotate(-15deg); opacity: 0; }
  100% { transform: scale(1) rotate(0); opacity: 1; }
}

.cell.win-cell {
  background: rgba(255, 211, 90, 0.18);
  border-color: var(--win);
  animation: winPulse 0.9s ease-in-out infinite;
}
@keyframes winPulse {
  0%, 100% { box-shadow: 0 0 0 rgba(255, 211, 90, 0); }
  50% { box-shadow: 0 0 22px rgba(255, 211, 90, 0.55); }
}

.game-footer {
  flex: 0 0 auto;
  text-align: center;
  padding-top: 2px;
}
.stake-note { color: var(--text-dim); font-size: 12px; }

/* ---------- Reactions (never sent to the server database — relayed live
   between the two players only, for the duration of the match) ---------- */
.reaction-bar {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
  gap: 6px;
  padding: 2px 0 4px;
}
.reaction-btn {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-card);
  font-size: 17px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.2s ease;
}
.reaction-btn:active { transform: scale(0.85); background: var(--bg-elevated); }

.flying-reaction {
  position: absolute;
  left: 50%;
  bottom: 10%;
  font-size: 34px;
  transform: translateX(-50%);
  pointer-events: none;
  animation: reactionFly 1.4s ease-out forwards;
  z-index: 30;
}
@keyframes reactionFly {
  0% { transform: translate(-50%, 0) scale(0.5); opacity: 0; }
  15% { transform: translate(-50%, -10px) scale(1.2); opacity: 1; }
  100% { transform: translate(-50%, -140px) scale(1) rotate(8deg); opacity: 0; }
}

/* ---------- Countdown ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(6, 7, 14, 0.72);
  backdrop-filter: blur(4px);
}
.overlay[hidden] { display: none; }

.countdown-number {
  font-size: min(100px, 26vh);
  font-weight: 900;
  color: var(--accent-2);
  text-shadow: 0 0 40px rgba(0, 229, 199, 0.6);
  animation: countdownPop 0.9s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes countdownPop {
  0% { transform: scale(0.3); opacity: 0; }
  40% { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}
.countdown-number.go {
  font-size: min(74px, 20vh);
  color: var(--success);
}

/* ---------- Modal ---------- */
.modal-overlay { padding: 20px; }
.modal {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 26px 22px;
  width: 100%;
  max-width: 320px;
  text-align: center;
  box-shadow: var(--shadow);
  animation: modalIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  max-height: 90vh;
  overflow-y: auto;
}
@keyframes modalIn {
  from { opacity: 0; transform: scale(0.85) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}
.result-emoji { font-size: 44px; margin-bottom: 6px; animation: bounce 0.6s ease; }
@keyframes bounce {
  0% { transform: translateY(-14px); opacity: 0; }
  60% { transform: translateY(4px); }
  100% { transform: translateY(0); opacity: 1; }
}
.result-title { font-size: 19px; font-weight: 800; margin-bottom: 6px; }
.result-sub { color: var(--text-dim); font-size: 13px; margin-bottom: 12px; }
.coin-change {
  font-size: 17px;
  font-weight: 800;
  margin-bottom: 14px;
}
.coin-change.positive { color: var(--success); animation: coinFloatUp 0.6s ease; }
.coin-change.negative { color: var(--danger); animation: coinFloatDown 0.6s ease; }
@keyframes coinFloatUp {
  from { transform: translateY(10px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
@keyframes coinFloatDown {
  from { transform: translateY(-10px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
.modal-actions { display: flex; flex-direction: column; gap: 10px; margin-top: 6px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(28px + var(--tg-safe-bottom));
  transform: translateX(-50%);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  z-index: 200;
  box-shadow: var(--shadow);
  max-width: 88%;
  text-align: center;
  animation: toastIn 0.25s ease, toastOut 0.25s ease 2.6s forwards;
}
.toast[hidden] { display: none; }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 12px); } to { opacity: 1; transform: translate(-50%, 0); } }
@keyframes toastOut { to { opacity: 0; transform: translate(-50%, 8px); } }

/* ---------- Loading ---------- */
.loading-overlay { background: var(--bg); z-index: 500; }
.loader-ring {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 4px solid var(--border);
  border-top-color: var(--accent-2);
  animation: spin 0.8s linear infinite;
}

/* ---------- Misc ---------- */
::selection { background: var(--accent); color: #fff; }

@media (max-width: 340px) {
  .countdown-number { font-size: 90px; }
}

/* Short viewports (small phones, split-screen, landscape) — trim vertical
   chrome further so the board never needs the screen to scroll. */
@media (max-height: 640px) {
  .screen { padding-top: 10px; }
  .home-hero { margin: 0 0 12px; }
  .screen-title { margin: 0 0 10px; }
  .turn-indicator { min-height: 14px; }
}
@media (max-height: 560px) {
  .game-header { display: none; }
  .board { max-height: 100%; }
}
