/* ===== Fonts =====
   Nunito is bundled (fonts/nunito, SIL Open Font License 1.1), so the app looks the same without internet.
   One variable file covers every weight the app uses (700–900). */
@font-face {
  font-family: 'Nunito';
  src: url('../fonts/nunito/Nunito-Variable.ttf') format('truetype');
  font-weight: 200 1000;
  font-style: normal;
  font-display: swap;
}

/* ===== Tokens ===== */
@property --hold-progress {
  syntax: '<number>';
  inherits: false;
  initial-value: 0;
}

:root {
  /* Sizes scale with the viewport, so Windows display scaling does not matter. */
  --item-size: clamp(88px, 14vmin, 300px);
  --zone-size: calc(var(--item-size) * 1.2);
  --radius: calc(var(--item-size) * 0.2);
  --gap: min(calc(var(--item-size) * 0.35), 1.8vw);

  --color-bg-top: #eaf4ff;
  --color-bg-bottom: #fff7e8;
  --color-text: #2f3a56;
  --color-muted: #5d6b8a;
  --color-primary: #5c6bc0;
  --color-success: #43a047;
  --color-highlight: #ffb300;
  --color-surface: #ffffff;
  --color-on-primary: #ffffff;
  --color-backdrop: rgba(47, 58, 86, 0.45);
  --color-divider: rgba(47, 58, 86, 0.12);
  --color-zone-default: #90a4d4;
  --shadow-item: 0 0.6vmin 1.6vmin rgba(47, 58, 86, 0.18);
  --shadow-lift: 0 1.6vmin 3.2vmin rgba(47, 58, 86, 0.28);

  --font-main: 'Nunito', 'Segoe UI', system-ui, sans-serif;
  --font-emoji: 'Segoe UI Emoji', 'Apple Color Emoji', 'Noto Color Emoji', sans-serif;

  /* Overridden from config.js at startup */
  --return-ms: 280ms;
  --hold-ms: 1000ms;
}

/* ===== Base: block browser touch gestures ===== */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: var(--font-main);
  color: var(--color-text);
  background: linear-gradient(180deg, var(--color-bg-top), var(--color-bg-bottom));
}

/* Scene root (js/scene.js draws the bands inside, see css/scene.css). */
#app {
  width: 100vw;
  height: 100vh;
}

#app[hidden] { display: none; }

/* Author credit: faint, in the 2% bottom gap of the scene (css/scene.css rows), above every overlay, never touchable. */
.credit {
  position: fixed;
  right: 1.2vw;
  bottom: 0.3vh;
  z-index: 200;
  font-family: var(--font-main);
  font-weight: 700;
  font-size: 1.4vh;
  line-height: 1;
  color: var(--color-text);
  opacity: 0.45;
  pointer-events: none;
  white-space: nowrap;
}

/* One step container in the stage band; the stage keeps its side padding away from the corners. */
.step {
  /* The step dots and station posts reach a little into the bottom of the stage band; keep content above them. */
  --rail-clearance: 3vh;
  height: 100%;
  padding-bottom: var(--rail-clearance);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
  min-height: 0;
}

.row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: var(--gap);
}

/* ===== Content ===== */
.content-text {
  font-family: var(--font-emoji);
  line-height: 1;
  pointer-events: none;
}

.content-image {
  width: 80%;
  height: 80%;
  object-fit: contain;
  pointer-events: none;
}

/* ===== Draggable items ===== */
.item {
  position: relative;
  z-index: 1;
  width: var(--item-size);
  height: var(--item-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--item-size) * 0.58);
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-item);
  cursor: grab;
  touch-action: none;
  will-change: transform;
}

.item.is-dragging {
  z-index: 100;
  cursor: grabbing;
  box-shadow: var(--shadow-lift);
}

.item.is-returning {
  transition: transform var(--return-ms) ease-out;
}

