/* «Собери слово» engine style. Default colours come from «Три поросёнка» (read-syllables);
   a lesson repaints them with its own theme.css, which is loaded after this file. */

:root {
  --ink: #3d2b2f;
  --muted: #8b7576;
  --sky: #d7f0ff;
  --cream: #fff8ec;
  --main: #ff7096;         /* main colour — piggy pink: big speaker, main button */
  --main-dark: #e04f78;
  --main-soft: #ffe1ea;
  --tile-border: #ffc4d3;
  --tile-shadow: #ffb0c4;
  --cell: #ffffff;
  --cell-border: #eadfcd;
  --ok: #22a35a;
  --ok-bg: #dcf8e7;
  --bad: #e5484d;
  --bad-bg: #ffe4e4;
  --level-1: #f2b31c;      /* level colours: straw / sticks / stone */
  --level-1-soft: #fff1c9;
  --level-2: #b9773d;
  --level-2-soft: #f5e4d2;
  --level-3: #6f7d95;
  --level-3-soft: #e6ebf3;
  --grass: #7ccf5b;
}

body { --accent: var(--level-1); --accent-soft: var(--level-1-soft); }
body[data-level="1"] { --accent: var(--level-2); --accent-soft: var(--level-2-soft); }
body[data-level="2"] { --accent: var(--level-3); --accent-soft: var(--level-3-soft); }

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: linear-gradient(180deg, var(--sky) 0%, var(--cream) 55%) fixed;
  color: var(--ink);
  font-family: "Nunito", ui-rounded, "SF Pro Rounded", system-ui, -apple-system, sans-serif;
  font-weight: 600;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}
button:disabled { cursor: default; }

/* ---------- Modes: each page shows only its own blocks ---------- */

/* .only-mic — both reading-aloud modes: "aloud" (a word) and "sentence" */
body[data-mode="assemble"] .only-mic,
body:not([data-mode="assemble"]) .only-assemble,
body:not([data-mode="sentence"]) .only-sentence,
body[data-mode="sentence"] .not-sentence { display: none !important; }

/* ---------- Levels ---------- */

