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

:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f9fafb;
  --bg-tertiary: #f3f4f6;
  --text-primary: #111827;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --border: #e5e7eb;
  --border-focus: #3b82f6;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-light: #eff6ff;
  --success: #10b981;
  --success-light: #ecfdf5;
  --danger: #ef4444;
  --danger-light: #fef2f2;
  --chat-user: #3b82f6;
  --chat-user-text: #ffffff;
  --chat-ai: #f3f4f6;
  --chat-ai-text: #111827;
  --toolbar-bg: #f9fafb;
  --scrollbar-track: #f1f1f1;
  --scrollbar-thumb: #c1c1c1;
}

[data-theme="dark"] {
  --bg-primary: #111827;
  --bg-secondary: #1f2937;
  --bg-tertiary: #374151;
  --text-primary: #f9fafb;
  --text-secondary: #9ca3af;
  --text-muted: #6b7280;
  --border: #374151;
  --border-focus: #60a5fa;
  --accent: #60a5fa;
  --accent-hover: #3b82f6;
  --accent-light: #1e3a5f;
  --success: #34d399;
  --success-light: #064e3b;
  --danger: #f87171;
  --danger-light: #7f1d1d;
  --chat-user: #3b82f6;
  --chat-user-text: #ffffff;
  --chat-ai: #1f2937;
  --chat-ai-text: #f9fafb;
  --toolbar-bg: #1f2937;
  --scrollbar-track: #1f2937;
  --scrollbar-thumb: #4b5563;
}

[data-theme="sepia"] {
  --bg-primary: #fdf8f0;
  --bg-secondary: #f5ead6;
  --bg-tertiary: #e8d5b5;
  --text-primary: #5c4b37;
  --text-secondary: #8b7355;
  --text-muted: #a89880;
  --border: #e0d0b8;
  --border-focus: #c49a6c;
  --accent: #c49a6c;
  --accent-hover: #a67c52;
  --accent-light: #f5ead6;
  --success: #7cb342;
  --success-light: #f1f8e9;
  --danger: #d32f2f;
  --danger-light: #fce4ec;
  --chat-user: #c49a6c;
  --chat-user-text: #ffffff;
  --chat-ai: #f5ead6;
  --chat-ai-text: #5c4b37;
  --toolbar-bg: #f5ead6;
  --scrollbar-track: #f5ead6;
  --scrollbar-thumb: #c49a6c;
}

[data-theme="ocean"] {
  --bg-primary: #0f172a;
  --bg-secondary: #1e293b;
  --bg-tertiary: #334155;
  --text-primary: #e2e8f0;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --border: #334155;
  --border-focus: #38bdf8;
  --accent: #38bdf8;
  --accent-hover: #0ea5e9;
  --accent-light: #1e3a5f;
  --success: #34d399;
  --success-light: #064e3b;
  --danger: #f87171;
  --danger-light: #7f1d1d;
  --chat-user: #0ea5e9;
  --chat-user-text: #ffffff;
  --chat-ai: #1e293b;
  --chat-ai-text: #e2e8f0;
  --toolbar-bg: #1e293b;
  --scrollbar-track: #1e293b;
  --scrollbar-thumb: #475569;
}

[data-theme="rose"] {
  --bg-primary: #fdf2f8;
  --bg-secondary: #fce7f3;
  --bg-tertiary: #fbcfe8;
  --text-primary: #831843;
  --text-secondary: #9d174d;
  --text-muted: #be185d;
  --border: #fbcfe8;
  --border-focus: #ec4899;
  --accent: #ec4899;
  --accent-hover: #db2777;
  --accent-light: #fce7f3;
  --success: #10b981;
  --success-light: #ecfdf5;
  --danger: #ef4444;
  --danger-light: #fef2f2;
  --chat-user: #ec4899;
  --chat-user-text: #ffffff;
  --chat-ai: #fce7f3;
  --chat-ai-text: #831843;
  --toolbar-bg: #fce7f3;
  --scrollbar-track: #fce7f3;
  --scrollbar-thumb: #ec4899;
}

