/* ══════════════════════════════════════════════════════════════════════
   LLM RESPONSE STYLING
   --------------------------------------------------------------------
   Polished, ChatGPT/Claude/Perplexity-style typography and components
   for rendered markdown inside .response-text bubbles.

   Scoped to .response-text so existing app chrome is unaffected.
   Light + dark via semantic tokens. Respects prefers-reduced-motion.
   ══════════════════════════════════════════════════════════════════════ */

/* ── Design tokens (light) ─────────────────────────────────────────── */
.response-text {
    --llm-bg-primary: #ffffff;
    --llm-bg-soft: #f8fafc;
    --llm-bg-code: #f6f8fa;
    --llm-bg-inline-code: rgba(225, 29, 72, .07);

    --llm-text-primary: #0f172a;
    --llm-text-secondary: #475569;
    --llm-text-muted: #64748b;
    --llm-text-inline-code: #be123c;

    --llm-accent: #2563eb;
    --llm-accent-soft: rgba(37, 99, 235, .12);
    --llm-accent-strong: #1d4ed8;

    --llm-border: rgba(15, 23, 42, .12);
    --llm-border-soft: rgba(15, 23, 42, .06);

    --llm-tip: #16a34a;
    --llm-tip-bg: rgba(22, 163, 74, .08);
    --llm-warn: #d97706;
    --llm-warn-bg: rgba(217, 119, 6, .09);
    --llm-info: #2563eb;
    --llm-info-bg: rgba(37, 99, 235, .08);
    --llm-error: #dc2626;
    --llm-error-bg: rgba(220, 38, 38, .08);

    --llm-font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter",
        "Helvetica Neue", Arial, sans-serif;
    --llm-font-mono: "JetBrains Mono", "Fira Code", ui-monospace,
        SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;

    --llm-radius: 10px;
    --llm-radius-lg: 14px;
}

/* Dark mode — applies to the response area regardless of the rest
   of the app's theme. Activated by [data-theme="dark"] on <html>.
   Also auto-activates from system preference when no explicit theme. */
[data-theme="dark"] .response-text,
[data-theme="dark"] .llm-response {
    --llm-bg-primary: #1a1a1a;
    --llm-bg-soft: #202225;
    --llm-bg-code: #0f1115;
    --llm-bg-inline-code: rgba(244, 114, 182, .12);

    --llm-text-primary: #e7ecf3;
    --llm-text-secondary: #c4ccd6;
    --llm-text-muted: #94a3b8;
    --llm-text-inline-code: #f9a8d4;

    --llm-accent: #60a5fa;
    --llm-accent-soft: rgba(96, 165, 250, .18);
    --llm-accent-strong: #93c5fd;

    --llm-border: rgba(255, 255, 255, .12);
    --llm-border-soft: rgba(255, 255, 255, .06);

    --llm-tip: #4ade80;
    --llm-tip-bg: rgba(74, 222, 128, .12);
    --llm-warn: #fbbf24;
    --llm-warn-bg: rgba(251, 191, 36, .12);
    --llm-info: #60a5fa;
    --llm-info-bg: rgba(96, 165, 250, .14);
    --llm-error: #f87171;
    --llm-error-bg: rgba(248, 113, 113, .14);
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .response-text,
    :root:not([data-theme="light"]) .llm-response {
        --llm-bg-primary: #1a1a1a;
        --llm-bg-soft: #202225;
        --llm-bg-code: #0f1115;
        --llm-bg-inline-code: rgba(244, 114, 182, .12);

        --llm-text-primary: #e7ecf3;
        --llm-text-secondary: #c4ccd6;
        --llm-text-muted: #94a3b8;
        --llm-text-inline-code: #f9a8d4;

        --llm-accent: #60a5fa;
        --llm-accent-soft: rgba(96, 165, 250, .18);
        --llm-accent-strong: #93c5fd;

        --llm-border: rgba(255, 255, 255, .12);
        --llm-border-soft: rgba(255, 255, 255, .06);

        --llm-tip: #4ade80;
        --llm-tip-bg: rgba(74, 222, 128, .12);
        --llm-warn: #fbbf24;
        --llm-warn-bg: rgba(251, 191, 36, .12);
        --llm-info: #60a5fa;
        --llm-info-bg: rgba(96, 165, 250, .14);
        --llm-error: #f87171;
        --llm-error-bg: rgba(248, 113, 113, .14);
    }
}

/* ── Container — overrides terser legacy .response-text styles ───── */
.response-text {
    font-family: var(--llm-font-body);
    font-size: 16px;
    line-height: 1.7;
    color: var(--llm-text-primary);
    white-space: normal;
    word-break: normal;
    overflow-wrap: anywhere;
    /* Width is governed by the enclosing chat-bubble — don't double-cap. */
}

