:root {
  --bg: #14171f;
  --panel: #1e2230;
  --panel-2: #262b3c;
  --panel-3: #2f3548;
  --accent: #5b8dff;
  --accent-dim: #3d5fb0;
  --accent-2: #ff5d6c;
  --accent-2-dim: #b1414c;
  --text: #f1f3f9;
  --muted: #96a0b8;
  --cell: #333a4f;
  --wall: #e3b256;
  --wall-dark: #b98a35;
  --wall-hover: #ffd479;
  --green: #3ecb85;
  --border: #363c52;
  --radius: 16px;
  font-family: "Vazirmatn", "Tahoma", sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-tap-highlight-color: transparent; }

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background:
    radial-gradient(circle at 15% -10%, #2a3459 0%, transparent 45%),
    radial-gradient(circle at 100% 10%, #2c1f3d 0%, transparent 40%),
    var(--bg);
  color: var(--text);
  display: flex;
  justify-content: center;
  padding: 16px 12px calc(24px + env(safe-area-inset-bottom, 0px));
  padding-top: calc(16px + env(safe-area-inset-top, 0px));
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
  touch-action: pan-y;
}

.app { width: 100%; max-width: 780px; }

.app-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; margin-bottom: 18px; flex-wrap: wrap;
  position: sticky; top: 0; z-index: 15;
  background: rgba(20, 23, 31, .82); backdrop-filter: blur(10px);
  margin-inline: -12px; padding: 10px 12px; border-radius: 0 0 16px 16px;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-icon { border-radius: 10px; box-shadow: 0 4px 14px rgba(91,141,255,.35); flex-shrink: 0; }
.app-header h1 { font-size: 22px; margin: 0; letter-spacing: -0.5px; display: flex; align-items: baseline; gap: 6px; }
.app-header .subtitle { font-size: 12.5px; color: var(--muted); font-weight: normal; }

.auth-bar { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.auth-bar .muted { color: var(--muted); }
.hidden { display: none !important; }

.screen { display: none; }
.screen.active { display: block; animation: screenIn .28s cubic-bezier(.2,.8,.2,1); }
@keyframes screenIn { from { opacity: 0; transform: translateY(10px) scale(.99); } to { opacity: 1; transform: none; } }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ---------- تب‌های لابی (بازی دو نفره / لیگ و جام) ---------- */
.lobby-tabs {
  display: flex; gap: 6px; margin-bottom: 16px;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 14px; padding: 5px;
  position: sticky; top: 66px; z-index: 10;
}
.lobby-tab-btn {
  flex: 1; padding: 11px 8px; border: none; border-radius: 10px; cursor: pointer;
  background: transparent; color: var(--muted); font-weight: 700; font-family: inherit; font-size: 14px;
  transition: background .15s, color .15s;
}
.lobby-tab-btn.active { background: linear-gradient(180deg, #6a9bff, var(--accent)); color: #fff; box-shadow: 0 4px 14px rgba(91,141,255,.3); }
.lobby-panel { display: none; }
.lobby-panel.active { display: block; animation: screenIn .22s ease; }

.card {
  background: linear-gradient(180deg, var(--panel), var(--panel) 60%, #1a1e2a);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  margin-bottom: 16px;
  box-shadow: 0 8px 24px rgba(0,0,0,.28);
}
.card.center { text-align: center; }
.card h2 {
  margin-top: 0; font-size: 17px; display: flex; align-items: center;
  justify-content: space-between; gap: 8px;
}

label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 6px; }

input[type="text"], input[type="password"], select {
  width: 100%;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 15px;
  outline: none;
  font-family: inherit;
  transition: border-color .15s;
}
input[type="text"]:focus, input[type="password"]:focus, select:focus { border-color: var(--accent); }

.row-options {
  display: flex; align-items: center; gap: 10px; margin-top: 12px; flex-wrap: wrap;
}
.checkbox-row {
  display: flex; align-items: center; gap: 8px; margin: 0;
  font-size: 14px; color: var(--text); cursor: pointer; user-select: none;
}
.checkbox-row input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); cursor: pointer; }
.row-options input[type="text"], .row-options select { flex: 1; min-width: 140px; }

.btn {
  border: none;
  border-radius: 11px;
  padding: 13px 18px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 16px;
  width: 100%;
  transition: transform .08s ease, filter .15s ease, opacity .15s ease;
  font-family: inherit;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: scale(.97); }
.btn.primary { background: linear-gradient(180deg, #6a9bff, var(--accent)); color: #fff; box-shadow: 0 4px 14px rgba(91,141,255,.35); }
.btn.secondary { background: linear-gradient(180deg, #ff7683, var(--accent-2)); color: #fff; box-shadow: 0 4px 14px rgba(255,93,108,.3); }
.btn.tiny { width: auto; padding: 7px 13px; font-size: 12.5px; margin-top: 0; border-radius: 8px; }
.btn.ghost { background: var(--panel-3); color: var(--text); }
.btn.wide { width: 100%; margin-top: 10px; background: var(--panel-2); color: var(--text); border: 1px solid var(--border); }
.btn.wide.active-mode { background: var(--wall); color: #2b2110; border-color: transparent; }
.btn:disabled { opacity: .5; cursor: not-allowed; filter: none; }

.divider { text-align: center; color: var(--muted); font-size: 13px; margin: 10px 0; }

.error-text { color: var(--accent-2); font-size: 13px; min-height: 18px; margin-top: 8px; }
.small { font-size: 12.5px; }

.room-code-box {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  background: var(--panel-2); border: 1px dashed var(--border); border-radius: 10px; padding: 14px;
  font-size: 26px; letter-spacing: 6px; font-weight: 700; margin: 14px 0;
}

.spinner {
  width: 26px; height: 26px; margin: 10px auto 0;
  border: 3px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- لیست اتاق‌ها ---------- */
.room-list { display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.room-item {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px;
}
.room-item .ri-name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; unicode-bidi: plaintext; }
.room-item .ri-meta { font-size: 12px; color: var(--muted); display: flex; gap: 8px; align-items: center; flex-shrink: 0; }
.room-item .ri-lock { color: var(--wall); }
.room-item .ri-timer { color: var(--accent); }
.room-item .btn { width: auto; margin-top: 0; padding: 8px 14px; font-size: 13px; flex-shrink: 0; }

/* ---------- بازی ---------- */
.game-wrap {
  display: flex; flex-direction: column; gap: 16px; align-items: center;
}

.side-panel {
  width: 100%; max-width: 480px;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px;
  display: flex; flex-direction: column; gap: 10px;
}

.player-badge {
  display: flex; align-items: center; gap: 8px;
  background: var(--panel-2); padding: 10px 12px; border-radius: 10px;
}
.dot { width: 14px; height: 14px; border-radius: 50%; flex-shrink: 0; box-shadow: 0 0 0 3px rgba(255,255,255,.06); }
.dot-p0 { background: var(--accent); }
.dot-p1 { background: var(--accent-2); }
.pname { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.walls { font-size: 13px; color: var(--muted); }

.conn-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); flex-shrink: 0; }
.conn-dot.offline { background: var(--accent-2); animation: blink 1.2s infinite; }
@keyframes blink { 50% { opacity: .3; } }

.turn-indicator {
  text-align: center; font-weight: 700; padding: 9px; border-radius: 10px;
  background: var(--green); color: #06331b;
}
.turn-indicator.waiting-opponent { background: var(--panel-2); color: var(--muted); }

.timer-box {
  text-align: center; font-weight: 700; padding: 7px; border-radius: 10px;
  background: var(--panel-3); color: var(--text); font-variant-numeric: tabular-nums;
}
.timer-box.urgent { background: var(--accent-2-dim); color: #fff; animation: blink 1s infinite; }

.room-mini { font-size: 13px; color: var(--muted); text-align: center; }
.hint { font-size: 12px; color: var(--muted); line-height: 1.7; margin: 0; }

.board {
  display: grid;
  background: #0f121a;
  padding: 8px;
  border-radius: 12px;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.04), 0 10px 30px rgba(0,0,0,.4);
  width: min(92vw, 480px);
  height: min(92vw, 480px);
}

.cell {
  background: var(--cell);
  border-radius: 5px;
  box-shadow: inset 0 -2px 0 rgba(0,0,0,.15);
}
.cell.goal-p0 { background: color-mix(in srgb, var(--cell) 78%, var(--accent) 22%); }
.cell.goal-p1 { background: color-mix(in srgb, var(--cell) 78%, var(--accent-2) 22%); }

.cell.movable {
  background: var(--green);
  cursor: pointer;
  box-shadow: 0 0 0 2px rgba(62,203,133,.35), inset 0 -2px 0 rgba(0,0,0,.15);
}
.cell.movable:hover { filter: brightness(1.15); }

.pawn {
  border-radius: 50%;
  width: 78%; height: 78%;
  margin: 11%;
  box-shadow: inset 0 -3px 6px rgba(0,0,0,.3), 0 2px 8px rgba(0,0,0,.45);
  transition: transform .15s ease;
}
.pawn.p0 { background: radial-gradient(circle at 35% 30%, #9ec0ff, var(--accent)); }
.pawn.p1 { background: radial-gradient(circle at 35% 30%, #ffb0b8, var(--accent-2)); }

/* دیوارها: باید کاملاً یکپارچه و بدون شکستگی دیده بشن */
.wall-slot { background: transparent; border-radius: 2px; transition: background .1s; }
.wall-slot.h, .wall-slot.v { cursor: default; }
.wall-slot.placeable { cursor: pointer; }
.wall-slot.placeable:hover { background: var(--wall-hover); opacity: .55; }
.wall-slot.filled {
  background: linear-gradient(180deg, var(--wall-hover), var(--wall) 55%, var(--wall-dark));
  box-shadow: 0 1px 3px rgba(0,0,0,.4);
}

/* نقطه‌ی تقاطع بین شکاف‌ها: وقتی بخشی از یک دیوار پابرجاست، هم‌رنگ دیوار می‌شه
   تا نوار دیوار یکپارچه و بدون بریدگی دیده بشه؛ وقتی خالیه، منطقه‌ی خنثی برای کلیک باقی می‌مونه */
.gap-dot { background: #0f121a; border-radius: 2px; }
.gap-dot.filled {
  background: linear-gradient(180deg, var(--wall-hover), var(--wall) 55%, var(--wall-dark));
  box-shadow: 0 1px 3px rgba(0,0,0,.4);
}

.modal {
  position: fixed; inset: 0; background: rgba(10,12,18,.72); backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center; padding: 20px;
  z-index: 20;
}
.modal.hidden { display: none; }
.modal-box {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 26px;
  max-width: 360px; width: 100%; text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.modal-box h2 { font-size: 18px; }
.modal-actions { margin-top: 10px; display: flex; flex-direction: column; gap: 8px; }

.tabs { display: flex; gap: 6px; margin-bottom: 6px; }
.tab-btn {
  flex: 1; padding: 9px; border: none; border-radius: 9px; cursor: pointer;
  background: var(--panel-2); color: var(--muted); font-weight: 600; font-family: inherit; font-size: 13.5px;
}
.tab-btn.active { background: var(--accent); color: #fff; }
.tab-panel { display: none; text-align: right; }
.tab-panel.active { display: block; }

.profile-summary { display: flex; gap: 10px; margin: 14px 0; }
.profile-stat {
  flex: 1; padding: 12px; border-radius: 10px; background: var(--panel-2);
  font-size: 14px; text-align: center;
}
.profile-stat.win b { color: var(--green); font-size: 20px; display: block; }
.profile-stat.loss b { color: var(--accent-2); font-size: 20px; display: block; }
.profile-vs-list { text-align: right; max-height: 220px; overflow-y: auto; }
.profile-vs-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 4px; border-bottom: 1px solid var(--border); font-size: 13.5px;
}
.profile-vs-row:last-child { border-bottom: none; }
.profile-vs-row .score { color: var(--muted); font-variant-numeric: tabular-nums; }

.banner {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: var(--accent-2); color: #fff; padding: 12px 20px; border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0,0,0,.35); z-index: 30; max-width: 90vw; text-align: center;
}
.banner.info { background: var(--accent); }
.banner.hidden { display: none; }

@media (min-width: 640px) {
  .game-wrap { flex-direction: row-reverse; align-items: flex-start; justify-content: center; }
  .side-panel { width: 250px; }
}

/* ---------- نوار پایین ناوبری، مخصوص گوشی ---------- */
.bottom-nav {
  display: none;
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 25;
  background: rgba(24, 28, 40, .92); backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  padding: 6px 10px calc(6px + env(safe-area-inset-bottom, 0px));
  justify-content: space-around; align-items: stretch;
  box-shadow: 0 -8px 24px rgba(0,0,0,.35);
}
.bn-item {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: none; border: none; color: var(--muted); font-family: inherit;
  font-size: 11px; font-weight: 600; padding: 6px 14px; border-radius: 12px;
  cursor: pointer; transition: color .15s, background .15s;
}
.bn-item .bn-icon { font-size: 20px; line-height: 1; transition: transform .15s; }
.bn-item.active { color: var(--accent); }
.bn-item.active .bn-icon { transform: translateY(-2px) scale(1.08); }
.bn-item:active .bn-icon { transform: scale(.9); }

@media (max-width: 639px) {
  .bottom-nav { display: flex; }
  .bottom-nav.in-game { display: none; }
  body { padding-bottom: calc(78px + env(safe-area-inset-bottom, 0px)); }
  body.game-active { padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px)); }
  .app-header { top: -1px; }
  .lobby-tabs { top: 60px; }
  /* بزرگ‌تر کردن ناحیه‌ی لمسی شکاف‌های دیوار برای انگشت */
  .wall-slot.placeable { position: relative; }
  .wall-slot.placeable::after {
    content: ''; position: absolute; inset: -6px; border-radius: 4px;
  }
}

/* ---------- جشن برد (کانفتی سبک) ---------- */
.confetti-piece {
  position: fixed; top: -10px; width: 8px; height: 14px; z-index: 40;
  border-radius: 2px; opacity: .95; pointer-events: none;
  animation: confettiFall linear forwards;
}
@keyframes confettiFall {
  to { transform: translateY(100vh) rotate(540deg); opacity: .2; }
}

/* ---------- مسابقات (جام حذفی / لیگ) ---------- */
.champion-name { font-size: 22px; font-weight: 800; color: var(--wall); text-align: center; margin: 6px 0 0; }

.matches-list { display: flex; flex-direction: column; gap: 8px; }
.match-box {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; font-size: 13.5px;
}
.match-box .mb-round-tag { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.match-box .mb-row {
  display: flex; justify-content: space-between; align-items: center; padding: 3px 0; gap: 8px;
}
.match-box .mb-row .mb-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; unicode-bidi: plaintext; }
.match-box .mb-row.winner .mb-name { color: var(--green); font-weight: 700; }
.match-box .mb-row.tbd .mb-name { color: var(--muted); }
.match-box .mb-vs { text-align: center; font-size: 11px; color: var(--muted); padding: 1px 0; }
.match-box .btn { margin-top: 8px; padding: 9px; font-size: 12.5px; }
.match-box .mb-status { font-size: 11px; color: var(--muted); text-align: center; margin-top: 4px; }

.standings-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.standings-table th, .standings-table td { padding: 8px 6px; text-align: center; border-bottom: 1px solid var(--border); }
.standings-table th { color: var(--muted); font-weight: 600; font-size: 11.5px; }
.standings-table td:first-child, .standings-table th:first-child { text-align: right; }
.standings-table tr:last-child td { border-bottom: none; }
.standings-table td.champion-row { color: var(--wall); font-weight: 700; }

.bracket { display: flex; gap: 20px; overflow-x: auto; padding: 6px 2px 14px; }
.bracket-round { display: flex; flex-direction: column; justify-content: space-around; gap: 14px; min-width: 190px; flex-shrink: 0; }
.bracket-round-title { font-size: 12px; color: var(--muted); text-align: center; font-weight: 700; margin-bottom: 2px; }
