/* ============ Craft Wars — mobile-first ============ */
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  height: 100%; overflow: hidden;
  background: #0b0e1a; color: #e8ecff;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  touch-action: manipulation; user-select: none; -webkit-user-select: none;
  overscroll-behavior: none;
}
#app { position: fixed; inset: 0; }

.screen { position: absolute; inset: 0; display: none; flex-direction: column; }
.screen.active { display: flex; }

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }

/* ---------- logo / loading ---------- */
.game-logo {
  font-weight: 900; letter-spacing: 2px; text-align: center;
  font-size: clamp(34px, 9vw, 64px); line-height: 1;
  text-shadow: 0 4px 0 rgba(0,0,0,.55), 0 0 28px rgba(80,140,255,.35);
}
.logo-star { color: #6fb7ff; } .logo-war { color: #ffb84d; }
.game-logo.big { margin-bottom: 4px; }
.tagline { text-align: center; color: #9aa6d8; letter-spacing: 4px; font-size: 13px; text-transform: uppercase; margin-bottom: 26px; }

#screen-loading { align-items: center; justify-content: center; background: radial-gradient(circle at 50% 30%, #17203d 0%, #0b0e1a 70%); }
.load-box { width: min(340px, 80vw); text-align: center; }
.load-bar { height: 14px; margin: 22px 0 10px; border: 2px solid #33406e; border-radius: 8px; overflow: hidden; background: #10152b; }
#load-fill { height: 100%; width: 0%; background: linear-gradient(90deg, #2f7bff, #6fb7ff); transition: width .15s; }
#load-text { color: #9aa6d8; font-size: 13px; }

/* ---------- menu ---------- */
#screen-menu { justify-content: flex-end; }
.menu-banner {
  position: absolute; inset: 0; background-size: cover; background-position: center 72%;
  filter: saturate(1.05);
}
.menu-banner::after { content: ""; position: absolute; inset: 0;
  background: linear-gradient(180deg, rgba(11,14,26,.25) 0%, rgba(11,14,26,.55) 55%, rgba(11,14,26,.96) 100%); }
.menu-content {
  position: relative; z-index: 1; display: flex; flex-direction: column; align-items: center;
  padding: 20px 24px calc(30px + env(safe-area-inset-bottom)); gap: 12px;
}
.menu-row { display: flex; gap: 12px; }
.menu-credits { color: #ffd76f; font-weight: 700; margin-top: 4px; }
#btn-mute { position: absolute; top: calc(10px + env(safe-area-inset-top)); right: 14px; z-index: 2; }

/* ---------- buttons ---------- */
.btn {
  border-radius: 14px; font-weight: 800; letter-spacing: 1px;
  padding: 12px 20px; font-size: 15px;
  box-shadow: 0 4px 0 rgba(0,0,0,.45), inset 0 1px 0 rgba(255,255,255,.25);
  transition: transform .06s, filter .1s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,.45); }
.btn-xl { width: min(320px, 82vw); padding: 16px 20px; font-size: 18px; }
.btn-primary { background: linear-gradient(180deg, #4f95ff, #1f5fd6); color: #fff; }
.btn-endless { background: linear-gradient(180deg, #b366ff, #6b2fc9); color: #fff; }
.btn-secondary { background: linear-gradient(180deg, #3a4568, #262e4d); color: #dbe4ff; }
.btn-danger { background: linear-gradient(180deg, #ff6f5e, #c93a2f); color: #fff; }
.btn-ghost { color: #8f9bce; text-decoration: underline; font-size: 13px; padding: 8px; }
.btn.locked { filter: grayscale(.9) brightness(.6); }
.btn-icon {
  width: 42px; height: 42px; border-radius: 12px; font-size: 18px; font-weight: 800;
  background: rgba(30,38,70,.85); border: 1px solid #3a4a80; color: #dbe4ff;
  display: inline-flex; align-items: center; justify-content: center;
}

/* ---------- generic screens ---------- */
.screen-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 14px 10px;
  background: linear-gradient(180deg, #141a33, #10152b); border-bottom: 1px solid #26305a;
}
.screen-head h2 { font-size: 18px; letter-spacing: 3px; }
.head-credits { color: #ffd76f; font-weight: 800; min-width: 60px; text-align: right; }

/* level select */
#screen-levels { background: radial-gradient(circle at 50% 0%, #1a2342 0%, #0b0e1a 75%); }
.level-grid {
  flex: 1; overflow-y: auto; padding: 16px; display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  align-content: start; padding-bottom: calc(20px + env(safe-area-inset-bottom));
}
.level-card {
  border-radius: 14px; padding: 12px; text-align: left; position: relative;
  background: linear-gradient(180deg, #222c52, #171e3a); border: 1px solid #34407376;
  box-shadow: 0 3px 0 rgba(0,0,0,.4); min-height: 92px;
  display: flex; flex-direction: column; justify-content: space-between; gap: 6px;
}
.level-card:active { transform: translateY(2px); }
.level-card.locked { filter: grayscale(1) brightness(.55); }
.level-card.boss { border-color: #a13d3d; background: linear-gradient(180deg, #4a2330, #24141f); }
.level-num { font-size: 11px; color: #8f9bce; font-weight: 700; letter-spacing: 1px; }
.level-name { font-weight: 800; font-size: 14px; line-height: 1.15; }
.level-stars { color: #ffd76f; font-size: 13px; letter-spacing: 2px; }
.level-lock { position: absolute; top: 10px; right: 10px; font-size: 16px; }

/* armory */
#screen-armory { background: radial-gradient(circle at 50% 0%, #1a2342 0%, #0b0e1a 75%); }
.armory-hint { padding: 12px 16px 4px; color: #9aa6d8; font-size: 13px; }
.armory-list { flex: 1; overflow-y: auto; padding: 12px 16px calc(20px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 10px; }
.armory-item {
  display: flex; align-items: center; gap: 12px; padding: 12px;
  background: linear-gradient(180deg, #202950, #161d38); border: 1px solid #34407376; border-radius: 14px;
}
.armory-icon { font-size: 26px; width: 40px; text-align: center; }
.armory-info { flex: 1; }
.armory-name { font-weight: 800; font-size: 14px; }
.armory-desc { font-size: 12px; color: #9aa6d8; }
.armory-pips { margin-top: 4px; letter-spacing: 2px; font-size: 11px; color: #ffd76f; }
.armory-buy {
  min-width: 84px; padding: 10px 8px; border-radius: 12px; font-weight: 800; font-size: 13px;
  background: linear-gradient(180deg, #35b06a, #1d7a44); color: #fff;
  box-shadow: 0 3px 0 rgba(0,0,0,.4);
}
.armory-buy:disabled { filter: grayscale(.8) brightness(.6); }
.armory-buy.maxed { background: linear-gradient(180deg, #57618c, #3a4364); }

/* help */
#screen-help { background: #0e1226; }
.help-body { flex: 1; overflow-y: auto; padding: 16px 20px calc(24px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 12px; font-size: 14px; line-height: 1.5; color: #c6cff2; }
.help-body .fine { color: #6c77a8; font-size: 11px; }
#btn-reset { align-self: flex-start; margin-top: 8px; }

/* ---------- battle ---------- */
#screen-battle { background: #000; }
#game-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

#hud-top {
  position: absolute; top: 0; left: 0; right: 0; z-index: 5;
  display: flex; align-items: flex-start; gap: 8px;
  padding: calc(6px + env(safe-area-inset-top)) 10px 0;
  pointer-events: none;
}
.hp-wrap { flex: 1; }
.hp-wrap.right .hp-label { text-align: right; }
.hp-label { font-size: 10px; font-weight: 800; letter-spacing: 1px; color: #cfd9ff; text-shadow: 0 1px 2px #000; margin-bottom: 3px; }
.hp-bar { height: 12px; border-radius: 7px; background: rgba(8,10,20,.75); border: 1px solid rgba(120,140,220,.5); overflow: hidden; }
.hp-fill { height: 100%; width: 100%; transition: width .2s; }
.hp-fill.player { background: linear-gradient(180deg, #52d97c, #1f9c4d); }
.hp-fill.enemy { background: linear-gradient(180deg, #ff7a5e, #d0342c); float: right; }
.minerals-box {
  background: rgba(8,10,20,.78); border: 1px solid rgba(120,140,220,.5); border-radius: 10px;
  padding: 3px 10px 4px; text-align: center; min-width: 86px;
}
.minerals-val { font-weight: 900; font-size: 16px; color: #bfe1ff; text-shadow: 0 1px 2px #000; }
.income-val { font-size: 10px; color: #7fd6a4; font-weight: 700; }

#push-bar {
  position: absolute; top: calc(44px + env(safe-area-inset-top)); left: 10%; right: 10%; height: 5px; z-index: 5;
  background: linear-gradient(90deg, #2f7bff 0 50%, #d0342c 50% 100%);
  border-radius: 3px; opacity: .85; pointer-events: none;
}
#push-marker {
  position: absolute; top: -4px; width: 4px; height: 13px; left: 50%;
  background: #fff; border-radius: 2px; box-shadow: 0 0 6px #fff;
}

#battle-ctrl { position: absolute; top: calc(52px + env(safe-area-inset-top)); right: 10px; z-index: 6; display: flex; gap: 8px; }
#battle-ctrl .btn-icon { width: 38px; height: 38px; font-size: 15px; }

#wave-alert {
  position: absolute; top: 26%; left: 0; right: 0; z-index: 6; text-align: center;
  font-weight: 900; font-size: clamp(18px, 5vw, 30px); letter-spacing: 3px;
  color: #ff5e4d; text-shadow: 0 2px 0 #000, 0 0 24px rgba(255,60,40,.7);
  opacity: 0; pointer-events: none; transition: opacity .3s;
}
#wave-alert.show { opacity: 1; animation: pulse .5s infinite alternate; }
@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.07); } }

#endless-score {
  position: absolute; top: calc(58px + env(safe-area-inset-top)); left: 0; right: 0; z-index: 5;
  text-align: center; font-weight: 900; color: #e6b3ff; text-shadow: 0 1px 3px #000; font-size: 14px;
  pointer-events: none;
}

/* bottom command bar */
#hud-bottom {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 6;
  padding: 6px 8px calc(8px + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(10,13,26,.15), rgba(10,13,26,.92) 38%);
  display: flex; flex-direction: column; gap: 6px;
}
#unit-bar, #util-bar {
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; align-items: flex-end;
}
#unit-bar::-webkit-scrollbar, #util-bar::-webkit-scrollbar { display: none; }
#util-bar { justify-content: flex-start; }

.spawn-btn {
  position: relative; flex: 0 0 auto; width: 64px; height: 72px; border-radius: 12px;
  background: linear-gradient(180deg, #2a3560, #1a2140);
  border: 1px solid #40518f; box-shadow: 0 3px 0 rgba(0,0,0,.5);
  display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
  overflow: hidden;
}
.spawn-btn img { position: absolute; top: 4px; left: 50%; transform: translateX(-50%); height: 44px; image-rendering: pixelated; pointer-events: none; }
.spawn-cost { position: relative; z-index: 2; font-size: 11px; font-weight: 800; color: #bfe1ff; text-shadow: 0 1px 2px #000; padding-bottom: 3px; }
.spawn-btn:active { transform: translateY(2px); }
.spawn-btn.poor .spawn-cost { color: #ff8d7e; }
.spawn-btn.poor img { filter: grayscale(.8) brightness(.6); }
.spawn-cd {
  position: absolute; inset: 0; z-index: 3; pointer-events: none;
  background: conic-gradient(rgba(6,8,16,.82) calc(var(--cd) * 360deg), transparent 0);
}
.util-btn {
  position: relative; flex: 0 0 auto; min-width: 84px; height: 46px; border-radius: 12px; padding: 4px 10px;
  background: linear-gradient(180deg, #234a2e, #16301e); border: 1px solid #3f7a4f;
  box-shadow: 0 3px 0 rgba(0,0,0,.5); overflow: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.15;
}
.util-btn:active { transform: translateY(2px); }
.util-btn .u-name { font-size: 11px; font-weight: 800; }
.util-btn .u-sub { font-size: 10px; color: #9fe8b8; font-weight: 700; }
.util-btn.spell { background: linear-gradient(180deg, #35306b, #221d4a); border-color: #6a5fd0; }
.util-btn.spell .u-sub { color: #c9befc; }
.util-btn.nuke { background: linear-gradient(180deg, #6b3030, #401a1a); border-color: #d05f5f; }
.util-btn.nuke .u-sub { color: #ffb3a8; }
.util-btn.poor, .util-btn:disabled { filter: grayscale(.7) brightness(.65); }
.util-btn .spawn-cd { border-radius: 12px; }

/* modals */
.modal-wrap {
  position: absolute; inset: 0; z-index: 20; display: none;
  align-items: center; justify-content: center; background: rgba(4,6,14,.72); backdrop-filter: blur(3px);
}
.modal-wrap.show { display: flex; }
.modal {
  width: min(400px, 88vw); max-height: 86vh; overflow-y: auto;
  background: linear-gradient(180deg, #1d2547, #131832); border: 1px solid #3c4b8c;
  border-radius: 18px; padding: 22px; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  box-shadow: 0 12px 40px rgba(0,0,0,.6);
}
.modal h3 { font-size: 26px; letter-spacing: 2px; }
.modal-lvl { font-size: 11px; letter-spacing: 3px; color: #8f9bce; font-weight: 800; }
.intro-enemies { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.intro-enemies .foe { display: flex; flex-direction: column; align-items: center; width: 64px; font-size: 10px; color: #c6cff2; gap: 3px; }
.intro-enemies .foe img { height: 46px; image-rendering: pixelated; }
.intro-tip { font-size: 13px; color: #9aa6d8; line-height: 1.45; }
.stars { font-size: 34px; letter-spacing: 6px; min-height: 42px; }
.stars .dim { opacity: .25; }
.result-lines { font-size: 14px; color: #c6cff2; display: flex; flex-direction: column; gap: 4px; }
.result-lines b { color: #ffd76f; }
#result-unlock:empty { display: none; }
#result-unlock {
  background: rgba(80,140,255,.12); border: 1px dashed #4f95ff; padding: 10px 14px; border-radius: 12px;
  color: #bfe1ff; font-weight: 700; font-size: 14px; display: flex; align-items: center; gap: 10px;
}
#result-unlock img { height: 40px; image-rendering: pixelated; }
#modal-result h3.win { color: #7fe89d; text-shadow: 0 0 22px rgba(80,255,140,.4); }
#modal-result h3.lose { color: #ff7a5e; text-shadow: 0 0 22px rgba(255,90,60,.4); }

/* toast */
#toast {
  position: fixed; left: 50%; bottom: 120px; transform: translateX(-50%) translateY(20px);
  background: rgba(20,26,52,.95); border: 1px solid #4f95ff; color: #dbe9ff;
  padding: 10px 18px; border-radius: 12px; font-weight: 700; font-size: 14px;
  opacity: 0; pointer-events: none; transition: all .3s; z-index: 50; white-space: nowrap;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 380px) {
  .spawn-btn { width: 56px; height: 66px; }
  .spawn-btn img { height: 38px; }
}
/* phones held sideways: single compact command row so the lane stays visible */
@media (orientation: landscape) and (max-height: 500px) {
  #hud-bottom { flex-direction: row; align-items: flex-end; gap: 8px; }
  #unit-bar { flex: 1 1 auto; min-width: 0; }
  #util-bar { flex: 0 0 auto; }
  .spawn-btn { width: 52px; height: 58px; }
  .spawn-btn img { height: 32px; }
  .spawn-cost { font-size: 10px; }
  .util-btn { min-width: 68px; height: 44px; padding: 2px 8px; }
  .util-btn .u-name { font-size: 10px; }
}
@media (min-width: 700px) {
  .level-grid { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); max-width: 900px; margin: 0 auto; width: 100%; }
  .armory-list { max-width: 640px; margin: 0 auto; width: 100%; }
  .help-body { max-width: 640px; margin: 0 auto; width: 100%; }
}
