:root {
    /* JS converts this vw value to a physical px value to eliminate calc jitter during resizing */
    --base-font-size: clamp(12px, 2.35vw, 40px);
    --base-line-height: 1.25;
    --content-max-width: 3000px;
}

body {
    font-family: "Times New Roman", "Cambria", "Latin Modern Roman", "Georgia", serif;
    padding: 1em 1em;
    margin: 0 auto;
    max-width: var(--content-max-width);
    font-size: var(--base-font-size);
    line-height: var(--base-line-height);
    color: var(--vscode-editor-foreground);
    background-color: var(--vscode-editor-background);

    text-align: justify;
    text-justify: auto;

    hyphens: manual;
    -webkit-hyphens: manual;
    -moz-hyphens: manual;

    word-wrap: break-word;
    overflow-wrap: break-word;
}

/* --- Heading System: Use em for scaling relative to body text --- */
h1, h2, h3, h4 {
    font-family: inherit;
    font-weight: bold;
    line-height: 1.2;
}

/* Paper Title \title */
h1.latex-title {
    font-size: 1.7em;
    text-align: center;
    margin-bottom: 0.5em;
    margin-top: 0.5em;
}

/* Author Info \author */
div.latex-author {
    font-size: 1.1em;
    text-align: center;
    margin-bottom: 0.25em;
    font-style: italic;
}

.latex-author-item {
    display: inline-block;
    margin: 0 0.55em 0.35em;
    text-align: center;
    vertical-align: top;
}

.latex-author-email {
    display: block;
    font-size: 0.86em;
    font-style: normal;
}

.latex-affiliations {
    text-align: center;
    margin-bottom: 0.75em;
}

.latex-editor {
    text-align: left;
    margin-bottom: 0.75em;
}

/* Date Info \date */
div.latex-date {
    font-size: 1.1em;
    text-align: center;
    margin-bottom: 2em;
    font-style: italic;
}

/* Section Heading \section (##) */
h2 {
    font-size: 1.4em;
    margin-top: 1em !important;
    margin-bottom: 0.7em !important;
}

/* Subsection Heading \subsection (###) */
h3 {
    font-size: 1.2em;
    margin-top: 1.0em !important;
    margin-bottom: 0.7em !important;
}

/* Sub-subsection Heading \subsubsection (####) */
h4 {
    font-size: 1.05em;
    margin-top: 0.5em !important;
    margin-bottom: 0.5em !important;
    font-style: italic;
}

/* Improve local update efficiency */
/* Preload/Full Render Mode: Force calculation of all layouts */
/* When body or container has this class, optimization is disabled */
.preload-mode .latex-block {
    content-visibility: visible !important;
    contain-intrinsic-size: unset !important;
    contain: none !important;
}

/* Default State: Enable high-performance local rendering */
.latex-block {
    /* Use auto with a specific pixel value to help browser "remember" previously rendered height */
    content-visibility: auto;
    contain-intrinsic-size: auto var(--avg-height, 7em);
    contain: layout paint style;
}

.latex-block-shell {
    box-sizing: border-box;
    overflow: hidden;
}

.latex-block-shell > .latex-block {
    content-visibility: visible;
    contain-intrinsic-size: unset;
}

/* --- Body Text Typography --- */
.latex-block p {
    text-indent: 2em; /* Standard LaTeX 2em indentation */
    margin-top: 0.2em;
    margin-bottom: 0.2em;
    word-break: break-word;
    text-rendering: optimizeLegibility;
}

/* Remove indentation in specific contexts */
h1 + p, h2 + p, h3 + p, h4 + p,
.latex-title + div + p,
.latex-abstract + p,
ul + p, ol + p,
p:has(.no-indent-marker) {
    text-indent: 0 !important;
}

.no-indent-marker { display: none; }

/* --- Math Formula KaTeX Adaptation --- */
.katex {
    /* Inherit parent font size, avoid using calc to force calculation */
    font-size: 1em !important;
}

.katex-display {
    margin: 1em 0 !important;
    overflow-x: auto;
    overflow-y: hidden;
    contain: layout;
}

/* TikZ Container */
.tikz-container {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    margin: 0 0;
    text-align: center;
    overflow: visible;
}

