@font-face {
  font-family: 'WenKai';
  src: url(fonts/wenkai.woff2) format('woff2');
  font-weight: 700;
  font-display: block;
}

:root {
  --bg: #17110c;
  --bg2: #241a12;
  --label: #f5efe6;
  --label2: rgba(245, 239, 230, .6);
  --label3: rgba(245, 239, 230, .32);
  --sep: rgba(255, 255, 255, .12);
  --fill: rgba(120, 120, 128, .24);
  --cell-bg: #1c1c1e;
  --group: #1c1c1e;
  --blue: #0a84ff;
  --green: #30d158;
  --red-team: #e0483a;
  --black-team: #e8e2d6;
  --wood1: #e2b878;
  --wood2: #c99556;
  --line: rgba(78, 44, 14, .55);
  --ivory1: #fbf3e2;
  --ivory2: #e9d8b6;
  --piece-red: #b8231a;
  --piece-black: #1d1a17;
  --back1: #7a4a22;
  --back2: #4e2c12;
  --spring: cubic-bezier(.2, .9, .25, 1.12);
  --ease: cubic-bezier(.25, .8, .25, 1);
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --cell: 80px;
  --bar-h: 64px;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--label);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang TC", "Noto Sans TC", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}
body { position: fixed; inset: 0; background: radial-gradient(120% 70% at 50% 50%, #2c2017 0%, var(--bg) 70%) var(--bg); }
button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; }

#app {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: var(--sat) var(--sar) var(--sab) var(--sal);
}

/* ---------- 玩家資訊列 ---------- */
.bar {
  flex: 0 0 var(--bar-h);
  height: var(--bar-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  position: relative;
  transition: transform .45s var(--ease);
}
#bar1 { order: 0; }
#stage { order: 1; }
#bar0 { order: 2; }
body.facing #bar1 { transform: rotate(180deg); }

.pinfo { display: flex; flex-direction: column; justify-content: center; min-width: 0; flex: 0 0 auto; }
.pname { display: flex; align-items: center; gap: 7px; font-size: 17px; font-weight: 600; letter-spacing: -.2px; }
.dot {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--label3);
  box-shadow: 0 0 0 2px rgba(255,255,255,.08);
  transition: background .3s, box-shadow .3s;
}
.bar[data-side="r"] .dot { background: var(--red-team); }
.bar[data-side="b"] .dot { background: #2a2622; box-shadow: 0 0 0 2px var(--black-team); }
.pstate { font-size: 13px; color: var(--label2); margin-top: 2px; height: 16px; transition: color .25s; white-space: nowrap; }
.bar.active .pstate { color: var(--label); font-weight: 600; }
.bar.active .pname::after {
  content: '';
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 8px var(--green);
  animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: .35; } }

.tray {
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  overflow: hidden;
  padding-right: 2px;
}
.chip {
  --s: calc(var(--bar-h) * .46);
  width: var(--s); height: var(--s);
  flex: 0 0 var(--s);
  border-radius: 50%;
  display: grid; place-items: center;
  font-family: 'WenKai', "Kaiti TC", "STKaiti", serif;
  font-weight: 700;
  font-size: calc(var(--s) * .62);
  line-height: 1;
  background: radial-gradient(circle at 35% 30%, var(--ivory1), var(--ivory2));
  box-shadow: 0 1px 2px rgba(0,0,0,.5), inset 0 0 0 1.5px rgba(120,80,30,.35);
  margin-left: calc(var(--s) * var(--overlap, -.18));
  animation: chipIn .45s var(--spring) both;
}
.chip.r { color: var(--piece-red); }
.chip.b { color: var(--piece-black); }
@keyframes chipIn { from { transform: scale(.2); opacity: 0; } }

.actions { display: flex; gap: 8px; flex: 0 0 auto; }
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  display: grid; place-items: center;
  background: var(--fill);
  color: var(--label);
  transition: transform .2s var(--ease), opacity .2s, background .2s;
}
.icon-btn svg { width: 21px; height: 21px; }
.icon-btn:active, .icon-btn.pressed { transform: scale(.88); background: rgba(120,120,128,.4); }
.icon-btn:disabled { opacity: .3; }
.pill-btn {
  height: 34px; padding: 0 16px; border-radius: 17px;
  background: var(--blue); color: #fff; font-weight: 600; font-size: 15px;
  transition: transform .2s var(--ease), opacity .2s;
  animation: chipIn .35s var(--spring) both;
}
.pill-btn:active { transform: scale(.92); opacity: .8; }

