/* PWA / standalone: safe areas + full-screen overlay layout */
html {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

html.pwa-standalone body {
  min-height: 100dvh;
}

/* Shared full-screen overlay shell */
.auth-modal,
.modal-overlay,
.maker-overlay,
.drop-modal-overlay,
.custom-modal-overlay,
.trade-modal-overlay,
.sell-price-modal-overlay,
.comment-modal,
.image-overlay,
.thuldai-bot-modal,
.fullscreen-modal,
.chat-modal-overlay,
.simple-chat-modal,
#feedbackModal,
[data-site-overlay-root] {
  box-sizing: border-box;
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  min-height: 100dvh;
  max-height: 100dvh;
}

@supports (height: 100dvh) {
  .auth-modal,
  .modal-overlay,
  .maker-overlay,
  .drop-modal-overlay,
  .custom-modal-overlay,
  .trade-modal-overlay,
  .sell-price-modal-overlay,
  .comment-modal,
  .image-overlay,
  .thuldai-bot-modal,
  .fullscreen-modal,
  .chat-modal-overlay,
  .simple-chat-modal,
  #feedbackModal,
  [data-site-overlay-root] {
    height: 100dvh;
  }
}

.comment-modal:not(.active) {
  pointer-events: none !important;
}

.comment-modal.active,
.comment-modal[style*="flex"],
.auth-modal.active,
.modal-overlay.active,
.drop-modal-overlay.active,
.image-overlay.active,
.thuldai-bot-modal[style*="flex"] {
  pointer-events: auto;
}

.comment-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.comment-modal-close {
  position: relative !important;
  top: auto !important;
  right: auto !important;
  flex-shrink: 0;
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
  z-index: 2;
}

.modal-close,
.auth-modal-close,
.bot-close,
.image-overlay-close,
.drop-modal-close,
.x-btn {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}

#formModal.active .modal-dialog {
  max-height: calc(100dvh - var(--safe-top) - var(--safe-bottom));
}

html.pwa-standalone .thuldai-bot-modal[style*="flex"] {
  padding: 0;
  height: 100dvh;
  max-height: 100dvh;
  box-sizing: border-box;
  background: var(--bg-secondary);
}

html.pwa-standalone .thuldai-bot-modal .bot-container {
  width: 100%;
  height: 100%;
  min-height: 100dvh;
  max-height: 100dvh;
  border: none;
  box-shadow: none;
}

html.pwa-standalone .thuldai-bot-modal .bot-header {
  padding-top: calc(8px + var(--safe-top));
}

@media (max-width: 779px) {
  .thuldai-bot-modal[style*="flex"],
  .thuldai-bot-modal.active {
    padding: 0 !important;
    background: var(--bg-secondary);
  }
}

html.pwa-standalone .modal-overlay.active .modal-dialog {
  min-height: calc(100dvh - var(--safe-top) - var(--safe-bottom));
}

/* Modal toolbar: title left, close right */
#formModal.active .box-head,
#authModal.active .auth-modal-header,
#accountModal.active .auth-modal-header {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  gap: 0.75rem;
  width: 100%;
  box-sizing: border-box;
  text-align: left;
}

#authModal.active .auth-modal-header,
#accountModal.active .auth-modal-header {
  background: none;
}

.modal-header-text {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  text-align: left;
}

#formModal.active .box-head .inline-icon-text,
#authModal.active .auth-modal-title,
#authModal.active .inline-icon-text,
#accountModal.active .auth-modal-title,
#accountModal.active .inline-icon-text {
  text-align: left;
  margin: 0;
}

#formModal.active .box-head .modal-close,
#authModal.active .auth-modal-close,
#accountModal.active .auth-modal-close {
  order: 2;
  position: static !important;
  top: auto !important;
  right: auto !important;
  left: auto !important;
  inset: auto !important;
  flex: 0 0 44px;
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  margin: 0 0 0 auto !important;
  align-self: center;
  z-index: 5;
}

#formModal.active .modal-dialog {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

#formModal.active .box-head {
  flex-shrink: 0;
  height: auto;
  min-height: 3.25rem;
  max-height: none;
  z-index: 2;
}

#formModal.active .box-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  height: auto;
  max-height: none;
  justify-content: flex-start;
}

#formModal.active .box-body textarea {
  margin-top: 0;
}

@media (max-width: 799px) {
  #formModal.active .box-head {
    padding-top: max(0.5rem, var(--safe-top));
    padding-right: max(0.75rem, var(--safe-right));
    padding-left: max(0.75rem, var(--safe-left));
  }

  #authModal.active .auth-modal-header,
  #accountModal.active .auth-modal-header {
    padding-top: max(0.5rem, calc(var(--safe-top) * 0.25));
    padding-right: max(3.25rem, calc(44px + var(--safe-right)));
    padding-left: max(0.5rem, var(--safe-left));
    min-height: 3.25rem;
  }

  #authModal.active .auth-modal-close,
  #accountModal.active .auth-modal-close {
    position: fixed !important;
    top: max(10px, var(--safe-top)) !important;
    right: max(10px, var(--safe-right)) !important;
    left: auto !important;
    margin: 0 !important;
    z-index: calc(var(--z-overlay) + 15);
  }
}
