* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none; }

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #87CEEB;
  font-family: -apple-system, system-ui, sans-serif;
  touch-action: none;
  overscroll-behavior: none;
}

#stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

.hidden { display: none !important; }

/* Bottom UI: shapes pill on top, palette pill below */
#ui {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 10;
}

#shapes,
#ui-bottom {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: 999px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
}

#palette {
  display: flex;
  gap: 10px;
}

.swatch {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 4px solid rgba(255, 255, 255, 0.9);
  box-shadow: inset 0 -3px 0 rgba(0,0,0,0.18), 0 2px 6px rgba(0,0,0,0.2);
  cursor: pointer;
  transition: transform 0.15s ease;
  flex-shrink: 0;
  padding: 0;
}

.swatch:active { transform: scale(0.92); }
.swatch.active {
  transform: scale(1.15);
  border-color: #333;
}

.shape-btn {
  width: 56px;
  height: 56px;
  border-radius: 16px;
  border: 3px solid transparent;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  transition: transform 0.15s ease, border-color 0.15s ease;
  flex-shrink: 0;
  padding: 4px;
}

.shape-btn:active { transform: scale(0.92); }
.shape-btn.active {
  border-color: #333;
  background: #FFF8DC;
}

.shape-btn svg { width: 100%; height: 100%; }

.tool {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: #fff;
  color: #555;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
  transition: transform 0.15s ease, background 0.15s ease;
  flex-shrink: 0;
}

.tool:active { transform: scale(0.92); }
.tool.active {
  background: #FF5252;
  color: #fff;
}

#btn-templates {
  position: fixed;
  top: max(16px, env(safe-area-inset-top));
  left: max(16px, env(safe-area-inset-left));
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: none;
  background: #fff;
  color: #FB8C00;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(0,0,0,0.2);
  z-index: 10;
  transition: transform 0.15s ease;
}
#btn-templates:active { transform: scale(0.92); }

#btn-template-exit {
  position: fixed;
  top: max(16px, env(safe-area-inset-top));
  right: max(16px, env(safe-area-inset-right));
  width: 60px;
  height: 60px;
  border-radius: 50%;
  border: none;
  background: #fff;
  color: #555;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(0,0,0,0.2);
  z-index: 11;
}
#btn-template-exit:active { transform: scale(0.92); }

/* Templates overlay */
#templates-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 30, 50, 0.55);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  animation: fadeIn 0.2s ease;
}
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
#templates-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  width: min(100%, 720px);
}
.template-tile {
  aspect-ratio: 1;
  background: #fff;
  border: none;
  border-radius: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 64px;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(0,0,0,0.2);
  transition: transform 0.15s ease;
}
.template-tile:active { transform: scale(0.94); }
.template-tile .name {
  font-size: 18px;
  font-weight: 600;
  color: #555;
}

@media (max-width: 480px) {
  .swatch, .shape-btn, .tool { width: 48px; height: 48px; }
  #palette { gap: 6px; }
  #shapes, #ui-bottom { gap: 6px; padding: 6px 10px; }
  #btn-templates { width: 60px; height: 60px; }
  #btn-templates svg { width: 32px; height: 32px; }
  #templates-grid { grid-template-columns: repeat(2, 1fr); }
  .template-tile { font-size: 48px; }
}
