/* ============================================================
   SOFIACHAT — Design System
   Modern WhatsApp-style chat interface
   ============================================================ */

:root {
    --primary: #7c3aed;
    --primary-hover: #6d28d9;
    --primary-bg: rgba(124, 58, 237, 0.08);
    --success: #22c55e;
    --warning: #eab308;
    --danger: #ef4444;
    --info: #3b82f6;

    --bg-body: #f0f2f5;
    --bg-card: #ffffff;
    --bg-sidebar: #ffffff;
    --bg-chat: #efeae2;
    --bg-chat-pattern: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23d5cfc6' fill-opacity='0.15'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
    --bg-bubble-in: #ffffff;
    --bg-bubble-out: #d9fdd3;
    --bg-input: #ffffff;
    --bg-header: #ffffff;
    --bg-secondary: #f3f4f6;
    --bg-hover: rgba(0, 0, 0, 0.03);

    --border-light: #e5e7eb;
    --border-default: #d1d5db;
    --border: #e5e7eb;
    --text-primary: #111827;
    --text-secondary: #4b5563;
    --text-muted: #9ca3af;
    --text-on-primary: #ffffff;

    --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.07);
    --shadow-lg: 0 10px 25px rgba(0,0,0,0.1);

    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-mono: 'JetBrains Mono', monospace;
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
}

*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    font-size: 14px;
    -webkit-font-smoothing: antialiased;
}

body {
    font-family: var(--font-sans);
    background: var(--bg-body);
    color: var(--text-primary);
    line-height: 1.5;
    min-height: 100vh;
    overflow: hidden;
}

button,
input,
textarea,
select {
    font: inherit;
}

img {
    max-width: 100%;
    display: block;
}

