:root {
  --paper: #FFF4DF;
  --paper-2: #FBE8C4;
  --ink: #1B1A17;
  --ink-soft: #5E5A50;
  --grass: #23B26D;
  --grass-deep: #128552;
  --sun: #FFC93C;
  --tomato: #FF5A36;
  --sky: #4BB8F2;
  --card: #FFFFFF;
  --radius: 18px;
  --border: 3px solid var(--ink);
  --shadow: 4px 4px 0 var(--ink);
  --shadow-lg: 6px 6px 0 var(--ink);
  --font: 'Bricolage Grotesque', ui-rounded, system-ui, -apple-system, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font);
  background: var(--paper);
  background-image: radial-gradient(circle at 1px 1px, rgba(27,26,23,.09) 1.2px, transparent 0);
  background-size: 22px 22px;
  color: var(--ink);
  font-size: 17px;
  line-height: 1.35;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

#app {
  max-width: 520px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px max(28px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 16px;
  min-height: 100dvh;
}
#app > [data-k] { display: contents; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; }
.center { text-align: center; }
.muted { color: var(--ink-soft); }
.small { font-size: 14px; }
.spacer { flex: 1; }
.hidden { display: none !important; }

h1, h2, h3 { margin: 0; line-height: 1.05; letter-spacing: -0.02em; }
h1 { font-size: 44px; font-weight: 800; }
h2 { font-size: 30px; font-weight: 800; }
h3 { font-size: 20px; font-weight: 800; }

/* Top bar */
.topbar { display: flex; align-items: center; gap: 10px; min-height: 40px; }
.brand { font-weight: 800; font-size: 22px; display: flex; align-items: center; gap: 6px; letter-spacing: -0.03em; }
.brand .logo { width: 30px; height: 30px; }
.pill {
  border: var(--border); border-radius: 999px; padding: 4px 12px; font-weight: 800; font-size: 14px;
  background: var(--card); white-space: nowrap;
}
.pill.code { letter-spacing: .14em; }
.iconbtn {
  border: var(--border); background: var(--card); border-radius: 12px; width: 40px; height: 40px;
  display: grid; place-items: center; font-size: 18px; cursor: pointer; box-shadow: 2px 2px 0 var(--ink);
  color: var(--ink); padding: 0;
}
.iconbtn:active { transform: translate(2px, 2px); box-shadow: none; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--grass); display: inline-block; }
.dot.off { background: var(--tomato); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .3; } }

