:root {
  --bg1: #fff6d6;
  --bg2: #ffe3ef;
  --ink: #2b2140;
  --ink-soft: #5e5476;
  --card: #ffffff;
  --line: #ecdff5;
  --pink: #ff5d8f;
  --orange: #ff9f1c;
  --yellow: #ffd23f;
  --green: #2ec4b6;
  --blue: #3a86ff;
  --purple: #8338ec;
  --red: #e63946;
  --radius: 18px;
  --shadow: 0 6px 0 rgba(43, 33, 64, 0.12), 0 10px 24px rgba(43, 33, 64, 0.08);
  --topbar-h: 52px;
  font-size: 17px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: 'Jua', 'Apple SD Gothic Neo', 'Malgun Gothic', system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(160deg, var(--bg1), var(--bg2)) fixed;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }

/* ---------- 상단바 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 10px;
  padding: 0 14px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(8px);
  border-bottom: 2px solid var(--line);
}
.topbar .brand { font-size: 1.15rem; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 14px; border-radius: 999px; border: 0;
  background: var(--purple); color: #fff; font-size: 1rem;
  box-shadow: 0 3px 0 rgba(0,0,0,.15);
}
.topbar .btn, .topbar .chip { white-space: nowrap; flex: none; }
.chip.light { background: #f1e9ff; color: var(--purple); box-shadow: none; }
.chip.teacher { background: var(--green); }

/* ---------- 레이아웃 ---------- */
main { min-height: calc(100vh - var(--topbar-h)); }
.page { max-width: 1100px; margin: 0 auto; padding: 22px 16px 60px; }
.page h1 { font-size: clamp(1.7rem, 4.5vw, 2.6rem); margin: 6px 0 18px; text-align: center; }
.page h2 { font-size: 1.4rem; margin: 26px 0 12px; }
.sub { color: var(--ink-soft); text-align: center; margin: -8px 0 20px; font-size: 1.05rem; }
.panel {
  background: var(--card); border-radius: var(--radius); padding: 18px;
  box-shadow: var(--shadow); margin-bottom: 18px;
}
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }
.muted { color: var(--ink-soft); }
.center { text-align: center; }

/* ---------- 버튼 ---------- */
.btn {
  border: 0; border-radius: 14px; padding: 12px 20px; font-size: 1.1rem;
  background: var(--blue); color: #fff; box-shadow: 0 4px 0 rgba(0,0,0,.18);
  transition: transform .08s, box-shadow .08s, filter .15s;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 48px;
}
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,.18); }
.btn:disabled { background: #b9b3c6; cursor: not-allowed; filter: none; transform: none; }
.btn.pink { background: var(--pink); }
.btn.orange { background: var(--orange); }
.btn.green { background: var(--green); }
.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.5rem; padding: 16px 34px; min-height: 64px; border-radius: 18px; }
.btn.small { font-size: .95rem; padding: 8px 12px; min-height: 38px; border-radius: 10px; }

/* ---------- 시작 화면 ---------- */
.hero-emoji { font-size: 3.2rem; text-align: center; margin-top: 8px; }
.num-grid {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(78px, 1fr));
}
.num-btn {
  aspect-ratio: 1; border: 0; border-radius: 20px; font-size: 1.9rem; color: #fff;
  box-shadow: 0 5px 0 rgba(0,0,0,.18); transition: transform .08s;
}
.num-btn:active { transform: translateY(3px) scale(.97); }
.num-btn small { display: block; font-size: .8rem; opacity: .9; }
.teacher-link { display: block; margin: 30px auto 0; }

/* ---------- 게임 목록 카드 ---------- */
.game-grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.game-card {
  background: #fff; border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow);
  border: 0; text-align: left; display: flex; flex-direction: column; gap: 8px;
  border-top: 8px solid var(--c, var(--pink)); min-height: 150px;
  transition: transform .12s;
}
button.game-card:hover { transform: translateY(-3px); }
.game-card .title { font-size: 1.35rem; line-height: 1.3; word-break: keep-all; overflow-wrap: anywhere; }
.game-card .meta { color: var(--ink-soft); font-size: .98rem; }
.game-card .best { margin-top: auto; font-size: 1.05rem; color: var(--purple); }
.empty { text-align: center; color: var(--ink-soft); padding: 40px 10px; font-size: 1.15rem; }

