/* Playdonya — play stage, HUD, overlays, game-over sheet, leaderboard, online room.
   Uses the contract tokens with fallbacks so it works before design/tokens.css lands.
   Logical properties only (RTL-safe). */

.pd-stage {
  --pd-bg: var(--bg, #14111f);
  --pd-bg2: var(--bg-2, #1c1830);
  --pd-surface: var(--surface, #231e3a);
  --pd-surface2: var(--surface-2, #2d2748);
  --pd-line: var(--line, #3a3358);
  --pd-ink: var(--ink, #f6f1ff);
  --pd-ink2: var(--ink-2, #c9c0e0);
  --pd-ink3: var(--ink-3, #8f86ad);
  --pd-brand: var(--brand, #ffb000);
  --pd-brand2: var(--brand-2, #ff7a1a);
  --pd-accent: var(--accent, #36d6c3);
  --pd-good: var(--good, #4ade80);
  --pd-bad: var(--bad, #f43f5e);
  --pd-on-brand: var(--on-brand, #1a1200);
  --pd-r-sm: var(--r-sm, 8px);
  --pd-r-md: var(--r-md, 12px);
  --pd-r-lg: var(--r-lg, 18px);
  --pd-r-xl: var(--r-xl, 24px);
  --pd-pill: var(--r-pill, 999px);
  --pd-ease: var(--ease, cubic-bezier(.2, .8, .2, 1));
  --pd-dur: var(--dur-2, 220ms);
  --pd-hud-h: 52px;

  position: relative;
  display: flex;
  flex-direction: column;
  inline-size: 100%;
  block-size: clamp(460px, calc(100svh - 140px), 860px);
  background: var(--pd-bg);
  color: var(--pd-ink);
  border: 1px solid var(--pd-line);
  border-radius: var(--pd-r-xl);
  overflow: hidden;
  box-shadow: var(--sh-2, 0 10px 30px rgb(0 0 0 / .35));
  font-family: var(--font-body, system-ui, sans-serif);
  isolation: isolate;
  contain: layout paint;
  -webkit-tap-highlight-color: transparent;
}
@media (max-width: 600px) {
  .pd-stage { block-size: calc(100svh - 96px); min-block-size: 480px; border-radius: var(--pd-r-lg); }
}
.pd-stage:fullscreen, .pd-stage.pd-fs {
  block-size: 100vh; block-size: 100dvh; inline-size: 100vw; border-radius: 0; border: 0;
}
.pd-stage.pd-fs { position: fixed; inset: 0; z-index: 1000; }
html.pd-noscroll, html.pd-noscroll body { overflow: hidden; }

/* ---------- HUD ---------- */
.pd-hud {
  flex: none;
  display: flex; align-items: center; gap: 6px;
  block-size: var(--pd-hud-h);
  padding-inline: 12px 8px;
  padding-block: 6px;
  background: linear-gradient(to bottom, var(--pd-surface), var(--pd-bg2));
  border-block-end: 1px solid var(--pd-line);
}
.pd-hud-score, .pd-hud-best { display: flex; flex-direction: column; line-height: 1.05; min-inline-size: 56px; }
.pd-hud-best { opacity: .8; }
.pd-hud-label { font-size: 11px; color: var(--pd-ink3); letter-spacing: .02em; }
.pd-score-val, .pd-best-val { font-family: var(--font-display, inherit); font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }
.pd-score-val { color: var(--pd-brand); display: inline-block; transform-origin: center; }
.pd-bump { animation: pd-bump 260ms var(--pd-ease); }
@keyframes pd-bump { 30% { transform: scale(1.22); } 100% { transform: scale(1); } }
.pd-versus .pd-hud-score, .pd-versus .pd-hud-best { display: none; }
.pd-hud-spacer { flex: 1; }
.pd-chip {
  padding: 3px 10px; border-radius: var(--pd-pill); font-size: 12px; font-weight: 700;
  background: color-mix(in srgb, var(--pd-accent) 18%, transparent); color: var(--pd-accent);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-inline-size: 30vw;
}
.pd-ib {
  display: inline-grid; place-items: center; inline-size: 40px; block-size: 40px; flex: none;
  border: 0; border-radius: var(--pd-r-md); background: transparent; color: var(--pd-ink2); cursor: pointer;
  transition: background var(--pd-dur) var(--pd-ease), color var(--pd-dur) var(--pd-ease);
}
.pd-ib:hover { background: var(--pd-surface2); color: var(--pd-ink); }
.pd-ib:disabled { opacity: .35; cursor: default; background: transparent; }
.pd-ib[hidden] { display: none; }
.pd-ic { display: block; }
:where(.pd-stage) :focus-visible { outline: 2px solid var(--pd-brand); outline-offset: 2px; }

/* ---------- body + canvas host ---------- */
.pd-body { position: relative; flex: 1; min-block-size: 0; }
.pd-host { position: absolute; inset: 0; overflow: hidden; background: var(--pd-bg); }
.pd-host canvas { display: block; inline-size: 100%; block-size: 100%; }

.pd-loading, .pd-error { display: grid; place-items: center; block-size: 100%; padding: 24px; text-align: center; color: var(--pd-ink2); }
.pd-spinner {
  inline-size: 40px; block-size: 40px; border-radius: 50%;
  border: 4px solid color-mix(in srgb, var(--pd-brand) 25%, transparent); border-block-start-color: var(--pd-brand);
  animation: pd-spin 800ms linear infinite;
}
.pd-spinner-sm { inline-size: 16px; block-size: 16px; border-width: 2px; display: inline-block; vertical-align: -3px; margin-inline-end: 8px; }
@keyframes pd-spin { to { transform: rotate(360deg); } }

/* ---------- layers (start / pause / over / room / leaderboard) ---------- */
.pd-layer {
  position: absolute; inset: 0; z-index: 5;
  display: grid; place-items: center; padding: 16px;
  background: color-mix(in srgb, var(--pd-bg) 42%, transparent);
  -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px);
  overflow: auto; overscroll-behavior: contain;
  animation: pd-fade var(--pd-dur) var(--pd-ease);
}
.pd-layer[hidden] { display: none; }
.pd-stage[data-layer="pause"] .pd-host, .pd-stage[data-layer="over"] .pd-host { filter: saturate(.7); }
@keyframes pd-fade { from { opacity: 0; } }
.pd-card {
  inline-size: min(100%, 420px);
  background: color-mix(in srgb, var(--pd-surface) 94%, transparent);
  border: 1px solid var(--pd-line); border-radius: var(--pd-r-xl);
  padding: 22px 20px 18px; box-shadow: var(--sh-3, 0 20px 50px rgb(0 0 0 / .45));
  text-align: center;
  animation: pd-pop 320ms var(--pd-ease);
}
.pd-card-sm { inline-size: min(100%, 360px); }
@keyframes pd-pop { from { transform: translateY(12px) scale(.97); opacity: 0; } }
.pd-title { margin: 0 0 6px; font-family: var(--font-display, inherit); font-size: var(--fs-xl, 26px); line-height: 1.2; font-weight: 800; }
.pd-tagline, .pd-muted { margin: 0 0 14px; color: var(--pd-ink2); font-size: var(--fs-sm, 14px); line-height: 1.6; }
.pd-actions { display: grid; gap: 10px; margin-block: 14px 6px; }
.pd-actions-row { grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); }
.pd-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; white-space: nowrap;
  min-block-size: 48px; padding: 10px 18px; border-radius: var(--pd-pill);
  border: 1px solid transparent; font: inherit; font-weight: 800; font-size: 16px; cursor: pointer;
  background: var(--pd-surface2); color: var(--pd-ink);
  transition: transform 120ms var(--pd-ease), background var(--pd-dur) var(--pd-ease), box-shadow var(--pd-dur) var(--pd-ease);
}
.pd-btn-2l { flex-direction: column; gap: 2px; white-space: normal; }
.pd-btn small { font-size: 12px; font-weight: 600; opacity: .8; }
.pd-btn:active { transform: scale(.97); }
.pd-btn:disabled { opacity: .5; cursor: default; }
.pd-btn[hidden] { display: none; }
.pd-primary {
  background: linear-gradient(135deg, var(--pd-brand), var(--pd-brand2)); color: var(--pd-on-brand);
  box-shadow: 0 6px 20px color-mix(in srgb, var(--pd-brand) 35%, transparent);
}
.pd-primary:hover { box-shadow: 0 8px 26px color-mix(in srgb, var(--pd-brand) 50%, transparent); }
.pd-secondary { background: var(--pd-surface2); border-color: var(--pd-line); }
.pd-secondary:hover { border-color: var(--pd-brand); }
.pd-ghost { background: transparent; color: var(--pd-ink2); }
.pd-ghost:hover { color: var(--pd-ink); background: var(--pd-surface2); }
.pd-link { border: 0; background: none; color: var(--pd-brand); font: inherit; text-decoration: underline; cursor: pointer; padding: 0; }

.pd-field { margin-block: 12px 4px; display: grid; gap: 6px; justify-items: center; }
.pd-field-label { display: block; font-size: 12px; color: var(--pd-ink3); font-weight: 700; }
.pd-seg { display: inline-flex; padding: 3px; gap: 2px; border-radius: var(--pd-pill); background: var(--pd-bg2); border: 1px solid var(--pd-line); }
.pd-seg-btn {
  min-block-size: 38px; min-inline-size: 72px; padding: 6px 12px; border: 0; border-radius: var(--pd-pill);
  background: transparent; color: var(--pd-ink2); font: inherit; font-weight: 700; font-size: 14px; cursor: pointer;
}
.pd-seg-btn[aria-checked="true"] { background: var(--pd-surface2); color: var(--pd-ink); box-shadow: inset 0 0 0 1px var(--pd-brand); }

.pd-howto { margin-block-start: 10px; text-align: start; font-size: 14px; color: var(--pd-ink2); line-height: 1.7; }
.pd-howto summary { cursor: pointer; font-weight: 700; color: var(--pd-ink); text-align: center; list-style-position: inside; padding-block: 6px; }
.pd-howto p { margin: 6px 0; }
.pd-switch { display: inline-flex; align-items: center; gap: 8px; margin-block-start: 8px; font-size: 14px; color: var(--pd-ink2); cursor: pointer; min-block-size: 40px; }
.pd-switch input { inline-size: 18px; block-size: 18px; accent-color: var(--pd-brand); }

/* game over sheet */
.pd-sheet { padding-block-start: 18px; }
.pd-final { display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap; margin-block: 6px 2px; }
.pd-final-score {
  font-family: var(--font-display, inherit); font-size: clamp(44px, 12vw, 64px); font-weight: 900; line-height: 1;
  font-variant-numeric: tabular-nums; color: var(--pd-brand);
  animation: pd-score-in 600ms var(--pd-ease);
}
@keyframes pd-score-in { from { transform: scale(.5); opacity: 0; } 60% { transform: scale(1.08); } }
.pd-badge { padding: 4px 10px; border-radius: var(--pd-pill); background: var(--pd-good); color: #062b14; font-size: 13px; font-weight: 800; animation: pd-pop 500ms 200ms both var(--pd-ease); }
.pd-final-meta { display: flex; justify-content: center; gap: 4px 14px; flex-wrap: wrap; color: var(--pd-ink2); font-size: 14px; }
.pd-submit { margin-block-start: 12px; display: grid; gap: 6px; }
.pd-submit[hidden], .pd-nick-row[hidden] { display: none; }
.pd-inline { display: flex; gap: 8px; align-items: center; }
.pd-inline .pd-input { flex: 1; min-inline-size: 0; }
.pd-inline .pd-btn { min-block-size: 44px; padding-inline: 14px; font-size: 14px; white-space: nowrap; }
.pd-input {
  min-block-size: 44px; padding: 8px 12px; border-radius: var(--pd-r-md);
  border: 1px solid var(--pd-line); background: var(--pd-bg2); color: var(--pd-ink); font: inherit; font-size: 16px;
  inline-size: 100%;
}
.pd-input:focus { outline: none; border-color: var(--pd-brand); box-shadow: 0 0 0 3px color-mix(in srgb, var(--pd-brand) 25%, transparent); }
.pd-submit-status, .pd-form-error, .pd-rematch-status { margin: 4px 0 0; min-block-size: 1.4em; font-size: 13px; color: var(--pd-ink2); }
.pd-form-error { color: var(--pd-bad); }
.pd-room-form { display: grid; gap: 6px; text-align: start; }

/* online */
.pd-code { margin-block: 6px 12px; }
.pd-code b { display: block; font-family: var(--font-mono, ui-monospace, monospace); font-size: 34px; letter-spacing: .18em; color: var(--pd-brand); }
.pd-waiting { margin: 12px 0 0; font-size: 14px; color: var(--pd-ink2); }
.pd-online-bar {
  position: absolute; inset-block-end: 12px; inset-inline-start: 50%; transform: translateX(-50%); z-index: 4;
  display: inline-flex; align-items: center; gap: 8px; max-inline-size: calc(100% - 24px);
  padding: 6px 14px; border-radius: var(--pd-pill);
  background: color-mix(in srgb, var(--pd-surface) 88%, transparent); border: 1px solid var(--pd-line);
  font-size: 13px; font-weight: 700; pointer-events: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
[dir="rtl"] .pd-online-bar { transform: translateX(50%); }
.pd-online-bar[hidden] { display: none; }
.pd-dot { inline-size: 8px; block-size: 8px; border-radius: 50%; background: var(--pd-ink3); flex: none; }
.pd-online-bar[data-tone="good"] .pd-dot { background: var(--pd-good); box-shadow: 0 0 0 4px color-mix(in srgb, var(--pd-good) 25%, transparent); }
.pd-online-bar[data-tone="bad"] .pd-dot { background: var(--pd-bad); }
.pd-online-bar[data-tone="wait"] .pd-dot { background: var(--pd-brand); animation: pd-blink 1.2s infinite; }
@keyframes pd-blink { 50% { opacity: .3; } }

/* toast */
.pd-toast {
  position: absolute; inset-block-end: 16px; inset-inline-start: 50%; transform: translateX(-50%); z-index: 9;
  padding: 8px 16px; border-radius: var(--pd-pill); background: var(--pd-ink); color: var(--pd-bg); font-weight: 700; font-size: 14px;
  animation: pd-pop 240ms var(--pd-ease);
}
[dir="rtl"] .pd-toast { transform: translateX(50%); }
.pd-toast[hidden] { display: none; }

/* ---------- leaderboard (inside .play-lb on the page, or the in-stage drawer) ---------- */
.pd-lb { color: var(--ink, #f6f1ff); }
.pd-lb-card { text-align: start; inline-size: min(100%, 460px); max-block-size: 100%; display: flex; flex-direction: column; }
.pd-lb-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-block-end: 8px; }
.pd-lb-title { margin: 0; font-family: var(--font-display, inherit); font-size: var(--fs-lg, 20px); font-weight: 800; }
.pd-tabs { display: flex; gap: 4px; flex-wrap: wrap; margin-block-end: 8px; }
.pd-tab {
  min-block-size: 36px; padding: 6px 12px; border-radius: var(--r-pill, 999px); border: 1px solid var(--line, #3a3358);
  background: transparent; color: var(--ink-2, #c9c0e0); font: inherit; font-size: 13px; font-weight: 700; cursor: pointer;
}
.pd-tab[aria-selected="true"] { background: var(--brand, #ffb000); color: var(--on-brand, #1a1200); border-color: transparent; }
.pd-lb-list { list-style: none; margin: 0; padding: 0; overflow: auto; min-block-size: 120px; }
.pd-lb-list[aria-busy="true"] { opacity: .6; }
.pd-lb-row {
  display: grid; grid-template-columns: 2.6em 1fr auto; align-items: center; gap: 8px;
  padding: 8px 10px; border-radius: var(--r-md, 12px); font-variant-numeric: tabular-nums;
}
.pd-lb-row:nth-child(odd) { background: color-mix(in srgb, var(--surface-2, #2d2748) 55%, transparent); }
.pd-lb-row:nth-child(1) .pd-lb-rank { color: #ffd34d; }
.pd-lb-row:nth-child(2) .pd-lb-rank { color: #d8dbe6; }
.pd-lb-row:nth-child(3) .pd-lb-rank { color: #e0a36a; }
.pd-lb-rank { font-weight: 900; color: var(--ink-3, #8f86ad); text-align: center; }
.pd-lb-nick { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.pd-lb-nick small { color: var(--ink-3, #8f86ad); font-weight: 600; }
.pd-lb-score { font-weight: 800; color: var(--brand, #ffb000); }
.pd-me { box-shadow: inset 0 0 0 1.5px var(--accent, #36d6c3); }
.pd-lb-gap { margin-block-start: 10px; }
.pd-lb-empty { padding: 18px 8px; color: var(--ink-3, #8f86ad); text-align: center; }

@media (max-width: 420px) {
  .pd-hud { gap: 2px; padding-inline: 10px 4px; }
  .pd-hud-best { min-inline-size: 44px; }
  .pd-ib { inline-size: 38px; }
  .pd-card { padding: 18px 14px 14px; }
}
@media (prefers-reduced-motion: reduce) {
  .pd-stage *, .pd-stage *::before, .pd-stage *::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
}
