:root {
  --sky1: #5ab0ff;
  --sky2: #bfe3ff;
  --ink: #12304d;
  --card: #ffffff;
  --accent: #ff7a3d;
  --blue: #2f6fd0;
  --good: #22b07d;
  --bad: #e5484d;
  --shadow: 0 10px 30px rgba(18, 48, 77, 0.14);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: linear-gradient(180deg, var(--sky1) 0%, var(--sky2) 70%, #e9f6ff 100%);
  user-select: none;
  -webkit-user-select: none;
}

.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  text-align: center;
}
.screen.active { display: flex; }
.hidden { display: none !important; }

h1, h2 { margin: 0; }
.logo {
  font-size: clamp(44px, 13vw, 68px);
  font-weight: 800;
  line-height: 0.95;
  letter-spacing: -1px;
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.logo small { font-size: 0.45em; font-weight: 700; letter-spacing: 0; opacity: 0.9; }
.logo-emoji { font-size: 0.9em; margin-bottom: 6px; animation: bob 2.4s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-10px) rotate(-6deg); } }
.sky-deco {
  position: absolute;
  top: 8%;
  left: -20%;
  font-size: 64px;
  opacity: 0.75;
  animation: drift 26s linear infinite;
  pointer-events: none;
}
.sky-deco.far { top: 22%; font-size: 38px; opacity: 0.5; animation-duration: 41s; animation-delay: -18s; }
@keyframes drift { to { transform: translateX(140vw); } }

.how { max-width: 340px; margin: 0; line-height: 1.45; }
.best, .note { margin: 0; opacity: 0.85; }

.name-row { display: flex; gap: 8px; align-items: center; font-weight: 600; }
.name-row input {
  font: inherit;
  width: 180px;
  padding: 9px 12px;
  border-radius: 12px;
  border: 2px solid rgba(18, 48, 77, 0.12);
  background: rgba(255, 255, 255, 0.9);
  color: var(--ink);
  user-select: text;
}

.menu { display: flex; flex-direction: column; gap: 10px; width: min(340px, 100%); }
.row { display: flex; gap: 10px; width: min(340px, 100%); }
.row > * { flex: 1; }

