:root {
  --bg-dark: #0e0a16;
  --panel-bg: #1c1528;
  --panel-mid: #281e3a;
  --border-gold: #e6b83a;
  --border-gold-dark: #7a5214;
  --pepto: #ff7cc8;
  --pepto-deep: #c4368a;
  --toxic: #7cf052;
  --toxic-dark: #257a2e;
  --crimson: #e83b4b;
  --text: #f6f2ff;
  --muted: #b2a6cc;
}

* {
  box-sizing: border-box;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg-dark);
  color: var(--text);
  font-family: 'Press Start 2P', 'Courier New', monospace;
  image-rendering: pixelated;
}

img, canvas {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

#game-root {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: #0b0812;
}

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

/* Gothic Stepped Pixel Box Frame */
.pixel-panel {
  background: linear-gradient(180deg, #271d3a 0%, #161022 100%);
  border: 3px solid var(--border-gold);
  box-shadow:
    0 0 0 3px #120c1c,
    inset 0 0 0 3px #422e14,
    0 12px 32px rgba(0, 0, 0, 0.8);
}

.pixel-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 12px;
  letter-spacing: 0.5px;
  color: #fff;
  background: linear-gradient(180deg, #4f3a75 0%, #2e2049 100%);
  border: 3px solid var(--border-gold);
  box-shadow: 0 4px 0 #140c1f, inset 0 2px 0 rgba(255,255,255,0.22);
  padding: 10px 18px;
  cursor: pointer;
  text-transform: uppercase;
  text-shadow: 2px 2px 0 #12091b;
  transition: transform 0.06s, filter 0.1s;
}
.pixel-btn:hover {
  filter: brightness(1.18);
  transform: translateY(-1px);
}
.pixel-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #140c1f;
}
.pixel-btn.primary {
  background: linear-gradient(180deg, #d9366a 0%, #8e1940 100%);
  border-color: #ffe374;
  font-size: 15px;
  padding: 14px 28px;
}
.pixel-btn.toxic {
  background: linear-gradient(180deg, #3ea832 0%, #1d611c 100%);
  border-color: #a4ff70;
}
.pixel-btn.small {
  font-size: 10px;
  padding: 7px 12px;
}
.pixel-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  filter: grayscale(0.7);
}

/* ==================== TITLE & MENU OVERLAY ==================== */
.screen-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 20;
}
.hidden {
  display: none !important;
}

#title-screen {
  background-color: #0d0816;
  background-image:
    radial-gradient(ellipse at center, rgba(16, 10, 28, 0.25) 0%, rgba(8, 5, 14, 0.88) 100%),
    url('assets/title-bg.png');
  background-size: cover;
  background-position: center;
  padding: 10px 14px;
  overflow-y: auto;
  overflow-x: hidden;
  justify-content: center;
}

.title-banner {
  text-align: center;
  margin-top: 4px;
  margin-bottom: 10px;
}
.title-main {
  font-size: clamp(20px, 3.4vw, 40px);
  line-height: 1.2;
  margin: 0;
  color: #ffe46e;
  text-shadow:
    0 4px 0 #7a2d12,
    0 7px 0 #1c0814,
    0 0 26px rgba(124, 240, 82, 0.55);
  letter-spacing: 2px;
}
.title-main .drip {
  color: #88f754;
  text-shadow:
    0 4px 0 #1c5e20,
    0 7px 0 #0c1c10,
    0 0 22px rgba(136, 247, 84, 0.7);
}
.title-sub {
  font-size: 9.5px;
  color: var(--pepto);
  margin-top: 5px;
  text-shadow: 2px 2px 0 #16091f;
  letter-spacing: 1.5px;
}

.menu-grid {
  display: grid;
  grid-template-columns: 1.25fr 0.95fr;
  gap: 12px;
  width: min(1000px, 96vw);
  margin-bottom: 10px;
}

@media (max-width: 820px) {
  .menu-grid { grid-template-columns: 1fr; }
}

