/* === toolsinsight.cc BETA — modern agentic UI (ChatGPT/Manus-inspired) === */
/* v=191: conversation sidebar on the left, chat session in the middle. */

:root {
  --bg: #0b0f14;
  --bg2: #10161d;
  --card: #151c25;
  --card2: #1b2430;
  --card3: #222d3b;
  --accent: #3b82f6;
  --accent-hover: #2f6fe0;
  --accent-light: rgba(59,130,246,0.12);
  --accent-grad: linear-gradient(135deg,#3b82f6,#6366f1);
  --text: #e8edf2;
  --text2: #aab4c2;
  --text-muted: #7c8798;
  --danger: #ef4444;
  --success: #22c55e;
  --warning: #f59e0b;
  --radius: 0.75rem;
  --radius-sm: 0.5rem;
  --border: rgba(255,255,255,0.08);
  --border-strong: rgba(255,255,255,0.14);
  --shadow: 0 8px 30px rgba(0,0,0,0.35);
  --shadow-sm: 0 2px 10px rgba(0,0,0,0.25);
  --sidebar-w: 302px; /* v215: 302px = 336px - 10% (user asked narrower sidebar) */
  --header-h: 52px;
}

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

html, body { height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Raleway', sans-serif;
  min-height: 100dvh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* Material Symbols (modern icons) */
.material-symbols-outlined {
  font-family: 'Material Symbols Outlined';
  font-weight: normal; font-style: normal;
  font-size: 20px; line-height: 1; letter-spacing: normal;
  text-transform: none; display: inline-block; white-space: nowrap;
  word-wrap: normal; direction: ltr;
  -webkit-font-feature-settings: 'liga'; font-feature-settings: 'liga';
  -webkit-font-smoothing: antialiased;
  vertical-align: middle;
}
.material-symbols-outlined.sm { font-size: 16px; }
.material-symbols-outlined.lg { font-size: 24px; }

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

/* ========== LOGIN ========== */
.login-page {
  display: flex; align-items: center; justify-content: center;
  min-height: 100dvh;
  background: radial-gradient(1200px 600px at 50% -10%, rgba(59,130,246,0.15), transparent 60%), var(--bg);
}
.login-card {
  background: var(--card); padding: 2.5rem; border-radius: 1rem;
  width: 100%; max-width: 400px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 0.75rem;
}
.login-card h1 { font-family: 'Montserrat', sans-serif; font-size: 1.4rem; font-weight: 800; }
.login-card .subtitle { color: var(--text-muted); font-size: 0.85rem; margin-bottom: 0.5rem; }
.login-card input {
  width: 100%; padding: 0.7rem 0.9rem; background: var(--bg2);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  color: var(--text); font-size: 0.9rem; outline: none;
  transition: border-color 0.15s;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button {
  padding: 0.7rem; background: var(--accent-grad); color: #fff;
  border: none; border-radius: var(--radius-sm); cursor: pointer;
  font-weight: 700; font-size: 0.95rem; transition: opacity 0.15s;
}
.login-card button:hover { opacity: 0.9; }
.login-card .error { color: var(--danger); font-size: 0.8rem; min-height: 1rem; }
/* v217: signup placeholder on login page */
.signup-note { margin-top: 1rem; padding-top: 0.9rem; border-top: 1px solid var(--border); text-align: center; }
.signup-title { font-size: 0.8rem; font-weight: 700; color: var(--text2); }
.signup-sub { font-size: 0.75rem; color: var(--text-muted); margin-top: 0.2rem; }

/* ========== HEADER ========== */
.header {
  height: var(--header-h); display: flex; align-items: center; gap: 0.75rem;
  padding: 0 1rem; background: rgba(11,15,20,0.85); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border); position: relative; z-index: 20;
}
.header .burger { background: none; border: none; color: var(--text2); cursor: pointer; width: 40px; height: 40px; padding: 0; display: flex; align-items: center; justify-content: center; border-radius: var(--radius-sm); line-height: 1; }
.header .burger .material-symbols-outlined { font-size: 24px; pointer-events: none; }
.header .burger:hover { background: var(--card2); color: var(--text); }
.header .logo { font-family: 'Montserrat', sans-serif; font-weight: 800; font-size: 1rem; color: var(--text); display: flex; align-items: center; gap: 0.4rem; }
.header .logo span { color: var(--accent); }
.header .user-info { margin-left: auto; display: flex; align-items: center; gap: 0.6rem; }
.header .user-info span { color: var(--text2); font-size: 0.85rem; }
.header .badge { background: var(--accent-light); color: var(--accent); padding: 0.15rem 0.5rem; border-radius: 999px; font-size: 0.7rem; font-weight: 700; }
.header button {
  background: var(--card2); color: var(--text2); border: 1px solid var(--border);
  padding: 0.35rem 0.7rem; border-radius: var(--radius-sm); cursor: pointer; font-size: 0.8rem;
  transition: all 0.15s;
}
.header button:hover { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ========== MAIN LAYOUT ========== */
#appPage { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.main-layout { display: flex; height: calc(100dvh - var(--header-h)); overflow: hidden; }

/* ========== LEFT: CONVERSATION SIDEBAR ========== */
.sidebar {
  width: var(--sidebar-w); flex-shrink: 0; background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden;
  transition: margin-left 0.2s ease, flex-basis 0.2s ease, width 0.2s ease;
}
.sidebar.open { margin-left: 0; }
/* v192: desktop — burger collapses the sidebar so the chat fills the width */
.sidebar.collapsed { flex: 0 0 0; width: 0; min-width: 0; overflow: hidden; border-right: none; }

.sidebar-top { display: flex; align-items: center; gap: 0.5rem; padding: 0.75rem; }
.new-chat-btn {
  flex: 1; padding: 0.6rem 0.8rem; background: var(--accent-grad); color: #fff;
  border: none; border-radius: var(--radius-sm); cursor: pointer;
  font-weight: 700; font-size: 0.85rem; transition: opacity 0.15s;
}
.new-chat-btn:hover { opacity: 0.9; }
.sidebar-close-btn { background: none; border: none; color: var(--text-muted); font-size: 1.2rem; cursor: pointer; padding: 0.25rem 0.4rem; border-radius: var(--radius-sm); }
.sidebar-close-btn:hover { background: var(--card2); color: var(--text); }

.sidebar-nav { display: flex; gap: 0.35rem; padding: 0 0.75rem 0.6rem; border-bottom: 1px solid var(--border); }
.sidebar-nav .nav-item { flex: 1; justify-content: center; padding: 0.45rem 0.5rem; font-size: 0.8rem; }

.nav-item {
  display: flex; align-items: center; gap: 0.6rem; padding: 0.55rem 0.75rem;
  color: var(--text2); cursor: pointer; border-radius: var(--radius-sm);
  font-size: 0.85rem; transition: all 0.12s; user-select: none;
}
.nav-item:hover { background: var(--card2); color: var(--text); }
.nav-item.active { background: var(--accent-light); color: var(--accent); font-weight: 600; }
.nav-item .nav-icon { font-size: 1rem; width: 1.2rem; text-align: center; }

/* Conversation list */
.conv-list { flex: 1; overflow-y: auto; padding: 0.5rem; display: flex; flex-direction: column; gap: 2px; }
.conv-list::-webkit-scrollbar { width: 6px; }

.session-tab {
  display: flex; align-items: center; gap: 0.35rem; padding: 0.5rem 0.6rem;
  border-radius: var(--radius-sm); cursor: pointer; color: var(--text2);
  font-size: 0.82rem; transition: all 0.12s; position: relative;
  border: 1px solid transparent;
}
.session-tab:hover { background: var(--card2); color: var(--text); }
.session-tab.active { background: var(--accent-light); color: var(--text); border-color: rgba(59,130,246,0.25); }
.session-tab > .session-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-tab .rename, .session-tab .share, .session-tab .del {
  opacity: 0.6; font-size: 0.8rem; cursor: pointer; padding: 0.1rem 0.25rem; border-radius: 4px; transition: opacity 0.12s;
}
.session-tab:hover .rename, .session-tab:hover .share, .session-tab:hover .del { opacity: 1; }
.session-tab .rename:hover, .session-tab .share:hover { background: var(--card3); opacity: 1; }
.session-tab .del:hover { background: rgba(239,68,68,0.2); color: var(--danger); opacity: 1; }

.session-tab.new-tab { justify-content: center; color: var(--accent); font-weight: 600; border: 1px dashed var(--border-strong); margin-top: 0.25rem; }
.session-tab.new-tab:hover { background: var(--accent-light); }
.session-tab.delete-all { justify-content: center; color: var(--text-muted); font-size: 0.75rem; }
.session-tab.delete-all:hover { color: var(--danger); background: rgba(239,68,68,0.1); }

/* v107: session running indicator — Material-style wavy dots while the agent job is active.
   The dots are inserted as the tab's FIRST child, so they must NOT inherit the
   name-span rule (`.session-tab > .session-name { flex:1 }`) — pin them to a
   fixed size so they never stretch into an ellipse. */
.session-tab.running { border-color: rgba(59,130,246,0.45); background: rgba(59,130,246,0.08); }
.session-tab > span.session-run-dots {
  flex: 0 0 auto; align-self: center;
  display: inline-flex; align-items: center; gap: 3px;
  width: 20px; height: 10px;
}
/* v225: queued job marker in the session list */
.session-tab > span.session-queued {
  flex: 0 0 auto; align-self: center;
  font-size: 0.8rem; line-height: 1; margin-right: 2px;
  opacity: 0.85;
}
.session-run-dots i {
  width: 4px; height: 4px; border-radius: 50%;
  background: var(--accent, #3b82f6); box-shadow: 0 0 4px rgba(59,130,246,0.6);
  animation: sessionWave 1s ease-in-out infinite;
}
.session-run-dots i:nth-child(2) { animation-delay: 0.15s; }
.session-run-dots i:nth-child(3) { animation-delay: 0.3s; }
@keyframes sessionWave {
  0%, 100% { transform: translateY(0); opacity: 0.4; }
  50% { transform: translateY(-4px); opacity: 1; }
}

.sidebar-bottom { border-top: 1px solid var(--border); padding: 0.5rem; display: flex; flex-direction: column; gap: 2px; }

/* Ollama config */
.sidebar-section { margin-top: 0.5rem; border-top: 1px solid var(--border); padding-top: 0.5rem; }
.sidebar-section-header { display: flex; align-items: center; justify-content: space-between; padding: 0.4rem 0.5rem; color: var(--text-muted); font-size: 0.75rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
/* v214: collapsible Ollama section — the whole header row is the click target. */
.sidebar-section-header.ollama-header { cursor: pointer; user-select: none; flex-wrap: wrap; }
.sidebar-section-header.ollama-header:hover { color: var(--text); background: var(--card2); }
.sidebar-ollama-title { display: inline-flex; align-items: center; gap: 0.3rem; min-width: 0; }
.sidebar-ollama-right { display: inline-flex; align-items: center; gap: 0.4rem; }
.sidebar-ollama-chevron { font-size: 0.7rem; transition: transform 0.15s; }
.sidebar-section.collapsed .sidebar-ollama-chevron { transform: rotate(-90deg); }
/* v215: when collapsed, the model name drops to its own line BELOW "Ollama Config". */
.sidebar-ollama-model { display: none; font-size: 0.7rem; color: var(--text); font-weight: 500; text-transform: none; letter-spacing: normal; white-space: nowrap; max-width: 140px; overflow: hidden; text-overflow: ellipsis; }
.sidebar-section.collapsed .sidebar-ollama-model { display: block; flex-basis: 100%; margin-top: 0.25rem; padding-left: 2rem; max-width: 100%; }
.sidebar-section.collapsed .sidebar-section-body { display: none; }
.sidebar-status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); flex-shrink: 0; }
.sidebar-status-dot.on { background: var(--success); box-shadow: 0 0 6px var(--success); }
.sidebar-status-dot.off { background: var(--danger); }
.sidebar-section-body { display: flex; flex-direction: column; gap: 0.4rem; padding: 0.4rem 0.5rem 0.5rem; }
.sidebar-section-body label { font-size: 0.7rem; color: var(--text-muted); }
.sidebar-section-body input, .sidebar-section-body select {
  width: 100%; padding: 0.45rem 0.6rem; background: var(--bg);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  color: var(--text); font-size: 0.8rem; outline: none;
}
.sidebar-section-body input:focus, .sidebar-section-body select:focus { border-color: var(--accent); }
/* v229: capability icons (👁🧮🧠💻⚡) next to the selected model. */
.sidebar-model-icons { font-size: 0.85rem; letter-spacing: 1px; line-height: 1; margin-top: -0.1rem; }
.sidebar-ollama-btns { display: flex; gap: 0.4rem; }
.sidebar-ollama-btns button {
  flex: 1; padding: 0.4rem; background: var(--card2); color: var(--text2);
  border: 1px solid var(--border); border-radius: var(--radius-sm); cursor: pointer; font-size: 0.75rem;
  transition: all 0.12s;
}
.sidebar-ollama-btns button:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.sidebar-ollama-status { font-size: 0.7rem; color: var(--text-muted); }

/* v218: paid (managed) users — provider & key hidden, only model choosable */
.sidebar-ollama-locked { display: none; font-size: 0.68rem; color: var(--warning); background: rgba(245,158,11,.07); border: 1px dashed var(--warning); border-radius: var(--radius-sm); padding: 0.4rem 0.5rem; line-height: 1.4; }
#sidebarOllama.managed .ollama-managed-hide { display: none !important; }
#sidebarOllama.managed #sidebarOllamaTest { display: none !important; }
#sidebarOllama.managed .sidebar-ollama-locked { display: block; }

/* v217: credit budget widget (sidebar, above Ollama config) */
.credit-widget {
  display: flex; align-items: center; gap: 0.5rem;
  padding: 0.45rem 0.5rem; margin-top: 0.5rem;
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius-sm); cursor: pointer;
  transition: all 0.12s;
}
.credit-widget:hover { background: var(--card2); border-color: var(--accent); }
.credit-icon { font-size: 1.1rem; color: var(--warning); flex-shrink: 0; }
.credit-text { flex: 1; font-size: 0.8rem; font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.credit-chevron { font-size: 1rem; color: var(--text-muted); flex-shrink: 0; }

/* v217: credit popup */
/* v233: one long scrollable popup — the modal itself scrolls, no inner
   scroll areas (sessions/pricing/daily all flow naturally). */
.credit-modal { max-width: 760px; width: 92vw; max-height: 80vh; overflow-y: auto; }
.credit-summary { display: flex; flex-direction: column; gap: 0.35rem; margin: 0.75rem 0; padding: 0.75rem; background: var(--card2); border-radius: var(--radius-sm); }
.credit-summary-row { display: flex; justify-content: space-between; font-size: 0.85rem; }
.credit-summary-label { color: var(--text-muted); }
.credit-summary-val { color: var(--text); font-weight: 600; }
.credit-bar { height: 6px; background: var(--bg); border-radius: 999px; margin-top: 0.4rem; overflow: hidden; }
.credit-bar-fill { height: 100%; background: var(--accent-grad); border-radius: 999px; transition: width 0.3s; }
.credit-sessions { }
.credit-sessions-title { font-size: 0.7rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); margin: 0.5rem 0 0.35rem; }
.credit-session-row { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--border); }
.credit-session-row:hover { background: var(--card2); }
.credit-session-info { min-width: 0; }
.credit-session-name { font-size: 0.85rem; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.credit-session-date { font-size: 0.7rem; color: var(--text-muted); }
.credit-session-credits { font-size: 0.85rem; font-weight: 700; color: var(--accent); flex-shrink: 0; }
.credit-empty, .credit-loading { color: var(--text-muted); font-size: 0.85rem; padding: 0.75rem 0; }
.credit-upgrade { margin-top: 0.75rem; padding: 0.75rem; background: var(--accent-light); border: 1px dashed var(--accent); border-radius: var(--radius-sm); text-align: center; }
.credit-upgrade-title { font-size: 0.85rem; font-weight: 700; color: var(--accent); }
.credit-upgrade-sub { font-size: 0.75rem; color: var(--text2); margin-top: 0.2rem; }
/* v232: daily credits chart (stacked by model) inside the credits popup */
.credit-daily { margin-top: 0.75rem; border-top: 1px solid var(--border); padding-top: 0.5rem; }
/* v219: model pricing + usage table inside the credits popup */
.credit-pricing { margin-top: 0.75rem; border-top: 1px solid var(--border); padding-top: 0.5rem; }
.credit-pricing-title { font-size: 0.7rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); margin: 0.5rem 0 0.35rem; }
.credit-pricing-admin { background: var(--card2); color: var(--text2); font-size: 0.6rem; font-weight: 600; text-transform: none; letter-spacing: 0; padding: 0.1rem 0.35rem; border-radius: 0.25rem; }
.credit-pricing-note { font-size: 0.7rem; color: var(--text-muted); margin-bottom: 0.4rem; line-height: 1.4; }
.credit-pricing-rate { font-size: 0.7rem; font-weight: 600; color: var(--accent, #4da3ff); margin-bottom: 0.3rem; }
.credit-pricing-table { display: flex; flex-direction: column; gap: 0.3rem; }
.credit-pricing-row { display: flex; align-items: center; gap: 0.35rem; padding: 0.35rem 0.4rem; background: var(--card2); border-radius: var(--radius-sm); flex-wrap: wrap; }
/* read-only combined table: Model | Price in/out | My credits */
.credit-pricing-trow { display: grid; grid-template-columns: 1fr auto auto; gap: 0.5rem; align-items: center; padding: 0.35rem 0.5rem; background: var(--card2); border-radius: var(--radius-sm); }
.credit-pricing-trow.credit-pricing-head { background: transparent; padding: 0.1rem 0.5rem; font-size: 0.65rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); }
.credit-pricing-model { font-size: 0.78rem; color: var(--text); font-weight: 600; min-width: 9rem; flex: 1; }
.credit-pricing-sub { font-size: 0.65rem; color: var(--text-muted); font-weight: 400; }
.credit-pricing-price { font-size: 0.75rem; color: var(--text2); font-weight: 600; flex-shrink: 0; }
.credit-pricing-credits { font-size: 0.78rem; color: var(--accent); font-weight: 700; text-align: right; min-width: 4.5rem; }
.credit-pricing-vision { font-size: 0.6rem; font-weight: 600; color: #818cf8; background: rgba(99,102,241,0.12); padding: 0.05rem 0.35rem; border-radius: 0.25rem; margin-left: 0.3rem; white-space: nowrap; vertical-align: middle; }
.credit-price-input { width: 4.2rem; background: var(--card); color: var(--text); border: 1px solid var(--border); border-radius: 0.25rem; padding: 0.2rem 0.35rem; font-size: 0.75rem; }
.credit-price-btn { background: var(--card); color: var(--accent); border: 1px solid var(--border); border-radius: 0.25rem; padding: 0.2rem 0.5rem; font-size: 0.7rem; cursor: pointer; }
.credit-price-btn:hover { border-color: var(--accent); }
.credit-price-block { color: var(--danger); }
.credit-price-unblock { color: var(--success); }
.credit-price-status { font-size: 0.6rem; font-weight: 700; text-transform: uppercase; padding: 0.05rem 0.3rem; border-radius: 0.25rem; margin-left: 0.3rem; }
.credit-price-status.ok { background: rgba(34,197,94,0.15); color: var(--success); }
.credit-price-status.blocked { background: rgba(239,68,68,0.15); color: var(--danger); }
.credit-price-status.overcap { background: rgba(245,158,11,0.15); color: var(--warning); }
.credit-price-status.fallback { background: rgba(99,102,241,0.15); color: #818cf8; }

/* ========== CENTER: CHAT AREA ========== */
.chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.chat-wrapper { flex: 1; display: flex; flex-direction: column; min-height: 0; width: 100%; }

/* Status bar */
.session-bar { flex-shrink: 0; }
.status-bar {
  display: flex; align-items: center; padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--border); background: var(--bg2);
}
.session-bar-actions { display: flex; align-items: center; gap: 0.75rem; }
.toggle-tools-btn {
  background: none; border: 1px solid var(--border); color: var(--text2);
  padding: 0.3rem 0.6rem; border-radius: 999px; cursor: pointer; font-size: 0.75rem;
  transition: all 0.12s;
}
.toggle-tools-btn:hover { border-color: var(--accent); color: var(--accent); }
.toggle-tools-btn.tools-off { opacity: 0.5; }
.retry-counter-session { font-size: 0.7rem; color: var(--accent); }
.status-spinner {
  width: 14px; height: 14px; border: 2px solid var(--border-strong);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* v107: session search bar (status bar) */
.session-search {
  display: flex; align-items: center; gap: 0.1rem;
  background: var(--card); border: 1px solid var(--border);
  border-radius: 999px; padding: 0.15rem 0.35rem 0.15rem 0.5rem;
  transition: border-color 0.12s;
}
.session-search:focus-within { border-color: var(--accent); }
.session-search-icon { font-size: 1rem; color: var(--text-muted); }
.session-search input {
  width: 130px; background: none; border: none; outline: none;
  color: var(--text); font-size: 0.75rem; padding: 0.2rem 0.3rem;
}
.session-search input::placeholder { color: var(--text-muted); }
.session-search-count {
  font-size: 0.65rem; color: var(--accent); min-width: 2.3rem;
  text-align: center; white-space: nowrap; font-variant-numeric: tabular-nums;
}
.session-search-btn {
  background: none; border: none; color: var(--text-muted); cursor: pointer;
  width: 22px; height: 22px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; transition: all 0.12s;
}
.session-search-btn:hover { background: var(--card3); color: var(--accent); }
.session-search-btn .material-symbols-outlined { font-size: 1rem; }
.session-files-btn {
  background: none; border: 1px solid var(--border); color: var(--text2);
  padding: 0.3rem 0.6rem; border-radius: 999px; cursor: pointer; font-size: 0.75rem;
  display: inline-flex; align-items: center; gap: 0.25rem; transition: all 0.12s;
  white-space: nowrap;
}
.session-files-btn:hover { border-color: var(--accent); color: var(--accent); }
.session-files-btn .material-symbols-outlined { font-size: 1rem; }
/* highlight a matched message in the chat */
.chat-msg.search-match {
  outline: 2px solid var(--accent); outline-offset: 2px;
  border-radius: var(--radius); background: rgba(59,130,246,0.08);
}
/* v107: word-level highlight of the matched text inside the bubble */
#chatMessages mark.search-hl {
  background: rgba(59,130,246,0.45); color: #fff; border-radius: 3px; padding: 0 2px;
}
/* user bubbles have white text on the blue accent gradient — use a light veil */
#chatMessages .chat-msg.user mark.search-hl {
  background: rgba(255,255,255,0.3); color: #fff;
}

/* v107: rendered-markdown preview container (mirrors .chat-msg.assistant) */
.preview-md {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 1rem; max-height: 80vh; overflow: auto;
  font-size: 0.9rem; line-height: 1.6; color: var(--text); word-wrap: break-word;
}
.preview-md p { margin: 0.4rem 0; }
.preview-md p:first-child { margin-top: 0; }
.preview-md p:last-child { margin-bottom: 0; }
.preview-md pre {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 0.6rem; overflow-x: auto; font-size: 0.8rem;
}
.preview-md code { font-family: 'SF Mono', Menlo, Consolas, monospace; }
.preview-md a { color: var(--accent); }
.preview-md table { border-collapse: collapse; margin: 0.5rem 0; }
.preview-md th, .preview-md td { border: 1px solid var(--border); padding: 0.3rem 0.55rem; font-size: 0.82rem; }

/* v107: session files popup */
.session-files-modal { width: min(580px, 92vw); }
.session-files-hint { font-size: 0.7rem; color: var(--text-muted); margin-bottom: 0.5rem; }
.session-files-list {
  max-height: 60vh; overflow-y: auto; display: flex; flex-direction: column;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--bg2);
}
.session-files-empty { padding: 1.2rem; text-align: center; color: var(--text-muted); font-size: 0.82rem; }
.session-files-date {
  position: sticky; top: 0; z-index: 1;
  background: var(--bg2); color: var(--accent);
  font-size: 0.68rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em;
  padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--border);
}
.session-file-row {
  display: flex; align-items: center; gap: 0.55rem;
  padding: 0.42rem 0.6rem; border-radius: var(--radius-sm);
  transition: background 0.12s;
}
.session-file-row:hover { background: var(--card2); }
.session-file-icon { flex: 0 0 auto; font-size: 0.95rem; }
.session-file-name {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 0.82rem;
}
.session-file-size { flex: 0 0 auto; font-size: 0.7rem; color: var(--text-muted); }
.session-file-time { flex: 0 0 auto; font-size: 0.7rem; color: var(--text-muted); }

