:root {
  --paper: #fbf8ef;
  --line: #dbe5f3;
  --margin: #eeb0b0;
  --ink: #1d2a5b;
  --ink-soft: #5a6488;
  --pencil: #8a90a8;
  --card: #ffffff;
  --highlight: #ffe066;
  --good: #2b8a3e;
  --good-bg: #e6f6ea;
  --match: #c76b00;
  --match-bg: #fff1dc;
  --bad: #c92a5a;
  --bad-bg: #fde8ef;
  --shadow: 0 1px 0 rgba(29, 42, 91, .06), 0 6px 18px -8px rgba(29, 42, 91, .25);
  --radius: 14px;
  --hand: 'Patrick Hand', 'Comic Sans MS', cursive;
  --body: 'Nunito', system-ui, -apple-system, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  color: var(--ink);
  font-family: var(--body);
  font-size: 16px;
  line-height: 1.45;
  background-color: var(--paper);
  /* Ruled notebook paper with a red margin line */
  background-image:
    linear-gradient(90deg, transparent 27px, var(--margin) 27px, var(--margin) 29px, transparent 29px),
    repeating-linear-gradient(to bottom, transparent 0 31px, var(--line) 31px 32px);
  background-attachment: local;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px calc(24px + env(safe-area-inset-bottom)) 40px;
}

h1, h2, h3, .hand { font-family: var(--hand); font-weight: 400; letter-spacing: .01em; }
h1 { font-size: 2.6rem; line-height: 1; margin: 12px 0 4px; }
h2 { font-size: 1.9rem; line-height: 1.1; margin: 0 0 8px; }
h3 { font-size: 1.35rem; margin: 0 0 6px; }
p { margin: 0 0 12px; }
.muted { color: var(--ink-soft); }
.small { font-size: .875rem; }
.center { text-align: center; }
.stack > * + * { margin-top: 12px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; }
[hidden] { display: none !important; }

.hl {
  background: linear-gradient(transparent 55%, var(--highlight) 55%, var(--highlight) 90%, transparent 90%);
  padding: 0 .1em;
}

/* ---------- paper card ---------- */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  border: 1px solid rgba(29, 42, 91, .08);
}

