:root {
  color-scheme: light;
  --navy: #14256f;
  --navy-dark: #0b174e;
  --navy-soft: #1c3287;
  --grid: #d5dbe7;
  --header: #f4f6fb;
  --paper: #ffffff;
  --muted: #64748b;
  --ink: #0f172a;
  --blue: #0a84ff;
  --red: #ff3b30;
  --green: #16813a;
  --active: #fff6a8;
  --panel: #f4f6fa;
  --line: #d8dee8;
  --radius: 8px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    linear-gradient(180deg, #f9fbff 0%, #edf2f8 100%);
  color: var(--ink);
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.screen-wake-video {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 60;
  width: 1px;
  height: 1px;
  opacity: 0.01;
  pointer-events: none;
}

.app-shell {
  width: min(820px, 100%);
  min-height: 100dvh;
  margin: 0 auto;
  background: #f7f9fc;
}

.topbar {
  min-height: 52px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 7px 10px;
  background: rgba(255, 255, 255, 0.88);
  border-bottom: 1px solid rgba(216, 222, 232, 0.9);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand p,
.label,
.player-side {
  margin: 0;
  color: var(--muted);
  font-size: 10px;
  font-weight: 850;
  letter-spacing: 0;
  text-transform: uppercase;
}

.brand h1 {
  margin: 0;
  overflow: hidden;
  font-size: 20px;
  line-height: 1;
  letter-spacing: 0;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.board-mark {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border: 3px solid #111827;
  border-radius: 50%;
  background:
    radial-gradient(circle, #111827 0 9%, transparent 10%),
    repeating-conic-gradient(from -9deg, #ef4444 0 18deg, #f8fafc 18deg 36deg, #111827 36deg 54deg, #22c55e 54deg 72deg);
  box-shadow: inset 0 0 0 6px #f8fafc, inset 0 0 0 10px #111827;
}

.board-mark span {
  width: 7px;
  height: 7px;
  border: 2px solid #30d158;
  border-radius: 50%;
  background: var(--red);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.match-status {
  min-height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
  border-radius: 999px;
  background: #e8f2ff;
  color: #075fb6;
  font-size: 12px;
  font-weight: 850;
  white-space: nowrap;
}

.icon-button {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border-radius: 50%;
  background: rgba(15, 23, 42, 0.08);
  color: var(--ink);
}

.icon-button svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.workspace {
  min-height: calc(100dvh - 52px);
}

.score-view {
  min-height: calc(100dvh - 52px);
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px;
  background: #f7f9fc;
}

.score-strip {
  display: none;
}

.players {
  order: 1;
  min-height: 56px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 114px minmax(0, 1fr);
  align-items: stretch;
  overflow: hidden;
  border-radius: 16px;
  background: linear-gradient(135deg, var(--navy-soft), var(--navy));
  box-shadow: 0 10px 28px rgba(20, 37, 111, 0.2);
  color: #fff;
}

.player-tab,
.leg-score {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  color: #fff;
}

.player-tab {
  padding: 0 10px;
  font-size: clamp(22px, 5vw, 34px);
  font-weight: 500;
}

.player-tab span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-tab.active {
  font-weight: 650;
}

.leg-score {
  font-size: clamp(32px, 7vw, 48px);
  font-weight: 450;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.visit-history {
  order: 2;
  flex: 0 0 auto;
  min-height: 0;
  margin: 0;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--grid);
  border-radius: 14px;
  background: var(--paper);
  box-shadow: 0 10px 28px rgba(15, 23, 42, 0.07);
}

.history-title {
  display: none;
}

.history-table {
  width: 100%;
  height: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  background: var(--paper);
  font-variant-numeric: tabular-nums;
}

.history-table .score-col {
  width: 21%;
}

.history-table .to-go-col {
  width: 24%;
}

.history-table .round-col {
  width: 10%;
}

.history-table th,
.history-table td {
  border: 1px solid var(--grid);
  text-align: center;
}

.history-table th {
  height: 38px;
  padding: 4px 2px;
  background: var(--header);
  color: #334155;
  font-size: clamp(20px, 5vw, 34px);
  font-weight: 750;
  line-height: 1;
  text-transform: none;
}

.history-table th:nth-child(3),
.history-table td:nth-child(3) {
  background: #e8e8eb;
}

.history-table td {
  height: 64px;
  padding: 2px;
  font-size: clamp(34px, 9vw, 56px);
  font-weight: 650;
  line-height: 1;
}

.history-table .start-row td {
  height: 86px;
  font-size: clamp(48px, 12vw, 74px);
}

.history-round {
  color: #111827;
}

.active-entry-cell {
  background: linear-gradient(180deg, #fffbd0, var(--active));
  box-shadow: inset 0 0 0 2px rgba(255, 214, 10, 0.42);
}

#activeVisitPanel {
  order: 3;
}

.active-visit-panel {
  overflow: hidden;
  border-radius: 18px;
  background: linear-gradient(180deg, var(--navy), var(--navy-dark));
  color: #fff;
  box-shadow: 0 18px 38px rgba(20, 37, 111, 0.28);
}

.dock-scores {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0;
  padding: 10px 12px 4px;
}

.dock-score {
  min-width: 0;
  display: grid;
  gap: 2px;
  padding: 6px 8px 8px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  text-align: center;
}

.dock-score span {
  overflow: hidden;
  color: rgba(255, 255, 255, 0.72);
  font-size: 13px;
  font-weight: 750;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dock-score strong {
  display: block;
  padding: 0 8px 3px;
  border-radius: 8px;
  font-size: clamp(70px, 18vw, 132px);
  font-weight: 900;
  line-height: 0.95;
  font-variant-numeric: tabular-nums;
}

.dock-score.active strong {
  background: #fff;
  color: var(--navy);
}

.dock-actions {
  min-height: 46px;
  display: grid;
  grid-template-columns: 90px minmax(0, 1fr) 90px;
  align-items: center;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}

.dock-actions button {
  height: 46px;
  background: transparent;
  color: #fff;
  font-size: 18px;
}

.dock-actions div {
  min-width: 0;
  text-align: center;
}

.dock-actions span,
.dock-actions strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dock-actions span {
  color: rgba(255, 255, 255, 0.62);
  font-size: 10px;
  font-weight: 850;
  text-transform: uppercase;
}

.dock-actions strong {
  color: #fff;
  font-size: 15px;
  font-weight: 800;
}

.player-visit {
  padding: 10px 0 0;
}

.visit-score-row {
  padding: 0 12px 12px;
}

.visit-score-row input {
  width: 100%;
  height: 58px;
  border: 0;
  border-radius: 12px;
  background: #fff;
  color: var(--navy);
  outline: none;
  text-align: center;
  font-size: 40px;
  font-weight: 850;
  font-variant-numeric: tabular-nums;
}

.visit-score-row input::placeholder {
  color: rgba(18, 37, 109, 0.38);
}

.score-keypad {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  padding: 0 12px 12px;
}

.score-keypad button {
  min-height: 72px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  font-size: clamp(42px, 11vw, 62px);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.13);
}

.score-keypad .enter-key {
  background: #ffffff;
  color: var(--navy);
  font-size: clamp(28px, 7vw, 42px);
  font-weight: 850;
}

.score-keypad button:active,
.dock-actions button:active {
  background: var(--navy-dark);
}

.message {
  order: 4;
  min-height: 28px;
  padding: 5px 10px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 750;
}

.message.error {
  color: var(--red);
}

.message.success {
  color: var(--green);
}

.settings-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  align-items: start;
  justify-items: end;
  padding: 12px;
  background: rgba(15, 23, 42, 0.34);
}

.settings-overlay[hidden] {
  display: none;
}

.settings-panel {
  width: min(520px, 100%);
  height: min(760px, calc(100dvh - 24px));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.78);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 18px 52px rgba(15, 23, 42, 0.13);
  backdrop-filter: blur(22px);
  -webkit-backdrop-filter: blur(22px);
}

.settings-save-bar {
  margin: 0;
  padding: 12px 14px max(12px, env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.94);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

.tournament-header {
  padding: 14px;
  background: rgba(255, 255, 255, 0.96);
  border-bottom: 1px solid var(--line);
}

.settings-content {
  min-height: 0;
  overflow-y: auto;
  padding: 14px;
  overscroll-behavior: contain;
}

.save-settings {
  width: 100%;
  min-height: 72px;
  border-radius: var(--radius);
  background: var(--blue);
  color: #fff;
  font-size: 24px;
  font-weight: 950;
}

.save-settings:active {
  background: #0077e6;
}

.tournament-header,
.entry-actions {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.tournament-header h2,
.settings-group-heading h3 {
  margin: 2px 0 0;
  font-size: 22px;
  line-height: 1;
}

.settings-group {
  margin-top: 14px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
}

.match-player-fields {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.match-player-fields label {
  min-width: 0;
}

.match-player-fields span {
  display: block;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 900;
  text-transform: uppercase;
}

.player-entry {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  margin-top: 14px;
}

.player-entry input,
.match-player-fields input {
  width: 100%;
  min-width: 0;
  height: 48px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0 12px;
  background: #fff;
  color: var(--ink);
  outline: none;
}

.player-entry input:focus,
.match-player-fields input:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 4px rgba(10, 132, 255, 0.12);
}

.player-entry button,
.primary-wide,
.secondary-wide,
.settings-reset {
  min-height: 46px;
  padding: 0 14px;
  border-radius: var(--radius);
  background: var(--blue);
  color: #fff;
  font-weight: 900;
}

.settings-reset {
  width: 100%;
  margin-top: 10px;
  background: rgba(255, 59, 48, 0.11);
  color: var(--red);
}

.primary-wide {
  width: 100%;
}

.secondary-wide {
  width: 100%;
  background: #eef2ff;
  color: var(--navy);
}

.tournament-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.roster {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 42px;
  margin: 12px 0;
}

.roster-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  min-height: 34px;
  padding: 0 8px 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  font-size: 14px;
  font-weight: 850;
}

.roster-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.roster-chip button {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(255, 59, 48, 0.11);
  color: var(--red);
  font-size: 16px;
  line-height: 1;
}

.bracket {
  display: grid;
  gap: 12px;
  margin-top: 14px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.league-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 10px;
}

.league-header h3 {
  margin: 2px 0 0;
  font-size: 20px;
  line-height: 1;
}

.league-header > span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 850;
}

.league-list {
  display: grid;
  gap: 8px;
}

.league-match {
  display: grid;
  gap: 7px;
  padding: 9px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.league-match.current {
  border-color: rgba(10, 132, 255, 0.55);
  box-shadow: inset 0 0 0 1px rgba(10, 132, 255, 0.18);
}

.league-match.played {
  background: #f8fafc;
}

.league-pair {
  min-width: 0;
  display: grid;
  gap: 3px;
  background: transparent;
  color: var(--ink);
  text-align: left;
}

.league-pair span {
  color: var(--muted);
  font-size: 11px;
  font-weight: 900;
  text-transform: uppercase;
}

.league-pair strong {
  overflow: hidden;
  font-size: 16px;
  line-height: 1.1;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.league-pair em {
  color: var(--muted);
  font-style: normal;
  font-weight: 800;
}

.league-result {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.league-result button {
  min-width: 0;
  min-height: 34px;
  padding: 0 8px;
  overflow: hidden;
  border-radius: 7px;
  background: var(--panel);
  color: var(--ink);
  font-size: 13px;
  font-weight: 850;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.league-result button.winner {
  background: rgba(48, 209, 88, 0.16);
  color: var(--green);
}

.round h3 {
  margin: 14px 0 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

.match {
  display: grid;
  gap: 6px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.match-player {
  min-height: 38px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--panel);
  color: var(--ink);
  text-align: left;
  font-weight: 850;
}

.match-player:not(:disabled):hover {
  background: #e8f2ff;
}

.match-player:disabled {
  color: var(--muted);
  cursor: default;
}

.match-player.winner {
  background: rgba(48, 209, 88, 0.15);
  color: var(--green);
}

.empty-state {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
  font-weight: 750;
}

@media (max-width: 780px) {
  html,
  body {
    overflow-x: hidden;
  }

  .app-shell {
    width: 100%;
  }

  .topbar {
    min-height: 40px;
    padding: 4px 8px;
  }

  .workspace,
  .score-view {
    min-height: calc(100dvh - 40px);
  }

  .score-view {
    gap: 6px;
    padding: 6px;
  }

  .board-mark {
    width: 28px;
    height: 28px;
  }

  .brand h1 {
    font-size: 15px;
  }

  .icon-button {
    width: 32px;
    height: 32px;
  }

  .match-status {
    min-height: 28px;
    font-size: 11px;
  }

  .match-status {
    max-width: 126px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .players {
    min-height: 40px;
    grid-template-columns: minmax(0, 1fr) 86px minmax(0, 1fr);
    border-radius: 14px;
  }

  .player-tab {
    font-size: 18px;
  }

  .leg-score {
    font-size: 27px;
  }

  .visit-history {
    max-height: 286px;
    overflow: auto;
    border-radius: 12px;
  }

  .history-table th {
    height: 25px;
    font-size: 14px;
  }

  .history-table td {
    height: 34px;
    font-size: 28px;
  }

  .history-table .start-row td {
    height: 44px;
    font-size: 36px;
  }

  .active-visit-panel {
    border-radius: 16px;
  }

  .dock-scores {
    padding: 5px 7px 3px;
  }

  .dock-score {
    padding: 4px 6px 6px;
    border-radius: 10px;
  }

  .dock-score span {
    font-size: 10px;
  }

  .dock-score strong {
    font-size: 50px;
  }

  .dock-actions {
    grid-template-columns: 64px minmax(0, 1fr) 64px;
    min-height: 34px;
  }

  .dock-actions button {
    height: 34px;
    font-size: 13px;
  }

  .dock-actions span {
    font-size: 9px;
  }

  .dock-actions strong {
    font-size: 13px;
  }

  .player-visit {
    padding-top: 7px;
  }

  .visit-score-row {
    padding: 0 8px 6px;
  }

  .visit-score-row input {
    height: 38px;
    border-radius: 10px;
    font-size: 27px;
  }

  .score-keypad {
    gap: 6px;
    padding: 0 8px 8px;
  }

  .score-keypad button {
    min-height: 42px;
    border-radius: 10px;
    font-size: 34px;
  }

  .score-keypad .enter-key {
    font-size: 24px;
  }

  .message {
    min-height: 22px;
    padding: 3px 8px;
    font-size: 13px;
  }

  .settings-overlay {
    align-items: start;
    padding: 8px;
  }

  .settings-panel {
    width: 100%;
    max-height: calc(100vh - 16px);
  }

  .player-entry,
  .match-player-fields,
  .tournament-actions {
    grid-template-columns: 1fr;
  }

  .player-entry button {
    width: 100%;
  }
}

@media (max-width: 390px) {
  .brand p {
    display: none;
  }

  .brand h1 {
    max-width: 134px;
  }

  .player-tab {
    font-size: 17px;
  }

  .leg-score {
    font-size: 25px;
  }

  .history-table th {
    font-size: 13px;
  }

  .history-table td {
    height: 32px;
    font-size: 26px;
  }

  .history-table .start-row td {
    height: 40px;
    font-size: 34px;
  }

  .dock-score strong {
    font-size: 46px;
  }

  .visit-score-row input {
    height: 36px;
    font-size: 25px;
  }

  .score-keypad button {
    min-height: 40px;
    font-size: 32px;
  }
}
