/* ============ Reset / base ============ */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; overscroll-behavior: none; }
body { overflow: hidden; }  /* editor screen — стейдж сам управляет канвасом, страница не должна скроллиться */
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  background: #f5f5f7; color: #1d1d1f; font-size: 15px; line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input { font: inherit; }

:root {
  --pink: #e91e63; --pink-dark: #c2185b; --pink-soft: #fce4ec;
  --gray-50: #fafafa; --gray-100: #f5f5f7; --gray-200: #ebebef;
  --gray-300: #d6d6dc; --gray-500: #86868b; --gray-700: #4a4a52;
  --gray-900: #1d1d1f;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.06);
  --shadow-md: 0 4px 16px rgba(0,0,0,.08);
  --shadow-lg: 0 12px 40px rgba(0,0,0,.18);
  --radius: 12px; --radius-lg: 16px;
}
/* 100dvh = dynamic viewport — учитывает выезжающие toolbars браузера (iOS URL bar внизу,
   Chrome Android вверху, и т.д.). Fallback 100vh для старых браузеров. */
.screen { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }

/* ============ Logo ============ */
.logo {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 22px; letter-spacing: 0.5px; color: var(--pink);
  font-weight: 600; user-select: none;
}

.hint { color: var(--gray-500); font-size: 13px; margin: 0; }

/* ============ Editor / Upload header ============ */
.ed-header {
  background: #fff; border-bottom: 1px solid var(--gray-200);
  padding: 0 18px; height: 60px;
  display: grid; grid-template-columns: auto 1fr auto; gap: 12px; align-items: center;
  flex-shrink: 0;
}
.hbtn {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 8px 12px; border-radius: 8px; font-size: 14px; font-weight: 500;
  color: var(--gray-700); transition: background .15s, color .15s; white-space: nowrap;
}
.hbtn svg { width: 18px; height: 18px; flex-shrink: 0; }
.hbtn:hover { background: var(--gray-100); color: var(--gray-900); }
.hbtn.primary { background: var(--pink); color: #fff; padding: 8px 16px; }
.hbtn.primary:hover { background: var(--pink-dark); }
.hbtn.close-btn { padding: 8px 12px; }
.ed-actions { display: flex; gap: 4px; }
.ed-actions-center { justify-content: center; }

/* ============ Upload Screen ============ */
.upload-body {
  flex: 1; overflow-y: auto;
  padding: 32px 32px 56px;
  /* Центрируем по вертикали, чтобы не было пустоты снизу. safe — при длинном
     контенте (демозалы) ведёт себя как start, не обрезая верх. */
  display: flex; flex-direction: column; justify-content: safe center;
}
#upload-title {
  margin: 0 0 32px; font-size: 26px; font-weight: 600; letter-spacing: -0.2px;
  text-align: center;
}
/* Адаптивный layout. По-умолчанию 1 центрированная колонка (когда комнат нет —
   ephemeral mode скрывает .upload-col-right через [hidden]). Если правая колонка
   присутствует и не скрыта — переключаемся на 2 колонки. На <900px всегда стек.
   :has() — Safari 15.4+/Chrome 105+/Firefox 121+, в 2026 уже стандарт. */
.upload-cols {
  display: grid; gap: 32px;
  grid-template-columns: minmax(0, 480px);
  justify-content: center;
  max-width: 1400px; margin: 0 auto 48px;
}
.upload-cols:has(.upload-col-right:not([hidden])) {
  grid-template-columns: 320px 1fr;
  justify-content: start;
}
.upload-col-left { display: flex; flex-direction: column; gap: 12px; }
.dropzone-big {
  background: var(--pink); color: #fff; border-radius: 12px;
  height: 64px; display: flex; align-items: center; justify-content: center; gap: 12px;
  font-size: 17px; font-weight: 600; cursor: pointer; user-select: none;
  box-shadow: 0 2px 10px rgba(233,30,99,.25);
  transition: background .15s, transform .05s;
}
.dropzone-big:hover { background: var(--pink-dark); }
.dropzone-big:active { transform: scale(0.99); }
.dropzone-big.drag { background: var(--pink-dark); outline: 3px solid var(--pink-soft); }
.dz-icon-big { width: 24px; height: 24px; }

