/* ============================================================
   Omnichannel Chat System - Stylesheet
   ============================================================ */

/* --- Reset & Base --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-light: #dbeafe;
    --success: #16a34a;
    --warning: #f59e0b;
    --danger: #dc2626;
    --danger-light: #fef2f2;
    --gray-50: #f9fafb;
    --gray-100: #f3f4f6;
    --gray-200: #e5e7eb;
    --gray-300: #d1d5db;
    --gray-400: #9ca3af;
    --gray-500: #6b7280;
    --gray-600: #4b5563;
    --gray-700: #374151;
    --gray-800: #1f2937;
    --gray-900: #111827;
    --white: #ffffff;
    --bg: #f9fafb;
    --sidebar-w: 380px;
    --header-h: 56px;
    --composer-h: 72px;
    --radius: 8px;
    --radius-lg: 12px;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
    --shadow: 0 1px 3px rgba(0,0,0,0.1), 0 1px 2px rgba(0,0,0,0.06);
    --shadow-lg: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.06);
    --transition: 150ms ease;

    /* Channel Colors */
    --whatsapp: #25D366;
    --instagram: #E1306C;
    --messenger: #0084FF;
    --tiktok: #000000;
}

html { font-size: 14px; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background: var(--bg);
    color: var(--gray-800);
    line-height: 1.5;
    overflow: hidden;
    height: 100vh;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* --- Layout --- */
.app-layout {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* --- Sidebar (Conversation List) --- */
.sidebar {
    width: var(--sidebar-w);
    min-width: var(--sidebar-w);
    background: var(--white);
    border-right: 1px solid var(--gray-200);
    display: flex;
    flex-direction: column;
    height: 100vh;
    z-index: 10;
}

.sidebar-header {
    padding: 12px 16px;
    border-bottom: 1px solid var(--gray-200);
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: var(--header-h);
    background: var(--white);
}

.sidebar-header h1 {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--gray-900);
    flex: 1;
}

.sidebar-header .stats-badge {
    background: var(--primary);
    color: white;
    font-size: 0.75rem;
    padding: 2px 8px;
    border-radius: 999px;
    font-weight: 600;
}

/* Search & Filters */
.sidebar-search {
    padding: 8px 12px;
    border-bottom: 1px solid var(--gray-100);
}

.search-input {
    width: 100%;
    padding: 8px 12px 8px 36px;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    font-size: 0.9rem;
    outline: none;
    background: var(--gray-50);
    transition: border var(--transition);
}
.search-input:focus {
    border-color: var(--primary);
    background: var(--white);
}

.search-wrapper {
    position: relative;
}
.search-wrapper .search-icon {
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--gray-400);
    font-size: 0.9rem;
}

.sidebar-filters {
    display: flex;
    gap: 6px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--gray-100);
    overflow-x: auto;
    flex-wrap: nowrap;
}

.filter-btn {
    padding: 4px 10px;
    border: 1px solid var(--gray-200);
    border-radius: 999px;
    font-size: 0.75rem;
    cursor: pointer;
    background: var(--white);
    color: var(--gray-600);
    white-space: nowrap;
    transition: all var(--transition);
}
.filter-btn:hover { background: var(--gray-50); border-color: var(--gray-300); }
.filter-btn.active { background: var(--primary); color: white; border-color: var(--primary); }

/* Conversation List */
.conversation-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
}

.conv-item {
    display: flex;
    align-items: flex-start;
    padding: 12px 16px;
    cursor: pointer;
    border-bottom: 1px solid var(--gray-50);
    transition: background var(--transition);
    gap: 10px;
    position: relative;
}
.conv-item:hover { background: var(--gray-50); }
.conv-item.active { background: var(--primary-light); }
.conv-item.unread { font-weight: 600; }

.conv-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--gray-200);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    font-weight: 600;
    color: var(--gray-600);
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
}

.conv-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.chat-header-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    overflow: hidden;
    flex-shrink: 0;
    margin-right: 8px;
}

.chat-header-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.conv-avatar .channel-dot {
    position: absolute;
    bottom: -2px;
    right: -2px;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 2px solid var(--white);
    font-size: 7px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
}

.conv-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.conv-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.conv-name {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--gray-900);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conv-time {
    font-size: 0.7rem;
    color: var(--gray-400);
    white-space: nowrap;
    flex-shrink: 0;
}

