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

@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Arabic:wght@400;500;600&display=swap');

html {
    height: 100%; /* Make html element fill the viewport */
}

body {
    font-family: 'IBM Plex Sans Arabic', sans-serif;
    background: linear-gradient(135deg, 
        rgba(135, 206, 235, 0.015) 0%, 
        rgba(173, 216, 230, 0.02) 50%, 
        rgba(240, 248, 255, 0.025) 100%);
    color: #333;
    display: flex;
    flex-direction: column;
    min-height: 100%; /* Use min-height to allow content to grow */
    /* REMOVED: overflow: hidden; to allow pull-to-refresh */
}

/* --- Header --- */
.header {
    height: 60px;
    background: linear-gradient(135deg, rgba(255, 255, 255, 0.9), rgba(248, 250, 252, 0.8));
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(135, 206, 235, 0.1);
    display: flex;
    align-items: center;
    padding: 0 20px;
    justify-content: space-between;
    position: sticky; /* Keep header at the top */
    top: 0;
    z-index: 100;
    flex-shrink: 0;
    width: 100%;
}
.header-right { display: flex; align-items: center; gap: 15px; }
.menu-button, .new-chat-button {
    background: transparent; border: 1px solid rgba(135, 206, 235, 0.2);
    border-radius: 8px; width: 40px; height: 40px; display: flex;
    justify-content: center; align-items: center; cursor: pointer;
    font-size: 1.3rem; color: #555; transition: all 0.2s ease;
}
.menu-button:hover, .new-chat-button:hover {
    background: rgba(135, 206, 250, 0.1); border-color: rgba(135, 206, 235, 0.3);
}
.login-button {
    background-color: #111;
    color: white;
    padding: 8px 16px;
    border-radius: 8px;
    text-decoration: none;
    font-size: 0.9rem;
    font-family: 'IBM Plex Sans Arabic', sans-serif;
    transition: background-color 0.2s ease;
}
.login-button:hover {
    background-color: #333;
}

/* --- Sidebar --- */
.sidebar {
    position: fixed; top: 0; right: 0; width: 50%; height: 100vh;
    background: linear-gradient(180deg, rgba(248, 250, 252, 0.98) 0%, rgba(241, 245, 249, 0.95) 100%);
    backdrop-filter: blur(15px); -webkit-backdrop-filter: blur(15px);
    border-left: 1px solid rgba(135, 206, 235, 0.15);
    transform: translateX(100%);
    transition: transform 0.3s ease;
    z-index: 200;
    display: flex;
    flex-direction: column;
    will-change: transform;
}
.sidebar.visible { transform: translateX(0); }
.sidebar-top-actions {
    padding: 15px; border-bottom: 1px solid rgba(135, 206, 235, 0.1);
}
.subscribe-button {
    width: 100%; background: linear-gradient(45deg, #3a7bd5, #00d2ff); color: white;
    border: none; border-radius: 8px; padding: 10px; font-size: 0.9rem;
    display: flex; align-items: center; justify-content: center; gap: 8px;
    cursor: pointer; transition: all 0.2s ease; font-family: 'IBM Plex Sans Arabic', sans-serif;
}
.subscribe-button:hover { transform: translateY(-2px); box-shadow: 0 4px 15px rgba(0, 150, 255, 0.2); }
.subscribe-button i { font-size: 1.2rem; }

.sidebar-header {
    padding: 15px 20px 10px; display: flex; align-items: center; justify-content: space-between;
}
.sidebar-title { font-size: 1.1rem; font-weight: 500; color: #333; }
.sidebar-search-container { position: relative; padding: 0 20px 15px; }
.sidebar-search-input {
    width: 100%; padding: 8px 35px 8px 12px; border-radius: 8px;
    border: 1px solid rgba(135, 206, 235, 0.2);
    background: rgba(255, 255, 255, 0.7); outline: none;
    font-family: 'IBM Plex Sans Arabic', sans-serif;
}
.sidebar-search-container .bx-search {
    position: absolute; top: 50%; transform: translateY(-50%); right: 30px; color: #888;
}
.sidebar-bottom {
    margin-top: auto; padding: 15px; display: flex; align-items: center;
    justify-content: space-between; border-top: 1px solid rgba(135, 206, 235, 0.1);
}
.user-account { display: flex; align-items: center; gap: 10px; color: #444; }
.user-account i { font-size: 1.5rem; }
.sidebar-bottom-actions {
    display: flex;
    gap: 10px;
    align-items: center;
}
.settings-button, .logout-button {
    background: none; border: none; font-size: 1.5rem; color: #777;
    cursor: pointer; transition: color 0.2s ease;
}
.settings-button:hover { color: #3a7bd5; }
.logout-button:hover { color: #c0392b; }

/* --- Main Content --- */
.main-container { 
    flex-grow: 1; /* Let it take available space */
    display: flex; 
    flex-direction: column; 
    height: calc(100vh - 60px); /* Fill space below header */
    overflow: hidden; /* Prevent internal scrolling */
}
.welcome-content {
    flex-grow: 1; display: flex; flex-direction: column; justify-content: center;
    align-items: center; transition: all 0.3s ease; padding: 20px; overflow-y: auto;
}
.welcome-content.hidden { display: none; }
.welcome-logo { width: 80px; margin-bottom: 20px; }
.welcome-title { font-size: 1.2rem; font-weight: 500; color: #34495e; text-align: center; }
.welcome-content h1, .welcome-content h2, .welcome-content span { display: none; }

/* --- Suggestion Cards (Now above input) --- */
.suggestion-grid {
    display: flex; flex-wrap: nowrap; gap: 12px; width: 100%;
    max-width: 90%; margin: 0 auto; overflow-x: auto;
    padding: 5px 0 10px 0; margin-bottom: 15px; scrollbar-width: thin;
    scrollbar-color: rgba(135, 206, 235, 0.4) transparent; transition: opacity 0.3s ease, transform 0.3s ease;
}
.suggestion-grid.hidden { opacity: 0; transform: translateY(20px); height: 0; padding: 0; margin-bottom: 0; pointer-events: none; }
.suggestion-grid::-webkit-scrollbar { height: 5px; }
.suggestion-grid::-webkit-scrollbar-thumb {
    background-color: rgba(135, 206, 235, 0.4); border-radius: 10px;
}
.suggestion-card {
    background: rgba(255, 255, 255, 0.7); border: 1px solid rgba(135, 206, 235, 0.15);
    border-radius: 10px; padding: 10px; flex: 0 0 140px; display: flex;
    flex-direction: column; align-items: center; justify-content: center; text-align: center;
    cursor: pointer; transition: all 0.2s ease; font-family: 'IBM Plex Sans Arabic', sans-serif;
    font-size: 0.75rem; color: #444;
}
.suggestion-card:hover { transform: translateY(-3px); box-shadow: 0 3px 12px rgba(135, 206, 235, 0.1); }
.suggestion-card i { font-size: 1.6rem; margin-bottom: 5px; color: #34495e; }

/* --- Chat Area --- */
.chat-area { flex-grow: 1; padding: 20px; overflow-y: auto; display: none; }
.chat-area.visible { display: block; }
.message-wrapper { display: flex; flex-direction: column; margin-bottom: 15px; max-width: 90%; }

/* رسائل afnanai في الجانب الأيمن */
.assistant-wrapper { 
    align-items: flex-end; 
    display: flex; 
    gap: 10px; 
    margin-left: auto;
    flex-direction: row-reverse; /* اللوغو على اليمين */
}
.assistant-avatar { 
    width: 35px; 
    height: 35px; 
    border-radius: 50%; 
    flex-shrink: 0; 
    margin-top: 0;
    align-self: flex-start;
}
.message-content-wrapper { 
    display: flex; 
    flex-direction: column; 
    width: 100%; 
    align-items: flex-end;
}
.assistant-name { 
    font-size: 0.8rem; 
    font-weight: 500; 
    color: #333; 
    margin-bottom: 4px; 
}

/* رسائل المستخدم في الجانب الأيسر */
.user-wrapper { 
    align-items: flex-start; 
    margin-right: auto;
}
.user-message-content-wrapper {
    display: flex;
    flex-direction: column;
    width: 100%;
    align-items: flex-start;
}
.user-message-info {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
}
.user-message-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
}
.user-message-name {
    font-size: 0.85rem;
    font-weight: 500;
    color: #555;
}
.message {
    padding: 15px 20px; border-radius: 16px; width: fit-content; max-width: 100%;
    line-height: 1.5; animation: slideIn 0.3s ease; position: relative;
}
.message.user { 
    background: linear-gradient(135deg, rgba(135, 206, 250, 0.15), rgba(173, 216, 230, 0.18));
    border-bottom-left-radius: 4px;
    align-self: flex-start;
}
.message.assistant { 
    background: rgba(255, 255, 255, 0.8);
    border-bottom-right-radius: 4px;
    border: 1px solid rgba(135, 206, 235, 0.1);
    align-self: flex-end;
}
@keyframes slideIn { from { opacity: 0; transform: translateY(15px); } to { opacity: 1; transform: translateY(0); } }

/* --- Action Bar for Messages --- */
.action-bar { 
    display: flex; 
    gap: 8px; 
    padding: 8px 10px 0;
    margin-top: 5px;
}
/* أزرار المستخدم على اليسار */
.user-wrapper .action-bar { 
    justify-content: flex-start;
    align-self: flex-start;
}
/* أزرار afnanai على اليمين */
.assistant-wrapper .action-bar { 
    justify-content: flex-end;
    align-self: flex-end;
}
.action-bar button {
    background: none; border: none; color: #777; font-size: 1.1rem;
    cursor: pointer; transition: color 0.2s;
}
.action-bar button:hover { color: #333; }

/* --- Input Area --- */
.input-container {
    padding: 0 20px;
    /* ADDED: A safe area padding at the bottom */
    padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 10px);
    max-width: 800px; margin: 0 auto;
    width: 100%; flex-shrink: 0; display: flex; flex-direction: column; align-items: center;
}
.input-wrapper { position: relative; padding: 2px; border-radius: 25px; transition: all 0.3s ease; width: 100%; }
.input-wrapper::before {
    content: ''; position: absolute; top: 0; left: 0; right: 0; bottom: 0;
    border-radius: 25px; background: linear-gradient(45deg, rgba(135, 206, 250, 0.3), rgba(173, 216, 230, 0.4));
    opacity: 0; transition: opacity 0.3s ease; z-index: 0;
}
.input-wrapper.focused::before { opacity: 1; }
.input-bar {
    position: relative; z-index: 1; background: linear-gradient(135deg, rgba(255, 255, 255, 0.9), rgba(248, 250, 252, 0.8));
    backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-radius: 23px; padding: 8px;
    display: flex; align-items: flex-start; box-shadow: 0 2px 15px rgba(135, 206, 235, 0.1);
    border: 1px solid #d1d5db;
}
.input-content { flex-grow: 1; display: flex; flex-direction: column; }

.send-button {
    background: #111; color: white; border: none; border-radius: 19px;
    width: 38px; height: 38px; display: flex; justify-content: center;
    align-items: center; cursor: pointer; font-size: 1.5rem;
    margin-left: 8px; transition: all 0.2s ease; flex-shrink: 0;
}
.send-button:hover:not(:disabled) { background: #333; transform: scale(1.05); }
.send-button.stop-generation { background: #c0392b; }

.chat-textarea {
    flex-grow: 1; border: none; outline: none; background: transparent; padding: 8px 15px; font-size: 1rem;
    font-family: 'IBM Plex Sans Arabic', sans-serif; color: #333; resize: none; line-height: 1.4;
    max-height: 150px; overflow-y: auto;
}
.chat-textarea::placeholder { color: #999; }
.disclaimer {
    text-align: center; font-size: 0.75rem; color: #888; margin-top: 12px; opacity: 0.8;
    max-width: 800px; width: 100%;
}

@media (max-width: 768px) {
     .sidebar { width: 80%; }
     .welcome-title { font-size: 1rem; }
}


.image-upload-button {
    background: transparent;
    border: none;
    color: #555;
    font-size: 1.5rem;
    cursor: pointer;
    transition: color 0.2s ease;
    margin-right: 8px;
}

.image-upload-button:hover {
    color: #333;
    transform: scale(1.1) rotate(-15deg);
}

.user-profile-image {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    margin-right: 10px;
}

.user-info {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
}

.user-email {
    font-size: 0.75rem;
    color: #888;
}

.chat-history-list {
    flex-grow: 1;
    overflow-y: auto;
    padding: 10px 20px;
}

.chat-history-item {
    display: flex;
    align-items: center;
    padding: 10px;
    margin-bottom: 8px;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.2s ease;
    color: #444;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-history-item:hover {
    background-color: rgba(135, 206, 235, 0.1);
}

.chat-history-item.active {
    background-color: rgba(135, 206, 235, 0.2);
    font-weight: 500;
    color: #333;
}

.chat-history-item i {
    margin-left: 10px;
    font-size: 1.1rem;
    flex-shrink: 0;
}


/* ===== Dark Mode Styles ===== */
body.dark-mode {
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
    color: #e0e0e0;
}

body.dark-mode .header {
    background: rgba(0, 0, 0, 0.3);
    border-bottom-color: rgba(255, 255, 255, 0.1);
}

body.dark-mode .login-button {
    background: rgba(255, 255, 255, 0.1);
    color: #e0e0e0;
    border-color: rgba(255, 255, 255, 0.2);
}

body.dark-mode .login-button:hover {
    background: rgba(255, 255, 255, 0.2);
}

body.dark-mode .menu-button,
body.dark-mode .new-chat-button {
    background: rgba(255, 255, 255, 0.1);
    color: #e0e0e0;
}

body.dark-mode .menu-button:hover,
body.dark-mode .new-chat-button:hover {
    background: rgba(255, 255, 255, 0.2);
}

body.dark-mode .sidebar {
    background: rgba(0, 0, 0, 0.4);
    border-right-color: rgba(255, 255, 255, 0.1);
}

body.dark-mode .subscribe-button {
    background: rgba(58, 123, 213, 0.2);
    border-color: rgba(58, 123, 213, 0.3);
}

body.dark-mode .subscribe-button:hover {
    background: rgba(58, 123, 213, 0.3);
}

body.dark-mode .sidebar-title {
    color: #e0e0e0;
}

body.dark-mode .sidebar-search-container {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.1);
}

body.dark-mode .sidebar-search-input {
    background: transparent;
    color: #e0e0e0;
}

body.dark-mode .sidebar-search-input::placeholder {
    color: #888;
}

body.dark-mode .chat-history-item {
    background: rgba(255, 255, 255, 0.05);
    color: #e0e0e0;
}

body.dark-mode .chat-history-item:hover {
    background: rgba(255, 255, 255, 0.1);
}

body.dark-mode .sidebar-bottom {
    border-top-color: rgba(255, 255, 255, 0.1);
}

body.dark-mode .user-account {
    color: #e0e0e0;
}

body.dark-mode .settings-button,
body.dark-mode .logout-button {
    color: #aaa;
}

body.dark-mode .settings-button:hover {
    color: #5a9bd5;
}

body.dark-mode .logout-button:hover {
    color: #e74c3c;
}

body.dark-mode .welcome-title {
    color: #e0e0e0;
}

body.dark-mode .suggestion-card {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.1);
    color: #e0e0e0;
}

body.dark-mode .suggestion-card:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.2);
}

body.dark-mode .input-bar {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.1);
}

body.dark-mode .chat-textarea {
    background: transparent;
    color: #e0e0e0;
}

body.dark-mode .chat-textarea::placeholder {
    color: #888;
}

body.dark-mode .send-button {
    background: rgba(58, 123, 213, 0.3);
}

body.dark-mode .send-button:hover {
    background: rgba(58, 123, 213, 0.5);
}

body.dark-mode .image-upload-button {
    color: #aaa;
}

body.dark-mode .image-upload-button:hover {
    color: #5a9bd5;
}

body.dark-mode .disclaimer {
    color: #888;
}

body.dark-mode .message {
    background: rgba(255, 255, 255, 0.05);
    color: #e0e0e0;
}

body.dark-mode .message.user {
    background: rgba(58, 123, 213, 0.2);
}

body.dark-mode .message-actions button {
    color: #aaa;
}

body.dark-mode .message-actions button:hover {
    color: #5a9bd5;
}

/* ===== Tools Modal ===== */
.tools-modal {
    position: absolute; /* Changed from fixed to absolute */
    bottom: 60px; /* Position above the input bar */
    right: 20px; /* Position on the right side */
    width: 300px; /* Set a fixed width */
    max-width: calc(100% - 40px); /* Ensure it doesn't exceed screen width */
    background: none; /* Remove background overlay */
    backdrop-filter: none; /* Remove backdrop filter */
    display: none;
    z-index: 1000;
    animation: none; /* Remove animation for now */
    transform-origin: bottom right; /* Set transform origin for better animation */
}

.tools-modal.active {
    display: block;
    animation: scaleIn 0.2s ease-out forwards; /* New animation for small modal */
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.tools-modal-content {
    background: rgba(255, 255, 255, 0.98); /* Slightly more opaque */
    border-radius: 15px; /* Uniform border radius */
    padding: 15px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15); /* Add shadow */
    width: 100%; /* Fill the parent width */
    margin: 0; /* Remove auto margin */
    animation: none; /* Remove animation */
    border: 1px solid rgba(135, 206, 235, 0.3);
}

body.dark-mode .tools-modal-content {
    background: rgba(26, 26, 46, 0.98);
    border: 1px solid rgba(255, 255, 255, 0.15);
}

@keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

@keyframes scaleIn {
    from { opacity: 0; transform: scale(0.8); }
    to { opacity: 1; transform: scale(1); }
}

.tools-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 10px; /* Reduced gap */
}

.tool-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 20px;
    background: rgba(135, 206, 235, 0.1);
    border: 1px solid rgba(135, 206, 235, 0.2);
    border-radius: 15px;
    cursor: pointer;
    transition: all 0.2s ease;
    font-family: 'IBM Plex Sans Arabic', sans-serif;
}

body.dark-mode .tool-item {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.1);
}

.tool-item:hover {
    background: rgba(135, 206, 235, 0.2);
    border-color: rgba(135, 206, 235, 0.4);
    transform: translateY(-2px);
}

body.dark-mode .tool-item:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.2);
}

.tool-item i {
    font-size: 2rem;
    color: #3a7bd5;
}

body.dark-mode .tool-item i {
    color: #5a9bd5;
}

.tool-item span {
    font-size: 0.9rem;
    color: #555;
    text-align: center;
}

body.dark-mode .tool-item span {
    color: #ccc;
}

.tools-button {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: #555;
    cursor: pointer;
    padding: 5px 10px;
    transition: all 0.2s ease;
}

.tools-button:hover {
    color: #3a7bd5;
}

body.dark-mode .tools-button {
    color: #aaa;
}

body.dark-mode .tools-button:hover {
    color: #5a9bd5;
}

/* Responsive Design for Tools Modal */
@media (max-width: 768px) {
    .tools-modal {
        bottom: 60px;
        right: 10px;
        width: 250px;
    }
    .tools-modal-content {
        padding: 10px;
    }
    
    .tools-grid {
        grid-template-columns: 1fr; /* Single column on small screens */
        gap: 8px;
    }
    
    .tool-item {
        padding: 10px;
        flex-direction: row; /* Align items horizontally */
        justify-content: flex-start;
    }
    
    .tool-item i {
        font-size: 1.5rem;
    }
    
    .tool-item span {
        font-size: 0.9rem;
    }
}

@media (max-width: 480px) {
    .tools-modal {
        bottom: 60px;
        right: 10px;
        width: 200px;
    }
    .tools-modal-content {
        padding: 8px;
    }
    
    .tools-grid {
        grid-template-columns: 1fr;
        gap: 6px;
    }
    
    .tool-item {
        padding: 8px;
    }
    
    .tool-item i {
        font-size: 1.4rem;
    }
    
    .tool-item span {
        font-size: 0.85rem;
    }
}

@media (min-width: 1024px) {
    .tools-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}
