:root {
  --bg: #FBF8F2;
  --ink: #21211D;
  --muted: #66625A;
  --line: #E4DDD0;
  --card: #FFFFFF;
  --accent: #B93617;
  --accent-hover: #9E2C10;
  --accent-soft: #F7E6DF;
  --guide: #721BDA;
  --danger: #A4161A;
  --ok: #1F6E43;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(33, 33, 29, .05), 0 8px 24px -12px rgba(33, 33, 29, .14);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --maxh: 62vh;
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, p { margin: 0; }
button, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}
.skip {
  position: absolute; left: 12px; top: -48px; z-index: 10;
  background: var(--ink); color: #fff; padding: 8px 14px; border-radius: 8px;
}
.skip:focus { top: 12px; }

:focus-visible { outline: 3px solid var(--guide); outline-offset: 2px; border-radius: 6px; }

/* ---------- Header / hero ---------- */
.top {
  max-width: 1200px; margin: 0 auto; padding: 20px 24px 0;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
}
.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; font-weight: 750; font-size: 19px; letter-spacing: -.01em; }
.brand-mark { width: 30px; height: 30px; }
.top-note { color: var(--muted); font-size: 14px; }

.hero { max-width: 1200px; margin: 0 auto; padding: 28px 24px 20px; }
.hero h1 { font-size: clamp(30px, 4.2vw, 46px); line-height: 1.08; letter-spacing: -.025em; font-weight: 800; max-width: 28ch; }
.hero p { margin-top: 12px; font-size: clamp(17px, 1.6vw, 20px); color: var(--muted); }

/* ---------- Tool layout ---------- */
.tool {
  max-width: 1200px; margin: 0 auto; padding: 0 24px 56px;
  display: grid; gap: 20px;
  grid-template-columns: minmax(0, 1fr) 340px;
  grid-template-rows: auto 1fr;
  align-items: start;
}
.stage-card { grid-column: 1; grid-row: 1; min-width: 0; }
.thumbs-card { grid-column: 1; grid-row: 2; min-width: 0; }
.controls { grid-column: 2; grid-row: 1 / span 2; position: sticky; top: 16px; }
.download-mobile { display: none; }

.panel, .stage-card, .thumbs-card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.panel { padding: 18px 20px; }

