:root {
  --blue: #1f6bff;
  --blue-dark: #1246c9;
  --sky: #bfe4ff;
  --sky-light: #e6f4ff;
  --ink: #17223b;
  --muted: #6b7a99;
  --white: #fff;
  --yellow: #ffe56b;
  --pink: #ff3d7f;
  --radius: 22px;
  --shadow: 0 10px 30px rgba(18, 70, 201, 0.18);
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: 'Fredoka', 'Nunito', system-ui, -apple-system, 'Segoe UI', sans-serif;
  color: var(--ink);
  background: linear-gradient(180deg, #d6ecff 0%, #eef6ff 100%);
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
button { font: inherit; cursor: pointer; }
img { display: block; }

#app {
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  position: relative;
  z-index: 2;
}
#app[hidden] { display: none; }

/* ---------- wall ---------- */
#wall { position: fixed; inset: 0; z-index: 0; background: #8fd3ff; }
#wall canvas { display: block; width: 100%; height: 100%; }
.wall-title {
  position: absolute; left: 24px; top: 18px; font-weight: 700; font-size: 22px; color: var(--white);
  text-shadow: 0 2px 0 rgba(0,0,0,.15); letter-spacing: .3px; pointer-events: none;
}
.fab {
  position: absolute; right: max(20px, env(safe-area-inset-right)); bottom: max(20px, env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: 12px;
  border: 0; border-radius: 999px; padding: 12px 24px 12px 12px;
  background: var(--blue); color: var(--white); font-size: 20px; font-weight: 700;
  box-shadow: 0 6px 0 var(--blue-dark), 0 14px 30px rgba(18,70,201,.35);
  transition: transform .08s, box-shadow .08s;
}
.fab .plus { width: 44px; height: 44px; border-radius: 50%; background: var(--white); color: var(--blue); display: grid; place-items: center; font-size: 34px; line-height: 1; }
.fab:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--blue-dark); }
.fab[hidden] { display: none; }
body.creator-open #wall::after { content: ''; position: absolute; inset: 0; background: rgba(10, 30, 80, .45); backdrop-filter: blur(4px); }
body.creator-open .fab { display: none; }
.icon-btn.small { width: 40px; height: 40px; }
.icon-btn.small svg { width: 20px; height: 20px; }
.screen { display: none; flex: 1; min-height: 0; flex-direction: column; }
.screen.active { display: flex; }

