:root {
    --primary: #1877f2;
    --primary-dark: #0f5fd0;
    --primary-soft: #e7f1ff;
    --grad: linear-gradient(135deg, #1877f2, #42a5ff);
    --bg: #f0f2f5;
    --card: #ffffff;
    --border: #e4e6eb;
    --text: #050505;
    --muted: #65676b;
    --green: #31a24c;
    --green-soft: #e6f5ea;
    --red: #e41e3f;
    --red-soft: #fdecef;
    --amber: #b8860b;
    --amber-soft: #fff6dd;
    --radius: 12px;
    --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 4px 16px rgba(0, 0, 0, .05);
    --shadow-lg: 0 8px 30px rgba(0, 0, 0, .12);
    --sidebar-w: 250px;
    --topbar-h: 62px;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans Thai", Tahoma, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: none; }
h1, h2, h3, h4 { margin: 0; font-weight: 700; }
p { margin: 0; }

/* ============ Layout ============ */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: #fff;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 20px 22px;
    font-size: 19px;
    font-weight: 800;
    color: var(--primary);
    border-bottom: 1px solid var(--border);
}
.brand .logo {
    width: 36px; height: 36px;
    border-radius: 10px;
    background: var(--grad);
    display: flex; align-items: center; justify-content: center;
    color: #fff; font-size: 19px;
}

.user-chip {
    display: flex; align-items: center; gap: 10px;
    padding: 16px 22px;
    border-bottom: 1px solid var(--border);
}
.user-chip .meta { min-width: 0; }
.user-chip .name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-chip .role { font-size: 12px; color: var(--muted); }

.nav { padding: 12px; flex: 1; }
.nav a {
    display: flex; align-items: center; gap: 12px;
    padding: 11px 14px;
    border-radius: 10px;
    color: var(--text);
    font-weight: 600;
    margin-bottom: 2px;
    transition: background .15s;
}
.nav a:hover { background: var(--bg); }
.nav a.active { background: var(--primary-soft); color: var(--primary-dark); }
.nav a .ico { font-size: 17px; width: 20px; text-align: center; }
.nav a .badge {
    margin-left: auto;
    background: var(--red); color: #fff;
    font-size: 11px; font-weight: 700;
    min-width: 20px; height: 20px;
    border-radius: 10px;
    display: flex; align-items: center; justify-content: center;
    padding: 0 6px;
}

.sidebar-footer { padding: 14px 22px; border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); }
.demo-tag {
    display: inline-flex; align-items: center; gap: 6px;
    background: var(--amber-soft); color: var(--amber);
    padding: 5px 10px; border-radius: 8px; font-weight: 700;
    margin-bottom: 10px;
}

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
    height: var(--topbar-h);
    background: #fff;
    border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between;
    padding: 0 26px;
    position: sticky; top: 0; z-index: 20;
}
.topbar h1 { font-size: 18px; }
.topbar .top-right { display: flex; align-items: center; gap: 14px; }
.topbar .user-drop { display: flex; align-items: center; gap: 10px; }
.topbar .user-drop .name { font-weight: 700; }

.content { padding: 26px; max-width: 1200px; width: 100%; margin: 0 auto; }
.content.chat-content { max-width: none; padding: 0; height: calc(100vh - var(--topbar-h)); }

