/* Uitvinderij Stoetman — gedeelde stijl */

:root {
  --ink: #2b2118;
  --ink-soft: #5a4a3c;
  --paper: #f7f0df;
  --paper-dark: #eadfc4;
  --white: #fffaf0;
  --red: #d9483b;
  --mustard: #f2c14e;
  --yellow: #ffe17a;
  --teal: #3fa7a0;
  --purple: #7b4fa3;
  --green: #6fae5b;

  --font-hand: 'Patrick Hand', 'Comic Sans MS', 'Chalkboard SE', cursive;
  --font-title: 'Chewy', 'Patrick Hand', 'Comic Sans MS', cursive;
  --font-sign: 'Rye', 'Chewy', Georgia, serif;

  /* Handgetekende rand: ongelijke hoeken */
  --wobbly-radius: 255px 18px 225px 18px / 18px 225px 18px 255px;
  --wobbly-radius-2: 18px 225px 18px 255px / 255px 18px 225px 18px;
}

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

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background-color: var(--paper);
  /* Papierkorrel */
  background-image:
    radial-gradient(ellipse at 50% 40%, rgba(255, 250, 235, 0.9), rgba(234, 223, 196, 0.6) 70%, rgba(200, 182, 150, 0.5)),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.35 0 0 0 0 0.28 0 0 0 0 0.2 0 0 0 0.12 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  color: var(--ink);
  font-family: var(--font-hand);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

.svg-defs {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Scènes ---------- */

#app {
  position: fixed;
  inset: 0;
}

.scene {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  will-change: transform, opacity;
}

.scene[hidden] {
  display: none;
}

.scene-svg {
  width: 100%;
  height: 100%;
  display: block;
  /* Tekeningen mogen buiten de viewBox doorlopen, zodat er op elk schermformaat geen lege rand is */
  overflow: visible;
  user-select: none;
  -webkit-user-select: none;
}

/* Hulpklassen voor SVG-animaties: draaien/schalen rond het eigen midden of de onderkant */
.scene-svg .fb,
#viewer-stage .fb,
svg .fb {
  transform-box: fill-box;
  transform-origin: center;
}

svg .fb-bottom {
  transform-box: fill-box;
  transform-origin: 50% 100%;
}

svg .fb-top {
  transform-box: fill-box;
  transform-origin: 50% 0%;
}

/* Klikbare dingen in een tekening */
svg .clickable {
  cursor: pointer;
}

svg .clickable:focus {
  outline: none;
}

svg .clickable:focus-visible .focus-ring,
svg .clickable:focus-visible > .focus-ring {
  opacity: 1;
}

svg .focus-ring {
  opacity: 0;
  fill: none;
  stroke: var(--mustard);
  stroke-width: 6;
  stroke-dasharray: 14 10;
  pointer-events: none;
}

/* ---------- Knoppen rechtsboven / linksboven ---------- */

#hud {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  pointer-events: none;
  z-index: 20;
}

#hud > * {
  pointer-events: auto;
}

#btn-sound {
  margin-left: auto;
}

.hud-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 48px;
  min-width: 48px;
  padding: 6px 16px;
  font-family: var(--font-hand);
  font-size: 22px;
  color: var(--ink);
  background: var(--white);
  border: 3px solid var(--ink);
  border-radius: var(--wobbly-radius);
  box-shadow: 3px 4px 0 var(--ink);
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.hud-btn svg {
  width: 26px;
  height: 26px;
  flex: none;
}

.hud-btn:hover {
  transform: translate(-1px, -2px) rotate(-1deg);
  box-shadow: 4px 6px 0 var(--ink);
}

.hud-btn:active {
  transform: translate(2px, 3px);
  box-shadow: 1px 1px 0 var(--ink);
}

.hud-btn[hidden] {
  display: none;
}

#btn-sound .snd-off {
  display: none;
}

#btn-sound[aria-pressed='false'] .snd-waves {
  display: none;
}

#btn-sound[aria-pressed='false'] .snd-off {
  display: inline;
}

/* ---------- Kijkkaart (de grote versie van een uitvinding) ---------- */

#viewer {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
}

#viewer[hidden] {
  display: none;
}

.viewer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(43, 33, 24, 0.55);
  backdrop-filter: blur(2px);
}

