:root {
  --bg: #000;
  --fg: #f5f5f5;
  --muted: #8f98c4;
  --accent: #ffd400;
  --red: #ff3b3b;
  --cyan: #00e5ff;
  --wall: #2f5bff;
  --wall-dark: #0b1446;
  --pad: clamp(44px, min(16vw, 8.5dvh), 72px);
  --font: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
}

body {
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

/* ---------- Layout (portrait first) ---------- */

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 640px;
  margin: 0 auto;
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
}

/* ---------- HUD ---------- */

.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 12px 4px;
}

.hud-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.hud-high {
  align-items: center;
}

.hud-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.hud-1up {
  color: var(--red);
  animation: blink 1.2s step-end infinite;
}

.hud-value {
  font-size: 18px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 0 6px rgba(255, 255, 255, 0.25);
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  background: transparent;
  border: 2px solid var(--wall);
  border-radius: 10px;
}

.icon-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.pause-icon {
  width: 12px;
  height: 14px;
  border-left: 4px solid var(--fg);
  border-right: 4px solid var(--fg);
}

/* ---------- Board ---------- */

.board {
  position: relative;
  flex: 1 1 0;
  min-height: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  touch-action: none;
}

/* Faint CRT scanlines over the playfield. */
.board::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  background: repeating-linear-gradient(
    to bottom,
    rgba(255, 255, 255, 0.035) 0 1px,
    transparent 1px 3px
  );
}

#game-canvas {
  display: block;
}

/* Menu screens cover the whole viewport, not just the board, so they get
   the space the HUD and D-pad would otherwise take on small phones. */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding:
    max(12px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
  background: rgba(0, 0, 0, 0.7);
}

.overlay[hidden] {
  display: none;
}

/* Title and buttons always stay visible; only the body scrolls if needed. */
.panel {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 340px;
  max-height: 100%;
  overflow: hidden;
  padding: 18px 20px;
  text-align: center;
  background: rgba(5, 8, 30, 0.95);
  border: 2px solid var(--wall);
  border-radius: 16px;
  box-shadow: 0 0 24px rgba(47, 91, 255, 0.35);
}

.panel > * {
  flex: none;
}

.panel > .overlay-body {
  flex: 0 1 auto;
  min-height: 0;
  margin-bottom: 4px;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
}

.title {
  margin: 0 0 8px;
  font-size: 28px;
  letter-spacing: 0.15em;
  color: var(--accent);
}

.title.logo {
  font-size: clamp(34px, 11vw, 46px);
  text-shadow: 3px 3px 0 #ff7a00, 6px 6px 0 rgba(255, 59, 59, 0.55);
}

.title.danger {
  color: var(--red);
  text-shadow: 0 0 12px rgba(255, 59, 59, 0.5);
}

.message {
  margin: 0 0 14px;
  line-height: 1.5;
  white-space: pre-line;
}

.message:empty {
  display: none;
}

.overlay-body:empty {
  display: none;
}

.panel-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.btn {
  min-height: 46px;
  padding: 0 20px;
  font-size: 15px;
  font-weight: 700;
  color: #000;
  background: var(--accent);
  border: 0;
  border-radius: 12px;
}

.btn-secondary {
  color: var(--fg);
  background: transparent;
  border: 2px solid #4b5aa0;
}

.hint {
  margin: 14px 0 0;
  font-size: 11px;
  line-height: 1.6;
  color: var(--muted);
}

.hint[hidden] {
  display: none;
}

@media (max-height: 640px) {
  .panel {
    padding: 14px 16px;
  }

  .title.logo {
    font-size: 32px;
  }

  .message {
    margin-bottom: 10px;
  }

  .hint {
    display: none;
  }
}

/* ---------- Title screen pieces ---------- */

.roster {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 10px;
  margin: 2px 0 12px;
  text-align: left;
}

.roster-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
}

.roster-item canvas {
  flex: none;
  width: 28px;
  height: 28px;
}

.roster-role {
  display: block;
  font-size: 11px;
  font-weight: 400;
  letter-spacing: 0;
  color: var(--muted);
}

.legend {
  margin: 0 0 12px;
  font-size: 11px;
  line-height: 1.7;
  color: var(--muted);
}

.legend b {
  color: var(--fg);
}

.scores-title {
  margin: 0 0 6px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.scores {
  margin: 0 0 14px;
  padding: 8px 12px;
  list-style: none;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  border: 1px solid #26307a;
  border-radius: 10px;
}

.scores li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 2px 0;
}

.scores li.current {
  color: var(--accent);
  font-weight: 700;
}

.scores-meta {
  color: var(--muted);
}

.scores li.current .scores-meta {
  color: inherit;
}

.scores-empty {
  margin: 0 0 14px;
  font-size: 12px;
  color: var(--muted);
}

/* ---------- Status bar (lives + level) ---------- */

.status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 28px;
  padding: 4px 12px;
}

.lives {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 20px;
}

.life {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: conic-gradient(var(--accent) 0 60deg, transparent 60deg 120deg, var(--accent) 120deg);
}

.lives.gained .life:last-child {
  animation: pop 0.7s ease-out;
}

.level-info {
  display: flex;
  align-items: center;
  gap: 8px;
}

.level-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.fruit-strip {
  display: block;
  height: 18px;
}

/* ---------- Touch controls ---------- */

.controls {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 4px 0 12px;
  touch-action: none;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, var(--pad));
  grid-template-rows: repeat(3, var(--pad));
  grid-template-areas:
    ".    up    ."
    "left .     right"
    ".    down  .";
  gap: 4px;
}

.dpad-btn {
  display: grid;
  place-items: center;
  padding: 0;
  background: var(--wall-dark);
  border: 2px solid var(--wall);
  border-radius: 14px;
  touch-action: none;
}

.dpad-btn::before {
  content: "";
  width: 38%;
  height: 38%;
  background: var(--fg);
  clip-path: polygon(50% 10%, 92% 85%, 8% 85%);
}

.dpad-btn:active {
  background: var(--wall);
}

.dpad-btn.up    { grid-area: up; }
.dpad-btn.down  { grid-area: down; }
.dpad-btn.left  { grid-area: left; }
.dpad-btn.right { grid-area: right; }

.dpad-btn.down::before  { transform: rotate(180deg); }
.dpad-btn.left::before  { transform: rotate(-90deg); }
.dpad-btn.right::before { transform: rotate(90deg); }

/* ---------- Animations ---------- */

@keyframes blink {
  50% { opacity: 0.25; }
}

@keyframes pop {
  0%   { transform: scale(0.2); }
  60%  { transform: scale(1.5); }
  100% { transform: scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  .hud-1up,
  .lives.gained .life:last-child {
    animation: none;
  }
}

/* ---------- Landscape phones: controls beside the maze ---------- */

@media (orientation: landscape) and (max-height: 540px) {
  .app {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto minmax(0, 1fr) auto;
    max-width: none;
  }

  .hud {
    grid-column: 1 / -1;
    padding-top: 4px;
    padding-bottom: 2px;
  }

  .board  { grid-column: 1; grid-row: 2; }
  .status { grid-column: 1; grid-row: 3; min-height: 24px; padding-top: 2px; padding-bottom: 2px; }

  .controls {
    grid-column: 2;
    grid-row: 2 / 4;
    padding: 0 16px;
  }
}

/* ---------- Mouse + keyboard devices don't need the pad ---------- */

@media (hover: hover) and (pointer: fine) {
  .controls {
    display: none;
  }
}
