/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --primary: #6C63FF;
    --primary-dark: #5A52E0;
    --primary-light: #EDE7FF;
    --secondary: #4ECDC4;
    --secondary-light: #E7F9F6;
    --bg: #F8F9FA;
    --card: #FFFFFF;
    --card-shadow: 0 2px 12px rgba(0,0,0,0.08);
    --text: #2D3436;
    --muted: #636E72;
    --error: #FF6B6B;
    --success: #00B894;
    --customer-bubble: #F1F0F0;
    --ai-bubble: #EDE7FF;
    --owner-bubble: #E7F9F6;
    --border: #E0E0E0;
    --radius-card: 12px;
    --radius-btn: 8px;
    --radius-bubble: 20px;
    --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}

html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
}

[x-cloak] { display: none !important; }

/* ===== TYPOGRAPHY ===== */
h1 { font-size: 1.75rem; font-weight: 700; }
h2 { font-size: 1.25rem; font-weight: 600; }
h3 { font-size: 1.1rem; font-weight: 600; }

/* ===== BUTTONS ===== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.625rem 1.25rem;
    border: none;
    border-radius: var(--radius-btn);
    font-family: var(--font);
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    text-decoration: none;
}
.btn:disabled { opacity: 0.6; cursor: not-allowed; }
.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: #fff;
    box-shadow: 0 2px 8px rgba(108,99,255,0.3);
}
.btn-primary:hover:not(:disabled) {
    box-shadow: 0 4px 16px rgba(108,99,255,0.4);
    transform: translateY(-1px);
}
.btn-secondary {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
}
.btn-secondary:hover:not(:disabled) { background: #eee; }
.btn-danger {
    background: var(--error);
    color: #fff;
}
.btn-danger:hover:not(:disabled) { background: #e55; }
.btn-sm { padding: 0.375rem 0.75rem; font-size: 0.8125rem; }
.btn-block { width: 100%; }
.btn-icon {
    width: 32px; height: 32px; padding: 0;
    border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    font-size: 0.75rem;
}

/* ===== FORM CONTROLS ===== */
.form-group { margin-bottom: 1rem; }
.form-label {
    display: block;
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--text);
    margin-bottom: 0.375rem;
}
.form-hint {
    font-size: 0.75rem;
    color: var(--muted);
    margin-top: 0.25rem;
}
.form-input, .form-textarea, .form-select {
    width: 100%;
    padding: 0.625rem 0.875rem;
    border: 1.5px solid var(--border);
    border-radius: var(--radius-btn);
    font-family: var(--font);
    font-size: 0.875rem;
    color: var(--text);
    background: #fff;
    transition: border-color 0.2s, box-shadow 0.2s;
}
.form-input:focus, .form-textarea:focus, .form-select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(108,99,255,0.1);
}
.form-textarea { resize: vertical; min-height: 80px; }
.form-input::placeholder, .form-textarea::placeholder { color: #B2BEC3; }

/* Checkbox / Radio */
.form-check {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-bottom: 0.375rem;
    cursor: pointer;
    font-size: 0.875rem;
}
.form-check input[type="checkbox"],
.form-check input[type="radio"] {
    width: 18px; height: 18px;
    accent-color: var(--primary);
    cursor: pointer;
    flex-shrink: 0;
}

/* ===== CARD ===== */
.card {
    background: var(--card);
    border-radius: var(--radius-card);
    box-shadow: var(--card-shadow);
    padding: 1.5rem;
}
.card-title {
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* ===== HEADER ===== */
.header {
    background: var(--primary);
    padding: 0 1.5rem;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: 0 2px 8px rgba(108,99,255,0.25);
}
.header-logo {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    text-decoration: none;
    color: #fff;
}
.header-logo img { width: 32px; height: 32px; border-radius: 6px; }
.header-logo span { font-size: 1.125rem; font-weight: 700; letter-spacing: -0.025em; }
.header-nav { display: flex; align-items: center; gap: 0.5rem; }
.header-nav a {
    color: rgba(255,255,255,0.85);
    text-decoration: none;
    font-size: 0.875rem;
    font-weight: 500;
    padding: 0.375rem 0.75rem;
    border-radius: var(--radius-btn);
    transition: all 0.2s;
}
.header-nav a:hover, .header-nav a.active {
    color: #fff;
    background: rgba(255,255,255,0.15);
}
.header-nav .btn-logout {
    color: rgba(255,255,255,0.85);
    background: rgba(255,255,255,0.1);
    border: 1px solid rgba(255,255,255,0.2);
    margin-left: 0.25rem;
}
.header-nav .btn-logout:hover { background: rgba(255,255,255,0.2); }

/* ===== LOGIN PAGE ===== */
.login-wrapper {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    background: linear-gradient(135deg, #F8F9FA 0%, #EDE7FF 100%);
}
.login-card {
    background: var(--card);
    border-radius: var(--radius-card);
    box-shadow: 0 8px 32px rgba(108,99,255,0.12);
    padding: 2.5rem;
    width: 100%;
    max-width: 400px;
}
.login-logo {
    text-align: center;
    margin-bottom: 1.5rem;
}
.login-logo img { margin-bottom: 0.75rem; border-radius: 12px; }
.login-logo h1 { font-size: 1.5rem; }
.login-logo p { font-size: 0.875rem; color: var(--muted); margin-top: 0.25rem; }
.login-error {
    background: #FFF5F5;
    color: var(--error);
    padding: 0.625rem 0.875rem;
    border-radius: var(--radius-btn);
    font-size: 0.8125rem;
    margin-bottom: 1rem;
    border: 1px solid rgba(255,107,107,0.2);
}

/* ===== SETUP PAGE ===== */
.setup-container {
    max-width: 760px;
    margin: 0 auto;
    padding: 1.5rem 1rem 3rem;
}
.setup-container .card { margin-bottom: 1.25rem; }
.tag-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.5rem;
    margin-bottom: 0.5rem;
}
.tag {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 0.25rem 0.75rem;
    font-size: 0.8125rem;
}
.tag-remove {
    cursor: pointer;
    color: var(--muted);
    font-size: 0.875rem;
    line-height: 1;
    border: none;
    background: none;
    padding: 0;
}
.tag-remove:hover { color: var(--error); }
.qa-pair {
    background: var(--bg);
    border-radius: var(--radius-btn);
    padding: 1rem;
    margin-bottom: 0.75rem;
    position: relative;
}
.qa-pair .btn-remove-qa {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
}
.add-row {
    display: flex;
    gap: 0.5rem;
    margin-top: 0.5rem;
}
.add-row .form-input { flex: 1; }
.webhook-url-box {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--bg);
    border: 1px dashed var(--border);
    border-radius: var(--radius-btn);
    padding: 0.625rem 0.875rem;
    font-size: 0.8125rem;
    font-family: 'Courier New', monospace;
    word-break: break-all;
}
.webhook-url-box span { flex: 1; }
.webhook-url-box .copy-btn {
    flex-shrink: 0;
    cursor: pointer;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--radius-btn);
    padding: 0.375rem 0.75rem;
    font-size: 0.75rem;
    font-weight: 600;
    font-family: var(--font);
    transition: all 0.2s;
}
.webhook-url-box .copy-btn:hover { background: var(--primary-dark); }
.test-reply-bubble {
    background: var(--ai-bubble);
    border-radius: var(--radius-bubble);
    padding: 0.875rem 1.125rem;
    margin-top: 0.75rem;
    font-size: 0.875rem;
    white-space: pre-wrap;
}
.test-input-row {
    display: flex;
    gap: 0.5rem;
}
.test-input-row .form-input { flex: 1; }
.save-status {
    text-align: center;
    font-size: 0.8125rem;
    margin-top: 0.75rem;
}
.save-status.success { color: var(--success); }
.save-status.error { color: var(--error); }