/* ---------- 棋盤 ---------- */
#stage {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  place-items: center;
  position: relative;
}
#board {
  position: relative;
  border-radius: calc(var(--cell) * .14);
  background:
    repeating-linear-gradient(87deg, rgba(120,70,20,.07) 0 2px, transparent 2px 9px),
    repeating-linear-gradient(93deg, rgba(255,240,200,.06) 0 1px, transparent 1px 13px),
    linear-gradient(160deg, var(--wood1), var(--wood2));
  box-shadow:
    0 18px 40px rgba(0,0,0,.55),
    0 2px 0 rgba(255,255,255,.25) inset,
    0 -3px 0 rgba(0,0,0,.18) inset;
  touch-action: none;
}
#grid { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#hints, #pieces { position: absolute; inset: 0; pointer-events: none; }

.hint {
  position: absolute;
  left: 0; top: 0;
  width: var(--cell); height: var(--cell);
  display: grid; place-items: center;
  animation: hintIn .25s var(--ease) both;
}
.hint::before { content: ''; border-radius: 50%; }
.hint.move::before {
  width: 26%; height: 26%;
  background: rgba(30, 90, 40, .55);
  box-shadow: 0 0 0 3px rgba(255,255,255,.25);
}
.hint.cap::before, .hint.dark::before {
  width: 94%; height: 94%;
  border: 4px solid rgba(214, 40, 30, .9);
  box-shadow: 0 0 14px rgba(255, 60, 40, .55);
  animation: ring 1.1s ease-in-out infinite;
}
.hint.dark::before { border-style: dashed; border-color: rgba(255, 170, 30, .95); box-shadow: 0 0 14px rgba(255, 170, 30, .5); }
.hint.last::before {
  width: 88%; height: 88%;
  border-radius: 22%;
  background: rgba(255, 245, 200, .28);
  box-shadow: inset 0 0 0 2px rgba(255, 250, 220, .35);
  animation: none;
}
.hint.from::before { width: 30%; height: 30%; border: 2px dashed rgba(80, 40, 10, .45); background: none; box-shadow: none; animation: none; }
@keyframes hintIn { from { opacity: 0; transform: var(--t) scale(.6); } }
@keyframes ring { 50% { transform: scale(.94); opacity: .75; } }

/* ---------- 棋子 ---------- */
.piece {
  position: absolute;
  left: 0; top: 0;
  width: var(--cell); height: var(--cell);
  display: grid; place-items: center;
  z-index: 1;
  will-change: transform;
  transition: transform .32s var(--ease);
}
.piece.moving { z-index: 5; }
.piece.dragging { z-index: 10; transition: none; }
.piece.dying { z-index: 0; }
.piece .lift {
  width: 86%; height: 86%;
  position: relative;
  transition: transform .28s var(--spring), filter .28s;
  filter: drop-shadow(0 3px 2px rgba(60, 30, 5, .45));
}
.piece.sel .lift { transform: scale(1.1) translateY(-4%); filter: drop-shadow(0 12px 10px rgba(40, 20, 0, .45)); }
.piece.dragging .lift { transform: scale(1.22); filter: drop-shadow(0 20px 16px rgba(30, 15, 0, .45)); transition-duration: .18s; }
.piece.dying .lift { transform: scale(.2); opacity: 0; transition: transform .35s var(--ease) .12s, opacity .3s .15s; }
.piece.shake .lift { animation: shake .35s; }
@keyframes shake { 20%, 60% { transform: translateX(-6%); } 40%, 80% { transform: translateX(6%); } }

.flipper { position: absolute; inset: 0; }
.piece .front { visibility: hidden; }
.piece.up .front { visibility: visible; }
.piece.up .back { visibility: hidden; }
.piece.flipping .lift { animation: flipLift .5s var(--ease); }
.piece.flipping .flipper { animation: flipX .5s var(--ease); }
.piece.flipping .back { visibility: visible; animation: backOut .5s linear both; }
@keyframes flipLift { 45% { transform: scale(1.18) translateY(-6%); } }
@keyframes flipX { 0% { transform: scaleX(1); } 48% { transform: scaleX(.02); } 100% { transform: scaleX(1); } }
@keyframes backOut { 0%, 48% { opacity: 1; } 48.1%, 100% { opacity: 0; } }