/* Cards & buttons */
.card {
  background: var(--card); border: var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px;
}
.card.sun { background: var(--sun); }
.card.grass { background: var(--grass); color: #fff; }
.card.sky { background: var(--sky); }
.card.tomato { background: var(--tomato); color: #fff; }

.btn {
  font: inherit; font-weight: 800; font-size: 19px; color: var(--ink);
  border: var(--border); border-radius: 16px; background: var(--card);
  padding: 15px 18px; min-height: 58px; width: 100%;
  box-shadow: var(--shadow); cursor: pointer; transition: transform .06s, box-shadow .06s;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  touch-action: manipulation; user-select: none;
}
.btn:active:not(:disabled) { transform: translate(4px, 4px); box-shadow: 0 0 0 var(--ink); }
.btn.primary { background: var(--grass); color: #fff; }
.btn.sun { background: var(--sun); }
.btn.dark { background: var(--ink); color: var(--paper); }
.btn.ghost { background: transparent; box-shadow: none; border-style: dashed; }
.btn.small { min-height: 44px; font-size: 16px; padding: 8px 12px; border-radius: 12px; box-shadow: 3px 3px 0 var(--ink); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

.input {
  font: inherit; font-size: 22px; font-weight: 600; color: var(--ink);
  width: 100%; padding: 14px 16px; min-height: 60px;
  border: var(--border); border-radius: 16px; background: var(--card); outline: none;
  box-shadow: inset 0 -4px 0 rgba(0,0,0,.06);
}
.input:focus { box-shadow: 0 0 0 4px var(--sun); }
.input.code { text-transform: uppercase; letter-spacing: .3em; text-align: center; font-weight: 800; font-size: 28px; }
.input::placeholder { color: #B5AE9E; font-weight: 400; letter-spacing: normal; }
label.lbl { font-weight: 800; font-size: 14px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-soft); }

/* Home */
.hero { text-align: center; padding: 18px 0 4px; }
.hero .sheep { width: 120px; height: 120px; animation: bob 2.4s ease-in-out infinite; }
.hero h1 { font-size: 64px; margin-top: 4px; }
.hero p { margin: 6px 0 0; font-size: 19px; color: var(--ink-soft); }
@keyframes bob { 50% { transform: translateY(-6px) rotate(-3deg); } }

.avatars { display: flex; gap: 8px; overflow-x: auto; padding: 4px 4px 10px; scroll-snap-type: x mandatory; scrollbar-width: none; }
.avatars::-webkit-scrollbar { display: none; }
.avatar-pick {
  flex: none; width: 54px; height: 54px; font-size: 30px; border-radius: 14px; border: var(--border);
  background: var(--card); display: grid; place-items: center; cursor: pointer; scroll-snap-align: center; padding: 0;
}
.avatar-pick.on { background: var(--sun); box-shadow: 3px 3px 0 var(--ink); transform: translateY(-2px); }

.divider { display: flex; align-items: center; gap: 12px; color: var(--ink-soft); font-weight: 800; font-size: 14px; }
.divider::before, .divider::after { content: ''; flex: 1; height: 3px; background: var(--ink); opacity: .15; border-radius: 2px; }

/* Lobby */
.bigcode { font-size: 64px; font-weight: 800; letter-spacing: .14em; text-align: center; line-height: 1; padding-left: .14em; }
.qrwrap { display: grid; place-items: center; }
#qr { background: #fff; padding: 12px; border: var(--border); border-radius: 16px; box-shadow: var(--shadow); line-height: 0; }
#qr svg { width: 176px; height: 176px; display: block; }

.players { display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); gap: 10px; }
.player {
  position: relative; border: var(--border); border-radius: 16px; background: var(--card); padding: 10px 6px 8px;
  text-align: center; font-weight: 800; font-size: 15px; cursor: default; animation: pop .35s cubic-bezier(.3,1.6,.6,1) both;
}
.player.tap { cursor: pointer; }
.player .face { font-size: 34px; line-height: 1.1; width: 52px; height: 52px; border-radius: 50%; display: grid; place-items: center; margin: 0 auto 4px; border: 3px solid var(--ink); }
.player .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player.me { background: var(--paper-2); }
.player.away { opacity: .45; }
.player .crown { position: absolute; top: -12px; left: 50%; transform: translateX(-50%) rotate(-8deg); font-size: 20px; }
.player .tick { position: absolute; top: 6px; right: 6px; width: 22px; height: 22px; border-radius: 50%; background: var(--grass); color: #fff; font-size: 13px; display: grid; place-items: center; border: 2px solid var(--ink); }
@keyframes pop { from { transform: scale(.6); opacity: 0; } }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  font: inherit; font-weight: 800; font-size: 15px; border: var(--border); border-radius: 999px; padding: 7px 14px;
  background: var(--card); cursor: pointer; color: var(--ink); min-height: 40px;
}
.chip.on { background: var(--ink); color: var(--paper); }
.chip:disabled { cursor: default; }
.setting { display: flex; flex-direction: column; gap: 6px; }

/* Round header */
.roundbar { display: flex; align-items: center; gap: 10px; font-weight: 800; }
.category-card { text-align: center; padding: 22px 16px 24px; background: var(--sun); position: relative; overflow: hidden; }
.category-card .kicker { font-size: 13px; text-transform: uppercase; letter-spacing: .1em; font-weight: 800; opacity: .75; }
.category-card .cat { font-size: clamp(30px, 9vw, 44px); font-weight: 800; line-height: 1.02; margin-top: 6px; letter-spacing: -0.02em; word-break: break-word; }
.category-card.enter { animation: slam .5s cubic-bezier(.2,1.4,.5,1) both; }
@keyframes slam { from { transform: scale(1.3) rotate(-3deg); opacity: 0; } }

.timer { height: 14px; border: var(--border); border-radius: 999px; background: var(--card); overflow: hidden; position: relative; }
.timer i { position: absolute; inset: 0; transform-origin: left; background: var(--grass); transition: transform .25s linear, background .3s; }
.timer.low i { background: var(--tomato); }
.timer-row { display: flex; align-items: center; gap: 10px; font-weight: 800; font-variant-numeric: tabular-nums; }
.timer-row .timer { flex: 1; }

.locked { display: flex; align-items: center; gap: 12px; }
.locked .ans { font-size: 26px; font-weight: 800; word-break: break-word; }

.waiting { text-align: center; padding: 28px 12px; }
.waiting .sheep { width: 96px; height: 96px; animation: bob 1.6s ease-in-out infinite; }

.answered-strip { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.mini {
  display: inline-flex; align-items: center; gap: 6px; border: 2px solid var(--ink); border-radius: 999px; padding: 3px 10px 3px 4px;
  background: var(--card); font-weight: 800; font-size: 14px; transition: background .2s;
}
.mini .f { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-size: 16px; }
.mini.done { background: var(--grass); color: #fff; }
.mini.away { opacity: .45; }

/* Reveal */
.verdict { text-align: center; padding: 18px 14px; }
.verdict .big { font-size: 34px; font-weight: 800; line-height: 1; }
.verdict .pts { font-size: 56px; font-weight: 800; line-height: 1; margin-top: 6px; }
.verdict.enter { animation: pop .5s cubic-bezier(.3,1.6,.6,1) both; animation-delay: var(--d, 0s); }

.group {
  border: var(--border); border-radius: var(--radius); background: var(--card); box-shadow: var(--shadow);
  padding: 12px 14px; display: flex; flex-direction: column; gap: 10px; position: relative;
}
.group.enter { animation: dealIn .45s cubic-bezier(.2,1.3,.5,1) both; animation-delay: var(--d, 0s); }
@keyframes dealIn { from { transform: translateY(24px) scale(.9) rotate(-2deg); opacity: 0; } }
.group.herd { background: var(--grass); color: #fff; }
.group.mine { outline: 4px solid var(--sun); outline-offset: 3px; }
.group.lone { background: var(--card); }
.group.none { background: transparent; border-style: dashed; box-shadow: none; }
.group.sel { background: var(--sky); color: var(--ink); transform: translate(2px,2px); box-shadow: 2px 2px 0 var(--ink); }
.group.tap { cursor: pointer; }
.group .head { display: flex; align-items: baseline; gap: 10px; }
.group .label { font-size: 26px; font-weight: 800; flex: 1; word-break: break-word; line-height: 1.05; }
.group .count { font-weight: 800; font-size: 15px; border: 2px solid currentColor; border-radius: 999px; padding: 2px 10px; white-space: nowrap; }
.group .who { display: flex; flex-wrap: wrap; gap: 6px; }
.group .who .mini { color: var(--ink); }
.group .who .mini.tap { cursor: pointer; }
.group .raw { font-size: 13px; opacity: .85; }
.group .merged { font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; opacity: .8; }

.hosttip { font-size: 14px; color: var(--ink-soft); text-align: center; padding: 0 8px; }

/* Scoreboard */
.board { display: flex; flex-direction: column; gap: 8px; }
.brow {
  display: flex; align-items: center; gap: 10px; border: var(--border); border-radius: 14px; background: var(--card);
  padding: 8px 12px; font-weight: 800; transition: transform .4s;
}
.brow.me { background: var(--paper-2); }
.brow .rank { width: 26px; text-align: center; font-size: 18px; }
.brow .f { width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center; font-size: 22px; border: 2px solid var(--ink); }
.brow .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brow .sc { font-size: 22px; font-variant-numeric: tabular-nums; }
.brow .plus { font-size: 14px; color: var(--grass-deep); min-width: 34px; text-align: right; }

/* Game over */
.podium { display: grid; grid-template-columns: 1fr 1fr 1fr; align-items: end; gap: 8px; padding-top: 16px; }
.pod { text-align: center; font-weight: 800; }
.pod .f { font-size: 44px; animation: bob 2s ease-in-out infinite; }
.pod .nm { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pod .blk { border: var(--border); border-radius: 12px 12px 0 0; border-bottom: none; display: grid; place-items: center; font-size: 28px; box-shadow: var(--shadow); }
.pod.p1 .blk { height: 120px; background: var(--sun); }
.pod.p2 .blk { height: 86px; background: var(--card); }
.pod.p3 .blk { height: 62px; background: var(--paper-2); }
.awards { display: grid; gap: 10px; }
.award { display: flex; gap: 12px; align-items: center; }
.award .em { font-size: 32px; }
.award b { display: block; }

/* Sheet (bottom modal) */
.sheet-bg { position: fixed; inset: 0; background: rgba(27,26,23,.45); z-index: 50; display: flex; align-items: flex-end; justify-content: center; animation: fade .15s; }
.sheet { background: var(--paper); border: var(--border); border-bottom: none; border-radius: 22px 22px 0 0; width: 100%; max-width: 520px; padding: 18px 16px max(20px, env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 10px; animation: up .22s cubic-bezier(.2,1,.4,1); }
@keyframes fade { from { opacity: 0; } }
@keyframes up { from { transform: translateY(100%); } }

/* Toast */
#toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translate(-50%, 140%);
  background: var(--ink); color: var(--paper); font-weight: 800; padding: 12px 18px; border-radius: 14px;
  transition: transform .25s cubic-bezier(.2,1.3,.5,1), visibility .25s; visibility: hidden; z-index: 60; max-width: 90vw; text-align: center;
}
#toast.show { transform: translate(-50%, 0); visibility: visible; }
#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 40; width: 100vw; height: 100vh; }

.banner { border: var(--border); border-radius: 14px; padding: 10px 14px; font-weight: 800; background: var(--tomato); color: #fff; display: flex; align-items: center; gap: 8px; }

/* TV mode: big room screen */
body.tv #app { max-width: 1400px; padding: 32px 48px; gap: 24px; }
body.tv { font-size: 22px; }
body.tv .bigcode { font-size: 140px; }
body.tv #qr svg { width: 300px; height: 300px; }
body.tv .players { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 16px; }
body.tv .player { font-size: 22px; }
body.tv .player .face { width: 80px; height: 80px; font-size: 50px; }
body.tv .category-card .cat { font-size: 88px; }
body.tv .groups { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 18px; align-items: start; }
body.tv .group .label { font-size: 40px; }
body.tv .mini { font-size: 20px; }
body.tv .mini .f { width: 34px; height: 34px; font-size: 22px; }
body.tv .tvgrid { display: grid; grid-template-columns: 1.2fr 1fr; gap: 32px; align-items: start; }
body.tv .brow { font-size: 26px; padding: 12px 16px; }
body.tv .timer { height: 22px; }

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