:root {
  --light: #f6e6c4;
  --dark: #9a5b34;
  --frame: #6a3a1f;
  --frame2: #8d5330;
  --ink: #2f2a3a;
  --cream: #fffaf0;
  --accent: #e0562f;
  --lemon: #ffd23f;
  --sea: #1f7a8c;
  --n: 8;
  --bs: min(calc(100vh - 176px), calc(100vw - 440px), 880px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: "Trebuchet MS", "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  color: var(--ink);
  background: #3d8ea0 url(img/bg.webp) center / cover no-repeat fixed;
  overflow-x: hidden;
  user-select: none; -webkit-user-select: none;
}
body::before {
  content: ""; position: fixed; inset: 0; pointer-events: none;
  background: radial-gradient(ellipse at 50% 55%, rgba(20, 40, 60, .12), rgba(20, 30, 50, .55));
}
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }

#app { position: relative; min-height: 100%; display: flex; flex-direction: column; align-items: center; padding: 8px 16px 16px; }
#top { width: 100%; max-width: calc(var(--bs) + 340px); display: flex; align-items: center; justify-content: space-between; padding: 2px 2px 8px; }
#top h1 { margin: 0; font-size: 26px; letter-spacing: .5px; color: #fff; text-shadow: 0 2px 0 var(--ink), 0 3px 8px rgba(0,0,0,.4); }
#top h1 span { color: var(--lemon); }
.snd { display: flex; gap: 8px; }
.snd button { width: 42px; height: 42px; border-radius: 50%; border: 2px solid var(--ink); background: var(--cream); display: grid; place-items: center; padding: 0; box-shadow: 0 3px 0 var(--ink); font-weight: 900; font-size: 20px; }
.snd button:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.snd svg { width: 22px; height: 22px; }

#main { display: flex; gap: 22px; align-items: flex-start; justify-content: center; }
#boardCol { width: var(--bs); flex: none; }

