:root {
  --bg: #f6f3ec;
  --card: #ffffff;
  --ink: #2c3440;
  --muted: #6b7380;
  --primary: #4f7cac;
  --primary-dark: #3d6590;
  --go: #4c9a6a;
  --go-dark: #3c7f56;
  --soft: #e8eef5;
  --good-bg: #e5f4ea;
  --good-ink: #2f6b45;
  --miss-bg: #fff4e3;
  --miss-ink: #8a5a14;
  --fix: #f2b25c;
  --danger: #b5523b;
  --radius: 20px;
  --shadow: 0 4px 14px rgba(44, 52, 64, 0.10);
  font-size: 20px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Rounded", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
[hidden] { display: none !important; }

#app {
  max-width: 1040px;
  margin: 0 auto;
  min-height: 100%;
  padding: calc(env(safe-area-inset-top) + 16px) calc(env(safe-area-inset-right) + 20px)
           calc(env(safe-area-inset-bottom) + 12px) calc(env(safe-area-inset-left) + 20px);
}

.screen { display: flex; flex-direction: column; align-items: center; gap: 18px; }
#practice-screen { min-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 28px); gap: 14px; }
.prompt-row { display: flex; flex-direction: column; align-items: center; gap: 14px; width: 100%; }
#practice-screen > :not(.kbd):not(.topbar) { max-width: 760px; }
#start-screen { justify-content: center; min-height: 80vh; text-align: center; }
.logo img { border-radius: 28px; box-shadow: var(--shadow); }
h1 { font-size: 2.6rem; margin: 0; letter-spacing: 0.5px; }
.sub { font-size: 1.3rem; color: var(--muted); margin: 0; }
.level-line { font-size: 1.3rem; margin: 0; }
.note { color: var(--muted); font-size: 1rem; }
.note.small { font-size: 0.85rem; }

