:root {
  --bg:       #0f172a;
  --surface:  #1e293b;
  --surface2: #273549;
  --border:   #334155;
  --accent:   #f59e0b;
  --accent2:  #fbbf24;
  --success:  #22c55e;
  --danger:   #ef4444;
  --text:     #f1f5f9;
  --muted:    #94a3b8;
  --radius:   12px;
  --tab-h:    60px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body { display: flex; flex-direction: column; height: 100dvh; }

/* ── Setup overlay ── */
#setup-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
#setup-overlay.hidden { display: none; }

.setup-card {
  background: var(--surface);
  border-radius: 20px;
  padding: 32px 24px;
  width: 100%; max-width: 420px;
  display: flex; flex-direction: column; gap: 20px;
}
.setup-card h1 { font-size: 1.5rem; text-align: center; }
.setup-card .subtitle { color: var(--muted); font-size: 0.9rem; text-align: center; }

.setup-tabs { display: flex; gap: 8px; }
.setup-tab {
  flex: 1; padding: 10px;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; color: var(--muted);
  font-size: 0.9rem; font-weight: 600; cursor: pointer;
}
.setup-tab.active { background: var(--accent); color: #000; border-color: var(--accent); }

.setup-panel { display: none; flex-direction: column; gap: 12px; }
.setup-panel.active { display: flex; }

label { font-size: 0.85rem; color: var(--muted); }
.label-hint { font-weight: 400; font-size: 0.78rem; color: var(--border); }

input[type=text], input[type=password] {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px 14px;
  color: var(--text); font-size: 1rem; width: 100%;
  outline: none;
}
input[type=text]:focus, input[type=password]:focus { border-color: var(--accent); }

.field-hint { font-size: 0.8rem; min-height: 1rem; }
.field-hint.ok    { color: var(--success); }
.field-hint.error { color: var(--danger); }

.pin-input {
  text-align: center; letter-spacing: 0.4em;
  font-size: 1.4rem; font-family: monospace;
}

.avatar-name-row {
  display: flex; align-items: center; gap: 12px;
}
.avatar-name-row input { flex: 1; }
.avatar-preview {
  width: 52px; height: 52px; border-radius: 50%;
  background: var(--surface2); border: 2px solid var(--border);
  flex-shrink: 0; transition: opacity 0.2s;
}

.btn {
  padding: 14px 20px; border-radius: 10px; border: none;
  font-size: 1rem; font-weight: 700; cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 8px;
}
.btn-primary { background: var(--accent); color: #000; }
.btn-primary:active { opacity: 0.85; }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:active { background: var(--surface2); }
.btn-danger { background: var(--danger); color: #fff; }

/* ── Main layout ── */
#app { display: flex; flex-direction: column; flex: 1; min-height: 0; }
#app.hidden { display: none; }

.tab-content {
  flex: 1; overflow-y: auto; padding: 16px;
  display: none;
}
.tab-content.active { display: block; }

/* ── Bottom nav ── */
.bottom-nav {
  height: var(--tab-h); flex-shrink: 0;
  display: flex; background: var(--surface);
  border-top: 1px solid var(--border);
}
.nav-btn {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  background: transparent; border: none;
  color: var(--muted); font-size: 0.65rem; font-weight: 600;
  cursor: pointer; gap: 3px;
}
.nav-btn svg { width: 22px; height: 22px; }
.nav-btn.active { color: var(--accent); }

/* ── Capture tab ── */
.capture-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px;
}
.capture-header h2 { font-size: 1.1rem; }

.action-row { display: flex; gap: 10px; margin-bottom: 20px; }
.action-row .btn { flex: 1; font-size: 0.85rem; padding: 12px 8px; border-radius: 10px; }
.action-row .btn svg { width: 20px; height: 20px; }

#voice-btn { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
#voice-btn.listening { background: var(--danger); color: #fff; border-color: var(--danger); animation: pulse 1.2s infinite; }
#camera-btn { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
#camera-btn.active { background: var(--success); color: #fff; border-color: var(--success); }

@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.7} }

.search-box {
  position: relative; margin-bottom: 14px;
}
.search-box input {
  padding-left: 40px; background: var(--surface);
}
.search-box svg {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  width: 18px; height: 18px; color: var(--muted);
}

.state-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 8px;
}
.state-btn {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 4px;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  cursor: pointer; min-height: 60px; justify-content: center;
}
.state-btn:active { transform: scale(0.96); }
.state-btn .code { font-size: 1.1rem; font-weight: 800; }
.state-btn .pts { font-size: 0.65rem; color: var(--muted); }
.state-btn.spotted { background: #052e16; border-color: var(--success); }
.state-btn.spotted .code { color: var(--success); }
.state-btn.canada { background: #1c1033; border-color: #7c3aed; }
.state-btn.canada.spotted { background: #2e1065; border-color: #a78bfa; }
.state-btn.canada.spotted .code { color: #a78bfa; }

/* ── Section header ── */
.section-header {
  font-size: 0.75rem; font-weight: 700; letter-spacing: 0.08em;
  color: var(--muted); text-transform: uppercase;
  margin: 16px 0 10px;
}

/* ── Toast ── */
#toast {
  position: fixed; bottom: calc(var(--tab-h) + 16px); left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 20px; font-size: 0.9rem; font-weight: 600;
  pointer-events: none; opacity: 0; transition: all 0.25s;
  white-space: nowrap; z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.success { border-color: var(--success); color: var(--success); }
#toast.error { border-color: var(--danger); color: var(--danger); }

/* ── Leaderboard ── */
.period-tabs { display: flex; gap: 6px; margin-bottom: 16px; }
.period-tab {
  flex: 1; padding: 9px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px;
  color: var(--muted); font-size: 0.8rem; font-weight: 700; cursor: pointer;
}
.period-tab.active { background: var(--accent); color: #000; border-color: var(--accent); }

.player-row {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px 16px;
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 8px;
}
.player-rank { font-size: 1.2rem; width: 28px; text-align: center; flex-shrink: 0; }
.player-info { flex: 1; min-width: 0; }
.player-name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player-sub { font-size: 0.8rem; color: var(--muted); }
.player-pts { font-size: 1.4rem; font-weight: 800; color: var(--accent); }
.player-row.is-me { border-color: var(--accent); background: #1a1200; }

.avatar-sm {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--surface2); flex-shrink: 0;
}

.family-stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 16px;
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 16px;
}
.family-stat-label { color: var(--muted); font-size: 0.85rem; }
.family-stat-val { font-size: 1.8rem; font-weight: 800; color: var(--accent); }

/* ── US SVG Map ── */
#us-svg-map { margin-top: 12px; }
#us-svg { display: block; width: 100%; touch-action: manipulation; }

#us-svg path.state,
#us-svg .state path {
  fill: #1e293b !important;
  stroke: #334155 !important;
  stroke-width: 0.75 !important;
  stroke-opacity: 1 !important;
  fill-opacity: 1 !important;
  cursor: pointer;
  transition: fill 0.15s;
}
#us-svg path.state:hover,
#us-svg .state:hover path { fill: #273549 !important; }
#us-svg path.state.spotted,
#us-svg .state.spotted path {
  fill: #052e16 !important;
  stroke: #22c55e !important;
}

/* ── Map / tracker ── */
.geo-grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 3px;
  font-size: 0.6rem;
}
.geo-cell {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 4px; padding: 5px 2px;
  text-align: center; font-weight: 700;
  min-height: 36px; display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}
.geo-cell.empty { background: transparent; border-color: transparent; pointer-events: none; }
.geo-cell.spotted { background: #052e16; border-color: var(--success); color: var(--success); }
.geo-cell.ca-spotted { background: #2e1065; border-color: #a78bfa; color: #a78bfa; }

.progress-bar-wrap { margin: 20px 0 8px; }
.progress-label { display: flex; justify-content: space-between; font-size: 0.85rem; margin-bottom: 6px; }
.progress-label span:last-child { color: var(--accent); font-weight: 700; }
.progress-bar { height: 8px; background: var(--surface2); border-radius: 4px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--accent); border-radius: 4px; transition: width 0.4s; }
.progress-fill.ca { background: #7c3aed; }

/* ── Scrapbook ── */
.photo-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px;
}
.photo-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; overflow: hidden; cursor: pointer;
}
.photo-card img { width: 100%; aspect-ratio: 4/3; object-fit: cover; }
.photo-card-info { padding: 8px 10px; }
.photo-card-code { font-weight: 800; font-size: 1rem; }
.photo-card-meta { font-size: 0.7rem; color: var(--muted); }

.no-photos {
  text-align: center; color: var(--muted); padding: 48px 24px;
  font-size: 0.9rem;
}

/* ── Profile tab ── */
.profile-hero {
  display: flex; flex-direction: column; align-items: center;
  padding: 24px 16px 20px; gap: 10px;
}
.avatar-lg {
  width: 96px; height: 96px; border-radius: 50%;
  background: var(--surface2); border: 3px solid var(--accent);
}
.profile-name-view {
  display: flex; align-items: center; gap: 10px;
}
.profile-display-name { font-size: 1.4rem; font-weight: 800; }
.edit-name-btn {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 6px; padding: 4px 12px;
  color: var(--muted); font-size: 0.8rem; cursor: pointer;
}
.edit-name-btn:active { background: var(--surface); }
.profile-name-edit {
  display: none; width: 100%; flex-direction: column; gap: 8px;
}
.username-tag { color: var(--muted); font-size: 0.85rem; }

.profile-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 20px; margin-bottom: 12px;
}
.profile-card h3 { margin-bottom: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; font-size: 0.75rem; }
.stat-row { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); }
.stat-row:last-child { border-bottom: none; }
.stat-row span:first-child { color: var(--muted); font-size: 0.9rem; }
.stat-row span:last-child { font-weight: 700; }

