/* Layout: one calm screen — word column left, picture centre, help right, sentence tray below.
   Single light theme on purpose: the illustrations are drawn on white and the app is for young children. */
:root {
  color-scheme: light;
  --ground: #edf3f8;        /* soft sky, low-stimulus background */
  --paper: #ffffff;         /* picture card */
  --ink: #1f2a37;           /* word text and outlines */
  --muted: #5c6b7c;
  --sun: #ffc83d;           /* word cards, from the draft */
  --sun-deep: #e9a800;
  --bike: #2f7df6;          /* hint highlight, the bicycle's blue */
  --slot: #c9d6e3;
  --leaf: #2e9d63;          /* placed / success */
  --leaf-soft: #e3f4ea;
  --berry: #e5484d;         /* wrong-place flash */
  --berry-soft: #fde8e8;

  --font-word: 'Nunito', 'Segoe UI', 'Roboto', system-ui, sans-serif;
  --font-ui: 'Nunito', 'Segoe UI', 'Roboto', system-ui, sans-serif;
  --radius: 18px;
  --word-size: clamp(1.15rem, 0.9rem + 1vw, 1.7rem);
  --tap: 64px;              /* minimum target height for small hands */
}

[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--font-ui);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

.app {
  box-sizing: border-box;
  height: 100%;
  display: grid;
  grid-template-columns: minmax(150px, 230px) minmax(0, 1fr) auto;
  grid-template-rows: minmax(0, 1fr) auto;
  grid-template-areas:
    "bank stage tools"
    "tray tray  tray";
  gap: clamp(12px, 2vw, 28px);
  padding-block: clamp(12px, 2.5vw, 28px);
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}

/* ---- word bank ---------------------------------------------------------- */
.bank {
  grid-area: bank;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: clamp(12px, 2.4vh, 24px);
  min-width: 0;
}

.word, .word-ghost {
  font: 800 var(--word-size) / 1 var(--font-word);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink);
  background: var(--sun);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  min-height: var(--tap);
  padding: 0 18px;
  box-shadow: 0 4px 0 var(--sun-deep);
  cursor: grab;
  touch-action: none;
  transition: transform 120ms ease, box-shadow 120ms ease, opacity 160ms ease;
}
.word:active { transform: translateY(2px); box-shadow: 0 2px 0 var(--sun-deep); }
.word:focus-visible, .slot:focus-visible, .icon-btn:focus-visible, .picture:focus-visible {
  outline: 4px solid var(--bike);
  outline-offset: 3px;
}
.word.is-selected { transform: translateY(-3px); box-shadow: 0 0 0 5px var(--paper), 0 0 0 9px var(--ink); }
/* Placed words keep their space so the column never jumps around. */
.word.is-placed { visibility: hidden; }
.word.is-dragging { opacity: 0.25; }
.word.is-hint, .slot.is-hint {
  border-color: var(--bike);
  box-shadow: 0 0 0 5px color-mix(in srgb, var(--bike) 30%, transparent);
  animation: glow 1.4s ease-in-out infinite;
}
.word.is-hint { text-decoration: underline 4px var(--bike); text-underline-offset: 6px; }
.word.is-nudged { animation: wobble 380ms ease; }

.word-ghost {
  position: fixed;
  z-index: 50;
  margin: 0;
  pointer-events: none;
  cursor: grabbing;
  box-shadow: 0 14px 28px rgb(31 42 55 / 0.25);
  transition: none;
}
.word-ghost.is-returning { transition: transform 260ms cubic-bezier(0.2, 0.8, 0.3, 1); }

/* ---- picture ------------------------------------------------------------ */
.stage { grid-area: stage; min-width: 0; min-height: 0; display: grid; place-items: center; }
.picture {
  position: relative;
  width: 100%;
  height: 100%;
  border: 0;
  padding: clamp(8px, 1.5vw, 20px);
  background: var(--paper);
  border-radius: calc(var(--radius) * 1.6);
  box-shadow: 0 1px 0 var(--slot), 0 18px 40px -24px rgb(31 42 55 / 0.35);
  cursor: pointer;
  display: grid;
  place-items: center;
  overflow: hidden;
}
/* The image is positioned inside the card (not sized by it), so `contain` always shows the whole
   picture; the bottom band is kept free for the "tap to hear" label. */
.picture img {
  position: absolute;
  inset: 16px 16px 48px;
  width: calc(100% - 32px);
  height: calc(100% - 64px);
  object-fit: contain;
  pointer-events: none;
}
.picture-hint {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--ground);
  color: var(--muted);
  font: 700 0.85rem/1 var(--font-ui);
  white-space: nowrap;
}