[data-theme="mint"] {
  --bg-primary: #f0fdfa;
  --bg-secondary: #ccfbf1;
  --bg-tertiary: #99f6e4;
  --text-primary: #134e4a;
  --text-secondary: #0d9488;
  --text-muted: #14b8a6;
  --border: #99f6e4;
  --border-focus: #14b8a6;
  --accent: #14b8a6;
  --accent-hover: #0d9488;
  --accent-light: #ccfbf1;
  --success: #10b981;
  --success-light: #ecfdf5;
  --danger: #ef4444;
  --danger-light: #fef2f2;
  --chat-user: #14b8a6;
  --chat-user-text: #ffffff;
  --chat-ai: #ccfbf1;
  --chat-ai-text: #134e4a;
  --toolbar-bg: #ccfbf1;
  --scrollbar-track: #ccfbf1;
  --scrollbar-thumb: #14b8a6;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--scrollbar-track); }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { opacity: 0.8; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  height: 48px;
  z-index: 10;
}
.header-left, .header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}
.header h1 {
  font-size: 16px;
  font-weight: 700;
}
.header a {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
}
.header a:hover { color: var(--accent); text-decoration: none; }

/* Buttons */
.btn {
  padding: 6px 14px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-tertiary); }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-success { background: var(--success); color: #fff; border-color: var(--success); }
.btn-success:hover { opacity: 0.9; }
.full-width { width: 100%; }
.toolbar-btn { font-size: 12px; padding: 5px 10px; }

/* Theme Select */
.theme-select {
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 12px;
  cursor: pointer;
}

/* Main Layout */
.main-layout {
  display: flex;
  height: calc(100vh - 48px);
}

/* Left Sidebar */
.sidebar-left {
  width: 380px;
  min-width: 380px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar-section {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.sidebar-section:last-child {
  border-bottom: none;
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.sidebar-title {
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 2px;
}
.sidebar-hint {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 4px;
  line-height: 1.4;
}
.sidebar-subtitle {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 4px;
}
.sidebar-textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 13px;
  line-height: 1.5;
  resize: vertical;
  font-family: inherit;
}
.sidebar-textarea:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(59,130,246,0.1);
}

/* Presets */
.sidebar-presets { margin-top: 4px; }
.preset-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.preset-btn {
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  text-align: center;
}
.preset-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-light);
}

/* Chat */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 0;
  min-height: 100px;
}
.chat-msg {
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.5;
  max-width: 90%;
  word-wrap: break-word;
}
.chat-msg.user {
  background: var(--chat-user);
  color: var(--chat-user-text);
  align-self: flex-end;
  border-bottom-right-radius: 4px;
}
.chat-msg.assistant {
  background: var(--chat-ai);
  color: var(--chat-ai-text);
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}
.chat-input-row {
  display: flex;
  gap: 6px;
  align-items: flex-end;
}
.chat-input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 12px;
  line-height: 1.4;
  resize: none;
  font-family: inherit;
}
.chat-input:focus {
  outline: none;
  border-color: var(--border-focus);
}
.chat-send-btn {
  padding: 8px 12px;
  font-size: 12px;
  white-space: nowrap;
}

/* Editor Main */
.editor-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.editor-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: var(--toolbar-bg);
  border-bottom: 1px solid var(--border);
  gap: 8px;
}
.editor-tabs {
  display: flex;
  gap: 2px;
}
.editor-tab {
  padding: 5px 12px;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}
.editor-tab.active {
  background: var(--bg-primary);
  color: var(--text-primary);
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}
.editor-tab:hover:not(.active) { color: var(--text-primary); }
.editor-actions {
  display: flex;
  gap: 6px;
}
.editor-content {
  flex: 1;
  display: flex;
  overflow: hidden;
}
.editor-pane {
  flex: 1;
  overflow: hidden;
}
.editor-pane.hidden { display: none; }

