:root {
  --bs: 360px;
  --paper: #fdf7e8;
  --ink: #3f2614;
  --user: #2b5d8c;
  --line: #5b3a20;
  --thin: #dcc7a2;
  --peer: #f3e5c4;
  --same: #f5cf86;
  --sel: #ffd27a;
  --wrong: #d0433f;
  --sage: #5d8a4a;
  --wood: #7a4a26;
  --cream: #fff8ea;
  --shadow: 0 6px 0 rgba(70, 38, 14, .35), 0 14px 30px rgba(40, 20, 5, .28);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: #b9772f url(img/bg.webp) center / cover no-repeat;
  color: var(--ink);
  font-family: "Trebuchet MS", "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; touch-action: manipulation; }
.hidden { display: none !important; }

#app {
  height: 100%; width: 100%;
  display: grid; justify-content: center; align-content: center;
  padding: max(6px, env(safe-area-inset-top)) 8px max(6px, env(safe-area-inset-bottom));
  gap: 6px;
  grid-template-columns: var(--bs);
  grid-template-areas: "top" "owl" "stats" "board" "tools" "pad" "new";
}
#top { grid-area: top; display: flex; flex-direction: column; align-items: center; gap: 5px; }
h1 {
  margin: 0; font-size: 22px; letter-spacing: .5px; color: var(--cream); line-height: 1.1;
  text-shadow: 0 2px 0 #5b3416, 0 0 2px #5b3416;
}
h1 span { color: #ffd27a; }
#diffs { display: flex; background: rgba(60, 33, 13, .55); border-radius: 999px; padding: 3px; gap: 2px; }
#diffs button {
  padding: 6px 10px; border-radius: 999px; font-size: 13.5px; font-weight: 700; color: #f7e6c6;
  transition: background .2s, color .2s, transform .1s;
}
#diffs button.on { background: var(--cream); color: var(--wood); box-shadow: 0 2px 0 rgba(0,0,0,.25); }
#diffs button:active { transform: scale(.95); }

#owlBox { grid-area: owl; display: flex; align-items: center; gap: 8px; min-height: 64px; }
#owl { width: 66px; height: 66px; object-fit: contain; flex: none; filter: drop-shadow(0 3px 2px rgba(50,25,5,.35)); transform-origin: 50% 90%; }
#owl.bounce { animation: owlBounce .6s ease; }
#owl.shake { animation: owlShake .45s ease; }
@keyframes owlBounce { 0%{transform:translateY(0) scale(1)} 30%{transform:translateY(-10px) scale(1.06,.96)} 60%{transform:translateY(0) scale(.96,1.05)} 100%{transform:none} }
@keyframes owlShake { 0%,100%{transform:rotate(0)} 25%{transform:rotate(-8deg)} 75%{transform:rotate(8deg)} }
#bubble {
  position: relative; flex: 1; background: var(--cream); border: 2.5px solid var(--line); border-radius: 14px;
  padding: 7px 11px; font-size: 14px; font-weight: 600; line-height: 1.25; box-shadow: 0 3px 0 rgba(70,38,14,.3);
  min-height: 40px; display: flex; align-items: center;
}
#bubble::before {
  content: ""; position: absolute; left: -10px; top: 50%; margin-top: -8px; border: 8px solid transparent;
  border-right: 10px solid var(--line); border-left: 0;
}
#bubble::after {
  content: ""; position: absolute; left: -6px; top: 50%; margin-top: -6px; border: 6px solid transparent;
  border-right: 7px solid var(--cream); border-left: 0;
}
#bubble.pop { animation: bubblePop .3s ease; }
@keyframes bubblePop { 0%{transform:scale(.92)} 60%{transform:scale(1.03)} 100%{transform:scale(1)} }

#stats {
  grid-area: stats; display: flex; align-items: center; justify-content: space-between; gap: 6px;
  background: rgba(60, 33, 13, .55); color: #fbeacb; border-radius: 12px; padding: 4px 6px 4px 12px; font-size: 14.5px;
}
#stats b { color: #fff; font-variant-numeric: tabular-nums; }
#stats .lbl { opacity: .85; }
#stats .ico { width: 16px; height: 16px; vertical-align: -3px; }
#diffLabel { font-weight: 700; color: #ffd27a; }
#mistakes.bad { color: #ff9b8f; }
.snd { display: flex; gap: 6px; }
#mute, #music {
  width: 36px; height: 30px; border-radius: 9px; background: rgba(255,248,234,.16);
  display: grid; place-items: center;
}
#mute svg, #music svg { width: 22px; height: 22px; }

