:root {
  --ink: #f3f4fa;
  --muted: #a4a9c3;
  --panel: rgba(13, 15, 28, 0.62);
  --line: rgba(255, 255, 255, 0.26);
  --tile: rgba(255, 255, 255, 0.05);
  --accent: #7c5cff;
  --match: #2f9e51;
  --key: #3a3f5c;
  --key-wide: #565c82;
  --tile-size: min(15.5vw, 64px, 8.2vh);
  --gap: 6px;
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #0f1220;
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}

button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }

/* Full-screen photo */
.bg {
  position: fixed;
  inset: 0;
  background: #0f1220 url("../assets/bg.jpg") center 35% / cover no-repeat;
}

/* Blurred dark panel that holds the whole game */
.app {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(100% - 20px, 500px);
  height: calc(100dvh - 20px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  margin: calc(10px + env(safe-area-inset-top)) auto 0;
  padding: 10px 8px 6px;
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.5);
}

.header { position: relative; text-align: center; flex: none; }
.account-btn {
  position: absolute;
  top: 0;
  right: 2px;
  max-width: 34%;
  padding: 6px 11px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  font-size: 0.76rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.account-btn:hover { background: rgba(255, 255, 255, 0.16); }
.header h1 { margin: 0; font-size: clamp(1.5rem, 6vw, 2rem); font-weight: 800; letter-spacing: 0.01em; }
.logo { display: inline-block; animation: bob 3s ease-in-out infinite; }
.subtitle { margin: 2px 0 0; font-size: 1.05rem; font-weight: 700; letter-spacing: 0.08em; }
.goal { margin: 2px 0 0; font-size: 0.82rem; color: var(--muted); }
.goal b { color: #5fd583; letter-spacing: 0.06em; }

/* Board */
.board-wrap {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 10px 0;
  scrollbar-width: thin;
}
.board { display: flex; flex-direction: column; align-items: center; gap: var(--gap); min-height: 100%; justify-content: flex-start; }
.row { display: grid; grid-template-columns: repeat(4, var(--tile-size)); gap: var(--gap); }
.tile {
  width: var(--tile-size);
  height: var(--tile-size);
  display: grid;
  place-items: center;
  font-size: calc(var(--tile-size) * 0.52);
  font-weight: 800;
  text-transform: uppercase;
  background: var(--tile);
  border: 1.5px solid var(--line);
  border-radius: 6px;
  user-select: none;
}
.row.current .tile { border-style: dashed; }
.row.current .tile.filled { border-style: solid; border-color: rgba(255, 255, 255, 0.7); animation: pop 0.1s ease-out; }
.tile.match { background: var(--match); border-color: var(--match); color: #fff; }
.row.win .tile { box-shadow: 0 0 14px rgba(95, 213, 131, 0.55); }
.row.reveal .tile { animation: flip 0.45s ease both; }
.row.reveal .tile:nth-child(2) { animation-delay: 0.07s; }
.row.reveal .tile:nth-child(3) { animation-delay: 0.14s; }
.row.reveal .tile:nth-child(4) { animation-delay: 0.21s; }
.row.shake { animation: shake 0.45s; }

/* Keyboard */
.keyboard { flex: none; display: flex; flex-direction: column; gap: 6px; padding: 6px 0 4px; touch-action: manipulation; }
.krow { display: flex; justify-content: center; gap: 5px; }
.key {
  flex: 1 1 0;
  min-width: 0;
  max-width: 44px;
  height: clamp(42px, 6.6vh, 56px);
  padding: 0;
  border: 0;
  border-radius: 7px;
  background: var(--key);
  font-weight: 700;
  font-size: 1rem;
  text-transform: uppercase;
  user-select: none;
}
.key.wide { flex: 1.6 1 0; max-width: 70px; background: var(--key-wide); font-size: 0.72rem; letter-spacing: 0.03em; }
.key.wide.icon { font-size: 1.25rem; }
.key:active, .key.pressed { filter: brightness(1.5); transform: translateY(1px); }
.keyboard.done .key { opacity: 0.45; }

.footer { flex: none; display: flex; justify-content: center; flex-wrap: wrap; gap: 2px 14px; padding: 4px 0 2px; }
.link { background: none; border: 0; padding: 4px 2px; font-size: 0.78rem; color: var(--muted); text-decoration: underline; text-underline-offset: 3px; }
.link:hover { color: var(--ink); }

/* Toast */
.toast {
  position: fixed;
  top: 18%;
  left: 50%;
  transform: translate(-50%, -8px);
  padding: 10px 16px;
  border-radius: 10px;
  background: #f3f4fa;
  color: #14172a;
  font-weight: 700;
  font-size: 0.92rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
  z-index: 40;
  white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Emoji rain */
.rain { position: fixed; inset: 0; overflow: hidden; pointer-events: none; z-index: 20; }
.drop { position: absolute; top: -12vh; animation: fall linear forwards; will-change: transform; }

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(5, 6, 14, 0.62);
  animation: fade 0.18s ease-out;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  position: relative;
  width: min(100%, 400px);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  padding: 22px 20px 20px;
  background: #171a2e;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 18px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
  animation: rise 0.22s ease-out;
}
.modal h2 { margin: 0 26px 10px; font-size: 1.3rem; }
.modal p { margin: 8px 0; line-height: 1.45; font-size: 0.95rem; }
.modal .small { font-size: 0.8rem; color: var(--muted); }
.modal-close { position: absolute; top: 8px; right: 10px; width: 36px; height: 36px; border: 0; background: none; font-size: 1.6rem; line-height: 1; color: var(--muted); }
.modal h3 { margin: 16px 0 6px; font-size: 0.78rem; letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); }
.btn {
  display: inline-block;
  margin-top: 14px;
  padding: 12px 22px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  font-size: 1rem;
}
.btn:hover { filter: brightness(1.12); }
.big { font-size: 1.1rem; font-weight: 700; }
.big b { color: #5fd583; }

.mini { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.mini .row { grid-template-columns: repeat(4, 32px); gap: 4px; }
.mini .tile { width: 32px; height: 32px; font-size: 16px; border-radius: 4px; border-width: 1px; }
.example { display: flex; align-items: center; justify-content: center; gap: 10px; margin: 12px 0; }
.example .arrow { font-size: 1.3rem; color: var(--muted); }
.paths { display: flex; justify-content: center; gap: 22px; }

.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 6px 0; }
.stat b { display: block; font-size: 1.6rem; }
.stat span { font-size: 0.7rem; color: var(--muted); line-height: 1.2; display: block; }
.dist { display: grid; grid-template-columns: auto 1fr; gap: 6px 8px; align-items: center; text-align: left; }
.dist .label { font-size: 0.85rem; font-weight: 700; text-align: right; }
.dist .bar { min-width: 26px; padding: 3px 8px; border-radius: 4px; background: var(--key); font-size: 0.8rem; font-weight: 700; text-align: right; }
.dist .bar.best { background: var(--match); color: #fff; }

/* Forms (name, account) */
.field {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--ink);
  font: inherit;
  font-size: 1rem; /* 16px keeps iOS from zooming */
  text-align: center;
}
.field:focus { outline: none; border-color: var(--accent); }
.form { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.form .btn { margin-top: 0; }
.form-row { display: flex; flex-wrap: wrap; gap: 8px; }
.form-row .field { flex: 1 1 170px; min-width: 0; text-align: left; }
.form-row .btn { flex: 1 0 auto; }
.form-row .btn { padding: 10px 16px; font-size: 0.9rem; border-radius: 10px; }
.error { min-height: 1.2em; margin: 2px 0 0; color: #ff8d8d; font-size: 0.85rem; font-weight: 600; }
.btn.secondary { background: var(--key); }
.btn.google { background: #fff; color: #1f1f1f; }
.btn:disabled { opacity: 0.55; cursor: default; }
.divider { display: flex; align-items: center; gap: 10px; margin: 12px 0 2px; color: var(--muted); font-size: 0.78rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: rgba(255, 255, 255, 0.16); }

/* Leaderboard */
.tabs { display: flex; gap: 6px; padding: 4px; margin: 4px 0 12px; border-radius: 999px; background: rgba(255, 255, 255, 0.07); }
.tab { flex: 1; padding: 8px 10px; border: 0; border-radius: 999px; background: none; font-weight: 700; font-size: 0.9rem; color: var(--muted); }
.tab.active { background: var(--accent); color: #fff; }
.lb { width: 100%; border-collapse: collapse; font-size: 0.92rem; text-align: left; }
.lb th { padding: 4px 8px; font-size: 0.68rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); font-weight: 700; }
.lb td { padding: 7px 8px; border-top: 1px solid rgba(255, 255, 255, 0.08); }
.lb .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.lb .rank { width: 2.4em; color: var(--muted); font-variant-numeric: tabular-nums; }
.lb .name { max-width: 0; width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.lb tr.me td { background: rgba(124, 92, 255, 0.28); }
.lb tr.me td:first-child { border-radius: 8px 0 0 8px; }
.lb tr.me td:last-child { border-radius: 0 8px 8px 0; }
.lb tr.gap td { padding: 2px 8px; text-align: center; color: var(--muted); border-top: 0; }
.lb-wrap { max-height: 52vh; overflow-y: auto; }

@keyframes bob { 50% { transform: translateY(-3px) rotate(-6deg); } }
@keyframes pop { from { transform: scale(0.86); } }
@keyframes flip { 0% { transform: rotateX(90deg) scale(0.9); } 60% { transform: rotateX(-12deg) scale(1.06); } 100% { transform: none; } }
@keyframes shake { 10%, 90% { transform: translateX(-2px); } 20%, 80% { transform: translateX(4px); } 30%, 50%, 70% { transform: translateX(-7px); } 40%, 60% { transform: translateX(7px); } }
@keyframes fall { to { transform: translateY(125vh) rotate(var(--spin, 360deg)); } }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(14px) scale(0.97); } }

/* Tablets and desktops: let more of the photo show */
@media (min-width: 700px) and (min-height: 700px) {
  .app { height: min(calc(100dvh - 48px), 900px); margin-top: max(24px, calc((100dvh - 900px) / 2)); padding: 16px 16px 10px; }
  .key { max-width: 46px; }
}

@media (prefers-reduced-motion: reduce) {
  .logo, .row.reveal .tile, .row.shake, .modal, .modal-backdrop { animation: none; }
}

.actions { display: flex; justify-content: center; flex-wrap: wrap; gap: 10px; }
