/* ========================================
   n8n Workflow Builder — Dark SaaS Theme
   ======================================== */

/* --- CSS Variables --- */
:root {
  --bg-primary: #0f1117;
  --bg-surface: #1a1d27;
  --bg-surface-hover: #22253a;
  --bg-input: #151821;
  --border-color: #2a2d3a;
  --border-focus: #6366f1;
  --accent-primary: #6366f1;
  --accent-primary-hover: #818cf8;
  --accent-secondary: #22d3ee;
  --text-primary: #e2e8f0;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --user-bubble: #4f46e5;
  --user-bubble-text: #ffffff;
  --ai-bubble: #1e2030;
  --ai-border: #6366f1;
  --success: #22c55e;
  --error: #ef4444;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.3);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 24px;
  --transition: 0.2s ease;
}

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

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--bg-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* --- Background Mesh Canvas --- */
#mesh-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
}

/* --- App Container --- */
.app {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  height: calc(100vh - 24px);
  max-width: 1400px;
  margin: 12px auto;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-color);
  overflow: hidden;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(99, 102, 241, 0.05);
}

/* --- Header --- */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 32px;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-surface);
  flex-shrink: 0;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-icon {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  object-fit: contain;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.01em;
}

.header-subtitle {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 1px;
}

.btn-new-chat {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-new-chat:hover {
  background: var(--bg-surface-hover);
  border-color: var(--accent-primary);
  color: var(--text-primary);
}

/* --- Messages Area --- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 32px 40px;
  scroll-behavior: smooth;
}

.messages::-webkit-scrollbar {
  width: 6px;
}

.messages::-webkit-scrollbar-track {
  background: transparent;
}

.messages::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 3px;
}

.messages::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* --- Welcome State --- */
.welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 48px 40px 40px;
}

.welcome-icon {
  width: 64px;
  height: 64px;
  border-radius: 20px;
  object-fit: contain;
  margin-bottom: 20px;
  box-shadow: 0 8px 32px rgba(234, 75, 113, 0.3);
}

.welcome h2 {
  font-size: 24px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.welcome p {
  font-size: 15px;
  color: var(--text-secondary);
  max-width: 560px;
  margin-bottom: 36px;
}

.suggestions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 600px;
}

.suggestion-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  transition: all var(--transition);
  font-family: inherit;
  line-height: 1.4;
}

.suggestion-card:hover {
  background: var(--bg-surface-hover);
  border-color: var(--accent-primary);
  color: var(--text-primary);
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(99, 102, 241, 0.15);
}

.suggestion-icon {
  font-size: 20px;
  flex-shrink: 0;
}

/* --- Message Bubbles --- */
.message {
  display: flex;
  margin-bottom: 20px;
  animation: fadeIn 0.3s ease;
}

.message-user {
  justify-content: flex-end;
}

.message-assistant {
  justify-content: flex-start;
}

.message-content {
  max-width: 75%;
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.6;
  word-wrap: break-word;
}

.message-user .message-content {
  background: var(--user-bubble);
  color: var(--user-bubble-text);
  border-bottom-right-radius: 4px;
}

.message-assistant .message-content {
  background: var(--ai-bubble);
  color: var(--text-primary);
  border-left: 3px solid var(--ai-border);
  border-bottom-left-radius: 4px;
}

.message-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}

.message-user .message-label {
  color: rgba(255, 255, 255, 0.7);
  text-align: right;
}

.message-assistant .message-label {
  color: var(--accent-secondary);
}

.message-time {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 6px;
}

.message-user .message-time {
  text-align: right;
  color: rgba(255, 255, 255, 0.5);
}

/* --- Markdown Content in AI Messages --- */
.message-assistant .message-content h1,
.message-assistant .message-content h2,
.message-assistant .message-content h3 {
  margin-top: 12px;
  margin-bottom: 6px;
  font-weight: 600;
}

.message-assistant .message-content h3 {
  font-size: 15px;
}

.message-assistant .message-content p {
  margin-bottom: 8px;
}

.message-assistant .message-content p:last-child {
  margin-bottom: 0;
}

