:root {
  --bg: #0f1115;
  --panel: #171a21;
  --border: #2a2e38;
  --text: #e8e9ec;
  --muted: #8a8f9c;
  --accent: #4f8cff;
  --accent-dim: #2c4a86;
  --filled: #35c46a;
  --danger: #e5584f;
}

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

html { touch-action: manipulation; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior-y: contain;
}

header {
  padding: max(20px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 16px max(16px, env(safe-area-inset-left));
  max-width: 720px;
  margin: 0 auto;
}

h1 { margin: 0 0 4px; font-size: 1.6rem; }
.subtitle { margin: 0 0 16px; color: var(--muted); font-size: 0.92rem; }

.name-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}
.name-row label { font-size: 0.85rem; color: var(--muted); white-space: nowrap; }
.name-row input {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px;
  border-radius: 8px;
  font-size: 16px; /* prevents iOS Safari auto-zoom on focus */
}

.track-wrap { margin-bottom: 16px; }
.track {
  position: relative;
  height: 48px;
  border-radius: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  overflow: hidden;
}
.track-line {
  position: absolute;
  top: 50%;
  left: 0;
  right: 34px;
  height: 2px;
  transform: translateY(-50%);
  background-image: repeating-linear-gradient(90deg, var(--border) 0 14px, transparent 14px 26px);
}
.finish-flag {
  position: absolute;
  top: 50%;
  right: 6px;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  border-radius: 2px;
  background-image: conic-gradient(var(--text) 90deg, #111 90deg 180deg, var(--text) 180deg 270deg, #111 270deg);
  background-size: 8px 8px;
}
.car {
  position: absolute;
  top: 50%;
  left: 0%;
  transform: translate(-50%, -50%);
  color: var(--accent);
  transition: left 1.3s cubic-bezier(0.22, 0.61, 0.36, 1);
}
.track-stats {
  display: flex;
  justify-content: space-between;
  margin-top: 6px;
  font-size: 0.85rem;
  color: var(--muted);
}
.track-stats span:last-child { color: var(--text); font-weight: 500; }

.upload-next-btn {
  width: 100%;
  padding: 14px;
  font-size: 1.05rem;
  font-weight: 600;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 10px;
  cursor: pointer;
}
.upload-next-btn:active { background: var(--accent-dim); }

main {
  padding: 0 max(16px, env(safe-area-inset-right)) max(40px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  max-width: 720px;
  margin: 0 auto;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
  gap: 4px;
  margin-top: 20px;
}

.cell {
  position: relative;
  aspect-ratio: 1;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--muted);
  font-size: 0.72rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
  font-variant-numeric: tabular-nums;
}
.cell.filled {
  border-color: var(--filled);
  color: white;
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0,0,0,0.6);
}
.cell.filled img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: brightness(0.55);
}
.cell.filled span { position: relative; z-index: 1; }
.cell.next-target {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-dim);
}
.cell.locked {
  opacity: 0.35;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
}
.modal.hidden { display: none; }
.modal-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  max-width: 420px;
  width: 100%;
  position: relative;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-close {
  position: absolute;
  top: 10px; right: 14px;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.5rem;
  cursor: pointer;
}
#modalImageWrap {
  width: 100%;
  aspect-ratio: 4/3;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 10px;
}
#modalImageWrap.empty { display: none; }
#modalImage { width: 100%; height: 100%; object-fit: cover; }
.modal-meta { color: var(--muted); font-size: 0.85rem; min-height: 1.2em; }
.modal-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px;
  border-radius: 8px;
  font-size: 16px; /* prevents iOS Safari auto-zoom on focus */
  margin-top: 12px;
}
.modal-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.modal-actions button {
  padding: 12px;
  border-radius: 8px;
  border: none;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  background: var(--accent);
  color: white;
}
.modal-actions .secondary { background: var(--border); color: var(--text); }
.modal-actions .danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.hidden { display: none !important; }
