* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #0a0a12;
  font-family: 'Segoe UI', system-ui, sans-serif;
  color: #e0e0e0;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

canvas#game {
  display: block;
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 0;
}

/* ── HUD ── */
#hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  pointer-events: none;
  z-index: 10;
  padding: 8px 12px;
  padding-top: calc(8px + env(safe-area-inset-top, 0px));
  padding-left: calc(12px + env(safe-area-inset-left, 0px));
  padding-right: calc(12px + env(safe-area-inset-right, 0px));
}

#hud-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

#hud-stats {
  display: flex;
  gap: 16px;
  font-size: 14px;
  text-shadow: 0 0 4px #000;
}

#xp-bar-container {
  flex: 1;
  max-width: 400px;
  height: 18px;
  background: rgba(0,0,0,0.6);
  border-radius: 9px;
  position: relative;
  overflow: hidden;
  border: 1px solid #444;
}

#xp-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #6a5acd, #9b59b6);
  border-radius: 9px;
  transition: width 0.3s;
}

#xp-text {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 11px;
  line-height: 18px;
  font-weight: bold;
  text-shadow: 0 0 3px #000;
}

#hp-bar-container {
  width: 160px;
  height: 10px;
  background: rgba(0,0,0,0.6);
  border-radius: 5px;
  position: relative;
  overflow: hidden;
  border: 1px solid #555;
}

#hp-bar {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, #e74c3c, #c0392b);
  border-radius: 5px;
  transition: width 0.2s;
}

#hp-text {
  position: absolute;
  top: -1px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 9px;
  line-height: 12px;
  text-shadow: 0 0 3px #000;
}

/* ── Mobile Controls ── */
#mobile-controls {
  display: none;
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 30;
  pointer-events: none;
}

@media (pointer: coarse) {
  #mobile-controls { display: block; }
}

#joystick-area {
  position: absolute;
  bottom: calc(40px + env(safe-area-inset-bottom, 0px));
  left: calc(20px + env(safe-area-inset-left, 0px));
  pointer-events: auto;
}

canvas#joystick {
  opacity: 0.5;
  display: block;
}

#action-cluster {
  position: absolute;
  bottom: calc(48px + env(safe-area-inset-bottom, 0px));
  right: calc(14px + env(safe-area-inset-right, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
  pointer-events: none;
}

#action-cluster button {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  pointer-events: auto;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  outline: 10px solid transparent;
}

#panic-btn {
  border: 2px solid #7ec8e3;
  background: rgba(100,200,255,0.3);
  color: #7ec8e3;
}

#ult-btn {
  border: 2px solid #c9a0ff;
  background: rgba(150,100,255,0.3);
  color: #c9a0ff;
  font-size: 15px;
}

#action-cluster button.on-cooldown {
  opacity: 0.45;
  border-color: #444;
  background: rgba(40,40,40,0.4);
}

#upgrade-modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(2, 2, 8, 0.96);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
  padding-top: calc(16px + env(safe-area-inset-top, 0px));
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.upgrade-modal-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  max-width: 420px;
  width: 95%;
  gap: 16px;
  margin: 0;
  flex-shrink: 0;
}

#upgrade-modal h2 {
  font-size: 26px;
  margin: 0;
  color: #d4b8ff;
  text-shadow: 0 0 6px rgba(150,100,255,0.3);
  letter-spacing: 1px;
}

#upgrade-choices {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 700px;
}

.upgrade-card {
  position: relative;
  background: rgba(30,25,50,0.9);
  border: 2px solid #6a5acd;
  border-radius: 12px;
  padding: 20px;
  width: 200px;
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s, box-shadow 0.15s;
  text-align: center;
  pointer-events: auto;
}

.upgrade-card:hover {
  transform: translateY(-4px);
  border-color: #9b59b6;
  box-shadow: 0 4px 20px rgba(150,100,255,0.3);
}

.upgrade-card .card-icon {
  font-size: 36px;
  margin-bottom: 8px;
}

