:root {
  --bg: #000;
  --card: rgba(18, 21, 26, 0.92);
  --line: rgba(255, 255, 255, 0.1);
  --text: #f1f3f4;
  --muted: #9aa0a6;
  --accent: #46c34f;
  --accent-2: #2e8f36;
  --error: #ff6b6b;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow: hidden;
}

/* Full-window stage; the canvas keeps 16:9 and letterboxes on any screen shape. */
#stage {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: #000;
}

#canvas {
  /* !important: SDL may write inline sizes; CSS owns the display size, SDL owns the pixel size. */
  width: min(100vw, 100dvh * 16 / 9) !important;
  height: min(100dvh, 100vw * 9 / 16) !important;
  display: block;
  outline: none;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
}

/* Fullscreen button: appears on pointer movement, fades when idle. */
#fullscreen {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.55);
  color: var(--text);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.3s;
  z-index: 3;
}
#fullscreen svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
#stage.pointer-active #fullscreen,
#fullscreen:focus-visible { opacity: 1; }
#stage.loading #fullscreen { opacity: 1; }

/* Loading + crash overlays */
#loader, #crash {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 2;
  background:
    radial-gradient(1200px 600px at 50% 120%, rgba(70, 195, 79, 0.18), transparent 60%),
    radial-gradient(800px 400px at 50% -20%, rgba(70, 195, 79, 0.08), transparent 60%),
    #07090b;
  overflow: hidden;
  transition: opacity 0.5s;
}
#crash { z-index: 3; }
#loader.done { opacity: 0; pointer-events: none; }
#crash[hidden] { display: none; }

/* Title-scene art behind the loader (drawn by loader-art.js from the game's files; absent = gradient). */
#loader-art {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  image-rendering: pixelated;
  opacity: 0;
  transition: opacity 0.8s ease;
}
#loader::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse at 50% 55%, transparent 35%, rgba(0, 0, 0, 0.55) 100%),
    linear-gradient(180deg, rgba(0, 0, 0, 0.25), transparent 30%, transparent 60%, rgba(0, 0, 0, 0.6));
  opacity: 0;
  transition: opacity 0.8s ease;
}
#loader.has-art #loader-art,
#loader.has-art::after { opacity: 1; }
#loader > .card { position: relative; z-index: 1; }

#loader.has-art .card {
  background: rgba(10, 12, 16, 0.62);
  border-color: rgba(255, 255, 255, 0.14);
  backdrop-filter: blur(10px) saturate(1.2);
  -webkit-backdrop-filter: blur(10px) saturate(1.2);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.6), inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

#loader-logo {
  display: none;
  width: min(100%, 360px);
  height: auto;
  margin: -8px auto 10px;
  image-rendering: pixelated;
  filter: drop-shadow(0 6px 14px rgba(0, 0, 0, 0.55));
}
#loader.has-art #loader-logo { display: block; }
#loader.has-art .title-text {
  /* the logo replaces the text title; keep the text for screen readers */
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}
#loader.has-art .eyebrow { text-align: center; margin-bottom: 16px; }

.card {
  width: min(520px, 100%);
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--card);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1 {
  margin: 0 0 22px;
  font-size: 40px;
  font-weight: 800;
  letter-spacing: 0.02em;
  line-height: 1;
}
h1 span {
  display: block;
  margin-top: 6px;
  color: var(--accent);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0;
}

#steps {
  display: flex;
  gap: 8px;
  margin: 0 0 16px;
  padding: 0;
  list-style: none;
}
#steps li {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}
#steps li.active { border-color: var(--accent); color: var(--text); }
#steps li.done { color: var(--accent); }
#steps li.done::before { content: "✓ "; }

#bar {
  height: 8px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.08);
  overflow: hidden;
}
#fill {
  width: 0;
  height: 100%;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  transition: width 0.2s;
}
#bar.indeterminate #fill {
  width: 35%;
  animation: slide 1.1s ease-in-out infinite;
}
@keyframes slide {
  from { transform: translateX(-100%); }
  to { transform: translateX(290%); }
}

.meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 13px;
}
#detail { font-variant-numeric: tabular-nums; text-align: right; }

/* Start screen: Quest (just play) or Play to Earn (name + Solana address) */
#start { margin-top: 18px; }
#start[hidden], #start [hidden] { display: none; }
#start .modes { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
#start .mode {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  padding: 14px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  cursor: pointer;
}
#start .mode b { font: 700 15px/1 "Silkscreen", system-ui, sans-serif; letter-spacing: 0.04em; }
#start .mode span { color: var(--muted); font-size: 12px; }
#start .mode.p2e {
  border: 0;
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  color: #04170a;
  animation: pulse 1.6s ease-in-out infinite;
}
#start .mode.p2e span { color: #0b3a14; }
#start .mode:hover { filter: brightness(1.1); }
#start .mode:focus-visible { outline: 2px solid #fff; outline-offset: 3px; }
@keyframes pulse {
  50% { box-shadow: 0 0 0 6px rgba(70, 195, 79, 0.18); }
}
#p2e-form .field { display: block; margin: 0 0 12px; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.1em; }
#p2e-form input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.45);
  color: var(--text);
  font: 700 15px/1.2 "Silkscreen", ui-monospace, monospace;
  letter-spacing: 0.04em;
}
#p2e-wallet { font-size: 12px !important; letter-spacing: 0 !important; font-family: ui-monospace, monospace !important; }
#p2e-form input:focus-visible { outline: 2px solid var(--accent); border-color: var(--accent); }
#p2e-form .hint { margin: 0 0 8px; color: var(--muted); font-size: 12px; text-transform: none; letter-spacing: 0; }
#p2e-form .err { margin: 0 0 10px; min-height: 1em; color: var(--error); font-size: 13px; }