/* ---------- form controls ---------- */
label { display: block; font-weight: 700; font-size: .9rem; margin-bottom: 6px; }
input[type=text] {
  width: 100%;
  font: inherit;
  font-size: 1.15rem;
  color: var(--ink);
  padding: 12px 14px;
  border: 2px solid #cfd6e8;
  border-radius: 12px;
  background: #fff;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input[type=text]:focus { border-color: var(--ink); box-shadow: 0 0 0 4px rgba(29, 42, 91, .12); }
input[type=text]:disabled, input[readonly] { background: #f4f5f9; color: var(--ink-soft); }
input.code-input { text-transform: uppercase; letter-spacing: .35em; font-weight: 800; text-align: center; font-size: 1.5rem; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 52px;
  padding: 12px 20px;
  border-radius: 14px;
  border: 2px solid var(--ink);
  background: var(--ink);
  color: #fff;
  font: inherit;
  font-weight: 800;
  font-size: 1.05rem;
  cursor: pointer;
  text-decoration: none;
  box-shadow: 0 3px 0 #0e1638;
  transition: transform .06s, box-shadow .06s, opacity .15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #0e1638; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.block { width: 100%; }
.btn.secondary { background: #fff; color: var(--ink); box-shadow: 0 3px 0 #c8cee0; border-color: #c8cee0; }
.btn.secondary:active { box-shadow: 0 1px 0 #c8cee0; }
.btn.yellow { background: var(--highlight); color: var(--ink); box-shadow: 0 3px 0 #c9a900; border-color: #c9a900; }
.btn.good { background: var(--good); border-color: var(--good); box-shadow: 0 3px 0 #1c5e2a; }
.btn.small { min-height: 38px; padding: 6px 12px; font-size: .9rem; border-radius: 10px; }
.link-btn { background: none; border: 0; padding: 6px; font: inherit; color: var(--ink-soft); text-decoration: underline; cursor: pointer; }

.divider { display: flex; align-items: center; gap: 10px; color: var(--pencil); font-family: var(--hand); font-size: 1.2rem; margin: 18px 0; }
.divider::before, .divider::after { content: ''; flex: 1; border-top: 2px dashed #cfd6e8; }

/* ---------- home ---------- */
.logo { text-align: center; padding: 18px 0 8px; }
.logo .scribble { display: inline-block; transform: rotate(-3deg); }
.logo .tag { font-family: var(--hand); font-size: 1.25rem; color: var(--ink-soft); }
.paper-plane { font-size: 3rem; display: block; animation: float 3s ease-in-out infinite; }
@keyframes float { 50% { transform: translateY(-6px) rotate(4deg); } }

.rules { font-size: .95rem; }
.rules summary { cursor: pointer; font-family: var(--hand); font-size: 1.3rem; list-style: none; }
.rules summary::-webkit-details-marker { display: none; }
.rules summary::before { content: '＋ '; }
.rules[open] summary::before { content: '－ '; }
.rules ol { padding-left: 1.2em; margin: 8px 0 0; }
.rules li { margin-bottom: 4px; }

/* ---------- lobby ---------- */
.room-code {
  font-family: var(--body);
  font-weight: 800;
  font-size: 3rem;
  letter-spacing: .25em;
  line-height: 1;
  margin: 4px 0 0 .25em;
}
.qr { width: 180px; height: 180px; margin: 8px auto; display: block; }
.join-url { word-break: break-all; font-size: .85rem; color: var(--ink-soft); }

.players { list-style: none; margin: 0; padding: 0; }
.players li {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 4px;
  border-bottom: 1px dashed #dde2ef;
  font-weight: 700;
  animation: pop .3s ease-out;
}
.players li:last-child { border-bottom: 0; }
.players li.offline { opacity: .45; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center; flex: none;
  font-family: var(--hand); font-size: 1.2rem; color: #fff;
}
.badge {
  font-size: .72rem; font-weight: 800; letter-spacing: .04em; text-transform: uppercase;
  padding: 3px 8px; border-radius: 99px; background: var(--highlight); color: var(--ink);
}
.badge.soft { background: #eef1f8; color: var(--ink-soft); }
.x-btn { margin-left: auto; background: none; border: 0; color: var(--pencil); font-size: 1.3rem; cursor: pointer; padding: 2px 8px; }

/* ---------- playing ---------- */
.timer-bar {
  position: sticky; top: 0; z-index: 5;
  margin: -16px -16px 14px -40px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px 40px;
  background: rgba(251, 248, 239, .94);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border-bottom: 2px solid var(--line);
}
.timer-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.clock { font-family: var(--body); font-weight: 800; font-size: 2.1rem; font-variant-numeric: tabular-nums; line-height: 1; }
.clock.warn { color: var(--bad); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; } }
.progress { height: 6px; border-radius: 99px; background: #e4e8f2; overflow: hidden; margin-top: 8px; }
.progress > div { height: 100%; background: var(--ink); transform-origin: left; transition: transform 1s linear, background .3s; }
.progress.warn > div { background: var(--bad); }

.prompt {
  display: grid;
  grid-template-columns: 56px 1fr;
  gap: 4px 12px;
  align-items: center;
  margin-bottom: 12px;
  padding: 12px;
}
.letter {
  grid-row: span 2;
  width: 56px; height: 56px;
  display: grid; place-items: center;
  font-family: var(--hand); font-size: 2.3rem; line-height: 1;
  background: var(--highlight);
  border-radius: 10px;
  transform: rotate(-4deg);
  box-shadow: 2px 3px 0 rgba(0, 0, 0, .08);
}
.prompt:nth-child(even) .letter { transform: rotate(3deg); background: #ffd6e0; }
.prompt:nth-child(3n) .letter { background: #cdeffd; }
.topic { font-weight: 800; font-size: 1.02rem; line-height: 1.2; }
.prompt input { grid-column: 1 / -1; margin-top: 6px; font-family: var(--hand); font-size: 1.4rem; padding: 8px 12px; }
.prompt .hint { grid-column: 1 / -1; font-size: .8rem; min-height: 1.1em; color: var(--ink-soft); }
.prompt .hint.bad { color: var(--bad); font-weight: 700; }
.prompt .hint.ok { color: var(--good); }
.prompt.filled { border-color: rgba(43, 138, 62, .35); }

.play-footer {
  position: sticky; bottom: 0;
  margin: 16px -16px 0 -40px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom)) 40px;
  background: linear-gradient(transparent, var(--paper) 30%);
}

.times-up {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: center;
  background: rgba(251, 248, 239, .9);
  font-family: var(--hand); font-size: 3rem; text-align: center;
  animation: pop .35s ease-out;
}

/* ---------- reveal ---------- */
.reveal-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.step-dots { display: flex; gap: 5px; }
.step-dots span { width: 9px; height: 9px; border-radius: 50%; background: #d9deea; }
.step-dots span.on { background: var(--ink); }
.step-dots span.now { background: var(--highlight); box-shadow: 0 0 0 2px var(--ink); }

.category-card { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; padding: 14px 16px; }
.category-card .letter { width: 68px; height: 68px; font-size: 2.9rem; grid-row: auto; }
.category-card .topic { font-family: var(--hand); font-weight: 400; font-size: 1.7rem; }

.answers { list-style: none; margin: 0 0 16px; padding: 0; }
.answer {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  margin-bottom: 8px;
  border-radius: 12px;
  background: #fff;
  box-shadow: var(--shadow);
  min-height: 60px;
}
.answer .who { font-size: .8rem; font-weight: 800; color: var(--ink-soft); }
.answer .what { font-family: var(--hand); font-size: 1.45rem; line-height: 1.1; word-break: break-word; }
.answer .what.none { color: var(--pencil); }
.answer.hidden-answer { background: repeating-linear-gradient(-45deg, #f1f3f9 0 10px, #e9ecf5 10px 20px); box-shadow: none; }
.answer.hidden-answer .what { color: var(--pencil); }
.answer.flip { animation: flip .45s ease-out; }
.answer.unique { background: var(--good-bg); }
.answer.match { background: var(--match-bg); }
.answer.wrong-letter, .answer.rejected { background: var(--bad-bg); }
.answer.wrong-letter .what, .answer.rejected .what { text-decoration: line-through; text-decoration-thickness: 2px; }
.chip {
  flex: none; margin-left: auto;
  font-weight: 800; font-size: .8rem;
  padding: 4px 10px; border-radius: 99px;
  white-space: nowrap;
}
.chip.unique { background: var(--good); color: #fff; }
.chip.match { background: var(--match); color: #fff; }
.chip.wrong-letter, .chip.rejected { background: var(--bad); color: #fff; }
.chip.blank { background: #e4e8f2; color: var(--ink-soft); }
.judge {
  flex: none;
  border: 2px solid #cfd6e8; background: #fff; color: var(--ink-soft);
  border-radius: 10px; font: inherit; font-size: .78rem; font-weight: 800;
  padding: 6px 8px; cursor: pointer;
}
.judge.on { border-color: var(--bad); color: var(--bad); }

@keyframes flip {
  0% { transform: perspective(600px) rotateX(-90deg); opacity: 0; }
  100% { transform: perspective(600px) rotateX(0); opacity: 1; }
}
@keyframes pop {
  0% { transform: scale(.9); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

/* ---------- leaderboard ---------- */
.board { list-style: none; margin: 0; padding: 0; }
.board li {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 4px;
  border-bottom: 1px dashed #dde2ef;
  font-weight: 700;
  position: relative;
}
.board li:last-child { border-bottom: 0; }
.board li.me { background: linear-gradient(90deg, rgba(255, 224, 102, .45), transparent); border-radius: 8px; }
.board .rank { width: 26px; text-align: center; font-family: var(--hand); font-size: 1.35rem; color: var(--ink-soft); flex: none; }
.board .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board .pts { font-weight: 800; font-size: 1.2rem; font-variant-numeric: tabular-nums; }
.board .gain {
  position: absolute; right: 44px;
  font-weight: 800; color: var(--good);
  animation: gain 1.2s ease-out forwards;
}
.board .gain.match { color: var(--match); }
@keyframes gain {
  0% { opacity: 0; transform: translateY(6px); }
  20% { opacity: 1; transform: translateY(0); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-10px); }
}

.host-bar {
  position: sticky; bottom: 0;
  margin: 16px -16px 0 -40px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom)) 40px;
  background: linear-gradient(transparent, var(--paper) 30%);
}
.waiting { text-align: center; font-family: var(--hand); font-size: 1.25rem; color: var(--ink-soft); padding: 8px 0; }

/* ---------- final ---------- */
.podium { display: flex; align-items: flex-end; justify-content: center; gap: 8px; margin: 18px 0 8px; }
.podium .step {
  flex: 1; max-width: 130px;
  text-align: center;
  animation: rise .7s cubic-bezier(.2, .9, .3, 1.2) backwards;
}
.podium .block {
  border-radius: 12px 12px 4px 4px;
  display: grid; place-items: center;
  font-family: var(--hand); font-size: 2.2rem;
  color: var(--ink);
  box-shadow: var(--shadow);
}
/* Position decides left/centre/right; rank decides height, so ties stand equally tall. */
.podium .r1 .block { height: 130px; background: var(--highlight); }
.podium .r2 .block { height: 95px; background: #dfe4ef; }
.podium .r3 .block { height: 70px; background: #f3d3b5; }
.podium .pos1 { order: 2; animation-delay: .5s; }
.podium .pos2 { order: 1; animation-delay: .25s; }
.podium .pos3 { order: 3; }
.podium .pname { font-weight: 800; margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.podium .ppts { font-size: .85rem; color: var(--ink-soft); font-weight: 700; }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
.winner { text-align: center; font-family: var(--hand); font-size: 2.2rem; line-height: 1.1; margin-top: 8px; }
.recap { width: 100%; border-collapse: collapse; font-size: .92rem; }
.recap td { padding: 6px 4px; border-bottom: 1px dashed #dde2ef; vertical-align: top; }
.recap td:first-child { font-family: var(--hand); font-size: 1.3rem; width: 28px; }
.recap .ans { font-family: var(--hand); font-size: 1.2rem; }
.recap .pts { text-align: right; font-weight: 800; white-space: nowrap; }

.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 30; overflow: hidden; }
.confetti i {
  position: absolute; top: -12px; width: 9px; height: 14px; border-radius: 2px;
  animation: fall linear forwards;
}
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

/* ---------- toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 40;
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  background: var(--ink); color: #fff;
  padding: 12px 18px; border-radius: 12px;
  font-weight: 700; box-shadow: var(--shadow);
  animation: pop .2s ease-out;
}

.conn-banner {
  position: fixed; top: 0; left: 0; right: 0; z-index: 50;
  background: var(--bad); color: #fff; text-align: center;
  font-weight: 700; font-size: .85rem; padding: 6px 12px;
  padding-top: max(6px, env(safe-area-inset-top));
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

@media (min-width: 700px) {
  #app { padding-top: 32px; }
  h1 { font-size: 3.2rem; }
}
