:root {
  --bg: #03030f;
  --panel: #0a0a22;
  --maze-blue: #2121de;
  --pac-yellow: #ffde00;
  --pinky: #ff54a0;
  --inky: #00f0ff;
  --blinky: #ff3c46;
  --neon-grad: linear-gradient(90deg, var(--inky), var(--pinky), var(--pac-yellow));
  --font: 'Press Start 2P', 'Courier New', monospace;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: #e8e8ff;
  font-family: var(--font);
  overflow: hidden;
}

#app {
  height: 100vh;
  height: 100dvh; /* mobile browsers with dynamic chrome */
  position: relative;
}

/* ---- CRT overlay: scanlines + vignette (pure decoration) ---- */
#crt {
  pointer-events: none;
  position: fixed;
  inset: 0;
  z-index: 50;
  background:
    repeating-linear-gradient(0deg,
      rgba(0, 0, 0, 0.18) 0px,
      rgba(0, 0, 0, 0.18) 1px,
      transparent 1px,
      transparent 3px);
}
#crt::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at center,
    transparent 55%, rgba(0, 0, 10, 0.55) 100%);
}

/* ---- Full-viewport board; every control floats above it ---- */
#board-wrap {
  position: absolute;
  inset: 0;
}

#board {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none; /* all touch gestures are handled by the game */
}
#board.grabbing { cursor: grabbing; }

/* ---- Corner overlay panels ---- */
.overlay {
  position: absolute;
  z-index: 15;
  background: rgba(7, 7, 28, 0.88);
  border: 2px solid var(--maze-blue);
  border-radius: 10px;
  box-shadow: 0 0 14px rgba(33, 33, 222, 0.55);
  padding: 9px 11px;
}

#panel-stats { top: 12px; left: 12px; }
#mini-title {
  font-size: 8px;
  letter-spacing: 2px;
  margin-bottom: 8px;
  background: var(--neon-grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 8px rgba(0, 240, 255, 0.45));
  white-space: nowrap;
}
#stats { display: flex; gap: 14px; align-items: center; }
#stats .stat { text-align: left; }
#stats .label { color: var(--inky); margin-bottom: 4px; font-size: 7px; }
#stats .value { color: #fff; font-size: 9px; text-shadow: 0 0 6px var(--pinky); }
#stats .stat:nth-child(1) .value { text-shadow: 0 0 6px var(--inky); }
#stats .stat:nth-child(3) .value { color: var(--pac-yellow); text-shadow: 0 0 6px var(--pac-yellow); }
#energy-bar { color: var(--inky); letter-spacing: 1px; }
#energy-bar.low { color: var(--blinky); }

#panel-nav {
  top: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  gap: 7px;
  flex-wrap: wrap;
  justify-content: flex-end;
  max-width: 60vw;
}

#map-nav {
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px;
}
#map-nav button {
  width: 44px;
  height: 44px;
  padding: 0;
  font-size: 16px;
  line-height: 1;
}

/* ---- Shapes tray (bottom-left) ---- */
#panel-shapes {
  bottom: 12px;
  left: 12px;
  max-width: min(460px, calc(100vw - 24px));
  padding: 8px;
}
#shapes-buttons { display: flex; gap: 8px; }

#shape-tray {
  margin-bottom: 8px;
  max-height: 46vh;
  overflow-y: auto;
  padding-right: 4px;
}
#shape-tray.hidden { display: none; }

.tray-hint {
  font-size: 7px;
  color: var(--pac-yellow);
  text-shadow: 0 0 6px rgba(255, 222, 0, 0.6);
  margin: 2px 0 6px;
  letter-spacing: 1px;
}

.tray-group {
  font-size: 7px;
  color: var(--inky);
  text-shadow: 0 0 6px rgba(0, 240, 255, 0.6);
  padding: 7px 2px 4px;
  border-bottom: 1px solid rgba(33, 33, 222, 0.5);
  margin-bottom: 5px;
}

.tray-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 4px;
}

.tray-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  width: 64px;
  padding: 5px 2px 4px;
  background: #060618;
  border: 2px solid rgba(33, 33, 222, 0.7);
  border-radius: 8px;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none; /* drag is handled with pointer events */
}
.tray-tile:hover {
  border-color: var(--inky);
  box-shadow: 0 0 10px rgba(0, 240, 255, 0.5);
}
.tray-tile:active { cursor: grabbing; }
.tray-tile.selected {
  border-color: var(--pac-yellow);
  box-shadow: 0 0 14px rgba(255, 222, 0, 0.6);
}
.tray-tile.selected span { color: var(--pac-yellow); }
.tray-tile canvas {
  image-rendering: pixelated;
  background: #03030f;
  border-radius: 4px;
}
.tray-tile span {
  font-size: 6px;
  color: #b8b8f0;
  text-align: center;
  line-height: 1.4;
  word-break: break-word;
}

/* Floating thumbnail that follows the pointer while dragging a shape */
#drag-ghost {
  position: fixed;
  z-index: 60;
  pointer-events: none;
  padding: 4px;
  background: rgba(7, 7, 28, 0.9);
  border: 2px solid var(--pac-yellow);
  border-radius: 8px;
  box-shadow: 0 0 14px rgba(255, 222, 0, 0.6);
}
#drag-ghost.hidden { display: none; }
#drag-ghost.over-map { opacity: 0.35; } /* the board ghost preview takes over */
#drag-ghost canvas { display: block; image-rendering: pixelated; }

