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

:root {
  --bg: #0a0e1a;
  --panel: #12182b;
  --accent: #00d4ff;
  --accent2: #ff6b35;
  --gold: #ffd700;
  --purple: #b388ff;
  --green: #4ade80;
  --text: #e8ecf4;
  --muted: #8892a8;
  --common: #9ca3af;
  --rare: #3b82f6;
  --epic: #a855f7;
  --legend: #f59e0b;
  --play-margin-top: 50px;
  --play-margin-bottom: 120px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

html, body {
  height: 100%;
  height: 100dvh;
  font-family: "Segoe UI", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

#app { width: 100%; height: 100%; position: relative; }

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.screen.active { display: flex; }

#screen-game {
  padding: 0;
  justify-content: stretch;
  touch-action: none;
  overscroll-behavior: none;
}
#screen-game.active { display: block; }

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  background: #050810;
  touch-action: none;
}

/* 触屏战斗控件 */
.touch-controls {
  display: none;
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}
body.is-touch #screen-game.active .touch-controls {
  display: block;
}

.touch-hint {
  position: absolute;
  left: 50%;
  bottom: calc(88px + var(--safe-bottom));
  transform: translateX(-50%);
  font-size: 0.8rem;
  color: var(--muted);
  pointer-events: none;
  opacity: 0.85;
  transition: opacity 0.5s;
  white-space: nowrap;
}
.touch-hint.hidden { opacity: 0; }

.touch-btn {
  position: absolute;
  pointer-events: auto;
  border: none;
  border-radius: 50%;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.touch-dash {
  right: calc(16px + var(--safe-right));
  bottom: calc(20px + var(--safe-bottom));
  width: 72px;
  height: 72px;
  font-size: 0.95rem;
  color: #0a0e1a;
  background: linear-gradient(135deg, #fbbf24, var(--accent2));
  box-shadow: 0 4px 24px rgba(255, 107, 53, 0.45);
}
.touch-dash:active { transform: scale(0.94); }
.touch-dash.cooldown {
  opacity: 0.45;
  filter: grayscale(0.5);
}
.touch-pause {
  right: calc(16px + var(--safe-right));
  top: calc(52px + var(--safe-top));
  width: 44px;
  height: 44px;
  font-size: 1.1rem;
  color: var(--accent);
  background: rgba(18, 24, 43, 0.85);
  border: 1px solid rgba(0, 212, 255, 0.35);
}

/* 主菜单 */
#screen-menu {
  background:
    radial-gradient(ellipse 80% 50% at 50% 0%, rgba(0, 212, 255, 0.15), transparent),
    radial-gradient(ellipse 60% 40% at 80% 80%, rgba(255, 107, 53, 0.1), transparent),
    var(--bg);
}

.title-wrap { text-align: center; margin-bottom: 3rem; }
.title-wrap h1 {
  font-size: 3.2rem;
  letter-spacing: 0.2em;
  background: linear-gradient(135deg, var(--accent), #7dd3fc);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.subtitle { color: var(--muted); margin-top: 0.5rem; font-size: 1.1rem; }

.menu-buttons { display: flex; flex-direction: column; gap: 1rem; min-width: 220px; }
.high-score { margin-top: 2rem; color: var(--muted); font-size: 0.95rem; }
.high-score span { color: var(--gold); }

.btn {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  font-family: inherit;
  min-height: 44px;
  touch-action: manipulation;
}
@media (hover: hover) {
  .btn:hover { transform: translateY(-2px); }
}
.btn:active { transform: scale(0.98); }
.btn-primary {
  background: linear-gradient(135deg, #0891b2, var(--accent));
  color: #0a0e1a;
  font-weight: 600;
  box-shadow: 0 4px 20px rgba(0, 212, 255, 0.35);
}
.btn-secondary {
  background: var(--panel);
  color: var(--accent);
  border: 1px solid rgba(0, 212, 255, 0.4);
}
.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid rgba(255,255,255,0.1);
}
.btn-tiny { padding: 0.35rem 0.7rem; font-size: 0.8rem; }
.btn-mute, .btn-mute-menu {
  min-width: 44px;
  padding: 0.35rem 0.5rem;
  line-height: 1;
}
.btn-mute.muted, .btn-mute-menu.muted { opacity: 0.65; }
.btn-mute-menu {
  margin-top: 1rem;
  font-size: 0.9rem;
}

/* 机库 */
#screen-hangar {
  display: none;
  flex-direction: column;
  padding: 1rem 1.5rem;
  background: var(--bg);
  overflow: hidden;
}
#screen-hangar.active { display: flex; }

.hangar-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  width: 100%;
  max-width: 1200px;
  margin-bottom: 1rem;
}
.hangar-header h2 { flex: 1; font-size: 1.4rem; }
.credits { color: var(--gold); }

.hangar-body {
  flex: 1;
  display: grid;
  grid-template-columns: 280px 1fr 1fr;
  gap: 1rem;
  width: 100%;
  max-width: 1200px;
  min-height: 0;
  overflow: hidden;
}

.ship-preview-panel {
  background: var(--panel);
  border-radius: 12px;
  padding: 1rem;
  border: 1px solid rgba(0, 212, 255, 0.2);
}
#ship-preview { display: block; width: 100%; border-radius: 8px; background: #080c18; }
.ship-stats { margin-top: 0.75rem; font-size: 0.85rem; line-height: 1.6; color: var(--muted); }
.ship-stats strong { color: var(--accent); }

.equip-slots, .inventory-panel {
  background: var(--panel);
  border-radius: 12px;
  padding: 1rem;
  border: 1px solid rgba(255,255,255,0.06);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.equip-slots h3, .inventory-panel h3 { font-size: 0.95rem; margin-bottom: 0.75rem; }
.hint { font-weight: normal; color: var(--muted); font-size: 0.75rem; }

.slot-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.6rem;
  overflow-y: auto;
}

.equip-slot {
  background: #0d1220;
  border: 2px dashed rgba(255,255,255,0.15);
  border-radius: 8px;
  padding: 0.5rem;
  min-height: 72px;
  cursor: pointer;
  transition: border-color 0.2s;
}
.equip-slot:hover, .equip-slot.selected {
  border-color: var(--accent);
  border-style: solid;
}
.equip-slot .slot-label { font-size: 0.7rem; color: var(--muted); }
.equip-slot .part-name { font-size: 0.85rem; margin-top: 0.25rem; }
.equip-slot.empty .part-name { color: var(--muted); font-style: italic; }

.inventory-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.5rem;
  overflow-y: auto;
  align-content: start;
}

