/* ═══════════════════════════════════════════════════════════════════
   IZDAT AI Constructor — redesign v1
   ═══════════════════════════════════════════════════════════════════ */

:root {
  --bg:       #f4f4f1;
  --ink:      #171717;
  --muted:    #777;
  --line:     #deded8;
  --card:     #ffffff;
  --acid:     #cae808;
  --red:      #ff264a;
  --shadow:   0 18px 50px rgba(0,0,0,.10);
  --radius:   28px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Inter', Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

/* ── Shell ─────────────────────────────────────────────────────────── */
.shell {
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* ── Topbar ────────────────────────────────────────────────────────── */
.topbar {
  height: 56px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  z-index: 10;
}

.brand {
  display: inline-flex;
  align-items: center;
  height: 36px;
  text-decoration: none;
  flex-shrink: 0;
}

.brand-logo {
  display: block;
  width: auto;
  height: 34px;
  max-width: 150px;
}

.top-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

.btn-topbar-action {
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 16px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s, transform .15s;
}

.btn-topbar-action:hover {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
  transform: translateY(-1px);
}

.btn-topbar-action.active {
  background: var(--acid);
  border-color: var(--acid);
  color: var(--ink);
}

.pill {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 16px;
  background: #fff;
  font-weight: 700;
  font-size: 13px;
  white-space: nowrap;
  color: var(--muted);
}

.pill strong { color: var(--ink); }

/* Кнопка-корзина в шапке + бейдж количества */
.cart-pill {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: var(--ink);
  transition: border-color .15s ease, box-shadow .15s ease, transform .05s ease;
}
.cart-pill:hover { border-color: var(--ink); box-shadow: 0 2px 10px rgba(0,0,0,.08); }
.cart-pill:active { transform: translateY(1px); }
.cart-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: #e23c3c;
  color: #fff;
  font-size: 12px;
  font-weight: 800;
  line-height: 1;
}
.cart-badge[hidden] { display: none; }
.badge-pop { animation: badge-pop .28s ease; }
@keyframes badge-pop {
  0%   { transform: scale(0.6); }
  60%  { transform: scale(1.25); }
  100% { transform: scale(1); }
}

.ai-badge {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}

.btn-restart {
  border: 1.5px solid var(--line);
  border-radius: 999px;
  padding: 8px 14px;
  background: #fff;
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  cursor: pointer;
  transition: background .15s, color .15s, border-color .15s;
  white-space: nowrap;
}
.btn-restart:hover {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}
.btn-restart.restarting {
  opacity: .5;
  cursor: not-allowed;
  animation: btn-shimmer 1s infinite;
}

/* ── Main grid ─────────────────────────────────────────────────────── */
.main {
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr) 420px;
  grid-template-rows: minmax(0, 1fr);
  gap: 16px;
  padding: 16px;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  height: calc(100vh - 56px);
}

.template-drawer {
  width: 100%;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  min-height: 0;
}

.template-drawer-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 22px 22px 16px;
  border-bottom: 1px solid var(--line);
}

.template-drawer-eyebrow,
.template-drawer-label {
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .12em;
  color: var(--muted);
}

.template-drawer-head h2 {
  margin-top: 6px;
  font-size: 24px;
  line-height: 1.05;
}

.template-drawer-section {
  padding: 18px 22px;
  border-bottom: 1px solid var(--line);
}

.template-drawer-section:last-child {
  border-bottom: none;
}

/* ── Panel (left sidebar) ──────────────────────────────────────────── */
.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 18px;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 0;
  min-height: 0;
}

.section {
  border-bottom: 1px solid var(--line);
  padding: 0 0 20px;
  margin: 0 0 20px;
}
.section:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.label {
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--muted);
  margin-bottom: 12px;
}

