/*
 * KM Connect — design system
 * ตัวอักษร Inter (ละติน) + Noto Sans Thai (ไทยแบบไม่มีหัว อ่านง่ายบนจอ) · สีกลาง ๆ + น้ำเงินเป็นสีหลัก
 * สว่างเป็นค่าเริ่มต้น สลับมืดตามเครื่อง · mobile-first: จอแคบเห็นทีละหน้า, ≥900px เห็น 2 คอลัมน์
 */
:root {
  --font: "Inter", "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  --bg: #f6f7f9; --panel: #ffffff; --subtle: #f2f4f7; --line: #e4e7ec; --line-strong: #d0d5dd;
  --text: #101828; --text-2: #344054; --muted: #667085;
  --primary: #2563eb; --primary-hover: #1d4ed8; --primary-ink: #ffffff; --primary-soft: #eff4ff;
  --ok: #079455; --ok-soft: #ecfdf3; --warn: #b54708; --warn-soft: #fffaeb; --err: #d92d20; --err-soft: #fef3f2;
  --bot: #5925dc; --bot-soft: #f4f3ff; --line-green: #06c755;
  --radius: 12px; --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --shadow-lg: 0 12px 24px -4px rgba(16, 24, 40, .12), 0 4px 8px -2px rgba(16, 24, 40, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c0e12; --panel: #13161b; --subtle: #1a1d24; --line: #262a33; --line-strong: #333845;
    --text: #f2f4f7; --text-2: #cecfd2; --muted: #94969c;
    --primary: #3b82f6; --primary-hover: #60a5fa; --primary-soft: #172554;
    --ok: #47cd89; --ok-soft: #053321; --warn: #fdb022; --warn-soft: #4e1d09; --err: #f97066; --err-soft: #55160c;
    --bot: #bdb4fe; --bot-soft: #27115f;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4); --shadow-lg: 0 12px 24px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 15px/1.55 var(--font); -webkit-font-smoothing: antialiased; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }
.muted { color: var(--muted); } .small { font-size: 13px; }
.warn-t { color: var(--warn); } .err { color: var(--err); }
code, .mono { font-family: ui-monospace, "SF Mono", Consolas, monospace; font-size: 12.5px; word-break: break-all; }
.ico { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; vertical-align: -3px; }

/* ---------- ปุ่ม / ป้าย ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 38px; padding: 0 14px;
  border-radius: var(--radius-sm); border: 1px solid var(--line-strong); background: var(--panel); color: var(--text-2);
  font-weight: 500; cursor: pointer; white-space: nowrap; box-shadow: var(--shadow); transition: background .15s, border-color .15s; }
.btn:hover { background: var(--subtle); text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { background: var(--primary-hover); }
.btn.small { height: 32px; padding: 0 10px; font-size: 13px; }
.btn.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, var(--line)); }
.btn.line { background: var(--line-green); border-color: var(--line-green); color: #fff; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { display: inline-grid; place-items: center; width: 38px; height: 38px; border-radius: var(--radius-sm);
  border: 0; background: none; color: var(--text-2); cursor: pointer; }
.icon-btn:hover { background: var(--subtle); }
.icon-btn .ico { width: 20px; height: 20px; }
.link-btn { background: none; border: 0; padding: 0; color: var(--primary); cursor: pointer; font-size: inherit; }
.badge { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--primary); color: #fff;
  font-size: 11.5px; font-weight: 600; line-height: 20px; text-align: center; }
.badge:empty { display: none; }
.tag { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 500; line-height: 20px; padding: 0 8px;
  border-radius: 6px; background: var(--subtle); color: var(--text-2); white-space: nowrap; }
.tag.brand { background: color-mix(in srgb, var(--c) 12%, transparent); color: color-mix(in srgb, var(--c) 85%, var(--text)); }
.tag.brand::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--c); }
.tag.ch-line { background: color-mix(in srgb, var(--line-green) 14%, transparent); color: color-mix(in srgb, var(--line-green) 70%, var(--text)); }
.tag.warn { background: var(--warn-soft); color: var(--warn); }
.tag.ok { background: var(--ok-soft); color: var(--ok); }
.tag.muted { color: var(--muted); }

/* ---------- โครงแอป ---------- */
.app { display: flex; height: 100dvh; }
.pane { background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.list-pane { width: 100%; }
.chat-pane { display: none; width: 100%; background: var(--bg); }
body.show-chat .list-pane { display: none; }
body.show-chat .chat-pane { display: flex; }
@media (min-width: 900px) {
  .list-pane { width: 380px; flex: none; border-right: 1px solid var(--line); display: flex !important; }
  .chat-pane { display: flex; flex: 1; }
  .back { display: none !important; }
}
.topbar { display: flex; align-items: center; gap: 4px; padding: 10px 12px 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line); background: var(--panel); }
.topbar h1 { display: flex; align-items: center; gap: 10px; font-size: 17px; font-weight: 700; letter-spacing: -.01em; margin: 0; flex: 1; }
.logo { width: 28px; height: 28px; border-radius: 8px; flex: none; }
.tabs { display: flex; gap: 4px; padding: 8px 12px 0; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; display: inline-flex; justify-content: center; align-items: center; gap: 6px; padding: 8px 10px 10px;
  background: none; border: 0; border-bottom: 2px solid transparent; color: var(--muted); font-weight: 500; cursor: pointer; }
.tabs button.on { border-color: var(--primary); color: var(--text); font-weight: 600; }
#chatsView, #leadsView { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.filters { padding: 12px 12px 10px; display: flex; flex-direction: column; gap: 10px; border-bottom: 1px solid var(--line); }
.search { position: relative; }
.search .ico { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.search input { width: 100%; height: 38px; padding: 0 12px 0 36px; border-radius: var(--radius-sm); border: 1px solid var(--line-strong); background: var(--panel); }
.chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips button { flex: none; display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 12px; border-radius: 15px;
  border: 1px solid var(--line); background: var(--panel); color: var(--text-2); cursor: pointer; font-size: 13px; font-weight: 500; }
.chips button:hover { background: var(--subtle); }
.chips button.on { background: var(--text); color: var(--panel); border-color: var(--text); }
.chips button.on[style*="--c"] { background: var(--c); border-color: var(--c); color: #fff; }

/* ---------- รายการแชท ---------- */
.conv-list, .lead-list { list-style: none; margin: 0; padding: 4px 0; overflow-y: auto; flex: 1; }
.conv { display: flex; gap: 12px; padding: 12px 14px; margin: 0 6px; border-radius: 10px; cursor: pointer; }
.conv:hover { background: var(--subtle); }
.conv.active { background: var(--primary-soft); }
.conv strong { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv.unread strong { font-weight: 700; }
.conv.unread .preview { color: var(--text); font-weight: 500; }
.avatar { flex: none; width: 44px; height: 44px; border-radius: 50%; background: color-mix(in srgb, var(--c) 18%, var(--panel));
  color: color-mix(in srgb, var(--c) 85%, var(--text)); display: grid; place-items: center; font-weight: 700; font-size: 17px; overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.conv-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.row time { font-size: 12px; color: var(--muted); flex: none; }
.preview { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tags { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.empty { padding: 48px 24px; text-align: center; color: var(--muted); margin: auto; list-style: none; }
.empty .ico { width: 40px; height: 40px; stroke-width: 1.5; display: block; margin: 0 auto 10px; opacity: .6; }
.lead { padding: 12px 14px; margin: 0 6px; border-radius: 10px; cursor: pointer; display: flex; flex-direction: column; gap: 4px; }
.lead:hover { background: var(--subtle); }
.lead .preview { white-space: normal; }
.lead select { height: 30px; border-radius: 6px; border: 1px solid var(--line-strong); background: var(--panel); padding: 0 6px; font-size: 13px; }
.lead-tools { display: flex; justify-content: space-between; align-items: center; gap: 8px; }

/* ---------- หน้าแชท ---------- */
.chat { display: flex; flex-direction: column; height: 100%; min-height: 0; width: 100%; }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--panel); flex-wrap: wrap; }
.who { flex: 1; min-width: 150px; display: flex; flex-direction: column; gap: 3px; }
.who strong { font-size: 16px; font-weight: 600; }
.who small { color: var(--muted); font-size: 12.5px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.chat-actions { display: flex; gap: 6px; }
.bot-state { display: inline-flex; align-items: center; gap: 5px; align-self: flex-start; font-size: 12px; font-weight: 500; padding: 1px 8px; border-radius: 6px; background: var(--subtle); color: var(--muted); }
.bot-state.on { background: var(--bot-soft); color: var(--bot); }
.bot-state.off { background: var(--warn-soft); color: var(--warn); }
.bot-state .ico { width: 14px; height: 14px; }
.lead-strip { display: flex; flex-direction: column; gap: 4px; padding: 8px 16px; background: var(--ok-soft); color: var(--text); font-size: 14px; border-bottom: 1px solid var(--line); }
.lead-strip .ico { color: var(--ok); }
.msgs { list-style: none; margin: 0; padding: 20px 16px; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 10px; }
@media (min-width: 900px) { .msgs { padding: 24px max(24px, calc((100% - 760px) / 2)); } }
.msg { max-width: min(78%, 560px); display: flex; flex-direction: column; }
.msg.in { align-self: flex-start; } .msg.out { align-self: flex-end; align-items: flex-end; }
.bubble { padding: 9px 14px; border-radius: 16px; background: var(--panel); border: 1px solid var(--line); line-height: 1.5; overflow-wrap: anywhere; font-size: 15px; }
.msg.in .bubble { border-bottom-left-radius: 4px; }
.msg.out .bubble { border-bottom-right-radius: 4px; }
.msg.staff .bubble { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.msg.bot .bubble { background: var(--bot-soft); border-color: transparent; color: var(--text); }
.meta { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; font-size: 12px; color: var(--muted); margin: 4px 4px 0; }
.meta .ico { width: 13px; height: 13px; }
.pcards { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.pcard { font-size: 13px; padding: 4px 10px; border-radius: 6px; background: var(--panel); border: 1px solid var(--line); color: var(--text-2); }
.draft { margin: 0 16px 10px; padding: 12px 14px; border: 1px dashed var(--bot); border-radius: var(--radius); background: var(--bot-soft); }
.draft-label { display: flex; align-items: center; gap: 6px; font-size: 12.5px; font-weight: 600; color: var(--bot); }
.draft-text { white-space: pre-wrap; margin: 6px 0 10px; }
.draft-actions { display: flex; gap: 6px; }
.composer { display: flex; gap: 8px; align-items: flex-end; padding: 12px 14px; padding-bottom: max(12px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--panel); }
.composer textarea { flex: 1; resize: none; min-height: 42px; padding: 10px 14px; border-radius: var(--radius); border: 1px solid var(--line-strong); background: var(--panel); max-height: 160px; line-height: 1.45; }
.composer textarea:focus { border-color: var(--primary); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent); }
.composer .btn { height: 42px; }
.toast { position: fixed; left: 50%; bottom: 88px; transform: translateX(-50%); background: var(--text); color: var(--panel);
  padding: 10px 16px; border-radius: 10px; max-width: calc(100% - 32px); z-index: 10; box-shadow: var(--shadow-lg); font-size: 14px; }

/* ---------- ล็อกอิน ---------- */
.login-page { display: grid; place-items: center; min-height: 100dvh; padding: 16px; }
.login-card { background: var(--panel); padding: 32px 28px; border-radius: 16px; width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; border: 1px solid var(--line); box-shadow: var(--shadow-lg); }
.login-card .logo { width: 44px; height: 44px; border-radius: 12px; }
.login-card h1 { margin: 4px 0 0; font-size: 22px; letter-spacing: -.01em; }
.login-card p { margin: 0; }

/* ---------- ฟอร์ม ---------- */
.login-card label, .form label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 500; color: var(--text-2); }
.login-card input, .form input, .form select, .form textarea { height: 40px; padding: 0 12px; border-radius: var(--radius-sm); border: 1px solid var(--line-strong); background: var(--panel); width: 100%; font-weight: 400; color: var(--text); }
.form textarea { height: auto; min-height: 110px; padding: 10px 12px; line-height: 1.5; }
.form input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--primary); }
.form input[type=color] { padding: 3px; }
.login-card input:focus, .form input:focus, .form select:focus, .form textarea:focus { border-color: var(--primary); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent); }
.form { display: grid; gap: 14px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px 16px; }
.alert { background: var(--err-soft); color: var(--err); padding: 10px 14px; border-radius: var(--radius-sm); font-size: 14px; margin-bottom: 12px; }
.alert.ok { background: var(--ok-soft); color: var(--ok); }
.set { color: var(--ok); font-size: 12px; font-weight: 500; } .unset { color: var(--warn); font-size: 12px; font-weight: 500; }

/* ---------- หลังบ้าน ---------- */
.admin { max-width: 1040px; margin: 0 auto; padding: 0 16px 80px; }
.admin .topbar { margin: 0 -16px 0; position: sticky; top: 0; z-index: 5; }
.admin-nav { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; margin: 0 -16px 18px; padding: 0 16px; border-bottom: 1px solid var(--line); background: var(--panel); }
.admin-nav::-webkit-scrollbar { display: none; }
.admin-nav a { padding: 10px 12px; color: var(--muted); font-weight: 500; white-space: nowrap; border-bottom: 2px solid transparent; }
.admin-nav a:hover { color: var(--text); text-decoration: none; }
.admin-nav a.on { color: var(--text); border-color: var(--primary); font-weight: 600; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 14px; box-shadow: var(--shadow); }
.card > b:first-child { display: block; font-size: 16px; font-weight: 600; margin-bottom: 4px; }
.card p { margin: 6px 0; }
table.t { width: 100%; border-collapse: collapse; font-size: 14px; }
table.t th { font-size: 12px; font-weight: 600; color: var(--muted); }
table.t td, table.t th { padding: 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.scroll-x { overflow-x: auto; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 14px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; color: var(--text); display: flex; flex-direction: column; gap: 2px; box-shadow: var(--shadow); }
.stat:hover { text-decoration: none; border-color: var(--line-strong); }
.stat span { color: var(--muted); font-size: 13px; font-weight: 500; order: -1; }
.stat b { font-size: 30px; font-weight: 700; letter-spacing: -.02em; line-height: 1.2; }
.progress { height: 8px; background: var(--subtle); border-radius: 4px; margin: 10px 0 6px; overflow: hidden; }
.progress span { display: block; height: 100%; background: var(--ok); border-radius: 4px; }
.checklist { list-style: none; margin: 8px 0 0; padding: 0; }
.checklist li { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.checklist li:last-child { border-bottom: 0; }
.checklist .mark { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; border: 1.5px solid var(--line-strong); flex: none; color: transparent; }
.checklist .mark .ico { width: 13px; height: 13px; stroke-width: 3; }
.checklist li.ok .mark { background: var(--ok); color: #fff; border-color: var(--ok); }
.checklist li.ok .grow { color: var(--muted); }
.grow { flex: 1; min-width: 0; }
details.fold { padding: 0; }
details.fold > summary { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; cursor: pointer; list-style: none; padding: 16px 20px; }
details.fold > summary::-webkit-details-marker { display: none; }
details.fold > summary b { font-size: 16px; }
details.fold > summary::after { content: ""; margin-left: auto; width: 8px; height: 8px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(45deg); transition: transform .15s; }
details.fold:not([open]) > summary::after { transform: rotate(-45deg); }
details.fold[open] > summary { border-bottom: 1px solid var(--line); }
details.fold > form { padding: 4px 20px 0; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
h3.sec { font-size: 12.5px; margin: 14px 0 -2px; color: var(--muted); font-weight: 600; letter-spacing: .02em; }
h3.sec small { font-weight: 400; }
.savebar { display: flex; gap: 8px; flex-wrap: wrap; position: sticky; bottom: 0; background: var(--panel); padding: 12px 0 16px; border-top: 1px solid var(--line); margin-top: 4px; }
.copy-row { display: flex; gap: 8px; align-items: center; background: var(--subtle); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 6px 6px 6px 12px; }
.copy-row code { flex: 1; min-width: 0; }