/* Vertical rhythm: 16px between siblings, 32px before major headings. */
.response-text > * + * {
    margin-top: 16px;
}

.response-text > * + h1,
.response-text > * + h2 {
    margin-top: 32px;
}

.response-text > * + h3 {
    margin-top: 24px;
}

/* ── Typography ──────────────────────────────────────────────────── */
.response-text h1,
.response-text h2,
.response-text h3,
.response-text h4,
.response-text h5,
.response-text h6 {
    color: var(--llm-text-primary);
    letter-spacing: -0.01em;
    line-height: 1.3;
}

.response-text h1 {
    font-size: 28px;
    font-weight: 700;
}

.response-text h2 {
    font-size: 22px;
    font-weight: 600;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--llm-border-soft);
}

.response-text h3 {
    font-size: 18px;
    font-weight: 600;
    position: relative;
    padding-left: 12px;
}

.response-text h3::before {
    /* colored accent bar */
    content: "";
    position: absolute;
    left: 0;
    top: 4px;
    bottom: 4px;
    width: 3px;
    border-radius: 2px;
    background: var(--llm-accent);
}

.response-text h4 {
    font-size: 16px;
    font-weight: 600;
}

.response-text h5,
.response-text h6 {
    font-size: 14px;
    font-weight: 600;
    color: var(--llm-text-secondary);
    text-transform: none;
}

.response-text p {
    margin: 0;
    font-size: 16px;
    line-height: 1.7;
    color: var(--llm-text-primary);
}

.response-text strong {
    font-weight: 650;
    color: var(--llm-text-primary);
}

.response-text em {
    color: inherit;
}

.response-text a {
    color: var(--llm-accent);
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
    text-decoration-color: var(--llm-accent-soft);
    transition: text-decoration-color .15s ease, color .15s ease;
}

.response-text a:hover {
    color: var(--llm-accent-strong);
    text-decoration-color: var(--llm-accent);
}

.response-text a:focus-visible {
    outline: 2px solid var(--llm-accent);
    outline-offset: 2px;
    border-radius: 3px;
}

/* ── Lists with custom bullets ───────────────────────────────────── */
.response-text ul,
.response-text ol {
    margin: 0;
    padding: 0;
    list-style: none;
}

.response-text li {
    position: relative;
    padding-left: 22px;
    line-height: 1.7;
}

.response-text li + li {
    margin-top: 14px;
}

/* Unordered — small, quiet neutral dot. Blue is reserved for the
   numbered section badges so the list doesn't read as a wall of blue. */
.response-text ul > li::before {
    content: "";
    position: absolute;
    left: 8px;
    top: 0.7em;
    width: 4px;
    height: 4px;
    border-radius: 50%;
    background: var(--llm-text-muted);
    opacity: .65;
}

/* Nested list bullets: thinner ring */
.response-text ul ul > li::before {
    background: transparent;
    border: 1px solid var(--llm-text-muted);
    opacity: .5;
    width: 5px;
    height: 5px;
}

/* Ordered — numbered badge. Flex layout so the badge stays aligned
   with the first text line regardless of font-size or line-height,
   and bold/long text wraps cleanly under itself rather than under
   the badge. The renderer wraps content in .llm-ol-text so inline
   children (strong, em, etc.) flow as a single flex item. */
.response-text ol > li {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding-left: 0;
}

.response-text ol > li::before {
    /* unused for ol — bullet is the explicit .llm-ol-num span */
    content: none;
}

.response-text .llm-ol-num {
    flex: 0 0 auto;
    min-width: 22px;
    height: 22px;
    padding: 0 6px;
    margin-top: 3px;        /* nudge to optical-center on first text line */
    border-radius: 999px;
    background: var(--llm-accent);
    color: #fff;
    font-family: var(--llm-font-body);
    font-size: 12px;
    font-weight: 600;
    line-height: 22px;
    text-align: center;
    box-sizing: border-box;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.response-text .llm-ol-text {
    flex: 1 1 auto;
    min-width: 0;
}

.response-text ul ul,
.response-text ul ol,
.response-text ol ul,
.response-text ol ol {
    margin-top: 8px;
}

/* ── Inline code ─────────────────────────────────────────────────── */
.response-text :not(pre) > code {
    font-family: var(--llm-font-mono);
    font-size: 0.88em;
    padding: 1px 6px;
    border-radius: 6px;
    background: var(--llm-bg-inline-code);
    color: var(--llm-text-inline-code);
    border: none;
}

/* ── Code blocks with header + copy button ──────────────────────── */
.response-text .llm-code {
    margin: 0;
    border-radius: var(--llm-radius);
    border: 1px solid var(--llm-border);
    background: var(--llm-bg-code);
    overflow: hidden;
    position: relative;
}

.response-text .llm-code-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 10px 6px 14px;
    background: var(--llm-bg-soft);
    border-bottom: 1px solid var(--llm-border-soft);
    font-family: var(--llm-font-body);
    font-size: 12px;
    color: var(--llm-text-muted);
    letter-spacing: 0.02em;
}

