/* ========================================
   ⚖️ المستشار القانوني العراقي - واجهة محسّنة
   ======================================== */

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

:root {
    --primary: #1a73e8;
    --primary-dark: #1557b0;
    --text-dark: #202124;
    --text-light: #5f6368;
    --border: #e8eaed;
    --bg-light: #f8f9fa;
    --bg-white: #ffffff;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 20px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans Arabic', sans-serif;
    background: linear-gradient(180deg, #f8f9fa 0%, #e8eaed 100%);
    color: var(--text-dark);
    line-height: 1.6;
    overflow-x: hidden;
}

.container {
    max-width: 100vw;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ========================================
   الهيدر المدمج
   ======================================== */

.header-toggle {
    position: fixed;
    top: 12px;
    right: 12px;
    z-index: 1001;
    width: 40px;
    height: 40px;
    border: none;
    background: var(--bg-white);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: var(--shadow-md);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s;
}

.header-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 16px rgba(0,0,0,0.12);
}

.header-toggle svg {
    width: 20px;
    height: 20px;
    color: var(--text-dark);
}

.header {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    color: white;
    padding: 16px 20px;
    box-shadow: var(--shadow-md);
    transition: all 0.4s ease;
    position: sticky;
    top: 0;
    z-index: 1000;
}

.header.hidden {
    transform: translateY(-100%);
}

.header-content {
    max-width: 1400px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 20px;
}

.header-content h1 {
    font-size: 20px;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 8px;
}

.user-section {
    display: flex;
    gap: 12px;
    align-items: center;
}

.btn-link {
    color: white;
    text-decoration: none;
    padding: 6px 16px;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    background: rgba(255, 255, 255, 0.15);
    transition: all 0.2s;
}

.btn-link:hover {
    background: rgba(255, 255, 255, 0.25);
}

.user-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-name {
    font-size: 14px;
    font-weight: 500;
}

.btn-logout {
    background: rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: white;
    padding: 6px 16px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s;
}

.btn-logout:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* ========================================
   منطقة المحادثة - أقصى مساحة
   ======================================== */

.main-content {
    flex: 1;
    display: flex;
    padding: 0;
}

.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    max-width: 1400px;
    margin: 0 auto;
    width: 100%;
    height: calc(100vh - 65px);
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    scroll-behavior: smooth;
}

/* ========================================
   الرسائل
   ======================================== */

.message {
    margin-bottom: 20px;
    display: flex;
    gap: 12px;
    animation: fadeIn 0.3s ease;
}

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

.message.user {
    flex-direction: row-reverse;
}

.message-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
}

.message.user .message-avatar {
    background: linear-gradient(135deg, #10b981, #059669);
}

.message-content {
    max-width: 75%;
    background: var(--bg-white);
    padding: 16px 20px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    line-height: 1.7;
    font-size: 15px;
}

.message.user .message-content {
    background: linear-gradient(135deg, #10b981, #059669);
    color: white;
}

.message-content p {
    margin-bottom: 12px;
}

.message-content p:last-child {
    margin-bottom: 0;
}

.message-content strong {
    font-weight: 600;
    color: var(--primary);
}

.message.user .message-content strong {
    color: rgba(255, 255, 255, 0.95);
}

.legal-references {
    margin-top: 10px;
    padding: 10px;
    background: #f0f7ff;
    border-right: 3px solid var(--primary);
    border-radius: var(--radius-sm);
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 6px;
}

.legal-references h4 {
    font-size: 13px;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: 6px;
    grid-column: 1 / -1;
}

.reference-item {
    padding: 6px 8px;
    margin-bottom: 0;
    background: white;
    border-radius: 4px;
    font-size: 11px;
    line-height: 1.4;
    display: flex;
    align-items: flex-start;
    gap: 6px;
}

.reference-item:last-child {
    margin-bottom: 0;
}

.loading-dots {
    display: flex;
    gap: 6px;
    padding: 20px;
}

.loading-dots span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--primary);
    animation: bounce 1.4s infinite ease-in-out;
}

.loading-dots span:nth-child(2) {
    animation-delay: -0.3s;
}

.loading-dots span:nth-child(3) {
    animation-delay: -0.15s;
}

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0); opacity: 0.5; }
    40% { transform: scale(1); opacity: 1; }
}

.response-timer {
    margin-top: 8px;
    font-size: 12px;
    color: var(--text-light);
    display: flex;
    align-items: center;
    gap: 6px;
}

