/* ============================================
   COMPREHENSIVE LAYOUT FIXES
   Applied October 6, 2025
   Fixes ALL overlapping boxes, images, and text
   ============================================ */

/* ============================================
   1. CONTAINER & GRID FIXES
   ============================================ */

/* Fix all containers to prevent overflow */
.container {
  max-width: var(--container-max-width, 1280px);
  margin: 0 auto;
  padding: 0 var(--space-lg);
  width: 100%;
  box-sizing: border-box;
  position: relative;
}

/* Ensure all main content areas have proper spacing */
main,
.dashboard-main,
.community-main,
.racing-main,
.parts-main,
.marketplace-main,
.profile-main {
  margin-top: 0;
  padding-top: var(--space-3xl);
  padding-bottom: var(--space-3xl);
  min-height: calc(100vh - var(--header-height, 64px));
  position: relative;
  overflow-x: hidden;
}

/* Fix grid layouts to prevent overlap */
.grid,
[class*="-grid"],
.feature-cards,
.benefits-grid,
.listings-grid,
.vehicles-grid,
.categories-grid,
.results-grid,
.parts-grid {
  display: grid;
  gap: var(--space-xl);
  width: 100%;
  box-sizing: border-box;
}

/* Prevent grid items from overflowing */
.grid > *,
[class*="-grid"] > * {
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  box-sizing: border-box;
}

/* ============================================
   2. CARD & BOX OVERLAP FIXES
   ============================================ */

/* Fix all card types */
.card,
.feature-card,
.benefit-item,
.category-card,
.listing-card,
.vehicle-card,
.thread-card,
.game-card,
.part-card,
.service-card,
.reminder-card {
  position: relative;
  overflow: hidden;
  box-sizing: border-box;
  word-wrap: break-word;
  overflow-wrap: break-word;
  hyphens: auto;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 100%;
}

/* Prevent card content from overflowing */
.card-header,
.card-body,
.card-content,
.card-footer,
.listing-content,
.vehicle-info,
.thread-content {
  position: relative;
  overflow: hidden;
  word-wrap: break-word;
  overflow-wrap: break-word;
  box-sizing: border-box;
  width: 100%;
}

/* Fix card titles and text */
.card h1,
.card h2,
.card h3,
.card h4,
.card h5,
.card h6,
.card p,
[class*="-card"] h1,
[class*="-card"] h2,
[class*="-card"] h3,
[class*="-card"] h4,
[class*="-card"] h5,
[class*="-card"] h6,
[class*="-card"] p {
  margin: 0 0 var(--space-md) 0;
  word-wrap: break-word;
  overflow-wrap: break-word;
  hyphens: auto;
  max-width: 100%;
}

.card p:last-child,
[class*="-card"] p:last-child {
  margin-bottom: 0;
}

/* ============================================
   3. IMAGE OVERFLOW FIXES
   ============================================ */

/* Fix all images */
img {
  max-width: 100%;
  height: auto;
  display: block;
  box-sizing: border-box;
}

/* Fix image containers */
.image-container,
.listing-image,
.vehicle-image,
.category-image,
.user-avatar,
.game-thumbnail,
.part-image,
[class*="-image"] {
  position: relative;
  overflow: hidden;
  width: 100%;
  box-sizing: border-box;
  display: block;
}

.image-container img,
.listing-image img,
.vehicle-image img,
.category-image img,
.game-thumbnail img,
.part-image img,
[class*="-image"] img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* Fix aspect ratios */
.aspect-16-9 {
  position: relative;
  width: 100%;
  padding-bottom: 56.25%;
  overflow: hidden;
}

