* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg: #161421;
  --surface: #1e1b2e;
  --card: #252238;
  --card-hover: #2c2940;
  --text: #f0ebe3;
  --text-soft: #c4bfb6;
  --muted: #7e7a90;
  --accent: #e07a5f;
  --accent-light: #f2a68d;
  --accent-glow: rgba(224, 122, 95, 0.12);
  --accent-glow-strong: rgba(224, 122, 95, 0.25);
  --border: #2e2b42;
  --border-light: #3a3752;
  --danger: #d4564e;
  --radius: 14px;
  --radius-sm: 10px;
  --font-display: "Quicksand", sans-serif;
  --font-body: "Nunito Sans", sans-serif;
}

body {
  font-family: var(--font-body);
  background: var(--bg);
  background-image: radial-gradient(ellipse at 50% 0%, rgba(224, 122, 95, 0.04) 0%, transparent 60%);
  color: var(--text);
  min-height: 100dvh;
  padding: 1rem 1rem 3rem;
  max-width: 600px;
  margin: 0 auto;
  -webkit-font-smoothing: antialiased;
}

/* Grain texture overlay */
.grain {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9999;
  opacity: 0.03;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  background-size: 200px;
}

.screen { display: none; }
.screen.active { display: block; }
.hidden { display: none !important; }

/* --- Entrance animations --- */

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(14px); }
  to { opacity: 1; transform: translateY(0); }
}

.screen.active .brand { animation: fadeUp 0.5s ease-out both; }
.screen.active .stagger-1 { animation: fadeUp 0.5s ease-out 0.08s both; }
.screen.active .stagger-2 { animation: fadeUp 0.5s ease-out 0.16s both; }
.screen.active .stagger-3 { animation: fadeUp 0.5s ease-out 0.24s both; }

/* --- Brand / Title --- */

.brand {
  text-align: center;
  padding: 1.5rem 0 1.25rem;
}

.brand h1 {
  font-family: var(--font-display);
  font-size: 1.9rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1;
}

.brand-swoop { color: var(--accent); }
.brand-score { color: var(--text); }

/* --- Cards --- */

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 0.875rem;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
}

.card h2 {
  font-family: var(--font-display);
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
  margin-bottom: 0.875rem;
}

/* --- Toggle --- */

.toggle-group {
  display: flex;
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 3px;
  margin-bottom: 1rem;
}

.toggle {
  flex: 1;
  padding: 0.55rem 0.5rem;
  border: none;
  background: transparent;
  font-family: var(--font-body);
  font-size: 0.88rem;
  font-weight: 500;
  cursor: pointer;
  color: var(--muted);
  transition: color 0.25s;
  position: relative;
  z-index: 1;
  border-radius: 7px;
}

.toggle.active {
  color: #fff;
  font-weight: 600;
}

.toggle-indicator {
  position: absolute;
  top: 3px;
  bottom: 3px;
  width: calc(50% - 3px);
  background: var(--accent);
  border-radius: 7px;
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  transform: translateX(calc(100% + 3px));
  box-shadow: 0 2px 8px rgba(224, 122, 95, 0.3);
}

.toggle-group:has(.toggle:first-of-type.active) .toggle-indicator {
  transform: translateX(0);
}

/* --- Score direction toggle --- */

.score-direction {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin-top: 0.875rem;
}

.score-direction label {
  font-size: 0.88rem;
  color: var(--text-soft);
  white-space: nowrap;
}

.toggle-sm {
  flex: 1;
}

.toggle-sm .toggle {
  font-size: 0.78rem;
  padding: 0.38rem 0.4rem;
}

/* --- Target input --- */

