@font-face {
  font-family: 'Fredoka';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('fonts/fredoka-hebrew.woff2') format('woff2');
  unicode-range: U+0307-0308, U+0590-05FF, U+200C-2010, U+20AA, U+25CC, U+FB1D-FB4F;
}
@font-face {
  font-family: 'Fredoka';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('fonts/fredoka-latin-ext.woff2') format('woff2');
  unicode-range: U+0100-02BA, U+02BD-02C5, U+02C7-02CC, U+02CE-02D7, U+02DD-02FF, U+0304, U+0308, U+0329, U+1D00-1DBF, U+1E00-1E9F, U+1EF2-1EFF, U+2020, U+20A0-20AB, U+20AD-20C4, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
@font-face {
  font-family: 'Fredoka';
  font-style: normal;
  font-weight: 400 700;
  font-display: swap;
  src: url('fonts/fredoka-latin.woff2') format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  background: #2b1d3a;
  font-family: 'Fredoka', 'Trebuchet MS', system-ui, sans-serif;
  color: #3a2a1a;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

#app { position: fixed; inset: 0; }

.screen { position: absolute; inset: 0; display: none; }
.screen.active { display: flex; }

/* ===== Menu ===== */
#menu {
  background:
    radial-gradient(circle at 20% 15%, rgba(255, 255, 255, .55) 0 6%, transparent 7%),
    radial-gradient(circle at 85% 80%, rgba(255, 255, 255, .25) 0 9%, transparent 10%),
    radial-gradient(circle at 30% 20%, #ffe7a8, #ffb347 45%, #ff7f50);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex-direction: column;
}

.menu-inner {
  width: min(1100px, 100%);
  margin: auto;
  padding: clamp(12px, 3vw, 32px);
  padding-top: max(clamp(12px, 3vw, 32px), env(safe-area-inset-top));
  padding-bottom: max(clamp(12px, 3vw, 32px), env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
}

@media (min-aspect-ratio: 1/1) and (min-width: 700px) {
  .menu-inner { grid-template-columns: minmax(240px, 1fr) 1.6fr; align-items: center; }
}

.title-block { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 12px; }

#demo {
  width: clamp(90px, min(22vw, 20vh), 190px);
  height: clamp(90px, min(22vw, 20vh), 190px);
  animation: bob 2.4s ease-in-out infinite;
  filter: drop-shadow(0 8px 10px rgba(0, 0, 0, .25));
}

h1 {
  font-size: clamp(2.2rem, 7vw, 3.8rem);
  line-height: .95;
  color: #fff;
  text-shadow: 0 4px 0 #c0502b, 0 8px 18px rgba(0, 0, 0, .25);
  letter-spacing: 1px;
}
h1 span { display: block; font-size: 1em; color: #fff6d5; margin-top: 2px; }
#startBtn { margin: 14px 0; }

@media (max-height: 720px) and (orientation: portrait) {
  .menu-inner { gap: 12px; }
  .title-block { gap: 8px; }
  #menu .card { padding: 12px 16px; }
  #menu .card h2 { font-size: 1.15rem; margin-bottom: 6px; }
  #menu .scores li { padding: 4px 10px; }
}
@media (max-height: 600px) and (orientation: portrait) {
  .menu-inner { gap: 8px; padding-top: max(8px, env(safe-area-inset-top)); padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  .title-block { gap: 6px; }
  #demo { width: 60px; height: 60px; }
  h1 { font-size: 1.9rem; }
  .btn.big { font-size: 1.2rem; }
  #startBtn { margin: 6px 0; }
  .toggles .btn.small { font-size: .8rem; }
  #menu .scores li { padding: 3px 10px; }
}

.brand {
  flex: none;
  padding: 8px 12px max(14px, env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #7a3b1a;
  font-weight: 600;
  font-size: .95rem;
  letter-spacing: .5px;
}
.brand img {
  width: 40px; height: auto;
  background: rgba(255, 255, 255, .85);
  border-radius: 10px;
  padding: 3px;
  box-shadow: 0 2px 0 rgba(0, 0, 0, .12);
}
@media (max-height: 720px) and (orientation: portrait) {
  .brand img { width: 30px; }
  .brand { font-size: .85rem; }
}

.toggles { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px 10px; }

/* How to Play modal */
dialog.how {
  margin: auto;
  border: none;
  color: inherit;
  text-align: left;
  max-width: min(520px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
}
dialog.how::backdrop { background: rgba(30, 15, 40, .55); }
dialog.how h2 { text-align: center; }
dialog.how h3 { color: #c0502b; font-size: 1.15rem; }
.tutorial-copy, .tutorial-captions { display: grid; }
.tutorial-copy > *, .tutorial-captions > * { grid-area: 1 / 1; }
.tutorial-measure { visibility: hidden; }
.tutorial-progress { color: #986039; font-size: .85rem; font-weight: 700; }
.tutorial-description { min-height: 3.9em; line-height: 1.3; }
#tutorialDemo { display: block; width: 100%; height: auto; background: #d7eff0; border-radius: 14px; }
.tutorial-caption { text-align: center; font-size: .9rem; font-weight: 700; color: #a14820; min-height: 1.3em; }
.tutorial-controls .btn { font-size: .9rem; line-height: 1.6; }
#tutorialNext { width: 8em; }
@media (any-pointer: coarse) {
  dialog.how .keys { display: none; }
}
@media (max-height: 600px) and (orientation: portrait) {
  dialog.how { padding: 12px 16px; }
  #tutorialDemo { width: min(100%, 40vh); margin-inline: auto; }
}
@media (max-height: 480px) and (orientation: landscape) {
  dialog.how[open] {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-areas: "heading heading" "text demo" "keys caption" "controls controls";
    gap: 2px 12px;
    padding: 10px 16px;
  }
  dialog.how h2 { grid-area: heading; }
  .tutorial-copy { grid-area: text; }
  dialog.how .tutorial-description { min-height: 0; }
  #tutorialDemo { grid-area: demo; }
  .tutorial-captions { grid-area: caption; }
  dialog.how .keys { grid-area: keys; margin-top: 2px; }
  dialog.how .tutorial-controls { grid-area: controls; margin-top: 4px; }
}

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }

.card {
  background: rgba(255, 255, 255, .9);
  border-radius: 22px;
  padding: 16px 20px;
  box-shadow: 0 6px 0 rgba(0, 0, 0, .12);
}
.card h2 { font-size: 1.3rem; color: #c0502b; margin-bottom: 8px; }

.keys { margin-top: 10px; font-size: .85rem; opacity: .75; }
kbd { background: #eee; border-radius: 5px; padding: 0 5px; border: 1px solid #ccc; font-family: inherit; }

.scores { list-style: none; display: grid; gap: 4px; counter-reset: rank; }
.scores li {
  counter-increment: rank;
  display: grid;
  grid-template-columns: 2em 1fr auto auto;
  gap: 8px;
  align-items: center;
  padding: 6px 10px;
  border-radius: 12px;
  background: #fff4dc;
  font-weight: 600;
}
.scores li::before { content: counter(rank); color: #c0502b; font-weight: 700; }
.scores li:nth-child(1)::before { content: '🥇'; }
.scores li:nth-child(2)::before { content: '🥈'; }
.scores li:nth-child(3)::before { content: '🥉'; }
.scores li .lvl { font-size: .8rem; opacity: .65; }
.scores li .pts { color: #c0502b; }
.scores li.new { background: #ffe066; animation: pulse 1s ease-in-out 4; }
.scores li.empty { grid-template-columns: 1fr; text-align: center; opacity: .7; }
.scores li.empty::before { content: none; }

/* ===== Buttons ===== */
.btn {
  font-family: inherit;
  border: none;
  cursor: pointer;
  border-radius: 999px;
  padding: .55em 1.2em;
  font-weight: 700;
  font-size: 1rem;
  background: #fff;
  color: #c0502b;
  box-shadow: 0 4px 0 rgba(0, 0, 0, .22);
  transition: transform .08s, box-shadow .08s, filter .15s;
  touch-action: manipulation;
}
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0, 0, 0, .22); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.big {
  font-size: clamp(1.3rem, 4vw, 1.8rem);
  background: #5fd35f;
  color: #fff;
  box-shadow: 0 6px 0 #2f9a2f;
  text-shadow: 0 2px 0 rgba(0, 0, 0, .2);
  animation: pulse 1.6s ease-in-out infinite;
}
.btn.small { font-size: .9rem; padding: .4em .9em; }
.btn.primary { background: #5fd35f; color: #fff; box-shadow: 0 4px 0 #2f9a2f; }
.btn.go { background: #ffd23d; color: #7a3b00; box-shadow: 0 4px 0 #c99a00; min-width: 5.2em; }
.btn.go.dash { background: #ff5fa2; color: #fff; box-shadow: 0 4px 0 #b8306b; }
.btn.go.ready { animation: pulse 1s ease-in-out infinite; }
.btn.go.route-ready { --route-shadow: #c99a00; animation: routePulse .85s ease-in-out infinite; }
.btn.go.dash.route-ready { --route-shadow: #b8306b; }
.btn.icon { padding: .45em .7em; }

/* ===== Game screen ===== */
#game {
  flex-direction: column;
  background:
    radial-gradient(circle, rgba(255, 255, 255, .18) 2px, transparent 2.5px) 0 0 / 26px 26px,
    linear-gradient(160deg, #56c7c2, #3e8fb8);
}

.hud {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 14px;
  padding: 6px 12px;
  padding-top: max(6px, env(safe-area-inset-top));
  padding-left: max(12px, env(safe-area-inset-left));
  padding-right: max(12px, env(safe-area-inset-right));
  background: rgba(20, 30, 60, .35);
  color: #fff;
}
.stat { display: flex; flex-direction: column; line-height: 1.05; min-width: 44px; }
.stat small { font-size: .65rem; opacity: .8; text-transform: uppercase; letter-spacing: 1px; }
.stat b { font-size: 1.25rem; white-space: nowrap; }
.lives-count { font-size: .9em; margin-left: 1px; }
.stat b.bump { animation: bump .3s ease-out; }

.timer {
  flex: 1 1 140px;
  height: 26px;
  background: rgba(255, 255, 255, .25);
  border-radius: 13px;
  position: relative;
  overflow: hidden;
}
#timerFill {
  position: absolute; left: 0; top: 0; bottom: 0;
  width: 100%;
  background: linear-gradient(90deg, #ffd23d, #ff7a3d);
  border-radius: 13px;
}
#timerFill.run { background: linear-gradient(90deg, #5fd35f, #2fbf8f); }
#timerFill.danger { animation: flash .4s steps(2) infinite; }
#timerText {
  position: relative;
  display: block;
  text-align: center;
  line-height: 26px;
  font-weight: 600;
  font-size: .85rem;
  white-space: nowrap;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .5);
}
.hud-btns { display: flex; gap: 8px; }

@media (orientation: portrait) {
  .hud-btns { margin-left: auto; }
  .timer { order: 5; flex-basis: 100%; }
}
@media (max-width: 420px) {
  .hud { gap: 6px 10px; padding-left: max(8px, env(safe-area-inset-left)); padding-right: max(8px, env(safe-area-inset-right)); }
  .stat { min-width: 0; }
  .stat small { letter-spacing: .5px; }
  .stat b { font-size: 1.1rem; }
  .hud-btns { gap: 6px; }
  .hud .btn { font-size: .9rem; }
  .hud .btn.go { min-width: 0; padding: .5em .8em; }
  .hud .btn.icon { padding: .45em .6em; }
}
@media (max-width: 340px) {
  .hud { gap: 6px 8px; }
  .stat b { font-size: 1rem; }
  .hud .btn { font-size: .8rem; }
}

#play {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 10px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
}
#play.portrait { flex-direction: column; }

#board {
  border-radius: 10px;
  box-shadow: 0 0 0 6px #ff9f43, 0 0 0 10px #c56a1c, 0 14px 26px rgba(0, 0, 0, .35);
  touch-action: none;
  cursor: pointer;
}
#queue {
  cursor: pointer;
  touch-action: none;
  border-radius: 14px;
  background: rgba(255, 255, 255, .28);
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, .55);
}

#effects {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}

/* ===== Overlay ===== */
#overlay {
  position: absolute;
  inset: 0;
  background: rgba(30, 15, 40, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 10;
}
#overlay.hidden { display: none; }
.panel {
  background: #fff8e7;
  border-radius: 24px;
  padding: 20px 24px;
  max-width: 440px;
  width: 100%;
  max-height: 100%;
  overflow: auto;
  text-align: center;
  box-shadow: 0 10px 0 rgba(0, 0, 0, .2);
  animation: pop .4s cubic-bezier(.2, 1.6, .4, 1);
}
.panel h2 { font-size: clamp(1.5rem, 5vw, 2.1rem); color: #c0502b; margin-bottom: 6px; }
.panel p { margin: 6px 0; }
.panel .tip { font-size: 1.05rem; }
.panel .chips { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; margin: 10px 0; }
.panel .chips span { background: #ffe9b8; border-radius: 999px; padding: 3px 10px; font-weight: 600; font-size: .9rem; }
.panel table { margin: 10px auto; border-collapse: collapse; min-width: 220px; }
.panel td { padding: 3px 8px; text-align: left; }
.panel td:last-child { text-align: right; font-weight: 700; }
.panel tr.total td { border-top: 2px dashed #e0b77a; font-size: 1.2rem; color: #c0502b; }
.panel table.tally { cursor: pointer; min-width: min(300px, 100%); }
.panel .tally td { font-size: .95rem; }
.panel .tally .v { display: inline-block; min-width: 4.5em; font-variant-numeric: tabular-nums; }
.panel .tally td.neg { color: #d64545; }
.panel .tally tr.total td { font-size: 1.25rem; }
.panel .tally tr.pending, .panel .life-msg.pending { visibility: hidden; }
.panel .tally tr:not(.pending) td:first-child { animation: slideIn .25s ease-out; }
.panel .life-msg { animation: pop .3s ease-out; }
.bounce { animation: numBounce .45s cubic-bezier(.3, 1.6, .5, 1); }
@keyframes numBounce { 0% { transform: scale(1); } 45% { transform: scale(1.45) translateY(-3px); color: #ff8a00; } 100% { transform: scale(1); } }
@keyframes slideIn { from { transform: translateX(-12px); opacity: 0; } to { transform: none; opacity: 1; } }
.panel .row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 14px; }
.panel input {
  font-family: inherit;
  font-size: 1.1rem;
  padding: .45em .8em;
  border-radius: 999px;
  border: 3px solid #ffb347;
  text-align: center;
  width: min(240px, 100%);
  outline: none;
  -webkit-user-select: text;
  user-select: text;
}
.panel .big-emoji { font-size: 3rem; line-height: 1; animation: bob 1.2s ease-in-out infinite; display: inline-block; }
@media (max-height: 640px) {
  .panel .big-emoji { font-size: 2.2rem; }
  .panel h2 { margin-bottom: 2px; }
  .panel p { margin: 3px 0; }
  .panel table { margin: 6px auto; }
  .panel .tally td { padding: 2px 6px; font-size: .88rem; }
  .panel .tally tr.total td { font-size: 1.1rem; }
  .panel .row { margin-top: 8px; }
}

@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.06); } }
@keyframes routePulse {
  0%, 100% { transform: scale(1); box-shadow: 0 4px 0 var(--route-shadow), 0 0 0 0 rgba(255, 255, 255, .75); }
  50% { transform: scale(1.1); box-shadow: 0 4px 0 var(--route-shadow), 0 0 0 8px rgba(255, 255, 255, .15); }
}
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(-2deg); } 50% { transform: translateY(-8px) rotate(2deg); } }
@keyframes pop { from { transform: scale(.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes bump { 0% { transform: scale(1); } 40% { transform: scale(1.35); color: #ffe066; } 100% { transform: scale(1); } }
@keyframes flash { 50% { opacity: .35; } }

/* Compact HUD for short landscape phones */
@media (max-height: 460px) and (orientation: landscape) {
  .hud { padding-top: 4px; padding-bottom: 4px; }
  .stat b { font-size: 1.05rem; }
  .btn.go, .btn.icon { padding: .35em .8em; font-size: .9rem; }
  #play { padding: 8px; gap: 14px; }
}