.item.is-locked {
  pointer-events: none;
  background: transparent;
  box-shadow: none;
}

.item-placeholder {
  width: var(--item-size);
  height: var(--item-size);
  visibility: hidden;
}

.item--number .content-text {
  font-family: var(--font-main);
  font-weight: 900;
}

.item--number:nth-child(4n + 1) { color: #e65100; }
.item--number:nth-child(4n + 2) { color: #2e7d32; }
.item--number:nth-child(4n + 3) { color: #1565c0; }
.item--number:nth-child(4n + 4) { color: #ad1457; }

/* ===== Drop zones ===== */
.zone {
  --zone-color: var(--color-zone-default);
  width: var(--zone-size);
  height: var(--zone-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--item-size) * 0.45);
  border: calc(var(--item-size) * 0.05) dashed var(--zone-color);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--zone-color) 45%, white);
  transition: transform 150ms ease-out, box-shadow 150ms ease-out;
}

.zone.is-hover {
  transform: scale(1.08);
  box-shadow: 0 0 0 calc(var(--item-size) * 0.06) var(--color-highlight);
}

.zone.is-filled {
  border-style: solid;
  border-color: var(--color-success);
}

.zone.is-filled .zone-marker { display: none; }

.zone.is-wrong { animation: shake 400ms ease-in-out; }

.zone-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  opacity: 0.75;
}

/* Sorting: a zone that receives several items */
.zone--multi {
  position: relative;
  width: calc(var(--zone-size) * 1.6);
  flex-wrap: wrap;
  align-content: center;
  gap: calc(var(--item-size) * 0.06);
  padding: calc(var(--item-size) * 0.08);
}

.zone--multi .zone-marker {
  position: absolute;
  inset: 0;
  opacity: 0.35;
  font-size: calc(var(--item-size) * 0.7);
}

.zone--multi.is-filled .zone-marker { display: flex; }

.zone--multi .item {
  width: calc(var(--item-size) * 0.55);
  height: calc(var(--item-size) * 0.55);
  font-size: calc(var(--item-size) * 0.36);
}

/* ===== Prompt (teacher asks, child taps) ===== */
.choice {
  width: calc(var(--item-size) * 1.45);
  height: calc(var(--item-size) * 1.45);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--item-size) * 0.85);
  background: var(--color-surface);
  border: calc(var(--item-size) * 0.05) solid transparent;
  border-radius: var(--radius);
  box-shadow: var(--shadow-item);
  cursor: pointer;
  touch-action: none;
  transition: transform 150ms ease-out, border-color 150ms ease-out;
}

.choice.is-selected {
  border-color: var(--color-highlight);
  transform: scale(1.08);
}

.choice.is-correct {
  border-color: var(--color-success);
  transform: scale(1.12);
  animation: pop 500ms ease-out;
}

.choice.is-wrong { animation: shake 400ms ease-in-out; }

/* ===== Corners (tap to count) ===== */
.shape-figure {
  --corner-size: calc(var(--item-size) * 0.75);
  position: relative;
  width: min(48vh, 40vw);
  aspect-ratio: 1;
}

.shape-figure svg {
  width: 100%;
  height: 100%;
  overflow: visible;
}

.shape-outline {
  stroke: rgba(47, 58, 86, 0.35);
  stroke-width: 1.5;
  stroke-linejoin: round;
}

.corner {
  position: absolute;
  width: var(--corner-size);
  height: var(--corner-size);
  /* Centered with margins, not transform: P.motion animations replace the transform. */
  margin-left: calc(var(--corner-size) * -0.5);
  margin-top: calc(var(--corner-size) * -0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--corner-size) * 0.55);
  font-weight: 900;
  color: var(--color-text);
  border: calc(var(--corner-size) * 0.07) dashed #fff;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.35);
  touch-action: none;
  cursor: pointer;
}

.corner.is-done {
  border-style: solid;
  border-color: var(--color-text);
  background: var(--color-highlight);
  animation: corner-pop 400ms ease-out;
}

