:root{
  --ink:#3A2418;
  --cream:#FFF6E0;
  --sun:#FFD84D;
  --tang:#FF8A3D;
  --cobalt:#2B4CC7;
  --pave:#E8A85C;
  --sand:#F8D9A8;
  --redish:#E5533D;
  --shadow:0 6px 0 rgba(58,36,24,.18);
  --shadow-sm:0 4px 0 rgba(58,36,24,.18);
  --r-lg:26px;
  --r-sm:16px;
}

*{box-sizing:border-box;-webkit-tap-highlight-color:transparent;}
html,body{height:100%;margin:0;padding:0;}
body{
  font-family:'Tajawal',system-ui,-apple-system,'Segoe UI',sans-serif;
  background:var(--cream);
  color:var(--ink);
  overflow:hidden;
  overscroll-behavior:none;
  -webkit-user-select:none;user-select:none;
  -webkit-font-smoothing:antialiased;
}
button{font-family:inherit;color:inherit;cursor:pointer;border:none;background:none;}

#game{
  position:relative;
  height:100%;
  width:100%;
  display:flex;
  flex-direction:column;
  background:var(--cream);
  overflow:hidden;
}

/* ---------------- HUD ---------------- */
.hud{
  flex:0 0 auto;
  display:grid;
  grid-template-columns:auto minmax(36px,1fr) auto auto auto;
  align-items:center;
  gap:8px;
  padding:7px 10px;
  background:var(--cream);
  border-bottom:3px solid var(--ink);
  position:relative;
  z-index:5;
}
.hearts{display:flex;align-items:center;gap:4px;}
.life{
  width:20px;height:18px;display:block;
  transition:opacity .18s ease,transform .18s ease,filter .18s ease;
}
.life.lost{opacity:.2;filter:grayscale(1);transform:scale(.82);}
.gapbar{min-width:0;}
.gap-track{
  position:relative;
  height:15px;
  border:3px solid var(--ink);
  border-radius:999px;
  background:var(--cream);
  overflow:hidden;
}
.gap-fill{
  position:absolute;inset:0 auto 0 0;
  width:34%;
  background:var(--tang);
  border-radius:999px;
  transition:width .12s linear,background-color .25s ease;
}
.gap-fill.danger{background:var(--redish);animation:gapPulse .55s ease-in-out infinite alternate;}
@keyframes gapPulse{from{filter:brightness(1);}to{filter:brightness(1.22);}}
.scorebox{display:flex;align-items:baseline;gap:3px;line-height:1;}
.score-num{font-size:21px;font-weight:900;font-variant-numeric:tabular-nums;}
.score-lbl{font-size:10px;font-weight:700;opacity:.55;}
.icon-btn{
  width:34px;height:34px;flex:0 0 auto;
  display:flex;align-items:center;justify-content:center;
  border:3px solid var(--ink);
  border-radius:12px;
  background:var(--cream);
  color:var(--ink);
  box-shadow:0 3px 0 rgba(58,36,24,.18);
  transition:transform .09s ease,box-shadow .09s ease;
}
.icon-btn:active{transform:translateY(2px);box-shadow:0 1px 0 rgba(58,36,24,.18);}
.icon-btn .ico-sun{display:none;}
.icon-btn.on .ico-moon{display:none;}
.icon-btn.on .ico-sun{display:block;}

/* ---------------- Stage ---------------- */
.stage{
  position:relative;
  flex:1 1 auto;
  min-height:0;
  overflow:hidden;
  background:#FFE9B8;
}
.stage canvas{display:block;width:100%;height:100%;}
.stage.tense::after{
  content:'';
  position:absolute;inset:0;
  pointer-events:none;
  z-index:4;
  box-shadow:inset 0 0 66px 14px rgba(229,83,61,.42);
  animation:tensePulse .9s ease-in-out infinite alternate;
}
@keyframes tensePulse{from{opacity:.5;}to{opacity:1;}}

