/* ==========================================================================
   Mal dein Spiel — Gestaltung

   Grundsätze:
   - Große Flächen zum Antippen. Kinderfinger sind ungenau, und ein Tablet
     wird oft im Stehen bedient. Nichts Anklickbares unter 48 Pixel.
   - Kräftige Kontraste. Klassenzimmer haben Fensterlicht und Beamer-Dämmerung.
   - Farbe wird nie als einziges Unterscheidungsmerkmal benutzt — Gefahrenfelder
     haben zusätzlich ein Kreuz, das Ziel einen Stern.
   ========================================================================== */

:root {
  --blau:        #2563eb;
  --blau-dunkel: #1d4ed8;
  --gruen:       #16a34a;
  --gelb:        #facc15;
  --rot:         #dc2626;
  --schwarz:     #1f2430;

  --text:        #1f2430;
  --text-leise:  #5b6474;
  --papier:      #ffffff;
  --hintergrund: #f4f6fb;
  --rand:        #d8dee9;

  --radius:      14px;
  --radius-klein: 10px;
  --schatten:    0 2px 8px rgba(31, 36, 48, 0.08);
  --schatten-hoch: 0 6px 20px rgba(31, 36, 48, 0.14);

  --schrift: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html {
  /* Verhindert, dass iOS beim Drehen die Schrift eigenmächtig vergrößert */
  -webkit-text-size-adjust: 100%;

  /* Kein Überziehen am Seitenende und kein Pull-to-Refresh beim Malen.
     Gehört auf html, nicht nur auf body — sonst greift es auf iOS nicht. */
  overscroll-behavior: none;
}

body {
  margin: 0;
  font-family: var(--schrift);
  font-size: 17px;
  line-height: 1.55;
  color: var(--text);
  background: var(--hintergrund);

  /* Kein Gummiband-Effekt beim Wischen auf dem Spielfeld */
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;

  padding:
    env(safe-area-inset-top) env(safe-area-inset-right)
    env(safe-area-inset-bottom) env(safe-area-inset-left);
}

h1, h2, h3 { line-height: 1.25; margin: 0 0 0.5em; }

/* Nur für Screenreader sichtbar */
.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;
}

/* --- Bildschirme ---------------------------------------------------------- */

.screen { display: none; min-height: 100vh; min-height: 100dvh; }
.screen--aktiv { display: flex; flex-direction: column; }

.inhalt {
  flex: 1;
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px 18px 48px;
}
.inhalt--schmal { max-width: 680px; }

/* --- Startseite ----------------------------------------------------------- */

.kopf { text-align: center; padding: 40px 18px 24px; }

