@font-face {
  font-family: "Avela Rechnen";
  src: url("fonts/AvelaRechnen-Regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Avela Rechnen";
  src: url("fonts/AvelaRechnen-Bold.woff2") format("woff2");
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #62646e;
  --paper: #ffffff;
  --ink: #1f2933;
  --muted: #6b7280;
  --line: #e0e0e0;
  --strong-line: #2a2a2a;
  --surface: #ffffff;
  --surface-2: #e8e8e8;
  --green: #2f9e61;
  --danger: #c2413b;
  --radius: 8px;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.08);

  /* Stellenfarben: Einer, Zehner, Hunderter, Tausender, Zehntausender, Hunderttausender */
  --place-0: #b7791f;
  --place-1: #c2413b;
  --place-2: #2f9e61;
  --place-3: #2563eb;
  --place-4: #7c3aed;
  --place-5: #0891b2;
  --place-0-soft: #fbe6a6;
  --place-1-soft: #f6c9c6;
  --place-2-soft: #c5e8d3;
  --place-3-soft: #c9d9fb;
  --place-4-soft: #ddd0fa;
  --place-5-soft: #c3e7f0;

  --focus: #db2777;
  --digit-font: "Avela Rechnen", ui-rounded, system-ui, sans-serif;
  --cell: clamp(30px, 3.2vw, 44px);
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--ink);
  font-family: "SF Pro Display", "SF Pro Text", -apple-system, BlinkMacSystemFont, system-ui, "Segoe UI", Inter, ui-sans-serif, sans-serif;
  font-size: 15px;
  -webkit-text-size-adjust: 100%;
}

.app-frame {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
  gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}

button, input, select { font: inherit; color: inherit; }

button {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--green); border-color: var(--green); color: #fff; }
button.primary:hover { filter: brightness(1.06); }
button:disabled { opacity: 0.4; cursor: default; }

input[type="text"], select {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 6px 10px;
  min-height: 36px;
}
input[type="text"]:focus, select:focus, button:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--green) 60%, transparent);
  outline-offset: 1px;
}

.icon-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  display: inline-grid;
  place-items: center;
}
.icon-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Feste Rasterzeilen: Die Fehlermeldung ist meist ausgeblendet und darf die Bühne nicht verschieben. */
.topbar { grid-row: 1; }
.task-error { grid-row: 2; }
.stage { grid-row: 3; }
.verbal { grid-row: 4; }
.verbal { gap: 12px; flex-wrap: wrap; }
.verbal-text { flex: 1 1 320px; }

