:root {
  color-scheme: dark;
  --bg: #000000;
  --panel: #141414;
  --panel-2: #222222;
  --border: #3a3a3a;
  --text: #f2f2f2;
  --muted: #9a9a9a;
  --pos: #8fd17a;
  --neg: #f08a7a;
  --gold: #d9a441;
  --danger: #c0392b;
  --radius: 10px;

  --red: #d64532;
  --green: #4f9a3c;
  --blue: #2f7fc1;
  --white: #e8e6df;
  --yellow: #e5b72a;
  --purple: #8a52b8;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #ffffff;
  --panel: #f1f1f1;
  --panel-2: #ffffff;
  --border: #cfcfcf;
  --text: #111111;
  --muted: #666666;
  --pos: #2e7d32;
  --neg: #c62828;
  --white: #b9b5aa;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  margin: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

button { font: inherit; color: inherit; cursor: pointer; }

.footer h1 {
  margin: 0;
  font-size: 1.15rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #ffffff;
  font-family: Georgia, "Times New Roman", serif;
}
:root[data-theme="light"] .footer h1 { color: var(--text); }
.footer-actions { display: flex; align-items: center; gap: 6px; }
.icon-btn {
  background: none;
  border: 0;
  padding: 8px;
  border-radius: 50%;
  color: var(--muted);
  display: flex;
}
.icon-btn:active { background: var(--panel); }

/* ---------- Player selector ---------- */
.players {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin: 10px 12px 8px;
  padding: 4px;
  background: var(--panel);
  border-radius: calc(var(--radius) + 4px);
  position: sticky;
  top: 0;
  z-index: 5;
}
.player-tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 6px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
}
.player-tab .pname { font-size: 0.85rem; font-weight: 600; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-tab .ptotal { font-size: 1.5rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.player-tab[aria-selected="true"] {
  background: var(--gold);
  color: #241a0e;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
}

/* ---------- Board (one vertical column per color) ---------- */
.board {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(var(--cols, 5), minmax(0, 1fr));
  gap: 5px;
  padding: 0 8px 8px;
  align-items: stretch;
  grid-auto-rows: 1fr;
}

.row {
  --c: var(--muted);
  --on-text: #fff;
  background: var(--panel);
  border-radius: var(--radius);
  border-top: 5px solid var(--c);
  padding: 5px 4px 6px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.color-red    { --c: var(--red); }
.color-green  { --c: var(--green); }
.color-blue   { --c: var(--blue); }
.color-white  { --c: var(--white); --on-text: #222; }
.color-yellow { --c: var(--yellow); --on-text: #2a1f00; }
.color-purple { --c: var(--purple); }

.row-head {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding-bottom: 5px;
  min-width: 0;
}
.cname {
  font-weight: 700;
  color: var(--c);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cscore { font-weight: 800; font-size: 1.15rem; font-variant-numeric: tabular-nums; line-height: 1.3; }
.cscore.pos { color: var(--pos); }
.cscore.neg { color: var(--neg); }
.calc {
  color: var(--muted);
  font-size: 0.58rem;
  line-height: 1.25;
  min-height: 2.5em;
  font-variant-numeric: tabular-nums;
}

.cells {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
/* Cards grow to fill the available height; wagers get a smaller share. */
.card.num { flex: 1 1 0; }

.card {
  border: 1.5px solid color-mix(in srgb, var(--c) 45%, transparent);
  background: var(--panel-2);
  border-radius: 6px;
  min-height: 34px;
  width: 100%;
  padding: 0;
  font-weight: 700;
  font-size: clamp(1rem, 2.4vh, 1.8rem);
  font-variant-numeric: tabular-nums;
  transition: transform 0.08s, background 0.12s;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
.card:active:not(:disabled) { transform: scale(0.92); }
.card.on {
  background: var(--c);
  border-color: var(--c);
  color: var(--on-text);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
}
.card.taken {
  opacity: 0.22;
  cursor: not-allowed;
  border-style: dashed;
  text-decoration: line-through;
}

.wagers {
  flex: 2.1 1 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding-bottom: 4px;
  margin-bottom: 2px;
  border-bottom: 1px dashed var(--border);
}
.card.wager {
  flex: 1 1 0;
  min-height: 24px;
  font-size: clamp(0.9rem, 2vh, 1.4rem);
  line-height: 1;
  filter: grayscale(1) opacity(0.55);
  text-decoration: none;
}
.card.wager.on { filter: none; }
.card.wager.taken { filter: grayscale(1) opacity(0.4); }

/* ---------- Footer ---------- */
.footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 14px 14px;
}

.btn {
  border: 1px solid var(--border);
  background: var(--panel-2);
  border-radius: var(--radius);
  padding: 10px 18px;
  font-weight: 600;
}
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-primary { background: var(--gold); border-color: var(--gold); color: #241a0e; }

/* ---------- Modals ---------- */
.modal {
  border: 0;
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  padding: 20px;
  width: min(92vw, 380px);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
}
.modal::backdrop { background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(2px); }
.modal h2 { margin: 0 0 8px; font-size: 1.2rem; }
.modal p { margin: 0 0 16px; color: var(--muted); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.field { display: flex; flex-direction: column; gap: 6px; margin-top: 14px; font-size: 0.85rem; color: var(--muted); }
.field input {
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}
.hint { font-size: 0.8rem; margin-top: 16px !important; }

.switch {
  appearance: none;
  -webkit-appearance: none;
  width: 50px;
  height: 30px;
  border-radius: 15px;
  background: var(--border);
  position: relative;
  cursor: pointer;
  transition: background 0.2s;
  margin: 0;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.2s;
}
.switch:checked { background: var(--purple); }
.switch-theme:checked { background: var(--gold); }
.switch:checked::after { transform: translateX(20px); }
