:root {
  --ground: #F4F5F2; --panel: #FFFFFF; --line: #E3E5E0; --line-2: #D3D7D0; --ink: #15191E; --ink-2: #3A424D;
  --muted: #5B6370; --accent: #0B6B57; --accent-ink: #084C3E; --accent-soft: #E3F1EC; --sel: #F1F7F4;
  --warn: #8A3D05; --warn-soft: #FCE9D6; --danger: #B42318; --note: #FFF6DB; --note-line: #C99A1B; --note-ink: #4A3906;
  --line-green: #06C755; --web: #DCE6F5; --web-ink: #23467F; --top: #15191E;
  --mono: 'IBM Plex Mono', ui-monospace, monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--ground); color: var(--ink); font: 14px/1.5 'IBM Plex Sans Thai', 'Noto Sans Thai', system-ui, -apple-system, 'Segoe UI', sans-serif; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
[hidden] { display: none !important; }

/* ---------- buttons & inputs ---------- */
.btn { height: 34px; display: inline-flex; align-items: center; gap: 6px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--line-2); background: var(--panel); font-size: 13px; white-space: nowrap; }
.btn:hover { background: #F7F8F6; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--accent-ink); }
.btn.danger { color: var(--danger); }
.btn.sm { height: 28px; padding: 0 9px; font-size: 12px; }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn { width: 36px; height: 36px; border: 0; border-radius: 8px; background: transparent; display: inline-flex; align-items: center; justify-content: center; color: var(--ink-2); }
.icon-btn:hover { background: #EEF0EC; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; }
.field input, .field select, .field textarea { height: 40px; border: 1px solid var(--line-2); border-radius: 8px; padding: 0 12px; font-weight: 400; font-size: 14px; background: #fff; }
.field textarea { height: auto; min-height: 90px; padding: 10px 12px; resize: vertical; }
.field .hint { font-weight: 400; color: var(--muted); font-size: 12px; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; padding: 1px 8px; border-radius: 10px; background: #EEF0EC; color: var(--ink-2); white-space: nowrap; }
.count { font-family: var(--mono); font-size: 12px; color: var(--muted); }
.count.hot { background: var(--warn-soft); color: var(--warn); padding: 0 6px; border-radius: 10px; }

/* ---------- login ---------- */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth form { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; display: flex; flex-direction: column; gap: 16px; }
.auth h1 { margin: 0; font-size: 22px; display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 30px; height: 30px; border-radius: 8px; background: var(--accent); display: inline-flex; align-items: center; justify-content: center; flex: 0 0 auto; }
.error { color: var(--danger); font-size: 13px; }

/* ---------- shell ---------- */
.shell { height: 100vh; display: flex; flex-direction: column; }
.top { height: 56px; flex: 0 0 auto; display: flex; align-items: center; gap: 16px; padding: 0 16px; background: var(--top); color: #F4F5F2; }
.top .brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; min-width: 0; }
.top .brand small { color: #A9B0BA; font-weight: 400; font-size: 12px; }
.search { flex: 1 1 260px; max-width: 520px; display: flex; align-items: center; gap: 8px; height: 36px; padding: 0 12px; border-radius: 8px; background: #252B33; }
.search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: #F4F5F2; }
.search input::placeholder { color: #A9B0BA; }
.top .right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.presence { height: 32px; display: flex; align-items: center; gap: 8px; padding: 0 10px; border-radius: 16px; border: 1px solid #3A424D; background: transparent; color: #F4F5F2; font-size: 13px; }
.dot { width: 8px; height: 8px; border-radius: 4px; background: #9AA1AB; display: inline-block; flex: 0 0 auto; }
.dot.online { background: #34C27A; } .dot.busy { background: #E5A00D; }
.avatar { width: 32px; height: 32px; border-radius: 16px; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: 12px; flex: 0 0 auto; background: var(--accent-soft); color: var(--accent-ink); overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.lg { width: 64px; height: 64px; border-radius: 32px; font-size: 20px; }
.avatar.md { width: 40px; height: 40px; border-radius: 20px; font-size: 13px; }
.avatar.sm { width: 26px; height: 26px; border-radius: 13px; font-size: 10px; }
.top .avatar { background: var(--accent-soft); }
.menu { position: absolute; z-index: 50; background: #fff; color: var(--ink); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 12px 32px rgba(0,0,0,.14); padding: 6px; min-width: 200px; display: flex; flex-direction: column; }
.menu button, .menu a { text-align: left; border: 0; background: transparent; padding: 8px 10px; border-radius: 6px; font-size: 13px; color: var(--ink); text-decoration: none; display: flex; align-items: center; gap: 8px; }
.menu button:hover, .menu a:hover { background: #F2F3F0; }
.menu .sep { height: 1px; background: var(--line); margin: 4px 0; }
.menu .label { font-size: 11px; color: var(--muted); padding: 6px 10px 2px; font-weight: 600; letter-spacing: .4px; }

.main { flex: 1 1 auto; display: flex; min-height: 0; }
.nav { width: 232px; flex: 0 0 auto; background: var(--panel); border-right: 1px solid var(--line); padding: 14px 10px; display: flex; flex-direction: column; gap: 18px; overflow-y: auto; }
.nav h2 { margin: 0; font-size: 11px; font-weight: 600; letter-spacing: .6px; color: var(--muted); padding: 0 10px 6px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 8px; color: var(--ink); text-decoration: none; }
.nav a:hover { background: #F2F3F0; }
.nav a.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.nav a span.grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav .foot { margin-top: auto; border-top: 1px solid var(--line); padding-top: 10px; }
.ch-badge { width: 18px; height: 18px; border-radius: 5px; display: inline-flex; align-items: center; justify-content: center; font-size: 10px; font-weight: 700; flex: 0 0 auto; }
.ch-badge.line { background: var(--line-green); color: #0B2A17; }
.ch-badge.webchat { background: var(--web); color: var(--web-ink); }
.ch-badge.tiny { width: 16px; height: 16px; font-size: 9px; border: 2px solid #fff; position: absolute; right: -3px; bottom: -3px; }

.list { width: 340px; flex: 0 0 auto; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.list .head { padding: 14px 16px 0; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.list .head h1 { margin: 0; font-size: 17px; }
.tabs { display: flex; gap: 4px; padding: 8px 16px 0; border-bottom: 1px solid var(--line); }
.tabs button { height: 36px; padding: 0 10px; border: 0; border-bottom: 2px solid transparent; background: transparent; color: var(--muted); font-size: 13px; }
.tabs button[aria-selected="true"] { border-bottom-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.items { flex: 1; overflow-y: auto; }
.item { display: flex; gap: 12px; padding: 13px 16px; border-bottom: 1px solid #F0F1EE; border-left: 3px solid transparent; text-decoration: none; color: var(--ink); }
.item:hover { background: #FAFBF9; }
.item.active { background: var(--sel); border-left-color: var(--accent); }
.item .pic { position: relative; flex: 0 0 auto; }
.item .body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.item .row1 { display: flex; justify-content: space-between; gap: 8px; }
.item .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .time { font-size: 12px; color: var(--muted); flex: 0 0 auto; }
.item .pv { font-size: 13px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item.unread .name, .item.unread .pv { font-weight: 700; color: var(--ink); }
.item.waiting .time { color: var(--warn); font-weight: 600; }
.item .row3 { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.unread-n { margin-left: auto; min-width: 18px; height: 18px; border-radius: 9px; background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; padding: 0 5px; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.more { margin: 12px auto; display: block; }

.chat { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; background: var(--ground); }
.chat .bar { display: flex; align-items: center; gap: 12px; padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; position: relative; }
.chat .bar h2 { margin: 0; font-size: 15px; }
.chat .bar .sub { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.chat .bar .actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }
.stream { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.msg { display: flex; gap: 10px; max-width: 72%; }
.msg.out { align-self: flex-end; flex-direction: column; align-items: flex-end; gap: 3px; }
.msg .col { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.bubble { background: #fff; border: 1px solid var(--line); padding: 9px 14px; border-radius: 4px 14px 14px 14px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.out .bubble { background: var(--accent); border-color: var(--accent); color: #fff; border-radius: 14px 4px 14px 14px; }
.msg.out.note .bubble { background: var(--note); border: 1px dashed var(--note-line); color: var(--note-ink); }
.msg.out.failed .bubble { background: #fff; color: var(--ink); border: 1px solid var(--danger); }
.bubble img { display: block; max-width: 260px; max-height: 280px; border-radius: 8px; cursor: zoom-in; }
.bubble .file { display: flex; align-items: center; gap: 8px; color: inherit; }
.msg .meta { font-size: 11px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.msg .meta .err { color: var(--danger); }
.sys { align-self: center; font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; text-align: center; }
.day { align-self: center; font-size: 12px; color: var(--muted); background: #E9EBE6; padding: 2px 12px; border-radius: 12px; }
.typing-ind { align-self: flex-start; font-size: 12px; color: var(--muted); }
.composer { margin: 0 20px 20px; background: var(--panel); border: 1px solid var(--line-2); border-radius: 12px; display: flex; flex-direction: column; position: relative; }
.composer.note { background: var(--note); border-color: var(--note-line); }
.composer .ctabs { display: flex; gap: 4px; padding: 6px 8px 0; align-items: center; }
.composer .ctabs button { height: 30px; padding: 0 12px; border: 0; border-radius: 6px; background: transparent; color: var(--muted); font-size: 13px; }
.composer .ctabs button[aria-selected="true"] { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.composer.note .ctabs button[aria-selected="true"] { background: #FBE9B5; color: var(--note-ink); }
.composer textarea { border: 0; outline: 0; resize: none; padding: 10px 14px; min-height: 70px; max-height: 240px; background: transparent; font-size: 14px; }
.composer .cbar { display: flex; align-items: center; gap: 4px; padding: 4px 8px 8px; }
.composer .cbar .hint { font-size: 12px; color: var(--muted); margin-left: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.composer .cbar .btn.primary { margin-left: auto; }
.canned { position: absolute; left: 8px; right: 8px; bottom: calc(100% + 6px); background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 10px 28px rgba(0,0,0,.14); max-height: 240px; overflow-y: auto; padding: 4px; }
.canned button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 8px 10px; border-radius: 6px; }
.canned button[aria-selected="true"], .canned button:hover { background: var(--accent-soft); }
.canned b { font-family: var(--mono); font-size: 12px; margin-right: 8px; }
.placeholder { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); gap: 8px; padding: 20px; text-align: center; }

.side { width: 300px; flex: 0 0 auto; background: var(--panel); border-left: 1px solid var(--line); padding: 20px 18px; display: flex; flex-direction: column; gap: 22px; overflow-y: auto; }
.side h3 { margin: 0 0 8px; font-size: 12px; font-weight: 600; letter-spacing: .6px; color: var(--muted); }
.side .who { display: flex; flex-direction: column; align-items: center; gap: 6px; text-align: center; }
.kv { display: grid; grid-template-columns: 84px minmax(0, 1fr); gap: 6px 10px; font-size: 13px; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; overflow-wrap: anywhere; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; }
.history a { display: flex; flex-direction: column; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px; text-decoration: none; color: var(--ink); margin-bottom: 6px; }
.history a small { color: var(--muted); }

/* ---------- settings ---------- */
.settings { flex: 1; overflow-y: auto; padding: 28px 32px; }
.settings h1 { margin: 0 0 4px; font-size: 22px; }
.settings .lead { color: var(--muted); margin: 0 0 20px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 20px; margin-bottom: 16px; }
.card h2 { margin: 0 0 12px; font-size: 15px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 14px; }
.form { display: flex; flex-direction: column; gap: 14px; max-width: 640px; }
.form .actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
.tbl th { text-align: left; color: var(--muted); font-weight: 600; font-size: 12px; padding: 8px; border-bottom: 1px solid var(--line); }
.tbl td { padding: 10px 8px; border-bottom: 1px solid #F0F1EE; vertical-align: middle; }
.tblwrap { overflow-x: auto; }
.code { display: block; background: var(--top); color: #E3F1EC; font-family: var(--mono); font-size: 12px; padding: 10px 12px; border-radius: 8px; overflow-x: auto; white-space: pre; }
.copyrow { display: flex; gap: 8px; align-items: stretch; } .copyrow .code { flex: 1; min-width: 0; }
.notice { display: flex; gap: 8px; font-size: 12px; color: var(--ink-2); background: var(--ground); padding: 10px 12px; border-radius: 8px; }

/* ---------- modal & toast ---------- */
.overlay { position: fixed; inset: 0; background: rgba(21,25,30,.45); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 100; }
.modal { background: #fff; border-radius: 14px; padding: 22px; width: 100%; max-width: 460px; max-height: 90vh; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.modal h2 { margin: 0; font-size: 17px; }
.lightbox img { max-width: 92vw; max-height: 88vh; border-radius: 8px; background: #fff; }
#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--top); color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 13px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 200; max-width: 90vw; }
#toast.show { opacity: 1; }
#toast.err { background: var(--danger); }

/* ---------- responsive ---------- */
@media (max-width: 1280px) { .side { display: none; } .side.force { display: flex; position: fixed; right: 0; top: 56px; bottom: 0; z-index: 40; box-shadow: -10px 0 30px rgba(0,0,0,.12); } }
@media (max-width: 1000px) { .nav { display: none; } .nav.force { display: flex; position: fixed; left: 0; top: 56px; bottom: 0; z-index: 40; box-shadow: 10px 0 30px rgba(0,0,0,.12); } .show-sm { display: inline-flex !important; } }
@media (min-width: 1001px) { .show-sm { display: none !important; } }
@media (max-width: 760px) {
  .list { width: 100%; } .main.has-conv .list { display: none; } .main:not(.has-conv) .chat { display: none; }
  .top .brand small, .search { display: none; } .msg { max-width: 88%; } .settings { padding: 18px 16px; }
}
@media (min-width: 761px) { .back-sm { display: none !important; } }
