:root {
  /* Layout: a phone-first app shell; paper cards on the picnic gingham, tab bar pinned to the bottom like a real app */
  --cloth: #ffc6d2;
  --weave: rgba(255, 255, 255, 0.5);
  --paper: #fff8fa;
  --white: #ffffff;
  --ink: #4a1f2e;
  --ink-soft: #7d4b5c;
  --line: rgba(74, 31, 46, 0.18);
  --berry: #e8435d;
  --berry-deep: #b92a44;
  --blush: #ffa3b5;
  --butter: #ffe7a3;
  --gold: #ffc53d;
  --leaf: #3e8e57;
  --leaf-soft: #cdeccf;
  --sky: #ffe1e8;
  --dusk: #d9ccf5;
  --bee: #ffd23f;
  --bear: #d9a27c;
  --bear-in: #b47652;
  --hog-face: #f6dcc4;
  --mac-pink: #ffb3c4;
  --mac-pistachio: #c6e5a3;
  --mac-lavender: #d6c4f5;
  --mac-lemon: #fff0a0;
  --mac-peach: #ffcfa8;
  --mac-mint: #b8ebdf;

  --f-display: "Bagel Fat One", ui-rounded, "Arial Rounded MT Bold", system-ui, sans-serif;
  --f-hand: "Gaegu", "Chalkboard SE", "Comic Sans MS", ui-rounded, cursive;

  --tabs-h: 84px;
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
/* No stretchy overscroll bounce: the app should feel like an app, not a web page. */
html { background: var(--cloth); overscroll-behavior-y: none; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body { min-height: 100%; }

body {
  margin: 0;
  background: transparent; /* the page colour comes from html; the gingham layer sits between them */
  color: var(--ink);
  font-family: var(--f-hand);
  font-size: 19px;
  line-height: 1.3;
  -webkit-tap-highlight-color: transparent;
}

/* The gingham lives on its own fixed layer so the phone's GPU just holds it still while you scroll.
   (background-attachment: fixed repaints on every frame on Android and jitters at the ends of the page.)
   It overhangs top and bottom so it still covers the screen while the address bar slides in and out. */
body::before {
  content: "";
  position: fixed;
  z-index: -1;
  inset: -120px 0;
  pointer-events: none;
  background-color: var(--cloth);
  background-image:
    linear-gradient(90deg, var(--weave) 50%, transparent 50%),
    linear-gradient(var(--weave) 50%, transparent 50%);
  background-size: 44px 44px;
  background-position: 0 120px;
  transform: translateZ(0);
}

.app {
  max-width: 34rem;
  margin: 0 auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-top: calc(env(safe-area-inset-top, 0px) + 14px);
  padding-bottom: calc(var(--tabs-h) + 24px);
}

h1, h2, h3 { font-family: var(--f-display); font-weight: 400; margin: 0; text-wrap: balance; }
p { margin: 0; }

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
:focus-visible { outline: 3px dashed var(--berry-deep); outline-offset: 3px; }

.eyebrow { font-weight: 700; font-size: 1.05rem; color: var(--ink-soft); letter-spacing: 0.02em; }
.hand { font-size: 1.15rem; color: var(--ink-soft); }

.card {
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: 24px;
  box-shadow: 5px 6px 0 var(--ink);
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-family: var(--f-display);
  font-size: 1.15rem;
  line-height: 1;
  padding: 0.6em 1.2em 0.7em;
  border: 3px solid var(--ink);
  border-radius: 999px;
  background: var(--white);
  box-shadow: 0 4px 0 var(--ink);
  transition: transform 0.08s, box-shadow 0.08s, background 0.15s;
  user-select: none;
  -webkit-user-select: none;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn.berry { background: var(--berry); color: var(--white); }
.btn.berry:hover { background: var(--berry-deep); }
.btn.big { font-size: 1.45rem; padding: 0.55em 1.6em 0.65em; }
.btn[disabled] { opacity: 0.6; pointer-events: none; }

/* ---------- header ---------- */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  text-decoration: none;
  font-family: var(--f-display);
  font-size: clamp(1.05rem, 5vw, 1.3rem);
  min-width: 0;
}
.brand-mark { display: flex; }
.brand-mark .avatar + .avatar { margin-left: -12px; }
.who {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 12px 3px 4px;
  border: 2.5px solid var(--ink);
  border-radius: 999px;
  background: var(--paper);
  font-size: 1.05rem;
}
.who b { color: var(--berry-deep); }

.avatar { flex: none; display: block; }

/* ---------- gate ---------- */
.gate { min-height: calc(100dvh - 40px); display: grid; place-items: center; padding-block: 24px; }
.gate-card { width: 100%; padding: 28px 22px 24px; display: grid; gap: 10px; }
.gate-faces { display: flex; gap: 4px; }
.gate-card h1 { font-size: clamp(2.4rem, 11vw, 3.2rem); line-height: 1; }
.field-label { font-weight: 700; font-size: 1.15rem; margin-top: 10px; }
.input {
  width: 100%;
  padding: 10px 14px;
  border: 2.5px solid var(--ink);
  border-radius: 14px;
  background: var(--white);
  font-size: 1.25rem;
}
.input::placeholder { color: rgba(125, 75, 92, 0.6); }
.gate-who { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.pick {
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 14px 8px 12px;
  border: 3px solid var(--ink);
  border-radius: 20px;
  background: var(--white);
  box-shadow: 0 4px 0 var(--ink);
  font-family: var(--f-display);
  font-size: 1.2rem;
  transition: transform 0.08s, box-shadow 0.08s;
}
.pick:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.pick[aria-busy="true"] { background: var(--butter); }
.error { min-height: 1.3em; font-weight: 700; color: var(--berry-deep); }

/* ---------- views ---------- */
.view { display: grid; gap: 22px; }
.view-head { display: grid; gap: 2px; }
.view-head h1 { font-size: clamp(2rem, 9vw, 2.6rem); line-height: 1.02; }
.view-head h1 .pn { color: var(--berry); }
.view-head .sub { margin-top: 6px; max-width: 34ch; }
.block { display: grid; gap: 10px; }
.block h2 { font-size: 1.3rem; }

/* ---------- trivia ---------- */
.stack { display: grid; gap: 16px; }
.qcard { padding: 18px 18px 18px; display: grid; gap: 12px; }
.qmeta { display: flex; align-items: center; gap: 6px; font-weight: 700; font-size: 1.05rem; color: var(--ink-soft); }
.qcard h3 { font-size: 1.45rem; line-height: 1.1; }
.opts { display: grid; gap: 8px; }
.opt {
  text-align: left;
  padding: 10px 14px;
  border: 2.5px solid var(--ink);
  border-radius: 14px;
  background: var(--white);
  font-weight: 700;
  font-size: 1.2rem;
  line-height: 1.15;
  box-shadow: 0 3px 0 var(--ink);
  transition: transform 0.08s, box-shadow 0.08s, background 0.15s;
}
.opt:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.opt[disabled] { cursor: default; transform: none; }
.opt.picked { background: var(--butter); }
.opt.right { background: var(--leaf-soft); }
.opt.wrong { background: var(--blush); text-decoration: line-through; text-decoration-thickness: 2px; }
.qres { font-weight: 700; font-size: 1.2rem; }
.qres.yay { color: var(--leaf); }
.qres.nay { color: var(--berry-deep); }

.empty { padding: 20px 18px; display: flex; gap: 14px; align-items: center; }
.empty p { font-size: 1.15rem; }
.empty b { font-family: var(--f-display); font-weight: 400; display: block; font-size: 1.2rem; color: var(--ink); }

.write { padding: 20px 18px 20px; display: grid; gap: 12px; }
.write h2 { font-size: 1.5rem; }
.goal { display: flex; align-items: center; gap: 8px; font-size: 1.1rem; color: var(--ink-soft); }
.goal svg { flex: none; }
.write form { display: grid; gap: 8px; }
.write label { font-weight: 700; font-size: 1.1rem; margin-top: 4px; }
.write label span { font-weight: 400; color: var(--ink-soft); }
.write textarea { resize: vertical; min-height: 3.2em; }
.input.right-ans { border-color: var(--leaf); box-shadow: inset 0 0 0 1.5px var(--leaf); }
.write .btn { justify-self: start; margin-top: 8px; }

.results { list-style: none; margin: 0; padding: 0; display: grid; }
.results li {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 12px;
  padding: 10px 2px;
  border-bottom: 2px dashed var(--line);
}
.results li:last-child { border-bottom: 0; }
.results .q { font-weight: 700; min-width: 0; overflow-wrap: anywhere; }
.results .sub { grid-column: 1; color: var(--ink-soft); font-size: 1.05rem; min-width: 0; overflow-wrap: anywhere; }
.results .chip { grid-column: 2; grid-row: 1 / span 2; align-self: center; }
.results .none { color: var(--ink-soft); grid-template-columns: 1fr; }
.results.games li { grid-template-columns: auto 1fr auto; align-items: center; }
.results.games .sub { grid-column: 2; }
.results.games .avatar { grid-row: 1 / span 2; }
.results.games .chip { grid-column: 3; }

.chip {
  display: inline-block;
  padding: 1px 10px 3px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.95rem;
  white-space: nowrap;
  background: var(--white);
}
.chip.wait { background: var(--butter); }
.chip.got { background: var(--leaf-soft); }
.chip.miss { background: var(--blush); }
.chip.daily { background: var(--mac-lavender); }

/* ---------- games ---------- */
.game-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.game-card {
  display: grid;
  gap: 4px;
  padding: 0 0 12px;
  text-align: left;
  border: 3px solid var(--ink);
  border-radius: 20px;
  background: var(--paper);
  box-shadow: 4px 5px 0 var(--ink);
  overflow: hidden;
  transition: transform 0.12s, box-shadow 0.12s;
}
.game-card:active { transform: translate(2px, 3px); box-shadow: 2px 2px 0 var(--ink); }
.game-card .thumb {
  display: block;
  width: 100%;
  aspect-ratio: 4 / 3;
  max-width: 100%;
  border-bottom: 3px solid var(--ink);
  margin-bottom: 6px;
}
.gc-title { font-family: var(--f-display); font-size: 1.08rem; line-height: 1.05; padding-inline: 12px; }
.gc-duel { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; padding-inline: 12px; font-size: 0.98rem; font-weight: 700; color: var(--ink-soft); }
.gc-duel .lead { color: var(--berry-deep); }
.gc-duel .crown { width: 18px; height: 15px; }

.game-screen { display: grid; gap: 12px; }
.game-bar { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px; }
.game-bar h1 { font-size: 1.35rem; text-align: center; line-height: 1.05; }
.back, .icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 44px;
  height: 44px;
  padding: 0 12px;
  border: 2.5px solid var(--ink);
  border-radius: 999px;
  background: var(--paper);
  font-weight: 700;
  font-size: 1.1rem;
}
.icon-btn { padding: 0; width: 44px; }

.mode { display: grid; grid-template-columns: 1fr 1fr; border: 2.5px solid var(--ink); border-radius: 999px; padding: 3px; background: var(--paper); }
.mode input { position: absolute; opacity: 0; pointer-events: none; }
.mode label { text-align: center; padding: 6px 8px 8px; border-radius: 999px; font-weight: 700; font-size: 1.1rem; cursor: pointer; }
.mode input:checked + label { background: var(--ink); color: var(--paper); }
.mode input:focus-visible + label { outline: 3px dashed var(--berry-deep); outline-offset: 2px; }

.arena {
  position: relative;
  height: clamp(360px, calc(100dvh - 228px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)), 700px);
  border: 3px solid var(--ink);
  border-radius: 22px;
  overflow: hidden;
  background: var(--sky);
  box-shadow: 5px 6px 0 var(--ink);
}
.arena canvas { display: block; width: 100%; height: 100%; touch-action: none; user-select: none; -webkit-user-select: none; }

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(255, 248, 250, 0.62);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}
/* Sized to fit the shortest arena without scrolling. */
.ov-card {
  width: min(100%, 22rem);
  display: grid;
  gap: 12px;
  justify-items: center;
  text-align: center;
  padding: 16px 16px 18px;
}
.ov-card h2 { font-size: 1.6rem; line-height: 1.02; }
.how { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; text-align: left; justify-self: stretch; }
.how li { display: flex; align-items: center; gap: 10px; font-size: 1.08rem; line-height: 1.12; }
.how canvas { flex: none; width: 34px; height: 34px; }
.ov-duel { display: grid; gap: 4px; justify-items: center; justify-self: stretch; }
.duel-strip { display: flex; align-items: center; justify-content: center; gap: 6px; flex-wrap: wrap; }
.duel-strip .vs { font-weight: 700; color: var(--ink-soft); }
.duel-pill {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px 3px 4px;
  border: 2.5px solid var(--ink);
  border-radius: 999px;
  background: var(--white);
  font-weight: 700;
  font-size: 1rem;
}
.duel-pill.lead { background: var(--butter); }
.duel-pill b { font-family: var(--f-display); font-weight: 400; font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.duel-pill b.none { color: var(--ink-soft); font-family: var(--f-hand); font-weight: 700; }
.duel-pill .crown { position: absolute; top: -12px; right: -8px; transform: rotate(16deg); }
.big-score { font-family: var(--f-display); font-size: 3.4rem; line-height: 0.9; color: var(--berry); -webkit-text-stroke: 2px var(--ink); paint-order: stroke fill; }
.ov-msg { font-weight: 700; font-size: 1.15rem; line-height: 1.2; }
.ov-msg.win { color: var(--leaf); }
.ov-note { font-size: 1.05rem; color: var(--ink-soft); }
.ov-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }

