:root {
  --tb: 44px;
  --u: 1px;                 /* board width / 640, set by JS */
  --gold: #f0c24b; --gold2: #b8862a; --stone: #2a1e10; --stone2: #3b2a15; --ink: #fff4d6;
  --safe-t: env(safe-area-inset-top, 0px); --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px); --safe-l: env(safe-area-inset-left, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: #120c06; color: var(--ink);
  font-family: "Trebuchet MS", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "Hiragino Sans", sans-serif;
  overscroll-behavior: none; overflow: hidden; }
#app { height: 100dvh; width: 100dvw; display: flex; flex-direction: column; align-items: center;
  padding: var(--safe-t) var(--safe-r) var(--safe-b) var(--safe-l);
  background: radial-gradient(ellipse at 50% 30%, #3a2a14 0%, #120c06 75%); }

/* ---------- toolbar ---------- */
#toolbar { height: var(--tb); width: min(100%, 980px); display: flex; align-items: center; gap: 10px; padding: 0 8px; flex: none; }
.logo { background: none; border: 0; cursor: pointer; color: var(--gold); font: 900 22px/1 Georgia, "Songti SC", serif; letter-spacing: 3px;
  text-shadow: 0 2px 0 #5b3b0c, 0 0 12px rgba(240,194,75,.35); padding: 6px 4px; min-height: 44px; user-select: none; }
.hudline { flex: 1; font-size: 13px; opacity: .9; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
.tools { display: flex; gap: 6px; }
.tbtn { width: 44px; height: 38px; border-radius: 10px; border: 1px solid #6b4a1a; background: linear-gradient(#4a3318, #2a1c0c); color: var(--ink);
  font-size: 18px; cursor: pointer; }
.tbtn:active, .btn:active, .vbtn:active { transform: translateY(1px); filter: brightness(1.15); }
.tbtn:focus-visible, .btn:focus-visible, .logo:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

/* ---------- board ---------- */
#stageWrap { flex: 1; width: 100%; min-height: 0; display: flex; align-items: center; justify-content: center; padding-bottom: 20px; }
#board { position: relative; aspect-ratio: 4 / 3;
  width: min(98vw, calc((100dvh - var(--tb) - 24px - var(--safe-t) - var(--safe-b)) * 4 / 3));
  max-height: calc(100dvh - var(--tb) - 24px); border: 3px solid #6b4a1a; border-radius: 12px; overflow: hidden; background: #000;
  box-shadow: 0 10px 40px rgba(0,0,0,.6), inset 0 0 0 1px rgba(255,220,140,.15); touch-action: none; }
#game { width: 100%; height: 100%; display: block; touch-action: none; cursor: crosshair; image-rendering: auto; }

/* ---------- overlay & panels ---------- */
#overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(12,7,2,.45); backdrop-filter: blur(calc(var(--u)*2)); }
#overlay.hidden { display: none; }
.panel { width: calc(var(--u) * 330); max-height: 96%; overflow: auto; padding: calc(var(--u)*16) calc(var(--u)*18); text-align: center;
  background: linear-gradient(160deg, rgba(60,42,20,.93), rgba(28,18,8,.95)); border: 2px solid var(--gold2); border-radius: calc(var(--u)*14);
  box-shadow: 0 8px 30px rgba(0,0,0,.6), inset 0 0 20px rgba(240,194,75,.08); font-size: calc(var(--u)*14); }
.panel[hidden] { display: none; }
.panel.wide { width: calc(var(--u) * 430); }
.panel h1.big { margin: 0; font: 900 calc(var(--u)*58)/1 Georgia, "Songti SC", serif; letter-spacing: calc(var(--u)*6); cursor: pointer; user-select: none;
  color: var(--gold); text-shadow: 0 calc(var(--u)*3) 0 #5b3b0c, 0 0 calc(var(--u)*24) rgba(240,194,75,.45); }
.panel h2 { margin: 0 0 calc(var(--u)*10); color: var(--gold); font-size: calc(var(--u)*24); }
.sub { margin: calc(var(--u)*2) 0 calc(var(--u)*12); opacity: .8; font-size: calc(var(--u)*13); }
.note { color: #ffe9a3; min-height: 1.2em; }
.stack { display: flex; flex-direction: column; gap: calc(var(--u)*8); }
.btn { font: inherit; font-weight: 700; color: var(--ink); cursor: pointer; min-height: 44px; padding: 0 16px; border-radius: 10px; border: 1px solid #8c6522;
  background: linear-gradient(#5a3f1d, #34230e); text-decoration: none; display: inline-flex; align-items: center; justify-content: center; }
.btn:hover { filter: brightness(1.15); }
.btn.primary { background: linear-gradient(#e0ac3c, #a8741a); color: #2a1a05; border-color: #f6d27a; }
.btn.small { min-height: 38px; padding: 0 12px; font-size: 13px; }
.btn.danger { border-color: #a33; background: linear-gradient(#6d2a24, #3d1411); }
.btn[disabled] { opacity: .4; pointer-events: none; }
.badge { display: inline-block; margin: calc(var(--u)*12) 0 calc(var(--u)*8); padding: 8px 12px; min-height: 36px; border-radius: 999px; font-size: calc(var(--u)*12);
  color: #ffe9a3; text-decoration: none; border: 1px dashed rgba(240,194,75,.55); background: rgba(240,194,75,.08); }
.grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: calc(var(--u)*8); margin-bottom: calc(var(--u)*12); }
.grid .cell { min-height: 48px; border-radius: 10px; border: 1px solid #8c6522; background: linear-gradient(#5a3f1d,#34230e); color: var(--ink); cursor: pointer; font: inherit; font-weight: 800; }
.grid .cell small { display: block; font-weight: 400; opacity: .7; font-size: .7em; }
.grid .cell[disabled] { opacity: .35; cursor: not-allowed; }
.scores { width: 100%; border-collapse: collapse; margin-bottom: calc(var(--u)*10); font-size: calc(var(--u)*13); }
.scores td, .scores th { padding: 4px 6px; border-bottom: 1px solid rgba(255,220,140,.15); text-align: left; }
.scores td:last-child, .scores th:last-child { text-align: right; }
.howto { text-align: left; margin: 0 0 calc(var(--u)*8); padding-left: 1.2em; line-height: 1.45; }
.powers { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; margin-bottom: calc(var(--u)*10); text-align: left; font-size: .9em; }
.powers div { display: grid; grid-template-columns: 34px 1fr; align-items: center; column-gap: 8px; }
.powers img { grid-row: span 2; } .powers small { opacity: .75; }
.form { display: flex; flex-direction: column; gap: 8px; text-align: left; }
.form label { display: grid; grid-template-columns: 38% 1fr; align-items: center; gap: 8px; }
.form label.row { display: flex; }
.form input[type=range] { width: 100%; min-height: 28px; accent-color: var(--gold); }
.form input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--gold); }
.form select, .form input[type=text] { min-height: 40px; border-radius: 8px; border: 1px solid #8c6522; background: #20150a; color: var(--ink); padding: 0 8px; font: inherit; width: 100%; }
.form fieldset { border: 1px solid rgba(240,194,75,.35); border-radius: 10px; padding: 8px; margin: 2px 0; }
.form legend { padding: 0 6px; color: var(--gold); font-size: .9em; }
.row { display: flex; gap: 8px; align-items: center; margin: 4px 0; } .row.wrap { flex-wrap: wrap; }
.warn { color: #ff9c8a; font-size: .85em; margin: 6px 0 0; }

.banner { position: absolute; left: 0; right: 0; top: 36%; text-align: center; pointer-events: none; opacity: 0; transform: scale(.9);
  font: 900 calc(var(--u)*46)/1.1 Georgia, "Songti SC", serif; color: var(--gold); letter-spacing: 3px;
  text-shadow: 0 calc(var(--u)*3) 0 #5b3b0c, 0 0 calc(var(--u)*20) rgba(0,0,0,.9); transition: opacity .25s, transform .25s; }
.banner.show { opacity: 1; transform: scale(1); }
.banner small { display: block; font: 600 calc(var(--u)*16)/1.4 "Trebuchet MS", sans-serif; letter-spacing: 1px; color: #fff4d6; }

/* ---------- virtual button (touch) ---------- */
.vbtn { position: absolute; right: calc(var(--u)*10); bottom: calc(var(--u)*10); width: max(52px, calc(var(--u)*56)); height: max(52px, calc(var(--u)*56)); border-radius: 50%;
  border: 2px solid var(--gold); background: rgba(60,40,15,.8); color: var(--gold); font-size: 26px; cursor: pointer; display: none; z-index: 5; }
@media (pointer: coarse), (max-width: 700px) { .vbtn { display: block; } }

/* ---------- toast / egg / rotate / footer ---------- */
.toast { position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translate(-50%, 20px); opacity: 0; pointer-events: none; z-index: 30;
  background: rgba(30,20,8,.95); border: 1px solid var(--gold2); border-radius: 999px; padding: 10px 18px; font-size: 14px; max-width: 92vw; transition: .25s; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.egg { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.7); display: flex; align-items: center; justify-content: center; padding: 16px; }
.egg[hidden] { display: none; }
.egg .card { width: min(420px, 100%); background: linear-gradient(160deg, #4b3417, #1f1307); border: 2px solid var(--gold); border-radius: 16px; padding: 22px; text-align: center;
  box-shadow: 0 0 60px rgba(240,194,75,.35); animation: pop .4s ease-out; display: flex; flex-direction: column; gap: 10px; }
.egg .confetti { font-size: 44px; animation: bob 1.2s ease-in-out infinite; }
.egg h3 { margin: 0; color: var(--gold); font-size: 22px; } .egg p { margin: 0; line-height: 1.5; }
@keyframes pop { from { transform: scale(.7); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes bob { 50% { transform: translateY(-6px) rotate(8deg); } }
.rotate { position: fixed; inset: 0; z-index: 50; background: #120c06; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 24px; font-size: 18px; }
.rotate[hidden] { display: none; } .rotate div { font-size: 64px; animation: bob 1.4s infinite; }
.foot { position: fixed; bottom: calc(2px + var(--safe-b)); left: 0; right: 0; text-align: center; font-size: 10px; opacity: .45; pointer-events: none; }

/* ---------- small screens ---------- */
@media (max-width: 700px) {
  :root { --tb: 40px; }
  .logo { font-size: 18px; } .hudline { font-size: 11px; } .tbtn { width: 44px; height: 36px; }
  .foot { display: none; } #stageWrap { padding-bottom: 4px; }
  #board { width: min(100vw, calc((100dvh - var(--tb) - 8px) * 4 / 3)); border-width: 2px; border-radius: 6px; }
}
@media (max-height: 520px) { .foot { display: none; } #stageWrap { padding-bottom: 4px; } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