/* Toggle row (direct chat) */
.toggle-row { display: flex; gap: 0.5rem; padding: 0.5rem 1rem; border-bottom: 1px solid var(--border); }
.toggle-btn {
  background: var(--card2); color: var(--text2); border: 1px solid var(--border);
  padding: 0.35rem 0.7rem; border-radius: 999px; cursor: pointer; font-size: 0.75rem;
  transition: all 0.12s;
}
.toggle-btn:hover { border-color: var(--accent); color: var(--accent); }

/* Messages */
.chat-messages { flex: 1; overflow-y: auto; padding: 1.25rem 1rem; display: flex; flex-direction: column; gap: 0.6rem; }

.chat-msg {
  max-width: 85%; padding: 0.7rem 0.95rem; border-radius: 1rem;
  font-size: 0.9rem; line-height: 1.55; word-wrap: break-word;
  animation: msgIn 0.18s ease;
}
@keyframes msgIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.chat-msg.user {
  align-self: flex-end; background: var(--accent-grad); color: #fff;
  border-bottom-right-radius: 0.25rem;
}
.chat-msg.assistant {
  align-self: flex-start; background: var(--card); color: var(--text);
  border: 1px solid var(--border); border-bottom-left-radius: 0.25rem;
}
.chat-msg.assistant p { margin: 0.4rem 0; }
.chat-msg.assistant p:first-child { margin-top: 0; }
.chat-msg.assistant p:last-child { margin-bottom: 0; }
.chat-msg.assistant pre {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 0.6rem; overflow-x: auto; font-size: 0.8rem;
}
.chat-msg.assistant code { font-family: 'SF Mono', Menlo, Consolas, monospace; }
.chat-msg.assistant a { color: var(--accent); }