.toasts{
  position:absolute;left:0;right:0;top:12%;
  display:flex;flex-direction:column;align-items:center;gap:6px;
  pointer-events:none;z-index:6;
}
.toast{
  font-weight:900;font-size:19px;line-height:1;
  color:var(--ink);
  padding:8px 18px;
  border-radius:999px;
  background:rgba(255,246,224,.94);
  border:3px solid var(--ink);
  box-shadow:0 4px 0 rgba(58,36,24,.16);
  white-space:nowrap;
  animation:toastPop 1.4s ease forwards;
}
@keyframes toastPop{
  0%{opacity:0;transform:translateY(12px) scale(.82);}
  14%{opacity:1;transform:translateY(0) scale(1);}
  74%{opacity:1;transform:translateY(0) scale(1);}
  100%{opacity:0;transform:translateY(-16px) scale(.94);}
}

/* ---------------- Controls ---------------- */
.controls{
  flex:0 0 auto;
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
  padding:9px 14px 12px;
  background:var(--cream);
  border-top:3px solid var(--ink);
  position:relative;
  z-index:5;
}
.ctrl-group{display:flex;align-items:center;gap:10px;justify-content:flex-start;}
.ctrl-group-actions{justify-content:flex-end;}

[data-meeem-game-controls] button.ctrl{
  flex:0 0 auto;
  width:64px;height:64px;min-width:64px;max-width:64px;min-height:64px;
  display:flex;align-items:center;justify-content:center;
  border:4px solid var(--ink);
  border-radius:999px;
  box-shadow:var(--shadow);
  color:var(--ink);
  transition:transform .08s ease,box-shadow .08s ease,filter .12s ease;
  touch-action:none;
}
[data-meeem-game-controls] button.ctrl svg{width:30px;height:30px;display:block;}
.ctrl-move{background:var(--cream);}
.ctrl-jump{background:var(--tang);}
.ctrl-slide{background:var(--cobalt);color:var(--cream);}
[data-meeem-game-controls] button.ctrl:active,
[data-meeem-game-controls] button.ctrl.pressed{
  transform:translateY(4px);
  box-shadow:0 2px 0 rgba(58,36,24,.18);
  filter:brightness(1.06);
}
.ctrl:focus-visible,.btn:focus-visible,.icon-btn:focus-visible{
  outline:3px solid var(--cobalt);outline-offset:3px;
}

/* ---------------- Menus ---------------- */
.menu{
  position:absolute;inset:0;z-index:30;
  display:flex;align-items:center;justify-content:center;
  padding:16px;
  background:linear-gradient(180deg,rgba(255,246,224,.90),rgba(255,236,206,.86));
  -webkit-backdrop-filter:blur(7px);
  backdrop-filter:blur(7px);
  overflow-y:auto;
  -webkit-overflow-scrolling:touch;
}
.menu.hidden{display:none;}
.menu-card{
  width:100%;max-width:380px;
  display:flex;flex-direction:column;align-items:center;
  gap:11px;
  text-align:center;
  margin:auto 0;
  padding:6px 0;
}
.menu-card.over{gap:10px;padding-top:8px;}

.eyebrow{
  margin:0;
  font-size:12px;font-weight:800;letter-spacing:.04em;
  color:#8a6a50;
}
.title{
  margin:0;
  font-size:clamp(34px,11vw,52px);
  font-weight:900;
  line-height:1;
  color:var(--ink);
  text-shadow:4px 4px 0 rgba(255,138,61,.55);
}
.sub{
  margin:0;font-size:15px;font-weight:700;line-height:1.55;
  color:#6b4a32;max-width:320px;
}
.over-title{
  margin:0;font-size:clamp(25px,8vw,32px);font-weight:900;line-height:1.2;
  color:var(--ink);
}

