:root {
  --bg-0: #050b18;
  --bg-1: #0a1830;
  --bg-2: #0f2448;

  --board-ink: #0a2540;
  --board-ink-2: #081c36;
  --board-edge: #05101f;
  --board-frame: #1b120a;
  --board-frame-hi: #3a2a18;

  --peg: #d6dae3;
  --peg-shadow: #2a2f3c;
  --string: #f1f3f8;

  --ink: #f7f9fc;
  --ink-dim: #9ba7bd;
  --ink-dimmer: #5d6a82;

  --accent: #00a0df;     /* Portbase cyan */
  --accent-hot: #f5b90b; /* gouden hanger */
  --danger: #e63946;
  --ok: #2ca04a;

  --kc-red:    #e63946;
  --kc-blue:   #1f6feb;
  --kc-white:  #f4f5f7;
  --kc-black:  #1a1d24;
  --kc-yellow: #f6c945;
  --kc-green:  #2ca04a;

  --radius: 14px;
  --radius-sm: 10px;

  --shadow-card:
    0 1px 0 rgba(255, 255, 255, 0.04) inset,
    0 30px 60px rgba(0, 0, 0, 0.5),
    0 2px 8px rgba(0, 0, 0, 0.4);

  --font-display: "Bebas Neue", Impact, sans-serif;
  --font: "Inter", system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1400px 900px at 50% -10%, #13305d 0%, #0a1830 45%, #050b18 100%),
    var(--bg-0);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ---------- Spinner ---------- */

.spinner {
  display: inline-block;
  width: 22px;
  height: 22px;
  border: 3px solid rgba(255, 255, 255, 0.15);
  border-top-color: var(--accent-hot);
  border-right-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
.spinner.lg { width: 56px; height: 56px; border-width: 5px; }
@keyframes spin { to { transform: rotate(360deg); } }

.loading-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  background: rgba(5, 11, 24, 0.55);
  backdrop-filter: blur(2px);
  border-radius: inherit;
  z-index: 5;
  color: var(--ink);
}
.loading-overlay .label {
  font-family: var(--font-display);
  letter-spacing: 2px;
  font-size: 18px;
  color: var(--accent-hot);
}
.loading-overlay .sub {
  color: var(--ink-dim);
  font-size: 12px;
  text-align: center;
  max-width: 280px;
  line-height: 1.4;
}

.panel-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 40px 20px;
  color: var(--ink-dim);
  font-size: 13px;
}

.btn-spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(0, 0, 0, 0.2);
  border-top-color: rgba(0, 0, 0, 0.8);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: -2px;
  margin-right: 6px;
}

button[disabled] { opacity: 0.6; cursor: not-allowed; }

button { font-family: inherit; }

/* ---------- Auth gate ---------- */

.auth-gate {
  position: fixed;
  inset: 0;
  background: rgba(3, 8, 18, 0.86);
  display: grid;
  place-items: center;
  z-index: 1000;
  backdrop-filter: blur(6px);
}

.auth-card {
  background: linear-gradient(180deg, #0e2a4a 0%, #081f3a 100%);
  border: 1px solid rgba(0, 160, 223, 0.25);
  padding: 36px 32px 28px;
  border-radius: var(--radius);
  width: min(380px, 92vw);
  box-shadow: var(--shadow-card);
  text-align: center;
}

.auth-trophy { font-size: 40px; margin-bottom: 8px; }

.auth-card h1 {
  margin: 0 0 4px;
  font-family: var(--font-display);
  font-size: 34px;
  letter-spacing: 2px;
  color: var(--accent-hot);
  text-shadow: 0 2px 12px rgba(245, 185, 11, 0.25);
}

.auth-card p { margin: 0 0 20px; color: var(--ink-dim); font-size: 13px; }

.auth-card input {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid #2a4c77;
  background: #050f1f;
  color: var(--ink);
  font-size: 15px;
  margin-bottom: 14px;
  outline: none;
}
.auth-card input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(0, 160, 223, 0.2); }

