:root {
  --bg-deep: #06111f;
  --bg-mid: #0e223a;
  --panel: rgba(5, 14, 28, 0.84);
  --panel-border: rgba(115, 214, 255, 0.2);
  --text-main: #eef6ff;
  --text-dim: #9fb8cf;
  --cyan: #66e4ff;
  --gold: #ffd86c;
  --danger: #ff6d6d;
  --lime: #8cffbf;
  --shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at top, rgba(102, 228, 255, 0.18), transparent 32%),
    radial-gradient(circle at 20% 80%, rgba(255, 216, 108, 0.1), transparent 24%),
    linear-gradient(180deg, #040b15 0%, #071526 35%, #040a13 100%);
  color: var(--text-main);
  font-family: "Trebuchet MS", "Noto Sans TC", sans-serif;
}

body {
  display: flex;
  align-items: stretch;
  justify-content: center;
  padding: 20px;
}

.app-shell {
  display: grid;
  width: min(1240px, 100%);
  grid-template-columns: minmax(280px, 360px) minmax(320px, 1fr);
  gap: 20px;
}

.hud-panel,
.canvas-wrap {
  position: relative;
  overflow: hidden;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 28px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.hud-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px;
  font-size: 0.92em;
}

.hud-panel::before,
.canvas-wrap::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(135deg, rgba(102, 228, 255, 0.08), transparent 26%),
    linear-gradient(315deg, rgba(255, 216, 108, 0.08), transparent 22%);
  pointer-events: none;
}

.brand {
  position: relative;
  z-index: 1;
}

.brand-kicker,
.message-tag {
  margin: 0 0 8px;
  letter-spacing: 0.24em;
  font-size: 0.72rem;
  color: var(--cyan);
}

.brand h1,
.message-card h2 {
  margin: 0;
  font-family: Impact, Haettenschweiler, "Arial Narrow Bold", sans-serif;
  letter-spacing: 0.04em;
  line-height: 1;
}

.brand h1 {
  font-size: clamp(1.5rem, 3.2vw, 2.2rem);
}

.brand-copy {
  font-size: 0.84rem;
}

.brand-copy,
.tips-panel p,
.message-card p {
  margin: 0;
  color: var(--text-dim);
  line-height: 1.6;
  white-space: pre-line;
}

.stats-grid {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
}

.stat-card {
  display: grid;
  gap: 4px;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(7, 21, 38, 0.72);
  border: 1px solid rgba(102, 228, 255, 0.12);
}

.stat-card.wide {
  grid-column: 1 / -1;
}

.stat-card span {
  color: var(--text-dim);
  font-size: 0.72rem;
}

.stat-card strong {
  font-size: 1.05rem;
}

.meter {
  width: 100%;
  height: 12px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 999px;
  overflow: hidden;
}

.meter-fill {
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, #8cffbf 0%, #f3ff7b 100%);
  box-shadow: 0 0 18px rgba(140, 255, 191, 0.45);
  transition: width 180ms ease;
}

.leaderboard-panel {
  position: relative;
  z-index: 1;
  display: grid;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.04);
  font-size: 0.82rem;
}

