:root {
  --bg: #12151b;
  --panel: #1c2129;
  --panel-2: #262c36;
  --line: #333b47;
  --text: #e9edf2;
  --muted: #8b95a3;
  --accent: #2fbf71;
  --accent-ink: #06210f;
  --double: #e0a526;
  --triple: #e0523f;
  --radius: 14px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
}

[hidden] { display: none !important; }

button {
  font: inherit;
  color: inherit;
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 10px;
  padding: 0.6rem 1rem;
  cursor: pointer;
  transition: background 0.1s, transform 0.05s, border-color 0.1s;
  touch-action: manipulation; /* no double-tap zoom on mobile */
  -webkit-tap-highlight-color: transparent;
}
button:hover:not(:disabled) { border-color: var(--muted); }
button:active:not(:disabled) { transform: scale(0.97); }
button:disabled { opacity: 0.35; cursor: not-allowed; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
  font-weight: 700;
}
button.secondary { background: transparent; }

kbd {
  font: 600 0.7em ui-monospace, monospace;
  padding: 0.1em 0.4em;
  border: 1px solid currentColor;
  border-radius: 4px;
  opacity: 0.6;
  margin-left: 0.4em;
}

.hint { color: var(--muted); font-size: 0.85rem; font-weight: 400; }

/* ---------------------------------------------------------- setup */

.setup {
  max-width: 640px;
  margin: 0 auto;
  padding: 3rem 1.5rem;
}
.setup h1 { font-size: 2.2rem; margin: 0 0 2rem; }
.setup h2 { font-size: 1.1rem; margin: 0 0 0.75rem; display: flex; gap: 0.75rem; align-items: baseline; }
.setup section { margin-bottom: 2rem; }

