@font-face {
  font-family: Fredoka;
  src: url("fonts/Fredoka.ttf") format("truetype");
  font-weight: 300 700;
  font-display: swap;
}

:root {
  --paper: #FFF8E7;  --paper-dot: #F3E4C2;
  --ink: #23262F;    --ink-soft: #5B5F6B;  --line: #E6D9BC;  --white: #FFFFFF;
  --primary: #FF3E8E; --primary-ink: #FFFFFF;
  --danger: #D7263D;
  --choice-0: #FF5C5C; --choice-1: #FF9F1C; --choice-2: #8AC926;
  --choice-3: #1DBFB4; --choice-4: #3A86FF; --choice-5: #A163F5;
  --radius: 1rem; --radius-lg: 1.5rem;
  --shadow: 0 0.25rem 0 rgb(0 0 0 / .12);
  --choice: var(--choice-0);
}

html { font-size: min(calc(100vw / 120), calc(100vh / 67.5)); }

[hidden] { display: none !important; }

body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  font-family: Fredoka, "Segoe UI", system-ui, sans-serif;
  font-weight: 500;
  font-size: 1.5rem;
  color: var(--ink);
  background: radial-gradient(var(--paper-dot) 0.12rem, transparent 0.14rem) 0 0 / 1.5rem 1.5rem, var(--paper);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

h1, p { margin: 0; }
h1 { font-size: 3rem; font-weight: 700; line-height: 1.1; }
img { display: block; }

:focus-visible { outline: 0.3rem solid var(--ink); outline-offset: 0.2rem; }

/* ---------------------------------------------------------- buttons */

.btn, .key {
  --btn: var(--white);
  appearance: none;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 4.5rem;
  padding: 0 2rem;
  font: inherit;
  font-size: 2rem;
  font-weight: 600;
  color: var(--ink);
  background: var(--btn);
  border: 0.2rem solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: 0 0.4rem 0 color-mix(in oklab, var(--btn) 70%, black);
  cursor: pointer;
  transition: translate 0.06s, box-shadow 0.06s;
}

.btn:active, .key:active {
  translate: 0 0.25rem;
  box-shadow: 0 0.15rem 0 color-mix(in oklab, var(--btn) 70%, black);
}

.btn-primary, .key-done {
  --btn: var(--primary);
  color: var(--primary-ink);
  border-color: var(--primary);
}

.btn:disabled, .key:disabled {
  opacity: 0.45;
  box-shadow: none;
  cursor: default;
}

.btn:disabled:active, .key:disabled:active { translate: none; }

.btn-back {
  position: absolute;
  left: 2rem;
  bottom: 2rem;
  min-height: 4.5rem;
}

/* ---------------------------------------------------------- screens */

.screen {
  position: relative;
  box-sizing: border-box;
  height: 100vh;
  overflow: hidden;
}

/* ---------------------------------------------------------- setup */

#screen-setup {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
  padding-top: 2rem;
}

#screen-setup h1 { font-size: 2.5rem; }

#setup-title {
  box-sizing: border-box;
  width: 80rem;
  height: 5rem;
  padding: 0 1.5rem;
  font: inherit;
  font-size: 2rem;
  color: var(--ink);
  background: var(--white);
  border: 0.25rem solid var(--line);
  border-radius: var(--radius-lg);
  user-select: text;
  -webkit-user-select: text;
}

#setup-title::placeholder, .choice-label::placeholder { color: var(--ink-soft); opacity: 0.7; }

.palette {
  display: grid;
  grid-template-columns: repeat(14, 6rem);
  gap: 0.75rem;
}

.palette-item {
  appearance: none;
  position: relative;
  box-sizing: border-box;
  width: 6rem;
  height: 6rem;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--white);
  border: 0.25rem solid var(--line);
  border-radius: var(--radius);
  cursor: pointer;
}

.palette-item img { width: 4.5rem; height: 4.5rem; }
.palette-item:disabled { opacity: 0.35; cursor: default; }

.palette-item.is-selected {
  border: 0.4rem solid var(--primary);
  background: color-mix(in oklab, var(--choice) 18%, white);
}

.order-badge {
  position: absolute;
  top: -0.9rem;
  right: -0.9rem;
  width: 2.4rem;
  height: 2.4rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--white);
  background: var(--choice);
  border: 0.2rem solid var(--white);
  border-radius: 50%;
}

