:root {
  --bg: #070b1a;
  --panel: rgba(16, 22, 46, 0.78);
  --panel-border: rgba(255, 255, 255, 0.12);
  --text: #f1f3ff;
  --muted: #9aa3c7;
  --accent: #ff4fa3;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: ui-rounded, 'SF Pro Rounded', 'Nunito', system-ui, -apple-system, 'Segoe UI', sans-serif;
}

#scene {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  pointer-events: none;
}
.brand { font-weight: 800; font-size: 22px; letter-spacing: 0.04em; }
.counter { color: var(--muted); font-size: 14px; }

.history {
  position: fixed;
  top: 64px;
  right: 16px;
  width: 250px;
  max-height: calc(100vh - 180px);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 12px;
  backdrop-filter: blur(10px);
}
.history-title { font-size: 12px; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); margin-bottom: 8px; }
.history ol { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.history li {
  display: grid;
  grid-template-columns: 14px 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 6px 4px;
  font-size: 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}
.history li:last-child { border-bottom: 0; }
.history li.new { animation: slidein 0.4s ease-out; }
.history .chip { width: 14px; height: 14px; border-radius: 50%; box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.25); }
.history .dist { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.history li.win .dist { color: var(--accent); font-weight: 700; }
.legend { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 11px; color: var(--muted); }
.legend-bar {
  flex: 1; height: 8px; border-radius: 4px;
  background: linear-gradient(90deg, rgb(90,169,255), rgb(127,224,196), rgb(255,224,102), rgb(255,154,60), rgb(229,35,43));
}

.bottom {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  padding: 0 16px calc(20px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  pointer-events: none;
}
.bottom > * { pointer-events: auto; }

.message {
  min-height: 24px;
  padding: 4px 14px;
  font-size: 15px;
  border-radius: 999px;
  background: transparent;
  color: var(--text);
  opacity: 0;
  transition: opacity 0.2s;
}
.message.show { opacity: 1; background: var(--panel); border: 1px solid var(--panel-border); }
.message.error { color: #ffb3c6; }

.inputrow {
  width: min(560px, 100%);
  display: flex;
  gap: 8px;
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 18px;
  backdrop-filter: blur(10px);
}
.inputrow.shake { animation: shake 0.4s; }
.inputrow input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 17px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 12px 14px;
  outline: none;
}
.inputrow input:focus { border-color: var(--accent); }
.inputrow input:disabled { opacity: 0.6; }
.inputrow input::placeholder { color: var(--muted); }
button {
  font: inherit;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  border: 0;
  border-radius: 12px;
  padding: 0 20px;
  cursor: pointer;
}
button:disabled { opacity: 0.5; cursor: default; }
button:active:not(:disabled) { transform: translateY(1px); }

.bubble {
  position: fixed;
  left: 0; top: 0;
  z-index: 5;
  pointer-events: none;
  opacity: 0;
  transform: translate(-9999px, -9999px);
}
.bubble-body {
  position: absolute;
  left: 0; bottom: 26px;
  transform: translateX(-50%) scale(1);
  transform-origin: 50% 100%;
  white-space: nowrap;
  padding: 10px 18px;
  font-size: 16px;
  font-weight: 700;
  color: #3a2745;
  background: #fff;
  border-radius: 28px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
}
.bubble-body::before, .bubble-body::after {
  content: '';
  position: absolute;
  background: #fff;
  border-radius: 50%;
}
.bubble-body::before { width: 22px; height: 22px; left: -6px; top: -8px; box-shadow: 18px -8px 0 2px #fff, 44px -6px 0 0 #fff; z-index: -1; }
.bubble-body::after { width: 16px; height: 16px; right: -4px; bottom: -4px; box-shadow: -30px 6px 0 2px #fff; z-index: -1; }
.bubble .dot { position: absolute; background: #fff; border-radius: 50%; box-shadow: 0 3px 8px rgba(0,0,0,.3); }
.bubble .d1 { width: 14px; height: 14px; left: -7px; bottom: 8px; }
.bubble .d2 { width: 8px; height: 8px; left: -3px; bottom: -2px; }
.bubble.pop .bubble-body { animation: pop 0.35s cubic-bezier(.2, 1.6, .4, 1); }
.bubble.angry .bubble-body { color: #fff; background: #e0262e; animation: pop 0.25s cubic-bezier(.2, 1.6, .4, 1), rumble 0.12s linear infinite; }
.bubble.angry .bubble-body::before, .bubble.angry .bubble-body::after { background: #e0262e; box-shadow: none; }
.bubble.angry .dot { background: #e0262e; }
.bubble.happy .bubble-body { background: #fff4a8; color: #5a3a00; }
.bubble.happy .bubble-body::before, .bubble.happy .bubble-body::after { background: #fff4a8; box-shadow: none; }
.bubble.happy .dot { background: #fff4a8; }
.bubble.sad .bubble-body { background: #dfe9ff; color: #2c3d73; }
.bubble.sad .bubble-body::before, .bubble.sad .bubble-body::after { background: #dfe9ff; box-shadow: none; }
.bubble.sad .dot { background: #dfe9ff; }

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(5, 8, 20, 0.6);
  backdrop-filter: blur(6px);
  transition: opacity 0.3s;
}
.overlay.hidden { opacity: 0; pointer-events: none; }
.card {
  width: min(440px, 100%);
  text-align: center;
  padding: 28px 26px;
  background: rgba(20, 26, 54, 0.95);
  border: 1px solid var(--panel-border);
  border-radius: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.card h1 { margin: 6px 0 10px; font-size: 26px; }
.card p { margin: 0 0 12px; line-height: 1.5; color: #d6dbf5; }
.card p.small { font-size: 13px; color: var(--muted); }
.globe-emoji { font-size: 48px; }
.win-card { animation: pop 0.5s cubic-bezier(.2, 1.4, .4, 1); }
button.primary { padding: 12px 28px; font-size: 17px; margin-top: 6px; }

#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 30; overflow: hidden; }
#confetti i {
  position: absolute;
  top: -20px;
  width: 10px; height: 16px;
  opacity: 0.95;
  animation: fall linear forwards;
}

@keyframes fall {
  to { transform: translate3d(var(--dx), 110vh, 0) rotate(var(--rot)); }
}
@keyframes pop {
  0% { transform: translateX(-50%) scale(0.2); opacity: 0; }
  100% { transform: translateX(-50%) scale(1); opacity: 1; }
}
.win-card { animation-name: popcard; }
@keyframes popcard { 0% { transform: scale(0.6); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
@keyframes rumble {
  0% { margin-left: -2px; } 50% { margin-left: 2px; } 100% { margin-left: -2px; }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-9px); } 40% { transform: translateX(8px); }
  60% { transform: translateX(-6px); } 80% { transform: translateX(4px); }
}
@keyframes slidein { from { opacity: 0; transform: translateX(14px); } to { opacity: 1; transform: none; } }

@media (max-width: 720px) {
  .history {
    top: 52px; left: 10px; right: 10px; width: auto;
    max-height: 96px;
    padding: 8px 10px;
  }
  .history-title, .legend { display: none; }
  .history ol { display: flex; gap: 10px; overflow-x: auto; overflow-y: hidden; }
  .history li { white-space: nowrap; border: 0; padding: 4px 2px; }
}
