/* ============================================================
   ADDED: Mobile-first UI Overrides & App-like Experience
   ============================================================ */

/* --- Theme System --- */
:root {
  --sheet-bg: #0B0E14;
  --sheet-text: #f8f9fa;
  --sheet-secondary: #a9abbd;
  --sheet-handle: rgba(255,255,255,0.2);
  --nav-bg: #0B0E14;
  --nav-border: rgba(255,255,255,0.08);
  --nav-text: #5e6073;
  --nav-text-active: #f8f9fa;
  --tab-content-bg: #0B0E14;
  --overlay-bg: rgba(0,0,0,0.5);
  --ptr-bg: rgba(11,14,20,0.95);
  --nav-height: 60px;
  --header-height: 80px;
}

.light-theme {
  --sheet-bg: #ffffff;
  --sheet-text: #1a1a2e;
  --sheet-secondary: #5e6073;
  --sheet-handle: rgba(0,0,0,0.15);
  --nav-bg: #ffffff;
  --nav-border: rgba(0,0,0,0.08);
  --nav-text: #999;
  --nav-text-active: #1a1a2e;
  --tab-content-bg: #f4f4f4;
  --overlay-bg: rgba(0,0,0,0.3);
  --ptr-bg: rgba(255,255,255,0.95);
}

/* --- Bottom Navigation Dock --- */
.mobile-nav-dock {
  display: none;
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: var(--nav-height);
  background: var(--nav-bg);
  border-top: 1px solid var(--nav-border);
  z-index: 1000;
  justify-content: space-around;
  align-items: center;
  padding: 0 env(safe-area-inset-bottom);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
}

.mobile-nav-btn {
  background: none;
  border: none;
  color: var(--nav-text);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 0.55rem;
  font-weight: 600;
  gap: 1px;
  cursor: pointer;
  padding: 4px 8px;
  font-family: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  letter-spacing: 0.03em;
  transition: color 0.2s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  min-width: 56px;
  flex: 1;
  max-width: 80px;
  position: relative;
}

.mobile-nav-btn i {
  width: 20px;
  height: 20px;
  transition: transform 0.2s ease;
}

.mobile-nav-btn span {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

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

.mobile-nav-btn.active i {
  transform: scale(1.1);
}

.mobile-nav-btn.active::after {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 24px;
  height: 2px;
  background: var(--neon-gold);
  border-radius: 0 0 2px 2px;
}

/* --- Mobile Tab Panels (structural only; content from .app-body) --- */
.mobile-tab-panels {
  display: none;
}

/* --- Bottom Sheet --- */
.bottom-sheet {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 1001;
  background: var(--sheet-bg);
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  box-shadow: 0 -4px 30px rgba(0,0,0,0.4);
  transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
  will-change: transform;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
}

.bottom-sheet.hidden-sheet {
  transform: translateY(100%) !important;
  pointer-events: none;
}

.bottom-sheet.peek {
  transform: translateY(calc(-1 * (100% - 80px)));
}

.bottom-sheet.open {
  transform: translateY(0);
}

body.sheet-open {
  overflow: hidden;
}

.sheet-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

.sheet-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.sheet-handle-area {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px 0 4px;
  cursor: grab;
  touch-action: pan-y;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.sheet-handle-area:active {
  cursor: grabbing;
}

.sheet-handle {
  width: 40px;
  height: 5px;
  background: var(--sheet-handle);
  border-radius: 3px;
  transition: background 0.2s ease;
}

.sheet-content {
  padding: 8px 16px 16px;
  overflow-y: auto;
  flex: 1;
  -webkit-overflow-scrolling: touch;
  color: var(--sheet-text);
}

.sheet-content::-webkit-scrollbar {
  width: 3px;
}

.sheet-content::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.1);
  border-radius: 2px;
}

.sheet-content h3 {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--sheet-secondary);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 8px;
}

.sheet-content h2 {
  font-size: 1.2rem;
  font-weight: 700;
  margin-bottom: 4px;
}

.sheet-content .stat-row {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  font-size: 0.85rem;
}

.sheet-content .stat-label {
  color: var(--sheet-secondary);
}

.sheet-content .stat-value {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
}

/* --- Theme Toggle Button --- */
.theme-toggle-btn {
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.1);
  color: var(--text-secondary);
  border-radius: 50%;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  -webkit-tap-highlight-color: transparent;
  margin-left: auto;
  flex-shrink: 0;
}

.theme-toggle-btn:hover {
  background: rgba(255,255,255,0.1);
  color: var(--neon-cyan);
}

