/* מרוץ החשבון — bright, saturated, cartoonish (Pixar Cars energy).
   RTL Hebrew, mobile-first (375px), 44px touch targets, logical properties,
   transform+opacity animations only. */

:root {
  --blue: #1b8ef2;
  --blue-d: #0a6fce;
  --green: #34c759;
  --green-d: #1e9b41;
  --orange: #ff9500;
  --orange-d: #d67400;
  --red: #ff3b30;
  --yellow: #ffd60a;
  --ink: #1c2b3a;
  --ink-soft: #48627b;
  --paper: #ffffff;
  --sky-1: #7fd0ff;
  --sky-2: #c9efff;
  --road: #38414d;
  --radius: 22px;
  --shadow-btn: 0 6px 0;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  font-size: 16px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  font-family: 'Heebo', system-ui, sans-serif;
  color: var(--ink);
  background: var(--sky-2);
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

#app { position: relative; height: 100dvh; width: 100vw; overflow: hidden; }

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: calc(16px + var(--safe-t)) 16px calc(16px + var(--safe-b));
  animation: screen-in .28s ease-out both;
}
.screen[hidden] { display: none; }
@keyframes screen-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) { .screen { animation: none; } }

/* ---------- buttons ---------- */
.btn {
  appearance: none;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-weight: 900;
  font-size: 1.25rem;
  color: #fff;
  min-height: 44px;
  padding: 16px 24px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: transform .08s ease, box-shadow .08s ease, opacity .15s ease, filter .15s ease;
  will-change: transform;
}
.btn-emoji { font-size: 1.4em; line-height: 1; }
.btn-huge { width: 100%; font-size: 1.6rem; padding: 20px; min-height: 68px; }
.btn-primary { background: var(--blue); box-shadow: var(--shadow-btn) var(--blue-d); }
.btn-secondary { background: var(--green); box-shadow: var(--shadow-btn) var(--green-d); }
.btn-ghost { background: rgba(255,255,255,.85); color: var(--ink); box-shadow: var(--shadow-btn) #c4d3e2; }
.btn:active:not(:disabled) { transform: translateY(4px); box-shadow: 0 2px 0 var(--blue-d); }
.btn-secondary:active:not(:disabled) { box-shadow: 0 2px 0 var(--green-d); }
.btn:disabled { opacity: .45; filter: grayscale(.3); cursor: default; }
.btn-primary:not(:disabled) { animation: btn-pulse 1.6s ease-in-out infinite; }
@keyframes btn-pulse { 0%,100% { transform: none; } 50% { transform: scale(1.025); } }
@media (prefers-reduced-motion: reduce) { .btn-primary:not(:disabled) { animation: none; } }

.back-btn {
  position: absolute;
  inset-block-start: calc(12px + var(--safe-t));
  inset-inline-start: 12px;
  width: 48px; height: 48px;
  border: none; border-radius: 50%;
  background: rgba(255,255,255,.9);
  box-shadow: 0 3px 0 #c4d3e2;
  font-size: 1.6rem; font-weight: 900; color: var(--ink);
  cursor: pointer; z-index: 5;
}
.back-btn:active { transform: translateY(2px); }

/* ---------- home ---------- */
[data-screen="home"] { justify-content: space-between; align-items: center; }
.home-bg {
  position: absolute; inset: 0; z-index: -1;
  background:
    radial-gradient(120% 60% at 50% 0%, var(--sky-1), var(--sky-2) 70%),
    linear-gradient(180deg, transparent 62%, #6bbf3f 62%, #5aa835 100%);
}
.home-bg::after {
  content: ""; position: absolute; inset-inline: 0; bottom: 0; height: 38%;
  background: repeating-linear-gradient(90deg, var(--road) 0 40px, #313a45 40px 80px);
  border-block-start: 6px solid #2a323c;
}
.home-inner { display: flex; flex-direction: column; align-items: center; justify-content: space-between; height: 100%; width: 100%; max-width: 460px; gap: 12px; }
.game-title {
  margin: 12px 0 0;
  text-align: center;
  font-size: clamp(2.6rem, 13vw, 4rem);
  font-weight: 900;
  line-height: .95;
  color: #fff;
  text-shadow: 0 4px 0 var(--blue-d), 0 6px 14px rgba(0,0,0,.25);
}
.game-title span { color: var(--yellow); text-shadow: 0 4px 0 var(--orange-d), 0 6px 14px rgba(0,0,0,.25); }
.home-car { width: min(78vw, 340px); filter: drop-shadow(0 12px 10px rgba(0,0,0,.22)); animation: bob 2.6s ease-in-out infinite; }
.home-car svg { width: 100%; height: auto; display: block; }
@keyframes bob { 0%,100% { transform: translateY(0) rotate(-1deg); } 50% { transform: translateY(-10px) rotate(1deg); } }
@media (prefers-reduced-motion: reduce) { .home-car { animation: none; } }
.home-actions { width: 100%; display: flex; flex-direction: column; gap: 14px; margin-bottom: 6px; }

/* ---------- lobby (create/join) ---------- */
.lobby { margin: auto; width: 100%; max-width: 440px; display: flex; flex-direction: column; align-items: center; gap: 18px; }
.lobby-block { width: 100%; display: flex; flex-direction: column; align-items: center; gap: 18px; animation: screen-in .25s ease-out both; }
.lobby-label { margin: 0; font-size: 1.3rem; font-weight: 800; color: var(--ink-soft); }
.lobby-hint { margin: 0; font-size: 1.1rem; color: var(--ink-soft); text-align: center; }
.code-display {
  font-size: clamp(3.4rem, 18vw, 5.4rem);
  font-weight: 900;
  letter-spacing: .14em;
  color: var(--blue);
  background: #fff;
  border-radius: var(--radius);
  padding: 12px 28px;
  box-shadow: 0 6px 0 #cfe0f0, inset 0 0 0 4px #eaf3fc;
  direction: ltr;
  min-width: 3.5em;
  text-align: center;
}
.code-input { color: var(--ink); }
.lobby-status { display: flex; align-items: center; gap: 10px; font-size: 1.15rem; font-weight: 700; color: var(--ink-soft); }
.lobby-status-ok { color: var(--green-d); }
.lobby-status-warn { color: var(--orange-d); }
.tick { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; border-radius: 50%; background: var(--green); color: #fff; }
.inline-error { margin: -6px 0 0; color: var(--red); font-weight: 800; font-size: 1.05rem; min-height: 1.2em; }
.err-icon { font-size: 3rem; }

/* spinner */
.spinner { width: 46px; height: 46px; border-radius: 50%; border: 6px solid #d5e6f6; border-top-color: var(--blue); animation: spin 1s linear infinite; }
.spinner-sm { width: 24px; height: 24px; border-width: 4px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- keypad ---------- */
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 100%; max-width: 380px; }
.keypad-race { max-width: 100%; gap: 8px; }
.key {
  appearance: none; border: none;
  font-family: inherit; font-weight: 900;
  font-size: 1.9rem;
  min-height: 56px;
  border-radius: 16px;
  background: #fff;
  color: var(--ink);
  box-shadow: 0 4px 0 #cbd8e6;
  cursor: pointer;
  transition: transform .06s ease, box-shadow .06s ease, background .1s;
  display: flex; align-items: center; justify-content: center;
}
.keypad-race .key { min-height: 52px; font-size: 1.7rem; }
.key:active { transform: translateY(3px); box-shadow: 0 1px 0 #cbd8e6; background: #eef5fc; }
.key-send { background: var(--green); color: #fff; box-shadow: 0 4px 0 var(--green-d); }
.key-send:active { box-shadow: 0 1px 0 var(--green-d); }
.key-del { background: #ffe1c2; color: var(--orange-d); box-shadow: 0 4px 0 #f0c79a; }

/* ---------- countdown ---------- */
[data-screen="countdown"] {
  align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% 40%, var(--blue), var(--blue-d));
}
.count-num {
  font-size: clamp(7rem, 42vw, 16rem);
  font-weight: 900; color: #fff;
  text-shadow: 0 8px 0 rgba(0,0,0,.18);
}
.count-num.pop { animation: count-pop .8s ease-out; }
.count-num.go { color: var(--yellow); }
@keyframes count-pop { 0% { transform: scale(.3); opacity: 0; } 30% { transform: scale(1.15); opacity: 1; } 70% { transform: scale(1); } 100% { transform: scale(.85); opacity: .2; } }

/* ---------- race ---------- */
[data-screen="race"] { padding: 0; justify-content: space-between; }
.track {
  position: relative;
  flex: 1 1 auto;
  min-height: 42%;
  overflow: hidden;
  direction: ltr; /* the racetrack is a physical space, not text — cars run left→right */
  background: linear-gradient(180deg, var(--sky-1) 0%, var(--sky-2) 55%);
  padding-top: var(--safe-t);
}
.track-sky::before {
  content: "☁️ ☁️"; position: absolute; inset-block-start: 14%; inset-inline: 0;
  text-align: center; font-size: 2rem; opacity: .8; letter-spacing: 40px;
}
.track-road {
  position: absolute; inset-inline: 0; bottom: 0; height: 62%;
  background:
    linear-gradient(180deg, #454f5c 0 6px, var(--road) 6px 100%);
  border-block-start: 5px solid #eef2f5;
}
.track-road::before {
  content: ""; position: absolute; inset-inline: 0; top: 50%; height: 6px; transform: translateY(-50%);
  background: repeating-linear-gradient(90deg, #f5d33a 0 34px, transparent 34px 70px);
  animation: road-move .6s linear infinite;
}
@keyframes road-move { from { background-position-x: 0; } to { background-position-x: -70px; } }
.track.paused .track-road::before { animation-play-state: paused; }
@media (prefers-reduced-motion: reduce) { .track-road::before { animation: none; } }

.lane { position: absolute; inset-inline: 0; height: 0; }
.lane-opp { bottom: 46%; z-index: 3; }
.lane-own { bottom: 20%; z-index: 4; }
.car {
  position: absolute;
  left: 50%;
  width: 128px;
  transform: translateX(-50%); /* own car pinned to horizontal centre */
}
.car svg { width: 100%; height: auto; display: block; filter: drop-shadow(0 8px 6px rgba(0,0,0,.25)); }
.car-own { width: 138px; }
.car-opp { transition: transform .32s cubic-bezier(.2,.8,.25,1); }
.car-own.boost svg { animation: boost-shake .34s ease-out; }
@keyframes boost-shake {
  0% { transform: translateX(0) scale(1); }
  30% { transform: translateX(calc(var(--kick, 8px) * -1)) scale(1.07, .95); }
  60% { transform: translateX(calc(var(--kick, 8px) * .3)) scale(1.02, .99); }
  100% { transform: none; }
}
/* speed-lines flash behind the car on a correct answer (kinetic, no text) */
.car-own.boost::before {
  content: ""; position: absolute; inset-block: 30% 30%; right: 100%; width: 46px;
  background: repeating-linear-gradient(90deg, rgba(255,255,255,.9) 0 4px, transparent 4px 12px);
  border-radius: 4px;
  animation: speed-flash .34s ease-out;
}
@keyframes speed-flash { from { opacity: .9; transform: translateX(20px); } to { opacity: 0; transform: translateX(-10px); } }

.edge-cue {
  position: absolute; inset-block-start: -8px;
  display: flex; align-items: center; gap: 4px;
  background: rgba(28,43,58,.85); color: #fff;
  padding: 6px 10px; border-radius: 14px;
  font-weight: 900; font-size: 1rem; direction: ltr;
  animation: cue-pulse 1s ease-in-out infinite;
}
.edge-cue-right { inset-inline-end: 8px; }
.edge-cue-left { inset-inline-start: 8px; }
.cue-arrow { font-size: 1.2rem; }
@keyframes cue-pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.08); } }
@media (prefers-reduced-motion: reduce) { .edge-cue { animation: none; } }

.finish-flag {
  position: absolute; inset-block-end: 12%; left: 50%;
  width: 22px; height: 46%;
  background:
    repeating-conic-gradient(#1c2b3a 0 25%, #fff 0 50%) 0 0 / 16px 16px;
  border-inline-start: 4px solid #1c2b3a;
  transform: translateX(-50%);
  opacity: 0;
  z-index: 2;
}
.finish-flag.visible { opacity: 1; }

.qpanel {
  flex: 0 0 auto;
  background: #fff;
  border-start-start-radius: 26px;
  border-start-end-radius: 26px;
  box-shadow: 0 -8px 24px rgba(0,0,0,.12);
  padding: 14px 14px calc(14px + var(--safe-b));
  display: flex; flex-direction: column; gap: 10px;
}
.qrow { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.question {
  font-size: clamp(2rem, 9vw, 2.8rem); font-weight: 900; color: var(--ink);
  direction: ltr; white-space: nowrap;
}
.answer {
  display: flex; align-items: baseline; gap: 6px; direction: ltr;
  font-size: clamp(1.8rem, 8vw, 2.6rem); font-weight: 900;
  background: #eef5fc; border-radius: 14px; padding: 6px 16px; min-width: 3.4em; justify-content: center;
  box-shadow: inset 0 0 0 3px #dbeafd;
}
.answer-text { color: var(--blue); min-width: .6em; }
.answer-text.filled { color: var(--blue); }
.answer-eq { color: var(--ink-soft); }

/* ---------- finish screens ---------- */
.screen-finish { align-items: center; justify-content: center; overflow: hidden; }
[data-screen="winner"] { background: radial-gradient(circle at 50% 35%, #ffe89b, #ffb43f); }
[data-screen="loser"] { background: linear-gradient(180deg, var(--sky-1), var(--sky-2)); }
.confetti { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.finish-inner { position: relative; z-index: 2; display: flex; flex-direction: column; align-items: center; gap: 22px; width: 100%; max-width: 460px; padding: 20px; }
.win-badge { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.win-car { width: min(72vw, 320px); animation: win-bounce 1s ease infinite; }
.win-car svg { width: 100%; height: auto; }
@keyframes win-bounce { 0%,100% { transform: translateY(0) scale(1); } 50% { transform: translateY(-14px) scale(1.04); } }
.win-title { margin: 0; font-size: clamp(2.4rem, 11vw, 3.4rem); font-weight: 900; color: #fff; text-shadow: 0 4px 0 var(--orange-d); }

.unlock-card {
  background: #fff; border-radius: var(--radius); padding: 18px 22px;
  display: flex; flex-direction: column; align-items: center; gap: 6px; width: 100%;
  box-shadow: 0 10px 0 rgba(0,0,0,.12); animation: unlock-in .5s cubic-bezier(.2,1.3,.4,1) both;
}
@keyframes unlock-in { from { transform: scale(.6) translateY(20px); opacity: 0; } to { transform: none; opacity: 1; } }
.unlock-label { margin: 0; font-weight: 900; color: var(--orange-d); font-size: 1.2rem; }
.unlock-car { width: min(60vw, 260px); }
.unlock-car svg { width: 100%; height: auto; }
.unlock-name { margin: 0; font-size: 1.4rem; font-weight: 900; color: var(--ink); }

.finish-actions { width: 100%; display: flex; flex-direction: column; gap: 12px; animation: screen-in .3s both; }

.lose-scene { position: relative; width: 100%; height: 150px; display: flex; align-items: center; justify-content: center; }
.lose-flag {
  position: absolute; inset-inline-end: 8%; inset-block: 0; width: 20px;
  background: repeating-conic-gradient(#1c2b3a 0 25%, #fff 0 50%) 0 0 / 14px 14px;
  border-inline-start: 4px solid #1c2b3a;
}
.lose-car { width: 190px; animation: lose-roll 2.4s ease-in-out infinite; }
.lose-car svg { width: 100%; height: auto; filter: drop-shadow(0 8px 6px rgba(0,0,0,.2)); }
@keyframes lose-roll { 0%,100% { transform: translateX(0); } 50% { transform: translateX(6px); } }

/* ---------- overlay ---------- */
.overlay {
  position: absolute; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  background: rgba(20,30,42,.6); backdrop-filter: blur(3px);
  animation: screen-in .2s ease both;
}
.overlay[hidden] { display: none; }
.overlay-card {
  background: #fff; border-radius: var(--radius); padding: 28px 24px;
  display: flex; flex-direction: column; align-items: center; gap: 16px;
  width: min(88vw, 360px); box-shadow: 0 12px 40px rgba(0,0,0,.3);
}
.overlay-title { margin: 0; font-size: 1.5rem; font-weight: 900; }

/* wider screens: keep it phone-shaped and centred */
@media (min-width: 620px) {
  #app { max-width: 560px; margin-inline: auto; box-shadow: 0 0 60px rgba(0,0,0,.15); }
}
