:root {
  --pink: #ff4f9a;
  --pink-deep: #d9307a;
  --blue: #2f8cff;
  --blue-deep: #1a66d1;
  --green: #2fbf55;
  --green-deep: #219443;
  --gold: #ffc531;
  --bg: #1f2d33;
  --bg-2: #2a3c44;
  --text: #f6f3ee;
  --text-dim: #b9c4c8;
  --tile: #f2f2f7;
  --glyph: #8e8e93;
  --ink: #1c1c1e;
  --radius: 18px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(120% 80% at 50% 0%, var(--bg-2), var(--bg) 70%) fixed, var(--bg);
  color: var(--text);
  font-family: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

body { overflow: hidden; }

button { font: inherit; color: inherit; touch-action: manipulation; -webkit-tap-highlight-color: transparent; cursor: pointer; }
button:disabled { cursor: default; }
:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }

#app {
  position: fixed;
  inset: 0;
  height: 100vh;
  height: 100dvh;
}

.screen {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: calc(var(--safe-top) + 12px) calc(var(--safe-right) + 16px) calc(var(--safe-bottom) + 12px) calc(var(--safe-left) + 16px);
}

.p0 { --pc: var(--pink); --pc-deep: var(--pink-deep); }
.p1 { --pc: var(--blue); --pc-deep: var(--blue-deep); }

/* ---------- Icon tiles (grey, Shortcuts-style) ---------- */
.tile {
  --s: 44px;
  width: var(--s);
  height: var(--s);
  flex: none;
  border-radius: 24%;
  background: linear-gradient(180deg, #fbfbfd, #e4e4ea);
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.6) inset, 0 2px 6px rgba(0, 0, 0, 0.25);
  display: grid;
  place-items: center;
}
.tile svg { width: 62%; height: 62%; fill: var(--glyph); }

