:root {
  color-scheme: light dark;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--bg);
  --bg: Canvas;
  --ink: CanvasText;
  --muted: GrayText;
  --line: ButtonBorder;
  --divider: ButtonBorder;
  --surface: Canvas;
  --field: Field;
  --accent: LinkText;
  --selected: ButtonFace;
  --quiet: ButtonFace;
  --own: #ffdf56;
  --own-ink: #201800;
  --hold: #17663a;
  --save: #215da5;
  --steal: #ad2337;
  --wrong: #222;
  --score-ink: #fff;
  --radius: 2px;
  font-synthesis: none;
}

:root[data-color-mode="light"] { color-scheme: light; }
:root[data-color-mode="dark"] { color-scheme: dark; }

@supports (color: light-dark(white, black)) {
  :root {
    --bg: light-dark(#f2f2f2, #252525);
    --ink: light-dark(#171717, #eee);
    --muted: light-dark(#555, #bbb);
    --line: light-dark(#aaa, #777);
    --divider: light-dark(#d5d5d5, #545454);
    --surface: light-dark(#fff, #303030);
    --field: light-dark(#fff, #222);
    --accent: light-dark(#245a8e, #a6cff5);
    --selected: light-dark(#dce8f3, #3b4a58);
    --quiet: light-dark(#ededed, #373737);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
body { margin: 0; line-height: 1.25; }
h1, h2, h3, p { margin: 0; }
h2 { font-size: 1rem; }
a { color: var(--accent); text-underline-offset: 0.13em; }
button, input, select { font: inherit; color: inherit; touch-action: manipulation; }
button {
  min-block-size: 2.15rem;
  padding: 0.25rem 0.6rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}
button:hover:not(:disabled), button[aria-pressed="true"] { background: var(--selected); }
button:disabled { opacity: 0.55; cursor: default; }
button[aria-pressed="true"] { border-color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.masthead { border-block-end: 1px solid var(--line); }
.masthead-inner, .page { width: calc(100% - 1.25rem); margin-inline: auto; }
.masthead-inner, .masthead-title, .masthead-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 0.35rem 0.75rem; }
.masthead-inner { min-height: 2.7rem; padding-block: 0.23rem; justify-content: space-between; }
.masthead h1 { font-size: 1rem; }
.masthead nav a { display: inline-flex; align-items: center; min-height: 2.15rem; white-space: nowrap; }
.brand { font-size: 0.88rem; }
.theme-control { display: flex; align-items: center; gap: 0.3rem; font-size: 0.9rem; }
.theme-control select { min-height: 2.15rem; padding: 0.2rem 0.3rem; background: var(--field); border: 1px solid var(--line); border-radius: var(--radius); }
.page { padding-block: 0.55rem 1.2rem; }
.notice { background: var(--selected); border: 1px solid var(--line); padding: 0.4rem 0.55rem; margin-bottom: 0.5rem; }
.section-header, .match-toolbar, .match-footer, .dialog-heading { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.4rem; }
.section-header { margin-bottom: 0.65rem; }
.section-header p, .match-footer p { color: var(--muted); margin-block-start: 0.25rem; }
.tools, .form-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 0.25rem; }
.form-actions { margin-block-start: 0.55rem; justify-content: flex-end; }
.setup-hint { color: var(--muted); font-size: 0.88rem; }
.primary { background: var(--selected); border-color: var(--accent); font-weight: 650; padding-inline: 1rem; }
.primary:hover:not(:disabled) { text-decoration: underline; }
.panel { margin: 0; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); padding: 0.4rem 0.55rem 0.6rem; min-inline-size: 0; }
.panel legend { font-weight: 700; padding-inline: 0.4rem; }
.fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.6rem 1rem; }
.field { display: grid; min-inline-size: 0; gap: 0.25rem; align-content: start; color: var(--muted); font-size: 0.9rem; font-weight: 600; }
.field input, .field select { width: 100%; min-inline-size: 0; min-height: 2.15rem; padding: 0.25rem 0.4rem; border-radius: var(--radius); border: 1px solid var(--line); background: var(--field); color: var(--ink); font-size: 1rem; font-weight: 400; }
.field input:disabled { background: var(--quiet); color: var(--muted); }
.team-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.35rem; }
#lineups, .start-panel { margin-block-start: 0.55rem; }
.lineup legend { max-inline-size: 100%; overflow-wrap: anywhere; }
.lineup-fields { display: grid; grid-template-columns: minmax(0, 1fr) minmax(2.4rem, 16%) auto; gap: 0.25rem 0.35rem; }
.lineup-label { text-align: center; color: var(--muted); font-size: 0.8rem; }
.lineup-fields input[type="number"] { padding-inline: 0.25rem; }
.captain-seat, .captain-none { display: flex; align-items: center; gap: 0.25rem; min-block-size: 1.65rem; }
.captain-seat { min-inline-size: 1.75rem; justify-content: center; }
.captain-none { margin-block-start: 0.25rem; font-size: 0.85rem; }
.captain-seat input, .captain-none input { margin: 0; accent-color: var(--accent); width: 1rem; height: 1rem; }
.setup-details { margin-block-start: 0.4rem; }
.setup-details summary, .sheet-attempts summary { cursor: pointer; }
.setup-details .fields { margin-top: 0.5rem; }
.choices { display: flex; flex-wrap: wrap; gap: 0.4rem 1rem; }
.choices label { display: flex; align-items: center; gap: 0.3rem; min-height: 2.2rem; overflow-wrap: anywhere; }
.choices input { accent-color: var(--accent); width: 1rem; height: 1rem; flex: none; }
.first-row { display: flex; align-items: center; flex-wrap: wrap; gap: 0.25rem 0.7rem; }
.first-row output { color: var(--muted); font-size: 0.9rem; font-weight: 600; }
.first-row output:empty { display: none; }
#coin { display: grid; place-items: center; inline-size: 3.1rem; padding: 0; perspective: 5rem; }
#coin[aria-disabled="true"] { cursor: wait; }
.coin-disc { display: grid; inline-size: 1.45em; block-size: 1.45em; transform: rotateY(var(--face-angle, 0deg)); transform-style: preserve-3d; }
.coin-disc[data-face="T"] { --face-angle: 180deg; }
.coin-face { grid-area: 1 / 1; display: grid; place-items: center; border-radius: 50%; border: 1px solid var(--line); background: var(--own); color: var(--own-ink); font-weight: 700; backface-visibility: hidden; }
.coin-back { transform: rotateY(180deg); }
@media (prefers-reduced-motion: no-preference) {
  .coin-disc.spinning { animation: coin-spin 3s ease-out; }
}
@keyframes coin-spin {
  from { transform: rotateY(0deg); }
  to { transform: rotateY(calc(1800deg + var(--face-angle, 0deg))); }
}
.icon-control { display: grid; place-items: center; min-inline-size: 2.2rem; min-block-size: 2.15rem; padding: 0; font: 600 1.3rem/1 system-ui, sans-serif; }
.match-menu { position: relative; z-index: 2; }
.match-menu summary { list-style: none; cursor: pointer; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.match-menu summary::-webkit-details-marker { display: none; }
.match-menu[open] summary { background: var(--selected); }
.match-menu-items { position: absolute; inset-block-start: calc(100% + 0.2rem); inset-inline-end: 0; display: grid; gap: 0.2rem; min-inline-size: max-content; padding: 0.25rem; background: var(--surface); border: 1px solid var(--line); }
.match-menu-items button { text-align: start; }
.clock { font: 1rem/1.2 monospace; padding: 0.3rem 0.5rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); white-space: nowrap; }
.score-strip { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: stretch; gap: 0.35rem; margin-block: 0.4rem; }
.score-strip > button { padding-inline: clamp(0.55rem, 2vw, 1.4rem); }
.turn { text-align: center; background: var(--selected); border: 1px solid var(--line); border-radius: var(--radius); padding: 0.35rem 0.45rem; overflow-wrap: anywhere; }
.turn h2 { font-variant-numeric: tabular-nums; }
.turn p { color: var(--muted); font-size: 0.88rem; margin-top: 0.15rem; }
.team { min-inline-size: 0; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.team-head { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 0.2rem 0.5rem; align-items: center; padding: 0.35rem 0.55rem; }
.team-name-block { min-inline-size: 0; }
.team-name { color: var(--ink); font-size: clamp(1rem, 1.5vw, 1.25rem); overflow-wrap: anywhere; }
.team-score { font-size: clamp(1.65rem, 4.7vh, 2.75rem); line-height: 1; font-variant-numeric: tabular-nums; text-align: end; }
.team-score-info { font-size: 0.83rem; color: var(--muted); grid-column: 1 / -1; }
.team-round { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 0.35rem; border-block: 1px solid var(--divider); padding: 0.22rem 0.5rem; }
.team-round p { font-size: 0.83rem; color: var(--muted); }
.round-compact { display: none; }
.player { padding: 0.24rem 0.5rem 0.35rem; border-block-end: 1px solid var(--divider); }
.player:last-child { border-block-end: 0; }
.player[data-own="true"] { background: var(--selected); box-shadow: inset 2px 0 var(--accent); }
.player-head { display: flex; justify-content: space-between; align-items: baseline; gap: 0.2rem 0.5rem; flex-wrap: wrap; margin-bottom: 0.28rem; }
.player-label { display: flex; align-items: baseline; gap: 0.35rem; min-width: 0; }
.seat { min-width: 1.5rem; font-size: 0.88rem; color: var(--muted); }
.player-name { font-weight: 600; overflow-wrap: anywhere; }
.player-name.captain { font-weight: 800; }
.player-points { font-variant-numeric: tabular-nums; white-space: nowrap; font-size: 0.82rem; color: var(--muted); }
.player-points strong { color: var(--ink); font-size: 1rem; }
.score-actions { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0.25rem; }
.score-actions button, .team-pass { padding-inline: 0.2rem; min-height: 2.05rem; }
.team-pass { padding-inline: 0.5rem; }
.score-actions [data-action="two"]:enabled { background: var(--own); color: var(--own-ink); }
.score-actions [data-action="two"].awarded { background: var(--hold); color: var(--score-ink); }
.score-actions [data-action="wrong"]:enabled, .sheet-mark.wrong { background: var(--wrong); color: var(--score-ink); }
.score-grid[data-phase="teammates"] [data-action="one"]:enabled, .sheet-mark.save { background: var(--save); color: var(--score-ink); }
.score-grid[data-phase="opponents"] [data-action="one"]:enabled, .sheet-mark.steal { background: var(--steal); color: var(--score-ink); }
.score-actions [data-action="pass"]:enabled { background: var(--quiet); }
.score-actions button:hover:enabled { text-decoration: underline; }
.match-footer { justify-content: flex-end; margin-top: 0.7rem; }
.match-footer p { margin: 0 auto 0 0; font-size: 0.87rem; }
dialog { color: var(--ink); background: var(--surface); width: min(94%, 29rem); max-height: 90vh; border: 1px solid var(--line); border-radius: var(--radius); padding: 0.65rem; }
dialog::backdrop { background: #0008; }
dialog p { margin-block: 0.55rem; }
.wide-dialog { width: min(96%, 68rem); }
.table-wrap { overflow: auto; max-block-size: 65vh; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); margin-block: 0.45rem; }
table { width: 100%; border-spacing: 0; text-align: start; font-size: 0.9rem; }
caption { text-align: start; font-weight: 700; padding: 0.35rem 0.45rem; }
th, td { text-align: start; padding: 0.32rem 0.45rem; border-block-end: 1px solid var(--divider); vertical-align: top; }
thead th { position: sticky; top: 0; background: var(--quiet); color: var(--ink); white-space: nowrap; }
tbody tr:last-child > * { border-block-end: 0; }
tbody tr:nth-child(even) { background: var(--bg); }
.logs-filters { display: flex; align-items: flex-end; flex-wrap: wrap; gap: 0.55rem; }
.logs-filters label:first-child { flex: 1 1 15rem; }
.logs-filters select { min-width: 9.5rem; }
.logs-filters button { min-height: 2.15rem; }
.logs-count { margin-top: 0.7rem; color: var(--muted); }
.log-table { min-width: 56rem; }
.log-table time { white-space: nowrap; }
.log-table small, .event-table small { color: var(--muted); display: block; font-size: 0.85rem; }
.row-actions { display: flex; gap: 0.45rem; align-items: center; }
.row-actions a { white-space: nowrap; }
.usage { margin-top: 1rem; }
.usage > summary { cursor: pointer; font-weight: 700; }
.usage p { margin-top: 0.6rem; color: var(--muted); }
.event-table { min-width: 36rem; }
.event-table td { overflow-wrap: anywhere; }

@supports (grid-template-rows: subgrid) {
  .score-grid { row-gap: 0; }
  .score-grid > .team { display: grid; grid-row: span 6; grid-template-rows: subgrid; grid-template-columns: minmax(0, 1fr); }
}

#sheet-dialog .dialog-heading { position: sticky; inset-block-start: 0; z-index: 1; background: var(--surface); }
.sheet-columns { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.6rem; }
.sheet-team { min-inline-size: 0; }
.sheet-team h3, .sheet-notes h3 { font-size: 1rem; }
.sheet-team-info { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; gap: 0.25rem; }
.sheet-team-info p { margin: 0; color: var(--muted); font-size: 0.85rem; }
.sheet-grid { min-inline-size: 21rem; }
.sheet-grid :is(th, td) { text-align: center; padding: 0.3rem 0.25rem; white-space: nowrap; }
.sheet-grid thead th { position: static; white-space: normal; }
.sheet-team .table-wrap { max-block-size: none; }
.sheet-grid :is(th, td):nth-child(-n+5) { width: 12%; }
.sheet-seat { display: grid; justify-items: start; gap: 0.15rem; min-inline-size: 1.8rem; }
.sheet-bonuses { display: flex; flex-wrap: wrap; justify-content: end; gap: 0.12rem; inline-size: 100%; }
.sheet-mark { display: inline-block; text-align: center; min-inline-size: 1.25rem; border-radius: 2px; padding: 0.05rem 0.19rem; font-size: 0.8rem; font-weight: 650; background: var(--quiet); color: var(--ink); }
.sheet-mark.hold { background: var(--hold); color: var(--score-ink); }
.sheet-mark.blank { color: var(--muted); }
.sheet-total { font-weight: 700; }
.sheet-players :is(th, td) { padding: 0.3rem; }
.sheet-players td:nth-child(2) { overflow-wrap: anywhere; }
.sheet-notes dl { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 0.2rem 0.45rem; font-size: 0.88rem; }
.sheet-notes dd { margin: 0; overflow-wrap: anywhere; }
.sheet-notes dt { font-weight: 650; }
.sheet-key { display: flex; flex-wrap: wrap; gap: 0.35rem 0.8rem; padding: 0; list-style: none; font-size: 0.88rem; }
.sheet-attempts .table-wrap { max-block-size: none; }

@media (max-width: 49rem) {
  .sheet-columns { grid-template-columns: minmax(0, 1fr); }
  .sheet-grid { min-inline-size: 18rem; font-size: 0.82rem; }
  .sheet-grid :is(th, td) { padding-inline: 0.18rem; }
}
@media (max-width: 43rem) {
  .score-actions { gap: 0.13rem; }
  .score-actions button { min-height: 2.25rem; font-size: 0.72rem; }
  .score-actions button:is([data-action="pass"], [data-action="wrong"]) { padding-inline: 0; }
  .score-actions button:is([data-action="two"], [data-action="one"]) { font-size: 0.9rem; font-weight: 700; }
  .point-unit { display: none; }
  .team-grid { column-gap: 0.3rem; }
  .match-toolbar { gap: 0.25rem; }
  .match-toolbar .tools { gap: 0.2rem; }
  .score-strip { gap: 0.28rem; position: sticky; top: 0; z-index: 1; background: var(--bg); }
  .score-actions button, .team-pass { scroll-margin-top: 5rem; }
  .turn h2 { font-size: 1.04rem; }
  .turn p, .team-name, .team-score-info, .player-name, .team-round p { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .player-head { flex-wrap: nowrap; gap: 0.15rem; }
  .player-points { flex: none; position: relative; }
  .player-start { position: absolute; inset-inline-start: 0; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .team-head, .team-round, .player { padding-inline: 0.3rem; }
  .team-round { flex-wrap: nowrap; gap: 0.15rem; }
  .team-round p, .team-score-info, .player-points { font-size: 0.72rem; }
  .round-full { display: none; }
  .round-compact { display: inline; }
  .team-round p { min-inline-size: 0; }
  .team-round .team-pass { min-block-size: 1.75rem; font-size: 0.74rem; padding-inline: 0.2rem; white-space: nowrap; }
  .seat { min-inline-size: auto; font-size: 0.72rem; }
  .player-name { min-inline-size: 0; font-size: 0.86rem; }
  .player-label { gap: 0.22rem; }
  .match-footer { margin-top: 0.35rem; }
}
@media (max-width: 34rem) {
  .masthead-inner, .page { width: calc(100% - 0.65rem); }
  .page { padding-block: 0.35rem 0.45rem; }
  .masthead-title .brand { display: none; }
  .masthead h1 { font-size: 0.92rem; }
  .masthead-actions { gap: 0.4rem; }
  .masthead-actions nav { font-size: 0.84rem; }
  .theme-control label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .theme-control select { min-block-size: 1.8rem; }
  .fields { grid-template-columns: minmax(0, 1fr); }
  #setup .section-header { margin-block-end: 0.2rem; }
  .lineup { padding-inline: 0.3rem; }
  .lineup-fields { column-gap: 0.2rem; }
  .lineup-fields .field input { min-block-size: 2rem; }
  .first-row { gap: 0.2rem 0.5rem; }
  .first-row .choices { font-size: 0.88rem; column-gap: 0.55rem; }
  .draw-tools button { font-size: 0.81rem; min-block-size: 1.8rem; padding-inline: 0.35rem; }
  .setup-details { padding-block: 0.3rem; }
  .clock { padding-inline: 0.3rem; font-size: 0.9rem; }
  .score-strip > button { font-size: 0.82rem; }
  .turn p { font-size: 0.78rem; }
}

@media (max-width: 21rem) {
  .score-actions button[data-action="wrong"] { font-size: 0.65rem; }
}

@media (pointer: coarse) and (max-width: 43rem) {
  .score-actions button { min-block-size: clamp(2.05rem, 6.5svh, 2.75rem); }
  .captain-seat { min-inline-size: 1.85rem; }
}

@media (max-height: 30rem) and (orientation: landscape) and (min-width: 35rem) {
  .masthead-inner { min-block-size: 2.05rem; padding-block: 0; }
  .masthead nav a, .theme-control select { min-block-size: 1.9rem; }
  .page { padding-block: 0.15rem; }
  #setup { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); column-gap: 0.45rem; align-items: start; }
  #setup .section-header, .setup-hint { grid-column: 1 / -1; }
  #setup .section-header { margin-bottom: 0.2rem; }
  #lineups { grid-column: 1; grid-row: 3 / span 3; margin-top: 0.3rem; }
  .lineup-fields { row-gap: 0.2rem; }
  .start-panel, .setup-details, #setup .form-actions { grid-column: 2; }
  .start-panel { grid-row: 3; margin-top: 0.3rem; }
  .setup-details { grid-row: 4; }
  .setup-details .fields { grid-template-columns: minmax(0, 1fr); }
  #setup .form-actions { grid-row: 5; }
  .icon-control { min-block-size: 1.95rem; }
  .score-strip { margin-block: 0.25rem; }
  .turn { padding-block: 0.18rem; }
  .turn p { margin-top: 0; }
  .team-head { padding-block: 0.18rem; }
  .player { display: grid; grid-template-columns: minmax(0, 0.85fr) minmax(0, 1.2fr); align-items: center; gap: 0.25rem; padding-block: 0.14rem; }
  .player-head { margin: 0; gap: 0.1rem; }
  .player-name, .player-points { font-size: 0.78rem; }
  .point-unit { display: none; }
  .score-actions { gap: 0.12rem; }
  .score-actions button { min-block-size: 1.85rem; font-size: 0.7rem; }
  .match-footer { margin-top: 0.25rem; }
  .match-footer p { font-size: 0.78rem; }
}

@media (max-height: 22rem) and (orientation: landscape) and (min-width: 35rem) {
  .masthead-inner, .icon-control { min-block-size: 1.8rem; }
  .masthead nav a, .theme-control select { min-block-size: 1.7rem; }
  .page { padding-block: 0.14rem 0.08rem; }
  .score-strip { margin-block: 0.18rem; }
  .turn { display: flex; justify-content: center; align-items: baseline; gap: 0.35rem; }
  .turn h2 { flex: none; font-size: 0.92rem; }
  .turn p { min-inline-size: 0; font-size: 0.76rem; }
  .team-head { grid-template-columns: minmax(0, 1fr) minmax(0, 0.9fr) auto; }
  .team-score { grid-column: 3; grid-row: 1; font-size: 1.4rem; }
  .team-score-info { grid-column: 2; grid-row: 1; }
  .team-round .team-pass, .score-actions button { min-block-size: 1.65rem; }
  .player { padding-block: 0.1rem; }
  .match-footer { margin-top: 0.15rem; }
  #setup .panel { padding-block: 0.24rem 0.35rem; }
  #setup button, .lineup-fields .field input { min-block-size: 1.7rem; }
  #setup .field input { padding-block: 0.14rem; }
  .lineup-fields { row-gap: 0.15rem; }
  .captain-seat, .captain-none, .choices label { min-block-size: 1.4rem; }
}
@media print {
  :root, :root[data-color-mode] { color-scheme: light; }
  body { background: white; }
  body:has(#sheet-dialog[open]) > :not(#sheet-dialog) { display: none; }
  #sheet-dialog[open] { position: static; max-height: none; width: 100%; border: 0; padding: 0; overflow: visible; }
  #sheet-dialog button { display: none; }
  #sheet-dialog .dialog-heading { position: static; }
  #sheet-dialog .table-wrap { max-height: none; overflow: visible; }
  .sheet-grid { min-inline-size: 0; }
  .sheet-mark { print-color-adjust: exact; }
  thead th { position: static; }
}
