:root {
  color-scheme: dark;
  --bg: #17211b;
  --panel: #223127;
  --panel-2: #2b3d30;
  --ink: #f4ead0;
  --muted: #c6b989;
  --line: #536146;
  --gold: #d8a93c;
  --green: #76a85d;
  --red: #cf644f;
  --blue: #6fb4c7;
  --violet: #9d7bd6;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at top left, rgba(216, 169, 60, 0.18), transparent 34rem),
    linear-gradient(135deg, #111915 0%, #213226 58%, #141b18 100%);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  min-height: 42px;
  border: 1px solid #6b6744;
  border-radius: 6px;
  padding: 0 14px;
  background: #334633;
  color: var(--ink);
  cursor: pointer;
  transition: transform 120ms ease, border-color 120ms ease, background 120ms ease;
}

button:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: var(--gold);
  background: #40563d;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

button.primary {
  background: linear-gradient(180deg, #d4a641, #a77525);
  border-color: #f0ce6b;
  color: #21170b;
  font-weight: 800;
}

button.ghost {
  background: transparent;
}

.button-link {
  display: inline-grid;
  min-height: 42px;
  align-items: center;
  border: 1px solid #6b6744;
  border-radius: 6px;
  padding: 0 14px;
  color: var(--ink);
  text-decoration: none;
}

input {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 12px;
  background: #111a15;
  color: var(--ink);
}

.app {
  min-height: 100vh;
}

.screen {
  display: none;
  min-height: 100vh;
  padding: 24px;
}

.screen.active {
  display: block;
}

body.start-map-select #screen-select .back-menu {
  display: none;
}

#screen-menu {
  align-content: center;
  max-width: 1000px;
  margin: 0 auto;
}

.brand {
  display: grid;
  grid-template-columns: 104px 1fr;
  gap: 24px;
  align-items: center;
}

.crest {
  display: grid;
  width: 104px;
  aspect-ratio: 1;
  place-items: center;
  border: 3px solid var(--gold);
  border-radius: 8px;
  background: linear-gradient(145deg, #304833, #1b271f);
  box-shadow: inset 0 0 0 8px rgba(216, 169, 60, 0.12);
  color: var(--gold);
  font-size: 2rem;
  font-weight: 900;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 10px;
  font-size: clamp(2.2rem, 5vw, 4.4rem);
  line-height: 1;
}

h2 {
  margin-bottom: 14px;
  font-size: 1.25rem;
}

p {
  color: var(--muted);
  line-height: 1.6;
}

.menu-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(180px, 1fr));
  gap: 14px;
  max-width: 620px;
  margin-top: 36px;
}

.topbar,
.hud {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 16px;
}

.topbar h2 {
  flex: 1;
  margin: 0;
}

.generator-grid {
  display: grid;
  grid-template-columns: minmax(280px, 380px) 1fr;
  gap: 18px;
}

.panel,
.toolbox {
  border: 1px solid rgba(216, 169, 60, 0.28);
  border-radius: 8px;
  background: rgba(34, 49, 39, 0.9);
  box-shadow: 0 18px 42px rgba(0, 0, 0, 0.28);
}

.panel {
  padding: 16px;
}

#seed-form {
  display: grid;
  gap: 14px;
  align-content: start;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 0.93rem;
}

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

.preview-panel {
  display: grid;
  min-width: 0;
  place-items: center;
}

canvas {
  display: block;
  max-width: 100%;
  border: 1px solid #6b6744;
  border-radius: 8px;
  background: #1b291f;
}

.map-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}

.map-card {
  display: grid;
  gap: 10px;
  border: 1px solid rgba(216, 169, 60, 0.25);
  border-radius: 8px;
  padding: 14px;
  background: rgba(34, 49, 39, 0.9);
}

.map-card h3 {
  margin: 0;
}

.map-card small {
  color: var(--muted);
  overflow-wrap: anywhere;
}

.hud {
  flex-wrap: wrap;
}

.stat {
  display: grid;
  min-width: 92px;
  min-height: 52px;
  align-content: center;
  border: 1px solid rgba(216, 169, 60, 0.25);
  border-radius: 6px;
  padding: 6px 10px;
  background: rgba(16, 25, 20, 0.72);
}

.stat span {
  color: var(--muted);
  font-size: 0.75rem;
}