#setup-chosen {
  display: flex;
  justify-content: center;
  gap: 1.25rem;
  min-height: 4.5rem;
}

.chosen-item { display: flex; align-items: center; gap: 0.5rem; }
.chosen-item img { width: 3.5rem; height: 3.5rem; }

.choice-label {
  box-sizing: border-box;
  width: 12rem;
  height: 4.5rem;
  padding: 0 1rem;
  font: inherit;
  font-size: 1.5rem;
  color: var(--ink);
  background: color-mix(in oklab, var(--choice) 18%, white);
  border: 0.25rem solid var(--choice);
  border-radius: var(--radius);
  user-select: text;
  -webkit-user-select: text;
}

#setup-hint { font-size: 1.5rem; color: var(--ink-soft); }

#setup-start { width: 30rem; height: 6rem; font-size: 2.25rem; }

/* ---------------------------------------------------------- wall */

#screen-wall {
  display: grid;
  grid-template-rows: 9rem 1fr 12rem;
  padding: 1.25rem 2rem;
}

#wall-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 2rem;
  min-height: 0;
}

#wall-title {
  font-size: 3.25rem;
  text-wrap: balance;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

#tally { display: flex; gap: 1rem; margin-right: 6.5rem; flex: none; }

.tally {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.4rem 1.1rem 0.4rem 0.8rem;
  background: color-mix(in oklab, var(--choice) 18%, white);
  border: 0.2rem solid var(--choice);
  border-radius: var(--radius-lg);
}

.tally img { width: 2.5rem; height: 2.5rem; }
.tally-count { font-size: 2rem; font-weight: 700; min-width: 1.2ch; text-align: center; }

#board {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-auto-rows: calc(25% - 0.5625rem);
  gap: 0.75rem;
  align-content: start;
  overflow: hidden;
  min-height: 0;
}

#board-empty {
  position: absolute;
  top: 10.25rem;
  bottom: 13.25rem;
  left: 2rem;
  right: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  font-size: 2.5rem;
  font-weight: 600;
  color: var(--ink-soft);
  pointer-events: none;
}

#wall-bar { display: flex; align-items: center; justify-content: center; }

#my-turn { width: 30rem; height: 7rem; font-size: 3rem; font-weight: 700; }

#teacher-hotspot {
  position: absolute;
  top: 0;
  right: 0;
  width: 8rem;
  height: 8rem;
  background: transparent;
  touch-action: none;
}

.sticker {
  appearance: none;
  box-sizing: border-box;
  align-self: center;
  justify-self: center;
  width: 9.5rem;
  height: 9.5rem;
  padding: 0.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  font: inherit;
  color: var(--ink);
  background: color-mix(in oklab, var(--choice) 18%, white);
  border: 0.25rem solid var(--choice);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  rotate: var(--tilt, 0deg);
  cursor: pointer;
}

.sticker-emoji { width: 5.5rem; height: 5.5rem; }

.sticker-name {
  max-width: 100%;
  font-size: 1.25rem;
  font-weight: 700;
  line-height: 1.2;
  overflow: hidden;
  white-space: nowrap;
}

/* ---------------------------------------------------------- name */

#screen-name h1 { padding-top: 3rem; text-align: center; }

#name-error {
  position: absolute;
  top: 13rem;
  left: calc(50% - 35rem);
  box-sizing: border-box;
  width: 70rem;
  display: flex;
  align-items: center;
  gap: 2rem;
  padding: 1rem 1.5rem;
  font-size: 1.75rem;
  background: #FDE4E7;
  border: 0.25rem solid var(--danger);
  border-radius: var(--radius-lg);
}

#name-error p { flex: 1; }
#name-error-wall { flex: none; }

#name-field {
  position: absolute;
  top: 30rem;
  left: calc(50% - 30rem);
  box-sizing: border-box;
  width: 60rem;
  height: 6rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3rem;
  font-weight: 600;
  background: var(--white);
  border: 0.25rem solid var(--line);
  border-radius: var(--radius-lg);
}

#name-field:empty::before {
  content: attr(data-placeholder);
  color: var(--ink-soft);
  opacity: 0.6;
  font-weight: 500;
}

#keyboard-slot {
  position: absolute;
  top: 37.25rem;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
}

.keyboard { display: flex; flex-direction: column; align-items: center; gap: 0.75rem; }
.key-row { display: flex; gap: 0.75rem; }

