/* --- Question Page (C01, C02) --- */

.question-page {
  /* iOS standalone PWA では 100dvh / innerHeight がコールドスタート直後に
     実機の可視領域より短い値を返し、下部に余白が残る（ナビゲーションで
     リフローするまで直らない）。dvh/JS実測に依存せず、実ビューポートへ
     直接ピン留めする position:fixed + inset:0 で確実に全画面を覆う。 */
  position: fixed;
  inset: 0;
  width: auto;
  max-width: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding-top: env(safe-area-inset-top, 0px);
  overflow: hidden;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  background: var(--color-bg-quiz);
}

.question-page *,
.question-page *::before,
.question-page *::after {
  -webkit-tap-highlight-color: transparent;
}

.question-page button,
.question-page [role="button"] {
  touch-action: manipulation;
}

.question-page canvas,
.question-page img,
.question-page svg {
  -webkit-user-drag: none;
}


.btn-quit {
  width: 36px;
  height: 36px;
  border-radius: 12px;
  border: none;
  background: var(--color-surface);
  color: var(--color-text-sub);
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(var(--color-text-rgb), .06);
}

/* プログレスバー＋カウンター */
.progress-bar-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 2px 0;
  flex: 1;
}
.progress-bar-track {
  flex: 1;
  height: 8px;
  border-radius: 9px;
  background: var(--color-border);
  overflow: hidden;
}
.progress-bar-fill {
  height: 100%;
  background: var(--color-primary);
  border-radius: 9px;
  transition: width 0.3s ease;
}
.progress-bar-counter {
  font-size: 0.8125rem;
  font-weight: 700;
  color: #9A978F;
  letter-spacing: 0.04em;
  white-space: nowrap;
}
.progress-bar-counter-sep {
  color: var(--color-text-mute);
}

.problem-token {
  display: inline-block;
  line-height: 1;
  font-weight: 800;
}
.token-num {
  font-size: 58px;
  color: var(--color-text);
}
.token-text {
  font-size: 32px;
  color: var(--color-text);
  white-space: nowrap;
}
.token-blank {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 64px;
  height: 64px;
  border: 4px dashed #9BE3C6;
  border-radius: 14px;
  font-size: 42px;
  color: var(--color-primary);
}

/* かず表示 (count) */
.problem-count {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.problem-count-icons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px 12px;
  max-width: 320px;
}
.problem-count-icon {
  font-size: 48px;
  line-height: 1;
}
.problem-count-prompt {
  font-size: 24px;
  font-weight: 600;
  color: var(--color-muted);
}

/* 合成分解 (group) — 全体のうち一部を囲む視覚補助（1行に最大9個まで収める） */
.problem-group {
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  margin-bottom: 16px;
}
.problem-group-ring {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 3px solid var(--color-primary);
  border-radius: 999px;
  background: var(--color-primary-soft);
  margin-right: 8px;
}
.problem-group-icon {
  font-size: 36px;
  line-height: 1;
}