.auth-card button {
  width: 100%;
  padding: 12px;
  border-radius: var(--radius-sm);
  border: none;
  background: linear-gradient(180deg, #22b6f0, var(--accent));
  color: #02121f;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.3px;
  cursor: pointer;
  text-transform: uppercase;
}
.auth-card button:hover { filter: brightness(1.08); }

.auth-error { color: var(--danger); font-size: 13px; margin-top: 12px; }

/* ---------- Topbar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 28px;
  background: rgba(5, 11, 24, 0.6);
  backdrop-filter: blur(12px) saturate(1.1);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.topbar-title { display: flex; align-items: center; gap: 14px; }
.topbar-title .trophy { font-size: 28px; filter: drop-shadow(0 2px 6px rgba(245, 185, 11, 0.35)); }
.topbar-title .titles { display: flex; flex-direction: column; line-height: 1.05; }
.topbar-title strong {
  font-family: var(--font-display);
  font-size: 26px;
  letter-spacing: 2px;
  color: var(--accent-hot);
}
.topbar-title .subtitle { color: var(--ink-dim); font-size: 11px; text-transform: uppercase; letter-spacing: 1.5px; }

.topbar-actions { display: flex; gap: 8px; align-items: center; }
.topbar-actions button {
  padding: 10px 14px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.04);
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 120ms, border-color 120ms, transform 80ms;
}
.topbar-actions button:hover { background: rgba(255, 255, 255, 0.08); }
.topbar-actions button:active { transform: translateY(1px); }

.topbar-actions .primary {
  background: linear-gradient(180deg, #22b6f0, var(--accent));
  color: #02121f;
  border-color: transparent;
}
.topbar-actions .primary:hover { filter: brightness(1.08); background: linear-gradient(180deg, #22b6f0, var(--accent)); }

.topbar-actions .ghost { padding: 10px 12px; }
#rules-btn {
  width: 38px;
  padding: 0;
  height: 38px;
  border-radius: 50%;
  font-weight: 700;
  display: grid;
  place-items: center;
}

/* ---------- Board ---------- */

.board-wrap {
  display: grid;
  place-items: start center;
  padding: 36px 16px 56px;
}

.board {
  position: relative;
  width: min(1100px, 96vw);
  aspect-ratio: 5 / 4;
  container-type: inline-size;
  border-radius: 18px;
  padding: 14px;
  background:
    /* subtle noise */
    radial-gradient(circle at 20% 15%, rgba(255, 255, 255, 0.04) 0 1px, transparent 2px) 0 0 / 40px 40px,
    radial-gradient(circle at 80% 75%, rgba(255, 255, 255, 0.03) 0 1px, transparent 2px) 0 0 / 50px 50px,
    linear-gradient(180deg, #102d52 0%, var(--board-ink) 55%, var(--board-ink-2) 100%);
  border: 1px solid rgba(0, 160, 223, 0.15);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.08),
    inset 0 0 120px rgba(0, 0, 0, 0.5),
    0 40px 90px rgba(0, 0, 0, 0.65),
    0 10px 20px rgba(0, 0, 0, 0.35);
}

/* gold corner accents to sell the "golden hanger" theme */
.board::before,
.board::after {
  content: "";
  position: absolute;
  width: 28px;
  height: 28px;
  border: 2px solid var(--accent-hot);
  opacity: 0.65;
}
.board::before { top: 10px; left: 10px; border-right: 0; border-bottom: 0; border-top-left-radius: 8px; }
.board::after  { bottom: 10px; right: 10px; border-left: 0; border-top: 0; border-bottom-right-radius: 8px; }

/* Slot anchors the peg position; the keychain (full SVG with peg+string+ring+tag)
   hangs from it and swings slightly from the peg. */
.slot {
  position: absolute;
  /* x anchor = horizontal center, y anchor = peg position at the very top */
  transform: translate(-50%, 0);
  pointer-events: none;
}
.slot .keychain,
.slot .empty-slot { pointer-events: auto; }

/* ---------- Keychain (single SVG: peg + string + ring + tag) ---------- */

.keychain {
  --tilt: 0deg;
  /* keychain width scales with the board width via container queries:
     7.6cqw = 7.6% of the board width, clamped so it never gets silly. */
  width: clamp(56px, 7.6cqw, 96px);
  /* matches the 140x168 viewBox aspect ratio */
  height: calc(clamp(56px, 7.6cqw, 96px) * 168 / 140);
  cursor: grab;
  user-select: none;
  /* rotate around the peg at the top-center, not the middle */
  transform-origin: 50% 6%;
  transform: rotate(var(--tilt));
  transition: transform 180ms ease, filter 180ms ease;
  filter: drop-shadow(0 5px 8px rgba(0, 0, 0, 0.55));
}
.keychain:hover { transform: rotate(calc(var(--tilt) + 1deg)); filter: drop-shadow(0 10px 12px rgba(0, 0, 0, 0.6)); }

/* Empty slot — click to join */
.empty-slot {
  width: clamp(56px, 7.6cqw, 96px);
  height: calc(clamp(56px, 7.6cqw, 96px) * 168 / 140);
  background: transparent;
  border: none;
  padding: 0;
  cursor: pointer;
  opacity: 0.55;
  transition: opacity 160ms ease, transform 160ms ease;
}
.empty-slot svg { display: block; width: 100%; height: 100%; overflow: visible; }
.empty-slot:hover { opacity: 1; transform: translateY(-2px); }
.empty-slot:hover svg rect,
.empty-slot:hover svg circle:not([fill="#3a4255"]) { stroke: var(--accent-hot); }
.empty-slot:hover svg text { fill: var(--accent-hot); }

@media (max-width: 640px) {
  .empty-slot { width: 68px; height: calc(68px * 168 / 140); }
}
.keychain:active { cursor: grabbing; }
.keychain.drag-over { filter: drop-shadow(0 0 0 3px var(--accent-hot)); }
.keychain.drag-over svg .tag-body { stroke: var(--accent-hot); stroke-width: 2; }
.keychain.flipped svg .tag-body { filter: saturate(0.25) brightness(0.55); }
.keychain.flipped svg .tag-label { opacity: 0; }
.keychain.flipped svg .tag-back { opacity: 1; }
.keychain.flipped svg .rank-badge { opacity: 0; }

.keychain svg { display: block; width: 100%; height: 100%; overflow: visible; }
.keychain svg .tag-back { opacity: 0; transition: opacity 160ms; }

/* ---------- Side panel (history) ---------- */

.side-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: min(380px, 92vw);
  height: 100vh;
  background: linear-gradient(180deg, #0e2a4a, #061a30);
  border-left: 1px solid rgba(0, 160, 223, 0.2);
  box-shadow: -20px 0 60px rgba(0, 0, 0, 0.6);
  z-index: 60;
  display: flex;
  flex-direction: column;
  animation: slideIn 200ms ease;
}
.side-panel.left {
  right: auto;
  left: 0;
  border-left: none;
  border-right: 1px solid rgba(0, 160, 223, 0.2);
  box-shadow: 20px 0 60px rgba(0, 0, 0, 0.6);
  animation: slideInLeft 200ms ease;
}
@keyframes slideIn { from { transform: translateX(100%); } to { transform: translateX(0); } }
@keyframes slideInLeft { from { transform: translateX(-100%); } to { transform: translateX(0); } }

/* Player details body */
.player-body { padding: 20px 24px; overflow-y: auto; }
.player-hero {
  display: flex;
  align-items: center;
  gap: 16px;
  padding-bottom: 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  margin-bottom: 18px;
}
.player-hero .mini-chain {
  width: 72px;
  height: calc(72px * 168 / 140);
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.5));
  flex-shrink: 0;
}
.player-hero .who { min-width: 0; }
.player-hero .who-name {
  font-family: var(--font-display);
  letter-spacing: 1.5px;
  font-size: 26px;
  color: var(--accent-hot);
  margin: 0;
  line-height: 1;
}
.player-hero .who-rank {
  color: var(--ink-dim);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  margin-top: 4px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}