.filter-tabs {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.6rem;
  flex-wrap: wrap;
}
.filter-tabs button {
  padding: 0.3rem 0.6rem;
  font-size: 0.75rem;
  border-radius: 4px;
  border: 1px solid rgba(255,255,255,0.1);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-family: inherit;
}
.filter-tabs button.active {
  background: rgba(0, 212, 255, 0.2);
  color: var(--accent);
  border-color: var(--accent);
}

.part-card {
  background: #0d1220;
  border-radius: 8px;
  padding: 0.5rem;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.15s, transform 0.15s;
}
.part-card:hover { transform: scale(1.02); }
.part-card.equipped { opacity: 0.5; pointer-events: none; }
.part-card.selected { border-color: var(--accent); }

.part-card .rarity-bar { height: 3px; border-radius: 2px; margin-bottom: 0.35rem; }
.rarity-common .rarity-bar { background: var(--common); }
.rarity-rare .rarity-bar { background: var(--rare); }
.rarity-epic .rarity-bar { background: var(--epic); }
.rarity-legend .rarity-bar { background: var(--legend); }

.part-card .name { font-size: 0.8rem; font-weight: 600; }
.part-card .type { font-size: 0.65rem; color: var(--muted); }
.part-card .stats { font-size: 0.65rem; color: var(--muted); margin-top: 0.25rem; }

.hangar-footer {
  display: flex;
  gap: 1rem;
  justify-content: center;
  margin-top: 1rem;
  max-width: 1200px;
  width: 100%;
}

/* HUD */
.hud {
  position: absolute;
  top: 0; left: 0; right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  pointer-events: none;
  background: linear-gradient(180deg, rgba(0,0,0,0.6), transparent);
}
.hud > * { pointer-events: auto; }
.hud-left { display: flex; align-items: center; gap: 0.5rem; font-size: 0.85rem; }
.hp-bar {
  width: 120px;
  height: 8px;
  background: rgba(255,255,255,0.15);
  border-radius: 4px;
  overflow: hidden;
}
#hp-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, #ef4444, var(--green));
  transition: width 0.2s;
}
.hud-center, .hud-right {
  display: flex;
  gap: 1.5rem;
  font-size: 0.9rem;
}
.hud strong { color: var(--accent); }

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
}
.overlay.hidden { display: none; }

