@font-face {
  font-family: "Press Start 2P";
  src: url("fonts/press-start-2p.woff2") format("woff2");
  font-display: swap;
}
@font-face {
  font-family: "VT323";
  src: url("fonts/vt323.woff2") format("woff2");
  font-display: swap;
}

:root {
  --bg: #120a06;
  --panel: #22140a;
  --ink: #fff1d0;
  --muted: #d4a862;
  --border: #5c3b1e;
  --border-filled: #d8a860;
  --key: #8a6236;
  --key-hi: #b4864e;
  --key-lo: #3c2410;
  --correct: #3a9a3a;
  --correct-hi: #74d064;
  --correct-lo: #1c5a1c;
  --present: #e89a10;
  --present-hi: #ffd050;
  --present-lo: #965400;
  --absent: #4a3220;
  --absent-hi: #6a4a30;
  --absent-lo: #24160c;
  --sunset: #f06a20;
  --magenta: #c02c5c;
  --pixel: "Press Start 2P", "Courier New", monospace;
  --body: "VT323", "Courier New", monospace;
  --tile: 58px;
  color-scheme: dark;
}

/* High contrast (colour-blind friendly): orange = right spot, blue = wrong spot */
.contrast {
  --correct: #f07a2a;
  --correct-hi: #ffb070;
  --correct-lo: #9a4410;
  --present: #3a9ae8;
  --present-hi: #8ccaff;
  --present-lo: #1a5a9a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--body);
  font-size: 20px;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

body {
  background:
    radial-gradient(ellipse at 50% 0%, #2a1408 0%, transparent 70%),
    var(--bg);
}

button {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
  cursor: pointer;
  border-radius: 0;
}

canvas { image-rendering: pixelated; image-rendering: crisp-edges; display: block; }

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 520px;
  margin: 0 auto;
  padding-bottom: env(safe-area-inset-bottom);
  user-select: none;
  -webkit-user-select: none;
}

/* CRT scanlines over everything */
.crt {
  position: fixed;
  inset: 0;
  z-index: 200;
  pointer-events: none;
  /* Scanlines + vignette as plain gradients: a big blurred box-shadow here is very slow on iPhone */
  background:
    repeating-linear-gradient(to bottom, rgba(0, 0, 0, 0.18) 0 1px, transparent 1px 3px),
    radial-gradient(ellipse at center, transparent 60%, rgba(0, 0, 0, 0.45) 100%);
  transform: translateZ(0);
}

/* ---------- Header: animated savanna ---------- */
#hud {
  position: relative;
  flex: 0 0 auto;
  height: calc(128px + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  overflow: hidden;
  border-bottom: 4px solid #000;
}

#scene {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.title {
  position: relative;
  text-align: center;
  margin-top: 12px;
  pointer-events: none;
}

h1 {
  margin: 0;
  font-family: var(--pixel);
  font-size: 24px;
  font-weight: 400;
  color: #ffe060;
  letter-spacing: 2px;
  text-shadow: 3px 3px 0 var(--magenta), 6px 6px 0 #2a0c30;
}

.tagline {
  font-family: var(--pixel);
  font-size: 7px;
  margin-top: 8px;
  color: var(--ink);
  letter-spacing: 1px;
  text-shadow: 2px 2px 0 #000;
}

.hud-left { position: relative; display: flex; gap: 6px; margin: 10px 0 0 8px; }
.hud-right { position: relative; display: flex; gap: 6px; margin: 10px 8px 0 0; }

.hud-btn {
  position: relative;
  width: 36px;
  height: 36px;
  margin: 10px 0 0 8px;
  padding: 0;
  border: 0;
  background: rgba(18, 10, 6, 0.75);
  box-shadow: inset 0 0 0 3px var(--ink), 3px 3px 0 #000;
  font-family: var(--pixel);
  font-size: 14px;
  display: grid;
  place-items: center;
}
.hud-left .hud-btn, .hud-right .hud-btn { margin: 0; }
.hud-btn svg { width: 18px; height: 18px; }
.hud-btn:active { transform: translate(2px, 2px); box-shadow: inset 0 0 0 3px var(--ink), 1px 1px 0 #000; }

/* ---------- Mode bar & hints ---------- */
#subbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px 0;
  min-height: 42px;
}

#mode-label {
  font-family: var(--pixel);
  font-size: 9px;
  line-height: 1.5;
  color: var(--muted);
}

