:root {
  --bg: #ffffff;
  --fg: #3c3c3c;
  --muted: #777777;
  --line: #e5e5e5;
  --card: #ffffff;
  --soft: #f7f7f7;
  --green: #58cc02;
  --green-d: #58a700;
  --green-bg: #d7ffb8;
  --red: #ff4b4b;
  --red-d: #ea2b2b;
  --red-bg: #ffdfe0;
  --blue: #1cb0f6;
  --blue-d: #1899d6;
  --blue-bg: #ddf4ff;
  --gold: #ffc800;
  --gold-d: #e5a500;
  --locked: #e5e5e5;
  --locked-d: #c8c8c8;
  --code-bg: #f6f8fa;
  --t-br: #8b949e;
  --t-kw: #8250df;
  --t-str: #0a7f3f;
  --t-num: #cf5a00;
  --t-sp: #cf222e;
  --t-sym: #24292f;
  --t-com: #8b949e;
  --radius: 16px;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #131f24;
    --fg: #f1f7fb;
    --muted: #8a9ba6;
    --line: #37464f;
    --card: #131f24;
    --soft: #1b2b33;
    --green-bg: #1f3a14;
    --red-bg: #3d1a1d;
    --blue-bg: #13344a;
    --locked: #37464f;
    --locked-d: #263238;
    --code-bg: #1b2b33;
    --t-br: #6e7f8a;
    --t-kw: #d2a8ff;
    --t-str: #7ee787;
    --t-num: #ffa657;
    --t-sp: #ff7b72;
    --t-sym: #e6edf3;
    --t-com: #6e7f8a;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Segoe UI", sans-serif;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100dvh;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

code,
pre {
  font-family: var(--mono);
}

/* ---------- ボタン ---------- */

.btn {
  display: block;
  width: 100%;
  padding: 14px 16px;
  border: 0;
  border-radius: var(--radius);
  font-weight: 800;
  font-size: 1rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  transition: transform 0.05s, filter 0.15s;
}

.btn-primary {
  background: var(--green);
  color: #fff;
  box-shadow: 0 4px 0 var(--green-d);
}

.btn-primary:active:not(:disabled) {
  transform: translateY(4px);
  box-shadow: none;
}

.btn-primary:disabled {
  background: var(--locked);
  color: var(--muted);
  box-shadow: 0 4px 0 var(--locked-d);
  cursor: default;
}

.btn-ghost {
  margin-top: 12px;
  background: transparent;
  color: var(--blue);
  border: 2px solid var(--line);
  box-shadow: 0 4px 0 var(--line);
}

.icon-btn {
  border: 0;
  background: none;
  font-size: 1.3rem;
  padding: 6px;
  line-height: 1;
}

/* ---------- ホーム ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--bg);
  border-bottom: 2px solid var(--line);
}

.brand {
  font-family: var(--mono);
  font-weight: 800;
  color: var(--green);
  font-size: 1.1rem;
}

.stats {
  display: flex;
  align-items: center;
  gap: 12px;
  font-weight: 800;
}

.stat-streak {
  color: #ff9600;
}

.stat-xp {
  color: var(--gold-d);
}

.path {
  padding: 16px;
}

.unit {
  margin-bottom: 28px;
}

.unit-banner {
  background: var(--c);
  color: #fff;
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: 0 4px 0 color-mix(in srgb, var(--c) 70%, #000);
}

.unit-banner small {
  font-weight: 800;
  opacity: 0.85;
  letter-spacing: 0.05em;
}

.unit-banner h2 {
  margin: 2px 0 2px;
  font-size: 1.3rem;
}

.unit-banner p {
  margin: 0;
  opacity: 0.9;
  font-size: 0.9rem;
}

.nodes {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  padding: 64px 0 4px;
}

.node-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.node {
  position: relative;
  width: 72px;
  height: 64px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  -webkit-appearance: none;
  appearance: none;
  font-size: 1.6rem;
  font-weight: 800;
  line-height: 1;
  color: #fff;
  background: var(--c);
  box-shadow: 0 6px 0 color-mix(in srgb, var(--c) 70%, #000);
  transition: transform 0.05s;
}

.node:active {
  transform: translateY(6px);
  box-shadow: none;
}

.node-done {
  background: var(--gold);
  box-shadow: 0 6px 0 var(--gold-d);
}

.node-locked {
  background: var(--locked);
  box-shadow: 0 6px 0 var(--locked-d);
  font-size: 1.2rem;
}

/* 現在のレッスンのリング: 影(6px)を含めた見た目の中心に合わせる */
.node-current::before {
  content: "";
  position: absolute;
  inset: -10px -10px -16px;
  border: 6px solid color-mix(in srgb, var(--c) 30%, transparent);
  border-radius: 50%;
  pointer-events: none;
  animation: pulse 1.6s ease-in-out infinite;
}

.node-current:active::before {
  inset: -10px;
}

@keyframes pulse {
  50% {
    transform: scale(1.06);
  }
}

.node-tip {
  position: absolute;
  bottom: calc(100% + 12px);
  padding: 6px 12px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--c);
  font-weight: 800;
  font-size: 0.85rem;
  white-space: nowrap;
  animation: bob 1.6s ease-in-out infinite;
}