/* ---------- Buttons ---------- */
.btn {
  border: 0;
  border-radius: 999px;
  padding: 15px 28px;
  font-size: 19px;
  font-weight: 700;
  background: #fff;
  color: var(--pc-deep, var(--ink));
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.18), 0 8px 20px rgba(0, 0, 0, 0.2);
  transition: transform 0.08s ease;
  min-height: 52px;
}
.btn:active { transform: translateY(2px) scale(0.98); }
.btn-green { background: var(--green); color: #fff; }
.btn-gold { background: var(--gold); color: #3a2a00; }
.btn-ghost { background: transparent; color: var(--text-dim); box-shadow: none; font-weight: 600; font-size: 16px; padding: 10px 16px; min-height: 44px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; }

/* ---------- Setup ---------- */
.setup { overflow-y: auto; gap: 14px; }
.title {
  margin: 4px 0 0;
  text-align: center;
  font-size: clamp(30px, 9vw, 44px);
  font-weight: 800;
  letter-spacing: -0.02em;
}
.title .t-pink { color: var(--pink); }
.title .t-blue { color: var(--blue); }
.subtitle { margin: 0; text-align: center; color: var(--text-dim); font-size: 15px; }
.players { display: grid; gap: 14px; }
.player-panel {
  background: rgba(255, 255, 255, 0.06);
  border-radius: var(--radius);
  border-top: 5px solid var(--pc);
  padding: 12px 14px 14px;
  display: grid;
  gap: 10px;
  min-width: 0;
}
.player-panel h2 { margin: 0; font-size: 16px; color: var(--pc); text-transform: uppercase; letter-spacing: 0.06em; }
.name-input {
  width: 100%;
  font: inherit;
  font-size: 18px;
  font-weight: 600;
  padding: 12px 14px;
  border-radius: 12px;
  border: 3px solid transparent;
  background: #fff;
  color: var(--ink);
}
.name-input:focus { outline: none; border-color: var(--pc); }
.icon-grid {
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: repeat(2, 50px);
  grid-auto-columns: 50px;
  gap: 10px;
  overflow-x: auto;
  padding: 4px;
  scrollbar-width: none;
}
.icon-grid::-webkit-scrollbar { display: none; }
.icon-choice { border: 0; padding: 0; background: none; border-radius: 24%; }
.icon-choice .tile { --s: 50px; }
.icon-choice[aria-pressed="true"] { outline: 4px solid var(--pc); outline-offset: 2px; }
.icon-random .tile { background: transparent; box-shadow: none; border: 2px dashed var(--glyph); }
.icon-random .tile svg { fill: var(--text-dim); }
.setup-footer { margin-top: auto; display: flex; justify-content: center; padding-top: 6px; }

/* ---------- Mode picker ---------- */
.modes-screen { gap: 16px; justify-content: center; }
.modes { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; width: 100%; max-width: 520px; margin: 0 auto; }
.mode-card {
  border: 0;
  border-radius: 22px;
  padding: 14px 10px 12px;
  background: linear-gradient(180deg, #e3b57b, #c98e52);
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.15), 0 8px 18px rgba(0, 0, 0, 0.35);
  display: grid;
  justify-items: center;
  gap: 6px;
  color: #4a2a0a;
  font-size: 20px;
  font-weight: 800;
}
.mode-card img { width: min(28vw, 110px); height: min(28vw, 110px); pointer-events: none; filter: drop-shadow(0 4px 4px rgba(70, 35, 0, 0.35)); }
.mode-card:active { transform: scale(0.97); }

/* ---------- Cover screens (look away / pass the phone) ---------- */
.cover {
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 14px;
  background: radial-gradient(120% 90% at 50% 20%, var(--pc, var(--bg-2)), var(--pc-deep, var(--bg)));
}
.cover.neutral { background: radial-gradient(120% 90% at 50% 20%, #3c5560, var(--bg)); }
.cover .tile { --s: 104px; margin-bottom: 4px; }
.cover .kicker { font-size: 15px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.12em; opacity: 0.85; margin: 0; }
.cover h1 { margin: 0; font-size: clamp(30px, 8.5vw, 44px); line-height: 1.1; font-weight: 800; max-width: 16ch; }
.cover p { margin: 0; font-size: 19px; max-width: 26ch; line-height: 1.35; opacity: 0.95; }
.cover .btn { margin-top: 14px; }
.cover .rules { list-style: none; padding: 0; margin: 4px 0 0; display: grid; gap: 6px; font-size: 18px; font-weight: 600; }

/* ---------- Game screen ---------- */
.game { gap: 10px; padding-bottom: calc(var(--safe-bottom) + 10px); }
.hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-areas: "p0 mid p1" "banner banner banner";
  align-items: center;
  gap: 8px 8px;
}
.chip {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  padding: 5px 10px 5px 5px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.07);
  border: 2px solid transparent;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.chip.p0 { grid-area: p0; justify-self: start; }
.chip.p1 { grid-area: p1; justify-self: end; flex-direction: row-reverse; padding: 5px 5px 5px 10px; }
.chip.active { border-color: var(--pc); box-shadow: 0 0 14px color-mix(in srgb, var(--pc) 55%, transparent); }
.chip .tile { --s: 34px; }
.chip-text { display: grid; min-width: 0; line-height: 1.1; }
.chip.p1 .chip-text { text-align: right; }
.chip-name { font-size: 14px; font-weight: 700; color: var(--pc); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 9ch; }
.chip-score { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.clock { font-size: 19px; font-weight: 800; font-variant-numeric: tabular-nums; }
.clock.low { color: #ff6259; animation: pulse 0.8s ease-in-out infinite; }
.mid { grid-area: mid; display: grid; justify-items: center; gap: 2px; }
.round-label { font-size: 14px; font-weight: 800; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-dim); white-space: nowrap; }
.menu-btn { border: 0; background: rgba(255, 255, 255, 0.08); border-radius: 999px; width: 44px; height: 28px; color: var(--text-dim); font-size: 18px; line-height: 1; }
.banner {
  grid-area: banner;
  border-radius: 14px;
  padding: 9px 12px;
  text-align: center;
  font-size: 17px;
  font-weight: 800;
  color: #fff;
  background: var(--pc, #3c5560);
  box-shadow: 0 4px 14px color-mix(in srgb, var(--pc, #000) 40%, transparent);
  transition: background 0.25s;
}

.board-area { flex: 1; min-height: 0; display: grid; place-items: center; position: relative; }

.board {
  --cols: 3;
  --rows: 7;
  --cw: 80px;
  --ch: 80px;
  --gap: 4px;
  --pad: 12px;
  --art: 64px;
  --handle: 26px;
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cw));
  grid-template-rows: repeat(var(--rows), var(--ch));
  gap: var(--gap);
  padding: var(--pad);
  border-radius: 24px;
  background:
    radial-gradient(120% 70% at 25% 10%, rgba(255, 240, 210, 0.28), transparent 60%),
    repeating-linear-gradient(var(--grain, 91deg),
      rgba(110, 60, 15, 0.13) 0 2px, transparent 2px 11px,
      rgba(90, 45, 10, 0.09) 11px 12px, transparent 12px 27px,
      rgba(255, 230, 190, 0.08) 27px 29px, transparent 29px 41px),
    linear-gradient(var(--grain, 91deg), #dfb37a, #cf9659 45%, #d9a76c 70%, #c88b4f);
  box-shadow:
    inset 0 0 0 2px rgba(255, 235, 200, 0.25),
    inset 0 -8px 16px rgba(90, 45, 5, 0.28),
    inset 0 8px 14px rgba(255, 240, 215, 0.18),
    0 18px 34px rgba(0, 0, 0, 0.5);
}
.board[data-orient="landscape"] { --grain: 1deg; }

/* The cheese-board handle, on the hardware "bottom" of the phone. */
.board::after {
  content: "";
  position: absolute;
  background:
    radial-gradient(circle at center, var(--bg) 0 7px, rgba(70, 35, 5, 0.6) 8px, transparent 10px),
    linear-gradient(var(--grain, 91deg), #d6a56a, #c4874c);
  box-shadow: inset 0 0 0 2px rgba(255, 235, 200, 0.2), 0 8px 16px rgba(0, 0, 0, 0.4);
}
.board[data-handle="bottom"] { margin-bottom: var(--handle); }
.board[data-handle="right"] { margin-right: var(--handle); }
.board[data-handle="left"] { margin-left: var(--handle); }
.board[data-handle="bottom"]::after { left: 50%; top: 100%; width: 74px; height: var(--handle); margin-left: -37px; border-radius: 0 0 20px 20px; margin-top: -2px; }
.board[data-handle="right"]::after { top: 50%; left: 100%; height: 74px; width: var(--handle); margin-top: -37px; border-radius: 0 20px 20px 0; margin-left: -2px; }
.board[data-handle="left"]::after { top: 50%; right: 100%; height: 74px; width: var(--handle); margin-top: -37px; border-radius: 20px 0 0 20px; margin-right: -2px; }

.snack {
  border: 0;
  padding: 0;
  margin: 0;
  background: none;
  display: grid;
  place-items: center;
  position: relative;
  border-radius: 14px;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}
.snack img {
  width: var(--art);
  height: var(--art);
  pointer-events: none;
  -webkit-user-drag: none;
  /* The photos are cut out, so give them a soft shadow to sit on the board. */
  filter: drop-shadow(0 3px 3px rgba(70, 35, 0, 0.35));
  transition: transform 0.12s ease, opacity 0.25s ease;
}
.board.loading .snack img { opacity: 0; }
.board.loading .snack { pointer-events: none; }
.board.loading::before {
  content: "Setting out the snacks…";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: rgba(74, 42, 10, 0.75);
  font-size: 18px;
  font-weight: 800;
  animation: pulse 1.2s ease-in-out infinite;
}
/* Picked snacks get a halo in the picker's colour: pink, blue, or half and half when both picked it.
   Pink is on the left and blue on the right, like the scoreboard. */
.snack { isolation: isolate; } /* keeps the halo behind the photo but above the board */
.snack.by0 { --h0: var(--pink); --h1: var(--pink); }
.snack.by1 { --h0: var(--blue); --h1: var(--blue); }
.snack.by0.by1 { --h0: var(--pink); --h1: var(--blue); }
.snack:is(.by0, .by1)::before,
.snack:is(.by0, .by1)::after {
  content: "";
  position: absolute;
  z-index: -1;
  width: calc(var(--art) * 1.04);
  height: calc(var(--art) * 1.04);
  border-radius: 50%;
  background: conic-gradient(var(--h1) 0 50%, var(--h0) 0 100%);
  animation: halo-in 0.35s ease-out;
}
/* The ring */
.snack:is(.by0, .by1)::before {
  -webkit-mask: radial-gradient(closest-side, transparent calc(100% - 3.5px), #000 calc(100% - 3px));
  mask: radial-gradient(closest-side, transparent calc(100% - 3.5px), #000 calc(100% - 3px));
}
/* A soft tinted glow inside it */
.snack:is(.by0, .by1)::after {
  -webkit-mask: radial-gradient(closest-side, transparent 35%, rgba(0, 0, 0, 0.4) 100%);
  mask: radial-gradient(closest-side, transparent 35%, rgba(0, 0, 0, 0.4) 100%);
}
@keyframes halo-in { from { transform: scale(0.5); opacity: 0; } }
.snack:active img { transform: scale(0.92); }
.snack.gone img { visibility: hidden; }
.snack .tile { position: absolute; --s: calc(var(--art) * 0.8); }

.board.locked .snack { pointer-events: none; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 40;
  width: max-content;
  max-width: min(86vw, 360px);
  padding: 14px 20px;
  border-radius: 18px;
  background: rgba(20, 20, 22, 0.92);
  color: #fff;
  font-size: 18px;
  font-weight: 700;
  text-align: center;
  line-height: 1.3;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  transform: translate(-50%, -50%) scale(0.9);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
}
.toast.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(10, 15, 18, 0.6);
  animation: fade-in 0.2s ease;
}
.modal {
  width: min(100%, 360px);
  background: #fff;
  color: var(--ink);
  border-radius: 26px;
  padding: 24px 20px 20px;
  text-align: center;
  display: grid;
  justify-items: center;
  gap: 10px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
  animation: pop-in 0.28s cubic-bezier(0.2, 1.4, 0.4, 1);
}
.modal h2 { margin: 0; font-size: 26px; line-height: 1.15; font-weight: 800; }
.modal p { margin: 0; font-size: 17px; color: #48484a; }
.modal .tile { --s: 72px; }
.modal .btn { box-shadow: 0 3px 0 rgba(0, 0, 0, 0.15); }
.modal .btn-primary { background: var(--pc, var(--green)); color: #fff; }
.modal .btn-plain { background: #ececf0; color: var(--ink); }
.progress { width: 100%; height: 6px; border-radius: 3px; background: #ececf0; overflow: hidden; }
.progress > div { height: 100%; width: 100%; background: var(--pc, var(--green)); transform-origin: left; }
.mini-score { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }
.mini-score .s0 { color: var(--pink-deep); }
.mini-score .s1 { color: var(--blue-deep); }

/* ---------- Results ---------- */
.results { align-items: center; justify-content: center; text-align: center; gap: 12px; overflow-y: auto; }
.results .tile { --s: 110px; }
.results h1 { margin: 0; font-size: clamp(34px, 10vw, 52px); font-weight: 800; line-height: 1.05; color: var(--pc); }
.results .final-score { font-size: 30px; font-weight: 800; font-variant-numeric: tabular-nums; }
.results .final-score .s0 { color: var(--pink); }
.results .final-score .s1 { color: var(--blue); }
.results p { margin: 0; color: var(--text-dim); font-size: 17px; }
.bonus-callout {
  margin: 6px 0;
  padding: 14px 16px;
  border-radius: 20px;
  background: rgba(255, 197, 49, 0.12);
  border: 2px solid var(--gold);
  display: grid;
  gap: 10px;
  justify-items: center;
}
.bonus-callout strong { color: var(--gold); font-size: 18px; }

/* ---------- Effects ---------- */
.confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 60; }
.balloons { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 59; }
.balloon { position: absolute; bottom: -180px; will-change: transform; }
.balloon svg { display: block; filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.25)); }

@keyframes fade-in { from { opacity: 0; } }
@keyframes pop-in { from { transform: scale(0.85); opacity: 0; } }
@keyframes pulse { 50% { opacity: 0.45; } }

/* ---------- Landscape phones: one-row HUD to save height ---------- */
@media (orientation: landscape) and (max-height: 560px) {
  .screen { padding-top: calc(var(--safe-top) + 8px); padding-bottom: calc(var(--safe-bottom) + 8px); }
  .game { gap: 6px; }
  .hud { grid-template-columns: auto 1fr auto auto; grid-template-areas: "p0 banner mid p1"; gap: 10px; }
  .banner { padding: 6px 12px; font-size: 16px; }
  .mid { grid-auto-flow: column; align-items: center; gap: 8px; }
  .players { grid-template-columns: 1fr 1fr; }
  .modes { grid-template-columns: repeat(4, minmax(0, 1fr)); max-width: 760px; }
  .mode-card img { width: min(16vw, 22vh, 110px); height: min(16vw, 22vh, 110px); }
  .cover { gap: 8px; }
  .cover .tile { --s: 72px; }
  .cover h1 { font-size: 32px; max-width: 24ch; }
  .cover .btn { margin-top: 6px; }
  .results .tile { --s: 70px; }
}

@media (min-width: 700px) and (orientation: portrait) {
  .players { grid-template-columns: 1fr 1fr; }
}

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