/* ==========================================================================
   B2B CRM — Design System
   Design tokens (CSS variables) + reusable component classes.
   Every screen is built on top of these; no screen defines ad-hoc colors.
   ========================================================================== */

:root {
  /* Color palette */
  --c-bg:        #f4f6fb;   /* app background   */
  --c-surface:   #ffffff;   /* cards, panels    */
  --c-surface-2: #f8fafc;   /* subtle fills     */
  --c-border:    #e3e8ef;
  --c-text:      #1a2233;
  --c-muted:     #6b7688;
  --c-primary:   #3b5bdb;
  --c-primary-d: #2f49b0;
  --c-primary-l: #eef2ff;
  --c-success:   #1a936f;
  --c-success-l: #e6f5ef;
  --c-danger:    #d64545;
  --c-danger-l:  #fbeaea;
  --c-warn:      #b7791f;
  --c-warn-l:    #fbf3e2;
  --c-sidebar:   #141a2e;
  --c-sidebar-2: #9aa4bf;

  /* Spacing scale */
  --s-1: 4px;  --s-2: 8px;  --s-3: 12px; --s-4: 16px;
  --s-5: 24px; --s-6: 32px; --s-7: 48px; --s-8: 64px;

  /* Radius / shadow / type */
  --r-sm: 6px; --r-md: 10px; --r-lg: 14px; --r-pill: 999px;
  --shadow-sm: 0 1px 2px rgba(20, 26, 46, .06);
  --shadow-md: 0 4px 16px rgba(20, 26, 46, .10);
  --shadow-lg: 0 12px 40px rgba(20, 26, 46, .18);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --fs-xs: 12px; --fs-sm: 13px; --fs-md: 14px; --fs-lg: 16px; --fs-xl: 20px; --fs-2xl: 28px;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  font-size: var(--fs-md);
  color: var(--c-text);
  background: var(--c-bg);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, h4 { margin: 0; font-weight: 650; line-height: 1.25; }
h1 { font-size: var(--fs-2xl); }
h2 { font-size: var(--fs-xl); }
h3 { font-size: var(--fs-lg); }
a { color: var(--c-primary); text-decoration: none; }
p { margin: 0 0 var(--s-3); }
.muted { color: var(--c-muted); }
.hidden { display: none !important; }

/* ---------- App shell ---------- */
.app-shell { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }

.sidebar {
  background: var(--c-sidebar);
  color: #fff;
  display: flex; flex-direction: column;
  padding: var(--s-5) var(--s-4);
  gap: var(--s-2);
}
.brand { display: flex; align-items: center; gap: var(--s-3); padding: 0 var(--s-2) var(--s-5); }
.brand .logo {
  width: 34px; height: 34px; border-radius: var(--r-md);
  background: linear-gradient(135deg, var(--c-primary), #6f86ff);
  display: grid; place-items: center; font-weight: 800; color: #fff;
}
.brand .name { font-weight: 700; font-size: var(--fs-lg); }
.nav-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--c-sidebar-2); padding: var(--s-4) var(--s-2) var(--s-1);
}
.nav-item {
  display: flex; align-items: center; gap: var(--s-3);
  padding: var(--s-2) var(--s-3); border-radius: var(--r-sm);
  color: var(--c-sidebar-2); cursor: pointer; font-weight: 550; font-size: var(--fs-md);
}
.nav-item:hover { background: rgba(255,255,255,.06); color: #fff; }
.nav-item.active { background: var(--c-primary); color: #fff; }
.sidebar .spacer { flex: 1; }

.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--s-4) var(--s-6); background: var(--c-surface);
  border-bottom: 1px solid var(--c-border);
}
.topbar .who { display: flex; align-items: center; gap: var(--s-3); color: var(--c-muted); font-size: var(--fs-sm); }
.pill-role {
  font-size: var(--fs-xs); font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
  background: var(--c-primary-l); color: var(--c-primary-d);
  padding: 2px var(--s-2); border-radius: var(--r-pill);
}
.content { padding: var(--s-6); max-width: 1180px; width: 100%; }
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--s-5); gap: var(--s-4); }
.page-head .sub { color: var(--c-muted); margin-top: var(--s-1); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2);
  font-family: inherit; font-size: var(--fs-md); font-weight: 600; line-height: 1;
  padding: 10px var(--s-4); border-radius: var(--r-sm);
  border: 1px solid transparent; cursor: pointer; transition: background .12s, border-color .12s, opacity .12s;
  white-space: nowrap;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn--primary { background: var(--c-primary); color: #fff; }
.btn--primary:hover:not(:disabled) { background: var(--c-primary-d); }
.btn--secondary { background: var(--c-surface); color: var(--c-text); border-color: var(--c-border); }
.btn--secondary:hover:not(:disabled) { background: var(--c-surface-2); }
.btn--danger { background: var(--c-danger); color: #fff; }
.btn--danger:hover:not(:disabled) { background: #bd3a3a; }
.btn--success { background: var(--c-success); color: #fff; }
.btn--success:hover:not(:disabled) { background: #15795c; }
.btn--ghost { background: transparent; color: var(--c-primary); border-color: transparent; padding: 6px var(--s-2); }
.btn--ghost:hover:not(:disabled) { background: var(--c-primary-l); }
.btn--sm { padding: 6px var(--s-3); font-size: var(--fs-sm); }
.btn--block { width: 100%; }

/* ---------- Cards ---------- */
.card {
  background: var(--c-surface); border: 1px solid var(--c-border);
  border-radius: var(--r-lg); box-shadow: var(--shadow-sm);
}
.card__head { padding: var(--s-4) var(--s-5); border-bottom: 1px solid var(--c-border); display: flex; align-items: center; justify-content: space-between; }
.card__body { padding: var(--s-5); }
.card + .card { margin-top: var(--s-5); }

/* ---------- Forms ---------- */
.field { margin-bottom: var(--s-4); }
.field label { display: block; font-size: var(--fs-sm); font-weight: 600; margin-bottom: var(--s-2); color: var(--c-text); }
.input, .select, .textarea {
  width: 100%; font-family: inherit; font-size: var(--fs-md); color: var(--c-text);
  padding: 10px var(--s-3); border: 1px solid var(--c-border); border-radius: var(--r-sm);
  background: var(--c-surface); transition: border-color .12s, box-shadow .12s;
}
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--c-primary); box-shadow: 0 0 0 3px var(--c-primary-l);
}
.textarea { min-height: 76px; resize: vertical; }
.field-row { display: flex; gap: var(--s-4); }
.field-row > * { flex: 1; }
.checks { display: flex; flex-wrap: wrap; gap: var(--s-2); }
.check {
  display: inline-flex; align-items: center; gap: var(--s-2);
  padding: var(--s-2) var(--s-3); border: 1px solid var(--c-border); border-radius: var(--r-sm);
  cursor: pointer; font-size: var(--fs-sm); background: var(--c-surface);
}
.check input { accent-color: var(--c-primary); }
.form-error { color: var(--c-danger); font-size: var(--fs-sm); margin-bottom: var(--s-3); min-height: 18px; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; font-size: var(--fs-md); }
.table th {
  text-align: left; font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .04em;
  color: var(--c-muted); font-weight: 700; padding: var(--s-3) var(--s-4); border-bottom: 1px solid var(--c-border);
}
.table td { padding: var(--s-3) var(--s-4); border-bottom: 1px solid var(--c-border); }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr.clickable { cursor: pointer; }
.table tbody tr.clickable:hover { background: var(--c-surface-2); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table .actions { text-align: right; white-space: nowrap; }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: var(--fs-xs); font-weight: 700; padding: 3px var(--s-2); border-radius: var(--r-pill);
  background: var(--c-surface-2); color: var(--c-muted); border: 1px solid var(--c-border);
}
.badge--open  { background: var(--c-primary-l); color: var(--c-primary-d); border-color: transparent; }
.badge--won   { background: var(--c-success-l); color: var(--c-success);   border-color: transparent; }
.badge--lost  { background: var(--c-danger-l);  color: var(--c-danger);    border-color: transparent; }
.badge--stage { background: #eef1f6; color: #4a5568; border-color: transparent; }

/* ---------- Metric tiles ---------- */
.metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--s-4); margin-bottom: var(--s-5); }
.metric { background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--r-lg); padding: var(--s-5); box-shadow: var(--shadow-sm); }
.metric .label { color: var(--c-muted); font-size: var(--fs-sm); font-weight: 600; }
.metric .value { font-size: var(--fs-2xl); font-weight: 700; margin-top: var(--s-2); font-variant-numeric: tabular-nums; }
.metric .value.success { color: var(--c-success); }

/* Progress bar for actual-vs-benchmark gap */
.bar { height: 8px; border-radius: var(--r-pill); background: var(--c-surface-2); overflow: hidden; min-width: 120px; }
.bar > span { display: block; height: 100%; background: var(--c-success); border-radius: var(--r-pill); }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(20, 26, 46, .45);
  display: flex; align-items: flex-start; justify-content: center; padding: var(--s-7) var(--s-4);
  z-index: 50; overflow-y: auto;
}
.modal {
  background: var(--c-surface); border-radius: var(--r-lg); box-shadow: var(--shadow-lg);
  width: 100%; max-width: 520px; animation: pop .12s ease-out;
}
@keyframes pop { from { transform: translateY(6px); opacity: .6; } to { transform: none; opacity: 1; } }
.modal__head { padding: var(--s-5); border-bottom: 1px solid var(--c-border); display: flex; align-items: center; justify-content: space-between; }
.modal__body { padding: var(--s-5); }
.modal__foot { padding: var(--s-4) var(--s-5); border-top: 1px solid var(--c-border); display: flex; justify-content: flex-end; gap: var(--s-3); }
.modal .x { background: none; border: none; font-size: 22px; line-height: 1; color: var(--c-muted); cursor: pointer; }

