:root {
  --bg: #0e1120;
  --panel: #171b30;
  --panel-2: #20264a;
  --line: #2c3360;
  --text: #eef0fa;
  --muted: #9aa1c4;
  --accent: #ff4d5e;
  --accent-d: #c22e3e;
  --teal: #2dd4bf;
  --gold: #ffcf4a;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  font-family: Cairo, system-ui, -apple-system, "Segoe UI", sans-serif;
  background: radial-gradient(circle at 50% 0%, #1b2040 0%, var(--bg) 60%);
  color: var(--text);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
button { font-family: inherit; cursor: pointer; touch-action: manipulation; }

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding-top: var(--safe-t);
}

/* HUD */
/* sits above the game screens (z 10) so it stays usable on the menu, below the account screens (z 15) */
#topbar {
  position: relative; z-index: 12;
  display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 12px 0;
}
.top-group { display: flex; align-items: center; gap: 8px; min-width: 0; }
.icon-btn {
  width: 40px; height: 40px; flex: none;
  border: 1px solid var(--line); border-radius: 14px;
  background: var(--panel); color: var(--text); font-size: 18px;
}
.icon-btn:active, .pill-btn:active { transform: scale(.94); }
.icon-btn.on { border-color: var(--teal); box-shadow: 0 0 0 2px rgba(45, 212, 191, .18); }
.top-user {
  min-width: 0; max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 13px; font-weight: 700; color: var(--teal);
}
.pill-btn {
  flex: none; padding: 8px 18px;
  border: 1px solid var(--accent); border-radius: 99px;
  background: transparent; color: var(--accent); font-size: 14px; font-weight: 900;
}
/* account buttons are parked while a level is running (the timer has no pause) */
body.playing #btn-profile, body.playing #btn-login { opacity: .35; pointer-events: none; }

#hud { display: flex; justify-content: space-around; align-items: center; gap: 6px; padding: 8px 12px 6px; }
#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 30; }
.hud-item {
  display: flex; flex-direction: column; align-items: center;
  min-width: 78px; padding: 4px 10px; line-height: 1.2;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
}
.hud-label { font-size: 11px; color: var(--muted); }
.hud-item b { font-size: 20px; font-weight: 900; font-variant-numeric: tabular-nums; }
#hud-time.low { color: var(--accent); animation: pulse .45s infinite alternate; }

#timebar { height: 5px; margin: 2px 14px 0; background: var(--panel); border-radius: 99px; overflow: hidden; }
#timebar-fill { height: 100%; width: 100%; background: var(--teal); border-radius: 99px; transform-origin: right; }
#timebar-fill.low { background: var(--accent); }

/* Stage */
#stage { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; padding: 8px; }
#game {
  display: block; border-radius: 14px; background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 10px 40px rgba(0, 0, 0, .5), 0 0 0 4px rgba(45, 212, 191, .05);
}

/* D-pad */
#dpad {
  display: grid;
  grid-template-columns: repeat(3, 82px);
  grid-template-rows: repeat(2, 66px);
  gap: 8px;
  justify-content: center;
  width: max-content;
  margin: 0 auto calc(10px + var(--safe-b));
  padding: 10px 12px;
  background: rgba(23, 27, 48, .7);
  border: 1px solid var(--line);
  border-radius: 28px;
  touch-action: none;
}
.dbtn {
  border: 1px solid #3a4380; border-radius: 20px;
  background: linear-gradient(180deg, #2c3468 0%, #1e2448 100%);
  color: var(--text); font-size: 26px;
  box-shadow: 0 5px 0 #0a0d1a, inset 0 1px 0 rgba(255, 255, 255, .12);
  transition: transform .06s, box-shadow .06s, background .06s;
}
.dbtn.on {
  transform: translateY(4px);
  background: var(--teal); color: var(--bg); border-color: var(--teal);
  box-shadow: 0 1px 0 #0a0d1a, 0 0 18px rgba(45, 212, 191, .55);
}
@media (max-height: 640px) {
  #dpad { grid-template-rows: repeat(2, 54px); padding: 6px 10px; }
}
.dbtn.up { grid-column: 2; grid-row: 1; }
.dbtn.left { grid-column: 1; grid-row: 2; }
.dbtn.down { grid-column: 2; grid-row: 2; }
.dbtn.right { grid-column: 3; grid-row: 2; }

/* Overlay */
.overlay {
  position: fixed; inset: 0; z-index: 10;
  display: none; align-items: center; justify-content: center;
  padding: calc(16px + var(--safe-t)) 16px calc(16px + var(--safe-b));
  background: rgba(8, 10, 20, .72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.overlay.active { display: flex; animation: fade .2s ease-out; }
.card {
  width: 100%; max-width: 360px; max-height: 100%; overflow: auto;
  padding: 24px 20px; text-align: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .6);
  animation: pop .25s ease-out;
}
.ov-emoji { font-size: 52px; line-height: 1.2; }
.card h1 { font-size: 30px; font-weight: 900; line-height: 1.35; color: var(--accent); }
.card p { margin-top: 6px; font-size: 16px; color: var(--muted); }

.stats { display: grid; gap: 6px; margin-top: 14px; }
.stats:empty { display: none; }
.stats div {
  display: flex; justify-content: space-between; align-items: center;
  padding: 7px 14px; background: var(--panel-2); border-radius: 12px;
}
.stats span { color: var(--muted); font-size: 14px; }
.stats b { font-size: 18px; color: var(--teal); font-variant-numeric: tabular-nums; }

.badge {
  display: inline-block; margin-top: 12px; padding: 4px 14px;
  border-radius: 99px; background: var(--gold); color: var(--bg); font-weight: 900;
  animation: pulse .6s infinite alternate;
}
.btn {
  display: block; width: 100%; margin-top: 16px; padding: 12px;
  border: 0; border-radius: 16px;
  background: var(--accent); color: #fff; font-size: 20px; font-weight: 900;
  box-shadow: 0 5px 0 var(--accent-d);
}
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--accent-d); }