.btn {
  font: inherit;
  border: 0;
  border-radius: var(--radius);
  padding: 14px 22px;
  min-height: 60px;
  min-width: 60px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.08s ease, background 0.2s ease;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: #fff; box-shadow: var(--shadow); }
.btn-primary:active { background: var(--primary-dark); }
.btn-go { background: var(--go); color: #fff; box-shadow: var(--shadow); }
.btn-go:active { background: var(--go-dark); }
.btn-soft { background: var(--soft); color: var(--ink); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-danger { background: #f7e3de; color: var(--danger); }
.btn-huge { font-size: 1.7rem; padding: 20px 56px; min-height: 84px; }

.topbar { width: 100%; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.spacer { flex: 1; }
.chip { background: var(--card); border-radius: 999px; padding: 10px 18px; font-size: 1.05rem; box-shadow: var(--shadow); }
.parent-btn { font-size: 0.9rem; border: 2px solid #d9dee6; min-height: 52px; }
.score { background: var(--card); border-radius: 999px; padding: 8px 28px; box-shadow: var(--shadow);
  font-size: 2.4rem; font-weight: 700; color: var(--primary-dark); white-space: nowrap; transition: background 0.3s ease; }
.score .score-star { color: var(--fix); margin-right: 6px; }
.score .score-sep, .score #score-goal { color: var(--muted); font-weight: 600; }
.score.met { background: var(--good-bg); color: var(--good-ink); }

.say-row { margin-top: 10px; }
.btn-say { font-size: 2.2rem; padding: 26px 64px; min-height: 110px; border-radius: 32px; }
.say-icon { font-size: 2.2rem; vertical-align: middle; }
.sentence-text { font-size: 1.4rem; text-align: center; background: var(--card); padding: 14px 20px; border-radius: var(--radius); box-shadow: var(--shadow); margin: 0; max-width: 760px; line-height: 1.5; }

.answer-display { width: 100%; max-width: 760px; min-height: 76px; display: flex; align-items: center; justify-content: center;
  background: #fff; border: 3px solid var(--primary); border-radius: 18px; padding: 8px 18px; position: relative;
  font-size: 2.4rem; font-weight: 600; letter-spacing: 4px; user-select: none; -webkit-user-select: none; cursor: default; }
.answer-text { white-space: pre; }
.caret { display: inline-block; width: 4px; height: 1.1em; margin-left: 3px; background: var(--primary); border-radius: 2px; animation: blink 1.1s steps(1) infinite; }
.answer-display.is-done { border-color: var(--good-ink); color: var(--good-ink); }
.answer-display.is-done .caret { display: none; }
.answer-placeholder { position: absolute; left: 0; right: 0; text-align: center; font-size: 1.3rem; font-weight: 400; letter-spacing: 0; color: #a0a8b3; pointer-events: none; padding-left: 40px; }
.answer-display.shake { animation: shake 0.3s; }
@keyframes blink { 50% { opacity: 0; } }

.kbd { width: 100%; max-width: 1000px; margin-top: auto; display: flex; flex-direction: column; gap: var(--kgap);
  --kgap: 8px; --kh: clamp(50px, 7.6vh, 76px);
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; touch-action: manipulation; }
.kbd-row { display: flex; justify-content: center; gap: var(--kgap); }
.kbd-key { flex: 0 1 calc((100% - 9 * var(--kgap)) / 10); height: var(--kh); min-width: 0; padding: 0;
  font: inherit; font-size: clamp(1.5rem, 3.6vh, 2rem); font-weight: 500; color: var(--ink);
  background: #fff; border: none; border-radius: 12px; box-shadow: 0 2px 0 #c9d1dc;
  touch-action: manipulation; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; cursor: pointer;
  transition: transform 0.06s ease, background 0.06s ease; }
.kbd-key.pressed, .kbd-key:active { background: var(--soft); transform: translateY(2px) scale(0.96); box-shadow: none; }
.kbd-back { flex-basis: calc((100% - 9 * var(--kgap)) / 10 * 2 + var(--kgap)); background: #e3e8ef; font-size: clamp(1.5rem, 3.6vh, 2rem); }
.kbd-enter { flex: 0 1 60%; background: var(--primary); color: #fff; font-weight: 700; box-shadow: 0 2px 0 var(--primary-dark); }
.kbd-enter.is-next { background: var(--good-ink); box-shadow: 0 2px 0 #1e4d33; }
.kbd-enter.pressed, .kbd-enter:active { background: var(--primary-dark); }

.feedback { width: 100%; max-width: 760px; min-height: 40px; text-align: center; border-radius: var(--radius); }
.feedback.good, .feedback.miss { padding: 16px 20px; box-shadow: var(--shadow); }
.feedback.good { background: var(--good-bg); color: var(--good-ink); }
.feedback.miss { background: var(--miss-bg); color: var(--miss-ink); }
.fb-title { font-size: 1.6rem; font-weight: 600; margin: 0 0 6px; }
.fb-hint, .fb-level { font-size: 1.15rem; margin: 10px 0 0; }
.fb-level { font-weight: 600; }
.diff-word { font-size: 3rem; letter-spacing: 6px; color: var(--ink); font-weight: 600; margin: 6px 0; }
.diff-word .l-fix { background: var(--fix); color: #3a2a0a; border-radius: 8px; padding: 0 3px; }
.diff-yours { font-size: 1.15rem; color: var(--muted); }

.celebration { position: fixed; inset: 0; z-index: 20; display: flex; align-items: center; justify-content: center; background: rgba(246, 243, 236, 0.7); }
.celebration canvas { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.celebration-card { position: relative; background: var(--card); border-radius: 28px; padding: 28px 36px; text-align: center; box-shadow: var(--shadow); max-width: 90%; animation: pop 0.4s ease-out; }
.celebration-big { font-size: 2.4rem; font-weight: 700; margin: 0 0 8px; color: var(--good-ink); }
.celebration-msg { font-size: 1.4rem; margin: 0 0 18px; }
@keyframes pop { from { transform: scale(0.7); opacity: 0; } to { transform: scale(1); opacity: 1; } }

.gate { text-align: center; }
.gate > p { font-size: 1.3rem; margin: 0 0 10px; }
.pin-dots { display: flex; gap: 16px; justify-content: center; margin: 6px 0 16px; }
.pin-dots span { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--primary); }
.pin-dots span.filled { background: var(--primary); }
.pin-dots.shake { animation: shake 0.35s; }
@keyframes shake { 25% { transform: translateX(-10px); } 75% { transform: translateX(10px); } }
.pin-pad { display: grid; grid-template-columns: repeat(3, 96px); gap: 14px; justify-content: center; }
.pin-key { background: var(--soft); color: var(--ink); font-size: 2rem; min-height: 80px; border-radius: 22px; }
.pin-key.pin-fn { font-size: 1.1rem; }
.gate-msg { min-height: 1.4em; }
.today-line { font-size: 1.3rem; margin: 0 0 10px; }
.goal-row { display: flex; align-items: center; gap: 12px; }
.goal-btn { font-size: 2rem; width: 72px; }
#goal-input { font-size: 1.8rem; width: 120px; text-align: center; padding: 10px; border-radius: 14px; border: 2px solid #cfd8e3; }

.overlay { position: fixed; inset: 0; background: rgba(44, 52, 64, 0.45); display: flex; align-items: flex-start; justify-content: center; overflow-y: auto; padding: 30px 16px; z-index: 10; }
.panel { background: var(--card); border-radius: 24px; padding: 24px 28px; width: 100%; max-width: 640px; box-shadow: var(--shadow); }
.panel h2 { margin-top: 0; }
.panel h3 { margin: 22px 0 8px; font-size: 1.1rem; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }
.gate-form { display: flex; gap: 10px; }
#gate-answer { font-size: 1.6rem; width: 140px; padding: 10px 14px; border-radius: 14px; border: 2px solid #cfd8e3; }
.level-buttons { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.level-btn { background: var(--soft); color: var(--ink); text-align: left; }
.level-btn.active { background: var(--primary); color: #fff; }
.stats { padding-left: 20px; line-height: 1.6; margin: 0; }
.field { display: flex; flex-direction: column; gap: 6px; margin: 10px 0; }
.field select, .field input[type=range] { font: inherit; padding: 10px; border-radius: 12px; }
.reset-confirm { background: #fbefec; border-radius: 16px; padding: 12px 16px; margin-top: 10px; }
.reset-confirm .btn { margin: 4px 8px 4px 0; }
.close-btn { width: 100%; margin-top: 22px; }

.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

@media (max-width: 640px) {
  :root { font-size: 17px; }
  .btn-say { padding: 20px 40px; }
  .score { font-size: 1.8rem; padding: 6px 18px; }
  .pin-pad { grid-template-columns: repeat(3, 80px); }
  .diff-word { font-size: 2.3rem; }
}
@media (orientation: landscape) and (max-height: 860px) {
  .screen, #practice-screen { gap: 10px; }
  .btn-say { min-height: 64px; padding: 8px 32px; font-size: 1.8rem; }
  .say-row { margin-top: 0; flex: none; }
  .prompt-row { flex-direction: row; justify-content: center; gap: 16px; }
  .topbar { margin-bottom: -4px; }
  .score { font-size: 2rem; padding: 4px 22px; }
  .sentence-text { padding: 10px 18px; font-size: 1.3rem; }
  .answer-display { min-height: 64px; font-size: 2.1rem; }
  .feedback.good, .feedback.miss { padding: 8px 16px; }
  .diff-word { font-size: 2.2rem; margin: 2px 0; }
  .fb-title { margin: 0; }
  .fb-hint { margin: 2px 0 0; }
}
@media (orientation: portrait) and (max-height: 1100px) {
  #practice-screen { gap: 10px; }
  .say-row { margin-top: 0; }
  .btn-say { min-height: 84px; padding: 14px 52px; }
  .feedback.good, .feedback.miss { padding: 10px 16px; }
  .diff-word { font-size: 2.4rem; margin: 2px 0; }
  .fb-hint { margin: 4px 0 0; }
}
@media (prefers-reduced-motion: reduce) {
  .btn, .score { transition: none; }
  .celebration-card, .pin-dots.shake, .answer-display.shake { animation: none; }
  .caret { animation: none; }
  .kbd-key { transition: none; }
}
