/* Bottom sheets / modals: always above the 5-tab bottom nav, scrollable body, actions pinned
   at the bottom with safe-area spacing. Layout-only (2026-10-24, /trade 「确认报价与订单」). */
:root { --sheet-safe-bottom: calc(max(env(safe-area-inset-bottom, 0px), 16px) + 24px); }

/* 1) While any sheet/modal is open: hide the bottom nav and lock background scroll. */
/* (doubled class: must out-rank `.page-root:has([data-page-masthead="omitted"]) .mobile-tabbar{display:flex!important}`) */
html:has(.jdex-modal:not(.hidden)) body .mobile-tabbar.mobile-tabbar,
html:has(.modal-backdrop) body .mobile-tabbar.mobile-tabbar,
html:has(dialog[open]) body .mobile-tabbar.mobile-tabbar,
html:has(.tr3-sheet:not([hidden])) body .mobile-tabbar.mobile-tabbar { display: none !important; }
html:has(.jdex-modal:not(.hidden)),
html:has(.jdex-modal:not(.hidden)) body,
html:has(.modal-backdrop),
html:has(.modal-backdrop) body,
html:has(dialog[open]),
html:has(dialog[open]) body,
html:has(.tr3-sheet:not([hidden])),
html:has(.tr3-sheet:not([hidden])) body { overflow: hidden !important; overscroll-behavior: none; }
/* jdex-modal lives inside the page (main.tr2-shell is its own z-index:1 stacking context, below the
   nav). Flatten ancestor stacking contexts while it is open so z-index:200 applies page-wide. */
body *:has(.jdex-modal:not(.hidden)) { z-index: auto !important; }

/* 2) jdex-modal: header / scrollable body / pinned footer. dvh with vh fallback. */
.jdex-modal-content {
  display: flex !important;
  flex-direction: column;
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow: hidden !important;
}
.jdex-modal-header, .jdex-modal-footer { flex: none; }
.jdex-modal-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.jdex-modal-footer {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 20px 20px;
  border-top: 1px solid rgba(148, 163, 184, .22);
  background: transparent;
}
.jdex-modal-footer .swap-primary-btn { margin: 0 !important; width: 100%; }
.jdex-modal-footer .swap-primary-btn.hidden { display: none !important; }

@media (max-width: 640px) {
  .jdex-modal { padding: 12px 12px var(--sheet-safe-bottom); }
  .jdex-modal-content:not(.swap-confirm-sheet) {
    max-height: calc(100vh - 12px - var(--sheet-safe-bottom));
    max-height: calc(100dvh - 12px - var(--sheet-safe-bottom));
  }
  .jdex-modal .swap-confirm-sheet {
    position: fixed !important;
    left: 0 !important; right: 0 !important; bottom: 0 !important; top: auto !important;
    width: 100% !important; max-width: none !important; margin: 0 !important;
    max-height: 85vh;
    max-height: 85dvh;
    border-radius: 18px 18px 0 0 !important;
  }
  .swap-confirm-sheet .jdex-modal-body { padding-bottom: 8px; }
  .swap-confirm-sheet .jdex-modal-footer { padding: 12px 16px var(--sheet-safe-bottom); }
}

/* Browsers without :has() (old Android WebViews): keep the sheet above the nav instead of under it. */
@supports not selector(:has(*)) {
  @media (max-width: 640px) {
    .jdex-modal .swap-confirm-sheet { bottom: var(--ia-bottom-space, 0px) !important; max-height: calc(85vh - var(--ia-bottom-space, 0px)); }
  }
}

/* 3) WD.modal (.modal-backdrop > .modal-content, e.g. swap 「确认兑换」 / txGuard confirm): vertically centred
   on every phone size (main.css used to pin it to the top at <=480px), symmetric safe-area padding,
   dvh max-height and internal scroll. The tab nav is hidden while a modal is open (rule 1). */
@media (max-width: 640px) {
  .modal-backdrop {
    align-items: center !important;
    justify-content: center !important;
    padding: calc(env(safe-area-inset-top, 0px) + 16px) 12px calc(env(safe-area-inset-bottom, 0px) + 16px) !important;
  }
  .modal-content {
    margin: auto 0 !important;
    max-width: 440px !important;
    max-height: calc(100vh - 32px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)) !important;
    max-height: calc(100dvh - 32px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)) !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }
}
.swap-sign-confirm .txg-row-v { overflow-wrap: anywhere; }
.swap-sign-confirm .swap-sign-step { margin: -4px 0 14px; font-size: 12px; line-height: 1.55; color: #64748b; }
html[data-theme="dark"] .swap-sign-confirm .swap-sign-step { color: #94a3b8; }

/* 4) Native <dialog> sheets (language, network pickers, pool / launch confirm, filters):
   cap to the dynamic viewport, scroll inside, keep the last action clear of the home indicator. */
@media (max-width: 640px) {
  dialog[open] {
    max-height: calc(100vh - 16px);
    max-height: calc(100dvh - 16px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }
}
/* Bottom-anchored dialog sheets: keep the last row clear of the iPhone home indicator
   (env() is 0 on devices without one, so Android layouts are unchanged). */
@media (max-width: 640px) {
  .mk2-sheet { max-height: 78vh; max-height: 78dvh; }
  .mk2-sheet .mk2-sheet-list { max-height: 66vh; max-height: 66dvh; padding-bottom: calc(18px + env(safe-area-inset-bottom, 0px)); }
  dialog.me2-dlg { padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px)); }
  dialog.network-picker-dialog { padding-bottom: env(safe-area-inset-bottom, 0px) !important; }
}

/* 5) Launch mainnet confirm + pool create confirm (<dialog> modals): explicitly centred with safe-area room. */
dialog.launch-mainnet-confirm[open],
dialog.guided-confirm[open] {
  margin: auto !important;
  inset: 0;
  max-height: calc(100vh - 32px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  max-height: calc(100dvh - 32px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  overflow-y: auto;
  overscroll-behavior: contain;
}
