/* tryon.css — styles of the frame try-on widget. All classes start with "to-".
   Colours and sizes come from the page's CSS variables (with fallbacks).
   Layout knobs: --to-cols, --to-gap, --to-stage-min, --to-stage-ratio, --tryon-sticky-top. */

[data-tryon] {
  --to-css: 1;
  container-type: inline-size;
  container-name: tryon;
  display: block;
  width: 100%;
}

/* ---------- before the script runs ----------
   The page's intro stays visible, the first demo portrait sits where the photo will be,
   and an empty block holds the controls' place so the page below does not jump. */
[data-tryon]:not(.to-root) {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-auto-columns: minmax(0, 1fr);
  column-gap: var(--to-gap, 24px);
  row-gap: 16px;
  align-items: start;
}
[data-tryon]:not(.to-root) > img,
[data-tryon]:not(.to-root) > picture > img {
  display: block; width: 100%; height: auto; aspect-ratio: var(--to-stage-ratio, 4 / 5); object-fit: cover;
  border-radius: var(--r-lg, 20px); background: var(--surface, #f4f1ea);
}
[data-tryon]:not(.to-root) > img ~ img,
[data-tryon]:not(.to-root) > picture ~ picture { display: none; }
[data-tryon]:not(.to-root)::after { content: ""; display: block; height: 600px; }
@container tryon (max-width: 300px) { [data-tryon]:not(.to-root)::after { height: 740px; } }
@container tryon (min-width: 560px) { [data-tryon]:not(.to-root)::after { height: 470px; } }
@container tryon (min-width: 640px) {
  [data-tryon]:not(.to-root) > [data-tryon-intro] { grid-column: 1; grid-row: 1; }
  [data-tryon]:not(.to-root)::after { grid-column: 1; grid-row: 2; height: 590px; }
  [data-tryon]:not(.to-root) > img:first-of-type,
  [data-tryon]:not(.to-root) > picture:first-of-type { grid-column: 2; grid-row: 1 / span 2; align-self: stretch; }
  [data-tryon]:not(.to-root) > img:first-of-type,
  [data-tryon]:not(.to-root) > picture:first-of-type > img { height: auto; min-height: var(--to-stage-min, 420px); aspect-ratio: auto; contain: size; }
}
@container tryon (min-width: 1140px) { [data-tryon]:not(.to-root)::after { height: 440px; } }

/* ---------- mounted ---------- */

.to-root {
  color: var(--ink, #101618);
  font-family: var(--font-text, "Libre Franklin", system-ui, sans-serif);
  font-size: 15px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
}
.to-root *, .to-root *::before, .to-root *::after { box-sizing: border-box; }
:where(.to-root) :where(button, input) { font: inherit; color: inherit; margin: 0; }
.to-root [hidden] { display: none !important; }
:where(.to-root) svg { display: block; }

/* One column (narrower than 640 px): intro, photo, controls — the panel box dissolves
   (display: contents) so the photo can sit between the intro and the controls. */
.to-layout { display: grid; grid-template-columns: minmax(0, 1fr); row-gap: 16px; align-items: start; }
.to-panel { display: contents; }
.to-panel > [data-tryon-intro] { order: 0; }
.to-stage { order: 1; }
.to-controls { order: 2; }

/* Two columns from 640 px: panel left, photo right, the photo as tall as the panel. */
@container tryon (min-width: 640px) {
  .to-layout { grid-template-columns: var(--to-cols, minmax(0, 1fr) minmax(0, 1fr)); gap: var(--to-gap, 24px); align-items: stretch; }
  .to-panel { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
  .to-root .to-stage { aspect-ratio: auto; height: 100%; min-height: var(--to-stage-min, 420px); }
}
@supports not (container-type: inline-size) {
  @media (min-width: 700px) {
    .to-layout { grid-template-columns: var(--to-cols, minmax(0, 1fr) minmax(0, 1fr)); gap: var(--to-gap, 24px); align-items: stretch; }
    .to-panel { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
    .to-root .to-stage { aspect-ratio: auto; height: 100%; min-height: var(--to-stage-min, 420px); }
  }
}

/* ---------- stage ---------- */

.to-stage {
  position: relative;
  width: 100%;
  aspect-ratio: var(--to-stage-ratio, 4 / 5);
  overflow: hidden;
  border-radius: var(--r-lg, 20px);
  background: var(--surface, #f4f1ea);
  isolation: isolate;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
/* one column: the photo stays on screen while the visitor scrolls the options.
   A page with a fixed header sets --tryon-sticky-top to the header height. */
@container tryon (max-width: 639.98px) {
  .to-stage { position: sticky; top: var(--tryon-sticky-top, 0px); z-index: 2; }
}
.to-photo { position: absolute; inset: 0; }
.to-root .to-photo-img {
  position: absolute; left: 0; top: 0; display: block;
  max-width: none; max-height: none; transform-origin: 0 0;
  border-radius: 0; pointer-events: none;
}
.to-root .to-photo-img.to-anim { transition: transform .35s cubic-bezier(.2, .7, .2, 1); }
.to-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; transform: scaleX(-1); background: var(--ink, #101618); }
.to-overlay {
  position: absolute; inset: 0; width: 100%; height: 100%;
  overflow: visible; pointer-events: none;
  transition: opacity .3s ease .3s, visibility 0s linear .3s;
}
.to-root[data-state="loading"] .to-overlay,
.to-root[data-state="camera"] .to-overlay { opacity: 0; visibility: hidden; transition: none; }

/* drag handle: an axis-aligned box around the frame (only the SVG frame rotates) */
.to-handle {
  position: absolute; left: 0; top: 0;
  border-radius: var(--r-md, 12px);
  cursor: grab;
  touch-action: none;
}
.to-stage.to-dragging, .to-stage.to-dragging .to-handle { cursor: grabbing; }

.to-status {
  position: absolute; z-index: 3; top: 12px; left: 12px; right: 12px;
  display: flex; align-items: center; gap: 10px;
  min-height: 44px; padding: 4px 4px 4px 14px;
  border-radius: var(--r-md, 12px);
  background: var(--bg, #ffffff);
  background: color-mix(in srgb, var(--bg, #ffffff) 94%, transparent);
  color: var(--ink, #101618);
  font-size: 14px; line-height: 1.3;
}
.to-status-text { flex: 1; padding: 8px 0; }
.to-status.to-warn { border: 1px solid var(--line-strong, rgba(16, 22, 24, .2)); }
.to-spinner { display: none; flex: none; width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--line-strong, rgba(16, 22, 24, .2)); border-top-color: var(--action, #00787a); animation: to-spin .9s linear infinite; }
.to-status.to-busy .to-spinner { display: block; }
.to-status.to-busy { padding-right: 14px; }
.to-status-close {
  flex: none; display: grid; place-items: center;
  width: 44px; height: 44px; padding: 0;
  border: 0; border-radius: var(--r-btn, 8px); background: transparent; cursor: pointer;
}
.to-status-close svg { width: 18px; height: 18px; }
.to-status-close:hover { background: var(--surface, #f4f1ea); }
@keyframes to-spin { to { transform: rotate(360deg); } }

.to-models { position: absolute; z-index: 2; left: 12px; bottom: 12px; display: flex; gap: 8px; }
.to-model {
  width: 48px; height: 48px; padding: 0; overflow: hidden;
  border: 2px solid var(--bg, #ffffff); border-radius: 50%;
  background: var(--surface, #f4f1ea); cursor: pointer;
}
.to-model img { display: block; width: 100%; height: 100%; object-fit: cover; }
.to-model[aria-pressed="true"] { border-color: var(--action, #00787a); outline: 2px solid var(--bg, #ffffff); outline-offset: 0; }

.to-credit {
  position: absolute; z-index: 1; right: 10px; bottom: 8px; max-width: 60%; margin: 0;
  padding: 2px 6px; border-radius: var(--r-sm, 8px);
  background: color-mix(in srgb, var(--ink, #101618) 55%, transparent);
  color: var(--bg, #ffffff);
  font-size: 11px; line-height: 1.3; text-align: right;
}
.to-root[data-state="loading"] .to-credit, .to-root[data-state="camera"] .to-credit,
.to-root[data-state="camera"] .to-models { display: none; }

.to-cam-bar {
  position: absolute; z-index: 3; left: 0; right: 0; bottom: 16px;
  display: flex; align-items: center; justify-content: center; gap: 16px;
}
.to-shutter {
  display: grid; place-items: center;
  width: 68px; height: 68px; padding: 0;
  border: 4px solid var(--bg, #ffffff); border-radius: 50%;
  background: transparent; cursor: pointer;
}
.to-shutter span { width: 50px; height: 50px; border-radius: 50%; background: var(--bg, #ffffff); }
.to-shutter:active span { transform: scale(.92); }
.to-cam-bar .to-cam-cancel { position: absolute; left: 16px; }

/* ---------- controls ---------- */

.to-controls {
  container-type: inline-size;
  container-name: tocontrols;
  display: flex; flex-direction: column; gap: 16px;
  min-width: 0;
}
/* narrow: «Загрузить своё фото» on its own row, «Сделать селфи» and «Сохранить фото» share the next one */
.to-actions { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 8px; }
.to-actions .to-btn { min-width: 0; padding-left: 10px; padding-right: 10px; gap: 6px; font-size: 14px; }
.to-actions .to-act-upload, .to-actions:not(.to-has-cam) .to-act-save { grid-column: 1 / -1; }
@container tocontrols (max-width: 399.98px) {
  .to-actions .to-act-selfie, .to-actions .to-act-save { padding-left: 8px; padding-right: 8px; font-size: 13px; }
}
.to-privacy { margin: -8px 0 0; color: var(--muted, #676c6e); font-size: 13px; line-height: 1.35; text-wrap: pretty; }

.to-group { min-width: 0; margin: 0; padding: 0; border: 0; }
.to-legend {
  display: block; float: none; width: 100%;
  margin: 0 0 6px; padding: 0;
  font-size: 13px; font-weight: 600; line-height: 1.3; letter-spacing: .01em; color: var(--ink, #101618);
}

.to-shapes { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
.to-opt, .to-swatch, .to-seg { position: relative; display: block; cursor: pointer; }
.to-opt-box {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 5px;
  min-height: 64px; height: 100%; padding: 8px 5px;
  border: 1px solid var(--line, rgba(16, 22, 24, .11));
  border-radius: var(--r-md, 12px);
  background: var(--bg, #ffffff);
  color: var(--ink, #101618);
  transition: border-color .15s ease, background-color .15s ease;
}
.to-opt-box svg { width: 100%; max-width: 60px; height: 22px; }
.to-opt-label { font-size: 12px; line-height: 1.2; text-align: center; overflow-wrap: break-word; hyphens: manual; }
.to-opt:hover .to-opt-box { border-color: var(--line-strong, rgba(16, 22, 24, .2)); }
.to-opt input:checked + .to-opt-box {
  padding: 7px 4px;
  border: 2px solid var(--action, #00787a);
  background: var(--surface, #f4f1ea);
}

.to-colors { display: flex; flex-wrap: wrap; gap: 4px 6px; }
.to-swatch { display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 46px; }
.to-swatch-box {
  display: grid; place-items: center;
  width: 46px; height: 46px; padding: 4px;
  border: 2px solid transparent; border-radius: 50%;
  transition: border-color .15s ease;
}
.to-swatch-box svg { width: 34px; height: 34px; }
.to-swatch-label { font-size: 12px; line-height: 1.25; color: var(--muted, #676c6e); white-space: nowrap; }
.to-swatch:hover .to-swatch-box { border-color: var(--line-strong, rgba(16, 22, 24, .2)); }
.to-swatch input:checked + .to-swatch-box { border-color: var(--action, #00787a); }
.to-swatch input:checked ~ .to-swatch-label { color: var(--ink, #101618); }

.to-lenses { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.to-seg-box {
  display: flex; align-items: center; justify-content: center;
  min-height: 44px; height: 100%; padding: 6px 8px;
  border: 1px solid var(--line-strong, rgba(16, 22, 24, .2));
  border-radius: var(--r-btn, 8px);
  background: var(--bg, #ffffff);
  font-size: 13px; line-height: 1.15; text-align: center; overflow-wrap: break-word; hyphens: manual;
  transition: background-color .15s ease, border-color .15s ease, color .15s ease;
}
.to-seg:hover .to-seg-box { background: var(--surface, #f4f1ea); }
.to-seg input:checked + .to-seg-box { background: var(--action, #00787a); border-color: var(--action, #00787a); color: var(--action-ink, #ffffff); }

.to-adjust-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.to-icon-btn {
  display: grid; place-items: center; flex: none;
  width: 44px; height: var(--btn-h, 44px); min-height: 44px; padding: 0;
  border: 1px solid var(--line-strong, rgba(16, 22, 24, .2));
  border-radius: var(--r-btn, 8px);
  background: var(--bg, #ffffff); color: var(--ink, #101618);
  cursor: pointer;
  transition: background-color .15s ease;
}
.to-icon-btn svg { width: 20px; height: 20px; }
.to-icon-btn:hover { background: var(--surface, #f4f1ea); }
.to-icon-btn:active { background: var(--line, rgba(16, 22, 24, .11)); }
.to-range { flex: 1 1 96px; min-width: 88px; display: flex; align-items: center; gap: 8px; }
.to-range-label { flex: none; font-size: 12px; color: var(--muted, #676c6e); }
.to-range input {
  flex: 1; min-width: 0; height: 44px; margin: 0; padding: 0;
  -webkit-appearance: none; appearance: none; background: transparent; cursor: pointer;
}
.to-range input::-webkit-slider-runnable-track {
  height: 4px; border-radius: 2px;
  background: linear-gradient(var(--line-strong, rgba(16, 22, 24, .2)), var(--line-strong, rgba(16, 22, 24, .2))) center / 2px 12px no-repeat, var(--line, rgba(16, 22, 24, .11));
}
.to-range input::-moz-range-track { height: 4px; border-radius: 2px; background: var(--line, rgba(16, 22, 24, .11)); }
.to-range input::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 24px; height: 24px; margin-top: -10px; border-radius: 50%;
  border: 3px solid var(--bg, #ffffff); background: var(--action, #00787a);
  outline: 1px solid var(--line-strong, rgba(16, 22, 24, .2));
}
.to-range input::-moz-range-thumb { width: 18px; height: 18px; border-radius: 50%; border: 3px solid var(--bg, #ffffff); background: var(--action, #00787a); }
.to-range input:focus-visible { outline: 3px solid var(--action, #00787a); outline-offset: 2px; border-radius: var(--r-sm, 8px); }
.to-adjust-row .to-btn-ghost { padding: 8px; font-size: 14px; }

/* compact panel (560 px and wider): eight shapes in a row, all actions in one row */
@container tocontrols (min-width: 560px) {
  .to-actions { display: flex; flex-wrap: nowrap; }
  .to-actions .to-btn { flex: 1 1 auto; padding-left: 14px; padding-right: 14px; gap: 8px; }
  .to-shapes { grid-template-columns: repeat(8, minmax(0, 1fr)); }
  .to-opt-box { min-height: 66px; padding: 7px 3px; gap: 4px; }
  .to-opt input:checked + .to-opt-box { padding: 6px 2px; }
  .to-opt-label { font-size: 11px; }
}
/* narrow panel: «Сбросить» shows its icon only (the name stays in aria-label), the tilt slider keeps room */
@container tocontrols (max-width: 379.98px) {
  .to-adjust-row .to-btn-ghost { width: 44px; padding: 0; }
  .to-adjust-row .to-btn-ghost span { display: none; }
}
/* very narrow: three shapes per row, smaller lens labels, so words never break mid-letter */
@container tocontrols (max-width: 300px) {
  .to-shapes { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .to-seg-box { padding: 6px 4px; font-size: 12px; }
}

/* ---------- buttons ---------- */

.to-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--btn-h, 44px); padding: 10px 18px;
  border: 1px solid transparent; border-radius: var(--r-btn, 8px);
  font-family: var(--font-text, "Libre Franklin", system-ui, sans-serif);
  font-size: 15px; font-weight: 600; line-height: 1.2; text-align: center; text-decoration: none;
  cursor: pointer;
  transition: background-color .15s ease, border-color .15s ease, color .15s ease;
}
.to-btn svg { flex: none; width: 20px; height: 20px; }
.to-btn-primary { background: var(--action, #00787a); color: var(--action-ink, #ffffff); }
.to-btn-primary:hover { background: color-mix(in srgb, var(--action, #00787a) 86%, var(--ink, #101618)); }
.to-btn-primary:active { background: color-mix(in srgb, var(--action, #00787a) 74%, var(--ink, #101618)); }
.to-btn-secondary { background: var(--bg, #ffffff); color: var(--ink, #101618); border-color: var(--line-strong, rgba(16, 22, 24, .2)); }
.to-btn-secondary:hover { background: var(--surface, #f4f1ea); }
.to-btn-secondary:active { background: var(--line, rgba(16, 22, 24, .11)); }
.to-btn-ghost { background: transparent; color: var(--ink, #101618); padding: 10px 12px; }
.to-btn-ghost:hover { background: var(--surface, #f4f1ea); }
.to-cta { color: var(--action-ink, #ffffff); }

/* ---------- focus & helpers ---------- */

.to-root :focus-visible { outline: 3px solid var(--action, #00787a); outline-offset: 2px; }
.to-root .to-handle:focus-visible {
  outline: 2px dashed var(--bg, #ffffff);
  outline-offset: 0;
  background: color-mix(in srgb, var(--action, #00787a) 14%, transparent);
}
.to-opt input:focus-visible + .to-opt-box,
.to-swatch input:focus-visible + .to-swatch-box,
.to-seg input:focus-visible + .to-seg-box { outline: 3px solid var(--action, #00787a); outline-offset: 2px; }
.to-sr {
  position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
}

@media (prefers-reduced-motion: reduce) {
  .to-root *, .to-root *::before, .to-root *::after { transition: none !important; animation: none !important; }
  .to-spinner { border-top-color: var(--line-strong, rgba(16, 22, 24, .2)); background: var(--action, #00787a); transform: scale(.55); }
}