/* ========================================
   رفع الملفات - مدمج
   ======================================== */

.file-upload-area {
    padding: 0 20px;
    max-height: 120px;
    overflow-y: auto;
}

.uploaded-files {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    padding: 10px 0;
}

.uploaded-file {
    background: white;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 10px 14px;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 13px;
    max-width: 280px;
    box-shadow: var(--shadow-sm);
}

.uploaded-file.loading {
    opacity: 0.6;
    border-color: var(--primary);
}

.file-icon {
    width: 20px;
    height: 20px;
    color: var(--primary);
    flex-shrink: 0;
}

.uploaded-file.loading .file-icon {
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

.file-info {
    flex: 1;
    min-width: 0;
}

.file-name {
    font-weight: 500;
    color: var(--text-dark);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 13px;
}

.file-size {
    font-size: 11px;
    color: var(--text-light);
    margin-top: 2px;
}

.file-analysis {
    font-size: 11px;
    color: var(--primary);
    margin-top: 4px;
}

.remove-file {
    background: #fee;
    color: #c00;
    border: none;
    border-radius: 50%;
    width: 22px;
    height: 22px;
    cursor: pointer;
    font-size: 14px;
    line-height: 1;
    flex-shrink: 0;
    transition: all 0.2s;
}

.remove-file:hover {
    background: #fcc;
}

.file-error {
    background: #fee;
    color: #c00;
    padding: 10px;
    border-radius: var(--radius-sm);
    font-size: 12px;
}

/* ========================================
   شريط التحميل الجذاب للملفات والصور
   ======================================== */

.file-upload-progress {
    margin-top: 6px;
}

.progress-bar {
    width: 100%;
    height: 4px;
    background: #e8eaed;
    border-radius: 2px;
    overflow: hidden;
    position: relative;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #1a73e8 0%, #34a853 100%);
    border-radius: 2px;
    transition: width 0.3s ease;
    position: relative;
    overflow: hidden;
}

.progress-fill::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(90deg,
        transparent 0%,
        rgba(255, 255, 255, 0.3) 50%,
        transparent 100%
    );
    animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
    0% { transform: translateX(-100%); }
    100% { transform: translateX(100%); }
}

.progress-text {
    font-size: 10px;
    color: var(--text-light);
    margin-top: 4px;
    text-align: center;
}

/* ========================================
   صندوق الإدخال - مدمج وبسيط
   ======================================== */

.chat-input-container {
    padding: 16px 20px 12px 20px;
    background: var(--bg-white);
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 12px;
}

/* ✅ نص إرشادي للملفات */
.file-upload-hint {
    padding: 8px 20px;
    background: var(--bg-white);
    text-align: center;
    font-size: 11px;
    color: var(--text-light);
    line-height: 1.4;
    border-top: 1px solid rgba(0, 0, 0, 0.05);
}

.attach-btn {
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    color: var(--text-light);
    cursor: pointer;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.attach-btn:hover {
    background: var(--bg-light);
    color: var(--primary);
}

.attach-btn svg {
    width: 20px;
    height: 20px;
}

#questionInput {
    flex: 1;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 14px 18px;
    font-size: 16px;
    font-family: inherit;
    resize: none;
    outline: none;
    transition: all 0.3s;
    min-height: 80px;
    max-height: 300px;
    line-height: 1.5;
}

#questionInput:hover {
    border-color: var(--primary);
    box-shadow: 0 2px 8px rgba(26, 115, 232, 0.15);
}

#questionInput:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.15), 0 4px 12px rgba(26, 115, 232, 0.1);
}

