*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f5f6f8;
  --card-bg: #fff;
  --border: #e2e5ea;
  --text: #1a1d23;
  --text-muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --tag-bg: #eff6ff;
  --tag-text: #1e40af;
  --tag-border: #bfdbfe;
  --success: #059669;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --shadow-lg: 0 4px 12px rgba(0,0,0,.1);
}

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  -webkit-text-size-adjust: 100%;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

/* Header / Search */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: #fff;
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.header-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px 20px;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 12px;
}

.logo {
  font-size: 20px;
  font-weight: 700;
  color: var(--primary);
  white-space: nowrap;
}

.stats {
  font-size: 13px;
  color: var(--text-muted);
  white-space: nowrap;
}

.search-row {
  display: flex;
  gap: 10px;
}

.search-box {
  flex: 1;
  position: relative;
}

.search-box svg {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  width: 18px;
  height: 18px;
  color: var(--text-muted);
  pointer-events: none;
}

#search {
  width: 100%;
  padding: 11px 14px 11px 42px;
  font-size: 16px; /* >= 16 voorkomt iOS zoom on focus */
  border: 2px solid var(--border);
  border-radius: var(--radius);
  outline: none;
  transition: border-color .15s;
  background: #fff;
  font-family: inherit;
  appearance: none;
  -webkit-appearance: none;
}

#search:focus { border-color: var(--primary); }
#search::placeholder { color: #9ca3af; }
#search::-webkit-search-decoration,
#search::-webkit-search-cancel-button,
#search::-webkit-search-results-button,
#search::-webkit-search-results-decoration { display: none; }

.filter-btn {
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 500;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  cursor: pointer;
  color: var(--text);
  white-space: nowrap;
  transition: border-color .15s, background .15s;
  font-family: inherit;
}

.filter-btn:hover { border-color: var(--primary); }
.filter-btn.active { border-color: var(--primary); background: var(--tag-bg); color: var(--primary); }

/* Filter panel */
.filter-panel {
  display: none;
  padding: 12px 0 0;
}

.filter-panel.open { display: block; }

.filter-panel > label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  display: block;
  margin-bottom: 6px;
}

.filter-select {
  width: 100%;
  padding: 8px 12px;
  font-size: 14px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  outline: none;
  cursor: pointer;
  font-family: inherit;
}

.filter-select:focus { border-color: var(--primary); }

.filter-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

/* Groep autocomplete */
.autocomplete-wrap {
  position: relative;
}

.autocomplete-wrap label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  display: block;
  margin-bottom: 6px;
}

.autocomplete-input-wrap {
  position: relative;
}

.autocomplete-input {
  width: 100%;
  padding: 8px 34px 8px 12px;
  font-size: 14px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  outline: none;
  font-family: inherit;
}

.autocomplete-input:focus { border-color: var(--primary); }
.autocomplete-input::placeholder { color: #9ca3af; }

.autocomplete-clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 20px;
  height: 20px;
  border: none;
  background: #e5e7eb;
  border-radius: 50%;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
  line-height: 1;
  padding: 0;
}

.autocomplete-clear:hover { background: #d1d5db; }
.autocomplete-clear.visible { display: flex; }

.autocomplete-list {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 200;
  background: #fff;
  border: 2px solid var(--primary);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  max-height: 260px;
  overflow-y: auto;
  display: none;
  box-shadow: var(--shadow-lg);
  -webkit-overflow-scrolling: touch;
}

.autocomplete-list.open { display: block; }

.autocomplete-item {
  padding: 8px 12px;
  font-size: 13px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.autocomplete-item:hover,
.autocomplete-item.highlighted { background: var(--tag-bg); }

.autocomplete-item .count {
  font-size: 11px;
  color: var(--text-muted);
  background: #f3f4f6;
  padding: 1px 7px;
  border-radius: 10px;
}

.autocomplete-selected-tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
  padding: 4px 10px;
  font-size: 13px;
  font-weight: 500;
  background: var(--tag-bg);
  color: var(--tag-text);
  border: 1px solid var(--tag-border);
  border-radius: 14px;
}

.autocomplete-selected-tag button {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--tag-text);
  font-size: 15px;
  font-weight: 700;
  line-height: 1;
  padding: 0;
  opacity: .6;
}

