:root {
    --primary: #007bff;
    --danger: #ff4d4f;
    --bg-color: #f4f7f9;
    --sidebar-width: 300px;
    --history-width: 260px;
    --msg-user: #e3f2fd;
    --msg-ai: #ffffff;
    --base-font-size: 16px;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: -apple-system, system-ui, sans-serif;
    font-size: var(--base-font-size);
    background-color: var(--bg-color);
    display: flex;
    height: 100dvh;
    margin: 0;
    overflow: hidden;
}

/* --- Left Sidebar --- */
#sidebar {
    width: var(--sidebar-width);
    background: #fff;
    border-right: 1px solid #e1e4e8;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
    z-index: 20;
}

.sidebar-header {
    padding: 15px;
    border-bottom: 1px solid #eee;
    font-weight: bold;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #fafafa;
}

#tree-container {
    flex: 1;
    overflow-y: auto;
    padding: 10px;
    font-size: calc(var(--base-font-size) - 2px);
}

.sidebar-footer {
    padding: 15px;
    border-top: 1px solid #eee;
    background: #fafafa;
}

.upload-btn {
    width: 100%;
    padding: 10px;
    background: #eef6ff;
    color: var(--primary);
    border: 1px dashed var(--primary);
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
    font-size: calc(var(--base-font-size) - 3px);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: 0.2s;
}

.upload-btn:hover {
    background: var(--primary);
    color: #fff;
}

details {
    margin-bottom: 5px;
}

summary {
    cursor: pointer;
    padding: 8px;
    border-radius: 6px;
    list-style: none;
    display: flex;
    align-items: center;
    font-weight: 500;
    color: #333;
}

summary::before {
    content: '▸';
    display: inline-block;
    width: 1.2em;
    color: #999;
}

details[open]>summary::before {
    transform: rotate(90deg);
}

.chap-item {
    padding: 8px 12px 8px 40px;
    cursor: pointer;
    border-radius: 6px;
    color: #444;
    font-size: calc(var(--base-font-size) - 3px);
    border-left: 2px solid transparent;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chap-item.active {
    background: #eef6ff;
    font-weight: 600;
    color: var(--primary);
    border-left-color: var(--primary);
}

/* --- Community Sidebar UI --- */
.community-node {
    display: none;
    /* deprecated, replaced by explorer-card */
}

/* --- Unified Explorer Sidebar UI --- */
.explorer-card {
    border: 1px solid #e1e4e8;
    border-radius: 8px;
    margin-bottom: 8px;
    background: #fff;
    transition: all 0.25s ease;
    overflow: hidden;
    list-style: none;
}

.explorer-card[open] {
    border-color: var(--primary);
    box-shadow: 0 4px 12px rgba(0, 123, 255, 0.08);
}

.explorer-summary {
    padding: 10px 12px;
    background: #fdfdfd;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: calc(var(--base-font-size) - 2.5px);
    color: #444;
    cursor: pointer;
    list-style: none;
}

.explorer-summary::-webkit-details-marker {
    display: none;
}

.explorer-summary:hover {
    background: #f8f9fa;
}

.explorer-card[open] .explorer-summary {
    background: #f0f7ff;
    border-bottom: 1px solid #eef2f7;
    color: var(--primary);
}

.explorer-label {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    flex: 1;
    min-width: 0;
}

.explorer-name-text {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.explorer-badge {
    background: var(--danger);
    color: white;
    padding: 1px 6px;
    border-radius: 10px;
    font-size: calc(var(--base-font-size) - 6px);
    font-weight: 800;
    margin-right: 8px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    flex-shrink: 0;
}

.explorer-card[open] .explorer-badge {
    display: none;
}

.explorer-actions {
    display: flex;
    gap: 5px;
    align-items: center;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.explorer-summary:hover .explorer-actions {
    opacity: 1;
}

.comm-icon-btn {
    background: #fff;
    border: 1px solid #e1e4e8;
    border-radius: 5px;
    width: 26px;
    height: 26px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: calc(var(--base-font-size) - 5px);
    color: #666;
    transition: all 0.2s;
}

.comm-icon-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.comm-icon-btn.leave:hover {
    border-color: var(--danger);
    color: var(--danger);
}

.explorer-tree-content {
    padding: 8px 0 8px 10px;
    background: #fff;
}

.chap-item-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.delete-chap-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: #aaa;
    font-size: calc(var(--base-font-size) - 2px);
    padding: 2px 5px;
    display: inline-block;
}

.delete-chap-btn:hover {
    color: var(--danger);
}

/* --- Right Sidebar --- */
#history-sidebar {
    width: var(--history-width);
    background: #fff;
    border-left: 1px solid #e1e4e8;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
    z-index: 20;
}

#history-container {
    flex: 1;
    overflow-y: auto;
}

