:root {
  --bg: #f7f2ed;
  --panel: #fffaf5;
  --panel-strong: #ffffff;
  --ink: #2d2523;
  --muted: #81736c;
  --line: rgba(45, 37, 35, 0.12);
  --accent: #a46a52;
  --accent-dark: #6f3e31;
  --shadow: 0 20px 60px rgba(59, 44, 38, 0.12);
  --radius: 22px;
  color-scheme: light;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh;
  background: radial-gradient(circle at top left, #fff 0 0, transparent 360px), var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }

.hidden { display: none !important; }
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: center;
  padding: 14px 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 250, 245, 0.86);
  backdrop-filter: blur(18px);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 270px; }
.brand-mark {
  display: grid; place-items: center;
  width: 44px; height: 44px;
  border-radius: 50%;
  background: linear-gradient(135deg, #caa184, #7e4938);
  color: white;
  font-family: Georgia, serif;
  font-size: 24px;
  box-shadow: 0 10px 30px rgba(126, 73, 56, 0.25);
}
.brand strong { display: block; letter-spacing: -0.02em; }
.brand span { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; }
.top-actions { display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: 10px; }
.btn {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 10px 14px;
  color: var(--ink);
  background: var(--panel-strong);
  box-shadow: 0 6px 22px rgba(45, 37, 35, 0.06);
  transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease;
  font-weight: 700;
  font-size: 13px;
  white-space: nowrap;
}
.btn:hover { transform: translateY(-1px); box-shadow: 0 10px 28px rgba(45, 37, 35, 0.1); border-color: rgba(164, 106, 82, .35); }
.btn.primary { background: linear-gradient(135deg, var(--accent), var(--accent-dark)); color: #fff; border-color: transparent; }
.btn.subtle { background: rgba(255, 255, 255, .62); }
.file-label { position: relative; overflow: hidden; }
.file-label input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.app-shell {
  display: grid;
  grid-template-columns: minmax(280px, 320px) minmax(360px, 1fr) minmax(290px, 340px);
  gap: 14px;
  padding: 14px;
}
.sidebar, .editor {
  min-height: calc(100vh - 90px);
  max-height: calc(100vh - 90px);
  overflow: auto;
  padding-right: 2px;
}
.panel {
  background: rgba(255, 250, 245, .84);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 12px 38px rgba(45, 37, 35, 0.06);
  padding: 18px;
  margin-bottom: 14px;
}
.intro-panel h2, .panel h2, .stage-toolbar h1 { margin: 0 0 8px; letter-spacing: -0.035em; }
.panel h2 { font-size: 18px; }
.eyebrow {
  margin: 0 0 8px;
  color: var(--accent-dark);
  font-size: 11px;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: .16em;
}
.muted { color: var(--muted); }
.tiny { font-size: 12px; line-height: 1.45; }
.error { color: #a4312a; }
.field-label { display: block; margin: 12px 0 7px; font-weight: 800; font-size: 12px; color: #53443f; }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 11px 12px;
  background: rgba(255,255,255,.86);
  color: var(--ink);
  outline: none;
}
textarea { min-height: 84px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: rgba(164,106,82,.45); box-shadow: 0 0 0 4px rgba(164,106,82,.08); }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
input[type="color"] { height: 46px; padding: 5px; }
.check-row { display: flex; gap: 10px; align-items: center; margin: 12px 0; font-weight: 700; font-size: 13px; color: #564741; }
.check-row input { width: auto; }
.mini-stats { display: flex; justify-content: space-between; gap: 8px; padding-top: 14px; font-size: 12px; color: var(--muted); }
.template-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.template-card {
  border: 1px solid var(--line);
  background: rgba(255,255,255,.62);
  border-radius: 18px;
  padding: 8px;
  box-shadow: 0 10px 28px rgba(45,37,35,.06);
  transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease;
}
.template-card:hover { transform: translateY(-2px); box-shadow: 0 14px 38px rgba(45,37,35,.10); }
.template-card.active { border-color: rgba(164, 106, 82, .62); box-shadow: 0 0 0 3px rgba(164,106,82,.08), 0 14px 38px rgba(45,37,35,.10); }
.template-thumb {
  width: 100%;
  aspect-ratio: 5 / 7;
  border-radius: 12px;
  background: #fff;
  display: block;
}
.template-meta { padding: 8px 4px 2px; }
.template-meta strong { display: block; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.template-meta span { display: block; margin-top: 2px; color: var(--muted); font-size: 11px; }

.stage-wrap {
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.stage-toolbar {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
  margin-bottom: 14px;
  padding: 4px 2px;
}
.stage-toolbar h1 { font-size: clamp(22px, 2vw, 32px); }
.download-group { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.canvas-card {
  width: 100%;
  max-width: min(560px, 72vh, 100%);
  background: #fff;
  border-radius: 26px;
  padding: 14px;
  box-shadow: var(--shadow);
  border: 1px solid rgba(255,255,255,.9);
}
#designCanvas {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 18px;
  background: #fff;
}
.hint-row {
  width: 100%;
  max-width: 560px;
  margin: 14px auto 0;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}
.fields-form .field-item { margin-bottom: 12px; }
.fields-form .field-item label { display: block; font-size: 12px; font-weight: 900; color: #55453f; margin-bottom: 6px; }
.help-panel ol { margin: 10px 0 0 20px; padding: 0; color: var(--muted); font-size: 13px; line-height: 1.8; }
.toast {
  position: fixed;
  left: 50%; bottom: 26px;
  transform: translateX(-50%) translateY(20px);
  opacity: 0;
  background: #2d2523;
  color: #fff;
  border-radius: 999px;
  padding: 12px 16px;
  box-shadow: 0 18px 55px rgba(45,37,35,.25);
  transition: opacity .2s ease, transform .2s ease;
  z-index: 50;
  pointer-events: none;
  font-size: 13px;
  font-weight: 800;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(circle at top left, rgba(255,255,255,.9), rgba(247,242,237,.96));
}
.gate-card {
  width: min(520px, 100%);
  background: var(--panel-strong);
  border: 1px solid var(--line);
  border-radius: 30px;
  box-shadow: var(--shadow);
  padding: 34px;
  text-align: center;
}
.gate-card h1 { margin: 0 0 8px; font-size: 34px; letter-spacing: -0.04em; }
.gate-row { display: flex; gap: 10px; margin-top: 20px; }
.gate-row input { text-align: center; text-transform: uppercase; }

@media (max-width: 1260px) {
  .app-shell { grid-template-columns: 300px 1fr; }
  .editor { grid-column: 1 / -1; max-height: none; min-height: 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
  .canvas-card { max-width: min(560px, 80vh, 100%); }
  .editor .panel { margin: 0; }
}
@media (max-width: 820px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .top-actions { justify-content: flex-start; }
  .app-shell { grid-template-columns: 1fr; padding: 12px; }
  .sidebar, .editor { min-height: 0; max-height: none; overflow: visible; }
  .editor { display: block; }
  .editor .panel { margin-bottom: 14px; }
  .template-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stage-toolbar { align-items: flex-start; flex-direction: column; }
  .canvas-card { max-width: 100%; padding: 8px; border-radius: 18px; }
  #designCanvas { border-radius: 12px; }
  .gate-row { flex-direction: column; }
}


.full-width { width: 100%; justify-content: center; }
.drag-copy { margin: 0; }
.matching-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
.mini-check {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255,255,255,.7);
  font-size: 13px;
  font-weight: 700;
}
.mini-check.checked {
  border-color: rgba(164,106,82,.45);
  box-shadow: 0 0 0 3px rgba(164,106,82,.08);
}
.mini-check input { width: auto; }
#designCanvas { cursor: move; }
.mobile-steps li { margin-bottom: 6px; }
@media (max-width: 820px) {
  .matching-grid { grid-template-columns: 1fr; }
}


/* FIXED PREVIEW LAYOUT */
html, body { width: 100%; overflow-x: hidden; }
.app-shell {
  height: calc(100vh - 82px);
  overflow: hidden;
  align-items: stretch;
}
.sidebar, .editor {
  max-height: calc(100vh - 104px);
  overflow-y: auto;
  overflow-x: hidden;
}
.stage-wrap {
  min-height: 0;
  max-height: calc(100vh - 104px);
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 10px 20px;
}
.stage-toolbar {
  position: sticky;
  top: 0;
  z-index: 5;
  background: rgba(247,242,237,.92);
  backdrop-filter: blur(12px);
  padding: 8px 4px 12px;
}
.canvas-card {
  width: auto;
  max-width: min(500px, 54vh, 100%);
  padding: 10px;
  margin: 0 auto;
}
#designCanvas {
  width: auto;
  max-width: 100%;
  max-height: calc(100vh - 270px);
  object-fit: contain;
  margin: 0 auto;
}
.hint-row { max-width: 500px; }
@media (max-width: 1260px) {
  .app-shell {
    height: auto;
    overflow: visible;
  }
  .sidebar, .editor, .stage-wrap {
    max-height: none;
    overflow: visible;
  }
  .canvas-card { max-width: min(520px, 80vw); }
  #designCanvas { max-height: none; width: 100%; }
}
@media (max-width: 820px) {
  .canvas-card { max-width: 100%; }
}
