:root {
    --xchat-primary: #6a5cf5;
    --xchat-primary-dark: #5644e6;
    --xchat-primary-light: #eeecff;
    --xchat-bg: #f5f5fb;
    --xchat-surface: #ffffff;
    --xchat-border: #e7e7f2;
    --xchat-text: #1e2030;
    --xchat-text-muted: #80839a;
    --xchat-success: #22c55e;
    --xchat-danger: #ef4444;
    --xchat-radius-lg: 20px;
    --xchat-radius-md: 14px;
    --xchat-shadow: 0 10px 30px rgba(30, 24, 90, 0.08);
    --xchat-shadow-sm: 0 2px 10px rgba(30, 24, 90, 0.06);
}

* {
    box-sizing: border-box;
}

html, body {
    height: 100%;
}

.xchat-body {
    margin: 0;
    height: 100vh;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: var(--xchat-text);
    background: radial-gradient(circle at top left, #efedff 0%, var(--xchat-bg) 45%);
}

button {
    font-family: inherit;
}

/* ---------- app shell ---------- */

.xchat-app {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

.xchat-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.7rem 1.5rem;
    background: var(--xchat-surface);
    border-bottom: 1px solid var(--xchat-border);
    flex-shrink: 0;
}

.xchat-self-chip {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.xchat-self-chip .xchat-avatar {
    width: 30px;
    height: 30px;
    min-width: 30px;
    font-size: 0.7rem;
}

.xchat-self-chip .xchat-header-name {
    font-size: 0.85rem;
}

.xchat-brand {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 700;
    font-size: 1.05rem;
}

.xchat-brand-center {
    justify-content: center;
    margin-bottom: 0.75rem;
    font-size: 1.15rem;
}

.xchat-brand-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border-radius: 9px;
    background: linear-gradient(135deg, var(--xchat-primary), #9b8cf5);
    color: #fff;
    font-size: 0.9rem;
}

.xchat-main {
    flex: 1 1 auto;
    display: flex;
    min-height: 0;
}

/* ---------- fade-in ---------- */

.xchat-fade-in {
    animation: xchatFadeIn .25s ease both;
}

@keyframes xchatFadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes xchatPop {
    from { opacity: 0; transform: translateY(8px) scale(.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

/* ---------- identify / picker panels ---------- */

.xchat-panel {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem 1rem;
    width: 100%;
}

.xchat-panel-card {
    width: 100%;
    max-width: 400px;
    background: var(--xchat-surface);
    border-radius: var(--xchat-radius-lg);
    box-shadow: var(--xchat-shadow);
    padding: 2rem;
}

.xchat-panel-card-wide {
    max-width: 460px;
}

.xchat-panel h1 {
    font-size: 1.5rem;
    font-weight: 700;
    margin: 0 0 0.35rem;
}

.xchat-panel-sub {
    color: var(--xchat-text-muted);
    font-size: 0.92rem;
    margin: 0 0 1.5rem;
}

.xchat-field {
    margin-bottom: 1rem;
}

.xchat-field label {
    display: block;
    font-size: 0.82rem;
    font-weight: 600;
    margin-bottom: 0.35rem;
    color: var(--xchat-text);
}

.xchat-field input {
    width: 100%;
    padding: 0.65rem 0.85rem;
    border: 1.5px solid var(--xchat-border);
    border-radius: var(--xchat-radius-md);
    font-size: 0.95rem;
    background: var(--xchat-bg);
    transition: border-color .15s ease, background .15s ease;
}

.xchat-field input:focus {
    outline: none;
    border-color: var(--xchat-primary);
    background: #fff;
}

/* ---------- buttons ---------- */

.xchat-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    border: none;
    border-radius: var(--xchat-radius-md);
    padding: 0.7rem 1.2rem;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform .08s ease, box-shadow .15s ease, background .15s ease;
}

.xchat-btn:active {
    transform: scale(.98);
}

.xchat-btn-primary {
    background: linear-gradient(135deg, var(--xchat-primary), var(--xchat-primary-dark));
    color: #fff;
    box-shadow: 0 6px 16px rgba(106, 92, 245, .35);
}

.xchat-btn-primary:hover {
    box-shadow: 0 8px 20px rgba(106, 92, 245, .45);
}

.xchat-btn-block {
    width: 100%;
}

.xchat-btn-ghost {
    background: var(--xchat-bg);
    color: var(--xchat-text);
    border: 1px solid var(--xchat-border);
}

.xchat-btn-ghost:hover {
    background: var(--xchat-primary-light);
    border-color: var(--xchat-primary);
}

.xchat-btn-sm {
    padding: 0.4rem 0.8rem;
    font-size: 0.8rem;
}

/* ---------- avatars / presence ---------- */

.xchat-agent-list {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.xchat-agent-card {
    display: flex;
    align-items: center;
    gap: 0.85rem;
    padding: 0.75rem 0.95rem;
    border: 1.5px solid var(--xchat-border);
    border-radius: var(--xchat-radius-md);
    background: var(--xchat-surface);
    text-align: left;
    cursor: pointer;
    transition: border-color .15s ease, transform .1s ease, box-shadow .15s ease;
    animation: xchatPop .25s ease both;
}

.xchat-agent-card:hover {
    border-color: var(--xchat-primary);
    box-shadow: var(--xchat-shadow-sm);
    transform: translateY(-1px);
}

.xchat-agent-card:active {
    transform: translateY(0) scale(.99);
}

.xchat-agent-card-name {
    font-weight: 600;
    font-size: 0.95rem;
}

.xchat-avatar {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    min-width: 42px;
    border-radius: 50%;
    background: var(--xchat-primary);
    color: #fff;
    font-weight: 700;
    font-size: 0.85rem;
    flex-shrink: 0;
}

.xchat-presence-dot {
    position: absolute;
    right: -1px;
    bottom: -1px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #c6c8d6;
    border: 2.5px solid #fff;
    transition: background .15s ease;
}

.xchat-presence-dot.online {
    background: var(--xchat-success);
    box-shadow: 0 0 0 3px rgba(34, 197, 94, .18);
}

/* ---------- guest chat window ---------- */

.xchat-window {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    background: var(--xchat-surface);
}

.xchat-window-header {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    padding: 0.85rem 1.25rem;
    border-bottom: 1px solid var(--xchat-border);
    background: var(--xchat-surface);
    flex-shrink: 0;
}

.xchat-back-btn {
    font-size: 1.4rem;
    margin-right: -0.2rem;
}

.xchat-header-info {
    display: flex;
    flex-direction: column;
    line-height: 1.25;
    min-width: 0;
}

.xchat-header-name {
    font-weight: 700;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.xchat-header-status {
    font-size: 0.78rem;
    color: var(--xchat-text-muted);
}

.xchat-header-status.online {
    color: var(--xchat-success);
    font-weight: 600;
}

.xchat-messages {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 1.25rem;
    background: var(--xchat-bg);
    display: flex;
    flex-direction: column;
    scrollbar-width: thin;
    scrollbar-color: #d3d5e6 transparent;
}

.xchat-messages::-webkit-scrollbar {
    width: 8px;
}

.xchat-messages::-webkit-scrollbar-thumb {
    background: #d3d5e6;
    border-radius: 8px;
}

.xchat-messages.xchat-dragover {
    outline: 2px dashed var(--xchat-primary);
    outline-offset: -8px;
    background: var(--xchat-primary-light);
}

.xchat-load-more {
    text-align: center;
    margin-bottom: 0.75rem;
}

.xchat-date-sep {
    text-align: center;
    margin: 1rem 0;
}

.xchat-date-sep span {
    display: inline-block;
    padding: 0.2rem 0.85rem;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--xchat-text-muted);
    background: var(--xchat-surface);
    border: 1px solid var(--xchat-border);
    border-radius: 999px;
}

.xchat-msg {
    display: flex;
    flex-direction: column;
    max-width: 68%;
    margin-bottom: 0.65rem;
    animation: xchatPop .18s ease both;
}

.xchat-msg-mine {
    align-self: flex-end;
    align-items: flex-end;
}

.xchat-msg-theirs {
    align-self: flex-start;
    align-items: flex-start;
}

.xchat-msg-bubble {
    padding: 0.6rem 0.9rem;
    border-radius: 18px;
    word-wrap: break-word;
    white-space: pre-wrap;
    font-size: 0.92rem;
    line-height: 1.4;
}

.xchat-msg-mine .xchat-msg-bubble {
    background: linear-gradient(135deg, var(--xchat-primary), var(--xchat-primary-dark));
    color: #fff;
    border-bottom-right-radius: 5px;
}

.xchat-msg-theirs .xchat-msg-bubble {
    background: var(--xchat-surface);
    border: 1px solid var(--xchat-border);
    border-bottom-left-radius: 5px;
}

.xchat-msg-meta {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.68rem;
    color: var(--xchat-text-muted);
    margin-top: 0.2rem;
    opacity: 0;
    transition: opacity .1s ease;
}

.xchat-msg:hover .xchat-msg-meta {
    opacity: 1;
}

.xchat-msg-delete {
    border: none;
    background: none;
    padding: 0;
    font-size: 0.78rem;
    cursor: pointer;
    color: var(--xchat-text-muted);
}

.xchat-msg-delete:hover {
    color: var(--xchat-danger);
}

.xchat-msg-image, .xchat-msg-video {
    max-width: 260px;
    max-height: 260px;
    border-radius: 14px;
    display: block;
}

.xchat-msg-audio {
    width: 250px;
}

.xchat-msg-caption {
    margin-top: 0.4rem;
}

.xchat-file-card {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.55rem 0.75rem;
    background: var(--xchat-surface);
    border: 1px solid var(--xchat-border);
    border-radius: 12px;
    text-decoration: none;
    color: inherit;
    min-width: 200px;
}

.xchat-msg-mine .xchat-file-card {
    background: rgba(255,255,255,.16);
    border-color: rgba(255,255,255,.3);
    color: #fff;
}

.xchat-file-icon {
    font-size: 1.4rem;
}

.xchat-file-info {
    display: flex;
    flex-direction: column;
    line-height: 1.25;
    overflow: hidden;
}

.xchat-file-name {
    font-weight: 600;
    font-size: 0.85rem;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
}

.xchat-file-size {
    font-size: 0.72rem;
    opacity: .75;
}

/* ---------- typing indicator ---------- */

.xchat-typing {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0 1.25rem;
    font-size: 0.75rem;
    color: var(--xchat-text-muted);
    height: 1.5rem;
}

.xchat-typing-dots {
    display: inline-flex;
    gap: 3px;
}

.xchat-typing-dots i {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: var(--xchat-text-muted);
    display: inline-block;
    animation: xchatTypingBounce 1s infinite ease-in-out;
}

.xchat-typing-dots i:nth-child(2) { animation-delay: .15s; }
.xchat-typing-dots i:nth-child(3) { animation-delay: .3s; }

@keyframes xchatTypingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: .5; }
    30% { transform: translateY(-3px); opacity: 1; }
}

/* ---------- composer ---------- */

.xchat-composer {
    border-top: 1px solid var(--xchat-border);
    padding: 0.75rem 1rem;
    background: var(--xchat-surface);
    flex-shrink: 0;
}

.xchat-composer-row {
    display: flex;
    align-items: flex-end;
    gap: 0.3rem;
}

.xchat-icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: none;
    font-size: 1.2rem;
    line-height: 1;
    width: 38px;
    height: 38px;
    border-radius: 10px;
    cursor: pointer;
    color: var(--xchat-text-muted);
    transition: background .12s ease, color .12s ease;
    flex-shrink: 0;
}

.xchat-icon-btn:hover {
    background: var(--xchat-primary-light);
    color: var(--xchat-primary);
}

.xchat-icon-btn.recording {
    background: var(--xchat-danger);
    color: #fff;
    animation: xchatPulse 1s infinite;
}

@keyframes xchatPulse {
    0% { box-shadow: 0 0 0 0 rgba(239,68,68,.5); }
    70% { box-shadow: 0 0 0 9px rgba(239,68,68,0); }
    100% { box-shadow: 0 0 0 0 rgba(239,68,68,0); }
}

.xchat-message-input {
    flex: 1 1 auto;
    resize: none;
    max-height: 120px;
    border: 1.5px solid var(--xchat-border);
    border-radius: 20px;
    padding: 0.55rem 1rem;
    font-size: 0.92rem;
    font-family: inherit;
    background: var(--xchat-bg);
    transition: border-color .15s ease, background .15s ease;
}

.xchat-message-input:focus {
    outline: none;
    border-color: var(--xchat-primary);
    background: #fff;
}

.xchat-send-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    min-width: 40px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, var(--xchat-primary), var(--xchat-primary-dark));
    color: #fff;
    font-size: 1.05rem;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(106, 92, 245, .35);
    transition: transform .08s ease, box-shadow .15s ease;
    flex-shrink: 0;
}

