:root {
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #efeeea;
  --border: #e3e1dc;
  --text: #1a1a1a;
  --text-muted: #6b6b6b;
  --accent: #d6451f;
  --accent-strong: #b53814;
  --accent-contrast: #ffffff;
  --danger: #c13b3b;
  --success: #2f8f5a;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
  --radius: 14px;
  --radius-sm: 10px;
  --tap: 56px;
  --gap: 10px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141414;
    --surface: #1d1d1d;
    --surface-2: #262626;
    --border: #2f2f2f;
    --text: #f3f3f3;
    --text-muted: #9e9e9e;
    --accent: #ee5a31;
    --accent-strong: #ff744a;
    --accent-contrast: #1a1a1a;
    --danger: #e25a5a;
    --success: #5cc28a;
    --shadow: 0 4px 14px rgba(0, 0, 0, 0.45), 0 1px 2px rgba(0, 0, 0, 0.3);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
    Arial, "Noto Sans", system-ui, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
}

#app {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px 16px calc(24px + var(--safe-bottom));
}

.screen {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.screen[hidden] {
  display: none;
}

/* ================= Setup ================= */

.setup-header {
  text-align: center;
  margin-top: 12px;
  margin-bottom: 4px;
}

.setup-header h1 {
  margin: 0;
  font-size: 32px;
  letter-spacing: -0.02em;
}

.setup-header .subtitle {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 14px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.card h2 {
  margin: 0 0 12px;
  font-size: 18px;
}

.player-list {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.player-list:empty::before {
  content: "Noch keine Spieler. Füge jemanden hinzu, um zu starten.";
  color: var(--text-muted);
  font-size: 14px;
  display: block;
  padding: 4px 2px 8px;
}

.player-row {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 6px 6px 6px 12px;
}

.player-row .name {
  flex: 1;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-row .order {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  min-width: 18px;
  text-align: right;
}

.icon-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 999px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover {
  background: var(--surface-2);
}

.icon-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.add-player {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.add-player input {
  flex: 1;
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 0 12px;
  height: 48px;
  font: inherit;
}

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

.recent-names {
  margin-top: 12px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.recent-label {
  color: var(--text-muted);
  font-size: 13px;
  margin-right: 2px;
}

.recent-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 999px;
  cursor: pointer;
}

.chip:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* ================= Buttons ================= */

.btn {
  appearance: none;
  border: 1px solid transparent;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  height: 48px;
  padding: 0 16px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 60ms ease;
}

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

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

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
}

.btn-primary:hover {
  background: var(--accent-strong);
}

.btn-secondary {
  background: var(--surface-2);
  border-color: var(--border);
}

.btn-danger {
  background: transparent;
  color: var(--danger);
  border-color: var(--border);
}

.btn-ghost {
  background: transparent;
}

.btn-outline {
  background: transparent;
  color: var(--accent);
  border-color: var(--accent);
}

.btn-outline:hover {
  background: var(--accent);
  color: var(--accent-contrast);
}

.btn-block {
  width: 100%;
}

/* ================= Toggle ================= */

.toggle {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px 12px;
  cursor: pointer;
  user-select: none;
}

.toggle input[type="checkbox"] {
  appearance: none;
  width: 44px;
  height: 26px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  position: relative;
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease;
}

.toggle input[type="checkbox"]::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
  transition: transform 120ms ease;
}

.toggle input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}

.toggle input[type="checkbox"]:checked::after {
  transform: translateX(18px);
}

.toggle-label {
  font-weight: 600;
}

.toggle-hint {
  grid-column: 2 / -1;
  color: var(--text-muted);
  font-size: 13px;
}

/* ================= Game Header ================= */

.game-header {
  display: grid;
  grid-template-columns: 36px 1fr 36px;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  margin-bottom: 4px;
}

.game-title {
  text-align: center;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.game-title-sub {
  display: block;
  font-size: 11px;
  font-weight: 500;
  margin-top: 2px;
  color: var(--text-muted);
  text-transform: none;
  letter-spacing: 0;
}

/* ================= Scoreboard ================= */

.scoreboard {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 8px;
}

.score-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  position: relative;
  box-shadow: var(--shadow);
  transition: border-color 120ms ease, transform 120ms ease;
}

.score-row.is-current {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.score-row.is-current::before {
  content: "";
  position: absolute;
  inset: -1px;
  border-radius: inherit;
  pointer-events: none;
  box-shadow: 0 0 0 2px var(--accent) inset;
}

.score-row .name {
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 500;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

.score-row.is-current .name {
  color: var(--accent);
}

.score-row .score {
  font-size: 32px;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

.score-row.is-winner {
  border-color: var(--success);
}

.score-row.is-winner .score {
  color: var(--success);
}

/* ================= Current turn ================= */

.current-turn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  box-shadow: var(--shadow);
}

.current-turn-label {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 6px;
}

.current-darts {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.dart-slot {
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 10px;
  text-align: center;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
  min-height: 38px;
}

.dart-slot.has-value {
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
}

.dart-slot.is-bust {
  color: var(--danger);
  text-decoration: line-through;
}

/* ================= Keypad ================= */

.keypad {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.multipliers {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--gap);
}

.mult-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  height: var(--tap);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 80ms ease, color 80ms ease, border-color 80ms ease;
}

.mult-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.mult-btn.is-active {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}

.numbers {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: var(--gap);
}

.specials {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: var(--gap);
}

.num-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  height: var(--tap);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 80ms ease, transform 60ms ease;
}

.num-btn:hover {
  background: var(--surface-2);
}

.num-btn:active {
  transform: scale(0.96);
}

.num-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.num-btn.miss {
  color: var(--text-muted);
}

/* ================= Sheet (Menu) ================= */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.sheet[hidden] {
  display: none;
}

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(2px);
}

.sheet-content {
  position: relative;
  width: 100%;
  max-width: 720px;
  background: var(--surface);
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  padding: 20px 16px calc(20px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.25);
  animation: sheet-in 180ms ease-out;
}

@keyframes sheet-in {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.sheet-content h3 {
  margin: 0 0 4px;
  font-size: 18px;
}

/* ================= Win Overlay ================= */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 30;
}

.overlay[hidden] {
  display: none;
}

.overlay-content {
  background: var(--surface);
  border-radius: 18px;
  padding: 24px;
  width: 100%;
  max-width: 420px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.4);
  animation: pop-in 200ms ease-out;
}

@keyframes pop-in {
  from {
    transform: scale(0.92);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

.trophy {
  font-size: 48px;
  line-height: 1;
}

.overlay-content h2 {
  margin: 0;
  font-size: 28px;
  letter-spacing: -0.02em;
}

.winner-sub {
  margin: 0;
  color: var(--text-muted);
}

.standings {
  list-style: none;
  margin: 4px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
}

.standings li {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
}

.standings li.is-winner {
  background: transparent;
  border-color: var(--success);
}

.standings .rank {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 13px;
}

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

.standings .score {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 18px;
}

.standings li.is-winner .score {
  color: var(--success);
}

/* ================= Misc ================= */

@media (max-width: 500px) {
  :root {
    --tap: 52px;
    --gap: 8px;
  }

  .numbers {
    grid-template-columns: repeat(4, 1fr);
  }

    .add-player button {
      flex: 1;
    }
}