.tikz-container svg {
    width: 100% !important;
    height: auto !important;

    display: flex;
    margin: 0 auto;

    max-width: none !important;

    color: var(--vscode-editor-foreground);
    text-align: center;
}

.tikz-container[data-tikz-state="queued"] > svg:not(.tikz-stale-preview),
.tikz-container[data-tikz-state="rendering"] > svg:not(.tikz-stale-preview),
.tikz-container[data-tikz-state="stale"] > svg:not(.tikz-stale-preview) {
    display: none;
}

.tikz-stale-preview {
    opacity: 0.72;
}

/* ==========================================================================
   Dark Mode Adaptation for TikZ
   ========================================================================== */

/* Strategy: In dark mode, TikZ's default "black" (#000) is invisible.
   We use Attribute Selectors to target elements specifically set to black,
   and force them to use the VS Code foreground color (light).

   We do NOT use 'filter: invert(1)' because that would mess up Red/Blue colors.
*/

/* 1. Invert Black Strokes (Lines, Arrows, Borders) */
body:is(.vscode-dark, [data-theme="dark"]) .tikz-container svg [stroke="#000"],
body:is(.vscode-dark, [data-theme="dark"]) .tikz-container svg [stroke="black"],
body:is(.vscode-dark, [data-theme="dark"]) .tikz-container svg [stroke="rgb(0,0,0)"],
body:is(.vscode-dark, [data-theme="dark"]) .tikz-container svg [stroke="rgb(0, 0, 0)"] {
    stroke: var(--vscode-editor-foreground) !important;
}

/* 2. Invert Black Fills (Text, Solid Shapes) */
/* Note: Pattern fills use url(#id), so they won't match these selectors, which is good. */
body:is(.vscode-dark, [data-theme="dark"]) .tikz-container svg [fill="#000"],
body:is(.vscode-dark, [data-theme="dark"]) .tikz-container svg [fill="black"],
body:is(.vscode-dark, [data-theme="dark"]) .tikz-container svg [fill="rgb(0,0,0)"],
body:is(.vscode-dark, [data-theme="dark"]) .tikz-container svg [fill="rgb(0, 0, 0)"] {
    fill: var(--vscode-editor-foreground) !important;
}

/* 3. Ensure Text elements default to foreground if no fill is specified */
body:is(.vscode-dark, [data-theme="dark"]) .tikz-container svg text {
    fill: var(--vscode-editor-foreground);
}

/* ==========================================================================
   Theorem Blocks (Theme-Adaptive Box)
   ========================================================================== */

.latex-theorem {
    /* Keep theorem boxes readable across host themes without hard-coded colors. */
    background-color: color-mix(in srgb, var(--vscode-textLink-foreground) 10%, transparent);
    border-left: 4px solid color-mix(in srgb, var(--vscode-textLink-foreground) 40%, transparent);

    padding: 10px 15px;
    margin: 1.5em 0;
    border-radius: 4px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
    page-break-inside: avoid;
}

.latex-theorem-header {
    color: var(--vscode-textLink-foreground);
}

.latex-style-scope .latex-theorem-header {
    color: inherit;
}

/* Paragraph handling inside theorem */
.latex-theorem p {
    margin: 0.5em 0;
    display: inline-block;
    width: 100%;
}
.latex-theorem p:first-of-type {
    margin-top: 0;
    display: inline;
}

/* Proof QED Symbol */
.latex-qed {
    color: var(--vscode-editor-foreground);
    opacity: 0.7;
}


/* --- Float Placeholders (Figure/Table) --- */
.latex-float-placeholder {
    display: block;
    border: 2px dashed #ff7875;
    background-color: rgba(255, 120, 117, 0.05);
    color: #cf1322;
    padding: 1em;
    margin: 1.5em 0;
    border-radius: 6px;
    font-family: "Courier New", monospace;
    font-size: 0.7em;
    line-height: 1.0;
    white-space: pre-wrap;
    overflow-x: auto;
}

/* Verbatim/code blocks */
.latex-verbatim {
    display: block;
    margin: 1em 0;
    padding: 0.85em 1em;
    border: 1px solid var(--vscode-widget-border, rgba(127, 127, 127, 0.35));
    border-radius: 6px;
    background-color: var(--vscode-textCodeBlock-background, rgba(127, 127, 127, 0.10));
    overflow-x: auto;
    white-space: pre;
}

