:root {
  --bg: #FFF8EC;
  --bg2: #FDEFD8;
  --card: #FFFFFF;
  --line: #EFE3CC;
  --ink: #2A3329;
  --ink2: #6B7F68;
  --ink3: #A2B19E;
  --green: #4EBF7F;
  --green-d: #3A9862;
  --green-l: #E3F6EA;
  --amber: #F2994A;
  --amber-d: #D27A2E;
  --amber-l: #FDEBD9;
  --blue: #76B8E8;
  --blue-d: #4F93C7;
  --blue-l: #E4F1FB;
  --pink: #F59AAE;
  --pink-d: #DB6F88;
  --pink-l: #FDE6EB;
  --red: #E8615A;
  --r: 22px;
  --shadow: 0 6px 0 rgba(42, 51, 41, .06), 0 10px 28px rgba(160, 120, 60, .10);
  color-scheme: light;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  font-family: 'Huninn', 'Baloo 2', 'PingFang TC', 'Noto Sans TC', system-ui, sans-serif;
  background: var(--bg);
  background-image:
    radial-gradient(circle at 12% 8%, rgba(78, 191, 127, .13) 0 90px, transparent 91px),
    radial-gradient(circle at 92% 22%, rgba(242, 153, 74, .12) 0 70px, transparent 71px),
    radial-gradient(circle at 80% 92%, rgba(118, 184, 232, .14) 0 110px, transparent 111px);
  background-attachment: fixed;
  color: var(--ink);
  min-height: 100dvh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
.num { font-family: 'Baloo 2', 'Huninn', system-ui, sans-serif; font-weight: 800; font-variant-numeric: tabular-nums; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: rgba(255, 248, 236, .88);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--ink); font-size: 20px; }
.brand img { animation: wobble 6s ease-in-out infinite; }
.who { font-size: 14px; color: var(--ink2); display: flex; align-items: center; gap: 8px; }
.who button { background: none; border: 0; color: var(--ink3); text-decoration: underline; cursor: pointer; padding: 4px; }
.app { max-width: 620px; margin: 0 auto; padding: 8px 16px calc(40px + env(safe-area-inset-bottom)); }

