:root {
  --felt: #0f5132;
  --felt-2: #146c43;
  --ink: #1d2330;
  --muted: #667085;
  --bg: #eef2ef;
  --card: #fff;
  --line: #e3e7e4;
  --red: #c0262d;
  --green: #12813d;
  --blue: #1f4e9c;
  --gold: #b7791f;
  --tile: #fffdf6;
  --tile-edge: #d9cfb4;
  --radius: 14px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang HK", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); padding-bottom: env(safe-area-inset-bottom); }
button { font: inherit; color: inherit; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: linear-gradient(135deg, var(--felt), var(--felt-2));
  color: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.15);
}
.topbar h1 { font-size: 19px; margin: 0; display: flex; align-items: center; gap: 8px; }
.topbar h1 small { font-weight: 400; opacity: .75; font-size: 13px; }
.glyph { background: #fff; color: var(--red); border-radius: 6px; padding: 0 6px; font-size: 18px; }
.icon-btn { background: transparent; border: 0; font-size: 22px; padding: 4px 8px; cursor: pointer; color: inherit; }

main { max-width: 640px; margin: 0 auto; padding: 12px; display: grid; gap: 12px; }
.card { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: 0 1px 3px rgba(16,24,40,.08); }
.card h2 { font-size: 17px; margin: 0 0 12px; display: flex; align-items: center; gap: 8px; }
.step { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--felt); color: #fff; font-size: 13px; }
.hint { color: var(--muted); font-size: 14px; margin: -4px 0 12px; }
.cn { color: var(--muted); font-weight: 400; }

.field { margin-bottom: 14px; }
.field > label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button {
  flex: 1 1 0; min-width: 64px; min-height: 46px; padding: 6px 8px;
  border: 1.5px solid var(--line); background: #f8faf9; border-radius: 10px; cursor: pointer; font-weight: 600; font-size: 15px;
}
.seg button .cn { display: block; font-size: 13px; }
.seg button.on { background: var(--felt); border-color: var(--felt); color: #fff; }
.seg button.on .cn { color: #d1fadf; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 46px; padding: 0 16px; border-radius: 12px; border: 1.5px solid var(--line);
  background: #fff; font-weight: 600; cursor: pointer; text-align: center;
}
.btn.big { min-height: 58px; font-size: 17px; flex: 1; }
.btn.primary { background: var(--felt); border-color: var(--felt); color: #fff; }
.btn.danger { color: var(--red); border-color: #f4c7c9; }
.photo-actions { display: flex; gap: 10px; }
.link-btn { background: none; border: 0; color: var(--felt-2); text-decoration: underline; padding: 10px 0 0; cursor: pointer; font-size: 14px; }
.small-btn { border: 1.5px solid var(--line); background: #fff; border-radius: 8px; padding: 4px 10px; font-size: 13px; font-weight: 600; cursor: pointer; }
.small-btn.ghost { color: var(--muted); }

.preview { position: relative; margin-top: 12px; border-radius: 12px; overflow: hidden; background: #000; }
.preview img { display: block; width: 100%; max-height: 50vh; object-fit: contain; }
.scan-overlay { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 10px; background: rgba(0,0,0,.55); color: #fff; font-weight: 600; }
.spinner { width: 38px; height: 38px; border: 4px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.status { margin: 10px 0 0; padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.status.ok { background: #ecfdf3; color: #05603a; }
.status.warn { background: #fffaeb; color: #93370d; }
.status.err { background: #fef3f2; color: #b42318; }

.group { margin-bottom: 16px; }
.group-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.group-head h3 { font-size: 15px; margin: 0; }
.count { font-size: 13px; font-weight: 700; color: var(--green); background: #ecfdf3; padding: 2px 8px; border-radius: 99px; }
.count.bad { color: #b42318; background: #fef3f2; }
.tile-row { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.empty { color: var(--muted); font-size: 14px; margin: 0; }

/* tiles */
.tile {
  --w: 40px;
  width: var(--w); height: calc(var(--w) * 1.36);
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1;
  background: var(--tile); border: 1px solid var(--tile-edge); border-radius: 6px;
  box-shadow: 0 2px 0 #c9bb93, 0 3px 4px rgba(0,0,0,.12);
  padding: 0; cursor: default; user-select: none; font-family: "PingFang HK", "Noto Serif TC", "Songti TC", serif;
}
button.tile { cursor: pointer; }
button.tile:active { transform: translateY(1px); }
.tile.small { --w: 34px; }
.tile b { font-size: calc(var(--w) * .46); font-weight: 700; }
.tile i { font-style: normal; font-size: calc(var(--w) * .32); margin-top: 2px; }
.k-m b { color: var(--ink); } .k-m i { color: var(--red); }
.k-p b { color: var(--blue); } .k-p i { color: var(--blue); }
.k-s b { color: var(--green); } .k-s i { color: var(--green); }
.k-z b { font-size: calc(var(--w) * .58); color: var(--ink); }
.t-RD b { color: var(--red); } .t-GD b { color: var(--green); }
.t-WD .frame { width: 58%; height: 62%; border: 3px solid var(--blue); border-radius: 3px; }
.k-b { background: #fff7f0; }
.k-b b { color: #9a3412; font-size: calc(var(--w) * .5); }
.k-b i { color: var(--blue); font-weight: 700; }
.tile.add { background: #f3f6f4; border: 1.5px dashed #a3b1a8; box-shadow: none; color: var(--muted); font-size: 22px; font-family: inherit; }

.meld { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; padding: 8px; border: 1px solid var(--line); border-radius: 10px; margin-bottom: 6px; }
.meld .tile-row { flex: 0 0 auto; }
.meld-tag { font-size: 13px; color: var(--muted); margin-right: auto; }

.extras summary, .decomp summary { cursor: pointer; font-weight: 600; font-size: 15px; padding: 6px 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding-top: 8px; }
.chip { border: 1.5px solid var(--line); background: #f8faf9; border-radius: 99px; padding: 8px 12px; cursor: pointer; font-weight: 600; }
.chip small { font-weight: 400; color: var(--muted); }
.chip.on { background: var(--gold); border-color: var(--gold); color: #fff; }
.chip.on small { color: #fff4d6; }

/* result */
.result { border: 2px solid var(--felt); }
.result.invalid { border-color: #f4c7c9; }
.result-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.fan-total { display: flex; align-items: baseline; gap: 4px; }
#fanTotal { font-size: 64px; font-weight: 800; color: var(--felt); line-height: 1; }
.result.invalid #fanTotal { color: #b42318; }
.fan-unit { font-size: 28px; font-weight: 700; color: var(--red); }
.badge { font-size: 13px; font-weight: 700; padding: 6px 10px; border-radius: 99px; background: #ecfdf3; color: #05603a; }
.badge.bad { background: #fef3f2; color: #b42318; }
.badge.gold { background: #fef0c7; color: #93370d; }
.fan-items { list-style: none; margin: 14px 0 0; padding: 0; }
.fan-items li { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
.it-cn { font-weight: 700; font-size: 17px; min-width: 56px; }
.it-en { color: var(--muted); font-size: 14px; }
.it-en em { font-style: normal; font-size: 12px; }
.it-fan { font-weight: 800; font-size: 18px; color: var(--felt); }
.fan-items li.cap .it-fan { color: var(--gold); }
.msg { margin: 10px 0 0; padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.msg.err { background: #fef3f2; color: #b42318; }
.msg.warn { background: #fffaeb; color: #93370d; }
.decomp { margin-top: 12px; }
.decomp-row { display: flex; align-items: center; gap: 10px; margin: 6px 0; font-size: 13px; color: var(--muted); }

/* dialogs */
dialog { border: 0; padding: 0; border-radius: 18px 18px 0 0; width: 100%; max-width: 640px; margin: auto auto 0; max-height: 85vh; }
dialog::backdrop { background: rgba(16,24,40,.5); }
@media (min-width: 700px) { dialog { border-radius: 18px; margin: auto; } }
.sheet { padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); }
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.sheet-head h3 { margin: 0; }
.picker-grid { display: grid; grid-template-columns: repeat(9, 1fr); gap: 6px; justify-items: center; }
.picker-grid .tile { --w: min(36px, 9vw); }
.sheet-actions { display: flex; justify-content: flex-end; margin-top: 14px; }
.sheet-actions [hidden] { display: none; }

.foot { text-align: center; color: var(--muted); font-size: 12px; padding: 8px 16px 24px; }
.hint.sub { margin: 8px 0 0; font-size: 13px; }
.doc-preview { min-height: 140px; display: grid; place-items: center; color: #fff; font-weight: 600; padding: 20px; text-align: center; word-break: break-all; }
