/*
 * RTL (Right-to-Left) STYLING BEST PRACTICES
 * ==========================================
 * This application uses Hebrew (RTL) as the primary language.
 * Use CSS Logical Properties to avoid manual RTL overrides.
 *
 * KEY PRINCIPLES:
 *
 * 1. SPACING: Use logical properties instead of directional ones
 *    ✅ margin-inline-start / margin-inline-end  (NOT margin-left / margin-right)
 *    ✅ padding-inline-start / padding-inline-end (NOT padding-left / padding-right)
 *    ✅ margin-block-start / margin-block-end    (NOT margin-top / margin-bottom)
 *
 * 2. TEXT ALIGNMENT: Use logical values
 *    ✅ text-align: start / end  (NOT left / right)
 *
 * 3. POSITIONING: Use logical inset properties
 *    ✅ inset-inline-start / inset-inline-end  (NOT left / right)
 *    ✅ inset-block-start / inset-block-end    (NOT top / bottom)
 *
 * 4. BORDERS: Use logical border properties
 *    ✅ border-inline-start / border-inline-end  (NOT border-left / border-right)
 *    ✅ border-start-start-radius                (NOT border-top-left-radius)
 *    ✅ border-end-start-radius                  (NOT border-bottom-left-radius)
 *
 * 5. FLEXBOX: Default flex-direction works with RTL automatically
 *    ✅ Use natural HTML order (icon, then text)
 *    ❌ Avoid flex-direction: row-reverse for RTL adaptation
 *
 * EXAMPLES:
 *    Dropdown positioning: inset-inline-end: 0  (aligns to button edge)
 *    Message bubbles: border-end-start-radius   (adapts corner based on direction)
 *    Icon spacing: margin-inline-start: 12px    (consistent gap in any direction)
 *
 * For more details, see: docs/rtl.md
 */

/* Accessibility improvements */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Focus rings for all interactive elements */
button:focus,
input:focus,
textarea:focus,
a:focus {
    outline: 3px solid #6b21a8;
    outline-offset: 2px;
}

/* Color contrast improvements */
.message-time {
    color: #374151;
    /* Improved contrast - meets 4.5:1 ratio */
}

#prompt::placeholder {
    color: #4b5563;
    /* Improved contrast - meets 4.5:1 ratio */
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
    background: #e5ddd5;
    height: 100vh;
    overflow: hidden;
    direction: rtl;
}

.chat-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 800px;
    margin: 0 auto;
    background: #fff;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}

/* Header */
.chat-header {
    background: #6b21a8;
    color: white;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    gap: 12px;
}

.header-actions {
    display: flex;
    gap: 8px;
}

.icon-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s;
}

.icon-btn svg {
    width: 24px;
    height: 24px;
    stroke: white;
}

.icon-btn:hover:not(:disabled) {
    background: rgba(255, 255, 255, 0.3);
}

.icon-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Menu dropdown */
.menu-container {
    position: relative;
}

.menu-dropdown {
    position: absolute;
    top: calc(100% + 8px);
    inset-inline-end: 0;
    background: white;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    min-width: 220px;
    z-index: 1000;
    overflow: hidden;
}

.menu-item {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 12px 16px;
    border: none;
    background: white;
    color: #374151;
    font-size: 15px;
    text-align: end;
    cursor: pointer;
    transition: background 0.2s;
}

.menu-item svg {
    flex-shrink: 0;
}

.menu-item:hover:not(:disabled) {
    background: #f3f4f6;
}

.menu-item:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Header */
.chat-header {
    background: #6b21a8;
    color: white;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-content {
    display: flex;
    align-items: center;
    gap: 12px;
}

.bot-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: #581c87;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    text-decoration: none;
    cursor: pointer;
}

.header-info h1 {
    font-size: 18px;
    font-weight: 500;
    margin-bottom: 2px;
    color: white;
}

.header-info {
    margin-inline-end: auto;
    text-align: start;
}

.status {
    font-size: 13px;
    color: white;
}

/* Messages */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px 16px;
    background: #e5ddd5;
    background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.03'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

.message {
    margin-bottom: 12px;
    display: flex;
    flex-direction: column;
    max-width: 70%;
    animation: fadeIn 0.3s ease-in;
}

.user-message {
    align-self: flex-end;
}

.bot-message,
.thinking-message,
.tool-message {
    align-self: flex-start;
}

