:root {
  --theme: #4dabf7; --bg: #f8f9fa;
  --color-a: #74c0fc; --color-b: #ff922b; --color-c: #ff8787;
  --success: #40c057; --warning: #fcc419; --danger: #fa5252;
  --text: #343a40; --border: #ced4da;
}

* { box-sizing: border-box; }
body { font-family: 'Hiragino Maru Gothic ProN', 'Rounded M+ 1c', sans-serif; background-color: var(--bg); margin: 0; padding: 10px; display: flex; justify-content: center; color: var(--text); }
.app-container { background: white; max-width: 900px; width: 100%; border-radius: 12px; box-shadow: 0 8px 24px rgba(0,0,0,0.1); overflow: hidden; }

/* Header & Mission */
header { background: #e7f5ff; padding: 15px; text-align: center; border-bottom: 3px solid var(--theme); }
h1 { margin: 0 0 10px; color: #1864ab; font-size: 1.5rem; }
.notebook-prompt { display: inline-block; background: white; border: 2px solid #74c0fc; color: #1864ab; padding: 5px 15px; border-radius: 20px; font-weight: bold; }
.mission-bar { background: #f1f3f5; padding: 10px 15px; border-bottom: 2px dashed var(--border); display: flex; align-items: center; gap: 10px; flex-wrap: wrap;}
.mission-label { font-weight: bold; }

/* Buttons */
.btn { padding: 8px 12px; border: none; border-radius: 6px; cursor: pointer; font-weight: bold; font-size: 0.95rem; transition: all 0.1s; }
.btn:active { transform: translateY(2px); }
.btn-outline { background: white; border: 2px solid #adb5bd; color: var(--text); }
.btn-outline.active-prob { border-color: var(--theme); background: #e7f5ff; color: #1864ab; }
.btn-gray { background: #dee2e6; color: var(--text); }
.btn-gray.active-pbtn { background: var(--theme); color: white; border-color: var(--theme); }
.btn-yellow { background: var(--warning); color: var(--text); }

/* Tabs & Container */
.visual-container { padding: 20px; min-height: 450px; background: white; }
.visual-content { display: none; animation: fadeIn 0.3s; }
.visual-content.active { display: block; }
.vis-header { display: flex; justify-content: space-between; align-items: center; font-weight: bold; margin-bottom: 20px; color: #495057; padding: 10px; background: #f8f9fa; border-radius: 8px; border-left: 5px solid var(--warning); }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* Character */
.char-container { display: flex; align-items: center; gap: 10px; margin-bottom: 25px; position: relative; }
.char-icon { font-size: 2.2rem; background: #e9ecef; border-radius: 50%; width: 50px; height: 50px; display: flex; justify-content: center; align-items: center; border: 2px solid #adb5bd; }
.char-name { position: absolute; bottom: -18px; left: 10px; font-size: 0.8rem; font-weight: bold; color: #868e96; }
.speech-bubble { position: relative; background: #fff4e6; border: 2px solid #ffa94d; border-radius: 12px; padding: 10px 15px; font-weight: bold; color: #d9480f; flex: 1; }
.speech-bubble::before { content: ''; position: absolute; top: 15px; left: -12px; border-width: 10px 12px 10px 0; border-style: solid; border-color: transparent #ffa94d transparent transparent; }
.speech-bubble::after { content: ''; position: absolute; top: 17px; left: -9px; border-width: 8px 10px 8px 0; border-style: solid; border-color: transparent #fff4e6 transparent transparent; }

/* --- 筆算グリッド共通デザイン --- */
.math-grid { display: flex; flex-direction: column; font-size: 1.8rem; font-weight: bold; font-family: 'Courier New', monospace; width: max-content; }
.mg-row { display: flex; height: 45px; align-items: center; position: relative; }
.border-bottom { border-bottom: 3px solid #343a40; margin-bottom: 5px; padding-bottom: 5px; }
.mg-cell { width: 40px; height: 40px; display: flex; justify-content: center; align-items: center; position: relative; }
.mg-cell.val { color: #343a40; }
.num-box { width: 35px; height: 38px; text-align: center; border: 2px solid #adb5bd; border-radius: 6px; font-weight: bold; font-size: 1.6rem; color: #1864ab; background: white; transition: all 0.2s; padding: 0; }
.num-box:focus { outline: none; border-color: var(--theme); background: #e7f5ff; }
.num-box.correct { border-color: var(--success); background: #ebfbee; color: #2b8a3e; }
.side-note { position: absolute; left: 100%; margin-left: 15px; font-size: 1.2rem; color: #868e96; white-space: nowrap; font-family: 'Hiragino Maru Gothic ProN', sans-serif; font-weight: normal; }

/* 1. ひっ算のしくみ */
.mechanism-stage { display: flex; align-items: center; justify-content: center; gap: 30px; background: #f8f9fa; padding: 20px; border-radius: 12px; border: 2px dashed var(--border); }
.mech-left { display: flex; flex-direction: column; gap: 15px; }
.split-box { padding: 10px 15px; border-radius: 8px; border: 2px solid; font-size: 1.4rem; font-weight: bold; display: flex; align-items: center; gap: 10px; }
.blue-box { background: #e7f5ff; border-color: #74c0fc; color: #1864ab; }
.red-box { background: #fff0f6; border-color: #faa2c1; color: #a61e4d; }
.num-input-row { width: 70px; height: 35px; font-size: 1.3rem; text-align: center; border: 2px solid #adb5bd; border-radius: 6px; font-weight: bold; }
.num-input-row.correct { border-color: var(--success); background: #ebfbee; color: #2b8a3e; }
.mech-arrow { text-align: center; font-weight: bold; color: #ff922b; font-size: 1.1rem; }
.mech-right { background: white; padding: 15px 30px; border-radius: 12px; border: 2px solid #dee2e6; box-shadow: 0 4px 8px rgba(0,0,0,0.05); }
.red-bg { background: rgba(255, 192, 203, 0.2); border-radius: 6px; }
.blue-bg { background: rgba(173, 216, 230, 0.2); border-radius: 6px; }
.empty-zero { color: #ced4da; font-size: 1.5rem; }

/* 2. まちがいさがし */
.mistake-stage { display: flex; justify-content: center; gap: 40px; background: #f8f9fa; padding: 20px; border-radius: 12px; flex-wrap: wrap; }
.mistake-bad, .mistake-good { background: white; padding: 20px 30px; border-radius: 12px; border: 2px solid #dee2e6; display: flex; flex-direction: column; align-items: center; }
.bad-title { color: var(--danger); font-weight: bold; font-size: 1.2rem; margin-bottom: 15px; border-bottom: 2px solid var(--danger); padding-bottom: 5px; }
.good-title { color: var(--success); font-weight: bold; font-size: 1.2rem; margin-bottom: 15px; border-bottom: 2px solid var(--success); padding-bottom: 5px; }
.bad-math { font-size: 2rem; font-family: 'Courier New', monospace; font-weight: bold; color: #c92a2a; letter-spacing: 5px; line-height: 1.2; margin-bottom: 15px; }
.bad-math hr { border: 1px solid #c92a2a; margin: 5px 0; }
.bad-reason { font-size: 1rem; color: #495057; background: #fff4e6; padding: 10px; border-radius: 8px; text-align: center; border: 1px solid #ffd8a8; font-weight: bold; }

/* 3. ワークシート (おたすけモード) */
.support-toggle { background: #fff3cd; border: 2px solid #ffeeba; padding: 5px 15px; border-radius: 20px; color: #856404; cursor: pointer; display: flex; align-items: center; gap: 5px; }
.support-toggle input { width: 18px; height: 18px; cursor: pointer; }
.prob-selector { display: flex; gap: 10px; justify-content: center; margin-bottom: 20px; flex-wrap: wrap; }
.practice-stage { display: flex; justify-content: center; background: white; padding: 30px; border-radius: 12px; border: 2px solid #dee2e6; box-shadow: inset 0 0 10px rgba(0,0,0,0.02); }

/* おたすけモードON時のスタイル */
.support-on .math-grid { background-image: linear-gradient(90deg, transparent 38px, #e9ecef 38px, #e9ecef 40px); background-size: 40px 100%; }
.support-on .memo-row { display: flex; }
.support-on .help-zero { display: flex; color: #adb5bd; font-size: 1.6rem; }
.support-on .help-text { display: inline; color: #4dabf7; }

.memo-row { display: none; height: 25px; margin-bottom: 5px; }
.memo-box { width: 25px; height: 25px; border: 1px solid #ffc9c9; background: #fff0f6; border-radius: 50%; text-align: center; font-size: 0.9rem; color: #c92a2a; font-weight: bold; margin-left: 7px; }
.help-zero { display: none; }
.help-text { display: none; }

/* 共通パーツ */
.feedback-msg { text-align: center; font-size: 1.2rem; font-weight: bold; color: var(--success); min-height: 30px; margin-top: 15px; }
.summary-box { background: white; border: 3px solid #63e6be; border-radius: 12px; padding: 15px 20px; margin-top: 20px; font-weight: bold; font-size: 1.2rem; line-height: 1.6; color: #087f5b; box-shadow: inset 0 0 0 3px #e6fcf5; display: none; }
.summary-box.visible { display: block; animation: fadeIn 0.5s; }
.summary-box strong { color: var(--danger); background: #fff0f6; padding: 0 5px; border-radius: 4px; }

.notebook-guide { background: #fff9db; padding: 20px; border-top: 3px dashed #fcc419; text-align: center; margin-top: 20px;}
.notebook-guide h3 { margin: 0 0 10px; color: #d4a017; }
.notebook-keywords { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; margin-top: 10px;}
.notebook-keywords span { background: white; padding: 8px 15px; border-radius: 20px; border: 2px solid #ffe066; font-weight: bold; }
.modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 1000; }
.modal-content { background: white; padding: 30px; border-radius: 12px; max-width: 450px; width: 90%; text-align: center; border-top: 8px solid var(--warning); line-height: 1.5; font-weight: bold; }
.hidden { display: none !important; }