/* Layout: one column — players, the roll, the score card (columns upright, rows sideways), score bar, action in thumb reach */
:root {
  --bg: #17110e;
  --surface: #241a15;
  --surface-2: #2f231c;
  --line: #4a3a30;
  --accent: #ff6b2c;
  --accent-deep: #b8461c;
  --on-accent: #17110e;
  --focus: #ffbd4a;
  --text: #f7ede1;
  --muted: #b5a191;
  --bust: #b8332b;
  --glow: rgba(255, 107, 44, .16);
  --paper: #f3e7cf;
  --cell: #fffaf0;
  --mark: #2b1d14;
  --sleep-ink: #2b1d14;
  --red: #c0392b; --red-ink: #ffffff; --red-num: #a8271c;
  --yellow: #f2c230; --yellow-ink: #2b1d14; --yellow-num: #7a5a00;
  --green: #267a40; --green-ink: #ffffff; --green-num: #1f6e38;
  --blue: #2a5fc0; --blue-ink: #ffffff; --blue-num: #1f4fa8;
  --display: 'Bagel Fat One', 'Arial Black', system-ui, sans-serif;
  --body: 'Rethink Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0; min-height: 100vh;
  background: radial-gradient(130% 55% at 50% 105%, var(--glow), transparent 62%), var(--bg);
  color: var(--text); font-family: var(--body); font-size: 16px; line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; }
button:focus-visible, input:focus-visible, a:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
h1, h2, h3, h4, p { margin: 0; }
#app {
  max-width: 560px; margin: 0 auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-block: 0 112px;
  display: grid; gap: 14px;
}
.eyebrow, .label { font-size: 11px; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); }
.muted { color: var(--muted); }

/* ---------- buttons and controls ---------- */
.btn {
  appearance: none; border: 0; cursor: pointer; width: 100%; min-height: 52px; padding: 0 18px; border-radius: 14px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; font-weight: 800; font-size: 17px;
}
.btn:active { transform: translateY(2px); }
.btn:disabled { opacity: .5; cursor: default; transform: none; }
.btn.primary { background: var(--accent); color: var(--on-accent); box-shadow: 0 3px 0 var(--accent-deep); }
.btn.ghost { background: var(--surface); color: var(--text); border: 1.5px solid var(--line); }
.btn.bust { background: var(--bust); color: #ffffff; }
.btn.text { background: transparent; color: var(--muted); min-height: 44px; }
.btn.big { min-height: 58px; font-family: var(--display); font-weight: 400; font-size: 21px; }
.icon-btn { appearance: none; flex: none; width: 46px; height: 46px; border-radius: 14px; cursor: pointer; border: 1.5px solid var(--line); background: var(--surface); display: grid; place-items: center; }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn:disabled { opacity: .35; cursor: default; }
.segmented { display: inline-flex; flex: none; gap: 2px; padding: 3px; border-radius: 12px; background: var(--bg); }
.segmented button { appearance: none; border: 0; cursor: pointer; min-height: 40px; padding: 0 12px; border-radius: 9px; background: none; color: var(--muted); font-weight: 700; font-size: 15px; }
.segmented button[aria-checked="true"] { background: var(--accent); color: var(--on-accent); }
.stepper { display: flex; align-items: center; gap: 4px; padding: 4px; border-radius: 14px; background: var(--bg); }
.stepper button { appearance: none; width: 44px; height: 44px; border: 0; border-radius: 10px; background: var(--surface-2); font-size: 24px; font-weight: 700; cursor: pointer; }
.stepper button:disabled { opacity: .3; cursor: default; }
.stepper output { width: 36px; text-align: center; font-family: var(--display); font-size: 24px; }

/* ---------- avatars ---------- */
.avatar { flex: none; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; background: var(--pc); color: #17110e; font-weight: 800; }
.avatar.pic { padding: 0; overflow: hidden; }
.avatar.pic svg { display: block; width: 100%; height: 100%; }
.avatar.avatar-btn { appearance: none; border: 0; padding: 0; cursor: pointer; position: relative; overflow: visible; width: 44px; height: 44px; }
.avatar-btn svg { border-radius: 50%; }
.avatar-btn::after {
  content: ""; position: absolute; right: -3px; bottom: -3px; width: 18px; height: 18px; border-radius: 50%;
  background: var(--accent) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M4 20h4L19 9l-4-4L4 16z'/%3E%3C/svg%3E") center / 11px no-repeat;
  box-shadow: 0 0 0 2px var(--surface);
}
.picker-head { margin: 4px 0 0; font-family: var(--display); font-weight: 400; font-size: 18px; }
.picker-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(62px, 1fr)); gap: 12px 6px; }
.picker-item { appearance: none; border: 0; background: none; cursor: pointer; display: grid; justify-items: center; gap: 4px; padding: 4px 0; border-radius: 12px; }
.picker-item .avatar { width: 52px; height: 52px; }
.picker-item.on .avatar { box-shadow: 0 0 0 3px var(--focus); }
.picker-name { font-size: 11.5px; line-height: 1.2; text-align: center; color: var(--muted); }
.picker-item.on .picker-name { font-weight: 700; color: var(--text); }

