.modal-content {
  scrollbar-width: thin;
  scrollbar-color: #7c3aed transparent;
}

.modal-content::-webkit-scrollbar {
  width: 5px;
  height: 5px;
}
.modal-content::-webkit-scrollbar-track {
  background: rgba(0, 0, 0, 0.06);
}
.modal-content::-webkit-scrollbar-thumb {
  background: linear-gradient(135deg, #7c3aed, #22d3ee);
  border-radius: 8px;
}

.modal {
  display: none;
  position: fixed;
  z-index: 1050;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  outline: 0;
  background-color: rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: var(--crm-hover-transition);
}

.modal.show {
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 1;
  overflow-x: hidden;
  overflow-y: auto;
}

#baseConfirmationModal.modal {
  z-index: 2800;
}

.modal-content {
  position: relative;
  background-color: var(--bg-white);
  background-clip: padding-box;
  border-radius: 12px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);

  box-sizing: border-box;
  width: clamp(320px, 92%, 1100px);
  margin: 2rem auto;
  transform: scale(0.96);
  opacity: 0;
  transition: var(--crm-hover-transition);
  
  max-height: calc(var(--app-vh, 100dvh) - 4rem - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  height: fit-content;
  display: flex;
  flex-direction: column;
  /* visible: hover scale of footer actions must paint. Scroll stays on
     .modal-scroll-body. A non-none transform on this same box would turn
     overflow:visible into a clipper, so the open state uses transform: none. */
  overflow: visible;
}

#baseModal .modal-content.modal--narrow {
  width: clamp(340px, 70vw, 560px);
}

#feedbackModal .modal-content {
  width: min(640px, 92vw);
  max-width: 92vw;
}

#feedbackModal .feedback-bug-field {
  margin-bottom: 0;
}

#feedbackModal .feedback-bug-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border, #e2e8f0);
  background: var(--bg-white, #fff);
  box-sizing: border-box;
}

#feedbackModal .feedback-bug-toggle__text {
  font-weight: 600;
  color: var(--text-strong, var(--text));
  font-size: 14px;
  line-height: 1.2;
}

#feedbackModal .feedback-bug-toggle__switch {
  flex-shrink: 0;
}

#feedbackModal #feedbackForm .form-group > label {
  display: block;
  font-weight: 600;
  color: var(--text-strong, var(--text));
  margin-bottom: 6px;
}

#feedbackModal #feedbackForm .feedback-title-input {
  display: block;
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border, #e2e8f0);
  background: var(--bg-white, #fff);
  color: var(--text, #0f172a);
  font-size: 14px;
  line-height: 1.2;
  box-sizing: border-box;
}

#feedbackModal #feedbackForm .feedback-title-input:focus {
  outline: none;
  border-color: var(--border-strong, #cbd5e1) !important;
  box-shadow: 0 0 0 3px rgba(148, 163, 184, 0.14) !important;
}

#baseConfirmationModal .modal-content.modal--narrow {
  width: clamp(340px, 70vw, 560px);
}

.modal.show .modal-content {
  transform: none;
  opacity: 1;
}

.modal.fade-out .modal-content {
  transform: scale(0.8);
  opacity: 0;
}

.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1040;
  width: 100vw;
  height: 100vh;
  background-color: #000;
  opacity: 0.5;
}

.modal.closing .modal-content {
  transform: translateY(-16px);
  opacity: 0;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
  border-radius: 12px 12px 0 0;
  overflow: hidden;
}

.modal-title {
  margin: 0;
  font-size: 20px;
  line-height: 1.2;
  font-weight: 600;
  color: var(--text-strong);
}

.modal-close.close,
.builder-modal__close {
  position: static;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 400;
  line-height: 1;
  padding: 0;
  text-align: center;
  -webkit-appearance: none;
  appearance: none;
  color: var(--text-light);
  cursor: pointer;
  border-radius: 8px;
  transition: var(--crm-hover-transition);
  background: transparent;
  border: none;
}

.modal-close.close:hover,
.builder-modal__close:hover {
  color: #d9363e;
  background: var(--bg-soft-2);
}

.modal-body {
  padding: 10px 24px 0;
  /* без flex-grow: короткие диалоги не растягиваются на max-height */
  flex: 0 1 auto;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.modal-body:has(> form.modal-footer-host),
.modal-body:has(> .modal-footer-host) {
  overflow: visible;
}

/* Длинная форма с футером: верх/низ тела не дублируют отступы карточек и кнопок. */
.modal-body:has(> form.modal-footer-host) {
  padding-top: 0;
  padding-bottom: 0;
}

.modal-body > form {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
}

.modal-body > form.modal-footer-host {
  overflow: visible;
  gap: 0;
}

.modal-footer {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  position: static;
  background: var(--bg-white);
  flex: 0 0 auto;
}

#baseConfirmationModal .modal-footer {
  display: none;
}