.latex-verbatim code {
    font-family: var(--vscode-editor-font-family, "Courier New", monospace);
    font-size: 0.86em;
}

/* Algorithm Block Styles */
.latex-algorithm {
    margin: 1.5em 0;
    border-top: 2px solid var(--vscode-editor-foreground);
    border-bottom: 2px solid var(--vscode-editor-foreground);
    padding: 10px;
}

/* Caption */
.latex-algorithm .alg-caption {
    text-align: center;
    font-weight: bold;
    margin-bottom: 10px;
}

/* Lists (ul/ol) */
.latex-algorithm .alg-list {
    padding-left: 2em;
    margin: 10px 0;
}

/* Handle list styles based on tag type */
.latex-algorithm ol.alg-list {
    list-style: decimal;
}

.latex-algorithm ul.alg-list {
    list-style: none;
}

/* List Items (General) */
.latex-algorithm .alg-item {
    margin-bottom: 3px;
    padding-left: 5px; /* Standard indentation/padding */
}

.latex-figure { text-align: center; margin: 20px 0; }
.latex-figure img { max-width: 100%; height: auto; }
.latex-subfigure-grid {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.75em 1.2em;
    align-items: flex-start;
}
.latex-subfigure {
    box-sizing: border-box;
    min-width: 12em;
    text-align: center;
}
.latex-subfigure canvas,
.latex-subfigure img {
    width: 100%;
}
.subfigure-caption {
    margin-top: 0.35em;
    font-size: 0.92em;
    line-height: 1.3;
}

/* Table preview */
.latex-table {
    margin: 1.35em 0;
}

.latex-table .table-caption {
    text-align: center;
    margin: 0 0 0.65em;
    font-size: 0.95em;
    line-height: 1.35;
}

.latex-table .table-body {
    width: 100%;
    overflow-x: auto;
    display: block;
    margin-bottom: 0.6em;
    -webkit-overflow-scrolling: touch;
    scrollbar-color: var(--vscode-scrollbarSlider-background) transparent;
    scrollbar-width: thin;
}

.latex-table table {
    width: auto;
    min-width: min(100%, 38rem);
    max-width: none;
    border-collapse: collapse;
    border-spacing: 0;
    margin: 0 auto;
    font-size: 0.94em;
    line-height: 1.35;
    color: var(--vscode-editor-foreground);
}

.latex-style-scope .latex-table table {
    color: inherit;
}

.latex-table .latex-tabular-preview {
    border-top: 1.6px solid var(--vscode-editor-foreground);
    border-bottom: 1.6px solid var(--vscode-editor-foreground);
}

.latex-table td,
.latex-table th {
    padding: 0.42em 0.85em;
    border: 0;
    vertical-align: top;
    text-align: left;
}

.latex-table th {
    font-weight: 600;
}

.latex-table thead tr:last-child th {
    border-bottom: 1px solid var(--vscode-editor-foreground);
}

.latex-table tr.table-row-rule-above td,
.latex-table tr.table-row-rule-above th {
    border-top: 1px solid var(--vscode-editor-foreground);
}

.latex-table .table-cell-align-center {
    text-align: center;
}

.latex-table .table-cell-align-right {
    text-align: right;
}

.latex-table .latex-nested-tabular {
    display: inline-flex;
    flex-direction: column;
    gap: 0.12em;
    min-width: max-content;
    vertical-align: middle;
}

.latex-table .latex-nested-tabular-row {
    display: flex;
    align-items: baseline;
}

.latex-table .latex-nested-tabular-cell {
    display: inline-block;
    white-space: nowrap;
}

.latex-table .latex-nested-tabular-math {
    align-items: flex-start;
}

.latex-table .latex-makecell {
    display: inline-flex;
    flex-direction: column;
    gap: 0.12em;
    vertical-align: middle;
}

.latex-table .latex-makecell-line {
    display: block;
    white-space: nowrap;
}

.latex-table .latex-tnote {
    margin-left: 0.12em;
    font-size: 0.72em;
    line-height: 0;
    vertical-align: super;
}

.latex-table .latex-tablenotes {
    max-width: min(100%, 52rem);
    margin: 0.45em auto 0;
    font-size: 0.86em;
    line-height: 1.35;
}