/* ── Upload block ──────────────────────────────────────────────────── */
.upload {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 2px dashed #c8c8c0;
  border-radius: 22px;
  padding: 28px 20px;
  background: linear-gradient(135deg, #fff, #f8f8f4);
  cursor: pointer;
  transition: border-color .2s, background .2s;
  text-align: center;
}

.upload:hover,
.upload.dragover {
  border-color: var(--ink);
  background: #f0f0ec;
}

.upload input[type="file"] {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.upload-icon {
  font-size: 36px;
  line-height: 1;
  color: var(--muted);
  transition: color .2s;
}

.upload:hover .upload-icon { color: var(--ink); }

.upload strong {
  font-size: 18px;
  font-weight: 800;
  display: block;
  color: var(--ink);
}

.upload small {
  font-size: 12px;
  color: var(--muted);
  display: block;
}

.upload.has-file {
  border-color: var(--ink);
  border-style: solid;
}

.upload.has-file .upload-icon { color: #22c55e; }

/* ── Buttons ───────────────────────────────────────────────────────── */
.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: 18px;
  padding: 18px 20px;
  width: 100%;
  font-family: inherit;
  font-size: 17px;
  font-weight: 800;
  cursor: pointer;
  transition: transform .15s, box-shadow .15s, opacity .15s;
  position: relative;
  overflow: hidden;
}

.btn:not(:disabled):hover  { transform: translateY(-2px); }
.btn:not(:disabled):active { transform: translateY(0); }

.btn:disabled {
  opacity: .38;
  cursor: not-allowed;
  transform: none !important;
}

.btn-icon {
  font-size: 15px;
  opacity: .8;
}

/* Acid / yellow */
.btn-acid {
  background: var(--acid);
  color: var(--ink);
  box-shadow: 0 8px 22px rgba(202,232,8,.40);
}
.btn-acid:not(:disabled):hover {
  box-shadow: 0 12px 28px rgba(202,232,8,.55);
}

/* Dark */
.btn-dark {
  background: var(--ink);
  color: #fff;
  box-shadow: 0 8px 22px rgba(0,0,0,.20);
}
.btn-dark:not(:disabled):hover {
  box-shadow: 0 12px 28px rgba(0,0,0,.30);
}

/* Order button */
.btn-order {
  background: var(--acid);
  color: var(--ink);
  font-size: 16px;
  padding: 14px 28px;
  width: auto;
  border-radius: 16px;
  box-shadow: 0 6px 18px rgba(202,232,8,.35);
}

/* Ghost */
.btn-ghost {
  background: #f3f3ef;
  color: var(--ink);
  border: 1px solid var(--line);
  box-shadow: none;
}

/* Loading shimmer while AI runs */
.btn.ai-running {
  animation: btn-shimmer 1.4s infinite;
  pointer-events: none;
}

@keyframes btn-shimmer {
  0%, 100% { opacity: .7; }
  50% { opacity: 1; }
}

/* Stack */
.stack { display: grid; gap: 12px; }

/* ── Result / error ────────────────────────────────────────────────── */
.result {
  font-size: 12px;
  color: var(--muted);
  margin-top: 8px;
  min-height: 1em;
  line-height: 1.4;
}
.result.error { color: #ef4444; }

/* ── Shape selector ────────────────────────────────────────────────── */
.shape-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.shape-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1.5px solid var(--line);
  border-radius: 18px;
  padding: 14px 10px;
  background: #fafaf7;
  cursor: pointer;
  font-family: inherit;
  font-weight: 700;
  font-size: 13px;
  color: var(--muted);
  transition: background .15s, border-color .15s, color .15s;
}

.shape-card svg {
  width: 32px;
  height: 32px;
  color: var(--muted);
  transition: color .15s;
}

.shape-card:hover {
  border-color: #bbb;
  background: #f5f5f1;
  color: var(--ink);
}

.shape-card.active {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
}

.shape-card.active svg { color: #fff; }

/* ── Shape Presets row (5 icon buttons) ─────────────────────────────── */
.shape-presets {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}
.shape-presets .shape-card {
  padding: 8px 2px;
  border-radius: 10px;
  font-size: 10px;
  gap: 5px;
}
.shape-presets .shape-card svg {
  width: 22px;
  height: 22px;
}

/* ── Controls ──────────────────────────────────────────────────────── */
.controls {
  display: grid;
  gap: 14px;
  margin-top: 14px;
}

.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.field {
  display: grid;
  gap: 6px;
}

.field span {
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
}

.field input[type="number"],
.field select,
.field textarea {
  width: 100%;
  border: 1.5px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  background: #fff;
  font-family: inherit;
  font-weight: 700;
  font-size: 15px;
  color: var(--ink);
  transition: border-color .15s;
  appearance: none;
  -webkit-appearance: none;
}

.field input[type="number"]:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--ink);
}

.field textarea {
  font-weight: 400;
  font-size: 14px;
  resize: none;
  line-height: 1.4;
}

.field select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23777'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 14px center;
  padding-right: 32px;
  cursor: pointer;
}

.field input[type="range"] {
  width: 100%;
  accent-color: var(--ink);
  height: 4px;
  cursor: pointer;
}

.range-val {
  font-size: 11px;
  color: var(--muted);
  font-weight: 600;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin-top: 8px;
  user-select: none;
}

.check-row input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--ink);
  cursor: pointer;
}

.izdat-settings {
  display: grid;
  gap: 12px;
}

.izdat-setting {
  display: grid;
  gap: 6px;
}

.izdat-setting-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.izdat-setting-head span {
  font-size: 15px;
  font-weight: 700;
  color: var(--ink);
}

.izdat-setting-head output {
  min-width: 64px;
  text-align: center;
  border-radius: 12px;
  padding: 6px 10px;
  background: #f1f2ea;
  color: var(--ink);
  font-size: 14px;
  font-weight: 800;
}

