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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f0f0f0;
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
  touch-action: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* ── Toolbar ─────────────────────────────────────────────────── */
#toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  background: #fff;
  border-bottom: 1px solid #ddd;
  flex-shrink: 0;
  flex-wrap: wrap;
  -webkit-overflow-scrolling: touch;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 3px;
  padding-right: 6px;
  border-right: 1px solid #eee;
}

.toolbar-group:last-child { border-right: none; }

.toolbar-right { margin-left: auto; }

#toolbar button {
  padding: 5px 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  background: #fff;
  cursor: pointer;
  font-size: 12px;
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}

#toolbar button:hover { background: #f5f5f5; }
#toolbar button:active { background: #e8e8e8; }
#toolbar button.primary { background: #2563eb; color: #fff; border-color: #2563eb; }
#toolbar button.primary:hover { background: #1d4ed8; }

#toolbar label {
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 4px;
}

#toolbar input[type="range"] { width: 60px; }

/* ── Workspace ───────────────────────────────────────────────── */
#workspace {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* ── Palette ─────────────────────────────────────────────────── */
#palette {
  width: 150px;
  background: #fff;
  border-right: 1px solid #ddd;
  overflow-y: auto;
  flex-shrink: 0;
  padding: 6px;
  -webkit-overflow-scrolling: touch;
}

#palette h3 {
  font-size: 13px;
  color: #333;
  margin-bottom: 6px;
  padding-bottom: 4px;
  border-bottom: 1px solid #eee;
}

.palette-section { margin-bottom: 10px; }
.palette-section h4 {
  font-size: 10px;
  color: #999;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 4px;
}

.palette-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
}

.palette-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 3px;
  border: 1px solid #eee;
  border-radius: 4px;
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.palette-item:hover { background: #f0f7ff; border-color: #bdd7ff; }
.palette-item:active { cursor: grabbing; background: #dbeafe; }

.palette-item canvas {
  width: 36px;
  height: 36px;
  pointer-events: none;
}

.palette-item span {
  font-size: 9px;
  color: #666;
  margin-top: 1px;
}

/* ── Canvas ──────────────────────────────────────────────────── */
#canvas-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #e8e8e8;
}

#canvas-container canvas {
  position: absolute;
  top: 0;
  left: 0;
}

/* ── Library Panel ───────────────────────────────────────────── */
#library-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: 200px;
  max-height: 60%;
  background: #fff;
  border-left: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  border-radius: 0 0 0 8px;
  box-shadow: -2px 2px 8px rgba(0,0,0,0.1);
  z-index: 100;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  transition: transform 0.2s;
}

#library-panel.collapsed {
  transform: translateX(calc(100% - 32px));
}

#library-panel .library-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  background: #2563eb;
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

#library-panel .library-header span { flex: 1; }

#library-panel .library-body {
  padding: 6px;
}

.library-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px;
  border: 1px solid #eee;
  border-radius: 4px;
  margin-bottom: 4px;
  cursor: pointer;
  font-size: 12px;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s;
}

.library-item:hover { background: #f0f7ff; }
.library-item:active { background: #dbeafe; }

.library-item .lib-delete {
  border: none;
  background: none;
  cursor: pointer;
  color: #c00;
  font-size: 16px;
  padding: 0 4px;
  -webkit-tap-highlight-color: transparent;
}

.library-empty {
  color: #999;
  font-size: 11px;
  padding: 8px 4px;
  text-align: center;
}

/* ── Modal ───────────────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal.hidden { display: none; }

.modal-content {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  width: 92%;
  max-width: 600px;
  max-height: 85vh;
  overflow-y: auto;
}

.modal-content h3 { margin-bottom: 8px; }
.modal-content p { color: #666; margin-bottom: 12px; font-size: 14px; }

.modal-content textarea {
  width: 100%;
  font-family: monospace;
  font-size: 13px;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  resize: vertical;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}

.modal-actions button {
  padding: 8px 16px;
  border: 1px solid #ccc;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.modal-actions button.primary {
  background: #2563eb;
  color: #fff;
  border-color: #2563eb;
}

#ai-status {
  margin-top: 12px;
  padding: 8px;
  border-radius: 4px;
  font-size: 13px;
}

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

/* ── Responsive (mobile/iPad) ────────────────────────────────── */
@media (max-width: 768px) {
  #palette { width: 100px; padding: 4px; }
  .palette-grid { grid-template-columns: 1fr; }
  .palette-item canvas { width: 32px; height: 32px; }
  #toolbar { padding: 3px 6px; gap: 3px; }
  #toolbar button { padding: 4px 6px; font-size: 11px; }
  #library-panel { width: 180px; }
}

@media (max-width: 480px) {
  #palette { width: 80px; }
  .palette-item span { font-size: 8px; }
  #library-panel { width: 160px; max-height: 50%; }
}