/* ---------- Stage ---------- */
.stage-card { padding: 16px; }
.stage {
  position: relative; border-radius: 12px;
  display: grid; place-items: center; padding: 12px;
  background: #F4F0E8;
}
.stage.is-empty { min-height: 360px; border: 2px dashed #D6CDBC; background: #FAF7F1; }
.stage.is-dragging { border: 2px dashed var(--guide); background: #F3ECFD; }
.drop-hint {
  position: absolute; inset: 0; display: none; place-items: center;
  font-weight: 700; font-size: 20px; color: var(--guide);
  background: rgba(243, 236, 253, .88); border-radius: 12px; pointer-events: none;
}
.stage.is-dragging .drop-hint { display: grid; }

.empty { text-align: center; padding: 24px 12px; }
.empty-art { width: 120px; height: auto; margin-bottom: 14px; }
.empty-title { font-weight: 700; font-size: 20px; }
.empty-sub { color: var(--muted); font-size: 14px; margin-top: 4px; }
.empty-actions { margin-top: 18px; display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.stage:not(.is-empty) .empty { display: none; }
kbd {
  font: 600 12px/1 var(--font); padding: 2px 6px; border-radius: 5px;
  border: 1px solid var(--line); border-bottom-width: 2px; background: #fff;
}

.checker {
  background-color: #fff;
  background-image:
    conic-gradient(#ECE7DD 25%, transparent 0 50%, #ECE7DD 0 75%, transparent 0);
  background-size: 16px 16px;
}
.sheet {
  position: relative;
  width: min(100%, calc(var(--maxh) * var(--ar, 1.5)));
  aspect-ratio: var(--ar, 1.5);
  border-radius: 4px; overflow: hidden;
  box-shadow: 0 0 0 1px rgba(33, 33, 29, .08);
}
.sheet canvas, .overlay { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.sheet.pixelated canvas { image-rendering: pixelated; }

.overlay .shade { fill: var(--guide); fill-opacity: .2; }
.overlay .halo { fill: none; stroke: #fff; stroke-opacity: .9; stroke-width: 3.5; vector-effect: non-scaling-stroke; }
.overlay .cut { fill: none; stroke: var(--guide); stroke-width: 1.75; vector-effect: non-scaling-stroke; }
.overlay .bad { fill: var(--danger); fill-opacity: .18; }

.sheet-meta { margin-top: 10px; color: var(--muted); font-size: 14px; display: flex; gap: 8px; flex-wrap: wrap; }
.sheet-meta strong { color: var(--ink); font-weight: 650; overflow-wrap: anywhere; }

/* ---------- Controls ---------- */
.group + .group { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--line); }
.group h3, .h3-label { display: block; font-size: 16px; font-weight: 700; margin-bottom: 10px; }
.group-image[hidden] + .group { margin-top: 0; padding-top: 0; border-top: 0; }
.btn-block { width: 100%; }
.privacy { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 14px; color: var(--ok); }
.privacy-center { justify-content: center; margin-top: 16px; }
.privacy svg { width: 15px; height: 15px; flex: none; }
.load-msg { margin-top: 10px; font-size: 14px; font-weight: 600; color: var(--danger); }
.load-msg.is-note { color: var(--muted); font-weight: 500; }
.status:empty { display: none; }

.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.help { font-size: 13px; color: var(--muted); margin-top: 6px; overflow-wrap: anywhere; }

input[type="text"], input[type="number"] {
  width: 100%; min-width: 0; height: 44px; padding: 0 12px;
  font-size: 16px; /* >=16px prevents iOS zoom */
  background: #fff; border: 1px solid #D6CDBC; border-radius: var(--radius-sm);
}
input[type="text"]:focus-visible, input[type="number"]:focus-visible { outline-offset: 0; border-color: var(--guide); }

.stepper {
  display: grid; grid-template-columns: 40px minmax(0, 1fr) 40px;
  border: 1px solid #D6CDBC; border-radius: var(--radius-sm); overflow: hidden; background: #fff;
}
.stepper input {
  border: 0; border-radius: 0; text-align: center; font-weight: 650; padding: 0 4px;
  -moz-appearance: textfield; appearance: textfield;
}
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper:focus-within { border-color: var(--guide); box-shadow: 0 0 0 3px rgba(114, 27, 218, .18); }
.stepper input:focus-visible { outline: none; }
.step-btn {
  border: 0; background: #F6F2EA; cursor: pointer; font-size: 20px; line-height: 1; font-weight: 500;
  touch-action: manipulation;
}
.step-btn:hover { background: #EEE7DA; }
.step-btn:focus-visible { outline-offset: -3px; }

.spacing { margin-top: 14px; }
.spacing summary {
  cursor: pointer; font-weight: 600; font-size: 14px; padding: 6px 0;
  display: flex; align-items: center; gap: 8px; list-style: none;
}
.spacing summary::-webkit-details-marker { display: none; }
.spacing summary::before {
  content: ""; width: 7px; height: 7px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor;
  transform: rotate(-45deg); transition: transform .15s;
}
.spacing[open] summary::before { transform: rotate(45deg); }
.summary-note { font-weight: 500; color: var(--muted); }
.spacing .help { margin: 4px 0 12px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 16px; border-radius: 12px; cursor: pointer;
  font-weight: 650; font-size: 15px; border: 1px solid transparent; white-space: nowrap;
  touch-action: manipulation; transition: background-color .15s, border-color .15s, transform .05s;
}
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-ghost { background: #fff; border-color: #D6CDBC; }
.btn-ghost:hover { border-color: var(--ink); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-big { width: 100%; min-height: 54px; font-size: 17px; border-radius: 14px; }

progress {
  width: 100%; height: 8px; margin-top: 12px; border: 0; border-radius: 99px; overflow: hidden;
  background: #EFE9DE; appearance: none; -webkit-appearance: none;
}
progress::-webkit-progress-bar { background: #EFE9DE; }
progress::-webkit-progress-value { background: var(--accent); border-radius: 99px; }
progress::-moz-progress-bar { background: var(--accent); border-radius: 99px; }

.status { margin-top: 10px; font-size: 14px; color: var(--muted); min-height: 1.5em; }
.status.is-error { color: var(--danger); font-weight: 600; }
.status.is-ok { color: var(--ok); }

/* ---------- Thumbnails ---------- */
.thumbs-card { padding: 18px 16px; }
.thumbs-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; padding: 0 4px; }
.thumbs-head h2 { font-size: 16px; font-weight: 700; }
.thumbs-note { font-size: 13px; color: var(--muted); }
.thumbs { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); }
.thumbs.match-cols { grid-template-columns: repeat(var(--tcols, 4), minmax(0, 1fr)); }
.thumb {
  width: 100%; border: 1px solid var(--line); background: #fff; border-radius: 12px; padding: 6px;
  cursor: pointer; display: grid; gap: 6px; text-align: center;
  transition: border-color .15s, box-shadow .15s;
}
.thumb:hover:not(:disabled) { border-color: var(--guide); box-shadow: 0 0 0 3px rgba(114, 27, 218, .12); }
.thumb:disabled { cursor: default; }
.thumbs.is-stale { opacity: .55; }
.thumb-img { aspect-ratio: 4 / 3; border-radius: 6px; display: grid; place-items: center; overflow: hidden; min-width: 0; }
.thumb canvas { width: 100%; height: 100%; min-width: 0; min-height: 0; object-fit: contain; display: block; }
.thumb canvas.pixelated { image-rendering: pixelated; }
.thumb-name { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thumbs-more, .thumbs-empty { font-size: 14px; color: var(--muted); padding: 4px; }
.thumbs-more { margin-top: 10px; }

/* ---------- How it works / footer ---------- */
.how { max-width: 1200px; margin: 0 auto; padding: 8px 24px 56px; }
.how h2 { font-size: 22px; font-weight: 750; letter-spacing: -.01em; margin-bottom: 16px; }
.how-steps { list-style: none; margin: 0; padding: 0; display: grid; gap: 16px; grid-template-columns: repeat(3, 1fr); }
.how-steps li { background: transparent; border-top: 2px solid var(--ink); padding-top: 14px; }
.how-num { font-size: 13px; font-weight: 700; color: var(--accent); }
.how-steps h3 { font-size: 17px; margin: 4px 0 6px; }
.how-steps p { color: var(--muted); font-size: 15px; }

.foot {
  max-width: 1200px; margin: 0 auto; padding: 24px; border-top: 1px solid var(--line);
  display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; color: var(--muted); font-size: 14px;
}

/* ---------- Mobile ---------- */
@media (max-width: 880px) {
  .tool { grid-template-columns: minmax(0, 1fr); grid-template-rows: none; padding: 0 16px 40px; gap: 16px; }
  .stage-card, .thumbs-card, .controls { grid-column: auto; grid-row: auto; position: static; }
  .download-desktop { display: none; }
  .download-mobile { display: block; }
  .top, .hero, .how, .foot { padding-left: 16px; padding-right: 16px; }
  .hero { padding-top: 28px; padding-bottom: 20px; }
  .top-note { display: none; }
  .stage.is-empty { min-height: 260px; }
  :root { --maxh: 52vh; }
  .how-steps { grid-template-columns: 1fr; }
}
@media (max-width: 380px) {
  .btn { padding: 0 12px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; }
}
