/* ==================== VARIABLES ==================== */
html {
    /* Backgrounds */
    --bg-main: #ffffff;
    --bg-secondary: #f8fafc;
    --bg-tertiary: #f1f5f9;
    --bg-hover: #e2e8f0;
    --bg-active: #ede9fe;
    --bg-active-light: #f5f3ff;
    --bg-input-wrapper: #fafbfc;
    --bg-auth-subtitle: #f3f4f6;
    
    /* Text */
    --text-primary: #334155;
    --text-secondary: #94a3b8;
    --text-dark: #1e293b;
    --text-muted: #64748b;
    --text-white: #ffffff;
    
    /* Borders */
    --border-color: #e2e8f0;
    --border-color-dark: #cbd5e1;
    --border-auth: #e5e7eb;
    
    /* Accents */
    --accent-primary: #6366f1;
    --accent-secondary: #8b5cf6;
    --accent-gradient: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    --accent-blockquote: #c7d2fe;
    
    /* Status Colors */
    --success-color: #16a34a;
    --success-bg: #f0fdf4;
    --success-border: #4ade80;
    --danger-color: #ef4444;
    --danger-bg: #fee2e2;
}

/* Dark Mode Overrides */
html.dark {
    --bg-main: #0f172a;
    --bg-secondary: #1e293b;
    --bg-tertiary: #334155;
    --bg-hover: #475569;
    --bg-active: #312e81;
    --bg-active-light: #3730a3;
    --bg-input-wrapper: #0f172a;
    --bg-auth-subtitle: #1e293b;
    
    --text-primary: #f8fafc;
    --text-secondary: #cbd5e1;
    --text-dark: #f1f5f9;
    --text-muted: #94a3b8;
    --text-white: #ffffff;
    
    --border-color: #334155;
    --border-color-dark: #475569;
    --border-auth: #334155;
    
    --accent-primary: #818cf8;
    --accent-secondary: #a78bfa;
    --accent-blockquote: #4f46e5;
    
    --success-color: #4ade80;
    --success-bg: #14532d;
    --success-border: #22c55e;
    --danger-color: #f87171;
    --danger-bg: #7f1d1d;
}

/* ==================== STYLES ==================== */

.chat-fab {
    position: fixed;
    bottom: 30px;
    right: 30px;
    width: 64px;
    height: 64px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 9999;
}

.chat-fab .rafikey-icon {
    width: 64px;
    height: 64px;
}

.chat-fab:focus, .chat-window button:focus {
    outline: none;
}

.chat-fab:hover {
    transform: scale(1.1);
}