.message-content {
    padding: 8px 12px;
    border-radius: 18px;
    word-wrap: break-word;
    line-height: 1.4;
    font-size: 16px;
}

.user-message .message-content {
    background: #e9d5ff;
    border-end-end-radius: 4px;
    white-space: pre-wrap;
}

.bot-message .message-content,
.thinking-message .message-content,
.tool-message .message-content {
    border-end-start-radius: 4px;
}

.bot-message .message-content {
    background: white;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.thinking-message .message-content {
    background: #fef3c7;
    border: 1px solid #f59e0b;
    font-style: italic;
    color: #92400e;
}

.tool-message .message-content {
    background: #dbeafe;
    border: 1px solid #3b82f6;
    color: #1e40af;
}

.message-time {
    font-size: 11px;
    color: #374151;
    /* Improved contrast */
    margin-top: 4px;
    text-align: end;
}

.bot-message .message-time,
.thinking-message .message-time,
.tool-message .message-time {
    text-align: start;
}

.tool-indicator {
    font-size: 11px;
    font-weight: 600;
    color: #1d4ed8;
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* Typing indicator */
.typing .message-content {
    padding: 12px 16px;
}

.typing-dots {
    display: flex;
    gap: 4px;
}

.typing-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #999;
    animation: typing 1.4s infinite ease-in-out;
}

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

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

/* Input */
.input-container {
    padding: 12px 16px;
    background: #f0f0f0;
    border-top: 1px solid #e0e0e0;
}

/* Footer */
.chat-footer {
    background: #f3f4f6;
    padding: 8px 16px;
    text-align: center;
    border-top: 1px solid #e0e0e0;
}

.accessibility-link {
    color: #6b21a8;
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
}

.accessibility-link:hover {
    text-decoration: underline;
}

.accessibility-link:focus {
    outline: 2px solid #6b21a8;
    outline-offset: 2px;
}

.input-wrapper {
    display: flex;
    align-items: center;
    gap: 8px;
    background: white;
    border-radius: 24px;
    padding: 8px 12px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    flex-direction: row-reverse;
}

#prompt {
    flex: 1;
    border: none;
    outline: none;
    resize: none;
    font-size: 16px;
    line-height: 1.5;
    max-height: 120px;
    min-height: 24px;
    height: 24px;
    font-family: inherit;
    background: transparent;
    overflow-y: hidden;
    padding: 0;
    vertical-align: middle;
}

#prompt::placeholder {
    color: #4b5563;
    /* Improved contrast - meets 4.5:1 ratio */
}

#send-btn {
    background: #6b21a8;
    border: none;
    border-radius: 50%;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 12px;
    transition: background 0.2s;
    color: white;
    flex-shrink: 0;
}

#send-btn:disabled {
    background: #9ca3af;
    cursor: not-allowed;
}

#send-btn:hover {
    background: #581c87;
}

#send-btn:active {
    transform: scale(0.95);
}

#send-btn svg {
    width: 20px;
    height: 20px;
    stroke: white;
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes typing {

    0%,
    60%,
    100% {
        transform: translateY(0);
    }

    30% {
        transform: translateY(-10px);
    }
}

/* Mobile responsiveness */
@media (max-width: 768px) {
    .chat-container {
        height: 100vh;
        height: 100dvh;
        max-width: none;
        box-shadow: none;
    }

    .message {
        max-width: 85%;
    }

    .messages-container {
        padding: 16px 12px;
        padding-bottom: 20px;
    }

    .input-container {
        padding: 8px 12px;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
    }

    .chat-footer {
        padding-bottom: max(8px, env(safe-area-inset-bottom));
    }

    .history-container {
        max-width: none;
    }
}

/* RTL support */
/* Most RTL adaptations now handled automatically by CSS logical properties */

[dir="rtl"] .header-content {
    flex-direction: row-reverse;
}

[dir="rtl"] .input-wrapper {
    flex-direction: row;
}

/* History View */
.history-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 800px;
    margin: 0 auto;
    background: #fff;
}

.history-header {
    background: #6b21a8;
    color: white;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 16px;
}

.history-header .header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    gap: 12px;
}

.history-header .header-info h1 {
    font-size: 18px;
    font-weight: 500;
    color: white;
}

.history-table {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
}

.history-row {
    display: grid;
    grid-template-columns: 1fr 140px auto;
    gap: 16px;
    padding: 16px;
    border-bottom: 1px solid #f0f0f0;
    cursor: pointer;
    transition: background 0.2s;
    align-items: center;
}

