// Live OpenSpec canonical intent views for Plan, Build, and Review perspectives.

///|
fn live_stage_of(stage : String) -> StageId {
  match stage {
    "explore" => Explore
    "propose" => Propose
    "implement" => Implement
    "verify" => VerifyGate
    "release" | "archived" => Archived
    _ => Implement
  }
}

///|
fn live_status_label(status : @app.WireIntentStatus) -> (String, String) {
  match status {
    Active => ("Active", "var(--mc-cobalt)")
    ReadyToVerify => ("Ready to Verify", "var(--mc-good-fg)")
    Archived => ("Archived", "var(--rui-muted-foreground)")
    Blocked(reason) => ("Blocked: \{reason}", "var(--mc-wait-fg)")
  }
}

///|
fn live_grade_class(grade : String) -> String {
  match grade {
    "A" | "B" => "mc-grade mc-grade-good"
    "C" => "mc-grade mc-grade-warn"
    _ => "mc-grade mc-grade-bad"
  }
}

///|
fn live_eval_for(
  sdlc : @app.WorkspaceSdlc?,
  item_id : String,
) -> @app.WireItemEvaluation? {
  sdlc.bind(s => s.evaluations.iter().find_first(e => e.item_id == item_id))
}

///|
fn live_plan_card(
  item : @app.WireIntentItem,
  eval : @app.WireItemEvaluation?,
  selected : Bool,
  emit : @cmd.Emit[Msg],
) -> @html.Html {
  let (status_text, status_color) = match eval {
    Some(ev) =>
      if ev.gates_passed {
        ("Rego Gates Passed", "var(--mc-good-fg)")
      } else {
        let open = ev.gates.iter().filter(g => !g.passed).count()
        ("\{open} Rego gate(s) open", "var(--mc-wait-fg)")
      }
    None => live_status_label(item.status)
  }
  let score_badge = match eval {
    Some(ev) =>
      @html.span(class=live_grade_class(ev.grade), "\{ev.grade} · \{ev.score}")
    None => @html.span(class="mc-pill-good", "Live")
  }
  let segments = item.packet.documents
    .iter()
    .map(doc => {
      @html.span(
        class="mc-seg-ok",
        title="\{doc.role.label()}: \{doc.path}",
        @html.nothing,
      )
    })
    .to_array()
  @html.button(
    class=if selected { "mc-zen-card selected" } else { "mc-zen-card" },
    attrs=@html.Attrs::build().aria_pressed(selected.to_string()),
    on_click=emit(LiveIntentSelected(item.id)),
    [
      @html.div(
        style=[
          "display:flex;justify-content:space-between;align-items:center;gap:0.375rem;width:100%",
        ],
        [
          @html.span(
            style=[
              "font-size:0.625rem;font-weight:700;letter-spacing:0.05em;color:var(--mc-cobalt)",
            ],
            "• CHANGE",
          ),
          score_badge,
        ],
      ),
      @html.div(
        style=[
          "font-size:0.8125rem;font-weight:650;line-height:1.3;text-align:left",
        ],
        item.title,
      ),
      @html.div(
        style=[
          "font-size:0.6875rem;font-family:var(--mc-mono);color:var(--rui-muted-foreground);text-align:left",
        ],
        item.slug,
      ),
      if !segments.is_empty() {
        @html.div(class="mc-seg-bar", segments)
      } else {
        @html.nothing
      },
      @html.div(
        style=[
          "display:flex;justify-content:space-between;font-size:0.6875rem;color:var(--rui-muted-foreground);width:100%",
        ],
        [
          @html.span("\{item.packet.documents.length()} artifacts"),
          if item.task_progress.total > 0 {
            @html.span(
              "\{item.task_progress.completed}/\{item.task_progress.total} tasks",
            )
          } else {
            @html.nothing
          },
        ],
      ),
      @html.div(
        style=[
          "display:flex;justify-content:space-between;align-items:center;padding-top:0.375rem;border-top:1px solid var(--rui-border);font-size:0.6875rem;width:100%",
        ],
        [
          @html.span(
            style=["font-weight:600;color:\{status_color}"],
            status_text,
          ),
          @html.span(
            style=["color:var(--rui-muted-foreground);font-size:0.625rem"],
            item.adapter.to_string(),
          ),
        ],
      ),
    ],
  )
}

