"""Dependency-free inline-SVG charts. Server-rendered so the cockpit needs no JS charting library — the SVG
string is embedded directly in the HTML. nav_sparkline maps a NAV series to a polyline scaled to the box."""
from __future__ import annotations


def nav_sparkline(nav: list[float], *, width: int = 120, height: int = 28,
                  color: str = "#3fb950") -> str:
    box = f'<svg width="{width}" height="{height}" viewBox="0 0 {width} {height}" '\
          'xmlns="http://www.w3.org/2000/svg">'
    if not nav:
        return box + "</svg>"
    lo, hi = min(nav), max(nav)
    span = (hi - lo) or 1.0
    n = len(nav)
    step = width / (n - 1) if n > 1 else 0.0
    pts = []
    for i, v in enumerate(nav):
        x = i * step
        y = height - (v - lo) / span * (height - 2) - 1     # 1px padding, y inverted
        pts.append(f"{x:.1f},{y:.1f}")
    stroke = color if nav[-1] >= nav[0] else "#f85149"
    return (box + f'<polyline fill="none" stroke="{stroke}" stroke-width="1.5" '
            f'points="{" ".join(pts)}" /></svg>')
