:root {
  --primary: #2563EB;
  --primary-dark: #1D4ED8;
  --primary-light: #EFF6FF;
  --success: #16A34A;
  --success-light: #F0FDF4;
  --warning: #D97706;
  --warning-light: #FFFBEB;
  --danger: #DC2626;
  --danger-light: #FEF2F2;
  --bg: #F1F5F9;
  --card: #FFFFFF;
  --text: #0F172A;
  --text-muted: #64748B;
  --border: #E2E8F0;
  --nav-height: 64px;
  --header-height: 56px;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px 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);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  min-height: 100vh;
  position: relative;
}

/* ── Header ── */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  height: var(--header-height);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
}
.header-back {
  width: 36px; height: 36px;
  border: none; background: none;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; border-radius: 8px; color: var(--primary);
  flex-shrink: 0;
}
.header-back svg { width: 22px; height: 22px; }
.header-title { flex: 1; font-size: 17px; font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-action {
  width: 36px; height: 36px;
  border: none; background: var(--primary); color: white;
  border-radius: 8px; display: flex; align-items: center; justify-content: center;
  cursor: pointer; font-size: 22px; font-weight: 300; line-height: 1; flex-shrink: 0;
}

/* ── Main Content ── */
.main {
  padding: 16px;
  padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom) + 16px);
}

/* ── Bottom Nav ── */
.bottom-nav {
  position: fixed; bottom: 0; left: 50%; transform: translateX(-50%);
  width: 100%; max-width: 640px;
  height: calc(var(--nav-height) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--border);
  display: flex; z-index: 100;
}
.nav-item {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 3px; border: none; background: none;
  cursor: pointer; color: var(--text-muted); font-size: 11px; padding: 8px 4px;
  transition: color 0.15s; -webkit-tap-highlight-color: transparent;
}
.nav-item.active { color: var(--primary); }
.nav-item svg { width: 22px; height: 22px; }

/* ── Cards ── */
.card {
  background: var(--card); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); margin-bottom: 12px;
}
.card-title {
  font-size: 12px; font-weight: 600; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 12px;
}