.stat strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.game-shell {
  display: grid;
  grid-template-columns: 190px minmax(360px, 1fr) 220px;
  gap: 14px;
  align-items: start;
}

.toolbox,
.upgrade-panel {
  position: sticky;
  top: 12px;
}

.toolbox {
  display: grid;
  gap: 10px;
  padding: 14px;
}

.tower-choice {
  display: grid;
  min-height: 64px;
  gap: 3px;
  justify-items: start;
  text-align: left;
}

.tower-choice.selected {
  border-color: var(--gold);
  background: #4b5635;
}

.tower-choice span,
#selected-info {
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.35;
}

.upgrade-panel {
  display: grid;
  gap: 10px;
  align-content: start;
}

.icon-button {
  display: inline-grid;
  width: 44px;
  min-width: 44px;
  min-height: 44px;
  place-items: center;
  padding: 0;
  font-size: 1.2rem;
  font-weight: 900;
}

.mobile-game-topbar,
.mobile-game-bottombar,
.tower-float-card {
  display: none;
}

.end-screen {
  display: none;
  place-items: center;
  align-content: center;
  text-align: center;
}

.end-screen.active {
  display: grid;
}

@media (max-width: 980px) {
  .generator-grid,
  .game-shell {
    grid-template-columns: 1fr;
  }

  .toolbox,
  .upgrade-panel {
    position: static;
  }

  .toolbox {
    grid-template-columns: repeat(2, minmax(150px, 1fr));
  }

  .toolbox h2 {
    grid-column: 1 / -1;
  }
}

@media (max-width: 620px) {
  .screen {
    padding: 14px;
  }

  .brand,
  .menu-actions,
  .toolbox {
    grid-template-columns: 1fr;
  }

  .topbar {
    flex-wrap: wrap;
  }
}

#screen-game.mobile-mode {
  position: fixed;
  inset: 0;
  min-height: 100dvh;
  padding: 0;
  overflow: hidden;
  background: #101812;
  touch-action: none;
}

#screen-game.mobile-mode .hud,
#screen-game.mobile-mode .toolbox,
#screen-game.mobile-mode .upgrade-panel {
  display: none;
}

#screen-game.mobile-mode .game-shell {
  display: block;
  width: 100vw;
  height: 100dvh;
}

#screen-game.mobile-mode #game-canvas {
  width: 100vw;
  height: 100dvh;
  max-width: none;
  border: 0;
  border-radius: 0;
  touch-action: none;
}

#screen-game.mobile-mode .mobile-game-topbar {
  position: fixed;
  z-index: 20;
  top: max(8px, env(safe-area-inset-top));
  left: 8px;
  right: 8px;
  display: flex;
  gap: 6px;
  align-items: center;
  overflow-x: auto;
  pointer-events: auto;
}

.mobile-stat {
  display: grid;
  min-width: 48px;
  min-height: 44px;
  align-content: center;
  border: 1px solid rgba(240, 206, 107, 0.42);
  border-radius: 6px;
  padding: 4px 7px;
  background: rgba(16, 25, 20, var(--mobile-toolbar-opacity, 0.5));
  backdrop-filter: blur(4px);
}

.mobile-stat span {
  color: var(--muted);
  font-size: 0.68rem;
}

.mobile-stat strong {
  max-width: 72px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.82rem;
}

#screen-game.mobile-mode .mobile-game-topbar button,
#screen-game.mobile-mode .mobile-game-bottombar button {
  background-color: rgba(34, 49, 39, var(--mobile-toolbar-opacity, 0.5));
  backdrop-filter: blur(4px);
}

#screen-game.mobile-mode .mobile-game-topbar .primary {
  background: rgba(216, 169, 60, var(--mobile-toolbar-opacity, 0.5));
  color: #170f08;
}

#screen-game.mobile-mode .mobile-game-bottombar {
  position: fixed;
  z-index: 20;
  left: 8px;
  right: 8px;
  bottom: max(8px, env(safe-area-inset-bottom));
  display: flex;
  gap: 10px;
  justify-content: space-between;
  pointer-events: none;
}

#screen-game.mobile-mode.mobile-layout-top .mobile-game-bottombar {
  display: none;
}

#screen-game.mobile-mode.mobile-layout-split .mobile-game-topbar .mobile-upgrade-btn,
#screen-game.mobile-mode.mobile-layout-split .mobile-game-topbar .mobile-sell-btn {
  display: none;
}

