/**
 * Общие стили модалок crop для /constructor и /sheet-constructor.
 * Подключается после layout-tool/constructor.css и до theme-dark файлов.
 *
 * Все модалки (cropModal, cropHelpModal, constructorHelpModal) унифицированы
 * по расположению с #colorModal: сверху. Справки — max-width 520px / 1040px,
 * высота карточки 97dvh, тело с flex-прокруткой; у «Конструктор» крестик в углу шапки.
 */

/* ─────────────────────────────────────────────────────────────────────────────
   Общее расположение оверлеев: сверху экрана с минимальным зазором
   ───────────────────────────────────────────────────────────────────────────── */
#cropModal.modal-overlay.open,
#cropHelpModal.modal-overlay.open,
#constructorHelpModal.modal-overlay.open {
  align-items: flex-start !important;
  justify-content: center !important;
  padding-top: max(env(safe-area-inset-top, 0px), 1vh);
  padding-bottom: max(env(safe-area-inset-bottom, 0px), 1vh);
  overflow-y: auto;
  min-height: 100dvh;
}

/* Справка по обрезке — max-width 520px; высота окна 97dvh */
#cropHelpModal.modal-overlay.open > .modal {
  align-self: flex-start !important;
  margin-top: 0 !important;
  box-sizing: border-box;
  flex-shrink: 0;
  display: flex !important;
  flex-direction: column !important;
  overflow: hidden !important;
  min-height: 0 !important;
  width: calc(100vw - 24px) !important;
  max-width: 520px !important;
  height: 97dvh !important;
  max-height: 97dvh !important;
  overscroll-behavior: contain;
}

/* «Справка: Конструктор» — max-width 1040px; высота окна 97dvh */
#constructorHelpModal.modal-overlay.open > .modal {
  align-self: flex-start !important;
  margin-top: 0 !important;
  box-sizing: border-box;
  flex-shrink: 0;
  display: flex !important;
  flex-direction: column !important;
  overflow: hidden !important;
  min-height: 0 !important;
  width: calc(100vw - 24px) !important;
  max-width: 1040px !important;
  height: 97dvh !important;
  max-height: 97dvh !important;
  overscroll-behavior: contain;
}

/* Тело справок заполняет высоту между шапкой и «Понятно», прокрутка внутри */
#cropHelpModal.modal-overlay.open > .modal.crop-help-modal .crop-help-modal__body,
#constructorHelpModal.modal-overlay.open > .modal.crop-help-modal .crop-help-modal__body {
  flex: 1 1 auto;
  min-height: 0;
  max-height: none !important;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#cropHelpModal.modal-overlay.open > .modal.crop-help-modal .crop-help-modal__actions,
#constructorHelpModal.modal-overlay.open > .modal.crop-help-modal .crop-help-modal__actions {
  flex-shrink: 0;
}

/* Обработка изображения: карточка побольше (превью + зум), но тоже сверху */
#cropModal.modal-overlay.open > .modal.crop-modal {
  align-self: flex-start !important;
  margin-top: 0 !important;
  box-sizing: border-box;
  flex-shrink: 0;
  width: calc(100vw - 24px) !important;
  max-width: 680px !important;
  max-height: calc(100dvh - max(env(safe-area-inset-top, 0px), 1vh) - max(env(safe-area-inset-bottom, 0px), 1vh)) !important;
  overflow: hidden !important;
}

/* ─────────────────────────────────────────────────────────────────────────────
   Модалка «Обработка изображения» (#cropModal)
   ───────────────────────────────────────────────────────────────────────────── */
