/**
 * NAI Dashboard - FINAL FIXED CSS v6
 * All layout, sizing, and styling issues fixed
 */

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

.settings-accordion-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.settings-accordion-section {
  border: 1px solid var(--border-color, rgba(148, 163, 184, .28));
  border-radius: 8px;
  background: var(--card-bg, rgba(15, 23, 42, .72));
  margin-bottom: 12px;
  overflow: clip;
}

.settings-accordion-summary {
  cursor: pointer;
  list-style: none;
  padding: 13px 16px;
  font-weight: 700;
  color: var(--text-primary, #f8fafc);
  display: flex;
  align-items: center;
  gap: 10px;
  user-select: none;
}

.settings-accordion-summary::-webkit-details-marker {
  display: none;
}

.settings-accordion-summary::before {
  content: "▶";
  font-size: .8rem;
  color: var(--text-secondary, #94a3b8);
  width: 1em;
}

.settings-accordion-section[open] > .settings-accordion-summary::before {
  content: "▼";
}

.settings-accordion-section > :not(summary) {
  padding-left: 16px;
  padding-right: 16px;
}

.settings-accordion-section > :last-child {
  padding-bottom: 16px;
}

.effort-select {
  flex: 0 0 auto;
  width: 148px;
  min-width: 132px;
  max-width: 170px;
  align-self: center;
  height: 34px;
  padding: 0 22px 0 8px;
  margin-left: 0;
  margin-right: 0;
  border-radius: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-sources {
  margin-top: 10px;
  padding: 10px;
  border: 1px solid var(--border-color, rgba(148, 163, 184, .28));
  border-radius: 8px;
  background: rgba(15, 23, 42, .35);
  font-size: .86rem;
}

.message-source-item {
  margin-top: 5px;
  overflow-wrap: anywhere;
}

.settings-debug-output {
  white-space: pre-wrap;
  background: rgba(15, 23, 42, .48);
  border: 1px solid var(--border-color, rgba(148, 163, 184, .28));
  border-radius: 8px;
  padding: 10px;
  color: var(--text-secondary, #cbd5e1);
  max-height: 180px;
  overflow: auto;
}

@media (max-width: 768px) {
  .modal-dialog,
  .modal-content,
  .modal-box {
    max-width: calc(100vw - 20px) !important;
    max-height: calc(100vh - 20px) !important;
  }

  .modal-body {
    max-height: calc(100vh - 150px);
    overflow: auto;
  }

  .permission-row,
  .health-row {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .settings-grid,
  .settings-grid.two-col,
  .settings-grid.three-col,
  .settings-check-grid {
    grid-template-columns: 1fr !important;
  }

  .effort-select {
    flex: 1 1 100%;
    width: 100%;
    min-width: 100%;
    max-width: 100%;
  }

  .settings-accordion-summary {
    padding: 12px;
  }
}

html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}

/* Mobile viewport height fix for iOS Safari */
:root {
  --vh: 1vh;
}

body {
  height: 100vh; /* Fallback */
  height: calc(var(--vh, 1vh) * 100); /* Dynamic height for mobile */
}

/* ========== THEME ========== */
body.dark {
  --bg-primary: #000000;
  --bg-secondary: #0a0a0a;
  --bg-tertiary: #1a1a1a;
  --card-bg: #111111;
  --text-primary: #ffffff;
  --text-secondary: rgba(255, 255, 255, 0.7);
  --border-color: rgba(255, 255, 255, 0.1);
  --accent-color: #00ff88;
  --input-bg: rgba(255, 255, 255, 0.05);
  --input-text: #ffffff;
  --dropdown-bg: #1a1a1a;
  --dropdown-text: #ffffff;
  --notification-bg: #1a1a1a;
  --toast-bg: #1a1a1a;
}

body.light {
  --bg-primary: #ffffff;
  --bg-secondary: #f5f5f5;
  --bg-tertiary: #eeeeee;
  --card-bg: #ffffff;
  --text-primary: #000000;
  --text-secondary: rgba(0, 0, 0, 0.6);
  --border-color: rgba(0, 0, 0, 0.12);
  --accent-color: #00dd77;
  --input-bg: rgba(0, 0, 0, 0.05);
  --input-text: #000000;
  --dropdown-bg: #ffffff;
  --dropdown-text: #000000;
  --notification-bg: #ffffff;
  --toast-bg: #ffffff;
}

body {
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: var(--base-font-size, 16px);
}

/* ========== NAVBAR ========== */
.nai-navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  z-index: 1000;
}

.navbar-container {
  width: 100%;
  height: 100%;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.navbar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text-primary);
  text-decoration: none;
  font-size: 20px;
  font-weight: 600;
}

.navbar-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

/* FIX: User info - HORIZONTAL layout (avatar and name side-by-side) */
.user-info {
  display: flex !important;
  flex-direction: row !important;
  align-items: center !important;
  justify-content: flex-start !important;
  gap: 8px !important;
  padding: 6px 12px !important;
  background: var(--input-bg);
  border-radius: 8px;
  min-width: auto;
}

.user-avatar-img,
.user-avatar-icon {
  width: 32px !important;
  height: 32px !important;
  max-width: 32px !important;
  max-height: 32px !important;
  min-width: 32px !important;
  min-height: 32px !important;
  border-radius: 50% !important;
  object-fit: cover;
  display: inline-flex !important;
  align-items: center !important;
  justify-content: center !important;
  margin: 0 !important;
  flex-shrink: 0 !important;
}

.user-avatar-icon {
  font-size: 18px;
  background: linear-gradient(135deg, #aa00ff, #ff6b6b);
  color: white;
}

.user-name {
  font-size: 14px !important;
  font-weight: 500 !important;
  color: var(--text-secondary) !important;
  text-align: left !important;
  line-height: 1.2 !important;
  white-space: nowrap !important;
  display: inline-block !important;
  margin: 0 !important;
}

/* ========== BUTTONS ========== */
.btn-icon {
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-icon:hover {
  background: var(--input-bg);
  border-color: var(--accent-color);
}

/* Mobile action buttons (Video, Web, Tools, Agent icons) */
.btn-mobile-action {
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  font-size: 16px;
}

.btn-mobile-action:hover {
  background: var(--input-bg);
  border-color: var(--accent-color);
}

.btn-primary {
  background: var(--accent-color);
  color: #000000;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.2s;
}

.btn-primary:hover {
  filter: brightness(1.1);
}

.btn-secondary {
  background: transparent;
  color: var(--text-primary);
  border: 1px solid var(--border-color);
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-secondary:hover {
  background: var(--input-bg);
  border-color: var(--accent-color);
}

.btn-secondary.active {
  background: var(--accent-color);
  color: #fff;
  border-color: var(--accent-color);
}

/* ========== CONTAINER ========== */
.nai-container {
  display: flex;
  height: calc(100vh - 60px);
  margin-top: 60px;
  overflow: hidden;
  position: relative;
}

/* ========== SIDEBAR ========== */
.nai-sidebar {
  width: 280px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
  transition: margin-left 0.3s ease;
  position: relative;
  z-index: 100;
}

.nai-sidebar.collapsed {
  margin-left: -280px;
}

/* FIX: Reopen handle - FORCE visibility when collapsed */
.sidebar-reopen-handle {
  position: fixed !important;
  left: 0 !important;
  top: 50% !important;
  transform: translateY(-50%) !important;
  width: 24px !important;
  height: 80px !important;
  background: var(--accent-color) !important;
  border-radius: 0 12px 12px 0 !important;
  display: none !important;
  align-items: center !important;
  justify-content: center !important;
  cursor: pointer !important;
  z-index: 999 !important;
  transition: all 0.3s ease !important;
  box-shadow: 2px 0 12px rgba(0, 255, 136, 0.5) !important;
}

.sidebar-reopen-handle:hover {
  width: 32px !important;
  background: var(--accent-color) !important;
  filter: brightness(1.3) !important;
  box-shadow: 2px 0 20px rgba(0, 255, 136, 0.8) !important;
}

.sidebar-reopen-handle::before {
  content: '▶' !important;
  color: #000000 !important;
  font-size: 16px !important;
  font-weight: bold !important;
}

/* Show handle ONLY when sidebar is collapsed */
body.sidebar-collapsed .sidebar-reopen-handle,
.nai-sidebar.collapsed + .sidebar-reopen-handle {
  display: flex !important;
}

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

.sidebar-header h3 {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
}

.sidebar-content {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.sidebar-content::-webkit-scrollbar {
  width: 6px;
}

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

.chat-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.chat-list li {
  padding: 12px;
  margin-bottom: 8px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
}

.chat-list li:hover {
  background: var(--bg-tertiary);
  border-color: var(--accent-color);
}

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid var(--border-color);
}

.btn-new-chat {
  width: 100%;
  background: var(--accent-color);
  color: #000000;
  border: none;
  padding: 10px;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: all 0.2s;
}

.btn-new-chat:hover {
  filter: brightness(1.1);
}

/* ========== MAIN ========== */
.nai-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

/* ========== USER DROPDOWN MENU ========== */
.user-dropdown-menu {
  position: fixed;
  top: 70px;
  right: 20px;
  width: 200px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  opacity: 0;
  visibility: hidden;
  transform: translateY(-10px);
  transition: all 0.2s ease;
  z-index: 10000;
  overflow: hidden;
  pointer-events: none;
}

.user-dropdown-menu.active {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
  pointer-events: auto;
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  color: var(--text-primary);
  text-decoration: none;
  transition: background 0.2s ease;
  font-size: 14px;
}

.dropdown-item:hover {
  background: var(--bg-tertiary);
}

.dropdown-icon {
  font-size: 16px;
  width: 20px;
  text-align: center;
}

.dropdown-divider {
  height: 1px;
  background: var(--border-color);
  margin: 4px 0;
}

/* ========== OPTIONS TOGGLE ========== */
.controls-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: linear-gradient(135deg, rgba(170, 0, 255, 0.1), rgba(255, 107, 107, 0.1));
  border-bottom: 2px solid rgba(170, 0, 255, 0.3);
  cursor: pointer;
  user-select: none;
  flex-shrink: 0;
  transition: all 0.2s ease;
  z-index: 100;
  position: relative;
}

.controls-toggle:hover {
  background: linear-gradient(135deg, rgba(170, 0, 255, 0.15), rgba(255, 107, 107, 0.15));
}

.controls-toggle:active {
  background: linear-gradient(135deg, rgba(170, 0, 255, 0.2), rgba(255, 107, 107, 0.2));
}

.controls-toggle-text {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
}

.controls-toggle-icon {
  font-size: 20px;
  transition: transform 0.3s ease;
  color: #aa00ff;
}

.controls-toggle.active .controls-toggle-icon {
  transform: rotate(180deg);
}

/* ========== CONTROLS CONTAINER (Wraps BOTH Rows) ========== */
.controls-container {
  max-height: 800px;
  opacity: 1;
  overflow: visible;
  transition: max-height 0.4s ease, opacity 0.3s ease;
  flex-shrink: 0;
  position: relative;
  z-index: 10; /* Lower z-index so it doesn't overlay content */
  background: var(--bg-primary);
  pointer-events: auto;
}

.controls-container.expanded {
  max-height: 800px;
  opacity: 1;
  overflow: visible;
  background: var(--bg-primary);
  pointer-events: auto;
  z-index: 10;
}

/* Desktop: Controls always visible, toggle button hidden */
@media (min-width: 769px) {
  .controls-toggle {
    display: none !important;
  }
  
  .controls-container {
    max-height: none !important;
    opacity: 1 !important;
    overflow: visible !important;
    background: transparent !important;
    z-index: auto;
  }
}

/* ========== CONTROLS ========== */
.controls-row {
  padding: 16px 20px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  gap: 16px;
  align-items: flex-end;
  flex-wrap: wrap;
  flex-shrink: 0;
  position: relative;
  pointer-events: auto;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 180px;
}

.control-group label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
}

.control-group.flex-grow-1 {
  flex: 1;
  min-width: 200px;
}

.effort-control-group {
  min-width: 150px;
}

.cloud-model-control {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.cloud-model-control select {
  flex: 1 1 auto;
  min-width: 0;
}

.cloud-model-refresh-btn {
  flex: 0 0 auto;
  width: 34px;
  min-width: 34px;
  min-height: 34px;
  padding: 0;
  white-space: nowrap;
  font-size: 18px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.form-select, .form-control {
  background: var(--dropdown-bg);
  border: 1px solid var(--border-color);
  color: var(--dropdown-text);
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 14px;
  outline: none;
}

.form-select option {
  background: var(--dropdown-bg);
  color: var(--dropdown-text);
}

.btn-group-horizontal {
  display: flex;
  gap: 8px;
}

.btn-think-tank {
  background: linear-gradient(135deg, #ff6b6b, #ffd93d);
  color: #000000;
  border: none;
  padding: 10px 20px;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

/* ========== PANELS ========== */
.content-panel {
  display: none;
  flex-direction: column;
  overflow: hidden;
  flex: 1;
  min-height: 0;
}

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

/* ========== CHAT PANEL ========== */
#chatPanel {
  flex-direction: column;
}

.chat-window, .chat-output-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  min-height: 0;
}

.chat-window::-webkit-scrollbar,
.chat-output-container::-webkit-scrollbar {
  width: 8px;
}

.chat-window::-webkit-scrollbar-thumb,
.chat-output-container::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 4px;
}

.welcome-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
}

.welcome-icon {
  font-size: 64px;
  margin-bottom: 20px;
}

.welcome-screen h2 {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 12px;
}

.features {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 16px;
}

.feature-badge {
  background: rgba(0, 255, 136, 0.1);
  border: 1px solid rgba(0, 255, 136, 0.3);
  color: var(--accent-color);
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 14px;
}

.chat-input-container {
  flex-shrink: 0;
  background: var(--bg-secondary);
}

.prompt-bar {
  padding: 12px 20px;
  border-top: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  gap: 12px;
  align-items: center;
}

.file-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-tertiary, #1e1e1e);
  border: 1px solid var(--border-color, #2a2a2a);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 0.78rem;
  color: var(--text-primary);
  max-width: 200px;
  overflow: hidden;
}
.file-chip-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.file-chip-size {
  color: var(--text-secondary, #888);
  flex-shrink: 0;
}
.file-chip-remove {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-secondary, #888);
  font-size: 12px;
  padding: 0 2px;
  line-height: 1;
  flex-shrink: 0;
}
.file-chip-remove:hover { color: var(--text-primary); }

.input-bar {
  padding: 16px 20px;
  border-top: 1px solid var(--border-color);
  display: flex;
  gap: 12px;
  align-items: flex-end;
  min-height: 76px;
}

/* ========== MESSAGES ========== */
/* ========== CHAT MESSAGES ========== */
.message {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 20px;
  width: 100%;
  max-width: 100%;
}

.message.user {
  align-self: flex-end;
  align-items: flex-end;
}

.message.ai {
  align-self: flex-start;
  align-items: flex-start;
}

/* Message metadata (avatar + name) */
.msg-meta {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.message.user .msg-meta {
  align-items: flex-end;
}

.message.ai .msg-meta {
  align-items: flex-start;
}

/* User avatar in chat messages */
.message .user-avatar {
  width: 40px !important;
  height: 40px !important;
  border-radius: 50%;
  object-fit: cover;
  display: block;
}

/* User name in chat messages */
.message .user-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  display: block;
}

/* Message body */
.msg-body {
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  padding: 12px 16px;
  border-radius: 12px;
  line-height: 1.6;
  color: var(--text-primary);
  word-wrap: break-word;
  width: 100%;
}

.message.user .msg-body {
  background: linear-gradient(135deg, rgba(170, 0, 255, 0.1), rgba(255, 107, 107, 0.1));
  border-color: rgba(170, 0, 255, 0.3);
}

/* Message actions */
.msg-actions {
  display: flex;
  gap: 8px;
  opacity: 0.5;
  transition: opacity 0.2s;
}

.message:hover .msg-actions {
  opacity: 1;
}

.copy-icon {
  cursor: pointer;
  font-size: 14px;
}

/* Image/PDF previews */
.img-preview,
.pdf-preview {
  max-width: 100%;
  border-radius: 8px;
  margin-top: 8px;
}

/* OLD MESSAGE CSS - DEPRECATED */

/* ========== INPUT ========== */
.btn-attach {
  width: 44px;
  height: 44px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  color: var(--text-primary);
}

.input-textarea {
  flex: 1;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  color: var(--input-text);
  padding: 12px 16px;
  border-radius: 8px;
  resize: none;
  min-height: 44px;
  max-height: 40vh;   /* Chat Input Redesign: auto-grow cap (JS sizes height to scrollHeight ≤ 40vh, then scrolls) */
  font-size: 14px;
  line-height: 1.5;
  outline: none;
}

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

.btn-send {
  width: 44px;
  height: 44px;
  background: var(--accent-color);
  color: #000000;
  border: none;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}

/* v3.6 (Track C): the send button becomes a red STOP button while a response streams. */
.btn-send .stop-icon { display: none; }
.btn-send.streaming { background: var(--danger-color, #dc2626); color: #ffffff; }
.btn-send.streaming .send-icon { display: none; }
.btn-send.streaming .stop-icon { display: inline-flex; }

.btn-clear, .btn-export {
  width: 44px;
  height: 44px;
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}

/* ========== MEDIA/TOOLS/SETTINGS ========== */
.panel-header {
  padding: 20px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
  background: var(--bg-primary);
  z-index: 10;
  position: relative;
}

.panel-header h2 {
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.panel-actions {
  display: flex;
  gap: 12px;
}

/* FIX: Media library container */
#mediaPanel {
  flex-direction: column;
  position: relative !important;
  z-index: 5 !important;
}

#mediaPanel .panel-header {
  z-index: 20 !important;
}

#mediaPanel .media-grid {
  z-index: 10 !important;
}

/* FIX: Media buttons styling */
.media-form {
  padding: 20px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  margin: 20px;
  position: relative;
  z-index: 8;
}

.form-actions {
  display: flex;
  gap: 12px;
  margin-top: 16px;
}

.form-actions .btn-primary,
.form-actions .btn-secondary {
  padding: 10px 20px;
  font-size: 14px;
}

/* FIX: Settings scroll */
.settings-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto !important;
  min-height: 0;
}

.settings-content::-webkit-scrollbar {
  width: 8px;
}

.settings-content::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 4px;
}

.settings-section {
  margin-bottom: 32px;
  padding-bottom: 32px;
  border-bottom: 1px solid var(--border-color);
}

.settings-check-grid.compact {
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px 12px;
}

.settings-code-preview {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: var(--bg-primary);
  color: var(--text-primary);
  white-space: pre-wrap;
  word-break: break-word;
}

.settings-list-item {
  overflow-wrap: anywhere;
}

.usage-settings-rows .settings-list-item span {
  word-break: break-word;
}

.permission-row,
.health-row {
  display: grid;
  grid-template-columns: minmax(160px, 1fr) minmax(180px, 1fr) auto;
  gap: 10px;
  align-items: center;
}

.btn-mini {
  min-height: 32px;
  padding: 6px 10px;
  font-size: 0.82rem;
}

.nai-safe-mode #nai-theme-custom-css,
.nai-safe-mode .rag-module-hidden {
  display: none !important;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
summary:focus-visible {
  outline: 2px solid var(--accent-color);
  outline-offset: 2px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
}

/* FIX: Media grid with thumbnails */
.media-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.media-item {
  position: relative;
  aspect-ratio: 16/9;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.2s;
}

.media-item:hover {
  border-color: var(--accent-color);
  transform: scale(1.02);
}

.media-item img,
.media-item video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.media-item-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 8px 12px;
  background: linear-gradient(to top, rgba(0,0,0,0.9), transparent);
  color: white;
  font-size: 12px;
}

.media-item-actions {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.2s;
}

.media-item:hover .media-item-actions {
  opacity: 1;
}

.media-item-actions button {
  padding: 4px 8px;
  background: rgba(0, 0, 0, 0.8);
  border: 1px solid var(--accent-color);
  color: var(--accent-color);
  border-radius: 4px;
  font-size: 11px;
  cursor: pointer;
}

/* FIX: Tools grid with proper cards */
#toolsPanel {
  flex-direction: column;
}

.tools-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.tool-card {
  padding: 24px 20px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  cursor: pointer;
  text-align: center;
  transition: all 0.2s;
  min-height: 140px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.tool-card:hover {
  background: var(--bg-tertiary);
  border-color: var(--accent-color);
  transform: translateY(-2px);
}

.tool-icon {
  font-size: 40px;
  margin-bottom: 12px;
  display: block;
}

.tool-name {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 6px;
}

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

/* ========== MODAL ========== */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  z-index: 3000;
  align-items: center;
  justify-content: center;
}

.modal.show {
  display: flex !important;
}

.modal-content {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  width: 90%;
  max-width: 500px;
}

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

.modal-header h3 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.btn-close {
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-size: 24px;
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-close:hover {
  background: var(--input-bg);
}

.modal-body {
  padding: 20px;
}

.modal-footer {
  padding: 20px;
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* ========== RESPONSIVE ========== */
/* ========== MOBILE RESPONSIVE ========== */

/* Tablet & Small Desktop (768px - 1024px) */
@media (max-width: 1024px) {
  .nai-sidebar {
    width: 240px;
  }
  
  .navbar-brand {
    font-size: 18px;
  }
  
  .chat-window, .chat-output-container {
    padding: 16px;
  }
}

/* Mobile Landscape & Tablet Portrait (max-width: 768px) */
@media (max-width: 768px) {
  /* Sidebar - slide in from left */
  .nai-sidebar {
    position: fixed;
    left: -280px;
    top: 0;
    bottom: 0;
    z-index: 1000;
    transition: left 0.3s ease;
    box-shadow: 2px 0 10px rgba(0, 0, 0, 0.5);
  }
  
  .nai-sidebar.open {
    left: 0;
  }
  
  /* Overlay when sidebar is open */
  body.sidebar-open::after {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;
  }
  
  /* Main content takes full width */
  .nai-main {
    margin-left: 0;
    width: 100%;
  }
  
  /* User dropdown - adjust for mobile */
  .user-dropdown-menu {
    right: 10px;
    top: 60px;
    width: 180px;
  }
  
  /* Mobile: Controls collapsed by default, toggle visible */
  .controls-toggle {
    display: flex !important;
  }
  
  .controls-container {
    max-height: 0 !important;
    opacity: 0 !important;
    overflow: hidden !important;
    pointer-events: none !important;
  }
  
  .controls-container.expanded {
    max-height: 600px !important;
    opacity: 1 !important;
    overflow: visible !important;
    background: #000000 !important;
    z-index: 500 !important;
    pointer-events: auto !important;
    position: relative;
  }
  
  /* Navbar adjustments */
  .navbar {
    height: 56px;
    padding: 0 12px;
  }
  
  .navbar-brand {
    font-size: 16px;
    gap: 8px;
  }
  
  .navbar-brand img {
    width: 28px;
    height: 28px;
  }
  
  .navbar-actions {
    gap: 8px;
  }
  
  /* User info - stack vertically on mobile */
  .user-info {
    flex-direction: column;
    gap: 4px;
    padding: 4px 8px;
    min-width: auto;
  }
  
  .user-avatar-img,
  .user-avatar-icon {
    width: 28px !important;
    height: 28px !important;
  }
  
  .user-name {
    font-size: 11px !important;
  }
  
  /* Control bar - better mobile layout */
  .control-bar {
    padding: 12px;
    gap: 8px;
  }
  
  /* Model selector - smaller on mobile */
  #modelSelect {
    font-size: 13px;
    padding: 8px 32px 8px 12px;
  }

  .cloud-model-control {
    width: 100%;
  }

  .cloud-model-refresh-btn {
    width: 40px;
    min-width: 40px;
  }
  
  /* Chat window - less padding on mobile */
  .chat-window, .chat-output-container {
    padding: 12px;
  }
  
  /* Messages - full width on mobile */
  .message {
    margin-bottom: 16px;
  }
  
  .msg-body {
    font-size: 15px;
    padding: 10px 12px;
  }
  
  /* Avatar in messages - smaller */
  .message .user-avatar {
    width: 36px !important;
    height: 36px !important;
  }
  
  .message .user-name {
    font-size: 11px;
  }
  
  /* Input area - mobile friendly */
  .input-container {
    padding: 12px;
    gap: 8px;
  }
  
  .input-textarea {
    font-size: 16px; /* Prevents zoom on iOS */
    padding: 10px 12px;
    min-height: 44px; /* Touch target size */
  }
  
  .btn-send {
    width: 44px;
    height: 44px;
    min-width: 44px;
  }
  
  .btn-attach {
    width: 44px;
    height: 44px;
  }
  
  /* Buttons - larger touch targets */
  .btn-icon {
    width: 40px;
    height: 40px;
    min-width: 40px;
  }
  
  /* Tab buttons - stack or scroll */
  .tab-buttons {
    gap: 8px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  .tab-btn {
    padding: 8px 16px;
    font-size: 14px;
    white-space: nowrap;
  }
  
  /* Think-Tank button - smaller */
  .btn-thinktank {
    padding: 10px 16px;
    font-size: 14px;
  }
  
  /* Modals - full screen on mobile */
  .settings-panel,
  #mediaPanel,
  #toolsPanel {
    width: 100%;
    max-width: 100%;
  }
  
  /* History panel - mobile optimized */
  .history-panel {
    padding: 12px;
  }
  
  .history-item {
    padding: 10px 12px;
    font-size: 14px;
  }
  
  /* New chat button - full width on mobile */
  #newChatBtn {
    width: 100%;
    justify-content: center;
  }
}

/* Mobile Portrait (max-width: 480px) */
@media (max-width: 480px) {
  /* Even more compact navbar */
  .navbar {
    height: 52px;
  }
  
  .navbar-brand {
    font-size: 14px;
  }
  
  .navbar-brand img {
    width: 24px;
    height: 24px;
  }
  
  /* Hide text on small screens, icon only */
  .btn-icon span {
    display: none;
  }
  
  /* User info - icon only on very small screens */
  .user-name {
    display: none;
  }
  
  .user-info {
    padding: 4px;
    min-width: auto;
  }
  
  /* Control bar - vertical stack if needed */
  .control-bar {
    flex-wrap: wrap;
  }
  
  /* Search bar - full width on small screens */
  .search-container {
    width: 100%;
    order: 3;
  }
  
  /* Model selector - full width */
  #modelSelect {
    font-size: 12px;
  }
  
  /* Chat window - minimal padding */
  .chat-window, .chat-output-container {
    padding: 8px;
  }
  
  /* Messages - compact */
  .message {
    margin-bottom: 12px;
  }
  
  .msg-body {
    font-size: 14px;
    padding: 8px 10px;
  }
  
  .message .user-avatar {
    width: 32px !important;
    height: 32px !important;
  }
  
  /* Input - more compact */
  .input-container {
    padding: 8px;
    gap: 6px;
  }
  
  .input-textarea {
    font-size: 16px; /* Prevents zoom */
    padding: 8px 10px;
  }
  
  .btn-send,
  .btn-attach {
    width: 40px;
    height: 40px;
    min-width: 40px;
  }
  
  /* Sidebar narrower on very small screens */
  .nai-sidebar {
    width: 260px;
    left: -260px;
  }
  
  /* Tab buttons - smaller */
  .tab-btn {
    padding: 6px 12px;
    font-size: 13px;
  }
}

