/* ===========================
   DESIGN TOKENS
=========================== */
:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;

  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;

  --accent: #2563eb;
  --accent-light: #eff6ff;
  --accent-hover: #1d4ed8;

  --green: #16a34a;
  --green-light: #f0fdf4;
  --danger: #dc2626;
  --danger-light: #fef2f2;
  --orange: #ea580c;
  --orange-light: #fff7ed;

  --nav-height: 68px;
  --header-height: 106px;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.05), 0 2px 4px rgba(0,0,0,0.04);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.08), 0 4px 10px rgba(0,0,0,0.05);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-xl: 20px;

  --font: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* ===========================
   RESET & BASE
=========================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { scroll-behavior: smooth; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-primary);
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button { font-family: var(--font); cursor: pointer; }
a { font-family: var(--font); }

/* ===========================
   HEADER
=========================== */
.app-header {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 200;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  padding: 0.75rem 1rem 0.5rem;
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.6rem;
}

.logo {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.logo-icon { font-size: 1.5rem; line-height: 1; }

.logo-title {
  font-size: 1.1rem;
  font-weight: 800;
  color: var(--accent);
  letter-spacing: -0.5px;
  line-height: 1.2;
}

.logo-sub {
  font-size: 0.7rem;
  color: var(--text-muted);
  font-weight: 400;
  line-height: 1.2;
  margin-top: 1px;
}

.filter-toggle-btn {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--surface);
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 0.5rem 0.75rem;
  color: var(--text-secondary);
  font-size: 0.85rem;
  font-weight: 600;
  min-height: 44px;
  min-width: 44px;
  position: relative;
  transition: all 0.15s ease;
  flex-shrink: 0;
}

.filter-toggle-btn:hover,
.filter-toggle-btn.active {
  background: var(--accent-light);
  border-color: var(--accent);
  color: var(--accent);
}

.filter-label { white-space: nowrap; }

.filter-badge {
  position: absolute;
  top: -6px; right: -6px;
  background: var(--danger);
  color: white;
  font-size: 0.65rem;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  border: 2px solid var(--surface);
}

/* Search */
.search-bar {
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 0.85rem;
  color: var(--text-muted);
  pointer-events: none;
  flex-shrink: 0;
}

