:root {
  --bg1: #DFF3FF;
  --bg2: #FFF4DC;
  --ink: #2B2D42;
  --muted: #6B7280;
  --ok: #2BB673;
  --no: #FF5A5F;
  --gold: #FFB703;
  --card: #FFFFFF;
  --c: #3DB2FF;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: linear-gradient(180deg, var(--bg1) 0%, var(--bg2) 100%) fixed;
  user-select: none; -webkit-user-select: none;
  touch-action: manipulation;
  overflow: hidden;
  overscroll-behavior: none;
}
#app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; padding: 0; }
input { font: inherit; }

.screen {
  flex: 1; min-height: 0;
  display: flex; flex-direction: column;
  padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  animation: fadeIn .25s ease-out;
}

/* ---------- buttons ---------- */
.btn {
  background: var(--c); color: #fff;
  border-radius: 20px;
  padding: 14px 28px;
  font-weight: 800;
  font-size: clamp(20px, 3.2vmin, 30px);
  box-shadow: 0 6px 0 rgba(0,0,0,.2);
  transition: transform .08s, box-shadow .08s;
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
}
.btn:active { transform: translateY(4px); box-shadow: 0 2px 0 rgba(0,0,0,.2); }
.btn.ghost { background: #fff; color: var(--ink); box-shadow: 0 6px 0 #D7DCE5; }
.btn.small { padding: 10px 18px; font-size: clamp(16px, 2.4vmin, 22px); border-radius: 14px; }
.btn.danger { background: var(--no); }

.icon-btn {
  width: clamp(52px, 8vmin, 72px); height: clamp(52px, 8vmin, 72px);
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 4px 0 #D7DCE5;
  font-size: clamp(24px, 4vmin, 36px);
  display: inline-flex; align-items: center; justify-content: center;
  flex: none;
}
.icon-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #D7DCE5; }

.pill {
  background: #fff; border-radius: 999px; padding: 8px 18px;
  font-weight: 800; font-size: clamp(18px, 3vmin, 28px);
  box-shadow: 0 4px 0 #D7DCE5;
  display: inline-flex; align-items: center; gap: 8px; white-space: nowrap;
}

/* ---------- home ---------- */
.home-top { display: flex; align-items: center; gap: 12px; margin-bottom: clamp(10px, 2vmin, 20px); }
.home-top .hello { flex: 1; min-width: 0; }
.home-top h1 { margin: 0; font-size: clamp(26px, 5vmin, 46px); font-weight: 900; line-height: 1.1; }
.home-top p { margin: 4px 0 0; color: var(--muted); font-weight: 700; font-size: clamp(15px, 2.4vmin, 22px); }
.grid {
  flex: 1; min-height: 0; overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(clamp(150px, 26vmin, 240px), 1fr));
  gap: clamp(12px, 2.2vmin, 22px);
  align-content: start;
  padding: 4px 4px 16px;
}
.card {
  background: var(--c);
  border-radius: 28px;
  padding: clamp(12px, 2vmin, 18px);
  color: #fff;
  box-shadow: 0 8px 0 rgba(0,0,0,.18);
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  transition: transform .08s, box-shadow .08s;
  text-align: center;
}
.card:active { transform: translateY(5px); box-shadow: 0 3px 0 rgba(0,0,0,.18); }
.card .ic {
  width: 100%; aspect-ratio: 1.35;
  background: rgba(255,255,255,.92); border-radius: 20px;
  display: flex; align-items: center; justify-content: center;
  font-size: clamp(48px, 9vmin, 84px);
}
.card .nm { font-weight: 900; font-size: clamp(18px, 2.9vmin, 26px); line-height: 1.15; }
.card .lv { font-size: clamp(14px, 2.2vmin, 20px); letter-spacing: 2px; color: #FFF3B0; text-shadow: 0 1px 0 rgba(0,0,0,.2); }

.rest {
  margin: auto; text-align: center; background: #fff; border-radius: 32px;
  padding: clamp(24px, 5vmin, 48px); box-shadow: 0 10px 0 #D7DCE5; max-width: 640px;
}
.rest .big { font-size: clamp(80px, 16vmin, 140px); }
.rest h2 { font-size: clamp(26px, 4.5vmin, 40px); margin: 8px 0; }
.rest p { font-size: clamp(18px, 3vmin, 26px); color: var(--muted); font-weight: 700; }

/* ---------- game ---------- */
.topbar { display: flex; align-items: center; gap: clamp(8px, 1.6vmin, 14px); }
.dots { flex: 1; display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.dot { width: clamp(12px, 2vmin, 18px); height: clamp(12px, 2vmin, 18px); border-radius: 50%; background: rgba(0,0,0,.12); }
.dot.cur { background: var(--c); transform: scale(1.25); }
.dot.good { background: var(--ok); }
.dot.meh { background: var(--gold); }
.starcount.pop { animation: pop .5s; }

.prompt {
  text-align: center; font-weight: 900;
  font-size: clamp(24px, 4.6vmin, 44px);
  margin: clamp(8px, 2vmin, 18px) auto 0;
  max-width: 1000px; line-height: 1.25;
  display: flex; align-items: center; justify-content: center; gap: 12px;
}
.prompt.long { font-size: clamp(19px, 3.2vmin, 30px); font-weight: 800; background: #fff; border-radius: 22px; padding: 12px 20px; box-shadow: 0 4px 0 #D7DCE5; }
.visual {
  flex: 1 1 auto; min-height: 0; width: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: clamp(8px, 2vmin, 18px);
  padding: clamp(6px, 1.5vmin, 14px) 0;
}
.choices {
  display: flex; flex-wrap: wrap; justify-content: center;
  gap: clamp(12px, 2.4vmin, 24px);
  padding-bottom: 6px;
}
.choice {
  min-width: clamp(96px, 17vmin, 180px);
  min-height: clamp(96px, 17vmin, 180px);
  padding: 8px 18px;
  background: #fff;
  border-radius: 26px;
  border: 5px solid transparent;
  box-shadow: 0 7px 0 #D7DCE5, 0 12px 24px rgba(0,0,0,.06);
  font-size: clamp(40px, 8.5vmin, 84px);
  font-weight: 900;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  transition: transform .08s, box-shadow .08s;
  line-height: 1;
}
.choice:active { transform: translateY(5px); box-shadow: 0 2px 0 #D7DCE5; }
.choice.ok { border-color: var(--ok); background: #E6F9EF; animation: pop .45s; }
.choice.reveal { animation: pulse 1s infinite; }
.choice.no { animation: shake .4s; opacity: .3; pointer-events: none; }
.choice small { font-size: clamp(14px, 2.2vmin, 20px); font-weight: 800; color: var(--muted); margin-top: 6px; }
.choices.txt .choice { font-size: clamp(24px, 4.4vmin, 42px); min-height: clamp(80px, 13vmin, 130px); padding: 8px 24px; }
.choices.pics .choice { padding: 12px; }
.choices.pics .choice svg { width: clamp(90px, 17vmin, 170px); height: auto; display: block; }
.choices.emo .choice { font-size: clamp(48px, 10vmin, 96px); }

.feedback { min-height: clamp(56px, 9vmin, 80px); display: flex; align-items: center; justify-content: center; }
.explain { font-weight: 800; font-size: clamp(18px, 3vmin, 28px); color: var(--ink); background: #FFF6D6; border-radius: 16px; padding: 8px 16px; margin-right: 14px; }

/* ---------- visual pieces ---------- */
.egrid { display: grid; gap: clamp(4px, 1vmin, 10px); justify-items: center; align-items: center; }
.eitem { font-size: var(--es, 48px); line-height: 1.1; position: relative; transition: transform .15s; }
.eitem.cross { opacity: .35; }
.eitem.cross::before {
  content: ""; position: absolute; left: -6%; right: -6%; top: 50%; height: 6px; margin-top: -3px;
  background: var(--no); border-radius: 3px; transform: rotate(-35deg);
}
.eitem.counted { transform: scale(.9); }
.eitem.counted::after {
  content: attr(data-k);
  position: absolute; right: -8px; top: -6px;
  background: var(--c); color: #fff; font-size: clamp(14px, 2.4vmin, 22px); font-weight: 900;
  min-width: 1.6em; height: 1.6em; border-radius: 999px; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 0 rgba(0,0,0,.2);
}
.egrid.tens { padding: 8px; border-radius: 16px; background: rgba(255,255,255,.6); }
.group { background: rgba(255,255,255,.7); border-radius: 22px; padding: clamp(8px, 1.6vmin, 14px); }
.row { display: flex; align-items: center; justify-content: center; gap: clamp(10px, 2vmin, 22px); flex-wrap: wrap; }
.op { font-size: clamp(40px, 8vmin, 80px); font-weight: 900; color: var(--c); }
.eq { font-size: clamp(44px, 10vmin, 110px); font-weight: 900; letter-spacing: .04em; white-space: nowrap; }
.slot {
  display: inline-block; min-width: 1.2em; text-align: center;
  color: var(--gold); border: 4px dashed var(--gold); border-radius: 18px; padding: 0 .15em; line-height: 1.15;
}
.slot.filled { color: var(--ok); border-color: var(--ok); border-style: solid; background: #E6F9EF; animation: pop .45s; }
svg .slot { fill: #C77D00; border: 0; }
svg .slot.filled { fill: var(--ok); }

.cmp { display: flex; align-items: center; justify-content: center; gap: clamp(12px, 3vmin, 40px); }
.cmp-side { display: flex; flex-direction: column; align-items: center; gap: 10px; background: rgba(255,255,255,.7); border-radius: 24px; padding: clamp(10px, 2vmin, 20px); min-width: clamp(120px, 22vmin, 240px); }
.cmp-num { font-size: clamp(44px, 10vmin, 100px); font-weight: 900; line-height: 1; }
.cmp .slot { font-size: clamp(44px, 10vmin, 100px); font-weight: 900; }

.seq { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: clamp(6px, 1.4vmin, 14px); max-width: 100%; }
.seq > span { font-size: var(--es, 56px); line-height: 1.2; background: rgba(255,255,255,.75); border-radius: 18px; padding: 4px 8px; min-width: 1.4em; text-align: center; font-weight: 900; }
.seq > span.slot { background: #FFF6D6; }
.seq > span.slot.filled { background: #E6F9EF; }

.bond { width: min(70vmin, 420px); height: auto; }
.bond circle { stroke-width: 5; }
.bond text { font-weight: 900; font-size: 52px; text-anchor: middle; dominant-baseline: central; fill: var(--ink); font-family: var(--font); }

.scene { width: min(92vw, 720px); max-height: 100%; height: auto; background: rgba(255,255,255,.75); border-radius: 24px; }
.scene .sh { cursor: pointer; }
.scene .sh.counted { opacity: .45; }
.scene .badge { font-weight: 900; font-size: 22px; text-anchor: middle; dominant-baseline: central; fill: #fff; font-family: var(--font); }

.clock { width: clamp(180px, 40vmin, 380px); height: auto; }
.choices .clock { width: clamp(110px, 20vmin, 190px); }

.week { display: flex; gap: clamp(4px, 1vmin, 10px); flex-wrap: wrap; justify-content: center; }
.week .day { background: #fff; border-radius: 16px; padding: 8px 6px; min-width: clamp(64px, 11vmin, 110px); text-align: center; font-weight: 800; font-size: clamp(15px, 2.6vmin, 24px); box-shadow: 0 4px 0 #D7DCE5; line-height: 1.2; }
.week .day.today { background: var(--c); color: #fff; box-shadow: 0 4px 0 rgba(0,0,0,.2); }
.week .day.today::after { content: "hôm nay"; display: block; font-size: .7em; opacity: .9; }

.sudoku { display: grid; grid-template-columns: repeat(4, 1fr); background: #fff; border-radius: 18px; padding: 6px; box-shadow: 0 6px 0 #D7DCE5; }
.sudoku .cell {
  width: clamp(54px, 11vmin, 96px); height: clamp(54px, 11vmin, 96px);
  display: flex; align-items: center; justify-content: center;
  font-size: clamp(32px, 7vmin, 62px);
  border: 2px solid #E5E7EB;
  transition: background .2s;
}
.sudoku .cell:nth-child(4n+2) { border-right: 5px solid #9CA3AF; }
.sudoku .cell:nth-child(n+5):nth-child(-n+8) { border-bottom: 5px solid #9CA3AF; }
.sudoku .cell.empty { background: #F3F4F6; }
.sudoku .cell.hl { background: #FFF3C4; }
.sudoku .cell.q { background: #FFE08A; }
.sudoku .cell .slot { border: 0; padding: 0; font-weight: 900; }

.story-pic { display: flex; align-items: center; justify-content: center; gap: clamp(10px, 2vmin, 20px); flex-wrap: wrap; }
.eitem.gone { opacity: .3; transform: translateY(-10px) scale(.85); }

.hint-row { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; animation: fadeIn .3s; }
.hint-row span { width: clamp(22px, 4vmin, 36px); height: clamp(22px, 4vmin, 36px); border-radius: 50%; background: #D1D5DB; }
.hint-row span.on { background: var(--c); }

/* ---------- result ---------- */
.result { align-items: center; justify-content: center; text-align: center; gap: clamp(8px, 2vmin, 18px); }
.result .big { font-size: clamp(90px, 20vmin, 170px); line-height: 1; animation: bounce 1.2s infinite; }
.result h1 { margin: 0; font-size: clamp(32px, 6vmin, 56px); }
.result .msg { font-size: clamp(20px, 3.4vmin, 32px); font-weight: 800; color: var(--muted); }
.result .stars { font-size: clamp(30px, 6vmin, 56px); font-weight: 900; }
.result .new { background: #fff; border-radius: 26px; padding: 14px 26px; box-shadow: 0 6px 0 #D7DCE5; font-weight: 800; font-size: clamp(18px, 3vmin, 26px); }
.result .new .st { font-size: clamp(60px, 12vmin, 110px); display: block; animation: pop .6s; }
.result .actions { display: flex; gap: 16px; flex-wrap: wrap; justify-content: center; margin-top: 8px; }

/* ---------- stickers ---------- */
.book { flex: 1; min-height: 0; overflow-y: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(clamp(80px, 13vmin, 120px), 1fr)); gap: 12px; padding: 8px 4px 16px; align-content: start; }
.book .sl { aspect-ratio: 1; background: #fff; border-radius: 22px; display: flex; align-items: center; justify-content: center; font-size: clamp(44px, 8vmin, 72px); box-shadow: 0 5px 0 #D7DCE5; }
.book .sl.locked { background: rgba(255,255,255,.5); color: #C4C9D4; font-weight: 900; font-size: clamp(30px, 5vmin, 44px); box-shadow: none; border: 3px dashed #D1D5DB; }
.title-row { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.title-row h2 { margin: 0; flex: 1; font-size: clamp(24px, 4.4vmin, 40px); }
.sub { color: var(--muted); font-weight: 700; font-size: clamp(16px, 2.6vmin, 22px); margin: 0 0 6px; }

/* ---------- parent ---------- */
.parent { overflow-y: auto; }
.parent .wrap { max-width: 860px; width: 100%; margin: 0 auto; display: flex; flex-direction: column; gap: 14px; padding-bottom: 24px; }
.panel { background: #fff; border-radius: 20px; padding: 16px 18px; box-shadow: 0 4px 0 #D7DCE5; font-size: 17px; }
.panel h3 { margin: 0 0 10px; font-size: 19px; }
.panel .line { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 8px 0; }
.panel .line > label { min-width: 170px; font-weight: 700; }
.panel input[type=text] { border: 2px solid #D1D5DB; border-radius: 12px; padding: 8px 12px; font-size: 18px; min-width: 220px; user-select: text; -webkit-user-select: text; }
.seg { display: inline-flex; background: #F1F3F7; border-radius: 12px; padding: 3px; gap: 3px; flex-wrap: wrap; }
.seg button { padding: 8px 14px; border-radius: 10px; font-weight: 700; font-size: 16px; }
.seg button.on { background: var(--c); color: #fff; }
.note { color: var(--muted); font-size: 15px; line-height: 1.45; margin: 6px 0 0; }
.note.warn { color: #B45309; background: #FFF7E6; padding: 10px 12px; border-radius: 12px; }
table { width: 100%; border-collapse: collapse; font-size: 16px; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid #EEF0F4; }
th { color: var(--muted); font-weight: 700; font-size: 14px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- modal (parent gate) ---------- */
.overlay { position: fixed; inset: 0; background: rgba(20,24,40,.45); display: flex; align-items: center; justify-content: center; z-index: 50; animation: fadeIn .2s; }
.modal { background: #fff; border-radius: 28px; padding: 22px; width: min(92vw, 420px); text-align: center; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal h3 { margin: 0 0 4px; font-size: 22px; }
.modal .q { font-size: 40px; font-weight: 900; margin: 8px 0; }
.modal .disp { font-size: 36px; font-weight: 900; min-height: 52px; border-bottom: 3px solid #E5E7EB; margin: 0 30px 12px; }
.modal .disp.bad { color: var(--no); animation: shake .4s; }
.keys { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.keys button { background: #F1F3F7; border-radius: 16px; font-size: 28px; font-weight: 800; padding: 14px 0; }
.keys button:active { background: #E2E6EE; }
.keys button.go { background: var(--ok); color: #fff; }

/* ---------- effects ---------- */
.floater { position: fixed; z-index: 40; font-size: 44px; font-weight: 900; pointer-events: none; animation: floatUp 1s ease-out forwards; }
.confetti { position: fixed; top: -20px; width: 12px; height: 18px; border-radius: 3px; z-index: 30; pointer-events: none; animation: fall linear forwards; }

@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.18); } 100% { transform: scale(1); } }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-12px); } 40% { transform: translateX(12px); } 60% { transform: translateX(-8px); } 80% { transform: translateX(8px); } }
@keyframes pulse { 0%,100% { box-shadow: 0 7px 0 #D7DCE5, 0 0 0 0 rgba(43,182,115,.6); } 50% { box-shadow: 0 7px 0 #D7DCE5, 0 0 0 14px rgba(43,182,115,0); } }
@keyframes bounce { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-14px); } }
@keyframes floatUp { from { opacity: 1; transform: translateY(0) scale(1); } to { opacity: 0; transform: translateY(-90px) scale(1.4); } }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