/* Landscape phone orientation */
@media (max-height: 500px) and (orientation: landscape) {
  .navbar {
    height: 48px;
  }
  
  .chat-window, .chat-output-container {
    padding: 8px;
  }
  
  .message {
    margin-bottom: 10px;
  }
  
  .msg-body {
    padding: 6px 10px;
  }
  
  .input-container {
    padding: 6px;
  }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
  /* Larger touch targets */
  button,
  .btn,
  .btn-icon,
  .tab-btn,
  .history-item {
    min-height: 44px;
    min-width: 44px;
  }
  
  /* Remove hover effects on touch devices */
  button:hover,
  .btn:hover,
  .btn-icon:hover,
  .tab-btn:hover {
    transform: none;
  }
  
  /* Better active states for touch */
  button:active,
  .btn:active,
  .btn-icon:active {
    transform: scale(0.95);
  }
  
  /* Prevent text selection on buttons */
  button,
  .btn,
  .btn-icon,
  .tab-btn {
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
  }
}

/* ========== IMAGE GENERATION MODAL ========== */
#imageGenModal .modal-content {
  overflow: hidden;
}

#imageGenModal .modal-body {
  max-height: 70vh;
  overflow-y: auto;
}

#imageGenModal textarea {
  resize: vertical;
  min-height: 80px;
}

