/* Instant Lesson: a light, welcoming start page and a dark, call-style lesson room.
   Sections: tokens, base, buttons, start page, cards, lesson room (stage, tutor tile,
   self-view, shared screen, pictures on the shared screen, celebration, captions,
   controls, debug), tutor avatar, motion, wider screens. */

/* ---------- tokens ---------- */

:root {
  /* palette */
  --sun-50: #fffaf0;
  --sun-100: #fff1d6;
  --sun-200: #ffe3a8;
  --sun-400: #ffc940;
  --sun-500: #ffae1f;
  --coral-300: #ff9d8a;
  --coral-600: #e4572e;
  --cocoa-800: #4a2a18;
  --indigo-500: #4f46e5;
  --indigo-600: #4338ca;
  --indigo-800: #2f2a8f;
  --ink-900: #1c2140;
  --ink-700: #3d4468;
  --ink-500: #5c6485;
  --line-200: #e9e1d0;
  --line-400: #cfc6b2;
  --paper: #ffffff;
  --mint-400: #4fd6a8;
  --sky-400: #6cb6ff;
  --red-500: #e5484d;
  --red-600: #c93a40;
  --rose-50: #ffeceb;
  --rose-300: #f3b3b0;
  --rose-800: #96181f;
  --night-950: #0b1024;
  --night-900: #121935;
  --night-800: #1b2447;
  --night-700: #27325e;
  --night-600: #36437a;
  --cloud-100: #eef1ff;
  --cloud-300: #b9c1e6;
  --chip: rgba(11, 16, 36, 0.74);
  --screen: #fffdf8; /* the surface of the tutor's shared screen */

  /* roles (light theme) */
  --bg: var(--sun-50);
  --text: var(--ink-900);
  --text-soft: var(--ink-500);
  --focus: var(--indigo-500);

  --radius-l: 28px;
  --radius-m: 18px;
  --radius-s: 12px;
  --shadow-card: 0 1px 0 rgba(28, 33, 64, 0.04), 0 22px 44px -22px rgba(28, 33, 64, 0.35);
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}

body[data-theme="room"] {
  --bg: var(--night-950);
  --text: var(--cloud-100);
  --text-soft: var(--cloud-300);
  --focus: var(--sun-400);
}

/* ---------- base ---------- */

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: var(--font);
  font-size: 1.125rem;
  line-height: 1.5;
  color: var(--text);
  background-color: var(--bg);
  background-image:
    radial-gradient(60rem 30rem at 90% -10%, var(--sun-200), transparent 62%),
    radial-gradient(44rem 28rem at -10% 105%, #dde4ff, transparent 60%);
  background-attachment: fixed;
}

body[data-theme="room"] {
  background-image: radial-gradient(80rem 40rem at 50% -25%, var(--night-700), transparent 65%);
}

h1,
p {
  margin: 0;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}

/* Headings only receive focus from script when the view changes. */
h1[tabindex="-1"]:focus {
  outline: none;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.noscript {
  margin: 2rem auto;
  max-width: 32rem;
  padding: 1rem;
  text-align: center;
}

.icon {
  width: 1.5rem;
  height: 1.5rem;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon--solid {
  fill: currentColor;
  stroke: none;
}

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.6rem;
  min-height: 3rem;
  padding: 0.7rem 1.4rem;
  border: 0;
  border-radius: 999px;
  font-weight: 800;
  line-height: 1.2;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background-color 0.12s ease;
}

.btn--primary {
  background: var(--indigo-500);
  color: #fff;
  box-shadow: 0 6px 0 var(--indigo-800);
}

.btn--primary:hover {
  background: var(--indigo-600);
}

.btn--primary:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 var(--indigo-800);
}

.btn--big {
  width: 100%;
  min-height: 4rem;
  font-size: 1.4rem;
}

.btn--ghost {
  width: 100%;
  background: transparent;
  color: var(--ink-700);
  border: 2px solid var(--line-400);
}

.btn--ghost:hover {
  background: var(--sun-50);
}

.btn--small {
  min-height: 2.5rem;
  padding: 0.4rem 0.9rem;
  font-size: 0.9rem;
  background: var(--night-600);
  color: #fff;
}

.btn:disabled {
  opacity: 0.75;
  cursor: progress;
}

/* ---------- start page ---------- */

.start {
  padding: clamp(1.25rem, 5vw, 3rem) 1rem 3rem;
}

.start__wrap {
  max-width: 40rem;
  margin: 0 auto;
}

.start__header {
  margin-bottom: 1.5rem;
  text-align: center;
}

.avatar--hello {
  width: 9.5rem; /* Bilby is mostly ears: the face is about 60% of this */
  margin: 0 auto 0.25rem;
}

.start h1 {
  font-size: clamp(2.4rem, 8vw, 3.4rem);
  font-weight: 900;
  line-height: 1.1;
  letter-spacing: -0.02em;
}

.start__subtitle {
  max-width: 30rem;
  margin: 0.6rem auto 0;
  font-size: 1.2rem;
  color: var(--ink-700);
}