.history-item {
    padding: 10px 15px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.03);
    cursor: pointer;
    transition: all 0.2s;
    border-left: 3px solid transparent;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.history-item:hover {
    background: #f8fafc;
}

.history-item.active {
    background: #f1f5f9;
    border-left-color: var(--primary);
}

.history-group-header {
    padding: 15px 15px 5px;
    font-size: calc(var(--base-font-size) - 5px);
    text-transform: uppercase;
    font-weight: 700;
    color: #94a3b8;
    letter-spacing: 0.8px;
    background: #fff;
    position: sticky;
    top: 0;
    z-index: 5;
}

.history-content {
    flex: 1;
    min-width: 0;
}

.history-title-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 2px;
}

.history-title {
    font-weight: 600;
    font-size: calc(var(--base-font-size) - 3px);
    color: #1e293b;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}

.history-meta {
    font-size: calc(var(--base-font-size) - 5px);
    color: #64748b;
    display: flex;
    align-items: center;
    gap: 6px;
}

.history-badge {
    font-size: calc(var(--base-font-size) - 6px);
    padding: 1px 4px;
    border-radius: 4px;
    background: #f1f5f9;
    color: #64748b;
    font-weight: 600;
}

.subject-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #cbd5e1;
    flex-shrink: 0;
}

.delete-session-btn {
    background: #fee2e2;
    border: none;
    color: #ef4444;
    cursor: pointer;
    font-size: calc(var(--base-font-size) - 4px);
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    opacity: 0;
    transition: 0.2s;
}

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

/* On mobile there's no hover — always show delete button */
@media (max-width: 900px) {
    .delete-session-btn {
        opacity: 1;
        background: #fee2e2;
    }
}

.delete-session-btn:hover {
    background: #ffe6e6;
    color: var(--danger);
}

/* --- Main Content --- */
#main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    min-width: 0;
}

header {
    background: #fff;
    padding: 12px 16px;
    border-bottom: 1px solid #e1e4e8;
    display: flex;
    flex-direction: column;
    gap: 12px;
    z-index: 10;
    box-shadow: 0 2px 5px rgba(0,0,0,0.04);
}

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

.header-title {
    font-weight: bold;
    font-size: calc(var(--base-font-size) + 2px);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}

#chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    max-width: 950px;
    margin: 0 auto;
    width: 100%;
}

.message {
    padding: 14px 20px;
    border-radius: 20px;
    font-size: var(--base-font-size);
    line-height: 1.6;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.btn-action {
    background: #ffffff;
    border: 1.5px solid #e5e7eb;
    padding: 8px 16px;
    border-radius: 10px;
    font-size: calc(var(--base-font-size) - 2px);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    color: #374151;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.btn-action:hover {
    background: #f9fafb;
    border-color: #d1d5db;
    transform: translateY(-1px);
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

.btn-action:active {
    transform: translateY(0);
}

#stop-tts {
    background: #fff1f2;
    color: #e11d48;
    border-color: #fecdd3;
}

#stop-tts:hover {
    background: #ffe4e6;
    border-color: #fda4af;
}

@media (max-width: 900px) {
    .message {
        font-size: calc(var(--base-font-size) + 2px);
        padding: 14px 18px;
    }

    .header-title {
        font-size: 1.1rem;
    }

    .btn-action {
        padding: 10px 14px;
        font-size: calc(var(--base-font-size) - 2px);
        border-radius: 12px;
    }

    .input-container {
        padding: 8px;
        border-radius: 24px;
    }
    
    textarea {
        font-size: 18px;
    }
}

header {
    background: #fff;
    padding: 12px 16px;
    border-bottom: 1px solid #e1e4e8;
    display: flex;
    flex-direction: column;
    gap: 12px;
    z-index: 10;
    box-shadow: 0 2px 4px rgba(0,0,0,0.02);
}

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

