*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #0f0f1a;
    --surface: #1a1a2e;
    --surface-2: #222240;
    --border: #2a2a4a;
    --accent: #10b981;
    --accent-dim: rgba(16, 185, 129, 0.12);
    --accent-border: rgba(16, 185, 129, 0.3);
    --text: #e0e0e0;
    --text-muted: #666;
    --text-dim: #888;
    --bubble-agent: #1a3a2e;
    --bubble-user: #222240;
    --success: #4ade80;
    --warning: #fbbf24;
    --danger: #f87171;
    --info: #60a5fa;
    --radius: 16px;
    --radius-sm: 8px;
    --sidebar-w: 300px;
}

html, body {
    height: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    min-height: 100dvh;
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

/* ── Auth Screen ── */

.auth-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 16px;
}

.auth-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 24px;
    padding: 48px 40px;
    width: 100%;
    max-width: 400px;
}

.auth-logo {
    text-align: center;
    margin-bottom: 32px;
}

.auth-logo-icon {
    font-size: 2.8rem;
    line-height: 1;
    margin-bottom: 12px;
}

.auth-logo h1 {
    font-size: 1.6rem;
    font-weight: 800;
    color: #fff;
    margin-bottom: 6px;
}

.auth-logo p {
    font-size: 0.875rem;
    color: var(--text-dim);
    line-height: 1.5;
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.form-group label {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-dim);
    letter-spacing: 0.5px;
    text-transform: uppercase;
}

.form-input {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 0.95rem;
    padding: 12px 16px;
    outline: none;
    transition: border-color 0.2s;
    width: 100%;
}

.form-input::placeholder {
    color: var(--text-muted);
}

.form-input:focus {
    border-color: var(--accent);
}

.btn-primary {
    background: linear-gradient(135deg, #10b981, #059669);
    border: none;
    border-radius: var(--radius-sm);
    color: #fff;
    cursor: pointer;
    font-size: 0.95rem;
    font-weight: 600;
    padding: 14px;
    margin-top: 6px;
    transition: opacity 0.2s, transform 0.15s;
    width: 100%;
}

.btn-primary:hover {
    opacity: 0.9;
    transform: translateY(-1px);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-primary:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.form-error {
    background: rgba(248, 113, 113, 0.1);
    border: 1px solid rgba(248, 113, 113, 0.3);
    border-radius: var(--radius-sm);
    color: var(--danger);
    font-size: 0.85rem;
    padding: 10px 14px;
    display: none;
}

.form-error.visible {
    display: block;
}

/* ── Dashboard Screen ── */

.dashboard-screen {
    display: flex;
    height: 100vh;
    height: 100dvh;
    height: var(--app-height, 100dvh);
    overflow: hidden;
}

/* Sidebar */

.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.sidebar-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 16px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.sidebar-title {
    font-size: 0.9rem;
    font-weight: 700;
    color: #fff;
    display: flex;
    align-items: center;
    gap: 6px;
    flex: 1;
    min-width: 0;
}

.sidebar-icon {
    font-size: 1rem;
    flex-shrink: 0;
}

.agent-badge {
    background: var(--accent-dim);
    border: 1px solid var(--accent-border);
    border-radius: 50px;
    color: #6ee7b7;
    font-size: 10px;
    font-weight: 600;
    padding: 2px 10px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 90px;
    flex-shrink: 0;
}

.btn-logout {
    background: none;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.9rem;
    padding: 4px 8px;
    flex-shrink: 0;
    transition: color 0.2s, border-color 0.2s;
}

.btn-logout:hover {
    color: var(--danger);
    border-color: var(--danger);
}

.conv-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}

.conv-list::-webkit-scrollbar {
    width: 4px;
}

.conv-list::-webkit-scrollbar-track {
    background: transparent;
}

.conv-list::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

.conv-empty {
    padding: 24px 16px;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.85rem;
}

.conv-item {
    padding: 12px 16px;
    cursor: pointer;
    border-left: 3px solid transparent;
    transition: background 0.15s, border-color 0.15s;
}

.conv-item:hover {
    background: var(--surface-2);
}

.conv-item.active {
    background: var(--accent-dim);
    border-left-color: var(--accent);
}

.conv-item-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 4px;
}

.conv-item-id {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text);
    font-family: monospace;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
}