.latex-table .latex-tablenotes ul {
    margin: 0;
    padding-left: 0;
}

.latex-table .table-body::-webkit-scrollbar {
    height: 8px;
}

.latex-table .table-body::-webkit-scrollbar-thumb {
    background-color: var(--vscode-scrollbarSlider-background);
    border-radius: 4px;
}

.latex-table .table-body::-webkit-scrollbar-track {
    background-color: var(--vscode-editor-background);
}

/* --- Abstract & Keywords --- */
.latex-abstract {
    margin: 1.2em 3.0em; /* Use em for left/right margins */
    font-size: 0.92em;    /* Abstract font size is slightly smaller */
    line-height: 1.4;
}

.latex-abstract-title {
    display: block;
    text-align: center;
    font-weight: bold;
    margin-bottom: 0.6em;
    font-variant: small-caps;
}

.latex-keywords {
    margin: 0.5em 3.5em 2em 3.5em;
    font-size: 0.9em;
    font-style: italic;
}

.latex-keywords strong {
    font-style: normal;
    font-weight: bold;
    margin-right: 0.5em;
}

/* --- List System --- */
ul, ol {
    padding-left: 2.0em;
    margin: 0.5em 0;
}

li {
    margin-bottom: 0.4em;
}

/* Nested Ordered List Styles (Academic Style) */
/* Level 1: 1, 2, 3 */
ol { list-style-type: decimal; }

/* Level 2: a, b, c */
ol ol { list-style-type: lower-alpha; }

/* Level 3: i, ii, iii */
ol ol ol { list-style-type: lower-roman; }

/* Fix alignment for custom label lists (\item[Label]) */
.latex-list-custom-label {
    list-style: none;
}

.latex-list-label {
    font-weight: 600;
    margin-right: 0.5em;
}


/* General Basic Styles for Citation Links */
.latex-link {
    color: #007acc;
    padding: 0 0.1em;
    transition: background-color 0.2s;
}

.latex-cite, .latex-citep, .latex-citet {
    color: #2e7d32;
}

.latex-citep-container::before { content: "("; }
.latex-citep-container::after { content: ")"; }

.latex-eqref::before { content: "("; }
.latex-eqref::after { content: ")"; }
/* Hover Effect */
.latex-link:hover {
    background-color: rgba(0, 122, 204, 0.2);
    text-decoration: underline;
}

/* --- Jump Highlight Animation System --- */

/* Level 1: Block Highlight - Faint yellow background */
@keyframes flash-highlight {
    0% { background-color: transparent; }
    20% { background-color: rgba(255, 235, 59, 0.4); } /* Faint Yellow */
    100% { background-color: transparent; }
}

.jump-highlight {
    animation: flash-highlight 1.4s ease-out;
}

/* Level 2: Precise Word Highlight - Strong orange background */
@keyframes fade-word {
    0% { background-color: transparent; }
    20% { background-color: rgba(255, 235, 59, 0.45) }
    100% {
        background-color: transparent;
    }
}

.highlight-word {
    animation: fade-word 1.5s ease-out;
}