.header-title {
    font-weight: 700;
    font-size: calc(var(--base-font-size) + 2px);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    color: #1a1a1a;
}

#chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
}

.message {
    padding: 14px 18px;
    border-radius: 20px;
    font-size: var(--base-font-size);
    line-height: 1.6;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

.btn-action {
    background: #fff;
    border: 1px solid #d1d5db;
    padding: 8px 16px;
    border-radius: 8px;
    font-size: calc(var(--base-font-size) - 2px);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    color: #374151;
    display: flex;
    align-items: center;
    gap: 6px;
}

.btn-action:hover {
    background: #f9fafb;
    border-color: #9ca3af;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

#stop-tts {
    background: #fef2f2;
    color: #dc2626;
    border-color: #fecaca;
}

#stop-tts:hover {
    background: #fee2e2;
    border-color: #f87171;
}

.btn-retry {
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid #f87171;
    display: inline-flex;
    padding: 4px 12px;
    border-radius: 15px;
    font-size: 0.85rem;
    cursor: pointer;
    margin-top: 8px;
    font-weight: 600;
    transition: all 0.2s;
    align-items: center;
    gap: 5px;
}

.btn-retry:hover {
    background: #fecaca;
    border-color: #ef4444;
    transform: translateY(-1px);
}

.btn-retry:active {
    transform: translateY(0);
}

@media (max-width: 900px) {
    .message {
        font-size: 17px;
        padding: 12px 16px;
    }

    .header-title {
        font-size: 16px;
    }

    .btn-action {
        padding: 6px 12px;
        font-size: calc(var(--base-font-size) - 3px);
    }
}

header {
    background: #fff;
    padding: 8px 12px;
    border-bottom: 1px solid #e1e4e8;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 10;
}

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

.header-title {
    font-weight: 700;
    font-size: calc(var(--base-font-size) + 4px);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    color: #111827;
}

#chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
    display: flex;
    flex-direction: column;
    max-width: 900px;
    margin: 0 auto;
    width: 100%;
}

.message-wrapper {
    display: flex;
    flex-direction: column;
    margin-bottom: 15px;
    max-width: 88%;
}

.user-wrapper {
    align-self: flex-end;
    align-items: flex-end;
}

.assistant-wrapper {
    align-self: flex-start;
    align-items: flex-start;
    width: 100%;
}

.message {
    padding: 14px 20px;
    border-radius: 20px;
    font-size: var(--base-font-size);
    line-height: 1.6;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.user {
    background-color: var(--msg-user);
    color: #1e293b;
    border-bottom-right-radius: 4px;
}

.assistant {
    background-color: var(--msg-ai);
    border: 1px solid #e1e4e8;
    border-bottom-left-radius: 4px;
}

.input-outer {
    padding: 10px 12px 20px;
    width: 100%;
    background: transparent;
}

.input-container {
    max-width: 800px;
    margin: 0 auto;
    background: #fff;
    padding: 6px;
    border-radius: 30px;
    display: flex;
    align-items: flex-end;
    gap: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    border: 1px solid #e1e4e8;
    flex-wrap: nowrap;
}

textarea {
    flex: 1;
    min-width: 0;
    padding: 10px 4px;
    border: none;
    outline: none;
    font-size: var(--base-font-size);
    resize: none;
    max-height: 120px;
    background: transparent;
    line-height: 1.4;
}

.icon-btn {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    background: #f0f2f5;
    flex-shrink: 0;
}

.icon-btn.recording {
    background: var(--danger);
    color: white;
    animation: pulse 1.5s infinite;
}

#send-btn {
    background: var(--primary);
    color: white;
    font-size: 18px;
}

.actions {
    display: flex;
    gap: 8px;
    margin-top: 5px;
    opacity: 0.8;
}

.btn-action {
    background: #fff;
    border: 1px solid #ddd;
    padding: 4px 10px;
    border-radius: 15px;
    font-size: calc(var(--base-font-size) - 1px);
    cursor: pointer;
}

#model-select,
#menu-toggle,
#history-toggle {
    display: none;
}

