:root {
  --bg: #eef3f1;
  --surface: #ffffff;
  --surface-2: #f5f8f7;
  --ink: #0f2027;
  --muted: #5a6b72;
  --line: #dbe4e1;
  --accent: #0d6b5c;
  --accent-2: #e3f1ed;
  --accent-ink: #ffffff;
  --ball: #c9e62e;
  --ball-ink: #26300a;
  --warn: #a4501a;
  --warn-bg: #fbefe5;
  --good: #19795f;
  --sel: #f3b81f;
  --shadow: 0 1px 2px rgba(15, 32, 39, .06), 0 4px 16px rgba(15, 32, 39, .05);
  --r: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0a1316; --surface: #12201f; --surface-2: #172826; --ink: #e6efed; --muted: #92a6a3;
    --line: #233533; --accent: #34b296; --accent-2: #173a33; --accent-ink: #04130f;
    --ball: #d4ef3a; --ball-ink: #1d2507; --warn: #f0a36c; --warn-bg: #33221a; --good: #5fd3b1;
    --sel: #f3c443; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0a1316; --surface: #12201f; --surface-2: #172826; --ink: #e6efed; --muted: #92a6a3;
  --line: #233533; --accent: #34b296; --accent-2: #173a33; --accent-ink: #04130f;
  --ball: #d4ef3a; --ball-ink: #1d2507; --warn: #f0a36c; --warn-bg: #33221a; --good: #5fd3b1;
  --sel: #f3c443; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 18px; height: 18px; flex: none; }
#app { max-width: 760px; margin: 0 auto; padding: 0 16px 40px; }

/* header + tabs */
.top { padding: max(18px, env(safe-area-inset-top)) 0 10px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.scorer-link { flex: none; padding: 8px 14px; border-radius: 999px; background: var(--accent-2); color: var(--accent); font-weight: 600; font-size: 14px; text-decoration: none; }
.brand { display: flex; align-items: center; gap: 12px; }
.brand svg { width: 40px; height: 40px; }
.brand h1 { margin: 0; font-size: 22px; letter-spacing: -.01em; }
.brand p { margin: 0; color: var(--muted); font-size: 13px; }
.tabs {
  position: sticky; top: 0; z-index: 5; display: flex; gap: 4px; padding: 8px 0;
  background: var(--bg);
}
.tab {
  flex: 1; border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 9px 10px; font-weight: 600; display: flex; align-items: center; justify-content: center; gap: 6px;
}
.tab.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.badge { font-size: 11px; background: var(--accent-2); color: var(--accent); border-radius: 999px; padding: 0 7px; line-height: 18px; }
.tab.on .badge { background: rgba(255, 255, 255, .22); color: inherit; }
.badge.live { background: var(--ball); color: var(--ball-ink); }

/* cards */
main { display: flex; flex-direction: column; gap: 12px; padding-top: 4px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 16px; box-shadow: var(--shadow); }
.card h2 { margin: 0 0 10px; font-size: 17px; }
.card h3 { margin: 0; font-size: 15px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 8px; }
.card-head h2 { margin: 0; }
.hint { color: var(--muted); font-size: 13px; margin: 6px 0 10px; }
.hint.center { text-align: center; }
.meta { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.warn { background: var(--warn-bg); color: var(--warn); border-radius: 10px; padding: 10px 12px; margin: 8px 0; font-size: 14px; }
.summary { margin: 0 0 12px; }
.rules { color: var(--muted); font-size: 13px; margin: 12px 0 0; padding-top: 12px; border-top: 1px dashed var(--line); }
.foot { text-align: center; color: var(--muted); font-size: 12px; margin-top: 24px; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 40px;
  padding: 8px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface-2); font-weight: 600;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 34px; padding: 5px 10px; font-size: 14px; }
.btn.wide { width: 100%; }
.btn.big { min-height: 50px; font-size: 16px; }
.btn:disabled { opacity: .45; cursor: not-allowed; filter: none; }
.link { background: none; border: 0; color: var(--accent); font-weight: 600; padding: 2px 6px; font-size: 13px; }
.icon { background: none; border: 0; width: 34px; height: 34px; border-radius: 8px; display: grid; place-items: center; color: var(--muted); }
.icon:hover { background: var(--surface-2); color: var(--warn); }
.mini { display: flex; gap: 2px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* players */
.add-row { display: flex; gap: 8px; }
.add-row input { flex: 1; min-width: 0; }
input:not([type]), input[type="number"], select, textarea {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 10px; padding: 9px 12px; min-height: 40px;
}
textarea { width: 100%; resize: vertical; margin: 8px 0; }
.bulk summary { cursor: pointer; color: var(--accent); font-weight: 600; font-size: 14px; margin-top: 10px; }
.plist { list-style: none; margin: 0 0 12px; padding: 0; }
.prow { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); }
.prow:first-child { border-top: 0; }
.prow.off .pname, .prow.off .stepper, .prow.off .gender { opacity: .45; }
.check {
  width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--line); background: transparent;
  display: grid; place-items: center; color: transparent; flex: none;
}
.check[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.pname { flex: 1; min-width: 0; border-color: transparent !important; background: transparent !important; padding: 6px 8px !important; font-weight: 600; }
.pname:focus { border-color: var(--line) !important; background: var(--surface-2) !important; }
.stepper { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); flex: none; }
.stepper button { background: none; border: 0; width: 30px; height: 32px; display: grid; place-items: center; color: var(--muted); }
.stepper button:hover { color: var(--accent); }
.stepper span { min-width: 30px; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.stepper.big button { width: 44px; height: 42px; }
.stepper.big span { min-width: 40px; font-size: 18px; }
.gender { width: 34px; height: 32px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface-2); font-weight: 700; flex: none; color: var(--muted); }
.gender.gM { background: #dfeafe; color: #2456b3; border-color: #bcd0f7; }
.gender.gF { background: #fbe3ee; color: #a8336a; border-color: #f2bfd5; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .gender.gM { background: #1a2a45; color: #8fb3ff; border-color: #2b416a; }
  :root:not([data-theme="light"]) .gender.gF { background: #3b1d2b; color: #f59cc5; border-color: #5a2c42; }
}

/* setup */
.fmts { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.fmt {
  text-align: left; border: 1.5px solid var(--line); background: var(--surface-2); border-radius: 12px; padding: 12px;
  display: flex; flex-direction: column; gap: 2px;
}
.fmt b { font-size: 15px; }
.fmt small { color: var(--accent); font-weight: 600; font-size: 12px; }
.fmt span { color: var(--muted); font-size: 12.5px; margin-top: 4px; }
.fmt.on { border-color: var(--accent); background: var(--accent-2); box-shadow: inset 0 0 0 1px var(--accent); }
.fields { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
.field > label { font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 6px; }
.field > label em { font-style: normal; color: var(--muted); font-weight: 500; font-size: 12px; }
.field small { color: var(--muted); font-size: 12.5px; }
.field select { width: 100%; }
.field .warn { font-size: 13px; margin: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--surface-2); border-radius: 999px; padding: 4px 11px; font-size: 13px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 4px;
}
.chip svg { width: 14px; height: 14px; }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chip.add { border-style: dashed; color: var(--muted); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--surface-2); }
.seg button { border: 0; background: none; padding: 8px 16px; font-weight: 600; border-left: 1px solid var(--line); }
.seg button:first-child { border-left: 0; }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.toggle {
  display: flex; gap: 12px; align-items: flex-start; width: 100%; text-align: left; background: none; border: 0;
  padding: 12px 0 0; margin-top: 12px; border-top: 1px solid var(--line);
}
.toggle b { display: block; }
.toggle small { color: var(--muted); font-size: 13px; }
.sw { width: 42px; height: 24px; border-radius: 999px; background: var(--line); position: relative; flex: none; margin-top: 1px; transition: background .15s; }
.sw::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.25); }
.toggle.on .sw { background: var(--accent); }
.toggle.on .sw::after { transform: translateX(18px); }
.go .summary { font-size: 14.5px; }

/* session */
.fair { display: grid; grid-template-columns: repeat(auto-fit, minmax(105px, 1fr)); gap: 8px; margin: 8px 0 12px; }
.fchip { border-radius: 10px; padding: 8px 10px; background: var(--surface-2); border: 1px solid var(--line); }
.fchip b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.fchip span { color: var(--muted); font-size: 12px; }
.fchip.good b { color: var(--good); }
.fchip.meh b { color: var(--warn); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.banner {
  margin-top: 12px; padding: 10px 12px; border-radius: 10px; background: var(--warn-bg); color: var(--warn);
  display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 14px; flex-wrap: wrap;
}
.roster { margin-top: 12px; padding-top: 4px; border-top: 1px solid var(--line); }
.teams { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.teams li { display: flex; align-items: center; gap: 6px; padding: 6px 8px; border-radius: 10px; background: var(--surface-2); }
.tno { width: 24px; height: 24px; border-radius: 50%; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-size: 12px; font-weight: 700; flex: none; }
.amp { color: var(--muted); }
.teams .lvl { margin-left: auto; }

.round.done { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.rstat { color: var(--good); font-size: 13px; font-weight: 600; display: inline-flex; align-items: center; gap: 4px; }
.rstat svg { width: 15px; height: 15px; }
.match {
  display: grid; grid-template-columns: 1fr auto 1fr; grid-template-areas: "court court court" "a vs b";
  gap: 8px 10px; align-items: stretch; padding: 10px 0; border-top: 1px solid var(--line);
}
.court { grid-area: court; font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.side {
  grid-area: a; display: flex; flex-direction: column; gap: 8px; padding: 8px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface-2);
}
.side.right { grid-area: b; }
.side.win { background: var(--accent-2); border-color: color-mix(in srgb, var(--accent) 45%, transparent); }
.side.lost { opacity: .7; }
.names { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.names .pl { background: var(--surface); }
.stay { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--warn); }
.won { font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: .05em; color: var(--accent); }
.vs { grid-area: vs; align-self: center; color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; }
.wins { display: flex; gap: 6px; margin-top: auto; }
.res {
  flex: 1; min-height: 38px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
  font-weight: 800; font-size: 15px; font-variant-numeric: tabular-nums;
}
.res:hover { border-color: var(--accent); }
.res.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.fixed { font-size: 16px; padding: 8px 0 2px; }
.pl {
  display: inline-block; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line);
  font-weight: 600; font-size: 14px; line-height: 1.3; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
button.pl:hover { border-color: var(--accent); }
.pl.sel { background: var(--sel); border-color: var(--sel); color: #2a1f00; }
.lvl { font-size: 11px; color: var(--muted); font-weight: 600; padding: 0 2px; font-variant-numeric: tabular-nums; }
.lvl::before { content: "lvl "; opacity: .7; }
.rest { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding-top: 10px; border-top: 1px dashed var(--line); }
.rest > span { color: var(--muted); font-size: 13px; margin-right: 2px; }
.rest .pl { opacity: .8; }
.after { display: flex; flex-direction: column; gap: 8px; align-items: center; }
.after .btn:not(.wide) { min-width: 200px; }

/* standings */
.tablewrap { overflow-x: auto; margin: 0 -4px; }
.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th, .table td { padding: 8px 6px; text-align: center; border-bottom: 1px solid var(--line); }
.table th { font-size: 12px; color: var(--muted); font-weight: 600; }
.table .l { text-align: left; }
.table td.l { font-weight: 600; }
.table tr.top1 td:first-child { color: #b88a00; font-weight: 800; }
.table tr.top2 td:first-child { color: #7b8a91; font-weight: 800; }
.table tr.top3 td:first-child { color: #a0623a; font-weight: 800; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: calc(18px + env(safe-area-inset-bottom)); transform: translate(-50%, 140%);
  background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: 12px; display: flex; gap: 14px; align-items: center;
  max-width: calc(100% - 32px); transition: transform .2s ease; z-index: 20; font-size: 14px; box-shadow: 0 6px 24px rgba(0,0,0,.2);
}
.toast.show { transform: translate(-50%, 0); }
.toast button { background: none; border: 0; color: var(--ball); font-weight: 700; padding: 0; }

@media (max-width: 640px) {
  .fmts { grid-template-columns: 1fr; }
  .fmt span { font-size: 12px; }
  .fields { grid-template-columns: 1fr 1fr; }
  .fields .field:last-child { grid-column: 1 / -1; }
  .match { grid-template-columns: 1fr; grid-template-areas: "court" "a" "vs" "b"; gap: 6px; }
  .side { flex-direction: row; align-items: center; flex-wrap: wrap; }
  .names { flex: 1 1 150px; }
  .wins { margin-top: 0; flex: 0 0 auto; }
  .res { min-width: 50px; }
  .vs { text-align: center; }
}
@media (max-width: 380px) {
  .prow { gap: 4px; }
  .stepper button { width: 26px; }
}

@media print {
  body { background: #fff; color: #000; }
  .tabs, .actions, .after, .top p, .hint, .btn, .toast, .foot, .banner, .fair { display: none !important; }
  .card { box-shadow: none; break-inside: avoid; border-color: #ccc; }
  .res:not(.on) { display: none; }
  .side { border-color: #ccc; }
}