/* Chat Window */
.chat-window {
    font-family: 'Inter', sans-serif;
    position: fixed;
    bottom: 110px;
    right: 30px;
    width: 480px;
    height: 600px;
    border-radius: 20px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: 9998;
    transform: scale(0) translateY(20px);
    opacity: 0;
    line-height: normal;
    transform-origin: bottom right;
    transition: all 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

.chat-window.open {
    transform: scale(1) translateY(0);
    opacity: 1;
}

/* Chat Header */
.chat-header {
    background: var(--accent-gradient);
    padding: 0px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 60px;
}

.chat-header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.sidebar-toggle-btn {
    width: 32px;
    height: 32px;
    border-radius: 25px;
    border: none;
    background: rgba(255, 255, 255, 0.15);
    cursor: pointer;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    display: none;
}

.sidebar-toggle-btn.visible {
    display: flex;
}

.sidebar-toggle-btn:hover {
    background: rgba(255, 255, 255, 0.25);
}

.sidebar-toggle-btn svg {
    width: 16px;
    height: 16px;
    fill: var(--text-white);
}

.support-avatar {
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-header-info h3 {
    color: var(--text-white);
    font-size: 14px;
    font-weight: 600;
    margin: 0;
    border: 0;
    font-family: 'Inter', sans-serif;
    text-transform: none;
    margin-bottom: 2px;
}

.chat-header-info .status {
    color: rgba(255, 255, 255, 0.8);
    font-size: 12px;
    display: flex;
    align-items: center;
    gap: 5px;
}

.status-dot {
    width: 7px;
    height: 7px;
    background: var(--success-border);
    border-radius: 50%;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.chat-header-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.header-btn {
    width: 32px;
    height: 32px;
    border-radius: 25px;
    border: none;
    background: rgba(255, 255, 255, 0.15);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.header-btn:hover {
    background: rgba(255, 255, 255, 0.25);
}

.header-btn svg {
    width: 16px;
    height: 16px;
    fill: var(--text-white);
}

.rafikey-icon {
    display: inline-block;
    width: 48px;
    height: 48px;
    background-repeat: no-repeat;
    background-size: 100% 100%;
    background-image: url("./bot.svg");
}

/* Chat Container (sidebar + main) */
.chat-container {
    flex: 1;
    display: flex;
    overflow: hidden;
    background: var(--bg-main);
}

/* Sidebar */
.chat-sidebar {
    width: 240px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    overflow: hidden;
    transform: translateX(-240px);
    position: fixed;
    z-index: 2;
    height: calc(100% - 60px);
}

.chat-sidebar.open {
    transform: translateX(0px);
}

.sidebar-header {
    padding: 14px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.new-chat-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 10px;
    border-radius: 10px;
    border: 2px dashed var(--border-color-dark);
    background: var(--bg-main);
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    color: var(--accent-primary);
    font-family: 'Inter', sans-serif;
    transition: all 0.2s;
}

.new-chat-btn:hover {
    border-color: var(--accent-primary);
    background: var(--bg-active-light);
}

.new-chat-btn svg {
    width: 16px;
    height: 16px;
    fill: var(--accent-primary);
}

.chat-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.chat-list::-webkit-scrollbar {
    width: 3px;
}

.chat-list::-webkit-scrollbar-thumb {
    background: var(--border-color-dark);
    border-radius: 3px;
}

.chat-list-item {
    padding: 10px 12px;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.15s;
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
    position: relative;
    background: var(--bg-tertiary);
}

.chat-list-item:hover {
    background: var(--bg-hover);
}

.chat-list-item.active {
    background: var(--bg-active);
}

.chat-list-item-info {
    flex: 1;
    overflow: hidden;
}

.chat-list-item-title {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-list-item-preview {
    font-size: 11px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 2px;
}

.chat-list-item-date {
    font-size: 10px;
    color: var(--text-secondary);
    white-space: nowrap;
}

.chat-list-item-delete {
    width: 24px;
    height: 24px;
    border-radius: 6px;
    border: none;
    background: transparent;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
    flex-shrink: 0;
}

.chat-list-item:hover .chat-list-item-delete {
    display: flex;
}

.chat-list-item-delete:hover {
    background: var(--danger-bg);
}

.chat-list-item-delete svg {
    width: 14px;
    height: 14px;
    fill: var(--danger-color);
}

/* Main Chat Area */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 480px;
}

/* Chat Body */
.chat-body {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.chat-body::-webkit-scrollbar {
    width: 4px;
}

.chat-body::-webkit-scrollbar-track {
    background: transparent;
}

.chat-body::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

.message {
    display: flex;
    gap: 10px;
    max-width: 85%;
    animation: msgIn 0.3s ease;
}

@keyframes msgIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.msg-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
    font-weight: 700;
    flex-shrink: 0;
}

.msg-avatar img {
    width: 100%;
    height: 100%;
}

.message.user .msg-avatar {
    border-radius: 50%;
    overflow: hidden;
}

.msg-bubble {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.5;
    position: relative;
}

.msg-bubble hr {
    border-color: var(--text-white);
    border-top-color: var(--border-color-dark);
    padding-top: 5px;
    margin-top: 5px;
}

.message.bot .msg-bubble {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
}

.message.user .msg-bubble {
    background: var(--accent-gradient);
    color: var(--text-white);
    border-bottom-right-radius: 4px;
}

.msg-time {
    font-size: 10px;
    opacity: 0.6;
    margin-top: 4px;
}

.message.user .msg-time {
    text-align: right;
}

.message.bot .msg-bubble:hover .msg-actions {
    opacity: 1;
}

/* Copy button (bot messages only) */
.msg-actions {
    margin-top: 6px;
    opacity: 0;
    transition: 0.5s all;
    position: absolute;
    right: 8px;
    bottom: 8px;
}

.copy-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 4px 10px;
    border: 1px solid var(--border-color);
    background: var(--bg-main);
    border-radius: 8px;
    cursor: pointer;
    font-size: 11px;
    font-weight: 500;
    color: var(--text-muted);
    font-family: 'Inter', sans-serif;
    line-height: normal;
    transition: all 0.2s;
}

.copy-btn:hover {
    border-color: var(--accent-primary);
    color: var(--accent-primary);
    background: var(--bg-active-light);
}

.copy-btn.copied {
    border-color: var(--success-border);
    color: var(--success-color);
    background: var(--success-bg);
}

.copy-btn svg {
    width: 13px;
    height: 13px;
    fill: currentColor;
}

/* Markdown content inside bot bubbles */
.msg-bubble {
    overflow-wrap: break-word;
}

.msg-bubble > *:first-child {
    margin-top: 0;
}

.msg-bubble > *:last-child {
    margin-bottom: 0;
}

.msg-bubble p {
    margin: 0;
    font-size: 14px;
}

.msg-bubble p + p {
    margin: 0 0 8px;
}

.msg-bubble h1,
.msg-bubble h2,
.msg-bubble h3,
.msg-bubble h4,
.msg-bubble h5,
.msg-bubble h6 {
    margin: 10px 0 6px;
    font-size: 15px;
    font-weight: 600;
    color: var(--text-dark);
    line-height: 1.3;
    border: none;
    background: none;
    text-transform: none;
}

.msg-bubble h1 { font-size: 17px; }
.msg-bubble h2 { font-size: 16px; }

.msg-bubble ul,
.msg-bubble ol {
    margin: 0 0 8px;
    padding-left: 20px;
}

.msg-bubble li {
    margin: 2px 0;
}

.msg-bubble a {
    color: var(--accent-primary);
    text-decoration: none;
}

.msg-bubble a:hover {
    text-decoration: underline;
}

.msg-bubble code {
    background: var(--bg-hover);
    padding: 2px 5px;
    border-radius: 4px;
    font-size: 12.5px;
    font-family: 'Consolas', 'Courier New', monospace;
}

.msg-bubble pre {
    background: #1e293b; /* دائماً داكن للأكواد البرمجية */
    color: #e2e8f0;
    padding: 10px 12px;
    border-radius: 10px;
    overflow-x: auto;
    margin: 8px 0;
}

.msg-bubble pre code {
    background: transparent;
    color: inherit;
    padding: 0;
    font-size: 12.5px;
    line-height: 1.5;
}

.msg-bubble blockquote {
    border-left: 3px solid var(--accent-blockquote);
    margin: 8px 0;
    padding: 2px 12px;
    color: var(--text-muted);
}

.msg-bubble table {
    border-collapse: collapse;
    margin: 8px 0;
    width: 100%;
    font-size: 13px;
}

.msg-bubble th,
.msg-bubble td {
    border: 1px solid var(--border-color);
    padding: 6px 10px;
    text-align: left;
}

.msg-bubble th {
    background: var(--bg-tertiary);
    font-weight: 600;
}

.msg-bubble img {
    max-width: 100%;
    border-radius: 8px;
}

.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
    background: var(--bg-tertiary);
    border-radius: 16px;
    border-bottom-left-radius: 4px;
    width: fit-content;
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    background: var(--text-secondary);
    border-radius: 50%;
    animation: typing 1.4s infinite;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-6px); }
}

