:root {
  --bg1: #fff7e8;
  --bg2: #e6f0ff;
  --ink: #26324b;
  --muted: #6b7690;
  --card: #ffffff;
  --line: #dfe5f2;
  --primary: #4c6ef5;
  --primary-d: #3b57c9;
  --accent: #ff6b6b;
  --ok: #2fb36d;
  --ok-bg: #e5f8ee;
  --ng: #e8505b;
  --ng-bg: #fdecee;
  --warn: #ffb02e;
  --radius: 18px;
  --shadow: 0 6px 20px rgba(38, 50, 75, .10);
  --sans: "Hiragino Sans", "Yu Gothic UI", "Meiryo", "Noto Sans JP", "Segoe UI", system-ui, sans-serif;
  --kanji: "UD Digi Kyokasho N-R", "UD デジタル 教科書体 N-R", "Kyokasho ICA", "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP", serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--sans); color: var(--ink); line-height: 1.5;
  background: linear-gradient(160deg, var(--bg1), var(--bg2)) fixed;
  -webkit-user-select: none; user-select: none; overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
.app { min-height: 100vh; min-height: 100dvh; display: flex; justify-content: center; }
.stage { width: 100%; max-width: 560px; padding: max(env(safe-area-inset-top), 12px) 16px max(env(safe-area-inset-bottom), 20px); }
.screen { padding-top: 52px; display: flex; flex-direction: column; gap: 14px; animation: slideIn .28s ease-out; }
@keyframes slideIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

.mute-btn {
  position: fixed; top: max(env(safe-area-inset-top), 10px); right: 12px; z-index: 30; width: 42px; height: 42px;
  border-radius: 50%; border: 2px solid var(--line); background: #fff; color: var(--primary); display: grid; place-items: center; box-shadow: var(--shadow);
}
.mute-btn[aria-pressed="true"] { color: var(--muted); }

