:root {
  color-scheme: dark;
  --bg: #0c0c0f;
  --panel: #15151a;
  --line: #26262e;
  --text: #f2f2f5;
  --muted: #8b8b98;
  --accent: #fb7185;
  --p1: #fb7185;
  --p2: #60a5fa;
  --ladder: #34d399;
  --snake: #fb7185;
  --cell-a: #14141a;
  --cell-b: #191920;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; min-height: 100%; background: var(--bg); }
body {
  font-family: "Vazirmatn", system-ui, Tahoma, sans-serif; color: var(--text);
  touch-action: manipulation; -webkit-text-size-adjust: 100%;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.emoji { height: 1.15em; width: 1.15em; vertical-align: -.2em; display: inline-block; }

/* ---------- buttons ---------- */
.btn {
  font: inherit; cursor: pointer; color: var(--text); background: var(--panel);
  border: 1px solid var(--line); border-radius: 12px; padding: 12px 16px; min-height: 46px;
}
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: transparent; color: #1b0509; font-weight: 800; }
.btn.ghost { background: transparent; }
.btn.sm { padding: 6px 12px; min-height: 38px; font-size: 13px; }

/* ---------- setup ---------- */
.screen { min-height: 100dvh; }
#setup { display: grid; place-items: center; padding: 16px; }
.sheet { width: min(400px, 100%); display: flex; flex-direction: column; gap: 14px; text-align: center; }
.sheet h1 { margin: 0; font-size: 26px; }
.sheet p { margin: 0; }
.sheet label { display: flex; flex-direction: column; gap: 6px; text-align: right; font-size: 14px; color: var(--muted); }
.sheet label.check { flex-direction: row; align-items: center; gap: 10px; color: var(--text); }
.sheet input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }
.sheet input:not([type=checkbox]) {
  font: inherit; font-size: 16px; padding: 12px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel); color: var(--text);
}
.label { text-align: right; font-size: 14px; color: var(--muted); }
.levels { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.level {
  font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 4px; display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer;
}
.level .le { font-size: 22px; line-height: 1.2; }
.level b { font-size: 14px; }
.level small { font-size: 11px; color: var(--muted); }
.level.on { border-color: var(--accent); background: #2a1217; }

/* ---------- game layout ---------- */
.game { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; max-width: 1100px; margin: 0 auto; padding: 10px 10px 0; }
.players { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.pl {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px;
  display: flex; align-items: center; gap: 8px; min-width: 0;
}
.pl.on { border-color: var(--c); }
.pl .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--c); flex: none; }
.pl b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.pl span { margin-inline-start: auto; font-size: 12px; color: var(--muted); white-space: nowrap; }

.dock {
  position: sticky; bottom: 0; z-index: 5; display: flex; align-items: center; gap: 12px;
  padding: 10px 0 calc(10px + env(safe-area-inset-bottom)); background: linear-gradient(transparent, var(--bg) 18%);
}
.dock .btn { flex: 1; min-height: 56px; font-size: 17px; }

.dice {
  flex: none; width: 56px; height: 56px; border-radius: 12px; background: #f4f4f6; padding: 8px;
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
}
.dice.rolling { animation: shake .45s ease; }
@keyframes shake { 25% { transform: rotate(-18deg) scale(1.08); } 60% { transform: rotate(12deg); } }
.pip { width: 100%; height: 100%; border-radius: 50%; background: #1b1b22; transform: scale(.75); }

.log-box { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: calc(14px + env(safe-area-inset-bottom)); }
.log-box summary { cursor: pointer; font-size: 14px; color: var(--muted); }
.log { list-style: none; margin: 10px 0; padding: 0; max-height: 220px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.log li { padding: 6px 10px; border-radius: 8px; background: #0003; border-inline-start: 3px solid var(--line); }
.tools { display: flex; gap: 8px; }

@media (min-width: 900px) {
  .game { grid-template-columns: minmax(0, 1fr) 320px; align-items: start; gap: 14px 20px; padding: 20px; }
  .board-wrap { grid-column: 1; grid-row: 1 / span 4; }
  .players, .dock, .log-box { grid-column: 2; }
  .dock { position: static; background: none; padding: 0; }
  .log-box { margin: 0; }
  .board { width: min(100%, calc(100dvh - 40px)); }
}

/* ---------- board ---------- */
.board-wrap { display: flex; justify-content: center; }
.board {
  position: relative; width: 100%; aspect-ratio: 1; direction: ltr; container-type: inline-size;
  border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--line);
  user-select: none; -webkit-user-select: none;
}
.cells { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(10, 1fr); grid-template-rows: repeat(10, 1fr); gap: .2cqw; }
.cell { position: relative; background: var(--cell-a); display: grid; place-items: center; }
.cell.alt { background: var(--cell-b); }
.cell .n { position: absolute; top: .5cqw; left: .8cqw; font-style: normal; font-size: 2.6cqw; color: #6c6c7a; line-height: 1; }
.cell .mark { font-size: 4.4cqw; line-height: 1; margin-top: 1.4cqw; }
.cell.sp { background: #22151a; }
.cell.cd { background: #1d1829; }
.cell.cd .mark { font-weight: 800; color: #a78bfa; font-size: 4.6cqw; }
.cell.start { background: #261319; }
.cell.start .mark { font-size: 2.4cqw; color: var(--accent); font-weight: 800; }
.cell.flash { animation: flash .8s ease 2; }
@keyframes flash { 50% { box-shadow: inset 0 0 0 .6cqw var(--text); } }

.overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.tokens { position: absolute; inset: 0; pointer-events: none; }
.token {
  position: absolute; width: 4.8cqw; height: 4.8cqw; border-radius: 50%; transform: translate(-50%, -50%);
  border: .5cqw solid #fff; box-shadow: 0 .4cqw 1.2cqw #000a;
  transition: left .16s ease, top .16s ease;
  display: grid; place-items: center; font-weight: 800; font-size: 2.4cqw; color: #111; line-height: 1;
}
.token.slide { transition: left .7s cubic-bezier(.5, 0, .3, 1), top .7s cubic-bezier(.5, 0, .3, 1); }
.token.active { animation: bob 1s ease-in-out infinite; }
@keyframes bob { 50% { transform: translate(-50%, -62%); } }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; z-index: 50; background: #000c; display: grid; place-items: center; padding: 16px; }
.modal-card {
  width: min(440px, 100%); max-height: 100%; overflow-y: auto; background: var(--panel); border: 1px solid var(--line);
  border-radius: 20px; padding: 24px 20px calc(20px + env(safe-area-inset-bottom)); text-align: center; animation: pop .2s ease;
}
@keyframes pop { from { transform: translateY(14px); opacity: 0; } }
.m-emoji { font-size: 52px; line-height: 1.1; }
.m-tag { display: inline-block; margin-top: 8px; font-size: 12px; padding: 3px 10px; border-radius: 99px; background: #0004; color: var(--muted); }
.modal-card h2 { margin: 12px 0 6px; font-size: 22px; line-height: 1.5; }
.modal-card p { margin: 0 0 12px; line-height: 1.9; }
.m-buttons { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.m-buttons .btn { flex: 1 1 140px; }
.m-timer { display: flex; flex-direction: column; align-items: center; gap: 10px; margin: 6px 0 12px; }
.m-timer .time { font-size: 40px; font-weight: 800; direction: ltr; font-variant-numeric: tabular-nums; }
.track { width: 100%; height: 6px; border-radius: 3px; background: #0005; overflow: hidden; }
.track i { display: block; height: 100%; width: 100%; background: var(--accent); transition: width 1s linear; }

@media (max-width: 599px) {
  .modal { place-items: end center; padding: 0; }
  .modal-card { border-radius: 20px 20px 0 0; width: 100%; }
}

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: calc(90px + env(safe-area-inset-bottom)); left: 50%; transform: translate(-50%, 20px); opacity: 0;
  background: #000e; border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px; transition: .2s;
  pointer-events: none; z-index: 60; max-width: calc(100% - 32px); width: max-content; text-align: center; font-size: 14px; line-height: 1.7;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.confetti { position: fixed; top: -10px; width: 8px; height: 12px; z-index: 70; pointer-events: none; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }
