/* Light Theme Default */
        :root {
            --chat-theme-color: var(--dynamic-theme-color, #10B981);
            --bg-main: #f9fafb; /* gray-50 */
            --bg-sidebar: #f3f4f6; /* gray-100 */
            --bg-input: #ffffff;
            --text-main: #111827; /* gray-900 */
            --text-muted: #6b7280; /* gray-500 */
            --border-color: rgba(0,0,0,0.1);
            --bubble-user-text: #ffffff;
            --bubble-ai-text: #111827;
            --sidebar-hover: rgba(0,0,0,0.05);
            --sidebar-active: rgba(0,0,0,0.08);
            --icon-color: #6b7280;
        }

        /* Dark Theme */
        html.dark {
            --bg-main: #131314;
            --bg-sidebar: #1e1f20;
            --bg-input: #1e1f20;
            --text-main: #e3e3e3;
            --text-muted: #c4c7c5;
            --border-color: rgba(255,255,255,0.1);
            --bubble-user-text: #ffffff;
            --bubble-ai-text: #e3e3e3;
            --sidebar-hover: rgba(255,255,255,0.08);
            --sidebar-active: rgba(255,255,255,0.12);
            --icon-color: #c4c7c5;
        }


        body {
            background-color: var(--bg-main);
            color: var(--text-main);
            margin: 0;
            overflow: hidden; /* Prevent body scroll, everything scrolls internally */
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
        }
        
        /* Layout */
        .app-container {
            display: flex;
            height: 100dvh; /* Better mobile support */
            width: 100vw;
            max-width: 100vw;
            overflow-x: hidden;
        }
        
        /* Sidebar */
        .sidebar {
            width: 260px;
            background-color: var(--bg-sidebar);
            display: flex;
            flex-direction: column;
            transition: transform 0.3s ease, width 0.3s ease, background-color 0.3s;
            flex-shrink: 0;
            z-index: 40;
            overflow-x: hidden;
            border-right: 1px solid var(--border-color);
        }
        .sidebar.collapsed {
            width: 68px;
        }
        .sidebar.collapsed .sidebar-text,
        .sidebar.collapsed .history-list,
        .sidebar.collapsed .history-header,
        .sidebar.collapsed .sidebar-toggle-btn,
        .sidebar.collapsed #custom-ai-section,
        .sidebar.collapsed .pwa-install-trigger {
            display: none !important;
        }
        .sidebar.collapsed .app-logo-container {
            width: 100%;
            justify-content: center;
        }
        .sidebar.collapsed .new-chat-btn {
            width: 40px;
            height: 40px;
            padding: 0;
            justify-content: center;
            border-radius: 50%;
            margin: 0.5rem auto;
        }
        .sidebar.collapsed .sidebar-footer button,
        .sidebar.collapsed .sidebar-footer a {
            justify-content: center;
            padding: 10px 0;
        }
        .sidebar.collapsed #profile-btn {
            background-color: transparent !important;
            border-color: transparent !important;
            box-shadow: none !important;
            padding: 10px 0 !important;
        }
        .sidebar.collapsed #profile-btn:hover {
            background-color: var(--sidebar-hover) !important;
        }
        
        .wa-join-btn {
            color: #ffffff !important;
        }
        
        .sidebar.collapsed .wa-join-btn {
            background-color: transparent !important;
            box-shadow: none !important;
            color: var(--text-muted) !important;
        }
        .sidebar.collapsed .wa-join-btn:hover {
            background-color: var(--sidebar-hover) !important;
        }
        
        @media (max-width: 768px) {
            .sidebar {
                position: fixed;
                top: 0;
                left: 0;
                height: 100%;
                transform: translateX(-100%);
                width: 280px;
            }
            .sidebar.mobile-open {
                transform: translateX(0);
                box-shadow: 4px 0 24px rgba(0,0,0,0.2);
            }
        }
        
        .sidebar-header {
            padding: 1rem;
            display: flex;
            align-items: center;
            justify-content: flex-end;
        }
        
        .new-chat-btn {
            background-color: transparent;
            color: var(--text-main);
            border: none;
            padding: 10px 15px;
            border-radius: 8px;
            display: flex;
            align-items: center;
            gap: 10px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: background 0.2s;
            width: calc(100% - 1rem);
            margin: 0.25rem auto;
            white-space: nowrap;
        }
        .new-chat-btn:hover { background-color: var(--sidebar-hover); }
        .new-chat-btn svg { color: var(--text-main); }
        
        .sidebar-scrollable {
            flex-grow: 1;
            overflow-y: auto;
            overflow-x: hidden;
            display: flex;
            flex-direction: column;
        }
        .sidebar-scrollable::-webkit-scrollbar { width: 6px; }
        .sidebar-scrollable::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 4px; opacity: 0.5; }

        .history-list {
            padding: 0 1rem;
        }
        
        .history-item {
            padding: 10px 12px;
            border-radius: 8px;
            font-size: 13px;
            color: var(--text-main);
            cursor: pointer;
            display: flex;
            align-items: center;
            margin-bottom: 2px;
            transition: background 0.2s;
        }
        .history-item:hover, .history-item.active { background-color: var(--sidebar-active); }
        
        .sidebar-footer {
            padding: 1rem;
            font-size: 13px;
            display: flex;
            flex-direction: column;
            gap: 4px;
        }
        .sidebar-footer a {
            display: flex;
            align-items: center;
            gap: 10px;
            color: var(--text-muted);
            padding: 10px;
            border-radius: 8px;
            text-decoration: none;
            transition: background 0.2s, color 0.2s;
            white-space: nowrap;
            font-weight: 500;
        }
        .sidebar-footer a:hover { background-color: var(--sidebar-hover); color: var(--text-main); }
        
        /* Main Chat Area */
        .main-content {
            flex-grow: 1;
            display: flex;
            flex-direction: column;
            position: relative;
            height: 100%;
            min-width: 0;
            width: 100%;
        }
        
        .main-content.is-empty .chat-container {
            flex-grow: 0;
            margin-top: auto;
            overflow-y: visible;
        }
        
        .main-content.is-empty .input-wrapper {
            margin-bottom: auto;
        }
        
        .top-nav {
            height: 60px;
            padding: 0 1rem;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        
        .toggle-btn {
            background: transparent;
            border: none;
            color: var(--icon-color);
            padding: 8px;
            border-radius: 50%;
            cursor: pointer;
            transition: background 0.2s, color 0.2s;
        }
        .toggle-btn:hover { background: var(--sidebar-hover); color: var(--text-main); }
        
        .chat-container {
            flex-grow: 1;
            overflow-y: auto;
            overflow-x: hidden;
            padding: 2rem 1rem;
            display: flex;
            flex-direction: column;
            min-width: 0;
            width: 100%;
        }
        .chat-container::-webkit-scrollbar { width: 8px; }
        .chat-container::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 4px; }
        
        .message-row {
            width: 100%;
            max-width: 800px;
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            margin: 0 auto 2rem auto;
            min-width: 0;
        }
        .message-row.user { align-items: flex-end; }
        
        .message-bubble {
            max-width: 85%;
            line-height: 1.6;
            font-size: 15px;
            word-wrap: break-word;
        }
        
        .message-row.user .message-bubble {
            background-color: var(--chat-theme-color);
            color: var(--bubble-user-text);
            padding: 12px 20px;
            border-radius: 20px 20px 4px 20px;
        }
        
        .message-bubble {
            max-width: 85%;
            word-wrap: break-word;
            word-break: break-word;
            overflow-wrap: break-word;
            min-width: 0;
            overflow-x: hidden;
        }
        
        .bubble-content {
            max-width: 100%;
            overflow-x: hidden;
            min-width: 0;
            width: 100%;
        }
        
        .message-bubble pre {
            max-width: 100%;
            overflow-x: auto;
            white-space: pre-wrap; /* Better to wrap text in chat than scroll */
            word-break: break-word;
        }

        .message-row.assistant .message-bubble {
            color: var(--bubble-ai-text);
            padding: 0;
            overflow-x: hidden;
            min-width: 0;
            width: 100%;
        }
        
        @media (max-width: 768px) {
            .message-bubble {
                max-width: 100%;
                font-size: 14.5px;
            }
            .message-row.user .message-bubble {
                max-width: 90%;
            }
        }
        
        .message-bubble pre {
            background: var(--bg-input);
            padding: 1rem;
            border-radius: 8px;
            overflow-x: auto;
            margin: 1rem 0;
            border: 1px solid var(--border-color);
            max-width: 100%;
        }
        .message-bubble code { font-family: monospace; font-size: 14px; background: var(--sidebar-hover); padding: 2px 4px; border-radius: 4px; word-break: break-word; }
        .message-bubble pre code { background: transparent; padding: 0; word-break: normal; }
        .message-bubble p { margin-bottom: 1rem; max-width: 100%; }
        .message-bubble p:last-child { margin-bottom: 0; }
        
        /* Kitab Search Thinking Block */
        .kitab-thinking-block {
            border: 1px solid var(--border-color, #e2e8f0);
            border-radius: 12px;
            overflow: hidden;
            margin-bottom: 8px;
            background: var(--bg-main, #fff);
            transition: all 0.3s ease;
        }
        .kitab-thinking-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 10px 14px;
            cursor: pointer;
            user-select: none;
            transition: background 0.2s ease;
        }
        .kitab-thinking-header:hover {
            background: rgba(0,0,0,0.03);
        }
        html.dark .kitab-thinking-header:hover {
            background: rgba(255,255,255,0.03);
        }
        .kitab-thinking-indicator {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        .kitab-thinking-spinner {
            width: 18px;
            height: 18px;
            border: 2px solid rgba(59,130,246,0.2);
            border-top-color: #3b82f6;
            border-radius: 50%;
            animation: ks-spin 0.8s linear infinite;
        }
        .kitab-thinking-spinner.done {
            animation: none;
            border-color: #10b981;
            background: #10b981;
            position: relative;
        }
        .kitab-thinking-spinner.done::after {
            content: '✓';
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: white;
            font-size: 10px;
            font-weight: bold;
        }
        @keyframes ks-spin {
            to { transform: rotate(360deg); }
        }
        .kitab-thinking-label {
            font-size: 13px;
            font-weight: 500;
            color: var(--text-muted, #64748b);
        }
        .kitab-thinking-chevron {
            color: var(--text-muted, #94a3b8);
            transition: transform 0.3s ease;
        }
        .kitab-thinking-block.collapsed .kitab-thinking-chevron {
            transform: rotate(-90deg);
        }
        .kitab-thinking-content {
            max-height: 400px;
            overflow-y: auto;
            overflow-x: hidden;
            transition: max-height 0.4s ease, opacity 0.3s ease, padding 0.3s ease;
            padding: 0 14px 12px;
            opacity: 1;
            word-wrap: break-word;
        }
        .kitab-thinking-block.collapsed .kitab-thinking-content {
            max-height: 0;
            opacity: 0;
            padding: 0 14px;
        }
        .kitab-thinking-steps {
            display: flex;
            flex-direction: column;
            gap: 4px;
            width: 100%;
        }
        .kitab-thinking-step {
            font-size: 12px;
            color: var(--text-muted, #64748b);
            padding: 3px 0;
            border-left: 2px solid rgba(59,130,246,0.15);
            padding-left: 10px;
            font-family: 'Amiri', serif, system-ui;
            word-wrap: break-word;
            word-break: break-word;
            white-space: pre-wrap;
            width: 100%;
            overflow-x: hidden;
        }
        
        /* Font Styles for Arabic & Latin */
        .font-arabic {
            font-family: 'Amiri', serif;
        }
        .font-lora {
            font-family: 'Lora', serif;
        }
        .arabic-text {
            text-align: center;
            font-family: 'Amiri', serif;
            font-size: 1.75rem; /* larger font for better readability */
            font-weight: 700;
            line-height: 2.2; /* extra spacing for harakat */
            margin-bottom: 1.5rem;
            direction: rtl;
        }
        .latin-text {
            text-align: left;
            font-family: 'Lora', serif;
            margin-bottom: 1rem; /* mb-4 */
        }
        
        /* Input Area */
        .input-wrapper {
            max-width: 800px;
            width: 100%;
            margin: 0 auto;
            padding: 0 1rem 1.5rem 1rem;
            position: relative;
        }
        
        @media (max-width: 768px) {
            .input-wrapper {
                padding: 0 0.5rem 0.25rem 0.5rem;
            }
        }

        .attachment-preview {
            display: flex;
            gap: 8px;
            padding: 8px 16px;
            margin-bottom: 8px;
            overflow-x: auto;
        }
        .attachment-item {
            position: relative;
            background: var(--bg-input);
            border: 1px solid var(--border-color);
            border-radius: 8px;
            padding: 4px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .attachment-item img {
            max-height: 40px;
            border-radius: 4px;
        }
        .attachment-remove {
            position: absolute;
            top: -6px;
            right: -6px;
            background: #ef4444;
            color: white;
            border-radius: 50%;
            width: 18px;
            height: 18px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            cursor: pointer;
            border: 2px solid var(--bg-color);
        }
        
        .input-box {
            background: var(--bg-input);
            border-radius: 24px;
            display: flex;
            align-items: flex-end;
            padding: 8px 16px;
            border: 1px solid var(--border-color);
            transition: background 0.3s, border-color 0.3s;
            box-shadow: 0 2px 6px rgba(0,0,0,0.05);
        }
        .input-box:focus-within { border-color: var(--chat-theme-color); box-shadow: 0 4px 12px rgba(0,0,0,0.08); }
        html.dark .input-box { box-shadow: none; }
        html.dark .input-box:focus-within { background: #2a2b2c; border-color: rgba(255,255,255,0.2); }
        
        .chat-textarea {
            flex-grow: 1;
            background: transparent;
            border: none;
            color: var(--text-main);
            font-size: 15px;
            padding: 8px 0;
            resize: none;
            outline: none;
            max-height: 150px;
            min-height: 24px;
            line-height: 1.5;
        }
        
        .send-btn {
            background: transparent;
            color: var(--chat-theme-color);
            border: none;
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: background 0.2s, opacity 0.2s;
            flex-shrink: 0;
            margin-left: 8px;
        }
        .send-btn:hover { background: var(--sidebar-hover); }
        .send-btn:disabled { opacity: 0.5; cursor: not-allowed; }
        
        /* Typing indicator */
        .typing-indicator {
            display: inline-flex;
            gap: 4px;
            padding: 8px 0;
        }
        .typing-dot {
            width: 6px;
            height: 6px;
            background: var(--chat-theme-color);
            border-radius: 50%;
            animation: typing 1.4s infinite;
        }
        .typing-dot:nth-child(2) { animation-delay: 0.2s; }
        .typing-dot:nth-child(3) { animation-delay: 0.4s; }
        @keyframes typing { 0%, 100% { opacity: 0.3; transform: scale(1); } 50% { opacity: 1; transform: scale(1.2); } }
        
        .empty-state {
            margin: auto;
            text-align: center;
            color: var(--text-muted);
        }
        .empty-state h1 {
            font-size: 2.5rem;
            background: linear-gradient(90deg, var(--chat-theme-color), #a855f7);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            margin-bottom: 1rem;
        }

        /* Glass Card */
        .glass-card {
            background: var(--bg-input);
            border: 1px solid var(--border-color);
            border-radius: 1rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
        html.dark .glass-card {
            background: rgba(30, 31, 32, 0.6);
            backdrop-filter: blur(12px);
            border: 1px solid rgba(255, 255, 255, 0.1);
            box-shadow: none;
        }

        /* Select Dropdown Fix */
        #chat-agent-selector option, 
        #chat-agent-selector optgroup {
            background-color: var(--bg-input);
            color: var(--text-main);
        }

        /* ========================================================= */
        /* Markdown Tables Styling (SaaS UI) */
        /* ========================================================= */
        .table-responsive {
            width: 100%;
            max-width: 100%;
            display: block;
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
            margin: 1.5rem 0;
            border-radius: 0;
            border: none;
            background-color: transparent;
            box-shadow: none;
        }
        .table-responsive::-webkit-scrollbar {
            height: 6px;
        }
        .table-responsive::-webkit-scrollbar-thumb {
            background: rgba(100, 116, 139, 0.3);
            border-radius: 3px;
        }
        .dark .table-responsive::-webkit-scrollbar-thumb {
            background: rgba(148, 163, 184, 0.2);
        }
        
        .message-bubble table {
            width: 100%;
            min-width: 500px;
            border-collapse: separate;
            border-spacing: 0;
            margin: 0;
            border: none;
            background-color: transparent;
            box-shadow: none;
            table-layout: auto;
        }
        .message-bubble thead {
            background-color: transparent;
        }
        html.dark .message-bubble thead {
            background-color: transparent;
        }
        .message-bubble th {
            font-weight: 700;
            text-align: left;
            padding: 16px 20px;
            border-bottom: 1px solid var(--border-color);
            font-size: 14.5px;
            color: var(--text-main);
            white-space: nowrap;
        }
        .message-bubble td {
            padding: 16px 20px;
            border-bottom: 1px solid var(--border-color);
            vertical-align: middle;
            font-size: 14.5px;
            line-height: 1.6;
            white-space: normal;
        }
        .message-bubble td:first-child {
            white-space: nowrap;
        }
        .message-bubble td .arabic-text {
            white-space: nowrap !important;
            display: inline-block;
        }
        .message-bubble tbody tr:last-child td {
            border-bottom: none;
        }
        .message-bubble tbody tr {
            transition: background-color 0.2s ease;
        }
        .message-bubble tbody tr:hover td {
            background-color: var(--sidebar-hover);
        }

        /* Enhancing Arabic Text automatically using unicode-range font scaling */
        @font-face {
            font-family: 'AmiriScaled';
            src: local('Amiri'), local('Times New Roman');
            size-adjust: 140%;
            unicode-range: U+0600-06FF, U+0750-077F, U+08A0-08FF, U+FB50-FDFF, U+FE70-FEFF;
        }

        .message-bubble table td, .message-bubble table th {
            font-family: 'AmiriScaled', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        }