/* Math Survival — minimalist mobile-first arcade stylesheet */

:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #15181f;
  --muted: #6b7280;
  --line: #e3e6eb;
  --green: #22c55e;
  --green-dark: #16a34a;
  --green-ink: #0b4a24;
  --red: #ef4444;
  --red-dark: #dc2626;
  --amber: #f59e0b;
  --key: #ffffff;
  --key-press: #e9ecf1;
  --shadow: 0 2px 0 rgba(0, 0, 0, 0.08), 0 6px 14px rgba(15, 23, 42, 0.06);
  --radius: 16px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --mono: ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--mono);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

body { position: fixed; inset: 0; }

.app {
  position: absolute;
  inset: 0;
  height: 100vh;
  height: 100dvh;
  padding: var(--sat) var(--sar) var(--sab) var(--sal);
  display: flex;
  justify-content: center;
}

.screen {
  width: 100%;
  max-width: 480px;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 12px 14px;
  position: relative;
}
.screen[hidden] { display: none; }

button {
  font: inherit;
  color: inherit;
  border: 0;
  margin: 0;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}
button:focus { outline: none; }
button:focus-visible { outline: 3px solid rgba(34, 197, 94, 0.5); outline-offset: 2px; }

.btn {
  border-radius: var(--radius);
  font-weight: 800;
  letter-spacing: 0.06em;
  box-shadow: var(--shadow);
  transition: transform 80ms ease, filter 80ms ease, background 80ms ease;
}
.btn:active:not(:disabled) { transform: translateY(2px) scale(0.99); box-shadow: 0 0 0 rgba(0,0,0,0.08), 0 2px 6px rgba(15,23,42,0.06); }
.btn-primary { background: var(--green); color: #fff; }
.btn-primary:active:not(:disabled) { background: var(--green-dark); }
.btn-primary:disabled { background: #cfd4dc; color: #fff; box-shadow: none; cursor: default; }
.btn-secondary { background: var(--panel); color: var(--ink); border: 2px solid var(--line); }
.btn-big { width: 100%; padding: 20px 24px; font-size: clamp(22px, 6vw, 30px); }

.btn-sound {
  position: absolute;
  top: 10px;
  right: 12px;
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 20px;
  line-height: 1;
  box-shadow: var(--shadow);
  z-index: 5;
}
.btn-sound[aria-pressed="false"] { filter: grayscale(1); opacity: 0.7; }
.btn-sound-game { position: static; width: 34px; height: 34px; font-size: 16px; box-shadow: none; }
.hud-side { display: flex; align-items: center; gap: 10px; min-width: 64px; justify-content: flex-end; }

/* ---------- Start screen ---------- */
.screen-start { justify-content: safe center; align-items: center; text-align: center; gap: 16px; position: relative; overflow-y: auto; padding-top: 24px; padding-bottom: 20px; }
.screen-start > * { flex-shrink: 0; }
.title {
  margin: 0;
  font-size: clamp(40px, 13vw, 72px);
  line-height: 0.95;
  font-weight: 900;
  letter-spacing: 0.02em;
}
.title::after { content: ""; display: block; width: 64px; height: 6px; margin: 16px auto 0; border-radius: 3px; background: var(--green); }
.tagline { margin: 0; color: var(--muted); font-size: clamp(16px, 4.5vw, 20px); font-weight: 600; }
.best-line { margin: 0; color: var(--muted); font-size: 17px; }
.best-line strong { color: var(--ink); font-size: 20px; }
.screen-start .btn-big { max-width: 320px; margin-top: 8px; }
.rules {
  list-style: none; padding: 0; margin: 4px 0 0;
  width: 100%; max-width: 320px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow);
}
.rules li { display: flex; justify-content: space-between; padding: 12px 18px; font-weight: 600; }
.rules li + li { border-top: 1px solid var(--line); }
.rule-k { color: var(--muted); }
.rule-v { font-weight: 800; font-variant-numeric: tabular-nums; }
.good { color: var(--green-dark); }
.bad { color: var(--red-dark); }

.visits { margin: 4px 0 0; color: var(--muted); font-size: 13px; letter-spacing: 0.04em; font-variant-numeric: tabular-nums; }
.visits strong { color: var(--ink); }

/* ---------- Game screen ---------- */
.screen-game { padding: 10px 12px 10px; gap: 0; }

.hud { flex: 0 0 auto; height: 17%; min-height: 96px; display: flex; flex-direction: column; justify-content: center; gap: 6px; }
.hud-row { display: flex; align-items: center; justify-content: space-between; }
.hud-score { display: flex; flex-direction: column; min-width: 108px; }
.hud-best { display: flex; flex-direction: column; }
.hud-best { align-items: flex-end; }
.hud-label { font-size: 11px; letter-spacing: 0.16em; color: var(--muted); font-weight: 700; }
.hud-value { font-size: clamp(22px, 7vw, 30px); font-weight: 900; font-variant-numeric: tabular-nums; line-height: 1.1; }
.hud-timer-wrap { position: relative; display: flex; justify-content: center; flex: 1; }
.hud-timer {
  font-size: clamp(44px, 14vw, 60px);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  transition: color 150ms ease;
}
.hud-timer.warn { color: var(--amber); }
.hud-timer.danger { color: var(--red); animation: pulse 0.6s ease-in-out infinite; }
.hud-timer.bump { animation: bump 220ms ease-out; }
.hud-timer.danger.bump { animation: bump 220ms ease-out, pulse 0.6s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.06); } }
@keyframes bump { 0% { transform: scale(1); } 40% { transform: scale(1.12); } 100% { transform: scale(1); } }

