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

/* ===== CSS変数（mm ベース・動的上書き対応） ===== */
:root {
  --C: 8mm;     /* セルサイズ（JS で .print-sheet に上書き） */
  --BL: 3.6mm;  /* かぎ括弧の曲線幅 ≈ C × 0.45 */
  --border: #bbb;
  --line:   #1f2933;
  --accent: #3a7bd5;
}

/* ===== リセット ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Hiragino Kaku Gothic Pro', 'Meiryo', sans-serif;
  background: #f0f2f5;
  color: #222;
}

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

/* 左パネル */
.panel {
  width: 260px; min-width: 220px;
  background: #fff; border-right: 1px solid #ddd;
  padding: 16px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 14px;
}
.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; }
.panel-section select,
.panel-section input[type=number],
.panel-section input[type=text] {
  width: 100%; padding: 6px 8px;
  border: 1px solid #ccc; border-radius: 6px;
  font-size: 13px; background: #fafafa;
}
.panel-section select:focus,
.panel-section input:focus { outline: none; border-color: var(--accent); }

/* パターン折りたたみ：select と同じ見た目に統一 */
.pat-details { width: 100%; }
.pat-summary {
  width: 100%; padding: 6px 8px;
  border: 1px solid #ccc; border-radius: 6px;
  background: #fafafa; cursor: pointer; list-style: none;
  font-size: 13px; color: #333;
  display: flex; align-items: center; justify-content: space-between;
  user-select: none;
}
.pat-summary::-webkit-details-marker { display: none; }
.pat-summary::after { content: '▾'; font-size: 10px; color: #666; }
.pat-details[open] .pat-summary {
  border-color: var(--accent);
  border-bottom-left-radius: 0; border-bottom-right-radius: 0;
}
.pat-details[open] .pat-summary::after { content: '▴'; }
.pat-details .check-group {
  padding: 8px 10px;
  border: 1px solid var(--accent); border-top: none;
  border-bottom-left-radius: 6px; border-bottom-right-radius: 6px;
  background: #fff;
}

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

.toggle-row { display: flex; align-items: center; justify-content: space-between; }
.toggle-row span { font-size: 13px; }
.toggle { position: relative; width: 40px; height: 22px; }
.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; }
.btn-secondary { background: #eee; color: #333; }
.btn-print     { background: #27ae60; color: #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; }
.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; } }


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

/* プレビューエリア */
.preview-area { flex: 1; overflow: auto; padding: 24px; display: flex; flex-direction: column; align-items: center; }

/* ── シートラッパー（ズーム用） ── */
.sheet-wrapper { flex-shrink: 0; }

/* ===== プリントシート ===== */
@media print { .top-btns { display: none; } }


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

/* プレビューエリア */
.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 15mm;
  box-shadow: 0 2px 12px rgba(0,0,0,.12);
  transform-origin: top left;
}

.sheet-header {
  display: flex; align-items: flex-end; gap: 16px; margin-bottom: 4mm;
}

/* 指示エリアツールバー */
.instruction-toolbar {
  display: flex; align-items: center; gap: 6px;
  margin-bottom: 3px;
  font-size: 11px; color: #999;
}
.instruction-toolbar-label { margin-right: 2px; }
.inst-size-btn {
  width: 20px; height: 20px; padding: 0;
  border: 1px solid #ddd; border-radius: 4px;
  background: #f5f5f5; color: #555;
  font-size: 13px; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.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; } }

/* 指示・問題文エリア */
.sheet-instruction {
  min-height: 6mm;
  margin-bottom: 6mm;
  font-size: 13px; line-height: 1.7; color: #222;
  outline: none;
  white-space: pre-wrap;
}
/* 画面：空のとき placeholder を表示、薄い下線でエリアを示す */
.sheet-instruction:empty::before {
  content: attr(data-placeholder);
  color: #bbb;
}
.sheet-instruction[contenteditable="true"] {
  border-bottom: 1px dashed #ccc;
  padding-bottom: 2px;
  cursor: text;
}
@page {
  size: A4 portrait;
  margin: 0;
}

/* 印刷：入力UIを消す */
@media print {
  .sheet-instruction { border: none; padding: 0; }
  .sheet-instruction.print-hide { display: none; }
}
.sheet-title {
  flex: 1;
  font-family: 'M PLUS Rounded 1c', 'Hiragino Maru Gothic Pro', sans-serif;
  font-size: 32px; font-weight: 800;
  outline: none;
  min-width: 40mm;
}
/* 画面：編集可能ヒント */
.sheet-title[contenteditable="true"]:empty::before {
  content: attr(data-placeholder);
  color: #ccc;
}
@media print {
  .sheet-title[contenteditable] { cursor: default; }
}
.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;
}