/* ---------- generic ---------- */
.card {
  background: var(--card); border-radius: var(--r); box-shadow: var(--shadow);
  padding: 20px; border: 2px solid var(--line);
}
.stack > * + * { margin-top: 14px; }
h1, h2, h3 { margin: 0; font-weight: 400; }
h1 { font-size: 28px; }
h2 { font-size: 22px; }
.muted { color: var(--ink2); }
.small { font-size: 14px; }
.center { text-align: center; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.spread { justify-content: space-between; }
.grow { flex: 1; }

.btn {
  --c: var(--green); --cd: var(--green-d);
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 50px; padding: 10px 22px;
  border: 0; border-radius: 18px; cursor: pointer;
  background: var(--c); color: #fff; font-size: 18px;
  box-shadow: 0 5px 0 var(--cd);
  transition: transform .08s, box-shadow .08s, filter .15s;
  user-select: none; text-decoration: none;
}
.btn:active:not(:disabled) { transform: translateY(4px); box-shadow: 0 1px 0 var(--cd); }
.btn:disabled { filter: grayscale(.8) opacity(.5); cursor: not-allowed; }
.btn.block { width: 100%; }
.btn.amber { --c: var(--amber); --cd: var(--amber-d); }
.btn.blue { --c: var(--blue); --cd: var(--blue-d); }
.btn.pink { --c: var(--pink); --cd: var(--pink-d); }
.btn.ghost { --c: #fff; --cd: var(--line); color: var(--ink); border: 2px solid var(--line); }
.btn.sm { min-height: 40px; padding: 6px 14px; font-size: 15px; border-radius: 14px; box-shadow: 0 4px 0 var(--cd); }

.input {
  width: 100%; min-height: 54px; padding: 12px 16px; font-size: 20px;
  border: 2px solid var(--line); border-radius: 16px; background: #fff; outline: none;
}
.input:focus { border-color: var(--green); box-shadow: 0 0 0 4px var(--green-l); }

.pill { display: inline-flex; align-items: center; gap: 4px; padding: 3px 12px; border-radius: 999px; font-size: 14px; background: var(--green-l); color: var(--green-d); }
.pill.amber { background: var(--amber-l); color: var(--amber-d); }
.pill.blue { background: var(--blue-l); color: var(--blue-d); }
.pill.pink { background: var(--pink-l); color: var(--pink-d); }

/* ---------- login ---------- */
.hero { text-align: center; padding: 30px 0 10px; }
.hero .mascot { width: 92px; height: 92px; animation: bounce 2.4s ease-in-out infinite; }
.hero h1 { margin-top: 12px; font-size: 34px; }
.hero p { margin: 6px 0 0; color: var(--ink2); }
.pin { letter-spacing: 14px; text-align: center; font-family: 'Baloo 2', monospace; font-weight: 800; font-size: 30px; }

/* ---------- lobby ---------- */
.games { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.game-tile {
  position: relative; display: flex; flex-direction: column; gap: 6px;
  padding: 18px 14px 16px; border-radius: var(--r); text-decoration: none; color: var(--ink);
  background: var(--card); border: 2px solid var(--line); box-shadow: var(--shadow);
  transition: transform .15s;
  min-height: 170px;
}
.game-tile:active { transform: scale(.97); }
.game-tile .emoji { font-size: 44px; line-height: 1; filter: drop-shadow(0 4px 0 rgba(0,0,0,.06)); }
.game-tile .t { font-size: 21px; }
.game-tile .d { font-size: 13px; color: var(--ink2); line-height: 1.45; }
.game-tile .tag { position: absolute; top: 12px; right: 12px; }
.game-tile.g1 { background: linear-gradient(160deg, #fff 55%, var(--blue-l)); }
.game-tile.g2 { background: linear-gradient(160deg, #fff 55%, var(--green-l)); }
.game-tile.g3 { background: linear-gradient(160deg, #fff 55%, var(--amber-l)); }
.game-tile.g4 { background: linear-gradient(160deg, #fff 55%, var(--pink-l)); }
.game-tile:hover .emoji { animation: wobble .6s; }

/* ---------- rules ---------- */
.rules { line-height: 1.75; }
.rules ol, .rules ul { padding-left: 1.3em; margin: 6px 0; }
.rules li + li { margin-top: 4px; }
.rules b { color: var(--green-d); font-weight: 400; background: var(--green-l); padding: 0 4px; border-radius: 6px; }
.back { display: inline-flex; align-items: center; gap: 4px; color: var(--ink2); text-decoration: none; font-size: 15px; padding: 6px 0; }

/* ---------- hud ---------- */
.hud { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.hud .stat { background: #fff; border: 2px solid var(--line); border-radius: 14px; padding: 4px 12px; min-width: 70px; text-align: center; }
.hud .stat small { display: block; font-size: 11px; color: var(--ink2); }
.hud .stat .num { font-size: 22px; line-height: 1.1; }
.progress { height: 10px; background: var(--bg2); border-radius: 999px; overflow: hidden; }
.progress > i { display: block; height: 100%; background: var(--green); border-radius: 999px; transition: width .3s; }

/* ---------- keypad ---------- */
.answer { display: flex; align-items: center; justify-content: center; gap: 10px; }
.answer .display {
  min-width: 130px; height: 58px; border-radius: 16px; background: #fff; border: 3px solid var(--line);
  display: flex; align-items: center; justify-content: center; font-size: 34px;
}
.answer .display.shake { animation: shake .35s; border-color: var(--red); }
.answer .display.good { border-color: var(--green); background: var(--green-l); }
.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; max-width: 360px; margin: 0 auto; }
.keypad button {
  height: 50px; border-radius: 16px; border: 2px solid var(--line); background: #fff; font-size: 26px;
  box-shadow: 0 4px 0 var(--line); cursor: pointer;
  font-family: 'Baloo 2', sans-serif; font-weight: 800;
}
.keypad button:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--line); }
.keypad button.ok { background: var(--green); color: #fff; border-color: var(--green-d); box-shadow: 0 4px 0 var(--green-d); font-family: 'Huninn'; font-size: 20px; font-weight: 400; }

/* ---------- cubes ---------- */
.stage3d {
  position: relative; width: min(100%, 50dvh); margin: 0 auto; aspect-ratio: 1 / 1;
  border-radius: var(--r); overflow: hidden; touch-action: none;
  background: radial-gradient(circle at 50% 35%, #fff, var(--blue-l));
  border: 2px solid var(--line);
}
.stage3d canvas { display: block; width: 100%; height: 100%; }
.stage3d .hint { position: absolute; bottom: 10px; left: 0; right: 0; text-align: center; font-size: 13px; color: var(--ink2); pointer-events: none; }

/* ---------- rects ---------- */
.target { text-align: center; }
.target .num { font-size: 44px; color: var(--green-d); line-height: 1; }
.grid {
  --n: 5;
  position: relative; display: grid; grid-template-columns: repeat(var(--n), 1fr); gap: 5px;
  width: min(100%, 440px, 46dvh); margin: 0 auto; padding: 8px; border-radius: var(--r); background: var(--bg2);
}
.grid .cell {
  aspect-ratio: 1; display: flex; align-items: center; justify-content: center;
  background: #fff; border-radius: 12px; font-size: clamp(18px, 6.5vw, 30px);
  box-shadow: 0 3px 0 var(--line); transition: background .15s, transform .15s;
}
.grid .cell.hl { background: var(--green); color: #fff; transform: scale(.94); box-shadow: 0 3px 0 var(--green-d); }
.grid .cell.sel { background: var(--amber-l); }
.rect-list { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.rect-list button { border: 2px solid var(--line); background: #fff; border-radius: 12px; padding: 4px 10px; cursor: pointer; font-size: 14px; }
.rect-list button.on { background: var(--green); color: #fff; border-color: var(--green-d); }

/* ---------- poker ---------- */
.cards { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; max-width: 400px; margin: 0 auto; perspective: 800px; }
.cards.board { grid-template-columns: repeat(5, 1fr); max-width: 420px; }
.pcard { position: relative; aspect-ratio: 5 / 7; cursor: pointer; border: 0; padding: 0; background: none; }
.pcard .in { position: absolute; inset: 0; transition: transform .5s cubic-bezier(.3, 1.4, .5, 1); transform-style: preserve-3d; }
.pcard.down .in { transform: rotateY(180deg); }
.pcard .f, .pcard .b {
  position: absolute; inset: 0; border-radius: 12px; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  box-shadow: 0 3px 0 rgba(0,0,0,.08);
}
.pcard .f { background: #fff; border: 2px solid var(--line); }
.pcard .f .rk { font-family: 'Baloo 2', sans-serif; font-weight: 800; font-size: clamp(18px, 6vw, 28px); line-height: 1; }
.pcard .f .st { font-size: clamp(16px, 5.5vw, 26px); line-height: 1.1; }
.pcard .f.red { color: #E05A6E; }
.pcard .b {
  transform: rotateY(180deg);
  background: var(--green) repeating-linear-gradient(45deg, transparent 0 8px, rgba(255,255,255,.18) 8px 16px);
  border: 3px solid #fff; outline: 2px solid var(--green-d);
}
.pcard .b::after { content: '🍀'; font-size: clamp(16px, 5vw, 24px); }
.pcard.pick .in { transform: rotateY(180deg) translateY(-6px); }
.pcard.pick .b { background-color: var(--amber); outline-color: var(--amber-d); }
.pcard.pick .b::after { content: '✓'; color: #fff; font-size: 26px; }
.pcard.win .f { border-color: var(--green); background: var(--green-l); }
.pcard.dim { opacity: .35; }
.pcard.slot .f { background: var(--bg2); border: 2px dashed var(--ink3); }
.countdown { font-size: 44px; color: var(--amber); text-align: center; line-height: 1; }
.handname { font-size: 26px; text-align: center; }

/* ---------- stones ---------- */
.stands { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stand {
  --c: var(--green); --cl: var(--green-l);
  position: relative; border-radius: 18px; background: var(--cl); padding: 8px 6px 10px; text-align: center;
  border: 3px solid transparent; min-height: 210px; display: flex; flex-direction: column;
  transition: border-color .2s, transform .2s;
}
.stand.r { --c: #E8615A; --cl: #FDE3E1; }
.stand.g { --c: var(--green); --cl: var(--green-l); }
.stand.b { --c: var(--blue-d); --cl: var(--blue-l); }
.stand .cat { font-size: 26px; line-height: 1; transition: transform .2s; }
.stand.danger .cat { animation: tremble .5s infinite; }
.stand .pile { flex: 1; display: flex; flex-direction: column-reverse; align-items: center; gap: 3px; padding: 6px 0; }
.stone {
  width: 70%; height: 22px; border-radius: 40% 46% 38% 44% / 60% 60% 45% 45%;
  background: #A9A196; color: #fff; font-family: 'Baloo 2'; font-weight: 800; font-size: 14px; line-height: 22px;
  box-shadow: inset 0 -4px 0 rgba(0,0,0,.15);
  animation: drop .35s cubic-bezier(.3, 1.6, .5, 1);
}
.stone.w0 { width: 40%; background: #CFC8BD; }
.stone.w5, .stone.w6 { background: #847B70; width: 86%; }
.stand .total { font-size: 28px; color: var(--c); line-height: 1; }
.stand .total small { font-size: 13px; color: var(--ink2); }
.stand .meter { height: 8px; border-radius: 999px; background: #fff; overflow: hidden; margin: 4px 6px 0; }
.stand .meter i { display: block; height: 100%; background: var(--c); transition: width .3s; }
.stand.pickable { cursor: pointer; border-color: var(--c); animation: pulse 1s infinite; }
.stand.boom { animation: boom .6s; }
.players { display: flex; flex-direction: column; gap: 6px; }
.pl {
  text-align: left;
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 14px; background: #fff; border: 2px solid var(--line);
}
.pl.me { border-color: var(--green); }
.pl.turn { background: var(--amber-l); border-color: var(--amber); }
.pl .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pl .pen { font-size: 20px; color: var(--red); }
.chipdot { display: inline-block; width: 18px; height: 18px; border-radius: 50%; vertical-align: middle; border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.1); }
.chipdot.r { background: #E8615A; } .chipdot.g { background: var(--green); } .chipdot.b { background: var(--blue-d); }
.chipdot.w { background: conic-gradient(#E8615A, #F2C94C, var(--green), var(--blue-d), #B78AE8, #E8615A); }
.hand { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.hand button {
  width: 46px; height: 58px; border-radius: 12px; border: 2px solid var(--line); background: #fff; cursor: pointer;
  font-family: 'Baloo 2'; font-weight: 800; font-size: 26px; box-shadow: 0 4px 0 var(--line);
}
.hand button.on { background: var(--ink); color: #fff; border-color: var(--ink); transform: translateY(-6px); }
.chips { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.chips button {
  display: flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 999px; min-height: 44px;
  border: 2px solid var(--line); background: #fff; cursor: pointer; font-size: 15px;
}
.chips button.on { border-color: var(--ink); box-shadow: 0 0 0 3px var(--amber-l); }
.chips button:disabled { opacity: .35; cursor: not-allowed; }
.log { font-size: 13px; color: var(--ink2); max-height: 150px; overflow-y: auto; line-height: 1.6; padding: 10px 14px; }
.log div:last-child { color: var(--ink); }
.rooms { display: flex; flex-direction: column; gap: 8px; }
.room { display: flex; align-items: center; gap: 10px; padding: 10px 14px; background: #fff; border-radius: 14px; border: 2px solid var(--line); }
.seg { display: flex; background: var(--bg2); border-radius: 14px; padding: 4px; gap: 4px; }
.seg button { flex: 1; border: 0; background: none; border-radius: 10px; padding: 8px 6px; cursor: pointer; font-size: 15px; min-height: 40px; }
.seg button.on { background: #fff; box-shadow: 0 2px 0 var(--line); }
.timerbar { height: 6px; border-radius: 999px; background: var(--bg2); overflow: hidden; }
.timerbar i { display: block; height: 100%; background: var(--amber); }

/* ---------- leaderboard ---------- */
.board { width: 100%; border-collapse: collapse; }
.board td, .board th { padding: 10px 6px; text-align: left; border-bottom: 2px dashed var(--line); font-weight: 400; }
.board th { font-size: 13px; color: var(--ink2); }
.board td.r { width: 42px; text-align: center; font-size: 20px; }
.board td.s { text-align: right; }
.board tr.me td { background: var(--amber-l); }
.board tr.me td:first-child { border-radius: 12px 0 0 12px; }
.board tr.me td:last-child { border-radius: 0 12px 12px 0; }
.empty { text-align: center; padding: 26px 0; color: var(--ink3); }

/* ---------- result ---------- */
.result { text-align: center; }
.result .big { font-size: 60px; color: var(--green-d); line-height: 1; }
.result .emoji { font-size: 64px; animation: bounce 1.6s ease-in-out infinite; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translate(-50%, 140%);
  background: var(--ink); color: #fff; padding: 12px 20px; border-radius: 16px; z-index: 50;
  transition: transform .3s cubic-bezier(.3, 1.4, .5, 1), opacity .2s; max-width: calc(100% - 32px); text-align: center;
  opacity: 0; visibility: hidden; pointer-events: none;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; visibility: visible; }

/* ---------- animations ---------- */
@keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-8px); } }
@keyframes wobble { 0%, 100% { transform: rotate(0); } 25% { transform: rotate(-8deg); } 75% { transform: rotate(8deg); } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25% { transform: translateX(-8px); } 75% { transform: translateX(8px); } }
@keyframes drop { from { transform: translateY(-40px) scale(.6); opacity: 0; } }
@keyframes pulse { 50% { transform: scale(1.03); } }
@keyframes tremble { 0%, 100% { transform: rotate(-6deg); } 50% { transform: rotate(6deg); } }
@keyframes boom { 0% { transform: scale(1); } 30% { transform: scale(1.08) rotate(-2deg); background: #FFD9A8; } 100% { transform: scale(1); } }
@keyframes pop { from { transform: scale(.6); opacity: 0; } }
.pop { animation: pop .35s cubic-bezier(.3, 1.6, .5, 1); }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

@media (max-width: 380px) {
  .games { gap: 10px; }
  .game-tile { min-height: 150px; padding: 14px 10px; }
  .game-tile .t { font-size: 18px; }
  .hand button { width: 40px; height: 52px; }
}
