:root {
  --blue: #2f5bd7;
  --blue-hover: #2449b5;
  --ink: #162235;
  --muted: #6f7d8f;
  --soft-muted: #8d98a6;
  --panel: rgba(248, 250, 252, 0.92);
  --panel-solid: #f4f6f8;
  --line: rgba(38, 56, 79, 0.11);
  --line-strong: rgba(38, 56, 79, 0.17);
  --shadow: 0 24px 65px rgba(47, 72, 98, 0.16);
  --danger: #bb2d3b;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; width: 100%; }
html { background: #d9eef6; -webkit-text-size-adjust: 100%; }
body {
  min-height: 100vh;
  min-height: 100svh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
  color: var(--ink);
  background:
    radial-gradient(circle at 15% 0%, rgba(255,255,255,.72), transparent 31%),
    linear-gradient(180deg, #c9ebf8 0%, #eaf7fb 100%);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; }
button, a, input { touch-action: manipulation; }
button { -webkit-tap-highlight-color: transparent; }
button, a { transition: transform .16s ease, background-color .16s ease, border-color .16s ease, box-shadow .16s ease, opacity .16s ease; }
.hidden { display: none !important; }

.topbar {
  position: relative;
  z-index: 20;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  min-height: 58px;
  padding: 9px clamp(12px, 2vw, 26px);
  background: rgba(247, 251, 253, 0.72);
  backdrop-filter: blur(18px) saturate(130%);
  border-bottom: 1px solid rgba(255,255,255,.78);
  box-shadow: 0 1px 0 rgba(52, 78, 104, .07);
}
.brand-block { display: flex; align-items: center; gap: 10px; min-width: 0; }
.topbar h1 {
  margin: 0;
  font-size: clamp(20px, 2vw, 24px);
  line-height: 1;
  letter-spacing: -.035em;
  font-weight: 900;
}
.player-badge {
  min-width: 0;
  max-width: 190px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(78, 96, 120, .08);
  color: #667589;
  font-size: 11px;
  font-weight: 800;
}
.top-actions { display: flex; align-items: center; gap: 7px; }
.back-btn, .icon-btn {
  min-height: 38px;
  border: 1px solid transparent;
  border-radius: 11px;
  display: inline-grid;
  place-items: center;
  cursor: pointer;
  text-decoration: none;
  font-weight: 850;
}
.back-btn {
  padding: 8px 15px;
  background: var(--blue);
  color: #fff;
  box-shadow: 0 6px 16px rgba(47,91,215,.18);
}
.back-btn:hover { background: var(--blue-hover); transform: translateY(-1px); }
.icon-btn {
  width: 38px;
  padding: 0;
  background: rgba(255,255,255,.72);
  border-color: rgba(49, 66, 88, .14);
  color: var(--ink);
}
.icon-btn:hover { background: #fff; transform: translateY(-1px); box-shadow: 0 5px 14px rgba(37,51,69,.09); }
.icon-btn svg { width: 18px; height: 18px; display: block; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.hud {
  position: relative;
  z-index: 3;
  display: flex;
  justify-content: center;
  gap: 7px;
  padding: 8px 12px 7px;
}
.hud-box {
  min-width: 78px;
  padding: 6px 10px 7px;
  text-align: center;
  border: 1px solid rgba(255,255,255,.74);
  border-radius: 12px;
  background: rgba(250,252,253,.76);
  backdrop-filter: blur(12px);
  box-shadow: 0 5px 16px rgba(49, 70, 92, .06);
}
.hud-label {
  color: #7e8a99;
  font-size: 9px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .09em;
}
.hud-value { margin-top: 1px; font-size: 15px; font-weight: 900; line-height: 1.15; }

.game-layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 760px) 310px;
  justify-content: center;
  align-items: center;
  gap: 18px;
  padding: 0 18px 12px;
}
.game-area {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.canvas-wrap {
  position: relative;
  width: 100%;
  height: min(78vh, 880px);
  height: min(78dvh, 880px);
  min-height: min(460px, calc(100dvh - 150px));
  overflow: hidden;
  border-radius: 26px;
  border: 1px solid rgba(255,255,255,.9);
  background: linear-gradient(180deg, #7fd6ff 0%, #b8efff 70%, #e7fdff 100%);
  box-shadow: var(--shadow), inset 0 0 0 1px rgba(43,68,92,.06);
}
canvas { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; -webkit-user-select: none; }
.tap-bar { padding: 6px 8px 0; color: #728093; font-size: 11px; font-weight: 750; text-align: center; }

.overlay {
  position: absolute;
  inset: 0;
  z-index: 8;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(31, 45, 59, .24);
  backdrop-filter: blur(2.5px);
}
.overlay-card {
  width: min(88%, 330px);
  padding: 22px 22px 20px;
  border: 1px solid rgba(255,255,255,.84);
  border-radius: 18px;
  background: rgba(249,250,251,.96);
  color: #101827;
  text-align: center;
  box-shadow: 0 20px 52px rgba(25,39,54,.21);
}
.overlay-card h2 { margin: 0 0 7px; font-size: 24px; letter-spacing: -.025em; }
.overlay-card p { margin: 0 0 15px; color: #667386; font-size: 14px; line-height: 1.45; }
.main-btn {
  min-height: 40px;
  padding: 9px 17px;
  border: 0;
  border-radius: 10px;
  background: var(--blue);
  color: #fff;
  font-weight: 850;
  cursor: pointer;
  box-shadow: 0 7px 16px rgba(47,91,215,.19);
}
.main-btn:hover:not(:disabled) { background: var(--blue-hover); transform: translateY(-1px); }
.main-btn:disabled { opacity: .5; cursor: not-allowed; box-shadow: none; }
.wide { width: 100%; }

.leaderboard-panel {
  align-self: center;
  width: 100%;
  max-height: min(78vh, 880px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 16px;
  border: 1px solid rgba(255,255,255,.82);
  border-radius: 20px;
  background: rgba(247, 249, 251, .9);
  backdrop-filter: blur(18px) saturate(125%);
  box-shadow: 0 18px 48px rgba(46,69,91,.12), inset 0 0 0 1px rgba(39,58,79,.035);
}
.leaderboard-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.leaderboard-head h2 { margin: 1px 0 0; font-size: 21px; line-height: 1.15; letter-spacing: -.025em; }
.leaderboard-subtitle { margin: 4px 0 0; color: var(--muted); font-size: 11px; font-weight: 650; }
.eyebrow {
  display: block;
  margin-bottom: 2px;
  color: #8b95a2;
  font-size: 8px;
  font-weight: 900;
  letter-spacing: .16em;
}
.drawer-close {
  display: none;
  width: 31px;
  height: 31px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: rgba(255,255,255,.74);
  color: #5e6b7d;
  font-size: 21px;
  line-height: 1;
  cursor: pointer;
}
.leaderboard-columns {
  display: grid;
  grid-template-columns: 36px minmax(0,1fr) auto;
  align-items: center;
  gap: 8px;
  padding: 0 9px 6px;
  color: #98a2af;
  font-size: 8px;
  font-weight: 850;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.leaderboard-columns span:last-child { text-align: right; }
.leaderboard-message { padding: 18px 4px; color: #7c8898; font-size: 12px; text-align: center; }
.leaderboard-entries {
  min-height: 0;
  overflow: auto;
  display: grid;
  gap: 6px;
  scrollbar-width: thin;
  scrollbar-color: rgba(77,92,110,.25) transparent;
}
.leaderboard-row {
  display: grid;
  grid-template-columns: 36px minmax(0,1fr) auto;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 7px 9px;
  border: 1px solid rgba(49,65,85,.07);
  border-radius: 12px;
  background: rgba(255,255,255,.64);
}
.leaderboard-row.rank-1 { background: linear-gradient(90deg, rgba(255,244,196,.72), rgba(255,255,255,.72)); border-color: rgba(195,150,37,.2); }
.leaderboard-row.rank-2 { background: linear-gradient(90deg, rgba(232,237,243,.82), rgba(255,255,255,.72)); }
.leaderboard-row.rank-3 { background: linear-gradient(90deg, rgba(242,219,202,.68), rgba(255,255,255,.72)); }
.leaderboard-row.is-you {
  border-color: rgba(47,91,215,.36);
  box-shadow: inset 0 0 0 1px rgba(47,91,215,.06), 0 4px 13px rgba(47,91,215,.08);
}
.rank-icon {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  color: #8a96a5;
  font-size: 11px;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}
.rank-icon.numeric { background: rgba(78,96,120,.07); }
.crown-svg { width: 24px; height: 18px; display: block; filter: drop-shadow(0 1px 1px rgba(15,23,42,.1)); }
.crown-gold { --crown-fill: #efc44d; --crown-stroke: #a87516; }
.crown-silver { --crown-fill: #cfd7df; --crown-stroke: #7c8896; }
.crown-bronze { --crown-fill: #c98a60; --crown-stroke: #8a4c2b; }
.crown-svg path { fill: var(--crown-fill); stroke: var(--crown-stroke); stroke-width: 1.25; }
.player-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #233145;
  font-size: 12px;
  font-weight: 800;
}
.you-pill {
  margin-left: 6px;
  padding: 2px 5px 2px;
  border-radius: 999px;
  background: rgba(47,91,215,.11);
  color: var(--blue);
  font-size: 7px;
  font-weight: 900;
  text-transform: uppercase;
  vertical-align: 1px;
}
.player-score { color: #27364a; font-variant-numeric: tabular-nums; font-size: 12px; font-weight: 900; }
.leaderboard-toggle { display: none; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(37,48,60,.43);
  backdrop-filter: blur(7px);
}
.modal {
  position: fixed;
  inset: 0;
  z-index: 41;
  display: grid;
  place-items: center;
  padding: 16px;
}
.modal-card {
  position: relative;
  width: min(390px, 100%);
  padding: 20px;
  border: 1px solid rgba(52,65,81,.13);
  border-radius: 17px;
  background: #f1f3f5;
  box-shadow: 0 22px 62px rgba(19,31,43,.27), inset 0 1px 0 rgba(255,255,255,.88);
}
#usernameModal .modal-card { width: min(360px, 100%); padding: 19px; }
.modal-card h2 {
  margin: 1px 0 6px;
  color: #1c2838;
  font-size: 21px;
  line-height: 1.2;
  letter-spacing: -.025em;
}
#usernameModal .modal-card h2 { font-size: 20px; }
.modal-card > p {
  margin: 0 0 15px;
  color: #737f8e;
  font-size: 12px;
  line-height: 1.45;
}
.modal-card label:not(.confirm-row) {
  display: block;
  margin-bottom: 5px;
  color: #344256;
  font-size: 10px;
  font-weight: 850;
}
.modal-card input[type="text"], .modal-card input:not([type]) {
  width: 100%;
  min-height: 39px;
  padding: 8px 10px;
  border: 1px solid #c7cdd5;
  border-radius: 9px;
  outline: none;
  background: #fafbfc;
  color: #172335;
  font-size: 13px;
  box-shadow: inset 0 1px 2px rgba(30,41,59,.035);
}
.modal-card input::placeholder { color: #a0a8b2; }
.modal-card input:focus {
  border-color: #7f9ce8;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(80,116,216,.12);
}
.field-help { margin: 5px 0 0; color: #87919d; font-size: 9px; }
.form-error { min-height: 16px; margin: 5px 0 7px; color: var(--danger); font-size: 10px; font-weight: 700; }
.modal-close {
  position: absolute;
  top: 10px;
  right: 11px;
  width: 30px;
  height: 30px;
  border: 1px solid rgba(43,56,73,.1);
  border-radius: 9px;
  background: rgba(255,255,255,.46);
  color: #687586;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.confirm-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin: 11px 0 3px;
  color: #657285;
  font-size: 11px;
  line-height: 1.35;
  cursor: pointer;
}
.confirm-row input { margin-top: 1px; }
#usernameModal .main-btn { min-height: 39px; border-radius: 9px; font-size: 13px; }

.toast {
  position: fixed;
  z-index: 70;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  max-width: min(430px, calc(100vw - 28px));
  padding: 9px 13px;
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 10px;
  background: #263344;
  color: #fff;
  font-size: 12px;
  font-weight: 750;
  box-shadow: 0 13px 32px rgba(15,23,42,.24);
}

.celebration-layer {
  position: absolute;
  inset: 0;
  z-index: 10;
  pointer-events: none;
  overflow: hidden;
}
.celebration-message {
  position: absolute;
  left: 50%;
  top: 16px;
  transform: translateX(-50%) translateY(-10px);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: max-content;
  max-width: calc(100% - 28px);
  min-height: 42px;
  padding: 9px 13px;
  border: 1px solid rgba(255,255,255,.86);
  border-radius: 12px;
  background: rgba(250,251,252,.96);
  color: #253247;
  text-align: center;
  font-weight: 850;
  font-size: 13px;
  box-shadow: 0 10px 28px rgba(21,34,48,.18);
  animation: celebrationToast 2.6s ease forwards;
  will-change: transform, opacity;
}
.celebration-message.top1 { border-color: rgba(214,168,51,.28); box-shadow: 0 10px 28px rgba(21,34,48,.18), 0 0 0 3px rgba(239,196,77,.08); }
.celebration-crown { flex: 0 0 auto; width: 25px; height: 19px; }
.confetti-piece {
  position: absolute;
  top: -16px;
  width: 6px;
  height: 9px;
  border-radius: 2px;
  opacity: .76;
  animation: confettiFall var(--fall, 1550ms) linear forwards;
  will-change: transform, opacity;
}
@keyframes celebrationToast {
  0% { opacity: 0; transform: translateX(-50%) translateY(-10px) scale(.97); }
  12%, 74% { opacity: 1; transform: translateX(-50%) translateY(0) scale(1); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-6px) scale(.99); }
}
@keyframes confettiFall { to { transform: translate3d(var(--drift, 0px), 72dvh, 0) rotate(440deg); opacity: .08; } }

@media (max-width: 1080px) {
  .game-layout { grid-template-columns: minmax(0, 700px) 285px; gap: 14px; }
}

/* Desktop-only placement. Keep this above the mobile drawer breakpoint. */
@media (min-width: 981px) {
  .leaderboard-panel {
    align-self: flex-start;
    margin-top: 12px;
  }
}

@media (max-width: 980px) {
  .game-layout { display: block; padding: 0 8px max(8px, env(safe-area-inset-bottom)); overflow: hidden; }
  .canvas-wrap {
    width: min(98vw, 700px);
    height: min(76vh, 880px);
    height: min(76dvh, 880px);
    min-height: min(420px, calc(100dvh - 145px));
    margin: 0 auto;
  }
  .leaderboard-toggle { display: inline-grid; }
  .leaderboard-panel {
    position: fixed;
    z-index: 35;
    top: max(68px, calc(58px + env(safe-area-inset-top)));
    right: max(10px, env(safe-area-inset-right));
    bottom: max(10px, env(safe-area-inset-bottom));
    width: min(330px, calc(100vw - 20px));
    max-height: none;
    transform: translateX(calc(100% + 24px));
    transition: transform .22s ease;
    box-shadow: 0 24px 60px rgba(24,38,53,.22);
  }
  .leaderboard-panel.open { transform: translateX(0); }
  .drawer-close { display: grid; place-items: center; }
}

@media (max-width: 560px) {
  .topbar {
    min-height: 53px;
    padding: max(7px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 7px max(8px, env(safe-area-inset-left));
  }
  .brand-block { display: block; }
  .topbar h1 { font-size: 18px; }
  .player-badge { display: block; width: max-content; max-width: 130px; margin-top: 3px; padding: 2px 6px; font-size: 9px; }
  .top-actions { gap: 5px; }
  .back-btn { min-height: 34px; padding: 7px 10px; font-size: 11px; }
  .icon-btn { width: 34px; min-height: 34px; border-radius: 9px; }
  .hud { padding: 5px 6px; }
  .hud-box { min-width: 65px; padding: 5px 6px; border-radius: 10px; }
  .hud-label { font-size: 8px; }
  .hud-value { font-size: 13px; }
  .canvas-wrap {
    height: calc(100vh - 143px);
    height: calc(100dvh - 143px);
    min-height: 0;
    max-height: calc(100dvh - 143px);
    border-radius: 19px;
  }
  .tap-bar { padding-top: 4px; font-size: 10px; }
  .modal {
    padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }
  .modal-card, #usernameModal .modal-card { padding: 17px; border-radius: 15px; }
  .modal-card input[type="text"], .modal-card input:not([type]) { font-size: 16px; }
  .modal-card h2, #usernameModal .modal-card h2 { font-size: 19px; }
  .celebration-message { top: 10px; min-height: 38px; padding: 7px 10px; font-size: 11px; }
  .celebration-crown { width: 21px; height: 16px; }
}

@media (max-height: 680px) {
  .canvas-wrap {
    min-height: 0;
    height: calc(100vh - 135px);
    height: calc(100dvh - 135px);
  }
}

@media (max-height: 500px) {
  .modal { overflow-y: auto; align-items: start; }
  .modal-card { margin: 10px 0; }
}

/* Compact landscape phones: keep the actual game usable instead of clipping it. */
@media (max-height: 500px) and (orientation: landscape) {
  .topbar { min-height: 44px; padding-top: max(4px, env(safe-area-inset-top)); padding-bottom: 4px; }
  .topbar h1 { font-size: 16px; }
  .player-badge { display: none; }
  .hud { padding: 3px 6px; }
  .hud-box { min-width: 58px; padding: 3px 6px 4px; }
  .hud-label { font-size: 7px; }
  .hud-value { font-size: 11px; }
  .canvas-wrap { height: calc(100dvh - 91px); min-height: 0; border-radius: 14px; }
  .tap-bar { display: none; }
  .leaderboard-panel { top: max(48px, calc(44px + env(safe-area-inset-top))); }
  .celebration-message { top: 6px; }
}

.toast {
  bottom: max(18px, env(safe-area-inset-bottom));
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
