:root {
  --bg: #0b1020;
  --bg2: #121a2f;
  --card: #161f38;
  --border: rgba(255, 255, 255, 0.08);
  --text: #eef2ff;
  --muted: #8b95b5;
  --primary: #5b8cff;
  --primary-press: #3d6fe0;
  --accent: #3dd6c6;
  --danger: #ff6b7a;
  --warn: #ffc857;
  --given: #eef2ff;
  --user: #7eb6ff;
  --conflict: #ff6b7a;
  --select: rgba(91, 140, 255, 0.22);
  --same: rgba(91, 140, 255, 0.1);
  --peer: #a78bfa;
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  --mono: "SF Mono", ui-monospace, Menlo, monospace;
}

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

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

input {
  user-select: text;
  -webkit-user-select: text;
}

body {
  min-height: 100dvh;
  background:
    radial-gradient(1200px 600px at 50% -10%, rgba(91, 140, 255, 0.18), transparent 60%),
    radial-gradient(800px 400px at 100% 100%, rgba(61, 214, 198, 0.08), transparent 50%),
    var(--bg);
}

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: calc(12px + var(--safe-top)) 16px calc(16px + var(--safe-bot));
  display: flex;
  flex-direction: column;
}

.screen {
  display: none;
  flex-direction: column;
  gap: 16px;
  flex: 1;
}

.screen.active {
  display: flex;
}

.hero {
  text-align: center;
  padding: 28px 8px 8px;
}

.logo {
  font-size: 48px;
  line-height: 1;
  filter: drop-shadow(0 8px 24px rgba(91, 140, 255, 0.35));
}

h1 {
  margin: 12px 0 6px;
  font-size: 1.85rem;
  letter-spacing: -0.03em;
  font-weight: 750;
}

.tagline {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
  line-height: 1.4;
}

.card {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.04), transparent 40%), var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.stack.tight {
  gap: 10px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}

.field input {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg2);
  color: var(--text);
  border-radius: 12px;
  padding: 14px 14px;
  font-size: 1.05rem;
  font-weight: 600;
  outline: none;
  text-transform: none;
  letter-spacing: normal;
}

#home-code {
  text-transform: uppercase;
  letter-spacing: 0.18em;
  font-family: var(--mono);
  text-align: center;
  font-size: 1.25rem;
}

.field input:focus {
  border-color: rgba(91, 140, 255, 0.55);
  box-shadow: 0 0 0 3px rgba(91, 140, 255, 0.15);
}

.seg {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  background: var(--bg2);
  padding: 4px;
  border-radius: 12px;
  border: 1px solid var(--border);
}

.seg button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 650;
  font-size: 0.9rem;
  padding: 10px 6px;
  border-radius: 9px;
  cursor: pointer;
}

.seg button.on {
  background: var(--primary);
  color: white;
  box-shadow: 0 4px 14px rgba(91, 140, 255, 0.35);
}

.btn {
  appearance: none;
  border: 0;
  border-radius: 14px;
  padding: 15px 18px;
  font: inherit;
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  transition: transform 0.08s ease, opacity 0.15s ease, background 0.15s ease;
}

