:root {
    --primary-color: #4f46e5;
    --primary-hover: #4338ca;
    --secondary-color: #6b7280;
    --background: #ffffff;
    --surface: #f9fafb;
    --border: #e5e7eb;
    --text-primary: #111827;
    --text-secondary: #6b7280;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --sidebar-width: 320px;
    --header-height: 72px;
    --chat-max-width: 900px;
    --radius: 8px;
    --transition: 150ms ease;
}

@media (prefers-color-scheme: dark) {
    :root {
        --background: #0f172a;
        --surface: #1e293b;
        --border: #334155;
        --text-primary: #f1f5f9;
        --text-secondary: #94a3b8;
    }
}

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

body {
    font-family: 'JetBrains Mono', 'Courier New', monospace;
    background: var(--background);
    color: var(--text-primary);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Ensure all form elements and buttons use JetBrains Mono */
button, input, select, textarea {
    font-family: 'JetBrains Mono', 'Courier New', monospace;
}

.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

.app-header {
    display: flex;
    align-items: center;
    height: var(--header-height);
    padding: 0 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    gap: 15px;
}

.app-icon {
    height: 42px;
    width: auto;
    margin-right: 12px;
}

.app-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin-right: auto;
}

.settings-btn {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px 12px;
    font-size: 1.2rem;
    cursor: pointer;
    transition: all var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
    height: 36px;
    width: 36px;
}

.settings-btn:hover {
    background: var(--background);
    transform: scale(1.05);
}

.settings-btn[aria-expanded="true"] {
    background: var(--primary-color);
    border-color: var(--primary-color);
}

.status-container {
    display: flex;
    align-items: center;
    gap: 10px;
    position: relative;
}

.status-pill {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    background: var(--background);
    border: 1px solid var(--border);
    border-radius: 20px;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all var(--transition);
    user-select: none;
}

.status-pill:hover {
    background: var(--surface);
    border-color: var(--primary-color);
    transform: translateY(-1px);
}

.status-pill:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

.status-pill[aria-expanded="true"] {
    background: var(--surface);
    border-color: var(--primary-color);
}

.status-indicator {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--warning);
    animation: pulse 2s infinite;
}

.status-pill[data-status="ready"] .status-indicator {
    background: var(--success);
    animation: none;
}

.status-pill[data-status="error"] .status-indicator {
    background: var(--danger);
    animation: none;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* Status Details Popup */
.status-details {
    position: absolute;
    top: calc(100% + 10px);
    right: 0;
    min-width: 320px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    z-index: 200;
    opacity: 0;
    transform: translateY(-10px);
    transition: opacity 0.2s ease, transform 0.2s ease;
    pointer-events: none;
}

.status-details:not([hidden]) {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.status-details-content {
    padding: 16px;
}

.status-section {
    margin-bottom: 16px;
}

.status-section:last-child {
    margin-bottom: 0;
}

.status-section h4 {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
    margin-bottom: 10px;
}

.status-info {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.status-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.875rem;
}

.status-label {
    color: var(--text-secondary);
    font-weight: 500;
}

.status-value {
    color: var(--text-primary);
    font-family: 'JetBrains Mono', 'Courier New', monospace;
    font-size: 0.85rem;
}

.app-main {
    display: flex;
    flex: 1;
    overflow: hidden;
}

.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    max-width: var(--chat-max-width);
    margin: 0 auto;
    width: 100%;
}

.chat-transcript {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    scroll-behavior: smooth;
}

.welcome-message {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-secondary);
}

.welcome-message h2 {
    color: var(--text-primary);
    margin-bottom: 10px;
}

.loading-status {
    margin-top: 20px;
    padding: 10px;
    background: var(--surface);
    border-radius: var(--radius);
    font-size: 0.875rem;
}

.message {
    margin-bottom: 20px;
    animation: fadeIn 0.3s ease;
}

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

