:root {
  --bg-a: #05070f;
  --bg-b: #101631;
  --bg-c: #1a2450;
  --panel: rgba(10, 14, 31, 0.78);
  --panel-2: rgba(23, 33, 75, 0.55);
  --line: rgba(122, 166, 255, 0.3);
  --line-strong: rgba(145, 201, 255, 0.62);
  --text: #e9f2ff;
  --muted: #9fb7dc;
  --accent: #ffc857;
  --accent-2: #7bf1a8;
  --danger: #ff6b6b;
  --shadow: 0 15px 36px rgba(2, 4, 11, 0.55);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  color: var(--text);
  font-family: "Space Grotesk", "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 20% 20%, rgba(255, 200, 87, 0.2) 0, transparent 32%),
    radial-gradient(circle at 80% 18%, rgba(123, 241, 168, 0.16) 0, transparent 35%),
    linear-gradient(140deg, var(--bg-a), var(--bg-b) 48%, var(--bg-c));
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.04) 1px, transparent 1px);
  background-size: 32px 32px;
  opacity: 0.2;
}

#app.prelaunch {
  opacity: 0;
  pointer-events: none;
}

#titleScreen {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  overflow: hidden;
  background:
    radial-gradient(circle at 20% 22%, rgba(255, 200, 87, 0.2) 0, transparent 34%),
    radial-gradient(circle at 78% 16%, rgba(123, 241, 168, 0.16) 0, transparent 36%),
    linear-gradient(150deg, #04060d, #0f1736 55%, #182347);
}

#victoryScreen {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  overflow: hidden;
  background:
    radial-gradient(circle at 22% 22%, rgba(123, 241, 168, 0.25) 0, transparent 34%),
    radial-gradient(circle at 76% 18%, rgba(255, 200, 87, 0.22) 0, transparent 36%),
    linear-gradient(150deg, #081117, #102739 55%, #21445a);
}

#titleScreen.hidden {
  display: none;
}

#victoryScreen.hidden {
  display: none;
}

.numberRain {
  position: absolute;
  left: -10%;
  right: -10%;
  top: 15%;
  font-family: "Chakra Petch", sans-serif;
  font-size: clamp(0.95rem, 1.5vw, 1.35rem);
  letter-spacing: 0.35em;
  color: rgba(164, 198, 255, 0.23);
  white-space: nowrap;
  animation: number-slide 18s linear infinite;
}

.numberRain.second {
  top: auto;
  bottom: 18%;
  color: rgba(255, 210, 127, 0.22);
  animation-duration: 23s;
  animation-direction: reverse;
}

@keyframes number-slide {
  from {
    transform: translateX(0);
  }
  to {
    transform: translateX(-22%);
  }
}

.titleCard {
  width: min(92vw, 680px);
  border-radius: 18px;
  border: 1px solid rgba(142, 182, 255, 0.4);
  background: linear-gradient(145deg, rgba(10, 17, 36, 0.9), rgba(21, 33, 75, 0.78));
  box-shadow: 0 20px 48px rgba(2, 5, 16, 0.66);
  padding: clamp(20px, 4vw, 34px);
  text-align: center;
}

.victoryCard {
  border-color: rgba(123, 241, 168, 0.56);
  background: linear-gradient(145deg, rgba(8, 29, 29, 0.9), rgba(22, 57, 60, 0.82));
}

.victoryStats {
  margin-top: 16px;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid rgba(149, 226, 191, 0.45);
  background: rgba(5, 19, 20, 0.7);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.victoryStat {
  border-radius: 8px;
  border: 1px solid rgba(149, 226, 191, 0.26);
  background: rgba(13, 34, 36, 0.72);
  padding: 8px;
  text-align: left;
}

.victoryStatLabel {
  display: block;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.7rem;
  color: #a6d7cb;
}

.victoryStatValue {
  display: block;
  margin-top: 3px;
  font-family: "Chakra Petch", sans-serif;
  font-weight: 700;
  font-size: 1.05rem;
  color: #e7fff2;
}

.titleCard .kicker {
  margin: 0;
  color: #92b8f6;
  text-transform: uppercase;
  letter-spacing: 0.22em;
  font-size: 0.72rem;
}

.titleCard h1 {
  margin: 8px 0 12px;
  font-family: "Chakra Petch", sans-serif;
  color: var(--accent);
  letter-spacing: 0.08em;
  font-size: clamp(1.9rem, 4.8vw, 3rem);
}

.titleCard .subtitle {
  margin: 0 auto;
  max-width: 56ch;
  line-height: 1.4;
  color: #d7e6ff;
  font-size: clamp(0.95rem, 1.6vw, 1.05rem);
}

.titleActions {
  margin-top: 18px;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}

.titleActions .toolButton {
  min-width: 170px;
}

#app {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 10px;
  padding: 10px;
}

#topHud {
  display: grid;
  grid-template-columns: minmax(220px, 1.2fr) repeat(4, minmax(110px, 1fr));
  gap: 10px;
}

.card,
.panel,
#bottomHud {
  border-radius: 14px;
  border: 1px solid var(--line);
  background: linear-gradient(145deg, var(--panel), var(--panel-2));
  box-shadow: var(--shadow);
  backdrop-filter: blur(5px);
}

