:root {
  --bg: #f8fafc;
  --panel: #ffffff;
  --panel-2: #f9fbff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #eef1f5;
  --accent: #2563eb;
  --accent-d: #1d4ed8;
  --accent-soft: #eaf1ff;
  --green: #059669;
  --amber: #d97706;
  --red: #dc2626;
  --shadow: 0 1px 2px rgba(15, 23, 42, .04), 0 8px 22px rgba(15, 23, 42, .05);
  --shadow-lg: 0 10px 40px rgba(15, 23, 42, .16);
  --r: 12px;
  --side-w: 232px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 13.5px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.spacer { flex: 1; }
.error { color: var(--red); min-height: 1em; font-size: 13px; margin-top: 6px; }
a { color: var(--accent); }

/* ---------- Inputs & buttons ---------- */
input, textarea, select {
  background: var(--panel); border: 1px solid #dfe4ec; color: var(--ink);
  border-radius: 10px; padding: 10px 12px; font: inherit; width: 100%;
}
input:focus, textarea:focus, select:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea { min-height: 96px; resize: vertical; }
label.field { display: flex; flex-direction: column; gap: 6px; font-size: 12.5px;
  font-weight: 600; color: #475569; margin-bottom: 12px; }

.btn {
  background: var(--panel); color: var(--ink); border: 1px solid #dfe4ec;
  border-radius: 10px; padding: 9px 14px; font: inherit; font-weight: 600; cursor: pointer;
  box-shadow: var(--shadow); transition: filter .15s, background .15s, transform .05s;
  display: inline-flex; align-items: center; gap: 7px; white-space: nowrap;
}
.btn:hover { background: #f4f7fb; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff;
  box-shadow: 0 8px 20px rgba(37, 99, 235, .25); }
.btn.primary:hover { background: var(--accent-d); }
.btn.danger { background: var(--red); border-color: transparent; color: #fff; box-shadow: 0 8px 20px rgba(220,38,38,.22); }
.btn.danger:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; box-shadow: none; }
.btn.ghost:hover { background: #eef2f7; }
.btn.small { padding: 7px 11px; font-size: 12.5px; }
.btn.block { width: 100%; justify-content: center; }
.btn:disabled { opacity: .6; cursor: not-allowed; }

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px;
  background: radial-gradient(900px 420px at 50% -10%, #e7eefc 0, transparent 60%), var(--bg);
}
.login-card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px;
  padding: 30px 26px; width: 100%; max-width: 370px; box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column; gap: 6px;
}
.login-card .field { margin-top: 8px; }
.brand { display: flex; align-items: center; gap: 10px; font-size: 20px; font-weight: 800; letter-spacing: -.01em; }
.brand .mk, .logo .mk {
  width: 32px; height: 32px; border-radius: 9px; display: grid; place-items: center; color: #fff;
  background: linear-gradient(135deg, var(--accent), #60a5fa); box-shadow: 0 6px 16px rgba(37,99,235,.3);
}
.login-card > .muted { margin: 2px 0 6px; }
.login-card .btn.primary { margin-top: 8px; }
.demo-hint { font-size: 12px; color: var(--muted); text-align: center; margin-top: 10px; line-height: 1.7;
  border-top: 1px solid var(--line); padding-top: 12px; }

/* ---------- App shell ---------- */
#app { display: grid; grid-template-columns: var(--side-w) 1fr; min-height: 100vh; }

.sidebar {
  background: var(--panel); border-right: 1px solid var(--line); padding: 18px 12px;
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column;
}
.logo { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 15px; padding: 6px 8px 18px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 11px; padding: 9px 12px; border-radius: 9px;
  color: var(--muted); font-weight: 600; cursor: pointer; text-decoration: none; font-size: 13.5px;
}
.nav-item .ni-ico { width: 18px; text-align: center; }
.nav-item:hover { background: #f1f5f9; color: var(--ink); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.side-user {
  margin-top: auto; display: flex; align-items: center; gap: 10px; padding: 10px;
  border: 1px solid var(--line); border-radius: 12px;
}
.avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 700; flex: none; }
.su-name { font-weight: 650; }
.su-role { font-size: 12px; color: var(--muted); text-transform: capitalize; }

.content { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px;
  background: rgba(248, 250, 252, .85); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line); padding: 12px 24px; min-height: 56px;
}
.page-title { font-weight: 700; font-size: 15px; }
.icon-btn { display: none; background: transparent; border: 0; font-size: 20px; cursor: pointer; color: var(--ink); padding: 4px 8px; }
main { padding: 22px 24px; max-width: 1120px; width: 100%; }

.scrim { display: none; }

/* ---------- Headings ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; gap: 12px; }
.page-head h2 { margin: 0; font-size: 21px; letter-spacing: -.01em; }
.page-head .sub { color: var(--muted); margin-top: 2px; font-size: 13px; }

/* ---------- Cards ---------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r);
  box-shadow: var(--shadow); margin-bottom: 16px; overflow: hidden; }
.card > h3 { margin: 0; padding: 14px 18px; font-size: 14.5px; border-bottom: 1px solid var(--line); }
.card .card-body { padding: 16px 18px; }
.grid { display: grid; gap: 14px; }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

/* ---------- Rows / list items ---------- */
.list-item { display: flex; align-items: center; gap: 14px; padding: 13px 18px;
  border-bottom: 1px solid var(--line); cursor: pointer; }
.card .list-item:last-child { border-bottom: 0; }
.list-item:hover { background: var(--panel-2); }
.list-item .li-main { flex: 1; min-width: 0; }
.li-title { font-weight: 650; }
.li-sub { font-size: 12.5px; color: var(--muted); }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

/* ---------- Tables ---------- */
table.tbl { width: 100%; border-collapse: collapse; }
table.tbl th { text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); font-weight: 700; padding: 11px 16px; border-bottom: 1px solid var(--line); background: var(--panel-2); }
table.tbl td { padding: 12px 16px; border-bottom: 1px solid var(--line); }
table.tbl tbody tr:last-child td { border-bottom: 0; }
table.tbl tbody tr.clickable { cursor: pointer; }
table.tbl tbody tr.clickable:hover { background: var(--panel-2); }
.cust { display: flex; align-items: center; gap: 11px; }
.c-avatar { width: 34px; height: 34px; border-radius: 9px; background: var(--accent-soft); color: var(--accent);
  display: grid; place-items: center; font-weight: 700; font-size: 12px; flex: none; }
