:root {
  --orange: #FF7F00;
  --orange-hot: #FFB000;
  --black: #000;
  --ink: #fff;
  --muted: #a8a8a8;
  --intensity: 0;
  --cell: 48px;
  --bw: min(calc(100vw - 32px), 560px, calc(100vh - 190px));
}
@supports (height: 100dvh) {
  :root { --bw: min(calc(100vw - 32px), 560px, calc(100dvh - 190px)); }
}

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

html, body {
  height: 100%;
  background: var(--black);
  color: var(--ink);
  overscroll-behavior: none;
}

body {
  font-family: 'Anton', Impact, 'Arial Narrow Bold', sans-serif;
  letter-spacing: 0.02em;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ---------- Background ---------- */
.bg {
  position: fixed;
  inset: -100px;
  background: repeating-linear-gradient(-45deg,
    rgba(255, 127, 0, 0.08) 0 22px,
    transparent 22px 64px);
  opacity: calc(0.55 + var(--intensity) * 0.45);
  animation: stripes calc(30s - var(--intensity) * 20s) linear infinite;
  z-index: 0;
  pointer-events: none;
}
@keyframes stripes { to { transform: translateX(90.51px); } }

/* ---------- Screens ---------- */
.screen {
  position: fixed;
  inset: 0;
  z-index: 1;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  justify-content: safe center;
  gap: 18px;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  text-align: center;
  overflow-y: auto;
}
[hidden] { display: none !important; }
.screen.active { display: flex; }
#screen-game { gap: 12px; }

/* ---------- Buttons ---------- */
.buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 14px;
}
.btn {
  font-family: inherit;
  font-size: 28px;
  line-height: 1.1;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  min-width: 180px;
  padding: 12px 32px;
  background: var(--black);
  color: var(--orange);
  border: 4px solid var(--orange);
  box-shadow: 6px 6px 0 var(--orange);
  cursor: pointer;
  transition: transform 0.08s, box-shadow 0.08s;
}
.btn.primary { background: var(--orange); color: var(--black); box-shadow: 6px 6px 0 var(--ink); }
.btn.ghost { border-color: var(--muted); color: var(--muted); box-shadow: none; min-width: 120px; }
.btn:hover:not(:disabled) { transform: translate(-2px, -2px); box-shadow: 8px 8px 0 currentColor; }
.btn.primary:hover:not(:disabled) { box-shadow: 8px 8px 0 var(--ink); }
.btn.ghost:hover { box-shadow: none; color: var(--ink); border-color: var(--ink); }
.btn:active:not(:disabled) { transform: translate(4px, 4px); box-shadow: 2px 2px 0 currentColor; }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn:focus-visible, .icon-btn:focus-visible { outline: 3px solid var(--ink); outline-offset: 3px; }

.icon-btn {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  font-size: 20px;
  font-family: inherit;
  color: var(--orange);
  background: var(--black);
  border: 3px solid var(--orange);
  cursor: pointer;
  display: grid;
  place-items: center;
}
.icon-btn.corner { position: absolute; top: max(16px, env(safe-area-inset-top)); right: 16px; }

/* ---------- Menu ---------- */
.logo {
  width: min(72vw, 340px, 42vh);
  height: auto;
  filter: drop-shadow(0 0 30px rgba(255, 127, 0, 0.35));
}
.bob { animation: bob 3s ease-in-out infinite; }
@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-1.5deg); }
  50% { transform: translateY(-10px) rotate(1.5deg); }
}
.tagline { font-size: 20px; line-height: 1.3; color: var(--ink); }
.tagline b { color: var(--orange); font-weight: normal; }
.best-line { font-size: 16px; color: var(--muted); letter-spacing: 0.06em; }
.best-line span { color: var(--orange); }