.stat-card {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: 10px;
  padding: 12px 10px;
  text-align: center;
}
.stat-card .stat-value {
  font-family: var(--font-display);
  font-size: 28px;
  letter-spacing: 1px;
  color: var(--ink);
  line-height: 1;
}
.stat-card.win .stat-value { color: var(--ok); }
.stat-card.loss .stat-value { color: var(--danger); }
.stat-card.rate .stat-value { color: var(--accent); }
.stat-card .stat-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--ink-dimmer);
  margin-top: 4px;
}

.player-recent h3 {
  margin: 0 0 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--accent);
}
.player-recent ul { list-style: none; margin: 0; padding: 0; }
.player-recent li {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  border-radius: 8px;
  margin-bottom: 6px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.05);
  font-size: 13px;
}
.player-recent .result-badge {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 11px;
  color: #02121f;
}
.player-recent .result-badge.w { background: var(--ok); }
.player-recent .result-badge.l { background: var(--danger); }
.player-recent .vs { color: var(--ink-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player-recent .vs strong { color: var(--ink); }
.player-recent .score {
  font-family: var(--font-display);
  font-size: 16px;
  letter-spacing: 1.5px;
  padding: 2px 8px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.06);
}
.player-empty { color: var(--ink-dimmer); font-size: 13px; text-align: center; padding: 20px 0; }

.side-panel header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.side-panel h2 {
  margin: 0;
  font-family: var(--font-display);
  letter-spacing: 1.5px;
  color: var(--accent-hot);
  font-size: 22px;
}

.icon-btn {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--ink);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.06); }

