/* ========================================
   SmoothAgent — CSS
   Zero deps, iframe-friendly, responsive
   ======================================== */

:root {
  --primary: #6366f1;
  --primary-light: #818cf8;
  --bg: #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-chat: #151525;
  --text: #e5e7eb;
  --text-secondary: #9ca3af;
  --border: #2d2d44;
  --card-bg: #1e1e32;
  --card-border: #2d2d44;
  --success: #22c55e;
  --error: #ef4444;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.2);
  --shadow: 0 2px 8px rgba(0,0,0,0.3);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'SF Mono', 'Cascadia Code', 'Consolas', monospace;
}

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

html, body {
  height: 100%;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
}

/* SPA view container — fills remaining space below nav */
#app-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: calc(100vh - 48px);
  overflow: hidden;
}

/* Chat root — fills the view container */
#chat-root {
  display: flex;
  height: 100%;
  overflow: hidden;
}

.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 260px;
  background: #0a0a14;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar.hidden {
  display: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.new-chat-btn {
  width: 30px;
  height: 30px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

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

.sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.sidebar-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: all 0.1s;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.sidebar-item:hover {
  background: var(--bg-secondary);
  color: var(--text);
}

.sidebar-item.active {
  background: var(--bg-secondary);
  color: var(--text);
  font-weight: 500;
}

.sidebar-item-status {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.sidebar-item-status.streaming {
  width: 12px;
  height: 12px;
}

.status-spinner {
  display: block;
  width: 12px;
  height: 12px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

.sidebar-item-status.done {
  background: var(--success);
}

.sidebar-item-title {
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.sidebar-item-delete {
  display: none;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 14px;
  cursor: pointer;
  padding: 0 2px;
  flex-shrink: 0;
}

.sidebar-item:hover .sidebar-item-delete {
  display: block;
}

.sidebar-item-delete:hover {
  color: var(--error);
}

.sidebar-toggle {
  width: 36px;
  height: 36px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-right: 13px;
  transition: all 0.15s;
}

.sidebar-toggle:hover {
  color: var(--text);
  border-color: var(--primary);
}

/* Mobile sidebar */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 50;
    box-shadow: 4px 0 20px rgba(0,0,0,0.5);
  }

  .sidebar.hidden {
    display: none;
  }
}

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

.chat-header-title {
  font-weight: 600;
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.chat-title-input {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text);
  font-weight: 600;
  font-size: 16px;
  padding: 4px 8px;
  border-radius: 6px;
  outline: none;
  min-width: 120px;
  max-width: 300px;
}

.chat-title-input:hover {
  border-color: var(--border);
}

.chat-title-input:focus {
  border-color: var(--primary);
  background: var(--bg-secondary);
}

.dark-badge {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  padding: 2px 6px;
  border-radius: 4px;
}

.chat-header-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

.mcp-select {
  padding: 6px 28px 6px 10px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  outline: none;
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1L5 5L9 1' stroke='%239ca3af' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  max-width: 200px;
}

.mcp-select:focus {
  border-color: var(--primary);
}

.remove-mcp-btn {
  width: 28px;
  height: 28px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}

.remove-mcp-btn:hover {
  color: var(--error);
  border-color: var(--error);
}

/* Messages area */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: var(--bg-chat);
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}

/* Message bubble */
.message {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--radius);
  word-wrap: break-word;
  white-space: pre-wrap;
  animation: fadeIn 0.15s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.message-user {
  align-self: flex-end;
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.message-assistant {
  align-self: flex-start;
  background: var(--bg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}

.message-assistant .message-text {
  min-height: 1.5em;
}

.message-assistant .message-text a {
  color: var(--primary);
  text-decoration: underline;
}

/* Execution cards */
/* Tool pills — compact inline badges */
.tool-pill {
  display: inline-block;
  padding: 2px 10px;
  margin: 3px 4px 3px 0;
  border-radius: 4px;
  font-size: 12px;
  font-family: var(--font-mono);
  background: rgba(99, 102, 241, 0.12);
  color: #a5b4fc;
  border: 1px solid rgba(99, 102, 241, 0.2);
}
.tool-pill.error {
  background: rgba(239, 68, 68, 0.12);
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.2);
}

/* Working indicator */
.working-indicator {
  align-self: flex-start;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  color: var(--text-secondary);
  font-size: 14px;
  animation: fadeIn 0.15s ease-out;
}

.working-dots {
  display: flex;
  gap: 4px;
}

.working-dots span {
  width: 6px;
  height: 6px;
  background: var(--text-secondary);
  border-radius: 50%;
  animation: bounce 1.2s infinite;
}

.working-dots span:nth-child(2) { animation-delay: 0.2s; }
.working-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-4px); }
}