.conv-item-time {
    font-size: 0.72rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

.status-badge {
    display: inline-block;
    font-size: 10px;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 50px;
}

.status-badge.unassigned {
    background: rgba(251, 191, 36, 0.15);
    color: #fbbf24;
    border: 1px solid rgba(251, 191, 36, 0.3);
}

.status-badge.waiting-agent {
    background: rgba(96, 165, 250, 0.15);
    color: #60a5fa;
    border: 1px solid rgba(96, 165, 250, 0.3);
}

.status-badge.waiting-user {
    background: rgba(16, 185, 129, 0.15);
    color: #10b981;
    border: 1px solid rgba(16, 185, 129, 0.3);
}

.status-badge.waiting-assign {
    background: rgba(251, 113, 133, 0.15);
    color: #fb7185;
    border: 1px solid rgba(251, 113, 133, 0.3);
}

.status-badge.assigned {
    background: rgba(139, 92, 246, 0.15);
    color: #8b5cf6;
    border: 1px solid rgba(139, 92, 246, 0.3);
}

.status-badge.closed {
    background: rgba(107, 114, 128, 0.15);
    color: #9ca3af;
    border: 1px solid rgba(107, 114, 128, 0.3);
}

.btn-refresh {
    background: none;
    border: none;
    border-top: 1px solid var(--border);
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.8rem;
    padding: 12px 16px;
    text-align: left;
    transition: color 0.2s;
    width: 100%;
    flex-shrink: 0;
}

.btn-refresh:hover {
    color: var(--accent);
}

/* Chat panel */

.chat-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

.chat-empty {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.chat-empty-icon {
    font-size: 2.5rem;
}

.chat-inner {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

/* Header */

.chat-header {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 16px 24px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    background: var(--surface);
}

.chat-header-info {
    flex: 1;
    min-width: 0;
}

.chat-header-title {
    font-size: 0.95rem;
    font-weight: 700;
    color: #fff;
    font-family: monospace;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-header-sub {
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-top: 2px;
}

.chat-header-status {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
    flex-shrink: 0;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text-muted);
    flex-shrink: 0;
    transition: background 0.3s, box-shadow 0.3s;
}

.status-dot.connecting {
    background: var(--warning);
    box-shadow: 0 0 6px var(--warning);
}

.status-dot.connected {
    background: var(--success);
    box-shadow: 0 0 6px var(--success);
}

.status-dot.disconnected {
    background: var(--danger);
    box-shadow: 0 0 6px var(--danger);
}

/* Messages */

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 24px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
}

.chat-messages::-webkit-scrollbar {
    width: 4px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

.msg-row {
    display: flex;
    flex-direction: column;
    max-width: 75%;
}

.msg-row.user {
    align-self: flex-start;
    align-items: flex-start;
}

.msg-row.agent {
    align-self: flex-end;
    align-items: flex-end;
}

.msg-bubble {
    border-radius: 18px;
    font-size: 0.92rem;
    line-height: 1.55;
    padding: 10px 16px;
    word-break: break-word;
}

.msg-row.user .msg-bubble {
    background: var(--bubble-user);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
    color: var(--text);
}

.msg-row.agent .msg-bubble {
    background: var(--bubble-agent);
    border-bottom-right-radius: 4px;
    color: var(--text);
}

.msg-meta {
    font-size: 0.72rem;
    color: var(--text-muted);
    margin-top: 4px;
    padding: 0 4px;
}

.msg-row.agent .msg-meta {
    text-align: right;
}

/* Input */

.chat-footer {
    border-top: 1px solid var(--border);
    padding: 16px 20px;
    display: flex;
    gap: 10px;
    align-items: flex-end;
    flex-shrink: 0;
    background: var(--surface);
}

.chat-input {
    flex: 1;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 20px;
    color: var(--text);
    font-family: inherit;
    font-size: 0.92rem;
    line-height: 1.4;
    max-height: 120px;
    outline: none;
    overflow-y: auto;
    padding: 11px 18px;
    resize: none;
    transition: border-color 0.2s;
}

.chat-input::placeholder {
    color: var(--text-muted);
}

.chat-input:focus {
    border-color: var(--accent);
}

.btn-send {
    background: linear-gradient(135deg, #10b981, #059669);
    border: none;
    border-radius: 50%;
    color: #fff;
    cursor: pointer;
    flex-shrink: 0;
    height: 44px;
    width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s, transform 0.15s;
}

.btn-send:hover {
    opacity: 0.9;
    transform: scale(1.05);
}

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

.btn-send:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
}

.btn-send svg {
    width: 18px;
    height: 18px;
}

/* Unread dot */

.unread-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent);
    box-shadow: 0 0 6px var(--accent);
    flex-shrink: 0;
}

/* Assign button */

.btn-assign {
    background: linear-gradient(135deg, #10b981, #059669);
    border: none;
    border-radius: 50px;
    color: #fff;
    cursor: pointer;
    font-size: 0.78rem;
    font-weight: 600;
    padding: 6px 16px;
    flex-shrink: 0;
    transition: opacity 0.2s, transform 0.15s;
    white-space: nowrap;
}

.btn-assign:hover {
    opacity: 0.9;
    transform: translateY(-1px);
}

.btn-assign:active {
    transform: translateY(0);
}

.btn-assign:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* Back button (visible only on mobile) */

.btn-back {
    display: none;
    background: none;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    cursor: pointer;
    width: 36px;
    height: 36px;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: color 0.2s, border-color 0.2s;
    padding: 0;
}

.btn-back:hover {
    color: var(--accent);
    border-color: var(--accent);
}

.btn-back svg {
    width: 18px;
    height: 18px;
}

/* Preloader */

.preloader {
    position: fixed;
    inset: 0;
    z-index: 100;
    background: var(--bg);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 1;
    transition: opacity 0.25s ease;
}

.preloader.fade-out {
    opacity: 0;
    pointer-events: none;
}

.preloader-spinner {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    animation: preloader-spin 0.8s linear infinite;
}

@keyframes preloader-spin {
    to { transform: rotate(360deg); }
}

/* Utility */

.hidden {
    display: none !important;
}

/* ── Responsive ─────────────────────────────────────────────────────────── */

@media (max-width: 1024px) {
    :root {
        --sidebar-w: 260px;
    }

    .chat-header {
        padding: 14px 18px;
    }

    .chat-messages {
        padding: 16px 18px;
    }

    .chat-footer {
        padding: 14px 16px;
    }
}

@media (max-width: 768px) {
    .auth-card {
        padding: 36px 24px;
        border-radius: 20px;
    }

    .auth-logo h1 {
        font-size: 1.4rem;
    }

    .dashboard-screen {
        position: relative;
    }

    .sidebar {
        width: 100%;
        border-right: none;
    }

    .chat-panel {
        position: absolute;
        inset: 0;
        background: var(--bg);
        transform: translateX(100%);
        transition: transform 0.28s ease;
        will-change: transform;
        z-index: 5;
    }

    .dashboard-screen.chat-open .chat-panel {
        transform: translateX(0);
    }

    .btn-back {
        display: inline-flex;
    }

    .chat-header {
        padding: 12px 14px;
        gap: 10px;
    }

    .chat-header-title {
        font-size: 0.9rem;
    }

    .chat-header-status span:last-child {
        display: none;
    }

    .chat-messages {
        padding: 14px 14px;
        gap: 8px;
    }

    .msg-row {
        max-width: 88%;
    }

    .msg-bubble {
        font-size: 0.9rem;
        padding: 9px 14px;
    }

    .chat-footer {
        padding: 10px 12px;
        gap: 8px;
    }

    .chat-input {
        font-size: 16px;
        padding: 10px 16px;
    }

    .btn-send {
        width: 42px;
        height: 42px;
    }

    .conv-item {
        padding: 14px 16px;
    }

    .btn-refresh {
        padding: 14px 16px;
        font-size: 0.85rem;
    }
}

@media (max-width: 480px) {
    .auth-screen {
        padding: 12px;
    }

    .auth-card {
        padding: 28px 20px;
        border-radius: 16px;
    }

    .auth-logo {
        margin-bottom: 24px;
    }

    .auth-logo-icon {
        font-size: 2.4rem;
    }

    .auth-logo h1 {
        font-size: 1.25rem;
    }

    .auth-logo p {
        font-size: 0.82rem;
    }

    .form-input {
        font-size: 16px;
    }

    .sidebar-header {
        padding: 14px 14px;
    }

    .sidebar-title {
        font-size: 0.85rem;
    }

    .agent-badge {
        max-width: 110px;
    }

    .conv-item-id {
        font-size: 0.8rem;
    }
}

@media (hover: none) {
    .btn-primary:hover,
    .btn-send:hover {
        opacity: 1;
        transform: none;
    }

    .conv-item:hover {
        background: transparent;
    }

    .conv-item:active {
        background: var(--surface-2);
    }
}