.target-input {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.target-input label {
  font-size: 0.88rem;
  color: var(--text-soft);
  white-space: nowrap;
}

.target-input input {
  width: 80px;
  padding: 0.45rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 1.05rem;
  font-weight: 600;
  text-align: center;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.target-input input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

/* --- Recent name tiles --- */

#recent-names {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.875rem;
}

#recent-names:empty { margin-bottom: 0; }

.name-tile {
  padding: 0.32rem 0.8rem;
  border: 1px solid var(--border-light);
  border-radius: 100px;
  background: var(--surface);
  color: var(--text-soft);
  font-family: var(--font-body);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.name-tile:hover {
  border-color: var(--accent);
  color: var(--accent-light);
  background: var(--accent-glow);
}

/* --- Player rows --- */

.player-row {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.4rem;
  animation: fadeUp 0.25s ease-out both;
}

.player-name {
  flex: 1;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 0.95rem;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.player-name::placeholder {
  color: var(--muted);
  font-weight: 400;
}

.player-name:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.remove-player {
  width: 36px;
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  color: var(--muted);
  transition: all 0.15s;
}

.remove-player:hover {
  color: var(--danger);
  border-color: var(--danger);
  background: rgba(212, 86, 78, 0.08);
}

/* --- Buttons --- */

.btn-primary {
  display: block;
  width: 100%;
  padding: 0.8rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-family: var(--font-body);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.2s, box-shadow 0.2s, transform 0.1s;
  letter-spacing: 0.01em;
  box-shadow: 0 2px 10px rgba(224, 122, 95, 0.2);
}

.btn-primary:hover {
  background: var(--accent-light);
  box-shadow: 0 4px 20px var(--accent-glow-strong);
}

.btn-primary:active { transform: scale(0.985); }

.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-soft);
  transition: all 0.15s;
}

.btn-icon:hover {
  border-color: var(--border-light);
  color: var(--text);
  background: var(--card-hover);
}

/* --- Scoring screen --- */

.scoring-header {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.75rem 0 1rem;
}

.scoring-title {
  font-family: var(--font-display);
  font-size: 1.15rem;
  font-weight: 700;
  flex: 1;
  letter-spacing: -0.01em;
}

#game-info {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
  padding: 0.3rem 0.7rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 100px;
}

/* --- Score grid --- */

.grid-wrapper {
  overflow-x: auto;
  margin-bottom: 0.875rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--card);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
}

#score-grid {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

#score-grid th,
#score-grid td {
  padding: 0.4rem 0.25rem;
  text-align: center;
  border-bottom: 1px solid var(--border);
}

#score-grid th:first-child,
#score-grid td:first-child {
  text-align: left;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 500;
  padding-left: 0.5rem;
  white-space: nowrap;
}

/* Header */
#score-grid thead th {
  background: var(--surface);
  font-family: var(--font-display);
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-soft);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  max-width: 72px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  position: sticky;
  top: 0;
  z-index: 2;
}

/* Body rows */
#score-grid tbody tr {
  background: var(--card);
  transition: background 0.1s;
}

#score-grid tbody tr:nth-child(odd) {
  background: rgba(30, 27, 46, 0.5);
}

#score-grid tbody tr:hover {
  background: var(--card-hover);
}

/* Totals row */
#score-grid tfoot td {
  background: var(--surface);
  font-weight: 700;
  font-size: 1.05rem;
  border-bottom: none;
  border-top: 2px solid var(--accent);
  color: var(--text-soft);
  padding-top: 0.6rem;
  padding-bottom: 0.6rem;
}

#score-grid tfoot td.leader {
  color: var(--accent-light);
}

/* Grid inputs */
#score-grid input {
  width: 40px;
  padding: 0.3rem 0.1rem;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  color: var(--text);
  font-family: var(--font-body);
  font-size: 16px;
  font-weight: 500;
  text-align: center;
  font-variant-numeric: tabular-nums;
  transition: all 0.15s;
}

#score-grid input.untouched {
  color: var(--muted);
  opacity: 0.4;
}

#score-grid input.untouched:focus {
  opacity: 1;
  color: var(--text);
}

#score-grid input:hover {
  background: var(--surface);
  border-color: var(--border);
}

#score-grid input:focus {
  outline: none;
  background: var(--surface);
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent-glow);
}

/* Score status message */
.score-status {
  font-size: 0.8rem;
  color: var(--accent);
  text-align: center;
  padding: 0.5rem 0.75rem;
  margin-bottom: 0.75rem;
  background: var(--accent-glow);
  border: 1px solid rgba(224, 122, 95, 0.2);
  border-radius: var(--radius-sm);
}

/* Grid actions */
.grid-actions {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.875rem;
}

.grid-actions #add-round {
  flex: 1;
}

.grid-actions #add-round.hidden + .btn-danger-ghost {
  width: 100%;
}

.btn-danger-ghost {
  padding: 0.5rem 0.75rem;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: var(--font-body);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-danger-ghost:hover {
  color: var(--danger);
  border-color: var(--danger);
  background: rgba(212, 86, 78, 0.08);
}

/* --- Game over --- */

#game-over {
  text-align: center;
  padding: 1.5rem 0;
  animation: fadeUp 0.4s ease-out both;
}

.winner-card {
  background: var(--card);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  padding: 2rem 1.5rem;
  box-shadow: 0 0 40px var(--accent-glow), 0 0 80px var(--accent-glow);
}

.winner-card h2 {
  font-family: var(--font-display);
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--accent-light);
  margin-bottom: 1.25rem;
}

.winner-card .btn-primary {
  max-width: 200px;
  margin: 0 auto;
}

/* --- Focus / accessibility --- */

button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Remove number spinners */
input[type="number"] { -moz-appearance: textfield; }
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; }