.modal.crop-modal {
  padding: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  max-height: min(96vh, 920px);
  width: 100%;
  max-width: min(96vw, 680px);
  text-align: center;
}
.crop-modal__head {
  position: relative;
  flex-shrink: 0;
  z-index: 6;
  padding: 16px 48px 12px 20px;
  margin: 0;
  text-align: center;
  background: #fff;
  border-bottom: 1px solid var(--border, #E0E0E0);
}
.crop-modal__title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  text-align: center;
  padding: 0 8px;
}
.modal.crop-modal .crop-modal__title { margin-bottom: 0; }
/* Абсолют только в шапке окна обрезки; тот же класс на «?» конструктора — иначе прилипает к правому краю fixed-панели */
.crop-modal__help-btn {
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border, #E0E0E0);
  border-radius: 50%;
  background: var(--bg-soft, #f4f5f7);
  color: var(--muted, #666666);
  font-size: 16px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
}
.crop-modal__head > .crop-modal__help-btn {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
}
.crop-modal__help-btn:hover {
  border-color: var(--accent, #4a6cf7);
  color: var(--accent, #4a6cf7);
  background: #fff;
}
.crop-modal__help-btn--close {
  position: static;
  transform: none;
  border-radius: 8px;
  width: 36px;
  height: 36px;
  font-size: 22px;
  font-weight: 400;
}
.crop-modal__help-btn--close:focus,
.crop-modal__help-btn--close:focus-visible {
  outline: none;
  box-shadow: none;
}
.crop-modal__scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px 20px 8px;
  text-align: center;
  -webkit-overflow-scrolling: touch;
}
.crop-modal__viewport {
  position: relative;
  display: block;
  width: 100%;
  height: min(65vh, 640px);
  max-height: min(65vh, 640px);
  margin: 0 auto 12px;
  overflow: auto;
  overscroll-behavior: contain;
  box-sizing: border-box;
  min-height: 0;
  cursor: grab;
  touch-action: none;
  user-select: none;
}
.crop-modal__viewport.crop-modal__viewport--panning { cursor: grabbing; }
.crop-modal__viewport-center {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 100%;
  width: max-content;
  min-height: 100%;
  height: max-content;
}
.crop-modal__zoom-inner { position: relative; display: block; flex: 0 0 auto; }
.crop-modal__viewport img {
  display: block;
  width: auto;
  height: auto;
  max-width: none;
  max-height: none;
  user-select: none;
  pointer-events: none;
}
body.constructor-page #cropModal img#cropImg { max-width: none !important; }
.crop-modal__frame {
  position: absolute;
  left: 0;
  top: 0;
  z-index: 2;
  box-sizing: border-box;
  border: 1px solid #52525b;
  box-shadow: none;
  cursor: move;
}
.crop-modal__handle {
  position: absolute;
  width: 16px;
  height: 16px;
  background: #fafafa;
  border: 1px solid #52525b;
  border-radius: 2px;
}
.crop-modal__handle[data-h="nw"] { left: -8px; top: -8px; cursor: nwse-resize; }
.crop-modal__handle[data-h="ne"] { right: -8px; top: -8px; cursor: nesw-resize; }
.crop-modal__handle[data-h="sw"] { left: -8px; bottom: -8px; cursor: nesw-resize; }
.crop-modal__handle[data-h="se"] { right: -8px; bottom: -8px; cursor: nwse-resize; }
.crop-modal__footer {
  flex-shrink: 0;
  z-index: 6;
  margin: 0;
  padding: 12px 20px 16px;
  border-top: 1px solid var(--border, #E0E0E0);
  background: #fff;
  box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.06);
}
.crop-modal__footer-row--trim {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 12px;
}
.crop-modal__footer-left,
.crop-modal__footer-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
}
.crop-modal__footer-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted, #666666);
}

/**
 * Единый визуал окна «Обработка изображения» на /sheet-constructor и /constructor.
 * Эталон — конструктор листов (layout-tool-local): белая карточка, тень, вторичные кнопки с нейтральной рамкой.
 * Селектор с #cropModal перебивает различия layout-tool vs layout-tool-local и глобальные .btn-outline из style.css.
 */
#cropModal.modal-overlay .modal.crop-modal {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
  /* Отдельный композитный слой: иначе Safari при большом scrollWidth/scrollHeight превью «ломает» полупрозрачный фон оверлея (чёрный за карточкой) */
  isolation: isolate;
  transform: translate3d(0, 0, 0);
  -webkit-transform: translate3d(0, 0, 0);
}

/* Оверлей — свой слой под карточкой, тот же сценарий WebKit */
#cropModal.modal-overlay.open {
  isolation: isolate;
  transform: translate3d(0, 0, 0);
  -webkit-transform: translate3d(0, 0, 0);
}

#cropModal.modal-overlay .modal.crop-modal .crop-modal__footer .btn {
  box-sizing: border-box;
  font-weight: 600;
  font-size: 13px;
  line-height: 1.2;
  border-radius: 6px;
  min-height: 36px;
  padding: 8px 14px;
}

#cropModal.modal-overlay .modal.crop-modal .crop-modal__footer .btn.btn-small {
  min-height: 34px;
  padding: 6px 12px;
  font-size: 13px;
}

#cropModal.modal-overlay .modal.crop-modal .crop-modal__footer .btn.btn-outline,
#cropModal.modal-overlay .modal.crop-modal .crop-modal__footer .btn.btn-small.btn-outline {
  background: #fff !important;
  color: #1a1d26 !important;
  border: 1px solid #d1d5db !important;
}