/* v107: blinking "..." working indicator — the last bubble while the agent runs */
.chat-msg.working-dots {
  align-self: flex-start; background: var(--card); color: var(--text);
  border: 1px solid var(--border); border-bottom-left-radius: 0.25rem;
  display: flex; align-items: center; gap: 0.35rem;
  padding: 0.8rem 1rem; width: fit-content;
}
.chat-msg.working-dots .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent); display: inline-block;
  animation: dotBlink 1.2s infinite ease-in-out;
}
.chat-msg.working-dots .dot:nth-child(2) { animation-delay: 0.15s; }
.chat-msg.working-dots .dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes dotBlink {
  0%, 60%, 100% { opacity: 0.25; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-2px); }
}

/* Tool bubbles */
body.hide-tools .tool-bubble { display: none !important; }
/* v107: even with tools hidden, keep "running" bubbles visible so the user can
   see the agent is still working (hiding them looked like the agent stopped). */
body.hide-tools .tool-bubble.tool-running { display: block !important; }
.chat-msg.tool-bubble {
  align-self: flex-start; max-width: 92%; background: var(--bg2);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 0.5rem 0.75rem; font-size: 0.78rem; color: var(--text2);
  font-family: 'SF Mono', Menlo, Consolas, monospace; cursor: pointer;
  transition: border-color 0.12s;
}
.chat-msg.tool-bubble:hover { border-color: var(--border-strong); }
.chat-msg.tool-bubble strong { color: var(--text); font-weight: 600; }
.chat-msg.tool-bubble.tool-done { border-left: 3px solid var(--success); }
.chat-msg.tool-bubble.tool-running { border-left: 3px solid var(--accent); }
.chat-msg.tool-bubble.tool-error { border-left: 3px solid var(--danger); color: var(--danger); }
.chat-msg.tool-bubble .file-dl-link { color: var(--accent); text-decoration: none; }
.chat-msg.tool-bubble .file-preview-btn { background: none; border: 1px solid var(--border); color: var(--text2); border-radius: 4px; cursor: pointer; padding: 0.1rem 0.3rem; margin-left: 0.3rem; }