/* ---------- HUD ---------- */
.hud {
  width: var(--bw);
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.stat {
  flex: 1 1 0;
  min-width: 0;
  background: var(--black);
  border: 3px solid var(--orange);
  padding: 2px 6px 4px;
}
.stat label {
  display: block;
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--orange);
}
.stat span {
  display: block;
  font-size: 24px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.stat.low { background: var(--orange); color: var(--black); animation: lowPulse 0.5s ease-in-out infinite alternate; }
.stat.low label { color: var(--black); }
@keyframes lowPulse { to { transform: scale(1.06); } }
.stat .bump { animation: bump 0.25s ease-out; }
@keyframes bump { 50% { transform: scale(1.18); color: var(--orange-hot); } }

/* ---------- Board ---------- */
.board-wrap {
  position: relative;
  width: var(--bw);
  aspect-ratio: 1 / 1;
  border: 4px solid var(--orange);
  background: var(--black);
  box-shadow: 0 0 0 4px var(--black), 0 0 calc(30px + var(--intensity) * 40px) rgba(255, 127, 0, calc(0.2 + var(--intensity) * 0.35));
}
.board {
  position: absolute;
  inset: 0;
  overflow: hidden;
  touch-action: none;
  cursor: pointer;
  background: repeating-conic-gradient(#151515 0 25%, #0a0a0a 0 50%) 0 0 / calc(200% / 7) calc(200% / 7);
}
#fx, .overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
.overlay { overflow: hidden; }
.board-wrap.shake { animation: shake 0.32s ease-out; }
@keyframes shake {
  20% { transform: translate(-6px, 3px); }
  40% { transform: translate(5px, -4px); }
  60% { transform: translate(-4px, -2px); }
  80% { transform: translate(3px, 2px); }
}

/* ---------- Tiles ---------- */
.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: calc(100% / 7);
  height: calc(100% / 7);
  will-change: transform;
}
.face {
  position: absolute;
  inset: 7%;
  border-radius: 10%;
  background-color: #222;
  background-size: cover;
  background-position: center;
  border: calc(var(--cell) * 0.065) solid var(--ring, var(--orange));
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.7), inset 0 0 0 2px rgba(0, 0, 0, 0.4);
  transition: transform 0.12s ease-out, box-shadow 0.12s;
}
/* Tint the photo with the ring color so tile types read apart at a glance.
   'color' keeps the photo's light/dark but takes the ring's hue (white ring = grayscale). */
.face::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--ring, transparent);
  mix-blend-mode: color;
  opacity: 0.6;
  pointer-events: none;
}
.face.cross::after { display: none; }
.face.cross {
  inset: 5%;
  border-radius: 14%;
  border: 2px solid var(--orange);
  background-size: cover;
  background-position: center;
  animation: crossGlow 1.1s ease-in-out infinite alternate;
}
@keyframes crossGlow {
  from { box-shadow: 0 0 4px var(--orange), 0 0 0 0 rgba(255, 127, 0, 0.6); }
  to { box-shadow: 0 0 calc(var(--cell) * 0.3) var(--orange-hot), 0 0 0 3px rgba(255, 176, 0, 0.7); }
}
.tile.flame::before {
  content: '';
  position: absolute;
  inset: -4%;
  border-radius: 14%;
  background: radial-gradient(circle, rgba(255, 176, 0, 0.95) 0%, rgba(255, 90, 0, 0.6) 48%, transparent 70%);
  animation: flicker 0.18s ease-in-out infinite alternate;
}
.tile.flame .face { box-shadow: 0 0 calc(var(--cell) * 0.2) var(--orange-hot); border-color: var(--orange-hot); }
@keyframes flicker {
  from { transform: scale(0.95); opacity: 0.75; }
  to { transform: scale(1.08); opacity: 1; }
}

.face.selected {
  transform: scale(1.14);
  box-shadow: 0 0 0 3px var(--orange), 0 0 calc(var(--cell) * 0.35) var(--orange);
  z-index: 2;
}
.tile:has(.selected) { z-index: 2; }
.face.hint { animation: hint 0.7s ease-in-out infinite alternate; }
@keyframes hint {
  to { transform: scale(1.1); box-shadow: 0 0 0 3px var(--ink), 0 0 calc(var(--cell) * 0.3) var(--ink); }
}
.face.nope { animation: nope 0.3s ease-in-out; }
@keyframes nope {
  25% { transform: translateX(-8%); }
  75% { transform: translateX(8%); }
}
.face.pop { animation: pop 0.26s ease-in forwards; }
@keyframes pop {
  0% { transform: scale(1); }
  35% { transform: scale(1.25); filter: brightness(1.8); }
  100% { transform: scale(0); opacity: 0; filter: brightness(2.5); }
}
.face.born { animation: born 0.4s cubic-bezier(.34, 1.56, .64, 1); }
.face.cross.born { animation: born 0.4s cubic-bezier(.34, 1.56, .64, 1), crossGlow 1.1s ease-in-out 0.4s infinite alternate; }
@keyframes born {
  from { transform: scale(0) rotate(-90deg); }
  to { transform: scale(1) rotate(0); }
}

