:root {
  --bg: #f4f7ff;
  --card: #ffffff;
  --primary: #5b8def;
  --primary-dark: #3a6fd6;
  --accent: #ffb84d;
  --good: #38c172;
  --bad: #ef4444;
  --text: #1f2a44;
  --muted: #6b7592;
  --border: #e3e8f5;
  --radius: 16px;
  --shadow: 0 6px 20px rgba(91,141,239,0.10);
}

* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", "Noto Sans JP", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  background: linear-gradient(135deg, var(--primary) 0%, #7aa3ff 100%);
  color: #fff;
  box-shadow: 0 2px 12px rgba(91,141,239,0.25);
}
.logo { font-weight: 800; font-size: 18px; letter-spacing: 0.5px; }
.stats { display: flex; gap: 10px; font-size: 14px; }
.stat { background: rgba(255,255,255,0.2); padding: 4px 10px; border-radius: 999px; }
.stat b { margin-left: 2px; }

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: 60px;
}

.view { display: none; animation: fade .25s ease; }
.view.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.hero {
  background: var(--card);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
  text-align: center;
  margin-bottom: 16px;
}
.hero h1 { margin: 0 0 6px; font-size: 22px; }
.hero .sub { color: var(--muted); margin: 0 0 14px; font-size: 14px; }

