:root {
    --primary: #f56a00;
    --primary-dark: #d95800;
    --primary-soft: #fff1e6;
    --bg: #f5f7fb;
    --panel: #ffffff;
    --text: #1f2937;
    --muted: #6b7280;
    --line: #e5e7eb;
    --green: #16a34a;
    --red: #dc2626;
    --shadow: 0 18px 50px rgba(31, 41, 55, .12);
    --radius: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
    font-family: Inter, "PingFang SC", "Microsoft YaHei", Arial, sans-serif;
    color: var(--text);
    background: var(--bg);
}
button, input, textarea { font: inherit; }
button { cursor: pointer; }
a { color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

.btn {
    border: 1px solid var(--line);
    background: #fff;
    color: var(--text);
    border-radius: 8px;
    padding: 10px 16px;
    font-weight: 600;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: .18s ease;
}
.btn:hover { transform: translateY(-1px); border-color: #cbd5e1; }
.btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn.primary:hover { background: var(--primary-dark); }
.btn.small { padding: 7px 11px; font-size: 13px; }

.alert, .toast {
    border-radius: 9px;
    padding: 12px 14px;
    font-size: 14px;
}
.alert.error, .toast.error { background: #fef2f2; border: 1px solid #fecaca; color: #b91c1c; }
.toast.ok { background: #f0fdf4; border: 1px solid #bbf7d0; color: #15803d; }
.toast {
    position: fixed;
    top: 18px;
    right: 22px;
    z-index: 50;
    box-shadow: 0 8px 30px rgba(0, 0, 0, .12);
    animation: toast-in .25s ease;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } }

.stack-form { display: grid; gap: 15px; }
.stack-form label { display: grid; gap: 7px; font-size: 14px; font-weight: 600; }
.stack-form input, .name-card input {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 11px 12px;
    outline: none;
    background: #fff;
}
.stack-form input:focus, .name-card input:focus, textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(245, 106, 0, .12);
}

/* Login */
.login-body {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
    background:
        radial-gradient(circle at 15% 20%, rgba(245, 106, 0, .12), transparent 28%),
        radial-gradient(circle at 85% 80%, rgba(59, 130, 246, .12), transparent 30%),
        var(--bg);
}
.login-card {
    width: min(420px, 100%);
    background: #fff;
    border-radius: 20px;
    padding: 34px;
    box-shadow: var(--shadow);
    text-align: center;
}
.brand-mark {
    width: 58px;
    height: 58px;
    margin: 0 auto 15px;
    border-radius: 16px;
    display: grid;
    place-items: center;
    background: linear-gradient(135deg, var(--primary), #ff9b45);
    color: #fff;
    font-size: 28px;
    font-weight: 800;
}
.login-card h1 { margin: 0 0 6px; font-size: 25px; }
.login-card .muted { margin: 0 0 22px; }
.login-card .alert { margin-bottom: 15px; text-align: left; }
.login-card .stack-form { text-align: left; }
.login-tip { margin: 20px 0 0; color: var(--muted); font-size: 13px; }
.login-tip code { background: #f3f4f6; border-radius: 5px; padding: 2px 5px; }

/* Dashboard */
.dashboard-body { height: 100vh; overflow: hidden; }
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
    width: 230px;
    background: #161b26;
    color: #fff;
    display: flex;
    flex-direction: column;
    flex: 0 0 auto;
}
.sidebar-brand {
    height: 70px;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 0 20px;
    border-bottom: 1px solid rgba(255,255,255,.08);
}
.sidebar-brand span {
    width: 34px;
    height: 34px;
    border-radius: 9px;
    display: grid;
    place-items: center;
    background: var(--primary);
    font-weight: 800;
}
.sidebar-brand strong { font-size: 16px; }
.agent-profile {
    padding: 20px;
    display: flex;
    gap: 11px;
    align-items: center;
    border-bottom: 1px solid rgba(255,255,255,.08);
}
.avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: linear-gradient(135deg, #ffd8b8, var(--primary));
    display: grid;
    place-items: center;
    font-weight: 800;
    color: #5f2a00;
}
.agent-profile div:last-child { display: grid; gap: 3px; }
.agent-profile small { color: #9ca3af; }
.sidebar nav { padding: 15px 10px; display: grid; gap: 6px; }
.nav-item {
    border: 0;
    color: #cbd5e1;
    background: transparent;
    text-align: left;
    border-radius: 9px;
    padding: 11px 12px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.nav-item:hover, .nav-item.active { background: rgba(245,106,0,.17); color: #fff; }
.nav-item .badge {
    margin-left: auto;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    border-radius: 999px;
    display: grid;
    place-items: center;
    background: var(--primary);
    color: #fff;
    font-size: 11px;
}
.sidebar-footer { margin-top: auto; padding: 18px 20px; border-top: 1px solid rgba(255,255,255,.08); }
.sidebar-footer a { color: #cbd5e1; text-decoration: none; font-size: 14px; }
.main-area { min-width: 0; flex: 1; display: flex; flex-direction: column; }
.content-pane { display: none; min-height: 0; height: 100vh; }
.content-pane.active { display: flex; flex-direction: column; }
.topbar {
    min-height: 86px;
    background: #fff;
    border-bottom: 1px solid var(--line);
    padding: 15px 22px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
}
.topbar h1 { margin: 0; font-size: 21px; }
.topbar p { margin: 5px 0 0; color: var(--muted); font-size: 13px; }
.link-box {
    display: flex;
    align-items: center;
    gap: 8px;
    background: #f8fafc;
    padding: 8px;
    border-radius: 10px;
    border: 1px solid var(--line);
}
.link-box > span { color: var(--muted); font-size: 12px; white-space: nowrap; }
.link-box input { width: 280px; border: 0; background: transparent; color: #334155; outline: none; font-size: 12px; }
.workspace { min-height: 0; flex: 1; display: grid; grid-template-columns: 290px minmax(420px, 1fr) 260px; }
.conversation-list-panel, .chat-panel, .info-panel { min-height: 0; background: #fff; }
.conversation-list-panel { border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.panel-title { height: 54px; display: flex; align-items: center; justify-content: space-between; padding: 0 14px; border-bottom: 1px solid var(--line); }
.icon-btn { width: 30px; height: 30px; border: 1px solid var(--line); border-radius: 8px; background: #fff; font-size: 18px; color: var(--muted); }
.search-wrap { padding: 10px 12px; border-bottom: 1px solid var(--line); }
.search-wrap input { width: 100%; border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; outline: none; }
.conversation-list { overflow: auto; flex: 1; }
.empty-list { padding: 30px 18px; text-align: center; color: var(--muted); font-size: 13px; }
.error-text { color: var(--red); }
.conversation-row {
    width: 100%;
    border: 0;
    background: #fff;
    border-bottom: 1px solid #f0f2f5;
    padding: 12px;
    display: grid;
    grid-template-columns: 40px minmax(0,1fr) auto;
    align-items: center;
    gap: 10px;
    text-align: left;
}
.conversation-row:hover, .conversation-row.active { background: var(--primary-soft); }
.mini-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: grid;
    place-items: center;
    background: #e8eef9;
    color: #3b4b65;
    font-weight: 700;
}
.conversation-body { min-width: 0; display: grid; gap: 5px; }
.conversation-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.conversation-top strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14px; }
.conversation-top time { color: #9ca3af; font-size: 11px; white-space: nowrap; }
.conversation-preview { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.unread-dot { min-width: 20px; height: 20px; padding: 0 5px; border-radius: 999px; display: grid; place-items: center; background: var(--primary); color: #fff; font-size: 11px; }
.chat-panel { display: flex; flex-direction: column; min-width: 0; }
.chat-empty { flex: 1; display: grid; place-content: center; text-align: center; color: var(--muted); }
.empty-icon { font-size: 48px; opacity: .55; }
.chat-empty h2 { color: #374151; margin: 12px 0 5px; font-size: 19px; }
.chat-empty p { margin: 0; }
.chat-active { min-height: 0; flex: 1; display: flex; flex-direction: column; }
.chat-header { height: 54px; padding: 0 16px; border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; }
.chat-header > div { display: grid; gap: 2px; }
.chat-header small { color: var(--muted); font-size: 11px; }
.message-list { flex: 1; min-height: 0; overflow: auto; padding: 20px; background: #f7f8fb; }
.message { display: flex; flex-direction: column; margin-bottom: 16px; max-width: 76%; }
.message.incoming { align-items: flex-start; margin-right: auto; }
.message.outgoing { align-items: flex-end; margin-left: auto; }
.message-bubble { white-space: pre-wrap; word-break: break-word; border-radius: 12px; padding: 10px 13px; line-height: 1.55; font-size: 14px; box-shadow: 0 1px 2px rgba(0,0,0,.04); }
.message.incoming .message-bubble { background: #fff; border: 1px solid var(--line); border-top-left-radius: 3px; }
.message.outgoing .message-bubble { background: var(--primary); color: #fff; border-top-right-radius: 3px; }
.message-meta { margin-top: 5px; font-size: 10px; color: #9ca3af; }
.composer { border-top: 1px solid var(--line); padding: 11px 14px; background: #fff; }
.composer textarea, .visitor-composer textarea { border: 0; width: 100%; min-height: 76px; resize: none; outline: none; line-height: 1.5; }
.composer-footer { display: flex; justify-content: space-between; align-items: center; }
.composer-footer span { color: var(--muted); font-size: 12px; }
.info-panel { border-left: 1px solid var(--line); padding: 18px; overflow: auto; }
.info-panel h3 { margin: 3px 0 11px; font-size: 14px; }
.info-card, .tips { border: 1px solid var(--line); border-radius: 10px; background: #fafafa; padding: 12px; margin-bottom: 22px; font-size: 12px; }
.info-row { display: flex; justify-content: space-between; gap: 10px; padding: 7px 0; border-bottom: 1px dashed #e5e7eb; }
.info-row:last-child { border-bottom: 0; }
.info-row span { color: var(--muted); }
.info-row strong { text-align: right; font-weight: 600; }
.tips p { color: var(--muted); line-height: 1.7; margin: 0 0 8px; }
.tips p:last-child { margin-bottom: 0; }

.admin-grid { padding: 22px; display: grid; grid-template-columns: 320px minmax(0,1fr); gap: 20px; overflow: auto; }
.card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; box-shadow: 0 8px 24px rgba(31,41,55,.05); align-self: start; }
.card h2 { margin: 0 0 18px; font-size: 17px; }
.table-wrap { overflow: auto; }
table { width: 100%; border-collapse: collapse; min-width: 760px; }
th, td { padding: 12px 10px; border-bottom: 1px solid var(--line); text-align: left; font-size: 13px; vertical-align: middle; }
th { color: var(--muted); font-weight: 600; background: #fafafa; }
td > strong, td > small { display: block; }
td > small { color: var(--muted); margin-top: 3px; }
.table-link { display: flex; gap: 6px; }
.table-link input { width: 230px; border: 1px solid var(--line); border-radius: 6px; padding: 7px; font-size: 11px; }
.table-link button, .link-button { border: 0; background: transparent; color: var(--primary-dark); padding: 4px; font-size: 12px; }
.inline-form { display: inline; }
.status { display: inline-flex; align-items: center; gap: 5px; }
.status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; }
.status.online::before { background: var(--green); }
.status.offline::before { background: #9ca3af; }
.settings-card { width: min(480px, calc(100% - 44px)); margin: 22px; }
.modal { position: fixed; inset: 0; z-index: 100; background: rgba(15,23,42,.48); display: grid; place-items: center; padding: 20px; }
.modal-card { width: min(420px,100%); background: #fff; border-radius: 14px; padding: 24px; position: relative; box-shadow: var(--shadow); }
.modal-card h2 { margin-top: 0; }
.modal-close { position: absolute; top: 10px; right: 12px; border: 0; background: transparent; font-size: 26px; color: #9ca3af; }

/* Visitor chat */
.visitor-body {
    min-height: 100vh;
    padding: 34px 20px;
    background:
        radial-gradient(circle at 88% 8%, rgba(59,130,246,.09) 0 5px, transparent 6px) 0 0/48px 48px,
        linear-gradient(145deg, #ffffff 0%, #f3f7ff 100%);
}
.visitor-shell { width: min(1180px, 100%); min-height: 760px; margin: 0 auto; border-radius: 14px; overflow: hidden; background: #fff; box-shadow: var(--shadow); display: flex; flex-direction: column; }
.visitor-header { min-height: 82px; background: linear-gradient(90deg, var(--primary), #ff7d14); color: #fff; display: flex; align-items: center; gap: 14px; padding: 0 26px; }
.visitor-avatar { width: 58px; height: 58px; border-radius: 50%; background: #ffe2cb; color: #8c3d00; border: 3px solid rgba(255,255,255,.7); display: grid; place-items: center; font-size: 24px; font-weight: 800; }
.visitor-header > div:last-child { display: grid; gap: 6px; }
.visitor-header strong { font-size: 18px; }
.visitor-header span { font-size: 13px; opacity: .9; display: flex; align-items: center; gap: 6px; }
.visitor-header i { width: 8px; height: 8px; border-radius: 50%; background: #79e39c; box-shadow: 0 0 0 3px rgba(121,227,156,.22); }
.visitor-main { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0,1fr) 315px; }
.visitor-chat { min-height: 0; border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.welcome-banner { margin: 24px 30px 10px; padding: 14px 17px; border: 1px solid #e4eaf4; border-radius: 11px; color: #374151; background: #fff; font-size: 14px; }
.visitor-message-list { background: #fff; padding: 20px 30px; }
.visitor-composer { border-top: 1px solid var(--line); padding: 13px 18px 16px; }
.visitor-composer textarea { min-height: 100px; }
.visitor-tools { display: flex; justify-content: space-between; align-items: center; }
.visitor-tools span { color: #9ca3af; font-size: 12px; }
.visitor-side { padding: 26px; background: #fff; overflow: auto; }
.promo-card { border-radius: 12px; overflow: hidden; border: 1px solid var(--line); }
.promo-visual { height: 245px; padding: 28px; display: flex; flex-direction: column; justify-content: flex-end; color: #fff; background:
    radial-gradient(circle at 70% 30%, rgba(34,211,238,.7), transparent 25%),
    linear-gradient(155deg, #081426, #123c75 60%, #1476a8); }
.promo-visual span { align-self: flex-start; border: 1px solid rgba(255,255,255,.35); border-radius: 999px; padding: 4px 9px; font-size: 12px; margin-bottom: auto; }
.promo-visual strong { font-size: 26px; }
.promo-visual small { margin-top: 7px; opacity: .75; }
.promo-card h2 { margin: 18px 18px 8px; font-size: 18px; }
.promo-card p { margin: 0 18px 20px; color: var(--muted); line-height: 1.6; font-size: 13px; }
.name-card { margin-top: 18px; border: 1px solid var(--line); border-radius: 10px; padding: 15px; }
.name-card label { display: grid; gap: 7px; font-size: 13px; font-weight: 600; }
.name-card .btn { width: 100%; margin-top: 10px; }
.visitor-footer { text-align: center; color: #9ca3af; font-size: 12px; padding: 12px; border-top: 1px solid #f0f0f0; }
.not-found-card { width: min(520px, calc(100% - 40px)); margin: 120px auto; background: #fff; border-radius: 16px; padding: 35px; text-align: center; box-shadow: var(--shadow); }

@media (max-width: 1100px) {
    .workspace { grid-template-columns: 260px minmax(400px,1fr); }
    .info-panel { display: none; }
    .link-box > span { display: none; }
    .link-box input { width: 220px; }
    .admin-grid { grid-template-columns: 1fr; }
}

@media (max-width: 760px) {
    .dashboard-body { overflow: auto; }
    .app-shell { display: block; }
    .sidebar { width: 100%; min-height: auto; }
    .sidebar-brand, .agent-profile, .sidebar-footer { display: none; }
    .sidebar nav { display: flex; overflow: auto; padding: 8px; }
    .nav-item { white-space: nowrap; }
    .content-pane { height: calc(100vh - 58px); }
    .topbar { min-height: auto; align-items: flex-start; flex-direction: column; }
    .link-box { width: 100%; }
    .link-box input { width: 100%; min-width: 0; }
    .workspace { grid-template-columns: 120px minmax(0,1fr); }
    .conversation-row { grid-template-columns: 36px minmax(0,1fr); padding: 9px 7px; }
    .mini-avatar { width: 36px; height: 36px; }
    .conversation-preview, .conversation-top time, .unread-dot { display: none; }
    .conversation-top strong { font-size: 12px; }
    .search-wrap { display: none; }
    .message { max-width: 90%; }
    .admin-grid, .settings-card { margin: 0; padding: 14px; width: 100%; }

    .visitor-body { padding: 0; }
    .visitor-shell { min-height: 100vh; border-radius: 0; }
    .visitor-main { grid-template-columns: 1fr; }
    .visitor-chat { border-right: 0; min-height: 75vh; }
    .visitor-side { display: none; }
    .visitor-header { padding: 0 16px; }
    .visitor-avatar { width: 50px; height: 50px; }
    .welcome-banner { margin: 14px 14px 8px; }
    .visitor-message-list { padding: 14px; }
}