.xchat-send-btn:hover {
    box-shadow: 0 6px 16px rgba(106, 92, 245, .45);
}

.xchat-send-btn:active {
    transform: scale(.94);
}

.xchat-emoji-picker {
    display: flex;
    flex-wrap: wrap;
    gap: 0.2rem;
    padding: 0.6rem;
    margin-top: 0.5rem;
    border: 1px solid var(--xchat-border);
    border-radius: var(--xchat-radius-md);
    background: var(--xchat-bg);
    animation: xchatPop .15s ease both;
}

.xchat-emoji {
    font-size: 1.3rem;
    cursor: pointer;
    padding: 0.2rem 0.35rem;
    border-radius: 8px;
    transition: background .1s ease, transform .1s ease;
}

.xchat-emoji:hover {
    background: var(--xchat-primary-light);
    transform: scale(1.15);
}

.xchat-attachment-preview {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.7rem;
    margin-bottom: 0.5rem;
    background: var(--xchat-primary-light);
    border-radius: var(--xchat-radius-md);
    font-size: 0.85rem;
    animation: xchatPop .15s ease both;
}

.xchat-attachment-name {
    flex: 1 1 auto;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: 600;
}

.btn-close-attachment {
    border: none;
    background: none;
    font-size: 1.2rem;
    line-height: 1;
    cursor: pointer;
    color: var(--xchat-text-muted);
}

