/*
 * PDF AI Indexer — chat widget styles.
 *
 * All themable values are CSS custom properties on .pdf-ai-chat-widget.
 * Defaults match the original look. The plugin can override any of
 * these by emitting an inline <style> block that targets the same
 * scope — this is how the Settings page colour controls work.
 *
 * Override examples (in your theme's CSS):
 *   .pdf-ai-chat-widget {
 *       --pdf-chat-header-bg: #2d6a4f;          // solid colour
 *       --pdf-chat-header-text: #ffffff;
 *       --pdf-chat-user-bubble-bg: #2d6a4f;
 *       --pdf-chat-send-button-bg: #2d6a4f;
 *   }
 */
.pdf-ai-chat-widget {
    /* Widget shell */
    --pdf-chat-widget-bg: #ffffff;
    --pdf-chat-widget-border: #ddd;
    --pdf-chat-widget-radius: 12px;
    --pdf-chat-widget-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

    /* Header */
    --pdf-chat-header-bg: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    --pdf-chat-header-text: #ffffff;
    --pdf-chat-header-font-size: 20px;
    --pdf-chat-header-font-weight: 600;
    --pdf-chat-header-font-family: inherit;

    /* Messages area
     *
     * The panel grows with its content from the min-height up to the
     * max-height, then scrolls internally — so short answers don't leave
     * a large empty box, and long ones don't push the page furniture
     * off-screen.
     *
     * The effective ceiling is min(max-height, cap). The cap is a
     * viewport-relative safety limit: an admin can set a generous pixel
     * max-height for desktop without that value swamping a phone screen,
     * because the cap wins whenever the viewport is short. Both are
     * overridable from Settings → Chat appearance.
     */
    --pdf-chat-messages-bg: #f9fafb;
    --pdf-chat-messages-min-height: 300px;
    --pdf-chat-messages-max-height: 500px;
    --pdf-chat-messages-max-height-cap: 75vh;

    /* Formatted answer content (headings, lists, code, quotes) */
    --pdf-chat-heading-color: #111827;
    --pdf-chat-heading-weight: 600;
    --pdf-chat-code-bg: #f3f4f6;
    --pdf-chat-code-text: #b91c1c;
    --pdf-chat-quote-border: #d1d5db;
    --pdf-chat-quote-text: #4b5563;
    --pdf-chat-rule-color: #e5e7eb;

    /* User message bubble */
    --pdf-chat-user-bubble-bg: #667eea;
    --pdf-chat-user-bubble-text: #ffffff;

    /* Assistant message bubble */
    --pdf-chat-assistant-bubble-bg: #ffffff;
    --pdf-chat-assistant-bubble-text: #1f2937;
    --pdf-chat-assistant-bubble-border: #e5e7eb;

    /* Source links */
    --pdf-chat-source-link-bg: #f3f4f6;
    --pdf-chat-source-link-text: #667eea;
    --pdf-chat-source-link-hover-bg: #e5e7eb;
    --pdf-chat-source-link-hover-text: #5a67d8;

    /* Input area */
    --pdf-chat-input-bg: #ffffff;
    --pdf-chat-input-text: #1f2937;
    --pdf-chat-input-border: #e5e7eb;
    --pdf-chat-input-focus-border: #667eea;
    --pdf-chat-input-font-size: 15px;
    --pdf-chat-input-font-family: inherit;
    --pdf-chat-input-font-weight: 400;

    /* Send button */
    --pdf-chat-send-button-bg: #667eea;
    --pdf-chat-send-button-text: #ffffff;
    --pdf-chat-send-button-hover-bg: #5a67d8;
    --pdf-chat-send-button-font-size: 15px;
    --pdf-chat-send-button-font-family: inherit;
    --pdf-chat-send-button-font-weight: 600;

    /* Loading + error */
    --pdf-chat-loading-dot: #667eea;
    --pdf-chat-error-bg: #fee2e2;
    --pdf-chat-error-text: #991b1b;
    --pdf-chat-error-border: #fecaca;

    max-width: 800px;
    margin: 30px auto;
    border: 1px solid var(--pdf-chat-widget-border);
    border-radius: var(--pdf-chat-widget-radius);
    background: var(--pdf-chat-widget-bg);
    box-shadow: 0 2px 12px rgba(0,0,0,0.1);
    font-family: var(--pdf-chat-widget-font);
}

