/* ==========================================================================
   طراحی: «باجه‌ی آرکید» — الهام از توکن‌های فلزی و بلیت‌های پرفراژدار سالن بازی،
   چون هسته‌ی این پلتفرم «سکه» است: وارد شدن به بازی با سکه، برد = سکه بیشتر.
   هر کارت بازی مثل یک ژتون/بلیت آرکید طراحی شده با لبه‌ی دندانه‌دار.
   ========================================================================== */

   :root {
    /* رنگ */
    --bg-deep:      #14121F;   /* پس‌زمینه اصلی: بنفش-سرمه‌ای تیره، نه مشکی خالص */
    --bg-panel:     #1D1A2E;   /* پنل‌ها و کارت‌ها */
    --bg-panel-2:   #262239;   /* لایه‌ی روتر داخل کارت‌ها */
    --line:         #383253;   /* خطوط ظریف جداکننده */
    --gold:         #F0B429;   /* رنگ سکه/توکن — امضای بصری اصلی */
    --gold-dim:     #7A5B18;
    --teal:         #2DD9C4;   /* حالت زنده / نوبت شما */
    --coral:        #FF6B5B;   /* خطا / باخت */
    --text-hi:      #F4F1FA;
    --text-mid:     #ABA5C6;
    --text-low:     #6B6588;
  
    /* تایپوگرافی */
    --font-display: 'Space Grotesk', 'Vazirmatn', sans-serif;
    --font-body:    'Vazirmatn', sans-serif;
    --font-mono:    'JetBrains Mono', monospace;
  
    /* هندسه */
    --radius: 18px;
    --radius-sm: 10px;
  }
  
  * { box-sizing: border-box; }
  
  html, body {
    margin: 0;
    padding: 0;
    background: var(--bg-deep);
    color: var(--text-hi);
    font-family: var(--font-body);
    -webkit-tap-highlight-color: transparent;
  }
  
  body {
    background-image:
      radial-gradient(circle at 15% 0%, rgba(240,180,41,0.08), transparent 40%),
      radial-gradient(circle at 85% 15%, rgba(45,217,196,0.06), transparent 35%);
  }
  
  #app {
    max-width: 480px;
    margin: 0 auto;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    padding-bottom: 78px; /* جا برای tabbar */
  }
  
  button { font-family: inherit; cursor: pointer; }
  :focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }
  
  @media (prefers-reduced-motion: reduce) {
    * { animation: none !important; transition: none !important; }
  }
  
  /* ---------- Topbar ---------- */
  .topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    background: rgba(20,18,31,0.92);
    backdrop-filter: blur(8px);
    z-index: 10;
  }
  
  .profile { display: flex; align-items: center; gap: 10px; }
  
  .avatar {
    width: 40px; height: 40px;
    border-radius: 12px;
    background: linear-gradient(145deg, var(--teal), #1a9c8c);
    display: flex; align-items: center; justify-content: center;
    font-family: var(--font-display);
    font-weight: 700;
    color: #082521;
    font-size: 16px;
    overflow: hidden; /* برای اینکه عکس داخلش گرد بمونه، نه لبه‌ها بیرون بزنه */
    flex-shrink: 0;
    border: 2px solid #fff;
  }
  
  .avatar.has-photo { background: var(--bg-panel-2); }
  
  .avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
  }
  
  .username { font-weight: 600; font-size: 14px; }
  .conn-status { font-size: 11px; color: var(--text-low); }
  .conn-status.online { color: var(--teal); }
  
  .coin-badge {
    display: flex; align-items: center; gap: 6px;
    background: var(--bg-panel-2);
    border: 1px solid var(--gold-dim);
    padding: 7px 12px;
    border-radius: 999px;
    font-family: var(--font-mono);
    font-weight: 700;
    color: var(--gold);
    font-size: 14px;
  }
  .coin-icon { font-size: 13px; }
  
  /* ---------- Main view ---------- */
  #view {
    flex: 1;
    padding: 18px 16px 8px;
  }
  
  .section-title {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 20px;
    margin: 4px 0 4px;
  }
  .section-sub {
    color: var(--text-mid);
    font-size: 13px;
    margin: 0 0 16px;
  }
  
  /* ---------- کارت بازی به‌شکل «توکن آرکید» با لبه‌ی دندانه‌دار ---------- */
  .game-ticket {
    position: relative;
    background: var(--bg-panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 16px 16px 16px 18px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    gap: 14px;
    overflow: hidden;
  }
  
  /* لبه‌ی پرفراژدار سمت راست، مثل جدا شدن بلیت */
  .game-ticket::before {
    content: "";
    position: absolute;
    top: 0; bottom: 0; right: 78px;
    width: 0;
    border-right: 2px dashed var(--line);
  }
  .game-ticket::after {
    content: "";
    position: absolute;
    right: 71px; top: -8px;
    width: 16px; height: 16px;
    background: var(--bg-deep);
    border-radius: 50%;
  }
  
  .ticket-icon {
    flex: 0 0 60px;
    height: 60px;
    border-radius: 14px;
    background: var(--bg-panel-2);
    display: flex; align-items: center; justify-content: center;
    font-size: 26px;
    margin-right: 8px;
  }
  
  .ticket-body { flex: 1; min-width: 0; }
  .ticket-name { font-weight: 700; font-size: 15px; margin-bottom: 2px; }
  .ticket-meta { font-size: 12px; color: var(--text-mid); }
  .ticket-meta b { color: var(--gold); font-family: var(--font-mono); }
  
  .ticket-cta {
    flex: 0 0 auto;
    background: var(--gold);
    color: #241a02;
    border: none;
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    font-weight: 700;
    font-size: 13px;
    white-space: nowrap;
  }
  .ticket-cta:disabled { opacity: 0.5; }
  .ticket-cta.waiting {
    background: var(--bg-panel-2);
    color: var(--teal);
    border: 1px solid var(--teal);
  }
  
  /* ---------- Rankings ---------- */
  .rank-row {
    display: flex; align-items: center; gap: 12px;
    padding: 11px 14px;
    background: var(--bg-panel);
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    margin-bottom: 8px;
  }
  .rank-pos {
    font-family: var(--font-mono);
    font-weight: 700;
    width: 26px;
    color: var(--text-mid);
    text-align: center;
  }
  .rank-row.top1 .rank-pos { color: var(--gold); }
  .rank-row.top2 .rank-pos { color: #C6C6D6; }
  .rank-row.top3 .rank-pos { color: #CD8B5A; }
  .rank-name { flex: 1; font-weight: 600; font-size: 14px; }
  .rank-score { font-family: var(--font-mono); color: var(--teal); font-weight: 700; font-size: 13px; }
  .rank-stats { font-size: 11px; color: var(--text-low); }
  
  .scope-tabs { display: flex; gap: 8px; margin-bottom: 14px; overflow-x: auto; }
  .scope-tab {
    background: var(--bg-panel-2);
    border: 1px solid var(--line);
    color: var(--text-mid);
    padding: 7px 14px;
    border-radius: 999px;
    font-size: 12px;
    white-space: nowrap;
  }
  .scope-tab.active { background: var(--teal); color: #052824; border-color: var(--teal); font-weight: 700; }
  
  /* ---------- Wallet ---------- */
  .coin-hero {
    text-align: center;
    padding: 26px 10px 22px;
    background: radial-gradient(circle at 50% 0%, rgba(240,180,41,0.14), transparent 65%);
    border: 1px solid var(--gold-dim);
    border-radius: var(--radius);
    margin-bottom: 18px;
  }
  .coin-hero-amount {
    font-family: var(--font-mono);
    font-size: 40px;
    font-weight: 700;
    color: var(--gold);
  }
  .coin-hero-label { font-size: 12px; color: var(--text-mid); margin-top: 4px; }
  
  .btn-primary {
    width: 100%;
    background: var(--teal);
    color: #052824;
    border: none;
    border-radius: var(--radius-sm);
    padding: 13px;
    font-weight: 700;
    font-size: 14px;
    margin-bottom: 18px;
  }
  .btn-primary:disabled { opacity: 0.5; }
  
  .tx-row {
    display: flex; justify-content: space-between; align-items: center;
    padding: 10px 4px;
    border-bottom: 1px solid var(--line);
    font-size: 13px;
  }
  .tx-note { color: var(--text-mid); }
  .tx-amount { font-family: var(--font-mono); font-weight: 700; }
  .tx-amount.pos { color: var(--teal); }
  .tx-amount.neg { color: var(--coral); }
  
  /* ---------- Tabbar ---------- */
  .tabbar {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    max-width: 480px;
    margin: 0 auto;
    display: flex;
    background: var(--bg-panel);
    border-top: 1px solid var(--line);
    padding: 8px 6px calc(8px + env(safe-area-inset-bottom));
  }
  .tab-btn {
    flex: 1;
    background: none;
    border: none;
    color: var(--text-low);
    display: flex; flex-direction: column; align-items: center; gap: 4px;
    font-size: 11px;
    padding: 6px 0;
    border-radius: var(--radius-sm);
  }
  .tab-btn .tab-icon { font-size: 17px; }
  .tab-btn.active { color: var(--teal); background: rgba(45,217,196,0.08); }
  
  /* ---------- Toast ---------- */
  .toast {
    position: fixed;
    bottom: 90px; left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--bg-panel-2);
    border: 1px solid var(--line);
    color: var(--text-hi);
    padding: 10px 18px;
    border-radius: 999px;
    font-size: 13px;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s, transform 0.2s;
    z-index: 50;
    max-width: 90%;
    text-align: center;
  }
  .toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
  
  /* ---------- Empty / loading states ---------- */
  .empty-state {
    text-align: center;
    padding: 40px 12px;
    color: var(--text-mid);
    font-size: 13px;
  }
  
  /* ==========================================================================
     بازی دوز (Tic-Tac-Toe)
     ========================================================================== */
  .match-header {
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: 18px;
  }
  .match-vs { font-family: var(--font-display); font-weight: 600; font-size: 15px; }
  .match-turn {
    font-size: 12px;
    padding: 5px 11px;
    border-radius: 999px;
    background: var(--bg-panel-2);
    color: var(--text-mid);
  }
  .match-turn.yours { background: rgba(45,217,196,0.15); color: var(--teal); font-weight: 700; }
  
  .ttt-board {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    width: 100%;
    aspect-ratio: 1;
    margin: 0 auto 18px;
  }
  .ttt-cell {
    background: var(--bg-panel);
    border: 1px solid var(--line);
    border-radius: 12px;
    font-family: var(--font-display);
    font-size: 34px;
    font-weight: 700;
    display: flex; align-items: center; justify-content: center;
    color: var(--text-hi);
  }
  .ttt-cell.x { color: var(--teal); }
  .ttt-cell.o { color: var(--gold); }
  .ttt-cell:not(.filled):not(:disabled):active { background: var(--bg-panel-2); }
  
  .match-result {
    text-align: center;
    padding: 22px 16px;
    border-radius: var(--radius);
    margin-bottom: 16px;
  }
  .match-result.win  { background: rgba(45,217,196,0.1); border: 1px solid var(--teal); }
  .match-result.lose { background: rgba(255,107,91,0.1); border: 1px solid var(--coral); }
  .match-result.draw { background: var(--bg-panel-2); border: 1px solid var(--line); }
  .match-result-title { font-family: var(--font-display); font-weight: 700; font-size: 20px; margin-bottom: 4px; }
  .match-result-coins { font-family: var(--font-mono); color: var(--gold); font-size: 14px; }
  
  .searching-state {
    text-align: center;
    padding: 50px 16px;
  }
  .searching-token {
    width: 64px; height: 64px;
    margin: 0 auto 16px;
    border-radius: 50%;
    border: 3px solid var(--line);
    border-top-color: var(--gold);
    animation: spin 0.9s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }