/* Big, high-contrast, paper-scorepad look. */
:root {
  --red: #8b1e1e;
  --red-soft: #f6e7e4;
  --ink: #1c1917;
  --muted: #57534e;
  --paper: #fffdf8;
  --line: #a8a29e;
  --current: #fff4c2;
  --leader: #1f6f3a;
  --leader-bg: #e3f3e8;
  --cell-w: 5.5rem;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 20px;
  color: var(--ink);
  background: var(--paper);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  /* The page itself never scrolls; only the score grid does, so its pinned edges stay visible. */
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.topbar { flex: none; }

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--red);
  color: #fff;
}
.topbar h1 { margin: 0; font-size: 1.5rem; letter-spacing: 0.02em; }
.actions { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; }
.start-at { display: flex; align-items: center; gap: 0.4rem; font-weight: 600; font-size: 1.05rem; }
.start-at select {
  font: inherit;
  font-weight: 700;
  min-height: 48px;
  padding: 0.25rem 0.5rem;
  border-radius: 10px;
  border: 2px solid #fff;
  background: #fff;
  color: var(--red);
}

.btn {
  font: inherit;
  font-size: 1.05rem;
  font-weight: 600;
  min-height: 48px;
  padding: 0.5rem 1.1rem;
  border-radius: 10px;
  border: 2px solid currentColor;
  background: #fff;
  color: var(--red);
  cursor: pointer;
}
.btn-primary { background: var(--red); color: #fff; border-color: var(--red); }
.topbar .btn-primary { border-color: #fff; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

main { padding: 1rem; flex: 1; min-height: 0; display: flex; flex-direction: column; }
.hint { flex: none; }

.sheet-wrap {
  overflow: auto;
  flex: 0 1 auto; /* shrink to fit the screen, never stretch past the table */
  min-height: 0;
  overscroll-behavior: contain;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: #fff;
}

.sheet {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  table-layout: fixed; /* every player shares the width evenly, so all columns stay on screen */
}
.sheet th, .sheet td {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 0;
  text-align: center;
}

/* Sticky header row, round column and totals row so names & totals stay visible while scrolling. */
.sheet thead th { position: sticky; top: 0; z-index: 2; background: var(--red-soft); border-bottom: 2px solid var(--ink); }
.sheet tfoot th, .sheet tfoot td { position: sticky; bottom: 0; z-index: 2; background: var(--red-soft); border-top: 2px solid var(--ink); border-bottom: 0; }
.sheet .round-col { position: sticky; left: 0; z-index: 1; background: var(--red-soft); width: 5.5rem; min-width: 5.5rem; border-right: 2px solid var(--ink); }
.sheet thead .round-col, .sheet tfoot .round-col { z-index: 3; }
.corner { font-size: 0.9rem; color: var(--muted); font-weight: 600; }

.player-col { min-width: 0; }
.name-cell { position: relative; }
.name {
  width: 100%;
  font: inherit;
  font-weight: 700;
  font-size: 1.05rem;
  text-align: center;
  text-overflow: ellipsis;
  padding: 1.15rem 0.25rem 0.5rem; /* top room so the × sits above the name, not over it */
  border: 0;
  background: transparent;
  color: var(--ink);
}
.name::placeholder { color: #a8a29e; font-weight: 500; }
.remove {
  position: absolute;
  top: 0;
  right: 0;
  width: 26px;
  height: 26px;
  padding: 0;
  border: 0;
  border-radius: 0 0 0 8px;
  background: transparent;
  color: var(--muted);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
}
.remove:hover, .remove:focus-visible { background: #e7e5e4; color: var(--red); }

.score {
  width: 100%;
  min-width: 0;
  height: 56px;
  font: inherit;
  font-size: 1.5rem;
  font-variant-numeric: tabular-nums;
  text-align: center;
  border: 0;
  background: transparent;
  color: var(--ink);
}
.name:focus, .score:focus { outline: 3px solid var(--red); outline-offset: -3px; background: #fff; }

tbody tr.current td { background: var(--current); }
tbody tr.current .round-col { background: #f5d77a; }

/* Domino badge for the round, like the double on the paper pad. */
.domino {
  display: inline-flex;
  align-items: stretch;
  border: 2px solid var(--ink);
  border-radius: 6px;
  background: #fff;
  font-weight: 800;
  font-size: 1.1rem;
  color: var(--red);
  font-variant-numeric: tabular-nums;
}
.domino span { min-width: 1.6em; padding: 0.1rem 0.15rem; }
.domino span + span { border-left: 2px solid var(--ink); }

.total {
  font-size: 1.6rem;
  font-weight: 800;
  padding: 0.6rem 0.25rem !important;
  font-variant-numeric: tabular-nums;
}
.total.leader { color: var(--leader); background: var(--leader-bg) !important; }
.total.leader::after { content: " ★"; font-size: 1rem; vertical-align: middle; }

.hint { color: var(--muted); font-size: 1rem; text-align: center; margin: 0.75rem 0 0; }

dialog {
  border: 0;
  border-radius: 14px;
  padding: 1.5rem;
  max-width: min(26rem, calc(100vw - 2rem));
  font-size: 1.2rem;
  box-shadow: 0 10px 40px rgb(0 0 0 / 0.3);
}
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
dialog p { margin: 0 0 1.25rem; }
#name-title { display: block; font-weight: 700; margin-bottom: 0.6rem; }
#name-input {
  display: block;
  width: 100%;
  font: inherit;
  font-size: 1.4rem;
  padding: 0.6rem 0.75rem;
  margin-bottom: 1.25rem;
  border: 2px solid var(--line);
  border-radius: 10px;
}
#name-input:focus { outline: none; border-color: var(--red); }
.name[readonly] { cursor: pointer; }
.dialog-option {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin: -0.5rem 0 1.25rem;
  font-weight: 600;
  cursor: pointer;
}
.dialog-option input { width: 28px; height: 28px; accent-color: var(--red); margin: 0; }
.dialog-option[hidden] { display: none; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 0.75rem; }

/* Count dots from a photo */
.photo-dialog { width: min(40rem, calc(100vw - 1rem)); max-width: none; max-height: calc(100dvh - 1rem); padding: 1rem 1.1rem; overflow: auto; }
.photo-title { margin: 0 0 0.75rem; font-size: 1.3rem; }
.photo-who { display: flex; flex-wrap: wrap; gap: 0.75rem; margin-bottom: 0.9rem; }
.photo-who[hidden] { display: none; }
.photo-who label { display: flex; align-items: center; gap: 0.4rem; font-weight: 600; }
.photo-who select {
  font: inherit; font-weight: 700; min-height: 48px; max-width: 11rem;
  padding: 0.25rem 0.5rem; border-radius: 10px; border: 2px solid var(--line); background: #fff; color: var(--ink);
}
.file-btn { display: inline-flex; align-items: center; gap: 0.4rem; position: relative; overflow: hidden; }
/* Invisible but still "there": some iOS versions don't report picks from a display:none file input. */
.file-input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; font-size: 0; }
.photo-status { margin: 0.75rem 0; font-size: 1rem; color: var(--muted); }
.photo-status.error { color: #7a1010; background: #fde8e8; border: 2px solid #d64545; border-radius: 8px; padding: 0.5rem 0.7rem; }
.photo-total { margin: 0 0 0.4rem; font-weight: 700; }
#photo-total { font-size: 2.6rem; font-weight: 800; color: var(--red); font-variant-numeric: tabular-nums; }
.photo-warn { margin: 0 0 0.6rem; padding: 0.5rem 0.7rem; border-radius: 8px; background: #fff4c2; border: 2px solid #e0b100; font-weight: 600; font-size: 1rem; }
.photo-help { margin: 0 0 0.6rem; font-size: 1rem; }
#photo-canvas {
  display: block; margin: 0 auto 0.6rem; max-width: 100%; max-height: 45vh; max-height: 45dvh; width: auto; height: auto;
  border-radius: 8px; touch-action: manipulation; cursor: crosshair;
}
#photo-ncanvas {
  display: block; margin: 0 auto 0.6rem; max-width: 100%; max-height: 55vh; max-height: 55dvh; width: auto; height: auto;
  border-radius: 8px; touch-action: manipulation; cursor: pointer;
}
#photo-canvas[hidden], #photo-ncanvas[hidden], .photo-help[hidden], .photo-kind[hidden] { display: none; }
#photo-result { margin-bottom: 0.5rem; }
.photo-kind { margin: 0 0 0.6rem; font-size: 1rem; color: var(--muted); }
.link-btn {
  font: inherit; font-weight: 700; color: var(--red); background: none; border: 0; padding: 0.35rem 0.1rem;
  text-decoration: underline; cursor: pointer; min-height: 44px;
}
.chip-unsure { display: inline-block; padding: 0 0.4rem; border-radius: 6px; background: #ff9f1a; color: #1c1917; font-weight: 700; }
/* Number picker for a tapped half */
.picker { background: #fff; border: 2px solid var(--ink); border-radius: 10px; padding: 0.6rem 0.75rem; margin: 0 0 0.75rem; }
.picker[hidden] { display: none; }
.picker-label { margin: 0 0 0.5rem; font-weight: 700; }
.picker-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 0.4rem; margin-bottom: 0.6rem; }
.picker-grid button {
  min-height: 44px; font-size: 1.15rem; font-weight: 700; border: 2px solid var(--ink); border-radius: 8px;
  background: #fff; color: var(--ink); font-variant-numeric: tabular-nums; cursor: pointer; padding: 0;
}
.picker-grid button[aria-pressed="true"] { background: #2f6fd6; border-color: #2f6fd6; color: #fff; }
.picker-actions { display: flex; gap: 0.5rem; justify-content: flex-end; }
/* Use / Cancel stay pinned to the bottom of the screen however tall the photo is. */
.photo-dialog .dialog-actions {
  position: sticky;
  bottom: -1rem; /* cover the dialog's bottom padding too */
  margin: 0 -1.1rem -1rem;
  padding: 0.75rem 1.1rem calc(0.75rem + env(safe-area-inset-bottom));
  background: #fff;
  border-top: 1px solid var(--line);
  box-shadow: 0 -4px 12px rgb(0 0 0 / 0.08);
  z-index: 1;
}
/* 📷 on the score box you're in, sitting just above the box. */
.sheet tbody td { position: relative; }
.cell-camera {
  position: absolute;
  left: 50%;
  bottom: calc(100% - 6px);
  transform: translateX(-50%);
  z-index: 5;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 36px;
  border-radius: 18px;
  background: var(--red);
  border: 2px solid #fff;
  box-shadow: 0 2px 8px rgb(0 0 0 / 0.35);
  font-size: 1.1rem;
  cursor: pointer;
  overflow: hidden;
}
.cell-camera[hidden] { display: none; }
.score.flash { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: #b8f0c8; } to { background: transparent; } }

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

@media (max-width: 600px) {
  body { font-size: 18px; }
  :root { --cell-w: 4.25rem; }
  .topbar h1 { font-size: 1.25rem; }
  .sheet .round-col { width: 4.25rem; min-width: 4.25rem; }
  main { padding: 0.5rem; }
  .name { font-size: 0.85rem; padding: 1.1rem 0.15rem 0.5rem; }
  .topbar { padding: 0.6rem 0.5rem; }
  .actions { gap: 0.4rem; }
  .btn { font-size: 0.95rem; padding: 0.4rem 0.65rem; }
  .start-at { font-size: 0.95rem; gap: 0.3rem; }
  .start-at select { padding: 0.25rem 0.3rem; }
  .wide-only { display: none; }
}

/* Crowded table on a phone (6–8 players): narrower round column, smaller text. */
@media (max-width: 600px) {
  .sheet[data-count="6"] .round-col, .sheet[data-count="7"] .round-col, .sheet[data-count="8"] .round-col { width: 2.9rem; min-width: 0; }
  .sheet[data-count="6"] .domino, .sheet[data-count="7"] .domino, .sheet[data-count="8"] .domino { font-size: 0.72rem; border-width: 1.5px; border-radius: 4px; }
  .sheet[data-count="6"] .domino span, .sheet[data-count="7"] .domino span, .sheet[data-count="8"] .domino span { min-width: 1.45em; padding: 0.1rem 0; }
  .sheet[data-count="6"] .corner, .sheet[data-count="7"] .corner, .sheet[data-count="8"] .corner { font-size: 0.65rem; }
  .sheet[data-count="6"] .name, .sheet[data-count="7"] .name, .sheet[data-count="8"] .name { font-size: 0.7rem; padding: 1rem 0.05rem 0.45rem; }
  .sheet[data-count="6"] .remove, .sheet[data-count="7"] .remove, .sheet[data-count="8"] .remove { width: 20px; height: 20px; font-size: 0.9rem; }
  .sheet[data-count="6"] .score, .sheet[data-count="7"] .score, .sheet[data-count="8"] .score { font-size: 1.15rem; padding: 0; }
  .sheet[data-count="6"] .total, .sheet[data-count="7"] .total, .sheet[data-count="8"] .total { font-size: 1.05rem; padding: 0.6rem 0 !important; }
  .sheet[data-count="6"] tfoot .round-col, .sheet[data-count="7"] tfoot .round-col, .sheet[data-count="8"] tfoot .round-col { font-size: 0.7rem; }
  .sheet[data-count="6"] .total.leader::after, .sheet[data-count="7"] .total.leader::after, .sheet[data-count="8"] .total.leader::after { content: "★"; font-size: 0.7rem; vertical-align: super; }
}
