:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-alt: #f8f9fb;
  --border: #e3e5ea;
  --text: #1a1d23;
  --text-muted: #6b7280;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --success: #10b981;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.04);
  --sidebar-w: 220px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { margin: 0 0 .5em; font-weight: 600; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }

button, .btn {
  font-family: inherit;
  font-size: .9rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 8px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
button:hover, .btn:hover { background: var(--surface-alt); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); }
.btn-sm { padding: 4px 10px; font-size: .8rem; }
.btn-icon { padding: 6px 8px; line-height: 1; }

input, select, textarea {
  font-family: inherit;
  font-size: .9rem;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid color-mix(in srgb, var(--primary) 35%, transparent);
  border-color: var(--primary);
}
label { font-size: .82rem; color: var(--text-muted); display: block; margin-bottom: 4px; }
.field { margin-bottom: 14px; }

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar .brand {
  font-weight: 700;
  font-size: 1.05rem;
  padding: 8px 10px 16px;
}

.sidebar a.nav-link {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: var(--radius);
  color: var(--text);
  font-size: .9rem;
}
.sidebar a.nav-link:hover { background: var(--surface-alt); text-decoration: none; }
.sidebar a.nav-link.active { background: color-mix(in srgb, var(--primary) 12%, transparent); color: var(--primary); font-weight: 600; }

.sidebar .sidebar-footer {
  margin-top: auto;
  font-size: .8rem;
  color: var(--text-muted);
  padding: 10px;
  border-top: 1px solid var(--border);
}

.main {
  flex: 1;
  min-width: 0;
  padding: 20px 24px 60px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

.mobile-topbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

.table { width: 100%; border-collapse: collapse; }
.table th, .table td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-size: .88rem;
}
.table th { color: var(--text-muted); font-weight: 600; font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; }
.table tr:hover td { background: var(--surface-alt); }

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

.tag {
  display: inline-flex;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: .72rem;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.kanban-board {
  display: flex;
  gap: 14px;
  overflow-x: auto;
  padding-bottom: 20px;
  align-items: flex-start;
}

.kanban-col {
  background: var(--surface-alt);
  border-radius: var(--radius);
  width: 270px;
  min-width: 270px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 140px);
}

.kanban-col-header {
  padding: 10px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  border-bottom: 2px solid var(--border);
  font-weight: 600;
  font-size: .85rem;
}

.kanban-col-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.kanban-col-count { color: var(--text-muted); font-weight: 400; margin-left: auto; font-size: .78rem; }

.kanban-cards {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 60px;
}

.kanban-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  cursor: grab;
  box-shadow: var(--shadow);
}
.kanban-card:hover { border-color: var(--primary); }
.kanban-card .name { font-weight: 600; font-size: .88rem; margin-bottom: 2px; }
.kanban-card .meta { font-size: .76rem; color: var(--text-muted); display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.kanban-card.dragging-ghost { opacity: .4; }

.sortable-ghost { opacity: .3; }

.filters-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 16px;
  align-items: flex-end;
}
.filters-bar .field { margin-bottom: 0; min-width: 140px; }

.view-toggle { display: flex; gap: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 3px; }
.view-toggle a { padding: 6px 12px; border-radius: 7px; font-size: .85rem; color: var(--text-muted); }
.view-toggle a.active { background: var(--primary); color: #fff; }

.contact-layout { display: grid; grid-template-columns: 320px 1fr; gap: 16px; }

.chat-window {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 160px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.chat-messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.chat-msg { max-width: 72%; padding: 8px 12px; border-radius: 12px; font-size: .88rem; }
.chat-msg.in { align-self: flex-start; background: var(--surface-alt); border: 1px solid var(--border); }
.chat-msg.out { align-self: flex-end; background: var(--primary); color: #fff; }
.chat-msg .sender { font-size: .7rem; opacity: .75; margin-bottom: 3px; }
.chat-msg img { max-width: 100%; border-radius: 8px; margin-top: 4px; display: block; }
.chat-msg a.file-link { display: inline-block; margin-top: 4px; font-size: .82rem; }
.chat-input-row { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--border); }
.chat-input-row textarea { resize: none; flex: 1; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 8px 0; border-bottom: 1px dashed var(--border); font-size: .85rem; }
.timeline li:last-child { border-bottom: none; }

.custom-fields-grid { display: flex; flex-direction: column; gap: 12px; }

.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.4);
  display: none; align-items: center; justify-content: center; z-index: 200; padding: 16px;
}
.modal-overlay.open { display: flex; }
.modal { background: var(--surface); border-radius: var(--radius); padding: 20px; width: 100%; max-width: 480px; max-height: 90vh; overflow-y: auto; }

.toast-stack { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 300; }
.toast { background: var(--text); color: #fff; padding: 10px 16px; border-radius: var(--radius); font-size: .85rem; box-shadow: var(--shadow); }
.toast.error { background: var(--danger); }

.drag-handle { cursor: grab; color: var(--text-muted); }

.row { display: flex; gap: 10px; align-items: center; }
.row.between { justify-content: space-between; }
.muted { color: var(--text-muted); }
.text-sm { font-size: .82rem; }
.mt-1 { margin-top: 8px; } .mt-2 { margin-top: 16px; } .mb-1 { margin-bottom: 8px; }

.stage-list-item {
  display: flex; align-items: center; gap: 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; margin-bottom: 8px;
}
.color-dot { width: 14px; height: 14px; border-radius: 50%; flex-shrink: 0; }

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

@media (max-width: 900px) {
  .contact-layout { grid-template-columns: 1fr; }
  .chat-window { height: 60vh; }
}

@media (max-width: 760px) {
  .app-shell { flex-direction: column; }
  .sidebar { display: none; }
  .mobile-topbar { display: flex; }
  .main { padding: 14px; }
  .kanban-col { width: 85vw; min-width: 85vw; }
  .sidebar.mobile-open {
    display: flex; position: fixed; inset: 0 20% 0 0; z-index: 100; height: 100vh;
    box-shadow: 4px 0 20px rgba(0,0,0,.2);
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f26;
    --surface-alt: #22262e;
    --border: #2d313a;
    --text: #e7e9ec;
    --text-muted: #9aa1ac;
    --shadow: 0 1px 3px rgba(0,0,0,.4);
  }
}
