:root {
  --felt: #1d4a3a;
  --felt-dark: #143529;
  --panel: #10251d;
  --ink: #eef4ef;
  --muted: #9fb5a8;
  --accent: #ffcc33;
  --me: #7cc4ff;
  --ai: #ff8f7a;
  --card-w: 80px;
  --slot-w: 82px;
  --active-w: 108px;
  color-scheme: dark;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--felt-dark);
  color: var(--ink);
  font: 14px/1.45 -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}
button {
  font: inherit;
  color: var(--ink);
  background: #2d5a49;
  border: 1px solid #4b7d69;
  border-radius: 6px;
  padding: 5px 12px;
  cursor: pointer;
}
button:hover { background: #386c58; }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: #d9a520; border-color: #f0c040; color: #1c1300; font-weight: 600; }
button.primary:hover { background: #ebb72a; }
button.danger { background: #8a2c22; border-color: #c4483a; }
button.danger:hover { background: #a3352a; }
button.link { background: none; border: none; padding: 0; color: var(--accent); text-decoration: underline; }
select { font: inherit; padding: 3px 6px; border-radius: 6px; }
.muted { color: var(--muted); }
.hidden { display: none !important; }

.topbar {
  display: flex; flex-wrap: wrap; gap: 8px 20px; align-items: center;
  padding: 8px 16px; background: var(--panel); border-bottom: 1px solid #2a4a3c;
}
.topbar h1 { font-size: 18px; margin: 0; }
.controls { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; }
.controls label { display: flex; gap: 6px; align-items: center; }

.layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; height: calc(100vh - 52px); }
.board { overflow: auto; padding: 4px 14px 0; background: radial-gradient(ellipse at center, var(--felt), var(--felt-dark)); }
.welcome { margin: 80px auto; text-align: center; color: var(--muted); font-size: 16px; }

.side-area { display: flex; flex-direction: column; gap: 6px; padding: 6px 0; }
.row { display: flex; gap: 8px; justify-content: center; align-items: flex-end; min-height: 20px; }
.hand-row { flex-wrap: wrap; }
.hand-row.mine {
  position: sticky; bottom: 0; z-index: 5; padding: 8px; border-radius: 10px 10px 0 0;
  background: rgba(10, 30, 22, .94); box-shadow: 0 -4px 14px rgba(0,0,0,.4); min-height: calc(var(--card-w) * 1.4 + 16px);
}
.opp .hand-row .card { --card-w: 46px; }
.opp .hand-row .card:not(.back) { --card-w: 64px; }

.meta { display: flex; flex-wrap: wrap; gap: 4px 14px; align-items: center; justify-content: center; font-size: 13px; color: var(--muted); }
.meta b { color: var(--ink); font-size: 15px; }
.me .meta b { color: var(--me); }
.opp .meta b { color: var(--ai); }
.deckname { color: var(--ink); }
.prizes { display: inline-flex; gap: 2px; align-items: center; }
.prizes em { font-style: normal; margin-left: 4px; }
.prize { display: inline-block; width: 10px; height: 14px; border-radius: 2px; background: linear-gradient(135deg, #2b62c8, #183c80); border: 1px solid #8fb0f0; }

.card {
  width: var(--card-w); aspect-ratio: 680 / 949; border-radius: 5px; overflow: hidden; position: relative;
  box-shadow: 0 2px 4px rgba(0,0,0,.4); flex: none; transition: transform .1s;
}
.card img, .slot img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card.back { background: repeating-linear-gradient(45deg, #2b62c8 0 6px, #2152b0 6px 12px); border: 2px solid #9db8f0; }
.card.hand:hover { transform: translateY(-6px); }
.can { outline: 3px solid var(--accent); outline-offset: 1px; cursor: pointer; box-shadow: 0 0 12px rgba(255,204,51,.6); }
.target { outline: 2px dashed rgba(255,204,51,.5); outline-offset: 2px; }

.slot {
  width: var(--slot-w); aspect-ratio: 680 / 949; border-radius: 6px; position: relative; flex: none;
  box-shadow: 0 2px 6px rgba(0,0,0,.5); background: rgba(0,0,0,.15);
}
.slot img { border-radius: 6px; }
.slot.active { width: var(--active-w); }
.slot.empty { border: 2px dashed rgba(255,255,255,.18); box-shadow: none; }
.slot .hp {
  position: absolute; top: 3px; right: 3px; background: rgba(0,0,0,.78); padding: 0 5px; border-radius: 8px;
  font-weight: 700; font-size: 13px;
}
.slot .hp small { font-weight: 400; color: var(--muted); font-size: 10px; }
.slot .hp.hurt { color: #ffb3a3; }
.slot .dmg {
  position: absolute; top: 34%; left: 50%; transform: translateX(-50%); background: #c8321e; color: #fff; font-weight: 800;
  padding: 1px 8px; border-radius: 12px; border: 2px solid #fff; font-size: 14px;
}
.slot .pips { position: absolute; bottom: 3px; left: 3px; right: 3px; display: flex; flex-wrap: wrap; gap: 2px; }
.slot .tooltag, .slot .prizetag {
  position: absolute; left: 3px; background: rgba(40,40,80,.9); font-size: 11px; padding: 0 5px; border-radius: 4px;
}
.slot .tooltag { top: 22px; }
.slot .prizetag { top: 3px; background: rgba(150,30,90,.9); }
.slot .statustag { position: absolute; left: 3px; right: 3px; bottom: 26px; background: rgba(120,40,160,.92); font-size: 11px; padding: 0 5px; border-radius: 4px; text-align: center; }
.bench-row { flex-wrap: wrap; }
.pip {
  display: inline-flex; width: 18px; height: 18px; border-radius: 50%; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700; color: #fff; border: 1px solid rgba(255,255,255,.7); vertical-align: middle;
}
.pip.fire { background: #e0532c; } .pip.water { background: #2f86dc; } .pip.lightning { background: #e9b81c; color: #222; }
.pip.grass { background: #3e9b3e; } .pip.psychic { background: #a24fb5; } .pip.fighting { background: #b36a2c; }
.pip.darkness { background: #2f3b48; } .pip.metal { background: #8792a0; } .pip.dragon { background: #b29233; }
.pip.colorless { background: #d6d6d6; color: #333; }

.mid {
  display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; justify-content: center;
  margin: 4px 0; padding: 8px; background: rgba(0,0,0,.25); border-radius: 10px;
}
.mid .turn { color: var(--muted); }
.mid .status { font-weight: 700; color: var(--accent); }
.mid .notes { color: var(--muted); font-size: 12px; }

.side { display: flex; flex-direction: column; background: var(--panel); border-left: 1px solid #2a4a3c; min-height: 0; }
.zoom { padding: 10px; border-bottom: 1px solid #2a4a3c; max-height: 62%; overflow: auto; }
.zoom > img { width: 100%; max-width: 300px; display: block; margin: 0 auto 8px; border-radius: 10px; }
.zoom .hint { color: var(--muted); }
.zinfo { background: rgba(255,255,255,.06); padding: 6px 8px; border-radius: 6px; margin-bottom: 6px; }
.ztext p { margin: 6px 0; }
.ztext .ab { background: #c0392b; padding: 0 6px; border-radius: 4px; font-size: 12px; }
.log { flex: 1; overflow: auto; padding: 8px 10px; font-size: 13px; min-height: 120px; }
.log .l { padding: 1px 0; }
.log .who0 b { color: var(--me); }
.log .who1 b { color: var(--ai); }
.log .turn { margin-top: 8px; padding: 2px 6px; background: rgba(255,255,255,.08); border-radius: 4px; font-weight: 700; }
.log .attack { color: #ffe08a; }
.log .ko { color: #ff9c8a; font-weight: 700; }

.menu {
  position: fixed; z-index: 20; background: #0d1f18; border: 1px solid #4b7d69; border-radius: 8px; padding: 6px;
  display: flex; flex-direction: column; gap: 4px; min-width: 180px; box-shadow: 0 6px 20px rgba(0,0,0,.6);
}
.menu .mtitle { font-weight: 700; padding: 2px 4px 4px; color: var(--accent); }
.menu button { text-align: left; }
.menu button.attack { background: #7a2c1c; border-color: #b4513a; }

.modal {
  position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center; padding: 16px;
}
.modal .box {
  background: var(--panel); border: 1px solid #4b7d69; border-radius: 12px; padding: 16px 18px;
  max-width: min(900px, 100%); max-height: 90vh; overflow: auto; box-shadow: 0 10px 40px rgba(0,0,0,.7);
}
.modal h2 { margin: 0 0 6px; font-size: 18px; }
.opts { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 12px; }
.opts button { padding: 8px 18px; }
.pick { display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0; }
.pcard { width: 110px; cursor: pointer; text-align: center; font-size: 12px; border-radius: 8px; padding: 4px; border: 2px solid transparent; }
.pcard img { width: 100%; border-radius: 6px; display: block; margin-bottom: 3px; }
.pcard.on { border-color: var(--accent); background: rgba(255,204,51,.15); }
.foot { display: flex; gap: 12px; align-items: center; justify-content: flex-end; margin-top: 8px; }

/* 牌組編輯 */
.builder { padding: 8px 0 16px; }
.bhead { display: flex; flex-wrap: wrap; gap: 8px 10px; align-items: center; }
.bhead input { font: inherit; font-size: 16px; font-weight: 700; padding: 5px 8px; border-radius: 6px; border: 1px solid #4b7d69; background: #0d1f18; color: var(--ink); min-width: 240px; flex: 1; }
.btotal { font-weight: 800; font-size: 18px; padding: 2px 10px; border-radius: 6px; }
.btotal.ok { background: #2c7a4b; }
.btotal.bad { background: #8a3324; }
.bsub { display: flex; flex-wrap: wrap; gap: 6px 18px; align-items: center; margin: 8px 0 4px; color: var(--muted); }
.bwarn { color: var(--accent); margin: 4px 0; }
.bmsg { min-height: 1.4em; color: #9fe0b5; opacity: 0; transition: opacity .2s; }
.bmsg.show { opacity: 1; }
.bcols { display: grid; grid-template-columns: minmax(240px, 320px) minmax(0, 1fr); gap: 14px; align-items: start; }
.bdeck { background: rgba(0,0,0,.22); border-radius: 10px; padding: 6px 10px 10px; }
.bdeck h4 { margin: 10px 0 4px; font-size: 13px; color: var(--accent); }
.brow { display: flex; align-items: center; gap: 6px; padding: 2px 0; }
.brow img { width: 28px; border-radius: 3px; }
.brow .bname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brow b { width: 20px; text-align: center; }
.step { padding: 0 8px; line-height: 22px; }
.filters { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 8px; }
.chip { padding: 3px 10px; border-radius: 14px; }
.chip.on { background: #d9a520; color: #1c1300; border-color: #f0c040; }
.bgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; }
.bpcard { position: relative; cursor: pointer; text-align: center; font-size: 12px; }
.bpcard img { width: 100%; border-radius: 6px; display: block; box-shadow: 0 2px 4px rgba(0,0,0,.4); }
.bpcard:hover img { outline: 2px solid var(--accent); }
.bpcard.in img { outline: 2px solid #5fbf85; }
.bpcard .badge { position: absolute; top: 4px; right: 4px; background: #2c7a4b; border: 2px solid #fff; border-radius: 12px; padding: 0 7px; font-weight: 800; }
.bpname { display: block; margin-top: 2px; }

@media (max-width: 1000px) {
  .bcols { grid-template-columns: 1fr; }
  .layout { grid-template-columns: 1fr; height: auto; }
  .side { border-left: none; border-top: 1px solid #2a4a3c; }
  .zoom { max-height: none; }
  .log { max-height: 300px; }
  :root { --card-w: 62px; --slot-w: 64px; --active-w: 88px; }
}

.mid .stadium { padding: 2px 10px; border: 1px solid var(--accent); border-radius: 999px; color: var(--accent); cursor: default; }

/* 自訂牌組同步狀態、匯出／匯入 */
.sync { font-size: 12px; color: var(--muted); white-space: nowrap; }
.sync.synced { color: #9fe0b5; }
.sync.offline, .sync.error { color: var(--accent); }
button.on { background: #386c58; border-color: var(--accent); }
.btransfer { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 12px; margin: 8px 0; padding: 10px 12px; background: rgba(0,0,0,.22); border-radius: 10px; }
.btransfer section { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.btransfer h4 { margin: 0; font-size: 13px; color: var(--accent); }
.btransfer p { margin: 0; }
.btransfer textarea { font: 12px/1.4 ui-monospace, Menlo, monospace; width: 100%; padding: 6px 8px; border-radius: 6px; border: 1px solid #4b7d69; background: #0d1f18; color: var(--ink); resize: vertical; }
.brow2 { display: flex; flex-wrap: wrap; gap: 8px; }
.filebtn { display: inline-block; background: #2d5a49; border: 1px solid #4b7d69; border-radius: 6px; padding: 5px 12px; cursor: pointer; }
.filebtn:hover { background: #386c58; }
.filebtn:focus-within, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* 牌組編輯器的卡池搜尋、分頁 */
.bsearch { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.bsearch input { flex: 1 1 220px; min-width: 0; font: inherit; padding: 5px 8px; border-radius: 6px; border: 1px solid #4b7d69; background: #0d1f18; color: var(--ink); }
.bsearch input:focus-visible, .bsearch select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.pcount { margin: 0 0 6px; font-size: 12px; }
.pager { display: flex; gap: 12px; align-items: center; justify-content: center; margin-top: 12px; font-variant-numeric: tabular-nums; }

/* 帳號：登入按鈕、登入視窗、提示列 */
.account { display: flex; gap: 8px; align-items: center; font-size: 13px; }
.account .who { color: var(--muted); max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#auth { z-index: 40; }
.authbox { width: min(420px, 100%); display: flex; flex-direction: column; gap: 8px; }
.authbox p { margin: 0; }
.field { display: flex; flex-direction: column; gap: 4px; font-weight: 600; }
.field input { font: inherit; font-size: 16px; padding: 8px 10px; border-radius: 6px; border: 1px solid #4b7d69; background: #0d1f18; color: var(--ink); }
#authCode { letter-spacing: 6px; font-variant-numeric: tabular-nums; font-size: 22px; }
.authmsg { min-height: 1.3em; color: var(--ai); }
.authbox .foot { flex-wrap: wrap; }
.alertbar { position: fixed; left: 50%; top: calc(12px + env(safe-area-inset-top, 0px)); transform: translate(-50%, -150%); z-index: 50; background: #2c7a4b; color: #fff; padding: 8px 16px; border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,.4); transition: transform .25s; max-width: calc(100vw - 32px); }
.alertbar.show { transform: translate(-50%, 0); }
@media (prefers-reduced-motion: reduce) { .alertbar { transition: none; } }

/* 勝率模擬 */
.sim { padding: 8px 0 24px; max-width: 900px; display: flex; flex-direction: column; gap: 10px; }
.sim h2 { margin: 0; font-size: 20px; }
.sim h3 { margin: 10px 0 0; font-size: 15px; color: var(--accent); }
.sim p { margin: 0; }
.simtop { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.simform { display: flex; flex-wrap: wrap; gap: 10px 12px; align-items: flex-end; }
.simform label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); min-width: 0; }
.simform select { max-width: min(320px, 100%); }
.simprog { display: flex; gap: 10px; align-items: center; font-variant-numeric: tabular-nums; }
.simprog progress { flex: 1; height: 10px; accent-color: var(--accent); }
.simres { background: rgba(0,0,0,.22); border-radius: 10px; padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; }
.simhead { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.simhead.right { justify-content: flex-end; }
.simA { font-weight: 700; }
.simpct { font-size: 28px; color: var(--me); font-variant-numeric: tabular-nums; }
.simhead.right b { color: var(--ai); font-variant-numeric: tabular-nums; }
.simbar { display: flex; height: 14px; border-radius: 7px; overflow: hidden; background: #0d1f18; }
.simbar .sa { background: var(--me); }
.simbar .sd { background: var(--muted); }
.simbar .sb { background: var(--ai); }
.simstats { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 4px 14px; margin: 4px 0 0; font-variant-numeric: tabular-nums; }
.simstats dt { color: var(--muted); }
.simstats dd { margin: 0; }
.simtable { overflow-x: auto; }
.simtable table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
.simtable th, .simtable td { text-align: left; padding: 6px 10px 6px 0; border-bottom: 1px solid #2a4a3c; }
.simtable th { color: var(--muted); font-weight: 600; font-size: 13px; }