/* ===== Pattern & counting ===== */
.pattern-box {
  width: var(--item-size);
  height: var(--item-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--item-size) * 0.55);
  /* Flat on purpose: must not look like a draggable item. */
  background: rgba(255, 255, 255, 0.55);
  border: calc(var(--item-size) * 0.025) solid #d5dbeb;
  border-radius: var(--radius);
}

/* Pattern sequence: always one line. Long sequences shrink their tiles to fit the stage width
   (the stage keeps 8vw side padding); --sequence-length is set by js/renderers/pattern.js. */
.pattern-sequence {
  --stage-inner-width: 84vw;
  --zone-to-box: 1.2;
  --placed-item-share: 0.84;
  --sequence-tile: min(var(--zone-size),
    calc((var(--stage-inner-width) - (var(--sequence-length) - 1) * var(--gap)) / var(--sequence-length)));
  flex-wrap: nowrap;
}

.pattern-sequence > .zone {
  width: var(--sequence-tile);
  height: var(--sequence-tile);
}

.pattern-sequence > .pattern-box {
  width: calc(var(--sequence-tile) / var(--zone-to-box));
  height: calc(var(--sequence-tile) / var(--zone-to-box));
}

.pattern-sequence > .zone > .item {
  width: calc(var(--sequence-tile) * var(--placed-item-share));
  height: calc(var(--sequence-tile) * var(--placed-item-share));
}

.count-objects {
  --columns: 5;
  display: grid;
  grid-template-columns: repeat(var(--columns), auto);
  gap: calc(var(--item-size) * 0.12);
  padding: calc(var(--item-size) * 0.18);
  font-size: calc(var(--item-size) * 0.42);
  background: rgba(255, 255, 255, 0.55);
  border: calc(var(--item-size) * 0.025) solid #d5dbeb;
  border-radius: var(--radius);
}

.equals {
  font-size: calc(var(--item-size) * 0.6);
  font-weight: 900;
}

/* ===== Navigation (scene nav row, away from corners) ===== */
.scene__nav.is-busy { opacity: 0.5; }
.scene__nav.is-busy > .nav-button { pointer-events: none; }
/* During scene travel only the ↻ hold (full restart) stays armed; app.js runs it after the travel. */
.scene__nav.is-busy.allows-restart > .nav-button--reset { pointer-events: auto; }

.nav-button {
  position: relative;
  width: var(--scene-nav-size);
  height: var(--scene-nav-size);
  padding: 0;
  border: none;
  border-radius: 50%;
  font-family: var(--font-main);
  font-size: calc(var(--scene-nav-size) * 0.42);
  font-weight: 900;
  color: #fff;
  background: var(--color-primary);
  box-shadow: var(--shadow-item);
  cursor: pointer;
  touch-action: none; /* avoids pointercancel during long press */
  transition: opacity 150ms;
}

.nav-button:disabled { opacity: 0.3; cursor: default; }
.nav-button.is-waiting { opacity: 0.45; }
.nav-button.is-ready {
  background: var(--color-success);
  animation: pulse 1.4s ease-in-out infinite;
}

/* Long-press progress ring; --hold-ms is set per button (▶ skip, ↻ restart). */
.nav-button::after {
  content: '';
  position: absolute;
  inset: calc(var(--scene-nav-size) * -0.12);
  border-radius: 50%;
  background: conic-gradient(var(--color-highlight) calc(var(--hold-progress) * 1%), transparent 0);
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 8px), #000 calc(100% - 7px));
  mask: radial-gradient(farthest-side, transparent calc(100% - 8px), #000 calc(100% - 7px));
  opacity: 0;
  pointer-events: none;
}

.nav-button.is-holding { opacity: 1; }
.nav-button.is-holding::after {
  opacity: 1;
  animation: hold-fill var(--hold-ms) linear forwards;
}

