:root {
  --c-bg: #f5f6f8;
  --c-surface: #ffffff;
  --c-border: #e2e5ea;
  --c-text: #1f2937;
  --c-muted: #6b7280;
  --c-primary: #2563eb;
  --c-accent: #2563eb;
  --c-danger: #dc2626;
  --c-ok: #16a34a;
  --c-warn: #d97706;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', 'PingFang HK', 'Microsoft JhengHei', sans-serif;
  background: var(--c-bg);
  color: var(--c-text);
  font-size: 15px;
}
a { color: var(--c-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Header bar (Una standard) ─────────────────────────────────────────── */
header.topbar {
  position: sticky; top: 0; z-index: 50;
  display: flex; align-items: center; justify-content: space-between;
  background: var(--c-surface);
  border-bottom: 1px solid var(--c-border);
  padding: 0 16px; height: 52px; gap: 12px;
}
.topbar .app-name { font-weight: 700; font-size: 1.05rem; white-space: nowrap; }
.topbar .right { display: flex; align-items: center; gap: 10px; min-width: 0; }
.ai-badge {
  font-size: .75rem; padding: 2px 8px; border-radius: 999px;
  background: #eef2ff; color: #4338ca; white-space: nowrap;
}
.user-email { color: var(--c-muted); font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.btn-icon {
  background: transparent; border: 1.5px solid var(--c-border); color: var(--c-muted);
  width: 28px; height: 28px; border-radius: 50%; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; padding: 0;
  font-size: .9rem; transition: border-color .15s, color .15s;
}
.btn-icon:hover { border-color: var(--c-accent); color: var(--c-accent); }

/* ── Buttons / forms ───────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--c-border); border-radius: 8px;
  background: var(--c-surface); color: var(--c-text);
  padding: 8px 14px; font-size: .88rem; font-weight: 500;
  cursor: pointer; font-family: inherit; white-space: nowrap;
  transition: background .15s, border-color .15s;
}
.btn:hover { border-color: #9ca3af; }
.btn.primary { background: var(--c-primary); border-color: var(--c-primary); color: #fff; }
.btn.primary:hover { background: #1d4ed8; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--c-danger); border-color: #fecaca; }
.btn.danger:hover { background: #fef2f2; }
.btn.small { padding: 4px 10px; font-size: .8rem; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.field { margin-bottom: 12px; }
.field-label { display: block; font-size: .8rem; font-weight: 600; color: var(--c-muted); margin-bottom: 4px; }
.req { color: var(--c-danger); }
.input, select.input, textarea.input {
  width: 100%; padding: 8px 10px; border: 1px solid var(--c-border); border-radius: 8px;
  font-size: .9rem; font-family: inherit; background: var(--c-surface); color: var(--c-text);
}
.input:focus { outline: 2px solid #bfdbfe; border-color: var(--c-primary); }
textarea.input { resize: vertical; min-height: 70px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.form-grid .full { grid-column: 1 / -1; }
.error-box {
  background: #fef2f2; border: 1px solid #fecaca; color: var(--c-danger);
  border-radius: 8px; padding: 8px 12px; font-size: .85rem; margin-bottom: 10px;
}

/* ── Layout ────────────────────────────────────────────────────────────── */
main.wrap { max-width: 1280px; margin: 0 auto; padding: 16px; }

/* Row 1: view switcher (left) + actions (right) */
.toolbar-top {
  display: flex; justify-content: space-between; align-items: center;
  gap: 12px; flex-wrap: wrap; margin-bottom: 12px;
}
.view-tabs {
  display: flex; background: #e9edf3; border-radius: 10px; padding: 3px; gap: 2px; flex-wrap: wrap;
}
.vtab {
  border: none; background: transparent; padding: 7px 14px; border-radius: 8px;
  font-size: .87rem; font-weight: 500; color: var(--c-muted); cursor: pointer; font-family: inherit;
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.vtab.active { background: var(--c-surface); color: var(--c-text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.toolbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Row 2: filters for the current view */
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 12px; }
.toolbar .search-box { flex: 1 1 240px; position: relative; }
.toolbar .search-box .input { padding-left: 34px; }
.toolbar .search-box::before { content: '🔍'; position: absolute; left: 10px; top: 8px; font-size: .85rem; opacity: .6; }

.tabs { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 14px; }
.tab {
  border: 1px solid var(--c-border); background: var(--c-surface); border-radius: 999px;
  padding: 5px 13px; font-size: .84rem; cursor: pointer; color: var(--c-muted); font-family: inherit;
  display: inline-flex; align-items: center; gap: 6px;
}
.tab.active { background: var(--c-primary); border-color: var(--c-primary); color: #fff; }
.tab .count { font-size: .72rem; background: rgba(0,0,0,.08); border-radius: 999px; padding: 1px 7px; }
.tab.active .count { background: rgba(255,255,255,.25); }
.tab .pending-badge { background: var(--c-danger); color: #fff; border-radius: 999px; font-size: .7rem; padding: 1px 6px; }

/* ── Table (desktop) / cards (mobile) ──────────────────────────────────── */
.list-panel { background: var(--c-surface); border: 1px solid var(--c-border); border-radius: 12px; overflow: hidden; }
.table-scroll { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: .87rem; }
table.data th {
  text-align: left; padding: 10px 12px; color: var(--c-muted); font-size: .74rem;
  text-transform: uppercase; letter-spacing: .04em; border-bottom: 1px solid var(--c-border);
  background: #fafbfc; white-space: nowrap;
}
table.data td { padding: 10px 12px; border-bottom: 1px solid #f0f1f4; vertical-align: top; }
th.th-sort { cursor: pointer; user-select: none; transition: color .12s; }
th.th-sort:hover { color: var(--c-primary); }
th.th-sort.sorted { color: var(--c-primary); }

/* Contacts card view — sort bar (columns aren't available in the card layout) */
.sort-bar { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.sort-lbl { font-size: .8rem; color: var(--c-muted); margin-right: 2px; }
.sort-chip {
  border: 1px solid var(--c-border); background: var(--c-surface); border-radius: 999px;
  padding: 4px 12px; font-size: .82rem; cursor: pointer; color: var(--c-muted); font-family: inherit;
}
.sort-chip:hover { border-color: #9ca3af; color: var(--c-text); }
.sort-chip.active { background: var(--c-primary); border-color: var(--c-primary); color: #fff; }
table.data tr.rowlink { cursor: pointer; }
table.data tr.rowlink:hover { background: #f8faff; }
table.data td .sub { color: var(--c-muted); font-size: .78rem; margin-top: 2px; }
.empty-state { padding: 40px 20px; text-align: center; color: var(--c-muted); }
.pager { display: flex; align-items: center; gap: 10px; justify-content: center; padding: 12px; color: var(--c-muted); font-size: .84rem; }

.chip {
  display: inline-block; font-size: .72rem; border-radius: 999px; padding: 2px 9px;
  background: #eef2ff; color: #4338ca; white-space: nowrap;
}
.chip.src-excel { background: #f0fdf4; color: #15803d; }
.chip.src-namecard { background: #fff7ed; color: #c2410c; }
.chip.src-unagpt-trial { background: #ecfeff; color: #0e7490; }
.chip.src-app { background: #eef2ff; color: #4338ca; }
.chip.cat { background: #f3f4f6; color: #374151; }

/* ── Drawer (org / contact detail) ─────────────────────────────────────── */
#drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(680px, 100vw);
  background: var(--c-surface); border-left: 1px solid var(--c-border);
  box-shadow: -6px 0 24px rgba(0,0,0,.08); z-index: 900;
  transform: translateX(105%); transition: transform .2s ease; display: flex; flex-direction: column;
}
#drawer.open { transform: translateX(0); }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 14px 18px; border-bottom: 1px solid var(--c-border);
}
.drawer-head h2 { margin: 0; font-size: 1.05rem; }
.drawer-body { flex: 1; overflow-y: auto; padding: 16px 18px 24px; }
.drawer-section { margin-bottom: 22px; }
.drawer-section h3 {
  font-size: .78rem; text-transform: uppercase; letter-spacing: .05em;
  color: var(--c-muted); margin: 0 0 8px;
  display: flex; align-items: center; justify-content: space-between;
}
.kv { display: grid; grid-template-columns: 130px 1fr; gap: 4px 10px; font-size: .87rem; }
.kv .k { color: var(--c-muted); }
.contact-item, .interaction-item {
  border: 1px solid var(--c-border); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px;
}
.contact-item .name { font-weight: 600; }
.contact-open { cursor: pointer; border-radius: 6px; margin: -3px; padding: 3px; transition: background .12s; min-width: 0; }
.contact-open:hover { background: #f4f8ff; }

/* ── Contacts view — full-detail cards ─────────────────────────────────── */
.contact-list { display: flex; flex-direction: column; }
.contact-card {
  display: flex; justify-content: space-between; gap: 14px;
  padding: 14px 16px; border-bottom: 1px solid #f0f1f4;
}
.contact-card:last-child { border-bottom: none; }
.contact-card:hover { background: #fafbfc; }
.cc-body { min-width: 0; flex: 1; }
.cc-head { display: flex; align-items: baseline; flex-wrap: wrap; gap: 4px 7px; margin-bottom: 3px; }
.cc-name { font-weight: 600; font-size: 1rem; }
.cc-zh { font-weight: 500; }
.cc-title { color: var(--c-muted); font-size: .85rem; }
.cc-org { font-size: .86rem; color: var(--c-primary); margin-bottom: 7px; display: inline-block; }
.cc-org[data-org] { cursor: pointer; }
.cc-org[data-org]:hover { text-decoration: underline; }
.cc-org .sub { color: var(--c-muted); }
.cc-lines { display: flex; flex-direction: column; gap: 2px; font-size: .88rem; }
.cc-line { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cc-ic { display: inline-block; width: 1.4em; }
.cc-lbl { color: var(--c-muted); font-size: .74rem; display: inline-block; min-width: 40px; }
.cc-notes { margin-top: 7px; font-size: .82rem; color: var(--c-muted); line-height: 1.45; }
.cc-added { margin-top: 6px; font-size: .74rem; color: var(--c-muted); }
.cc-actions { display: flex; flex-direction: column; gap: 5px; flex-shrink: 0; align-items: stretch; }
.cc-actions .btn { justify-content: center; }
.focus-banner {
  padding: 9px 14px; background: #eff6ff; border: 1px solid #bfdbfe; border-radius: 8px;
  margin-bottom: 10px; font-size: .87rem; color: #1e40af;
}
.contact-item .meta, .interaction-item .meta { color: var(--c-muted); font-size: .8rem; margin-top: 2px; }
.card-gallery { display: flex; flex-wrap: wrap; gap: 8px; }
.card-gallery img {
  height: 84px; border-radius: 8px; border: 1px solid var(--c-border); cursor: zoom-in; display: block;
}

/* ── Modals ────────────────────────────────────────────────────────────── */
#modal-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 1000;
  align-items: flex-start; justify-content: center; padding: 4vh 1rem; overflow-y: auto;
}
#modal-overlay.visible { display: flex; }
.modal-box {
  background: var(--c-surface); border-radius: 12px; width: clamp(320px, 100%, 640px);
  box-shadow: 0 8px 32px rgba(0,0,0,.18); padding: 18px 20px; margin-bottom: 4vh;
}
.modal-box h2 { margin: 0 0 14px; font-size: 1.05rem; }
.modal-box.wide { width: clamp(320px, 100%, 900px); }

/* ── Card review ───────────────────────────────────────────────────────── */
.review-grid { display: grid; grid-template-columns: 340px 1fr; gap: 16px; }
.review-grid .photo img { width: 100%; border-radius: 10px; border: 1px solid var(--c-border); }
.person-block { border: 1px solid var(--c-border); border-radius: 10px; padding: 12px; margin-bottom: 12px; }
.person-block.confirmed { opacity: .55; }
.person-block .status-line { font-size: .8rem; margin-bottom: 8px; }
.match-box {
  background: #fffbeb; border: 1px solid #fde68a; border-radius: 8px;
  padding: 8px 10px; font-size: .82rem; margin-bottom: 10px;
}
.match-box label { display: block; cursor: pointer; padding: 3px 0; }
.upload-drop {
  border: 2px dashed var(--c-border); border-radius: 12px; padding: 30px 16px; text-align: center;
  color: var(--c-muted); cursor: pointer; transition: border-color .15s, background .15s;
}
.upload-drop:hover, .upload-drop.drag { border-color: var(--c-primary); background: #f8faff; }
.upload-list { margin-top: 12px; font-size: .85rem; }
.upload-list .u-item { display: flex; align-items: center; gap: 8px; padding: 4px 0; }

/* ── Toast ─────────────────────────────────────────────────────────────── */
#toast-wrap { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 1200; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast {
  background: #111827; color: #fff; border-radius: 10px; padding: 10px 16px; font-size: .86rem;
  box-shadow: 0 4px 16px rgba(0,0,0,.25); max-width: min(560px, 90vw);
}
.toast a { color: #93c5fd; }
.toast.err { background: #7f1d1d; }

/* ── Login page ────────────────────────────────────────────────────────── */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card {
  background: var(--c-surface); border: 1px solid var(--c-border); border-radius: 16px;
  padding: 36px 32px; width: min(400px, 100%); text-align: center;
  box-shadow: 0 4px 24px rgba(0,0,0,.06);
}
.login-card .logo { font-size: 2.2rem; margin-bottom: 6px; }
.login-card h1 { font-size: 1.3rem; margin: 0 0 4px; }
.login-card p { color: var(--c-muted); font-size: .88rem; margin: 0 0 22px; }
.ms-btn {
  display: inline-flex; align-items: center; gap: 10px; width: 100%; justify-content: center;
  background: #2f2f2f; color: #fff; border: none; border-radius: 8px;
  padding: 11px 16px; font-size: .92rem; cursor: pointer; font-family: inherit;
}
.ms-btn:hover { background: #1f1f1f; }

/* ── Feedback button + modal (shared Una pattern) ──────────────────────── */
.btn-feedback {
  background: transparent; border: 1.5px solid var(--c-border); color: var(--c-muted);
  width: 28px; height: 28px; border-radius: 50%; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; padding: 0;
  transition: border-color .15s, color .15s;
}
.btn-feedback:hover { border-color: var(--c-accent); color: var(--c-accent); }
.btn-feedback:disabled { opacity: .5; cursor: wait; pointer-events: none; }

.fb-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 1100;
  align-items: center; justify-content: center; padding: 1rem;
}
.fb-overlay.visible { display: flex; }
.fb-box {
  background: var(--c-surface); border-radius: 10px;
  width: clamp(320px, calc(100vw - 32px), 560px); max-height: 90vh;
  display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 8px 32px rgba(0,0,0,.18);
}
.fb-header { display: flex; align-items: center; justify-content: space-between; padding: 14px 20px; border-bottom: 1px solid var(--c-border); flex-shrink: 0; }
.fb-title { font-size: 1rem; font-weight: 600; }
.fb-body { padding: 18px 20px 20px; overflow-y: auto; flex: 1; }
.fb-type-row { display: flex; gap: .5rem; }
.fb-type-btn {
  flex: 1; padding: .55rem .75rem; border: 2px solid var(--c-border); border-radius: 8px;
  background: transparent; font-size: .88rem; font-weight: 500; cursor: pointer; font-family: inherit; color: var(--c-muted);
  display: flex; align-items: center; justify-content: center; gap: .35rem;
  transition: border-color .15s, background .15s, color .15s;
}
.fb-type-btn.selected { border-color: var(--c-primary); background: #eff6ff; color: var(--c-primary); }
.fb-type-btn:hover:not(.selected) { border-color: #9ca3af; color: var(--c-text); }
.fb-ss-wrap {
  border: 1px solid var(--c-border); border-radius: 6px; overflow: hidden;
  min-height: 56px; max-height: 180px; display: flex; align-items: center; justify-content: center;
  background: var(--c-bg); cursor: zoom-in;
}
.fb-ss-wrap img { max-width: 100%; max-height: 180px; display: block; }
.fb-ss-ph { color: var(--c-muted); font-size: .85rem; padding: 1.2rem; text-align: center; }
.guide-close-btn {
  background: transparent; border: none; font-size: 1.4rem; color: var(--c-muted);
  cursor: pointer; line-height: 1; padding: 2px 6px;
}
.guide-close-btn:hover { color: var(--c-text); }

/* ── Guide overlay ─────────────────────────────────────────────────────── */
#guide-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 1050;
  align-items: flex-start; justify-content: center; padding: 5vh 1rem; overflow-y: auto;
}
#guide-overlay.visible { display: flex; }
#guide-overlay .guide-box {
  background: var(--c-surface); border-radius: 12px; max-width: 620px; width: 100%;
  padding: 22px 26px; box-shadow: 0 8px 32px rgba(0,0,0,.18);
}
#guide-overlay h2 { margin-top: 0; }
#guide-overlay h3 { font-size: .95rem; margin: 18px 0 6px; }
#guide-overlay p, #guide-overlay li { font-size: .88rem; color: var(--c-text); line-height: 1.55; }

/* ── Responsive ────────────────────────────────────────────────────────── */
@media (max-width: 720px) {
  .review-grid { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .user-email { display: none; }
  table.data .hide-sm { display: none; }
  main.wrap { padding: 10px; }
  .kv { grid-template-columns: 110px 1fr; }
  .contact-card { flex-direction: column; }
  .cc-actions { flex-direction: row; flex-wrap: wrap; }
}
