:root {
  --bg: #fff6e9;
  --ink: #2b2140;
  --muted: #8a7fa3;
  --panel: #ffffff;
  --line: #2b2140;
  --primary: #ff5d8f;
  --primary-dark: #d63c6e;
  --secondary: #4dabf7;
  --secondary-dark: #2b86cf;
  --yellow: #ffd43b;
  --green: #51cf66;
  --mark: #ff5d8f;
  --radius: 18px;
  --shadow: 0 5px 0 var(--line);
  font-family: "Fredoka", ui-rounded, "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }

/* Persian: rounded Persian font, no letter spacing (it breaks joined letters) */
:root:lang(fa) { font-family: "Vazirmatn", "Fredoka", system-ui, sans-serif; }
:lang(fa) .logo { letter-spacing: 0; font-weight: 800; }
:lang(fa) .dab-btn { letter-spacing: 0; }
:lang(fa) .join-row input::placeholder { letter-spacing: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  overflow-x: hidden;
}
button, input { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.center { text-align: center; }

.bg-dots {
  position: fixed; inset: 0; z-index: -1;
  background-image: radial-gradient(#ffd9a8 2px, transparent 2px);
  background-size: 28px 28px;
  opacity: .7;
}

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 22px;
}
.logo {
  font-weight: 700; font-size: 30px; letter-spacing: .5px;
  color: var(--primary);
  text-shadow: 2px 2px 0 var(--line);
  -webkit-text-stroke: 1px var(--line);
}
.logo span { color: var(--yellow); display: inline-block; animation: wobble 2.4s ease-in-out infinite; }
.topbar-right { display: flex; gap: 10px; align-items: center; }
.room-tag {
  background: var(--yellow); border: 2px solid var(--line); border-radius: 999px;
  padding: 4px 12px; font-weight: 600; letter-spacing: 2px;
}
.icon-btn {
  width: 42px; height: 42px; border-radius: 50%;
  border: 2px solid var(--line); background: #fff; cursor: pointer;
  box-shadow: 0 3px 0 var(--line); font-size: 18px;
}
.icon-btn:active { transform: translateY(3px); box-shadow: none; }

/* Panels */
.screen { padding: 10px 18px 40px; }
.card-panel {
  background: var(--panel);
  border: 3px solid var(--line);
  border-radius: 26px;
  box-shadow: 0 8px 0 var(--line);
  padding: 28px;
}
.home-panel, .lobby-panel { max-width: 440px; margin: 20px auto; text-align: center; }
h1 { margin: 10px 0 6px; font-size: 32px; }
h2 { margin: 24px 0 10px; font-size: 20px; }
h3 { margin: 0 0 12px; }

.balls-deco { display: flex; justify-content: center; gap: 10px; margin-bottom: 6px; }
.balls-deco span {
  width: 52px; height: 52px; border-radius: 50%;
  display: grid; place-items: center; font-weight: 700; font-size: 20px;
  background: var(--c); border: 3px solid var(--line);
  box-shadow: inset -5px -6px 0 rgba(0,0,0,.12);
  animation: bob 2s ease-in-out infinite;
}
.balls-deco span:nth-child(2) { animation-delay: .2s; }
.balls-deco span:nth-child(3) { animation-delay: .4s; }
.balls-deco span:nth-child(4) { animation-delay: .6s; }

.field { display: block; text-align: left; margin: 20px 0 14px; }
.field span { display: block; font-weight: 600; margin-bottom: 6px; }
input {
  width: 100%; padding: 12px 16px; font-size: 18px;
  border: 3px solid var(--line); border-radius: 14px; outline: none;
  background: #fffdf8;
}
input:focus { border-color: var(--secondary); }

.btn {
  border: 3px solid var(--line); border-radius: 14px;
  padding: 10px 18px; font-weight: 600; cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform .08s, box-shadow .08s;
}
.btn:active { transform: translateY(5px); box-shadow: 0 0 0 var(--line); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.big { width: 100%; font-size: 20px; padding: 14px; margin-top: 8px; }
.btn.small { font-size: 14px; padding: 6px 12px; box-shadow: 0 3px 0 var(--line); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-secondary { background: var(--secondary); color: #fff; }
.btn-ghost { background: #fff; margin-top: 14px; }

.or { margin: 20px 0 12px; position: relative; color: var(--muted); }
.or::before { content: ""; position: absolute; left: 0; right: 0; top: 50%; border-top: 2px dashed #e6dccb; }
.or span { background: #fff; position: relative; padding: 0 10px; }
.join-row { display: flex; gap: 10px; }
.join-row input { text-transform: uppercase; letter-spacing: 6px; text-align: center; font-weight: 600; }
.error { color: var(--primary-dark); min-height: 1.2em; margin: 10px 0 0; }

/* Lobby */
.big-code {
  font-size: 56px; font-weight: 700; letter-spacing: 10px;
  color: var(--secondary); text-shadow: 3px 3px 0 var(--line);
  -webkit-text-stroke: 1.5px var(--line);
}
.player-chips { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip {
  display: flex; align-items: center; gap: 6px;
  border: 2px solid var(--line); border-radius: 999px; padding-block: 4px; padding-inline: 4px 12px;
  background: #fff; animation: pop .3s ease-out;
}
.avatar {
  width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--line);
  display: grid; place-items: center; font-weight: 700; font-size: 14px;
}
.chip small { color: var(--muted); }
.setting { margin-top: 22px; display: flex; flex-direction: column; gap: 8px; font-weight: 600; }
.seg { display: flex; border: 3px solid var(--line); border-radius: 14px; overflow: hidden; }
.seg button {
  flex: 1; padding: 8px; border: 0; background: #fff; cursor: pointer; font-weight: 600;
}
.seg button + button { border-left: 2px solid var(--line); }
.seg button.on { background: var(--yellow); }
.seg.locked button { cursor: default; }

/* Game layout */
#game {
  display: grid; gap: 20px;
  grid-template-columns: 340px 1fr 220px;
  align-items: start;
  max-width: 1400px; margin: 0 auto;
}
@media (max-width: 1100px) {
  #game { grid-template-columns: 1fr; }
  .side { order: 3; }
  .board { max-width: 520px; margin: 0 auto; }
}

.caller { overflow: hidden; }
.board-wrap summary { cursor: pointer; font-weight: 600; color: var(--muted); margin-bottom: 8px; }
.caller, .side { background: #fff; border: 3px solid var(--line); border-radius: 22px; box-shadow: 0 6px 0 var(--line); padding: 16px; }
.ball-stage { display: flex; gap: 16px; align-items: center; margin-bottom: 14px; }
.big-ball {
  flex: none; width: 120px; height: 120px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff 0 18%, var(--ball, #ffd43b) 19%);
  border: 4px solid var(--line);
  box-shadow: inset -10px -12px 0 rgba(0,0,0,.12), 0 6px 0 var(--line);
  display: grid; place-items: center;
}
.big-ball span {
  background: #fff; width: 66px; height: 66px; border-radius: 50%;
  border: 3px solid var(--line);
  display: grid; place-items: center; font-weight: 700; font-size: 34px;
}
.big-ball.roll { animation: roll .6s cubic-bezier(.3,1.6,.5,1); }
.caller-info { flex: 1; min-width: 0; }
.recent { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.recent span {
  width: 32px; height: 32px; border-radius: 50%; border: 2px solid var(--line);
  display: grid; place-items: center; font-size: 13px; font-weight: 600; background: var(--c);
}
.recent span:first-child { animation: pop .3s; }

.board { display: grid; grid-template-columns: repeat(10, 1fr); gap: 4px; }
.board div {
  aspect-ratio: 1; border-radius: 8px; display: grid; place-items: center;
  font-size: 13px; font-weight: 600; color: #c9bfdb; background: #f6f1ff;
}
.board div.on { color: var(--ink); background: var(--c); border: 2px solid var(--line); }
.board div.last { animation: pulse 1s ease-in-out infinite; }

/* Cards */
.my-cards { display: flex; flex-direction: column; gap: 18px; }
#cards { display: flex; flex-direction: column; gap: 18px; }
.watching {
  background: #fff; border: 3px dashed var(--line); border-radius: 18px; padding: 24px;
  text-align: center; font-size: 18px; font-weight: 600;
}
.ticket {
  background: var(--tc, #fff);
  border: 3px solid var(--line); border-radius: 22px;
  box-shadow: 0 6px 0 var(--line);
  padding: 14px;
  animation: dealIn .5s ease-out both;
}
.ticket:nth-child(2) { animation-delay: .12s; }
.ticket:nth-child(3) { animation-delay: .24s; }
.ticket-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; font-weight: 600; }
.grid { display: grid; grid-template-columns: repeat(9, 1fr); gap: 6px; }
.cell {
  aspect-ratio: 1; border-radius: 12px;
  display: grid; place-items: center;
  font-size: clamp(14px, 2.2vw, 26px); font-weight: 700;
  position: relative; user-select: none;
}
.cell.empty { background: rgba(255,255,255,.35); border: 2px dashed rgba(43,33,64,.18); }
.cell.num { background: #fff; border: 3px solid var(--line); cursor: pointer; transition: transform .1s; }
.cell.num:hover { transform: scale(1.06); }
.cell.num.called:not(.marked) { box-shadow: 0 0 0 3px var(--yellow); }
.cell.marked::after {
  content: ""; position: absolute; inset: 8%;
  border-radius: 50%; background: var(--mark); opacity: .78;
  animation: stamp .25s ease-out;
}
.cell.marked { color: #fff; }
.cell span { position: relative; z-index: 1; }
.cell.shake { animation: shake .35s; }
.dab-btn {
  border: 3px solid var(--line); border-radius: 12px; padding: 6px 14px;
  font-weight: 700; font-size: 16px; letter-spacing: 1px;
  background: #e9e3f5; color: var(--muted); cursor: pointer;
  box-shadow: 0 3px 0 var(--line);
}
.dab-btn.ready { background: var(--green); color: #fff; animation: pulse 0.9s ease-in-out infinite; }
.dab-btn:active { transform: translateY(3px); box-shadow: none; }
.tip { text-align: center; margin: 0; }

/* Side */
.score-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.score-list li { display: flex; align-items: center; gap: 8px; }
.score-list .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.togo {
  font-size: 13px; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  background: #f3eefc; white-space: nowrap;
}
.togo.hot { background: var(--primary); color: #fff; animation: pulse 1s infinite; }
.reactions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 18px; padding-top: 14px; border-top: 2px dashed #eee3d0; }
.reactions button {
  font-size: 22px; width: 42px; height: 42px; border-radius: 12px;
  border: 2px solid var(--line); background: #fff; cursor: pointer;
}
.reactions button:active { transform: scale(.9); }

/* Overlays */
.overlay {
  position: fixed; inset: 0; background: rgba(43,33,64,.45);
  display: grid; place-items: center; z-index: 20; padding: 18px;
}
.winner-panel { max-width: 560px; width: 100%; text-align: center; animation: pop .4s ease-out; }
.trophy { font-size: 64px; animation: bob 1.5s ease-in-out infinite; }
#winnerCard { margin: 18px 0 6px; }
#winnerCard .ticket { animation: none; }
#winnerCard .cell { font-size: 18px; cursor: default; }

.countdown {
  position: fixed; inset: 0; display: grid; place-items: center; z-index: 15; pointer-events: none;
  font-size: 160px; font-weight: 700; color: var(--yellow);
  -webkit-text-stroke: 4px var(--line); text-shadow: 6px 6px 0 var(--line);
}
.countdown span { animation: zoomOut .9s ease-out both; }

.toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 30; align-items: center; }
.toast {
  background: var(--ink); color: #fff; border-radius: 999px; padding: 10px 18px;
  font-weight: 500; animation: toastIn .3s ease-out; box-shadow: 0 4px 0 rgba(0,0,0,.25);
}
.toast.oops { background: var(--primary); }

.floaters { position: fixed; inset: 0; pointer-events: none; z-index: 25; overflow: hidden; }
.floater { position: absolute; bottom: -60px; font-size: 40px; animation: floatUp 2.6s ease-out forwards; text-align: center; }
.floater small { display: block; font-size: 12px; font-weight: 600; background: #fff; border: 2px solid var(--line); border-radius: 8px; padding: 0 6px; }
#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 21; width: 100%; height: 100%; }

/* Animations */
@keyframes bob { 50% { transform: translateY(-8px); } }
@keyframes wobble { 0%,100% { transform: rotate(0); } 25% { transform: rotate(-12deg); } 75% { transform: rotate(12deg); } }
@keyframes pop { 0% { transform: scale(.4); opacity: 0; } 70% { transform: scale(1.1); } 100% { transform: scale(1); opacity: 1; } }
@keyframes pulse { 50% { transform: scale(1.08); } }
@keyframes roll { 0% { transform: translateX(-60px) rotate(-360deg) scale(.6); } 100% { transform: translateX(0) rotate(0); } }
@keyframes stamp { 0% { transform: scale(1.8); opacity: 0; } 100% { transform: scale(1); opacity: .78; } }
@keyframes shake { 20%,60% { transform: translateX(-5px); } 40%,80% { transform: translateX(5px); } }
@keyframes dealIn { 0% { transform: translateY(40px) rotate(-3deg); opacity: 0; } 100% { transform: none; opacity: 1; } }
@keyframes toastIn { from { transform: translateY(20px); opacity: 0; } }
@keyframes floatUp { 0% { transform: translateY(0) scale(.6); opacity: 0; } 15% { opacity: 1; transform: translateY(-80px) scale(1.1); } 100% { transform: translateY(-80vh) scale(1); opacity: 0; } }
@keyframes zoomOut { 0% { transform: scale(2.2); opacity: 0; } 30% { transform: scale(1); opacity: 1; } 100% { transform: scale(.8); opacity: 0; } }