/* Input area */
.chat-input-area {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

.chat-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.chat-input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-secondary);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  resize: none;
  outline: none;
  max-height: 120px;
  transition: border-color 0.15s;
}

.chat-input:focus {
  border-color: var(--primary);
}

.chat-input::placeholder {
  color: var(--text-secondary);
}

.chat-send-btn,
.chat-cancel-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, opacity 0.15s, border-color 0.15s, box-shadow 0.15s;
}

.chat-send-btn {
  background: var(--primary);
  color: #fff;
}

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

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

.chat-cancel-btn {
  background: transparent;
  color: var(--error);
  border: 2px solid var(--error);
  animation: cancel-pulse 1.5s ease-in-out infinite;
}

.chat-cancel-btn:hover {
  background: var(--error);
  color: #fff;
  opacity: 1;
}

/* Utility */
.hidden {
  display: none !important;
}

/* Error banner */
.error-banner {
  padding: 10px 14px;
  background: #2d1b1b;
  border: 1px solid #5c2020;
  border-radius: var(--radius-sm);
  color: var(--error);
  font-size: 14px;
  align-self: stretch;
}
.notice-banner {
  padding: 10px 14px;
  background: #1b2d1b;
  border: 1px solid #205c20;
  border-radius: var(--radius-sm);
  color: #6fcf6f;
  font-size: 14px;
  align-self: stretch;
  animation: notice-fade 6s ease-in-out forwards;
}
.notice-banner.reconnecting {
  background: #2d2a1b;
  border-color: #5c5020;
  color: #cfbf6f;
  animation: reconnect-pulse 1.5s ease-in-out infinite;
}
@keyframes reconnect-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}
@keyframes notice-fade {
  0%, 70% { opacity: 1; }
  100% { opacity: 0; }
}

/* Responsive */
@media (max-width: 480px) {
  .chat-messages {
    padding: 12px;
  }

  .message {
    max-width: 92%;
  }

  .chat-input-area {
    padding: 8px 12px;
  }
}

/* App nav (shared between studio and dashboard) */
.app-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 48px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

.app-nav-brand {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
}

.app-nav-links {
  display: flex;
  align-items: center;
  gap: 6px;
}

.app-nav-link {
  padding: 5px 12px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  text-decoration: none;
  transition: all 0.15s;
}

.app-nav-link:hover {
  color: var(--text);
  background: var(--bg-secondary);
}

.app-nav-link.active {
  color: var(--primary-light);
  background: rgba(99, 102, 241, 0.1);
}

/* Auth bar in nav */
.auth-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: 12px;
  padding-left: 12px;
  border-left: 1px solid var(--border);
}

.auth-email {
  font-size: 12px;
  color: var(--text-secondary);
}

.plan-badge {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--primary-light);
  background: rgba(99, 102, 241, 0.15);
  border: 1px solid rgba(99, 102, 241, 0.3);
  padding: 2px 8px;
  border-radius: 4px;
}

.auth-logout-btn {
  padding: 4px 10px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 11px;
  cursor: pointer;
  transition: all 0.15s;
}