/* ---- tools (help, settings) ---------------------------------------------- */
.tools { grid-area: tools; display: flex; flex-direction: column; justify-content: space-between; align-items: center; gap: 16px; }
.icon-btn {
  display: grid;
  place-items: center;
  border: 3px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  border-radius: 22px;
  cursor: pointer;
}
.help-btn { width: clamp(72px, 8vw, 104px); aspect-ratio: 1; max-width: 100%; font: 900 clamp(2.2rem, 3vw, 3rem)/1 var(--font-ui); box-shadow: 0 4px 0 var(--slot); }
.help-btn:active { transform: translateY(2px); box-shadow: 0 2px 0 var(--slot); }
.gear-btn { width: 48px; height: 48px; border-width: 2px; border-radius: 14px; color: var(--muted); border-color: var(--slot); }

/* ---- sentence tray ------------------------------------------------------- */
.tray-row { grid-area: tray; display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.tray {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: clamp(10px, 1.6vw, 20px);
  min-width: 0;
}
.slot {
  font: 800 var(--word-size) / 1 var(--font-word);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink);
  min-height: calc(var(--tap) + 8px);
  background: var(--paper);
  border: 3px dashed var(--slot);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 140ms ease, background 140ms ease, transform 140ms ease;
}
.slot.is-ready { border-color: var(--muted); }
.slot.is-over { border-style: solid; border-color: var(--ink); transform: scale(1.04); background: #fff8e3; }
.slot.is-filled { border-style: solid; border-color: var(--leaf); background: var(--leaf-soft); cursor: default; animation: pop 260ms ease; }
.app.is-complete .slot.is-filled { border-color: var(--leaf); box-shadow: 0 0 0 4px color-mix(in srgb, var(--leaf) 25%, transparent); }

.replay-btn {
  min-height: var(--tap);
  padding: 0 22px;
  border-radius: var(--radius);
  border: 3px solid var(--leaf);
  background: var(--leaf);
  color: #fff;
  font: 800 1.1rem/1 var(--font-ui);
  display: inline-flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
}

.next-btn { background: var(--bike); border-color: var(--bike); }

/* ---- settings dialog ------------------------------------------------------ */
.settings {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  background: rgb(31 42 55 / 0.35);
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.settings-card {
  width: min(420px, 100%);
  background: var(--paper);
  border-radius: 22px;
  padding: 24px;
  display: grid;
  gap: 18px;
  user-select: text;
}
.settings-card h2 { margin: 0; font: 800 1.35rem/1.2 var(--font-ui); text-wrap: balance; }
.field { display: grid; gap: 8px; }
.field-head { display: flex; justify-content: space-between; font-weight: 700; }
.field-head output { font-variant-numeric: tabular-nums; color: var(--muted); }
.field input[type="range"] { width: 100%; accent-color: var(--bike); height: 28px; }
.voice-note { margin: 0; color: var(--muted); font-size: 0.9rem; line-height: 1.45; }
.settings-actions { display: flex; gap: 12px; justify-content: flex-end; flex-wrap: wrap; }
.btn { min-height: 44px; padding: 0 18px; border-radius: 12px; font: 700 1rem/1 var(--font-ui); cursor: pointer; border: 2px solid var(--ink); background: var(--paper); color: var(--ink); }
.btn-primary { background: var(--ink); color: var(--paper); }

.error { position: fixed; inset: auto 16px 16px; padding: 14px 18px; border-radius: 14px; background: var(--paper); border: 2px solid #c2410c; color: var(--ink); }

/* ---- wrong placement ---------------------------------------------------- */
.slot.is-nudged { animation: wobble 380ms ease, wrong-flash 700ms ease; }

/* ---- start gate (first tap unlocks sound) --------------------------------- */
.start {
  position: fixed;
  inset: 0;
  z-index: 65;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--ground) 82%, transparent);
  backdrop-filter: blur(4px);
}
.start-btn {
  display: grid;
  justify-items: center;
  gap: 6px;
  padding: 28px 44px 22px;
  border-radius: 36px;
  border: 4px solid var(--ink);
  background: var(--sun);
  color: var(--ink);
  font: 900 1.6rem/1 var(--font-ui);
  box-shadow: 0 6px 0 var(--sun-deep);
  cursor: pointer;
  animation: breathe 2.4s ease-in-out infinite;
}
.start-btn:active { transform: translateY(3px); box-shadow: 0 3px 0 var(--sun-deep); }
.start-btn:focus-visible { outline: 4px solid var(--bike); outline-offset: 4px; }

/* ---- fireworks overlay ----------------------------------------------------- */
.fireworks { position: fixed; inset: 0; width: 100%; height: 100%; z-index: 55; pointer-events: none; }

/* ---- celebration --------------------------------------------------------- */
.sparkles { position: absolute; inset: 0; pointer-events: none; }
.sparkles i { position: absolute; width: 14px; height: 14px; border-radius: 50%; opacity: 0; background: var(--sun); }
.app.is-complete .sparkles i { animation: float 1.6s ease-out forwards; }
.sparkles i:nth-child(2n) { background: var(--bike); }
.sparkles i:nth-child(3n) { background: var(--leaf); }

@keyframes glow { 50% { box-shadow: 0 0 0 9px color-mix(in srgb, var(--bike) 18%, transparent); } }
@keyframes wobble { 25% { transform: translateX(-6px); } 50% { transform: translateX(5px); } 75% { transform: translateX(-3px); } }
@keyframes wrong-flash { 0%, 60% { border-color: var(--berry); border-style: solid; background: var(--berry-soft); } }
@keyframes breathe { 50% { transform: scale(1.05); } }
@keyframes pop { from { transform: scale(0.94); } to { transform: scale(1); } }
@keyframes float { 0% { opacity: 0; transform: translateY(0) scale(0.6); } 20% { opacity: 1; } 100% { opacity: 0; transform: translateY(-120px) scale(1); } }

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

/* ---- tablet portrait ------------------------------------------------------- */
/* Phones are landscape-only (see .rotate-hint below); this layout is for upright tablets. */
@media (orientation: portrait) and (min-width: 601px) and (max-width: 900px) {
  .app {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas:
      "bank  tools"
      "stage stage"
      "tray  tray";
  }
  .bank { flex-direction: row; flex-wrap: wrap; justify-content: flex-start; gap: 10px; }
  .word { padding: 0 14px; }
  .tools { flex-direction: row-reverse; align-items: flex-start; justify-content: flex-start; }
  .help-btn { width: 64px; }
  .tray { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .picture-hint { font-size: 0.75rem; }
}

/* ---- phone landscape: compact, the sentence reads left to right in one row ---- */
@media (orientation: landscape) and (max-height: 520px) {
  :root { --tap: 42px; --word-size: clamp(0.85rem, 0.5rem + 1.6vh, 1.05rem); --radius: 12px; }
  .app {
    grid-template-columns: minmax(118px, 160px) minmax(0, 1fr) auto;
    gap: 8px 12px;
    padding-block: 8px;
  }
  .bank { gap: 6px; }
  .word { padding: 0 8px; letter-spacing: 0.02em; border-width: 2px; box-shadow: 0 3px 0 var(--sun-deep); }
  .tray { grid-template-columns: repeat(auto-fit, minmax(84px, 1fr)); gap: 8px; }
  .slot { min-height: calc(var(--tap) + 4px); letter-spacing: 0.02em; border-width: 2px; padding: 0 4px; }
  .help-btn { width: 54px; border-radius: 16px; font-size: 1.8rem; }
  .gear-btn { width: 40px; height: 40px; }
  .picture { padding: 6px; border-radius: 18px; }
  .picture img { inset: 6px 6px 30px; width: calc(100% - 12px); height: calc(100% - 36px); }
  .picture-hint { bottom: 6px; font-size: 0.7rem; padding: 4px 10px; }
  .replay-btn { min-height: var(--tap); padding: 0 14px; font-size: 0.95rem; }
  .start-btn { padding: 18px 32px 14px; font-size: 1.3rem; }
}

/* ---- phone held upright: ask to rotate ---------------------------------------- */
.rotate-hint { display: none; }
@media (orientation: portrait) and (max-width: 600px) {
  .rotate-hint {
    position: fixed;
    inset: 0;
    z-index: 80;
    display: grid;
    place-content: center;
    justify-items: center;
    gap: 18px;
    padding-left: max(16px, env(safe-area-inset-left, 0px));
    padding-right: max(16px, env(safe-area-inset-right, 0px));
    background: var(--ground);
    color: var(--ink);
    font: 900 1.6rem/1.2 var(--font-ui);
    text-align: center;
  }
  .rotate-hint svg { width: 132px; height: 132px; animation: tilt 2.4s ease-in-out infinite; }
}
@keyframes tilt { 0%, 30% { transform: rotate(0deg); } 55%, 100% { transform: rotate(-90deg); } }