.history-list {
  list-style: none;
  margin: 0;
  padding: 8px 12px;
  overflow-y: auto;
}
.history-list li {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-radius: 10px;
  margin-bottom: 6px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.05);
}
.history-list .name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-list .name.left  { text-align: right; }
.history-list .name.right { text-align: left; }
.history-list .name.winner { color: var(--accent-hot); }
.history-list .score {
  font-family: var(--font-display);
  font-size: 20px;
  letter-spacing: 2px;
  color: var(--ink);
  padding: 4px 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.06);
}
.history-list .date {
  grid-column: 1 / -1;
  text-align: center;
  font-size: 11px;
  color: var(--ink-dimmer);
  margin-top: 2px;
}
.history-empty {
  text-align: center;
  color: var(--ink-dimmer);
  padding: 40px 20px;
  font-size: 13px;
}

/* ---------- Card dialogs ---------- */

.card-dialog {
  border: none;
  border-radius: var(--radius);
  padding: 0;
  background: transparent;
  color: var(--ink);
}
.card-dialog::backdrop {
  background: rgba(3, 8, 18, 0.75);
  backdrop-filter: blur(4px);
}
.card-dialog form,
.card-dialog .rules-body {
  background: linear-gradient(180deg, #0e2a4a, #081f3a);
  padding: 28px 28px 22px;
  border-radius: var(--radius);
  width: min(420px, 92vw);
  border: 1px solid rgba(0, 160, 223, 0.25);
  box-shadow: var(--shadow-card);
}
.card-dialog h2 {
  margin: 0 0 18px;
  font-family: var(--font-display);
  font-size: 26px;
  letter-spacing: 1.5px;
  color: var(--accent-hot);
}
.card-dialog h3 {
  margin: 14px 0 6px;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--accent);
}
.card-dialog ul {
  margin: 0 0 4px;
  padding-left: 20px;
  font-size: 13px;
  color: var(--ink-dim);
  line-height: 1.55;
}
.card-dialog label {
  display: block;
  margin-bottom: 12px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--ink-dim);
}
.card-dialog select,
.card-dialog input {
  width: 100%;
  padding: 11px 12px;
  margin-top: 6px;
  border-radius: var(--radius-sm);
  border: 1px solid #2a4c77;
  background: #050f1f;
  color: var(--ink);
  font-size: 14px;
  font-family: inherit;
  outline: none;
}
.card-dialog select:focus,
.card-dialog input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(0, 160, 223, 0.2);
}

