/* ========================================
   Design System — 50 States Marathon Tracker
   ======================================== */

/* --- Custom Properties --- */
:root {
  --bg-primary: #0f172a;
  --bg-surface: #1e293b;
  --bg-muted: #334155;
  --text-primary: #f1f5f9;
  --text-secondary: #cbd5e1;
  --text-tertiary: #94a3b8;
  --border: #475569;
  --state-completed: #22c55e;
  --state-planned: #f59e0b;
  --state-empty: #3b4f6b;
  --accent: #0ea5e9;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --shadow-lg: 0 20px 60px rgba(0, 0, 0, 0.5);
  --font-body: 'Inter', system-ui, -apple-system, sans-serif;
  --font-heading: 'Manrope', 'Inter', system-ui, sans-serif;
}

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

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-body);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  min-height: 100vh;
}

/* --- Layout --- */
.app {
  position: relative;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
}

/* --- Header --- */
.header {
  text-align: center;
  padding: 48px 0 32px;
}

.header-title {
  font-family: var(--font-heading);
  font-weight: 700;
  font-size: 2.5rem;
  letter-spacing: -0.02em;
  margin-bottom: 8px;
  background: linear-gradient(135deg, var(--text-primary) 0%, var(--accent) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.header-subtitle {
  font-size: 1.125rem;
  color: var(--text-secondary);
  font-weight: 400;
}

/* --- Progress Section --- */
.progress-section {
  text-align: center;
  margin-bottom: 40px;
}

.progress-counter {
  font-family: var(--font-heading);
  font-weight: 700;
  font-size: 3.5rem;
  color: var(--state-completed);
  line-height: 1.1;
}

.progress-counter .counter-total {
  color: var(--text-tertiary);
  font-size: 2rem;
}

.progress-label {
  color: var(--text-secondary);
  font-size: 0.95rem;
  margin-top: 4px;
  margin-bottom: 16px;
}

.progress-bar {
  width: 100%;
  max-width: 500px;
  height: 12px;
  background: var(--bg-muted);
  border-radius: 6px;
  margin: 0 auto;
  overflow: hidden;
  position: relative;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--state-completed), #34d399);
  border-radius: 6px;
  transition: width 1.5s cubic-bezier(0.22, 1, 0.36, 1);
  position: relative;
}

.progress-fill::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(
    90deg,
    transparent 0%,
    rgba(255, 255, 255, 0.2) 50%,
    transparent 100%
  );
  animation: shimmer 2s ease-in-out infinite;
}

@keyframes shimmer {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

/* --- Error Banner --- */
.error-banner {
  background: rgba(239, 68, 68, 0.15);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: #fca5a5;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  text-align: center;
  margin-bottom: 24px;
  font-size: 0.9rem;
}

/* --- Map Section --- */
.map-section {
  margin-bottom: 48px;
}

.map-wrapper {
  background: var(--bg-surface);
  border-radius: var(--radius-md);
  padding: 24px;
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
  position: relative;
}

.map-svg {
  width: 100%;
  height: auto;
  display: block;
}

.state-path {
  transition: fill 0.2s ease;
  outline: none;
}

.state-path.focused {
  filter: brightness(1.3);
}

.state-path:focus-visible {
  filter: brightness(1.3);
  outline: none;
}

/* --- Map Tooltip --- */
.map-tooltip {
  position: absolute;
  background: var(--bg-surface);
  color: var(--text-primary);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 500;
  pointer-events: none;
  transition: opacity 0.15s ease;
  border: 1px solid var(--border);
  white-space: nowrap;
  z-index: 10;
}

/* --- Legend --- */
.legend {
  display: flex;
  justify-content: center;
  gap: 24px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.legend-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
}

.legend-dot.completed { background: var(--state-completed); }
.legend-dot.planned { background: var(--state-planned); }
.legend-dot.remaining { background: var(--state-empty); border: 1px solid var(--border); }

/* --- Stats Grid --- */
.stats-section {
  margin-bottom: 64px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}

.stat-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.stat-value {
  font-family: var(--font-heading);
  font-weight: 700;
  font-size: 1.75rem;
  color: var(--text-primary);
  line-height: 1.2;
}

.stat-value.accent {
  color: var(--state-completed);
}

.stat-value.pr {
  color: var(--accent);
}

.stat-label {
  font-size: 0.8rem;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-top: 4px;
}

.stat-sublabel {
  font-size: 0.8rem;
  color: var(--text-tertiary);
  margin-top: 2px;
}

/* --- Overlay Backdrop --- */
.overlay-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 100;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.overlay-backdrop.active {
  opacity: 1;
  visibility: visible;
}

/* --- Overlay Modal --- */
.overlay {
  position: fixed;
  z-index: 101;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  overflow-y: auto;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s ease;

  /* Desktop: centered card */
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.95);
  width: 90%;
  max-width: 560px;
  max-height: 80vh;
  border-radius: var(--radius-md);
}