.modal-content > .modal-body:not(:has(form, .modal-footer-host, .modal-scroll-body)) {
  overflow: auto;
  padding-bottom: 16px;
}
/* Единый футер модалок CRM: .button-wrapper + .action-btn → 50/50,
   слева Отмена, справа основное действие. */
.modal .button-wrapper,
.modal-content .button-wrapper,
.button-wrapper.modal-actions,
.crm-modal .button-wrapper {
  display: flex;
  gap: 5px !important;
  /* center, не stretch: stretch + Bootstrap box-sizing:border-box
     фиксирует used-height линии — padding кнопки не меняет её внешний размер. */
  align-items: center;
}

.modal .button-wrapper > .action-btn,
.modal-content .button-wrapper > .action-btn,
.button-wrapper.modal-actions > .action-btn,
.crm-modal .button-wrapper > .action-btn {
  flex: 1 1 0;
  min-width: 0;
  width: 100%;
  max-width: none;
  height: auto;
  min-height: 40px;
  padding: 10px 16px;
  font-size: 13px;
  line-height: 1.2;
  box-sizing: border-box;
}

/* Футер — сосед скролла, не sticky: поля не уезжают под кнопки. */
.modal-footer-host {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  overflow: visible;
}

.modal-footer-host > .modal-scroll-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding-top: 16px;
  padding-bottom: 0;
}

/* После wrap в .modal-scroll-body form.gap больше не действует на секции. */
.modal-footer-host > .modal-scroll-body > .form-section + .form-section {
  margin-top: 20px;
}

.modal-footer-host > .button-wrapper:last-child,
.modal-body > form > .button-wrapper:last-child,
.modal-body > .button-wrapper:last-of-type {
  position: static;
  z-index: 4;
  flex: 0 0 auto;
  margin-top: auto;
  margin-left: -24px;
  margin-right: -24px;
  margin-bottom: 0;
  padding: 16px 28px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg-white);
  box-shadow: none;
  overflow: visible;
  border-radius: 0 0 12px 12px;
}

.modal-content > .button-wrapper {
  position: static;
  z-index: 4;
  flex: 0 0 auto;
  margin-top: auto;
  margin-left: 0;
  margin-right: 0;
  margin-bottom: 0;
  padding: 16px 28px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg-white);
  box-shadow: none;
  overflow: visible;
  border-radius: 0 0 12px 12px;
}

.crm-modal__dialog > .button-wrapper,
.crm-modal__footer {
  flex: 0 0 auto;
  margin-top: auto;
  position: static;
  z-index: 4;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  background: var(--bg-white);
}

.crm-modal__body .button-wrapper {
  position: static;
  z-index: 4;
  margin-top: auto;
  background: var(--bg-white);
}

#baseConfirmationModal .modal-confirmation-actions {
  margin-top: auto;
}

#baseConfirmationModal .delete-confirm p {
  margin: 0;
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
}

.modal-footer .btn {
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.2;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--text);
}

.modal-footer .btn.btn-primary {
  background: #1677ff;
  border-color: #1677ff;
  color: #fff;
}

.modal-footer .btn.btn-primary:hover {
  background: #165fcc;
  border-color: #165fcc;
}

.modal-footer .btn.btn-secondary:hover {
  background: var(--bg-soft-2);
}

.modal-content h2.modal-title {
  margin: 0;
}

@media (max-width: 576px) {
  .modal {
    align-items: flex-start;
    padding-top: max(12px, env(safe-area-inset-top));
    padding-bottom: env(safe-area-inset-bottom);
  }
  .modal-content {
    margin: 0.5rem auto;

    width: calc(100% - 16px);
    max-width: 100%;
    max-height: calc(var(--app-vh, 100dvh) - 1rem - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    margin-top: 0;
  }

  #feedbackModal .modal-content {
    width: calc(100% - 16px);
    max-width: 100%;
  }
  .modal-header {
    padding: 16px 16px;
  }
  .modal-body {
    padding: 10px 14px 0;
  }
  .modal-footer-host > .button-wrapper:last-child,
  .modal-body > form > .button-wrapper:last-child,
  .modal-body > .button-wrapper:last-of-type {
    margin-left: -14px;
    margin-right: -14px;
    margin-bottom: 0;
    padding: 14px 18px 18px;
  }
  .modal-content > .button-wrapper {
    margin-left: 0;
    margin-right: 0;
    margin-bottom: 0;
    padding: 14px 18px 18px;
  }
  .modal-footer {
    padding: 12px 16px;
  }
}

