:root {
    --background-primary: #121212;
    --background-secondary: #1E1E1E;
    --background-tertiary: #2A2A2A;
    --text-primary: #E0E0E0;
    --text-secondary: #A0A0A0;
    --accent-primary: #8A2BE2; /* BlueViolet */
    --accent-primary-hover: #7B24CB;
    --border-color: #333333;
    --shadow-color: rgba(0, 0, 0, 0.5);
    --danger-color: #E53935;
    --danger-color-hover: #C62828;

    --font-family: 'Vazirmatn', sans-serif;
    --border-radius: 12px;
    --transition-speed: 0.3s;
}

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

body {
    font-family: var(--font-family);
    background-color: var(--background-primary);
    color: var(--text-primary);
    line-height: 1.7;
    direction: rtl;
    font-feature-settings: "calt", "liga", "case", "ss01";
}

#app {
    width: 100%;
    min-height: 100vh;
    transition: filter var(--transition-speed) ease;
}

/* --- General Button Styles --- */
.icon-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 50%;
    transition: color var(--transition-speed), background-color var(--transition-speed);
}
.icon-btn svg {
    width: 28px;
    height: 28px;
}
.icon-btn:hover {
    color: var(--text-primary);
    background-color: var(--background-tertiary);
}

.text-btn {
    background: none;
    border: none;
    color: var(--accent-primary);
    cursor: pointer;
    font-family: inherit;
    font-size: 1rem;
    padding: 0.5rem 1rem;
    border-radius: var(--border-radius);
    transition: background-color var(--transition-speed);
}
.text-btn:hover {
    background-color: rgba(138, 43, 226, 0.1);
}


/* --- Views --- */
.view {
    display: none;
    flex-direction: column;
    animation: fadeIn 0.5s ease-in-out;
    padding: 1.5rem;
}
.view.active {
    display: flex;
}

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

/* --- App Header --- */
.app-header {
    text-align: center;
    padding: 1rem 0 2rem 0;
}
.app-header h1 {
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--text-primary);
}
.app-header p {
    font-size: 1.1rem;
    color: var(--text-secondary);
}

.library-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: right;
}

/* --- API Key Setup & Settings View --- */
#api-key-setup-view, #settings-view {
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}

.api-key-form-container, .settings-form-container {
    background-color: var(--background-secondary);
    border-radius: var(--border-radius);
    padding: 2rem;
    max-width: 600px;
    width: 100%;
    box-shadow: 0 4px 20px var(--shadow-color);
    text-align: center;
}

.settings-form-container {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    text-align: right;
}
.settings-form-container h2 {
    text-align: center;
    color: var(--text-primary);
}
.settings-form-container p {
    text-align: center;
    color: var(--text-secondary);
    margin-bottom: 1rem;
}
.settings-form-container .text-btn {
    align-self: center;
    margin-top: 1rem;
}


.api-key-instructions {
    text-align: right;
    margin-bottom: 1.5rem;
}

.api-key-instructions h3 {
    font-size: 1.3rem;
    color: var(--text-primary);
    margin-bottom: 1rem;
}

.api-key-instructions ol {
    list-style-position: inside;
    padding-right: 1.5rem;
    margin-bottom: 1rem;
}

.api-key-instructions li {
    margin-bottom: 0.5rem;
    color: var(--text-secondary);
}

.api-key-instructions a {
    color: var(--accent-primary);
    text-decoration: none;
}

.api-key-instructions a:hover {
    text-decoration: underline;
}

.warning-text {
    color: var(--danger-color);
    font-weight: 500;
    margin-top: 1rem;
}

.api-key-input-section {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

#api-key-input, #settings-api-key-input {
    width: 100%;
    padding: 0.8rem 1rem;
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
    background-color: var(--background-tertiary);
    color: var(--text-primary);
    font-family: var(--font-family);
    font-size: 1rem;
    text-align: left; /* For LTR API Key */
    direction: ltr;
}

#api-key-input:focus, #settings-api-key-input:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px rgba(138, 43, 226, 0.3);
}

#verify-api-key-btn, #save-settings-btn {
    background-color: var(--accent-primary);
    color: white;
    border: none;
    padding: 0.8rem 1.5rem;
    border-radius: var(--border-radius);
    font-family: inherit;
    font-size: 1.1rem;
    cursor: pointer;
    transition: background-color var(--transition-speed);
}

#verify-api-key-btn:hover, #save-settings-btn:hover {
    background-color: var(--accent-primary-hover);
}

.error-message {
    color: var(--danger-color);
    font-size: 0.9rem;
    margin-top: 0.5rem;
}
.error-message.hidden { display: none; }

.support-link-container {
    margin-top: 1.5rem;
    font-size: 0.95rem;
    color: var(--text-secondary);
}

.mmd-link {
    color: var(--accent-primary);
    text-decoration: none;
    font-weight: 600;
    transition: color var(--transition-speed);
}

.mmd-link:hover {
    color: var(--accent-primary-hover);
    text-decoration: underline;
}

/* --- Library View --- */
.book-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 1.5rem;
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
}

.book-card {
    background-color: var(--background-secondary);
    border-radius: var(--border-radius);
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 20px var(--shadow-color);
    transition: transform var(--transition-speed), box-shadow var(--transition-speed);
    cursor: pointer;
}
.book-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 30px var(--shadow-color);
}

