{# Cellular badge content — dynamic rim + pattern strip + slabs.

   Geometry derives from the resolver-supplied width (measure_text-driven),
   left_panel_width, sep_width, seam_width, indicator_size. Pattern grid is 3 columns × 4 rows;
   per-cell size scales from compact (4×4 at 4px pitch) to default (6×7 at
   6px pitch). Family resolution: ComposeSpec.variant or cellular paradigm's
   frame_variant_defaults.badge (= "blue").

   State-mode badges (is_state_badge=True) render a ring+bit indicator on
   the right; its fill is driven by the state-signal-cascade partial which
   binds data-hw-status to --hw-state-signal / --hw-state-value. #}
{# v0.3.0: cellular_palette is a structured tone dict supplied by
   compose/palette.py. primary holds the active tone (8 chromatic fields);
   secondary mirrors primary in solo mode so paired bifamily templates
   branch-free; bridge carries fusion-zone colors when paired. Badges are
   monofamily — only primary is consumed. #}
{% set pattern_cells_trio = cellular_palette.primary.cellular_cells %}
{% set label_slab_fill = cellular_palette.primary.label_slab %}
{% set label_text_fill = cellular_palette.primary.label_text %}
{% set value_text_fill = cellular_palette.primary.value_text %}
{% set top_highlight = cellular_palette.primary.rim_stops[0].color %}
{% set dark_lip = cellular_palette.primary.rim_stops[-1].color %}

<g clip-path="url(#{{ uid }}-clip)">
  {# 1. Metallic rim — full viewBox, drop-shadow-lifted #}
  <rect width="{{ width }}" height="{{ height }}" fill="url(#{{ uid }}-rim)" filter="url(#{{ uid }}-lift)"/>
  {# 2. Dark lip — 1px inset stroke #}
  <rect x="{{ cellular_inner_inset }}" y="{{ cellular_inner_inset }}" width="{{ cellular_inner_stroke_w }}" height="{{ cellular_inner_stroke_h }}" fill="none" stroke="{{ dark_lip }}" stroke-width="{{ cellular_inner_stroke_width }}"/>
  {# 3. Inner canvas #}
  <rect x="{{ cellular_canvas_inset }}" y="{{ cellular_canvas_inset }}" width="{{ cellular_canvas_w }}" height="{{ cellular_canvas_h }}" fill="{{ pattern_cells_trio[2] }}"/>
  {# 4. Top highlight hairline (bright edge of rim) #}
  <rect x="{{ cellular_content_start_x }}" y="{{ cellular_canvas_inset }}" width="{{ cellular_content_w }}" height="{{ cellular_top_highlight_h }}" fill="{{ top_highlight }}" opacity="0.22"/>
  {# 5. Bottom ambient occlusion #}
  <rect x="{{ cellular_content_start_x }}" y="{{ cellular_bottom_y }}" width="{{ cellular_content_w }}" height="{{ cellular_bottom_highlight_h }}" fill="{{ dark_lip }}" opacity="0.55"/>
  {# 6. Label slab — family-colored deep fill. Starts at the resolver-supplied
     pattern edge so the layout engine owns bookend geometry and compact
     spacing. #}
  <rect x="{{ cellular_label_slab_x }}" y="{{ cellular_canvas_inset }}" width="{{ cellular_label_slab_w }}" height="{{ cellular_canvas_h }}" fill="{{ label_slab_fill }}"/>
  {# 7. Refined seam — 1px gradient + 2px shadow #}
  <rect x="{{ left_panel_width }}" y="{{ cellular_canvas_inset }}" width="{{ cellular_seam_w }}" height="{{ cellular_canvas_h }}" fill="url(#{{ uid }}-seam)"/>
  <rect x="{{ cellular_seam_shadow_x }}" y="{{ cellular_canvas_inset }}" width="{{ seam_width }}" height="{{ cellular_canvas_h }}" fill="var(--dna-surface-deep)"/>
  {# 8. Value slab — gradient canvas #}
  <rect x="{{ cellular_value_slab_x }}" y="{{ cellular_canvas_inset }}" width="{{ cellular_value_slab_w }}" height="{{ cellular_canvas_h }}" fill="url(#{{ uid }}-vb)"/>
  {# 9. Pattern strip — 3 × 4 living substrate on left edge #}
  <g opacity="{{ cellular_pattern_opacity }}">
  {% for cell in cellular_pattern_cells %}
    <rect x="{{ cell.x }}" y="{{ cell.y }}" width="{{ cell.w }}" height="{{ cell.h }}" fill="{{ pattern_cells_trio[cell.color_index] }}" class="{{ cell.css_class }}"/>
  {% endfor %}
  </g>
</g>

{# 10. Glyph — rebuilt inline from glyph_path + glyph_viewbox with
      family-specific fill. The pre-baked glyph_svg has genome.glyph_inner
      hardcoded (blue-family tint) and fixed dimensions that don't respect
      the cellular paradigm's glyph_size override. Reconstructing here uses
      the paradigm-driven glyph_render_size and picks fill from the family. #}
{% set variant_glyph_fill = cellular_palette.primary.seam_mid %}
{% if has_glyph and glyph_path %}
<g data-hw-zone="glyph" transform="translate({{ glyph_x }},{{ glyph_y }})">
  <svg width="{{ glyph_render_size }}" height="{{ glyph_render_size }}" viewBox="{{ glyph_render_viewbox | default(glyph_viewbox) }}" overflow="visible">
    <path d="{{ glyph_path }}"{% if glyph_fill_rule %} fill-rule="{{ glyph_fill_rule }}"{% endif %} fill="{{ variant_glyph_fill }}"/>
  </svg>
</g>
{% endif %}

{# 11. Label text — Orbitron 700 per paradigm config #}
<text data-hw-zone="label"
      x="{{ label_x }}" y="{{ text_y }}"
      text-anchor="middle"
      font-family="{{ label_font_family }}"
      font-size="{{ badge_label_font_size }}" font-weight="700"
      fill="{{ label_text_fill }}"
      letter-spacing="0.1em"{% if label_text_length and label_text_length > 0 %} textLength="{{ label_text_length }}" lengthAdjust="spacingAndGlyphs"{% endif %}>{{ label_display | default(label | upper) }}</text>

{# 12. Value text — Chakra Petch 700 per paradigm config.
      State-mode: fill routed through --hw-state-value (CSS cascade). #}
<text data-hw-zone="value"
      x="{{ value_x }}" y="{{ text_y }}"
      text-anchor="middle"
      font-family="{{ value_font_family }}"
      font-size="{{ badge_value_font_size }}" font-weight="700"
      {% if is_state_badge %}class="hw-value-text" fill="var(--hw-state-value, {{ value_text_fill }})"{% else %}fill="{{ value_text_fill }}"{% endif %}{% if value_text_length and value_text_length > 0 %} textLength="{{ value_text_length }}" lengthAdjust="spacingAndGlyphs"{% endif %}>{{ value }}</text>

{# 13. State indicator (ring + bit) — gated on resolver-supplied show_indicator
      (v0.2.25 mode-driven). indicator_y is text-baseline-pinned by
      compose/layout.py. inner_bit_w / inner_bit_offset come from the
      same engine — no template-side arithmetic (Invariant 6). #}
{% if show_indicator %}
{% set indicator_ring_class = "hw-ring" %}
{% set indicator_ring_stroke = "var(--hw-state-signal, " ~ label_text_fill ~ ")" %}
{% set indicator_stroke_width_value = "1.2" %}
{% set indicator_bit_class = "hw-bit" %}
{% set indicator_bit_fill = "var(--hw-state-signal, " ~ label_text_fill ~ ")" %}
{% include "frames/badge/indicators/" ~ indicator_shape ~ "-indicator.j2" %}
{% endif %}