.aspect-16-9 img,
.aspect-16-9 > * {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.aspect-1-1 {
  position: relative;
  width: 100%;
  padding-bottom: 100%;
  overflow: hidden;
}

.aspect-1-1 img,
.aspect-1-1 > * {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* ============================================
   4. TEXT OVERFLOW FIXES
   ============================================ */

/* Fix all text elements */
h1,
h2,
h3,
h4,
h5,
h6,
p,
span,
a,
label,
li {
  word-wrap: break-word;
  overflow-wrap: break-word;
  hyphens: auto;
  max-width: 100%;
  box-sizing: border-box;
}

/* Fix long text that should truncate */
.truncate,
.text-truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

/* Fix multi-line truncation */
.line-clamp-2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.line-clamp-3 {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ============================================
   5. FLEX LAYOUT FIXES
   ============================================ */

/* Fix flexbox containers */
.flex,
[class*="flex-"],
.nav-content,
.tab-navigation,
.filter-bar,
.actions-bar,
.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-md);
  box-sizing: border-box;
  width: 100%;
}

/* Prevent flex children from overflowing */
.flex > *,
[class*="flex-"] > * {
  min-width: 0;
  min-height: 0;
  box-sizing: border-box;
}

/* Fix specific flex layouts */
.flex-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-md);
}

.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-md);
}

/* ============================================
   6. NAVIGATION FIXES
   ============================================ */

/* Fix navigation to not overlap content */
.main-nav {
  position: sticky;
  top: 0;
  left: 0;
  right: 0;
  z-index: var(--z-sticky);
  background: var(--color-bg);
  border-bottom: 1px solid var(--color-border);
  width: 100%;
  box-sizing: border-box;
}

.nav-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-lg);
  min-height: var(--header-height, 64px);
  width: 100%;
}

.nav-links {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  flex-wrap: wrap;
}

/* Fix mobile menu */
@media (max-width: 768px) {
  .nav-links {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    flex-direction: column;
    background: var(--color-bg);
    border-bottom: 1px solid var(--color-border);
    padding: var(--space-lg);
    display: none;
    box-shadow: var(--shadow-lg);
    z-index: var(--z-dropdown);
  }

  .nav-links.active {
    display: flex;
  }
}

/* ============================================
   7. TAB SYSTEM FIXES
   ============================================ */

/* Fix tab navigation */
.tab-navigation {
  display: flex;
  gap: var(--space-sm);
  border-bottom: 2px solid var(--color-border);
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  width: 100%;
  margin-bottom: var(--space-xl);
}

.tab-navigation::-webkit-scrollbar {
  display: none;
}

.tab-btn {
  flex-shrink: 0;
  white-space: nowrap;
  padding: var(--space-md) var(--space-lg);
}

/* Fix tab content */
.tab-content {
  display: none;
  width: 100%;
  box-sizing: border-box;
}

.tab-content.active {
  display: block;
}

/* ============================================
   8. MODAL FIXES
   ============================================ */

/* Ensure modals don't cause body overflow */
body.modal-open {
  overflow: hidden !important;
}

.modal {
  position: fixed !important;
  top: 0 !important;
  left: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  width: 100% !important;
  height: 100% !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  z-index: 9999 !important;
  overflow-y: auto !important;
  padding: 1rem;
}

.modal-content {
  position: relative !important;
  max-width: 600px !important;
  width: calc(100% - var(--space-xl) * 2) !important;
  max-height: 90vh !important;
  margin: auto !important;
  overflow: hidden !important;
  box-sizing: border-box !important;
  pointer-events: auto !important;
  z-index: 1 !important;
}

.modal-body {
  max-height: calc(90vh - 200px) !important;
  overflow-y: auto !important;
  overflow-x: hidden !important;
  box-sizing: border-box !important;
}

/* ============================================
   9. FORM FIXES
   ============================================ */

/* Fix form layouts */
form {
  width: 100%;
  box-sizing: border-box;
}

.form-group {
  margin-bottom: var(--space-lg);
  width: 100%;
  box-sizing: border-box;
}

.form-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: var(--space-md);
  width: 100%;
  box-sizing: border-box;
}

/* Fix form inputs */
.form-input,
.form-textarea,
.form-select,
input,
textarea,
select {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  font-family: inherit;
  font-size: var(--font-size-base);
}

