:root {
  --ink: #f4f1ea;
  --ink-dim: rgba(244, 241, 234, 0.55);
  --accent: #e8c37e;
  --line: rgba(255, 255, 255, 0.12);
  --panel: rgba(0, 0, 0, 0.22);
  --good: #7ad19a;
  --die-face: #f7f4ee;
  --die-pip: #1a1a1a;
  --die-edge: rgba(0, 0, 0, 0.25);
  --radius: 14px;
  --felt-a: #1f6b45;
  --felt-b: #124a2e;
}

body.felt-green { --felt-a: #1f6b45; --felt-b: #0f3d26; }
body.felt-burgundy { --felt-a: #6e1f2f; --felt-b: #3d0f18; }
body.dice-inverted { --die-face: #16171b; --die-pip: #f7f4ee; --die-edge: rgba(0,0,0,0.6); }

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

body {
  min-height: 100vh;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
  background-color: var(--felt-b);
  background-image:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='150' height='150'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.82' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E"),
    radial-gradient(135% 115% at 50% -6%, transparent 46%, rgba(0,0,0,0.5) 100%),
    radial-gradient(120% 90% at 50% 0%, var(--felt-a), var(--felt-b));
  background-size: 150px 150px, cover, cover;
  background-repeat: repeat, no-repeat, no-repeat;
  background-attachment: fixed;
  background-blend-mode: soft-light, normal, normal;
  padding: 20px clamp(12px, 4vw, 40px) 60px;
}

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  max-width: 980px; margin: 0 auto 20px;
}
.brand { display: flex; align-items: baseline; gap: 12px; }
.logo {
  font-weight: 800; letter-spacing: -0.02em; font-size: 30px;
  color: var(--accent);
  text-shadow: 0 2px 0 rgba(0,0,0,0.25);
}
.best {
  font-size: 13px; color: var(--ink-dim); font-variant-numeric: tabular-nums; white-space: nowrap;
  padding: 4px 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
  transition: color .3s ease, border-color .3s ease;
}
.best.flash { color: var(--accent); border-color: var(--accent); }
.toggles { display: flex; gap: 8px; }
.chip {
  background: var(--panel); color: var(--ink); border: 1px solid var(--line);
  padding: 8px 12px; border-radius: 999px; font-size: 13px; cursor: pointer;
  transition: transform .12s ease, background .2s ease;
}
.chip:hover { background: rgba(0,0,0,0.35); transform: translateY(-1px); }

.table {
  display: grid; grid-template-columns: 1fr 340px; gap: 24px;
  max-width: 980px; margin: 0 auto;
  align-items: start;
}

.players { display: flex; gap: 12px; margin-bottom: 40px; }
.player {
  flex: 1; display: flex; align-items: center; justify-content: space-between;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 16px;
  transition: border-color .2s ease, box-shadow .2s ease;
}
.player.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), 0 0 16px rgba(232,195,126,.22), 0 8px 22px rgba(0,0,0,.3); }
.player.active .ptotal { color: var(--accent); }
.pname { font-size: 14px; color: var(--ink-dim); }
.pname-input {
  font: inherit; font-size: 14px; color: var(--ink); background: transparent;
  border: none; border-bottom: 1px dashed transparent; width: 60%;
  padding: 2px 0; transition: border-color .2s ease;
}
.pname-input:hover { border-bottom-color: var(--line); }
.pname-input:focus { outline: none; border-bottom-color: var(--accent); color: var(--accent); }
.ptotal { font-size: 24px; font-weight: 800; font-variant-numeric: tabular-nums; transition: color .3s ease; }

.dice-tray {
  display: flex; gap: 14px; justify-content: center; flex-wrap: wrap;
  min-height: 92px; padding: 14px; margin: 4px 0 18px;
}
.die {
  width: 72px; height: 72px; border-radius: 16px;
  background: var(--die-face);
  display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  padding: 12px; cursor: pointer; position: relative;
  box-shadow: inset 0 -4px 0 var(--die-edge), 0 6px 14px rgba(0,0,0,.35);
  transition: transform .15s ease, box-shadow .2s ease, opacity .2s ease;
}
.die .pip {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--die-pip); align-self: center; justify-self: center;
}
.die.held { transform: translateY(-8px); box-shadow: inset 0 -4px 0 var(--die-edge), 0 0 0 3px var(--accent), 0 10px 18px rgba(0,0,0,.4); }
.die.rolling { animation: tumble .5s ease; }
.die.disabled { pointer-events: none; opacity: .9; }
@keyframes tumble {
  0% { transform: rotate(0) scale(1); }
  25% { transform: rotate(-18deg) scale(1.08); }
  50% { transform: rotate(16deg) scale(0.96); }
  75% { transform: rotate(-8deg) scale(1.04); }
  100% { transform: rotate(0) scale(1); }
}
/* pip layouts via nth positions set in JS with data-face */
.die[data-face="1"] .pip:nth-child(1){grid-area:2/2}
.die[data-face="2"] .pip:nth-child(1){grid-area:1/1}.die[data-face="2"] .pip:nth-child(2){grid-area:3/3}
.die[data-face="3"] .pip:nth-child(1){grid-area:1/1}.die[data-face="3"] .pip:nth-child(2){grid-area:2/2}.die[data-face="3"] .pip:nth-child(3){grid-area:3/3}
.die[data-face="4"] .pip:nth-child(1){grid-area:1/1}.die[data-face="4"] .pip:nth-child(2){grid-area:1/3}.die[data-face="4"] .pip:nth-child(3){grid-area:3/1}.die[data-face="4"] .pip:nth-child(4){grid-area:3/3}
.die[data-face="5"] .pip:nth-child(1){grid-area:1/1}.die[data-face="5"] .pip:nth-child(2){grid-area:1/3}.die[data-face="5"] .pip:nth-child(3){grid-area:2/2}.die[data-face="5"] .pip:nth-child(4){grid-area:3/1}.die[data-face="5"] .pip:nth-child(5){grid-area:3/3}
.die[data-face="6"] .pip:nth-child(1){grid-area:1/1}.die[data-face="6"] .pip:nth-child(2){grid-area:1/3}.die[data-face="6"] .pip:nth-child(3){grid-area:2/1}.die[data-face="6"] .pip:nth-child(4){grid-area:2/3}.die[data-face="6"] .pip:nth-child(5){grid-area:3/1}.die[data-face="6"] .pip:nth-child(6){grid-area:3/3}