#imageGenModal .btn-sm {
  padding: 6px 12px;
  font-size: 13px;
  border-radius: 6px;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 3px solid var(--border-color);
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

/* Image result styling */
#imageGenResult img {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  transition: transform 0.2s ease;
}

#imageGenResult img:hover {
  transform: scale(1.02);
}

/* ========== IMAGE MODE TABS ========== */
.image-mode-tabs {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--border-color);
  background: var(--bg-tertiary);
  overflow-x: auto;
  scrollbar-width: none;
  width: 100%;
  box-sizing: border-box;
}

.image-mode-tabs::-webkit-scrollbar { display: none; }

.image-mode-tab {
  flex: 1;
  min-width: 0;
  padding: 10px 8px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  transition: all 0.2s ease;
  border-bottom: 3px solid transparent;
  margin-bottom: -2px;
}

.image-mode-tab:hover {
  color: var(--text-primary);
  background: rgba(170, 0, 255, 0.1);
}

.image-mode-tab.active {
  color: var(--green);
  border-bottom-color: var(--green);
  background: rgba(0, 255, 136, 0.05);
}

.image-mode-content {
  display: none;
}

.image-mode-content.active {
  display: block;
}

/* Image preview styling */
#editImagePreview img,
#editMaskPreview img,
#variationImagePreview img {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  max-height: 300px;
  object-fit: contain;
}

/* Modal pointer events */
.modal, .modal-dialog, .modal-content { pointer-events: auto; }

.speak-icon.speaking {
  opacity: 1;
  animation: naiPulse 0.8s infinite alternate;
}

@keyframes naiPulse {
  from { transform: scale(1); }
  to   { transform: scale(1.2); }
}

/* ========== MY FILES (RAG) TOGGLE ========== */
.my-files-group {
  min-width: auto !important;
}

.my-files-toggle {
  display: flex;
  align-items: center;
}

.my-files-checkbox {
  display: none;
}

.my-files-label {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: var(--input-bg);
  border: 2px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s ease;
  user-select: none;
}

.my-files-label:hover {
  border-color: var(--accent-color);
  background: rgba(0, 255, 136, 0.05);
}

.my-files-checkbox:checked + .my-files-label {
  background: linear-gradient(135deg, rgba(0, 255, 136, 0.2), rgba(0, 200, 100, 0.1));
  border-color: var(--accent-color);
  box-shadow: 0 0 12px rgba(0, 255, 136, 0.3);
}

.my-files-icon {
  font-size: 16px;
}

.my-files-text {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}

.my-files-status {
  font-size: 11px;
  font-weight: 500;
  padding: 2px 6px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-secondary);
}

.my-files-checkbox:checked + .my-files-label .my-files-status {
  background: var(--accent-color);
  color: #000;
}

/* ========== DESKTOP: Hide Mobile-Only Elements ========== */
@media (min-width: 769px) {
  /* Hide mobile image button on desktop (desktop has it in prompt-bar) */
  #mobileImageBtn,
  #mobileVideoBtn,
  #mobileWebBtn,
  #mobileToolsBtn,
  #mobileAgentBtn,
  .btn-image,
  .btn-mobile-action {
    display: none !important;
  }
}

/* ========== MOBILE INPUT IMPROVEMENTS ========== */
@media (max-width: 768px) {
  /* Larger input area on mobile */
  .input-bar {
    padding: 12px 16px;
    gap: 10px;
    min-height: 90px;
    flex-wrap: wrap;
  }
  
  /* Textarea - bigger for 2 rows, takes full width to push buttons down */
  .input-textarea {
    font-size: 16px !important; /* Prevents zoom on iOS */
    padding: 14px 16px !important;
    min-height: 72px !important;
    border-radius: 12px !important;
    flex: 1 1 100%;
    width: 100%;
    order: 1;
  }
  
  /* ALL elements except textarea go after it */
  .input-bar > *:not(.input-textarea) {
    order: 2;
  }
  
  /* Button row on mobile - ALL buttons including image and attach label */
  .input-bar button,
  .input-bar label[for="fileInput"],
  .input-bar .btn-attach,
  .input-bar .btn-send,
  .input-bar .btn-clear,
  .input-bar .btn-export,
  .input-bar .btn-image,
  .input-bar .btn-mobile-action,
  button#imageBtn,
  button#mobileImageBtn,
  button#mobileVideoBtn,
  button#mobileWebBtn,
  button#mobileToolsBtn,
  button#mobileAgentBtn {
    width: 48px !important;
    height: 48px !important;
    min-width: 48px !important;
    border-radius: 12px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    visibility: visible !important;
    opacity: 1 !important;
  }
  
  /* NUCLEAR OPTION: Force all input-bar children visible */
  .input-bar > *:not(.input-textarea) {
    visibility: visible !important;
    opacity: 1 !important;
  }
  
  .btn-send {
    background: var(--accent-color) !important;
    margin-left: auto; /* Push send to right */
  }
  
  /* Prompt bar improvements on mobile */
  .prompt-bar {
    padding: 10px 16px;
    gap: 8px;
    flex-wrap: wrap;
  }
  
  .prompt-bar .form-select {
    flex: 1;
    min-width: 120px;
  }
  
  .prompt-bar .btn-icon,
  .prompt-bar .btn-secondary {
    padding: 10px 12px !important;
    font-size: 13px;
  }
  
  /* My Files toggle on mobile - DON'T make it full width */
  .my-files-group {
    order: -1;
    width: auto;
    margin-bottom: 8px;
  }
  
  .my-files-label {
    padding: 8px 12px;
  }
  
  /* Controls row mobile layout */
  .controls-row {
    padding: 12px 16px;
    gap: 10px;
  }
  
  .control-group {
    min-width: 100% !important;
  }
  
  .control-group.flex-grow-1 {
    min-width: 100% !important;
  }
}

/* Extra small screens */
@media (max-width: 480px) {
  .input-bar {
    padding: 10px 12px;
    min-height: 85px;
  }
  
  .input-textarea {
    min-height: 70px !important;
    padding: 12px 14px !important;
  }
  
  /* ALL buttons including image */
  .input-bar button,
  .input-bar label[for="fileInput"],
  .input-bar .btn-attach,
  .input-bar .btn-send,
  .input-bar .btn-clear,
  .input-bar .btn-export,
  .input-bar .btn-image,
  .input-bar .btn-mobile-action,
  button#imageBtn,
  button#mobileImageBtn,
  button#mobileVideoBtn,
  button#mobileWebBtn,
  button#mobileToolsBtn,
  button#mobileAgentBtn {
    width: 44px !important;
    height: 44px !important;
    min-width: 44px !important;
    display: inline-flex !important;
    visibility: visible !important;
    opacity: 1 !important;
  }
  
  /* NUCLEAR OPTION: Force ALL child elements visible */
  .input-bar > * {
    visibility: visible !important;
    opacity: 1 !important;
  }
  
  /* Except textarea should stay normal */
  .input-bar .input-textarea {
    width: 100% !important;
    height: auto !important;
    min-height: 70px !important;
  }
  
  .prompt-bar {
    padding: 8px 12px;
  }
  
  /* Hide less important buttons on very small screens */
  .prompt-bar .btn-secondary:not(#promptLogicBtn) {
    display: none;
  }
}

/* ========== LOGIC FLOW BUILDER ========== */
.flow-attached-files {
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 16px;
}

.flow-files-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.flow-file-item {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.flow-nodes-container {
  margin-bottom: 16px;
}

.flow-nodes-container > label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
}

.flow-nodes-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 12px;
  min-height: 100px;
  max-height: 400px;
  overflow-y: auto;
  padding: 8px;
  background: var(--input-bg);
  border: 1px dashed var(--border-color);
  border-radius: 8px;
}

.flow-nodes-list:empty::before {
  content: "Add nodes to build your flow...";
  color: var(--text-muted);
  font-style: italic;
  text-align: center;
  padding: 40px;
  display: block;
}

/* Flow Node Card */
.flow-node {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 12px;
  position: relative;
}

.flow-node.node-prompt {
  border-left: 3px solid #3b82f6;
}

.flow-node.node-if {
  border-left: 3px solid #f59e0b;
}

.flow-node.node-tool {
  border-left: 3px solid #8b5cf6;
}

.flow-node.node-output {
  border-left: 3px solid #10b981;
}

.flow-node-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.flow-node-type {
  font-weight: 600;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.flow-node-actions {
  display: flex;
  gap: 4px;
}

.flow-node-actions button {
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
  font-size: 14px;
  opacity: 0.6;
  transition: opacity 0.2s;
}

.flow-node-actions button:hover {
  opacity: 1;
  background: var(--input-bg);
}

.flow-node-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.flow-node-body .form-control {
  font-size: 13px;
  padding: 8px 10px;
}

.flow-node-body textarea.form-control {
  min-height: 60px;
  resize: vertical;
}

.flow-node-body label {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

/* IF Node specific */
.flow-if-condition {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.flow-if-condition select,
.flow-if-condition input {
  flex: 1;
  min-width: 100px;
}

.flow-if-freeform {
  margin-top: 8px;
}

.flow-if-freeform textarea {
  font-family: monospace;
  font-size: 12px;
}

.flow-if-toggle {
  font-size: 11px;
  color: var(--accent-color);
  cursor: pointer;
  margin-top: 4px;
}

.flow-if-toggle:hover {
  text-decoration: underline;
}

/* Tool Node specific */
.flow-tool-select {
  width: 100%;
}

.btn-add-node:disabled,
.flow-tool-select:disabled {
  opacity: .58;
  cursor: not-allowed;
}

.btn-add-node:disabled[data-type="tool"]::after {
  content: " (planned)";
}

.flow-tool-params {
  background: var(--input-bg);
  border-radius: 6px;
  padding: 8px;
  margin-top: 8px;
  font-size: 12px;
  color: var(--text-muted);
}

/* Output Node specific */
.flow-output-format {
  width: 100%;
}

/* Add Node Buttons */
.flow-add-node {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.btn-add-node {
  background: var(--input-bg);
  border: 1px dashed var(--border-color);
  color: var(--text-primary);
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.2s;
}

.btn-add-node:hover {
  border-color: var(--accent-color);
  background: var(--card-bg);
}

/* Saved Flows Section */
.flow-saved-section {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border-color);
}

.flow-saved-section > label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
}

/* Flow connector line between nodes */
.flow-node::after {
  content: "";
  position: absolute;
  bottom: -12px;
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: 12px;
  background: var(--border-color);
}

.flow-node:last-child::after {
  display: none;
}

/* ========== AGENT MODE ========== */
.agent-attached-files {
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 16px;
}

.agent-files-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.agent-files-list .no-files {
  color: var(--text-muted);
  font-style: italic;
  font-size: 13px;
}

.agent-file-item {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}

/* Context Sources Grid */
.agent-context-sources {
  margin-bottom: 16px;
}

.agent-context-sources > label {
  display: block;
  margin-bottom: 10px;
  font-weight: 500;
}

.context-source-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

@media (max-width: 600px) {
  .context-source-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 900px) {
  .mobile-sidebar-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    margin-right: 8px;
  }

  .navbar-container {
    gap: 8px;
  }

  .navbar-brand span {
    display: none;
  }

  .controls-row,
  .prompt-bar,
  .input-bar {
    flex-wrap: wrap;
  }

  .btn-secondary,
  .btn-sidebar-action,
  .btn-mobile-action {
    min-height: 40px;
  }

  .modal-dialog,
  .modal-content,
  .modal-box {
    max-width: calc(100vw - 24px) !important;
    max-height: calc(100vh - 24px) !important;
  }

  .modal-content,
  .modal-box {
    overflow: hidden;
  }

  .modal-body {
    overflow-y: auto;
  }
}

@media (max-width: 640px) {
  .control-group,
  .input-bar > *,
  .prompt-bar > * {
    min-width: 0;
  }

  .prompt-bar .btn-secondary,
  .input-bar .btn-secondary,
  .btn-mobile-action {
    min-width: 44px;
    padding: 0 10px;
    white-space: nowrap;
  }

  .agent-artifact-card {
    align-items: stretch;
    flex-direction: column;
  }
}

.context-source-card {
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
}

.context-source-header {
  padding: 10px 12px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border-color);
}

.context-source-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-weight: 500;
  font-size: 13px;
}

.context-source-toggle input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.context-source-body {
  padding: 10px 12px;
}

.context-source-body label {
  font-size: 11px;
  color: var(--text-muted);
  display: block;
  margin-bottom: 4px;
}

.context-source-body small {
  font-size: 11px;
  color: var(--text-muted);
  display: block;
  margin-top: 6px;
}

.context-slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.context-slider-row input[type="range"] {
  flex: 1;
  height: 6px;
  -webkit-appearance: none;
  background: var(--border-color);
  border-radius: 3px;
  outline: none;
}

.context-slider-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  background: var(--accent-color);
  border-radius: 50%;
  cursor: pointer;
}

.context-slider-row span {
  min-width: 30px;
  text-align: right;
  font-weight: 600;
  font-size: 13px;
}

.context-coming-soon {
  background: var(--card-bg);
  border: 1px dashed var(--border-color);
  border-radius: 4px;
  padding: 6px 8px;
  margin-top: 8px;
  font-size: 11px;
  color: var(--text-muted);
  text-align: center;
}

/* Advanced Options */
.agent-advanced-options {
  margin-top: 16px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
}

.agent-advanced-options summary {
  padding: 12px;
  cursor: pointer;
  font-weight: 500;
  background: var(--input-bg);
  border-radius: 8px;
}

.agent-advanced-options[open] summary {
  border-radius: 8px 8px 0 0;
  border-bottom: 1px solid var(--border-color);
}

