:root {
  --bg: #0b110d;
  --primary: #93c47d;
  --accent: #d6b45a;
  --danger: #c98354;
  --success: #b7d78a;
  --gold: #ffd166;
  --text: #d9e0d2;
  --pixel: 2px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%; height: 100%;
  background: radial-gradient(circle at 50% 20%, #101710 0%, var(--bg) 70%);
  font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
  color: var(--text);
  overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  touch-action: none;
  -webkit-user-select: none; user-select: none;
}

#game-wrap {
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  width: 100%; max-width: 1300px;
}

#top-controls {
  position: fixed; top: max(12px, env(safe-area-inset-top)); right: max(12px, env(safe-area-inset-right));
  display: flex; gap: 8px; z-index: 20;
}
.icon-btn, .close-btn {
  width: 48px; height: 48px; min-width: 44px; min-height: 44px;
  border: 1px solid rgba(147,196,125,.55); border-radius: 8px;
  color: var(--primary); background: rgba(15,25,18,.88); font: 700 20px monospace;
  cursor: pointer; box-shadow: 0 0 12px rgba(147,196,125,.14);
}
.icon-btn:hover, .icon-btn:focus-visible { background: rgba(147,196,125,.16); }
.icon-btn.muted { color: #8c958c; border-color: #626a62; }

/* ========== HUD ========== */
#hud {
  width: min(96vw, 1260px);
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 16px; letter-spacing: 1px; font-weight: 600;
  background: rgba(16, 25, 17, 0.72);
  border: 1px solid rgba(147, 196, 125, 0.25);
  border-radius: 8px;
  text-shadow: 0 0 8px var(--primary);
  flex-wrap: wrap; gap: 6px;
}
.hud-group { display: flex; gap: 14px; flex-wrap: wrap; }
.hud-group.center { color: var(--gold); }
.hud-group.right { color: var(--danger); }
#hud-score { color: var(--primary); }
#hud-wave  { color: var(--success); }
#hud-mode, #hud-diff { font-size: 0.85em; opacity: 0.85; }
#hud-enemies { color: var(--accent); }

.hidden { display: none !important; }

/* ========== Boss 血条 ========== */
#boss-bar-wrap {
  width: min(96vw, 1260px);
  display: flex; align-items: center; gap: 14px;
  padding: 6px 14px;
  background: rgba(25, 29, 22, 0.72);
  border: 1px solid rgba(214, 180, 90, 0.5);
  border-radius: 8px;
}
#boss-name { color: var(--accent); font-weight: 700; letter-spacing: 2px; text-shadow: 0 0 10px var(--accent); white-space: nowrap; }
#boss-bar { flex: 1; height: 14px; background: #1b1e19; border: 1px solid var(--accent); border-radius: 7px; overflow: hidden; }
#boss-bar-fill { height: 100%; width: 100%; background: linear-gradient(90deg, #b27b56, var(--accent)); box-shadow: 0 0 12px var(--accent); transition: width 0.15s linear; }

/* ========== Canvas ========== */
#game {
  position: relative;
  width: min(96vw, 1260px);
  aspect-ratio: 1260 / 740;
  border: 1px solid rgba(147, 196, 125, 0.35);
  border-radius: 10px;
  box-shadow: 0 0 30px rgba(147, 196, 125, 0.15), inset 0 0 20px rgba(147, 196, 125, 0.05);
  overflow: hidden;
}
#canvas { display: block; width: 100%; height: 100%; image-rendering: pixelated; }

