/* ---------- Design tokens ---------- */
:root {
  color-scheme: light dark;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 48px;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-full: 999px;

  --dur-fast: 150ms;
  --dur-base: 220ms;
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);

  /* Light theme semantic tokens */
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-raised: #ffffff;
  --border: #e4e6ea;
  --text-primary: #14161a;
  --text-secondary: #6b7280;
  --text-tertiary: #9aa0ab;
  --accent: #2563eb;
  --accent-contrast: #ffffff;
  --accent-soft: #eaf0fe;
  --success: #15803d;
  --success-soft: #e8f6ec;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --focus-ring: #2563eb;
  --shadow-sm: 0 1px 2px rgba(20, 22, 26, 0.06);
  --shadow-md: 0 8px 24px rgba(20, 22, 26, 0.10);
  --scrim: rgba(15, 17, 21, 0.5);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101216;
    --surface: #16181d;
    --surface-raised: #1c1f26;
    --border: #272b33;
    --text-primary: #edeef1;
    --text-secondary: #9aa0ab;
    --text-tertiary: #6b7280;
    --accent: #6d8bff;
    --accent-contrast: #0b0d12;
    --accent-soft: rgba(109, 139, 255, 0.14);
    --success: #34c759;
    --success-soft: rgba(52, 199, 89, 0.14);
    --danger: #ff6b6b;
    --danger-soft: rgba(255, 107, 107, 0.14);
    --focus-ring: #6d8bff;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.4);
    --scrim: rgba(0, 0, 0, 0.6);
  }
}

:root[data-theme="dark"] {
  --bg: #101216; --surface: #16181d; --surface-raised: #1c1f26; --border: #272b33;
  --text-primary: #edeef1; --text-secondary: #9aa0ab; --text-tertiary: #6b7280;
  --accent: #6d8bff; --accent-contrast: #0b0d12; --accent-soft: rgba(109, 139, 255, 0.14);
  --success: #34c759; --success-soft: rgba(52, 199, 89, 0.14);
  --danger: #ff6b6b; --danger-soft: rgba(255, 107, 107, 0.14);
  --focus-ring: #6d8bff; --scrim: rgba(0, 0, 0, 0.6);
}
:root[data-theme="light"] {
  --bg: #f4f5f7; --surface: #ffffff; --surface-raised: #ffffff; --border: #e4e6ea;
  --text-primary: #14161a; --text-secondary: #6b7280; --text-tertiary: #9aa0ab;
  --accent: #2563eb; --accent-contrast: #ffffff; --accent-soft: #eaf0fe;
  --success: #15803d; --success-soft: #e8f6ec;
  --danger: #dc2626; --danger-soft: #fdecec;
  --focus-ring: #2563eb; --scrim: rgba(15, 17, 21, 0.5);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
}

:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

button {
  font-family: inherit;
  cursor: pointer;
  touch-action: manipulation;
}
button:disabled { cursor: not-allowed; opacity: 0.5; }

svg.icon { width: 18px; height: 18px; flex-shrink: 0; }

/* ---------- App shell ---------- */
.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: var(--space-5);
  padding: var(--space-3) var(--space-5);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.topbar .brand {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: 600;
  font-size: 15px;
  color: var(--text-primary);
  margin-right: auto;
}
.topbar .brand svg { color: var(--accent); }

.tabs {
  display: flex;
  gap: var(--space-1);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  padding: 3px;
}
.tab-btn {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-full);
  font-size: 13px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 36px;
  transition: background var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
}
.tab-btn:hover { color: var(--text-primary); }
.tab-btn.active { background: var(--surface); color: var(--accent); box-shadow: var(--shadow-sm); }
.tab-btn .count {
  background: var(--danger);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: var(--radius-full);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
}

.view { flex: 1; min-height: 0; }
.hidden { display: none !important; }

/* ---------- Conversas view ---------- */
.convos-layout {
  height: 100%;
  display: grid;
  grid-template-columns: 340px 1fr;
}
@media (max-width: 720px) {
  .convos-layout { grid-template-columns: 1fr; }
  .convos-layout.show-thread .list-pane { display: none; }
  .convos-layout:not(.show-thread) .thread-pane { display: none; }
}

.list-pane {
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.search-box {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.search-box .input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}
.search-box svg {
  position: absolute;
  left: var(--space-3);
  color: var(--text-tertiary);
}
.search-box input {
  width: 100%;
  padding: var(--space-2) var(--space-3) var(--space-2) 36px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-primary);
  font-size: 14px;
  height: 40px;
}
.search-box input::placeholder { color: var(--text-tertiary); }

.convo-list { flex: 1; overflow-y: auto; }

.convo-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
  background: transparent;
  width: 100%;
  border-left: none; border-right: none; border-top: none;
  text-align: left;
  transition: background var(--dur-fast) var(--ease-out);
}
.convo-row:hover { background: var(--bg); }
.convo-row.active { background: var(--accent-soft); }