/* ---------- Overlay text ---------- */
.popup {
  position: absolute;
  transform: translate(-50%, -50%);
  font-size: calc(var(--cell) * 0.5);
  color: var(--ink);
  -webkit-text-stroke: 1px var(--black);
  text-shadow: 0 2px 0 var(--black), 0 0 10px var(--orange);
  white-space: nowrap;
  animation: floatUp 0.9s ease-out forwards;
}
.popup.time { color: var(--orange-hot); font-size: calc(var(--cell) * 0.6); }
@keyframes floatUp {
  from { opacity: 0; transform: translate(-50%, -20%) scale(0.6); }
  20% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); }
  to { opacity: 0; transform: translate(-50%, -160%) scale(1); }
}
.callout {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: calc(var(--cell) * 1.1);
  line-height: 1;
  white-space: pre;
  text-align: center;
  color: var(--orange);
  -webkit-text-stroke: 2px var(--black);
  text-shadow: 4px 4px 0 var(--black), 0 0 24px rgba(255, 127, 0, 0.8);
  animation: callout 1.1s cubic-bezier(.2, 1.4, .4, 1) forwards;
}
.callout.with-slogan {
  width: 94%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: calc(var(--cell) * 0.1);
  animation-duration: 1.8s;
}
.callout .slogan {
  font-size: calc(var(--cell) * 0.38);
  line-height: 1.15;
  white-space: normal;
  text-align: center;
  color: var(--black);
  background: var(--orange);
  -webkit-text-stroke: 0;
  text-shadow: none;
  padding: calc(var(--cell) * 0.06) calc(var(--cell) * 0.18);
  box-shadow: 4px 4px 0 var(--black);
}
.callout.big { font-size: calc(var(--cell) * 1.35); color: var(--ink); text-shadow: 4px 4px 0 var(--orange), 0 0 30px var(--orange); }
@keyframes callout {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.3) rotate(-8deg); }
  20% { opacity: 1; transform: translate(-50%, -50%) scale(1.1) rotate(-4deg); }
  75% { opacity: 1; transform: translate(-50%, -50%) scale(1) rotate(-4deg); }
  100% { opacity: 0; transform: translate(-50%, -70%) scale(1.05) rotate(-4deg); }
}

/* ---------- Progress ---------- */
.progress {
  width: var(--bw);
  height: 14px;
  border: 3px solid var(--orange);
  background: var(--black);
}
#progress-fill {
  height: 100%;
  width: 0;
  background: repeating-linear-gradient(-45deg, var(--orange) 0 8px, var(--orange-hot) 8px 16px);
  transition: width 0.3s ease-out;
}