.segmented { display: inline-flex; gap: 0.5rem; margin: 0 1rem 0.5rem 0; }
.segmented button { font-size: 1.4rem; font-weight: 700; padding: 0.7rem 2rem; }
.segmented button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.player-list { list-style: none; margin: 0 0 1rem; padding: 0; display: grid; gap: 0.5rem; }
.player-list li {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.5rem 0.75rem;
}
.player-list li.selected { border-color: var(--accent); }
.player-list li.empty { color: var(--muted); justify-content: center; padding: 1.25rem; }
.player-list .pick {
  width: 2.4rem; height: 2.4rem; padding: 0;
  border-radius: 50%; font-weight: 700;
}
.player-list li.selected .pick { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.player-list .name { flex: 1; font-size: 1.15rem; font-weight: 600; }
.player-list .stats { color: var(--muted); font-size: 0.85rem; }
.player-list .icon { padding: 0.3rem 0.6rem; background: transparent; border-color: transparent; color: var(--muted); }
.player-list .icon:hover { color: var(--text); }
.player-list .icon.danger:hover { color: var(--triple); }

.add-player { display: flex; gap: 0.5rem; }
.add-player input {
  flex: 1;
  font: inherit;
  color: inherit;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.6rem 0.9rem;
}

.setup-actions { display: flex; gap: 0.75rem; justify-content: flex-end; }
.setup-actions button { font-size: 1.15rem; padding: 0.9rem 1.6rem; }

/* ---------------------------------------------------------- game */
/* Base (mobile) layout: players on top, current turn, buttons below.
   Desktop switches to players-left further down. */

.game {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto auto 1fr;
  gap: 0.75rem;
  padding:
    max(1rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right))
    max(1rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
  overflow: hidden;
}

.sidebar { display: grid; gap: 0.6rem; min-height: 0; }
.sidebar-actions { display: flex; gap: 0.5rem; }
.sidebar-actions button { flex: 1; }

.topbar { display: flex; align-items: center; gap: 0.5rem; }
.mode-label { flex: 1; text-align: center; font-weight: 700; color: var(--muted); letter-spacing: 0.05em; }

.scoreboard {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 0.6rem;
  max-height: 38dvh;
  overflow-y: auto;
}
.card {
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  padding: 0.6rem 1rem;
  opacity: 0.7;
  transition: opacity 0.15s, border-color 0.15s;
}
.card.active { opacity: 1; border-color: var(--accent); box-shadow: 0 0 0 4px rgb(47 191 113 / 0.15); }
.card.winner { opacity: 1; border-color: var(--double); }
.card-name { font-weight: 600; font-size: 1.1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-score { font-size: 3rem; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.card-meta { display: flex; gap: 1rem; color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.card-last {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-top: 0.4rem;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}
.card-last-points { color: var(--muted); margin-right: 0.25rem; min-width: 5.5ch; }
.card-last.bust .card-last-points { color: var(--triple); font-weight: 700; }
.dart-chip {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.05rem 0.45rem;
  font-weight: 600;
}

/* collapsed: one scrollable row, only name + score */
.players-collapsed .card-meta,
.players-collapsed .card-last,
.players-collapsed .sidebar-actions { display: none; }
.players-collapsed .scoreboard { display: flex; overflow-x: auto; overflow-y: hidden; }
.players-collapsed .card {
  flex: 0 0 auto;
  min-width: 9rem;
  display: flex; align-items: baseline; justify-content: space-between; gap: 0.75rem;
}
.players-collapsed .card-score { font-size: 1.6rem; }

.turn {
  display: grid;
  grid-template-columns: 1fr auto auto;
  grid-template-areas: "player darts total" "checkout checkout checkout";
  align-items: center;
  gap: 0.25rem 1.25rem;
  background: var(--panel);
  border-radius: var(--radius);
  padding: 0.6rem 1.25rem;
}
.turn-player { grid-area: player; font-size: 1.5rem; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.turn-darts { grid-area: darts; display: flex; gap: 0.5rem; }
.slot {
  width: 5.5rem; height: 3rem;
  display: grid; place-items: center;
  border: 2px dashed var(--line);
  border-radius: 10px;
  font-size: 1.3rem; font-weight: 700;
}
.slot.filled { border-style: solid; border-color: var(--muted); background: var(--panel-2); }
.turn-total { grid-area: total; font-size: 1.8rem; font-weight: 800; min-width: 4ch; text-align: right; font-variant-numeric: tabular-nums; }
.checkout { grid-area: checkout; color: var(--double); font-weight: 600; min-height: 1.3em; }

.pad {
  display: grid;
  grid-template-rows: 1fr auto;
  gap: 0.6rem;
  min-height: 0;
}
.pad-numbers {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-auto-rows: 1fr;
  gap: 0.6rem;
  min-height: 0;
}
.pad button { font-size: 2rem; font-weight: 700; border-radius: var(--radius); }
.num { font-variant-numeric: tabular-nums; }
.num.bull { background: #1f3a2b; border-color: #2c5a40; }
.num.bullseye { background: #8f2424; border-color: #b33a3a; color: #fff; }
.num.bullseye:hover:not(:disabled) { border-color: #e06060; }
.num.miss { grid-column: span 3; background: transparent; color: var(--muted); }

.pad-side { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0.6rem; min-height: 4rem; }
.mod { font-size: 1.5rem !important; }
#mod-double.active { background: var(--double); border-color: var(--double); color: #2a1c00; }
#mod-triple.active { background: var(--triple); border-color: var(--triple); color: #2a0600; }
.undo { font-size: 1.4rem !important; background: transparent; }

/* modifier tint on number buttons for visual feedback */
.pad:has(#mod-double.active) .num:not(:disabled):not(.miss) { border-color: var(--double); }
.pad:has(#mod-triple.active) .num:not(:disabled):not(.miss) { border-color: var(--triple); }

/* ---------------------------------------------------------- winner overlay */

.overlay {
  position: fixed; inset: 0;
  background: rgb(0 0 0 / 0.6);
  display: grid; place-items: center;
  backdrop-filter: blur(3px);
}
.dialog {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 2rem 2.5rem;
  text-align: center;
  min-width: min(380px, 90vw);
}
.trophy { font-size: 3.5rem; }
.dialog h2 { font-size: 2rem; margin: 0.5rem 0; }
.dialog p { color: var(--muted); margin: 0 0 1.5rem; }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 0.6rem; justify-content: center; }

/* ---------------------------------------------------------- statistics */

.stats-overlay { grid-template-rows: minmax(0, 1fr); place-items: stretch; padding: 2rem; z-index: 10; }
.stats-panel {
  width: min(1100px, 100%);
  margin: 0 auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  display: grid;
  grid-template-rows: auto 1fr;
  align-self: start;
  min-height: 0;
  max-height: 100%;
  overflow: hidden;
}
.stats-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem 1.25rem;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--line);
}
.stats-header h2 { margin: 0; font-size: 1.4rem; }
.stats-header .tabs { flex: 1; margin: 0; }
.stats-header .tabs button { font-size: 1rem; padding: 0.5rem 1rem; }
.stats-body { overflow-y: auto; padding: 1.25rem; min-height: 0; }
.stats-body h3 { font-size: 1rem; margin: 1.5rem 0 0.6rem; }
.stats-body h3:first-child { margin-top: 0; }
.stats-body > .hint { margin: 0 0 1rem; }
.empty-state { color: var(--muted); text-align: center; padding: 3rem 1rem; }

.segmented.small button { font-size: 0.9rem; padding: 0.35rem 0.9rem; }
.stats-toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.5rem; margin-bottom: 1rem; }
.stats-toolbar .segmented { margin: 0; }
.danger-text { color: var(--triple); }

.table-scroll { overflow-x: auto; }
.stats-table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
  font-size: 0.95rem;
}
.stats-table th, .stats-table td {
  padding: 0.45rem 0.75rem;
  border-bottom: 1px solid var(--line);
  text-align: right;
  white-space: nowrap;
}
.stats-table thead th { color: var(--muted); font-weight: 600; font-size: 0.85rem; }
.stats-table tbody th { text-align: left; color: var(--muted); font-weight: 500; }
.stats-table.players tbody th { color: var(--text); font-weight: 700; }
.stats-table th:first-child { text-align: left; }
.stats-table .is-winner { color: var(--double); }
.stats-table tbody tr:hover { background: var(--panel-2); }

.stats-table.rounds td { line-height: 1.15; }
.round-score { display: block; font-weight: 700; }
.round-rest { display: block; font-size: 0.75rem; color: var(--muted); }
.stats-table.rounds td.ton .round-score { color: var(--accent); }
.stats-table.rounds td.max .round-score { color: var(--double); }
.stats-table.rounds td.bust .round-score { color: var(--triple); }

.history-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.6rem; }
.history-item { background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); }
.history-item.open { border-color: var(--muted); }
.history-item .game-detail { padding: 0 1rem 1rem; }
.history-head { display: flex; align-items: center; gap: 0.5rem; padding-right: 0.5rem; }
.history-toggle {
  flex: 1;
  text-align: left;
  background: transparent;
  border: none;
  padding: 0.75rem 1rem;
  display: grid;
  gap: 0.3rem;
}
.history-toggle:active:not(:disabled) { transform: none; }
.history-meta { color: var(--muted); font-size: 0.85rem; }
.history-players { display: flex; flex-wrap: wrap; gap: 0.25rem 1.25rem; font-weight: 600; }
.history-players small { color: var(--muted); font-weight: 400; }
.history-players .is-winner { color: var(--double); }
.history-head .icon { background: transparent; border-color: transparent; color: var(--muted); padding: 0.3rem 0.6rem; }
.history-head .icon:hover { color: var(--triple); }

/* ================================================================ desktop */
/* Players as a list on the left, turn + buttons on the right. */

@media (min-width: 761px) and (min-height: 521px) {
  .game {
    grid-template-columns: clamp(260px, 24vw, 380px) 1fr;
    grid-template-rows: auto 1fr;
    grid-template-areas: "sidebar turn" "sidebar pad";
    gap: 1rem;
  }
  .sidebar { grid-area: sidebar; grid-template-rows: auto 1fr auto; }
  .players-toggle { display: none; }
  .mode-label { text-align: right; }

  /* always the full list on desktop, regardless of the mobile toggle */
  .game .scoreboard {
    display: flex;
    flex-direction: column;
    max-height: none;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
  }
  .game .card { display: block; min-width: 0; flex: none; }
  .game .card-meta { display: flex; }
  .game .card-last { display: flex; }
  .game .sidebar-actions { display: flex; }
  .game .card-score { font-size: 3rem; }

  .turn { grid-area: turn; }
  .pad {
    grid-area: pad;
    grid-template-columns: 1fr clamp(180px, 16vw, 260px);
    grid-template-rows: none;
  }
  .pad-numbers { grid-template-columns: repeat(6, 1fr); }
  .num.miss { grid-column: span 2; }
  .pad-side { grid-template-columns: none; grid-template-rows: 1fr 1fr 1fr; }
}

/* ================================================================ small screens */
/* Tighter sizing for phones (upright or sideways). */

@media (max-width: 760px), (max-height: 520px) {
  .game { gap: 0.5rem; padding:
    max(0.5rem, env(safe-area-inset-top)) max(0.5rem, env(safe-area-inset-right))
    max(0.5rem, env(safe-area-inset-bottom)) max(0.5rem, env(safe-area-inset-left)); }
  .topbar button, .sidebar-actions button { padding: 0.35rem 0.7rem; font-size: 0.9rem; }
  .mode-label { font-size: 0.8rem; }
  .sidebar { gap: 0.4rem; }
  kbd { display: none; }

  .scoreboard { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0.4rem; }
  .card { padding: 0.4rem 0.7rem; border-radius: 10px; }
  .card.active { box-shadow: none; }
  .card-score { font-size: 2rem; }
  .card-meta, .card-last { font-size: 0.75rem; gap: 0.5rem; }
  .card-last { margin-top: 0.2rem; gap: 0.25rem; }
  .players-collapsed .card { min-width: 7.5rem; }
  .players-collapsed .card-score { font-size: 1.4rem; }

  .turn {
    grid-template-columns: 1fr auto;
    grid-template-areas: "player total" "darts darts" "checkout checkout";
    gap: 0.35rem 0.75rem;
    padding: 0.5rem 0.75rem;
    border-radius: 10px;
  }
  .turn-player { font-size: 1.1rem; }
  .turn-total { font-size: 1.3rem; }
  .turn-darts { gap: 0.4rem; }
  .slot { flex: 1; width: auto; height: 2.3rem; font-size: 1.1rem; border-radius: 8px; }
  .checkout { font-size: 0.85rem; min-height: 1.1em; }

  .pad, .pad-numbers, .pad-side { gap: 0.4rem; }
  .pad button { font-size: 1.4rem; border-radius: 10px; padding: 0; }
  .pad-side { min-height: 3.2rem; }
  .mod, .undo { font-size: 1.1rem !important; }

  .setup { padding: 1.5rem 1rem; }
  .setup h1 { font-size: 1.7rem; margin-bottom: 1.25rem; }
  .setup section { margin-bottom: 1.25rem; }
  .segmented button { font-size: 1.1rem; padding: 0.6rem 1.1rem; }
  .setup-actions { flex-wrap: wrap; }
  .setup-actions button { flex: 1; }
  .dialog { padding: 1.5rem; }

  .stats-overlay { padding: 0; }
  .stats-panel { border-radius: 0; border: none; width: 100%; align-self: stretch; }
  .stats-header { padding:
    max(0.6rem, env(safe-area-inset-top)) max(0.75rem, env(safe-area-inset-right))
    0.6rem max(0.75rem, env(safe-area-inset-left)); gap: 0.5rem 0.75rem; }
  .stats-header h2 { font-size: 1.15rem; flex: 1; }
  .stats-header .tabs { order: 3; flex-basis: 100%; display: flex; }
  .stats-header .tabs button { flex: 1; font-size: 0.85rem; padding: 0.4rem 0.5rem; }
  .stats-body { padding: 0.75rem max(0.75rem, env(safe-area-inset-left)) max(0.75rem, env(safe-area-inset-bottom)); }
  .stats-table { font-size: 0.85rem; }
  .stats-table th, .stats-table td { padding: 0.35rem 0.5rem; }
  .history-toggle { padding: 0.6rem 0.75rem; }
  .history-item .game-detail { padding: 0 0.5rem 0.75rem; }
}