.conv-preview {
    font-size: 0.8rem;
    color: var(--gray-500);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conv-meta {
    display: flex;
    gap: 4px;
    align-items: center;
    margin-top: 2px;
}

.conv-badge {
    font-size: 0.65rem;
    padding: 1px 6px;
    border-radius: 999px;
    font-weight: 600;
}
.badge-ai { background: #ede9fe; color: #7c3aed; }
.badge-unread { background: var(--primary); color: white; min-width: 18px; text-align: center; }
.badge-window-closed { background: var(--danger-light); color: var(--danger); }
.badge-attention { background: #fef3c7; color: #b45309; animation: pulse-attention 2s infinite; }
@keyframes pulse-attention { 0%,100% { opacity:1; } 50% { opacity:0.6; } }
.conv-item.attention { border-left: 3px solid #f59e0b; }
.badge-export { background: #dbeafe; color: #1d4ed8; }
.btn-export-active { background: #dbeafe !important; border-radius: 4px; }
.attention-banner { display:flex; align-items:center; gap:8px; padding:8px 16px; background:#fef3c7; color:#92400e; font-size:0.85rem; border-bottom:1px solid #fde68a; }
.attention-banner button { margin-left:auto; background:#f59e0b; color:white; border:none; padding:4px 12px; border-radius:4px; cursor:pointer; font-size:0.8rem; }
.attention-banner button:hover { background:#d97706; }

/* Customer Info Popover */
.phone-link { cursor:pointer; text-decoration:underline dotted; color:var(--primary); position:relative; }
.phone-link:hover { color:var(--primary-dark, #1d4ed8); }
.customer-popover {
    position:absolute; top:100%; left:0; z-index:999;
    background:white; border:1px solid var(--gray-200); border-radius:8px;
    box-shadow:0 4px 20px rgba(0,0,0,0.15); padding:12px 16px;
    min-width:280px; max-width:380px; font-size:0.8rem; line-height:1.5;
    margin-top:6px;
}
.customer-popover::before {
    content:''; position:absolute; top:-6px; left:16px;
    width:12px; height:12px; background:white;
    border-left:1px solid var(--gray-200); border-top:1px solid var(--gray-200);
    transform:rotate(45deg);
}
.customer-popover .cp-loading { color:var(--gray-400); padding:8px 0; }
.customer-popover .cp-error { color:var(--danger); }
.customer-popover .cp-label { font-weight:600; color:var(--gray-500); font-size:0.7rem; text-transform:uppercase; margin-top:8px; }
.customer-popover .cp-label:first-child { margin-top:0; }
.customer-popover .cp-value { color:var(--gray-800); }
.customer-popover .cp-badge { display:inline-block; padding:1px 6px; border-radius:999px; font-size:0.7rem; font-weight:600; }
.customer-popover .cp-badge-ok { background:#d1fae5; color:#065f46; }
.customer-popover .cp-badge-warn { background:#fef3c7; color:#92400e; }
.customer-popover .cp-badge-danger { background:#fee2e2; color:#991b1b; }
.customer-popover .cp-treatments { margin-top:6px; }
.customer-popover .cp-treatment { padding:2px 0; border-bottom:1px solid var(--gray-100); }
.customer-popover .cp-close { position:absolute; top:4px; right:6px; cursor:pointer; color:var(--gray-400); font-size:1.1rem; line-height:1; padding:4px 6px; z-index:1000; }
.customer-popover .cp-close:hover { color:var(--gray-700); background:var(--gray-100); border-radius:4px; }
.badge-channel {
    font-size: 0.6rem;
    padding: 1px 5px;
    border-radius: 3px;
    color: white;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* --- Main Chat Area --- */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: var(--gray-50);
    min-width: 0;
}

/* Chat Header */
.chat-header {
    display: flex;
    align-items: center;
    padding: 10px 20px;
    background: var(--white);
    border-bottom: 1px solid var(--gray-200);
    min-height: var(--header-h);
    gap: 12px;
}

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

.chat-header-name {
    font-size: 1rem;
    font-weight: 700;
    color: var(--gray-900);
}

.chat-header-meta {
    display: flex;
    gap: 8px;
    align-items: center;
    font-size: 0.75rem;
    color: var(--gray-500);
    flex-wrap: wrap;
}

.chat-header-actions {
    display: flex;
    gap: 6px;
    align-items: center;
    flex-shrink: 0;
}

.window-timer {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 8px;
    border-radius: var(--radius);
    font-size: 0.75rem;
    font-weight: 600;
}
.window-timer.open { background: #ecfdf5; color: var(--success); }
.window-timer.warning { background: #fffbeb; color: var(--warning); }
.window-timer.closed { background: var(--danger-light); color: var(--danger); }

/* Empty State */
.chat-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 12px;
    color: var(--gray-400);
}
.chat-empty-icon { font-size: 3rem; opacity: 0.5; }
.chat-empty-text { font-size: 1rem; }

/* Messages Area */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 20px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.msg-date-divider {
    text-align: center;
    padding: 8px 0;
    font-size: 0.7rem;
    color: var(--gray-400);
    position: relative;
}
.msg-date-divider span {
    background: var(--gray-50);
    padding: 0 12px;
    position: relative;
    z-index: 1;
}
.msg-date-divider::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    height: 1px;
    background: var(--gray-200);
}

.msg-bubble {
    max-width: 70%;
    padding: 8px 12px;
    border-radius: var(--radius-lg);
    position: relative;
    word-wrap: break-word;
    line-height: 1.45;
}

.msg-row {
    display: flex;
    margin-bottom: 2px;
}
.msg-row.inbound { justify-content: flex-start; }
.msg-row.outbound { justify-content: flex-end; }

.msg-row.inbound .msg-bubble {
    background: var(--white);
    border: 1px solid var(--gray-200);
    border-bottom-left-radius: 4px;
    color: var(--gray-800);
}

.msg-row.outbound .msg-bubble {
    background: var(--primary);
    color: white;
    border-bottom-right-radius: 4px;
}
.msg-row.outbound .msg-bubble.ai-msg {
    background: #7c3aed;
}
.msg-row.outbound .msg-bubble.system-msg {
    background: var(--gray-300);
    color: var(--gray-700);
    max-width: 90%;
    text-align: center;
    font-size: 0.8rem;
    margin: 4px auto;
    border-radius: var(--radius);
}

.msg-text { font-size: 0.9rem; }
.msg-text p { margin-bottom: 4px; }
.msg-text p:last-child { margin-bottom: 0; }

.msg-footer {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 6px;
    margin-top: 4px;
    font-size: 0.65rem;
    opacity: 0.7;
}

.msg-time { white-space: nowrap; }

.msg-status { font-size: 0.7rem; }

.msg-ai-badge {
    font-size: 0.6rem;
    padding: 0px 4px;
    background: rgba(255,255,255,0.2);
    border-radius: 3px;
}
.msg-row.inbound .msg-ai-badge {
    background: rgba(124, 58, 237, 0.1);
    color: #7c3aed;
}

/* Media in messages */
.msg-image {
    max-width: 280px;
    border-radius: var(--radius);
    margin-bottom: 4px;
    cursor: pointer;
}
.msg-image img {
    width: 100%;
    border-radius: var(--radius);
    display: block;
}

.msg-audio {
    margin-bottom: 4px;
}
.msg-audio audio {
    max-width: 250px;
    height: 36px;
}
.msg-transcript {
    font-size: 0.75rem;
    font-style: italic;
    opacity: 0.8;
    margin-top: 4px;
    padding-top: 4px;
    border-top: 1px solid rgba(255,255,255,0.2);
}
.msg-row.inbound .msg-transcript {
    border-top-color: var(--gray-200);
}

/* Composer */
.chat-composer {
    padding: 12px 20px;
    background: var(--white);
    border-top: 1px solid var(--gray-200);
}

.composer-wrapper {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.composer-input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
    font-size: 0.9rem;
    resize: none;
    outline: none;
    max-height: 120px;
    min-height: 42px;
    font-family: inherit;
    line-height: 1.4;
    transition: border var(--transition);
}
.composer-input:focus { border-color: var(--primary); }
.composer-input:disabled { background: var(--gray-100); }

.composer-actions {
    display: flex;
    gap: 4px;
}

.template-notice {
    padding: 8px 12px;
    background: #fffbeb;
    border: 1px solid #fcd34d;
    border-radius: var(--radius);
    font-size: 0.8rem;
    color: #92400e;
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 6px;
}

.ai-suggestion-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: #ede9fe;
    border-radius: var(--radius);
    margin-bottom: 8px;
    font-size: 0.8rem;
}
.ai-suggestion-bar .label { color: #7c3aed; font-weight: 600; }

/* --- Buttons --- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 16px;
    border-radius: var(--radius);
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    border: 1px solid transparent;
    transition: all var(--transition);
    white-space: nowrap;
    font-family: inherit;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-success { background: var(--success); color: white; }
.btn-danger { background: var(--danger); color: white; }
.btn-outline { background: transparent; border: 1px solid var(--gray-300); color: var(--gray-700); }
.btn-outline:hover { background: var(--gray-50); }
.btn-ghost { background: transparent; color: var(--gray-600); padding: 6px 8px; }
.btn-ghost:hover { background: var(--gray-100); }
.btn-sm { padding: 4px 10px; font-size: 0.75rem; }
.btn-icon { padding: 6px; width: 36px; height: 36px; border-radius: 50%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-send {
    background: var(--primary);
    color: white;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    transition: background var(--transition);
    flex-shrink: 0;
}
.btn-send:hover { background: var(--primary-hover); }
.btn-send:disabled { background: var(--gray-300); cursor: not-allowed; }

/* --- Select / Dropdown --- */
.select-sm {
    padding: 4px 8px;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    font-size: 0.75rem;
    outline: none;
    background: var(--white);
    cursor: pointer;
}

/* --- Modal --- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 100;
    display: none;
    align-items: center;
    justify-content: center;
}
.modal-overlay.show { display: flex; }

.modal {
    background: var(--white);
    border-radius: var(--radius-lg);
    padding: 24px;
    width: 90%;
    max-width: 500px;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}
.modal-header h2 { font-size: 1.1rem; font-weight: 700; }

.modal-body { margin-bottom: 16px; }

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

/* --- Form Elements --- */
.form-group { margin-bottom: 12px; }
.form-group label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--gray-700);
    margin-bottom: 4px;
}
.form-control {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    font-size: 0.9rem;
    outline: none;
    font-family: inherit;
}
.form-control:focus { border-color: var(--primary); }

textarea.form-control { resize: vertical; min-height: 80px; }

/* --- Toast / Notifications --- */
.toast-container {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 200;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.toast {
    padding: 10px 16px;
    border-radius: var(--radius);
    font-size: 0.85rem;
    background: var(--gray-800);
    color: white;
    box-shadow: var(--shadow-lg);
    animation: slideIn 0.3s ease;
    max-width: 350px;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.warning { background: var(--warning); color: var(--gray-900); }

@keyframes slideIn {
    from { transform: translateX(100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

/* --- Login Page --- */
.login-page {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    background: var(--gray-100);
}

.login-card {
    background: var(--white);
    border-radius: var(--radius-lg);
    padding: 40px 32px;
    width: 100%;
    max-width: 400px;
    box-shadow: var(--shadow-lg);
}

.login-card h1 {
    font-size: 1.5rem;
    font-weight: 700;
    text-align: center;
    margin-bottom: 8px;
    color: var(--gray-900);
}

.login-card .subtitle {
    text-align: center;
    color: var(--gray-500);
    font-size: 0.85rem;
    margin-bottom: 24px;
}

.login-error {
    padding: 8px 12px;
    background: var(--danger-light);
    color: var(--danger);
    border-radius: var(--radius);
    font-size: 0.85rem;
    margin-bottom: 12px;
    display: none;
}

/* --- Settings Page --- */
.settings-container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
}

.settings-section {
    background: var(--white);
    border-radius: var(--radius-lg);
    padding: 20px;
    margin-bottom: 16px;
    box-shadow: var(--shadow-sm);
}

.settings-section h3 {
    font-size: 1rem;
    font-weight: 700;
    margin-bottom: 12px;
    color: var(--gray-900);
}

/* --- Mobile Menu Button --- */
.mobile-menu-btn {
    display: none;
    background: none;
    border: none;
    font-size: 1.3rem;
    cursor: pointer;
    padding: 4px;
    color: var(--gray-700);
}

.mobile-back-btn {
    display: none;
    background: none;
    border: none;
    font-size: 1.1rem;
    cursor: pointer;
    padding: 4px 8px;
    color: var(--gray-700);
}

/* --- Responsive --- */
@media (max-width: 768px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: 100%;
        min-width: 100%;
        transform: translateX(0);
        transition: transform 0.3s ease;
        z-index: 20;
    }
    .sidebar.hidden-mobile {
        transform: translateX(-100%);
    }

    .chat-area {
        width: 100%;
    }

    .mobile-menu-btn { display: block; }
    .mobile-back-btn { display: block; }

    .msg-bubble { max-width: 85%; }

    .chat-header-actions { gap: 2px; }

    .settings-container { padding: 12px; }
}

/* --- Scrollbar --- */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--gray-300); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--gray-400); }

/* --- Loading Spinner --- */
.spinner {
    display: inline-block;
    width: 18px;
    height: 18px;
    border: 2px solid var(--gray-200);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --- Template Picker --- */
.template-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.template-item {
    padding: 10px 12px;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    cursor: pointer;
    transition: all var(--transition);
}
.template-item:hover { border-color: var(--primary); background: var(--primary-light); }
.template-item .template-title { font-weight: 600; font-size: 0.85rem; }
.template-item .template-body { font-size: 0.8rem; color: var(--gray-500); margin-top: 2px; }

/* --- Typing Indicator --- */
.typing-indicator {
    display: inline-flex;
    gap: 3px;
    padding: 8px 12px;
}
.typing-indicator span {
    width: 6px;
    height: 6px;
    background: var(--gray-400);
    border-radius: 50%;
    animation: bounce 1.4s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-4px); }
}

/* --- Tab Navigation --- */
.tab-nav {
    display: flex;
    gap: 0;
    border-bottom: 2px solid var(--gray-200);
    margin-bottom: 16px;
}

.tab-btn {
    padding: 8px 16px;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--gray-500);
    cursor: pointer;
    transition: all var(--transition);
}
.tab-btn:hover { color: var(--gray-700); }
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); }
