:root {
  --bg: #FFF8EE;
  --ink: #3B2F4A;
  --muted: #8A7E96;
  --card: #FFFFFF;
  --accent: #FF8A5B;
  --accent-2: #6C9EFF;
  --good: #3CC48B;
  --line: #EFE4D6;
  --shadow: 0 8px 24px rgba(91, 60, 30, .12);
  --radius: 28px;
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }

.screen { display: none; position: fixed; inset: 0; flex-direction: column; }
.screen.active { display: flex; }

/* ---------- Start / break ---------- */
.start-box {
  margin: auto; text-align: center; padding: 24px; max-width: 520px;
}
.start-logo { font-size: 110px; animation: bob 2.4s ease-in-out infinite; }
.start-box h1 { font-size: 48px; margin: 8px 0; }
.start-box p { font-size: 20px; color: var(--muted); line-height: 1.5; }
.start-box .hint { font-size: 15px; }
.big-btn {
  margin: 20px 0; padding: 22px 56px; font-size: 32px; font-weight: 800;
  background: var(--accent); color: #fff; border-radius: 999px; box-shadow: var(--shadow);
  transition: transform .15s;
}
.big-btn:active { transform: scale(.95); }
.link-btn { display: block; margin: 16px auto; color: var(--muted); font-size: 18px; text-decoration: underline; }
.credit { font-size: 14px !important; margin-top: 18px; }
.credit a { color: var(--accent); font-weight: 800; text-decoration: none; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 20px; flex: 0 0 auto;
}
.brand { font-size: 26px; font-weight: 800; }
.stars {
  font-size: 24px; font-weight: 800; background: #FFF1C2; padding: 8px 18px; border-radius: 999px;
}
.stars.pop { animation: pop .5s; }
.icon-btn {
  width: 56px; height: 56px; font-size: 30px; border-radius: 50%;
  background: var(--card); box-shadow: var(--shadow);
}
.play-title { font-size: 26px; font-weight: 800; text-align: center; }
.play-title small { display: block; font-size: 15px; color: var(--muted); font-weight: 600; }

/* Hold-to-activate button (parent gate) */
.hold-btn {
  position: relative; overflow: hidden; padding: 12px 18px; border-radius: 999px;
  background: var(--card); box-shadow: var(--shadow); font-size: 16px; font-weight: 700; color: var(--muted);
}
.hold-btn.wide { padding: 18px 28px; font-size: 18px; margin-top: 16px; }
.hold-fill {
  position: absolute; inset: 0; width: 0; background: #FFE0D2; transition: width 0s;
}
.hold-btn.holding .hold-fill { width: 100%; transition: width 2s linear; }
.hold-label { position: relative; }

