:root {
    --bg: #0f1419;
    --panel: #1a2332;
    --border: #2d3a4f;
    --text: #e7ecf3;
    --muted: #8b9cb3;
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --visitor: #1e3a5f;
    --agent: #14532d;
    --system: #374151;
    --danger: #ef4444;
    --radius: 12px;
    font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: linear-gradient(160deg, #0f1419 0%, #152238 100%);
    color: var(--text);
    min-height: 100vh;
}

.visitor-page {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 2rem 1rem;
}

.site-header {
    text-align: center;
    margin-bottom: 1.5rem;
}

.site-header h1 { margin: 0 0 0.5rem; font-size: 1.75rem; }
.site-header p { margin: 0; color: var(--muted); }

.chat-shell {
    width: 100%;
    max-width: 520px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    display: flex;
    flex-direction: column;
    height: min(70vh, 640px);
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
}

.admin-chat {
    max-width: none;
    height: calc(100vh - 2rem);
    margin: 1rem;
}

.chat-header {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 600;
}

.chat-header #chat-title {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.visitor-exit-btn {
    flex-shrink: 0;
    margin-left: auto;
    padding: 0.35rem 0.65rem;
    font-size: 0.8rem;
    font-weight: 500;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: transparent;
    color: var(--muted);
    cursor: pointer;
}

.visitor-exit-btn:hover {
    color: var(--text);
    border-color: var(--muted);
}

.visitor-exit-btn.hidden {
    display: none !important;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--muted);
}

.status-dot.online { background: #22c55e; }

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.msg {
    max-width: 85%;
    padding: 0.65rem 0.9rem;
    border-radius: 10px;
    font-size: 0.95rem;
    line-height: 1.45;
    word-break: break-word;
}

.msg .meta {
    font-size: 0.7rem;
    color: var(--muted);
    margin-bottom: 0.25rem;
}

.msg.visitor {
    align-self: flex-end;
    background: var(--accent);
    color: #fff;
}

.msg.agent {
    align-self: flex-start;
    background: var(--agent);
}

.msg.system {
    align-self: center;
    background: var(--system);
    color: var(--muted);
    font-size: 0.85rem;
    max-width: 95%;
    text-align: center;
}

.msg.msg-media.visitor {
    background: rgba(59, 130, 246, 0.25);
    color: var(--text);
}

.msg.msg-media.agent {
    background: rgba(20, 83, 45, 0.45);
}

.chat-image {
    max-width: min(240px, 100%);
    max-height: 280px;
    border-radius: 8px;
    display: block;
    object-fit: contain;
}

.chat-sticker {
    width: 80px;
    height: 80px;
    display: block;
}

.chat-sticker-inline {
    width: 48px;
    height: 48px;
    vertical-align: middle;
    display: inline-block;
    object-fit: contain;
}

.msg-emoji-content {
    font-size: 2.25rem;
    line-height: 1.2;
}

.composer-wrap {
    position: relative;
    border-top: 1px solid var(--border);
}

.composer-toolbar {
    display: flex;
    gap: 0.35rem;
    padding: 0.5rem 0.75rem 0;
}

.tool-btn {
    border: 1px solid var(--border);
    background: #0f1419;
    border-radius: 8px;
    padding: 0.35rem 0.55rem;
    cursor: pointer;
    font-size: 1.1rem;
    line-height: 1;
}

.tool-btn:hover {
    border-color: var(--accent);
}

.emoji-panel {
    position: absolute;
    left: 0.75rem;
    right: 0.75rem;
    bottom: 100%;
    margin-bottom: 0.35rem;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
    z-index: 20;
    max-height: 220px;
    display: flex;
    flex-direction: column;
}

.emoji-panel.hidden {
    display: none;
}

.emoji-panel-tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
}

.emoji-tab {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--muted);
    padding: 0.5rem;
    cursor: pointer;
    font-size: 0.85rem;
}

.emoji-tab.active {
    color: var(--accent);
    font-weight: 600;
}