.badge {
  display: inline-block;
  margin-top: 0.9rem;
  padding: 0.3rem 0.85rem;
  border-radius: 999px;
  background: var(--sun-200);
  color: var(--cocoa-800);
  font-size: 0.9rem;
  font-weight: 800;
}

.panel {
  display: grid;
  gap: 1.25rem;
  padding: clamp(1.25rem, 5vw, 2rem);
  border: 1px solid var(--line-200);
  border-radius: var(--radius-l);
  background: var(--paper);
  box-shadow: var(--shadow-card);
}

.field label {
  display: block;
  margin-bottom: 0.5rem;
  font-size: 1.2rem;
  font-weight: 800;
  line-height: 1.3;
}

.field textarea {
  display: block;
  width: 100%;
  min-height: 3.75rem;
  padding: 0.85rem 1rem;
  border: 2px solid var(--line-400);
  border-radius: var(--radius-m);
  background: var(--sun-50);
  font-size: 1.15rem;
  line-height: 1.4;
  resize: vertical;
}

.field textarea::placeholder {
  color: var(--ink-500);
  opacity: 0.85;
}

.field textarea:focus-visible {
  border-color: var(--indigo-500);
  outline-offset: 1px;
}

.form-error {
  padding: 0.85rem 1rem;
  border: 2px solid var(--rose-300);
  border-radius: var(--radius-m);
  background: var(--rose-50);
  color: var(--rose-800);
  font-weight: 700;
}

.notice {
  font-size: 1rem;
  color: var(--ink-700);
}

.notice a {
  color: var(--indigo-600);
  font-weight: 700;
}

/* ---------- cards (pre-join, expired, mic help, end, safe stop, error) ---------- */

.card-view {
  display: grid;
  place-items: center;
  min-height: 100vh;
  min-height: 100dvh;
  padding: 1.25rem;
}

.card {
  --focus: var(--indigo-500);
  display: grid;
  justify-items: center;
  gap: 1rem;
  width: min(100%, 32rem);
  padding: clamp(1.5rem, 6vw, 2.5rem);
  border-radius: 32px;
  background: var(--paper);
  color: var(--ink-900);
  text-align: center;
  box-shadow: var(--shadow-card);
}

.card h1 {
  font-size: clamp(1.6rem, 6vw, 2.1rem);
  font-weight: 900;
  line-height: 1.2;
}

.card__body {
  font-size: 1.2rem;
  color: var(--ink-700);
}

.card__small {
  font-size: 0.9rem;
  color: var(--ink-500);
}

.card__actions {
  display: grid;
  gap: 0.9rem;
  width: 100%;
  margin-top: 0.5rem;
}

.card__actions:empty {
  display: none;
}

.avatar--card {
  width: 10rem;
}

/* A soft "pause" mark for the safe-stop card: calm, not a warning sign. */
.card__art--pause {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.7rem;
  width: 5.5rem;
  height: 5.5rem;
  border-radius: 50%;
  background: #e6ebff;
}

.card__art--pause::before,
.card__art--pause::after {
  content: "";
  width: 0.8rem;
  height: 2rem;
  border-radius: 0.4rem;
  background: #8e9be0;
}

/* ---------- lesson room ---------- */

.room {
  display: flex;
  flex-direction: column;
  overflow-x: hidden; /* celebration stars may fly past the edge; never scroll sideways */
  overflow-x: clip;
  gap: 0.75rem;
  max-width: 82rem;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: 0.75rem;
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
}

.room__top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.15rem 0.4rem;
}

.room__brand {
  font-size: 1.05rem;
  font-weight: 800;
  color: var(--text-soft);
}