.duel { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; justify-self: stretch; }
.duel-side {
  display: grid;
  justify-items: center;
  gap: 2px;
  padding: 8px 6px;
  border: 2.5px solid var(--ink);
  border-radius: 16px;
  background: var(--white);
  position: relative;
}
.duel-side.lead { background: var(--butter); }
.duel-side .n { font-weight: 700; font-size: 1rem; }
.duel-side .v { font-family: var(--f-display); font-size: 1.5rem; line-height: 1; font-variant-numeric: tabular-nums; }
.duel-side .v.none { font-family: var(--f-hand); font-weight: 700; font-size: 1.05rem; color: var(--ink-soft); }
.duel-side .crown { position: absolute; top: -13px; right: -6px; transform: rotate(14deg); }

/* Inside a game the back button replaces the tab bar. */
body.in-game .tabs { display: none !important; }
body.in-game .app { padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 18px); }
body.in-game .toast { bottom: calc(env(safe-area-inset-bottom, 0px) + 24px); }

/* While a round is running, the arena takes the screen. */
body.playing { overflow: hidden; }
body.playing .top,
body.playing .tabs,
body.playing .mode { display: none !important; }
body.playing .arena { height: calc(100dvh - 92px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)); }
body.playing .app { padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 12px); }

/* ---------- scoreboard ---------- */
.board { padding: 14px 14px 10px; display: grid; }
.board-row {
  display: grid;
  grid-template-columns: minmax(0, 1.3fr) 1fr 1fr;
  align-items: center;
  gap: 8px;
  padding: 7px 0;
  border-bottom: 2px dashed var(--line);
}
.board-row:last-child { border-bottom: 0; }
.board-row.head { border-bottom: 3px solid var(--ink); padding-top: 0; }
.board-row .label { font-weight: 700; font-size: 1.08rem; line-height: 1.1; }
.board-row .label small { display: block; font-size: 0.92rem; font-weight: 400; color: var(--ink-soft); }
.board-row .player { display: grid; justify-items: center; gap: 2px; font-family: var(--f-display); font-size: 1rem; }
.cell {
  justify-self: center;
  position: relative;
  min-width: 3.2em;
  text-align: center;
  padding: 3px 10px 5px;
  border-radius: 999px;
  font-family: var(--f-display);
  font-size: 1.3rem;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.cell.lead { background: var(--butter); border: 2.5px solid var(--ink); }
.cell.lead .crown { position: absolute; top: -12px; right: -10px; transform: rotate(16deg); }
.cell.zero { color: var(--ink-soft); font-family: var(--f-hand); font-weight: 700; }

.duel-list { display: grid; gap: 10px; }
.duel-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 2.5px solid var(--ink);
  border-radius: 16px;
  background: var(--paper);
}
.duel-row .t { font-family: var(--f-display); font-size: 1.05rem; line-height: 1.05; min-width: 0; }
.duel-row .t small { display: block; font-family: var(--f-hand); font-weight: 700; font-size: 0.98rem; color: var(--ink-soft); }
.mini { display: inline-flex; align-items: center; gap: 4px; font-family: var(--f-display); font-size: 1.1rem; font-variant-numeric: tabular-nums; padding: 2px 8px 3px 3px; border-radius: 999px; }
.mini.lead { background: var(--butter); box-shadow: inset 0 0 0 2px var(--ink); }
.mini.none { color: var(--ink-soft); font-family: var(--f-hand); font-weight: 700; }