.pill {
  font-family: var(--pixel);
  font-size: 8px;
  padding: 9px 10px;
  border: 0;
  background: var(--panel);
  box-shadow: inset 0 0 0 2px var(--border);
  color: var(--muted);
  white-space: nowrap;
}
.pill:disabled { opacity: 0.6; cursor: default; }
.pill.ready {
  background: var(--present);
  color: #2a1400;
  box-shadow: inset 2px 2px 0 var(--present-hi), inset -2px -2px 0 var(--present-lo), 3px 3px 0 #000;
  animation: blink 1s steps(1) infinite;
}
.pill.ready:active { transform: translate(2px, 2px); }

@keyframes blink { 50% { filter: brightness(1.25); } }

#hints { padding: 0 12px; }
#hints div {
  margin-top: 8px;
  padding: 6px 10px;
  background: var(--panel);
  box-shadow: inset 0 0 0 2px var(--present);
  font-size: 19px;
  line-height: 1.1;
  animation: appear 0.3s steps(3);
}
#hints b { font-family: var(--pixel); font-size: 8px; font-weight: 400; color: var(--present); margin-right: 6px; }

@keyframes appear { from { opacity: 0; } }

/* ---------- Board ---------- */
#board-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 8px 12px;
}

#board {
  display: grid;
  grid-template-rows: repeat(6, var(--tile));
  gap: 6px;
}

.row { display: grid; gap: 6px; }

.tile {
  width: var(--tile);
  height: var(--tile);
  background: rgba(34, 20, 10, 0.6);
  box-shadow: inset 0 0 0 3px var(--border);
  display: grid;
  place-items: center;
  font-family: var(--pixel);
  font-size: calc(var(--tile) * 0.42);
  padding-top: 2px;
  color: var(--ink);
}