.menu-box {
  padding: 11px 12px;
  min-width: 0;
  max-width: 100%;
}
.menu-box h3 {
  margin: 0 0 8px 0;
  font-size: 10.5px;
  color: #ffe46e;
  text-shadow: 2px 2px 0 #0d0816;
  border-bottom: 2px solid #4c366b;
  padding-bottom: 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.char-list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.char-list.five-col {
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}
@media (max-width: 900px) {
  .char-list.five-col { grid-template-columns: repeat(3, 1fr); }
}

.char-card {
  background: rgba(18, 12, 29, 0.92);
  border: 3px solid #4b396b;
  padding: 5px;
  cursor: pointer;
  text-align: center;
  transition: transform 0.08s, border-color 0.1s;
}
.char-card.locked {
  opacity: 0.72;
  border-color: #36294d;
}
.char-card.locked img.portrait {
  filter: grayscale(0.85) brightness(0.55);
}
.portrait-wrap {
  position: relative;
}
.lock-badge {
  position: absolute;
  inset: auto 2px 8px 2px;
  background: rgba(12, 8, 22, 0.9);
  border: 1px solid #ffe46e;
  color: #ffe46e;
  font-size: 7px;
  padding: 2px 3px;
}
.char-card:hover {
  border-color: var(--pepto);
  transform: translateY(-2px);
}
.char-card.selected {
  border-color: #ffe46e;
  background: linear-gradient(180deg, #372352 0%, #1d132e 100%);
  box-shadow: 0 0 16px rgba(255, 228, 110, 0.35);
}
.char-card img.portrait {
  width: 100%;
  max-height: 98px;
  aspect-ratio: 1 / 1;
  border: 2px solid #2b1f40;
  display: block;
  margin: 0 auto 5px auto;
  object-fit: cover;
}
.char-card .cname {
  font-size: 8px;
  color: #fff;
  margin-bottom: 3px;
}
.char-card .ctitle {
  font-size: 6.5px;
  color: var(--pepto);
  margin-bottom: 4px;
}
.char-card .cbonus {
  font-size: 6.2px;
  line-height: 1.4;
  color: var(--toxic);
}

.relic-section {
  margin-top: 8px;
  padding-top: 7px;
  border-top: 2px solid #3c2b59;
}
.relic-header {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 7px;
  color: #ffe46e;
  margin-bottom: 6px;
  flex-wrap: wrap;
}
.relic-row {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
}
.relic-pill {
  font-family: 'Press Start 2P', monospace;
  font-size: 6.5px;
  color: #e4dbf5;
  background: #140e22;
  border: 2px solid #4a3769;
  padding: 4px 6px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
}
.relic-pill:hover { border-color: #8ef6ff; }
.relic-pill.selected {
  border-color: #ffe46e;
  background: #36214f;
  color: #ffe46e;
}
.relic-pill.locked {
  opacity: 0.52;
}

.stage-card.locked {
  opacity: 0.68;
}
.mod-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 7px;
  margin-top: 8px;
}
.mod-toggle {
  font-family: 'Press Start 2P', monospace;
  font-size: 7px;
  color: #cfc4e6;
  background: #140d21;
  border: 2px solid #4a3769;
  padding: 7px 6px;
  cursor: pointer;
}
.mod-toggle.active {
  border-color: #ff6262;
  background: linear-gradient(180deg, #5e1d2f 0%, #2d0e1a 100%);
  color: #ffe46e;
  box-shadow: 0 0 12px rgba(255, 98, 98, 0.35);
}

.char-detail-bar {
  margin-top: 8px;
  padding: 7px 9px;
  background: rgba(12, 8, 20, 0.85);
  border: 2px solid #3c2b59;
  font-size: 8px;
  line-height: 1.5;
  color: #dcd2f0;
  display: flex;
  align-items: center;
  gap: 10px;
}
.char-detail-bar .quote {
  color: #ffe46e;
  font-style: italic;
}

.stage-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.stage-card {
  background: rgba(18, 12, 29, 0.92);
  border: 3px solid #4b396b;
  padding: 8px 10px;
  cursor: pointer;
  transition: border-color 0.1s;
}
.stage-card:hover { border-color: var(--pepto); }
.stage-card.selected {
  border-color: var(--toxic);
  background: linear-gradient(90deg, #253b2c 0%, #19132b 100%);
  box-shadow: 0 0 14px rgba(124, 240, 82, 0.25);
}
.stage-card .sname {
  font-size: 10px;
  color: #fff;
  margin-bottom: 4px;
}
.stage-card .ssub {
  font-size: 8px;
  color: #ffe46e;
  margin-bottom: 5px;
}
.stage-card .sdesc {
  font-size: 7.5px;
  color: var(--muted);
  line-height: 1.5;
}

.menu-actions {
  display: flex;
  gap: 14px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 10px;
}
.gold-pill {
  background: rgba(16, 11, 24, 0.92);
  border: 2px solid var(--border-gold);
  padding: 8px 14px;
  font-size: 11px;
  color: #ffe46e;
  display: flex;
  align-items: center;
  gap: 8px;
}
.controls-hint {
  font-size: 8px;
  color: #c8bddf;
  background: rgba(12, 8, 22, 0.8);
  padding: 6px 12px;
  border: 1px solid #3c2c58;
  text-align: center;
}

/* ==================== POWERUP SHOP MODAL ==================== */
#shop-modal {
  background: rgba(8, 5, 14, 0.88);
  z-index: 30;
  padding: 16px;
}
.shop-box {
  width: min(820px, 95vw);
  max-height: 90vh;
  overflow-y: auto;
  padding: 18px;
}
.shop-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
  border-bottom: 2px solid #4c366b;
  padding-bottom: 10px;
}
.shop-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}
.shop-item {
  background: #140e21;
  border: 2px solid #4a3769;
  padding: 10px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.shop-item .pips {
  display: flex;
  gap: 4px;
}
.shop-item .pip {
  width: 10px;
  height: 10px;
  background: #2b2042;
  border: 1px solid #69528f;
}
.shop-item .pip.on {
  background: #88f754;
  border-color: #d8ffba;
}

/* ==================== IN-GAME HUD (Authentic VS Layout) ==================== */
#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

.xp-bar-wrap {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 20px;
  background: #120c1d;
  border-bottom: 3px solid #ffe46e;
  box-shadow: 0 3px 8px rgba(0,0,0,0.7);
}
#xp-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #2784e8 0%, #5ce8ff 65%, #ffffff 100%);
  transition: width 0.1s linear;
}
#level-badge {
  position: absolute;
  right: 10px;
  top: 3px;
  font-size: 10px;
  color: #fff;
  text-shadow: 2px 2px 0 #000;
}

