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

body {
  background-color: #090000;
  background-image: radial-gradient(ellipse 90% 90% at 50% 50%, #150404 20%, #050000 100%);
  background-size: cover;
  background-repeat: no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  overflow: hidden;
}

#game-container {
  position: relative;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  box-shadow:
    0 0 0 2px #3a0000,
    0 0 0 4px #0e0000,
    0 0 0 6px #2a0000,
    0 0 80px #5a000066,
    0 0 160px #3a000044;
}

#game-canvas {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  width: 512px;
  height: 480px;
  background: #000;
}

@media (min-width: 1024px) {
  #game-canvas {
    width: 768px;
    height: 720px;
  }
}

@media (min-width: 1536px) {
  #game-canvas {
    width: 1024px;
    height: 960px;
  }
}

/* ===== TOUCH CONTROLS (hidden on desktop) ===== */
#touch-controls {
  display: none;
  user-select: none;
  -webkit-user-select: none;
}

/* Off-screen input used to open the mobile keyboard during name entry */
#name-input {
  position: fixed;
  top: -200px;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

/* ===== MOBILE / TOUCH SCREEN ===== */
/* any-pointer: coarse catches Android devices where the primary pointer
   may be reported as "fine" (e.g. stylus-capable phones) even though
   a touchscreen is present. hover: none && pointer: coarse would miss them. */
@media (any-pointer: coarse) {

  /* ── PORTRAIT ── */
  body {
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    height: 100svh;
    min-height: 0;
    overflow: hidden;
    gap: 6px;
    padding:
      env(safe-area-inset-top, 6px)
      env(safe-area-inset-right, 8px)
      max(env(safe-area-inset-bottom, 0px), 10px)
      env(safe-area-inset-left, 8px);
  }

  #game-container {
    flex: 1 1 0;
    min-height: 0;
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  #game-canvas {
    max-width: min(95vw, 512px);
    max-height: 100%;
    width: auto;
    height: auto;
    aspect-ratio: 512 / 480;
  }

  #touch-controls {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    width: min(95vw, 512px);
    gap: 6px;
  }

  #touch-shortcuts {
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  .shortcut-group {
    display: flex;
    flex-direction: row;
    gap: 4px;
  }

  #touch-main {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
  }

  /* ── LANDSCAPE ── */
  @media (orientation: landscape) {
    /*
     * Layout:
     *   [ D-PAD + OK/BACK ]   [ CANVAS ]   [ SHORTCUTS (2-col grid) ]
     *
     * #touch-controls is display:contents so its children join the body grid.
     * #touch-main takes the left grid area and stacks dpad + actions vertically.
     */
    body {
      display: grid;
      grid-template-areas: "left canvas right";
      grid-template-columns: auto 1fr auto;
      grid-template-rows: 100%;
      height: 100svh;
      justify-items: center;
      align-items: center;
      gap: 4px 8px;
      padding:
        max(env(safe-area-inset-top, 0px), 4px)
        max(env(safe-area-inset-right, 0px), 16px)
        max(env(safe-area-inset-bottom, 0px), 4px)
        max(env(safe-area-inset-left, 0px), 16px);
    }

    /* Dissolve touch-controls wrapper — children join the body grid directly */
    #touch-controls {
      display: contents;
    }

    /* Left panel: dpad stacked above OK / Back */
    #touch-main {
      grid-area: left;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      gap: 10px;
    }

    #touch-actions {
      flex-direction: column;
      align-items: center;
      justify-content: center;
      gap: 8px;
    }

    #game-container {
      grid-area: canvas;
      width: 100%;
      height: 100%;
      min-width: 0;
      min-height: 0;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    /* Canvas fits within its grid cell; aspect-ratio handles the other dimension */
    #game-canvas {
      width: auto;
      height: auto;
      max-width: 100%;
      max-height: 100%;
      aspect-ratio: 512 / 480;
    }

    /* Right panel: shortcuts in grouped column */
    #touch-shortcuts {
      grid-area: right;
      display: flex;
      flex-direction: column;
      gap: 4px;
      align-content: center;
      justify-content: center;
    }

    .shortcut-group {
      display: flex;
      flex-direction: row;
      gap: 4px;
    }

    /* Slightly smaller controls in landscape so canvas gets maximum space */
    .dpad-btn,
    .dpad-center {
      width: 38px;
      height: 38px;
    }

    .action-btn {
      width: 44px;
      height: 44px;
    }
  }
}