.hud-float {
  position: absolute;
  top: -4px; right: -6px;
  font-size: 18px; font-weight: 900;
  color: var(--green-dark);
  opacity: 0;
  pointer-events: none;
  white-space: nowrap;
}
.hud-float.bad { color: var(--red-dark); }
.hud-float.show { animation: floatUp 700ms ease-out forwards; }
@keyframes floatUp { 0% { opacity: 0; transform: translateY(8px); } 20% { opacity: 1; } 100% { opacity: 0; transform: translateY(-22px); } }

.streak { display: flex; justify-content: center; gap: 10px; }
.dot { width: 14px; height: 14px; border-radius: 50%; background: #d7dbe2; border: 2px solid #c9ced6; transition: background 120ms ease, transform 120ms ease, border-color 120ms ease; }
.dot.on { background: var(--green); border-color: var(--green-dark); transform: scale(1.1); }
.streak.flash .dot { background: var(--green); border-color: var(--green-dark); animation: dotFlash 500ms ease-out; }
.streak.shake { animation: shake 250ms ease-in-out; }
@keyframes dotFlash { 0% { transform: scale(1); } 50% { transform: scale(1.5); } 100% { transform: scale(1); } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }

.arena {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  gap: 8px;
  padding-top: 26px;
}
.problem {
  font-size: clamp(42px, 13vw, 64px);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
  line-height: 1.1;
  text-align: center;
  transition: transform 120ms ease, opacity 120ms ease;
}
.problem.swap { animation: swapIn 180ms ease-out; }
@keyframes swapIn { 0% { opacity: 0; transform: translateY(-10px); } 100% { opacity: 1; transform: translateY(0); } }
.problem.wrong { animation: shake 250ms ease-in-out; }

.answer {
  min-width: 56%;
  min-height: 1.3em;
  padding: 4px 18px;
  border-bottom: 4px solid var(--line);
  font-size: clamp(38px, 12vw, 56px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
  text-align: center;
  color: var(--ink);
  display: flex; align-items: center; justify-content: center;
  transition: border-color 120ms ease;
}
.answer.has { border-color: var(--ink); }
.answer .caret { display: inline-block; width: 3px; height: 0.9em; background: var(--green); border-radius: 2px; animation: blink 1s steps(1) infinite; }
.answer.has .caret { display: none; }
@keyframes blink { 50% { opacity: 0; } }

.feedback {
  position: absolute;
  right: 6%;
  top: 50%;
  transform: translateY(-50%);
  width: 64px; height: 64px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 40px; font-weight: 900; color: #fff;
  opacity: 0;
  pointer-events: none;
}
.feedback.ok { background: var(--green); animation: pop 300ms ease-out; }
.feedback.ko { background: var(--red); animation: pop 300ms ease-out; }
@keyframes pop { 0% { opacity: 0; transform: translateY(-50%) scale(0.5); } 30% { opacity: 1; transform: translateY(-50%) scale(1.15); } 100% { opacity: 0; transform: translateY(-50%) scale(1); } }

.bonus {
  position: absolute;
  top: -6px; left: 50%; transform: translateX(-50%);
  padding: 5px 16px;
  border-radius: 999px;
  background: var(--green);
  color: #fff;
  font-size: clamp(16px, 5vw, 21px);
  font-weight: 900;
  letter-spacing: 0.04em;
  box-shadow: 0 8px 20px rgba(34, 197, 94, 0.35);
  opacity: 0;
  pointer-events: none;
  white-space: nowrap;
}

.bonus.show { animation: bonusPop 900ms cubic-bezier(.2,.9,.3,1.2) forwards; }
@keyframes bonusPop { 0% { opacity: 0; transform: translate(-50%, 10px) scale(0.6); } 20% { opacity: 1; transform: translate(-50%, 0) scale(1.08); } 75% { opacity: 1; transform: translate(-50%, 0) scale(1); } 100% { opacity: 0; transform: translate(-50%, -14px) scale(0.95); } }

.pad { flex: 0 0 auto; height: 48%; display: flex; flex-direction: column; gap: 8px; }
.keys {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: 8px;
  overflow: hidden;
}
.key {
  background: var(--key);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: clamp(26px, 8vw, 34px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 0; min-width: 0; overflow: hidden;
  transition: background 60ms ease, transform 60ms ease;
}
.key:active, .key.pressed { background: var(--key-press); transform: translateY(2px); box-shadow: 0 0 0 rgba(0,0,0,0.08), 0 2px 6px rgba(15,23,42,0.06); }
.key-back { color: var(--muted); }
.key-skip { background: #fff3f3; color: var(--red-dark); font-size: clamp(16px, 4.6vw, 20px); letter-spacing: 0.06em; line-height: 1; gap: 2px; }
.key-skip small { font-size: 12px; font-weight: 700; opacity: 0.8; }
.key-skip:active { background: #ffe3e3; }
.key:disabled { opacity: 0.5; cursor: default; }
.btn-submit { flex: 0 0 auto; height: 15%; min-height: 48px; width: 100%; font-size: clamp(20px, 6vw, 26px); }

/* ---------- Game over ---------- */
.screen-over { justify-content: safe center; align-items: center; text-align: center; gap: 8px; overflow-y: auto; padding-top: 18px; padding-bottom: 18px; }
.over-label { margin: 0; color: var(--muted); letter-spacing: 0.2em; font-weight: 800; font-size: 14px; }
.over-score { font-size: clamp(72px, 24vw, 120px); font-weight: 900; line-height: 1; font-variant-numeric: tabular-nums; }
.over-record { margin: 0; color: var(--green-dark); font-weight: 900; font-size: clamp(18px, 5.5vw, 24px); letter-spacing: 0.08em; animation: recordIn 500ms ease-out; }
@keyframes recordIn { 0% { opacity: 0; transform: scale(0.7); } 60% { transform: scale(1.1); } 100% { opacity: 1; transform: scale(1); } }
.over-buttons { width: 100%; max-width: 320px; display: flex; flex-direction: column; gap: 12px; margin-top: 10px; flex-shrink: 0; }
.over-buttons .btn-big { padding: 16px 24px; }

/* ---------- World ranking ---------- */
.world { width: 100%; max-width: 320px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px 14px 8px; margin-top: 4px; flex-shrink: 0; }
.world-start { margin-top: 0; padding-bottom: 6px; }
.world-start .world-list li { padding: 4px 0; font-size: 14px; }
.world-title { margin: 0 0 6px; font-size: 11px; letter-spacing: 0.18em; color: var(--muted); font-weight: 800; }
.world-name { display: flex; gap: 8px; margin: 4px 0 6px; }
.world-name input { flex: 1 1 auto; min-width: 0; font: inherit; font-weight: 800; font-size: 18px; letter-spacing: 0.08em; text-transform: uppercase; text-align: center; padding: 10px 12px; border: 2px solid var(--line); border-radius: 12px; background: var(--bg); color: var(--ink); outline: none; -webkit-user-select: text; user-select: text; }
.world-name input:focus { border-color: var(--green); }
.world-name .btn { flex: 0 0 auto; padding: 10px 16px; font-size: 15px; }
.world-me { margin: 2px 0 6px; font-weight: 800; color: var(--green-dark); font-size: 15px; }
.world-list { list-style: none; margin: 0; padding: 0; font-variant-numeric: tabular-nums; }
.world-list li { display: flex; align-items: center; gap: 8px; padding: 5px 0; border-top: 1px solid var(--line); font-size: 15px; font-weight: 600; }
.world-list li:first-child { border-top: 0; }
.world-list .pos { width: 22px; color: var(--muted); font-weight: 800; text-align: right; }
.world-list .nm { flex: 1 1 auto; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; letter-spacing: 0.04em; }
.world-list .sc { font-weight: 900; }
.world-list li.me { color: var(--green-dark); }
.world-list .empty { color: var(--muted); font-weight: 600; font-size: 14px; padding: 6px 0; }
.linkish { background: none; border: 0; color: var(--muted); font-size: 12px; text-decoration: underline; padding: 6px 0 2px; }
.credit { margin: 14px 0 0; color: var(--muted); font-size: 13px; letter-spacing: 0.1em; }

.toast {
  position: absolute;
  left: 50%; bottom: calc(var(--sab) + 28px);
  transform: translate(-50%, 20px);
  background: var(--ink); color: #fff;
  padding: 10px 18px; border-radius: 999px;
  font-weight: 700; font-size: 15px;
  opacity: 0; pointer-events: none;
  transition: opacity 200ms ease, transform 200ms ease;
  max-width: 90%; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Short screens (landscape phones, small browsers) */
@media (max-height: 560px) {
  .hud { min-height: 72px; }
  .keys { gap: 6px; }
  .pad { gap: 6px; }
  .arena { padding-top: 18px; }
  .hud-timer { font-size: 36px; }
  .hud-value { font-size: 20px; }
  .problem { font-size: 36px; }
  .answer { font-size: 32px; }
  .key { font-size: 22px; }
  .screen { padding: 6px 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; animation-iteration-count: 1 !important; transition-duration: 1ms !important; }
  .feedback.ok, .feedback.ko { opacity: 1; animation: none; transition: opacity 1ms; }
  .feedback { opacity: 0; }
  .hud-float.show, .bonus.show { opacity: 1; animation: none; }
}
