* { box-sizing: border-box; }
html, body { margin: 0; }

:root {
  --sheet-w: 210mm;
  --sheet-h: 297mm;
  --page-w: 190mm;
  --page-h: 276mm;
}

body {
  font-family: "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
  background: #e8eaed;
  color: #202124;
}

#app { display: flex; height: 100vh; }

#sidebar {
  width: 470px;
  flex: none;
  background: #fff;
  border-right: 1px solid #dadce0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px 16px 44px;
}

#preview { flex: 1; overflow: auto; padding: 20px; }

h1 { font-size: 18px; margin: 4px 0 4px; color: #174ea6; }
.subtitle { font-size: 11.5px; color: #5f6368; margin: 0 0 10px; line-height: 1.4; }

h2 {
  font-size: 13.5px;
  margin: 16px 0 8px;
  padding-left: 8px;
  border-left: 4px solid #1a73e8;
}

button {
  font: inherit;
  font-size: 12.5px;
  cursor: pointer;
  border: 1px solid #dadce0;
  background: #fff;
  border-radius: 6px;
  padding: 4px 8px;
}
button:hover { background: #f1f3f4; }

button.primary {
  background: #1a73e8;
  border-color: #1a73e8;
  color: #fff;
  font-weight: bold;
  padding: 6px 14px;
}
button.primary:hover { background: #1765cc; }

input[type=text], input[type=date], select, textarea {
  font: inherit;
  font-size: 12.5px;
  padding: 4px 6px;
  border: 1px solid #dadce0;
  border-radius: 5px;
}
#catalog-filter { width: 100%; margin-bottom: 6px; }

#stage-filter {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 4px;
  margin-bottom: 8px;
}
#stage-filter button {
  font-size: 11px;
  padding: 5px 2px;
  line-height: 1.25;
  text-align: center;
  white-space: normal;
  word-break: keep-all;
}

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 6px 0; font-size: 12.5px; }
.muted { color: #5f6368; font-size: 11.5px; }
.status { font-size: 12px; color: #188038; min-height: 1.4em; font-weight: bold; }

.catalog-group summary { cursor: pointer; font-weight: bold; font-size: 13px; padding: 4px 0; }
.catalog-item-wrap { padding: 4px 6px; border-radius: 6px; }
.catalog-item-wrap:hover { background: #f1f3f4; }
.catalog-item { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.catalog-item .name { flex: 1; line-height: 1.35; }
.catalog-item .last { color: #5f6368; font-size: 11px; width: 4.2em; text-align: right; }
.catalog-item.done .name { color: #9aa0a6; text-decoration: line-through; }
.catalog-goal { font-size: 11px; color: #5f6368; margin: 2px 0 0 28px; line-height: 1.35; }

.badge { display: inline-block; font-size: 10px; font-weight: bold; padding: 1px 5px; border-radius: 4px; flex: none; }
.badge-基礎 { background: #e6f4ea; color: #137333; border: 1px solid #ceead6; }
.badge-標準 { background: #e8f0fe; color: #1967d2; border: 1px solid #d2e3fc; }
.badge-発展 { background: #fef7e0; color: #b06000; border: 1px solid #feefc3; }

.set-item {
  display: flex;
  align-items: center;
  gap: 4px;
  border: 1px solid #dadce0;
  border-radius: 8px;
  padding: 5px 6px;
  margin-bottom: 6px;
  background: #fff;
  font-size: 12.5px;
}
.set-item.dragging { opacity: 0.4; }
.set-item .handle { cursor: grab; color: #9aa0a6; padding: 0 4px; }
.set-item .name { flex: 1; }
.set-item input[type=number] { width: 44px; text-align: center; font: inherit; }

.panel-box {
  margin-top: 14px;
  border: 1px solid #dadce0;
  border-radius: 8px;
  padding: 8px 10px;
  background: #fafafa;
  font-size: 12px;
}
.panel-box summary {
  cursor: pointer;
  font-weight: bold;
  color: #3c4043;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.panel-body { margin-top: 8px; padding-top: 8px; border-top: 1px solid #e8eaed; }
.archive-entry {
  background: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  padding: 6px 8px;
  margin-bottom: 6px;
}
.archive-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-bottom: 3px; }
.archive-titles { font-size: 11.5px; color: #333; margin-bottom: 5px; }
.archive-actions { display: flex; gap: 4px; flex-wrap: wrap; }
.archive-actions button { font-size: 11px; padding: 2px 6px; }

/* ---------- A4用紙プレビュー＆印刷レイアウト ---------- */
.sheet {
  width: var(--sheet-w);
  height: var(--sheet-h);
  padding: 10mm;
  margin: 0 auto 18px;
  background: #fff;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.25);
  overflow: hidden;
}

.page {
  width: var(--page-w);
  height: var(--page-h);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  color: #000;
}

body.font-gothic .page {
  font-family: "BIZ UDPGothic", "BIZ UDPゴシック", "Hiragino Sans", "Yu Gothic", "Meiryo", sans-serif;
}
body.font-maru .page {
  font-family: "Hiragino Maru Gothic ProN", "BIZ UDPGothic", "Hiragino Sans", "Meiryo", sans-serif;
}
body.font-kyokasho .page {
  font-family: "UD デジタル 教科書体 NK-R", "UD Digi Kyokasho NK-R", "YuKyokasho Yoko", "Klee", "Hiragino Sans", sans-serif;
}

.page-meta {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  font-size: 10pt;
  margin-bottom: 1.5mm;
}
.page-meta .meta-serial { color: #555; font-size: 8.5pt; font-family: monospace; }
.page-meta .meta-name { margin-left: auto; }
.page-meta .name-line {
  display: inline-block;
  width: 45mm;
  border-bottom: 1px solid #000;
  margin-left: 2mm;
}

.page-title {
  font-size: 16.5pt;
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
  border-bottom: 2px solid #000;
  padding-bottom: 1.2mm;
  margin-bottom: 1.8mm;
}

.page-instruction {
  font-size: 9.2pt;
  line-height: 1.35;
  color: #222;
  margin-bottom: 2.5mm;
}

.problems {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  row-gap: 4mm;
}

.item {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  padding-bottom: 2mm;
  border-bottom: 1px dashed #ccc;
}
.item:last-child { border-bottom: none; }

.item-body {
  display: flex;
  align-items: flex-start;
  gap: 0.55em;
}

.qno {
  flex: none;
  font-weight: bold;
  line-height: 1.6;
}

.expr {
  flex: 1;
  min-width: 0;
  line-height: 1.62;
  white-space: normal;
}

/* 広大な自由作図・思考スペース */
.thinking-workspace {
  flex: 1;
  min-height: 18mm;
  margin-top: 2mm;
  position: relative;
}
.workspace-watermark {
  position: absolute;
  right: 1mm;
  bottom: 0.5mm;
  font-size: 7.5pt;
  color: #c4c9d0;
  pointer-events: none;
}

.prob-svg-wrap {
  margin-top: 2.5mm;
  max-width: 78mm;
}

/* 解答・構造解説ページの装飾 */
.ans-main {
  font-size: 1.06em;
  margin-bottom: 1.5mm;
}
.ans-diagram {
  margin: 1.5mm 0;
  max-width: 125mm;
}
.ans-steps {
  font-size: 0.86em;
  line-height: 1.48;
  color: #111;
  margin-top: 1mm;
}
.ans-bridge {
  font-size: 0.78em;
  line-height: 1.42;
  color: #334155;
  background: #f8fafc;
  border-left: 3px solid #3b82f6;
  padding: 1.2mm 2.5mm;
  margin-top: 1.5mm;
}

/* 数式組版パーツ */
.m-frac {
  display: inline-flex;
  flex-direction: column;
  align-items: stretch;
  text-align: center;
  vertical-align: middle;
  line-height: 1.1;
  font-size: 0.9em;
  margin: 0 0.12em;
}
.m-frac .m-n { border-bottom: 1.3px solid #000; padding: 0 0.22em; }
.m-frac .m-d { padding: 0 0.22em; }
.m-mixed { display: inline-flex; align-items: center; gap: 0.1em; vertical-align: middle; }
.m-sqrt { display: inline-flex; align-items: baseline; }
.m-sqrt .m-radicand { border-top: 1.2px solid #000; padding: 0 0.15em; }

@media print {
  body { background: #fff; }
  #sidebar { display: none !important; }
  #app { display: block; height: auto; }
  #preview { padding: 0; overflow: visible; }
  .workspace-watermark { color: #d0d5dc; }

  .sheet {
    width: auto;
    height: auto;
    padding: 0;
    margin: 0;
    box-shadow: none;
    overflow: visible;
    break-after: page;
    page-break-after: always;
  }
  .sheet:last-child { break-after: auto; page-break-after: auto; }
}

.ans-self-check {
  font-size: 0.75em;
  color: #475569;
  margin-top: 1.5mm;
  padding-top: 1mm;
  border-top: 1px dotted #cbd5e1;
}
.remedy-tree-box {
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 6px;
  padding: 8px;
  margin-top: 6px;
}
.remedy-cp-item {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  padding: 4px 0;
  border-bottom: 1px dashed #f3e8d2;
  font-size: 12px;
  line-height: 1.35;
}
.remedy-cp-item:last-child { border-bottom: none; }
.remedy-layer-tag {
  display: inline-block;
  font-size: 10px;
  background: #e0e7ff;
  color: #3730a3;
  padding: 1px 5px;
  border-radius: 4px;
  margin-left: 4px;
}

/* 枠からはみ出さないHTML+SVGハイブリッド解説カード */
.ans-diagram { max-width: 100% !important; }
.vis-card {
  background: #f8fafc;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  padding: 5px 8px;
  margin: 2px 0;
  max-width: 100%;
}
.vis-caption {
  font-size: 0.84em;
  color: #1e293b;
  margin-bottom: 3px;
  line-height: 1.4;
}
.vis-legend {
  font-size: 0.83em;
  color: #1e293b;
  line-height: 1.54;
  margin-top: 3px;
  display: block;
  line-break: strict;
  word-break: normal;
  overflow-wrap: anywhere;
}
.vis-legend > span {
  margin-right: 10px;
}
.leg-blue { color: #1d4ed8; font-weight: bold; }
.leg-amber { color: #b45309; font-weight: bold; }
.leg-red { color: #b91c1c; font-weight: bold; }
.elim-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 3px 8px;
  font-size: 0.85em;
  align-items: center;
  margin: 3px 0;
}
.pill-blue {
  display: inline-block;
  background: #dbeafe;
  border: 1px solid #93c5fd;
  border-radius: 4px;
  padding: 0 5px;
  color: #1e3a8a;
}
.pill-amber {
  display: inline-block;
  background: #fef3c7;
  border: 1px solid #fcd34d;
  border-radius: 4px;
  padding: 0 5px;
  color: #78350f;
}

.smart-recommend-box {
  background: #f0f6ff;
  border: 1px solid #bfdbfe;
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 10px;
}
.smart-rec-head {
  font-size: 12px;
  font-weight: bold;
  color: #1e40af;
  margin-bottom: 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.smart-rec-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 5px;
}
.smart-rec-grid button {
  font-size: 11.5px;
  padding: 5px 3px;
  font-weight: bold;
  text-align: center;
}

/* 子ども4人の切り替えタブバー */
.learner-bar {
  background: #f8fafc;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  padding: 7px 8px;
  margin-bottom: 10px;
}
.learner-bar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11.5px;
  font-weight: bold;
  color: #334155;
  margin-bottom: 5px;
}
.learner-tabs {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 4px;
}
.learner-tabs button {
  font-size: 12px;
  padding: 5px 2px;
  border-radius: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.learner-tabs button.active-learner {
  background: #0f766e;
  border-color: #0f766e;
  color: #fff;
  font-weight: bold;
}
.learner-edit-panel {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px dashed #cbd5e1;
  font-size: 11.5px;
}
.learner-edit-row {
  display: grid;
  grid-template-columns: 38px 1fr auto;
  gap: 5px;
  align-items: center;
  margin-bottom: 4px;
}