.izdat-setting-value-input {
  width: 76px;
  min-width: 76px;
  text-align: center;
  border-radius: 12px;
  padding: 6px 10px;
  border: 1px solid #d9dacd;
  background: #f1f2ea;
  color: var(--ink);
  font-size: 14px;
  font-weight: 800;
}

.izdat-setting-value-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(213, 255, 0, 0.28);
}

.izdat-setting input[type="range"] {
  width: 100%;
  accent-color: #7c3aed;
  height: 4px;
  cursor: pointer;
}

.corner-mode-range {
  margin-bottom: 0;
}

.corner-mode-labels {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.2;
}

.as-check {
  margin-top: 2px;
  color: var(--ink);
}

.as-check span {
  color: var(--ink);
}

.as-help {
  margin-top: -4px;
  font-size: 12px;
  line-height: 1.35;
  color: var(--muted);
}

.as-advanced {
  margin-top: 6px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.as-advanced summary {
  list-style: none;
  cursor: pointer;
  user-select: none;
  font-size: 13px;
  font-weight: 800;
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.as-advanced summary::-webkit-details-marker {
  display: none;
}

.as-advanced summary::after {
  content: "▾";
  font-size: 14px;
  color: var(--muted);
  transition: transform .18s ease;
}

.as-advanced[open] summary::after {
  transform: rotate(180deg);
}

.as-advanced-body {
  display: grid;
  gap: 12px;
  margin-top: 12px;
}

/* ── Stage (right) ─────────────────────────────────────────────────── */
.stage {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  /* Use grid so canvas-wrap row is hard-capped with minmax(0,1fr) */
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  min-height: 0;
  overflow: hidden;
}

.stage-status {
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 12px;
}

/* ── Canvas wrap ───────────────────────────────────────────────────── */
.canvas-wrap {
  position: relative;
  padding: 52px 16px 16px;
  background: #ededeb;
  overflow: hidden;
  min-height: 0;
  user-select: none;
}

.shape-snap-row {
  margin-top: 10px;
}

.workspace-head {
  position: absolute;
  top: 14px;
  left: 18px;
  right: 18px;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
  pointer-events: none;
}

.workspace-title {
  color: #676d73;
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

.canvas-shell {
  position: absolute;
  top: 52px;
  bottom: 16px;
  left: 16px;
  right: 16px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.9),
    0 12px 34px rgba(20, 24, 28, 0.16);
  border: 1px solid rgba(21, 24, 28, 0.13);
  background: #ecece8;
  transition: box-shadow .18s ease, border-color .18s ease, background-color .18s ease;
}

.canvas-shell.is-dragover {
  border-color: rgba(213, 255, 0, 0.8);
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.9),
    0 0 0 4px rgba(213, 255, 0, 0.18),
    0 12px 34px rgba(20, 24, 28, 0.16);
  background: linear-gradient(180deg, rgba(213, 255, 0, 0.08), rgba(255, 255, 255, 0.96));
}

.workspace-ruler {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  opacity: .72;
  display: none;
}

.workspace-ruler-x {
  top: 0;
  left: 24px;
  right: 0;
  height: 22px;
  border-bottom: 1px solid rgba(54, 60, 66, 0.13);
  background:
    repeating-linear-gradient(90deg, rgba(75, 82, 88, 0.28) 0 1px, transparent 1px 24px),
    rgba(255, 255, 255, 0.45);
}

.workspace-ruler-y {
  top: 24px;
  left: 0;
  bottom: 0;
  width: 22px;
  border-right: 1px solid rgba(54, 60, 66, 0.13);
  background:
    repeating-linear-gradient(0deg, rgba(75, 82, 88, 0.28) 0 1px, transparent 1px 24px),
    rgba(255, 255, 255, 0.45);
}

#stickerCanvas {
  position: relative;
  z-index: 1;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none; /* pointer events handle all touch interactions */
  user-select: none;
}

/* ── AI Magic overlay ──────────────────────────────────────────────── */
.magic {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(255,255,255,.75);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 1200;
  padding: 24px;
}

.magic.on {
  display: flex;
  animation: magic-in .25s ease;
}

@keyframes magic-in {
  from { opacity: 0; backdrop-filter: blur(0); }
  to   { opacity: 1; backdrop-filter: blur(10px); }
}

.magic-card {
  background: var(--ink);
  color: #fff;
  border-radius: 24px;
  padding: 26px 36px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0,0,0,.25);
  font-weight: 900;
  font-size: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.sparkles {
  font-size: 32px;
  animation: sparkle-pulse 1.2s ease-in-out infinite alternate;
  letter-spacing: 4px;
}

@keyframes sparkle-pulse {
  from { transform: scale(.92); opacity: .6; filter: brightness(.8); }
  to   { transform: scale(1.08); opacity: 1; filter: brightness(1.2); }
}

