:root {
  --ink: #1b2240; --blue: #1f47c6; --royal: #1d3a9e; --line: #b9c6ec; --paper: #fffefa; --cream: #f6f1e3; --kraft: #efe4c8;
  --red: #d8432f; --mustard: #f2b72e; --mint: #3aa57a; --sel: #ffe08a; --tint: #eaf0ff; --same: #cdd9ff; --err: #ffd9d2;
  --bs: 360px; --cs: 40px; --g: 8px; --fs: 15px; --pw: 300px;
  --font: "Segoe UI", "Trebuchet MS", Roboto, "Helvetica Neue", Arial, system-ui, sans-serif;
  --serif: Georgia, "Times New Roman", "DejaVu Serif", serif;
}
* { 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-color: var(--cream);
  background-image:
    linear-gradient(90deg, transparent calc(var(--g) * 2.2), rgba(216,67,47,.45) calc(var(--g) * 2.2), rgba(216,67,47,.45) calc(var(--g) * 2.2 + 2px), transparent calc(var(--g) * 2.2 + 2px)),
    linear-gradient(rgba(31,71,198,.09) 1px, transparent 1px), linear-gradient(90deg, rgba(31,71,198,.09) 1px, transparent 1px);
  background-size: 100% 100%, 22px 22px, 22px 22px;
  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: calc(var(--g) * 1.4); padding: var(--g); justify-content: center; align-items: center; }

