* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #070908; color: #eee; font: 14px/1.35 system-ui, -apple-system, "PingFang TC", "Noto Sans TC", sans-serif; user-select: none; -webkit-user-select: none; }
#gl { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; touch-action: none; }
#hud { position: fixed; inset: 0; pointer-events: none; }
#hud button, #over .card, #spinBox, #power, #fine { pointer-events: auto; }
#top { position: absolute; top: max(10px, env(safe-area-inset-top)); left: 12px; right: 12px; display: flex; justify-content: space-between; align-items: center; }
#brand { font-size: 15px; letter-spacing: .5px; text-shadow: 0 1px 3px #000; color: #e9e2cf; }
#brand b { color: #f3c969; font-weight: 700; }
#brand span { margin-left: 8px; font-size: 12px; padding: 2px 7px; border-radius: 10px; background: #ffffff1c; color: #cfd; }
#buttons { display: flex; gap: 8px; }
button { white-space: nowrap; font: inherit; color: #f2efe6; background: #1b1f1dcc; border: 1px solid #ffffff2a; border-radius: 10px; padding: 7px 13px; backdrop-filter: blur(6px); cursor: pointer; }
button:active { background: #2e3a33; }
#tray { position: absolute; top: calc(max(10px, env(safe-area-inset-top)) + 40px); left: 12px; display: flex; gap: 4px; flex-wrap: wrap; max-width: 60vw; }
#tray .b { width: 20px; height: 20px; border-radius: 50%; font-size: 10px; font-weight: 700; display: grid; place-items: center; color: #111; box-shadow: inset -2px -3px 4px #0006, 0 1px 2px #000; }
#msg { position: absolute; top: 22%; left: 50%; transform: translateX(-50%); padding: 8px 18px; border-radius: 20px; background: #000a; color: #fff; font-size: 16px; opacity: 0; transition: opacity .35s; white-space: pre-line; text-align: center; max-width: 92vw; line-height: 1.5; }
#msg.show { opacity: 1; }
#spinBox { position: absolute; left: 14px; bottom: max(16px, env(safe-area-inset-bottom)); text-align: center; touch-action: none; }
#spinBall { width: 86px; height: 86px; border-radius: 50%; background: radial-gradient(circle at 35% 30%, #fff, #e8e4da 45%, #a9a49a 100%); position: relative; box-shadow: 0 3px 10px #000a; cursor: crosshair; }
#spinBall::before, #spinBall::after { content: ""; position: absolute; background: #0002; }
#spinBall::before { left: 50%; top: 10%; bottom: 10%; width: 1px; }
#spinBall::after { top: 50%; left: 10%; right: 10%; height: 1px; }
#spinDot { position: absolute; width: 14px; height: 14px; margin: -7px 0 0 -7px; border-radius: 50%; background: #d22; box-shadow: 0 0 0 2px #fff8; left: 50%; top: 50%; }
.lbl { font-size: 12px; color: #cfc9b8; margin-top: 4px; text-shadow: 0 1px 2px #000; }
#fine { position: absolute; left: 50%; transform: translateX(-50%); bottom: max(16px, env(safe-area-inset-bottom)); display: flex; gap: 14px; }
#fine button { width: 54px; height: 44px; font-size: 18px; }
#powerBox { position: absolute; right: 14px; top: 50%; transform: translateY(-50%); text-align: center; }
#power { width: 58px; height: min(46vh, 300px); border-radius: 14px; background: linear-gradient(#1a1d1bcc, #101211cc); border: 1px solid #ffffff2a; position: relative; overflow: hidden; touch-action: none; cursor: ns-resize; }
#powerFill { position: absolute; left: 0; right: 0; top: 0; height: 0%; background: linear-gradient(#3fcf6e, #f2c94c 55%, #e5482f); opacity: .9; }
#powerKnob { position: absolute; left: 0; right: 0; bottom: 6px; font-size: 10px; color: #ddd8; line-height: 1.25; }
#help { position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(max(16px, env(safe-area-inset-bottom)) + 56px); font-size: 12px; color: #ffffffa0; text-shadow: 0 1px 2px #000; text-align: center; width: min(92vw, 640px); }
@media (max-width: 640px) { #help { display: none; } #brand span { display: none; } }
#tray .b.dim { opacity: .22; }
#players { position: absolute; top: calc(max(10px, env(safe-area-inset-top)) + 4px); left: 50%; transform: translateX(-50%); text-align: center; }
#players .row { display: flex; gap: 10px; justify-content: center; }
#players .pl { position: relative; min-width: 150px; padding: 6px 10px; border-radius: 12px; background: #0d100fcc; border: 1px solid #ffffff22; opacity: .8; transition: all .25s; }
#players .pl.active { opacity: 1; border-color: #f3c969; box-shadow: 0 0 0 1px #f3c96955, 0 0 18px #f3c96933; }
#players .pl.winner { opacity: 1; border-color: #5cff9a; }
#players .nm { font-weight: 700; color: #f3e6c4; font-size: 14px; }
#players .grp { font-size: 12px; color: #bfc9c0; }
#players .balls { display: flex; gap: 2px; justify-content: center; margin-top: 4px; min-height: 16px; }
#players .b { width: 16px; height: 16px; border-radius: 50%; font-size: 8px; font-weight: 700; display: grid; place-items: center; box-shadow: inset -1px -2px 3px #0006; }
#status { margin-top: 6px; font-size: 13px; color: #fff; text-shadow: 0 1px 3px #000; }
#over { position: absolute; inset: 0; display: none; place-items: center; background: #0007; }
#over.show { display: grid; }
#over .card { padding: 22px 28px; border-radius: 18px; background: #121614ee; border: 1px solid #f3c96966; text-align: center; min-width: 260px; }
#over h2 { margin: 0 0 6px; color: #f3c969; font-size: 24px; }
#over p { margin: 0 0 16px; color: #ddd; }
#over .btns { display: flex; gap: 10px; justify-content: center; }
@media (max-width: 640px) {
  #players { top: calc(max(10px, env(safe-area-inset-top)) + 44px); width: 96vw; }
  #players .pl { position: relative; min-width: 0; flex: 1; padding: 4px 6px; }
  #tray { top: auto; bottom: calc(max(16px, env(safe-area-inset-bottom)) + 120px); }
}
@media (max-width: 640px) {
  #brand { font-size: 13px; white-space: nowrap; }
  #brand b { display: none; }
  #buttons { gap: 6px; }
  #buttons button { padding: 6px 9px; font-size: 13px; }
  #players { top: calc(max(10px, env(safe-area-inset-top)) + 42px); }
}
#fade { position: fixed; inset: 0; background: #000; opacity: 0; pointer-events: none; }

button.ico { padding-left: 8px; padding-right: 8px; position: relative; }
button.ico.off { opacity: 0.45; }
button.ico.off::after { content: ""; position: absolute; left: 20%; right: 20%; top: 50%; height: 2px; background: #f66; transform: rotate(-35deg); }

/* player panel balls: on table = big, bright, glowing; pocketed = small, grey, checked */
#players .balls { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; justify-content: center; margin-top: 5px; min-height: 26px; }
#players .b { position: relative; width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; flex: none;
  box-shadow: inset -2px -3px 4px #0007, inset 2px 2px 3px #fff5, 0 0 0 1.5px #ffffffd0, 0 0 8px var(--glow, transparent); }
#players .b span { width: 13px; height: 13px; border-radius: 50%; background: #fff; color: #111; font-size: 9.5px; font-weight: 800; line-height: 13px; text-align: center; }
#players .b.dim { width: 14px; height: 14px; opacity: .3; filter: grayscale(1); box-shadow: inset -1px -1px 2px #0006; }
#players .b.dim span { display: none; }
#players .b.dim::after { content: "✓"; position: absolute; inset: 0; display: grid; place-items: center; color: #fff; font-size: 11px; font-weight: 900; text-shadow: 0 0 2px #000; }
#players .left { font-size: 11px; color: #f3e6c4; margin-top: 3px; min-height: 13px; }
@media (max-width: 640px) {
  #players .balls { gap: 3px; min-height: 22px; }
  #players .b { width: 21px; height: 21px; }
  #players .b span { width: 12px; height: 12px; font-size: 9px; line-height: 12px; }
  #players .b.dim { width: 12px; height: 12px; }
  #players .b.dim::after { font-size: 9px; }
}

/* online */
#btnLeave { display: none; }
body.online #btnLeave { display: inline-block; }
body.online #btnOnline, body.online #btnMode, body.online #btnRack { display: none; }
#roomBar { position: absolute; top: calc(max(10px, env(safe-area-inset-top)) + 40px); left: 12px; font-size: 13px; color: #e9e2cf; text-shadow: 0 1px 2px #000; display: flex; gap: 8px; align-items: center; pointer-events: auto; }
#roomBar b { font-size: 16px; letter-spacing: 2px; color: #f3c969; }
#roomBar button { padding: 4px 9px; font-size: 12px; }
#roomBar .ok { color: #6be08f; font-size: 12px; }
#roomBar .bad { color: #ff8a7a; font-size: 12px; }
body.online #tray { top: calc(max(10px, env(safe-area-inset-top)) + 74px); }
#players .pl.off { opacity: .55; border-style: dashed; }
#lobby { position: absolute; inset: 0; display: none; place-items: center; background: #0008; pointer-events: auto; }
#lobby.show { display: grid; }
#lobby .card { padding: 22px 26px; border-radius: 18px; background: #121614f2; border: 1px solid #f3c96966; text-align: center; width: min(92vw, 340px); display: flex; flex-direction: column; gap: 12px; }
#lobby h2 { margin: 0; color: #f3c969; font-size: 22px; }
#lobby input { font: inherit; color: #fff; background: #0b0e0d; border: 1px solid #ffffff33; border-radius: 10px; padding: 8px 10px; user-select: text; -webkit-user-select: text; }
#lobby .nick { display: flex; gap: 8px; align-items: center; justify-content: center; color: #cfc9b8; }
#lobby .nick input { flex: 1; min-width: 0; }
#lobby .btns { display: flex; gap: 10px; justify-content: center; }
#lobby .btns button { flex: 1; padding: 10px; }
#lobby .join { display: flex; gap: 8px; }
#lobby .join input { flex: 1; min-width: 0; text-transform: uppercase; letter-spacing: 2px; text-align: center; }
#lobby p { margin: 0; min-height: 18px; color: #ffd38a; font-size: 13px; }
#lobby .ghost { background: transparent; border-color: #ffffff1c; color: #bbb; }
@media (max-width: 640px) { body.online #tray { top: auto; } #roomBar { top: auto; bottom: calc(max(16px, env(safe-area-inset-bottom)) + 150px); } }

/* identity */
.avatar { display: block; flex: none; filter: drop-shadow(0 1px 2px #0008); }
#players .who { display: flex; gap: 6px; align-items: center; justify-content: center; }
#players .av:empty { display: none; }
#players .av .avatar { width: 26px; height: 26px; }
#lobby .me { display: flex; gap: 10px; align-items: center; justify-content: center; }
#lobby .me b { color: #f3e6c4; font-size: 17px; max-width: 170px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.small { padding: 4px 10px !important; font-size: 12px !important; }
#profile { position: absolute; inset: 0; display: none; place-items: center; background: #000a; pointer-events: auto; z-index: 30; }
#profile.show { display: grid; }
#profile .card { padding: 20px 22px; border-radius: 18px; background: #121614f5; border: 1px solid #f3c96966; text-align: center; width: min(94vw, 380px); display: flex; flex-direction: column; gap: 10px; max-height: 94vh; overflow: auto; }
#profile h2 { margin: 0; color: #f3c969; font-size: 21px; }
#profile .sub { margin: 0; color: #aab3ab; font-size: 12px; }
#profile .guest { padding: 10px; }
#profile .guest small { color: #f3c969; margin-left: 4px; }
#profile .or { color: #8a948b; font-size: 12px; }
#profile input { font: inherit; color: #fff; background: #0b0e0d; border: 1px solid #ffffff33; border-radius: 10px; padding: 8px 10px; text-align: center; user-select: text; -webkit-user-select: text; }
#profile #pfMsg { margin: 0; min-height: 16px; color: #ff9a8a; font-size: 13px; }
#profile .btns { display: flex; gap: 10px; }
#profile .btns button { flex: 1; padding: 10px; }
.avgrid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; }
.avgrid button { padding: 2px; border-radius: 50%; background: transparent; border: 2px solid transparent; aspect-ratio: 1; display: grid; place-items: center; }
.avgrid button .avatar { width: 100%; height: 100%; }
.avgrid button[aria-checked="true"] { border-color: #f3c969; box-shadow: 0 0 10px #f3c96988; }
@media (max-width: 400px) { .avgrid { grid-template-columns: repeat(6, 1fr); } }
/* quick chat */
#btnChat { display: none; position: absolute; right: 14px; bottom: calc(max(16px, env(safe-area-inset-bottom)) + 118px); width: 44px; height: 44px; border-radius: 50%; padding: 0; font-size: 20px; pointer-events: auto; z-index: 12; }
body.online #btnChat { display: grid; place-items: center; }
#btnChat.cool { opacity: .45; }
#chatPick { display: none; position: absolute; right: 14px; bottom: calc(max(16px, env(safe-area-inset-bottom)) + 170px); width: 210px; padding: 8px; border-radius: 14px; background: #121614f2; border: 1px solid #f3c96955; pointer-events: auto; z-index: 13; flex-direction: column; gap: 6px; }
#chatPick.show { display: flex; }
#chatPick .phr { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
#chatPick .phr button { padding: 7px 4px; font-size: 13px; border-radius: 9px; }
#chatPick .phr button:disabled { opacity: .45; }
#players .bubble { position: absolute; top: calc(100% + 8px); left: 50%; transform: translate(-50%, -4px) scale(.9); opacity: 0; pointer-events: none; white-space: nowrap; padding: 6px 12px; border-radius: 14px; background: #fff8ec; color: #1b1b1b; font-size: 14px; font-weight: 700; box-shadow: 0 4px 14px #0007; transition: opacity .18s, transform .18s; z-index: 5; }
#players .bubble::before { content: ""; position: absolute; top: -6px; left: 50%; margin-left: -6px; border: 6px solid transparent; border-top: 0; border-bottom-color: #fff8ec; }
#players .bubble.show { opacity: 1; transform: translate(-50%, 0) scale(1); }
body.online #players .who { pointer-events: auto; cursor: pointer; }