/* ---------- 표 ---------- */
.table-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; font-family: system-ui, 'Malgun Gothic', sans-serif; font-size: 1rem; }
.tbl th, .tbl td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.tbl th { background: #faf5ff; font-family: 'Jua', sans-serif; font-weight: normal; white-space: nowrap; }
.tbl tr.best td { background: #fff8d9; font-weight: 600; }
.tbl tr.me td { background: #e6f7ff; font-weight: 700; }
.tbl tr.err td { background: #ffecee; color: #9b1c27; }
.tbl td.num { white-space: nowrap; }
.tbl .cell-input {
  width: 100%; min-width: 90px; border: 2px solid transparent; border-radius: 8px; padding: 6px 8px;
  background: transparent; font-family: system-ui, 'Malgun Gothic', sans-serif; font-size: 1rem;
}
.tbl .cell-input:hover { border-color: var(--line); background: #fff; }
.tbl .cell-input:focus { outline: none; border-color: var(--blue); background: #fff; }
.x-btn {
  border: 0; background: #ffe1e6; color: var(--red); width: 34px; height: 34px; border-radius: 10px; font-size: 1.1rem;
}

/* ---------- 폼 ---------- */
label.field { display: block; margin-bottom: 14px; }
label.field > span { display: block; margin-bottom: 6px; font-size: 1.05rem; }
.input, textarea.input, select.input {
  width: 100%; padding: 12px 14px; border: 2px solid var(--line); border-radius: 12px; background: #fff;
  font-family: system-ui, 'Malgun Gothic', sans-serif; font-size: 1.05rem;
}
.input:focus { outline: none; border-color: var(--blue); }
textarea.input { min-height: 180px; resize: vertical; line-height: 1.55; }
.input.short { width: 110px; display: inline-block; text-align: center; }
.err-list { color: #b0182a; background: #ffecee; border-radius: 12px; padding: 10px 14px; margin: 10px 0; font-family: system-ui, sans-serif; }
.err-list div { margin: 3px 0; }
.ok-msg { color: #0b7a55; margin-top: 8px; }
.form-err { color: #b0182a; margin-top: 8px; min-height: 1.2em; }
.tabs { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; margin-bottom: 18px; }
.tabs .btn.active { background: var(--purple); }

/* ---------- 모달 (배경과 관계없이 항상 선명한 대비) ---------- */
.modal-back {
  position: fixed; inset: 0; z-index: 100; background: rgba(20, 12, 35, 0.6);
  display: flex; align-items: center; justify-content: center; padding: 16px;
  animation: fade .15s;
}
.modal {
  background: #ffffff !important; color: #1d1530 !important;
  width: min(440px, 100%); border-radius: 22px; padding: 24px 22px 20px;
  box-shadow: 0 20px 60px rgba(0,0,0,.35); border: 3px solid #1d1530;
  animation: pop .2s;
}
.modal * { color: inherit; }
.modal h3 { margin: 0 0 10px; font-size: 1.45rem; color: #1d1530 !important; }
.modal p { margin: 0 0 14px; font-size: 1.08rem; line-height: 1.5; color: #2f2547 !important; }
.modal .input { color: #1d1530 !important; background: #fff !important; border-color: #9b8fb5; font-size: 1.4rem; text-align: center; letter-spacing: .3em; }
.modal .modal-err { color: #c1121f !important; background: #ffe5e8; border-radius: 10px; padding: 8px 10px; margin-top: 10px; font-size: 1rem; }
.modal .actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }
.modal .btn { color: #ffffff !important; }
.modal .btn.ghost { color: #1d1530 !important; background: #f1edf7 !important; border-color: #c9bfdc; }

/* ---------- 토스트 ---------- */
#toast-root { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 120; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { background: #1d1530; color: #fff; padding: 12px 20px; border-radius: 14px; font-size: 1.05rem; box-shadow: 0 8px 20px rgba(0,0,0,.25); animation: pop .2s; }

/* ---------- 게임 화면 ---------- */
.game-wrap { display: flex; flex-direction: column; height: calc(100dvh - var(--topbar-h)); }
.test-banner {
  background: repeating-linear-gradient(45deg, #ffd23f, #ffd23f 14px, #ffc300 14px, #ffc300 28px);
  color: #2b2140; text-align: center; padding: 7px 10px; font-size: 1.05rem; flex: none;
  border-bottom: 2px solid #e0a800;
}
.status-line {
  flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 12px; min-height: 44px;
}
.status-line .g-title { flex: 1; font-size: 1rem; color: var(--ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status-line .quit { border: 0; background: #fff; border-radius: 10px; width: 34px; height: 34px; box-shadow: 0 2px 0 var(--line); font-size: 1rem; }
.badge { padding: 5px 12px; border-radius: 999px; background: var(--ink); color: #fff; font-size: 1.05rem; white-space: nowrap; transition: transform .15s; }
.badge.score.bump { animation: bump .35s; }
.badge.combo { background: var(--pink); animation: pop .2s; }
.badge.speed { background: var(--orange); animation: pop .2s; }
.board-area { flex: 1; min-height: 0; position: relative; padding: 4px 10px 10px; }
.board-area.scroll { overflow-y: auto; }
.board { display: grid; gap: 10px; margin: 0 auto; }
.board.shake { animation: shake .4s; }
.mcard {
  position: relative; border: 0; border-radius: 16px; padding: 8px;
  background: #fff; box-shadow: 0 5px 0 rgba(43,33,64,.15);
  display: flex; align-items: center; justify-content: center; text-align: center;
  overflow: hidden; transition: transform .12s, box-shadow .12s, opacity .35s, background .2s;
  border: 3px solid transparent;
}
.mcard.term { background: #fff; border-color: #ffd0de; }
.mcard.answer { background: #eef6ff; border-color: #c6dcff; }
.mcard .txt {
  display: block; width: 100%; max-height: 100%; line-height: 1.2;
  word-break: keep-all; overflow-wrap: anywhere; font-family: 'Jua', 'Malgun Gothic', sans-serif;
}
.mcard:hover { transform: translateY(-2px); }
.mcard.selected { background: #fff3b0; border-color: var(--orange); transform: scale(1.04); box-shadow: 0 8px 0 rgba(255,159,28,.35); }
.mcard.wrong { background: #ffd6da; border-color: var(--red); animation: shake .35s; }
.mcard.matched { animation: matched .45s forwards; pointer-events: none; }
.mcard.gone { visibility: hidden; }
.mcard.hint { animation: wiggle .7s ease-in-out infinite; border-color: var(--yellow); box-shadow: 0 0 0 4px rgba(255,210,63,.7), 0 5px 0 rgba(43,33,64,.15); }
.mcard .hint-icon { position: absolute; top: 2px; right: 6px; font-size: 1.1rem; }

.pre-start { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; height: 100%; padding: 20px; text-align: center; }
.pre-start h1 { font-size: clamp(1.8rem, 6vw, 3rem); margin: 0; word-break: keep-all; }
.pre-start .rules { color: var(--ink-soft); font-size: 1.05rem; line-height: 1.7; }

/* 이펙트 */
.fx-layer { position: fixed; inset: 0; pointer-events: none; z-index: 60; overflow: hidden; }
.fx { position: absolute; transform: translate(-50%, -50%); font-size: 2rem; white-space: nowrap; text-shadow: 0 3px 0 #fff, 0 0 12px #fff; animation: floatUp 1.1s ease-out forwards; }
.fx.plus { color: var(--green); }
.fx.minus { color: var(--red); font-size: 2.4rem; }
.fx.combo { color: var(--pink); font-size: clamp(2rem, 7vw, 3.6rem); animation: comboPop 1.1s ease-out forwards; }
.fx.speed { color: var(--orange); font-size: clamp(1.5rem, 5vw, 2.4rem); animation: comboPop 1.1s ease-out forwards; }
.fx.info { color: #fff; background: rgba(29,21,48,.85); text-shadow: none; padding: 8px 16px; border-radius: 12px; font-size: 1.1rem; }
.confetti { position: absolute; top: -20px; width: 10px; height: 16px; border-radius: 3px; animation: fall linear forwards; }

/* ---------- 결과 화면 ---------- */
.result-score { text-align: center; font-size: clamp(2rem, 7vw, 3.4rem); margin: 6px 0; color: var(--pink); }
.result-combo { text-align: center; font-size: 1.4rem; color: var(--purple); margin-bottom: 18px; }
.lb-list { list-style: none; padding: 0; margin: 0; }
.lb-list li { display: flex; gap: 12px; align-items: center; padding: 10px 14px; border-radius: 12px; font-size: 1.2rem; }
.lb-list li:nth-child(odd) { background: #faf5ff; }
.lb-list li.me { background: #d9f2ff; outline: 3px solid var(--blue); }
.lb-list .rank { width: 3.2em; }
.lb-list .score { margin-left: auto; color: var(--purple); }

/* 막대 그래프 */
.bars { display: flex; flex-direction: column; gap: 8px; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 160px) 1fr; gap: 10px; align-items: center; }
.bar-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { background: #f3eefa; border-radius: 10px; height: 30px; position: relative; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 10px; display: flex; align-items: center; justify-content: flex-end; padding-right: 8px; color: #fff; font-size: .95rem; min-width: 40px; transition: width .6s; }

/* ---------- 애니메이션 ---------- */
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { transform: scale(.85); opacity: 0; } }
@keyframes bump { 50% { transform: scale(1.25); } }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-9px); } 40% { transform: translateX(9px); } 60% { transform: translateX(-6px); } 80% { transform: translateX(6px); } }
@keyframes wiggle { 0%,100% { transform: rotate(0); } 25% { transform: rotate(-4deg) scale(1.03); } 75% { transform: rotate(4deg) scale(1.03); } }
@keyframes matched { 0% { transform: scale(1.1); background: #b8f5d0; border-color: var(--green); } 100% { transform: scale(.6); opacity: 0; } }
@keyframes floatUp { 0% { opacity: 0; transform: translate(-50%, -30%) scale(.7); } 15% { opacity: 1; transform: translate(-50%, -50%) scale(1.1); } 100% { opacity: 0; transform: translate(-50%, -160%) scale(1); } }
@keyframes comboPop { 0% { opacity: 0; transform: translate(-50%, -50%) scale(.3) rotate(-8deg); } 20% { opacity: 1; transform: translate(-50%, -50%) scale(1.2) rotate(3deg); } 70% { opacity: 1; transform: translate(-50%, -60%) scale(1); } 100% { opacity: 0; transform: translate(-50%, -90%) scale(.9); } }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

@media (max-width: 600px) {
  :root { font-size: 16px; --topbar-h: 48px; }
  .page { padding: 16px 12px 50px; }
  .topbar { gap: 6px; padding: 0 8px; }
  .topbar .brand { display: none; }
  .topbar > :first-child:not(.brand), .topbar .brand + * { margin-left: auto; }
  .topbar .btn.small { padding: 6px 9px; font-size: .88rem; min-height: 34px; }
  .topbar .chip { padding: 6px 10px; font-size: .9rem; max-width: 46vw; overflow: hidden; text-overflow: ellipsis; }
  .num-grid { grid-template-columns: repeat(auto-fill, minmax(64px, 1fr)); gap: 9px; }
  .num-btn { font-size: 1.6rem; border-radius: 16px; }
  .board { gap: 7px; }
  .mcard { border-radius: 12px; padding: 5px; }
  .btn.big { font-size: 1.3rem; }
}
@media (prefers-reduced-motion: reduce) {
  .mcard.hint { animation: none; }
}