.viewer-card {
  /* Hoogte die de rest van de kaart (titel, uitleg, knoppen, randen) ongeveer nodig heeft; de rest is voor het plaatje */
  --stage-reserve: 250px;
  position: relative;
  width: min(1100px, 100%);
  max-height: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 14px 20px 16px;
  background: var(--paper);
  background-image:
    linear-gradient(rgba(63, 167, 160, 0.09) 1px, transparent 1px),
    linear-gradient(90deg, rgba(63, 167, 160, 0.09) 1px, transparent 1px);
  background-size: 28px 28px;
  border: 3px solid var(--ink);
  border-radius: 12px 26px 14px 22px / 22px 12px 26px 14px;
  box-shadow: 6px 8px 0 rgba(43, 33, 24, 0.85);
}

/* Plakbandjes op de hoeken */
.viewer-card::before,
.viewer-card::after {
  content: '';
  position: absolute;
  width: 110px;
  height: 30px;
  background: rgba(242, 193, 78, 0.6);
  border: 1px dashed rgba(43, 33, 24, 0.25);
  top: -14px;
  pointer-events: none;
}

.viewer-card::before {
  left: 24px;
  transform: rotate(-6deg);
}

.viewer-card::after {
  right: 24px;
  transform: rotate(5deg);
}

.viewer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

#viewer-title {
  margin: 0;
  font-family: var(--font-title);
  font-weight: 400;
  font-size: clamp(28px, 4.2vw, 46px);
  line-height: 1.05;
  letter-spacing: 0.5px;
}

#viewer-desc {
  margin: 0;
  font-size: clamp(17px, 2vw, 22px);
  line-height: 1.3;
  color: var(--ink-soft);
}

.viewer-stage-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  justify-content: center;
}

#viewer-stage {
  display: block;
  /* Altijd precies 5:3 (net als de viewBox): als de hoogte beperkt is, wordt hij ook smaller, zodat je nooit
     witte stroken of tekening van buiten de viewBox naast het plaatje ziet. */
  width: 100%;
  max-width: calc((100vh - var(--stage-reserve)) * 5 / 3);
  max-width: calc((100dvh - var(--stage-reserve)) * 5 / 3);
  height: auto;
  max-height: calc(100vh - var(--stage-reserve));
  max-height: calc(100dvh - var(--stage-reserve));
  aspect-ratio: 1000 / 600;
  background: var(--white);
  border: 3px solid var(--ink);
  border-radius: 8px 16px 10px 14px / 14px 8px 16px 10px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.viewer-caption {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  /* max-content: anders wordt een absoluut geplaatst blokje met left: 50% maar half zo breed als de stage en
     breekt elke zin in twee hoge regels. Niet breder dan 90% van het plaatje. */
  width: max-content;
  max-width: min(90%, calc((100vh - var(--stage-reserve)) * 1.5));
  max-width: min(90%, calc((100dvh - var(--stage-reserve)) * 1.5));
  text-wrap: balance;
  line-height: 1.2;
  padding: 6px 16px;
  font-size: clamp(18px, 2.2vw, 26px);
  text-align: center;
  background: var(--white);
  border: 3px solid var(--ink);
  border-radius: var(--wobbly-radius-2);
  box-shadow: 3px 3px 0 var(--ink);
  pointer-events: none;
}

.viewer-caption:empty {
  display: none;
}

.viewer-controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px 14px;
  padding-top: 4px;
}

.viewer-controls:empty {
  display: none;
}

/* Grote, stevige knoppen voor kinderhanden */
.kid-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 52px;
  padding: 6px 20px;
  font-family: var(--font-hand);
  font-size: clamp(19px, 2.2vw, 24px);
  line-height: 1;
  color: var(--ink);
  background: var(--yellow);
  border: 3px solid var(--ink);
  border-radius: var(--wobbly-radius);
  box-shadow: 3px 4px 0 var(--ink);
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background-color 0.2s;
  touch-action: manipulation;
}

.kid-btn:nth-child(3n + 2) {
  background: #bfe6e2;
  border-radius: var(--wobbly-radius-2);
}

.kid-btn:nth-child(3n + 3) {
  background: #f6c6d6;
}