/* Modern Dropdown Menu (Chapter Tools) */
.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    right: 0;
    top: calc(100% + 8px);
    background-color: #ffffff;
    min-width: 220px;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
    z-index: 100;
    border-radius: 12px;
    border: 1px solid #e5e7eb;
    padding: 8px;
    transform-origin: top right;
    animation: dropdownFadeIn 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes dropdownFadeIn {
    from {
        opacity: 0;
        transform: scale(0.95) translateY(-10px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

.dropdown-content.show {
    display: block;
}

.dropdown-header {
    padding: 8px 12px;
    font-size: calc(var(--base-font-size) - 5px);
    font-weight: 700;
    color: #9ca3af;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.dropdown-divider {
    height: 1px;
    background-color: #f3f4f6;
    margin: 8px -8px;
}

.dropdown-content a {
    color: #374151;
    padding: 10px 12px;
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: calc(var(--base-font-size) - 2px);
    font-weight: 500;
    border-radius: 8px;
    transition: all 0.15s ease;
}

.dropdown-content a:hover {
    background-color: #f3f4f6;
    color: var(--primary);
    transform: translateX(4px);
}

.dropdown-content a.special-tool {
    color: var(--primary);
    font-weight: 600;
}

.dropdown-content a.special-tool:hover {
    background-color: #eff6ff;
}

/* Generic Modal Styles */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    z-index: 100;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(5px);
}

.modal {
    background: #fff;
    padding: 20px;
    border-radius: 12px;
    width: 95%;
    max-width: 600px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
}

.modal-header-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding: 5px 30px;
    background: #fff;
    border-bottom: 1px solid #eee;
    margin-top: -0px;
    margin-left: -0px;
    margin-right: -0px;
    box-shadow: 0 2px 15px rgba(0,0,0,0.04);
}

.modal-header-container h3 {
    margin: 0;
    color: #333;
}

.modal-header-container .btn-secondary {
    padding: 6px 12px;
    font-size: 1.1rem;
    line-height: 1;
    border-radius: 8px;
}

.modal-group {
    display: flex;
    flex-direction: column;
    gap: 5px;
}

.modal-group label {
    font-size: 13px;
    font-weight: 600;
    color: #555;
}

.modal input[type="text"],
.modal input[type="file"],
.modal select {
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 14px;
    width: 100%;
    background: #fff;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 20px;
}

.btn-primary {
    background: var(--primary);
    color: white;
    border: none;
    padding: 10px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
}

.btn-primary:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.btn-subtle-primary {
    background: #eef6ff;
    color: var(--primary);
    border: 1px solid #d0e8ff;
    padding: 6px 14px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-subtle-primary:hover {
    background: var(--primary);
    color: #fff;
}

.btn-subtle-success {
    background: #e6f4ea;
    color: #1e7e34;
    border: 1px solid #ccead1;
    padding: 6px 14px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-subtle-success:hover {
    background: #28a745;
    color: #fff;
}

.btn-secondary {
    background: #f0f2f5;
    color: #333;
    border: none;
    padding: 10px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 600;
}

/* --- Upload Modal & Tabs --- */
.upload-tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    background: #f0f2f5;
    padding: 4px;
    border-radius: 10px;
}

.upload-tab {
    flex: 1;
    padding: 10px;
    text-align: center;
    cursor: pointer;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    color: #666;
    transition: all 0.2s;
}

.upload-tab.active {
    background: #fff;
    color: var(--primary);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.upload-section {
    display: none;
    flex-direction: column;
    gap: 15px;
    animation: fadeIn 0.3s ease;
}

.upload-section.active {
    display: flex;
}

/* --- Floating OCR Progress --- */
#ocr-floating-widget {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    width: 90%;
    max-width: 400px;
    background: rgba(255, 255, 255, 0.98);
    border: 1px solid #e1e4e8;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
    border-radius: 12px;
    padding: 15px;
    z-index: 2000;
    display: none;
    backdrop-filter: blur(10px);
    animation: slideUp 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes slideUp {
    from {
        bottom: -100px;
        opacity: 0;
    }

    to {
        bottom: 20px;
        opacity: 1;
    }
}

.progress-container {
    width: 100%;
    height: 6px;
    background: #eee;
    border-radius: 3px;
    overflow: hidden;
    margin-top: 10px;
}

.progress-bar {
    height: 100%;
    background: var(--primary);
    width: 0%;
    transition: width 0.3s ease;
}

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

    100% {
        transform: rotate(360deg);
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

/* --- Flashcard Modal --- */
#flashcard-modal-overlay .modal {
    max-width: 500px;
    text-align: center;
}

.flashcard-container {
    perspective: 1000px;
    margin-bottom: 10px;
}

.flashcard-inner {
    position: relative;
    width: 100%;
    height: 300px;
    transition: transform 0.6s;
    transform-style: preserve-3d;
    cursor: pointer;
}

.flashcard-front,
.flashcard-back {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px;
    border-radius: 12px;
    border: 1px solid #ddd;
    background: #fff;
    overflow-y: auto;
    overflow-x: hidden;
    word-wrap: break-word;
}

/* THIS IS THE CRITICAL LINE THAT WAS MISSING! */
.flashcard-back {
    transform: rotateY(180deg);
}

.flashcard-inner.is-flipped {
    transform: rotateY(180deg);
}

/* Flexbox magic: center short text, allow long text to scroll from top */
.flashcard-front h2 {
    margin: auto 0;
    width: 100%;
}

/* .flashcard-back p { margin: auto 0; width: 100%; font-size: 22px; line-height: 1.5; text-align: center; } */
/* Apply font size to all text elements on the back */
.flashcard-back>* {
    font-size: 22px;
    line-height: 1.5;
    text-align: center;
    margin-top: 10px;
    margin-bottom: 10px;
}

/* Specific handling for lists to keep them looking good */
.flashcard-back ul,
.flashcard-back ol {
    text-align: left;
    display: inline-block;
    /* Helps with centering the list block */
    margin: auto;
}

.flashcard-back li {
    font-size: 22px;
    margin-bottom: 8px;
}

/* Auto margin ensures centering for all block types */
.flashcard-front h2,
.flashcard-back>* {
    margin-left: auto;
    margin-right: auto;
}

/* Custom Scrollbars for cards */
.flashcard-front::-webkit-scrollbar,
.flashcard-back::-webkit-scrollbar {
    width: 6px;
}

.flashcard-front::-webkit-scrollbar-thumb,
.flashcard-back::-webkit-scrollbar-thumb {
    background-color: #ccc;
    border-radius: 3px;
}

/* --- Dictation Mode Specifics --- */
.flashcard-inner.mode-dictation.is-testing .flashcard-front h2 {
    color: #fff;
    user-select: none;
}

.dictation-hint {
    display: none;
    color: var(--primary);
    font-size: 13px;
    font-weight: 600;
    margin-bottom: 15px;
    text-align: center;
}

.flashcard-nav-info {
    color: #888;
    font-size: 13px;
    margin-bottom: 15px;
}

.flashcard-controls {
    display: flex;
    flex-direction: column;
    gap: 15px;
    align-items: center;
}

.flashcard-controls .btn-group {
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}

#test-controls .answer-input-group {
    display: flex;
    width: 100%;
    gap: 6px;
}

#test-controls textarea {
    flex: 1;
    min-height: 80px;
    resize: vertical;
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-family: inherit;
}

#evaluation-result {
    min-height: 40px;
    margin-top: 10px;
    font-size: 14px;
}