///|
/// Live Plan perspective board displaying real OpenSpec changes from the server.
fn live_plan_perspective(
  project : @app.Project,
  ws : MockupWorkspace,
  intent : @app.WorkspaceIntent,
  model : Model,
  view : PlanView,
  emit : @cmd.Emit[Msg],
) -> @html.Html {
  let selected_item = model.selected_live_intent_item(intent)
  let selected_id = selected_item.map(i => i.id).unwrap_or("")
  let live_sdlc = model.active_live_sdlc()
  let content = match view {
    Board => {
      let cols = ws
        .stages()
        .iter()
        .map(stage => {
          let cards = intent.items
            .iter()
            .filter(item => live_stage_of(item.stage) == stage.id)
            .collect()
          let out_hint = match stage.outputs.get(0) {
            Some(first) => "Out: \{first}"
            None => ""
          }
          @html.div(
            class="mc-zen-col",
            style=["--col-accent:\{stage_accent(stage.id)}"],
            [
              @html.div(
                style=[
                  "display:flex;justify-content:space-between;align-items:center;gap:0.375rem",
                ],
                [
                  @html.span(style=["font-size:0.8125rem;font-weight:650"], [
                    @html.text(stage.id.label()),
                    @html.span(
                      style=[
                        "margin-inline-start:0.375rem;color:var(--rui-muted-foreground);font-weight:500",
                      ],
                      "\{cards.length()}",
                    ),
                  ]),
                  if stage.id.is_gate() {
                    @html.span(class="mc-pill-warn", "Gate")
                  } else {
                    @html.nothing
                  },
                ],
              ),
              @html.div(
                style=["font-size:0.6875rem;color:var(--rui-muted-foreground)"],
                out_hint,
              ),
              @html.div(
                style=["display:flex;flex-direction:column;gap:0.5rem"],
                cards
                .iter()
                .map(item => {
                  live_plan_card(
                    item,
                    live_eval_for(live_sdlc, item.id),
                    item.id == selected_id,
                    emit,
                  )
                })
                .to_array(),
              ),
            ],
          )
        })
        .to_array()
      let summary_bar = @html.div(
        style=[
          "display:flex;justify-content:space-between;align-items:center;gap:0.5rem;flex-wrap:wrap;font-size:0.75rem;color:var(--rui-muted-foreground)",
        ],
        [
          @html.span(
            "Live OpenSpec adapter · \{intent.items.length()} active change(s) · \{intent.specs.length()} main spec(s)",
          ),
          @rui.button(
            variant=Outline,
            size=Sm,
            on_click=emit(ActionChosen(SetPerspective(Review))),
            "Open Reading Packet ->",
          ),
        ],
      )
      @html.div(style=["display:flex;flex-direction:column;gap:0.75rem"], [
        summary_bar,
        @html.div(class="mc-zen-board", cols),
      ])
    }
    StateMachine =>
      workflow_diagram_card(
        project.name,
        ws,
        is_bpmn=false,
        mode=PlanMode,
        emit,
      )
    Bpmn =>
      workflow_diagram_card(project.name, ws, is_bpmn=true, mode=PlanMode, emit)
  }
  @html.div(
    style=[
      "flex:1;min-height:0;overflow:auto;padding:1rem 1.25rem 1.5rem;display:flex;flex-direction:column;gap:1rem",
    ],
    content,
  )
}

