:root {
  --bg: #f4f4f7;
  --surface: #ffffff;
  --surface-2: #ececf2;
  --text: #16161d;
  --muted: #6b6b7b;
  --border: #dedee6;
  --primary: #09a4f4;        /* vibrant blue for filled highlights */
  --primary-text: #082f49;   /* dark navy reads well on the light blue */
  --link: #0284c7;           /* deeper blue for text so it's readable on white */
  --danger: #dc2626;
  --leader: #d97706;
  --radius: 14px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0e13;
    --surface: #1b1b23;
    --surface-2: #262631;
    --text: #f2f2f7;
    --muted: #9a9aac;
    --border: #2f2f3b;
    --link: #09a4f4;
    --danger: #f05252;
    --leader: #fbbf24;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

button, input { font: inherit; color: inherit; }

button {
  touch-action: manipulation;
  cursor: pointer;
  border: none;
  border-radius: 12px;
  padding: 12px 16px;
  font-weight: 600;
  background: var(--surface-2);
  user-select: none;
  -webkit-user-select: none;
}
button:active { transform: scale(0.97); }
[hidden] { display: none !important; }

.primary { background: var(--primary); color: var(--primary-text); }
.secondary { background: var(--surface-2); }
.danger { background: var(--danger); color: #fff; }
.ghost { background: transparent; color: var(--link); }
.danger-text { color: var(--danger); }
.wide { width: 100%; }

/* Top bar */
.topbar {
  position: relative;
  z-index: 5; /* keeps the dropdown above the table's pinned columns */
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
}
.topbar h1 { margin: 0; font-size: 1.5rem; letter-spacing: -0.01em; }

main { padding: 0 16px 24px; max-width: 900px; margin: 0 auto; }

/* Gear button and its dropdown menu */
.menu-wrap { position: relative; }
.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
}
.icon-btn[aria-expanded="true"] { background: var(--surface-2); color: var(--text); }

.menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 10;
  width: 264px;
  padding: 6px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 12px 32px rgb(0 0 0 / 0.25);
  display: grid;
  gap: 2px;
}
.menu-section {
  display: grid;
  gap: 8px;
  padding: 8px 8px 12px;
  margin-bottom: 4px;
  border-bottom: 1px solid var(--border);
}
.menu-label { font-size: 0.8rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.menu-item {
  width: 100%;
  padding: 12px 10px;
  border-radius: 10px;
  background: transparent;
  text-align: left;
  font-weight: 500;
}
.menu-item:active { transform: none; background: var(--surface-2); }
.menu-item:disabled { opacity: 0.4; cursor: default; }
@media (hover: hover) { .menu-item:not(:disabled):hover { background: var(--surface-2); } }

/* High / low score toggle */
.segmented {
  display: flex;
  padding: 3px;
  border-radius: 12px;
  background: var(--surface-2);
}
.segmented button {
  flex: 1;
  padding: 8px 10px;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
}
.segmented button[aria-pressed="true"] {
  background: var(--primary);
  color: var(--primary-text);
}

/* Score table: scrolls sideways inside its box when there are many rounds,
   with the names pinned on the left and totals pinned on the right. */
.table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border-radius: var(--radius);
  -webkit-overflow-scrolling: touch;
}

.scores {
  border-collapse: separate;
  border-spacing: 0;
  min-width: 100%;
  font-variant-numeric: tabular-nums;
}

.scores th, .scores td {
  padding: 0;
  height: 52px;
  border-bottom: 1px solid var(--border);
  text-align: center;
  white-space: nowrap;
}
.scores tbody tr:last-child td, .scores tbody tr:last-child th { border-bottom: none; }

.scores thead th {
  height: 40px;
  padding: 0 8px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.scores .name-col {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--surface);
  text-align: left;
  border-right: 1px solid var(--border);
}
.scores .total-col {
  position: sticky;
  right: 0;
  z-index: 1;
  background: var(--surface);
  border-left: 1px solid var(--border);
  padding: 0 12px;
  min-width: 64px;
  font-weight: 700;
  font-size: 1.1rem;
}
.scores thead .total-col { font-size: 0.8rem; }

/* Whoever is currently winning */
.scores tr.leader .total-col { color: var(--leader); }
.crown { display: none; flex: none; margin-left: 2px; }
.scores tr.leader .crown { display: inline; }

/* Names are typed straight into the cell. The box is sized to the name (see
   fitName in app.js) so the crown sits right after it. */
.name-cell {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  height: 100%;
  padding: 0 8px;
  background: var(--surface); /* covers the Delete button until swiped */
  transition: transform 0.2s ease;
}
.name-cell.dragging { transition: none; user-select: none; -webkit-user-select: none; }

/* Swipe a name left to reveal Delete. The name column handles sideways
   swipes itself instead of scrolling the table. */
.scores tr[data-player] .name-col { overflow: hidden; touch-action: pan-y; }
.swipe-delete {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 76px;
  padding: 0;
  border-radius: 0;
  background: var(--danger);
  color: #fff;
  font-weight: 600;
}
.swipe-delete:active { transform: none; opacity: 0.85; }
.name-input {
  min-width: 2ch;
  max-width: 120px;
  height: 36px;
  padding: 0 4px;
  border: none;
  border-radius: 6px;
  background: transparent;
  font-size: 16px; /* 16px or more stops iOS zooming in on focus */
  font-weight: 600;
  text-overflow: ellipsis;
}
.name-input:focus {
  outline: 2px solid var(--primary);
  background: var(--surface-2);
}
.new-player .name-input { width: 120px; font-weight: 400; }
.new-player .name-input::placeholder { color: var(--link); opacity: 1; }
/* Once tapped, "+ Player" disappears and it's just an empty edit box. */
.new-player .name-input:focus::placeholder { color: transparent; }

.round-head {
  min-width: 64px;
}
/* Tappable Player / Total headers; the active sort is blue with ▲ or ▼. */
.sort-btn {
  padding: 6px 4px;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.sort-btn[data-dir] { color: var(--link); }
.sort-btn[data-dir="asc"]::after { content: " ▲"; font-size: 0.7em; }
.sort-btn[data-dir="desc"]::after { content: " ▼"; font-size: 0.7em; }

.round-btn {
  padding: 6px 8px;
  color: var(--muted);
  font-size: 0.8rem;
  letter-spacing: 0.04em;
}

.score-input {
  width: 64px;
  height: 52px;
  padding: 0 4px;
  border: none;
  background: transparent;
  text-align: center;
  font-size: 1.1rem;
  -moz-appearance: textfield;
}
.score-input::-webkit-outer-spin-button,
.score-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.score-input:focus {
  outline: 2px solid var(--primary);
  outline-offset: -2px;
  border-radius: 6px;
  background: var(--surface-2);
}

.actions {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 10px;
  margin-top: 16px;
}

/* Bottom sheet */
.sheet {
  width: 100%;
  max-width: 520px;
  margin: auto auto 0;
  padding: 20px 16px calc(16px + env(safe-area-inset-bottom));
  border: none;
  border-radius: 20px 20px 0 0;
  background: var(--surface);
  color: var(--text);
}
.sheet::backdrop { background: rgb(0 0 0 / 0.45); }
.sheet form { display: grid; gap: 12px; }
.sheet-title { margin: 0; font-size: 1.15rem; font-weight: 700; text-align: center; }
.sheet-message { margin: 0 0 4px; text-align: center; color: var(--muted); line-height: 1.4; }
.sheet-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