.xp-wrap { margin-top: 12px; }
.xp-bar {
  height: 10px; background: #e6ecfb; border-radius: 999px; overflow: hidden;
}
.xp-fill {
  height: 100%; background: linear-gradient(90deg, var(--accent), #ff8c66);
  width: 0%; transition: width .4s ease;
}
.xp-text { font-size: 12px; color: var(--muted); margin-top: 6px; }

.menu {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}
.menu-card {
  background: var(--card);
  border: none; cursor: pointer;
  border-radius: var(--radius);
  padding: 18px 14px;
  text-align: left;
  box-shadow: var(--shadow);
  transition: transform .12s ease;
}
.menu-card:active { transform: scale(0.97); }
.menu-card .emoji { font-size: 28px; }
.menu-card .title { font-weight: 700; margin: 6px 0 2px; font-size: 15px; }
.menu-card .desc { color: var(--muted); font-size: 12px; }

.back {
  background: none; border: none;
  color: var(--primary); cursor: pointer;
  padding: 6px 0; font-size: 14px; margin-bottom: 8px;
}

h2 { font-size: 18px; margin: 4px 0 12px; }

.cat-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}
.cat-list.small { grid-template-columns: repeat(3, 1fr); }
.cat-card {
  background: var(--card); border: 2px solid transparent;
  border-radius: 14px; padding: 14px 10px;
  cursor: pointer; box-shadow: var(--shadow);
  text-align: center; transition: all .15s;
  font-size: 14px;
}
.cat-card.active { border-color: var(--primary); background: #eef3ff; }
.cat-card .ja { font-weight: 700; }
.cat-card .cnt { color: var(--muted); font-size: 11px; margin-top: 4px; }

.row {
  display: flex; gap: 10px; align-items: center; flex-wrap: wrap;
  margin: 8px 0 16px;
}
.row label { font-size: 14px; color: var(--muted); }

.play-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.progress { flex: 1; height: 8px; background: #e6ecfb; border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; background: var(--primary); width: 0%; transition: width .3s; }
.qcount { font-size: 13px; color: var(--muted); }

.qbox { background: var(--card); border-radius: var(--radius); padding: 22px; box-shadow: var(--shadow); }
.q-text { font-size: 22px; font-weight: 700; text-align: center; margin: 10px 0 22px; }
.hint { color: var(--muted); font-size: 13px; text-align: center; margin-bottom: 8px; }

.options { display: grid; gap: 10px; }
.option {
  background: #f7faff; border: 2px solid var(--border);
  border-radius: 12px; padding: 14px 16px;
  text-align: left; cursor: pointer; font-size: 15px;
  transition: all .12s;
  -webkit-tap-highlight-color: transparent;
}
@media (hover: hover) {
  .option:hover { border-color: var(--primary); }
}
.option:focus { outline: none; }
.option:focus-visible { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(91,141,239,0.25); }
.option.correct { background: #e2f8ec; border-color: var(--good); color: #166e3a; }
.option.wrong { background: #fde7e9; border-color: var(--bad); color: #8c1c24; }
.option:disabled { cursor: default; }

.feedback { margin-top: 14px; min-height: 24px; text-align: center; font-weight: 600; }
.feedback.good { color: var(--good); }
.feedback.bad { color: var(--bad); }

.result-card {
  background: var(--card); border-radius: var(--radius);
  padding: 28px; text-align: center; box-shadow: var(--shadow);
  margin-bottom: 16px;
}
.result-emoji { font-size: 56px; }
.result-score { font-size: 32px; font-weight: 800; margin: 10px 0 4px; }
.result-pts { color: var(--accent); font-weight: 700; font-size: 18px; }

.primary, .ghost {
  flex: 1; padding: 12px 16px; border-radius: 12px; border: none;
  font-weight: 700; font-size: 15px; cursor: pointer;
}
.primary { background: var(--primary); color: #fff; }
.primary:active { background: var(--primary-dark); }
.ghost { background: #fff; color: var(--primary); border: 2px solid var(--primary); }

.chat-log {
  background: var(--card); border-radius: var(--radius);
  padding: 14px; min-height: 320px; max-height: 60vh; overflow-y: auto;
  box-shadow: var(--shadow); margin-bottom: 12px;
}
.msg { margin: 8px 0; display: flex; }
.msg.user { justify-content: flex-end; }
.bubble {
  max-width: 80%; padding: 10px 14px; border-radius: 16px;
  font-size: 14px; line-height: 1.5; white-space: pre-wrap;
}
.msg.user .bubble { background: var(--primary); color: #fff; border-bottom-right-radius: 4px; }
.msg.bot .bubble { background: #eef3ff; color: var(--text); border-bottom-left-radius: 4px; }
.msg.bot .bubble.speakable { cursor: pointer; }
.msg.bot .bubble.speakable:hover { box-shadow: 0 2px 10px rgba(91,141,239,0.18); }
.bubble-en { white-space: pre-wrap; }
.bubble .ja { display: block; color: var(--muted); font-size: 12px; margin-top: 4px; border-top: 1px dashed #cdd6ee; padding-top: 4px; }
.bubble .fix { display: block; color: var(--good); font-size: 12px; margin-top: 4px; }
.bubble .fix.warn { color: #d97706; }

.chat-form {
  display: flex; gap: 8px;
  align-items: flex-start;
}
.chat-form input {
  flex: 1; padding: 12px 14px; border-radius: 12px;
  border: 2px solid var(--border); font-size: 15px;
  background: #fff; min-width: 0;
  height: 48px;
}
.chat-form input:focus { outline: none; border-color: var(--primary); }
.chat-form button[type="submit"] {
  padding: 0 18px; border-radius: 12px; border: none;
  background: var(--primary); color: #fff; font-weight: 700; cursor: pointer;
  height: 48px;
  align-self: flex-start;
}
.chat-center-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}
.mic-btn {
  flex: 0 0 auto;
  width: 48px; height: 48px;
  border-radius: 12px;
  border: 2px solid var(--border);
  background: #fff;
  font-size: 20px; line-height: 1; cursor: pointer;
  transition: all .15s ease;
  -webkit-tap-highlight-color: transparent;
}
.mic-btn:active { transform: scale(0.95); }
.mic-btn.listening {
  background: #fde7e9;
  border-color: var(--bad);
  color: var(--bad);
  animation: mic-pulse 1.2s ease-in-out infinite;
}
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239,68,68,0.45); }
  50% { box-shadow: 0 0 0 8px rgba(239,68,68,0); }
}

/* Two left-side voice buttons stacked: 🎤 (English) on top, 💡 (JA→EN) below */
.chat-form { align-items: flex-start; }
.chat-mic-col {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 0 0 auto;
}
/* Default look matches the mic button (white). Only the listening state
   uses an amber tint so it is visually distinct from the mic's red pulse. */
.help-btn.listening {
  background: #fff3c4;
  border-color: #f59e0b;
  color: #b45309;
  animation: help-pulse 1.2s ease-in-out infinite;
}
@keyframes help-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(245,158,11,0.45); }
  50% { box-shadow: 0 0 0 8px rgba(245,158,11,0); }
}

/* Help translation: sits to the right of the 💡 button as plain text;
   wraps to additional lines when the English gets long. */
.help-preview {
  padding: 12px 6px;
  font-size: 14px;
  line-height: 1.45;
  color: var(--text);
  word-break: break-word;
  min-height: 48px;
  display: flex;
  align-items: center;
  animation: fade .2s ease;
}
.help-preview::before {
  content: "💡 ";
  opacity: 0.7;
  margin-right: 2px;
  flex: 0 0 auto;
}
.hp-text { word-break: break-word; flex: 1; }

.words-list { display: grid; gap: 8px; }
.word-item {
  background: var(--card); border-radius: 12px; padding: 12px 14px;
  box-shadow: var(--shadow);
}
.word-item .en { font-weight: 700; }
.word-item .ja { color: var(--muted); font-size: 13px; }
.word-item .ex { color: var(--muted); font-size: 12px; margin-top: 4px; font-style: italic; }

.word-card { cursor: pointer; transition: transform .12s, box-shadow .15s; -webkit-tap-highlight-color: transparent; }
.word-card:active { transform: scale(0.98); }
.word-card.revealed { box-shadow: 0 4px 16px rgba(91,141,239,0.18); }
.word-front { display: flex; align-items: center; gap: 6px; font-size: 16px; font-weight: 700; }
.word-front-text { flex: 1; }
.word-back { margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--border); animation: fade .25s ease; }
.word-back .en { font-size: 16px; font-weight: 700; }
.word-back .ja { font-size: 14px; color: var(--text); font-weight: 500; }
.word-back .ex { color: var(--muted); font-size: 12px; margin-top: 4px; font-style: italic; }

.speakable { cursor: pointer; transition: opacity .12s; }
.speakable:hover { opacity: 0.75; }
.speakable:active { opacity: 0.5; }
.speak-icon { font-size: 0.85em; opacity: 0.7; margin-right: 2px; }

.login-overlay {
  position: fixed; inset: 0; z-index: 1000;
  background: linear-gradient(135deg, var(--primary) 0%, #7aa3ff 100%);
  display: none; align-items: center; justify-content: center;
  padding: 20px;
}
.login-overlay.show { display: flex; }
.login-box {
  background: #fff; border-radius: 20px; padding: 28px 24px;
  width: 100%; max-width: 360px; text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.2);
  animation: fade .35s ease;
}
.login-logo { font-size: 28px; font-weight: 800; color: var(--primary); margin-bottom: 8px; }
.login-box p { color: var(--muted); margin: 0 0 16px; font-size: 14px; }
.login-box input {
  width: 100%; padding: 12px 14px; border-radius: 12px;
  border: 2px solid var(--border); font-size: 16px; margin-bottom: 12px;
  background: #fff;
}
.login-box input:focus { outline: none; border-color: var(--primary); }
.login-box button {
  width: 100%; padding: 12px; background: var(--primary); color: #fff;
  border: none; border-radius: 12px; font-weight: 700; font-size: 15px;
  cursor: pointer;
}
.login-box button:active { background: var(--primary-dark); }
.login-err { color: var(--bad); font-size: 13px; margin-top: 10px; min-height: 18px; }

.progress-content { display: grid; gap: 14px; }
.progress-content h3 { margin: 8px 0 6px; font-size: 15px; }
.progress-summary {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px;
  background: var(--card); border-radius: var(--radius); padding: 16px;
  box-shadow: var(--shadow);
}
.ps-item { text-align: center; }
.ps-num { font-size: 20px; font-weight: 800; color: var(--primary); }
.ps-lbl { font-size: 11px; color: var(--muted); margin-top: 2px; }

.day-chart {
  display: flex; gap: 4px; align-items: flex-end;
  background: var(--card); border-radius: var(--radius); padding: 12px;
  box-shadow: var(--shadow); overflow-x: auto; min-height: 140px;
}
.day-bar {
  flex: 1; min-width: 18px; height: 110px; display: flex; flex-direction: column;
  justify-content: flex-end; align-items: center; gap: 4px;
}
.day-bar-fill {
  width: 100%; background: linear-gradient(180deg, var(--accent), #ff8c66);
  border-radius: 4px 4px 0 0; min-height: 2px; transition: height .3s;
}
.day-bar-lbl { font-size: 9px; color: var(--muted); }

.recent-list {
  background: var(--card); border-radius: var(--radius); padding: 4px 0;
  box-shadow: var(--shadow);
}
.recent-item {
  display: grid; grid-template-columns: 1fr auto auto auto; gap: 10px;
  padding: 10px 14px; border-bottom: 1px solid var(--border); font-size: 13px;
  align-items: center;
}
.recent-item:last-child { border-bottom: none; }
.r-mode { font-weight: 700; }
.r-score { color: var(--muted); }
.r-pts { color: var(--accent); font-weight: 700; }
.r-when { color: var(--muted); font-size: 11px; }
.muted { color: var(--muted); padding: 12px; text-align: center; }

/* ==== レッスン終了ボタン (home) ==== */
.end-lesson-btn {
  display: block; width: 100%; margin-top: 18px;
  padding: 14px 16px; border: none; border-radius: 14px;
  background: linear-gradient(135deg, #6b7bd6 0%, #8b6dc4 100%);
  color: #fff; font-weight: 700; font-size: 15px; cursor: pointer;
  box-shadow: 0 4px 14px rgba(107,123,214,0.3);
  -webkit-tap-highlight-color: transparent;
}
.end-lesson-btn:active { transform: scale(0.98); }

/* ==== レッスン終了 (ending view) ==== */
.ending {
  background: linear-gradient(180deg, #fff 0%, #f0f5ff 100%);
  border-radius: var(--radius); padding: 24px 20px 20px;
  box-shadow: var(--shadow); text-align: center;
  animation: fade .35s ease;
}
.ending-stage {
  position: relative; width: 160px; height: 160px; margin: 8px auto 4px;
}
.ending-character {
  font-size: 100px; line-height: 160px;
  animation: char-bounce 1.4s ease-in-out infinite;
  filter: drop-shadow(0 6px 14px rgba(255,184,77,0.4));
}
.sparkle {
  position: absolute; font-size: 24px;
  animation: sparkle 1.8s ease-in-out infinite;
}
.sparkle.s1 { top: 8px; left: 12px; animation-delay: 0s; }
.sparkle.s2 { top: 18px; right: 14px; font-size: 30px; animation-delay: 0.4s; }
.sparkle.s3 { bottom: 14px; left: 28px; font-size: 20px; animation-delay: 0.8s; }
@keyframes char-bounce {
  0%, 100% { transform: translateY(0) scale(1); }
  50% { transform: translateY(-12px) scale(1.06); }
}
@keyframes sparkle {
  0%, 100% { transform: scale(0.6) rotate(0deg); opacity: 0.4; }
  50% { transform: scale(1.2) rotate(20deg); opacity: 1; }
}
.ending-title { margin: 8px 0 4px; font-size: 22px; }
.ending-sub { color: var(--muted); margin: 0 0 16px; font-size: 14px; }
.ending-stats {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px;
  background: #fff; border-radius: 14px; padding: 14px 8px;
  margin-bottom: 16px;
}
.es-item { text-align: center; }
.es-num { font-size: 20px; font-weight: 800; color: var(--primary); }
.es-lbl { font-size: 11px; color: var(--muted); margin-top: 2px; }
.ending-bubble {
  background: #fff; border-radius: 14px; padding: 14px 16px;
  box-shadow: 0 2px 8px rgba(91,141,239,0.1);
  margin-bottom: 16px;
}
.ending-bubble p { margin: 0; font-size: 16px; font-weight: 600; }
.ending-bubble p.ja { color: var(--muted); font-size: 13px; font-weight: 400; margin-top: 6px; }

@media (max-width: 480px) {
  .ending-stats { grid-template-columns: repeat(5, 1fr); }
  .es-num { font-size: 16px; }
}

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(100px);
  background: #1f2a44; color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 14px; opacity: 0; transition: all .3s; z-index: 100;
  pointer-events: none;
  max-width: calc(100% - 24px);
}
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }
.toast.toast-long {
  border-radius: 14px;
  padding: 14px 18px;
  white-space: pre-line;
  text-align: left;
  line-height: 1.55;
  font-size: 13px;
  width: min(440px, calc(100% - 24px));
  box-shadow: 0 10px 30px rgba(0,0,0,0.25);
}

@media (min-width: 600px) {
  .menu { grid-template-columns: repeat(5, 1fr); }
  .cat-list { grid-template-columns: repeat(3, 1fr); }
  .cat-list.small { grid-template-columns: repeat(6, 1fr); }
  .hero h1 { font-size: 26px; }
}

@media (max-width: 480px) {
  .progress-summary { grid-template-columns: repeat(3, 1fr); }
  .ps-item:nth-child(n+4) { grid-column: span 1; }
}
