:root {
  --bg: #fff4d6;
  --bg2: #ffe7b3;
  --ink: #3d2c1e;
  --ink-soft: #7a6450;
  --card: #ffffff;
  --green: #4caf50;
  --green-d: #388e3c;
  --orange: #ff9800;
  --orange-d: #ef6c00;
  --blue: #29b6f6;
  --blue-d: #0288d1;
  --red: #ef5350;
  --shadow: 0 6px 0 rgba(61, 44, 30, .15);
  --radius: 28px;
  --ui-font: "Mali", "Leelawadee UI", "Tahoma", sans-serif;
  --letter-font: "Sarabun", "Leelawadee UI", "Tahoma", sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(circle at 50% 0%, #fffaf0 0%, var(--bg) 55%, var(--bg2) 100%);
  background-attachment: fixed;
  color: var(--ink);
  font-family: var(--ui-font);
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

button {
  font-family: inherit;
  color: inherit;
  border: none;
  cursor: pointer;
  touch-action: manipulation;
}

.hidden { display: none !important; }

/* ---------- หน้าจอ ---------- */
.screen {
  display: none;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 16px;
  padding-top: 26px;
  flex-direction: column;
  align-items: center;
}
.screen.active { display: flex; animation: fade-in .25s ease; }
@keyframes fade-in { from { opacity: 0; transform: scale(.98); } to { opacity: 1; transform: none; } }

.topbar {
  width: 100%;
  max-width: 900px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.screen-title { margin: 0; font-size: clamp(22px, 5vw, 32px); text-align: center; flex: 1; }
.icon-btn, .icon-spacer { width: 60px; height: 60px; flex: none; }
.icon-btn {
  font-size: 30px;
  border-radius: 20px;
  background: var(--card);
  box-shadow: var(--shadow);
}
.icon-btn:active, .big-btn:active, .menu-card:active, .tile:active, .round-btn:active, .group-tab:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 rgba(61, 44, 30, .15);
}

.stars-pill {
  background: var(--card);
  border-radius: 999px;
  padding: 10px 22px;
  font-size: 26px;
  box-shadow: var(--shadow);
}

/* ---------- หน้าเริ่ม ---------- */
.start-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 8px;
}
.mascot { font-size: clamp(100px, 26vw, 170px); line-height: 1; }
.bob { animation: bob 2.4s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-12px) rotate(-4deg); } }
.title {
  margin: 0;
  font-size: clamp(40px, 11vw, 76px);
  color: var(--orange-d);
  text-shadow: 0 4px 0 #fff;
  letter-spacing: 1px;
}
.title.small { font-size: clamp(34px, 9vw, 56px); }
.subtitle { margin: 0 0 18px; font-size: clamp(20px, 5vw, 28px); color: var(--ink-soft); }

.big-btn {
  font-size: clamp(26px, 6vw, 36px);
  font-weight: 700;
  padding: 18px 48px;
  border-radius: 999px;
  color: #fff;
  box-shadow: 0 8px 0 rgba(0, 0, 0, .18);
}
.btn-green { background: var(--green); }
.btn-orange { background: var(--orange); }

.parent-link {
  position: fixed;
  right: 16px;
  bottom: 16px;
  background: rgba(255, 255, 255, .7);
  padding: 8px 14px;
  border-radius: 14px;
  font-size: 15px;
  color: var(--ink-soft);
}

