/* Base layer: document reset, global theme tokens, layout shell, and score table styling.
   Dark gray canvas with color accents coming from per-player --accent variables set in JS. */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  font-size: 1rem;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg);
}

/* Theme */
:root {
  --bg: #1e1e1e;
  --bg-elevated: #2a2a2a;
  --bg-input: #333333;
  --border: #3d3d3d;
  --text: #e8e8e8;
  --text-muted: #9a9a9a;
  --focus: #6cb0ff;
  --radius: 10px;
  --min-tap: 44px;
  /* Match header reset button to player count (segmented) height */
  --header-control-h: 2.25rem;
}

.skip-link {
  position: absolute;
  left: -9999px;
  z-index: 100;
  padding: 0.5rem 1rem;
  background: var(--focus);
  color: #111;
  font-weight: 600;
  text-decoration: none;
  border-radius: 4px;
}
.skip-link:focus {
  left: 0.5rem;
  top: 0.5rem;
}

.app-header {
  padding: 0.9rem 0.75rem 0.85rem;
  text-align: center;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, #242424 0%, var(--bg) 100%);
}

.app-title {
  margin: 0 0 0.6rem;
  text-align: center;
  font-size: 1.6rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1.2;
}

/* Reset (left) + player count: centered as a group */
.app-header__controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.5rem 0.75rem;
  max-width: 36rem;
  margin: 0 auto;
}

.app-header__players {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem 0.45rem;
}

.app-header__players-label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  white-space: nowrap;
}

.app-main {
  max-width: 56rem;
  margin: 0 auto;
  padding: 1rem 0.75rem 2.5rem;
}

.panel {
  margin-bottom: 1.25rem;
  padding: 1rem 1rem 1.25rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.panel__title {
  margin: 0 0 0.65rem;
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
}

.btn {
  min-height: var(--min-tap);
  padding: 0.45rem 1rem;
  font: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease, opacity 0.12s ease;
}

/* In header: same height as segmented player buttons */
.app-header .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--header-control-h);
  height: var(--header-control-h);
  padding: 0 0.55rem;
  font-size: 0.8rem;
  line-height: 1.2;
  border-radius: 6px;
  box-sizing: border-box;
}
.btn:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn--danger {
  color: #ffd6d4;
  background: #4a2e2c;
  border-color: #6b3e3a;
}
.btn--danger:hover:not(:disabled) {
  background: #5c3a36;
  border-color: #7a4a44;
}

/* 2–6 player picker (used in app header) */
.segmented {
  display: flex;
  flex-wrap: wrap;
  gap: 0.2rem;
}

.segmented--header .segmented__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.9rem;
  min-height: var(--header-control-h);
  height: var(--header-control-h);
  padding: 0 0.4rem;
  font-size: 0.8rem;
  line-height: 1.2;
  box-sizing: border-box;
}

.segmented__btn {
  min-width: 2.25rem;
  min-height: var(--header-control-h);
  padding: 0.2rem 0.5rem;
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
}
.segmented__btn:hover {
  background: #3a3a3a;
}
.segmented__btn[aria-pressed="true"] {
  background: #3d4f63;
  border-color: #5a7ab0;
  color: #fff;
}
.segmented__btn:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* Score table: horizontal scroll on narrow screens, sticky first column */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 -0.25rem;
  padding: 0 0.25rem;
}

.score-table {
  width: 100%;
  min-width: 280px;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.9rem;
}

.score-table th,
.score-table td {
  padding: 0.4rem 0.35rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.score-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--bg-elevated);
  font-weight: 600;
  text-align: left;
  border-bottom: 2px solid var(--border);
}

/* Body: R1, R2 — sticky on horizontal scroll only; scrolls with table vertically */
.score-table tbody th.round-label {
  position: sticky;
  left: 0;
  z-index: 1;
  min-width: 3.5rem;
  max-width: 4.5rem;
  background: #2c2c2c;
  font-weight: 500;
  color: var(--text-muted);
  text-align: center;
  border-right: 1px solid var(--border);
  box-shadow: 1px 0 0 var(--border);
}

/* Top-left empty header: not sticky on X (scrolls away); still sticky on Y in scroll parent */
.score-table thead th.corner {
  position: sticky;
  top: 0;
  left: auto;
  z-index: 3;
  min-width: 2.75rem;
  background: #323232;
  border-bottom: 2px solid var(--border);
  border-right: 1px solid var(--border);
}