/* Chat Footer */
.chat-footer {
    padding: 14px 16px;
    border-top: 1px solid var(--bg-tertiary);
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--bg-main);
}

.chat-input-wrapper {
    flex: 1;
    position: relative;
}

.chat-input {
    width: 100%;
    padding: 12px 16px;
    border: 2px solid var(--border-color);
    border-radius: 12px;
    font-size: 14px;
    font-family: 'Inter', sans-serif;
    outline: none;
    transition: border-color 0.2s;
    line-height: normal;
    resize: none;
    background: var(--bg-main);
    color: var(--text-primary);
}

.chat-input:focus {
    border-color: var(--accent-primary);
    box-shadow: none;
}

.chat-input::placeholder {
    color: var(--text-secondary);
}

.send-btn {
    width: 40px;
    height: 40px;
    border-radius: 10px;
    border: none;
    background: var(--accent-gradient);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.send-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

.send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.send-btn svg {
    width: 18px;
    height: 18px;
    fill: var(--text-white);
}

/* Feedback Section */
.feedback-section {
    padding: 14px 16px;
    border-top: 1px solid var(--bg-tertiary);
    background: var(--bg-input-wrapper);
}

.feedback-section p {
    font-size: 13px;
    color: var(--text-muted);
    margin-bottom: 10px;
    text-align: center;
}

.feedback-emojis {
    display: flex;
    justify-content: center;
    gap: 12px;
}

.emoji-btn {
    width: 44px;
    height: 44px;
    border-radius: 12px;
    border: 2px solid var(--border-color);
    background: var(--bg-main);
    cursor: pointer;
    font-size: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.emoji-btn:hover {
    transform: scale(1.15);
    border-color: var(--accent-primary);
    background: var(--bg-active-light);
}

.emoji-btn.selected {
    border-color: var(--accent-primary);
    background: var(--bg-active);
    transform: scale(1.1);
}

.feedback-thanks {
    text-align: center;
    color: var(--accent-primary);
    font-size: 13px;
    font-weight: 500;
    display: none;
}

.feedback-thanks.show {
    display: block;
    animation: msgIn 0.3s ease;
}

/* Auth Screen */
.auth-screen {
    position: absolute;
    inset: 0;
    background: var(--bg-main);
    z-index: 10;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0 28px;
    border-radius: 20px;
}

.auth-screen.hidden {
    display: none;
}

.auth-terms {
    text-decoration: underline;
    color: var(--accent-primary);
}

.auth-logo {
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
}

.auth-logo .rafikey-icon {
    width: 128px;
    height: 128px;
}

.auth-screen h2 {
    font-size: 20px;
    color: var(--text-dark);
    margin-bottom: 6px;
    margin-top: 0;
}

.auth-screen .auth-subtitle {
    font-size: 14px;
    margin-bottom: 12px;
    margin-top: 12px;
    border: 1px solid var(--border-auth);
    background-color: var(--bg-auth-subtitle);
    color: var(--text-primary);
    padding: 14px;
    line-height: 1.5;
    border-radius: 8px;
}

.auth-form {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.auth-submit-btn {
    width: 100%;
    padding: 12px;
    border-radius: 12px;
    border: none;
    background: var(--accent-gradient);
    color: var(--text-white);
    font-size: 14px;
    font-weight: 600;
    font-family: 'Inter', sans-serif;
    cursor: pointer;
    transition: all 0.2s;
    margin-top: 4px;
}

.auth-submit-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 20px rgba(99, 102, 241, 0.4);
}

.user-chat-input {
    width: 100%;
    box-sizing: border-box;
}

.error-username {
    font-size: 14px;
    color: #ef4444;
    padding: 5px 0;
}

/* Empty state */
.empty-chats {
    text-align: center;
    padding: 20px;
    color: var(--text-secondary);
    font-size: 12px;
}

.empty-chats svg {
    width: 32px;
    height: 32px;
    fill: var(--border-color-dark);
    margin-bottom: 8px;
}

/* Responsive */
@media (max-width: 480px) {
    .chat-window {
        width: calc(100vw - 20px);
        right: 10px;
        bottom: 100px;
        height: 75vh;
    }

    .chat-fab {
        bottom: 20px;
        right: 20px;
    }

    .chat-sidebar.open {
        width: 180px;
        min-width: 180px;
    }
}