.face {
  position: absolute; inset: 0;
  border-radius: 50%;
  display: grid; place-items: center;
}
.front {
  background:
    radial-gradient(circle at 34% 28%, #fffaf0 0%, var(--ivory1) 30%, var(--ivory2) 100%);
  box-shadow:
    inset 0 -3px 0 rgba(150, 105, 50, .45),
    inset 0 2px 1px rgba(255, 255, 255, .9);
}
.front::before {
  content: '';
  position: absolute; inset: 9%;
  border-radius: 50%;
  border: max(1.5px, calc(var(--cell) * .018)) solid currentColor;
  opacity: .55;
}
.front span {
  font-family: 'WenKai', "Kaiti TC", "STKaiti", "BiauKai", serif;
  font-weight: 700;
  font-size: calc(var(--cell) * .47);
  line-height: 1;
  transform: translateY(-2%);
  transition: transform .45s var(--ease);
}
.piece.r .front { color: var(--piece-red); }
.piece.b .front { color: var(--piece-black); }
.piece.rot .front span, .piece.rot .front::before { transform: rotate(180deg) translateY(-2%); }
.back {
  background:
    radial-gradient(circle at 50% 50%, transparent 0 26%, rgba(255, 220, 160, .25) 27% 29%, transparent 30%),
    radial-gradient(circle at 34% 28%, #a5683a, var(--back1) 45%, var(--back2));
  box-shadow:
    inset 0 -3px 0 rgba(0, 0, 0, .35),
    inset 0 2px 1px rgba(255, 220, 170, .4);
}
.back::after {
  content: '';
  position: absolute; inset: 9%;
  border-radius: 50%;
  border: max(1.5px, calc(var(--cell) * .018)) solid rgba(255, 220, 160, .35);
}

/* ---------- HUD ---------- */
#hud {
  position: fixed;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%) scale(.85);
  padding: 12px 22px;
  border-radius: 16px;
  background: rgba(30, 30, 32, .78);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  font-size: 17px; font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s, transform .35s var(--spring);
  z-index: 50;
  white-space: nowrap;
}
#hud.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }
#hud.rot { transform: translate(-50%, -50%) rotate(180deg) scale(.85); }
#hud.rot.show { transform: translate(-50%, -50%) rotate(180deg) scale(1); }

/* ---------- Sheet ---------- */
.sheet-wrap { position: fixed; inset: 0; z-index: 100; }
.sheet-wrap[hidden] { display: none; }
.backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .4); opacity: 0; transition: opacity .35s; }
.sheet-wrap.open .backdrop { opacity: 1; }
.sheet {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  margin: 0 auto;
  max-width: 560px;
  max-height: calc(100% - var(--sat) - 12px);
  display: flex; flex-direction: column;
  background: #000;
  border-radius: 12px 12px 0 0;
  transform: translateY(100%);
  transition: transform .45s cubic-bezier(.32, .72, 0, 1);
  box-shadow: 0 -10px 40px rgba(0, 0, 0, .4);
}
.sheet-wrap.open .sheet { transform: translateY(0); }
.sheet.dragging { transition: none; }
@media (min-width: 700px) and (min-height: 700px) {
  .sheet { bottom: auto; top: 50%; border-radius: 14px; max-height: min(720px, calc(100% - 80px)); transform: translateY(calc(50vh + 50%)); }
  .sheet-wrap.open .sheet { transform: translateY(-50%); }
}
.sheet-head {
  position: relative;
  flex: 0 0 auto;
  height: 56px;
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 16px 0;
  touch-action: none;
}
.grabber { position: absolute; top: 6px; left: 50%; width: 36px; height: 5px; margin-left: -18px; border-radius: 3px; background: rgba(255, 255, 255, .25); }
.sheet-head h2 { margin: 0; font-size: 17px; font-weight: 600; position: absolute; left: 50%; transform: translateX(-50%); }
.nav-btn { color: var(--blue); font-size: 17px; min-width: 60px; }
.nav-btn.left { text-align: left; }
.nav-btn:active { opacity: .4; }
.sheet-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 0 16px calc(24px + var(--sab));
  touch-action: pan-y;
}
.group-title { font-size: 13px; text-transform: uppercase; color: var(--label2); padding: 22px 16px 7px; }
.group { background: var(--group); border-radius: 10px; overflow: hidden; }
.group-foot { font-size: 13px; color: var(--label2); padding: 7px 16px 0; line-height: 1.4; }
.group-foot.center { text-align: center; padding-top: 22px; }
.row {
  min-height: 48px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 16px;
  position: relative;
  width: 100%;
  text-align: left;
}
.row + .row::before { content: ''; position: absolute; top: 0; left: 16px; right: 0; height: .5px; background: var(--sep); }
.row-text { display: flex; flex-direction: column; }
.row-text b { font-weight: 400; font-size: 17px; }
.row-text small { font-size: 13px; color: var(--label2); margin-top: 2px; }
.row.action { color: var(--blue); font-size: 17px; justify-content: center; transition: background .15s; }
.row.action:active { background: rgba(255, 255, 255, .08); }
.rank-row { justify-content: center; }
.rank-list { font-family: 'WenKai', "Kaiti TC", serif; font-size: 20px; letter-spacing: 1px; }
#rowDarkFail { transition: opacity .25s; }
#rowDarkFail.off { opacity: .35; pointer-events: none; }