.advanced-options-body {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.advanced-options-body .form-group {
  margin-bottom: 0;
}

.advanced-options-body label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

/* Context Debug Drawer */
.agent-context-debug {
  margin-top: 16px;
  background: #1a1a2e;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
}

.agent-context-debug > label {
  display: block;
  padding: 10px 12px;
  background: #0d0d1a;
  font-weight: 500;
  font-size: 13px;
  border-bottom: 1px solid var(--border-color);
}

.debug-content {
  padding: 12px;
  max-height: 200px;
  overflow-y: auto;
}

.debug-content pre {
  margin: 0;
  font-size: 11px;
  font-family: 'Monaco', 'Consolas', monospace;
  color: #a0ffa0;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Agent Result Area */
.agent-result-area {
  margin-top: 16px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
}

.agent-result-area > label {
  display: block;
  padding: 10px 12px;
  background: var(--card-bg);
  font-weight: 500;
  font-size: 13px;
  border-bottom: 1px solid var(--border-color);
}

.agent-result-content {
  padding: 12px;
  max-height: 300px;
  overflow-y: auto;
  font-size: 14px;
  line-height: 1.6;
}

.agent-result-content .agent-thinking {
  color: var(--text-muted);
  font-style: italic;
}

.agent-result-content .agent-error {
  color: #ef4444;
}

.context-source-status {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 6px 8px;
  margin-top: 8px;
  font-size: 11px;
  color: var(--text-muted);
}

.context-source-status.is-unavailable {
  border-style: dashed;
}

.agent-artifacts-list {
  padding: 0 12px 12px;
}

.agent-artifacts-heading {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin: 8px 0;
}

.agent-artifact-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  background: var(--card-bg);
  margin-top: 8px;
  font-size: 13px;
}

.agent-artifact-card > div {
  min-width: 0;
  overflow-wrap: anywhere;
}

#agentModeModal .modal-dialog,
#agentModeModal .modal-content {
  max-width: min(900px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
}

#agentModeModal .modal-content {
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

#agentModeModal .modal-body {
  overflow-y: auto;
  min-height: 0;
}

#agentModeModal .modal-header,
#agentModeModal .modal-footer {
  flex: 0 0 auto;
}

#agentModeModal .agent-result-content,
#agentModeModal #agentRunsList {
  max-height: min(340px, 42vh);
}

.mobile-sidebar-btn {
  display: none;
}

/* ========== PHASE 4: LATENCY UX ========== */

/* Notification Center in Navbar */
.notification-center {
  position: relative;
  margin-right: 12px;
}

.notification-bell {
  background: transparent;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  position: relative;
  transition: background 0.2s;
}

.notification-bell:hover {
  background: var(--input-bg);
}

.notification-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  background: #ef4444;
  color: white;
  font-size: 10px;
  font-weight: 700;
  min-width: 16px;
  height: 16px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
}

.notification-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  width: 320px;
  max-height: 400px;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
  display: none;
  z-index: 4000;
  overflow: hidden;
}

.notification-dropdown.show {
  display: block;
}

.notification-dropdown-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  background: var(--input-bg);
}

.notification-dropdown-header span {
  font-weight: 600;
}

.notification-dropdown-header .btn-link {
  background: none;
  border: none;
  color: var(--accent-color);
  font-size: 12px;
  cursor: pointer;
}

.notification-list {
  max-height: 340px;
  overflow-y: auto;
}

.notification-empty {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-muted);
  font-style: italic;
}

.notification-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-color);
  cursor: pointer;
  transition: background 0.2s;
}

.notification-item:hover {
  background: var(--input-bg);
}

.notification-item:last-child {
  border-bottom: none;
}

.notification-item.unread {
  background: rgba(59, 130, 246, 0.1);
}

.notification-item-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 4px;
}

.notification-item-title {
  font-weight: 600;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.notification-item-time {
  font-size: 11px;
  color: var(--text-muted);
}

.notification-item-body {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.4;
}

/* Toast Notifications */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 5000;
  display: flex;
  flex-direction: column-reverse;
  gap: 10px;
  max-width: 380px;
}

.toast {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
  padding: 14px 16px;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  animation: toastSlideIn 0.3s ease-out;
  cursor: pointer;
  transition: transform 0.2s, opacity 0.2s;
}

.toast:hover {
  transform: translateX(-4px);
}

.toast.toast-hiding {
  animation: toastSlideOut 0.3s ease-in forwards;
}

@keyframes toastSlideIn {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

@keyframes toastSlideOut {
  from {
    transform: translateX(0);
    opacity: 1;
  }
  to {
    transform: translateX(100%);
    opacity: 0;
  }
}

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

.toast-content {
  flex: 1;
  min-width: 0;
}

.toast-title {
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 2px;
}

.toast-message {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.4;
}

.toast-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 16px;
  cursor: pointer;
  padding: 0;
  line-height: 1;
  opacity: 0.6;
  transition: opacity 0.2s;
}

.toast-close:hover {
  opacity: 1;
}

.toast.toast-success {
  border-left: 3px solid #10b981;
}

.toast.toast-error {
  border-left: 3px solid #ef4444;
}

.toast.toast-warning {
  border-left: 3px solid #f59e0b;
}

.toast.toast-info {
  border-left: 3px solid #3b82f6;
}

/* In-Chat Progress Bubble */
.chat-progress-bubble {
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 12px 16px;
  margin: 8px 0;
  display: flex;
  align-items: center;
  gap: 12px;
  animation: progressPulse 2s ease-in-out infinite;
}

@keyframes progressPulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.7;
  }
}

.progress-spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border-color);
  border-top-color: var(--accent-color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

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

.progress-content {
  flex: 1;
}

.progress-title {
  font-weight: 500;
  font-size: 14px;
  margin-bottom: 2px;
}

.progress-status {
  font-size: 12px;
  color: var(--text-muted);
}

.progress-stages {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.progress-stage {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  color: var(--text-muted);
}

.progress-stage.active {
  background: var(--accent-color);
  border-color: var(--accent-color);
  color: #000;
}

.progress-stage.completed {
  background: #10b981;
  border-color: #10b981;
  color: #fff;
}

/* Progress Bar (optional) */
.progress-bar-container {
  width: 100%;
  height: 4px;
  background: var(--border-color);
  border-radius: 2px;
  margin-top: 8px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  background: var(--accent-color);
  border-radius: 2px;
  transition: width 0.3s ease;
}

/* Mobile adjustments */
@media (max-width: 480px) {
  .toast-container {
    left: 10px;
    right: 10px;
    bottom: 10px;
    max-width: none;
  }
  
  .notification-dropdown {
    width: 290px;
    right: -50px;
  }
}

/* ========== PHASE 5: PROJECTS BUILDER ========== */

/* Sidebar Actions */
.sidebar-actions {
  display: flex;
  gap: 6px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border-color);
}

.btn-sidebar-action {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 10px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-sidebar-action:hover {
  background: var(--card-bg);
  border-color: var(--accent-color);
}

/* Sidebar Search */
.sidebar-search {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-search-input {
  width: 100%;
  padding: 8px 12px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 13px;
}

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

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

/* Projects Tree */
.projects-tree {
  padding: 8px;
}

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

.projects-empty p {
  margin: 0 0 8px 0;
  font-size: 14px;
}

.projects-empty small {
  font-size: 12px;
  opacity: 0.7;
}

/* Project Item */
.project-item {
  margin-bottom: 4px;
}

.project-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
}

.project-header:hover {
  background: var(--card-bg);
  border-color: var(--accent-color);
}

.project-header.active {
  border-color: var(--accent-color);
  background: rgba(59, 130, 246, 0.1);
}

.project-expand {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  color: var(--text-muted);
  transition: transform 0.2s;
}

.project-item.expanded .project-expand {
  transform: rotate(90deg);
}

.project-color {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  flex-shrink: 0;
}

.project-info {
  flex: 1;
  min-width: 0;
}

.project-name {
  font-weight: 500;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.project-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.2s;
}

.project-header:hover .project-actions {
  opacity: 1;
}

.project-action-btn {
  background: transparent;
  border: none;
  padding: 4px;
  font-size: 12px;
  cursor: pointer;
  border-radius: 4px;
  opacity: 0.6;
  transition: all 0.2s;
}

.project-action-btn:hover {
  opacity: 1;
  background: var(--input-bg);
}

.project-pin {
  color: #f59e0b;
}

/* Project Chats List */
.project-chats {
  display: none;
  margin-left: 20px;
  padding: 4px 0 4px 12px;
  border-left: 2px solid var(--border-color);
}

.project-item.expanded .project-chats {
  display: block;
}

.project-chats-empty {
  padding: 12px;
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
}

/* Chat Item */
.chat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  margin-bottom: 2px;
}

.chat-item:hover {
  background: var(--input-bg);
}

.chat-item.active {
  background: var(--accent-color);
  color: #000;
}

.chat-item.active .chat-meta {
  color: rgba(0, 0, 0, 0.6);
}

.chat-icon {
  font-size: 14px;
  flex-shrink: 0;
}

.chat-info {
  flex: 1;
  min-width: 0;
}

.chat-name {
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.chat-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.2s;
}

.chat-item:hover .chat-actions {
  opacity: 1;
}

.chat-item.active .chat-actions {
  opacity: 1;
}

.chat-action-btn {
  background: transparent;
  border: none;
  padding: 4px;
  font-size: 11px;
  cursor: pointer;
  border-radius: 4px;
  opacity: 0.7;
  transition: all 0.2s;
}

.chat-action-btn:hover {
  opacity: 1;
  background: rgba(0, 0, 0, 0.1);
}

/* Standalone Chats Section */
.standalone-chats {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border-color);
}

.standalone-chats-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px 8px 8px;
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* Project Color Picker */
.project-color-picker {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.color-option {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: 2px solid transparent;
  cursor: pointer;
  transition: all 0.2s;
}

.color-option:hover {
  transform: scale(1.1);
}

.color-option.selected {
  border-color: var(--text-primary);
  box-shadow: 0 0 0 2px var(--card-bg);
}

/* Quick Chat button updated */
.btn-new-chat {
  font-size: 13px;
}

/* Mobile sidebar adjustments */
@media (max-width: 768px) {
  .sidebar-actions {
    flex-direction: column;
  }
  
  .btn-sidebar-action {
    justify-content: flex-start;
  }
  
  .project-actions,
  .chat-actions {
    opacity: 1;
  }
}

/* ========== PHASE 6: STRUCTURED MEMORY / FACTS ========== */

/* Sidebar Tabs */
.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border-color);
  padding: 0 8px;
}

.sidebar-tab {
  flex: 1;
  padding: 10px 12px;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

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

.sidebar-tab.active {
  color: var(--accent-color);
  border-bottom-color: var(--accent-color);
}

.sidebar-tab-content {
  display: none;
  padding: 8px;
}

.sidebar-tab-content.active {
  display: block;
}

/* Facts Panel */
.facts-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.facts-search {
  padding: 0;
}

.facts-actions {
  display: flex;
}

.facts-actions .btn-sidebar-action {
  flex: 1;
}

/* Facts Section */
.facts-section {
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
}

.facts-section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border-color);
  font-size: 12px;
  font-weight: 500;
}

.facts-count {
  background: var(--accent-color);
  color: #000;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 11px;
  font-weight: 600;
}

.facts-list {
  max-height: 300px;
  overflow-y: auto;
}

.facts-empty {
  padding: 30px 16px;
  text-align: center;
  color: var(--text-muted);
}

.facts-empty p {
  margin: 0 0 6px 0;
  font-size: 13px;
}

.facts-empty small {
  font-size: 11px;
  opacity: 0.7;
}

/* Fact Item */
.fact-item {
  padding: 12px;
  border-bottom: 1px solid var(--border-color);
  transition: background 0.2s;
}

.fact-item:last-child {
  border-bottom: none;
}

.fact-item:hover {
  background: var(--card-bg);
}

.fact-item.pending {
  background: rgba(245, 158, 11, 0.1);
  border-left: 3px solid #f59e0b;
}

.fact-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 6px;
}

.fact-key {
  font-weight: 600;
  font-size: 13px;
  color: var(--accent-color);
  display: flex;
  align-items: center;
  gap: 6px;
}

.fact-category {
  font-size: 12px;
}

.fact-actions {
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.2s;
}

.fact-item:hover .fact-actions {
  opacity: 1;
}

.fact-action-btn {
  background: transparent;
  border: none;
  padding: 4px 6px;
  font-size: 12px;
  cursor: pointer;
  border-radius: 4px;
  opacity: 0.7;
  transition: all 0.2s;
}

.fact-action-btn:hover {
  opacity: 1;
  background: var(--input-bg);
}

.fact-action-btn.approve {
  color: #10b981;
}

.fact-action-btn.reject {
  color: #ef4444;
}

.fact-value {
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-primary);
  margin-bottom: 6px;
}

.fact-meta {
  display: flex;
  gap: 12px;
  font-size: 11px;
  color: var(--text-muted);
}

.fact-meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.fact-confidence {
  display: flex;
  align-items: center;
  gap: 4px;
}

.fact-confidence-bar {
  width: 40px;
  height: 4px;
  background: var(--border-color);
  border-radius: 2px;
  overflow: hidden;
}

.fact-confidence-fill {
  height: 100%;
  background: var(--accent-color);
  border-radius: 2px;
}

/* Pending fact actions */
.pending-fact-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.pending-fact-actions button {
  flex: 1;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-approve {
  background: #10b981;
  border: none;
  color: white;
}

.btn-approve:hover {
  background: #059669;
}

.btn-reject {
  background: transparent;
  border: 1px solid #ef4444;
  color: #ef4444;
}

.btn-reject:hover {
  background: #ef4444;
  color: white;
}

/* Fact extraction notification in chat */
.fact-extraction-notice {
  background: rgba(59, 130, 246, 0.1);
  border: 1px solid var(--accent-color);
  border-radius: 8px;
  padding: 12px;
  margin: 8px 0;
  font-size: 13px;
}

.fact-extraction-notice-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-weight: 500;
}

.fact-extraction-notice-body {
  padding-left: 24px;
}

