:root {
  --bg: #0d0d1a;
  --surface: #181830;
  --card: #22224a;
  --card2: #1e1e3c;
  --border: #2e2e58;
  --primary: #7c6fff;
  --primary-dk: #5a48d4;
  --accent: #ff6b6b;
  --success: #4ecdc4;
  --warn: #ffd93d;
  --text: #f0f0f5;
  --muted: #7878aa;
  --active-col: rgba(124,111,255,0.13);
  --active-border: #7c6fff;
  --r: 12px;
  --rs: 8px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { scroll-behavior: smooth; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  padding-bottom: 32px;
}

/* ── HEADER ── */
.hdr {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.hdr h1 { font-size: 1.15rem; font-weight: 700; flex: 1; }
.hdr .sub { font-size: 0.78rem; color: var(--muted); }
.btn-back {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 1.3rem;
  cursor: pointer;
  padding: 4px 8px 4px 0;
  line-height: 1;
}

/* ── BUTTONS ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 22px;
  border-radius: var(--rs);
  border: none;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .15s, transform .1s;
  text-decoration: none;
}
.btn:active { transform: scale(0.96); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dk); }
.btn-danger { background: var(--accent); color: #fff; }
.btn-outline {
  background: transparent;
  border: 1.5px solid var(--border);
  color: var(--text);
}
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }
.btn-success { background: var(--success); color: #0d0d1a; }
.btn-sm { padding: 7px 14px; font-size: 0.82rem; }
.btn-full { width: 100%; }
.btn:disabled { opacity: .4; cursor: not-allowed; }

/* ── HOME ── */
.home-intro {
  padding: 24px 16px 8px;
  font-size: 0.9rem;
  color: var(--muted);
}
.game-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
  padding: 14px 16px;
}
.game-card {
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: var(--r);
  padding: 26px 14px 20px;
  cursor: pointer;
  text-align: center;
  transition: border-color .2s, transform .12s, box-shadow .2s;
  position: relative;
  overflow: hidden;
}
.game-card::before {
  content: '';
  position: absolute;
  inset: 0;
  opacity: 0;
  transition: opacity .2s;
  border-radius: var(--r);
}
.game-card:hover::before { opacity: 1; }
.game-card:active { transform: scale(0.96); }
.game-card:hover { box-shadow: 0 4px 24px rgba(124,111,255,.2); }
.game-card .gc-icon { font-size: 3rem; margin-bottom: 10px; display: block; }
.game-card .gc-name { font-size: 1.05rem; font-weight: 700; }
.game-card .gc-desc { font-size: 0.78rem; color: var(--muted); margin-top: 4px; }
.game-card .gc-badge {
  position: absolute;
  top: 10px;
  right: 10px;
  background: var(--primary);
  color: #fff;
  font-size: 0.7rem;
  font-weight: 700;
  border-radius: 20px;
  padding: 2px 8px;
}

/* ── GAME TYPE VIEW ── */
.gt-section { padding: 0 16px; }
.section-title {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
  margin: 20px 0 10px;
}
.session-card {
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: var(--rs);
  padding: 14px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: border-color .15s, transform .1s;
  display: flex;
  align-items: center;
  gap: 12px;
}
.session-card:hover { border-color: var(--primary); }
.session-card:active { transform: scale(0.98); }
.session-card.active-sess { border-color: var(--success); }
.sc-info { flex: 1; min-width: 0; }
.sc-name { font-weight: 600; font-size: 0.95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sc-players { font-size: 0.78rem; color: var(--muted); margin-top: 3px; }
.sc-date { font-size: 0.72rem; color: var(--muted); }
.sc-arrow { color: var(--muted); font-size: 1rem; }
.live-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 6px var(--success);
  flex-shrink: 0;
  animation: pulse 1.5s infinite;
}
@keyframes pulse {
  0%,100% { opacity: 1; }
  50% { opacity: .4; }
}
.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 20px;
  font-size: 0.85rem;
  background: var(--card);
  border-radius: var(--rs);
  border: 1px dashed var(--border);
}