.key {
  width: 5.5rem;
  height: 5.5rem;
  padding: 0;
  font-size: 2.25rem;
  border-radius: var(--radius);
}

.key-wide { width: 11rem; font-size: 2rem; }
.key-space { width: 22rem; font-size: 2rem; }
.key-done { width: 14rem; font-size: 2rem; }

/* ---------------------------------------------------------- pick */

#screen-pick {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 3rem;
}

#pick-choices {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: 2rem;
  padding-bottom: 6rem;
}

#pick-choices.is-many { max-width: 54rem; }

.pick-choice {
  appearance: none;
  box-sizing: border-box;
  width: 16rem;
  height: 18rem;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  font: inherit;
  color: var(--ink);
  background: color-mix(in oklab, var(--choice) 18%, white);
  border: 0.4rem solid var(--choice);
  border-radius: var(--radius-lg);
  box-shadow: 0 0.4rem 0 color-mix(in oklab, var(--choice) 70%, black);
  cursor: pointer;
}

.pick-choice:active { translate: 0 0.25rem; box-shadow: 0 0.15rem 0 color-mix(in oklab, var(--choice) 70%, black); }
.pick-choice img { width: 10rem; height: 10rem; }
.pick-label {
  max-width: 100%;
  min-height: 2.1rem;
  font-size: 1.75rem;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------------------------------------------------------- confirm */

#screen-confirm {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 3rem;
}

#confirm-emoji { width: 20rem; height: 20rem; margin-top: 2rem; }
#confirm-name { margin-top: 1rem; font-size: 3rem; font-weight: 700; }

#confirm-bar {
  margin-top: auto;
  padding-bottom: 2.5rem;
  display: flex;
  align-items: center;
  gap: 3rem;
}

#confirm-back { width: 20rem; height: 6rem; }
#confirm-ok { width: 26rem; height: 7rem; font-size: 2.5rem; }

/* ---------------------------------------------------------- celebrate */

#screen-celebrate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  background: rgb(255 248 231 / .85);
  pointer-events: auto;
}

#screen-celebrate[hidden] { display: none; }
#celebrate-emoji { width: 20rem; height: 20rem; }
#celebrate-caption { font-size: 3rem; font-weight: 700; }

#screen-celebrate.celebrate--in { animation: fade-in 150ms ease-out both; }
#screen-celebrate.celebrate--out { animation: fade-out var(--fade-out, 200ms) ease-in forwards; }
#celebrate-emoji.celebrate-bounce { animation: bounce-in 600ms ease-out both; }

#confetti {
  position: fixed;
  inset: 0;
  z-index: 60;
  overflow: hidden;
  pointer-events: none;
}

.confetti {
  position: absolute;
  top: -2rem;
  left: var(--x);
  width: .9rem;
  height: 1.4rem;
  background: var(--c);
  rotate: var(--r);
  animation: confetti-fall var(--d) linear forwards;
}

.sticker--pending { visibility: hidden; }
.sticker--land { animation: sticker-land 250ms ease-out; }
.sticker--reveal { animation: fade-in 250ms ease-out; }

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes fade-out { from { opacity: 1; } to { opacity: 0; } }

@keyframes bounce-in {
  0% { scale: .3; }
  60% { scale: 1.15; }
  80% { scale: .95; }
  100% { scale: 1; }
}

@keyframes confetti-fall {
  from { transform: translateY(-10vh); rotate: var(--r); }
  to { transform: translateY(110vh); rotate: calc(var(--r) + 540deg); }
}

@keyframes sticker-land {
  0% { scale: 1.15; }
  60% { scale: .95; }
  100% { scale: 1; }
}

/* ---------------------------------------------------------- reduced motion */

@media (prefers-reduced-motion: reduce) {
  .btn, .key { transition: none; }
  #celebrate-emoji.celebrate-bounce,
  .confetti,
  .sticker--land { animation: none; }
  .results-bar { transition: none; }
  .results-col.is-winner .results-emoji { animation: none; }
}