/* ---------- setup ---------- */
.setup { display: grid; gap: 14px; padding-top: 18px; }
.back-link { justify-self: start; display: inline-flex; align-items: center; min-height: 44px; padding: 0 4px; margin-bottom: -10px; color: var(--muted); font-weight: 700; text-decoration: none; }
.hero { display: grid; gap: 6px; }
.hero h1 { font-family: var(--display); font-weight: 400; font-size: clamp(52px, 15vw, 68px); line-height: 1; }
.hero p { color: var(--muted); max-width: 34ch; }
.box { background: var(--surface); border-radius: 18px; padding: 16px; display: grid; gap: 12px; }
.box h2 { font-size: 17px; font-weight: 800; }
.field-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.choices { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.choice {
  appearance: none; cursor: pointer; text-align: left; min-height: 78px; padding: 12px; border-radius: 14px;
  border: 1.5px solid var(--line); background: none; display: grid; align-content: start; gap: 4px;
}
.choice strong { font-size: 16px; }
.choice span { font-size: 13.5px; color: var(--muted); }
.choice[aria-checked="true"] { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent); }
.names { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.names li { display: flex; align-items: center; gap: 10px; }
.names input {
  flex: 1; min-width: 0; height: 48px; border-radius: 12px; border: 1.5px solid var(--line);
  background: var(--bg); color: var(--text); padding: 0 14px; font: inherit; font-size: 17px; font-weight: 600;
}
.names input::placeholder { color: var(--muted); font-weight: 400; }

/* ---------- top bar and players ---------- */
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 6;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding-block: 12px 8px; background: linear-gradient(var(--bg) 80%, transparent);
}
.turn { display: flex; align-items: center; gap: 10px; min-width: 0; }
.turn-text { display: grid; min-width: 0; }
.turn-name { font-family: var(--display); font-size: 26px; line-height: 1.05; color: var(--pc); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; gap: 8px; flex: none; }
.strip { display: flex; flex-wrap: wrap; gap: 8px; padding: 2px; }
.strip-item {
  appearance: none; flex: none; cursor: pointer; min-height: 48px; padding: 6px 10px 6px 6px; border-radius: 14px;
  border: 1.5px solid var(--line); background: var(--surface); display: flex; align-items: center; gap: 8px;
}
.strip-item .avatar { width: 32px; height: 32px; font-size: 14px; }
.strip-item.on { border-color: var(--pc); box-shadow: inset 0 0 0 1.5px var(--pc); }
.strip-name { font-weight: 700; max-width: 9ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { font-size: 11px; font-weight: 800; padding: 2px 7px; border-radius: 999px; background: var(--accent); color: var(--on-accent); }
.badge.done { background: var(--surface-2); color: var(--text); }

/* ---------- roll ---------- */
.status { background: var(--surface); border-radius: 16px; padding: 12px 14px; display: grid; gap: 10px; }
.status p { color: var(--muted); }
.status b { color: var(--text); }
.dice-row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.die {
  width: 42px; height: 42px; border-radius: 22%; padding: 6px;
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr);
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .18), 0 2px 0 rgba(0, 0, 0, .35);
}
.die i { margin: 16%; border-radius: 50%; }
.die-white { background: #fbf7ef; }
.die-white i.on, .die-yellow i.on { background: #241813; }
.die-red { background: var(--red); }
.die-yellow { background: var(--yellow); }
.die-green { background: var(--green); }
.die-blue { background: var(--blue); }
.die-red i.on, .die-green i.on, .die-blue i.on { background: #ffffff; }
.die.gone { background: transparent; box-shadow: none; border: 2px dashed var(--line); opacity: .5; }
.dice-row.rolling .die { animation: tumble .3s ease-in-out infinite; }
.combos { display: flex; flex-wrap: wrap; gap: 6px; }
.combo { padding: 4px 10px; border-radius: 999px; font-weight: 800; font-size: 14px; background: var(--surface-2); }
.combo-red { background: var(--red); color: var(--red-ink); }
.combo-yellow { background: var(--yellow); color: var(--yellow-ink); }
.combo-green { background: var(--green); color: var(--green-ink); }
.combo-blue { background: var(--blue); color: var(--blue-ink); }

/* ---------- score card ---------- */
.qcard {
  background: var(--paper); border-radius: 18px; padding: 8px;
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px;
  box-shadow: 0 12px 30px -18px rgba(0, 0, 0, .7);
}
.row-red { --row: var(--red); --row-ink: var(--red-ink); --row-num: var(--red-num); }
.row-yellow { --row: var(--yellow); --row-ink: var(--yellow-ink); --row-num: var(--yellow-num); }
.row-green { --row: var(--green); --row-ink: var(--green-ink); --row-num: var(--green-num); }
.row-blue { --row: var(--blue); --row-ink: var(--blue-ink); --row-num: var(--blue-num); }
.qrow {
  display: grid; grid-template-rows: auto repeat(12, 46px); gap: 4px; padding: 4px; border-radius: 12px;
  background: color-mix(in srgb, var(--row) 22%, var(--paper));
}
.qrow-head {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  padding: 5px 2px; border-radius: 8px; background: var(--row); color: var(--row-ink);
  font-weight: 800; font-size: 13px; line-height: 1.1;
}
.qrow-head .dir::before { content: "↓"; }
.qrow-head .shut { font-size: 10px; letter-spacing: .06em; text-transform: uppercase; }
.cell {
  appearance: none; border: 0; padding: 0; cursor: pointer; position: relative;
  display: grid; place-items: center; border-radius: 8px;
  background: var(--cell); color: var(--row-num);
  font-family: var(--display); font-size: 20px; line-height: 1;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, .08);
}
.cell[aria-disabled="true"] { cursor: default; }
.cell .mark { position: absolute; inset: 6%; width: 88%; height: 88%; }
.cell.marked .num { opacity: .3; }
.cell.lost { opacity: .3; }
.cell.blocked .num { opacity: .45; }
.cell.allowed { box-shadow: 0 0 0 3px var(--accent); animation: glow 1.2s ease-in-out infinite; z-index: 1; }
.cell.lock { color: var(--row-num); cursor: default; }
.cell.lock .lock-art { width: 22px; height: 22px; }
.cell.lock.marked .lock-art { opacity: .35; }
.qrow.closed .qrow-head { opacity: .7; }
button.cell.lock { cursor: pointer; }
.cell:disabled { cursor: default; }
.cell.lock.can-close::after {
  content: ""; position: absolute; inset: 5px; border-radius: 6px; pointer-events: none;
  border: 1.5px dashed color-mix(in srgb, var(--row-num) 45%, transparent);
}
.cell.lock.by-other { background: var(--row); color: var(--row-ink); --sleep-ink: var(--row); box-shadow: none; }
.qrow.by-other { background: color-mix(in srgb, var(--row) 9%, var(--paper)); }
.qrow.by-other .cell:not(.lock) { opacity: .4; filter: saturate(.4); }
.qrow.by-other .cell.marked { opacity: .7; }
.qrow.by-other .qrow-head .shut { font-size: 9px; letter-spacing: .02em; white-space: nowrap; }
@media (orientation: landscape) and (min-width: 640px) {
  #app {
    max-width: 1100px; padding-bottom: 92px;
    grid-template-columns: minmax(220px, 260px) minmax(0, 1fr); grid-auto-flow: row dense;
    column-gap: 16px; align-items: start;
  }
  #app > .topbar, #app > .setup, #app > .end { grid-column: 1 / -1; }
  #app > .qcard { grid-column: 2; grid-row: 2 / span 4; }
  #app > .strip, #app > .status, #app > .scorebar, #app > .action-bar { grid-column: 1; }
  #app > .action-bar { left: max(16px, env(safe-area-inset-left, 0px)); translate: none; width: 260px; }
  .qcard { grid-template-columns: 1fr; }
  .qrow { grid-template-rows: 46px; grid-template-columns: 64px repeat(12, minmax(0, 1fr)); }
  .qrow-head .dir::before { content: "→"; }
  .qrow.by-other .qrow-head .dir { display: none; }
  .cell { font-size: 17px; }
  .setup, .end { max-width: 560px; justify-self: center; width: 100%; }
}

/* ---------- score bar ---------- */
.scorebar { background: var(--surface); border-radius: 16px; padding: 12px 14px; display: grid; gap: 10px; }
.pens { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.pen-row { display: flex; gap: 6px; }
.pen { appearance: none; cursor: pointer; position: relative; width: 44px; height: 44px; border-radius: 10px; border: 2px solid var(--line); background: var(--cell); padding: 0; }
.pen.on { border-color: var(--bust); }
.pen .mark { position: absolute; inset: 8%; width: 84%; height: 84%; }
.points { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; font-variant-numeric: tabular-nums; }
.pt { min-width: 34px; text-align: center; padding: 4px 6px; border-radius: 8px; font-weight: 800; }
.pt-red { background: var(--red); color: var(--red-ink); }
.pt-yellow { background: var(--yellow); color: var(--yellow-ink); }
.pt-green { background: var(--green); color: var(--green-ink); }
.pt-blue { background: var(--blue); color: var(--blue-ink); }
.pt-pen { background: var(--surface-2); color: var(--text); }
.op { color: var(--muted); font-weight: 700; }
.points .total { margin-left: auto; font-family: var(--display); font-weight: 400; font-size: 30px; }

/* ---------- action bar ---------- */
.action-bar {
  position: fixed; z-index: 8; left: 50%; translate: -50% 0;
  bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  width: min(calc(100% - 2 * max(16px, env(safe-area-inset-left, 0px))), 528px);
}
.action-bar:empty { display: none; }
.action-bar .btn { box-shadow: 0 3px 0 var(--accent-deep), 0 10px 30px rgba(0, 0, 0, .45); }
.action-bar .btn.ghost { box-shadow: 0 10px 30px rgba(0, 0, 0, .45); }

/* ---------- end ---------- */
.end { display: grid; gap: 16px; padding-top: 26px; }
.end-hero { display: grid; gap: 6px; text-align: center; }
.end-hero h1 { font-family: var(--display); font-weight: 400; font-size: clamp(34px, 10vw, 48px); line-height: 1.05; color: var(--focus); }
.rank { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.rank li { display: flex; align-items: center; gap: 10px; padding: 10px 14px 10px 10px; border-radius: 16px; background: var(--surface); }
.rank li.first { box-shadow: inset 0 0 0 1.5px var(--focus); }
.rank .place { width: 22px; text-align: center; font-family: var(--display); font-size: 20px; color: var(--muted); }
.rank .who { flex: 1; min-width: 0; display: grid; gap: 4px; }
.p-name { font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.breakdown { display: flex; flex-wrap: wrap; gap: 4px; }
.breakdown .pt { min-width: 28px; padding: 2px 5px; font-size: 13px; }
.rank .total { font-family: var(--display); font-weight: 400; font-size: 28px; }
.end-actions { display: grid; gap: 8px; }

/* ---------- sheets, modal, toast ---------- */
.backdrop { position: fixed; inset: 0; z-index: 20; background: rgba(8, 5, 4, .6); animation: fade .2s ease; }
.sheet {
  position: fixed; z-index: 21; bottom: 0; left: 50%; translate: -50% 0; width: min(100%, 560px); max-height: 86%; overflow-y: auto;
  border-radius: 22px 22px 0 0; background: var(--surface); display: grid; gap: 14px;
  padding-top: 10px; padding-left: max(18px, env(safe-area-inset-left, 0px)); padding-right: max(18px, env(safe-area-inset-right, 0px));
  padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  box-shadow: 0 -10px 40px rgba(0, 0, 0, .5); animation: rise .25s cubic-bezier(.2, .9, .3, 1);
}
.grabber { width: 40px; height: 5px; border-radius: 3px; background: var(--line); justify-self: center; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.sheet-head h3 { font-family: var(--display); font-weight: 400; font-size: 24px; }
.close-btn { appearance: none; border: 0; width: 40px; height: 40px; border-radius: 12px; background: var(--surface-2); cursor: pointer; display: grid; place-items: center; }
.close-btn svg { width: 18px; height: 18px; }
.menu-list { display: grid; gap: 6px; }
.menu-item {
  appearance: none; border: 0; cursor: pointer; width: 100%; min-height: 54px; padding: 0 14px; border-radius: 14px;
  background: var(--surface-2); text-align: left; font-weight: 700; font-size: 16.5px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
}
.menu-item.static { cursor: default; padding-block: 8px; }
.menu-item.danger { color: #ff9a8f; }
.toggle-row { appearance: none; border: 0; padding: 0; background: none; cursor: pointer; width: 100%; text-align: left; display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.toggle-row > span:first-child { display: grid; gap: 2px; }
.toggle-row strong { font-size: 17px; font-weight: 800; }
.toggle-row .muted { font-size: 14px; }
.switch { flex: none; width: 52px; height: 32px; border-radius: 999px; position: relative; background: var(--line); transition: background .2s ease; }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: var(--text); transition: transform .2s ease; }
[aria-checked="true"] .switch { background: var(--accent); }
[aria-checked="true"] .switch::after { transform: translateX(20px); }
.menu-block { display: grid; gap: 8px; }
.menu-label { font-weight: 800; }
.closed-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip { appearance: none; cursor: pointer; min-height: 44px; padding: 0 16px; border-radius: 999px; border: 2px solid var(--line); background: none; font-weight: 800; }
.chip[aria-pressed="true"] { border-color: transparent; }
.chip-red[aria-pressed="true"] { background: var(--red); color: var(--red-ink); }
.chip-yellow[aria-pressed="true"] { background: var(--yellow); color: var(--yellow-ink); }
.chip-green[aria-pressed="true"] { background: var(--green); color: var(--green-ink); }
.chip-blue[aria-pressed="true"] { background: var(--blue); color: var(--blue-ink); }
.chip:disabled { cursor: default; opacity: .8; }
.rules { margin: 0; padding-left: 22px; display: grid; gap: 10px; }
.rules b { color: var(--focus); }
.modal-backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(8, 5, 4, .6); animation: fade .2s ease; }
.modal {
  position: fixed; z-index: 41; left: 50%; top: 50%; translate: -50% -50%; width: min(calc(100% - 48px), 360px);
  padding: 20px; border-radius: 20px; background: var(--surface); box-shadow: 0 20px 60px rgba(0, 0, 0, .6);
  display: grid; gap: 10px; animation: pop .2s ease;
}
.modal h3 { font-family: var(--display); font-weight: 400; font-size: 24px; line-height: 1.1; }
.modal p { color: var(--muted); }
.modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 6px; }
.modal-actions .btn { min-height: 48px; font-size: 16px; padding: 0 10px; }
.toast {
  position: fixed; z-index: 30; left: 50%; translate: -50% 0; bottom: calc(86px + env(safe-area-inset-bottom, 0px));
  width: min(calc(100% - 32px), 528px); display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-radius: 16px; background: var(--paper); color: var(--mark);
  box-shadow: 0 12px 30px rgba(0, 0, 0, .55); font-weight: 600; font-size: 15px;
}
.toast span { flex: 1; min-width: 0; }

@keyframes glow { 0%, 100% { box-shadow: 0 0 0 3px var(--accent); } 50% { box-shadow: 0 0 0 3px var(--accent), 0 0 14px 2px var(--accent); } }
@keyframes tumble { 0% { transform: rotate(-16deg) translateY(-3px); } 50% { transform: rotate(14deg) translateY(3px); } 100% { transform: rotate(-16deg) translateY(-3px); } }
@keyframes rise { from { translate: -50% 30px; opacity: 0; } to { translate: -50% 0; opacity: 1; } }
@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { opacity: 0; scale: .95; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
