:root {
  --bg: #fff7ec;
  --panel: #ffffff;
  --ink: #4a2f55;
  --muted: #85758f;
  --line: #eedfe6;
  --accent: #8a6fe0;
  --green: #3fa97a;
  --green-dark: #2b7d59;
  --red: #e05584;
  --yellow: #ffd36e;
  --radius: 18px;
  --shadow: 0 4px 0 rgba(74, 47, 85, 0.14);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: 'Nunito', 'Comic Neue', system-ui, sans-serif;
  font-weight: 600;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  overflow: hidden;
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
canvas { image-rendering: pixelated; image-rendering: crisp-edges; }
[hidden] { display: none !important; }

/* Rótulos de texto só aparecem no modo grande */
.label { display: none; }
body.modo-grande .label { display: inline; }

/* ---------- Botões ---------- */
.big-btn {
  border: 0;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font-size: 28px;
  font-weight: 800;
  padding: 18px 34px;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.18);
  transition: transform 0.08s;
}
.big-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0, 0, 0, 0.18); }
.big-btn.green { background: var(--green); }
.big-btn.small { font-size: 18px; padding: 12px 16px; width: 100%; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 2px solid var(--line);
  background: var(--panel);
  border-radius: 999px;
  padding: 10px 16px;
  font-size: 17px;
  font-weight: 800;
  min-height: 48px;
  box-shadow: var(--shadow);
}
.chip.green { background: var(--green); border-color: var(--green-dark); color: #fff; }
.chip.danger { color: #c0003a; }

.icon-btn {
  border: 0;
  background: var(--panel);
  border-radius: 14px;
  width: 52px;
  height: 52px;
  font-size: 26px;
  box-shadow: var(--shadow);
}
.icon-btn.small { width: 44px; height: 44px; font-size: 20px; }

/* ---------- Tela inicial ---------- */
#home {
  height: 100%;
  overflow-y: auto;
  padding: 24px 16px 40px;
  max-width: 960px;
  margin: 0 auto;
}
.home-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.logo { font-size: clamp(30px, 6vw, 48px); font-weight: 800; margin: 0; }
.logo span { color: var(--red); }
.home-new {
  margin: 28px 0;
  padding: 28px 16px;
  background: var(--panel);
  border-radius: 28px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
.tour-check { display: flex; align-items: center; gap: 10px; font-size: 20px; }
.tour-check input { width: 28px; height: 28px; accent-color: var(--green); }
.home-list h2 { font-size: 24px; }
.project-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 14px; }
.project-card {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.project-open { border: 0; background: none; display: flex; flex-direction: column; align-items: center; gap: 8px; font-size: 17px; font-weight: 800; }
.project-open canvas { width: 96px; height: 96px; background: #dff4ff; border-radius: 12px; }
.project-actions { display: flex; justify-content: center; gap: 8px; }
.muted { color: var(--muted); }
.home-footer { margin-top: 28px; }
.home-footer label { cursor: pointer; }

/* ---------- Estúdio ---------- */
#studio { height: 100%; display: flex; flex-direction: column; }
.studio-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--panel);
  border-bottom: 3px solid var(--line);
}
.project-name {
  border: 0;
  background: none;
  font-size: 18px;
  font-weight: 800;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tabs { display: flex; gap: 8px; margin: 0 auto; }
.tab {
  border: 3px solid var(--line);
  background: var(--bg);
  border-radius: 16px;
  min-width: 64px;
  height: 56px;
  padding: 0 14px;
  font-size: 26px;
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.tab .label { font-size: 18px; }
.tab.on { border-color: var(--accent); background: #e2f4ff; }
.tab.play { background: var(--green); border-color: var(--green-dark); color: #fff; }
.tab.play.on { outline: 4px solid var(--yellow); }
.bar-right { display: flex; gap: 8px; }

.view { flex: 1; min-height: 0; display: flex; gap: 12px; padding: 12px; }

/* ---------- Desenhar ---------- */
.tools { display: flex; flex-direction: column; gap: 14px; }
.tool-group { display: flex; flex-direction: column; gap: 8px; }
.tool {
  border: 3px solid transparent;
  background: var(--panel);
  border-radius: 16px;
  min-width: 60px;
  height: 60px;
  font-size: 28px;
  box-shadow: var(--shadow);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 10px;
}
.tool .label { font-size: 15px; }
.tool.on { border-color: var(--accent); background: #e2f4ff; }

.pixel-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 12px; }
.pixel-stage { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; }
#pixel-canvas {
  border-radius: 12px;
  box-shadow: 0 0 0 4px var(--panel), 0 8px 0 4px rgba(29, 43, 83, 0.12);
  touch-action: none;
  cursor: crosshair;
}
.palette { display: grid; grid-template-columns: repeat(8, 1fr); gap: 8px; max-width: 560px; width: 100%; margin: 0 auto; }
.swatch {
  aspect-ratio: 1;
  min-height: 40px;
  border: 3px solid rgba(0, 0, 0, 0.15);
  border-radius: 12px;
}
.swatch.on { border: 4px solid var(--ink); transform: scale(1.08); box-shadow: 0 0 0 3px #fff inset; }

.drawer {
  width: 210px;
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
}
.drawer h3 { margin: 4px 0 0; font-size: 18px; }
.preview-box { display: flex; align-items: center; gap: 10px; }
#pixel-preview { width: 64px; height: 64px; background: #dff4ff; border-radius: 10px; }
.sprite-kind { font-size: 22px; font-weight: 800; }
.sprite-list { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.sprite-card {
  position: relative;
  border: 3px solid var(--line);
  background: #f5f9ff;
  border-radius: 12px;
  padding: 4px;
  aspect-ratio: 1;
}
.sprite-card canvas { width: 100%; height: 100%; }
.sprite-card.on { border-color: var(--accent); background: #e2f4ff; }
.kind-tag { position: absolute; right: -6px; bottom: -6px; font-size: 18px; }
.kind-picker { display: flex; flex-wrap: wrap; gap: 6px; }
.kind-picker .chip { min-height: 44px; padding: 6px 10px; font-size: 22px; }

/* ---------- Montar ---------- */
.level-palette {
  width: 150px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  padding: 4px;
}
.pal-item {
  border: 3px solid var(--line);
  background: var(--panel);
  border-radius: 14px;
  padding: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: var(--shadow);
  min-height: 64px;
}
.pal-item canvas { width: 48px; height: 48px; background: #dff4ff; border-radius: 8px; flex: none; }
.pal-item .big-emoji { width: 48px; font-size: 32px; text-align: center; }
.pal-label { font-size: 20px; font-weight: 800; text-align: left; }
.pal-label .label { font-size: 14px; }
.pal-item.on { border-color: var(--accent); background: #e2f4ff; }
.level-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 10px; }
.level-scroll {
  flex: 1;
  min-height: 0;
  overflow-x: auto;
  overflow-y: hidden;
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  padding: 8px;
}
#level-canvas { display: block; border-radius: 10px; touch-action: none; cursor: pointer; }
.level-actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* ---------- Jogar ---------- */
#view-jogar { justify-content: center; }
.play-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
}
.play-top {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  width: min(100%, 640px);
  min-height: 32px;
}
.play-back { display: none; }
.play-stage {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
}
.hud { font-size: 24px; font-weight: 800; min-height: 32px; }
#game-canvas {
  width: min(100%, calc((100vh - 260px) * 4 / 3));
  aspect-ratio: 4 / 3;
  border-radius: var(--radius);
  box-shadow: 0 0 0 5px var(--panel), 0 10px 0 5px rgba(29, 43, 83, 0.12);
  background: #8fd8ff;
}
.touch-controls { display: flex; justify-content: space-between; width: min(100%, 640px); }
.pad { display: flex; gap: 14px; }
.pad-btn {
  width: 84px;
  height: 84px;
  border-radius: 50%;
  border: 0;
  background: var(--panel);
  font-size: 40px;
  box-shadow: 0 6px 0 rgba(29, 43, 83, 0.18);
  touch-action: none;
}
.pad-btn.on { transform: translateY(4px); box-shadow: 0 2px 0 rgba(29, 43, 83, 0.18); background: #e2f4ff; }
.pad-btn.jump { background: var(--yellow); }
.play-msg {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -60%);
  background: var(--panel);
  border-radius: 24px;
  padding: 20px 28px;
  text-align: center;
  font-size: 20px;
  box-shadow: 0 10px 30px rgba(29, 43, 83, 0.25);
}
.play-msg p { margin: 6px 0; }
.msg-big { font-size: 56px; }
.msg-actions { display: flex; gap: 10px; justify-content: center; margin-top: 12px; flex-wrap: wrap; }

/* ---------- Tour ---------- */
#tour { position: fixed; inset: 0; pointer-events: none; z-index: 50; }
#tour-ring {
  position: fixed;
  border: 5px solid var(--yellow);
  border-radius: 20px;
  box-shadow: 0 0 0 4px rgba(255, 163, 0, 0.6), 0 0 24px 6px rgba(255, 236, 39, 0.8);
  animation: pulse 1.2s ease-in-out infinite;
}
#tour-hand {
  position: fixed;
  font-size: 40px;
  animation: tap 0.9s ease-in-out infinite;
  filter: drop-shadow(0 3px 2px rgba(0, 0, 0, 0.3));
}
#tour-panel {
  position: fixed;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(640px, calc(100% - 32px));
  background: var(--panel);
  border: 4px solid var(--accent);
  border-radius: 24px;
  padding: 12px 16px;
  display: flex;
  gap: 14px;
  align-items: center;
  pointer-events: auto;
  box-shadow: 0 10px 30px rgba(29, 43, 83, 0.25);
}
/* durante o tour, o estúdio deixa espaço embaixo para o balão do Pixo */
body.touring #studio { padding-bottom: 150px; }
body.touring #game-canvas { width: min(100%, calc((100vh - 410px) * 4 / 3)); }
#tour-panel.pop { animation: pop 0.35s ease-out; }
.mascot canvas { width: 80px; height: 80px; animation: bob 1.6s ease-in-out infinite; }
.tour-body { flex: 1; }
#tour-text { margin: 0 0 10px; font-size: 20px; line-height: 1.35; }
.tour-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.tour-buttons .chip { min-height: 44px; font-size: 16px; padding: 6px 12px; }

.toast {
  position: fixed;
  top: max(16px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  border-radius: 999px;
  padding: 12px 22px;
  font-size: 18px;
  z-index: 60;
}

@keyframes pulse { 50% { transform: scale(1.03); opacity: 0.75; } }
@keyframes tap { 50% { transform: translateY(-10px); } }
@keyframes bob { 50% { transform: translateY(-4px); } }
@keyframes pop { from { transform: translateX(-50%) scale(0.9); } to { transform: translateX(-50%) scale(1); } }

/* ---------- Telas estreitas (tablet em pé, celular) ---------- */
@media (max-width: 820px) {
  .studio-bar { flex-wrap: wrap; }
  .tabs { order: 3; width: 100%; justify-content: center; }
  .project-name { flex: 1; }
  #view-desenhar { flex-direction: column; overflow-y: auto; }
  .tools { flex-direction: row; flex-wrap: wrap; justify-content: center; }
  .tool-group { flex-direction: row; }
  .pixel-stage { min-height: 300px; }
  .drawer { width: 100%; }
  .sprite-list { grid-template-columns: repeat(6, minmax(0, 1fr)); }
  #view-montar { flex-direction: column; }
  .level-palette { width: 100%; flex-direction: row; overflow-x: auto; overflow-y: hidden; flex: none; }
  .pal-item { flex: none; }
  #game-canvas { width: min(100%, calc((100vh - 330px) * 4 / 3)); }
  body.touring #game-canvas { width: min(100%, calc((100vh - 480px) * 4 / 3)); }
  .pad-btn { width: 72px; height: 72px; font-size: 34px; }
}

/* ---------- Animações (rodada de "juice") ---------- */
.view:not([hidden]) { animation: viewIn 0.28s ease-out; }
.screen:not([hidden]) { animation: viewIn 0.3s ease-out; }
.tab, .tool, .chip, .icon-btn, .swatch, .pal-item, .sprite-card, .big-btn, .pad-btn, .project-open {
  transition: transform 0.12s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.12s, background-color 0.15s, border-color 0.15s;
}
.tab:active, .tool:active, .chip:active, .icon-btn:active, .pal-item:active, .sprite-card:active, .project-open:active { transform: scale(0.92); }
.swatch:active { transform: scale(0.85); }
@media (hover: hover) {
  .tab:hover, .tool:hover, .chip:hover, .icon-btn:hover, .pal-item:hover, .sprite-card:hover { transform: translateY(-2px); }
  .swatch:hover { transform: scale(1.08); }
  .big-btn:hover { transform: translateY(-2px) scale(1.03); }
}
.tab.on, .tool.on, .pal-item.on, .sprite-card.on { animation: pick 0.35s cubic-bezier(0.34, 1.56, 0.64, 1); }
.tab.play:not(.on) { animation: invite 2.2s ease-in-out infinite; }
.icon-btn.off { opacity: 0.6; }
.logo { animation: logoIn 0.7s cubic-bezier(0.34, 1.56, 0.64, 1); }
.big-btn.green { animation: invite 2.4s ease-in-out infinite; }
#hud-coins { display: inline-block; }
#hud-coins.pop { animation: hudPop 0.35s cubic-bezier(0.34, 1.56, 0.64, 1); }
.play-msg:not([hidden]) { animation: msgIn 0.45s cubic-bezier(0.34, 1.56, 0.64, 1); }
.msg-stars { font-size: 46px; display: flex; justify-content: center; gap: 6px; }
.msg-stars .star { display: inline-block; opacity: 0; animation: starIn 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) forwards; }
.msg-stars .star:not(.on) { filter: grayscale(1) brightness(1.4); }
.toast:not([hidden]) { animation: toastIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1); }
#tour-panel .mascot canvas { animation: bob 1.6s ease-in-out infinite, blink 4s steps(1) infinite; }

@keyframes viewIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes pick { 0% { transform: scale(0.9); } 60% { transform: scale(1.08); } 100% { transform: scale(1); } }
@keyframes invite { 0%, 100% { box-shadow: 0 6px 0 rgba(0, 0, 0, 0.18); } 50% { box-shadow: 0 6px 0 rgba(0, 0, 0, 0.18), 0 0 0 6px rgba(0, 184, 74, 0.25); } }
@keyframes logoIn { from { transform: scale(0.6) rotate(-6deg); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes hudPop { 50% { transform: scale(1.35); } }
@keyframes msgIn { from { transform: translate(-50%, -60%) scale(0.6); opacity: 0; } to { transform: translate(-50%, -60%) scale(1); opacity: 1; } }
@keyframes starIn { from { opacity: 0; transform: scale(0.2) rotate(-90deg); } to { opacity: 1; transform: none; } }
@keyframes toastIn { from { transform: translate(-50%, -30px); opacity: 0; } to { transform: translateX(-50%); opacity: 1; } }
@keyframes blink { 0%, 96% { filter: none; } 97%, 100% { filter: brightness(0.6); } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .msg-stars .star { opacity: 1; }
}

/* App instalável */
.home-footer { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.install-hint { flex-basis: 100%; margin: 4px 0 0; }

/* Primeiro e segundo desenho (andar, chão por baixo) */
.frame-tabs { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.frame-tab { min-width: 44px; justify-content: center; }
.frame-tab.on { background: var(--accent, #ffa300); color: #fff; border-color: transparent; }

/* Cartões dos mundos na tela inicial */
.home-new h2 { margin: 0; }
.worlds { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; width: 100%; max-width: 720px; }
.world {
  display: grid; justify-items: center; align-content: center; gap: 6px;
  min-height: 150px; padding: 14px 8px; border: 3px solid var(--panel); border-radius: var(--radius);
  box-shadow: var(--shadow); cursor: pointer; font: inherit; font-size: 20px; font-weight: 800; color: var(--ink);
  transition: transform 0.15s;
}
.world canvas { width: 80px; height: 80px; image-rendering: pixelated; }
.world.dark { color: #fff; }
.world.blank { background: var(--panel); border: 3px dashed var(--line); }
.world-blank { font-size: 52px; line-height: 80px; }
.world:active { transform: translateY(3px); }
@media (hover: hover) { .world:hover { transform: translateY(-3px) scale(1.03); } }
@media (max-width: 620px) { .worlds { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

/* Pergunta com ícones */
.ask {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding:
    max(16px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
  background: rgba(74, 47, 85, 0.45);
}
.ask-box { background: var(--panel); border-radius: 28px; box-shadow: var(--shadow); padding: 24px 28px; max-width: 420px; width: 100%; display: grid; justify-items: center; gap: 10px; text-align: center; animation: msgIn 0.25s ease-out; }
.ask-icon { font-size: 64px; line-height: 1; }
.ask-box p { margin: 0; font-size: 20px; font-weight: 800; }
.ask-buttons { display: flex; gap: 18px; margin-top: 6px; }
.ask-btn { width: 96px; height: 96px; font-size: 48px; border-radius: 24px; border: 3px solid var(--line); background: var(--panel); box-shadow: var(--shadow); cursor: pointer; }
.ask-btn.yes { border-color: var(--green); }
.ask-btn.no { border-color: var(--red); }
.ask-btn:active { transform: translateY(3px); }
@keyframes askIn { from { transform: scale(0.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.ask-box { animation-name: askIn; }

/* Lembrete de cópia */
.backup { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; margin: 0 0 24px; padding: 14px 18px; background: #fff3c4; border: 2px solid var(--yellow); border-radius: var(--radius); }
.backup-icon { font-size: 30px; }
.backup-text { flex: 1 1 220px; font-weight: 700; }
body:not(.modo-grande) .backup-text { display: none; }

/* Share the studio address as a QR code drawn on the device. */
.share-box { max-width: 440px; }
#share-qr {
  width: min(280px, 72vw);
  height: auto;
  background: #fff;
  border-radius: 12px;
}
.ask-box .share-url {
  margin: 0;
  max-width: 100%;
  font-size: 14px;
  font-weight: 700;
  color: var(--muted);
  word-break: break-all;
  user-select: text;
  -webkit-user-select: text;
}
.share-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }

/* Phone play: the game covers the studio bar and keeps a way back. */
body.play-focus .studio-bar { display: none; }
body.play-focus #view-jogar {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  background: var(--bg);
  padding:
    max(10px, env(safe-area-inset-top))
    max(12px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom))
    max(12px, env(safe-area-inset-left));
}
body.play-focus .play-wrap {
  flex: 1;
  width: 100%;
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  justify-items: center;
  align-items: center;
  gap: 8px;
}
body.play-focus .play-top,
body.play-focus .touch-controls { width: 100%; }
body.play-focus .play-top {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
}
body.play-focus .play-back {
  display: inline-flex;
  grid-column: 1;
  justify-self: start;
  position: relative;
  z-index: 2;
}
body.play-focus .hud {
  grid-column: 2;
  position: static;
  transform: none;
  text-align: center;
}
body.play-focus .play-stage {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  container-type: size;
  display: grid;
  place-items: end center;
}
body.play-focus #game-canvas {
  width: min(100cqw, calc(100cqh * 4 / 3));
  height: min(100cqh, calc(100cqw * 3 / 4));
  max-width: 100%;
  max-height: 100%;
}
@media (max-height: 480px) {
  body.play-focus .pad-btn { width: 58px; height: 58px; font-size: 28px; }
  body.play-focus .play-back { min-height: 44px; padding: 6px 12px; font-size: 16px; }
  body.play-focus .hud { font-size: 18px; min-height: 24px; }
  body.play-focus .play-wrap { gap: 6px; }
}