.pdf-ai-chat-widget .chat-header {
    padding: 20px 25px;
    background: var(--pdf-chat-header-bg);
    border-radius: var(--pdf-chat-widget-radius) var(--pdf-chat-widget-radius) 0 0;
}

.pdf-ai-chat-widget .chat-header h3 {
    margin: 0;
    font-size: var(--pdf-chat-header-font-size);
    font-weight: var(--pdf-chat-header-font-weight);
    font-family: var(--pdf-chat-header-font-family);
    color: var(--pdf-chat-header-text);
}

.pdf-ai-chat-widget .chat-input-container {
    padding: 20px 25px;
    background: var(--pdf-chat-widget-bg);
    border-bottom: 1px solid #e5e7eb;
}

.pdf-ai-chat-widget .chat-messages {
    min-height: var(--pdf-chat-messages-min-height);
    max-height: min(
        var(--pdf-chat-messages-max-height),
        var(--pdf-chat-messages-max-height-cap)
    );
    overflow-y: auto;
    padding: 20px 25px;
    background: var(--pdf-chat-messages-bg);
    border-radius: 0 0 var(--pdf-chat-widget-radius) var(--pdf-chat-widget-radius);
}

.pdf-ai-chat-widget .chat-messages:empty::before {
    content: '';
    display: block;
}

.pdf-ai-chat-widget .message {
    margin-bottom: 20px;
    animation: pdf-chat-fadeIn 0.3s ease-in;
}

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

.pdf-ai-chat-widget .message.user {
    display: flex;
    justify-content: flex-end;
}

.pdf-ai-chat-widget .message.assistant {
    display: flex;
    justify-content: flex-start;
}

.pdf-ai-chat-widget .message-content {
    max-width: 75%;
    padding: 12px 16px;
    border-radius: 12px;
    position: relative;
    word-wrap: break-word;
}

.pdf-ai-chat-widget .message.user .message-content {
    background: var(--pdf-chat-user-bubble-bg);
    color: var(--pdf-chat-user-bubble-text);
    border-bottom-right-radius: 4px;
}

.pdf-ai-chat-widget .message.assistant .message-content {
    background: var(--pdf-chat-assistant-bubble-bg);
    color: var(--pdf-chat-assistant-bubble-text);
    border: 1px solid var(--pdf-chat-assistant-bubble-border);
    border-bottom-left-radius: 4px;
}

.pdf-ai-chat-widget .message-text {
    line-height: 1.5;
    margin: 0;
}

/* User messages are unformatted, so newlines must be preserved. */
.pdf-ai-chat-widget .message-text--plain {
    white-space: pre-wrap;
}

/* Assistant messages are rendered into real block elements, so normal
   whitespace collapsing applies — pre-wrap here would double up the
   spacing that the elements already provide. */
.pdf-ai-chat-widget .message-text--rich {
    white-space: normal;
}

/* ------------------------------------------------------------------
   Formatted answer content
   ------------------------------------------------------------------
   First and last children have their outer margins removed so the
   bubble padding stays even regardless of which block type happens to
   start or end an answer. */
.pdf-ai-chat-widget .message-text--rich > *:first-child { margin-top: 0; }
.pdf-ai-chat-widget .message-text--rich > *:last-child  { margin-bottom: 0; }

.pdf-ai-chat-widget .md-p {
    margin: 0 0 0.85em;
    line-height: 1.55;
}

/* A wholly-bold line used as a section heading. */
.pdf-ai-chat-widget .md-p--lead {
    margin: 1.2em 0 0.4em;
    color: var(--pdf-chat-heading-color);
}

.pdf-ai-chat-widget .md-h {
    margin: 1.2em 0 0.45em;
    font-size: 1.05em;
    font-weight: var(--pdf-chat-heading-weight);
    line-height: 1.35;
    color: var(--pdf-chat-heading-color);
}

.pdf-ai-chat-widget .md-ul,
.pdf-ai-chat-widget .md-ol {
    margin: 0 0 0.85em;
    padding-left: 1.4em;
}

.pdf-ai-chat-widget .md-li {
    margin-bottom: 0.3em;
    line-height: 1.5;
}

