:root {
    /* New Luxurious Light Palette */
    --background-color: #f8f9fa;
    --surface-color: #ffffff;
    --primary-accent-color: #4263eb;
    --primary-accent-hover: #5c7cfa;
    --text-color: #212529;
    --muted-text-color: #6c757d;
    --border-color: #dee2e6;
    --error-color: #e03131;
    --success-color: #2f9e44;
    --favorite-color: #fcc419;
    --questioner-bubble-bg: #e9ecef;
    --answerer-bubble-bg: #dbe4ff;

    --font-family: 'Inter', sans-serif;
    --border-radius: 10px;
    --box-shadow: 0 8px 25px rgba(0, 0, 0, 0.07);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

body {
    font-family: var(--font-family);
    background-color: var(--background-color);
    color: var(--text-color);
    line-height: 1.7;
    direction: rtl;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transition: background-color 0.4s ease; /* Smooth transition for overlay */
}

body.history-open {
    overflow: hidden;
}

#app {
    max-width: 850px;
    margin: 0 auto;
    padding: 2.5rem 1.5rem;
    animation: fadeIn 0.8s ease-out;
    transition: transform 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

body[dir="rtl"].history-open #app {
    transform: translateX(-380px);
}

.hidden {
    display: none !important;
}

/* Modal */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(5px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.modal-overlay.show {
    opacity: 1;
    visibility: visible;
}

.modal-content {
    background: var(--surface-color);
    padding: 2.5rem;
    border-radius: var(--border-radius);
    box-shadow: var(--box-shadow);
    width: 90%;
    max-width: 500px;
    text-align: center;
    animation: fadeIn 0.5s ease-out forwards;
    border: 1px solid var(--border-color);
}

.modal-content h2 {
    margin-bottom: 1rem;
    font-weight: 700;
    color: var(--text-color);
    font-size: 1.8rem;
}

.modal-content p {
    color: var(--muted-text-color);
    margin-bottom: 1.5rem;
}

.input-group {
    display: flex;
    margin: 1.5rem 0;
}

.input-group input {
    flex-grow: 1;
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
    border-left: none;
}

.input-group button {
    border-top-right-radius: 0;
    border-bottom-right-radius: 0;
    background: var(--primary-accent-color);
    color: #ffffff;
}

.input-group button:hover {
    background: var(--primary-accent-hover);
}

.status-message {
    min-height: 24px;
    font-weight: 500;
    margin-top: 1rem;
}
.status-message.success { color: var(--success-color); }
.status-message.error { color: var(--error-color); }
.api-key-link { font-size: 0.9rem; color: var(--muted-text-color); }
.api-key-link a { color: var(--primary-accent-color); text-decoration: none; font-weight: 500; }

/* Header */
.main-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 2.5rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid var(--border-color);
}

.header-controls {
    display: flex;
    gap: 0.75rem;
    order: 1; /* For RTL, puts it on the left */
}

.header-text-content {
    order: 2; /* For RTL, puts it on the right */
    text-align: right;
}

.main-header h1 {
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--text-color);
    margin-bottom: 0.25rem;
}

.main-header p {
    font-size: 1.1rem;
    color: var(--muted-text-color);
}

#edit-api-key-btn, #open-history-btn, #new-chat-btn {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--muted-text-color);
    border-radius: 50%;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    cursor: pointer;
    flex-shrink: 0;
    transition: all 0.2s ease;
}

#edit-api-key-btn:hover:not(:disabled), #open-history-btn:hover:not(:disabled) {
    background-color: var(--surface-color);
    color: var(--primary-accent-color);
    border-color: var(--primary-accent-color);
    transform: translateY(-2px);
    box-shadow: 0 4px 10px rgba(66, 99, 235, 0.1);
}

#edit-api-key-btn svg, #open-history-btn svg {
    width: 20px;
    height: 20px;
}

/* Card */
.card {
    background: var(--surface-color);
    padding: 2rem;
    border-radius: var(--border-radius);
    margin-bottom: 2rem;
    border: 1px solid var(--border-color);
    box-shadow: var(--box-shadow);
}

/* Setup Section */
.setup-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1.5rem;
    margin-bottom: 1.5rem;
}

@media (min-width: 768px) {
    .setup-grid {
        grid-template-columns: 1fr 1fr;
    }
    .setup-grid .form-group:first-child {
        grid-column: 1 / -1;
    }
}

.form-group {
    display: flex;
    flex-direction: column;
}

.setup-actions {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 1rem;
    margin-top: 2rem;
}