.c-name { font-weight: 650; }
.c-sub { color: var(--muted); font-size: 12px; }
.chev { color: #cbd5e1; }

/* ---------- Segmented filter ---------- */
.toolbar { display: flex; gap: 10px; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.toolbar h3 { margin: 0; font-size: 14.5px; }
.seg { margin-left: auto; display: inline-flex; background: #eef2f7; border-radius: 9px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 6px 12px; border-radius: 7px; font: inherit;
  font-weight: 600; color: var(--muted); cursor: pointer; font-size: 12.5px; }
.seg button.on { background: #fff; color: var(--ink); box-shadow: var(--shadow); }

/* ---------- Badges ---------- */
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 99px;
  font-size: 11.5px; font-weight: 700; background: #eef2f7; color: var(--muted); }
.badge.dot::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.onsite { background: #e7f6ef; color: #047857; }
.badge.nearby { background: #fdf1e1; color: #b45309; }
.badge.outside { background: #fce9e9; color: #b91c1c; }
.badge.unknown { background: #eef2f7; color: var(--muted); }
.badge.overdue { background: #fce9e9; color: #b91c1c; }
.badge.today { background: var(--accent-soft); color: var(--accent); }
.badge.fresh { background: #e7f6ef; color: #047857; }
.badge.stale { background: #eef2f7; color: var(--muted); }
.badge.never { background: #fce9e9; color: #b91c1c; }

/* ---------- Stat / KPI ---------- */
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 16px; box-shadow: var(--shadow); }
.stat .num { font-size: 25px; font-weight: 800; letter-spacing: -.02em; }
.stat .lbl { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* ---------- Timeline ---------- */
.timeline { position: relative; padding-left: 20px; }
.timeline::before { content: ""; position: absolute; left: 5px; top: 4px; bottom: 4px; width: 2px; background: var(--line); }
.tl-item { position: relative; margin-bottom: 16px; }
.tl-item::before { content: ""; position: absolute; left: -20px; top: 3px; width: 11px; height: 11px;
  border-radius: 50%; background: var(--accent); border: 2px solid var(--panel); box-shadow: 0 0 0 2px var(--panel); }
.tl-item.visit::before { background: var(--green); }
.tl-item.note::before { background: #7c3aed; }
.tl-item.call::before { background: var(--amber); }
.tl-item.followup::before { background: var(--red); }
.tl-item.document::before { background: #94a3b8; }
.tl-head { font-weight: 650; font-size: 13.5px; }
.tl-meta { font-size: 12px; color: var(--muted); }
.tl-body { font-size: 13px; margin-top: 3px; white-space: pre-wrap; color: #334155; }

/* ---------- Map ---------- */
#map, .mini-map { width: 100%; height: 400px; border-radius: var(--r); border: 1px solid var(--line); }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12.5px; margin: 4px 0 12px; color: var(--muted); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; vertical-align: middle; }

/* ---------- Recorder ---------- */
.rec-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--red); display: inline-block; }
.rec-dot.live { animation: pulse 1s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .3; } }

/* ---------- Modal ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: flex;
  align-items: flex-start; justify-content: center; padding: 24px 16px; z-index: 80; overflow-y: auto; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; width: 100%;
  max-width: 520px; padding: 22px; box-shadow: var(--shadow-lg); margin-top: 36px; }
.modal h3 { margin-top: 0; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }

/* ---------- Toast ---------- */
.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); background: var(--ink);
  color: #fff; padding: 11px 18px; border-radius: 11px; box-shadow: var(--shadow-lg); z-index: 100;
  max-width: 90%; font-size: 13.5px; font-weight: 500; }
.toast.ok { background: #065f46; }
.toast.err { background: #991b1b; }

.tag-ai { font-size: 11px; background: #f3ecff; color: #7c3aed; padding: 1px 8px; border-radius: 99px; font-weight: 700; }
.empty { color: var(--muted); text-align: center; padding: 34px; }

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  #app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 70; top: 0; bottom: 0; left: 0; width: 250px; height: 100vh;
    transform: translateX(-100%); transition: transform .22s ease; box-shadow: var(--shadow-lg); }
  .sidebar.open { transform: none; }
  .scrim.show { display: block; position: fixed; inset: 0; background: rgba(15, 23, 42, .4); z-index: 65; }
  .icon-btn { display: inline-flex; }
  main { padding: 16px; }
  .topbar { padding: 12px 16px; }
}
@media (max-width: 560px) {
  .hide-sm { display: none !important; }
  table.tbl th, table.tbl td { padding: 10px 12px; }
}