.hud-top-row {
  position: absolute;
  top: 26px;
  left: 10px;
  right: 10px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

.slots-wrap {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.slot-row {
  display: flex;
  gap: 4px;
}
.inv-slot {
  width: 34px;
  height: 34px;
  background: rgba(16, 11, 26, 0.85);
  border: 2px solid #59447a;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}
.inv-slot.evolved {
  border-color: #ffe46e;
  box-shadow: 0 0 8px rgba(255, 228, 110, 0.65);
  background: rgba(58, 34, 16, 0.9);
}
.inv-slot canvas {
  width: 26px;
  height: 26px;
}
.inv-slot .lv {
  position: absolute;
  right: 1px;
  bottom: 1px;
  font-size: 7px;
  color: #ffe46e;
  background: rgba(0, 0, 0, 0.8);
  padding: 1px 2px;
}

.hud-center {
  text-align: center;
}
#hud-timer {
  font-size: 20px;
  color: #fff;
  text-shadow: 3px 3px 0 #12081b, 0 0 12px rgba(255, 124, 200, 0.45);
}
#hud-wave-label {
  font-size: 8px;
  color: var(--pepto);
  margin-top: 4px;
  text-shadow: 2px 2px 0 #000;
}

.hud-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
}
.hud-stats-line {
  display: flex;
  gap: 12px;
  font-size: 10px;
  background: rgba(14, 9, 22, 0.78);
  padding: 5px 10px;
  border: 2px solid #483566;
  text-shadow: 2px 2px 0 #000;
}
.hud-controls {
  display: flex;
  gap: 6px;
  pointer-events: auto;
}

