:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #f0efec;
  --line: #e4e3df;
  --text: #141413;
  --text-2: #52514e;
  --muted: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --bar: #2a78d6;
  --bar-soft: #b7d3f6;
  --danger: #c63a3a;

  /* 상태 배지 — 색은 보조, 글자가 주 */
  --st0-bg: #fff1d6; --st0-fg: #8a5a00;   /* 신청완료 = 아직 연락 전 */
  --st1-bg: #ecebe7; --st1-fg: #52514e;   /* 부재중 */
  --st2-bg: #dff1f3; --st2-fg: #1d6670;   /* 통화완료 (미전환) */
  --st3-bg: #dce9fb; --st3-fg: #1c5cab;   /* 계약 진행중 */
  --st4-bg: #ddf3e2; --st4-fg: #1f7a35;   /* 계약 완료 */

  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --line: #31312e;
    --text: #f4f4f1; --text-2: #c3c2b7; --muted: #8f8e86;
    --accent: #3987e5; --bar: #3987e5; --bar-soft: #184f95; --danger: #e66767;
    --st0-bg: #3d2f12; --st0-fg: #f2c66b;
    --st1-bg: #2c2c2a; --st1-fg: #c3c2b7;
    --st2-bg: #163538; --st2-fg: #7fd0d9;
    --st3-bg: #182c47; --st3-fg: #8dbaf2;
    --st4-bg: #173521; --st4-fg: #86d69a;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --line: #31312e;
  --text: #f4f4f1; --text-2: #c3c2b7; --muted: #8f8e86;
  --accent: #3987e5; --bar: #3987e5; --bar-soft: #184f95; --danger: #e66767;
  --st0-bg: #3d2f12; --st0-fg: #f2c66b;
  --st1-bg: #2c2c2a; --st1-fg: #c3c2b7;
  --st2-bg: #163538; --st2-fg: #7fd0d9;
  --st3-bg: #182c47; --st3-fg: #8dbaf2;
  --st4-bg: #173521; --st4-fg: #86d69a;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font-family: 'Pretendard Variable', Pretendard, -apple-system, 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
  font-size: 14px; line-height: 1.5; -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1.5em; margin: 0; font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- 버튼·입력 ---------- */
.btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 8px;
  padding: 7px 12px; cursor: pointer; white-space: nowrap; text-decoration: none; display: inline-flex;
  align-items: center; justify-content: center; gap: 6px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
input[type=search], input[type=password], select, textarea {
  border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 8px 10px;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

/* ---------- 로그인 ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 28px; display: grid; gap: 12px; box-shadow: var(--shadow);
}
.login-card h1 { margin: 0; font-size: 20px; }
.login-card p { margin: 0; }

/* ---------- 상단 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10; background: var(--surface); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 16px; padding: 10px 20px; flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.mock { font-size: 11px; font-weight: 600; background: var(--st0-bg); color: var(--st0-fg); padding: 2px 8px; border-radius: 99px; }
.tabs { display: flex; gap: 4px; }
.tabs button {
  border: 0; background: transparent; padding: 8px 14px; border-radius: 8px; cursor: pointer; color: var(--text-2); font-weight: 600;
}
.tabs button[aria-selected=true] { background: var(--surface-2); color: var(--text); }
.top-actions { margin-left: auto; display: flex; align-items: center; gap: 6px; }

main { padding: 20px; max-width: 1280px; margin: 0 auto; }
.tab { display: grid; gap: 16px; }

/* ---------- KPI ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.kpi {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px;
  box-shadow: var(--shadow); text-align: left; cursor: default;
}
button.kpi { cursor: pointer; }
button.kpi:hover { border-color: var(--accent); }
.kpi .label { color: var(--text-2); font-size: 13px; }
.kpi .value { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.kpi .sub { color: var(--muted); font-size: 12px; }
.kpi.alert { border-color: var(--st0-fg); }
.kpi.alert .value { color: var(--st0-fg); }

/* ---------- 필터 ---------- */
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.filters input[type=search] { flex: 1 1 220px; min-width: 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: transparent; padding: 7px 12px; cursor: pointer; color: var(--text-2); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--surface-2); color: var(--text); font-weight: 600; }