.btn{
  width:100%;max-width:330px;
  min-height:58px;
  display:flex;align-items:center;justify-content:center;
  padding:12px 20px;
  border:5px solid var(--ink);
  border-radius:999px;
  font-size:20px;font-weight:900;line-height:1.1;
  box-shadow:var(--shadow);
  transition:transform .09s ease,box-shadow .09s ease,filter .15s ease;
}
.btn:active{transform:translateY(4px);box-shadow:0 2px 0 rgba(58,36,24,.18);}
.btn-primary{background:var(--tang);color:var(--ink);}
.btn-primary:hover{filter:brightness(1.04);}
.btn-ghost{
  background:var(--cream);color:var(--ink);
  min-height:50px;font-size:16px;
  border-width:4px;
  box-shadow:var(--shadow-sm);
}
.btn-ghost:active{transform:translateY(3px);box-shadow:0 1px 0 rgba(58,36,24,.18);}
.best-line{margin:0;font-size:14px;font-weight:700;color:#6b4a32;}
.best-line b{font-weight:900;color:var(--ink);font-size:17px;}
.hint{
  margin:0;font-size:12px;font-weight:600;line-height:1.6;
  color:#8a6a50;max-width:300px;
}

/* score card on game over */
.scorecard{
  width:100%;max-width:330px;
  background:rgba(255,255,255,.72);
  border:5px solid var(--ink);
  border-radius:var(--r-lg);
  padding:13px 16px 11px;
  box-shadow:var(--shadow);
}
.sc-label{font-size:12px;font-weight:700;opacity:.6;}
.sc-value{
  font-size:clamp(38px,12vw,54px);
  font-weight:900;line-height:1;
  color:var(--ink);
  margin:2px 0 6px;
  font-variant-numeric:tabular-nums;
}
.sc-best{font-size:13px;font-weight:700;color:#6b4a32;}
.sc-best b{font-weight:900;color:var(--tang);}

/* leaderboard sheet */
.sheet{
  width:100%;max-width:400px;
  background:var(--cream);
  border:5px solid var(--ink);
  border-radius:var(--r-lg);
  box-shadow:0 -6px 0 rgba(58,36,24,.15);
  padding:14px;
  margin-top:auto;
  margin-bottom:auto;
}
.sheet-head{
  display:flex;align-items:center;justify-content:space-between;gap:10px;
  margin-bottom:10px;
}
.sheet-head h2{margin:0;font-size:20px;font-weight:900;}
.sheet-body{display:flex;flex-direction:column;gap:8px;}
.board-row{
  display:grid;grid-template-columns:34px 1fr auto;
  align-items:center;gap:8px;
  padding:9px 12px;
  border:3px solid var(--ink);
  border-radius:14px;
  background:#fff;
  font-weight:800;
  font-size:15px;
}
.board-row.me{background:#FFE9B8;}
.rank{font-weight:900;color:var(--tang);text-align:center;}
.handle{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;text-align:start;}
.pts{font-weight:900;font-variant-numeric:tabular-nums;}
.board-note{
  margin:0;font-size:12px;line-height:1.6;font-weight:500;color:#8a6a50;
  text-align:start;
}
.state-box{
  border:4px dashed rgba(58,36,24,.35);
  border-radius:var(--r-sm);
  padding:14px;
  text-align:center;
  font-size:14px;font-weight:700;line-height:1.6;
  color:#6b4a32;
  background:#FFFBF0;
}
.state-box b{display:block;font-size:16px;color:var(--ink);margin-bottom:4px;}
.sheet .btn{max-width:100%;}

/* ---------------- short screens ---------------- */
@media (max-height:560px){
  .controls{padding:6px 12px 8px;}
  [data-meeem-game-controls] button.ctrl{width:54px;height:54px;min-width:54px;max-width:54px;min-height:54px;}
  [data-meeem-game-controls] button.ctrl svg{width:24px;height:24px;}
  .title{font-size:clamp(34px,9vw,48px);}
  .btn{min-height:48px;font-size:17px;}
  .btn-ghost{min-height:44px;font-size:15px;}
  .menu-card{gap:8px;}
  .score-num{font-size:18px;}
  .toast{font-size:16px;padding:6px 14px;}
}
@media (max-width:360px){
  .life{width:17px;height:15px;}
  .score-num{font-size:18px;}
  .hud{gap:6px;padding:6px 8px;}
  [data-meeem-game-controls] button.ctrl{width:58px;height:58px;min-width:58px;max-width:58px;}
  .ctrl-group{gap:8px;}
}
/* ---------------- night toggle ---------------- */
.menu-row{display:flex;align-items:center;justify-content:center;gap:10px;flex-wrap:wrap;}
.pill-btn{
  min-height:38px;
  padding:6px 15px;
  display:inline-flex;align-items:center;justify-content:center;gap:6px;
  border:3px solid var(--ink);
  border-radius:999px;
  background:var(--cream);
  color:var(--ink);
  font-size:13px;font-weight:800;line-height:1;
  box-shadow:0 3px 0 rgba(58,36,24,.18);
  transition:transform .09s ease,box-shadow .09s ease;
}
.pill-btn:active{transform:translateY(2px);box-shadow:0 1px 0 rgba(58,36,24,.18);}
.pill-btn:focus-visible{outline:3px solid var(--cobalt);outline-offset:3px;}

/* ---------------- night mode ---------------- */
body.night{
  --cream:#161E3A;
  --ink:#F2E8D6;
  --sand:#26304F;
  --pave:#7C6AA8;
  --cobalt:#8FA6FF;
  --redish:#FF6B54;
  background:#101733;
  color:var(--ink);
}
body.night .hud,
body.night .controls{background:#141B33;border-color:#39456F;}
body.night .stage{background:#182146;}
body.night .gap-track{background:#101733;border-color:#5C6A9C;}
body.night .icon-btn{background:#1C2547;border-color:#5C6A9C;color:#F2E8D6;}
body.night .ctrl-move{background:#1C2547;border-color:#5C6A9C;color:#F2E8D6;}
body.night .ctrl-jump{background:var(--tang);color:#2A1408;border-color:#7A4420;}
body.night .ctrl-slide{background:#41539B;color:#0F1530;border-color:#8FA6FF;}
body.night .menu{background:linear-gradient(180deg,rgba(12,18,42,.95),rgba(20,27,51,.93));}
body.night .title{color:#FFF3DC;text-shadow:4px 4px 0 rgba(43,76,199,.6);}
body.night .sub,
body.night .hint,
body.night .best-line,
body.night .eyebrow,
body.night .board-note{color:#AEB6D6;}
body.night .best-line b{color:#FFE08A;}
body.night .btn-primary{color:#2A1408;border-color:#0B1026;}
body.night .btn-ghost{background:#1C2547;color:#F2E8D6;border-color:#5C6A9C;box-shadow:0 4px 0 rgba(0,0,0,.42);}
body.night .scorecard{background:rgba(255,255,255,.07);border-color:#5C6A9C;}
body.night .sc-best{color:#AEB6D6;}
body.night .sc-best b{color:#FFD84D;}
body.night .sheet{background:#141B33;border-color:#5C6A9C;box-shadow:0 -6px 0 rgba(0,0,0,.4);}
body.night .board-row{background:#1C2547;border-color:#39456F;}
body.night .board-row.me{background:#2A3563;border-color:#8FA6FF;}
body.night .state-box{background:#141B33;border-color:rgba(146,160,208,.5);color:#C9D0EA;}
body.night .state-box b{color:#FFF3DC;}
body.night .toast{background:rgba(16,23,51,.94);color:#FFF3DC;border-color:#5C6A9C;}
body.night .pill-btn{background:#1C2547;color:#F2E8D6;border-color:#5C6A9C;box-shadow:0 3px 0 rgba(0,0,0,.45);}

/* ---------------- results list additions ---------------- */
.board-sub{
  margin:2px 0 0;
  font-size:13px;font-weight:900;line-height:1.3;
  color:var(--ink);
  display:flex;align-items:center;gap:6px;
  text-align:start;
}
.board-sub::before{
  content:'';display:block;flex:0 0 auto;
  width:5px;height:16px;border-radius:3px;background:var(--tang);
}
.board-section{display:flex;flex-direction:column;gap:7px;}
.board-list{display:flex;flex-direction:column;gap:7px;}
.board-row.local{background:#FFFBF0;}
.board-row.local .handle{font-weight:700;font-size:13px;opacity:.85;}
.board-refresh{align-self:center;margin-top:4px;}

.sc-record{
  display:none;
  margin:0 0 6px;
  font-size:13px;font-weight:900;
  color:#B4600E;
}
.sc-record.on{display:block;animation:recordPop .5s ease;}
@keyframes recordPop{
  0%{transform:scale(.8);opacity:0;}
  60%{transform:scale(1.08);opacity:1;}
  100%{transform:scale(1);opacity:1;}
}

body.night .board-row.local{background:#1C2547;}
body.night .sc-record{color:#FFD84D;}

@media (prefers-reduced-motion:reduce){
  *{animation-duration:.001ms !important;animation-iteration-count:1 !important;transition-duration:.001ms !important;}
}