:root {
  color-scheme: light;
  --bg: #fff8ec;
  --ink: #2b2140;
  --muted: #6b6280;
  --line: #eadfcc;
  --card: #ffffff;
  --pink: #ff5d8f;
  --orange: #ff9f1c;
  --yellow: #ffd23f;
  --green: #2ec4b6;
  --blue: #3a86ff;
  --purple: #8338ec;
  --red: #e63946;
  --term-bg: #e8f1ff;
  --term-line: #3a86ff;
  --ans-bg: #fff1e0;
  --ans-line: #ff9f1c;
  --radius: 18px;
  --shadow: 0 4px 0 rgba(43, 33, 64, .12);
  --font-title: 'Jua', 'Nanum Gothic', system-ui, sans-serif;
  --font-body: 'Nanum Gothic', system-ui, -apple-system, 'Malgun Gothic', sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  background-image: radial-gradient(circle at 10% 10%, #ffe3ef 0 120px, transparent 121px),
                    radial-gradient(circle at 95% 25%, #dff6f3 0 160px, transparent 161px),
                    radial-gradient(circle at 80% 95%, #efe4ff 0 140px, transparent 141px);
  background-attachment: fixed;
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 18px;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { font-family: var(--font-title); font-weight: 400; margin: 0 0 .4em; }
.hidden { display: none !important; }
.loading { padding: 40px; text-align: center; color: var(--muted); }

#app { min-height: 100%; }
.page { max-width: 1100px; margin: 0 auto; padding: 16px 16px 48px; }

/* ---------- 버튼 ---------- */
.btn {
  border: none; border-radius: 14px; padding: 12px 20px; font-weight: 800;
  background: var(--blue); color: #fff; box-shadow: 0 4px 0 rgba(0,0,0,.18);
  transition: transform .08s, box-shadow .08s; display: inline-flex; align-items: center; gap: 6px; justify-content: center;
  min-height: 48px; text-decoration: none;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,.18); }
.btn:disabled { background: #b9b3c6 !important; color: #fff; cursor: not-allowed; box-shadow: none; transform: none; }
.btn.pink { background: var(--pink); }
.btn.orange { background: var(--orange); color: #2b2140; }
.btn.green { background: var(--green); color: #fff; }
.btn.purple { background: var(--purple); }
.btn.red { background: var(--red); }
.btn.ghost { background: #fff; color: var(--ink); border: 2px solid var(--line); box-shadow: 0 3px 0 var(--line); }
.btn.big { font-size: 1.4rem; padding: 16px 34px; min-height: 64px; border-radius: 20px; }
.btn.small { font-size: .9rem; padding: 8px 12px; min-height: 38px; border-radius: 10px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }

/* ---------- 상단 바 ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 12px 16px; background: #fff; border-bottom: 3px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-family: var(--font-title); font-size: 1.5rem; color: var(--purple); white-space: nowrap; }
.me-badge {
  border: none; background: linear-gradient(135deg, var(--yellow), var(--orange)); color: #2b2140;
  font-weight: 800; padding: 8px 14px; border-radius: 999px; box-shadow: var(--shadow); white-space: nowrap;
}
.teacher-badge { background: var(--purple); color: #fff; font-weight: 800; padding: 6px 12px; border-radius: 999px; font-size: .9rem; }

/* ---------- 로그인 ---------- */
.hero { text-align: center; padding: 28px 12px 8px; }
.hero h1 { font-size: clamp(2rem, 6vw, 3.4rem); color: var(--purple); text-shadow: 3px 3px 0 #ffd23f; }
.hero p { color: var(--muted); font-size: 1.1rem; margin: 0; }
.num-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 12px;
  max-width: 900px; margin: 24px auto;
}
.num-btn {
  aspect-ratio: 1; border: none; border-radius: 20px; font-family: var(--font-title); font-size: 2rem;
  color: #fff; box-shadow: 0 5px 0 rgba(0,0,0,.18); transition: transform .08s;
}
.num-btn small { display: block; font-size: .9rem; font-family: var(--font-body); font-weight: 800; }
.num-btn:active { transform: translateY(4px); box-shadow: 0 1px 0 rgba(0,0,0,.18); }
.teacher-entry { position: fixed; right: 14px; bottom: 14px; z-index: 5; }

/* ---------- 게임 목록 ---------- */
.section-title { display: flex; align-items: center; gap: 10px; margin: 18px 0 12px; flex-wrap: wrap; }
.section-title h2 { margin: 0; font-size: 1.8rem; }
.game-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; }
.game-card {
  background: #fff; border-radius: var(--radius); padding: 18px; border: 3px solid var(--line);
  box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 10px; text-align: left;
  position: relative; overflow: hidden;
}
.game-card::before { content: ''; position: absolute; inset: 0 0 auto 0; height: 8px; background: var(--accent, var(--pink)); }
button.game-card { cursor: pointer; transition: transform .1s; }
button.game-card:hover { transform: translateY(-3px) rotate(-.5deg); }
.game-card h3 { font-size: 1.45rem; margin: 6px 0 0; word-break: keep-all; overflow-wrap: anywhere; }
.meta { color: var(--muted); font-size: .95rem; display: flex; gap: 8px; flex-wrap: wrap; }
.pill { background: #f3eefc; color: var(--purple); border-radius: 999px; padding: 3px 10px; font-weight: 800; font-size: .85rem; }
.best { font-weight: 800; color: #c2410c; }
.empty { text-align: center; padding: 40px 16px; color: var(--muted); background: #fff; border-radius: var(--radius); border: 3px dashed var(--line); }

/* ---------- 매칭 게임 ---------- */
body.play-mode { overflow: hidden; }
body.play-mode #app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
.test-banner {
  background: repeating-linear-gradient(45deg, #ffd23f 0 14px, #ffe27a 14px 28px); color: #2b2140;
  text-align: center; font-weight: 800; padding: 6px 10px; font-size: .95rem; flex: none;
}
.play-top {
  flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 10px; background: #fff;
  border-bottom: 2px solid var(--line); min-height: 46px;
}
.icon-btn { border: 2px solid var(--line); background: #fff; border-radius: 10px; width: 38px; height: 34px; font-weight: 800; flex: none; }
.play-title { font-weight: 800; font-size: .95rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 1; }
.play-stats { display: flex; gap: 6px; align-items: center; flex: none; }
.chip { font-weight: 800; font-size: .85rem; border-radius: 999px; padding: 4px 10px; color: #fff; animation: chipIn .25s ease-out; white-space: nowrap; }
.chip.combo { background: var(--pink); }
.chip.speed { background: var(--purple); }
.score-badge { background: var(--ink); color: #fff; font-family: var(--font-title); font-size: 1.15rem; padding: 3px 14px; border-radius: 999px; min-width: 70px; text-align: center; white-space: nowrap; }
.score-badge.bump { animation: bump .35s; }
@keyframes chipIn { from { transform: scale(.4); opacity: 0; } }
@keyframes bump { 40% { transform: scale(1.3); background: var(--green); } }

.board-wrap { flex: 1; min-height: 0; position: relative; overflow: hidden; }
.board-wrap.scroll { overflow-y: auto; }
.board { display: grid; justify-content: center; align-content: center; padding: 10px; min-height: 100%; }
.board-wrap.scroll .board { align-content: start; }
.board.shake { animation: shake .4s; }
@keyframes shake { 20%, 60% { transform: translateX(-10px); } 40%, 80% { transform: translateX(10px); } }

.mcard {
  position: relative; border-radius: 14px; border: 3px solid; display: flex; align-items: center; justify-content: center;
  padding: 7%; overflow: hidden; user-select: none; -webkit-user-select: none; cursor: pointer;
  box-shadow: 0 4px 0 rgba(43,33,64,.15); transition: transform .12s, box-shadow .12s, opacity .35s, background .15s;
  touch-action: manipulation;
}
.mcard.term { background: var(--term-bg); border-color: var(--term-line); }
.mcard.answer { background: var(--ans-bg); border-color: var(--ans-line); }
.mcard .txt {
  width: 100%; text-align: center; font-weight: 800; line-height: 1.2; word-break: keep-all; overflow-wrap: normal; color: #1f1933;
}
.mcard .txt.wrap-any { overflow-wrap: anywhere; word-break: break-all; }
.mcard:hover { transform: translateY(-2px); }
.mcard.selected { background: #fff36b; border-color: #e0a800; transform: scale(1.04); box-shadow: 0 0 0 4px rgba(255,210,63,.6), 0 6px 0 rgba(0,0,0,.15); z-index: 2; }
.mcard.wrong { background: #ffd6d9 !important; border-color: var(--red) !important; animation: shake .35s; }
.mcard.matched { animation: pop .45s forwards; pointer-events: none; }
.mcard.gone { opacity: 0; pointer-events: none; animation: none; }
@keyframes pop { 0% { transform: scale(1); background: #b8f5d8; border-color: #10b981; } 40% { transform: scale(1.12); background: #b8f5d8; border-color: #10b981; } 100% { transform: scale(.7); opacity: 0; } }
.mcard.hint { animation: wiggle .6s ease-in-out infinite; border-color: #10b981; box-shadow: 0 0 0 4px rgba(16,185,129,.35); }
.mcard.hint.selected { animation: none; }
@keyframes wiggle { 0%, 100% { transform: rotate(0); } 25% { transform: rotate(-4deg); } 75% { transform: rotate(4deg); } }
.hint-tag {
  position: absolute; top: 3px; right: 4px; font-size: 12px; font-weight: 800; background: #10b981; color: #fff;
  border-radius: 999px; padding: 1px 7px; white-space: nowrap; max-width: calc(100% - 8px); overflow: hidden; text-overflow: ellipsis;
}

.start-overlay {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  background: rgba(255, 248, 236, .97); backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); text-align: center; padding: 20px; z-index: 5;
}
.start-overlay h1 { font-size: clamp(1.8rem, 6vw, 3rem); color: var(--purple); word-break: keep-all; }
.start-overlay p { margin: 0; color: var(--muted); font-weight: 700; }
.rules { background: #fff; border: 3px solid var(--line); border-radius: 16px; padding: 12px 16px; font-size: .95rem; text-align: left; max-width: 460px; line-height: 1.6; color: var(--ink); }

/* 효과 */
#fx-root { position: fixed; inset: 0; pointer-events: none; z-index: 50; overflow: hidden; }
.float-pts { position: fixed; font-family: var(--font-title); font-size: 2rem; color: #10b981; text-shadow: 2px 2px 0 #fff, -2px -2px 0 #fff; animation: floatUp 1s ease-out forwards; transform: translate(-50%, -50%); }
.float-pts.minus { color: var(--red); }
@keyframes floatUp { to { transform: translate(-50%, -160%); opacity: 0; } }
.banner-fx {
  position: fixed; left: 50%; top: 38%; transform: translate(-50%, -50%); font-family: var(--font-title);
  font-size: clamp(2.2rem, 9vw, 5rem); white-space: nowrap; animation: comboIn .9s ease-out forwards;
  -webkit-text-stroke: 2px #fff; paint-order: stroke fill; text-shadow: 0 6px 0 rgba(0,0,0,.15);
}
.banner-fx.speed { top: 52%; font-size: clamp(1.6rem, 6vw, 3.2rem); color: var(--purple); }
.banner-fx.small { font-size: clamp(1.2rem, 4vw, 2rem); top: 50%; color: var(--red); }
.banner-fx.hint { top: 16%; font-size: clamp(1.2rem, 4vw, 2rem); color: #059669; }
@keyframes comboIn { 0% { transform: translate(-50%, -50%) scale(.3) rotate(-8deg); opacity: 0; } 25% { transform: translate(-50%, -50%) scale(1.15) rotate(3deg); opacity: 1; } 70% { transform: translate(-50%, -50%) scale(1); opacity: 1; } 100% { transform: translate(-50%, -70%) scale(1); opacity: 0; } }
.confetti { position: fixed; top: -20px; width: 10px; height: 16px; border-radius: 3px; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

/* ---------- 결과 ---------- */
.result { text-align: center; padding-top: 24px; }
.result .game-name { font-size: clamp(1.8rem, 6vw, 3rem); color: var(--purple); word-break: keep-all; }
.result .final { font-family: var(--font-title); font-size: clamp(2.4rem, 10vw, 5rem); color: var(--pink); text-shadow: 4px 4px 0 var(--yellow); margin: 8px 0; }
.result .combo-line { font-size: 1.3rem; font-weight: 800; }
.panel { background: #fff; border: 3px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin: 18px auto; max-width: 560px; text-align: left; }
.panel h3 { font-size: 1.4rem; }
.notice { background: #fff6d6; border: 3px dashed var(--orange); border-radius: 16px; padding: 14px; font-weight: 800; max-width: 560px; margin: 18px auto; color: #5b3d00; }
.lb-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.lb-list li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px; background: #faf6ef; font-weight: 800; }
.lb-list li.me { background: #fff36b; outline: 3px solid var(--orange); }
.lb-rank { font-family: var(--font-title); font-size: 1.3rem; width: 56px; }
.lb-score { margin-left: auto; color: #c2410c; }

/* ---------- 표 ---------- */
.table-wrap { overflow-x: auto; background: #fff; border-radius: var(--radius); border: 3px solid var(--line); }
table { width: 100%; border-collapse: collapse; font-size: 1rem; }
th, td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
th { background: #f7f0ff; font-weight: 800; white-space: nowrap; color: var(--ink); }
tr.best-row td { background: #fff7cf; font-weight: 800; }
tr.err-row td { background: #ffe8ea; }
td.num { width: 52px; color: var(--muted); font-weight: 800; }
td.cell-edit { min-width: 140px; cursor: text; }
td.cell-edit:focus { outline: 3px solid var(--blue); outline-offset: -3px; background: #eef5ff; }
td.cell-edit:empty::before { content: '(클릭해서 입력)'; color: #b3a9c4; }
.del-btn { border: none; background: #ffe1e4; color: var(--red); font-weight: 800; border-radius: 10px; width: 36px; height: 36px; }
.err-msg { color: var(--red); font-weight: 800; font-size: .9rem; }

/* ---------- 교사 ---------- */
.tabs { display: flex; gap: 8px; flex-wrap: wrap; margin: 16px 0; }
.tab { border: 3px solid var(--line); background: #fff; border-radius: 14px; padding: 10px 18px; font-weight: 800; font-size: 1.05rem; }
.tab.on { background: var(--purple); border-color: var(--purple); color: #fff; }
.form-block { background: #fff; border: 3px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 18px; }
.form-block h3 { font-size: 1.4rem; }
label.field { display: block; font-weight: 800; margin: 10px 0 6px; }
.input, textarea.input, select.input {
  width: 100%; border: 3px solid var(--line); border-radius: 12px; padding: 10px 12px; background: #fffdf8; color: var(--ink);
}
.input:focus { outline: none; border-color: var(--blue); background: #fff; }
textarea.input { min-height: 180px; resize: vertical; line-height: 1.5; font-family: var(--font-body); }
textarea.names { min-height: 320px; }
.help { color: var(--muted); font-size: .95rem; margin: 6px 0; line-height: 1.5; }
.parse-summary { font-weight: 800; margin: 10px 0; }
.parse-summary .ok { color: #059669; }
.parse-summary .bad { color: var(--red); }
.errors { background: #ffe8ea; color: #9b1c27; border-radius: 12px; padding: 10px 14px; font-weight: 700; margin: 8px 0; }
.errors div { margin: 2px 0; }
.msg-ok { color: #059669; font-weight: 800; }
.msg-err { color: var(--red); font-weight: 800; }
.inline-num { width: 110px; display: inline-block; text-align: center; font-size: 1.2rem; font-weight: 800; }
.reg-title { font-size: 1.5rem; }
.reg-note { font-weight: 800; color: #5b3d00; background: #fff6d6; border-radius: 12px; padding: 10px 14px; margin: 6px 0 10px; }

/* 막대 그래프 */
.bars { display: flex; flex-direction: column; gap: 8px; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 160px) 1fr 70px; gap: 10px; align-items: center; font-weight: 800; }
.bar-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { background: #f3eefc; border-radius: 999px; height: 26px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 999px; animation: grow .8s ease-out; transform-origin: left; }
@keyframes grow { from { transform: scaleX(0); } }
.bar-val { text-align: right; color: #c2410c; }

/* ---------- 팝업 (항상 또렷하게: 배경/글자색 고정) ---------- */
.modal-back {
  position: fixed; inset: 0; background: rgba(20, 14, 35, .62); display: flex; align-items: center; justify-content: center;
  padding: 16px; z-index: 100; animation: fadeIn .15s;
}
.modal {
  background: #ffffff !important; color: #1f1933 !important; border-radius: 22px; padding: 22px; width: 100%; max-width: 440px;
  border: 4px solid #2b2140; box-shadow: 0 12px 0 rgba(0,0,0,.25); animation: popIn .2s ease-out;
}
.modal * { color: inherit; }
.modal h3 { font-size: 1.5rem; color: #1f1933 !important; margin-bottom: 10px; }
.modal p { line-height: 1.55; margin: 0 0 12px; color: #2b2140 !important; }
.modal .input { background: #ffffff; color: #1f1933 !important; border-color: #b9b0cc; font-size: 1.4rem; text-align: center; letter-spacing: .3em; }
.modal .modal-err { color: #c1121f !important; font-weight: 800; min-height: 1.4em; margin: 8px 0 0; }
.modal .btns { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }
.modal .btn { color: #ffffff !important; }
.modal .btn.ghost { background: #f1edf7 !important; color: #1f1933 !important; border-color: #cfc6de; }
.modal .btn.orange { color: #1f1933 !important; }
@keyframes fadeIn { from { opacity: 0; } }
@keyframes popIn { from { transform: scale(.85); opacity: 0; } }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #2b2140; color: #fff; padding: 12px 20px; border-radius: 14px; font-weight: 800; z-index: 120; animation: fadeIn .2s; max-width: calc(100% - 32px); text-align: center; }
.toast.err { background: #c1121f; }

@media (max-width: 600px) {
  body { font-size: 16px; }
  .num-grid { grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 9px; }
  .num-btn { font-size: 1.6rem; border-radius: 16px; }
  .btn.big { font-size: 1.2rem; padding: 14px 24px; }
  .chip { font-size: .75rem; padding: 3px 7px; }
  .score-badge { font-size: 1rem; min-width: 56px; padding: 3px 10px; }
  .bar-row { grid-template-columns: 80px 1fr 56px; }
  th, td { padding: 8px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition: none !important; }
}