/* Markdown Editor */
.markdown-textarea {
  width: 100%;
  height: 100%;
  padding: 20px 24px;
  border: none;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 13px;
  line-height: 1.7;
  resize: none;
  outline: none;
}
.markdown-textarea::placeholder {
  color: var(--text-muted);
}

/* Preview */
.preview-pane {
  overflow-y: auto;
  background: var(--bg-primary);
}
.markdown-body {
  padding: 24px 32px;
  max-width: 800px;
  color: var(--text-primary);
  background: var(--bg-primary);
}

/* GitHub markdown theme overrides */
.markdown-body h1 { font-size: 2em; border-bottom: 1px solid var(--border); padding-bottom: 0.3em; margin: 1.2em 0 0.6em; color: var(--text-primary); }
.markdown-body h2 { font-size: 1.5em; border-bottom: 1px solid var(--border); padding-bottom: 0.3em; margin: 1em 0 0.5em; color: var(--text-primary); }
.markdown-body h3 { font-size: 1.25em; margin: 0.8em 0 0.4em; color: var(--text-primary); }
.markdown-body h4 { font-size: 1em; margin: 0.6em 0 0.3em; color: var(--text-primary); }
.markdown-body p { margin: 0.5em 0; color: var(--text-primary); }
.markdown-body ul, .markdown-body ol { padding-left: 2em; margin: 0.5em 0; color: var(--text-primary); }
.markdown-body li { margin: 0.25em 0; }
.markdown-body code { background: var(--bg-tertiary); padding: 0.2em 0.4em; border-radius: 4px; font-size: 85%; color: var(--accent); }
.markdown-body pre { background: var(--bg-tertiary); padding: 16px; border-radius: 8px; overflow-x: auto; margin: 0.8em 0; }
.markdown-body pre code { background: none; padding: 0; color: var(--text-primary); }
.markdown-body blockquote { border-left: 4px solid var(--accent); padding: 0.5em 1em; margin: 0.8em 0; background: var(--accent-light); border-radius: 0 6px 6px 0; color: var(--text-secondary); }
.markdown-body table { border-collapse: collapse; width: 100%; margin: 0.8em 0; }
.markdown-body th, .markdown-body td { border: 1px solid var(--border); padding: 8px 12px; text-align: left; }
.markdown-body th { background: var(--bg-tertiary); font-weight: 600; }
.markdown-body hr { border: none; border-top: 1px solid var(--border); margin: 1.5em 0; }
.markdown-body strong { color: var(--text-primary); }
.markdown-body a { color: var(--accent); }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: var(--bg-secondary);
  border-radius: 12px;
  padding: 24px;
  max-width: 400px;
  width: 90%;
  border: 1px solid var(--border);
}
.modal h2 { font-size: 16px; font-weight: 600; margin-bottom: 16px; }
.modal-hint { font-size: 11px; color: var(--text-secondary); margin-top: -8px; margin-bottom: 12px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }

/* AI Label / Input / Select (shared) */
.ai-label { display: block; font-size: 12px; font-weight: 600; margin-bottom: 4px; color: var(--text-secondary); }
.ai-input, .ai-select {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 13px;
}
.ai-input:focus, .ai-select:focus { outline: none; border-color: var(--border-focus); }
#connectionStatus { font-size: 12px; margin-bottom: 8px; padding: 6px 10px; border-radius: 6px; }
#connectionStatus.success { background: var(--success-light); color: var(--success); }
#connectionStatus.error { background: var(--danger-light); color: var(--danger); }

/* Loading Overlay */
.loading-card {
  background: var(--bg-secondary);
  border-radius: 12px;
  padding: 32px;
  text-align: center;
  border: 1px solid var(--border);
}
.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-card p { font-size: 14px; color: var(--text-secondary); }

/* Responsive */
@media (max-width: 900px) {
  .main-layout { flex-direction: column; }
  .sidebar-left { width: 100%; min-width: 0; max-height: 50vh; }
  .editor-content { min-height: 50vh; }
}