.message-role {
    font-weight: 600;
    margin-bottom: 5px;
    font-size: 0.875rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.message-user .message-role {
    color: var(--primary-color);
}

.message-assistant .message-role {
    color: var(--secondary-color);
}

.message-content {
    padding: 12px 16px;
    border-radius: var(--radius);
    background: var(--surface);
    white-space: pre-wrap;
    word-wrap: break-word;
}

.message-user .message-content {
    background: var(--primary-color);
    color: white;
    margin-left: 20%;
}

.message-assistant .message-content {
    margin-right: 20%;
}

.message-content code {
    font-family: 'JetBrains Mono', monospace;
    background: rgba(0, 0, 0, 0.1);
    padding: 2px 4px;
    border-radius: 3px;
    font-size: 0.9em;
}

.message-content pre {
    background: rgba(0, 0, 0, 0.1);
    padding: 12px;
    border-radius: var(--radius);
    overflow-x: auto;
    margin: 10px 0;
}

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

.copy-button {
    float: right;
    padding: 4px 8px;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-family: 'JetBrains Mono', 'Courier New', monospace;
    font-size: 0.75rem;
    margin-bottom: 5px;
}

.copy-button:hover {
    background: var(--primary-hover);
}

.sources-panel {
    margin-top: 10px;
    padding: 10px;
    background: rgba(0, 0, 0, 0.05);
    border-radius: var(--radius);
    font-size: 0.875rem;
}

.sources-header {
    font-weight: 600;
    margin-bottom: 5px;
    cursor: pointer;
    user-select: none;
}

.sources-list {
    margin-top: 8px;
}

.source-item {
    padding: 4px 0;
    color: var(--text-secondary);
}

.chat-input-container {
    padding: 20px;
    border-top: 1px solid var(--border);
    background: var(--surface);
}

.input-wrapper {
    display: flex;
    gap: 10px;
    align-items: flex-end;
}

.chat-input {
    flex: 1;
    min-height: 44px;
    max-height: 120px;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--background);
    color: var(--text-primary);
    font-size: 1rem;
    resize: none;
    font-family: inherit;
    transition: border-color var(--transition);
}

.chat-input:focus {
    outline: none;
    border-color: var(--primary-color);
}

.input-controls {
    display: flex;
    gap: 8px;
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 10px 16px;
    border: none;
    border-radius: var(--radius);
    font-family: 'JetBrains Mono', 'Courier New', monospace;
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition);
    white-space: nowrap;
}

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

.btn-primary {
    background: var(--primary-color);
    color: white;
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-hover);
}

.btn-secondary {
    background: var(--secondary-color);
    color: white;
}

.btn-secondary:hover:not(:disabled) {
    background: #4b5563;
}

.btn-danger {
    background: var(--danger);
    color: white;
}

.btn-danger:hover:not(:disabled) {
    background: #dc2626;
}

.btn-icon {
    font-size: 1rem;
}

.input-stats {
    display: flex;
    gap: 20px;
    margin-top: 8px;
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.sidebar {
    position: fixed;
    top: var(--header-height);
    right: 0;
    bottom: 0;
    width: var(--sidebar-width);
    background: var(--surface);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: transform var(--transition);
    transform: translateX(100%);
    z-index: 100;
}

/* Sidebar open state */
.sidebar.open {
    transform: translateX(0);
    box-shadow: -5px 0 20px rgba(0, 0, 0, 0.1);
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20px;
    border-bottom: 1px solid var(--border);
}

.sidebar-header h2 {
    font-size: 1.125rem;
    font-weight: 600;
}

.sidebar-toggle {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    transition: all var(--transition);
}

.sidebar-toggle:hover {
    background: var(--background);
}

.sidebar-content {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
}

.settings-section {
    margin-bottom: 30px;
}

.settings-section h3 {
    font-size: 0.875rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
    margin-bottom: 15px;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
    position: relative;
}

/* Info icons and tooltips */
.info-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    margin-left: 6px;
    border-radius: 50%;
    background: var(--text-secondary);
    color: var(--background);
    font-size: 10px;
    font-weight: 400;
    font-style: italic;
    cursor: pointer;
    transition: all var(--transition);
    position: relative;
    vertical-align: middle;
    user-select: none;
}

.info-icon:hover {
    background: var(--primary-color);
}

