:root {
  --bg: #f4f7f6; --card: #fff; --text: #17302d; --muted: #5f7471; --border: #dbe5e3;
  --accent: #0f766e; --accent-ink: #fff; --chip: #eef3f2;
  --win: #15803d; --win-bg: #dcfce7; --loss: #b91c1c; --next: #ecfdf9; --danger: #b91c1c;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c1615; --card: #142321; --text: #e3efed; --muted: #8fa8a4; --border: #263d3a;
    --accent: #2dd4bf; --accent-ink: #052e2b; --chip: #1c2f2c;
    --win: #86efac; --win-bg: #14532d; --loss: #fca5a5; --next: #10302c; --danger: #fca5a5;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
.top {
  position: sticky; top: 0; z-index: 2; display: flex; justify-content: space-between; align-items: center;
  gap: 12px; padding: 10px max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
h1 { margin: 0; font-size: 1.15rem; }
h2 { margin: 0 0 8px; font-size: 1.05rem; }
.progress { color: var(--muted); font-size: .9rem; font-variant-numeric: tabular-nums; }
main { max-width: 640px; margin: 0 auto; padding: 16px; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.hint { margin: 8px 0 0; color: var(--muted); font-size: .85rem; }

fieldset { border: 0; margin: 0 0 12px; padding: 0; }
legend { padding: 0; margin-bottom: 6px; font-size: .85rem; font-weight: 600; color: var(--muted); }
.seg { display: flex; gap: 6px; }
.seg button {
  flex: 1; min-height: 44px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--chip); color: var(--text); font: inherit; font-weight: 600;
}
.seg button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.seg button:disabled { opacity: .5; }
.names { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
input[type="text"] {
  width: 100%; min-height: 44px; padding: 8px 12px; font: inherit; color: inherit;
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
}
button { font: inherit; cursor: pointer; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary {
  width: 100%; min-height: 48px; margin-top: 14px; border: 0; border-radius: 12px;
  background: var(--accent); color: var(--accent-ink); font-weight: 700;
}
button.ghost { min-height: 44px; border: 1px solid var(--border); border-radius: 12px; background: transparent; color: var(--text); }
button.danger { color: var(--danger); }

.champion {
  margin-bottom: 16px; padding: 14px; text-align: center; font-weight: 700; font-size: 1.1rem;
  border-radius: var(--radius); background: var(--win-bg); color: var(--win);
}

.matches { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.match { padding: 10px; border: 1px solid var(--border); border-radius: 12px; }
.match.next { background: var(--next); border-color: var(--accent); }
.match-head { display: flex; justify-content: space-between; font-size: .8rem; color: var(--muted); margin-bottom: 6px; }
.match.next .match-head b { color: var(--accent); }
.sides { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; align-items: center; }
.vs { color: var(--muted); font-size: .8rem; }
.side {
  min-height: 48px; padding: 6px 8px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--chip); color: var(--text); overflow-wrap: anywhere;
}
.side[aria-pressed="true"] { background: var(--win-bg); border-color: var(--win); color: var(--win); font-weight: 700; }
.side[aria-pressed="true"]::before { content: "✓ "; }

table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: 10px 4px; text-align: center; border-bottom: 1px solid var(--border); }
th { font-size: .8rem; color: var(--muted); font-weight: 600; }
.left { text-align: left; }
td.win { color: var(--win); font-weight: 700; }
td.loss { color: var(--loss); }
tr.first td.name { color: var(--accent); font-weight: 700; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 32px; }
.actions .primary { grid-column: 1 / -1; margin-top: 0; }

@media print {
  body { background: #fff; color: #000; }
  .top, .actions, #setup, .hint { display: none !important; }
  .card, .match { border-color: #999; break-inside: avoid; }
  .side[aria-pressed="true"] { background: #ddd; color: #000; }
}
