:root {
  --ink: #3d2b56;
  --ink-soft: #6b5a85;
  --pink: #ff6fb5;
  --pink-dark: #e04d97;
  --blue: #4da3ff;
  --blue-dark: #2f83e0;
  --green: #3fc380;
  --green-dark: #2a9e63;
  --sun: #ffd23f;
  --card: #ffffff;
  --bg1: #bfe6ff;
  --bg2: #fde2f3;
  --radius: 22px;
  --shadow: 0 6px 0 rgba(61, 43, 86, 0.15), 0 12px 30px rgba(61, 43, 86, 0.12);
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  font-family: "Fredoka", "Baloo 2", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: linear-gradient(160deg, var(--bg1), var(--bg2));
  user-select: none; -webkit-user-select: none;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; border: 0; }
input { font: inherit; color: var(--ink); user-select: text; -webkit-user-select: text; }
.hidden { display: none !important; }

.screen { position: fixed; inset: 0; display: flex; flex-direction: column; }

/* ---------- shared bits ---------- */
.card { background: var(--card); border-radius: 32px; box-shadow: var(--shadow); }
.big-btn {
  display: block; width: 100%; padding: 16px 20px; margin: 10px 0;
  border-radius: var(--radius); font-size: 1.3rem; font-weight: 700; color: #fff;
  background: var(--blue); box-shadow: 0 6px 0 var(--blue-dark);
  transition: transform .08s, box-shadow .08s;
}
.big-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--blue-dark); }
.big-btn.pink { background: var(--pink); box-shadow: 0 6px 0 var(--pink-dark); }
.big-btn.pink:active { box-shadow: 0 2px 0 var(--pink-dark); }
.big-btn.green { background: var(--green); box-shadow: 0 6px 0 var(--green-dark); }
.big-btn.green:active { box-shadow: 0 2px 0 var(--green-dark); }
.big-btn:disabled { opacity: .6; }
.link-btn { background: none; color: var(--ink-soft); font-size: 1rem; padding: 8px; margin-top: 4px; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 10px 14px; min-height: 46px;
  border-radius: 999px; background: #fff; font-size: 1.05rem; font-weight: 600;
  box-shadow: 0 3px 0 rgba(61,43,86,.15);
}
.pill:active { transform: translateY(2px); box-shadow: none; }
.pill.on { background: var(--sun); }
.icon-btn { font-size: 1.8rem; background: #fff3c4; border-radius: 16px; width: 56px; height: 56px; flex: none; box-shadow: 0 4px 0 #f0c94a; }
.icon-btn:active { transform: translateY(3px); box-shadow: none; }
.spacer { flex: 1; }
.error { color: #e53950; min-height: 1.3em; margin: 6px 0 0; font-weight: 600; }
.hint { color: var(--ink-soft); margin: 4px 0 10px; font-size: .95rem; }

/* ---------- auth ---------- */
#screen-auth { align-items: center; justify-content: center; padding: 16px; overflow-y: auto; }
.auth-card { width: min(460px, 100%); padding: 24px 24px 18px; text-align: center; position: relative; z-index: 1; max-height: 100%; overflow-y: auto; }
.logo { font-size: clamp(2.2rem, 7vw, 3rem); margin: 0 0 6px; line-height: 1; letter-spacing: .5px; }
.logo span { color: var(--pink); }
.lead { font-size: 1.2rem; margin: 6px 0 14px; }
.auth-panel h2 { font-size: 1.25rem; margin: 14px 0 6px; }
.auth-panel input {
  width: 100%; font-size: 1.4rem; font-weight: 600; text-align: center; padding: 12px;
  border-radius: 16px; border: 3px solid #e3d9f2; outline: none; background: #fbf8ff;
}
.auth-panel input:focus { border-color: var(--pink); }
.name-row { display: flex; gap: 10px; }
.pic-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.pic {
  font-size: 2rem; aspect-ratio: 1; border-radius: 18px; background: #f3eefc;
  box-shadow: 0 4px 0 #ddd2ef; display: grid; place-items: center;
}
.pic:active { transform: translateY(3px); box-shadow: none; }
.code-slots { display: flex; justify-content: center; gap: 10px; margin: 6px 0 12px; }
.slot {
  width: 58px; height: 58px; border-radius: 18px; border: 3px dashed #cbbde3; font-size: 2rem;
  display: grid; place-items: center; background: #fff;
}
.slot.filled { border-style: solid; border-color: var(--pink); background: #fff0f8; animation: pop .25s; }
.device-players { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-bottom: 6px; }
.device-players:empty { display: none; }
.device-player { background: #fff3c4; padding: 8px 14px; border-radius: 999px; font-weight: 600; box-shadow: 0 3px 0 #f0c94a; }
.sky-deco span { position: fixed; font-size: 2.6rem; animation: drift 9s ease-in-out infinite; opacity: .8; }
.sky-deco span:nth-child(1) { left: 6%; top: 12%; }
.sky-deco span:nth-child(2) { right: 8%; top: 18%; animation-delay: -2s; }
.sky-deco span:nth-child(3) { left: 12%; bottom: 14%; animation-delay: -4s; }
.sky-deco span:nth-child(4) { right: 14%; bottom: 10%; animation-delay: -6s; }
.sky-deco span:nth-child(5) { left: 48%; top: 4%; animation-delay: -3s; }

@keyframes drift { 0%,100% { transform: translate(0,0) rotate(-8deg); } 50% { transform: translate(18px,-22px) rotate(8deg); } }
@keyframes pop { 0% { transform: scale(.6); } 70% { transform: scale(1.12); } 100% { transform: scale(1); } }

/* ---------- top bars ---------- */
.topbar { display: flex; align-items: center; gap: 8px; padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top)); flex-wrap: wrap; }
.hello { font-size: 1.3rem; }

/* ---------- home ---------- */
.home-body { flex: 1; overflow-y: auto; padding: 4px 16px 30px; max-width: 1100px; width: 100%; margin: 0 auto; }
.home-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.home-actions .big-btn { margin: 0; font-size: 1.25rem; padding: 20px; }
.section-title { font-size: 1.6rem; margin: 24px 4px 10px; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 14px; }
.bfly-card { background: #fff; border-radius: var(--radius); padding: 10px; box-shadow: var(--shadow); text-align: center; transition: transform .12s; }
.bfly-card:hover { transform: translateY(-3px) rotate(-1deg); }
.bfly-card img { width: 100%; aspect-ratio: 4/3; object-fit: contain; animation: hover-flap 3s ease-in-out infinite; }
.bfly-card .n { font-weight: 700; font-size: 1.15rem; margin-top: 4px; }
.bfly-card .d { color: var(--ink-soft); font-size: .85rem; }
.empty { text-align: center; font-size: 1.2rem; color: var(--ink-soft); padding: 30px; }
@keyframes hover-flap { 0%,100% { transform: translateY(0) scaleX(1); } 50% { transform: translateY(-6px) scaleX(.9); } }
@media (max-width: 520px) { .home-actions { grid-template-columns: 1fr; } .gallery { grid-template-columns: repeat(2, 1fr); } }

/* ---------- paint ---------- */
.paint-top .bname { display: flex; align-items: center; gap: 6px; font-weight: 600; background: #fff; border-radius: 999px; padding: 4px 6px 4px 14px; box-shadow: 0 3px 0 rgba(61,43,86,.15); }
.paint-top .bname input { border: 0; outline: none; width: 9em; font-size: 1.05rem; font-weight: 600; background: #f6f1ff; border-radius: 999px; padding: 8px 12px; }
.release-btn {
  font-size: 1.3rem; font-weight: 700; color: #fff; padding: 12px 22px; border-radius: 999px;
  background: linear-gradient(90deg, #ff6fb5, #a855f7, #4da3ff); box-shadow: 0 5px 0 #7e3fbf;
  animation: glow 2.4s ease-in-out infinite;
}
.release-btn:active { transform: translateY(4px); box-shadow: 0 1px 0 #7e3fbf; }
@keyframes glow { 0%,100% { filter: brightness(1); } 50% { filter: brightness(1.15); } }

.paint-body { flex: 1; min-height: 0; display: flex; gap: 10px; padding: 0 10px 10px; padding-bottom: max(10px, env(safe-area-inset-bottom)); }
.tools { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-content: start; overflow-y: auto; padding: 4px; }
.tool {
  width: 62px; height: 62px; border-radius: 18px; background: #fff; font-size: 1.8rem;
  box-shadow: 0 4px 0 rgba(61,43,86,.15); display: grid; place-items: center; position: relative;
}
.tool.on { background: var(--sun); box-shadow: 0 4px 0 #e0b21f; transform: scale(1.05); }
.tool .tl { position: absolute; bottom: 2px; font-size: .55rem; font-weight: 600; color: var(--ink-soft); }
.stage-wrap { flex: 1; min-width: 0; min-height: 0; container-type: size; display: grid; place-items: center; }
.stage {
  position: relative; width: min(100cqw, calc(100cqh * 4 / 3)); aspect-ratio: 4 / 3;
  border-radius: 26px; background: radial-gradient(circle at 50% 45%, #f6f0ff, #e7defa);
  box-shadow: inset 0 0 0 4px #fff, var(--shadow); overflow: hidden;
}
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }
.side { display: flex; flex-direction: column; gap: 10px; overflow-y: auto; padding: 4px; }
.sizes { display: flex; gap: 6px; justify-content: center; }
.size { width: 46px; height: 46px; border-radius: 50%; background: #fff; box-shadow: 0 3px 0 rgba(61,43,86,.15); display: grid; place-items: center; }
.size i { display: block; background: var(--ink); border-radius: 50%; }
.size.on { background: var(--sun); }
.colors { display: grid; grid-template-columns: repeat(3, 46px); gap: 8px; }
.color { width: 46px; height: 46px; border-radius: 50%; box-shadow: inset 0 -4px 0 rgba(0,0,0,.15), 0 2px 0 rgba(61,43,86,.15); border: 3px solid #fff; }
.color.on { transform: scale(1.18); border-color: var(--ink); }

@media (orientation: portrait), (max-width: 760px) {
  .paint-body { flex-direction: column; }
  .stage-wrap { order: 0; }
  .tools { order: 1; display: flex; grid-template-columns: none; overflow-x: auto; overflow-y: hidden; flex: none; padding: 4px 2px 8px; }
  .tool { flex: none; width: 56px; height: 56px; font-size: 1.6rem; }
  .side { order: 2; flex-direction: column; align-items: stretch; overflow: visible; flex: none; gap: 8px; }
  .colors { grid-template-columns: repeat(9, 1fr); gap: 6px; max-width: 560px; width: 100%; margin: 0 auto; }
  .color { width: 100%; height: auto; aspect-ratio: 1; border-width: 2px; }
  .size { flex: none; }
  .paint-top .lbl { display: none; }
  .paint-top .bname input { width: 6.5em; }
  .release-btn { font-size: 1.1rem; padding: 10px 16px; }
}

/* ---------- world ---------- */
#screen-world { background: #bfe6ff; }
.world-host { position: absolute; inset: 0; }
.world-host canvas { width: 100%; height: 100%; display: block; }
.hud { position: absolute; inset: 0; pointer-events: none; display: flex; flex-direction: column; justify-content: space-between; padding: 12px; padding-top: max(12px, env(safe-area-inset-top)); padding-bottom: max(14px, env(safe-area-inset-bottom)); }
.hud button, .hud .joy, .hud .pill { pointer-events: auto; }
.hud-top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.status { background: rgba(255,255,255,.85); padding: 10px 16px; border-radius: 999px; font-weight: 600; font-size: 1.05rem; }
.status:empty { display: none; }
.counter { pointer-events: none !important; }
.back-mine { align-self: center; margin-top: 8px; }
.hud-bottom { display: flex; align-items: flex-end; justify-content: space-between; gap: 10px; margin-top: auto; }
.modes { display: flex; gap: 8px; background: rgba(255,255,255,.7); padding: 6px; border-radius: 999px; margin: 0 auto; }
.mode-btn { padding: 12px 18px; border-radius: 999px; font-weight: 700; font-size: 1.1rem; background: transparent; pointer-events: auto; }
.mode-btn.on { background: var(--pink); color: #fff; box-shadow: 0 4px 0 var(--pink-dark); }
.joy-wrap { position: absolute; left: max(18px, env(safe-area-inset-left)); bottom: max(86px, calc(env(safe-area-inset-bottom) + 76px)); }
.joy { width: 140px; height: 140px; border-radius: 50%; background: rgba(255,255,255,.35); border: 4px solid rgba(255,255,255,.8); position: relative; touch-action: none; }
.knob { position: absolute; left: 50%; top: 50%; width: 62px; height: 62px; margin: -31px 0 0 -31px; border-radius: 50%; background: #fff; box-shadow: 0 4px 10px rgba(0,0,0,.2); pointer-events: none; }
.updown { position: absolute; right: max(18px, env(safe-area-inset-right)); bottom: max(86px, calc(env(safe-area-inset-bottom) + 76px)); display: flex; flex-direction: column; gap: 12px; }
.ud { width: 74px; height: 74px; border-radius: 50%; font-size: 2rem; background: rgba(255,255,255,.85); box-shadow: 0 4px 0 rgba(61,43,86,.2); touch-action: none; }
.ud:active { transform: translateY(3px); box-shadow: none; }
.keys-hint { position: absolute; bottom: 82px; left: 50%; transform: translateX(-50%); background: rgba(61,43,86,.6); color: #fff; padding: 6px 14px; border-radius: 999px; font-size: .85rem; white-space: nowrap; }
@media (max-width: 760px) { .keys-hint { display: none; } .mode-btn { padding: 10px 12px; font-size: 1rem; } .status { font-size: .9rem; } }

/* ---------- modals & toasts ---------- */
.modal { position: fixed; inset: 0; background: rgba(61,43,86,.35); display: grid; place-items: center; z-index: 20; padding: 16px; }
.modal-card { width: min(380px, 100%); padding: 20px; text-align: center; animation: pop .25s; }
.modal-card img { width: 100%; aspect-ratio: 4/3; object-fit: contain; animation: hover-flap 2.4s ease-in-out infinite; }
.modal-card h2 { margin: 6px 0 8px; font-size: 1.6rem; }
.releasing { text-align: center; color: #fff; font-size: 1.4rem; font-weight: 600; }
.flutter { font-size: 6rem; animation: flutter .5s ease-in-out infinite alternate; }
@keyframes flutter { from { transform: scaleX(1) translateY(0); } to { transform: scaleX(.6) translateY(-10px); } }
.toasts { position: fixed; top: max(74px, calc(env(safe-area-inset-top) + 64px)); left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 30; pointer-events: none; width: max-content; max-width: 92vw; }
.toast { background: #fff; padding: 12px 20px; border-radius: 999px; font-weight: 600; font-size: 1.1rem; box-shadow: var(--shadow); animation: toast-in .3s ease-out; text-align: center; }
.toast.out { opacity: 0; transform: translateY(-10px); transition: all .4s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-14px) scale(.9); } to { opacity: 1; transform: none; } }