.search-input {
  width: 100%;
  background: var(--surface-2);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0.65rem 2.5rem 0.65rem 2.75rem;
  color: var(--text-primary);
  font-size: 0.95rem;
  font-family: var(--font);
  min-height: 44px;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.search-input:focus {
  outline: none;
  background: var(--surface);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.search-input::placeholder { color: var(--text-muted); }

.search-clear {
  position: absolute;
  right: 0.75rem;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1rem;
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s;
}
.search-clear:hover { color: var(--text-primary); }

/* ===========================
   ACTIVE FILTER PILLS
=========================== */
.active-filters {
  position: fixed;
  top: var(--header-height);
  left: 0; right: 0;
  z-index: 150;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0.5rem 1rem;
  display: flex;
  gap: 0.5rem;
  flex-wrap: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.active-filters::-webkit-scrollbar { display: none; }

.active-pill {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  background: var(--accent-light);
  color: var(--accent);
  border: 1px solid rgba(37, 99, 235, 0.2);
  border-radius: 20px;
  padding: 0.25rem 0.6rem;
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  min-height: 30px;
}

.active-pill:hover { background: #dbeafe; }
.active-pill .pill-remove { font-size: 0.9rem; line-height: 1; }

/* ===========================
   FILTER DRAWER
=========================== */
.filter-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 300;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  backdrop-filter: blur(2px);
}

.filter-overlay.open {
  opacity: 1;
  pointer-events: all;
}

.filter-drawer {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 400;
  background: var(--surface);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  box-shadow: 0 -10px 30px rgba(0,0,0,0.12);
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  will-change: transform;
}

.filter-drawer.open {
  transform: translateY(0);
}

.filter-drawer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.25rem 1.25rem 0.75rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.filter-drawer-title {
  font-size: 1.1rem;
  font-weight: 700;
}

.filter-close-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  color: var(--text-secondary);
  min-width: 44px;
  min-height: 44px;
  transition: background 0.15s;
}
.filter-close-btn:hover { background: var(--border); }

.filter-drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  -webkit-overflow-scrolling: touch;
}

.filter-section-title {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 0.6rem;
}

.filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.chip {
  background: var(--surface-2);
  border: 1.5px solid var(--border);
  border-radius: 20px;
  padding: 0.4rem 0.9rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
  min-height: 36px;
  transition: all 0.15s ease;
}

.chip:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.filter-drawer-footer {
  display: flex;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
  padding-bottom: max(1rem, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.filter-reset-btn {
  flex: 0 0 auto;
  background: none;
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 0.75rem 1.25rem;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-secondary);
  min-height: 48px;
  transition: all 0.15s;
}
.filter-reset-btn:hover { background: var(--surface-2); }

.filter-apply-btn {
  flex: 1;
  background: var(--accent);
  border: none;
  border-radius: var(--radius-md);
  padding: 0.75rem 1.25rem;
  font-size: 0.95rem;
  font-weight: 700;
  color: white;
  min-height: 48px;
  transition: background 0.15s;
}
.filter-apply-btn:hover { background: var(--accent-hover); }
.filter-apply-btn:active { transform: scale(0.98); }

/* ===========================
   MAIN CONTENT
=========================== */
.main-content {
  margin-top: var(--header-height);
  padding: 1rem 1rem calc(var(--nav-height) + 1.5rem);
  min-height: calc(100vh - var(--header-height) - var(--nav-height));
}

.main-content.filters-active {
  margin-top: calc(var(--header-height) + 38px);
}

/* ===========================
   RACE CARDS
=========================== */
.month-group { margin-bottom: 2rem; }

.month-header {
  font-size: 0.8rem;
  font-weight: 800;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 0.75rem;
  padding: 0.25rem 0.5rem;
  background: var(--border);
  border-radius: var(--radius-sm);
  display: inline-block;
}

.race-list {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.race-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0.85rem 1rem;
  box-shadow: var(--shadow-sm);
  display: grid;
  grid-template-columns: 64px 1fr auto;
  gap: 0.85rem;
  align-items: start;
  transition: box-shadow 0.2s, transform 0.2s;
  position: relative;
  overflow: hidden;
}

/* Certified badge stripe */
.race-card.certified::before {
  content: '';
  position: absolute;
  top: 0; left: 0;
  width: 4px;
  height: 100%;
  background: var(--accent);
  border-radius: 4px 0 0 4px;
}

/* Large/famous races get a slightly different look */
.race-card.featured {
  border-color: rgba(37, 99, 235, 0.25);
  background: linear-gradient(135deg, #ffffff 0%, #f8fbff 100%);
}

.race-card:hover {
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
}

.race-card:active { transform: scale(0.99); }

/* Date badge */
.date-badge {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--accent-light);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.25rem;
  color: var(--accent);
  min-width: 56px;
}

.date-badge .d-month {
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1;
  opacity: 0.8;
}

.date-badge .d-day {
  font-size: 1.7rem;
  font-weight: 800;
  line-height: 1;
  margin-top: 1px;
  letter-spacing: -1px;
}

.date-badge .d-dow {
  font-size: 0.65rem;
  font-weight: 500;
  opacity: 0.7;
  line-height: 1;
}

/* Card body */
.race-body { display: flex; flex-direction: column; gap: 0.3rem; min-width: 0; }

.race-name {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.35;
  word-break: break-all;
}

.race-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem 0.75rem;
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.race-meta-item { display: flex; align-items: center; gap: 0.2rem; }

.race-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-top: 0.2rem;
}

.tag {
  background: var(--surface-2);
  color: var(--text-secondary);
  padding: 0.15rem 0.5rem;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 500;
  border: 1px solid var(--border);
}

.tag.certified-tag {
  background: var(--accent-light);
  color: var(--accent);
  border-color: rgba(37, 99, 235, 0.2);
}

/* Card actions */
.race-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.fav-btn {
  background: none;
  border: none;
  color: var(--border-strong);
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: color 0.2s, background 0.2s;
}

.fav-btn:hover { background: var(--danger-light); color: var(--danger); }
.fav-btn.active { color: var(--danger); }
.fav-btn.active svg { fill: currentColor; }

.link-btn {
  background: var(--accent);
  color: white;
  text-decoration: none;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0.45rem 0.7rem;
  border-radius: var(--radius-sm);
  transition: background 0.15s;
  white-space: nowrap;
  min-height: 32px;
  display: flex;
  align-items: center;
}

.link-btn:hover { background: var(--accent-hover); }

/* ===========================
   TIMELINE (GANTT)
=========================== */
.timeline-view {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow-x: auto;
  box-shadow: var(--shadow-sm);
  -webkit-overflow-scrolling: touch;
}

.timeline-grid {
  display: grid;
  grid-template-columns: 150px repeat(12, minmax(48px, 1fr));
  min-width: 720px;
}

.tl-header {
  font-size: 0.75rem;
  font-weight: 700;
  text-align: center;
  padding: 0.65rem 0.25rem;
  border-bottom: 2px solid var(--border);
  color: var(--text-muted);
  background: var(--surface-2);
  position: sticky;
}

.tl-header.tl-sticky { position: sticky; left: 0; z-index: 10; text-align: left; padding-left: 0.75rem; border-right: 1px solid var(--border); }
.tl-header.tl-current { background: var(--accent-light); color: var(--accent); }

.tl-row { display: contents; }

.tl-name {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0 0.5rem 0 0.75rem;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  position: sticky;
  left: 0;
  background: var(--surface);
  z-index: 5;
  display: flex;
  align-items: center;
  min-height: 44px;
  transition: background 0.15s;
  color: var(--text-primary);
}

.tl-row:hover .tl-name,
.tl-row:hover .tl-cell { background: var(--accent-light); }

.tl-cell {
  border-bottom: 1px solid var(--border);
  border-right: 1px dashed #f0f4f8;
  position: relative;
  min-height: 44px;
  transition: background 0.15s;
}
.tl-cell:last-child { border-right: none; }

.tl-dot {
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 10px; height: 10px;
  background: var(--accent);
  border-radius: 50%;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.18);
  cursor: pointer;
  z-index: 3;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.tl-dot.certified { background: var(--green); box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.18); }