body.modal-open {
  overflow: hidden;
  touch-action: none;
}


.modal-content .modal-text-field {
  display: block;
  width: 100%;
  box-sizing: border-box;
  padding: 8px 10px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  line-height: 1.4;
  background: var(--bg-white, #fff);
  color: var(--text, #111827);
}

.modal-content .modal-text-field::placeholder {
  color: #9ca3af;
  opacity: 1;
}

.modal-content .modal-text-field:focus {
  outline: none;
  border-color: rgba(67, 97, 238, 0.45);
  box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.15);
}

.modal-content .modal-text-field--multiline {
  resize: vertical;
  min-height: 88px;
}

.modal-content #crm-tag-input.modal-text-field {
  padding: 10px 12px;
}

.modal-content .macro-form .field-hint,
.modal-content .modal-body > .field-hint,
.modal-content .modal-body .field-hint {
  color: #6b7280;
  font-size: 12px;
}

.modal-content .macro-form .field-hint {
  margin-top: 4px;
}

.modal-content .field-hint--more-space {
  margin-top: 6px;
}


html.dark-theme .modal-content {
  background-color: var(--bg-elevated);
  border-color: var(--border);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
}

html.dark-theme .modal-header {
  border-bottom-color: var(--border);
}

html.dark-theme .modal-footer {
  border-top-color: var(--border);
  background: var(--bg-elevated);
}

html.dark-theme .modal-footer-host > .button-wrapper:last-child,
html.dark-theme .modal-body > form > .button-wrapper:last-child,
html.dark-theme .modal-body > .button-wrapper:last-of-type,
html.dark-theme .modal-content > .button-wrapper,
html.dark-theme .crm-modal__dialog > .button-wrapper,
html.dark-theme .crm-modal__footer,
html.dark-theme .crm-modal__body .button-wrapper {
  background: var(--bg-elevated);
  border-top-color: var(--border);
  box-shadow: none;
}

html.dark-theme .modal-footer .btn {
  background: var(--bg-soft);
  border-color: var(--border);
  color: var(--text);
}

html.dark-theme .modal-footer .btn.btn-secondary:hover {
  background: var(--bg-soft-2);
}

html.dark-theme .modal-content::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.04);
}

html.dark-theme .modal {
  background-color: rgba(0, 0, 0, 0.7);
}


html.dark-theme .modal-content .form-group input[type="text"],
html.dark-theme .modal-content .form-group input[type="number"],
html.dark-theme .modal-content .form-group input[type="search"],
html.dark-theme .modal-content .form-group input[type="email"],
html.dark-theme .modal-content .form-group input[type="password"],
html.dark-theme .modal-content .form-group input[type="url"],
html.dark-theme .modal-content .form-group input[type="tel"],
html.dark-theme .modal-content .form-group textarea,
html.dark-theme .modal-content .form-group select {
  background: var(--bg-soft);
  border-color: var(--border-strong);
  color: var(--text);
}

html.dark-theme .modal-content .form-group input::placeholder,
html.dark-theme .modal-content .form-group textarea::placeholder {
  color: var(--text-light);
  opacity: 0.9;
}

html.dark-theme .modal-content .form-group input:focus,
html.dark-theme .modal-content .form-group textarea:focus,
html.dark-theme .modal-content .form-group select:focus {
  outline: none;
  border-color: #a78bfa;
  box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.22);
}

html.dark-theme .modal-content .modal-text-field,
html.dark-theme .modal-content #crm-tag-input.modal-text-field {
  background: var(--bg-soft);
  border-color: var(--border-strong);
  color: var(--text);
}

html.dark-theme .modal-content .modal-text-field::placeholder {
  color: var(--text-light);
  opacity: 0.9;
}

html.dark-theme .modal-content .modal-text-field:focus {
  border-color: #a78bfa;
  box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.22);
}

html.dark-theme .modal-content .macro-form .field-hint,
html.dark-theme .modal-content .modal-body > .field-hint,
html.dark-theme .modal-content .modal-body .field-hint {
  color: var(--text-light);
}

html.dark-theme #feedbackModal #feedbackForm .feedback-title-input:focus {
  border-color: rgba(148, 163, 184, 0.4) !important;
  box-shadow: 0 0 0 3px rgba(148, 163, 184, 0.08) !important;
}

html.dark-theme #feedbackModal .feedback-bug-toggle {
  background: var(--bg-elevated);
  border-color: var(--border);
}