.avatar {
  width: 40px; height: 40px;
  border-radius: var(--radius-full);
  background: var(--accent);
  color: var(--accent-contrast);
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 14px;
  flex-shrink: 0;
}

.convo-meta { flex: 1; min-width: 0; }
.convo-meta .row-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--space-2);
}
.convo-meta .name {
  font-weight: 600;
  font-size: 14px;
  color: var(--text-primary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.convo-meta .time { font-size: 11px; color: var(--text-tertiary); flex-shrink: 0; }
.convo-meta .preview {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-top: 2px;
}

.unread-badge {
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 11px;
  font-weight: 700;
  min-width: 20px; height: 20px;
  border-radius: var(--radius-full);
  display: inline-flex; align-items: center; justify-content: center;
  padding: 0 6px;
  flex-shrink: 0;
}

.thread-pane { display: flex; flex-direction: column; min-height: 0; }

.thread-header {
  display: none;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}
@media (max-width: 720px) {
  .thread-header { display: flex; }
}
.back-btn {
  border: none;
  background: transparent;
  color: var(--text-primary);
  padding: var(--space-2);
  border-radius: var(--radius-md);
  display: flex;
}
.back-btn:hover { background: var(--bg); }

.thread-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.bubble {
  max-width: min(560px, 78%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-3) var(--space-4);
  position: relative;
  box-shadow: var(--shadow-sm);
}
.bubble.auto { border-color: var(--accent); background: var(--accent-soft); }
.bubble .bubble-flags {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  margin-bottom: var(--space-1);
}
.bubble .flag-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 700;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.bubble .text { font-size: 14px; color: var(--text-primary); word-break: break-word; }
.media-preview {
  display: block;
  max-width: 100%;
  border-radius: var(--radius-md);
  margin-bottom: var(--space-2);
}
img.media-preview { max-height: 320px; object-fit: cover; }
audio.media-preview { width: 100%; height: 40px; }
a.doc-link {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
}
.bubble .foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  margin-top: var(--space-2);
}
.bubble .time { font-size: 11px; color: var(--text-tertiary); }
.bubble .task-btn {
  border: none;
  background: transparent;
  color: var(--text-tertiary);
  padding: 4px;
  border-radius: var(--radius-sm);
  display: flex;
  opacity: 0.7;
}
.bubble .task-btn:hover { opacity: 1; color: var(--accent); background: var(--accent-soft); }

/* ---------- Agenda view ---------- */
.agenda-layout {
  height: 100%;
  overflow-y: auto;
  padding: var(--space-5);
  max-width: 760px;
  margin: 0 auto;
}

.new-task-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  margin-bottom: var(--space-5);
  box-shadow: var(--shadow-sm);
}
.new-task-row {
  display: flex;
  gap: var(--space-2);
  flex-wrap: wrap;
}
.new-task-row input[type="text"] {
  flex: 1 1 220px;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-primary);
  font-size: 14px;
  height: 40px;
}
.new-task-row input[type="date"] {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-primary);
  font-size: 14px;
  height: 40px;
}
.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
  border: none;
  border-radius: var(--radius-md);
  padding: 0 var(--space-4);
  height: 40px;
  font-size: 14px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  transition: transform var(--dur-fast) var(--ease-out), box-shadow var(--dur-fast) var(--ease-out);
}
.btn-primary:hover { box-shadow: var(--shadow-md); }
.btn-primary:active { transform: scale(0.97); }

.section-label {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: var(--space-5) 0 var(--space-2);
}
.section-label:first-child { margin-top: 0; }

.task-card {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-2);
  transition: background var(--dur-fast) var(--ease-out);
}
.task-card.overdue { border-color: var(--danger); }
.task-card.done { opacity: 0.55; }
.task-card.done .task-text { text-decoration: line-through; }

.checkbox {
  width: 20px; height: 20px;
  border-radius: 6px;
  border: 2px solid var(--border);
  background: var(--surface);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  margin-top: 1px;
  transition: background var(--dur-fast) var(--ease-out), border-color var(--dur-fast) var(--ease-out);
}
.checkbox.checked { background: var(--success); border-color: var(--success); color: #fff; }

.task-body { flex: 1; min-width: 0; }
.task-text { font-size: 14px; color: var(--text-primary); }
.task-sub {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-1);
  font-size: 12px;
  color: var(--text-secondary);
  flex-wrap: wrap;
}
.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  font-weight: 600;
  font-size: 11px;
}
.pill.auto { background: var(--accent-soft); color: var(--accent); }
.pill.due { background: var(--bg); color: var(--text-secondary); }
.pill.due.overdue { background: var(--danger-soft); color: var(--danger); }
.pill.kind-tarefa { background: var(--bg); color: var(--text-secondary); }
.pill.kind-evento { background: var(--success-soft); color: var(--success); }
.pill.kind-convite { background: var(--accent-soft); color: var(--accent); }