/* ---------- เมนู ---------- */
.menu {
  flex: 1;
  width: 100%;
  max-width: 900px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 24px;
  align-content: center;
}
.menu-card {
  border-radius: 36px;
  padding: 34px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  color: #fff;
  box-shadow: 0 10px 0 rgba(0, 0, 0, .18);
}
.card-orange { background: linear-gradient(160deg, #ffb74d, var(--orange-d)); }
.card-blue { background: linear-gradient(160deg, #4fc3f7, var(--blue-d)); }
.menu-icon { font-size: clamp(72px, 18vw, 110px); line-height: 1.1; }
.menu-letters {
  font-family: var(--letter-font);
  font-weight: 700;
  background: #fff;
  color: var(--orange-d);
  border-radius: 28px;
  padding: 0 22px;
  font-size: clamp(56px, 14vw, 84px);
}
.menu-label { font-size: clamp(30px, 7vw, 42px); font-weight: 700; }

/* ---------- ด่าน 1 ---------- */
.group-tabs {
  width: 100%;
  max-width: 900px;
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding: 4px 4px 12px;
  scrollbar-width: none;
}
.group-tabs::-webkit-scrollbar { display: none; }
.group-tab {
  flex: none;
  min-width: 64px;
  height: 64px;
  padding: 0 10px;
  border-radius: 20px;
  font-family: var(--letter-font);
  font-size: 30px;
  font-weight: 700;
  color: #fff;
  box-shadow: var(--shadow);
  opacity: .55;
  position: relative;
}
.group-tab.selected { opacity: 1; outline: 4px solid #fff; outline-offset: -4px; }
.group-tab.locked { background: #c9bba8 !important; font-size: 26px; }
.group-tab .done {
  position: absolute;
  top: -6px;
  right: -6px;
  font-size: 20px;
}

.letter-grid {
  width: 100%;
  max-width: 900px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 18px;
  padding-bottom: 24px;
}
.tile {
  aspect-ratio: 1;
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: var(--shadow);
  font-family: var(--letter-font);
  font-weight: 700;
  font-size: clamp(64px, 16vw, 92px);
  line-height: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  border-bottom: 10px solid var(--tile-color, var(--orange));
}
.tile .mini-pic { font-size: 26px; position: absolute; top: 10px; right: 12px; }
.tile .star-badge { font-size: 20px; position: absolute; top: 10px; left: 12px; }
.locked-msg {
  grid-column: 1 / -1;
  text-align: center;
  font-size: 24px;
  color: var(--ink-soft);
  padding: 40px 10px;
}

/* การ์ดตัวอักษร */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(61, 44, 30, .45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 20;
  animation: fade-in .2s ease;
}
.letter-card, .celebrate-card, .gate-card {
  background: var(--card);
  border-radius: 40px;
  padding: 28px 24px 24px;
  width: min(460px, 100%);
  text-align: center;
  position: relative;
  box-shadow: 0 12px 0 rgba(0, 0, 0, .15);
}
.close-btn {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: #f3ebe0;
  font-size: 22px;
}
.card-letter {
  font-family: var(--letter-font);
  font-weight: 700;
  font-size: clamp(130px, 38vw, 190px);
  line-height: 1.1;
  color: var(--tile-color, var(--orange-d));
}
.card-pic { font-size: clamp(80px, 22vw, 110px); line-height: 1.1; }
.card-pic.pop, .card-letter.pop { animation: pop .45s ease; }
@keyframes pop { 40% { transform: scale(1.18) rotate(-5deg); } }
.card-word { font-size: clamp(32px, 8vw, 44px); font-weight: 700; margin: 8px 0 18px; }
.card-nav { display: flex; justify-content: center; gap: 18px; }
.round-btn {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  background: #f3ebe0;
  font-size: 30px;
  box-shadow: var(--shadow);
}
.btn-hear-sm { background: var(--blue); color: #fff; }

/* ---------- ด่าน 2 ---------- */
.quiz-wrap {
  flex: 1;
  width: 100%;
  max-width: 900px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.hear-btn {
  width: 130px;
  height: 130px;
  border-radius: 50%;
  background: var(--blue);
  color: #fff;
  font-size: 62px;
  box-shadow: 0 10px 0 var(--blue-d);
}
.hear-btn.speaking { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.08); } }
.quiz-hint { font-size: clamp(20px, 5vw, 26px); color: var(--ink-soft); margin: 6px 0 14px; }
.choices {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(var(--n, 2), minmax(0, 220px));
  justify-content: center;
  gap: clamp(14px, 4vw, 30px);
}
.choices .tile { font-size: clamp(80px, 22vw, 130px); }
.tile.correct { background: #e8f5e9; border-bottom-color: var(--green); animation: pop .5s ease; }
.tile.wrong { animation: shake .45s ease; opacity: .35; pointer-events: none; }
@keyframes shake { 20%, 60% { transform: translateX(-10px); } 40%, 80% { transform: translateX(10px); } }
.tile .reveal-pic {
  position: absolute;
  bottom: -8px;
  right: -8px;
  font-size: 54px;
  animation: pop .5s ease;
}

/* ---------- พัก ---------- */
.break-stars { font-size: 26px; margin: 0 0 22px; }

/* ---------- สติกเกอร์ ---------- */
.sticker-hint { margin: 0 0 14px; font-size: 20px; color: var(--ink-soft); }
.sticker-grid {
  width: 100%;
  max-width: 900px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 14px;
}
.sticker {
  aspect-ratio: 1;
  border-radius: 24px;
  background: var(--card);
  box-shadow: var(--shadow);
  font-size: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.sticker.empty { background: rgba(255, 255, 255, .45); box-shadow: none; border: 3px dashed #d9c7ad; }

/* ---------- ฉลอง ---------- */
.celebrate-icon { font-size: 120px; line-height: 1.2; animation: pop .6s ease; }
.celebrate-text { font-size: clamp(26px, 7vw, 34px); font-weight: 700; margin: 6px 0 22px; }

.fx { position: fixed; inset: 0; pointer-events: none; z-index: 30; overflow: hidden; }
.confetti {
  position: absolute;
  top: -40px;
  font-size: 32px;
  animation: fall linear forwards;
}
@keyframes fall { to { transform: translateY(110vh) rotate(540deg); } }
.fly-star {
  position: absolute;
  font-size: 48px;
  transition: transform .8s cubic-bezier(.5, -0.3, .7, 1), opacity .8s;
}

/* ---------- แถบเวลา ---------- */
.timebar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 14px;
  z-index: 10;
  padding: 4px 8px 0;
}
.timebar-track { height: 8px; background: rgba(255, 255, 255, .7); border-radius: 99px; overflow: hidden; }
.timebar-fill { height: 100%; width: 0; background: linear-gradient(90deg, #ffd54f, #ff8a65); transition: width 1s linear; }
.timebar-sun { position: absolute; top: -2px; font-size: 18px; transform: translateX(-50%); left: 8px; transition: left 1s linear; }

/* ---------- ผู้ปกครอง / ประตู ---------- */
.gate-card p { font-size: 20px; margin: 0 0 10px; }
.gate-q { font-size: 36px !important; font-weight: 700; }
.gate-input {
  width: 160px;
  font-size: 32px;
  text-align: center;
  padding: 10px;
  border-radius: 16px;
  border: 3px solid #e0d2bf;
  font-family: inherit;
  margin-bottom: 16px;
}
.gate-actions { display: flex; gap: 12px; justify-content: center; }
.small-btn {
  font-size: 20px;
  padding: 12px 26px;
  border-radius: 16px;
  background: #f3ebe0;
  font-weight: 700;
}
.small-btn.btn-green, .small-btn.btn-orange { color: #fff; }
.small-btn.btn-red { background: var(--red); color: #fff; }

.parent { user-select: text; -webkit-user-select: text; }
.parent-body {
  width: 100%;
  max-width: 820px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding-bottom: 40px;
}
.panel {
  background: var(--card);
  border-radius: 24px;
  padding: 18px 20px;
  box-shadow: var(--shadow);
}
.panel h3 { margin: 0 0 10px; font-size: 22px; }
.panel p { margin: 6px 0; font-size: 17px; line-height: 1.6; }
.stat-row { display: flex; flex-wrap: wrap; gap: 12px; }
.stat {
  flex: 1 1 140px;
  background: #fff8ec;
  border-radius: 16px;
  padding: 12px;
  text-align: center;
}
.stat b { display: block; font-size: 30px; }
.stat span { font-size: 15px; color: var(--ink-soft); }
.week { display: flex; align-items: flex-end; gap: 8px; height: 120px; margin-top: 8px; }
.week-day { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; font-size: 13px; color: var(--ink-soft); }
.week-bar { width: 100%; max-width: 40px; background: #ffcc80; border-radius: 8px 8px 0 0; min-height: 2px; }
.week-day small { margin-top: 4px; }
.group-row { display: flex; align-items: center; gap: 8px; margin: 8px 0; flex-wrap: wrap; }
.group-row .gname { width: 92px; white-space: nowrap; font-size: 15px; color: var(--ink-soft); }
.chip {
  font-family: var(--letter-font);
  font-weight: 700;
  font-size: 22px;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #eee5d8;
  color: #a8987f;
}
.chip.learning { background: #fff3c4; color: var(--ink); }
.chip.mastered { background: #c8e6c9; color: #1b5e20; }
.legend { font-size: 14px; color: var(--ink-soft); display: flex; gap: 14px; flex-wrap: wrap; margin-top: 8px; }
.legend i { display: inline-block; width: 14px; height: 14px; border-radius: 4px; vertical-align: middle; margin-right: 4px; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 10px 0; flex-wrap: wrap; font-size: 17px; }
.seg { display: flex; gap: 6px; }
.seg button { padding: 8px 14px; border-radius: 12px; background: #f3ebe0; font-size: 16px; }
.seg button.on { background: var(--orange); color: #fff; font-weight: 700; }
.warn { background: #fff3e0; border-left: 6px solid var(--orange); padding: 10px 12px; border-radius: 10px; }
.ok { color: var(--green-d); font-weight: 700; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 8px; }

@media (max-width: 480px) {
  .letter-grid { grid-template-columns: repeat(3, 1fr); gap: 12px; }
  .tile { font-size: 56px; border-radius: 22px; border-bottom-width: 8px; }
  .tile .mini-pic { font-size: 18px; top: 6px; right: 8px; }
  .tile .star-badge { font-size: 14px; top: 6px; left: 8px; }
  .icon-btn, .icon-spacer { width: 52px; height: 52px; font-size: 26px; }
  .hear-btn { width: 110px; height: 110px; font-size: 52px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
