/* Gallery */
.rag-gallery-container {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 24px;
    overflow-y: auto;
    background: var(--bg-main);
}

.rag-gallery-header {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 32px;
    position: relative;
}

.rag-gallery-header h2 {
    margin: 0;
    font-size: 24px;
    font-weight: 600;
    color: var(--text-primary);
    text-align: center;
}

.rag-add-space-btn {
    display: flex;
    align-items: center;
    position: absolute;
    right: 0;
    gap: 8px;
    padding: 10px 16px;
    background-color: var(--bg-button);
    color: var(--blue-accent);
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.rag-add-space-btn:hover {
    background-color: var(--bg-button-hover);
    transform: translateY(-1px);
}

.rag-space-section {
    margin-bottom: 40px;
}

.rag-space-section-heading {
    font-size: 14px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
    margin: 0 0 16px 0;
    opacity: 0.7;
}

.rag-space-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 320px), 1fr));
    gap: 20px;
}

.rag-space-card {
    background: var(--bg-panel);
    border-radius: 12px;
    padding: 24px;
    display: flex;
    flex-direction: column;
    transition: all 0.3s ease;
    position: relative;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    min-height: 280px;
    width: 100%;
}

.rag-space-card:hover {
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    transform: translateY(-4px);
}

.rag-space-card-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.rag-space-card-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.rag-space-card-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--blue-accent);
    margin: 0;
    text-align: center;
    line-height: 1.4;
}

.rag-space-card-stats {
    display: flex;
    justify-content: center;
    gap: 12px;
    font-size: 13px;
    color: var(--text-secondary);
}

.rag-space-card-creator {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    font-size: 13px;
    font-weight: 500;
    color: var(--text-secondary);
}

/* Edit view */
.rag-space-edit-container {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg-main);
    overflow: hidden;
}

.rag-space-edit-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 24px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-panel);
    position: relative;
}

.rag-back-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 0;
    background: none;
    color: var(--text-secondary);
    border: none;
    border-radius: 0;
    font-size: 14px;
    font-weight: 500;
    font-family: var(--font-family);
    cursor: pointer;
    transition: color 0.2s ease;
    white-space: nowrap;
    position: relative;
    z-index: 1;
    flex-shrink: 0;
}

.rag-back-btn:hover:not(:disabled) {
    color: var(--blue-accent);
}

.rag-back-btn:disabled {
    opacity: 0.65;
    cursor: not-allowed;
}

.rag-back-btn svg {
    flex-shrink: 0;
}

.rag-space-edit-title-block {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    text-align: center;
    max-width: min(55%, 520px);
    pointer-events: none;
}

.rag-space-edit-title-block p {
    margin: 2px 0 0;
}

.rag-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
    margin-left: auto;
    position: relative;
    z-index: 1;
}

.rag-toolbar-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    background-color: var(--bg-button);
    color: var(--blue-accent);
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    font-family: var(--font-family);
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
}

.rag-toolbar-btn:hover:not(:disabled) {
    background-color: var(--bg-button-hover);
    transform: translateY(-1px);
}

.rag-toolbar-btn:disabled {
    opacity: 0.65;
    cursor: not-allowed;
}

.rag-toolbar-btn svg {
    flex-shrink: 0;
}

.rag-space-edit-title-block h2 {
    margin: 0;
    font-size: 18px;
}

.rag-documents-panel {
    padding: 14px 18px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-panel);
}

.rag-documents-panel-full {
    flex: 1;
    min-height: 0;
    overflow: auto;
    border-bottom: none;
}

.rag-documents-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow: auto;
}

.rag-space-meta,
.rag-document-meta,
.rag-muted {
    color: var(--text-secondary);
    font-size: 12px;
}

/* Space chat */
.rag-space-chat-container {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Space modal */
.rag-space-modal-box {
    max-width: 1400px;
    position: relative;
    max-height: 90vh;
    overflow-y: auto;
}

.rag-space-modal-content {
    display: flex;
    gap: 24px;
    align-items: flex-start;
}

.rag-space-modal-column {
    flex: 1;
    min-width: 0;
}

.rag-space-modal-column-right {
    border-left: 2px solid #e5e7eb;
    padding-left: 24px;
}

.rag-modal-field-hint {
    margin: 8px 0 0;
    font-size: 12px;
    line-height: 1.45;
    color: var(--text-secondary);
}

.rag-modal-field-hint code {
    font-size: 11px;
    padding: 1px 4px;
    border-radius: 4px;
    background: rgba(15, 118, 110, 0.08);
}

.rag-document-row.read-only .rag-document-actions {
    display: none;
}

.rag-panel-title {
    font-weight: 600;
    font-size: 13px;
    margin-bottom: 8px;
}

.rag-document-row {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    padding: 10px;
    background: var(--bg-main);
}

.rag-document-name {
    font-weight: 600;
    font-size: 14px;
}

.rag-document-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    flex-shrink: 0;
    justify-content: flex-end;
}