.message-assistant .message-content ul,
.message-assistant .message-content ol {
  margin: 8px 0;
  padding-left: 20px;
}

.message-assistant .message-content li {
  margin-bottom: 4px;
}

.message-assistant .message-content code {
  background: rgba(99, 102, 241, 0.15);
  color: var(--accent-primary-hover);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
}

.message-assistant .message-content pre {
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 14px;
  margin: 10px 0;
  overflow-x: auto;
}

.message-assistant .message-content pre code {
  background: none;
  color: var(--text-primary);
  padding: 0;
  font-size: 13px;
  line-height: 1.5;
}

.message-assistant .message-content strong {
  color: var(--text-primary);
  font-weight: 600;
}

.message-assistant .message-content a {
  color: var(--accent-primary-hover);
  text-decoration: none;
}

.message-assistant .message-content a:hover {
  text-decoration: underline;
}

/* --- Typing Indicator --- */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 12px 16px;
  background: var(--ai-bubble);
  border-left: 3px solid var(--ai-border);
  border-radius: var(--radius);
  border-bottom-left-radius: 4px;
  max-width: 80px;
}

.typing-dot {
  width: 8px;
  height: 8px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: typingBounce 1.4s infinite;
}

.typing-dot:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dot:nth-child(3) {
  animation-delay: 0.4s;
}