.qr-placeholder {
  background: #fff; border: 1px solid var(--gray-200); border-radius: 12px;
  padding: 14px 16px;
  display: flex; align-items: center; gap: 12px;
  color: var(--gray-700); font-size: 14px; line-height: 1.35;
}
.qr-icon { width: 32px; height: 32px; flex-shrink: 0; color: var(--gray-700); }
/* На телефоне QR «загрузить с телефона» не нужен — пользователь уже на телефоне. */
@media (max-width: 560px) { .qr-placeholder { display: none; } }

/* Правая колонка: пользовательские комнаты */
.col-title {
  font-size: 16px; font-weight: 600; color: var(--gray-900);
  margin-bottom: 14px;
}
.rooms-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 16px;
}
.room-card {
  position: relative; border-radius: var(--radius); overflow: hidden;
  background: #fff; box-shadow: var(--shadow-sm); aspect-ratio: 1; cursor: pointer;
  transition: box-shadow .15s, transform .05s;
}
.room-card:hover { box-shadow: var(--shadow-md); }
.room-card:active { transform: scale(0.99); }
.room-card img { width: 100%; height: 100%; object-fit: cover; display: block; }
.room-card .room-del {
  position: absolute; top: 8px; right: 8px;
  width: 32px; height: 32px; border-radius: 50%;
  background: rgba(40,40,40,0.7); color: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; opacity: 0; transition: opacity .15s, background .15s;
}
.room-card:hover .room-del { opacity: 1; }
.room-del:hover { background: rgba(0,0,0,0.85); }
.room-del svg { width: 16px; height: 16px; }

/* Демозалы */
.demos { max-width: 1400px; margin: 0 auto 32px; }
.demos-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px;
}
.demo-card {
  cursor: pointer; border-radius: var(--radius); overflow: hidden;
  background: #fff; box-shadow: var(--shadow-sm);
  transition: box-shadow .15s, transform .05s;
}
.demo-card:hover { box-shadow: var(--shadow-md); }
.demo-card:active { transform: scale(0.99); }
.demo-card img { width: 100%; height: 160px; object-fit: cover; display: block; }
.demo-card-name { padding: 12px 14px; font-size: 14px; color: var(--gray-900); }

/* ============ Loading overlay ============ */
#loading-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  width: 100vw; height: 100vh; height: 100dvh; background: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 16px; z-index: 9999;  /* выше всего, в т.ч. mobile bottom-sheet */
}
/* Roomvo-style loading: серая иконка-плейсхолдер с пробегающей розовой "сканирующей" полоской */
.loading-frame {
  position: relative; width: 120px; height: 96px;
  display: flex; align-items: center; justify-content: center;
}
.loading-frame-svg { width: 100%; height: 100%; display: block; }
.loading-bar {
  position: absolute; left: 8%; right: 8%;
  height: 3px; background: var(--pink); border-radius: 2px;
  box-shadow: 0 0 10px rgba(233, 30, 99, 0.55);
  animation: scanBar 1.8s ease-in-out infinite;
}
@keyframes scanBar {
  0%   { top: 12%; opacity: 0.85; }
  50%  { top: 78%; opacity: 1; }
  100% { top: 12%; opacity: 0.85; }
}
/* Контейнер фиксированной высоты — чтобы новый/старый текст не прыгали и
   не накладывались друг на друга при ротации (на iOS Safari composite layer
   при transition + смене textContent давал артефакты). */
.loading-text {
  position: relative;
  width: 100%; min-height: 24px;
  text-align: center;
}
.loading-text > span {
  position: absolute; left: 0; right: 0; top: 0;
  font-size: 17px; font-weight: 500; color: var(--gray-900);
  opacity: 0; transition: opacity .35s ease;
  will-change: opacity;
}
.loading-text > span.visible { opacity: 1; }
.loading-sub { color: var(--gray-500); font-size: 13px; max-width: 280px; text-align: center; }