/* 問題グリッド：1fr で用紙幅を均等分割、問題はセル内中央寄せ */
.problems-grid {
  display: grid;
  grid-template-columns: repeat(var(--grid-cols, 3), 1fr);
  column-gap: var(--col-gap, 6mm);
  row-gap:    var(--row-gap, 6mm);
  justify-items: center;
  align-items:   start;
  align-content: start;
}

/* ===== 1問のラッパー ===== */
.prob-wrap { display: inline-flex; flex-direction: column; gap: 3px; }
.prob-label { font-size: calc(var(--C) * 0.45); font-weight: 600; }

/* ===== 筆算ブラケット本体 ===== */
.hissan {
  display: inline-flex;
  flex-direction: column;
  font-family: 'Hiragino Kaku Gothic Pro', 'Meiryo', monospace;
  color: var(--line);
}

/* 共通行 */
.h-row { display: flex; }

/* 商行：割る数幅 + 括弧幅分だけ左にインデント */
.q-row {
  padding-left: calc(var(--C) * var(--m, 1) + var(--BL));
}

/* 割る数行・内部 */
.main-row { align-items: stretch; }

.h-dvsor {
  display: flex;
  align-items: flex-end;   /* 文字を下寄せ（括弧の上線に揃える） */
  padding-bottom: 2px;
}

/* 括弧内部：上線 + SVG曲線 + 割られる数 */
.h-inner {
  border-top: 3px solid var(--line);
  padding-left: var(--BL);
  position: relative;
  display: flex;
}

/* かぎ括弧のSVG曲線 */
.bk-svg {
  position: absolute;
  left: 0; top: 0;
  width: var(--BL);
  height: 100%;
  overflow: visible;
}

/* 作業エリア */
.h-work {
  padding-left: calc(var(--C) * var(--m, 1) + var(--BL));
  display: flex;
  flex-direction: column;
}

/* 引き算線：常に n 桁全幅・グリッドON時のみ点線表示 */
.h-line {
  height: 0;
  margin: 0;
  width: calc(var(--C) * var(--n, 3));
}
.show-grid .h-line {
  border-top: 2px dashed #555;
}

/* ===== セル共通 ===== */
.hc {
  width: var(--C);
  height: var(--C);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--C) * 0.52);
  font-weight: 600;
  position: relative;
}

/* 割られる数 */
.dv-digit { color: #1f2933; }

/* 割る数 */
.dvsor-digit {
  width: var(--C);
  height: var(--C);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--C) * 0.55);
  font-weight: 700;
}

/* 商・積・余りなどの作業セル */
.wc {
  /* グリッドOFF時は罫線なし */
}

/* グリッドON：right+bottom border で隣接セルの境界を1本に */
.wc-g {
  border-right:  1px solid var(--border);
  border-bottom: 1px solid var(--border);
}
/* 各行・括弧内の先頭セルに left border を追加 */
.h-row > .wc-g:first-child,
.h-inner > .wc-g:first-child {
  border-left: 1px solid var(--border);
}
/* 商行の先頭セルに left border */
.q-row > .wc-g:first-child {
  border-left: 1px solid var(--border);
}
/* 商行・最初の作業行は top border を追加 */
.show-grid .q-row > .wc-g {
  border-top: 1px solid var(--border);
}
.show-grid .h-work > .h-row:first-child > .wc-g {
  border-top: 1px solid var(--border);
}

/* 積行：bottom を消して h-line に任せる */
.wc-g.no-bottom {
  border-bottom: none;
}


/* おろし桁（事前記入済み） */
.bringdown { color: #2c3e50; font-weight: 600; }

/* ヒント番号 */
.hint {
  position: absolute;
  top: 1px; left: 1px;
  font-size: calc(var(--C) * 0.27);
  color: #888;
  line-height: 1;
  font-weight: 400;
}

/* 答え行 */
.answer-row {
  font-size: calc(var(--C) * 0.4); color: #666;
  margin-top: calc(var(--C) * 0.15); padding-left: 2px;
}
.answer-row.answer-shown { color: #e25c4a; font-weight: 600; }



/* ===== 印刷スタイル ===== */
@media print {
  /* index.html から直接印刷する場合の設定 */
  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; margin: 0; transform: none !important;
    width: 210mm; height: 297mm; overflow: visible; padding: 12mm 15mm;
    page-break-after: always;
  }
  #sheetsContainer { gap: 0; }
  .print-sheet:last-child { page-break-after: avoid; }
  .prob-grid-toggle        { display: none; }
  .answer-row.answer-shown { display: none; }
  /* 縦方向もページを均等に使う */
  .problems-grid {
    min-height: var(--grid-min-h, 230mm);
    align-content: space-evenly;
  }
}

/* ── ズームバー ── */
.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; } }
