/* ==========================================================================
   PepFellas Laboratories — Responsive Modal, Scroll & Mobile Viewport Fixes
   ========================================================================== */

/* 1. Prevent page-level horizontal overextension without breaking sticky headers */
html, body {
  overflow-x: clip;
  max-width: 100%;
}

/* 2. Mobile Navbar & Auth Dropdown Protection */
#auth-container {
  min-width: 0 !important;
  max-width: 100%;
}
#user-menu-btn {
  max-width: 165px;
}
#user-menu-btn svg {
  flex-shrink: 0;
}
#user-menu-btn > span {
  display: inline-block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 90px;
}
#user-dropdown-panel {
  max-width: calc(100vw - 1.5rem);
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
@media (min-width: 640px) {
  #user-menu-btn { max-width: 240px; }
  #user-menu-btn > span { max-width: 160px; }
}

/* 3. Full-Height Modals: Dynamic Viewport Height (dvh) + Flex Chain Fix */
#admin-inventory-modal > div,
#admin-users-modal > div,
#admin-orders-modal > div,
#admin-coa-modal > div,
#admin-news-modal > div,
#admin-reviews-modal > div,
#admin-accounting-modal > div,
#user-orders-modal > div {
  height: 92vh;
  height: 92dvh;
  max-height: 94dvh;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Never crush modal headers, metric strips, or sub-navigation bars */
#admin-inventory-modal > div > div:first-child,
#admin-users-modal > div > div:first-child,
#admin-orders-modal > div > div:first-child,
#admin-orders-modal > div > div:nth-child(2),
#admin-coa-modal > div > div:first-child,
#admin-news-modal > div > div:first-child,
#admin-reviews-modal > div > div:first-child,
#admin-accounting-modal > div > div:first-child,
#admin-accounting-modal > div > div:nth-child(2),
#user-orders-modal > div > div:first-child {
  flex-shrink: 0;
}

#admin-inventory-modal > div > div:first-child {
  flex-wrap: wrap;
  gap: 0.75rem;
}

/* Core flex-body wrappers must have min-height: 0 & min-width: 0 for inner scrolling */
#admin-inventory-modal > div > div:nth-child(2),
#admin-users-modal > div > div:nth-child(2),
#admin-orders-modal > div > div:nth-child(3),
#admin-reviews-modal > div > div:nth-child(2) {
  flex: 1 1 0%;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

#admin-inventory-modal > div > div:nth-child(2) > div:first-child,
#admin-orders-modal > div > div:nth-child(3) > div:first-child {
  flex: 1 1 0%;
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 4. Personnel & Security Vault Tabs (Directory, IP Blacklist, Audit Logs) */
#view-users-directory:not(.hidden),
#view-security-logs:not(.hidden) {
  display: flex !important;
  flex-direction: column;
  flex: 1 1 0%;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

#view-users-directory > div:first-child,
#view-security-logs > div:first-child,
#admin-inventory-modal > div > div:nth-child(2) > div:first-child > div:first-child,
#admin-orders-modal > div > div:nth-child(3) > div:first-child > div:first-child,
#admin-reviews-modal > div > div:nth-child(2) > div:first-child {
  flex-shrink: 0;
}

#view-security-logs > div:first-child {
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* IP Blacklist: Vertical scroll on mobile/tablet, side-by-side independent scroll on desktop */
#view-ip-blacklist:not(.hidden) {
  display: flex !important;
  flex-direction: column;
  flex: 1 1 0%;
  min-height: 0;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
#view-ip-blacklist > .grid {
  min-width: 0;
  min-height: 0;
  overflow: visible;
  flex: 0 0 auto;
}
#view-ip-blacklist > .grid > div {
  min-width: 0;
}
#view-ip-blacklist > .grid > div:nth-child(2) {
  min-height: 300px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#view-ip-blacklist > .grid > div:nth-child(2) > div:first-child {
  flex-shrink: 0;
}
#view-ip-blacklist > .grid > div:nth-child(2) > div:nth-child(2) {
  flex: 1 1 0%;
  min-height: 0;
  min-width: 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

@media (min-width: 1024px) {
  #view-ip-blacklist:not(.hidden) {
    overflow: hidden;
  }
  #view-ip-blacklist > .grid {
    flex: 1 1 0%;
    height: 100%;
    overflow: hidden;
  }
  #view-ip-blacklist > .grid > div:first-child {
    min-height: 0;
    overflow-y: auto;
  }
  #view-ip-blacklist > .grid > div:nth-child(2) {
    min-height: 0;
  }
}

/* 5. Two-Axis Scrolling on ALL Admin & Active Orders Tables */
#view-users-directory > div:nth-child(2),
#view-security-logs > div:nth-child(2),
#admin-inventory-modal > div > div:nth-child(2) > div:first-child > div:nth-child(2),
#admin-orders-modal > div > div:nth-child(3) > div:first-child > div:nth-child(2),
#admin-reviews-modal > div > div:nth-child(2) > div:nth-child(2) {
  flex: 1 1 0%;
  min-height: 0;
  min-width: 0;
  overflow: auto !important;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* Active Orders Modal (user_modals.php) */
#user-orders-modal > div > div:nth-child(2) {
  flex: 1 1 0%;
  min-height: 0;
  min-width: 0;
  overflow: auto !important;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
#user-orders-modal > div > div:nth-child(2) > div {
  overflow-x: auto !important;
  overflow-y: visible;
  -webkit-overflow-scrolling: touch;
}