.tile.filled { box-shadow: inset 0 0 0 3px var(--border-filled); animation: pop 0.12s steps(2); }
.tile.correct, .tile.present, .tile.absent { color: #fff; text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.45); }
.tile.correct { background: var(--correct); box-shadow: inset 3px 3px 0 var(--correct-hi), inset -3px -3px 0 var(--correct-lo); }
.tile.present { background: var(--present); box-shadow: inset 3px 3px 0 var(--present-hi), inset -3px -3px 0 var(--present-lo); }
.tile.absent { background: var(--absent); box-shadow: inset 3px 3px 0 var(--absent-hi), inset -3px -3px 0 var(--absent-lo); color: #c8b090; }

.tile.flip { animation: flip 0.5s steps(8) forwards; }
.tile.bounce { animation: bounce 0.6s steps(6); }
.row.shake { animation: shake 0.4s steps(8); }

@keyframes pop { 50% { transform: scale(1.12); } }
@keyframes flip {
  0% { transform: scaleY(1); }
  50% { transform: scaleY(0); }
  100% { transform: scaleY(1); }
}
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  35% { transform: translateY(-40%); }
  65% { transform: translateY(-12%); }
}
@keyframes shake {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}

/* ---------- Keyboard ---------- */
#keyboard { padding: 0 4px 8px; }

.kb-row {
  display: flex;
  gap: 5px;
  margin-bottom: 9px;
  justify-content: center;
}

.key {
  flex: 1;
  height: 52px;
  max-width: 44px;
  border: 0;
  background: var(--key);
  color: var(--ink);
  box-shadow: inset 2px 2px 0 var(--key-hi), inset -2px -2px 0 var(--key-lo), 0 4px 0 #000;
  font-family: var(--pixel);
  font-size: 13px;
  padding: 2px 0 0;
  display: grid;
  place-items: center;
}
.key.wide { flex: 1.55; max-width: 68px; font-size: 8px; }
.key.spacer { flex: 0.5; visibility: hidden; }
.key:active { transform: translateY(3px); box-shadow: inset 2px 2px 0 var(--key-hi), inset -2px -2px 0 var(--key-lo), 0 1px 0 #000; }
.key.correct { background: var(--correct); box-shadow: inset 2px 2px 0 var(--correct-hi), inset -2px -2px 0 var(--correct-lo), 0 4px 0 #000; }
.key.present { background: var(--present); box-shadow: inset 2px 2px 0 var(--present-hi), inset -2px -2px 0 var(--present-lo), 0 4px 0 #000; }
.key.absent { background: var(--absent); color: #8a7058; box-shadow: inset 2px 2px 0 var(--absent-hi), inset -2px -2px 0 var(--absent-lo), 0 4px 0 #000; }

@media (max-width: 400px) {
  h1 { font-size: 20px; letter-spacing: 1px; }
  .tagline { font-size: 6px; }
}

@media (max-height: 680px) {
  #hud { height: calc(100px + env(safe-area-inset-top)); }
  .key { height: 44px; }
  .kb-row { margin-bottom: 7px; }
}

/* ---------- Toasts ---------- */
#toasts {
  position: fixed;
  top: calc(146px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 150;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}
.toast {
  background: var(--ink);
  color: #1a0c04;
  padding: 12px 14px 10px;
  box-shadow: inset 0 0 0 3px #1a0c04, 4px 4px 0 #000;
  font-family: var(--pixel);
  font-size: 10px;
  white-space: nowrap;
}
.toast.fade { opacity: 0; transition: opacity 0.3s steps(3); }

/* ---------- Dialog boxes ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(8, 4, 2, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  animation: appear 0.15s steps(2);
}
.modal[hidden] { display: none; }

.sheet {
  position: relative;
  width: 100%;
  max-width: 440px;
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  background: var(--panel);
  box-shadow: inset 0 0 0 3px var(--ink), inset 0 0 0 6px #000, inset 0 0 0 9px var(--border), 6px 6px 0 #000;
  padding: 26px 20px 22px;
  animation: drop 0.25s steps(4);
}
@keyframes drop { from { transform: translateY(-30px); opacity: 0; } }

.close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 32px;
  height: 32px;
  border: 0;
  background: none;
  font-family: var(--pixel);
  font-size: 12px;
  color: var(--muted);
}

.sheet h2, .sheet h3 {
  font-family: var(--pixel);
  font-weight: 400;
  text-align: center;
  color: #ffe060;
  text-shadow: 2px 2px 0 var(--magenta);
}
.sheet h2 { margin: 4px 0 16px; font-size: 14px; }
.sheet h3 { margin: 20px 0 10px; font-size: 10px; }
.sheet p { font-size: 20px; line-height: 1.15; margin: 8px 0; }
.sheet b { font-weight: 400; color: #ffe060; }
.sheet b.c-correct { color: var(--correct-hi); }
.sheet b.c-present { color: var(--present-hi); }
.sheet b.c-absent { color: #b0906a; }

.example { display: flex; gap: 5px; margin-top: 14px; --tile: 38px; }

#result { text-align: center; margin-bottom: 8px; }
#result-title {
  font-family: var(--pixel);
  font-size: 18px;
  color: #ffe060;
  text-shadow: 3px 3px 0 var(--magenta);
  margin: 4px 0 14px;
  animation: blink 0.6s steps(1) 4;
}
#result-title.lost { color: #ff6040; text-shadow: 3px 3px 0 #400; }
#result-scene {
  width: 100%;
  aspect-ratio: 2.2 / 1;
  box-shadow: 0 0 0 3px #000, 0 0 0 6px var(--border);
}
#result-sub { font-family: var(--pixel); font-size: 8px; color: var(--muted); margin-top: 16px; }
#result-name {
  font-family: var(--pixel);
  font-size: 20px;
  margin: 10px 0 14px;
  color: var(--ink);
  text-shadow: 3px 3px 0 #000;
  letter-spacing: 2px;
}
#result-fact {
  text-align: left;
  background: var(--bg);
  box-shadow: inset 0 0 0 2px var(--border);
  padding: 10px 12px;
}
#result-fact .label { font-family: var(--pixel); font-size: 8px; color: var(--correct-hi); }
#result-fact p { margin: 6px 0 0; }

.stats-row { display: flex; justify-content: center; gap: 6px; text-align: center; }
.stats-row > div { flex: 1; max-width: 90px; }
.stats-row span { display: block; font-family: var(--pixel); font-size: 20px; margin-bottom: 8px; }
.stats-row small { font-family: var(--pixel); font-size: 7px; color: var(--muted); }

#dist { display: flex; flex-direction: column; gap: 5px; }
.dist-row { display: flex; align-items: center; gap: 8px; font-family: var(--pixel); font-size: 10px; }
.dist-row .bar {
  background: var(--absent);
  box-shadow: inset 2px 2px 0 var(--absent-hi), inset -2px -2px 0 var(--absent-lo);
  padding: 5px 6px 3px;
  min-width: 26px;
  text-align: right;
}
.dist-row .bar.highlight { background: var(--correct); box-shadow: inset 2px 2px 0 var(--correct-hi), inset -2px -2px 0 var(--correct-lo); }

.next { text-align: center; margin: 22px 0 16px; }
.next small { display: block; font-family: var(--pixel); font-size: 8px; color: var(--muted); margin-bottom: 10px; }
#countdown { font-family: var(--pixel); font-size: 20px; }

.actions { display: flex; gap: 12px; }
.btn {
  flex: 1;
  height: 50px;
  border: 0;
  background: var(--key);
  box-shadow: inset 3px 3px 0 var(--key-hi), inset -3px -3px 0 var(--key-lo), 4px 4px 0 #000;
  font-family: var(--pixel);
  font-size: 10px;
  padding-top: 3px;
}
.btn:active { transform: translate(3px, 3px); box-shadow: inset 3px 3px 0 var(--key-hi), inset -3px -3px 0 var(--key-lo), 1px 1px 0 #000; }
.btn.primary { background: var(--correct); box-shadow: inset 3px 3px 0 var(--correct-hi), inset -3px -3px 0 var(--correct-lo), 4px 4px 0 #000; }
.btn.start { display: block; width: 100%; margin-top: 20px; flex: none; animation: blink 1s steps(1) infinite; }

/* ---------- Everyone's results ---------- */
#global {
  margin: 0 0 16px;
  padding: 4px 12px 12px;
  background: var(--bg);
  box-shadow: inset 0 0 0 2px var(--border);
}
#global h3 { margin: 12px 0 12px; }
#global .stats-row span { font-size: 16px; }
#gl-you { text-align: center; margin: 12px 0 0; color: var(--present-hi); }

/* ---------- Rank ---------- */
#rank {
  display: flex;
  align-items: center;
  gap: 14px;
  margin: 4px 0 20px;
  padding: 12px;
  background: var(--bg);
  box-shadow: inset 0 0 0 2px var(--border);
}
#rank.promoted { box-shadow: inset 0 0 0 2px var(--present); animation: blink 0.5s steps(1) 6; }
#rank-badge { width: 56px; height: 56px; flex: 0 0 auto; }
.rank-info { flex: 1; min-width: 0; }
#rank-label { font-family: var(--pixel); font-size: 7px; color: var(--muted); }
#rank-name { font-family: var(--pixel); font-size: 14px; margin: 6px 0 8px; color: #ffe060; text-shadow: 2px 2px 0 var(--magenta); }
#rank .meter { height: 12px; margin: 0 0 6px; }
#rank-meter { height: 100%; background: var(--present); box-shadow: inset 0 2px 0 var(--present-hi); min-width: 2px; }
#rank-next { font-size: 17px; color: var(--muted); }

/* ---------- Settings ---------- */
.setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 14px 0;
  border-bottom: 2px solid var(--border);
}
.setting:last-child { border-bottom: 0; }
.setting-name { font-family: var(--pixel); font-size: 10px; color: var(--ink); }
.setting p { margin: 6px 0 0; font-size: 18px; color: var(--muted); }
.toggle {
  flex: 0 0 auto;
  width: 64px;
  height: 34px;
  border: 0;
  font-family: var(--pixel);
  font-size: 9px;
  padding-top: 2px;
  background: var(--absent);
  color: #b0906a;
  box-shadow: inset 2px 2px 0 var(--absent-hi), inset -2px -2px 0 var(--absent-lo), 3px 3px 0 #000;
}
.toggle[aria-checked="true"] {
  background: var(--correct);
  color: #fff;
  box-shadow: inset 2px 2px 0 var(--correct-hi), inset -2px -2px 0 var(--correct-lo), 3px 3px 0 #000;
}
.toggle:disabled { opacity: 0.45; cursor: default; }
.toggle:active:not(:disabled) { transform: translate(2px, 2px); }

/* ---------- Save code ---------- */
.save-box { padding: 16px 0 4px; border-top: 2px solid var(--border); }
.save-box p { margin: 6px 0 12px; font-size: 18px; color: var(--muted); }
.code-row { display: flex; gap: 10px; align-items: stretch; margin-bottom: 14px; }
#save-code {
  flex: 1;
  display: grid;
  place-items: center;
  min-height: 44px;
  background: var(--bg);
  box-shadow: inset 0 0 0 2px var(--border-filled);
  font-family: var(--pixel);
  font-size: 11px;
  letter-spacing: 1px;
  color: #ffe060;
  user-select: all;
  -webkit-user-select: all;
}
#save-code.empty { font-family: var(--body); font-size: 18px; letter-spacing: 0; color: var(--muted); padding: 6px 10px; text-align: center; }
.code-row .btn { flex: 0 0 88px; height: auto; min-height: 44px; font-size: 9px; }
.restore-label { display: block; margin-bottom: 8px; }
#restore-input {
  flex: 1;
  min-width: 0;
  height: 44px;
  border: 0;
  border-radius: 0;
  padding: 0 10px;
  background: var(--bg);
  color: var(--ink);
  box-shadow: inset 0 0 0 2px var(--border);
  font-family: var(--body);
  font-size: 22px; /* 16px or more stops iPhones zooming in on focus */
  text-transform: uppercase;
}
#restore-input:focus { outline: none; box-shadow: inset 0 0 0 2px var(--present); }
#save-line { text-align: center; font-size: 18px; color: var(--muted); margin: 18px 0 0; }
#save-line b { font-family: var(--pixel); font-size: 9px; color: #ffe060; font-weight: 400; }