label {
    margin-bottom: 0.5rem;
    font-weight: 500;
    color: var(--muted-text-color);
    font-size: 0.9rem;
}

input[type="text"],
input[type="password"],
select,
textarea {
    width: 100%;
    padding: 0.8rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    font-family: var(--font-family);
    font-size: 1rem;
    background-color: var(--background-color);
    color: var(--text-color);
    transition: border-color 0.2s, box-shadow 0.2s;
}

input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary-accent-color);
    box-shadow: 0 0 0 3px rgba(66, 99, 235, 0.2);
}

.custom-prompt-area {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-top: 1rem;
    padding: 1.5rem;
    border: 1px dashed var(--border-color);
    border-radius: var(--border-radius);
    background-color: var(--background-color);
}

textarea {
    min-height: 100px;
    resize: vertical;
}

/* Buttons */
button, .cta-button {
    padding: 0.85rem 1.75rem;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    font-family: var(--font-family);
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    text-align: center;
    background-color: var(--surface-color);
    color: var(--muted-text-color);
}

button:hover:not(:disabled), .cta-button:hover:not(:disabled) {
    transform: translateY(-2px);
    border-color: #c0c9d4;
    color: var(--text-color);
}

button:disabled {
    cursor: not-allowed;
    opacity: 0.5;
}

.cta-button {
    background: var(--primary-accent-color);
    color: #ffffff;
    font-weight: 700;
    border: none;
    box-shadow: 0 4px 15px rgba(66, 99, 235, 0.15);
}

.cta-button:hover:not(:disabled) {
     background: var(--primary-accent-hover);
     box-shadow: 0 6px 20px rgba(66, 99, 235, 0.25);
     color: #ffffff;
     border: none;
}

/* Chat Section */
#chat-section {
    animation: fadeIn 0.6s ease-out;
}
.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    padding: 0 0.5rem;
}

#chat-title {
    font-size: 1.5rem;
    font-weight: 700;
}

#progress-indicator {
    font-size: 0.9rem;
    font-weight: 500;
    background: var(--surface-color);
    color: var(--muted-text-color);
    padding: 0.4rem 1rem;
    border-radius: 20px;
    border: 1px solid var(--border-color);
}

#chat-container {
    height: 65vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    padding: 1.5rem;
    background-color: var(--background-color);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
}

.chat-message {
    display: flex;
    align-items: flex-start;
    gap: 1rem;
    max-width: 80%;
    animation: fadeIn 0.5s ease-out forwards;
}

.avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    flex-shrink: 0;
    background-color: var(--surface-color);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.75rem;
    border: 1px solid var(--border-color);
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.message-content {
    padding: 0.8rem 1.2rem;
    border-radius: var(--border-radius);
    flex-grow: 1;
}

.message-author {
    font-weight: 700;
    margin-bottom: 0.35rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.95rem;
}

.message-text {
    white-space: pre-wrap;
    word-wrap: break-word;
    color: var(--text-color);
}

.message-text .thinking-indicator {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px 0;
}

@keyframes dot-flashing {
    0% { background-color: var(--primary-accent-color); }
    50%, 100% { background-color: rgba(66, 99, 235, 0.3); }
}

.dot-flashing {
    position: relative;
    width: 8px;
    height: 8px;
    border-radius: 5px;
    background-color: var(--primary-accent-color);
    color: var(--primary-accent-color);
    animation: dot-flashing 1s infinite linear alternate;
    animation-delay: .5s;
}

.dot-flashing::before, .dot-flashing::after {
    content: '';
    display: inline-block;
    position: absolute;
    top: 0;
}

.dot-flashing::before {
    left: -15px;
    width: 8px;
    height: 8px;
    border-radius: 5px;
    background-color: var(--primary-accent-color);
    color: var(--primary-accent-color);
    animation: dot-flashing 1s infinite alternate;
    animation-delay: 0s;
}

.dot-flashing::after {
    left: 15px;
    width: 8px;
    height: 8px;
    border-radius: 5px;
    background-color: var(--primary-accent-color);
    color: var(--primary-accent-color);
    animation: dot-flashing 1s infinite alternate;
    animation-delay: 1s;
}

/* Message alignment & Styling */
.chat-message.questioner {
    align-self: flex-start;
}
.chat-message.questioner .message-content {
    background-color: var(--questioner-bubble-bg);
    border-top-right-radius: 0;
}
.chat-message.questioner .message-author {
    color: var(--primary-accent-color);
}

