*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --bg: #120a24;
  --bg-card: #1e1238;
  --bg-elevated: #2a1a4e;
  --text: #f4f0ff;
  --text-muted: #a89bc4;
  --accent: #c9a227;
  --accent-hover: #dbb83a;
  --accent-soft: rgba(201, 162, 39, 0.15);
  --success: #4ade80;
  --danger: #f87171;
  --warning: #fbbf24;
  --border: rgba(255, 255, 255, 0.08);
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  --font: "DM Sans", system-ui, sans-serif;
  --font-display: "Fraunces", Georgia, serif;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

body {
  margin: 0;
  min-height: 100dvh;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(120, 80, 200, 0.25), transparent),
    radial-gradient(ellipse 60% 40% at 100% 100%, rgba(201, 162, 39, 0.08), transparent),
    var(--bg);
}

.app {
  max-width: 520px;
  margin: 0 auto;
  padding: 1.25rem 1rem calc(5rem + var(--safe-bottom));
  min-height: 100dvh;
}

.main-views {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.screen {
  display: none;
  flex-direction: column;
  gap: 1rem;
  animation: fadeIn 0.25s ease;
}

.screen--active {
  display: flex;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.hero {
  text-align: center;
  padding: 1.5rem 0 0.5rem;
}

.hero-icon {
  font-size: 2.75rem;
  line-height: 1;
  margin-bottom: 0.5rem;
}

.hero h1 {
  font-family: var(--font-display);
  font-size: 2.25rem;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.02em;
}

.hero-sub {
  margin: 0.35rem 0 0;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.15rem;
  box-shadow: var(--shadow);
}

.card h2 {
  font-family: var(--font-display);
  font-size: 1.15rem;
  margin: 0 0 0.35rem;
}

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

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

.player-list-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.player-list-item--setup {
  align-items: stretch;
}

.player-picker-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  min-width: 0;
}

.player-picker {
  width: 100%;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  padding: 0.7rem 0.85rem;
}

.player-picker:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.player-name-input--active {
  display: block !important;
}

.player-name-input {
  flex: 1;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  padding: 0.7rem 0.85rem;
  min-width: 0;
}

.player-name-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn-remove {
  flex-shrink: 0;
  width: 2.5rem;
  height: 2.5rem;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font-size: 1.25rem;
  cursor: pointer;
  line-height: 1;
}

.btn-remove:hover,
.btn-remove:focus-visible {
  background: rgba(248, 113, 113, 0.12);
  color: var(--danger);
}

.row-actions {
  margin-top: 0.75rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  border: none;
  border-radius: var(--radius-sm);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  padding: 0.75rem 1.1rem;
  transition: background 0.15s, transform 0.1s;
}

.btn:active {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: var(--accent);
  color: #1a1033;
}

.btn-primary:hover:not(:disabled),
.btn-primary:focus-visible:not(:disabled) {
  background: var(--accent-hover);
}

.btn-ghost {
  background: var(--bg-elevated);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-ghost:hover:not(:disabled),
.btn-ghost:focus-visible:not(:disabled) {
  background: rgba(255, 255, 255, 0.06);
}

.btn-sm {
  padding: 0.45rem 0.75rem;
  font-size: 0.85rem;
}

.btn-block {
  width: 100%;
}

.btn-danger-text {
  color: var(--danger);
}

.btn-danger-text:hover:not(:disabled),
.btn-danger-text:focus-visible:not(:disabled) {
  background: rgba(248, 113, 113, 0.12);
  color: var(--danger);
}

.game-header {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
}

.game-header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.game-header h1 {
  font-family: var(--font-display);
  font-size: 1.35rem;
  margin: 0;
}

.round-info {
  font-size: 0.95rem;
  color: var(--text-muted);
}

.round-info strong {
  color: var(--text);
}

.bid-meter {
  margin-top: 0.75rem;
  padding: 0.65rem 0.85rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  text-align: center;
}

.bid-meter--balanced {
  background: rgba(74, 222, 128, 0.12);
  color: var(--success);
}

.bid-meter--over {
  background: rgba(251, 191, 36, 0.12);
  color: var(--warning);
}

.bid-meter--under {
  background: rgba(96, 165, 250, 0.12);
  color: #93c5fd;
}

.phase-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.35rem;
}

.phase-tab {
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-weight: 600;
  padding: 0.65rem;
  cursor: pointer;
}

.phase-tab--active {
  background: var(--accent-soft);
  color: var(--accent);
}

.phase-tab:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.score-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-card);
}

.score-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.score-table th,
.score-table td {
  padding: 0.65rem 0.5rem;
  text-align: center;
  border-bottom: 1px solid var(--border);
}

