:root {
  --night: #1b1033;
  --night-2: #2b1850;
  --sign-red: #d7261e;
  --sign-red-dark: #9e1712;
  --bulb: #ffd23f;
  --neon: #ff4fa3;
  --tarp-blue: #1f5fbf;
  --paper: #fff6df;
  --ink: #2a1810;
  --wood: #7a3e17;
  --wood-dark: #5a2c10;
  --muted: #cdbfe6;
  --font-round: "Huninn", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  --font-heavy: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { --night: #1b1033; --paper: #fff6df; }
}
:root[data-theme="dark"] { --night: #1b1033; --paper: #fff6df; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--night);
  color: var(--paper);
  font-family: var(--font-round);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button { font-family: inherit; cursor: pointer; }
button:focus-visible, select:focus-visible { outline: 3px solid var(--bulb); outline-offset: 3px; }

#app { min-height: 100dvh; }
.screen { min-height: 100dvh; }

/* ---------- 開場 ---------- */
.screen-title {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 36px 16px 0;
  background:
    radial-gradient(1.5px 1.5px at 12% 18%, #fff8 50%, transparent 51%),
    radial-gradient(1.5px 1.5px at 72% 9%, #fff6 50%, transparent 51%),
    radial-gradient(1px 1px at 40% 30%, #fff5 50%, transparent 51%),
    radial-gradient(1px 1px at 88% 26%, #fff5 50%, transparent 51%),
    linear-gradient(180deg, #120a26 0%, var(--night) 45%, var(--night-2) 100%);
  overflow: hidden;
  position: relative;
}
.bulb-string {
  position: absolute;
  top: 0; left: -5%; right: -5%;
  height: 40px;
  background: radial-gradient(circle at 50% 70%, var(--bulb) 0 5px, transparent 6px) 0 0 / 34px 40px repeat-x;
  -webkit-mask: radial-gradient(120% 60px at 50% -10px, transparent 70%, #000 71%);
  mask: radial-gradient(120% 60px at 50% -10px, transparent 70%, #000 71%);
  opacity: 0.9;
  animation: twinkle 1.2s steps(2) infinite;
}
@keyframes twinkle { 50% { filter: hue-rotate(-40deg) saturate(1.6); } }

.sign {
  position: relative;
  margin-top: auto;
  padding: 26px 30px 22px;
  background: var(--sign-red);
  border: 4px solid var(--ink);
  border-radius: 18px;
  box-shadow: 0 8px 0 var(--ink);
  text-align: center;
  max-width: 560px;
  width: 100%;
}
.sign::before {
  content: "";
  position: absolute;
  inset: 8px;
  border-radius: 12px;
  background:
    radial-gradient(circle, var(--bulb) 0 4px, transparent 4.5px) 0 0 / 26px 26px repeat-x,
    radial-gradient(circle, var(--bulb) 0 4px, transparent 4.5px) 0 100% / 26px 26px repeat-x,
    radial-gradient(circle, var(--neon) 0 4px, transparent 4.5px) 0 0 / 26px 26px repeat-y,
    radial-gradient(circle, var(--neon) 0 4px, transparent 4.5px) 100% 0 / 26px 26px repeat-y;
  pointer-events: none;
  animation: chase 0.9s steps(2) infinite;
}
@keyframes chase { 50% { filter: brightness(0.55); } }
.sign-title {
  margin: 10px 0 4px;
  font-family: var(--font-heavy);
  font-weight: 900;
  font-size: clamp(42px, 11vw, 76px);
  line-height: 1.05;
  letter-spacing: 0.04em;
  color: var(--bulb);
  -webkit-text-stroke: 3px var(--ink);
  paint-order: stroke fill;
  text-shadow: 0 5px 0 var(--ink);
  transform: rotate(-2deg);
}
.sign-sub {
  margin: 8px 0 6px;
  font-size: clamp(16px, 4vw, 20px);
  color: var(--paper);
}

.stall {
  margin: 30px 0 28px;
  width: 100%;
  max-width: 440px;
  padding: 22px 20px 26px;
  background: var(--wood);
  border: 4px solid var(--ink);
  border-radius: 14px;
  box-shadow: 0 6px 0 var(--ink), inset 0 0 0 3px #9a5626;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: stretch;
  text-align: center;
  position: relative;
  z-index: 1;
}
.stall-q { font-size: 20px; color: var(--paper); }
.select-wrap { position: relative; }
.select-wrap::after {
  content: "▾";
  position: absolute;
  right: 16px; top: 50%;
  transform: translateY(-50%);
  color: var(--ink);
  font-size: 20px;
  pointer-events: none;
}
#county-select {
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
  font-family: var(--font-round);
  font-size: 22px;
  padding: 12px 44px 12px 16px;
  border: 3px solid var(--ink);
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  text-align: center;
  text-align-last: center;
}
.stall-best { margin: 0; color: #ffe8a8; font-size: 15px; min-height: 1.4em; }
.btn-go {
  font-family: var(--font-heavy);
  font-weight: 900;
  font-size: 26px;
  padding: 14px 18px;
  color: var(--ink);
  background: var(--bulb);
  border: 4px solid var(--ink);
  border-radius: 14px;
  box-shadow: 0 6px 0 var(--ink);
  transition: transform 0.08s, box-shadow 0.08s;
}
.btn-go:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--ink); }
.btn-plain {
  font-size: 18px;
  padding: 11px 16px;
  color: var(--paper);
  background: transparent;
  border: 3px solid var(--paper);
  border-radius: 12px;
}
.btn-plain:active { transform: translateY(2px); }
.toggles { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.chip {
  font-size: 14px;
  padding: 7px 12px;
  border-radius: 999px;
  border: 2px solid #f3d9b0;
  background: transparent;
  color: #f3d9b0;
}
.chip[aria-pressed="true"] { background: #f3d9b0; color: var(--ink); }
.howto { margin: 2px 0 0; font-size: 14px; line-height: 1.7; color: #f3d9b0; }

.tarp {
  margin-top: auto;
  flex: none;
  width: 120%;
  height: 74px;
  margin-bottom: 0;
  transform: translateY(14px);
  background:
    repeating-linear-gradient(90deg, #e03131 0 18px, #f8f9fa 18px 26px, var(--tarp-blue) 26px 44px, #f8f9fa 44px 52px);
  border-top: 4px solid var(--ink);
  -webkit-mask: radial-gradient(18px 14px at 18px 100%, transparent 98%, #000) -9px 0 / 36px 100% repeat-x;
  mask: radial-gradient(18px 14px at 18px 100%, transparent 98%, #000) -9px 0 / 36px 100% repeat-x;
}

/* ---------- 遊戲 ---------- */
.screen-game {
  height: 100dvh;
  background: linear-gradient(180deg, #120a26, var(--night));
  overflow: hidden;
}
.game-layout { height: 100%; display: flex; justify-content: center; }
.board-col {
  height: 100%;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 640px;
  padding: env(safe-area-inset-top) 6px env(safe-area-inset-bottom);
}
.hud {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas: "county score combo" "balls balls balls";
  align-items: center;
  gap: 2px 10px;
  padding: 8px 10px 6px;
  margin-top: 6px;
  background: var(--sign-red);
  border: 3px solid var(--ink);
  border-radius: 12px;
  box-shadow: 0 4px 0 var(--ink);
}
.hud-county {
  grid-area: county;
  font-size: 17px;
  color: var(--bulb);
  white-space: nowrap;
}
.hud-score { grid-area: score; display: flex; align-items: baseline; gap: 8px; justify-content: center; }
.hud-label { font-size: 13px; color: #ffd9d3; }
.hud-num {
  font-family: var(--font-heavy);
  font-weight: 900;
  font-size: 30px;
  line-height: 1;
  color: var(--paper);
  font-variant-numeric: tabular-nums;
  -webkit-text-stroke: 1.5px var(--ink);
  paint-order: stroke fill;
}
.hud-num.bump { animation: bump 0.25s; }
@keyframes bump { 40% { transform: scale(1.18); } }
.hud-combo {
  grid-area: combo;
  font-family: var(--font-heavy);
  font-weight: 900;
  font-size: 18px;
  color: var(--ink);
  background: var(--bulb);
  border: 2px solid var(--ink);
  border-radius: 8px;
  padding: 1px 8px;
}
.hud-balls { grid-area: balls; display: flex; gap: 5px; justify-content: center; padding-top: 2px; }
.hud-balls i {
  width: 13px; height: 13px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff 0 15%, #d5dbe3 40%, #6a7380 100%);
  border: 1.5px solid var(--ink);
}
.hud-balls i.used { background: #4a2a24; border-color: #3a201b; }
.hud-balls i.live { box-shadow: 0 0 0 2px var(--bulb); }

.ticker {
  margin: 8px 2px 4px;
  height: 26px;
  overflow: hidden;
  border-radius: 6px;
  background: #0d0719;
  border: 2px solid #3b2560;
  position: relative;
}
.ticker-track {
  position: absolute;
  top: 0; left: 0;
  white-space: nowrap;
  line-height: 22px;
  font-size: 15px;
  color: #ff9fd0;
  padding-left: 100%;
  will-change: transform;
}
.ticker-track.run { animation: tick var(--dur, 12s) linear forwards; }
@keyframes tick { to { transform: translateX(-100%); } }

.board-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#board {
  display: block;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  border-radius: 10px;
  box-shadow: 0 0 0 3px var(--ink), 0 10px 30px #0008;
}
.controls {
  display: grid;
  grid-template-columns: 1.4fr 1fr 1fr;
  gap: 8px;
  padding: 8px 2px 10px;
}
.ctl {
  font-size: 17px;
  padding: 11px 6px;
  color: var(--paper);
  background: var(--night-2);
  border: 2.5px solid #5b3e8c;
  border-radius: 12px;
}
.ctl:active { transform: translateY(2px); }
.ctl-nudge { background: var(--tarp-blue); border-color: #0d2f66; font-weight: 700; }
.ctl-nudge.warn { background: #c2410c; border-color: #7c2d12; }
.nudge-left {
  display: inline-block;
  min-width: 1.6em;
  margin-left: 4px;
  padding: 0 4px;
  border-radius: 6px;
  background: #0006;
  font-size: 14px;
}

.side { display: none; }

/* 寬螢幕：台面佔滿整個高度，HUD／跑馬燈／控制列與地標清單收進右側窄欄 */
@media (min-width: 900px) {
  .game-layout {
    display: grid;
    grid-template-columns: max-content 236px;
    grid-template-rows: auto auto auto minmax(0, 1fr);
    column-gap: 16px;
    justify-content: center;
    align-content: stretch;
    padding-block: 8px;
    padding-left: max(12px, env(safe-area-inset-left, 0px));
    padding-right: max(12px, env(safe-area-inset-right, 0px));
  }
  .board-col { display: contents; }
  .board-wrap { grid-column: 1; grid-row: 1 / -1; min-height: 0; }
  .hud { grid-column: 2; grid-row: 1; margin-top: 0; padding: 7px 9px 6px; gap: 2px 8px; }
  .hud-county { font-size: 15px; }
  .hud-num { font-size: 26px; }
  .hud-label { font-size: 12px; }
  .hud-combo { font-size: 15px; padding: 0 6px; }
  .hud-balls { gap: 4px; }
  .hud-balls i { width: 12px; height: 12px; }
  .ticker { grid-column: 2; grid-row: 2; margin: 8px 0 0; height: 24px; }
  .ticker-track { line-height: 20px; font-size: 14px; }
  .controls { grid-column: 2; grid-row: 3; padding: 8px 0 4px; gap: 6px; }
  .ctl { font-size: 15px; padding: 8px 4px; border-radius: 10px; }
  .nudge-left { font-size: 13px; }
  .side {
    grid-column: 2; grid-row: 4;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-height: 0;
    overflow-y: auto;
    padding: 6px 2px 4px 0;
  }
  .side-h { margin: 2px 0 0; font-size: 16px; font-weight: 400; color: var(--bulb); }
  .landmarks { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 5px; }
  .landmarks li {
    display: grid;
    grid-template-columns: 18px 1fr;
    gap: 1px 7px;
    padding: 6px 8px;
    border-radius: 9px;
    background: #ffffff0d;
    border: 1.5px solid #ffffff1a;
  }
  .landmarks li.hit { border-color: var(--bulb); background: #ffd23f1a; }
  .lm-check { grid-row: span 2; width: 18px; height: 18px; border-radius: 5px; border: 2px solid #ffffff55; display: grid; place-items: center; font-size: 12px; color: var(--ink); }
  .landmarks li.hit .lm-check { background: var(--bulb); border-color: var(--bulb); }
  .lm-name { font-size: 14px; line-height: 1.35; color: var(--paper); }
  .lm-desc { font-size: 12px; color: var(--muted); line-height: 1.4; }
  .side-best { margin: 4px 0 0; font-size: 14px; color: #ffe8a8; }
  .side-best strong { font-family: var(--font-heavy); font-size: 18px; margin-left: 6px; }
  .side-help { font-size: 12px; color: var(--muted); line-height: 1.5; }
  .side-help p { margin: 3px 0; }
  kbd {
    font-family: var(--font-round);
    font-size: 11px;
    padding: 0 5px;
    border-radius: 5px;
    border: 1.5px solid #ffffff55;
    background: #ffffff12;
  }
}

/* 直式手機：HUD、跑馬燈、控制列再精簡一點，把高度讓給台面 */
@media (max-width: 899px) and (orientation: portrait) {
  .board-col { padding-left: max(4px, env(safe-area-inset-left, 0px)); padding-right: max(4px, env(safe-area-inset-right, 0px)); }
  .hud { margin-top: 4px; padding: 5px 9px 5px; gap: 1px 8px; border-width: 2.5px; box-shadow: 0 3px 0 var(--ink); }
  .hud-num { font-size: 26px; }
  .hud-balls i { width: 12px; height: 12px; }
  .ticker { margin: 6px 0 3px; height: 23px; }
  .ticker-track { line-height: 19px; font-size: 14px; }
  .controls { padding: 6px 0 8px; gap: 6px; }
  .ctl { padding: 9px 6px; font-size: 16px; }
}

/* ---------- 疊層 ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: #0b0616cc;
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 10;
  overflow-y: auto;
}
.panel {
  width: 100%;
  max-width: 400px;
  padding: 24px 20px;
  border: 4px solid var(--ink);
  border-radius: 16px;
  box-shadow: 0 8px 0 var(--ink);
}
.panel-pause {
  background: var(--wood);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.panel-h { margin: 0 0 6px; text-align: center; font-size: 26px; font-weight: 400; color: var(--bulb); }

.ticket {
  position: relative;
  background: var(--paper);
  color: var(--ink);
  text-align: center;
  -webkit-mask:
    radial-gradient(10px at 0 50%, transparent 98%, #000) 0 0 / 51% 100% no-repeat,
    radial-gradient(10px at 100% 50%, transparent 98%, #000) 100% 0 / 51% 100% no-repeat;
  mask:
    radial-gradient(10px at 0 50%, transparent 98%, #000) 0 0 / 51% 100% no-repeat,
    radial-gradient(10px at 100% 50%, transparent 98%, #000) 100% 0 / 51% 100% no-repeat;
  box-shadow: none;
}
.ticket-county { margin: 0; font-size: 15px; color: #8a5a3c; }
.ticket-title {
  margin: 6px 0 2px;
  font-family: var(--font-heavy);
  font-weight: 900;
  font-size: 30px;
  color: var(--sign-red);
}
.ticket-score {
  margin: 0;
  font-family: var(--font-heavy);
  font-weight: 900;
  font-size: 64px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.ticket-score small { font-size: 20px; margin-left: 4px; }
.ticket-record {
  display: inline-block;
  margin: 2px 0 6px;
  padding: 2px 12px;
  background: var(--sign-red);
  color: var(--bulb);
  border-radius: 999px;
  font-size: 16px;
}
.ticket-prize {
  margin: 10px 0 12px;
  padding: 12px 10px;
  border-top: 2px dashed #c9a882;
  border-bottom: 2px dashed #c9a882;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.ticket-prize-k { font-size: 13px; color: #8a5a3c; }
.ticket-prize-v { font-size: 19px; }
.ticket-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 0 0 12px; }
.ticket-stats div { display: flex; flex-direction: column; gap: 2px; }
.ticket-stats dt { font-size: 12px; color: #8a5a3c; }
.ticket-stats dd { margin: 0; font-family: var(--font-heavy); font-weight: 900; font-size: 20px; }
.ticket-marks { list-style: none; margin: 0 0 16px; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.ticket-marks li { font-size: 13px; padding: 3px 9px; border-radius: 999px; border: 1.5px solid #c9a882; color: #8a5a3c; }
.ticket-marks li.hit { background: var(--ink); border-color: var(--ink); color: var(--bulb); }
.ticket-actions { display: grid; grid-template-columns: 1.4fr 1fr; gap: 10px; }
.ticket .btn-plain { color: var(--ink); border-color: var(--ink); }

/* 橫式手機（寬 < 900、高度不足）：HUD／跑馬燈／控制列改放台面左側，台面才有高度可用 */
@media (max-width: 899px) and (orientation: landscape) and (max-height: 560px) {
  .board-col {
    max-width: none;
    display: grid;
    grid-template-columns: minmax(0, 1fr) calc((100dvh - 12px) * 0.6 + 8px);
    grid-template-rows: auto auto 1fr auto;
    grid-template-areas: "hud board" "ticker board" ". board" "controls board";
    column-gap: 10px;
    padding: max(6px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  }
  .hud { grid-area: hud; margin-top: 0; }
  .ticker { grid-area: ticker; }
  .board-wrap { grid-area: board; }
  .controls { grid-area: controls; }
}

@media (max-height: 680px) {
  .hud { padding: 5px 8px 4px; }
  .hud-num { font-size: 24px; }
  .ticker { height: 22px; margin: 5px 2px 3px; }
  .ticker-track { line-height: 18px; font-size: 13px; }
  .controls { padding: 6px 2px 6px; }
  .ctl { padding: 8px 6px; font-size: 15px; }
}

@media (prefers-reduced-motion: reduce) {
  .bulb-string, .sign::before { animation: none; }
  .btn-go, .ctl { transition: none; }
}
