/* ===== MOBILE LAYOUT IMPROVEMENTS ===== */

/* Fix backdrop-filter on <nav> — it creates a containing block for position:fixed
   descendants, trapping .mobile-menu inside the ~60px nav height.
   Move the blur to .nav-inner instead so fixed positioning escapes correctly. */
nav { backdrop-filter: none; }
.nav-inner { backdrop-filter: blur(12px); }

/* Date column — override muted grey so date reads clearly in all leaderboard tables */
.lb-date { color: var(--text); }

/* By-track leaderboard: halve the track column min-width (main.css sets 280px) */
.lb-track-cell { min-width: 140px; }

/* Cars-at-track and drivers lap table: match the by-track column widths */
#cars-table .lap-track:not(.lb-rank) { min-width: 140px; }
#drivers-table .lap-track:not(.lb-rank) { min-width: 140px; }
#drivers-table .lap-car { min-width: 140px; }

/* ===== HAMBURGER BUTTON ===== */
.nav-hamburger {
  display: none;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 5px;
  width: 40px;
  height: 40px;
  background: transparent;
  border: 1px solid var(--border);
  cursor: pointer;
  padding: 0;
  flex-shrink: 0;
  transition: border-color 0.2s;
}

.nav-hamburger:hover { border-color: var(--muted); }

.nav-hamburger span {
  display: block;
  width: 18px;
  height: 1.5px;
  background: var(--text);
  transition: transform 0.25s ease, opacity 0.2s ease;
  transform-origin: center;
}

.nav-hamburger.is-open span:nth-child(1) { transform: translateY(6.5px) rotate(45deg); }
.nav-hamburger.is-open span:nth-child(2) { opacity: 0; transform: scaleX(0); }
.nav-hamburger.is-open span:nth-child(3) { transform: translateY(-6.5px) rotate(-45deg); }

