/* Layout: card stage on the left (sticky on desktop), step panel on the right; stacked on phones. The page stays quiet so the card is the star. */
:root {
  --bg: #efeeea;
  --surface: #f9f8f5;
  --surface-2: #ffffff;
  --fg: #1d1f24;
  --muted: #5e616b;
  --line: #d9d7d0;
  --line-strong: #b9b6ad;
  --paper: #fbf8f1;
  --air-red: #c8312f;
  --air-blue: #1f4fa0;
  --focus: #1f4fa0;
  --danger: #b3261e;
  --on-danger: #fff;
  --shadow: 0 1px 2px rgba(20, 20, 30, .14), 0 18px 40px -18px rgba(20, 20, 30, .45);
  --display: "Bricolage Grotesque", "Avenir Next", system-ui, sans-serif;
  --body: "Figtree", "Helvetica Neue", system-ui, sans-serif;
  --hand: "Caveat", "Bradley Hand", cursive;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171a; --surface: #1f2024; --surface-2: #26282d; --fg: #ecebe7; --muted: #a4a6ae;
    --line: #34363c; --line-strong: #4c4f57; --focus: #8fb3f0; --danger: #ff8a80; --on-danger: #1d1f24;
    --shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 18px 40px -18px rgba(0, 0, 0, .8);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16171a; --surface: #1f2024; --surface-2: #26282d; --fg: #ecebe7; --muted: #a4a6ae;
  --line: #34363c; --line-strong: #4c4f57; --focus: #8fb3f0; --danger: #ff8a80; --on-danger: #1d1f24;
  --shadow: 0 1px 2px rgba(0, 0, 0, .5), 0 18px 40px -18px rgba(0, 0, 0, .8);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.55 var(--body);
  -webkit-font-smoothing: antialiased;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.skip { position: absolute; left: 12px; top: -60px; z-index: 100; padding: 10px 16px; border-radius: 999px; background: var(--fg); color: var(--bg); font-weight: 600; text-decoration: none; }
.skip:focus { top: calc(10px + env(safe-area-inset-top, 0px)); }
.panel:focus { outline: none; }
.face-front:focus-visible { outline: 3px solid var(--focus); outline-offset: 4px; }
@media (forced-colors: active) {
  .chip.on, .seg button.on, .tpl.on, .font-opt.on, .emo.on, .sw.on, .sw-custom.on { outline: 3px solid Highlight; outline-offset: 2px; }
  .step-tab.on { border-bottom-color: Highlight; }
}
svg { stroke: currentColor; stroke-width: 2; fill: none; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- header ---------- */
.top, .app, .foot {
  max-width: 1240px;
  margin: 0 auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding-block: 18px 10px; }
.brand-h { margin: 0; font: inherit; }
.brand { display: inline-flex; align-items: center; gap: 10px; text-decoration: none; color: var(--fg); }
.brand span { font: 700 26px/1 var(--display); letter-spacing: -.02em; }
.brand em { font: 600 1.15em var(--hand); font-style: normal; color: var(--air-red); }
.tagline { margin: 0; color: var(--muted); font-size: 14px; }
.top-right { display: flex; align-items: center; gap: 12px; }
.theme-btn .i-sun { display: none; }
.theme-btn[aria-pressed="true"] .i-sun { display: block; }
.theme-btn[aria-pressed="true"] .i-moon { display: none; }

/* ---------- app grid ---------- */
.app { display: grid; grid-template-columns: minmax(0, 1.45fr) minmax(320px, 1fr); gap: 28px; align-items: start; padding-block: 8px 40px; }
@media (max-width: 880px) { .app { grid-template-columns: minmax(0, 1fr); gap: 18px; } }

.stage { min-width: 0; }
.stage-sticky { position: sticky; top: calc(12px + env(safe-area-inset-top, 0px)); display: flex; flex-direction: column; gap: 12px; }
@media (max-width: 880px) { .stage-sticky { position: static; } }

.card-wrap { perspective: 2200px; padding: 6px; }
.flipper {
  position: relative; width: 100%; max-width: 100%; aspect-ratio: 3 / 2;
  transform-style: preserve-3d;
  transition: transform .9s cubic-bezier(.3, .1, .2, 1.15);
}
.flipper.flipped { transform: rotateY(180deg); }
.face {
  position: absolute; inset: 0; width: 100%; height: 100%;
  border-radius: 6px; box-shadow: var(--shadow);
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
  background: var(--paper);
}
.face-front { touch-action: none; cursor: default; }
.face-front.grab { cursor: grab; }
.face-front.grabbing { cursor: grabbing; }
.face-back { transform: rotateY(180deg); }
.flipper.flipped .face-front, .flipper:not(.flipped) .face-back { pointer-events: none; }
@media (prefers-reduced-motion: reduce) { .flipper { transition: none; } }

.face-back { cursor: pointer; }
.stage-bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.bar-right { display: flex; align-items: center; gap: 6px; }
.bar-right .icon-btn { width: 40px; height: 40px; }
.hint { margin: 0; color: var(--muted); font-size: 13.5px; }

/* Phones: keep the card pinned at the top while the options scroll,
   and show the tools for the selected item as a sheet at the bottom. */
@media (max-width: 880px) and (min-height: 560px) {
  .stage {
    position: sticky; top: 0; z-index: 20;
    margin-inline: calc(-1 * max(16px, env(safe-area-inset-left, 0px)));
    padding: calc(6px + env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-left, 0px)) 8px;
    background: var(--bg);
    box-shadow: 0 10px 14px -12px rgba(0, 0, 0, .35);
  }
  .stage-sticky { gap: 8px; }
  .card-wrap { width: 100%; max-width: calc(36dvh * 1.5); margin-inline: auto; padding: 4px; }
  .hint { font-size: 12.5px; text-align: center; }
  .selection {
    position: fixed; z-index: 40;
    left: max(8px, env(safe-area-inset-left, 0px)); right: max(8px, env(safe-area-inset-right, 0px));
    bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    max-height: 44dvh; overflow-y: auto; overscroll-behavior: contain;
    background: var(--surface-2); box-shadow: 0 -6px 30px rgba(0, 0, 0, .28);
    border-radius: 16px;
  }
  .sel-head { position: sticky; top: -12px; background: var(--surface-2); margin: -12px -14px 0; padding: 12px 14px 8px; z-index: 1; }
}

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 40px; padding: 8px 14px; border-radius: 999px;
  border: 1px solid var(--line-strong); background: var(--surface-2);
  font-weight: 600; font-size: 14px; cursor: pointer; text-align: center;
  transition: background .15s, border-color .15s, transform .1s;
}
.btn:hover { border-color: var(--fg); }
.btn:active { transform: scale(.98); }
.btn svg { width: 18px; height: 18px; }
.btn.primary { background: var(--air-red); border-color: var(--air-red); color: #fff; }
.btn.primary:hover { background: #ad2725; border-color: #ad2725; }
.btn.ghost { background: transparent; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: var(--on-danger); }
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.file-btn:focus-within { outline: 2px solid var(--focus); outline-offset: 2px; }

.icon-btn {
  width: 36px; height: 36px; display: inline-grid; place-items: center;
  border-radius: 999px; border: 1px solid var(--line); background: var(--surface-2); cursor: pointer;
}
.icon-btn svg { width: 18px; height: 18px; }
.icon-btn:hover:not(:disabled) { border-color: var(--fg); }
.icon-btn:disabled { opacity: .35; cursor: default; }
.icon-btn.danger { color: var(--danger); }

.seg { display: inline-flex; padding: 3px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); }
.seg button { border: 0; background: transparent; padding: 6px 14px; border-radius: 999px; font-weight: 600; font-size: 14px; cursor: pointer; color: var(--muted); }
.seg button.on { background: var(--fg); color: var(--bg); }
.seg.small button { font-size: 13px; padding: 5px 12px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 999px;
  padding: 6px 12px; font-size: 13.5px; font-weight: 500; cursor: pointer;
}
.chip:hover { border-color: var(--line-strong); }
.chip.on { border-color: var(--fg); background: var(--fg); color: var(--bg); }

.swatches { display: flex; flex-wrap: wrap; gap: 7px; align-items: center; }
.sw {
  width: 30px; height: 30px; border-radius: 999px; cursor: pointer; padding: 0;
  border: 1px solid rgba(0, 0, 0, .18); box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .25);
}
.swatches.big .sw { width: 38px; height: 38px; border-radius: 8px; }
.sw.on { outline: 2px solid var(--fg); outline-offset: 2px; }
.sw-custom {
  position: relative; width: 30px; height: 30px; border-radius: 999px; overflow: hidden; cursor: pointer;
  background: conic-gradient(#e74c3c, #f1c40f, #2ecc71, #3498db, #9b59b6, #e74c3c);
  border: 1px solid rgba(0, 0, 0, .18);
}
.swatches.big .sw-custom { width: 38px; height: 38px; border-radius: 8px; }
.sw-custom input { position: absolute; inset: -8px; width: 200%; height: 200%; opacity: 0; cursor: pointer; }
.sw-custom.on { outline: 2px solid var(--fg); outline-offset: 2px; }

input[type="color"] { width: 44px; height: 32px; border: 1px solid var(--line); border-radius: 8px; padding: 2px; background: var(--surface-2); }

.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span, .range > span, .label { font-size: 12px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); }
.field input:not([type="color"]), .field textarea, .field select, #emojiSearch {
  width: 100%; border: 1px solid var(--line-strong); background: var(--surface-2); border-radius: 8px; padding: 9px 11px; min-height: 40px;
}
.field textarea { resize: vertical; }
.range { display: grid; grid-template-columns: 130px minmax(0, 1fr); align-items: center; gap: 12px; }
.range input { width: 100%; accent-color: var(--air-red); }
.toggle { display: inline-flex; align-items: center; gap: 8px; font-weight: 500; cursor: pointer; }
.toggle input { width: 18px; height: 18px; accent-color: var(--air-red); }
.row-wrap { display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; }
.row-wrap.end { justify-content: flex-end; }
.two { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
@media (max-width: 420px) { .two { grid-template-columns: minmax(0, 1fr); } .range { grid-template-columns: 1fr; gap: 4px; } }
.note { margin: 0; color: var(--muted); font-size: 13.5px; }

/* ---------- selection tools ---------- */
.selection { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; }
.sel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.sel-head strong { font: 700 16px var(--display); }
.sel-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.sel-body, .sel-group { display: flex; flex-direction: column; gap: 10px; }

/* ---------- panel ---------- */
.panel { min-width: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; overflow: clip; }
.steps { display: grid; grid-template-columns: repeat(3, 1fr); border-bottom: 1px solid var(--line); }
.step-tab {
  border: 0; background: transparent; padding: 14px 8px; cursor: pointer; font-weight: 600; color: var(--muted);
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; border-bottom: 3px solid transparent;
}
.step-tab b { width: 22px; height: 22px; border-radius: 999px; display: inline-grid; place-items: center; font-size: 12px; border: 1.5px solid currentColor; }
.step-tab.on { color: var(--fg); border-bottom-color: var(--air-red); }
.step-tab.on b { background: var(--air-red); border-color: var(--air-red); color: #fff; }
.step { display: flex; flex-direction: column; padding: 6px 18px 12px; }
.group { border-bottom: 1px solid var(--line); padding-block: 4px; }
.group:last-of-type { border-bottom: 0; }
.group > summary {
  list-style: none; cursor: pointer; padding: 12px 0; font: 700 16px var(--display);
  display: flex; justify-content: space-between; align-items: center;
}
.group > summary::-webkit-details-marker { display: none; }
.group > summary::after { content: "+"; font: 400 22px var(--body); color: var(--muted); }
.group[open] > summary::after { content: "–"; }
.group[open] { padding-bottom: 16px; }
.group[open] > :not(summary) + :not(summary) { margin-top: 14px; }
.group.static { padding-block: 16px; display: flex; flex-direction: column; gap: 12px; }
.group.static h2 { margin: 0; font: 700 18px var(--display); }
.step-nav {
  display: flex; justify-content: space-between; gap: 10px; padding: 12px 18px calc(14px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--line); background: var(--surface);
  position: sticky; bottom: 0; z-index: 5; border-radius: 0 0 14px 14px;
}

.templates { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.tpl { padding: 0; border: 2px solid transparent; border-radius: 6px; background: none; cursor: pointer; line-height: 0; }
.tpl canvas { width: 100%; aspect-ratio: 3 / 2; border-radius: 4px; box-shadow: 0 1px 3px rgba(0, 0, 0, .2); }
.tpl.on { border-color: var(--fg); }

.tray { display: flex; flex-wrap: wrap; gap: 6px; }
.emo {
  width: 46px; height: 46px; display: grid; place-items: center; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface-2); cursor: pointer; padding: 4px; font-size: 26px; line-height: 1;
  font-family: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", sans-serif;
}
.emo img { width: 100%; height: 100%; object-fit: contain; }
.emo:hover { border-color: var(--line-strong); transform: translateY(-1px); }
.emo.on { border-color: var(--fg); box-shadow: 0 0 0 1px var(--fg); }

.layouts { display: grid; grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); gap: 8px; }
.lay { border: 1px solid var(--line); background: var(--surface-2); border-radius: 8px; padding: 6px; cursor: pointer; display: flex; flex-direction: column; gap: 4px; align-items: stretch; }
.lay:hover { border-color: var(--line-strong); }
.lay .thumb { position: relative; aspect-ratio: 3 / 2; background: var(--bg); border-radius: 3px; }
.lay .thumb i { position: absolute; background: var(--muted); opacity: .45; border-radius: 1px; }
.lay small { font-size: 11.5px; color: var(--muted); text-align: center; }

.fonts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.font-opt { border: 1px solid var(--line); background: var(--surface-2); border-radius: 8px; padding: 8px 10px; cursor: pointer; text-align: left; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.font-opt .sample { font-size: 22px; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.font-opt small { font-size: 11.5px; color: var(--muted); }
.font-opt.on { border-color: var(--fg); box-shadow: 0 0 0 1px var(--fg); }

.downloads { display: flex; flex-wrap: wrap; gap: 8px; }
.confirm { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 10px; border-radius: 8px; background: var(--bg); }

/* ---------- emoji dialog ---------- */
dialog {
  width: min(560px, calc(100vw - 24px)); max-height: min(640px, calc(100dvh - 40px));
  border: 1px solid var(--line); border-radius: 14px; padding: 16px; background: var(--surface); color: var(--fg);
  display: none; flex-direction: column; gap: 10px; box-shadow: var(--shadow);
}
dialog[open] { display: flex; }
dialog::backdrop { background: rgba(10, 10, 20, .45); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; }
.dlg-head h2 { margin: 0; font: 700 18px var(--display); }
.cats { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; scrollbar-width: thin; }
.cats .chip { white-space: nowrap; }
.emoji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(46px, 1fr)); gap: 4px; overflow-y: auto; min-height: 200px; flex: 1; align-content: start; }
.emoji-grid .emo { width: auto; aspect-ratio: 1; height: auto; border-color: transparent; background: transparent; }
.emoji-grid .emo:hover { background: var(--surface-2); border-color: var(--line); }
.dlg-foot { font-size: 12px; }

.foot { padding-block: 10px 30px; color: var(--muted); font-size: 13px; }
.foot p { margin: 0; }
.foot .credit { margin-top: 6px; display: flex; flex-wrap: wrap; align-items: center; gap: 4px 0; }
.foot .links { display: inline-flex; flex-wrap: wrap; }
.foot .links a::before { content: "·"; display: inline-block; margin: 0 8px; color: var(--muted); text-decoration: none; }
.foot .links a { color: var(--fg); text-decoration: underline; text-decoration-color: var(--line-strong); text-underline-offset: 3px; }
.foot .links a:hover { text-decoration-color: currentColor; }
@media (max-width: 480px) {
  .foot .credit { flex-direction: column; align-items: flex-start; }
  .foot .links a:first-child::before { content: none; margin: 0; }
}
