/* ===========================
   リセット・基本
   =========================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       #eef2ff;
  --card:     #ffffff;
  --primary:  #1d4ed8;
  --primary2: #3b82f6;
  --accent:   #06b6d4;
  --red:      #dc2626;
  --red-bg:   #fff1f2;
  --text:     #0f172a;
  --muted:    #64748b;
  --border:   #e2e8f0;
  --radius-s: 10px;
  --radius-m: 16px;
  --radius-l: 24px;
  --shadow-s: 0 2px 8px rgba(30,64,175,.10);
  --shadow-m: 0 6px 24px rgba(30,64,175,.12);
}

html { font-size: 16px; }

body {
  font-family: "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}

/* ===========================
   スクリーン切り替え
   =========================== */
.screen { display: none; }
.screen.active { display: flex; flex-direction: column; min-height: 100dvh; }

/* ===========================
   ヘッダー
   =========================== */
.app-header {
  background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 100%);
  color: #fff;
  padding: 14px 20px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.app-header .logo { font-size: 24px; }
.app-header h1   { font-size: 16px; font-weight: 800; letter-spacing: .3px; }

/* ===========================
   アップロード画面
   =========================== */
#screen-upload .content {
  flex: 1;
  padding: 20px 16px 36px;
  max-width: 540px;
  margin: 0 auto;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field-label {
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  margin-bottom: 4px;
  display: block;
}

.text-input {
  width: 100%;
  padding: 13px 16px;
  border: 2px solid var(--border);
  border-radius: var(--radius-m);
  font-size: 15px;
  font-family: inherit;
  background: var(--card);
  color: var(--text);
  outline: none;
  transition: border-color .2s, box-shadow .2s;
}
.text-input:focus {
  border-color: var(--primary2);
  box-shadow: 0 0 0 3px rgba(59,130,246,.15);
}

/* ドロップゾーン */
.drop-zone {
  border: 2.5px dashed #bfdbfe;
  border-radius: var(--radius-l);
  background: var(--card);
  padding: 28px 20px 22px;
  text-align: center;
  cursor: pointer;
  transition: border-color .2s, background .2s, transform .15s;
  user-select: none;
}
.drop-zone:hover, .drop-zone.drag-over {
  border-color: var(--primary2);
  background: #eff6ff;
  transform: scale(1.01);
}

.drop-zone .dz-icon  { font-size: 44px; line-height: 1; margin-bottom: 10px; }
.drop-zone .dz-main  { font-size: 15px; font-weight: 800; color: var(--primary); margin-bottom: 4px; }
.drop-zone .dz-sub   { font-size: 12px; color: var(--muted); line-height: 1.6; }

.camera-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 14px;
  padding: 10px 22px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary2) 100%);
  color: #fff;
  border: none;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 800;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 3px 10px rgba(37,99,235,.35);
  transition: opacity .2s, transform .1s;
}
.camera-btn:active { transform: scale(.96); opacity: .9; }

/* 画像グリッド */
.image-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.image-thumb {
  position: relative;
  aspect-ratio: 3/4;
  border-radius: var(--radius-s);
  overflow: hidden;
  background: #dbeafe;
  box-shadow: var(--shadow-s);
}
.image-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.image-thumb .remove-btn {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 24px;
  height: 24px;
  background: rgba(15,23,42,.70);
  backdrop-filter: blur(4px);
  color: #fff;
  border: none;
  border-radius: 50%;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
}

/* 枚数バッジ */
#img-count {
  font-size: 12px;
  font-weight: 700;
  color: var(--primary2);
  text-align: right;
}

/* ヒント */
.hint-box {
  background: var(--card);
  border-radius: var(--radius-m);
  border: none;
  padding: 14px 16px;
  font-size: 12px;
  line-height: 1.85;
  color: var(--muted);
  box-shadow: var(--shadow-s);
}
.hint-box strong { color: var(--text); }