#tip {
  display: flex; flex-direction: column; gap: 4px;
  min-height: 64px; margin: 18px 0 0; padding: 12px 14px;
  border-left: 3px solid var(--accent); border-radius: 0 8px 8px 0;
  background: rgba(255, 255, 255, 0.04); font-size: 14px;
}
#tip[hidden] { display: none; }
#tip b { color: var(--accent); font-size: 11px; letter-spacing: 0.08em; }

/* Crash screen */
#crash h2 { margin: 0 0 6px; color: var(--error); font-size: 20px; }
#crash-msg { margin: 0 0 12px; color: var(--muted); word-break: break-word; }
#crash-log {
  max-height: 40dvh;
  margin: 0 0 16px;
  padding: 10px 12px;
  overflow: auto;
  border-radius: 8px;
  background: #0b0d10;
  color: var(--muted);
  font: 12px/1.35 ui-monospace, Consolas, monospace;
  white-space: pre-wrap;
  word-break: break-word;
}
.actions { display: flex; justify-content: flex-end; gap: 8px; }
.actions button {
  padding: 9px 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
.actions button.primary { border-color: var(--accent); background: var(--accent-2); }

/* ?debug: live log under the game */
#debug-log {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 30dvh;
  margin: 0;
  padding: 8px 12px;
  overflow: auto;
  background: rgba(0, 0, 0, 0.8);
  color: #9aa0a6;
  font: 11px/1.3 ui-monospace, Consolas, monospace;
  white-space: pre-wrap;
  z-index: 4;
}

@media (max-width: 480px) {
  .card { padding: 20px; }
  h1 { font-size: 32px; }
}

/* Leaderboard button (mirrors #fullscreen, sits just left of it) */
#leaderboard-btn {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: calc(max(12px, env(safe-area-inset-right)) + 48px);
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.55);
  color: var(--text);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.3s;
  z-index: 3;
}
#leaderboard-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#stage.pointer-active #leaderboard-btn,
#leaderboard-btn:focus-visible { opacity: 1; }
#stage.loading #leaderboard-btn { opacity: 0; }

/* Leaderboard overlay */
#lb {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.8);
  backdrop-filter: blur(3px);
  z-index: 5;
}
#lb[hidden] { display: none; }
#lb .card {
  width: min(440px, 100%);
  max-height: 88vh;
  padding: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 1px solid rgba(70, 195, 79, 0.35);
}
#lb h2 {
  margin: 0;
  padding: 14px 20px;
  font: 700 14px/1.2 "Silkscreen", system-ui, sans-serif;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #0a0a0a;
  background: linear-gradient(180deg, #5bd964, var(--accent));
}
#lb-submit { padding: 16px 20px; border-bottom: 1px solid var(--line); }
#lb-submit .lb-result { margin: 0 0 2px; font-size: 15px; }
#lb-submit .lb-result b { color: var(--accent); font: 700 22px "Silkscreen", monospace; vertical-align: -1px; }
#lb .lb-sub { color: var(--muted); font-size: 12px; }
#lb-msg { margin: 10px 0 0; min-height: 1em; }

#lb-list { list-style: none; margin: 0; padding: 8px 12px; overflow-y: auto; flex: 1; min-height: 72px; }
#lb-list li {
  display: grid;
  grid-template-columns: 2.4em 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 9px 10px;
  border-radius: 8px;
}
#lb-list li:nth-child(even) { background: rgba(255, 255, 255, 0.035); }
#lb-list li .rk { font: 700 13px/1 "Silkscreen", monospace; color: var(--muted); text-align: center; }
#lb-list li .nm { font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#lb-list li .sc { color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }
#lb-list li.gold   { background: rgba(255, 211, 77, 0.12); }
#lb-list li.gold   .rk { color: #ffd34d; }
#lb-list li.silver .rk { color: #cfd6dd; }
#lb-list li.bronze .rk { color: #e0954b; }
#lb-list li.lb-empty {
  display: block;
  text-align: center;
  padding: 34px 16px;
  color: var(--muted);
  font-size: 14px;
  background: none;
}
#lb > .card > .actions { padding: 12px 16px; border-top: 1px solid var(--line); }

/* Desktop-only warning (shown on touch / mobile before anything loads) */
#mobile-warn {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(0, 0, 0, 0.93);
  z-index: 7;
  text-align: center;
}
#mobile-warn[hidden] { display: none; }
#mobile-warn .card { width: min(380px, 100%); }
#mobile-warn h2 { margin: 0 0 14px; font: 700 20px "Silkscreen", system-ui, sans-serif; color: var(--accent); }
#mobile-warn p { margin: 0 0 12px; color: var(--text); }
#mobile-warn .sub { color: var(--muted); font-size: 13px; }
#mobile-warn .actions { justify-content: center; margin-top: 8px; }
