@import url('https://fonts.googleapis.com/css2?family=Mali:ital,wght@0,400;0,500;0,600;0,700;1,400;1,600;1,700&display=swap');

:root {
  --purple: #7c3aed;
  --purple-dark: #5b21b6;
  --pink: #ec4899;
  --blue: #3b82f6;
  --green: #22c55e;
  --orange: #f97316;
  --red: #ef4444;
  --gold: #fbbf24;
  --silver: #cbd5e1;
  --bronze: #d97706;
  --bg: #f5f3ff;
  --card: #ffffff;
  --text: #1e1b2e;
  --muted: #6b7280;
  --radius: 18px;
  --shadow: 0 8px 24px rgba(124, 58, 237, 0.12);
}

* { box-sizing: border-box; }

html { font-size: 22px; }

body {
  margin: 0;
  font-family: 'Mali', 'Prompt', sans-serif;
  font-weight: 700;
  background: linear-gradient(160deg, #ede9fe 0%, #fdf2f8 50%, #eff6ff 100%);
  color: var(--text);
  min-height: 100vh;
}

h1, h2, h3, h4 { font-weight: 800; }

/* เพิ่มความหนาให้ดูชัดกว่าตัวหนาปกติ (ฟอนต์รองรับสูงสุดแค่ 700) */
h1, h2, h3, .btn, .choice-btn, .q-text, .lvl-title, .brand,
.result-score, .reward-pill, .timer-pill, .score-pill, .stat-box .num {
  -webkit-text-stroke: 0.5px currentColor;
}

.container {
  max-width: 1320px;
  margin: 0 auto;
  padding: 20px;
}

/* ===== Top bar ===== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 22px;
  background: linear-gradient(90deg, var(--purple), var(--pink));
  color: #fff;
  box-shadow: var(--shadow);
  flex-wrap: wrap;
  gap: 10px;
}

.topbar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 800;
  font-size: 1.25rem;
}

.topbar .user-pill {
  display: flex;
  align-items: center;
  gap: 14px;
  background: rgba(255,255,255,0.18);
  padding: 8px 16px;
  border-radius: 999px;
  font-weight: 600;
  flex-wrap: wrap;
}

.topbar .user-pill span { display: flex; align-items: center; gap: 5px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: 999px;
  padding: 15px 26px;
  font-family: inherit;
  font-weight: 700;
  font-size: 1.15rem;
  cursor: pointer;
  transition: transform .12s ease, box-shadow .12s ease, opacity .12s ease;
}
.btn:hover { transform: translateY(-2px); }
.btn:active { transform: translateY(0); }

.btn-primary { background: linear-gradient(90deg, var(--purple), var(--pink)); color: #fff; box-shadow: 0 6px 16px rgba(124,58,237,.35); }
.btn-secondary { background: #fff; color: var(--purple); border: 2px solid var(--purple); }
.btn-success { background: linear-gradient(90deg, #22c55e, #16a34a); color: #fff; }
.btn-danger { background: #fee2e2; color: var(--red); }
.btn-ghost { background: rgba(255,255,255,0.2); color: #fff; }
.btn-block { width: 100%; }
.btn[disabled] { opacity: .5; cursor: not-allowed; transform: none; }

/* ===== Cards ===== */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px;
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
}

@media (max-width: 800px) {
  .grid-2 { grid-template-columns: 1fr; }
}

