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

:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface-2: #252542;
  --accent: #e94560;
  --accent-light: #ff6b81;
  --gold: #f5c542;
  --text: #eaeaea;
  --text-muted: #8888aa;
  --success: #4ecca3;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html {
  font-size: 16px;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: 'Outfit', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
  background-image:
    radial-gradient(ellipse at 20% 0%, rgba(233, 69, 96, 0.12) 0%, transparent 50%),
    radial-gradient(ellipse at 80% 100%, rgba(78, 204, 163, 0.08) 0%, transparent 50%);
}

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  position: relative;
}

.screen {
  display: none;
  flex-direction: column;
  min-height: 100dvh;
  padding: 24px 20px calc(24px + var(--safe-bottom));
  animation: fadeIn 0.3s ease;
}

.screen.active {
  display: flex;
}

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

/* Hero */
.hero {
  text-align: center;
  margin: 48px 0 40px;
}

.logo {
  font-size: 3rem;
  font-weight: 800;
  letter-spacing: -1px;
  line-height: 1;
}

.logo span {
  color: var(--accent);
}

.tagline {
  margin-top: 12px;
  color: var(--text-muted);
  font-size: 1rem;
}

.rules-hint {
  margin-top: auto;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
}

/* Buttons */
.menu-buttons {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 16px 24px;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 1.05rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s, background 0.15s;
  touch-action: manipulation;
}

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

.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-light));
  color: white;
  box-shadow: 0 4px 20px rgba(233, 69, 96, 0.35);
}

.btn-secondary {
  background: var(--surface-2);
  color: var(--text);
  border: 2px solid rgba(255, 255, 255, 0.08);
}

.btn-outline {
  background: transparent;
  color: var(--text);
  border: 2px solid rgba(255, 255, 255, 0.15);
}

.btn-small {
  padding: 10px 18px;
  font-size: 0.9rem;
  width: auto;
}

.btn-back {
  background: none;
  border: none;
  color: var(--text-muted);
  font-family: inherit;
  font-size: 0.95rem;
  cursor: pointer;
  padding: 0;
  margin-bottom: 24px;
  align-self: flex-start;
}

/* Join screen */
.code-input {
  width: 100%;
  padding: 18px;
  font-family: inherit;
  font-size: 1.8rem;
  font-weight: 700;
  letter-spacing: 6px;
  text-align: center;
  text-transform: uppercase;
  background: var(--surface);
  border: 2px solid var(--surface-2);
  border-radius: var(--radius);
  color: var(--gold);
  margin: 24px 0;
  outline: none;
}

.code-input:focus {
  border-color: var(--accent);
}

.hint {
  color: var(--text-muted);
  line-height: 1.5;
  margin-top: 8px;
}

.hint.subtle {
  font-size: 0.9rem;
  margin-top: 16px;
}

.error-msg {
  color: var(--accent);
  text-align: center;
  margin-top: 12px;
  font-size: 0.9rem;
}

.hidden {
  display: none !important;
}

/* Lobby */
.code-display {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  text-align: center;
  margin: 24px 0;
  box-shadow: var(--shadow);
}

.code-label {
  display: block;
  color: var(--text-muted);
  font-size: 0.85rem;
  margin-bottom: 8px;
}

.code-value {
  display: block;
  font-size: 2.5rem;
  font-weight: 800;
  letter-spacing: 8px;
  color: var(--gold);
  margin-bottom: 16px;
}

.lobby-status {
  text-align: center;
  color: var(--text-muted);
  padding: 32px 0;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--surface-2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Calculator choice */
.round-badge {
  background: var(--surface-2);
  display: inline-block;
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 20px;
}

.choice-buttons {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 32px;
}

/* Game header */
.game-header {
  margin-bottom: 20px;
}

.scoreboard {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 12px;
}

.player-score {
  flex: 1;
  text-align: center;
}

.player-score:first-child {
  text-align: left;
}

.player-score:last-child {
  text-align: right;
}

.player-name {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.score-value {
  display: block;
  font-size: 1.6rem;
  font-weight: 800;
  color: var(--gold);
  line-height: 1.2;
}

.sets {
  display: block;
  font-size: 0.7rem;
  color: var(--text-muted);
}

.vs-divider {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--accent);
  padding: 0 8px;
}

.round-info {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.timer-bar {
  position: relative;
  height: 28px;
  background: var(--surface);
  border-radius: 14px;
  overflow: hidden;
}

.timer-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--success), #3db88a);
  border-radius: 14px;
  transition: width 0.3s linear;
  width: 100%;
}

