:root {
  color-scheme: light;
  --bg1: #fff6d8;
  --bg2: #e3f4ff;
  --ink: #2b2347;
  --muted: #6b6390;
  --purple: #7c5cff;
  --pink: #ff5c9a;
  --orange: #ff9f1c;
  --yellow: #ffd23f;
  --green: #2ec4a0;
  --blue: #3a86ff;
  --red: #ef3e4a;
  --card-term: #ffe27a;
  --card-term-edge: #f2b705;
  --card-ans: #9fe3ff;
  --card-ans-edge: #3aa8e0;
  --radius: 18px;
  --shadow: 0 6px 0 rgba(43, 35, 71, 0.18);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: 'Gowun Dodum', 'Apple SD Gothic Neo', 'Malgun Gothic', system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(160deg, var(--bg1) 0%, #ffe3ef 45%, var(--bg2) 100%) fixed;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3, .fun, button, .btn { font-family: 'Jua', 'Gowun Dodum', system-ui, sans-serif; font-weight: 400; }
#app { min-height: 100dvh; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; cursor: pointer; border-radius: 14px;
  padding: 12px 20px; font-size: 1.15rem; line-height: 1.1;
  color: #fff; background: var(--purple);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.22);
  transition: transform 0.08s, box-shadow 0.08s, filter 0.15s;
  text-decoration: none; white-space: nowrap;
}
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.22); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn.pink { background: var(--pink); }
.btn.orange { background: var(--orange); }
.btn.green { background: var(--green); }
.btn.blue { background: var(--blue); }
.btn.red { background: var(--red); }
.btn.ghost { background: #fff; color: var(--ink); border: 2px solid #d9d2f5; box-shadow: 0 3px 0 #d9d2f5; }
.btn.small { padding: 8px 12px; font-size: 1rem; border-radius: 11px; }
.btn.big { padding: 18px 34px; font-size: 1.6rem; border-radius: 20px; }

/* ---------- layout ---------- */
.page { max-width: 1100px; margin: 0 auto; padding: 16px 16px 48px; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  padding: 10px 16px; background: rgba(255, 255, 255, 0.85); backdrop-filter: blur(6px);
  border-bottom: 3px solid #f0e6ff; position: sticky; top: 0; z-index: 20;
}
.topbar .brand { font-family: 'Jua', sans-serif; font-size: 1.3rem; color: var(--purple); }
.topbar .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.me-badge {
  font-family: 'Jua', sans-serif; font-size: 1.2rem;
  background: var(--yellow); color: var(--ink); border: 0; border-radius: 999px;
  padding: 8px 16px; cursor: pointer; box-shadow: 0 3px 0 #d9a800;
}
.me-badge small { font-size: 0.85rem; opacity: 0.75; margin-left: 4px; }

.hero { text-align: center; padding: 26px 10px 10px; }
.hero h1 { font-size: clamp(2rem, 6vw, 3.2rem); margin: 0; color: var(--purple); text-shadow: 3px 3px 0 #ffd6e8; }
.hero p { font-size: 1.25rem; margin: 10px 0 0; color: var(--muted); }

.panel {
  background: #fff; border-radius: var(--radius); padding: 18px; margin-top: 16px;
  box-shadow: var(--shadow); border: 2px solid #f0e6ff;
}
.panel h2 { margin: 0 0 12px; font-size: 1.5rem; color: var(--purple); }
.panel h3 { margin: 0 0 8px; font-size: 1.3rem; }
.muted { color: var(--muted); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.center { text-align: center; }
.err { color: var(--red); font-weight: bold; }
.ok { color: #128a6c; font-weight: bold; }

/* ---------- login grid ---------- */
.num-grid {
  display: grid; gap: 12px; margin: 22px auto 0; max-width: 960px;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
}
.num-btn {
  aspect-ratio: 1 / 1; border: 0; border-radius: 22px; cursor: pointer;
  font-family: 'Jua', sans-serif; font-size: clamp(1.7rem, 5vw, 2.3rem); color: #fff;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.2); transition: transform 0.08s;
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 4px;
}
.num-btn small { font-size: 0.9rem; opacity: 0.95; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.num-btn:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2); }
.teacher-link { margin-top: 30px; text-align: center; }

/* ---------- game list ---------- */
.game-grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); margin-top: 16px; }
.game-card {
  border: 0; text-align: left; cursor: pointer; border-radius: 22px; padding: 18px;
  color: var(--ink); background: #fff; box-shadow: var(--shadow); position: relative; overflow: hidden;
  display: flex; flex-direction: column; gap: 8px; min-height: 150px; transition: transform 0.1s;
}
.game-card:hover { transform: translateY(-3px) rotate(-0.4deg); }
.game-card::before { content: ''; position: absolute; inset: 0 0 auto 0; height: 10px; background: var(--accent, var(--purple)); }
.game-card .gt { font-family: 'Jua', sans-serif; font-size: 1.5rem; line-height: 1.25; margin-top: 4px; word-break: keep-all; }
.game-card .meta { display: flex; gap: 6px; flex-wrap: wrap; font-size: 0.95rem; }
.chip { display: inline-block; padding: 3px 10px; border-radius: 999px; background: #f1ecff; color: #4b3aa8; font-weight: bold; font-size: 0.9rem; }
.chip.gold { background: #fff1c2; color: #8a5a00; }
.chip.green { background: #d6f7ee; color: #0c6e55; }
.game-card .go { margin-top: auto; font-family: 'Jua', sans-serif; font-size: 1.2rem; color: var(--accent, var(--purple)); }
.empty { text-align: center; padding: 40px 10px; font-size: 1.2rem; color: var(--muted); }

/* ---------- play screen ---------- */
.play { height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
.test-banner {
  background: repeating-linear-gradient(45deg, #ffcf33, #ffcf33 14px, #ffb703 14px, #ffb703 28px);
  color: #2b2347; text-align: center; font-family: 'Jua', sans-serif; font-size: 1.05rem; padding: 6px 10px;
}
.hud {
  display: flex; align-items: center; gap: 8px; padding: 6px 10px; min-height: 44px;
  background: rgba(255, 255, 255, 0.9); border-bottom: 2px solid #f0e6ff;
}
.hud .ht { flex: 1; min-width: 0; font-size: 0.95rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud .ht b { color: var(--ink); }
.hud .quit { padding: 5px 10px; font-size: 0.9rem; }
.mult {
  font-family: 'Jua', sans-serif; font-size: 0.95rem; padding: 4px 9px; border-radius: 999px; color: #fff;
  animation: pop 0.25s ease-out;
}
.mult.combo { background: var(--pink); }
.mult.speed { background: var(--orange); }
.score-badge {
  font-family: 'Jua', sans-serif; font-size: 1.1rem; padding: 4px 14px; border-radius: 999px;
  background: var(--purple); color: #fff; min-width: 70px; text-align: center; transition: transform 0.15s;
}
.score-badge.bump { transform: scale(1.25); }
.board-wrap { flex: 1; min-height: 0; position: relative; }
.board {
  position: absolute; inset: 0; padding: 10px; display: grid; justify-content: center; align-content: center;
  overflow: hidden;
}
.board.scroll { overflow-y: auto; align-content: start; }
.card {
  position: relative; border-radius: 16px; cursor: pointer; user-select: none;
  display: flex; align-items: center; justify-content: center; text-align: center;
  padding: 8px; overflow: hidden;
  font-family: 'Jua', 'Gowun Dodum', sans-serif; line-height: 1.2; color: #2b2347;
  border: 3px solid transparent; transition: transform 0.12s, box-shadow 0.12s, opacity 0.35s;
}
.card.t { background: var(--card-term); box-shadow: 0 5px 0 var(--card-term-edge); }
.card.a { background: var(--card-ans); box-shadow: 0 5px 0 var(--card-ans-edge); }
.card .txt { display: block; width: 100%; word-break: keep-all; overflow-wrap: normal; }
.card .txt.anywhere { overflow-wrap: anywhere; word-break: break-all; }
.card:hover { transform: translateY(-2px); }
.card.sel { border-color: var(--purple); transform: scale(1.05); box-shadow: 0 0 0 4px rgba(124, 92, 255, 0.45), 0 5px 0 rgba(0, 0, 0, 0.2); z-index: 2; }
.card.wrong { background: #ffb3b8 !important; animation: shake 0.4s; }
.card.matched { animation: vanish 0.45s forwards; pointer-events: none; }
.card.gone { visibility: hidden; }
.card.hint { animation: wiggle 0.6s infinite; }
.card.hint2 { box-shadow: 0 0 0 5px #ffe14d, 0 0 22px 6px rgba(255, 210, 63, 0.9); }
.card .bulb { position: absolute; top: 2px; right: 6px; font-size: 1.2rem; }

.start-overlay {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px;
  text-align: center; padding: 20px; z-index: 5;
}
.start-overlay h1 { font-size: clamp(2rem, 7vw, 3.6rem); margin: 0; color: var(--purple); text-shadow: 3px 3px 0 #ffd6e8; word-break: keep-all; }
.rules { background: #fff; border-radius: 18px; padding: 14px 18px; box-shadow: var(--shadow); max-width: 520px; text-align: left; line-height: 1.7; font-size: 1.05rem; }

.shake-screen { animation: screenshake 0.4s; }
.fx-layer { position: fixed; inset: 0; pointer-events: none; z-index: 50; overflow: hidden; }
.floater {
  position: absolute; font-family: 'Jua', sans-serif; font-size: 1.8rem; color: var(--green);
  text-shadow: 2px 2px 0 #fff, -2px -2px 0 #fff, 2px -2px 0 #fff, -2px 2px 0 #fff;
  transform: translate(-50%, -50%); animation: floatUp 1s ease-out forwards; white-space: nowrap;
}
.floater.minus { color: var(--red); }
.combo-text {
  position: absolute; left: 50%; top: 38%; transform: translate(-50%, -50%);
  font-family: 'Jua', sans-serif; font-size: clamp(2.2rem, 9vw, 4.6rem); white-space: nowrap;
  color: #fff; -webkit-text-stroke: 2px rgba(0, 0, 0, 0.15);
  text-shadow: 0 4px 0 var(--pink), 0 0 24px rgba(255, 92, 154, 0.7);
  animation: comboPop 0.9s ease-out forwards;
}
.combo-text.speed { top: 52%; font-size: clamp(1.6rem, 6vw, 3rem); text-shadow: 0 4px 0 var(--orange), 0 0 24px rgba(255, 159, 28, 0.8); }
.combo-text.reset { top: 50%; font-size: clamp(1.2rem, 4.5vw, 2rem); text-shadow: 0 3px 0 var(--red); }

/* ---------- result ---------- */
.result { text-align: center; }
.result .big-score { font-family: 'Jua', sans-serif; font-size: clamp(3rem, 13vw, 6rem); color: var(--pink); line-height: 1; margin: 10px 0; text-shadow: 4px 4px 0 #ffe27a; }
.result .best-combo { font-family: 'Jua', sans-serif; font-size: 1.6rem; color: var(--purple); }
.newbest { display: inline-block; background: var(--yellow); padding: 6px 16px; border-radius: 999px; font-family: 'Jua', sans-serif; font-size: 1.2rem; margin-top: 8px; animation: pop 0.4s; }
.lb { list-style: none; padding: 0; margin: 0 auto; max-width: 460px; text-align: left; }
.lb li { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-radius: 14px; margin-bottom: 8px; background: #f7f4ff; font-size: 1.15rem; }
.lb li .rk { font-family: 'Jua', sans-serif; width: 56px; font-size: 1.25rem; }
.lb li .sc { margin-left: auto; font-family: 'Jua', sans-serif; color: var(--purple); }
.lb li.me { background: #fff1c2; border: 3px solid var(--orange); }
.lb li.gap { background: transparent; justify-content: center; color: var(--muted); padding: 0; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 1.02rem; }
.tbl th, .tbl td { padding: 9px 10px; border-bottom: 1px solid #eee8ff; text-align: left; vertical-align: middle; }
.tbl th { background: #f4f0ff; font-family: 'Jua', sans-serif; font-weight: 400; color: #4b3aa8; white-space: nowrap; }
.tbl tr.best td { background: #fff7d6; font-weight: bold; }
.tbl td.num { white-space: nowrap; }
.tbl td[contenteditable] { background: #fffdf2; cursor: text; border-radius: 6px; outline: none; min-width: 80px; }
.tbl td[contenteditable]:focus { box-shadow: inset 0 0 0 2px var(--purple); background: #fff; }
.tbl tr.bad td { background: #ffe6e8; }
.del-x { border: 0; background: #ffe0e3; color: var(--red); border-radius: 10px; width: 34px; height: 34px; font-size: 1.1rem; cursor: pointer; }

/* ---------- forms ---------- */
label.f { display: block; font-weight: bold; margin: 12px 0 6px; }
input.in, select.in, textarea.in {
  width: 100%; font: inherit; font-size: 1.1rem; padding: 10px 12px; border-radius: 12px;
  border: 2px solid #d9d2f5; background: #fff; color: var(--ink);
}
input.in:focus, select.in:focus, textarea.in:focus { outline: none; border-color: var(--purple); }
textarea.in { min-height: 160px; resize: vertical; line-height: 1.5; }
input.in.short { width: 110px; display: inline-block; }
.parse-status { margin: 10px 0; font-size: 1.05rem; }
.parse-errors { margin: 6px 0; padding: 10px 14px; background: #ffeef0; border-radius: 12px; color: #b0122c; }
.parse-errors div { margin: 2px 0; }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tab { border: 0; cursor: pointer; padding: 9px 16px; border-radius: 12px; font-size: 1.1rem; background: #f1ecff; color: #4b3aa8; }
.tab.on { background: var(--purple); color: #fff; }

/* ---------- bar chart ---------- */
.bars { display: flex; flex-direction: column; gap: 8px; }
.bar-row { display: grid; grid-template-columns: 140px 1fr 70px; gap: 10px; align-items: center; }
.bar-row .lbl { font-family: 'Jua', sans-serif; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { background: #f3efff; border-radius: 999px; height: 26px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 999px; background: linear-gradient(90deg, var(--pink), var(--orange)); width: 0; transition: width 0.8s ease-out; }
.bar-row:nth-child(1) .bar-fill { background: linear-gradient(90deg, #ffb703, #ffd23f); }
.bar-row:nth-child(2) .bar-fill { background: linear-gradient(90deg, #8d99ae, #c9d1dd); }
.bar-row:nth-child(3) .bar-fill { background: linear-gradient(90deg, #c77d3a, #e7a86b); }
.bar-row .val { font-family: 'Jua', sans-serif; text-align: right; color: var(--purple); }

/* ---------- modal (항상 또렷한 대비 — 바깥 색과 무관하게 고정 색상) ---------- */
.modal-back {
  position: fixed; inset: 0; background: rgba(20, 14, 40, 0.62); z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 16px; animation: fadeIn 0.15s;
}
.modal {
  background: #ffffff !important; color: #1d1733 !important;
  border-radius: 22px; padding: 22px; width: 100%; max-width: 430px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35); border: 4px solid #7c5cff; animation: pop 0.2s;
  font-family: 'Gowun Dodum', system-ui, sans-serif;
}
.modal * { color: inherit; }
.modal h3 { margin: 0 0 10px; font-family: 'Jua', sans-serif; font-weight: 400; font-size: 1.5rem; color: #3b1fa8 !important; }
.modal p { margin: 0 0 12px; font-size: 1.1rem; line-height: 1.5; color: #1d1733 !important; }
.modal input {
  width: 100%; font-size: 1.6rem; padding: 10px 14px; border-radius: 12px; text-align: center; letter-spacing: 0.3em;
  border: 3px solid #7c5cff; background: #ffffff; color: #1d1733 !important;
}
.modal .m-err { color: #c8102e !important; font-weight: bold; min-height: 1.4em; margin-top: 8px; }
.modal .m-btns { display: flex; gap: 10px; justify-content: flex-end; margin-top: 14px; flex-wrap: wrap; }
.modal .m-btns .btn { color: #ffffff !important; }
.modal .m-btns .btn.ghost { color: #1d1733 !important; background: #ffffff; }
.modal .btn.purple { background: #5b3fe0; }
.modal .btn.red { background: #d62839; }

/* ---------- toast ---------- */
#toast-root { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 120; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { background: #2b2347; color: #fff; padding: 12px 20px; border-radius: 14px; font-size: 1.05rem; box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25); animation: pop 0.2s; max-width: 90vw; }
.toast.err { background: #c8102e; color: #fff; }

#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 90; }

.loading { text-align: center; padding: 60px 10px; font-size: 1.3rem; color: var(--muted); }
.loading::after { content: ''; display: inline-block; width: 1em; animation: dots 1.2s steps(4) infinite; text-align: left; }

/* ---------- animations ---------- */
@keyframes pop { 0% { transform: scale(0.6); opacity: 0; } 70% { transform: scale(1.08); opacity: 1; } 100% { transform: scale(1); } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 20% { transform: translateX(-8px); } 40% { transform: translateX(8px); } 60% { transform: translateX(-6px); } 80% { transform: translateX(6px); } }
@keyframes screenshake { 0%, 100% { transform: translate(0, 0); } 20% { transform: translate(-7px, 2px); } 40% { transform: translate(7px, -2px); } 60% { transform: translate(-5px, 1px); } 80% { transform: translate(5px, -1px); } }
@keyframes vanish { 0% { transform: scale(1.1); opacity: 1; filter: brightness(1.3); } 100% { transform: scale(0.2) rotate(12deg); opacity: 0; } }
@keyframes wiggle { 0%, 100% { transform: rotate(0); } 25% { transform: rotate(-4deg) scale(1.03); } 75% { transform: rotate(4deg) scale(1.03); } }
@keyframes floatUp { 0% { opacity: 0; transform: translate(-50%, -30%) scale(0.7); } 20% { opacity: 1; transform: translate(-50%, -60%) scale(1.15); } 100% { opacity: 0; transform: translate(-50%, -170%) scale(1); } }
@keyframes comboPop { 0% { opacity: 0; transform: translate(-50%, -50%) scale(0.3) rotate(-8deg); } 25% { opacity: 1; transform: translate(-50%, -50%) scale(1.15) rotate(3deg); } 70% { opacity: 1; transform: translate(-50%, -50%) scale(1) rotate(0); } 100% { opacity: 0; transform: translate(-50%, -80%) scale(0.95); } }
@keyframes dots { 0% { content: ''; } 25% { content: '.'; } 50% { content: '..'; } 75% { content: '...'; } }

@media (max-width: 600px) {
  .btn { font-size: 1.05rem; padding: 11px 16px; }
  .topbar .brand { font-size: 1.1rem; }
  .bar-row { grid-template-columns: 96px 1fr 56px; }
  .card { border-radius: 12px; padding: 5px; }
  .floater { font-size: 1.4rem; }
}
@media (prefers-reduced-motion: reduce) {
  .card.hint { animation: none; }
}
