// 第3回 解答解説 用スタイル(中学受験生向け)
// 使い方: 各年度の main.typ 冒頭で
//   #import "../style.typ": *
//   #show: kaisetsu.with(nendo: "2025年度")

#let c-blue = rgb("#1e40af")
#let c-orange = rgb("#f59e0b")
#let c-green = rgb("#059669")
#let c-gray = rgb("#64748b")

#let kaisetsu(nendo: "", body) = {
  set document(title: nendo + " 豊島岡女子学園中学校 第3回 算数 解答解説")
  set page(
    paper: "a4",
    margin: (top: 20mm, bottom: 18mm, x: 18mm),
    numbering: "1",
    header: context {
      if counter(page).get().first() > 1 {
        set text(size: 8pt, fill: c-gray)
        [#nendo 豊島岡女子学園中学校 第3回 算数 解答解説]
        h(1fr)
      }
    },
  )
  set text(lang: "ja", font: ("New Computer Modern", "Hiragino Maru Gothic ProN"), size: 10.5pt)
  set par(justify: true, leading: 0.9em, spacing: 1.0em)
  show strong: set text(font: ("New Computer Modern", "Hiragino Kaku Gothic ProN"), weight: "bold")

  // 表紙
  align(center)[
    #v(6mm)
    #text(size: 22pt, font: "Hiragino Kaku Gothic ProN", weight: "bold")[#nendo 豊島岡女子学園中学校]
    #v(1mm)
    #text(size: 17pt, font: "Hiragino Kaku Gothic ProN", weight: "bold")[第3回 算数 解答解説]
    #v(3mm)
  ]
  body
}

// 解答一覧表: kotae-ichiran((("1","(1)", [2 7/12]), ...))
#let kotae-ichiran(rows) = {
  block(width: 100%, above: 4mm, below: 6mm)[
    #text(font: "Hiragino Kaku Gothic ProN", weight: "bold")[■ 解答一覧]
    #v(1mm)
    #table(
      columns: (auto, auto, 1fr),
      inset: (x: 7pt, y: 5pt),
      stroke: 0.5pt + rgb("#94a3b8"),
      fill: (x, y) => if y == 0 { rgb("#e2e8f0") },
      align: (center, center, left),
      [*大問*], [*小問*], [*答え*],
      ..rows.map(r => (r.at(0), r.at(1), r.at(2))).flatten(),
    )
  ]
}

// 大問見出し(四角囲み数字)。分野タグ付き
#let daimon(n, bunya: none) = {
  pagebreak(weak: true)
  block(above: 2mm, below: 3mm, width: 100%)[
    #box(stroke: 1pt, inset: (x: 6pt, y: 3pt))[#text(size: 15pt, weight: "bold", font: "Hiragino Kaku Gothic ProN")[#n]]
    #h(3mm)
    #if bunya != none {
      box(fill: rgb("#dbeafe"), inset: (x: 6pt, y: 3pt), radius: 8pt)[#text(size: 9pt, fill: c-blue)[#bunya]]
    }
  ]
}

// 問題(切り抜き画像)
#let mondai-img(path, width: 100%) = {
  block(
    width: 100%, inset: 6pt, stroke: 0.5pt + rgb("#cbd5e1"), radius: 3pt, below: 4mm,
  )[
    #text(size: 8pt, fill: c-gray)[問題]
    #align(center)[#image(path, width: width)]
  ]
}

// 小問見出し
#let shomon(n, title: none) = {
  block(above: 5mm, below: 2mm, sticky: true)[
    #text(size: 12pt, weight: "bold", font: "Hiragino Kaku Gothic ProN", fill: c-blue)[(#n)]
    #if title != none { h(2mm); text(size: 10pt, fill: c-gray)[#title] }
  ]
}

// 答え
#let kotae(body) = {
  block(above: 3mm, below: 3mm)[
    #box(fill: rgb("#fee2e2"), inset: (x: 7pt, y: 4pt), radius: 2pt)[#text(weight: "bold", fill: rgb("#991b1b"), font: "Hiragino Kaku Gothic ProN")[答え]]
    #h(2mm)
    #box(stroke: 1pt + rgb("#991b1b"), inset: (x: 10pt, y: 4pt))[#text(size: 12pt, weight: "bold")[#body]]
  ]
}

// 解き方の1ステップ(番号付き)。ビデオ解説の「語り」を文章にしたもの
#let step-counter = counter("step")
#let steps-reset() = step-counter.update(0)
#let step(body) = {
  step-counter.step()
  block(above: 2.5mm, below: 2.5mm, width: 100%)[
    #grid(
      columns: (7mm, 1fr),
      column-gutter: 2mm,
      context box(fill: c-blue, inset: 0pt, width: 6mm, height: 6mm, radius: 3mm)[
        #align(center + horizon)[#text(fill: white, size: 9pt, weight: "bold")[#step-counter.get().first()]]
      ],
      body,
    )
  ]
}

// 板書(式・図などを枠で囲む)
#let bansho(body) = {
  block(
    width: 100%, above: 2mm, below: 2mm, inset: 8pt,
    fill: rgb("#f8fafc"), stroke: (left: 2pt + rgb("#94a3b8")),
  )[#body]
}

// ポイント(この問題で覚えておきたいこと)
#let point(body) = {
  block(
    above: 4mm, below: 4mm, width: 100%, inset: 9pt, radius: 3pt,
    fill: rgb("#fef3c7"), stroke: (left: 3pt + c-orange),
  )[
    #text(weight: "bold", fill: rgb("#92400e"), font: "Hiragino Kaku Gothic ProN")[ポイント]
    #v(1mm)
    #body
  ]
}

// 別解
#let bekkai(body) = {
  block(
    above: 3mm, below: 3mm, width: 100%, inset: 9pt, radius: 3pt,
    fill: rgb("#ecfdf5"), stroke: (left: 3pt + c-green),
  )[
    #text(weight: "bold", fill: rgb("#065f46"), font: "Hiragino Kaku Gothic ProN")[別の解き方]
    #v(1mm)
    #body
  ]
}

// 注意(まちがえやすいところ)
#let chui(body) = {
  block(
    above: 3mm, below: 3mm, width: 100%, inset: 9pt, radius: 3pt,
    fill: rgb("#fdf2f8"), stroke: (left: 3pt + rgb("#be185d")),
  )[
    #text(weight: "bold", fill: rgb("#9d174d"), font: "Hiragino Kaku Gothic ProN")[ここに注意!]
    #v(1mm)
    #body
  ]
}

// 丸数字(比の①②③)
#let maru(n) = box(
  stroke: 0.6pt, inset: (x: 2.2pt, y: 1pt), radius: 1em, baseline: 0.12em,
)[#text(size: 0.85em)[#n]]

// 図(画像 or cetz の中身を中央寄せ+キャプション)
#let zu(body, caption: none) = {
  align(center)[
    #block(above: 2mm, below: 2mm)[
      #body
      #if caption != none { v(1mm); text(size: 8.5pt, fill: c-gray)[#caption] }
    ]
  ]
}
