* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; width: 100%; height: 100%;
  overflow: hidden; background: #152520; color: #3d2a1e;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", sans-serif;
  overscroll-behavior: none;
}
body {
  display: flex; align-items: stretch; justify-content: center;
  background:
    radial-gradient(circle at 50% 0%, #2b4940 0%, #152520 58%, #0e1815 100%);
}
button { font-family: inherit; border: 0; margin: 0; cursor: pointer; }

.game-shell {
  position: relative; width: 100%; width: min(100%, 480px, 60vh); width: min(100%, 480px, 60dvh); max-width: 480px;
  height: 100vh; height: 100dvh;
  overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none;
  background: #7fd3c8;
  box-shadow: 0 0 60px rgba(0, 0, 0, .45);
  isolation: isolate;
}
#game { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

/* ---------------- HUD ---------------- */
.hud {
  position: absolute; top: 0; left: 0; right: 0; z-index: 20;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) 12px 0;
  pointer-events: none;
}
.hud-row { display: flex; align-items: center; gap: 7px; }
.player-chip {
  display: flex; align-items: center; gap: 6px; min-width: 0; flex: 0 1 auto;
  max-width: 42%;
  background: rgba(255, 249, 237, .94);
  border: 2px solid rgba(112, 72, 40, .18);
  border-radius: 999px; padding: 3px 10px 3px 4px;
  box-shadow: 0 3px 0 rgba(118, 77, 43, .16), 0 6px 14px rgba(56, 36, 22, .10);
}
.avatar {
  position: relative; width: 29px; height: 29px; flex: 0 0 29px; border-radius: 50%;
  background: linear-gradient(145deg, #45b3a2, #267a72);
  border: 2px solid #fff6e6; overflow: hidden;
  color: #fff8e8; font-size: 13px; font-weight: 900;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 1px 3px rgba(50, 30, 18, .25);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar img[hidden] { display: none; }
.player-name {
  font-size: 12px; font-weight: 800; color: #5c3f2b; letter-spacing: .02em;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.wave-pill {
  display: flex; align-items: center; justify-content: center; gap: 5px;
  height: 29px; padding: 0 11px; border-radius: 999px; flex: 0 0 auto;
  background: linear-gradient(180deg, #ffa45b, #f57f32);
  border: 2px solid rgba(255, 255, 255, .38);
  color: #fff; font-size: 11.5px; font-weight: 900; letter-spacing: .04em;
  box-shadow: 0 3px 0 #c96022, 0 6px 14px rgba(112, 57, 19, .22);
  text-shadow: 0 1px 1px rgba(120, 53, 12, .3);
}
.wave-dot {
  width: 6px; height: 6px; border-radius: 50%; background: #fff5d9;
  box-shadow: 0 0 0 2px rgba(255, 245, 217, .3);
  animation: dotPulse 1.4s ease-in-out infinite;
}
.wave-pill.idle .wave-dot { animation: none; opacity: .55; }
.hud-right { margin-left: auto; display: flex; align-items: center; gap: 6px; }
.sound-btn {
  width: 32px; height: 32px; padding: 0; border-radius: 50%;
  background: rgba(255, 249, 237, .94);
  border: 2px solid rgba(112, 72, 40, .16);
  color: #8a684c; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 3px 0 rgba(118, 77, 43, .14);
  pointer-events: auto;
}
.sound-btn .mp-icon { width: 16px; height: 16px; }
.sound-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(118, 77, 43, .14); }
.score-chip {
  display: flex; align-items: center; gap: 3px; height: 29px; padding: 0 9px 0 7px;
  border-radius: 999px; background: rgba(255, 249, 237, .94);
  border: 2px solid rgba(112, 72, 40, .16);
  box-shadow: 0 3px 0 rgba(118, 77, 43, .14), 0 6px 14px rgba(56, 36, 22, .1);
  font-size: 12px; font-weight: 900; color: #7a4a20;
}
.score-chip .mp-icon { width: 15px; height: 15px; color: #f5b63c; }
.bars { display: flex; gap: 8px; margin-top: 8px; }
.bar-card {
  flex: 1 1 0; min-width: 0; padding: 4px 8px 6px;
  background: rgba(255, 249, 237, .94);
  border: 2px solid rgba(112, 72, 40, .14);
  border-radius: 11px;
  box-shadow: 0 3px 0 rgba(118, 77, 43, .13), 0 5px 12px rgba(56, 36, 22, .08);
}
.bar-top { display: flex; align-items: center; justify-content: space-between; gap: 4px; }
.bar-name {
  display: flex; align-items: center; gap: 3px;
  font-size: 10px; font-weight: 800; color: #74533a; white-space: nowrap;
}
.bar-name .mp-icon { width: 12px; height: 12px; color: #a57952; }
#gateNum, #wallNum { font-size: 10px; font-weight: 900; color: #9a7555; }
.bar-track {
  position: relative; height: 7px; margin-top: 3px; border-radius: 999px;
  background: #e7d8c2; overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(92, 59, 34, .2);
}
.bar-fill {
  display: block; height: 100%; border-radius: 999px;
  transition: width .28s ease;
  position: relative; z-index: 1;
}
.bar-fill.gate { width: 100%; background: linear-gradient(90deg, #ef5c46, #ff9c5c); }
.bar-fill.wall { width: 100%; background: linear-gradient(90deg, #3ca47d, #9bd663); }
.bar-shine {
  position: absolute; left: 2px; right: 2px; top: 1px; height: 2px; z-index: 2;
  border-radius: 999px; background: rgba(255, 255, 255, .45); pointer-events: none;
}
.bar-card.low { animation: lowPulse .8s ease-in-out infinite; }
.bar-card.low .bar-fill { background: linear-gradient(90deg, #e33c32, #ff7754); }

/* ---------------- Banner ---------------- */
.banner {
  position: absolute; left: 50%; top: 33%; transform: translate(-50%, -50%);
  z-index: 19; text-align: center; pointer-events: none; opacity: 0; width: 92%;
}
.banner.show { animation: bannerIn 2.1s cubic-bezier(.2, .8, .25, 1) forwards; }
.banner-main {
  font-size: 28px; font-weight: 900; letter-spacing: .08em; color: #fff6de;
  text-shadow: 0 2px 0 #a95220, 0 4px 0 rgba(95, 43, 12, .35), 0 8px 22px rgba(49, 25, 11, .45);
}
.banner-sub {
  display: inline-block; margin-top: 7px; padding: 4px 12px; border-radius: 999px;
  background: rgba(55, 33, 20, .55); border: 1px solid rgba(255, 240, 211, .25);
  font-size: 11.5px; font-weight: 700; color: #ffe9c4; letter-spacing: .05em;
  backdrop-filter: blur(3px);
}
.banner.win .banner-main { color: #fff2b8; text-shadow: 0 2px 0 #b9731d, 0 4px 0 rgba(95, 43, 12, .3), 0 8px 24px rgba(49, 25, 11, .4); }
.banner.lose .banner-main { color: #ffd4c4; text-shadow: 0 2px 0 #792d22, 0 4px 0 rgba(50, 20, 14, .35), 0 8px 24px rgba(49, 25, 11, .4); }

/* ---------------- Coach ---------------- */
.coach {
  position: absolute; z-index: 18; transform: translate(-50%, -100%);
  padding: 7px 12px; border-radius: 13px; white-space: nowrap;
  background: #fff8e9; border: 2px solid #e4c9a5; color: #6b4a31;
  font-size: 12px; font-weight: 800; letter-spacing: .02em;
  box-shadow: 0 4px 0 rgba(126, 84, 48, .16), 0 8px 18px rgba(68, 42, 22, .14);
  pointer-events: none; animation: coachFloat 2.2s ease-in-out infinite;
  transition: opacity .25s;
}
.coach::after {
  content: ""; position: absolute; left: 50%; bottom: -8px; transform: translateX(-50%);
  width: 12px; height: 12px; background: #fff8e9;
  border-right: 2px solid #e4c9a5; border-bottom: 2px solid #e4c9a5;
  transform: translateX(-50%) rotate(45deg);
}
.coach.hide { opacity: 0; }

/* ---------------- Prep tray ---------------- */
.prep-tray {
  position: absolute; left: 9px; right: 9px; bottom: calc(env(safe-area-inset-bottom, 0px) + 9px);
  z-index: 26; padding: 9px 11px 10px;
  background: linear-gradient(180deg, #fffaf0 0%, #fbe9cf 100%);
  border: 2px solid #e8cda8; border-radius: 19px;
  box-shadow: 0 -4px 0 rgba(146, 96, 52, .12), 0 12px 30px rgba(56, 34, 19, .24), inset 0 1px 0 #fff;
  transition: transform .28s cubic-bezier(.5, 0, .7, .3), opacity .22s;
}
.prep-tray.out { transform: translateY(125%); opacity: 0; pointer-events: none; }
.prep-head { display: flex; align-items: center; gap: 7px; }
.prep-title { font-size: 14.5px; font-weight: 900; color: #5f3d27; letter-spacing: .03em; }
.prep-count {
  padding: 2px 7px; border-radius: 999px; background: #f1dfc5; color: #a07044;
  font-size: 10px; font-weight: 900;
}
.start-btn {
  margin-left: auto; height: 38px; padding: 0 15px; border-radius: 11px;
  background: linear-gradient(180deg, #ffa55b, #f4782c);
  border: 2px solid rgba(255, 255, 255, .45);
  color: #fff; font-size: 12.5px; font-weight: 900; letter-spacing: .06em;
  box-shadow: 0 3px 0 #c95d1f, 0 5px 12px rgba(139, 66, 19, .24);
  transition: transform .1s, box-shadow .1s, filter .2s;
}
.start-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #c95d1f; }
.start-btn:disabled {
  background: #e9dbc7; color: #b69b7e; border: 2px dashed #d6bfa1;
  box-shadow: none; cursor: default;
}
.start-btn.ready { animation: startPulse 1.5s ease-in-out infinite; }
.prep-tip { margin: 3px 0 7px; font-size: 10.5px; font-weight: 600; color: #a17c5b; }
.unit-cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; }
.unit-card {
  position: relative; display: flex; flex-direction: column; align-items: center;
  padding: 4px 2px 6px; min-height: 80px; border-radius: 13px;
  background: #fffdf6; border: 2px solid #ead8bc;
  box-shadow: 0 3px 0 #e5d2b4;
  transition: transform .12s, box-shadow .12s, border-color .12s, background .12s;
}
.unit-card:active { transform: translateY(2px); box-shadow: 0 1px 0 #e5d2b4; }
.unit-card.active {
  border-color: #f59445; background: #fff2de; transform: translateY(-2px);
  box-shadow: 0 5px 0 #e4a067, 0 6px 14px rgba(190, 110, 49, .16);
}
.unit-thumb { width: 42px; height: 42px; display: block; margin-top: 1px; }
.unit-name { font-size: 12px; font-weight: 900; color: #5c3d27; line-height: 1.2; }
.unit-desc { font-size: 9px; font-weight: 600; color: #a18061; margin-top: 1px; white-space: nowrap; }
.unit-selected {
  position: absolute; top: 4px; right: 4px; width: 15px; height: 15px; border-radius: 50%;
  background: #f57f32; opacity: 0; transform: scale(.5); transition: .15s;
  box-shadow: 0 1px 2px rgba(90, 40, 10, .25);
}
.unit-selected::after {
  content: ""; position: absolute; left: 4.5px; top: 3px; width: 4px; height: 6px;
  border-right: 2px solid #fff; border-bottom: 2px solid #fff; transform: rotate(42deg);
}
.unit-card.active .unit-selected { opacity: 1; transform: scale(1); }

/* ---------------- Battle bar ---------------- */
.battle-bar {
  position: absolute; left: 9px; right: 9px; bottom: calc(env(safe-area-inset-bottom, 0px) + 9px);
  z-index: 26; padding: 7px 9px 9px;
  background: linear-gradient(180deg, #fffaf0 0%, #fbe9cf 100%);
  border: 2px solid #e8cda8; border-radius: 19px;
  box-shadow: 0 -4px 0 rgba(146, 96, 52, .12), 0 12px 30px rgba(56, 34, 19, .24), inset 0 1px 0 #fff;
  transform: translateY(135%); opacity: 0; pointer-events: none;
  transition: transform .3s cubic-bezier(.2, .8, .3, 1), opacity .22s;
}
.battle-bar.in { transform: translateY(0); opacity: 1; pointer-events: auto; }
.battle-tip {
  text-align: center; font-size: 10.5px; font-weight: 700; color: #987355;
  height: 14px; line-height: 14px; margin-bottom: 5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.action-btn {
  position: relative; display: flex; align-items: center; gap: 7px; height: 62px;
  padding: 0 9px; border-radius: 15px; overflow: hidden; color: #fff;
  border: 2px solid rgba(255, 255, 255, .34);
  box-shadow: 0 4px 0 rgba(65, 38, 18, .25), 0 7px 14px rgba(67, 38, 19, .18);
  transition: transform .1s, box-shadow .1s, filter .2s, outline-color .15s;
  text-align: left;
}
.action-btn:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(65, 38, 18, .25); }
.action-btn.arrow { background: linear-gradient(145deg, #4bb9a6, #2a857a); }
.action-btn.siege { background: linear-gradient(145deg, #ffa45b, #ee7026); }
.action-btn.aiming { outline: 3px solid #fff3c9; outline-offset: -5px; animation: aimPulse 1s ease-in-out infinite; }
.action-btn.cooling { filter: saturate(.55) brightness(.9); }
.action-btn.blocked { filter: grayscale(.15); }
.action-icon {
  width: 32px; height: 32px; flex: 0 0 32px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(255, 250, 236, .22); border: 1px solid rgba(255, 250, 236, .25);
}
.action-icon .mp-icon { width: 20px; height: 20px; color: #fff9e8; }
.action-copy { display: flex; flex-direction: column; min-width: 0; }
.action-label { font-size: 13.5px; font-weight: 900; letter-spacing: .03em; white-space: nowrap; text-shadow: 0 1px 2px rgba(49, 26, 12, .2); }
.action-sub { font-size: 9px; font-weight: 700; opacity: .85; margin-top: 1px; white-space: nowrap; }
.cool {
  position: absolute; left: 0; right: 0; bottom: 0; height: 0%;
  background: repeating-linear-gradient(-45deg, rgba(25, 15, 9, .36) 0 6px, rgba(25, 15, 9, .28) 6px 12px);
  pointer-events: none; transition: height .08s linear;
}

/* ---------------- Toast ---------------- */
.toast {
  position: absolute; left: 50%; bottom: 158px; transform: translate(-50%, 8px);
  z-index: 40; max-width: 84%; padding: 7px 13px; border-radius: 999px;
  background: rgba(49, 31, 20, .86); color: #fff3dc;
  font-size: 11.5px; font-weight: 700; letter-spacing: .02em; text-align: center;
  opacity: 0; pointer-events: none; transition: opacity .18s, transform .18s;
  backdrop-filter: blur(4px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.prep { bottom: calc(env(safe-area-inset-bottom, 0px) + 216px); }

/* ---------------- Result overlay ---------------- */
.overlay {
  position: absolute; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center;
  padding: 24px 20px;
  background: rgba(25, 17, 12, .6); backdrop-filter: blur(4px);
  animation: fadeIn .25s ease-out;
}
.overlay[hidden] { display: none; }
.result-card {
  position: relative; width: 100%; max-width: 330px;
  padding: 30px 20px 20px; border-radius: 25px; text-align: center;
  background: linear-gradient(180deg, #fffaf0 0%, #fbe6c6 100%);
  border: 3px solid #eacaa0;
  box-shadow: 0 10px 0 rgba(116, 71, 37, .12), 0 24px 50px rgba(23, 14, 9, .4), inset 0 1px 0 #fff;
  animation: cardPop .38s cubic-bezier(.2, 1.3, .4, 1);
}
.result-badge {
  position: absolute; left: 50%; top: 0; transform: translate(-50%, -50%);
  width: 62px; height: 62px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(160deg, #ffc262, #f47c2c);
  border: 3px solid #fff6e3;
  box-shadow: 0 5px 0 #c25c1d, 0 8px 18px rgba(126, 64, 22, .28);
}
.result-badge .mp-icon { width: 31px; height: 31px; color: #fff9e9; }
.result-card.lose .result-badge { background: linear-gradient(160deg, #8a9a9b, #536c70); box-shadow: 0 5px 0 #384b4e, 0 8px 18px rgba(38, 50, 52, .3); }
.result-card h2 { margin: 8px 0 6px; font-size: 23px; font-weight: 900; color: #5d3a22; letter-spacing: .06em; }
.result-card p { margin: 0 auto; max-width: 250px; font-size: 12px; line-height: 1.6; color: #947052; }
.result-stats {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px;
  margin: 16px 0 17px;
}
.result-stat {
  padding: 8px 3px 7px; border-radius: 13px;
  background: rgba(255, 253, 246, .78); border: 1.5px solid #eed9ba;
  box-shadow: inset 0 1px 0 #fff;
}
.result-num { display: block; font-size: 18px; font-weight: 900; color: #d06b28; line-height: 1.15; }
.result-label { display: block; margin-top: 2px; font-size: 9px; font-weight: 700; color: #a98262; }
.again-btn {
  width: 100%; height: 47px; border-radius: 14px;
  background: linear-gradient(180deg, #ffa45b, #f1772b);
  border: 2px solid rgba(255, 255, 255, .48);
  color: #fff; font-size: 15px; font-weight: 900; letter-spacing: .1em;
  box-shadow: 0 4px 0 #c25c1d, 0 8px 16px rgba(134, 63, 18, .22);
  transition: transform .1s, box-shadow .1s;
}
.again-btn:active { transform: translateY(2px); box-shadow: 0 2px 0 #c25c1d; }

/* ---------------- Animations ---------------- */
@keyframes dotPulse { 0%, 100% { transform: scale(1); opacity: 1; } 50% { transform: scale(.7); opacity: .65; } }
@keyframes lowPulse { 0%, 100% { border-color: rgba(214, 71, 51, .25); } 50% { border-color: rgba(214, 71, 51, .72); } }
@keyframes bannerIn {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(.8); }
  13% { opacity: 1; transform: translate(-50%, -50%) scale(1.04); }
  22% { transform: translate(-50%, -50%) scale(1); }
  78% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -62%) scale(.98); }
}
@keyframes coachFloat { 0%, 100% { margin-top: 0; } 50% { margin-top: -4px; } }
@keyframes startPulse { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-2px); } }
@keyframes aimPulse { 0%, 100% { outline-color: rgba(255, 243, 201, .45); } 50% { outline-color: rgba(255, 243, 201, 1); } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes cardPop { 0% { opacity: 0; transform: scale(.82) translateY(14px); } 100% { opacity: 1; transform: scale(1) translateY(0); } }

@media (max-width: 350px) {
  .player-name { display: none; }
  .player-chip { padding-right: 5px; }
  .action-label { font-size: 12.5px; }
  .action-btn { padding: 0 7px; gap: 5px; }
  .action-icon { width: 28px; height: 28px; flex-basis: 28px; }
  .unit-desc { font-size: 8px; }
  .bar-name { font-size: 9px; }
}
@media (max-height: 820px) {
  .prep-tray { padding: 7px 9px 8px; }
  .prep-tip { display: none; }
  .unit-card { min-height: 66px; padding: 3px 2px 4px; }
  .unit-thumb { width: 34px; height: 34px; }
  .unit-name { font-size: 11px; }
  .unit-desc { font-size: 8px; }
  .start-btn { height: 34px; }
}
@media (max-height: 700px) {
  .battle-bar { padding: 6px 8px 8px; }
  .action-btn { height: 56px; }
  .hud { padding-top: calc(env(safe-area-inset-top, 0px) + 5px); }
}
