:root {
  --ctx-bg: #f6f7fb;
  --ctx-surface: #ffffff;
  --ctx-surface-alt: #f1f4fa;
  --ctx-border: #e1e5ed;
  --ctx-border-strong: #cdd3dd;
  --ctx-text: #0f172a;
  --ctx-muted: #64748b;
  --ctx-primary: #2563eb;
  --ctx-primary-hover: #1d4ed8;
  --ctx-primary-soft: #dbeafe;
  --ctx-accent: #7c3aed;
  --ctx-success: #16a34a;
  --ctx-warn: #d97706;
  --ctx-error: #dc2626;
  --ctx-radius: 10px;
  --ctx-radius-sm: 6px;
  --ctx-shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 4px 12px rgba(15, 23, 42, 0.04);
  --ctx-shadow-lg: 0 4px 14px rgba(15, 23, 42, 0.08), 0 16px 32px rgba(15, 23, 42, 0.06);
  --ctx-fireflies: #f97316;
  --ctx-gmail: #ea4335;
  --ctx-slack: #4a154b;
  --ctx-calendar: #1a73e8;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--ctx-bg);
  color: var(--ctx-text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
}

body:not(.auth-ready) { display: none !important; }

/* The HTML `hidden` attribute must win over any `display: flex/grid/etc.` we set
   on overlay classes below; otherwise modals show on page load. */
[hidden] { display: none !important; }

.ctx-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- Header ---------- */
.ctx-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  background: var(--ctx-surface);
  border-bottom: 1px solid var(--ctx-border);
}

.ctx-brand {
  display: flex;
  align-items: center;
  gap: 14px;
}

.ctx-brand img {
  height: 32px;
  width: auto;
}

.ctx-brand div {
  display: flex;
  flex-direction: column;
  line-height: 1.25;
}

.ctx-brand strong {
  font-size: 16px;
  font-weight: 600;
}

.ctx-subtitle {
  color: var(--ctx-muted);
  font-size: 12px;
}

.ctx-header-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* ---------- Buttons ---------- */
.ctx-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  background: var(--ctx-surface);
  border: 1px solid var(--ctx-border-strong);
  border-radius: var(--ctx-radius-sm);
  color: var(--ctx-text);
  cursor: pointer;
  font-weight: 500;
  font-size: 13px;
  transition: all 0.15s ease;
}

.ctx-btn:hover {
  background: var(--ctx-surface-alt);
  border-color: var(--ctx-primary);
  color: var(--ctx-primary);
}

.ctx-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  background: var(--ctx-surface);
  color: var(--ctx-muted);
}

.ctx-btn-primary {
  background: var(--ctx-primary);
  border-color: var(--ctx-primary);
  color: #fff;
}

.ctx-btn-primary:hover {
  background: var(--ctx-primary-hover);
  border-color: var(--ctx-primary-hover);
  color: #fff;
}

.ctx-btn-ghost {
  background: transparent;
  border-color: transparent;
  padding: 4px 6px;
}

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

.ctx-btn i {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

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

.ctx-tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 12px 18px;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--ctx-muted);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
}

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

.ctx-tab.active {
  color: var(--ctx-primary);
  border-bottom-color: var(--ctx-primary);
}

.ctx-tab i {
  width: 14px;
  height: 14px;
}

.ctx-tab-count {
  background: var(--ctx-primary-soft);
  color: var(--ctx-primary);
  border-radius: 999px;
  padding: 1px 8px;
  font-size: 11px;
  font-weight: 600;
}

/* ---------- Banner ---------- */
.ctx-banner {
  margin: 12px 24px 0;
  padding: 10px 14px;
  border-radius: var(--ctx-radius-sm);
  background: var(--ctx-primary-soft);
  color: var(--ctx-primary);
  border: 1px solid var(--ctx-primary);
  font-size: 13px;
}

.ctx-banner.error {
  background: #fee2e2;
  border-color: var(--ctx-error);
  color: var(--ctx-error);
}

.ctx-banner.success {
  background: #dcfce7;
  border-color: var(--ctx-success);
  color: var(--ctx-success);
}

.ctx-banner.warn {
  background: #fef3c7;
  border-color: var(--ctx-warn);
  color: var(--ctx-warn);
}