/* ── Contour glow trace (drawn on canvas contour) ─────────────────── */
@keyframes contour-glow {
  0%, 100% { filter: drop-shadow(0 0 4px rgba(255,38,74,.7)); }
  50%       { filter: drop-shadow(0 0 12px rgba(255,38,74,1)); }
}

.stage.ai-done #stickerCanvas {
  animation: contour-glow 2s ease-in-out 3;
}

/* ── Bottom bar ────────────────────────────────────────────────────── */
.bottom-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-top: 1px solid var(--line);
  background: #fff;
  flex-shrink: 0;
}

.bottom-bar-spacer { flex: 1; }

/* ── Tool btn + badge wrapper ─────────────────────────────────────── */
.btn-with-badge {
  display: flex;
  flex-direction: column;
  gap: 0;
}

/* Green badge shown when action is done */
.tool-badge[hidden] {
  display: none;
}

.tool-badge {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  font-weight: 700;
  color: #16a34a;
  background: #f0fdf4;
  border: 1.5px solid #bbf7d0;
  border-top: none;
  border-radius: 0 0 14px 14px;
  padding: 5px 14px 7px;
  margin-top: -4px;
  animation: badge-in .2s ease;
}

.tool-badge-contour {
  color: #1d4ed8;
  background: #eff6ff;
  border-color: #bfdbfe;
}

@keyframes badge-in {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Button border-radius when badge is showing */
.btn-with-badge:has(.tool-badge:not([hidden])) .btn {
  border-radius: 18px 18px 0 0;
}

/* Tool reset row (Вернуть фон / Убрать контур) */
.tool-reset-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.btn-tool-ghost {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  padding: 9px 10px;
  background: #f7f7f3;
  color: var(--muted);
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  transition: background .15s, border-color .15s, color .15s;
  white-space: nowrap;
}

.btn-tool-ghost:hover:not(:disabled) {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}

.btn-tool-ghost:disabled {
  opacity: .35;
  cursor: not-allowed;
}

.btn-tool-danger {
  color: #c0392b;
  border-color: #f5c6c2;
  background: #fff5f5;
}

.btn-tool-danger:hover:not(:disabled) {
  background: #c0392b !important;
  border-color: #c0392b !important;
  color: #fff !important;
}

/* Tool rotate row */
.tool-rotate-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
}

.tool-rotate-label {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}

