@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; }
.panel-section { display: flex; flex-direction: column; gap: 6px; }
/* いま効かない設定は薄くして押せなくする（たし算の .locked と同じ） */
.panel-section.locked { opacity: 0.45; pointer-events: none; }
.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; }
.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; }

/* ── パターン（あまりなし／あまりあり：どちらか一方） ── */
.group-pills { display: flex; flex-wrap: wrap; gap: 5px; }
.group-pill {
  padding: 6px 12px; border: 1.5px solid #ddd; border-radius: 20px;
  background: #fff; color: #555; font-size: 13px; font-weight: 600;
  cursor: pointer; transition: all .15s;
}
.group-pill:hover { border-color: var(--accent); color: var(--accent); }
.group-pill.active {
  background: var(--accent); border-color: var(--accent);
  color: #fff; box-shadow: 0 1px 4px rgba(58,123,213,.3);
}

/* ── わる数の数字ピル ── */
.num-pills { display: flex; flex-wrap: wrap; gap: 4px; }
.num-pill {
  width: 30px; height: 30px; padding: 0;
  border: 1.5px solid #ddd; border-radius: 50%;
  background: #fff; color: #555; font-size: 13px; font-weight: 700;
  cursor: pointer; transition: all .12s;
}
.num-pill:hover { border-color: var(--accent); color: var(--accent); }
.num-pill.active {
  background: var(--accent); border-color: var(--accent);
  color: #fff; box-shadow: 0 1px 4px rgba(58,123,213,.3);
}
.pill-shortcuts { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.shortcut-btn {
  padding: 3px 9px; border: 1px solid #ddd; border-radius: 12px;
  background: #fafafa; color: #666; font-size: 11px; cursor: pointer;
  transition: all .12s;
}
.shortcut-btn:hover { border-color: var(--accent); color: var(--accent); background: #fff; }

/* ── 右上ナビ ── */
.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 {
  /* **A4 縦**。1問の高さが絵5こぶん（右が2列×5行）あるので、
     縦向きにして問題を積む。横向きだと2問しか入らない */
  background: #fff; width: 210mm; height: 297mm; overflow: hidden;
  padding: 12mm 16mm;   /* 1年生の縦向きの単元（たし算・ひき算）とそろえる */
  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: 6mm; }
/* 見出しは中身ぶんだけ取り、**余った幅は名前を書く線に回す。**
   固定幅にすると、題名が短いときに余白が捨てられて名前欄が狭いままになる
   （BACKLOG「書き込み欄の幅は固定値で決め打ちしない」と同じ考え方） */
.sheet-title {
  flex: 1 1 auto; min-width: 40mm; outline: none;
  font-family: 'M PLUS Rounded 1c', 'Hiragino Maru Gothic Pro', sans-serif;
  /* **題名の大きさは他の単元とそろえる（32px）。** 小さくすると単元ごとに
     見出しの大きさが違う紙になる。題名が長くて折り返しそうなときは、
     字を縮めるのではなく名前の線を少し縮める（下の .name-line） */
  font-size: 32px; font-weight: 800; white-space: nowrap;
}
.sheet-title[contenteditable="true"]:empty::before { content: attr(data-placeholder); color: #ccc; }
/* 名前欄は**紙の右端にそろえる。** 線に上限があるぶん余りが出るので、
   それを左（見出しとのあいだ）に寄せて、線を右端に着けておく */
.header-field { display: flex; align-items: flex-end; justify-content: flex-end;
                 gap: 4px; font-size: 18px; white-space: nowrap; flex: 1 1 auto; }
.header-field .line { display: inline-block; width: 60px; border-bottom: 1px solid #333; }
/* **上限と下限の両方を mm で持つ。**
   余った幅をぜんぶ線に回すと、A4 横では 190mm を超えて長すぎる線になる。
   1年生は名前をひらがなで大きく書く（1文字 10mm ほど）ので、
   姓と名で 8〜9文字ぶん＝ 90mm あれば足りる。
   下限は、題名を長く書き換えられてもこれ以下には狭まらない幅 */
/* 名前の線は他の縦向きの単元と同じ 70mm。題名が長いときだけ 60mm まで縮む */
.header-field .name-line { flex: 0 1 70mm; width: 70mm; min-width: 60mm; max-width: 70mm; }

.sheet-instruction {
  min-height: 6mm; margin-bottom: 6mm;
  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; } }

/* ── かずだけ ぬろう ── */
/* 1問＝「数字の箱」＋「イラスト10こ」。すべて mm で組み、
   問題数が変わっても1こあたりの大きさ（--S）だけを変える */
/* **行は紙の高さいっぱいに均等に配る。**
   上に詰めて下だけ大きく余ると、刷り損ねたように見える */
.on-list {
  display: flex; flex-direction: column;
  justify-content: space-evenly;
  gap: var(--row-gap, 6mm);
  min-height: var(--list-h, auto);
}

/* 1問 ＝ 番号｜ちらばった絵｜矢印｜2列×5行の絵 */
.on-prob { display: flex; align-items: center; gap: 4mm; }

.on-label {
  width: 10mm; flex: 0 0 10mm;
  /* 問題番号は他の縦向きの単元とそろえる（約 20px） */
  font-size: 5.2mm; font-weight: 600; color: #333;
}

/* ── 左：ちらばった絵 ──
   **枠で囲う。** 囲わないと、どこまでが「この問題の絵」か分からず、
   下の問題の絵と混ざって数えまちがえる */
.on-left {
  position: relative;
  flex: 0 0 calc(var(--S) * 5.5);
  width:  calc(var(--S) * 5.5);
  height: calc(var(--S) * 2.6);
  border: 1.2px solid #bbb; border-radius: 2mm;
}
/* 絵は中心を置き場所にする（generator が % で持っている） */
.on-spot {
  position: absolute;
  /* generator.js の ICON と同じ値（0.8）。変えるときは両方を変える */
  width: calc(var(--S) * 0.8); height: calc(var(--S) * 0.8);
  transform: translate(-50%, -50%) rotate(var(--rot, 0deg));
}
.on-spot img { width: 100%; height: 100%; object-fit: contain; }

/* ── まんなかの矢印 ── */
.on-arrow {
  flex: 0 0 6mm; height: 6mm; position: relative;
}
.on-arrow::after {
  content: ''; position: absolute; inset: 0;
  border-top: 1.2px solid #999; border-right: 1.2px solid #999;
  width: 3mm; height: 3mm; margin: auto;
  transform: rotate(45deg);
}

/* ── 右：5列×2行の10こ。ここを子どもが塗る ──
   **1こずつは囲わず、10こまとめて丸角の枠で囲う。**
   マスで区切ると「1こずつ塗る作業」に見えてしまい、
   10のまとまりとして見えにくくなる */
.on-right {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(var(--cols, 5), calc(var(--S) * 0.95));
  grid-auto-rows: calc(var(--S) * 0.95);
  gap: 0;
  border: 1.2px solid #333; border-radius: 2mm;
  padding: calc(var(--S) * 0.08);
}
.on-cell {
  display: flex; align-items: center; justify-content: center;
  padding: calc(var(--S) * 0.05);
}
.on-cell img { width: 100%; height: 100%; object-fit: contain; }

.on-circle {
  width: 100%; height: 100%; border-radius: 50%;
  border: 1.2px solid #333; box-sizing: border-box;
}
.on-circle-filled { background: #cecece; -webkit-print-color-adjust: exact; print-color-adjust: exact; }

/* 答えを見せるとき（丸つけ用）。左から n こ、**塗った絵**にする。
   薄くする指定は要らない（絵そのものが塗ってある） */
.on-cell.on-filled .on-circle { background: #cecece; -webkit-print-color-adjust: exact; print-color-adjust: exact; }

/* 設定画面でだけ、押して直せることを見せる */

/* ── 設問を直す画面（左の箱か番号を押すと開く） ──
   **この指定を消さないこと。** style.css を書き直したときに一度まるごと
   落としてしまい、画面は開いているのに何も見えず、「押しても変わらない」
   不具合になった（実機で確認）。表示・非表示はこの `.on` が受け持つ */
.icon-picker {
  position: fixed; inset: 0; z-index: 300;
  background: rgba(40,32,26,.5);
  display: none; align-items: center; justify-content: center; padding: 24px;
}
.icon-picker.on { display: flex; }
.icon-picker-box {
  background: #fff; border-radius: 14px; padding: 18px 20px;
  max-width: 900px; max-height: 86vh; overflow: auto;
}
.icon-picker h3 { font-size: 15px; margin-bottom: 4px; }
.icon-picker .lead { font-size: 12px; color: #777; margin-bottom: 12px; }
.icon-picker h4 { font-size: 13px; color: #555; margin: 14px 0 6px; }
.icon-picker .grid { display: flex; flex-wrap: wrap; gap: 8px; }
.icon-pick {
  width: 76px; padding: 6px 4px; border: 1.5px solid #e2e2e2; border-radius: 8px;
  background: #fff; cursor: pointer; font-size: 11px; color: #555;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.icon-pick:hover { border-color: var(--accent); color: var(--accent); }
.icon-pick.current { border-color: var(--accent); background: #eef4fd; }
.icon-pick img { width: 46px; height: 46px; object-fit: contain; }
.icon-picker-close {
  margin-top: 14px; padding: 7px 16px; border: none; border-radius: 8px;
  background: #f0f2f5; font-size: 13px; cursor: pointer;
}

/* ── 設問を直す画面の「かず」 ── */
.num-grid { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 4px; }
.num-pick {
  width: 44px; height: 44px; border-radius: 10px; cursor: pointer;
  border: 1.5px solid #d8d2c8; background: #fff;
  font-size: 18px; font-weight: 700; color: #333;
}
.num-pick:hover { background: #f6f9ff; }
.num-pick.current { border-color: #3b6cd4; background: #eaf1ff; color: #1e46a0; }

.on-label.editable, .on-left.editable { cursor: pointer; }
.on-label.editable:hover { background: #eef4fd; }
.on-left.editable:hover  { background: #f6f9ff; }
@media print {
  .on-label.editable:hover, .on-left.editable:hover { background: none; }
}

@page { size: A4 portrait; margin: 0; }
@media print {
  body { background: #fff; }
  .app { display: block; height: auto; }
  .panel { display: none; }
  .preview-area { padding: 0; overflow: visible; }
  .sheet-wrapper { width: auto !important; height: auto !important; }
  .print-sheet {
    box-shadow: none; transform: none !important; margin: 0;
    /* **印刷用の大きさも A4 縦にそろえる。** ここが横向き（297×210mm）の
       ままだと、紙が縦の用紙に押し込まれて 70% ほどに縮んで刷られる（実測） */
    width: 210mm; height: 297mm; overflow: visible;
    page-break-after: always;
  }
  #sheetsContainer { gap: 0; }
  .print-sheet:last-child { page-break-after: avoid; }
  .sheet-instruction { border: none; padding: 0; }
}

/* ── ズームバー ── */
.zoom-bar {
  position: fixed; bottom: 20px; right: 24px; z-index: 150;
  display: flex; align-items: center; gap: 1px;
  background: rgba(255,255,255,.88);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(0,0,0,.10);
  border-radius: 20px; padding: 3px 5px;
  box-shadow: 0 2px 10px rgba(0,0,0,.10);
  opacity: .55; transition: opacity .2s; user-select: none;
}
.zoom-bar:hover { opacity: 1; }
.zoom-btn {
  width: 26px; height: 26px; border: none; border-radius: 13px;
  background: transparent; font-size: 15px; font-weight: 700;
  color: #444; cursor: pointer; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  transition: background .12s;
}
.zoom-btn:hover { background: rgba(0,0,0,.07); }
.zoom-reset {
  min-width: 42px; border: none; border-radius: 4px;
  background: transparent; font-size: 12px; font-weight: 600;
  color: #555; cursor: pointer; padding: 2px 4px;
  transition: background .12s; border-left: 1px solid rgba(0,0,0,.09);
  margin-left: 2px; padding-left: 7px;
}
.zoom-reset:hover { background: rgba(0,0,0,.07); }
.zoom-slider {
  -webkit-appearance: none; appearance: none;
  width: 90px; height: 3px;
  background: rgba(0,0,0,.18); border-radius: 2px;
  outline: none; cursor: pointer; margin: 0 2px;
}
.zoom-slider::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 13px; height: 13px; border-radius: 50%;
  background: #3a7bd5; box-shadow: 0 1px 3px rgba(58,123,213,.4);
  transition: transform .1s;
}
.zoom-slider::-webkit-slider-thumb:hover { transform: scale(1.2); }
.zoom-slider::-moz-range-thumb {
  width: 13px; height: 13px; border-radius: 50%; border: none;
  background: #3a7bd5; box-shadow: 0 1px 3px rgba(58,123,213,.4);
}
@media print { .zoom-bar { display: none; } }

/* 印刷では拡大縮小を戻す。画面の 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. **紙面ちょうど（210mm ＝ 793.7px）だと高さが整数pxへ切り上がり**、
      わずかにはみ出して中身の無いページが挟まる。少しだけ低くする */
@media print {
  .print-sheet {
    /* A4 縦の高さ。**かずぬり（A4横）から持ってきた 209.8mm のままだと、
       紙が 210mm で切られ、下 87mm に地色が出たまま刷られる**（実測） */
    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; }