/* --- Input Area --- */
.input-area {
  padding: 20px 32px 24px;
  border-top: 1px solid var(--border-color);
  background: var(--bg-surface);
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 4px 4px 4px 16px;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.input-wrapper:focus-within {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

.input-wrapper textarea {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.5;
  padding: 10px 0;
  resize: none;
  min-height: 24px;
  max-height: 120px;
}

.input-wrapper textarea::placeholder {
  color: var(--text-muted);
}

.btn-send {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: var(--accent-primary);
  border: none;
  border-radius: var(--radius-sm);
  color: white;
  font-size: 18px;
  cursor: pointer;
  transition: all var(--transition);
  flex-shrink: 0;
}

.btn-send:hover {
  background: var(--accent-primary-hover);
  transform: scale(1.05);
}

.btn-send:disabled {
  background: var(--border-color);
  cursor: not-allowed;
  transform: none;
}

.input-hint {
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 8px;
}

.input-hint a {
  color: var(--accent-primary);
  text-decoration: none;
}

.input-hint a:hover {
  text-decoration: underline;
}

/* --- Error Message --- */
.message-error .message-content {
  background: rgba(239, 68, 68, 0.1);
  border-left: 3px solid var(--error);
  color: #fca5a5;
}

/* --- Deploy Button --- */
.deploy-wrapper {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
}

.btn-deploy {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--success);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-deploy:hover {
  background: #16a34a;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(34, 197, 94, 0.3);
}

.btn-deploy:disabled {
  background: var(--border-color);
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.btn-deploy.deploying {
  background: var(--accent-primary);
}

.deploy-status {
  font-size: 13px;
  color: var(--text-secondary);
}

.deploy-status.success {
  color: var(--success);
}

.deploy-status.error {
  color: var(--error);
}

.deploy-status a {
  color: var(--accent-primary-hover);
  text-decoration: none;
}

.deploy-status a:hover {
  text-decoration: underline;
}

/* --- Template Gallery --- */
.template-gallery {
  width: 100%;
  max-width: 1100px;
  margin-top: 24px;
}

.template-divider {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
  color: var(--text-muted);
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.template-divider::before,
.template-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border-color);
}

.template-search-wrapper {
  margin-bottom: 14px;
}

.template-search {
  width: 100%;
  padding: 10px 16px;
  background: var(--bg-input);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.template-search::placeholder {
  color: var(--text-muted);
}

.template-search:focus {
  border-color: var(--accent-primary);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

.template-categories {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  margin-bottom: 16px;
  scrollbar-width: none;
}

.template-categories::-webkit-scrollbar {
  display: none;
}

.template-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 14px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: 20px;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 12px;
  white-space: nowrap;
  cursor: pointer;
  transition: all var(--transition);
}

.template-pill:hover {
  background: var(--bg-surface-hover);
  border-color: var(--accent-primary);
  color: var(--text-primary);
}

.template-pill.active {
  background: var(--accent-primary);
  border-color: var(--accent-primary);
  color: #fff;
}

.template-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  margin-bottom: 20px;
}

.template-card {
  display: flex;
  flex-direction: column;
  padding: 20px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  transition: all var(--transition);
  min-height: 190px;
  min-width: 0;
  overflow: hidden;
}

.template-card:hover {
  border-color: var(--accent-primary);
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(99, 102, 241, 0.15);
}

.template-card-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  text-decoration: none;
  line-height: 1.35;
  margin-bottom: 8px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.template-card-title:hover {
  color: var(--accent-primary-hover);
}

.template-card-desc {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.45;
  margin-bottom: 10px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.template-card-nodes {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 10px;
  flex: 1;
  overflow: hidden;
}

.node-chip {
  font-size: 10px;
  padding: 2px 7px;
  border-radius: 4px;
  background: rgba(99, 102, 241, 0.12);
  color: var(--accent-primary-hover);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  letter-spacing: 0.01em;
}

.template-card-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.template-card-views {
  display: flex;
  align-items: center;
  gap: 3px;
}

.template-card-category {
  padding: 2px 8px;
  background: rgba(99, 102, 241, 0.15);
  color: var(--accent-primary-hover);
  border-radius: 10px;
  font-size: 10px;
  font-weight: 500;
}

.template-card-author {
  margin-left: auto;
  font-size: 10px;
  color: var(--text-muted);
}

.template-card-actions {
  display: flex;
  gap: 8px;
}

.template-card-actions button {
  flex: 1;
  padding: 8px 12px;
  border: none;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-template-use {
  background: #EA4B71;
  color: #fff;
}

.btn-template-use:hover {
  background: #d63d62;
}

.btn-template-use:disabled {
  background: var(--border-color);
  cursor: not-allowed;
}

.btn-template-customize {
  background: var(--bg-surface-hover);
  color: var(--text-secondary);
  border: 1px solid var(--border-color) !important;
}

.btn-template-customize:hover {
  border-color: var(--accent-primary) !important;
  color: var(--text-primary);
}

.btn-load-more {
  display: block;
  width: 100%;
  padding: 10px;
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-load-more:hover {
  background: var(--bg-surface-hover);
  border-color: var(--accent-primary);
  color: var(--text-primary);
}

.template-loading {
  text-align: center;
  padding: 24px;
  color: var(--text-muted);
  font-size: 13px;
}

.template-card-status {
  font-size: 11px;
  margin-top: 6px;
  min-height: 16px;
}

.template-card-status.success {
  color: var(--success);
}

.template-card-status.error {
  color: var(--error);
}

.template-card-status a {
  color: var(--accent-primary-hover);
  text-decoration: none;
}

.template-card-status a:hover {
  text-decoration: underline;
}

/* --- Animations --- */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes typingBounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-6px);
    opacity: 1;
  }
}

/* --- Responsive --- */
@media (max-width: 768px) {
  .app {
    max-width: 100%;
    margin: 0;
    height: 100vh;
    border-radius: 0;
    border: none;
    box-shadow: none;
  }

  .header {
    padding: 14px 16px;
  }

  .header-title {
    font-size: 16px;
  }

  .header-subtitle {
    display: none;
  }

  .messages {
    padding: 20px 16px;
  }

  .message-content {
    max-width: 85%;
    font-size: 14px;
  }

  .input-area {
    padding: 12px 16px 16px;
  }

  .welcome {
    padding: 32px 16px 24px;
  }

  .welcome h2 {
    font-size: 20px;
  }

  .suggestions {
    max-width: 100%;
  }

  .suggestion-card {
    font-size: 13px;
    padding: 12px 14px;
  }

  .template-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .template-gallery {
    max-width: 100%;
  }
}

@media (max-width: 480px) {
  .btn-new-chat span {
    display: none;
  }

  .message-content {
    max-width: 90%;
  }

  .template-grid {
    grid-template-columns: 1fr;
  }
}