/* ============ Editor body ============ */
.ed-body { display: flex; flex: 1; min-height: 0; }
#sidebar {
  width: 320px; flex-shrink: 0;
  background: #fff; border-right: 1px solid var(--gray-200);
  display: flex; flex-direction: column;
}
.sb-search {
  position: relative; padding: 14px 16px; border-bottom: 1px solid var(--gray-200);
}
.sb-search .search-icon {
  position: absolute; left: 26px; top: 50%; transform: translateY(-50%);
  width: 18px; height: 18px; color: var(--gray-500);
}
#search-input {
  width: 100%; padding: 9px 12px 9px 38px;
  border: 1px solid var(--gray-200); border-radius: 8px; font-size: 14px;
  background: var(--gray-100); transition: background .15s, border-color .15s;
}
#search-input:focus { outline: none; background: #fff; border-color: var(--gray-300); }

.catalog-list { flex: 1; overflow-y: auto; padding: 8px; }
.product-card {
  display: flex; gap: 12px; padding: 10px;
  border-radius: 8px; cursor: pointer; user-select: none;
  border: 1px solid transparent;
  transition: background .15s, border-color .15s;
}
.product-card:hover { background: var(--gray-100); }
.product-card.active { background: #fff; border-color: var(--pink); box-shadow: var(--shadow-sm); }
.product-card.active::after {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--pink);
  border-radius: 2px;
}
.pc-thumb {
  width: 64px; height: 64px; flex-shrink: 0; border-radius: 6px;
  background: var(--gray-100); object-fit: cover;
}
.pc-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
.pc-vendor { color: var(--gray-500); font-size: 12px; }
.pc-name { font-weight: 500; font-size: 14px; line-height: 1.25;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.pc-sizes-label { color: var(--gray-500); font-size: 11px; margin-top: 2px; }
.pc-chips {
  display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px;
}
.pc-chip {
  padding: 3px 7px; border-radius: 999px;
  background: var(--gray-100); border: 1px solid transparent;
  font-size: 11px; color: var(--gray-700);
  cursor: pointer; user-select: none;
}
.pc-chip.active { background: var(--pink); color: #fff; }
.pc-chip:hover:not(.active) { background: var(--gray-200); }

/* ============ Stage ============ */
#stage {
  flex: 1; display: flex; align-items: center; justify-content: center;
  background: #1d1d1f; padding: 24px; overflow: hidden;
}
#canvas-wrap {
  position: relative; border-radius: 8px; overflow: hidden;
  box-shadow: var(--shadow-lg);
  /* touch-action: none — на тач-устройствах одно-пальцевый drag должен крутить ковёр,
     а не скроллить страницу. Pinch-zoom тоже отключаем (наш own gesture system). */
  touch-action: none;
  -webkit-user-select: none; user-select: none;
}
#overlay { touch-action: none; }
#bg-img { position: relative; display: block; width: 100%; height: 100%; }
#fg-img, .rug-wrap, #overlay, .shading-overlay {
  position: absolute; top: 0; left: 0; width: 100%; height: 100%;
  pointer-events: none;
}
#overlay { overflow: visible; }
.rug-wrap { transform-origin: 0 0; overflow: hidden; }
.rug-wrap img { width: 100%; height: 100%; opacity: 1; pointer-events: none; display: block; }
/* Контактная тень — SVG-полилиния вдоль 2 рёбер у ближнего к камере угла.
   non-scaling-stroke даёт ровно N пикселей в screen-space независимо от
   перспективного warp'а matrix3d. drop-shadow для этого не подходил —
   локальный offset после warp'а растягивался разной перспективой на каждой
   стороне, давая неравномерную тень. Полилиния строится в render() в
   натуральных координатах кадра (см. #contact-shadow-svg). */
.contact-shadow {
  fill: none;
  stroke: rgba(0, 0, 0, 0.22);
  stroke-width: 2;
  stroke-linejoin: miter;
  stroke-linecap: butt;
  vector-effect: non-scaling-stroke;
  /* Лёгкое размытие — края тени становятся мягче, вид естественнее.
     0.6px на 2px stroke = соотношение норм для контактной тени. */
  filter: blur(0.6px);
}
.shading-overlay { mix-blend-mode: multiply; }