.response-text .llm-code-lang {
    text-transform: lowercase;
    font-weight: 500;
}

.response-text .llm-code-copy {
    appearance: none;
    border: 1px solid transparent;
    background: transparent;
    color: var(--llm-text-secondary);
    padding: 4px 10px;
    border-radius: 6px;
    font: inherit;
    font-size: 12px;
    cursor: pointer;
    opacity: 0;
    transition: opacity .15s ease, background .15s ease, color .15s ease, border-color .15s ease;
    min-height: 28px;
}

.response-text .llm-code:hover .llm-code-copy,
.response-text .llm-code-copy:focus-visible {
    opacity: 1;
}

.response-text .llm-code-copy:hover {
    background: var(--llm-accent-soft);
    color: var(--llm-accent-strong);
    border-color: var(--llm-accent-soft);
}

.response-text .llm-code-copy:focus-visible {
    outline: 2px solid var(--llm-accent);
    outline-offset: 2px;
}

.response-text .llm-code-copy.is-copied {
    color: var(--llm-tip);
    opacity: 1;
}

.response-text .llm-code pre {
    margin: 0;
    padding: 14px 16px;
    border: 0;
    background: transparent;
    overflow-x: auto;
    font-family: var(--llm-font-mono);
    font-size: 13.5px;
    line-height: 1.6;
    color: var(--llm-text-primary);
}

.response-text .llm-code code {
    font-family: var(--llm-font-mono);
    color: inherit;
    background: transparent;
    padding: 0;
    border-radius: 0;
}

/* Fallback for any bare <pre> that wasn't wrapped */
.response-text > pre,
.response-text pre:not(.llm-code pre) {
    border-radius: var(--llm-radius);
    border: 1px solid var(--llm-border);
    background: var(--llm-bg-code);
    padding: 14px 16px;
    font-family: var(--llm-font-mono);
    font-size: 13.5px;
    line-height: 1.6;
    overflow-x: auto;
}

/* ── Callouts (tip / warning / info / error) ────────────────────── */
.response-text .llm-callout {
    display: flex;
    gap: 12px;
    align-items: flex-start;
    padding: 12px 14px;
    border-radius: var(--llm-radius);
    border-left: 4px solid var(--llm-info);
    background: var(--llm-info-bg);
    color: var(--llm-text-primary);
}

.response-text .llm-callout-icon {
    flex: 0 0 auto;
    font-size: 18px;
    line-height: 1.4;
    user-select: none;
}

.response-text .llm-callout-body {
    flex: 1 1 auto;
    min-width: 0;
}

.response-text .llm-callout-title {
    font-weight: 650;
    margin-bottom: 4px;
    color: var(--llm-text-primary);
    font-size: 14px;
    letter-spacing: 0.01em;
    text-transform: uppercase;
}

.response-text .llm-callout-body > p {
    margin: 0;
}

.response-text .llm-callout-body > p + p {
    margin-top: 8px;
}

.response-text .llm-callout-tip {
    border-left-color: var(--llm-tip);
    background: var(--llm-tip-bg);
}

.response-text .llm-callout-tip .llm-callout-title {
    color: var(--llm-tip);
}

.response-text .llm-callout-warn {
    border-left-color: var(--llm-warn);
    background: var(--llm-warn-bg);
}

.response-text .llm-callout-warn .llm-callout-title {
    color: var(--llm-warn);
}

.response-text .llm-callout-info {
    border-left-color: var(--llm-info);
    background: var(--llm-info-bg);
}

.response-text .llm-callout-info .llm-callout-title {
    color: var(--llm-info);
}

.response-text .llm-callout-error {
    border-left-color: var(--llm-error);
    background: var(--llm-error-bg);
}

.response-text .llm-callout-error .llm-callout-title {
    color: var(--llm-error);
}

/* ── Blockquote ──────────────────────────────────────────────────── */
.response-text blockquote {
    margin: 0;
    padding: 10px 14px;
    border-left: 4px solid var(--llm-accent);
    background: var(--llm-accent-soft);
    border-radius: 0 var(--llm-radius) var(--llm-radius) 0;
    color: var(--llm-text-secondary);
    font-style: italic;
}

.response-text blockquote > p {
    margin: 0;
}

.response-text blockquote > p + p {
    margin-top: 8px;
}