///|
/// Live Build perspective pipeline displaying real OpenSpec changes from the server.
fn live_build_perspective(
  ws : MockupWorkspace,
  intent : @app.WorkspaceIntent,
  model : Model,
  emit : @cmd.Emit[Msg],
) -> @html.Html {
  let selected_id = model
    .selected_live_intent_item(intent)
    .map(i => i.id)
    .unwrap_or("")
  let groups = ws
    .stages()
    .iter()
    .map(stage => {
      let stage_items = intent.items
        .iter()
        .filter(i => live_stage_of(i.stage) == stage.id)
        .collect()
      let outs = stage.outputs.iter().to_array().join(", ")
      @html.div(style=["display:flex;flex-direction:column;gap:0.375rem"], [
        @html.div(
          style=[
            "display:flex;justify-content:space-between;align-items:center;gap:0.5rem;padding:0.125rem 0.25rem;font-size:0.75rem",
          ],
          [
            @html.div(style=["display:flex;align-items:center;gap:0.5rem"], [
              @html.span(
                style=[
                  "width:0.5rem;height:0.5rem;border-radius:50%;background:\{stage_accent(stage.id)};display:inline-block",
                ],
                @html.nothing,
              ),
              @html.strong(stage.id.label()),
              @html.span(
                style=["color:var(--rui-muted-foreground)"],
                "\{stage_items.length()}",
              ),
            ]),
            @html.span(
              style=["font-size:0.6875rem;color:var(--rui-muted-foreground)"],
              "out: \{outs}",
            ),
          ],
        ),
        @html.div(
          style=["display:flex;flex-direction:column;gap:0.375rem"],
          stage_items
          .iter()
          .map(item => {
            let (status_text, status_color) = live_status_label(item.status)
            let selected = item.id == selected_id
            @html.button(
              class=if selected {
                "mc-pipe-row selected"
              } else {
                "mc-pipe-row"
              },
              on_click=emit(LiveIntentSelected(item.id)),
              [
                @html.span(
                  style=[
                    "width:0.45rem;height:0.45rem;border-radius:50%;background:\{status_color}",
                  ],
                  @html.nothing,
                ),
                @html.span(
                  style=[
                    "font-family:var(--mc-mono);font-size:0.625rem;color:var(--rui-muted-foreground)",
                  ],
                  "CHG",
                ),
                @html.span(
                  class="mc-cut mc-pipe-title",
                  style=["font-weight:600;font-size:0.8125rem;text-align:left"],
                  item.title,
                ),
                @html.span(
                  class="mc-cut mc-pipe-intent",
                  style=[
                    "font-size:0.75rem;font-family:var(--mc-mono);color:var(--rui-muted-foreground);text-align:left",
                  ],
                  item.origin_path,
                ),
                @html.span(
                  class="mc-pipe-meta",
                  style=[
                    "font-family:var(--mc-mono);font-size:0.6875rem;color:var(--rui-muted-foreground)",
                  ],
                  if item.task_progress.total > 0 {
                    "\{item.task_progress.completed}/\{item.task_progress.total} tasks"
                  } else {
                    "\{item.packet.documents.length()} docs"
                  },
                ),
                @html.span(class="mc-pill-good", "Live"),
                @html.span(
                  class="mc-pipe-status",
                  style=[
                    "font-size:0.75rem;font-weight:600;text-align:right;color:\{status_color}",
                  ],
                  status_text,
                ),
              ],
            )
          })
          .to_array(),
        ),
      ])
    })
    .to_array()
  @html.div(
    style=[
      "flex:1;min-height:0;overflow:auto;padding:1rem 1.5rem 2rem;display:flex;flex-direction:column;gap:1rem",
    ],
    [
      @html.div(
        style=[
          "display:flex;justify-content:space-between;align-items:center;font-size:0.75rem;color:var(--rui-muted-foreground);flex-wrap:wrap;gap:0.5rem",
        ],
        [
          @html.span(
            "Live OpenSpec delivery pipeline · \{intent.items.length()} active change(s) · \{intent.specs.length()} main spec(s)",
          ),
          @rui.button(
            variant=Outline,
            size=Sm,
            on_click=emit(ActionChosen(SetPerspective(Review))),
            "Open Reading Packet ->",
          ),
        ],
      ),
      @html.fragment(groups),
    ],
  )
}

///|
/// Live Review perspective displaying the selected live OpenSpec reading packet.
fn live_review_perspective(
  model : Model,
  intent : @app.WorkspaceIntent,
  read_mode : ReadMode,
  emit : @cmd.Emit[Msg],
) -> @html.Html {
  guard model.selected_live_intent_item(intent) is Some(active_item) else {
    return @html.nothing
  }
  let docs = active_item.packet.documents
  let doc_idx = if model.selected_live_doc >= 0 &&
    model.selected_live_doc < docs.length() {
    model.selected_live_doc
  } else {
    0
  }
  guard docs.get(doc_idx) is Some(doc) else { return @html.nothing }

  let item_switcher = if intent.items.length() > 1 {
    @html.div(
      style=[
        "display:flex;flex-direction:column;gap:0.25rem;padding-bottom:0.5rem;border-bottom:1px solid var(--rui-border)",
      ],
      [
        @html.span(class="mc-kicker", "Active Changes"),
        @html.fragment(
          intent.items
          .iter()
          .map(item => {
            let is_cur = item.id == active_item.id
            @html.button(
              class=if is_cur { "mc-out-item active" } else { "mc-out-item" },
              on_click=emit(LiveIntentSelected(item.id)),
              [
                @html.span(
                  class="mc-cut",
                  style=["font-size:0.75rem;font-weight:600;text-align:left"],
                  item.title,
                ),
              ],
            )
          })
          .to_array(),
        ),
      ],
    )
  } else {
    @html.nothing
  }

  let outline_rows : Array[@html.Html] = []
  for idx = 0; idx < docs.length(); idx = idx + 1 {
    let d = docs[idx]
    let active = idx == doc_idx
    outline_rows.push(
      @html.button(
        class=if active { "mc-out-item active" } else { "mc-out-item" },
        attrs=if active {
          @html.Attrs::build().aria_current("true")
        } else {
          @html.Attrs::build()
        },
        on_click=emit(LiveDocSelected(idx)),
        [
          @html.span(class="mc-out-num", "\{d.step}"),
          @html.span(
            style=[
              "display:flex;flex-direction:column;gap:0.125rem;min-width:0;flex:1;text-align:left",
            ],
            [
              @html.span(
                class="mc-cut",
                style=["font-size:0.75rem;font-weight:600"],
                d.title,
              ),
              @html.span(
                style=["font-size:0.625rem;color:var(--rui-muted-foreground)"],
                d.role.label(),
              ),
            ],
          ),
        ],
      ),
    )
  }

  let doc_body : @html.Html = match read_mode {
    Source =>
      code_viewer(
        doc.source_lines.iter().join("\n"),
        lang=Markdown,
        label="Line-numbered document source",
        copy_id="mc-copy-live-review-source",
        fill=true,
      )
    Preview =>
      @html.div(style=["display:flex;flex-direction:column;gap:0.875rem"], [
        if !doc.summary.is_empty() {
          @html.p(
            style=[
              "margin:0;font-size:0.875rem;font-style:italic;color:var(--rui-muted-foreground)",
            ],
            doc.summary,
          )
        } else {
          @html.nothing
        },
        @html.div(
          style=[
            "display:flex;flex-direction:column;gap:0.375rem;font-size:0.9rem;line-height:1.7",
          ],
          doc.source_lines
          .iter()
          .filter(line => !line.trim().is_empty())
          .map(line => @html.p(style=["margin:0"], line))
          .to_array(),
        ),
      ])
    Structured =>
      if !doc.requirements.is_empty() {
        @html.div(style=["display:flex;flex-direction:column;gap:0.875rem"], [
          @html.fragment(
            doc.requirements
            .iter()
            .map(req => {
              let (badge, cls) = match req.kind {
                Added => ("ADDED", "mc-delta-block added")
                Modified => ("MODIFIED", "mc-delta-block")
                Removed => ("REMOVED", "mc-delta-block warn")
                Renamed => ("RENAMED", "mc-delta-block")
              }
              @html.div(class=cls, [
                @html.div(
                  style=[
                    "display:flex;justify-content:space-between;align-items:center;gap:0.5rem;margin-bottom:0.375rem",
                  ],
                  [
                    @html.strong(style=["font-size:0.8125rem"], req.title),
                    @html.span(class="mc-pill-good", badge),
                  ],
                ),
                @html.p(
                  style=[
                    "margin:0;font-size:0.875rem;line-height:1.65;color:var(--rui-foreground)",
                  ],
                  req.text,
                ),
                if !req.scenarios.is_empty() {
                  @html.div(
                    style=[
                      "margin-top:0.5rem;display:flex;flex-direction:column;gap:0.375rem",
                    ],
                    req.scenarios
                    .iter()
                    .map(sc => {
                      @html.div(
                        style=[
                          "padding:0.4rem 0.6rem;border-radius:0.375rem;background:var(--rui-muted);font-size:0.75rem",
                        ],
                        [
                          @html.strong(sc.title),
                          @html.div("WHEN \{sc.when_text}"),
                          @html.div("THEN \{sc.then_text}"),
                        ],
                      )
                    })
                    .to_array(),
                  )
                } else {
                  @html.nothing
                },
              ])
            })
            .to_array(),
          ),
        ])
      } else if !doc.tasks.is_empty() {
        @html.div(style=["display:flex;flex-direction:column;gap:0.5rem"], [
          @html.p(
            style=[
              "margin:0;font-size:0.8125rem;color:var(--rui-muted-foreground)",
            ],
            "\{active_item.task_progress.completed}/\{active_item.task_progress.total} tasks completed (\{active_item.task_progress.percent()}%)",
          ),
          @html.fragment(
            doc.tasks
            .iter()
            .map(task => {
              @html.div(
                style=[
                  "display:flex;align-items:center;gap:0.5rem;padding:0.5rem 0.75rem;border:1px solid var(--rui-border);border-radius:0.5rem;font-size:0.8125rem",
                ],
                [
                  @html.span(
                    class=if task.completed {
                      "mc-pill-good"
                    } else {
                      "mc-pill-warn"
                    },
                    if task.completed {
                      "Done"
                    } else {
                      "Open"
                    },
                  ),
                  if !task.code.is_empty() {
                    @html.span(
                      style=[
                        "font-family:var(--mc-mono);font-size:0.75rem;color:var(--rui-muted-foreground)",
                      ],
                      task.code,
                    )
                  } else {
                    @html.nothing
                  },
                  @html.span(task.label),
                ],
              )
            })
            .to_array(),
          ),
        ])
      } else {
        @html.div(style=["display:flex;flex-direction:column;gap:0.75rem"], [
          if !doc.summary.is_empty() {
            @html.p(
              style=[
                "margin:0;font-size:0.9rem;line-height:1.7;color:var(--rui-foreground)",
              ],
              doc.summary,
            )
          } else {
            @html.nothing
          },
          code_viewer(
            doc.source_lines.iter().join("\n"),
            lang=Markdown,
            label="Document source",
            copy_id="mc-copy-live-structured-source",
            fill=false,
          ),
        ])
      }
  }

  let secondary_shown = model.secondary_sidebar is SecondaryShown
  let companion_shown = model.companion_panel is CompanionShown
  let grid_class = match (secondary_shown, companion_shown) {
    (true, true) => "mc-review-grid"
    (false, true) => "mc-review-grid secondary-collapsed"
    (true, false) => "mc-review-grid companion-collapsed"
    (false, false) => "mc-review-grid secondary-collapsed companion-collapsed"
  }
  let (status_text, status_color) = live_status_label(active_item.status)
  let outline_panel = if secondary_shown {
    @html.aside(
      class="mc-review-outline",
      attrs=@html.Attrs::build().aria_label("Reading Packet"),
      [
        @html.div(
          style=[
            "display:flex;justify-content:space-between;align-items:center;gap:0.375rem",
          ],
          [
            @html.span(class="mc-kicker", "Reading Packet"),
            @rui.button(
              variant=Ghost,
              size=IconSm,
              title="Collapse Reading Packet sidebar",
              attrs=@html.Attrs::build().aria_label(
                "Collapse Reading Packet sidebar",
              ),
              on_click=emit(SecondarySidebarToggled),
              icon("M15 18l-6-6 6-6"),
            ),
          ],
        ),
        item_switcher,
        @html.div(
          style=["font-size:0.75rem;color:var(--rui-muted-foreground)"],
          "\{active_item.title} · \{docs.length()} docs",
        ),
        @html.div(
          style=["display:flex;flex-direction:column;gap:0.25rem"],
          outline_rows,
        ),
      ],
    )
  } else {
    @html.nothing
  }
  let splitter = if secondary_shown {
    secondary_splitter_view(model.secondary_sidebar_width, emit)
  } else {
    @html.nothing
  }
  let live_sdlc = model.active_live_sdlc()
  let item_eval = live_eval_for(live_sdlc, active_item.id)
  let rego_gate_card = match item_eval {
    Some(ev) =>
      @html.div(
        class="mc-surface-card",
        style=[
          "padding:0.75rem;display:flex;flex-direction:column;gap:0.4rem;font-size:0.75rem",
        ],
        [
          @html.div(
            style=[
              "display:flex;justify-content:space-between;align-items:center",
            ],
            [
              @html.span(class="mc-kicker", "Scorecard & Rego Gates"),
              @html.span(
                class=live_grade_class(ev.grade),
                "\{ev.grade} · \{ev.score}",
              ),
            ],
          ),
          @html.fragment(
            ev.gates
            .iter()
            .map(g => {
              @html.div(
                style=[
                  "display:flex;justify-content:space-between;align-items:center;gap:0.35rem",
                ],
                [
                  @html.span(g.name),
                  @html.span(
                    class=if g.passed { "mc-pill-good" } else { "mc-pill-warn" },
                    if g.passed {
                      "Pass"
                    } else {
                      "Open"
                    },
                  ),
                ],
              )
            })
            .to_array(),
          ),
        ],
      )
    None => @html.nothing
  }
  let companion = if companion_shown {
    @html.aside(
      class="mc-companion",
      attrs=@html.Attrs::build().aria_label("Reading Companion"),
      [
        @html.div(
          style=[
            "display:flex;justify-content:space-between;align-items:center;gap:0.5rem",
          ],
          [
            @html.span(class="mc-kicker", "Live OpenSpec Metadata"),
            @rui.button(
              variant=Ghost,
              size=IconSm,
              title="Collapse Reading Companion",
              attrs=@html.Attrs::build().aria_label(
                "Collapse Reading Companion",
              ),
              on_click=emit(CompanionPanelToggled),
              icon("M9 18l6-6-6-6"),
            ),
          ],
        ),
        @html.div(
          class="mc-surface-card",
          style=[
            "padding:0.75rem;display:flex;flex-direction:column;gap:0.45rem;font-size:0.75rem",
          ],
          [
            @html.div([
              @html.strong("Stage: "),
              @html.span(live_stage_of(active_item.stage).label()),
            ]),
            @html.div([
              @html.strong("Status: "),
              @html.span(style=["color:\{status_color}"], status_text),
            ]),
            @html.div([
              @html.strong("Origin: "),
              @html.span(
                style=["font-family:var(--mc-mono)"],
                active_item.origin_path,
              ),
            ]),
            @html.div([
              @html.strong("Tasks: "),
              @html.span(
                "\{active_item.task_progress.completed}/\{active_item.task_progress.total}",
              ),
            ]),
            @html.div([
              @html.strong("Main specs: "),
              @html.span("\{intent.specs.length()}"),
            ]),
          ],
        ),
        rego_gate_card,
        if !intent.diagnostics.is_empty() {
          @html.div(
            class="mc-surface-card",
            style=[
              "padding:0.75rem;display:flex;flex-direction:column;gap:0.35rem;font-size:0.75rem",
            ],
            [
              @html.span(class="mc-kicker", "Diagnostics"),
              @html.fragment(
                intent.diagnostics
                .iter()
                .map(diag => {
                  @html.div(
                    style=["color:var(--mc-wait-fg)"],
                    "• \{diag.message}",
                  )
                })
                .to_array(),
              ),
            ],
          )
        } else {
          @html.nothing
        },
      ],
    )
  } else {
    @html.nothing
  }
  @html.div(
    class=grid_class,
    style=["--mc-secondary-w:\{model.secondary_sidebar_width.pixels()}px"],
    [
      outline_panel,
      splitter,
      @html.div(
        class=if read_mode is Source {
          "mc-review-canvas code-mode"
        } else {
          "mc-review-canvas"
        },
        [
          @html.div(
            style=[
              "display:flex;justify-content:space-between;align-items:center;gap:0.5rem;flex-wrap:wrap",
            ],
            [
              @html.div(
                style=["display:flex;align-items:center;gap:0.375rem"],
                [
                  if !secondary_shown {
                    @rui.button(
                      variant=Ghost,
                      size=Sm,
                      title="Expand Reading Packet sidebar",
                      attrs=@html.Attrs::build().aria_label(
                        "Expand Reading Packet sidebar",
                      ),
                      on_click=emit(SecondarySidebarToggled),
                      [icon("M9 18l6-6-6-6"), @html.span("Packet")],
                    )
                  } else {
                    @html.nothing
                  },
                  @html.span(class="mc-kicker", doc.role.label()),
                  @html.span(class="mc-pill-good", "Live OpenSpec"),
                ],
              ),
              @html.div(
                style=["display:flex;align-items:center;gap:0.375rem"],
                [
                  @html.span(
                    style=[
                      "font-family:var(--mc-mono);font-size:0.7rem;color:var(--rui-muted-foreground)",
                    ],
                    doc.path,
                  ),
                  if !companion_shown {
                    @rui.button(
                      variant=Ghost,
                      size=Sm,
                      title="Expand Reading Companion",
                      attrs=@html.Attrs::build().aria_label(
                        "Expand Reading Companion",
                      ),
                      on_click=emit(CompanionPanelToggled),
                      [icon("M15 18l-6-6 6-6"), @html.span("Companion")],
                    )
                  } else {
                    @html.nothing
                  },
                ],
              ),
            ],
          ),
          @html.h2(
            style=[
              "margin:0;font-size:1.35rem;font-weight:700;letter-spacing:-0.02em",
            ],
            doc.title,
          ),
          doc_body,
        ],
      ),
      companion,
    ],
  )
}