/* Scroll to bottom */
.scroll-bottom-btn {
  position: absolute; bottom: 120px; right: 24px; width: 36px; height: 36px;
  background: var(--card2); color: var(--text2); border: 1px solid var(--border);
  border-radius: 50%; cursor: pointer; font-size: 1rem; box-shadow: var(--shadow-sm);
  transition: all 0.12s; z-index: 5;
  display: flex; align-items: center; justify-content: center; line-height: 1;
  padding: 0;
}
.scroll-bottom-btn:hover { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Input bar */
.chat-input-area {
  flex-shrink: 0; display: flex; align-items: flex-end; gap: 0.5rem;
  padding: 0.75rem 1rem calc(1rem + env(safe-area-inset-bottom)); border-top: 1px solid var(--border);
}
/* v216: highlight the chat input while dragging a file over it (drop = attach). */
.chat-input-area.drag-over { border-top-color: var(--accent); box-shadow: inset 0 0 0 2px var(--accent); }
.chat-input-area textarea {
  flex: 1; padding: 0.7rem 0.9rem; background: var(--card);
  border: 1px solid var(--border); border-radius: 1rem; color: var(--text);
  resize: none; font-family: inherit; font-size: 0.9rem; min-height: 44px;
  max-height: 480px; overflow-y: auto; line-height: 1.4; outline: none;
  transition: border-color 0.15s;
  /* v107: let the browser auto-grow the textarea natively so typing doesn't
     force a full-page layout on every keystroke (the old JS resize did).
     The JS _autoResizeInput becomes a no-op when this is supported. */
  field-sizing: content;
}
.chat-input-area textarea:focus { border-color: var(--accent); }
.chat-input-area select {
  background: var(--card2); color: var(--text2); border: 1px solid var(--border);
  padding: 0.4rem 0.5rem; border-radius: var(--radius-sm); font-size: 0.8rem; outline: none;
}
.retry-label { color: var(--text-muted); font-size: 0.7rem; }
.retry-counter { font-size: 0.7rem; color: var(--accent); }
.chat-input-area button {
  background: var(--card2); color: var(--text2); border: 1px solid var(--border);
  padding: 0.5rem 0.7rem; border-radius: var(--radius-sm); cursor: pointer; font-size: 0.9rem;
  transition: all 0.12s; height: 44px;
}
.chat-input-area button:hover { background: var(--card3); color: var(--text); }
.chat-input-area #sendBtn {
  background: var(--accent-grad); color: #fff; border: none; font-weight: 700;
  padding: 0.5rem 1.2rem; border-radius: 1rem;
}
.chat-input-area #sendBtn:hover { opacity: 0.9; }
.chat-input-area #sendBtn.stop-btn { background: var(--danger); }