/* 手机竖屏时提示旋转；游戏本身不强行 CSS 旋转，避免坐标系和触控坐标错位。 */
.orientation-lock {
  position: fixed; inset: 0; z-index: 40;
  display: flex; align-items: center; justify-content: center;
  padding: 20px; background: rgba(7, 12, 8, .96);
}
.orientation-card {
  width: min(86vw, 360px); padding: 24px 20px; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  border: 1px solid rgba(147,196,125,.55); border-radius: 12px;
  background: #101a12; color: var(--text);
}
.orientation-card strong { color: var(--gold); font-size: 20px; }
.orientation-card span { color: #aeb9a9; font-size: 13px; line-height: 1.5; }
.orientation-icon { color: var(--primary); font: 700 48px/1 monospace; }
.orientation-card button { min-height: 44px; padding: 8px 22px; border: 0; border-radius: 6px; color: #172016; background: var(--primary); font-weight: 700; }

/* ========== 覆盖层 ========== */
.overlay {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(7, 12, 8, 0.88);
  backdrop-filter: blur(6px);
  z-index: 10;
}
.panel {
  text-align: center; padding: 36px 52px;
  border: 1px solid rgba(147, 196, 125, 0.48);
  border-radius: 14px;
  background: rgba(13, 22, 15, 0.96);
  box-shadow: 0 0 40px rgba(147, 196, 125, 0.16);
  max-width: 520px; width: 90vw;
}
.title {
  font-size: clamp(28px, 5vw, 44px); letter-spacing: 6px;
  color: var(--primary);
  text-shadow: 0 0 18px var(--primary), 0 0 40px var(--primary);
}
.subtitle { margin-top: 8px; color: var(--accent); letter-spacing: 8px; font-size: 14px; }
.author { margin-top: 6px; color: var(--gold); font-size: 18px; letter-spacing: 10px; font-weight: 700; }
.desc { margin: 16px 0 10px; color: var(--text); opacity: 0.85; font-size: 14px; }

/* ========== 大厅选项 ========== */
.lobby-options { margin: 18px 0 8px; display: flex; flex-direction: column; gap: 14px; }
.option-group { display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: wrap; }
.opt-label { font-size: 14px; color: var(--primary); letter-spacing: 2px; min-width: 70px; }
.opt-btns { display: flex; gap: 8px; }
.opt-btn {
  padding: 7px 16px; font-size: 13px; letter-spacing: 1px;
  color: var(--text); background: rgba(28, 39, 29, 0.86);
  border: 1px solid rgba(147, 196, 125, 0.3); border-radius: 6px;
  cursor: pointer; transition: all 0.15s;
}
.opt-btn:hover { border-color: var(--primary); background: rgba(147, 196, 125, 0.1); }
.opt-btn.active {
  color: #10200f; background: var(--primary); font-weight: 700;
  box-shadow: 0 0 12px rgba(147, 196, 125, 0.6);
  border-color: var(--primary);
}

/* ========== 按钮 ========== */
.btn {
  margin-top: 14px; padding: 12px 44px;
  font-size: 18px; letter-spacing: 4px;
  color: #021018; font-weight: 700;
  background: var(--primary); border: none; border-radius: 8px;
  cursor: pointer;
  box-shadow: 0 0 18px rgba(147, 196, 125, 0.7);
  transition: transform 0.12s, box-shadow 0.12s;
}
.btn.secondary { color: var(--text); background: #303b31; box-shadow: none; }
.btn:hover { transform: translateY(-2px); box-shadow: 0 0 28px rgba(147, 196, 125, 0.9); }
.ctrl { margin-top: 14px; font-size: 12px; color: var(--text); opacity: 0.5; }

/* ========== 横幅 ========== */
.banner {
  position: fixed; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  pointer-events: none; z-index: 9;
}
#banner-main {
  font-size: clamp(36px, 6vw, 56px); font-weight: 700; letter-spacing: 8px;
  color: var(--primary); text-shadow: 0 0 24px var(--primary);
  animation: pulse 0.8s ease-in-out infinite alternate;
}
#banner-main.warn { color: var(--accent); text-shadow: 0 0 24px var(--accent); }
#banner-sub { margin-top: 8px; color: var(--text); letter-spacing: 4px; font-size: 18px; }
@keyframes pulse { from { opacity: 0.55; transform: scale(0.96); } to { opacity: 1; transform: scale(1.04); } }

/* ========== 关卡开场动画 ========== */
.level-intro {
  position: fixed; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: rgba(7, 12, 8, 0.9);
  z-index: 11; pointer-events: none;
}
#level-intro-text {
  font-size: clamp(48px, 8vw, 72px); font-weight: 700; letter-spacing: 10px;
  color: var(--primary);
  text-shadow: 0 0 30px var(--primary), 0 0 60px var(--primary);
  animation: levelZoom 2.2s ease-out forwards;
}
#level-intro-sub {
  margin-top: 12px; font-size: clamp(18px, 3vw, 28px);
  color: var(--gold); letter-spacing: 6px;
  animation: levelFade 2.2s ease-out forwards;
}
@keyframes levelZoom {
  0%   { transform: scale(3); opacity: 0; }
  30%  { transform: scale(1); opacity: 1; }
  80%  { transform: scale(1); opacity: 1; }
  100% { transform: scale(0.95); opacity: 0; }
}
@keyframes levelFade {
  0%   { opacity: 0; }
  25%  { opacity: 1; }
  80%  { opacity: 1; }
  100% { opacity: 0; }
}

/* ========== 触控 ========== */
#touch-controls {
  position: fixed; bottom: 20px; left: 0; right: 0;
  display: none; justify-content: space-between; align-items: flex-end;
  padding: 0 max(18px, 3vw) max(16px, env(safe-area-inset-bottom)); z-index: 5; pointer-events: none;
}
#touch-controls.touch-ready { display: flex; }
#touch-controls > * { pointer-events: auto; }

