* {
  box-sizing: border-box;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html {
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}

body {
  margin: 0;
  font-size: 16px;
  color: #1b2d45;
  min-height: 100vh;
  min-height: 100dvh;
  background-color: #7eb8f0;
  position: relative;
  touch-action: manipulation;
}

/* Full-screen BG on its own layer — avoids repainting the whole page when UI changes */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  background-color: #7eb8f0;
  background-image: url("assets/all-screens.png");
  background-size: cover;
  background-position: center top;
  background-repeat: no-repeat;
  transform: translateZ(0);
  pointer-events: none;
}

body.app-bg-game::before {
  background-image: url("assets/game.png");
}

body.app-battle .headerCard {
  display: none !important;
}

/* Fill space inside .app (respects padding + safe area); do not use extra 100dvh min-heights or the battle UI clips past the bottom. */
body.app-battle .screenStack {
  margin-top: 0;
  flex: 1 1 auto;
  min-height: 0;
  align-self: stretch;
}

body.app-battle {
  /* Visible soil above home bar; whole strip is brown to the glass edge (no sky gap under “ground”). */
  --battle-ground-brown: 8px;
  /* Total fixed bar = safe area + soil (matches #battleCard padding-bottom). */
  --battle-ground-strip: calc(env(safe-area-inset-bottom, 0px) + var(--battle-ground-brown));
  /* Extra room when towerStack uses transform: scale (finale) / drop motion bleed */
  --battle-tower-pad-drop: min(72px, 12vh);
  --battle-tower-pad-end: min(120px, 20vh);
}

body.app-battle .app {
  display: flex;
  flex-direction: column;
  position: relative;
  z-index: 2;
  min-height: 100dvh;
  min-height: 100svh;
  padding-top: max(6px, env(safe-area-inset-top, 0px));
  /* Strip itself fills safe area; no extra gap under the card */
  padding-bottom: 0;
}

/* Battle: stretch within padded main; chrome (game) on top, tower below */
body.app-battle #screenBattle.screen {
  bottom: 0;
  gap: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: visible;
}

#battleCard.battleCard {
  background: transparent !important;
  box-shadow: none !important;
}

/* Base: tower bottom flush with top of brown line (no extra “sky” gap; strip already clears safe area). */
body.app-battle #battleCard.battleCard {
  padding-bottom: var(--battle-ground-strip);
}

body.app-battle #battleCard.battleCard.battle-drop-mode {
  padding-bottom: calc(var(--battle-ground-strip) + var(--battle-tower-pad-drop));
}

body.app-battle #battleCard.battleCard.battle-end-reveal {
  padding-bottom: calc(var(--battle-ground-strip) + var(--battle-tower-pad-end));
}

/* Full viewport width, flush to physical bottom — brown fills safe area too (no floating line). */
.towerGroundBar {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  max-width: none;
  margin: 0;
  padding: 0;
  z-index: 1;
  pointer-events: none;
  box-sizing: border-box;
  background: linear-gradient(180deg, #8f5e40 0%, #7a4f34 28%, #4a301c 72%, #2a1a10 100%);
  border-top: 1px solid rgba(35, 22, 12, 0.45);
}

body.app-battle .towerGroundBar {
  display: block;
  height: var(--battle-ground-strip);
}

.app {
  position: relative;
  z-index: 0;
  max-width: 430px;
  margin: 0 auto;
  padding-top: calc(env(safe-area-inset-top, 0px) + 12px);
  padding-right: calc(env(safe-area-inset-right, 0px) + 12px);
  padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 20px);
  padding-left: calc(env(safe-area-inset-left, 0px) + 12px);
}

.screenStack {
  position: relative;
  margin-top: 10px;
  min-height: calc(100dvh - 168px);
  min-height: calc(100svh - 168px);
  transform: translateZ(0);
  isolation: isolate;
}

