///|
/// Render a native SVG macroblock & motion-vector anatomy map for a decoded frame.
pub fn render_frame_mb_svg(meta : FrameMeta) -> String {
  let cell = 24
  let pad_x = 24
  let pad_y = 56
  let legend_h = 44
  let grid_w = meta.mb_w * cell
  let grid_h = meta.mb_h * cell
  let svg_w = if grid_w + pad_x * 2 > 540 { grid_w + pad_x * 2 } else { 540 }
  let svg_h = pad_y + grid_h + legend_h + 16
  let sb = StringBuilder()
  sb.write_string(
    "",
  )
  sb.write_string(
    "",
  )
  let idr_tag = if meta.is_idr { " (IDR)" } else { "" }
  sb.write_string(
    "Frame #\{meta.frame_index} [\{meta.slice_type}\{idr_tag}] \{meta.width}x\{meta.height} | QP=\{meta.slice_qp} | \{meta.nal_bytes} B",
  )
  sb.write_string(
    "Macroblock grid: \{meta.mb_w}x\{meta.mb_h} (\{meta.mbs.length()} MBs) — color=MB type, white arrows=L0 MV",
  )
  // Draw MB cells
  for mb in meta.mbs {
    let x = pad_x + mb.mb_x * cell
    let y = pad_y + mb.mb_y * cell
    let fill = match mb.kind {
      MbKind::I4x4 => "#ef4444"
      MbKind::I16x16 => "#f97316"
      MbKind::IPcm => "#ec4899"
      MbKind::PSkip => "#334155"
      MbKind::P16x16 => "#2563eb"
      MbKind::P16x8 => "#0ea5e9"
      MbKind::P8x16 => "#06b6d4"
      MbKind::P8x8 => "#10b981"
    }
    sb.write_string(
      "",
    )
    // Draw MV arrow if non-zero
    if mb.mv_x != 0 || mb.mv_y != 0 {
      let cx = x + cell / 2
      let cy = y + cell / 2
      let dx = clamp_int(mb.mv_x / 2, -cell / 2 + 2, cell / 2 - 2)
      let dy = clamp_int(mb.mv_y / 2, -cell / 2 + 2, cell / 2 - 2)
      let ex = cx + dx
      let ey = cy + dy
      sb.write_string(
        "",
      )
      sb.write_string(
        "",
      )
    }
  }
  // Legend
  let ly = pad_y + grid_h + 24
  let items : Array[(String, String)] = [
    ("#ef4444", "I_4x4"),
    ("#f97316", "I_16x16"),
    ("#2563eb", "P_16x16"),
    ("#0ea5e9", "P_16x8/8x16"),
    ("#10b981", "P_8x8"),
    ("#334155", "P_Skip"),
  ]
  let mut lx = pad_x
  for item in items {
    sb.write_string(
      "",
    )
    sb.write_string(
      "\{item.1}",
    )
    lx += 82
  }
  sb.write_string("")
  sb.to_string()
}

///|
/// Render a native SVG per-frame bitrate & I/P bar chart across all decoded frames.
pub fn render_bitrate_svg(frames : Array[DecodedFrame]) -> String {
  let n = frames.length()
  let svg_w = 640
  let svg_h = 220
  let pad_l = 48
  let pad_r = 20
  let pad_t = 38
  let pad_b = 34
  let plot_w = svg_w - pad_l - pad_r
  let plot_h = svg_h - pad_t - pad_b
  let mut max_bytes = 1
  for f in frames {
    if f.meta.nal_bytes > max_bytes {
      max_bytes = f.meta.nal_bytes
    }
  }
  let sb = StringBuilder()
  sb.write_string(
    "",
  )
  sb.write_string(
    "",
  )
  sb.write_string(
    "Frame NAL Size Distribution (\{n} frames, peak=\{max_bytes} B)",
  )
  if n > 0 {
    let slot = plot_w / n
    let bw = if slot > 4 { slot - 2 } else { 2 }
    for i = 0; i < n; i = i + 1 {
      let f = frames[i]
      let h = f.meta.nal_bytes * plot_h / max_bytes
      let bh = if h > 2 { h } else { 2 }
      let x = pad_l + i * slot
      let y = pad_t + plot_h - bh
      let col = if f.meta.is_idr {
        "#ef4444"
      } else if f.meta.slice_type == "I" {
        "#f97316"
      } else {
        "#38bdf8"
      }
      sb.write_string(
        "",
      )
    }
  }
  sb.write_string(
    "",
  )
  sb.write_string(
    "Red=IDR, Orange=I, Blue=P",
  )
  sb.write_string("")
  sb.to_string()
}