/* ---------- Home ---------- */
.cat-grid {
  flex: 1; overflow-y: auto; padding: 8px 20px 28px;
  display: grid; gap: 18px; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); align-content: start;
}
.cat {
  position: relative; border-radius: var(--radius); padding: 22px 12px 18px; text-align: center;
  color: #fff; box-shadow: var(--shadow); transition: transform .15s; min-height: 190px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.cat:active { transform: scale(.95); }
.cat .e { font-size: 84px; line-height: 1.1; filter: drop-shadow(0 4px 6px rgba(0,0,0,.15)); }
.cat .n { font-size: 26px; font-weight: 800; margin-top: 8px; text-shadow: 0 2px 4px rgba(0,0,0,.18); }
.cat .p { position: absolute; top: 10px; right: 14px; font-size: 14px; font-weight: 700; opacity: .9; }

/* ---------- Play ---------- */
.stage { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 0; padding: 0 16px; }
.card {
  position: relative; background: var(--card); border-radius: 40px; box-shadow: var(--shadow);
  width: min(560px, 92vw); height: min(56vh, 520px); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 8px; cursor: pointer;
  border: 8px solid transparent; transition: border-color .3s, transform .3s;
}
.card.enter { animation: enter .45s cubic-bezier(.2,1.4,.4,1); }
.card.listening { border-color: #9EE6C5; }
.card.talking .card-visual { animation: talk .5s ease-in-out infinite; }
.card.happy { animation: happy .7s; }
.card-visual { font-size: min(26vh, 220px); line-height: 1; display: flex; align-items: center; gap: 12px; will-change: transform; }
.card-visual .plus { font-size: .3em; color: var(--muted); }
.card-visual .part { display: flex; flex-direction: column; align-items: center; font-size: .72em; }
.card-visual .part small { font-size: 34px; font-weight: 800; color: var(--muted); margin-top: 6px; }
.card-visual .part.lit small { color: var(--accent); }
.card-visual svg { width: min(30vh, 260px); height: auto; }
.card-word { font-size: min(9vh, 72px); font-weight: 900; letter-spacing: .02em; }

.status {
  height: 64px; margin-top: 14px; font-size: 26px; font-weight: 800; display: flex; align-items: center; gap: 12px;
  color: var(--muted);
}
.status .ear { font-size: 40px; animation: pulse 1s ease-in-out infinite; }
.status .meter { width: 160px; height: 16px; background: var(--line); border-radius: 99px; overflow: hidden; }
.status .meter i { display: block; height: 100%; width: 0; background: var(--good); border-radius: 99px; transition: width .06s; }

.controls { display: flex; justify-content: center; gap: 18px; padding: 8px 16px 0; flex: 0 0 auto; }
.ctl {
  width: 84px; height: 84px; border-radius: 50%; font-size: 36px; background: var(--card); box-shadow: var(--shadow);
  transition: transform .12s;
}
.ctl:active { transform: scale(.9); }
.ctl-main { background: var(--accent-2); color: #fff; width: 100px; height: 100px; font-size: 44px; margin-top: -8px; }
.ctl-star { background: #FFD84D; }
.ctl-labels { display: flex; justify-content: center; gap: 18px; padding: 6px 0 16px; font-size: 13px; color: var(--muted); font-weight: 700; }
.ctl-labels span { width: 84px; text-align: center; }
.ctl-labels span:nth-child(2) { width: 100px; }

/* ---------- Effects ---------- */
#fx { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 50; }
.confetti { position: absolute; font-size: 44px; animation: fly 1.4s ease-out forwards; }

@keyframes bob { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-14px); } }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.35); } 100% { transform: scale(1); } }
@keyframes enter { from { transform: scale(.6) rotate(-4deg); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes talk { 0%,100% { transform: scale(1); } 50% { transform: scale(1.06) rotate(-2deg); } }
@keyframes happy { 0% { transform: none; } 30% { transform: scale(1.08) rotate(3deg); } 60% { transform: scale(.98) rotate(-3deg); } 100% { transform: none; } }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.2); } }
@keyframes fly {
  0% { transform: translate(0,0) scale(.4); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(1.2) rotate(var(--rot)); opacity: 0; }
}

/* ---------- Parent ---------- */
#parent { background: #FAF6F0; user-select: text; -webkit-user-select: text; }
.tabs { display: flex; gap: 8px; padding: 0 20px 10px; overflow-x: auto; flex: 0 0 auto; }
.tabs button {
  padding: 10px 16px; border-radius: 999px; background: var(--card); font-size: 16px; font-weight: 700; white-space: nowrap;
  border: 2px solid var(--line);
}
.tabs button.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.tab-body { flex: 1; overflow-y: auto; padding: 4px 20px 40px; }
.tab { display: none; max-width: 900px; margin: 0 auto; }
.tab.active { display: block; }
.tab h3 { margin: 22px 0 10px; font-size: 20px; }
.panel { background: var(--card); border-radius: 18px; padding: 16px 18px; margin-bottom: 14px; border: 1px solid var(--line); }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 14px 16px; }
.kpi b { display: block; font-size: 30px; }
.kpi span { color: var(--muted); font-size: 14px; }
.days { display: flex; align-items: flex-end; gap: 10px; height: 130px; padding-top: 10px; }
.day { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; font-size: 12px; color: var(--muted); }
.day i { width: 100%; max-width: 46px; background: var(--accent-2); border-radius: 8px 8px 3px 3px; min-height: 3px; }
.day em { font-style: normal; font-weight: 700; color: var(--ink); font-size: 12px; }