/* --- Main Floating Window --- */
.hover-tooltip {
    position: fixed;
    z-index: 1000;

    display: flex;
    flex-direction: column;

    left: 2vw;
    top: 0;

    box-sizing: border-box;

    /* Visuals */
    background: var(--vscode-editor-background, #ffffff);
    color: var(--vscode-editor-foreground, #333333);
    border: 1px solid var(--vscode-widget-border, #dcdcdc);
    box-shadow: 0 12px 48px rgba(0, 0, 0, 0.25);
    border-radius: 8px;

    /* Sizing */
    width: max-content;
    min-width: 300px;
    max-width: 96vw;
    height: auto;
    max-height: 50vh;
    min-height: 100px;

    pointer-events: auto;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease-in-out;

    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

.hover-tooltip.visible {
    opacity: 0.98;
    visibility: visible;
}

/* --- Header --- */
.tooltip-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 2px 8px;
    background-color: var(--vscode-editorWidget-background, rgba(127, 127, 127, 0.1));
    border-bottom: 1px solid var(--vscode-widget-border, #dcdcdc);
    cursor: grab;
    user-select: none;
    flex-shrink: 0;
    border-radius: 8px 8px 0 0;
}

.tooltip-header:active {
    cursor: grabbing;
}

.drag-handle-icon {
    color: var(--vscode-descriptionForeground, #999);
    font-size: 0.8em;
    letter-spacing: 2px;
    opacity: 0.7;
    flex: 1;
}

/* --- Controls (Pin/Close) --- */
.header-controls {
    display: flex;
    gap: 4px;
}

/* Shared styles for all header buttons */
.icon-btn {
    background: none;
    border: none;
    color: var(--vscode-icon-foreground, #666);
    cursor: pointer;
    font-size: 0.6em;
    padding: 2px 6px;
    border-radius: 4px;
    transition: all 0.1s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover {
    background-color: var(--vscode-toolbar-hoverBackground, rgba(90, 93, 94, 0.31));
}

.pin-btn.active {
    color: var(--vscode-editor-background, white);
    background-color: var(--vscode-button-background, #007acc);
}

.close-btn:hover {
    background-color: #c42b1c;
    color: white;
}

.tooltip-content {
    flex: 1;
    padding: 12px 16px;
    overflow-y: auto;
    overflow-x: hidden;
    font-size: 0.85em;
    line-height: 1.1;
    margin-right: 1px;
    margin-bottom: 1px;
}

/* --- Resize Handles --- */
.resize-handle-bottom {
    height: 8px;
    width: 100%;
    background: transparent;
    cursor: ns-resize;
    flex-shrink: 0;
    position: relative;
    z-index: 10;
}
.resize-handle-bottom::after {
    content: ''; position: absolute; left: 50%; top: 3px; width: 30px; height: 2px; background-color: var(--vscode-scrollbarSlider-background, #ccc); transform: translateX(-50%); border-radius: 2px;
}

.resize-handle-right {
    position: absolute; right: 0; top: 0; bottom: 12px; width: 4px; background: transparent; cursor: ew-resize; z-index: 20;
}

.resize-handle-corner {
    position: absolute; right: 0; bottom: 0; width: 14px; height: 14px; cursor: nwse-resize; z-index: 20;
}
.resize-handle-corner::after {
    content: ''; position: absolute; right: 3px; bottom: 3px; width: 6px; height: 6px; border-right: 2px solid var(--vscode-scrollbarSlider-background, #999); border-bottom: 2px solid var(--vscode-scrollbarSlider-background, #999);
}

/* --- Latex Injection Styles --- */
.hover-tooltip .context-block { opacity: 0.6; transform: scale(0.99); margin-bottom: 8px !important; border-left: 3px solid transparent; }
.hover-tooltip .target-block { border-left: 4px solid var(--vscode-textLink-activeForeground, #007acc); padding-left: 12px !important; background-color: var(--vscode-editor-selectionBackground, rgba(0, 0, 0, 0.05)); border-radius: 4px; margin: 12px 0 !important; }
.hover-tooltip .latex-block { margin: 0 !important; padding: 2px 0 !important; border: none !important; box-shadow: none !important; background: transparent !important; }

/* --- Scrollbars --- */
/* Apply globally to webview body and tooltip content */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background-color: var(--vscode-scrollbarSlider-background); border-radius: 5px; border: 2px solid transparent; background-clip: padding-box !important;}
::-webkit-scrollbar-thumb:hover { background-color: var(--vscode-scrollbarSlider-hoverBackground); }
::-webkit-scrollbar-corner { background: transparent; }

/* Styles for VS Code (Webkit) */
/* Force application to all elements */
html, body {
    scrollbar-width: unset !important;
    scrollbar-color: unset !important;
}

::-webkit-scrollbar {
    width: 10px !important;
    height: 10px !important;
}

::-webkit-scrollbar-track {
    background: transparent !important;
}

::-webkit-scrollbar-thumb {
    background-color: var(--vscode-scrollbarSlider-background) !important;
    border-radius: 5px !important;
    border: 2px solid transparent !important;
    background-clip: content-box !important;
}

::-webkit-scrollbar-thumb:hover {
    background-color: var(--vscode-scrollbarSlider-hoverBackground) !important;
}

::-webkit-scrollbar-thumb:active {
    background-color: var(--vscode-scrollbarSlider-activeBackground) !important;
}

::-webkit-scrollbar-corner {
    background: transparent !important;
}
