:root {
  --light: #f3dfba;
  --dark: #c48a5a;
  --frame: #5a3620;
  --frame2: #7a4a2b;
  --ink: #3b2414;
  --cream: #fff6e6;
  --accent: #d9622b;
  --accent2: #2f7d5b;
  --bs: min(calc(100vh - 176px), calc(100vw - 420px), 660px);
}
* { 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: #6b3f22 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(60, 30, 12, .25), rgba(40, 20, 8, .7));
}
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: #fff3dc; text-shadow: 0 2px 0 #3b2414, 0 3px 8px rgba(0,0,0,.4); }
#top h1 span { color: #ffc861; }
.snd { display: flex; gap: 8px; }
.snd button { width: 42px; height: 42px; border-radius: 50%; border: 2px solid #3b2414; background: var(--cream); display: grid; place-items: center; padding: 0; box-shadow: 0 3px 0 #3b2414; }
.snd button:active { transform: translateY(2px); box-shadow: 0 1px 0 #3b2414; }
.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, 246, 230, .9); border: 2px solid rgba(59, 36, 20, .55); font-weight: 700; font-size: 15px; transition: box-shadow .2s, background .2s; }
.pstrip.turn { background: #fff2cc; box-shadow: 0 0 0 3px #ffc861; }
.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: #3a2418; }
.pname { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.caps { margin-left: auto; display: flex; align-items: center; min-width: 0; overflow: hidden; }
.caps .pc { width: 20px; height: 20px; margin-right: -7px; flex: none; }
.caps .adv { margin-left: 10px; font-size: 13px; color: #6b4a33; }

#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(8, 1fr); grid-template-rows: repeat(8, 1fr); border-radius: 4px; overflow: hidden; touch-action: none; box-shadow: 0 0 0 2px #3b2414; }
.sq { position: relative; }
.sq.l { background: var(--light); } .sq.d { background: var(--dark); }
.sq.last::after { content: ""; position: absolute; inset: 0; background: rgba(255, 205, 70, .5); }
.sq.sel::after { content: ""; position: absolute; inset: 0; background: rgba(255, 160, 40, .62); }
.sq.hov::before { content: ""; position: absolute; inset: 0; box-shadow: inset 0 0 0 4px rgba(255, 250, 235, .85); z-index: 1; }
.sq.mv::before { content: ""; position: absolute; left: 35%; top: 35%; width: 30%; height: 30%; border-radius: 50%; background: rgba(70, 38, 18, .32); z-index: 3; }
.sq.cap::before { content: ""; position: absolute; inset: 4%; border-radius: 50%; box-shadow: inset 0 0 0 calc(var(--bs) * .009) rgba(70, 38, 18, .4); z-index: 3; }
.sq.chk { background-image: radial-gradient(circle, rgba(235, 50, 30, .95) 0%, rgba(235, 60, 30, .55) 40%, rgba(235, 60, 30, 0) 72%); }
.coord { position: absolute; font-size: calc(var(--bs) * .024); 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: 12.5%; height: 12.5%; transition: transform .2s cubic-bezier(.3, .8, .3, 1); will-change: transform; }
.piece .pc { width: 100%; height: 100%; display: block; filter: drop-shadow(0 calc(var(--bs) * .005) 0 rgba(40, 20, 8, .35)); }
.piece.drag { transition: none; z-index: 10; }
.piece.drag .pc { transform: scale(1.18); filter: drop-shadow(0 calc(var(--bs) * .014) calc(var(--bs) * .01) rgba(40, 20, 8, .45)); }
.piece.lift .pc { transform: translateY(-4%) scale(1.06); }
.piece.gone { animation: gone .26s ease-in forwards; }
@keyframes gone { to { opacity: 0; transform: var(--t) scale(.4); } }
.piece.pop .pc { animation: pop .3s ease-out; }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.18); } 100% { transform: scale(1); } }

.pc { overflow: visible; }
symbol .o { fill: var(--pf); stroke: var(--ps); stroke-width: 3.6; stroke-linejoin: round; }
symbol .d { fill: none; stroke: var(--pd); stroke-width: 3; stroke-linecap: round; }
symbol .f { fill: var(--pd); }
symbol .hl { fill: none; stroke: var(--ph); stroke-width: 3; stroke-linecap: round; }
.pw { --pf: url(#gW); --ps: #3b2414; --pd: #3b2414; --ph: rgba(255, 255, 255, .95); }
.pb { --pf: url(#gB); --ps: #170c06; --pd: #f3dcb4; --ph: rgba(255, 220, 180, .35); }

#boardWrap > #promo, #boardWrap > #end, #boardWrap > #confirm { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(40, 20, 8, .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; }
#endCapi { width: 110px; height: auto; margin-top: -6px; }
#endReason { font-weight: 700; color: #7a4a2b; }
#endStats { font-size: 13px; color: #6b4a33; }
.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); }
.btn.small { font-size: 14px; padding: 6px 14px; margin-top: 10px; }
#promoBtns { display: flex; gap: 8px; justify-content: center; }
#promoBtns button { width: calc(var(--bs) * .14); height: calc(var(--bs) * .14); min-width: 56px; min-height: 56px; border: 3px solid var(--ink); border-radius: 14px; background: var(--light); padding: 4px; box-shadow: 0 3px 0 var(--ink); }
#promoBtns button:hover { background: #ffd98a; }
#promoBtns .pc { width: 100%; height: 100%; }

#side { width: 300px; flex: none; display: flex; flex-direction: column; gap: 12px; }
#capiBox { position: relative; display: flex; align-items: flex-end; gap: 6px; min-height: 140px; }
#capi { width: 116px; height: auto; flex: none; filter: drop-shadow(0 4px 6px rgba(0,0,0,.35)); transform-origin: 50% 100%; }
#capi.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: #4a2c19; border: 2px solid #2e1a0e; border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; border-radius: 9px; background: transparent; color: #f6e3c2; font-weight: 700; font-size: 14px; padding: 7px 4px; }
.seg button.on { background: var(--cream); color: var(--ink); box-shadow: 0 2px 0 rgba(0,0,0,.3); }
.seg.off button { color: #b89a80; } .seg.off button.on { background: #8a6a52; color: #f3e3cc; 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, 246, 230, .94); border: 2px solid rgba(59, 36, 20, .55); 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: #7a4a2b; }
#moves { list-style: none; margin: 0; padding: 0; max-height: calc(var(--bs) - 330px); min-height: 40px; overflow-y: auto; font-size: 15px; display: grid; grid-template-columns: 34px 1fr 1fr; row-gap: 2px; font-variant-numeric: tabular-nums; }
#moves li { display: contents; }
#moves .n { color: #9b7354; }
#moves .m { font-weight: 700; padding: 1px 4px; border-radius: 5px; }
#moves .m.cur { background: #ffd98a; }

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

/* ---------- phone / narrow ---------- */
@media (max-width: 819px), (max-aspect-ratio: 1/1) {
  :root { --bs: min(calc(100vw - 16px), calc(100dvh - 290px), 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; }
  #capiBox { order: 0; width: var(--bs); min-height: 0; height: 64px; align-items: center; }
  #capi { 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: 8px 4px; }
  #movesBox { order: 4; width: var(--bs); min-height: 0; }
  #moves { max-height: 120px; }
}
@media (max-width: 819px) and (max-height: 700px) {
  #capiBox { height: 52px; } #capi { width: 44px; }
  #bubble { font-size: 13px; }
}