/* 筆算レイアウト */
.canvas-equation-vertical {
  padding: 0 16px;
  align-items: flex-start;
  top: 45%;
  transform: translateY(-50%);
}
.problem-vertical {
  display: grid;
  align-items: center;
  justify-items: center;
  row-gap: 2px;
}
.problem-vertical-row {
  display: contents;
}
.problem-vertical-digit {
  font-size: 48px;
  font-weight: 800;
  color: var(--color-text);
  line-height: 1.1;
}
.problem-vertical-op {
  font-size: 38px;
  font-weight: 800;
  color: var(--color-coral);
}
.problem-vertical-carry {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 36px;
}
.problem-vertical-carry-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #FFEFE7;
  color: var(--color-coral);
  font-size: 22px;
  font-weight: 800;
  line-height: 1;
}
.problem-vertical-bar {
  width: 100%;
  height: 5px;
  background: var(--color-text);
  border-radius: 3px;
  margin: 4px 0 6px;
}
.problem-vertical-answer {
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 並び (sequence) レイアウト */
.canvas-equation-sequence {
  align-items: flex-end;
}
.problem-sequence {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.problem-sequence-cells {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 6px;
  flex-wrap: nowrap;
}
.problem-sequence-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex: 1 1 0;
  min-width: 0;
  position: relative;
}
.problem-sequence-label {
  font-size: 12px;
  color: #7A8693;
  line-height: 1;
  text-align: center;
}
.problem-sequence-value {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  background: var(--color-surface);
  border: 2px solid #d5dbe1;
  border-radius: 12px;
  padding: 10px 6px;
  aspect-ratio: 1;
  position: relative;
}
.problem-sequence-cell + .problem-sequence-cell .problem-sequence-value::before {
  content: "";
  position: absolute;
  left: -4px;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 0;
  height: 0;
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  border-left: 6px solid #b5bfc9;
}
.problem-sequence-cell.is-blank .problem-sequence-value {
  background: transparent;
  border-color: transparent;
  padding: 0;
}
/* 空欄のプレースホルダはセルいっぱいに広げ、数字セルと高さ・下端をそろえる
   (token-blank は既定で 64px 固定のため、幅広画面で数字セルより低くなり上にずれる) */
.problem-sequence-cell.is-blank .problem-token.token-blank {
  width: 100%;
  height: 100%;
  min-width: 0;
  box-sizing: border-box;
  border-width: 2px;
  font-size: 34px;
}
.problem-sequence-cell .problem-token.token-num {
  font-size: 34px;
}
.problem-sequence-value .answer-box {
  font-size: 34px;
  width: auto;
  height: auto;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: none;
  background: none;
}
.problem-sequence-value .answer-box-value,
.problem-sequence-value .answer-box-placeholder {
  font-size: 34px;
  line-height: 1;
  height: auto;
}

/* 答え入力ボックス (数式の中) */
.answer-box {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: max(94px, calc(var(--answer-digits, 1) * 48px + 28px));
  height: 80px;
  padding: 0 14px;
  border: 4px dashed #9BE3C6;
  border-radius: 16px;
  background: rgba(243, 251, 247, .82);
}
.answer-box-vertical {
  box-sizing: border-box;
  width: calc(var(--answer-digits, 1) * var(--vt-digit-col, 72px));
  min-width: 0;
  padding: 0 8px;
}
.answer-box-value {
  font-size: 54px;
  font-weight: 800;
  color: var(--color-primary-dark);
  line-height: 1;
}
.answer-box-placeholder {
  font-size: 54px;
  font-weight: 800;
  color: #CDE9DC;
  line-height: 1;
}

/* --- Number Input --- */


.answer-panel-slot {
  position: relative;
  width: 100%;
  flex-shrink: 0;
}

.answer-input-shell {
  width: 100%;
}

.answer-input-shell.is-hidden {
  visibility: hidden;
  pointer-events: none;
}

.answer-input-shell > .keypad,
.answer-input-shell > .remainder-input {
  flex-shrink: 0;
  margin-top: auto;
  padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
}

.answer-panel-slot.has-feedback {
  min-height: 168px;
}

.answer-panel-slot .inline-feedback {
  top: 0;
  bottom: auto;
}

.question-page > .keypad,
.question-page > .remainder-input {
  flex-shrink: 0;
  margin-top: auto;
  padding-bottom: max(12px, env(safe-area-inset-bottom, 12px));
}

/* --- Keypad --- */

.keypad {
  display: flex;
  flex-direction: column;
  gap: 7px;
  width: 100%;
  padding: 0;
  margin-top: 0;
  flex-shrink: 0;
}

.keypad-numbers {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}


.keypad-btn {
  min-height: 50px;
  font-size: 1.625rem;
  font-weight: 800;
  color: var(--color-text);
  border: none;
  border-radius: 18px;
  background: var(--color-surface);
  box-shadow: 0 2px 0 var(--key-shadow);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  transition: transform 0.06s, box-shadow 0.06s;
}

.keypad-btn:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: 0 0 0 var(--key-shadow);
}

.keypad-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.keypad-fn {
  font-size: 1.375rem;
  color: var(--color-coral-dark);
  background: var(--color-coral-soft);
  box-shadow: 0 2px 0 #F3C5B2;
}

.keypad-fn:active:not(:disabled) {
  box-shadow: 0 0 0 #F2C3AF;
}

/* --- Remainder Input --- */

