:root {
  --green: #1f6f4a;
  --green-dark: #17553a;
  --ball: #d9e84a;
  --bg: #f4f6f3;
  --card: #ffffff;
  --text: #1b2420;
  --muted: #5f6b65;
  --line: #dde3df;
  --danger: #b3261e;
  --radius: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111614;
    --card: #1b221f;
    --text: #e8eee9;
    --muted: #9aa7a0;
    --line: #2d3732;
    --green: #3aa673;
    --green-dark: #2f8c60;
    --danger: #f2817a;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: env(safe-area-inset-bottom);
}

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--green); color: #fff;
}
.brand { color: #fff; font-weight: 700; font-size: 1.15rem; text-decoration: none; }
.brand::before { content: ""; display: inline-block; width: .8em; height: .8em; margin-right: .45em; border-radius: 50%; background: var(--ball); vertical-align: -.05em; }
.icon-btn { background: transparent; border: 0; color: #fff; font-size: 1.4rem; width: 44px; height: 44px; border-radius: 50%; cursor: pointer; }
.icon-btn:active { background: rgba(255,255,255,.15); }

main { max-width: 560px; margin: 0 auto; padding: 16px 14px 48px; }
main.busy { opacity: .6; pointer-events: none; }

h1 { font-size: 1.35rem; margin: 4px 0 2px; }
h2 { font-size: 1.05rem; margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.center { text-align: center; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin: 14px 0;
}
.card.highlight { border: 2px solid var(--green); }

.chip { display: inline-block; font-size: .78rem; font-weight: 600; padding: 2px 9px; border-radius: 999px; background: var(--line); color: var(--text); vertical-align: middle; }
.chip.live { background: var(--green); color: #fff; }

label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 4px; }
input[type="text"], input[type="number"], input[type="password"], input[type="date"] {
  width: 100%; font: inherit; font-size: 1.05rem; color: var(--text);
  padding: 11px 12px; min-height: 46px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
}
input:focus-visible, button:focus-visible { outline: 3px solid var(--ball); outline-offset: 1px; }

.row { display: flex; gap: 10px; align-items: flex-end; }
.row > .grow { flex: 1; min-width: 0; }
.row > .skill-field { width: 84px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; min-height: 46px; padding: 0 18px;
  border-radius: 10px; border: 1px solid transparent; cursor: pointer;
  background: var(--green); color: #fff; text-decoration: none;
}
.btn:active { background: var(--green-dark); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.block { width: 100%; margin-top: 12px; font-size: 1.05rem; min-height: 52px; }
.btn.secondary { background: transparent; color: var(--green); border-color: var(--green); }
.btn.link { background: transparent; color: var(--green); padding: 0 6px; }
.btn.danger { background: transparent; color: var(--danger); border-color: transparent; }
.btn.danger.armed { background: var(--danger); color: #fff; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.btn-row > .btn { flex: 1; }

/* Players */
.players { list-style: none; margin: 12px 0 0; padding: 0; }
.players li { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.players li:first-child { border-top: 0; }
.players .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.players input[type="number"] { width: 70px; min-height: 40px; padding: 6px 8px; text-align: center; }
.remove { background: transparent; border: 0; color: var(--muted); font-size: 1.4rem; width: 40px; height: 40px; border-radius: 50%; cursor: pointer; }
.remove:active { background: var(--line); color: var(--danger); }
.count { margin-top: 10px; font-size: .9rem; }
.count.warn { color: var(--danger); }

/* Mode selector */
.modes { border: 0; padding: 0; margin: 0; display: grid; gap: 10px; }
.modes legend { font-weight: 700; margin-bottom: 8px; padding: 0; }
.mode { display: flex; gap: 12px; align-items: flex-start; padding: 12px; border: 2px solid var(--line); border-radius: 12px; cursor: pointer; font-weight: 400; margin: 0; }
.mode:has(input:checked) { border-color: var(--green); background: color-mix(in srgb, var(--green) 8%, transparent); }
.mode input { margin-top: 4px; width: 20px; height: 20px; accent-color: var(--green); flex: none; }
.mode strong { display: block; }
.mode span { color: var(--muted); font-size: .9rem; }

/* Courts and matches */
.court { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin-top: 12px; }
.court-title { display: flex; justify-content: space-between; align-items: center; font-size: .85rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-bottom: 8px; }
.team { display: flex; align-items: center; gap: 10px; padding: 6px 0; }
.team .names { flex: 1; min-width: 0; font-size: 1.05rem; }
.team.won .names { font-weight: 700; }
.team input { width: 72px; text-align: center; font-size: 1.25rem; font-weight: 700; }
.vs { font-size: .8rem; color: var(--muted); text-align: center; margin: -2px 0; }
.saved { color: var(--green); font-weight: 600; font-size: .85rem; text-transform: none; letter-spacing: 0; }

.past-match { display: flex; justify-content: space-between; gap: 10px; padding: 6px 0; border-top: 1px solid var(--line); font-size: .95rem; }
.past-match:first-of-type { border-top: 0; }
.past-match .score { font-variant-numeric: tabular-nums; font-weight: 700; white-space: nowrap; }
.past-match b { font-weight: 700; }
details.round { border-top: 1px solid var(--line); padding: 8px 0; }
details.round:first-of-type { border-top: 0; }
details.round summary { cursor: pointer; font-weight: 600; padding: 4px 0; }

/* Round robin round picker */
.rr-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.rr-head h2 { margin: 0; }
.round-picker {
  display: flex; gap: 8px; overflow-x: auto; padding: 10px 2px 6px; margin: 0 -2px;
  scrollbar-width: thin; -webkit-overflow-scrolling: touch;
}
.round-chip {
  flex: none; position: relative; width: 44px; height: 44px; border-radius: 50%;
  font: inherit; font-weight: 700; cursor: pointer;
  background: var(--bg); color: var(--text); border: 2px solid var(--line);
}
.round-chip.partial { border-color: var(--green); border-style: dashed; }
.round-chip.done { background: color-mix(in srgb, var(--green) 18%, var(--card)); border-color: var(--green); }
.round-chip.done::after {
  content: "\2713"; position: absolute; right: -3px; top: -3px; width: 18px; height: 18px; border-radius: 50%;
  background: var(--green); color: #fff; font-size: .7rem; line-height: 18px; text-align: center;
}
.round-chip.selected { background: var(--green); border-color: var(--green); color: #fff; border-style: solid; }

.done-note {
  margin: 14px 0 4px; padding: 12px 14px; border-radius: 10px; text-align: center;
  background: color-mix(in srgb, var(--green) 14%, transparent); font-weight: 500;
}

/* Standings */
table.standings { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.standings th, .standings td { padding: 7px 4px; text-align: right; border-top: 1px solid var(--line); }
.standings th { font-size: .78rem; color: var(--muted); font-weight: 600; border-top: 0; }
.standings th:nth-child(2), .standings td:nth-child(2) { text-align: left; }
.standings td:first-child, .standings th:first-child { width: 2em; text-align: left; color: var(--muted); }

/* Home */
.section-title { font-size: .85rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 6px 2px -4px; }
.live-session {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  border: 2px solid var(--green); color: var(--text); text-decoration: none; padding: 18px 16px;
}
.live-session:active { background: color-mix(in srgb, var(--green) 10%, var(--card)); }
.live-session strong { display: block; font-size: 1.15rem; margin-bottom: 4px; }
.live-session .go { color: var(--green); font-weight: 700; white-space: nowrap; }
.summary-btn {
  list-style: none; cursor: pointer; font-weight: 600; color: var(--green);
  min-height: 44px; display: flex; align-items: center;
}
.summary-btn::-webkit-details-marker { display: none; }
.summary-btn::before { content: "+"; font-size: 1.3rem; margin-right: 8px; }
details[open] > .summary-btn::before { content: "\2212"; }

.sessions { list-style: none; padding: 0; margin: 0; }
.sessions a { display: flex; justify-content: space-between; align-items: center; padding: 12px 2px; border-top: 1px solid var(--line); color: var(--text); text-decoration: none; }
.sessions li:first-child a { border-top: 0; }

#toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: calc(100% - 28px); width: max-content;
  background: #2b2b2b; color: #fff; padding: 12px 16px; border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0,0,0,.25); z-index: 10; font-size: .95rem;
}
#toast.error { background: var(--danger); color: #fff; }
@media (prefers-color-scheme: dark) { #toast.error { background: #8c1d18; } }
