/* --- Design tokens --- */
:root {
  --color-background-primary: #ffffff;
  --color-background-secondary: #f5f4f0;
  --color-background-hover: rgba(0, 0, 0, 0.04);
  --color-text-primary: #1a1a1a;
  --color-text-secondary: #666666;
  --color-text-tertiary: #999999;
  --color-border-tertiary: rgba(0, 0, 0, 0.12);
  --color-accent: #1a1a1a;
  --toolbar-radius: 14px;
  --popover-radius: 20px;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--color-background-secondary);
  font-family: system-ui, -apple-system, 'SF Pro Text', sans-serif;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

/* --- Canvas stack --- */
#canvas-container {
  position: relative;
  width: 100%;
  height: 100%;
  background: var(--color-background-secondary);
}

#canvas-container canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

#frozen-canvas { z-index: 1; }
#ghost-canvas { z-index: 2; pointer-events: none; }
#draw-canvas { z-index: 3; cursor: crosshair; filter: blur(var(--sketch-blur, 1.5px)); }
#transition-canvas { z-index: 4; pointer-events: none; }

#canvas-hint {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 6;
  color: var(--color-text-tertiary);
  font-size: 15px;
  font-weight: 400;
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.4s ease-out;
}

#canvas-hint.hidden {
  opacity: 0;
  pointer-events: none;
}

/* --- Render overlay --- */
#render-output {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: fill;
  pointer-events: none;
  z-index: 5;
  mix-blend-mode: var(--render-blend, normal);
  opacity: 0;
  filter: blur(0px) saturate(1.1);
  transition: opacity 0.8s ease-out, filter 1.2s ease-out, transform 1.2s ease-out;
}

#render-output.visible {
  opacity: var(--render-opacity, 0.5);
}

#render-output.appearing {
  filter: blur(6px) saturate(1.3);
  transform: scale(1.01);
}

/* --- Render status --- */
#render-status {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  color: var(--color-text-tertiary);
  font-size: 13px;
  text-align: center;
  pointer-events: none;
}

#render-status.active {
  color: #f57c00;
  animation: pulse 1.2s ease-in-out infinite;
}

#render-status.hidden {
  display: none;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* --- Toolbar --- */
#toolbar {
  position: fixed;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 2px;
  background: var(--color-background-primary);
  border: 0.5px solid var(--color-border-tertiary);
  border-radius: var(--toolbar-radius);
  padding: 5px 6px;
}

#toolbar button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 6px 10px;
  border: none;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
  font-size: 13px;
  color: var(--color-text-primary);
  white-space: nowrap;
  transition: background 0.15s ease;
}

#toolbar button:hover {
  background: var(--color-background-hover);
}

#toolbar button:active {
  background: rgba(0, 0, 0, 0.08);
}

#toolbar button:disabled {
  opacity: 0.3;
  cursor: default;
}

#toolbar button:disabled:hover {
  background: transparent;
}

.toolbar-sep {
  width: 0.5px;
  height: 18px;
  background: var(--color-border-tertiary);
  flex-shrink: 0;
}

/* Color swatch */
#color-swatch {
  display: block;
  width: 16px;
  height: 16px;
  border-radius: 4px;
  background: #1a1a1a;
  border: 0.5px solid var(--color-border-tertiary);
}

/* Size label */
#size-label {
  font-size: 13px;
  color: var(--color-text-secondary);
  font-variant-numeric: tabular-nums;
}

/* Brush label */
#brush-label {
  font-weight: 500;
  font-size: 13px;
}

/* --- Clear confirmation --- */
#clear-confirm {
  position: fixed;
  top: 56px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 110;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--color-background-primary);
  border: 0.5px solid var(--color-border-tertiary);
  border-radius: var(--toolbar-radius);
  padding: 6px 10px;
  font-size: 13px;
  color: var(--color-text-primary);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

#clear-confirm.hidden {
  display: none;
}

#clear-confirm button {
  padding: 4px 12px;
  border: none;
  border-radius: 8px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s ease;
}

#clear-yes {
  background: #e53935;
  color: white;
}

#clear-yes:hover {
  background: #c62828;
}

#clear-no {
  background: var(--color-background-hover);
  color: var(--color-text-primary);
}

#clear-no:hover {
  background: rgba(0, 0, 0, 0.08);
}

/* --- Popovers (shared) --- */
.popover {
  position: fixed;
  z-index: 105;
  background: var(--color-background-primary);
  border: 0.5px solid var(--color-border-tertiary);
  border-radius: var(--popover-radius);
  padding: 14px 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.popover.hidden {
  display: none !important;
}

/* --- Color picker --- */
#color-popover {
  top: 56px;
  left: 50%;
  transform: translateX(-50%);
}

.color-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 6px;
}

.color-dot {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.1s ease;
}

.color-dot:hover {
  transform: scale(1.15);
}

.color-dot.active {
  border-color: var(--color-accent);
}

/* --- Size popover --- */
#size-popover {
  top: 56px;
  left: 50%;
  transform: translateX(-50%);
  min-width: 180px;
}

#size-popover:not(.hidden) {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

#size-preview {
  width: 64px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#size-dot {
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--color-text-primary);
  transition: width 0.1s ease, height 0.1s ease;
}

#size-slider {
  width: 100%;
  accent-color: var(--color-accent);
}

#size-value {
  font-size: 12px;
  color: var(--color-text-tertiary);
  font-variant-numeric: tabular-nums;
}

