/* ---------- tokens ---------- */
:root {
  color-scheme: light;
  --bg: #fbf3f0;
  --bg-tint: radial-gradient(1200px 600px at 50% -10%, #ffe6ec 0%, transparent 60%);
  --surface: #ffffff;
  --surface-2: #fdf1f3;
  --ink: #2c1b22;
  --ink-soft: #7d6670;
  --line: #ecd9dd;
  --accent: #b23a6b;
  --accent-ink: #ffffff;
  --accent-soft: #fce7ee;
  --good: #2f7d5d;
  --bad: #c0392b;
  --bad-soft: #fdecea;
  --shadow: 0 1px 2px rgba(64,20,36,.06), 0 6px 18px rgba(64,20,36,.07);
  --r: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #171114;
    --bg-tint: radial-gradient(1200px 600px at 50% -10%, #3a1f2e 0%, transparent 60%);
    --surface: #221a20;
    --surface-2: #2c212a;
    --ink: #f6e9ee;
    --ink-soft: #b39aa6;
    --line: #392c35;
    --accent: #f2799f;
    --accent-ink: #2a0e1b;
    --accent-soft: #3a2029;
    --good: #6ddaa8;
    --bad: #ff8b7d;
    --bad-soft: #3a2320;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 18px rgba(0,0,0,.35);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg-tint), var(--bg);
  background-repeat: no-repeat;
  color: var(--ink);
  font: 16px/1.45 ui-rounded, "SF Pro Rounded", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.app {
  max-width: 620px;
  margin: 0 auto;
  padding: 16px;
  padding-block: max(14px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-bottom));
  /* The game screen sizes itself to the viewport; see #game below. */
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
.screen { flex: 1 1 auto; display: grid; gap: 16px; align-content: start; }
/* Must outrank .screen's display:grid, or hidden screens still render. */
[hidden] { display: none !important; }

/* ---------- masthead ---------- */
.masthead { text-align: center; padding-block: 8px 4px; }
.mast-pig { width: 76px; height: 76px; margin: 0 auto 6px; color: var(--accent); }
.mast-pig svg, .win-pig svg { width: 100%; height: 100%; fill: currentColor; display: block; }
h1 { margin: 0; font-size: 2rem; letter-spacing: -.02em; }
.tagline { margin: 2px 0 0; color: var(--ink-soft); font-size: .95rem; letter-spacing: .12em; text-transform: uppercase; }

/* ---------- cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 16px;
  box-shadow: var(--shadow);
}
.card-title { margin: 0 0 12px; font-size: .8rem; letter-spacing: .1em; text-transform: uppercase; color: var(--ink-soft); }

/* ---------- target ---------- */
.target-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.chips { display: flex; gap: 8px; flex: 1 1 auto; }
.chip {
  flex: 1 1 0; min-width: 62px; padding: 11px 6px;
  font: inherit; font-weight: 700; font-variant-numeric: tabular-nums;
  color: var(--ink); background: var(--surface-2);
  border: 1.5px solid var(--line); border-radius: 10px; cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.target-custom { display: flex; align-items: center; gap: 6px; }
.target-custom input {
  width: 92px; padding: 10px 10px; font: inherit; font-weight: 700;
  font-variant-numeric: tabular-nums; color: var(--ink);
  background: var(--surface-2); border: 1.5px solid var(--line); border-radius: 10px;
}
.unit { color: var(--ink-soft); font-size: .85rem; }

/* ---------- player setup ---------- */
.player-inputs { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.player-inputs li { display: flex; align-items: center; gap: 8px; }
.pnum {
  flex: none; width: 26px; height: 26px; display: grid; place-items: center;
  border-radius: 50%; background: var(--accent-soft); color: var(--accent);
  font-size: .8rem; font-weight: 800;
}
.player-inputs input {
  flex: 1 1 auto; min-width: 0; padding: 11px 12px; font: inherit; color: var(--ink);
  background: var(--surface-2); border: 1.5px solid var(--line); border-radius: 10px;
}
.player-inputs input:focus-visible, .target-custom input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.rm {
  flex: none; width: 34px; height: 34px; border-radius: 9px; cursor: pointer;
  background: transparent; border: 1.5px solid var(--line); color: var(--ink-soft);
  font-size: 1.1rem; line-height: 1;
}
.rm:hover { color: var(--bad); border-color: var(--bad); }
.btn-add { margin-top: 10px; width: 100%; }
.hint { margin: 10px 0 0; font-size: .85rem; color: var(--bad); min-height: 1.2em; }

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-weight: 700; cursor: pointer;
  border-radius: 12px; border: 1.5px solid transparent;
  padding: 12px 16px; transition: transform .06s ease, filter .15s ease;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--accent); color: var(--accent-ink); box-shadow: var(--shadow); }
.btn-ghost { background: var(--surface); color: var(--ink); border-color: var(--line); }
.btn-xl { padding: 16px; font-size: 1.1rem; width: 100%; }
.btn-tiny { padding: 7px 11px; font-size: .8rem; background: var(--surface); color: var(--ink-soft); border-color: var(--line); }

/* ---------- rules ---------- */
.rules summary { cursor: pointer; font-size: .8rem; letter-spacing: .1em; text-transform: uppercase; color: var(--ink-soft); font-weight: 700; }
.rules-list { list-style: none; margin: 14px 0 0; padding: 0; display: grid; gap: 7px; font-size: .9rem; }
.rules-list li { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; }
.rules-list li + li { border-top: 1px solid var(--line); padding-top: 7px; }
.rules-list b { font-variant-numeric: tabular-nums; }
.rules-list .bad b { color: var(--bad); font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; }

/* The whole game fits the viewport: everything here is fixed height except
   .pigs, which absorbs whatever is left over. Scoring a combo must never
   lengthen the page. */
#game:not([hidden]) { display: flex; flex-direction: column; gap: 10px; }

/* ---------- topbar ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.topbar-target { font-size: .85rem; color: var(--ink-soft); }
.topbar-target b { color: var(--ink); font-variant-numeric: tabular-nums; }

/* ---------- scoreboard ---------- */
.scoreboard { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.scoreboard li {
  position: relative; overflow: hidden;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px; border-radius: 10px;
  background: var(--surface); border: 1.5px solid var(--line);
}
.scoreboard li.is-turn { border-color: var(--accent); background: var(--accent-soft); }
/* Five or more players: two columns, so the roster doesn't crowd out the picker. */
.scoreboard.is-dense { grid-template-columns: 1fr 1fr; }
.scoreboard.is-dense .sb-name { font-size: .88rem; }
.scoreboard.is-dense .sb-score { font-size: 1rem; }
.sb-fill { position: absolute; inset: 0 auto 0 0; background: var(--accent); opacity: .1; }
.sb-name { position: relative; flex: 1 1 auto; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scoreboard li.is-turn .sb-name::before { content: "🐷 "; }
.sb-score { position: relative; font-variant-numeric: tabular-nums; font-weight: 800; font-size: 1.15rem; }

/* ---------- turn card ---------- */
.turn-card {
  background: var(--surface); border: 1.5px solid var(--line); border-radius: var(--r);
  padding: 12px; text-align: center; box-shadow: var(--shadow);
}
.turn-who { font-size: .9rem; color: var(--ink-soft); }
.turn-who span { color: var(--ink); font-weight: 700; }
.turn-total {
  font-size: 2.7rem; line-height: 1; font-weight: 800; font-variant-numeric: tabular-nums;
  letter-spacing: -.03em; margin-top: 2px;
}
/* Fixed height, scrolled sideways rather than wrapped: a turn that racks up
   rolls must not grow this card and shove the pig buttons down mid-turn. */
.roll-log {
  display: flex; gap: 6px; margin-top: 8px;
  height: 28px; align-items: center;
  overflow-x: auto; overflow-y: hidden;
  scrollbar-width: none; -ms-overflow-style: none;
  justify-content: flex-start;
  justify-content: safe center;
  /* Fade the ends so a chip cut off by scrolling reads as more-to-see
     rather than as a rendering glitch. */
  -webkit-mask-image: linear-gradient(to right, transparent, #000 12px, #000 calc(100% - 12px), transparent);
  mask-image: linear-gradient(to right, transparent, #000 12px, #000 calc(100% - 12px), transparent);
}
.roll-log::-webkit-scrollbar { display: none; }
.roll-chip {
  flex: none; white-space: nowrap;
  font-size: .78rem; font-weight: 700; padding: 4px 9px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--line); color: var(--ink-soft);
}
.roll-chip b { color: var(--good); font-variant-numeric: tabular-nums; }
.roll-chip.is-bad { background: var(--bad-soft); border-color: transparent; color: var(--bad); }

/* ---------- banner ---------- */
/* One fixed-height line inside the turn card. It reads as the card's label
   until a roll is scored, so it never shifts the pickers below it. */
.banner {
  height: 22px; margin-top: 4px; padding-inline: 10px; border-radius: 7px;
  display: grid; place-items: center; overflow: hidden;
  text-align: center; font-weight: 700; font-size: .8rem; line-height: 1.1;
  background: var(--accent-soft); color: var(--accent);
}
.banner:not(.is-empty) { animation: pop .22s ease; }
.banner.is-empty {
  background: transparent; color: var(--ink-soft);
  font-size: .7rem; letter-spacing: .14em; text-transform: uppercase;
}
.banner.is-bad { background: var(--bad-soft); color: var(--bad); }
@keyframes pop { from { transform: scale(.96); opacity: 0 } to { transform: scale(1); opacity: 1 } }
@media (prefers-reduced-motion: reduce) { .banner { animation: none } .btn:active { transform: none } }

/* ---------- pig pickers ---------- */
/* flex:1 + min-height:0 lets this block take the slack and shrink instead of
   pushing the action row off-screen. */
.pigs { flex: 1 1 auto; min-height: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.pig-col { display: flex; flex-direction: column; min-height: 0; }
.pig-head {
  margin: 0 0 6px; font-size: .68rem; letter-spacing: .12em; text-transform: uppercase;
  color: var(--ink-soft); text-align: center;
}
.pig-opts {
  flex: 1 1 auto; min-height: 0; display: grid; gap: 5px;
  grid-template-rows: repeat(6, minmax(40px, 1fr));
}
.opt {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; min-height: 0; cursor: pointer; text-align: left;
  background: var(--surface); border: 1.5px solid var(--line); border-radius: 11px;
  font: inherit; color: var(--ink);
}
.opt svg { flex: none; width: 30px; height: 30px; fill: currentColor; color: var(--ink-soft); }
.opt-text { min-width: 0; display: grid; }
.opt-name { font-weight: 700; font-size: .84rem; line-height: 1.2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.opt-val { font-size: .7rem; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.opt[aria-pressed="true"] {
  background: var(--accent); border-color: var(--accent); color: var(--accent-ink);
}
.opt[aria-pressed="true"] svg { color: var(--accent-ink); }
.opt[aria-pressed="true"] .opt-val { color: var(--accent-ink); opacity: .75; }

/* ---------- actions ---------- */
.btn-oinker {
  display: grid; gap: 0; align-content: center; padding: 8px 6px;
  background: var(--bad-soft); color: var(--bad); border-color: transparent;
  font-size: .85rem;
}
.btn-oinker small { font-weight: 600; opacity: .8; font-size: .62rem; letter-spacing: .04em; text-transform: uppercase; }
.actions { display: grid; grid-template-columns: 1fr 1fr 1.5fr; gap: 8px; align-items: stretch; }
.actions .btn { padding: 10px 8px; }
.btn-end { font-size: 1.02rem; }

/* ---------- winner ---------- */
.win-card {
  background: var(--surface); border: 1.5px solid var(--accent); border-radius: var(--r);
  padding: 26px 20px; text-align: center; box-shadow: var(--shadow); display: grid; gap: 12px;
}
.win-pig { width: 84px; height: 84px; margin: 0 auto; color: var(--accent); }
.win-kicker { margin: 0; font-size: .72rem; letter-spacing: .16em; text-transform: uppercase; color: var(--ink-soft); }
.win-name { margin: -6px 0 0; font-size: 2rem; letter-spacing: -.02em; }
.final { list-style: none; margin: 4px 0 8px; padding: 0; display: grid; gap: 6px; text-align: left; }
.final li {
  display: flex; gap: 10px; align-items: baseline;
  padding: 9px 13px; border-radius: 10px; background: var(--surface-2);
}
.final .fname { flex: 1 1 auto; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.final .fscore { font-variant-numeric: tabular-nums; font-weight: 800; }
.final .frank { color: var(--ink-soft); font-size: .8rem; font-weight: 700; width: 1.2em; }

/* Narrow: keep two columns (stacking them would double the picker's height),
   just tighten the type. */
@media (max-width: 360px) {
  .opt svg { width: 22px; height: 22px; }
  .opt-name { font-size: .74rem; }
  .opt-val { font-size: .62rem; }
}

/* Short: trade ornament for fit so the pickers and End turn stay on screen
   without scrolling. Covers 667-820px tall phones with up to 4 players. */
@media (max-height: 820px) {
  .app {
    padding-block: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-bottom));
  }
  #game:not([hidden]) { gap: 8px; }
  .topbar-target { font-size: .8rem; }
  .btn-tiny { padding: 5px 9px; font-size: .75rem; }
  .scoreboard { gap: 4px; }
  .scoreboard li { padding: 5px 10px; }
  .sb-name { font-size: .9rem; }
  .sb-score { font-size: 1rem; }
  .turn-card { padding: 8px; }
  .turn-who { font-size: .8rem; }
  .turn-total { font-size: 1.9rem; }
  .banner { height: 19px; margin-top: 3px; font-size: .75rem; }
  .roll-log { height: 23px; margin-top: 5px; }
  .pig-head { margin-bottom: 4px; font-size: .64rem; }
  .pig-opts { grid-template-rows: repeat(6, minmax(36px, 1fr)); gap: 4px; }
  .opt { padding: 3px 6px; gap: 6px; }
  .opt svg { width: 22px; height: 22px; }
  .opt-name { font-size: .76rem; }
  .opt-val { font-size: .62rem; }
  .actions .btn { padding: 8px 6px; }
  .btn-end { font-size: .95rem; }
}
}
