@import url("https://fonts.googleapis.com/css2?family=Fredoka:wght@400;500;600;700&family=Noto+Sans+TC:wght@400;500;700;900&display=swap");

:root {
  --ink: #2b3340;
  --ink-soft: #5a6575;
  --paper: #fffaf3;
  --card: #ffffff;
  --coral: #ff6b57;
  --coral-dark: #e85a48;
  --pink: #ff4d6a;
  --gold: #ffc53d;
  --sky: #4da3ff;
  --sky-dark: #2b86e6;
  --green: #3cb88a;
  --green-dark: #2a9a72;
  --lilac: #9b7ef0;
  --shadow: 0 10px 30px rgba(43, 51, 64, 0.1);
  --shadow-sm: 0 4px 14px rgba(43, 51, 64, 0.08);
  --radius: 24px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

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

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background:
    radial-gradient(1200px 600px at 10% -10%, #ffe8c7 0%, transparent 55%),
    radial-gradient(900px 500px at 110% 0%, #cde8ff 0%, transparent 50%),
    radial-gradient(800px 500px at 50% 120%, #d8f5e8 0%, transparent 50%),
    var(--paper);
  color: var(--ink);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  font-size: 18px;
  line-height: 1.5;
}

body { padding-top: var(--safe-t); }

button {
  font-family: inherit;
  cursor: pointer;
  border: 0;
}

h1, h2 { margin: 0; }

.app {
  width: min(860px, 100%);
  margin: 0 auto;
  min-height: 100dvh;
  padding: 16px 16px calc(24px + var(--safe-b));
  display: flex;
  flex-direction: column;
}

/* ---- Home ---- */
.home, .end, .game { animation: fade-in 0.4s ease; }
@keyframes fade-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

.hero {
  text-align: center;
  padding: 18px 8px 8px;
}
.float-row {
  display: flex;
  justify-content: center;
  gap: 10px;
  font-size: 2.4rem;
  margin-bottom: 8px;
  filter: drop-shadow(0 6px 8px rgba(0,0,0,0.08));
}
.float-row span { animation: bob 3s ease-in-out infinite; display: inline-block; }
.float-row span:nth-child(2) { animation-delay: 0.4s; }
.float-row span:nth-child(3) { animation-delay: 0.8s; }
.float-row span:nth-child(4) { animation-delay: 1.2s; }
.float-row span:nth-child(5) { animation-delay: 0.2s; }
@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-6deg); }
  50% { transform: translateY(-8px) rotate(6deg); }
}

.badge {
  display: inline-block;
  background: #fff;
  color: var(--coral-dark);
  border: 2px solid #ffd0c7;
  border-radius: 999px;
  padding: 4px 14px;
  font-weight: 700;
  font-size: 0.95rem;
  margin-bottom: 10px;
}
.hero h1 {
  font-size: clamp(1.8rem, 6vw, 2.6rem);
  font-weight: 900;
  letter-spacing: 0.04em;
  line-height: 1.25;
}
.hero .sub {
  margin: 8px 0 0;
  color: var(--ink-soft);
  font-size: 1.15rem;
  font-weight: 500;
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 20px 22px;
  margin: 14px 0;
}

.steps-preview {
  display: grid;
  gap: 12px;
}
.step-row {
  display: grid;
  grid-template-columns: 64px 1fr;
  gap: 12px;
  align-items: center;
  background: #fff8ef;
  border-radius: 18px;
  padding: 12px;
}
.step-row .icon {
  width: 64px;
  height: 64px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  font-size: 1.8rem;
  font-weight: 900;
  color: #fff;
}
.step-row.s1 .icon { background: var(--coral); }
.step-row.s2 .icon { background: var(--sky); }
.step-row.s3 .icon { background: var(--green); }
.step-row h2 { font-size: 1.15rem; font-weight: 800; }
.step-row p { margin: 2px 0 0; color: var(--ink-soft); font-size: 0.98rem; }

