:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --ink: #1a1a1a;
  --muted: #888888;
  --muted-2: #666666;
  --muted-3: #b0b0b0;
  --line: #e5e5e0;
  --line-2: #eeeeee;
  --track: #b42318;
  --tab-on: #1a1a1a;
  --sort: #3730a3;
  --chip-red: #e53935;
  --chip-purple: #7b1fa2;
  --chip-yellow: #fadb14;
  --group-entry: #eef2ff;
  --group-same: #fff1f0;
  --group-dist: #fffbe6;
  --group-anal: #f6ffed;
  --group-l1: #f9f0ff;
  --group-r1: #f9f0ff;
  --heat-hi-bg: #f6ffed;
  --heat-hi-fg: #135200;
  --heat-mid-bg: #fffbe6;
  --heat-mid-fg: #ad4e00;
  --heat-lo-bg: #fff1f0;
  --heat-lo-fg: #b42318;
  --scr: #b42318;
  --font: "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  --thead-h1: 32px;
  --thead-h2: 28px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.topbar-left {
  display: flex;
  align-items: baseline;
  gap: 14px;
  min-width: 0;
}

.brand {
  font-size: 16px;
  font-weight: 700;
  color: var(--ink);
  white-space: nowrap;
}

.race-meta {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.content-tabs {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.content-tabs .tab {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted-2);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 12px;
  cursor: pointer;
}

.content-tabs .tab.active {
  background: var(--tab-on);
  border-color: var(--tab-on);
  color: #fff;
  font-weight: 700;
}

.race-switch {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 20px;
}

.race-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.race-tabs button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted-2);
  border-radius: 4px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}

.race-tabs button.active {
  background: var(--ink);
  border-color: var(--ink);
  color: #fff;
  font-weight: 700;
}

.status {
  margin-left: auto;
  font-size: 11px;
  color: var(--muted-3);
}

.summary-bar {
  display: grid;
  grid-template-columns: minmax(220px, 1.2fr) minmax(280px, 1.4fr) minmax(200px, 1fr);
  gap: 20px;
  align-items: center;
  margin: 0 20px 10px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  min-height: 44px;
}

.tips-inline {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  font-size: 12px;
  color: var(--ink);
  font-weight: 400;
}

.tip-bracket {
  color: var(--ink);
  font-weight: 700;
  letter-spacing: 0.02em;
}

.summary-track {
  font-size: 12px;
  color: var(--track);
  font-weight: 400;
}

.summary-live {
  font-size: 12px;
  text-align: right;
}

.summary-live .live-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
  align-items: center;
}

.summary-live .live-label {
  color: var(--muted);
  font-size: 11px;
}

.tip-chip {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 24px;
  height: 22px;
  padding: 0 6px;
  border-radius: 4px;
  font-weight: 700;
  font-size: 12px;
}

.tip-chip.banker {
  background: var(--chip-purple);
  color: #fff;
}

.tip-chip.place {
  background: var(--chip-yellow);
  color: var(--ink);
}

.muted {
  color: var(--muted-3);
}

.table-panel {
  margin: 0 20px 24px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  /* 讓內層可橫向滾，不被父層撐滿鎖死 */
  min-width: 0;
}

.table-scroll {
  overflow-x: auto;
  overflow-y: auto;
  max-height: calc(100vh - 200px);
  max-width: 100%;
  -webkit-overflow-scrolling: touch;
}

table {
  width: max-content;
  min-width: max-content;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 12px;
  table-layout: auto;
}

th,
td {
  border-bottom: 1px solid var(--line-2);
  border-right: 1px solid var(--line-2);
  padding: 6px 7px;
  text-align: center;
  white-space: nowrap;
  vertical-align: middle;
  /* 禁止被壓窄導致「看起來鎖死、不能橫滑」 */
  min-width: max-content;
}

th:last-child,
td:last-child {
  border-right: none;
}

thead th {
  position: sticky;
  z-index: 3;
  font-weight: 700;
  font-size: 11px;
  color: var(--ink);
  box-sizing: border-box;
}

thead tr.head-groups th {
  top: 0;
  z-index: 4;
  height: var(--thead-h1);
  min-height: var(--thead-h1);
  max-height: var(--thead-h1);
  padding-top: 0;
  padding-bottom: 0;
  line-height: var(--thead-h1);
  vertical-align: middle;
}

thead tr.sub th {
  top: var(--thead-h1);
  z-index: 3;
  height: var(--thead-h2);
  min-height: var(--thead-h2);
  max-height: var(--thead-h2);
  font-weight: 400;
  font-size: 10px;
  color: var(--muted);
  background: #fafaf8;
  cursor: pointer;
  user-select: none;
  padding: 0 4px;
  line-height: var(--thead-h2);
  vertical-align: middle;
}

thead tr.sub th .th-label {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  max-width: 100%;
}

