/* ===== CRM System - Custom Styles ===== */

:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-light: #eff6ff;
  --primary-lighter: #dbeafe;
  --success: #16a34a;
  --success-light: #f0fdf4;
  --warning: #d97706;
  --warning-light: #fffbeb;
  --danger: #dc2626;
  --danger-light: #fef2f2;
  --purple: #9333ea;
  --purple-light: #faf5ff;
  --text: #1e293b;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --border: #e2e8f0;
  --border-light: #f1f5f9;
  --bg: #f8fafc;
  --bg-white: #ffffff;
  --shadow-sm: 0 1px 2px 0 rgba(0,0,0,0.05);
  --shadow: 0 1px 3px 0 rgba(0,0,0,0.1), 0 1px 2px 0 rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1), 0 2px 4px -1px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -2px rgba(0,0,0,0.05);
  --radius: 10px;
  --radius-sm: 6px;
  --radius-lg: 14px;
  --sidebar-width: 240px;
  --topbar-height: 60px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ===== Layout ===== */
.app-layout { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-width);
  background: linear-gradient(180deg, #1e293b 0%, #0f172a 100%);
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
  display: flex;
  flex-direction: column;
  transition: width 0.3s ease;
}

.sidebar-brand {
  padding: 16px 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}

.sidebar-brand .logo {
  width: 36px; height: 36px;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 18px; font-weight: 700;
}

.sidebar-brand .brand-text { color: #fff; }
.sidebar-brand .brand-name { font-size: 16px; font-weight: 700; line-height: 1.2; }
.sidebar-brand .brand-sub { font-size: 11px; color: rgba(255,255,255,0.5); }

.nav-menu { flex: 1; padding: 12px 10px; overflow-y: auto; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border-radius: 8px;
  color: rgba(255,255,255,0.6);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.2s;
  margin-bottom: 2px;
  position: relative;
}

.nav-item:hover { color: #fff; background: rgba(255,255,255,0.06); }

.nav-item.active {
  color: #fff;
  background: linear-gradient(90deg, rgba(59,130,246,0.2), rgba(139,92,246,0.1));
}

.nav-item.active::before {
  content: '';
  position: absolute;
  left: -10px; top: 50%; transform: translateY(-50%);
  width: 3px; height: 20px;
  background: #3b82f6;
  border-radius: 0 3px 3px 0;
}

.nav-item i { width: 18px; text-align: center; font-size: 15px; }

.nav-item .badge {
  margin-left: auto;
  background: #ef4444;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 10px;
  min-width: 18px;
  text-align: center;
}

.sidebar-footer {
  padding: 12px 10px;
  border-top: 1px solid rgba(255,255,255,0.08);
}

.sidebar-user {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s;
}

.sidebar-user:hover { background: rgba(255,255,255,0.06); }

.sidebar-user .avatar {
  width: 32px; height: 32px;
  border-radius: 8px;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 12px; font-weight: 600;
}

.sidebar-user .user-info { color: #fff; }
.sidebar-user .user-name { font-size: 13px; font-weight: 600; }
.sidebar-user .user-role { font-size: 11px; color: rgba(255,255,255,0.5); }

/* Main content */
.main-area {
  margin-left: var(--sidebar-width);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  height: var(--topbar-height);
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 24px;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 50;
}

.topbar-search {
  flex: 1;
  max-width: 400px;
  position: relative;
}

.topbar-search input {
  width: 100%;
  height: 36px;
  padding: 0 12px 0 36px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  background: var(--bg);
  outline: none;
  transition: all 0.2s;
}

.topbar-search input:focus {
  border-color: var(--primary);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}

.topbar-search i {
  position: absolute;
  left: 12px; top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 14px;
}

.topbar-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }

.topbar-btn {
  width: 36px; height: 36px;
  border-radius: 8px;
  border: none;
  background: transparent;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary);
  font-size: 16px;
  transition: all 0.2s;
  position: relative;
}

.topbar-btn:hover { background: var(--bg); color: var(--text); }

.topbar-btn .dot {
  position: absolute;
  top: 6px; right: 6px;
  width: 8px; height: 8px;
  background: #ef4444;
  border-radius: 50%;
  border: 2px solid #fff;
}

.content-area { flex: 1; padding: 24px; }

/* ===== Page Header ===== */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.page-title { font-size: 22px; font-weight: 700; color: var(--text); }
.page-subtitle { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }

/* ===== Cards ===== */
.card {
  background: #fff;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.2s;
}

.card:hover { box-shadow: var(--shadow); }

.card-body { padding: 20px; }

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

.card-title { font-size: 15px; font-weight: 600; color: var(--text); }

/* ===== KPI Cards ===== */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.kpi-card {
  background: #fff;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 20px;
  position: relative;
  overflow: hidden;
  transition: all 0.2s;
}

.kpi-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }

.kpi-card .kpi-icon {
  width: 44px; height: 44px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
  margin-bottom: 12px;
}

.kpi-card .kpi-label { font-size: 13px; color: var(--text-secondary); margin-bottom: 4px; }
.kpi-card .kpi-value { font-size: 28px; font-weight: 700; color: var(--text); line-height: 1.2; }
.kpi-card .kpi-change { font-size: 12px; margin-top: 6px; display: flex; align-items: center; gap: 4px; }
.kpi-change.up { color: var(--success); }
.kpi-change.down { color: var(--danger); }

/* ===== Badge / Tags ===== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.5;
}

.badge-blue { background: var(--primary-light); color: var(--primary); }
.badge-green { background: var(--success-light); color: var(--success); }
.badge-amber { background: var(--warning-light); color: var(--warning); }
.badge-red { background: var(--danger-light); color: var(--danger); }
.badge-purple { background: var(--purple-light); color: var(--purple); }
.badge-gray { background: #f3f4f6; color: #6b7280; }

/* Grade badges */
.grade-S { background: #fef2f2; color: #991b1b; font-weight: 700; }
.grade-A { background: #fffbeb; color: #92400e; font-weight: 700; }
.grade-B { background: #eff6ff; color: #1e40af; font-weight: 700; }
.grade-C { background: #f0fdf4; color: #166534; font-weight: 700; }
.grade-D { background: #f9fafb; color: #6b7280; font-weight: 700; }

/* ===== Tables ===== */
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.data-table th {
  text-align: left;
  padding: 10px 12px;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  background: var(--bg);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.data-table td {
  padding: 12px;
  border-bottom: 1px solid var(--border-light);
  vertical-align: middle;
}

.data-table tbody tr { transition: background 0.15s; }
.data-table tbody tr:hover { background: var(--primary-light); }

/* ===== Avatar ===== */
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 600;
  color: #fff;
  flex-shrink: 0;
}

.avatar-sm { width: 24px; height: 24px; font-size: 9px; }
.avatar-md { width: 32px; height: 32px; font-size: 11px; }
.avatar-lg { width: 40px; height: 40px; font-size: 14px; }

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 8px;
  border: none;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}

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

.btn-secondary { background: var(--bg); color: var(--text-secondary); border: 1px solid var(--border); }
.btn-secondary:hover { background: var(--border-light); color: var(--text); }

.btn-success { background: var(--success); color: #fff; }
.btn-success:hover { background: #15803d; }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #b91c1c; }

.btn-ghost { background: transparent; color: var(--text-secondary); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }

.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-lg { padding: 10px 20px; font-size: 15px; }

/* ===== Filters ===== */
.filter-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.filter-select, .filter-input {
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  background: #fff;
  outline: none;
  cursor: pointer;
}

.filter-select:focus, .filter-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}

/* ===== Pipeline / Kanban ===== */
.kanban-board {
  display: flex;
  gap: 16px;
  overflow-x: auto;
  padding-bottom: 16px;
}

.kanban-column {
  min-width: 280px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.kanban-column-header {
  padding: 10px 14px;
  border-radius: 8px 8px 0 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
}

.kanban-column-body {
  background: var(--bg);
  border-radius: 0 0 8px 8px;
  padding: 10px;
  flex: 1;
  min-height: 200px;
}

.deal-card {
  background: #fff;
  border-radius: 8px;
  border: 1px solid var(--border);
  padding: 14px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: all 0.2s;
}

.deal-card:hover { border-color: var(--primary); box-shadow: var(--shadow); transform: translateY(-1px); }

.deal-card .deal-name { font-size: 13px; font-weight: 600; margin-bottom: 6px; line-height: 1.4; }
.deal-card .deal-amount { font-size: 16px; font-weight: 700; color: var(--primary); margin-bottom: 8px; }
.deal-card .deal-meta { font-size: 11px; color: var(--text-muted); display: flex; flex-wrap: wrap; gap: 6px; }

/* ===== Funnel ===== */
.funnel-container { display: flex; flex-direction: column; gap: 4px; }

.funnel-stage {
  display: flex;
  align-items: center;
  padding: 16px 20px;
  color: #fff;
  border-radius: 6px;
  position: relative;
  transition: transform 0.2s;
}

.funnel-stage:hover { transform: scale(1.01); }

.funnel-stage .stage-label { font-size: 14px; font-weight: 600; min-width: 80px; }
.funnel-stage .stage-count { font-size: 20px; font-weight: 700; margin-left: 16px; }
.funnel-stage .stage-amount { font-size: 13px; margin-left: auto; opacity: 0.9; }

/* ===== Progress bars ===== */
.progress {
  height: 6px;
  background: var(--border-light);
  border-radius: 3px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  border-radius: 3px;
  transition: width 0.3s ease;
}

/* ===== Score circle ===== */
.score-circle {
  width: 36px; height: 36px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700;
  position: relative;
}

/* ===== Timeline ===== */
.timeline { position: relative; padding-left: 28px; }

.timeline::before {
  content: '';
  position: absolute;
  left: 10px; top: 0; bottom: 0;
  width: 2px;
  background: var(--border);
}

.timeline-item { position: relative; padding-bottom: 20px; }

.timeline-item::before {
  content: '';
  position: absolute;
  left: -22px; top: 4px;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--primary);
  z-index: 1;
}

.timeline-item .timeline-icon {
  position: absolute;
  left: -26px; top: 0;
  width: 20px; height: 20px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 9px;
  color: #fff;
  z-index: 2;
}

.timeline-item .timeline-date { font-size: 11px; color: var(--text-muted); margin-bottom: 2px; }
.timeline-item .timeline-title { font-size: 13px; font-weight: 600; margin-bottom: 2px; }
.timeline-item .timeline-detail { font-size: 12px; color: var(--text-secondary); }

/* ===== Email list ===== */
.email-list-item {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-light);
  cursor: pointer;
  transition: background 0.15s;
  display: flex;
  gap: 12px;
}

.email-list-item:hover { background: var(--primary-light); }
.email-list-item.unread { background: #fff; border-left: 3px solid var(--primary); }
.email-list-item.active { background: var(--primary-light); }

.email-list-item .email-from { font-size: 13px; font-weight: 600; }
.email-list-item .email-subject { font-size: 13px; font-weight: 500; color: var(--text); }
.email-list-item .email-preview { font-size: 12px; color: var(--text-muted); }
.email-list-item .email-time { font-size: 11px; color: var(--text-muted); white-space: nowrap; }

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal {
  background: #fff;
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 640px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
}

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

.modal-title { font-size: 17px; font-weight: 700; }

.modal-body { padding: 24px; }

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

/* ===== Form ===== */
.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: 13px; font-weight: 600; color: var(--text-secondary); margin-bottom: 6px; }
.form-input, .form-select, .form-textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  outline: none;
  transition: all 0.2s;
  background: #fff;
}

.form-input:focus, .form-select:focus, .form-textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}

.form-textarea { resize: vertical; min-height: 80px; }

/* ===== Tabs ===== */
.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--border); margin-bottom: 20px; }