.btn-close-attachment:hover {
    color: var(--xchat-danger);
}

.xchat-connect-error {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.6rem 0.85rem;
    margin-bottom: 0.75rem;
    background: #fef2f2;
    color: #991b1b;
    border: 1px solid #fecaca;
    border-radius: var(--xchat-radius-md);
    font-size: 0.82rem;
    animation: xchatPop .2s ease both;
}

.xchat-connect-error button {
    border: none;
    background: none;
    color: #991b1b;
    font-weight: 700;
    text-decoration: underline;
    cursor: pointer;
    padding: 0;
    font-size: inherit;
}

/* ---------- agent console ---------- */

.xchat-app-agent {
    background: var(--xchat-bg);
}

.xchat-console {
    display: grid;
    grid-template-columns: 320px 1fr;
    flex: 1 1 auto;
    min-height: 0;
}

.xchat-conversation-list-pane {
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--xchat-border);
    background: var(--xchat-surface);
    overflow: hidden;
}

.xchat-console-header {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 1rem 1.1rem;
    border-bottom: 1px solid var(--xchat-border);
}

.xchat-switch-btn {
    margin-left: auto;
    font-size: 1.1rem;
}

.xchat-conversation-list {
    flex: 1 1 auto;
    overflow-y: auto;
}

.xchat-conversation-item {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.75rem 1.1rem;
    cursor: pointer;
    border-bottom: 1px solid var(--xchat-border);
    position: relative;
    transition: background .12s ease;
}