.score-table th:first-child,
.score-table td:first-child {
  text-align: left;
  padding-left: 0.85rem;
  max-width: 7rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.score-table th {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  font-weight: 600;
}

.score-table tbody tr:last-child td {
  border-bottom: none;
}

.score-table .col-total {
  font-weight: 700;
  color: var(--accent);
}

.num-control {
  display: inline-flex;
  align-items: center;
  gap: 0;
  background: var(--bg-elevated);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  overflow: hidden;
}

.num-control button {
  width: 2.25rem;
  height: 2.25rem;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 1.1rem;
  cursor: pointer;
  line-height: 1;
}

.num-control button:hover:not(:disabled),
.num-control button:focus-visible:not(:disabled) {
  background: rgba(255, 255, 255, 0.06);
}

.num-control button:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.num-control span {
  min-width: 1.75rem;
  text-align: center;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.num-control--locked span {
  color: var(--text-muted);
}

.round-score {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.round-score--positive {
  color: var(--success);
}

.round-score--negative {
  color: var(--danger);
}

.round-score--zero {
  color: var(--text-muted);
}

.game-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: auto;
}

.final-table {
  width: 100%;
  border-collapse: collapse;
}

.final-table th,
.final-table td {
  padding: 0.6rem 0.25rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.final-table th:last-child,
.final-table td:last-child {
  text-align: right;
  font-weight: 700;
  color: var(--accent);
}

.final-table tbody tr:last-child td {
  border-bottom: none;
}

.final-table tr.winner td {
  color: var(--success);
}

.final-table tr.winner td:last-child {
  color: var(--success);
}

.modal {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-card);
  color: var(--text);
  padding: 1.25rem;
  max-width: calc(100vw - 2rem);
  width: 22rem;
  box-shadow: var(--shadow);
}

.modal--wide {
  width: min(28rem, calc(100vw - 2rem));
}

.modal::backdrop {
  background: rgba(0, 0, 0, 0.6);
}

.modal h2 {
  font-family: var(--font-display);
  margin: 0 0 1rem;
  font-size: 1.25rem;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.history-scroll {
  max-height: 50vh;
  overflow-y: auto;
  margin-bottom: 1rem;
  font-size: 0.85rem;
}

.history-round {
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
}

.history-round:last-child {
  border-bottom: none;
}

.history-round h3 {
  margin: 0 0 0.4rem;
  font-size: 0.9rem;
  color: var(--accent);
}

.history-player {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  color: var(--text-muted);
  padding: 0.15rem 0;
}

.history-player span:last-child {
  font-variant-numeric: tabular-nums;
}

.bottom-nav {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: min(520px, 100%);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.35rem;
  padding: 0.5rem 1rem calc(0.65rem + var(--safe-bottom));
  background: rgba(18, 10, 36, 0.92);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}

.bottom-nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.55rem 0.5rem;
  cursor: pointer;
}

.bottom-nav-btn--active {
  background: var(--accent-soft);
  color: var(--accent);
}

.bottom-nav-icon {
  font-size: 1.15rem;
  line-height: 1;
}

.stats-header {
  text-align: center;
  padding: 0.5rem 0;
}

.stats-header h1 {
  font-family: var(--font-display);
  font-size: 2rem;
  margin: 0;
}

.stats-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.35rem;
}

.stats-tab {
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-weight: 600;
  padding: 0.65rem;
  cursor: pointer;
}

.stats-tab--active {
  background: var(--accent-soft);
  color: var(--accent);
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.65rem;
  margin-bottom: 0.65rem;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.85rem;
}

.stat-card-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.stat-card-value {
  font-size: 1.35rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

.stat-card-hint {
  margin-top: 0.2rem;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.player-stat-list {
  padding: 0.35rem;
}

.player-stat-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  width: 100%;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: inherit;
  text-align: left;
  padding: 0.8rem 0.65rem;
  cursor: pointer;
}

.player-stat-row:hover,
.player-stat-row:focus-visible {
  background: rgba(255, 255, 255, 0.04);
}

.player-stat-row + .player-stat-row {
  border-top: 1px solid var(--border);
}

.player-stat-name {
  font-weight: 700;
  font-size: 1rem;
}

.player-stat-id {
  font-size: 0.72rem;
  color: var(--text-muted);
  font-family: ui-monospace, monospace;
  margin-top: 0.1rem;
}

.player-stat-metrics {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.1rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  flex-shrink: 0;
}

.player-stat-metrics strong {
  color: var(--text);
}

.player-detail-header {
  margin-bottom: 1rem;
}

.player-detail-header h2 {
  margin: 0;
  font-family: var(--font-display);
}

.back-btn {
  align-self: flex-start;
  margin-bottom: 0.35rem;
}

.leaderboard-row {
  display: grid;
  grid-template-columns: 1.75rem 1fr auto;
  gap: 0.5rem;
  align-items: center;
  width: 100%;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: inherit;
  text-align: left;
  padding: 0.7rem 0.25rem;
  cursor: pointer;
}

.leaderboard-row:hover,
.leaderboard-row:focus-visible {
  background: rgba(255, 255, 255, 0.04);
}

.leaderboard-row + .leaderboard-row {
  border-top: 1px solid var(--border);
}

.leaderboard-rank {
  font-weight: 700;
  color: var(--accent);
}

.leaderboard-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.recent-game {
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
}

.recent-game:last-child {
  border-bottom: none;
}

.recent-game-top {
  font-size: 0.92rem;
}

.recent-game-meta {
  margin-top: 0.2rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.import-textarea {
  width: 100%;
  margin: 0 0 0.75rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font: inherit;
  padding: 0.75rem;
  resize: vertical;
}

.import-file {
  width: 100%;
  margin-bottom: 0.75rem;
  color: var(--text-muted);
  font-size: 0.85rem;
}

@media (min-width: 400px) {
  .score-table {
    font-size: 0.95rem;
  }

  .num-control button {
    width: 2.5rem;
    height: 2.5rem;
  }
}
