:root {
  --bg: #f5f6f8; --card: #fff; --text: #1d2330; --muted: #6b7280; --line: #e3e6eb;
  --primary: #2f5bd3; --primary-ink: #fff; --danger: #c93838; --warn: #b7791f; --ok: #2f855a;
  --s1: #93b4f5; --s2: #6f95ec; --s3: #4d76e0; --s4: #2f5bd3; --s5: #2f855a;
  --radius: 8px;
}
* { box-sizing: border-box; }
html { font-size: 15px; }
body { margin: 0; background: var(--bg); color: var(--text);
  font-family: -apple-system, "Segoe UI", "Noto Sans TC", "Microsoft JhengHei", sans-serif; line-height: 1.5; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: .2rem 0 1rem; }
h2 { font-size: 1.1rem; margin: 0 0 .6rem; }
small { font-size: .85em; }
.muted { color: var(--muted); }
.center { text-align: center; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.req { color: var(--danger); }

.topbar { display: flex; align-items: center; gap: 1rem; padding: .6rem 1.2rem; background: #1d2330; color: #fff; flex-wrap: wrap; }
.topbar a { color: #dfe6f5; }
.topbar .brand { font-weight: 700; color: #fff; }
.topbar nav { display: flex; gap: 1rem; align-items: center; flex: 1; flex-wrap: wrap; }
.topbar .me { display: flex; gap: .8rem; align-items: center; }
.topbar .btn { color: var(--primary-ink); }
.env { font-size: .75rem; padding: .1rem .5rem; border-radius: 99px; background: var(--warn); color: #fff; font-weight: 700; }
.env-dev, .env-test { background: #6b7280; }
.linklike { background: none; border: 0; color: #dfe6f5; cursor: pointer; font: inherit; padding: 0; }

.container { max-width: 1280px; margin: 0 auto; padding: 1.2rem 1rem 3rem; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.2rem; margin-bottom: 1rem; }
.card.narrow { max-width: 480px; }
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 1rem; flex-wrap: wrap; margin-bottom: .6rem; }
.page-head h1 { margin-bottom: .4rem; }
.actions { display: flex; gap: .5rem; align-items: center; }
.actions form { margin: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.grid2 > .card { margin-bottom: 0; }
.grid2 { margin-bottom: 1rem; }
.grid-detail { display: grid; grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr); gap: 1rem; align-items: start; }
@media (max-width: 900px) { .grid2, .grid-detail { grid-template-columns: 1fr; } }

.btn { display: inline-block; background: var(--primary); color: var(--primary-ink); border: 1px solid var(--primary);
  border-radius: 6px; padding: .45rem .9rem; font: inherit; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { filter: brightness(1.08); text-decoration: none; }
.btn-sm { padding: .25rem .6rem; font-size: .9rem; }
.btn-ghost { background: #fff; color: var(--primary); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }

input, select, textarea { font: inherit; padding: .4rem .5rem; border: 1px solid #cdd2da; border-radius: 6px; background: #fff; color: var(--text); width: 100%; }
input[type=checkbox], input[type=radio] { width: auto; }
textarea { resize: vertical; }
label { display: block; font-size: .9rem; color: #374151; }
.stack > * + * { margin-top: .6rem; }
.row { display: flex; gap: .8rem; align-items: flex-end; margin-top: .8rem; }
.row.wrap { flex-wrap: wrap; }
.row label { min-width: 160px; }
.row .grow { flex: 1; }
@media (max-width: 700px) { .row { flex-direction: column; align-items: stretch; } }
form.inline { margin: 0; }
form.inline select { width: auto; }

.flash { padding: .6rem .9rem; border-radius: 6px; margin-bottom: 1rem; background: #e7f5ec; color: #1f5f3c; border: 1px solid #bfe3cd; }
.flash-error { background: #fdecec; color: #8a1f1f; border-color: #f3c4c4; }

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login { width: 100%; max-width: 360px; }
.login label { margin-bottom: .8rem; }
.login .btn { width: 100%; }

/* KPIs & funnel */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .8rem; margin-bottom: .6rem; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem; }
.kpi-label { color: var(--muted); font-size: .85rem; }
.kpi-val { font-size: 1.4rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.funnel { display: flex; flex-direction: column; gap: .45rem; }
.funnel-row { display: grid; grid-template-columns: 11rem 1fr 5.5rem 7.5rem; gap: .6rem; align-items: center; color: inherit; }
a.funnel-row:hover { background: #f1f4fb; text-decoration: none; border-radius: 4px; }
.funnel-bar-wrap { background: #eef0f4; border-radius: 4px; height: 1.3rem; overflow: hidden; }
.funnel-bar { display: block; height: 100%; min-width: 2px; border-radius: 4px; }
.funnel-num { text-align: right; font-weight: 600; }
.funnel-amt { text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
@media (max-width: 600px) { .funnel-row { grid-template-columns: 1fr 3rem; } .funnel-bar-wrap, .funnel-amt { display: none; } }
.s1 { --sc: var(--s1); } .s2 { --sc: var(--s2); } .s3 { --sc: var(--s3); } .s4 { --sc: var(--s4); } .s5 { --sc: var(--s5); }
.funnel-bar { background: var(--sc); }

.list { list-style: none; padding: 0; margin: 0; }
.list li { padding: .35rem 0; border-bottom: 1px solid var(--line); display: flex; gap: .6rem; flex-wrap: wrap; align-items: baseline; }
.list li:last-child { border-bottom: 0; }
.list .overdue .date { color: var(--danger); font-weight: 700; }
.date { font-variant-numeric: tabular-nums; color: #374151; }

/* tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .5rem .55rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: .85rem; color: var(--muted); font-weight: 600; white-space: nowrap; }
tbody tr:hover { background: #fafbfd; }
tr.inactive td { color: var(--muted); }
table.leads td { white-space: nowrap; }
table.leads td:nth-child(3), table.leads td:nth-child(9) { white-space: normal; min-width: 10rem; }

.stage-pill { display: inline-grid; place-items: center; width: 1.6rem; height: 1.6rem; border-radius: 50%; background: var(--sc); color: #fff; font-weight: 700; font-size: .85rem; }
.status-pill { display: inline-block; font-size: .8rem; padding: .05rem .5rem; border-radius: 99px; background: #e8edf9; color: #2a4596; }
.status-pill.status-won { background: #e3f3e9; color: #22663f; }
.status-pill.status-lost { background: #fbe6e6; color: #922; }
.status-pill.status-on_hold { background: #fbf1dc; color: #7a5210; }

.filters { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.filters input[type=search] { flex: 1 1 220px; width: auto; }
.filters select { width: auto; }
.view-toggle { margin-left: auto; display: flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.view-toggle a { padding: .3rem .8rem; color: var(--muted); }
.view-toggle a.on { background: var(--primary); color: #fff; }

.board { display: grid; grid-template-columns: repeat(5, minmax(210px, 1fr)); gap: .8rem; overflow-x: auto; }
.board-col { background: #eceff4; border-radius: var(--radius); padding: .5rem; min-height: 200px; }
.board-head { display: flex; justify-content: space-between; padding: .3rem .4rem .5rem; border-bottom: 3px solid var(--sc); margin-bottom: .5rem; }
.board-card { display: block; background: #fff; border: 1px solid var(--line); border-radius: 6px; padding: .5rem .6rem; margin-bottom: .5rem; color: inherit; }
.board-card:hover { border-color: var(--primary); text-decoration: none; }
.board-card.status-on_hold { opacity: .75; border-style: dashed; }
.board-card.status-lost { opacity: .55; }
.bc-cust { font-size: .8rem; color: var(--muted); }
.bc-title { font-weight: 600; }
.bc-meta { display: flex; gap: .4rem; flex-wrap: wrap; font-size: .85rem; margin-top: .2rem; font-variant-numeric: tabular-nums; }
.bc-owner { font-size: .8rem; }

/* lead form */
.stage-picker { display: grid; grid-template-columns: repeat(5, 1fr); gap: .5rem; }
@media (max-width: 900px) { .stage-picker { grid-template-columns: 1fr; } }
.stage-opt { display: flex; gap: .5rem; align-items: flex-start; border: 2px solid var(--line); border-radius: var(--radius); padding: .6rem; cursor: pointer; }
.stage-opt input { display: none; }
.stage-opt small { color: var(--muted); }
.stage-opt:has(input:checked) { border-color: var(--sc); background: #f4f7fe; }
.stage-num { display: inline-grid; place-items: center; min-width: 1.6rem; height: 1.6rem; border-radius: 50%; background: var(--sc, #9aa3b2); color: #fff; font-weight: 700; }
details.section > summary { list-style: none; cursor: pointer; display: flex; gap: .6rem; align-items: baseline; }
details.section > summary::-webkit-details-marker { display: none; }
details.section > summary h2::before { content: "▸ "; color: var(--muted); }
details.section[open] > summary h2::before { content: "▾ "; }
details.section[open] > summary { margin-bottom: .6rem; }
.field-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: .8rem 1rem; }
.field.wide { grid-column: 1 / -1; }
.field label { margin-bottom: .2rem; }
.field .help { font-size: .8rem; color: var(--muted); margin-top: .15rem; }
.field .err { font-size: .85rem; color: var(--danger); margin-top: .15rem; }
.field.has-error input, .field.has-error select, .field.has-error textarea { border-color: var(--danger); }
.hint-dot { display: none; width: .55rem; height: .55rem; border-radius: 50%; background: var(--warn); margin-left: .35rem; vertical-align: middle; }
p .hint-dot { display: inline-block; }
.field.recommended .hint-dot { display: inline-block; }
.field.recommended input, .field.recommended select, .field.recommended textarea { background: #fffbf0; }
.form-actions { display: flex; gap: .6rem; position: sticky; bottom: 0; background: var(--bg); padding: .8rem 0; }

/* lead detail */
.progress { list-style: none; padding: 0; margin: 0 0 .8rem; display: grid; grid-template-columns: repeat(5, 1fr); gap: .3rem; }
.progress li { display: flex; gap: .4rem; align-items: center; padding: .45rem .5rem; background: #eef0f4; border-radius: 6px; color: var(--muted); font-size: .9rem; }
.progress li .stage-num { --sc: #b8bfcc; }
.progress li.done { background: #e4ebfb; color: #2a4596; }
.progress li.done .stage-num { --sc: var(--s3); }
.progress li.current { background: var(--sc); color: #fff; font-weight: 700; }
.progress li.current .stage-num { background: #fff; color: var(--text); }
@media (max-width: 700px) { .progress { grid-template-columns: 1fr; } }
.summary-row { display: flex; gap: 1.2rem; flex-wrap: wrap; align-items: center; }
.stage-actions { display: flex; gap: .5rem; flex-wrap: wrap; margin-top: .9rem; padding-top: .9rem; border-top: 1px solid var(--line); }
.stage-actions input[name=note] { flex: 1 1 220px; width: auto; }
.kv { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: .5rem 1rem; margin: 0; }
.kv .wide { grid-column: 1 / -1; }
.kv dt { font-size: .8rem; color: var(--muted); }
.kv dd { margin: 0; white-space: pre-wrap; word-break: break-word; }
.kv .empty, .detail-section.is-empty { display: none; }
body.show-empty .kv .empty, body.show-empty .detail-section.is-empty { display: block; }
.kv .empty dd { color: #b0b6c0; }
.toggle-empty { display: block; margin-bottom: 1rem; }
.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { border-left: 3px solid var(--line); padding: .2rem 0 .7rem .7rem; }
.timeline li.tl-stage { border-left-color: var(--s3); }
.tl-meta { font-size: .8rem; color: var(--muted); }
.tl-body { white-space: pre-wrap; word-break: break-word; }
