:root {
    color-scheme: light;
    --theme-bg-app: #f3f4f6;
    --theme-bg-surface: #ffffff;
    --theme-bg-surface-alt: #f9fafb;
    --theme-bg-muted: #f3f4f6;
    --theme-bg-muted-2: #e5e7eb;
    --theme-bg-hover: #eef2f7;
    --theme-text: #111827;
    --theme-text-muted: #374151;
    --theme-text-subtle: #6b7280;
    --theme-text-faint: #9ca3af;
    --theme-border: #d1d5db;
    --theme-border-strong: #9ca3af;
    --theme-overlay: rgba(0, 0, 0, 0.3);
    --theme-shadow: 0 20px 50px rgba(15, 23, 42, 0.2);
}

html {
    background-color: var(--theme-bg-app);
}

body {
    background-color: var(--theme-bg-app);
    color: var(--theme-text);
    transition: background-color 0.18s ease, color 0.18s ease;
}

html.dark {
    color-scheme: dark;
    --theme-bg-app: #020617;
    --theme-bg-surface: #0f172a;
    --theme-bg-surface-alt: #111827;
    --theme-bg-muted: #1e293b;
    --theme-bg-muted-2: #334155;
    --theme-bg-hover: #243244;
    --theme-text: #e5e7eb;
    --theme-text-muted: #cbd5e1;
    --theme-text-subtle: #94a3b8;
    --theme-text-faint: #64748b;
    --theme-border: #334155;
    --theme-border-strong: #475569;
    --theme-overlay: rgba(2, 6, 23, 0.42);
    --theme-shadow: 0 22px 55px rgba(2, 6, 23, 0.55);
}

html.dark .bg-white {
    background-color: var(--theme-bg-surface);
}

html.dark .bg-white\/90 {
    background-color: rgba(15, 23, 42, 0.92);
}

html.dark .bg-gray-50 {
    background-color: var(--theme-bg-surface-alt);
}

html.dark .bg-gray-100 {
    background-color: var(--theme-bg-muted);
}

html.dark .bg-gray-200 {
    background-color: var(--theme-bg-muted-2);
}

html.dark .bg-gray-300 {
    background-color: #475569;
}

html.dark .hover\:bg-gray-50:hover {
    background-color: var(--theme-bg-hover);
}

html.dark .hover\:bg-gray-100:hover {
    background-color: #29384c;
}

html.dark .hover\:bg-gray-200:hover,
html.dark .hover\:bg-gray-300:hover {
    background-color: #3b4a61;
}

html.dark .text-gray-900,
html.dark .text-gray-800,
html.dark .text-gray-700 {
    color: var(--theme-text);
}

html.dark .text-gray-600,
html.dark .text-gray-500 {
    color: var(--theme-text-subtle);
}

html.dark .text-gray-400 {
    color: var(--theme-text-faint);
}

html.dark .text-gray-300 {
    color: var(--theme-text-muted);
}

html.dark .hover\:text-gray-600:hover,
html.dark .hover\:text-gray-700:hover,
html.dark .hover\:text-gray-800:hover {
    color: var(--theme-text);
}

html.dark .border-gray-100,
html.dark .border-gray-200,
html.dark .border-gray-300,
html.dark .border-gray-400 {
    border-color: var(--theme-border);
}

html.dark :is(.border, .border-t, .border-b, .border-l, .border-r, .border-x, .border-y):not([class*="border-blue"]):not([class*="border-green"]):not([class*="border-red"]):not([class*="border-yellow"]):not([class*="border-emerald"]):not([class*="border-rose"]):not([class*="border-purple"]) {
    border-color: var(--theme-border);
}

html.dark .bg-black\/30 {
    background-color: var(--theme-overlay);
}

html.dark .shadow,
html.dark .shadow-lg,
html.dark .shadow-xl {
    box-shadow: var(--theme-shadow);
}

html.dark .bg-blue-50 {
    background-color: rgba(37, 99, 235, 0.18);
}

html.dark .bg-blue-100 {
    background-color: rgba(30, 64, 175, 0.42);
}

html.dark .text-blue-600,
html.dark .text-blue-700 {
    color: #93c5fd;
}

html.dark .text-blue-800 {
    color: #bfdbfe;
}