/* ---------- Field guide ---------- */
.guide-progress { text-align: center; margin-bottom: 16px; }
#guide-count { font-family: var(--pixel); font-size: 9px; color: var(--muted); }
.meter {
  height: 14px;
  margin-top: 10px;
  background: var(--bg);
  box-shadow: inset 0 0 0 2px var(--border);
  padding: 3px;
}
#guide-meter { height: 100%; background: var(--correct); box-shadow: inset 0 2px 0 var(--correct-hi); min-width: 2px; }

#guide-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.card {
  position: relative;
  border: 0;
  padding: 0 0 6px;
  background: var(--bg);
  box-shadow: inset 0 0 0 2px var(--border);
  font-family: var(--pixel);
  font-size: 6px;
  color: var(--muted);
  text-align: center;
  overflow: hidden;
}
.card canvas { width: 100%; aspect-ratio: 36 / 28; margin-bottom: 5px; }
.card .num { position: absolute; top: 3px; left: 4px; font-size: 6px; color: var(--ink); text-shadow: 1px 1px 0 #000; }
.card .nm { display: block; padding: 0 2px; white-space: nowrap; overflow: hidden; text-overflow: clip; }
.card.found { color: var(--ink); box-shadow: inset 0 0 0 2px var(--border-filled); }
.card.found:active { transform: translate(1px, 1px); }
.card.locked { cursor: default; }
.card.locked .q {
  display: grid;
  place-items: center;
  aspect-ratio: 36 / 28;
  margin-bottom: 5px;
  font-size: 16px;
  color: var(--border);
}

.back {
  border: 0;
  background: none;
  font-family: var(--pixel);
  font-size: 9px;
  color: var(--muted);
  padding: 0;
  margin-bottom: 14px;
  display: block;
}
#guide-detail { text-align: center; }
#guide-number { font-family: var(--pixel); font-size: 8px; color: var(--muted); margin-bottom: 10px; }
#guide-scene { width: 100%; aspect-ratio: 2.2 / 1; box-shadow: 0 0 0 3px #000, 0 0 0 6px var(--border); }
#guide-name { font-family: var(--pixel); font-size: 20px; margin: 18px 0 8px; text-shadow: 3px 3px 0 #000; letter-spacing: 2px; }
#guide-group { font-family: var(--pixel); font-size: 8px; color: var(--muted); margin-bottom: 14px; }
.notes { text-align: left; background: var(--bg); box-shadow: inset 0 0 0 2px var(--border); padding: 10px 12px; }
.notes .label { font-family: var(--pixel); font-size: 8px; color: var(--correct-hi); }
.notes p { margin: 6px 0 0; }
#guide-found { font-family: var(--pixel); font-size: 8px; color: var(--present-hi); margin-top: 14px; line-height: 1.6; }

.btn.guide-link { display: block; width: 100%; margin-top: 12px; height: 42px; font-size: 8px; }

/* ---------- Win celebration ---------- */
.confetti {
  position: fixed;
  top: -20px;
  z-index: 120;
  pointer-events: none;
  animation: fall 2.4s steps(24) forwards;
}
@keyframes fall { to { transform: translate(var(--drift), 110vh); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}