/* ---------- tab bar ---------- */
.tabs {
  position: fixed;
  z-index: 30;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 8px max(16px, env(safe-area-inset-right, 0px)) calc(8px + env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
  background: var(--paper);
  border-top: 3px solid var(--ink);
}
.tabs button {
  position: relative;
  flex: 1;
  max-width: 10rem;
  display: grid;
  justify-items: center;
  gap: 1px;
  padding: 6px 4px 5px;
  border: 2.5px solid transparent;
  border-radius: 16px;
  background: none;
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--ink-soft);
}
.tabs button[aria-current="page"] { background: var(--butter); border-color: var(--ink); color: var(--ink); }
.tabs svg { width: 28px; height: 28px; }
.badge {
  position: absolute;
  top: 2px;
  right: calc(50% - 26px);
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--berry);
  color: var(--white);
  font-family: var(--f-display);
  font-size: 0.8rem;
  line-height: 18px;
}

/* ---------- toast & confetti ---------- */
.toast {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: calc(var(--tabs-h) + 14px);
  transform: translate(-50%, 20px);
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 8px 16px 10px;
  border: 2.5px solid var(--ink);
  border-radius: 16px;
  background: var(--ink);
  color: var(--paper);
  font-weight: 700;
  font-size: 1.1rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

#confetti { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 60; pointer-events: none; }

.write .error:empty { display: none; }
.field-hint { margin-top: -4px; font-size: 1.02rem; line-height: 1.2; color: var(--ink-soft); }
.brand { white-space: nowrap; }

@media (max-width: 380px) {
  .brand-mark { display: none; }
  .brand { font-size: 1.1rem; }
  .game-bar h1 { font-size: 1.1rem; }
  .back { padding: 0 10px; }
}

@media (min-width: 560px) {
  .game-grid { gap: 18px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