/* Player column headers: accent bar */
.player-th {
  min-width: 6.5rem;
  vertical-align: bottom;
  border-top: 3px solid var(--accent, var(--border));
  background: color-mix(in srgb, var(--accent, #555) 12%, var(--bg-elevated));
}

.player-th__row {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.4rem;
  padding: 0.2rem 0;
}

.player-th__name {
  width: 100%;
  min-height: 2.5rem;
  padding: 0.35rem 0.4rem;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
}
.player-th__name:focus {
  outline: 2px solid var(--accent, var(--focus));
  outline-offset: 1px;
}

.player-th__color {
  width: 100%;
  min-height: var(--min-tap);
  padding: 0.15rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-input);
  cursor: pointer;
}
.player-th__color:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.score-table td input.score-input {
  width: 100%;
  min-width: 3.5rem;
  min-height: 2.75rem;
  padding: 0.4rem 0.45rem;
  font: inherit;
  font-variant-numeric: tabular-nums;
  text-align: center;
  color: var(--text);
  background: var(--bg-input);
  border: 1px solid color-mix(in srgb, var(--col-accent, var(--border)) 40%, var(--border));
  border-radius: 6px;
  -moz-appearance: textfield;
}
.score-table td input.score-input::-webkit-outer-spin-button,
.score-table td input.score-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.score-table td input.score-input:focus {
  outline: 2px solid var(--col-accent, var(--focus));
  outline-offset: 1px;
}

.score-table tfoot th,
.score-table tfoot td {
  font-weight: 600;
  background: #323232;
  border-top: 2px solid var(--border);
  border-bottom: none;
}
.score-table tfoot .total-label {
  text-align: right;
  padding-right: 0.5rem;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 600;
}
.score-table tfoot .total-value {
  font-variant-numeric: tabular-nums;
  text-align: center;
  color: var(--text);
  border-top: 2px solid var(--col-accent, var(--border));
  background: color-mix(in srgb, var(--col-accent, #444) 10%, #323232);
}

/* Mobile: compact table so 4 players fit without horizontal scroll; desktop unchanged above 599px */
@media (max-width: 599px) {
  .panel {
    display: flex;
    flex-direction: column;
    padding: 0.65rem 0.4rem 0.75rem;
  }

  .table-wrap {
    width: 100%;
    min-width: 0;
    margin: 0;
    padding: 0;
    overflow-x: visible;
  }

  .score-table {
    table-layout: fixed;
    width: 100%;
    min-width: 0;
    font-size: 0.78rem;
  }

  .score-table th,
  .score-table td {
    padding: 0.2rem 0.12rem;
  }

  /* First column: round labels; wide enough for “Total” in footer, still leaves room for 4+ score columns */
  .score-table thead th.corner,
  .score-table tbody th.round-label,
  .score-table tfoot th.total-label {
    width: 2.1rem;
    min-width: 0;
    max-width: 2.1rem;
  }

  .score-table tbody th.round-label {
    font-size: 0.68rem;
    padding: 0.15rem 0.06rem;
  }

  .player-th {
    min-width: 0;
    width: auto;
    border-top-width: 2px;
  }

  .player-th__row {
    gap: 0.2rem;
    padding: 0.1rem 0;
  }

  .player-th__name {
    min-height: 1.75rem;
    padding: 0.15rem 0.2rem;
    font-size: 0.7rem;
  }

  .player-th__color {
    min-height: 1.6rem;
    max-height: 1.6rem;
    padding: 0.08rem;
  }

  .score-table td input.score-input {
    min-width: 0;
    min-height: 1.9rem;
    padding: 0.15rem 0.1rem;
    font-size: 0.8rem;
  }

  .score-table tfoot .total-label {
    font-size: 0.7rem;
    padding-right: 0.2rem;
  }
}

/* Mobile-only: “Add round” sits bottom-right of the Scores panel (in document flow) */
@media (max-width: 599px) {
  .add-round-fab {
    align-self: flex-end;
    flex-shrink: 0;
    margin-top: 0.45rem;
    min-height: 2.5rem;
    padding: 0.35rem 0.7rem;
    font: inherit;
    font-size: 0.8rem;
    font-weight: 600;
    color: #f2f6ff;
    background: #2d4a6e;
    border: 1px solid #3d5a7e;
    border-radius: 999px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
    cursor: pointer;
  }
  .add-round-fab[hidden] {
    display: none;
  }
  .add-round-fab:not([hidden]) {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  .add-round-fab:focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 2px;
  }
  .add-round-fab:hover {
    background: #355a88;
  }
}

@media (min-width: 600px) {
  .app-header {
    padding: 1.1rem 1.25rem 0.9rem;
  }
  .app-title {
    font-size: 1.75rem;
  }
  .app-header__controls {
    max-width: 40rem;
    gap: 0.5rem 1rem;
  }
  .app-main {
    padding: 1.25rem 1.25rem 3rem;
  }
  .panel {
    padding: 1.1rem 1.25rem 1.35rem;
  }
  .add-round-fab {
    display: none !important;
  }
}