#boardWrap { grid-area: board; position: relative; width: var(--bs); height: var(--bs); }
#board {
  width: 100%; height: 100%; background: var(--paper); border: 3px solid var(--line); border-radius: 10px;
  display: grid; grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(9, 1fr);
  box-shadow: var(--shadow); overflow: hidden; touch-action: none;
}
.cell {
  position: relative; display: grid; place-items: center;
  border-right: 1px solid var(--thin); border-bottom: 1px solid var(--thin);
  font-size: calc(var(--bs) / 9 * .6); font-weight: 700; line-height: 1; cursor: pointer;
  color: var(--user); transition: background-color .12s;
  font-variant-numeric: tabular-nums;
}
.cell.c2, .cell.c5 { border-right: 2.5px solid var(--line); }
.cell.c8 { border-right: 0; }
.cell.r2, .cell.r5 { border-bottom: 2.5px solid var(--line); }
.cell.r8 { border-bottom: 0; }
.cell.given { color: var(--ink); }
.cell.peer { background: var(--peer); }
.cell.same { background: var(--same); }
.cell.sel { background: var(--sel); box-shadow: inset 0 0 0 2.5px #c9781e; }
.cell.wrong { color: var(--wrong); }
.cell.wrong.sel, .cell.wrong.same { background: #f6c2b5; }
.cell .v { display: block; }
.cell.pop .v { animation: pop .28s ease; }
.cell.err .v { animation: err .4s ease; }
.cell.wave::after {
  content: ""; position: absolute; inset: 0; background: #ffe39a; opacity: 0; pointer-events: none;
  animation: wave .7s ease forwards; animation-delay: var(--dl, 0s);
}
.cell.wave .v { animation: wavev .7s ease; animation-delay: var(--dl, 0s); }
@keyframes pop { 0%{transform:scale(.4);opacity:.3} 60%{transform:scale(1.18)} 100%{transform:scale(1)} }
@keyframes err { 0%,100%{transform:translateX(0)} 20%{transform:translateX(-4px)} 40%{transform:translateX(4px)} 60%{transform:translateX(-3px)} 80%{transform:translateX(3px)} }
@keyframes wave { 0%{opacity:0} 35%{opacity:.85} 100%{opacity:0} }
@keyframes wavev { 0%,100%{transform:scale(1)} 40%{transform:scale(1.22)} }
.notes {
  position: absolute; inset: 1px; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  font-size: calc(var(--bs) / 9 * .25); font-weight: 700; color: #8a6a49; text-align: center; align-items: center;
}
.notes span.hl { color: #fff; background: #d9902f; border-radius: 4px; }

#end {
  position: absolute; inset: 0; display: grid; place-items: center; background: rgba(60, 33, 13, .45);
  border-radius: 10px; z-index: 5; animation: fadeIn .35s ease;
}
@keyframes fadeIn { from{opacity:0} to{opacity:1} }
#end .card {
  width: min(92%, 340px); background: var(--cream); border: 3px solid var(--line); border-radius: 18px;
  padding: 12px 16px 16px; text-align: center; box-shadow: var(--shadow); animation: cardIn .45s cubic-bezier(.2,1.4,.4,1);
}
@keyframes cardIn { from{transform:scale(.7) translateY(20px);opacity:0} to{transform:none;opacity:1} }
#endOwl { width: 96px; height: 96px; object-fit: contain; margin-top: -54px; filter: drop-shadow(0 4px 3px rgba(50,25,5,.35)); }
#end h2 { margin: 2px 0 2px; font-size: 26px; color: var(--wood); }
#end p { margin: 0 0 8px; font-size: 14px; }
#endStats { display: flex; justify-content: center; gap: 8px; margin-bottom: 10px; }
#endStats div { background: #f3e5c4; border-radius: 10px; padding: 6px 8px; min-width: 76px; }
#endStats small { display: block; font-size: 11.5px; opacity: .8; }
#endStats b { font-size: 18px; font-variant-numeric: tabular-nums; }
#endStats .rec { background: #ffe39a; }
#endDiffs { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px; margin-bottom: 10px; }
#endDiffs button { padding: 5px 9px; border-radius: 999px; font-size: 12.5px; font-weight: 700; background: #f3e5c4; }
#endDiffs button.on { background: var(--wood); color: var(--cream); }
.row { display: flex; gap: 8px; justify-content: center; }
.btn {
  background: var(--sage); color: #fff; font-weight: 800; font-size: 18px; padding: 11px 22px; border-radius: 14px;
  box-shadow: 0 4px 0 #3c5e2f; transition: transform .08s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #3c5e2f; }
.btn.ghost { background: #f3e5c4; color: var(--wood); box-shadow: 0 4px 0 #cdb588; }
.btn.small { font-size: 15px; padding: 8px 16px; }

#tools { grid-area: tools; display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.tool {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 1px;
  color: var(--cream); font-size: 12.5px; font-weight: 700; padding: 5px 0 4px; border-radius: 12px;
  background: rgba(60, 33, 13, .55); transition: transform .08s, background .2s;
}
.tool svg { width: 25px; height: 25px; }
.tool:active { transform: scale(.94); }
.tool i {
  position: absolute; top: 2px; right: calc(50% - 30px); font-style: normal; font-size: 10px; font-weight: 800;
  background: #8a6a49; color: #fff; border-radius: 999px; padding: 1px 5px;
}
#notes.on { background: var(--sage); }
#notes.on i { background: #ffd27a; color: var(--ink); }
#hint i { background: #d9902f; }
.tool:disabled { opacity: .45; }

#pad { grid-area: pad; display: grid; grid-template-columns: repeat(9, minmax(0, 1fr)); gap: 4px; min-width: 0; }
.num { min-width: 0; padding: 0; }
@media (max-width: 340px) {
  #pad { gap: 3px; }
  .num { font-size: 22px; }
  .num small { font-size: 9px; }
}
.num {
  position: relative; background: var(--cream); border: 2px solid var(--line); border-radius: 10px;
  font-size: 26px; font-weight: 800; color: var(--user); height: 54px; display: flex; flex-direction: column;
  align-items: center; justify-content: center; line-height: 1; box-shadow: 0 3px 0 rgba(70,38,14,.45);
  transition: transform .08s, opacity .2s;
}
.num small { font-size: 10.5px; color: #9a7b58; font-weight: 700; margin-top: 2px; }
.num:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(70,38,14,.45); }
.num.done { opacity: .3; }
#pad.notesMode .num { color: #8a6a49; background: #f4ecd9; }
#newGame { grid-area: new; justify-self: center; }

#fx { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 20; }

/* Wide screens: board left, panel right */
@media (min-aspect-ratio: 1/1) and (min-width: 700px) {
  #app {
    grid-template-columns: var(--bs) 300px; column-gap: 22px; row-gap: 10px;
    grid-template-rows: auto auto minmax(150px, auto) auto auto 1fr;
    grid-template-areas: "top top" "stats owl" "board owl" "board tools" "board pad" "board new";
  }
  #top { flex-direction: row; justify-content: space-between; }
  h1 { font-size: 30px; }
  #diffs button { font-size: 15px; padding: 7px 14px; }
  #owlBox { flex-direction: column; align-items: center; gap: 6px; align-self: start; }
  #owl { width: 120px; height: 120px; order: 2; }
  #bubble { width: 100%; order: 1; font-size: 15px; min-height: 56px; justify-content: center; text-align: center; }
  #bubble::before { left: 50%; top: auto; bottom: -11px; margin: 0 0 0 -9px; border: 9px solid transparent; border-top: 11px solid var(--line); border-bottom: 0; }
  #bubble::after { left: 50%; top: auto; bottom: -6px; margin: 0 0 0 -7px; border: 7px solid transparent; border-top: 8px solid var(--cream); border-bottom: 0; }
  #pad { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .num { height: 66px; font-size: 32px; }
  .num small { font-size: 12px; }
  .tool { font-size: 13px; padding: 8px 0 6px; }
  #newGame { align-self: start; }
}
