:root {
  --bg: #0b0a14;
  --panel: #1a1830;
  --panel2: #252244;
  --line: #3d3970;
  --text: #f4f1ff;
  --dim: #a9a3d0;
  --gold: #ffd23f;
  --good: #4fe08a;
  --warn: #ffa53a;
  --danger: #ff4d5e;
  --info: #4fc3ff;
  --font: ui-monospace, 'SF Mono', Menlo, Consolas, 'Liberation Mono', monospace;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: var(--font); overflow: hidden; overscroll-behavior: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; touch-action: none;
}
button { font-family: inherit; color: inherit; cursor: pointer; }
#app { position: fixed; inset: 0; display: flex; flex-direction: column; padding-top: env(safe-area-inset-top); }

/* HUD */
#hud { background: #120f22; border-bottom: 3px solid #000; padding: 6px 8px 4px; display: flex; flex-direction: column; gap: 5px; z-index: 2; }
.hud-row { display: flex; gap: 6px; align-items: stretch; }
.chip {
  background: var(--panel2); border: 2px solid #000; box-shadow: inset 0 -3px 0 rgba(0,0,0,.35);
  padding: 4px 8px; font-weight: 800; font-size: 16px; white-space: nowrap; display: flex; align-items: center; gap: 4px;
}
.chip.money { color: var(--gold); flex: 1.3; }
.chip.depth { color: var(--info); flex: 1; }
.chip.cargo { flex: 1; }
.chip.cargo.full { color: var(--danger); animation: blink .6s steps(2) infinite; }
.chip.btn { flex: 0 0 auto; background: #39345f; min-width: 42px; justify-content: center; }
.bar { flex: 1; position: relative; height: 22px; background: #000; border: 2px solid #000; }
.bar i { position: absolute; left: 0; top: 0; bottom: 0; width: 100%; }
.bar.fuel i { background: linear-gradient(#ffb547, #e07b10); }
.bar.hp i { background: linear-gradient(#6cf09a, #22a456); }
.bar.low i { background: linear-gradient(#ff6a6a, #c0182a); animation: blink .5s steps(2) infinite; }
.bar span { position: absolute; inset: 0; display: flex; align-items: center; padding-left: 6px; font-size: 13px; font-weight: 800; text-shadow: 1px 1px 0 #000, -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000; }
#h-goal { font-size: 12px; color: var(--dim); min-height: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#h-goal b { color: var(--gold); }
#h-goal .ok { color: var(--good); }
@keyframes blink { 50% { opacity: .45; } }

/* Escenario */
#stage { position: relative; flex: 1; min-height: 0; overflow: hidden; }
#cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; image-rendering: pixelated; }
#toasts { position: absolute; top: 8px; left: 8px; right: 8px; display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; }
.toast {
  background: #15132a; border: 2px solid #000; border-left: 6px solid var(--info); padding: 6px 10px; font-size: 13px;
  font-weight: 700; max-width: 520px; box-shadow: 0 3px 0 #000; animation: tin .18s ease-out; line-height: 1.3;
}
.toast.good { border-left-color: var(--good); }
.toast.warn { border-left-color: var(--warn); }
.toast.danger { border-left-color: var(--danger); background: #2a0f16; }
.toast.tip { border-left-color: var(--gold); background: #24200e; }
.toast.out { opacity: 0; transition: opacity .3s; }
@keyframes tin { from { transform: translateY(-8px); opacity: 0; } }
#heat { position: absolute; right: 8px; bottom: 8px; background: var(--danger); color: #fff; font-weight: 900; padding: 4px 8px; border: 2px solid #000; animation: blink .6s steps(2) infinite; }
#ctx-btn {
  position: absolute; left: 50%; bottom: 14px; transform: translateX(-50%); font-size: 18px; font-weight: 900; padding: 10px 18px;
  background: var(--gold); color: #1a1205; border: 3px solid #000; box-shadow: 0 4px 0 #000; animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse { 50% { transform: translateX(-50%) scale(1.06); } }

/* Controles táctiles */
#controls { display: none; }
.touch #controls {
  display: grid; grid-template-columns: 1fr auto 0.75fr; gap: 8px; align-items: stretch;
  background: #120f22; border-top: 3px solid #000; padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  height: min(34vh, 250px); flex: 0 0 auto;
}
#controls button {
  background: #2e2a52; border: 3px solid #000; box-shadow: inset 0 -5px 0 rgba(0,0,0,.4), 0 2px 0 #000;
  border-radius: 10px; font-size: 28px; font-weight: 900; color: #fff; padding: 0; touch-action: none;
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1;
}
#controls button.on { background: #5b52a8; box-shadow: inset 0 3px 0 rgba(0,0,0,.4); transform: translateY(2px); }
#controls small { font-size: 11px; font-weight: 800; margin-top: 4px; color: var(--gold); }
.pad { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr; gap: 6px; }
.b-left { grid-column: 1; grid-row: 1 / 3; }
.b-right { grid-column: 3; grid-row: 1 / 3; }
.b-up { grid-column: 2; grid-row: 1; font-size: 22px !important; }
.b-down { grid-column: 2; grid-row: 2; background: #4a3a1a !important; }
.b-down.on { background: #7a5a20 !important; }
.tools { display: flex; flex-direction: column; gap: 6px; width: 58px; }
.tools button { flex: 1; font-size: 22px !important; position: relative; }
.tools small { position: absolute; right: 3px; bottom: 2px; color: #fff !important; font-size: 12px !important; }
.tools button.off { opacity: .45; }
.thrust { display: flex; }
.b-thrust { flex: 1; background: #1f4a5e !important; font-size: 36px !important; }
.b-thrust.on { background: #2f7a9a !important; }

@media (orientation: landscape) and (max-height: 520px) {
  .touch #controls {
    position: absolute; left: 0; right: 0; bottom: 0; height: 46vh; background: none; border: none; pointer-events: none;
    grid-template-columns: 34vh auto 1fr 22vh; padding: 0 calc(12px + env(safe-area-inset-right)) 10px calc(12px + env(safe-area-inset-left));
  }
  .touch #controls > * { pointer-events: auto; }
  .touch #controls .tools { grid-column: 2; }
  .touch #controls .thrust { grid-column: 4; }
  #controls button { opacity: .85; }
  #hud { padding: 3px 8px; gap: 3px; }
  .chip { font-size: 14px; padding: 2px 6px; }
  .bar { height: 18px; }
  #h-goal { display: none; }
  .toast { font-size: 12px; padding: 4px 8px; }
}

/* Overlays */
.overlay { position: fixed; inset: 0; background: #07060ee6; display: flex; align-items: center; justify-content: center; z-index: 10; padding: 12px; padding-top: calc(12px + env(safe-area-inset-top)); }
.overlay[hidden], [hidden] { display: none !important; }
.panel {
  background: var(--panel); border: 3px solid #000; box-shadow: 0 0 0 3px var(--line), 0 8px 0 #000; padding: 16px;
  width: 100%; max-width: 460px; max-height: 100%; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; touch-action: pan-y;
}
.panel h2 { margin: 0; font-size: 22px; color: var(--gold); }
.panel p { margin: 0; line-height: 1.45; }
.note { font-size: 12px; color: var(--dim); }
button.big {
  font-size: 18px; font-weight: 900; padding: 13px; background: var(--gold); color: #1a1205; border: 3px solid #000; box-shadow: 0 4px 0 #000;
}
button.big:active { transform: translateY(3px); box-shadow: 0 1px 0 #000; }
button.big.alt { background: #39345f; color: #fff; }
button.big.warn { background: #b8323f; color: #fff; }
button.small { background: #39345f; border: 2px solid #000; font-size: 18px; padding: 4px 10px; }
.title-panel { text-align: center; align-items: stretch; }
.logo {
  margin: 4px 0 0; font-size: 54px; line-height: .95; letter-spacing: 3px; color: var(--gold);
  text-shadow: 4px 4px 0 #b4561a, 8px 8px 0 #000;
}
.tagline { color: var(--info); font-weight: 800; margin-bottom: 6px !important; }
.seed-row { display: flex; gap: 6px; justify-content: center; align-items: center; color: var(--dim); font-size: 14px; }
.seed-row input { width: 120px; background: #000; color: #fff; border: 2px solid var(--line); font-family: inherit; font-size: 16px; padding: 4px 6px; user-select: text; -webkit-user-select: text; }
.help { font-size: 12px; color: var(--dim); text-align: left; line-height: 1.5; background: #0f0d1e; padding: 8px; border: 2px solid #000; }

/* Taller */
.shop-panel { max-width: 560px; height: 100%; max-height: 760px; padding: 12px; }
.shop-head { display: flex; align-items: center; gap: 8px; }
.shop-head h2 { flex: 1; white-space: nowrap; }
.shop-head .chip.money { flex: 0 1 auto; }
.tabs { display: flex; gap: 4px; }
.tabs button { flex: 1; padding: 9px 2px; font-size: 13px; font-weight: 800; background: #2a2650; border: 2px solid #000; }
.tabs button.on { background: var(--gold); color: #1a1205; }
.tab-body { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; padding-right: 2px; min-height: 0; }
.card { background: var(--panel2); border: 2px solid #000; padding: 10px; display: flex; gap: 10px; align-items: center; }
.card.afford { border-color: var(--good); box-shadow: inset 0 0 0 1px var(--good); }
.card .ico { font-size: 28px; width: 40px; text-align: center; flex: 0 0 auto; }
.card .info { flex: 1; min-width: 0; }
.card .name { font-weight: 900; font-size: 16px; }
.card .desc { font-size: 12px; color: var(--dim); margin-top: 2px; line-height: 1.35; }
.card .desc b { color: var(--good); }
.pips { display: flex; gap: 3px; margin-top: 4px; }
.pips i { width: 14px; height: 7px; background: #000; border: 1px solid #555; }
.pips i.on { background: var(--gold); }
.card button.buy {
  flex: 0 0 auto; min-width: 92px; padding: 10px 8px; font-weight: 900; font-size: 15px; background: var(--good); color: #062010; border: 3px solid #000; box-shadow: 0 3px 0 #000;
}
.card button.buy:disabled { background: #4a4766; color: #a9a3d0; box-shadow: none; }
.card button.buy:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 #000; }
.sold { background: #10301e; border: 2px solid #000; padding: 10px; color: var(--good); font-weight: 800; }
.stat-grid { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; font-size: 15px; }
.stat-grid b { color: var(--gold); text-align: right; }
.section-title { font-size: 13px; color: var(--dim); font-weight: 800; margin-top: 4px; }

/* Escala de interfaz (opción Zoom) y foco para gamepad */
:root { --ui: 1; }
#hud, .panel, #toasts { zoom: var(--ui); }
body.pad button:focus { outline: 4px solid #4fc3ff; outline-offset: 2px; position: relative; z-index: 1; }