/* ============================================
   10. TABLE FIXES
   ============================================ */

/* Fix table overflow */
.table-container {
  width: 100%;
  overflow-x: auto;
  box-sizing: border-box;
}

table {
  width: 100%;
  border-collapse: collapse;
  box-sizing: border-box;
}

th,
td {
  padding: var(--space-md);
  text-align: left;
  word-wrap: break-word;
  overflow-wrap: break-word;
  box-sizing: border-box;
}

/* ============================================
   11. RESPONSIVE FIXES
   ============================================ */

/* Mobile fixes */
@media (max-width: 768px) {
  .container {
    padding: 0 var(--space-md);
  }

  main,
  [class*="-main"] {
    padding-top: var(--space-2xl);
    padding-bottom: var(--space-2xl);
  }

  /* Stack grid items on mobile */
  .feature-cards,
  .benefits-grid,
  .listings-grid,
  .vehicles-grid,
  .categories-grid,
  .results-grid,
  .parts-grid {
    grid-template-columns: 1fr !important;
    gap: var(--space-lg);
  }

  /* Full width cards on mobile */
  .card,
  [class*="-card"] {
    width: 100%;
    max-width: 100%;
  }

  /* Stack form rows on mobile */
  .form-row {
    grid-template-columns: 1fr !important;
  }

  /* Fix modal on mobile */
  .modal-content {
    width: calc(100% - var(--space-md) * 2);
    margin: var(--space-md) auto;
  }

  /* Reduce spacing on mobile */
  .hero,
  .benefits,
  .ai-demo,
  .marketplace-preview,
  section {
    padding: var(--space-2xl) 0;
  }
}

/* Tablet fixes */
@media (max-width: 1024px) {
  .container {
    max-width: 100%;
  }

  /* 2-column grids on tablet */
  .feature-cards,
  .benefits-grid,
  .listings-grid,
  .vehicles-grid,
  .categories-grid,
  .results-grid,
  .parts-grid {
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)) !important;
  }
}

/* ============================================
   12. UTILITY FIXES
   ============================================ */

/* Box sizing reset */
*,
*::before,
*::after {
  box-sizing: border-box;
}

/* Prevent horizontal scrolling */
html,
body {
  overflow-x: hidden;
  max-width: 100%;
  box-sizing: border-box;
}

/* Clear floats */
.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

/* Prevent text selection on buttons */
button,
.btn {
  -webkit-user-select: none;
  user-select: none;
}

/* Fix for long URLs and code */
pre,
code {
  overflow-x: auto;
  word-wrap: break-word;
  white-space: pre-wrap;
  max-width: 100%;
  box-sizing: border-box;
}

/* ============================================
   13. Z-INDEX HIERARCHY
   ============================================ */

/* Ensure proper stacking order */
.nav-content {
  z-index: var(--z-sticky);
}
.dropdown {
  z-index: var(--z-dropdown);
}
.modal-overlay {
  z-index: var(--z-modal-backdrop);
}
.modal {
  z-index: var(--z-modal);
}
.tooltip {
  z-index: var(--z-tooltip);
}

/* ============================================
   14. ANIMATION SAFETY
   ============================================ */

/* Prevent layout shift during animations */
@keyframes slideIn,
@keyframes slideOut,
@keyframes fadeIn,
@keyframes fadeOut {
  from,
  to {
    max-width: 100%;
    overflow: hidden;
  }
}

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

/* ============================================
   15. PRINT FIXES
   ============================================ */

@media print {
  /* Hide unnecessary elements */
  nav,
  .nav-content,
  .mobile-menu-toggle,
  .modal-overlay,
  button {
    display: none !important;
  }

  /* Prevent page breaks inside elements */
  .card,
  [class*="-card"],
  section {
    page-break-inside: avoid;
  }

  /* Remove overflow for printing */
  * {
    overflow: visible !important;
  }
}