.btn:active {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

.btn.primary {
  background: linear-gradient(180deg, #6d9aff, var(--primary));
  color: white;
  box-shadow: 0 8px 24px rgba(91, 140, 255, 0.35);
}

.btn.primary:active:not(:disabled) {
  background: var(--primary-press);
}

.btn.secondary {
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
}

.btn.ghost:active {
  background: rgba(255, 255, 255, 0.05);
}

.btn.small {
  padding: 8px 12px;
  font-size: 0.85rem;
  border-radius: 10px;
}

.btn.hidden,
.hidden {
  display: none !important;
}

.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 600;
}

.divider::before,
.divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

.footer-note {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  line-height: 1.45;
  margin: 8px 4px 0;
  padding: 0 8px;
}

/* Lobby */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.room-badge {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.room-badge strong {
  font-family: var(--mono);
  letter-spacing: 0.2em;
  font-size: 1.15rem;
}

.muted {
  color: var(--muted);
}

.muted.small {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 650;
}

.lobby-diff {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(61, 214, 198, 0.12);
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.players {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.player-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--bg2);
  border: 1px solid var(--border);
}

.player-row .avatar {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-weight: 800;
  background: linear-gradient(135deg, var(--primary), var(--peer));
  color: white;
  flex-shrink: 0;
}

.player-row .meta {
  flex: 1;
  min-width: 0;
}

.player-row .name {
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.player-row .sub {
  font-size: 0.8rem;
  color: var(--muted);
}

.player-row .status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
}

.player-row.ready .status-dot {
  background: var(--accent);
  box-shadow: 0 0 0 4px rgba(61, 214, 198, 0.18);
}

.player-row.you {
  border-color: rgba(91, 140, 255, 0.35);
}

.player-row.empty {
  opacity: 0.55;
  border-style: dashed;
}

.hint {
  margin: 0;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.4;
  text-align: center;
}

/* Game race bar */
.race-bar {
  display: flex;
  align-items: stretch;
  gap: 12px;
}

.timer-block {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 14px;
  min-width: 96px;
}

.timer {
  font-family: var(--mono);
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.race-players {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
  justify-content: center;
}

.race-p {
  display: grid;
  grid-template-columns: 72px 1fr 36px;
  align-items: center;
  gap: 8px;
  font-size: 0.78rem;
}

.race-p .rn {
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.race-p .rn.you {
  color: var(--user);
}

.race-p .rn.peer {
  color: var(--peer);
}

.bar {
  height: 8px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.06);
  overflow: hidden;
}

.bar > i {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: inherit;
  transition: width 0.25s ease;
  background: linear-gradient(90deg, var(--primary), var(--accent));
}

.race-p.peer .bar > i {
  background: linear-gradient(90deg, var(--peer), #c4b5fd);
}

.race-p .pct {
  text-align: right;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-family: var(--mono);
  font-size: 0.72rem;
}

/* Board */
.board-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 4px 0;
}

.board {
  --gap: 1px;
  width: min(100%, 92vw, 420px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  gap: var(--gap);
  background: #1a2440;
  border: 2px solid #2a3658;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.4);
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  background: var(--bg2);
  font-size: clamp(1rem, 5.2vw, 1.45rem);
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  color: var(--user);
}

/* 3x3 box borders */
.cell.box-r {
  box-shadow: inset -2px 0 0 #2a3658;
}
.cell.box-b {
  box-shadow: inset 0 -2px 0 #2a3658;
}
.cell.box-r.box-b {
  box-shadow: inset -2px 0 0 #2a3658, inset 0 -2px 0 #2a3658;
}

.cell.given {
  color: var(--given);
  font-weight: 750;
}

.cell.selected {
  background: var(--select);
}

.cell.same-num:not(.selected) {
  background: var(--same);
}

.cell.hl-row:not(.selected):not(.same-num),
.cell.hl-col:not(.selected):not(.same-num) {
  background: rgba(255, 255, 255, 0.03);
}

.cell.conflict {
  color: var(--conflict) !important;
  background: rgba(255, 107, 122, 0.12);
}

.cell .notes {
  position: absolute;
  inset: 2px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  font-size: clamp(0.45rem, 2.2vw, 0.62rem);
  font-weight: 600;
  color: var(--muted);
  line-height: 1;
  pointer-events: none;
}

.cell .notes span {
  display: grid;
  place-items: center;
  opacity: 0;
}

.cell .notes span.on {
  opacity: 1;
}

/* Controls */
.controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: auto;
}

.tool-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.tool {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  border-radius: 12px;
  padding: 10px 8px;
  font: inherit;
  font-weight: 650;
  font-size: 0.82rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
}

.tool-icon {
  font-size: 1.1rem;
  line-height: 1.2;
}

.tool[aria-pressed="true"] {
  background: rgba(91, 140, 255, 0.18);
  border-color: rgba(91, 140, 255, 0.45);
  color: #b8d0ff;
}

.numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px;
}

.numpad button {
  appearance: none;
  border: 0;
  background: var(--card);
  color: var(--text);
  border-radius: 12px;
  aspect-ratio: 1;
  max-height: 52px;
  font: inherit;
  font-weight: 750;
  font-size: 1.2rem;
  cursor: pointer;
  border: 1px solid var(--border);
  position: relative;
}

.numpad button:active {
  background: rgba(91, 140, 255, 0.2);
}

.numpad button .rem {
  position: absolute;
  bottom: 3px;
  right: 5px;
  font-size: 0.55rem;
  color: var(--muted);
  font-weight: 600;
}

.numpad button.done {
  opacity: 0.35;
}

.submit-btn {
  width: 100%;
}

/* Overlays */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(6, 10, 22, 0.72);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  padding: 24px;
}

.countdown-num {
  font-size: 6rem;
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.04em;
  text-shadow: 0 0 60px rgba(91, 140, 255, 0.6);
  animation: pop 0.35s ease;
}

.countdown-label {
  color: var(--muted);
  text-align: center;
  margin-top: 8px;
  font-weight: 650;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-size: 0.85rem;
}

@keyframes pop {
  from {
    transform: scale(0.5);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.result-card {
  width: min(100%, 360px);
  text-align: center;
}

.result-emoji {
  font-size: 3rem;
  line-height: 1;
  margin-bottom: 8px;
}

.result-card h2 {
  margin: 0 0 6px;
  font-size: 1.5rem;
}

.result-times {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 16px 0;
  text-align: left;
}

.result-times .rt {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--bg2);
  border: 1px solid var(--border);
  font-size: 0.9rem;
}

.result-times .rt.winner {
  border-color: rgba(255, 200, 87, 0.4);
  background: rgba(255, 200, 87, 0.08);
}

.result-times .time {
  font-family: var(--mono);
  font-weight: 700;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-bot));
  transform: translateX(-50%);
  z-index: 100;
  background: #1e293b;
  color: white;
  padding: 12px 18px;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 600;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  border: 1px solid var(--border);
  max-width: calc(100% - 32px);
  text-align: center;
  animation: toast-in 0.25s ease;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}

@media (min-height: 780px) {
  .numpad button {
    max-height: 56px;
  }
}

@media (max-height: 700px) {
  .hero {
    padding-top: 12px;
  }
  .logo {
    font-size: 36px;
  }
  h1 {
    font-size: 1.5rem;
  }
  .submit-btn {
    padding: 12px;
  }
}
