/**
 * Module: iOS Safari + Apple HIG + WCAG 2.2 AA Interaction & Accessibility Layer
 * Principle: Visual Size != Touch Target Size.
 * Preserves 100% of existing visual design, density, colors, 4px radii, and 400 typography
 * while expanding invisible hit targets (>= 44x44 CSS px), preventing iOS focus auto-zoom
 * across portrait & landscape, enforcing safe-area insets, and restoring keyboard focus.
 */

/* ==========================================================================
   1. Skip-to-Main Landmark & Accessible Keyboard Focus (:focus-visible only)
   ========================================================================== */
.skip-to-main-link {
  position: fixed;
  top: -100px;
  left: 12px;
  z-index: 501;
  padding: 8px 14px;
  background: var(--sf-orange, #ff5000);
  color: #ffffff !important;
  font-size: 13px;
  font-weight: 400;
  border-radius: 4px;
  text-decoration: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.18);
  transition: top 0.15s ease;
}

.skip-to-main-link:focus-visible {
  top: max(10px, env(safe-area-inset-top, 10px));
  outline: 2px solid #ffffff;
  outline-offset: -3px;
}

.account-sidebar-toggle:focus-visible,
.wallet-dialog button:focus-visible,
.notification-bell-btn:focus-visible,
.notif-tab-item:focus-visible,
.orders-qv-tab:focus-visible,
.policy-nav-link:focus-visible,
.support-box-hotline:focus-visible,
.support-box-zalo:focus-visible {
  outline: 2px solid var(--sf-orange, #ff5000) !important;
  outline-offset: 2px;
}

/* Reusable semantic checkbox hit wrapper (non-replaced <label>) */
.ios-chk-hit {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  position: relative;
  margin: 0;
  padding: 0;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

/* Reusable invisible touch target utility (for static/relative interactive elements) */
.ios-touch-target {
  position: relative;
  overflow: visible;
}

/* ==========================================================================
   2. Safe-Area Insets, 100dvh & Short-Viewport (Landscape / Keyboard) Guards
   ========================================================================== */
.storefront-sticky-header {
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
}

.storefront-mobile-nav {
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
}

.mobile-lang-onboarding {
  bottom: calc(68px + env(safe-area-inset-bottom, 0px));
}

/* Image Search Modal: scrollable body on short landscape viewports */
.img-modal-dialog {
  max-height: min(90dvh, 680px);
}

.img-modal-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
}

/* Lightbox: dynamic viewport height & notch/home-indicator safe areas */
.product-lightbox-modal {
  height: 100dvh;
}

.lightbox-close-btn {
  top: max(14px, calc(10px + env(safe-area-inset-top, 0px)));
  right: max(14px, calc(12px + env(safe-area-inset-right, 0px)));
}

.lightbox-thumbs-strip {
  padding-bottom: max(12px, calc(10px + env(safe-area-inset-bottom, 0px)));
}

/* Account offcanvas sidebar & CTV drawers: safe-area top & bottom */
@media (max-width: 900px) {
  .account-sidebar .account-user-card {
    padding-top: max(16px, calc(12px + env(safe-area-inset-top, 0px)));
  }

  .account-sidebar .account-sidebar-footer {
    padding-bottom: max(14px, calc(12px + env(safe-area-inset-bottom, 0px)));
  }
}

.ctv-drawer-head {
  padding-top: max(14px, calc(10px + env(safe-area-inset-top, 0px)));
}

.ctv-drawer-foot {
  padding-bottom: max(12px, calc(10px + env(safe-area-inset-bottom, 0px)));
}

/* Landscape / Virtual Keyboard open: prevent fixed min-height from pushing chat composer below keyboard */
@media (max-height: 560px) {
  .buyer-chat-shell {
    min-height: 0 !important;
    height: max(220px, calc(100dvh - 124px - env(safe-area-inset-bottom, 0px))) !important;
  }

  .account-modal-box,
  .quick-login-card,
  .buyer-modal-dialog,
  .ctv-modal-box,
  .order-cancel-modal-panel {
    max-height: calc(100dvh - 16px) !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }
}

/* Typography & Readability: prevent Vietnamese diacritic descender clipping in header cluster */
.nav-cluster-item > span:not(.cluster-avatar-circle),
.sticky-actions-cluster .nav-cluster-item > span:not(.cluster-avatar-circle) {
  height: auto;
  min-height: 14px;
  line-height: 1.2;
}

/* ==========================================================================
   3. iOS Safari & Touch Device Anti-Auto-Zoom Input Policy (>= 16px)
      Covers BOTH iPhone Portrait (<= 768px) AND iPhone Landscape (812px-956px)
      via (hover: none) and (pointer: coarse) + WebKit touch-callout support,
      without altering Desktop mouse/trackpad typography.
   ========================================================================== */
@media (max-width: 768px), (hover: none) and (pointer: coarse) {
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="button"]):not([type="submit"]):not([type="reset"]):not([type="file"]):not([type="image"]):not([type="color"]):not([type="hidden"]):not([readonly]),
  select,
  textarea,
  [contenteditable="true"],
  .form-input,
  .form-select,
  .form-textarea,
  .search-input-field,
  .sticky-search-input,
  .sku-stepper .sku-qty-input,
  .sku-stepper-clean input,
  .mro-qty-stepper .stepper-input,
  .qty-stepper .qty-input,
  .cd-qty-input,
  .price-input,
  .input-price-val,
  .sup-search-input,
  .vh-search-input,
  #source-deal-query,
  .search-select-search-input,
  .buyer-modal-input,
  #modal-unlock-balance .unlock-balance-field input,
  .wallet-form-field input,
  .wallet-form-field select,
  #topup-amount-input,
  .topup-amount-input,
  .account-orders-search-input,
  .tx-input,
  .tx-select,
  .parcel-claim-form select,
  .parcel-claim-form input,
  .delivery-inputs-grid input,
  #quick-order-query,
  .kg-input,
  .kg-select,
  .kg-textarea,
  .ps-cny-input-group input,
  .ps-list-search input,
  .ps-buyer-filter-bar input,
  .ps-buyer-field input,
  .ps-buyer-field select,
  .ps-buyer-field textarea,
  #ps-msg-input,
  .ctv-filter-bar > input,
  .ctv-filter-bar > select,
  .buyer-chat-input,
  #newModalSubject,
  .note-popover-textarea,
  .qr-cancel-reason-input {
    font-size: max(16px, 1em) !important;
  }

  /* Calibrate internal line-height & vertical padding on compact boxes (24px-36px)
     so 16px text + Vietnamese diacritics never clip vertically and box height stays identical */
  .sku-stepper .sku-qty-input,
  .sku-stepper-clean input,
  .mro-qty-stepper .stepper-input,
  .qty-stepper .qty-input,
  .cd-qty-input {
    line-height: 1.1 !important;
    padding-top: 0 !important;
    padding-bottom: 0 !important;
  }

  .input-price-val {
    line-height: 22px;
    padding-top: 0;
    padding-bottom: 0;
  }

  .price-input {
    line-height: 1.2;
    padding-top: 2px;
    padding-bottom: 2px;
  }

  .search-select-search-input {
    line-height: 1.25;
    padding-top: 4px;
    padding-bottom: 4px;
  }

  .form-select,
  .tx-select,
  .parcel-claim-form select,
  .parcel-claim-form input,
  .ps-list-search input,
  .ctv-filter-bar > input,
  .ctv-filter-bar > select {
    line-height: 1.25;
    padding-top: 4px;
    padding-bottom: 4px;
  }

  .buyer-chat-input {
    line-height: 21px;
    padding-top: 1.5px;
    padding-bottom: 1.5px;
  }
}