/* ボタン */
.btn {
  border: 0; border-radius: 14px; padding: 12px 20px; font-weight: 700; font-size: 1rem; min-height: 48px;
  transition: transform .08s ease, box-shadow .08s ease, filter .15s;
}
.btn:active { transform: translateY(2px); }
.btn.primary { background: var(--primary); color: #fff; box-shadow: 0 4px 0 var(--primary-d); }
.btn.primary:active { box-shadow: 0 2px 0 var(--primary-d); }
.btn.secondary { background: #fff; color: var(--primary); border: 2px solid var(--primary); box-shadow: 0 4px 0 #c9d4ff; }
.btn.ghost { background: transparent; color: var(--muted); border: 2px solid var(--line); }
.btn.big { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 16px 20px; font-size: 1.25rem; width: 100%; }
.btn.big small { font-size: .78rem; font-weight: 500; opacity: .85; }
.btn:focus-visible, .opt:focus-visible, .chip:focus-visible, .gcard:focus-visible, .icon-btn:focus-visible, .lang button:focus-visible { outline: 3px solid #ffb02e; outline-offset: 2px; }
.row { display: flex; gap: 10px; }
.row > .btn { flex: 1; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; border: 2px solid var(--line); background: #fff; font-size: 1.1rem; line-height: 1; flex: none; }

/* タイトル */
.title { align-items: center; text-align: center; padding-top: 44px; }
.logo { display: flex; gap: 8px; margin-top: 8px; }
.tile { width: 64px; height: 64px; display: grid; place-items: center; border-radius: 16px; font-family: var(--kanji); font-size: 2.3rem; color: #fff; box-shadow: 0 5px 0 rgba(0, 0, 0, .15); animation: bob 3s ease-in-out infinite; }
.tile.a { background: var(--primary); } .tile.b { background: var(--accent); animation-delay: .25s; } .tile.c { background: var(--ok); animation-delay: .5s; }
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(-2deg); } 50% { transform: translateY(-7px) rotate(2deg); } }
h1 { margin: 6px 0 0; font-size: clamp(1.7rem, 7vw, 2.3rem); letter-spacing: .02em; }
.tagline { margin: 0 0 4px; color: var(--muted); font-size: 1rem; }
.lang { display: inline-flex; background: #fff; border: 2px solid var(--line); border-radius: 999px; padding: 3px; }
.lang button { border: 0; background: transparent; border-radius: 999px; padding: 6px 18px; font-weight: 700; color: var(--muted); min-height: 36px; }
.lang button.on { background: var(--primary); color: #fff; }
.record { width: 100%; background: rgba(255, 255, 255, .7); border: 2px dashed var(--line); border-radius: var(--radius); padding: 10px 14px; margin-top: 4px; }
.rec-row { display: flex; justify-content: space-between; gap: 8px; font-size: .92rem; }
.rec-row.muted { justify-content: center; color: var(--muted); }
.source { font-size: .72rem; color: var(--muted); margin: 4px 0 0; }

/* 練習 */
.head { display: flex; align-items: center; gap: 10px; }
.head h2 { margin: 0; font-size: 1.3rem; }
.sec { margin: 6px 0 0; font-size: .95rem; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { border: 2px solid var(--line); background: #fff; border-radius: 999px; padding: 6px 14px; font-weight: 700; min-height: 38px; color: var(--muted); }
.chip.on { background: var(--primary); border-color: var(--primary); color: #fff; }
.note { margin: 0; font-size: .8rem; color: var(--muted); }
.check { display: flex; gap: 8px; align-items: center; font-size: .95rem; }
.check input { width: 20px; height: 20px; accent-color: var(--primary); }
.gridg { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.gcard { border: 0; border-radius: var(--radius); background: #fff; padding: 14px 12px; text-align: left; box-shadow: var(--shadow); border-left: 8px solid var(--primary); display: flex; flex-direction: column; gap: 2px; transition: transform .1s; }
.gcard:active { transform: scale(.97); }
.gcard.g1 { border-color: #2fb36d; } .gcard.g2 { border-color: #51cf66; } .gcard.g3 { border-color: #22b8cf; } .gcard.g4 { border-color: #4c6ef5; }
.gcard.g5 { border-color: #7950f2; } .gcard.g6 { border-color: #be4bdb; } .gcard.g7 { border-color: #ff922b; } .gcard.g8 { border-color: #f03e3e; }
.gname { font-size: 1.15rem; font-weight: 800; }
.gsub { font-size: .78rem; color: var(--muted); }
.gbest { font-size: .78rem; color: var(--primary); font-weight: 700; min-height: 1.2em; }

/* クイズ */
.quiz { padding-top: 54px; gap: 10px; }
.qbar { display: flex; align-items: center; gap: 10px; }
.progress { flex: 1; height: 12px; background: #fff; border-radius: 999px; border: 2px solid var(--line); overflow: hidden; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--primary), #7c93ff); border-radius: 999px; transition: width .35s ease; }
.count { font-weight: 800; font-variant-numeric: tabular-nums; min-width: 4.2em; text-align: right; font-size: .95rem; }
.combo { height: 26px; text-align: center; font-weight: 800; color: var(--accent); opacity: 0; transition: opacity .2s; }
.combo.show { opacity: 1; }
.combo.pop { animation: pop .4s ease-out; }
@keyframes pop { 0% { transform: scale(.6); } 60% { transform: scale(1.25); } 100% { transform: scale(1); } }
.qcard { position: relative; background: var(--card); border-radius: 24px; box-shadow: var(--shadow); padding: 18px 16px 18px; display: flex; flex-direction: column; gap: 8px; animation: cardIn .3s ease-out; }
@keyframes cardIn { from { opacity: 0; transform: translateX(24px); } to { opacity: 1; transform: none; } }
.gtag:not(:empty) { position: absolute; top: 10px; left: 14px; font-size: .78rem; font-weight: 800; color: var(--primary); background: #edf1ff; border-radius: 999px; padding: 2px 10px; }
.prompt { margin: 6px 0 0; text-align: center; font-weight: 700; color: var(--muted); font-size: 1rem; }
.big { text-align: center; line-height: 1.15; word-break: keep-all; }
.big.kanji { font-family: var(--kanji); font-size: clamp(64px, 24vw, 128px); }
.big.kanji.long { font-size: clamp(44px, 15vw, 86px); }
.big.kana { font-size: clamp(34px, 10vw, 52px); font-weight: 800; }
.sub { margin: 0; text-align: center; color: var(--muted); font-size: .95rem; min-height: 1.4em; }
.opts { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 10px; margin-top: 6px; }
.opts.kanji, .opts.kanjiword, .opts.radical { grid-template-columns: repeat(2, 1fr); }
.opt {
  position: relative; border: 2px solid var(--line); background: #fbfcff; border-radius: 16px; padding: 14px 12px; min-height: 60px;
  font-weight: 700; font-size: 1.1rem; display: flex; align-items: center; justify-content: center; text-align: center; gap: 8px;
  box-shadow: 0 3px 0 var(--line); transition: transform .08s, background .15s, border-color .15s;
}
.opt:active:not(:disabled) { transform: translateY(2px); box-shadow: 0 1px 0 var(--line); }
.opt .key { position: absolute; left: 8px; top: 6px; font-size: .68rem; color: #a2abc2; font-weight: 700; }
@media (pointer: coarse) { .opt .key { display: none; } }
.opts.kanji .a, .opts.kanjiword .a { font-family: var(--kanji); font-size: 2rem; font-weight: 400; }
.opts.kanjiword .a { font-size: 1.7rem; }
.opts.radical .a.rmark { font-family: var(--kanji); font-size: 1.9rem; font-weight: 400; }
.opts.radical .opt { flex-direction: column; gap: 0; }
.opts.radical .b { font-size: .85rem; }
.opts.text .a { font-size: 1.15rem; }
.opt:disabled { cursor: default; }
.opt.correct { background: var(--ok-bg); border-color: var(--ok); box-shadow: 0 3px 0 var(--ok); animation: pop .4s ease-out; }
.opt.wrong { background: var(--ng-bg); border-color: var(--ng); box-shadow: 0 3px 0 var(--ng); animation: shake .4s; }
@keyframes shake { 0%, 100% { transform: translateX(0); } 20% { transform: translateX(-8px); } 40% { transform: translateX(8px); } 60% { transform: translateX(-5px); } 80% { transform: translateX(5px); } }
.inputrow { display: flex; gap: 8px; margin-top: 6px; }
.inputrow input { flex: 1; min-width: 0; border: 2px solid var(--line); border-radius: 14px; padding: 10px 14px; font-size: 1.3rem; font-family: var(--sans); -webkit-user-select: text; user-select: text; }
.inputrow input:focus { outline: 3px solid #bac8ff; border-color: var(--primary); }

/* 手書き */
.write-wrap { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.write-box { position: relative; width: min(100%, 320px); aspect-ratio: 1 / 1; background: #fffdf5; border: 3px solid #e3d8b8; border-radius: 12px; touch-action: none; }
.write-box.revealed { opacity: .92; }
.write-box .guide-h, .write-box .guide-v { position: absolute; pointer-events: none; border: 0 dashed #e6dcc0; }
.write-box .guide-h { left: 0; right: 0; top: 50%; border-top-width: 2px; }
.write-box .guide-v { top: 0; bottom: 0; left: 50%; border-left-width: 2px; }
.write-box .hint-text { position: absolute; inset: auto 0 8px 0; text-align: center; font-size: .75rem; color: #c5b98f; pointer-events: none; }
.write-canvas { position: absolute; inset: 0; touch-action: none; cursor: crosshair; }
.write-answer { display: flex; align-items: center; gap: 12px; background: var(--ok-bg); border: 2px solid var(--ok); border-radius: 14px; padding: 6px 18px; animation: pop .35s ease-out; }
.write-answer b.kanji { font-family: var(--kanji); font-size: 3.4rem; font-weight: 400; line-height: 1.1; }

/* フィードバック */
.feedback { display: none; border-radius: var(--radius); padding: 12px 14px; gap: 4px; flex-direction: column; }
.feedback.in { display: flex; animation: slideIn .22s ease-out; }
.feedback.ok { background: var(--ok-bg); border: 2px solid var(--ok); }
.feedback.ng { background: var(--ng-bg); border: 2px solid var(--ng); }
.fb-title { font-weight: 800; font-size: 1.15rem; }
.feedback.ok .fb-title { color: #1f8f55; } .feedback.ng .fb-title { color: var(--ng); }
.fb-ans { font-weight: 700; }
.fb-exp { color: var(--ink); font-size: .95rem; }
.feedback .btn { margin-top: 6px; align-self: stretch; }
.flash { position: fixed; left: 50%; top: 38%; z-index: 40; transform: translate(-50%, -50%); font-size: 9rem; font-weight: 900; pointer-events: none; animation: flashAnim .72s ease-out forwards; }
.flash.ok { color: var(--ok); text-shadow: 0 6px 20px rgba(47, 179, 109, .45); }
.flash.ng { color: var(--ng); text-shadow: 0 6px 20px rgba(232, 80, 91, .45); }
@keyframes flashAnim { 0% { opacity: 0; transform: translate(-50%, -50%) scale(.3); } 25% { opacity: .95; transform: translate(-50%, -50%) scale(1.15); } 100% { opacity: 0; transform: translate(-50%, -62%) scale(1.3); } }

/* 判定中・結果 */
.judging { align-items: center; justify-content: center; min-height: 70vh; }
.spinner { width: 64px; height: 64px; border-radius: 50%; border: 8px solid #dbe4ff; border-top-color: var(--primary); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.judging-text { font-weight: 800; font-size: 1.3rem; color: var(--primary); }
.result { gap: 14px; }
.rcard { background: #fff; border-radius: 28px; box-shadow: var(--shadow); padding: 22px 16px 20px; text-align: center; border-top: 8px solid var(--primary); animation: cardIn .5s ease-out; }
.rc-lead { color: var(--muted); font-weight: 700; }
.rc-level { font-size: clamp(2rem, 10vw, 3.1rem); font-weight: 900; color: var(--primary); line-height: 1.2; margin-top: 4px; animation: pop .6s ease-out; }
.rc-desu { font-weight: 700; color: var(--muted); }
.rc-nick { display: inline-block; margin-top: 10px; background: #fff0f0; color: var(--accent); font-weight: 800; border-radius: 999px; padding: 4px 16px; }
.rc-score { margin-top: 12px; font-weight: 800; font-size: 1.05rem; }
.gauge { margin: 18px 6px 4px; }
.gtrack { position: relative; height: 16px; background: #e8ecf6; border-radius: 999px; }
.gfill { height: 100%; border-radius: 999px; background: linear-gradient(90deg, var(--ok), var(--warn) 60%, var(--accent)); animation: grow 1s ease-out; }
@keyframes grow { from { width: 0 !important; } }
.gmarker { position: absolute; top: -8px; width: 8px; height: 32px; margin-left: -4px; background: var(--ink); border-radius: 4px; box-shadow: 0 0 0 3px #fff; }
.gmarks { position: relative; height: 22px; margin-top: 10px; }
.gm { position: absolute; transform: translateX(-50%); font-size: .8rem; font-weight: 700; color: var(--muted); }
.bdbox { background: rgba(255, 255, 255, .85); border-radius: var(--radius); padding: 12px 14px; }
.bdbox h3 { margin: 0 0 8px; font-size: .95rem; color: var(--muted); }
.bd-row { display: flex; align-items: center; gap: 8px; margin: 5px 0; }
.bd-l { width: 3.2em; font-weight: 800; font-size: .85rem; }
.bd-bar { flex: 1; height: 12px; background: #e8ecf6; border-radius: 999px; overflow: hidden; }
.bd-bar i { display: block; height: 100%; background: var(--primary); border-radius: 999px; animation: grow 1s ease-out; }
.bd-v { width: 3.2em; text-align: right; font-size: .85rem; font-variant-numeric: tabular-nums; }
.actions { display: flex; flex-direction: column; gap: 10px; }
.stars { font-size: 3rem; letter-spacing: .1em; }
.star { color: #d6dbe8; display: inline-block; }
.star.on { color: var(--warn); animation: pop .5s ease-out backwards; text-shadow: 0 3px 8px rgba(255, 176, 46, .5); }
.perfect { text-align: center; font-weight: 800; color: var(--ok); }
.mlist { margin: 0; padding-left: 1.2em; font-size: .92rem; }
.toast { position: fixed; left: 50%; bottom: 28px; transform: translate(-50%, 20px); background: var(--ink); color: #fff; padding: 8px 18px; border-radius: 999px; opacity: 0; transition: .25s; pointer-events: none; z-index: 50; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 45; }
.confetti i { position: absolute; top: -20px; width: 10px; height: 16px; border-radius: 2px; animation: fall linear forwards; }
@keyframes fall { to { top: 105%; transform: translateX(40px) rotate(720deg); } }

/* オーバーレイ */
.overlay { position: fixed; inset: 0; background: rgba(38, 50, 75, .45); display: none; align-items: center; justify-content: center; z-index: 60; padding: 20px; }
.overlay.show { display: flex; }
.modal { background: #fff; border-radius: 22px; padding: 22px 20px 18px; width: min(100%, 360px); text-align: center; box-shadow: var(--shadow); }
.modal p { margin: 0 0 14px; font-weight: 800; font-size: 1.1rem; }

/* 高さが低い画面 (埋め込み枠・横向きスマホなど): 1画面に収まるよう詰める */
@media (max-height: 640px) {
  .big.kanji { font-size: clamp(48px, min(18vw, 20vh), 90px); }
  .big.kanji.long { font-size: clamp(36px, min(13vw, 15vh), 64px); }
  .screen { gap: 8px; }
  .quiz { padding-top: 48px; gap: 6px; }
  .qcard { padding: 12px 12px 12px; gap: 4px; border-radius: 18px; }
  .prompt { margin: 2px 0 0; font-size: .9rem; }
  .combo { height: 18px; font-size: .85rem; }
  .opts { gap: 8px; margin-top: 2px; }
  .opt { min-height: 48px; padding: 8px 10px; }
  .opts.kanji .a { font-size: 1.6rem; }
  .write-box { width: min(100%, 220px); }
  .feedback { padding: 8px 12px; }
  .feedback .btn { min-height: 40px; padding: 8px 16px; }
}
@media (max-height: 460px) {
  .quiz { padding-top: 44px; }
  .big.kanji { font-size: clamp(40px, 17vh, 64px); }
  .sub { font-size: .85rem; min-height: 1.2em; }
  .flash { font-size: 6rem; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
