:root {
  --bg: #fbf3f7;
  --card: #ffffff;
  --line: #f0d9e5;
  --pink: #b83280;
  --pink-dark: #831c5b;
  --pink-soft: #fde8f3;
  --text: #1a202c;
  --muted: #5a6778;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  position: fixed;
  inset: 0;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
body { display: flex; flex-direction: column; height: 100dvh; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }

#topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--card);
  border-bottom: 1px solid var(--line);
  overflow-x: auto;
  scrollbar-width: none;
  touch-action: pan-x;
}
#topbar::-webkit-scrollbar { display: none; }
#brand { display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: 18px; white-space: nowrap; }
#stitches { flex: 1; display: flex; gap: 6px; }
.stitch-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 96px;
  padding: 4px 10px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: #fff;
  line-height: 1.2;
  white-space: nowrap;
}
.stitch-btn b { font-size: 14px; }
.stitch-btn small { font-size: 11px; color: var(--muted); font-weight: 600; }
.stitch-btn .badge { color: #d69e2e; letter-spacing: 1px; }
.stitch-btn .badge .off { color: #e2e8f0; }
.stitch-btn.active { border-color: var(--pink); background: var(--pink-soft); }
#top-right { display: flex; align-items: center; gap: 8px; }
.seg { display: flex; border: 2px solid var(--line); border-radius: 12px; overflow: hidden; }
.seg-btn { padding: 8px 12px; border: 0; background: #fff; font-weight: 700; font-size: 14px; white-space: nowrap; }
.seg-btn.active { background: var(--pink); color: #fff; }
.icon-btn { width: 42px; height: 42px; border: 1px solid var(--line); border-radius: 10px; background: #fff; font-size: 20px; }

#stage { position: relative; flex: 1 1 auto; min-height: 0; }
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

#instruction {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  width: min(94%, 560px);
  padding: 10px 16px;
  border-radius: 14px;
  background: rgba(255, 255, 255, .94);
  box-shadow: 0 4px 16px rgba(0, 0, 0, .08);
  font-size: 17px;
  font-weight: 700;
  text-align: center;
  line-height: 1.35;
  pointer-events: none;
}
#instruction .sub { display: block; margin-top: 2px; font-size: 14px; font-weight: 600; color: var(--muted); }
#instruction .up { color: #2f855a; }
#instruction .down { color: #c05621; }
#instruction.oops { animation: shake .3s; background: #fff5f5; }
@keyframes shake {
  0%, 100% { transform: translateX(-50%); }
  25% { transform: translateX(calc(-50% - 8px)); }
  75% { transform: translateX(calc(-50% + 8px)); }
}

#result {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  width: min(94%, 480px);
  padding: 14px 18px;
  border-radius: 18px;
  background: #fff;
  border: 3px solid #2f855a;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .15);
  text-align: center;
  animation: pop .25s ease-out;
}
#result .stars { font-size: 30px; color: #d69e2e; letter-spacing: 4px; }
#result .stars .off { color: #e2e8f0; }
#result p { margin: 6px 0 10px; font-size: 16px; font-weight: 600; line-height: 1.4; }
#result .tip { font-size: 14px; color: var(--muted); font-weight: 600; }
#result .row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
#result button { padding: 10px 14px; border: 0; border-radius: 12px; background: #edf2f7; font-weight: 800; }
#result button.primary { background: var(--pink); color: #fff; }
@keyframes pop { from { transform: translateX(-50%) scale(.92); opacity: 0; } to { transform: translateX(-50%); opacity: 1; } }

#toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--card);
  border-top: 1px solid var(--line);
  overflow-x: auto;
  scrollbar-width: none;
  touch-action: pan-x;
}
#toolbar::-webkit-scrollbar { display: none; }
.group { display: flex; gap: 6px; align-items: center; }
.sep { width: 1px; height: 40px; background: var(--line); margin: 0 4px; }
.swatch {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 3px solid #fff;
  box-shadow: 0 0 0 2px var(--line);
}
.swatch.active { box-shadow: 0 0 0 3px var(--text); }
.tool {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 72px;
  height: 56px;
  padding: 4px 10px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: #fff;
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}
.tool .emoji { font-size: 20px; line-height: 1; }
.tool.active { border-color: var(--pink); background: var(--pink-soft); }
#counter { margin-left: auto; font-weight: 800; color: var(--muted); white-space: nowrap; }

#modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(26, 32, 44, .45);
  z-index: 50;
}
#modal[hidden] { display: none; }
#modal-card {
  width: min(100%, 540px);
  max-height: 90dvh;
  overflow-y: auto;
  touch-action: pan-y;
  padding: 20px 22px;
  border-radius: 18px;
  background: #fff;
}
#modal-card h2 { margin: 0 0 10px; }
#modal-body { font-size: 16px; line-height: 1.45; }
#modal-body ul { list-style: none; padding: 0; margin: 0; }
#modal-body li { display: flex; gap: 10px; margin-bottom: 10px; }
#modal-body li .ico { flex: 0 0 28px; font-size: 22px; text-align: center; }
.go {
  display: block;
  margin: 14px auto 0;
  height: 48px;
  padding: 0 28px;
  border: 0;
  border-radius: 14px;
  background: var(--pink);
  color: #fff;
  font-size: 17px;
  font-weight: 800;
  box-shadow: 0 3px 0 var(--pink-dark);
}

@media (max-width: 900px) {
  #brand span { display: none; }
  .stitch-btn { min-width: 80px; }
  .stitch-btn small:not(.badge) { display: none; }
}
