:root {
  --bg:#0a0a0f;
  --bg-2:#11111a;
  --bg-3:#1a1a26;
  --line:#262633;
  --line-2:#33333f;
  --fg:#e6e6f0;
  --fg-2:#a8a8b8;
  --fg-3:#6b6b7a;
  --accent:#818cf8;
  --accent-2:#6366f1;
  --accent-3:#4f46e5;
  --accent-glow:rgba(129,140,248,.25);
  --green:#4ade80;
  --yellow:#fbbf24;
  --red:#f87171;
  --pink:#f472b6;
  --purple:#a78bfa;
}
* { box-sizing: border-box; margin:0; padding:0; }
html, body { background:var(--bg); color:var(--fg); font-family:'Inter','Noto Sans Thai',system-ui,sans-serif; font-size:14px; height:100%; overflow:hidden; -webkit-font-smoothing:antialiased; }
button, input, textarea { font-family: inherit; color: inherit; }
input, textarea { background:var(--bg-3); border:1px solid var(--line); border-radius:8px; padding:10px 12px; color:var(--fg); outline:none; transition: border-color .15s; font-size:14px; }
input:focus, textarea:focus { border-color: var(--accent); }
button { background:none; border:none; cursor:pointer; color:var(--fg-2); }
button:disabled { opacity:.4; cursor:not-allowed; }

/* === VIEWS === */
.view { display:none; height:100%; }
.view.active { display:flex; }

