:root {
  --bg: #f5f6f8;
  --panel: #fff;
  --text: #1d232b;
  --muted: #6b7480;
  --line: #dfe3e8;
  --brand: #1f3a5f;
  --accent: #2f6fd6;
  --accent-soft: #e6effd;
  --danger: #c7372f;
  --danger-soft: #fdecea;
  --warn: #b26a00;
  --warn-soft: #fff4e0;
  --ok: #1f7a4d;
  --ok-soft: #e5f5ec;
  --radius: 8px;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 14px;
  color: var(--text);
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
[hidden] { display: none !important; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: 8px 16px; background: var(--brand); color: #fff;
}
.brand { font-size: 16px; white-space: nowrap; }
#nav { display: flex; gap: 4px; }
#nav a { color: #fff; text-decoration: none; padding: 6px 12px; border-radius: 6px; opacity: .75; white-space: nowrap; }
#nav a.active, #nav a:hover { background: rgba(255,255,255,.15); opacity: 1; }
.spacer { flex: 1; }
.who { font-size: 13px; opacity: .85; white-space: nowrap; }
.topbar .ghost { color: #fff; border-color: rgba(255,255,255,.35); }
.save-state { font-size: 12px; opacity: .8; white-space: nowrap; }
.save-state.busy { opacity: 1; color: #ffd98a; }

main { padding: 16px; max-width: 1600px; margin: 0 auto; }
.empty { color: var(--muted); padding: 32px 0; text-align: center; }

button {
  cursor: pointer; border: 1px solid var(--line); background: var(--panel);
  border-radius: 6px; padding: 6px 12px; line-height: 1.3;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.ghost { background: transparent; }
button.ok { background: var(--ok-soft); border-color: #b5dfc7; color: var(--ok); }
button.bad { background: var(--danger-soft); border-color: #f2c1bd; color: var(--danger); }
input, select, textarea {
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; background: #fff; min-width: 0;
}
textarea { resize: vertical; min-height: 60px; width: 100%; }
textarea[rows="1"] { min-height: 36px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.toolbar input[type=search] { width: 220px; }
.toolbar label { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.count { color: var(--muted); font-size: 13px; }
.bulk { background: var(--accent-soft); border-radius: var(--radius); padding: 8px 12px; }

.table-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow-x: auto; }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { position: sticky; top: 0; background: #f0f2f5; font-weight: 600; white-space: nowrap; font-size: 13px; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--accent); }
tbody tr { cursor: pointer; }
tbody tr:hover { background: #f8fafc; }
tr.lost td { color: #9aa1aa; }
td.clip { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
td.nowrap, .nowrap { white-space: nowrap; }
.sessions-table td { white-space: nowrap; cursor: default; }
.sessions-table tbody tr { cursor: default; }
.sessions-table select { max-width: 120px; }
.sessions-table input { width: 160px; }

.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; background: #eef1f4; font-size: 12px; margin: 1px 2px 1px 0; white-space: nowrap; }
.chip.won { background: var(--ok-soft); color: var(--ok); }
.chip.lost { background: #eceef1; color: #8a929c; }
.badge-new { display: inline-block; background: var(--danger); color: #fff; font-size: 11px; border-radius: 4px; padding: 0 5px; margin-right: 4px; }
.late { color: var(--danger); font-weight: 600; }
.never { color: var(--warn); }

.stats { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 12px; min-width: 96px; }
.stat b { display: block; font-size: 18px; }
.stat span { color: var(--muted); font-size: 12px; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 16px; margin-bottom: 12px; }
.card h3 { margin: 0 0 8px; font-size: 15px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

/* 詳細資料抽屜 */
#drawer-wrap { position: fixed; inset: 0; z-index: 20; }
.scrim { position: absolute; inset: 0; background: rgba(20, 28, 38, .35); }
#drawer {
  position: absolute; top: 0; right: 0; bottom: 0; width: min(620px, 100%);
  background: var(--bg); overflow-y: auto; box-shadow: -8px 0 24px rgba(0,0,0,.12);
}
.drawer-head { position: sticky; top: 0; z-index: 1; background: var(--panel); border-bottom: 1px solid var(--line); padding: 12px 16px; }
.drawer-head h2 { margin: 0; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.drawer-head .close { margin-left: auto; font-size: 18px; padding: 2px 10px; }
.drawer-body { padding: 12px 16px 48px; }
.call { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 8px; }
.call a.tel { font-size: 16px; font-weight: 600; text-decoration: none; padding: 4px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--accent-soft); }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 12px; }
.grid .wide { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 3px; }
.field > span { font-size: 12px; color: var(--muted); }
.chips-edit button { border-radius: 999px; padding: 2px 10px; font-size: 12px; margin: 0 4px 4px 0; }
.chips-edit button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.save-row { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.msg { font-size: 13px; color: var(--muted); }
.msg.err { color: var(--danger); white-space: pre-line; }
.intake { border-top: 1px dashed var(--line); padding-top: 10px; margin-top: 10px; }
.intake:first-of-type { border-top: 0; margin-top: 0; padding-top: 0; }
.intake-head { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 8px; }
.sub { color: var(--muted); font-size: 12px; }
.history { white-space: pre-line; font-size: 12px; color: var(--muted); }
details summary { cursor: pointer; color: var(--muted); font-size: 12px; margin-top: 6px; }
dl.raw { display: grid; grid-template-columns: max-content 1fr; gap: 2px 10px; font-size: 12px; margin: 6px 0 0; }
dl.raw dt { color: var(--muted); }
dl.raw dd { margin: 0; word-break: break-word; }
.logs { list-style: none; padding: 0; margin: 10px 0 0; }
.logs li { padding: 6px 0; border-bottom: 1px solid var(--line); }
.logs .meta { font-size: 12px; color: var(--muted); }
.logs .text { white-space: pre-line; word-break: break-word; }
.log-接通 .meta b { color: var(--ok); }
.log-未接通 .meta b { color: var(--danger); }

/* 匯入 */
.import textarea { min-height: 160px; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.map-table td { vertical-align: middle; }
.map-table .sample { color: var(--muted); max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.st-new { color: var(--ok); }
.st-exist { color: var(--accent); }
.st-skip { color: var(--muted); }
.st-err { color: var(--danger); }
.conflict { background: var(--warn-soft); border-radius: 4px; padding: 2px 6px; display: inline-flex; gap: 4px; align-items: center; margin: 1px 0; }

/* 名單列表：列上直接操作 */
button.sm { padding: 3px 8px; font-size: 12px; }
.c-act button + button { margin-left: 4px; }
.leads-table td { vertical-align: middle; }
.leads-table a.name { font-weight: 600; color: var(--text); text-decoration: none; }
.leads-table a.name:hover { color: var(--accent); text-decoration: underline; }
.leads-table .tags { margin-top: 2px; }
.leads-table .c-phone a { text-decoration: none; }
.leads-table .c-lecture { font-size: 12px; color: var(--muted); min-width: 150px; max-width: 220px; }
.leads-table .c-deal select { max-width: 110px; }
.leads-table .c-followUp { min-width: 200px; }
textarea.inline { min-height: 0; height: 32px; resize: none; overflow: hidden; padding: 5px 8px; line-height: 1.4; }
textarea.inline:focus { height: 72px; overflow: auto; }
select.t1 { background: var(--ok-soft); }
select.t2 { color: #8a929c; }

/* 詳細面板 */
.drawer-head .summary { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 8px; }
.inline-field { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }
.grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.four { grid-template-columns: repeat(4, minmax(0, 1fr)); margin-bottom: 8px; }
.card h4 { margin: 14px 0 4px; font-size: 13px; color: var(--muted); }
.note-add { display: flex; gap: 8px; align-items: flex-start; margin-top: 12px; }
.note-add textarea { min-height: 40px; }
.intake .move { margin-top: 8px; }
.invite { margin-top: 12px; }
.sources { margin-top: 12px; display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.sources details { flex-basis: 100%; }

#login { position: fixed; inset: 0; z-index: 40; background: var(--bg); display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 32px; text-align: center; max-width: 360px; width: 100%; }
.login-card h1 { font-size: 20px; margin: 0 0 8px; }
#login-btn { display: flex; justify-content: center; margin-top: 16px; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 50;
  background: #1d232b; color: #fff; padding: 10px 16px; border-radius: 8px; max-width: calc(100% - 32px);
  box-shadow: 0 6px 20px rgba(0,0,0,.2); white-space: pre-line;
}
#toast.err { background: var(--danger); }

@media (max-width: 720px) {
  main { padding: 12px; }
  .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 12px; }
  .who { display: none; }
  .toolbar input[type=search] { width: 100%; }
  .grid { grid-template-columns: 1fr; }
  .grid.three, .grid.four { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  /* 名單表格在手機上變成卡片：姓名｜電話、業務｜聯繫、講座、成交｜按鈕、追蹤 */
  .leads-table thead { display: none; }
  .leads-table tr { display: grid; grid-template-columns: 1fr auto; gap: 6px 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
  .leads-table td { border: 0; padding: 0; }
  .leads-table td.sel { display: none; }
  .leads-table td.c-name a.name { font-size: 15px; }
  .leads-table td.c-phone, .leads-table td.c-lastContact, .leads-table td.c-act { text-align: right; }
  .leads-table td.c-owners { font-size: 12px; color: var(--muted); }
  .leads-table td.c-lecture { grid-column: 1 / -1; max-width: none; }
  .leads-table td.c-followUp { grid-column: 1 / -1; min-width: 0; order: 10; }
  .leads-table td.c-lecture:empty { display: none; }
}
