/* Intel Engine Frontend
   Sophisticated, minimal, elegant. */

:root {
  --bg: #fafbfc;
  --surface: #ffffff;
  --surface-alt: #f6f8fa;
  --border: #e1e4e8;
  --border-light: #eef0f2;
  --text: #1a1a2e;
  --text-secondary: #586069;
  --text-muted: #8892b0;
  --accent: #3f51b5;
  --accent-light: #e8eaf6;
  --accent-dark: #303f9f;
  --header-bg: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  --success: #2e7d32;
  --success-light: #e8f5e9;
  --warning: #e65100;
  --warning-light: #fff3e0;
  --error: #c62828;
  --error-light: #ffebee;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 2px 12px rgba(0,0,0,0.06);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.10);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  --transition: 0.2s ease;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.6;
  height: 100vh;
  height: 100dvh; /* Safari dynamic viewport height */
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---- Header ---- */
.app-header {
  background: var(--header-bg);
  padding: 16px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
  z-index: 10;
}

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

.app-logo {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: rgba(255,255,255,0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: #8892b0;
}

.app-name {
  font-size: 15px;
  font-weight: 600;
  color: #ffffff;
  letter-spacing: 0.5px;
}

.app-subtitle {
  font-size: 11px;
  color: #8892b0;
  letter-spacing: 0.3px;
}

.beta-badge {
  display: inline-block;
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  padding: 2px 10px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: #8892b0;
  vertical-align: middle;
  margin-left: 8px;
}

.header-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

.header-user {
  font-size: 12px;
  color: #8892b0;
  margin-right: 4px;
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  font-family: var(--font);
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent-dark);
}
.btn-primary:hover { background: var(--accent-dark); }

.btn-secondary {
  background: rgba(255,255,255,0.08);
  color: #ccd6f6;
  border-color: rgba(255,255,255,0.12);
}
.btn-secondary:hover {
  background: rgba(255,255,255,0.14);
  color: #ffffff;
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border-color: var(--border);
}
.btn-ghost:hover {
  background: var(--surface-alt);
  border-color: var(--accent);
  color: var(--accent);
}

.btn-success {
  background: var(--success);
  color: white;
}
.btn-success:hover { background: #1b5e20; }

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

/* ---- Main Layout ---- */
.app-main {
  flex: 1;
  display: flex;
  overflow: hidden;
  min-height: 0; /* Safari flex fix */
}

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

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

.sidebar-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--text-muted);
}

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

.project-item {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  margin-bottom: 2px;
}

.project-item:hover { background: var(--surface-alt); }
.project-item.active {
  background: var(--accent-light);
  border-left: 3px solid var(--accent);
  padding-left: 9px;
}

.project-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 2px;
}

.project-meta {
  font-size: 10px;
  color: var(--text-muted);
  display: flex;
  gap: 8px;
}

.project-badge {
  font-size: 9px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.badge-ready { background: var(--success-light); color: var(--success); }
.badge-setup { background: var(--warning-light); color: var(--warning); }

/* ---- Content Area ---- */
.content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0; /* Safari flex fix */
  overflow: hidden;
}

/* ---- Welcome Screen ---- */
.welcome {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.welcome-inner {
  text-align: center;
  max-width: 440px;
}

.welcome-icon {
  font-size: 48px;
  margin-bottom: 20px;
  opacity: 0.3;
}

.welcome-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 8px;
}

.welcome-text {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.7;
  margin-bottom: 12px;
}

.welcome-steps {
  text-align: left;
  margin: 16px auto;
  max-width: 380px;
}

.welcome-step {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 12px;
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.5;
}

.welcome-step-num {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---- Footer ---- */
.app-footer {
  padding: 8px 24px;
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  border-top: 1px solid var(--border-light);
  flex-shrink: 0;
}

/* ---- Terms Modal ---- */
.terms-modal {
  max-width: 560px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.terms-content {
  font-size: 12px;
  line-height: 1.7;
  color: var(--text-secondary);
  overflow-y: auto;
  max-height: 400px;
  padding: 16px 0;
  border-top: 1px solid var(--border-light);
  border-bottom: 1px solid var(--border-light);
  margin: 12px 0;
}

.terms-content p {
  margin-bottom: 10px;
}

.terms-placeholder {
  color: var(--text-muted);
  font-size: 11px;
  margin-top: 16px;
}

.terms-accept-row {
  padding: 12px 0;
}

.terms-checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
}

.terms-checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

/* ---- Feedback Panel ---- */
.feedback-tab {
  position: fixed;
  bottom: 100px;
  right: 0;
  background: var(--accent);
  color: white;
  padding: 8px 16px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  border-radius: 6px 0 0 6px;
  box-shadow: var(--shadow);
  z-index: 100;
  transition: all var(--transition);
  font-family: var(--font);
}

.feedback-tab:hover {
  padding-right: 24px;
}

.feedback-panel {
  position: fixed;
  bottom: 100px;
  right: 0;
  width: 360px;
  max-width: 90vw;
  background: var(--surface);
  border-radius: var(--radius) 0 0 var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 101;
  overflow: hidden;
  box-sizing: border-box;
}


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

.feedback-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}