.leaderboard-list li {
  padding: 4px 0;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.panel-heading h2 {
  margin: 0;
  font-size: 1rem;
  letter-spacing: 0;
}

.leaderboard-list {
  display: grid;
  gap: 8px;
  margin: 0;
  padding-left: 22px;
  color: var(--text-dim);
}

.leaderboard-list li {
  padding: 8px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.leaderboard-list strong {
  color: var(--text-main);
}

.boss-stat[hidden] {
  display: none;
}

.boss-meter .meter-fill {
  background: linear-gradient(90deg, #ff6d6d 0%, #ffd86c 100%);
  box-shadow: 0 0 18px rgba(255, 109, 109, 0.4);
}

.install-button,
.primary-button,
.bomb-button,
.sound-button,
.focus-button,
.pause-button {
  border: 0;
  cursor: pointer;
  color: var(--text-main);
  font-family: inherit;
}

.install-button,
.primary-button {
  position: relative;
  z-index: 1;
  padding: 10px 14px;
  border-radius: 999px;
  background: linear-gradient(90deg, #116fb9 0%, #1aa5d4 100%);
  font-size: 0.92rem;
  font-weight: 700;
  transition: transform 180ms ease, filter 180ms ease;
}

.install-button:hover,
.primary-button:hover,
.bomb-button:hover,
.sound-button:hover {
  transform: translateY(-2px);
  filter: brightness(1.08);
}

.sound-button {
  flex: 0 0 auto;
  min-height: 36px;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(102, 228, 255, 0.12);
  border: 1px solid rgba(102, 228, 255, 0.18);
  font-size: 0.82rem;
  font-weight: 700;
}

.difficulty-select {
  min-height: 36px;
  margin-left: auto;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid rgba(102, 228, 255, 0.2);
  background: rgba(3, 10, 18, 0.82);
  color: var(--text-main);
  font: inherit;
}

.game-stage {
  min-width: 0;
}

.canvas-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: min(86vh, 920px);
  padding: max(14px, env(safe-area-inset-top))
    max(14px, env(safe-area-inset-right))
    max(14px, env(safe-area-inset-bottom))
    max(14px, env(safe-area-inset-left));
}

#gameCanvas {
  width: min(100%, 520px);
  max-height: calc(100vh - 56px);
  aspect-ratio: 3 / 5;
  border-radius: 24px;
  background:
    linear-gradient(180deg, rgba(35, 86, 138, 0.25) 0%, rgba(3, 9, 18, 0.85) 100%),
    #02070f;
  box-shadow: inset 0 0 50px rgba(0, 0, 0, 0.35);
  touch-action: none;
}

.overlay {
  position: absolute;
  z-index: 2;
  display: flex;
  gap: 10px;
}

.top-left {
  top: 18px;
  left: 18px;
}

.top-right {
  top: 18px;
  right: 18px;
}

.bottom-left {
  left: 18px;
  bottom: 110px;
}

.mini-chip {
  padding: 10px 14px;
  border-radius: 999px;
  background: rgba(4, 12, 23, 0.84);
  border: 1px solid rgba(102, 228, 255, 0.16);
  font-size: 0.84rem;
  backdrop-filter: blur(12px);
}

.mini-chip strong {
  color: var(--gold);
}

.mini-chip.accent {
  color: var(--lime);
}

.combo-chip {
  background: rgba(255, 216, 108, 0.18);
  border-color: rgba(255, 216, 108, 0.4);
}

.combo-chip strong {
  color: #ffd86c;
}

.combo-chip em {
  font-style: normal;
  color: var(--text-dim);
  margin-left: 6px;
}

.message-card {
  position: relative;
  z-index: 1;
  display: grid;
  gap: 6px;
  padding: 12px 14px;
  border-radius: 16px;
  background: rgba(3, 10, 18, 0.92);
  border: 1px solid rgba(102, 228, 255, 0.18);
  max-height: 60vh;
  overflow-y: auto;
  font-size: 0.86rem;
}

.message-card[hidden] {
  display: none;
}

.menu-card h2 {
  font-size: clamp(0.95rem, 2vw, 1.15rem);
}

.message-card p {
  font-size: 0.78rem;
  line-height: 1.45;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 4px 0 6px;
}

.tab-button {
  flex: 1 1 auto;
  min-width: 48px;
  padding: 5px 9px;
  border: 1px solid rgba(102, 228, 255, 0.18);
  border-radius: 999px;
  background: rgba(102, 228, 255, 0.08);
  color: var(--text-dim);
  font-size: 0.74rem;
  cursor: pointer;
}

.tab-button.is-active {
  background: rgba(102, 228, 255, 0.25);
  color: var(--text-main);
  border-color: rgba(102, 228, 255, 0.5);
}

.tab-pane {
  display: none;
  gap: 10px;
}

.tab-pane.is-active {
  display: grid;
}

.row-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-dim);
  font-size: 0.9rem;
}

.row-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.hint {
  color: var(--text-dim);
  font-size: 0.78rem;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
}

.card-tile {
  padding: 12px;
  border-radius: 14px;
  background: rgba(7, 21, 38, 0.7);
  border: 1px solid rgba(102, 228, 255, 0.18);
  cursor: pointer;
  text-align: left;
  color: var(--text-main);
}

.card-tile.is-active {
  border-color: var(--gold);
  box-shadow: 0 0 0 2px rgba(255, 216, 108, 0.25);
}

.card-tile.is-locked {
  opacity: 0.55;
  cursor: not-allowed;
}

.card-tile h3 {
  margin: 0 0 4px;
  font-size: 1rem;
}

.card-tile small {
  display: block;
  color: var(--text-dim);
  line-height: 1.4;
}

.card-tile .cost {
  display: inline-block;
  margin-top: 6px;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(255, 216, 108, 0.18);
  color: var(--gold);
  font-size: 0.78rem;
  font-weight: 700;
}

.shop-currency {
  color: var(--text-dim);
  margin: 0 0 6px;
}

.shop-currency strong {
  color: var(--gold);
  margin: 0 6px;
  font-size: 1.2rem;
}

.ach-list {
  display: grid;
  gap: 6px;
  max-height: 220px;
  overflow-y: auto;
}

