:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #fafbfd;
  --border: #e6e8ef;
  --border-strong: #d1d5db;
  --text: #0f172a;
  --text-muted: #64748b;
  --text-soft: #94a3b8;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --primary-soft: #eef2ff;
  --success: #10b981;
  --success-soft: #d1fae5;
  --warning: #f59e0b;
  --warning-soft: #fef3c7;
  --danger: #ef4444;
  --danger-soft: #fee2e2;
  --info: #0ea5e9;
  --info-soft: #e0f2fe;
  --shadow-sm: 0 1px 2px rgba(15,23,42,0.04);
  --shadow: 0 1px 3px rgba(15,23,42,0.06), 0 1px 2px rgba(15,23,42,0.04);
  --shadow-lg: 0 10px 25px rgba(15,23,42,0.10);
  --radius: 10px;
  --radius-sm: 6px;
  --sidebar-w: 248px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; font-size: 14px; }
a { color: inherit; text-decoration: none; }

.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.error { color: var(--danger); font-size: 13px; margin-top: 8px; }

/* ============ AUTH ============ */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #eef2ff 0%, #f6f7fb 50%, #ede9fe 100%);
  padding: 24px;
}
.auth-card {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  padding: 36px;
  width: 100%;
  max-width: 400px;
}
.auth-card .brand {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 28px;
  color: var(--primary);
}
.auth-card h1 { margin: 0; font-size: 22px; color: var(--text); }
.auth-card .brand p { margin: 0; font-size: 13px; }
.auth-card form { display: flex; flex-direction: column; gap: 14px; }
.auth-card label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
}
.auth-card input {
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--surface-2);
  color: var(--text);
  outline: none;
  transition: border 0.15s, box-shadow 0.15s;
}
.auth-card input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.auth-foot { text-align: center; margin-top: 18px; font-size: 12px; }

/* ============ BUTTONS ============ */
.btn-primary {
  background: var(--primary);
  color: white;
  border: none;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 14px;
  transition: background 0.15s;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-primary.btn-small { padding: 8px 14px; font-size: 13px; }
.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 13px;
}
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-ghost.btn-small { padding: 6px 10px; font-size: 12px; }
.btn-danger {
  background: var(--danger);
  color: white;
  border: none;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 13px;
}
.btn-danger:hover { background: #dc2626; }
.btn-icon {
  background: transparent;
  border: none;
  padding: 8px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  display: none;
}
.btn-icon:hover { background: var(--surface-2); }

/* ============ LAYOUT ============ */
#app-view { display: flex; min-height: 100vh; }
.sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 50;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px 22px;
  font-weight: 700;
  font-size: 17px;
  color: var(--primary);
  border-bottom: 1px solid var(--border);
}
.nav { flex: 1; padding: 16px 12px; display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
.nav-link {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-weight: 500;
  font-size: 14px;
  transition: background 0.12s, color 0.12s;
}
.nav-link:hover { background: var(--surface-2); color: var(--text); }
.nav-link.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.sidebar-foot { padding: 14px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 10px; }
.user-chip { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--primary); color: white;
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 14px;
}
.user-meta { flex: 1; min-width: 0; }
.user-meta > div:first-child { font-weight: 600; font-size: 13px; }
.user-meta .small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.main {
  margin-left: var(--sidebar-w);
  flex: 1;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 28px;
  display: flex;
  align-items: center;
  gap: 16px;
  position: sticky; top: 0; z-index: 20;
}
.topbar h2 { margin: 0; font-size: 18px; flex: 1; }
.topbar-actions { display: flex; gap: 10px; }
.view { padding: 28px; max-width: 1400px; width: 100%; }

