@font-face {
  font-family: "Fredoka";
  src: url("fonts/fredoka.woff2") format("woff2");
  font-weight: 500 700;
  font-display: swap;
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+2000-206F, U+20AC;
}
@font-face {
  font-family: "Mali";
  src: url("fonts/mali-thai.woff2") format("woff2");
  font-weight: 600;
  font-display: swap;
  unicode-range: U+0E01-0E5B, U+200C-200D, U+25CC;
}

:root {
  --bg: #FFF6E9;
  --ink: #2B3A55;
  --muted: #7A8599;
  --card: #FFFFFF;
  --shadow: 0 6px 0 rgba(43, 58, 85, 0.08), 0 10px 30px rgba(43, 58, 85, 0.08);
  --th: #FF7EA0;
  --en: #4DA3FF;
  --sv: #FFC21A;
  --font: "Fredoka", "Mali", ui-rounded, "SF Pro Rounded", system-ui, -apple-system, sans-serif;
  --tint: var(--bg);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-weight: 600;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  padding: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

img { -webkit-user-drag: none; pointer-events: none; }

.screen {
  position: fixed;
  inset: 0;
  display: none;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.screen.active { display: flex; }

/* ---------- Wordmark ---------- */
.wordmark {
  margin: 0;
  font-size: 44px;
  font-weight: 700;
  letter-spacing: 0.01em;
  line-height: 1;
  display: flex;
}
.wordmark span { display: inline-block; text-shadow: 0 3px 0 rgba(43, 58, 85, 0.12); }
.wordmark span:nth-child(1) { color: #FF6B6B; }
.wordmark span:nth-child(2) { color: #FF9F43; }
.wordmark span:nth-child(3) { color: #F5B700; }
.wordmark span:nth-child(4) { color: #3DBE6C; }
.wordmark span:nth-child(5) { color: #4DA3FF; }
.wordmark span:nth-child(6) { color: #9B7BFF; }
.wordmark.big { font-size: clamp(64px, 13vw, 128px); }
.wordmark.big span { text-shadow: 0 6px 0 rgba(43, 58, 85, 0.12); animation: hop 2.4s ease-in-out infinite; }
.wordmark.big span:nth-child(2) { animation-delay: 0.1s; }
.wordmark.big span:nth-child(3) { animation-delay: 0.2s; }
.wordmark.big span:nth-child(4) { animation-delay: 0.3s; }
.wordmark.big span:nth-child(5) { animation-delay: 0.4s; }
.wordmark.big span:nth-child(6) { animation-delay: 0.5s; }
@keyframes hop { 0%, 60%, 100% { transform: translateY(0); } 30% { transform: translateY(-10px); } }

/* ---------- Start ---------- */
#start {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  text-align: center;
}
.start-logo {
  width: clamp(140px, 26vmin, 240px);
  border-radius: 28%;
  box-shadow: var(--shadow);
  animation: float 3s ease-in-out infinite;
}
@keyframes float { 50% { transform: translateY(-8px) rotate(-2deg); } }
.tagline { margin: 0 16px; color: var(--muted); font-size: clamp(18px, 2.6vw, 24px); }
.start-btn {
  margin-top: 12px;
  width: 120px; height: 120px;
  border-radius: 50%;
  background: #3DBE6C;
  box-shadow: 0 8px 0 #2A9A54, 0 14px 30px rgba(61, 190, 108, 0.35);
  display: grid; place-items: center;
  animation: pulse 1.6s ease-in-out infinite;
}
.start-btn svg { width: 60px; height: 60px; fill: #fff; margin-left: 8px; }
.start-btn:active { transform: translateY(6px); box-shadow: 0 2px 0 #2A9A54; }
@keyframes pulse { 50% { transform: scale(1.06); } }

/* ---------- Home ---------- */
#home { flex-direction: column; }
.home-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 18px 24px 8px;
}
.home-logo { width: 56px; height: 56px; border-radius: 28%; box-shadow: var(--shadow); }
.home-header .wordmark { flex: 1; }

.icon-btn {
  width: 60px; height: 60px;
  border-radius: 50%;
  background: var(--card);
  box-shadow: var(--shadow);
  display: grid; place-items: center;
  position: relative;
  flex: none;
}
.icon-btn svg { width: 30px; height: 30px; fill: var(--ink); }
.settings-btn { width: 48px; height: 48px; opacity: 0.55; }
.settings-btn svg { width: 24px; height: 24px; transition: transform 0.7s linear; }
.settings-btn.holding svg { transform: rotate(180deg); }
.hold-ring {
  position: absolute; inset: -4px;
  border-radius: 50%;
  border: 4px solid #3DBE6C;
  clip-path: inset(0 100% 0 0);
  pointer-events: none;
}
.settings-btn.holding .hold-ring { transition: clip-path 0.7s linear; clip-path: inset(0 0 0 0); }

.grid {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  touch-action: pan-y;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(200px, 42vw), 1fr));
  gap: 20px;
  padding: 16px 24px 32px;
  align-content: start;
}
.tile {
  background: var(--tile, var(--card));
  border-radius: 32px;
  box-shadow: var(--shadow);
  padding: 18px 12px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  transition: transform 0.12s;
}
.tile:active { transform: scale(0.95); }
.tile-img {
  width: 62%;
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  position: relative;
}
.tile-img img { width: 100%; height: 100%; }
.tile-img .swatch { width: 80%; }
.tile-img.mix img { position: absolute; width: 58%; height: 58%; }
.tile-img.mix img:nth-child(1) { left: 0; top: 4%; transform: rotate(-10deg); }
.tile-img.mix img:nth-child(2) { right: 0; top: 0; transform: rotate(8deg); }
.tile-img.mix img:nth-child(3) { left: 21%; bottom: 0; }
.tile-names { display: flex; flex-direction: column; align-items: center; line-height: 1.4; }
.tile-names span:first-child { font-size: 24px; }
.tile-names span { font-size: 16px; color: var(--muted); }
.tile-names span:first-child { color: var(--ink); }

/* ---------- Card ---------- */
#card {
  flex-direction: column;
  background: var(--tint);
  transition: background-color 0.4s;
  touch-action: none;
}
.card-top {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 16px 24px 0;
}
.progress {
  flex: 1;
  height: 12px;
  border-radius: 6px;
  background: rgba(43, 58, 85, 0.08);
  overflow: hidden;
}
#progressBar {
  height: 100%;
  width: 0;
  border-radius: 6px;
  background: linear-gradient(90deg, #FF7EA0, #FFC21A, #3DBE6C, #4DA3FF);
  background-size: 100vw 100%;
  transition: width 0.35s ease;
}

.stage {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 8px 16px;
}
.picture {
  height: min(100%, 72vw);
  aspect-ratio: 1;
  max-width: 72vw;
  display: grid;
  place-items: center;
  border-radius: 40px;
}
.picture > * { width: 92%; height: 92%; }
.picture .emoji-fallback { font-size: min(46vh, 56vw); line-height: 1; width: auto; height: auto; }
.swatch {
  aspect-ratio: 1;
  border-radius: 50%;
  background: radial-gradient(circle at 32% 28%, rgba(255,255,255,0.45), rgba(255,255,255,0) 38%), var(--c);
  box-shadow: inset 0 -14px 0 rgba(0,0,0,0.12), var(--shadow);
}
.swatch.light { outline: 3px solid rgba(43, 58, 85, 0.12); outline-offset: -3px; }
.picture .swatch { width: 80%; height: 80%; }

.nav-btn {
  flex: none;
  width: 76px; height: 76px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.85);
  box-shadow: var(--shadow);
  display: grid; place-items: center;
  transition: transform 0.1s;
}
.nav-btn:active { transform: scale(0.9); }
.nav-btn svg { width: 40px; height: 40px; fill: none; stroke: var(--ink); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }

.words {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 14px 18px;
  padding: 0 20px 28px;
}
.word {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 26px 10px 16px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: var(--shadow);
  font-size: clamp(30px, 5.2vw, 60px);
  line-height: 1.25;
  transition: transform 0.2s cubic-bezier(.3,1.6,.6,1), background-color 0.2s, color 0.2s;
}
.word .flag { font-size: 0.6em; line-height: 1; }
.word[lang="th"] { font-family: "Mali", var(--font); }
.word.speaking { transform: scale(1.12); color: #fff; }
.word.speaking[data-lang="th"] { background: var(--th); }
.word.speaking[data-lang="en"] { background: var(--en); }
.word.speaking[data-lang="sv"] { background: var(--sv); color: var(--ink); }

/* animations */
.enter-next { animation: enterNext 0.35s cubic-bezier(.2,.9,.3,1.2); }
.enter-prev { animation: enterPrev 0.35s cubic-bezier(.2,.9,.3,1.2); }
@keyframes enterNext { from { transform: translateX(30vw) scale(0.7); opacity: 0; } }
@keyframes enterPrev { from { transform: translateX(-30vw) scale(0.7); opacity: 0; } }
.boing { animation: boing 0.6s cubic-bezier(.3,1.8,.5,1); }
@keyframes boing {
  0% { transform: scale(1); }
  25% { transform: scale(1.12, 0.9); }
  50% { transform: scale(0.94, 1.08) rotate(-3deg); }
  75% { transform: scale(1.04, 0.97) rotate(2deg); }
  100% { transform: scale(1); }
}

/* phones / small portrait: smaller arrows */
@media (max-width: 600px) {
  .nav-btn { width: 52px; height: 52px; }
  .nav-btn svg { width: 28px; height: 28px; }
  .stage { gap: 4px; padding: 8px 6px; }
  .words { gap: 10px; }
  .word { padding: 6px 18px 6px 12px; }
}
/* portrait: stack words vertically so long Thai words fit */
@media (orientation: portrait) and (min-width: 601px) {
  .words { flex-direction: column; align-items: center; }
}

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

/* ---------- Settings ---------- */
dialog {
  border: 0;
  border-radius: 28px;
  padding: 24px 28px;
  width: min(460px, calc(100vw - 32px));
  color: var(--ink);
  font-family: var(--font);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
dialog::backdrop { background: rgba(43, 58, 85, 0.4); }
dialog h2 { margin: 0 0 12px; font-size: 26px; }
dialog h3 { margin: 16px 0 8px; font-size: 16px; color: var(--muted); font-weight: 600; }
.lang-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.lang-list li {
  display: flex; align-items: center; gap: 12px;
  background: var(--bg);
  border-radius: 16px;
  padding: 8px 10px 8px 14px;
  font-size: 20px;
}
.lang-list label { flex: 1; display: flex; align-items: center; gap: 10px; }
.lang-list button {
  width: 40px; height: 40px; border-radius: 12px;
  background: #fff; font-size: 20px;
}
.lang-list button:disabled { opacity: 0.25; }
.toggle { display: flex; align-items: center; gap: 12px; margin-top: 14px; font-size: 18px; }
input[type="checkbox"] { width: 26px; height: 26px; accent-color: #3DBE6C; flex: none; }
.done-btn {
  margin-top: 22px; width: 100%;
  padding: 14px; border-radius: 16px;
  background: #3DBE6C; color: #fff; font-size: 20px;
}
