:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #1c2430;
  --muted: #667085;
  --line: #e4e7ec;
  --primary: #1d4ed8;
  --primary-press: #1e40af;
  --danger: #dc2626;
  --wa: #16a34a;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --topbar-h: 56px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  min-height: 100dvh;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--primary); text-decoration: none; }
img { max-width: 100%; }
[hidden] { display: none !important; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-press); }
.btn-secondary { background: #fff; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-danger-ghost { background: #fff5f5; border-color: #fecaca; color: var(--danger); }
.btn-block { width: 100%; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-height: 36px;
  padding: 6px 10px;
  border-radius: 999px;
  background: #eef2ff;
  color: #1e3a8a;
  font-size: 13px;
  font-weight: 600;
}
.chip-wa { background: #dcfce7; color: #166534; }
.chip-edit { background: #f2f4f7; color: #344054; }
.badge-int {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
  margin-left: 6px;
  vertical-align: middle;
}

/* Login */
.page-login { background: #eef1f6; }
.login-wrap {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(400px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 28px 22px;
  display: grid;
  gap: 14px;
}
.login-card h1 { margin: 0; font-size: 22px; }
.login-card .muted { margin: -6px 0 4px; color: var(--muted); font-size: 14px; }
.login-card label { display: grid; gap: 6px; font-size: 14px; font-weight: 600; }
.login-card input {
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 16px;
}
.alert {
  background: #fef2f2;
  color: #991b1b;
  border: 1px solid #fecaca;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
}

/* Fixed top navigation */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 600;
  min-height: calc(var(--topbar-h) + var(--safe-top));
  padding: calc(6px + var(--safe-top)) 10px 6px;
  background: rgba(255,255,255,0.97);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 1px 0 rgba(15,23,42,0.04);
}
.topbar-nav { gap: 4px; }
.topnav-link {
  padding: 8px 10px;
  border-radius: 8px;
  color: var(--muted);
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
  flex: 0 0 auto;
}
.topnav-link.active { background: #eef2ff; color: var(--primary); }
.topnav-search {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
}
.topnav-search input {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 16px;
  background: #fff;
}
.topbar-edit .topbar-title {
  flex: 1;
  text-align: center;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 15px;
}
.back-arrow {
  font-size: 28px;
  font-weight: 800;
  line-height: 1;
  color: var(--text);
  padding: 4px 10px;
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 0;
  text-decoration: none;
}

/* List */
.list-page, .edit-page {
  max-width: 960px;
  margin: 0 auto;
  padding: calc(var(--topbar-h) + var(--safe-top) + 12px) 12px calc(24px + var(--safe-bottom));
}
.page-edit .edit-page {
  padding-top: calc(var(--topbar-h) + var(--safe-top) + 12px);
}
.filters {
  display: grid;
  gap: 8px;
  grid-template-columns: 1fr;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 10px;
}
.filters select {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 16px;
  background: #fff;
}
.filters .check {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  font-size: 14px;
  font-weight: 600;
}
.list-meta { color: var(--muted); font-size: 13px; margin: 4px 2px 8px; }
.obj-list { display: grid; gap: 8px; }
.obj-row {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
}
.obj-name {
  display: inline-flex;
  align-items: center;
  color: var(--text);
  font-weight: 700;
  font-size: 16px;
  line-height: 1.3;
}
.obj-sub {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
  color: var(--muted);
  font-size: 13px;
  margin-top: 4px;
}
.obj-sub .addr { flex: 1 1 100%; }
.obj-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}
.empty { color: var(--muted); text-align: center; padding: 30px 10px; }

/* Edit form */
.card-form, .interview-section {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 14px;
  display: grid;
  gap: 12px;
}
.card-form label, .interview-body label {
  display: grid;
  gap: 6px;
  font-size: 14px;
  font-weight: 600;
}
.card-form input, .card-form select, .card-form textarea,
.interview-body textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 16px;
  font-family: inherit;
  background: #fff;
  color: var(--text);
}
.card-form textarea, .interview-body textarea { resize: vertical; min-height: 72px; }
.hint { color: var(--muted); font-weight: 400; font-size: 12px; }
.coords-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.action-row { display: flex; flex-wrap: wrap; gap: 8px; }
.contact-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.photo-block { display: grid; gap: 8px; }
.photo-preview {
  width: 100%;
  max-height: 240px;
  object-fit: cover;
  border-radius: 12px;
  background: #f2f4f7;
}
.photo-empty {
  min-height: 120px;
  display: grid;
  place-items: center;
  background: #f2f4f7;
  color: var(--muted);
  border-radius: 12px;
  font-size: 14px;
}
.photo-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.file-btn { cursor: pointer; }
.inline-link { font-size: 14px; }
.sticky-save {
  position: sticky;
  bottom: 0;
  padding: 10px 0 calc(4px + var(--safe-bottom));
  background: linear-gradient(180deg, rgba(255,255,255,0), #fff 30%);
}
.interview-section { margin-top: 12px; }
.badge { font-size: 12px; margin-left: 6px; }

/* Interview overlay */
.interview-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: stretch;
  justify-content: center;
}
.interview-overlay[hidden] { display: none !important; }
body.interview-open { overflow: hidden; }
.interview-panel {
  background: #fff;
  width: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}
.interview-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(8px + var(--safe-top)) 8px 8px 12px;
  border-bottom: 1px solid var(--line);
  background: #fff;
  flex: 0 0 auto;
}
.interview-title {
  flex: 1;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.interview-close {
  width: 44px;
  height: 44px;
  border: 0;
  background: #f2f4f7;
  border-radius: 12px;
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
}
.interview-body {
  flex: 1;
  overflow: auto;
  padding: 14px 12px calc(18px + var(--safe-bottom));
  display: grid;
  gap: 14px;
  -webkit-overflow-scrolling: touch;
}
.interview-footer {
  position: sticky;
  bottom: 0;
  display: grid;
  grid-template-columns: 1fr 1.4fr;
  gap: 8px;
  padding-top: 8px;
  background: linear-gradient(180deg, rgba(255,255,255,0), #fff 20%);
}

/* Map */
.page-map { overflow: hidden; background: #d9e2ec; }
#map {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100dvh;
  z-index: 1;
}
.map-btn {
  position: fixed;
  z-index: 700;
  width: 52px;
  height: 52px;
  border-radius: 16px;
  border: 0;
  background: transparent;
  color: #0f172a;
  display: grid;
  place-items: center;
  font-size: 28px;
  font-weight: 800;
  cursor: pointer;
}
.map-btn-exit {
  top: calc(10px + var(--safe-top));
  left: 8px;
  text-shadow: 0 1px 2px rgba(255,255,255,0.9), 0 0 8px rgba(255,255,255,0.8);
  color: #0f172a;
  box-shadow: none;
}
.map-btn-add {
  top: calc(10px + var(--safe-top));
  right: 10px;
  width: 52px;
  height: 52px;
  border-radius: 16px;
  background: #fff;
  color: var(--primary);
  box-shadow: 0 4px 16px rgba(15,23,42,0.18);
}
.map-add-ui { position: fixed; inset: 0; z-index: 650; pointer-events: none; }
.map-center-pin {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -100%);
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.35));
  pointer-events: none;
}
.map-hint {
  position: absolute;
  left: 50%;
  top: calc(70px + var(--safe-top));
  transform: translateX(-50%);
  background: #0f172a;
  color: #fff;
  padding: 10px 14px;
  border-radius: 999px;
  font-size: 14px;
  max-width: calc(100vw - 24px);
  text-align: center;
  pointer-events: none;
}
.map-add-actions {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(28px + var(--safe-bottom));
  display: flex;
  justify-content: center;
  gap: 18px;
  pointer-events: auto;
}
.map-btn-cancel,
.map-btn-ok {
  position: static;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  font-size: 28px;
  box-shadow: 0 6px 18px rgba(15,23,42,0.22);
}
.map-btn-cancel {
  background: #fff;
  color: var(--danger);
  border: 2px solid #fecaca;
}
.map-btn-ok {
  background: var(--primary);
  color: #fff;
  border: 2px solid var(--primary);
}

.search-pop {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 6px);
  z-index: 620;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 12px;
  max-height: min(50dvh, 360px);
  overflow: auto;
  box-shadow: 0 10px 30px rgba(15,23,42,0.15);
}
.page-map .search-pop {
  position: fixed;
  left: 10px;
  right: 10px;
  top: calc(var(--topbar-h) + var(--safe-top) + 6px);
}
.search-pop a {
  display: block;
  padding: 12px;
  border-bottom: 1px solid var(--line);
  color: var(--text);
  font-size: 14px;
}
.search-pop a:last-child { border-bottom: 0; }
.search-pop .meta { color: var(--muted); font-size: 12px; margin-top: 2px; }
.search-empty { padding: 12px; color: var(--muted); font-size: 14px; }

