:root {
  --bg: #fff8ec;
  --ink: #2b2350;
  --muted: #6b6490;
  --card: #ffffff;
  --line: #e8e1ff;
  --pink: #ff5fa2;
  --orange: #ff9f1c;
  --yellow: #ffd23f;
  --green: #2ec4b6;
  --blue: #3a86ff;
  --purple: #8338ec;
  --red: #ef476f;
  --radius: 18px;
  --shadow: 0 6px 0 rgba(43, 35, 80, 0.12), 0 10px 24px rgba(43, 35, 80, 0.08);
  --font-fun: 'Jua', 'Gowun Dodum', system-ui, sans-serif;
  --font-body: 'Gowun Dodum', 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%, rgba(255, 95, 162, 0.10) 0 120px, transparent 121px),
    radial-gradient(circle at 90% 20%, rgba(58, 134, 255, 0.10) 0 160px, transparent 161px),
    radial-gradient(circle at 80% 90%, rgba(46, 196, 182, 0.12) 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, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { font-family: var(--font-fun); font-weight: 400; margin: 0 0 .5em; }

/* ---------- 상단 바 ---------- */
#topbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px;
  background: linear-gradient(90deg, #8338ec, #3a86ff);
  color: #fff;
  box-shadow: 0 3px 0 rgba(0,0,0,.12);
  position: sticky; top: 0; z-index: 20;
}
#topbar.hidden { display: none; }
#topbar .brand { font-family: var(--font-fun); font-size: 24px; margin-right: auto; white-space: nowrap; }
#topbar .brand small { font-size: 14px; opacity: .85; margin-left: 6px; }
.tb-btn {
  border: 0; border-radius: 999px; padding: 8px 16px;
  background: rgba(255,255,255,.18); color: #fff;
  font-family: var(--font-fun); font-size: 18px;
}
.tb-btn:hover { background: rgba(255,255,255,.3); }
.tb-btn.active { background: #fff; color: #5b2bd6; }
.student-badge {
  border: 3px solid #fff; border-radius: 999px; padding: 6px 16px;
  background: var(--yellow); color: #2b2350;
  font-family: var(--font-fun); font-size: 20px;
}
.demo-flag { background: #ffd23f; color: #2b2350; border-radius: 8px; padding: 2px 8px; font-size: 13px; }

#view { padding: 20px 16px 40px; max-width: 1100px; margin: 0 auto; }
#view.full { padding: 0; max-width: none; }

/* ---------- 공통 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: 14px; padding: 12px 22px; min-height: 48px;
  font-family: var(--font-fun); font-size: 20px;
  background: var(--blue); color: #fff;
  box-shadow: 0 4px 0 rgba(0,0,0,.18);
  transition: transform .08s, box-shadow .08s, filter .15s;
}
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,.18); }
.btn:disabled { background: #c9c4dc !important; color: #fff; cursor: not-allowed; box-shadow: none; transform: none; }
.btn.pink { background: var(--pink); }
.btn.green { background: #10a89a; }
.btn.orange { background: #f08c00; }
.btn.purple { background: var(--purple); }
.btn.red { background: #d93a5f; }
.btn.ghost { background: #fff; color: var(--ink); border: 2px solid var(--line); box-shadow: 0 3px 0 var(--line); }
.btn.small { font-size: 16px; padding: 8px 14px; min-height: 40px; border-radius: 12px; }
.btn.big { font-size: 28px; padding: 16px 40px; border-radius: 20px; }

.panel {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 20px; margin-bottom: 20px; border: 2px solid var(--line);
}
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.muted { color: var(--muted); }
.page-title { font-size: 32px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-family: var(--font-fun); font-size: 18px; }
.input, select.input, textarea.input {
  border: 2px solid #d6ccff; border-radius: 12px; padding: 10px 12px; background: #fff; color: #2b2350;
  font-size: 18px; width: 100%;
}
.input:focus { outline: 3px solid #b79cff; border-color: #8338ec; }
textarea.input { min-height: 160px; resize: vertical; line-height: 1.5; }
.empty { text-align: center; padding: 40px 10px; color: var(--muted); font-size: 20px; }
.spinner { text-align: center; padding: 30px; font-size: 22px; color: var(--muted); }

/* ---------- 시작 화면 ---------- */
.hero { text-align: center; margin: 10px 0 24px; }
.hero h1 { font-size: clamp(32px, 6vw, 54px); color: #5b2bd6; margin-bottom: 6px; text-shadow: 0 3px 0 #ffd23f; }
.hero p { font-size: 20px; margin: 0; }
.num-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 12px; }
.num-btn {
  aspect-ratio: 1 / 1; border: 0; border-radius: 20px;
  font-family: var(--font-fun); font-size: clamp(26px, 5vw, 36px); color: #fff;
  box-shadow: 0 5px 0 rgba(0,0,0,.18);
  transition: transform .1s;
}
.num-btn:hover { transform: translateY(-3px) rotate(-2deg); }
.num-btn:active { transform: translateY(2px); }
.num-btn small { display: block; font-size: 14px; opacity: .9; }
.teacher-link { text-align: center; margin-top: 30px; }

/* ---------- 게임 목록 ---------- */
.game-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; }
.game-card {
  border: 0; text-align: left; border-radius: 22px; padding: 20px; color: #fff; min-height: 150px;
  display: flex; flex-direction: column; gap: 8px; box-shadow: 0 6px 0 rgba(0,0,0,.18);
  transition: transform .12s; position: relative; overflow: hidden;
}
.game-card:hover { transform: translateY(-4px) rotate(-.5deg); }
.game-card .gc-title { font-family: var(--font-fun); font-size: 26px; line-height: 1.2; word-break: keep-all; overflow-wrap: anywhere; }
.game-card .gc-meta { font-size: 16px; opacity: .95; }
.game-card .gc-best { margin-top: auto; background: rgba(255,255,255,.25); border-radius: 12px; padding: 6px 10px; font-family: var(--font-fun); font-size: 18px; align-self: flex-start; }
.game-card::after { content: ''; position: absolute; right: -30px; bottom: -30px; width: 110px; height: 110px; border-radius: 50%; background: rgba(255,255,255,.15); }
.c0 { background: linear-gradient(135deg, #ff5fa2, #ff8a5c); }
.c1 { background: linear-gradient(135deg, #3a86ff, #5e60ce); }
.c2 { background: linear-gradient(135deg, #10a89a, #2ec4b6); }
.c3 { background: linear-gradient(135deg, #f08c00, #ffb703); }
.c4 { background: linear-gradient(135deg, #8338ec, #b5179e); }
.c5 { background: linear-gradient(135deg, #e63946, #ff5fa2); }

.t-game { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 14px 16px; border-radius: 16px; background: #fff; border: 2px solid var(--line); margin-bottom: 10px; }
.t-game .tg-title { font-family: var(--font-fun); font-size: 22px; flex: 1 1 200px; word-break: keep-all; overflow-wrap: anywhere; }
.t-game .tg-title small { display: block; font-family: var(--font-body); font-size: 14px; color: var(--muted); }

/* ---------- 테이블 ---------- */
.tbl-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 17px; }
.tbl th { background: #efe9ff; color: #3d2c8d; font-family: var(--font-fun); font-weight: 400; text-align: left; padding: 10px; position: sticky; top: 0; }
.tbl th:first-child { border-top-left-radius: 12px; }
.tbl th:last-child { border-top-right-radius: 12px; }
.tbl td { padding: 9px 10px; border-bottom: 1px solid #eee8ff; vertical-align: middle; }
.tbl tr.best td { background: #fff6cc; font-weight: 700; }
.tbl tr.err td { background: #ffe5ea; color: #a4133c; }
.tbl tr.me td { background: #dff7f4; font-weight: 700; }
.tbl td.cell-edit { cursor: text; border-radius: 8px; min-width: 120px; word-break: keep-all; overflow-wrap: anywhere; }
.tbl td.cell-edit:hover { background: #f5f1ff; }
.tbl td.cell-edit:focus { outline: 3px solid #b79cff; background: #fff; }
.tbl td.cell-edit:empty::before { content: '(클릭해서 입력)'; color: #b0a8cc; }
.x-btn { border: 0; background: #ffe0e8; color: #c9184a; border-radius: 10px; width: 36px; height: 36px; font-size: 18px; }
.x-btn:hover { background: #ffc2d1; }
.sort-btn { border: 0; background: none; color: inherit; font: inherit; padding: 0; cursor: pointer; }
.err-list { background: #ffe5ea; color: #a4133c; border-radius: 12px; padding: 10px 14px; margin: 10px 0; font-size: 16px; }
.err-list div { margin: 2px 0; }
.ok-note { color: #0b7a70; font-size: 16px; }

/* 탭 */
.seg { display: inline-flex; background: #efe9ff; border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { border: 0; background: none; border-radius: 9px; padding: 6px 12px; font-family: var(--font-fun); font-size: 16px; color: #3d2c8d; }
.seg button.on { background: #fff; box-shadow: 0 2px 0 rgba(0,0,0,.08); }

/* 막대그래프 */
.bars { display: flex; flex-direction: column; gap: 8px; }
.bar-row { display: grid; grid-template-columns: 44px minmax(90px, 160px) 1fr; align-items: center; gap: 10px; }
.bar-rank { font-family: var(--font-fun); font-size: 20px; text-align: center; }
.bar-name { font-family: var(--font-fun); font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { background: #f1edff; 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-family: var(--font-fun); font-size: 16px; min-width: 44px; transition: width .6s cubic-bezier(.2,.9,.3,1.2); }
.bar-row.me .bar-name { color: #0b7a70; }

/* ---------- 게임 화면 ---------- */
.play { height: 100vh; height: 100dvh; display: flex; flex-direction: column; background: #fff8ec; }
.test-banner { background: #2b2350; color: #ffd23f; text-align: center; padding: 6px 10px; font-family: var(--font-fun); font-size: 17px; }
.play-top { display: flex; align-items: center; gap: 8px; padding: 8px 12px; min-height: 48px; }
.play-top .p-exit { border: 0; background: #efe9ff; color: #3d2c8d; border-radius: 10px; width: 38px; height: 38px; font-size: 18px; flex: none; }
.play-top .p-title { font-family: var(--font-fun); font-size: 17px; color: var(--muted); flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chips { display: flex; gap: 6px; align-items: center; flex: none; }
.chip { border-radius: 999px; padding: 4px 10px; font-family: var(--font-fun); font-size: 16px; color: #fff; animation: pop .3s; white-space: nowrap; }
.chip.combo { background: var(--pink); }
.chip.speed { background: var(--orange); }
.score-badge { background: #5b2bd6; color: #fff; border-radius: 999px; padding: 4px 14px; font-family: var(--font-fun); font-size: 20px; min-width: 70px; text-align: center; flex: none; transition: transform .15s; }
.score-badge.bump { transform: scale(1.25); background: #10a89a; }
.score-badge.down { background: #d93a5f; }

.board-wrap { flex: 1; min-height: 0; position: relative; padding: 4px 10px 10px; }
.board { width: 100%; height: 100%; display: grid; justify-content: center; align-content: center; overflow: hidden; }
.board.scroll { overflow-y: auto; align-content: start; padding-bottom: 20px; }
.board.shake { animation: shake .4s; }

.mcard {
  position: relative; border: 0; border-radius: 16px; padding: 8px; overflow: hidden;
  display: flex; align-items: center; justify-content: center; text-align: center;
  background: #fff; color: #2b2350; box-shadow: 0 5px 0 rgba(43,35,80,.18);
  transition: transform .12s, box-shadow .12s, opacity .4s, background .2s;
  user-select: none;
}
.mcard.t { background: #e9f1ff; border: 3px solid #a9c8ff; }
.mcard.a { background: #fff0f6; border: 3px solid #ffb3d1; }
.mcard:hover { transform: translateY(-2px); }
.mcard .txt { display: block; width: 100%; line-height: 1.2; word-break: keep-all; overflow-wrap: normal; font-family: var(--font-fun); }
.mcard .txt.wrap-any { overflow-wrap: anywhere; }
.mcard.sel { background: #ffd23f; border-color: #f08c00; transform: translateY(-4px) scale(1.04); box-shadow: 0 9px 0 rgba(240,140,0,.4); z-index: 2; }
.mcard.wrong { background: #ffccd5 !important; border-color: #ef476f !important; animation: wiggle .4s; }
.mcard.matched { background: #d3f8ef; border-color: #2ec4b6; animation: matchPop .45s forwards; pointer-events: none; }
.mcard.gone { opacity: .16; transform: scale(.94); box-shadow: none; }
.mcard.hint { animation: hintWiggle 1s infinite; box-shadow: 0 0 0 4px #ffd23f, 0 5px 0 rgba(43,35,80,.18); }
.mcard.hint-strong { box-shadow: 0 0 0 6px #ff9f1c, 0 0 24px 6px rgba(255,159,28,.7); }
.mcard .bulb { position: absolute; top: 2px; right: 4px; font-size: 18px; animation: pop .4s; }
.mcard .first { position: absolute; left: 4px; bottom: 3px; background: #2b2350; color: #ffd23f; border-radius: 8px; padding: 1px 6px; font-size: 13px; font-family: var(--font-body); max-width: 90%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.start-over { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px; }
.start-box { background: #fff; color: #2b2350; border-radius: 26px; padding: 26px; text-align: center; max-width: 560px; width: 100%; box-shadow: var(--shadow); border: 3px solid #e8e1ff; max-height: 100%; overflow: auto; }
.start-box h1 { font-size: clamp(30px, 6vw, 46px); color: #5b2bd6; word-break: keep-all; overflow-wrap: anywhere; }
.rules { text-align: left; font-size: 16px; background: #f7f4ff; border-radius: 14px; padding: 12px 16px; margin: 14px 0 20px; line-height: 1.7; }

.fx-layer { position: fixed; inset: 0; pointer-events: none; z-index: 50; overflow: hidden; }
.float-pts { position: absolute; font-family: var(--font-fun); font-size: 30px; color: #10a89a; text-shadow: 0 2px 0 #fff, 0 0 8px #fff; animation: floatUp 1s forwards; white-space: nowrap; transform: translate(-50%, -50%); }
.float-pts.neg { color: #d93a5f; }
.combo-fx { position: absolute; left: 50%; top: 38%; transform: translate(-50%, -50%); font-family: var(--font-fun); font-size: clamp(36px, 8vw, 72px); white-space: nowrap; animation: comboIn .9s forwards; -webkit-text-stroke: 2px #fff; paint-order: stroke fill; text-shadow: 0 5px 0 rgba(0,0,0,.15); }
.combo-fx.l2 { color: #3a86ff; } .combo-fx.l3 { color: #8338ec; } .combo-fx.l4 { color: #ff5fa2; }
.speed-fx { position: absolute; left: 50%; top: 52%; transform: translate(-50%, -50%); font-family: var(--font-fun); font-size: clamp(26px, 5vw, 44px); color: #f08c00; -webkit-text-stroke: 2px #fff; paint-order: stroke fill; animation: speedIn .8s forwards; white-space: nowrap; }
.reset-fx { position: absolute; left: 50%; top: 45%; transform: translate(-50%, -50%); font-family: var(--font-fun); font-size: clamp(22px, 4vw, 34px); color: #fff; background: #d93a5f; padding: 8px 18px; border-radius: 16px; animation: fadeOut 1.1s forwards; white-space: nowrap; }
.hint-toast { position: absolute; left: 50%; top: 70px; transform: translateX(-50%); background: #2b2350; color: #ffd23f; font-family: var(--font-fun); font-size: 20px; padding: 6px 16px; border-radius: 999px; animation: fadeOut 1.6s forwards; }
.confetti { position: absolute; top: -20px; width: 12px; height: 18px; border-radius: 3px; animation: fall linear forwards; }

/* 결과 */
.result { max-width: 720px; margin: 0 auto; text-align: center; }
.result h1 { font-size: clamp(30px, 6vw, 48px); color: #5b2bd6; word-break: keep-all; overflow-wrap: anywhere; }
.result .final { font-family: var(--font-fun); font-size: clamp(40px, 10vw, 80px); color: #ff5fa2; text-shadow: 0 5px 0 #ffd23f; margin: 6px 0; animation: pop .6s; }
.result .combo-line { font-family: var(--font-fun); font-size: 26px; color: #3a86ff; margin-bottom: 18px; }
.lb { text-align: left; }
.lb-row { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-radius: 14px; background: #f7f4ff; margin-bottom: 8px; font-family: var(--font-fun); font-size: 22px; }
.lb-row .medal { width: 40px; text-align: center; }
.lb-row .who { flex: 1; }
.lb-row.me { background: #ffd23f; box-shadow: 0 0 0 3px #f08c00; }
.notice { background: #2b2350; color: #ffd23f; border-radius: 14px; padding: 14px; font-family: var(--font-fun); font-size: 20px; margin: 16px 0; }

/* 내 기록 */
.hist-row { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-radius: 14px; background: #fff; border: 2px solid var(--line); margin-bottom: 8px; flex-wrap: wrap; }
.hist-row .h-title { font-family: var(--font-fun); font-size: 20px; flex: 1 1 180px; }
.hist-row .h-score { font-family: var(--font-fun); font-size: 24px; color: #5b2bd6; }
.hist-row.best { background: #fff6cc; border-color: #ffd23f; }

/* ---------- 팝업 (배경과 무관하게 항상 또렷하게) ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(20, 14, 50, .62); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 100; animation: fadeIn .15s; }
.modal {
  background: #ffffff !important; color: #1d1640 !important;
  border-radius: 22px; padding: 24px; width: 100%; max-width: 440px;
  box-shadow: 0 20px 60px rgba(0,0,0,.35); border: 4px solid #8338ec; animation: pop .25s;
  max-height: 90vh; overflow: auto;
}
.modal * { color: #1d1640; }
.modal h2 { font-size: 26px; color: #4a1fb8 !important; }
.modal p { font-size: 18px; line-height: 1.5; margin: 0 0 12px; }
.modal .btn, .modal .btn * { color: #ffffff; }
.modal .btn.ghost, .modal .btn.ghost * { color: #1d1640; background: #f1edff; border-color: #c9bdf5; }
.modal .m-err { color: #c9184a !important; font-weight: 700; min-height: 1.4em; font-size: 17px; }
.modal .input { background: #fff; color: #1d1640; font-size: 26px; text-align: center; letter-spacing: 6px; }
.modal .m-btns { display: flex; gap: 10px; justify-content: flex-end; margin-top: 10px; flex-wrap: wrap; }
.modal.warn { border-color: #f08c00; }
.modal .warn-head { background: #fff3c4; color: #7a4b00 !important; border-radius: 12px; padding: 10px 12px; font-family: var(--font-fun); font-size: 20px; margin-bottom: 12px; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #2b2350; color: #fff; padding: 12px 20px; border-radius: 14px; z-index: 120; font-size: 18px; box-shadow: 0 8px 24px rgba(0,0,0,.3); animation: pop .25s; max-width: 90vw; }
.toast.err { background: #c9184a; }

/* ---------- 애니메이션 ---------- */
@keyframes pop { 0% { transform: scale(.6); opacity: 0; } 70% { transform: scale(1.08); } 100% { transform: scale(1); opacity: 1; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-10px); } 40% { transform: translateX(10px); } 60% { transform: translateX(-6px); } 80% { transform: translateX(6px); } }
@keyframes wiggle { 0%,100% { transform: rotate(0); } 25% { transform: rotate(-4deg); } 75% { transform: rotate(4deg); } }
@keyframes hintWiggle { 0%,100% { transform: rotate(0) scale(1); } 10% { transform: rotate(-5deg) scale(1.04); } 20% { transform: rotate(5deg) scale(1.04); } 30% { transform: rotate(0) scale(1); } }
@keyframes matchPop { 0% { transform: scale(1); } 40% { transform: scale(1.12); } 100% { transform: scale(.94); opacity: .16; box-shadow: none; } }
@keyframes floatUp { 0% { opacity: 0; transform: translate(-50%, -20%) scale(.6); } 20% { opacity: 1; transform: translate(-50%, -60%) scale(1.15); } 100% { opacity: 0; transform: translate(-50%, -220%) scale(1); } }
@keyframes comboIn { 0% { opacity: 0; transform: translate(-50%, -50%) scale(.3) rotate(-10deg); } 25% { opacity: 1; transform: translate(-50%, -50%) scale(1.15) rotate(3deg); } 70% { opacity: 1; transform: translate(-50%, -50%) scale(1); } 100% { opacity: 0; transform: translate(-50%, -80%) scale(1); } }
@keyframes speedIn { 0% { opacity: 0; transform: translate(-150%, -50%) skewX(-20deg); } 30% { opacity: 1; transform: translate(-50%, -50%) skewX(-10deg); } 75% { opacity: 1; } 100% { opacity: 0; transform: translate(60%, -50%) skewX(-20deg); } }
@keyframes fadeOut { 0% { opacity: 0; } 15% { opacity: 1; } 75% { opacity: 1; } 100% { opacity: 0; } }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

@media (max-width: 600px) {
  body { font-size: 17px; }
  #view { padding: 14px 12px 30px; }
  #topbar { padding: 8px 12px; }
  #topbar .brand { font-size: 20px; }
  .tb-btn { font-size: 16px; padding: 6px 12px; }
  .student-badge { font-size: 17px; padding: 4px 12px; }
  .page-title { font-size: 26px; }
  .num-grid { grid-template-columns: repeat(auto-fill, minmax(68px, 1fr)); gap: 9px; }
  .bar-row { grid-template-columns: 34px 90px 1fr; }
  .play-top { padding: 6px 8px; }
  .score-badge { font-size: 17px; }
  .chip { font-size: 14px; padding: 3px 8px; }
  .board-wrap { padding: 2px 6px 6px; }
  .mcard { border-radius: 12px; padding: 5px; }
}
