:root {
    /* Light Mode Tokens */
    --bg-primary: #f8f9fc;
    --bg-secondary: #ffffff;
    --text-primary: #1f2937;
    --text-secondary: #6b7280;
    --accent: #6d5dfc;
    --accent-hover: #5b4ce3;
    --border-color: #e5e7eb;
    --glass-bg: rgba(255, 255, 255, 0.8);
    --glass-border: rgba(0, 0, 0, 0.05);
    --user-bubble: linear-gradient(135deg, #6d5dfc 0%, #5b4ce3 100%);
    --assistant-bubble: #f3f4f6;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

[data-theme="dark"] {
    /* Dark Mode Tokens */
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --accent: #8b5cf6;
    --accent-hover: #7c3aed;
    --border-color: #334155;
    --glass-bg: rgba(15, 23, 42, 0.8);
    --glass-border: rgba(255, 255, 255, 0.08);
    --user-bubble: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%);
    --assistant-bubble: #1e293b;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
    --shadow-md: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
}

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

html {
    font-size: 16px;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    transition: background-color 0.3s, color 0.3s;
}

/* 1️⃣ Layout System */
.container {
    width: 95%;
    max-width: 850px; /* Reduced from 1000px to make it tighter */
    height: 85vh; /* Slightly reduced height */
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-md);
    position: relative;
    overflow: hidden;
}

/* 6️⃣ Header Section */
header {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.brand h1 {
    font-size: 1.25rem;
    font-weight: 700;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.cognitive-stats {
    display: flex;
    gap: 0.5rem;
}

.stat-badge {
    padding: 0.25rem 0.75rem;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 99px;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.stat-badge span.count {
    color: var(--accent);
}

#theme-toggle {
    background: none;
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 0.5rem;
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

#theme-toggle:hover {
    background: var(--bg-primary);
    border-color: var(--accent);
}

/* 3️⃣ Chat Area & Bubbles */
#chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    scroll-behavior: smooth;
}

#chat-container::-webkit-scrollbar {
    width: 6px;
}

#chat-container::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 10px;
}

.message {
    max-width: 80%;
    width: fit-content; /* يضمن أن الفقاعة تأخذ حجم النص فقط */
    padding: 0.8rem 1.1rem;
    border-radius: 18px;
    font-size: 0.95rem;
    line-height: 1.5;
    position: relative;
    animation: fadeIn 0.3s ease-out;
    word-wrap: break-word;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.user {
    align-self: flex-end;
    background: var(--user-bubble);
    color: white;
    border-bottom-right-radius: 4px;
}

.assistant {
    align-self: flex-start;
    background: var(--assistant-bubble);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
    border: 1px solid var(--border-color);
}

/* Typing Indicator */
.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 10px 15px;
    background: var(--assistant-bubble);
    border-radius: 12px;
    width: fit-content;
    border: 1px solid var(--border-color);
}

.typing-indicator span {
    width: 6px;
    height: 6px;
    background: var(--accent);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out;
}

@keyframes bounce {
    0%, 80%, 100% { transform: translateY(0); }
    40% { transform: translateY(-5px); }
}

/* Input Area */
.input-area {
    padding: 1.25rem 1.5rem;
    border-top: 1px solid var(--border-color);
    background: var(--bg-secondary);
}

.action-buttons {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.btn-secondary {
    flex: 1;
    padding: 0.6rem;
    font-size: 0.8rem;
    font-weight: 600;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition: all 0.2s;
}

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

.input-group {
    display: flex;
    gap: 0.75rem;
    background: var(--bg-primary);
    padding: 0.5rem;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    transition: border-color 0.2s;
}

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

input[type="text"] {
    flex: 1;
    background: transparent;
    border: none;
    padding: 0.6rem 0.8rem;
    color: var(--text-primary);
    outline: none;
    font-size: 0.95rem;
}

#send-btn {
    width: 42px;
    height: 42px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

#send-btn:hover {
    background: var(--accent-hover);
}

/* Advanced Cognitive UI Styles */
.associative-link {
    margin-top: 10px;
    padding: 8px 12px;
    background: rgba(109, 93, 252, 0.05);
    border-radius: 10px;
    font-size: 0.8rem;
    color: var(--text-secondary);
    border: 1px dashed var(--accent);
}

.intervention-box {
    background: rgba(236, 72, 153, 0.05);
    border-right: 3px solid #ec4899;
    padding: 10px 12px;
    border-radius: 8px;
    margin-bottom: 10px;
    font-size: 0.9rem;
}

.intervention-box strong {
    color: #ec4899;
}

small {
    display: block;
    margin-top: 6px;
    opacity: 0.6;
    font-size: 0.7rem;
}

/* 7️⃣ تحسين Responsive */
@media (max-width: 768px) {
    .container { width: 100%; height: 100vh; border-radius: 0; border: none; }
    .message { max-width: 90%; }
    header { padding: 0.75rem 1rem; }
    .cognitive-stats { display: none; } /* Hide stats on very small screens to save space */
    .input-area { padding: 1rem; }
}

.hidden { display: none !important; }

/* Decision OS UI Components */
.message-wrapper {
    display: flex;
    flex-direction: column;
    margin-bottom: 1rem;
}

.decision-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-top: 0.4rem;
    padding: 0 0.5rem;
    align-self: flex-end;
}

.tool-btn {
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    padding: 0.3rem 0.6rem;
    border-radius: 6px;
    font-size: 0.7rem;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 4px;
}

.tool-btn svg {
    opacity: 0.7;
}

.tool-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
    background: var(--bg-secondary);
}

.tool-btn:hover svg {
    opacity: 1;
    stroke: var(--accent);
}

.tool-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.decision-result {
    border-right: 3px solid var(--accent);
    padding-right: 10px;
    background: rgba(109, 93, 252, 0.05);
    border-radius: 4px;
    padding: 8px;
    font-size: 0.9rem;
}

[data-theme="dark"] .decision-result {
    background: rgba(139, 92, 246, 0.1);
}