.feedback-close {
  background: none;
  border: none;
  font-size: 20px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0 4px;
}

.feedback-body {
  padding: 0;
  display: flex;
  flex-direction: column;
  height: 340px;
  overflow: hidden;
}

.feedback-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px 16px;
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
}

.feedback-msg {
  margin-bottom: 10px;
  max-width: 280px;
  min-width: 0;
}

.feedback-msg-agent {
  margin-right: auto;
}

.feedback-msg-user {
  margin-left: auto;
}

.feedback-msg-bubble {
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 12px;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: break-word;
  max-width: 100%;
}

.feedback-msg-agent .feedback-msg-bubble {
  background: var(--surface-alt);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

.feedback-msg-user .feedback-msg-bubble {
  background: var(--accent);
  color: white;
  border-bottom-right-radius: 4px;
}

.feedback-input-area {
  display: flex;
  gap: 6px;
  padding: 10px 12px;
  border-top: 1px solid var(--border-light);
}

.feedback-chat-input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 16px;
  font-size: 13px;
  font-family: var(--font);
  color: var(--text);
  resize: none;
  min-height: 32px;
  max-height: 80px;
  overflow-y: auto;
  line-height: 1.4;
}

.feedback-chat-input:focus {
  outline: none;
  border-color: var(--accent);
}

.feedback-send-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.feedback-send-btn:hover {
  background: var(--accent-dark);
}

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

.feedback-success {
  text-align: center;
  color: var(--success);
  font-size: 13px;
  font-weight: 600;
  padding: 16px;
}

.feedback-typing {
  font-size: 11px;
  color: var(--text-muted);
  padding: 4px 16px;
  font-style: italic;
}

/* ---- Content Header (project selected) ---- */
.content-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border-light);
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--surface);
  flex-shrink: 0;
}

.content-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}

.content-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* ---- Tabs ---- */
.tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border-light);
  background: var(--surface);
  padding: 0 24px;
  flex-shrink: 0;
}

.tab {
  padding: 10px 16px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all var(--transition);
}

.tab:hover { color: var(--text-secondary); }
.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* ---- Tab Panels ---- */
.tab-panel {
  flex: 1;
  display: none;
  flex-direction: column;
  overflow: hidden;
  min-height: 0; /* Safari flex fix */
}

.tab-panel.active {
  display: flex;
}

/* ---- Chat ---- */
.chat-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0; /* Safari flex fix */
}

.phase-bar {
  padding: 10px 24px;
  background: var(--accent-light);
  border-bottom: 1px solid #c5cae9;
  font-size: 11px;
  font-weight: 600;
  color: var(--accent-dark);
  letter-spacing: 0.3px;
  flex-shrink: 0;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  scroll-behavior: smooth;
  min-height: 0; /* Safari flex fix */
}

.message {
  max-width: 85%;
  margin-bottom: 16px;
  animation: messageIn 0.25s ease;
}

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

.message-assistant {
  margin-right: auto;
}

.message-user {
  margin-left: auto;
}

.message-bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  line-height: 1.7;
}

.message-assistant .message-bubble {
  background: var(--surface);
  border: 1px solid var(--border-light);
  color: var(--text);
  border-top-left-radius: 3px;
}

.message-user .message-bubble {
  background: var(--accent);
  color: white;
  border-top-right-radius: 3px;
}

.message-bubble p { margin-bottom: 8px; }
.message-bubble p:last-child { margin-bottom: 0; }
.message-bubble strong { font-weight: 700; }
.message-bubble code {
  font-family: var(--font-mono);
  font-size: 12px;
  background: rgba(0,0,0,0.06);
  padding: 1px 5px;
  border-radius: 3px;
}
.message-user .message-bubble code {
  background: rgba(255,255,255,0.15);
}

.message-bubble pre {
  background: #1a1a2e;
  color: #a8b2d8;
  padding: 12px;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 12px;
  margin: 8px 0;
}

.message-time {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 4px;
  padding: 0 4px;
}

.message-user .message-time { text-align: right; }

.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 12px 16px;
  align-items: center;
}

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

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

