:root {
  --bg: #f3f0e8;
  --surface: #ffffff;
  --ink: #1c1f24;
  --muted: #5d6470;
  --line: #d8d2c4;
  --soft: #e7e2d6;
  --primary: #1d6b4e;
  --primary-ink: #ffffff;
  --accent: #e9b42f;
  --warn: #b3261e;
  --focus: #1a5fd0;
  --p1: #c0392b; --p2: #1f5fbf; --p3: #1d8a52; --p4: #8e44ad;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #13171d; --surface: #1c222a; --ink: #eef1f5; --muted: #a3acb9; --line: #2e3640; --soft: #262e38;
    --primary: #36b47c; --primary-ink: #0b1a12; --accent: #f2c230; --warn: #ff8a80; --focus: #8ab4ff;
    --p1: #ff7a6b; --p2: #79a8ff; --p3: #4fd18b; --p4: #c79bf0;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #13171d; --surface: #1c222a; --ink: #eef1f5; --muted: #a3acb9; --line: #2e3640; --soft: #262e38;
  --primary: #36b47c; --primary-ink: #0b1a12; --accent: #f2c230; --warn: #ff8a80; --focus: #8ab4ff;
  --p1: #ff7a6b; --p2: #79a8ff; --p3: #4fd18b; --p4: #c79bf0; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 18px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: inherit; }
button, select, input { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.top {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap;
  padding: max(10px, env(safe-area-inset-top)) 16px 6px; max-width: 1080px; margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; font-weight: 800; font-size: 1.1rem; white-space: nowrap; min-height: 44px; }
.pad-mark {
  width: 30px; height: 34px; border-radius: 5px; background: var(--accent); border: 2px solid var(--ink); flex: none;
  display: grid; place-items: center; font-size: .8rem; font-weight: 900; color: #1c1f24; position: relative;
}
.pad-mark::before { content: ""; position: absolute; top: -5px; left: 4px; right: 4px; height: 6px; border-radius: 3px; background: var(--ink); }
.top nav { display: flex; gap: 2px; flex-wrap: wrap; }
.top nav a { font-size: .95rem; color: var(--muted); padding: 0 8px; min-height: 44px; display: inline-flex; align-items: center; text-decoration: none; border-radius: 8px; }
.top nav a[aria-current="page"] { color: var(--ink); font-weight: 700; }

main { max-width: 1080px; margin: 0 auto; padding: 0 16px; }
.app { max-width: 640px; margin: 0 auto; padding: 0 16px; }
h1.app-title { font-size: 1.5rem; margin: 6px 0 2px; line-height: 1.2; }
.app-sub { margin: 0 0 12px; color: var(--muted); font-size: .95rem; }

.btn {
  min-height: 48px; padding: 10px 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); font-weight: 650; cursor: pointer; text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; -webkit-tap-highlight-color: transparent;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.warn { color: var(--warn); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row > .btn { flex: 1; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.hint { color: var(--muted); font-size: .9rem; min-height: 1.4em; margin: 6px 0 0; }
.hint.bad { color: var(--warn); }

/* Scoreboard (shared) */
.scores { display: grid; gap: 6px; margin-bottom: 12px; }
.score-row {
  display: grid; grid-template-columns: 10px 1fr auto; align-items: center; gap: 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px; min-height: 52px;
}
.score-row .sw { width: 10px; height: 28px; border-radius: 4px; }
.score-row .nm { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score-row .nm small { color: var(--muted); font-weight: 500; margin-left: 6px; }
.score-row .pts { font-variant-numeric: tabular-nums; font-weight: 800; font-size: 1.35rem; }
.score-row.cur { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }
.score-row.lead .pts::after { content: " ★"; color: var(--accent); font-size: .9rem; }

/* Farkle */
.turn { display: grid; gap: 10px; }
.turn-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.turn-head .who { font-weight: 800; font-size: 1.1rem; }
.turn-total { font-variant-numeric: tabular-nums; font-size: 2.2rem; font-weight: 900; line-height: 1; }
.dice-pick { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.die {
  aspect-ratio: 1; min-height: 44px; border-radius: 10px; border: 2px solid var(--ink); background: var(--surface);
  cursor: pointer; padding: 0; display: grid; place-items: center; -webkit-tap-highlight-color: transparent;
}
.die svg { width: 78%; height: 78%; display: block; }
.die svg circle { fill: var(--ink); }
.tray { display: flex; gap: 6px; flex-wrap: wrap; min-height: 52px; align-items: center; padding: 4px; border: 1px dashed var(--line); border-radius: 12px; }
.tray .die { width: 44px; height: 44px; background: var(--accent); border-color: #1c1f24; }
.tray .die svg circle { fill: #1c1f24; }
.tray-empty { color: var(--muted); font-size: .9rem; padding: 0 8px; }
.tray-score { font-weight: 700; }
.quick { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.quick .btn { padding: 8px 4px; font-size: .9rem; }
.big-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.big-actions .btn { min-height: 56px; font-size: 1.05rem; }
.banner { background: var(--accent); color: #1c1f24; border-radius: 12px; padding: 10px 14px; font-weight: 700; }
.banner[hidden] { display: none; }

/* Calculator */
.calc-result { font-size: 1.05rem; }
.calc-result .total { font-size: 2rem; font-weight: 900; font-variant-numeric: tabular-nums; }
.calc-result ul { margin: 6px 0 0; padding-left: 20px; }

/* Cribbage board */
.board-svg { width: 100%; height: auto; display: block; background: #b9875a; border-radius: 12px; border: 2px solid #6b4423; }
.board-svg .hole { fill: #4a2e17; }
.board-svg .lbl { font: 600 8px system-ui, sans-serif; fill: #3a2410; }
.board-svg .line { stroke: #3a2410; stroke-width: 1; stroke-dasharray: 2 2; }
.crib-players { display: grid; gap: 10px; margin-top: 12px; }
.crib-player { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; border-left: 8px solid var(--c); }
.crib-player .head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; }
.crib-player .nm { font-weight: 750; }
.crib-player .sc { font-weight: 900; font-size: 1.8rem; font-variant-numeric: tabular-nums; line-height: 1; }
.crib-player .sc small { font-size: .85rem; color: var(--muted); font-weight: 600; }
.pegs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.pegs [data-more] { grid-column: span 2; }
.pegs .btn { padding: 6px 2px; min-height: 46px; font-size: 1rem; }
.num-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.num-grid .btn { padding: 4px; min-height: 46px; }

/* Card picker */
.slots { display: grid; grid-template-columns: repeat(4, 1fr) 12px 1fr; gap: 6px; align-items: end; }
.slot-wrap { display: grid; gap: 2px; text-align: center; font-size: .75rem; color: var(--muted); }
.slot {
  aspect-ratio: 5 / 7; min-height: 60px; border-radius: 8px; border: 2px dashed var(--line); background: var(--surface);
  font-weight: 800; font-size: 1.15rem; cursor: pointer; padding: 0;
}
.slot.filled { border-style: solid; border-color: var(--ink); }
.slot.sel { box-shadow: 0 0 0 3px var(--focus); }
.slot.red, .suit-btn.red { color: #c0392b; }
:root[data-theme="dark"] .slot.red, :root[data-theme="dark"] .suit-btn.red { color: #ff7a6b; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .slot.red, :root:not([data-theme="light"]) .suit-btn.red { color: #ff7a6b; } }
.rank-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; }
.suit-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 5px; }
.rank-grid .btn, .suit-grid .btn { padding: 4px; min-height: 46px; }
.suit-btn { font-size: 1.4rem; }
.picked { background: var(--ink) !important; color: var(--bg) !important; }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; font-weight: 600; cursor: pointer; }
.check input { width: 22px; height: 22px; }

/* Generic keeper */
.keeper-list { display: grid; gap: 8px; }
.kp { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; border-left: 8px solid var(--c); }
.kp .nm { font-weight: 700; }
.kp .tot { font-size: 1.6rem; font-weight: 900; font-variant-numeric: tabular-nums; }
.kp .add { grid-column: 1 / -1; display: grid; grid-template-columns: 48px 1fr 80px; gap: 6px; }
.kp .add input { min-height: 48px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); padding: 6px 12px; font-size: 1.1rem; min-width: 0; }
.kp.lead .tot::after { content: " ★"; color: var(--accent); font-size: 1rem; }
table.hist { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: .95rem; }
table.hist th, table.hist td { border-bottom: 1px solid var(--line); padding: 6px 4px; text-align: right; }
table.hist th:first-child, table.hist td:first-child { text-align: left; color: var(--muted); }
.hist-wrap { overflow-x: auto; margin-top: 14px; }

/* Dialog */
dialog { border: 0; padding: 0; border-radius: 18px; background: var(--surface); color: var(--ink); width: min(520px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); }
dialog::backdrop { background: rgba(0,0,0,.45); }
.sheet { padding: 18px; display: grid; gap: 12px; }
.sheet h2 { margin: 0; font-size: 1.2rem; }
.field { display: grid; gap: 4px; font-weight: 600; font-size: .95rem; }
.field input, .field select { min-height: 48px; padding: 8px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); font-size: 1.05rem; width: 100%; }
.names { display: grid; gap: 6px; }

/* Hub */
.hero { padding: 8px 0 4px; max-width: 70ch; }
.hero h1 { font-size: 1.9rem; line-height: 1.2; margin: 8px 0; }
.hero p { color: var(--muted); margin: 0 0 8px; }
.games { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); margin: 16px 0; }
.game { display: grid; gap: 6px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; text-decoration: none; }
.game h2 { margin: 0; font-size: 1.2rem; }
.game p { margin: 0; color: var(--muted); font-size: .95rem; }
.game .go { color: var(--primary); font-weight: 700; font-size: .95rem; }
.game .links { display: flex; gap: 4px 14px; flex-wrap: wrap; font-size: .95rem; }
.game .links a { padding: 8px 0; color: var(--primary); font-weight: 650; }

/* Article */
.article { max-width: 72ch; margin: 0 auto; padding: 8px 16px 32px; }
.article h1 { font-size: 1.85rem; line-height: 1.2; margin: 8px 0 12px; }
.article h2 { font-size: 1.3rem; margin-top: 32px; }
.article h3 { font-size: 1.05rem; margin: 20px 0 4px; }
.article .lede { font-size: 1.1rem; color: var(--muted); }
.article .cta { margin: 20px 0; display: flex; gap: 8px; flex-wrap: wrap; }
.article table { width: 100%; border-collapse: collapse; margin: 8px 0 16px; font-variant-numeric: tabular-nums; }
.article th, .article td { border-bottom: 1px solid var(--line); padding: 8px 6px; text-align: left; vertical-align: top; }
.article td:last-child, .article th:last-child { text-align: right; }
.article details { border-top: 1px solid var(--line); padding: 12px 0; }
.article summary { font-weight: 700; cursor: pointer; min-height: 32px; }
.app-article { padding-top: 24px; }
.previews { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin: 8px 0 16px; }
.previews figure { margin: 0; }
.previews img { width: 100%; height: auto; border: 1px solid var(--line); border-radius: 8px; background: #fff; }
.previews figcaption { font-size: .85rem; color: var(--muted); margin-top: 4px; text-align: center; }

.foot { max-width: 1080px; margin: 24px auto 0; padding: 16px 16px 80px; color: var(--muted); font-size: .85rem; border-top: 1px solid var(--line); }
.foot a { display: inline-block; padding: 10px 2px; }

@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
#board { aspect-ratio: 381 / 172; }
#board:has(svg) { aspect-ratio: auto; }
#players.crib-players:empty { min-height: 330px; }