/* Minimum table widths prevent column crushing & guarantee full horizontal scroll */
#admin-inventory-modal table     { min-width: 840px; }
#view-users-directory table      { min-width: 800px; }
#view-ip-blacklist table         { min-width: 640px; }
#view-security-logs table        { min-width: 780px; }
#admin-orders-modal table        { min-width: 960px; }
#admin-coa-modal table           { min-width: 600px; }
#admin-news-modal table          { min-width: 460px; }
#admin-reviews-modal table       { min-width: 760px; }
#user-orders-modal table         { min-width: 700px; }
#admin-order-details-modal table { min-width: 480px; }
#admin-accounting-modal table    { min-width: 760px; }

/* Make long Audit Log Client strings & IP Ban Reasons readable without hover tooltips */
#view-security-logs td:last-child,
#view-ip-blacklist td:nth-child(2) {
  white-space: normal !important;
  word-break: break-word;
  overflow: visible !important;
  text-overflow: clip !important;
  min-width: 180px;
  max-width: 320px;
}
#view-security-logs td:nth-child(1),
#view-security-logs td:nth-child(2),
#view-security-logs td:nth-child(3),
#view-ip-blacklist td:nth-child(1) {
  white-space: nowrap;
}

/* 6. COA & News Split-Pane Modals: Stack & Scroll on Mobile/Tablet (<1024px) */
#admin-coa-modal > div > div:nth-child(2),
#admin-news-modal > div > div:nth-child(2) {
  flex: 1 1 0%;
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}
#admin-coa-modal > div > div:nth-child(2) > div:first-child,
#admin-news-modal > div > div:nth-child(2) > div:first-child {
  width: 100% !important;
  flex-shrink: 0;
  overflow: visible;
  border-right: none;
  border-bottom: 1px solid #1f2430;
}
#admin-coa-modal > div > div:nth-child(2) > div:nth-child(2),
#admin-news-modal > div > div:nth-child(2) > div:nth-child(2) {
  flex: 0 0 auto;
  min-width: 0;
  overflow: visible;
}
#admin-coa-modal > div > div:nth-child(2) > div:nth-child(2) > div,
#admin-news-modal > div > div:nth-child(2) > div:nth-child(2) > div {
  max-height: 55vh;
  overflow: auto !important;
  -webkit-overflow-scrolling: touch;
}

@media (min-width: 1024px) {
  #admin-coa-modal > div > div:nth-child(2),
  #admin-news-modal > div > div:nth-child(2) {
    flex-direction: row;
    overflow: hidden;
  }
  #admin-coa-modal > div > div:nth-child(2) > div:first-child,
  #admin-news-modal > div > div:nth-child(2) > div:first-child {
    width: 400px !important;
    min-height: 0;
    overflow-y: auto;
    border-right: 1px solid #1f2430;
    border-bottom: none;
  }
  #admin-coa-modal > div > div:nth-child(2) > div:nth-child(2),
  #admin-news-modal > div > div:nth-child(2) > div:nth-child(2) {
    flex: 1 1 0%;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
  #admin-coa-modal > div > div:nth-child(2) > div:nth-child(2) > div,
  #admin-news-modal > div > div:nth-child(2) > div:nth-child(2) > div {
    max-height: none;
    flex: 1 1 0%;
    min-height: 0;
  }
}

/* 7. Slide-Over Drawers & Compact Modals (Auth, Checkout, Ban, Details, Chat) */
#product-form-drawer,
#user-form-drawer,
#admin-email-drawer,
#manual-order-drawer,
#fulfill-order-drawer,
#cart-drawer,
#wiki-drawer {
  max-width: 100vw;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

#auth-modal > div,
#order-ban-modal > div,
#checkout-modal > div {
  max-height: 92vh;
  max-height: 92dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

#order-contact-modal > div,
#admin-order-contact-modal > div {
  max-height: 90dvh;
  height: min(600px, 90dvh);
  overflow: hidden;
}
#customer-message-thread,
#admin-message-thread {
  height: auto !important;
  flex: 1 1 0%;
  min-height: 140px;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
}

#admin-order-details-modal > div > div:first-child {
  flex-wrap: wrap;
  gap: 0.5rem;
}
#admin-order-details-modal > div > div:nth-child(2) {
  min-height: 0;
  -webkit-overflow-scrolling: touch;
}
#admin-order-details-modal table {
  width: 100%;
}

/* 8. Wiki Tables Horizontal Scroll on Mobile */
.wiki-prose table {
  display: block;
  overflow-x: auto;
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
}

/* 9. Mobile Spacing Polish (<640px) */
@media (max-width: 639px) {
  #admin-inventory-modal > div > div:first-child,
  #admin-users-modal > div > div:first-child,
  #admin-orders-modal > div > div:first-child,
  #admin-orders-modal > div > div:nth-child(2),
  #admin-coa-modal > div > div:first-child,
  #admin-news-modal > div > div:first-child,
  #admin-reviews-modal > div > div:first-child,
  #admin-accounting-modal > div > div:first-child,
  #admin-accounting-modal > div > div:nth-child(2),
  #user-orders-modal > div > div:first-child {
    padding: 0.85rem 1rem !important;
  }

  #view-users-directory,
  #view-ip-blacklist,
  #view-security-logs,
  #user-orders-modal > div > div:nth-child(2),
  #admin-inventory-modal > div > div:nth-child(2) > div:first-child,
  #admin-orders-modal > div > div:nth-child(3) > div:first-child,
  #admin-reviews-modal > div > div:nth-child(2),
  #acct-scroll-body {
    padding: 0.85rem !important;
  }

  #admin-users-modal > div > div:first-child > div:nth-child(2) {
    width: 100%;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  #log-user-filter {
    width: 100% !important;
    max-width: 100%;
  }

  #admin-orders-modal > div > div:nth-child(2) {
    gap: 0.5rem !important;
  }
}