.kid-btn:hover {
  transform: translate(-1px, -2px) rotate(-1.5deg);
  box-shadow: 4px 6px 0 var(--ink);
}

.kid-btn:active,
.kid-btn[aria-pressed='true'] {
  transform: translate(2px, 3px);
  box-shadow: 1px 1px 0 var(--ink);
}

.kid-btn[aria-pressed='true'] {
  background: var(--mustard);
}

.kid-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.kid-btn svg {
  width: 24px;
  height: 24px;
  flex: none;
}

.kid-btn--close {
  background: var(--white);
  flex: none;
}

button:focus-visible {
  outline: 4px dashed var(--teal);
  outline-offset: 4px;
}

.noscript {
  position: fixed;
  inset: 40% 10% auto;
  text-align: center;
  font-size: 28px;
}

/* ---------- Kleine schermen ---------- */

@media (max-width: 640px), (max-height: 520px) {
  .hud-btn {
    font-size: 18px;
    min-height: 44px;
    padding: 4px 12px;
  }

  #viewer {
    padding: 8px;
  }

  .viewer-card {
    --stage-reserve: 170px;
    padding: 10px 10px 12px;
  }

  #viewer-desc {
    display: none;
  }

  .kid-btn {
    min-height: 46px;
    padding: 4px 14px;
  }

  /* Kleiner tekstballonnetje: op een klein plaatje mag het niet de helft bedekken */
  .viewer-caption {
    font-size: 16px;
    padding: 3px 10px;
    bottom: 6px;
    border-width: 2px;
    box-shadow: 2px 2px 0 var(--ink);
  }
}

/* Telefoon (of tablet) rechtop: het tekstballonnetje onder het plaatje in plaats van eroverheen. Er is ruimte
   genoeg onder het plaatje, en zo blijft het hele (smalle) plaatje zichtbaar. */
@media (max-aspect-ratio: 4/5) {
  .viewer-stage-wrap {
    flex-direction: column;
    align-items: center;
  }

  /* id-selector: wint ook van de eigen onderschrift-regels van een uitvinding */
  #viewer-stage-wrap > #viewer-caption {
    position: relative;
    /* left: 50% + translateX(-50%) (ook in de 'plop' van ui.say) = netjes in het midden */
    left: 50%;
    bottom: auto;
    align-self: flex-start;
    margin-top: 8px;
    max-width: 100%;
  }

  /* Leeg = onzichtbaar maar ruimte houden, zodat de kaart niet verspringt als er een zinnetje bijkomt */
  #viewer-stage-wrap > #viewer-caption:empty {
    display: block;
    visibility: hidden;
    min-height: calc(1.2em + 12px);
  }
}

/* Telefoon dwars (of een heel laag venster): plaatje links zo hoog als het scherm, titel, Terug en de knoppen
   in een kolom rechts. Anders blijft er maar een smalle strook over voor het plaatje. */
@media (max-height: 520px) and (orientation: landscape) {
  #viewer {
    padding: 6px;
  }

  .viewer-card {
    --stage-reserve: 50px;
    display: grid;
    /* smalle kolom rechts: elke pixel die we hier sparen, gaat naar het plaatje */
    grid-template-columns: minmax(0, 1fr) clamp(128px, 21vw, 200px);
    grid-template-rows: auto minmax(0, 1fr);
    column-gap: 12px;
    row-gap: 8px;
    padding: 8px 10px;
  }

  .viewer-head {
    grid-column: 2;
    grid-row: 1;
    flex-direction: column-reverse;
    align-items: stretch;
    gap: 8px;
    min-width: 0;
  }

  #viewer-title {
    font-size: 20px;
    text-align: center;
  }

  .viewer-stage-wrap {
    grid-column: 1;
    grid-row: 1 / span 2;
    align-items: center;
  }

  .viewer-controls {
    grid-column: 2;
    grid-row: 2;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-content: flex-start;
    gap: 8px;
    padding-top: 0;
  }

  .viewer-card .kid-btn {
    min-height: 40px;
    padding: 4px 10px;
    font-size: 17px;
    line-height: 1.05;
    text-align: center;
    white-space: normal;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
  }

  .hud-btn,
  .kid-btn {
    transition: none;
  }
}