.logo {
  font-size: clamp(2.1rem, 7vw, 3.2rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  margin-bottom: 0.15em;
  color: var(--blau);
}

.untertitel { margin: 0; color: var(--text-leise); font-size: 1.1rem; }

.kacheln {
  display: grid;
  gap: 14px;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  padding: 0 18px;
}

.kachel {
  display: grid;
  grid-template-columns: 56px 1fr;
  grid-template-rows: auto auto;
  gap: 2px 16px;
  align-items: center;
  text-align: left;

  min-height: 96px;
  padding: 18px 20px;
  border: 2px solid transparent;
  border-radius: var(--radius);
  background: var(--papier);
  box-shadow: var(--schatten);
  cursor: pointer;
  font-family: inherit;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}

.kachel:hover  { box-shadow: var(--schatten-hoch); }
.kachel:active { transform: scale(0.985); }
.kachel:focus-visible { outline: 3px solid var(--blau); outline-offset: 3px; }

.kachel__icon {
  grid-row: 1 / 3;
  display: grid;
  place-items: center;
  width: 56px; height: 56px;
  border-radius: 50%;
  font-size: 1.5rem;
  color: #fff;
}

.kachel--blau  .kachel__icon { background: var(--blau); }
.kachel--gruen .kachel__icon { background: var(--gruen); }
.kachel--gelb  .kachel__icon { background: #b58900; }
.kachel--grau  .kachel__icon { background: var(--text-leise); }

.kachel__titel { font-size: 1.2rem; font-weight: 700; }
.kachel__text  { font-size: 0.94rem; color: var(--text-leise); }

.datenschutz-hinweis {
  max-width: 680px;
  margin: 30px auto 0;
  padding: 16px 20px;
  border-radius: var(--radius);
  background: #eef4ff;
  border: 1px solid #cfe0ff;
  color: #23324d;
  font-size: 0.95rem;
}

.fortsetzen {
  max-width: 680px;
  margin: 16px auto 0;
  padding: 14px 20px;
  border-radius: var(--radius);
  background: #fff8e1;
  border: 1px solid #ffe9a8;
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
}
.fortsetzen p { margin: 0; flex: 1 1 200px; }

.fusszeile { text-align: center; padding: 30px 18px 24px; margin-top: auto; }

.link {
  background: none; border: 0; padding: 8px 12px;
  color: var(--text-leise); text-decoration: underline;
  font-family: inherit; font-size: 0.92rem; cursor: pointer;
}
.link:hover { color: var(--text); }

/* --- Kopfleiste in Unterseiten -------------------------------------------- */

.leiste {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 12px 16px;
  background: var(--papier);
  border-bottom: 1px solid var(--rand);
  position: sticky; top: 0; z-index: 10;
}
.leiste h2 { margin: 0; font-size: 1.15rem; flex: 1; }

.punktestand {
  font-weight: 700; font-size: 1.05rem;
  padding: 6px 14px; border-radius: 999px; background: #fff8e1;
  border: 1px solid #ffe9a8; white-space: nowrap;
}

/* --- Knöpfe --------------------------------------------------------------- */

.knopf {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px;
  padding: 12px 20px;
  border: 0; border-radius: var(--radius-klein);
  background: var(--blau); color: #fff;
  font-family: inherit; font-size: 1rem; font-weight: 600;
  cursor: pointer;
  transition: background 0.12s ease, transform 0.1s ease;
}
.knopf:hover  { background: var(--blau-dunkel); }
.knopf:active { transform: scale(0.97); }
.knopf:focus-visible { outline: 3px solid var(--schwarz); outline-offset: 2px; }
.knopf:disabled { background: #b7bfcd; cursor: not-allowed; transform: none; }

.knopf--gross { min-height: 58px; font-size: 1.1rem; padding: 14px 28px; }
.knopf--klein { min-height: 42px; padding: 8px 16px; font-size: 0.94rem; }

.knopf--still {
  background: var(--papier); color: var(--text);
  border: 2px solid var(--rand);
}
.knopf--still:hover { background: #eef1f7; }

.knopf--zurueck { background: none; color: var(--blau); padding: 10px 12px; }
.knopf--zurueck:hover { background: #eef4ff; }

.knopf--start { background: var(--gruen); }
.knopf--start:hover { background: #12833c; }

.knopf--datei { cursor: pointer; }

.knopfreihe { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 20px; }

/* --- Spielbildschirm ------------------------------------------------------ */

.spielfeld-bereich,
.malbereich {
  flex: 1;
  display: grid; place-items: center;
  padding: 16px;
  min-height: 200px;
}

#spiel-canvas, #mal-canvas {
  border-radius: 10px;
  box-shadow: var(--schatten-hoch);
  background: #fff;

  /* Hier ist `none` richtig: Auf dem Spielfeld soll jede Fingerbewegung malen
     beziehungsweise wischen, nicht die Seite verschieben. */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

/* Das Malraster ist per Tabulator erreichbar — dann muss man auch sehen,
   dass es den Fokus hat. */
#mal-canvas:focus-visible {
  outline: 3px solid var(--blau);
  outline-offset: 3px;
}

.spielmeldung, .malmeldung {
  margin: 0; padding: 0 18px 8px;
  text-align: center; font-weight: 600; min-height: 1.6em;
}
.spielmeldung--gewonnen { color: var(--gruen); }
.spielmeldung--verloren { color: var(--rot); }

.spiel-steuerung {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center;
  gap: 24px; padding: 8px 18px 20px;
}

/* Steuerkreuz: bewusst groß, damit es auch im Stehen mit dem Daumen geht.

   `touch-action: manipulation` statt `none`: Es unterdrückt den Doppeltipp-Zoom
   (und damit die spürbare Verzögerung auf iOS), lässt aber das Aufziehen mit
   zwei Fingern zu. Ein hartes `none` würde das Vergrößern der Seite verhindern
   — schlecht für Kinder, die genauer hinsehen wollen, und ein
   Barrierefreiheits-Problem. Nur auf dem Malraster selbst ist `none` richtig,
   weil dort jede Fingerbewegung malen soll. */
.dpad {
  display: grid;
  grid-template-columns: repeat(3, 68px);
  grid-template-rows: repeat(3, 68px);
  gap: 6px;
  touch-action: manipulation;
}

.dpad__knopf {
  border: 0; border-radius: 14px;
  background: var(--papier);
  box-shadow: var(--schatten);
  font-size: 1.5rem; color: var(--blau);
  cursor: pointer;
  display: grid; place-items: center;
  touch-action: manipulation;
  transition: background 0.1s ease, transform 0.08s ease;
}
.dpad__knopf:focus-visible { outline: 3px solid var(--blau); outline-offset: 2px; }
.dpad__knopf:active { background: var(--blau); color: #fff; transform: scale(0.94); }

.dpad__knopf--hoch   { grid-area: 1 / 2; }
.dpad__knopf--links  { grid-area: 2 / 1; }
.dpad__knopf--rechts { grid-area: 2 / 3; }
.dpad__knopf--runter { grid-area: 3 / 2; }

.spiel-knoepfe { display: flex; flex-direction: column; gap: 10px; }

.tastenhinweis {
  text-align: center; color: var(--text-leise);
  font-size: 0.88rem; padding: 0 18px 20px; margin: 0;
}

/* --- Malmodus ------------------------------------------------------------- */

.werkzeugleiste {
  display: flex; flex-wrap: wrap; gap: 10px; justify-content: center;
  padding: 8px 16px 4px;
}

.werkzeug {
  display: flex; align-items: center; gap: 10px;
  min-height: 52px; padding: 8px 16px 8px 8px;
  border: 3px solid transparent; border-radius: 999px;
  background: var(--papier); box-shadow: var(--schatten);
  font-family: inherit; font-size: 0.98rem; font-weight: 600;
  cursor: pointer;
}
.werkzeug__punkt {
  width: 34px; height: 34px; border-radius: 50%;
  border: 2px solid rgba(0,0,0,0.14);
}
.werkzeug[aria-pressed="true"] {
  border-color: var(--blau);
  background: #eef4ff;
}
.werkzeug:focus-visible { outline: 3px solid var(--blau); outline-offset: 2px; }

.mal-knoepfe {
  display: flex; flex-wrap: wrap; gap: 12px;
  align-items: center; justify-content: center;
  padding: 12px 18px 28px;
}

.rastergroesse {
  display: inline-flex; align-items: center; flex-wrap: wrap; gap: 8px 14px;
  font-size: 0.94rem; color: var(--text-leise);
}
.rastergroesse label { display: inline-flex; align-items: center; gap: 6px; }

.rastergroesse input[type="number"] {
  width: 76px; min-height: 44px; padding: 8px 10px;
  border: 2px solid var(--rand); border-radius: var(--radius-klein);
  background: var(--papier); font-family: inherit; font-size: 1rem;
  color: var(--text); text-align: center;
}
.rastergroesse input:focus-visible { outline: 3px solid var(--blau); outline-offset: 1px; }

/* --- Level- und Farblisten ------------------------------------------------ */

.levelliste { list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; }

.levelknopf {
  width: 100%; text-align: left;
  padding: 18px 22px; min-height: 76px;
  border: 2px solid transparent; border-radius: var(--radius);
  background: var(--papier); box-shadow: var(--schatten);
  font-family: inherit; cursor: pointer;
}
.levelknopf:hover { box-shadow: var(--schatten-hoch); }
.levelknopf strong { display: block; font-size: 1.12rem; margin-bottom: 2px; }
.levelknopf span   { color: var(--text-leise); font-size: 0.94rem; }

.farbliste { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.farbliste li { display: flex; align-items: center; gap: 14px; }
.farbprobe {
  width: 40px; height: 40px; border-radius: 8px; flex: none;
  border: 2px solid rgba(0,0,0,0.16);
}
.farbliste b { display: block; }
.farbliste small { color: var(--text-leise); }

/* --- Karten und Text ------------------------------------------------------ */

.karte {
  background: var(--papier);
  border: 1px solid var(--rand);
  border-radius: var(--radius);
  padding: 20px 22px;
  margin-bottom: 16px;
}
.karte h3 { margin-top: 0; }
.karte__vorspann { color: var(--text-leise); margin-top: -0.3em; }
.karte--tipp { background: #fff8e1; border-color: #ffe9a8; }

.fragenliste, .schrittliste { padding-left: 1.3em; }
.fragenliste li, .schrittliste li { margin-bottom: 0.5em; }

.anleitung { max-width: 60ch; }
.hinweis-klein { color: var(--text-leise); font-size: 0.9rem; }

.ergebnis-zeile { font-size: 1.15rem; font-weight: 600; }

/* --- Foto ----------------------------------------------------------------- */

.foto-buehne {
  display: grid; place-items: center;
  padding: 12px; margin: 14px 0;
  background: #e9edf5; border-radius: var(--radius);
}
#foto-canvas {
  max-width: 100%; border-radius: 8px;
  box-shadow: var(--schatten); touch-action: none; cursor: crosshair;
}

.vergleich {
  display: grid; gap: 18px;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  margin: 16px 0;
}
.vergleich__seite { text-align: center; }
.vergleich__seite h3 { font-size: 1rem; color: var(--text-leise); }
.vergleich canvas {
  max-width: 100%; border-radius: 8px; box-shadow: var(--schatten); background: #fff;
}

.erkennungs-info {
  padding: 12px 16px; border-radius: var(--radius-klein);
  background: #eef4ff; border: 1px solid #cfe0ff; font-size: 0.95rem;
}

.fehlerbox {
  padding: 16px 20px; border-radius: var(--radius);
  background: #fdecec; border: 1px solid #f6bcbc; color: #8d1f1f;
}

/* --- Statusleiste und Debug ----------------------------------------------- */

.statusleiste {
  position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%);
  z-index: 50; max-width: min(92vw, 560px);
  padding: 12px 22px; border-radius: 999px;
  background: var(--schwarz); color: #fff;
  font-size: 0.95rem; box-shadow: var(--schatten-hoch);
}

.debug {
  position: fixed; right: 12px; bottom: 12px; z-index: 40;
  max-width: min(94vw, 460px);
  background: var(--papier); border: 1px solid var(--rand);
  border-radius: var(--radius-klein); font-size: 0.82rem;
}
.debug summary {
  cursor: pointer; padding: 8px 14px; color: var(--text-leise); user-select: none;
}
.debug pre {
  margin: 0; padding: 0 14px 12px;
  max-height: 46vh; overflow: auto;
  white-space: pre-wrap; word-break: break-word;
  font-size: 0.78rem; line-height: 1.45;
}

/* --- Kleine Bildschirme --------------------------------------------------- */

@media (max-width: 560px) {
  body { font-size: 16px; }
  .kopf { padding: 26px 16px 18px; }
  .dpad { grid-template-columns: repeat(3, 62px); grid-template-rows: repeat(3, 62px); }
  .spiel-steuerung { gap: 16px; }

  /* Der Titel rutscht in eine eigene Zeile unter Zurück-Knopf und Punktestand.
     Wichtig: flex-basis statt width — die Grundregel setzt `flex: 1` und
     damit flex-basis auf 0, was ein width:100% wirkungslos macht. */
  .leiste h2 { font-size: 1.05rem; flex-basis: 100%; order: 3; }

  /* Platz für den eingeklappten Debug-Kasten, der unten rechts sitzt */
  .tastenhinweis { padding-bottom: 58px; }
  .debug { font-size: 0.76rem; max-width: 88vw; }
}

/* Auf niedrigen, breiten Bildschirmen (Tablet quer) das Steuerkreuz
   neben das Spielfeld statt darunter — sonst passt beides nicht. */
@media (min-width: 820px) and (max-height: 720px) {
  #screen-spiel.screen--aktiv { flex-direction: column; }
  .spiel-steuerung { padding-top: 0; }
}

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