:root {
  color-scheme: dark;
  --bg: #100b1c;
  --surface: rgba(255, 255, 255, 0.04);
  --surface-hover: rgba(255, 255, 255, 0.08);
  --border: rgba(255, 255, 255, 0.08);
  --text: #f5f2ff;
  --muted: #8f85a8;
  --purple: #a970ff;
  --purple-deep: #7b3ed8;
  --pink: #f46ebd;
  --mint: #68e0bd;
  --glow-purple: 0 0 60px rgba(169, 112, 255, 0.15);
  --glow-pink: 0 0 60px rgba(244, 110, 189, 0.12);
  --shadow-card: 0 8px 32px rgba(0, 0, 0, 0.3);
  --radius: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  height: -webkit-fill-available;
}

body {
  min-height: 100vh;
  min-height: -webkit-fill-available;
  margin: 0;
  color: var(--text);
  font-family: "DM Sans", -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}

body::before {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(ellipse 100% 60% at 50% -20%, rgba(169, 112, 255, 0.12), transparent),
    radial-gradient(ellipse 60% 40% at 90% 30%, rgba(244, 110, 189, 0.08), transparent);
  content: "";
}

button, input, select {
  font: inherit;
}

button {
  color: inherit;
  touch-action: manipulation;
}

.shell {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: -webkit-fill-available;
  max-width: 480px;
  margin: 0 auto;
  padding: 0 16px calc(var(--safe-bottom) + 24px);
}

/* ─── Top bar ─── */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 60px;
  padding: 8px 0;
}

.brand {
  display: flex;
  gap: 8px;
  align-items: center;
  color: var(--text);
  font-family: "Space Grotesk", sans-serif;
  font-weight: 700;
  font-size: 0.95rem;
  text-decoration: none;
}

.brand-icon {
  border-radius: 8px;
  flex-shrink: 0;
}

.mode-button {
  display: flex;
  gap: 6px;
  align-items: center;
  min-height: 36px;
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 0 14px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.85rem;
  background: var(--surface);
  transition: background 0.15s, border-color 0.15s;
}

.mode-button:active {
  background: var(--surface-hover);
  transform: scale(0.97);
}

.mode-button.active {
  border-color: rgba(104, 224, 189, 0.35);
  color: var(--mint);
  background: rgba(104, 224, 189, 0.08);
}

.mode-button .icon-lock {
  opacity: 0.6;
}

.mode-button.active .icon-lock {
  opacity: 1;
}

/* ─── Score card ─── */

.score-card {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-card);
}

.score-card::after {
  position: absolute;
  right: -80px;
  bottom: -120px;
  width: 260px;
  height: 260px;
  border-radius: 50%;
  background: rgba(169, 112, 255, 0.06);
  content: "";
  pointer-events: none;
}

/* ─── Empty state ─── */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  min-height: 380px;
  padding: 48px 24px;
  text-align: center;
  justify-content: center;
}

.empty-logo {
  margin-bottom: 8px;
  opacity: 0.7;
}

.empty-state h2 {
  margin: 0;
  font-family: "Space Grotesk", sans-serif;
  font-size: 1.4rem;
}

.empty-state p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.empty-state .primary-button {
  margin-top: 16px;
}

/* ─── Match view ─── */

.match-view {
  padding: 16px;
}

.match-meta {
  display: flex;
  justify-content: space-between;
  padding-bottom: 16px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.serve-label {
  color: var(--mint);
}

/* ─── Players ─── */

.players {
  display: grid;
  grid-template-columns: 1fr 32px 1fr;
  align-items: start;
  gap: 0;
}

.player {
  text-align: center;
  min-width: 0;
}

.player-name {
  overflow: hidden;
  margin: 0 0 2px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.score {
  width: 100%;
  border: 0;
  padding: 0;
  color: var(--text);
  font-family: "Space Grotesk", sans-serif;
  font-size: clamp(4.5rem, 28vw, 7rem);
  font-weight: 700;
  line-height: 1;
  letter-spacing: -0.08em;
  background: transparent;
  transition: transform 0.08s;
}

body:not(.scorer) .score {
  cursor: default;
}

body.scorer .score {
  cursor: pointer;
}

.score:active {
  transform: scale(0.92);
}

.player-one .score { text-shadow: var(--glow-purple); }
.player-two .score { text-shadow: var(--glow-pink); }

.divider {
  display: grid;
  place-items: center;
  padding-top: 28px;
}

.divider span {
  z-index: 1;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 50%;
  font-size: 0.55rem;
  font-weight: 700;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.04);
}

.score-controls {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: center;
  margin-top: 4px;
}

.score-controls button {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.06);
  transition: background 0.12s, transform 0.1s;
}