/* ========== RIGHT PANELS ========== */
.right-panel {
  width: 300px; flex-shrink: 0; background: var(--bg2); border-left: 1px solid var(--border);
  display: flex; flex-direction: column; overflow: hidden;
  transition: margin-right 0.2s ease;
}
/* v191: Tools/TODO panels are hidden by default — shown only when toggled (.open). */
.right-panel:not(.open) { display: none; }
.right-panel h3 {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1rem; font-family: 'Montserrat', sans-serif; font-size: 0.9rem;
  border-bottom: 1px solid var(--border); color: var(--text);
}
.close-panel { background: none; border: none; color: var(--text-muted); font-size: 1rem; cursor: pointer; }
.close-panel:hover { color: var(--danger); }
.right-panel > div:not(h3) { flex: 1; overflow-y: auto; padding: 0.5rem; }

.tool-item {
  display: flex; align-items: center; flex-wrap: wrap; gap: 0.5rem; padding: 0.5rem 0.6rem;
  border-radius: var(--radius-sm); cursor: pointer; color: var(--text2); font-size: 0.82rem;
  transition: all 0.12s;
}
.tool-item > span:first-child { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; }
.tool-item:hover { background: var(--card2); color: var(--text); }

/* Tool action buttons (Code / Share / Del) */
.tool-actions { margin-left: auto; display: flex; flex-wrap: wrap; gap: 0.25rem; }
.tool-actions .tool-act {
  background: var(--card2); color: var(--text2); border: 1px solid var(--border);
  border-radius: 0.4rem; cursor: pointer; padding: 0.25rem 0.45rem;
  font-size: 0.68rem; font-weight: 600; display: flex; align-items: center; gap: 0.2rem;
  transition: all 0.12s; line-height: 1;
}
.tool-actions .tool-act .material-symbols-outlined { font-size: 14px; }
.tool-actions .tool-act:hover { background: var(--card3); color: var(--text); border-color: var(--border-strong); }
.tool-actions .tool-act-share { color: var(--success); }
.tool-actions .tool-act-share:hover { background: rgba(34,197,94,0.15); border-color: var(--success); color: var(--success); }
.tool-actions .tool-act-del { color: var(--danger); }
.tool-actions .tool-act-del:hover { background: rgba(239,68,68,0.15); border-color: var(--danger); color: var(--danger); }

