:root {
  --bg1: #5b4bdb;
  --bg2: #8f6bff;
  --card: #ffffff;
  --ink: #2b2a4c;
  --green: #2fbf71;
  --green-d: #1f9457;
  --blue: #2f8cf0;
  --blue-d: #1f6cc0;
  --orange: #ff9f2e;
  --orange-d: #d97f0f;
  --gray: #9aa0b5;
  --gray-d: #7a8097;
  --key: #fff7d6;
  --key-d: #e6cf7c;
  --key-dk: #ffe9e0;
  --key-dk-d: #f0b9a4;
  --key-sm: #e3f4ff;
  --key-sm-d: #9fcdea;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  font-family: "Hiragino Maru Gothic ProN", "Hiragino Sans", "Hiragino Kaku Gothic ProN", sans-serif;
  color: var(--ink);
  background: linear-gradient(160deg, var(--bg1), var(--bg2));
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}
.screen { display: none; position: fixed; inset: 0; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.screen.active { display: flex; }

/* ボタン */
.btn {
  font: inherit; font-weight: 800; color: #fff; border: 0; cursor: pointer;
  border-radius: 18px; padding: 0 22px;
  box-shadow: 0 6px 0 rgba(0,0,0,.22);
  transition: transform .05s, box-shadow .05s;
}
.btn:active { transform: translateY(5px); box-shadow: 0 1px 0 rgba(0,0,0,.22); }
.btn-big { font-size: 34px; height: 92px; padding: 0 56px; border-radius: 28px; }
.btn-mid { font-size: 22px; height: 64px; }
.btn-small { font-size: 20px; height: 48px; padding: 0 20px; border-radius: 14px; }
.btn-green { background: var(--green); }
.btn-blue { background: var(--blue); }
.btn-orange { background: var(--orange); }
.btn-gray { background: var(--gray); }
.btn:disabled { opacity: .5; }

/* タイトル / クリア */
#screen-title, #screen-clear { align-items: center; justify-content: center; text-align: center; }
.title-box { color: #fff; display: flex; flex-direction: column; align-items: center; gap: 28px; }
.title-sub { margin: 0; font-size: 28px; font-weight: 800; opacity: .95; }
.title-box h1 {
  margin: 0; font-size: clamp(44px, 9vw, 96px); line-height: 1.2; font-weight: 900;
  text-shadow: 0 6px 0 rgba(0,0,0,.2);
}
.load-status { margin: 0; min-height: 1.4em; font-size: 18px; opacity: .9; }
.clear-text { animation: pop 1.2s ease-in-out infinite alternate; }
@keyframes pop { from { transform: scale(1) rotate(-1.5deg); } to { transform: scale(1.06) rotate(1.5deg); } }
#screen-clear { background: radial-gradient(circle at 50% 40%, #ffd84d, #ff8a3d 55%, #e0457b); }

/* クイズ */
#screen-quiz { flex-direction: column; padding: calc(env(safe-area-inset-top) + 8px) 16px calc(env(safe-area-inset-bottom) + 10px); gap: 8px; }
.quiz-header { display: flex; justify-content: space-between; align-items: center; color: #fff; }
.progress { font-size: 24px; font-weight: 800; }
.progress b { font-size: 30px; }
.quiz-main { flex: 1 1 0; min-height: 0; display: flex; gap: 16px; }
.image-wrap {
  flex: 1 1 50%; min-width: 0; background: var(--card); border-radius: 28px;
  display: flex; align-items: center; justify-content: center; padding: 12px;
  box-shadow: 0 8px 0 rgba(0,0,0,.18);
}
.image-wrap img { max-width: 100%; max-height: 100%; object-fit: contain; pointer-events: none; }
.answer-area { flex: 1 1 50%; min-width: 0; display: flex; flex-direction: column; justify-content: center; gap: 12px; }
.question { color: #fff; font-size: 26px; font-weight: 800; text-align: center; }
.answer-box {
  background: var(--card); border-radius: 22px; min-height: 96px; padding: 10px 18px;
  display: flex; align-items: center; justify-content: center; flex-wrap: wrap;
  font-size: 48px; font-weight: 900; letter-spacing: 4px; word-break: break-all;
  box-shadow: inset 0 4px 0 rgba(0,0,0,.08), 0 6px 0 rgba(0,0,0,.18);
}
.caret { display: inline-block; width: 4px; height: 52px; background: var(--blue); margin-left: 4px; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.action-row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.btn-answer { min-width: 150px; }
.voice-msg { min-height: 1.4em; text-align: center; color: #fff; font-size: 18px; font-weight: 700; }
.btn-blue.listening { background: #e0457b; animation: pulse .8s ease-in-out infinite alternate; }
@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.05); } }

/* キーボード */
.keyboard-area { flex: 0 0 auto; background: rgba(255,255,255,.18); border-radius: 22px; padding: 8px 10px 10px; }
.kb-tabs { display: flex; gap: 8px; margin-bottom: 6px; }
.kb-tab { font: inherit; font-weight: 800; font-size: 17px; border: 0; border-radius: 12px; padding: 6px 18px; background: rgba(255,255,255,.35); color: #fff; }
.kb-tab.active { background: #fff; color: var(--bg1); }
.keyboard { display: flex; gap: 14px; justify-content: center; }
.keyboard[hidden] { display: none; }
.kgroup { display: grid; grid-auto-flow: column; grid-template-rows: repeat(5, 1fr); gap: 5px; }
.key {
  font: inherit; font-weight: 900; font-size: 26px; color: var(--ink);
  width: clamp(30px, calc((100vw - 200px) / 18), 62px); height: clamp(40px, 6.2vh, 56px);
  background: var(--key); border: 0; border-radius: 12px; box-shadow: 0 4px 0 var(--key-d); padding: 0;
}
.key:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--key-d); }
.key.dk { background: var(--key-dk); box-shadow: 0 4px 0 var(--key-dk-d); }
.key.sm { background: var(--key-sm); box-shadow: 0 4px 0 var(--key-sm-d); }
.key.empty { visibility: hidden; }
.sym .key { width: clamp(52px, 7vw, 84px); height: clamp(48px, 7vh, 64px); font-size: 30px; }
.sym .kgroup { grid-template-rows: repeat(2, 1fr); }

/* オーバーレイ */
.result {
  position: fixed; inset: 0; z-index: 50; background: rgba(30, 20, 80, .6);
  display: flex; align-items: center; justify-content: center;
}
.result[hidden] { display: none; }
.result-card {
  background: var(--card); border-radius: 36px; padding: 24px 40px 28px; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  max-width: 90vw; max-height: 94vh; box-shadow: 0 10px 0 rgba(0,0,0,.2);
  animation: card-in .35s cubic-bezier(.2,1.4,.4,1);
}
@keyframes card-in { from { transform: scale(.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.result-title { font-size: 56px; font-weight: 900; }
.result-title.small { font-size: 30px; padding: 12px 0; }
.result-title.ok { color: var(--green-d); animation: stamp .5s cubic-bezier(.2,1.6,.4,1); }
.result-title.ng { color: var(--orange-d); }
@keyframes stamp { from { transform: scale(2.6) rotate(-12deg); opacity: 0; } to { transform: scale(1) rotate(0); opacity: 1; } }
.result-card img { max-width: min(48vh, 70vw); max-height: 38vh; object-fit: contain; }
.result-name { font-size: 46px; font-weight: 900; }
.result-card.shake { animation: shake .5s; }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-16px); } 40% { transform: translateX(14px); } 60% { transform: translateX(-10px); } 80% { transform: translateX(8px); } }
.confirm-btns { display: flex; gap: 24px; }
#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 100; }

@media (max-aspect-ratio: 1/1) {
  .quiz-main { flex-direction: column; }
  .image-wrap { flex: 1 1 0; min-height: 0; }
  .answer-area { flex: 0 0 auto; }
  .key { width: clamp(30px, calc((100vw - 140px) / 18), 48px); font-size: 22px; height: clamp(38px, 5vh, 52px); }
  .keyboard { gap: 8px; }
  .action-row .btn { font-size: 20px; height: 58px; padding: 0 16px; }
}
