:root {
  color-scheme: dark;
  --bg: #12122a;
  --bg-glow: #2a2160;
  --surface: #23234a;
  --sheet-bg: #17173a;
  --border: rgb(255 255 255 / 0.14);
  --text: #f4f4fb;
  --muted: #a6a6c8;
  --accent: #ff5a5f;
  --gold: #ffc23d;
  --on-gold: #1d1606;
  --label-width: 76px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --gutter-left: max(16px, env(safe-area-inset-left, 0px));
  --gutter-right: max(16px, env(safe-area-inset-right, 0px));
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
}

html {
  background-color: var(--bg);
  /* iOS Safari enlarges text in landscape unless told not to. */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  /* Keeps pinch zoom but drops double-tap zoom, so fast taps never zoom the page. */
  touch-action: manipulation;
}

body {
  min-height: 100dvh;
  /* Not "fixed": iOS ignores it and it forces repaints while scrolling. */
  background: radial-gradient(circle at 20% -10%, var(--bg-glow), var(--bg) 55%) no-repeat;
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.4;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

h1,
h2 {
  margin: 0;
}

h1 {
  font-size: 1.4rem;
}

h2 {
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

button,
input[type="text"] {
  min-height: 44px;
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: inherit;
  /* 16px or more stops iOS Safari from zooming in on focus. */
  font: inherit;
  font-size: 1rem;
}

button {
  cursor: pointer;
  transition: transform 0.1s;
}

button:active:not(:disabled) {
  transform: scale(0.96);
}

/* The page blocks text selection; a name field must still take typing and the caret. */
input[type="text"] {
  -webkit-user-select: text;
  user-select: text;
}

input[type="text"]::placeholder {
  color: var(--muted);
}

@media (hover: hover) {
  button:hover:not(:disabled) {
    border-color: rgb(255 255 255 / 0.35);
  }
}

button:focus-visible,
input:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 2px;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* Classes below set "display", which would otherwise beat the hidden attribute. */
[hidden] {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.count {
  margin-left: 8px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  letter-spacing: normal;
}

/* Top bar */

.topbar {
  max-width: 1100px;
  margin: 0 auto;
  padding: calc(12px + var(--safe-top)) var(--gutter-right) 4px var(--gutter-left);
}

/* Layout */

.app {
  display: grid;
  gap: 28px;
  max-width: 1100px;
  margin: 0 auto;
  padding: 8px var(--gutter-right) calc(32px + var(--safe-bottom)) var(--gutter-left);
}

.game {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 0;
}

.banner {
  margin: 0;
  padding: 10px 18px;
  border-radius: 14px;
  background: var(--gold);
  color: var(--on-gold);
  font-size: 1.2rem;
  font-weight: 800;
  text-align: center;
  text-wrap: balance;
}

.status {
  margin: 0;
  min-height: 1.4em;
  color: var(--muted);
  text-align: center;
}

/* Score sheet: one row per category, one column per player. */

/* Many players: the sheet scrolls sideways inside its box, never the page. */
.sheet-wrap {
  overflow-x: auto;
  /* A sideways swipe at the end of the sheet must not turn into "back" in the browser. */
  overscroll-behavior-x: contain;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--sheet-bg);
}

.sheet {
  width: 100%;
  /* --players is set from app.js. Below about 42px a column gets too narrow to tap. */
  min-width: calc(var(--label-width) + var(--players, 2) * 42px);
  table-layout: fixed;
  border-collapse: separate;
  border-spacing: 3px;
}

.sheet th,
.sheet td {
  padding: 0;
}

.sheet thead th {
  border-radius: 8px 8px 0 0;
  color: var(--muted);
}

/* Tap a name to change it. */
.head-name {
  display: block;
  width: 100%;
  min-height: 36px;
  overflow: hidden;
  padding: 4px 2px 6px;
  border: none;
  border-radius: 8px 8px 0 0;
  background: none;
  color: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sheet thead th.turn {
  color: var(--gold);
  box-shadow: inset 0 -3px var(--gold);
}

.sheet .corner,
.sheet tbody th,
.sheet tfoot th {
  /* Stays on screen while the player columns scroll sideways. */
  position: sticky;
  left: 0;
  z-index: 1;
  width: var(--label-width);
  background: var(--sheet-bg);
}

.sheet tbody th,
.sheet tfoot th {
  padding-inline: 4px;
  font-size: 0.9rem;
  font-weight: 600;
  text-align: left;
  white-space: nowrap;
}

.sheet-wrap.scrolled .corner,
.sheet-wrap.scrolled tbody th,
.sheet-wrap.scrolled tfoot th {
  box-shadow: 8px 0 8px -6px rgb(0 0 0 / 0.6);
}

.sheet tr.split > * {
  padding-top: 10px;
}

.die {
  display: inline-grid;
  place-items: center;
  width: 30px;
  aspect-ratio: 1;
  border-radius: 8px;
  background: linear-gradient(135deg, #fff, #dcdcea);
  color: #15152b;
  font-weight: 800;
}

.cell {
  display: grid;
  place-items: center;
  width: 100%;
  min-height: 42px;
  padding: 0;
  border-color: transparent;
  border-radius: 10px;
  font-size: 1.1rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.cell.empty {
  background: rgb(255 255 255 / 0.05);
}

.cell.crossed {
  color: var(--muted);
  font-weight: 600;
}

/* The player whose turn it is: the empty rows are dashed in gold, ready to tap. */
.cell.turn {
  border-color: rgb(255 194 61 / 0.45);
}

.cell.turn.empty {
  border-style: dashed;
  background: rgb(255 194 61 / 0.08);
}

.sheet tfoot th,
.sheet tfoot td {
  padding-top: 8px;
}

.sheet tfoot td {
  border-radius: 10px;
  font-size: 1.1rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.sheet tfoot td.leader {
  background: var(--gold);
  color: var(--on-gold);
}

.tools {
  display: flex;
  justify-content: center;
  gap: 8px;
}

/* Players */

.side {
  display: grid;
  gap: 28px;
  align-content: start;
}

.players,
.options {
  padding: 20px max(0px, calc((100% - 420px) / 2)) 0;
  border-top: 1px solid var(--border);
}

.players {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.player-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.player-list li {
  display: flex;
  gap: 8px;
}

.player-list input {
  flex: 1;
  min-width: 0;
}

.player-list .remove {
  width: 44px;
  padding: 0;
}

#add-player {
  align-self: center;
}

/* Rules */

/* A list of label | control rows, like the options of the bingo caller. */
.options {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 12px 14px;
}

.options h2,
.options #reset {
  grid-column: 1 / -1;
}

.options #reset {
  justify-self: center;
  margin-top: 4px;
}

/* Each label spans both columns and puts its text and control in the columns of .options. */
.field {
  display: grid;
  grid-column: 1 / -1;
  grid-template-columns: subgrid;
  align-items: center;
  justify-items: end;
  color: var(--muted);
}

.check {
  min-height: 44px;
  cursor: pointer;
}

.check input {
  justify-self: start;
  width: 22px;
  height: 22px;
  margin: 0;
  accent-color: var(--gold);
  cursor: pointer;
}

/* Picker: a sheet from the bottom of the screen on phones, within thumb reach. */

.picker {
  width: 100%;
  max-width: 480px;
  max-height: 100%;
  margin: auto auto 0;
  padding: 0;
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 20px 20px 0 0;
  background: var(--surface);
  color: var(--text);
}

.picker::backdrop {
  background: rgb(0 0 0 / 0.55);
}

.picker[open] {
  animation: rise 0.18s ease-out;
}

@keyframes rise {
  from {
    transform: translateY(24px);
    opacity: 0;
  }
}

.picker-body {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 20px var(--gutter-right) calc(20px + var(--safe-bottom)) var(--gutter-left);
}

.picker h2 {
  color: var(--text);
  font-size: 1.15rem;
  letter-spacing: normal;
  text-align: center;
  text-transform: none;
}

.picker-hint {
  margin: 0;
  color: var(--muted);
  text-align: center;
}

.picker-options {
  display: grid;
  grid-template-columns: repeat(var(--cols, 3), minmax(0, 1fr));
  gap: 8px;
}

.picker-options[data-cols="2"] {
  --cols: 2;
}

.option {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 68px;
  padding: 8px 4px;
  border-radius: 14px;
  background: rgb(255 255 255 / 0.06);
}

.option-value {
  font-size: 1.5rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

.option-note {
  color: var(--muted);
  font-size: 0.85rem;
}

.option.cross .option-value {
  color: var(--accent);
}

.option.wins {
  border-color: var(--gold);
  color: var(--gold);
}

.option.current {
  border-color: var(--text);
  box-shadow: 0 0 0 2px var(--text);
}

.picker-actions {
  display: flex;
  justify-content: center;
  gap: 8px;
}

/* Wide screens: the sheet on the left, players and rules on the right. */

@media (min-width: 600px) {
  .picker {
    margin: auto;
    border-bottom: 1px solid var(--border);
    border-radius: 20px;
  }

  .picker-body {
    padding: 24px;
  }
}

@media (min-width: 900px) {
  .app {
    grid-template-columns: minmax(0, 1fr) 360px;
    align-items: start;
    gap: 40px;
    padding-top: 16px;
  }

  .players {
    padding-top: 0;
    border-top: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .picker[open] {
    animation: none;
  }

  button {
    transition: none;
  }
}