/* ============ Components ============ */
.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 22px;
}
.card + .card { margin-top: 18px; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.card-head h2 { font-size: 16px; }
.card-head .sub { font-size: 13px; color: var(--muted); }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 7px;
    padding: 10px 16px;
    border-radius: 9px;
    border: 1px solid transparent;
    font-weight: 700; font-size: 13px;
    cursor: pointer;
    transition: all .15s;
    font-family: inherit;
    white-space: nowrap;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-ghost { background: #fff; color: var(--text); border-color: var(--border); }
.btn-ghost:hover:not(:disabled) { background: var(--bg); }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger:hover:not(:disabled) { filter: brightness(.94); }
.btn-sm { padding: 6px 11px; font-size: 12px; border-radius: 7px; }
.btn-lg { padding: 13px 20px; font-size: 14px; }
.btn-block { width: 100%; }

.badge {
    display: inline-flex; align-items: center; gap: 5px;
    padding: 3px 10px; border-radius: 999px;
    font-size: 12px; font-weight: 700;
}
.badge-green { background: var(--green-soft); color: var(--green); }
.badge-red { background: var(--red-soft); color: var(--red); }
.badge-amber { background: var(--amber-soft); color: var(--amber); }
.badge-blue { background: var(--primary-soft); color: var(--primary-dark); }
.badge-gray { background: var(--bg); color: var(--muted); }

.alert {
    padding: 13px 16px; border-radius: 10px;
    margin-bottom: 18px; font-weight: 600; font-size: 13px;
}
.alert-success { background: var(--green-soft); color: #1a6b32; }
.alert-error { background: var(--red-soft); color: #a5122c; }
.alert-info { background: var(--primary-soft); color: var(--primary-dark); }

.avatar {
    width: 40px; height: 40px;
    border-radius: 50%;
    color: #fff; font-weight: 700; font-size: 14px;
    display: inline-flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    user-select: none;
}
.avatar.sm { width: 32px; height: 32px; font-size: 12px; }
.avatar.lg { width: 52px; height: 52px; font-size: 19px; }

.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-weight: 700; margin-bottom: 6px; font-size: 13px; }
.form-control {
    width: 100%;
    padding: 11px 13px;
    border: 1px solid var(--border);
    border-radius: 9px;
    font-size: 14px;
    font-family: inherit;
    background: #fff;
    transition: border .15s, box-shadow .15s;
}
.form-control:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
select.form-control { appearance: auto; }
.hint { font-size: 12px; color: var(--muted); margin-top: 5px; }

.usage-bar {
    height: 8px; background: var(--bg); border-radius: 6px; overflow: hidden;
}
.usage-bar > span {
    display: block; height: 100%;
    background: var(--grad);
    border-radius: 6px;
    transition: width .4s;
}
.usage-bar.danger > span { background: var(--red); }
.usage-bar.warn > span { background: #f0b429; }

.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--border); font-size: 13px; }
.table th { color: var(--muted); font-weight: 700; background: #fafbfc; }
.table tr:last-child td { border-bottom: none; }
.table .cell-user { display: flex; align-items: center; gap: 10px; }

.empty-state { text-align: center; padding: 60px 20px; color: var(--muted); }
.empty-icon { font-size: 42px; margin-bottom: 12px; }
.empty-state h2 { color: var(--text); margin-bottom: 6px; }
.empty-state p { margin-bottom: 18px; }

.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ============ Dashboard ============ */
.stat-grid {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 16px; margin-bottom: 20px;
}
.stat-card {
    background: #fff; border: 1px solid var(--border); border-radius: var(--radius);
    padding: 20px; box-shadow: var(--shadow);
}
.stat-card .ico {
    width: 42px; height: 42px; border-radius: 11px;
    display: flex; align-items: center; justify-content: center;
    font-size: 20px; margin-bottom: 12px;
}
.stat-card .val { font-size: 26px; font-weight: 800; }
.stat-card .lbl { color: var(--muted); font-size: 13px; }
.ico.blue { background: var(--primary-soft); }
.ico.green { background: var(--green-soft); }
.ico.red { background: var(--red-soft); }
.ico.amber { background: var(--amber-soft); }

.dash-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
@media (max-width: 900px) { .dash-grid { grid-template-columns: 1fr; } }

.package-card-top { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.package-card-top .price { font-size: 22px; font-weight: 800; color: var(--primary); }

.conv-item {
    display: flex; align-items: center; gap: 12px;
    padding: 11px 12px; border-radius: 10px;
    transition: background .12s;
}
.conv-item:hover { background: var(--bg); }
.conv-item .info { min-width: 0; flex: 1; }
.conv-item .who { font-weight: 700; }
.conv-item .preview { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .time { color: var(--muted); font-size: 11px; flex-shrink: 0; }

/* ============ Pages ============ */
.pages-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.page-card { background: #fff; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.page-card .cover { height: 70px; background: var(--grad); position: relative; }
.page-card .avatar-wrap { position: relative; margin: -28px 18px 0; }
.page-card .body { padding: 14px 18px 18px; }
.page-card .pname { font-weight: 700; font-size: 15px; }
.page-card .pcat { color: var(--muted); font-size: 12px; margin-bottom: 12px; }
.page-stats { display: flex; gap: 18px; padding: 12px 0; border-top: 1px solid var(--border); margin-bottom: 12px; }
.page-stats .s { text-align: center; }
.page-stats .num { font-weight: 800; font-size: 17px; }
.page-stats .cap { font-size: 11px; color: var(--muted); }
.page-actions { display: flex; gap: 8px; }
.page-actions .btn { flex: 1; }

.limit-note {
    display: flex; align-items: center; gap: 10px;
    padding: 12px 16px; border-radius: 10px; background: var(--primary-soft);
    margin-bottom: 18px; font-weight: 600; font-size: 13px;
}

/* ============ Chat ============ */
.chat-shell { display: flex; height: 100%; background: #fff; }

.chat-list {
    width: 330px; flex-shrink: 0;
    border-right: 1px solid var(--border);
    display: flex; flex-direction: column;
    min-height: 0;
}
.chat-list-head { padding: 14px 14px 10px; border-bottom: 1px solid var(--border); }
.chat-list-head h2 { font-size: 16px; margin-bottom: 10px; }
.chat-tools { display: flex; gap: 8px; margin-bottom: 10px; }
.chat-tools select { flex: 1; min-width: 0; }
#chat-search { width: 100%; }
.conversation-list { flex: 1; overflow-y: auto; min-height: 0; }
.conv-row {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 14px; cursor: pointer;
    border-bottom: 1px solid #f0f2f5;
    transition: background .12s;
}
.conv-row:hover { background: #f7f8fa; }
.conv-row.active { background: var(--primary-soft); }
.conv-row .info { min-width: 0; flex: 1; }
.conv-row .top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.conv-row .who { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-row .pg { color: var(--primary); font-size: 11px; font-weight: 600; }
.conv-row .preview { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-row .time { color: var(--muted); font-size: 11px; flex-shrink: 0; }
.unread-dot {
    background: var(--red); color: #fff; font-size: 11px; font-weight: 700;
    min-width: 18px; height: 18px; border-radius: 9px;
    display: inline-flex; align-items: center; justify-content: center; padding: 0 5px;
    margin-top: 3px;
}

.chat-thread { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }
.chat-thread-head {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 20px; border-bottom: 1px solid var(--border);
}
.chat-thread-head .info { min-width: 0; }
.chat-thread-head .name { font-weight: 700; font-size: 15px; }
.chat-thread-head .pg { color: var(--primary); font-size: 12px; font-weight: 600; }
.chat-thread-head .spacer { flex: 1; }

.messages { flex: 1; overflow-y: auto; padding: 20px; background: #fafbfc; }
.day-sep { text-align: center; margin: 14px 0; }
.day-sep span { background: #e4e6eb; color: var(--muted); font-size: 11px; padding: 4px 12px; border-radius: 999px; }

.msg { display: flex; margin-bottom: 10px; max-width: 78%; }
.msg .avatar { align-self: flex-end; margin: 0 8px 4px 0; }
.msg.mine { margin-left: auto; flex-direction: row-reverse; }
.msg.mine .avatar { display: none; }
.bubble {
    padding: 9px 13px; border-radius: 16px;
    font-size: 14px; white-space: pre-wrap; word-break: break-word;
    background: #fff; border: 1px solid var(--border);
    border-bottom-left-radius: 5px;
}
.msg.mine .bubble {
    background: var(--primary); color: #fff; border: none;
    border-bottom-right-radius: 5px; border-bottom-left-radius: 16px;
}
.msg .meta { font-size: 10px; color: var(--muted); margin: 3px 4px 0; }
.msg.mine .meta { text-align: right; }

.composer {
    border-top: 1px solid var(--border);
    padding: 12px 16px;
    display: flex; align-items: flex-end; gap: 10px;
}
.composer textarea {
    flex: 1; resize: none; border: 1px solid var(--border); border-radius: 10px;
    padding: 11px 13px; font-family: inherit; font-size: 14px;
    max-height: 120px; line-height: 1.5;
}
.composer textarea:focus { outline: none; border-color: var(--primary); }
.composer .btn { height: 42px; }

.chat-empty { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); flex-direction: column; gap: 8px; }
.chat-empty .ico { font-size: 46px; }

/* ============ Packages ============ */
.packages-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 18px; }
.pkg-card {
    background: #fff; border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); padding: 24px; display: flex; flex-direction: column;
    position: relative;
}
.pkg-card.highlight { border: 2px solid var(--primary); }
.pkg-card .tag-now { position: absolute; top: -11px; left: 22px; background: var(--primary); color: #fff; padding: 3px 12px; border-radius: 999px; font-size: 11px; font-weight: 700; }
.pkg-card .pname { font-size: 17px; font-weight: 800; }
.pkg-card .price { font-size: 30px; font-weight: 800; margin: 10px 0 2px; }
.pkg-card .price small { font-size: 14px; color: var(--muted); font-weight: 600; }
.pkg-card .desc { color: var(--muted); font-size: 13px; margin-bottom: 16px; }
.pkg-card ul { list-style: none; margin: 0 0 20px; padding: 0; flex: 1; }
.pkg-card ul li { padding: 6px 0; font-size: 13px; display: flex; gap: 8px; }
.pkg-card ul li::before { content: "✓"; color: var(--green); font-weight: 800; }

.qr-box {
    width: 150px; height: 150px; margin: 0 auto 16px;
    border: 1px dashed var(--border); border-radius: 12px;
    display: flex; align-items: center; justify-content: center;
    color: var(--muted); font-size: 12px; text-align: center;
    background: repeating-conic-gradient(#f7f8fa 0 25%, #fff 0 50%) 0 0/14px 14px;
}
.pay-ref {
    background: var(--bg); padding: 12px 16px; border-radius: 10px;
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    font-weight: 700; margin-bottom: 16px;
}
.pay-ref .ref { font-family: monospace; letter-spacing: 1px; }

/* ============ Auth ============ */
.auth-wrap {
    min-height: 100vh; display: flex; align-items: center; justify-content: center;
    background: linear-gradient(135deg, #1877f2, #42a5ff);
    padding: 24px;
}
.auth-card {
    width: 100%; max-width: 420px;
    background: #fff; border-radius: 16px; box-shadow: var(--shadow-lg);
    padding: 34px;
}
.auth-card .brand { border: none; padding: 0 0 20px; justify-content: center; font-size: 22px; }
.auth-card h2 { text-align: center; margin-bottom: 6px; font-size: 20px; }
.auth-card .sub { text-align: center; color: var(--muted); margin-bottom: 22px; font-size: 13px; }
.auth-foot { text-align: center; margin-top: 18px; font-size: 13px; color: var(--muted); }
.demo-box {
    margin-top: 18px; padding: 13px 15px; border-radius: 10px;
    background: var(--amber-soft); font-size: 12px; color: var(--amber);
    line-height: 1.6;
}

/* ============ Team ============ */
.invite-link-box {
    display: flex; align-items: center; gap: 8px;
    background: var(--bg); border-radius: 8px; padding: 6px 6px 6px 12px;
    font-size: 12px; font-family: monospace;
}
.invite-link-box code { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

@media (max-width: 860px) {
    .sidebar { display: none; }
    .content { padding: 16px; }
    .chat-list { width: 100%; }
    .chat-shell { flex-direction: column; }
}