.send-btn {
    width: 44px;
    height: 44px;
    border: none;
    background: var(--primary);
    color: white;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.send-btn:hover:not(:disabled) {
    background: var(--primary-dark);
    transform: scale(1.05);
}

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

.send-btn svg {
    width: 20px;
    height: 20px;
}

/* ========================================
   التكيف مع الأجهزة الذكية
   ======================================== */

/* الأجهزة اللوحية */
@media (max-width: 1024px) {
    .chat-container {
        height: calc(100vh - 60px);
    }

    .header-content h1 {
        font-size: 18px;
    }

    .message-content {
        max-width: 80%;
    }
}

/* الهواتف الذكية */
@media (max-width: 768px) {
    .header {
        padding: 12px 16px;
    }

    .header-content h1 {
        font-size: 16px;
    }

    .user-section {
        gap: 8px;
    }

    .btn-link {
        padding: 5px 12px;
        font-size: 13px;
    }

    .chat-container {
        height: calc(100vh - 52px);
    }

    .chat-messages {
        padding: 16px 12px;
    }

    .message-content {
        max-width: 85%;
        font-size: 14px;
        padding: 12px 16px;
    }

    .message-avatar {
        width: 32px;
        height: 32px;
        font-size: 14px;
    }

    .chat-input-container {
        padding: 12px;
        gap: 8px;
    }

    #questionInput {
        font-size: 14px;
        padding: 10px 14px;
    }

    .send-btn,
    .attach-btn {
        width: 40px;
        height: 40px;
    }

    .file-upload-area {
        padding: 0 12px;
    }

    .uploaded-file {
        max-width: 100%;
    }
}

/* الهواتف الصغيرة */
@media (max-width: 480px) {
    .header-content h1 {
        font-size: 14px;
    }

    .user-section {
        flex-direction: column;
        align-items: flex-end;
        gap: 4px;
    }

    .btn-link {
        padding: 4px 10px;
        font-size: 12px;
    }

    .message-content {
        max-width: 90%;
        font-size: 13px;
        padding: 10px 14px;
    }

    .legal-references {
        padding: 12px;
    }

    .reference-item {
        font-size: 12px;
        padding: 8px;
    }

    #questionInput {
        font-size: 13px;
        padding: 10px 12px;
    }
}

/* تحسين القابلية للقراءة على الشاشات الكبيرة */
@media (min-width: 1440px) {
    .chat-messages {
        padding: 24px;
    }

    .message-content {
        max-width: 70%;
        font-size: 16px;
    }
}

/* وضع الطباعة */
@media print {
    .header-toggle,
    .header,
    .file-upload-area,
    .chat-input-container {
        display: none;
    }

    .chat-messages {
        padding: 0;
    }

    .message {
        break-inside: avoid;
    }
}

/* التمرير السلس */
.chat-messages::-webkit-scrollbar {
    width: 8px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: #d0d0d0;
    border-radius: 4px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: #b0b0b0;
}

/* تحسين الأداء */
.message-content,
.uploaded-file,
.send-btn,
.attach-btn,
.btn-link {
    will-change: transform;
}

/* الوضع الداكن (اختياري - للمتصفحات التي تدعمه) */
@media (prefers-color-scheme: dark) {
    :root {
        --text-dark: #e8eaed;
        --text-light: #9aa0a6;
        --bg-light: #202124;
        --bg-white: #292a2d;
        --border: #3c4043;
    }

    body {
        background: var(--bg-light);
        color: var(--text-dark);
    }

    .message-content {
        background: var(--bg-white);
        color: var(--text-dark);
    }

    .legal-references {
        background: #1a2332;
    }

    .uploaded-file {
        background: var(--bg-white);
    }

    .file-name {
        color: var(--text-dark);
    }
}

/* ========================================
   Streaming Progress Bar & Animations
   ======================================== */

.streaming-progress {
    margin: 15px 0;
    animation: fadeIn 0.3s ease;
}

.progress-bar-stream {
    width: 100%;
    height: 8px;
    background: linear-gradient(90deg, #e8eaed 0%, #f1f3f4 100%);
    border-radius: 20px;
    overflow: hidden;
    box-shadow: inset 0 1px 3px rgba(0,0,0,0.1);
}

.progress-fill-stream {
    height: 100%;
    background: linear-gradient(90deg, #1a73e8 0%, #4285f4 50%, #1a73e8 100%);
    background-size: 200% 100%;
    border-radius: 20px;
    transition: width 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    animation: shimmer 2s infinite;
    box-shadow: 0 2px 8px rgba(26, 115, 232, 0.3);
}

@keyframes shimmer {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

.progress-message {
    margin-top: 10px;
    font-size: 14px;
    color: var(--text-light);
    text-align: center;
    animation: pulse 1.5s ease infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 0.7; }
    50% { opacity: 1; }
}

.streaming-answer {
    animation: slideUp 0.4s ease;
}

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

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

/* Typing effect للنص المتدفق */
.streaming-answer p:last-child::after {
    content: '▊';
    animation: blink 1s step-end infinite;
    color: var(--primary);
    margin-left: 2px;
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}