/* Boss HP Bar */
#boss-bar-wrap {
  position: absolute;
  top: 86px;
  left: 50%;
  transform: translateX(-50%);
  width: min(460px, 82vw);
  text-align: center;
}
#boss-name {
  font-size: 10px;
  color: #ff6262;
  text-shadow: 2px 2px 0 #000;
  margin-bottom: 4px;
}
.boss-hp-outer {
  height: 14px;
  background: #1a0b12;
  border: 2px solid #ffe46e;
}
#boss-hp-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, #a81024 0%, #ff4538 100%);
}

#banner-toast {
  position: absolute;
  top: 130px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(20, 8, 28, 0.92);
  border: 3px solid #ff5252;
  color: #ffe46e;
  padding: 10px 20px;
  font-size: 12px;
  text-align: center;
  text-shadow: 2px 2px 0 #000;
  box-shadow: 0 0 24px rgba(255, 50, 70, 0.55);
  z-index: 15;
}

/* ==================== LEVEL UP MODAL ==================== */
#levelup-modal, #chest-modal, #pause-modal, #gameover-modal {
  background: rgba(8, 5, 16, 0.82);
  z-index: 25;
  padding: 16px;
}

.levelup-box {
  width: min(560px, 94vw);
  padding: 18px;
}
.levelup-title {
  text-align: center;
  font-size: 18px;
  color: #ffe46e;
  margin: 0 0 6px 0;
  text-shadow: 3px 3px 0 #1b0b26;
}
.levelup-sub {
  text-align: center;
  font-size: 9px;
  color: var(--pepto);
  margin-bottom: 14px;
}