@keyframes bob {
  50% {
    transform: translateY(-4px);
  }
}

.complete-all {
  text-align: center;
  font-weight: 800;
  font-size: 1.2rem;
  padding: 24px;
}

.footer-space {
  height: calc(80px + env(safe-area-inset-bottom));
}

.fab {
  position: fixed;
  right: max(16px, calc(50vw - 280px + 16px));
  bottom: calc(16px + env(safe-area-inset-bottom));
  padding: 14px 20px;
  border: 0;
  border-radius: 999px;
  background: var(--blue);
  color: #fff;
  font-weight: 800;
  box-shadow: 0 4px 0 var(--blue-d);
}

.fab:active {
  transform: translateY(4px);
  box-shadow: none;
}

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 10;
  background: rgb(0 0 0 / 0.4);
}

.sheet {
  position: fixed;
  left: 50%;
  bottom: 0;
  z-index: 11;
  width: min(560px, 100%);
  padding: 20px 20px calc(24px + env(safe-area-inset-bottom));
  border-radius: 20px 20px 0 0;
  background: var(--c);
  color: #fff;
  transform: translate(-50%, 100%);
  transition: transform 0.2s ease-out;
}

.sheet.open {
  transform: translate(-50%, 0);
}

.sheet small {
  font-weight: 800;
  opacity: 0.85;
}

.sheet h3 {
  margin: 4px 0;
  font-size: 1.4rem;
}

.sheet p {
  margin: 0 0 16px;
  opacity: 0.9;
}

.sheet .btn-primary {
  background: #fff;
  color: var(--c);
  box-shadow: 0 4px 0 rgb(0 0 0 / 0.2);
}

/* ---------- レッスン ---------- */

.lesson {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.lesson-top {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
}

.close {
  color: var(--muted);
}

.progress {
  flex: 1;
  height: 16px;
  border-radius: 999px;
  background: var(--locked);
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  border-radius: 999px;
  background: var(--green);
  transition: width 0.35s ease;
  box-shadow: inset 0 4px 0 rgb(255 255 255 / 0.25);
}

.hearts {
  font-weight: 800;
  color: var(--red);
  min-width: 3em;
  text-align: right;
}

.shake {
  animation: shake 0.4s;
}

@keyframes shake {
  25% {
    transform: translateX(-4px);
  }
  75% {
    transform: translateX(4px);
  }
}

.lesson-body {
  flex: 1;
  overflow-y: auto;
  padding: 8px 16px 24px;
}

.badge-retry {
  display: inline-block;
  margin-bottom: 8px;
  color: #ff9600;
  font-weight: 800;
  font-size: 0.85rem;
}

.prompt {
  margin: 0 0 16px;
  font-size: 1.4rem;
  line-height: 1.4;
}

.code {
  margin: 0 0 20px;
  padding: 16px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: var(--code-bg);
  font-size: 1.05rem;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: auto;
}

.t-br { color: var(--t-br); }
.t-kw { color: var(--t-kw); }
.t-str { color: var(--t-str); }
.t-num { color: var(--t-num); }
.t-sp { color: var(--t-sp); font-weight: 700; }
.t-sym { color: var(--t-sym); }
.t-com { color: var(--t-com); font-style: italic; }

/* 選択肢 */

.options {
  display: grid;
  gap: 10px;
}

.opt {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 56px;
  padding: 12px 14px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: 0 4px 0 var(--line);
  text-align: left;
  transition: transform 0.05s;
}

.opt:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: 0 2px 0 var(--line);
}

.opt-num {
  flex: none;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 2px solid var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 800;
}

.opt-text {
  font-size: 1.05rem;
}

.opt.selected {
  border-color: var(--blue);
  background: var(--blue-bg);
  box-shadow: 0 4px 0 var(--blue);
}

.opt.selected .opt-num {
  border-color: var(--blue);
  color: var(--blue);
}

.opt.correct {
  border-color: var(--green);
  background: var(--green-bg);
  box-shadow: 0 4px 0 var(--green);
}

