/* ==========================================================================
   Markdown Rendering Styles
   ========================================================================== */

/* Headings */
.md-heading {
    color: var(--text-primary);
    font-weight: 600;
    margin: 1rem 0 0.5rem 0;
    line-height: 1.3;
}

.md-h1 {
    font-size: 1.5rem;
    border-bottom: 1px solid var(--border);
    padding-bottom: 0.3rem;
}

.md-h2 {
    font-size: 1.3rem;
    border-bottom: 1px solid var(--border);
    padding-bottom: 0.2rem;
}

.md-h3 {
    font-size: 1.15rem;
}

.md-h4 {
    font-size: 1.05rem;
}

.md-h5 {
    font-size: 1rem;
}

.md-h6 {
    font-size: 0.95rem;
    color: var(--text-secondary);
}

/* First heading in a message shouldn't have top margin */
.assistant-text > .md-heading:first-child,
.user-text > .md-heading:first-child {
    margin-top: 0;
}

/* Paragraphs */
.md-paragraph {
    margin: 0.5rem 0;
    line-height: 1.6;
}

.md-paragraph:first-child {
    margin-top: 0;
}

.md-paragraph:last-child {
    margin-bottom: 0;
}

/* Text formatting */
.md-strong {
    font-weight: 600;
}

.md-emphasis {
    font-style: italic;
}

.md-strikethrough {
    text-decoration: line-through;
    color: var(--text-secondary);
}

/* Links */
.md-link {
    color: var(--link-color);
    text-decoration: underline;
    text-decoration-style: dotted;
    text-underline-offset: 2px;
    transition: all 0.15s ease;
}

.md-link:hover {
    color: var(--accent-hover);
    text-decoration-style: solid;
}

.md-link:visited {
    color: var(--link-visited);
}

/* Session-file download feedback (see markdown/portal_file_link.rs). The pull
   endpoint is fallible, so the outcome has to be reported in place rather than
   by navigating to an error page. */
.portal-file-status,
.portal-file-error {
    margin-left: 0.5rem;
    font-size: 0.85em;
}

.portal-file-status {
    color: var(--text-muted);
}

.portal-file-error {
    color: var(--error-color, #f7768e);
}

/* Inline code */
.md-inline-code {
    font-family: var(--font-mono);
    font-size: 0.9em;
    background: rgba(0, 0, 0, 0.3);
    padding: 0.15rem 0.35rem;
    border-radius: 4px;
    color: var(--accent);
}

/* Code blocks */
.md-code-block {
    position: relative;
    margin: 0.75rem 0;
    background: var(--bg-darker);
    border: 1px solid var(--border);
    border-radius: 6px;
    overflow-x: auto;
}

.md-code-block .md-code {
    display: block;
    padding: 0.75rem 1rem;
    font-family: var(--font-mono);
    font-size: 0.85rem;
    line-height: 1.5;
    color: var(--text-primary);
    white-space: pre;
}

.code-copy-button {
    position: absolute;
    top: 4px;
    right: 4px;
    padding: 0.2rem 0.5rem;
    background: var(--border);
    border: none;
    border-radius: 4px;
    color: var(--text-secondary);
    font-size: 0.7rem;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s, background 0.15s;
}

.md-code-block:hover .code-copy-button {
    opacity: 1;
}

.code-copy-button:hover {
    background: var(--accent);
    color: white;
}

.code-copy-button.copied {
    opacity: 1;
    background: var(--success);
    color: white;
}

/* Blockquotes */
.md-blockquote {
    margin: 0.75rem 0;
    padding: 0.5rem 1rem;
    border-left: 3px solid var(--accent);
    background: rgba(122, 162, 247, 0.08);
    color: var(--text-secondary);
}

.md-blockquote .md-paragraph {
    margin: 0.25rem 0;
}

/* Lists */
.md-list {
    margin: 0.5rem 0;
    padding-left: 1.5rem;
}

.md-ordered-list {
    list-style-type: decimal;
}

.md-unordered-list {
    list-style-type: disc;
}

.md-list-item {
    margin: 0.25rem 0;
    line-height: 1.5;
}

/* Nested lists */
.md-list .md-list {
    margin: 0.25rem 0;
}

/* Horizontal rule */
.md-rule {
    border: none;
    border-top: 1px solid var(--border);
    margin: 1rem 0;
}

/* Tables */
.md-table-wrapper {
    margin: 0.75rem 0;
    overflow-x: auto;
}

.md-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.md-table-head {
    background: var(--bg-darker);
}

.md-table-header {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    font-weight: 600;
    color: var(--text-primary);
}

.md-table-cell {
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    color: var(--text-primary);
}

.md-table-row:nth-child(even) {
    background: rgba(0, 0, 0, 0.15);
}

/* Images */
.md-image {
    max-width: 100%;
    height: auto;
    border-radius: 4px;
    margin: 0.5rem 0;
}

/* Remove pre-wrap on markdown-rendered text since we handle whitespace */
.assistant-text:has(.md-paragraph),
.assistant-text:has(.md-heading),
.assistant-text:has(.md-list),
.assistant-text:has(.md-table),
.user-text:has(.md-paragraph),
.user-text:has(.md-heading) {
    white-space: normal;
}

/* Tool Use Styling */
.tool-use {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    padding: 0.5rem 0.75rem;
    background: rgba(99, 102, 241, 0.1);
    border-left: 3px solid var(--accent);
    border-radius: 0 4px 4px 0;
    margin: 0.5rem 0;
    font-family: var(--font-mono);
    font-size: 0.85rem;
    overflow: hidden;
}

.tool-use-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
    overflow: hidden;
}

