/* Urfutam (medve.online/space) — Duolingo-night space look. Overlays are
   compact and scroll-free in BOTH orientations; the only scrollable area is
   the leaderboard list (3 rows visible, up to 30 by scrolling). */

:root {
  --green: #58cc02;
  --green-dark: #46a302;
  --green-light: #2d4a1a;
  --green-border: #58cc02;
  --green-text: #8ee55b;
  --red: #ff4b4b;
  --red-light: #4a1f24;
  --red-border: #ff4b4b;
  --red-text: #ff8a8a;
  --blue: #1cb0f6;
  --text: #f1f7fb;
  --muted: #a5b3c7;
  --border: #3a4a66;
  --card-bg: #17233c;
  --input-bg: #0f1a30;
  --panel-bg: rgba(15, 23, 42, 0.92);
  --page-bg: #060d1f;
}

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

body {
  font-family: "Nunito", -apple-system, "Segoe UI", Inter, sans-serif;
  background: var(--page-bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.wrap {
  width: 100%;
  height: 100vh;
  height: 100dvh;
  display: flex;
  justify-content: center;
}

.stage {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #101a33;
}

canvas#game { display: block; width: 100%; height: 100%; }

/* ---------- HUD ---------- */

#hud { position: absolute; top: 1.6rem; left: 0.6rem; pointer-events: none; z-index: 2; }

.chip {
  background: var(--panel-bg);
  border: 2px solid var(--border);
  border-bottom-width: 4px;
  border-radius: 0.75rem;
  padding: 0.25rem 0.75rem;
  font-weight: 900;
  font-size: 1rem;
}

/* ---------- Quiz (kept clear of the top edge for mobile Safari) ---------- */

#quiz {
  position: absolute;
  /* Answer controls live at the BOTTOM; score and fullscreen stay on top. */
  bottom: max(1rem, env(safe-area-inset-bottom));
  top: auto;
  left: 50%;
  transform: translateX(-50%);
  width: min(92%, 30rem);
  background: var(--panel-bg);
  border: 2px solid var(--border);
  border-bottom-width: 4px;
  border-radius: 1rem;
  padding: 0.5rem 0.75rem 0.6rem;
  text-align: center;
  z-index: 3;
}

.q-label {
  font-size: 0.8rem;
  font-weight: 800;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 0.35rem;
}

.answers { display: flex; gap: 0.5rem; justify-content: center; flex-wrap: wrap; }

.btn {
  font-family: inherit;
  font-weight: 800;
  font-size: 0.95rem;
  color: var(--text);
  background: var(--card-bg);
  border: 2px solid var(--border);
  border-bottom-width: 4px;
  border-radius: 0.75rem;
  padding: 0.5rem 0.9rem;
  min-height: 2.75rem;
  min-width: 5rem;
  cursor: pointer;
  transition: background 0.1s, transform 0.05s, border-color 0.1s;
  touch-action: manipulation;
}
.btn:hover { background: #1e2d4d; }
.btn:active { transform: translateY(2px); border-bottom-width: 2px; }
.btn:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }
.btn:disabled { cursor: default; }

.btn.correct { background: var(--green-light); border-color: var(--green-border); color: var(--green-text); }
.btn.wrong {
  background: var(--red-light);
  border-color: var(--red-border);
  color: var(--red-text);
  animation: shake 0.3s;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

.timerbar { height: 0.4rem; background: var(--border); border-radius: 0.2rem; margin-top: 0.5rem; overflow: hidden; }
.timerbar i {
  display: block; height: 100%; width: 100%;
  background: var(--green); border-radius: 0.2rem;
  transition: width 0.1s linear, background 0.2s;
}
.timerbar i.low { background: var(--red); }

/* ---------- Overlays ---------- */

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(3, 8, 20, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(0.4rem, 2vmin, 1rem);
  z-index: 4;
}

.card {
  background: var(--card-bg);
  border: 2px solid var(--border);
  border-radius: 1.25rem;
  padding: clamp(0.6rem, 2.5vmin, 1.25rem) clamp(0.8rem, 3vmin, 1.5rem);
  box-shadow: 0 0.5rem 1.5rem rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  gap: clamp(0.6rem, 2.5vmin, 1.25rem);
  max-width: min(96%, 40rem);
  max-height: 100%;
  overflow: hidden;
}

.card-main { flex: 1 1 auto; text-align: center; min-width: 0; }
.card-side { flex: 0 0 clamp(11rem, 34vw, 14rem); min-width: 0; }

.card h1, .card h2 {
  font-size: clamp(1.1rem, 4.5vmin, 1.6rem);
  font-weight: 900;
  margin-bottom: clamp(0.25rem, 1.5vmin, 0.5rem);
}

.rules {
  font-size: clamp(0.72rem, 2.6vmin, 0.9rem);
  line-height: 1.4;
  color: var(--muted);
  margin-bottom: clamp(0.4rem, 2vmin, 0.75rem);
}

/* Character select grid */
.char-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: clamp(0.4rem, 1.8vmin, 0.6rem);
  margin-bottom: clamp(0.4rem, 2vmin, 0.75rem);
}
.char-card {
  background: var(--input-bg);
  border: 2px solid var(--border);
  border-radius: 0.75rem;
  padding: clamp(0.2rem, 1vmin, 0.4rem);
  cursor: pointer;
  text-align: center;
  touch-action: manipulation;
}
.char-card img {
  width: 100%;
  height: clamp(4.5rem, 16vmin, 7rem);
  object-fit: contain;
}
.char-card .char-name {
  font-size: clamp(0.7rem, 2.4vmin, 0.85rem);
  font-weight: 800;
  color: var(--muted);
}
.char-card.selected { border-color: var(--green); background: var(--green-light); }
.char-card.selected .char-name { color: var(--green-text); }
.char-card:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }
.char-photo-icon {
  height: clamp(4.5rem, 16vmin, 7rem);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}