.todo-item { padding: 0.5rem 0.6rem; border-radius: var(--radius-sm); font-size: 0.82rem; color: var(--text2); border-left: 3px solid var(--border-strong); margin-bottom: 0.3rem; }
.todo-item.doing { border-left-color: var(--warning); color: var(--text); }
.todo-item.done { border-left-color: var(--success); color: var(--text-muted); text-decoration: line-through; }
.todo-item.skipped { border-left-color: var(--text-muted); opacity: 0.6; }

/* ========== MODALS ========== */
.modal-overlay {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0,0,0,0.6); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; z-index: 1000;
}
/* v191: modals are hidden by default and shown only when UI.openModal adds .open —
   otherwise every modal (incl. Change Password) rendered on top of the login page. */
.modal-overlay:not(.open) { display: none; }
.modal {
  background: var(--card); border: 1px solid var(--border); border-radius: 1rem;
  padding: 1.5rem; max-width: 560px; width: 92%; max-height: 90vh; overflow-y: auto;
  box-shadow: var(--shadow);
}
.modal h3 { font-family: 'Montserrat', sans-serif; font-size: 1.05rem; margin-bottom: 1rem; }
/* v225: queue confirm modal */
.queue-confirm-modal { max-width: 420px; text-align: center; }
.queue-confirm-modal p { color: var(--text2); font-size: 0.9rem; line-height: 1.5; margin: 0 0 0.25rem; }
.queue-confirm-modal .btn-row { justify-content: center; }
.modal input, .modal select {
  width: 100%; padding: 0.6rem 0.8rem; background: var(--bg2);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  color: var(--text); font-size: 0.85rem; outline: none; margin-bottom: 0.6rem;
}
.modal input:focus, .modal select:focus { border-color: var(--accent); }
.modal-close-top { position: absolute; top: 0.75rem; right: 0.75rem; background: none; border: none; color: var(--text-muted); font-size: 1.4rem; cursor: pointer; }

.btn-row { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 0.75rem; }
.btn { padding: 0.5rem 1rem; border-radius: var(--radius-sm); border: none; cursor: pointer; font-size: 0.85rem; font-weight: 600; }
.btn-primary { padding: 0.5rem 1rem; background: var(--accent-grad); color: #fff; border: none; border-radius: var(--radius-sm); cursor: pointer; font-size: 0.85rem; font-weight: 600; transition: opacity 0.12s; }
.btn-primary:hover { opacity: 0.9; }
.btn-secondary { padding: 0.5rem 1rem; background: var(--card2); color: var(--text2); border: 1px solid var(--border); border-radius: var(--radius-sm); cursor: pointer; font-size: 0.85rem; transition: all 0.12s; }
.btn-secondary:hover { background: var(--card3); color: var(--text); }

/* File explorer */
.file-explorer-modal { max-width: 720px; width: 94%; }
.breadcrumb { display: flex; gap: 0.4rem; align-items: center; flex-wrap: wrap; margin-bottom: 0.6rem; font-size: 0.8rem; color: var(--text2); }
.breadcrumb span { cursor: pointer; }
.breadcrumb span:hover { color: var(--accent); }
.parent-dir { color: var(--accent); cursor: pointer; }
.drop-zone {
  border: 2px dashed var(--border-strong); border-radius: var(--radius-sm);
  padding: 1.5rem; text-align: center; color: var(--text-muted); margin-bottom: 0.75rem;
  transition: all 0.15s; cursor: pointer;
}
.drop-zone:hover, .drop-zone.dragover { border-color: var(--accent); color: var(--accent); background: var(--accent-light); }
.drop-zone-text { font-size: 0.85rem; }
.file-list { list-style: none; }
.file-list li {
  display: flex; align-items: center; gap: 0.5rem; padding: 0.5rem 0.6rem;
  border-radius: var(--radius-sm); cursor: pointer; font-size: 0.85rem; color: var(--text2);
  transition: all 0.12s;
}
.file-list li:hover { background: var(--card2); color: var(--text); }
.file-list li .file-actions { margin-left: auto; display: flex; gap: 0.3rem; opacity: 0; transition: opacity 0.12s; }
.file-list li:hover .file-actions { opacity: 1; }

.user-list { list-style: none; }
.user-list li { display: flex; align-items: center; gap: 0.5rem; padding: 0.5rem 0.6rem; border-radius: var(--radius-sm); font-size: 0.85rem; color: var(--text2); }
.user-list li:hover { background: var(--card2); }

/* Tool detail */
.tool-detail-overlay {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0,0,0,0.7); display: flex; align-items: center; justify-content: center; z-index: 1000;
}
.tool-detail-modal {
  background: var(--card); border: 1px solid var(--border); border-radius: 1rem;
  padding: 1.25rem; max-width: 720px; width: 92%; max-height: 85vh; overflow-y: auto;
  box-shadow: var(--shadow);
}
.tool-detail-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.75rem; }
.tool-detail-header h3 { color: var(--accent); font-family: 'Montserrat', sans-serif; font-size: 0.95rem; }
.tool-detail-close { background: none; border: none; color: var(--text-muted); font-size: 1.3rem; cursor: pointer; }
.tool-detail-close:hover { color: var(--text); }
.tool-detail-body h4 { color: var(--text-muted); font-size: 0.75rem; margin: 0.6rem 0 0.3rem; text-transform: uppercase; letter-spacing: 0.04em; }
.tool-detail-body pre { background: var(--bg2); padding: 0.75rem; border-radius: var(--radius-sm); overflow-x: auto; font-size: 0.8rem; color: var(--text2); }
.tool-detail-short { color: var(--text2); font-size: 0.85rem; }
.tool-expand-btn { background: var(--card2); color: var(--accent); border: 1px solid var(--border); padding: 0.3rem 0.6rem; border-radius: var(--radius-sm); cursor: pointer; font-size: 0.75rem; }