.upgrade-choices {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.upgrade-card {
  display: flex;
  align-items: center;
  gap: 14px;
  background: linear-gradient(90deg, #271d3d 0%, #171026 100%);
  border: 3px solid #6e5696;
  padding: 12px;
  cursor: pointer;
  text-align: left;
  transition: transform 0.08s, border-color 0.1s;
}
.upgrade-card:hover {
  border-color: #ffe46e;
  transform: translateX(4px);
  background: linear-gradient(90deg, #3c2961 0%, #201536 100%);
}
.upgrade-card.evo-card {
  border-color: #ffe46e;
  background: linear-gradient(90deg, #4f2c12 0%, #241326 100%);
  box-shadow: 0 0 18px rgba(255, 210, 60, 0.4);
}
.upgrade-icon-box {
  width: 48px;
  height: 48px;
  flex-shrink: 0;
  background: #120b1f;
  border: 2px solid #967cc4;
  display: flex;
  align-items: center;
  justify-content: center;
}
.upgrade-icon-box canvas {
  width: 38px;
  height: 38px;
}
.upgrade-meta {
  flex: 1;
}
.upgrade-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}
.upgrade-name {
  font-size: 11px;
  color: #fff;
}
.upgrade-tag {
  font-size: 9px;
  padding: 3px 6px;
  background: #234d28;
  color: #9cff7a;
  border: 1px solid #76e35b;
}
.upgrade-tag.new {
  background: #611d49;
  color: #ff99d6;
  border-color: #ff7cc8;
}
.upgrade-tag.evo {
  background: #70490b;
  color: #fff08a;
  border-color: #ffe46e;
}
.upgrade-desc {
  font-size: 8.5px;
  line-height: 1.55;
  color: #d6cbe8;
}
.upgrade-hint {
  margin-top: 5px;
  font-size: 7.5px;
  color: #ffe46e;
}

/* ==================== CHEST MODAL ==================== */
.chest-box {
  width: min(520px, 94vw);
  padding: 20px;
  text-align: center;
}
.chest-rays {
  display: flex;
  justify-content: center;
  margin: 12px 0;
}
.chest-rewards {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 14px 0;
  text-align: left;
}
.chest-reward-row {
  display: flex;
  align-items: center;
  gap: 12px;
  background: #140d21;
  border: 2px solid var(--border-gold);
  padding: 8px 10px;
  font-size: 9.5px;
  animation: popIn 0.25s ease-out both;
}
@keyframes popIn {
  from { transform: scale(0.8); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* ==================== PAUSE & GAMEOVER ==================== */
.summary-box {
  width: min(600px, 94vw);
  max-height: 90vh;
  overflow-y: auto;
  padding: 20px;
  text-align: center;
}
.summary-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin: 14px 0;
}
.stat-tile {
  background: #130d21;
  border: 2px solid #483566;
  padding: 10px 6px;
}
.stat-tile .val {
  font-size: 13px;
  color: #ffe46e;
  margin-bottom: 4px;
}
.stat-tile .lbl {
  font-size: 7.5px;
  color: var(--muted);
}
.dmg-table {
  width: 100%;
  border-collapse: collapse;
  margin: 12px 0 18px 0;
  font-size: 8.5px;
  text-align: left;
}
.dmg-table th, .dmg-table td {
  padding: 6px 8px;
  border-bottom: 1px solid #35274d;
}
.dmg-table th {
  color: var(--pepto);
}
.evo-chart {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 6px;
  margin: 12px 0 16px 0;
  text-align: left;
  font-size: 7.5px;
}
.evo-pair {
  background: #130d21;
  border: 1px solid #3c2c58;
  padding: 6px 8px;
  color: #d6cbe8;
}
.evo-pair b { color: #ffe46e; }

/* ==================== URGENCY METER (BOTTOM HUD) ==================== */
#urg-wrap {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  width: min(440px, 82vw);
  background: rgba(14, 9, 24, 0.86);
  border: 2px solid #59447a;
  padding: 6px 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.75);
}
.urg-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 8.5px;
  margin-bottom: 5px;
  text-shadow: 2px 2px 0 #000;
}
.urg-left {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: #f6f2ff;
}
.urg-left b { color: #ffe46e; }
#urg-state { color: #88f754; }

.urg-bar-outer {
  position: relative;
  height: 14px;
  background: #181024;
  border: 2px solid #12091c;
  box-shadow: inset 0 0 0 1px #4c386e;
  overflow: hidden;
}
#urg-fill {
  height: 100%;
  width: 0%;
  background: repeating-linear-gradient(90deg, #7cf052 0 12px, #4eb832 12px 15px);
  transition: width 0.1s linear;
}
.urg-mark {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 70%;
  width: 2px;
  background: #ffe46e;
  box-shadow: 0 0 4px #000;
}

#urg-wrap.mid { border-color: #e6b83a; }
#urg-wrap.mid #urg-state { color: #ffe46e; }
#urg-wrap.mid #urg-fill {
  background: repeating-linear-gradient(90deg, #ffe46e 0 12px, #d19c26 12px 15px);
}

#urg-wrap.cramp {
  border-color: #f08a30;
  animation: urgShake 0.22s steps(2) infinite;
}
#urg-wrap.cramp #urg-state { color: #ff9e4a; }
#urg-wrap.cramp #urg-fill {
  background: repeating-linear-gradient(90deg, #d67628 0 12px, #8c4814 12px 15px);
}

#urg-wrap.imminent {
  border-color: #ff3b3b;
  box-shadow: 0 0 18px rgba(255, 59, 59, 0.7);
  animation: urgShake 0.12s steps(2) infinite;
}
#urg-wrap.imminent #urg-state { color: #ff5252; }
#urg-wrap.imminent #urg-fill {
  background: repeating-linear-gradient(90deg, #e83b3b 0 10px, #7a3614 10px 14px);
}

#urg-wrap.shame { border-color: #a6642e; }
#urg-wrap.shame #urg-state { color: #d98e4c; }

@keyframes urgShake {
  0% { transform: translateX(calc(-50% - 2px)); }
  100% { transform: translateX(calc(-50% + 2px)); }
}

/* ==================== UNLOCKS, LEVELUP FOOTER & 4-COL SUMMARY ==================== */
#unlocks-modal {
  background: rgba(8, 5, 14, 0.88);
  z-index: 30;
  padding: 16px;
}
.ach-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
  gap: 8px;
}
.ach-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #140d21;
  border: 2px solid #3f2f5c;
  padding: 8px 10px;
}
.ach-row.done {
  border-color: #7cf052;
  background: linear-gradient(90deg, #1b3622 0%, #140d21 100%);
}
.ach-info { flex: 1; }
.ach-top {
  display: flex;
  justify-content: space-between;
  font-size: 8.5px;
  margin-bottom: 4px;
}
.ach-desc {
  font-size: 7px;
  color: #cfc4e6;
  line-height: 1.45;
}

.levelup-footer {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 12px;
}

.summary-stats.four-col {
  grid-template-columns: repeat(4, 1fr);
}
@media (max-width: 560px) {
  .summary-stats.four-col { grid-template-columns: repeat(2, 1fr); }
}

.end-unlocks {
  margin-top: 10px;
  padding: 8px 10px;
  background: #233b1e;
  border: 2px solid #88f754;
  color: #ffe46e;
  font-size: 8px;
  line-height: 1.5;
}

/* ==================== MOBILE-FIRST, SAFE AREAS, LANDSCAPE & DUO/FOLDABLES ==================== */
.screen-overlay {
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
  padding-top: max(10px, env(safe-area-inset-top));
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  padding-left: max(10px, env(safe-area-inset-left));
  padding-right: max(10px, env(safe-area-inset-right));
}

.xp-bar-wrap {
  top: env(safe-area-inset-top);
}
.hud-top-row {
  top: calc(26px + env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  right: max(10px, env(safe-area-inset-right));
}
#urg-wrap {
  bottom: max(12px, env(safe-area-inset-bottom));
}

/* ---- 1. Portrait Phones (iPhone 14/15/16/17, Android narrow vertical) ---- */
@media (max-width: 680px) {
  #title-screen {
    justify-content: flex-start;
    padding: max(8px, env(safe-area-inset-top)) 10px 0 10px;
  }
  .title-banner {
    margin-top: 4px;
    margin-bottom: 8px;
  }
  .title-main {
    font-size: clamp(18px, 6.2vw, 28px);
  }
  .title-sub {
    font-size: 7.5px;
    letter-spacing: 0.6px;
  }
  #menu-records {
    font-size: 6.8px !important;
    line-height: 1.5;
  }
  .menu-grid {
    grid-template-columns: minmax(0, 1fr);
    width: 100%;
    gap: 10px;
    margin-bottom: 8px;
  }
  .menu-box {
    padding: 10px;
    min-width: 0;
    overflow: hidden;
  }
  .menu-box h3 {
    font-size: 9px;
  }

  /* Swipeable horizontal survivor carousel so every portrait is large & thumb-tappable! */
  .char-list.five-col {
    display: flex;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    gap: 8px;
    padding-bottom: 6px;
    scrollbar-width: thin;
  }
  .char-card {
    flex: 0 0 116px;
    scroll-snap-align: start;
    padding: 6px;
  }
  .char-card img.portrait {
    max-height: 92px;
  }
  .char-card .cname { font-size: 8px; }
  .char-card .cbonus { font-size: 6.5px; }

  .char-detail-bar {
    font-size: 7.2px;
    padding: 6px 8px;
  }
  .relic-row {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 4px;
  }
  .relic-pill {
    flex: 0 0 auto;
    min-height: 36px;
    padding: 5px 8px;
  }

  .stage-card {
    padding: 8px 9px;
  }
  .stage-card .sname { font-size: 9px; }
  .stage-card .sdesc { font-size: 7px; }
  .mod-toggle {
    min-height: 40px;
    font-size: 7px;
  }

  /* Sticky thumb-friendly Play bar at bottom of portrait screen */
  .menu-actions {
    position: sticky;
    bottom: 0;
    left: 0;
    right: 0;
    width: calc(100% + 20px);
    margin: 4px -10px 0 -10px;
    padding: 8px 10px max(10px, env(safe-area-inset-bottom)) 10px;
    background: linear-gradient(180deg, rgba(14, 9, 24, 0.94) 0%, #0d0816 100%);
    border-top: 2px solid var(--border-gold);
    gap: 6px;
    z-index: 5;
  }
  .gold-pill {
    padding: 6px 9px;
    font-size: 9px;
  }
  .menu-actions .pixel-btn.small {
    padding: 9px 10px;
    font-size: 8.5px;
    min-height: 40px;
  }
  .menu-actions .pixel-btn.primary {
    flex: 1 1 100%;
    order: -1;
    padding: 13px 16px;
    font-size: 13px;
    min-height: 46px;
  }
  .controls-hint {
    font-size: 6.8px;
    line-height: 1.5;
    margin: 6px 0 8px 0;
  }

  /* Compact mobile HUD */
  .inv-slot {
    width: 26px;
    height: 26px;
  }
  .inv-slot canvas {
    width: 20px;
    height: 20px;
  }
  #hud-timer {
    font-size: 15px;
  }
  #hud-wave-label {
    font-size: 6.5px;
  }
  .hud-stats-line {
    font-size: 8px;
    gap: 7px;
    padding: 4px 6px;
  }
  .hud-controls .pixel-btn.small {
    min-width: 36px;
    min-height: 34px;
    padding: 4px 6px;
    font-size: 11px;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  #boss-bar-wrap {
    top: calc(96px + env(safe-area-inset-top));
    width: 90vw;
  }
  #banner-toast {
    top: calc(134px + env(safe-area-inset-top));
    font-size: 9.5px;
    padding: 8px 12px;
    max-width: 92vw;
  }
  #urg-wrap {
    width: calc(100vw - 24px);
    padding: 5px 8px;
  }
  .urg-header { font-size: 7.5px; }
  .urg-bar-outer { height: 12px; }

  /* Modals on narrow mobile */
  .levelup-box, .chest-box, .summary-box, .shop-box {
    width: 96vw;
    max-height: 88dvh;
    overflow-y: auto;
    padding: 12px;
  }
  .upgrade-card {
    padding: 10px;
    gap: 10px;
    min-height: 64px;
  }
  .upgrade-icon-box { width: 42px; height: 42px; }
  .upgrade-name { font-size: 9.5px; }
  .upgrade-desc { font-size: 7.8px; }
  .shop-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }
  .ach-grid {
    grid-template-columns: 1fr;
  }
  .evo-chart {
    grid-template-columns: 1fr;
  }
}