/* ---------- 표 ---------- */
.table-wrap { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
table.leads { width: 100%; border-collapse: collapse; }
.leads th, .leads td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
.leads th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface); position: sticky; top: 0; }
.leads tbody tr { cursor: pointer; }
.leads tbody tr:hover { background: var(--surface-2); }
.leads tbody tr:last-child td { border-bottom: 0; }
.leads tr.fresh { animation: fresh 2.4s ease-out; }
@keyframes fresh { from { background: var(--st0-bg); } to { background: transparent; } }
.leads .when { white-space: nowrap; font-variant-numeric: tabular-nums; }
.leads .when .age { display: block; font-size: 12px; color: var(--st0-fg); font-weight: 600; }
.leads .name { font-weight: 600; white-space: nowrap; }
.leads .phone a { color: var(--accent); text-decoration: none; white-space: nowrap; font-variant-numeric: tabular-nums; }
.leads .memo { color: var(--text-2); max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { text-align: center; color: var(--muted); padding: 40px 0; margin: 0; }

.status-select {
  border: 0; border-radius: 99px; padding: 4px 26px 4px 10px; font-size: 12px; font-weight: 600; cursor: pointer;
  appearance: none; -webkit-appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 13px) 50%, calc(100% - 9px) 50%; background-size: 4px 4px; background-repeat: no-repeat;
}
.badge { display: inline-block; border-radius: 99px; padding: 3px 10px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.st0 { background-color: var(--st0-bg); color: var(--st0-fg); }
.st1 { background-color: var(--st1-bg); color: var(--st1-fg); }
.st2 { background-color: var(--st2-bg); color: var(--st2-fg); }
.st3 { background-color: var(--st3-bg); color: var(--st3-fg); }
.st4 { background-color: var(--st4-bg); color: var(--st4-fg); }

/* ---------- 카드·차트 ---------- */
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(420px, 100%), 1fr)); gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; box-shadow: var(--shadow); min-width: 0; }
.card h2 { margin: 0 0 2px; font-size: 15px; }
.card > p { margin: 0 0 14px; }