.family-code-display {
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px 16px;
  font-size: 1.4rem; font-weight: 800; letter-spacing: 0.15em;
  text-align: center; margin: 8px 0; color: var(--accent);
}

/* ── Loading / spinner ── */
.spinner {
  width: 32px; height: 32px; border: 3px solid var(--surface2);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.7s linear infinite; margin: 32px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Photo lightbox ── */
#lightbox {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,0.9);
  display: none; align-items: center; justify-content: center; flex-direction: column;
  gap: 16px; padding: 24px;
}
#lightbox.show { display: flex; }
#lightbox img { max-width: 100%; max-height: 70vh; border-radius: 12px; }
#lightbox .lb-info { color: var(--text); text-align: center; }
#lightbox .lb-close {
  position: absolute; top: 20px; right: 20px;
  background: var(--surface); border: none; border-radius: 50%;
  width: 40px; height: 40px; color: var(--text); font-size: 1.2rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}

/* ── How to play ── */
.rules-list { list-style: none; display: flex; flex-direction: column; gap: 12px; }
.rules-list li {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px 16px;
  font-size: 0.9rem; line-height: 1.5;
}
.rules-list li strong { color: var(--accent); }

/* ── Camera preview ── */
#camera-preview {
  position: fixed; inset: 0; z-index: 150;
  background: #000;
  display: none; flex-direction: column;
}
#camera-preview.show { display: flex; }
#camera-preview video { flex: 1; object-fit: cover; }
#camera-preview .cam-bar {
  display: flex; gap: 12px; padding: 20px;
  background: var(--bg); align-items: center; justify-content: center;
}
#cam-capture-btn {
  width: 70px; height: 70px; border-radius: 50%;
  border: 4px solid #fff; background: transparent; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
#cam-capture-btn::after {
  content: ''; width: 56px; height: 56px;
  background: #fff; border-radius: 50%;
}
#cam-cancel-btn {
  position: absolute; top: 16px; left: 16px;
  background: rgba(0,0,0,0.5); border: none; border-radius: 50%;
  width: 40px; height: 40px; color: #fff; font-size: 1.2rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}

canvas#cam-canvas { display: none; }

@media (min-width: 500px) {
  .state-grid { grid-template-columns: repeat(auto-fill, minmax(80px, 1fr)); }
}