.btn.outline {
  margin-top: 10px; background: transparent; color: var(--gold);
  border: 1px solid var(--gold); box-shadow: none; font-size: 16px;
}
.btn.outline:active { transform: scale(.97); box-shadow: none; }
.btn:disabled, .link:disabled { opacity: .55; }

/* Account screens */
.overlay.auth { z-index: 15; }
.auth .card { position: relative; text-align: right; }
.auth h2 { margin-bottom: 10px; font-size: 24px; font-weight: 900; color: var(--accent); text-align: center; }
.auth .ov-emoji, .auth .auth-note { text-align: center; }
.auth .auth-note { margin: 0 0 10px; font-size: 14px; }
.auth-close {
  position: absolute; top: 10px; left: 10px; width: 36px; height: 36px;
  border: 0; border-radius: 12px; background: var(--panel-2); color: var(--muted); font-size: 16px;
}
.auth label { display: block; margin-top: 10px; font-size: 13px; font-weight: 700; color: var(--muted); }
.auth input {
  display: block; width: 100%; margin-top: 4px; padding: 11px 12px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--bg); color: var(--text);
  font: inherit; font-size: 16px; /* 16px stops iOS zooming on focus */
  user-select: text; -webkit-user-select: text; touch-action: manipulation;
}
.auth input:focus { outline: none; border-color: var(--teal); box-shadow: 0 0 0 3px rgba(45, 212, 191, .18); }
.card .auth-msg { min-height: 20px; margin: 10px 0 0; font-size: 14px; font-weight: 700; color: var(--accent); text-align: center; }
.card .auth-msg.ok { color: var(--teal); }
.card .auth-msg:empty { min-height: 0; margin: 0; }
.link {
  display: block; width: 100%; margin-top: 10px; padding: 6px;
  border: 0; background: none; color: var(--teal); font-size: 14px; font-weight: 700; text-decoration: underline;
}

/* Share block on result screens */
.share { margin-top: 16px; padding-top: 14px; border-top: 1px dashed var(--line); }
.card .share-msg { margin: 0 0 10px; font-size: 16px; font-weight: 700; color: var(--gold); }
.card .share-title { margin: 0 0 8px; font-size: 13px; color: var(--muted); }
.share-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.share-btn {
  padding: 10px 8px; border: 1px solid var(--line); border-radius: 14px;
  background: var(--panel-2); color: var(--text); font-size: 15px; font-weight: 700;
}
.share-btn:active { transform: scale(.96); }
.share-btn.wa { background: #1fa855; border-color: #1fa855; color: #fff; }
.share-btn.x { background: #000; border-color: #3a3f55; color: #fff; }
.share-btn.copy { grid-column: 1 / -1; }

/* Short phones: tighten the result card so the share buttons fit on screen */
@media (max-height: 700px) {
  .card { padding: 12px 14px; border-radius: 20px; }
  .ov-emoji { font-size: 30px; }
  .card h1 { font-size: 21px; }
  .card p { margin-top: 2px; font-size: 14px; }
  .stats { gap: 4px; margin-top: 8px; }
  .stats div { padding: 3px 12px; }
  .stats b { font-size: 16px; }
  .badge { margin-top: 6px; padding: 2px 12px; font-size: 14px; }
  .btn { margin-top: 10px; padding: 8px; font-size: 18px; }
  .share { margin-top: 10px; padding-top: 8px; }
  .card .share-msg { margin-bottom: 6px; font-size: 13px; }
  .card .share-title { display: none; }
  .share-row { grid-template-columns: 1fr 1fr 1.4fr; gap: 6px; }
  .share-btn { padding: 8px 4px; font-size: 13px; }
  .share-btn.copy { grid-column: auto; }
}

@keyframes pulse { to { transform: scale(1.1); } }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { transform: scale(.92); opacity: 0; } }