/* === LOGIN === */
#view-login { align-items:center; justify-content:center; }
.login-card { background:var(--bg-2); border:1px solid var(--line); border-radius:18px; padding:32px; width:min(420px,92vw); box-shadow:0 24px 64px rgba(0,0,0,.4); }
.login-logo { display:flex; align-items:center; gap:14px; margin-bottom:24px; }
.login-logo .logo-mark { width:48px; height:48px; border-radius:14px; background:linear-gradient(135deg,#6366f1,#a78bfa); display:grid; place-items:center; font-size:22px; font-weight:700; color:#fff; box-shadow:0 8px 24px var(--accent-glow); }
.brand { font-size:22px; font-weight:700; }
.tagline { color:var(--fg-3); font-size:13px; }
#login-form { display:flex; flex-direction:column; gap:14px; }
#login-form label { display:flex; flex-direction:column; gap:6px; }
#login-form span { color:var(--fg-2); font-size:13px; font-weight:500; }
.btn-primary { background:linear-gradient(135deg,var(--accent-2),var(--accent)); color:#fff; padding:11px 18px; border-radius:10px; font-weight:600; font-size:14px; box-shadow:0 4px 14px var(--accent-glow); transition: transform .12s, opacity .12s; }
.btn-primary:hover { transform: translateY(-1px); }
.btn-primary.sm { padding:8px 14px; font-size:13px; }
.btn-ghost { background:transparent; color:var(--fg-2); padding:11px 18px; border-radius:10px; font-weight:500; }
.btn-ghost:hover { color:var(--fg); background:var(--bg-3); }
.login-error { color:var(--red); font-size:13px; min-height:18px; }
.login-hint { margin-top:18px; padding:12px; background:var(--bg-3); border-radius:8px; color:var(--fg-3); }
.login-hint code { background:var(--bg-2); padding:2px 6px; border-radius:4px; font-size:11px; color:var(--accent); font-family:'JetBrains Mono',monospace; }

/* === APP LAYOUT === */
#view-app { display:none; }
#view-app.active { display:grid; grid-template-columns: 340px 1fr; }
@media(max-width:768px){
  #view-app.active { grid-template-columns: 1fr; }
  #view-app.active.show-chat #sidebar { display:none; }
  #view-app.active.show-chat #chat-panel { display:flex; }
}
#view-app:not(.active) { display:none; }

#sidebar { background:var(--bg-2); border-right:1px solid var(--line); display:flex; flex-direction:column; min-height:0; }
@media(max-width:768px){
  #sidebar { border-right:none; }
}
.side-header { display:flex; align-items:center; gap:10px; padding:14px 16px; border-bottom:1px solid var(--line); }
.side-header .logo-mark { width:32px; height:32px; border-radius:8px; background:linear-gradient(135deg,#6366f1,#a78bfa); display:grid; place-items:center; font-size:14px; font-weight:700; color:#fff; flex:0 0 auto; }
.side-header .logo-mark.sm { display:none; }
.logo-mark, .logo-mark.sm { width:32px; height:32px; border-radius:8px; background:linear-gradient(135deg,#6366f1,#a78bfa); display:grid; place-items:center; font-size:14px; font-weight:700; color:#fff; }
.side-header .me { flex:1; min-width:0; }
.me-name { font-weight:600; font-size:14px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.me-role { font-size:11px; color:var(--fg-3); text-transform:uppercase; letter-spacing:.05em; }
.icon-btn { width:36px; height:36px; border-radius:8px; display:grid; place-items:center; color:var(--fg-2); transition:background .12s, color .12s; }
.icon-btn:hover { background:var(--bg-3); color:var(--fg); }

.side-tabs { display:flex; padding:8px 12px 0; gap:4px; }
.side-tabs .tab { flex:1; padding:10px; border-radius:8px 8px 0 0; font-size:13px; font-weight:500; color:var(--fg-3); border-bottom:2px solid transparent; transition:all .15s; }
.side-tabs .tab.active { color:var(--fg); border-bottom-color:var(--accent); background:var(--bg-3); }
.side-list { flex:1; overflow-y:auto; padding:6px; }

.conv-item, .shop-item { display:flex; align-items:center; gap:12px; padding:12px; border-radius:10px; cursor:pointer; transition: background .12s; position:relative; }
.conv-item:hover, .shop-item:hover { background:var(--bg-3); }
.conv-item.active, .shop-item.active { background:var(--bg-3); }
.avatar { width:42px; height:42px; border-radius:12px; background:linear-gradient(135deg,#6366f1,#a78bfa); display:grid; place-items:center; color:#fff; font-weight:600; font-size:15px; flex:0 0 auto; position:relative; }
.avatar.online::after { content:''; position:absolute; bottom:1px; right:1px; width:11px; height:11px; background:var(--green); border-radius:50%; border:2px solid var(--bg-2); box-shadow:0 0 6px var(--green); }
.conv-meta, .shop-meta { flex:1; min-width:0; }
.conv-name, .shop-name { font-weight:600; font-size:14px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.conv-preview { color:var(--fg-3); font-size:12px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; margin-top:2px; }
.conv-preview .conv-sub { color:var(--fg-3); }
.conv-time { font-size:11px; color:var(--fg-3); white-space:nowrap; }
.conv-badge { position:absolute; right:10px; bottom:10px; min-width:20px; height:20px; padding:0 6px; border-radius:10px; background:var(--accent-3); color:#fff; font-size:11px; font-weight:600; display:grid; place-items:center; }

.side-foot { padding:12px; border-top:1px solid var(--line); }

.empty-list { padding:30px 16px; text-align:center; color:var(--fg-3); font-size:13px; }

/* === CHAT PANEL === */
#chat-panel { display:flex; flex-direction:column; min-height:0; background: var(--bg); position:relative; }
#chat-header { display:flex; align-items:center; gap:10px; padding:14px 18px; border-bottom:1px solid var(--line); background:var(--bg-2); }
.chat-peer { display:flex; align-items:center; gap:12px; flex:1; min-width:0; }
.peer-info { min-width:0; }
.peer-name { font-weight:600; font-size:15px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.peer-status { display:flex; align-items:center; gap:6px; font-size:12px; color:var(--fg-3); }
.dot { width:8px; height:8px; border-radius:50%; background:var(--fg-3); flex:0 0 auto; }
.dot.online { background:var(--green); box-shadow:0 0 6px var(--green); }

.messages { flex:1; overflow-y:auto; padding:20px 16px; display:flex; flex-direction:column; gap:6px; }
.messages-empty, .chat-empty { display:flex; flex-direction:column; align-items:center; justify-content:center; padding:40px; text-align:center; color:var(--fg-3); flex:1; }
.chat-empty { display:none; }
#chat-panel.no-conv .chat-empty { display:flex; }
#chat-panel.no-conv #messages, #chat-panel.no-conv .composer, #chat-panel.no-conv #chat-header, #chat-panel.no-conv .typing-row { display:none; }
.empty-icon { font-size:48px; margin-bottom:12px; }
.empty-icon.big { font-size:64px; }
.empty-state h2, .empty-state h3 { color:var(--fg); font-weight:600; margin-bottom:6px; }
.empty-state p { font-size:13px; max-width:300px; line-height:1.6; }

/* Message bubbles */
.msg { display:flex; gap:8px; max-width:78%; animation: msgIn .25s ease-out; }
@keyframes msgIn { from { opacity:0; transform:translateY(6px); } to { opacity:1; transform:translateY(0); } }
.msg.mine { align-self:flex-end; flex-direction:row-reverse; }
.msg-body { padding:9px 14px; border-radius:16px; background:var(--bg-3); font-size:14px; line-height:1.5; word-wrap:break-word; max-width:100%; }
.msg.mine .msg-body { background:linear-gradient(135deg,var(--accent-2),var(--accent-3)); color:#fff; }
.msg-sender { font-size:11px; color:var(--fg-3); margin-bottom:3px; padding:0 4px; }
.msg-mine-sender { display:none; }
.msg-meta { display:flex; align-items:center; gap:6px; margin-top:4px; font-size:11px; color:var(--fg-3); justify-content:flex-end; }
.msg.mine .msg-meta { color: rgba(255,255,255,.65); }
.msg-attachment { display:block; max-width:280px; max-height:280px; border-radius:10px; margin-bottom:4px; }
.msg-attachment.file { display:flex; align-items:center; gap:10px; padding:10px 14px; background:var(--bg-2); border:1px solid var(--line); border-radius:10px; color:var(--fg-2); }
.msg-attachment.file svg { flex:0 0 auto; }
.msg-content { white-space:pre-wrap; word-break:break-word; }

.system-msg { text-align:center; color:var(--fg-3); font-size:12px; padding:10px; margin:8px auto; }
.system-msg span { background:var(--bg-3); padding:4px 12px; border-radius:10px; }

.typing-row { display:flex; align-items:center; gap:8px; padding:4px 18px 8px; color:var(--fg-3); font-size:12px; }
.typing-dots { display:inline-flex; align-items:center; gap:3px; }
.typing-dots i { width:5px; height:5px; border-radius:50%; background:var(--fg-3); animation: bounce 1.4s infinite; }
.typing-dots i:nth-child(2) { animation-delay: .2s; }
.typing-dots i:nth-child(3) { animation-delay: .4s; }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity:.4; } 30% { transform: translateY(-3px); opacity:1; } }

.composer { display:flex; align-items:flex-end; gap:8px; padding:12px 16px 16px; border-top:1px solid var(--line); background:var(--bg-2); }
#composer-input { flex:1; resize:none; max-height:120px; padding:10px 14px; line-height:1.5; }
.btn-send { width:42px; height:42px; border-radius:10px; background:linear-gradient(135deg,var(--accent-2),var(--accent-3)); color:#fff; display:grid; place-items:center; transition: transform .12s, opacity .12s; }
.btn-send:hover { transform: translateY(-1px); }
.btn-send:disabled { opacity:.4; transform:none; }

.only-mobile { display:none; }
@media(max-width:768px){
  .only-mobile { display:grid; }
  .btn-toggle-list { display:grid !important; }
  #view-app.active:not(.show-chat) #chat-panel { display:none; }
}

/* === MODAL === */
.modal { position:fixed; inset:0; background:rgba(0,0,0,.6); display:grid; place-items:center; padding:20px; z-index:100; animation: fadeIn .15s; }
.modal[hidden] { display: none !important; }
@keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
.modal-card { background:var(--bg-2); border:1px solid var(--line); border-radius:14px; padding:24px; width:min(440px, 100%); box-shadow:0 20px 60px rgba(0,0,0,.6); }
.modal-card h3 { font-size:17px; margin-bottom:18px; }
.modal-card form { display:flex; flex-direction:column; gap:12px; }
.modal-card label { display:flex; flex-direction:column; gap:5px; font-size:13px; color:var(--fg-2); }
.modal-error { color:var(--red); font-size:12.5px; min-height:18px; }
.modal-actions { display:flex; justify-content:flex-end; gap:8px; margin-top:6px; }

::-webkit-scrollbar { width:8px; height:8px; }
::-webkit-scrollbar-track { background:transparent; }
::-webkit-scrollbar-thumb { background:var(--line-2); border-radius:4px; }
::-webkit-scrollbar-thumb:hover { background:var(--accent); }

/* === Date separators === */
.date-sep { display:flex; align-items:center; justify-content:center; margin:14px 0 6px; position:relative; }
.date-sep::before, .date-sep::after { content:''; flex:1; height:1px; background:var(--line); margin:0 12px; }
.date-sep span { font-size:11px; color:var(--fg-3); background:var(--bg-2); padding:4px 12px; border-radius:10px; border:1px solid var(--line); white-space:nowrap; }

/* === Message status icons === */
.msg-status { font-size:11px; opacity:.85; display:inline-flex; align-items:center; gap:2px; margin-left:2px; }
.msg-status.sent { color:rgba(255,255,255,.65); }
.msg-status.read { color:#a7f3d0; }
.msg-status.failed { color:var(--red); font-weight:700; background:rgba(248,113,113,.15); border-radius:50%; width:16px; height:16px; display:grid; place-items:center; }
.msg-status.sending .spin { display:inline-block; width:8px; height:8px; border:1.5px solid rgba(255,255,255,.4); border-top-color:#fff; border-radius:50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.msg.failed .msg-body { border:1px dashed var(--red); }
.msg.pending { opacity:.7; }

/* === Sound toggle button === */
#btn-sound .ic-off { display:none; }
#btn-sound[aria-pressed=true] .ic-on { display:none; }
#btn-sound[aria-pressed=true] .ic-off { display:block; }
#btn-sound[aria-pressed=true] { color:var(--accent); }

/* === Lightbox === */
.lightbox { position:fixed; inset:0; background:rgba(0,0,0,.92); display:grid; place-items:center; z-index:200; padding:20px; cursor:zoom-out; animation: fadeIn .15s; }
.lightbox[hidden] { display:none !important; }
.lightbox img { max-width:96vw; max-height:96vh; border-radius:10px; box-shadow:0 12px 48px rgba(0,0,0,.5); cursor:auto; }
.lightbox-close { position:fixed; top:18px; right:18px; width:44px; height:44px; border-radius:50%; background:rgba(255,255,255,.12); color:#fff; font-size:32px; line-height:1; display:grid; place-items:center; border:0; cursor:pointer; transition: background .15s; }
.lightbox-close:hover { background:rgba(255,255,255,.22); }

/* === Toast notifications === */
#toast-container { position:fixed; right:16px; bottom:16px; display:flex; flex-direction:column; gap:8px; z-index:150; pointer-events:none; max-width:340px; }
.toast { display:flex; align-items:center; gap:10px; padding:10px 12px; background:var(--bg-2); border:1px solid var(--line-2); border-radius:12px; box-shadow:0 10px 30px rgba(0,0,0,.5); cursor:pointer; pointer-events:auto; transform: translateX(120%); transition: transform .25s ease-out; }
.toast.show { transform: translateX(0); }
.toast:hover { border-color:var(--accent); }
.toast .toast-avatar { width:36px; height:36px; border-radius:10px; background:linear-gradient(135deg,#6366f1,#a78bfa); display:grid; place-items:center; color:#fff; font-weight:600; font-size:14px; flex:0 0 auto; }
.toast .toast-body { flex:1; min-width:0; }
.toast .toast-name { font-size:13px; font-weight:600; color:var(--fg); }
.toast .toast-preview { font-size:12px; color:var(--fg-2); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; margin-top:2px; }
.toast .toast-time { color:var(--fg-3); font-size:11px; flex:0 0 auto; }

/* === Tabs === */
.tab-badge { display:inline-block; min-width:18px; height:18px; padding:0 6px; border-radius:9px; background:var(--red); color:#fff; font-size:10px; font-weight:600; vertical-align:1px; margin-left:4px; }
.tab-badge[hidden] { display:none; }

/* === Tickets === */
.ticket-row { display:grid; grid-template-columns:1fr 1fr; gap:12px; }
.ticket-row label { display:flex; flex-direction:column; gap:5px; font-size:13px; color:var(--fg-2); }
.ticket-row select { background:var(--bg-3); border:1px solid var(--line); border-radius:8px; padding:10px 12px; color:var(--fg); font-size:14px; outline:none; }
.ticket-row select:focus { border-color:var(--accent); }
.modal-card textarea { background:var(--bg-3); border:1px solid var(--line); border-radius:8px; padding:10px 12px; color:var(--fg); font-size:14px; outline:none; resize:vertical; }
.modal-card textarea:focus { border-color:var(--accent); }

.ticket-item { display:flex; flex-direction:column; gap:6px; padding:12px; border-radius:10px; cursor:pointer; transition: background .12s; border:1px solid transparent; }
.ticket-item:hover { background:var(--bg-3); }
.ticket-item.active { background:var(--bg-3); border-color:var(--line-2); }
.ticket-item-head { display:flex; align-items:center; gap:8px; }
.ticket-id { font-family:'JetBrains Mono',monospace; font-size:11px; color:var(--fg-3); }
.ticket-title-text { font-weight:600; font-size:14px; color:var(--fg); flex:1; min-width:0; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.ticket-preview { font-size:12px; color:var(--fg-3); overflow:hidden; text-overflow:ellipsis; display:-webkit-box; -webkit-line-clamp:2; -webkit-box-orient:vertical; }
.ticket-meta { display:flex; align-items:center; gap:6px; font-size:11px; color:var(--fg-3); }

.priority-low { color:#6b7280; }
.priority-normal { color:#60a5fa; }
.priority-high { color:#fbbf24; }
.priority-urgent { color:#f87171; }
.priority-low, .priority-normal, .priority-high, .priority-urgent { font-weight:600; }
.priority-urgent::before { content:''; }

.ticket-pill { display:inline-block; padding:3px 8px; border-radius:6px; font-size:11px; font-weight:600; line-height:1.3; border:1px solid var(--line); background:var(--bg-2); }
.ticket-pill[data-status=open] { background:rgba(96,165,250,.15); color:#60a5fa; border-color:rgba(96,165,250,.3); }
.ticket-pill[data-status=in_progress] { background:rgba(251,191,36,.15); color:#fbbf24; border-color:rgba(251,191,36,.3); }
.ticket-pill[data-status=waiting] { background:rgba(167,139,250,.15); color:#a78bfa; border-color:rgba(167,139,250,.3); }
.ticket-pill[data-status=resolved] { background:rgba(74,222,128,.15); color:#4ade80; border-color:rgba(74,222,128,.3); }
.ticket-pill[data-status=closed] { background:rgba(107,114,128,.15); color:#9ca3af; border-color:rgba(107,114,128,.3); }
.ticket-pill[data-priority=urgent] { background:rgba(248,113,113,.15); color:#f87171; border-color:rgba(248,113,113,.3); }

/* === Ticket detail view === */
#view-ticket { display:flex; flex-direction:column; min-height:0; background:var(--bg); }
#view-ticket[hidden] { display:none; }
#ticket-detail-header { display:flex; align-items:flex-start; gap:14px; padding:18px 20px; border-bottom:1px solid var(--line); background:var(--bg-2); }
.ticket-detail-meta { flex:1; min-width:0; }
.tdt-row { display:flex; align-items:center; gap:6px; margin-bottom:6px; flex-wrap:wrap; }
.ticket-detail-meta h2 { font-size:18px; font-weight:600; margin-bottom:4px; }
.td-sub { font-size:12px; color:var(--fg-3); }
.ticket-actions { display:flex; align-items:center; gap:6px; flex-wrap:wrap; }
.ticket-actions .btn-ghost { padding:6px 12px; font-size:12px; border-radius:8px; border:1px solid var(--line-2); }

.ticket-thread { flex:1; overflow-y:auto; padding:20px 16px; display:flex; flex-direction:column; gap:12px; }
.ticket-msg { max-width:78%; padding:10px 14px; border-radius:14px; background:var(--bg-3); }
.ticket-msg.mine { align-self:flex-end; background:linear-gradient(135deg,var(--accent-2),var(--accent-3)); color:#fff; }
.ticket-msg-meta { display:flex; align-items:center; gap:8px; margin-bottom:4px; font-size:11px; color:var(--fg-3); }
.ticket-msg.mine .ticket-msg-meta { color:rgba(255,255,255,.7); }
.ticket-msg-content { white-space:pre-wrap; word-break:break-word; }
.ticket-msg-system { text-align:center; align-self:center; max-width:none; background:rgba(167,139,250,.12); color:var(--purple); font-size:12px; padding:6px 14px; border-radius:14px; }
.ticket-composer { background:var(--bg-2); }
.ticket-composer textarea { flex:1; resize:none; max-height:140px; }

/* === Ticket cards in chat === */
.msg.ticket-msg-card .msg-body { padding:0; background:transparent; box-shadow:none; }
.msg.ticket-msg-card.mine .msg-body { background:transparent; }
.ticket-card { background:var(--bg-2); border:1px solid var(--line-2); border-radius:12px; padding:12px 14px; min-width:240px; }
.msg.mine .ticket-card { background:linear-gradient(135deg,var(--accent-2),var(--accent-3)); border:none; color:#fff; }
.ticket-card.system { background:rgba(167,139,250,.12); color:var(--purple); border:1px solid rgba(167,139,250,.3); text-align:center; font-size:12px; }
.ticket-card-head { display:flex; align-items:center; gap:6px; flex-wrap:wrap; margin-bottom:6px; }
.ticket-card-content { font-size:13px; line-height:1.5; margin-bottom:8px; white-space:pre-wrap; }
.ticket-card .btn-ghost.sm { padding:5px 10px; font-size:11px; border-radius:6px; border:1px solid var(--line-2); background:rgba(255,255,255,.08); color:#fff; }
.ticket-card .btn-ghost.sm:hover { background:rgba(255,255,255,.16); }
