:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #15181d;
  --muted: #5f6672;
  --border: #dde1e6;
  --primary: #0e7490;
  --primary-ink: #ffffff;
  --primary-soft: #e0f2f7;
  --wa: #1fa855;
  --wa-ink: #ffffff;
  --danger: #c2410c;
  --hot: #dc2626;
  --warm: #d97706;
  --cold: #2563eb;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 14px;
  --nav-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #181c22;
    --surface-2: #222831;
    --text: #e8ebef;
    --muted: #9aa3ae;
    --border: #2c333d;
    --primary: #38b2cf;
    --primary-ink: #04161c;
    --primary-soft: #11303a;
    --wa: #2fc566;
    --wa-ink: #04140a;
    --danger: #fb923c;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 16px);
}
a { color: var(--primary); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
img { max-width: 100%; display: block; }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) max(16px, calc((100% - 640px) / 2 + 16px)) 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
}
.topbar h1 { font-size: 20px; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .icon-btn { flex: none; }
main { max-width: 640px; margin: 0 auto; padding: 4px 16px 16px; }
.section-title { font-size: 13px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); margin: 22px 4px 8px; display: flex; justify-content: space-between; align-items: center; }
.section-title a { text-transform: none; letter-spacing: 0; font-weight: 500; }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--border); }
.pad { padding: 16px; }
.stack > * + * { margin-top: 12px; }
.row { display: flex; align-items: center; gap: 10px; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- bottom nav ---------- */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
  display: grid; grid-template-columns: repeat(5, 1fr); align-items: center;
  padding-inline: max(0px, calc((100% - 640px) / 2));
}
.nav a { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 11px; color: var(--muted); padding: 6px 0; }
.nav a.active { color: var(--primary); }
.nav a svg { width: 24px; height: 24px; }
.nav .scan-fab {
  width: 56px; height: 56px; margin: -22px auto 0; border-radius: 50%;
  background: var(--primary); color: var(--primary-ink); display: grid; place-items: center;
  box-shadow: 0 6px 16px rgba(14, 116, 144, .35);
}
.nav .scan-fab svg { width: 28px; height: 28px; }

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); font-weight: 600; cursor: pointer; text-align: center;
}
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: transparent; }
.btn.wa { background: var(--wa); color: var(--wa-ink); border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn.lg { min-height: 56px; font-size: 17px; border-radius: 14px; }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn svg { width: 20px; height: 20px; flex: none; }
.icon-btn { width: 42px; height: 42px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); display: grid; place-items: center; cursor: pointer; }
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn.wa { background: var(--wa); color: var(--wa-ink); border-color: transparent; }
.btn-row { display: flex; gap: 10px; }
.btn-row > * { flex: 1; }

label.field { display: block; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin: 0 2px 4px; }
.input, select.input, textarea.input {
  width: 100%; min-height: 46px; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); outline: none;
}
textarea.input { min-height: 90px; resize: vertical; }
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
@media (max-width: 360px) { .grid-2 { grid-template-columns: 1fr; } }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 7px 13px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); font-size: 14px; cursor: pointer; white-space: nowrap;
}
.chip.on { background: var(--primary); color: var(--primary-ink); border-color: transparent; }

.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: transparent; border-radius: 9px; padding: 9px 4px; font-size: 14px; cursor: pointer; }
.seg button.on { background: var(--surface); box-shadow: var(--shadow); font-weight: 600; }

/* ---------- home ---------- */
.hero-scan { display: flex; align-items: center; gap: 14px; padding: 18px; background: var(--primary); color: var(--primary-ink); border-radius: 18px; cursor: pointer; border: 0; width: 100%; text-align: left; }
.hero-scan svg { width: 34px; height: 34px; flex: none; }
.hero-scan strong { display: block; font-size: 18px; }
.hero-scan span { opacity: .85; font-size: 14px; }
.event-banner { display: flex; align-items: center; gap: 10px; padding: 12px 14px; margin-top: 12px; }
.event-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--wa); flex: none; box-shadow: 0 0 0 4px color-mix(in srgb, var(--wa) 25%, transparent); }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 12px; }
.stat { padding: 12px 8px; text-align: center; }
.stat b { display: block; font-size: 22px; line-height: 1.1; }
.stat span { font-size: 12px; color: var(--muted); }