@media (max-width: 900px) {

    #sidebar,
    #history-sidebar {
        position: absolute;
        height: 100%;
    }

    #sidebar {
        transform: translateX(-100%);
    }

    #sidebar.open {
        transform: translateX(0);
    }

    #history-sidebar {
        right: 0;
        transform: translateX(100%);
    }

    #history-sidebar.open {
        transform: translateX(0);
    }

    #menu-toggle,
    #history-toggle {
        display: flex;
    }

    .header-title {
        font-size: 14px;
        margin-right: 5px;
    }

    select {
        font-size: 11px;
        max-width: 100px;
    }

    /* HIDE THE ENTIRE SECOND ROW ON MOBILE */
    .voice-controls-row {
        display: none !important;
    }

    .dropdown .btn-action {
        padding: 4px 6px;
    }

    .dropdown .btn-action .desktop-only {
        display: none;
    }

    /* Adjust Stop button for mobile tightly */
    #stop-tts {
        padding: 4px 6px;
        font-size: 11px;
        white-space: nowrap;
    }
}

@keyframes pulse {
    0% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.05);
    }

    100% {
        transform: scale(1);
    }
}

/* Sync Status Indicator */
.status-dot {
    height: 10px;
    width: 10px;
    border-radius: 50%;
    display: inline-block;
    background-color: var(--danger);
}