.upgrade-card .card-title {
  font-size: 15px;
  font-weight: bold;
  color: #ede0ff;
  margin-bottom: 6px;
  line-height: 1.3;
}

.upgrade-card .card-desc {
  font-size: 12px;
  color: #bbb;
  line-height: 1.5;
}

.upgrade-card .card-rarity {
  font-size: 10px;
  margin-top: 8px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

/* Evolution stat detail line */
.evo-stat-line {
  margin-top: 6px;
  color: #ccc;
  font-size: 11px;
  line-height: 1.5;
}

.rarity-common { color: #aaa; }
.rarity-uncommon { color: #5dade2; }
.rarity-rare { color: #f39c12; }
.rarity-epic { color: #e74c3c; text-shadow: 0 0 3px rgba(231,76,60,0.35); }
.rarity-legendary { color: #e74c3c; }

/* Rarity card borders */
.upgrade-card.rarity-border-common { border-color: #6a5acd; }
.upgrade-card.rarity-border-rare {
  border-color: #f39c12;
  box-shadow: 0 0 6px rgba(243,156,18,0.15);
}
.upgrade-card.rarity-border-epic {
  border-color: #e74c3c;
  box-shadow: 0 0 8px rgba(231,76,60,0.2);
  background: rgba(50,20,30,0.9);
}
.upgrade-card.rarity-border-legendary {
  border-color: #ff6f00;
  box-shadow: 0 0 10px rgba(255,111,0,0.3);
  background: linear-gradient(135deg, rgba(50,30,10,0.95), rgba(60,20,0,0.9));
  animation: legendaryPulse 2s ease-in-out infinite;
}
.rarity-legendary { color: #ff6f00; text-shadow: 0 0 4px rgba(255,111,0,0.4); }
@keyframes legendaryPulse {
  0%, 100% { box-shadow: 0 0 10px rgba(255,111,0,0.3); }
  50% { box-shadow: 0 0 16px rgba(255,111,0,0.45); }
}

/* Cursed rarity */
.rarity-cursed { color: #9b59b6; text-shadow: 0 0 4px rgba(155,89,182,0.4); }
.upgrade-card.rarity-border-cursed {
  border-color: #8e44ad;
  box-shadow: 0 0 8px rgba(142,68,173,0.25), inset 0 0 12px rgba(80,20,80,0.2);
  background: linear-gradient(135deg, rgba(40,10,50,0.95), rgba(30,0,30,0.9));
  animation: cursedPulse 2.5s ease-in-out infinite;
}
@keyframes cursedPulse {
  0%, 100% { box-shadow: 0 0 8px rgba(142,68,173,0.25), inset 0 0 12px rgba(80,20,80,0.2); }
  50% { box-shadow: 0 0 14px rgba(142,68,173,0.4), inset 0 0 18px rgba(80,20,80,0.3); }
}

/* Mastery card styling */
.rarity-mastery { color: #5dade2; text-shadow: 0 0 3px rgba(93,173,226,0.3); }
.upgrade-card.mastery-card {
  border-color: #3498db;
  box-shadow: 0 0 5px rgba(52,152,219,0.15);
  background: rgba(15,30,50,0.9);
}

/* Lock button on upgrade cards */
.lock-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 50%;
  background: rgba(0,0,0,0.4);
  color: #aaa;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s;
  pointer-events: auto;
}
.lock-btn:hover { background: rgba(80,60,120,0.6); border-color: rgba(255,255,255,0.4); }
.lock-btn.locked { background: rgba(100,80,200,0.6); border-color: #ffd700; color: #ffd700; }

/* Reroll button */
#reroll-btn {
  margin-top: 0;
  padding: 10px 28px;
  font-size: 14px;
  background: rgba(80,60,120,0.7);
  color: #ddd;
  border: 1px solid #6a5acd;
  border-radius: 6px;
  cursor: pointer;
  pointer-events: auto;
  transition: background 0.15s, transform 0.1s;
}
#reroll-btn:hover:not(:disabled) {
  background: rgba(120,80,180,0.8);
  transform: scale(1.05);
}
#reroll-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

/* ── Title Screen ── */
#title-screen {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: radial-gradient(ellipse at center, #1a1030 0%, #0a0a12 70%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

#title-screen h1 {
  font-size: 48px;
  color: #c9a0ff;
  text-shadow: 0 0 20px rgba(150,100,255,0.6);
  margin-bottom: 8px;
}

.subtitle {
  color: #888;
  font-size: 16px;
  margin-bottom: 32px;
  font-style: italic;
}

#companion-select h3 {
  text-align: center;
  margin-bottom: 12px;
  color: #bbb;
}

#companion-options {
  display: flex;
  gap: 12px;
  margin-bottom: 24px;
  flex-wrap: wrap;
  justify-content: center;
}

.companion-btn {
  background: rgba(30,25,50,0.8);
  border: 2px solid #555;
  border-radius: 10px;
  padding: 12px 16px;
  color: #ddd;
  cursor: pointer;
  text-align: center;
  min-width: 100px;
  transition: border-color 0.2s, background 0.2s;
}

.companion-btn.selected {
  border-color: #9b59b6;
  background: rgba(100,60,180,0.3);
}

.companion-btn.locked {
  opacity: 0.4;
  cursor: not-allowed;
}

.companion-btn .comp-icon { font-size: 28px; }
.companion-btn .comp-name { font-size: 12px; margin-top: 4px; }

#start-btn, #restart-btn {
  padding: 14px 48px;
  font-size: 18px;
  background: linear-gradient(135deg, #6a5acd, #9b59b6);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  font-weight: bold;
  letter-spacing: 0.5px;
}

#restart-btn {
  margin-top: 6px;
  background: linear-gradient(135deg, rgba(100,70,180,0.7), rgba(140,80,200,0.7));
  border: 1px solid rgba(200,160,255,0.25);
}

#start-btn:hover, #restart-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 20px rgba(150,100,255,0.4);
}

#unlock-info {
  margin-top: 16px;
  font-size: 12px;
  color: #666;
}

/* ── Game Over ── */
#gameover-screen {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(2, 2, 8, 0.97);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 200;
  padding: 24px 16px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#gameover-screen h1 {
  font-size: 32px;
  color: #e74c3c;
  margin-bottom: 10px;
  text-shadow: 0 0 8px rgba(231,76,60,0.3);
  letter-spacing: 2px;
}

#run-stats {
  margin-bottom: 20px;
  font-size: 15px;
  line-height: 1.8;
  text-align: center;
  color: #d0d0d0;
  max-width: 420px;
  width: 100%;
}