/* ---------- buttons ---------- */
.btn {
  border: 0;
  border-radius: 999px;
  padding: 14px 28px;
  font-size: 18px;
  font-weight: 600;
  color: var(--white);
  background: var(--blue);
  box-shadow: 0 6px 0 var(--blue-dark), var(--shadow);
  transition: transform .08s, box-shadow .08s;
}
.btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--blue-dark); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.ghost { background: var(--white); color: var(--blue); box-shadow: 0 6px 0 #c9daf5; }
.btn.ghost:active { box-shadow: 0 2px 0 #c9daf5; }
.icon-btn {
  width: 52px; height: 52px;
  border: 0; border-radius: 50%;
  background: var(--white);
  color: var(--blue);
  box-shadow: var(--shadow);
  display: grid; place-items: center;
  font-size: 22px;
}
.icon-btn:disabled { opacity: .35; }
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- start ---------- */

/* ---------- create ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px 4px;
  gap: 8px;
}
.topbar h2 { margin: 0; font-size: 20px; color: var(--blue); white-space: nowrap; }
.code { display: flex; align-items: center; gap: 6px; }
#style-code {
  font-family: ui-monospace, Menlo, monospace;
  font-size: 10px; letter-spacing: 0;
  background: rgba(255,255,255,.7);
  border-radius: 8px; padding: 4px 8px;
  cursor: copy; transition: background .2s;
}
#style-code.copied { background: var(--yellow); }
#style-load { border: 0; background: transparent; color: var(--blue); font-size: 12px; text-decoration: underline; padding: 0; }

.stage { flex: 0 0 44%; position: relative; min-height: 220px; }
.stage-canvas { position: absolute; inset: 8px 16px 0; border-radius: var(--radius); overflow: hidden;
  background: radial-gradient(circle at 50% 30%, #ffffff 0%, #d9ecff 70%); box-shadow: inset 0 0 0 4px rgba(255,255,255,.6); }
.stage-canvas canvas { width: 100%; height: 100%; display: block; }
.stage-canvas.bounce canvas { animation: bounce .45s cubic-bezier(.3,1.6,.5,1); }
@keyframes bounce { 0% { transform: scale(1); } 35% { transform: scale(1.06, .94); } 70% { transform: scale(.97, 1.03); } 100% { transform: scale(1); } }
.stage-actions { position: absolute; right: 24px; top: 16px; display: flex; flex-direction: column; gap: 10px; }
.stage-next { position: absolute; left: 24px; bottom: 12px; }
.stage-next .btn { padding: 10px 20px; font-size: 16px; }

.editor {
  flex: 1; min-height: 0;
  margin-top: 12px;
  background: var(--white);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: 0 -8px 30px rgba(18,70,201,.10);
  display: flex; flex-direction: column;
}
.tabs {
  display: flex; gap: 4px; overflow-x: auto; padding: 10px 10px 4px; scrollbar-width: none;
  border-bottom: 1px solid #edf2fb;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto; width: 66px; border: 0; background: transparent; border-radius: 14px;
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px 0 6px;
  color: var(--muted); font-size: 11px; font-weight: 600;
}
.tab .ico { width: 44px; height: 44px; object-fit: contain; }
.tab .ico.on { display: none; }
.tab.active { color: var(--blue); background: var(--sky-light); }
.tab.active .ico.on { display: block; }
.tab.active .ico.off { display: none; }

.palette {
  display: flex; gap: 10px; overflow-x: auto; padding: 10px 14px 4px; scrollbar-width: none;
}
.palette::-webkit-scrollbar { display: none; }
.palette[hidden] { display: none; }
.swatch {
  flex: 0 0 34px; width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--white);
  background: var(--c); box-shadow: 0 0 0 2px #dfe7f5;
}
.swatch.selected { box-shadow: 0 0 0 3px var(--blue); transform: scale(1.1); }

.grid {
  flex: 1; min-height: 0; overflow-y: auto;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: 10px;
  padding: 12px 14px 24px; align-content: start; grid-auto-rows: max-content;
}
.item {
  display: block; border: 3px solid transparent; border-radius: 18px;
  background: var(--sky-light); padding: 0; overflow: hidden; position: relative;
}
.item .sq { position: relative; width: 100%; aspect-ratio: 1; }
.item .sq > * { position: absolute; inset: 0; width: 100%; height: 100%; }
.item.selected { border-color: var(--blue); background: var(--sky); }
.item.skin { background: var(--c); }
.item.skin.selected { box-shadow: inset 0 0 0 4px var(--white); }
.item.bg { background-size: cover; background-position: center; }
.item.none .sq::after { content: '∅'; position: absolute; inset: 0; display: grid; place-items: center; font-size: 30px; color: var(--muted); }

/* ---------- forms ---------- */
.form-screen { padding: 24px 20px; overflow-y: auto; gap: 12px; justify-content: center; }
.form-screen h2 { margin: 0; font-size: 28px; color: var(--blue); }
.form-screen p.lead { margin: 0 0 8px; color: var(--muted); }
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 14px; font-weight: 600; color: var(--muted); }
.field input, .field select {
  font: inherit; font-size: 16px; padding: 12px 14px; border-radius: 14px;
  border: 2px solid #dfe7f5; background: var(--white); color: var(--ink); width: 100%;
}
.field input:focus { outline: 0; border-color: var(--blue); }
.alert { color: var(--muted); font-size: 14px; min-height: 18px; }
.alert:not(:empty).error { color: var(--pink); }
.actions { display: flex; gap: 10px; justify-content: space-between; margin-top: auto; padding-top: 12px; }
.avatar-preview { width: min(46vw, 210px); aspect-ratio: 1; border-radius: 50%; background: var(--white); box-shadow: var(--shadow); overflow: hidden; align-self: center; margin: 4px 0 8px; }
.avatar-preview canvas, #done-avatar { width: 100%; height: 100%; display: block; }

/* ---------- done ---------- */
#screen-done { align-items: center; justify-content: center; text-align: center; padding: 24px; gap: 14px; background: linear-gradient(180deg, #d6ecff 0%, #eef6ff 100%); }
.form-screen, #screen-create { background: linear-gradient(180deg, #d6ecff 0%, #eef6ff 100%); }
#screen-done h2 { font-size: 34px; margin: 0; color: var(--blue); }
#done-avatar-wrap { width: min(60vw, 280px); aspect-ratio: 1; border-radius: 50%; background: var(--white); box-shadow: var(--shadow); overflow: hidden; animation: cheer 1.2s ease-in-out infinite; }
@keyframes cheer { 0%, 100% { transform: translateY(0) rotate(0); } 25% { transform: translateY(-10px) rotate(-3deg); } 75% { transform: translateY(-10px) rotate(3deg); } }
#cooldown { color: var(--muted); }

.fatal { position: fixed; inset: auto 0 0; margin: 0; background: #fee; color: #900; padding: 12px; }

/* ---------- desktop ---------- */
@media (min-width: 900px) {
  #app { max-width: 1100px; height: calc(100dvh - 40px); margin: 20px auto; border-radius: 28px; overflow: hidden; box-shadow: 0 30px 80px rgba(0,0,0,.35); }
  .form-screen, #screen-done { max-width: 560px; margin: 0 auto; width: 100%; }
  .form-screen { border-radius: 0; }
  #screen-create.active { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); padding: 16px; gap: 16px; }
  #screen-create .editor { min-width: 0; }
  #screen-create .topbar { grid-column: 1 / -1; }
  #screen-create .stage { flex: none; min-height: 0; }
  #screen-create .stage-canvas { inset: 0; }
  #screen-create .editor { margin-top: 0; border-radius: var(--radius); min-height: 0; }
  #style-code { font-size: 12px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
}