.ach-row {
  display: grid;
  grid-template-columns: 28px 1fr;
  gap: 8px;
  align-items: start;
  padding: 8px;
  border-radius: 10px;
  background: rgba(7, 21, 38, 0.7);
  border: 1px solid rgba(102, 228, 255, 0.12);
}

.ach-row.is-unlocked {
  border-color: rgba(140, 255, 191, 0.4);
  background: rgba(140, 255, 191, 0.12);
}

.ach-row .ach-icon {
  font-weight: 900;
  color: var(--gold);
  font-size: 1.1rem;
}

.ach-row strong {
  display: block;
  font-size: 0.92rem;
}

.ach-row small {
  display: block;
  color: var(--text-dim);
  font-size: 0.76rem;
  margin-top: 2px;
}

textarea {
  width: 100%;
  resize: vertical;
  background: rgba(7, 21, 38, 0.7);
  color: var(--text-main);
  border: 1px solid rgba(102, 228, 255, 0.3);
  border-radius: 10px;
  padding: 8px;
  font-family: inherit;
  font-size: 0.84rem;
}

.bomb-button {
  position: absolute;
  right: 24px;
  bottom: 24px;
  z-index: 4;
  width: 88px;
  height: 88px;
  border-radius: 50%;
  background:
    radial-gradient(circle at 35% 30%, rgba(255, 255, 255, 0.42), transparent 24%),
    linear-gradient(180deg, #ff7c57 0%, #d3192e 100%);
  font-weight: 900;
  letter-spacing: 0.08em;
  box-shadow:
    0 10px 35px rgba(212, 25, 46, 0.4),
    inset 0 -10px 24px rgba(71, 0, 0, 0.28);
}

.focus-button {
  position: absolute;
  right: 124px;
  bottom: 32px;
  z-index: 4;
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: linear-gradient(180deg, #5cb1ff 0%, #1a4cd4 100%);
  font-weight: 900;
  letter-spacing: 0.06em;
  box-shadow: 0 10px 30px rgba(26, 76, 212, 0.4);
}

.focus-button.is-active {
  background: linear-gradient(180deg, #ffd86c 0%, #d4861a 100%);
  box-shadow: 0 10px 30px rgba(212, 134, 26, 0.45);
}

.pause-button {
  position: absolute;
  left: 24px;
  bottom: 28px;
  z-index: 4;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: rgba(8, 18, 32, 0.84);
  border: 1px solid rgba(102, 228, 255, 0.3);
  font-weight: 900;
  font-size: 1rem;
  letter-spacing: 0.1em;
}

body[data-scene="menu"] .bomb-button,
body[data-scene="menu"] .focus-button,
body[data-scene="menu"] .pause-button,
body[data-scene="name-entry"] .bomb-button,
body[data-scene="name-entry"] .focus-button,
body[data-scene="name-entry"] .pause-button {
  display: none;
}

.game-overlay[hidden] {
  display: none;
}

.game-overlay {
  position: absolute;
  inset: 24px;
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(3, 10, 18, 0.92);
  border-radius: 22px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  padding: 24px;
  text-align: center;
}

.game-overlay h2 {
  margin: 0;
  font-family: Impact, Haettenschweiler, "Arial Narrow Bold", sans-serif;
  letter-spacing: 0.04em;
}

.game-overlay input {
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid rgba(102, 228, 255, 0.3);
  background: rgba(7, 21, 38, 0.7);
  color: var(--text-main);
  font-size: 1.1rem;
  text-align: center;
  font-family: inherit;
}

.game-overlay .overlay-tag {
  margin: 0;
  font-size: 0.78rem;
  letter-spacing: 0.32em;
  color: rgba(255, 216, 108, 0.9);
}

.continue-overlay h2 {
  color: #ff9a62;
  font-size: 32px;
}

.continue-overlay .continue-timer {
  margin: 4px 0 8px;
  color: #66e4ff;
  font-size: 0.95rem;
  letter-spacing: 0.05em;
}

.continue-overlay .continue-timer strong {
  font-size: 1.4rem;
  color: #ffd86c;
}

.stage-clear-overlay h2 {
  color: #ffd86c;
  font-size: 36px;
  text-shadow: 0 0 18px rgba(255, 216, 108, 0.5);
}

.stage-clear-overlay p {
  color: #d0e8ff;
  font-size: 0.95rem;
}

.weapon-slots {
  display: flex;
  gap: 4px;
  margin-top: 6px;
  pointer-events: auto;
  flex-wrap: wrap;
}

.weapon-slot {
  padding: 3px 8px;
  font-size: 11px;
  font-weight: 700;
  background: rgba(15, 30, 52, 0.6);
  color: rgba(255, 255, 255, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
  letter-spacing: 0.05em;
  user-select: none;
  transition: all 0.12s ease;
}

.weapon-slot.is-unlocked {
  color: #fff;
  background: rgba(15, 30, 52, 0.85);
  border-color: rgba(255, 255, 255, 0.28);
}

.weapon-slot.is-active {
  background: #66e4ff;
  color: #051624;
  border-color: #66e4ff;
  box-shadow: 0 0 12px rgba(102, 228, 255, 0.5);
}

.leaderboard-tabs {
  display: flex;
  gap: 4px;
  margin: 0 0 6px;
}

.lb-tab {
  flex: 1;
  padding: 4px 8px;
  font-size: 11px;
  font-weight: 700;
  background: rgba(15, 30, 52, 0.4);
  color: rgba(255, 255, 255, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  letter-spacing: 0.05em;
}

.lb-tab.is-active {
  background: rgba(102, 228, 255, 0.18);
  color: #fff;
  border-color: rgba(102, 228, 255, 0.55);
}

.lock-tag {
  color: #ff8a8a;
  font-size: 0.78rem;
}

.card-tile.is-locked {
  opacity: 0.55;
  cursor: not-allowed;
}

.replay-slots {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 8px;
  margin-bottom: 8px;
}

.replay-slot {
  display: flex;
  gap: 8px;
  padding: 6px;
  background: rgba(15, 30, 52, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  color: var(--text-main);
  align-items: center;
}

.replay-slot.is-active {
  border-color: rgba(102, 228, 255, 0.7);
  background: rgba(102, 228, 255, 0.12);
}

.replay-slot.is-empty {
  opacity: 0.45;
  cursor: default;
}

.replay-slot img {
  width: 36px;
  height: 60px;
  border-radius: 4px;
  object-fit: cover;
  background: #040810;
  flex: none;
}

.replay-slot .placeholder {
  width: 36px;
  height: 60px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.06);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: rgba(255, 255, 255, 0.3);
  flex: none;
}

.replay-slot .meta {
  display: flex;
  flex-direction: column;
  gap: 1px;
  font-size: 11px;
  line-height: 1.25;
  min-width: 0;
}

.replay-slot .meta strong {
  color: #ffd86c;
  font-size: 12px;
}

@media (max-width: 980px) {
  body {
    padding: 14px;
  }

  .app-shell {
    grid-template-columns: 1fr;
  }

  .hud-panel {
    order: 2;
  }

  .canvas-wrap {
    min-height: auto;
  }

  #gameCanvas {
    max-height: none;
  }

  /* On mobile, when in menu / name-entry, hide the canvas-wrap entirely so the
     menu (settings + start button) is the only thing the player sees. This is
     simpler and more reliable than the position:fixed modal approach which can
     fail on hud-panel due to its backdrop-filter creating a containing block. */
  body[data-scene="menu"] .canvas-wrap,
  body[data-scene="name-entry"] .canvas-wrap {
    display: none;
  }

  body[data-scene="menu"] .hud-panel,
  body[data-scene="name-entry"] .hud-panel {
    /* backdrop-filter creates a containing block for fixed descendants — drop it
       in menu scenes so any future fixed children (toasts etc.) escape correctly. */
    backdrop-filter: none;
  }

  body[data-scene="menu"] .message-card,
  body[data-scene="name-entry"] .message-card {
    position: relative;
    inset: auto;
    max-height: none;
    z-index: auto;
    box-shadow: none;
  }

  /* Hide canvas-side mini-chips while in menu — score/combo/etc. aren't meaningful then. */
  body[data-scene="menu"] .overlay,
  body[data-scene="name-entry"] .overlay {
    display: none;
  }
}

@media (max-width: 680px) {
  body {
    padding: 0;
  }

  .app-shell {
    gap: 0;
    width: 100%;
  }

  .canvas-wrap,
  .hud-panel {
    border-radius: 0;
    border-left: 0;
    border-right: 0;
  }

  .canvas-wrap {
    min-height: 100svh;
  }

  #gameCanvas {
    width: 100%;
    border-radius: 0;
  }

  .message-card {
    inset: auto 14px 14px;
  }

  /* (display:none on canvas-wrap inherited from the 980px @media block —
     no extra rules needed at 680px since menu is already free of canvas.) */

  .bomb-button {
    right: 18px;
    bottom: 18px;
    width: 80px;
    height: 80px;
  }

  .focus-button {
    right: 108px;
    bottom: 24px;
    width: 64px;
    height: 64px;
  }

  .pause-button {
    left: 18px;
    bottom: 22px;
    width: 52px;
    height: 52px;
  }
}