.opt.wrong {
  border-color: var(--red);
  background: var(--red-bg);
  box-shadow: 0 4px 0 var(--red);
}

.opt.correct .opt-num {
  border-color: var(--green);
  color: var(--green-d);
}

.opt.wrong .opt-num {
  border-color: var(--red);
  color: var(--red);
}

/* 穴埋め */

.blank {
  font: inherit;
  color: var(--fg);
  min-width: 4ch;
  margin: 0 2px;
  padding: 0 4px;
  border: 0;
  border-bottom: 3px solid var(--blue);
  border-radius: 4px 4px 0 0;
  background: var(--blue-bg);
  outline: none;
  font-size: 16px; /* iOS のズーム防止 */
}

.blank.correct {
  border-color: var(--green);
  background: var(--green-bg);
}

.blank.wrong {
  border-color: var(--red);
  background: var(--red-bg);
}

/* 並べ替え */

.answer-line {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 8px;
  min-height: 112px;
  padding: 12px 0;
  margin-bottom: 20px;
  border-top: 2px solid var(--line);
  border-bottom: 2px solid var(--line);
  background: repeating-linear-gradient(transparent 0 54px, var(--line) 54px 56px);
}

.answer-line.correct {
  border-color: var(--green);
}

.answer-line.wrong {
  border-color: var(--red);
}

.answer-hint {
  color: var(--muted);
  font-size: 0.9rem;
  align-self: center;
  margin: auto;
}

.bank {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.chip {
  min-width: 44px;
  min-height: 44px;
  padding: 8px 12px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  box-shadow: 0 3px 0 var(--line);
  font-size: 1.05rem;
}

.chip:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--line);
}

.chip-slot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--line);
  box-shadow: none;
  color: transparent;
}

/* マッチング */

.match {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.mcol {
  display: grid;
  gap: 10px;
}

.mitem {
  min-height: 60px;
  padding: 10px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: 0 4px 0 var(--line);
  font-size: 0.95rem;
  word-break: break-word;
}

.mitem.selected {
  border-color: var(--blue);
  background: var(--blue-bg);
  box-shadow: 0 4px 0 var(--blue);
}

.mitem.matched {
  opacity: 0.35;
  border-color: var(--green);
  box-shadow: none;
  transform: translateY(4px);
}

.mitem.mismatch {
  border-color: var(--red);
  background: var(--red-bg);
  animation: shake 0.4s;
}

/* フィードバック */

.lesson-foot {
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  border-top: 2px solid var(--line);
  background: var(--bg);
}

.lesson-foot.ok {
  background: var(--green-bg);
  border-color: transparent;
}

.lesson-foot.ng {
  background: var(--red-bg);
  border-color: transparent;
}

.lesson-foot.ng .btn-primary {
  background: var(--red);
  box-shadow: 0 4px 0 var(--red-d);
}

.feedback:empty {
  display: none;
}

.feedback {
  margin-bottom: 14px;
  animation: rise 0.2s ease-out;
}

@keyframes rise {
  from {
    transform: translateY(12px);
    opacity: 0;
  }
}

.fb-title {
  font-size: 1.3rem;
  font-weight: 800;
}

.ok .fb-title {
  color: var(--green-d);
}

.ng .fb-title,
.ng .fb-answer {
  color: var(--red-d);
}

.fb-answer {
  margin-top: 6px;
  font-weight: 700;
  white-space: pre-wrap;
}

.fb-explain {
  margin-top: 6px;
  font-size: 0.95rem;
  line-height: 1.5;
}

/* ---------- 結果 ---------- */

.result {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-height: 100dvh;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
  text-align: center;
}

.result-hero {
  font-size: 5rem;
  animation: pop 0.5s cubic-bezier(0.2, 1.6, 0.4, 1);
}

@keyframes pop {
  from {
    transform: scale(0.3);
    opacity: 0;
  }
}

.result h1 {
  margin: 8px 0 24px;
  color: var(--gold-d);
}

.result p {
  color: var(--muted);
}

.result-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}

.rc {
  overflow: hidden;
  border: 2px solid var(--c);
  border-radius: var(--radius);
}

.rc small {
  display: block;
  padding: 4px;
  background: var(--c);
  color: #fff;
  font-weight: 800;
  font-size: 0.75rem;
}

.rc b {
  display: block;
  padding: 12px 4px;
  color: var(--c);
  font-size: 1.1rem;
}

.rc-xp { --c: var(--gold); }
.rc-acc { --c: var(--green); }
.rc-time { --c: var(--blue); }

.result-streak {
  margin: 0 0 24px;
  font-weight: 800;
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}