/* 生成ボタン */
.generate-btn {
  width: 100%;
  padding: 17px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary2) 100%);
  color: #fff;
  border: none;
  border-radius: var(--radius-l);
  font-size: 16px;
  font-weight: 800;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(37,99,235,.40);
  transition: opacity .2s, transform .1s;
  letter-spacing: .3px;
}
.generate-btn:disabled { opacity: .35; cursor: not-allowed; box-shadow: none; }
.generate-btn:not(:disabled):active { transform: scale(.98); }

/* ===========================
   ローディング画面
   =========================== */
#screen-loading {
  justify-content: center;
  align-items: center;
  gap: 28px;
  background: linear-gradient(160deg, #1e3a8a 0%, #1d4ed8 60%, #0891b2 100%);
  color: #fff;
}

.spinner {
  width: 60px;
  height: 60px;
  border: 5px solid rgba(255,255,255,.25);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-title { font-size: 20px; font-weight: 800; }
.loading-sub   { font-size: 13px; color: rgba(255,255,255,.70); text-align: center; line-height: 1.7; }

/* ===========================
   結果画面
   =========================== */
#screen-result { background: var(--bg); }

.result-controls {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 10px 14px;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: var(--shadow-s);
}

.tab-group {
  display: flex;
  background: var(--bg);
  border-radius: var(--radius-s);
  padding: 3px;
  flex: 1;
  min-width: 200px;
}

.tab-btn {
  flex: 1;
  padding: 8px 4px;
  border: none;
  background: transparent;
  font-size: 12px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  color: var(--muted);
  border-radius: 7px;
  transition: background .2s, color .2s, box-shadow .2s;
}
.tab-btn.active {
  background: var(--card);
  color: var(--primary);
  box-shadow: var(--shadow-s);
}

.print-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 9px 14px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary2) 100%);
  color: #fff;
  border: none;
  border-radius: var(--radius-s);
  font-size: 12px;
  font-weight: 800;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(37,99,235,.30);
}

.back-btn {
  padding: 9px 12px;
  background: transparent;
  color: var(--muted);
  border: 2px solid var(--border);
  border-radius: var(--radius-s);
  font-size: 12px;
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  transition: border-color .2s, color .2s;
}
.back-btn:hover { border-color: var(--primary2); color: var(--primary); }

.worksheet-wrapper { padding: 20px 0; overflow-x: auto; }

/* ===========================
   A4 シート
   =========================== */
.sheet {
  width: 210mm;
  min-height: 297mm;
  background: #fff;
  margin: 0 auto 24px;
  padding: 18mm 20mm;
  box-shadow: var(--shadow-m);
  display: flex;
  flex-direction: column;
  page-break-after: always;
  border-radius: 4px;
}

/* シートヘッダー */
.sheet-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  border-bottom: 3px solid var(--primary);
  padding-bottom: 8px;
  margin-bottom: 16px;
}
.sheet-header .s-title {
  font-size: 21px;
  font-weight: 900;
  letter-spacing: 2px;
  color: var(--primary);
}
.sheet-header .s-meta {
  font-size: 11px;
  text-align: right;
  line-height: 1.8;
  color: var(--muted);
}

/* 回答シートは赤 */
.answer-sheet .sheet-header { border-bottom-color: var(--red); }
.answer-sheet .sheet-header .s-title { color: var(--red); }

/* 教科ブロック */
.subject-block { margin-bottom: 20px; }