.auth-logout-btn:hover {
  color: var(--error);
  border-color: var(--error);
}

/* Markdown rendering */
.md-code-block {
  background: #0a0a14;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px;
  font-family: var(--font-mono);
  font-size: 13px;
  overflow-x: auto;
  margin: 8px 0;
  white-space: pre;
}

.md-code {
  background: rgba(99, 102, 241, 0.15);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 13px;
}

.md-h { margin: 12px 0 6px; color: var(--text); }
h2.md-h { font-size: 18px; }
h3.md-h { font-size: 16px; }
h4.md-h { font-size: 14px; }

.md-ul, .md-ol { margin: 6px 0; padding-left: 20px; }
.md-li, .md-oli { margin: 2px 0; }

.md-link { color: var(--primary-light); text-decoration: underline; }

/* === File Upload === */

/* Attach button (paperclip) */
.chat-attach-btn {
  width: 40px;
  height: 40px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.15s;
}

.chat-attach-btn:hover {
  color: var(--primary-light);
  border-color: var(--primary);
}

/* File preview bar (above the form, shows pending files) */
.file-preview-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 0 4px;
}

.file-preview-item {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 12px;
  color: var(--text);
  animation: fadeIn 0.15s ease-out;
}

.file-preview-name {
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-preview-size {
  color: var(--text-secondary);
  font-size: 11px;
}

.file-preview-remove {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 16px;
  cursor: pointer;
  padding: 0 2px;
  line-height: 1;
}

.file-preview-remove:hover {
  color: var(--error);
}

/* Drop zone overlay */
.drop-zone-overlay {
  position: absolute;
  inset: 0;
  z-index: 100;
  background: rgba(99, 102, 241, 0.08);
  border: 2px dashed var(--primary);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.drop-zone-overlay.hidden {
  display: none;
}

.drop-zone-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  color: var(--primary-light);
  font-size: 16px;
  font-weight: 500;
}

.drop-zone-content svg {
  opacity: 0.7;
}

/* Make main-area relative for the drop overlay */
.main-area {
  position: relative;
}

/* Upload progress indicator in messages */
.upload-progress {
  align-self: flex-start;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  color: var(--text-secondary);
  font-size: 13px;
}

.upload-progress .spinner {
  width: 14px;
  height: 14px;
}

/* Thinking block (reasoning with left border accent) */
.thinking-block {
  margin: 8px 0;
  border-left: 3px solid rgba(99, 102, 241, 0.5);
  border-radius: 0 6px 6px 0;
  background: rgba(99, 102, 241, 0.06);
  overflow: hidden;
}
.thinking-block.thinking-active {
  animation: thinking-pulse 2s ease-in-out infinite;
}
@keyframes thinking-pulse {
  0%, 100% { border-left-color: rgba(99, 102, 241, 0.4); }
  50% { border-left-color: #a78bfa; }
}
.thinking-header {
  padding: 6px 10px;
  cursor: pointer;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 6px;
}
.thinking-header:hover { background: rgba(99, 102, 241, 0.08); }
.thinking-icon { display: none; }
.thinking-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: #a78bfa;
  background: rgba(99, 102, 241, 0.15);
  padding: 2px 8px;
  border-radius: 4px;
}
.thinking-content {
  padding: 6px 12px 10px;
  font-size: 13px;
  color: #94a3b8;
  max-height: 200px;
  overflow-y: auto;
}
.thinking-block.collapsed .thinking-content {
  display: none;
}
.thinking-content p { margin: 4px 0; }

/* Streaming indicator — pulsing cursor at end of text */
.message-assistant.streaming .message-text::after {
  content: '';
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--primary-light);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: cursor-blink 0.8s step-end infinite;
}
@keyframes cursor-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* Cancel button pulse keyframes */
@keyframes cancel-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
  50% { box-shadow: 0 0 0 4px rgba(239, 68, 68, 0); }
}