#joystick { position: relative; width: clamp(104px, 16vw, 160px); height: clamp(104px, 16vw, 160px); touch-action: none; }
#joystick-base {
  position: absolute; inset: 0;
  border: 2px solid rgba(147, 196, 125, 0.4);
  border-radius: 50%; background: rgba(10, 18, 32, 0.5);
}
#joystick-knob {
  position: absolute;
  width: 44px; height: 44px;
  top: 50%; left: 50%; transform: translate(-50%, -50%);
  border-radius: 50%;
  background: radial-gradient(circle, var(--primary), rgba(147, 196, 125, 0.4));
  box-shadow: 0 0 12px var(--primary);
  transition: transform 0.06s;
}

#fire-btn {
  width: clamp(76px, 12vw, 124px); height: clamp(76px, 12vw, 124px); min-width: 76px; min-height: 76px;
  border-radius: 50%;
  background: radial-gradient(circle, #b27b56, rgba(178, 123, 86, 0.4));
  border: 2px solid rgba(201, 131, 84, 0.6);
  box-shadow: 0 0 18px rgba(201, 131, 84, 0.35);
  color: #fff; font-weight: 700; font-size: 14px; letter-spacing: 2px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; touch-action: none; user-select: none; -webkit-user-select: none;
}

.warehouse-entry { width: 100%; min-height: 48px; margin-top: 2px; color: var(--gold); background: #263126; border: 1px solid rgba(214,180,90,.45); border-radius: 7px; font-weight: 700; cursor: pointer; }
.warehouse-entry span { float: right; color: var(--text); font-weight: 400; }
.warehouse-panel { position: relative; max-width: 620px; max-height: 86vh; overflow: auto; }
.close-btn { position: absolute; top: 12px; right: 12px; border-color: rgba(201,131,84,.45); color: var(--danger); }
.warehouse-title { color: var(--gold); letter-spacing: 3px; }
.warehouse-balance { margin: 10px 0 16px; color: var(--text); }
.warehouse-tabs { display: flex; gap: 8px; margin-bottom: 12px; }
.warehouse-tab { flex: 1; min-height: 44px; color: var(--text); background: #202b22; border: 1px solid #516051; border-radius: 6px; cursor: pointer; }
.warehouse-tab.active { color: #172016; background: var(--primary); border-color: var(--primary); font-weight: 700; }
.warehouse-list { display: grid; gap: 8px; text-align: left; }
.warehouse-card { display: grid; grid-template-columns: 46px 1fr auto; align-items: center; gap: 10px; padding: 8px; border: 1px solid #3d4b3d; background: rgba(31,44,33,.75); border-radius: 7px; }
.warehouse-card.equipped { border-color: var(--gold); box-shadow: inset 0 0 0 1px rgba(214,180,90,.25); }
.item-icon { width: 42px; height: 42px; display: grid; place-items: center; color: var(--gold); background: #151e17; border: 1px solid #687268; font: 700 22px monospace; }
.item-info { display: flex; flex-direction: column; gap: 4px; }
.item-info strong { color: var(--text); }
.item-info small { color: #9aa99a; font-size: 12px; }
.item-action { min-width: 84px; min-height: 44px; color: #172016; background: var(--gold); border: 0; border-radius: 6px; font-weight: 700; cursor: pointer; }
.item-action.owned { color: var(--text); background: #4a5b4b; }
.warehouse-tip { margin-top: 14px; color: #94a394; font-size: 12px; }
.confirm-actions { display: flex; justify-content: center; gap: 10px; }
.confirm-actions .btn { margin-top: 8px; min-width: 120px; }

/* ========== 移动端适配 ========== */
@media (max-width: 768px) {
  #hud { padding: 6px 10px; font-size: 13px; gap: 4px; }
  .hud-group { gap: 8px; }
  .panel { padding: 24px 20px; }
  .title { letter-spacing: 3px; }
  .opt-btn { padding: 6px 12px; font-size: 12px; }
  .btn { padding: 10px 32px; font-size: 16px; }
  #top-controls { top: max(8px, env(safe-area-inset-top)); right: max(8px, env(safe-area-inset-right)); gap: 4px; }
  .icon-btn { width: 44px; height: 44px; font-size: 17px; }
  .warehouse-card { grid-template-columns: 40px 1fr; }
  .item-action { grid-column: 2; justify-self: stretch; }
}

@media (hover: none) and (pointer: coarse) {
  /* 触屏显示状态由 JS 直接控制，兼容 Chrome/Safari/微信内置浏览器。 */
  .ctrl { display: none; }
}

@media (orientation: portrait) and (max-width: 900px) and (hover: none) and (pointer: coarse) {
  #game-wrap { visibility: hidden; }
  #top-controls { visibility: hidden; }
}

@media (orientation: landscape) and (max-height: 520px) {
  #game-wrap { gap: 4px; }
  #game { width: min(96vw, 150dvh); }
  #hud { padding: 4px 10px; font-size: 12px; }
  #touch-controls { padding-bottom: max(8px, env(safe-area-inset-bottom)); }
}