/* ---------- contact list ---------- */
.list { overflow: hidden; }
.item { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-top: 1px solid var(--border); color: inherit; }
.item:first-child { border-top: 0; }
.avatar {
  width: 44px; height: 44px; border-radius: 50%; flex: none; position: relative;
  display: grid; place-items: center; font-weight: 700; font-size: 15px;
  background: var(--primary-soft); color: var(--primary);
}
.avatar.lg { width: 64px; height: 64px; font-size: 22px; }
.avatar i { position: absolute; right: -1px; bottom: -1px; width: 13px; height: 13px; border-radius: 50%; border: 2px solid var(--surface); }
.i-hot { background: var(--hot); } .i-warm { background: var(--warm); } .i-cold { background: var(--cold); }
.item .name { font-weight: 600; }
.badge { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.due { background: color-mix(in srgb, var(--danger) 15%, transparent); color: var(--danger); font-weight: 600; }
.badge.st-new { background: var(--primary-soft); color: var(--primary); }
.badge.st-messaged { background: color-mix(in srgb, var(--wa) 15%, transparent); color: var(--wa); }
.empty { text-align: center; padding: 36px 16px; color: var(--muted); }
.empty svg { width: 42px; height: 42px; opacity: .5; margin: 0 auto 10px; }

/* ---------- scan ---------- */
.shots { display: grid; grid-template-columns: 1.4fr 1fr; gap: 10px; }
.shot {
  aspect-ratio: 1.6; border-radius: 14px; border: 2px dashed var(--border); background: var(--surface);
  display: grid; place-items: center; text-align: center; cursor: pointer; overflow: hidden; position: relative; color: var(--muted);
}
.shot img { width: 100%; height: 100%; object-fit: cover; }
.shot svg { width: 30px; height: 30px; margin: 0 auto 4px; }
.shot .x { position: absolute; top: 6px; right: 6px; width: 28px; height: 28px; border-radius: 50%; border: 0; background: rgba(0,0,0,.55); color: #fff; cursor: pointer; }
.reading { display: flex; align-items: center; gap: 12px; padding: 14px 16px; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--primary-soft); border-top-color: var(--primary); animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.warn { background: color-mix(in srgb, var(--warm) 14%, var(--surface)); border: 1px solid color-mix(in srgb, var(--warm) 40%, transparent); padding: 12px 14px; border-radius: 12px; font-size: 14px; }
.phone-pick { display: flex; align-items: center; gap: 10px; padding: 8px 2px; }
.phone-pick input { width: 20px; height: 20px; accent-color: var(--wa); }

/* ---------- contact detail ---------- */
.profile { display: flex; gap: 14px; align-items: center; }
.profile h2 { margin: 0; font-size: 21px; line-height: 1.2; }
.quick-actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-top: 14px; }
.qa { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 4px; border-radius: 12px; background: var(--surface-2); border: 0; cursor: pointer; font-size: 12px; color: var(--text); }
.qa svg { width: 22px; height: 22px; }
.qa.disabled { opacity: .4; pointer-events: none; }
.tpl-row { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.tpl-btn { display: flex; align-items: center; gap: 8px; padding: 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; text-align: left; font-size: 14px; font-weight: 600; }
.tpl-btn .e { font-size: 20px; }
.kv { display: grid; grid-template-columns: 90px 1fr; gap: 8px 12px; font-size: 15px; }
.kv dt { color: var(--muted); font-size: 13px; padding-top: 2px; }
.kv dd { margin: 0; word-break: break-word; }
.card-img { border-radius: 12px; overflow: hidden; border: 1px solid var(--border); cursor: zoom-in; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { display: flex; gap: 12px; padding: 12px 14px; border-top: 1px solid var(--border); }
.timeline li:first-child { border-top: 0; }
.timeline .ic { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; background: var(--surface-2); flex: none; font-size: 15px; }
.timeline .ic.whatsapp { background: color-mix(in srgb, var(--wa) 18%, transparent); }
.timeline .body { white-space: pre-wrap; word-break: break-word; }

/* ---------- sheet, toast, lightbox ---------- */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 40; animation: fade .15s; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41; max-height: 88vh; overflow: auto;
  background: var(--surface); border-radius: 20px 20px 0 0; padding: 10px 16px calc(16px + env(safe-area-inset-bottom));
  max-width: 640px; margin: 0 auto; animation: up .2s ease-out;
}
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 0 auto 12px; }
.sheet h3 { margin: 0 0 12px; font-size: 18px; }
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
.toast {
  position: fixed; left: 12px; right: 12px; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom) + 12px); z-index: 50;
  max-width: 560px; margin: 0 auto; display: flex; align-items: center; gap: 12px;
  background: #1d2228; color: #fff; padding: 12px 14px; border-radius: 14px; box-shadow: 0 8px 24px rgba(0,0,0,.25); animation: up .2s;
}
.toast .msg { flex: 1; font-size: 15px; }
.toast button { background: transparent; border: 0; color: #7dd3fc; font-weight: 700; cursor: pointer; padding: 6px 8px; font-size: 15px; }
.toast.error { background: #7c2d12; }
.toast .bar { position: absolute; left: 0; bottom: 0; height: 3px; background: #7dd3fc; border-radius: 0 0 0 14px; }
.lightbox { position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.9); display: grid; place-items: center; padding: 16px; cursor: zoom-out; }
.lightbox img { max-height: 100%; object-fit: contain; }

/* ---------- login ---------- */
.login { min-height: 80vh; display: grid; place-items: center; }
.login .card { width: 100%; max-width: 360px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 22px; }
.brand svg { width: 34px; height: 34px; }
