:root {
  --ink: #2a2350;
  --purple: #8b6cf0;
  --pink: #ff7eb6;
  --teal: #3fd7c4;
  --gold: #ffcc33;
  --sky: #7fd3ff;
  --card: #fffaf2;
  --font: ui-rounded, 'Arial Rounded MT Bold', 'Trebuchet MS', 'Segoe UI', sans-serif;
}
* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: #1a1048; color: var(--ink); font-family: var(--font);
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent;
  touch-action: manipulation; overscroll-behavior: none;
}
canvas#game { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); display: block; }
button { font-family: var(--font); cursor: pointer; color: var(--ink); }
button:disabled { cursor: default; }
kbd { display: inline-block; min-width: 1.8em; padding: 1px 6px; border: 2px solid var(--ink); border-bottom-width: 4px; border-radius: 7px; background: #fff; font-family: var(--font); font-weight: 700; font-size: 0.9em; text-align: center; }
[hidden] { display: none !important; }

/* ---------- buttons ---------- */
.primary, .secondary, .buy, .launch {
  border: 3px solid var(--ink); border-radius: 18px; font-weight: 800; font-size: 20px;
  padding: 12px 22px; box-shadow: 0 5px 0 var(--ink); transition: transform 0.08s, box-shadow 0.08s;
}
.primary { background: var(--gold); }
.secondary { background: #fff; }
.primary:active, .secondary:active, .buy:active, .launch:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--ink); }
.primary:disabled, .buy:disabled, .launch:disabled { background: #ddd; color: #888; box-shadow: 0 3px 0 #999; border-color: #999; }
.primary:focus-visible, .secondary:focus-visible, .buy:focus-visible { outline: 4px solid var(--teal); outline-offset: 3px; }
.big-btn { font-size: 22px; padding: 14px 26px; margin-top: 8px; }

/* ---------- menu ---------- */
.menu { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 20; }
.menu-card { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 12px; max-width: 520px; }
.menu h1 { margin: 0; display: flex; flex-direction: column; line-height: 0.95; color: #fff; text-shadow: 0 5px 0 var(--purple), 0 9px 0 var(--ink); }
.menu h1 span:first-child { font-size: clamp(40px, 9vw, 76px); letter-spacing: 3px; }
.menu h1 span:last-child { font-size: clamp(30px, 6.5vw, 54px); color: var(--gold); }
.byline { margin: 6px 0 10px; color: #ffd1e6; font-size: 22px; font-weight: 800; }
.menu-btn { width: min(320px, 80vw); }
.tagline { color: #e6e0ff; font-size: 16px; margin-top: 8px; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 5; }
#hud button, #hud .pill { pointer-events: auto; }
.hud-left { position: absolute; left: max(10px, env(safe-area-inset-left)); top: 10px; display: flex; flex-direction: column; gap: 6px; }
.pill { display: flex; align-items: center; gap: 6px; background: var(--card); border: 3px solid var(--ink); border-radius: 999px; padding: 4px 14px 4px 6px; font-weight: 800; font-size: 20px; box-shadow: 0 3px 0 var(--ink); width: max-content; transition: transform 0.2s; }
.pill .ico { width: 26px; height: 26px; }
.pill.bump { transform: scale(1.25); }
#bag.full { background: #ffe0e0; animation: wiggle 0.6s infinite; }
.rocket-pill { font-size: 15px; }
#mission {
  position: absolute; left: 50%; top: 10px; transform: translateX(-50%); width: min(470px, 46vw);
  background: var(--card); border: 3px solid var(--ink); border-radius: 18px; padding: 6px 14px 10px; box-shadow: 0 4px 0 var(--ink); text-align: center;
}
#mission-label { font-size: 13px; font-weight: 800; color: var(--purple); text-transform: uppercase; letter-spacing: 1px; }
#mission-text { font-size: 17px; font-weight: 700; line-height: 1.2; margin: 2px 0 6px; }
.bar { height: 12px; border-radius: 99px; background: #e6e0ff; border: 2px solid var(--ink); overflow: hidden; }
#mission-bar { height: 100%; width: 0; background: linear-gradient(90deg, var(--teal), var(--gold)); transition: width 0.4s; }
.hud-right { position: absolute; right: max(10px, env(safe-area-inset-right)); top: 10px; display: flex; gap: 8px; }
.round { width: 48px; height: 48px; border-radius: 50%; border: 3px solid var(--ink); background: var(--card); font-size: 22px; font-weight: 900; box-shadow: 0 3px 0 var(--ink); padding: 0; }
#btn-hint { background: var(--gold); }
#checklist { position: absolute; left: max(10px, env(safe-area-inset-left)); top: 150px; background: var(--card); border: 3px solid var(--ink); border-radius: 16px; padding: 8px 10px; box-shadow: 0 4px 0 var(--ink); width: 230px; pointer-events: auto; }
.cl-title { font-weight: 800; font-size: 14px; color: var(--purple); margin-bottom: 4px; }
#checklist .row { display: flex; align-items: center; gap: 6px; font-size: 15px; font-weight: 700; padding: 2px 0; }
#checklist .row span { flex: 1; }
#checklist .row.done { color: #1f9c6b; }
#checklist .row.done b::after { content: ' ✓'; }
.hint-btn { margin-top: 6px; width: 100%; border: 2px solid var(--ink); border-radius: 12px; background: var(--gold); font-weight: 800; font-size: 15px; padding: 7px; }
#keys-guide { position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%); background: rgba(255,250,242,0.85); border-radius: 12px; padding: 5px 12px; font-size: 14px; font-weight: 700; }
body.touch #keys-guide { display: none; }
.icon { vertical-align: middle; }

/* ---------- touch controls ---------- */
#touch { position: fixed; inset: auto 0 0 0; height: 0; z-index: 6; display: none; }
body.touch #touch { display: block; }
#touch.hidden { display: none !important; }
.t-group { position: fixed; bottom: max(14px, env(safe-area-inset-bottom)); display: flex; gap: 14px; }
.t-group.left { left: max(14px, env(safe-area-inset-left)); }
.t-group.right { right: max(14px, env(safe-area-inset-right)); }
.t-group button {
  width: 84px; height: 84px; border-radius: 50%; border: 4px solid var(--ink); background: rgba(255,250,242,0.88);
  font-size: 36px; font-weight: 900; box-shadow: 0 5px 0 var(--ink); touch-action: none; padding: 0;
}
.t-group button.pressed { transform: translateY(4px); box-shadow: 0 1px 0 var(--ink); background: #fff; }
#t-jump { background: rgba(127,211,255,0.92); }
#t-action { background: rgba(255,204,51,0.9); }
#t-action.ready { animation: pulse 0.9s infinite; background: var(--gold); }

/* ---------- toasts, achievements, captions ---------- */
#toasts { position: fixed; top: 120px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 6px; z-index: 30; pointer-events: none; width: min(560px, 92vw); }
.toast { background: #fff; border: 3px solid var(--ink); border-radius: 16px; padding: 8px 16px; font-weight: 800; font-size: 18px; box-shadow: 0 4px 0 var(--ink); animation: pop 0.3s; text-align: center; transition: opacity 0.4s, transform 0.4s; }
.toast.good { background: #e6fff4; }
.toast.rare { background: linear-gradient(90deg, #fff6c4, #ffe0f0, #d9fff9); font-size: 20px; }
.toast.info { background: #fff3d6; }
.toast.hint { background: #efe9ff; }
.toast.out { opacity: 0; transform: translateY(-10px); }
#achievements-pop { position: fixed; right: 12px; top: 72px; z-index: 31; pointer-events: none; }
.ach-pop { display: flex; gap: 10px; align-items: center; background: linear-gradient(135deg, #fff6c4, #ffe0f0); border: 3px solid var(--ink); border-radius: 18px; padding: 10px 14px; box-shadow: 0 5px 0 var(--ink); animation: slidein 0.4s; max-width: 300px; transition: opacity 0.4s; }
.ach-pop.out { opacity: 0; }
.ach-name { font-size: 18px; font-weight: 900; }
.ach-desc { font-size: 13px; }
.coin-txt { color: #b07a00; font-weight: 900; }
.medal { flex: none; width: 46px; height: 46px; border-radius: 50%; background: var(--gold); border: 3px solid var(--ink); display: grid; place-items: center; font-size: 26px; color: #fff; text-shadow: 0 2px 0 var(--ink); }
#caption { position: fixed; bottom: 40px; left: 50%; transform: translateX(-50%); background: rgba(255,250,242,0.92); border: 3px solid var(--ink); border-radius: 16px; padding: 8px 18px; font-size: 22px; font-weight: 800; z-index: 15; text-align: center; }
#skip { position: fixed; right: 16px; bottom: 16px; z-index: 16; border: 3px solid var(--ink); border-radius: 14px; background: #fff; font-weight: 800; font-size: 18px; padding: 10px 16px; }
.fly-coin { position: fixed; width: 26px; height: 26px; border-radius: 50%; background: radial-gradient(circle at 35% 35%, #fff3a0, var(--gold) 60%, #e0a400); border: 2px solid var(--ink); z-index: 40; transition: transform 0.8s cubic-bezier(.5,-0.3,.6,1), opacity 0.8s; pointer-events: none; }

/* ---------- dialogs ---------- */
#modals .modal-wrap, #error { position: fixed; inset: 0; background: rgba(20,10,60,0.55); display: flex; align-items: center; justify-content: center; padding: 12px; z-index: 25; }
#error { z-index: 50; }
.dialog {
  position: relative; background: var(--card); border: 4px solid var(--ink); border-radius: 26px; box-shadow: 0 8px 0 var(--ink);
  padding: 20px 24px; width: min(600px, 96vw); max-height: 94vh; overflow-y: auto; text-align: center; animation: pop 0.25s;
  -webkit-user-select: none; user-select: none;
}
.dialog h2 { margin: 4px 0 10px; font-size: 28px; }
.dialog h3 { margin: 14px 0 6px; font-size: 19px; color: var(--purple); text-align: left; }
.dialog p { font-size: 17px; margin: 8px 0; }
.dialog p.big { font-size: 20px; font-weight: 700; }
.dialog .small { font-size: 14px; color: #5a5280; }
.dialog .x { position: absolute; right: 10px; top: 10px; width: 42px; height: 42px; border-radius: 50%; border: 3px solid var(--ink); background: #fff; font-size: 18px; font-weight: 900; }
.row-btns { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; margin-top: 12px; }
.col-btns { display: flex; flex-direction: column; gap: 10px; align-items: stretch; max-width: 360px; margin: 0 auto; }
.dialog > .primary, .dialog > .secondary, .dialog > div > .secondary[data-close] { margin-top: 12px; }

table.sell { width: 100%; border-collapse: collapse; font-size: 17px; }
table.sell th { font-size: 13px; color: #6b6390; text-align: left; }
table.sell td { padding: 3px 4px; text-align: left; border-top: 2px dashed #e6e0ff; }
.earned { background: #e6fff4; border: 3px solid var(--ink); border-radius: 14px; padding: 8px; font-size: 22px; font-weight: 800; animation: pop 0.3s; }
.levels span { font-size: 28px; color: #ddd; -webkit-text-stroke: 1.5px var(--ink); }
.levels span.on { color: var(--gold); }
.bonus { font-size: 13px; background: #e6fff4; border-radius: 99px; padding: 2px 8px; color: #1f9c6b; }
.empty, .locked, .maxed { background: #f3f0ff; border-radius: 14px; padding: 10px; }
.upgrade { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; text-align: left; border: 3px solid var(--ink); border-radius: 16px; padding: 10px 12px; margin: 8px 0; background: #fff; }
.upgrade b { display: block; font-size: 18px; }
.upgrade b em { font-size: 12px; font-style: normal; background: var(--pink); color: #fff; border-radius: 99px; padding: 1px 7px; vertical-align: middle; }
.upgrade span { font-size: 14px; }
.upgrade small { grid-column: 1 / -1; color: #8a6d00; font-weight: 700; }
.upgrade.owned { background: #e6fff4; }
.upgrade.required:not(.owned) { border-color: var(--pink); box-shadow: 0 0 0 3px #ffd1e6; }
.owned-tag { font-weight: 800; color: #1f9c6b; }
.buy { background: var(--teal); font-size: 16px; padding: 8px 14px; border-radius: 14px; }
.launch { display: block; width: 100%; margin-top: 14px; font-size: 28px; padding: 16px; background: linear-gradient(180deg, #ff7eb6, #ff5f7e); color: #fff; text-shadow: 0 2px 0 var(--ink); }
.launch:not(:disabled) { animation: pulse 1.1s infinite; }
.pad #pad-rocket { float: right; width: 90px; height: 150px; margin: -6px 0 0 8px; }
.reqs p { text-align: left; margin: 3px 0; font-size: 16px; }
.reqs p.ok { color: #1f7a55; }

.crash { background: linear-gradient(180deg, #fff3d6, #ffe0f0); }
.crash-title, .mc-title { font-size: clamp(38px, 8vw, 60px); font-weight: 900; color: #fff; text-shadow: 0 4px 0 var(--ink), 0 0 0 var(--ink); -webkit-text-stroke: 2px var(--ink); line-height: 1; }
.crash-title { color: var(--pink); animation: wiggle 0.8s 2; }
.mc-title { color: var(--gold); animation: pop 0.6s; }
.mission { background: linear-gradient(180deg, #fffaf2, #e6f7ff); }
.medal-big { display: flex; align-items: center; gap: 14px; text-align: left; background: linear-gradient(135deg, #fff6c4, #ffe0f0); border: 3px solid var(--ink); border-radius: 20px; padding: 14px; margin: 12px auto; max-width: 440px; animation: pop 0.6s 0.3s both; }
.medal-big .medal { width: 70px; height: 70px; font-size: 40px; }
.medal-big b { font-size: 22px; display: block; }

.intro-art canvas { width: 180px; height: 150px; }
.keys-line .tch { display: none; }
body.touch .keys-line .tch { display: inline; }
body.touch .keys-line .desk { display: none; }
.howto { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }
.keys-card { flex: 1 1 200px; background: #fff; border: 3px solid var(--ink); border-radius: 16px; padding: 6px 12px; text-align: left; }
.keys-card h3 { margin: 4px 0; }
.keys-card p { margin: 6px 0; }
.tbtn { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 50%; border: 2px solid var(--ink); background: #fff; font-weight: 900; }
.story-steps { text-align: left; font-size: 17px; font-weight: 700; padding-left: 26px; }
.story-steps li { margin: 6px 0; }
.ach-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.ach-card { border: 3px solid #bbb; border-radius: 16px; padding: 10px; display: flex; flex-direction: column; align-items: center; gap: 4px; background: #f3f3f3; color: #777; font-size: 13px; }
.ach-card b { font-size: 15px; }
.ach-card .medal { background: #ccc; }
.ach-card.got { border-color: var(--ink); background: #fff8dc; color: var(--ink); }
.ach-card.got .medal { background: var(--gold); }
.explorer { font-size: 20px !important; font-weight: 900; color: var(--purple); }

/* ---------- build screen ---------- */
.dialog.build { width: min(760px, 97vw); }
.build-layout { display: flex; gap: 16px; align-items: center; justify-content: center; }
.build-layout.single { margin: 6px 0; }
.build-svg { width: min(230px, 34vw); height: auto; background: radial-gradient(circle at 50% 40%, #3b2a7a, #120a3a); border-radius: 20px; border: 3px solid var(--ink); flex: none; }
.build-svg .piece { animation: pop 0.4s; transform-origin: 100px 150px; }
.build-svg .slot circle { fill: rgba(255,255,255,0.15); stroke: #fff; stroke-width: 3; stroke-dasharray: 6 5; }
.build-svg .slot text { fill: #fff; font-size: 18px; font-weight: 900; text-anchor: middle; font-family: var(--font); }
.build-svg .slot { cursor: pointer; }
.build-svg.picking .slot circle { animation: glowpulse 0.9s infinite; }
.build-svg .slot.target circle { stroke: var(--gold); fill: rgba(255,204,51,0.35); }
.build-svg .slot.wiggle { animation: wiggle 0.5s 2; transform-origin: center; transform-box: fill-box; }
.build-svg .sparkles text { fill: var(--gold); font-size: 22px; animation: twinkle 1s infinite alternate; }
.tray { display: flex; flex-direction: column; gap: 8px; flex: 1; max-width: 330px; }
.part { display: flex; align-items: center; gap: 8px; text-align: left; border: 3px solid var(--ink); border-radius: 16px; background: #fff; padding: 8px 10px; box-shadow: 0 4px 0 var(--ink); }
.part b { display: block; font-size: 17px; }
.part small { font-size: 13px; }
.part.sel { background: var(--gold); transform: scale(1.04); }
.part.placed { background: #e6fff4; box-shadow: none; opacity: 0.85; }
.part:disabled:not(.placed) { opacity: 0.5; }
.check { border: 3px solid var(--ink); border-radius: 14px; background: #fff; font-size: 18px; font-weight: 800; padding: 10px; text-align: left; }
.check.on { background: #e6fff4; }
.build-progress { position: relative; height: 24px; border: 3px solid var(--ink); border-radius: 99px; background: #e6e0ff; overflow: hidden; margin: 0 auto 6px; max-width: 420px; }
.build-progress div { height: 100%; background: linear-gradient(90deg, var(--teal), var(--gold)); transition: width 0.4s; }
.build-progress span { position: absolute; inset: 0; font-weight: 800; font-size: 14px; line-height: 18px; }
.build-msg { font-weight: 800; font-size: 18px !important; min-height: 1.4em; color: var(--purple); }

/* ---------- rotate hint ---------- */
#rotate { display: none; position: fixed; left: 10px; right: 10px; bottom: 120px; z-index: 60; background: #fff; border: 3px solid var(--ink); border-radius: 16px; padding: 10px 14px; font-weight: 800; font-size: 17px; text-align: center; }
#rotate button { margin-left: 8px; border: 2px solid var(--ink); border-radius: 10px; background: var(--gold); font-weight: 800; padding: 4px 12px; }
body.touch.portrait #rotate:not(.dismissed) { display: block; }

/* ---------- small screens ---------- */
@media (max-height: 520px), (max-width: 700px) {
  .pill { font-size: 16px; padding: 2px 10px 2px 4px; }
  .pill .ico { width: 20px; height: 20px; }
  .rocket-pill { display: none; }
  #mission { width: min(380px, 50vw); padding: 4px 10px 7px; }
  #mission-text { font-size: 14px; }
  #mission-label { font-size: 11px; }
  .round { width: 40px; height: 40px; font-size: 18px; }
  .hud-right { gap: 5px; }
  #checklist { top: 92px; width: 190px; padding: 6px 8px; }
  #checklist .row { font-size: 13px; }
  .t-group button { width: 72px; height: 72px; font-size: 30px; }
  #toasts { top: 84px; }
  .toast { font-size: 15px; }
  .dialog { padding: 14px 16px; }
  .dialog h2 { font-size: 23px; }
  .menu-card { gap: 8px; }
  .primary, .secondary { font-size: 17px; padding: 9px 16px; }
}
@media (max-width: 560px) {
  #mission { top: 62px; width: 92vw; left: 50%; }
  .hud-left { flex-direction: row; }
  #checklist { top: 150px; }
  #toasts { top: 140px; }
  .build-layout { flex-direction: column; }
  .build-svg { width: 160px; }
  .hud-right .round { width: 38px; height: 38px; }
}

@keyframes pop { 0% { transform: scale(0.6); opacity: 0; } 70% { transform: scale(1.06); } 100% { transform: scale(1); opacity: 1; } }
@keyframes slidein { from { transform: translateX(120%); } to { transform: none; } }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.06); } }
@keyframes wiggle { 0%, 100% { transform: rotate(0); } 25% { transform: rotate(-4deg); } 75% { transform: rotate(4deg); } }
@keyframes glowpulse { 0%, 100% { stroke-opacity: 1; } 50% { stroke-opacity: 0.3; } }
@keyframes twinkle { from { opacity: 0.3; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; } }
body.cutscene #hud { display: none; }
.bico { width: 26px; height: 26px; display: block; margin: auto; }
.t-group button .bico { width: 44px; height: 44px; }
.check .light { display: inline-block; width: 20px; height: 20px; border-radius: 50%; background: #ff5f5f; border: 2px solid var(--ink); vertical-align: -4px; margin-right: 8px; box-shadow: 0 0 8px #ff5f5f; }
.check.on .light { background: #5cf08a; box-shadow: 0 0 10px #5cf08a; }
.tick { display: inline-grid; place-items: center; width: 22px; height: 22px; border: 2px solid var(--ink); border-radius: 6px; background: #fff; font-weight: 900; color: #1f9c6b; vertical-align: -3px; }
#toasts { z-index: 22; }
@media (max-width: 900px), (max-height: 520px) {
  #achievements-pop { top: auto; bottom: 110px; right: 50%; transform: translateX(50%); }
  .ach-pop { max-width: 300px; padding: 6px 10px; }
  .ach-pop .medal { width: 36px; height: 36px; font-size: 20px; }
  .ach-name { font-size: 15px; }
  .ach-desc { font-size: 12px; }
  #toasts .toast:nth-last-child(n+3) { display: none; }
}
