* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; overflow: hidden; overscroll-behavior: none; }
body {
  font-family: ui-rounded, "Nunito", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: #eef0ff; color: #2b2350;
  display: flex; flex-direction: column;
  height: 100vh; height: 100dvh;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
button { font: inherit; cursor: pointer; touch-action: manipulation; }

/* ---------- header ---------- */
header {
  flex: none; display: flex; align-items: center; gap: 12px;
  padding: 8px 12px; background: linear-gradient(90deg, #5b3cc4, #7c5cff); color: #fff;
}
.logo { font-weight: 900; font-size: 20px; white-space: nowrap; }
#tabs { flex: 1; display: flex; gap: 8px; overflow-x: auto; padding: 2px; scrollbar-width: none; }
#tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: none; min-width: 48px; height: 48px; border-radius: 14px; border: 3px solid transparent;
  background: rgba(255, 255, 255, .18); color: #fff; font-weight: 900; font-size: 20px; position: relative;
}
.tab.active { background: #fff; color: #5b3cc4; }
.tab.done::after { content: "✅"; position: absolute; right: -6px; top: -8px; font-size: 16px; }
.icon-btn { flex: none; width: 48px; height: 48px; border-radius: 14px; border: 0; background: rgba(255, 255, 255, .18); font-size: 22px; }

/* ---------- layout ---------- */
#app { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(300px, 46%) minmax(0, 1fr); gap: 10px; padding: 10px; }
#left { display: flex; flex-direction: column; justify-content: center; gap: 8px; min-height: 0; min-width: 0; }
#right { min-height: 0; min-width: 0; display: flex; flex-direction: column; gap: 8px; }
#blocklyDiv { flex: 1 1 0; min-height: 0; width: 100%; border-radius: 18px; overflow: hidden; background: #fff; box-shadow: 0 4px 14px rgba(60, 40, 140, .15); }
/* ---------- stage ---------- */
#stageSlot { flex: none; overflow: hidden; display: flex; align-items: center; justify-content: center; }
#stageFit { position: relative; width: 480px; height: 360px; border-radius: 18px; overflow: hidden; box-shadow: 0 6px 18px rgba(60, 40, 140, .25); border: 4px solid #fff; background: #fff; }
#stage { position: absolute; left: 0; top: 0; width: 480px; height: 360px; transform-origin: 0 0; overflow: hidden; }
.bg-grass  { background: radial-gradient(circle at 86% 16%, #fff3a6 0 26px, transparent 27px), linear-gradient(#8fd3ff 0 62%, #7ddc6f 62% 100%); }
.bg-sea    { background: radial-gradient(circle at 15% 15%, #fff 0 18px, transparent 19px), linear-gradient(#aee6ff 0 32%, #2d9cdb 32% 100%); }
.bg-space  { background: radial-gradient(circle at 20% 30%, #fff 0 2px, transparent 3px), radial-gradient(circle at 70% 18%, #fff 0 2px, transparent 3px), radial-gradient(circle at 45% 70%, #fff 0 2px, transparent 3px), radial-gradient(circle at 88% 62%, #fff 0 2px, transparent 3px), radial-gradient(circle at 10% 80%, #fff 0 2px, transparent 3px), radial-gradient(circle at 78% 85%, #ffd479 0 14px, transparent 15px), #0b1033; }
.bg-sunset { background: linear-gradient(#ff7e5f 0, #feb47b 60%, #6a4c93 60%); }
.bg-desert { background: radial-gradient(circle at 80% 18%, #fff 0 22px, transparent 23px), linear-gradient(#ffe29a 0 55%, #e9b872 55%); }
.bg-plain  { background: linear-gradient(#dfe3ff 1px, transparent 1px) 0 0 / 40px 40px, linear-gradient(90deg, #dfe3ff 1px, transparent 1px) 0 0 / 40px 40px, #fff; }

#goal { position: absolute; left: 0; top: 0; font-size: 56px; line-height: 1; transform: translate(-100px, -100px); }
#goal > span { position: absolute; left: 0; top: 0; transform: translate(-50%, -50%); animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { scale: 1; } 50% { scale: 1.12; } }
#spriteWrap { position: absolute; left: 0; top: 0; width: 0; height: 0; transition: transform .06s linear; }
#sprite { position: absolute; left: 0; top: 0; line-height: 1; font-size: 72px; cursor: pointer; touch-action: none; }
#sprite.tap { animation: boing .25s ease-out; }
@keyframes boing { 50% { scale: 1.25; } }
#bubble {
  position: absolute; left: 0; transform: translateX(-50%); bottom: 50px; width: max-content; max-width: 220px;
  background: #fff; border: 3px solid #2b2350; border-radius: 18px; padding: 6px 12px;
  font-size: 22px; font-weight: 800; text-align: center; color: #2b2350; z-index: 3;
}
#bubble::after { content: ""; position: absolute; left: 50%; bottom: -12px; width: 14px; height: 14px; background: #fff; border-right: 3px solid #2b2350; border-bottom: 3px solid #2b2350; transform: translateX(-50%) rotate(45deg); }

#scoreBadge { position: absolute; left: 10px; top: 10px; background: rgba(255, 255, 255, .92); border-radius: 999px; padding: 4px 14px; font-weight: 900; font-size: 22px; z-index: 5; }
#padRow { flex: none; display: flex; align-items: center; gap: 10px; padding: 0 4px; }
#pad { margin-left: auto; width: 170px; height: 116px; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 1fr); gap: 6px; grid-template-areas: ". up ." "left down right"; }
#pad.lr { width: 130px; height: 60px; grid-template-columns: repeat(2, 1fr); grid-template-rows: 1fr; grid-template-areas: "left right"; }
#pad.lr [data-dir=up], #pad.lr [data-dir=down] { display: none; }
#pad button { border: 0; border-radius: 16px; color: #fff; display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1; touch-action: none; }
#pad button span { font-size: 30px; font-weight: 900; }
#pad button small { font-size: 12px; font-weight: 800; letter-spacing: .5px; text-transform: uppercase; margin-top: 2px; }
#pad button:active { transform: translateY(3px); }
/* each direction has ONE colour, shared with its blocks in the code */
#pad [data-dir=left]  { background: #1E88E5; box-shadow: 0 4px 0 #1565C0; }
#pad [data-dir=right] { background: #F4511E; box-shadow: 0 4px 0 #BF360C; }
#pad [data-dir=up]    { background: #43A047; box-shadow: 0 4px 0 #2E7D32; }
#pad [data-dir=down]  { background: #8E24AA; box-shadow: 0 4px 0 #6A1B9A; }
#pad button:active { box-shadow: 0 1px 0 rgba(0, 0, 0, .3) !important; }
#pad [data-dir=up] { grid-area: up; } #pad [data-dir=left] { grid-area: left; }
#pad [data-dir=down] { grid-area: down; } #pad [data-dir=right] { grid-area: right; }

#win { position: absolute; inset: 0; z-index: 8; display: flex; align-items: center; justify-content: center; background: rgba(43, 35, 80, .45); }
.win-card { background: #fff; border-radius: 24px; padding: 16px 20px; text-align: center; width: min(90%, 360px); box-shadow: 0 10px 30px rgba(0, 0, 0, .3); }
.win-card h2 { margin: 2px 0; font-size: 26px; }
.win-card p { margin: 4px 0 10px; font-size: 18px; font-weight: 700; color: #6b6396; }
.win-emoji { font-size: 54px; animation: boing .6s ease-out; }
.win-btns { display: flex; flex-direction: column; gap: 8px; }
.confetti { position: absolute; top: -40px; font-size: 28px; z-index: 9; pointer-events: none; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(460px) rotate(540deg); } }

/* ---------- controls + mission card ---------- */
#controls { flex: none; display: flex; gap: 8px; }
.big { flex: 1; min-height: 56px; border: 0; border-radius: 16px; font-weight: 900; font-size: 22px; color: #fff; box-shadow: 0 5px 0 rgba(0, 0, 0, .25); }
.big:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(0, 0, 0, .25); }
.green { background: #2fbf5a; } .red { background: #ef476f; } .plain { background: #fff; color: #5b3cc4; box-shadow: 0 5px 0 #c9c3f0; }
body.running #goBtn { animation: glow 1s ease-in-out infinite; }
@keyframes glow { 50% { box-shadow: 0 5px 0 rgba(0, 0, 0, .25), 0 0 0 6px rgba(47, 191, 90, .4); } }

#missionCard { flex: none; max-height: 42%; overflow: auto; background: #fff; border-radius: 18px; padding: 10px 14px; box-shadow: 0 4px 14px rgba(60, 40, 140, .12); }
.mc-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
#mcIcon { font-size: 34px; }
#missionCard h2 { margin: 0; font-size: 22px; }
#missionCard p { margin: 6px 0; font-size: 19px; font-weight: 700; line-height: 1.3; }
#mcHint { background: #fff6d6; border-radius: 12px; padding: 8px 12px; font-size: 17px !important; }
.mc-btns { display: flex; gap: 6px; flex-wrap: wrap; margin-left: auto; }
.small { min-height: 44px; padding: 6px 14px; border-radius: 12px; border: 2px solid #d9d3ff; background: #f6f4ff; color: #5b3cc4; font-weight: 800; font-size: 16px; }

/* ---------- Blockly tweaks ---------- */
.blocklyToolboxDiv { font-family: inherit; }
.blocklyToolboxCategoryLabel { font-weight: 800; font-size: 17px; }

/* ---------- courses + space invaders ---------- */
#courses { flex: none; display: flex; gap: 4px; background: rgba(255, 255, 255, .18); border-radius: 14px; padding: 3px; }
.course { border: 0; background: transparent; color: #fff; font-weight: 800; font-size: 15px; padding: 8px 12px; border-radius: 11px; min-height: 42px; white-space: nowrap; }
.course.active { background: #fff; color: #5b3cc4; }
#aliens, #lasers { position: absolute; left: 0; top: 0; width: 480px; height: 360px; pointer-events: none; }
.alien { position: absolute; left: 0; top: 0; font-size: 40px; line-height: 1; }
.laser { position: absolute; left: 0; top: 0; width: 6px; height: 24px; border-radius: 3px; background: linear-gradient(#fff, #ffd479); box-shadow: 0 0 8px 2px rgba(255, 212, 121, .9); }
.boom { position: absolute; left: 0; top: 0; font-size: 44px; line-height: 1; animation: boom .45s ease-out forwards; }
@keyframes boom { from { scale: .4; opacity: 1; } to { scale: 1.8; opacity: 0; } }
#banner {
  position: absolute; left: 0; right: 0; top: 50%; transform: translateY(-50%); z-index: 7; text-align: center;
  font-weight: 900; font-size: calc(var(--s, 1) * 52px); color: #fff; text-shadow: 0 4px 0 rgba(0, 0, 0, .35);
  padding: 8px; background: rgba(239, 71, 111, .88);
}
#banner.win { background: rgba(47, 191, 90, .9); }
#fireBtn { width: 76px; height: 76px; border-radius: 50%; border: 0; background: #ef476f; font-size: 34px; box-shadow: 0 5px 0 #a8284a; touch-action: none; }
#fireBtn:active { transform: translateY(3px); box-shadow: 0 2px 0 #a8284a; }
#mcNew { background: #e8fbe9; border-radius: 12px; padding: 6px 12px; font-size: 16px !important; color: #14632a; }

/* ---------- portrait tablets / narrow screens (kept last so it wins) ---------- */
@media (orientation: portrait), (max-width: 760px) {
  #app { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 54%) minmax(0, 46%); gap: 6px; padding: 6px; }
  .logo { display: none; }
  #left { gap: 6px; }
  .big { min-height: 48px; font-size: 19px; }
  #missionCard { max-height: 44%; padding: 6px 12px; }
  #missionCard p { font-size: 16px; margin: 2px 0; }
  #mcHint { font-size: 15px !important; }
  #mcIcon { font-size: 26px; } #missionCard h2 { font-size: 18px; }
  #mcNew { font-size: 14px !important; padding: 3px 10px; }
  .small { min-height: 38px; font-size: 14px; padding: 4px 10px; }
  .course { font-size: 13px; padding: 6px 8px; min-height: 38px; }
  #fireBtn { width: 62px; height: 62px; font-size: 28px; }
  #pad { height: 96px; } #pad.lr { height: 54px; }
}