.pdf-ai-chat-widget .md-li:last-child { margin-bottom: 0; }

.pdf-ai-chat-widget .message-text--rich strong {
    font-weight: 600;
    color: var(--pdf-chat-heading-color);
}

.pdf-ai-chat-widget .md-code {
    background: var(--pdf-chat-code-bg);
    color: var(--pdf-chat-code-text);
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 0.9em;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.pdf-ai-chat-widget .md-quote {
    margin: 0 0 0.85em;
    padding: 2px 0 2px 12px;
    border-left: 3px solid var(--pdf-chat-quote-border);
    color: var(--pdf-chat-quote-text);
}

.pdf-ai-chat-widget .md-quote .md-p:last-child { margin-bottom: 0; }

.pdf-ai-chat-widget .md-hr {
    margin: 1.1em 0;
    border: 0;
    border-top: 1px solid var(--pdf-chat-rule-color);
}

.pdf-ai-chat-widget .md-link {
    color: var(--pdf-chat-source-link-text);
    text-decoration: underline;
}

.pdf-ai-chat-widget .message-sources {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid #e5e7eb;
    font-size: 13px;
}

.pdf-ai-chat-widget .message-sources strong {
    display: block;
    margin-bottom: 6px;
    color: #6b7280;
    font-weight: 600;
}

.pdf-ai-chat-widget .source-link {
    display: inline-block;
    margin-right: 12px;
    margin-bottom: 6px;
    color: var(--pdf-chat-source-link-text);
    text-decoration: none;
    padding: 4px 8px;
    background: var(--pdf-chat-source-link-bg);
    border-radius: 6px;
    transition: all 0.2s;
}

.pdf-ai-chat-widget .source-link:hover {
    background: var(--pdf-chat-source-link-hover-bg);
    color: var(--pdf-chat-source-link-hover-text);
}

.pdf-ai-chat-widget .source-marker {
    font-weight: 600;
    margin-right: 2px;
    opacity: 0.85;
}

.pdf-ai-chat-widget .source-name {
    /* Just inheriting from source-link; here in case we want to style separately later. */
}

.pdf-ai-chat-widget .source-page {
    opacity: 0.75;
    margin-left: 4px;
}

/* Each source now sits on its own line so longer source lists with
   page numbers stay readable. */
.pdf-ai-chat-widget .source-link {
    display: block;
    margin-right: 0;
}

/* Inline [*N] citation links within the answer text. Sized to be
   easy to tap on mobile (min ~40px implied via line-height) while
   visually unobtrusive in flowing prose. */
.pdf-ai-chat-widget .inline-citation {
    display: inline-block;
    color: var(--pdf-chat-source-link-text);
    text-decoration: none;
    padding: 2px 4px;
    margin: 0 1px;
    border-radius: 4px;
    background: var(--pdf-chat-source-link-bg);
    font-weight: 600;
    font-size: 0.95em;
    transition: background 0.15s;
}

.pdf-ai-chat-widget .inline-citation:hover,
.pdf-ai-chat-widget .inline-citation:focus {
    background: var(--pdf-chat-source-link-hover-bg);
    color: var(--pdf-chat-source-link-hover-text);
    text-decoration: none;
}

.pdf-ai-chat-widget .loading-message {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: var(--pdf-chat-assistant-bubble-bg);
    border: 1px solid var(--pdf-chat-assistant-bubble-border);
    border-radius: 12px;
    max-width: 75%;
}

.pdf-ai-chat-widget .loading-phase-label {
    color: var(--pdf-chat-assistant-bubble-text);
    opacity: 0.75;
    font-size: 14px;
    line-height: 1.4;
}

.pdf-ai-chat-widget .loading-dots {
    display: flex;
    gap: 6px;
}

.pdf-ai-chat-widget .loading-dots span {
    width: 8px;
    height: 8px;
    background: var(--pdf-chat-loading-dot);
    border-radius: 50%;
    animation: pdf-chat-bounce 1.4s infinite ease-in-out;
}

.pdf-ai-chat-widget .loading-dots span:nth-child(1) { animation-delay: -0.32s; }
.pdf-ai-chat-widget .loading-dots span:nth-child(2) { animation-delay: -0.16s; }

@keyframes pdf-chat-bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

.pdf-ai-chat-widget .chat-options {
    margin-bottom: 12px;
}

.pdf-ai-chat-widget .knowledge-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 14px;
    color: #6b7280;
    user-select: none;
}

