:root {
  --coral: #ff6b6b;
  --orange: #ff9f43;
  --yellow: #ffd93d;
  --green: #4cc96f;
  --teal: #20c4b8;
  --blue: #4d96ff;
  --purple: #9b5de5;
  --pink: #f15bb5;
  --ink: #1f2340;
  --ink-soft: #565b7a;
  --paper: #ffffff;
  --bg1: #fff6dc;
  --bg2: #e3f1ff;
  --radius: 18px;
  --shadow: 0 6px 0 rgba(31, 35, 64, 0.15), 0 10px 24px rgba(31, 35, 64, 0.12);
  --font: 'Jua', 'Apple SD Gothic Neo', 'Malgun Gothic', 'Noto Sans KR', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(circle at 10% 15%, rgba(255, 217, 61, 0.35) 0 60px, transparent 61px),
    radial-gradient(circle at 90% 10%, rgba(77, 150, 255, 0.25) 0 80px, transparent 81px),
    radial-gradient(circle at 85% 85%, rgba(241, 91, 181, 0.2) 0 90px, transparent 91px),
    radial-gradient(circle at 5% 90%, rgba(76, 201, 111, 0.25) 0 70px, transparent 71px),
    linear-gradient(160deg, var(--bg1), var(--bg2));
  background-attachment: fixed;
  -webkit-tap-highlight-color: transparent;
  font-size: 18px;
}
button, input, select, textarea { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; border: none; }
button:disabled { cursor: not-allowed; opacity: 0.5; }

#app { min-height: 100dvh; display: flex; flex-direction: column; }

/* ---------- 공통 버튼 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 12px 22px; border-radius: 14px; font-size: 1.1rem;
  background: var(--blue); color: #fff;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.18);
  transition: transform 0.08s, box-shadow 0.08s;
  white-space: nowrap;
}
.btn:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.18); }
.btn.big { font-size: 1.5rem; padding: 16px 34px; border-radius: 18px; }
.btn.small { font-size: 0.95rem; padding: 8px 14px; border-radius: 10px; }
.btn.coral { background: var(--coral); }
.btn.green { background: var(--green); }
.btn.yellow { background: var(--yellow); color: var(--ink); }
.btn.purple { background: var(--purple); }
.btn.orange { background: var(--orange); }
.btn.teal { background: var(--teal); }
.btn.ghost { background: #fff; color: var(--ink); border: 2px solid #d9dcef; box-shadow: 0 3px 0 #d9dcef; }
.btn.danger { background: #e8434b; }

/* ---------- 상단 바 ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 16px; background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(6px); border-bottom: 3px solid #f0e6c8;
  position: sticky; top: 0; z-index: 20; flex-wrap: wrap;
}
.topbar .brand { font-size: 1.3rem; display: flex; align-items: center; gap: 8px; }
.topbar .right { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.student-badge {
  background: var(--yellow); color: var(--ink); padding: 8px 16px; border-radius: 999px;
  font-size: 1.15rem; box-shadow: 0 3px 0 rgba(0, 0, 0, 0.15);
}

.page { width: 100%; max-width: 1100px; margin: 0 auto; padding: 20px 16px 60px; flex: 1; }
.page-title { font-size: clamp(1.8rem, 5vw, 2.8rem); text-align: center; margin: 10px 0 6px; }
.page-sub { text-align: center; color: var(--ink-soft); margin: 0 0 24px; font-size: 1.15rem; }

/* ---------- 시작 화면 ---------- */
.hero-title {
  font-size: clamp(2.2rem, 7vw, 4rem); text-align: center; margin: 24px 0 4px;
  text-shadow: 3px 3px 0 #fff, 6px 6px 0 rgba(255, 159, 67, 0.4);
}
.number-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 14px;
  max-width: 900px; margin: 0 auto;
}
.num-btn {
  aspect-ratio: 1 / 1; border-radius: 22px; font-size: clamp(1.6rem, 4vw, 2.2rem); color: #fff;
  box-shadow: var(--shadow); display: flex; flex-direction: column; align-items: center; justify-content: center;
  transition: transform 0.1s;
}
.num-btn small { font-size: 0.85rem; opacity: 0.95; margin-top: 2px; max-width: 90%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.num-btn:hover { transform: translateY(-3px) rotate(-2deg); }
.num-btn:active { transform: translateY(2px); }
.teacher-link { display: flex; justify-content: center; margin-top: 34px; }

/* ---------- 게임 목록 ---------- */
.game-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 18px; }
.game-card {
  background: #fff; border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow);
  text-align: left; display: flex; flex-direction: column; gap: 10px; border-top: 10px solid var(--blue);
  transition: transform 0.12s; min-height: 160px;
}
.game-card:hover { transform: translateY(-4px); }
.game-card h3 { margin: 0; font-size: 1.5rem; font-weight: normal; word-break: keep-all; overflow-wrap: anywhere; }
.game-card .meta { color: var(--ink-soft); font-size: 1rem; }
.game-card .best { margin-top: auto; font-size: 1.15rem; }
.game-card .best b { color: var(--coral); font-weight: normal; font-size: 1.4rem; }
.empty { text-align: center; padding: 50px 10px; color: var(--ink-soft); font-size: 1.3rem; background: rgba(255,255,255,0.7); border-radius: var(--radius); }