/* Code editor */
.code-editor-wrapper { border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: auto; height: 100%; }
.codejar-editor { min-height: 100%; background: var(--bg2); color: var(--text); font-family: 'SF Mono', Menlo, Consolas, monospace; font-size: 0.8rem; padding: 0.75rem; outline: none; }
.codejar-linenumbers { background: var(--bg2); color: var(--text-muted); }
.codejar-linenumber { color: var(--text-muted); }

/* Loading overlay */
.loading-overlay {
  position: fixed; top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(11,15,20,0.7); backdrop-filter: blur(4px);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 0.75rem; z-index: 2000; color: var(--text2); font-size: 0.9rem;
}
.spinner {
  width: 32px; height: 32px; border: 3px solid var(--border-strong);
  border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite;
}
.loading-dots::after { content: '...'; animation: dots 1.2s steps(4, end) infinite; }
@keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }

/* ========== MOBILE ========== */
.mobile-top-bar {
  display: none; height: 48px; align-items: center; gap: 0.5rem;
  padding: 0 0.75rem; background: var(--bg2); border-bottom: 1px solid var(--border);
}
.mobile-top-bar .burger {
  background: none; border: none; color: var(--text2); cursor: pointer;
  width: 48px; height: 48px; padding: 0; margin: 0; flex: 0 0 48px;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm); line-height: 1;
}
.mobile-top-bar .burger .material-symbols-outlined { font-size: 28px; pointer-events: none; }
.mobile-top-bar .burger:hover, .mobile-top-bar .burger:active { background: var(--card2); color: var(--text); }
.mobile-top-bar .logo { font-family: 'Montserrat', sans-serif; font-weight: 800; font-size: 0.9rem; }
.mobile-top-bar .mobile-actions { margin-left: auto; display: flex; align-items: center; gap: 0.4rem; }
.mobile-top-bar .mobile-actions button { background: none; border: none; color: var(--text2); font-size: 1rem; cursor: pointer; padding: 0.2rem 0.3rem; }
.status-dot-mobile { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); }
.status-dot-mobile.on { background: var(--success); }

@media (max-width: 900px) {
  .mobile-top-bar { display: flex; }
  .header { display: none; }
  .main-layout { height: calc(100dvh - 48px); }
  .sidebar { position: fixed; left: 0; top: 48px; bottom: 0; z-index: 100; margin-left: -100%; box-shadow: var(--shadow); }
  .sidebar.open { margin-left: 0; }
  .right-panel { position: fixed; right: 0; top: 48px; bottom: 0; z-index: 100; margin-right: -100%; box-shadow: var(--shadow); }
  .right-panel.open { margin-right: 0; }
  .chat-msg { max-width: 92%; }
  /* v191: on touch there is no hover — keep session actions (rename/share/delete) visible */
  .session-tab .rename, .session-tab .share, .session-tab .del { opacity: 0.8; }
  .chat-input-area { padding-bottom: calc(0.75rem + env(safe-area-inset-bottom)); }
  /* v191: show ALL chats on mobile — the session list is not independently
     scrollable; the whole sidebar scrolls instead so every session is reachable. */
  .conv-list { flex: none; overflow-y: visible; }
  .sidebar { overflow-y: auto; }
}

@media (max-width: 600px) {
  /* v191: keep the text input visible on small screens — drop the retry select */
  .chat-input-area .retry-label, .chat-input-area .retry-counter, .chat-input-area select { display: none; }
  .chat-input-area button { padding: 0.4rem 0.55rem; height: 40px; }
  .chat-input-area #sendBtn { padding: 0.4rem 0.9rem; }
  .chat-input-area textarea { min-height: 40px; }
  /* v205: mobile status bar — keep tools-toggle + search + session-files in ONE
     row. Tools and files become icon-only (text hidden, title attr still shows
     it on tap-and-hold); the search pill flexes so its input shrinks to fit. */
  .status-bar { padding: 0.4rem 0.5rem; }
  .session-bar-actions { gap: 0.4rem; }
  .toggle-tools-btn { padding: 0.3rem 0.45rem; }
  .toggle-tools-btn #toggleToolsLabel { display: none; }
  .session-search { flex: 1 1 auto; min-width: 80px; padding: 0.12rem 0.3rem 0.12rem 0.4rem; }
  .session-search input { width: 100%; min-width: 36px; padding: 0.2rem 0.2rem; }
  .session-search-count { min-width: 1.7rem; font-size: 0.6rem; }
  .session-files-btn { padding: 0.3rem 0.45rem; font-size: 0; }
  .session-files-btn .material-symbols-outlined { font-size: 1.1rem; }
}

/* ========== MISC ========== */
/* v107: KaTeX-rendered math inside chat bubbles */
.chat-msg .katex-display { margin: 0.5rem 0; overflow-x: auto; overflow-y: hidden; }
.chat-msg .katex { font-size: 1.05em; }
.error { color: var(--danger); font-size: 0.8rem; }
.token { color: var(--accent); }
.file-dl-link { color: var(--accent); text-decoration: none; }
.file-preview-btn { background: none; border: 1px solid var(--border); color: var(--text2); border-radius: 4px; cursor: pointer; padding: 0.1rem 0.3rem; margin-left: 0.3rem; }
.btn-dl { background: var(--accent-light); color: var(--accent); border: 1px solid var(--accent); padding: 0.3rem 0.6rem; border-radius: var(--radius-sm); cursor: pointer; font-size: 0.75rem; }