.remainder-input {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.remainder-fields {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  font-size: 1.125rem;
  font-weight: 600;
}

.remainder-box {
  min-width: 64px;
  min-height: 48px;
  padding: 8px 12px;
  font-size: 1.5rem;
  font-weight: 700;
  text-align: center;
  border: 4px dashed #9BE3C6;
  border-radius: 16px;
  background: var(--color-grid-bg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.remainder-box.active {
  border-style: solid;
  border-color: var(--color-primary);
  outline: 4px solid var(--color-primary-soft);
  outline-offset: -2px;
}

/* --- Inline Feedback (カード下インライン) --- */

.inline-feedback {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  border-radius: 16px;
  padding: 14px 20px;
  text-align: center;
  animation: rise 0.3s ease-out;
  z-index: 10;
}

/* オーバーレイ内では static に戻してボトムシート形式で下からスライドアップ */
.session-overlay-feedback .inline-feedback {
  position: relative;
  bottom: auto;
  left: auto;
  right: auto;
  border-radius: 28px 28px 0 0;
  padding: 22px 24px max(24px, calc(env(safe-area-inset-bottom) + 14px));
  box-shadow: 0 -10px 30px rgba(226, 103, 63, 0.18);
  animation: sheet-rise 0.34s cubic-bezier(0.33, 1, 0.68, 1);
}

.inline-feedback-correct {
  background: var(--color-primary-soft);
}

.inline-feedback-wrong {
  background: var(--color-coral-soft);
}

.inline-feedback-already {
  background: #F3F1EC;
}

.inline-feedback-text {
  font-size: 1.5rem;
  font-weight: 800;
}

.inline-feedback-correct .inline-feedback-text {
  color: var(--color-primary-dark);
}

.inline-feedback-wrong .inline-feedback-text {
  color: var(--color-coral-dark);
}

.inline-feedback-already .inline-feedback-text {
  color: var(--color-text-sub);
  font-size: 1.25rem;
}

.inline-feedback-wrong-detail {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.inline-feedback-answer {
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--color-text);
}

.inline-feedback-answer-value {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 2.4rem;
  margin-left: 6px;
  padding: 2px 12px;
  border-radius: 12px;
  background: var(--color-surface);
  color: var(--color-coral-dark);
  font-size: 1.6rem;
  font-weight: 800;
  box-shadow: 0 2px 0 rgba(226, 103, 63, 0.18);
}

.inline-feedback .btn-next {
  margin-top: 4px;
}


/* インライン警告 (空欄/書式不正) */
.feedback-warn {
  width: 100%;
  max-width: var(--control-width-sm);
  margin: 12px auto 0;
  background: var(--color-warn-bg);
  color: var(--color-warn);
  font-weight: 700;
  padding: 14px 18px;
  border-radius: var(--radius-sm);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
}

/* --- Result Pages --- */

.result-page {
  max-width: var(--max-width-child);
  margin: -16px auto 0;
  min-height: 100dvh;
  text-align: center;
  padding: 56px 16px 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  background: linear-gradient(170deg, #FFE7A8, #FFD166);
  border-radius: 24px;
}

.result-title {
  font-size: 3.5rem;
  font-weight: 800;
  color: #7A5A00;
  animation: rise 0.7s ease-out;
}

.result-page .result-big {
  background: var(--color-surface);
  border-radius: 30px;
  padding: 24px 32px;
  box-shadow: 0 6px 20px rgba(122, 90, 0, .12);
}

.result-subtitle {
  font-size: 1.125rem;
  font-weight: 800;
  color: #9A6A00;
  margin-top: -4px;
  animation: rise 0.75s ease-out;
}

.score-card {
  align-self: stretch;
  background: var(--color-surface);
  border-radius: 30px;
  padding: 24px 28px;
  box-shadow: 0 8px 22px rgba(122, 90, 0, .14);
  text-align: center;
  animation: rise 0.85s ease-out;
}
.score-label {
  font-size: 0.875rem;
  color: #B58A22;
  font-weight: 800;
  letter-spacing: 0.1em;
  margin-bottom: 6px;
}
.score-line {
  font-size: 1.375rem;
  font-weight: 800;
  color: var(--color-text);
}
.score-num {
  font-size: 3rem;
  color: var(--color-primary);
  margin: 0 0.15em;
}

.result-page .btn-primary {
  min-height: 84px;
}

/* 装飾パーティクル */
.result-particle {
  position: absolute;
  border-radius: 4px;
  animation: rise 1s ease-out both;
}
.particle-1 {
  width: 18px; height: 18px;
  background: var(--color-coral);
  top: 12%; left: 8%;
  border-radius: 50%;
  transform: rotate(15deg);
}
.particle-2 {
  width: 14px; height: 14px;
  background: var(--color-primary);
  top: 8%; right: 12%;
  transform: rotate(-20deg);
}
.particle-3 {
  width: 16px; height: 16px;
  background: var(--color-sunny);
  bottom: 18%; left: 6%;
  border-radius: 50%;
  transform: rotate(40deg);
}
.particle-4 {
  width: 12px; height: 12px;
  background: #5BB4E0;
  bottom: 22%; right: 10%;
  transform: rotate(-10deg);
}


/* --- Responsive --- */

@media (max-width: 480px) {
  .token-num { font-size: 42px; }
  .problem-count-icon { font-size: 38px; }
  .problem-count-icons { max-width: 260px; }
  .problem-count-prompt { font-size: 20px; }
  .problem-group { gap: 4px; }
  .problem-group-ring { padding: 5px 8px; gap: 4px; margin-right: 6px; }
  .problem-group-icon { font-size: 26px; }
  .answer-box { min-width: max(72px, calc(var(--answer-digits, 1) * 40px + 20px)); height: 62px; padding: 0 10px; }
  .answer-box-value, .answer-box-placeholder { font-size: 40px; }
  .keypad-btn {
    min-height: 42px;
    font-size: 1.3125rem;
    border-radius: 16px;
  }
  .keypad-fn { font-size: 1rem; }
  .keypad { gap: 5px; }
  .keypad-numbers { gap: 6px; }
  .digit-cell { width: 72px; height: 88px; }
  .inline-feedback { padding: 10px 16px; }
  .inline-feedback-text { font-size: 1.25rem; }
  .inline-feedback-answer { font-size: 1.125rem; }
  .result-title { font-size: 2.75rem; }

  .canvas-equation-vertical { padding: 16px 12px 0; }
  .problem-vertical { row-gap: 1px; --vt-op-col: 42px; --vt-digit-col: 54px; }
  .problem-vertical-digit { font-size: 36px; }
  .problem-vertical-op { font-size: 28px; }
  .problem-vertical-carry { height: 26px; }
  .problem-vertical-carry-badge { width: 26px; height: 26px; font-size: 16px; }
  .problem-vertical-bar { margin: 2px 0 4px; }
  .digit-cell { width: 64px; height: 78px; }
  .digit-cell-value { font-size: 30px; }
}

@media (min-width: 481px) and (max-width: 820px) and (max-height: 1100px) {
  .question-page:not(.question-page-feed) {
    padding-top: 12px;
  }


  .btn-quit {
    width: 32px;
    height: 32px;
    border-radius: 11px;
  }

  .progress-bar-row {
    padding-top: 2px;
  }

  .keypad {
    gap: 6px;
  }

  .keypad-numbers {
    gap: 7px;
  }

  .keypad-btn {
    min-height: 46px;
    border-radius: 17px;
  }

  .keypad-fn {
    font-size: 1.125rem;
  }
}

@media (max-height: 700px) {
  .problem-vertical { --vt-op-col: 36px; --vt-digit-col: 46px; }
  .problem-vertical-digit { font-size: 32px; }
  .problem-vertical-op { font-size: 24px; }
  .problem-vertical-carry { height: 22px; }
  .problem-vertical-carry-badge { width: 22px; height: 22px; font-size: 14px; }
  .problem-vertical-bar { height: 4px; margin: 2px 0 3px; }
  .canvas-equation-vertical { padding: 10px 10px 0; }
  .digit-cell { width: 58px; height: 72px; }
  .digit-cell-value { font-size: 28px; }
  .digit-cells { gap: 8px; }
  .token-num { font-size: 38px; }
  .answer-box { min-width: max(64px, calc(var(--answer-digits, 1) * 36px + 18px)); height: 56px; }
  .answer-box-value, .answer-box-placeholder { font-size: 36px; }
}


/* --- Digit Cells (per-digit input) --- */

.digit-cells {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin: 4px auto 0;
}

.digit-cell {
  width: 80px;
  height: 96px;
  border: 3px solid #D3D1C7;
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-surface);
  position: relative;
  transition: border-color 0.15s, box-shadow 0.15s;
  overflow: hidden;
}

.digit-cell.focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 4px rgba(46, 111, 176, 0.15);
}

.digit-cell.filled {
  border-color: var(--color-primary);
}

.digit-cell-value {
  font-size: 36px;
  font-weight: 800;
  color: var(--color-text);
  pointer-events: none;
  position: relative;
  z-index: 2;
}

.digit-cell-cursor {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  width: 24px;
  height: 3px;
  background: var(--color-primary);
  border-radius: 2px;
  animation: digit-blink 1s step-end infinite;
  pointer-events: none;
}

@keyframes digit-blink {
  50% { opacity: 0; }
}

.digit-cell-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  z-index: 3;
}

@media (min-width: 481px) and (max-width: 820px) and (max-height: 1100px) {
  .digit-cell {
    width: 72px;
    height: 86px;
    border-radius: 15px;
  }

  .digit-cell-value {
    font-size: 32px;
  }

  .digit-cells {
    gap: 8px;
    margin-top: 2px;
  }
}
