/**
 * Фоторедактор — полноэкран, фото на весь холст, панель примагничена снизу на всю ширину
 */

/* ---- Токены ---- */
.pe {
  --pe-space-1: 8px;
  --pe-space-2: 16px;
  --pe-space-3: 24px;
  --pe-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --pe-fs-cap: 11px;
  --pe-fs-body: 13px;
  --pe-fw-medium: 500;
  --pe-fw-semibold: 600;
  --pe-radius: 8px;
  --pe-radius-panel: 20px;
  --pe-tabs-h: 44px;
  --pe-bottom-h: 320px;
  --pe-bg: #000000;
  --pe-surface: rgba(28, 28, 30, 0.96);
  --pe-surface-2: rgba(44, 44, 46, 0.9);
  --pe-surface-3: #3a3a3c;
  --pe-text: #ffffff;
  --pe-text-2: #ebebf5;
  --pe-text-3: #8e8e93;
  --pe-separator: rgba(84, 84, 88, 0.65);
  --pe-accent: #0a84ff;
  --pe-accent-hover: #409cff;
  --pe-filter-preview-size: 64px;
}

/* ---- Корень: строго на весь экран, поверх всего ---- */
.pe {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  max-width: 100%;
  margin: 0;
  padding: 0;
  z-index: 10000;
  background: var(--pe-bg);
  font-family: var(--pe-font);
  box-sizing: border-box;
}

.pe.active {
  display: flex;
  flex-direction: column;
}

.pe__root {
  --pe-image-aspect: 1.777;
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  width: 100%;
  overflow: hidden;
  padding-bottom: var(--pe-bottom-h);
  box-sizing: border-box;
}

/* ---- Холст: на всю ширину, фото по центру, без выделения при перетаскивании ---- */
.pe__canvas {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: var(--pe-bg);
  position: relative;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  padding: 0;
}

.pe__canvas-inner {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  touch-action: none;
}

.pe__img,
#photo-modal-img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: auto;
  display: block;
}

/* ---- Оверлей кадрирования: правило третей + виньетка ---- */
.pe__crop-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.pe__crop-overlay.visible {
  opacity: 1;
}

.pe__rule-of-thirds {
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(to right, rgba(255,255,255,0.15) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(255,255,255,0.15) 1px, transparent 1px);
  background-size: 33.33% 33.33%, 33.33% 33.33%;
  background-position: 0 0, 0 0;
}

.pe__vignette {
  position: absolute;
  inset: 0;
  box-shadow: inset 0 0 25% 15% rgba(0,0,0,0.5);
  pointer-events: none;
}

.pe__crop-area-wrap {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
}

.pe__crop-area-wrap.active {
  pointer-events: auto;
}

/* ---- Блок масштаба сверху над фото ---- */
.pe__zoom-bar {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 14px;
  max-width: min(320px, 85vw);
  width: auto;
  background: rgba(28, 28, 30, 0.9);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--pe-separator);
  border-radius: 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
}