header {
  padding: 14px 16px 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.levels {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

.level {
  display: grid;
  grid-template-columns: auto auto;
  grid-template-areas: "icon name" "icon pips";
  align-items: center;
  column-gap: 10px;
  padding: 8px 16px 8px 10px;
  background: rgba(255, 255, 255, 0.7);
  border: 3px solid transparent;
  border-radius: 18px;
  text-align: left;
  transition: transform 0.15s, background 0.15s;
}
.level:hover { transform: translateY(-2px); }
.level.current { background: #fff; border-color: var(--accent); box-shadow: 0 4px 0 var(--accent-soft); }
.level-icon { grid-area: icon; font-size: 34px; line-height: 1; }
.level-name { grid-area: name; font-weight: 800; font-size: 16px; }
.pips { grid-area: pips; display: flex; gap: 4px; margin-top: 4px; }
/* A pip lights up: --fill goes from 0 to 1 — each word makes it brighter and slightly bigger */
.pips { align-items: center; }
.level:nth-child(1) { --pip: var(--level-1); }  /* each level has its own pip colour */
.level:nth-child(2) { --pip: var(--level-2); }
.level:nth-child(3) { --pip: var(--level-3); }
.pips i {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--pip, var(--accent)) calc(var(--fill, 0) * 100%), #e7ddd0);
  transform: scale(calc(0.8 + var(--fill, 0) * 0.3));
  transition: background 0.4s, transform 0.4s;
}
.pips i.full { box-shadow: 0 0 0 2px #fff, 0 0 8px 1px var(--pip, var(--accent)); }
.level.done .pips i { --pip: var(--ok); }

/* ---------- Word ---------- */

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 12px 16px 90px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

.board {
  min-height: 38vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
}

/* Word picture — a hint of what is being assembled */
.picture {
  width: clamp(120px, 22vh, 200px);
  aspect-ratio: 1;
  object-fit: contain;
  background: #fff;
  border-radius: 28px;
  padding: 8px;
  box-shadow: 0 6px 0 var(--accent-soft), 0 10px 24px rgba(61, 43, 47, 0.08);
  animation: pop 0.35s ease-out;
}
.picture[hidden] { display: none; }

.word-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
  flex-wrap: wrap;
}

.word {
  /* --n is set from the script: letter count + room for the dots */
  --box: min(118px, calc((100vw - 150px) / var(--n, 6)));
  display: flex;
  align-items: center;
  gap: calc(var(--box) * 0.16);
}

.group { display: flex; gap: calc(var(--box) * 0.07); }

/* A riddle answer of several words: each word is a block, a long phrase wraps between the words */
.word.phrase { flex-wrap: wrap; justify-content: center; column-gap: calc(var(--box) * 0.7); row-gap: calc(var(--box) * 0.3); }
.phrase-word { display: flex; align-items: center; gap: calc(var(--box) * 0.16); }

.cell {
  width: var(--box);
  height: calc(var(--box) * 1.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--box) * 0.82);
  font-weight: 900;
  line-height: 1;
  background: var(--cell);
  border: 3px solid var(--cell-border);
  border-radius: calc(var(--box) * 0.2);
  box-shadow: inset 0 -5px 0 rgba(61, 43, 47, 0.05);
}
.cell.filled { border-color: var(--accent); background: #fff; animation: pop 0.22s ease-out; }
.cell.ok { border-color: var(--ok); background: var(--ok-bg); color: var(--ok);
           animation: hop 0.5s ease-out both; animation-delay: calc(var(--i, 0) * 70ms); }
.cell.bad { border-color: var(--bad); background: var(--bad-bg); color: var(--bad); }

.dot {
  width: calc(var(--box) * 0.2);
  height: calc(var(--box) * 0.2);
  border-radius: 50%;
  background: var(--ink);
  flex: none;
}

.speaker {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: var(--main-soft);
  color: var(--main-dark);
  transition: transform 0.1s;
}
.speaker svg { width: 60%; height: 60%; }
.speaker:active:not(:disabled) { transform: scale(0.9); }
.speaker.big { width: 84px; height: 84px; background: var(--main); color: #fff; box-shadow: 0 5px 0 var(--main-dark); }
.speaker.small { width: 36px; height: 36px; flex: none; }
.speaker:disabled { opacity: 0.4; }

/* ---------- Buttons ---------- */

#message { margin: 0; min-height: 1.4em; font-size: 30px; font-weight: 900; text-align: center; }
#message.ok { color: var(--ok); animation: pop 0.3s ease-out; }
#message.bad { color: var(--bad); }

.controls { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }

.btn {
  font-size: 21px;
  font-weight: 800;
  padding: 12px 26px;
  border-radius: 999px;
  border: 3px solid #eadfcd;
  background: #fff;
  box-shadow: 0 4px 0 #eadfcd;
  transition: transform 0.1s, box-shadow 0.1s;
}
.btn:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 1px 0 #eadfcd; }
.btn.primary { background: var(--main); border-color: var(--main); color: #fff; box-shadow: 0 4px 0 var(--main-dark); }
.btn.primary:active:not(:disabled) { box-shadow: 0 1px 0 var(--main-dark); }
.btn:disabled { opacity: 0.45; }
.btn[hidden] { display: none; }

/* ---------- Tiles ---------- */

.tiles { display: flex; flex-wrap: wrap; justify-content: center; gap: 14px; max-width: 900px; }

.tile {
  display: flex;
  align-items: center;
  gap: 4px;
  padding-right: 8px;
  background: #fff;
  border: 3px solid var(--tile-border);
  border-radius: 20px;
  box-shadow: 0 5px 0 var(--tile-shadow);
  transition: transform 0.12s, opacity 0.2s;
}
.tile:not(.used):hover { transform: translateY(-3px); }
.tile.used { opacity: 0.25; box-shadow: none; transform: scale(0.94); }

.tile-text {
  border: none;
  background: none;
  font-size: 46px;
  font-weight: 900;
  padding: 6px 4px 6px 18px;
  min-width: 70px;
}
.tile-text:disabled { color: var(--ink); }

/* ---------- "Level complete" dialog ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(61, 43, 47, 0.35);
}
.overlay[hidden] { display: none; }
.card {
  max-width: 440px;
  width: 100%;
  padding: 28px 24px;
  background: #fff;
  border-radius: 28px;
  text-align: center;
  box-shadow: 0 10px 0 var(--accent-soft), 0 20px 50px rgba(0, 0, 0, 0.2);
  animation: pop 0.35s ease-out;
}
.card-icon { font-size: 84px; line-height: 1.1; }
.card h2 { margin: 10px 0 6px; font-size: 30px; font-weight: 900; }
.card p { margin: 0 0 22px; color: var(--muted); font-size: 18px; }

/* ---------- Reading aloud ---------- */

.score {
  padding: 8px 16px;
  background: #fff;
  border-radius: 18px;
  font-size: 22px;
  font-weight: 900;
  box-shadow: 0 4px 0 var(--accent-soft);
}
.score.bump { animation: pop 0.4s ease-out; }

/* No picture here, so the word doesn't need the picture's room */
body[data-mode="aloud"] .board { min-height: 24vh; }

/* The word is read, not assembled: each part is one plate «к · ры · са», so it isn't read letter by letter */
body[data-mode="aloud"] .group {
  gap: 0;
  padding: 0 calc(var(--box) * 0.22);
  background: #fff;
  border: 3px solid var(--accent);
  border-radius: calc(var(--box) * 0.2);
  box-shadow: inset 0 -5px 0 rgba(61, 43, 47, 0.05);
}
body[data-mode="aloud"] .group:has(.ok) { border-color: var(--ok); background: var(--ok-bg); }
body[data-mode="aloud"] .cell { width: auto; border: none; background: none; box-shadow: none; }

.heard { margin: 0; min-height: 1.4em; font-size: 22px; color: var(--muted); text-align: center; }
.heard.ok { color: var(--ok); }
.heard.miss { color: var(--bad); }

.mic {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  padding: 14px 40px;
  font-size: 30px;
  font-weight: 900;
  border: none;
  border-radius: 999px;
  background: var(--main);
  color: #fff;
  box-shadow: 0 5px 0 var(--main-dark);
  transition: transform 0.1s, box-shadow 0.1s;
}
.mic:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 2px 0 var(--main-dark); }
.mic:disabled { opacity: 0.45; }
.mic[hidden] { display: none; }
.mic.active { animation: listening 1.2s ease-in-out infinite; }
@keyframes listening {
  0%, 100% { box-shadow: 0 5px 0 var(--main-dark), 0 0 0 0 var(--main-soft); }
  50% { box-shadow: 0 5px 0 var(--main-dark), 0 0 0 14px var(--main-soft); }
}

.note { margin: 0; max-width: 520px; color: var(--muted); font-size: 16px; text-align: center; }
.note[hidden] { display: none; }

/* ---------- Reading a sentence aloud ---------- */

body[data-mode="sentence"] .board { min-height: 22vh; }

/* Big, calm text: words wrap between each other, a heard word turns green */
.sentence {
  margin: 0;
  max-width: 920px;
  padding: 18px 26px;
  background: #fff;
  border: 3px solid var(--accent);
  border-radius: 28px;
  box-shadow: 0 6px 0 var(--accent-soft);
  font-size: clamp(28px, 4.4vw, 48px);
  font-weight: 800;
  line-height: 1.45;
  text-align: center;
  animation: pop 0.35s ease-out;
}
.sentence-word { display: inline-block; padding: 0 2px; border-radius: 10px; transition: color 0.2s, background 0.2s; }
.sentence-word.ok { color: var(--ok); background: var(--ok-bg); }
/* After «Готово»: the key words that were not heard */
.sentence-word.miss { text-decoration: underline wavy var(--bad); text-underline-offset: 8px; text-decoration-thickness: 2px; }
/* A sentence read right: a green wave across the words */
.sentence-word.ok[style] { animation: hop 0.5s ease-out both; animation-delay: calc(var(--i, 0) * 60ms); }

/* The prize: the text of the voiced secret, with a speaker to hear it again */
.reward {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  max-width: 820px;
  padding: 18px 20px 18px 24px;
  background: var(--main-soft);
  border-radius: 24px;
  animation: pop 0.35s ease-out;
}
.reward[hidden] { display: none; }
.reward-text { margin: 0; font-size: 21px; line-height: 1.5; }
.reward .speaker.small { width: 48px; height: 48px; background: var(--main); color: #fff; box-shadow: 0 4px 0 var(--main-dark); }

/* ---------- Confetti and grass ---------- */

.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 20; }
.confetti span { position: absolute; top: -50px; animation: fall 1.8s ease-in forwards; }

.grass {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 44px;
  pointer-events: none;
  background:
    radial-gradient(circle at 20px 0, transparent 22px, var(--grass) 23px) 0 0 / 40px 44px repeat-x;
  opacity: 0.85;
}

/* ---------- Animations ---------- */

@keyframes pop { 0% { transform: scale(0.85); } 70% { transform: scale(1.06); } 100% { transform: scale(1); } }
@keyframes hop { 0%, 100% { transform: translateY(0); } 40% { transform: translateY(-18%); } }
@keyframes fall {
  to { transform: translate(var(--drift, 0), 110vh) rotate(540deg); opacity: 0.9; }
}
.shake { animation: shake 0.4s; }
@keyframes shake {
  20% { transform: translateX(-8px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(4px); }
}

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

/* ---------- Phone ---------- */

@media (max-width: 700px) {
  header { padding: 10px 8px 0; }
  .levels { gap: 6px; }
  .level { grid-template-columns: auto; grid-template-areas: "icon" "pips"; justify-items: center; padding: 6px 10px; }
  .level-name { display: none; }
  .level-icon { font-size: 28px; }
  .pips i { width: 11px; height: 11px; }
  main { gap: 14px; padding-bottom: 70px; }
  .board { min-height: 26vh; gap: 10px; }
  .word-row { flex-direction: column; gap: 10px; }
  .picture { width: 120px; border-radius: 22px; padding: 6px; }
  .word { --box: min(84px, calc((100vw - 32px) / var(--n, 6))); }
  .cell { border-width: 2px; }
  .speaker.big { width: 64px; height: 64px; }
  .btn { font-size: 17px; padding: 10px 16px; }
  #message { font-size: 22px; }
  .heard { font-size: 18px; }
  .mic { font-size: 24px; padding: 12px 32px; }
  .score { font-size: 18px; padding: 6px 12px; }
  .sentence { padding: 12px 14px; border-radius: 22px; font-size: clamp(22px, 6.4vw, 30px); }
  .reward { padding: 14px; gap: 10px; }
  .reward-text { font-size: 17px; }
  .reward .speaker.small { width: 40px; height: 40px; }
  .tiles { gap: 9px; }
  .tile { border-radius: 16px; padding-right: 6px; }
  .tile-text { font-size: 34px; padding: 4px 2px 4px 12px; min-width: 52px; }
  .speaker.small { width: 30px; height: 30px; }
  .grass { height: 30px; background-size: 30px 30px; }
}