.card {
  padding: 10px 12px;
}

.card.brand h1 {
  margin: 0;
  font-family: "Chakra Petch", sans-serif;
  letter-spacing: 0.06em;
  color: var(--accent);
  font-size: 1.3rem;
}

.card.brand p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.card.stat span {
  display: block;
  text-transform: uppercase;
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
}

.card.stat strong {
  display: block;
  margin-top: 6px;
  font-size: 1.24rem;
}

#stage {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(230px, 280px) 1fr minmax(240px, 290px);
  gap: 10px;
}

.panel {
  min-height: 0;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel h2 {
  margin: 0;
  font-family: "Chakra Petch", sans-serif;
  letter-spacing: 0.05em;
  font-size: 1rem;
}

.buttonGrid {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: auto;
  min-height: 0;
}

.buildSection {
  border: 1px solid rgba(159, 183, 220, 0.22);
  border-radius: 10px;
  padding: 7px;
  background: rgba(8, 12, 26, 0.45);
}

.buildSectionHeader {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 8px;
  align-items: center;
  padding: 4px 2px 6px;
  border: 0;
  background: transparent;
  color: inherit;
  cursor: pointer;
}

.buildSectionTitle {
  margin: 0;
  font-family: "Chakra Petch", sans-serif;
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.buildSectionMeta {
  font-size: 0.72rem;
  color: var(--muted);
  opacity: 0.85;
}

.buildSectionChevron {
  font-family: "Chakra Petch", sans-serif;
  font-weight: 700;
  color: var(--accent);
  min-width: 1ch;
  text-align: center;
}

.buildSection.collapsed {
  padding-bottom: 5px;
}

.buildSectionGrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 6px;
}

.buildBtn,
.toolButton {
  appearance: none;
  border-radius: 10px;
  color: var(--text);
  font: inherit;
}

.buildBtn {
  border: 1px solid rgba(159, 183, 220, 0.36);
  background: rgba(8, 12, 26, 0.84);
  cursor: pointer;
  text-align: left;
  padding: 8px;
  transition: transform 120ms ease, border-color 120ms ease;
}

.buildBtn .name {
  display: block;
  font-weight: 700;
  font-size: 0.92rem;
  line-height: 1.15;
  overflow-wrap: normal;
  word-break: normal;
}

.buildBtn .meta {
  display: block;
  margin-top: 4px;
  font-size: 0.76rem;
  color: var(--muted);
}

.buildBtn:hover {
  transform: translateY(-1px);
  border-color: var(--line-strong);
}

.buildBtn.active {
  border-color: rgba(255, 200, 87, 0.9);
  box-shadow: inset 0 0 0 1px rgba(255, 200, 87, 0.45);
}

.buildBtn.locked {
  opacity: 0.45;
  cursor: not-allowed;
}

.toolButton {
  border: 1px solid rgba(123, 241, 168, 0.5);
  background: linear-gradient(145deg, rgba(34, 83, 67, 0.72), rgba(26, 56, 69, 0.72));
  font-weight: 700;
  cursor: pointer;
  padding: 8px 10px;
}

.toolButton.active {
  border-color: rgba(255, 200, 87, 0.95);
  box-shadow: inset 0 0 0 1px rgba(255, 200, 87, 0.5);
  filter: brightness(1.08);
}

.toolButton:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  filter: none;
}

.toolButton.danger {
  border-color: rgba(255, 107, 107, 0.6);
  background: linear-gradient(145deg, rgba(89, 35, 48, 0.74), rgba(56, 26, 50, 0.74));
}

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

.toolsRow .toolButton {
  flex: 1 1 calc(50% - 4px);
  min-width: 0;
  text-align: center;
}

#canvasWrap {
  min-height: 0;
  border-radius: 18px;
  overflow: hidden;
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}

#gameCanvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  background: radial-gradient(circle at 40% 30%, #1a3369 0%, #0e1737 46%, #091022 100%);
}

.stack {
  max-height: 34vh;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.infoBox,
.unlockItem {
  border-radius: 10px;
  border: 1px solid rgba(159, 183, 220, 0.35);
  background: rgba(8, 12, 26, 0.72);
  padding: 8px 9px;
  font-size: 0.86rem;
  line-height: 1.38;
}

.unlockItem.done {
  border-color: rgba(123, 241, 168, 0.62);
  background: rgba(17, 43, 43, 0.78);
}

#bottomHud {
  padding: 8px 10px;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
}

#hints {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  color: var(--muted);
  font-size: 0.82rem;
}

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

@media (max-width: 1100px) {
  #stage {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(380px, 1fr) auto;
  }

  .panel {
    max-height: 240px;
  }

  #topHud {
    grid-template-columns: 1fr 1fr;
  }

  .card.brand {
    grid-column: span 2;
  }
}

@media (max-width: 680px) {
  #app {
    padding: 6px;
    gap: 6px;
  }

  #topHud {
    gap: 6px;
  }

  #bottomHud {
    grid-template-columns: 1fr;
  }

  .victoryStats {
    grid-template-columns: 1fr;
  }
}
