:root {
  --bg: #f0f2f5;
  --card: #ffffff;
  --text: #111b21;
  --muted: #667781;
  --line: #e2e6ea;
  --accent: #25d366;
  --accent-ink: #053b1e;
  --warn: #c0392b;
  --check: #e9edef;
  --radius: 16px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b141a;
    --card: #111b21;
    --text: #e9edef;
    --muted: #8696a0;
    --line: #233138;
    --check: #1c2a31;
    --warn: #ff8a80;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
}

main, .top { max-width: 560px; margin: 0 auto; padding: 0 16px; }

.top {
  display: flex; align-items: center; justify-content: space-between;
  padding-top: calc(12px + env(safe-area-inset-top));
  padding-bottom: 8px;
}
.top h1 { display: flex; gap: 8px; align-items: center; font-size: 20px; margin: 0; }

button, input, textarea { font: inherit; color: inherit; }
button {
  border: 0; border-radius: 12px; padding: 10px 16px; cursor: pointer;
  background: var(--accent); color: var(--accent-ink); font-weight: 600;
}
button:disabled { opacity: .5; cursor: default; }
button.ghost { background: transparent; color: var(--text); border: 1px solid var(--line); }

.card {
  background: var(--card); border-radius: var(--radius);
  padding: 16px; margin: 12px 0; box-shadow: 0 1px 2px rgb(0 0 0 / 6%);
}

/* ---------- source ---------- */
.drop {
  display: grid; place-items: center; gap: 4px; text-align: center;
  min-height: 180px; border: 2px dashed var(--line); border-radius: 12px;
  cursor: pointer; padding: 16px;
}
.drop span { color: var(--muted); }
.loading .drop { opacity: .5; pointer-events: none; }
.link { display: flex; gap: 8px; margin-top: 12px; }
.link input {
  flex: 1; min-width: 0; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 12px; background: transparent;
}

/* ---------- preview ---------- */
.stage {
  /* Sticky so edits further down the page stay visible on phones. */
  position: sticky; top: calc(8px + env(safe-area-inset-top)); z-index: 1;
  aspect-ratio: 1; width: min(100%, 360px, 42svh); margin: 8px auto 0; border-radius: var(--radius); overflow: hidden;
  box-shadow: 0 2px 12px rgb(0 0 0 / 12%);
  background: repeating-conic-gradient(var(--check) 0 25%, transparent 0 50%) 0 0 / 24px 24px var(--card);
}
.stage canvas, .stage img { display: block; width: 100%; height: 100%; }
/*
 * Scrolled down: shrink into the top-right corner. A transform (not a width change) keeps the
 * page layout still, so collapsing never shifts the content under the user's finger.
 * --mini-scale / --mini-shift are set in JS.
 */
.stage { transform-origin: top right; transition: transform .25s ease, border-radius .25s ease; }
.stage.collapsed {
  transform: translateX(var(--mini-shift, 0)) scale(var(--mini-scale, .3));
  border-radius: calc(var(--radius) * 2.5); cursor: pointer;
}
.stage.collapsed .busy-label { display: none; }

.busy {
  position: absolute; left: 8px; right: 8px; bottom: 8px; display: grid; gap: 6px;
  padding: 8px 10px; border-radius: 12px; background: rgb(17 27 33 / 70%); color: #fff;
  font-size: 13px; font-weight: 600; backdrop-filter: blur(6px);
}
.stage.collapsed .busy { padding: 10px; }
.stage.collapsed .busy-track { height: 14px; border-radius: 7px; }
.busy-track { height: 4px; border-radius: 2px; background: rgb(255 255 255 / 25%); overflow: hidden; }
.busy-bar { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
/* Long-press the finished sticker on iPhone to Copy / Save / Share it. */
.stage img { -webkit-touch-callout: default; }
.status { text-align: center; color: var(--muted); font-size: 14px; margin: 8px 0 0; min-height: 1.4em; }
.status.warn { color: var(--warn); }
.hint { width: fit-content; margin: 6px auto 0; padding-left: 20px; color: var(--muted); font-size: 14px; }

/* ---------- controls ---------- */
fieldset { border: 0; padding: 0; margin: 0 0 16px; display: grid; gap: 10px; }
fieldset:last-child { margin-bottom: 0; }
legend { font-weight: 700; margin-bottom: 8px; padding: 0; }
legend small, .field small { color: var(--muted); font-weight: 400; }

.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg label { flex: 1 0 auto; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: block; text-align: center; padding: 8px 12px; border-radius: 10px;
  border: 1px solid var(--line); white-space: nowrap; cursor: pointer; font-size: 15px;
}
.seg input:checked + span { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.f-bold { font-weight: 900 !important; }
.f-rounded { font-family: ui-rounded, "Arial Rounded MT Bold", system-ui; }
.f-hand { font-family: "Marker Felt", "Comic Sans MS", cursive; }

.row { display: flex; align-items: center; gap: 12px; }
.row > span { width: 56px; flex: none; color: var(--muted); font-size: 14px; }
.row input[type=range] { flex: 1; }
input[type=range] { accent-color: var(--accent); width: 100%; min-height: 32px; }

.swatches { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.swatch, .swatches input[type=color] {
  width: 28px; height: 28px; padding: 0; border-radius: 50%;
  border: 2px solid var(--line); background: none;
}
.swatches input[type=color]::-webkit-color-swatch-wrapper { padding: 0; }
.swatches input[type=color]::-webkit-color-swatch { border: 0; border-radius: 50%; }

textarea, .field input[type=text] {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line);
  border-radius: 12px; background: transparent; resize: none;
}
.field { display: grid; gap: 4px; margin-top: 10px; }
.field output { color: var(--muted); font-size: 14px; }
details summary { cursor: pointer; font-weight: 700; }

/* ---------- export bar ---------- */
.actions {
  position: fixed; inset: auto 0 0 0; display: flex; gap: 8px;
  max-width: 560px; margin: 0 auto;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, var(--bg) 30%);
}
.actions button { flex: 1; min-height: 52px; font-size: 17px; }
.actions .ghost { background: var(--card); }

.credit { max-width: 560px; margin: 24px auto 0; padding: 0 16px; text-align: center; color: var(--muted); font-size: 13px; }
.credit p { margin: 4px 0; }
.credit a { color: inherit; font-weight: 600; }
.credit-links { display: flex; align-items: center; justify-content: center; gap: 10px; }
.credit-links svg { display: block; }
.credit .github { display: inline-flex; align-items: center; gap: 6px; }

.toast {
  position: fixed; left: 50%; bottom: calc(92px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 12px;
  background: #111b21; color: #fff; font-size: 14px; box-shadow: 0 4px 16px rgb(0 0 0 / 25%);
}