/* ===== MOBILE BREAKPOINT ===== */
@media (max-width: 720px) {
  .nav-inner { padding: 14px 16px; }
  nav ul { display: none; }
  .nav-right { display: none; }
  .nav-hamburger { display: flex; }

  .hero, .setups-section, .ai-section, .featured-section,
  .detail-section, .cart-section, .admin-section { padding: 48px 16px; }
  .setups-grid { grid-template-columns: 1fr; }
  .ai-inputs { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .footer-inner { flex-direction: column; text-align: center; padding: 28px 16px; gap: 10px; }
  .hero-stats { gap: 24px; }
  /* Hero CTAs stack full-width instead of running off the right edge */
  .hero-actions { gap: 10px; }
  .hero-actions .btn-primary,
  .hero-actions .btn-ghost { flex: 1 1 100%; text-align: center; }
  .admin-table { font-size: 11px; }

  /* Admin pages are the one exception to the no-horizontal-scroll rule:
     keep the desktop sidebar layout and let the whole page scroll sideways */
  html:has(.admin-layout), body:has(.admin-layout) { overflow-x: auto; }
  .page-title { font-size: 52px; }
  .sim-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ===== MOBILE MENU ===== */
.mobile-menu {
  position: fixed;
  inset: 0;
  z-index: 200;
  pointer-events: none;
  /* Keep the closed drawer out of the tab order; the delay lets the
     slide-out animation finish before it is hidden */
  visibility: hidden;
  transition: visibility 0s 0.3s;
}

.mobile-menu.is-open {
  pointer-events: auto;
  visibility: visible;
  transition: visibility 0s;
}

.mobile-menu-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(10, 10, 8, 0.7);
  backdrop-filter: blur(4px);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.mobile-menu.is-open .mobile-menu-backdrop {
  opacity: 1;
}

.mobile-menu-panel {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(320px, 85vw);
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  overflow-y: auto;
}

.mobile-menu.is-open .mobile-menu-panel {
  transform: translateX(0);
}

.mobile-menu-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.mobile-menu-close {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  width: 36px;
  height: 36px;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.mobile-menu-close:hover { border-color: var(--text); color: var(--text); }

.mobile-nav-links {
  display: flex;
  flex-direction: column;
  padding: 12px 0;
  flex: 1;
}

.mobile-nav-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 24px;
  font-size: 13px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--muted);
  text-decoration: none;
  border-bottom: 1px solid var(--border);
  transition: color 0.2s, background 0.2s;
}

.mobile-nav-link:hover { color: var(--text); background: var(--surface2); }

.mobile-nav-link.nav-active {
  color: var(--accent);
  border-left: 3px solid var(--accent);
  padding-left: 21px;
}

/* Cart badge inside nav link */
.mobile-nav-link--cart .cart-badge {
  position: static;
  display: inline-flex;
  margin-left: 4px;
}

/* Sign In link — accent colour so it pops */
.mobile-nav-link--signin { color: var(--accent); }
.mobile-nav-link--signin:hover { color: var(--text); background: var(--surface2); }

.mobile-menu-footer {
  padding: 20px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex-shrink: 0;
}

.mobile-nav-user {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.mobile-nav-name {
  font-family: 'Bebas Neue', sans-serif;
  font-size: 18px;
  letter-spacing: 1px;
  color: var(--text);
  line-height: 1;
  margin-bottom: 4px;
}

.mobile-nav-profile-link {
  font-size: 10px;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--accent);
  text-decoration: none;
}

.mobile-nav-profile-link:hover { color: var(--text); }

.mobile-nav-signout-form { margin: 0; }

.mobile-nav-signout {
  width: 100%;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 11px 20px;
  font-family: var(--font-body);
  font-size: 11px;
  letter-spacing: 2px;
  text-transform: uppercase;
  cursor: pointer;
  transition: all 0.2s;
}

.mobile-nav-signout:hover { border-color: var(--accent2); color: var(--accent2); }

/* Prevent body scroll when mobile menu is open */
body.menu-open { overflow: hidden; }

/* ===== LEADERBOARD MOBILE ===== */
@media (max-width: 720px) {
  /* Never scroll the whole page sideways — wide tables scroll inside .lb-card */
  html, body { overflow-x: hidden; }

  /* Telemetry client card: let it wrap — icon + downloads on top,
     token/sign-in panel drops to its own full-width row below */
  .telemetry-card { flex-wrap: wrap; }
  .lb-footer-card { width: 100%; }
  .lb-header-filters { width: 100%; }
  .telemetry-card-token {
    flex: 1 1 100%;
    max-width: none;
    margin-left: 0;
    padding-left: 0;
    border-left: none;
    border-top: 1px solid var(--border);
    padding-top: 14px;
  }

  /* View tabs: horizontally scrollable so long labels don't wrap */
  .lb-view-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-wrap: nowrap;
  }
  .lb-view-tabs::-webkit-scrollbar { display: none; }
  .lb-view-tab { white-space: nowrap; flex-shrink: 0; }

  /* Filter: full-width search input */
  .lb-filters { flex-wrap: wrap; gap: 8px; }
  .lb-filter-input { flex: 1 1 100%; width: 100%; }

  /* Show 7 columns (overrides main.css nth-child(n+7) hide rule) */
  .lap-table th:nth-child(7),
  .lap-table td:nth-child(7) { display: table-cell; }

  /* Table: cols 1-7 visible; hide 8-10 and 12 for main leaderboard */
  #lb-table th:nth-child(8),
  #lb-table td:nth-child(8),
  #lb-table th:nth-child(9),
  #lb-table td:nth-child(9),
  #lb-table th:nth-child(10),
  #lb-table td:nth-child(10),
  #lb-table th:nth-child(12),
  #lb-table td:nth-child(12) { display: none; }

  /* …but keep the count column visible: Cars on the track (grouped by
     track) / Tracks for the car (grouped by car) — on the main leaderboard
     and the per-track/per-car drill-down tables alike. Class-based so it
     works whatever position the optional Date/Sim columns leave it in.
     The .lap-table selector overrides main.css's nth-child(n+7) hide by
     source order; the #lb-table one outranks this file's nth-child hides. */
  .lap-table th.lb-count-col,
  .lap-table td.lb-count-col,
  #lb-table th.lb-count-col,
  #lb-table td.lb-count-col { display: table-cell; }

  /* Track and car columns — fixed readable width on mobile */
  .lb-track-cell { min-width: 170px; }
  #lb-table .lap-car { min-width: 170px; }
  #cars-table .lap-track:not(.lb-rank) { min-width: 170px; }
  #cars-table .lap-car { min-width: 170px; }
  #drivers-table .lap-track:not(.lb-rank) { min-width: 170px; }
  #drivers-table .lap-car { min-width: 170px; }

  /* Driver lap-history table: few, narrow columns — show them all
     (overrides main.css nth-child(n+7) hide: Date + Telemetry button)
     and let it scroll inside .lb-card if it overflows */
  #driver-laps-table th,
  #driver-laps-table td { display: table-cell; white-space: nowrap; }

  /* Allow table to scroll horizontally if it still overflows */
  .lb-card { overflow-x: auto; }

  /* Lap Times pages (/laps, /laps/detail): tables live in .profile-edit-card,
     not .lb-card — give them the same scroll container, show every column
     (overrides main.css nth-child(n+7) hide rule), and keep a readable
     min-width so the table scrolls instead of squashing */
  .profile-edit-card:has(.lap-table) {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .profile-edit-card .lap-table th:nth-child(n+1),
  .profile-edit-card .lap-table td:nth-child(n+1) {
    display: table-cell;
    white-space: nowrap;
  }

  /* Smaller page title on leaderboard */
  .lb-section .page-title { font-size: 42px; }
}