.pe__zoom-bar .pe__btn-zoom {
  width: 36px;
  height: 36px;
  min-width: 36px;
  min-height: 36px;
  padding: 0;
  font-size: 20px;
  line-height: 1;
  font-family: var(--pe-font);
  color: var(--pe-text);
  background: var(--pe-surface-3);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.pe__zoom-bar .pe__btn-zoom:hover {
  background: var(--pe-surface-2);
}

.pe__zoom-bar .pe__range {
  flex: 1;
  min-width: 80px;
}

.pe__zoom-bar .pe__zoom-value {
  min-width: 40px;
  font-size: var(--pe-fs-cap);
  font-weight: var(--pe-fw-medium);
  color: var(--pe-accent);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* ---- Кнопка закрытия в правом верхнем углу ---- */
.pe__close-top {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 25;
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  font-size: 22px;
  line-height: 1;
  font-family: var(--pe-font);
  color: var(--pe-text);
  background: rgba(28, 28, 30, 0.9);
  border: 1px solid var(--pe-separator);
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.pe__close-top:hover {
  background: rgba(255, 69, 58, 0.9);
  border-color: #ff453a;
}

.pe__zoom-badge,
.photo-modal-zoom-indicator {
  position: absolute;
  bottom: var(--pe-space-2);
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 14px;
  background: rgba(0, 0, 0, 0.75);
  color: var(--pe-text);
  font-size: var(--pe-fs-cap);
  font-weight: var(--pe-fw-medium);
  border-radius: 20px;
  pointer-events: none;
  z-index: 10;
  display: none;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
}

/* ---- Нижняя панель: всегда на всю ширину экрана, отступы внутри ---- */
.pe__bottom {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100vw;
  min-height: var(--pe-bottom-h);
  height: var(--pe-bottom-h);
  margin: 0;
  padding: 0 calc(var(--pe-space-2) * 2) calc(var(--pe-space-2) * 2) calc(var(--pe-space-2) * 2);
  padding-bottom: calc(var(--pe-space-2) * 2 + env(safe-area-inset-bottom, 0));
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  background: var(--pe-surface);
  backdrop-filter: blur(24px) saturate(180%);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  border-top: 1px solid var(--pe-separator);
  border-radius: var(--pe-radius-panel) var(--pe-radius-panel) 0 0;
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.35);
  z-index: 50;
  overflow: hidden;
}

.pe__gripper {
  flex-shrink: 0;
  height: 4px;
  margin: 6px auto 2px;
  width: 36px;
  background: var(--pe-text-3);
  border-radius: 2px;
  opacity: 0.6;
}

.pe__footer-main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---- Таб-бар ---- */
.pe__tabs {
  flex-shrink: 0;
  height: var(--pe-tabs-h);
  min-height: var(--pe-tabs-h);
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 0;
  padding: 0;
  background: transparent;
  position: relative;
  width: 100%;
}

.pe__tab {
  flex: 1;
  max-width: 140px;
  padding: 0 var(--pe-space-2);
  font-size: var(--pe-fs-body);
  font-weight: var(--pe-fw-medium);
  font-family: var(--pe-font);
  color: var(--pe-text-3);
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s, border-color 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.pe__tab:hover {
  color: var(--pe-text);
}

.pe__tab.active {
  color: var(--pe-accent);
  border-bottom-color: var(--pe-accent);
}

.pe__panels {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  position: relative;
}

.pe__panel {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  padding: var(--pe-space-3) 0;
  overflow: hidden;
  background: transparent;
  width: 100%;
  gap: var(--pe-space-2);
}

.pe__panel.active,
.photo-modal-tab-content.active {
  display: flex;
}

.photo-modal-tab-content[hidden] {
  display: none !important;
}

/* ---- Вкладка «Кадрирование»: пропорции и поворот в одной строке ---- */
.pe__crop-section {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: var(--pe-space-2);
  padding: 0 var(--pe-space-2);
  width: 100%;
  box-sizing: border-box;
  min-height: 0;
}

.pe__aspect-scroll {
  display: flex;
  flex: 1;
  min-width: 0;
  gap: var(--pe-space-2);
  overflow-x: auto;
  padding: var(--pe-space-2) 0;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.pe__aspect-scroll::-webkit-scrollbar {
  display: none;
}

.pe__aspect-chip {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: var(--pe-space-1) var(--pe-space-2);
  min-width: 64px;
  font-size: var(--pe-fs-cap);
  font-weight: var(--pe-fw-medium);
  font-family: var(--pe-font);
  color: var(--pe-text-2);
  background: var(--pe-surface-3);
  border: 2px solid transparent;
  border-radius: var(--pe-radius);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.pe__aspect-chip:hover {
  background: var(--pe-surface-2);
  color: var(--pe-text);
}

.pe__aspect-chip.active {
  background: rgba(10, 132, 255, 0.25);
  color: var(--pe-accent);
  border-color: var(--pe-accent);
}

.pe__aspect-icon {
  font-size: 18px;
  line-height: 1;
  opacity: 0.9;
}

.pe__fine-rotation {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: var(--pe-space-2);
  padding: var(--pe-space-1) 0;
  min-width: 140px;
  max-width: 200px;
}

.pe__fine-rotation-icon {
  flex-shrink: 0;
  font-size: 18px;
  color: var(--pe-text-3);
}

.pe__fine-rotation-slider-wrap {
  flex: 1;
  position: relative;
  min-width: 0;
}

.pe__fine-rotation-value {
  flex-shrink: 0;
  min-width: 40px;
  font-size: var(--pe-fs-body);
  font-weight: var(--pe-fw-medium);
  color: var(--pe-accent);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.pe__slider-bubble {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translate(-50%, -8px);
  padding: 4px 8px;
  background: rgba(0,0,0,0.85);
  color: var(--pe-text);
  font-size: var(--pe-fs-cap);
  font-weight: var(--pe-fw-medium);
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
}

.pe__slider-bubble.visible {
  opacity: 1;
}

/* ---- Вкладка «Фильтры»: карусель и ползунок всегда в одной видимости ---- */
.pe__filters-carousel-wrap {
  flex: 0 0 auto;
  min-height: calc(var(--pe-filter-preview-size) + 52px);
  overflow: visible;
  padding: var(--pe-space-2) 0 0;
}

.pe__filters-carousel {
  display: flex;
  gap: var(--pe-space-3);
  overflow-x: auto;
  padding: var(--pe-space-2) 0 calc(var(--pe-space-3) * 2) 0;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.pe__filters-carousel::-webkit-scrollbar {
  display: none;
}

.pe__filter-item {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--pe-space-1);
  padding: var(--pe-space-1) 0;
  font-size: var(--pe-fs-cap);
  font-weight: var(--pe-fw-medium);
  font-family: var(--pe-font);
  color: var(--pe-text-2);
  background: none;
  border: none;
  border-radius: var(--pe-radius);
  cursor: pointer;
  transition: color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.pe__filter-item:hover {
  color: var(--pe-text);
}

.pe__filter-item.active {
  color: var(--pe-text);
}

.pe__filter-item.active .pe__filter-preview {
  border-color: var(--pe-accent);
  box-shadow: 0 0 0 2px var(--pe-accent);
}

.pe__filter-item.active .pe__filter-name {
  font-weight: var(--pe-fw-semibold);
}

.pe__filter-preview {
  width: var(--pe-filter-preview-size);
  height: var(--pe-filter-preview-size);
  min-width: var(--pe-filter-preview-size);
  min-height: var(--pe-filter-preview-size);
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid transparent;
  background: var(--pe-surface-3);
  background-size: cover;
  background-position: center;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.pe__filter-name {
  max-width: 76px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
}

.pe__filter-intensity {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: var(--pe-space-2);
  padding-top: var(--pe-space-3);
  margin-top: var(--pe-space-2);
  border-top: 1px solid var(--pe-separator);
}

.pe__filter-intensity-icon {
  font-size: 16px;
  color: var(--pe-text-3);
}

.pe__filter-intensity .pe__range {
  flex: 1;
  min-width: 0;
}

.pe__filter-intensity-value {
  flex-shrink: 0;
  min-width: 36px;
  font-size: var(--pe-fs-body);
  font-weight: var(--pe-fw-medium);
  color: var(--pe-accent);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* ---- Вкладка «Коррекция»: горизонтальная лента параметров ---- */
.pe__adjust-scroll {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  gap: var(--pe-space-3);
  padding: var(--pe-space-2) 0 calc((var(--pe-space-3) + var(--pe-space-2)) * 2) 0;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x mandatory;
  scrollbar-width: thin;
}

.pe__adjust-scroll::-webkit-scrollbar {
  height: 6px;
}

.pe__adjust-scroll::-webkit-scrollbar-track {
  background: var(--pe-surface-3);
  border-radius: 3px;
}

.pe__adjust-scroll::-webkit-scrollbar-thumb {
  background: var(--pe-text-3);
  border-radius: 3px;
}

.pe__adjust-row {
  flex: 0 0 auto;
  width: 116px;
  min-width: 116px;
  scroll-snap-align: start;
  scroll-snap-stop: always;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--pe-space-2);
  padding: var(--pe-space-2) var(--pe-space-2);
  background: var(--pe-surface-2);
  border-radius: var(--pe-radius);
  border: 1px solid var(--pe-separator);
}

.pe__adjust-label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: var(--pe-fs-cap);
  font-weight: var(--pe-fw-medium);
  color: var(--pe-text-2);
  cursor: pointer;
  text-align: center;
  line-height: 1.2;
}

.pe__adjust-label span:last-child {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.pe__adjust-icon {
  display: none;
}

.pe__adjust-row .pe__range {
  width: 100%;
  min-width: 0;
  max-width: none;
  height: 8px;
  margin: 0;
}

.pe__adjust-row .pe__range::-webkit-slider-thumb {
  width: 24px;
  height: 24px;
}

.pe__adjust-row .pe__range::-moz-range-thumb {
  width: 24px;
  height: 24px;
}

.pe__adjust-value {
  font-size: var(--pe-fs-cap);
  font-weight: var(--pe-fw-semibold);
  color: var(--pe-accent);
  font-variant-numeric: tabular-nums;
}

/* ---- Общий ползунок ---- */
.pe__range {
  height: 6px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--pe-surface-3);
  border-radius: 3px;
  outline: none;
}

.pe__range::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--pe-text);
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.pe__range::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--pe-text);
  cursor: pointer;
  border: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

/* ---- Статическая область действий: без боковых отступов ---- */
.pe__footer-actions {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--pe-space-1) var(--pe-space-2);
  padding-bottom: calc(var(--pe-space-1) + env(safe-area-inset-bottom, 0));
  background: transparent;
  gap: var(--pe-space-1);
}

.pe__footer-actions-left {
  display: flex;
  align-items: center;
  gap: var(--pe-space-2);
}

.pe__footer-actions-right {
  display: flex;
  align-items: center;
  gap: var(--pe-space-2);
}

.pe__footer-btn-icon {
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  font-size: 22px;
  line-height: 1;
  font-family: var(--pe-font);
  color: var(--pe-text-2);
  background: var(--pe-surface-3);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, opacity 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.pe__footer-btn-icon:hover {
  background: var(--pe-surface-2);
  color: var(--pe-text);
}

.pe__footer-btn-icon:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.pe__footer-btn-close {
  width: 56px;
  height: 56px;
  min-width: 56px;
  min-height: 56px;
  padding: 0;
  font-size: 24px;
  line-height: 1;
  font-family: var(--pe-font);
  color: var(--pe-text);
  background: var(--pe-surface-3);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.pe__footer-btn-close:hover {
  background: var(--pe-surface-2);
}

.pe__footer-btn-done {
  width: 56px;
  height: 56px;
  min-width: 56px;
  min-height: 56px;
  padding: 0;
  font-size: 26px;
  font-weight: 700;
  line-height: 1;
  font-family: var(--pe-font);
  color: #fff;
  background: var(--pe-accent);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.pe__footer-btn-done:hover {
  background: var(--pe-accent-hover);
}

/* ---- Область кадрирования (создаётся скриптом) ---- */
.crop-area {
  position: absolute;
  border: 2px solid var(--pe-accent);
  background: rgba(10, 132, 255, 0.08);
  box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.5);
  cursor: move;
  box-sizing: border-box;
  user-select: none;
  touch-action: none;
}

.crop-area::before {
  content: '';
  position: absolute;
  inset: -1px;
  border: 1px dashed rgba(255, 255, 255, 0.3);
  border-radius: 2px;
  pointer-events: none;
}

.crop-handles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.crop-handle {
  position: absolute;
  width: 16px;
  height: 16px;
  min-width: 16px;
  min-height: 16px;
  background: var(--pe-accent);
  border: 2px solid var(--pe-text);
  border-radius: 50%;
  pointer-events: auto;
  cursor: pointer;
  z-index: 10;
  touch-action: none;
}

.crop-handle-1 { top: -8px; left: -8px; cursor: nwse-resize; }
.crop-handle-2 { top: -8px; left: 50%; transform: translateX(-50%); cursor: ns-resize; }
.crop-handle-3 { top: -8px; right: -8px; cursor: nesw-resize; }
.crop-handle-4 { top: 50%; right: -8px; transform: translateY(-50%); cursor: ew-resize; }
.crop-handle-5 { bottom: -8px; right: -8px; cursor: nwse-resize; }
.crop-handle-6 { bottom: -8px; left: 50%; transform: translateX(-50%); cursor: ns-resize; }
.crop-handle-7 { bottom: -8px; left: -8px; cursor: nesw-resize; }
.crop-handle-8 { top: 50%; left: -8px; transform: translateY(-50%); cursor: ew-resize; }

/* ---- Адаптив ---- */
@media (max-width: 768px) {
  .pe {
    --pe-bottom-h: 220px;
    --pe-filter-preview-size: 56px;
  }

  .pe__adjust-row {
    width: 100px;
    min-width: 100px;
  }

  .pe__tab {
    font-size: 12px;
  }

  .pe__aspect-chip {
    min-width: 56px;
  }

  .pe__footer-btn-done {
    width: 52px;
    height: 52px;
    min-width: 52px;
    min-height: 52px;
  }
}