.word-row { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.word-row:last-child { border-bottom: 0; }
.word-row .we { font-size: 28px; width: 40px; text-align: center; }
.word-row .ww { font-weight: 800; font-size: 18px; min-width: 110px; }
.word-row .wm { color: var(--muted); font-size: 13px; flex: 1; min-width: 120px; }
.seg { display: inline-flex; border: 2px solid var(--line); border-radius: 999px; overflow: hidden; }
.seg button { padding: 6px 12px; font-size: 14px; font-weight: 700; color: var(--muted); }
.seg button.on[data-v="chua"] { background: #EEE; color: var(--ink); }
.seg button.on[data-v="gan"] { background: #FFE3A3; color: #7A5200; }
.seg button.on[data-v="duoc"] { background: #BFF0D8; color: #11663F; }
.target { font-size: 22px; opacity: .25; }
.target.on { opacity: 1; }

.small-btn {
  padding: 8px 14px; border-radius: 12px; background: #F1ECE4; font-weight: 700; font-size: 15px;
}
.small-btn.primary { background: var(--accent); color: #fff; }
.small-btn.rec { background: #FFD9D9; }
.small-btn.rec.on { background: #E53950; color: #fff; animation: pulse 1s infinite; }
.small-btn.danger { color: #C22; }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.muted { color: var(--muted); }

.field { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.field:last-child { border-bottom: 0; }
.field label { font-weight: 700; }
.field small { display: block; color: var(--muted); font-weight: 400; }
.field input[type=range] { width: 220px; }
.field select, .field input[type=text] { font: inherit; padding: 8px 12px; border-radius: 10px; border: 2px solid var(--line); background: #fff; }
.switch { width: 26px; height: 26px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr 110px auto; gap: 10px; align-items: end; }
.form-grid label { font-size: 13px; color: var(--muted); font-weight: 700; display: flex; flex-direction: column; gap: 4px; }
.form-grid input { font: inherit; padding: 9px 12px; border-radius: 10px; border: 2px solid var(--line); }
@media (max-width: 700px) { .form-grid { grid-template-columns: 1fr 1fr; } }

.guide { font-size: 17px; line-height: 1.6; }
.guide li { margin-bottom: 6px; }
.callout { background: #EEF4FF; border-left: 6px solid var(--accent-2); padding: 14px 16px; border-radius: 12px; margin: 12px 0; }
.callout.warn { background: #FFF1E8; border-left-color: var(--accent); }

.mic-test { display: flex; align-items: center; gap: 12px; }
.mic-test .meter { width: 200px; height: 14px; background: var(--line); border-radius: 99px; overflow: hidden; position: relative; }
.mic-test .meter i { display: block; height: 100%; width: 0; background: var(--good); }
.mic-test .meter b { position: absolute; top: 0; bottom: 0; width: 3px; background: #E53950; }

@media (max-height: 640px) {
  .ctl { width: 66px; height: 66px; font-size: 28px; }
  .ctl-main { width: 78px; height: 78px; font-size: 34px; }
  .ctl-labels span { width: 66px; } .ctl-labels span:nth-child(2) { width: 78px; }
  .status { height: 48px; margin-top: 8px; }
}

/* Nút ủng hộ tác giả (bảng ủng hộ ở /donate.js) */
.donate-pill { margin: 6px auto 0; display: inline-block; background: #FFF0F6; color: #E64980; font-weight: 900; font-size: 16px;
  padding: 10px 20px; border-radius: 999px; border: 2px solid #FFDEEB; }
.donate-pill:active { transform: scale(.96); }

/* ---------- Điện thoại ---------- */
.brand, .stars, .hold-label, .play-title { white-space: nowrap; }
@media (max-width: 600px) {
  .topbar { padding: 10px 12px; gap: 8px; }
  .brand { font-size: 19px; }
  .stars { font-size: 17px; padding: 6px 12px; }
  .hold-btn { padding: 9px 12px; font-size: 14px; }
  .icon-btn { width: 46px; height: 46px; font-size: 24px; }
  .play-title { font-size: 19px; overflow: hidden; text-overflow: ellipsis; }
  .cat-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; padding: 6px 12px 20px; }
  .cat { min-height: 150px; padding: 16px 8px 12px; }
  .cat .e { font-size: 64px; }
  .cat .n { font-size: 20px; }
  .controls { gap: 10px; }
  .ctl { width: 66px; height: 66px; font-size: 28px; }
  .ctl-main { width: 80px; height: 80px; font-size: 36px; margin-top: -6px; }
  .ctl-labels { gap: 10px; font-size: 12px; }
  .ctl-labels span { width: 66px; }
  .ctl-labels span:nth-child(2) { width: 80px; }
  .card { height: min(50vh, 460px); border-radius: 32px; }
  .status { font-size: 20px; }
}
.ctl-labels { padding-bottom: max(16px, env(safe-area-inset-bottom)); }
