* { margin: 0; padding: 0; box-sizing: border-box; }
  html, body { width: 100%; height: 100%; overflow: hidden; background: #7ec8e3; font-family: 'Tahoma', 'Segoe UI', sans-serif; }
  #gameCanvas { display: block; width: 100%; height: 100%; }

  #crosshair {
    position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
    width: 20px; height: 20px; pointer-events: none; z-index: 10;
  }
  #crosshair::before, #crosshair::after {
    content: ''; position: absolute; background: rgba(255,255,255,0.85);
  }
  #crosshair::before { width: 100%; height: 3px; top: 50%; left: 0; transform: translateY(-50%); }
  #crosshair::after { width: 3px; height: 100%; left: 50%; top: 0; transform: translateX(-50%); }

  #hud {
    position: fixed; top: 16px; right: 16px; z-index: 20;
    display: flex; flex-direction: column; gap: 10px; align-items: flex-end;
  }
  .pill {
    background: rgba(20,25,40,0.75); color: #fff; padding: 10px 20px;
    border-radius: 30px; border: 3px solid #ffd23f; font-weight: bold;
    font-size: 18px; box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    display: flex; align-items: center; gap: 8px;
  }
  #hearts { display: flex; gap: 4px; font-size: 22px; }
  #coinsPill { border-color: #ffb703; }

  #timerPill {
    border-color: #ff6b6b;
    transition: transform 0.15s ease-out, background 0.15s ease-out;
  }
  #timerPill.low { border-color: #ffa94d; }
  #timerPill.pulse { transform: scale(1.12); background: rgba(200,80,40,0.85); }

  #playerForm { margin-bottom: 18px; display: flex; gap: 14px; flex-wrap: wrap; justify-content: center; }
  .field { display: flex; flex-direction: column; align-items: center; gap: 6px; }
  .field label { font-size: 14px; opacity: 0.9; }
  .field input {
    padding: 10px 18px; border-radius: 30px; border: 3px solid #ffd23f;
    font-size: 16px; text-align: center; width: 220px; max-width: 80vw; font-family: inherit;
    outline: none; background: #fff; color: #222;
  }
  .field input.invalid { border-color: #fa5252; box-shadow: 0 0 0 3px rgba(250,82,82,0.35); }
  #formError {
    width: 100%; margin: 0 auto 14px; max-width: 460px; background: rgba(250,82,82,0.92);
    color: #fff; padding: 8px 16px; border-radius: 14px; font-size: 15px; font-weight: bold;
  }

  .leaderboardWrap {
    margin-top: 22px; background: rgba(0,0,0,0.35); border-radius: 14px;
    padding: 14px 22px; max-width: 380px; width: 100%;
  }
  .leaderboardWrap h3 { font-size: 16px; margin-bottom: 8px; color: #ffd23f; }
  #leaderboardList { list-style: none; text-align: right; font-size: 15px; line-height: 1.9; }
  #leaderboardList li { display: flex; justify-content: space-between; gap: 10px; }
  #leaderboardList li .rank { color: #ffd23f; font-weight: bold; margin-left: 6px; }

  #questionPanel {
    position: fixed; top: 16px; left: 50%; transform: translateX(-50%);
    z-index: 20; background: linear-gradient(180deg, #4c6ef5, #364fc7);
    border: 4px solid #ffd23f; border-radius: 18px; padding: 14px 32px;
    color: #fff; text-align: center; box-shadow: 0 6px 18px rgba(0,0,0,0.4);
    min-width: 280px;
  }
  #questionLabel { font-size: 14px; opacity: 0.85; margin-bottom: 4px; }
  #questionText { font-size: 32px; font-weight: bold; letter-spacing: 2px; }

  #answerBar {
    position: fixed; bottom: 30px; left: 50%; transform: translateX(-50%);
    z-index: 20; display: flex; gap: 14px;
  }
  .answerBlock {
    width: 64px; height: 64px; border-radius: 12px;
    display: flex; align-items: center; justify-content: center;
    font-size: 26px; font-weight: bold; color: #fff;
    border: 3px solid rgba(255,255,255,0.6);
    box-shadow: 0 4px 8px rgba(0,0,0,0.4), inset 0 -6px 0 rgba(0,0,0,0.2);
  }

  #instructions, #startScreen, #endScreen {
    position: fixed; inset: 0; z-index: 100;
    background: linear-gradient(180deg, #74c0fc, #4dabf7 40%, #91d17b 60%, #40c057);
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    text-align: center; color: #fff; padding: 20px;
  }
  #startScreen h1 {
    font-size: 52px; text-shadow: 4px 4px 0 #2b8a3e, 8px 8px 0 rgba(0,0,0,0.2);
    margin-bottom: 10px; color: #ffd23f;
  }
  #startScreen h2 { font-size: 20px; margin-bottom: 30px; text-shadow: 2px 2px 0 rgba(0,0,0,0.3); }
  .blockRow { display: flex; gap: 10px; margin-bottom: 30px; }
  .demoBlock {
    width: 50px; height: 50px; border-radius: 8px;
    box-shadow: inset 0 -8px 0 rgba(0,0,0,0.25), 0 4px 6px rgba(0,0,0,0.3);
    animation: bob 2s ease-in-out infinite;
  }
  @keyframes bob { 0%,100% { transform: translateY(0);} 50% { transform: translateY(-10px);} }

  #levelSelect { display: flex; gap: 18px; margin-bottom: 26px; flex-wrap: wrap; justify-content: center; }
  .levelBtn {
    background: #fff; color: #364fc7; border: none; padding: 18px 26px;
    border-radius: 16px; font-size: 18px; font-weight: bold; cursor: pointer;
    box-shadow: 0 6px 0 #364fc7, 0 8px 12px rgba(0,0,0,0.3);
    transition: transform 0.08s;
  }
  .levelBtn:active { transform: translateY(6px); box-shadow: 0 0 0 #364fc7; }
  .levelBtn small { display: block; font-weight: normal; font-size: 13px; margin-top: 4px; color: #495057; }

  #howto {
    background: rgba(0,0,0,0.35); padding: 16px 24px; border-radius: 14px;
    font-size: 15px; line-height: 1.9; max-width: 560px;
  }
  #howto b { color: #ffd23f; }

  #startBtn {
    margin-top: 24px; background: #ff922b; color: #fff; border: none;
    padding: 16px 50px; font-size: 22px; font-weight: bold; border-radius: 40px;
    cursor: pointer; box-shadow: 0 6px 0 #d9480f; transition: transform 0.08s;
  }
  #startBtn:active { transform: translateY(6px); box-shadow: none; }
  #startBtn:disabled { opacity: 0.5; cursor: not-allowed; }

  #endScreen h1 { font-size: 46px; margin-bottom: 14px; text-shadow: 3px 3px 0 rgba(0,0,0,0.3); }
  #endScreen p { font-size: 22px; margin-bottom: 26px; }
  #endScreen button {
    background: #ffd23f; color: #7a4a00; border: none; padding: 14px 40px;
    font-size: 20px; font-weight: bold; border-radius: 40px; cursor: pointer;
    box-shadow: 0 6px 0 #b8860b;
  }
  #endScreen button:active { transform: translateY(6px); box-shadow: none; }

  .hidden { display: none !important; }

  #feedback {
    position: fixed; top: 40%; left: 50%; transform: translate(-50%,-50%) scale(0);
    z-index: 30; font-size: 90px; font-weight: bold; pointer-events: none;
    transition: transform 0.25s ease-out;
  }
  #feedback.show { transform: translate(-50%,-50%) scale(1); }
  #feedback.correct { color: #40c057; text-shadow: 0 0 20px #fff, 3px 3px 0 rgba(0,0,0,0.3); }
  #feedback.wrong { color: #fa5252; text-shadow: 0 0 20px #fff, 3px 3px 0 rgba(0,0,0,0.3); }

  #coinPopup {
    position: fixed; top: 32%; left: 50%; transform: translate(-50%,-50%) scale(0);
    z-index: 31; font-size: 46px; font-weight: bold; pointer-events: none;
    color: #ffb703; text-shadow: 0 0 16px #fff, 2px 2px 0 rgba(0,0,0,0.3);
    transition: transform 0.2s ease-out, top 0.6s ease-out;
  }
  #coinPopup.show { transform: translate(-50%,-50%) scale(1); top: 20%; }

  #progressWrap {
    position: fixed; top: 16px; left: 16px; z-index: 20;
    background: rgba(20,25,40,0.7); border-radius: 20px; padding: 8px 16px;
    color: #fff; font-weight: bold; border: 3px solid #40c057;
  }

  #mobileHint { display: none; }
  @media (max-width: 768px), (pointer: coarse) {
    #mobileHint { display: block; font-size: 13px; margin-top: 10px; color: #ffe066; }
  }


  .leaderboardWrap ul { list-style: none; text-align: right; font-size: 15px; line-height: 1.9; }
  .leaderboardWrap li { display: flex; justify-content: space-between; gap: 10px; }
  .leaderboardWrap li .rank { color: #ffd23f; font-weight: bold; margin-left: 6px; }
  .leaderboardWrap .region { display: block; font-size: 11px; opacity: 0.8; line-height: 1.2; margin: -2px 0 2px 0; }

/* ================= Touch UI: dual virtual joysticks ================= */
html, body, #gameCanvas { touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
input { -webkit-user-select: text; user-select: text; touch-action: auto; }

#touchUI { position: fixed; inset: 0; z-index: 25; pointer-events: none; }
#touchUI.hidden { display: none !important; }

.joystick {
  position: absolute; bottom: calc(22px + env(safe-area-inset-bottom, 0px));
  width: 132px; height: 132px; border-radius: 50%;
  background: rgba(20,25,40,0.35); border: 3px solid rgba(255,255,255,0.55);
  box-shadow: inset 0 0 18px rgba(0,0,0,0.35), 0 4px 10px rgba(0,0,0,0.3);
  display: flex; align-items: center; justify-content: center;
  pointer-events: auto; touch-action: none;
}
/* the joystick sticks always stay physically left / right, regardless of the RTL page direction */
#moveStick { left: calc(24px + env(safe-area-inset-left, 0px)); }
#lookStick { right: calc(24px + env(safe-area-inset-right, 0px)); }
.joystick::before {
  content: ''; position: absolute; inset: 22%; border-radius: 50%;
  border: 2px dashed rgba(255,255,255,0.35);
}
.joy-knob {
  width: 58px; height: 58px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffe680, #ffd23f 55%, #e0a800);
  border: 3px solid #fff; box-shadow: 0 3px 8px rgba(0,0,0,0.45);
  transition: transform 0.08s ease-out; will-change: transform;
}
.joystick.held .joy-knob { transition: none; }
.joystick.held { background: rgba(20,25,40,0.5); }
.joy-label {
  position: absolute; top: -22px; left: 50%; transform: translateX(-50%);
  font-size: 12px; color: #fff; text-shadow: 0 1px 3px rgba(0,0,0,0.7); white-space: nowrap;
}

#actionButtons {
  position: absolute; right: calc(34px + env(safe-area-inset-right, 0px));
  bottom: calc(176px + env(safe-area-inset-bottom, 0px));
  display: flex; flex-direction: column; gap: 10px; align-items: center; pointer-events: none;
}
.mbtn {
  pointer-events: auto; touch-action: none; color: #fff; font-weight: bold; font-family: inherit;
  border: 3px solid #fff; background: rgba(20,25,40,0.6);
  box-shadow: 0 4px 8px rgba(0,0,0,0.35);
}
.mbtn.held { background: rgba(255,146,43,0.9); transform: scale(0.94); }
#jumpBtn { width: 72px; height: 72px; border-radius: 50%; font-size: 26px; }
#sprintBtn { width: 66px; height: 42px; border-radius: 14px; font-size: 14px; }