html.dark .border-blue-200,
html.dark .\!border-blue-200 {
    border-color: rgba(59, 130, 246, 0.45);
}

html.dark .hover\:bg-blue-50:hover {
    background-color: rgba(37, 99, 235, 0.26);
}

html.dark .hover\:text-blue-500:hover,
html.dark .hover\:text-blue-600:hover,
html.dark .hover\:text-blue-700:hover,
html.dark .hover\:text-blue-800:hover {
    color: #dbeafe;
}

html.dark .bg-green-50 {
    background-color: rgba(22, 163, 74, 0.16);
}

html.dark .bg-green-100 {
    background-color: rgba(21, 128, 61, 0.28);
}

html.dark .text-green-600,
html.dark .text-green-700 {
    color: #86efac;
}

html.dark .hover\:text-green-600:hover {
    color: #bbf7d0;
}

html.dark .bg-yellow-50 {
    background-color: rgba(202, 138, 4, 0.18);
}

html.dark .text-yellow-500,
html.dark .text-yellow-700 {
    color: #fcd34d;
}

html.dark .border-yellow-200 {
    border-color: rgba(202, 138, 4, 0.4);
}

html.dark .hover\:text-yellow-500:hover,
html.dark .hover\:text-yellow-900:hover {
    color: #fde68a;
}

html.dark .bg-red-50 {
    background-color: rgba(220, 38, 38, 0.16);
}

html.dark .text-red-300 {
    color: #fca5a5;
}

html.dark .text-red-500,
html.dark .text-red-600,
html.dark .text-red-700 {
    color: #f87171;
}

html.dark .border-red-200 {
    border-color: rgba(220, 38, 38, 0.35);
}

html.dark .hover\:text-red-500:hover,
html.dark .hover\:text-red-600:hover,
html.dark .hover\:text-red-700:hover {
    color: #fca5a5;
}

html.dark .bg-purple-100 {
    background-color: rgba(126, 34, 206, 0.26);
}

html.dark .text-purple-700 {
    color: #d8b4fe;
}

html.dark .bg-emerald-100 {
    background-color: rgba(5, 150, 105, 0.24);
}

html.dark .text-emerald-600,
html.dark .text-emerald-800 {
    color: #6ee7b7;
}

html.dark .hover\:text-emerald-800:hover {
    color: #a7f3d0;
}

html.dark .bg-rose-100 {
    background-color: rgba(225, 29, 72, 0.22);
}

html.dark .text-rose-600,
html.dark .text-rose-800 {
    color: #fda4af;
}

html.dark .hover\:text-rose-700:hover,
html.dark .hover\:text-rose-800:hover {
    color: #fecdd3;
}

html.dark .disabled\:bg-gray-400:disabled {
    background-color: #475569;
}

html.dark input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]),
html.dark textarea,
html.dark select {
    background-color: var(--theme-bg-surface);
    color: var(--theme-text);
    border-color: var(--theme-border);
}

html.dark input::placeholder,
html.dark textarea::placeholder {
    color: var(--theme-text-faint);
}

html.dark select option {
    background-color: var(--theme-bg-surface);
    color: var(--theme-text);
}

.prose blockquote {
    margin: 0.85rem 0;
    padding: 0.5rem 0.8rem;
    border-left: 4px solid #93c5fd;
    border-radius: 0 0.375rem 0.375rem 0;
    background-color: rgba(239, 246, 255, 0.72);
    color: var(--theme-text-muted);
}

.prose blockquote > :first-child {
    margin-top: 0;
}

.prose blockquote > :last-child {
    margin-bottom: 0;
}

.prose blockquote :where(p, li, strong, em, figcaption) {
    color: inherit;
}

html.dark .prose,
html.dark .prose :where(p, li, strong, em, blockquote, figcaption) {
    color: var(--theme-text);
}

html.dark .prose :where(h1, h2, h3, h4, h5, h6) {
    color: #f8fafc;
}

html.dark .prose a {
    color: #93c5fd;
}

html.dark .prose code {
    color: #bfdbfe;
}

html.dark .prose pre {
    background-color: #0b1120;
    color: #e2e8f0;
    border: 1px solid var(--theme-border);
}

html.dark .prose blockquote {
    border-left-color: var(--theme-border-strong);
    background-color: rgba(30, 41, 59, 0.72);
    color: var(--theme-text-muted);
}

