// 共通スタイル定義

#let book(title: "", author: "", body) = {
  set document(title: title, author: author)
  set page(
    paper: "iso-b5",
    margin: (top: 22mm, bottom: 20mm, inside: 24mm, outside: 18mm),
    numbering: "1",
  )
  set text(
    lang: "ja",
    font: ("New Computer Modern", "Hiragino Mincho ProN"),
    size: 11pt,
  )
  set par(justify: true, leading: 0.85em, first-line-indent: 0em)
  set heading(numbering: none)

  show heading.where(level: 1): it => {
    pagebreak(weak: true)
    set text(size: 20pt, weight: "bold")
    block(below: 1.4em)[#it.body]
  }
  show heading.where(level: 2): it => {
    set text(size: 15pt, weight: "bold")
    block(above: 1.4em, below: 0.9em)[
      #rect(stroke: 0.5pt, inset: (x: 8pt, y: 4pt))[#it.body]
    ]
  }
  show heading.where(level: 3): it => {
    set text(size: 12pt, weight: "bold")
    block(above: 1.1em, below: 0.5em)[#it.body]
  }
  show heading.where(level: 4): it => {
    set text(size: 10.5pt, weight: "bold")
    block(above: 0.8em, below: 0.3em)[#it.body]
  }

  body
}

// ─────────────────────────────
// 記法ヘルパー
// ─────────────────────────────

// 丸数字 ①②③… (中学受験では「マル1」「マル2」のように未知数を表す慣習)
#let maru(n) = {
  box(
    stroke: 0.5pt,
    inset: (x: 2pt, y: 1pt),
    radius: 1em,
    baseline: 0.1em,
  )[#text(size: 0.85em)[#n]]
}

// ルビ(振り仮名)
#let ruby(base, yomi) = {
  let ruby-text = text(size: 0.5em, yomi)
  box(
    inset: (top: 0.5em),
  )[
    #place(top + center, dy: -0.45em)[#ruby-text]
    #base
  ]
}

// 大問番号(豊島岡は四角囲み数字)
#let daimon(n) = {
  box(
    stroke: 0.6pt,
    inset: (x: 4pt, y: 2pt),
    outset: (y: 1pt),
  )[#text(weight: "bold")[#n]]
}

// 小問番号 (1), (2), ...
#let shomon(n) = text(weight: "regular")[(#n)]

// 空欄(数字を入れる四角)
#let blank(width: 2em) = box(
  stroke: 0.5pt,
  inset: (x: 4pt, y: 2pt),
  width: width,
  height: 1.3em,
)[]

// ─────────────────────────────
// 問題・解法ブロック
// ─────────────────────────────

// 大問見出し(問題セクション)
#let mondai-section(n, body) = {
  heading(level: 3)[#daimon(n) #h(0.5em) #body]
}

// 小問
#let shomon-q(n, body) = {
  block(above: 0.6em, below: 0.6em)[
    #grid(
      columns: (2.4em, 1fr),
      column-gutter: 0pt,
      align: (right + top, left + top),
      shomon(n), body,
    )
  ]
}

// パターン別チャプター用: サブカテゴリ見出し(旅人算―基本、流水算 など)
#let pattern-section(body) = {
  heading(level: 3)[#body]
}

// パターン別チャプター用: 出典付き問題見出し(年度・回・大問を明記)
#let mondai(source, body) = {
  block(above: 1.4em, below: 0.4em)[
    #block(
      inset: (x: 6pt, y: 3pt),
      fill: rgb("#f1f5f9"),
      radius: 2pt,
    )[#text(size: 9pt, weight: "bold", fill: rgb("#334155"))[#source]]
    #v(0.3em)
    #body
  ]
}

// 難易度表示 ★の数(1〜5)
#let difficulty(n) = {
  let filled = "★" * n
  let empty = "☆" * (5 - n)
  text(fill: rgb("#d97706"))[#filled] + text(fill: rgb("#d1d5db"))[#empty]
}

// 図参照ボックス(切り抜き画像 or CeTZ)
#let zukei(body, caption: none, width: 60%) = {
  align(center)[
    #block(width: width)[
      #body
      #if caption != none {
        v(0.3em)
        text(size: 9pt)[#caption]
      }
    ]
  ]
}

// ─────────────────────────────
// 解説ブロック
// ─────────────────────────────

#let kaiho-title = text(weight: "bold", fill: rgb("#1e40af"))[【解法】]
#let kotae-title = text(weight: "bold")[答]
#let point-title = text(weight: "bold", fill: rgb("#92400e"))[着眼点]

#let kaiho(body) = {
  block(above: 0.8em, below: 0.4em)[#kaiho-title #h(0.3em) #body]
}

#let kotae(body) = {
  block(above: 0.5em, below: 0.8em)[
    #kotae-title #h(0.4em) #box(
      stroke: 0.6pt,
      inset: (x: 8pt, y: 3pt),
    )[#text(weight: "bold")[#body]]
  ]
}

#let point(body) = {
  block(
    above: 0.6em, below: 0.6em,
    fill: rgb("#fef3c7"),
    inset: 8pt,
    radius: 3pt,
    width: 100%,
    stroke: (left: 2pt + rgb("#f59e0b")),
  )[#point-title #h(0.4em) #body]
}

// 解法レシピ用ブロック(パターン別の手順書)
#let recipe(title, body) = {
  block(
    above: 0.8em, below: 0.8em,
    fill: rgb("#ecfdf5"),
    inset: 9pt,
    radius: 3pt,
    width: 100%,
    stroke: (left: 2.5pt + rgb("#059669")),
  )[
    #text(weight: "bold", fill: rgb("#065f46"))[#title]
    #v(0.3em)
    #body
  ]
}

// 補足・別解
#let bekkai(body) = {
  block(above: 0.6em, below: 0.4em)[
    #text(weight: "bold", fill: rgb("#065f46"))[【別解】] #h(0.3em) #body
  ]
}

// 親向け数学的補足(中学以上の数式を許容)
#let oyamuke(body) = {
  block(
    above: 0.6em, below: 0.6em,
    fill: rgb("#eef2ff"),
    inset: 8pt,
    radius: 3pt,
    width: 100%,
    stroke: (left: 2pt + rgb("#4f46e5")),
  )[
    #text(weight: "bold", fill: rgb("#3730a3"))[親向け補足 (中学以上の数学)] \
    #body
  ]
}
