:root {
  --bg1: #5c0a10;
  --bg2: #b8242c;
  --bg3: #2a0306;
  --gold: #f5c451;
  --gold-2: #d99a1e;
  --ink: #fff8ec;
  --muted: rgba(255, 248, 236, .72);
  --card: rgba(255, 255, 255, .07);
  --card-line: rgba(245, 196, 81, .28);
  --danger: #ffb4a8;
  --radius: 18px;
  --font: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, -apple-system, "Segoe UI", sans-serif;
  --num: "SF Pro Rounded", "Arial Rounded MT Bold", "Nunito", "Segoe UI", var(--font);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font);
  color: var(--ink);
  background-color: var(--bg1);
  background-image: radial-gradient(120% 70% at 50% 0%, var(--bg2) 0%, var(--bg1) 55%, var(--bg3) 100%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.45rem; letter-spacing: .02em; }
h2 { font-size: 1.05rem; color: var(--gold); margin-bottom: 12px; letter-spacing: .04em; }
.muted { color: var(--muted); margin: 0; }
.small { font-size: .88rem; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: max(12px, env(safe-area-inset-top)) 16px 12px;
  max-width: 760px; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .06em; }
.top-actions { display: flex; gap: 8px; }

/* Controls */
.chip {
  font: inherit; font-size: .85rem; color: var(--ink);
  background: rgba(255, 255, 255, .1); border: 1px solid rgba(255, 255, 255, .18);
  border-radius: 999px; padding: 6px 14px; cursor: pointer;
}
.chip:hover { background: rgba(255, 255, 255, .18); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  font: inherit; font-weight: 700; font-size: 1rem; text-decoration: none; cursor: pointer;
  border-radius: 14px; padding: 13px 20px; border: 1px solid transparent; color: var(--ink);
  background: rgba(255, 255, 255, .12);
}
.btn.primary {
  width: 100%; color: #4a1600;
  background: linear-gradient(180deg, #ffe08a, var(--gold) 45%, var(--gold-2));
  box-shadow: 0 6px 18px rgba(0, 0, 0, .3), inset 0 1px 0 rgba(255, 255, 255, .6);
}
.btn.primary:disabled { opacity: .7; cursor: progress; }
.btn.ghost { background: transparent; border-color: var(--card-line); }
.btn.small { padding: 10px 14px; font-size: .92rem; border-radius: 12px; }
.btn:focus-visible, .chip:focus-visible, input:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
input {
  font: inherit; font-size: 1.05rem; color: var(--ink); width: 100%;
  background: rgba(0, 0, 0, .28); border: 1px solid rgba(255, 255, 255, .2);
  border-radius: 12px; padding: 12px 14px;
}
input::placeholder { color: rgba(255, 248, 236, .35); }

/* Layout */
main { padding: 0 16px max(24px, env(safe-area-inset-bottom)); }
.view { max-width: 760px; margin: 0 auto; }
.card {
  background: var(--card); border: 1px solid var(--card-line);
  border-radius: var(--radius); padding: 18px;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}

/* Join */
.join-card { max-width: 420px; margin: 6vh auto 0; text-align: center; padding: 28px 22px; }
.join-hero { display: block; margin: 0 auto 14px; filter: drop-shadow(0 8px 18px rgba(0, 0, 0, .4)); }
.join-card h1 { margin-bottom: 8px; }
.join-card form { margin-top: 22px; display: grid; gap: 14px; text-align: left; }
.field { display: grid; gap: 6px; font-size: .9rem; color: var(--muted); }
.code-input { text-transform: uppercase; letter-spacing: .3em; font-family: var(--num); font-weight: 700; font-size: 1.3rem; text-align: center; }
.error { color: var(--danger); margin: 0; font-size: .92rem; }
.demo-link { display: inline-block; margin-top: 18px; color: var(--gold); font-size: .9rem; }

/* Live */
.event-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin: 6px 0 14px; }
.event-titles { min-width: 0; }
.event-titles h1 { overflow-wrap: anywhere; }
.meta { margin: 6px 0 0; font-size: .85rem; color: var(--muted); letter-spacing: .03em; }
.pill { flex: none; font-size: .8rem; font-weight: 700; padding: 5px 11px; border-radius: 999px; background: rgba(255, 255, 255, .12); white-space: nowrap; }
.pill.live { background: #1f8f5f; }
.pill.live::before { content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: #b8ffd9; margin-right: 6px; vertical-align: 1px; animation: blink 1.4s infinite; }
.pill.ended { background: rgba(0, 0, 0, .35); color: var(--muted); }
@keyframes blink { 50% { opacity: .25; } }

.stage-card {
  position: relative;
  border-radius: 22px; padding: 16px 14px 14px;
  background: radial-gradient(90% 70% at 50% 35%, rgba(255, 210, 120, .14), rgba(0, 0, 0, .25) 70%), rgba(0, 0, 0, .22);
  border: 1px solid var(--card-line);
  box-shadow: inset 0 0 60px rgba(0, 0, 0, .35);
}
.round-head { text-align: center; min-height: 3.2em; }
.round-label { font-size: 1.25rem; font-weight: 800; letter-spacing: .04em; color: var(--ink); }
.round-label.active { color: var(--gold); text-shadow: 0 0 18px rgba(245, 196, 81, .45); }
.round-progress { font-size: .88rem; color: var(--muted); margin-top: 4px; min-height: 1.2em; }
.tank-wrap { height: min(58vh, 92vw); max-height: 440px; min-height: 240px; }
.tank-wrap canvas { width: 100%; height: 100%; display: block; }
.reveal-row { --size: 54px; display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; min-height: 54px; margin-top: 8px; }

.side { display: grid; gap: 14px; margin-top: 14px; }
.banner { border-radius: 16px; padding: 14px 16px; font-weight: 700; text-align: center; }
.banner.win { color: #4a1600; background: linear-gradient(180deg, #ffe08a, var(--gold)); box-shadow: 0 8px 24px rgba(245, 196, 81, .35); animation: pop .5s ease-out; }
.banner.ended { background: rgba(0, 0, 0, .3); border: 1px solid var(--card-line); }
@keyframes pop { 0% { transform: scale(.85); opacity: 0; } 70% { transform: scale(1.04); } 100% { transform: scale(1); opacity: 1; } }

.my-wrap { position: relative; display: flex; justify-content: center; }
.my-chip {
  font: inherit; color: var(--ink); cursor: pointer;
  display: inline-flex; align-items: center; gap: 10px;
  padding: 8px 8px 8px 16px; border-radius: 999px;
  background: rgba(255, 255, 255, .08); border: 1px solid var(--card-line);
}
.my-chip:hover { background: rgba(255, 255, 255, .14); }
.my-chip-label { font-size: .92rem; font-weight: 700; color: var(--gold); }
.my-chip-num {
  min-width: 2.6em; padding: 4px 12px; border-radius: 999px; text-align: center;
  font-size: .85rem; color: var(--muted); background: rgba(0, 0, 0, .25);
}
.my-chip.set .my-chip-num {
  font-family: var(--num); font-weight: 800; font-size: 1.05rem; letter-spacing: .08em;
  color: #3a1a00; background: #fff8ec;
}
.my-chip.won { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(245, 196, 81, .35); }
.my-chip.won .my-chip-num { background: linear-gradient(180deg, #ffe08a, var(--gold)); }
.my-pop {
  position: absolute; bottom: calc(100% + 10px); left: 50%; transform: translateX(-50%);
  z-index: 20; width: min(300px, calc(100vw - 32px));
  padding: 14px; border-radius: 16px; text-align: center;
  background: #3a070b; border: 1px solid var(--gold);
  box-shadow: 0 14px 34px rgba(0, 0, 0, .5);
  animation: pop .2s ease-out;
}
.my-pop::after {
  content: ""; position: absolute; top: 100%; left: 50%; margin-left: -8px;
  border: 8px solid transparent; border-top-color: var(--gold);
}
.my-pop-title { font-weight: 700; color: var(--gold); margin-bottom: 10px; }
.my-form { display: grid; grid-template-columns: 1fr auto; gap: 8px; margin-bottom: 8px; }
.my-form input { font-family: var(--num); font-weight: 700; text-align: center; letter-spacing: .15em; padding: 10px; }
.btn.primary-sm { color: #4a1600; background: linear-gradient(180deg, #ffe08a, var(--gold) 45%, var(--gold-2)); }
.my-pop .muted { margin: 0; line-height: 1.4; }
.link-btn { font: inherit; font-size: .85rem; color: var(--muted); background: none; border: 0; text-decoration: underline; margin-top: 8px; cursor: pointer; }

.prize-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.prize-list li { padding: 12px; border-radius: 14px; background: rgba(0, 0, 0, .18); border: 1px solid transparent; }
.prize-list li.current { border-color: var(--gold); background: rgba(245, 196, 81, .1); }
.prize-list li.empty { color: var(--muted); text-align: center; }
.prize-top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.prize-name { font-weight: 700; }
.prize-detail { font-size: .85rem; color: var(--muted); margin-top: 2px; }
.prize-count { flex: none; font-family: var(--num); font-weight: 700; color: var(--gold); font-size: .95rem; }
.prize-balls { --size: 40px; display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.prize-balls .muted { font-size: .85rem; }
.screen-mode .prize-balls .muted { display: none; }
.screen-mode .prize-balls:empty, .screen-mode .prize-balls:not(:has(.ball)) { display: none; }

.qr-card, .screen-tools { display: none; }
.footer-actions { text-align: center; }

/* Balls */
.ball {
  --size: 54px; --c1: #ff5a5f; --c2: #b3121b;
  width: var(--size); height: var(--size); flex: none;
  border-radius: 50%; display: grid; place-items: center;
  font-size: calc(var(--size) * .3);
  background: radial-gradient(circle at 32% 28%, #fff 0 5%, var(--c1) 24%, var(--c2) 100%);
  box-shadow: inset calc(var(--size) * -.06) calc(var(--size) * -.09) calc(var(--size) * .16) rgba(0, 0, 0, .28), 0 4px 12px rgba(0, 0, 0, .35);
  font-family: var(--num); font-weight: 800; color: #3a1a00;
  user-select: none;
}
.reveal-row .ball, .prize-balls .ball { --size: inherit; }
.reveal-row .ball { width: var(--size); height: var(--size); }
.ball[data-len="1"], .ball[data-len="2"] { font-size: calc(var(--size) * .42); }
.ball[data-len="4"] { font-size: calc(var(--size) * .27); }
.ball[data-len="5"], .ball[data-len="6"] { font-size: calc(var(--size) * .22); }
.ball > span {
  width: 66%; height: 66%; border-radius: 50%; display: grid; place-items: center;
  background: rgba(255, 255, 255, .95); line-height: 1; letter-spacing: -.02em;
  box-shadow: inset 0 -2px 4px rgba(0, 0, 0, .12);
}
.prize-balls .ball { cursor: zoom-in; }
.ball.mine { outline: 3px solid #fff; outline-offset: 2px; box-shadow: 0 0 0 6px rgba(245, 196, 81, .6), 0 4px 12px rgba(0, 0, 0, .35); }
.ball.voided { filter: grayscale(1) brightness(.7); opacity: .55; }
.ball.voided > span { text-decoration: line-through; }
.ball.land { animation: land .35s ease-out; }
@keyframes land { 0% { transform: scale(1.25); } 100% { transform: scale(1); } }

.fly-layer { position: fixed; inset: 0; pointer-events: none; z-index: 40; }
.fly-layer .ball { position: absolute; will-change: transform; transform-origin: 50% 50%; }
.fly-layer .ball > span { backface-visibility: hidden; }
.confetti { position: fixed; inset: 0; width: 100vw; height: 100vh; pointer-events: none; z-index: 50; }

.zoom {
  position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; align-content: center; gap: 20px;
  background: rgba(20, 2, 4, .82); backdrop-filter: blur(4px); cursor: zoom-out; padding: 16px;
}
.zoom .ball { --size: min(70vw, 60vh); animation: pop .35s ease-out; }
.zoom-caption { font-size: 1.3rem; font-weight: 700; color: var(--gold); text-align: center; }

.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 70; background: rgba(0, 0, 0, .8); color: var(--ink); padding: 10px 18px; border-radius: 999px;
  font-size: .92rem; max-width: calc(100vw - 32px); text-align: center;
}

/* ---------- Big-screen (projector) mode ---------- */
body.screen-mode { overflow: hidden; background-image: radial-gradient(110% 80% at 35% 20%, #a3202a 0%, #4a070c 55%, #160102 100%); }
body.screen-mode .topbar { display: none; }
body.screen-mode main { padding: 0; }
body.screen-mode #joinView { padding-top: 6vh; }
body.screen-mode #liveView {
  max-width: none; height: 100vh; padding: 3vh 3vw;
  display: grid; grid-template-columns: minmax(0, 1.65fr) minmax(300px, 1fr);
  grid-template-rows: auto minmax(0, 1fr); gap: 2.5vh 2.5vw;
}
.screen-mode .event-head { grid-column: 1 / -1; margin: 0; }
.screen-mode .event-head h1 { font-size: clamp(1.6rem, 4.4vh, 3.4rem); }
.screen-mode .event-head .muted { font-size: clamp(1rem, 2.2vh, 1.6rem); }
.screen-mode .meta { font-size: clamp(.9rem, 1.9vh, 1.4rem); }
.screen-mode .pill { font-size: clamp(.9rem, 1.8vh, 1.3rem); padding: .4em 1em; }
.screen-mode .stage-card { display: flex; flex-direction: column; min-height: 0; padding: 2.5vh 2vw; }
.screen-mode .round-label { font-size: clamp(2rem, 6.4vh, 5.2rem); }
.screen-mode .round-progress { font-size: clamp(1rem, 2.4vh, 1.8rem); }
.screen-mode .tank-wrap { flex: 1; height: auto; max-height: none; min-height: 0; }
.screen-mode .reveal-row { --size: clamp(60px, 11vh, 130px); gap: 1.4vh; min-height: var(--size); margin-top: 1.5vh; }
.screen-mode .side { margin: 0; min-height: 0; display: flex; flex-direction: column; gap: 2vh; }
.screen-mode .prize-card { position: relative; scroll-behavior: smooth; flex: 1 1 auto; min-height: 0; overflow: auto; scrollbar-width: none; }
.screen-mode .prize-card::-webkit-scrollbar { display: none; }
.screen-mode .qr-card, .screen-mode #endedBanner { flex: none; }
.screen-mode .prize-list { gap: 1.2vh; }
.screen-mode .prize-list li { padding: 1.4vh 1vw; }
.screen-mode .my-wrap, .screen-mode .footer-actions, .screen-mode #myBanner { display: none !important; }
.screen-mode .card { padding: 2vh 1.4vw; }
.screen-mode h2 { font-size: clamp(1.1rem, 2.6vh, 2rem); }
.screen-mode .prize-name { font-size: clamp(1rem, 2.4vh, 1.8rem); }
.screen-mode .prize-detail { font-size: clamp(.85rem, 1.7vh, 1.2rem); }
.screen-mode .prize-count { font-size: clamp(1rem, 2.2vh, 1.6rem); }
.screen-mode .prize-balls { --size: clamp(38px, 5.6vh, 70px); }
.screen-mode .qr-card { display: flex; align-items: center; gap: 1.4vw; }
.qr-box { flex: none; background: #fff; border-radius: 12px; padding: 8px; line-height: 0; }
.qr-box svg { display: block; width: clamp(100px, 15vh, 200px); height: auto; }
.qr-title { font-weight: 800; font-size: clamp(1.1rem, 2.6vh, 2rem); color: var(--gold); margin-bottom: 1vh; }
.qr-code { font-family: var(--num); font-weight: 800; letter-spacing: .2em; font-size: clamp(1.4rem, 4vh, 3rem); }
.screen-mode .screen-tools {
  display: flex; gap: 8px; position: fixed; right: 16px; bottom: 16px; z-index: 30;
  opacity: 0; transition: opacity .3s;
}
.screen-mode.show-tools .screen-tools { opacity: 1; }
.screen-mode:not(.show-tools) { cursor: none; }

@media (prefers-reduced-motion: reduce) {
  .pill.live::before, .banner.win { animation: none; }
}