/* ===== Error screen (for the content editor) ===== */
#error-screen {
  position: fixed;
  inset: 0;
  overflow: auto;
  padding: 4vh 6vw;
  background: #fff;
  font-family: var(--font-main);
  touch-action: pan-y;
  user-select: text;
  -webkit-user-select: text;
}

.error-title { color: #c62828; margin-top: 0; }
.error-intro { color: var(--color-muted); }
.error-list { padding-left: 1.2em; }
.error-item { margin-bottom: 1.2em; }
.error-field { display: block; margin: 0.3em 0; color: var(--color-primary); }
.error-message { margin: 0; }

/* ===== Teacher settings panel (js/settings.js; opened from the cover) =====
   The backdrop blocks the whole screen; the panel starts below the top 20% band and is centered,
   so no button sits in the band or near a corner. Only the scene list scrolls. */
.settings {
  --settings-target: max(64px, calc(var(--item-size) * 0.6));
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 22vh 0 3vh;
  background: var(--color-backdrop);
  font-family: var(--font-main);
  color: var(--color-text);
  touch-action: none;
}

.settings__panel {
  display: flex;
  flex-direction: column;
  gap: 1.6vh;
  width: min(72vw, 1500px);
  max-height: 100%;
  padding: 2.4vh 2.4vw;
  border-radius: 3vmin;
  background: var(--color-surface);
  box-shadow: var(--shadow-lift);
}

.settings__title {
  margin: 0;
  font-size: clamp(24px, 5vh, 110px);
  font-weight: 900;
  text-align: center;
}

.settings__heading {
  margin: 0;
  font-size: clamp(16px, 3vh, 64px);
  font-weight: 800;
  color: var(--color-muted);
}

.settings__list {
  flex: 1 1 auto;
  min-height: 0;
  margin: 0;
  padding: 0 1vw 0 0;
  list-style: none;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
}

.settings__row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 1.6vw;
  padding: 0.8vh 0;
  border-bottom: 0.2vmin solid var(--color-divider);
}

.settings__row--hidden .settings__row-title {
  color: var(--color-muted);
  text-decoration: line-through;
}

.settings__row-title {
  font-size: clamp(16px, 3.2vh, 70px);
  font-weight: 800;
}

.settings__button {
  min-width: var(--settings-target);
  min-height: var(--settings-target);
  padding: 0 2vw;
  border: 0.4vmin solid var(--color-primary);
  border-radius: 999px;
  background: var(--color-surface);
  color: var(--color-primary);
  font: inherit;
  font-size: clamp(15px, 2.8vh, 62px);
  font-weight: 800;
  cursor: pointer;
  touch-action: manipulation;
}

.settings__button:active { transform: scale(0.97); }
.settings__button:disabled { opacity: 0.35; cursor: default; transform: none; }
.settings__button--toggle { min-width: calc(var(--settings-target) * 2); }
.settings__button--toggle:not(.is-on) { background: var(--color-primary); color: var(--color-on-primary); }

.settings__footer {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  gap: 1.6vw;
}

.settings__option {
  display: inline-flex;
  align-items: center;
  gap: 1vw;
}

.settings__option.is-on .settings__option-state { color: var(--color-success); }
.settings__option:not(.is-on) .settings__option-state { color: var(--color-muted); }

.settings__button--close {
  background: var(--color-primary);
  color: var(--color-on-primary);
}

/* ===== Animations ===== */
@keyframes pop {
  0% { transform: scale(0.85); }
  60% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

@keyframes corner-pop {
  0% { transform: scale(0.8); }
  60% { transform: scale(1.25); }
  100% { transform: scale(1); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4%); }
  40% { transform: translateX(4%); }
  60% { transform: translateX(-3%); }
  80% { transform: translateX(3%); }
}

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

@keyframes hold-fill {
  from { --hold-progress: 0; }
  to { --hold-progress: 100; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; }
}