/* ---- 2. Square / Foldable / iPhone Duo Aspect Ratios (~0.75 to 1.25, e.g. 768x860, 820x900) ---- */
@media (min-width: 681px) and (max-width: 1080px) and (min-height: 600px) {
  #title-screen {
    padding: 8px 12px;
  }
  .title-banner {
    margin-top: 0;
    margin-bottom: 6px;
  }
  .title-main { font-size: clamp(22px, 3.6vw, 32px); }
  .menu-grid {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 20px; /* Crease-safe center gutter for foldables/Duo */
    width: min(980px, 96vw);
    margin-bottom: 8px;
  }
  .menu-box { padding: 9px 10px; }
  .char-list.five-col {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
  }
  .char-card { padding: 4px; }
  .char-card img.portrait {
    max-height: 70px;
    margin-bottom: 3px;
  }
  .char-card .cname { font-size: 7.2px; }
  .char-card .ctitle { font-size: 6px; margin-bottom: 2px; }
  .char-card .cbonus { font-size: 5.8px; line-height: 1.3; }
  .char-detail-bar { padding: 5px 7px; font-size: 7px; margin-top: 6px; }
  .relic-section { margin-top: 6px; padding-top: 5px; }
  .stage-card { padding: 7px 9px; }
  .menu-actions {
    gap: 8px;
    margin-bottom: 6px;
  }
  .menu-actions .pixel-btn.primary { padding: 11px 20px; font-size: 13px; }
  .controls-hint { font-size: 7px; padding: 5px 10px; }
  #urg-wrap {
    width: min(460px, 74vw);
  }
}