.xchat-conversation-item:hover {
    background: var(--xchat-bg);
}

.xchat-conversation-item.active {
    background: var(--xchat-primary-light);
}

.xchat-conversation-item.active::before {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 3px;
    background: var(--xchat-primary);
}

.xchat-conversation-item-body {
    flex: 1 1 auto;
    min-width: 0;
}

.xchat-conversation-item-top {
    display: flex;
    justify-content: space-between;
    gap: 0.4rem;
}

.xchat-conversation-name {
    font-weight: 700;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.xchat-conversation-time {
    font-size: 0.7rem;
    color: var(--xchat-text-muted);
    flex-shrink: 0;
}

.xchat-conversation-preview {
    font-size: 0.8rem;
    color: var(--xchat-text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.xchat-unread-badge {
    background: var(--xchat-primary);
    color: #fff;
    font-size: 0.7rem;
    font-weight: 700;
    min-width: 19px;
    height: 19px;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    animation: xchatPop .2s ease both;
}

.xchat-window-agent {
    height: 100%;
}

.xchat-empty-state {
    grid-column: 2;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--xchat-text-muted);
    gap: 0.5rem;
}

.xchat-empty-icon {
    font-size: 2.2rem;
    opacity: .6;
}

@media (max-width: 760px) {
    .xchat-console {
        grid-template-columns: 1fr;
    }

    .xchat-conversation-list-pane {
        display: none;
    }

    .xchat-panel-card {
        padding: 1.5rem;
    }
}