#cropModal.modal-overlay .modal.crop-modal .crop-modal__footer .btn.btn-outline:hover:not(:disabled),
#cropModal.modal-overlay .modal.crop-modal .crop-modal__footer .btn.btn-small.btn-outline:hover:not(:disabled) {
  background: #f3f4f6 !important;
  border-color: #9ca3af !important;
  color: #111827 !important;
}

#cropModal.modal-overlay .modal.crop-modal .crop-modal__footer .btn.btn-primary,
#cropModal.modal-overlay .modal.crop-modal .crop-modal__footer .btn.btn-primary.btn-small {
  background: #005bff !important;
  border: 1px solid #005bff !important;
  color: #fff !important;
}

#cropModal.modal-overlay .modal.crop-modal .crop-modal__footer .btn.btn-primary:hover:not(:disabled),
#cropModal.modal-overlay .modal.crop-modal .crop-modal__footer .btn.btn-primary.btn-small:hover:not(:disabled) {
  background: #0047cc !important;
  border-color: #0047cc !important;
  color: #fff !important;
}

/* Превью: минимальный зазор по бокам (1px), без лишнего отступа снизу до футера */
#cropModal.modal-overlay .modal.crop-modal .crop-modal__scroll {
  padding: 0 1px 0;
  -webkit-overflow-scrolling: auto;
}
#cropModal.modal-overlay .modal.crop-modal .crop-modal__viewport {
  margin: 0;
  -webkit-overflow-scrolling: auto;
  transform: translate3d(0, 0, 0);
  -webkit-transform: translate3d(0, 0, 0);
}
#cropModal.modal-overlay #cropZoomInner {
  transform: translate3d(0, 0, 0);
  -webkit-transform: translate3d(0, 0, 0);
}
#cropModal.modal-overlay .modal.crop-modal .crop-modal__footer {
  padding: 12px 16px 16px;
}