::-webkit-scrollbar {
    width: 5px;
    height: 5px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(0,0,0,0.15);
    border-radius: 3px;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes slideUp {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

@keyframes pulse {
    0%,100% { opacity: 1; }
    50% { opacity: 0.5; }
}

@keyframes modalSlideIn {
    from { opacity: 0; transform: translateY(20px) scale(0.97); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes emojiPopIn {
    from { opacity: 0; transform: scale(0.9) translateY(8px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

/* LOGIN */
.login-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #0c0a1a;
    background-image: radial-gradient(ellipse 80% 50% at 50% -20%, rgba(124,58,237,0.15), transparent);
}

.login-container {
    width: 100%;
    max-width: 400px;
    padding: 2rem;
}

.login-logo {
    text-align: center;
    margin-bottom: 2rem;
}

.login-logo h1 {
    font-size: 2rem;
    font-weight: 800;
    color: #fff;
}

.login-logo h1 span {
    color: var(--primary);
}

.login-logo p {
    color: #64748b;
    font-size: 0.9rem;
    margin-top: 0.25rem;
}

.login-card {
    background: rgba(23,28,38,0.85);
    backdrop-filter: blur(20px);
    border: 1px solid rgba(255,255,255,0.07);
    border-radius: var(--radius-lg);
    padding: 2rem;
    box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}

.login-card .form-input {
    background: rgba(15,18,25,0.7);
    border-color: rgba(255,255,255,0.08);
    color: #e2e8f0;
}

.login-card .form-input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(124,58,237,0.15);
}

.login-card .form-input::placeholder {
    color: #475569;
}

.login-card .form-label {
    color: #94a3b8;
}

/* FORMS */
.form-group {
    margin-bottom: 1rem;
}

.form-label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 0.3rem;
}

.form-input {
    width: 100%;
    padding: 0.6rem 0.85rem;
    background: var(--bg-input);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 0.9rem;
    outline: none;
    transition: all 0.15s;
}

.form-input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-bg);
}

.form-input::placeholder {
    color: var(--text-muted);
}

.form-select {
    width: 100%;
    padding: 0.6rem 0.85rem;
    background: var(--bg-input);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 0.9rem;
    outline: none;
    cursor: pointer;
}

.form-hint {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 0.2rem;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

/* BUTTONS */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.55rem 1rem;
    font-family: var(--font-sans);
    font-size: 0.875rem;
    font-weight: 600;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: var(--primary);
    color: var(--text-on-primary);
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
}

.btn-secondary {
    background: var(--bg-card);
    color: var(--text-secondary);
    border: 1px solid var(--border-light);
}

.btn-secondary:hover:not(:disabled) {
    background: #f9fafb;
}

.btn-danger {
    background: transparent;
    color: var(--danger);
    border: 1px solid rgba(239,68,68,0.25);
}

.btn-sm {
    padding: 0.35rem 0.65rem;
    font-size: 0.8rem;
}

.btn-block {
    width: 100%;
}

/* LAYOUT */
.app-layout {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* LEFT SIDEBAR */
.app-sidebar {
    width: 60px;
    background: var(--primary);
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 1rem 0;
    flex-shrink: 0;
}

.app-sidebar-logo {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: rgba(255,255,255,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-weight: 800;
    font-size: 1rem;
    margin-bottom: 1.5rem;
    cursor: pointer;
}

.app-sidebar-nav {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    flex: 1;
}

.app-sidebar-btn {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    border: none;
    background: transparent;
    color: rgba(255,255,255,0.6);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}

.app-sidebar-btn:hover {
    background: rgba(255,255,255,0.1);
    color: #fff;
}

.app-sidebar-btn.active {
    background: rgba(255,255,255,0.2);
    color: #fff;
}

.app-sidebar-footer {
    margin-top: auto;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    align-items: center;
}

.app-sidebar-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255,255,255,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 0.75rem;
    color: #fff;
    cursor: pointer;
}

/* CHAT LAYOUT */
.chat-layout {
    display: flex;
    flex: 1;
    height: 100%;
    overflow: hidden;
    min-width: 0;
}

/* CHAT LIST */
.chat-list-panel {
    width: 340px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-light);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    min-width: 280px;
}

.chat-list-header {
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.chat-list-header h2 {
    font-size: 1.1rem;
    font-weight: 700;
    flex: 1;
}

.chat-list-search {
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid var(--border-light);
}

.chat-list-search input {
    width: 100%;
    padding: 0.5rem 0.75rem;
    background: var(--bg-body);
    border: none;
    border-radius: 8px;
    font-size: 0.85rem;
    outline: none;
    color: var(--text-primary);
}

.chat-list-search input::placeholder {
    color: var(--text-muted);
}

.chat-list-body {
    flex: 1;
    overflow-y: auto;
}

.chat-list-empty {
    text-align: center;
    padding: 3rem 1.5rem;
    color: var(--text-muted);
}

.chat-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    cursor: pointer;
    border-bottom: 1px solid rgba(0,0,0,0.04);
    transition: background 0.1s;
}

.chat-item:hover {
    background: #f5f5f5;
}

.chat-item.active {
    background: var(--primary-bg);
}

.chat-item-info {
    flex: 1;
    min-width: 0;
}

.chat-item-name {
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-item-preview {
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 1px;
}

.chat-item-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.25rem;
    flex-shrink: 0;
}

.chat-item-time {
    font-size: 0.7rem;
    color: var(--text-muted);
}

.chat-item-badge {
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    background: var(--primary);
    color: #fff;
    font-size: 0.65rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
}

/* AVATAR */
.avatar {
    flex-shrink: 0;
    border-radius: 50%;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    color: #fff;
    user-select: none;
}

.avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* CHAT VIEW */
.chat-view {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg-chat);
    background-image: var(--bg-chat-pattern);
    min-width: 0;
}

.chat-view-header {
    padding: 0.65rem 1rem;
    background: var(--bg-header);
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    box-shadow: var(--shadow-sm);
    z-index: 10;
    min-height: 64px;
}

.chat-view-header-info {
    flex: 1;
    min-width: 0;
}

.chat-view-header-status {
    font-size: 0.75rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-contact-btn {
    background: transparent;
    border: none;
    padding: 0;
    margin: 0;
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--text-primary);
    cursor: pointer;
    text-align: left;
    transition: color 0.15s;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

.chat-contact-btn:hover {
    color: var(--primary);
    text-decoration: underline;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.msg-row {
    display: flex;
    margin-bottom: 2px;
}

.msg-row.in {
    justify-content: flex-start;
}

.msg-row.out {
    justify-content: flex-end;
}

.msg-bubble {
    max-width: 65%;
    padding: 0.5rem 0.65rem;
    border-radius: 8px;
    font-size: 0.9rem;
    line-height: 1.45;
    position: relative;
    box-shadow: 0 1px 1px rgba(0,0,0,0.06);
    word-wrap: break-word;
    white-space: pre-wrap;
}

.msg-row.in .msg-bubble {
    background: var(--bg-bubble-in);
    border-top-left-radius: 2px;
}

.msg-row.out .msg-bubble {
    background: var(--bg-bubble-out);
    border-top-right-radius: 2px;
}

.msg-time {
    font-size: 0.65rem;
    color: rgba(0,0,0,0.4);
    text-align: right;
    margin-top: 3px;
}

.msg-status {
    font-size: 0.6rem;
    color: rgba(0,0,0,0.35);
    margin-left: 4px;
}

.msg-template-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.68rem;
    color: var(--primary);
    font-weight: 600;
    margin-bottom: 0.25rem;
}

/* DATE SEPARATOR */
.msg-date-sep {
    text-align: center;
    padding: 0.5rem 0;
}

.msg-date-sep span {
    background: rgba(255,255,255,0.9);
    color: var(--text-muted);
    font-size: 0.72rem;
    font-weight: 600;
    padding: 0.25rem 0.75rem;
    border-radius: 6px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}

/* EMPTY CHAT VIEW */
.chat-view-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    background: var(--bg-body);
    text-align: center;
    padding: 2rem;
}

.chat-view-empty-icon {
    font-size: 4rem;
    margin-bottom: 1rem;
    opacity: 0.3;
}

.chat-view-empty h3 {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

/* WINDOW EXPIRED BANNER */
.window-expired {
    background: #fef3c7;
    border: 1px solid #fde68a;
    color: #92400e;
    padding: 0.6rem 1rem;
    font-size: 0.82rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* CHAT INPUT */
.chat-input-bar {
    padding: 0.6rem 1rem;
    background: var(--bg-header);
    border-top: 1px solid var(--border-light);
    display: flex;
    align-items: flex-end;
    gap: 0.6rem;
    position: relative;
}

.chat-input-bar textarea {
    flex: 1;
    resize: none;
    border: none;
    outline: none;
    font-family: var(--font-sans);
    font-size: 0.95rem;
    padding: 0.6rem 0.85rem;
    background: var(--bg-body);
    border-radius: 20px;
    max-height: 120px;
    min-height: 40px;
    line-height: 1.4;
    color: var(--text-primary);
}

.chat-input-bar textarea::placeholder {
    color: var(--text-muted);
}

.chat-send-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s;
}

.chat-send-btn:hover {
    background: var(--primary-hover);
}

.chat-send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* EMOJI PICKER */
.emoji-picker-container {
    position: relative;
    flex-shrink: 0;
}

.emoji-trigger-btn {
    background: transparent;
    border: none;
    font-size: 20px;
    cursor: pointer;
    padding: 6px 8px;
    border-radius: 8px;
    line-height: 1;
    opacity: 0.7;
    transition: opacity 0.15s, background 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.emoji-trigger-btn:hover {
    opacity: 1;
    background: rgba(0, 0, 0, 0.06);
}

.emoji-picker-popover {
    position: absolute;
    bottom: calc(100% + 8px);
    left: 0;
    z-index: 500;
    animation: emojiPopIn 0.15s ease;
}

.emoji-picker-popover emoji-picker {
    --background: #ffffff;
    --border-color: rgba(0, 0, 0, 0.08);
    --indicator-color: var(--primary);
    --input-border-color: rgba(0, 0, 0, 0.08);
    --input-font-color: #111827;
    --input-placeholder-color: rgba(0, 0, 0, 0.35);
    --category-emoji-padding: 0.375rem;
    --num-columns: 8;
    --emoji-size: 1.4rem;
    --button-hover-background: rgba(124, 58, 237, 0.08);
    width: 320px;
    height: 360px;
    border-radius: 14px;
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.18);
}

/* CHAT BACK BUTTON */
.mobile-only {
    display: none;
}

.chat-back-btn {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
}

.chat-back-btn:hover {
    background: rgba(0,0,0,0.05);
    color: var(--text-primary);
}

/* MODAL */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    animation: fadeIn 0.15s;
    padding: 1rem;
}

.modal {
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-lg);
    width: 100%;
    max-width: 520px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: var(--shadow-lg);
}