.screen {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  width: 100%;
  display: grid;
  gap: 10px;
  align-content: start;
  justify-items: stretch;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.screen.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.card {
  background: rgba(255, 255, 255, 0.97);
  border-radius: 16px;
  padding: 12px;
  box-shadow: 0 2px 10px rgba(11, 73, 128, 0.1);
  /* backdrop-filter is very expensive on iOS WKWebView during screen transitions */
}

.headerCard {
  background: rgba(255, 255, 255, 0.95);
}

h1 {
  margin: 0;
  font-size: 22px;
}

p {
  margin: 6px 0 0;
}

h2 {
  margin: 0;
  font-size: 18px;
}

h3.subheading {
  margin: 12px 0 4px;
  font-size: 15px;
  color: #4a5f78;
}

.grid {
  display: grid;
  gap: 8px;
}

.hint {
  margin: 0;
  font-size: 14px;
  line-height: 1.35;
  color: #4a5f78;
}

.matchmakingCard {
  text-align: center;
}

.matchmakingStatus {
  margin: 8px 0 4px;
  font-size: 18px;
  font-weight: 800;
  /* infinite opacity animation causes extra compositing on iOS WebView */
}

label {
  display: grid;
  gap: 4px;
}

label span {
  font-size: 12px;
  color: #637a95;
}

/* iOS Safari/WKWebView zooms the page on focus if control font-size is < 16px */
/* Never style type="range" like a text field — padding/border/min-height breaks thumb vs fill on WebKit */
input:not([type="range"]),
select,
textarea {
  font-size: 16px;
  line-height: 1.25;
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid #c9deef;
  padding: 10px;
  background: rgba(249, 253, 255, 0.95);
}

button {
  width: 100%;
  min-height: 46px;
  border-radius: 12px;
  border: none;
  font-weight: 800;
}

.primary {
  background: #3e9bff;
  color: #fff;
}

.secondary {
  background: #ffbf59;
  color: #2f2311;
}

.danger {
  background: #e24a4a;
  color: #fff;
}

.accountButtons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.accountButtons .secondary,
.accountButtons .primary {
  margin-top: 0;
}

.menuBigBtn {
  min-height: 52px;
  font-size: 17px;
}

.menuCard {
  gap: 10px;
}


.hidden {
  display: none !important;
}

.muted {
  color: #637a95;
}

.smallMsg {
  min-height: 1.2em;
  font-size: 13px;
}

.statSummary {
  font-size: 17px;
  font-weight: 800;
  margin: 0;
}

.scrollTable {
  max-height: 220px;
  overflow: auto;
  font-size: 14px;
  border: 1px solid #d6e7f6;
  border-radius: 10px;
  padding: 8px;
  background: rgba(255, 255, 255, 0.7);
}

table.leaderTable {
  width: 100%;
  border-collapse: collapse;
}

table.leaderTable th,
table.leaderTable td {
  text-align: left;
  padding: 6px 4px;
  border-bottom: 1px solid #e8f1fa;
}

table.leaderTable th {
  font-size: 12px;
  color: #637a95;
  position: sticky;
  top: 0;
  background: rgba(255, 255, 255, 0.95);
}

.matchRow {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid #e8f1fa;
  font-size: 14px;
}

.matchRow:last-child {
  border-bottom: none;
}

.badge-win {
  color: #0a7a3a;
  font-weight: 800;
}

.badge-loss {
  color: #a63a2a;
  font-weight: 800;
}

.themeField {
  display: grid;
  gap: 6px;
}

.themeLabel {
  font-size: 12px;
  color: #637a95;
}

.lobbyRow {
  display: grid;
  gap: 6px;
}

.pillRow {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

#lobbyTargetBtns.pillRow {
  grid-template-columns: repeat(4, 1fr);
}

.pillBtn {
  min-height: 44px;
  border-radius: 10px;
  border: 2px solid #c9deef;
  background: rgba(255, 255, 255, 0.95);
  font-size: 15px;
  font-weight: 800;
  color: #1b2d45;
  cursor: pointer;
}

.pillBtn.selected {
  border-color: #3e9bff;
  background: rgba(62, 155, 255, 0.18);
}

.pillBtn:disabled {
  opacity: 0.38;
  cursor: not-allowed;
}

.pregameCard {
  text-align: left;
}

.pregameBody {
  display: grid;
  gap: 12px;
}

.pregameRow {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px;
  border-radius: 12px;
  background: rgba(240, 248, 255, 0.9);
}

.pregameRow img {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 10px;
  border: 2px solid #c9deef;
}

.themeGrid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}

.themeOpt {
  width: 100%;
  aspect-ratio: 1;
  min-height: 0;
  padding: 0;
  border-radius: 10px;
  border: 3px solid transparent;
  background-size: cover;
  background-position: center;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
}

.themeOpt.selected {
  border-color: #3e9bff;
  box-shadow: 0 0 0 2px rgba(62, 155, 255, 0.35);
}

