  :root {
    --bg: #05070a;
    --grid: #0d1a14;
    --wall: #1fae6b;
    --hero: #ff3b1f;
    --hero-glow: #ff9d80;
    --enemy: #2bff8c;
    --enemy-glow: #9dffcf;
    --bullet: #fff2b0;
    --ui-text: #d8ffe9;
    --ui-dim: #4d7a63;

    /* Responsive control sizing — scales with the smaller viewport axis so
       phones and tablets both get comfortably-sized touch targets, and
       landscape/portrait share the same formulas. */
    --dpad-size: clamp(140px, 26vmin, 230px);
    --dpad-svg-size: clamp(130px, 24vmin, 214px);
    --nub-size: clamp(44px, 9vmin, 64px);
    --fire-size: clamp(78px, 15vmin, 112px);
    --bomb-size: clamp(50px, 10vmin, 72px);
  }

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

  html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    overflow: hidden;
    height: 100%;
    touch-action: none;
    font-family: 'Press Start 2P', 'Courier New', monospace;
  }

  #game-wrap {
    position: relative;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
  }

  #playfield-zone {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
  }

  #playfield-zone::after {
    content: '';
    position: absolute;
    inset: 0;
    background: repeating-linear-gradient(
      0deg,
      rgba(0,0,0,0.14) 0px,
      rgba(0,0,0,0.14) 1px,
      transparent 1px,
      transparent 3px
    );
    pointer-events: none;
    mix-blend-mode: multiply;
    opacity: 0.6;
  }

  canvas {
    background: var(--bg);
    display: block;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    image-rendering: pixelated;
  }

  /* The 640×480 logical screen: canvas + every HUD overlay. game.js sizes
     and scales this as one unit to fit #playfield-zone, so overlays keep
     their layout math while the whole screen grows on large displays. */
  #screen {
    position: relative;
    flex: none;
    width: 640px;
    height: 480px;
  }
  #game {
    width: 100%;
    height: 100%;
    image-rendering: auto; /* backing store is resized to match display px */
  }

  #hud {
    position: absolute;
    top: 14px;
    left: 16px;
    right: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: var(--ui-text);
    font-size: 16px;
    letter-spacing: 1px;
    text-shadow: 0 0 8px rgba(43,255,140,0.6);
    pointer-events: none;
    user-select: none;
  }

  #hud-hi { color: var(--ui-dim); }

  #hud-left { display: flex; align-items: center; }
  #nests {
    display: inline-flex;
    gap: 5px;
    margin-left: 12px;
    filter: drop-shadow(0 0 5px rgba(43,255,140,0.6));
  }
  .nesticon { width: 13px; height: 13px; display: block; }
  .nesticon polygon {
    fill: rgba(43,255,140,0.15);
    stroke: var(--enemy);
    stroke-width: 2;
  }

  #timer {
    position: absolute;
    top: 40px;
    left: 50%;
    transform: translateX(-50%);
    color: var(--ui-text);
    font-size: 12px;
    letter-spacing: 2px;
    text-shadow: 0 0 8px rgba(43,255,140,0.6);
    pointer-events: none;
    user-select: none;
    display: none;
  }
  #timer.hurry {
    color: var(--hero);
    text-shadow: 0 0 10px rgba(255,59,31,0.8);
    animation: timerPulse 0.5s ease-in-out infinite;
  }
  @keyframes timerPulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
  }

  #inv {
    position: absolute;
    top: 40px;
    left: 16px;
    display: none;
    gap: 6px;
    align-items: center;
    pointer-events: none;
    user-select: none;
  }
  .shieldicon { width: 14px; height: 14px; display: block; filter: drop-shadow(0 0 5px rgba(87,216,255,0.7)); }
  .shieldicon polygon { fill: rgba(87,216,255,0.18); stroke: #57d8ff; stroke-width: 2; }
  .bombicon { width: 14px; height: 14px; display: block; filter: drop-shadow(0 0 5px rgba(255,242,176,0.7)); }
  .bombicon circle { fill: rgba(255,242,176,0.18); stroke: #fff2b0; stroke-width: 2; }
  .bombicon line { stroke: #fff2b0; stroke-width: 2; }
  .inv-gap { width: 6px; }

  #weapon-hud {
    position: absolute;
    top: 40px;
    right: 16px;
    display: none;
    color: var(--ui-text);
    font-size: 10px;
    letter-spacing: 1.5px;
    text-shadow: 0 0 8px rgba(43,255,140,0.6);
    pointer-events: none;
    user-select: none;
  }

  #bossbar {
    position: absolute;
    top: 38px;
    left: 50%;
    transform: translateX(-50%);
    width: 56%;
    display: none;
    text-align: center;
    pointer-events: none;
    user-select: none;
  }
  #bossbar-name {
    font-size: 9px;
    letter-spacing: 2px;
    color: var(--hero);
    text-shadow: 0 0 8px rgba(255,59,31,0.7);
    margin-bottom: 3px;
  }
  #bossbar-track {
    height: 10px;
    border: 2px solid var(--hero);
    background: rgba(255,59,31,0.08);
    box-shadow: 0 0 10px rgba(255,59,31,0.35);
  }
  #bossbar-fill {
    height: 100%;
    width: 100%;
    background: linear-gradient(90deg, rgba(255,59,31,0.85), rgba(255,157,128,0.85));
  }

  #action-cluster {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    flex-shrink: 0;
  }

  #bomb-btn {
    width: var(--bomb-size);
    height: var(--bomb-size);
    border-radius: 50%;
    background: rgba(255,242,176,0.16);
    border: 2px solid #fff2b0;
    color: #fff2b0;
    font-size: 7px;
    letter-spacing: 0;
    line-height: 1.6;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
    user-select: none;
  }
  #bomb-btn.active { background: rgba(255,242,176,0.5); }
  #bomb-btn.off { opacity: 0.3; }

  #lives {
    display: flex;
    gap: 7px;
    align-items: center;
    min-width: 64px;
    justify-content: flex-end;
  }
  .life {
    width: 13px;
    height: 13px;
    border: 2px solid var(--hero);
    background: rgba(255,59,31,0.18);
    box-shadow: 0 0 7px rgba(255,59,31,0.55);
  }

  #toast {
    position: absolute;
    top: 62px;
    left: 50%;
    transform: translateX(-50%);
    color: var(--enemy);
    font-size: 11px;
    letter-spacing: 2px;
    text-shadow: 0 0 10px rgba(43,255,140,0.7);
    pointer-events: none;
    user-select: none;
    display: none;
    white-space: nowrap;
  }

  #status {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--ui-text);
    font-size: 24px;
    letter-spacing: 2px;
    text-align: center;
    text-shadow: 0 0 12px rgba(255,59,31,0.8);
    pointer-events: none;
    user-select: none;
    display: none;
  }
  #status .sub {
    font-size: 12px;
    color: var(--ui-dim);
    margin-top: 14px;
    letter-spacing: 1px;
    line-height: 1.6;
  }

  /* Control panel — a real reserved region below the playfield, cabinet-bezel style */
  #control-panel {
    position: relative;
    flex: 0 0 auto;
    width: 100%;
    height: 230px;
    display: none;
    align-items: center;
    justify-content: space-between;
    padding: 0 26px;
    background: linear-gradient(180deg, #0c1210 0%, #060907 100%);
    border-top: 3px solid var(--enemy);
    box-shadow:
      0 -6px 16px rgba(0,0,0,0.55),
      inset 0 2px 0 rgba(157,255,207,0.15),
      inset 0 -1px 0 rgba(0,0,0,0.4);
    z-index: 10;
  }

  #dpad {
    position: relative;
    width: var(--dpad-size);
    height: var(--dpad-size);
    pointer-events: auto;
    flex-shrink: 0;
  }

  #dpad-hit {
    position: absolute;
    inset: 0;
    border-radius: 50%;
  }

  #dpad-svg {
    position: absolute;
    left: 50%;
    top: 50%;
    width: var(--dpad-svg-size);
    height: var(--dpad-svg-size);
    margin: calc(var(--dpad-svg-size) / -2) 0 0 calc(var(--dpad-svg-size) / -2);
    pointer-events: none;
    overflow: visible;
  }

  #dpad-svg circle {
    fill: rgba(10,20,16,0.4);
    stroke: rgba(43,255,140,0.32);
    stroke-width: 2;
  }

  .dpad-spike {
    fill: var(--enemy);
    opacity: 0.32;
    transition: opacity 0.06s linear;
  }
  .dpad-spike.lit {
    opacity: 1;
    filter: drop-shadow(0 0 5px var(--enemy-glow));
  }

  /* Sliding puck representing current thumb offset, like an analog stick handle */
  #dpad-nub {
    position: absolute;
    left: 50%;
    top: 50%;
    width: var(--nub-size);
    height: var(--nub-size);
    margin: calc(var(--nub-size) / -2) 0 0 calc(var(--nub-size) / -2);
    border-radius: 50%;
    background: rgba(43,255,140,0.28);
    border: 2px solid var(--enemy-glow);
    box-shadow: 0 0 12px rgba(43,255,140,0.5);
    pointer-events: none;
    transform: translate(0px, 0px);
  }

  #fire-btn {
    position: relative;
    width: var(--fire-size);
    height: var(--fire-size);
    flex-shrink: 0;
    border-radius: 50%;
    background: rgba(255,59,31,0.25);
    border: 2px solid var(--hero-glow);
    color: var(--hero-glow);
    font-size: 8px;
    letter-spacing: 0;
    line-height: 1.6;
    text-align: center;
    padding: 0 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
    user-select: none;
  }
  #fire-btn.active {
    background: rgba(255,59,31,0.55);
  }
  #fire-btn.idle {
    animation: fireBtnPulse 1.1s ease-in-out infinite;
  }
  @keyframes fireBtnPulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.55; }
  }

  @media (pointer: coarse) {
    #control-panel { display: flex; }
    #hint-desktop { display: none; }
  }

  /* Portrait phones keep the reserved bezel strip below the screen.
     Landscape (phones rotated, or tablets) instead floats the two control
     clusters over the bottom corners of the playfield so the canvas can
     use the full width — critical once height becomes the scarce axis. */
  @media (pointer: coarse) and (orientation: landscape) {
    #control-panel {
      position: absolute;
      inset: 0;
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: flex-end;
      padding: 0 calc(3vw + env(safe-area-inset-right, 0px)) calc(3vh + env(safe-area-inset-bottom, 0px)) calc(3vw + env(safe-area-inset-left, 0px));
      background: none;
      border: none;
      box-shadow: none;
      pointer-events: none;
      z-index: 10;
    }
    #dpad, #action-cluster { pointer-events: auto; }
    #playfield-zone { width: 100%; }
  }

  #control-panel.swapped {
    flex-direction: row-reverse;
  }

  #settings-btn {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: rgba(43,255,140,0.12);
    border: 1px solid rgba(43,255,140,0.4);
    color: var(--enemy);
    font-family: 'Press Start 2P', monospace;
    font-size: 15px;
    font-weight: bold;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
    cursor: pointer;
    z-index: 30;
    text-shadow: 0 0 6px rgba(43,255,140,0.6);
  }
  #settings-btn:active { background: rgba(43,255,140,0.3); }

  #gamepad-badge {
    position: absolute;
    bottom: 12px;
    right: 14px;
    color: var(--enemy);
    font-size: 9px;
    letter-spacing: 1px;
    text-shadow: 0 0 8px rgba(43,255,140,0.6);
    pointer-events: none;
    user-select: none;
    display: none;
    z-index: 15;
  }
  #gamepad-badge.show { display: block; }

  #settings-overlay {
    position: fixed;
    inset: 0;
    background: rgba(2,4,3,0.82);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 20px;
  }
  #settings-overlay.open { display: flex; }

  #settings-panel {
    width: min(340px, 100%);
    max-height: min(560px, 86vh);
    overflow-y: auto;
    background: linear-gradient(180deg, #0c1210 0%, #060907 100%);
    border: 2px solid var(--enemy);
    box-shadow: 0 0 24px rgba(43,255,140,0.25);
    padding: 22px 20px;
    color: var(--ui-text);
  }

  #settings-title {
    font-size: 16px;
    letter-spacing: 3px;
    color: var(--enemy);
    text-shadow: 0 0 8px rgba(43,255,140,0.6);
    text-align: center;
    margin-bottom: 18px;
  }

  .settings-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 14px;
    font-size: 11px;
    letter-spacing: 1px;
  }
  .settings-row label { flex-shrink: 0; }
  .settings-row input[type="range"] { flex: 1 1 auto; accent-color: var(--enemy); }
  .settings-row input[type="checkbox"] {
    width: 18px; height: 18px;
    accent-color: var(--enemy);
  }
  #vol-readout { width: 28px; text-align: right; color: var(--ui-dim); }

  .settings-row.info-row {
    color: var(--ui-dim);
  }
  .settings-row.info-row span:last-child {
    color: var(--enemy);
  }

  .settings-divider {
    height: 1px;
    background: rgba(43,255,140,0.2);
    margin: 4px 0 16px;
  }

  .settings-subhead {
    font-size: 11px;
    letter-spacing: 2px;
    color: var(--enemy);
    text-shadow: 0 0 6px rgba(43,255,140,0.5);
    margin-bottom: 10px;
  }

  .help-list {
    font-size: 10px;
    line-height: 1.8;
    color: var(--ui-text);
    letter-spacing: 0.5px;
    margin-bottom: 16px;
  }
  .help-list b { color: #fff2b0; }

  .bestiary-list {
    margin-bottom: 4px;
  }
  .bestiary-entry {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 14px;
  }
  .creature-thumb {
    flex-shrink: 0;
    width: 56px;
    height: 56px;
    background: rgba(10,20,16,0.5);
    border: 1px solid rgba(43,255,140,0.35);
    border-radius: 4px;
  }
  .creature-info { flex: 1 1 auto; min-width: 0; }
  .creature-name {
    font-size: 10px;
    letter-spacing: 1.5px;
    color: #fff2b0;
    margin-bottom: 4px;
  }
  .creature-desc {
    font-size: 9px;
    line-height: 1.5;
    color: var(--ui-dim);
    letter-spacing: 0.3px;
  }

  .about-text {
    font-size: 10px;
    line-height: 1.8;
    color: var(--ui-dim);
    letter-spacing: 0.5px;
    margin-bottom: 18px;
  }
  .about-text a {
    color: var(--enemy);
    text-decoration: underline;
    text-shadow: 0 0 6px rgba(43,255,140,0.5);
  }
  .about-text a:active { color: var(--enemy-glow); }

  #settings-close {
    width: 100%;
    padding: 10px;
    background: rgba(43,255,140,0.15);
    border: 1px solid var(--enemy);
    color: var(--ui-text);
    font-family: inherit;
    font-size: 12px;
    letter-spacing: 2px;
    cursor: pointer;
  }
  #settings-close:active { background: rgba(43,255,140,0.4); }

  #hint-desktop {
    position: absolute;
    bottom: 14px;
    left: 50%;
    transform: translateX(-50%);
    color: var(--ui-dim);
    font-size: 11px;
    letter-spacing: 1px;
    pointer-events: none;
    user-select: none;
  }
