/* Profile > Chat History responsive layout corrections.
   Layout-only pass for Issue #181. Keep history behavior/data unchanged. */

#section-my-comparisons {
  min-width: 0;
}

#section-my-comparisons > div {
  min-width: 0;
}

/* Let long history labels and metadata use the available width safely. */
#section-my-comparisons li,
#section-my-comparisons li > div,
#section-my-comparisons li .group,
#section-my-comparisons li .group > div {
  min-width: 0;
}

@media (max-width: 639px) {
  /* Keep the history card comfortably inside narrow phones. */
  #section-my-comparisons > div {
    border-radius: 1rem;
  }

  /* History rows should prioritize the title and summary rather than squeeze them. */
  #section-my-comparisons li .group {
    padding-left: 0.875rem;
    padding-right: 0.875rem;
  }

  #section-my-comparisons li p.text-sm.font-semibold {
    white-space: normal !important;
    overflow: visible !important;
    text-overflow: clip !important;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    overflow: hidden !important;
  }

  /* Preserve tap targets while reducing the horizontal pressure from row actions. */
  #section-my-comparisons li button[title="Expand"],
  #section-my-comparisons li button[title="Open results"],
  #section-my-comparisons li button[title="Copy link"],
  #section-my-comparisons li button[title="Rerun"],
  #section-my-comparisons li button[title="Share"],
  #section-my-comparisons li button[title="Delete"] {
    min-width: 2.25rem;
    min-height: 2.25rem;
    padding: 0.5rem;
  }

  /* Never make delete depend on desktop hover on touch devices. */
  #section-my-comparisons li button[title="Delete"] {
    opacity: 1 !important;
  }

  /* Filters remain horizontally scrollable without changing the page width. */
  #section-my-comparisons > div > div.overflow-x-auto {
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
  }
}

/* Saved-history detail viewer.
   The component uses z-[250]; scope these rules to that overlay so other dialogs stay unchanged. */
@media (max-width: 639px) {
  .fixed.inset-0.z-\[250\] {
    z-index: 1000 !important;
    align-items: flex-start !important;
    overflow: hidden !important;
    padding: 0 !important;
  }

  .fixed.inset-0.z-\[250\] > div.relative.z-10 {
    width: 100% !important;
    height: calc(100dvh - var(--mobile-nav-height, 6.5rem) - env(safe-area-inset-bottom, 0px)) !important;
    max-height: calc(100dvh - var(--mobile-nav-height, 6.5rem) - env(safe-area-inset-bottom, 0px)) !important;
    min-height: 0 !important;
    border-radius: 0 !important;
    overflow: hidden !important;
  }

  /* The results area owns scrolling; header and actions stay reachable. */
  .fixed.inset-0.z-\[250\] > div.relative.z-10 > div.overflow-y-auto {
    min-height: 0 !important;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 1rem !important;
  }

  /* Footer: two usable columns, no horizontal clipping, no overlap with app nav. */
  .fixed.inset-0.z-\[250\] > div.relative.z-10 > div.border-t:last-child {
    position: relative !important;
    z-index: 20 !important;
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 0.5rem !important;
    padding: 0.75rem 0.875rem max(0.75rem, env(safe-area-inset-bottom, 0px)) !important;
    background: hsl(var(--card) / 0.98) !important;
  }

  .fixed.inset-0.z-\[250\] > div.relative.z-10 > div.border-t:last-child button,
  .fixed.inset-0.z-\[250\] > div.relative.z-10 > div.border-t:last-child [role="button"] {
    width: 100% !important;
    min-width: 0 !important;
    min-height: 2.75rem !important;
    height: auto !important;
    padding-left: 0.625rem !important;
    padding-right: 0.625rem !important;
    white-space: normal !important;
    line-height: 1.15 !important;
    text-align: center !important;
  }
}

/* Tablet: keep the detail viewer centered and bounded rather than phone-fullscreen. */
@media (min-width: 640px) and (max-width: 1023px) {
  .fixed.inset-0.z-\[250\] {
    padding: 1rem !important;
  }

  .fixed.inset-0.z-\[250\] > div.relative.z-10 {
    width: min(100%, 48rem) !important;
    max-width: 48rem !important;
    max-height: min(90dvh, 56rem) !important;
    min-height: 0 !important;
  }

  .fixed.inset-0.z-\[250\] > div.relative.z-10 > div.border-t:last-child {
    flex-wrap: wrap !important;
  }
}

/* Phone landscape can report a tablet-like CSS width, so height and orientation
   are the reliable signals. In landscape the app navigation becomes a left rail;
   the history viewer must cover it instead of reserving bottom-nav height. */
@media (orientation: landscape) and (max-height: 700px) and (max-width: 1366px) {
  .fixed.inset-0.z-\[250\] {
    z-index: 2147483000 !important;
    inset: 0 !important;
    width: 100vw !important;
    height: 100dvh !important;
    align-items: stretch !important;
    justify-content: stretch !important;
    overflow: hidden !important;
    padding: 0 !important;
  }

  .fixed.inset-0.z-\[250\] > div.absolute.inset-0 {
    position: fixed !important;
    inset: 0 !important;
  }

  .fixed.inset-0.z-\[250\] > div.relative.z-10 {
    width: 100vw !important;
    max-width: none !important;
    height: 100dvh !important;
    max-height: 100dvh !important;
    min-height: 0 !important;
    border-radius: 0 !important;
    overflow: hidden !important;
  }

  .fixed.inset-0.z-\[250\] > div.relative.z-10 > div.overflow-y-auto {
    min-height: 0 !important;
    padding-top: 0.625rem !important;
    padding-bottom: 0.625rem !important;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .fixed.inset-0.z-\[250\] > div.relative.z-10 > div.border-t:last-child {
    position: relative !important;
    z-index: 20 !important;
    display: grid !important;
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    align-items: stretch !important;
    gap: 0.5rem !important;
    padding: 0.5rem max(0.75rem, env(safe-area-inset-right, 0px)) 0.5rem max(0.75rem, env(safe-area-inset-left, 0px)) !important;
    background: hsl(var(--card) / 0.98) !important;
  }

  .fixed.inset-0.z-\[250\] > div.relative.z-10 > div.border-t:last-child > *,
  .fixed.inset-0.z-\[250\] > div.relative.z-10 > div.border-t:last-child button,
  .fixed.inset-0.z-\[250\] > div.relative.z-10 > div.border-t:last-child [role="button"] {
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
  }

  .fixed.inset-0.z-\[250\] > div.relative.z-10 > div.border-t:last-child button,
  .fixed.inset-0.z-\[250\] > div.relative.z-10 > div.border-t:last-child [role="button"] {
    min-height: 2.5rem !important;
    height: auto !important;
    padding: 0.5rem !important;
    white-space: normal !important;
    line-height: 1.1 !important;
    text-align: center !important;
  }
}