html.dark .prose blockquote :where(p, li, strong, em, figcaption) {
    color: inherit;
}

html.dark .content-tab.active {
    border-bottom-color: #60a5fa;
    color: #93c5fd;
}

html.dark #pdf-resize-handle {
    background: linear-gradient(to bottom, transparent 0%, var(--theme-border-strong) 50%, transparent 100%);
}

html.dark #pdf-resize-handle:hover,
html.dark #pdf-resize-handle.active {
    background: #3b82f6;
}

html.dark #pdf-toolbar {
    border-color: var(--theme-border);
}

html.dark #theme-toggle-btn svg {
    display: block;
}

/* ---- Mobile responsiveness ---- */

@media (max-width: 767px) {
    /* History sidebar: overlay width */
    #sidebar {
        width: 85vw !important;
        max-width: 320px !important;
        min-width: unset !important;
    }

    /* Chat container: taller default on mobile, resizable via JS */
    #chat-container {
        height: 75vh;
        min-height: 320px;
    }

    /* Chat sidebar: overlay within chat */
    #chat-sidebar {
        width: 200px !important;
        min-width: unset !important;
        max-width: none !important;
        background-color: var(--theme-bg-surface, #ffffff);
        box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
    }

    /* Chat dropdowns: fit mobile */
    #paper-picker-dropdown {
        width: calc(100vw - 2rem) !important;
        left: auto !important;
        right: -1rem;
    }
    #lesswrong-dialog {
        width: calc(100vw - 2rem) !important;
        left: auto !important;
        right: -1rem;
    }

    /* PDF viewer: shorter on mobile */
    #pdf-viewer-container {
        height: 75vh !important;
    }
    #pdf-resize-handle {
        display: none;
    }
}

/* Touch-friendly resize handles on coarse pointer devices */
@media (pointer: coarse) {
    #sidebar-resize-handle,
    #chat-sidebar-resize-handle {
        min-width: 8px;
    }
    #pdf-resize-handle,
    #chat-resize-handle {
        min-height: 12px;
    }
}

/* ---- Mobile chat focus mode ---- */
@media (max-width: 767px) {
    body.chat-focus-mode {
        overflow: hidden;
    }

    /* Hide everything except the chat */
    body.chat-focus-mode header,
    body.chat-focus-mode #paper-info-bar,
    body.chat-focus-mode #paper-section,
    body.chat-focus-mode #paper-links-section,
    body.chat-focus-mode #loading-section {
        display: none !important;
    }

    /* Chat fills viewport */
    body.chat-focus-mode #chat-section {
        position: fixed;
        inset: 0;
        margin: 0;
        z-index: 50;
        display: block !important;
    }
    body.chat-focus-mode #chat-container {
        height: 100dvh !important;
        min-height: 100dvh !important;
        border-radius: 0;
    }

    /* Show focus bar; hide the original heading row controls */
    body.chat-focus-mode #chat-focus-bar {
        display: flex;
    }
    body.chat-focus-mode #chat-header-row > .flex.items-center.gap-2.mb-3 {
        display: none;
    }

    /* Collapse options row by default; reveal when toggled */
    body.chat-focus-mode #chat-header-row {
        padding: 0;
        border-bottom: none;
    }
    body.chat-focus-mode .chat-options-row {
        display: none;
    }
    body.chat-focus-mode.chat-options-open #chat-header-row {
        padding: 0.75rem 1rem;
        border-bottom: 1px solid var(--theme-border, #e5e7eb);
        max-height: 40dvh;
        overflow-y: auto;
    }
    body.chat-focus-mode.chat-options-open .chat-options-row {
        display: flex;
    }

    /* Let the messages area shrink so the input wrapper stays anchored at the
       bottom when the options row is expanded. Overrides inline min-height. */
    body.chat-focus-mode #chat-messages {
        min-height: 0 !important;
    }

    /* Hide vertical resize handle in focus mode */
    body.chat-focus-mode #chat-resize-handle {
        display: none;
    }

    /* Input bar background (flex column already anchors it at the bottom) */
    body.chat-focus-mode #chat-input-wrapper {
        background: var(--theme-bg-surface, #ffffff);
        flex-shrink: 0;
    }
}