.ios-switch {
  -webkit-appearance: none; appearance: none;
  flex: 0 0 auto;
  width: 51px; height: 31px; margin: 0;
  border-radius: 16px;
  background: rgba(120, 120, 128, .32);
  position: relative;
  transition: background .25s;
  cursor: pointer;
}
.ios-switch::after {
  content: '';
  position: absolute; top: 2px; left: 2px;
  width: 27px; height: 27px; border-radius: 50%;
  background: #fff;
  box-shadow: 0 3px 8px rgba(0, 0, 0, .15), 0 3px 1px rgba(0, 0, 0, .06);
  transition: transform .3s var(--spring), width .2s;
}
.ios-switch:checked { background: var(--green); }
.ios-switch:checked::after { transform: translateX(20px); }
.ios-switch:active::after { width: 33px; }
.ios-switch:checked:active::after { transform: translateX(14px); }

.segmented {
  display: flex;
  background: rgba(118, 118, 128, .24);
  border-radius: 9px;
  padding: 2px;
  flex: 0 0 auto;
}
.segmented button {
  font-size: 13px; font-weight: 500;
  padding: 6px 12px;
  border-radius: 7px;
  min-width: 44px;
  transition: background .25s, box-shadow .25s;
}
.segmented button.on { background: #636366; box-shadow: 0 3px 8px rgba(0, 0, 0, .12); font-weight: 600; }

/* ---------- Alert ---------- */
.alert-wrap {
  position: fixed; inset: 0; z-index: 200;
  display: grid; place-items: center;
  background: rgba(0, 0, 0, .35);
  opacity: 0; transition: opacity .2s;
}
.alert-wrap[hidden] { display: none; }
.alert-wrap.open { opacity: 1; }
.alert {
  width: 270px;
  border-radius: 14px;
  overflow: hidden;
  background: rgba(37, 37, 37, .82);
  -webkit-backdrop-filter: blur(30px) saturate(180%);
  backdrop-filter: blur(30px) saturate(180%);
  transform: scale(1.15);
  transition: transform .3s var(--spring);
  text-align: center;
}
.alert-wrap.open .alert { transform: scale(1); }
.alert-wrap.rot .alert { transform: rotate(180deg) scale(1.15); }
.alert-wrap.rot.open .alert { transform: rotate(180deg) scale(1); }
.alert-body { padding: 19px 16px 18px; }
.alert h3 { margin: 0; font-size: 17px; font-weight: 600; }
.alert p { margin: 4px 0 0; font-size: 13px; line-height: 1.4; }
.alert-btns { display: flex; border-top: .5px solid rgba(255, 255, 255, .15); }
.alert-btns.stack { flex-direction: column; }
.alert-btns button {
  flex: 1; height: 44px;
  font-size: 17px; color: var(--blue);
  transition: background .1s;
}
.alert-btns button + button { border-left: .5px solid rgba(255, 255, 255, .15); }
.alert-btns.stack button + button { border-left: 0; border-top: .5px solid rgba(255, 255, 255, .15); }
.alert-btns button.bold { font-weight: 600; }
.alert-btns button.danger { color: #ff453a; }
.alert-btns button:active { background: rgba(255, 255, 255, .1); }

.no-anim .piece, .no-anim .hint { transition: none !important; animation: none !important; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