/* ===== MONITOR PAGE ===== */
.monitor-layout {
    display: flex;
    height: calc(100vh - 56px);
    overflow: hidden;
}
.contacts-panel {
    width: 340px;
    min-width: 280px;
    background: var(--card);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.contacts-header {
    padding: 1rem;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
    font-size: 0.9375rem;
    color: var(--text);
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.contacts-search {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
}
.contacts-search .form-input {
    padding: 0.5rem 0.75rem;
    font-size: 0.8125rem;
    border-radius: 999px;
    background: var(--bg);
}
.contacts-list {
    flex: 1;
    overflow-y: auto;
}
.contact-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.875rem 1rem;
    cursor: pointer;
    transition: background 0.15s;
    border-bottom: 1px solid #f0f0f0;
}
.contact-item:hover { background: var(--bg); }
.contact-item.active { background: var(--ai-bubble); }
.contact-avatar {
    width: 42px; height: 42px;
    border-radius: 50%;
    background: var(--primary-light);
    color: var(--primary);
    display: flex; align-items: center; justify-content: center;
    font-weight: 700;
    font-size: 1rem;
    flex-shrink: 0;
}
.contact-info { flex: 1; min-width: 0; }
.contact-name {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.contact-preview {
    font-size: 0.75rem;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 0.125rem;
}
.contact-time {
    font-size: 0.6875rem;
    color: var(--muted);
    white-space: nowrap;
    flex-shrink: 0;
    align-self: flex-start;
    margin-top: 0.125rem;
}

/* Chat Area */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    overflow: hidden;
}
.chat-header {
    background: var(--card);
    border-bottom: 1px solid var(--border);
    padding: 0.875rem 1.25rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}
.chat-header-name { font-weight: 600; font-size: 0.9375rem; }
.chat-header-phone { font-size: 0.75rem; color: var(--muted); }
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
}
.chat-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--muted);
    font-size: 0.9375rem;
}
.chat-empty-inner { text-align: center; }
.chat-empty-inner svg { width: 64px; height: 64px; opacity: 0.3; margin-bottom: 1rem; display: block; margin-left: auto; margin-right: auto; }

