:root {
    --sidebar-w: 268px;
    --sidebar-bg: #0f172a;
    --sidebar-fg: #cbd5e1;
    --sidebar-muted: #64748b;
    --accent: #2563eb;
    --bg: #f3f5fa;
    --card-radius: 14px;
    --c-blue: #3b82f6;
    --c-violet: #8b5cf6;
    --c-teal: #14b8a6;
    --c-rose: #f43f5e;
    --c-gray: #94a3b8;
}

html, body { height: 100%; }
body {
    font-family: "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
    background: var(--bg);
    color: #1e293b;
    font-size: 15px;
}
a { text-decoration: none; }
.small, small { font-size: .84em; }

/* ---------- Shell ---------- */
.app-shell { display: flex; min-height: 100vh; }
.app-sidebar {
    position: fixed; inset: 0 auto 0 0; width: var(--sidebar-w); z-index: 1040;
    background: var(--sidebar-bg); color: var(--sidebar-fg);
    display: flex; flex-direction: column; overflow-y: auto;
    transition: transform .2s ease;
}
.app-sidebar::-webkit-scrollbar { width: 6px; }
.app-sidebar::-webkit-scrollbar-thumb { background: #334155; border-radius: 6px; }
.brand { display: flex; align-items: center; gap: .6rem; padding: 1.1rem 1.2rem; color: #fff; font-weight: 700; font-size: 1.05rem; }
.brand:hover { color: #fff; }
.brand-logo {
    width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center;
    background: linear-gradient(135deg, #3b82f6, #8b5cf6 55%, #f43f5e); color: #fff;
}
.side-nav { padding: .25rem .6rem 1rem; flex: 1; }
.side-section {
    margin: 1rem .6rem .35rem; font-size: .74rem; font-weight: 700; letter-spacing: .02em;
    color: var(--sidebar-muted); text-transform: uppercase; display: flex; align-items: center; gap: .4rem;
}
.side-section i { font-size: .95rem; }
.side-blue i { color: var(--c-blue); }
.side-violet i { color: var(--c-violet); }
.side-teal i { color: var(--c-teal); }
.side-rose i { color: var(--c-rose); }
.side-gray i { color: var(--c-gray); }
.side-link {
    display: flex; align-items: center; gap: .65rem; padding: .52rem .75rem; margin: 1px 0;
    border-radius: 10px; color: var(--sidebar-fg); font-size: .93rem;
}
.side-link i { font-size: 1.05rem; opacity: .85; width: 1.2rem; text-align: center; }
.side-link.sub { padding-left: .9rem; }
.side-link:hover { background: #1e293b; color: #fff; }
.side-link.active { background: var(--accent); color: #fff; }
.side-link.active i { opacity: 1; }
.side-footer { padding: .8rem 1.2rem; font-size: .75rem; color: var(--sidebar-muted); }

.app-main { flex: 1; margin-left: var(--sidebar-w); min-width: 0; display: flex; flex-direction: column; }
.app-topbar {
    position: sticky; top: 0; z-index: 1030; display: flex; align-items: center; gap: .75rem;
    padding: .7rem 1.5rem; background: rgba(255,255,255,.92); backdrop-filter: blur(8px);
    border-bottom: 1px solid #e5e9f2;
}
.page-title { font-size: 1.15rem; font-weight: 700; margin: 0; }
.app-content { padding: 1.4rem 1.5rem 3rem; max-width: 1400px; width: 100%; }
.app-content.fluid { max-width: none; }
.avatar-sm {
    width: 26px; height: 26px; border-radius: 50%; display: inline-grid; place-items: center;
    background: var(--accent); color: #fff; font-size: .8rem; font-weight: 700;
}
.sidebar-backdrop { display: none; }

@media (max-width: 991.98px) {
    .app-sidebar { transform: translateX(-100%); }
    .app-sidebar.show { transform: none; }
    .app-main { margin-left: 0; }
    .sidebar-backdrop.show { display: block; position: fixed; inset: 0; background: rgba(15,23,42,.45); z-index: 1035; }
    .app-content { padding: 1rem .9rem 3rem; }
    .app-topbar { padding: .6rem .9rem; }
}

/* ---------- Cards / UI ---------- */
.card { border: 0; border-radius: var(--card-radius); box-shadow: 0 1px 2px rgba(15,23,42,.05), 0 1px 12px rgba(15,23,42,.04); }
.card-header { background: transparent; border-bottom: 1px solid #eef1f6; font-weight: 600; padding: .85rem 1.1rem; }
.card-body { padding: 1.1rem; }
.table > :not(caption) > * > * { padding: .6rem .7rem; }
.table thead th { font-size: .8rem; color: #64748b; font-weight: 600; background: #f8fafc; white-space: nowrap; }
.table-hover tbody tr:hover { background: #f8fafc; }
.form-label { font-weight: 500; font-size: .9rem; margin-bottom: .3rem; }
.form-text { font-size: .8rem; }
.btn { border-radius: 9px; }
.form-control, .form-select { border-radius: 9px; }
.badge { font-weight: 500; }
.text-pre { white-space: pre-wrap; word-break: break-word; }
.min-w-0 { min-width: 0; }
.cursor-pointer { cursor: pointer; }
.hint { font-size: .82rem; color: #64748b; }
.code-chip {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .8rem;
    background: #eef2ff; color: #3730a3; border-radius: 6px; padding: .1rem .4rem; cursor: pointer; white-space: nowrap;
}
.code-chip:hover { background: #e0e7ff; }

/* ช่องคีย์เวิร์ดแบบชิป (App.tagInput) */
.tag-input-source { display: none !important; }
.tag-input {
    display: flex; flex-wrap: wrap; align-items: center; gap: .3rem;
    height: auto; min-height: calc(1.5em + .75rem + 2px); padding: .3rem .5rem; cursor: text;
}
.tag-input.form-control-sm { min-height: calc(1.5em + .5rem + 2px); padding: .15rem .4rem; }
.tag-input:focus-within { border-color: #86b7fe; box-shadow: 0 0 0 .25rem rgba(13, 110, 253, .25); }
.tag-input.disabled { background: #e9ecef; cursor: default; }
.tag-input-field {
    flex: 1 1 140px; min-width: 140px; border: 0; outline: 0; background: transparent;
    padding: .15rem .1rem; font: inherit; color: inherit; min-height: 1.6em;
}
.tag-input-field::placeholder { color: var(--bs-secondary-color, #6c757d); }
.tag-chip {
    display: inline-flex; align-items: center; gap: .15rem; max-width: 100%;
    background: #e0e7ff; color: #1e3a8a; border-radius: 999px; padding: .12rem .25rem .12rem .65rem;
    font-size: .88rem; line-height: 1.45; font-weight: 500; cursor: default;
    transition: background-color .15s, color .15s;
}
.tag-chip-text { white-space: pre-wrap; word-break: break-word; }
.tag-chip-remove {
    border: 0; background: transparent; color: inherit; opacity: .85; font-size: 1.05rem; line-height: 1;
    width: 1.35em; height: 1.35em; border-radius: 50%; cursor: pointer; padding: 0; display: grid; place-items: center;
}
.tag-chip-remove:hover { opacity: 1; background: rgba(30, 58, 138, .14); }
.tag-chip-remove:focus-visible { opacity: 1; outline: 2px solid #2563eb; outline-offset: 1px; }
.tag-chip-special { background: #fef3c7; color: #92400e; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.tag-chip.is-selected { background: #1e3a8a; color: #fff; }
.tag-chip-special.is-selected { background: #92400e; color: #fff; }
.tag-chip.is-dup { animation: tag-chip-flash .9s ease-out; }
@keyframes tag-chip-flash {
    0%, 60% { background: #fde68a; color: #78350f; box-shadow: 0 0 0 3px rgba(245, 158, 11, .35); }
    100% { box-shadow: 0 0 0 0 rgba(245, 158, 11, 0); }
}
.tag-input.disabled .tag-chip { opacity: .65; }
.tag-input.disabled .tag-chip-remove { pointer-events: none; }

.stat-card { display: flex; align-items: center; gap: .9rem; }
.stat-icon { width: 46px; height: 46px; border-radius: 12px; display: grid; place-items: center; font-size: 1.35rem; flex: none; }
.stat-value { font-size: 1.45rem; font-weight: 700; line-height: 1.1; }
.stat-label { font-size: .82rem; color: #64748b; }
.bg-soft-blue { background: #eff6ff; color: var(--c-blue); }
.bg-soft-violet { background: #f5f3ff; color: var(--c-violet); }
.bg-soft-teal { background: #f0fdfa; color: var(--c-teal); }
.bg-soft-rose { background: #fff1f2; color: var(--c-rose); }
.bg-soft-amber { background: #fffbeb; color: #d97706; }
.bg-soft-gray { background: #f1f5f9; color: #475569; }

.module-card { border-top: 4px solid var(--c-gray); height: 100%; }
.module-card.m-blue { border-top-color: var(--c-blue); }
.module-card.m-violet { border-top-color: var(--c-violet); }
.module-card.m-teal { border-top-color: var(--c-teal); }
.module-card.m-rose { border-top-color: var(--c-rose); }

.page-avatar { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; background: #e2e8f0; flex: none; }
.empty-state { text-align: center; padding: 2.5rem 1rem; color: #64748b; }
.empty-state i { font-size: 2.2rem; display: block; margin-bottom: .5rem; color: #94a3b8; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: .3rem; }
.nav-tabs .nav-link { color: #475569; }
.nav-tabs .nav-link.active { font-weight: 600; }
.sticky-actions { position: sticky; bottom: 0; background: rgba(243,245,250,.95); padding: .8rem 0; z-index: 5; }

/* ---------- Bubble editor (chatbot replies) ---------- */
.bubble-list { display: flex; flex-direction: column; gap: .75rem; }
.bubble-item { border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
.bubble-head { display: flex; align-items: center; gap: .5rem; padding: .5rem .75rem; border-bottom: 1px solid #eef1f6; background: #f8fafc; border-radius: 12px 12px 0 0; }
.bubble-head .type { font-weight: 600; font-size: .88rem; }
.bubble-body { padding: .75rem; }
.bubble-sub { border: 1px dashed #cbd5e1; border-radius: 10px; padding: .6rem; background: #fcfdff; }
.variant-row + .variant-row { margin-top: .5rem; }
.thumb { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; border: 1px solid #e2e8f0; }

/* ---------- Inbox ---------- */
.inbox-wrap { display: grid; grid-template-columns: 340px 1fr; height: calc(100vh - 130px); min-height: 520px; background: #fff; border-radius: var(--card-radius); overflow: hidden; box-shadow: 0 1px 12px rgba(15,23,42,.05); }
.inbox-list { border-right: 1px solid #eef1f6; display: flex; flex-direction: column; min-height: 0; }
.inbox-filters { padding: .7rem; border-bottom: 1px solid #eef1f6; }
.inbox-items { overflow-y: auto; flex: 1; }
.inbox-item { display: flex; gap: .6rem; padding: .7rem .8rem; border-bottom: 1px solid #f1f5f9; cursor: pointer; }
.inbox-item:hover { background: #f8fafc; }
.inbox-item.active { background: #eff6ff; }
.inbox-item .name { font-weight: 600; font-size: .92rem; }
.inbox-item .preview { font-size: .82rem; color: #64748b; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.inbox-item .time { font-size: .72rem; color: #94a3b8; white-space: nowrap; }
.chat-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.chat-head { padding: .7rem 1rem; border-bottom: 1px solid #eef1f6; display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; }
.chat-thread { flex: 1; overflow-y: auto; padding: 1rem; background: #f8fafc; display: flex; flex-direction: column; gap: .35rem; }
.msg { max-width: 72%; padding: .5rem .8rem; border-radius: 16px; font-size: .92rem; white-space: pre-wrap; word-break: break-word; position: relative; }
.msg.in { align-self: flex-start; background: #fff; border: 1px solid #e2e8f0; border-bottom-left-radius: 6px; }
.msg.out { align-self: flex-end; background: #2563eb; color: #fff; border-bottom-right-radius: 6px; }
.msg.out.ai { background: #0d9488; }
.msg.out.admin, .msg.out.page { background: #475569; }
.msg.system { align-self: center; background: transparent; color: #94a3b8; font-size: .78rem; }
.msg-meta { font-size: .7rem; opacity: .75; margin-top: .15rem; }
.msg-actions { position: absolute; top: -12px; display: none; gap: 3px; z-index: 2; }
.msg.in .msg-actions { right: -10px; }
.msg.out .msg-actions { left: -10px; }
.msg:hover .msg-actions { display: flex; }
.msg-actions button { width: 26px; height: 26px; padding: 0; border-radius: 50%; border: 1px solid #e2e8f0; background: #fff; color: #475569; font-size: .8rem; line-height: 1; box-shadow: 0 1px 3px rgba(15,23,42,.12); }
.msg-actions button:hover { color: var(--accent); border-color: #bfdbfe; }
@media (hover: none) {
    .msg-actions { display: flex; position: static; justify-content: flex-end; margin-top: .25rem; }
}
.msg img { max-width: 220px; border-radius: 10px; display: block; }
.chat-compose { border-top: 1px solid #eef1f6; padding: .7rem; }
.chat-empty { flex: 1; display: grid; place-items: center; color: #94a3b8; }
@media (max-width: 991.98px) {
    .inbox-wrap { grid-template-columns: 1fr; height: auto; }
    .inbox-wrap.has-chat .inbox-list { display: none; }
    .inbox-wrap:not(.has-chat) .chat-pane { display: none; }
    .chat-thread { height: 60vh; }
    .msg { max-width: 88%; }
}

/* ---------- Live room ---------- */
.live-grid { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); gap: 1rem; }
.feed { height: calc(100vh - 280px); min-height: 420px; overflow-y: auto; }
.feed-item { display: flex; gap: .6rem; padding: .55rem .8rem; border-bottom: 1px solid #f1f5f9; }
.feed-item.cf { background: #f0fdf4; }
.feed-item.cf-fail { background: #fff7ed; }
.feed-item .who { font-weight: 600; font-size: .88rem; }
.feed-item .txt { font-size: .92rem; word-break: break-word; }
.feed-item .res { font-size: .78rem; color: #15803d; }
.feed-item.cf-fail .res { color: #c2410c; }
.live-dot { width: 10px; height: 10px; border-radius: 50%; background: #ef4444; display: inline-block; animation: pulse 1.4s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(239,68,68,.6); } 70% { box-shadow: 0 0 0 8px rgba(239,68,68,0); } 100% { box-shadow: 0 0 0 0 rgba(239,68,68,0); } }
.stock-pill { font-size: .78rem; border-radius: 999px; padding: .15rem .55rem; background: #f1f5f9; }
.stock-pill.low { background: #fef3c7; }
.stock-pill.out { background: #fee2e2; color: #b91c1c; }
@media (max-width: 991.98px) {
    .live-grid { grid-template-columns: 1fr; }
    .feed { height: 55vh; }
}

/* ---------- Login ---------- */
.login-page { min-height: 100vh; display: grid; place-items: center; background: radial-gradient(1200px 600px at 10% -10%, #dbeafe 0, transparent 60%), radial-gradient(900px 500px at 110% 110%, #fce7f3 0, transparent 60%), #f8fafc; padding: 1rem; }
.login-card { width: 100%; max-width: 400px; }
.login-brand { width: 56px; height: 56px; border-radius: 16px; display: grid; place-items: center; font-size: 1.6rem; color: #fff; background: linear-gradient(135deg, #3b82f6, #8b5cf6 55%, #f43f5e); margin: 0 auto 1rem; }