.timer {
  padding: 0.25rem 0.85rem;
  border-radius: 999px;
  background: var(--night-800);
  font-size: 1.05rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

/* The stage shows the tutor and, while the tutor is "presenting" (class is-presenting,
   set by app.js), the tutor's shared screen with the tutor in a small tile under or
   beside it. Both parts are positioned inside the stage, so its size never depends on
   which layout is showing: the captions and controls below do not move. */
.stage {
  --stage-gap: 0.75rem;
  --people-h: 6.25rem; /* presenting, stacked: height of the tutor's small tile */
  --people-w: clamp(16.5rem, 23vw, 19rem); /* presenting, side by side: width of the tutor's tile */
  --pip-w: 4.25rem; /* presenting: width of the self-view thumbnail */
  --swap: 0.45s cubic-bezier(0.3, 0.1, 0.2, 1); /* one timing for every part of the switch */
  position: relative;
  flex: 1 1 auto;
  min-height: 21rem;
}

.stage__main {
  position: absolute;
  z-index: 1;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-width: 0;
  --self-width: clamp(9.5rem, 20vw, 13.5rem); /* picture-in-picture size on wide tiles */
  transition: top var(--swap), left var(--swap), max-height var(--swap);
}

.stage.is-presenting .stage__main {
  top: calc(100% - var(--people-h));
}

.tile {
  overflow: hidden;
  border-radius: var(--radius-l);
}

/* tutor tile */

.tutor {
  --level: 0;
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  border: 3px solid transparent;
  background: radial-gradient(120% 95% at 50% 0%, var(--night-600), var(--night-800) 72%);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.tutor.is-talking {
  border-color: var(--sun-400);
  box-shadow: 0 0 0 4px rgba(255, 201, 64, 0.16);
}

.tutor__figure {
  position: absolute;
  inset: 3.6rem 1rem; /* clear of the chips in the tile's corners */
  max-width: 24rem;
  max-height: 24rem;
  margin: auto;
  transition: top var(--swap), right var(--swap), bottom var(--swap), left var(--swap);
}

/* Presenting, stacked: the tile is a short band. The tutor sits at its left end,
   the two chips move next to it and the self-view becomes a thumbnail at the right. */
.stage.is-presenting .tutor__figure {
  inset: 0.3rem calc(100% - var(--people-h) + 0.15rem) 0.3rem 0.45rem;
}

/* The SVG scales itself to fit this box, whatever shape the tile is. */
.avatar--tutor,
.avatar--tutor .avatar__svg {
  width: 100%;
  height: 100%;
}

.avatar--tutor {
  transition: opacity 0.4s ease, filter 0.4s ease;
}

.tutor[data-state="connecting"] .avatar--tutor {
  opacity: 0.55;
  filter: saturate(0.5);
}

.status {
  position: absolute;
  z-index: 1; /* above the tutor's halo */
  top: 0.85rem;
  left: 0.85rem;
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  max-width: calc(100% - 1.7rem);
  padding: 0.45rem 0.95rem;
  border-radius: 999px;
  background: var(--chip);
  font-size: 1.05rem;
  font-weight: 800;
  transition: top var(--swap), left var(--swap), padding var(--swap), font-size var(--swap);
}

.status__text,
.tile__who {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stage.is-presenting .status {
  top: 0.65rem;
  left: calc(var(--people-h) + 0.1rem);
  gap: 0.4rem;
  max-width: calc(100% - var(--people-h) - var(--pip-w) - 1.05rem);
  padding: 0.28rem 0.7rem;
  font-size: 0.82rem;
}

.status__dot {
  flex: none;
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 50%;
  background: var(--cloud-300);
}

.status[data-state="connecting"] .status__dot {
  animation: pulse 1.2s ease-in-out infinite;
}

.status[data-state="talking"] .status__dot {
  background: var(--sun-400);
}

.status[data-state="listening"] .status__dot {
  background: var(--mint-400);
}

.status[data-state="thinking"] .status__dot {
  background: var(--sky-400);
  animation: pulse 0.9s ease-in-out infinite;
}

.status[data-state="muted"] .status__dot {
  background: var(--coral-300);
}

/* live microphone meter: shown while listening so the child sees they are heard */
.meter {
  display: none;
  align-items: flex-end;
  gap: 3px;
  height: 1.15rem;
}

.status[data-state="listening"] .meter {
  display: inline-flex;
}

.meter i {
  width: 4px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.26);
  transition: background-color 0.08s linear;
}

.meter i:nth-child(1) { height: 35%; }
.meter i:nth-child(2) { height: 50%; }
.meter i:nth-child(3) { height: 65%; }
.meter i:nth-child(4) { height: 82%; }
.meter i:nth-child(5) { height: 100%; }

.meter i.is-on {
  background: var(--mint-400);
}

.tile__name {
  position: absolute;
  z-index: 1;
  bottom: 0.85rem;
  left: 0.85rem;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  max-width: calc(100% - 1.7rem);
  padding: 0.4rem 0.85rem;
  border-radius: 999px;
  background: var(--chip);
  font-size: 1.1rem;
  font-weight: 800;
  transition: bottom var(--swap), left var(--swap), padding var(--swap), font-size var(--swap);
}

.stage.is-presenting .tile__name {
  bottom: 0.65rem;
  left: calc(var(--people-h) + 0.1rem);
  max-width: calc(100% - var(--people-h) - var(--pip-w) - 1.05rem);
  padding: 0.25rem 0.65rem;
  font-size: 0.88rem;
}

.tile__tag {
  flex: none;
  padding: 0.1rem 0.45rem;
  border-radius: 6px;
  background: var(--sun-400);
  color: var(--night-950);
  font-size: 0.72rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

/* self-view: a compact strip on phones, a picture-in-picture tile on wider screens */

.self {
  display: grid;
  flex: none;
  grid-template-columns: 5.5rem minmax(0, 1fr);
  align-items: center;
  column-gap: 0.75rem;
  padding: 0.45rem;
  border: 2px solid var(--night-700);
  border-radius: var(--radius-m);
  background: var(--night-900);
}

.self__frame {
  display: grid;
  grid-row: span 2;
  place-items: center;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  border-radius: var(--radius-s);
  background: var(--night-700);
}

.self__video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(-1); /* mirror, like every video call */
}

.self__placeholder .icon {
  display: block;
  width: 2.2rem;
  height: 2.2rem;
  color: var(--cloud-300);
}

.self__label {
  font-size: 0.85rem;
  line-height: 1.3;
  color: var(--text-soft);
}

.self__note {
  font-size: 0.85rem;
  line-height: 1.3;
  color: var(--sun-200);
}

/* Presenting on a phone: the strip becomes a thumbnail at the right end of the tutor's band.
   It changes place in one step, so both ways it fades in instead of jumping. */
.self {
  animation: self-back 0.35s ease backwards;
}

.stage.is-presenting .self {
  position: absolute;
  z-index: 1;
  right: 0.55rem;
  bottom: calc((var(--people-h) - var(--pip-w) * 0.75) / 2 - 2px);
  display: block;
  width: var(--pip-w);
  padding: 0;
  box-shadow: 0 8px 18px -8px rgba(0, 0, 0, 0.7);
  animation: self-away 0.4s ease 0.1s backwards;
}

.stage.is-presenting .self__frame {
  border-radius: 0;
}

.stage.is-presenting .self__label,
.stage.is-presenting .self__note {
  display: none;
}

/* shared screen: what the tutor is "presenting" */

.share {
  position: absolute;
  inset: 0 0 calc(var(--people-h) + var(--stage-gap)) 0; /* stacked: above the tutor's band */
  visibility: hidden;
  opacity: 0;
  clip-path: inset(0 0 100% 0 round var(--radius-l)); /* closed: wiped away towards its top edge */
  transition: clip-path var(--swap), opacity 0.2s ease 0.25s, visibility 0s linear 0.45s;
}

.stage.is-presenting .share {
  visibility: visible;
  opacity: 1;
  clip-path: inset(0 0 0 0 round var(--radius-l));
  transition: clip-path var(--swap), opacity 0.2s ease, visibility 0s;
}

.share__window {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  border: 3px solid var(--mint-400);
  border-radius: var(--radius-l);
  background: var(--night-800);
}

.share__bar {
  display: flex;
  flex: none;
  align-items: center;
  gap: 0.55rem;
  min-width: 0;
  padding: 0.4rem 0.85rem 0.45rem;
  font-size: 0.95rem;
  font-weight: 800;
}

.share__badge {
  display: grid;
  flex: none;
  place-items: center;
  width: 1.65rem;
  height: 1.65rem;
  border-radius: 8px;
  background: var(--mint-400);
  color: var(--night-950);
}

.share__badge .icon {
  width: 1.15rem;
  height: 1.15rem;
  stroke-width: 2.2;
}

.share__title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* The screen itself: light, like paper, so pictures read well inside the dark room. */
.share__body {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  background-color: var(--screen);
  background-image: radial-gradient(rgba(28, 33, 64, 0.08) 1.5px, transparent 1.6px);
  background-position: center;
  background-size: 1.5rem 1.5rem;
  color: var(--ink-900);
  container: share / size;
}

/* ---------- pictures on the shared screen (built by visuals.js) ---------- */

/* Every size below is a share of the screen: --cw and --ch are 1% of its width and height.
   --wait lets the screen slide open before the picture comes to life. Entrance animations
   only describe where a part comes from, so with motion switched off everything is simply there. */
.vis {
  --cw: 0.6vw; /* rough stand-ins for browsers without container units */
  --ch: 0.4vh;
  --wait: 0.35s;
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: calc(var(--ch) * 4);
  padding: calc(var(--ch) * 5) calc(var(--cw) * 4);
  font-weight: 900;
  line-height: 1.05;
  text-align: center;
  animation: vis-fade 0.25s ease backwards;
}

.vis.has-title {
  --ch: 0.34vh; /* leave room for the caption */
}

@supports (width: 1cqw) {
  .vis {
    --cw: 1cqw;
    --ch: 1cqh;
  }

  .vis.has-title {
    --ch: 0.84cqh;
  }
}

.vis__title {
  flex: none;
  max-width: 100%;
  padding: 0.25em 0.9em;
  border-radius: 999px;
  background: rgba(79, 70, 229, 0.1);
  color: var(--indigo-800);
  font-size: clamp(0.9rem, calc(var(--ch) * 7), 1.5rem);
  font-weight: 800;
  line-height: 1.2;
  overflow-wrap: anywhere;
  animation: vis-rise 0.4s ease backwards;
  animation-delay: calc(var(--wait) + 0.2s);
}

.vis-emoji {
  font-family: "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", sans-serif;
  font-weight: 400;
}

/* fraction: a pizza and the fraction beside it (under it on an upright screen) */

.vis-fraction {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: calc(var(--cw) * 6);
}

.vis-pizza {
  flex: none;
  width: min(calc(var(--ch) * 86), calc(var(--cw) * 46));
  aspect-ratio: 1;
  overflow: visible;
}

.vis-pizza__plate {
  fill: #fff;
  stroke: rgba(28, 33, 64, 0.1);
  stroke-width: 1.5;
}

.vis-slice {
  transform-origin: 100px 100px;
  animation: vis-pop 0.5s cubic-bezier(0.2, 1.3, 0.4, 1) backwards;
  animation-delay: calc(var(--wait) + var(--i) * 80ms);
}

.vis-slice.is-off {
  opacity: 0.22;
}

.vis-slice__crust {
  fill: #e2a657;
}

.vis-slice__sauce {
  fill: #e4572e;
}

.vis-slice__cheese {
  fill: #ffd257;
}

.vis-slice__topping {
  fill: #c83a26;
  stroke: #a52c1b;
  stroke-width: 1.5;
}

.vis-pizza__cut {
  stroke: #fff;
  stroke-width: 3;
  stroke-linecap: round;
}

.vis-frac {
  display: grid;
  justify-items: center;
  font-size: min(calc(var(--ch) * 30), calc(var(--cw) * 15));
  line-height: 0.95;
  animation: vis-rise 0.4s ease backwards;
  animation-delay: calc(var(--wait) + 0.3s);
}

.vis-frac__num {
  color: var(--coral-600);
}

.vis-frac__bar {
  width: 100%;
  min-width: 1.1em;
  height: 0.09em;
  margin: 0.07em 0 0.09em;
  border-radius: 999px;
  background: var(--ink-900);
}

/* count: the things in rows of five, and the number counting up */

.vis-count {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: calc(var(--cw) * 6);
}

.vis-count__grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), auto);
  gap: 0.06em 0.14em;
  font-size: min(calc(var(--ch) * 64 / var(--rows)), calc(var(--cw) * 50 / var(--cols)), 9rem);
  line-height: 1.15;
}