.overlay.active {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, -50%) scale(1);
}

.overlay-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: none;
  border: none;
  color: var(--text-tertiary);
  font-size: 1.75rem;
  cursor: pointer;
  line-height: 1;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: color 0.15s ease, background 0.15s ease;
}

.overlay-close:hover {
  color: var(--text-primary);
  background: var(--bg-muted);
}

.overlay-close:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.overlay-content {
  padding: 32px;
}

.overlay-header {
  margin-bottom: 24px;
}

.overlay-state-abbrev {
  display: inline-block;
  font-family: var(--font-heading);
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--state-completed);
  background: rgba(34, 197, 94, 0.12);
  padding: 4px 10px;
  border-radius: 4px;
  letter-spacing: 0.05em;
  margin-bottom: 8px;
}

.overlay-state-name {
  font-family: var(--font-heading);
  font-weight: 700;
  font-size: 1.75rem;
  color: var(--text-primary);
}

.overlay-race-info {
  margin-bottom: 20px;
}

.overlay-race-name {
  font-family: var(--font-heading);
  font-weight: 600;
  font-size: 1.15rem;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.overlay-date {
  color: var(--text-secondary);
  font-size: 0.95rem;
}

.overlay-time {
  font-family: var(--font-heading);
  font-weight: 700;
  font-size: 1.5rem;
  color: var(--accent);
  margin-top: 8px;
}

.overlay-distance {
  color: var(--text-tertiary);
  font-size: 0.85rem;
  margin-top: 4px;
}

.overlay-notes {
  background: var(--bg-primary);
  border-radius: var(--radius-sm);
  padding: 16px;
  margin-top: 16px;
}

.overlay-notes p {
  color: var(--text-secondary);
  font-size: 0.95rem;
  line-height: 1.7;
  white-space: pre-wrap;
}

.overlay-planned {
  text-align: center;
  padding: 20px 0;
}

.overlay-planned-badge {
  display: inline-block;
  font-family: var(--font-heading);
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--state-planned);
  background: rgba(245, 158, 11, 0.12);
  padding: 6px 14px;
  border-radius: 20px;
  margin-bottom: 16px;
}

.overlay-planned .overlay-race-name {
  font-size: 1.25rem;
}

/* --- Footer --- */
.footer {
  text-align: center;
  padding: 32px 0;
  color: var(--text-tertiary);
  font-size: 0.8rem;
  border-top: 1px solid var(--border);
}

/* --- Back Arrow --- */
.back-arrow {
  position: absolute;
  top: 16px;
  left: 24px;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  color: var(--text-tertiary);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 50%;
  text-decoration: none;
  transition: color 0.2s ease, background 0.2s ease, border-color 0.2s ease;
}

.back-arrow:hover {
  color: var(--text-primary);
  background: var(--bg-muted);
  border-color: var(--text-tertiary);
}

/* --- Fade-in Animation --- */
.fade-in-section {
  opacity: 0;
  transform: translateY(20px);
  transition: opacity 0.6s ease, transform 0.6s ease;
}

.fade-in-section.visible {
  opacity: 1;
  transform: translateY(0);
}

/* --- Responsive --- */
@media (min-width: 640px) {
  .stats-grid {
    grid-template-columns: repeat(4, 1fr);
  }

  .map-wrapper {
    padding: 32px;
  }
}

@media (min-width: 1024px) {
  .header-title {
    font-size: 3rem;
  }

  .progress-counter {
    font-size: 4rem;
  }
}

@media (max-width: 639px) {
  .header {
    padding: 32px 0 24px;
  }

  .header-title {
    font-size: 1.75rem;
  }

  .progress-counter {
    font-size: 2.5rem;
  }

  .map-wrapper {
    padding: 12px;
    border-radius: var(--radius-sm);
  }

  .stat-value {
    font-size: 1.35rem;
  }

  /* Mobile: overlay as bottom sheet */
  .overlay {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    transform: translateY(100%);
    width: 100%;
    max-width: 100%;
    max-height: 85vh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  }

  .overlay.active {
    transform: translateY(0);
  }
}

/* --- Reduced Motion --- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .progress-fill::after {
    animation: none;
  }

  .fade-in-section {
    opacity: 1;
    transform: none;
  }
}
