/* === Drawing Sketchpad === */
/* Toddler-friendly finger-drawing canvas with color bar */

/* --- Reset & Base --- */
*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: #f5f5f7;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

.app-shell {
  position: relative;
  width: 100vw;
  height: 100dvh;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* --- Toolbar --- */
.toolbar {
  position: absolute;
  top: 0.5rem;
  left: 0.5rem;
  right: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.35rem 0.85rem;
  background: rgba(255, 255, 255, 0.64);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 999px;
  z-index: 5;
  gap: 0.5rem;
}

.toolbar h1 {
  font-size: clamp(0.72rem, 1.7vw, 0.9rem);
  margin: 0;
  color: #071224;
  font-weight: 600;
  white-space: nowrap;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

#goBack {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
  font-size: 0.75rem;
  font-weight: 600;
  color: #fff;
  background: linear-gradient(135deg, #f87171, #ef4444);
  border-radius: 999px;
  padding: 0.4rem 0.75rem;
  cursor: pointer;
  transition: opacity 0.15s;
}

#goBack:hover {
  opacity: 0.85;
}

.toolbar-actions button {
  font: inherit;
  font-weight: 600;
  border-radius: 999px;
  padding: 0.4rem 0.75rem;
  cursor: pointer;
  border: none;
  font-size: 0.75rem;
  transition: background 0.15s, opacity 0.15s;
}

#undoBtn {
  background: #e5e7eb;
  color: #374151;
}

#undoBtn:hover:not(:disabled) {
  background: #d1d5db;
}

#undoBtn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

#clearBtn {
  background: #f87171;
  color: #fff;
}

#clearBtn:hover {
  background: #ef4444;
}

/* --- Canvas --- */
.canvas-wrap {
  position: absolute;
  inset: 0;
  bottom: auto;
  /* Height: fill viewport minus color-bar height */
  height: calc(100% - 60px);
  overflow: hidden;
}

#drawCanvas {
  display: block;
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: crosshair;
  background: #ffffff;
}

/* --- Color Bar (fixed bottom) --- */
.color-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 10;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-top: 2px solid #eee;
  padding: 8px 12px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tools-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

/* --- Color Palette --- */
.palette {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
}

.color-swatch {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 3px solid white;
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

.color-swatch.active {
  border-color: white;
  box-shadow: 0 0 0 3px currentColor, 0 2px 8px rgba(0, 0, 0, 0.25);
  transform: scale(1.15);
}

.color-swatch:active {
  transform: scale(0.92);
}

/* --- Tool Buttons --- */
.tool-btn {
  padding: 8px 14px;
  border-radius: 12px;
  border: 2px solid #ddd;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  background: white;
  transition: all 0.15s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

.tool-btn:hover {
  background: #f3f4f6;
}

.tool-btn[aria-pressed="true"] {
  background: #f87171;
  color: white;
  border-color: #ef4444;
}

.new-btn {
  background: #f87171;
  color: white;
  border-color: #ef4444;
  font-weight: 700;
}

.new-btn:hover {
  background: #ef4444;
}

/* --- Size Row --- */
.size-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: #555;
  justify-content: center;
}

.size-row input[type="range"] {
  width: 120px;
  accent-color: #60a5fa;
  cursor: pointer;
}

/* --- Confetti Layer --- */
#confettiLayer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 20;
}

.confetti-piece {
  position: absolute;
  top: -20px;
  width: 12px;
  height: 18px;
  border-radius: 3px;
  opacity: 0.9;
  animation: confetti-fall var(--duration, 1800ms) ease-out forwards;
  will-change: transform, opacity;
}

.confetti-piece::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  animation: sparkle-pop 600ms ease-out forwards;
}

/* --- Keyframes --- */
@keyframes confetti-fall {
  0% {
    transform: translateY(0) translateX(0) rotate(0deg);
    opacity: 1;
  }
  25% {
    opacity: 1;
  }
  100% {
    transform: translateY(100vh) translateX(var(--drift, 60px)) rotate(var(--spin, 720deg));
    opacity: 0;
  }
}

@keyframes sparkle-pop {
  0% {
    box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.8);
    transform: scale(1);
  }
  50% {
    box-shadow: 0 0 8px 4px rgba(255, 255, 255, 0.6);
    transform: scale(1.3);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(255, 255, 255, 0);
    transform: scale(1);
  }
}

/* --- Responsive --- */
@media (max-width: 560px) {
  .color-bar {
    padding: 6px 8px;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
  }

  .toolbar {
    padding: 0.3rem 0.65rem;
    gap: 0.3rem;
  }

  .toolbar h1 {
    font-size: 0.7rem;
  }

  .toolbar-actions button,
  #goBack {
    font-size: 0.65rem;
    padding: 0.3rem 0.55rem;
  }

  .color-swatch {
    width: 34px;
    height: 34px;
  }

  .palette {
    gap: 4px;
  }

  .tools-row {
    gap: 5px;
  }

  .tool-btn {
    padding: 6px 10px;
    font-size: 12px;
    border-radius: 10px;
  }

  .size-row {
    font-size: 11px;
  }

  .size-row input[type="range"] {
    width: 90px;
  }
}