.chat-message.answerer {
    align-self: flex-end;
    flex-direction: row-reverse;
}
.chat-message.answerer .message-content {
    background: var(--answerer-bubble-bg);
    border-top-left-radius: 0;
}
.chat-message.answerer .message-author {
    color: #364fc7; /* Darker blue for answerer author */
}

/* Chat Controls */
.chat-controls-container {
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    margin-top: 1.5rem;
    justify-content: center;
}

#continue-chat-btn {
    background: var(--primary-accent-color);
    color: #ffffff;
    border: none;
}
#continue-chat-btn:hover:not(:disabled) {
    background: var(--primary-accent-hover);
    box-shadow: 0 4px 15px rgba(66, 99, 235, 0.15);
}

/* Save Menu */
.save-menu {
    position: relative;
    display: inline-block;
}
.save-options {
    display: none;
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background-color: var(--surface-color);
    min-width: 180px;
    box-shadow: var(--box-shadow);
    z-index: 10;
    border-radius: var(--border-radius);
    overflow: hidden;
    margin-bottom: 10px;
    border: 1px solid var(--border-color);
    animation: fadeIn 0.2s ease-out;
}
.save-options a {
    color: var(--muted-text-color);
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: right;
    font-size: 0.95rem;
    transition: all 0.2s ease;
}
.save-options a:hover {
    background-color: var(--background-color);
    color: var(--primary-accent-color);
}
.save-menu:hover .save-options {
    display: block;
}

/* History Panel */
#history-panel-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.3);
    z-index: 100;
    transition: opacity 0.4s ease;
}

#history-panel {
    position: fixed;
    top: 0;
    left: 0; /* Changed for LTR positioning in CSS, RTL handled by `dir` attribute */
    width: 380px;
    max-width: 90vw;
    height: 100%;
    background-color: var(--surface-color);
    box-shadow: 0 0 40px rgba(0,0,0,0.15);
    z-index: 101;
    display: flex;
    flex-direction: column;
    transform: translateX(-100%);
    transition: transform 0.4s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

body[dir="rtl"] #history-panel {
    left: auto;
    right: 0;
    transform: translateX(100%);
}

#history-panel.open {
    transform: translateX(0);
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.history-header h2 {
    font-size: 1.25rem;
}

#close-history-btn {
    background: none;
    border: none;
    font-size: 2rem;
    line-height: 1;
    color: var(--muted-text-color);
    cursor: pointer;
    padding: 0;
}

#history-list {
    flex-grow: 1;
    overflow-y: auto;
    padding: 1rem;
}

.empty-history-message {
    text-align: center;
    padding: 2rem;
    color: var(--muted-text-color);
}

.history-item {
    display: flex;
    gap: 1rem;
    padding: 1rem;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.history-item:last-child {
    border-bottom: none;
}

.history-item:hover {
    background-color: var(--background-color);
}

.history-item.favorite {
    background-color: #fff9e6;
}
body[dir="rtl"] .history-item.favorite {
    border-right: 4px solid var(--favorite-color);
}
body:not([dir="rtl"]) .history-item.favorite {
    border-left: 4px solid var(--favorite-color);
}

.history-item-main {
    flex-grow: 1;
}

.history-item-main h3 {
    font-size: 1.1rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
    color: var(--text-color);
}

.history-item-date {
    font-size: 0.8rem;
    color: var(--muted-text-color);
    margin-bottom: 0.5rem;
}

.history-item-preview {
    font-size: 0.9rem;
    color: var(--muted-text-color);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 200px;
}

.history-item-actions {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    align-items: center;
    justify-content: center;
}

.history-action-btn {
    background: none;
    border: none;
    color: var(--muted-text-color);
    padding: 0.4rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
}
.history-action-btn:hover {
    color: var(--text-color);
    background-color: #e9ecef;
}
.history-action-btn.favorite-btn.is-favorite {
    color: var(--favorite-color);
}


/* Responsive */
@media (max-width: 768px) {
    body {
      font-size: 15px;
    }
    #app {
        padding: 1.5rem 1rem;
    }
    .main-header {
        flex-direction: column-reverse; /* Put controls above text */
        align-items: flex-end;
        gap: 1rem;
    }
    .header-text-content {
        align-self: flex-end;
    }
    .main-header h1 {
        font-size: 2rem;
    }
    .card {
        padding: 1.5rem;
    }
    .chat-controls-container {
        flex-direction: column;
    }
    .chat-controls-container button, .save-menu {
        width: 100%;
    }
    .chat-message {
        max-width: 95%;
    }

}