/* ── NEW GAME WIZARD ── */
.wizard { padding: 20px 16px; }
.wizard .field { margin-bottom: 18px; }
.wizard label {
  display: block;
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 6px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .05em;
}
.wizard input[type="text"],
.wizard input[type="number"] {
  width: 100%;
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: var(--rs);
  padding: 12px 14px;
  color: var(--text);
  font-size: 1rem;
  outline: none;
  transition: border-color .15s;
}
.wizard input:focus { border-color: var(--primary); }
.players-list { display: flex; flex-direction: column; gap: 8px; }
.player-row { display: flex; gap: 8px; align-items: center; }
.player-row input { flex: 1; }
.btn-remove {
  background: none;
  border: 1.5px solid var(--border);
  color: var(--accent);
  border-radius: var(--rs);
  width: 40px; height: 42px;
  font-size: 1.1rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: background .15s;
}
.btn-remove:hover { background: rgba(255,107,107,.1); }
.radio-group { display: flex; flex-direction: column; gap: 8px; }
.radio-opt {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: var(--rs);
  padding: 12px 14px;
  cursor: pointer;
  transition: border-color .15s;
}
.radio-opt.selected { border-color: var(--primary); background: rgba(124,111,255,.08); }
.radio-opt input[type="radio"] { accent-color: var(--primary); width: 16px; height: 16px; }
.radio-opt .ro-label { font-weight: 600; font-size: 0.9rem; }
.radio-opt .ro-sub { font-size: 0.75rem; color: var(--muted); margin-top: 1px; }

/* ── SESSION / GAME VIEW ── */
.sess-hdr-bar {
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.current-player-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(124,111,255,.18);
  border: 1px solid var(--primary);
  border-radius: 20px;
  padding: 4px 12px;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--primary);
}
.cp-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--primary);
  animation: pulse 1s infinite;
}
.sess-actions {
  padding: 12px 16px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* ── YAHTZEE TABLE ── */
.table-wrap {
  overflow-x: auto;
  padding: 0 16px 16px;
  -webkit-overflow-scrolling: touch;
}
.score-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.85rem;
  min-width: max-content;
}
.score-table th, .score-table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.score-table th {
  background: var(--surface);
  font-weight: 700;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  position: sticky;
  top: 0;
  z-index: 10;
}
.score-table th:first-child,
.score-table td:first-child {
  position: sticky;
  left: 0;
  background: var(--surface);
  z-index: 5;
  min-width: 130px;
  font-weight: 600;
}
.score-table td:first-child { background: var(--card2); }
.score-table th.active-th {
  background: var(--active-col);
  border-bottom: 2px solid var(--active-border);
  color: var(--primary);
}
.score-table td.active-col { background: rgba(124,111,255,.06); }
.score-table tr.calc-row td { background: rgba(255,255,255,.03); color: var(--muted); font-style: italic; }
.score-table tr.calc-row td:first-child { font-style: normal; color: var(--text); }
.score-table tr.total-row td {
  background: rgba(124,111,255,.12);
  font-weight: 700;
  font-size: 1rem;
  color: var(--text);
  border-top: 2px solid var(--primary);
}
.score-table tr.total-row td:first-child { color: var(--primary); }
.score-table tr.sep-row td {
  background: var(--surface);
  height: 6px;
  padding: 0;
  border: none;
}
.cell-score {
  min-width: 60px;
  text-align: center;
  cursor: pointer;
  border-radius: 6px;
  transition: background .15s;
  font-weight: 600;
  /* reset <button> defaults */
  background: none;
  border: none;
  font: inherit;
  color: inherit;
  padding: 4px 6px;
  display: inline-block;
  width: 100%;
}
.cell-score:hover { background: rgba(124,111,255,.18); }
.cell-score.empty { color: var(--muted); font-size: 0.7rem; font-weight: 400; }
.cell-score.filled { color: var(--text); }
.cell-score.zero { color: rgba(255,107,107,.6); }
.score-table th:not(:first-child) { text-align: center; min-width: 70px; }
.player-th-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.player-th-turn {
  font-size: 0.65rem;
  background: var(--primary);
  color: #fff;
  border-radius: 4px;
  padding: 1px 5px;
}
.bonus-progress {
  font-size: 0.7rem;
  color: var(--warn);
}
.bonus-done { color: var(--success); }

/* ── GENERIC ROUNDS TABLE ── */
.rounds-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 0.87rem;
  min-width: max-content;
}
.rounds-table th, .rounds-table td {
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  text-align: center;
}
.rounds-table th:first-child, .rounds-table td:first-child {
  text-align: left;
  position: sticky;
  left: 0;
  background: var(--surface);
  z-index: 5;
  min-width: 80px;
  font-weight: 600;
  color: var(--muted);
}
.rounds-table th {
  background: var(--surface);
  font-weight: 700;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  position: sticky;
  top: 0;
  z-index: 10;
}
.rounds-table tr.total-row td {
  background: rgba(124,111,255,.12);
  font-weight: 700;
  font-size: 1rem;
  border-top: 2px solid var(--primary);
}
.rounds-table tr.total-row td:first-child { color: var(--primary); }
.cell-round {
  min-width: 70px;
  cursor: pointer;
  border-radius: 6px;
  padding: 6px 10px;
  transition: background .15s;
  font-weight: 600;
  /* reset <button> defaults */
  background: none;
  border: none;
  font: inherit;
  color: inherit;
  display: inline-block;
  width: 100%;
}
.cell-round:hover { background: rgba(124,111,255,.18); }
.cell-round.empty { color: var(--muted); font-size: 0.72rem; font-weight: 400; }