.themeOpt:disabled {
  opacity: 0.32;
  cursor: not-allowed;
  filter: grayscale(0.85);
}

.scoreRow {
  display: flex;
  justify-content: space-between;
}

.scoreRow span {
  display: block;
  font-size: 12px;
  color: #637a95;
}

.scoreRow strong {
  font-size: 22px;
}

.battleCard {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  padding: 8px 10px 10px;
  gap: 6px;
}

.battleToolbar {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  flex-shrink: 0;
  min-height: 44px;
  margin: 0 0 2px;
}

.battleLeaveBtn {
  position: relative;
  width: 46px;
  height: 46px;
  margin: 0;
  padding: 0;
  border: none;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 2px 12px rgba(20, 60, 100, 0.15);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.battleLeaveBtn:active {
  transform: scale(0.94);
}

.battleLeaveIcon {
  display: block;
}

.battleChrome {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 0 0 auto;
  transition: opacity 0.2s ease;
  padding: 12px 14px 14px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.86);
  box-shadow: 0 4px 24px rgba(30, 80, 130, 0.12);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.battleScoreRow {
  margin-top: 0;
}

.battleProblem {
  text-align: center;
  font-size: clamp(1.55rem, 6.5vw, 2.05rem);
  font-weight: 800;
  margin: 4px 0 2px;
  line-height: 1.2;
  letter-spacing: -0.03em;
  color: #0d2138;
}

.roundFeedback {
  margin: 2px 0 0;
  min-height: 1.35em;
  font-size: 14px;
  font-weight: 600;
  color: #2d6a4f;
  text-align: center;
}

.battleSubmit {
  margin-top: 4px;
  width: 100%;
  min-height: 48px;
  font-size: 17px;
  font-weight: 700;
}

.roundFeedback[hidden] {
  display: none !important;
}

#slider {
  margin-top: 8px;
  width: 100%;
  display: block;
  box-sizing: border-box;
  padding: 0;
  border: 0;
  min-height: 0;
  height: 44px;
  font-size: 16px;
  background: transparent;
  accent-color: #3e9bff;
}

.sliderRow {
  display: flex;
  justify-content: space-between;
  color: #637a95;
}

#outV {
  color: #0d2138;
  font-size: 20px;
  font-weight: 800;
}

.towerStage {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-self: stretch;
  min-height: 0;
  margin-top: 4px;
  margin-bottom: 0;
  background: transparent;
}