.pstrip { display: flex; align-items: center; gap: 8px; height: 34px; padding: 0 10px; margin: 4px 0; border-radius: 12px; background: rgba(255, 250, 240, .92); border: 2px solid rgba(47, 42, 58, .5); font-weight: 700; font-size: 15px; transition: box-shadow .2s, background .2s; }
.pstrip.turn { background: #fff5c8; box-shadow: 0 0 0 3px var(--lemon); }
.pstrip .dot { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--ink); flex: none; }
.pstrip .dot.w { background: #fffaf0; } .pstrip .dot.b { background: #2b2630; }
.pname { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.caps { margin-left: auto; display: flex; align-items: center; gap: 6px; font-size: 13px; white-space: nowrap; color: #5d5468; }
.caps i { display: inline-block; width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--ink); vertical-align: -2px; margin-right: 3px; }
.caps i.w { background: #fffaf0; } .caps i.b { background: #2b2630; }

#boardWrap { position: relative; width: var(--bs); height: var(--bs); padding: calc(var(--bs) * .028); border-radius: 14px;
  background: linear-gradient(145deg, var(--frame2), var(--frame)); box-shadow: 0 10px 30px rgba(0,0,0,.45), inset 0 2px 0 rgba(255,255,255,.15), inset 0 -3px 0 rgba(0,0,0,.25); }
#board { position: relative; width: 100%; height: 100%; display: grid; grid-template-columns: repeat(var(--n), 1fr); grid-template-rows: repeat(var(--n), 1fr); border-radius: 4px; overflow: hidden; touch-action: none; box-shadow: 0 0 0 2px #3b2414; cursor: pointer; }
.sq { position: relative; }
.sq.l { background: var(--light); }
.sq.d { background: var(--dark); background-image: linear-gradient(135deg, rgba(255,255,255,.06), rgba(0,0,0,.06)); }
.sq.last::after { content: ""; position: absolute; inset: 0; background: rgba(255, 210, 63, .42); }
.sq.sel::after { content: ""; position: absolute; inset: 0; background: rgba(255, 170, 40, .7); }
.sq.must::after { content: ""; position: absolute; inset: 0; box-shadow: inset 0 0 0 calc(var(--bs) * .008) #ffd23f; animation: blink .8s ease-in-out infinite alternate; }
@keyframes blink { from { opacity: .35; } }
.sq.mv::before { content: ""; position: absolute; left: 33%; top: 33%; width: 34%; height: 34%; border-radius: 50%; background: rgba(255, 236, 160, .75); box-shadow: 0 0 0 2px rgba(60, 30, 10, .45); z-index: 3; }
.sq.dest::before { content: ""; position: absolute; left: 22%; top: 22%; width: 56%; height: 56%; border-radius: 50%; background: rgba(120, 230, 140, .55); box-shadow: 0 0 0 3px rgba(20, 110, 50, .8); z-index: 3; }
.sq.victim::after { content: ""; position: absolute; inset: 6%; border-radius: 50%; box-shadow: 0 0 0 calc(var(--bs) * .007) #ff4d3a; z-index: 6; }
.sq.kb::before { content: ""; position: absolute; inset: 0; box-shadow: inset 0 0 0 calc(var(--bs) * .007) #34c3ff; z-index: 7; }
.coord { position: absolute; font-size: calc(var(--bs) * .022); font-weight: 800; pointer-events: none; z-index: 2; }
.coord.f { right: 5%; bottom: 2%; } .coord.r { left: 5%; top: 3%; }
.sq.l .coord { color: #b07a4c; } .sq.d .coord { color: #f6e3c2; }

#pieces { position: absolute; inset: 0; pointer-events: none; z-index: 4; }
.piece { position: absolute; left: 0; top: 0; width: calc(100% / var(--n)); height: calc(100% / var(--n)); transition: transform .18s cubic-bezier(.3, .8, .3, 1); will-change: transform; }
.piece svg { width: 100%; height: 100%; display: block; overflow: visible; }
.piece.lift svg { transform: translateY(-6%) scale(1.08); filter: drop-shadow(0 6px 4px rgba(0,0,0,.35)); }
.piece.gone { animation: gone .3s ease-in forwards; }
@keyframes gone { to { opacity: 0; transform: var(--t) scale(.3); } }
.piece.pop svg { animation: pop .45s ease-out; }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.25); } 100% { transform: scale(1); } }

.ov { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(20, 30, 45, .45); border-radius: 14px; z-index: 20; animation: fade .2s; }
@keyframes fade { from { opacity: 0; } }
.card { background: var(--cream); border: 3px solid var(--ink); border-radius: 18px; padding: 16px 20px; text-align: center; box-shadow: 0 6px 0 var(--ink), 0 12px 30px rgba(0,0,0,.4); max-width: 92%; }
.card h2 { margin: 2px 0 8px; font-size: 24px; }
.card p { margin: 4px 0 8px; font-size: 15px; }
#endGino { width: 110px; height: auto; margin-top: -6px; }
#endReason { font-weight: 700; color: #8d5330; }
#endStats { font-size: 13px; color: #5d5468; }
.row { display: flex; gap: 10px; justify-content: center; margin-top: 10px; flex-wrap: wrap; }
.btn { border: 3px solid var(--ink); border-radius: 14px; background: var(--accent); color: #fff; font-weight: 800; font-size: 17px; padding: 10px 18px; box-shadow: 0 4px 0 var(--ink); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn.ghost { background: #fff; color: var(--ink); }

#side { width: 300px; flex: none; display: flex; flex-direction: column; gap: 12px; }
#ginoBox { position: relative; display: flex; align-items: flex-end; gap: 6px; min-height: 130px; }
#gino { width: 112px; height: auto; flex: none; filter: drop-shadow(0 4px 6px rgba(0,0,0,.35)); transform-origin: 50% 100%; }
#gino.bounce { animation: bounce .45s; }
@keyframes bounce { 30% { transform: scale(1.08, .92); } 60% { transform: scale(.96, 1.05); } }
#bubble { position: relative; flex: 1; align-self: center; background: var(--cream); border: 3px solid var(--ink); border-radius: 16px; padding: 10px 12px; font-weight: 700; font-size: 15px; line-height: 1.3; box-shadow: 0 4px 0 var(--ink); }
#bubble::before { content: ""; position: absolute; left: -12px; top: 50%; margin-top: -8px; border: 8px solid transparent; border-right: 10px solid var(--ink); border-left: 0; }
#bubble.pulse { animation: pulse .35s; }
@keyframes pulse { 50% { transform: scale(1.04); } }

#opts { display: flex; flex-direction: column; gap: 6px; }
.seg { display: flex; background: #1d5f6e; border: 2px solid #123e48; border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; border-radius: 9px; background: transparent; color: #e6f6f8; font-weight: 700; font-size: 14px; padding: 7px 4px; white-space: nowrap; }
.seg button.on { background: var(--cream); color: var(--ink); box-shadow: 0 2px 0 rgba(0,0,0,.3); }
.seg.off button { color: #9cc3ca; } .seg.off button.on { background: #5b8f99; color: #e8f3f5; box-shadow: none; }

#tools { display: flex; gap: 8px; }
.tool { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; border: 3px solid var(--ink); border-radius: 14px; background: var(--cream); padding: 6px 4px; font-weight: 800; font-size: 13px; box-shadow: 0 4px 0 var(--ink); }
.tool:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.tool svg { width: 22px; height: 22px; }
.tool.primary { background: var(--accent); color: #fff; }
.tool:disabled { opacity: .45; }

#movesBox { background: rgba(255, 250, 240, .94); border: 2px solid rgba(47, 42, 58, .5); border-radius: 14px; padding: 8px 10px; min-height: 80px; }
#movesBox h3 { margin: 0 0 4px; font-size: 14px; text-transform: uppercase; letter-spacing: 1px; color: #8d5330; }
#moves { list-style: none; margin: 0; padding: 0; max-height: calc(var(--bs) - 360px); min-height: 40px; overflow-y: auto; font-size: 14px; display: grid; grid-template-columns: 30px 1fr 1fr; row-gap: 2px; font-variant-numeric: tabular-nums; }
#moves li { display: contents; }
#moves .n { color: #8a7f95; }
#moves .m { font-weight: 700; padding: 1px 4px; border-radius: 5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#moves .m.cur { background: #ffe98a; }

/* ---------- start panel + rules ---------- */
.ov2 { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; padding: 12px; background: rgba(16, 40, 56, .55); animation: fade .25s; }
.scard { display: flex; align-items: center; gap: 18px; max-width: 640px; width: 100%; max-height: 100%; background: var(--cream); border: 3px solid var(--ink); border-radius: 22px; padding: 18px 22px; box-shadow: 0 6px 0 var(--ink), 0 16px 40px rgba(0,0,0,.45); cursor: pointer; }
#startGino { width: 170px; height: auto; flex: none; filter: drop-shadow(0 4px 6px rgba(0,0,0,.25)); }
.sbody { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 9px; }
.sbody h2 { margin: 0; font-size: 34px; color: var(--accent); text-shadow: 0 2px 0 rgba(47,42,58,.2); }
.intro { margin: 0; font-size: 15px; line-height: 1.35; }
.keys { display: flex; flex-direction: column; gap: 4px; font-size: 14px; font-weight: 700; }
.k { display: inline-block; min-width: 24px; padding: 1px 6px; margin: 0 1px; border: 2px solid var(--ink); border-bottom-width: 4px; border-radius: 7px; background: #fff; text-align: center; font-size: 13px; }
.btn.big { font-size: 24px; padding: 12px 18px; background: var(--accent); }
.rcard { display: flex; flex-direction: column; max-width: 620px; width: 100%; max-height: 92vh; background: var(--cream); border: 3px solid var(--ink); border-radius: 20px; box-shadow: 0 6px 0 var(--ink), 0 16px 40px rgba(0,0,0,.45); overflow: hidden; }
.rcard h2 { margin: 0; padding: 14px 18px 6px; font-size: 24px; color: var(--accent); }
.rtext { overflow-y: auto; padding: 0 18px; font-size: 15px; line-height: 1.4; flex: 1 1 auto; min-height: 0; user-select: text; }
.rtext h3 { margin: 10px 0 4px; font-size: 16px; color: var(--sea); }
.rtext ul { margin: 0; padding-left: 20px; } .rtext li { margin: 2px 0; }
.rfoot { padding: 10px 18px 14px; text-align: center; border-top: 2px solid rgba(47,42,58,.15); }

#fx { position: fixed; inset: 0; pointer-events: none; z-index: 50; }

/* ---------- phone / narrow ---------- */
@media (max-aspect-ratio: 1/1), (max-width: 819px) and (max-aspect-ratio: 4/3) {
  :root { --bs: min(calc(100vw - 16px), calc(100dvh - 300px), 640px); }
  #app { padding: 4px 8px 12px; }
  #top { max-width: var(--bs); padding-bottom: 4px; }
  #top h1 { font-size: 22px; }
  .snd button { width: 38px; height: 38px; }
  #main { flex-direction: column; align-items: center; gap: 6px; }
  #side { display: contents; }
  #ginoBox { order: 0; width: var(--bs); min-height: 0; height: 60px; align-items: center; }
  #gino { width: 54px; }
  #bubble { font-size: 14px; padding: 6px 10px; border-width: 2px; box-shadow: 0 3px 0 var(--ink); }
  #boardCol { order: 1; }
  .pstrip { height: 30px; margin: 3px 0; font-size: 14px; }
  #tools { order: 2; width: var(--bs); }
  .tool { flex-direction: row; justify-content: center; gap: 6px; padding: 8px 4px; }
  .tool svg { width: 18px; height: 18px; }
  #opts { order: 3; width: var(--bs); }
  .seg button { padding: 7px 4px; font-size: 13px; }
  #movesBox { order: 4; width: var(--bs); min-height: 0; }
  #moves { max-height: 100px; }
  .scard { flex-direction: column; gap: 6px; padding: 14px 16px; text-align: center; }
  #startGino { width: 110px; }
  .sbody h2 { font-size: 28px; }
  .keys { align-items: center; }
}
@media (max-aspect-ratio: 4/3) and (max-width: 819px) and (max-height: 700px), (max-aspect-ratio: 1/1) and (max-height: 700px) {
  #ginoBox { height: 50px; } #gino { width: 44px; }
  #bubble { font-size: 13px; }
  #startGino { width: 70px; }
}

/* ---------- phone portrait: keep the whole column on screen ---------- */
@media (max-aspect-ratio: 1/1) {
  :root { --bs: min(calc(100vw - 16px), calc(100dvh - 400px), 640px); }
  #app { padding-bottom: 6px; }
  .seg button { padding: 6px 2px; }
  #opts { gap: 4px; }
  #movesBox { padding: 3px 8px; }
  #movesBox h3 { display: none; }
  #moves { max-height: 40px; min-height: 20px; }
}

/* ---------- small embedded window (landscape, short height) ---------- */
@media (min-aspect-ratio: 1/1) and (max-height: 560px) {
  :root { --side: clamp(230px, 34vw, 310px); --bs: min(calc(100vh - 76px), calc(100vw - var(--side) - 44px), 660px); }
  html, body { overflow: hidden; }
  #app { min-height: 0; height: 100%; padding: 6px 10px; display: grid; justify-content: center; align-content: center;
    grid-template-columns: var(--bs) var(--side); grid-template-rows: auto minmax(0, 1fr); column-gap: 16px; align-items: start;
    grid-template-areas: "board top" "board side"; }
  #main { display: contents; }
  #boardCol { grid-area: board; align-self: center; }
  #top { grid-area: top; max-width: none; padding: 0 0 6px; }
  #top h1 { font-size: 20px; }
  .snd { gap: 6px; }
  .snd button { width: 32px; height: 32px; box-shadow: 0 2px 0 var(--ink); font-size: 16px; }
  .snd svg { width: 18px; height: 18px; }
  .pstrip { height: 26px; margin: 3px 0; font-size: 13px; border-radius: 10px; }
  #side { grid-area: side; width: auto; min-height: 0; height: 100%; gap: 6px; }
  #ginoBox { min-height: 0; height: 58px; align-items: center; }
  #gino { width: 50px; }
  #bubble { font-size: 13px; padding: 5px 9px; border-width: 2px; box-shadow: 0 3px 0 var(--ink); }
  #opts { gap: 4px; }
  .seg button { font-size: 12px; padding: 4px 3px; }
  .tool { flex-direction: row; justify-content: center; gap: 5px; padding: 5px 3px; font-size: 12px; box-shadow: 0 3px 0 var(--ink); }
  .tool svg { width: 16px; height: 16px; }
  #movesBox { flex: 1 1 0; min-height: 0; display: flex; flex-direction: column; padding: 4px 8px; }
  #movesBox h3 { font-size: 11px; margin-bottom: 2px; }
  #moves { flex: 1 1 0; max-height: none; min-height: 0; font-size: 12px; align-content: start; }
  .card { max-height: 94%; overflow-y: auto; padding: 10px 14px; border-radius: 14px; }
  .card h2 { font-size: 19px; margin: 0 0 4px; }
  .card p { font-size: 13px; margin: 2px 0 4px; }
  #endGino { width: 60px; margin-top: 0; }
  .row { margin-top: 6px; }
  .btn { font-size: 15px; padding: 7px 14px; }
  .scard { padding: 12px 16px; gap: 14px; }
  #startGino { width: 120px; }
  .sbody { gap: 6px; }
  .sbody h2 { font-size: 26px; }
  .intro { font-size: 13px; }
  .keys { font-size: 12px; }
  .btn.big { font-size: 20px; padding: 8px 14px; }
  .rtext { font-size: 13px; }
  .rcard h2 { font-size: 20px; padding: 10px 14px 4px; }
  .rfoot { padding: 6px 14px 8px; }
}
@media (min-aspect-ratio: 1/1) and (max-height: 420px) {
  :root { --side: clamp(210px, 32vw, 270px); --bs: min(calc(100vh - 64px), calc(100vw - var(--side) - 40px), 660px); }
  #app { padding: 4px 10px; }
  .pstrip { height: 22px; margin: 2px 0; font-size: 12px; }
  #ginoBox { height: auto; } #gino { display: none; }
  #bubble { font-size: 12px; line-height: 1.25; padding: 4px 8px; box-shadow: 0 2px 0 var(--ink); }
  #bubble::before { display: none; }
  .tool { font-size: 11px; padding: 3px 2px; } .tool svg { width: 14px; height: 14px; }
  #movesBox { padding: 3px 6px; min-height: 30px; } #movesBox h3 { display: none; }
  #top { padding-bottom: 4px; } #top h1 { font-size: 17px; }
  .snd button { width: 28px; height: 28px; font-size: 14px; }
  .seg { padding: 2px; } .seg button { font-size: 11px; padding: 3px 2px; }
  #side { gap: 5px; }
  #endGino { width: 44px; }
  .btn { font-size: 14px; padding: 6px 12px; }
  .scard { padding: 10px 14px; }
  #startGino { width: 90px; }
  .sbody h2 { font-size: 22px; }
  .intro { font-size: 12px; }
  .sbody { gap: 5px; }
  .btn.big { font-size: 18px; padding: 6px 12px; }
}
@media (max-aspect-ratio: 1/1) and (max-height: 860px) {
  :root { --bs: min(calc(100vw - 16px), calc(100dvh - 350px), 640px); }
  #movesBox { display: none; }
}
/* ---------- big screens ---------- */
@media (min-aspect-ratio: 1/1) and (min-height: 900px) and (min-width: 1300px) {
  #top h1 { font-size: 32px; }
  #side { width: 360px; }
  #gino { width: 140px; }
  #bubble { font-size: 18px; }
  .seg button { font-size: 16px; padding: 9px 4px; }
  .tool { font-size: 15px; } .tool svg { width: 26px; height: 26px; }
  .pstrip { height: 40px; font-size: 17px; }
  #moves { font-size: 16px; }
  .snd button { width: 48px; height: 48px; }
}