/* --- Brush picker --- */
#brush-picker {
  top: 56px;
  right: 12px;
  left: auto;
  transform: none;
  width: 260px;
  padding: 8px;
}

.brush-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.brush-row:hover {
  background: var(--color-background-hover);
}

.brush-row.active {
  background: rgba(0, 0, 0, 0.04);
}

.brush-preview {
  flex-shrink: 0;
}

.brush-info {
  display: flex;
  flex-direction: column;
  gap: 1px;
  flex: 1;
  min-width: 0;
}

.brush-name {
  font-size: 13px;
  color: var(--color-text-primary);
  font-weight: 450;
}

.brush-desc {
  font-size: 11px;
  color: var(--color-text-tertiary);
}

.brush-check {
  flex-shrink: 0;
  color: var(--color-accent);
  opacity: 0;
  transition: opacity 0.15s ease;
}

.brush-row.active .brush-check {
  opacity: 1;
}

/* --- Warmup bar --- */
#warmup-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 150;
  height: 32px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
  border-top: 0.5px solid var(--color-border-tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.5s ease, transform 0.5s ease;
}

#warmup-bar.hidden {
  opacity: 0;
  transform: translateY(100%);
  pointer-events: none;
}

#warmup-progress {
  position: absolute;
  top: 0;
  left: 0;
  height: 2px;
  background: var(--color-text-tertiary);
  transition: width 0.4s ease-out;
  width: 0%;
}

#warmup-text {
  font-size: 12px;
  color: var(--color-text-tertiary);
  letter-spacing: 0.01em;
}

/* --- Onboarding overlay --- */
#onboarding-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(245, 244, 240, 0.85);
  backdrop-filter: blur(12px);
  transition: opacity 0.4s ease;
}

#onboarding-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

#onboarding-card {
  background: var(--color-background-primary);
  border: 0.5px solid var(--color-border-tertiary);
  border-radius: 24px;
  padding: 28px 24px 20px;
  width: 300px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.08);
  text-align: center;
}

#onboarding-card h2 {
  font-size: 18px;
  font-weight: 600;
  color: var(--color-text-primary);
  margin-bottom: 4px;
  text-align: left;
}

#onboarding-card p {
  font-size: 13px;
  color: var(--color-text-tertiary);
  margin-bottom: 18px;
  text-align: left;
}

#onboarding-brushes {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

#onboarding-brushes .brush-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.15s ease;
  text-align: left;
}

#onboarding-brushes .brush-row:hover {
  background: var(--color-background-hover);
}

/* --- AI status (hidden from UI, used internally) --- */
.ai-status { display: none; }

/* --- Debug toggle --- */
#btn-debug-toggle {
  position: fixed;
  bottom: 12px;
  right: 12px;
  z-index: 100;
  padding: 6px 12px;
  border: 0.5px solid var(--color-border-tertiary);
  border-radius: 8px;
  background: var(--color-background-primary);
  cursor: pointer;
  font-size: 11px;
  color: var(--color-text-tertiary);
  opacity: 0.6;
  transition: opacity 0.2s;
}

#btn-debug-toggle:hover {
  opacity: 1;
}

/* --- Debug panel --- */
#debug-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 280px;
  height: 100%;
  z-index: 200;
  background: rgba(30, 30, 30, 0.95);
  backdrop-filter: blur(8px);
  box-shadow: -2px 0 12px rgba(0, 0, 0, 0.3);
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  color: #ccc;
}

#debug-panel.hidden {
  display: none;
}

.debug-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.debug-header h3 {
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #888;
  margin: 0;
}

#btn-debug-close {
  background: none;
  border: none;
  color: #888;
  font-size: 20px;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
}

#btn-debug-close:hover {
  color: #ccc;
}

#debug-panel label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: #aaa;
}

#debug-panel input[type="range"] { width: 100%; }

#debug-panel input[type="text"] {
  padding: 6px 8px;
  border: 1px solid #444;
  border-radius: 4px;
  font-size: 13px;
  background: #222;
  color: #ddd;
}

#debug-panel select {
  padding: 6px 8px;
  border: 1px solid #444;
  border-radius: 4px;
  font-size: 13px;
  background: #222;
  color: #ddd;
}

#debug-panel span {
  font-size: 11px;
  color: #666;
}

#dbg-state {
  font-size: 12px;
  color: #888;
  padding: 8px;
  background: #222;
  border-radius: 4px;
  font-family: monospace;
}

/* --- Responsive --- */

/* iPhone */
@media (max-width: 480px) {
  #toolbar {
    top: 8px;
    padding: 4px 5px;
    gap: 1px;
  }

  #toolbar button {
    padding: 6px 8px;
    font-size: 12px;
  }

  #btn-clear {
    font-size: 0;
  }
  #btn-clear::before {
    content: '';
    display: block;
    width: 16px;
    height: 16px;
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 16 16' fill='none' stroke='%231a1a1a' stroke-width='1.5' stroke-linecap='round'%3E%3Cpath d='M4 4l8 8M12 4l-8 8'/%3E%3C/svg%3E") center/contain no-repeat;
  }

  #brush-picker {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    top: auto;
    width: 100%;
    border-radius: var(--popover-radius) var(--popover-radius) 0 0;
    padding: 14px 16px 24px;
  }

  #color-popover,
  #size-popover {
    left: 8px;
    right: 8px;
    width: auto;
    transform: none;
  }
}

/* iPad */
@media (min-width: 768px) {
  #toolbar {
    padding: 5px 8px;
    gap: 3px;
  }

  #toolbar button {
    padding: 6px 12px;
  }
}