/* Dual-Screen Hinged Foldables (Viewport Segments API — Surface Duo / iPhone Duo dual-pane!) */
@media (horizontal-viewport-segments: 2) {
  .menu-grid {
    grid-template-columns: env(viewport-segment-width 0 0) env(viewport-segment-width 1 0);
    gap: calc(env(viewport-segment-left 1 0) - env(viewport-segment-right 0 0));
    width: 100%;
    padding: 0 8px;
  }
}

/* ---- 3. Short Landscape Phones (e.g. iPhone 844x390 landscape) ---- */
@media (max-height: 500px) and (orientation: landscape) {
  #title-screen {
    justify-content: flex-start;
    padding: 4px max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
  }
  .title-banner {
    margin: 2px 0 4px 0;
  }
  .title-main { font-size: 18px; }
  .title-sub { display: none; }
  #menu-records { display: none; }
  .menu-grid {
    grid-template-columns: minmax(0, 1.25fr) minmax(0, 0.95fr);
    gap: 8px;
    margin-bottom: 4px;
  }
  .menu-box { padding: 6px 8px; }
  .menu-box h3 { font-size: 8px; margin-bottom: 4px; padding-bottom: 3px; }
  .char-list.five-col { grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 4px; }
  .char-card { padding: 3px; }
  .char-card img.portrait { max-height: 44px; margin-bottom: 2px; }
  .char-card .cname { font-size: 6.2px; }
  .char-card .ctitle, .char-card .cbonus { display: none; }
  .char-detail-bar { font-size: 6.5px; padding: 3px 6px; margin-top: 4px; }
  .relic-section { margin-top: 4px; padding-top: 4px; }
  .relic-header { font-size: 6.2px; margin-bottom: 3px; }
  .relic-pill { padding: 3px 5px; font-size: 6px; }
  .stage-list { gap: 4px; }
  .stage-card { padding: 4px 7px; }
  .stage-card .sname { font-size: 8px; margin-bottom: 2px; }
  .stage-card .ssub { font-size: 6.5px; margin-bottom: 0; }
  .stage-card .sdesc { display: none; }
  .mod-row { margin-top: 5px; gap: 5px; }
  .mod-toggle { padding: 5px 4px; font-size: 6.5px; }
  .menu-actions {
    position: sticky;
    bottom: 0;
    width: 100%;
    padding: 5px 8px max(5px, env(safe-area-inset-bottom)) 8px;
    background: rgba(13, 8, 22, 0.94);
    border-top: 2px solid var(--border-gold);
    gap: 8px;
    margin-bottom: 0;
    z-index: 5;
  }
  .menu-actions .pixel-btn.primary { padding: 8px 18px; font-size: 11px; }
  .menu-actions .pixel-btn.small { padding: 6px 9px; font-size: 8px; }
  .gold-pill { padding: 5px 9px; font-size: 9px; }
  .controls-hint { display: none; }

  /* Compact Landscape HUD & 2-col Level-Up Modal */
  .xp-bar-wrap { height: 14px; }
  #level-badge { font-size: 8px; top: 2px; }
  .hud-top-row { top: 18px; }
  .inv-slot { width: 26px; height: 26px; }
  .inv-slot canvas { width: 20px; height: 20px; }
  #hud-timer { font-size: 15px; }
  #urg-wrap {
    bottom: 6px;
    width: min(360px, 60vw);
    padding: 4px 8px;
  }
  .urg-header { font-size: 7px; margin-bottom: 3px; }
  .urg-bar-outer { height: 10px; }

  .levelup-box {
    width: min(760px, 94vw);
    padding: 10px 14px;
  }
  .levelup-title { font-size: 13px; margin-bottom: 3px; }
  .levelup-sub { font-size: 7.5px; margin-bottom: 8px; }
  .upgrade-choices {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
  }
  .upgrade-card {
    flex-direction: column;
    text-align: center;
    padding: 8px;
    gap: 6px;
  }
  .upgrade-top { flex-direction: column; gap: 4px; }
  .upgrade-icon-box { width: 36px; height: 36px; }
  .upgrade-icon-box canvas { width: 28px; height: 28px; }
  .upgrade-name { font-size: 8px; }
  .upgrade-desc { font-size: 7px; }
  .levelup-footer { margin-top: 8px; }
}
