:root {
  --bg: #0a1220; --panel: #111c2e; --panel2: #18273d; --line: #26405f; --text: #e6f1ff; --dim: #8aa2c0;
  --accent: #38e0a0; --warn: #ffb547; --bad: #ff5d6c; --gold: #ffd166;
  --st: env(safe-area-inset-top, 0px); --sb: env(safe-area-inset-bottom, 0px);
  --sl: env(safe-area-inset-left, 0px); --sr: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; touch-action: none;
  overscroll-behavior: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
#app { position: fixed; inset: 0; padding: var(--st) var(--sr) var(--sb) var(--sl); }
.screen { display: none; position: absolute; inset: var(--st) var(--sr) var(--sb) var(--sl); flex-direction: column; }
.screen.active { display: flex; }
button { font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 14px; cursor: pointer; touch-action: manipulation; }
button:active { transform: scale(0.97); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: linear-gradient(#2fd394, #1ea676); color: #04140d; border-color: #5ff0b8; font-weight: 700; }
button.danger { border-color: #7a2a35; color: #ffb3bb; }
button:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
small { color: var(--dim); }

/* Home */
#home { align-items: center; justify-content: center; text-align: center; padding: 16px;
  background: radial-gradient(circle at 50% 30%, #13304a 0, transparent 60%); }
.logo canvas { width: 150px; height: 150px; }
h1 { margin: 4px 0 0; font-size: 44px; line-height: 0.95; letter-spacing: 2px; text-transform: uppercase; }
h1 span { color: var(--accent); }
.tag { color: var(--dim); margin: 10px 0 22px; }
.menu { display: flex; flex-direction: column; gap: 10px; width: min(300px, 86vw); }
.menu button { padding: 14px; font-size: 18px; display: flex; justify-content: center; gap: 8px; align-items: baseline; }
.menu .big { font-size: 22px; padding: 16px; }
.foot { color: var(--dim); font-size: 12px; margin-top: 24px; }

/* Shared bars */
.bar { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; gap: 8px; }
.bar h2 { margin: 0; font-size: 22px; }
.bar > span { min-width: 44px; }
.back { width: 44px; height: 44px; padding: 0; font-size: 28px; line-height: 1; }
.pill { background: var(--panel2); border-radius: 99px; padding: 4px 10px; color: var(--gold); font-weight: 700; text-align: center; }

/* Levels */
.level-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; padding: 8px 14px 20px; overflow-y: auto; touch-action: pan-y; }
.level { aspect-ratio: 1 / 1.05; display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 2px; padding: 6px; }
.level b { font-size: 26px; }
.level small { font-size: 11px; line-height: 1.1; }
.level .stars { color: var(--gold); letter-spacing: 1px; font-size: 14px; }
.level .stars i { color: #33465f; font-style: normal; }
.level.locked { opacity: 0.4; }
.level.boss { border-color: #7a3442; }

/* Settings and how-to */
.form { display: flex; flex-direction: column; gap: 16px; padding: 12px 20px; }
.form label { display: flex; justify-content: space-between; align-items: center; gap: 12px; background: var(--panel); padding: 12px 14px; border-radius: 12px; }
.form input[type=range] { width: 55%; accent-color: var(--accent); touch-action: pan-x; }
.form input[type=checkbox] { width: 26px; height: 26px; accent-color: var(--accent); }
.note { margin: -8px 4px 0; color: var(--dim); font-size: 13px; }
.howto { padding: 4px 18px 24px; overflow-y: auto; touch-action: pan-y; }
.howto h3 { margin: 18px 0 8px; }
.cards { display: grid; gap: 8px; }
.cards div { display: flex; gap: 10px; align-items: center; background: var(--panel); border-radius: 12px; padding: 8px 10px; }
.cards img { width: 40px; height: 40px; flex: none; }
.cards p { margin: 0; font-size: 14px; }

/* Game */
#game { background: var(--bg); }
.hud { display: flex; align-items: center; gap: 6px; padding: 6px 8px; height: 52px; }
.stat { display: flex; align-items: center; gap: 5px; background: var(--panel); border-radius: 10px; padding: 6px 8px; flex: 1; min-width: 0; height: 40px; overflow: hidden; }
.stat:first-child { flex: none; } /* core integrity is at most two digits; leave the room to scrap and wave */
.stat b { font-size: 17px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.stat small { font-size: 10px; font-weight: 700; }
.ico { width: 16px; height: 16px; border-radius: 50%; flex: none; }
.ico.core { background: radial-gradient(#fff 0, #38e0a0 45%, #136b4c 100%); box-shadow: 0 0 8px #38e0a0; }
.ico.scrap { background: conic-gradient(#ffd166 0 25%, #b98a2d 0 50%, #ffd166 0 75%, #b98a2d 0); border-radius: 3px; }
.stat.hurt { animation: hurt 0.4s; }
@keyframes hurt { 0%, 100% { background: var(--panel); } 30% { background: #6b1d27; } }
.hud-btn { width: 46px; height: 40px; padding: 0; font-weight: 700; flex: none; }
#board-wrap { position: relative; flex: 1; min-height: 0; }
#board { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
#toast { position: absolute; left: 50%; top: 10px; transform: translateX(-50%); background: #000c; border: 1px solid var(--line);
  padding: 6px 12px; border-radius: 99px; font-size: 14px; pointer-events: none; opacity: 0; transition: opacity 0.25s; white-space: nowrap; max-width: 94%; overflow: hidden; text-overflow: ellipsis; }
#toast.show { opacity: 1; }
.panel { padding: 6px 8px 8px; display: flex; flex-direction: column; gap: 6px; }
.wave-row { display: flex; gap: 6px; align-items: stretch; }
.preview { flex: 1; display: flex; gap: 4px; align-items: center; background: var(--panel); border-radius: 10px; padding: 4px 8px; overflow: hidden; min-height: 44px; }
.preview span { display: flex; align-items: center; gap: 2px; font-size: 13px; font-weight: 700; }
.preview img { width: 26px; height: 26px; }
.preview .boss { color: var(--bad); }
.preview em { font-style: normal; color: var(--dim); font-size: 12px; margin-right: 2px; }
#btn-wave { min-width: 124px; font-size: 15px; padding: 6px 10px; line-height: 1.15; }
#btn-wave small { display: block; color: #0a3d29; font-size: 11px; font-weight: 600; }
.build-bar, .sel-panel { display: grid; gap: 6px; height: 64px; }
.build-bar { grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 4px; }
.build-bar button { padding: 2px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0; border-radius: 10px; }
.build-bar img { width: 34px; height: 34px; }
.build-bar small { font-size: 12px; color: var(--gold); font-weight: 700; }
.build-bar button.on { border-color: var(--gold); background: #2b3a1f; box-shadow: inset 0 0 0 1px var(--gold); }
.build-bar button.poor small { color: var(--bad); }
.sel-panel { grid-template-columns: 1fr auto auto 44px; align-items: stretch; }
[hidden] { display: none !important; }
.sel-info { display: flex; flex-direction: column; justify-content: center; background: var(--panel); border-radius: 10px; padding: 4px 10px; min-width: 0; }
.sel-info b { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sel-info small { font-size: 11px; line-height: 1.25; }
.sel-panel button { padding: 4px 10px; font-size: 14px; line-height: 1.15; }
.sel-panel button small { display: block; font-size: 12px; }
#btn-upgrade small { color: #0a3d29; }
#btn-sell small { color: var(--gold); }
.tutorial { position: absolute; left: 10px; right: 10px; top: 60px; background: #071a2bf2; border: 1px solid var(--accent); border-radius: 14px;
  padding: 12px 14px; box-shadow: 0 8px 30px #0008; z-index: 5; }
.tutorial p { margin: 0 0 10px; font-size: 15px; }
.tut-btns { display: flex; justify-content: space-between; gap: 8px; }
.tut-btns button { padding: 8px 12px; font-size: 14px; }
.overlay { position: absolute; inset: 0; background: #020810c0; display: flex; align-items: center; justify-content: center; z-index: 10; padding: 20px; }
.overlay[hidden] { display: none; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 22px; width: min(340px, 100%); text-align: center;
  display: flex; flex-direction: column; gap: 10px; animation: pop 0.35s cubic-bezier(.2, 1.4, .4, 1); }
.card h2 { margin: 0; font-size: 28px; }
.card p { margin: 0; color: var(--dim); }
.card .fork { text-align: left; font-size: 14px; }
.card .big-stars { font-size: 46px; color: var(--gold); letter-spacing: 6px; line-height: 1; }
.card .big-stars i { color: #2a3a52; font-style: normal; }
.win h2 { color: var(--accent); }
.card .unlock b { color: var(--gold); }
.lose h2 { color: var(--bad); }
@keyframes pop { from { transform: scale(0.8); opacity: 0; } }
.reduce-motion *, .reduce-motion *::before { animation: none !important; transition: none !important; }
@media (min-width: 700px) { #game { max-width: 560px; margin: 0 auto; left: 0; right: 0; } }
@media (max-height: 700px) {
  .logo canvas { width: 90px; height: 90px; }
  h1 { font-size: 34px; }
  .tag { margin: 6px 0 12px; }
  .menu { gap: 7px; }
  .menu button, .menu .big { padding: 10px; font-size: 17px; }
  .foot { margin-top: 10px; }
}
.tutorial { pointer-events: none; }
.tutorial button { pointer-events: auto; }
