:root {
  color-scheme: light;
  --bg: #f5f3ff;
  --surface: #ffffff;
  --surface-2: #ede9fe;
  --ink: #202033;
  --muted: #64627a;
  --primary: #4f46e5;
  --primary-dark: #3730a3;
  --accent: #f59e0b;
  --good: #16804c;
  --bad: #c53d46;
  --line: #d9d4f4;
  --shadow: 0 10px 26px rgba(45, 38, 112, .12);
  --radius: 24px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: "Noto Sans KR", "Apple SD Gothic Neo", "Malgun Gothic", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button, select, input { font: inherit; }
button { touch-action: manipulation; }

body {
  min-height: 100dvh;
  background:
    radial-gradient(circle at 10% 0%, rgba(245, 158, 11, .13), transparent 26rem),
    radial-gradient(circle at 90% 10%, rgba(79, 70, 229, .15), transparent 28rem),
    var(--bg);
}

.app {
  width: min(100%, 780px);
  min-height: 100dvh;
  margin: 0 auto;
  padding-bottom: max(18px, env(safe-area-inset-bottom));
}

.app.calm {
  --bg: #f4f7f4;
  --surface-2: #e8f0ea;
  --primary: #1f6f5b;
  --primary-dark: #164d40;
  --accent: #bd7430;
  --line: #cfded2;
}

.topbar {
  height: 68px;
  display: grid;
  grid-template-columns: 48px 1fr 48px;
  align-items: center;
  padding: max(8px, env(safe-area-inset-top)) 16px 0;
}

.brand {
  justify-self: center;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1.28rem;
  font-weight: 900;
}

.brand-mark { color: var(--accent); font-size: 1.5rem; }

.icon-btn {
  width: 46px;
  height: 46px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(255,255,255,.86);
  color: var(--ink);
  font-size: 1.35rem;
  cursor: pointer;
}

.screen { padding: 12px 18px 28px; }
.hidden { display: none !important; }

.hero {
  padding: 24px 20px;
  border-radius: 30px;
  background: linear-gradient(145deg, var(--primary), var(--primary-dark));
  color: white;
  box-shadow: var(--shadow);
  overflow: hidden;
  position: relative;
}

.hero::after {
  content: "✦";
  position: absolute;
  right: 18px;
  bottom: -24px;
  font-size: 7rem;
  color: rgba(255,255,255,.11);
  transform: rotate(12deg);
}

.hero h1 { margin: 0 0 8px; font-size: clamp(1.9rem, 7vw, 3rem); line-height: 1.15; }
.hero p { margin: 0; line-height: 1.65; font-size: 1.02rem; }

.section-title {
  margin: 26px 2px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.section-title h2 { margin: 0; font-size: 1.25rem; }
.section-title span { color: var(--muted); font-size: .9rem; }

.settings-grid, .mode-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.setting-card, .mode-card, .panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 6px 18px rgba(45,38,112,.06);
}

.setting-card { padding: 14px; }
.setting-card label { display: block; color: var(--muted); font-size: .88rem; margin-bottom: 8px; }
.setting-card select {
  width: 100%;
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--surface);
  color: var(--ink);
  padding: 8px 10px;
  font-weight: 800;
}

.mode-card {
  min-height: 156px;
  padding: 18px 15px;
  text-align: left;
  color: var(--ink);
  cursor: pointer;
  position: relative;
}
.mode-card:active { transform: scale(.985); }
.mode-icon { display: block; font-size: 2.15rem; margin-bottom: 12px; }
.mode-card strong { display: block; font-size: 1.08rem; margin-bottom: 5px; }
.mode-card small { color: var(--muted); line-height: 1.45; }
.level-chip {
  position: absolute;
  top: 14px;
  right: 13px;
  padding: 5px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--primary-dark);
  font-weight: 900;
  font-size: .75rem;
}

.primary-btn, .secondary-btn, .danger-btn {
  width: 100%;
  min-height: 58px;
  border-radius: 18px;
  border: 0;
  padding: 13px 18px;
  font-weight: 900;
  cursor: pointer;
}
.primary-btn { background: var(--primary); color: #fff; box-shadow: 0 8px 18px rgba(79,70,229,.22); }
.secondary-btn { background: var(--surface-2); color: var(--primary-dark); border: 1px solid var(--line); }
.danger-btn { background: #fff0f0; color: var(--bad); border: 1px solid #f2c6ca; }
.wide-action { margin-top: 14px; }

.game-head {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: start;
  margin-bottom: 14px;
}
.game-head h1 { margin: 0 0 6px; font-size: 1.6rem; }
.game-head p { margin: 0; color: var(--muted); line-height: 1.5; }
.score-box {
  min-width: 78px;
  padding: 9px 10px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  text-align: center;
}
.score-box small { display: block; color: var(--muted); }
.score-box strong { font-size: 1.15rem; }

.progress {
  height: 10px;
  background: var(--surface-2);
  border-radius: 999px;
  overflow: hidden;
  margin: 12px 0 18px;
}
.progress > span {
  display: block;
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--accent), var(--primary));
  transition: width .25s linear;
}

