:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --ink: #16192c;
  --muted: #6b7180;
  --line: #e6e9f0;
  --brand: #2e5bff;
  --brand-ink: #1e3eb8;
  --won: #17a673;
  --lost: #e5484d;
  --warn: #e5484d;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(16,25,44,.06), 0 8px 24px rgba(16,25,44,.06);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 20px;
  padding: 14px 24px; background: var(--surface);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 800; font-size: 20px; letter-spacing: -.02em; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.brand .crm { color: var(--brand); }
.brand-logo { height: 30px; width: auto; display: block; }
.tabs { display: flex; gap: 4px; margin-left: 8px; }
.tab {
  border: 0; background: transparent; color: var(--muted);
  font-size: 15px; font-weight: 600; padding: 8px 14px; border-radius: 999px; cursor: pointer;
}
.tab.active { background: #eef2ff; color: var(--brand-ink); }
.btn {
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  padding: 9px 14px; border-radius: 10px; font-weight: 600; cursor: pointer; font-size: 14px;
}
.btn.primary { background: var(--brand); color: #fff; border-color: var(--brand); margin-left: auto; }
.btn.ghost { background: transparent; }
.btn:hover { filter: brightness(.98); }

.view { padding: 24px; max-width: 1200px; margin: 0 auto; }
h1 { font-size: 22px; margin: 0 0 2px; }
.sub { color: var(--muted); font-size: 13px; margin: 0 0 20px; }

/* KPI grid */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; }
.kpi {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow);
}
.kpi .val { font-size: 26px; font-weight: 800; letter-spacing: -.02em; }
.kpi .lbl { color: var(--muted); font-size: 13px; margin-top: 4px; }
.kpi.warn .val { color: var(--warn); }

/* Pipeline board */
.board { display: flex; gap: 14px; overflow-x: auto; padding-bottom: 12px; }
.column { min-width: 260px; flex: 0 0 260px; }
.col-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 10px; }
.col-head b { font-size: 14px; }
.col-head small { color: var(--muted); }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px; margin-bottom: 10px; cursor: grab; box-shadow: var(--shadow);
}
.card:hover { border-color: var(--brand); }
.card:active { cursor: grabbing; }
.card.dragging { opacity: .4; }

/* Drop target */
.col-body {
  min-height: 55vh; padding: 4px; border-radius: 12px;
  outline: 2px dashed transparent; transition: background .12s, outline-color .12s;
}
.column.drop-over .col-body { background: rgba(46, 91, 255, .09); outline-color: var(--brand); }
.col-empty { color: var(--muted); text-align: center; padding: 18px 0; font-size: 13px; }
.card .nm { font-weight: 700; }
.card .co { color: var(--muted); font-size: 13px; }
.card .mt { color: var(--brand-ink); font-size: 12px; font-weight: 600; margin-top: 6px; }