.tab {
  padding: 10px 18px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.2s;
}

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

/* ===== Grid utilities ===== */
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }

/* ===== Misc ===== */
.text-primary { color: var(--primary); }
.text-success { color: var(--success); }
.text-danger { color: var(--danger); }
.text-warning { color: var(--warning); }
.text-muted { color: var(--text-muted); }
.text-secondary { color: var(--text-secondary); }
.font-bold { font-weight: 700; }
.font-semibold { font-weight: 600; }
.text-sm { font-size: 12px; }
.text-xs { font-size: 11px; }
.text-lg { font-size: 16px; }
.text-xl { font-size: 20px; }
.text-2xl { font-size: 24px; }
.text-3xl { font-size: 28px; }
.mb-0 { margin-bottom: 0; }
.mb-2 { margin-bottom: 8px; }
.mb-3 { margin-bottom: 12px; }
.mb-4 { margin-bottom: 16px; }
.mb-6 { margin-bottom: 24px; }
.mt-2 { margin-top: 8px; }
.mt-4 { margin-top: 16px; }
.mt-6 { margin-top: 24px; }
.flex { display: flex; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.gap-2 { gap: 8px; }
.gap-3 { gap: 12px; }
.gap-4 { gap: 16px; }
.flex-wrap { flex-wrap: wrap; }
.flex-1 { flex: 1; }
.rounded { border-radius: var(--radius); }
.rounded-full { border-radius: 50%; }
.cursor-pointer { cursor: pointer; }
.w-full { width: 100%; }
.text-center { text-align: center; }
.text-right { text-align: right; }
.relative { position: relative; }
.overflow-hidden { overflow: hidden; }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

/* Animations */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

.fade-in { animation: fadeIn 0.3s ease; }
.slide-up { animation: slideUp 0.3s ease; }

/* AI Alert styling */
.ai-alert {
  display: flex;
  gap: 12px;
  padding: 14px 16px;
  border-radius: 10px;
  border: 1px solid;
  margin-bottom: 10px;
}

.ai-alert.high { border-color: #fca5a5; background: #fef2f2; }
.ai-alert.medium { border-color: #fcd34d; background: #fffbeb; }
.ai-alert.low { border-color: #93c5fd; background: #eff6ff; }

.ai-alert .alert-icon { width: 36px; height: 36px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 16px; flex-shrink: 0; }
.ai-alert.high .alert-icon { background: #fee2e2; color: #991b1b; }
.ai-alert.medium .alert-icon { background: #fef3c7; color: #92400e; }
.ai-alert.low .alert-icon { background: #dbeafe; color: #1e40af; }

/* Source bar chart */
.source-bar { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.source-bar .source-name { width: 120px; font-size: 12px; font-weight: 500; }
.source-bar .source-track { flex: 1; height: 24px; background: var(--border-light); border-radius: 4px; overflow: hidden; position: relative; }
.source-bar .source-fill { height: 100%; border-radius: 4px; display: flex; align-items: center; padding: 0 8px; color: #fff; font-size: 11px; font-weight: 600; transition: width 0.5s ease; }
.source-bar .source-value { width: 60px; text-align: right; font-size: 12px; font-weight: 600; }

/* Org tree */
.org-node {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  padding: 12px 20px;
  border-radius: 10px;
  border: 2px solid var(--border);
  background: #fff;
  min-width: 120px;
}

.org-node .node-label { font-size: 11px; color: var(--text-muted); margin-bottom: 4px; }
.org-node .node-name { font-size: 14px; font-weight: 600; }
.org-node .node-users { font-size: 11px; color: var(--text-secondary); margin-top: 4px; }

/* Editable org node */
.org-node-editable { position: relative; transition: box-shadow 0.2s; }
.org-node-editable:hover { box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
.org-node-actions {
  display: flex;
  gap: 2px;
  margin-top: 6px;
  opacity: 0;
  transition: opacity 0.2s;
}
.org-node-editable:hover .org-node-actions { opacity: 1; }
.btn-xs {
  padding: 2px 6px;
  font-size: 11px;
  border: none;
  background: transparent;
  cursor: pointer;
  border-radius: 4px;
}
.btn-xs:hover { background: rgba(0,0,0,0.08); }

/* Toggle Switch */
.toggle-switch {
  width: 44px;
  height: 24px;
  border-radius: 12px;
  background: var(--border);
  border: none;
  cursor: pointer;
  position: relative;
  transition: background 0.3s;
  flex-shrink: 0;
  padding: 0;
}
.toggle-switch.on {
  background: var(--success, #16a34a);
}
.toggle-knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.3s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.toggle-switch.on .toggle-knob {
  transform: translateX(20px);
}