.autocomplete-selected-tag button:hover { opacity: 1; }

/* Main content */
.main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px;
}

/* Contact cards */
.contact-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: box-shadow .15s;
}

.contact-card:hover { box-shadow: var(--shadow-lg); }

.card-header {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  padding: 16px 18px;
  cursor: pointer;
  user-select: none;
}

.card-header:hover { background: #f9fafb; }

.avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  flex-shrink: 0;
}

.card-info { flex: 1; min-width: 0; }

.card-name {
  font-size: 16px;
  font-weight: 600;
  line-height: 1.3;
}

.card-company {
  font-size: 13px;
  color: var(--text-muted);
}

.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 6px;
  font-size: 13px;
  color: var(--text-muted);
}

.card-meta span {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.card-meta svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

.card-badges {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
  align-items: flex-start;
  padding-top: 2px;
}

.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 12px;
  white-space: nowrap;
}

.badge-groups { background: #eff6ff; color: #1e40af; }
.badge-docs { background: #fef3c7; color: #92400e; }
.badge-notes { background: #ecfdf5; color: #065f46; }

.chevron {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  color: var(--text-muted);
  transition: transform .2s;
  align-self: center;
}

.contact-card.expanded .chevron { transform: rotate(180deg); }

/* Card details */
.card-details {
  display: none;
  border-top: 1px solid var(--border);
  padding: 18px;
  background: #fafbfc;
}

.contact-card.expanded .card-details { display: block; }

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.detail-item label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .05em;
  display: block;
  margin-bottom: 2px;
}

.detail-item span {
  font-size: 14px;
  word-break: break-word;
}

.detail-item a {
  color: var(--primary);
  text-decoration: none;
}

.detail-item a:hover { text-decoration: underline; }

.detail-section {
  margin-top: 16px;
}

.detail-section-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tag {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 14px;
  background: var(--tag-bg);
  color: var(--tag-text);
  border: 1px solid var(--tag-border);
  cursor: pointer;
  transition: background .15s;
}

.tag:hover { background: #dbeafe; }

.doc-list, .note-list {
  list-style: none;
  font-size: 13px;
}

.doc-list li {
  padding: 8px 0;
  border-bottom: 1px solid #eee;
  display: flex;
  align-items: center;
  gap: 8px;
}

.doc-info {
  flex: 1;
  min-width: 0;
}

.doc-name {
  word-break: break-all;
  font-weight: 500;
}

.doc-path {
  font-size: 11px;
  color: var(--text-muted);
  word-break: break-all;
  margin-top: 2px;
}

.btn-copy {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  font-size: 11px;
  font-weight: 600;
  color: var(--primary);
  background: var(--tag-bg);
  border: 1px solid var(--tag-border);
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
  transition: all .15s;
  font-family: inherit;
}

.btn-copy:hover { background: #dbeafe; }
.btn-copy.copied { background: #ecfdf5; color: var(--success); border-color: #a7f3d0; }

.btn-copy svg { width: 13px; height: 13px; }

.note-list li {
  padding: 8px 0;
  border-bottom: 1px solid #eee;
}

.note-text {
  margin-top: 4px;
  font-size: 14px;
  color: var(--text);
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.doc-list li:last-child, .note-list li:last-child { border-bottom: none; }

.doc-date { color: var(--text-muted); white-space: nowrap; flex-shrink: 0; font-size: 12px; }

/* Delete button */
.card-actions {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
}

.btn-delete {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 600;
  color: var(--danger);
  background: #fff;
  border: 1.5px solid var(--danger);
  border-radius: 8px;
  cursor: pointer;
  transition: all .15s;
  font-family: inherit;
}

.btn-delete:hover {
  background: var(--danger);
  color: #fff;
}

.btn-delete svg { width: 15px; height: 15px; }

/* Modal */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 1000;
  align-items: center;
  justify-content: center;
}

.modal-overlay.open { display: flex; }

.modal {
  background: #fff;
  border-radius: 14px;
  padding: 28px;
  max-width: 420px;
  width: 90%;
  box-shadow: 0 20px 60px rgba(0,0,0,.2);
  padding-bottom: max(28px, env(safe-area-inset-bottom));
}

.modal h3 { font-size: 18px; margin-bottom: 8px; }
.modal p { font-size: 14px; color: var(--text-muted); margin-bottom: 20px; }
.modal-name { font-weight: 700; color: var(--text); }

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.btn {
  padding: 9px 20px;
  font-size: 14px;
  font-weight: 600;
  border-radius: 8px;
  cursor: pointer;
  border: none;
  transition: all .15s;
  font-family: inherit;
}

.btn-cancel { background: var(--bg); color: var(--text); }
.btn-cancel:hover { background: #e5e7eb; }
.btn-confirm-delete { background: var(--danger); color: #fff; }
.btn-confirm-delete:hover { background: var(--danger-hover); }

/* Toast */
.toast {
  position: fixed;
  bottom: max(24px, calc(env(safe-area-inset-bottom) + 24px));
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: #1f2937;
  color: #fff;
  padding: 12px 24px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 500;
  box-shadow: 0 8px 30px rgba(0,0,0,.2);
  z-index: 2000;
  opacity: 0;
  transition: transform .3s, opacity .3s;
  display: flex;
  align-items: center;
  gap: 12px;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

.toast-undo {
  color: #60a5fa;
  font-weight: 700;
  cursor: pointer;
  background: none;
  border: none;
  font-size: 14px;
  font-family: inherit;
}

.toast-undo:hover { text-decoration: underline; }

/* Loading / Empty */
.loading { text-align: center; padding: 60px 20px; color: var(--text-muted); font-size: 16px; }

.empty-state { text-align: center; padding: 60px 20px; color: var(--text-muted); }
.empty-state svg { width: 48px; height: 48px; margin-bottom: 12px; opacity: .4; }
.empty-state p { font-size: 15px; }

/* Highlight */
mark { background: #fef08a; color: inherit; padding: 0 1px; border-radius: 2px; }

/* Responsive */
@media (max-width: 640px) {
  .header-inner { padding: 12px 14px; }
  .header-top { flex-direction: column; align-items: flex-start; gap: 4px; }
  .card-header { padding: 12px 14px; gap: 10px; }
  .card-details { padding: 14px; }
  .detail-grid { grid-template-columns: 1fr; }
  .filter-row { grid-template-columns: 1fr; }
  .card-badges { display: none; }
  .avatar { width: 38px; height: 38px; font-size: 14px; }
  .card-name { font-size: 15px; }
}

* { -webkit-tap-highlight-color: transparent; }
input, textarea { -webkit-user-select: text; user-select: text; }

/* ── Login ── */
.login { padding: 32px 16px; }
.login[hidden] { display: none; }
.login-card {
  max-width: 380px; margin: 0 auto; padding: 24px; background: var(--card-bg);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 1px 3px rgba(0,0,0,.06); display: flex; flex-direction: column; gap: 8px;
}
.login-card h2 { margin: 0 0 4px; font-size: 1.2rem; }
.login-card p { margin: 0 0 8px; color: var(--text-muted); font-size: .9rem; line-height: 1.4; }
.login-card label { font-size: .85rem; font-weight: 600; margin-top: 4px; }
.login-card input {
  font: inherit; font-size: 16px; padding: 10px 12px; border: 2px solid var(--border);
  border-radius: var(--radius); outline: none; background: #fff;
}
.login-card input:focus { border-color: var(--primary); }
.btn-login { margin-top: 12px; background: var(--primary); color: #fff; }
.btn-login:hover { background: var(--primary-hover); }
.btn-login:disabled { opacity: .6; }
.login-error { color: var(--danger); font-size: .9rem; min-height: 1.2em; }
body.logged-out .search-row, body.logged-out .filter-panel, body.logged-out .stats { display: none; }
.filter-footer { margin-top: 12px; text-align: right; }
.logout-btn {
  font: inherit; font-size: .85rem; background: none; border: none; color: var(--text-muted);
  text-decoration: underline; cursor: pointer; padding: 4px;
}