.score-row { display: flex; align-items: end; gap: 10px; }
.score-row label { flex: 1; margin-bottom: 0; }
.score-sep { font-size: 24px; padding-bottom: 10px; color: var(--ink-dim); }

.card-dialog menu {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin: 20px 0 0;
  padding: 0;
}
.card-dialog menu button {
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  font-weight: 700;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
#result-cancel { background: rgba(255, 255, 255, 0.06); color: var(--ink); border: 1px solid rgba(255, 255, 255, 0.1); }
#result-submit, .card-dialog menu button.primary {
  background: linear-gradient(180deg, #22b6f0, var(--accent));
  color: #02121f;
}
.result-error { color: var(--danger); font-size: 13px; margin: 6px 0 0; }

/* ---------- Color picker (join form) ---------- */

.color-picker {
  display: flex;
  gap: 10px;
  margin-top: 8px;
}
.color-picker .swatch {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.1);
  cursor: pointer;
  padding: 0;
  transition: transform 120ms, border-color 120ms, box-shadow 120ms;
}
.color-picker .swatch:hover { transform: translateY(-2px); }
.color-picker .swatch.selected {
  border-color: var(--accent-hot);
  box-shadow: 0 0 0 3px rgba(245, 185, 11, 0.3);
}
.color-picker .swatch.red    { background: radial-gradient(circle at 30% 25%, #ff6775, var(--kc-red)); }
.color-picker .swatch.blue   { background: radial-gradient(circle at 30% 25%, #5b9bff, var(--kc-blue)); }
.color-picker .swatch.white  { background: radial-gradient(circle at 30% 25%, #ffffff, var(--kc-white)); }
.color-picker .swatch.black  { background: radial-gradient(circle at 30% 25%, #3b4150, var(--kc-black)); }
.color-picker .swatch.yellow { background: radial-gradient(circle at 30% 25%, #ffe085, var(--kc-yellow)); }
.color-picker .swatch.green  { background: radial-gradient(circle at 30% 25%, #5cd476, var(--kc-green)); }

#join-cancel { background: rgba(255, 255, 255, 0.06); color: var(--ink); border: 1px solid rgba(255, 255, 255, 0.1); }
#join-submit {
  background: linear-gradient(180deg, #22b6f0, var(--accent));
  color: #02121f;
}

/* ---------- Responsive ---------- */

/* ---------- Tablet / phone: pannable pyramid ----------

   Strategy: keep the desktop pyramid shape and DOM exactly as-is. Render the
   board at a fixed comfortable width (720px) and let the wrapper scroll
   horizontally on narrow viewports. Phones get a "look at the real
   scoreboard through a window" feel — names stay readable, ranks stay in
   place, you pan left/right to see the wide bottom rows. */

@media (max-width: 760px) {
  .topbar { padding: 10px 12px; gap: 8px; }
  /* Drop the wordmark on phones — the trophy is enough branding. */
  .topbar-title .titles { display: none; }
  .topbar-title { gap: 0; }
  .topbar-title .trophy { font-size: 26px; }

  .topbar-actions { gap: 6px; }
  .topbar-actions button { padding: 8px 10px; font-size: 12px; }
  #rules-btn { width: 34px; height: 34px; }

  /* Shorten the primary button so all three actions fit. */
  #add-result-btn::before { content: "+ Uitslag"; }
  #add-result-btn { font-size: 0; padding: 8px 12px; }
  #add-result-btn::before { font-size: 12px; }

  .board-wrap {
    display: block;
    padding: 16px 0 32px;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
    /* Hint to mobile users that the board is pannable. */
    scrollbar-width: thin;
  }

  .board {
    width: 720px;
    min-width: 720px;
    margin: 0 auto;
    aspect-ratio: 5 / 4;
    /* The container query unit is now relative to the fixed 720px board,
       so keychain sizing is identical to a desktop board at the same width. */
  }

  .board > .slot { scroll-snap-align: none; }

  .keychain { cursor: pointer; }
}