/* โทนสีเดียวกับ dashboard ของบอท Messenger (ทีมคุ้นอยู่แล้ว) · 3 คอลัมน์: กล่อง | รายการ | แชท */
:root {
  --bg: #f4f5f7; --surface: #fcfcfb; --line: #e4e6eb; --grid: #ececea;
  --text: #0b0b0b; --text-2: #52514e; --muted: #7a7974;
  --accent: #2a78d6; --accent-bg: #e8f1fc;
  --ok: #137333; --ok-bg: #e6f4ea; --warn: #8a4b00; --warn-bg: #fff4e5; --bad: #b3261e; --bad-bg: #fdecea;
  --sales: #6b4fbb; --sales-bg: #f0ecfa;
  --admin-bg: #e8f1fc; --auto-bg: #efefed;
  --flag-bg: #fdecea;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212; --surface: #1a1a19; --line: #33332f; --grid: #2a2a27;
    --text: #ffffff; --text-2: #c3c2b7; --muted: #9a998f;
    --accent: #3987e5; --accent-bg: #1b2a3d;
    --ok: #81c995; --ok-bg: #1e3a27; --warn: #ffcc80; --warn-bg: #3a2a10; --bad: #f2b8b5; --bad-bg: #3b1d1b;
    --sales: #b9a6f2; --sales-bg: #2a2440;
    --admin-bg: #1b2a3d; --auto-bg: #262624;
    --flag-bg: #3b1d1b;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 14px/1.55 -apple-system, BlinkMacSystemFont, "Sukhumvit Set", "Noto Sans Thai", "Segoe UI", sans-serif;
  display: flex; flex-direction: column; }
h1, h2, h3 { margin: 0; line-height: 1.3; }
button, select, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- แถบบน ---------- */
.top { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 20px; padding: 12px max(16px, env(safe-area-inset-right, 0px)) 10px max(16px, env(safe-area-inset-left, 0px));
  background: var(--surface); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: baseline; gap: 10px; }
.brand h1 { font-size: 18px; }
.brand .sub { color: var(--muted); }
.asof { color: var(--text-2); font-size: 13px; }
.me { margin-left: auto; font-size: 13px; display: flex; align-items: center; gap: 6px; }
.me .muted, .muted { color: var(--text-2); font-size: 12.5px; }
.settings { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; color: var(--text-2); }
.settings select { margin: 0 4px; padding: 2px 4px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); }
.banner { padding: 6px max(16px, env(safe-area-inset-left, 0px)); background: var(--warn-bg); color: var(--warn); font-size: 12.5px; }