.vis-count__item {
  animation: vis-pop 0.4s cubic-bezier(0.2, 1.5, 0.4, 1) backwards;
  animation-delay: calc(var(--wait) + var(--i) * var(--step));
}

.vis-count__num {
  min-width: calc(var(--digits) * 0.62em);
  color: var(--indigo-500);
  font-size: min(calc(var(--ch) * 46), calc(var(--cw) * 22));
  font-variant-numeric: tabular-nums;
  animation: vis-pop 0.4s cubic-bezier(0.2, 1.5, 0.4, 1) backwards;
  animation-delay: var(--wait);
  transition: color 0.2s ease, transform 0.2s cubic-bezier(0.2, 1.6, 0.4, 1);
}

.vis-count__num.is-counting {
  color: var(--ink-500);
  transform: scale(0.84);
}

/* math: one sum, as large as fits */

.vis-math {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: min(calc(var(--ch) * 46), calc(var(--cw) * 88 / var(--ems)), 12rem);
  font-variant-numeric: lining-nums;
  white-space: nowrap;
}

.vis-math > span {
  animation: vis-rise 0.4s ease backwards;
  animation-delay: calc(var(--wait) + var(--i) * 110ms);
}

.vis-math__op,
.vis-math__eq {
  margin: 0 0.24em;
  color: var(--indigo-500);
}