.rug-hit, .rot-handle { pointer-events: auto; }
#ring-poly { pointer-events: stroke; cursor: grab; }
#ring-poly:active { cursor: grabbing; }
.rot-handle {
  fill: var(--pink); stroke: #fff;
  stroke-width: 6; stroke-linejoin: round; paint-order: stroke fill;
  cursor: grab; filter: drop-shadow(0 2px 4px rgba(0,0,0,.5));
}
.rot-handle:active { cursor: grabbing; }
.rug-hit { fill: rgba(255,255,255,0.001); cursor: grab; }
.rug-hit:active { cursor: grabbing; }

/* ============ Editor footer (bottom toolbar) ============ */
.ed-footer {
  background: #fff; border-top: 1px solid var(--gray-200);
  height: 76px; padding: 0 24px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
}
.footer-product { display: flex; align-items: center; gap: 14px; min-width: 220px; }
.footer-thumb { width: 50px; height: 50px; border-radius: 8px; object-fit: cover;
  background: var(--gray-100); flex-shrink: 0; }
.footer-product-meta { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.footer-vendor { color: var(--gray-500); font-size: 12px; }
.footer-product-name { font-weight: 600; font-size: 14px; max-width: 220px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.footer-actions { display: flex; gap: 4px; }
.fbtn {
  display: inline-flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 14px; border-radius: 8px;
  font-size: 12px; color: var(--gray-700);
  transition: background .15s, color .15s;
}
.fbtn svg { width: 20px; height: 20px; }
.fbtn:hover { background: var(--gray-100); color: var(--gray-900); }

/* ============ Embed mode (iframe) ============ */
/* Embed: крестик не нужен (его даёт оверлей-контейнер), тулбар редактора прячем.
   На экране загрузки оставляем шапку с ЛОГОТИПОМ — брендинг как у Roomvo. */
body.embed .close-btn { display: none; }
body.embed #editor .ed-header,
body.embed .upload-header { display: none; }
body.embed #upload-screen .ed-header { justify-content: flex-start; }

/* ============ Mobile ============ */
@media (max-width: 900px) {
  /* На мобиле/планшете — стек, независимо от наличия правой колонки.
     Перебиваем :has()-правило по специфичности явным дублирующим селектором. */
  .upload-cols,
  .upload-cols:has(.upload-col-right:not([hidden])) {
    grid-template-columns: 1fr;
    justify-content: stretch;
  }
  #sidebar { width: 260px; }
  .ed-header { padding: 0 8px; height: 52px; }
  .hbtn span { display: none; }
  .hbtn.primary { padding: 8px 12px; }
  .hbtn.primary span { display: inline; }
  .ed-actions { gap: 0; }
  .ed-footer { height: 64px; padding: 0 8px; }
  .footer-product-meta { display: none; }
  .footer-product { min-width: auto; }
  .fbtn span { display: none; }
}

@media (max-width: 560px) {
  .upload-body { padding: 16px; padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
  #upload-title { font-size: 20px; margin-bottom: 16px; }
  .demos-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
  /* Safe-area для iPhone notch + home indicator */
  .ed-header { padding-top: env(safe-area-inset-top); height: calc(48px + env(safe-area-inset-top)); }
  .ed-footer { padding-bottom: env(safe-area-inset-bottom); height: calc(56px + env(safe-area-inset-bottom)); }
  #sidebar { padding-bottom: env(safe-area-inset-bottom); }
  /* Sidebar → bottom-sheet (slide-up при .open). 70dvh — динамическая высота
     (учитывает выезжающий URL bar Safari и т.д.) */
  #sidebar {
    position: fixed; left: 0; right: 0; bottom: 0;
    width: 100%; height: 70vh; max-height: 70vh;
    height: 70dvh; max-height: 70dvh;
    background: white; border-radius: 16px 16px 0 0;
    box-shadow: 0 -8px 24px rgba(0,0,0,0.18);
    transform: translateY(100%); transition: transform .25s ease;
    z-index: 100; display: flex; flex-direction: column;
  }
  #sidebar.open { transform: translateY(0); }
  #sidebar::before {
    content: ""; display: block; width: 40px; height: 4px;
    background: var(--gray-300); border-radius: 2px;
    margin: 8px auto 4px;
  }
  #sidebar-backdrop {
    position: fixed; inset: 0; background: rgba(0,0,0,0.4);
    z-index: 99; opacity: 0; pointer-events: none;
    transition: opacity .25s;
  }
  #sidebar-backdrop.visible { opacity: 1; pointer-events: auto; }
  /* На мобиле скрываем горизонтальный stage padding чтобы канвас был полнее */
  #stage { padding: 8px; }
  /* Header — компактнее */
  .ed-header { height: 48px; padding: 0 6px; }
  .hbtn { padding: 6px 8px; font-size: 12px; }
  .hbtn svg { width: 16px; height: 16px; }
  .hbtn.primary span { display: none; } /* "Добавить в корзину" → только иконка */
  /* Footer — туже, отдельно add-rug-btn-mobile */
  .ed-footer { height: 56px; gap: 4px; }
  .fbtn { min-width: 48px; padding: 4px; }
  .fbtn svg { width: 22px; height: 22px; }
}