.pdf-ai-chat-widget .knowledge-toggle input[type="checkbox"] {
    cursor: pointer;
}

.pdf-ai-chat-widget .knowledge-toggle:hover {
    color: #374151;
}

.pdf-ai-chat-widget .chat-input-wrapper {
    display: flex;
    gap: 12px;
    align-items: flex-end;
}

.pdf-ai-chat-widget #chat-input {
    flex: 1;
    padding: 12px 16px;
    background: var(--pdf-chat-input-bg);
    color: var(--pdf-chat-input-text);
    border: 2px solid var(--pdf-chat-input-border);
    border-radius: 8px;
    font-size: var(--pdf-chat-input-font-size);
    font-family: var(--pdf-chat-input-font-family);
    font-weight: var(--pdf-chat-input-font-weight);
    resize: none;
    transition: border-color 0.2s;
}

.pdf-ai-chat-widget #chat-input:focus {
    outline: none;
    border-color: var(--pdf-chat-input-focus-border);
}

.pdf-ai-chat-widget .send-button {
    padding: 12px 24px;
    background: var(--pdf-chat-send-button-bg);
    color: var(--pdf-chat-send-button-text);
    border: none;
    border-radius: 8px;
    font-size: var(--pdf-chat-send-button-font-size);
    font-family: var(--pdf-chat-send-button-font-family);
    font-weight: var(--pdf-chat-send-button-font-weight);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.2s;
    white-space: nowrap;
}

.pdf-ai-chat-widget .send-button:hover {
    background: var(--pdf-chat-send-button-hover-bg);
    transform: translateY(-1px);
}

.pdf-ai-chat-widget .send-button:active {
    transform: translateY(0);
}

.pdf-ai-chat-widget .send-button:disabled {
    background: #9ca3af;
    cursor: not-allowed;
    transform: none;
}

.pdf-ai-chat-widget .error-message {
    background: var(--pdf-chat-error-bg);
    color: var(--pdf-chat-error-text);
    padding: 6px 12px;
    border-radius: 6px;
    margin-bottom: 12px;
    font-size: inherit;
    line-height: 1.5;
}

/* Scrollbar styling */
.pdf-ai-chat-widget .chat-messages::-webkit-scrollbar { width: 8px; }
.pdf-ai-chat-widget .chat-messages::-webkit-scrollbar-track { background: #f1f1f1; }
.pdf-ai-chat-widget .chat-messages::-webkit-scrollbar-thumb { background: #cbd5e0; border-radius: 4px; }
.pdf-ai-chat-widget .chat-messages::-webkit-scrollbar-thumb:hover { background: #a0aec0; }

/* Responsive */
@media (max-width: 768px) {
    .pdf-ai-chat-widget { margin: 20px 10px; border-radius: 8px; }
    .pdf-ai-chat-widget .chat-header { padding: 15px 20px; border-radius: 8px 8px 0 0; }
    /* Tighten the viewport cap on small screens. We adjust the cap
       rather than hard-setting max-height, so an admin's configured
       value is still respected where it fits — and is still overridden
       where it wouldn't. */
    .pdf-ai-chat-widget .chat-messages {
        padding: 15px 20px;
        --pdf-chat-messages-max-height-cap: 60vh;
    }
    .pdf-ai-chat-widget .message-content { max-width: 85%; }
    .pdf-ai-chat-widget .chat-input-container { padding: 15px 20px; }
    .pdf-ai-chat-widget .chat-input-wrapper { flex-direction: column; gap: 10px; }
    /* In a column-direction flex container, flex:1 means "fill vertical
       space", which leaves horizontal width to the textarea's default
       (governed by its `cols` attribute, hence the narrow appearance and
       whitespace on the left). Explicit 100% width fixes it. box-sizing:
       border-box keeps the textarea flush with the send button despite
       both having different padding. */
    .pdf-ai-chat-widget #chat-input { width: 100%; box-sizing: border-box; }
    .pdf-ai-chat-widget .send-button { width: 100%; justify-content: center; }
}