/* ===== Auth ===== */
.auth-tabs { display: flex; gap: 8px; margin-bottom: 20px; background: #f3f0ff; padding: 6px; border-radius: 999px; }
.auth-tab {
  flex: 1; text-align: center; padding: 10px; border-radius: 999px; cursor: pointer;
  font-weight: 700; color: var(--muted);
}
.auth-tab.active { background: linear-gradient(90deg, var(--purple), var(--pink)); color: #fff; }

.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-weight: 600; margin-bottom: 6px; font-size: .92rem; }
.form-group input, .form-group select {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 2px solid #e5e0fa;
  font-family: inherit; font-size: 1rem; background: #fbfaff;
}
.form-group input:focus, .form-group select:focus { outline: none; border-color: var(--purple); }

.msg { padding: 10px 14px; border-radius: 10px; margin-bottom: 14px; font-weight: 600; font-size: .9rem; }
.msg-error { background: #fee2e2; color: #b91c1c; }
.msg-success { background: #dcfce7; color: #15803d; }

/* ===== Hero ===== */
.hero { text-align: center; padding: 30px 10px; }
.hero h1 { font-size: 2.6rem; margin: 0 0 6px; }
.hero p { color: var(--muted); font-weight: 600; font-size: 1.1rem; margin: 0; }

/* ===== Stats overview ===== */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 12px;
  margin: 20px 0;
}
.stat-box {
  background: #fff; border-radius: 14px; padding: 16px; text-align: center; box-shadow: var(--shadow);
}
.stat-box .num { font-size: 2rem; font-weight: 800; color: var(--purple); }
.stat-box .label { font-size: .8rem; color: var(--muted); margin-top: 4px; }

/* ===== Podium ===== */
.podium {
  display: flex; align-items: flex-end; justify-content: center; gap: 14px;
  margin: 30px 0 10px; flex-wrap: wrap;
}
.podium-item { text-align: center; width: 130px; }
.podium-avatar {
  width: 60px; height: 60px; border-radius: 50%; margin: 0 auto 8px;
  display: flex; align-items: center; justify-content: center; font-size: 1.6rem;
  color: #fff; font-weight: 800; box-shadow: 0 6px 14px rgba(0,0,0,.15);
  animation: floaty 2.4s ease-in-out infinite;
}
.podium-name { font-weight: 700; font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.podium-gold-amt { font-size: .82rem; color: var(--muted); font-weight: 600; }
.podium-bar {
  border-radius: 14px 14px 0 0; margin-top: 10px; display: flex; align-items: flex-start;
  justify-content: center; padding-top: 8px; font-size: 1.8rem; font-weight: 800; color: #fff;
}
.podium-1 .podium-avatar { background: linear-gradient(135deg, #fde047, var(--gold)); width: 76px; height: 76px; font-size: 2rem; }
.podium-1 .podium-bar { height: 110px; background: linear-gradient(180deg, #fde047, var(--gold)); }
.podium-2 .podium-avatar { background: linear-gradient(135deg, #e2e8f0, var(--silver)); }
.podium-2 .podium-bar { height: 80px; background: linear-gradient(180deg, #e2e8f0, var(--silver)); }
.podium-3 .podium-avatar { background: linear-gradient(135deg, #fbbf7a, var(--bronze)); }
.podium-3 .podium-bar { height: 60px; background: linear-gradient(180deg, #fbbf7a, var(--bronze)); }

@keyframes floaty {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

.rank-table { width: 100%; border-collapse: collapse; margin-top: 10px; }
.rank-table th, .rank-table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid #f0edfb; }
.rank-table th { color: var(--muted); font-size: .82rem; font-weight: 700; }
.rank-table td { font-weight: 600; }
.rank-badge {
  display: inline-flex; align-items: center; justify-content: center; width: 26px; height: 26px;
  border-radius: 50%; background: #f3f0ff; color: var(--purple); font-weight: 800; font-size: .82rem;
}

.empty-note { text-align: center; color: var(--muted); padding: 20px; font-size: .9rem; }

/* ===== Levels ===== */
.level-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 16px; }
.level-card {
  background: #fff; border-radius: var(--radius); padding: 20px; text-align: center; box-shadow: var(--shadow);
  cursor: pointer; border: 3px solid transparent; transition: transform .12s ease, border-color .12s ease;
}
.level-card:hover { transform: translateY(-4px); }
.level-card.locked { cursor: not-allowed; opacity: .55; }
.level-card .lvl-num {
  width: 72px; height: 72px; border-radius: 50%; margin: 0 auto 10px; display: flex; align-items: center;
  justify-content: center; font-weight: 800; font-size: 1.9rem; color: #fff;
  background: linear-gradient(135deg, var(--purple), var(--pink));
}
.level-card.locked .lvl-num { background: #cbd5e1; }
.level-card .lvl-title { font-weight: 800; font-size: 1.3rem; margin-bottom: 4px; }
.level-card .lvl-sub { font-size: 1rem; font-weight: 600; color: var(--muted); }

/* ===== Quiz ===== */
.quiz-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; flex-wrap: wrap; gap: 10px; }
.progress-bar { flex: 1; height: 12px; background: #ede9fe; border-radius: 999px; overflow: hidden; margin: 0 16px; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--purple), var(--pink)); transition: width 1s linear; }
.progress-fill.time-low { background: linear-gradient(90deg, var(--orange), var(--red)); }
.timer-pill, .score-pill {
  display: inline-flex; align-items: center; gap: 6px; background: #f3f0ff; color: var(--purple-dark);
  padding: 10px 20px; border-radius: 999px; font-weight: 800; font-size: 1.35rem; white-space: nowrap;
}
.score-pill { background: #dcfce7; color: #15803d; }
.attempted-pill { background: #e0f2fe; color: #075985; }

#quizView {
  min-height: 75vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.question-box { position: relative; text-align: center; padding: 40px 10px; }
.question-box .q-text { font-size: 4.4rem; font-weight: 800; color: var(--purple-dark); }

/* ตัวจับเวลาต่อข้อ (ไม่เกิน 10 วินาที) */
.q-timer-bar-wrap {
  width: 100%; max-width: 420px; margin: 24px auto 0; height: 16px;
  background: #ede9fe; border-radius: 999px; overflow: hidden;
}
.q-timer-bar {
  height: 100%; width: 100%; background: linear-gradient(90deg, var(--green), var(--orange));
}
.q-timer-bar.danger { background: linear-gradient(90deg, var(--orange), var(--red)); }

/* ข้อความชื่นชม / ให้กำลังใจ หลังตอบแต่ละข้อ */
.feedback-msg {
  min-height: 1.6em; margin-top: 16px; font-size: 1.3rem; font-weight: 800;
  opacity: 0; transform: translateY(6px); transition: opacity .2s ease, transform .2s ease;
}
.feedback-msg.show { opacity: 1; transform: translateY(0); animation: pop .3s ease; }
.feedback-praise { color: #15803d; }
.feedback-encourage { color: #c2410c; }
@keyframes pop {
  0% { transform: scale(0.85) translateY(6px); }
  60% { transform: scale(1.05) translateY(0); }
  100% { transform: scale(1) translateY(0); }
}

/* เอฟเฟกต์ระเบิดเมื่อตอบไม่ทัน */
.explosion-overlay {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: 7.5rem; opacity: 0; pointer-events: none; transform: scale(0.3);
}
.explosion-overlay.active { animation: explode .6s ease-out; }
@keyframes explode {
  0% { opacity: 0; transform: scale(0.3) rotate(0deg); }
  30% { opacity: 1; transform: scale(1.5) rotate(-8deg); }
  60% { opacity: 1; transform: scale(1.15) rotate(8deg); }
  100% { opacity: 0; transform: scale(1.7) rotate(0deg); }
}
.shake { animation: shake .5s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-10px); }
  40% { transform: translateX(10px); }
  60% { transform: translateX(-7px); }
  80% { transform: translateX(7px); }
}

.choice-grid {
  display: flex; flex-wrap: nowrap; gap: 14px; margin-top: 30px;
}
.choice-btn {
  flex: 1 1 0; min-width: 0; padding: 30px 10px; border-radius: 20px; border: 3px solid #e5e0fa; background: #fff;
  font-family: 'Mali', 'Prompt', sans-serif; font-size: 2.3rem; font-weight: 800; cursor: pointer; transition: all .12s ease;
}
@media (max-width: 560px) {
  #quizView { min-height: 0; }
  .choice-btn { font-size: 1.5rem; padding: 18px 4px; }
  .question-box .q-text { font-size: 2.8rem; }
  .explosion-overlay { font-size: 4.5rem; }
}
.choice-btn:hover { border-color: var(--purple); background: #f5f3ff; }
.choice-btn.correct { background: #dcfce7; border-color: var(--green); color: #15803d; }
.choice-btn.wrong { background: #fee2e2; border-color: var(--red); color: #b91c1c; }
.choice-btn[disabled] { cursor: not-allowed; }

.result-box { text-align: center; padding: 20px; }
.result-emoji { font-size: 4.6rem; }
.result-score { font-size: 2.8rem; font-weight: 800; color: var(--purple); margin: 10px 0; }
.reward-row { display: flex; justify-content: center; gap: 20px; margin: 18px 0; }
.reward-pill { background: #fff7e6; padding: 14px 24px; border-radius: 999px; font-weight: 800; font-size: 1.5rem; }

/* ===== กล่องสุ่มเพชร ===== */
.box-hint { font-weight: 700; color: var(--purple-dark); margin: 6px 0 16px; }
.box-grid { display: flex; justify-content: center; gap: 18px; margin-bottom: 10px; flex-wrap: wrap; }
.mystery-box {
  width: 100px; height: 100px; border-radius: 20px; border: none; cursor: pointer;
  background: linear-gradient(135deg, var(--purple), var(--pink)); color: #fff; font-size: 2.6rem;
  box-shadow: 0 6px 16px rgba(124,58,237,.35); transition: transform .15s ease;
}
.mystery-box:hover:not([disabled]) { transform: translateY(-4px) scale(1.05); }
.mystery-box[disabled] { cursor: not-allowed; }
.mystery-box.box-faded { opacity: .35; filter: grayscale(60%); }
.mystery-box.box-open {
  background: linear-gradient(135deg, #fde047, var(--gold)); font-size: 1.3rem; font-weight: 800; color: #78350f;
  animation: boxpop .4s ease;
}
@keyframes boxpop {
  0% { transform: scale(0.7) rotate(-8deg); }
  60% { transform: scale(1.15) rotate(4deg); }
  100% { transform: scale(1) rotate(0deg); }
}

/* ===== สรุปผลแยกตามแม่สูตรคูณ ===== */
.weak-callout {
  background: #fff7ed; color: #9a3412; border: 2px solid #fdba74; border-radius: 14px;
  padding: 12px 18px; font-weight: 800; margin: 16px 0 10px; font-size: 1.05rem;
}
.breakdown-table { width: 100%; max-width: 460px; margin: 10px auto 0; border-collapse: collapse; }
.breakdown-table th, .breakdown-table td { padding: 8px 10px; text-align: center; border-bottom: 1px solid #f0edfb; font-size: .95rem; }
.breakdown-table th { color: var(--muted); font-weight: 700; }
.breakdown-table td { font-weight: 700; }
.breakdown-table tr.weak-row td { background: #fff7ed; color: #9a3412; }
.weak-areas-card { margin-top: 24px; }
.weak-areas-card .breakdown-table { max-width: 520px; }

/* ===== Admin tabs ===== */
.tabs { display: flex; gap: 8px; margin-bottom: 20px; }
.tab-btn {
  padding: 10px 20px; border-radius: 12px; border: none; background: #f3f0ff; color: var(--purple);
  font-weight: 700; cursor: pointer; font-family: inherit;
}
.tab-btn.active { background: linear-gradient(90deg, var(--purple), var(--pink)); color: #fff; }

.pending-card {
  background: #fff; border-radius: 14px; padding: 16px 18px; box-shadow: var(--shadow); margin-bottom: 14px;
}
.pending-card .p-top { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 10px; margin-bottom: 10px; }
.pending-card .p-name { font-weight: 800; font-size: 1.05rem; }
.pending-card .p-meta { color: var(--muted); font-size: .85rem; }
.pending-form { display: grid; grid-template-columns: 1fr 1fr auto auto; gap: 10px; align-items: center; }
@media (max-width: 700px) { .pending-form { grid-template-columns: 1fr; } }

.filter-row { display: flex; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.filter-row select, .filter-row input { padding: 10px 14px; border-radius: 10px; border: 2px solid #e5e0fa; font-family: inherit; }
.filter-row input { flex: 1; min-width: 180px; }

.admin-table-wrap { overflow-x: auto; }
.admin-table { width: 100%; border-collapse: collapse; min-width: 640px; }
.admin-table th, .admin-table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid #f0edfb; font-size: .9rem; }
.admin-table th { background: #f8f6ff; color: var(--muted); font-weight: 700; }

.footer-note { text-align: center; color: var(--muted); font-size: .8rem; padding: 20px; }

.spinner {
  width: 30px; height: 30px; border: 4px solid #e5e0fa; border-top-color: var(--purple);
  border-radius: 50%; animation: spin .8s linear infinite; margin: 20px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }
