:root {
  --bg: #f1f5f9;
  --ink: #1e293b;
  --panel: #ffffff;
  --line: #e2e8f0;
  --accent: #0d9488;
  --accent-dark: #0f766e;
  --accent-light: #f0fdfa;
  --gold: #f59e0b;
  --ok: #16a34a;
  --err: #dc2626;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
}

/* ── Header ─────────────────────────────────────────────────────────── */

header {
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 60%, #334155 100%);
  color: white;
  box-shadow: 0 2px 12px rgba(0,0,0,0.3);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-inner {
  max-width: 1020px;
  margin: 0 auto;
  padding: 0.875rem 1.25rem;
  display: flex;
  align-items: center;
  gap: 1rem;
}

header h1 {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  white-space: nowrap;
  flex-shrink: 0;
}

nav {
  display: flex;
  gap: 0.15rem;
  align-items: center;
  flex-wrap: wrap;
  margin-left: auto;
}

nav a {
  color: rgba(255,255,255,0.8);
  text-decoration: none;
  font-size: 0.875rem;
  padding: 0.4rem 0.65rem;
  border-radius: 6px;
  transition: background 0.15s, color 0.15s;
}

nav a:hover {
  color: white;
  background: rgba(255,255,255,0.12);
}

/* ── Hamburger toggle ────────────────────────────────────────────────── */

.nav-toggle {
  display: none;
  background: transparent;
  border: none;
  padding: 0.45rem 0.4rem;
  cursor: pointer;
  flex-direction: column;
  gap: 5px;
  margin-left: auto;
  flex-shrink: 0;
  border-radius: 6px;
  transform: none;
  box-shadow: none;
}

.nav-toggle:hover { background: rgba(255,255,255,0.1); box-shadow: none; }
.nav-toggle:active { transform: none; }

.nav-toggle span {
  display: block;
  width: 22px;
  height: 2px;
  background: rgba(255,255,255,0.9);
  border-radius: 2px;
  transition: transform 0.25s, opacity 0.2s;
}

.nav-toggle.open span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
.nav-toggle.open span:nth-child(2) { opacity: 0; transform: scaleX(0); }
.nav-toggle.open span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

/* ── Main layout ─────────────────────────────────────────────────────── */

main {
  max-width: 980px;
  margin: 1.5rem auto;
  padding: 0 0.75rem;
}

/* ── Panels ──────────────────────────────────────────────────────────── */

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1.25rem;
  margin-bottom: 1.25rem;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06), 0 4px 12px rgba(0,0,0,0.04);
}

.panel h2, .panel h3 {
  margin-top: 0;
  color: var(--ink);
  border-bottom: 2px solid var(--line);
  padding-bottom: 0.5rem;
  margin-bottom: 1rem;
  font-weight: 600;
}

.panel h2 { font-size: 1.3rem; }
.panel h3 { font-size: 1.05rem; color: #475569; }

/* ── Tables ──────────────────────────────────────────────────────────── */

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 -0.25rem;
  padding: 0 0.25rem;
}

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

th {
  background: var(--accent);
  color: white;
  padding: 0.5rem 0.6rem;
  text-align: left;
  white-space: nowrap;
  font-weight: 600;
  letter-spacing: 0.02em;
  font-size: 0.82rem;
  text-transform: uppercase;
}

td {
  border-bottom: 1px solid var(--line);
  padding: 0.45rem 0.6rem;
  text-align: left;
}

tbody tr:nth-child(even) td { background: #f8fafc; }
tbody tr:hover td { background: var(--accent-light); }

/* ── Forms ───────────────────────────────────────────────────────────── */

.form-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.6rem;
}

.inline-form {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
  margin: 0.25rem 0;
}

label {
  font-size: 0.875rem;
  color: #475569;
  font-weight: 500;
}

input, select {
  padding: 0.5rem 0.65rem;
  border: 1.5px solid var(--line);
  border-radius: 7px;
  background: #fff;
  font-family: inherit;
  font-size: 0.95rem;
  width: 100%;
  color: var(--ink);
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(13,148,136,0.15);
}

/* ── Buttons ─────────────────────────────────────────────────────────── */

button {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 7px;
  background: var(--accent);
  color: white;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 500;
  transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
}

button:hover {
  background: var(--accent-dark);
  box-shadow: 0 2px 8px rgba(13,148,136,0.3);
}

button:active { transform: scale(0.97); }

button.danger { background: #dc2626; }
button.danger:hover { background: #b91c1c; box-shadow: 0 2px 8px rgba(220,38,38,0.25); }

/* ── Nav utility ─────────────────────────────────────────────────────── */

.inline-nav-form { margin: 0; }

.link-button {
  background: transparent;
  border: none;
  color: rgba(255,255,255,0.8);
  padding: 0.4rem 0.65rem;
  font-size: 0.875rem;
  cursor: pointer;
  border-radius: 6px;
  font-family: inherit;
  font-weight: normal;
  text-decoration: none;
  box-shadow: none;
}

.link-button:hover {
  color: white;
  background: rgba(255,255,255,0.12);
  box-shadow: none;
}

.link-button:active { transform: none; }

/* ── Misc ────────────────────────────────────────────────────────────── */

.narrow-form { max-width: 460px; }

.alerts {
  list-style: none;
  padding: 0;
  margin-bottom: 1rem;
}

.alert {
  padding: 0.65rem 0.9rem;
  margin-bottom: 0.5rem;
  border-radius: 8px;
  border-left: 4px solid;
  font-size: 0.9rem;
}

.alert.success {
  background: #f0fdf4;
  color: #15803d;
  border-color: var(--ok);
}

.alert.error {
  background: #fef2f2;
  color: var(--err);
  border-color: var(--err);
}

.winner {
  font-size: 1.1rem;
  color: #065f46;
  font-weight: 600;
}

.small-text { font-size: 0.9rem; }

/* ── Responsive ──────────────────────────────────────────────────────── */

@media (min-width: 700px) {
  .form-grid { grid-template-columns: 1fr 1fr; }
  .form-grid button { grid-column: span 2; }
}

@media (max-width: 699px) {
  .header-inner {
    flex-wrap: wrap;
    padding: 0.75rem 1rem 0;
    gap: 0;
  }

  header h1 {
    flex: 1;
    font-size: 1.05rem;
    padding-bottom: 0.75rem;
  }

  .nav-toggle {
    display: flex;
    align-self: flex-start;
    margin-top: 0.2rem;
    margin-left: auto;
  }

  nav {
    width: 100%;
    max-height: 0;
    overflow: hidden;
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    margin-left: 0;
    transition: max-height 0.3s ease;
  }

  nav.open {
    max-height: 520px;
    padding-bottom: 0.5rem;
    border-top: 1px solid rgba(255,255,255,0.1);
  }

  nav a {
    padding: 0.75rem 0.5rem;
    font-size: 0.95rem;
    border-radius: 0;
    display: block;
    border-bottom: 1px solid rgba(255,255,255,0.05);
  }

  nav .inline-nav-form { width: 100%; }

  nav .link-button {
    padding: 0.75rem 0.5rem;
    font-size: 0.95rem;
    width: 100%;
    text-align: left;
    border-radius: 0;
    display: block;
  }

  .panel { padding: 1rem; }
  th, td { padding: 0.4rem 0.45rem; font-size: 0.82rem; }
  main { margin-top: 1rem; }
}

/* ── Stats page ─────────────────────────────────────────────────────── */

.stat-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.stat-card {
  background: var(--panel);
  border: 1.5px solid var(--line);
  border-radius: 12px;
  padding: 0.9rem 0.75rem;
  text-align: center;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
  transition: transform 0.15s, box-shadow 0.15s;
}

.stat-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0,0,0,0.1);
}

.stat-card--accent {
  border-color: var(--accent);
  background: var(--accent-light);
}

.stat-card--streak {
  border-color: #f59e0b;
  background: #fffbeb;
}

.stat-number {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--accent);
  word-break: break-word;
}

.stat-card--streak .stat-number { color: #d97706; }

.stat-label {
  font-size: 0.78rem;
  color: #64748b;
  margin-top: 0.3rem;
  line-height: 1.3;
}

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

@media (min-width: 700px) {
  .chart-grid { grid-template-columns: 1fr 1fr; }
}

.chart-container {
  position: relative;
  width: 100%;
  height: 260px;
}

.chart-container--tall { height: 320px; }

/* ── Head-to-head table ─────────────────────────────────────────────── */

.h2h-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.h2h-table { min-width: max-content; }

.h2h-table th:first-child {
  text-align: right;
  white-space: nowrap;
  padding-right: 0.6rem;
}

.h2h-table thead th {
  white-space: nowrap;
  text-align: center;
}

.h2h-table td {
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* Leaderboard rank states */
tr.row-provisional td { color: #94a3b8; font-style: italic; }
tr.row-unranked td { color: #cbd5e1; }

.needs-games {
  font-size: 0.8rem;
  color: #d97706;
  font-style: italic;
  white-space: nowrap;
}

.h2h-win  { background: #f0fdf4; font-weight: bold; }
.h2h-loss { background: #fef2f2; }
.h2h-even { background: #fefce8; }
.h2h-self,
.h2h-none { color: #cbd5e1; text-align: center; }

/* ── Live game ───────────────────────────────────────────────────────── */

.play-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 0.75rem;
}

.play-scores { display: flex; gap: 1rem; }

.score-box {
  background: var(--panel);
  border: 1.5px solid var(--line);
  border-radius: 10px;
  padding: 0.5rem 0.9rem;
  text-align: center;
  min-width: 80px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.07);
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0 0.3rem;
}

.score-name {
  display: block;
  font-size: 0.8rem;
  color: #64748b;
  flex: 1;
}

.score-num {
  display: block;
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--accent);
  width: 100%;
  text-align: center;
}

/* ── Cards ───────────────────────────────────────────────────────────── */

.card-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: flex-end;
}

.card {
  position: relative;
  display: inline-flex;
  flex-direction: column;
  justify-content: space-between;
  width: 54px;
  height: 78px;
  border: 1.5px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 2px 6px rgba(0,0,0,0.12);
  font-family: "Georgia", serif;
  font-size: 0.85rem;
  padding: 3px 4px;
  vertical-align: bottom;
  transition: border-color 0.12s, box-shadow 0.12s, background 0.12s;
  user-select: none;
}

.card.red { color: #dc2626; }
.card.black { color: #1a1a1a; }

.card-corner {
  line-height: 1.1;
  font-size: 0.8rem;
  font-weight: bold;
}

.card-tl { align-self: flex-start; }
.card-br { align-self: flex-end; transform: rotate(180deg); }

.card-center {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  font-size: 1.5rem;
  line-height: 1;
}

.card.selected {
  outline: 3px solid var(--accent);
  transform: translateY(-8px);
  box-shadow: 0 6px 14px rgba(13,148,136,0.3);
}

.card.clickable { cursor: pointer; }

.card.clickable:hover {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent), 0 2px 8px rgba(0,0,0,0.12);
  background: var(--accent-light);
}

.card.card-dragging { opacity: 0.3; }

.card-back {
  background: linear-gradient(135deg, #1e293b 25%, #0f172a 100%);
  border-color: #0f172a;
  align-items: center;
  justify-content: center;
}

.card-back-inner {
  font-size: 2rem;
  color: rgba(255,255,255,0.12);
}

/* ── Turn indicator ──────────────────────────────────────────────────── */

#status-panel.your-turn {
  border-color: var(--ok);
  background: #f0fdf4;
  box-shadow: 0 0 0 3px rgba(22,163,74,0.15), 0 2px 8px rgba(0,0,0,0.06);
}

#status-panel.their-turn {
  border-color: var(--line);
  background: var(--panel);
}

.score-box--active {
  border-color: var(--ok) !important;
  box-shadow: 0 0 0 3px rgba(22,163,74,0.2) !important;
}

/* ── Player color indicators ─────────────────────────────────────────── */

.player-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: middle;
  flex-shrink: 0;
}

.player-red .player-dot  { background: #ef4444; }
.player-blue .player-dot { background: #3b82f6; }

.peg-legend {
  font-size: 0.8rem;
  margin-bottom: 0.4rem;
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.peg-legend-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.peg-legend-dot.red  { background: #ef4444; }
.peg-legend-dot.blue { background: #3b82f6; }

/* ── Pegging game area ───────────────────────────────────────────────── */

#peg-game-area {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.peg-section { padding: 0.75rem 1rem; }

.peg-section-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 0.4rem;
}

.peg-pile-section {
  background: var(--accent-light);
  border: 2px dashed var(--accent);
  border-radius: 10px;
  padding: 0.75rem 1rem;
}

.peg-pile-header {
  font-size: 0.9rem;
  margin-bottom: 0.4rem;
  color: #475569;
}

/* ── Cut cards ───────────────────────────────────────────────────────── */

.cut-cards { margin-top: 0.5rem; }

/* ── Counting summary ────────────────────────────────────────────────── */

.counting-row {
  border: 1.5px solid var(--line);
  border-radius: 10px;
  padding: 0.75rem;
  margin-bottom: 0.6rem;
  background: #f8fafc;
}

.counting-row-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.3rem;
}

.counting-pts {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
}

.counting-reasons {
  font-size: 0.82rem;
  color: #64748b;
  font-style: italic;
  margin-top: 0.2rem;
}

/* ── Peg board ───────────────────────────────────────────────────────── */

.peg-board {
  display: grid;
  grid-template-columns: repeat(30, 1fr);
  gap: 3px;
  max-width: 100%;
  overflow-x: auto;
}

.peg-hole {
  position: relative;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--line);
  border: 1px solid #cbd5e1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 8px;
}

.peg {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  position: absolute;
  opacity: 0;
  transition: opacity 0.2s;
}

.peg.active { opacity: 1; }

.p1-peg { background: #ef4444; left: 2px; }
.p2-peg { background: #3b82f6; right: 2px; }
