/* 共用基礎樣式：重置、結構、響應式骨架。外觀由各主題 theme.css 通過 CSS 變量與覆蓋決定 */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
  margin: 0; min-height: 100vh;
  font-family: var(--font-body, system-ui, "PingFang HK", "Microsoft JhengHei", sans-serif);
  color: var(--text, #222); background: var(--bg, #fff);
  line-height: 1.55; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
  overflow-x: clip;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input { font: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }
ol, ul { list-style: none; margin: 0; padding: 0; }
h1, h2, h3, p { margin: 0; }
table { border-collapse: collapse; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--focus, var(--accent, #06f)); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
html.modal-open { overflow: hidden; }
.wrap { width: min(100% - 32px, var(--wrap, 1200px)); margin-inline: auto; }
time, .num { font-variant-numeric: tabular-nums; }

/* ---------- 號碼球 ---------- */
.balls { --ball: 40px; display: flex; flex-wrap: wrap; align-items: center; gap: var(--ball-gap, 10px); }
.balls--lg { --ball: var(--ball-lg, 64px); }
.balls--md { --ball: var(--ball-md, 48px); }
.balls--sm { --ball: var(--ball-sm, 30px); --ball-gap: var(--ball-gap-sm, 6px); }
.ball {
  width: var(--ball); height: var(--ball); display: grid; place-items: center; flex: none;
  border-radius: var(--ball-radius, 50%); background: var(--bc, #888); color: var(--ball-fg, #fff);
  font-family: var(--font-num, inherit); font-weight: var(--ball-weight, 700);
  font-size: calc(var(--ball) * .46); line-height: 1; font-variant-numeric: tabular-nums;
  transition: transform .25s cubic-bezier(.2, .8, .2, 1), box-shadow .25s, filter .25s;
}
.ball b { font-weight: inherit; }
.ball:hover { transform: translateY(-3px) scale(1.06); }
.ball.c1 { --bc: var(--c1); } .ball.c2 { --bc: var(--c2); } .ball.c3 { --bc: var(--c3); } .ball.c4 { --bc: var(--c4); } .ball.c5 { --bc: var(--c5); }
.ball.c6 { --bc: var(--c6); } .ball.c7 { --bc: var(--c7); } .ball.c8 { --bc: var(--c8); } .ball.c9 { --bc: var(--c9); } .ball.c10 { --bc: var(--c10); }
.ball.is-red { --bc: var(--red); } .ball.is-blue { --bc: var(--blue); }
.chip { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: var(--chip-radius, 50%); font: 700 12px/1 var(--font-num, inherit); background: var(--bc, #888); color: var(--ball-fg, #fff); }
.chip.c1, .lane-car.c1 { --bc: var(--c1); } .chip.c2, .lane-car.c2 { --bc: var(--c2); } .chip.c3, .lane-car.c3 { --bc: var(--c3); }
.chip.c4, .lane-car.c4 { --bc: var(--c4); } .chip.c5, .lane-car.c5 { --bc: var(--c5); } .chip.c6, .lane-car.c6 { --bc: var(--c6); }
.chip.c7, .lane-car.c7 { --bc: var(--c7); } .chip.c8, .lane-car.c8 { --bc: var(--c8); } .chip.c9, .lane-car.c9 { --bc: var(--c9); }
.chip.c10, .lane-car.c10 { --bc: var(--c10); }
.ball-vs { display: grid; place-items: center; padding-inline: 4px; font: 800 11px/1 var(--font-num, inherit); letter-spacing: .1em; color: var(--muted); }

@keyframes ball-in {
  0% { opacity: 0; transform: perspective(500px) rotateX(-90deg) translateY(14px) scale(.7); }
  60% { opacity: 1; transform: perspective(500px) rotateX(12deg) scale(1.08); }
  100% { opacity: 1; transform: none; }
}
.is-reveal .ball { animation: ball-in .75s cubic-bezier(.2, .8, .2, 1) both; animation-delay: calc(var(--i, 0) * 90ms); }

/* 手機：10 球排成 5 + 5，大球自動縮放 */
@media (max-width: 640px) {
  .balls--race.balls--lg, .balls--race.balls--md {
    --ball: clamp(40px, calc((100vw - 90px) / 5), 64px);
    display: grid; grid-template-columns: repeat(5, var(--ball)); justify-content: space-between; row-gap: 12px; width: 100%;
  }
  .balls--index.balls--lg { --ball: clamp(44px, calc((100vw - 120px) / 4.6), 64px); gap: 8px; }
  .balls--index.balls--md { --ball: 46px; gap: 8px; }
}

/* ---------- 標籤 ---------- */
.tags { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.tag {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 10px; min-height: 26px;
  border-radius: var(--tag-radius, 999px); font-size: 13px; font-weight: 600; line-height: 1;
  background: var(--tag-bg, rgba(127,127,127,.14)); color: var(--tag-fg, inherit);
  transition: transform .2s, background .2s, color .2s;
}
.tag b { font-family: var(--font-num, inherit); font-weight: 700; }
.tag:hover { transform: translateY(-1px); }
.tags--sm .tag { min-height: 22px; padding: 2px 8px; font-size: 12px; }
.tag.is-big, .tag.is-dragon { --tag-bg: var(--t-big-bg); --tag-fg: var(--t-big-fg); }
.tag.is-small, .tag.is-tiger { --tag-bg: var(--t-small-bg); --tag-fg: var(--t-small-fg); }
.tag.is-odd { --tag-bg: var(--t-odd-bg); --tag-fg: var(--t-odd-fg); }
.tag.is-even { --tag-bg: var(--t-even-bg); --tag-fg: var(--t-even-fg); }
.tag.is-red { --tag-bg: color-mix(in srgb, var(--red) 20%, transparent); --tag-fg: var(--red-ink, var(--red)); }
.tag.is-blue { --tag-bg: color-mix(in srgb, var(--blue) 20%, transparent); --tag-fg: var(--blue-ink, var(--blue)); }

.void { display: inline-block; padding: 4px 14px; border-radius: 999px; font-size: 13px; letter-spacing: .08em; background: rgba(127,127,127,.16); color: var(--muted); }

/* ---------- 按鈕與頁籤 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 42px; padding: 0 22px;
  font-weight: 600; letter-spacing: .04em; transition: transform .2s, background .2s, color .2s, box-shadow .2s, border-color .2s;
}
.btn:hover { transform: translateY(-2px); }
.btn:active { transform: translateY(0); }
.tabs { display: inline-flex; }
.tab { transition: color .2s, background .2s, border-color .2s, box-shadow .2s, transform .2s; }

/* ---------- 近期開獎列表 ---------- */
.rrow {
  display: grid; grid-template-columns: 128px 1fr auto; align-items: center; gap: 14px;
  padding: 12px 16px; cursor: pointer; transition: background .2s, transform .2s, border-color .2s, box-shadow .2s;
}
.rrow-body { display: grid; gap: 8px; justify-items: start; min-width: 0; }
.rrow-meta { display: grid; line-height: 1.25; }
.rrow-qi { font: 600 14px var(--font-num, inherit); letter-spacing: .02em; }
.rrow time { font-size: 12px; color: var(--muted); }
.rrow-go { font-size: 12px; color: var(--muted); transition: transform .2s, color .2s; }
.rrow:hover .rrow-go { transform: translateX(4px); color: var(--accent); }

/* ---------- 歷史表格 ---------- */
.htable { width: 100%; }
.htable th { text-align: left; font-weight: 600; font-size: 13px; white-space: nowrap; }
.htable td { vertical-align: middle; }
.hrow { cursor: pointer; transition: background .2s, box-shadow .2s, transform .2s; }
.hrow .c-qi { display: grid; line-height: 1.3; }
.hrow .c-qi .qi { font: 600 14px var(--font-num, inherit); }
.hrow .c-qi time { font-size: 12px; color: var(--muted); }
.hrow .c-go { text-align: right; font-size: 12px; color: var(--muted); white-space: nowrap; transition: color .2s, transform .2s; }
.hrow:hover .c-go { color: var(--accent); transform: translateX(3px); }
.empty { display: grid; gap: 6px; place-items: center; padding: 56px 16px; text-align: center; color: var(--muted); }
.empty b { color: var(--text); font-size: 16px; }

@media (max-width: 760px) {
  .rrow { grid-template-columns: 1fr auto; row-gap: 8px; padding: 12px; }
  .rrow-meta { grid-column: 1; grid-template-columns: auto auto; justify-content: start; gap: 10px; align-items: baseline; }
  .rrow-body { grid-column: 1 / -1; grid-row: 2; }
  .rrow-go { grid-column: 2; grid-row: 1; }
  .balls--race.balls--sm { --ball: clamp(22px, calc((100vw - 88px) / 10 - 4px), 32px); gap: 4px; flex-wrap: nowrap; }
  .htable thead { display: none; }
  .htable, .htable tbody, .htable tr, .htable td { display: block; width: 100%; }
  .htable tr.hrow { padding: 12px; margin-bottom: 8px; position: relative; }
  .htable td { padding: 4px 0 !important; border: 0 !important; }
  .htable .c-qi { grid-template-columns: auto auto; justify-content: start; gap: 10px; align-items: baseline; }
  .htable .c-go { position: absolute; top: 14px; right: 12px; width: auto; }
}

/* ---------- 分頁 ---------- */
.pager { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.pg { min-width: 38px; height: 38px; display: inline-grid; place-items: center; padding: 0 6px; font: 600 14px var(--font-num, inherit); transition: background .2s, color .2s, border-color .2s, transform .2s; }
.pg:hover:not(:disabled):not(.is-active):not(.pg--gap) { transform: translateY(-2px); }
.pg:disabled { opacity: .35; cursor: not-allowed; }
.pg--gap { cursor: default; opacity: .5; }

/* ---------- 動畫舞台佔位 ---------- */
.stage { position: relative; }
.stage-inner { position: relative; display: flex; flex-direction: column; height: 100%; min-height: var(--stage-h, 340px); overflow: hidden; }
.stage-bar { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 14px; z-index: 2; position: relative; }
.stage-badge { display: inline-flex; align-items: center; gap: 8px; padding: 4px 12px; border-radius: 999px; font-size: 12px; font-weight: 700; letter-spacing: .1em; }
.stage-badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.stage-note { font-size: 11px; letter-spacing: .12em; opacity: .55; }
.track { position: relative; flex: 1; display: grid; grid-template-rows: repeat(10, minmax(0, 1fr)); gap: var(--lane-gap, 3px); padding: 0 14px 14px; }
.lane { display: grid; grid-template-columns: var(--lane-label, 96px) 1fr; align-items: center; min-height: 22px; }
.lane-label { display: flex; align-items: center; gap: 8px; min-width: 0; font-size: 12px; }
.lane-no { width: 18px; text-align: center; font: 700 12px var(--font-num, inherit); opacity: .8; }
.lane-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; opacity: .85; }
.lane-run { position: relative; height: 100%; }
.lane-car {
  position: absolute; z-index: 1; top: 50%; left: calc(var(--pos, 10) * .88%); height: 80%; aspect-ratio: 2.2 / 1; max-height: 26px;
  display: grid; place-items: center; transform: translateY(-50%); border-radius: var(--car-radius, 6px);
  background: var(--bc); color: var(--ball-fg, #fff); font: 800 11px/1 var(--font-num, inherit);
  transition: left 1.35s cubic-bezier(.45, .05, .3, 1);
}
.finish { position: absolute; top: 0; bottom: 14px; right: 24px; width: 8px; background: repeating-conic-gradient(#fff 0 25%, #111 0 50%) 0 0 / 8px 8px; opacity: .75; }
.duel { flex: 1; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px; padding: 12px 18px 22px; }
.duel-side { display: grid; gap: 10px; justify-items: center; text-align: center; }
.duel-name { font-weight: 700; letter-spacing: .06em; }
.duel-name em { font-style: normal; margin-left: 6px; font-size: 12px; padding: 1px 7px; border-radius: 4px; background: var(--bc); color: #fff; }
.duel-side.is-red { --bc: var(--red); } .duel-side.is-blue { --bc: var(--blue); }
.duel-num { font: 700 clamp(56px, 9vw, 104px)/1 var(--font-num, inherit); color: var(--bc); transition: transform .4s; font-variant-numeric: tabular-nums; }
.duel-bar { width: 100%; max-width: 240px; height: 8px; border-radius: 99px; background: rgba(127,127,127,.2); overflow: hidden; }
.duel-bar i { display: block; height: 100%; width: var(--v, 0%); background: var(--bc); border-radius: inherit; transition: width 1.2s cubic-bezier(.45, .05, .3, 1); }
.duel-sub { font-size: 12px; opacity: .6; }
.duel-vs { font: 800 20px var(--font-num, inherit); letter-spacing: .1em; opacity: .7; }
.stage.is-lead-red .duel-side.is-red .duel-num, .stage.is-lead-blue .duel-side.is-blue .duel-num { transform: scale(1.06); }

@media (max-width: 640px) {
  .lane { grid-template-columns: 30px 1fr; }
  .lane-name { display: none; }
  .track { padding: 0 10px 12px; }
  .duel { grid-template-columns: 1fr 1fr; padding: 8px 10px 18px; }
  .duel-vs { display: none; }
}

/* ---------- 車號對照 ---------- */
.legend { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.legend-item { display: flex; align-items: center; gap: 10px; padding: 8px 10px; min-width: 0; transition: background .2s, transform .2s; }
.legend-item:hover { transform: translateY(-2px); }
.legend-name { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.legend-code { margin-left: auto; font: 12px var(--font-num, inherit); color: var(--muted); }

/* ---------- 詳情彈層 ---------- */
.modal { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 16px; }
.modal-backdrop { position: absolute; inset: 0; background: var(--backdrop, rgba(0, 0, 0, .6)); opacity: 0; transition: opacity .22s; backdrop-filter: blur(4px); }
.modal-card {
  position: relative; width: min(100%, 760px); max-height: min(88vh, 860px); overflow: auto; overscroll-behavior: contain;
  padding: 28px; background: var(--modal-bg, var(--surface, #fff)); color: var(--text); border: 1px solid var(--line, transparent);
  opacity: 0; transform: translateY(18px) scale(.98); transition: opacity .22s, transform .28s cubic-bezier(.2, .8, .2, 1);
}
.modal.is-open .modal-backdrop { opacity: 1; }
.modal.is-open .modal-card { opacity: 1; transform: none; }
.modal-close { position: absolute; top: 12px; right: 12px; width: 38px; height: 38px; display: grid; place-items: center; font-size: 26px; line-height: 1; opacity: .7; transition: opacity .2s, transform .2s, background .2s; z-index: 2; }
.modal-close:hover { opacity: 1; transform: rotate(90deg); }
.dm-head { display: grid; gap: 4px; margin-bottom: 18px; padding-right: 40px; }
.dm-eyebrow { font-size: 12px; letter-spacing: .14em; color: var(--accent); font-weight: 700; }
.dm-title { font: 700 26px/1.2 var(--font-display, inherit); }
.dm-time { font-size: 13px; color: var(--muted); }
.dm-nums { display: grid; gap: 14px; padding: 18px 0; }
.dm-sub { font-size: 14px; letter-spacing: .1em; margin: 6px 0 12px; color: var(--muted); font-weight: 600; }
.dm-scroll { overflow-x: auto; margin-inline: -4px; padding-inline: 4px; }
.dm-table { width: 100%; font-size: 14px; min-width: 520px; }
.dm-table th { text-align: left; font-size: 12px; font-weight: 600; padding: 8px 10px; color: var(--muted); white-space: nowrap; }
.dm-table td { padding: 8px 10px; border-top: 1px solid var(--line); }
.dm-table tbody tr { transition: background .2s; }
.dm-table .t-rank { font: 700 15px var(--font-num, inherit); }
.dm-table .t-name small { display: block; font: 11px var(--font-num, inherit); color: var(--muted); }
.dm-table .t-price { font-family: var(--font-num, inherit); white-space: nowrap; }
.dm-table .dig { font: 700 15px var(--font-num, inherit); text-align: center; color: var(--muted); }
.dm-table .dig.d1 { color: var(--accent); }
.dm-note { margin-top: 12px; font-size: 13px; color: var(--muted); line-height: 1.7; }
.dm-note b { color: var(--text); font-weight: 600; }
.dm-steps { display: grid; gap: 12px; }
.dm-step { padding: 16px 18px; border: 1px solid var(--line); border-left: 3px solid var(--bc, var(--accent)); background: var(--step-bg, transparent); }
.dm-step.is-red { --bc: var(--red); } .dm-step.is-blue { --bc: var(--blue); } .dm-step.is-versus { --bc: var(--accent); }
.dm-step h3 { font-size: 14px; margin-bottom: 8px; letter-spacing: .06em; color: var(--bc); }
.dm-eq { font-size: 14px; line-height: 1.9; color: var(--muted); }
.dm-eq b { color: var(--text); font-family: var(--font-num, inherit); font-weight: 600; }
.dm-eq .dm-res { color: var(--bc); font-size: 18px; }
.dm-foot { margin-top: 16px; font-size: 12px; color: var(--muted); word-break: break-all; }
.dm-foot code { font-family: var(--font-num, ui-monospace, monospace); padding: 2px 8px; background: rgba(127,127,127,.12); border-radius: 4px; }

@media (max-width: 640px) {
  .modal { align-items: end; padding: 0; }
  .modal-card { width: 100%; max-height: 92vh; padding: 22px 16px 28px; border-radius: 18px 18px 0 0 !important; transform: translateY(40px); }
  .dm-title { font-size: 22px; }
}

/* ---------- 預覽主題切換器 ---------- */
.theme-switch { position: fixed; right: 14px; bottom: calc(14px + var(--switch-lift, 0px)); z-index: 90; font-family: system-ui, "Microsoft JhengHei", sans-serif; }
.ts-toggle {
  display: inline-flex; align-items: center; gap: 8px; height: 38px; padding: 0 16px; border-radius: 99px; font-size: 13px; font-weight: 600;
  background: rgba(20, 20, 24, .88); color: #fff; border: 1px solid rgba(255,255,255,.18); backdrop-filter: blur(10px); box-shadow: 0 6px 20px rgba(0,0,0,.28);
  transition: transform .2s, background .2s;
}
.ts-toggle:hover { transform: translateY(-2px); background: rgba(20, 20, 24, .98); }
.ts-dot { width: 10px; height: 10px; border-radius: 50%; background: conic-gradient(#d9b45f 0 25%, #ff2e93 0 50%, #1d4f9e 0 75%, #e10600 0); }
.ts-panel {
  position: absolute; right: 0; bottom: 46px; min-width: 190px; padding: 6px; border-radius: 14px; display: grid; gap: 2px;
  background: rgba(20, 20, 24, .96); border: 1px solid rgba(255,255,255,.16); box-shadow: 0 16px 40px rgba(0,0,0,.4);
  opacity: 0; transform: translateY(8px); pointer-events: none; transition: opacity .18s, transform .18s;
}
.theme-switch.is-open .ts-panel { opacity: 1; transform: none; pointer-events: auto; }
.ts-item { display: block; width: 100%; text-align: left; padding: 9px 12px; border-radius: 9px; font-size: 13px; color: #e9e9ee; transition: background .15s; }
.ts-item:hover { background: rgba(255,255,255,.1); }
.ts-item.is-current { background: rgba(255,255,255,.16); font-weight: 700; }
.ts-item.is-current::after { content: "　目前"; font-size: 11px; opacity: .6; }
.ts-sep { height: 1px; margin: 4px 6px; background: rgba(255,255,255,.14); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
}