thead tr.sub th .sort-arrow {
  display: inline-block;
  width: 0.85em;
  min-width: 0.85em;
  margin-left: 1px;
  font-size: 10px;
  line-height: 1;
  text-align: left;
  visibility: hidden;
}

thead tr.sub th.is-sorted .sort-arrow {
  visibility: visible;
  color: var(--sort);
}

thead tr.sub th:hover {
  color: var(--sort);
}

thead tr.sub th.is-sorted {
  color: var(--sort);
  font-weight: 700;
}

thead th.group-entry {
  background: var(--group-entry);
}

thead th.group-same {
  background: var(--group-same);
}

thead th.group-dist {
  background: var(--group-dist);
}

thead th.group-anal {
  background: var(--group-anal);
}

thead th.group-l1 {
  background: var(--group-l1);
}

thead th.group-r1 {
  background: var(--group-r1);
}

thead tr.sub th.sub-entry {
  background: var(--group-entry);
}

thead tr.sub th.sub-same {
  background: #fff8f7;
}

thead tr.sub th.sub-dist {
  background: #fffdf3;
}

thead tr.sub th.sub-anal {
  background: #f8fff4;
}

thead tr.sub th.sub-l1 {
  background: #f3e8ff;
  color: #6b21a8;
}

thead tr.sub th.sub-r1 {
  background: #f3e8ff;
  color: #6b21a8;
}

th.entry-sort-group {
  text-align: left;
  width: 1px;
  max-width: max-content;
  white-space: nowrap;
  padding-left: 8px;
  padding-right: 8px;
}

th.entry-sort {
  position: sticky;
  overflow: visible;
  vertical-align: middle;
  text-align: left;
  width: 1px;
  max-width: max-content;
  white-space: nowrap;
  padding-left: 8px;
  padding-right: 8px;
  cursor: default;
}

.entry-head-title {
  display: block;
  font-size: 11px;
  font-weight: 700;
  color: var(--ink);
  margin-bottom: 4px;
}

.sort-trigger {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--sort);
  font-weight: 700;
  font-size: 11px;
  cursor: pointer;
  border: none;
  background: transparent;
  padding: 0;
  font-family: inherit;
}

.sort-trigger:hover {
  text-decoration: underline;
}

.sort-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 8px;
  z-index: 20;
  min-width: 140px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(26, 26, 26, 0.12);
  padding: 6px 0;
  text-align: left;
}

.sort-menu[hidden] {
  display: none;
}

.sort-menu button {
  display: flex;
  width: 100%;
  align-items: center;
  gap: 8px;
  border: none;
  background: transparent;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--ink);
  cursor: pointer;
  font-family: inherit;
}

.sort-menu button:hover {
  background: var(--group-entry);
}

.sort-menu button.is-on {
  color: var(--sort);
  font-weight: 700;
}

.sort-menu .check {
  width: 14px;
  color: var(--sort);
}

td.horse-cell {
  text-align: left;
  /* 同表頭：欄寬≈騎師練馬師行＋padding，不吃多餘空間 */
  width: 1px;
  max-width: max-content;
  white-space: nowrap;
  padding-left: 8px;
  padding-right: 8px;
}

td.horse-cell .horse-main {
  display: inline;
}

td.horse-cell .name {
  font-weight: 700;
  color: var(--ink);
}

td.horse-cell .odd {
  font-weight: 700;
  font-size: 12px;
  color: var(--ink);
  margin-left: 0.35em;
}

td.horse-cell .odd.is-hot {
  color: var(--chip-red);
}

td.horse-cell .odd.is-scr {
  color: var(--scr);
}

td.horse-cell .sub {
  display: block;
  margin-top: 2px;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}

.badge {
  display: inline-flex;
  min-width: 22px;
  height: 20px;
  padding: 0 4px;
  align-items: center;
  justify-content: center;
  border-radius: 3px;
  font-size: 11px;
  font-weight: 700;
}

.badge-red {
  background: var(--chip-red);
  color: #fff;
}

.badge-purple {
  background: var(--chip-purple);
  color: #fff;
}

.badge-yellow {
  background: var(--chip-yellow);
  color: var(--ink);
}

td.heat-hi {
  background: var(--heat-hi-bg);
  color: var(--heat-hi-fg);
  font-weight: 700;
}

td.heat-mid {
  background: var(--heat-mid-bg);
  color: var(--heat-mid-fg);
  font-weight: 700;
}

td.heat-lo {
  background: var(--heat-lo-bg);
  color: var(--heat-lo-fg);
  font-weight: 700;
}

td.sprint-neg {
  color: #b42318;
  font-weight: 700;
}

td.scr {
  color: var(--scr);
  font-weight: 700;
}

tr.is-scr {
  opacity: 0.7;
}

@media (max-width: 1100px) {
  .summary-bar {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .summary-live {
    text-align: left;
  }

  .topbar {
    flex-direction: column;
    align-items: flex-start;
  }
}