button {
  font: inherit;
  font-weight: 700;
  border: 0;
  border-radius: 16px;
  cursor: pointer;
  color: #fff;
  background: var(--accent);
  transition: transform 0.08s, filter 0.15s;
}
button:hover { filter: brightness(1.05); }
button:active { transform: scale(0.97); }
button:disabled { opacity: 0.5; cursor: default; }
button.big { font-size: 20px; padding: 16px 20px; width: min(340px, 100%); }
button.alt { background: var(--blue); }
button.small { font-size: 15px; padding: 12px 16px; background: #fff; color: var(--ink); min-width: 120px; }
button.danger { color: var(--bad); }

/* Waiting / join */
.spinner { font-size: 64px; animation: bob 1.4s ease-in-out infinite; }
.code-box { background: #fff; border-radius: 20px; padding: 16px 24px; box-shadow: var(--shadow); }
.code-box p { margin: 0 0 6px; }
.code-box button { background: var(--sky2); }
.code { font-size: 48px; font-weight: 800; letter-spacing: 8px; margin-bottom: 10px; }
.code-input {
  font: inherit;
  font-size: 40px;
  font-weight: 800;
  letter-spacing: 10px;
  text-align: center;
  text-transform: uppercase;
  width: 220px;
  padding: 10px;
  border-radius: 16px;
  border: 3px solid rgba(18, 48, 77, 0.15);
  color: var(--ink);
  user-select: text;
}
.error { color: var(--bad); font-weight: 700; min-height: 1.2em; margin: 0; }

/* Leaderboard */
#board { justify-content: flex-start; padding-top: max(24px, env(safe-area-inset-top)); }
.tabs { display: flex; gap: 8px; }
.tab { background: rgba(255, 255, 255, 0.5) !important; }
.tab.on { background: #fff !important; }
.board-list {
  list-style: none;
  margin: 0;
  padding: 6px;
  width: min(420px, 100%);
  flex: 1;
  overflow-y: auto;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 20px;
  box-shadow: var(--shadow);
  user-select: text;
}
.board-list li {
  display: grid;
  grid-template-columns: 44px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 12px;
  text-align: left;
}
.board-list li:nth-child(-n+3) .rk { font-size: 20px; }
.board-list li.me { background: #fff1e8; }
.board-list .rk { font-weight: 800; opacity: 0.7; }
.board-list .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.board-list .sc { font-weight: 800; font-size: 18px; }
.board-list .empty { display: block; text-align: center; opacity: 0.7; padding: 30px 10px; }
.board-me { margin: 0; font-weight: 700; min-height: 1.3em; }
button.invite { width: min(340px, 100%); background: rgba(255, 255, 255, 0.55); }
#friends { justify-content: flex-start; padding-top: max(24px, env(safe-area-inset-top)); }
.friend-list li { grid-template-columns: 1fr auto; }
.friend-list .st { display: block; font-size: 13px; font-weight: 600; opacity: 0.7; }
.friend-list .st.on { color: var(--good); opacity: 1; }
.friend-list button { min-width: 0; padding: 10px 14px; background: var(--accent); color: #fff; }
.friend-list button:disabled { background: #e3ebf3; color: var(--ink); }
.friend-list .acts { display: flex; gap: 6px; }
.friend-list .acts button.no { background: #eef4fa; color: var(--ink); }
.friend-list li.head { display: block; padding: 10px 12px 2px; font-size: 13px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.5px; opacity: 0.6; }
.search-row { display: flex; gap: 8px; width: min(420px, 100%); }
.search-row input {
  flex: 1; min-width: 0; font: inherit; padding: 10px 14px; border-radius: 14px;
  border: 2px solid rgba(18, 48, 77, 0.12); background: #fff; color: var(--ink); user-select: text;
}
.search-row button { min-width: 0; }
button.go { background: var(--accent); color: #fff; }
.board-list.results { flex: 0 1 auto; max-height: 34vh; }
#friends .board-list:not(.results) { flex: 1; }

.notify-banner {
  display: flex; align-items: center; gap: 8px; width: min(380px, 100%);
  background: #fff; border-radius: 16px; padding: 8px 8px 8px 14px; box-shadow: var(--shadow); text-align: left; font-weight: 600; font-size: 14px;
}
.notify-banner span { flex: 1; }
.notify-banner button { min-width: 0; padding: 9px 12px; }
.notify-banner button:last-child { background: #eef4fa; }

.challenger {
  position: fixed; z-index: 25; left: 50%; top: max(12px, env(safe-area-inset-top)); transform: translateX(-50%);
  width: min(420px, calc(100% - 24px)); display: flex; align-items: center; gap: 8px;
  background: var(--ink); color: #fff; border-radius: 18px; padding: 10px 10px 10px 16px; box-shadow: var(--shadow);
  font-weight: 700; animation: drop-in 0.25s ease-out;
}
.challenger span { flex: 1; min-width: 0; }
.challenger button { min-width: 0; padding: 9px 12px; }
@keyframes drop-in { from { transform: translate(-50%, -30px); opacity: 0; } }

.modal { position: fixed; inset: 0; z-index: 30; background: rgba(18, 48, 77, 0.45); display: grid; place-items: center; padding: 16px; }
.modal-box { background: #fff; border-radius: 24px; padding: 24px 20px; width: min(340px, 100%); text-align: center; box-shadow: var(--shadow); }
.modal-emoji { font-size: 56px; line-height: 1; }
.modal-box p { font-size: 19px; font-weight: 700; margin: 12px 0 18px; }
.modal-box .row { width: 100%; }
.modal-box button { background: #eef4fa; }
.modal-box button.go { background: var(--accent); color: #fff; }
.board-actions button { min-width: 0; }

/* Game */
#game { justify-content: space-between; gap: 8px; touch-action: none; }
.hud { width: min(480px, 100%); display: flex; justify-content: space-between; gap: 10px; }
.score-chip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: rgba(255, 255, 255, 0.92);
  border-radius: 14px;
  padding: 6px 14px;
  font-weight: 600;
  width: calc(50% - 5px);
}
.score-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score-chip b { font-size: 24px; }
.score-chip.opp.out { opacity: 0.5; }

.timer {
  width: min(480px, 100%);
  height: 12px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.5);
  overflow: hidden;
}
#timer-bar { height: 100%; width: 100%; background: var(--good); border-radius: 6px; }

.hint {
  background: rgba(255, 255, 255, 0.4);
  color: var(--ink);
  font-size: 17px;
  padding: 12px 22px;
  border-radius: 999px;
}
.hint.up { animation: nudge-up 1.6s ease-in-out infinite; }
.hint.down { animation: nudge-down 1.6s ease-in-out infinite; }
@keyframes nudge-up { 50% { transform: translateY(-5px); } }
@keyframes nudge-down { 50% { transform: translateY(5px); } }

.stage {
  position: relative;
  flex: 1;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.card {
  position: absolute;
  width: min(270px, 70vw, 42vh);
  aspect-ratio: 1;
  background: var(--card);
  border-radius: 32px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 14px;
  animation: pop 0.14s ease-out;
  will-change: transform;
}
.card .e { font-size: min(130px, 32vw, 19vh); line-height: 1.05; }
.card .n { font-size: clamp(17px, 5vw, 22px); font-weight: 800; line-height: 1.15; }
.card.dragging { transition: none; }
.card.snap { transition: transform 0.15s ease-out; }
.card.fly-up { animation: fly-up 0.35s ease-in forwards; }
.card.drop { animation: drop 0.35s ease-in forwards; }
.card.wrong { animation: shake 0.4s; box-shadow: 0 0 0 6px var(--bad), var(--shadow); }
@keyframes pop { from { transform: scale(0.6); opacity: 0; } }
@keyframes fly-up { to { transform: translateY(-80vh) rotate(-14deg) scale(0.7); opacity: 0; } }
@keyframes drop { to { transform: translateY(80vh) rotate(10deg); opacity: 0; } }
@keyframes shake {
  20%, 60% { transform: translateX(-12px); }
  40%, 80% { transform: translateX(12px); }
}

.countdown {
  font-size: 120px;
  font-weight: 800;
  color: var(--blue);
  animation: pop 0.3s ease-out;
}

/* Over */
.over-emoji { font-size: 110px; line-height: 1; }
.reason { margin: 0; font-size: 18px; font-weight: 600; max-width: 340px; }
.final { display: flex; gap: 12px; width: min(340px, 100%); }
.final > div {
  flex: 1 1 0;
  min-width: 0;
  background: #fff;
  border-radius: 18px;
  padding: 10px 12px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}
.final span { font-size: 14px; opacity: 0.75; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.final b { font-size: 36px; }
#over.win h2 { color: var(--good); }
#over.lose h2 { color: var(--bad); }

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translate(-50%, 40px);
  opacity: 0;
  background: var(--ink);
  color: #fff;
  padding: 10px 18px;
  border-radius: 12px;
  transition: 0.25s;
  pointer-events: none;
  max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