.score-controls button:active {
  background: rgba(255, 255, 255, 0.14);
  transform: scale(0.9);
}

.score-controls button svg {
  stroke: var(--muted);
}

/* ─── Match actions ─── */

.match-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.primary-button,
.secondary-button {
  flex: 1;
  max-width: 160px;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
  font-weight: 700;
  font-size: 0.9rem;
  transition: transform 0.1s;
}

.secondary-button {
  background: var(--surface);
}

.secondary-button:active {
  background: var(--surface-hover);
  transform: scale(0.97);
}

.primary-button {
  border: 0;
  color: white;
  background: linear-gradient(135deg, var(--purple), var(--purple-deep));
  box-shadow: 0 8px 24px rgba(116, 52, 223, 0.25);
}

.primary-button:active {
  transform: scale(0.97);
  opacity: 0.9;
}

.scorer-only {
  display: none;
}

body.scorer .scorer-only {
  display: flex;
}

/* ─── History ─── */

.history-section {
  padding-top: 24px;
}

.section-heading h2 {
  margin: 0 0 12px;
  font-family: "Space Grotesk", sans-serif;
  font-size: 1.2rem;
  color: var(--muted);
}

.history-list {
  display: grid;
  gap: 8px;
}

.history-item {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  background: var(--surface);
}

.history-player {
  font-size: 0.85rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.history-player.winner {
  color: var(--mint);
  font-weight: 600;
}

.history-score {
  font-family: "Space Grotesk", sans-serif;
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
}

.history-empty {
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 20px;
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}

/* ─── Footer ─── */

.footer {
  display: flex;
  gap: 6px;
  align-items: center;
  justify-content: center;
  margin-top: auto;
  padding: 32px 0 0;
  color: var(--muted);
  font-size: 0.75rem;
  opacity: 0.5;
}

.footer-logo {
  opacity: 0.6;
}

/* ─── Dialogs ─── */

dialog {
  width: calc(100% - 32px);
  max-width: 400px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  color: var(--text);
  background: #1b1430;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.5);
  overscroll-behavior: contain;
}

dialog::backdrop {
  background: rgba(7, 4, 12, 0.7);
  backdrop-filter: blur(4px);
}

dialog form {
  display: grid;
  gap: 16px;
  padding: 20px;
}

.dialog-heading {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-heading h2 {
  margin: 0;
  font-family: "Space Grotesk", sans-serif;
  font-size: 1.2rem;
}

.dialog-sub {
  margin: -8px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.4;
}

.icon-button {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.1rem;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.06);
}

.icon-button:active {
  background: rgba(255, 255, 255, 0.12);
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

input,
select {
  width: 100%;
  min-height: 46px;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0 14px;
  color: var(--text);
  font-size: 1rem;
  outline: 0;
  background: rgba(255, 255, 255, 0.05);
  -webkit-appearance: none;
  appearance: none;
}

input:focus,
select:focus {
  border-color: var(--purple);
  box-shadow: 0 0 0 3px rgba(169, 112, 255, 0.15);
}

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238f85a8' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}

select option {
  background: #1b1430;
}

.full-width {
  width: 100%;
}

.form-error {
  min-height: 18px;
  margin: -4px 0;
  color: #ff8fa8;
  font-size: 0.82rem;
  line-height: 1.3;
}

/* ─── Toast ─── */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-bottom));
  z-index: 100;
  transform: translateX(-50%) translateY(20px);
  opacity: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 20px;
  color: var(--text);
  font-size: 0.85rem;
  font-weight: 600;
  background: #2b203c;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
  transition: opacity 0.2s, transform 0.25s cubic-bezier(0.16, 1, 0.3, 1);
  pointer-events: none;
  white-space: nowrap;
}

.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ─── Utilities ─── */

.hidden {
  display: none !important;
}
