@import url('https://fonts.googleapis.com/css2?family=M+PLUS+Rounded+1c:wght@700;800&display=swap');

:root {
  --C: 10mm;
  --accent: #3a7bd5;
  --border: #bbb;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Hiragino Kaku Gothic Pro', 'Meiryo', sans-serif;
  background: #FBF8F1; color: #222; /* つくしのひろばのクリーム */
}

/* ===== アプリレイアウト ===== */
.app { display: flex; height: 100vh; overflow: hidden; }

.panel {
  width: 270px; min-width: 230px;
  background: #fff; border-right: 1px solid #ddd;
  padding: 16px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px;
}
.panel h2 { font-size: 14px; font-weight: 700; color: #555; letter-spacing: .05em; }
/* **[hidden] は display:flex に負ける。** 明示的に消さないと、
   hidden を立てた区画がそのまま見えてしまう（がい数・大きな数と同じ不具合） */
.panel-section { display: flex; flex-direction: column; gap: 6px; }
.panel-section[hidden] { display: none !important; }
.panel-section label { font-size: 13px; font-weight: 600; color: #333; }
.panel-section input[type=number] {
  width: 100%; padding: 6px 8px;
  border: 1px solid #ccc; border-radius: 6px;
  font-size: 13px; background: #fafafa;
}
.panel-section input:focus { outline: none; border-color: var(--accent); }
.hint-note { font-size: 11px; color: #999; font-weight: 400; }

.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.toggle-row span { font-size: 13px; }
.toggle { position: relative; width: 40px; height: 22px; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; inset: 0; background: #ccc; border-radius: 22px; cursor: pointer; transition: background .2s; }
.slider::before { content: ''; position: absolute; left: 3px; top: 3px; width: 16px; height: 16px; background: #fff; border-radius: 50%; transition: transform .2s; }
.toggle input:checked + .slider { background: var(--accent); }
.toggle input:checked + .slider::before { transform: translateX(18px); }

.btn { width: 100%; padding: 9px; border: none; border-radius: 8px; font-size: 14px; font-weight: 600; cursor: pointer; transition: opacity .15s; }
.btn:hover { opacity: .85; }
.btn-primary { background: var(--accent); color: #fff; }

.divider { border: none; border-top: 1px solid #eee; }

/* ── 桁の組み合わせ ── */
.digit-selector { display: flex; flex-direction: column; gap: 5px; }
.combo-pill {
  padding: 6px 10px; border: 1.5px solid #ddd; border-radius: 8px;
  background: #fff; color: #555; font-size: 12px; font-weight: 600;
  cursor: pointer; transition: all .15s; text-align: left;
}
.combo-pill:hover { border-color: var(--accent); color: var(--accent); }
.combo-pill.active {
  background: var(--accent); border-color: var(--accent);
  color: #fff; box-shadow: 0 1px 4px rgba(58,123,213,.3);
}

/* ── パターン ── */
.check-group { display: flex; flex-direction: column; gap: 5px; }
.check-row { display: flex; align-items: center; gap: 7px; font-size: 13px; cursor: pointer; user-select: none; }
.check-row input[type=checkbox] { width: 15px; height: 15px; cursor: pointer; accent-color: var(--accent); flex-shrink: 0; }
.check-all { font-weight: 600; padding-bottom: 5px; border-bottom: 1px solid #eee; }

/* ── 右上ナビ ── */
.top-btns { position: fixed; top: 16px; right: 20px; z-index: 200; display: flex; gap: 8px; }
.top-btn { padding: 8px 18px; border: none; border-radius: 8px; font-size: 13px; font-weight: 600; cursor: pointer; box-shadow: 0 2px 8px rgba(0,0,0,.18); transition: opacity .15s, transform .1s; }
.top-btn:hover   { opacity: .88; }
.top-btn:active  { transform: translateY(1px); }
.top-btn:disabled { opacity: .4; cursor: default; transform: none; }
.top-btn-settings { background: #fff; color: #333; }
/* 目次へ戻る。単元に入ったあと帰り道がブラウザの戻るしか無かったので足した
   （つくしのひろばに同梱するにあたっての追加。UI_RULES.md にも入れてある） */
.top-btn-back { background: #fff; color: #333; text-decoration: none; display: inline-flex; align-items: center; }

.top-btn-settings.current { background: #e8edf2; color: #555; box-shadow: none; cursor: default; }
.top-btn-preview  { background: #e67e22; color: #fff; }
.top-btn-print    { background: #27ae60; color: #fff; }
.top-btn-answer   { background: #f0f2f5; color: #333; }
.top-btn-answer.active { background: #2ecc71; color: #fff; }
@media print { .top-btns { display: none; } }

/* ── プレビューエリア ── */
.preview-area { flex: 1; overflow: auto; padding: 24px; display: flex; flex-direction: column; align-items: center; }
.sheet-wrapper { flex-shrink: 0; }
#sheetsContainer { display: flex; flex-direction: column; align-items: center; gap: 20px; }

.print-sheet {
  background: #fff; width: 210mm; height: 297mm; overflow: hidden;
  padding: 12mm 16mm;
  box-shadow: 0 8px 24px -8px rgba(92,68,51,.35); /* クリーム地で紙の縁が出る濃さ */
  transform-origin: top left;
  position: relative; /* 出どころ表示を紙の左下に置くため */
}

.sheet-header { display: flex; align-items: flex-end; gap: 16px; margin-bottom: 5mm; }
.sheet-title {
  flex: 1; min-width: 40mm; outline: none;
  font-family: 'M PLUS Rounded 1c', 'Hiragino Maru Gothic Pro', sans-serif;
  font-size: 32px; font-weight: 800;
}
.sheet-title[contenteditable="true"]:empty::before { content: attr(data-placeholder); color: #ccc; }
.header-field { display: flex; align-items: flex-end; gap: 4px; font-size: 18px; white-space: nowrap; }
.header-field .line { display: inline-block; width: 60px; border-bottom: 1px solid #333; }
.header-field .name-line { width: 70mm; }

.sheet-instruction {
  min-height: 6mm; margin-bottom: 5mm;
  font-size: 15px; line-height: 1.7; color: #222;
  outline: none; white-space: pre-wrap;
}
.sheet-instruction:empty::before { content: attr(data-placeholder); color: #bbb; }
.sheet-instruction[contenteditable="true"] { border-bottom: 1px dashed #ccc; padding-bottom: 2px; cursor: text; }
.instruction-toolbar { display: flex; align-items: center; gap: 6px; margin-bottom: 3px; font-size: 11px; color: #999; }
.inst-size-btn { width: 20px; height: 20px; padding: 0; border: 1px solid #ddd; border-radius: 4px; background: #f5f5f5; color: #555; font-size: 13px; display: flex; align-items: center; justify-content: center; cursor: pointer; }
.inst-size-btn:hover { background: #e8e8e8; }
#instSizeDisplay { font-size: 12px; color: #555; min-width: 18px; text-align: center; }
@media print { .no-print { display: none !important; } }

/* ── 問題グリッド ── */
.problems-grid {
  display: grid;
  grid-template-columns: repeat(var(--grid-cols, 3), 1fr);
  column-gap: var(--col-gap, 6mm);
  row-gap: var(--row-gap, 8mm);
  /* **列の中では左寄せ**（利用者の指定・2026-09-21）。
     中央寄せだと、1列のときだけ問題が紙の真ん中に寄り、
     2列のときと左端がそろわなかった */
  justify-items: start;
  align-items: start;
}
.print-sheet .problems-grid {
  min-height: var(--grid-min-h, 240mm);
  align-content: space-evenly;
}
/* 問題が少ない紙（7問未満）は、上から詰めて余りを下の余白にする
   （利用者の指定・2026-09-21）。space-evenly のままだと、
   少ない問題が紙じゅうに散らばる */
.print-sheet.few-rows .problems-grid { align-content: start; }

.prob-wrap { display: inline-flex; flex-direction: column; gap: 2px; }
.prob-label { font-size: calc(var(--C) * 0.45); font-weight: 600; }

/* ===== 数の手直し（設定画面のみ・印刷には出ない） ===== */


.mult-row.editable { cursor: pointer; border-radius: 3px; transition: background .1s; }
.mult-row.editable:hover {
  background: rgba(58,123,213,.13);
  box-shadow: 0 0 0 1.5px var(--accent);
}

.cell-editor {
  position: absolute; top: 100%; left: 0; z-index: 60;
  margin-top: 4px; padding: 8px;
  background: #fff; border: 1px solid #ccc; border-radius: 8px;
  box-shadow: 0 4px 14px rgba(0,0,0,.18);
}
.cell-editor input {
  width: 96px; padding: 5px 7px;
  border: 1.5px solid #ccc; border-radius: 6px;
  font-size: 14px; font-family: inherit;
}
.cell-editor input:focus { outline: none; border-color: var(--accent); }
.cell-editor-note {
  width: 190px; margin-top: 5px;
  font-size: 11px; line-height: 1.5; color: #888;
}
.cell-editor-note.error { color: #e74c3c; }
/* 入力欄と決定ボタン */
.cell-editor-row { display: flex; align-items: center; gap: 6px; }
.cell-editor-ok {
  padding: 6px 12px; border: none; border-radius: 6px;
  background: var(--accent); color: #fff;
  font-size: 13px; font-weight: 600; white-space: nowrap; cursor: pointer;
}
.cell-editor-ok:hover  { opacity: .85; }
.cell-editor-ok:active { transform: translateY(1px); }

@media print {
  .cell-editor { display: none; }
  .mult-row.editable:hover { background: none; box-shadow: none; }
}

/* ── 分数の式 ── */
/* **番号は式の左どなり**（利用者の指定・2026-09-19）。
   2列のグリッドにして、1行目に「番号｜式」を置く。align-items: center が
   効くのは行ごとなので、番号は式（＝分数）の上下中心にそろう。
   ヒントの行は2列目に続けるので、番号の位置は動かない */
.prob-wrap {
  display: inline-grid;
  grid-template-columns: auto auto;
  column-gap: calc(var(--C) * 0.12);
  align-items: center;
  justify-items: start;
  row-gap: 1px;
}
.prob-label { grid-column: 1; grid-row: 1; font-size: calc(var(--C) * 0.45); font-weight: 600; }
.fr-expr { grid-column: 2; grid-row: 1; }
.fr-conv, .fr-den-hint { grid-column: 2; }

.fr-expr, .fr-conv {
  display: inline-flex;
  align-items: center;
  white-space: nowrap;
}

/* 帯分数の整数部 */
.fr-term { display: inline-flex; align-items: center; }
/* 帯分数の整数部。**分数の数字より少し小さく**（利用者の指定・2026-09-20）。
   0.62 では整数だけが大きく見え、分数と釣り合わなかった */
.fr-whole {
  font-size: calc(var(--C) * 0.52);
  font-weight: 600;
  padding-right: calc(var(--C) * 0.08);
}
.fr-whole-blank { min-width: calc(var(--C) * 0.40); }

/* 分数：分子・横線・分母の縦組み */
.frac {
  display: inline-flex;
  flex-direction: column;
  align-items: stretch;
  text-align: center;
  padding: 0 calc(var(--C) * 0.10);
  font-size: calc(var(--C) * 0.40);
  font-weight: 600;
  line-height: 1.15;
}
.frac .fr-n { border-bottom: 2px solid currentColor; padding-bottom: 1px; }
.frac .fr-d { padding-top: 1px; }
.frac .fr-n, .frac .fr-d { min-width: calc(var(--C) * 0.34); }

/* 答えを書く欄。答えは分数にも整数にもなるので、分数の線は引かない。
   書き込む場所だけを下線で示し、分数が書けるだけの高さをとる。 */
/* 答えを書く場所。**下線は引かない**（利用者の指定・2026-09-19）。
   場所だけ空けておく。分数は線のある形なので、下線があると
   分母の線と紛れて見える */
.fr-blank {
  display: inline-block;
  width:  calc(var(--C) * 1.30);
  height: calc(var(--C) * 1.00);
  margin-left: calc(var(--C) * 0.06);
}
.fr-blank-wide { width: calc(var(--C) * 1.90); }

.fr-op, .fr-eq {
  font-size: calc(var(--C) * 0.50);
  font-weight: 700;
  padding: 0 calc(var(--C) * 0.06);
}

/* ヒント1：通分したあとの式 */
.fr-conv { color: #bbb; margin-top: calc(var(--C) * 0.10); }
.fr-conv .frac .fr-n { border-bottom-color: #bbb; }

/* ヒント2：そろえる分母 */
/* ヒント：**分母に薄い斜線を引き、その右斜め下にそろえた分母を書く**
   （利用者の指定・2026-09-20）。赤ペンで直すときの書き方に合わせた。
   **薄字も斜線も場所を取らせない**（絶対配置）。ヒントのぶん高さが増えると、
   紙に収めるために問題全体が小さくなってしまう */
.frac .fr-d-cut { position: relative; }
.frac .fr-d-cut::after {
  content: '';
  position: absolute;
  left: -6%; right: -6%; top: 50%;
  border-top: 1.5px solid #bbb;
  transform: rotate(-24deg);
}
.fr-d-new {
  position: absolute;
  left: 92%;
  top: 58%;
  font-size: calc(var(--C) * 0.30);
  font-weight: 600;
  color: #bbb;
  line-height: 1;
}

/* ── なぞり書き（子どもがなぞる式だけを薄く） ── */
.print-sheet.trace-mode .fr-whole,
.print-sheet.trace-mode .frac,
.print-sheet.trace-mode .fr-op,
.print-sheet.trace-mode .fr-eq { color: #ccc; }
.print-sheet.trace-mode .frac .fr-n { border-bottom-color: #ccc; }

/* ── 数の手直し（設定画面のみ・印刷には出ない） ── */
.fr-term.editable { cursor: pointer; border-radius: 4px; transition: background .1s; }
.fr-term.editable:hover {
  background: rgba(58,123,213,.13);
  box-shadow: 0 0 0 1.5px var(--accent);
}

.cell-editor {
  position: absolute; top: 100%; left: 0; z-index: 60;
  margin-top: 4px; padding: 8px;
  background: #fff; border: 1px solid #ccc; border-radius: 8px;
  box-shadow: 0 4px 14px rgba(0,0,0,.18);
}
.cell-editor-row { display: flex; align-items: center; gap: 6px; }
.cell-editor input {
  width: 110px; padding: 5px 7px;
  border: 1.5px solid #ccc; border-radius: 6px;
  font-size: 14px; font-family: inherit;
}
.cell-editor input:focus { outline: none; border-color: var(--accent); }
.cell-editor-ok {
  padding: 6px 12px; border: none; border-radius: 6px;
  background: var(--accent); color: #fff;
  font-size: 13px; font-weight: 600; white-space: nowrap; cursor: pointer;
}
.cell-editor-ok:hover  { opacity: .85; }
.cell-editor-ok:active { transform: translateY(1px); }
.cell-editor-note {
  width: 230px; margin-top: 5px;
  font-size: 11px; line-height: 1.5; color: #888;
}
.cell-editor-note.error { color: #e74c3c; }

@media print {
  .cell-editor { display: none; }
  .fr-term.editable:hover { background: none; box-shadow: none; }
}

.prob-wrap { position: relative; }

/* 印刷では拡大縮小を戻す。画面の zoom をそのまま持ち込むと紙の寸法が狂う */
@media print {
  .print-sheet { zoom: 1 !important; transform: none !important; }
}
/* ── 印刷で白紙のページが出るのを直す（2026-08-31・全単元） ──────────
   Safari で「2枚目以降が白紙」「設定を変えると全部白紙」になる原因が3つある。
   角のもんだい・補角では 2026-08-26 に直してあったが、残り16単元に入っていなかった。

   **この節は必ずスタイルシートの末尾に置く。** 途中に置くと、後ろにある
   `.print-sheet` の指定（overflow: visible など）に上書きされて効かなくなる。

   1. **改ページは包む箱（.sheet-wrapper）に付ける。**
      `.print-sheet:last-child` は「箱の中のただ一つの子」なので**すべての紙に当たり**、
      page-break-after を avoid に上書きしていた（実測: 3枚とも avoid）。
      改ページが1つも入らないため、2枚目以降が用紙から溢れて白紙になる
   2. **印刷は block で流す。** flex のままだと、紙が用紙に入りきらないときに
      ページをまたいで分割されず、紙まるごと次のページへ送られる（＝白紙）
   3. **紙面ちょうど（297mm ＝ 1122.52px）だと高さが整数pxへ切り上がり**、
      わずかにはみ出して中身の無いページが挟まる。少しだけ低くする */
@media print {
  .print-sheet {
    height: 296.8mm !important;
    overflow: hidden !important;
    /* 改ページは箱が持つ。紙に残っていると最後に空ページが増える */
    page-break-after: auto !important;
    break-after: auto !important;
  }
  /* **画面の部品は必ず消す。** block で流すと、消し忘れた部品が紙の「上」に
     積まれて紙を押し下げ、下がはみ出して2ページ目ができる
     （kaku・hokaku は設定パネル、bunsu はズームバーを消していなかった。
      flex のうちは横に並んでいたので気づけなかった） */
  .panel, .top-btns, .zoom-bar, .instruction-toolbar, .no-print { display: none !important; }
  /* **包む箱は印刷用に戻しきる。** display だけ直しても、画面用の高さ・はみ出し・
     余白が残っていると紙が切り取られたり、余白のぶんだけ空ページが増える
     （bunsu は .app の高さが 100vh のままで3枚目まで届かず、
      .preview-area の下余白 24px が4ページ目を作っていた） */
  html, body, .app, .preview-area, #sheetsContainer {
    display: block !important;
    height: auto !important;
    overflow: visible !important;
    padding: 0 !important;
    margin: 0 !important;
    gap: 0 !important;
  }
  .sheet-wrapper { break-after: page; page-break-after: always; }
  .sheet-wrapper:last-child { break-after: auto; page-break-after: auto; }
}

/* ── 出どころ表示（刷った紙だけを見ても、どこの教材か分かるように）──
   **no-print を付けない。** 画面の飾りではなく、紙に残すためのもの。
   位置と大きさはドリルのPDFと同じ（高さ3.5mm・左8mm・下5mm）。
   紙の下の余白の中に置くので、問題の場所は取らない。
   決めごとは docs/backlog.md「5.（実装ずみ）」 */
.sheet-credit {
  position: absolute; left: 8mm; bottom: 5mm; height: 3.5mm;
  pointer-events: none; user-select: none;
}
.sheet-credit img { height: 100%; width: auto; display: block; }
