/* Mini App craftgen.
 *
 * Раскладка как у нативного Telegram: фон страницы — secondary_bg, разделы —
 * карточки section_bg, заголовки разделов — section_header_text. Цвета берутся
 * из темы Telegram (--tg-*, ставит applyTheme в app.js); вне Telegram — свои,
 * светлая и тёмная схема.
 *
 * Превью прилипает к верху и сжимается (.stage.compact), пока крутят ручки:
 * каждая правка видна сразу. Цели касания — не меньше 44 px (WCAG 2.5.8 — 24).
 */
:root {
  --page: #f3eee6;          /* фон страницы (secondary_bg) */
  --card: #fffdf9;          /* карточка раздела (section_bg) */
  --text: #2b2724;
  --muted: #7a7169;         /* hint / subtitle */
  --head: #8a6a4f;          /* section_header_text */
  --accent: #b5652f;        /* button */
  --accent-text: #ffffff;   /* button_text */
  --accent-ink: #9a521f;    /* accent_text: ссылки, значения в заголовках */
  --line: #e6ddd0;          /* section_separator */
  --danger: #b3261e;        /* destructive_text */
  --ok: #2e7d32;
  --stage-bg: #ece5da;
  --radius: 16px;
  --gutter: 16px;
  --tap: 44px;
  --stage-h: clamp(240px, 44vh, 440px);
  --stage-h-compact: clamp(150px, 24vh, 220px);
  --safe-bottom: max(env(safe-area-inset-bottom), var(--tg-safe-area-inset-bottom, 0px));
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --page: #161412;
    --card: #221f1c;
    --text: #efe9e1;
    --muted: #a39a90;
    --head: #c9a27f;
    --accent: #e08a4f;
    --accent-text: #1b1917;
    --accent-ink: #f0a36f;
    --line: #34302b;
    --danger: #f2877f;
    --ok: #7bc47f;
    --stage-bg: #1d1a18;
  }
}
/* В Telegram — только его тема (applyTheme ставит --tg-* из themeParams) */
:root[data-tg] {
  --page: var(--tg-secondary-bg, #f3eee6);
  --card: var(--tg-section-bg, var(--tg-bg, #fffdf9));
  --text: var(--tg-text, #2b2724);
  --muted: var(--tg-subtitle, var(--tg-hint, #7a7169));
  --head: var(--tg-section-header, var(--tg-hint, #8a6a4f));
  --accent: var(--tg-button, #b5652f);
  --accent-text: var(--tg-button-text, #ffffff);
  --accent-ink: var(--tg-accent-text, var(--tg-link, #9a521f));
  --line: var(--tg-separator, color-mix(in srgb, var(--muted) 25%, transparent));
  --danger: var(--tg-destructive, #b3261e);
  --stage-bg: color-mix(in srgb, var(--page) 70%, var(--card));
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--page); color: var(--text); }
/* сдвиг от сжатия превью компенсирует setupCompactStage сама: автоподстройка
   Chrome (scroll anchoring) сложилась бы с ней, а в Safari её нет вовсе */
html, body { overflow-anchor: none; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
main { max-width: 640px; margin: 0 auto; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ------------------------------------------------------------------ вид изделия */
.kinds-bar { padding: 10px var(--gutter) 8px; background: var(--page); }
.kinds { gap: 6px; }
.kinds .chip { font-weight: 600; }

/* ------------------------------------------------------------------ превью */
.stage {
  position: sticky; top: 0; z-index: 3;
  height: var(--stage-h);
  background: radial-gradient(ellipse at 50% 38%, var(--card), var(--stage-bg) 72%);
  border-bottom: 1px solid var(--line);
  /* без перехода высоты: setupCompactStage меряет сдвиг сразу после смены класса */
}
.stage.compact { height: var(--stage-h-compact); box-shadow: 0 6px 14px -10px rgb(0 0 0 / 35%); }
/* Контраст под изделием: светлое (белое мыло, віск слонової кістки, прозора основа) —
   на тёмном фоне; тёмное в тёмной теме — на светлом. Класс ставит applyLook(). */
.stage.on-dark { background: radial-gradient(ellipse at 50% 38%, #6f6a66, #3b3734 72%); }
.stage.on-light { background: radial-gradient(ellipse at 50% 38%, #fbf8f3, #d9d2c7 72%); }
.stage.on-dark .chip.glass, .stage.on-dark .hint { color: #f4efe8; }
#stage-sentinel { height: 1px; margin-top: -1px; }
/* вертикальный жест прокручивает страницу, горизонтальный — крутит модель */
#view { width: 100%; height: 100%; display: block; touch-action: pan-y; transition: opacity .2s; }
.stage.building #view { opacity: .55; }
.stage-top {
  position: absolute; top: 10px; left: var(--gutter); right: var(--gutter);
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  pointer-events: none;
}
.stage-top > * { pointer-events: auto; }
.hint {
  position: absolute; bottom: 8px; left: 0; right: 0; width: max-content; max-width: 90%;
  margin: 0 auto; padding: 4px 10px; border-radius: 999px; font-size: 12px; color: var(--muted);
  background: color-mix(in srgb, var(--card) 80%, transparent); pointer-events: none;
  transition: opacity .4s;
}
.hint.gone { opacity: 0; }
.stage.compact .hint { display: none; }
.spinner {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  display: flex; align-items: center; gap: 8px; max-width: 86%;
  padding: 8px 14px; border-radius: 999px; background: var(--card); color: var(--text);
  font-size: 13px; box-shadow: 0 2px 10px rgb(0 0 0 / 15%);
}
.spinner::before {
  content: ""; flex: none; width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.badge {
  padding: 5px 10px; border-radius: 999px; font-size: 12px; color: var(--muted);
  background: color-mix(in srgb, var(--card) 88%, transparent); border: 1px solid var(--line);
}
.badge.ok { color: var(--ok); border-color: currentColor; }
.chip.glass { background: color-mix(in srgb, var(--card) 85%, transparent); backdrop-filter: blur(6px); }

/* ------------------------------------------------------------------ панель */
.panel { padding: 12px 12px calc(16px + var(--safe-bottom)); }
.card {
  background: var(--card); border-radius: var(--radius);
  padding: 14px var(--gutter); margin: 0 0 12px;
}
.card .sub + .sub { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--line); }
.card-title {
  display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap;
  margin: 0 0 10px; font-size: 14px; font-weight: 600; color: var(--head);
}
.h-val { font-weight: 500; color: var(--text); }
.h-val:not(:empty)::before { content: "· "; color: var(--muted); }
.muted { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.row .card-title { margin: 0; }

/* ------------------------------------------------------------------ чипы */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips.scroll {
  flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; overscroll-behavior-x: contain;
  scroll-snap-type: x proximity; scroll-padding: 0 var(--gutter);
  margin: 0 calc(-1 * var(--gutter)); padding: 2px var(--gutter);
  /* край затухает: видно, что лента продолжается */
  -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 12px, #000 calc(100% - 28px), transparent);
  mask-image: linear-gradient(90deg, transparent 0, #000 12px, #000 calc(100% - 28px), transparent);
}
.chips.scroll::-webkit-scrollbar { display: none; }
.chips.scroll .chip { scroll-snap-align: start; }
.chip {
  flex: none; min-height: var(--tap); display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--line); background: var(--page); color: var(--text);
  border-radius: 999px; padding: 0 16px; cursor: pointer; line-height: 1.15; text-align: center;
  transition: background-color .15s, border-color .15s, color .15s;
}
.chip[aria-checked="true"], .chip[aria-pressed="true"] {
  background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600;
}
/* сегменты (тип рельефа, режим): равные доли строки; не влезли по самому длинному слову
   (320 px, «Логотип + напис») — переносятся второй строкой, а не вылезают за экран */
.chips.seg { flex-wrap: wrap; }
.chips.seg .chip { flex: 1 1 0; padding: 0 8px; }

/* ------------------------------------------------------------------ цвет */
.swatches { display: grid; grid-template-columns: repeat(auto-fill, minmax(var(--tap), 1fr)); gap: 10px; }
.swatch {
  position: relative; aspect-ratio: 1; min-width: var(--tap); border-radius: 50%; cursor: pointer;
  border: 1px solid color-mix(in srgb, var(--text) 18%, transparent); padding: 0;
}
.swatch[aria-checked="true"] { box-shadow: 0 0 0 3px var(--card), 0 0 0 5px var(--accent); }
/* выбранный — не только цветом (WCAG 1.4.1): галочка с тенью видна на любом фоне */
.swatch[aria-checked="true"]::after {
  content: "✓"; position: absolute; inset: 0; display: grid; place-items: center;
  color: #fff; font-size: 18px; font-weight: 700; text-shadow: 0 0 3px rgb(0 0 0 / 85%);
}

/* ------------------------------------------------------------------ ручки */
.knobs:not(:empty) { margin-top: 6px; }
.knob { margin: 12px 0 0; }
.knob-head { display: flex; justify-content: space-between; align-items: baseline; font-size: 14px; }
.knob-head output { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--accent-ink); }
.knob-row { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.knob-row input { flex: 1; min-width: 0; }
.step {
  flex: none; width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--page); font-size: 18px; line-height: 1; cursor: pointer;
}
.step:disabled { opacity: .35; cursor: default; }
input[type="range"] {
  -webkit-appearance: none; appearance: none; height: var(--tap); background: transparent; margin: 0;
  accent-color: var(--accent);
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 6px; border-radius: 3px;
  background: linear-gradient(90deg, var(--accent) var(--fill, 50%), var(--line) var(--fill, 50%));
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 26px; height: 26px; margin-top: -10px; border-radius: 50%;
  background: var(--card); border: 2px solid var(--accent); box-shadow: 0 1px 4px rgb(0 0 0 / 25%);
}
input[type="range"]::-moz-range-track { height: 6px; border-radius: 3px; background: var(--line); }
input[type="range"]::-moz-range-progress { height: 6px; border-radius: 3px; background: var(--accent); }
input[type="range"]::-moz-range-thumb {
  width: 24px; height: 24px; border-radius: 50%; background: var(--card); border: 2px solid var(--accent);
}

/* ------------------------------------------------------------------ напис / логотип (P5) */
.decor-box { margin: 12px 0 0; }
.decor-modes { margin-top: 12px; }
.field-label { display: block; font-size: 14px; margin: 0 0 6px; }
.field {
  width: 100%; resize: none; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--page); color: var(--text);
  font: inherit; font-size: 16px; line-height: 1.35;   /* 16px — iOS не увеличивает страницу */
}
.field:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.field-err { color: var(--danger); font-size: 13px; margin: 6px 0 0; }
.note { font-size: 13px; margin: 8px 0 0; padding: 8px 10px; border-radius: 10px;
        background: var(--page); border-left: 3px solid var(--accent); }
.logo-row { display: flex; align-items: center; gap: 12px; }
.logo-preview { width: 64px; height: 64px; object-fit: contain; padding: 6px;
                border-radius: 10px; background: #fff; border: 1px solid var(--line); }

/* ------------------------------------------------------------------ силует (вирубки) */
/* спрайт outlines.svg: символи для <use>, сам не видно */
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
/* картинка силуету: тіло — колір тексту, візерунок (деталі штампа) — колір картки */
.o-body { fill: var(--o-fill, currentColor); }
.o-det { fill: var(--o-det, var(--card)); }
.o-ico { display: block; width: 100%; height: 100%; color: var(--text); }
.outline-pick { margin-top: 12px; }
.outline-current {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 64px; padding: 8px 12px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--page); cursor: pointer;
  text-align: left;
}
.outline-current .o-ico { flex: none; width: 48px; height: 48px; }
.outline-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.outline-label { font-size: 12px; color: var(--muted); }
.outline-change { flex: none; color: var(--accent-ink); font-weight: 600; font-size: 14px; }

html.sheet-open, html.sheet-open body { overflow: hidden; }
.sheet { position: fixed; inset: 0; z-index: 9; background: color-mix(in srgb, #000 35%, transparent);
         display: flex; align-items: flex-end; justify-content: center; }
.sheet-card {
  width: 100%; max-width: 640px; height: min(88vh, 760px); display: flex; flex-direction: column;
  gap: 10px; background: var(--card); color: var(--text); border-radius: 20px 20px 0 0;
  padding: 14px var(--gutter) 0; box-shadow: 0 -4px 20px rgb(0 0 0 / 20%);
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sheet-head h2 { margin: 0; font-size: 17px; }
.sheet-head .link { margin: 0; }
.sheet .field { flex: none; }
.sheet .chips.scroll { flex: none; }
.sheet-body { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
              padding-bottom: calc(16px + var(--safe-bottom)); margin: 0 calc(-1 * var(--gutter));
              padding-left: var(--gutter); padding-right: var(--gutter); }
.sheet-body > .muted:first-child { margin: 0 0 10px; }
.o-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; }
.o-tile {
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 4px 6px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--page); cursor: pointer;
  min-height: var(--tap);
}
.o-tile .o-ico { width: 56px; height: 56px; }
.o-tile span {
  font-size: 12px; line-height: 1.2; text-align: center; overflow: hidden;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
}
.o-tile[aria-checked="true"] { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset;
                               font-weight: 600; }
.o-tile[aria-checked="true"] .o-ico { color: var(--accent); }

/* ------------------------------------------------------------------ заказ */
.stepper { display: flex; align-items: center; gap: 2px; }
.stepper button {
  width: var(--tap); height: var(--tap); border-radius: 50%; border: 1px solid var(--line);
  background: var(--page); font-size: 20px; cursor: pointer;
}
.stepper output { min-width: 36px; text-align: center; font-weight: 600; font-variant-numeric: tabular-nums; }
.facts:not(:empty) {
  display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 14px 0 0;
  padding-top: 12px; border-top: 1px solid var(--line); font-size: 14px;
}
.facts dt { color: var(--muted); }
.facts dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.changes { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.changes h3 { margin: 0 0 6px; font-size: 13px; font-weight: 600; color: var(--head); }
.changes ul { margin: 0; padding-left: 18px; font-size: 14px; }
.changes li { margin: 2px 0; }

.notice { padding: 10px 12px; margin: 0 0 12px; border-radius: 12px; background: var(--card);
          border-left: 3px solid var(--danger); font-size: 14px; }
.notice.info { border-left-color: var(--accent); }

/* своя кнопка — только вне Telegram (там MainButton); прилипает к низу экрана */
.actions {
  position: sticky; bottom: 0; z-index: 2; margin: 0 -12px; padding: 10px 12px calc(10px + var(--safe-bottom));
  background: linear-gradient(transparent, var(--page) 18px);
}
[data-tg-main] .actions { display: none; }
.primary {
  display: block; width: 100%; min-height: 50px; padding: 0 16px; border: 0;
  border-radius: 12px; background: var(--accent); color: var(--accent-text);
  font-weight: 600; font-size: 16px; cursor: pointer;
}
.primary:disabled { opacity: .45; cursor: default; }
.link { display: block; min-height: var(--tap); margin: 4px auto 0; background: none; border: 0;
        color: var(--accent-ink); cursor: pointer; }

/* ------------------------------------------------------------------ согласие */
.consent { position: fixed; inset: 0; z-index: 10; background: color-mix(in srgb, #000 35%, transparent);
           display: flex; align-items: flex-end; justify-content: center; overflow-y: auto; }
.consent-card { width: 100%; max-width: 640px; background: var(--card); color: var(--text);
                border-radius: 20px 20px 0 0; padding: 20px var(--gutter) calc(18px + var(--safe-bottom));
                box-shadow: 0 -4px 20px rgb(0 0 0 / 20%); }
.consent-card h2 { margin: 0 0 10px; font-size: 18px; }
.consent-text { white-space: pre-line; font-size: 14px; max-height: 55vh; overflow-y: auto; margin: 0 0 12px; }

/* ------------------------------------------------------------------ движение */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation-duration: .01ms !important; }
  .chips.scroll { scroll-behavior: auto; }
}