/* Contacts list */
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.row { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); cursor: pointer; }
.row:last-child { border-bottom: 0; }
.row:hover { background: #f8faff; }
.row .meta { color: var(--muted); font-size: 13px; }
.search { width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; font-size: 15px; margin-bottom: 14px; }

/* Badges */
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge.HOT { background: #fdeceb; color: var(--lost); }
.badge.WARM { background: #fff4e5; color: #b76e00; }
.badge.COLD { background: #eef2ff; color: var(--brand-ink); }
.badge.WON { background: #e7f7f0; color: var(--won); }
.badge.LOST { background: #fdeceb; color: var(--lost); }

/* Drawer */
.drawer { position: fixed; inset: 0; z-index: 20; }
.drawer.hidden { display: none; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(16,25,44,.4); }
.drawer-panel {
  position: absolute; right: 0; top: 0; height: 100%; width: min(480px, 92vw);
  background: var(--surface); box-shadow: -8px 0 32px rgba(16,25,44,.18);
  padding: 24px; overflow-y: auto;
}
.drawer-panel h2 { margin: 0 0 4px; }
.field { display: block; margin: 12px 0; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; font-size: 14px; font-family: inherit;
}
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0 4px; }
.chip { border: 1px solid var(--line); background: var(--surface); padding: 6px 12px; border-radius: 999px; font-size: 13px; cursor: pointer; }
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.section-t { font-weight: 700; margin: 22px 0 8px; font-size: 14px; }
.timeline .item { padding: 10px 0; border-bottom: 1px solid var(--line); }
.timeline .item .t { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.timeline .item .b { font-size: 14px; margin-top: 2px; }
.task { display: flex; align-items: center; gap: 10px; padding: 8px 0; }
.task.done .tt { text-decoration: line-through; color: var(--muted); }

/* Activities: List / Calendar */
.act-top { display: flex; align-items: center; gap: 16px; margin-bottom: 14px; }
.act-top h1 { margin: 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--surface); }
.seg-btn { border: 0; background: transparent; padding: 7px 14px; font-size: 14px; font-weight: 600; color: var(--muted); cursor: pointer; }
.seg-btn.on { background: #eef2ff; color: var(--brand-ink); }
.od { color: var(--lost); font-weight: 600; }

.cal-nav { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.cal-nav .btn { padding: 6px 12px; }
.cal-range { color: var(--muted); font-size: 14px; margin-left: 6px; }

.cal { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.cal-head, .cal-grid { display: grid; grid-template-columns: 62px repeat(7, minmax(90px, 1fr)); }
.cal-head { border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--surface); z-index: 2; }
.cal-gut { padding: 10px 8px; font-size: 11px; font-weight: 700; color: var(--muted); }
.cal-dh { padding: 10px 8px; text-align: center; font-size: 13px; font-weight: 600; color: var(--muted); border-left: 1px solid var(--line); }
.cal-dh .dnum { display: inline-block; min-width: 22px; height: 22px; line-height: 22px; border-radius: 50%; }
.cal-dh.is-today { color: var(--brand-ink); }
.cal-dh.is-today .dnum { background: var(--brand); color: #fff; }

.cal-scroll { max-height: 62vh; overflow: auto; }
.cal-time { font-size: 11px; color: var(--muted); padding: 4px 8px; text-align: right; height: 48px; box-sizing: border-box; border-top: 1px solid var(--line); }
.cal-cell { height: 48px; box-sizing: border-box; border-top: 1px solid var(--line); border-left: 1px solid var(--line); padding: 2px; cursor: pointer; overflow: hidden; }
.cal-cell:hover { background: rgba(46, 91, 255, .05); }
.cal-ev {
  display: flex; align-items: center; gap: 5px; background: #eef2ff; color: var(--brand-ink);
  border-radius: 6px; padding: 3px 6px; font-size: 12px; font-weight: 600; margin-bottom: 2px;
  cursor: pointer; white-space: nowrap; overflow: hidden;
}
.cal-ev:hover { filter: brightness(.96); }
.cal-ev .ev-t { overflow: hidden; text-overflow: ellipsis; }
.cal-ev.done { opacity: .5; text-decoration: line-through; }
@media (prefers-color-scheme: dark) { .seg-btn.on, .cal-ev { background: #26305a; } }

/* Admin dashboard */
.admin { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; align-items: start; }
.admin-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.admin-card h3 { margin: 0 0 6px; font-size: 16px; }
.admin-card .btn.primary { margin-left: 0; }
.ed-row { display: flex; gap: 6px; align-items: center; margin-bottom: 8px; }
.ed-row .ed-val { flex: 1; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; }
.ed-row .ed-type { padding: 8px; border: 1px solid var(--line); border-radius: 8px; }
.ed-row .btn { padding: 6px 9px; }
/* Built-in field editor: label | Show | Required | reorder */
.ed-head { display: flex; gap: 6px; font-size: 12px; color: var(--muted); margin-bottom: 6px; font-weight: 600; }
.ed-head span:first-child { flex: 1; }
.ed-head span + span { flex: 0 0 52px; text-align: center; }
.ed-head span:last-child { margin-right: 72px; }
.ed-cell { flex: 0 0 52px; display: flex; justify-content: center; align-items: center; }
.ed-cell input { accent-color: var(--brand); width: 16px; height: 16px; }
.core-row .ed-val:disabled, .ed-cell input:disabled { opacity: .5; }
.kv { display: flex; justify-content: space-between; padding: 5px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.kv span { color: var(--muted); }
.csv-tbl { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 6px; }
.csv-tbl th, .csv-tbl td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.csv-tbl th { color: var(--muted); font-weight: 600; }
#csvTemplate { display: inline-block; text-decoration: none; }

/* Auth */
.auth-area { margin-left: 12px; display: flex; align-items: center; gap: 10px; }
.auth-area .who { font-size: 13px; color: var(--muted); }
.auth-wrap { min-height: 70vh; display: flex; align-items: center; justify-content: center; }
.auth-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 28px; width: min(380px, 92vw);
}
.auth-card h1 { font-size: 20px; margin-bottom: 4px; }
.auth-card .btn.primary { width: 100%; margin-left: 0; margin-top: 8px; }
.auth-card .switch { text-align: center; margin-top: 14px; font-size: 13px; color: var(--muted); }
.auth-card .switch a { color: var(--brand); cursor: pointer; }
.auth-err { color: var(--lost); font-size: 13px; min-height: 18px; margin-top: 6px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 18px; border-radius: 10px; z-index: 40; font-size: 14px;
}
.toast.hidden { display: none; }
.muted { color: var(--muted); }
.empty { color: var(--muted); text-align: center; padding: 40px; }

@media (prefers-color-scheme: dark) {
  :root { --bg:#0f1220; --surface:#181c2e; --ink:#eef0f6; --muted:#9aa0b4; --line:#272c42; }
}