/* Zwischenfragen */
.question-toggle[aria-pressed="true"] { background: var(--green); border-color: var(--green); color: #fff; }
.popover-section { margin: 8px 0 2px; padding-top: 8px; border-top: 1px solid var(--line); font-size: 13px; color: var(--muted); }

.q-badge { padding: 3px 10px; border-radius: 999px; font-size: 15px; white-space: nowrap; border: 1px solid transparent; }
.q-badge.is-ok { background: color-mix(in srgb, var(--green) 14%, #fff); border-color: color-mix(in srgb, var(--green) 45%, #fff); color: #1d6a40; }
.q-badge.is-shown { background: #fff4d6; border-color: #e7c66a; color: #7a5600; white-space: normal; }
.q-score { font-size: 15px; color: var(--muted); }

.q-bar { flex: 1 1 100%; display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; }
.q-main { flex: 1 1 280px; }
.q-prompt { font-weight: 600; }
.q-feedback { min-height: 1.3em; font-size: 15px; color: var(--danger); }
.q-pad { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.q-pad button { min-width: 38px; min-height: 38px; padding: 4px 8px; font-family: var(--digit-font); font-size: 20px; }
.q-pad button.primary { font-family: inherit; font-size: 15px; padding: 4px 14px; }
.q-value {
  min-width: 76px;
  min-height: 38px;
  margin-right: 6px;
  padding: 2px 10px;
  display: inline-grid;
  place-items: center;
  border: 2px solid var(--focus);
  border-radius: var(--radius);
  font-family: var(--digit-font);
  font-size: 22px;
}

/* Während einer Antippfrage zeigen die Tafeln, dass sie die Antwort erwarten. */
.app-frame.is-picking .panel { outline: 2px dashed color-mix(in srgb, var(--focus) 55%, transparent); outline-offset: -2px; }

.bottombar { grid-row: 5; }

/* Kopfzeile */
.topbar {
  position: relative;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: var(--paper);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.topbar-spacer { flex: 1; }

.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.segment {
  border: 0;
  border-radius: 0;
  min-width: 44px;
  min-height: 36px;
  font-size: 22px;
  line-height: 1;
  font-family: var(--digit-font);
}
.segment.is-active { background: var(--green); color: #fff; }

.task-form { display: flex; gap: 6px; }
#taskInput { width: 150px; font-family: var(--digit-font); font-size: 18px; }
.random-group { display: flex; gap: 6px; }
#levelSelect { max-width: 230px; }

.popover {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 20;
  width: min(340px, calc(100vw - 16px));
  padding: 10px 12px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.2);
}
.check-field { display: flex; gap: 8px; align-items: flex-start; padding: 6px 0; cursor: pointer; }
.check-field input { margin-top: 3px; }
[hidden] { display: none !important; }

.task-error {
  padding: 6px 12px;
  background: color-mix(in srgb, var(--danger) 12%, #fff);
  border: 1px solid color-mix(in srgb, var(--danger) 40%, var(--line));
  border-radius: var(--radius);
  color: var(--danger);
}

/* Bühne mit drei Tafeln */
.stage {
  min-height: 0;
  display: grid;
  gap: 8px;
  grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) minmax(0, 1.15fr);
  grid-template-areas:
    "material halb"
    "material notation";
}
#materialPanel { grid-area: material; }
#halbPanel { grid-area: halb; }
#notationPanel { grid-area: notation; }

.panel {
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  min-height: 0;
}
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 4px 2px 12px;
  background: var(--surface-2);
  border-bottom: 1px solid var(--line);
  min-height: 36px;
}
.panel-title { font-size: 13px; color: var(--muted); letter-spacing: 0.02em; }
.panel-toggle { border: 0; background: transparent; width: 32px; height: 32px; }
.panel-toggle svg { transition: transform 0.2s; }
.panel.is-covered .panel-toggle svg { transform: rotate(180deg); }
.panel-body { min-height: 0; overflow: auto; }
.mount { min-height: 100%; display: grid; place-items: center; padding: 12px; }

/* Rollladen: verdeckt den Inhalt, damit Kinder den nächsten Schritt vorhersagen können. */
.panel-cover {
  position: absolute;
  inset: 37px 0 0 0;
  background: repeating-linear-gradient(180deg, #d9dbe0 0 14px, #cfd1d7 14px 16px);
  transform: translateY(calc(-100% - 40px));
  transition: transform 0.25s ease;
  pointer-events: none;
}
.panel.is-covered .panel-cover { transform: none; }

.verbal {
  min-height: 52px;
  display: flex;
  align-items: center;
  padding: 8px 14px;
  background: var(--paper);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-size: 18px;
  line-height: 1.35;
}

.bottombar {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  padding: 6px;
  background: var(--paper);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.bottombar .icon-btn { width: 44px; height: 40px; }
.step-label { min-width: 120px; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }

@media (max-width: 820px) {
  .app-frame { height: auto; min-height: 100dvh; }
  .stage {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: none;
    grid-template-areas: "notation" "material" "halb";
  }
  .panel { min-height: 240px; }
  /* Über den Tafeln, deren Hervorhebungen eigene Ebenen haben */
  .verbal { position: sticky; bottom: 60px; z-index: 10; }
  .bottombar { position: sticky; bottom: 8px; z-index: 10; }
}

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

/* ---------- Selbst eintragen ----------
   Eingabefelder liegen in den Sichten (.entry-slot); jede Sicht setzt Größe und Lage über ihre
   eigene Klasse (mk-slot, mn-slot, dn-entry, sl-slot). */
.q-bar.is-entry .q-value { display: none; }
.q-feedback.is-info { color: var(--muted); }
.q-feedback.is-ok { color: #1d6a40; }
.q-feedback.is-shown { color: #7a5600; }
.app-frame .panel.is-entry-target { outline: 2px dashed color-mix(in srgb, var(--focus) 45%, transparent); outline-offset: -2px; }

.entry-slot {
  display: inline-grid;
  place-items: center;
  min-width: 1.8em;
  min-height: 1.3em;
  padding: 0 0.25em;
  border: 2px dashed color-mix(in srgb, var(--focus) 55%, transparent);
  border-radius: 6px;
  background: color-mix(in srgb, var(--focus) 5%, var(--paper));
  color: var(--ink);
  font-family: var(--digit-font);
  line-height: 1;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  vertical-align: middle;
}
.entry-slot:focus { outline: none; }
.entry-slot.is-active {
  border-style: solid;
  border-color: var(--focus);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 22%, transparent);
}
.entry-slot.is-active.is-empty::after {
  content: "";
  width: 2px;
  height: 0.9em;
  background: var(--focus);
  animation: entry-caret 1s steps(1) infinite;
}
.entry-slot.is-wrong {
  border-style: solid;
  border-color: var(--danger);
  background: color-mix(in srgb, var(--danger) 14%, var(--paper));
  color: var(--danger);
  animation: entry-shake 0.36s ease-in-out;
}
.entry-slot.is-wrong.is-active { box-shadow: 0 0 0 3px color-mix(in srgb, var(--danger) 25%, transparent); }
.entry-slot.is-right {
  border-style: solid;
  border-color: var(--green);
  background: color-mix(in srgb, var(--green) 14%, var(--paper));
  color: #1d6a40;
}
.entry-slot.is-shown { border-style: solid; border-color: #e7c66a; background: #fff4d6; color: #7a5600; }
@keyframes entry-caret { 50% { opacity: 0; } }
@keyframes entry-shake {
  20% { transform: translateX(-3px); }
  45% { transform: translateX(3px); }
  70% { transform: translateX(-2px); }
}

/* Weiter-Knopf, wenn im Eintragen-Modus nichts einzutragen ist */
.bottombar .is-nudge:not(:disabled) {
  background: var(--green);
  border-color: var(--green);
  color: #fff;
  animation: entry-nudge 1.6s ease-in-out infinite;
}
@keyframes entry-nudge { 50% { box-shadow: 0 0 0 5px color-mix(in srgb, var(--green) 25%, transparent); } }