.emoji-panel-page {
    overflow-y: auto;
    padding: 0.5rem;
    flex: 1;
}

.emoji-panel-page.hidden {
    display: none;
}

.emoji-grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 0.25rem;
}

.emoji-item {
    border: none;
    background: transparent;
    font-size: 1.35rem;
    cursor: pointer;
    padding: 0.25rem;
    border-radius: 6px;
}

.emoji-item:hover {
    background: rgba(255, 255, 255, 0.08);
}

.sticker-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.35rem;
}

.sticker-item {
    border: 1px solid transparent;
    background: transparent;
    padding: 0.25rem;
    border-radius: 8px;
    cursor: pointer;
}

.sticker-item:hover {
    border-color: var(--border);
    background: rgba(255, 255, 255, 0.04);
}

.sticker-item img {
    width: 100%;
    height: auto;
    display: block;
}

.composer-pending {
    padding: 0.5rem 0.75rem 0;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.composer-pending.hidden {
    display: none !important;
}

.pending-image-box {
    position: relative;
    display: inline-block;
    max-width: 120px;
}

.pending-image-preview {
    display: block;
    max-width: 120px;
    max-height: 88px;
    border-radius: 8px;
    border: 1px solid var(--border);
    object-fit: cover;
}

.pending-image-remove {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--panel);
    color: var(--text);
    cursor: pointer;
    font-size: 14px;
    line-height: 1;
    padding: 0;
}

.pending-image-hint {
    font-size: 0.75rem;
    color: var(--muted);
}

.composer {
    display: flex;
    gap: 0.5rem;
    padding: 0.75rem;
    align-items: flex-end;
}

.composer textarea {
    flex: 1;
    resize: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.65rem 0.75rem;
    background: #0f1419;
    color: var(--text);
    font: inherit;
}

.composer button {
    border: none;
    background: var(--accent);
    color: #fff;
    padding: 0.65rem 1.25rem;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
}

.composer button:hover { background: var(--accent-hover); }

.composer-actions {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.btn-secondary {
    background: transparent !important;
    border: 1px solid var(--border) !important;
    color: var(--muted) !important;
    font-weight: 500 !important;
}

.btn-secondary:hover { color: var(--danger) !important; border-color: var(--danger) !important; }

.btn-danger {
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 8px;
    background: var(--danger);
    color: #fff;
    font-weight: 600;
    cursor: pointer;
}

.btn-danger:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.admin-modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.admin-modal.hidden {
    display: none !important;
}

.admin-modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
}

.admin-modal-card {
    position: relative;
    width: 100%;
    max-width: 400px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem 1.5rem;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
}

.admin-modal-card h2 {
    margin: 0 0 0.75rem;
    font-size: 1.1rem;
}

.admin-modal-card .muted {
    margin: 0 0 1.25rem;
    line-height: 1.5;
}

.admin-modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.65rem;
}

.admin-modal-actions .btn-secondary {
    padding: 0.5rem 1rem;
    border-radius: 8px;
    cursor: pointer;
}

.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 1rem;
}

.login-card {
    width: 100%;
    max-width: 380px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.login-card h1 { margin: 0; text-align: center; }
.login-card label { display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.9rem; }
.login-card input {
    padding: 0.6rem 0.75rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: #0f1419;
    color: var(--text);
}

.login-card button {
    padding: 0.75rem;
    border: none;
    border-radius: 8px;
    background: var(--accent);
    color: #fff;
    font-weight: 600;
    cursor: pointer;
}

.invite-gate {
    display: none;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: 1rem;
    padding-top: max(1rem, env(safe-area-inset-top));
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
}

.visitor-page.visitor-locked .invite-gate {
    display: flex;
}

.visitor-page.visitor-locked .site-header,
.visitor-page.visitor-locked .chat-shell {
    display: none !important;
}

.invite-card {
    margin: 0 auto;
}

.hidden {
    display: none !important;
}

.muted { color: var(--muted); font-size: 0.85rem; margin: 0; }
.hint { font-size: 0.75rem; color: var(--muted); margin: 0; }
.error { color: var(--danger); margin: 0; font-size: 0.9rem; }

.admin-page {
    display: grid;
    grid-template-columns: 280px 1fr;
    min-height: 100vh;
}

.sidebar {
    background: var(--panel);
    border-right: 1px solid var(--border);
    padding: 1rem;
    overflow-y: auto;
}

.sidebar-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}