.modal-header {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border-light);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-header h3 {
    font-size: 1rem;
    font-weight: 700;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 1.3rem;
    line-height: 1;
}

.modal-body {
    padding: 1.25rem;
}

.modal-footer {
    padding: 0.85rem 1.25rem;
    border-top: 1px solid var(--border-light);
    display: flex;
    justify-content: flex-end;
    gap: 0.6rem;
    flex-wrap: wrap;
}

/* CONTACT MODAL */
.contact-modal {
    max-width: 620px;
}

.contact-modal-header {
    align-items: center;
}

.contact-modal-header-info {
    min-width: 0;
}

.contact-details-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.85rem;
}

.contact-detail-card {
    background: #f8fafc;
    border: 1px solid var(--border-light);
    border-radius: 10px;
    padding: 0.85rem 0.95rem;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
    min-width: 0;
}

.contact-detail-card label {
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--text-muted);
    letter-spacing: 0.03em;
}

.contact-detail-card span {
    font-size: 0.9rem;
    color: var(--text-primary);
    word-break: break-word;
}

.contact-detail-card-full {
    grid-column: 1 / -1;
}

/* TOAST */
.toast-container {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.toast {
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    padding: 0.75rem 1rem;
    min-width: 260px;
    max-width: 380px;
    box-shadow: var(--shadow-md);
    animation: slideUp 0.25s;
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.toast.success {
    border-left: 3px solid var(--success);
}

.toast.error {
    border-left: 3px solid var(--danger);
}

.toast-message {
    flex: 1;
    font-size: 0.875rem;
    font-weight: 500;
}

.toast-close {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
}

/* SPINNER */
.spinner {
    width: 18px;
    height: 18px;
    border: 2px solid var(--border-light);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

.spinner-lg {
    width: 28px;
    height: 28px;
    border-width: 3px;
}

.loading-center {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 3rem;
}

/* BADGE */
.badge {
    display: inline-flex;
    align-items: center;
    padding: 0.15rem 0.5rem;
    border-radius: 4px;
    font-size: 0.73rem;
    font-weight: 600;
}

.badge-success {
    background: rgba(34,197,94,0.1);
    color: var(--success);
}

.badge-danger {
    background: rgba(239,68,68,0.1);
    color: var(--danger);
}

.badge-info {
    background: rgba(59,130,246,0.1);
    color: var(--info);
}

.badge-warning {
    background: rgba(234,179,8,0.1);
    color: var(--warning);
}

/* CONNECTIONS PAGE */
.page-container {
    max-width: 900px;
    margin: 0 auto;
    padding: 2rem;
}

.page-title {
    font-size: 1.4rem;
    font-weight: 800;
    margin-bottom: 0.5rem;
}

.page-subtitle {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-bottom: 1.5rem;
}

.card {
    background: var(--bg-card);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
}

.card-body {
    padding: 1.25rem;
}

/* WEBHOOK URL BOX */
.webhook-box {
    background: var(--bg-body);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    padding: 0.6rem 0.85rem;
    font-family: var(--font-mono);
    font-size: 0.78rem;
    color: var(--primary);
    word-break: break-all;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.webhook-box button {
    flex-shrink: 0;
}

/* UTILITY */
.text-muted { color: var(--text-muted); }
.text-sm { font-size: 0.8rem; }
.mt-1 { margin-top: 0.5rem; }
.mt-2 { margin-top: 1rem; }
.mb-1 { margin-bottom: 0.5rem; }
.mb-2 { margin-bottom: 1rem; }
.flex { display: flex; }
.gap-1 { gap: 0.5rem; }
.gap-2 { gap: 1rem; }

/* RESPONSIVE */
@media (max-width: 1100px) {
    .chat-list-panel {
        width: 310px;
        min-width: 250px;
    }
}

@media (max-width: 900px) {
    .chat-list-panel {
        width: 280px;
        min-width: 230px;
    }

    .msg-bubble {
        max-width: 78%;
    }

    .page-header-mobile-stack {
        align-items: stretch !important;
    }
}

@media (max-width: 768px) {
    body {
        overflow: hidden;
    }

    .app-sidebar {
        width: 56px;
    }

    .chat-layout.mobile-show-list .chat-view {
        display: none;
    }

    .chat-layout.mobile-show-chat .chat-list-panel {
        display: none;
    }

    .chat-list-panel {
        width: 100%;
        min-width: 0;
        border-right: none;
    }

    .chat-view {
        width: 100%;
        min-width: 0;
    }

    .mobile-only,
    .chat-back-btn {
        display: inline-flex;
    }

    .chat-view-header {
        padding: 0.65rem 0.8rem;
        gap: 0.6rem;
    }

    .chat-contact-btn {
        font-size: 0.92rem;
    }

    .chat-view-header-status {
        font-size: 0.72rem;
    }

    .chat-item {
        padding: 0.8rem 0.9rem;
    }

    .chat-item-name {
        font-size: 0.92rem;
    }

    .chat-item-preview {
        font-size: 0.8rem;
    }

    .chat-messages {
        padding: 0.85rem 0.7rem;
    }

    .msg-bubble {
        max-width: 88%;
        font-size: 0.95rem;
        padding: 0.55rem 0.7rem;
        border-radius: 12px;
    }

    .chat-input-bar {
        padding: 0.55rem 0.65rem;
        gap: 0.45rem;
    }

    .chat-input-bar textarea {
        font-size: 16px;
        min-height: 42px;
    }

    .chat-send-btn {
        width: 42px;
        height: 42px;
    }

    .modal-overlay {
        padding: 0;
        align-items: flex-end;
    }

    .modal,
    .contact-modal {
        width: 100%;
        max-width: 100%;
        max-height: 92vh;
        border-radius: 20px 20px 0 0;
    }

    .contact-details-grid {
        grid-template-columns: 1fr;
    }

    .emoji-picker-popover {
        left: 50%;
        transform: translateX(-50%);
    }

    .emoji-picker-popover emoji-picker {
        width: calc(100vw - 24px);
        max-width: 360px;
    }

    .toast-container {
        top: auto;
        bottom: 1rem;
        right: 0.75rem;
        left: 0.75rem;
    }

    .toast {
        min-width: 0;
        width: 100%;
        max-width: none;
    }
}

@media (max-width: 480px) {
    .login-container {
        padding: 1rem;
    }

    .login-card {
        padding: 1.25rem;
    }

    .form-row {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }

    .page-title {
        font-size: 1.2rem;
    }

    .page-subtitle {
        font-size: 0.85rem;
    }

    .card-body {
        padding: 1rem;
    }

    .chat-list-header {
        padding: 0.8rem;
    }

    .chat-list-search {
        padding: 0.5rem 0.6rem;
    }

    .chat-item {
        padding: 0.75rem 0.8rem;
    }

    .chat-messages {
        padding: 0.75rem 0.55rem;
    }

    .msg-bubble {
        max-width: 92%;
        font-size: 0.92rem;
    }

    .contact-modal-header-info h3,
    .contact-modal-header-info h2 {
        font-size: 1rem !important;
    }

    .emoji-trigger-btn {
        font-size: 18px;
        padding: 5px 6px;
    }
}

@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .chat-input-bar {
        padding-bottom: calc(0.55rem + env(safe-area-inset-bottom));
    }
}

/* ============================================================
   CONTATOS — Aba de Contatos v2
   ============================================================ */

.contacts-page {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

.contacts-page-header {
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.contacts-search-bar {
    padding: 0.75rem 1.25rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.contacts-search-input-wrap {
    position: relative;
    display: flex;
    align-items: center;
}

.contacts-search-icon {
    position: absolute;
    left: 0.75rem;
    width: 15px;
    height: 15px;
    color: var(--text-muted);
    pointer-events: none;
    flex-shrink: 0;
}

.contacts-search-input {
    width: 100%;
    padding: 0.5rem 2.25rem 0.5rem 2.1rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.88rem;
    color: var(--text-primary);
    transition: border-color 0.15s, box-shadow 0.15s;
}

.contacts-search-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.08);
}

.contacts-search-input::placeholder {
    color: var(--text-muted);
}

.contacts-search-clear {
    position: absolute;
    right: 0.5rem;
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.8rem;
    padding: 0.2rem 0.35rem;
    border-radius: 4px;
    line-height: 1;
    transition: color 0.15s;
}

.contacts-search-clear:hover {
    color: var(--text-primary);
}

/* LISTA */
.contacts-list {
    flex: 1;
    overflow-y: auto;
}

/* CARD */
.contact-card {
    display: flex;
    align-items: center;
    gap: 0.875rem;
    padding: 0.75rem 1.25rem;
    cursor: pointer;
    transition: background 0.1s;
    border-bottom: 1px solid var(--border);
}

.contact-card:hover {
    background: var(--bg-hover);
}

.contact-card:last-child {
    border-bottom: none;
}

.contact-card-info {
    flex: 1;
    min-width: 0;
}

.contact-card-name {
    font-weight: 600;
    font-size: 0.92rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.contact-card-phone {
    font-size: 0.81rem;
    color: var(--text-muted);
    margin-top: 0.1rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.contact-card-connection {
    opacity: 0.65;
}

.contact-card-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.3rem;
    flex-shrink: 0;
}

.contact-card-date {
    font-size: 0.73rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.contact-card-actions {
    display: flex;
    align-items: center;
    gap: 0.3rem;
}

.contact-card-action-btn {
    border: none;
    border-radius: 6px;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: opacity 0.15s, transform 0.1s, background 0.15s;
    flex-shrink: 0;
}

.contact-card-action-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.contact-card-action-btn svg {
    width: 13px;
    height: 13px;
}

.contact-card-chat-btn {
    background: var(--primary);
    opacity: 0.82;
}

.contact-card-chat-btn:hover {
    opacity: 1;
    transform: scale(1.08);
}

.contact-card-chat-btn svg {
    stroke: #fff;
}

.contact-card-delete-btn {
    background: rgba(239, 68, 68, 0.1);
    color: #ef4444;
}

.contact-card-delete-btn:hover {
    background: rgba(239, 68, 68, 0.2);
    transform: scale(1.08);
}

.contact-card-delete-btn svg {
    stroke: #ef4444;
}

/* VAZIO */
.contacts-empty {
    text-align: center;
    padding: 3rem 1.5rem;
    color: var(--text-primary);
}

/* PAGINAÇÃO */
.contacts-pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    padding: 0.875rem 1rem;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.contacts-pagination-info {
    font-size: 0.83rem;
    color: var(--text-muted);
}

/* RESPONSIVO CONTATOS */
@media (max-width: 768px) {
    .contacts-page-header {
        padding: 1rem;
    }

    .contacts-search-bar {
        padding: 0.625rem 1rem;
    }

    .contact-card {
        padding: 0.75rem 1rem;
        gap: 0.75rem;
    }

    .contact-card-date {
        display: none;
    }
}