:root {
  --blue: #1f6fb2;
  --blue-d: #174f80;
  --red: #c8102e;
  --red-d: #8e0b20;
  --sand: #f3e9d2;
  --sand-d: #d9c7a1;
  --ink: #2b2118;
  --card: #fffaf0;
  --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: var(--sand);
  color: var(--ink);
  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 */
#hud {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: var(--blue);
  color: #fff;
  border-bottom: 4px solid var(--red);
}
.hud-item { display: flex; flex-direction: column; align-items: center; line-height: 1.15; min-width: 50px; }
.hud-label { font-size: 11px; opacity: .85; }
.hud-item span:last-child { font-size: 20px; font-weight: 900; }
#hud-time.low { color: #ffd23f; animation: pulse .45s infinite alternate; }
#hud-time.hit { color: #ff8a8a; }
#hud-power { flex: 1; text-align: center; font-weight: 700; font-size: 15px; white-space: nowrap; }
#btn-pause {
  background: rgba(255, 255, 255, .18);
  border: 0; color: #fff; font-size: 18px;
  width: 40px; height: 40px; border-radius: 12px;
}

/* Stage */
#stage {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px;
}
#game { display: block; border-radius: 10px; box-shadow: 0 4px 18px rgba(0, 0, 0, .2); background: var(--sand-d); }

/* D-pad */
#dpad {
  display: grid;
  grid-template-columns: repeat(3, 66px);
  grid-template-rows: repeat(2, 54px);
  gap: 6px;
  justify-content: center;
  padding: 6px 0 calc(10px + var(--safe-b));
}
.dbtn {
  border: 0; border-radius: 14px;
  background: #fff; color: var(--blue);
  font-size: 24px;
  box-shadow: 0 4px 0 var(--sand-d);
}
.dbtn.on, .dbtn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--sand-d); background: #e6f0fa; }
.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; }
@media (hover: hover) and (pointer: fine) { #dpad { display: none; } }

/* Overlays */
.overlay {
  position: fixed; inset: 0;
  display: none;
  align-items: center; justify-content: center;
  padding: calc(16px + var(--safe-t)) 16px calc(16px + var(--safe-b));
  background: rgba(23, 79, 128, .55);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  z-index: 10;
}
.overlay.active { display: flex; animation: fade .2s ease-out; }
.card {
  width: 100%; max-width: 380px; max-height: 100%;
  overflow: auto;
  background: var(--card);
  border-radius: 22px;
  border-top: 6px solid var(--red);
  padding: 20px 18px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .3);
  animation: pop .25s ease-out;
}
.title { font-size: 34px; font-weight: 900; color: var(--red); line-height: 1.3; }
.title.small { font-size: 26px; }
.tagline { font-size: 16px; margin-top: 4px; }
.emoji-row { font-size: 34px; letter-spacing: 4px; }
.goal-emoji { font-size: 54px; animation: bob 1s infinite alternate ease-in-out; }
.hint { font-size: 13px; opacity: .75; }
.best { margin-top: 8px; font-size: 16px; }
.best b { color: var(--blue); font-size: 20px; }

.legend { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin: 12px 0 8px; font-size: 13px; text-align: right; }
.legend div { background: #fff; border-radius: 10px; padding: 6px 8px; border: 1px solid #eadfc6; }
.legend .wide { grid-column: 1 / -1; text-align: center; }

.stats { margin: 10px 0 4px; display: grid; gap: 6px; }
.stats div { display: flex; justify-content: space-between; background: #fff; border-radius: 10px; padding: 6px 12px; border: 1px solid #eadfc6; }
.stats b { font-size: 18px; color: var(--blue); }
.stats .total b { color: var(--red); }
hr { border: 0; border-top: 2px dashed #e5d7b8; margin: 12px 0; }

.level-tag { font-weight: 700; color: var(--blue); }
.level-name { font-size: 26px; font-weight: 900; }
.flavor { font-size: 16px; margin: 4px 0; }
.timer-tag { font-weight: 700; opacity: .8; }

.btn {
  display: block; width: 100%;
  margin-top: 10px; padding: 11px;
  border: 0; border-radius: 14px;
  font-size: 20px; font-weight: 900;
  background: var(--red); color: #fff;
  box-shadow: 0 4px 0 var(--red-d);
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--red-d); }
.btn.secondary { background: var(--blue); box-shadow: 0 4px 0 var(--blue-d); }
.btn.ghost { background: transparent; color: var(--blue); border: 2px solid var(--blue); box-shadow: none; }

.badge {
  display: inline-block; margin-top: 8px; padding: 4px 14px;
  border-radius: 999px; background: #ffd23f; font-weight: 900;
  animation: pulse .6s infinite alternate;
}

#toast {
  position: fixed; left: 50%; bottom: calc(28px + var(--safe-b));
  transform: translate(-50%, 20px);
  max-width: 90%;
  padding: 10px 16px; border-radius: 12px;
  background: var(--ink); color: #fff; text-align: center;
  opacity: 0; pointer-events: none; z-index: 20;
  transition: opacity .25s, transform .25s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@keyframes pulse { to { transform: scale(1.12); } }
@keyframes bob { to { transform: translateY(-6px); } }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { transform: scale(.92); opacity: 0; } }