.extracted-fact-preview {
  background: var(--card-bg);
  border-radius: 6px;
  padding: 8px 12px;
  margin: 8px 0;
  font-size: 12px;
}

.extracted-fact-preview .key {
  color: var(--accent-color);
  font-weight: 600;
}

.fact-extraction-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  padding-left: 24px;
}

/* ========== PHASE 7: RAG MEMORY EDITOR ========== */

/* Sidebar header actions */
.sidebar-header-actions {
  display: flex;
  gap: 4px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.sidebar-vdb-toggle .my-files-label {
  min-height: 32px;
  padding: 5px 8px;
  border-width: 1px;
}

.sidebar-vdb-toggle .my-files-status {
  padding: 1px 5px;
}

/* RAG Editor Toolbar */
.rag-editor-toolbar {
  display: flex;
  gap: 12px;
  padding: 12px 16px;
  background: var(--input-bg);
  border-bottom: 1px solid var(--border-color);
  flex-wrap: wrap;
}

.rag-filter-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.rag-filter-group label {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

.rag-filter-group .form-control {
  min-width: 150px;
}

.rag-search-group {
  flex: 1;
  display: flex;
  gap: 8px;
  min-width: 200px;
}

.rag-search-group input {
  flex: 1;
}

.rag-action-group {
  display: flex;
  gap: 4px;
}

.rag-action-group button {
  padding: 8px 12px;
}

/* RAG Stats Panel */
.rag-stats-panel {
  padding: 16px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border-color);
}

.rag-stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

@media (max-width: 600px) {
  .rag-stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

.rag-stat-card {
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 16px;
  text-align: center;
}

.rag-stat-value {
  font-size: 24px;
  font-weight: 700;
  color: var(--accent-color);
  margin-bottom: 4px;
}

.rag-stat-label {
  font-size: 12px;
  color: var(--text-muted);
}

/* Memory List */
.rag-memory-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.rag-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 40px;
  color: var(--text-muted);
}

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

.rag-empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
  opacity: 0.5;
}

.rag-empty p {
  margin: 0 0 8px 0;
}

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

.rag-offline code {
  background: var(--bg-secondary);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 12px;
}

/* Memory Item */
.rag-memory-item {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  margin-bottom: 8px;
  overflow: hidden;
  transition: all 0.2s;
}

.rag-memory-item:hover {
  border-color: var(--accent-color);
}

.rag-memory-item.selected {
  border-color: var(--accent-color);
  background: rgba(59, 130, 246, 0.1);
}

.rag-memory-item.pinned {
  border-left: 3px solid #f59e0b;
}

.rag-memory-item.excluded {
  opacity: 0.5;
  border-left: 3px solid #ef4444;
}

.rag-memory-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  cursor: pointer;
}

.rag-memory-checkbox {
  width: 18px;
  height: 18px;
  cursor: pointer;
}

.rag-memory-icon {
  font-size: 18px;
  flex-shrink: 0;
}

.rag-memory-info {
  flex: 1;
  min-width: 0;
}

.rag-memory-title {
  font-weight: 500;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 2px;
}

.rag-memory-meta {
  display: flex;
  gap: 12px;
  font-size: 11px;
  color: var(--text-muted);
}

.rag-memory-score {
  display: flex;
  align-items: center;
  gap: 4px;
}

.rag-score-bar {
  width: 50px;
  height: 4px;
  background: var(--border-color);
  border-radius: 2px;
  overflow: hidden;
}

.rag-score-fill {
  height: 100%;
  background: var(--accent-color);
  border-radius: 2px;
}

.rag-memory-actions {
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.2s;
}

.rag-memory-item:hover .rag-memory-actions {
  opacity: 1;
}

.rag-memory-action-btn {
  background: transparent;
  border: none;
  padding: 6px;
  font-size: 14px;
  cursor: pointer;
  border-radius: 4px;
  opacity: 0.7;
  transition: all 0.2s;
}

.rag-memory-action-btn:hover {
  opacity: 1;
  background: var(--input-bg);
}

.rag-memory-preview {
  padding: 0 12px 12px 54px;
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.5;
  display: none;
}

.rag-memory-item.expanded .rag-memory-preview {
  display: block;
}

.rag-memory-preview-text {
  background: var(--input-bg);
  border-radius: 6px;
  padding: 10px;
  font-family: 'Monaco', 'Consolas', monospace;
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 200px;
  overflow-y: auto;
}

/* Detail Panel (Slide-in) */
.rag-detail-panel {
  position: absolute;
  top: 0;
  right: -400px;
  width: 400px;
  height: 100%;
  background: var(--card-bg);
  border-left: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  transition: right 0.3s ease;
  z-index: 10;
}

.rag-detail-panel.open {
  right: 0;
}

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

.rag-detail-header h4 {
  margin: 0;
  font-size: 16px;
}

.rag-detail-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.rag-detail-section {
  margin-bottom: 20px;
}

.rag-detail-section label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}

.rag-detail-value {
  font-size: 14px;
  line-height: 1.5;
}

.rag-detail-text {
  background: var(--input-bg);
  border-radius: 6px;
  padding: 12px;
  font-size: 13px;
  line-height: 1.6;
  max-height: 300px;
  overflow-y: auto;
}

.rag-detail-actions {
  display: flex;
  gap: 8px;
  padding: 16px;
  border-top: 1px solid var(--border-color);
}

.rag-detail-actions button {
  flex: 1;
}

.btn-danger {
  background: #ef4444;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
}

.btn-danger:hover {
  background: #dc2626;
}

/* Footer */
.rag-footer-info {
  flex: 1;
  font-size: 13px;
  color: var(--text-muted);
}

/* Tags */
.rag-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.rag-tag {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 12px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
}

.rag-tag.pinned {
  background: rgba(245, 158, 11, 0.2);
  border-color: #f59e0b;
  color: #f59e0b;
}

.rag-tag.excluded {
  background: rgba(239, 68, 68, 0.2);
  border-color: #ef4444;
  color: #ef4444;
}

/* Mobile adjustments */
@media (max-width: 768px) {
  .rag-editor-toolbar {
    flex-direction: column;
  }
  
  .rag-filter-group,
  .rag-search-group,
  .rag-action-group {
    width: 100%;
  }
  
  .rag-detail-panel {
    width: 100%;
    right: -100%;
  }
}

/* ========== PHASE 8: RESULT ENVELOPE / RETURN TYPES ========== */

/* Result Panel */
.result-panel {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  margin: 0 16px 16px 16px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  animation: resultPanelSlideIn 0.3s ease-out;
}

@keyframes resultPanelSlideIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.result-panel.minimized {
  max-height: 50px;
  overflow: hidden;
}

.result-panel.minimized .result-panel-content,
.result-panel.minimized .result-panel-footer {
  display: none;
}

.result-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--input-bg);
  border-bottom: 1px solid var(--border-color);
}

.result-panel-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 14px;
}

.result-panel-icon {
  font-size: 18px;
}

.result-panel-actions {
  display: flex;
  gap: 4px;
}

.result-panel-content {
  padding: 16px;
  max-height: 500px;
  overflow-y: auto;
}

.result-panel-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background: var(--input-bg);
  border-top: 1px solid var(--border-color);
}

.result-panel-meta {
  font-size: 12px;
  color: var(--text-muted);
}

.result-panel-footer-actions {
  display: flex;
  gap: 8px;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 12px;
}

/* Result Types */

/* Chat Result */
.result-chat {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.result-chat-message {
  padding: 12px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.6;
}

.result-chat-message.user {
  background: var(--accent-color);
  color: #000;
  align-self: flex-end;
  max-width: 80%;
}

.result-chat-message.assistant {
  background: var(--input-bg);
  align-self: flex-start;
  max-width: 90%;
}

/* Links Result */
.result-links {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.result-link-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  text-decoration: none;
  color: var(--text-primary);
  transition: all 0.2s;
}

.result-link-card:hover {
  border-color: var(--accent-color);
  background: var(--card-bg);
  transform: translateX(4px);
}

.result-link-icon {
  font-size: 24px;
  flex-shrink: 0;
}

.result-link-info {
  flex: 1;
  min-width: 0;
}

.result-link-label {
  font-weight: 500;
  font-size: 14px;
  margin-bottom: 2px;
}

.result-link-url {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.result-link-arrow {
  color: var(--text-muted);
  font-size: 18px;
}

/* Artifacts Result */
.result-artifacts {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
}

.result-artifact-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 20px 16px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  text-decoration: none;
  color: var(--text-primary);
  transition: all 0.2s;
  text-align: center;
}

.result-artifact-card:hover {
  border-color: var(--accent-color);
  background: var(--card-bg);
}

.result-artifact-icon {
  font-size: 36px;
}

.result-artifact-name {
  font-weight: 500;
  font-size: 13px;
  word-break: break-word;
}

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

.result-artifact-download {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  padding: 6px 12px;
  background: var(--accent-color);
  color: #000;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
}

/* Media Result */
.result-media {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.result-media-item {
  border-radius: 8px;
  overflow: hidden;
  background: var(--input-bg);
}

.result-media-item img {
  width: 100%;
  max-height: 400px;
  object-fit: contain;
}

.result-media-item video,
.result-media-item audio {
  width: 100%;
}

.result-media-item iframe {
  width: 100%;
  height: 400px;
  border: none;
}

.result-media-caption {
  padding: 10px 12px;
  font-size: 13px;
  color: var(--text-secondary);
  border-top: 1px solid var(--border-color);
}

/* Widget/Interactive Result */
.result-widget {
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
}

.result-widget-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border-color);
}

.result-widget-title {
  font-weight: 500;
  font-size: 13px;
}

.result-widget-actions button {
  font-size: 12px;
}

.result-widget-content {
  padding: 0;
}

.result-widget-content iframe {
  width: 100%;
  height: 400px;
  border: none;
}

/* Table Result */
.result-table {
  overflow-x: auto;
}

.result-table table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.result-table th,
.result-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border-color);
}

.result-table th {
  background: var(--input-bg);
  font-weight: 600;
  white-space: nowrap;
}

.result-table tr:hover td {
  background: var(--input-bg);
}

/* Code Result */
.result-code {
  background: #1a1a2e;
  border-radius: 8px;
  overflow: hidden;
}

.result-code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: #0d0d1a;
  border-bottom: 1px solid var(--border-color);
}

.result-code-language {
  font-size: 12px;
  color: var(--text-muted);
}

.result-code-copy {
  font-size: 12px;
  padding: 4px 8px;
}

.result-code-content {
  padding: 12px;
  overflow-x: auto;
}

.result-code-content pre {
  margin: 0;
  font-family: 'Monaco', 'Consolas', monospace;
  font-size: 13px;
  line-height: 1.5;
  color: #e0e0e0;
}

/* Multi-part Result */
.result-multi {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.result-section {
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
}

.result-section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--card-bg);
  border-bottom: 1px solid var(--border-color);
  font-weight: 500;
  font-size: 13px;
}

.result-section-content {
  padding: 12px;
}

/* Summary Banner */
.result-summary {
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.1), rgba(139, 92, 246, 0.1));
  border: 1px solid var(--accent-color);
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;
}

.result-summary-title {
  font-weight: 600;
  font-size: 16px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.result-summary-text {
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-secondary);
}

/* Error Result */
.result-error {
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid #ef4444;
  border-radius: 8px;
  padding: 16px;
}

.result-error-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: #ef4444;
  margin-bottom: 8px;
}

.result-error-message {
  font-size: 14px;
  color: var(--text-secondary);
}

.result-error-details {
  margin-top: 12px;
  padding: 10px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 6px;
  font-family: monospace;
  font-size: 12px;
  color: var(--text-muted);
}

/* Loading State */
.result-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  gap: 16px;
}

