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

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

:root {
  --bg: #1a1612;
  --dirt: #3d2f24;
  --dirt-light: #5a4636;
  --rock: #2a2520;
  --gold: #f5c542;
  --ore: #8b7355;
  --accent: #e07a3d;
  --text: #e8dcc8;
  --panel: #2a2218;
  --panel-light: #3a2f22;
  --good: #6abe6a;
  --bad: #b85050;
}

body {
  font-family: 'Trebuchet MS', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

.game {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px;
  position: relative;
}

/* ---------- ZONE THEMES ---------- */
/* Each zone exposes --tunnel-bg / --tunnel-border / --pile-bg / --pile-color
   so .shaft-tunnel, .barrier-tunnel and .ore-pile pick them up via var()
   fallbacks. Falls back to the surface look when no zone is set. */
[data-zone="surface"] {
  --tunnel-bg: linear-gradient(180deg, #5a4636, #2a2520);
  --tunnel-border: #1a1410;
  --pile-bg: #2a2520;
  --pile-color: #f5c542;
  --pile-border: #8b7355;
}
[data-zone="deep"] {
  --tunnel-bg: linear-gradient(180deg, #3a5a45, #15281c);
  --tunnel-border: #0c1a10;
  --pile-bg: #15281c;
  --pile-color: #8be0a0;
  --pile-border: #4a8a60;
}
[data-zone="forge"] {
  --tunnel-bg: linear-gradient(180deg, #6a3520, #3a1010);
  --tunnel-border: #2a0808;
  --pile-bg: #3a1010;
  --pile-color: #ff8060;
  --pile-border: #9a4030;
}
[data-zone="magma"] {
  --tunnel-bg: linear-gradient(180deg, #a04018, #5a1000);
  --tunnel-border: #3a0a00;
  --pile-bg: #5a1000;
  --pile-color: #ffb060;
  --pile-border: #c05030;
}
[data-zone="ice"] {
  --tunnel-bg: linear-gradient(180deg, #4a7aa0, #1a3a5a);
  --tunnel-border: #0a1a3a;
  --pile-bg: #1a3a5a;
  --pile-color: #a0d8ff;
  --pile-border: #5a8ab0;
}
[data-zone="cosmic"] {
  --tunnel-bg: linear-gradient(180deg, #5a2a8a, #2a0a5a);
  --tunnel-border: #1a0a3a;
  --pile-bg: #2a0a5a;
  --pile-color: #d0a0ff;
  --pile-border: #6a4a9a;
}

/* ---------- COMIC FX OVERLAY ---------- */
.fx-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 50;
}
.fx-pow {
  position: absolute;
  font-family: 'Impact', 'Arial Black', sans-serif;
  font-weight: 900;
  font-size: 28px;
  letter-spacing: 1px;
  color: #ffeb3b;
  text-shadow:
    -2px -2px 0 #000, 2px -2px 0 #000,
    -2px 2px 0 #000, 2px 2px 0 #000,
    0 4px 0 #c54e00, 0 6px 8px rgba(0,0,0,0.5);
  transform: rotate(var(--rot, -8deg)) scale(0);
  animation: powAnim 0.7s cubic-bezier(.34, 1.56, .64, 1) forwards;
  white-space: nowrap;
}
@keyframes powAnim {
  0%   { transform: rotate(var(--rot)) scale(0) translateY(0); opacity: 0; }
  20%  { transform: rotate(var(--rot)) scale(1.4) translateY(-8px); opacity: 1; }
  60%  { transform: rotate(var(--rot)) scale(1.0) translateY(-12px); opacity: 1; }
  100% { transform: rotate(var(--rot)) scale(0.85) translateY(-40px); opacity: 0; }
}
.fx-money {
  position: absolute;
  font-weight: 900;
  font-size: 16px;
  color: #ffd84a;
  text-shadow:
    -1.5px -1.5px 0 #000, 1.5px -1.5px 0 #000,
    -1.5px 1.5px 0 #000, 1.5px 1.5px 0 #000,
    0 0 8px rgba(255,200,0,0.6);
  animation: floatupAnim 1.2s ease-out forwards;
  white-space: nowrap;
}
@keyframes floatupAnim {
  0%   { opacity: 0; transform: translateY(0) scale(0.5); }
  20%  { opacity: 1; transform: translateY(-10px) scale(1.1); }
  100% { opacity: 0; transform: translateY(-60px) scale(1); }
}
.fx-ore {
  position: absolute;
  width: 11px; height: 11px;
  background: linear-gradient(135deg, #ffd84a, #b07000);
  border: 1.5px solid #1a1a1a;
  border-radius: 2px;
  transform: translate(-50%, -50%);
  animation: oreFly 0.7s cubic-bezier(.45, 0, .55, 1) forwards;
  box-shadow: 0 1px 0 rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.3);
  z-index: 60;
}
@keyframes oreFly {
  0%   { transform: translate(-50%, -50%) scale(1) rotate(0deg); opacity: 0; }
  10%  { opacity: 1; }
  100% {
    transform:
      translate(calc(-50% + var(--dx)), calc(-50% + var(--dy)))
      scale(0.55) rotate(180deg);
    opacity: 0.2;
  }
}

.cargo-pulse {
  animation: cargoPulse 0.4s ease-out;
}
@keyframes cargoPulse {
  0%   { transform: scale(1);   filter: brightness(1); }
  40%  { transform: scale(1.45); filter: brightness(1.6); }
  100% { transform: scale(1);   filter: brightness(1); }
}

.fx-puff {
  position: absolute;
  width: 24px; height: 24px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(220,210,180,0.9), rgba(220,210,180,0));
  transform: translate(-50%, -50%) scale(0.3);
  animation: puffAnim 0.6s ease-out forwards;
}
@keyframes puffAnim {
  0%   { transform: translate(-50%, -50%) scale(0.3); opacity: 0.9; }
  100% { transform: translate(-50%, -50%) scale(2.5); opacity: 0; }
}

/* ---------- CLICK PULSE ---------- */
.click-pulse {
  animation: clickPulseAnim 0.25s ease-out;
}
@keyframes clickPulseAnim {
  0%   { transform: scale(1); filter: brightness(1); }
  50%  { transform: scale(1.04); filter: brightness(1.4); }
  100% { transform: scale(1); filter: brightness(1); }
}

/* ---------- MANUAL HINTS ---------- */
.shaft.manual:not(.locked) .shaft-tunnel,
.elevator.manual,
.worker.manual,
.processor.manual {
  cursor: pointer;
}
.shaft.manual:not(.locked):not(.full) .shaft-tunnel::after,
.elevator.manual.idle::after,
.processor.manual::after {
  content: '👆';
  position: absolute;
  right: 6px;
  top: 4px;
  font-size: 14px;
  animation: tapHint 1s infinite;
  pointer-events: none;
}
.worker.manual.idle::after {
  content: '👆';
  position: absolute;
  top: -16px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 14px;
  animation: tapHint 1s infinite;
  pointer-events: none;
}
@keyframes tapHint {
  0%, 100% { transform: translateY(0); opacity: 0.7; }
  50%      { transform: translateY(-4px); opacity: 1; }
}
.shaft.manual:not(.locked):hover .shaft-tunnel,
.elevator.manual:hover,
.worker.manual:hover,
.processor.manual:hover {
  filter: brightness(1.15);
}

/* ---------- MULTI-MINER ---------- */
.miners {
  display: flex;
  gap: 4px;
  align-items: flex-end;
  height: 56px;
}
.miner-slot {
  filter: drop-shadow(0 2px 0 rgba(0,0,0,0.5));
}
.miner-extra {
  font-size: 14px;
  font-weight: 900;
  color: var(--gold);
  text-shadow: 1px 1px 0 #000, -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000;
  margin-left: 4px;
  align-self: center;
}

/* ---------- CARTOON MINER (SVG) ---------- */
.char-miner { display: block; }
.char-miner .miner-arm {
  transform-origin: 28px 24px;
  transform-box: fill-box;
}
.shaft.mining .miner-slot .char-miner .miner-arm,
.shaft.manual:not(.locked):not(.full) .miner-slot .char-miner .miner-arm {
  animation: armSwing var(--mine-anim-duration, 0.55s) cubic-bezier(.4,.05,.6,1) infinite;
}
@keyframes armSwing {
  0%, 100% { transform: rotate(-35deg); }
  45%      { transform: rotate(-35deg); }
  60%      { transform: rotate(40deg); }
  75%      { transform: rotate(40deg); }
}
.shaft.mining .miner-slot,
.shaft.manual:not(.locked):not(.full) .miner-slot {
  animation: bodyBob var(--mine-anim-duration, 0.55s) ease-in-out infinite;
}
@keyframes bodyBob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-2px); }
}

/* ---------- CARTOON HAULER (SVG) ---------- */
.char-hauler { display: block; }
.char-hauler .leg-left  { transform-origin: 16px 36px; transform-box: fill-box; }
.char-hauler .leg-right { transform-origin: 24px 36px; transform-box: fill-box; }
.char-hauler .sack { opacity: 0; transition: opacity 0.15s; }
.worker.has-cargo .char-hauler .sack { opacity: 1; }

.worker:not(.idle) .char-hauler {
  animation: walkBob 0.36s ease-in-out infinite alternate;
}
.worker:not(.idle) .char-hauler .leg-left  { animation: legLeft  0.36s linear infinite; }
.worker:not(.idle) .char-hauler .leg-right { animation: legRight 0.36s linear infinite; }
@keyframes walkBob {
  0%   { transform: translateY(0); }
  100% { transform: translateY(-3px); }
}
@keyframes legLeft {
  0%, 100% { transform: rotate(20deg); }
  50%      { transform: rotate(-20deg); }
}
@keyframes legRight {
  0%, 100% { transform: rotate(-20deg); }
  50%      { transform: rotate(20deg); }
}

/* ---------- ZONE GATEWAY ROW ---------- */
.zone-gateway {
  height: 100px;
  position: relative;
  display: flex;
  align-items: center;
  cursor: pointer;
}
.gateway-tunnel {
  position: absolute;
  left: calc(50% + 22px);
  right: 12px;
  height: 64px;
  top: 18px;
  background:
    linear-gradient(180deg, var(--tunnel-bg, linear-gradient(180deg, var(--accent), #6a3010)));
  border: 2px solid var(--tunnel-border, var(--accent));
  border-left: none;
  border-radius: 0 6px 6px 0;
  display: flex;
  align-items: center;
  padding: 0 14px;
  gap: 12px;
  box-shadow: inset 0 0 12px rgba(0,0,0,0.5), 0 0 12px rgba(245,197,66,0.25);
}
.gateway-tunnel.locked {
  background:
    repeating-linear-gradient(45deg, rgba(0,0,0,0.25) 0 6px, rgba(0,0,0,0) 6px 12px),
    var(--tunnel-bg, linear-gradient(180deg, #6a4838, #3a2010));
  border-color: var(--tunnel-border, #1a0e08);
}
.zone-gateway:hover .gateway-tunnel { filter: brightness(1.15); }
.gateway-icon {
  font-size: 28px;
  filter: drop-shadow(0 2px 0 rgba(0,0,0,0.6));
}
.gateway-info { flex: 1; min-width: 0; }
.gateway-name {
  font-weight: 900;
  font-size: 14px;
  letter-spacing: 0.5px;
  color: #fff;
  text-shadow: 1px 1px 0 #000;
}
.gateway-sub {
  font-size: 11px;
  opacity: 0.75;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.gateway-cost {
  background: var(--accent);
  color: #fff;
  font-weight: 900;
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 13px;
  white-space: nowrap;
  border: 2px solid #1a1a1a;
  text-shadow: 1px 1px 0 rgba(0,0,0,0.5);
}
.gateway-arrow {
  font-size: 24px;
  font-weight: 900;
  color: var(--gold);
  text-shadow: 1px 1px 0 #000;
}
.zone-gateway.unlocked .gateway-tunnel {
  background: var(--tunnel-bg, linear-gradient(180deg, #5a4030, #2a1810));
  border-color: var(--tunnel-border, var(--good));
  box-shadow: inset 0 0 12px rgba(0,0,0,0.4), 0 0 8px rgba(106,190,106,0.25);
}

/* ---------- BARRIER ROW ---------- */
.barrier {
  height: 100px;
  position: relative;
  display: flex;
  align-items: center;
}
.barrier-tunnel {
  position: absolute;
  left: calc(50% + 22px);
  right: 12px;
  height: 64px;
  top: 18px;
  background:
    repeating-linear-gradient(45deg, rgba(0,0,0,0.25) 0 6px, rgba(0,0,0,0) 6px 12px),
    var(--tunnel-bg, linear-gradient(180deg, #6a4838, #3a2010));
  border: 2px solid var(--tunnel-border, #1a0e08);
  border-left: none;
  border-radius: 0 6px 6px 0;
  display: flex;
  align-items: center;
  padding: 0 14px;
  gap: 12px;
  cursor: pointer;
  box-shadow: inset 0 0 12px rgba(0,0,0,0.6);
}
.barrier-tunnel:hover { filter: brightness(1.15); }
.barrier-icon {
  font-size: 30px;
  filter: drop-shadow(0 2px 0 rgba(0,0,0,0.6));
}
.barrier-info { flex: 1; min-width: 0; }
.barrier-name {
  font-weight: 900;
  font-size: 14px;
  letter-spacing: 0.5px;
  color: #f5d4a0;
  text-shadow: 1px 1px 0 #000;
}
.barrier-desc {
  font-size: 11px;
  opacity: 0.7;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.barrier-action {
  background: var(--bad);
  color: #fff;
  font-weight: 900;
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 13px;
  white-space: nowrap;
  border: 2px solid #1a1a1a;
  text-shadow: 1px 1px 0 rgba(0,0,0,0.5);
}
.barrier-tunnel:hover .barrier-action { background: #d46060; }
.barrier-cost { color: #fff; }

/* ---------- MANAGER UPGRADE STYLE ---------- */
.upgrade-manager {
  background: linear-gradient(135deg, #4a3520, #5a4030);
  border-color: var(--accent);
}
.upgrade-manager .upgrade-name {
  color: var(--gold);
}
.upgrade-manager.affordable {
  box-shadow: 0 0 12px rgba(245,197,66,0.4);
}

.hud {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 20px;
  background: var(--panel);
  border-radius: 8px;
  border: 2px solid var(--panel-light);
  margin-bottom: 12px;
}

.money {
  font-size: 28px;
  font-weight: bold;
  color: var(--gold);
  text-shadow: 0 2px 0 #000;
}

.rate {
  font-size: 14px;
  color: var(--good);
}

.reset-btn,
.hud-btn {
  background: var(--panel-light);
  color: var(--text);
  border: 1px solid #555;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  opacity: 0.7;
}
.reset-btn { margin-left: auto; }
.hud-btn { font-size: 18px; padding: 4px 10px; line-height: 1; }
#lang-btn { margin-left: auto; }
.hud-btn-lang { font-size: 13px; font-weight: bold; letter-spacing: 1px; padding: 4px 10px; }
.reset-btn:hover,
.hud-btn:hover { opacity: 1; background: var(--accent); }

/* ---------- ZONE TABS ---------- */
.zone-tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 4px 0 8px;
  margin-bottom: 8px;
  scrollbar-width: thin;
  scrollbar-color: var(--panel-light) transparent;
  -webkit-overflow-scrolling: touch;
}
.zone-tabs::-webkit-scrollbar { height: 6px; }
.zone-tabs::-webkit-scrollbar-track { background: transparent; }
.zone-tabs::-webkit-scrollbar-thumb { background: var(--panel-light); border-radius: 3px; }

.zone-tab {
  flex: 0 0 auto;
  background: var(--panel);
  border: 2px solid var(--panel-light);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  color: var(--text);
  font-family: inherit;
  min-width: 84px;
  transition: border-color 0.15s, background 0.15s, transform 0.05s;
}
.zone-tab:active { transform: translateY(1px); }
.zone-tab:hover:not(.locked) { background: var(--panel-light); }
.zone-tab.active {
  border-color: var(--accent);
  background: var(--panel-light);
  box-shadow: 0 0 0 1px var(--accent), 0 2px 8px rgba(0,0,0,0.4);
}
.zone-tab.locked {
  opacity: 0.55;
  border-style: dashed;
  cursor: not-allowed;
}
.zone-tab.locked.afford {
  opacity: 1;
  cursor: pointer;
  border-color: var(--good);
  border-style: solid;
  animation: zoneAffordPulse 1.5s ease-in-out infinite;
}
@keyframes zoneAffordPulse {
  0%, 100% { box-shadow: 0 0 0 1px var(--good), 0 0 0 rgba(106,190,106,0); }
  50%      { box-shadow: 0 0 0 1px var(--good), 0 0 12px rgba(106,190,106,0.6); }
}
.zt-icon {
  font-size: 20px;
  line-height: 1;
  filter: drop-shadow(0 1px 0 rgba(0,0,0,0.5));
}
.zt-name {
  font-size: 11px;
  letter-spacing: 0.3px;
  font-weight: bold;
  white-space: nowrap;
}
.zt-cost {
  font-size: 10px;
  color: var(--gold);
  font-weight: bold;
  white-space: nowrap;
}
.zone-tab.locked.afford .zt-cost { color: var(--good); }
.zt-rate {
  font-size: 9px;
  color: var(--ore);
  opacity: 0.6;
}

/* ---------- PROCESSOR STEAM ---------- */
.proc-steam {
  position: absolute;
  top: -2px;
  left: 50%;
  transform: translateX(-50%);
  width: 16px;
  height: 16px;
  pointer-events: none;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(220,210,200,0.7), rgba(220,210,200,0));
  opacity: 0;
}
.processor:not(.idle) .proc-steam {
  animation: procSteamRise 1.4s ease-out infinite;
}
@keyframes procSteamRise {
  0%   { transform: translate(-50%, 0) scale(0.6); opacity: 0; }
  20%  { opacity: 0.7; }
  100% { transform: translate(-50%, -22px) scale(1.4); opacity: 0; }
}

/* ---------- BUFFS BAR (HUD) ---------- */
.buffs-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  flex: 1;
  justify-content: flex-end;
  margin-right: 6px;
}
.buff-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: linear-gradient(135deg, rgba(245,197,66,0.25), rgba(224,122,61,0.18));
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 11px;
  font-weight: bold;
  color: var(--gold);
  text-shadow: 0 1px 0 #000;
  position: relative;
  animation: buffChipPulse 1.6s ease-in-out infinite;
}
@keyframes buffChipPulse {
  0%, 100% { box-shadow: 0 0 0 rgba(245,197,66,0); }
  50%      { box-shadow: 0 0 8px rgba(245,197,66,0.55); }
}
.buff-icon { font-size: 14px; line-height: 1; }
.buff-mul { color: #fff; }
.buff-time { opacity: 0.75; font-size: 10px; min-width: 26px; text-align: right; }
.buff-stack {
  position: absolute;
  top: -6px; right: -6px;
  background: var(--bad);
  color: #fff;
  font-size: 9px;
  font-weight: 900;
  border-radius: 999px;
  padding: 1px 5px;
  border: 1px solid #1a1a1a;
}

/* ---------- ARTIFACTS ---------- */
.artifact {
  position: absolute;
  width: 38px;
  height: 38px;
  border: none;
  background: transparent;
  cursor: pointer;
  z-index: 60;
  transform: translate(-50%, -50%);
  padding: 0;
  pointer-events: auto;
  filter: drop-shadow(0 0 8px rgba(255,220,120,0.7));
  animation: artifactBob 1.8s ease-in-out infinite, artifactRot 6s linear infinite;
}
.artifact .artifact-icon {
  position: relative;
  z-index: 1;
  display: inline-block;
  font-size: 24px;
  text-shadow: 0 0 8px rgba(255,220,120,0.9), 0 1px 0 #000, 0 -1px 0 #000;
}
.artifact .artifact-glow {
  position: absolute;
  inset: -8px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255,230,120,0.7), rgba(255,160,40,0.2) 60%, transparent 75%);
  animation: artifactPulse 1.4s ease-in-out infinite;
}
.artifact:hover { filter: drop-shadow(0 0 14px rgba(255,255,180,1)); }
.artifact:active { transform: translate(-50%, -50%) scale(0.92); }
.artifact.despawning {
  animation: artifactFade 0.3s ease-out forwards !important;
}
@keyframes artifactBob {
  0%, 100% { transform: translate(-50%, -50%) translateY(0); }
  50%      { transform: translate(-50%, -50%) translateY(-8px); }
}
@keyframes artifactRot {
  from { rotate: 0deg; }
  to   { rotate: 360deg; }
}
@keyframes artifactPulse {
  0%, 100% { opacity: 0.7; transform: scale(1); }
  50%      { opacity: 1.0; transform: scale(1.18); }
}
@keyframes artifactFade {
  to { opacity: 0; transform: translate(-50%, -50%) scale(1.6); }
}

.fx-spark {
  position: absolute;
  width: 6px; height: 6px;
  background: radial-gradient(circle, #fff, #ffe080);
  border-radius: 50%;
  pointer-events: none;
  z-index: 60;
  transform: translate(-50%, -50%);
  animation: sparkFly 0.7s ease-out forwards;
}
@keyframes sparkFly {
  0%   { opacity: 1; transform: translate(-50%, -50%) translate(0, 0) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -50%) translate(calc(cos(var(--ang)) * 36px), calc(sin(var(--ang)) * 36px)) scale(0.4); }
}

/* ---------- SETTINGS MODAL ---------- */
.modal-content.settings { max-width: 460px; text-align: left; }
.modal-content.settings h2 { text-align: center; }
.settings-help {
  font-size: 12px;
  opacity: 0.7;
  margin-bottom: 10px;
}
#save-textarea {
  width: 100%;
  height: 100px;
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: 11px;
  background: #1a1410;
  color: var(--text);
  border: 1px solid var(--panel-light);
  border-radius: 4px;
  padding: 8px;
  resize: vertical;
  margin-bottom: 10px;
  word-break: break-all;
}
.settings-row {
  display: flex;
  gap: 8px;
  justify-content: space-between;
  margin-top: 6px;
}
.settings-row button {
  flex: 1;
  background: var(--panel-light);
  color: var(--text);
  border: 1px solid #555;
  padding: 9px 10px;
  border-radius: 4px;
  font-weight: bold;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}
.settings-row button:hover { background: var(--accent); border-color: var(--accent); }
.settings-row button.danger { color: var(--bad); border-color: #5a2828; }
.settings-row button.danger:hover { background: var(--bad); color: #fff; border-color: var(--bad); }
.settings-status {
  margin-top: 10px;
  text-align: center;
  font-size: 12px;
  color: var(--good);
  min-height: 16px;
  opacity: 0;
}
.settings-status.flash { animation: statusFlash 2s ease-out; }
@keyframes statusFlash {
  0%  { opacity: 0; }
  10% { opacity: 1; }
  80% { opacity: 1; }
  100% { opacity: 0; }
}

.scene {
  background: var(--bg);
  border-radius: 8px;
  border: 2px solid var(--panel-light);
  overflow: hidden;
  position: relative;
}

/* ---------- SCENE BACKDROP (per-zone) ---------- */
.scene-bg {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(180deg, #4a6a8a 0%, #6a8aa0 25%, #8b7355 26%, var(--dirt) 30%, var(--dirt) 100%);
  transition: background 0.4s ease-out;
}
.scene-bg[data-zone="surface"] {
  background:
    linear-gradient(180deg, #4a6a8a 0%, #6a8aa0 25%, #8b7355 26%, #3d2f24 30%, #2a1f15 100%);
}
.scene-bg[data-zone="deep"] {
  background:
    linear-gradient(180deg, #2a3a4a 0%, #3a5060 25%, #4a5a3a 26%, #2a3a25 30%, #15281c 100%);
}
.scene-bg[data-zone="forge"] {
  background:
    radial-gradient(ellipse at 50% 110%, rgba(255,90,30,0.35), transparent 60%),
    linear-gradient(180deg, #2a1010 0%, #4a1a10 25%, #6a3520 26%, #3a1010 30%, #1a0808 100%);
}
.scene-bg[data-zone="magma"] {
  background:
    radial-gradient(ellipse at 50% 110%, rgba(255,140,40,0.55), transparent 50%),
    radial-gradient(ellipse at 30% 90%, rgba(255,80,20,0.4), transparent 40%),
    linear-gradient(180deg, #3a0810 0%, #5a1810 22%, #7a2810 26%, #5a1810 32%, #1a0408 100%);
}
.scene-bg[data-zone="ice"] {
  background:
    radial-gradient(ellipse at 50% 0%, rgba(180,220,255,0.25), transparent 60%),
    linear-gradient(180deg, #2a4a6a 0%, #4a7aa0 25%, #6090b0 26%, #2a4a6a 30%, #15283a 100%);
}
.scene-bg[data-zone="cosmic"] {
  background:
    radial-gradient(ellipse at 30% 20%, rgba(180,140,240,0.25), transparent 60%),
    radial-gradient(ellipse at 70% 80%, rgba(120,80,200,0.2), transparent 60%),
    linear-gradient(180deg, #0a0418 0%, #1a0838 25%, #2a1248 26%, #1a0830 30%, #050208 100%);
}

/* ---------- SCENE PARTICLES (ambient) ---------- */
.scene-particles {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.55;
  background-repeat: repeat;
}
.scene-particles[data-zone="surface"] {
  background-image:
    radial-gradient(circle, rgba(220,200,160,0.5) 0.8px, transparent 1.5px);
  background-size: 90px 90px;
  animation: dustDrift 20s linear infinite;
  opacity: 0.3;
}
.scene-particles[data-zone="deep"] {
  background-image:
    radial-gradient(circle, rgba(160,200,160,0.4) 0.8px, transparent 1.5px);
  background-size: 70px 70px;
  animation: dustDrift 24s linear infinite;
  opacity: 0.25;
}
.scene-particles[data-zone="forge"],
.scene-particles[data-zone="magma"] {
  background-image:
    radial-gradient(circle, #ff9040 1px, transparent 2px),
    radial-gradient(circle, #ffb060 0.7px, transparent 1.5px);
  background-size: 70px 110px, 50px 80px;
  animation: emberRise 5.5s linear infinite;
  opacity: 0.7;
}
.scene-particles[data-zone="magma"] { opacity: 0.85; }
.scene-particles[data-zone="ice"] {
  background-image:
    radial-gradient(circle, rgba(220,240,255,0.85) 0.9px, transparent 1.6px),
    radial-gradient(circle, rgba(180,220,255,0.6) 0.6px, transparent 1.2px);
  background-size: 60px 80px, 90px 110px;
  animation: snowFall 18s linear infinite;
  opacity: 0.6;
}
.scene-particles[data-zone="cosmic"] {
  background-image:
    radial-gradient(circle, #ffffff 0.8px, transparent 1.4px),
    radial-gradient(circle, #d0a0ff 0.6px, transparent 1.2px),
    radial-gradient(circle, #80c0ff 0.5px, transparent 1px);
  background-size: 80px 80px, 130px 130px, 200px 200px;
  animation: starsTwinkle 4s ease-in-out infinite alternate;
  opacity: 0.85;
}
@keyframes emberRise {
  from { background-position: 0 0, 0 0; }
  to   { background-position: 0 -110px, 0 -80px; }
}
@keyframes snowFall {
  from { background-position: 0 0, 0 0; }
  to   { background-position: 0 80px, 0 110px; }
}
@keyframes dustDrift {
  from { background-position: 0 0; }
  to   { background-position: 90px 0; }
}
@keyframes starsTwinkle {
  from { opacity: 0.65; }
  to   { opacity: 0.95; }
}

/* SURFACE */
.surface {
  height: 110px;
  position: relative;
  display: flex;
  align-items: flex-end;
  padding-bottom: 10px;
  border-bottom: 3px solid #2a1f15;
}

.dropoff {
  position: absolute;
  left: 50%;
  bottom: 10px;
  transform: translateX(-50%);
  width: 80px;
  text-align: center;
  z-index: 2;
}
.dropoff .label { font-size: 11px; opacity: 0.7; }
.ore-count {
  background: var(--rock);
  color: var(--gold);
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: bold;
  font-size: 14px;
  border: 1px solid var(--ore);
  display: inline-block;
  min-width: 40px;
}

.walkway {
  position: absolute;
  left: calc(50% + 40px);
  right: 140px;
  bottom: 10px;
  height: 50px;
  background: repeating-linear-gradient(90deg, #5a4636 0 8px, #4a3826 8px 16px);
  border-top: 2px solid #6a5436;
  border-bottom: 2px solid #2a1f15;
}

.worker {
  position: absolute;
  bottom: 0px;
  left: 0;
  width: 44px;
  transition: left 0.05s linear, transform 0.05s linear;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.worker-body {
  filter: drop-shadow(0 2px 0 rgba(0,0,0,0.5));
  display: flex;
  align-items: flex-end;
  height: 48px;
}
.worker-cargo {
  background: var(--gold);
  color: #000;
  font-size: 10px;
  font-weight: bold;
  padding: 1px 4px;
  border-radius: 3px;
  margin-top: -4px;
  min-width: 18px;
  text-align: center;
  opacity: 0;
}
.worker.has-cargo .worker-cargo { opacity: 1; }

.processor {
  position: absolute;
  right: 12px;
  bottom: 8px;
  width: 110px;
  text-align: center;
  background: var(--panel);
  border: 2px solid var(--accent);
  border-radius: 6px;
  padding: 6px 4px;
  z-index: 2;
}
.proc-icon {
  font-size: 32px;
  animation: spin 4s linear infinite;
  display: inline-block;
}
.processor.idle .proc-icon { animation-play-state: paused; }
.processor .label { font-size: 11px; opacity: 0.7; margin: 2px 0; }
.proc-bar {
  height: 6px;
  background: #1a1410;
  border-radius: 3px;
  overflow: hidden;
  margin: 2px 0;
}
.proc-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--gold));
  width: 0;
  transition: width 0.05s linear;
}
.proc-buffer {
  font-size: 11px;
  color: var(--ore);
}

@keyframes spin {
  from { transform: rotate(0); }
  to { transform: rotate(360deg); }
}

/* UNDERGROUND */
.underground {
  position: relative;
  min-height: 360px;
  padding-top: 0;
}

.elevator-shaft {
  position: absolute;
  left: calc(50% - 22px);
  top: 0;
  bottom: 0;
  width: 44px;
  background: linear-gradient(90deg, #1a1410 0 4px, #2a201a 4px calc(100% - 4px), #1a1410 calc(100% - 4px) 100%);
  border-left: 2px solid #1a1410;
  border-right: 2px solid #1a1410;
  z-index: 1;
}
.elevator-shaft::before, .elevator-shaft::after {
  content: '';
  position: absolute;
  top: 0; bottom: 0;
  width: 2px;
  background: #555;
}
.elevator-shaft::before { left: 8px; }
.elevator-shaft::after { right: 8px; }

.elevator {
  position: absolute;
  left: 4px;
  right: 4px;
  height: 36px;
  background: linear-gradient(180deg, #8a7050, #5a4030);
  border: 2px solid #2a1810;
  border-radius: 3px;
  top: 0;
  transition: top 0.05s linear;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  overflow: hidden;
}
.elev-fill {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 0%;
  background: linear-gradient(180deg, #ffd84a 0%, #c08000 60%, #6a3a00 100%);
  border-top: 1px solid #ffeb8a;
  transition: height 0.18s ease-out, background 0.2s;
  pointer-events: none;
  z-index: 0;
  box-shadow: 0 -2px 4px rgba(255,200,0,0.3);
}
.elevator.full .elev-fill {
  background: linear-gradient(180deg, #ffeb3b 0%, #ffa500 60%, #c04000 100%);
  animation: fullFlash 0.6s ease-out infinite alternate;
}
@keyframes fullFlash {
  from { filter: brightness(1); }
  to   { filter: brightness(1.25); }
}
.elev-cargo {
  position: relative;
  z-index: 1;
}
.elev-cargo {
  background: var(--gold);
  color: #000;
  font-size: 10px;
  font-weight: bold;
  padding: 1px 4px;
  border-radius: 3px;
  min-width: 20px;
  text-align: center;
}

.shafts {
  display: flex;
  flex-direction: column;
  padding-top: 30px;
}

.shaft {
  height: 100px;
  position: relative;
  display: flex;
  align-items: center;
}

.shaft-tunnel {
  position: absolute;
  left: calc(50% + 22px);
  right: 12px;
  height: 64px;
  top: 18px;
  background: var(--tunnel-bg, linear-gradient(180deg, var(--dirt-light), var(--rock)));
  border: 2px solid var(--tunnel-border, #1a1410);
  border-left: none;
  border-radius: 0 6px 6px 0;
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
}

.mine-progress {
  flex: 1;
  height: 8px;
  background: #1a1410;
  border-radius: 4px;
  overflow: hidden;
  max-width: 200px;
}
.mine-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--ore), var(--gold));
  width: 0;
  transition: width 0.05s linear;
}

.ore-pile {
  background: var(--pile-bg, var(--rock));
  color: var(--pile-color, var(--gold));
  padding: 2px 8px;
  border-radius: 4px;
  font-weight: bold;
  font-size: 13px;
  border: 1px solid var(--pile-border, var(--ore));
  min-width: 50px;
  text-align: center;
}

.shaft-label {
  position: absolute;
  left: calc(50% - 90px);
  width: 60px;
  text-align: right;
  font-size: 11px;
  opacity: 0.6;
}

.locked-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  cursor: pointer;
  border-radius: 0 6px 6px 0;
  font-size: 13px;
  letter-spacing: 0.5px;
}
.locked-overlay:hover { background: rgba(60,30,0,0.85); }
.shaft:not(.locked) .locked-overlay { display: none; }

/* UPGRADES */
.upgrades {
  margin-top: 12px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 8px;
}

.upgrade {
  background: var(--panel);
  border: 1px solid var(--panel-light);
  border-radius: 6px;
  padding: 10px 12px;
  cursor: pointer;
  user-select: none;
  transition: background 0.1s, transform 0.05s;
}
.upgrade:hover:not(.disabled) {
  background: var(--panel-light);
  border-color: var(--accent);
}
.upgrade:active:not(.disabled) { transform: translateY(1px); }
.upgrade.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.upgrade-name {
  font-weight: bold;
  font-size: 13px;
  display: flex;
  justify-content: space-between;
  margin-bottom: 4px;
}
.upgrade-lv { color: var(--accent); font-size: 11px; }
.upgrade-effect {
  font-size: 11px;
  opacity: 0.7;
  margin-bottom: 6px;
}
.upgrade-cost {
  font-size: 13px;
  color: var(--gold);
  font-weight: bold;
}
.upgrade.affordable .upgrade-cost { color: var(--good); }

.upgrade-bulk {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}
.bulk-btn {
  flex: 1;
  background: rgba(0,0,0,0.25);
  color: var(--text);
  border: 1px solid #555;
  border-radius: 4px;
  padding: 4px 6px;
  font-size: 11px;
  font-weight: bold;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  text-align: center;
  opacity: 0.6;
  transition: background 0.1s, border-color 0.1s, opacity 0.1s;
}
.bulk-btn:hover { background: rgba(255,255,255,0.08); }
.bulk-btn.afford {
  opacity: 1;
  border-color: var(--good);
  color: var(--good);
}
.bulk-btn.afford:hover { background: var(--good); color: #fff; }
.bulk-btn:active { transform: translateY(1px); }

.section-title {
  grid-column: 1 / -1;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 2px;
  opacity: 0.6;
  margin-top: 6px;
  padding-bottom: 2px;
  border-bottom: 1px solid var(--panel-light);
}

/* MODAL */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--panel);
  border: 2px solid var(--accent);
  border-radius: 8px;
  padding: 24px 32px;
  text-align: center;
  max-width: 400px;
}
.modal-content h2 { color: var(--gold); margin-bottom: 12px; }
.offline-earned {
  font-size: 28px;
  color: var(--gold);
  font-weight: bold;
  margin: 16px 0;
}
.modal-content button {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 10px 28px;
  border-radius: 4px;
  font-weight: bold;
  font-size: 14px;
  cursor: pointer;
  margin-top: 8px;
}
.modal-content button:hover { background: #f08a4d; }

/* ---------- MOBILE (<= 600px) ---------- */
/* Compress horizontal layout. Row heights stay at 100px so the elevator
   y-position math in game.js still works — only widths and surface elements
   reflow. Elevator + dropoff move to the left edge so shafts get more room. */
@media (max-width: 600px) {
  body { font-size: 14px; }
  .game {
    padding: 8px;
    padding-left: max(8px, env(safe-area-inset-left));
    padding-right: max(8px, env(safe-area-inset-right));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  /* HUD */
  .hud { padding: 10px 14px; gap: 14px; margin-bottom: 6px; flex-wrap: wrap; }
  .money { font-size: 22px; }
  .rate { font-size: 12px; }
  .reset-btn { font-size: 11px; padding: 5px 10px; }
  .hud-btn { font-size: 16px; padding: 4px 8px; }
  .buffs-bar { gap: 4px; margin-right: 4px; }
  .buff-chip { padding: 2px 7px; font-size: 10px; }
  .buff-icon { font-size: 12px; }
  .buff-time { font-size: 9px; min-width: 22px; }

  /* Artifact: slightly smaller on phones */
  .artifact { width: 34px; height: 34px; }
  .artifact .artifact-icon { font-size: 20px; }

  /* Zone tabs */
  .zone-tabs { gap: 4px; padding: 2px 0 6px; margin-bottom: 6px; }
  .zone-tab { min-width: 70px; padding: 5px 8px; }
  .zt-icon { font-size: 16px; }
  .zt-name { font-size: 10px; }
  .zt-cost { font-size: 9px; }

  /* Settings modal */
  .modal-content.settings { padding: 16px 14px; max-width: calc(100vw - 24px); }
  #save-textarea { height: 80px; font-size: 10px; }
  .settings-row button { padding: 8px 6px; font-size: 12px; }

  /* Surface — dropoff and processor moved to opposite edges */
  .surface { height: 100px; padding-bottom: 8px; }
  .dropoff {
    left: 8px;
    transform: none;
    width: 64px;
  }
  .dropoff .label { font-size: 10px; }
  .ore-count { font-size: 12px; min-width: 36px; padding: 1px 4px; }
  .walkway {
    left: 80px;
    right: 96px;
    height: 42px;
  }
  .processor {
    width: 86px;
    right: 6px;
    padding: 4px 2px;
  }
  .proc-icon { font-size: 24px; }
  .processor .label { font-size: 10px; margin: 1px 0; }
  .proc-buffer { font-size: 10px; }

  /* Underground — elevator moved to left edge to free shaft width */
  .elevator-shaft { left: 14px; width: 38px; }
  .elevator { height: 32px; }
  .elev-cargo { font-size: 9px; min-width: 18px; padding: 1px 3px; }

  .shafts { padding-top: 30px; }

  /* Shafts/barriers — full width to the right of the elevator */
  .shaft-tunnel,
  .barrier-tunnel {
    left: 56px;
    right: 8px;
    height: 64px;
    top: 18px;
    padding: 0 10px;
    gap: 8px;
  }

  /* Label as small chip above the tunnel */
  .shaft-label {
    left: 56px;
    top: 0;
    width: auto;
    text-align: left;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    opacity: 0.75;
    color: var(--gold);
    padding-left: 2px;
  }

  /* Miners — smaller SVGs */
  .miners { height: 50px; gap: 2px; }
  .miner-slot .char-miner { width: 24px; height: 30px; }
  .miner-extra { font-size: 12px; }

  /* Worker / hauler */
  .worker { width: 36px; }
  .worker-body { height: 40px; }
  .worker-body .char-hauler { width: 32px; height: 38px; }
  .worker-cargo { font-size: 9px; padding: 1px 3px; min-width: 16px; }

  /* Barrier (legacy) + Gateway */
  .barrier-icon, .gateway-icon { font-size: 24px; }
  .barrier-name, .gateway-name { font-size: 12px; }
  .barrier-desc, .gateway-sub { font-size: 10px; }
  .barrier-action, .gateway-cost { padding: 5px 9px; font-size: 11px; }
  .gateway-arrow { font-size: 20px; }
  .gateway-tunnel { left: 56px; right: 8px; padding: 0 10px; gap: 8px; }

  /* Mine progress bar — drop the max-width cap so it fills the shaft */
  .mine-progress { max-width: none; }

  /* Ore pile chip */
  .ore-pile { font-size: 11px; min-width: 44px; padding: 2px 6px; }

  /* Upgrades — narrower cards, denser grid */
  .upgrades { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 6px; margin-top: 8px; }
  .upgrade { padding: 8px 10px; }
  .upgrade-name { font-size: 12px; margin-bottom: 3px; }
  .upgrade-effect { font-size: 10px; margin-bottom: 4px; }
  .upgrade-cost { font-size: 12px; }
  .upgrade-bulk { gap: 4px; margin-top: 5px; }
  .bulk-btn { font-size: 10px; padding: 4px 4px; }

  /* Section title */
  .section-title { font-size: 11px; letter-spacing: 1.5px; }

  /* Modal scales to viewport */
  .modal-content {
    padding: 18px 20px;
    max-width: calc(100vw - 32px);
    margin: 0 16px;
  }
  .modal-content h2 { font-size: 18px; }
  .offline-earned { font-size: 22px; margin: 10px 0; }
  .modal-content button { padding: 9px 22px; font-size: 13px; }

  /* FX scale down so they don't dominate a small viewport */
  .fx-pow { font-size: 22px; }
  .fx-money { font-size: 14px; }

  /* Tap feedback (replaces hover-only states for touch) */
  .shaft.manual:not(.locked):active .shaft-tunnel,
  .elevator.manual:active,
  .worker.manual:active,
  .processor.manual:active,
  .upgrade:active:not(.disabled) { filter: brightness(1.2); }
}

/* Very small phones */
@media (max-width: 360px) {
  .money { font-size: 18px; }
  .processor { width: 76px; }
  .walkway { right: 86px; }
  .upgrades { grid-template-columns: repeat(auto-fill, minmax(135px, 1fr)); }
  .upgrade-name { font-size: 11px; }
}

/* Landscape phone — keep things compact vertically */
@media (max-height: 500px) and (orientation: landscape) {
  .surface { height: 90px; }
  .hud { padding: 6px 12px; margin-bottom: 6px; }
  .money { font-size: 18px; }
}

/* Hover effects only on devices that actually have hover */
@media (hover: none) {
  .upgrade:hover:not(.disabled) { background: var(--panel); border-color: var(--panel-light); }
  .shaft.manual:not(.locked):hover .shaft-tunnel,
  .elevator.manual:hover,
  .worker.manual:hover,
  .processor.manual:hover,
  .barrier-tunnel:hover,
  .locked-overlay:hover { filter: none; background: inherit; }
}
