:root {
  --lac: #3b0d08; --lac2: #5c1a10; --gold: #e7c063; --gold2: #b8862f; --ivory: #fbf1d9; --ink: #2a1610;
  --jade: #2f8a62; --red: #c0271b; --fs: clamp(12px, min(2.1vh, 2.6vw) + 5px, 24px);
  font-size: var(--fs);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--lac); color: var(--ivory);
  font-family: "Be Vietnam Pro", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; user-select: none; -webkit-user-select: none; }
#cv { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; touch-action: none; outline: none; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; touch-action: manipulation; }
.hidden { display: none !important; }

/* header */
#top { position: fixed; display: flex; align-items: center; gap: .35rem; padding: 0 .3rem; z-index: 3;
  background: linear-gradient(#5c1a10ee, #3b0d08ee); border: 2px solid var(--gold2); border-radius: 14px; box-shadow: 0 3px 10px #0006; }
#top h1 { flex: 1; margin: 0; font-size: 1.15rem; font-weight: 800; letter-spacing: .02em; color: var(--gold); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#top h1 small { font-size: .72rem; font-weight: 600; color: var(--ivory); opacity: .85; margin-left: .2rem; }
.icon { width: 2.3rem; height: 2.3rem; min-width: 34px; min-height: 34px; display: grid; place-items: center; border-radius: 50%; color: var(--gold);
  background: #ffffff10; }
.icon:hover { background: #ffffff22; }
.icon svg { width: 62%; height: 62%; }
.icon .slash { display: none; }
.icon.off .slash { display: inline; }
.icon.off .wave { display: none; }
.icon.off { color: #b9a58a; }

/* side panel */
#panel { position: fixed; z-index: 2; display: flex; flex-direction: column; gap: .45rem; padding: .55rem; min-height: 0;
  background: linear-gradient(160deg, #4a130ce8, #2c0905ee); border: 2px solid var(--gold2); border-radius: 16px; box-shadow: 0 6px 18px #0007; }
#mascot { display: flex; align-items: center; gap: .5rem; min-height: 0; }
#buf { width: 5.6rem; height: 5.6rem; object-fit: contain; flex: none; filter: drop-shadow(0 3px 3px #0006); transition: transform .2s; }
#buf.pop { transform: scale(1.08) rotate(-3deg); }
#bubble { position: relative; flex: 1; background: var(--ivory); color: var(--ink); border-radius: 14px; padding: .45rem .65rem; font-size: .86rem; line-height: 1.3; font-weight: 600; min-height: 2.6rem; }
#bubble::before { content: ""; position: absolute; left: -8px; top: 50%; margin-top: -8px; border: 8px solid transparent; border-right-color: var(--ivory); border-left: 0; }
#status { font-size: .84rem; font-weight: 700; color: var(--gold); background: #0003; border-radius: 10px; padding: .35rem .55rem; line-height: 1.25; }
#status.alert { color: #fff; background: var(--red); animation: pulse .9s ease-in-out infinite alternate; }
@keyframes pulse { to { filter: brightness(1.2); } }
#players { display: flex; flex-direction: column; gap: .3rem; }
.pl { display: flex; align-items: center; gap: .4rem; padding: .25rem .4rem; border-radius: 10px; background: #ffffff0d; border: 2px solid transparent; min-height: 2.1rem; }
.pl.turn { border-color: var(--gold); background: #e7c06322; }
.pl b { font-size: .86rem; white-space: nowrap; }
.caps { display: flex; flex-wrap: wrap; gap: 1px; margin-left: auto; justify-content: flex-end; }
.caps .disc { width: 1.15rem; height: 1.15rem; font-size: .7rem; border-width: 1px; }
.disc { width: 1.7rem; height: 1.7rem; flex: none; border-radius: 50%; display: inline-grid; place-items: center; font-weight: 800; font-size: 1rem; line-height: 1;
  background: radial-gradient(circle at 35% 30%, #fffaf0, #ead6a8); border: 2px solid var(--gold2);
  font-family: "Noto Serif CJK SC", "Noto Serif TC", "Songti SC", "SimSun", "PMingLiU", serif; }
.disc.red { color: var(--red); }
.disc.black { color: #1d1a17; }
.disc.viet { font-family: inherit; font-size: .5rem; letter-spacing: -.02em; }
#tools { display: grid; grid-template-columns: repeat(4, 1fr); gap: .35rem; }
.tool { display: flex; flex-direction: column; align-items: center; gap: .1rem; padding: .35rem .1rem; border-radius: 12px; background: #ffffff14; color: var(--ivory);
  font-size: .72rem; font-weight: 700; border: 1px solid #e7c06355; min-height: 44px; justify-content: center; }
.tool svg { width: 1.3rem; height: 1.3rem; color: var(--gold); }
.tool:hover { background: #ffffff24; }
.tool:disabled { opacity: .4; }
.tool.gold { background: linear-gradient(#f0cf78, #c9962f); color: var(--ink); border-color: #fff3; }
.tool.gold svg { color: var(--ink); }
#movesBox { flex: 1; min-height: 0; display: flex; flex-direction: column; background: #0003; border-radius: 10px; padding: .3rem .5rem; }
#movesBox h3 { margin: 0 0 .2rem; font-size: .78rem; color: var(--gold); }
#moves { flex: 1; min-height: 0; overflow-y: auto; margin: 0; padding: 0 0 0 1.8rem; font-size: .8rem; line-height: 1.45; font-variant-numeric: tabular-nums;
}
#moves li::marker { color: #e7c063aa; }
#moves li span { display: inline-block; min-width: 3.6em; }
#moves li .b { color: #d8cbb3; }

/* compact panels */
#panel.compact #movesBox { display: none; }
#panel.compact #buf { width: 4rem; height: 4rem; }
#panel.tiny { gap: .3rem; padding: .4rem; }
#panel.tiny #buf { width: 3rem; height: 3rem; }
#panel.tiny #bubble { font-size: .76rem; min-height: 0; padding: .3rem .5rem; }
#panel.tiny #status { font-size: .76rem; padding: .25rem .45rem; }
#panel.tiny .pl { min-height: 1.7rem; padding: .1rem .35rem; }
#panel.tiny .tool { min-height: 38px; padding: .2rem .1rem; font-size: .66rem; }
#panel.tiny .tool svg { width: 1.05rem; height: 1.05rem; }
#panel.port #players { flex-direction: row; }
#panel.port .pl { flex: 1; min-width: 0; }
#panel.port .pl b { overflow: hidden; text-overflow: ellipsis; }

#banner { position: fixed; z-index: 4; left: 50%; top: 40%; transform: translate(-50%, -50%); pointer-events: none; padding: .4rem 1.6rem;
  font-size: 2.2rem; font-weight: 900; color: #fff; background: linear-gradient(#d23a22, #8d150c); border: 3px solid var(--gold); border-radius: 18px;
  box-shadow: 0 8px 26px #000a; animation: bannerIn .9s ease-out both; white-space: nowrap; }
@keyframes bannerIn { 0% { transform: translate(-50%, -50%) scale(.4); opacity: 0; } 18% { transform: translate(-50%, -50%) scale(1.12); opacity: 1; }
  30% { transform: translate(-50%, -50%) scale(1); } 80% { opacity: 1; } 100% { opacity: 0; transform: translate(-50%, -60%) scale(1); } }

/* overlays */
.overlay { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 10px; background: #1a0603aa; }
.panel { position: relative; max-width: 940px; width: 100%; max-height: calc(100% - 4px); overflow: hidden;
  background: radial-gradient(circle at 20% 0%, #6b2214, #3b0d08 70%); border: 3px solid var(--gold); border-radius: 22px; padding: 1rem 1.2rem;
  box-shadow: 0 0 0 6px #3b0d08, 0 0 0 8px var(--gold2), 0 18px 50px #000b; }
.panel h2 { margin: 0; color: var(--gold); font-size: 1.9rem; font-weight: 900; letter-spacing: .01em; }
.start { display: grid; grid-template-columns: 1fr 1.1fr; gap: 1rem 1.4rem; align-items: center; }
.colA, .colB { display: flex; flex-direction: column; gap: .7rem; min-width: 0; }
.brand { display: flex; align-items: center; gap: .7rem; }
.brand img { width: 5.4rem; height: 5.4rem; object-fit: contain; filter: drop-shadow(0 3px 4px #0008); }
.brand h2 { font-size: 2.4rem; line-height: 1; }
.sub { margin: .2rem 0 0; color: #e8d6b4; font-style: italic; }
.intro { margin: 0; line-height: 1.4; font-size: .92rem; color: #f3e6cc; }
.intro strong { color: var(--gold); }
.btn { display: inline-flex; align-items: center; justify-content: center; padding: .65rem 1.2rem; border-radius: 999px; font-weight: 800; font-size: 1rem;
  background: linear-gradient(#f6d985, #cf9b33); color: var(--ink); box-shadow: 0 4px 0 #8a5a17, 0 6px 14px #0006; min-height: 44px; }
.btn:active { transform: translateY(2px); box-shadow: 0 2px 0 #8a5a17; }
.btn.big { font-size: 1.45rem; padding: .8rem 1.4rem; width: 100%; animation: glow 1.6s ease-in-out infinite alternate; }
@keyframes glow { to { box-shadow: 0 4px 0 #8a5a17, 0 0 26px #f6d98599; } }
.btn.ghost { background: #ffffff14; color: var(--ivory); box-shadow: none; border: 2px solid #e7c06388; }
.btn.small { font-size: .85rem; padding: .4rem .9rem; min-height: 36px; }
.keys { margin: 0; font-size: .74rem; color: #d9c7a6; line-height: 1.6; }
.k { display: inline-block; padding: 0 .35rem; border-radius: 5px; background: #ffffff1c; border: 1px solid #e7c06366; color: var(--ivory); font-weight: 700; }
.modes { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; }
.mode { text-align: left; padding: .55rem .7rem; border-radius: 14px; background: #ffffff10; border: 2px solid #e7c06344; display: flex; flex-direction: column; gap: .1rem; }
.mode b { font-size: 1.02rem; color: var(--ivory); }
.mode span { font-size: .74rem; color: #d6c4a3; }
.mode.on { background: linear-gradient(#f3d27d, #c89534); border-color: #fff8; }
.mode.on b, .mode.on span { color: var(--ink); }
#opts { display: flex; flex-direction: column; gap: .35rem; }
.optrow { display: flex; align-items: center; gap: .5rem; justify-content: space-between; }
.optrow > span { font-size: .8rem; color: #e8d6b4; white-space: nowrap; }
.seg { display: flex; background: #0004; border-radius: 999px; padding: 3px; gap: 2px; }
.seg button { padding: .3rem .7rem; border-radius: 999px; font-size: .8rem; font-weight: 700; color: #e8d6b4; min-height: 32px; white-space: nowrap; }
.seg button.on { background: var(--gold); color: var(--ink); }
#rulesLink { align-self: flex-start; }

.list { display: flex; flex-direction: column; gap: .6rem; max-width: 720px; max-height: calc(100% - 4px); }
.list .foot { display: flex; justify-content: center; padding-top: .2rem; flex: none; }
#puzGrid { display: grid; grid-template-columns: repeat(5, 1fr); gap: .5rem; overflow-y: auto; min-height: 0; padding: 4px; }
.pz { position: relative; padding: .5rem .3rem; border-radius: 14px; background: #ffffff12; border: 2px solid #e7c06355; display: flex; flex-direction: column; align-items: center; gap: .05rem; }
.pz b { font-size: 1.2rem; color: var(--gold); }
.pz span { font-size: .7rem; color: #e2d2b3; }
.pz.done { background: #2f8a6255; border-color: #7fd4ad; }
.pz.done::after { content: "✓"; position: absolute; right: 6px; top: 3px; color: #9ff0c8; font-weight: 900; }
.pz.today { box-shadow: 0 0 0 3px var(--gold), 0 0 16px #f6d985aa; }
.pz .tag { position: absolute; top: -9px; left: 50%; transform: translateX(-50%); background: var(--red); color: #fff; font-size: .62rem; font-weight: 800; padding: 1px 6px; border-radius: 99px; white-space: nowrap; }
.rules .scroll { overflow-y: auto; min-height: 0; padding-right: .3rem; font-size: .86rem; line-height: 1.45; color: #f3e6cc; }
.rules p { margin: .3rem 0; }
.rules b { color: var(--gold); }
.pcs { list-style: none; padding: 0; margin: .3rem 0; display: grid; grid-template-columns: 1fr 1fr; gap: .3rem .8rem; }
.pcs li { display: flex; gap: .45rem; align-items: flex-start; }
.pcs .disc { margin-top: .1rem; }
.end { display: flex; align-items: center; gap: 1rem; max-width: 560px; }
.end img { width: 8rem; height: 8rem; object-fit: contain; flex: none; }
.endtxt { display: flex; flex-direction: column; gap: .5rem; align-items: flex-start; }
.end p { margin: 0; color: #f3e6cc; line-height: 1.4; }
.end .row { display: flex; gap: .5rem; flex-wrap: wrap; }

/* small / short screens */
@media (max-height: 560px) {
  .panel { padding: .7rem .9rem; }
  .brand img { width: 3.8rem; height: 3.8rem; }
  .brand h2 { font-size: 1.9rem; }
  .colA, .colB { gap: .45rem; }
  .btn.big { font-size: 1.2rem; padding: .55rem 1rem; }
  .mode { padding: .35rem .55rem; }
  .end img { width: 5.5rem; height: 5.5rem; }
}
@media (max-height: 420px) {
  .intro { font-size: .8rem; }
  .keys { display: none; }
  .sub { display: none; }
  .mode span { display: none; }
  .seg button { min-height: 28px; padding: .2rem .55rem; }
  #puzGrid { grid-template-columns: repeat(8, 1fr); }
  .pz span { display: none; }
  .pcs { font-size: .8rem; }
}
@media (max-aspect-ratio: 1/1) {
  .start { grid-template-columns: 1fr; gap: .6rem; }
  .colA, .colB { display: contents; }
  .start .brand { order: 1; } .start .intro { order: 2; } .start .modes { order: 3; } .start #opts { order: 4; } .start #playBtn { order: 5; } .start #rulesLink { order: 6; justify-self: center; } .start .keys { order: 7; }
  .pcs { grid-template-columns: 1fr; }
  #puzGrid { grid-template-columns: repeat(4, 1fr); }
  .panel h2 { font-size: 1.6rem; }
}
@media (max-aspect-ratio: 1/1) and (max-width: 420px) {
  .panel { padding: .8rem .8rem; }
  .brand img { width: 4rem; height: 4rem; }
  .intro { font-size: .84rem; }
  .keys { display: none; }
}