.rag-parsed-modal-content {
    max-width: 900px;
}

.rag-cite-modal-content {
    max-width: 900px;
}

.rag-cite-modal-meta {
    margin-bottom: 12px;
    font-size: 14px;
    font-weight: 600;
    color: #0f766e;
    line-height: 1.4;
}

.rag-cite-modal-text {
    font-size: 14px;
    line-height: 1.6;
    min-width: 0;
    overflow-wrap: break-word;
    word-break: break-word;
}

.rag-cite-modal-text ul,
.rag-cite-modal-text ol {
    list-style-position: outside;
}

.rag-parsed-content {
    font-size: 14px;
    line-height: 1.6;
    min-width: 0;
    overflow-wrap: break-word;
    word-break: break-word;
}

.rag-parsed-content ul,
.rag-parsed-content ol {
    list-style-position: outside;
}

.rag-doc-action-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 6px 12px;
    background-color: transparent;
    color: var(--blue-accent);
    border: none;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 400;
    font-family: var(--font-family);
    cursor: pointer;
    transition: all 0.2s;
}

.rag-doc-action-btn:hover:not(:disabled):not(.loading) {
    background-color: var(--bg-hover);
}

.rag-doc-action-btn:active:not(:disabled):not(.loading) {
    transform: scale(0.98);
}

.rag-doc-action-btn:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

.rag-doc-action-btn.loading {
    opacity: 0.85;
    cursor: wait;
    pointer-events: none;
}

.rag-doc-action-btn.success {
    color: #166534;
}

.rag-doc-action-btn.danger {
    color: #b91c1c;
}

.rag-doc-action-btn.danger:hover:not(:disabled):not(.loading) {
    background-color: rgba(185, 28, 28, 0.08);
}

.rag-doc-action-btn.danger.success {
    color: #166534;
}

.rag-btn-spinner {
    display: inline-block;
    width: 10px;
    height: 10px;
    border: 2px solid rgba(3, 105, 161, 0.25);
    border-top-color: #0369a1;
    border-radius: 50%;
    animation: rag-btn-spin 0.7s linear infinite;
    flex-shrink: 0;
}

.rag-doc-action-btn.danger .rag-btn-spinner {
    border-color: rgba(185, 28, 28, 0.25);
    border-top-color: #b91c1c;
}

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

.rag-toast {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 10000;
    padding: 12px 16px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    color: #fff;
    background: #166534;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
    opacity: 0;
    transform: translateX(16px);
    pointer-events: none;
    transition: opacity 0.25s ease, transform 0.25s ease;
}

.rag-toast-visible {
    opacity: 1;
    transform: translateX(0);
}

.rag-toast-error {
    background: #991b1b;
}

.rag-toast-info {
    background: #0369a1;
}

.rag-document-row.status-processing {
    border-color: #0369a1;
    background: rgba(3, 105, 161, 0.06);
}

.rag-document-row.status-processing .rag-document-meta {
    color: #0369a1;
}

.rag-document-row.status-processing .rag-document-meta::after {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    margin-left: 6px;
    border-radius: 50%;
    background: #0369a1;
    animation: rag-processing-pulse 1.2s ease-in-out infinite;
    vertical-align: middle;
}

@keyframes rag-processing-pulse {
    0%, 100% { opacity: 0.35; }
    50% { opacity: 1; }
}

.rag-chat-messages {
    flex: 1;
}

/* Document space summary card (shown at top of RAG chat) */
.rag-space-info-message {
    margin: 20px auto;
    padding: 20px 24px;
    max-width: 600px;
    background: var(--bg-panel);
    border: 2px solid rgba(15, 118, 110, 0.22);
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(15, 118, 110, 0.08);
    flex-shrink: 0;
}

.rag-space-info-header {
    display: flex;
    align-items: center;
    gap: 12px;
}

.rag-space-info-icon {
    width: 44px;
    height: 44px;
    background: rgba(15, 118, 110, 0.12);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: #0f766e;
}

.rag-space-info-heading {
    flex: 1;
    min-width: 0;
}

.rag-space-info-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: #0f766e;
    font-weight: 500;
}

.rag-space-info-title {
    margin: 4px 0 0;
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    line-height: 1.3;
    word-break: break-word;
}

.rag-space-info-meta-row {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 6px;
    font-size: 13px;
    color: var(--text-secondary);
}

.rag-space-info-description {
    margin: 12px 0 0;
    font-size: 14px;
    line-height: 1.6;
    color: var(--text-secondary);
}