.vis-math__eq {
  color: var(--coral-600);
}

.vis-math__paren {
  color: var(--ink-500);
  font-weight: 700;
}

.vis-math__ask {
  display: inline-grid;
  place-items: center;
  min-width: 1.15em;
  height: 1.3em;
  margin: 0 0.05em;
  border: 0.06em dashed var(--sun-500);
  border-radius: 0.2em;
  background: var(--sun-100);
  color: var(--cocoa-800);
}

/* word: the word very large, its picture above */

.vis-word {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: calc(var(--ch) * 2);
  max-width: 100%;
}

.vis-word__pic {
  font-size: min(calc(var(--ch) * 36), calc(var(--cw) * 34));
  line-height: 1.15;
  animation: vis-pop 0.5s cubic-bezier(0.2, 1.5, 0.4, 1) backwards;
  animation-delay: var(--wait);
}

.vis-word__text {
  max-width: calc(var(--ems) * 1em + 0.5em);
  font-size: min(calc(var(--ch) * 30 / var(--lines)), calc(var(--cw) * 86 / var(--ems)), 10rem);
  letter-spacing: 0.01em;
  text-wrap: balance;
}

.vis-word--plain .vis-word__text {
  font-size: min(calc(var(--ch) * 54 / var(--lines)), calc(var(--cw) * 86 / var(--ems)), 12rem);
}

.vis-word__chunk {
  display: inline-block;
  white-space: nowrap;
}

.vis-word__letter {
  display: inline-block;
  animation: vis-rise 0.35s ease backwards;
  animation-delay: calc(var(--wait) + 0.15s + var(--i) * 55ms);
}

/* scene, sequence: big pictures with a small label under each */