@keyframes typing {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

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

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

.chat-input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  font-family: var(--font);
  resize: none;
  min-height: 42px;
  max-height: 120px;
  line-height: 1.5;
  transition: border-color var(--transition);
  color: var(--text);
  background: var(--surface);
}

.chat-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(63, 81, 181, 0.1);
}

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

.upload-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
  flex-shrink: 0;
}

.upload-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-light);
}

.upload-btn svg { width: 16px; height: 16px; }

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

.send-btn:hover { background: var(--accent-dark); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.send-btn svg {
  width: 18px;
  height: 18px;
}

/* ---- Digests Panel ---- */
.digests-panel {
  padding: 24px;
  overflow-y: auto;
}

.digest-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.digest-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all var(--transition);
}

.digest-item:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow);
}

.digest-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.digest-date {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.digest-meta {
  font-size: 11px;
  color: var(--text-muted);
}

.digest-type {
  font-size: 10px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.type-daily { background: var(--accent-light); color: var(--accent); }
.type-weekly { background: var(--success-light); color: var(--success); }

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-muted);
}

.empty-state-icon {
  font-size: 36px;
  margin-bottom: 12px;
  opacity: 0.3;
}

.empty-state-text {
  font-size: 13px;
  line-height: 1.6;
}

/* ---- Run Status ---- */
.run-status {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

.run-pending { background: var(--warning-light); color: var(--warning); }
.run-running { background: var(--accent-light); color: var(--accent); }
.run-completed { background: var(--success-light); color: var(--success); }
.run-failed { background: var(--error-light); color: var(--error); }

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---- Modal ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fadeIn 0.15s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 24px;
  width: 400px;
  max-width: 90vw;
  animation: modalIn 0.2s ease;
}

@keyframes modalIn {
  from { opacity: 0; transform: scale(0.95) translateY(10px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.modal-title {
  font-size: 16px;
  font-weight: 700;
  margin-bottom: 16px;
  color: var(--text);
}

.modal-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: var(--font);
  margin-bottom: 16px;
  color: var(--text);
}

.modal-input:focus {
  outline: none;
  border-color: var(--accent);
}

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

/* ---- Digest Viewer ---- */
.digest-viewer {
  flex: 1;
  overflow: hidden;
  display: none;
}

.digest-viewer.active { display: flex; flex-direction: column; }

.digest-viewer-header {
  padding: 12px 24px;
  border-bottom: 1px solid var(--border-light);
  background: var(--surface);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

.digest-viewer-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.digest-iframe {
  flex: 1;
  border: none;
  width: 100%;
}

/* ---- Mobile ---- */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: -280px;
    top: 0;
    bottom: 0;
    z-index: 50;
    width: 280px;
    transition: left var(--transition);
    box-shadow: var(--shadow-lg);
  }

  .sidebar.open { left: 0; }

  .mobile-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.3);
    z-index: 49;
    display: none;
  }

  .mobile-overlay.active { display: block; }

  .message { max-width: 95%; }

  .app-header { padding: 12px 16px; }

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

  .content-header { padding: 12px 16px; }
  .tabs { padding: 0 16px; }
}

/* ---- Config Ready Banner ---- */
.config-ready-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  background: var(--success-light);
  border: 1px solid #c8e6c9;
  border-radius: var(--radius);
  animation: messageIn 0.3s ease;
}

.config-ready-icon {
  color: var(--success);
  flex-shrink: 0;
}

.config-ready-text {
  font-size: 13px;
  font-weight: 600;
  color: var(--success);
  line-height: 1.5;
}

.btn-pulse {
  animation: btnPulse 1s ease-in-out 3;
}

@keyframes btnPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(46, 125, 50, 0); }
  50% { box-shadow: 0 0 0 6px rgba(46, 125, 50, 0.3); }
}

/* ---- Scrollbar ---- */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: #d1d5db;
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover { background: #9ca3af; }

/* ---- Markdown in messages ---- */
.message-bubble h1, .message-bubble h2, .message-bubble h3 {
  font-size: 14px;
  font-weight: 700;
  margin-top: 12px;
  margin-bottom: 4px;
}

.message-bubble ul, .message-bubble ol {
  padding-left: 20px;
  margin: 6px 0;
}

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

.message-bubble blockquote {
  border-left: 3px solid var(--border);
  padding-left: 12px;
  color: var(--text-secondary);
  margin: 8px 0;
}

.message-bubble hr {
  border: none;
  border-top: 1px solid var(--border-light);
  margin: 12px 0;
}

.message-bubble table {
  border-collapse: collapse;
  margin: 8px 0;
  font-size: 12px;
  width: 100%;
}

.message-bubble th, .message-bubble td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}

.message-bubble th {
  background: var(--surface-alt);
  font-weight: 700;
}