.rag-space-info-stats {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    margin-top: 14px;
    font-size: 13px;
    color: var(--text-secondary);
}

.rag-space-info-stat {
    display: inline-flex;
    align-items: center;
    gap: 5px;
}

.rag-space-info-stat svg {
    flex-shrink: 0;
    opacity: 0.85;
}

.rag-space-info-stat-sep {
    color: var(--text-secondary);
    opacity: 0.5;
    user-select: none;
}

.rag-space-info-footer {
    margin-top: 12px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.rag-space-info-badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 12px;
    font-weight: 500;
    line-height: 1;
    padding: 5px 10px;
    border-radius: 999px;
}

.rag-space-info-badge.public {
    background: rgba(3, 105, 161, 0.1);
    color: #0369a1;
}

.rag-space-info-badge.private {
    background: rgba(100, 116, 139, 0.12);
    color: var(--text-secondary);
}

.rag-retrieving .message-content {
    padding: 12px 16px;
}

.rag-retrieving-text {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.rag-retrieving-main {
    display: flex;
    align-items: center;
    gap: 8px;
}

.rag-retrieving-extras .rag-key-phrases {
    margin: 0;
}

.rag-retrieving-label {
    font-family: var(--font-family);
    font-size: 13px;
    font-weight: 600;
    font-style: italic;
    line-height: 1.4;
    color: var(--text-primary);
}

.rag-retrieving-extras .rag-key-phrases-label {
    font-size: 14px;
    font-weight: 500;
}

.rag-retrieving-extras .rag-key-phrase {
    font-size: 14px;
    line-height: 1.4;
}

.rag-retrieving-extras .rag-key-phrase.active {
    font-weight: 500;
}

.rag-retrieving-text .typing-indicator {
    padding: 0;
    margin: 0;
}

.rag-retrieving-text .typing-dot {
    width: 6px;
    height: 6px;
}

.rag-retrieving .message-time {
    margin-top: 2px;
}

.rag-retrieving .message-avatar {
    background: #0369a1;
    color: #fff;
}

.rag-preprocess-terminal {
    margin: 0;
    padding: 12px 14px;
    border-radius: 8px;
    background: #0f172a;
    color: #e2e8f0;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
    font-size: 13px;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
    overflow-x: auto;
    border: 1px solid rgba(148, 163, 184, 0.25);
}

.rag-context .message-avatar {
    background: #0f766e;
    color: #fff;
}

.rag-sources-summary {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 10px;
}

.rag-sources-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.rag-key-phrases {
    margin: 0 0 12px;
    padding: 10px 12px;
    border: 1px solid rgba(15, 118, 110, 0.16);
    border-radius: 12px;
    background: rgba(15, 118, 110, 0.06);
}

.rag-key-phrases-label {
    display: block;
    margin-bottom: 8px;
    font-size: 12px;
    font-weight: 700;
    color: #0f766e;
}

.rag-key-phrase-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.rag-key-phrase {
    padding: 5px 8px;
    border-radius: 999px;
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    font-size: 12px;
    line-height: 1.2;
}

.rag-key-phrase.active {
    border-color: rgba(15, 118, 110, 0.45);
    background: rgba(15, 118, 110, 0.12);
    color: #0f766e;
    font-weight: 600;
}

.rag-phrase-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.rag-phrase-group-title {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px;
    font-size: 12px;
    color: var(--text-secondary);
}

.rag-phrase-group-title strong {
    color: #0f766e;
    font-size: 13px;
}

.rag-phrase-group-count {
    padding: 3px 6px;
    border-radius: 999px;
    background: rgba(3, 105, 161, 0.1);
    color: #0369a1;
    font-weight: 600;
}

.rag-phrase-group-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.rag-source-card {
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background: var(--bg-panel);
}

.rag-source-card:not(.expanded) {
    overflow: hidden;
}

.rag-source-card.expanded {
    border-color: #0f766e;
    box-shadow: 0 0 0 1px rgba(15, 118, 110, 0.12);
}

.rag-source-card-header {
    width: 100%;
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: start;
    gap: 10px;
    padding: 10px 12px;
    border: 0;
    background: transparent;
    color: inherit;
    text-align: left;
    cursor: pointer;
}

.rag-source-card-header:hover {
    background: rgba(15, 118, 110, 0.05);
}

.rag-source-index {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 24px;
    height: 24px;
    border-radius: 999px;
    background: #0f766e;
    color: #fff;
    font-size: 12px;
    font-weight: 700;
    flex-shrink: 0;
}

.rag-source-heading {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
}

.rag-source-doc {
    font-size: 14px;
    font-weight: 600;
    color: #0f766e;
    word-break: break-word;
}

.rag-source-section {
    font-size: 12px;
    color: var(--text-secondary);
    word-break: break-word;
}

.rag-source-preview {
    font-size: 12px;
    line-height: 1.45;
    color: var(--text-secondary);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.rag-source-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 6px;
    flex-shrink: 0;
}

.rag-source-tag {
    font-size: 11px;
    line-height: 1;
    padding: 4px 6px;
    border-radius: 999px;
    background: rgba(3, 105, 161, 0.1);
    color: #0369a1;
    white-space: nowrap;
}

.rag-source-toggle {
    font-size: 12px;
    color: var(--text-secondary);
}

.rag-source-card-body {
    border-top: 1px solid var(--border-color);
    padding: 0 14px 14px 16px;
    max-height: 320px;
    overflow-y: auto;
    overflow-x: visible;
    transition: max-height 0.25s ease, opacity 0.25s ease, padding 0.25s ease;
}

.rag-source-card-body.collapsed {
    max-height: 0;
    opacity: 0;
    overflow: hidden;
    padding-top: 0;
    padding-bottom: 0;
    border-top-color: transparent;
}

.rag-source-text {
    font-size: 14px;
    line-height: 1.55;
    padding: 12px 0;
    min-width: 0;
    overflow-wrap: break-word;
    word-break: break-word;
}

.rag-source-text ul,
.rag-source-text ol {
    list-style-position: outside;
}

.rag-source-text .katex {
    font-size: 1.05em;
}

.rag-source-text eq {
    display: inline;
}

.rag-cite-wrap {
    position: relative;
    display: inline;
    white-space: nowrap;
}

.rag-cite-pill {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.15em;
    height: 1.15em;
    margin: 0 1px;
    padding: 0 4px;
    border: 0;
    border-radius: 999px;
    background: #0f766e;
    color: #fff;
    font-size: 0.68em;
    font-weight: 700;
    line-height: 1;
    vertical-align: super;
    cursor: pointer;
}

.rag-cite-wrap:hover .rag-cite-pill,
.rag-cite-wrap:focus-within .rag-cite-pill {
    background: #115e59;
}

.rag-cite-preview {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 1000;
    display: none;
    width: min(640px, calc(100vw - 32px));
    max-width: calc(100vw - 32px);
    padding: 12px 16px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: var(--bg-panel);
    box-shadow: 0 8px 24px rgba(15, 23, 42, 0.16);
    text-align: left;
    white-space: normal;
    pointer-events: none;
    overflow: visible;
}

.rag-cite-preview.is-visible {
    display: block;
}

.rag-cite-preview-title {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
    font-weight: 700;
    color: #0f766e;
    line-height: 1.4;
}

.rag-cite-preview-text {
    display: block;
    font-size: 14px;
    line-height: 1.5;
    color: var(--text-secondary);
    max-height: 480px;
    overflow-x: hidden;
    overflow-y: auto;
    overflow-wrap: break-word;
    padding-bottom: 8px;
    box-sizing: border-box;
    scrollbar-gutter: stable;
}

.rag-cite-preview-text p {
    margin: 0.35em 0;
}

.rag-cite-preview-text p:first-child {
    margin-top: 0;
}

.rag-cite-preview-text p:last-child {
    margin-bottom: 0;
}

.rag-cite-preview-text ul,
.rag-cite-preview-text ol {
    margin: 0.35em 0;
    padding-left: 1.25em;
    list-style-position: outside;
}

.rag-cite-preview-text strong {
    color: var(--text-primary);
}

.rag-error {
    color: #b91c1c;
    font-size: 12px;
}

/* RAG settings panel (right sidebar) */
.rag-settings {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.rag-settings .settings-header {
    width: 100%;
    flex-shrink: 0;
}

.rag-settings .settings-header h2 {
    white-space: nowrap;
}

.rag-settings .setting-group {
    margin-top: 32px;
}

.rag-settings .setting-group:first-child {
    margin-top: 0;
}

.rag-settings .setting-item:last-child {
    margin-bottom: 0;
}

.rag-settings-hint {
    font-size: 12px;
    color: var(--text-secondary);
    line-height: 1.5;
    margin: 0;
}

@media (max-width: 768px) {
    .rag-gallery-container {
        padding: 16px;
    }

    .rag-add-space-btn {
        position: static;
        margin-top: 12px;
    }

    .rag-gallery-header {
        flex-direction: column;
    }

    .rag-space-edit-header {
        flex-direction: column;
        align-items: stretch;
    }

    .rag-space-edit-title-block {
        position: static;
        transform: none;
        max-width: none;
        order: -1;
        margin-bottom: 4px;
    }

    .rag-header-actions {
        margin-left: 0;
    }
}