#run-stats strong {
  color: #ffffff;
}

.death-build-label {
  font-size: 22px;
  color: #ffd700;
  margin-bottom: 14px;
  font-weight: bold;
  letter-spacing: 1px;
  text-shadow: 0 0 6px rgba(255,215,0,0.25);
}

.death-stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 24px;
  text-align: left;
  font-size: 14px;
  line-height: 1.9;
  margin-bottom: 10px;
}

.death-stat-grid span {
  color: #999;
}

.death-stat-grid strong {
  color: #eee;
}

.death-synergies {
  margin-top: 6px;
  font-size: 13px;
  color: #bbb;
}

.death-breakdown {
  margin-top: 10px;
  font-size: 13px;
  color: #888;
  line-height: 1.6;
  text-align: left;
  max-width: 360px;
  width: 100%;
  border-top: 1px solid rgba(255,255,255,0.08);
  padding-top: 10px;
}

.death-breakdown-entry {
  margin-bottom: 6px;
}

.death-breakdown-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.death-breakdown-name { color: #ccc; }
.death-breakdown-lvl { color: #888; font-size: 11px; }
.death-breakdown-dmg { color: #e0e0e0; font-weight: bold; text-align: right; white-space: nowrap; font-size: 12px; }
.death-breakdown-dmg::after { content: ' dmg'; color: #777; font-weight: normal; font-size: 11px; }

.death-breakdown-mods {
  font-size: 10px;
  color: #999;
  padding-left: 2px;
  margin-top: 1px;
  line-height: 1.4;
}

.hidden {
  display: none !important;
}

/* ── Mobile Portrait Responsive ── */
@media (pointer: coarse) and (max-width: 600px) {
  #hud {
    padding-top: calc(10px + env(safe-area-inset-top, 0px));
    padding-left: calc(10px + env(safe-area-inset-left, 0px));
    padding-right: calc(10px + env(safe-area-inset-right, 0px));
  }

  /* Reflow HUD: XP bar full width on top row, HP + stats on second row */
  #hud-top {
    flex-direction: column;
    align-items: stretch;
    gap: 5px;
    margin-bottom: 0;
  }

  #xp-bar-container {
    max-width: 100%;
    height: 16px;
  }
  #xp-text { font-size: 10px; line-height: 16px; }

  #hud-stats {
    gap: 10px;
    font-size: 12px;
    justify-content: flex-end;
  }

  /* HP bar sits in the stats row on mobile */
  #hp-bar-container {
    width: 100%;
    max-width: 140px;
    height: 8px;
    margin-top: 2px;
  }
  #hp-text { font-size: 8px; line-height: 10px; }

  /* Upgrade modal: respect safe areas, smaller cards */
  #upgrade-modal {
    padding: calc(12px + env(safe-area-inset-top, 0px)) env(safe-area-inset-right, 8px)
             calc(12px + env(safe-area-inset-bottom, 0px)) env(safe-area-inset-left, 8px);
  }
  .upgrade-modal-inner { gap: 10px; }
  #upgrade-modal h2 { font-size: 18px; }

  .upgrade-card {
    width: 44%;
    min-width: 130px;
    max-width: 170px;
    padding: 12px 8px;
  }
  .upgrade-card .card-icon { font-size: 24px; margin-bottom: 4px; }
  .upgrade-card .card-title { font-size: 13px; line-height: 1.2; }
  .upgrade-card .card-desc { font-size: 10px; line-height: 1.4; }
  .upgrade-card .card-rarity { font-size: 9px; }

  #upgrade-choices { gap: 8px; }

  /* Title screen: tighter on narrow phones */
  #title-screen {
    padding: env(safe-area-inset-top, 16px) 16px env(safe-area-inset-bottom, 16px) 16px;
  }
  #title-screen h1 { font-size: 30px; }
  .subtitle { font-size: 13px; margin-bottom: 18px; }
  .companion-btn { padding: 8px 10px; min-width: 80px; }
  .companion-btn .comp-icon { font-size: 22px; }
  .companion-btn .comp-name { font-size: 10px; }

  /* Game over: safe area padding, scrollable */
  #gameover-screen {
    padding: calc(16px + env(safe-area-inset-top, 0px)) 12px
             calc(16px + env(safe-area-inset-bottom, 0px)) 12px;
    justify-content: flex-start;
    padding-top: calc(24px + env(safe-area-inset-top, 0px));
  }
  #gameover-screen h1 { font-size: 24px; margin-bottom: 8px; }
  #run-stats { font-size: 13px; }
  .death-stat-grid { font-size: 12px; gap: 2px 14px; }
  .death-breakdown { font-size: 12px; }
  .death-breakdown-mods { font-size: 9px; }
  .death-build-label { font-size: 17px; margin-bottom: 10px; }
}

/* Short viewport fallback: when content can't fit centered, top-align with padding */
@media (max-height: 580px) {
  #upgrade-modal {
    align-items: flex-start;
    padding-top: calc(20px + env(safe-area-inset-top, 0px));
  }
}

/* ── Touch Debug Mode ── */
body.touch-debug #action-cluster button {
  background: rgba(255,0,0,0.35) !important;
  border: 3px dashed yellow !important;
}
body.touch-debug #action-cluster {
  outline: 2px dashed orange;
}
body.touch-debug #joystick-area {
  outline: 3px dashed lime;
}
body.touch-debug #mobile-controls {
  outline: 1px dashed cyan;
}
