:root {
  --ink: #23324d; --blue: #2f5fd0; --navy: #1d3a8a; --line: #c9dcf2; --thick: #2a4a8c; --paper: #fffef9; --cream: #fffbea;
  --red: #e2503f; --yellow: #f6c945; --sel: #fff07a; --tint: #eaf3ff; --same: #d4f0b4; --err: #ffd9d3; --green: #5aa832; --orange: #f08a24;
  --bs: 360px; --cs: 40px; --g: 8px; --fs: 15px; --pw: 300px;
  --font: "Nunito", "Segoe UI", "Trebuchet MS", Roboto, "Helvetica Neue", Arial, system-ui, sans-serif;
  --grid-bg: linear-gradient(#e3edf9 1px, transparent 1px) 0 0 / 1.1em 1.1em, linear-gradient(90deg, #e3edf9 1px, transparent 1px) 0 0 / 1.1em 1.1em;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font-family: var(--font); color: var(--ink); font-size: var(--fs);
  background: #e9c48f url(img/bg.webp) center / cover no-repeat; touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
button { font-family: inherit; font-size: inherit; color: inherit; cursor: pointer; border: 0; background: none; padding: 0; touch-action: manipulation; }
.hidden { display: none !important; }
#app { position: fixed; inset: 0; display: flex; gap: var(--g); padding: var(--g); justify-content: center; align-items: center; }

/* ---------- board: a page of squared notebook paper ---------- */
#boardWrap { width: var(--bs); height: var(--bs); flex: none; position: relative; }
#boardWrap::before { /* paper sheet under the board, slightly rotated */
  content: ""; position: absolute; inset: -1.5%; background: #fff; border-radius: 4px; transform: rotate(-1.4deg);
  box-shadow: 0 10px 24px rgba(90,60,20,.35);
}
#board {
  position: relative; width: 100%; height: 100%; display: grid; gap: 3px; padding: 3px;
  grid-template-columns: repeat(var(--bcols, 3), 1fr); grid-template-rows: repeat(var(--brows, 3), 1fr);
  background: var(--thick); border-radius: 6px; box-shadow: 0 2px 0 rgba(0,0,0,.08);
}
.box { display: grid; gap: 1px; background: var(--line); grid-template-columns: repeat(var(--icols, 3), 1fr); grid-template-rows: repeat(var(--irows, 3), 1fr); }
.cell { background: var(--paper); position: relative; display: flex; align-items: center; justify-content: center; cursor: pointer; min-width: 0; min-height: 0; }
.cell .v { font-size: calc(var(--cs) * .6); font-weight: 700; line-height: 1; color: var(--blue); }
body.big .cell .v { font-size: calc(var(--cs) * .74); }
.cell.given .v { color: var(--ink); font-weight: 800; }
.cell.hinted .v { color: var(--green); }
.cell.wrong .v { color: var(--red); text-decoration: line-through; text-decoration-thickness: 2px; }
.cell.tint { background: var(--tint); }
.cell.same { background: var(--same); }
.cell.conf { background: var(--err); }
.cell.sel { background: var(--sel); box-shadow: inset 0 0 0 2px #e5b100; }
.cell.unit { background: #fff6c4; }
.cell.flash { animation: flash .6s ease-out; }
.cell.pop .v { animation: pop .28s ease-out; }
@keyframes flash { 0% { background: #c9f0a4; } 100% { } }
@keyframes pop { 0% { transform: scale(.4) rotate(-8deg); } 70% { transform: scale(1.18); } 100% { transform: scale(1); } }
.cell .n { position: absolute; inset: 6%; display: grid; grid-auto-rows: 1fr; }
.cell .n span { font-size: calc(var(--cs) * .24); line-height: 1; display: flex; align-items: center; justify-content: center; color: #7a8aa8; font-weight: 700; }
#board.n4 .cell .n span, #board.n6 .cell .n span { font-size: calc(var(--cs) * .2); }
.cell .n span.hl { color: #fff; background: var(--blue); border-radius: 30%; }
/* kids grids: every digit has its own crayon colour */
#board.n4 .cell .v, #board.n6 .cell .v { font-size: calc(var(--cs) * .56); }
#board.n4 .cell.d1 .v, #board.n6 .cell.d1 .v { color: #e2503f; }
#board.n4 .cell.d2 .v, #board.n6 .cell.d2 .v { color: #2f5fd0; }
#board.n4 .cell.d3 .v, #board.n6 .cell.d3 .v { color: #3f9a28; }
#board.n4 .cell.d4 .v, #board.n6 .cell.d4 .v { color: #d9820f; }
#board.n6 .cell.d5 .v { color: #8a4cc9; }
#board.n6 .cell.d6 .v { color: #d63b8f; }
#board.n4 .cell.wrong .v, #board.n6 .cell.wrong .v { color: var(--red); }

/* ---------- panel ---------- */
#panel { width: var(--pw); height: var(--bs); display: flex; flex-direction: column; gap: calc(var(--g) * .8); min-width: 0; }
#top { display: flex; align-items: center; gap: .5em; background: var(--paper); border-radius: .6em; padding: .35em .6em; box-shadow: 0 4px 10px rgba(90,60,20,.25); flex: none; border-left: 4px solid var(--red); }
.logo { display: flex; align-items: baseline; gap: .25em; color: var(--navy); margin-right: auto; white-space: nowrap; min-width: 0; overflow: hidden; }
.logo b { font-size: 1.15em; font-weight: 900; }
.logo small { font-size: .85em; font-weight: 800; color: var(--orange); }
.tag { background: var(--blue); color: #fff; font-weight: 800; font-size: .8em; padding: .2em .6em; border-radius: 1em; white-space: nowrap; flex: none; max-width: 10em; overflow: hidden; text-overflow: ellipsis; }
.tag.l0, .dl.l0 { background: #3f9a28; } .tag.l1, .dl.l1 { background: #2f8fd0; } .tag.l2, .dl.l2 { background: #d9820f; } .tag.l3, .dl.l3 { background: var(--red); }
.tag.l4, .tag.l5 { background: #8a4cc9; }
.dl { color: #fff !important; border-radius: 1em; margin: .1em .2em; font-weight: 800; }
.stat { display: flex; align-items: center; gap: .25em; font-size: .9em; white-space: nowrap; color: var(--navy); }
.stat .lbl { font-size: .85em; opacity: .8; }
.ico { width: 1.1em; height: 1.1em; }
.snd { display: flex; gap: .2em; }
.ib { width: 2em; height: 2em; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: var(--tint); color: var(--navy); }
.ib svg { width: 1.2em; height: 1.2em; }
#coachBox { display: flex; align-items: center; gap: .5em; flex: none; min-height: 0; }
#snail { width: 4.4em; height: 4.4em; object-fit: contain; flex: none; filter: drop-shadow(0 3px 3px rgba(90,60,20,.35)); }
#snail.bounce { animation: bounce .5s; }
@keyframes bounce { 30% { transform: translateY(-12%) rotate(-4deg); } 60% { transform: translateY(0) scale(.97); } }
#bubble > span { display: block; }
#bubble {
  flex: 1; background: var(--paper); background: var(--grid-bg), var(--paper); border-radius: .5em; padding: .45em .7em; font-size: .9em; line-height: 1.4; position: relative;
  box-shadow: 0 4px 10px rgba(90,60,20,.22); min-height: 2.6em; display: flex; align-items: center;
}
#bubble::before { content: ""; position: absolute; left: -.45em; top: 50%; margin-top: -.45em; border: .45em solid transparent; border-right-color: var(--paper); border-left: 0; }
#bubble b { color: var(--red); }
#tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: .5em; flex: none; }
.tool { /* sticky notes */
  position: relative; background: #fff3a6; border-radius: .2em .2em .9em .2em; padding: .35em .2em; display: flex; flex-direction: column; align-items: center; gap: .1em;
  color: var(--navy); font-weight: 800; font-size: .68em; box-shadow: 0 3px 6px rgba(90,60,20,.3); white-space: nowrap; min-width: 0;
}
.tool:nth-child(2) { background: #ffd9e4; } .tool:nth-child(3) { background: #d6f2c4; } .tool:nth-child(4) { background: #cfe6ff; }
.tool span { overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.tool svg { width: 2em; height: 2em; }
.tool:active { transform: translateY(2px) rotate(-1deg); }
.tool i { position: absolute; top: -.35em; right: .15em; font-style: normal; font-size: .75em; background: var(--red); color: #fff; border-radius: 1em; padding: .05em .45em; }
#notes.on { background: var(--orange); color: #fff; }
#notes.on i { background: var(--navy); }
#pad { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 1fr; gap: .45em; flex: 1 1 auto; min-height: 0; }
#pad.p4 { grid-template-columns: repeat(2, 1fr); }
.num { /* white paper tiles with crayon-coloured digits */
  display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 0; position: relative;
  color: var(--blue); background: var(--paper); border-radius: .7em; box-shadow: 0 3px 0 #c9b48e, 0 5px 10px rgba(90,60,20,.22); border-bottom: 3px solid var(--line);
  container-type: size;
}
.num b { font-size: 1.75em; font-size: min(2.6em, 52cqmin); line-height: 1; font-weight: 800; }
.num small { font-size: .62em; font-size: max(.62em, min(1em, 14cqmin)); color: #8a97b0; font-weight: 700; }
.num.d1 b { color: #e2503f; } .num.d2 b { color: #2f5fd0; } .num.d3 b { color: #3f9a28; } .num.d4 b { color: #d9820f; } .num.d5 b { color: #8a4cc9; }
.num.d6 b { color: #d63b8f; } .num.d7 b { color: #1f9a9a; } .num.d8 b { color: #b0562b; } .num.d9 b { color: #1d3a8a; }
.num:active { transform: translateY(2px); box-shadow: 0 1px 0 #c9b48e; }
.num.done { opacity: .35; }
body.notes-on .num { background: #fff3e2; box-shadow: 0 3px 0 #e2b48c; }
body.notes-on .num b { color: #a0602a !important; font-weight: 600; }
#bottom { display: grid; grid-template-columns: repeat(3, 1fr); gap: .4em; flex: none; }
.chip { background: rgba(255,254,249,.95); color: var(--navy); font-weight: 800; border-radius: 2em; padding: .45em .3em; font-size: .82em; white-space: nowrap; box-shadow: 0 3px 8px rgba(90,60,20,.25); overflow: hidden; text-overflow: ellipsis; min-width: 0; }
#dailyBtn { background: var(--yellow); color: var(--ink); }

/* portrait: stack header, bubble, board, tools, pad, bottom */
body.port #app { flex-direction: column; justify-content: center; }
body.port #panel { display: contents; }
body.port #top { order: 1; width: var(--pw); }
body.port #coachBox { order: 2; width: var(--pw); }
body.port #boardWrap { order: 3; margin: .3em 0; }
body.port #tools { order: 4; width: var(--pw); }
body.port #pad { order: 5; width: var(--pw); grid-template-columns: repeat(9, 1fr); flex: none; height: calc(var(--fs) * 3.6); }
body.port #pad.p6 { grid-template-columns: repeat(6, 1fr); } body.port #pad.p4 { grid-template-columns: repeat(4, 1fr); }
body.port #bottom { order: 6; width: var(--pw); }
body.port #snail { width: 3.4em; height: 3.4em; }
body.port .num b { font-size: 1.6em; font-size: min(1.8em, 52cqmin); }
body.short #snail { width: 3.2em; height: 3.2em; }
body.short #bubble { min-height: 2.3em; padding: .3em .6em; font-size: .85em; }
body.tiny #coachBox { display: none; }
body.short #top { gap: .35em; padding: .3em .45em; }
body.short .tool { font-size: .64em; padding: .3em .05em; }
body.short .tool svg { width: 1.6em; height: 1.6em; }
body.short .stat { font-size: .82em; }
.logo.s1 small { display: none; }
.logo.s2 { visibility: hidden; }
body.short .chip { font-size: .74em; padding: .4em .2em; }

/* ---------- modal: a notebook page ---------- */
#modal { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; padding: 2vh 2vw; background: rgba(70,45,15,.45); }
.card {
  background: var(--grid-bg), var(--paper); border-radius: .5em 1em 1em .5em; width: min(47em, 96vw); max-height: 96vh; display: flex; flex-direction: column;
  box-shadow: 0 18px 50px rgba(60,35,10,.5); overflow: hidden; font-size: clamp(12px, min(2.3vh, 1.9vw) + 4px, 19px);
  border-left: .55em solid var(--red);
}
#cardBody { overflow-y: auto; padding: 1em 1.2em .6em; flex: 1 1 auto; min-height: 0; }
#cardFoot { flex: none; padding: .6em 1.2em .9em; display: flex; gap: .6em; justify-content: center; flex-wrap: wrap; background: rgba(255,251,234,.92); border-top: 1px dashed #d9c9a3; }
#cardFoot:empty { display: none; }
.card h2 { margin: 0 0 .3em; color: var(--navy); font-size: 1.4em; }
.card p { margin: .3em 0; line-height: 1.55; }
.btn { background: var(--green); color: #fff; font-weight: 900; font-size: 1.05em; border-radius: 2em; padding: .6em 1.6em; box-shadow: 0 4px 0 #3b7a1d, 0 8px 16px rgba(60,35,10,.25); white-space: nowrap; }
.btn:active { transform: translateY(2px); box-shadow: 0 2px 0 #3b7a1d; }
.btn.ghost { background: #fff; color: var(--navy); box-shadow: 0 4px 0 #c9b48e; }
.row { display: flex; gap: .6em; justify-content: center; flex-wrap: wrap; }

.menu { display: grid; grid-template-columns: 10em 1fr; gap: 1em; align-items: center; }
.menu .hero { text-align: center; }
.menu .hero img { width: 9em; height: 9em; object-fit: contain; }
.menu .hero h1 { margin: 0; color: var(--navy); font-size: 1.6em; line-height: 1.1; font-weight: 900; }
.menu .hero h1 small { display: block; font-size: .5em; color: var(--orange); margin-top: .2em; }
.intro { font-size: .86em; color: #3d4a66; }
.levels { display: grid; grid-template-columns: 1fr 1fr; gap: .45em; margin: .45em 0; }
.lv { background: #fff; border-radius: .6em; padding: .4em .6em .4em 2.4em; text-align: left; box-shadow: 0 3px 0 #d9c9a3; border: 2px solid transparent; position: relative; min-width: 0; }
.lv i { position: absolute; left: .45em; top: 50%; transform: translateY(-50%); width: 1.55em; height: 1.55em; border-radius: 50%; color: #fff; font-style: normal; font-weight: 900; display: flex; align-items: center; justify-content: center; font-size: .9em; }
.lv.l0 i { background: #3f9a28; } .lv.l1 i { background: #2f8fd0; } .lv.l2 i { background: #d9820f; } .lv.l3 i { background: var(--red); } .lv.l4 i, .lv.l5 i { background: #8a4cc9; }
.lv.l1 { border-color: #9cc8f0; }
.lv b { display: block; font-size: 1em; color: var(--navy); }
.lv span { font-size: .74em; color: #5b6680; display: block; line-height: 1.3; }
.lv em { font-style: normal; font-size: .72em; color: var(--green); font-weight: 800; }
.lv:hover, .lv:focus-visible { border-color: var(--blue); }
.daily-cta { width: 100%; background: var(--yellow); border-radius: .6em; padding: .5em .8em; font-weight: 900; display: flex; justify-content: space-between; align-items: center; gap: .6em; box-shadow: 0 3px 0 #c99a1a; }
.daily-cta span { white-space: nowrap; }
.keys { font-size: .72em; color: #5b6680; margin-top: .45em; line-height: 1.9; display: flex; flex-wrap: wrap; gap: .2em .8em; align-items: center; }
.opts { display: flex; gap: .5em; flex-wrap: wrap; margin-top: .3em; font-size: .78em; }
.opt { background: #fff; border: 1px solid #d9c9a3; border-radius: 1em; padding: .15em .7em; color: var(--navy); }
.opt b { color: var(--red); }
kbd { display: inline-block; background: #fff; border: 1px solid #c9b48e; border-bottom-width: 3px; border-radius: .35em; padding: 0 .4em; font-family: inherit; font-size: .95em; line-height: 1.5; color: var(--ink); }

.week { display: grid; grid-template-columns: repeat(7, 1fr); gap: .4em; margin: .6em 0; }
.day { background: #fff; border-radius: .6em; padding: .4em .1em; text-align: center; box-shadow: 0 3px 0 #d9c9a3; position: relative; border: 2px solid transparent; min-width: 0; }
.day b { display: block; font-size: 1.1em; color: var(--navy); }
.day span { display: block; font-size: .7em; color: #5b6680; }
.day i { display: block; font-style: normal; font-size: .68em; font-weight: 800; color: var(--blue); margin-top: .15em; }
.day.today { border-color: var(--yellow); box-shadow: 0 0 0 3px rgba(246,201,69,.5), 0 0 16px rgba(246,201,69,.95); }
.day.today i { color: var(--red); }
.day span.dl { font-size: .6em; padding: 0 .2em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.day.solved { background: #e6f6dc; }
.day.solved b::after { content: " ✓"; color: var(--green); font-size: .8em; }
.streak { display: flex; gap: 1em; justify-content: center; font-weight: 800; color: var(--navy); }
.streak b { color: var(--red); font-size: 1.3em; }

.guide ol { margin: .3em 0; padding-left: 1.4em; }
.guide li { margin: .45em 0; line-height: 1.5; }
.guide li b { color: var(--navy); }
.win { display: grid; grid-template-columns: 9em 1fr; gap: 1em; align-items: center; }
.win img { width: 9em; height: 9em; object-fit: contain; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: .4em; margin: .5em 0; }
.stats div { background: #fff; border-radius: .5em; padding: .35em .6em; box-shadow: 0 2px 0 #e6d8b6; }
.stats small { display: block; font-size: .72em; color: #5b6680; }
.stats b { color: var(--navy); font-size: 1.1em; }
.newbest { color: var(--red); font-weight: 900; }

@media (max-height: 520px) {
  .menu { grid-template-columns: 7em 1fr; gap: .7em; }
  .menu .hero img { width: 6em; height: 6em; }
  .menu .hero h1 { font-size: 1.3em; }
  .win { grid-template-columns: 6em 1fr; }
  .win img { width: 6em; height: 6em; }
  #cardBody { padding: .6em 1em .4em; }
  #cardFoot { padding: .4em 1em .5em; }
  .btn { padding: .45em 1.3em; }
  .lv span { display: none; }
  .lv { padding-top: .35em; padding-bottom: .35em; }
  .levels { grid-template-columns: 1fr 1fr 1fr; }
  .menu .intro { font-size: .8em; }
  .keys { display: none; }
}
@media (max-height: 400px) {
  .menu .hero img { width: 4.5em; height: 4.5em; }
  .menu .intro { display: none; }
}
@media (max-width: 560px) and (orientation: portrait) {
  .menu { grid-template-columns: 1fr; gap: .2em; }
  .menu .hero { display: flex; align-items: center; justify-content: center; gap: .6em; }
  .menu .hero img { width: 5em; height: 5em; }
  .win { grid-template-columns: 5.5em 1fr; }
  .win img { width: 5.5em; height: 5.5em; }
  .week { gap: .25em; }
  .levels { grid-template-columns: 1fr 1fr; }
}
@media (min-aspect-ratio: 1/1) { .keys .ph { display: none; } }

/* ---------- coach marks ---------- */
#coach { position: fixed; inset: 0; z-index: 60; }
#coachCard {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 3vh; width: min(26em, 92vw);
  background: var(--grid-bg), var(--paper); border-radius: .8em; padding: .8em 1em; box-shadow: 0 14px 40px rgba(0,0,0,.5); font-size: clamp(12px, min(2.3vh, 1.9vw) + 4px, 18px);
  border-left: .4em solid var(--red);
}
#coachCard.top { bottom: auto; top: 3vh; }
#coachCard.mid { bottom: auto; top: 50%; transform: translate(-50%, -50%); }
#coachCard p { margin: 0 0 .6em; line-height: 1.55; }
#coachCard p b { color: var(--red); }
#coachCard .btn { font-size: .95em; padding: .4em 1.2em; }
.coached { position: relative; z-index: 55; box-shadow: 0 0 0 4px var(--yellow), 0 0 0 9999px rgba(50,30,8,.6) !important; border-radius: .8em; }
#fx { position: fixed; inset: 0; pointer-events: none; z-index: 70; width: 100%; height: 100%; }