.btn {
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 64px;
  padding: 0 28px;
  border-radius: 999px;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: 0.04em;
  box-shadow: 0 8px 0 rgba(0,0,0,0.12);
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s;
  user-select: none;
}
.btn:active { transform: translateY(3px); box-shadow: 0 5px 0 rgba(0,0,0,0.12); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; box-shadow: none; }
.btn-primary { background: var(--coral); color: #fff; width: 100%; }
.btn-primary:hover:not(:disabled) { filter: brightness(1.05); }
.btn-sky { background: var(--sky); color: #fff; }
.btn-green { background: var(--green); color: #fff; }
.btn-ghost {
  background: #fff;
  color: var(--ink);
  border: 3px solid #eadfce;
  box-shadow: 0 5px 0 #eadfce;
}

.teacher-note {
  margin-top: 10px;
  background: #fff;
  border: 2px dashed #e6d5b8;
  border-radius: 18px;
  overflow: hidden;
}
.teacher-note summary {
  list-style: none;
  cursor: pointer;
  padding: 14px 16px;
  font-weight: 700;
  color: var(--ink-soft);
  font-size: 1rem;
}
.teacher-note summary::-webkit-details-marker { display: none; }
.teacher-note summary::before { content: "📘 "; }
.teacher-note .body {
  padding: 0 16px 16px;
  color: var(--ink-soft);
  font-size: 0.98rem;
}
.teacher-note li { margin: 6px 0; }

.footer-note {
  text-align: center;
  color: #8a93a0;
  font-size: 0.9rem;
  margin-top: auto;
  padding-top: 16px;
}

/* ---- Game chrome ---- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}
.progress {
  font-weight: 800;
  font-size: 1.15rem;
  background: #fff;
  border-radius: 999px;
  padding: 8px 16px;
  box-shadow: var(--shadow-sm);
}
.home-mini {
  min-height: 48px;
  padding: 0 16px;
  font-size: 1rem;
  box-shadow: 0 4px 0 #eadfce;
}

.step-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin-bottom: 14px;
}
.step-tab {
  min-height: 56px;
  border-radius: 18px;
  background: #fff;
  color: var(--ink-soft);
  font-weight: 800;
  font-size: 0.95rem;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 3px solid transparent;
  line-height: 1.15;
}
.step-tab .n {
  font-family: Fredoka, "Noto Sans TC", sans-serif;
  font-size: 0.8rem;
  opacity: 0.75;
}
.step-tab.on-1 { border-color: var(--coral); color: var(--coral-dark); background: #fff1ee; }
.step-tab.on-2 { border-color: var(--sky); color: var(--sky-dark); background: #eef6ff; }
.step-tab.on-3 { border-color: var(--green); color: var(--green-dark); background: #eefaf4; }
.step-tab.done { box-shadow: inset 0 0 0 2px #ccefdc; }

.sentence-card {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 14px;
  margin-bottom: 14px;
  min-height: 110px;
}
.sentence {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 2px 0;
  line-height: 2.2;
}
.token {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 0.12em 0.22em;
  margin: 0.08em 0.1em;
  font-size: clamp(1.35rem, 4.6vw, 1.85rem);
  font-weight: 700;
  border-radius: 12px;
  border: 0;
  background: transparent;
  color: var(--ink);
  min-height: 52px;
  min-width: 44px;
}
.token:not(.punct):hover { background: #fff4e5; }
.token.punct {
  min-width: 0;
  min-height: 0;
  padding: 0 1px;
  cursor: default;
  font-weight: 500;
}
.token.circled::before {
  content: "";
  position: absolute;
  inset: -4px -7px;
  border: 3.5px solid var(--pink);
  border-radius: 50% 46% 52% 48%;
  transform: rotate(-7deg);
  pointer-events: none;
  animation: pop-circle 0.22s ease;
  box-shadow: 0 0 0 3px rgba(255, 77, 106, 0.12);
}
@keyframes pop-circle {
  from { transform: rotate(-7deg) scale(0.7); opacity: 0; }
  to { transform: rotate(-7deg) scale(1); opacity: 1; }
}
.token.locked { pointer-events: none; }

.panel {
  background: #fff;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  flex: 1;
  display: flex;
  flex-direction: column;
}
.hint-title {
  font-weight: 800;
  font-size: 1.2rem;
  margin-bottom: 4px;
}
.hint-title.s1 { color: var(--coral-dark); }
.hint-title.s2 { color: var(--sky-dark); }
.hint-title.s3 { color: var(--green-dark); }
.hint-sub { color: var(--ink-soft); margin: 0 0 12px; }

/* objects */
.obj-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.counter {
  font-weight: 800;
  background: #eef6ff;
  color: var(--sky-dark);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 1.05rem;
}
.counter.ok { background: #e8f8f0; color: var(--green-dark); }
.counter.over { background: #ffe8e8; color: #c92a2a; }
.btn-restore {
  min-height: 48px;
  padding: 0 16px;
  font-size: 1.05rem;
  box-shadow: 0 4px 0 #eadfce;
}

.obj-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 10px;
  touch-action: none;
  user-select: none;
  padding: 4px 0 8px;
}
.obj {
  aspect-ratio: 1;
  border-radius: 50%;
  border: 3px solid #ffe0b8;
  background: #fff8ee;
  font-size: clamp(1.6rem, 7vw, 2.3rem);
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: 0 4px 0 #f3d7b0;
  transition: transform 0.12s ease, filter 0.2s, opacity 0.2s;
  position: relative;
  transform: rotate(var(--tilt, 0deg));
}
.obj:active { transform: scale(0.92); }
.obj.removing {
  animation: fly-away 0.45s ease forwards;
  pointer-events: none;
}
.obj.removed {
  opacity: 0.35;
  filter: grayscale(0.7);
  box-shadow: none;
  border-style: dashed;
  border-color: #c5c0b8;
  transform: scale(0.9) rotate(var(--tilt, 0deg));
}
.obj.removed::after {
  content: "";
  position: absolute;
  width: 70%;
  height: 4px;
  background: #c92a2a;
  border-radius: 4px;
  transform: rotate(-25deg);
}
@keyframes fly-away {
  0% { transform: scale(1) rotate(0) translateY(0); opacity: 1; }
  60% { transform: scale(1.15) rotate(-18deg) translateY(-18px); }
  100% { transform: scale(0.9) rotate(12deg) translateY(0); opacity: 0.35; }
}

/* keypad */
.keypad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin: 8px 0 12px;
}
.key {
  min-height: 56px;
  border-radius: 16px;
  background: #f4f7fb;
  color: var(--ink);
  font-family: Fredoka, "Noto Sans TC", sans-serif;
  font-size: 1.35rem;
  font-weight: 700;
  border: 3px solid transparent;
  box-shadow: 0 3px 0 #d9e1ec;
}
.key:active { transform: translateY(2px); box-shadow: none; }
.key.on {
  background: #e8f8f0;
  border-color: var(--green);
  color: var(--green-dark);
}
.key.zero { grid-column: 2 / 5; }

.message {
  min-height: 52px;
  border-radius: 16px;
  padding: 10px 14px;
  margin: 8px 0 12px;
  font-weight: 700;
  display: none;
}
.message.show { display: block; }
.message.info { background: #eef6ff; color: var(--sky-dark); }
.message.warn { background: #fff4df; color: #9a6b00; }
.message.ok { background: #e8f8f0; color: var(--green-dark); }
.message.err { background: #ffe8e8; color: #c92a2a; }

.actions { display: flex; gap: 10px; margin-top: auto; }
.actions .btn { flex: 1; }

.done-banner {
  display: none;
  text-align: center;
  background: #e8f8f0;
  border-radius: 18px;
  padding: 12px;
  margin-bottom: 10px;
  font-weight: 800;
  color: var(--green-dark);
  font-size: 1.2rem;
}
.done-banner.show { display: block; }
.tick { font-size: 1.4rem; margin-right: 6px; }

/* celebration */
.celebrate {
  position: fixed;
  inset: 0;
  background: rgba(43, 51, 64, 0.28);
  display: none;
  place-items: center;
  z-index: 20;
  padding: 20px;
}
.celebrate.show { display: grid; }
.celebrate .pop {
  background: #fff;
  border-radius: 28px;
  padding: 28px 24px 22px;
  text-align: center;
  width: min(360px, 100%);
  box-shadow: var(--shadow);
  animation: pop-in 0.3s ease;
}
@keyframes pop-in {
  from { transform: scale(0.8); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
.stars { font-size: 2.4rem; letter-spacing: 6px; }
.celebrate h2 { font-size: 2rem; margin: 8px 0; }
.celebrate p { color: var(--ink-soft); margin: 0 0 16px; }

/* end */
.end { text-align: center; padding-top: 24px; }
.end .big-emoji { font-size: 4rem; }
.end h1 { font-size: clamp(1.8rem, 6vw, 2.4rem); margin: 8px 0 12px; }
.retry-list {
  text-align: left;
  background: #fff8ef;
  border-radius: 16px;
  padding: 14px 16px;
  color: var(--ink-soft);
  margin: 0 0 16px;
}
.retry-list strong { color: var(--ink); }

.hidden { display: none !important; }

@media (min-width: 700px) {
  .obj-grid { grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: 12px; }
  .key { min-height: 64px; font-size: 1.5rem; }
}
@media (max-width: 420px) {
  .step-tab { font-size: 0.82rem; min-height: 52px; padding: 4px; }
  .token { min-height: 46px; }
}

#obj-area, #keypad-area { flex: 0 0 auto; margin-top: 10px; }
#panel-2, #panel-3 { flex: 0 0 auto; }
@media (max-height: 720px) {
  .obj-grid { gap: 6px; }
}
