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

:root {
  --accent: #3a7bd5;
  --border: #ddd;
}

*, *::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 {
  /* 左の設定欄の幅は**多くの単元にそろえて 270px**（15単元が 270、とけいは 260 だった）。
     ここが違うと、枚数などの欄の幅も単元ごとに 10px ずれて見える */
  width: 270px; min-width: 220px;
  background: #fff; border-right: 1px solid #ddd;
  padding: 16px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px;
  flex-shrink: 0;
}
.panel h2 { font-size: 14px; font-weight: 700; color: #555; letter-spacing: .05em; }
.panel-section { display: flex; flex-direction: column; gap: 6px; }
.panel-section label { font-size: 13px; font-weight: 600; color: #333; }
/* 枚数（数値の欄）も select と同じ見た目にする。**他の単元はすべてこの指定がある。**
   とけいだけ抜けていて、枚数の欄がブラウザ既定の幅（狭い）のままだった */
.panel-section input[type=number],
.panel-section select { width: 100%; padding: 6px 8px; border: 1px solid #ccc; border-radius: 6px; font-size: 13px; background: #fafafa; }
.panel-section input:focus,
.panel-section select:focus { outline: none; border-color: var(--accent); }

.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; }

.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; }

/* 時の固定設定 */
.hour-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.hour-row input[type=radio] { accent-color: var(--accent); cursor: pointer; }
#fixedHourSelect { width: 70px; padding: 4px 6px; border: 1px solid #ccc; border-radius: 6px; font-size: 13px; }

/* ── 右上ナビ ── */
.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; }

/* ── プリントシート（横向きA4）── */
.print-sheet {
  background: #fff;
  width: 297mm; height: 210mm; overflow: hidden;
  padding: 6mm 10mm;
  box-shadow: 0 8px 24px -8px rgba(92,68,51,.35); /* クリーム地で紙の縁が出る濃さ */
  display: flex; flex-direction: column;
  transform-origin: top left;
  position: relative; /* 出どころ表示を紙の左下に置くため */
}

/* **文字の下端（ベースライン）でそろえる。** flex-end は箱の下端をそろえるので、
   指示文の下線と余白のぶんだけ字が上へずれ、題名と高さが食い違って見えた */
.sheet-header { display: flex; align-items: baseline; gap: 10px; margin-bottom: 3mm; flex-shrink: 0; }

.sheet-instruction {
  flex: 2;
  /* 読むのは低学年の子ども。13px では題名（28px）に対して小さすぎた */
  font-size: 15px; line-height: 1.5; color: #222;
  outline: none; white-space: pre-wrap; min-width: 30mm;
  border-bottom: 1px dashed #ccc; padding-bottom: 2px;
  align-self: flex-end;
}
.sheet-instruction:empty::before { content: attr(data-placeholder); color: #bbb; }
@media print {
  .sheet-instruction { border: none; padding: 0; }
  .sheet-instruction.print-hide { display: none; }
}
.sheet-title {
  flex: 1; min-width: 40mm; outline: none;
  font-family: 'M PLUS Rounded 1c', 'Hiragino Maru Gothic Pro', sans-serif;
  font-size: 28px; 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: 16px; white-space: nowrap; }
.header-field .line { display: inline-block; width: 60px; border-bottom: 1px solid #333; }
.header-field .name-line { width: 60mm; }

/* ── 時計グリッド ── */
/* **文字盤の大きさは「行に入る高さ」で決める。**
   幅だけで決めて 78mm 固定にしていたため、答え欄が高くなる設定
   （ひらがな・ルビつき）で行が伸び、紙から 2.3mm はみ出して下が切れていた。
   `overflow: hidden` で隠れるので画面では気づけない。
   紙は高さの決まった縦並びなので、行の高さも決まる。あとは flex に
   縮めてもらえば、収まるときは 78mm のまま、収まらないときだけ小さくなる */
.clocks-grid {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: 1fr;          /* 行の高さを等分＝紙に収まる高さにする */
  gap: 0mm 3mm;
  align-content: stretch;
  align-items: stretch;
}

.clock-problem {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 1mm;
  min-height: 0;                /* 既定の min-height:auto だと縮まない */
}

.clock-face {
  /* 78mm を上限に、行に入らなければ縮む（aspect-ratio で横も一緒に縮む） */
  flex: 0 1 auto;
  height: 78mm; width: auto; max-width: 100%; min-height: 0;
  aspect-ratio: 1;
}
.clock-problem .answer-blank { flex: 0 0 auto; }
.clock-face svg { width: 100%; height: 100%; }

/* ── 答え欄 ── */
.answer-blank {
  font-size: 22px; font-weight: 600; letter-spacing: .05em;
  display: flex; align-items: flex-end; gap: 3px;
  white-space: nowrap;
}
.answer-blank ruby { display: inline-flex; flex-direction: column-reverse; align-items: center; }
.answer-blank rt { font-size: 0.65em; font-weight: 400; letter-spacing: 0; line-height: 1; }
.ans-blank-h {
  display: inline-block; width: 2.8em; border-bottom: 2px solid #333;
  vertical-align: bottom; margin: 0 3px;
}
.ans-blank-m {
  display: inline-block; width: 3.2em; border-bottom: 2px solid #333;
  vertical-align: bottom; margin: 0 3px;
}
/* 半（30分）用。「はん」とも「30ぷん」とも書けるよう、分の欄より広くする */
.ans-blank-free {
  display: inline-block; width: 4.6em; border-bottom: 2px solid #333;
  vertical-align: bottom; margin: 0 3px;
}
.ans-val { color: #e8786a; font-weight: 700; margin: 0 3px; }

/* ── 印刷 ── */
@page { size: A4 landscape; margin: 0; }
@media print {
  body { background: #fff; }
  .app { display: block; height: auto; }
  .panel { display: none; }
  .preview-area { padding: 0; overflow: visible; display: block; }
  .sheet-wrapper { width: auto !important; height: auto !important; }
  .print-sheet { box-shadow: none; transform: none !important; margin: 0; width: 297mm; height: 210mm; overflow: visible; padding: 6mm 10mm; page-break-after: always; }
  #sheetsContainer { gap: 0; }
  .print-sheet:last-child { page-break-after: avoid; }
  .no-print { display: none !important; }
}

/* ── ズームバー ── */
.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; } }
.ans-val { color: #e25c4a; font-weight: 700; }
@media print { .ans-val { color: #e25c4a !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; } }

/* 印刷では拡大縮小を戻す。画面の 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 {
    height: 209.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; }