/* ---------- ตัวเลขสรุป ---------- */
.tiles { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 10px; padding: 12px max(16px, env(safe-area-inset-right, 0px)) 0 max(16px, env(safe-area-inset-left, 0px)); }
.tile { text-align: left; border: 1px solid var(--line); background: var(--surface); border-radius: 8px; padding: 10px 12px; cursor: pointer; display: grid; gap: 2px; min-width: 0; }
.tile:hover { border-color: var(--accent); }
.tile.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.tile .t { font-size: 12.5px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .v { font-size: 24px; font-weight: 600; font-variant-numeric: tabular-nums; }
.tile .h { font-size: 11.5px; color: var(--muted); line-height: 1.35; }
.tile.risk .v { color: var(--bad); }
.tile.risk .v.zero { color: var(--muted); }
.tile.wait .v { color: var(--warn); }

/* ---------- 3 คอลัมน์ ---------- */
.layout { flex: 1; min-height: 0; display: grid; grid-template-columns: 220px minmax(320px, 1fr) minmax(380px, 1.25fr); gap: 12px;
  padding: 12px max(16px, env(safe-area-inset-right, 0px)) 16px max(16px, env(safe-area-inset-left, 0px)); }
.side, .list, .chat { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; min-height: 0; min-width: 0; }
.side { padding: 8px; display: flex; flex-direction: column; gap: 2px; overflow: auto; }
.side .grp { font-size: 11.5px; color: var(--muted); padding: 10px 8px 4px; letter-spacing: .02em; }
.navbtn { display: flex; justify-content: space-between; align-items: center; gap: 8px; width: 100%; border: 0; background: none; padding: 7px 8px; border-radius: 6px; cursor: pointer; text-align: left; }
.navbtn:hover { background: var(--bg); }
.navbtn.on { background: var(--accent-bg); color: var(--accent); font-weight: 600; }
.navbtn .c { font-variant-numeric: tabular-nums; color: var(--muted); font-weight: 400; }
.navbtn.on .c { color: var(--accent); }

.list { display: flex; flex-direction: column; }
.listhead { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.listhead h2 { font-size: 15px; flex: 1; min-width: 160px; }
.listhead input { flex: 1; min-width: 140px; max-width: 240px; padding: 5px 9px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.listbody { overflow: auto; flex: 1; }
.row { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 10px 12px; border-bottom: 1px solid var(--grid); cursor: pointer; width: 100%; border-left: 3px solid transparent; background: none; text-align: left; border-top: 0; border-right: 0; }
.row:hover { background: var(--bg); }
.row.on { background: var(--accent-bg); border-left-color: var(--accent); }
.row .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.row .when { color: var(--muted); font-size: 12px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.row .chips { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 4px; }
.row .why { grid-column: 1 / -1; font-size: 12.5px; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row.box-watch .why { color: var(--bad); }
.row.box-waiting .why { color: var(--warn); }
.row.box-phone .why, .chathead .why.box-phone { color: var(--accent); font-weight: 600; }
.more { padding: 12px; color: var(--muted); font-size: 12.5px; text-align: center; }

.chip { display: inline-flex; align-items: center; gap: 3px; font-size: 11.5px; padding: 0 7px; border-radius: 999px; background: var(--bg); color: var(--text-2); border: 1px solid var(--line); white-space: nowrap; line-height: 1.6; }
.chip.risk { background: var(--bad-bg); color: var(--bad); border-color: transparent; }
.chip.stage { background: var(--accent-bg); color: var(--accent); border-color: transparent; }
.chip.group { background: var(--sales-bg); color: var(--sales); border-color: transparent; }
.chip.ok { background: var(--ok-bg); color: var(--ok); border-color: transparent; }
.chip.oa { border-color: var(--line); background: var(--surface); color: var(--text-2); font-weight: 600; }
.chip.oa-main { color: var(--accent); border-color: var(--accent); }
.chip.warnchip { background: var(--warn-bg); color: var(--warn); border-color: transparent; }

/* ---------- แผงแชท ---------- */
.chat { display: flex; flex-direction: column; }
.chat .empty { margin: auto; color: var(--muted); padding: 24px; text-align: center; }
.chathead { padding: 12px 14px; border-bottom: 1px solid var(--line); display: grid; gap: 6px; }
.chathead h3 { font-size: 15px; }
.chathead .why { font-size: 13px; color: var(--text-2); }
.chathead .why.box-watch { color: var(--bad); }
.chathead .why.box-waiting { color: var(--warn); }
.chathead .tools { display: flex; gap: 6px; flex-wrap: wrap; }
.btn { border: 1px solid var(--line); background: var(--surface); border-radius: 6px; padding: 3px 10px; cursor: pointer; font-size: 12.5px; }
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.close { display: none; }
.stagebar { display: grid; grid-template-columns: repeat(12, 1fr); gap: 2px; }
.stagebar i { height: 5px; border-radius: 2px; background: var(--grid); }
.stagebar i.done { background: var(--accent); }
.log { flex: 1; overflow: auto; padding: 12px 14px; display: flex; flex-direction: column; gap: 6px; }
.day { align-self: center; font-size: 11.5px; color: var(--muted); padding: 6px 0 2px; }
.msg { max-width: 82%; display: grid; gap: 2px; }
.msg .meta { font-size: 11px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.msg .b { padding: 6px 10px; border-radius: 10px; white-space: pre-wrap; overflow-wrap: anywhere; background: var(--bg); border: 1px solid var(--line); }
.msg.customer { align-self: flex-start; }
.msg.sales { align-self: flex-start; }
.msg.sales .b { background: var(--sales-bg); border-color: transparent; }
.msg.admin, .msg.auto { align-self: flex-end; }
.msg.admin .meta, .msg.auto .meta { justify-content: flex-end; }
.msg.admin .b { background: var(--admin-bg); border-color: transparent; }
.msg.auto .b { background: var(--auto-bg); border-color: transparent; color: var(--text-2); font-size: 12.5px; }
.msg .b.media { color: var(--muted); font-style: italic; }
.msg.flag .b { background: var(--flag-bg); border-color: var(--bad); }
.msg.flag.done .b { background: var(--bg); border-color: var(--ok); }

/* ---------- ปิดธง ---------- */
.handle { display: grid; gap: 6px; padding: 8px 10px; border-radius: 8px; background: var(--bad-bg); }
.handle.ok { background: var(--ok-bg); }
.handle .hrow { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; }
.handle textarea, .handle input { border: 1px solid var(--line); border-radius: 6px; background: var(--surface); padding: 5px 8px; min-width: 0; }
.handle textarea { width: 100%; resize: vertical; }
.handle input { flex: 1; max-width: 200px; }
.handle .muted { color: var(--text-2); font-size: 12.5px; }
.handle .note-text { white-space: pre-wrap; font-size: 13px; }
.handle .err { color: var(--bad); font-size: 12.5px; }
.handle .hist { font-size: 12px; color: var(--text-2); }
.handle .hist summary { cursor: pointer; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { color: #fff; filter: brightness(1.08); }
.msg .b.clip { max-height: 7.5em; overflow: hidden; position: relative; cursor: pointer; }
.msg .b.clip::after { content: 'แสดงทั้งหมด ▾'; position: absolute; right: 8px; bottom: 4px; font-size: 11px; color: var(--accent); background: inherit; padding-left: 8px; }

/* ---------- ขั้นตอนงาน ---------- */
.stages { padding: 12px; display: grid; gap: 6px; }
.stg { display: grid; grid-template-columns: 2.2em minmax(0, 13em) 1fr 3.5em; align-items: center; gap: 8px; border: 0; background: none; padding: 4px 6px; border-radius: 6px; cursor: pointer; text-align: left; }
.stg:hover { background: var(--bg); }
.stg.on { background: var(--accent-bg); }
.stg .n { color: var(--muted); font-variant-numeric: tabular-nums; }
.stg .l { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stg .bar { height: 12px; border-radius: 3px; background: var(--accent); min-width: 2px; }
.stg .v { text-align: right; font-variant-numeric: tabular-nums; }
.note { padding: 0 12px 12px; color: var(--muted); font-size: 12.5px; }

/* ---------- กล่องเรียนรู้ ---------- */
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; background: none; padding: 3px 10px; cursor: pointer; font-size: 12.5px; }
.seg button.on { background: var(--accent-bg); color: var(--accent); font-weight: 600; }
.qa { padding: 10px 12px; border-bottom: 1px solid var(--grid); display: grid; gap: 4px; }
.qa .q { font-weight: 600; white-space: pre-wrap; overflow-wrap: anywhere; }
.qa .a { white-space: pre-wrap; overflow-wrap: anywhere; color: var(--text-2); border-left: 3px solid var(--line); padding-left: 8px; max-height: 6.5em; overflow: hidden; }
.qa .m { font-size: 11.5px; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; font-size: 11.5px; }

/* ---------- จอเล็ก ---------- */
@media (max-width: 1100px) {
  .tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .layout { grid-template-columns: 190px minmax(0, 1fr); }
  .chat { position: fixed; inset: 0; z-index: 10; border-radius: 0; display: none;
    padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px); }
  .chat.open { display: flex; }
  .close { display: inline-block; }
}
@media (max-width: 640px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .layout { grid-template-columns: 1fr; }
  .side { flex-direction: row; flex-wrap: wrap; }
  .side .grp { display: none; }
  .navbtn { width: auto; }
  .settings { margin-left: 0; }
  body { height: auto; }
  .listbody { max-height: 70vh; }
}
@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; } }

/* ---------- FAQ ---------- */
.faqform { display: grid; gap: 8px; margin-top: 6px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
.faqform .frow { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: center; }
.faqform .lab { display: grid; gap: 3px; font-size: 12.5px; color: var(--text-2); }
.faqform input:not([type=radio]), .faqform textarea, .faqform select { border: 1px solid var(--line); border-radius: 6px; background: var(--surface); padding: 5px 8px; min-width: 0; width: 100%; color: var(--text); font-size: 14px; }
.faqform .frow input.by { width: auto; flex: 1; max-width: 200px; }
.faqform textarea { resize: vertical; }
.faqform .exq { color: var(--text); }
.faqform .err, .faq .err { color: var(--bad); font-size: 12.5px; }
.faq .ans { white-space: pre-wrap; overflow-wrap: anywhere; border-left: 3px solid var(--ok); padding-left: 8px; }
.faq .hist { font-size: 12.5px; color: var(--text-2); }
.faq .hist summary { cursor: pointer; }
.faq .acts { gap: 6px; }

.asof .muted { color: var(--muted); }

/* ---------- ร่างคำตอบ ---------- */
.draft .ctx { font-size: 12px; color: var(--muted); border-left: 2px solid var(--line); padding-left: 8px; display: grid; gap: 1px; }
.draft .proposal { display: grid; gap: 4px; justify-items: end; }
.draft .proposal .b { max-width: 92%; padding: 6px 10px; border-radius: 10px; white-space: pre-wrap; overflow-wrap: anywhere; background: var(--admin-bg); }
.draft .warnbox { background: var(--warn-bg); color: var(--warn); padding: 6px 10px; border-radius: 6px; }
.draft .hist { font-size: 12.5px; color: var(--text-2); }
.draft .hist summary { cursor: pointer; }
.banner.live { background: var(--bad-bg); color: var(--bad); }

/* FAQ ถึงกำหนดทบทวน */
.warnmark { color: var(--warn); font-weight: 600; font-size: 12px; }