/* Rotate buttons */
.rotate-group {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.btn-rotate {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: #f7f7f3;
  color: var(--ink);
  touch-action: none;            /* удержание не скроллит страницу */
  user-select: none; -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  font-size: 18px;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .15s;
  line-height: 1;
}

.btn-rotate:hover {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
  transform: scale(1.08);
}

.btn-rotate:active {
  transform: scale(.95);
}

.btn-rotate.btn-rotate-danger {
  color: #e5485f;
  border-color: rgba(229, 72, 95, 0.35);
}

.btn-rotate.btn-rotate-danger:hover {
  background: #e5485f;
  border-color: #e5485f;
  color: #fff;
}

.btn-reset-stage {
  color: #c0392b;
  border-color: #f5c6c2;
  background: #fff5f5;
  font-size: 14px;
}
.btn-reset-stage:hover {
  background: #c0392b;
  border-color: #c0392b;
  color: #fff;
}
.btn-reset-stage:disabled {
  opacity: .3;
  cursor: not-allowed;
  transform: none !important;
}

/* Кнопка «Центрировать» в нижнем ряду под канвасом (рядом с поворотом) */
.btn-center-stage {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 14px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: #f7f7f3;
  color: var(--ink);
  font: inherit;
  font-weight: 800;
  font-size: 13px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background .15s ease, color .15s ease, border-color .15s ease, transform .1s ease;
  -webkit-tap-highlight-color: transparent;
}
.btn-center-stage:hover {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
}
.btn-center-stage:active { transform: scale(.97); }
.btn-center-stage:disabled {
  opacity: .4;
  cursor: not-allowed;
  transform: none !important;
}
@media (max-width: 1200px) {
  .btn-center-stage { height: 44px; font-size: 14px; padding: 0 16px; }
}

/* ── Scrollbar ─────────────────────────────────────────────────────── */
.panel::-webkit-scrollbar { width: 4px; }
.panel::-webkit-scrollbar-track { background: transparent; }
.panel::-webkit-scrollbar-thumb { background: var(--line); border-radius: 99px; }

/* ── Mobile ────────────────────────────────────────────────────────── */
@media (max-width: 1200px) {
  /* Fix scrolling: desktop uses overflow:hidden everywhere for a
     full-viewport app; on mobile we need a normal scroll document.
     IMPORTANT: must be `visible`, NOT `auto`. overflow:auto on BOTH
     html AND body makes <body> a scroll container for position:sticky,
     but the real scroller is the viewport (documentElement) — so the
     sticky .stage attaches to a box that never scrolls and stays inert.
     `visible` lets the viewport scroll normally and keeps sticky alive. */
  html, body {
    height: auto;
    overflow: visible;
    max-width: 100%;
    overflow-x: clip;
  }

  .shell {
    height: auto;
    min-height: 100vh;
    min-height: 100dvh; /* excludes mobile browser chrome */
    overflow: visible;
    max-width: 100%;
    overflow-x: clip;
  }

  .main {
    grid-template-columns: 1fr;
    padding: 10px;
    gap: 10px;
    height: auto;      /* remove fixed calc(100vh - 56px) */
    overflow: visible; /* remove overflow:hidden */
    max-width: 100%;
    overflow-x: clip;
  }

  /* Sticky topbar so brand stays visible while scrolling */
  .topbar {
    position: sticky;
    top: 0;
    z-index: 50;
    padding: 0 14px;
    height: 52px;
  }

  .brand { height: 30px; }
  .brand-logo { height: 28px; max-width: 120px; }

  /* Panel: natural height, no scroll clipping */
  .panel {
    max-height: none;
    overflow-y: visible;
    overflow-x: clip;
    max-width: 100%;
    min-width: 0;
    width: 100%;
    box-sizing: border-box;
  }

  /* Template drawer: same */
  .template-drawer {
    max-height: none;
    overflow-y: visible;
  }

  /* Tablet/phone: stage lives below upload and gets the full column width. */
  .stage {
    min-height: 420px;
    height: clamp(420px, 86vw, 880px);
    max-width: 100%;
    min-width: 0;
    box-sizing: border-box;
  }

  .stage > * {
    min-width: 0;
    max-width: 100%;
    width: 100%;
    box-sizing: border-box;
  }

  .canvas-wrap {
    min-width: 0;
    max-width: 100%;
    width: 100%;
    box-sizing: border-box;
  }

  .bottom-bar {
    min-width: 0;
    max-width: 100%;
    overflow-x: visible;
    flex-wrap: wrap;
    justify-content: center;
    gap: 5px;
    padding: 7px;
  }

  .bottom-bar-spacer {
    display: none;
  }

  .bottom-bar .btn-center-stage {
    flex: 0 0 40px !important;
    width: 40px !important;
    max-width: 40px;
    min-width: 0;
    height: 40px;
    padding: 0 !important;
    box-sizing: border-box;
    justify-content: center;
    white-space: nowrap;
    overflow: hidden;
    font-size: 0;
  }

  .bottom-bar .btn-center-stage::before {
    content: "⊕";
    color: var(--ink);
    font-size: 20px;
    line-height: 1;
  }

  .panel .canvas-wrap {
    padding: 46px 10px 10px;
  }

  .panel .canvas-shell {
    top: 46px;
    bottom: 10px;
    left: 10px;
    right: 10px;
    border-radius: 12px;
    max-width: calc(100% - 20px);
  }

  .panel .workspace-head {
    top: 11px;
    left: 12px;
    right: 12px;
  }

  .workspace-title {
    font-size: 10px;
  }

  /* Bigger touch targets for buttons */
  .btn {
    min-height: 52px;
    padding: 14px 20px;
  }

  .btn-rotate {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }

  /* Compact upload zone */
  .upload {
    padding: 18px 16px;
    gap: 4px;
  }
  .upload-icon { font-size: 28px; }
  .upload strong { font-size: 16px; }

  /* Checkout button: full height for easy thumb tap */
  .co-add { height: 56px; font-size: 16px; }

  /* ── Mobile stage slot ───────────────────────────────────────────── */
  /* Слот скрыт на десктопе; на мобиле JS вставляет в него .stage */
  .mobile-stage-slot { display: contents; }

  /* Stage inside the panel must stay inside the phone viewport. */
  .panel .stage {
    margin-left: 0;
    margin-right: 0;
    width: 100%;
    border-radius: 0;
    margin-bottom: 20px;
    /* sticky: stays visible while settings scroll beneath */
    position: sticky;
    top: 52px;
    z-index: 40;
  }
}

@media (max-width: 500px) {
  .top-actions { gap: 6px; }
  .pill { padding: 6px 12px; font-size: 12px; }
  .ai-badge { display: none; }
  .template-mode-switch { grid-template-columns: 1fr; }

  /* Tighter padding in drawer sections on small phones */
  .template-drawer-section { padding: 14px 16px; }
  .template-drawer-head { padding: 16px 16px 12px; }

  .stage {
    min-height: 420px;
    height: clamp(420px, 112vw, 560px);
  }

  .bottom-bar {
    gap: 4px;
    padding: 6px;
  }

  .btn-rotate,
  .bottom-bar .btn-center-stage {
    flex-basis: 36px !important;
    width: 36px !important;
    max-width: 36px;
    height: 36px;
  }

  .bottom-bar .btn-center-stage::before {
    font-size: 18px;
  }
}
.template-mode-switch{
  display:grid;
  grid-template-columns:repeat(2,minmax(0,1fr));
  gap:10px;
}

.template-mode-card{
  width:100%;
  border:1px solid rgba(23,32,39,0.12);
  background:#fff;
  border-radius:16px;
  padding:12px 14px;
  text-align:left;
  cursor:pointer;
  transition:border-color .18s ease, box-shadow .18s ease, transform .18s ease;
  min-height:112px;
}

.template-mode-card strong,
.template-part-card strong{
  display:block;
  font-size:14px;
  line-height:1.2;
  color:#172027;
}

.template-mode-card small,
.template-part-card small{
  display:block;
  margin-top:4px;
  color:#687382;
  line-height:1.35;
}

.template-mode-card.active{
  border-color:#d2f500;
  box-shadow:0 0 0 3px rgba(210,245,0,0.22);
}

.template-mode-card:disabled {
  opacity: .48;
  cursor: not-allowed;
  transform: none !important;
}

.template-regular-button {
  margin-top: 14px;
}
/* .btn задаёт display:flex и перебивает браузерный [hidden]{display:none}.
   Явно гасим кнопку в свободном режиме (видимостью рулит app.js через .hidden). */
.template-regular-button[hidden] {
  display: none !important;
}

.template-parts-list{
  display:grid;
  gap:10px;
}

.template-layers-wrap{
  display:grid;
  gap:10px;
  margin-top:14px;
}

.template-layers-list{
  display:grid;
  gap:8px;
}

.template-layer-card{
  border:1px solid rgba(23,32,39,0.12);
  background:#fff;
  border-radius:14px;
  padding:12px;
  display:grid;
  gap:10px;
}

.template-layer-card.active{
  border-color:#ff5b73;
  box-shadow:0 0 0 3px rgba(255,91,115,0.14);
}

.template-layer-empty{
  border:1px dashed rgba(23,32,39,0.14);
  border-radius:14px;
  padding:14px;
  color:#687382;
  background:#fafbfd;
}

.template-part-card{
  border:1px solid rgba(23,32,39,0.12);
  background:#fff;
  border-radius:16px;
  padding:12px;
}

.template-part-card.active{
  border-color:#ff5b73;
  box-shadow:0 0 0 3px rgba(255,91,115,0.14);
}

.template-part-top{
  display:flex;
  align-items:flex-start;
  justify-content:space-between;
  gap:10px;
}

.template-part-badge{
  flex:0 0 auto;
  min-width:68px;
  text-align:center;
  border-radius:999px;
  padding:5px 9px;
  font-size:11px;
  font-weight:700;
  background:#f3f5f6;
  color:#687382;
}

.template-part-badge.ready{
  background:rgba(210,245,0,0.35);
  color:#172027;
}

.template-part-actions{
  display:flex;
  gap:8px;
  margin-top:10px;
}

.template-part-actions .btn{
  flex:1 1 0;
}

/* ── Checkout (правая панель) ─────────────────────────────────────────── */
.checkout-section{
  margin-top:auto;            /* прижимаем блок к низу правой панели */
  border-bottom:none !important;
}
.checkout{
  padding:18px;
  border-radius:22px;
  background:#111;
  color:#fff;
}
.co-row{
  display:flex;
  align-items:center;
  justify-content:space-between;
  margin-bottom:8px;
  color:#cfcfcf;
  font-size:13px;
  font-weight:700;
}
.co-row span:last-child{
  color:#fff;
  font-weight:800;
}
.co-total{
  display:flex;
  align-items:flex-end;
  justify-content:space-between;
  padding-top:12px;
  margin-top:10px;
  border-top:1px solid rgba(255,255,255,.16);
  font-weight:900;
  font-size:15px;
}
.co-total strong{
  font-size:31px;
  line-height:1;
}
.co-actions{
  display:grid;
  gap:8px;
  margin-top:16px;
}
.co-add{
  height:52px;
  border:0;
  border-radius:16px;
  background:var(--acid);
  color:#111;
  font-family:inherit;
  font-size:15px;
  font-weight:900;
  cursor:pointer;
  transition:filter .15s ease, transform .05s ease;
}
.co-add:hover:not(:disabled){ filter:brightness(.96); }
.co-add:active:not(:disabled){ transform:translateY(1px); }
.co-add:disabled{ opacity:.45; cursor:not-allowed; }
.co-cart{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  gap:8px;
  height:46px;
  border-radius:15px;
  border:1px solid rgba(255,255,255,.25);
  background:transparent;
  color:#fff;
  font-size:14px;
  font-weight:800;
  text-decoration:none;
  cursor:pointer;
  transition:background .15s ease, border-color .15s ease;
}
.co-cart:hover{
  background:rgba(255,255,255,.08);
  border-color:rgba(255,255,255,.5);
}

/* ── Настройка шаблона — обновлённый вид ───────────────────────────────
   Сегментированный переключатель вариантов + аккуратные карточки частей.
   Только стили: DOM/логику app.js не трогаем. */

/* Вариант: сегмент-контрол (вместо выпадающего select) */
.variant-switch{
  display:grid;
  grid-auto-flow:column;
  grid-auto-columns:1fr;          /* равные колонки в один ряд */
  gap:4px;
  margin-top:8px;
  padding:4px;
  background:#f1f3f4;
  border:1px solid var(--line);
  border-radius:16px;
}
.variant-chip{
  min-width:0;
  padding:10px 8px;
  border:0;
  border-radius:12px;
  background:transparent;
  color:#687382;
  font:inherit;
  font-size:12.5px;
  font-weight:700;
  line-height:1.18;
  text-align:center;
  white-space:normal;
  cursor:pointer;
  transition:background .15s ease, color .15s ease, box-shadow .15s ease;
}
.variant-chip:hover{ color:var(--ink); }
.variant-chip.is-active{
  background:var(--acid);
  color:var(--ink);
  box-shadow:0 1px 2px rgba(0,0,0,.14);
}

/* Карточки частей шаблона — мягче и на-бренд (акид вместо розового) */
.template-parts-list{ gap:12px; margin-top:14px; }
.template-part-card{
  border:1px solid var(--line);
  border-radius:18px;
  padding:14px 14px 12px;
  background:#fff;
  transition:border-color .15s ease, box-shadow .15s ease, background .15s ease;
}
.template-part-card.active{
  border-color:var(--acid);
  background:#fcffe8;
  box-shadow:0 0 0 3px rgba(202,232,8,0.28);
}
.template-part-card strong{ font-size:14.5px; }
.template-part-badge{
  align-self:flex-start;
  min-width:0;
  padding:4px 11px;
  font-size:10.5px;
  font-weight:800;
  letter-spacing:.04em;
  text-transform:uppercase;
  background:#eef0f1;
  color:#8a93a0;
}
.template-part-badge.ready{
  background:var(--acid);
  color:var(--ink);
}
.template-part-actions{ gap:8px; margin-top:12px; }
.template-part-actions .btn{
  flex:1 1 0;
  height:44px;
  border-radius:13px;
  font-size:13px;
  font-weight:700;
}

/* Слой-карточки — тот же акид-акцент для единообразия */
.template-layer-card.active{
  border-color:var(--acid);
  background:#fcffe8;
  box-shadow:0 0 0 3px rgba(202,232,8,0.28);
}

/* На ПК (≥901px) убираем дублирующую per-part «Загрузить»: основной путь —
   верхний-левый дропзон с drag&drop. На телефоне/планшете (≤1200px, см. media выше)
   кнопка остаётся, т.к. там нет перетаскивания. Прячем только upload
   (data-template-upload) — «Выбрать» и «Удалить» в слоях не трогаем. */
@media (min-width: 1201px) {
  .mobile-stage-slot { display: none; } /* слот только для мобиле */
  .template-part-actions [data-template-upload] { display:none; }
  /* «Выбрать» сама растянется на всю ширину (flex:1 1 0). */
}

/* ═══════════════════════════════════════════════════════════════════
   Mobile constructor shell — таб-бар + bottom-sheet (≤1200px)
   Десктоп: всё скрыто, секции живут в .panel как обычно.
   ═══════════════════════════════════════════════════════════════════ */
.m-shell { display: none; }

@media (max-width: 1200px) {
  .m-shell { display: block; }
  html, body, .shell, .main { max-width: 100%; overflow-x: hidden; }

  /* Освобождаем место под фиксированный таб-бар, чтобы низ контента не прятался */
  .main { padding-bottom: calc(78px + env(safe-area-inset-bottom)) !important; }

  /* Пустые секции-обёртки, чьи дети уехали в sheet */
  .m-hide { display: none !important; }

  /* ── Таб-бар ─────────────────────────────────────────────────────── */
  .m-tabbar {
    position: fixed; left: 0; right: 0; bottom: 0;
    z-index: 60;
    display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px;
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
    background: rgba(255,255,255,.94);
    -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
    border-top: 1px solid var(--line);
    box-shadow: 0 -10px 30px rgba(0,0,0,.10);
  }
  .m-tab {
    appearance: none; -webkit-appearance: none;
    border: 0; background: #f3f3ef; color: var(--ink);
    border-radius: 16px; min-height: 56px;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    font: inherit; font-weight: 800; font-size: 11px; line-height: 1.1;
    cursor: pointer; text-align: center; padding: 4px 2px;
  }
  .m-tab .m-ico { font-size: 18px; line-height: 1; }
  .m-tab.is-active { background: var(--ink); color: #fff; }
  .m-tab.m-buy { background: var(--acid); color: var(--ink); }
  .m-tab.m-buy.is-active { background: var(--ink); color: #fff; }
  .m-tab.m-buy .m-price { font-size: 12px; font-weight: 900; }

  /* ── Затемнение ──────────────────────────────────────────────────── */
  .m-scrim {
    position: fixed; inset: 0; z-index: 55;
    background: rgba(0,0,0,.34);
    opacity: 0; pointer-events: none; transition: opacity .18s ease;
  }
  .m-scrim.is-open { opacity: 1; pointer-events: auto; }

  /* ── Bottom-sheet ────────────────────────────────────────────────── */
  .m-sheet {
    position: fixed; left: 0; right: 0;
    bottom: calc(84px + env(safe-area-inset-bottom));
    z-index: 58;
    max-height: 68dvh;
    background: #fff; border: 1px solid var(--line); border-bottom: 0;
    border-radius: 26px 26px 0 0;
    box-shadow: 0 -18px 50px rgba(0,0,0,.20);
    transform: translateY(112%);
    transition: transform .24s cubic-bezier(.2,.8,.2,1);
    display: flex; flex-direction: column;
    overflow: hidden;
    touch-action: pan-y;
  }
  .m-sheet.is-open { transform: translateY(0); }
  .m-sheet-grip {
    width: 52px; height: 5px; border-radius: 99px; background: var(--line);
    margin: 8px auto 0; flex: 0 0 auto; cursor: grab; touch-action: none;
  }
  .m-sheet-head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 10px 16px 12px; border-bottom: 1px solid var(--line); flex: 0 0 auto;
  }
  .m-sheet-title { font-weight: 900; font-size: 16px; }
  .m-sheet-close {
    width: 34px; height: 34px; border-radius: 12px;
    border: 1px solid var(--line); background: #f3f3ef;
    font-size: 20px; font-weight: 900; line-height: 1; cursor: pointer;
  }
  .m-sheet-body {
    overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch;
    padding: 14px 16px 18px; min-height: 0;
  }
  .m-panel { display: none; }
  .m-panel.is-active { display: block; min-width: 0; max-width: 100%; }

  /* Перенесённые секции внутри sheet: убираем нижний разделитель/отступ,
     sheet сам задаёт паддинги. Между секциями — ровный зазор. */
  .m-panel > .section,
  .m-panel > #sizeControlsBlock,
  .m-panel > #orderControlsBlock,
  .m-panel > .template-drawer-section {
    border-bottom: 0 !important;
    margin: 0 0 16px !important;
    padding: 0 !important;
  }
  .m-panel > :last-child { margin-bottom: 0 !important; }

  /* Чекаут внутри sheet «Купить» — снимаем прижатие к низу панели */
  .m-panel .checkout-section { margin-top: 0 !important; }

  /* Mobile-only cleanup requested for the phone bottom-sheet flow. */
  #uploadSection > .label,
  #templateModeSection > .template-drawer-label {
    display: none !important;
  }

  #uploadSection .upload {
    min-height: 0;
    padding: 12px 14px;
    border-radius: 18px;
    gap: 2px;
  }
  #uploadSection .upload-icon { font-size: 24px; }
  #uploadSection .upload strong { font-size: 15px; line-height: 1.15; }
  #uploadSection .upload small {
    font-size: 11px;
    line-height: 1.25;
    max-width: 100%;
    overflow-wrap: anywhere;
  }

  .m-panel[data-panel="templates"],
  .m-panel[data-panel="templates"] * {
    min-width: 0;
  }
  .m-panel[data-panel="templates"] {
    overflow-x: hidden;
  }
  .m-panel[data-panel="templates"] .template-mode-switch,
  .m-panel[data-panel="templates"] .template-parts-list,
  .m-panel[data-panel="templates"] .template-layers-list,
  .m-panel[data-panel="templates"] .variant-switch {
    width: 100%;
    max-width: 100%;
  }
  .m-panel[data-panel="templates"] .template-mode-card,
  .m-panel[data-panel="templates"] .template-part-card,
  .m-panel[data-panel="templates"] .template-layer-card {
    width: 100%;
    max-width: 100%;
  }
  .template-variant-field {
    display: block;
    width: 100%;
  }
  .template-variant-title {
    display: block;
    width: 100%;
    margin-bottom: 8px;
    font-size: 11px;
    font-weight: 900;
    letter-spacing: .12em;
    line-height: 1.25;
    text-transform: uppercase;
    color: var(--muted);
  }
  .template-regular-button-mobile {
    width: 100%;
  }
}