@supports (-webkit-touch-callout: none) {
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="button"]):not([type="submit"]):not([type="reset"]):not([type="file"]):not([type="image"]):not([type="color"]):not([type="hidden"]):not([readonly]),
  select,
  textarea,
  [contenteditable="true"] {
    font-size: max(16px, 1em) !important;
  }
}

/* ==========================================================================
   4. Invisible Touch Target Expansion (>= 44x44 CSS px, Zero Visual Size Diff)
      Applied on mobile & coarse-pointer touch devices.
   ========================================================================== */
@media (max-width: 768px), (hover: none) and (pointer: coarse) {
  /* 4.1 Unclip ancestor containers where safe so pseudo-element hit regions are active */
  .dropzone-preview {
    overflow: visible;
  }

  .dropzone-preview > img {
    border-radius: 6px;
  }

  .buyer-chat-attachment-item {
    overflow: visible !important;
  }

  .buyer-chat-attachment-item img {
    border-radius: 7px;
  }

  .orders-qv-meta {
    overflow: visible !important;
  }

  .toast-item.toast-visible:not(.toast-hiding) {
    overflow: visible !important;
  }

  /* Search capsules: allow vertical hit expansion to 44px while preserving rounded corners */
  .search-box-1688,
  .sticky-search-box {
    overflow: visible !important;
  }

  /* 4.2 Static/Flex Interactive Controls: add position: relative ONLY to non-absolutely-positioned elements */
  .ios-touch-target,
  .buyer-modal-close-btn,
  .account-modal-close-btn,
  .cart-modal-close,
  .cart-mobile-sheet-close,
  .cart-mobile-back,
  .cd-back-btn,
  .cd-sheet-close,
  .cd-edit-toggle,
  .cd-delete-selected,
  .btn-close-modal,
  .btn-modal-close,
  .btn-close-note-popover,
  .img-modal-close,
  .error-close-btn,
  .mobile-search-back-btn,
  .mobile-search-history-clear,
  .account-sidebar-close,
  .ctv-modal-close-btn,
  .buyer-chat-mobile-back,
  .buyer-chat-new-modal-close,
  .buyer-chat-attach-btn,
  .buyer-chat-send-btn,
  .buyer-chat-presets-toggle,
  .ps-chat-attach-btn,
  .ps-chat-send-btn,
  #ps-msg-preview-clear,
  .btn-item-remove,
  .toast-close-btn,
  .buyer-balance-toggle-btn,
  .btn-compact-info,
  .wallet-shortfall-info,
  .btn-copy-order-code,
  .orders-qv-copy-btn,
  .orders-qv-cta,
  .btn-action-link,
  .btn-clean-link,
  .btn-summary-link,
  .btn-change-address,
  .btn-delete-selected,
  .btn-inline-note-pill,
  .btn-edit-saved-addr,
  #btn-open-products-modal,
  .account-order-show-more-btn,
  .sf-drp-nav,
  .search-camera-inside-btn,
  .search-selector-title,
  .sticky-selector-title,
  .search-btn-submit,
  .sticky-search-submit,
  .mobile-search-submit,
  .pdp-mobile-chat-btn,
  .pdp-mobile-btn,
  .checkout-mobile-chat-btn,
  .btn-mobile-submit,
  .cd-checkout-btn,
  .qv-chat-btn,
  .qv-btn-cart,
  .qv-btn-buy,
  .buyer-modal-btn,
  .btn-modal-primary,
  .btn-modal-cancel,
  .col-check-item,
  .cart-select-all,
  .cd-select-all {
    position: relative;
  }

  /* Centered 44x44 invisible hit area for isolated buttons (both relative above AND already-absolute buttons) */
  .ios-touch-target::after,
  .buyer-modal-close-btn::after,
  .quick-login-close::after,
  .account-modal-close-btn::after,
  .order-cancel-modal-close::after,
  .cart-modal-close::after,
  .cart-mobile-sheet-close::after,
  .cart-mobile-back::after,
  .cd-back-btn::after,
  .cd-sheet-close::after,
  .btn-close-modal::after,
  .btn-modal-close::after,
  .btn-close-note-popover::after,
  .img-modal-close::after,
  .error-close-btn::after,
  .mobile-search-back-btn::after,
  .mobile-search-history-clear::after,
  .account-sidebar-close::after,
  .ctv-modal-close-btn::after,
  .loy-celeb-close::after,
  .buyer-chat-mobile-back::after,
  .buyer-chat-lightbox-close::after,
  .buyer-chat-new-modal-close::after,
  .lightbox-close-btn::after,
  .buyer-chat-attach-btn::after,
  .buyer-chat-send-btn::after,
  .buyer-chat-presets-toggle::after,
  .buyer-chat-jump-bottom::after,
  .ps-chat-attach-btn::after,
  .ps-chat-send-btn::after,
  #ps-msg-preview-clear::after,
  .toast-close-btn::after,
  .eye-toggle-btn::after,
  .buyer-balance-toggle-btn::after,
  .btn-compact-info::after,
  .wallet-shortfall-info::after,
  .btn-copy-order-code::after,
  .orders-qv-copy-btn::after,
  .orders-qv-cta::after,
  .quick-order-card-remove-btn::after,
  .preview-remove-btn::after,
  .buyer-chat-attachment-remove::after,
  .vh-remove-btn::after,
  .sf-drp-clear-inline::after,
  .sf-drp-nav::after,
  .btn-action-link::after,
  .btn-clean-link::after,
  .btn-summary-link::after,
  .btn-change-address::after,
  .btn-delete-selected::after,
  .btn-inline-note-pill::after,
  .btn-edit-saved-addr::after,
  #btn-open-products-modal::after,
  .account-order-show-more-btn::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: max(100%, 44px);
    height: max(100%, 44px);
    transform: translate(-50%, -50%);
  }

  /* Vertical-only 44px hit expansion for full-width/adjacent bar buttons (zero horizontal overlap) */
  .search-selector-title::after,
  .sticky-selector-title::after,
  .search-camera-inside-btn::after,
  .search-btn-submit::after,
  .sticky-search-submit::after,
  .mobile-search-submit::after,
  .pdp-mobile-chat-btn::after,
  .pdp-mobile-btn::after,
  .checkout-mobile-chat-btn::after,
  .btn-mobile-submit::after,
  .cd-checkout-btn::after,
  .qv-chat-btn::after,
  .qv-btn-cart::after,
  .qv-btn-buy::after,
  .buyer-modal-btn::after,
  .btn-modal-primary::after,
  .btn-modal-cancel::after,
  .cd-edit-toggle::after,
  .cd-delete-selected::after {
    content: "";
    position: absolute;
    left: -4px;
    right: -4px;
    top: 50%;
    height: max(100%, 44px);
    transform: translateY(-50%);
  }

  /* Cart item remove (top-right corner above .btn-change-sku): expand upward & horizontally without intercepting .btn-change-sku below */
  .btn-item-remove::after {
    content: "";
    position: absolute;
    top: -15px;
    bottom: -2px;
    left: -10px;
    right: -8px;
  }

  /* 4.3 Quantity Steppers (- | input | +): Outward-Only Hit Zones (Zero Overlap with Center Input!) */
  .sku-stepper,
  .sku-stepper-clean,
  .mro-qty-stepper,
  .qty-stepper,
  .cd-qty-stepper {
    overflow: visible !important;
  }

  .sku-stepper .btn-stepper:first-child,
  .sku-stepper-clean button:first-child,
  .mro-qty-stepper .stepper-btn:first-child,
  .qty-stepper .btn-qty-minus,
  .cd-qty-stepper .cd-qty-btn:first-child {
    position: relative;
    border-top-left-radius: 4px !important;
    border-bottom-left-radius: 4px !important;
  }

  .sku-stepper .btn-stepper:last-child,
  .sku-stepper-clean button:last-child,
  .mro-qty-stepper .stepper-btn:last-child,
  .qty-stepper .btn-qty-plus,
  .cd-qty-stepper .cd-qty-btn:last-child {
    position: relative;
    border-top-right-radius: 4px !important;
    border-bottom-right-radius: 4px !important;
  }

  /* Minus button extends outward LEFT only (right edge stays flush at 0, never touching qty input) */
  .sku-stepper .btn-stepper:first-child::after,
  .sku-stepper-clean button:first-child::after,
  .mro-qty-stepper .stepper-btn:first-child::after,
  .qty-stepper .btn-qty-minus::after {
    content: "";
    position: absolute;
    right: 0;
    top: 50%;
    width: max(100%, 44px);
    height: 44px;
    transform: translateY(-50%);
  }

  /* Plus button extends outward RIGHT within parent padding (left edge stays flush at 0, never touching qty input or overflowing scrollWidth) */
  .sku-stepper .btn-stepper:last-child::after,
  .sku-stepper-clean button:last-child::after,
  .mro-qty-stepper .stepper-btn:last-child::after,
  .qty-stepper .btn-qty-plus::after {
    content: "";
    position: absolute;
    left: 0;
    right: -9px;
    top: 50%;
    height: 44px;
    transform: translateY(-50%);
  }

  /* Cart Drawer item remove + qty stepper: asymmetric vertical split within 10px .cd-items-list padding so Trash, '+' and scrollWidth never collide */
  .cd-item-remove {
    position: relative;
  }

  .cd-item-remove::after {
    content: "";
    position: absolute;
    top: -17px;
    bottom: -3px;
    left: -10px;
    right: -9px;
  }

  .cd-qty-stepper .cd-qty-btn:first-child::after {
    content: "";
    position: absolute;
    right: 0;
    left: -20px;
    top: -3px;
    bottom: -15px;
  }

  .cd-qty-stepper .cd-qty-btn:last-child::after {
    content: "";
    position: absolute;
    left: 0;
    right: -9px;
    top: -3px;
    bottom: -15px;
  }

  /* 4.4 Checkboxes in Cart & Cart Drawer: place <input> at z-index: 1 above ::before so center clicks hit <input> directly while outer 44px ring hits <label> */
  .cart-select-all > input[type="checkbox"],
  .cd-select-all > input[type="checkbox"],
  .col-check-item > input[type="checkbox"],
  .ios-chk-hit > input[type="checkbox"] {
    position: relative;
    z-index: 1;
  }

  .cart-select-all::before,
  .cd-select-all::before {
    content: "";
    position: absolute;
    left: -4px;
    right: -4px;
    top: 50%;
    height: max(100%, 44px);
    transform: translateY(-50%);
  }

  .col-check-item::before {
    content: "";
    position: absolute;
    top: 50%;
    left: -10px;
    right: -6px;
    height: max(100%, 44px);
    transform: translateY(-50%);
  }

  .ios-chk-hit::before {
    content: "";
    position: absolute;
    top: 50%;
    left: -10px;
    right: -6px;
    height: 44px;
    transform: translateY(-50%);
  }

  /* 4.5 Paired / Adjacent Controls: Split-Gap Asymmetric Hit Zones (Zero Overlap!) */
  /* Mobile Search Modal: Clear (left) + Camera (right) with 4px gap */
  .mobile-search-clear-btn,
  .mobile-search-camera-btn {
    position: relative;
  }

  .mobile-search-clear-btn::after {
    content: "";
    position: absolute;
    top: -7px;
    bottom: -7px;
    left: -12px;
    right: -2px;
  }

  .mobile-search-camera-btn::after {
    content: "";
    position: absolute;
    top: -7px;
    bottom: -7px;
    left: -2px;
    right: -12px;
  }

  /* Orders & Notification Sheet Headers: Action Link (left) + Close (right) with 6px gap */
  .orders-header-all-link,
  .orders-sheet-close-btn,
  .notif-mark-all-btn,
  .notif-sheet-close-btn,
  .qv-full-link,
  .qv-close-btn {
    position: relative;
  }

  .orders-header-all-link::after,
  .notif-mark-all-btn::after,
  .qv-full-link::after {
    content: "";
    position: absolute;
    top: 50%;
    left: -8px;
    right: -3px;
    height: max(100%, 44px);
    transform: translateY(-50%);
  }

  .orders-sheet-close-btn::after,
  .notif-sheet-close-btn::after,
  .qv-close-btn::after {
    content: "";
    position: absolute;
    top: 50%;
    left: -7px;
    right: -9px;
    height: 44px;
    transform: translateY(-50%);
  }

  /* Floating Chat Widget Close Button: outward right/top/bottom so iframe expand button on left isn't overlapped */
  .storefront-chat-widget__close-btn::after {
    content: "";
    position: absolute;
    top: -10px;
    bottom: -6px;
    left: -2px;
    right: -14px;
  }

  /* Floating Right Dock on Mobile (visual 38x38px circles with 8px gap -> 44x44px hit box with 2px clearance) */
  .storefront-right-dock .dock-item {
    overflow: visible !important;
  }

  .storefront-right-dock .dock-item::after {
    content: "";
    position: absolute;
    inset: -3px;
    bottom: -3px;
    left: -3px;
    right: -3px;
    height: auto;
    background: transparent;
  }

  /* Header Nav Cluster on Mobile: 44px vertical touch zone, split horizontal gap */
  .header-nav-cluster .nav-cluster-item {
    position: relative;
  }

  .header-nav-cluster .nav-cluster-item::after {
    content: "";
    position: absolute;
    top: 50%;
    left: -2px;
    right: -2px;
    height: max(100%, 44px);
    transform: translateY(-50%);
  }

  /* SKU Swatch Cards (gap: 8px -> 4px vertical/horizontal hit expansion reaches >= 44px height with 0px overlap) */
  .sku-swatch-card,
  .sku-clean-item,
  .mro-sku-pill {
    position: relative;
  }

  .sku-swatch-card::after,
  .sku-clean-item::after,
  .mro-sku-pill::after {
    content: "";
    position: absolute;
    inset: -4px;
  }

  /* Wallet QR Copy & Share/Download Buttons (6px gap) */
  .copy-badge-btn,
  .wallet-qr-util-btn,
  .btn-qr-util,
  .kg-copy-btn {
    position: relative;
  }

  .copy-badge-btn::after,
  .wallet-qr-util-btn::after,
  .btn-qr-util::after,
  .kg-copy-btn::after {
    content: "";
    position: absolute;
    top: 50%;
    left: -3px;
    right: -3px;
    height: 44px;
    transform: translateY(-50%);
  }

  /* Stacked Checkout Bank Copy Buttons (.topup-bank-row ~36px pitch -> max 6px vertical expansion per side) */
  .btn-topup-copy {
    position: relative;
  }

  .btn-topup-copy::after {
    content: "";
    position: absolute;
    top: -6px;
    bottom: -6px;
    left: -10px;
    right: -10px;
  }

  /* Auth Modal inline links (Quên mật khẩu, Đăng ký ngay, Đổi số khác, Gửi lại mã) */
  #btn-modal-to-forgot,
  #btn-switch-to-otp-mode,
  #btn-switch-to-password-mode,
  #btn-modal-change-phone,
  #btn-forgot-change-phone,
  #btn-modal-resend-otp,
  #btn-modal-forgot-resend-otp,
  #btn-modal-back-from-forgot,
  #modal-unlock-balance .unlock-balance-forgot button {
    position: relative;
  }

  #btn-modal-to-forgot::after,
  #btn-switch-to-otp-mode::after,
  #btn-switch-to-password-mode::after,
  #btn-modal-change-phone::after,
  #btn-forgot-change-phone::after,
  #btn-modal-resend-otp::after,
  #btn-modal-forgot-resend-otp::after,
  #btn-modal-back-from-forgot::after,
  #modal-unlock-balance .unlock-balance-forgot button::after {
    content: "";
    position: absolute;
    top: 50%;
    left: -8px;
    right: -8px;
    height: 44px;
    transform: translateY(-50%);
  }

  /* Cart SKU modal dense rows (30px stepper + 5px row padding = 40px row pitch): cap vertical hit expansion to prevent row-to-row overlap */
  #modal-cart-sku .sku-stepper .btn-stepper:first-child::after,
  #modal-cart-sku .sku-stepper .btn-stepper:last-child::after {
    height: 38px;
  }

  /* Account Sidebar Left-Edge Toggle (28x74px visual -> cap rightward expansion to 4px gutter so adjacent account cards/controls are never covered) */
  .account-sidebar:not(.is-open) .account-sidebar-toggle::after {
    content: "";
    position: absolute;
    top: -4px;
    bottom: -4px;
    left: 0;
    width: 32px;
  }
}