.char-photo-icon svg { width: 55%; height: 55%; }

.name-input {
  font-family: inherit;
  font-weight: 800;
  font-size: clamp(0.9rem, 3vmin, 1rem);
  color: var(--text);
  background: var(--input-bg);
  border: 2px solid var(--border);
  border-bottom-width: 4px;
  border-radius: 0.75rem;
  padding: clamp(0.35rem, 1.5vmin, 0.5rem) 0.9rem;
  width: 100%;
  text-align: center;
  margin-bottom: clamp(0.4rem, 2vmin, 0.6rem);
}
.name-input:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }
.name-input::placeholder { color: var(--muted); font-weight: 700; }

.btn-primary {
  background: var(--green);
  border-color: var(--green);
  border-bottom: 4px solid var(--green-dark);
  color: #0b1d0b;
  font-size: clamp(0.95rem, 3vmin, 1.05rem);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: clamp(0.45rem, 2vmin, 0.65rem) 1.5rem;
}
.btn-primary:hover { background: #61e002; }
.btn-primary:disabled { background: #3a6620; border-color: #3a6620; color: #7d9b6c; }

/* Photo modal */
.card-photo { flex-direction: column; text-align: center; }
.photo-hint { font-size: clamp(0.7rem, 2.4vmin, 0.85rem); color: var(--muted); max-width: 24rem; line-height: 1.4; }
#cropCanvas {
  background: var(--input-bg);
  border: 2px solid var(--border);
  border-radius: 0.75rem;
  max-height: min(46vh, 24rem);
  max-width: 90%;
  touch-action: none; /* the canvas handles its own dragging */
}
.zoom-row {
  display: flex; align-items: center; gap: 0.6rem;
  font-size: 0.85rem; font-weight: 800; color: var(--muted);
  width: min(90%, 20rem);
}
.zoom-row input { flex: 1; }
.photo-btns { display: flex; gap: 0.5rem; flex-wrap: wrap; justify-content: center; }

/* Game-over card */
.score-row { display: flex; align-items: center; justify-content: center; gap: 0.6rem; margin-bottom: clamp(0.2rem, 1vmin, 0.4rem); }
.score-big { font-size: clamp(2rem, 9vmin, 3rem); font-weight: 900; color: var(--green); line-height: 1; }
.score-label { font-size: clamp(0.7rem, 2.5vmin, 0.85rem); color: var(--muted); text-align: left; line-height: 1.2; }
.best { font-size: clamp(0.8rem, 2.8vmin, 0.9rem); font-weight: 800; color: var(--blue); margin-bottom: 0.2rem; }
.last-word { font-size: clamp(0.75rem, 2.8vmin, 0.9rem); color: var(--muted); margin-bottom: clamp(0.4rem, 2vmin, 0.75rem); }
.last-word b { color: var(--text); }
.over-btns { display: flex; gap: 0.5rem; flex-wrap: wrap; justify-content: center; }

/* Leaderboard: 3 rows visible, scrolls to 30 */
.lb-title {
  font-size: clamp(0.7rem, 2.5vmin, 0.8rem);
  font-weight: 800; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.06em;
  margin-bottom: 0.25rem; text-align: center;
}
.lb-place { font-size: clamp(0.8rem, 2.8vmin, 0.95rem); font-weight: 800; color: var(--blue); margin-bottom: 0.35rem; text-align: center; }
.lb-top {
  list-style: none;
  counter-reset: lb;
  max-height: calc(3 * 2.35rem);
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding-right: 0.15rem;
}
.lb-top li {
  counter-increment: lb;
  display: flex; align-items: center; gap: 0.45rem;
  background: var(--input-bg);
  border: 2px solid var(--border);
  border-radius: 0.6rem;
  padding: 0.25rem 0.55rem;
  margin-bottom: 0.3rem;
  font-weight: 800;
  font-size: clamp(0.75rem, 2.8vmin, 0.9rem);
  height: 2.05rem;
}
.lb-top li::before { content: counter(lb); color: var(--muted); min-width: 1.2rem; text-align: center; flex: 0 0 auto; }
.lb-top li.me { border-color: var(--green); background: var(--green-light); }
.lb-name { flex: 1; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-score { color: var(--green-text); flex: 0 0 auto; }

/* ---------- Fullscreen button ---------- */

.fs-btn {
  position: absolute;
  top: 1.6rem;
  right: 0.6rem;
  z-index: 5;
  width: 2.75rem;
  height: 2.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
  background: var(--panel-bg);
  border: 2px solid var(--border);
  border-bottom-width: 4px;
  border-radius: 0.75rem;
  cursor: pointer;
  touch-action: manipulation;
}
.fs-btn:hover { background: #1e2d4d; }
.fs-btn:active { transform: translateY(2px); border-bottom-width: 2px; }
.fs-btn svg { width: 1.25rem; height: 1.25rem; }

body.pseudo-fs .wrap { height: 100dvh; }

.hidden { display: none !important; }

/* ---------- Portrait: cards go column, character grid 3x2 ---------- */

@media (orientation: portrait) {
  .card { flex-direction: column; }
  .card-side { flex: 0 0 auto; width: 100%; }
  /* Portrait: two big character cards per row */
  .char-grid { grid-template-columns: repeat(2, 1fr); }
  .char-card img, .char-photo-icon { height: clamp(6rem, 24vmin, 9rem); }
}

/* Landscape phones: tighten */
@media (max-height: 30rem) {
  #quiz { bottom: 0.5rem; padding: 0.35rem 0.6rem 0.45rem; }
  .q-label { display: none; }
  .btn { font-size: 0.85rem; min-height: 2.5rem; padding: 0.35rem 0.7rem; }
  .rules { display: none; } /* height is scarce; the game explains itself */
  .char-card img, .char-photo-icon { height: 2.6rem; }
}

/* Score chip and fullscreen always stay at the TOP (quiz owns the bottom). */