.tool-icon {
    font-size: 0.9rem;
}

.tool-name {
    color: var(--accent);
    font-weight: 600;
    flex-shrink: 0;
}

.tool-args {
    color: var(--text-secondary);
    margin: 0;
    padding: 0.25rem 0;
    white-space: pre-wrap;
    word-break: break-word;
    font-size: 0.8rem;
    line-height: 1.4;
}

.tool-input {
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Tool Result Styling */
.tool-result {
    padding: 0.5rem 0.75rem;
    background: rgba(34, 197, 94, 0.1);
    border-left: 3px solid var(--success);
    border-radius: 0 4px 4px 0;
    margin: 0.5rem 0;
    font-family: var(--font-mono);
    font-size: 0.85rem;
}

.tool-result.error {
    background: rgba(239, 68, 68, 0.1);
    border-left-color: var(--error);
}

.tool-result-content {
    margin: 0;
    font-family: var(--font-mono);
    font-size: 0.8rem;
    color: var(--text-secondary);
    white-space: pre-wrap;
    word-wrap: break-word;
    max-height: 200px;
    overflow-y: auto;
}

.tool-result-image-tag {
    color: var(--text-secondary);
    font-style: italic;
    font-size: 0.85em;
}

.tool-result-image {
    /* Frame hugs the image but never exceeds the message block: a small
       image stays its natural size, a large one is capped at block width.
       `margin-inline: auto` centers the frame within the block. */
    width: fit-content;
    max-width: 100%;
    margin: 0.5rem auto;
    overflow: hidden;
    border-radius: 4px;
    border: 1px solid var(--border);
    cursor: pointer;
}

.tool-result-image img {
    max-width: 100%;
    max-height: 600px;
    object-fit: contain;
    display: block;
}

/* An SVG may declare no intrinsic pixel size (a bare `viewBox`, or percentage
   `width`/`height`) — common in hand-authored diagrams. It then has only an
   aspect ratio, so the shrink-to-fit frame above sizes to the image while the
   image's `max-width: 100%` sizes to the frame. Browsers resolve that cycle at
   zero and the diagram renders 0x0: invisible, with no `onerror` to trigger the
   "media expired" fallback.

   A `min-width: min(100%, 320px)` floor used to break that cycle, but the floor
   then became the *actual* width: on any viewport wider than 320px the `min()`
   resolves to 320px, so a 1380-wide diagram arrived as a 320px sliver. Give the
   frame a definite full-width basis instead and let the image fill it — the
   same reasoning the lightbox rule below already applies: an SVG is vector, so
   scaling one up stays crisp. SVGs that *do* carry a pixel size now scale to
   the column rather than stopping at their natural width, which is the wanted
   behavior for a figure in a transcript. Frame class is set by
   `needs_size_fallback` in
   frontend/src/components/message_renderer/renderers/media.rs. */
.tool-result-image.svg {
    width: 100%;
}

.tool-result-image.svg img {
    width: 100%;
    height: auto;

    /* `height: auto` keeps the aspect ratio, so the 600px cap above (which
       stops large raster screenshots dominating the transcript) would letterbox
       a tall diagram inside a full-width box instead of shrinking it. Cap
       against the viewport instead; the lightbox is one click away for
       anything taller. */
    max-height: 80vh;
}

/* Video shown via `agent-portal show`. Sizing mirrors `.tool-result-image`
   so images and videos read as one family in the transcript. */
.tool-result-video {
    width: fit-content;
    max-width: 100%;
    margin: 0.5rem auto;
    border-radius: 4px;
    border: 1px solid var(--border);
    overflow: hidden;
    background: #000;
}

.tool-result-video video {
    max-width: 100%;
    max-height: 600px;
    display: block;
}

/* Placeholder when a served media blob has expired/been evicted from its
   bounded store (the persisted transcript row can outlive the bytes). */
.media-expired {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    margin: 0.5rem 0;
    padding: 0.4rem 0.7rem;
    border: 1px dashed var(--border);
    border-radius: 4px;
    color: var(--text-secondary);
    font-size: 0.9em;
    font-style: italic;
}

.media-expired-icon {
    font-style: normal;
}

/* Image lightbox overlay */
.image-lightbox {
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
}

.image-lightbox-content {
    position: relative;
    max-width: 95vw;
    max-height: 95vh;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.image-lightbox-content img {
    max-width: 95vw;
    max-height: 85vh;
    object-fit: contain;
    border-radius: 4px;
}

/* Same 0x0 collapse as `.tool-result-image.svg` (see the note there): this img
   is the cross-axis item of a centered flex column, so it is shrink-to-fit too.
   Here we set `width` rather than `min-width` — the lightbox is the "zoom" view,
   and an SVG is vector, so scaling one up to fill the overlay stays crisp. */
.image-lightbox-content img.svg {
    width: min(95vw, 900px);
}

.image-lightbox-controls {
    display: flex;
    gap: 1rem;
    align-items: center;
    margin-top: 1rem;
}

.image-lightbox-download {
    padding: 0.5rem 1rem;
    background: var(--accent);
    color: var(--bg-dark);
    border-radius: 4px;
    text-decoration: none;
    font-weight: 600;
    font-size: 0.85rem;
}

.image-lightbox-download:hover {
    background: var(--accent-hover);
}

.image-lightbox-close {
    padding: 0.5rem 0.75rem;
    background: rgba(255, 255, 255, 0.15);
    color: var(--text-primary);
    border: none;
    border-radius: 4px;
    font-size: 1.25rem;
    cursor: pointer;
    line-height: 1;
}

.image-lightbox-close:hover {
    background: rgba(255, 255, 255, 0.25);
}

.tool-result-message {
    padding: 0;
}

.tool-result-message .message-body {
    padding: 0;
}

/* Read tool path styling — the diff-card / diff-view rules previously
   colocated here moved to `styles/tools/diff.css` with the rest of the
   shared `DiffCard` chrome (#823). */
.read-file-path {
    color: var(--text-primary);
    font-weight: 500;
    word-break: break-all;
}

/* Write Tool Styling */
.write-tool {
    background: rgba(99, 102, 241, 0.05);
}

.write-tool .tool-use-header {
    flex-wrap: wrap;
}

.write-file-path {
    color: var(--text-primary);
    font-weight: 500;
    word-break: break-all;
}

.write-size {
    color: var(--text-muted);
    font-size: 0.75rem;
}

/* No `margin-top`: the only parent is `.tool-use`, which is a flex column with
   `gap: 0.25rem`. Flex margins add to `gap` rather than collapsing into it, so a
   margin here rendered the gap below the tool header at 0.75rem — 3x intended. */
.write-preview {
    border-radius: 4px;
    overflow: hidden;
    background: rgba(0, 0, 0, 0.2);
}

.write-content {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    line-height: 1.5;
    max-height: 300px;
    overflow-y: auto;
    margin: 0;
    padding: 0.5rem;
}

.write-line {
    display: flex;
}

.write-line .line-number {
    flex-shrink: 0;
    width: 3rem;
    text-align: right;
    padding-right: 0.75rem;
    color: var(--text-muted);
    user-select: none;
}

.write-line .line-content {
    flex: 1;
    white-space: pre-wrap;
    word-break: break-all;
    color: var(--text-secondary);
}

.write-truncated {
    padding: 0.5rem;
    text-align: center;
    color: var(--text-muted);
    font-style: italic;
    border-top: 1px solid var(--border-color);
}


/* Math regions typeset by KaTeX. The element is rendered childless by Yew
   (see markdown/math_span.rs) — KaTeX owns everything inside it, which is
   what keeps its DOM mutations out of Yew's bundle. Display math gets a
   block wrapper so KaTeX's own `.katex-display` margins lay out predictably
   inside an inline context. */
.md-math-display {
    display: block;
}

/* Portable figures are poster-first. A live renderer replaces the poster only
   after the artifact and every executable runtime asset have been verified. */
.rizzma-figure {
    width: min(100%, 960px);
    overflow: hidden;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background: #16161e;
    margin-inline: auto;
}

.rizzma-viewport {
    position: relative;
    width: 100%;
    max-height: 72vh;
    overflow: hidden;
}

.rizzma-poster,
.rizzma-frame {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border: 0;
    object-fit: contain;
}

.rizzma-poster {
    z-index: 0;
}

.rizzma-frame {
    z-index: 1;
}

.rizzma-frame.hidden {
    visibility: hidden;
}

.rizzma-poster-missing {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    color: var(--text-muted);
}

.rizzma-mount {
    position: absolute;
    left: 50%;
    bottom: 0.75rem;
    transform: translateX(-50%);
    border: 1px solid rgba(122, 162, 247, 0.55);
    border-radius: 3px;
    padding: 0.4rem 0.7rem;
    background: rgba(26, 27, 38, 0.92);
    color: #c0caf5;
    cursor: pointer;
    z-index: 2;
}

.rizzma-mount:disabled {
    cursor: default;
    color: var(--text-muted);
}

.rizzma-error {
    position: absolute;
    inset: auto 0 0;
    padding: 0.4rem 0.6rem;
    background: rgba(247, 118, 142, 0.16);
    color: #f7768e;
    font-size: 0.78rem;
    z-index: 3;
}

.rizzma-controls {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    padding: 0.45rem 0.6rem;
    background: rgba(26, 27, 38, 0.94);
    color: #c0caf5;
    font-size: 0.78rem;
}

.rizzma-controls button {
    border: 1px solid rgba(122, 162, 247, 0.55);
    border-radius: 3px;
    padding: 0.25rem 0.55rem;
    background: transparent;
    color: inherit;
    cursor: pointer;
}

.rizzma-controls input[type="range"] {
    flex: 1;
    min-width: 5rem;
}

.rizzma-parameter-controls {
    display: grid;
    gap: 0.45rem;
    padding: 0.55rem 0.65rem;
    border-top: 1px solid var(--border-color);
    background: rgba(26, 27, 38, 0.94);
}

.rizzma-parameter {
    display: grid;
    grid-template-columns: minmax(6rem, auto) minmax(8rem, 1fr) 4.5rem;
    align-items: center;
    gap: 0.6rem;
    color: #c0caf5;
    font-size: 0.78rem;
}

.rizzma-parameter input[type="range"] {
    min-width: 0;
}

.rizzma-parameter > span {
    overflow-wrap: anywhere;
}

.rizzma-parameter output {
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
    text-align: right;
}