/* ---------- Body / Views ---------- */
.ctx-body {
  flex: 1;
  padding: 20px 24px 28px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.ctx-view {
  display: none;
  flex: 1;
  min-height: 0;
  flex-direction: column;
}

.ctx-view.active {
  display: flex;
}

.ctx-card {
  background: var(--ctx-surface);
  border: 1px solid var(--ctx-border);
  border-radius: var(--ctx-radius);
  padding: 20px 22px;
  box-shadow: var(--ctx-shadow);
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.ctx-card-header {
  margin-bottom: 16px;
}

.ctx-card-header h2 {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 4px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.ctx-card-header h2 i {
  width: 18px;
  height: 18px;
  color: var(--ctx-primary);
}

.ctx-muted {
  color: var(--ctx-muted);
  font-size: 13px;
  line-height: 1.5;
  margin: 0;
}

/* ---------- Chat layout ---------- */
.ctx-chat-layout {
  flex: 1;
  display: grid;
  grid-template-columns: 240px 1fr;
  gap: 16px;
  min-height: 0;
}

.ctx-conv-sidebar {
  background: var(--ctx-surface);
  border: 1px solid var(--ctx-border);
  border-radius: var(--ctx-radius);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.ctx-conv-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--ctx-border);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ctx-muted);
  font-weight: 600;
}

.ctx-conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}

.ctx-conv-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--ctx-radius-sm);
  padding: 8px 10px;
  cursor: pointer;
  margin-bottom: 4px;
}

.ctx-conv-item:hover {
  background: var(--ctx-surface-alt);
}

.ctx-conv-item.active {
  background: var(--ctx-primary-soft);
  border-color: var(--ctx-primary);
}