.status-dot.active {
    background-color: var(--success);
    box-shadow: 0 0 8px var(--success);
}

.status-dot.listening {
    background-color: #ffc107;
    box-shadow: 0 0 8px #ffc107;
}

/* Sync Modal Specifics */
.sync-code-display {
    font-size: 28px;
    font-weight: bold;
    letter-spacing: 4px;
    text-align: center;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 8px;
    border: 2px dashed #ccc;
    margin-bottom: 20px;
    color: var(--primary);
}

.sync-divider {
    text-align: center;
    margin: 20px 0;
    position: relative;
}

.sync-divider::before {
    content: "";
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    border-top: 1px solid #ddd;
    z-index: 1;
}

.sync-divider span {
    background: #fff;
    padding: 0 10px;
    color: #888;
    font-size: 12px;
    position: relative;
    z-index: 2;
}

/* OCR Processing Overlay */
.modal {
    position: relative;
}

/* Toast Message Styles */
.toast-container {
    position: fixed;
    top: 20px;
    left: 0;
    right: 0;
    z-index: 10000;
    display: flex;
    justify-content: center;
    pointer-events: none;
}

.toast {
    pointer-events: all;
    min-width: 300px;
    max-width: 80vw;
    padding: 16px 24px;
    border-radius: 8px;
    color: white;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 12px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    margin: 0 10px;
    transform: translateY(-100%);
    opacity: 0;
}

.toast.show {
    animation: slideDown 0.3s ease-out forwards, slideUp 0.3s ease-in var(--toast-delay, 3s) forwards;
    animation-delay: 0s, var(--toast-delay, 3s);
}

.toast-success {
    background-color: var(--success, #10b981);
}

.toast-error {
    background-color: var(--danger, #ef4444);
}

.toast-info {
    background-color: var(--primary, #3b82f6);
}

.toast-warning {
    background-color: #f59e0b;
}

.toast-icon {
    font-size: 20px;
    flex-shrink: 0;
}

.toast-message {
    flex: 1;
}

@keyframes slideDown {
    from {
        transform: translateY(-100%);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

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

/* Ensure overlay stays inside the modal */

/* Live Tutor Modal Enhancements */
.live-tutor-modal-content {
    max-width: 600px;
    padding: 0;
    overflow: hidden;
    background: linear-gradient(135deg, #f0f4f8, #e0e7ed);
    border-radius: 18px;
    box-shadow: 0 15px 40px rgba(0, 0, 0, 0.25);
    border: none;
}

#live-tutor-setup-panel,
#live-tutor-live-panel {
    padding: 30px;
}

.live-tutor-modal-content h3 {
    color: #2c3e50;
    font-size: 1.8rem;
    font-weight: 700;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.live-tutor-modal-content h3 svg {
    font-size: 1.5em;
}

.live-tutor-modal-content p {
    font-size: 15px;
    color: #555;
    line-height: 1.6;
}

.live-tutor-modal-content label {
    font-size: 14px;
    font-weight: 600;
    color: #444;
    margin-bottom: 6px;
}

.live-tutor-modal-content input[type="password"],
.live-tutor-modal-content select {
    padding: 12px 15px;
    border: 1px solid #cdd5df;
    border-radius: 9px;
    font-size: 15px;
    background-color: #fcfdfe;
    transition: all 0.2s ease;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05);
}

.live-tutor-modal-content input[type="password"]:focus,
.live-tutor-modal-content select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.15);
    background-color: #fff;
}

.live-tutor-modal-content small {
    font-size: 11px;
    color: #777;
    margin-top: 5px;
    display: block;
}

#live-tutor-connect-btn,
#live-tutor-mute-btn,
#live-tutor-live-panel .btn-primary {
    padding: 12px 25px;
    border-radius: 10px;
    font-size: 16px;
    font-weight: 700;
    box-shadow: 0 4px 15px rgba(0, 123, 255, 0.2);
    transition: all 0.2s ease;
}

#live-tutor-connect-btn:hover,
#live-tutor-live-panel .btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(0, 123, 255, 0.3);
}

#live-tutor-live-panel .btn-primary {
    background: var(--danger);
    box-shadow: 0 4px 15px rgba(255, 77, 79, 0.2);
}