/* ============ CARDS / KPIs ============ */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
}
.kpi-label { color: var(--text-muted); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.kpi-value { font-size: 26px; font-weight: 700; margin-top: 6px; color: var(--text); }
.kpi-sub { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.kpi-icon-wrap {
  width: 38px; height: 38px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  background: var(--primary-soft); color: var(--primary);
  float: right;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  margin-bottom: 18px;
}
.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.card-header h3 { margin: 0; font-size: 15px; }
.card-body { padding: 18px 20px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.grid-3 { display: grid; grid-template-columns: 2fr 1fr; gap: 18px; }
@media (max-width: 1000px) { .grid-2, .grid-3 { grid-template-columns: 1fr; } }

.chart-wrap { position: relative; height: 280px; }

/* ============ TABLES ============ */
.toolbar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 16px;
  align-items: center;
}
.search-input {
  flex: 1;
  min-width: 220px;
  padding: 9px 12px 9px 36px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><circle cx='11' cy='11' r='8'/><line x1='21' y1='21' x2='16.65' y2='16.65'/></svg>") no-repeat 12px center;
  outline: none;
}
.search-input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.filter-select {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  outline: none;
}

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}
table { width: 100%; border-collapse: collapse; }
thead th {
  text-align: left;
  padding: 12px 16px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
tbody td {
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr { transition: background 0.1s; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.clickable { cursor: pointer; }

.row-name { font-weight: 600; color: var(--text); }
.row-sub { font-size: 12px; color: var(--text-muted); }

.actions-cell { display: flex; gap: 6px; justify-content: flex-end; }
.icon-btn {
  background: transparent;
  border: none;
  padding: 6px;
  border-radius: 6px;
  color: var(--text-muted);
  display: inline-flex;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }

/* ============ BADGES ============ */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.badge-lead { background: var(--info-soft); color: var(--info); }
.badge-prospect { background: var(--warning-soft); color: var(--warning); }
.badge-client { background: var(--success-soft); color: var(--success); }
.badge-new { background: #e0e7ff; color: #4f46e5; }
.badge-qualified { background: var(--info-soft); color: var(--info); }
.badge-proposal { background: var(--warning-soft); color: var(--warning); }
.badge-negotiation { background: #fce7f3; color: #db2777; }
.badge-won { background: var(--success-soft); color: var(--success); }
.badge-lost { background: var(--danger-soft); color: var(--danger); }

/* ============ MODAL ============ */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(15,23,42,0.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 20px;
  animation: fadeIn 0.15s ease-out;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal {
  background: var(--surface);
  border-radius: 14px;
  max-width: 560px;
  width: 100%;
  max-height: 90vh;
  overflow: auto;
  box-shadow: var(--shadow-lg);
}
.modal.modal-lg { max-width: 720px; }
.modal-header {
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
}
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 22px; }
.modal-footer {
  padding: 14px 22px;
  border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 10px;
}
.modal-close {
  background: transparent; border: none; color: var(--text-muted);
  padding: 4px; border-radius: 6px;
}
.modal-close:hover { background: var(--surface-2); color: var(--text); }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 600px) { .form-grid { grid-template-columns: 1fr; } }
.form-grid label.full { grid-column: 1 / -1; }
.modal label {
  display: flex; flex-direction: column;
  gap: 5px; font-size: 12px; font-weight: 600; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.03em;
}
.modal input, .modal select, .modal textarea {
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  outline: none;
  text-transform: none;
  letter-spacing: normal;
  font-weight: normal;
}
.modal input:focus, .modal select:focus, .modal textarea:focus {
  border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft);
}
.modal textarea { resize: vertical; min-height: 80px; }

/* ============ TIMELINE ============ */
.timeline { display: flex; flex-direction: column; gap: 14px; }
.timeline-item {
  display: flex;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
.timeline-icon {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.timeline-icon.call { background: var(--info-soft); color: var(--info); }
.timeline-icon.email { background: var(--primary-soft); color: var(--primary); }
.timeline-icon.note { background: var(--warning-soft); color: var(--warning); }
.timeline-icon.meeting { background: var(--success-soft); color: var(--success); }
.timeline-content { flex: 1; min-width: 0; }
.timeline-content h4 { margin: 0 0 4px; font-size: 14px; }
.timeline-content p { margin: 0; color: var(--text-muted); font-size: 13px; }
.timeline-date { font-size: 11px; color: var(--text-soft); margin-top: 6px; }

/* ============ KANBAN ============ */
.kanban {
  display: grid;
  grid-template-columns: repeat(6, minmax(220px, 1fr));
  gap: 14px;
  overflow-x: auto;
  padding-bottom: 10px;
}
.kanban-col {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-height: 400px;
}
.kanban-col-header {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 13px;
  display: flex; justify-content: space-between; align-items: center;
}
.kanban-col-header .count {
  background: var(--surface); color: var(--text-muted);
  padding: 2px 8px; border-radius: 999px; font-size: 11px;
}
.kanban-col-total { font-size: 12px; color: var(--text-muted); font-weight: normal; padding: 6px 14px 0; }
.kanban-list {
  padding: 10px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 60px;
}
.kanban-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  cursor: grab;
  transition: box-shadow 0.12s, transform 0.12s;
}
.kanban-card:hover { box-shadow: var(--shadow); }
.kanban-card.sortable-ghost { opacity: 0.4; }
.kanban-card.sortable-chosen { cursor: grabbing; }
.kanban-card-name { font-weight: 600; font-size: 13px; margin-bottom: 4px; }
.kanban-card-meta { font-size: 11px; color: var(--text-muted); display: flex; justify-content: space-between; align-items: center; }
.kanban-card-amount { font-weight: 600; color: var(--primary); }

/* ============ TOAST ============ */
#toast-root {
  position: fixed; top: 20px; right: 20px;
  display: flex; flex-direction: column; gap: 8px;
  z-index: 1000;
}
.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--primary);
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  min-width: 240px;
  animation: slideIn 0.2s ease-out;
}
.toast.success { border-left-color: var(--success); }
.toast.error { border-left-color: var(--danger); }
@keyframes slideIn { from { transform: translateX(20px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ============ EMPTY ============ */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
}
.empty-state svg { color: var(--text-soft); margin-bottom: 8px; }
.empty-state p { margin: 4px 0; }

/* ============ DETAIL VIEW ============ */
.detail-grid { display: grid; grid-template-columns: 320px 1fr; gap: 18px; }
@media (max-width: 900px) { .detail-grid { grid-template-columns: 1fr; } }
.field-row { padding: 8px 0; border-bottom: 1px solid var(--border); }
.field-row:last-child { border-bottom: none; }
.field-label { font-size: 11px; text-transform: uppercase; color: var(--text-muted); letter-spacing: 0.05em; }
.field-value { margin-top: 3px; }

/* ============ RESPONSIVE ============ */
@media (max-width: 900px) {
  .sidebar { transform: translateX(-100%); transition: transform 0.2s; }
  .sidebar.open { transform: translateX(0); }
  .main { margin-left: 0; }
  .btn-icon { display: inline-flex; }
  .view { padding: 18px; }
  .topbar { padding: 12px 18px; }
}