/* ── Stats Grid ── */
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.stat-card { background: var(--card); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat-label { font-size: 12px; color: var(--text-muted); font-weight: 500; margin-bottom: 4px; }
.stat-value { font-size: 20px; font-weight: 700; color: var(--text); line-height: 1.2; }
.stat-value.success { color: var(--success); }
.stat-value.warning { color: var(--warning); }
.stat-value.primary { color: var(--primary); }
.stat-value.danger { color: var(--danger); }

/* ── List Items ── */
.list-item {
  display: flex; align-items: center; padding: 12px 0;
  border-bottom: 1px solid var(--border); gap: 12px; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.list-item:last-child { border-bottom: none; }
.list-item:active { background: var(--bg); margin: 0 -16px; padding: 12px 16px; border-radius: var(--radius-sm); }
.list-item-content { flex: 1; min-width: 0; }
.list-item-title { font-size: 15px; font-weight: 500; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-item-sub { font-size: 13px; color: var(--text-muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-item-right { text-align: right; flex-shrink: 0; }

/* ── Avatar ── */
.avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--primary-light); color: var(--primary);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px; flex-shrink: 0;
}
.avatar-sm { width: 32px; height: 32px; font-size: 11px; }
.avatar-lg { width: 56px; height: 56px; font-size: 20px; }

/* ── Badges ── */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 20px;
  font-size: 12px; font-weight: 500;
}
.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-gray { background: var(--border); color: var(--text-muted); }

/* Sales Stage badges */
.stage-Prospecting   { background: #F0F9FF; color: #0369A1; }
.stage-Qualification { background: #F5F3FF; color: #6D28D9; }
.stage-Proposal      { background: #FFF7ED; color: #C2410C; }
.stage-Negotiation   { background: #FFFBEB; color: #B45309; }
.stage-Closed-Won    { background: var(--success-light); color: var(--success); }
.stage-Closed-Lost   { background: var(--danger-light); color: var(--danger); }

/* ── Search ── */
.search-box { position: relative; margin-bottom: 14px; }
.search-input {
  width: 100%; padding: 10px 16px 10px 40px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 15px; background: var(--card); outline: none; color: var(--text);
  font-family: inherit;
}
.search-input:focus { border-color: var(--primary); }
.search-icon {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  color: var(--text-muted); width: 18px; height: 18px; pointer-events: none;
}

/* ── Tabs ── */
.tabs { display: flex; background: var(--card); border-radius: var(--radius-sm); padding: 4px; margin-bottom: 14px; box-shadow: var(--shadow); }
.tab {
  flex: 1; padding: 7px 4px; text-align: center; font-size: 13px; font-weight: 500;
  border: none; background: none; border-radius: 6px; color: var(--text-muted);
  cursor: pointer; transition: all 0.15s; font-family: inherit;
}
.tab.active { background: var(--primary); color: white; }

/* ── Forms ── */
.form-group { margin-bottom: 14px; }
.form-label { display: block; font-size: 13px; font-weight: 500; color: var(--text-muted); margin-bottom: 6px; }
.form-input, .form-select, .form-textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); font-size: 15px; background: var(--card);
  color: var(--text); outline: none; font-family: inherit; appearance: none;
}
.form-input:focus, .form-select:focus, .form-textarea:focus { border-color: var(--primary); }
.form-textarea { resize: vertical; min-height: 80px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ── Buttons ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 20px; border: none; border-radius: var(--radius-sm);
  font-size: 15px; font-weight: 600; cursor: pointer; transition: all 0.15s;
  font-family: inherit; -webkit-tap-highlight-color: transparent;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-primary:active { background: var(--primary-dark); }
.btn-danger { background: var(--danger); color: white; }
.btn-ghost { background: var(--bg); color: var(--text); }
.btn-ghost:hover { background: var(--border); }
.btn-full { width: 100%; }
.btn-sm { padding: 6px 14px; font-size: 13px; }
.btn:disabled { opacity: 0.5; pointer-events: none; }

/* ── Modal (bottom sheet) ── */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 200;
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  background: var(--card); border-radius: 20px 20px 0 0; padding: 20px;
  width: 100%; max-width: 640px;
  max-height: 90vh; overflow-y: auto;
  padding-bottom: calc(20px + env(safe-area-inset-bottom));
}
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.modal-title { font-size: 17px; font-weight: 600; }
.modal-close {
  width: 32px; height: 32px; border: none; background: var(--border);
  border-radius: 50%; cursor: pointer; display: flex; align-items: center;
  justify-content: center; color: var(--text-muted); font-size: 18px; line-height: 1;
}
.modal-actions { display: flex; gap: 10px; margin-top: 20px; }
.modal-actions .btn { flex: 1; }

/* ── Empty State ── */
.empty-state { text-align: center; padding: 40px 24px; color: var(--text-muted); }
.empty-state-icon { font-size: 40px; margin-bottom: 10px; }
.empty-state-title { font-size: 16px; font-weight: 600; color: var(--text); margin-bottom: 6px; }
.empty-state-sub { font-size: 14px; }

/* ── Toast ── */
.toast {
  position: fixed; bottom: calc(var(--nav-height) + env(safe-area-inset-bottom) + 12px);
  left: 50%; transform: translateX(-50%);
  background: var(--text); color: white; padding: 10px 20px;
  border-radius: 20px; font-size: 14px; z-index: 300;
  opacity: 0; transition: opacity 0.25s; white-space: nowrap; pointer-events: none;
}
.toast.show { opacity: 1; }

/* ── Pipeline bars ── */
.pipeline-stage { margin-bottom: 12px; }
.pipeline-stage-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 5px; }
.pipeline-stage-name { font-size: 13px; font-weight: 600; }
.pipeline-stage-meta { font-size: 12px; color: var(--text-muted); }
.pipeline-bar { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
.pipeline-bar-fill { height: 100%; background: var(--primary); border-radius: 3px; transition: width 0.4s ease; }

/* ── Task Item ── */
.task-item { display: flex; align-items: flex-start; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.task-item:last-child { border-bottom: none; }
.task-check {
  width: 22px; height: 22px; border: 2px solid var(--border); border-radius: 50%;
  flex-shrink: 0; cursor: pointer; display: flex; align-items: center; justify-content: center;
  margin-top: 1px; transition: all 0.15s; background: white;
}
.task-check.done { background: var(--success); border-color: var(--success); color: white; }
.task-check svg { width: 12px; height: 12px; display: none; }
.task-check.done svg { display: block; }
.task-body { flex: 1; }
.task-title { font-size: 15px; color: var(--text); }
.task-title.done { text-decoration: line-through; color: var(--text-muted); }
.task-meta { font-size: 12px; color: var(--text-muted); margin-top: 3px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.priority-dot { width: 6px; height: 6px; border-radius: 50%; display: inline-block; }
.priority-High { background: var(--danger); }
.priority-Medium { background: var(--warning); }
.priority-Low { background: var(--success); }

/* ── Activity ── */
.activity-item { display: flex; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.activity-item:last-child { border-bottom: none; }
.activity-icon {
  width: 32px; height: 32px; border-radius: 50%; background: var(--primary-light);
  display: flex; align-items: center; justify-content: center; font-size: 14px; flex-shrink: 0;
}
.activity-body { flex: 1; min-width: 0; }
.activity-subject { font-size: 14px; font-weight: 500; color: var(--text); }
.activity-meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

/* ── Note ── */
.note-item { padding: 10px 12px; background: #FAFAFA; border-radius: var(--radius-sm); border-left: 3px solid var(--primary); margin-bottom: 8px; position: relative; }
.note-text { font-size: 14px; color: var(--text); line-height: 1.5; }
.note-meta { font-size: 11px; color: var(--text-muted); margin-top: 4px; }
.note-delete { position: absolute; top: 8px; right: 8px; background: none; border: none; cursor: pointer; color: var(--text-muted); font-size: 16px; padding: 2px 6px; }

/* ── Quick Note ── */
.quick-note { display: flex; gap: 8px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }
.quick-note-input {
  flex: 1; padding: 9px 12px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); font-size: 14px; outline: none;
  font-family: inherit; background: var(--bg);
}
.quick-note-input:focus { border-color: var(--primary); background: white; }
.quick-note-btn {
  padding: 9px 14px; background: var(--primary); color: white;
  border: none; border-radius: var(--radius-sm); font-size: 14px; font-weight: 600;
  cursor: pointer; font-family: inherit; white-space: nowrap;
}

/* ── Section Header ── */
.section-header { display: flex; align-items: center; justify-content: space-between; margin: 16px 0 8px; }
.section-title { font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.section-action { font-size: 13px; color: var(--primary); font-weight: 500; border: none; background: none; cursor: pointer; font-family: inherit; padding: 4px 8px; }

/* ── Customer Header Card ── */
.customer-header { display: flex; align-items: center; gap: 14px; margin-bottom: 4px; }
.customer-avatar { width: 52px; height: 52px; border-radius: 14px; background: var(--primary); color: white; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 18px; flex-shrink: 0; }
.customer-name { font-size: 20px; font-weight: 700; color: var(--text); }
.customer-meta { font-size: 14px; color: var(--text-muted); margin-top: 2px; }
.customer-info-row { display: flex; gap: 6px; margin-top: 10px; flex-wrap: wrap; }

/* ── Contact Row ── */
.contact-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.contact-row:last-child { border-bottom: none; }
.contact-info { flex: 1; min-width: 0; }
.contact-name { font-size: 14px; font-weight: 500; color: var(--text); }
.contact-title { font-size: 12px; color: var(--text-muted); margin-top: 1px; }
.contact-actions { display: flex; gap: 4px; }
.contact-btn {
  width: 30px; height: 30px; border: 1px solid var(--border); border-radius: 8px;
  background: white; display: flex; align-items: center; justify-content: center;
  text-decoration: none; color: var(--text-muted); font-size: 14px;
}

/* ── Opportunity Row ── */
.opp-row { padding: 10px 0; border-bottom: 1px solid var(--border); cursor: pointer; }
.opp-row:last-child { border-bottom: none; }
.opp-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.opp-name { font-size: 14px; font-weight: 500; color: var(--text); flex: 1; }
.opp-amount { font-size: 14px; font-weight: 700; color: var(--text); flex-shrink: 0; }
.opp-meta { display: flex; align-items: center; gap: 8px; margin-top: 4px; flex-wrap: wrap; }

/* ── Login ── */
.login-screen {
  min-height: 100vh; display: flex; flex-direction: column;
  align-items: center; justify-content: center; padding: 32px 24px; background: var(--bg);
}
.login-logo { font-size: 36px; font-weight: 800; color: var(--primary); margin-bottom: 4px; }
.login-tagline { font-size: 14px; color: var(--text-muted); margin-bottom: 40px; }
.login-card { background: var(--card); border-radius: var(--radius); padding: 24px; width: 100%; max-width: 360px; box-shadow: var(--shadow-md); }
.login-title { font-size: 18px; font-weight: 700; margin-bottom: 20px; text-align: center; }

/* ── Spinner ── */
.spinner {
  display: inline-block; width: 22px; height: 22px;
  border: 2px solid var(--border); border-top-color: var(--primary);
  border-radius: 50%; animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-screen { display: flex; align-items: center; justify-content: center; min-height: 200px; }

/* ── FAB ── */
.fab {
  position: fixed; bottom: calc(var(--nav-height) + env(safe-area-inset-bottom) + 16px); right: max(16px, calc(50vw - 320px + 16px));
  width: 52px; height: 52px; background: var(--primary); color: white;
  border: none; border-radius: 50%; box-shadow: var(--shadow-md);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; z-index: 50; font-size: 26px; font-weight: 300;
}

/* ── Responsive desktop ── */
@media (min-width: 640px) {
  .stats-grid { grid-template-columns: repeat(4, 1fr); }
}
