:root {
  --bg: #f4f2ec;
  --surface: #ffffff;
  --surface-2: #f8f7f3;
  --text: #22251f;
  --muted: #61665c;
  --line: #d9d6cc;
  --accent: #2f6f5e;
  --accent-ink: #ffffff;
  --accent-soft: #e2efe9;
  --warn: #8a5a00;
  --error: #b3261e;
  --answer: #d0342c;
  --font: 'BIZ UDPGothic', 'Hiragino Sans', 'Hiragino Kaku Gothic ProN', 'Yu Gothic', Meiryo, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #1b1d1a;
    --surface: #262923;
    --surface-2: #2e322b;
    --text: #e9e8e2;
    --muted: #a6aa9f;
    --line: #42463d;
    --accent: #7cc4ad;
    --accent-ink: #10221c;
    --accent-soft: #2b3c35;
    --warn: #e5b45c;
    --error: #f2948c;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}

.visually-hidden {
  position: absolute !important; width: 1px !important; height: 1px !important;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

/* ------------------------------------------------------------------ */
/* 画面レイアウト                                                     */
/* ------------------------------------------------------------------ */

.app-header { padding: 20px 16px 4px; max-width: 1400px; margin: 0 auto; }
.app-header h1 { margin: 0; font-size: 1.5rem; letter-spacing: .02em; }
.breadcrumb { display: flex; flex-wrap: wrap; gap: 6px; font-size: .85rem; color: var(--muted); margin-bottom: 4px; }
.breadcrumb a { color: var(--accent); }
.app-header p { margin: 4px 0 0; color: var(--muted); font-size: .92rem; }

.app {
  display: grid;
  grid-template-columns: minmax(300px, 380px) minmax(0, 1fr);
  gap: 20px;
  padding: 16px;
  max-width: 1400px;
  margin: 0 auto;
  align-items: start;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 4px 18px 18px;
  position: sticky;
  top: 16px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
}

.group { padding: 14px 0; border-bottom: 1px solid var(--line); }
.group h2 { margin: 0 0 10px; font-size: .8rem; letter-spacing: .12em; color: var(--muted); font-weight: 700; }

fieldset { border: 0; margin: 0 0 12px; padding: 0; min-width: 0; }
legend { padding: 0; margin-bottom: 6px; font-size: .9rem; font-weight: 700; }

.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row .field { flex: 1 1 90px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field label { font-size: .9rem; font-weight: 700; }

input[type='number'], input[type='text'], select {
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  width: 100%;
  min-height: 42px;
}
input:focus-visible, select:focus-visible, button:focus-visible, .chips input:focus-visible + label {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* トグルチップ（ラジオ・チェックボックス） */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips legend { width: 100%; }
.chips input, .dans input { position: absolute; opacity: 0; pointer-events: none; }
.chips label, .dans label {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 38px; padding: 4px 14px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface-2);
  cursor: pointer; user-select: none; font-size: .95rem;
}
.chips input:checked + label, .dans input:checked + label {
  background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 700;
}
.chips-ops label { padding: 6px 16px; font-size: 1rem; }

.dans { display: grid; grid-template-columns: repeat(9, 1fr); gap: 4px; }
.dans legend { grid-column: 1 / -1; }
.dans label { padding: 0; border-radius: 8px; min-height: 36px; }
.dans input:focus-visible + label { outline: 3px solid var(--accent); outline-offset: 2px; }

.op-panel {
  margin-top: 12px; padding: 12px 12px 2px;
  background: var(--surface-2); border-radius: 10px; border: 1px solid var(--line);
}
.op-panel select, .op-panel input[type='text'] { background: var(--surface); }
.op-panel .chips label, .op-panel .dans label { background: var(--surface); }
.op-panel .chips input:checked + label, .op-panel .dans input:checked + label { background: var(--accent); }

.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 2px 8px; }
.checks legend { grid-column: 1 / -1; }
.check { display: flex; align-items: center; gap: 6px; min-height: 34px; cursor: pointer; font-size: .95rem; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }
.op-panel > .check { margin-bottom: 10px; }

.hint { color: var(--muted); font-size: .82rem; margin: 6px 0 0; }

.link-btn {
  font: inherit; font-size: .8rem; font-weight: 400;
  background: none; border: 0; padding: 2px 4px; margin-left: 4px;
  color: var(--accent); text-decoration: underline; cursor: pointer;
}

.actions { display: grid; gap: 8px; padding-top: 16px; }
.btn {
  font: inherit; font-size: 1rem; font-weight: 700;
  min-height: 46px; padding: 8px 16px;
  border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface-2); color: var(--text);
  cursor: pointer; position: relative;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-size: 1.05rem; }
.btn-quiet { background: transparent; font-weight: 400; font-size: .92rem; min-height: 40px; }
.btn[data-done]::after {
  content: attr(data-done);
  position: absolute; inset: 0; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink); border-radius: inherit;
}