.logout { color: var(--muted); font-size: 0.85rem; text-decoration: none; }
.logout:hover { color: var(--text); }

.sidebar h2 { font-size: 0.85rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }

.conv-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.conv-list li {
    padding: 0.75rem;
    border-radius: 8px;
    cursor: pointer;
    border: 1px solid transparent;
    margin-bottom: 0.35rem;
}

.conv-list li:hover { background: rgba(255, 255, 255, 0.04); }
.conv-list li.active { border-color: var(--accent); background: rgba(59, 130, 246, 0.12); }

.conv-list .name { font-weight: 600; font-size: 0.95rem; }
.conv-list .preview {
    font-size: 0.8rem;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 0.25rem;
}

.conv-list .conv-note {
    font-size: 0.75rem;
    color: var(--accent);
    margin-top: 0.35rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    opacity: 0.9;
}

.visitor-note-panel {
    padding: 0.65rem 1rem;
    border-bottom: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.03);
    flex-shrink: 0;
}

.visitor-note-head {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 0.5rem;
    margin-bottom: 0.4rem;
    font-size: 0.85rem;
}

.visitor-note-panel textarea {
    width: 100%;
    box-sizing: border-box;
    padding: 0.5rem 0.65rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: #0f1419;
    color: var(--text);
    font: inherit;
    resize: vertical;
    min-height: 2.5rem;
}

.visitor-note-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-top: 0.45rem;
}

.visitor-note-actions .btn-secondary {
    padding: 0.35rem 0.75rem;
    font-size: 0.85rem;
}

.admin-msg-toolbar {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.45rem 1rem;
    border-bottom: 1px solid var(--border);
    background: rgba(255, 255, 255, 0.02);
    flex-shrink: 0;
    font-size: 0.85rem;
}

.admin-msg-toolbar.hidden {
    display: none !important;
}

.admin-msg-select-all-label {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    cursor: pointer;
    user-select: none;
}

.msg-admin {
    position: relative;
}

.msg-admin-bar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.25rem;
    opacity: 0.55;
    transition: opacity 0.15s;
}

.msg-admin:hover .msg-admin-bar,
.msg-admin:focus-within .msg-admin-bar {
    opacity: 1;
}

.msg-pick {
    display: inline-flex;
    align-items: center;
    cursor: pointer;
}

.msg-del-one {
    padding: 0.15rem 0.45rem;
    font-size: 0.72rem;
    border-radius: 4px;
    border: 1px solid var(--border);
    background: transparent;
    color: var(--muted);
    cursor: pointer;
}

.msg-del-one:hover {
    color: var(--danger);
    border-color: var(--danger);
}

.badge {
    display: inline-block;
    background: var(--accent);
    color: #fff;
    font-size: 0.65rem;
    padding: 0.1rem 0.35rem;
    border-radius: 999px;
    margin-left: 0.35rem;
}

/* ---------- Mobile / touch ---------- */
html {
    -webkit-text-size-adjust: 100%;
}

button,
.tool-btn,
.emoji-item,
.composer button,
.login-card button {
    touch-action: manipulation;
}

.admin-back-btn {
    display: none;
}

.admin-chat-header {
    gap: 0.65rem;
}

.admin-chat-header #admin-chat-title {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