/* ---------- Game over ---------- */
.over-title {
  font-weight: normal;
  font-size: clamp(64px, 18vw, 120px);
  line-height: 0.9;
  color: var(--orange);
  text-transform: uppercase;
  text-shadow: 6px 6px 0 var(--ink);
  transform: rotate(-3deg);
}
.final { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.final label { font-size: 14px; letter-spacing: 0.16em; color: var(--muted); text-transform: uppercase; }
#final-score { font-size: clamp(48px, 14vw, 80px); line-height: 1; font-variant-numeric: tabular-nums; }
.final-best { font-size: 16px; color: var(--muted); }
.final-best span { color: var(--orange); }
.badge {
  background: var(--orange);
  color: var(--black);
  padding: 4px 14px;
  font-size: 22px;
  text-transform: uppercase;
  transform: rotate(-3deg);
  animation: badge 0.6s ease-in-out infinite alternate;
}
.badge[hidden] { display: none; }
@keyframes badge { to { transform: rotate(3deg) scale(1.08); } }
.strip {
  display: flex;
  align-items: center;
  gap: clamp(12px, 3vw, 20px);
  background: var(--orange);
  color: var(--black);
  padding: 10px 24px 10px 10px;
  font-size: clamp(26px, 7.5vw, 44px);
  line-height: 1.05;
  text-align: left;
  text-transform: uppercase;
  box-shadow: 6px 6px 0 var(--ink);
}
.strip img { width: clamp(72px, 20vw, 110px); height: auto; aspect-ratio: 1; display: block; flex: 0 0 auto; }

/* ---------- Leaderboard ---------- */
.btn.wide { min-width: min(100%, 340px); }
.panel {
  width: min(100%, 480px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.screen-title {
  font-weight: normal;
  font-size: clamp(36px, 10vw, 56px);
  line-height: 1;
  color: var(--orange);
  text-transform: uppercase;
  text-shadow: 4px 4px 0 var(--ink);
}
.submit-score { font-size: 22px; color: var(--muted); text-transform: uppercase; }
.submit-score span { color: var(--ink); }
.form { display: flex; flex-direction: column; gap: 6px; text-align: left; }
.form label {
  margin-top: 8px;
  font-size: 14px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--orange);
}
.form label small { color: var(--muted); font-size: 12px; letter-spacing: 0.06em; }
.form input, .form textarea {
  width: 100%;
  font: 18px/1.35 system-ui, -apple-system, 'Segoe UI', sans-serif;
  color: var(--ink);
  background: var(--black);
  border: 3px solid var(--orange);
  border-radius: 0;
  padding: 10px 12px;
  -webkit-user-select: text;
  user-select: text;
  resize: none;
}
.form input:focus, .form textarea:focus { outline: 3px solid var(--ink); outline-offset: 2px; }
.form .invalid { border-color: #FF3B3B; }
.counter { align-self: flex-end; font-size: 13px; color: var(--muted); }
.form-note { font: 14px/1.4 system-ui, -apple-system, 'Segoe UI', sans-serif; color: var(--muted); text-align: center; }
.form-error {
  min-height: 1.4em;
  font: 600 15px/1.4 system-ui, -apple-system, 'Segoe UI', sans-serif;
  color: #FF6B6B;
  text-align: center;
}
.form .buttons { margin-top: 6px; }

.board-rank {
  align-self: center;
  background: var(--orange);
  color: var(--black);
  padding: 4px 14px;
  font-size: 24px;
  text-transform: uppercase;
}
.board-list { list-style: none; display: flex; flex-direction: column; gap: 6px; text-align: left; }
.board-list li {
  display: grid;
  grid-template-columns: 2.2em 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.85);
  border: 2px solid #2a2a2a;
}
.board-list li.me { border-color: var(--orange); box-shadow: 0 0 16px rgba(255, 127, 0, 0.45); }
.board-list li:nth-child(-n+3) .pos { color: var(--orange); }
.board-list .pos { font-size: 22px; color: var(--muted); }
.board-list .who { min-width: 0; display: flex; flex-direction: column; }
.board-list .nick { font-size: 20px; overflow-wrap: anywhere; }
.board-list .msg {
  font: 14px/1.35 system-ui, -apple-system, 'Segoe UI', sans-serif;
  color: var(--muted);
  overflow-wrap: anywhere;
}
.board-list .pts { font-size: 22px; color: var(--orange); font-variant-numeric: tabular-nums; }
.board-status { font: 15px/1.4 system-ui, -apple-system, 'Segoe UI', sans-serif; color: var(--muted); }
.board-status:empty { display: none; }

/* ---------- Flash & toast ---------- */
#flash {
  position: fixed;
  inset: 0;
  z-index: 10;
  pointer-events: none;
  background: var(--orange);
  opacity: 0;
}
#flash.on { animation: flash 0.45s ease-out; }
@keyframes flash { from { opacity: 0.55; } to { opacity: 0; } }
#toast {
  position: fixed;
  left: 50%;
  bottom: 32px;
  z-index: 11;
  transform: translate(-50%, 40px);
  background: var(--ink);
  color: var(--black);
  padding: 10px 18px;
  font-size: 18px;
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 420px) {
  .btn { font-size: 24px; min-width: 150px; padding: 10px 24px; }
  .stat span { font-size: 20px; }
  .tagline { font-size: 17px; }
}

@media (prefers-reduced-motion: reduce) {
  .bg, .bob, .badge, .stat.low, .face.hint, .face.cross, .tile.flame::before { animation: none !important; }
  .board-wrap.shake { animation: none; }
  #flash.on { animation: none; }
}