.book-card-content .book-title {
    font-size: 1.4rem;
    font-weight: 600;
    margin-bottom: 0.5rem;
    color: var(--text-primary);
}
.book-card-content .book-progress {
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.book-card-actions {
    margin-top: 1.5rem;
    align-self: flex-end;
}

.delete-book-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 50%;
    transition: color var(--transition-speed), background-color var(--transition-speed);
}
.delete-book-btn svg {
    width: 24px;
    height: 24px;
}
.delete-book-btn:hover {
    color: var(--danger-color);
    background-color: rgba(229, 57, 53, 0.1);
}

.empty-state {
    text-align: center;
    margin-top: 4rem;
    color: var(--text-secondary);
}
.empty-state.hidden { display: none; }
.empty-state svg {
    width: 80px;
    height: 80px;
    margin-bottom: 1.5rem;
    stroke-width: 1;
}
.empty-state h2 {
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
    color: var(--text-primary);
}

/* --- Reader View --- */
#reader-view {
    height: 100vh;
    padding: 0;
    background-color: var(--background-primary);
}

.reader-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    background-color: var(--background-secondary);
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    z-index: 10;
}

#back-to-library-btn {
    background: none;
    border: none;
    color: var(--text-primary);
    cursor: pointer;
}
#back-to-library-btn svg { width: 24px; height: 24px; }

#reader-book-title {
    font-size: 1.2rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#page-indicator {
    font-size: 1rem;
    color: var(--text-secondary);
    min-width: 100px;
    text-align: left;
}

#book-content-wrapper {
    flex-grow: 1;
    overflow-y: auto;
    padding: 2rem;
}

.page-content {
    max-width: 800px;
    margin: 0 auto;
    font-size: 1.2rem;
    line-height: 2.2;
    text-align: right;
}

.reader-footer {
    display: flex;
    justify-content: space-between;
    padding: 1rem 1.5rem;
    background-color: var(--background-secondary);
    border-top: 1px solid var(--border-color);
    position: sticky;
    bottom: 0;
}

.reader-footer button {
    background-color: var(--accent-primary);
    color: white;
    border: none;
    padding: 0.8rem 1.5rem;
    font-size: 1rem;
    font-family: inherit;
    border-radius: var(--border-radius);
    cursor: pointer;
    transition: background-color var(--transition-speed);
}
.reader-footer button:hover:not(:disabled) { background-color: var(--accent-primary-hover); }
.reader-footer button:disabled { background-color: var(--background-tertiary); color: var(--text-secondary); cursor: not-allowed; }

/* --- Floating Action Button --- */
.floating-action-button {
    position: fixed;
    bottom: 2rem;
    left: 2rem;
    width: 60px;
    height: 60px;
    background-color: var(--accent-primary);
    color: white;
    border: none;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 4px 20px var(--shadow-color);
    cursor: pointer;
    transition: transform var(--transition-speed), background-color var(--transition-speed);
    z-index: 100;
}
.floating-action-button:hover { 
    background-color: var(--accent-primary-hover);
    transform: scale(1.1);
}
.floating-action-button svg { width: 30px; height: 30px; }

/* --- Loading Overlay --- */
#loading-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(5px);
    display: flex; flex-direction: column; justify-content: center; align-items: center;
    z-index: 1000; transition: opacity 0.3s;
}
#loading-overlay.hidden { opacity: 0; pointer-events: none; }

.spinner {
    border: 4px solid var(--background-tertiary);
    border-top: 4px solid var(--accent-primary);
    border-radius: 50%;
    width: 50px; height: 50px;
    animation: spin 1s linear infinite;
    margin-bottom: 1rem;
}

#loading-message { font-size: 1.1rem; color: var(--text-primary); }

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* --- Responsive Design --- */
@media (max-width: 768px) {
    .view { padding: 1rem; }
    .app-header h1 { font-size: 2rem; }
    .app-header p { font-size: 1rem; }
    .book-grid { grid-template-columns: 1fr; gap: 1rem; }
    #reader-view { padding: 0; }
    .reader-header { padding: 0.75rem 1rem; }
    #reader-book-title { font-size: 1rem; }
    #page-indicator { font-size: 0.9rem; min-width: 80px; }
    #book-content-wrapper { padding: 1.5rem 1rem; }
    .page-content { font-size: 1.1rem; line-height: 2; }
    .reader-footer { padding: 0.75rem 1rem; }
    .floating-action-button { bottom: 1rem; left: 1rem; }
    .app-footer { font-size: 0.8rem; padding: 0.75rem 1rem; }
}

.app-footer {
    text-align: center;
    padding: 1rem;
    background-color: var(--background-secondary);
    border-top: 1px solid var(--border-color);
    color: var(--text-secondary);
    font-size: 0.9rem;
    position: relative; /* Changed from fixed to relative to be at the bottom of the content */
    bottom: 0;
    width: 100%;
}
.app-footer .mmd-link {
    color: var(--accent-primary);
    text-decoration: none;
    font-weight: 600;
    transition: color var(--transition-speed);
}
.app-footer .mmd-link:hover {
    color: var(--accent-primary-hover);
    text-decoration: underline;
}