/* ── FINISHED GAME BANNER ── */
.finished-banner {
  background: rgba(78,205,196,.12);
  border: 1.5px solid var(--success);
  border-radius: var(--r);
  padding: 16px;
  margin: 16px 16px 0;
  text-align: center;
}
.finished-banner .fb-title { font-size: 1.1rem; font-weight: 700; color: var(--success); margin-bottom: 4px; }
.finished-banner .fb-winner { font-size: 0.9rem; color: var(--text); }

/* ── STANDINGS ── */
.standings { padding: 0 16px; }
.standing-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.standing-rank {
  width: 28px; height: 28px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.8rem;
  font-weight: 700;
  flex-shrink: 0;
}
.rank-1 { background: #ffd700; color: #000; }
.rank-2 { background: #c0c0c0; color: #000; }
.rank-3 { background: #cd7f32; color: #fff; }
.rank-n { background: var(--card); color: var(--muted); }
.standing-name { flex: 1; font-weight: 600; }
.standing-score { font-weight: 700; font-size: 1.05rem; color: var(--primary); }

/* ── MODAL ── */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.7);
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  backdrop-filter: blur(3px);
}
#modal-overlay.hidden { display: none; }
#modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r) var(--r) 0 0;
  padding: 24px 20px 32px;
  width: 100%;
  max-width: 640px;
  animation: slideUp .08s ease;
}
@keyframes slideUp {
  from { transform: translateY(40px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
.modal-title { font-size: 1rem; font-weight: 700; margin-bottom: 4px; }
.modal-sub { font-size: 0.82rem; color: var(--muted); margin-bottom: 18px; }
.modal-input {
  width: 100%;
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: var(--rs);
  padding: 14px;
  color: var(--text);
  font-size: 1.5rem;
  text-align: center;
  outline: none;
  margin-bottom: 16px;
}
.modal-input:focus { border-color: var(--primary); }
.modal-actions { display: flex; gap: 10px; }
.modal-actions .btn { flex: 1; }

/* ── SEN GAME ── */
.sen-warning {
  background: rgba(255,107,107,.12);
  border-left: 4px solid var(--accent);
  padding: 10px 16px;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--accent);
}
.sen-over-th {
  color: var(--accent) !important;
}
.rounds-table th.sen-over-th { background: rgba(255,107,107,.08); }

/* ── LOCKED CELL ── */
.cell-score.locked {
  cursor: default;
  opacity: 0.35;
}
.cell-score.locked:hover { background: none; }

/* ── ADMIN BADGE ── */
.admin-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(255,211,61,.15);
  border: 1px solid var(--warn);
  border-radius: 20px;
  padding: 4px 12px;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--warn);
  animation: pulse 1.5s infinite;
}

/* ── GEAR FAB ── */
.fab-gear {
  position: fixed;
  bottom: 24px;
  right: 20px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--card);
  border: 2px solid var(--border);
  color: var(--muted);
  font-size: 1.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(0,0,0,.5);
  transition: border-color .2s, color .2s, transform .15s;
  z-index: 80;
}
.fab-gear:hover { border-color: var(--warn); color: var(--warn); transform: rotate(30deg); }
.fab-gear--active {
  border-color: var(--warn);
  color: var(--warn);
  background: rgba(255,211,61,.12);
  animation: spin-slow 3s linear infinite;
}
@keyframes spin-slow { to { transform: rotate(360deg); } }

/* ── TOAST ── */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 10px 20px;
  font-size: 0.85rem;
  z-index: 300;
  animation: fadeInOut 2.5s ease forwards;
  white-space: nowrap;
}
@keyframes fadeInOut {
  0% { opacity: 0; transform: translateX(-50%) translateY(10px); }
  15% { opacity: 1; transform: translateX(-50%) translateY(0); }
  75% { opacity: 1; }
  100% { opacity: 0; }
}

/* ── MISC ── */
.spacer { height: 20px; }
.loading {
  text-align: center;
  padding: 40px;
  color: var(--muted);
}
.loader {
  display: inline-block;
  width: 20px; height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .6s linear infinite;
  margin-right: 8px;
  vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

hr.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 4px 0;
}

/* Archive collapse */
.archive-toggle {
  width: 100%;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.8rem;
  padding: 8px 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
}
.archive-toggle:hover { color: var(--text); }

/* Responsive */
@media (max-width: 360px) {
  .game-grid { grid-template-columns: 1fr; }
  .game-card .gc-icon { font-size: 2.2rem; }
}