.result-loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border-color);
  border-top-color: var(--accent-color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.result-loading-text {
  font-size: 14px;
  color: var(--text-muted);
}

/* Mobile adjustments */
@media (max-width: 768px) {
  .result-panel {
    margin: 0 8px 8px 8px;
  }
  
  .result-artifacts {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .result-panel-content {
    max-height: 350px;
  }
}

/* ---- Settings UI additions ---- */
.input-with-action {
  display: flex;
  gap: 8px;
  align-items: center;
}
.input-with-action .form-control {
  flex: 1;
}
.btn-test {
  background: var(--bg-tertiary, #1e1e1e);
  border: 1px solid var(--border-color, #2a2a2a);
  color: var(--text-secondary, #aaa);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: .8rem;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s;
}
.btn-test:hover { background: var(--bg-hover, #252525); }
.btn-danger {
  background: #7f1d1d;
  border: 1px solid #991b1b;
  color: #fca5a5;
  border-radius: 6px;
  padding: 8px 14px;
  font-size: .875rem;
  cursor: pointer;
}
.btn-danger:hover { background: #991b1b; }
.label-hint {
  font-size: .75rem;
  color: var(--text-tertiary, #555);
  font-weight: 400;
}
.form-range {
  width: 100%;
  accent-color: #3b82f6;
}
.settings-actions {
  display: flex;
  align-items: center;
  padding-top: 16px;
}

/* ── API key row: input + show/hide + clear + test ── */
.input-key-row {
  display: flex;
  gap: 6px;
  align-items: center;
}
.input-key-row .form-control {
  flex: 1;
  min-width: 0;
}
.btn-icon-sm {
  background: var(--bg-tertiary, #1e1e1e);
  border: 1px solid var(--border-color, #2a2a2a);
  color: var(--text-secondary, #aaa);
  border-radius: 6px;
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: .85rem;
  flex-shrink: 0;
  transition: background .15s;
}
.btn-icon-sm:hover { background: var(--bg-hover, #252525); }
.btn-clear-key { font-size: .7rem; }
.btn-edit-key { font-size: .75rem; color: var(--text-secondary, #888); }

.settings-grid.two-col {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
.settings-check-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 8px 12px;
  margin-top: 8px;
}
.settings-check-grid label {
  color: var(--text-secondary, #aaa);
  font-size: .9rem;
}
.settings-status-line,
.form-hint {
  color: var(--text-secondary, #aaa);
  font-size: .84rem;
  margin: 8px 0 0;
}
.settings-list {
  margin-top: 10px;
  display: grid;
  gap: 6px;
}
.settings-list-item {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--border-color, #2a2a2a);
  background: var(--card-bg, #111);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--text-primary, #fff);
}
.settings-list-item small,
.settings-empty {
  color: var(--text-secondary, #aaa);
}
.btn-action-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-right: 4px;
}

body.layout-writer .chat-window,
body.layout-writer .chat-output-container {
  font-size: 1.08rem;
  line-height: 1.72;
  max-width: 1220px;
  width: 100%;
  align-self: center;
}
body.layout-writer .controls-container {
  max-height: 210px;
  overflow: auto;
}
body.layout-writer .prompt-bar {
  justify-content: center;
}
body.layout-writer .prompt-wrapper,
body.layout-writer .input-area,
body.layout-writer #userInput {
  min-height: 120px;
}
body.layout-writer #mcpToolsBtn,
body.layout-writer #datasetBtn,
body.layout-writer #skillsBtn,
body.layout-writer #mediaTabBtn,
body.layout-writer #toolsTabBtn {
  opacity: .62;
}
body.layout-writer .message,
body.layout-writer .msg {
  max-width: min(980px, 96%);
}

body.layout-coder .nai-main {
  grid-template-columns: minmax(0, 1fr);
}
body.layout-coder .result-panel,
body.layout-coder .agent-result-body {
  font-family: Consolas, "Courier New", monospace;
}
body.layout-coder #mcpToolsBtn,
body.layout-coder #agentModeBtn,
body.layout-coder #toolsTabBtn {
  box-shadow: 0 0 0 2px rgba(59,130,246,.35);
}
body.layout-coder .chat-window,
body.layout-coder .chat-output-container {
  font-family: Consolas, "Courier New", monospace;
}
body.layout-coder pre,
body.layout-coder code {
  font-size: .95rem;
  line-height: 1.55;
}
body.layout-coder .agent-run-bubble,
body.layout-coder .agent-artifacts,
body.layout-coder .agent-result-body {
  max-width: min(980px, calc(100vw - 32px));
}
body.layout-team .team-main-control-bar {
  border-color: #38bdf8;
  display: flex !important;
  box-shadow: 0 0 0 2px rgba(56,189,248,.18);
}
body.layout-team #teamPanelBtn,
body.layout-team #teamMainOpenPanelBtn {
  box-shadow: 0 0 0 2px rgba(56,189,248,.35);
}
body.layout-team #teamPanel {
  display: block;
}
body.layout-team .team-panel-grid,
body.layout-team .team-relay-section,
body.layout-team .team-files-section {
  gap: 16px;
}
body.layout-team .team-relay-section,
body.layout-team .team-files-section {
  border-width: 2px;
}
body.layout-analysis .web-search-source-block,
body.layout-analysis .dataset-context-card,
body.layout-analysis .message-source-block {
  border-color: #22c55e;
}
body.layout-analysis #webToolsBtn,
body.layout-analysis #datasetBtn,
body.layout-analysis #skillsBtn {
  box-shadow: 0 0 0 2px rgba(34,197,94,.35);
}
body.layout-analysis .message.system,
body.layout-analysis .message-source-block {
  max-width: 1200px;
}
body.layout-analysis .msg-body a,
body.layout-analysis .message a {
  text-decoration: underline;
  text-underline-offset: 3px;
}
body.layout-analysis .controls-row:first-child {
  border-left: 3px solid #22c55e;
  padding-left: 12px;
}
body.layout-media #mediaTabBtn,
body.layout-media #imageGenBtn,
body.layout-media #videoGenBtn {
  box-shadow: 0 0 0 2px rgba(236,72,153,.35);
}
body.layout-media #chatPanel .chat-window {
  min-height: 45vh;
}
body.layout-media #mediaPanel {
  display: block;
}
body.layout-media .media-grid {
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
}
body.layout-media .media-row-thumb,
body.layout-media .media-row-thumb-wrap {
  min-width: 72px;
  min-height: 72px;
}
body.layout-agent #agentModeBtn,
body.layout-agent .agent-run-bubble {
  box-shadow: 0 0 0 2px rgba(168,85,247,.35);
}
body.layout-agent .agent-run-bubble {
  width: min(460px, calc(100vw - 32px));
}
body.layout-agent #agentModeBtn {
  transform: translateY(-1px);
}
body.layout-agent .agent-result-body,
body.layout-agent .agent-context-grid,
body.layout-agent .agent-runtime-grid {
  max-width: 1100px;
}
body.layout-agent .agent-run-bubble {
  right: 20px;
  bottom: 96px;
}
body.layout-security #mcpToolsBtn,
body.layout-security #agentModeBtn {
  border-color: #f59e0b;
  box-shadow: 0 0 0 2px rgba(245,158,11,.28);
}
.rag-module-hidden {
  display: none !important;
}
body.layout-custom-active .rag-layout-root {
  outline: 2px solid rgba(59,130,246,.18);
  outline-offset: -6px;
}
.rag-ui-module-grid {
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

@media (min-width: 1180px) {
  body.layout-writer .rag-layout-root {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
  }
  body.layout-writer .nai-main {
    padding-inline: clamp(24px, 5vw, 86px);
  }
  body.layout-writer .rag-region-tools {
    max-height: 150px;
  }
  body.layout-writer .rag-region-chat {
    min-height: 0;
  }
  body.layout-coder .rag-layout-root,
  body.layout-analysis .rag-layout-root,
  body.layout-team .rag-layout-root,
  body.layout-media .rag-layout-root,
  body.layout-agent .rag-layout-root,
  body.layout-security .rag-layout-root {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-columns: minmax(0, 1fr) minmax(320px, 420px);
    grid-template-areas:
      "controls controls"
      "chat side";
    gap: 12px;
  }
  body.layout-coder .controls-container,
  body.layout-analysis .controls-container,
  body.layout-team .controls-container,
  body.layout-media .controls-container,
  body.layout-agent .controls-container,
  body.layout-security .controls-container {
    grid-area: controls;
  }
  body.layout-coder #chatPanel,
  body.layout-analysis #chatPanel,
  body.layout-team #chatPanel,
  body.layout-media #chatPanel,
  body.layout-agent #chatPanel,
  body.layout-security #chatPanel {
    grid-area: chat;
    display: flex !important;
    min-height: 0;
  }
  body.layout-coder #toolsPanel,
  body.layout-agent #toolsPanel,
  body.layout-security #toolsPanel,
  body.layout-analysis #toolsPanel,
  body.layout-team #teamPanel,
  body.layout-media #mediaPanel {
    grid-area: side;
    display: flex !important;
    min-height: 0;
    overflow: auto;
    border: 2px solid rgba(59,130,246,.28);
    border-radius: 10px;
  }
  body.layout-analysis #toolsPanel {
    border-color: rgba(34,197,94,.36);
  }
  body.layout-team #teamPanel {
    border-color: rgba(56,189,248,.42);
  }
  body.layout-media #mediaPanel {
    border-color: rgba(236,72,153,.36);
  }
  body.layout-agent #toolsPanel {
    border-color: rgba(168,85,247,.38);
  }
  body.layout-analysis .chat-window,
  body.layout-analysis .chat-output-container {
    max-width: 1280px;
  }
  body.layout-team .nai-container {
    grid-template-columns: minmax(300px, 380px) minmax(0, 1fr);
  }
}
.prompt-status-pill {
  align-self: center;
  border: 1px solid var(--border-color, #2a2a2a);
  border-radius: 999px;
  color: var(--text-secondary, #aaa);
  background: var(--bg-tertiary, #151515);
  padding: 6px 10px;
  font-size: .78rem;
  white-space: nowrap;
}
.prompt-status-pill.active,
#webToolsBtn.web-context-on {
  border-color: #22c55e;
  color: #86efac;
  background: rgba(34,197,94,.1);
}
#webToolsBtn.web-context-on,
#webToolsBtn[aria-pressed="true"] {
  border-color: #22c55e;
  color: #86efac;
  background: rgba(34,197,94,.14);
  box-shadow: inset 0 0 0 1px rgba(34,197,94,.28), 0 0 0 2px rgba(34,197,94,.12);
}
.settings-list-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

/* ── API key status states ── */
.input-key-row .form-control.key-configured {
  border-color: #22c55e;
  background: rgba(34,197,94,.06);
  color: #86efac;
}
.input-key-row .form-control.key-editing { border-color: #3b82f6; }

/* ── User list in settings ── */
.user-list-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 8px;
  background: var(--bg-tertiary, #1e1e1e);
  margin-bottom: 6px; font-size: .875rem;
}
.user-list-item .user-role-badge {
  font-size: .7rem; padding: 2px 7px; border-radius: 99px;
  background: #1d3a6e; color: #93c5fd; margin-left: auto;
}
.user-list-item .user-role-badge.admin { background: #3b2712; color: #fbbf24; }
.user-list-item .btn-delete-user {
  background: transparent; border: 1px solid #2a2a2a; border-radius: 6px;
  color: #ef4444; cursor: pointer; padding: 3px 8px; font-size: .75rem;
  margin-left: 4px;
}
.user-list-item .btn-delete-user:hover { background: rgba(239,68,68,.1); }

/* ── settings subsection (collapsible) ── */
.settings-subsection {
  border: 1px solid var(--border-color, #2a2a2a);
  border-radius: 8px;
  margin-top: 12px;
}
.settings-subsection > summary {
  padding: 10px 14px;
  cursor: pointer;
  font-size: .875rem;
  font-weight: 600;
  list-style: none;
  user-select: none;
}
.settings-subsection > summary::-webkit-details-marker { display: none; }
.settings-subsection > summary::before {
  content: '▶ ';
  font-size: .7rem;
  vertical-align: middle;
}
.settings-subsection[open] > summary::before { content: '▼ '; }
.settings-subsection-body {
  padding: 12px 14px 14px;
  border-top: 1px solid var(--border-color, #2a2a2a);
}

/* Teams */
.team-layout {
  display: grid;
  grid-template-columns: minmax(220px, 300px) minmax(0, 1fr);
  gap: 16px;
  padding: 16px;
  min-height: 0;
}
.team-sidebar,
.team-workspace,
.team-guest-shell {
  border: 1px solid var(--border-color, #2a2a2a);
  border-radius: 8px;
  background: var(--card-bg, #111);
  padding: 14px;
}
.team-sidebar,
.team-workspace {
  min-height: 0;
}
.team-form-row,
.team-message-compose {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
}
.team-form-row .form-control,
.team-message-compose .form-control {
  min-width: 0;
}
.team-status {
  color: var(--text-secondary, #aaa);
  font-size: .875rem;
  margin: 8px 0 12px;
  min-height: 20px;
}
.team-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
}
.team-list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border-color, #2a2a2a);
  border-radius: 8px;
  background: var(--bg-tertiary, #1e1e1e);
  padding: 8px 10px;
  min-width: 0;
}
.team-list-item > span:first-child {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.team-role-badge,
.team-role-select {
  font-size: .78rem;
}
.team-role-badge {
  color: #93c5fd;
  background: rgba(59,130,246,.12);
  border-radius: 99px;
  padding: 2px 8px;
}
.team-messages {
  border: 1px solid var(--border-color, #2a2a2a);
  border-radius: 8px;
  background: var(--bg-secondary, #0f0f0f);
  min-height: 240px;
  max-height: 45vh;
  overflow: auto;
  padding: 10px;
  margin-bottom: 10px;
}
.team-message {
  border-bottom: 1px solid var(--border-color, #2a2a2a);
  padding: 8px 0;
}
.team-message:last-child {
  border-bottom: 0;
}
.team-message div {
  display: flex;
  gap: 8px;
  align-items: baseline;
}
.team-message span {
  color: var(--text-tertiary, #777);
  font-size: .75rem;
}
.team-message p {
  margin: 4px 0 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.team-guest-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg-primary, #0b0b0b);
}
.team-guest-shell {
  width: min(900px, 100%);
}

.team-main-control-bar {
  margin: 8px 12px 0;
  padding: 10px 12px;
  border: 1px solid var(--accent-color, #3b82f6);
  border-radius: 8px;
  background: color-mix(in srgb, var(--accent-color, #3b82f6) 12%, var(--bg-secondary, #151515));
  box-shadow: 0 0 0 1px rgba(59,130,246,.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.team-main-control-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.team-main-control-info span {
  color: var(--text-secondary, #aaa);
  font-size: .86rem;
}
.team-main-control-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.web-search-results {
  display: none;
  margin-top: 6px;
  max-height: 180px;
  overflow: auto;
  border: 1px solid var(--border-color, #2a2a2a);
  border-radius: 8px;
  background: var(--bg-secondary, #151515);
  padding: 8px;
  font-size: .84rem;
}
.web-search-results.active {
  display: block;
}
.web-search-result {
  padding: 6px 0;
  border-bottom: 1px solid var(--border-color, #2a2a2a);
}
.web-search-result:last-child {
  border-bottom: 0;
}
.web-search-result a {
  color: var(--accent-color, #60a5fa);
  font-weight: 600;
  text-decoration: none;
}
.web-search-result p {
  color: var(--text-secondary, #aaa);
  margin: 3px 0 0;
}
.plugin-management-section {
  border: 1px solid var(--accent-color, #3b82f6);
  border-radius: 8px;
  padding: 14px;
  background: color-mix(in srgb, var(--accent-color, #3b82f6) 8%, transparent);
}
.plugin-management-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}
.plugin-connector-status {
  color: var(--text-secondary, #aaa);
  font-size: .86rem;
  margin: 8px 0 12px;
}
.plugin-capability-card {
  border: 1px solid var(--border-color, #2a2a2a);
  border-radius: 8px;
  padding: 10px;
  background: var(--card-bg, #111);
}
.plugin-capability-card code {
  background: var(--bg-tertiary, #222);
  padding: 2px 5px;
  border-radius: 4px;
  margin-right: 4px;
}

.message-attachments {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
}
.message-attachment-card {
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--border-color, #2a2a2a);
  border-radius: 8px;
  padding: 8px;
  background: var(--card-bg, #111);
}
.message-attachment-card .attachment-image-preview {
  width: 52px;
  height: 52px;
  object-fit: cover;
  border-radius: 6px;
  margin: 0;
}
.attachment-file-icon {
  width: 52px;
  height: 52px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-tertiary, #222);
  font-size: 1.4rem;
}
.message-attachment-meta {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.message-attachment-meta strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.message-attachment-meta span {
  color: var(--text-secondary, #aaa);
  font-size: .8rem;
}
.message-attachment-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
}
.team-relay-qr {
  margin-top: 10px;
}
.team-files-section,
.team-relay-section {
  grid-column: 1 / -1;
  clear: both;
  min-width: 0;
}
.team-files-section .form-actions,
.team-relay-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}
.team-relay-section {
  margin-top: 12px;
  border-top: 1px solid var(--border-color, #2a2a2a);
  padding-top: 14px;
}
.team-relay-section .form-group,
.team-relay-section .settings-row,
.team-relay-section #teamRelayJoinUrl {
  max-width: 760px;
}
.team-relay-actions button {
  white-space: nowrap;
}
.relay-qr-placeholder {
  border: 1px dashed var(--border-color, #2a2a2a);
  border-radius: 8px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overflow-wrap: anywhere;
  background: var(--card-bg, #111);
}
.relay-qr-placeholder small {
  color: var(--text-secondary, #aaa);
}
.agent-run-bubble {
  position: fixed;
  right: 18px;
  bottom: 88px;
  width: min(360px, calc(100vw - 32px));
  z-index: 1200;
  background: var(--card-bg, #111);
  color: var(--text-primary, #fff);
  border: 1px solid var(--border-color, #2a2a2a);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0,0,0,.28);
  padding: 12px;
}
.agent-run-bubble-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
}
.agent-run-bubble-head span {
  font-size: .78rem;
  color: var(--text-secondary, #aaa);
  text-transform: uppercase;
}
.agent-run-bubble-task {
  color: var(--text-secondary, #aaa);
  font-size: .9rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 10px;
}
.agent-run-bubble-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.agent-run-list-item {
  border: 1px solid var(--border-color, #2a2a2a);
  border-radius: 8px;
  padding: 8px;
  margin-bottom: 8px;
  background: var(--card-bg, #111);
}
.agent-run-list-item span {
  float: right;
  color: var(--text-secondary, #aaa);
  font-size: .8rem;
}
.agent-run-list-item p {
  clear: both;
  color: var(--text-secondary, #aaa);
  margin: 4px 0 8px;
}

#datasetModal .modal-dialog {
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  display: flex;
}

#datasetModal .modal-content {
  max-height: calc(100vh - 32px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#datasetModal .modal-body {
  min-height: 0;
  max-height: calc(100vh - 120px);
  overflow-y: auto;
}

@media (max-width: 768px) {
  .team-layout {
    grid-template-columns: 1fr;
    padding: 10px;
  }
  .team-form-row,
  .team-message-compose {
    flex-wrap: wrap;
  }
  .team-messages {
    max-height: 38vh;
  }
  .team-main-control-bar {
    align-items: stretch;
    flex-direction: column;
  }
  .team-main-control-actions {
    justify-content: flex-start;
  }
  .settings-grid.two-col {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  #datasetBtn,
  #skillsBtn,
  #webToolsBtn,
  #mcpToolsBtn,
  #agentModeBtn {
    width: 44px;
    max-width: 44px;
    padding: 0;
    justify-content: center;
  }
  .btn-action-label {
    display: none;
  }
  .btn-action-icon {
    margin-right: 0;
  }
}

/* ========== MOBILE: Sprint 5 Polish ========== */

/* 1. iOS Safari: prompt/input bar stays above keyboard using safe-area insets.
      viewport-fit=cover is set in the meta tag so env() values are non-zero on notched devices. */
.input-bar,
.prompt-bar {
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
}

@media (max-width: 768px) {
  .input-bar  { padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
  .prompt-bar { padding-bottom: calc(8px  + env(safe-area-inset-bottom, 0px)); }
}

/* 2. Sidebar overlays chat (not pushes) on small screens — already fixed via fixed+z-index,
      but ensure the main content does NOT shift when sidebar opens. */
@media (max-width: 768px) {
  .nai-main {
    margin-left: 0 !important;
    width: 100% !important;
  }
  /* Prevent any sidebar-open class from shifting main */
  body.sidebar-open .nai-main {
    transform: none !important;
    margin-left: 0 !important;
  }
}

/* 3. Touch targets: minimum 44×44px on all interactive elements on mobile */
@media (max-width: 768px) {
  button,
  .btn,
  .nav-link,
  .fact-action-btn,
  .node-move-up,
  .node-move-down,
  .node-delete,
  .rag-memory-item input[type="checkbox"],
  select,
  .sidebar-tab,
  .chat-history-item,
  .project-item {
    min-height: 44px;
    min-width: 44px;
  }

  /* Icon-only buttons that are naturally smaller — pad them out */
  .icon-btn,
  .btn-icon,
  .send-btn,
  .attach-btn {
    padding: 10px;
    min-height: 44px;
    min-width: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
}

/* 4. Eliminate horizontal scroll on screens under 400px */
@media (max-width: 400px) {
  html, body {
    overflow-x: hidden;
    max-width: 100vw;
  }

  /* Prevent any fixed-width element from breaking layout */
  .modal-content,
  .settings-panel,
  .card,
  .result-panel {
    max-width: calc(100vw - 16px) !important;
    overflow-x: hidden;
  }

  /* Navbar: shrink brand text */
  .navbar-brand span { display: none; }
  .navbar { padding: 0 8px; }

  /* Tables in result panel — allow horizontal scroll within container only */
  .result-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Code blocks — scroll within, not page */
  pre, code {
    max-width: 100%;
    overflow-x: auto;
    white-space: pre;
    word-break: normal;
  }
}

/* ── v3.3.1 — chat artifact 0-file empty state ───────────────────────────── */
.chat-artifact-empty {
  display: flex;
  gap: 12px;
  padding: 14px 16px;
  border: 1px solid #f3d4d4;
  background: #fff8f8;
  border-radius: 8px;
  color: #5a2020;
}
.chat-artifact-empty-icon {
  font-size: 24px;
  line-height: 1;
}
.chat-artifact-empty-body {
  flex: 1;
  min-width: 0;
}
.chat-artifact-empty-title {
  font-weight: 600;
  margin-bottom: 4px;
}
.chat-artifact-empty-msg {
  font-size: 14px;
  line-height: 1.4;
  margin-bottom: 6px;
}
.chat-artifact-empty-hint {
  font-size: 12px;
  opacity: 0.7;
}

/* ── v3.3.2 (C1) — generic modal close button ────────────────────────────── */
.modal-close-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  color: #666;
  transition: background 0.15s, color 0.15s;
}
.modal-close-btn:hover {
  background: rgba(0,0,0,0.08);
  color: #111;
}
.modal-close-btn:focus-visible {
  outline: 2px solid #1a5fb4;
  outline-offset: 2px;
}

/* ── v3.3.2 (C6/C7) — Watch modal Result tab ─────────────────────────────── */
.watch-result-panel {
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.watch-result-status {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 6px;
}
.watch-result-status.completed {
  background: rgba(46, 125, 50, 0.08);
  color: #2e7d32;
}
.watch-result-status.failed {
  background: rgba(211, 47, 47, 0.08);
  color: #c62828;
}
.watch-result-status-icon { font-size: 18px; font-weight: 700; }
.watch-result-status-text { font-weight: 600; }
.watch-result-status-meta { margin-left: auto; font-size: 12px; opacity: 0.7; }
.watch-result-summary {
  font-size: 14px;
  line-height: 1.5;
  color: #333;
}
.watch-result-files {
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  overflow: hidden;
}
.watch-result-files-heading {
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 600;
  background: #fafafa;
  border-bottom: 1px solid #e0e0e0;
}
.watch-result-file-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-bottom: 1px solid #f0f0f0;
}
.watch-result-file-row:last-child { border-bottom: none; }
.watch-result-file-icon { font-size: 16px; }
.watch-result-file-name {
  flex: 1;
  font-family: 'Menlo', Consolas, monospace;
  font-size: 13px;
}
.watch-result-file-size {
  font-size: 12px;
  color: #777;
  font-variant-numeric: tabular-nums;
}
.watch-result-file-download {
  padding: 5px 10px;
  background: #1a5fb4;
  color: white;
  border-radius: 4px;
  text-decoration: none;
  font-size: 12px;
}
.watch-result-file-download:hover { background: #154e9b; }
.watch-result-actions {
  display: flex;
  gap: 8px;
}

/* v3.3.3 (C3): persistent Watch button on completed-build chat messages. */
.chat-artifact-watch-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-left: 8px;
  padding: 2px 10px;
  font-size: 12px;
  line-height: 1.6;
  color: #1a5fb4;
  background: rgba(26, 95, 180, 0.08);
  border: 1px solid rgba(26, 95, 180, 0.35);
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.chat-artifact-watch-btn:hover {
  background: rgba(26, 95, 180, 0.16);
  border-color: #1a5fb4;
}
.chat-artifact-watch-btn:focus-visible {
  outline: 2px solid #1a5fb4;
  outline-offset: 1px;
}

/* ===== v3.9.7 (Track G): searchable model picker — wraps #modelSelect ===== */
.mp-wrap { position: relative; flex: 1 1 auto; min-width: 0; }
.mp-input { width: 100%; cursor: text; }
.mp-panel { display: none; position: absolute; top: 100%; left: 0; right: 0; z-index: 1200; max-height: 340px; overflow-y: auto; margin-top: 4px; background: var(--dropdown-bg, #1a1a1a); color: var(--dropdown-text, #fff); border: 1px solid var(--border-color, rgba(255,255,255,.1)); border-radius: 8px; box-shadow: 0 12px 32px rgba(0,0,0,.45); }
.mp-wrap.mp-open .mp-panel { display: block; }
.mp-group { padding: 7px 12px 3px; font-size: 10px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--text-secondary, rgba(255,255,255,.6)); position: sticky; top: 0; background: var(--dropdown-bg, #1a1a1a); }
.mp-row { display: flex; align-items: center; gap: 8px; padding: 6px 12px; cursor: pointer; }
.mp-row:hover, .mp-row.mp-hi { background: var(--bg-tertiary, rgba(255,255,255,.08)); }
.mp-star { flex: 0 0 auto; cursor: pointer; color: #e3b341; font-size: 14px; line-height: 1; opacity: .55; }
.mp-star.on, .mp-star:hover { opacity: 1; }
.mp-label { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.mp-tag { flex: 0 0 auto; font-size: 10px; padding: 1px 6px; border-radius: 999px; background: var(--input-bg, rgba(255,255,255,.06)); color: var(--text-secondary, rgba(255,255,255,.6)); }
.mp-empty { padding: 12px; font-size: 13px; color: var(--text-secondary, rgba(255,255,255,.6)); }

/* =====================================================================
   CHAT INPUT REDESIGN — THE COMPOSER (presentation-only)
   One bar: [ + menu ] [ auto-grow textarea ] [ Effort ] [ send ].
   The + menu mirrors the #notificationDropdown .show + outside-click pattern.
   ===================================================================== */

.composer-bar { position: relative; }

/* --- + trigger + the active-feature mini-icon --- */
.composer-plus { position: relative; flex: 0 0 auto; align-self: flex-end; display: flex; }
.composer-plus-btn {
  position: relative;
  width: 44px; height: 44px;
  background: var(--input-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 24px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; flex-shrink: 0;
  transition: background .15s, border-color .15s;
}
.composer-plus-btn:hover { background: var(--card-bg); border-color: var(--accent-color); }
.composer-plus-btn[aria-expanded="true"] { border-color: var(--accent-color); background: var(--card-bg); }
.composer-plus-sign { pointer-events: none; transform: translateY(-1px); }
.composer-active-icon {
  position: absolute; top: -7px; right: -7px;
  min-width: 18px; height: 18px; padding: 0 2px; box-sizing: border-box;
  font-size: 11px; line-height: 16px; text-align: center;
  border-radius: 9px;
  background: var(--card-bg);
  border: 1px solid var(--accent-color);
  display: none;
}
.composer-active-icon.is-on { display: block; }

/* --- dropdown menu (opens UPWARD from the bottom bar) --- */
.composer-menu {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 0;
  min-width: 232px;
  max-width: 280px;
  /* Cap to the space above the bar so the upward menu is never clipped by
     #chatPanel's .content-panel{overflow:hidden}; it scrolls internally instead. */
  max-height: min(60vh, 460px, calc(100vh - 150px));
  overflow-y: auto;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .3);
  padding: 6px;
  display: none;
  z-index: 4000;
}
.composer-menu.show { display: block; }

/* --- menu rows: the relocated REAL controls, rendered as full-width rows.
   Scoped under .input-bar so the layout properties out-specify the mobile
   `.input-bar button {width:48px!important}` / `label[for="fileInput"]` nukes
   without editing those rules. --- */
.input-bar .composer-menu .composer-row {
  box-sizing: border-box;
  width: 100% !important;
  height: auto !important;
  min-width: 0 !important;
  min-height: 0 !important;
  margin: 0 !important;
  display: flex !important;
  align-items: center;
  justify-content: flex-start;
  gap: 10px;
  padding: 9px 10px !important;
  border: none !important;
  border-radius: 8px;
  background: transparent !important;
  color: var(--text-primary) !important;
  font-size: 14px;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
  position: relative;
}
.composer-menu .composer-row:hover,
.composer-menu .composer-row:focus-visible { background: var(--input-bg) !important; outline: none; }
/* neutralise the legacy .btn-secondary.active accent-fill if it lingers on a relocated button */
.composer-menu .composer-row.active { background: transparent !important; color: var(--text-primary) !important; }

.composer-row-icon {
  flex: 0 0 auto; width: 20px;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 16px; line-height: 1;
}
.composer-row-icon svg { width: 16px; height: 16px; }
.composer-row-label { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; }
.composer-row-caret { flex: 0 0 auto; color: var(--text-secondary); font-size: 12px; }

/* active-state ✓ — slot exists on every toggle-able row; shown only when the row is flagged
   .is-active (Web Context now; Agent/Dataset/Skills stay dark until a getter is wired). */
.composer-row-check { flex: 0 0 auto; color: var(--accent-color); font-weight: 700; display: none; }
.composer-row.is-active .composer-row-check { display: inline; }
.composer-row.is-active .composer-row-label { color: var(--accent-color); }

/* separators */
.composer-sep { height: 1px; margin: 6px 4px; background: var(--border-color); }

/* --- Select Prompt flyout --- */
.composer-flyout-parent { outline: none; }
.composer-flyout {
  position: fixed;
  min-width: 200px;
  max-width: 260px;
  max-height: min(50vh, 380px);
  overflow-y: auto;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .3);
  padding: 6px;
  display: none;
  z-index: 4001;
}
.composer-flyout-parent.open .composer-flyout,
.composer-flyout-parent:hover .composer-flyout,
.composer-flyout-parent:focus-within .composer-flyout { display: block; }
.composer-flyout-empty { padding: 14px 10px; color: var(--text-secondary); font-size: 13px; text-align: center; white-space: normal; }
.composer-flyout-group { padding: 8px 10px 4px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--text-secondary); }
.input-bar .composer-flyout .composer-flyout-item {
  box-sizing: border-box;
  width: 100% !important;
  height: auto !important;
  min-width: 0 !important;
  display: block;
  text-align: left;
  padding: 8px 10px !important;
  border: none !important;
  background: transparent !important;
  color: var(--text-primary) !important;
  font-size: 14px;
  border-radius: 7px;
  cursor: pointer;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.composer-flyout .composer-flyout-item:hover,
.composer-flyout .composer-flyout-item:focus-visible { background: var(--input-bg) !important; outline: none; }

/* --- composer Effort (relocated from the toolbar — compact, next to send) --- */
.composer-effort.effort-select {
  align-self: flex-end;
  height: 44px;
  width: auto;
  min-width: 116px;
  max-width: 150px;
  flex: 0 0 auto;
}

/* --- cloud model dropdown: widen so model names stop truncating --- */
#modelSelect { min-width: 240px; }
.cloud-model-control { min-width: 220px; }
.cloud-model-control .mp-wrap { min-width: 220px; }   /* v3.9.7 searchable picker wraps #modelSelect */

/* --- Local Engine: label-above on/off toggle (the same #leSwitch checkbox, styled as a slider) --- */
.local-engine-group { min-width: 120px; }
.le-toggle { position: relative; display: inline-flex; align-items: center; cursor: pointer; height: 34px; }
.le-toggle input { position: absolute; opacity: 0; width: 0; height: 0; }
.le-toggle-track {
  position: relative; display: inline-block;
  width: 44px; height: 24px;
  background: var(--border-color);
  border-radius: 999px;
  transition: background .18s;
}
.le-toggle-thumb {
  position: absolute; top: 2px; left: 2px;
  width: 20px; height: 20px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: transform .18s;
}
.le-toggle input:checked + .le-toggle-track { background: var(--accent-color); }
.le-toggle input:checked + .le-toggle-track .le-toggle-thumb { transform: translateX(20px); }
.le-toggle input:focus-visible + .le-toggle-track { box-shadow: 0 0 0 2px var(--accent-color); }

/* ══════ WS-2: Autonomy control surface ══════ */
.autonomy-body { display:flex; flex-direction:column; gap:10px; margin:6px 0; }
.autonomy-ctl { display:flex; flex-direction:column; gap:3px; }
.autonomy-ctl-head { display:flex; align-items:center; gap:6px; flex-wrap:wrap; }
.autonomy-ctl-label { font-weight:600; font-size:.85rem; }
.autonomy-ctl-bool { display:flex; align-items:center; gap:6px; }
.autonomy-save-row { display:flex; align-items:center; gap:10px; margin-top:8px; }
.autonomy-enf { font-size:.68rem; font-weight:700; padding:1px 6px; border-radius:8px; text-transform:uppercase; letter-spacing:.03em; }
.autonomy-enf-ok { background:rgba(34,197,94,.16); color:#22c55e; }
.autonomy-enf-warn { background:rgba(234,179,8,.16); color:#eab308; }
.autonomy-enf-muted { background:rgba(148,163,184,.16); color:#94a3b8; }
.autonomy-status { font-size:.7rem; font-weight:700; padding:1px 6px; border-radius:8px; text-transform:uppercase; }
.autonomy-status-ok { background:rgba(34,197,94,.16); color:#22c55e; }
.autonomy-status-err { background:rgba(239,68,68,.16); color:#ef4444; }
.autonomy-status-muted { background:rgba(148,163,184,.16); color:#94a3b8; }
.autonomy-master-row { display:flex; align-items:center; gap:12px; flex-wrap:wrap; margin:6px 0; }
.autonomy-master-badge { font-weight:800; padding:3px 10px; border-radius:8px; }
.autonomy-master-on { background:rgba(34,197,94,.18); color:#22c55e; }
.autonomy-master-off { background:rgba(148,163,184,.18); color:#94a3b8; }
.autonomy-auto-row, .autonomy-grant-row, .autonomy-cred-row, .autonomy-skill-row, .autonomy-run-row { border:1px solid var(--border-color,#334155); border-radius:8px; padding:8px 10px; margin-bottom:8px; display:flex; justify-content:space-between; gap:10px; flex-wrap:wrap; }
.autonomy-auto-actions, .autonomy-skill-actions { display:flex; gap:6px; flex-wrap:wrap; align-items:flex-start; }
.autonomy-auto-history:not(:empty) { flex-basis:100%; border-top:1px dashed var(--border-color,#334155); margin-top:6px; padding-top:6px; }
.autonomy-council-role { border:1px solid var(--border-color,#334155); border-radius:8px; padding:10px; display:flex; flex-direction:column; gap:8px; margin-bottom:8px; }
.autonomy-council-role-head, .autonomy-council-role-actions, .autonomy-council-badges { display:flex; align-items:center; gap:8px; flex-wrap:wrap; }
.autonomy-council-role-head .autonomy-council-role-id { min-width:180px; flex:1; }
.autonomy-council-picker { display:grid; grid-template-columns:minmax(160px,.7fr) minmax(220px,1.3fr); gap:8px; }
.autonomy-council-grid { display:grid; grid-template-columns:repeat(auto-fit,minmax(150px,1fr)); gap:8px; align-items:end; }
.autonomy-council-picker label, .autonomy-council-grid label { display:flex; flex-direction:column; gap:3px; font-size:.76rem; color:var(--muted-color,#94a3b8); }
.autonomy-council-receipts { max-height:300px; overflow:auto; }
.autonomy-council-receipt { border:1px solid var(--border-color,#334155); border-radius:8px; padding:8px 10px; margin-bottom:8px; }
@media (max-width:720px) { .autonomy-council-picker { grid-template-columns:1fr; } }
.autonomy-audit-row, .autonomy-occ { font-size:.8rem; padding:3px 2px; border-bottom:1px solid var(--border-color,#334155); }

/* ── P8R-Q1 Q-UI02 — resizable Memory Editor + full-workspace graph ──────────
   The base `.modal-content{max-width:500px}` capped the authored 900px and
   there was no #ragEditorModal override (unlike #agentModeModal/#datasetModal),
   so the Memory Editor was stuck at 500px and non-resizable. This gives it a
   governed min/default/max, edge/corner resize, a stable header/footer, and an
   in-app maximized workspace. The Vault graph canvas already follows its
   container via ResizeObserver, so it fills whatever box the modal grants. */
#ragEditorModal .modal-dialog {
  max-width: none;          /* release the inline 900px so JS/resize governs width */
  max-height: none;
  width: auto;
  display: flex;
}
#ragEditorModal .modal-content {
  max-width: none;          /* release the 500px base cap */
  width: var(--rag-editor-w, 80vw);
  height: var(--rag-editor-h, 82vh);
  min-width: 720px;
  min-height: 520px;
  /* viewport-safe maximum with a small safety margin */
  max-width: calc(100vw - 24px);
  max-height: calc(100vh - 24px);
  resize: both;             /* native edge/corner resize; keyboard-reachable via the OS */
  overflow: hidden;         /* only the intended body regions scroll (below) */
  display: flex;
  flex-direction: column;
}
/* the graph mount fills the body region beneath header + toolbar */
#ragEditorModal .modal-body { min-height: 0; }
#ragEditorModal #vaultGraphMount { min-height: 0; }

/* in-app maximized workspace: expand to the full usable window, keep chrome */
#ragEditorModal.rag-graph-maximized { align-items: stretch; justify-content: stretch; padding: 0; }
#ragEditorModal.rag-graph-maximized .modal-dialog { width: 100%; height: 100%; }
#ragEditorModal.rag-graph-maximized .modal-content {
  width: 100vw;
  height: 100vh;
  max-width: 100vw;
  max-height: 100vh;
  min-width: 0;
  min-height: 0;
  border-radius: 0;
  resize: none;             /* maximized state is not user-resized */
}
/* compact toolbar in maximized mode (keep controls, save vertical space) */
#ragEditorModal.rag-graph-maximized .rag-editor-toolbar { padding-top: 6px; padding-bottom: 6px; }

/* graph mode controls (maximize / pop out / restore) live in the toolbar */
.rag-graph-mode-controls { display: inline-flex; gap: 6px; margin-left: auto; }
.rag-graph-mode-controls button { white-space: nowrap; }

/* ── P0-B: provider diagnostic result panel ──────────────────────────────── */
.diagnostic-result {
  margin-top: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border, #2a2a2a);
  border-radius: 6px;
  background: var(--bg-subtle, #141414);
  font-size: 12px;
  line-height: 1.55;
}
.diagnostic-result[hidden] { display: none; }
.diag-head { font-size: 12px; letter-spacing: .04em; margin-bottom: 6px; }
.diag-head .diag-sub { color: var(--text-muted, #8a8a8a); margin-left: 8px; letter-spacing: 0; }
.diag-row { display: flex; gap: 10px; }
.diag-k { flex: 0 0 130px; color: var(--text-muted, #8a8a8a); }
.diag-v { flex: 1 1 auto; word-break: break-word; font-family: var(--font-mono, ui-monospace, monospace); }
.diag-reason {
  margin-top: 8px; padding: 8px 10px;
  border-left: 3px solid var(--border, #3a3a3a);
  background: var(--bg-hover, #1c1c1c);
  color: var(--text, #d4d4d4);
  white-space: pre-wrap;
}
.diag-actions { margin-top: 10px; display: flex; gap: 8px; }
.btn-diagnostic[aria-busy="true"] { opacity: .7; cursor: progress; }

/* ── P0-C/D/E: route indicator + clarification ───────────────────────────── */
.route-indicator {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  margin: 0 0 6px; padding: 6px 10px; font-size: 12px;
  border: 1px solid var(--border, #2a2a2a); border-radius: 6px;
  background: var(--bg-subtle, #141414);
}
.route-indicator[hidden] { display: none; }
.route-chip {
  padding: 2px 8px; border-radius: 10px; font-weight: 600; letter-spacing: .02em;
  background: var(--bg-hover, #1f1f1f); border: 1px solid var(--border, #333);
}
.route-Build, .route-Edit { color: #22c55e; border-color: #22c55e55; }
.route-Answer, .route-Diagnose { color: #60a5fa; border-color: #60a5fa55; }
.route-Clarify { color: #f59e0b; border-color: #f59e0b55; }
.route-Automate, .route-EmployeeWorkflow { color: #a78bfa; border-color: #a78bfa55; }
.route-outcome { color: var(--text, #d4d4d4); }
.route-object { color: var(--text-muted, #8a8a8a); font-family: var(--font-mono, monospace); }
.route-override { color: var(--text-muted, #8a8a8a); font-style: italic; }
.route-correct { margin-left: auto; background: none; border: 1px solid var(--border, #333);
  color: var(--text-muted, #8a8a8a); border-radius: 4px; padding: 2px 8px; cursor: pointer; font-size: 11px; }
.route-correct:hover { color: var(--text, #d4d4d4); border-color: var(--text-muted, #555); }
.route-clarify-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: flex; align-items: center; justify-content: center; z-index: 9999;
}
.route-clarify {
  background: var(--bg, #161616); border: 1px solid var(--border, #333);
  border-radius: 8px; padding: 20px; max-width: 520px; width: calc(100% - 40px);
}
.route-clarify h3 { margin: 0 0 10px; font-size: 15px; }
.route-clarify-prompt { font-family: var(--font-mono, monospace); font-size: 12px;
  color: var(--text-muted, #8a8a8a); background: var(--bg-subtle, #101010);
  padding: 8px 10px; border-radius: 4px; word-break: break-word; }
.route-clarify-reason { font-size: 12px; color: var(--text-muted, #8a8a8a); }
.route-clarify-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }

/* ── Store policy 11.16: "Report AI-generated content" ─────────────────────── */
.ai-report-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 8px;
  font-size: 12px;
  line-height: 1.6;
  color: var(--text-secondary, #9a9a9a);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
  cursor: pointer;
}
.ai-report-btn:hover,
.ai-report-btn:focus-visible {
  color: var(--text-primary, inherit);
  border-color: var(--border-color, rgba(128, 128, 128, 0.4));
}
.ai-report-row { display: flex; justify-content: flex-end; margin-top: 6px; }
#aiReportModal { z-index: 10001; }
.ai-report-intro { font-size: 13px; line-height: 1.5; color: var(--text-secondary, #9a9a9a); margin: 0 0 12px; }
.ai-report-include { display: flex; align-items: center; gap: 6px; font-size: 13px; margin: 4px 0 8px; }
.ai-report-result { margin-top: 10px; }
.ai-report-status { font-size: 13px; margin: 0 0 8px; }
.ai-report-result textarea { font-family: var(--font-mono, monospace); font-size: 12px; }
.ai-report-result-actions { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.welcome-screen .ai-disclosure { max-width: 520px; margin: 16px auto 0; font-size: 12px; line-height: 1.5; color: var(--text-secondary, #9a9a9a); text-align: center; }
#aiReportModal textarea.form-control,
#aiReportModal select.form-control { width: 100%; box-sizing: border-box; }
.ai-report-result-actions a { text-decoration: none; display: inline-flex; align-items: center; }