/* ---------- board: a sheet of paper taped to the page ---------- */
#boardWrap { width: var(--bs); height: var(--bs); flex: none; position: relative; }
#board {
  width: 100%; height: 100%; display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); gap: 3px; padding: 3px;
  background: var(--royal); border-radius: 4px; box-shadow: 0 1px 0 #fff, 0 10px 24px rgba(60,45,20,.28), 0 0 0 calc(var(--cs) * .12) var(--paper), 0 0 0 calc(var(--cs) * .12 + 1px) #e2dccb;
}
.tape { position: absolute; width: calc(var(--cs) * 1.25); height: calc(var(--cs) * .36); background: rgba(242,183,46,.55); top: calc(var(--cs) * -.12); z-index: 2; pointer-events: none; box-shadow: 0 1px 2px rgba(0,0,0,.08); }
.tape.t1 { left: calc(var(--cs) * -.1); transform: rotate(-32deg); }
.tape.t2 { right: calc(var(--cs) * -.1); transform: rotate(32deg); }
.box { display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); gap: 1px; background: var(--line); }
.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: 600; line-height: 1; color: var(--blue); }
body.big .cell .v { font-size: calc(var(--cs) * .74); }
.cell.given .v { color: var(--ink); font-weight: 700; }
.cell.hinted .v { color: var(--mint); }
.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 var(--mustard); }
.cell.unit { background: #e1f4ea; }
.cell.flash { animation: flash .6s ease-out; }
.cell.pop .v { animation: pop .28s ease-out; }
@keyframes flash { 0% { background: #c5ecd8; } 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-template: repeat(3, 1fr) / repeat(3, 1fr); }
.cell .n span { font-size: calc(var(--cs) * .24); line-height: 1; display: flex; align-items: center; justify-content: center; color: #6a6f88; font-weight: 600; }
body.big .cell .n span { font-size: calc(var(--cs) * .28); }
.cell .n span.hl { color: #fff; background: var(--blue); border-radius: 30%; }

/* ---------- panel ---------- */
#panel { width: var(--pw); height: var(--bs); display: flex; flex-direction: column; gap: calc(var(--g) * .75); min-width: 0; }
#top { display: flex; align-items: center; gap: .55em; background: var(--paper); border-radius: .35em; padding: .3em .6em; box-shadow: 0 3px 8px rgba(60,45,20,.15); border-left: .35em solid var(--blue); flex: none; }
.logo { display: flex; align-items: baseline; gap: .3em; margin-right: auto; white-space: nowrap; min-width: 0; overflow: hidden; }
.logo b { font-family: var(--serif); font-size: 1.3em; color: var(--royal); }
.logo span { font-family: var(--serif); font-style: italic; font-size: 1.05em; color: var(--red); }
.stat { display: flex; align-items: center; gap: .25em; font-size: .92em; white-space: nowrap; color: var(--royal); font-variant-numeric: tabular-nums; }
.ico { width: 1.1em; height: 1.1em; }
.snd { display: flex; gap: .25em; }
.ib { width: 2em; height: 2em; border-radius: .4em; display: flex; align-items: center; justify-content: center; background: var(--tint); color: var(--royal); }
.ib svg { width: 1.2em; height: 1.2em; }

/* notebook index tabs for the level */
#tabs { display: grid; grid-template-columns: repeat(5, 1fr); gap: .25em; flex: none; border-bottom: 3px solid var(--royal); padding: 0 .2em; }
.tab { background: var(--kraft); color: #5b5440; font-weight: 700; font-size: .68em; padding: .45em .05em .35em; letter-spacing: -.01em; border-radius: .6em .6em 0 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; box-shadow: inset 0 -2px 0 rgba(0,0,0,.06); }
.tab.on { background: var(--royal); color: #fff; }
.tab.l0:not(.on) { border-top: 3px solid var(--mint); } .tab.l1:not(.on) { border-top: 3px solid #4aa3df; } .tab.l2:not(.on) { border-top: 3px solid var(--mustard); }
.tab.l3:not(.on) { border-top: 3px solid #ec7a35; } .tab.l4:not(.on) { border-top: 3px solid var(--red); }

#coachBox { display: flex; align-items: center; gap: .5em; flex: none; min-height: 0; }
#dog { width: 4.4em; height: 4.4em; object-fit: contain; flex: none; filter: drop-shadow(0 3px 3px rgba(60,45,20,.25)); transform-origin: 50% 90%; }
#dog.wag { animation: wag .55s; }
@keyframes wag { 25% { transform: rotate(-7deg) translateY(-6%); } 55% { transform: rotate(5deg); } 80% { transform: rotate(-2deg); } }
#bubble > span { display: block; }
#bubble {
  flex: 1; background: var(--paper); border: 2px solid var(--royal); border-radius: .8em; padding: .4em .7em; font-size: .9em; line-height: 1.4; position: relative;
  min-height: 2.6em; display: flex; align-items: center; box-shadow: 3px 3px 0 rgba(29,58,158,.18);
}
#bubble::before { content: ""; position: absolute; left: -.55em; top: 50%; margin-top: -.45em; border: .45em solid transparent; border-right-color: var(--royal); border-left: 0; }
#bubble b { color: var(--red); }

#pad { display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: 1fr; gap: .4em; flex: 1 1 auto; min-height: 0; }
.num {
  display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 0; position: relative;
  color: var(--blue); background: var(--paper); border: 2px solid var(--line); border-radius: .5em; box-shadow: 0 3px 0 #c9cfe6;
  container-type: size;
}
.num b { font-size: 1.75em; font-size: min(2.6em, 54cqmin); line-height: 1; font-weight: 700; font-family: var(--serif); }
.num small { font-size: .62em; font-size: max(.62em, min(1em, 14cqmin)); color: #8a8fa8; font-weight: 700; }
.num:hover { border-color: var(--blue); }
.num:active { transform: translateY(2px); box-shadow: 0 1px 0 #c9cfe6; }
.num.done { opacity: .35; }
body.notes-on .num { color: #6a6f88; background: #fbf6e6; border-style: dashed; border-color: #c8b98d; }

#tools { display: grid; grid-template-columns: repeat(5, 1fr); gap: .4em; flex: none; }
.tool {
  position: relative; background: var(--paper); border-radius: .5em; padding: .3em .15em; display: flex; flex-direction: column; align-items: center; gap: .05em;
  color: var(--royal); font-weight: 700; font-size: .66em; border: 2px solid var(--line); white-space: nowrap; min-width: 0;
}
.tool span { overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.tool svg { width: 2em; height: 2em; }
.tool:active { transform: translateY(2px); }
.tool i { position: absolute; top: -.45em; right: -.2em; font-style: normal; font-size: .72em; background: var(--red); color: #fff; border-radius: 1em; padding: .05em .45em; }
#notes.on { background: var(--mustard); color: var(--ink); border-color: #c8901a; }
#notes.on i { background: var(--royal); }
#bottom { display: grid; grid-template-columns: repeat(4, 1fr); gap: .35em; flex: none; }
.chip { background: var(--paper); color: var(--royal); font-weight: 700; border-radius: .4em; padding: .45em .2em; font-size: .74em; white-space: nowrap; border: 1px solid var(--line); overflow: hidden; text-overflow: ellipsis; min-width: 0; }
#dailyBtn { background: var(--mustard); color: var(--ink); border-color: #c8901a; }

/* portrait: header, tabs, bubble, board, pad, tools, 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 #tabs { order: 2; width: var(--pw); }
body.port #coachBox { order: 3; width: var(--pw); }
body.port #boardWrap { order: 4; margin: calc(var(--g) * .3) 0; }
body.port #pad { order: 5; width: var(--pw); grid-template-columns: repeat(9, 1fr); flex: none; height: calc(var(--fs) * 3.6); }
body.port #tools { order: 6; width: var(--pw); }
body.port #bottom { order: 7; width: var(--pw); }
body.port #dog { width: 3.4em; height: 3.4em; }
body.port .num b { font-size: 1.6em; font-size: min(1.8em, 54cqmin); }
body.short #dog { display: none; }
body.short #bubble::before { display: none; }
body.short #bubble { min-height: 2.3em; padding: .25em .55em; font-size: .84em; }
body.tiny #coachBox { display: none; }
body.short #top { gap: .35em; padding: .25em .45em; }
body.short .logo span { display: none; }
body.short .tool { font-size: .6em; padding: .25em .05em; }
body.short .stat { font-size: .82em; }
body.short .chip { font-size: .72em; padding: .4em .2em; }
body.short .tab { font-size: .68em; }

/* ---------- modal ---------- */
#modal { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; padding: 2vh 2vw; background: rgba(40,34,20,.45); }
.card {
  background: var(--paper); border-radius: .5em; width: min(47em, 96vw); max-height: 96vh; display: flex; flex-direction: column;
  box-shadow: 0 18px 50px rgba(0,0,0,.4); overflow: hidden; font-size: clamp(12px, min(2.3vh, 1.9vw) + 4px, 19px);
  border-top: .45em solid var(--royal);
  background-image: linear-gradient(rgba(31,71,198,.06) 1px, transparent 1px), linear-gradient(90deg, rgba(31,71,198,.06) 1px, transparent 1px); background-size: 18px 18px;
}
#cardBody { overflow-y: auto; padding: .9em 1.2em .5em; flex: 1 1 auto; min-height: 0; }
#cardFoot { flex: none; padding: .55em 1.2em .85em; display: flex; gap: .6em; justify-content: center; flex-wrap: wrap; }
#cardFoot:empty { display: none; }
.card h2 { margin: 0 0 .3em; color: var(--royal); font-size: 1.35em; font-family: var(--serif); }
.card p { margin: .3em 0; line-height: 1.55; }
.btn { background: var(--red); color: #fff; font-weight: 800; font-size: 1.05em; border-radius: .5em; padding: .6em 1.6em; box-shadow: 0 4px 0 #9f2c1d, 0 8px 16px rgba(0,0,0,.15); white-space: nowrap; }
.btn:active { transform: translateY(2px); box-shadow: 0 2px 0 #9f2c1d; }
.btn.ghost { background: var(--paper); color: var(--royal); box-shadow: 0 4px 0 var(--line); border: 2px solid var(--line); }
.row { display: flex; gap: .6em; justify-content: center; flex-wrap: wrap; }

.menu { display: grid; grid-template-columns: 11em 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(--royal); font-size: 1.9em; line-height: 1.1; font-family: var(--serif); }
.menu .hero h1 em { color: var(--red); }
.menu .hero h1 small { display: block; font-size: .4em; color: #6a6450; font-family: var(--font); font-weight: 700; margin-top: .3em; }
.intro { font-size: .88em; color: #3d4366; }
.levels { display: grid; grid-template-columns: 1fr 1fr; gap: .45em; margin: .5em 0; }
.lv { background: var(--paper); border-radius: .5em; padding: .4em .7em .4em 2.4em; text-align: left; border: 2px solid var(--line); position: relative; }
.lv i { position: absolute; left: .5em; top: 50%; transform: translateY(-50%); width: 1.5em; height: 1.5em; border-radius: .35em; color: #fff; font-style: normal; font-weight: 800; display: flex; align-items: center; justify-content: center; font-size: .9em; }
.lv.l0 i { background: var(--mint); } .lv.l1 i { background: #4aa3df; } .lv.l2 i { background: var(--mustard); color: var(--ink); } .lv.l3 i { background: #ec7a35; } .lv.l4 i { background: var(--red); }
.lv b { display: block; font-size: 1em; color: var(--royal); }
.lv span { font-size: .75em; color: #5a5f78; display: block; line-height: 1.35; }
.lv em { font-style: normal; font-size: .72em; color: var(--red); font-weight: 700; }
.lv:hover, .lv:focus-visible { border-color: var(--blue); }
.daily-cta { width: 100%; background: var(--mustard); border-radius: .5em; padding: .5em .8em; font-weight: 800; display: flex; justify-content: space-between; align-items: center; gap: .6em; border: 2px solid #c8901a; }
.daily-cta span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.keys { grid-column: 1 / -1; font-size: .72em; color: #5a5f78; margin-top: .1em; line-height: 1.9; display: flex; flex-wrap: wrap; gap: .2em .8em; align-items: center; }
.opt { background: var(--paper); border: 1px solid var(--line); border-radius: .4em; padding: .1em .6em; font-size: 1em; color: var(--royal); }
.opt b { color: var(--red); }
kbd { display: inline-block; background: #fff; border: 1px solid var(--line); border-bottom-width: 3px; border-radius: .3em; 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: var(--paper); border-radius: .5em; padding: .4em .1em; text-align: center; border: 2px solid var(--line); position: relative; min-width: 0; }
.day b { display: block; font-size: 1.1em; color: var(--royal); }
.day span { display: block; font-size: .7em; color: #5a5f78; }
.day i { display: block; font-style: normal; font-size: .68em; font-weight: 700; color: var(--blue); margin-top: .15em; }
.day.today { border-color: var(--mustard); box-shadow: 0 0 0 3px rgba(242,183,46,.45), 0 0 16px rgba(242,183,46,.9); }
.day.today i { color: var(--red); }
.dl { color: #fff !important; border-radius: .3em; margin: .1em .2em; font-weight: 700; font-size: .6em !important; padding: 0 .1em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dl.l0 { background: var(--mint); } .dl.l1 { background: #4aa3df; } .dl.l2 { background: #c8901a; } .dl.l3 { background: #ec7a35; } .dl.l4 { background: var(--red); }
.day.solved { background: #e4f5ec; }
.day.solved b::after { content: " ✓"; color: var(--mint); font-size: .8em; }
.streak { display: flex; gap: 1em; justify-content: center; font-weight: 700; color: var(--royal); }
.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(--royal); }
.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: .4em; padding: .3em .6em; border: 1px solid var(--line); }
.stats small { display: block; font-size: .72em; color: #5a5f78; }
.stats b { color: var(--royal); font-size: 1.1em; }
.newbest { color: var(--red); font-weight: 800; }

@media (max-height: 520px) {
  .menu { grid-template-columns: 7.5em 1fr; gap: .7em; }
  .menu .hero img { width: 6.5em; height: 6.5em; }
  .menu .hero h1 { font-size: 1.55em; }
  .win { grid-template-columns: 6em 1fr; }
  .win img { width: 6em; height: 6em; }
  #cardBody { padding: .6em 1em .3em; }
  #cardFoot { padding: .35em 1em .55em; }
  .btn { padding: .45em 1.3em; }
  .lv .cl { display: none; }
  .lv { padding-top: .3em; padding-bottom: .3em; }
  .menu .intro { font-size: .8em; }
  .keys .ph { 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: 5.5em; height: 5.5em; }
  .win { grid-template-columns: 5.5em 1fr; }
  .win img { width: 5.5em; height: 5.5em; }
  .week { gap: .2em; }
  .levels { grid-template-columns: 1fr; }
}
@media (min-aspect-ratio: 1/1) { .keys .ph { display: none; } }

/* ---------- coach marks ---------- */
#coach { position: fixed; inset: 0; z-index: 60; pointer-events: none; }
#coachCard {
  pointer-events: auto; position: absolute; left: 50%; transform: translateX(-50%); bottom: 3vh; width: min(26em, 92vw);
  background: var(--paper); border-radius: .5em; border-top: .35em solid var(--mustard); padding: .8em 1em; box-shadow: 0 14px 40px rgba(0,0,0,.45); font-size: clamp(12px, min(2.3vh, 1.9vw) + 4px, 18px);
}
#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(--mustard), 0 0 0 9999px rgba(40,34,20,.6) !important; border-radius: .5em; }
#fx { position: fixed; inset: 0; pointer-events: none; z-index: 70; width: 100%; height: 100%; }