/* ---------- Toast ---------- */
.toast-wrap { position: fixed; bottom: var(--s-5); right: var(--s-5); display: flex; flex-direction: column; gap: var(--s-2); z-index: 100; }
.toast {
  padding: var(--s-3) var(--s-4); border-radius: var(--r-sm); box-shadow: var(--shadow-md);
  color: #fff; font-size: var(--fs-sm); font-weight: 550; animation: pop .12s ease-out; max-width: 340px;
}
.toast--ok  { background: var(--c-success); }
.toast--err { background: var(--c-danger); }

/* ---------- Empty state ---------- */
.empty { text-align: center; padding: var(--s-7) var(--s-4); color: var(--c-muted); }
.empty .big { font-size: 40px; margin-bottom: var(--s-3); }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(135deg, #eef2ff, #f4f6fb); padding: var(--s-4); }
.login-card { width: 100%; max-width: 380px; }
.login-card .brand { justify-content: center; padding-bottom: var(--s-4); }

/* ---------- Small helpers ---------- */
.row { display: flex; align-items: center; gap: var(--s-3); }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: var(--s-2); }
.mt-4 { margin-top: var(--s-4); }
.section-title { font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .05em; color: var(--c-muted); font-weight: 700; margin-bottom: var(--s-3); }

/* Follow-up history */
.fu-item { display: flex; gap: var(--s-3); padding: var(--s-3) 0; border-bottom: 1px solid var(--c-border); }
.fu-item:last-child { border-bottom: none; }
.fu-date { flex: 0 0 96px; color: var(--c-muted); font-size: var(--fs-sm); font-variant-numeric: tabular-nums; }
.fu-note { flex: 1; }

/* Detail layout */
.detail-grid { display: grid; grid-template-columns: 1.4fr 1fr; gap: var(--s-5); align-items: start; }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: var(--s-2) var(--s-4); font-size: var(--fs-md); }
.kv dt { color: var(--c-muted); }
.kv dd { margin: 0; font-weight: 550; }

@media (max-width: 860px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; flex-wrap: wrap; align-items: center; padding: var(--s-3); }
  .sidebar .spacer, .nav-label { display: none; }
  .metrics { grid-template-columns: 1fr; }
  .detail-grid { grid-template-columns: 1fr; }
  .field-row { flex-direction: column; }
}