.icon-btn {
  border: none;
  background: transparent;
  color: var(--text-tertiary);
  padding: var(--space-2);
  border-radius: var(--radius-md);
  display: flex;
  flex-shrink: 0;
}
.icon-btn:hover { color: var(--danger); background: var(--danger-soft); }

/* ---------- CRM ---------- */
.crm-open-btn { margin-left: auto; }
@media (max-width: 720px) { .thread-header .crm-open-btn { display: flex; } }
.thread-header strong { flex: 1; }

.crm-board {
  height: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  display: flex;
  gap: var(--space-4);
  padding: var(--space-5);
}
.crm-column {
  flex: 0 0 280px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.crm-column-head {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
  font-weight: 700;
  font-size: 13px;
  color: var(--text-primary);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}
.crm-column-head .count { background: var(--bg); color: var(--text-secondary); font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: var(--radius-full); }
.crm-column-body { flex: 1; overflow-y: auto; padding: var(--space-3); display: flex; flex-direction: column; gap: var(--space-2); }

.crm-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-3);
  text-align: left;
  width: 100%;
  border-left-width: 1px;
}
.crm-card:hover { border-color: var(--accent); }
.crm-card .name { font-weight: 600; font-size: 13px; color: var(--text-primary); }
.crm-card .preview { font-size: 12px; color: var(--text-secondary); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.crm-card .meta-row { display: flex; align-items: center; gap: var(--space-2); margin-top: var(--space-2); flex-wrap: wrap; }
.crm-card .tag { font-size: 10px; font-weight: 600; background: var(--accent-soft); color: var(--accent); padding: 2px 7px; border-radius: var(--radius-full); }
.crm-card .deal { font-size: 11px; font-weight: 700; color: var(--success); }

.stage-pills { display: flex; gap: var(--space-2); flex-wrap: wrap; }
.stage-pill {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-secondary);
  border-radius: var(--radius-full);
  padding: 6px 14px;
  font-size: 12px;
  font-weight: 600;
}
.stage-pill.active { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }

.note-add-row { display: flex; gap: var(--space-2); }
.note-add-row input { flex: 1; }
.note-add-row button { flex-shrink: 0; padding: 0 var(--space-3); }

.notes-list { margin-top: var(--space-3); display: flex; flex-direction: column; gap: var(--space-2); max-height: 180px; overflow-y: auto; }
.note-item {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: var(--space-2) var(--space-3);
  font-size: 13px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: var(--space-2);
}
.note-item .note-text { flex: 1; }
.note-item .note-time { font-size: 10px; color: var(--text-tertiary); display: block; margin-top: 2px; }
.crm-modal { max-width: 460px; }

/* ---------- Empty states ---------- */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  text-align: center;
  padding: var(--space-7) var(--space-5);
  color: var(--text-secondary);
  height: 100%;
}
.empty-state svg { color: var(--text-tertiary); width: 40px; height: 40px; }
.empty-state h3 { margin: 0; font-size: 15px; color: var(--text-primary); font-weight: 600; }
.empty-state p { margin: 0; font-size: 13px; max-width: 320px; }

/* ---------- Modal ---------- */
.modal-scrim {
  position: fixed; inset: 0;
  background: var(--scrim);
  display: flex; align-items: center; justify-content: center;
  padding: var(--space-4);
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur-base) var(--ease-out);
}
.modal-scrim.open { opacity: 1; pointer-events: auto; }

.modal {
  background: var(--surface-raised);
  border-radius: var(--radius-lg);
  padding: var(--space-5);
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow-md);
  transform: translateY(8px) scale(0.98);
  opacity: 0;
  transition: transform var(--dur-base) var(--ease-out), opacity var(--dur-base) var(--ease-out);
}
.modal-scrim.open .modal { transform: translateY(0) scale(1); opacity: 1; }

.modal h2 { margin: 0 0 var(--space-4); font-size: 16px; font-weight: 700; }
.modal label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: var(--space-1);
  margin-top: var(--space-3);
}
.modal label:first-of-type { margin-top: 0; }
.modal textarea, .modal input {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  resize: vertical;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  margin-top: var(--space-5);
}
.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-primary);
  border-radius: var(--radius-md);
  padding: 0 var(--space-4);
  height: 40px;
  font-size: 14px;
  font-weight: 600;
}
.btn-ghost:hover { background: var(--bg); }

/* ---------- Toast ---------- */
.toast-stack {
  position: fixed;
  bottom: var(--space-5);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  z-index: 200;
  width: min(360px, calc(100% - 32px));
}
.toast {
  background: var(--surface-raised);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
  border-radius: var(--radius-md);
  padding: var(--space-3) var(--space-4);
  font-size: 13px;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: var(--space-2);
  animation: toast-in var(--dur-base) var(--ease-out);
}
.toast svg { color: var(--success); flex-shrink: 0; }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

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