.subject-title {
  font-size: 13px;
  font-weight: 800;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary2) 100%);
  color: #fff;
  padding: 5px 14px;
  border-radius: 6px 6px 0 0;
  display: inline-block;
}
.answer-sheet .subject-title {
  background: linear-gradient(135deg, #b91c1c 0%, var(--red) 100%);
}

.subject-body {
  border: 2px solid #bfdbfe;
  border-radius: 0 6px 6px 6px;
  padding: 12px 14px;
  background: #f8faff;
}
.answer-sheet .subject-body { border-color: #fecaca; background: #fff8f8; }

/* 問題 */
.problem { margin-bottom: 16px; }
.problem:last-child { margin-bottom: 0; }

.problem-header {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 5px;
}

.problem-num {
  background: #dbeafe;
  border-radius: 50%;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 800;
  flex-shrink: 0;
  margin-top: 2px;
  color: var(--primary);
}
.answer-sheet .problem-num { background: #fee2e2; color: var(--red); }

.problem-text { font-size: 13px; line-height: 1.75; }

/* 計算スペース */
.work-space {
  margin: 4px 0 4px 30px;
  border: 1.5px dashed #bfdbfe;
  border-radius: var(--radius-s);
  min-height: 44px;
  background: #f0f9ff;
  position: relative;
  font-size: 12px;
  color: var(--red);
  line-height: 1.7;
  padding: 18px 10px 6px;
}
.work-space::before {
  content: "計算スペース";
  position: absolute;
  top: 4px;
  left: 8px;
  font-size: 9px;
  color: #93c5fd;
  font-weight: 700;
}
.work-space.blank { color: transparent; }

/* 答え欄 */
.answer-area {
  margin: 5px 0 0 30px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.answer-area.tall { align-items: flex-start; }

.answer-label {
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  white-space: nowrap;
  flex-shrink: 0;
}

/* 空欄 */
.answer-blank {
  flex: 1;
  border-bottom: 2.5px solid #334155;
  min-height: 26px;
  max-width: 340px;
}
.answer-area.tall .answer-blank {
  min-height: 52px;
  border: 2px solid #334155;
  border-radius: var(--radius-s);
  max-width: 100%;
}

/* 記入済み（赤） */
.answer-filled {
  flex: 1;
  border-bottom: 2.5px solid var(--red);
  min-height: 26px;
  max-width: 340px;
  font-size: 13px;
  font-weight: 800;
  color: var(--red);
  padding: 2px 4px;
  line-height: 1.5;
}
.answer-area.tall .answer-filled {
  min-height: 52px;
  border: 2px solid var(--red);
  border-radius: var(--radius-s);
  max-width: 100%;
  padding: 4px 8px;
}

/* 英語書き取り */
.writing-lines { margin: 4px 0 0 30px; }
.writing-line {
  border-bottom: 1.5px solid #cbd5e1;
  height: 28px;
  position: relative;
  font-size: 13px;
  font-weight: 700;
  color: var(--red);
  line-height: 24px;
  padding-bottom: 2px;
}
.writing-line::after {
  content: "";
  position: absolute;
  bottom: 8px; left: 0; right: 0;
  border-bottom: 1px dashed #e2e8f0;
}

/* フッター */
.sheet-footer {
  margin-top: auto;
  padding-top: 10px;
  border-top: 1px solid #e2e8f0;
  font-size: 10px;
  color: #94a3b8;
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

/* エラートースト */
.error-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #dc2626;
  color: #fff;
  padding: 12px 20px;
  border-radius: var(--radius-m);
  font-size: 14px;
  font-weight: 700;
  box-shadow: 0 4px 20px rgba(0,0,0,.25);
  z-index: 999;
  max-width: 90vw;
  text-align: center;
  animation: slideUp .2s ease;
}
@keyframes slideUp {
  from { opacity: 0; transform: translateX(-50%) translateY(10px); }
}

/* ===========================
   印刷
   =========================== */
@media print {
  body { background: #fff; }
  #screen-upload, #screen-loading { display: none !important; }
  #screen-result { display: block !important; min-height: auto; }
  .result-controls, .app-header { display: none !important; }
  .worksheet-wrapper { padding: 0; overflow: visible; }
  .sheet {
    width: 100%; min-height: auto;
    padding: 15mm 18mm;
    box-shadow: none; margin: 0;
    page-break-after: always;
    border-radius: 0;
  }
  .question-sheet, .answer-sheet { display: flex !important; }
}

/* スマホ縮小表示 */
@media (max-width: 240mm) {
  .worksheet-wrapper { padding: 10px 0; }
  .sheet { transform: scale(0.88); transform-origin: top center; margin-bottom: -20px; }
}