.info-icon:focus {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

.tooltip {
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    margin-top: 8px;
    padding: 10px 14px;
    background: #ffffff;
    color: #1a1a1a;
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: var(--radius);
    font-size: 0.8125rem;
    font-weight: 400;
    font-style: normal;
    line-height: 1.5;
    white-space: normal;
    max-width: 280px;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
    z-index: 10000;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s, visibility 0.3s;
    pointer-events: none;
}

.tooltip::before {
    content: '';
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    width: 0;
    height: 0;
    border-left: 6px solid transparent;
    border-right: 6px solid transparent;
    border-bottom: 6px solid rgba(0, 0, 0, 0.15);
}

.tooltip::after {
    content: '';
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%) translateY(1px);
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-bottom: 5px solid #ffffff;
}

/* Tooltip active state - controlled by JavaScript */
.tooltip.active {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

/* Adjust tooltip position for edge cases */
.tooltip.tooltip-right {
    left: auto;
    right: 0;
    transform: none;
}

.tooltip.tooltip-right::before,
.tooltip.tooltip-right::after {
    left: auto;
    right: 10px;
    transform: none;
}

/* Label with info icon flex layout */
.label-with-info {
    display: inline-flex;
    align-items: center;
}

/* Ensure consistent alignment for all form controls */
.form-group .form-control,
.form-group .slider {
    width: 100%;
    margin-top: 4px;
}

.form-control {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--background);
    color: var(--text-primary);
    font-family: 'JetBrains Mono', 'Courier New', monospace;
    font-size: 0.875rem;
    transition: border-color var(--transition);
}

.form-control:focus {
    outline: none;
    border-color: var(--primary-color);
}

.slider {
    width: 100%;
    height: 6px;
    border-radius: 3px;
    background: #e2e8f0;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
    margin-top: 8px;
    margin-bottom: 4px;
}

.slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--primary-color);
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    transition: box-shadow 0.2s;
}

.slider::-webkit-slider-thumb:hover {
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

.slider::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--primary-color);
    cursor: pointer;
    border: none;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    transition: box-shadow 0.2s;
}

.slider::-moz-range-thumb:hover {
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

/* Override form-group label styles for toggle */
label.toggle-label {
    display: inline-flex !important;
    align-items: center;
    cursor: pointer;
    margin-bottom: 0 !important;
    user-select: none;
    font-weight: 500;
}

.toggle-input {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 28px;
    background: #cbd5e1;
    border: 2px solid #e2e8f0;
    border-radius: 14px;
    transition: all 0.3s ease;
    flex-shrink: 0;
    margin-right: 12px;
}

.toggle-text {
    line-height: 28px;
    vertical-align: middle;
}

.toggle-switch::after {
    content: '';
    position: absolute;
    top: 1px;
    left: 2px;
    width: 22px;
    height: 22px;
    background: white;
    border-radius: 50%;
    transition: transform 0.3s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.toggle-input:checked + .toggle-switch {
    background: var(--primary-color);
    border-color: var(--primary-color);
}

.toggle-input:checked + .toggle-switch::after {
    transform: translateX(22px);
}

/* Hover and focus states for better accessibility */
.toggle-label:hover .toggle-switch {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.toggle-input:focus-visible + .toggle-switch {
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.25);
    outline: none;
}

.toggle-input:disabled + .toggle-switch {
    opacity: 0.5;
    cursor: not-allowed;
}

.toggle-input:disabled ~ .toggle-text {
    opacity: 0.5;
    cursor: not-allowed;
}

.file-drop-zone {
    position: relative;
    padding: 30px;
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    text-align: center;
    cursor: pointer;
    transition: all var(--transition);
}

.file-drop-zone:hover,
.file-drop-zone.drag-over {
    border-color: var(--primary-color);
    background: rgba(79, 70, 229, 0.05);
}

.drop-zone-content {
    pointer-events: none;
}

.drop-icon {
    font-size: 2rem;
    display: block;
    margin-bottom: 10px;
}

.drop-hint {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 5px;
}

.file-input {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
}

.documents-info {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    padding: 10px;
    background: var(--background);
    border-radius: var(--radius);
    font-size: 0.875rem;
}

.action-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
}


@media (max-width: 768px) {
    .sidebar {
        width: 100%;
        max-width: 400px;
        top: 0;
        height: 100vh;
    }


    .message-user .message-content {
        margin-left: 10%;
    }

    .message-assistant .message-content {
        margin-right: 10%;
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        animation: none !important;
        transition: none !important;
    }
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Document Management */
.document-list-container {
    margin-bottom: 15px;
}

.document-list {
    max-height: 250px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px;
    background: rgba(0, 0, 0, 0.02);
}

.no-documents {
    text-align: center;
    color: #666;
    padding: 20px;
    font-style: italic;
}

.document-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    margin-bottom: 8px;
    background: var(--surface);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    transition: background-color 0.2s;
}

.document-item:hover {
    background-color: rgba(0, 0, 0, 0.03);
}

.document-item:last-child {
    margin-bottom: 0;
}

.document-info {
    flex: 1;
    min-width: 0;
}

.document-name {
    font-weight: 600;
    margin-bottom: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.document-meta {
    font-size: 0.85em;
    color: #666;
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

.doc-size, .doc-chunks, .doc-date {
    white-space: nowrap;
}

.btn-delete-doc {
    background: transparent;
    border: none;
    font-size: 1.2em;
    cursor: pointer;
    padding: 8px;
    border-radius: var(--radius);
    transition: background-color 0.2s, transform 0.2s;
    flex-shrink: 0;
}

.btn-delete-doc:hover {
    background: rgba(231, 76, 60, 0.1);
    transform: scale(1.1);
}

.document-actions {
    display: flex;
    gap: 10px;
    justify-content: space-between;
    margin-top: 10px;
}

.document-actions .btn {
    flex: 1;
}

.btn-danger {
    background: #e74c3c;
}

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

.success-message {
    animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translate(-50%, -20px);
    }
    to {
        opacity: 1;
        transform: translate(-50%, 0);
    }
}

:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

/* TTS Controls */
.tts-controls {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    margin-top: 12px;
    padding: 8px;
    background: var(--surface);
    border-radius: var(--radius);
    border: 1px solid var(--border);
}

.tts-button-row {
    display: flex;
    align-items: center;
    gap: 8px;
}

.tts-action-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--background);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition);
}