.theme-toggle-btn i {
  width: 16px;
  height: 16px;
}

/* --- Pull-to-refresh indicator --- */
.pull-indicator {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 60px;
  z-index: 999;
  justify-content: center;
  align-items: flex-end;
  padding-bottom: 8px;
  background: var(--ptr-bg);
  border-bottom: 1px solid var(--nav-border);
  font-size: 0.75rem;
  color: var(--nav-text);
  font-family: 'JetBrains Mono', monospace;
  transform: translateY(-100%);
  transition: transform 0.3s ease;
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
}

.pull-indicator.visible {
  transform: translateY(0);
}

.pull-indicator .spinner {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255,255,255,0.1);
  border-top-color: var(--neon-cyan);
  border-radius: 50%;
  animation: ptr-spin 0.6s linear infinite;
  margin-right: 8px;
  display: none;
}

.pull-indicator.loading .spinner {
  display: inline-block;
}

@keyframes ptr-spin {
  to { transform: rotate(360deg); }
}

/* ============================================================
   Mobile Responsive Overrides (max 767px)
   ============================================================ */
@media (max-width: 767px) {
  body {
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
  }

  .app-container {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
  }

  /* --- Header compact on mobile --- */
  .app-header {
    padding: 0 8px !important;
    height: var(--header-height) !important;
    flex-wrap: nowrap !important;
    overflow: hidden !important;
    flex-shrink: 0;
    z-index: 100;
  }

  .header-logo .logo-text h1 {
    font-size: 0.75rem !important;
  }

  .header-standings-hud {
    gap: 4px !important;
  }

  .podium-card {
    padding: 0 6px !important;
    height: 36px !important;
    gap: 4px !important;
    min-width: 0 !important;
  }

  .podium-card.rank-1 {
    height: 42px !important;
  }

  .podium-party-name {
    font-size: 0.5rem !important;
  }

  .podium-seats {
    font-size: 0.65rem !important;
  }

  .podium-rank-badge {
    width: 16px !important;
    height: 16px !important;
    font-size: 0.45rem !important;
  }

  .podium-micro-beam {
    display: none !important;
  }

  /* --- App body fills middle area --- */
  .app-body {
    position: relative !important;
    flex: 1 !important;
    display: flex !important;
    flex-direction: column !important;
    overflow: hidden !important;
    min-height: 0 !important;
  }

  /* --- Tab-based visibility control --- */

  /* MAP TAB: show map-viewport full */
  .app-body.tab-map .sidebar-left,
  .app-body.tab-map .sidebar-right {
    display: none !important;
  }
  .app-body.tab-map .map-viewport {
    display: flex !important;
    flex: 1 !important;
    position: relative !important;
    left: auto !important;
    right: auto !important;
    top: auto !important;
    bottom: auto !important;
    height: 100% !important;
    min-height: 0 !important;
  }

  /* STANDINGS TAB: show sidebar-left with analytics panel */
  .app-body.tab-standings .map-viewport,
  .app-body.tab-standings .sidebar-right {
    display: none !important;
  }
  .app-body.tab-standings .sidebar-left {
    display: flex !important;
    position: static !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    height: 100% !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    border-radius: 0 !important;
    background: var(--tab-content-bg) !important;
    backdrop-filter: none !important;
    scroll-behavior: smooth;
    padding: 12px !important;
  }
  .app-body.tab-standings .sidebar-left #panel-analytics {
    display: flex !important;
    flex-direction: column;
    gap: 12px;
  }
  .app-body.tab-standings .sidebar-left #panel-streets {
    display: none !important;
  }
  .app-body.tab-standings .sidebar-left .layer-selector-grid {
    display: none !important;
  }

  /* INSIGHTS TAB: show sidebar-right (drawer) */
  .app-body.tab-insights .sidebar-left,
  .app-body.tab-insights .map-viewport {
    display: none !important;
  }
  .app-body.tab-insights .sidebar-right {
    display: flex !important;
    position: static !important;
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    border-radius: 0 !important;
    background: var(--tab-content-bg) !important;
    backdrop-filter: none !important;
    padding: 0 !important;
    transform: none !important;
  }
  .app-body.tab-insights .sidebar-right .drawer-empty {
    padding: 40px 20px !important;
  }

  /* ANALYTICS TAB: show sidebar-left with sub-tab switching (Spatial/Booth) */
  .app-body.tab-analytics .map-viewport,
  .app-body.tab-analytics .sidebar-right {
    display: none !important;
  }
  .app-body.tab-analytics .sidebar-left {
    display: flex !important;
    position: static !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    height: 100% !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    border-radius: 0 !important;
    background: var(--tab-content-bg) !important;
    backdrop-filter: none !important;
    padding: 12px !important;
  }
  .app-body.tab-analytics .sidebar-left #panel-analytics {
    display: flex !important;
    flex-direction: column;
    gap: 12px;
  }
  .app-body.tab-analytics .sidebar-left #panel-analytics.hidden {
    display: none !important;
  }
  .app-body.tab-analytics .sidebar-left #panel-streets {
    display: none !important;
    flex-direction: column;
    height: 100% !important;
  }
  .app-body.tab-analytics .sidebar-left #panel-streets:not(.hidden) {
    display: flex !important;
  }

  /* --- Mobile analytics sub-tab bar --- */
  .mobile-analytics-subtabs {
    display: flex;
    gap: 4px;
    margin-bottom: 14px;
    background: rgba(255,255,255,0.03);
    border-radius: var(--border-radius-md);
    padding: 3px;
    flex-shrink: 0;
  }
  .mobile-analytics-subtab {
    flex: 1;
    padding: 8px 6px;
    text-align: center;
    font-size: 0.6rem;
    font-weight: 600;
    font-family: var(--font-outfit);
    color: var(--nav-text);
    background: none;
    border: none;
    border-radius: calc(var(--border-radius-md) - 2px);
    cursor: pointer;
    transition: all 0.2s ease;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    letter-spacing: 0.03em;
  }
  .mobile-analytics-subtab.active {
    color: var(--text-primary);
    background: rgba(255,255,255,0.08);
    box-shadow: 0 1px 4px rgba(0,0,0,0.2);
  }
  .mobile-analytics-subtab:active {
    transform: scale(0.97);
  }

  /* Hide sub-tab bar when not in analytics tab */
  .app-body:not(.tab-analytics) .mobile-analytics-subtabs {
    display: none !important;
  }

  .app-body:not(.tab-analytics) .mobile-spatial-select {
    display: none !important;
  }

  /* --- Error toast inside analytics panel --- */
  .mobile-error-toast {
    background: rgba(255, 60, 60, 0.12);
    border: 1px solid rgba(255, 60, 60, 0.25);
    color: #ff6b6b;
    padding: 10px 14px;
    border-radius: var(--border-radius-md);
    font-size: 0.68rem;
    font-weight: 500;
    margin-bottom: 12px;
    animation: mobile-error-in 0.3s ease;
    flex-shrink: 0;
  }
  @keyframes mobile-error-in {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
  }

  /* --- Spatial autocomplete search --- */
  .mobile-spatial-search-box {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: var(--border-radius-md);
    padding: 8px 10px;
    position: relative;
  }
  .mobile-spatial-search-input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-primary);
    font-size: 0.75rem;
    font-family: var(--font-outfit);
  }
  .mobile-spatial-search-input::placeholder {
    color: var(--text-muted);
  }
  .mobile-spatial-search-dropdown {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: var(--bg-obsidian-card);
    border: 1px solid var(--nav-border);
    border-radius: var(--border-radius-md);
    max-height: 200px;
    overflow-y: auto;
    z-index: 100;
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  }
  .mobile-spatial-search-dropdown.visible {
    display: block;
  }
  .spatial-search-item {
    padding: 10px 12px;
    cursor: pointer;
    border-bottom: 1px solid rgba(255,255,255,0.04);
    transition: background 0.15s ease;
  }
  .spatial-search-item:last-child {
    border-bottom: none;
  }
  .spatial-search-item:hover {
    background: rgba(255,255,255,0.06);
  }
  .spatial-search-item.muted {
    color: var(--text-muted);
    font-size: 0.7rem;
    cursor: default;
  }
  .spatial-search-item .item-title {
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-primary);
  }
  .spatial-search-item .item-sub {
    display: block;
    font-size: 0.58rem;
    color: var(--text-muted);
    margin-top: 2px;
    letter-spacing: 0.03em;
  }

  /* --- Booth autocomplete search (same pattern as spatial) --- */
  .booth-autocomplete-box {
    display: flex;
    align-items: center;
    gap: 8px;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: var(--border-radius-md);
    padding: 8px 10px;
    position: relative;
  }
  .booth-autocomplete-input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-primary);
    font-size: 0.75rem;
    font-family: var(--font-outfit);
  }
  .booth-autocomplete-input::placeholder {
    color: var(--text-muted);
  }
  .booth-autocomplete-dropdown {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: var(--bg-obsidian-card);
    border: 1px solid var(--nav-border);
    border-radius: var(--border-radius-md);
    max-height: 200px;
    overflow-y: auto;
    z-index: 100;
    box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  }
  .booth-autocomplete-dropdown.visible {
    display: block;
  }
  .booth-search-item {
    padding: 10px 12px;
    cursor: pointer;
    border-bottom: 1px solid rgba(255,255,255,0.04);
    transition: background 0.15s ease;
  }
  .booth-search-item:last-child {
    border-bottom: none;
  }
  .booth-search-item:hover {
    background: rgba(255,255,255,0.06);
  }
  .booth-search-item.muted {
    color: var(--text-muted);
    font-size: 0.7rem;
    cursor: default;
  }
  .booth-search-item .item-title {
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-primary);
  }
  .booth-search-item .item-sub {
    display: block;
    font-size: 0.58rem;
    color: var(--text-muted);
    margin-top: 2px;
    letter-spacing: 0.03em;
  }
  .app-body:not(.tab-analytics) .booth-autocomplete-wrapper {
    display: none !important;
  }

  /* --- Map adjustments on mobile --- */
  .map-viewport .map-toolbar {
    position: absolute !important;
    left: 6px !important;
    right: 6px !important;
    top: 6px !important;
    width: auto !important;
    flex-wrap: wrap;
    gap: 6px;
    padding: 6px 8px;
    min-height: auto;
    border-radius: var(--border-radius-md);
  }

  .map-toolbar .btn-toggle-sidebar {
    display: none !important;
  }

  .map-toolbar .floating-nav-pill {
    display: none !important;
  }

  .map-search-container {
    width: 100% !important;
  }

  .telemetry-text-strip {
    position: absolute !important;
    top: auto !important;
    bottom: 6px !important;
    right: 6px !important;
    left: 6px !important;
    width: auto !important;
    gap: 6px !important;
    padding: 6px 10px !important;
    flex-wrap: wrap;
    justify-content: center;
    border-radius: var(--border-radius-md);
  }

  .map-legend-panel {
    bottom: 6px !important;
    right: 6px !important;
    max-height: 35vh;
    font-size: 0.7rem;
  }

  .map-mode-indicator {
    display: none !important;
  }

  .btn-restore-map {
    top: 60px !important;
    right: 6px !important;
    padding: 4px 8px !important;
    font-size: 0.6rem !important;
  }

  .btn-restore-map span {
    display: none;
  }

  .map-watermark-logo {
    display: none !important;
  }

  .tooltip-gauge-panel {
    right: 0 !important;
    left: auto !important;
  }

  .leaflet-control-zoom {
    display: none !important;
  }

  .loading-overlay p {
    font-size: 0.6rem !important;
  }

  .loading-overlay .loading-status {
    font-size: 0.55rem !important;
  }

  /* --- Nav dock visible on mobile --- */
  .mobile-nav-dock {
    display: flex !important;
    flex-shrink: 0;
  }

  /* --- Bottom sheet on mobile (above nav dock) --- */
  .bottom-sheet {
    bottom: var(--nav-height);
    max-height: calc(85vh - var(--nav-height));
  }

  .sheet-overlay {
    bottom: var(--nav-height);
  }

  /* --- Section title sizing --- */
  .sidebar-left .section-title {
    font-size: 0.65rem !important;
  }

  .sidebar-left .section-title i {
    width: 14px !important;
    height: 14px !important;
  }

  /* --- Scrollbar styling for mobile panels --- */
  .sidebar-left::-webkit-scrollbar,
  .sidebar-right::-webkit-scrollbar {
    width: 3px;
  }

  .sidebar-left::-webkit-scrollbar-thumb,
  .sidebar-right::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.08);
    border-radius: 2px;
  }

  /* --- Fix drawer content scrolling on mobile --- */
  .sidebar-right .drawer-body {
    max-height: none !important;
    overflow: visible !important;
  }

  /* --- Ensure accordion cards look right --- */
  .sidebar-left .accordion-card summary.section-title {
    font-size: 0.65rem !important;
    padding: 8px !important;
  }

  /* --- Pull indicator visible on mobile --- */
  .pull-indicator {
    display: flex;
  }
}

/* ============================================================
   Tablet adjustments (768px - 1023px)
   ============================================================ */
@media (min-width: 768px) and (max-width: 1023px) {
  .mobile-nav-dock {
    display: none !important;
  }
}

/* ============================================================
   Ensure bottom sheet doesn't conflict with Leaflet
   ============================================================ */
.leaflet-zoom-anim .leaflet-zoom-animated {
  will-change: transform;
}
