def lines_to_svg(lines,
                 filename="output.svg",
                 stroke="white",
                 stroke_width=1,
                 margin=10,
                 flip_y=False,
                 stroke_linecap="round",
                 add_xml_declaration=True):
    """
    Write an SVG file from a list of lines.

    Parameters
    ----------
    lines : list of [[x1,y1],[x2,y2]]
        Example: [ [[10,20],[30,40]], [[50,60],[70,80]] ]
    filename : str
        Output SVG filename.
    stroke : str
        Stroke color (any valid SVG color).
    stroke_width : number
        Stroke width in pixels.
    margin : number
        Margin added on all sides (in same units as points).
    flip_y : bool
        If True, flips the Y axis so (0,0) is bottom-left (Cartesian).
    stroke_linecap : str
        Line cap style, e.g. "butt", "round", "square".
    add_xml_declaration : bool
        If True, include XML declaration at top.

    Returns
    -------
    svg_text : str
        The produced SVG content (also written to `filename`).
    """

    # flatten to collect all points
    pts = []
    for seg in lines:
        if not (isinstance(seg, (list,tuple)) and len(seg) >= 2):
            continue
        for p in seg[:2]:
            pts.append((float(p[0]), float(p[1])))

    if not pts:
        # empty svg fallback
        svg = '<?xml version="1.0" encoding="UTF-8"?>\n<svg xmlns="http://www.w3.org/2000/svg"></svg>\n' if add_xml_declaration else '<svg xmlns="http://www.w3.org/2000/svg"></svg>\n'
        with open(filename, "w", encoding="utf-8") as f:
            f.write(svg)
        return svg

    xs = [p[0] for p in pts]
    ys = [p[1] for p in pts]
    minx, maxx = min(xs), max(xs)
    miny, maxy = min(ys), max(ys)
    width = maxx - minx if maxx > minx else 1.0
    height = maxy - miny if maxy > miny else 1.0

    vb_x = minx - margin
    vb_y = miny - margin
    vb_w = width + 2 * margin
    vb_h = height + 2 * margin

    lines_svg = []
    for seg in lines:
        try:
            (x1, y1), (x2, y2) = seg[0], seg[1]
        except Exception:
            # skip malformed
            continue
        # Use <line> elements
        lines_svg.append(
            f'<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}" '
            f'stroke="{stroke}" stroke-width="{stroke_width}" stroke-linecap="{stroke_linecap}" />'
        )

    # Optional flip Y: wrap shapes in a group with transform.
    transform_group_open = ""
    transform_group_close = ""
    if flip_y:
        # We want to flip vertically inside the viewBox.
        # Translate by (0, vb_h) then scale(1,-1) will flip Y inside the viewBox.
        transform_group_open = f'<g transform="translate(0,{vb_h}) scale(1,-1)">'
        transform_group_close = '</g>'

    xml_decl = '<?xml version="1.0" encoding="UTF-8"?>\n' if add_xml_declaration else ""
    svg = (
        xml_decl +
        f'<svg xmlns="http://www.w3.org/2000/svg" '
        f'viewBox="{vb_x} {vb_y} {vb_w} {vb_h}" '
        f'width="{vb_w}" height="{vb_h}">\n'
        f'  {transform_group_open}\n'
        + "\n  ".join(lines_svg) + "\n"
        f'  {transform_group_close}\n'
        '</svg>\n'
    )

    with open(filename, "w", encoding="utf-8") as f:
        f.write(svg)

    return svg
