:root {
  --bg: #fff8ec;
  --ink: #2b2140;
  --muted: #6b6280;
  --line: #eadfcf;
  --panel: #ffffff;
  --pink: #ff6b9a;
  --orange: #ff9f43;
  --yellow: #ffd23f;
  --green: #2ecc71;
  --teal: #1abc9c;
  --blue: #4d8dff;
  --purple: #8e6cff;
  --red: #ff4757;
  --shadow: 0 6px 0 rgba(43, 33, 64, .12);
  --radius: 18px;
  --font-fun: 'Jua', 'Noto Sans KR', sans-serif;
  --font: 'Noto Sans KR', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  background-image:
    radial-gradient(circle at 10% 15%, #ffe3ef 0 80px, transparent 81px),
    radial-gradient(circle at 92% 8%, #e0f2ff 0 110px, transparent 111px),
    radial-gradient(circle at 85% 92%, #fff1c2 0 90px, transparent 91px);
  background-attachment: fixed;
  color: var(--ink);
  font-family: var(--font);
  font-size: 17px;
  -webkit-tap-highlight-color: transparent;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { font-family: var(--font-fun); font-weight: 400; margin: 0; }

/* ---------- 공통 레이아웃 ---------- */
.page { max-width: 1100px; margin: 0 auto; padding: 16px 16px 48px; }
.topbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  margin-bottom: 18px;
}
.topbar .spacer { flex: 1; }
.brand { font-family: var(--font-fun); font-size: 26px; display: flex; align-items: center; gap: 8px; }
.panel {
  background: var(--panel); border: 3px solid var(--ink); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 20px;
}
.panel + .panel { margin-top: 18px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* ---------- 버튼 ---------- */
.btn {
  border: 3px solid var(--ink); border-radius: 14px; background: #fff;
  padding: 10px 18px; font-weight: 700; font-size: 17px;
  box-shadow: 0 4px 0 var(--ink); transition: transform .08s, box-shadow .08s;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 48px; white-space: nowrap;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; box-shadow: 0 4px 0 var(--ink); }
.btn.primary { background: var(--pink); color: #fff; }
.btn.blue { background: var(--blue); color: #fff; }
.btn.green { background: var(--green); color: #fff; }
.btn.yellow { background: var(--yellow); color: var(--ink); }
.btn.purple { background: var(--purple); color: #fff; }
.btn.red { background: var(--red); color: #fff; }
.btn.small { padding: 6px 12px; min-height: 38px; font-size: 15px; border-width: 2px; box-shadow: 0 3px 0 var(--ink); }
.btn.big { font-size: 24px; padding: 14px 34px; min-height: 64px; font-family: var(--font-fun); font-weight: 400; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  border: 3px solid var(--ink); border-radius: 999px; background: var(--yellow);
  padding: 6px 14px; font-weight: 900; box-shadow: 0 3px 0 var(--ink); min-height: 44px;
}
button.chip:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }

/* ---------- 로그인 ---------- */
.hero { text-align: center; margin: 18px 0 22px; }
.hero h1 { font-size: clamp(34px, 7vw, 56px); line-height: 1.15; }
.hero h1 .c1 { color: var(--pink); } .hero h1 .c2 { color: var(--blue); } .hero h1 .c3 { color: var(--orange); }
.hero p { font-size: 19px; color: var(--muted); margin: 10px 0 0; }
.num-grid {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
}
.num-btn {
  aspect-ratio: 1; border: 3px solid var(--ink); border-radius: 20px;
  font-family: var(--font-fun); font-size: clamp(26px, 5vw, 36px);
  box-shadow: 0 5px 0 var(--ink); color: var(--ink);
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1;
}
.num-btn small { font-size: 15px; font-family: var(--font); font-weight: 700; margin-top: 4px; }
.num-btn:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--ink); }

/* ---------- 게임 목록 ---------- */
.game-grid { display: grid; gap: 16px; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); }
.game-card {
  text-align: left; border: 3px solid var(--ink); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); background: #fff; position: relative;
  display: flex; flex-direction: column; gap: 10px; min-height: 160px;
}
.game-card h3 { font-size: 24px; line-height: 1.25; word-break: keep-all; overflow-wrap: anywhere; }
.game-card .meta { display: flex; gap: 8px; flex-wrap: wrap; font-size: 15px; }
.tag { border-radius: 999px; padding: 3px 10px; background: #f3eefc; font-weight: 700; font-size: 14px; }
.best { margin-top: auto; font-weight: 900; font-size: 18px; }
button.game-card { cursor: pointer; transition: transform .1s; }
button.game-card:hover { transform: translateY(-3px) rotate(-.4deg); }
.empty { text-align: center; padding: 50px 20px; color: var(--muted); font-size: 19px; }

/* ---------- 게임 화면 ---------- */
.game-screen { height: 100vh; height: 100dvh; display: flex; flex-direction: column; padding: 8px; gap: 8px; }
.test-banner {
  background: var(--purple); color: #fff; font-weight: 900; text-align: center;
  border: 3px solid var(--ink); border-radius: 12px; padding: 6px 10px; font-size: 16px;
}
.hud { display: flex; align-items: center; gap: 8px; min-height: 40px; }
.hud .title { flex: 1; min-width: 0; font-weight: 700; font-size: 15px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud .exit { border: 2px solid var(--ink); background: #fff; border-radius: 10px; width: 38px; height: 38px; font-size: 18px; font-weight: 900; flex: none; }
.score-badge {
  background: var(--ink); color: #fff; border-radius: 999px; padding: 6px 14px;
  font-weight: 900; font-size: 17px; min-width: 76px; text-align: center; flex: none;
  transition: transform .15s;
}
.score-badge.bump { transform: scale(1.25); background: var(--pink); }
.mult { border-radius: 999px; padding: 4px 10px; font-weight: 900; font-size: 14px; border: 2px solid var(--ink); flex: none; animation: pop .25s; }
.mult.combo { background: var(--orange); color: #fff; }
.mult.speed { background: var(--yellow); color: var(--ink); }
.board-wrap { flex: 1; min-height: 0; position: relative; }
.board {
  position: absolute; inset: 0; display: grid; justify-content: center; align-content: center;
  overflow: hidden; padding: 2px;
}
.board.scroll { overflow-y: auto; align-content: start; padding-bottom: 20px; }
.card {
  position: relative; border: 3px solid var(--ink); border-radius: 16px; background: #fff;
  box-shadow: 0 5px 0 var(--ink); display: flex; align-items: center; justify-content: center;
  padding: 8px; overflow: hidden; cursor: pointer; user-select: none; color: var(--ink);
  transition: transform .12s, box-shadow .12s, opacity .4s, background .15s;
}
.card .txt {
  display: block; width: 100%; text-align: center; font-weight: 900; line-height: 1.2;
  word-break: keep-all; overflow-wrap: normal; white-space: normal;
  overflow: hidden; /* scrollWidth/scrollHeight로 넘침을 정확히 측정하기 위해 필요 */
  transition: none;
}
.card .txt.break { overflow-wrap: anywhere; word-break: break-all; }
.card:active { transform: translateY(3px); box-shadow: 0 2px 0 var(--ink); }
.card.selected { transform: translateY(-4px) scale(1.04); box-shadow: 0 0 0 5px var(--blue), 0 8px 0 var(--ink); z-index: 2; }
.card.wrong { background: #ffd0d5 !important; animation: wobble .4s; }
.card.matched { background: #b8f5cf !important; animation: matchpop .45s forwards; pointer-events: none; }
.card.gone { opacity: 0; pointer-events: none; transform: scale(.85); }
.card.hint { animation: hintwiggle 1s ease-in-out infinite; box-shadow: 0 0 0 4px var(--yellow), 0 5px 0 var(--ink); }
.card .hint-badge {
  position: absolute; top: 3px; right: 5px; font-size: 14px; background: var(--yellow);
  border: 2px solid var(--ink); border-radius: 999px; padding: 0 6px; font-weight: 900; line-height: 1.5;
}
.board.shake { animation: shake .4s; }
.start-overlay {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  background: rgba(255, 248, 236, .92); z-index: 5; border-radius: 16px; text-align: center; padding: 16px;
}
.start-overlay h2 { font-size: clamp(28px, 6vw, 46px); }
.start-overlay .rules { font-size: 16px; color: var(--ink); line-height: 1.7; max-width: 480px; }

/* 이펙트 */
.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: 34px; color: var(--green);
  -webkit-text-stroke: 1.5px var(--ink); animation: floatup 1s ease-out forwards; transform: translate(-50%, -50%);
}
.float-pts.minus { color: var(--red); }
.combo-text {
  position: absolute; left: 50%; top: 42%; transform: translate(-50%, -50%);
  font-family: var(--font-fun); font-size: clamp(38px, 9vw, 84px); white-space: nowrap;
  color: var(--orange); -webkit-text-stroke: 2px var(--ink); text-shadow: 0 6px 0 rgba(0,0,0,.15);
  animation: comboin .9s ease-out forwards;
}
.combo-text.speed { color: var(--yellow); top: 56%; font-size: clamp(30px, 7vw, 60px); }
.combo-text.reset { color: #fff; background: var(--red); -webkit-text-stroke: 0; font-size: clamp(20px, 4vw, 30px); padding: 6px 18px; border-radius: 14px; border: 3px solid var(--ink); top: 55%; }
.combo-text.hintmsg { color: #fff; background: var(--ink); -webkit-text-stroke: 0; font-size: clamp(20px, 4vw, 28px); padding: 6px 18px; border-radius: 14px; top: 18%; }
.confetti { position: absolute; top: -20px; width: 12px; height: 18px; border-radius: 3px; animation: fall linear forwards; }

/* ---------- 결과 ---------- */
.result-hero { text-align: center; padding: 10px 0 6px; }
.result-hero h1 { font-size: clamp(30px, 6vw, 50px); word-break: keep-all; }
.big-score { font-family: var(--font-fun); font-size: clamp(54px, 13vw, 110px); color: var(--pink); -webkit-text-stroke: 2px var(--ink); line-height: 1.1; margin: 8px 0; }
.result-sub { font-size: 22px; font-weight: 900; }
.lb { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.lb li {
  display: flex; align-items: center; gap: 12px; padding: 10px 14px; border: 2px solid var(--line);
  border-radius: 12px; font-weight: 700; font-size: 18px;
}
.lb li .rk { font-family: var(--font-fun); font-size: 22px; width: 54px; }
.lb li .sc { margin-left: auto; font-weight: 900; }
.lb li.me { background: #fff3b0; border-color: var(--ink); border-width: 3px; }
.lb li.gap { border: none; justify-content: center; color: var(--muted); padding: 0; }

/* ---------- 교사 ---------- */
.tabs { display: flex; gap: 8px; flex-wrap: wrap; }
.tab { border: 3px solid var(--ink); border-radius: 12px; padding: 8px 16px; background: #fff; font-weight: 900; min-height: 46px; }
.tab.active { background: var(--ink); color: #fff; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-weight: 900; }
.input, .textarea, .select {
  border: 3px solid var(--ink); border-radius: 12px; padding: 10px 12px; background: #fff; color: var(--ink);
  font-size: 17px; width: 100%;
}
.input:focus, .textarea:focus, .select:focus { outline: 4px solid #bcd3ff; }
.textarea { min-height: 180px; resize: vertical; line-height: 1.6; font-family: var(--font); }
.err-list { background: #ffe8ea; border: 2px solid var(--red); color: #8a0d1b; border-radius: 12px; padding: 10px 14px; margin: 10px 0; font-weight: 700; }
.err-list div + div { margin-top: 4px; }
.ok-msg { color: #0d7a3c; font-weight: 900; }
.bad-msg { color: #c0122a; font-weight: 900; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 16px; }
.tbl th, .tbl td { border-bottom: 2px solid var(--line); padding: 8px; text-align: left; vertical-align: middle; }
.tbl th { background: #f6f0ff; font-weight: 900; white-space: nowrap; }
.tbl th.sortable { cursor: pointer; }
.tbl td.num { width: 44px; color: var(--muted); font-weight: 700; }
.tbl td.edit { cursor: text; border-radius: 8px; }
.tbl td.edit:hover { background: #fff8d6; }
.tbl td.edit:focus { outline: 3px solid var(--blue); background: #fff; }
.tbl td.edit.empty::before { content: '(클릭해서 입력)'; color: #b3a9c2; font-weight: 400; }
.tbl tr.best-row td { background: #fff6c8; font-weight: 900; }
.tbl tr.sub td { background: #fbf8ff; font-size: 15px; }
.tbl .x { border: 2px solid var(--ink); background: #fff; border-radius: 8px; width: 34px; height: 34px; font-weight: 900; color: var(--red); }
.tbl-wrap { overflow-x: auto; }
.chart { display: flex; flex-direction: column; gap: 8px; }
.bar-row { display: grid; grid-template-columns: 120px 1fr 70px; gap: 10px; align-items: center; font-weight: 700; }
.bar-row .lbl { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-track { background: #f1ecf7; border-radius: 999px; height: 26px; overflow: hidden; border: 2px solid var(--ink); }
.bar { height: 100%; border-radius: 999px; transition: width .6s; }
.bar-row .v { text-align: right; font-weight: 900; }
.notice { background: #eef4ff; border: 2px dashed var(--blue); border-radius: 12px; padding: 12px 14px; line-height: 1.6; }
.section-title { font-size: 26px; margin-bottom: 12px; }
.inline-num { width: 100px; display: inline-block; text-align: center; }
.grid-2 { display: grid; gap: 18px; grid-template-columns: 1fr 1fr; }
@media (max-width: 800px) { .grid-2 { grid-template-columns: 1fr; } .bar-row { grid-template-columns: 90px 1fr 56px; } }

/* ---------- 팝업 (항상 흰 바탕 + 진한 글씨로 고정) ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(20, 14, 35, .62); z-index: 100;
  display: flex; align-items: center; justify-content: center; padding: 16px; animation: fadein .15s;
}
.modal {
  background: #ffffff !important; color: #1d1530 !important;
  border: 4px solid #1d1530; border-radius: 22px; box-shadow: 0 10px 0 #1d1530;
  width: min(440px, 100%); padding: 24px; animation: pop .2s;
  max-height: calc(100dvh - 32px); overflow-y: auto;
}
.modal * { color: inherit; }
.modal h2 { font-size: 26px; margin-bottom: 10px; color: #1d1530 !important; }
.modal p { font-size: 18px; line-height: 1.6; margin: 0 0 14px; color: #1d1530 !important; }
.modal .input { background: #fff !important; color: #1d1530 !important; border-color: #1d1530; font-size: 24px; text-align: center; letter-spacing: 6px; }
.modal .modal-err { color: #c0122a !important; font-weight: 900; min-height: 26px; margin-top: 8px; }
.modal .actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }
.modal .btn { color: #1d1530 !important; background: #ffffff; border-color: #1d1530; box-shadow: 0 4px 0 #1d1530; }
.modal .btn.primary { background: #e8366f; color: #ffffff !important; }
.modal .btn.red { background: #d92638; color: #ffffff !important; }
.modal .btn.blue { background: #2f6fe8; color: #ffffff !important; }

/* 토스트 */
#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-weight: 700; box-shadow: 0 6px 18px rgba(0,0,0,.25); animation: pop .2s; max-width: 90vw; text-align: center; }
.toast.error { background: #c0122a; }

/* ---------- 애니메이션 ---------- */
@keyframes pop { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-12px); } 40% { transform: translateX(10px); } 60% { transform: translateX(-7px); } 80% { transform: translateX(5px); } }
@keyframes wobble { 0%,100% { transform: rotate(0); } 25% { transform: rotate(-5deg); } 75% { transform: rotate(5deg); } }
@keyframes matchpop { 0% { transform: scale(1); } 40% { transform: scale(1.12); } 100% { transform: scale(.9); opacity: 0; } }
@keyframes hintwiggle { 0%,100% { transform: rotate(0); } 15% { transform: rotate(-4deg); } 30% { transform: rotate(4deg); } 45% { transform: rotate(-2deg); } 60% { transform: rotate(0); } }
@keyframes floatup { from { opacity: 1; transform: translate(-50%, -50%); } to { opacity: 0; transform: translate(-50%, -150%) scale(1.2); } }
@keyframes comboin { 0% { opacity: 0; transform: translate(-50%, -50%) scale(.4) 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(.95); } }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

@media (max-width: 520px) {
  body { font-size: 16px; }
  .panel { padding: 14px; }
  .num-grid { grid-template-columns: repeat(auto-fill, minmax(66px, 1fr)); gap: 9px; }
  .btn.big { font-size: 21px; padding: 12px 24px; }
  .game-screen { padding: 6px; gap: 6px; }
  .card { border-width: 2px; border-radius: 12px; box-shadow: 0 3px 0 var(--ink); padding: 5px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}