/* ---- Toast + connection status ---- */
#toast {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 9px;
  padding: 8px 14px;
  border: 2px solid var(--blinky);
  border-radius: 8px;
  background: rgba(20, 0, 10, 0.85);
  color: var(--blinky);
  text-shadow: 0 0 8px rgba(255, 60, 70, 0.8);
  pointer-events: none;
  z-index: 30;
}
#toast.hidden { display: none; }

#conn-status {
  position: absolute;
  bottom: 10px;
  right: 12px;
  font-size: 8px;
  color: var(--inky);
  text-shadow: 0 0 8px rgba(0, 240, 255, 0.7);
  pointer-events: none;
  z-index: 10;
}
#conn-status.offline {
  color: var(--blinky);
  animation: blink 1.1s steps(2, start) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* ---- Help panel (on demand) ---- */
#help-panel {
  position: absolute;
  top: 64px;
  right: 12px;
  max-width: 440px;
  padding: 14px 16px;
  background: rgba(7, 7, 28, 0.95);
  border: 2px solid var(--maze-blue);
  border-radius: 10px;
  box-shadow: 0 0 18px rgba(33, 33, 222, 0.7);
  font-size: 8px;
  line-height: 1.9;
  z-index: 20;
}
#help-panel.hidden { display: none; }
#help-panel .help-title {
  color: var(--pac-yellow);
  text-shadow: 0 0 8px rgba(255, 222, 0, 0.6);
  margin-bottom: 8px;
  font-size: 9px;
}
#help-panel ul { list-style: none; }
#help-panel li { margin-bottom: 4px; color: #b8b8f0; }
#help-panel b { color: var(--inky); }

/* ---- Buttons & inputs ---- */
button {
  font-family: var(--font);
  font-size: 8px;
  color: #e8e8ff;
  background: #11113a;
  border: 2px solid var(--maze-blue);
  border-radius: 8px;
  padding: 8px 10px;
  cursor: pointer;
  transition: transform 0.08s, box-shadow 0.15s, border-color 0.15s;
  text-shadow: 0 0 4px rgba(0, 240, 255, 0.4);
  touch-action: manipulation; /* no double-tap zoom on rapid presses */
  user-select: none;
  -webkit-user-select: none;
}

button:hover {
  border-color: var(--inky);
  box-shadow: 0 0 12px rgba(0, 240, 255, 0.5);
  transform: translateY(-1px);
}

button:active { transform: translateY(1px); }

button.active {
  border-color: var(--pac-yellow);
  color: var(--pac-yellow);
  box-shadow: 0 0 14px rgba(255, 222, 0, 0.55);
  text-shadow: 0 0 6px rgba(255, 222, 0, 0.8);
}

.goto-box {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.goto-box input {
  font-family: var(--font);
  font-size: 8px;
  width: 92px;
  padding: 8px 5px;
  color: var(--pac-yellow);
  background: #060618;
  border: 2px solid var(--maze-blue);
  border-radius: 8px;
  outline: none;
  text-align: center;
}
.goto-box input:focus {
  border-color: var(--inky);
  box-shadow: 0 0 10px rgba(0, 240, 255, 0.4);
}

/* ---- Life builder modal ---- */
#builder-modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 8, 0.8);
}
#builder-modal.hidden { display: none; }

#builder-card {
  background: var(--panel);
  border: 3px solid var(--maze-blue);
  border-radius: 12px;
  box-shadow: 0 0 24px rgba(33, 33, 222, 0.8);
  padding: 16px;
  text-align: center;
}

.builder-title {
  font-size: 10px;
  color: var(--pac-yellow);
  text-shadow: 0 0 8px rgba(255, 222, 0, 0.6);
  margin-bottom: 12px;
}

#builder-grid {
  display: grid;
  grid-template-columns: repeat(16, 1fr);
  grid-template-rows: repeat(16, 1fr);
  width: min(384px, 86vw);
  aspect-ratio: 1 / 1;
  gap: 1px;
  background: #1a1a4a;
  border: 2px solid var(--maze-blue);
  border-radius: 6px;
  padding: 2px;
  margin: 0 auto 12px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.bcell {
  background: #060618;
  border-radius: 3px;
  cursor: pointer;
}
.bcell:hover { background: #15154a; }
.bcell.on {
  background: var(--pac-yellow);
  box-shadow: 0 0 6px rgba(255, 222, 0, 0.8);
}

.builder-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
}

/* ---- Small screens (phones) ---- */
@media (max-width: 700px) {
  .overlay { padding: 7px 8px; }
  #panel-stats { top: 8px; left: 8px; }
  #mini-title { display: none; }
  #stats { gap: 9px; }
  #stats .label { font-size: 6px; margin-bottom: 3px; }
  #stats .value { font-size: 7px; }

  #panel-nav {
    top: auto;
    bottom: 8px;
    right: 8px;
    max-width: 70vw;
    gap: 5px;
  }
  #conn-status {
    bottom: auto;
    top: 10px;
    right: 12px;
  }
  .goto-box input { width: 58px; padding: 8px 4px; }
  button { font-size: 7px; padding: 8px 8px; }

  #map-nav { right: 8px; }

  #panel-shapes {
    bottom: 8px;
    left: 8px;
    max-width: calc(100vw - 16px);
  }
  #shape-tray { max-height: 38vh; }

  #help-panel {
    left: 8px;
    right: 8px;
    top: 56px;
    max-width: none;
    font-size: 7px;
  }

  #builder-card { padding: 12px; max-width: 94vw; }
  .builder-title { font-size: 9px; }
}
