:root {
  --sky1: #bfe6ff; --sky2: #fff4dc;
  --brick: #d9b99b; --brick-dark: #c49a78; --mortar: #e9d6c2;
  --cell-bg: #f6ead9; --cell-floor: #e8d3b8; --bar: rgba(72, 84, 104, .88);
  --ink: #2d3436; --muted: #6b7280; --accent: #ff7a45; --good: #10b981; --life: #4b3b6b;
  --card: #fffdf8; --shadow: 0 6px 18px rgba(60, 40, 20, .15);
  --radius: 18px;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(180deg, var(--sky1) 0%, var(--sky2) 55%, #f3e3c9 100%) fixed;
  min-height: 100vh;
}
.demo-banner { background: #ffe58f; text-align: center; padding: 8px; font-weight: 700; }

/* ---------- header ---------- */
.top { display: flex; flex-wrap: wrap; gap: 16px; align-items: center; justify-content: space-between; padding: 22px 28px 8px; }
.brand { display: flex; gap: 14px; align-items: center; }
.logo { font-size: 52px; filter: drop-shadow(0 4px 4px rgba(0,0,0,.15)); }
h1 { margin: 0; font-size: 30px; font-weight: 900; letter-spacing: 1px; }
.sub { margin: 2px 0 0; color: var(--muted); }
.stats { display: flex; gap: 12px; flex-wrap: wrap; }
.stat { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px 18px; display: flex; flex-direction: column; align-items: center; min-width: 108px; }
.stat .num { font-size: 28px; font-weight: 900; line-height: 1.15; }
.stat .num.small { font-size: 16px; padding: 6px 0 4px; }
.stat .lbl { font-size: 13px; color: var(--muted); }
.stat.big { background: var(--accent); color: #fff; min-width: 140px; }
.stat.big .num { font-size: 40px; }
.stat.big .lbl { color: #fff3ea; }

.toolbar { padding: 8px 28px 14px; display: flex; gap: 12px; align-items: center; }
#search { width: min(360px, 100%); font-size: 16px; padding: 10px 16px; border-radius: 999px; border: 2px solid #f0d7bd; background: #fff; outline: none; }
#search:focus { border-color: var(--accent); }
.hint { color: var(--muted); font-size: 14px; }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 22px; padding: 0 28px 28px; align-items: start; }
@media (max-width: 980px) { .layout { grid-template-columns: 1fr; } .board { position: static !important; max-height: none !important; } }

/* ---------- jail building ---------- */
.jail-wrap { background: var(--brick); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
  background-image:
    linear-gradient(var(--mortar) 2px, transparent 2px),
    linear-gradient(90deg, var(--mortar) 2px, transparent 2px);
  background-size: 48px 24px, 48px 48px; }
.jail-roof { background: #8b5e3c; color: #fff; text-align: center; font-weight: 900; font-size: 20px; letter-spacing: 6px; padding: 12px; border-bottom: 6px solid #6d462a; }
.jail { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 18px; padding: 18px; }
.empty { grid-column: 1 / -1; background: var(--card); border-radius: var(--radius); padding: 40px; text-align: center; font-size: 20px; }

.cell { position: relative; background: var(--cell-bg); border-radius: 14px; border: 5px solid #7b8494; overflow: hidden; box-shadow: inset 0 -30px 0 var(--cell-floor); }
.cell-head { position: relative; z-index: 600; display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 6px 10px; background: #5a6372; color: #fff; font-size: 13px; }
.cell-no { font-weight: 900; }
.cell-info { opacity: .9; }
.cell.first { border-color: var(--good); }
.cell.first .cell-head { background: var(--good); }
.cell.lifer { border-color: var(--life); background: #e3dbef; box-shadow: inset 0 -30px 0 #d3c7e6; grid-column: 1 / -1; }
.cell.lifer .cell-head { background: var(--life); }
.floor { position: relative; height: 158px; margin: 0 6px; }
.bars { position: absolute; inset: 30px 0 0 0; pointer-events: none; z-index: 500;
  background:
    linear-gradient(var(--bar), var(--bar)) top / 100% 6px no-repeat,
    repeating-linear-gradient(90deg, transparent 0 34px, var(--bar) 34px 39px); }
.door { position: absolute; right: 8px; bottom: 6px; z-index: 550; font-size: 34px; display: flex; flex-direction: column; align-items: center; line-height: 1; }
.door span { font-size: 11px; font-weight: 900; color: #fff; background: var(--good); border-radius: 6px; padding: 2px 6px; margin-top: 2px; }

/* ---------- inmates ---------- */
.inmate { position: absolute; width: 56px; cursor: pointer; display: flex; flex-direction: column; align-items: center;
  transition-property: left, top; transition-timing-function: linear; transition-duration: 2s; }
.inmate .tag { font-size: 11px; font-weight: 700; background: rgba(255,255,255,.92); border-radius: 8px; padding: 0 5px; max-width: 86px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; box-shadow: 0 1px 2px rgba(0,0,0,.15); }
.inmate .sprite { width: 48px; height: 63px; transition: transform .2s; }
.inmate.flip .sprite { transform: scaleX(-1); }
.inmate .sprite svg { width: 100%; height: 100%; overflow: visible; }
.inmate.walking .sprite svg { animation: bob .32s ease-in-out infinite alternate; }
.inmate .timer { font-size: 10px; font-weight: 900; color: #fff; background: var(--accent); border-radius: 6px; padding: 0 5px; margin-top: -3px; }
.cell.lifer .inmate .timer { background: var(--life); }
.inmate:hover .sprite svg { animation: hop .4s ease; }
.inmate.match .tag { background: #ffe58f; outline: 2px solid var(--accent); }
.inmate.match .sprite { filter: drop-shadow(0 0 6px #ffb020); }
.inmate.dim { opacity: .25; }
@keyframes bob { from { transform: translateY(0) rotate(-3deg); } to { transform: translateY(-3px) rotate(3deg); } }
@keyframes hop { 50% { transform: translateY(-10px); } }

/* ---------- yard ---------- */
.yard { background: linear-gradient(#a8e6a1, #7fcf86); padding: 12px 18px 18px; border-top: 6px solid #6d462a; }
.yard h2 { margin: 0 0 6px; font-size: 18px; }
.yard h2 small { font-weight: 400; color: #2f5d34; font-size: 13px; }
.yard-floor { display: flex; flex-wrap: wrap; gap: 6px 14px; min-height: 90px; align-items: flex-end; }
.freeman { display: flex; flex-direction: column; align-items: center; width: 70px; cursor: pointer; }
.freeman .sprite { width: 46px; height: 60px; }
.freeman .sprite svg { width: 100%; height: 100%; animation: stretch 1.6s ease-in-out infinite; transform-origin: 50% 100%; }
.freeman .tag { font-size: 11px; font-weight: 700; background: #fff; border-radius: 8px; padding: 0 5px; max-width: 80px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.freeman small { font-size: 10px; color: #1f4d26; }
.freeman.dim { opacity: .25; }
.freeman.match .tag { background: #ffe58f; }
.yard-empty { color: #2f5d34; padding: 20px 0; }
@keyframes stretch { 0%, 100% { transform: scaleY(1); } 50% { transform: scaleY(1.07) translateY(-2px); } }

/* ---------- leaderboard ---------- */
.board { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 10px 10px; position: sticky; top: 14px; max-height: calc(100vh - 28px); display: flex; flex-direction: column; }
.board h2 { margin: 0 8px 8px; font-size: 20px; }
#leaderboard { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.row { display: grid; grid-template-columns: 30px 34px 1fr auto; gap: 8px; align-items: center; padding: 6px 8px; border-radius: 12px; cursor: pointer; }
.row:hover { background: #fff1e4; }
.row .rank { font-weight: 900; color: var(--muted); text-align: center; }
.row .mini svg { width: 30px; height: 40px; }
.row .who { display: flex; flex-direction: column; min-width: 0; }
.row .who b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .who small { color: var(--muted); font-size: 11px; }
.row .cd { font-family: ui-monospace, Consolas, monospace; font-weight: 700; font-size: 13px; text-align: right; }
.row.soon { background: #e7f8ef; }
.row.soon .cd { color: var(--good); }
.row.life .cd { color: var(--life); }
.row.dim { display: none; }
.row.match { outline: 2px solid var(--accent); }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(30, 20, 10, .45); display: flex; align-items: center; justify-content: center; z-index: 2000; padding: 16px; }
.modal[hidden] { display: none; }
.modal-card { position: relative; background: var(--card); border-radius: 22px; box-shadow: 0 20px 50px rgba(0,0,0,.3); width: min(440px, 100%); padding: 22px 24px; animation: pop .2s ease; }
@keyframes pop { from { transform: scale(.9); opacity: 0; } }
.close { position: absolute; right: 12px; top: 10px; border: 0; background: none; font-size: 20px; cursor: pointer; color: var(--muted); }
.m-head { display: flex; gap: 16px; align-items: center; margin-bottom: 10px; }
.m-avatar svg { width: 92px; height: 120px; }
.m-head h3 { margin: 0 0 6px; font-size: 24px; word-break: break-all; }
.chip { display: inline-block; background: var(--accent); color: #fff; font-weight: 700; padding: 2px 10px; border-radius: 999px; font-size: 13px; }
.chip-life { background: var(--life); }
.chip-free { background: var(--good); }
.m-list { display: grid; grid-template-columns: 88px 1fr; gap: 6px 10px; margin: 8px 0 12px; }
.m-list dt { color: var(--muted); }
.m-list dd { margin: 0; font-weight: 700; }
.m-cd { font-family: ui-monospace, Consolas, monospace; color: var(--accent); font-size: 18px; }
.progress { height: 12px; background: #f1e4d4; border-radius: 999px; overflow: hidden; }
.progress div { height: 100%; background: linear-gradient(90deg, #ffb37a, var(--accent)); }
.progress-lbl { font-size: 12px; color: var(--muted); margin-top: 4px; }
.m-link { display: block; margin-top: 14px; color: #2563eb; text-decoration: none; font-size: 14px; word-break: break-all; }
.m-link:hover { text-decoration: underline; }

footer { text-align: center; color: var(--muted); font-size: 12px; padding: 0 20px 24px; }