/* ---------- 게임 화면 ---------- */
.play-wrap { height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
.test-banner {
  background: repeating-linear-gradient(45deg, #ffe066, #ffe066 14px, #ffd43b 14px, #ffd43b 28px);
  color: #2b2100; text-align: center; padding: 6px 10px; font-size: 1.05rem; border-bottom: 2px solid #e0b400;
}
.play-bar {
  display: flex; align-items: center; gap: 10px; padding: 6px 12px; background: rgba(255,255,255,0.9);
  border-bottom: 2px solid #eee3c4; min-height: 46px;
}
.play-bar .exit { background: #eef0fb; color: var(--ink); border-radius: 10px; padding: 6px 10px; font-size: 0.95rem; }
.play-bar .ptitle { font-size: 1rem; color: var(--ink-soft); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.play-bar .stats { display: flex; gap: 6px; align-items: center; }
.chip { padding: 4px 10px; border-radius: 999px; font-size: 0.95rem; color: #fff; animation: pop 0.25s; }
.chip.combo { background: var(--coral); }
.chip.speed { background: var(--purple); }
.score-badge {
  background: var(--ink); color: #fff; padding: 5px 14px; border-radius: 999px; font-size: 1.1rem; min-width: 70px; text-align: center;
  transition: transform 0.15s;
}
.score-badge.bump { transform: scale(1.25); background: var(--green); }
.score-badge.drop { background: #e8434b; }

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

.mcard {
  position: relative; border-radius: 16px; padding: 8px; display: flex; align-items: center; justify-content: center;
  text-align: center; box-shadow: 0 5px 0 rgba(31, 35, 64, 0.18); transition: transform 0.12s, opacity 0.3s, box-shadow 0.12s;
  overflow: hidden; user-select: none; touch-action: manipulation;
}
.mcard .txt { display: block; width: 100%; line-height: 1.2; word-break: keep-all; overflow-wrap: normal; }
.mcard.term { color: #fff; }
.mcard.answer { background: #fff; color: var(--ink); border: 4px solid #cfd5ff; }
.mcard:hover:not(.matched) { transform: translateY(-2px); }
.mcard.selected { transform: translateY(-4px) scale(1.04); box-shadow: 0 0 0 5px var(--yellow), 0 8px 0 rgba(31, 35, 64, 0.2); z-index: 2; }
.mcard.wrong { animation: wrongflash 0.45s; }
.mcard.matched { opacity: 0; transform: scale(0.6) rotate(8deg); pointer-events: none; }
.mcard.hint { animation: wiggle 0.9s infinite; box-shadow: 0 0 0 5px #ffe066, 0 0 22px 6px rgba(255, 217, 61, 0.9); }
.mcard .hint-icon { position: absolute; top: 2px; right: 6px; font-size: 1.2rem; }

.start-overlay, .result-wrap {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; padding: 20px; text-align: center; overflow-y: auto;
}
.start-overlay h1 { font-size: clamp(2rem, 6vw, 3.6rem); margin: 0; font-weight: normal; word-break: keep-all; }
.rules { background: #fff; border-radius: var(--radius); padding: 14px 20px; box-shadow: var(--shadow); max-width: 520px; text-align: left; line-height: 1.7; color: var(--ink); }

.result-wrap { justify-content: flex-start; padding-top: 30px; }
.result-wrap h1 { font-size: clamp(1.8rem, 5vw, 3rem); margin: 0; font-weight: normal; word-break: keep-all; }
.final-score { font-size: clamp(2.6rem, 9vw, 5rem); color: var(--coral); text-shadow: 3px 3px 0 #fff; animation: pop 0.5s; }
.final-combo { font-size: 1.5rem; color: var(--purple); }
.result-extra { width: 100%; display: flex; justify-content: center; }
.result-buttons { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }
.leaderboard { background: #fff; border-radius: var(--radius); padding: 16px 20px; box-shadow: var(--shadow); width: min(460px, 100%); }
.leaderboard h3 { margin: 0 0 10px; font-weight: normal; font-size: 1.4rem; }
.lb-row { display: flex; justify-content: space-between; gap: 16px; padding: 8px 12px; border-radius: 10px; font-size: 1.15rem; }
.lb-row:nth-child(odd) { background: #f6f7fd; }
.lb-row.me { background: var(--yellow) !important; }
.lb-row .medal { width: 2em; display: inline-block; }
.notice-box { background: #fff8d6; border: 3px dashed #e0b400; color: #3d3000; border-radius: var(--radius); padding: 16px 20px; max-width: 460px; }

/* ---------- 이펙트 ---------- */
#fx-layer { position: fixed; inset: 0; pointer-events: none; z-index: 50; overflow: hidden; }
.float-text {
  position: absolute; transform: translate(-50%, -50%); font-size: 1.8rem; white-space: nowrap;
  text-shadow: 2px 2px 0 #fff, -2px -2px 0 #fff, 2px -2px 0 #fff, -2px 2px 0 #fff;
  animation: floatUp 1.1s ease-out forwards;
}
.float-text.plus { color: var(--green); }
.float-text.minus { color: #e8434b; font-size: 2.2rem; }
.float-text.combo { color: var(--coral); font-size: 2.4rem; }
.float-text.speed { color: var(--purple); font-size: 2rem; }
.float-text.info { color: var(--ink-soft); font-size: 1.2rem; }
.confetti { position: absolute; top: -20px; width: 12px; height: 18px; border-radius: 3px; animation: fall linear forwards; }

@keyframes floatUp { 0% { opacity: 0; transform: translate(-50%, -30%) scale(0.6); } 15% { opacity: 1; transform: translate(-50%, -60%) scale(1.15); } 100% { opacity: 0; transform: translate(-50%, -200%) scale(1); } }
@keyframes pop { 0% { transform: scale(0.4); } 70% { transform: scale(1.15); } 100% { transform: scale(1); } }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-12px); } 40% { transform: translateX(10px); } 60% { transform: translateX(-8px); } 80% { transform: translateX(6px); } }
@keyframes wrongflash { 0%,100% { filter: none; } 30%,70% { filter: brightness(0.9) sepia(1) hue-rotate(-50deg) saturate(5); } }
@keyframes wiggle { 0%,100% { transform: rotate(0); } 25% { transform: rotate(-4deg) scale(1.03); } 75% { transform: rotate(4deg) scale(1.03); } }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

/* ---------- 팝업 (항상 또렷한 대비) ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15, 18, 40, 0.65); display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 16px; animation: fadein 0.15s;
}
.modal {
  background: #ffffff !important; color: #1f2340 !important; border-radius: 22px; padding: 26px 24px 20px;
  width: min(440px, 100%); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4); border: 4px solid #ffd93d; animation: pop 0.25s;
}
.modal * { color: #1f2340; }
.modal h2 { margin: 0 0 10px; font-size: 1.6rem; font-weight: normal; }
.modal p { margin: 6px 0 14px; font-size: 1.15rem; line-height: 1.5; color: #3a3f5c; }
.modal input {
  width: 100%; font-size: 1.6rem; padding: 12px 14px; border-radius: 12px; border: 3px solid #c9cde6;
  background: #ffffff; color: #1f2340; text-align: center; letter-spacing: 0.3em;
}
.modal input:focus { outline: none; border-color: #4d96ff; }
.modal .error { color: #c62828 !important; min-height: 1.4em; margin: 8px 0 0; font-size: 1.05rem; }
.modal .actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }
.modal .actions .btn { color: #ffffff; }
.modal .actions .btn.ghost { color: #1f2340; background: #ffffff; }
@keyframes fadein { from { opacity: 0; } }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(120px); background: #1f2340; color: #ffffff;
  padding: 12px 22px; border-radius: 999px; z-index: 120; transition: transform 0.25s; font-size: 1.05rem; max-width: 90vw; text-align: center;
}
#toast.show { transform: translateX(-50%) translateY(0); }
#toast.error { background: #c62828; }

/* ---------- 교사 화면 ---------- */
.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tab { background: #eef0fb; color: var(--ink); padding: 8px 16px; border-radius: 10px; font-size: 1.05rem; }
.tab.active { background: var(--ink); color: #fff; }
.panel { background: #fff; border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); margin-bottom: 20px; }
.panel h2 { margin: 0 0 14px; font-weight: normal; font-size: 1.5rem; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 1.05rem; color: var(--ink-soft); }
.input, .select, .textarea {
  border: 3px solid #d9dcef; border-radius: 12px; padding: 10px 12px; background: #fff; color: var(--ink); font-size: 1.1rem;
  font-family: 'Apple SD Gothic Neo', 'Malgun Gothic', 'Noto Sans KR', system-ui, sans-serif;
}
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--blue); }
.textarea { min-height: 180px; resize: vertical; line-height: 1.6; }
.hint-text { color: var(--ink-soft); font-size: 0.98rem; line-height: 1.5; }

.tlist { display: flex; flex-direction: column; gap: 10px; }
.titem { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 14px 16px; border-radius: 14px; background: #f7f8fe; flex-wrap: wrap; }
.titem .name { font-size: 1.3rem; }
.titem .meta { color: var(--ink-soft); font-size: 0.95rem; }

table.tbl { width: 100%; border-collapse: collapse; font-family: 'Apple SD Gothic Neo', 'Malgun Gothic', 'Noto Sans KR', system-ui, sans-serif; font-size: 1rem; }
.tbl th, .tbl td { padding: 9px 10px; border-bottom: 1px solid #eceef8; text-align: left; vertical-align: middle; }
.tbl th { background: #f2f3fb; font-weight: 600; color: var(--ink-soft); position: sticky; top: 0; }
.tbl td.editable { cursor: text; border-radius: 6px; }
.tbl td.editable:hover { background: #fff8d6; }
.tbl td.editable:focus { outline: 3px solid var(--blue); background: #fff; }
.tbl td.editable:empty::before { content: '(눌러서 입력)'; color: #b4b8d0; }
.tbl tr.err td { background: #ffecec; color: #b3261e; }
.tbl tr.best td { background: #fff6cc; }
.tbl .del { background: #ffe1e1; color: #c62828; border-radius: 8px; padding: 4px 10px; font-size: 1rem; }
.table-wrap { overflow-x: auto; max-height: 520px; overflow-y: auto; border-radius: 12px; border: 2px solid #eceef8; }
.parse-status { margin: 10px 0; font-size: 1.05rem; }
.parse-status .ok { color: #1e8e3e; }
.parse-status .bad { color: #c62828; }
.err-list { color: #b3261e; font-size: 0.98rem; margin: 6px 0 0; padding-left: 20px; font-family: system-ui, sans-serif; }

.bars { display: flex; flex-direction: column; gap: 8px; margin-top: 6px; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 160px) 1fr 70px; gap: 10px; align-items: center; font-size: 1rem; }
.bar-row .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { background: #f0f1f8; border-radius: 8px; height: 26px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 8px; transition: width 0.5s; }
.bar-row .val { text-align: right; }

.saved-note { color: #1e8e3e; font-size: 1rem; }
.name-preview { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.name-preview span { background: #eef0fb; padding: 4px 10px; border-radius: 999px; font-size: 0.95rem; font-family: system-ui, sans-serif; }

@media (max-width: 600px) {
  body { font-size: 16px; }
  .number-grid { grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); gap: 10px; }
  .panel { padding: 14px; }
  .btn.big { font-size: 1.3rem; padding: 14px 26px; }
}