.tts-action-btn:hover {
    color: var(--primary-color);
    border-color: var(--primary-color);
    transform: scale(1.05);
}

.tts-action-btn:active {
    transform: scale(0.98);
}

.download-audio-btn {
    animation: fadeInScale 0.3s ease;
}

@keyframes fadeInScale {
    from { opacity: 0; transform: scale(0.8); }
    to { opacity: 1; transform: scale(1); }
}

.tts-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: 2px solid var(--primary-color);
    border-radius: 50%;
    background: var(--background);
    color: var(--primary-color);
    cursor: pointer;
    transition: all var(--transition);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.tts-button:hover {
    background: var(--primary-color);
    color: white;
    transform: scale(1.05);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.tts-button:active {
    transform: scale(0.98);
}

.tts-button[aria-label="Pause text-to-speech"] {
    background: var(--primary-color);
    color: white;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% {
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }
    50% {
        box-shadow: 0 2px 8px rgba(79, 70, 229, 0.4);
    }
    100% {
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }
}

.tts-button[aria-label="Loading text-to-speech"] {
    background: var(--surface);
    color: var(--primary-color);
    border-style: dashed;
    cursor: wait;
}

.tts-loading-spinner {
    animation: spin 1.5s linear infinite;
}

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

.tts-progress-container {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
}

.tts-progress-bar {
    flex: 1;
    height: 8px;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 4px;
    overflow: hidden;
    position: relative;
    box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
}

.tts-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary-color), var(--primary-hover));
    border-radius: 4px;
    transition: width 0.1s ease;
    width: 0%;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.tts-progress-text {
    font-size: 0.75rem;
    color: var(--text-secondary);
    min-width: 45px;
    text-align: right;
}

/* Adjust for mobile */
@media (max-width: 768px) {
    .tts-controls {
        width: 100%;
        padding: 6px;
    }

    .tts-button {
        width: 36px;
        height: 36px;
    }

    .tts-action-btn {
        width: 32px;
        height: 32px;
    }

    .tts-action-btn svg {
        width: 14px;
        height: 14px;
    }

    .tts-progress-bar {
        height: 6px;
    }
}