.controls { display: flex; align-items: center; justify-content: center; gap: 16px; margin-bottom: 10px; }
.roll-btn {
  background: var(--accent); color: #23180a; border: none; font-weight: 800;
  font-size: 17px; padding: 13px 30px; border-radius: 999px; cursor: pointer;
  box-shadow: 0 6px 16px rgba(0,0,0,.3); transition: transform .12s ease, filter .2s ease;
}
.roll-btn:hover:not(:disabled) { transform: translateY(-2px); filter: brightness(1.05); }
.roll-btn:disabled { opacity: .45; cursor: not-allowed; }
.rolls { color: var(--ink-dim); font-size: 14px; }
.status { text-align: center; color: var(--ink-dim); font-size: 14px; min-height: 20px; }
.lastmove { text-align: center; font-size: 13px; color: var(--accent); min-height: 18px; margin-top: 4px; opacity: .9; }
.lastmove.pop { animation: movepop .45s ease; }
@keyframes movepop { 0% { transform: scale(.92); opacity: 0; } 60% { transform: scale(1.03); } 100% { transform: scale(1); opacity: .9; } }

.card-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.scorecard { width: 100%; border-collapse: collapse; font-size: 14px; }
.scorecard th, .scorecard td { border: 1px solid var(--line); }
.scorecard th { text-align: left; padding: 9px 12px; color: var(--accent); font-size: 12px; text-transform: uppercase; letter-spacing: .05em; background: rgba(0,0,0,.18); }
.scorecard th.num { text-align: center; width: 62px; }
.scorecard th.num.name { font-size: 10px; line-height: 1.2; white-space: normal; text-transform: none; word-break: normal; overflow-wrap: break-word; hyphens: none; vertical-align: bottom; padding: 6px 5px; }
.scorecard td { padding: 8px 12px; }
.scorecard tr.cat .catname { color: var(--ink); }
.scorecard tr.cat td.val, .scorecard tr.cat td.aival { text-align: center; width: 60px; color: var(--ink-dim); font-variant-numeric: tabular-nums; }
.scorecard tr.cat.open-you td.val { cursor: pointer; color: var(--good); font-weight: 700; background: rgba(122,209,154,.10); }
.scorecard tr.cat.open-you:hover td.val { background: rgba(122,209,154,.22); box-shadow: inset 0 0 0 1px var(--good); }
.scorecard tr.cat.open-locked td.val { color: var(--ink-dim); opacity: .4; }
/* armed (selected, pending Submit) box */
.scorecard tr.cat td.catname { position: relative; }
.scorecard tr.cat.armed td.val { color: var(--accent); font-weight: 800; background: rgba(232,195,126,.26); box-shadow: inset 0 0 0 2px var(--accent); }
.scorecard tr.cat.open-you.armed:hover td.val { background: rgba(232,195,126,.30); }
/* while armed, keep the name on one line and reserve room for the Submit word (truncate if needed) */
.scorecard tr.cat.armed td.catname { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding-right: 82px; }
/* absolutely positioned + no background so it never reflows the name or changes row height */
.submit-score {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%);
  background: none; border: none; cursor: pointer;
  color: var(--good); font-weight: 800; font-size: 12px; letter-spacing: .08em; text-transform: uppercase;
  padding: 4px 4px; line-height: 1;
  text-shadow: 0 0 8px rgba(122,209,154,.9), 0 0 16px rgba(122,209,154,.55);
  animation: submitGlow 1.1s ease-in-out infinite alternate;
}
.submit-score:hover { color: #a8e6bf; }
@keyframes submitGlow {
  from { text-shadow: 0 0 5px rgba(122,209,154,.5), 0 0 10px rgba(122,209,154,.3); }
  to   { text-shadow: 0 0 12px rgba(122,209,154,1), 0 0 22px rgba(122,209,154,.65); }
}
.scorecard tr.cat.used td.val, .scorecard tr.cat.used td.aival { color: var(--ink); }
.scorecard tr.subtotal td { font-weight: 700; color: var(--accent); background: rgba(0,0,0,.15); }
.scorecard tr.subtotal td:first-child { color: var(--ink-dim); font-weight: 600; text-transform: uppercase; font-size: 11px; letter-spacing: .04em; }
.scorecard tr.subtotal.total td { color: var(--ink); background: rgba(0,0,0,.28); }
.scorecard tr.subtotal.total td.val { color: var(--accent); }
.scorecard tr.subtotal.grand td { background: rgba(232,195,126,.16); border-top: 2px solid var(--accent); }
.scorecard tr.subtotal.grand td:first-child { color: var(--accent); }
.scorecard tr.subtotal.grand td.val { color: var(--ink); font-size: 16px; }

#fireworks { position: fixed; inset: 0; pointer-events: none; z-index: 30; }

.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 20; }
.overlay.hidden { display: none; }
.modal { background: linear-gradient(160deg, #262421, #17150f); border: 1px solid var(--line); border-radius: 20px; padding: 32px 40px; text-align: center; width: 100%; max-width: min(360px, 92vw); box-shadow: 0 20px 60px rgba(0,0,0,.5); }
.modal h2 { color: var(--accent); margin-bottom: 10px; }
.modal p { color: var(--ink-dim); margin-bottom: 22px; line-height: 1.5; }

/* game-over result rows */
.result { margin: 6px 0 20px; }
.result-row {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 16px; border-radius: 12px; margin-bottom: 8px;
  border: 1px solid transparent;
}
.result-row .rname { flex: 1; text-align: left; font-size: 15px; font-weight: 600; overflow-wrap: anywhere; }
.result-row .rscore { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.result-row.winner { background: rgba(232,195,126,0.14); border-color: rgba(232,195,126,0.45); color: var(--accent); }
.result-row.loser { color: var(--ink-dim); }
.result-row.tie { background: rgba(255,255,255,0.05); border-color: var(--line); color: var(--ink); }
.result-badge { font-size: 13px; font-weight: 700; color: var(--accent); margin-bottom: 4px; }

.statsmodal { max-width: min(420px, 92vw); text-align: left; }
.statsmodal h2 { text-align: center; margin-bottom: 20px; }
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 24px; }
.stat-tile {
  background: rgba(255,255,255,0.05); border: 1px solid var(--line); border-radius: 12px;
  padding: 12px 10px; text-align: center;
}
.stat-tile .stat-num { font-size: 22px; font-weight: 800; color: var(--ink); font-variant-numeric: tabular-nums; }
.stat-tile .stat-lbl { font-size: 10px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-dim); margin-top: 3px; }
.stat-tile.wide { grid-column: span 3; display: flex; align-items: center; justify-content: space-between; text-align: left; }
.stat-tile.wide .stat-lbl { margin-top: 0; }
.stat-tile.accent .stat-num { color: var(--accent); }

.site-foot {
  max-width: 980px; margin: 44px auto 0; text-align: center;
  color: var(--ink-dim); font-size: 13px;
}
.site-foot a { color: var(--accent); text-decoration: none; font-weight: 600; }
.site-foot a:hover { text-decoration: underline; }

@media (max-width: 820px) {
  .table { grid-template-columns: 1fr; }

  /* stack the header: logo + best on one row, toggles wrap below */
  .topbar { flex-direction: column; align-items: stretch; gap: 12px; margin-bottom: 14px; }
  .brand { justify-content: space-between; }
  .logo { font-size: 26px; }
  .toggles { flex-wrap: wrap; }
  .chip { padding: 10px 14px; }

  /* keep all five dice on one row, sized to fit down to ~320px */
  .dice-tray { flex-wrap: nowrap; gap: clamp(6px, 2vw, 14px); padding: 12px 2px; }
  .die {
    width: clamp(44px, 13.5vw, 68px);
    height: clamp(44px, 13.5vw, 68px);
    border-radius: 14px;
    padding: clamp(6px, 2vw, 12px);
  }
  .die .pip { width: clamp(6px, 2vw, 12px); height: clamp(6px, 2vw, 12px); }
}

@media (max-width: 400px) {
  .modal { padding: 26px 22px; }
}

/* subtle staggered entrance so the board arrives with a little life */
@keyframes riseIn { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }
.players   { animation: riseIn .5s ease both; }
.dice-tray { animation: riseIn .5s ease .07s both; }
.controls  { animation: riseIn .5s ease .14s both; }
.status    { animation: riseIn .5s ease .2s both; }
.card-wrap { animation: riseIn .55s ease .26s both; }

@media (prefers-reduced-motion: reduce) {
  .players, .dice-tray, .controls, .status, .card-wrap { animation: none; }
  .die.rolling { animation: none; }
  .submit-score { animation: none; }
}