/* Leaflet markers */
.crm-pin { background: transparent; border: 0; }
.crm-pin-inner {
  width: 28px;
  height: 28px;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  background: #2563eb;
  border: 2px solid #fff;
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
  position: relative;
}
.crm-pin-dot {
  position: absolute;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #ef4444;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(45deg);
  box-shadow: 0 0 0 2px rgba(255,255,255,0.85);
  display: none;
}
.crm-pin-inner.interviewed .crm-pin-dot { display: block; }
.crm-pin-inner.focused {
  box-shadow: 0 0 0 3px #fff, 0 0 0 6px #1d4ed8, 0 2px 8px rgba(0,0,0,0.3);
}
.leaflet-container a.crm-pin,
.crm-pin.pin-focus .crm-pin-inner,
.leaflet-marker-icon.pin-focus .crm-pin-inner {
  animation: pinPulse 1.4s ease-in-out infinite;
}
@keyframes pinPulse {
  0%, 100% { filter: drop-shadow(0 0 0 rgba(29,78,216,0.7)); }
  50% { filter: drop-shadow(0 0 10px rgba(29,78,216,0.95)); }
}
.leaflet-popup-content { margin: 10px 12px; font-size: 14px; }
.leaflet-popup-content h3 { margin: 0 0 4px; font-size: 15px; }
.popup-actions { margin-top: 8px; }

/* toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 2000;
  background: #0f172a;
  color: #fff;
  padding: 12px 18px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(15,23,42,0.25);
}

@media (min-width: 720px) {
  .filters {
    grid-template-columns: 1fr auto auto auto;
    align-items: center;
  }
  .interview-overlay { align-items: center; padding: 24px; }
  .interview-panel {
    width: min(920px, 100%);
    height: min(90vh, 900px);
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 20px 50px rgba(15,23,42,0.25);
  }
  .obj-row .obj-main {
    display: grid;
    grid-template-columns: 2fr 1.2fr;
    gap: 8px 16px;
    align-items: start;
  }
  .obj-actions { grid-column: 2; justify-content: flex-end; margin-top: 0; }
  .topnav-link { font-size: 15px; padding: 8px 12px; }
}