.ctx-conv-item .ctx-conv-title {
  display: block;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.3;
  color: var(--ctx-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.ctx-chat-main {
  display: flex;
  flex-direction: column;
  background: var(--ctx-surface);
  border: 1px solid var(--ctx-border);
  border-radius: var(--ctx-radius);
  min-height: 0;
  box-shadow: var(--ctx-shadow);
}

.ctx-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.ctx-chat-welcome h2 {
  font-size: 22px;
  margin: 0 0 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.ctx-chat-welcome h2 i {
  width: 22px;
  height: 22px;
  color: var(--ctx-accent);
}

.ctx-chat-welcome p {
  color: var(--ctx-muted);
  line-height: 1.5;
  max-width: 720px;
}

.ctx-suggestion-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
  margin-top: 16px;
  max-width: 720px;
}

.ctx-suggestion {
  text-align: left;
  background: var(--ctx-surface-alt);
  border: 1px solid var(--ctx-border);
  border-radius: var(--ctx-radius-sm);
  padding: 10px 12px;
  color: var(--ctx-text);
  cursor: pointer;
  font-size: 13px;
}

.ctx-suggestion:hover {
  border-color: var(--ctx-primary);
  background: var(--ctx-primary-soft);
}

.ctx-message {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 92%;
}

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

.ctx-message.user .ctx-message-bubble {
  background: var(--ctx-primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.ctx-message.assistant {
  align-self: flex-start;
}

.ctx-message.assistant .ctx-message-bubble {
  background: var(--ctx-surface-alt);
  color: var(--ctx-text);
  border: 1px solid var(--ctx-border);
  border-bottom-left-radius: 4px;
}

.ctx-message-bubble {
  padding: 12px 14px;
  border-radius: 14px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-wrap: break-word;
  font-size: 14px;
}

.ctx-tool-trail {
  margin-top: 6px;
  border: 1px solid var(--ctx-border);
  border-radius: var(--ctx-radius-sm);
  background: var(--ctx-surface);
  font-size: 12px;
}

.ctx-tool-trail summary {
  cursor: pointer;
  padding: 8px 12px;
  color: var(--ctx-muted);
  display: flex;
  align-items: center;
  gap: 6px;
  list-style: none;
}

.ctx-tool-trail summary::-webkit-details-marker { display: none; }

.ctx-tool-trail summary i {
  width: 12px;
  height: 12px;
}

.ctx-tool-trail[open] summary {
  border-bottom: 1px solid var(--ctx-border);
  color: var(--ctx-text);
}

.ctx-tool-list {
  list-style: none;
  margin: 0;
  padding: 8px 12px;
}

.ctx-tool-list li {
  padding: 4px 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  border-bottom: 1px dashed var(--ctx-border);
}

.ctx-tool-list li:last-child {
  border-bottom: 0;
}

.ctx-tool-name {
  font-weight: 600;
  color: var(--ctx-accent);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
}

.ctx-tool-args {
  color: var(--ctx-muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  white-space: pre-wrap;
  word-break: break-word;
}

.ctx-tool-count {
  color: var(--ctx-muted);
  font-size: 11px;
}

.ctx-thinking {
  display: inline-flex;
  gap: 4px;
  padding: 10px 14px;
}

.ctx-thinking-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ctx-muted);
  animation: ctx-bounce 1.2s infinite ease-in-out both;
}

.ctx-thinking-dot:nth-child(1) { animation-delay: -0.32s; }
.ctx-thinking-dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes ctx-bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

.ctx-chat-input {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 14px;
  border-top: 1px solid var(--ctx-border);
  background: var(--ctx-surface);
}

.ctx-chat-input textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--ctx-border-strong);
  border-radius: var(--ctx-radius-sm);
  padding: 10px 12px;
  font-family: inherit;
  font-size: 13px;
  background: var(--ctx-surface);
  color: var(--ctx-text);
  line-height: 1.4;
  max-height: 200px;
}

.ctx-chat-input textarea:focus {
  outline: none;
  border-color: var(--ctx-primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

/* ---------- Timeline ---------- */
.ctx-filter-bar {
  display: grid;
  grid-template-columns: 2fr 1fr 1.5fr 1fr 1fr auto;
  gap: 8px;
  margin-bottom: 16px;
}

.ctx-filter-bar input,
.ctx-filter-bar select {
  border: 1px solid var(--ctx-border-strong);
  border-radius: var(--ctx-radius-sm);
  padding: 8px 10px;
  font-family: inherit;
  font-size: 13px;
  background: var(--ctx-surface);
  color: var(--ctx-text);
}

/* Participant filter is rendered as a contenteditable div (not an <input>)
   to defeat Chrome's email autofill heuristic at the root — autofill never
   targets non-input elements. These rules make it visually identical to a
   real text input. */
.ctx-filter-bar .ctx-fakeinput {
  border: 1px solid var(--ctx-border-strong);
  border-radius: var(--ctx-radius-sm);
  padding: 8px 10px;
  font-family: inherit;
  font-size: 13px;
  background: var(--ctx-surface);
  color: var(--ctx-text);
  min-height: 18px;
  line-height: 18px;
  cursor: text;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ctx-filter-bar .ctx-fakeinput:empty::before {
  content: attr(data-placeholder);
  color: var(--ctx-muted);
  pointer-events: none;
}

.ctx-filter-bar .ctx-fakeinput:focus {
  outline: 2px solid var(--ctx-accent, #2563eb);
  outline-offset: -1px;
}

.ctx-timeline-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  padding-right: 6px;
}

.ctx-timeline-row {
  border: 1px solid var(--ctx-border);
  border-radius: var(--ctx-radius-sm);
  background: var(--ctx-surface);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.ctx-timeline-row:hover {
  border-color: var(--ctx-border-strong);
  box-shadow: var(--ctx-shadow);
}

.ctx-timeline-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 11px;
  color: var(--ctx-muted);
  flex-wrap: wrap;
}

.ctx-source-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 11px;
  color: #fff;
}

.ctx-source-badge.fireflies { background: var(--ctx-fireflies); }
.ctx-source-badge.gmail { background: var(--ctx-gmail); }
.ctx-source-badge.slack { background: var(--ctx-slack); }
.ctx-source-badge.calendar { background: var(--ctx-calendar); }

.ctx-timeline-title {
  font-weight: 600;
  font-size: 14px;
  color: var(--ctx-text);
}

.ctx-timeline-participants {
  font-size: 12px;
  color: var(--ctx-muted);
}

.ctx-timeline-snippet {
  font-size: 13px;
  color: var(--ctx-text);
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.ctx-empty {
  padding: 36px;
  text-align: center;
  color: var(--ctx-muted);
  border: 1px dashed var(--ctx-border-strong);
  border-radius: var(--ctx-radius);
  background: var(--ctx-surface);
}

.ctx-timeline-truncated {
  padding: 10px 14px;
  margin-bottom: 12px;
  font-size: 12.5px;
  color: var(--ctx-muted);
  border: 1px solid var(--ctx-border);
  border-radius: var(--ctx-radius);
  background: var(--ctx-surface);
}

/* ---------- Connections ---------- */
.ctx-connection-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 14px;
}

.ctx-conn-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  border: 1px solid var(--ctx-border);
  border-radius: var(--ctx-radius);
  padding: 16px;
  background: var(--ctx-surface);
}

.ctx-conn-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.ctx-conn-name {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 15px;
}

.ctx-conn-name > span {
  display: inline-flex;
  flex-direction: column;
  line-height: 1.15;
}

.ctx-conn-sub {
  display: block;
  font-size: 11px;
  font-weight: 400;
  color: var(--ctx-text-muted);
  margin-top: 2px;
}

.ctx-conn-name i {
  width: 18px;
  height: 18px;
  color: var(--ctx-primary);
}

.ctx-conn-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 500;
  padding: 2px 10px;
  border-radius: 999px;
  background: #fee2e2;
  color: var(--ctx-error);
}

.ctx-conn-status.connected {
  background: #dcfce7;
  color: var(--ctx-success);
}

.ctx-conn-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

.ctx-conn-meta {
  font-size: 12px;
  color: var(--ctx-muted);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.ctx-conn-actions {
  display: flex;
  gap: 8px;
}

.ctx-conn-actions button {
  flex: 1;
}

.ctx-sync-summary {
  margin-top: 18px;
  font-size: 13px;
  color: var(--ctx-muted);
}

.ctx-sync-summary code {
  background: var(--ctx-surface-alt);
  padding: 1px 6px;
  border-radius: 4px;
}

/* ---------- Modals ---------- */
.ctx-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.ctx-modal-card {
  background: var(--ctx-surface);
  border-radius: var(--ctx-radius);
  max-width: 480px;
  width: 100%;
  box-shadow: var(--ctx-shadow-lg);
  display: flex;
  flex-direction: column;
}

.ctx-modal-wide {
  max-width: 760px;
  max-height: 80vh;
}

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

.ctx-modal-header h3 {
  margin: 0;
  font-size: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.ctx-modal-close {
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--ctx-muted);
  padding: 4px;
}

.ctx-modal-close:hover { color: var(--ctx-text); }

.ctx-modal-body {
  padding: 20px;
  overflow-y: auto;
}

.ctx-modal-body label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  margin: 14px 0 6px;
  color: var(--ctx-muted);
}

.ctx-modal-body input[type=password],
.ctx-modal-body input[type=text] {
  width: 100%;
  border: 1px solid var(--ctx-border-strong);
  border-radius: var(--ctx-radius-sm);
  padding: 10px 12px;
  font-size: 13px;
}

.ctx-modal-error {
  color: var(--ctx-error);
  font-size: 12px;
  margin-top: 8px;
}

.ctx-modal-link a {
  color: var(--ctx-primary);
  text-decoration: none;
}

.ctx-modal-link a:hover { text-decoration: underline; }

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

.ctx-settings-section {
  margin-bottom: 18px;
}

.ctx-settings-section h4 {
  margin: 0 0 6px;
  font-size: 13px;
  font-weight: 600;
}

.ctx-settings-section pre {
  background: var(--ctx-surface-alt);
  padding: 12px;
  border-radius: var(--ctx-radius-sm);
  font-size: 12px;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
  border: 1px solid var(--ctx-border);
  max-height: 280px;
  overflow-y: auto;
}

/* ---------- Progress modal ---------- */
.ctx-progress-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1100;
}

.ctx-progress-card {
  background: var(--ctx-surface);
  border-radius: var(--ctx-radius);
  padding: 20px 24px;
  display: flex;
  align-items: center;
  gap: 14px;
  box-shadow: var(--ctx-shadow-lg);
  min-width: 280px;
}

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

.ctx-progress-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

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

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .ctx-chat-layout {
    grid-template-columns: 1fr;
  }
  .ctx-conv-sidebar {
    max-height: 200px;
  }
  .ctx-filter-bar {
    grid-template-columns: 1fr 1fr;
  }
}