/* ------------------------------------------------------------ results */
#screen-results { display: grid; grid-template-rows: 6.5rem minmax(0, 1fr) 8rem; padding: 1.25rem 2rem; }
#screen-results.results--in { animation: fade-in 150ms ease-out both; }
#results-title { font-size: 2.75rem; text-align: center; text-wrap: balance; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
#results-chart {
  display: grid; grid-auto-flow: column; grid-template-columns: var(--cols, 1fr);
  grid-template-rows: minmax(14rem, 1fr) 7rem minmax(0, max-content);
  column-gap: 1.5rem; row-gap: 0.5rem; min-height: 0;
}
.results-col { grid-row: 1 / -1; min-width: 0; display: grid; grid-template-rows: subgrid; }
.results-track { box-sizing: border-box; height: auto; min-height: 0; padding-top: 3.5rem; display: flex; align-items: flex-end; justify-content: center; }
.results-bar {
  position: relative; box-sizing: border-box;
  width: min(100% - 2rem, 12rem); height: 0; min-height: 0.6rem;
  background: var(--choice); border: 0.25rem solid color-mix(in oklab, var(--choice) 70%, black);
  border-radius: var(--radius) var(--radius) 0.3rem 0.3rem;
  transition: height 500ms cubic-bezier(.2, .8, .2, 1);
}
.results-col.is-grown .results-bar { height: calc(var(--ratio, 0) * 100%); }
.results-count { position: absolute; left: 0; right: 0; bottom: calc(100% + 0.35rem); text-align: center; font-size: 2.5rem; font-weight: 700; line-height: 1; }
.results-head { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0.25rem; min-width: 0; }
.results-emoji { width: 4.5rem; height: 4.5rem; }
.results-label { max-width: 100%; font-size: 1.25rem; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.results-names { display: flex; flex-wrap: wrap; justify-content: center; align-content: flex-start; gap: 0.35rem; overflow: hidden; min-height: 0; font-size: 1.5rem; }
.results-name {
  box-sizing: border-box; max-width: 100%; padding: 0.1rem 0.45rem; line-height: 1.2; font-weight: 600;
  white-space: nowrap; overflow: hidden;
  background: color-mix(in oklab, var(--choice) 18%, white); border: 0.15rem solid var(--choice); border-radius: 0.6rem;
}
.results-col.is-winner .results-bar { box-shadow: 0 0 0 0.4rem var(--white), 0 0 0 0.9rem var(--choice); }
.results-col.is-winner .results-count::before { content: "★ "; color: var(--primary); }
.results-col.is-winner .results-emoji { animation: winner-bounce 600ms ease-out 2; }
#results-actions { display: flex; align-items: center; justify-content: center; gap: 2rem; }
#results-actions .btn { height: 6rem; font-size: 2.25rem; padding: 0 3rem; }
@keyframes winner-bounce { 0%, 100% { scale: 1; } 50% { scale: 1.3; } }

/* ---------------------------------------------------------- teacher panel */

.btn-danger {
  --btn: var(--danger);
  color: var(--white);
  border-color: var(--danger);
}

.teacher-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgb(35 38 47 / .55);
}

.teacher-backdrop[hidden] { display: none; }

.teacher-card {
  position: relative;
  box-sizing: border-box;
  width: 60rem;
  max-height: 60rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  padding: 2rem;
  background: var(--white);
  border-radius: var(--radius-lg);
}

.teacher-card h1 { margin: 0; font-size: 3rem; }

.teacher-body {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  min-height: 0;
  overflow: auto;
  padding-bottom: 0.5rem;
}

#teacher-sound { align-self: flex-start; }
#teacher-results { align-self: flex-start; }

#teacher-votes {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.teacher-vote {
  display: flex;
  align-items: center;
  gap: 1rem;
  min-height: 4.5rem;
  font-size: 1.5rem;
  border-bottom: 0.15rem solid var(--line);
}

.teacher-emoji { width: 2rem; height: 2rem; }
.teacher-name { flex: 1; font-weight: 600; }
.teacher-vote .btn { min-height: 4.5rem; font-size: 1.5rem; padding: 0 1.5rem; }
.teacher-empty { font-size: 1.5rem; color: var(--ink-soft); }

.teacher-actions { display: flex; gap: 1.25rem; }

.teacher-footer { margin: 0; font-size: 1rem; color: var(--ink-soft); }

.teacher-confirm-backdrop {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgb(35 38 47 / .55);
  border-radius: var(--radius-lg);
}

.teacher-confirm {
  box-sizing: border-box;
  width: 48rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  padding: 2rem;
  background: var(--white);
  border: 0.2rem solid var(--line);
  border-radius: var(--radius-lg);
}

.teacher-confirm p { margin: 0; font-size: 2rem; font-weight: 600; }