@media (max-height: 420px) {
  .joystick { width: 108px; height: 108px; bottom: 12px; }
  .joy-knob { width: 48px; height: 48px; }
  #actionButtons { bottom: 132px; flex-direction: row; }
}

/* ================= All-players window ================= */
.showPlayersBtn, #endScreen .showPlayersBtn {
  margin-top: 12px; background: #ffd23f; color: #7a4a00; border: none; cursor: pointer;
  padding: 10px 22px; font-size: 15px; font-weight: bold; font-family: inherit;
  border-radius: 30px; box-shadow: 0 4px 0 #b8860b;
}
.showPlayersBtn:active, #endScreen .showPlayersBtn:active { transform: translateY(4px); box-shadow: none; }

#playersModal {
  position: fixed; inset: 0; z-index: 200; background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modalBox {
  background: linear-gradient(180deg, #4c6ef5, #364fc7); color: #fff;
  border: 4px solid #ffd23f; border-radius: 18px; width: 100%; max-width: 620px;
  max-height: 85vh; display: flex; flex-direction: column; box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}
.modalHead { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px 8px; }
.modalHead h3 { font-size: 20px; color: #ffd23f; }
#closePlayersBtn {
  background: #fa5252; color: #fff; border: 2px solid #fff; border-radius: 50%;
  width: 36px; height: 36px; font-size: 16px; cursor: pointer; font-family: inherit;
}
.modalBody { overflow-y: auto; touch-action: pan-y; padding: 6px 14px 16px; }
#playersTable { width: 100%; border-collapse: collapse; font-size: 15px; text-align: center; }
#playersTable th { position: sticky; top: 0; background: #2b3f9e; color: #ffd23f; padding: 8px 6px; font-size: 14px; }
#playersTable td { padding: 8px 6px; border-bottom: 1px solid rgba(255,255,255,0.18); }
#playersTable tbody tr:nth-child(1) td:first-child { color: #ffd23f; font-weight: bold; }
#playersTable td:nth-child(2) { text-align: right; font-weight: bold; }
.emptyMsg { text-align: center; padding: 26px 10px; font-size: 16px; }
@media (max-width: 480px) { #playersTable { font-size: 13px; } #playersTable th { font-size: 12px; } }
.modalNote { padding: 8px 16px 12px; font-size: 12px; text-align: center; opacity: 0.85; border-top: 1px solid rgba(255,255,255,0.2); }

#storageStatus { max-width: 520px; margin: -14px auto 18px; padding: 7px 14px; border-radius: 12px; font-size: 13px; font-weight: bold; line-height: 1.6; }
#storageStatus.ok { background: rgba(43,138,62,0.85); color: #fff; }
#storageStatus.warn { background: rgba(250,82,82,0.92); color: #fff; }

#playersBtnStart { margin-top: 22px; }
#startBtn { margin-top: 14px; }

/* ================= Scrollable start / end screens ================= */
/* The screens can be taller than small displays (phones, small laptops): let them scroll.
   margin:auto on the first/last child keeps the content centered when it fits,
   and lets it scroll from the very top when it does not. */
#startScreen, #endScreen {
  overflow-y: auto; overflow-x: hidden;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  touch-action: pan-y;
  justify-content: flex-start;
  padding-bottom: calc(30px + env(safe-area-inset-bottom, 0px));
}
#startScreen > :first-child, #endScreen > :first-child { margin-top: auto; }
#startScreen > :last-child,  #endScreen > :last-child  { margin-bottom: auto; }
#startScreen *, #endScreen * { touch-action: pan-y; }
#startScreen input, #startScreen .levelBtn, #startScreen button,
#endScreen button { touch-action: manipulation; }

@media (max-height: 700px), (max-width: 480px) {
  #startScreen h1 { font-size: 38px; }
  #startScreen h2 { font-size: 16px; margin-bottom: 16px; }
  .blockRow { margin-bottom: 16px; }
  .demoBlock { width: 38px; height: 38px; }
  #levelSelect { gap: 10px; margin-bottom: 16px; }
  .levelBtn { padding: 12px 16px; font-size: 16px; }
  #endScreen h1 { font-size: 34px; }
  #endScreen p { font-size: 18px; }
}