:root {
  --bg: #f5f7fb; --card: #ffffff; --ink: #0f172a; --muted: #64748b; --line: #e2e8f0;
  --primary: #1e3a8a; --primary-ink: #ffffff; --accent: #f59e0b; --soft: #eef2ff;
  --ok: #16a34a; --warn-bg: #fff7ed; --warn-ink: #9a3412; --err: #dc2626;
  --s-new: #64748b; --s-visited: #16a34a; --s-followup: #ea580c; --s-interested: #ca8a04;
  --s-onboarded: #7c3aed; --s-not_interested: #dc2626; --s-not_rental: #be123c; --s-not_found: #57534e;
  --r: 14px; --shadow: 0 6px 24px rgba(15, 23, 42, .12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1120; --card: #111a2e; --ink: #e5e9f2; --muted: #94a3b8; --line: #1f2a44;
    --primary: #3b5bdb; --soft: #172345; --warn-bg: #2a1a0c; --warn-ink: #fdba74; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1120; --card: #111a2e; --ink: #e5e9f2; --muted: #94a3b8; --line: #1f2a44;
  --primary: #3b5bdb; --soft: #172345; --warn-bg: #2a1a0c; --warn-ink: #fdba74; color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif; overflow: hidden; -webkit-tap-highlight-color: transparent; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
a { color: var(--primary); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.err { color: var(--err); min-height: 1.2em; margin: 4px 0; font-size: 14px; }
.boot { display: grid; place-items: center; height: 100%; color: var(--muted); }

/* ---------- buttons */
.btn { border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 9px 14px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; color: var(--ink); white-space: nowrap; }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); font-weight: 600; }
.btn.ok { background: var(--ok); color: #fff; border-color: var(--ok); font-weight: 600; }
.btn.big { width: 100%; justify-content: center; padding: 13px; font-size: 16px; }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.icon-btn { border: 0; background: transparent; color: inherit; font-size: 20px; width: 40px; height: 40px; border-radius: 10px; cursor: pointer; }
.icon-btn:hover { background: rgba(255,255,255,.12); }

/* ---------- auth */
.auth { height: 100%; overflow: auto; display: grid; place-items: center; padding: 16px; }
.auth-card { background: var(--card); border-radius: 20px; box-shadow: var(--shadow); padding: 28px 22px; width: 100%; max-width: 380px; text-align: center; }
.auth-card h1 { margin: 8px 0 2px; font-size: 22px; }
.auth-logo { width: 64px; height: 64px; border-radius: 16px; }
.auth form { text-align: left; margin-top: 16px; display: grid; gap: 12px; }
label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
input, select, textarea { border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 11px 12px; color: var(--ink); width: 100%; }
textarea { min-height: 96px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; }

/* ---------- main layout (mobile first) */
.main { display: grid; height: 100dvh; grid-template-areas: "top" "body" "nav"; grid-template-rows: auto 1fr auto; }
.topbar { grid-area: top; background: var(--primary); color: #fff; display: flex; align-items: center; justify-content: space-between; padding: 6px 8px 6px 12px; padding-top: max(6px, env(safe-area-inset-top)); z-index: 1300; }
.brand { display: flex; align-items: center; gap: 8px; font-size: 16px; }
.brand img { width: 28px; height: 28px; border-radius: 8px; }
.top-actions { display: flex; align-items: center; gap: 2px; }
.sync { font-size: 12px; opacity: .85; margin-right: 4px; }
.tabs { grid-area: nav; display: grid; grid-template-columns: repeat(5, 1fr); background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); z-index: 1300; }
.tabs button { border: 0; background: none; padding: 7px 2px 6px; display: grid; justify-items: center; gap: 1px; font-size: 11.5px; color: var(--muted); cursor: pointer; position: relative; }
.tabs button i { font-style: normal; font-size: 19px; filter: grayscale(.6); }
.tabs button.active { color: var(--primary); font-weight: 700; }
.tabs button.active i { filter: none; }
.badge { position: absolute; top: 3px; left: 54%; background: var(--err); color: #fff; font-size: 10px; border-radius: 9px; padding: 0 5px; min-width: 16px; line-height: 16px; }
.body { grid-area: body; position: relative; min-height: 0; overflow: hidden; }
.map { position: absolute; inset: 0; z-index: 1; background: #dfe7ef; }
.panel { position: absolute; inset: 0; overflow-y: auto; background: var(--bg); z-index: 900; padding: 12px 12px 90px; }
.main[data-tab="map"] .panel { display: none; }
.main:not([data-tab="map"]) .map-ui { display: none; }
.map-ui > * { z-index: 800; }
.map-ui #map-filters { position: absolute; top: 8px; left: 8px; right: 8px; }
.map-buttons { position: absolute; right: 10px; bottom: 96px; display: grid; gap: 8px; }
.fab-sm { width: 42px; height: 42px; border-radius: 50%; border: 0; background: var(--card); box-shadow: var(--shadow); font-size: 18px; cursor: pointer; color: var(--ink); }
.fab { position: absolute; right: 10px; bottom: 24px; width: 58px; height: 58px; border-radius: 50%; border: 0; background: var(--accent); color: #111; font-size: 30px; box-shadow: var(--shadow); cursor: pointer; }
.active-visit { position: absolute; left: 8px; right: 66px; bottom: 24px; background: var(--ok); color: #fff; border-radius: 14px; padding: 10px 12px; box-shadow: var(--shadow); display: flex; gap: 8px; align-items: center; justify-content: space-between; }
.active-visit .btn { background: #fff; color: var(--ok); border: 0; font-weight: 700; }

/* desktop: tabs in the top bar, list panel on the left, map on the right */
@media (min-width: 900px) {
  .main { grid-template-areas: "top nav" "body body"; grid-template-columns: auto 1fr; grid-template-rows: auto 1fr; }
  .topbar { width: 420px; padding-right: 12px; gap: 12px; }
  .tabs { background: var(--primary); border: 0; display: flex; justify-content: flex-end; padding: 0 8px; }
  .tabs button { color: rgba(255,255,255,.75); grid-auto-flow: column; gap: 6px; font-size: 14px; padding: 0 14px; }
  .tabs button.active { color: #fff; box-shadow: inset 0 -3px 0 var(--accent); }
  .tabs button i { font-size: 16px; }
  .badge { top: 8px; left: auto; right: 2px; }
  .body { display: grid; grid-template-columns: 420px 1fr; }
  .panel, .main[data-tab="map"] .panel { position: relative; display: block; inset: auto; border-right: 1px solid var(--line); padding-bottom: 24px; }
  .map { position: relative; inset: auto; grid-column: 2; grid-row: 1; }
  .panel { grid-column: 1; grid-row: 1; }
  .map-ui { position: absolute; left: 420px; right: 0; top: 0; bottom: 0; pointer-events: none; }
  .map-ui > * { pointer-events: auto; }
  .main:not([data-tab="map"]) .map-ui { display: block; }
  .map-ui #map-filters { display: none; }
  .map-buttons { bottom: 100px; }
  .active-visit { right: 80px; left: auto; width: 380px; }
}

/* ---------- cards & lists */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; margin-bottom: 12px; }
.card h3, .panel h2 { margin: 0 0 8px; font-size: 16px; }
.panel h2 { font-size: 18px; margin: 4px 2px 10px; }
.hint { background: var(--soft); border-radius: 12px; padding: 10px 12px; font-size: 14px; margin-bottom: 12px; }
.warn { background: var(--warn-bg); color: var(--warn-ink); border-radius: 10px; padding: 8px 10px; font-size: 13.5px; margin: 8px 0; }
.warn div + div { margin-top: 3px; }
.row { display: flex; gap: 10px; align-items: center; padding: 10px 4px; border-bottom: 1px solid var(--line); cursor: pointer; }
.row:last-child { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.row .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .sub { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .side { text-align: right; font-size: 12.5px; color: var(--muted); white-space: nowrap; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; background: var(--c, var(--s-new)); box-shadow: 0 0 0 2px var(--card), 0 0 0 3px var(--c, var(--s-new)); }
.check { width: 26px; height: 26px; flex: none; border-radius: 8px; border: 2px solid var(--line); background: var(--card); display: grid; place-items: center; font-weight: 800; color: #fff; cursor: pointer; }
.check.on { background: var(--primary); border-color: var(--primary); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chips.scroll { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
.chips.scroll::-webkit-scrollbar { display: none; }
.chip { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 7px 12px; font-size: 13.5px; cursor: pointer; white-space: nowrap; box-shadow: 0 1px 2px rgba(0,0,0,.05); }
.chip.on { background: var(--primary); color: #fff; border-color: var(--primary); }
.chip.st { --c: var(--s-new); border-color: var(--c); color: var(--c); background: var(--card); font-weight: 600; padding: 3px 10px; font-size: 12.5px; cursor: default; box-shadow: none; }
select.chip { width: auto; padding-right: 26px; }
.tag { display: inline-block; font-size: 12px; background: var(--soft); border-radius: 6px; padding: 1px 7px; margin: 0 4px 4px 0; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px; text-align: center; }
.stat b { display: block; font-size: 22px; }
.stat span { font-size: 12px; color: var(--muted); }
.bar { height: 6px; border-radius: 4px; background: var(--line); overflow: hidden; margin-top: 4px; }
.bar i { display: block; height: 100%; background: var(--ok); }
.zone { border: 1px solid var(--line); border-radius: 12px; background: var(--card); margin-bottom: 8px; overflow: hidden; }
.zone > summary { list-style: none; padding: 11px 12px; cursor: pointer; display: flex; gap: 10px; align-items: center; }
.zone > summary::-webkit-details-marker { display: none; }
.zone .zone-body { padding: 0 10px 6px; border-top: 1px solid var(--line); }
.zone.current { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; }
.num { width: 26px; height: 26px; border-radius: 50%; background: var(--soft); display: grid; place-items: center; font-size: 12px; font-weight: 700; flex: none; }
.search { margin-bottom: 10px; }
.empty { text-align: center; color: var(--muted); padding: 24px 8px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.history .v { border-left: 3px solid var(--c, var(--line)); padding: 4px 0 6px 10px; margin: 8px 0; font-size: 14px; }

/* ---------- map pins */
.pin-wrap { background: none; border: 0; }
.pin { --c: var(--s-new); width: 28px; height: 28px; border-radius: 50% 50% 50% 4px; transform: rotate(-45deg); background: var(--c); border: 2px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.35); display: grid; place-items: center; }
.pin span { transform: rotate(45deg); color: #fff; font-size: 12px; font-weight: 800; line-height: 1; }
.pin.approx { opacity: .78; }
.pin.planned { width: 36px; height: 36px; border: 3px solid var(--accent); }
.pin.planned span { font-size: 14px; }
.me-dot { width: 18px; height: 18px; border-radius: 50%; background: #2563eb; border: 3px solid #fff; box-shadow: 0 0 0 6px rgba(37,99,235,.25); }
.leaflet-container { font: inherit; }
.leaflet-popup-content { margin: 10px 12px; }

/* ---------- sheet (bottom on phone, right drawer on desktop) */
.backdrop { position: fixed; inset: 0; background: rgba(15,23,42,.35); z-index: 1400; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; max-height: 90dvh; overflow-y: auto; background: var(--card); border-radius: 20px 20px 0 0; z-index: 1500; box-shadow: 0 -8px 30px rgba(0,0,0,.2); padding: 6px 16px calc(18px + env(safe-area-inset-bottom)); animation: up .18s ease-out; }
@keyframes up { from { transform: translateY(40px); opacity: .4; } to { transform: none; opacity: 1; } }
.sheet-grip { width: 42px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 10px; }
.sheet-head { display: flex; align-items: flex-start; gap: 8px; }
.sheet-head h2 { margin: 0; font-size: 19px; flex: 1; }
.sheet section { margin-top: 14px; }
.sheet h4 { margin: 0 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.form-grid { display: grid; gap: 12px; }
.choice { display: flex; flex-wrap: wrap; gap: 8px; }
.choice .chip { padding: 9px 13px; font-size: 14px; }
.timer { font-variant-numeric: tabular-nums; }
@media (min-width: 900px) {
  .backdrop { background: rgba(15,23,42,.18); }
  .sheet { left: auto; top: 0; bottom: 0; width: 460px; max-height: none; border-radius: 0; padding-top: 16px; animation: side .18s ease-out; }
  .sheet-grip { display: none; }
  @keyframes side { from { transform: translateX(40px); opacity: .4; } to { transform: none; opacity: 1; } }
}

/* ---------- toast */
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(84px + env(safe-area-inset-bottom)); background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 12px; z-index: 2000; max-width: calc(100% - 32px); box-shadow: var(--shadow); font-size: 14px; }
@media (min-width: 900px) { .toast { bottom: 28px; } }