#cropHelpModal.modal-overlay { z-index: 10501; }
/* Клоны кнопок в тексте справки: не наследовать absolute от .crop-modal__help-btn */
.crop-help-modal .crop-help-clone .crop-modal__help-btn {
  position: static !important;
  right: auto !important;
  top: auto !important;
  transform: none !important;
}
/* Клон «Обновить раскладку» (btn-success): контраст на белом фоне справки */
#cropHelpModal .crop-help-modal__body .crop-help-clone .btn.btn-success.btn-icon-outline {
  background: #fff !important;
  border: 1px solid #16a34a !important;
  color: #15803d !important;
}
#cropHelpModal .crop-help-modal__body .crop-help-clone .btn.btn-success.btn-icon-outline .constructor-svg-icon {
  stroke: currentColor !important;
}
.crop-help-modal {
  max-width: min(95vw, 880px);
  width: 100%;
  text-align: left;
  container-type: inline-size;
}
.crop-help-modal__head {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
  padding: 12px 16px;
  min-height: 36px;
}
.crop-help-modal__title { margin: 0; font-size: 17px; font-weight: 600; text-align: center; flex: 1; }
.crop-help-modal__body {
  font-size: 13px;
  line-height: 1.55;
  color: var(--text, #1A1A1A);
  max-height: min(62vh, 520px);
  overflow-y: auto;
  padding-right: 4px;
}
.crop-help-modal__body ul { margin: 0 0 12px; padding-left: 1.4em; list-style: disc outside; color: var(--muted, #4b5563); }
.crop-help-modal__body ul li { margin-bottom: 6px; }
.crop-help-modal__body .crop-help-btns-pair { display: inline-flex; align-items: center; gap: 8px; vertical-align: middle; margin-right: 4px; }
.crop-help-modal__body .crop-help-clone { display: inline-flex; align-items: center; vertical-align: middle; margin: 0 6px 2px 0; pointer-events: none; }
.crop-help-modal__body .crop-help-clone__node { pointer-events: none; }
.crop-help-modal__body .crop-help-clone--btn .btn,
.crop-help-modal__body .crop-help-clone--btn.btn { width: 44px; height: 44px; min-width: 44px; min-height: 44px; }
.crop-help-modal__body .crop-help-clone--cart .btn,
.crop-help-modal__body .crop-help-clone--cart.btn { min-height: 36px; padding: 6px 10px; font-size: 12px; }
.crop-help-modal__body .crop-help-clone--toggle .mfooter-toggle-btn { min-width: 44px; min-height: 36px; }
.crop-help-modal__h5 { margin: 0 0 8px; font-size: 13px; font-weight: 600; color: var(--text, #1A1A1A); }
.crop-help-modal__sec { margin-bottom: 16px; }
.crop-help-modal__sec:last-child { margin-bottom: 0; }
.crop-help-modal__sec h4 { margin: 0 0 8px; font-size: 14px; color: var(--text, #1A1A1A); }
.crop-help-modal__sec p { margin: 0 0 8px; color: var(--muted, #4b5563); }
.crop-help-modal__actions { margin-top: 12px; justify-content: flex-end; }

/* Справки обрезки и конструктора: «Понятно» по центру на всю ширину */
#cropHelpModal .crop-help-modal__actions,
#constructorHelpModal .crop-help-modal__actions {
  justify-content: center;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  gap: 0;
}
#cropHelpModal .crop-help-modal__actions > .btn,
#constructorHelpModal .crop-help-modal__actions > .btn {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

.crop-preview-color-modal .modal { position: relative; width: min(440px, 94vw); max-height: 85vh; overflow-y: auto; }
.crop-preview-color-modal .modal h3 { font-size: 14px; margin-bottom: 8px; text-align: center; }
.crop-preview-color-modal__hint { font-size: 11px; color: var(--muted, #666666); margin: 0 0 10px; line-height: 1.4; text-align: center; }
.crop-preview-color-modal__grid { display: grid; grid-template-columns: repeat(12, 1fr); gap: 4px; margin: 0 0 8px; width: 100%; box-sizing: border-box; }
.crop-prev-swatch { aspect-ratio: 1; width: 100%; min-height: 0; padding: 0; border: 1px solid rgba(0, 0, 0, .12); border-radius: 4px; cursor: pointer; box-sizing: border-box; }
.crop-prev-swatch:hover { filter: brightness(1.06); z-index: 1; }
.crop-prev-swatch.selected { outline: 2px solid var(--accent, #4a6cf7); outline-offset: 1px; box-shadow: 0 0 0 1px #fff; }
.crop-preview-color-modal__details { width: 100%; margin: 0 0 4px; text-align: left; }
.crop-preview-color-modal__summary { cursor: pointer; font-size: 12px; font-weight: 600; color: var(--accent, #4a6cf7); user-select: none; list-style: none; padding: 6px 0; }
.crop-preview-color-modal__summary::-webkit-details-marker { display: none; }
.crop-preview-color-modal__details[open] .crop-preview-color-modal__summary { margin-bottom: 0; color: var(--text, #1A1A1A); }
.crop-preview-color-modal__native-hidden { position: absolute; left: 0; bottom: 0; width: 1px; height: 1px; overflow: hidden; opacity: .001; }

@media (max-width: 768px) {
  /* Мобильный: все модалки на 98% ширины */
  #cropModal.modal-overlay.open,
  #cropHelpModal.modal-overlay.open,
  #constructorHelpModal.modal-overlay.open {
    padding-left: 1vw !important;
    padding-right: 1vw !important;
  }
  #cropModal.modal-overlay.open > .modal.crop-modal {
    width: 98vw !important;
    max-width: 98vw !important;
    max-height: calc(100dvh - max(env(safe-area-inset-top, 0px), 1vh) - max(env(safe-area-inset-bottom, 0px), 1vh)) !important;
  }
  #cropHelpModal.modal-overlay.open > .modal,
  #constructorHelpModal.modal-overlay.open > .modal {
    width: 98vw !important;
    max-width: 98vw !important;
    height: 97dvh !important;
    max-height: 97dvh !important;
  }
  #cropModal.modal-overlay .modal.crop-modal .crop-modal__scroll {
    flex: 1 1 auto;
    min-height: 0;
    padding: 0 1px 0;
    -webkit-overflow-scrolling: auto;
  }
  .crop-modal__viewport {
    flex: 1 1 auto;
    min-height: 140px;
    height: calc(98vh - 230px) !important;
    max-height: calc(98vh - 230px) !important;
  }
  /* Подпись «Триминг» у кнопок Авто/Сброс — на телефоне скрыта */
  #cropModal .crop-modal__footer-label {
    display: none !important;
  }
  /* Справки: flex-колонка с прокруткой тела */
  #cropHelpModal.modal-overlay .modal.crop-help-modal,
  #constructorHelpModal.modal-overlay .modal.crop-help-modal {
    display: flex;
    flex-direction: column;
    min-height: 0;
  }
  #cropHelpModal .crop-help-modal__body,
  #constructorHelpModal .crop-help-modal__body {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .crop-modal__footer-row--trim { flex-direction: column; align-items: stretch; }
  .crop-modal__footer-left,
  .crop-modal__footer-right { justify-content: flex-start; }
}