.vis-scene,
.vis-seq {
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

.vis-item {
  display: grid;
  justify-items: center;
  min-width: 0;
}

.vis-item__pic {
  line-height: 1.2;
}

.vis-item__label,
.vis-compare__label {
  margin-top: 0.4em;
  color: var(--ink-700);
  font-size: clamp(0.85rem, calc(var(--ch) * 6.5), 1.6rem);
  font-weight: 800;
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.vis-scene {
  gap: calc(var(--cw) * 6);
  font-size: min(calc(var(--ch) * 52), calc(var(--cw) * 62 / var(--count)));
}

.vis-scene .vis-item__label {
  max-width: 9em;
}

/* Each thing pops in with its label, then floats gently over its shadow. */
.vis-scene .vis-item {
  animation: vis-pop 0.5s cubic-bezier(0.2, 1.5, 0.4, 1) backwards;
  animation-delay: calc(var(--wait) + var(--i) * 140ms);
}

.vis-scene .vis-item__pic {
  animation: vis-bob 3.2s ease-in-out infinite;
  animation-delay: calc(var(--wait) + 0.6s + var(--i) * 0.45s);
}

.vis-item__shadow {
  width: 0.6em;
  height: 0.07em;
  border-radius: 50%;
  background: rgba(28, 33, 64, 0.14);
  animation: vis-shadow 3.2s ease-in-out infinite;
  animation-delay: calc(var(--wait) + 0.6s + var(--i) * 0.45s);
}

.vis-seq {
  --pic: min(calc(var(--ch) * 44), calc(var(--cw) * 52 / var(--count)));
  gap: 0.1em;
  font-size: var(--pic);
}

.vis-seq .vis-item {
  flex: 0 1 1.25em;
  animation: vis-pop 0.45s cubic-bezier(0.2, 1.4, 0.4, 1) backwards;
  animation-delay: calc(var(--wait) + var(--i) * 0.5s);
}

/* A label may reach half-way under the arrows on each side before it has to wrap. */
.vis-seq .vis-item__label {
  width: max-content;
  max-width: calc(var(--pic) * 1.85);
  font-size: clamp(0.72rem, min(calc(var(--ch) * 6), calc(var(--pic) * 0.3)), 1.45rem);
  overflow-wrap: break-word;
}

.vis-seq__arrow {
  flex: none;
  width: 0.42em;
  margin-top: 0.46em;
  fill: none;
  stroke: var(--indigo-500);
  stroke-width: 6;
  stroke-linecap: round;
  stroke-linejoin: round;
  animation: vis-slide 0.35s ease backwards;
  animation-delay: calc(var(--wait) + var(--i) * 0.5s - 0.2s);
}

/* compare: a big one and a small one standing on the same line */

.vis-compare {
  display: grid;
  grid-template-columns: auto auto;
  align-items: end;
  justify-content: center;
  justify-items: center;
  column-gap: calc(var(--cw) * 7);
  font-size: min(calc(var(--ch) * 54), calc(var(--cw) * 38));
}

.vis-compare__pic {
  line-height: 1.12;
  transform-origin: 50% 100%;
  animation: vis-pop 0.5s cubic-bezier(0.2, 1.5, 0.4, 1) backwards;
  animation-delay: var(--wait);
}

.vis-compare__pic--small {
  font-size: 0.42em;
  animation-delay: calc(var(--wait) + 0.45s);
}

.vis-compare__ground {
  grid-column: 1 / -1;
  width: 100%;
  height: 4px;
  border-radius: 999px;
  background: rgba(28, 33, 64, 0.14);
}

.vis-compare__label {
  align-self: start;
  max-width: 9em;
  animation: vis-fade 0.4s ease backwards;
  animation-delay: calc(var(--wait) + 0.2s);
}

.vis-compare__label--small {
  animation-delay: calc(var(--wait) + 0.65s);
}

.vis-compare__label:empty {
  display: none;
}

/* shape: the outline is drawn, then the corners are counted */

.vis-shape-wrap {
  display: flex;
}

.vis-shape {
  width: min(calc(var(--ch) * 88), calc(var(--cw) * 60));
  aspect-ratio: 1;
  overflow: visible;
}

.vis-shape__fill {
  fill: #e3e6ff;
  animation: vis-fade 0.5s ease backwards;
  animation-delay: calc(var(--wait) + 0.5s);
}

.vis-shape__line {
  fill: none;
  stroke: var(--indigo-500);
  stroke-width: 6;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 1; /* the path's length is declared as 1, so this is one whole lap */
  animation: vis-draw 0.9s ease-in-out backwards;
  animation-delay: var(--wait);
}

.vis-shape__corner {
  transform-box: fill-box;
  transform-origin: center;
  animation: vis-pop 0.4s cubic-bezier(0.2, 1.5, 0.4, 1) backwards;
  animation-delay: calc(var(--wait) + 0.7s + var(--i) * 130ms);
}

.vis-shape__dot {
  fill: var(--coral-600);
  stroke: #fff;
  stroke-width: 3;
}

.vis-shape__count {
  fill: #fff;
  font-size: 12px;
  font-weight: 900;
  dominant-baseline: central;
  text-anchor: middle;
}

/* An upright screen (a phone held upright): stack what sat side by side. */
@container share (orientation: portrait) {
  .vis-fraction,
  .vis-count {
    flex-direction: column;
    gap: calc(var(--ch) * 3);
  }

  .vis-pizza {
    width: min(calc(var(--ch) * 54), calc(var(--cw) * 74));
  }

  .vis-frac {
    font-size: min(calc(var(--ch) * 14), calc(var(--cw) * 18));
  }

  .vis-count__grid {
    font-size: min(calc(var(--ch) * 50 / var(--rows)), calc(var(--cw) * 72 / var(--cols)), 9rem);
  }

  .vis-count__num {
    font-size: min(calc(var(--ch) * 22), calc(var(--cw) * 30));
  }

  .vis-shape {
    width: min(calc(var(--ch) * 78), calc(var(--cw) * 84));
  }
}

/* ---------- celebration: stars around the tutor after a right answer ---------- */

/* A zero-size box that app.js puts at the middle of the tutor's face. */
.sparkles {
  position: absolute;
  z-index: 3;
  top: var(--y, 50%);
  left: var(--x, 50%);
  width: 0;
  height: 0;
  pointer-events: none;
}

/* One four-pointed star. visuals.js gives each its own path (--x0/--y0 to --x1/--y1), size, spin and delay. */
.sparkle {
  position: absolute;
  top: calc(var(--size) / -2);
  left: calc(var(--size) / -2);
  width: var(--size);
  height: var(--size);
  background: var(--sun-400);
  clip-path: polygon(50% 0, 62% 38%, 100% 50%, 62% 62%, 50% 100%, 38% 62%, 0 50%, 38% 38%);
  animation: sparkle-fly 1.1s cubic-bezier(0.15, 0.7, 0.3, 1) var(--delay) both;
}

.sparkle--white {
  background: #fff;
}

.sparkle--coral {
  background: var(--coral-300);
}

.sparkle--mint {
  background: var(--mint-400);
}

.sparkle--sky {
  background: var(--sky-400);
}

.tutor.is-celebrating {
  animation: tutor-cheer 1.2s ease;
}

.tutor.is-celebrating .tutor__figure {
  animation: tutor-hop 0.75s cubic-bezier(0.3, 0.6, 0.4, 1);
}

/* captions */

.captions {
  display: grid;
  flex: none;
  align-content: center;
  gap: 0.15rem;
  min-height: 5.4rem;
  padding: 0.7rem 1.1rem;
  border-radius: 20px;
  background: var(--night-800);
  text-align: center;
}

.captions__prev {
  overflow: hidden;
  font-size: 1rem;
  color: var(--text-soft);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.captions__now {
  font-size: clamp(1.2rem, 2.6vw, 1.6rem);
  font-weight: 700;
  line-height: 1.3;
}

/* controls */

.controls {
  display: flex;
  flex: none;
  align-items: flex-start;
  justify-content: center;
  gap: clamp(0.35rem, 3vw, 1.5rem);
  padding: 0.25rem 0;
}

.ctl {
  display: grid;
  justify-items: center;
  gap: 0.35rem;
  min-width: 4.6rem;
  padding: 0.25rem;
  border: 0;
  border-radius: var(--radius-m);
  background: none;
  color: var(--text);
  cursor: pointer;
}

.ctl__icon {
  display: grid;
  place-items: center;
  width: 3.75rem;
  height: 3.75rem;
  border-radius: 999px;
  background: var(--night-700);
  transition: background-color 0.15s ease, transform 0.1s ease;
}

.ctl:hover .ctl__icon {
  background: var(--night-600);
}

.ctl:active .ctl__icon {
  transform: scale(0.94);
}

.ctl .icon {
  width: 1.75rem;
  height: 1.75rem;
}

.ctl__label {
  font-size: 0.9rem;
  font-weight: 800;
}

.ctl[aria-pressed="true"] .icon--off,
.ctl[aria-pressed="false"] .icon--on {
  display: none;
}

.ctl--toggle[aria-pressed="true"] .ctl__icon {
  background: var(--indigo-500);
}

.ctl--mute[aria-pressed="true"] .ctl__icon {
  background: var(--cloud-100);
  color: var(--night-950);
}

.ctl--end .ctl__icon {
  width: 5.75rem;
  background: var(--red-500);
  color: #fff;
}

.ctl--end:hover .ctl__icon {
  background: var(--red-600);
}

/* debug tools (?debug=1 on a server with debug tools on) */

.debug {
  display: grid;
  flex: none;
  gap: 0.4rem;
  padding: 0.6rem 0.75rem;
  border: 1px dashed var(--night-600);
  border-radius: var(--radius-s);
  background: var(--night-900);
  color: var(--text-soft);
  font-size: 0.85rem;
}

.debug__form {
  display: flex;
  gap: 0.5rem;
}

.debug__form input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 0.45rem 0.7rem;
  border: 1px solid var(--night-600);
  border-radius: 10px;
  background: var(--night-800);
  color: var(--text);
}

.debug__events {
  overflow: hidden;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- tutor avatar ---------- */

.avatar__svg {
  display: block;
  width: 100%;
  height: auto;
  overflow: visible;
}

/* The drawing itself (Bilby) carries its own colours; everything that moves is in avatar.css. */

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

@keyframes pulse {
  50% { opacity: 0.35; }
}

/* The self-view fading in at its new place when the stage switches layout on a phone. */
@keyframes self-away {
  from { opacity: 0; transform: scale(0.8); }
}

@keyframes self-back {
  from { opacity: 0; }
}

/* Pictures on the shared screen: each keyframe only says where a part comes from. */
@keyframes vis-fade {
  from { opacity: 0; }
}

@keyframes vis-pop {
  from { opacity: 0; transform: scale(0.3); }
}

@keyframes vis-rise {
  from { opacity: 0; transform: translateY(0.3em); }
}

@keyframes vis-slide {
  from { opacity: 0; transform: translateX(-0.3em); }
}

@keyframes vis-draw {
  from { stroke-dashoffset: 1; }
}

@keyframes vis-bob {
  50% { transform: translateY(-6%); }
}

@keyframes vis-shadow {
  50% { opacity: 0.6; transform: scale(0.8); }
}

@keyframes sparkle-fly {
  0% { opacity: 0; transform: translate(var(--x0), var(--y0)) scale(0.2) rotate(0deg); }
  20% { opacity: 1; }
  65% { opacity: 1; }
  100% { opacity: 0; transform: translate(var(--x1), var(--y1)) scale(1) rotate(var(--spin)); }
}

@keyframes tutor-cheer {
  25% {
    border-color: var(--sun-400);
    box-shadow: 0 0 0 6px rgba(255, 201, 64, 0.3), 0 0 2.5rem rgba(255, 201, 64, 0.4);
  }
}

@keyframes tutor-hop {
  30% { transform: translateY(-7%) rotate(-3deg); }
  55% { transform: translateY(0) rotate(2deg); }
  75% { transform: translateY(-2.5%) rotate(0deg); }
}

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

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

@media (min-width: 40rem) {
  .room {
    gap: 1rem;
    padding: 1rem 1.25rem;
  }

  .ctl__icon {
    width: 4rem;
    height: 4rem;
  }

  .ctl--end .ctl__icon {
    width: 6.5rem;
  }
}

/* From here the tutor tile is wide enough for the self-view to float over its corner,
   picture-in-picture, so the tutor can use the full height. */
@media (min-width: 42.5rem) {
  .stage {
    --people-h: clamp(6.25rem, 15vh, 8.5rem);
    --pip-w: calc((var(--people-h) - 1rem) * 4 / 3);
    min-height: 15rem;
  }

  /* Side insets keep the centred tutor clear of the self-view tile. */
  .tutor__figure {
    inset: 1.75rem calc(var(--self-width) + 1.6rem);
    max-width: 26rem;
    max-height: 26rem;
  }

  .self {
    position: absolute;
    right: 0.85rem;
    bottom: 0.85rem;
    display: block;
    width: var(--self-width);
    padding: 0;
    box-shadow: 0 12px 26px -12px rgba(0, 0, 0, 0.7);
    animation: none;
    transition: right var(--swap), bottom var(--swap), width var(--swap);
  }

  .self__frame {
    border-radius: 0;
  }

  .self__label,
  .self__note {
    max-height: 5rem;
    overflow: hidden;
    padding: 0.4rem 0.6rem 0.5rem;
    font-size: 0.78rem;
    transition: max-height var(--swap), padding var(--swap), opacity 0.2s ease;
  }

  .self__note {
    padding-top: 0;
  }

  .tile__name {
    max-width: calc(100% - 16rem);
  }

  /* Presenting: the self-view stays in its corner and only gets smaller; its note folds away. */
  .stage.is-presenting .self {
    right: 0.5rem;
    bottom: 0.5rem;
    animation: none;
  }

  .stage.is-presenting .self__label,
  .stage.is-presenting .self__note {
    display: block;
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    opacity: 0;
  }
}

/* Wide or lying-down windows: the shared screen and the tutor sit side by side. */
@media (min-width: 58rem), (min-width: 42.5rem) and (orientation: landscape) {
  .stage {
    --pip-w: 5rem;
  }

  .share {
    inset: 0 calc(var(--people-w) + var(--stage-gap)) 0 0;
    clip-path: inset(0 100% 0 0 round var(--radius-l)); /* closed: wiped away towards its left edge */
  }

  .stage__main {
    max-height: 100%;
    margin-block: auto; /* presenting: the shorter tile sits half-way up the screen's side */
  }

  .stage.is-presenting .stage__main {
    top: 0;
    left: calc(100% - var(--people-w));
    max-height: 25rem;
  }

  /* The tutor's tile is upright here, so it keeps its normal arrangement. */
  .stage.is-presenting .tutor__figure {
    inset: 3.4rem 0.9rem 4.9rem;
  }

  .stage.is-presenting .status {
    top: 0.85rem;
    left: 0.85rem;
    gap: 0.55rem;
    max-width: calc(100% - 1.7rem);
    padding: 0.45rem 0.95rem;
    font-size: 1.05rem;
  }

  .stage.is-presenting .tile__name {
    bottom: 0.85rem;
    left: 0.85rem;
    gap: 0.4rem;
    max-width: calc(100% - var(--pip-w) - 1.9rem);
    padding: 0.35rem 0.7rem;
    font-size: 0.95rem;
  }

  .stage.is-presenting .self {
    right: 0.6rem;
    bottom: 0.6rem;
  }
}