@media (max-width: 768px) {
    .admin-page {
        display: block;
        height: 100vh;
        height: 100dvh;
        overflow: hidden;
    }

    .sidebar {
        height: 100%;
        max-height: none;
        padding-bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
    }

    .workspace {
        display: none;
        height: 100%;
    }

    body.admin-chat-open .sidebar {
        display: none;
    }

    body.admin-chat-open .workspace {
        display: flex;
        flex-direction: column;
        min-height: 0;
    }

    .admin-back-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
        width: 44px;
        height: 44px;
        margin: -0.35rem 0 -0.35rem -0.5rem;
        border: 1px solid var(--border);
        border-radius: 10px;
        background: #0f1419;
        color: var(--text);
        font-size: 1.25rem;
        cursor: pointer;
        padding: 0;
    }

    .admin-chat {
        flex: 1;
        min-height: 0;
        height: auto;
        margin: 0;
        border-radius: 0;
        border: none;
    }

    .admin-page .composer {
        flex-wrap: wrap;
    }

    .admin-page .composer textarea {
        width: 100%;
        min-height: 2.75rem;
        font-size: 16px;
    }

    .admin-page .composer-actions {
        flex-direction: row;
        width: 100%;
        justify-content: space-between;
    }

    .admin-page .composer-actions button {
        min-height: 44px;
        flex: 1;
    }

    .admin-page .composer-actions .btn-secondary {
        flex: 0 1 auto;
        min-width: 7rem;
    }

    .login-page {
        padding: max(1rem, env(safe-area-inset-top)) 1rem
            calc(1rem + env(safe-area-inset-bottom, 0px));
    }

    .login-card {
        padding: 1.5rem;
    }

    .login-card input {
        font-size: 16px;
        min-height: 44px;
    }

    .login-card button {
        min-height: 48px;
    }
}

@media (max-width: 640px) {
    .visitor-page {
        align-items: stretch;
        padding: 0;
        padding-top: env(safe-area-inset-top, 0px);
        min-height: 100vh;
        min-height: 100dvh;
        height: 100vh;
        height: 100dvh;
    }

    .site-header {
        flex-shrink: 0;
        margin: 0;
        padding: 0.65rem 1rem 0.5rem;
        text-align: left;
    }

    .site-header h1 {
        font-size: 1.125rem;
        line-height: 1.3;
    }

    .site-header p {
        font-size: 0.8rem;
        line-height: 1.35;
    }

    .visitor-page .chat-shell {
        flex: 1;
        min-height: 0;
        max-width: none;
        height: auto;
        border-radius: 0;
        border-left: none;
        border-right: none;
        border-bottom: none;
        box-shadow: none;
    }

    .visitor-page .chat-header {
        padding: 0.75rem 1rem;
        font-size: 0.95rem;
    }

    .visitor-page .messages {
        padding: 0.75rem;
        -webkit-overflow-scrolling: touch;
    }

    .visitor-page .msg {
        max-width: 88%;
        font-size: 0.9375rem;
    }

    .visitor-page .composer-wrap {
        padding-bottom: env(safe-area-inset-bottom, 0px);
        background: var(--panel);
    }

    .visitor-page .composer {
        padding: 0.5rem 0.65rem 0.65rem;
        align-items: stretch;
    }

    .visitor-page .composer textarea {
        font-size: 16px;
        min-height: 44px;
        max-height: 120px;
    }

    .visitor-page .composer button[type="submit"] {
        min-width: 4.25rem;
        min-height: 44px;
        padding: 0.65rem 1rem;
        align-self: flex-end;
    }

    .visitor-page .tool-btn {
        min-width: 44px;
        min-height: 44px;
        padding: 0.5rem;
    }

    .visitor-page .emoji-panel {
        left: 0.5rem;
        right: 0.5rem;
        max-height: min(42vh, 280px);
    }

    .visitor-page .emoji-grid {
        grid-template-columns: repeat(7, 1fr);
    }

    .visitor-page .sticker-grid {
        grid-template-columns: repeat(4, 1fr);
    }

    .chat-image {
        max-width: min(200px, 72vw);
        max-height: 220px;
    }
}