.toast {
  position: absolute;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 212, 255, 0.2);
  border: 1px solid var(--accent);
  padding: 0.75rem 1.5rem;
  border-radius: 8px;
  animation: fadeIn 0.3s;
}
.toast.hidden { display: none; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateX(-50%) translateY(10px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* 结算 */
#screen-result { background: rgba(0,0,0,0.85); }
#screen-result h2 { font-size: 2rem; margin-bottom: 1.5rem; }
.result-stats { text-align: center; margin-bottom: 2rem; line-height: 2; }
.result-stats strong { color: var(--gold); font-size: 1.2rem; }
.new-record { color: var(--accent2) !important; font-weight: bold; }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal.hidden { display: none; }
.modal-box {
  background: var(--panel);
  padding: 1.5rem 2rem;
  border-radius: 12px;
  max-width: 420px;
  border: 1px solid rgba(0, 212, 255, 0.3);
}
.modal-box h3 { margin: 1rem 0 0.5rem; color: var(--accent); font-size: 0.95rem; }
.modal-box h3:first-child { margin-top: 0; }
.modal-box ul { margin-left: 1.2rem; line-height: 1.8; color: var(--muted); font-size: 0.9rem; }
.modal-box kbd {
  background: #1e293b;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  font-size: 0.85rem;
}
.modal-box .btn { margin-top: 1rem; width: 100%; }

body:not(.is-touch) .help-mobile { display: none; }
body.is-touch .help-desktop { display: none; }

/* —— 手机 / 平板 —— */
@media (max-width: 900px) {
  .hangar-body {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    overflow: visible;
  }
}

@media (max-width: 768px), (pointer: coarse) {
  :root {
    --play-margin-top: 56px;
    --play-margin-bottom: 108px;
  }

  #screen-menu,
  #screen-result {
    padding: calc(1rem + var(--safe-top)) calc(1rem + var(--safe-right))
      calc(1rem + var(--safe-bottom)) calc(1rem + var(--safe-left));
    justify-content: center;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .title-wrap { margin-bottom: 2rem; }
  .title-wrap h1 { font-size: 2rem; letter-spacing: 0.12em; }

  .menu-buttons {
    width: 100%;
    max-width: 320px;
    padding: 0 0.5rem;
  }
  .menu-buttons .btn { width: 100%; }

  #screen-hangar {
    padding: calc(0.5rem + var(--safe-top)) calc(0.5rem + var(--safe-right))
      calc(0.5rem + var(--safe-bottom)) calc(0.5rem + var(--safe-left));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    justify-content: flex-start;
  }

  .hangar-header {
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
  }
  .hangar-header h2 { font-size: 1.1rem; width: 100%; order: -1; }
  .btn-back { min-height: 40px; padding: 0.5rem 0.75rem; }

  .hangar-body {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    flex: none;
    min-height: auto;
  }

  .ship-preview-panel { padding: 0.75rem; }
  #ship-preview { max-height: 200px; object-fit: contain; }
  .ship-stats { font-size: 0.8rem; }

  .equip-slots,
  .inventory-panel {
    min-height: 0;
    max-height: none;
  }

  .slot-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
  }
  .equip-slot { min-height: 64px; }

  .inventory-grid {
    grid-template-columns: repeat(2, 1fr);
    max-height: 240px;
  }

  .part-card { padding: 0.65rem; min-height: 44px; }

  .hangar-footer {
    flex-direction: column;
    gap: 0.5rem;
    padding-bottom: 0.5rem;
  }
  .hangar-footer .btn { width: 100%; }

  .hud {
    flex-wrap: wrap;
    gap: 0.35rem 0.75rem;
    padding: calc(0.5rem + var(--safe-top)) calc(0.5rem + var(--safe-right)) 0.5rem calc(0.5rem + var(--safe-left));
  }
  .hud-left { flex: 1 1 100%; font-size: 0.75rem; }
  .hp-bar { flex: 1; max-width: 140px; min-width: 80px; }
  .hud-center { font-size: 0.78rem; gap: 0.75rem; }
  .hud-right { font-size: 0.78rem; gap: 0.5rem; }
  body.is-touch .hud-right #btn-pause { display: none; }

  .toast {
    bottom: calc(5.5rem + var(--safe-bottom));
    font-size: 0.85rem;
    max-width: 90%;
    text-align: center;
  }

  .modal-box {
    margin: 1rem;
    max-height: 85dvh;
    overflow-y: auto;
    padding: 1.25rem 1.25rem;
    width: calc(100% - 2rem);
  }

  #screen-result h2 { font-size: 1.5rem; }
  .result-stats { font-size: 0.95rem; }
}

@media (max-width: 380px) {
  .inventory-grid { grid-template-columns: 1fr; }
  .touch-dash { width: 64px; height: 64px; font-size: 0.85rem; }
}
