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

:root {
  --bg: #faf8f5;
  --surface: #ffffff;
  --text: #2d2a26;
  --text-muted: #8a857e;
  --primary: #4a7c59;
  --primary-hover: #3d6649;
  --accent: #e8a849;
  --border: #e5e0d8;
  --radius: 12px;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
  overflow-x: hidden;
}

/* === Layout === */
#app {
  max-width: 800px;
  margin: 0 auto;
  padding: 16px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  text-align: center;
  padding: 12px 0;
}

header h1 {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--primary);
}

.tagline {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin-top: 2px;
}

/* === PIN Screen === */
.pin-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 48px 24px;
}

.pin-box h2 {
  font-size: 1.3rem;
  font-weight: 600;
}

#pin-input {
  font-size: 1.5rem;
  text-align: center;
  width: 180px;
  padding: 12px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  outline: none;
  letter-spacing: 0.3em;
}

#pin-input:focus {
  border-color: var(--primary);
}

.error {
  color: #c44;
  font-size: 0.9rem;
}

/* === Buttons === */
.btn {
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-weight: 600;
  border-radius: var(--radius);
  transition: background 0.15s, transform 0.1s;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--primary);
  color: white;
  padding: 12px 28px;
  font-size: 1rem;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-tool {
  background: var(--surface);
  border: 2px solid var(--border);
  color: var(--text);
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.btn-tool:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.btn-tool.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

/* === Toolbar === */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
  padding: 12px 16px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}

.tool-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tool-label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

#brush-size {
  width: 80px;
  accent-color: var(--primary);
}

/* === Color Palette === */
#color-palette {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.color-swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
}

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

.color-swatch.active {
  border-color: var(--text);
}

/* === Canvas === */
.canvas-area {
  position: relative;
  background: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  touch-action: none;
}

#drawing-canvas {
  display: block;
  width: 100%;
  cursor: crosshair;
  touch-action: none;
}

.saving-indicator {
  position: absolute;
  top: 12px;
  right: 12px;
  background: var(--primary);
  color: white;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  opacity: 0;
  transition: opacity 0.3s;
}

.saving-indicator.visible {
  opacity: 1;
}

/* === Prompt Area === */
.prompt-area {
  display: flex;
  gap: 12px;
  margin-top: 12px;
  align-items: stretch;
}

#prompt-input {
  flex: 1;
  padding: 12px 16px;
  font-size: 1rem;
  font-family: inherit;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  outline: none;
}

#prompt-input:focus {
  border-color: var(--primary);
}

#prompt-input::placeholder {
  color: var(--text-muted);
}

.btn-magic {
  min-width: 140px;
  white-space: nowrap;
}

/* === Results === */
#results-area, #history-area {
  margin-top: 24px;
}

#results-area h2, #history-area h2 {
  font-size: 1.1rem;
  margin-bottom: 12px;
  color: var(--text-muted);
}

#results-gallery, #history-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
}

.result-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 2px solid var(--border);
  overflow: hidden;
}

.result-card img {
  width: 100%;
  display: block;
  background: #e5e0d8;
}

.result-card .result-prompt {
  padding: 8px 12px;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* === Responsive === */
@media (max-width: 600px) {
  #app {
    padding: 8px;
  }

  header h1 {
    font-size: 1.5rem;
  }

  .toolbar {
    gap: 10px;
    padding: 10px;
  }

  .prompt-area {
    flex-direction: column;
  }

  .btn-magic {
    min-width: unset;
  }
}