.msg-row { display: flex; margin-bottom: 0.125rem; }
.msg-row.left { justify-content: flex-start; }
.msg-row.right { justify-content: flex-end; }
.msg-bubble {
    max-width: 70%;
    border-radius: var(--radius-bubble);
    padding: 0.75rem 1rem;
    font-size: 0.875rem;
    line-height: 1.5;
    word-wrap: break-word;
}
.msg-bubble.customer {
    background: var(--customer-bubble);
    border-bottom-left-radius: 4px;
}
.msg-bubble.ai {
    background: var(--ai-bubble);
    border-bottom-right-radius: 4px;
}
.msg-bubble.owner {
    background: var(--owner-bubble);
    border-bottom-right-radius: 4px;
}
.msg-label {
    font-size: 0.6875rem;
    font-weight: 600;
    margin-bottom: 0.25rem;
    display: flex;
    align-items: center;
    gap: 0.25rem;
}
.msg-label.ai { color: var(--primary); }
.msg-label.owner { color: #00B894; }
.msg-time {
    font-size: 0.6875rem;
    color: var(--muted);
    margin-bottom: 0.375rem;
    padding: 0 0.25rem;
}

/* Reply Bar */
.reply-bar {
    background: var(--card);
    border-top: 1px solid var(--border);
    padding: 0.75rem 1rem;
    display: flex;
    gap: 0.5rem;
    align-items: center;
}
.reply-bar .form-input {
    flex: 1;
    border-radius: 999px;
    padding: 0.625rem 1rem;
}
.reply-bar .btn-send {
    width: 42px; height: 42px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}
.reply-bar .btn-send:hover { transform: scale(1.05); box-shadow: 0 2px 8px rgba(108,99,255,0.3); }
.reply-bar .btn-send:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

/* ===== MOBILE BACK BUTTON ===== */
.mobile-back {
    display: none;
    background: none;
    border: none;
    color: var(--primary);
    font-size: 1.25rem;
    cursor: pointer;
    padding: 0.25rem;
    margin-right: 0.25rem;
}

/* ===== LOADING SPINNER ===== */
.spinner {
    display: inline-block;
    width: 18px; height: 18px;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}
.spinner-dark {
    border-color: rgba(108,99,255,0.2);
    border-top-color: var(--primary);
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Fade in */
.fade-in { animation: fadeIn 0.3s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ===== TOAST ===== */
.toast-container {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}
.toast {
    background: var(--card);
    border-radius: var(--radius-btn);
    box-shadow: 0 4px 16px rgba(0,0,0,0.15);
    padding: 0.75rem 1.25rem;
    font-size: 0.8125rem;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    animation: slideIn 0.3s ease;
    max-width: 320px;
}
.toast.success { border-left: 3px solid var(--success); }
.toast.error { border-left: 3px solid var(--error); }
@keyframes slideIn { from { opacity: 0; transform: translateX(100%); } to { opacity: 1; transform: translateX(0); } }

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
    .contacts-panel {
        width: 100%;
        position: absolute;
        left: 0; top: 0; bottom: 0;
        z-index: 10;
    }
    .monitor-layout { position: relative; }
    .contacts-panel.hidden-mobile { display: none; }
    .chat-area.hidden-mobile { display: none; }
    .mobile-back { display: block; }
    .header { padding: 0 1rem; }
    .header-nav { gap: 0.25rem; }
    .header-nav a { font-size: 0.8125rem; padding: 0.25rem 0.5rem; }
    .setup-container { padding: 1rem 0.75rem 2rem; }
    .card { padding: 1.25rem; }
    .msg-bubble { max-width: 85%; }
    .login-card { padding: 1.75rem; }
}

/* Scrollbar styling */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #D0D0D0; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #B0B0B0; }