.hbars { display: grid; gap: 12px; }
.hbar { display: grid; grid-template-columns: 130px 1fr 92px; gap: 10px; align-items: center; }
.hbar .lab { font-size: 13px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hbar .track { height: 14px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.hbar .fill { height: 100%; background: var(--bar); border-radius: 0 4px 4px 0; min-width: 2px; transition: width .3s; }
.hbar .val { text-align: right; font-variant-numeric: tabular-nums; font-size: 13px; }
.hbar .val b { font-weight: 700; }
.hbar .val span { color: var(--muted); font-size: 12px; margin-left: 4px; }
.hbar .step { grid-column: 2 / 4; font-size: 12px; color: var(--muted); margin-top: -8px; }

.vbars svg { display: block; overflow: visible; max-width: 100%; }
.vbars .axis text { fill: var(--muted); font-size: 11px; }
.vbars .grid line { stroke: var(--line); }
.vbars rect.bar { fill: var(--bar); }
.vbars rect.hit { fill: transparent; cursor: default; }
.vbars g.col:hover rect.bar { fill: var(--text-2); }

table.mini { width: 100%; border-collapse: collapse; font-size: 13px; }
.mini th, .mini td { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: right; font-variant-numeric: tabular-nums; }
.mini th:first-child, .mini td:first-child { text-align: left; }
.mini th { font-size: 12px; color: var(--muted); font-weight: 600; }
.mini tr:last-child td { border-bottom: 0; }

.tip {
  position: fixed; z-index: 50; pointer-events: none; background: var(--text); color: var(--bg);
  padding: 6px 10px; border-radius: 6px; font-size: 12px; white-space: nowrap; transform: translate(-50%, -110%);
}

/* ---------- 상세 패널 ---------- */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.28); z-index: 20; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(440px, 100%); z-index: 30; background: var(--surface);
  border-left: 1px solid var(--line); padding: 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px;
}
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.drawer h2 { margin: 0; font-size: 20px; }
.drawer h3 { margin: 12px 0 0; font-size: 13px; color: var(--muted); font-weight: 600; }
.call { padding: 11px; font-size: 15px; }
.info { display: grid; grid-template-columns: 76px 1fr; gap: 6px 12px; margin: 0; }
.info dt { color: var(--muted); }
.info dd { margin: 0; }
.stage-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.stage-btns button { border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 9px; cursor: pointer; font-weight: 600; }
.stage-btns button.cur { border-color: currentColor; }
.stage-btns button:last-child { grid-column: 1 / -1; }
textarea { width: 100%; resize: vertical; line-height: 1.6; }
.memo-actions { display: flex; justify-content: space-between; align-items: center; }
.summary { background: var(--surface-2); border-radius: 8px; padding: 10px 12px; }
.summary p { margin: 4px 0 0; white-space: pre-wrap; }
.timeline { list-style: none; padding: 0; margin: 0; display: grid; gap: 0; }
.timeline li { display: grid; grid-template-columns: 14px 1fr auto; gap: 10px; align-items: center; padding: 6px 0; color: var(--muted); }
.timeline li .dot { width: 10px; height: 10px; border-radius: 50%; border: 2px solid var(--line); }
.timeline li.done { color: var(--text); }
.timeline li.done .dot { background: var(--bar); border-color: var(--bar); }
.timeline li .t { font-variant-numeric: tabular-nums; font-size: 12px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 60;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-weight: 600; box-shadow: var(--shadow);
}

/* ---------- 모바일: 표 → 카드 ---------- */
@media (max-width: 760px) {
  .topbar { padding: 10px 16px; gap: 8px; }
  .top-actions { margin-left: 0; width: 100%; justify-content: flex-end; }
  .top-actions #updated { margin-right: auto; }
  .tabs { order: 3; width: 100%; }
  .tabs button { flex: 1; }
  main { padding: 16px; }
  .kpis { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .kpi { padding: 10px 12px; }
  .kpi .label { font-size: 12px; line-height: 1.35; }
  .kpi .value { font-size: 20px; }
  .kpi .sub { font-size: 11px; line-height: 1.35; }
  .filters select { flex: 1 1 40%; }
  .seg { width: 100%; }
  .seg button { flex: 1; }

  .table-wrap { background: transparent; border: 0; box-shadow: none; }
  table.leads thead { display: none; }
  table.leads, .leads tbody { display: block; }
  .leads tbody tr {
    display: grid; grid-template-columns: 1fr auto; grid-template-areas: "name status" "phone when" "meta meta" "memo memo";
    gap: 4px 10px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 10px;
  }
  .leads td { padding: 0; border: 0; }
  .leads td.name { grid-area: name; font-size: 16px; }
  .leads td.status { grid-area: status; justify-self: end; }
  .leads td.phone { grid-area: phone; font-size: 15px; }
  .leads td.when { grid-area: when; justify-self: end; text-align: right; font-size: 12px; color: var(--muted); }
  .leads td.channel { grid-area: meta; font-size: 12px; color: var(--text-2); }
  .leads td.channel::after { content: attr(data-product); margin-left: 6px; color: var(--muted); }
  .leads td.product { display: none; }
  .leads td.memo { grid-area: memo; max-width: none; font-size: 13px; }
  .leads td.memo:empty { display: none; }

  .hbar { grid-template-columns: 96px 1fr 80px; }
}