#screen-game.mobile-mode.mobile-layout-top .mobile-game-topbar {
  flex-wrap: nowrap;
}

#screen-game.mobile-mode.mobile-layout-top .mobile-game-topbar::after {
  content: "";
  flex: 0 0 1px;
}

.mobile-tower-row,
.mobile-action-row {
  display: flex;
  gap: 8px;
  pointer-events: auto;
}

.mobile-top-towers {
  display: none;
}

#screen-game.mobile-mode.mobile-layout-top .mobile-top-towers {
  display: flex;
}

.mobile-tower-btn {
  position: relative;
  display: grid;
  width: 48px;
  min-width: 48px;
  min-height: 48px;
  place-items: center;
  padding: 0;
}

.mobile-tower-btn.selected {
  border-color: var(--gold);
  box-shadow: inset 0 0 0 2px rgba(240, 206, 107, 0.35);
}

.mobile-tower-btn::before {
  content: "";
  width: 28px;
  height: 28px;
  background: currentColor;
  color: var(--gold);
  clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}

.mobile-tower-btn[data-tower="rapid"]::before {
  clip-path: polygon(40% 0, 60% 0, 60% 35%, 82% 35%, 82% 58%, 60% 58%, 60% 100%, 40% 100%, 40% 58%, 18% 58%, 18% 35%, 40% 35%);
}

.mobile-tower-btn[data-tower="frost"]::before {
  color: var(--blue);
  clip-path: polygon(42% 0, 58% 0, 58% 42%, 100% 42%, 100% 58%, 58% 58%, 58% 100%, 42% 100%, 42% 58%, 0 58%, 0 42%, 42% 42%);
}

.mobile-tower-btn[data-tower="arcane"]::before {
  color: var(--violet);
}

.mobile-tower-btn[data-tower="splash"]::before {
  color: #df8a44;
  clip-path: polygon(50% 0, 100% 100%, 0 100%);
}

#screen-game.mobile-mode .tower-float-card {
  position: fixed;
  z-index: 30;
  display: none;
  max-width: min(280px, calc(100vw - 24px));
  border: 1px solid rgba(240, 206, 107, 0.58);
  border-radius: 8px;
  padding: 10px 12px;
  background: rgba(16, 25, 20, 0.9);
  color: var(--ink);
  box-shadow: 0 14px 32px rgba(0, 0, 0, 0.38);
  pointer-events: none;
}

#screen-game.mobile-mode .tower-float-card.visible {
  display: block;
}

.multiplayer-app {
  padding: 18px;
}

.multiplayer-topbar {
  margin-bottom: 14px;
}

.multiplayer-topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 1.6rem;
}

.multiplayer-layout {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}

.mp-controls {
  display: grid;
  gap: 12px;
  position: sticky;
  top: 12px;
  max-height: calc(100vh - 92px);
  overflow: auto;
}

.mp-controls h2 {
  margin: 12px 0 0;
}

.check-row {
  grid-template-columns: 20px 1fr;
  align-items: center;
}

.check-row input {
  min-height: auto;
}

.status-line {
  min-height: 28px;
  color: var(--muted);
  font-size: 0.88rem;
  overflow-wrap: anywhere;
}

.client-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(360px, 1fr));
  gap: 14px;
}

.client-panel {
  min-width: 0;
  border: 1px solid rgba(216, 169, 60, 0.28);
  border-radius: 8px;
  padding: 12px;
  background: rgba(22, 33, 27, 0.82);
}

.client-hud {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 10px;
}

.client-hud div {
  display: grid;
  gap: 2px;
  min-height: 48px;
  align-content: center;
  border: 1px solid rgba(216, 169, 60, 0.22);
  border-radius: 6px;
  padding: 6px 8px;
  background: rgba(0, 0, 0, 0.16);
}

.client-hud span {
  color: var(--muted);
  font-size: 0.75rem;
}

.client-actions {
  display: grid;
  gap: 10px;
  margin-top: 10px;
}

.tower-bar {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.tower-bar .tower-choice {
  min-height: 56px;
  padding: 6px 8px;
}

.mp-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 14px;
}

@media (max-width: 1180px) {
  .multiplayer-layout,
  .client-grid {
    grid-template-columns: 1fr;
  }

  .mp-controls {
    position: static;
    max-height: none;
  }
}

@media (max-width: 620px) {
  .tower-bar,
  .client-hud {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