.towerScroll {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;
  max-height: none;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 4px 0 0;
  border: none;
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.towerScroll::-webkit-scrollbar {
  display: none;
  width: 0;
  height: 0;
}

.towerStack {
  display: flex;
  flex-direction: column-reverse;
  align-items: center;
  /* column-reverse: main-start = bottom — pack from bottom so the base sits on the “ground”, no air gap */
  justify-content: flex-start;
  flex-shrink: 0;
  gap: 0;
  width: 100%;
  min-height: 24px;
}

/* Raw square sprites — compact (≈4× smaller than prior 200px / 72vw) */
.floor-block {
  width: min(50px, 18vw);
  aspect-ratio: 1;
  height: auto;
  flex-shrink: 0;
  margin: 0;
  padding: 0;
  border: none;
  border-radius: 0;
  outline: none;
  box-shadow: none;
  background-color: transparent;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}

.floor-block[data-theme="blue"] {
  background-image: url("assets/floor_blue.png");
}
.floor-block[data-theme="green"] {
  background-image: url("assets/floor_green.png");
}
.floor-block[data-theme="orange"] {
  background-image: url("assets/floor_orange.png");
}
.floor-block[data-theme="red"] {
  background-image: url("assets/floor_red.png");
}
.floor-block[data-theme="yellow"] {
  background-image: url("assets/floor_yellow.png");
}

.floor-block.floor-roof {
  aspect-ratio: 1;
  height: auto;
  border: none;
  border-radius: 0;
  box-shadow: none;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}

.floor-block.floor-roof[data-theme="blue"] {
  background-image: url("assets/roof_blue.png");
}
.floor-block.floor-roof[data-theme="green"] {
  background-image: url("assets/roof_green.png");
}
.floor-block.floor-roof[data-theme="orange"] {
  background-image: url("assets/roof_orange.png");
}
.floor-block.floor-roof[data-theme="red"] {
  background-image: url("assets/roof_red.png");
}
.floor-block.floor-roof[data-theme="yellow"] {
  background-image: url("assets/roof_yellow.png");
}

/* Drop from above only — no scale / no upward bounce (those read as “lift then fall”). */
@keyframes floorDropIn {
  0% {
    transform: translateY(calc(-48vh - 32px));
    opacity: 0.92;
  }
  78% {
    transform: translateY(0);
    opacity: 1;
  }
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

.floor-drop {
  animation: floorDropIn 0.68s cubic-bezier(0.22, 1, 0.36, 1) forwards;
  z-index: 2;
}

.battleCard.battle-drop-mode .battleChrome {
  display: none !important;
}

.battleCard.battle-drop-mode {
  position: fixed;
  inset: 0;
  z-index: 40;
  max-width: none;
  width: 100%;
  height: 100dvh;
  height: 100svh;
  margin: 0;
  /* padding-bottom set on body.app-battle #battleCard.battleCard.battle-drop-mode (id beats .battleCard…) */
  padding-top: max(6px, env(safe-area-inset-top, 0px));
  padding-right: max(10px, env(safe-area-inset-right, 0px));
  padding-bottom: 0;
  padding-left: max(10px, env(safe-area-inset-left, 0px));
  border-radius: 0;
  gap: 0;
}

.battleCard.battle-drop-mode .towerStage {
  flex: 1 1 auto;
  min-height: 0;
  margin-top: 0;
}

.battleCard.battle-drop-mode .towerScroll {
  max-height: none;
  flex: 1;
  min-height: 0;
  height: 100%;
  border: none;
  background: transparent;
  /* hidden clipped the falling block against the bottom edge (looked like “one floor under ground”) */
  overflow-y: visible;
}

/* Match end: tower-only fullscreen “cinema”, then result modal */
.battleCard.battle-end-reveal .battleChrome {
  display: none !important;
}

.battleCard.battle-end-reveal {
  position: fixed;
  inset: 0;
  z-index: 40;
  max-width: none;
  width: 100%;
  height: 100dvh;
  height: 100svh;
  margin: 0;
  padding-top: max(6px, env(safe-area-inset-top, 0px));
  padding-right: max(10px, env(safe-area-inset-right, 0px));
  padding-bottom: 0;
  padding-left: max(10px, env(safe-area-inset-left, 0px));
  border-radius: 0;
  gap: 0;
}

.battleCard.battle-end-reveal .towerStage {
  flex: 1 1 auto;
  min-height: 0;
  margin-top: 0;
}

.battleCard.battle-end-reveal .towerScroll {
  max-height: none;
  flex: 1;
  min-height: 0;
  height: 100%;
  border: none;
  background: transparent;
  scroll-behavior: auto;
}

/* Fullscreen finale: whole tower reads larger (from base), independent of per-floor pop */
.battleCard.battle-end-reveal .towerStack {
  transform-origin: center bottom;
  animation: battleTowerEndZoom 2.75s cubic-bezier(0.18, 0.82, 0.22, 1) forwards;
}

@keyframes battleTowerEndZoom {
  0% {
    transform: scale(0.78);
  }
  42% {
    transform: scale(1.28);
  }
  72% {
    transform: scale(1.12);
  }
  100% {
    transform: scale(1.2);
  }
}

@keyframes floorEndPop {
  0% {
    opacity: 0.1;
    transform: translateY(26px) scale(0.78);
  }
  52% {
    opacity: 1;
    transform: translateY(0) scale(1.08);
  }
  76% {
    transform: translateY(0) scale(0.97);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.floor-end-pop {
  animation: floorEndPop 0.95s cubic-bezier(0.22, 1, 0.45, 1) forwards;
  opacity: 0.1;
}

.modalOverlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: grid;
  place-items: center;
  padding: max(16px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
  background: rgba(13, 33, 56, 0.48);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
}

.modalOverlay[hidden] {
  display: none !important;
}

.modalCard {
  width: min(360px, 100%);
  background: #fff;
  border-radius: 20px;
  padding: 28px 22px 24px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.22);
}

.modalCard h2 {
  margin: 0 0 10px;
  font-size: 1.45rem;
  color: #0b6b45;
}

.modalBody {
  margin: 0 0 20px;
  font-size: 16px;
  line-height: 1.45;
  color: #1b2d45;
  white-space: pre-line;
}

.modalBtn {
  width: 100%;
}

.modalBtnRow {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.modalCardDelete h2 {
  color: #8b2525;
}