/* ── Tables ──────────────────────────────────────────────────────── */
.response-text .response-table-wrap {
    margin: 0;
    border: 1px solid var(--llm-border);
    border-radius: var(--llm-radius);
    overflow-x: auto;
}

.response-text table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    background: var(--llm-bg-primary);
}

.response-text thead {
    background: var(--llm-bg-soft);
}

.response-text th,
.response-text td {
    border: 0;
    border-bottom: 1px solid var(--llm-border-soft);
    padding: 10px 14px;
    text-align: left;
    vertical-align: top;
    color: var(--llm-text-primary);
    font-size: 14px;
}

.response-text th {
    font-weight: 600;
    color: var(--llm-text-secondary);
    font-size: 13px;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    border-bottom: 1px solid var(--llm-border);
}

.response-text tbody tr {
    transition: background .15s ease;
}

.response-text tbody tr:hover {
    background: var(--llm-bg-soft);
}

.response-text tbody tr:last-child td {
    border-bottom: 0;
}

/* Stacked table override for narrow widths */
.response-text table.is-stacked tr {
    border: 1px solid var(--llm-border);
    border-radius: var(--llm-radius);
    background: var(--llm-bg-primary);
}

.response-text table.is-stacked td {
    border: none;
    border-bottom: 1px solid var(--llm-border-soft);
}

.response-text table.is-stacked td::before {
    color: var(--llm-text-muted);
}

/* ── Citation markers (Perplexity-style footnotes) ───────────────
   Intentionally small and quiet — the full sources strip is shown
   below the response. These should read as superscript footnotes,
   not interactive buttons. Adjacent citations collapse without
   visible gaps so `[1][2][3]` reads as one unit. */
.response-text .llm-cite {
    display: inline-block;
    min-width: 14px;
    padding: 0 4px;
    margin: 0;
    border-radius: 6px;
    background: transparent;
    color: var(--llm-text-muted);
    font-family: var(--llm-font-body);
    font-size: 0.7em;
    font-weight: 600;
    line-height: 1.4;
    text-align: center;
    text-decoration: none;
    vertical-align: super;
    cursor: pointer;
    transition: background .15s ease, color .15s ease;
    border: 1px solid var(--llm-border-soft);
}

/* Adjacent citations: drop the left border so they read as a group */
.response-text .llm-cite + .llm-cite {
    border-left: 0;
    border-top-left-radius: 0;
    border-bottom-left-radius: 0;
    margin-left: -1px;
}

.response-text .llm-cite:not(:has(+ .llm-cite)) {
    /* tail of the group keeps its right radius */
}

.response-text .llm-cite:hover,
.response-text .llm-cite:focus-visible {
    background: var(--llm-accent-soft);
    color: var(--llm-accent-strong);
    text-decoration: none;
}

.response-text .llm-cite:focus-visible {
    outline: 2px solid var(--llm-accent);
    outline-offset: 1px;
}

/* ── Horizontal rule ─────────────────────────────────────────────── */
.response-text hr {
    border: 0;
    height: 1px;
    background: var(--llm-border-soft);
    margin: 24px 0;
}

/* ── Streaming animation ────────────────────────────────────────── */
.response-text > * {
    animation: llmFadeIn 0.3s ease-in both;
}

@keyframes llmFadeIn {
    from {
        opacity: 0;
        transform: translateY(2px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Typing cursor at end of streaming text */
.response-text[data-streaming="true"]::after {
    content: "";
    display: inline-block;
    width: 2px;
    height: 1em;
    background: var(--llm-accent);
    margin-left: 2px;
    vertical-align: -2px;
    animation: llmCursorBlink 1s steps(2, start) infinite;
}

@keyframes llmCursorBlink {
    to {
        visibility: hidden;
    }
}

@media (prefers-reduced-motion: reduce) {
    .response-text > *,
    .response-text[data-streaming="true"]::after {
        animation: none;
    }
}

/* ── Responsive ──────────────────────────────────────────────────── */
@media (max-width: 720px) {
    .response-text {
        font-size: 15px;
        line-height: 1.65;
    }

    .response-text h1 { font-size: 24px; }
    .response-text h2 { font-size: 20px; }
    .response-text h3 { font-size: 17px; }

    .response-text .llm-code pre {
        font-size: 13px;
        padding: 12px 14px;
    }

    .response-text li {
        padding-left: 24px;
    }

    .response-text ol > li {
        padding-left: 32px;
    }

    /* Touch-friendly tap targets */
    .response-text .llm-code-copy {
        min-height: 36px;
        opacity: 1; /* always visible on touch */
    }

    .response-text .llm-cite {
        min-width: 24px;
        height: 24px;
        font-size: 12px;
    }
}