.timer-fill.warning {
  background: linear-gradient(90deg, var(--gold), #e6a020);
}

.timer-fill.danger {
  background: linear-gradient(90deg, var(--accent), var(--accent-light));
}

.timer-text {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.9rem;
}

/* Target */
.target-display {
  text-align: center;
  background: linear-gradient(135deg, var(--surface), var(--surface-2));
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
  border: 2px solid rgba(245, 197, 66, 0.2);
}

.target-label {
  display: block;
  font-size: 0.8rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 4px;
}

.target-number {
  font-size: 3.5rem;
  font-weight: 800;
  color: var(--gold);
  line-height: 1;
}

/* Numbers grid */
.numbers-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}

.number-chip {
  aspect-ratio: 1.4;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-2);
  border: 2px solid transparent;
  border-radius: var(--radius-sm);
  font-size: 1.4rem;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s;
  touch-action: manipulation;
  user-select: none;
}

.number-chip:active:not(.used):not(.disabled) {
  transform: scale(0.95);
}

.number-chip.selected {
  border-color: var(--accent);
  background: rgba(233, 69, 96, 0.15);
}

.number-chip.used {
  opacity: 0.25;
  cursor: not-allowed;
  pointer-events: none;
}

.number-chip.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.number-chip.large {
  background: rgba(78, 204, 163, 0.12);
  color: var(--success);
}

.number-chip.result {
  background: rgba(245, 197, 66, 0.15);
  color: var(--gold);
  border-color: rgba(245, 197, 66, 0.3);
}

/* Calculator panel */
.calc-panel {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 16px;
}

.calc-display {
  width: 100%;
  padding: 12px;
  font-family: inherit;
  font-size: 1.4rem;
  font-weight: 600;
  text-align: right;
  background: var(--bg);
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text);
  margin-bottom: 10px;
}

.calc-keys {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.calc-key {
  padding: 14px 8px;
  background: var(--surface-2);
  border: none;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: inherit;
  font-size: 1.1rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

.calc-key:active {
  background: var(--accent);
}

.calc-key.wide {
  grid-column: span 2;
}

/* Play area */
.play-area {
  flex: 1;
}

.step-display {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  min-height: 80px;
  margin-bottom: 12px;
}

.step-hint {
  color: var(--text-muted);
  font-size: 0.9rem;
  text-align: center;
}

.step-line {
  font-size: 1.1rem;
  font-weight: 600;
  padding: 4px 0;
  text-align: center;
}

.step-line .result-val {
  color: var(--gold);
}

.op-buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.btn-op {
  padding: 16px;
  background: var(--surface-2);
  border: 2px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: inherit;
  font-size: 1.5rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  transition: all 0.15s;
}

.btn-op:active,
.btn-op.active {
  border-color: var(--accent);
  background: rgba(233, 69, 96, 0.15);
}

.btn-op:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.action-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.action-buttons .btn-primary {
  flex: 1;
  min-width: 120px;
}

/* Results */
.result-content {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  margin: 24px 0;
  text-align: center;
}

.result-row {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  font-size: 0.95rem;
}

.result-row:last-child {
  border-bottom: none;
}

.result-row.highlight {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--gold);
}

.result-big {
  font-size: 3rem;
  font-weight: 800;
  margin: 16px 0;
}

.result-big.exact {
  color: var(--success);
}

.result-big.close {
  color: var(--gold);
}

.result-big.far {
  color: var(--accent);
}

/* Final screen */
.final-content {
  text-align: center;
  margin: auto 0;
  padding: 32px 0;
}

.final-icon {
  font-size: 4rem;
  margin-bottom: 16px;
}

.final-content h2 {
  font-size: 2rem;
  margin-bottom: 12px;
}

.final-content p {
  color: var(--text-muted);
  line-height: 1.6;
}

.final-score {
  font-size: 1.2rem;
  color: var(--gold);
  font-weight: 700;
  margin-top: 16px;
}

/* Solo mode: hide p2 name adjustment */
.solo-mode #score-p2 .player-name {
  visibility: hidden;
}

.solo-mode .vs-divider {
  visibility: hidden;
}

@media (min-width: 481px) {
  .screen {
    padding-top: 40px;
  }
}
