/* El móvil cambia el mundo a retrato en Game.resize(), sin deformar los sprites. */
:root { color-scheme: dark; font-family: "Courier New", monospace; color: #eae5d5; background: #10191d; }
* { box-sizing: border-box; }
body { margin: 0; min-height: 100svh; display: grid; place-items: center; padding: 10px 18px; background: radial-gradient(ellipse at 50% 35%, #233237, #10191d 70%); }
button { font: inherit; cursor: pointer; }
button:disabled { opacity: .45; cursor: default; }
button:focus-visible, canvas:focus-visible { outline: 2px solid #81dec3; outline-offset: 2px; }
button:hover:not(:disabled) { filter: brightness(1.18); }
.arcade { width: min(100%, 1200px, calc((100svh - 220px) * 1.5)); }
.masthead { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin-bottom: 10px; }
.eyebrow { color: #9aabac; font-size: 9px; letter-spacing: 2px; line-height: 1.6; margin: 0 0 8px; }
h1 { font-size: clamp(22px, 4vw, 30px); letter-spacing: -1px; line-height: 1; margin: 0; }
h1 span { color: #f1ba62; margin-left: 9px; }
h1 i { color: #81dec3; font-style: normal; margin-left: 14px; }
.status { font-size: 10px; color: #81dec3; white-space: nowrap; }
.cabinet { border: 1px solid #465252; padding: 6px; background: #192428; box-shadow: 0 18px 65px #0005; }
.hud { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 12px 9px; }
.hud { flex-wrap: wrap; gap: 6px; }
.hud > .power-inventory { display: flex; gap: 6px; }
.power-inventory .inventory > span[aria-hidden] { display: block; font-size: 18px; white-space: nowrap; line-height: 1; }
.power-inventory .inventory b { font-size: 12px; }
.power-inventory .inventory small { font-size: 8px; }
.hud #sound { font-size: 8px; padding: 4px; }
.hud > div { display: grid; gap: 4px; }
.hud > div > span { font-size: 8px; letter-spacing: 1px; color: #9aabac; }
.hud strong { font-size: 20px; font-weight: normal; }
#score { color: #f1ba62; }
#health { color: #ff91a6; letter-spacing: 3px; }
.hud button { border: 1px solid #465252; background: transparent; color: #eae5d5; min-width: 32px; min-height: 36px; }
.hud .expand { font-size: 9px; padding: 0 8px; }
.hud .inventory { display: flex; align-items: center; gap: 7px; padding: 4px 8px; border-radius: 10px; border-style: dashed; opacity: 1; text-align: left; }
.inventory canvas { width: 24px; height: 30px; }
.inventory span { display: grid; gap: 4px; }
.inventory b { font-size: 9px; color: #acbebb; }
.inventory small { font-size: 7px; color: #90a19e; }
.hud .inventory.loaded { border-style: solid; border-color: #f1ba62; background: #f1ba6215; }
.inventory.loaded b { color: #f1ba62; }
.hud .inventory.active { border-color: #ff794b; background: #ff794b20; }
.inventory.active b { color: #ff996e; }
.battlebar { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 6px 12px; padding: 8px 12px; border-top: 1px solid #354247; color: #819b9c; font-size: 9px; }
.battlebar b { color: #c7ddd1; font-weight: normal; }
.battlebar small { margin-left: 6px; font-size: inherit; }
.battlebar .laser-clock b, #boss-phase { color: #ff91a6; }
.screen { position: relative; isolation: isolate; overflow: hidden; border: 1px solid #354247; background: #0c151c; }
canvas { display: block; image-rendering: pixelated; }
#game { width: 100%; height: auto; touch-action: none; }
.overlay { position: absolute; inset: 0; z-index: 5; display: grid; place-items: center; padding: 16px; background: #0710159c; }
[hidden] { display: none !important; }
.start-card { width: min(100%, 360px); padding: 26px; background: #142126f5; border: 1px solid #75806e; box-shadow: 7px 7px 0 #07101580; }
.start-card .eyebrow { color: #f1ba62; font-size: 9px; letter-spacing: 1px; }
h2 { white-space: pre-line; font-family: system-ui, sans-serif; font-size: clamp(30px, 5vw, 47px); letter-spacing: -2px; line-height: 1.02; margin: 18px 0; }
.start-card p:not(.eyebrow) { color: #a8b8b7; font-size: 12px; line-height: 1.7; margin-bottom: 20px; }
.primary { display: flex; justify-content: space-between; width: 100%; border: 0; padding: 15px 18px; background: #f1ba62; color: #172022; font-weight: bold; letter-spacing: 2px; }
/* El overlay se dimensiona respecto al canvas, no al viewport ni al HUD. */
.overlay:has(#upgrade-modal:not([hidden])) { padding: 0; background: rgba(0, 0, 0, .65); overflow: hidden; }
.overlay:has(#upgrade-modal:not([hidden])) > .start-card { display: none; }
#upgrade-modal {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 40%; height: 50%; max-height: 50%; overflow: hidden;
  display: flex; flex-direction: column; gap: 5px; min-height: 0;
  padding: 10px; border: 1px solid #89968b; background: #142126f5;
  box-shadow: 3px 3px 0 #0008; text-align: center;
}
#upgrade-title { margin: 0 0 8px; font: bold clamp(11px, 1.3vw, 17px)/1.25 "Courier New", monospace; letter-spacing: .4px; }
.upgrade-cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; flex: 1; min-height: 0; }
.upgrade-card {
  --accent: #81dec3; display: flex; justify-content: center; align-items: center;
  min-width: 44px; min-height: 44px; padding: 0; border: 0; border-radius: 0;
  color: #eae5d5; background: #0c191f; touch-action: manipulation;
  transition: background-color .12s, box-shadow .12s;
}
.upgrade-damage { --accent: #ff986c; }
.upgrade-card img { display: block; width: 100%; height: 100%; min-height: 0; object-fit: contain; image-rendering: pixelated; pointer-events: none; }
.upgrade-card:hover:not(:disabled), .upgrade-card:focus-visible { filter: none; background: #203239; box-shadow: inset 0 0 10px #ffffff0d; outline-color: var(--accent); }
.upgrade-card:active { background: #30464c; box-shadow: inset 2px 2px 0 #0006; }
#upgrade-hint { margin: 8px 0 0; font-size: 9px; line-height: 1.3; color: #a8b8b7; }
.fullscreen-start { display: block; width: 100%; min-height: 48px; margin-top: 10px; padding: 10px; border: 1px solid #81dec3; background: #81dec315; color: #b4eddc; font-size: 10px; font-weight: bold; line-height: 1.5; }
.start-card .fullscreen-message { color: #ffc76a; font-size: 10px; margin: 10px 0 0; }
.start-card small { display: block; margin-top: 15px; text-align: center; color: #a8b8b7; font-size: 9px; }
.screen-label { display: flex; justify-content: space-between; padding: 7px 12px 0; color: #78918f; font-size: 8px; letter-spacing: 1px; pointer-events: none; }
.controls { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; padding: 9px 12px 3px; color: #a8b8b7; font-size: 9px; }
kbd { display: inline-block; font: inherit; border: 1px solid #506060; padding: 4px 6px; color: #eae5d5; }
.controls button { border: 0; color: #a8b8b7; background: transparent; font-size: 9px; padding: 0; }
footer { display: flex; justify-content: space-between; gap: 14px; margin-top: 8px; color: #879b9e; font-size: 8px; line-height: 1.7; }
.reveal { position: absolute; z-index: 2; top: 36%; left: 50%; transform: translateX(-50%); width: max-content; max-width: 90%; padding: 12px 18px; background: #160b0bf0; border: 1px solid #d63222; color: #ffb19b; text-align: center; font-size: clamp(12px, 2vw, 20px); pointer-events: none; }
.reveal small { display: block; margin-top: 6px; font-size: 9px; }
/* El gamepad móvil ocupa su propia franja y nunca cubre la zona de juego. */
.touch-controls { display: none; grid-template-columns: 1fr 1fr; gap: 12px; padding: 12px; border-top: 1px solid #354247; background: #121e23; }
.touch-controls > div { display: flex; min-width: 0; gap: 10px; }
.touch-controls button { flex: 1; min-width: 0; min-height: 64px; border: 1px solid #a2bdbb77; border-radius: 14px; background: #294046; color: #eae5d5; font-size: 26px; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.touch-controls small { display: block; font-size: 8px; margin-top: 3px; }
.touch-controls .fire { min-height: 70px; color: #ffd179; border-color: #f1ba6277; background: #44341f; }
.touch-controls .pressed { background: #397260; border-color: #81dec3; }
.touch-controls button:disabled { opacity: .42; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.arcade:fullscreen { width: 100%; padding: 4px; display: grid; place-content: center; background: #10191d; }
.arcade:fullscreen .masthead, .arcade:fullscreen footer { display: none; }
.arcade:fullscreen .cabinet { width: min(calc(100vw - 8px), calc((100dvh - 156px) * 1.5)); }
@media (max-height: 560px) and (min-width: 641px) { .arcade { width: min(100%, 900px); } }
@media (max-width: 640px) {
  body { display: block; padding: env(safe-area-inset-top) 0 0; background: #0c151c; }
  .arcade, .arcade:fullscreen, .arcade:fullscreen .cabinet { width: 100%; padding: 0; }
  .masthead { padding: 10px 10px 6px; margin: 0; }
  .masthead .eyebrow, .status, h1 i, footer, .speed-stat, .hud .expand { display: none; }
  h1 { font-size: 22px; }
  .cabinet { border: 0; padding: 0; background: transparent; box-shadow: none; }
  .hud { padding: 5px 10px; gap: 6px; }
  .hud > .speed-stat { display: none; }
  .hud > div > span { font-size: 7px; letter-spacing: 0; }
  .hud strong { font-size: 16px; }
  .hud .inventory { padding: 3px 5px; gap: 3px; }
  .inventory b { font-size: 8px; }
  .inventory small { font-size: 6px; }
  .inventory canvas { width: 20px; height: 25px; }
  .hud #pause { min-width: 40px; min-height: 44px; }
  .battlebar { font-size: 8px; padding: 6px 9px; gap: 5px; }
  .battlebar small { margin-left: 2px; }
  .screen { border: 0; }
  .screen-label { padding: 6px 10px; font-size: 7px; }
  .controls { padding: 0 10px 6px; justify-content: end; }
  .controls > span { display: none; }
  .controls button { min-height: 30px; }
  .start-card { max-width: 310px; padding: 20px; }
  h2 { font-size: 38px; margin: 12px 0; }
  .start-card p:not(.eyebrow) { font-size: 11px; }
}

/* En vertical, el canvas mantiene su proporción y los cuatro botones quedan debajo. */
@media (max-width: 760px), (pointer: coarse) and (orientation: portrait) {
  body { display: block; min-height: 100svh; padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom); background: #0c151c; }
  .arcade { width: 100%; min-height: 100svh; }
  .masthead .eyebrow, .status, h1 i, footer, .speed-stat, .hud .expand { display: none; }
  .masthead { padding: 10px 10px 6px; margin: 0; }
  h1 { font-size: 22px; }
  .cabinet { display: flex; flex-direction: column; width: 100%; border: 0; padding: 0; background: transparent; box-shadow: none; }
  .hud { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)) auto; }
  .hud { grid-template-columns: repeat(3, minmax(0, 1fr)) 44px; }
  .hud > .power-inventory { grid-column: 1 / 4; grid-row: 2; display: flex; }
  .hud .inventory { flex: 1; min-height: 44px; justify-content: center; }
  .hud #pause { grid-column: 4; grid-row: 1; }
  .hud #sound { grid-column: 4; grid-row: 2; min-height: 44px; }
  .battlebar { min-height: 30px; }
  .screen { flex: none; width: 100%; border: 0; }
  #game { width: 100%; height: auto; max-height: none; }
  .touch-controls { display: grid; grid-template-columns: 1fr 1fr; align-items: end; width: 100%; padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); gap: 12px; }
  .touch-controls > div { gap: 10px; }
  .touch-controls button { min-height: clamp(58px, 9vh, 76px); font-size: clamp(23px, 7vw, 32px); }
  .touch-controls .actions { display: grid; grid-template-columns: 1fr; gap: 8px; }
  .touch-powers { display: flex; justify-content: flex-end; gap: 12px; padding-right: 3px; }
  .touch-powers button { position: relative; flex: 0 0 48px; width: 48px; height: 48px; min-height: 48px; border-radius: 50%; font-size: 19px; }
  .touch-powers button > span { display: block; white-space: nowrap; font-size: 16px; line-height: 1; }
  .power-count { position: absolute; right: -3px; top: -3px; min-width: 19px; height: 19px; border-radius: 50%; border: 1px solid #f1ba62; background: #102027; color: #ffe1a6; display: grid; place-items: center; font-size: 11px; }
  .touch-controls .fire { width: 100%; min-height: 64px; }
  .touch-controls .directions button { min-height: 64px; }
  .touch-powers .loaded { border-color: #f1ba62; }
  .touch-powers .active { border-color: #ff794b; background: #622e23; }
  .screen-label { min-height: 22px; }
  .overlay { overflow: auto; }
  .start-card { max-height: 100%; overflow-y: auto; }
  .controls { padding-bottom: 6px; }
  .arcade:fullscreen { display: flex; flex-direction: column; justify-content: flex-start; min-height: 100dvh; height: 100dvh; overflow: hidden; padding: 0; }
  .arcade:fullscreen .cabinet { width: 100%; }
  /* Dos columnas también en retrato; el panel deja visible la mayor parte del campo. */
  #upgrade-modal { width: min(88%, 360px); max-height: 50%; padding: 8px; }
  #upgrade-title { font-size: clamp(12px, 3.4vw, 16px); }
}

/* En teléfonos bajos se reduce el dibujo, conservando los 44 px del botón. */
@media (max-height: 700px) {
  #upgrade-modal { padding: 6px; }
  #upgrade-title { font-size: 12px; margin-bottom: 6px; }
  #upgrade-hint { display: none; }
}

/* Al final de la hoja: sustituye el layout móvil anterior, incluido fullscreen. */
@media (max-width: 768px) {
  body { display: block; height: 100dvh; min-height: 0; padding: 0; overflow: hidden; background: #0c151c; }
  .arcade, .arcade:fullscreen {
    --gamepad-height: 72px;
    position: relative; display: flex; flex-direction: column;
    width: 100%; height: 100dvh; min-height: 0;
    padding: env(safe-area-inset-top, 0px) 0 0; overflow: hidden;
  }
  .cabinet, .arcade:fullscreen .cabinet {
    display: flex; flex: 1; flex-direction: column; min-height: 0; width: 100%;
    padding: 0 0 var(--gamepad-height); border: 0; box-shadow: none; background: transparent;
  }

  /* El título ocupa el hueco de la primera columna del HUD, sin otra fila. */
  .masthead, .arcade:fullscreen .masthead {
    position: absolute; top: calc(env(safe-area-inset-top, 0px) + 4px); left: 8px;
    display: flex; align-items: center; width: calc((100% - 16px) * .3); height: 32px;
    margin: 0; padding: 0; z-index: 2; pointer-events: none;
  }
  .masthead .eyebrow, .status, h1 i, footer, .hud > .speed-stat,
  .hud .expand, .screen-label, .controls { display: none; }
  h1 { font-size: clamp(10px, 3vw, 16px); letter-spacing: -.5px; white-space: nowrap; }
  h1 span { margin-left: 2px; }
  .hud {
    flex: none; display: grid; grid-template-columns: 30% 25% 20% 25%;
    grid-template-rows: 32px 44px; gap: 2px 0; padding: 4px 8px;
  }
  .hud > .score-stat { grid-area: 1 / 2; }
  .hud > .wave-stat { grid-area: 1 / 3; }
  .hud > .health-stat { grid-area: 1 / 4; }
  .hud > .score-stat, .hud > .wave-stat, .hud > .health-stat {
    min-width: 0; gap: 2px; align-content: center; justify-items: center;
  }
  .hud > div > span { font-size: 7px; letter-spacing: 0; }
  .hud strong { font-size: clamp(12px, 3.5vw, 16px); line-height: 1; }
  #health { letter-spacing: 1px; }
  .hud > .power-inventory { grid-area: 2 / 1 / 3 / 3; display: flex; gap: 4px; margin-right: 4px; }
  .hud .inventory { flex: 1; min-width: 0; min-height: 44px; padding: 3px; gap: 3px; justify-content: center; }
  .power-inventory .inventory > span[aria-hidden] { font-size: 15px; }
  .power-inventory .inventory b { font-size: 11px; }
  .power-inventory .inventory small { font-size: 7px; }
  /* Pausa y sonido comparten siempre la segunda fila, a la derecha. */
  .hud #pause { grid-area: 2 / 3; margin-right: 4px; }
  .hud #sound { grid-area: 2 / 4; font-size: 8px; padding: 2px; }
  .hud #pause, .hud #sound { width: auto; min-width: 44px; min-height: 44px; }
  .battlebar {
    flex: none; flex-wrap: nowrap; min-height: 24px; padding: 6px 8px;
    gap: 12px; overflow-x: auto; font-size: 8px; scrollbar-width: none;
  }
  .battlebar > span { flex: none; white-space: nowrap; }

  /* El canvas llena únicamente el espacio entre HUD y gamepad. */
  .screen { flex: 1; min-height: 0; width: 100%; border: 0; }
  #game { position: absolute; inset: 0; width: 100%; height: 100%; max-height: none; }
  .touch-controls {
    position: absolute; bottom: 0; left: 0; z-index: 10;
    display: grid; grid-template-columns: 1fr 1fr; align-items: end;
    width: 100%; height: var(--gamepad-height); margin: 0;
    padding: 7px 10px 0; gap: 12px;
  }
  .touch-controls > .directions { gap: 8px; }
  .touch-controls > .actions { display: flex; justify-content: flex-end; }
  .touch-controls .directions button, .touch-controls .fire { height: 64px; min-height: 64px; font-size: 26px; }
  .touch-controls .fire { flex: 0 1 100px; width: 100px; }

  /* Poderes fuera del flujo: dos círculos sobre Fuego, pegados al lateral. */
  .touch-powers {
    position: absolute; right: 10px; bottom: calc(100% + 10px); z-index: 11;
    display: flex; flex-direction: column; gap: 10px; padding: 0; margin: 0;
  }
  .touch-powers button { position: relative; flex: 0 0 55px; width: 55px; height: 55px; min-height: 55px; border-radius: 50%; font-size: 20px; }
  .touch-powers button > span { font-size: 18px; }
  .power-count {
    position: absolute; top: -3px; right: -3px; display: grid; place-items: center;
    min-width: 19px; height: 19px; border: 1px solid #f1ba62; border-radius: 50%;
    background: #102027; color: #ffe1a6; font-size: 11px;
  }
  /* Los botones flotantes no se superponen a los menús abiertos. */
  .cabinet:has(.overlay:not([hidden])) .touch-powers { visibility: hidden; }
  .overlay { padding: 12px; overflow: auto; }
  .start-card { width: min(100%, 310px); max-height: 100%; padding: 16px; overflow-y: auto; }
  h2 { font-size: clamp(26px, 7vw, 36px); margin: 10px 0; }
  .start-card p:not(.eyebrow) { font-size: 11px; line-height: 1.5; margin-bottom: 12px; }
  #pause-restart { min-height: 44px; margin-top: 8px; }
  #upgrade-modal { width: min(88%, 360px); max-height: 50%; padding: 8px; }
  #upgrade-title { font-size: clamp(12px, 3.4vw, 16px); }
}