/* ============ Multi-rug UI ============ */
.add-mode-hint {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%);
  background: #d63384; color: white; padding: 10px 18px; border-radius: 22px;
  font-size: 14px; font-weight: 500; z-index: 50; pointer-events: none;
  box-shadow: 0 4px 14px rgba(214, 51, 132, 0.35);
}
.fbtn.active {
  background: #fff0f6; border-color: #d63384; color: #d63384;
}
.fbtn.disabled, .fbtn:disabled {
  opacity: 0.45; cursor: not-allowed;
}

/* ===== Embed одного ковра (пришли с карточки товара, ?sku=) ===== */
/* Каталог не нужен — показываем только этот ковёр. #stage (flex:1) сам займёт место. */
body.embed-single #sidebar,
body.embed-single #sidebar-backdrop,
body.embed-single #size-btn { display: none !important; }

/* ======== iOS-style toggle (по/против ворса) ======== */
/* Mobile-first: tap-area 44px+ через ::before, компактный визуал. */
.fiber-toggle-wrap {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px; user-select: none;
}
.fiber-side {
  font-size: 12px; color: var(--gray-500);
  transition: color .2s, font-weight .2s;
  cursor: default;
}
.fiber-side.active { color: #d63384; font-weight: 600; }
.ios-switch {
  position: relative; width: 40px; height: 24px; flex-shrink: 0;
  background: var(--gray-300); border: none; border-radius: 12px;
  padding: 2px; cursor: pointer;
  transition: background .25s ease;
}
.ios-switch::before {
  /* Невидимая расширенная зона тапа для mobile (Apple HIG ≥44pt) */
  content: ''; position: absolute; inset: -12px -8px;
}
.ios-switch[aria-checked="true"] { background: #d63384; }
.ios-switch-thumb {
  display: flex; align-items: center; justify-content: center;
  width: 20px; height: 20px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25), 0 1px 1px rgba(0,0,0,0.1);
  transform: translateX(0);
  transition: transform .25s cubic-bezier(.4,1.4,.6,1);
}
.ios-switch[aria-checked="true"] .ios-switch-thumb { transform: translateX(16px); }
.fiber-icon { width: 12px; height: 12px; color: var(--gray-700); transition: transform .25s ease; }
.ios-switch[aria-checked="true"] .fiber-icon { color: #d63384; transform: rotate(60deg); }

@media (max-width: 560px) {
  .fiber-toggle-wrap { padding: 6px 6px; gap: 6px; }
  .fiber-side { font-size: 11px; }
}
.product-card.on-scene:not(.active) {
  outline: 2px solid #f8b7d4; outline-offset: -2px;
}
.product-card.on-scene:not(.active) .pc-thumb { opacity: 0.85; }

/* QR приём фото с телефона */
.qr-placeholder .qr-img { width: 132px; height: 132px; display: block; margin: 0 auto 8px; }
.phone-received { text-align: center; }
.phone-received img { max-width: 100%; border-radius: 10px; }
.phone-received p { color: #1a7f37; font-weight: 600; }