.instruction {
  min-height: 58px;
  padding: 14px 16px;
  margin-bottom: 14px;
  border-radius: 18px;
  background: var(--surface-2);
  color: var(--primary-dark);
  font-weight: 900;
  line-height: 1.5;
  text-align: center;
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 9px;
}
.memory-card {
  min-height: 100px;
  padding: 7px 4px;
  border: 2px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  cursor: pointer;
  overflow: hidden;
}
.memory-card .emoji { font-size: clamp(2rem, 10vw, 3.5rem); line-height: 1; }
.memory-card .word { font-weight: 900; font-size: clamp(.85rem, 3.5vw, 1.08rem); overflow-wrap: anywhere; }
.memory-card .lang { color: var(--muted); font-size: .68rem; font-weight: 800; }
.memory-card.covered { background: var(--primary); color: white; font-size: 2.6rem; }
.memory-card.selectable:active { transform: scale(.96); }
.memory-card.selected { border-color: var(--accent); box-shadow: 0 0 0 4px rgba(245,158,11,.22); }
.memory-card.correct { border-color: var(--good); background: #e9f8ef; }
.memory-card.wrong { border-color: var(--bad); background: #fff0f1; }

.sequence-board {
  min-height: 142px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 16px;
  margin-bottom: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}
.sequence-token {
  width: 66px;
  height: 76px;
  border: 2px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  display: grid;
  place-items: center;
  font-size: 2.2rem;
  cursor: pointer;
}
.sequence-token.picked { opacity: .35; transform: scale(.92); }
.answer-slots {
  min-height: 86px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 7px;
  margin-bottom: 14px;
}
.answer-slot {
  width: 52px;
  height: 62px;
  border: 2px dashed var(--line);
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: rgba(255,255,255,.5);
  font-size: 1.8rem;
}

.piano {
  display: grid;
  grid-template-columns: repeat(var(--keys, 5), minmax(0, 1fr));
  gap: 5px;
  height: 240px;
  padding: 10px;
  border-radius: 24px;
  background: #282638;
}
.piano-key {
  border: 0;
  border-radius: 0 0 13px 13px;
  background: linear-gradient(#fff, #e9e6ef);
  color: #3b3950;
  font-weight: 900;
  font-size: 1rem;
  cursor: pointer;
  box-shadow: inset 0 -8px 0 #d7d3df;
}
.piano-key.active {
  background: linear-gradient(#ffe899, #f5b83e);
  box-shadow: inset 0 -6px 0 #d28b18;
  transform: translateY(3px);
}

.number-display {
  min-height: 190px;
  display: grid;
  place-items: center;
  margin-bottom: 16px;
  border-radius: 28px;
  background: linear-gradient(145deg, var(--primary), var(--primary-dark));
  color: white;
  font-size: clamp(4.8rem, 24vw, 8rem);
  font-weight: 900;
  box-shadow: var(--shadow);
}
.number-answer {
  min-height: 66px;
  padding: 12px 15px;
  margin-bottom: 13px;
  border-radius: 18px;
  background: var(--surface);
  border: 2px solid var(--line);
  text-align: center;
  font-size: 2rem;
  letter-spacing: .18em;
  font-weight: 900;
}
.keypad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 9px;
}
.keypad button {
  min-height: 62px;
  border: 1px solid var(--line);
  border-radius: 17px;
  background: var(--surface);
  color: var(--ink);
  font-size: 1.35rem;
  font-weight: 900;
}

.color-stage {
  min-height: 210px;
  display: grid;
  place-items: center;
  margin-bottom: 16px;
  border-radius: 28px;
  background: var(--surface);
  border: 1px solid var(--line);
}
.color-flash {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  box-shadow: inset 0 -14px 24px rgba(0,0,0,.15), 0 12px 30px rgba(30,30,50,.12);
}
.color-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.color-btn {
  min-height: 76px;
  border: 4px solid white;
  border-radius: 20px;
  box-shadow: 0 5px 14px rgba(30,30,50,.14);
  cursor: pointer;
}
.color-btn.active { outline: 5px solid var(--ink); transform: scale(.95); }

.result-card {
  padding: 26px 20px;
  border-radius: 30px;
  background: var(--surface);
  border: 1px solid var(--line);
  text-align: center;
  box-shadow: var(--shadow);
}
.result-icon { font-size: 4rem; }
.result-card h1 { margin: 8px 0; }
.result-card p { color: var(--muted); line-height: 1.6; }
.result-score { font-size: 3rem; color: var(--primary); font-weight: 900; margin: 16px 0 4px; }
.result-actions { display: grid; gap: 10px; margin-top: 20px; }

.mini-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 18px 0;
}
.mini-stats div {
  padding: 12px 6px;
  border-radius: 16px;
  background: var(--surface-2);
}
.mini-stats strong { display: block; font-size: 1.3rem; }
.mini-stats small { color: var(--muted); }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(calc(100% - 36px), 560px);
  padding: 15px 18px;
  border-radius: 16px;
  background: #262437;
  color: white;
  font-weight: 800;
  text-align: center;
  z-index: 20;
  box-shadow: 0 12px 30px rgba(0,0,0,.22);
}

.disclaimer {
  margin: 22px 4px 0;
  color: var(--muted);
  font-size: .78rem;
  line-height: 1.55;
  text-align: center;
}

@media (max-width: 430px) {
  .screen { padding-inline: 12px; }
  .cards-grid { gap: 6px; }
  .memory-card { min-height: 88px; border-radius: 15px; }
  .settings-grid { grid-template-columns: 1fr; }
}

@media (min-width: 700px) {
  .mode-grid { grid-template-columns: repeat(3, 1fr); }
  .cards-grid { gap: 12px; }
  .memory-card { min-height: 124px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .001ms !important; animation-duration: .001ms !important; }
}