/* Shortcut button row (context-sensitive actions) */
.shortcut-btn {
  flex: 1;
  height: 44px;
  background: linear-gradient(180deg, #383838 0%, #242424 100%);
  border: 1px solid #555;
  border-radius: 8px;
  color: #bcbcbc;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.08), 0 2px 4px rgba(0,0,0,0.5);
  transition: filter 0.06s, transform 0.06s;
}

/* TABS — standalone pill, prominent nav button */
.shortcut-tab {
  flex: none;
  width: 100%;
  height: 54px;
  border-radius: 27px;
  background: linear-gradient(180deg, #2a3a50 0%, #162030 100%);
  border: 1px solid #4a7aaa;
  box-shadow: inset 0 1px 0 rgba(120,180,255,0.15), 0 2px 6px rgba(0,0,0,0.6), 0 0 10px rgba(40,100,180,0.2);
}

.shortcut-tab .shortcut-label {
  color: #90c8ff;
  letter-spacing: 1.5px;
}

.shortcut-tab .shortcut-key {
  color: #4a7ab0;
}

.shortcut-tab:active {
  background: linear-gradient(180deg, #1a5080 0%, #0d2840 100%);
  border-color: #60aaee;
  box-shadow: inset 0 1px 0 rgba(120,180,255,0.2), 0 0 10px rgba(60,140,220,0.4);
}

.shortcut-tab:active .shortcut-label {
  color: #c8e8ff;
}

/* Smaller action-tier buttons (USE, ENVOY) */
.shortcut-btn-sm {
  height: 38px;
  opacity: 0.85;
}

.shortcut-label {
  font-family: 'Silkscreen', monospace;
  font-size: 11px;
  font-weight: 700;
  color: #e8e8e8;
  line-height: 1;
  letter-spacing: 0.5px;
}

.shortcut-key {
  font-family: 'Silkscreen', monospace;
  font-size: 9px;
  font-weight: 400;
  color: #888;
  line-height: 1;
}

.shortcut-btn:disabled {
  opacity: 0.3;
  filter: grayscale(0.6);
  pointer-events: none;
}

.shortcut-btn:active {
  background: linear-gradient(180deg, #4a3a00 0%, #2e2400 100%);
  border-color: #c8a830;
  box-shadow: inset 0 1px 0 rgba(255,220,80,0.15), 0 0 6px rgba(200,168,48,0.3);
  transform: scale(0.95);
}

.shortcut-btn:active .shortcut-label {
  color: #f8d878;
}

.shortcut-btn:active .shortcut-key {
  color: #a07820;
}

/* D-pad layout */
#touch-dpad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}

.dpad-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 3px;
}

.dpad-btn {
  width: 40px;
  height: 40px;
  background: linear-gradient(180deg, #3a3a3a 0%, #222 100%);
  border: 1px solid #585858;
  border-radius: 8px;
  color: #e8b840;
  font-family: 'Silkscreen', monospace;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  line-height: 1;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.1), 0 3px 6px rgba(0,0,0,0.6);
  transition: transform 0.05s, box-shadow 0.05s;
}

.dpad-btn:active {
  background: linear-gradient(180deg, #4a3a00 0%, #2a2000 100%);
  border-color: #d4a830;
  color: #fce870;
  box-shadow: inset 0 1px 0 rgba(255,220,80,0.2), 0 0 8px rgba(200,168,48,0.35);
  transform: scale(0.90);
}

.dpad-center {
  width: 40px;
  height: 40px;
  background: radial-gradient(circle, #1e1e1e 0%, #141414 100%);
  border-radius: 6px;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.7);
}

/* Action buttons (OK / Back) */
#touch-actions {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 14px;
}

.action-btn {
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: 2px solid transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: transform 0.06s, box-shadow 0.06s, filter 0.06s;
}

.action-btn .shortcut-label {
  font-family: 'Silkscreen', monospace;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.5px;
  line-height: 1;
}

.action-hint {
  font-family: 'Silkscreen', monospace;
  font-size: 9px;
  font-weight: 400;
  line-height: 1;
  color: rgba(255,255,255,0.65);
}

.action-btn-a {
  background: radial-gradient(circle at 40% 35%, #00aa00, #004400);
  border-color: #00cc00;
  color: #c8ff40;
  box-shadow: 0 0 0 1px #003300, 0 3px 8px rgba(0,180,0,0.35), inset 0 1px 0 rgba(255,255,255,0.15);
}

.action-btn-b {
  background: radial-gradient(circle at 40% 35%, #cc2200, #5a0000);
  border-color: #ee3300;
  color: #ffaa88;
  box-shadow: 0 0 0 1px #3a0000, 0 3px 8px rgba(200,40,0,0.35), inset 0 1px 0 rgba(255,255,255,0.12);
}

.action-btn:active {
  transform: scale(0.88);
  filter: brightness(1.35);
  box-shadow: none;
}

/* ===== SITE FOOTER ===== */
#site-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  text-align: center;
  padding: 5px 8px 6px;
  font-family: 'Courier New', Courier, monospace;
  font-size: 11px;
  letter-spacing: 1px;
  color: #4a2222;
  pointer-events: none;
  z-index: 10;
  background: linear-gradient(transparent, #06000099 50%);
}

#site-footer a {
  color: #6a3333;
  text-decoration: none;
  pointer-events: all;
  transition: color 0.15s;
}

#site-footer a:hover {
  color: #cc4444;
}

/* When the virtual keyboard is open, hide touch controls so the canvas isn't squashed */
body.keyboard-open #touch-controls {
  display: none;
}

/* Hide footer on touch/mobile to avoid overlapping controls */
@media (hover: none) and (pointer: coarse) {
  #site-footer {
    display: none;
  }
}

/* ===== PAGE CHROME: scanlines, outer frame, corner brackets ===== */
#page-chrome {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 20;
}

/* Outer viewport frame */
#page-chrome::before {
  content: '';
  position: absolute;
  inset: 0;
  border: 2px solid #1e0000;
  box-shadow: inset 0 0 0 1px #0e0000, inset 0 0 80px #3a000022;
}

/* Scanline overlay */
#page-chrome::after {
  content: '';
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent 0px,
    transparent 3px,
    #00000014 3px,
    #00000014 4px
  );
}

/* Corner bracket accents */
.corner {
  position: absolute;
  width: 18px;
  height: 18px;
  border-color: #660000;
  border-style: solid;
}

.corner-tl { top: 10px;    left: 10px;    border-width: 2px 0 0 2px; }
.corner-tr { top: 10px;    right: 10px;   border-width: 2px 2px 0 0; }
.corner-bl { bottom: 10px; left: 10px;    border-width: 0 0 2px 2px; }
.corner-br { bottom: 10px; right: 10px;   border-width: 0 2px 2px 0; }

/* Hide page chrome on touch/mobile — controls already fill the screen */
@media (hover: none) and (pointer: coarse) {
  #page-chrome {
    display: none;
  }
}