.tl-dot:hover,
.tl-dot:focus {
  transform: translate(-50%, -50%) scale(1.6);
  box-shadow: 0 0 0 5px rgba(37, 99, 235, 0.2);
}

/* Tooltip on tl-dot */
.tl-dot::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--text-primary);
  color: white;
  padding: 4px 8px;
  border-radius: 6px;
  font-size: 0.7rem;
  font-family: var(--font);
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
}
.tl-dot:hover::after { opacity: 1; }

/* ===========================
   MAP VIEW
=========================== */
.map-view {
  width: 100%;
  height: 55vh;
  min-height: 360px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.map-instruction {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--accent-light);
  border: 1px solid rgba(37, 99, 235, 0.2);
  border-radius: var(--radius-md);
  padding: 0.85rem 1rem;
  margin-bottom: 0.75rem;
  font-size: 0.85rem;
  color: var(--text-primary);
}

.map-instruction strong { display: block; font-weight: 700; margin-bottom: 2px; }
.map-instruction span { color: var(--text-secondary); font-size: 0.8rem; }
.map-instruction-icon { font-size: 1.5rem; flex-shrink: 0; }

.map-filter-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0.65rem 1rem;
  box-shadow: var(--shadow-sm);
}

.map-circle-info {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.map-filter-btn {
  background: var(--accent);
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.5rem 1rem;
  font-size: 0.82rem;
  font-weight: 700;
  color: white;
  min-height: 36px;
  transition: background 0.15s;
}
.map-filter-btn:hover { background: var(--accent-hover); }

/* ===========================
   FAVORITES VIEW
=========================== */
.favorites-view-wrapper { /* same as race-grid */ }

/* ===========================
   EMPTY STATE
=========================== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  padding: 4rem 2rem;
  color: var(--text-muted);
  text-align: center;
}

.empty-state-emoji { font-size: 3.5rem; line-height: 1; }
.empty-state-title { font-size: 1.1rem; font-weight: 700; color: var(--text-secondary); }
.empty-state-sub { font-size: 0.88rem; line-height: 1.6; max-width: 260px; }

/* ===========================
   BOTTOM NAV
=========================== */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 200;
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -4px 12px rgba(0,0,0,0.06);
  display: flex;
  height: var(--nav-height);
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.68rem;
  font-weight: 600;
  padding: 0.5rem 0.25rem;
  transition: color 0.15s;
  position: relative;
  min-height: 44px;
}

.nav-btn svg { transition: transform 0.2s; }
.nav-btn:hover svg { transform: translateY(-2px); }

.nav-btn.active {
  color: var(--accent);
}

.nav-btn.active::before {
  content: '';
  position: absolute;
  top: 0; left: 50%;
  transform: translateX(-50%);
  width: 32px; height: 3px;
  background: var(--accent);
  border-radius: 0 0 3px 3px;
}

.nav-badge {
  position: absolute;
  top: 4px; right: calc(50% - 18px);
  background: var(--danger);
  color: white;
  font-size: 0.6rem;
  font-weight: 800;
  min-width: 16px;
  height: 16px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
  border: 1.5px solid var(--surface);
}

/* ===========================
   RESULT COUNT
=========================== */
.result-count {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
  margin-bottom: 0.75rem;
}

/* ===========================
   RESPONSIVE
=========================== */
@media (min-width: 640px) {
  :root { --header-height: 110px; }
  .app-header { padding: 1rem 1.5rem 0.75rem; }
  .main-content { padding: 1.25rem 1.5rem calc(var(--nav-height) + 1.5rem); }
}