.preview { min-width: 0; }
.status { margin: 0 0 10px; font-size: .92rem; color: var(--muted); min-height: 1.6em; }
.status.is-warning { color: var(--warn); }
.status.is-error { color: var(--error); font-weight: 700; }

.sheets { zoom: var(--zoom, 1); display: flex; flex-direction: column; align-items: center; gap: 24px; }

.app-footer { max-width: 1400px; margin: 0 auto; padding: 8px 16px 32px; color: var(--muted); font-size: .85rem; }
.app-footer p { margin: 2px 0; }
.app-footer a { color: var(--accent); }

@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .panel { position: static; max-height: none; overflow: visible; }
  .actions { grid-template-columns: 1fr 1fr; }
  .actions .btn-primary { grid-column: 1 / -1; }
}

/* ------------------------------------------------------------------ */
/* プリント本体（A4・常に白地）                                       */
/* ------------------------------------------------------------------ */

.sheet {
  width: 210mm;
  height: 297mm;
  padding: 12mm 12mm 12mm;
  background: #fff;
  color: #111;
  font-family: var(--font);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .12), 0 8px 24px rgba(0, 0, 0, .08);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  flex: none;
}

.sheet-header { min-height: 26mm; flex: none; border-bottom: .4mm solid #111; padding-bottom: 2mm; display: flex; flex-direction: column; justify-content: space-between; }
.sheet-header-top { display: flex; align-items: baseline; justify-content: space-between; gap: 4mm; }
.sheet-title { margin: 0; font-size: 6.5mm; font-weight: 700; line-height: 1.25; min-width: 0; overflow-wrap: anywhere; }
.answer-badge {
  display: inline-block; margin-left: 3mm; padding: .3mm 2.5mm;
  font-size: 4mm; vertical-align: .6mm;
  border: .35mm solid var(--answer); color: var(--answer); border-radius: 1mm;
}
.sheet-no { font-size: 3.6mm; color: #555; white-space: nowrap; }
.sheet-meta { display: flex; flex-wrap: wrap; gap: 2mm 7mm; font-size: 4.2mm; align-items: flex-end; }
.meta-item { white-space: nowrap; }
.meta-name { min-width: 62mm; border-bottom: .3mm solid #111; padding-right: 4mm; }

.sheet-probe { position: absolute; left: -9999px; top: 0; visibility: hidden; font-family: var(--font); }
.sheet-probe .problem { display: inline-flex; padding: 0; }
.problems { display: grid; flex: none; font-size: var(--font); }

.problem {
  display: flex; align-items: center; gap: .3em;
  padding: 0 1.5mm;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
/* 文字サイズの式は render.js の noFontMm() と合わせること */
.no { font-size: min(1em, max(.42em, 2.6mm)); color: #444; min-width: 1.9em; text-align: right; margin-right: .15em; }
.expr { display: inline-flex; align-items: center; gap: .18em; }
.sym { width: 1em; text-align: center; }
.num { display: inline-block; min-width: calc(var(--digits) * .6em); text-align: right; }
.num.blank {
  min-width: calc(var(--digits) * .6em + .5em);
  height: 1.25em; border: .3mm solid #111; border-radius: .08em;
  text-align: center; line-height: 1.2em;
}
.answer-slot { min-width: calc(var(--digits) * .6em + .9em); text-align: left; padding-left: .15em; }
.ans { display: inline-flex; align-items: center; gap: .2em; }
.amari { font-size: .6em; margin: 0 .2em; }
.num.given { text-align: left; }
.filled { color: var(--answer); font-weight: 700; }

/* ------------------------------------------------------------------ */
/* 印刷                                                               */
/* ------------------------------------------------------------------ */

@page { size: A4 portrait; margin: 0; }

@media print {
  html, body { background: #fff; }
  .app-header, .app-footer, .panel, .status { display: none !important; }
  .app { display: block; padding: 0; margin: 0; max-width: none; }
  .sheets { zoom: 1 !important; display: block; }
  .sheet { box-shadow: none; margin: 0; break-after: page; page-break-after: always; }
  .sheet:last-child { break-after: auto; page-break-after: auto; }
  .filled, .answer-badge { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