.history-row:hover {
    background: #f9f9f9;
}

.history-delete-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0;
    opacity: 0.6;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.history-delete-btn svg {
    width: 20px;
    height: 20px;
    stroke: #666;
}

.history-delete-btn:hover {
    opacity: 1;
}

.history-datetime {
    font-size: 16px;
    color: #666;
    text-align: right;
    white-space: nowrap;
}

.history-count {
    font-size: 16px;
    color: #666;
    text-align: right;
    white-space: nowrap;
}

.history-empty {
    text-align: center;
    padding: 60px 20px;
    color: #999;
}

.history-error {
    text-align: center;
    padding: 60px 20px;
    color: #dc2626;
    background: #fef2f2;
    border-radius: 8px;
    margin: 16px;
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-content {
    background: white;
    padding: 24px;
    border-radius: 8px;
    max-width: 400px;
    width: 90%;
}

.modal-content p {
    margin: 0 0 20px 0;
    font-size: 16px;
    text-align: center;
}

.modal-actions {
    display: flex;
    gap: 12px;
    justify-content: center;
}

.btn-danger {
    background: #dc2626;
    color: white;
    border: none;
    padding: 10px 24px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
}

.btn-danger:hover {
    background: #b91c1c;
}

.btn-secondary {
    background: #e5e7eb;
    color: #374151;
    border: none;
    padding: 10px 24px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
}

.btn-secondary:hover {
    background: #d1d5db;
}

/* Rate limit error message */
.system-message {
    display: flex;
    align-items: flex-start;
    padding: 16px;
    margin: 16px 0;
    border-radius: 8px;
    background: #fff3cd;
    border: 1px solid #ffc107;
}

.rate-limit-error {
    background: #f8d7da;
    border-color: #f5c6cb;
}

.context-limit-error {
    background: #e3f2fd;
    border-color: #2196F3;
}

.context-limit-error .error-icon {
    font-size: 24px;
    color: #1565c0;
}

.context-limit-error .error-text strong {
    color: #1565c0;
}

.context-limit-error .error-text p {
    color: #1976d2;
}

.new-conversation-btn {
    margin-top: 12px;
    padding: 8px 16px;
    background: #2196F3;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
}

.new-conversation-btn:hover {
    background: #1976d2;
}

.error-icon {
    margin-inline-start: 12px;
    color: #721c24;
    flex-shrink: 0;
}

.error-icon svg {
    width: 24px;
    height: 24px;
}

.error-text strong {
    display: block;
    margin-bottom: 8px;
    color: #721c24;
}

.error-text p {
    margin: 4px 0;
    color: #856404;
}

/* Toast notification */
.toast {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    padding: 12px 24px;
    border-radius: 8px;
    background: #333;
    color: white;
    z-index: 1000;
    animation: slideUp 0.3s ease;
}

.toast.warning {
    background: #f57c00;
}

.toast.error {
    background: #dc2626;
}

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

/* Loading spinner */
.loading-indicator {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 24px;
    color: #666;
}

.loading-spinner {
    width: 24px;
    height: 24px;
    border: 3px solid #e0e0e0;
    border-top-color: #2196F3;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Enhanced typing indicator */
.typing-dots {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
}

.typing-dots span {
    width: 8px;
    height: 8px;
    background: #999;
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out;
}

.typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.typing-dots span:nth-child(2) { animation-delay: -0.16s; }

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

/* Markdown content styling */
.message-content p {
    margin: 0 0 0.5em 0;
}

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

.message-content ul,
.message-content ol {
    margin: 0.5em 0;
    padding-inline-end: 1.5em;
    padding-inline-start: 0;
    list-style-position: inside;
}

.message-content li {
    margin: 0.25em 0;
}

.message-content code {
    background: rgba(0, 0, 0, 0.08);
    padding: 0.1em 0.3em;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    font-size: 0.9em;
}

.message-content pre {
    background: #1e1e1e;
    color: #d4d4d4;
    padding: 0.75em 1em;
    border-radius: 8px;
    overflow-x: auto;
    margin: 0.5em 0;
}

.message-content pre code {
    background: none;
    padding: 0;
    color: inherit;
}

.message-content strong {
    font-weight: 600;
}

.message-content a {
    color: #7c3aed;
    text-decoration: underline;
}

.message-content blockquote {
    border-inline-end: 3px solid #7c3aed;
    margin: 0.5em 0;
    padding-inline-end: 1em;
    color: #6b7280;
}