#live-tutor-live-panel .btn-primary:hover {
    box-shadow: 0 6px 20px rgba(255, 77, 79, 0.3);
}

#live-tutor-status {
    background: #e9ecef;
    border: 1px solid #dee2e6;
    color: #495057;
    padding: 12px 15px;
    border-radius: 10px;
    font-size: 14px;
    text-align: center;
    font-weight: 500;
}

#live-tutor-viz {
    border: 1px solid #dee2e6;
    background: #f8f9fa;
    border-radius: 10px;
}

#live-tutor-transcript {
    background: #ffffff;
    border: 1px solid #e1e4e8;
    border-radius: 10px;
    padding: 10px 8px;
    height: 250px;
    max-height: 400px;
    font-size: 1.25rem;
    line-height: 1.6;
    color: #333;
    box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05);
    overflow-y: auto;
}

#live-tutor-transcript div {
    color: #888;
    text-align: center;
    padding: 10px;
}

#live-tutor-transcript .live-tutor-msg {
    margin-bottom: 2px;
    padding: 0;
}

#live-tutor-transcript .live-tutor-msg-label {
    font-size: 12px;
    text-transform: uppercase;
    opacity: 0.7;
    padding: 10px 0 10px 0;
    margin: 0;
    font-weight: 600;
}

#live-tutor-transcript .live-tutor-msg-text {
    font-size: 18px;
    line-height: 1.6;
    padding: 0;
    margin: 0;
}

#live-tutor-mute-btn {
    background: #f0f2f5;
    color: #333;
    border: 1px solid #ccc;
    width: 60px;
    height: 60px;
    font-size: 24px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

#live-tutor-mute-btn.active {
    background: var(--danger);
    color: white;
    box-shadow: 0 4px 15px rgba(255, 77, 79, 0.2);
}

#live-tutor-mute-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

#live-tutor-mute-btn.active:hover {
    box-shadow: 0 6px 20px rgba(255, 77, 79, 0.3);
}

.ocr-processing-view {
    display: none;
    /* Hidden by default */
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.95);
    z-index: 20;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border-radius: 12px;
    padding: 20px;
    text-align: center;
}

.ocr-spinner {
    width: 50px;
    height: 50px;
    border: 5px solid #f3f3f3;
    border-top: 5px solid var(--primary);
    border-radius: 50%;
    animation: ocr-spin 1s linear infinite;
    margin-bottom: 20px;
}

@keyframes ocr-spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.processing-title {
    font-weight: bold;
    color: var(--primary);
    font-size: 18px;
    margin-bottom: 10px;
}

.message-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    margin-top: 5px;
    width: 100%;
}

.message-time {
    font-size: 10px;
    color: #999;
    white-space: nowrap;
}

/* Adjust actions container to work with the footer layout */
.actions {
    display: flex;
    align-items: center;
    /* Aligns buttons and time vertically */
    gap: 6px;
    margin-top: 4px;
    opacity: 0.8;
}

.btn-action {
    background: #fff;
    border: 1px solid #ddd;
    padding: 2px 8px;
    /* Reduced padding */
    border-radius: 12px;
    font-size: calc(var(--base-font-size) - 4px);
    /* Smaller font */
    cursor: pointer;
    white-space: nowrap;
}

.message-time {
    font-size: 10px;
    /* Very small, subtle font */
    color: #aaa;
    margin-left: 4px;
    white-space: nowrap;
}

/* Community chapter styling */
.community-chapter {
    background: #e8f4ff !important;
    border-left: 2px solid var(--primary) !important;
    color: #0056b3 !important;
}

.community-chapter .chap-item-name {
    color: #0056b3 !important;
}

.community-section {
    border-radius: 6px;
    overflow: hidden;
}

.community-details summary:hover {
    background: #f0f7ff;
}

.community-details details[open]>summary::before {
    transform: rotate(90deg);
}

.community-details details>summary::before {
    content: '▸';
    display: inline-block;
    width: 1.2em;
    color: #999;
}

.community-details details>summary {
    list-style: none;
    display: flex;
    align-items: center;
}