* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #5FAF43;
  color: #3C2817;
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
  touch-action: manipulation;
}

[hidden] { display: none !important; }

#scene {
  position: fixed;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  z-index: 1;
}

/* ---------------- 顶部玩家信息 ---------------- */
.hud {
  position: fixed;
  top: calc(9px + env(safe-area-inset-top));
  left: 0;
  right: 0;
  z-index: 7;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 13px;
  pointer-events: none;
}

.player-chip {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 48px;
  max-width: 176px;
  padding: 4px 13px 4px 4px;
  border-radius: 999px;
  background: rgba(255, 250, 239, 0.92);
  border: 1px solid rgba(255, 255, 255, 0.95);
  box-shadow: 0 3px 10px rgba(96, 55, 13, 0.16);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}

.avatar {
  position: relative;
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  border-radius: 50%;
  overflow: hidden;
  background: #FFE0B4;
  box-shadow: 0 0 0 2px #FF9A4E, 0 2px 4px rgba(147, 72, 12, 0.28);
}

.avatar img,
.avatar-fallback {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.player-meta {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.player-meta b {
  display: block;
  max-width: 92px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
  line-height: 1.2;
  font-weight: 800;
  letter-spacing: 0.1px;
}

.player-meta small {
  display: block;
  font-size: 9px;
  line-height: 1.2;
  color: #A38059;
  letter-spacing: 0.4px;
}

.hud-stats {
  display: flex;
  align-items: center;
  gap: 6px;
  pointer-events: auto;
}

.stat-pill {
  display: flex;
  align-items: center;
  gap: 3px;
  height: 34px;
  padding: 0 10px 0 8px;
  border-radius: 999px;
  background: rgba(255, 250, 239, 0.93);
  border: 1px solid rgba(255, 255, 255, 0.95);
  box-shadow: 0 3px 10px rgba(96, 55, 13, 0.15);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  font-size: 14px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.stat-pill .mp-icon { width: 17px; height: 17px; }

.stat-pill.coin { color: #8A5316; }
.stat-pill.coin .mp-icon { color: #F5A623; }

.stat-pill.heart { color: #B23B31; }
.stat-pill.heart .mp-icon { color: #FF6659; }

.stat-pill.heart.danger { animation: hpPulse 0.65s ease-in-out infinite alternate; }
.stat-pill.bump { animation: pillBump 0.3s ease-out; }
.stat-pill.coin.bump .mp-icon { color: #FFC53D; }

@keyframes hpPulse {
  from { box-shadow: 0 3px 10px rgba(96, 55, 13, 0.15); }
  to { box-shadow: 0 0 0 4px rgba(255, 76, 60, 0.3), 0 3px 12px rgba(190, 40, 30, 0.28); }
}

@keyframes pillBump {
  0% { transform: scale(1); }
  45% { transform: scale(1.17); }
  100% { transform: scale(1); }
}

/* ---------------- 提示 / 公告 / 飘字 ---------------- */
.hint {
  position: fixed;
  left: 50%;
  top: calc(68px + env(safe-area-inset-top));
  z-index: 8;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 15px 8px 12px;
  border-radius: 999px;
  background: rgba(55, 38, 21, 0.82);
  color: #FFF5E0;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.2px;
  box-shadow: 0 6px 16px rgba(52, 31, 9, 0.2);
  transform: translate(-50%, 0);
  transition: opacity 0.3s ease, transform 0.3s ease;
  pointer-events: none;
  white-space: nowrap;
}

.hint.hide {
  opacity: 0;
  transform: translate(-50%, -8px);
}

.hint-spark {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #FFC15C;
  box-shadow: 0 0 0 0 rgba(255, 193, 92, 0.7);
  animation: spark 1.4s ease-out infinite;
}

@keyframes spark {
  0% { box-shadow: 0 0 0 0 rgba(255, 193, 92, 0.65); }
  70% { box-shadow: 0 0 0 7px rgba(255, 193, 92, 0); }
  100% { box-shadow: 0 0 0 0 rgba(255, 193, 92, 0); }
}

.banner {
  position: fixed;
  top: 24%;
  left: 0;
  right: 0;
  z-index: 9;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  pointer-events: none;
  opacity: 0;
  transform: translateY(12px) scale(0.92);
}

.banner.show { animation: bannerIn 1.9s cubic-bezier(0.2, 0.9, 0.25, 1) forwards; }

.banner b {
  font-size: 33px;
  line-height: 1.1;
  font-weight: 900;
  letter-spacing: 1px;
  color: #FFF6DC;
  text-shadow: 0 2px 0 #E67A27, 0 5px 0 rgba(128, 57, 9, 0.22), 0 9px 20px rgba(89, 45, 8, 0.4);
}

.banner span {
  max-width: 88vw;
  padding: 4px 12px;
  border-radius: 999px;
  background: rgba(67, 42, 17, 0.62);
  color: #FFECC7;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

@keyframes bannerIn {
  0% { opacity: 0; transform: translateY(16px) scale(0.88); }
  14% { opacity: 1; transform: translateY(0) scale(1.04); }
  22% { transform: translateY(0) scale(1); }
  78% { opacity: 1; transform: translateY(0) scale(1); }
  100% { opacity: 0; transform: translateY(-12px) scale(0.98); }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 210px;
  z-index: 26;
  max-width: 82vw;
  padding: 9px 16px;
  border-radius: 999px;
  background: rgba(46, 30, 15, 0.9);
  color: #FFF7E5;
  font-size: 12px;
  font-weight: 700;
  box-shadow: 0 8px 20px rgba(42, 24, 6, 0.24);
  opacity: 0;
  transform: translate(-50%, 8px);
  transition: opacity 0.24s ease, transform 0.24s ease;
  pointer-events: none;
  white-space: nowrap;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------------- 炮塔详情气泡 ---------------- */
.tower-pop {
  position: fixed;
  z-index: 22;
  width: 206px;
  padding: 10px 12px 11px;
  border-radius: 18px;
  background: #FFF9EC;
  border: 2px solid #F4D8A8;
  box-shadow: 0 10px 24px rgba(92, 51, 10, 0.24), inset 0 1px 0 #fff;
  transform: translate(-50%, -100%);
  animation: popIn 0.16s ease-out;
}

.tower-pop.below { transform: translate(-50%, 0); }

.tower-pop::after {
  content: "";
  position: absolute;
  left: var(--arrow-x, 50%);
  bottom: -8px;
  width: 12px;
  height: 12px;
  margin-left: -6px;
  background: #FFF9EC;
  border-right: 2px solid #F4D8A8;
  border-bottom: 2px solid #F4D8A8;
  transform: rotate(45deg);
  border-radius: 0 0 3px 0;
}

.tower-pop.below::after {
  top: -8px;
  bottom: auto;
  border: 0;
  border-left: 2px solid #F4D8A8;
  border-top: 2px solid #F4D8A8;
  border-radius: 3px 0 0 0;
}

@keyframes popIn {
  from { opacity: 0; margin-top: 6px; }
  to { opacity: 1; margin-top: 0; }
}

.tp-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.tp-head b { font-size: 14px; font-weight: 900; }

.tp-level {
  padding: 2px 7px;
  border-radius: 999px;
  background: #FFEAC8;
  color: #C06A15;
  font-size: 9px;
  font-weight: 900;
}

.tp-stats {
  display: flex;
  gap: 12px;
  margin: 4px 0 8px;
  color: #9A7B55;
  font-size: 10px;
  font-weight: 700;
}

.tp-actions { display: flex; gap: 6px; }

.tp-btn {
  flex: 1;
  height: 32px;
  border: 1px solid #EBD5AC;
  border-radius: 11px;
  background: #FFFDF7;
  color: #7E5A2E;
  font-family: inherit;
  font-size: 11px;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.1s ease, filter 0.1s ease;
}

.tp-btn.primary {
  flex: 1.18;
  border-color: #F08B3B;
  background: linear-gradient(180deg, #FFA75D, #FF8332);
  color: #fff;
  box-shadow: 0 2px 0 #D96418;
}

.tp-btn:active { transform: translateY(1px) scale(0.98); }
.tp-btn:disabled { opacity: 0.45; filter: grayscale(0.3); }

/* ---------------- 底部操作台 ---------------- */
.dock {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 12;
  padding: 11px 13px calc(9px + env(safe-area-inset-bottom));
  border-radius: 26px 26px 0 0;
  border-top: 1px solid rgba(255, 255, 255, 0.94);
  background:
    radial-gradient(120% 70% at 15% 0%, rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0) 62%),
    linear-gradient(180deg, #FFF9EA 0%, #FFF2D6 62%, #FFEBC6 100%);
  box-shadow: 0 -9px 28px rgba(85, 49, 9, 0.18), inset 0 2px 0 rgba(255, 255, 255, 0.7);
}

.dock::before {
  content: "";
  position: absolute;
  top: 6px;
  left: 50%;
  width: 36px;
  height: 4px;
  margin-left: -18px;
  border-radius: 999px;
  background: rgba(202, 161, 100, 0.35);
}

.dock-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 48px;
  padding-top: 1px;
}

.wave-copy { min-width: 0; }

.wave-kicker {
  display: block;
  font-size: 9px;
  line-height: 1.2;
  font-weight: 800;
  letter-spacing: 1.3px;
  color: #B88B50;
}

.wave-line {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-top: 1px;
}

.wave-line b {
  font-size: 21px;
  line-height: 1.08;
  font-weight: 900;
  letter-spacing: -0.4px;
  color: #3D2816;
}

.wave-line small {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #9A7850;
  font-size: 10.5px;
  font-weight: 700;
}

.start-btn {
  flex: 0 0 auto;
  min-width: 137px;
  height: 48px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 0 14px;
  border: 0;
  border-radius: 16px;
  background: linear-gradient(180deg, #FFA85E 0%, #FF8433 55%, #FF7A25 100%);
  box-shadow: 0 4px 0 #D86118, 0 8px 16px rgba(213, 94, 20, 0.28), inset 0 1px 0 rgba(255, 255, 255, 0.45);
  color: #FFFDF7;
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 900;
  letter-spacing: 0.2px;
  cursor: pointer;
  transition: transform 0.09s ease, box-shadow 0.09s ease, filter 0.16s ease;
  animation: startGlow 1.8s ease-in-out infinite;
}

.start-btn .mp-icon {
  width: 20px;
  height: 20px;
  filter: drop-shadow(0 1px 0 rgba(140, 55, 7, 0.35));
}

.start-btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #D86118, 0 3px 8px rgba(213, 94, 20, 0.2);
}

.start-btn:disabled {
  animation: none;
  filter: saturate(0.58) brightness(1.04);
  box-shadow: 0 3px 0 #D86118, 0 5px 10px rgba(213, 94, 20, 0.18);
  cursor: default;
}

.start-btn:disabled:active { transform: none; }

@keyframes startGlow {
  0%, 100% { box-shadow: 0 4px 0 #D86118, 0 8px 16px rgba(213, 94, 20, 0.28), inset 0 1px 0 rgba(255, 255, 255, 0.45); }
  50% { box-shadow: 0 4px 0 #D86118, 0 9px 22px rgba(255, 137, 60, 0.46), inset 0 1px 0 rgba(255, 255, 255, 0.45); }
}

.tower-row {
  display: flex;
  align-items: stretch;
  gap: 8px;
  margin-top: 9px;
}

.tower-card {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  height: 78px;
  padding: 3px 2px 5px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 1px;
  border: 2px solid transparent;
  border-radius: 16px;
  background: #FFFDF7;
  box-shadow: 0 2px 0 #EAD7B2;
  color: #553921;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.12s ease, background 0.12s ease, box-shadow 0.12s ease;
}

.tower-card:active { transform: translateY(1px) scale(0.975); }

.tower-card.active {
  border-color: #FF8A3D;
  background: #FFF3DD;
  box-shadow: 0 3px 0 #F2B878, 0 0 0 3px rgba(255, 138, 61, 0.13);
  transform: translateY(-2px);
}

.tc-art {
  width: 38px;
  height: 38px;
  margin-top: 1px;
  display: block;
  pointer-events: none;
}

.tc-art canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.tc-tag {
  position: absolute;
  top: 5px;
  right: 6px;
  padding: 1px 5px;
  border-radius: 999px;
  background: #F4E8D1;
  color: #A5855E;
  font-size: 8px;
  font-weight: 800;
  line-height: 1.4;
}

.tower-card.active .tc-tag { background: #FFE1B9; color: #B5671C; }

.tc-name {
  font-size: 11px;
  line-height: 1.2;
  font-weight: 900;
  white-space: nowrap;
}

.tc-cost {
  display: inline-flex;
  align-items: center;
  gap: 1px;
  font-size: 10px;
  line-height: 1.1;
  font-weight: 900;
  color: #A16A2D;
  font-variant-numeric: tabular-nums;
}

.tc-cost .mp-icon { width: 11px; height: 11px; color: #F5A623; }

.tower-card.broke .tc-cost { color: #C85041; }
.tower-card.broke .tc-cost .mp-icon { color: #D85A49; }

/* ---------------- 结算 ---------------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  background: rgba(45, 28, 12, 0.52);
  -webkit-backdrop-filter: blur(7px);
  backdrop-filter: blur(7px);
  opacity: 0;
  transition: opacity 0.28s ease;
}

.overlay.show { opacity: 1; }

.result-card {
  position: relative;
  width: min(336px, 92vw);
  padding: 0 20px 18px;
  border-radius: 28px;
  border: 2px solid #FFE1B2;
  background:
    radial-gradient(100% 60% at 50% 0%, #FFFDF6 0%, #FFF5E2 68%, #FFEBC9 100%);
  box-shadow: 0 22px 50px rgba(52, 28, 5, 0.34), inset 0 2px 0 #fff;
  text-align: center;
  transform: translateY(12px) scale(0.92);
  transition: transform 0.34s cubic-bezier(0.2, 1.25, 0.35, 1);
}

.overlay.show .result-card { transform: translateY(0) scale(1); }

.result-art {
  width: 132px;
  height: 122px;
  margin: -66px auto -8px;
  filter: drop-shadow(0 8px 10px rgba(128, 62, 12, 0.2));
}

.result-art svg { width: 100%; height: 100%; display: block; }
.result-art .art-sad { display: none; }
.overlay.lose .result-art .art-happy { display: none; }
.overlay.lose .result-art .art-sad { display: block; }

.result-kicker {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  background: #FFE8C2;
  color: #C06A15;
  font-size: 9px;
  font-weight: 900;
  letter-spacing: 2px;
}

.result-card h2 {
  margin: 7px 0 4px;
  font-size: 25px;
  line-height: 1.15;
  font-weight: 900;
  letter-spacing: -0.5px;
  color: #392411;
}

.result-card p {
  margin: 0 auto 13px;
  max-width: 250px;
  color: #937353;
  font-size: 12px;
  line-height: 1.55;
  font-weight: 600;
}

.result-stats {
  display: flex;
  align-items: stretch;
  margin-bottom: 14px;
  padding: 9px 4px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid #F3E2C3;
}

.result-stats div {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.result-stats div + div { border-left: 1px solid #F1E0C1; }

.result-stats b {
  font-size: 20px;
  line-height: 1.15;
  font-weight: 900;
  color: #E27422;
  font-variant-numeric: tabular-nums;
}

.result-stats span { font-size: 9px; color: #A78A66; font-weight: 700; }

.again-btn {
  width: 100%;
  height: 48px;
  border: 0;
  border-radius: 15px;
  background: linear-gradient(180deg, #FFA85E, #FF7C28);
  box-shadow: 0 4px 0 #D86118, 0 8px 15px rgba(213, 94, 20, 0.24);
  color: #fff;
  font-family: inherit;
  font-size: 15px;
  font-weight: 900;
  letter-spacing: 1px;
  cursor: pointer;
  transition: transform 0.09s ease, box-shadow 0.09s ease;
}

.again-btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #D86118;
}

.overlay.lose .result-kicker { background: #E9E4FF; color: #6F5BC2; }
.overlay.lose .result-stats b { color: #7E68C9; }
.overlay.lose .result-art { filter: grayscale(0.25) drop-shadow(0 8px 10px rgba(80, 60, 20, 0.2)); }

/* ---------------- 小屏适配 ---------------- */
@media (max-width: 350px) {
  .hud { padding: 0 10px; }
  .player-chip { max-width: 148px; padding-right: 9px; }
  .player-meta b { max-width: 70px; font-size: 12px; }
  .stat-pill { padding: 0 8px 0 6px; font-size: 13px; }
  .dock { padding-left: 10px; padding-right: 10px; }
  .tower-row { gap: 6px; }
  .tower-card { height: 74px; }
  .start-btn { min-width: 124px; font-size: 12.5px; }
  .wave-line b { font-size: 19px; }
  .tower-pop { width: 194px; }
}

@media (max-height: 700px) {
  .player-chip { height: 44px; }
  .avatar { width: 34px; height: 34px; flex-basis: 34px; }
  .tower-card { height: 72px; }
  .tc-art { width: 33px; height: 33px; }
  .dock-top { min-height: 44px; }
  .start-btn { height: 44px; }
  .hint { top: calc(62px + env(safe-area-inset-top)); }
  .toast { bottom: 186px; }
}
