:root {
  --nacht: #141030;
  --holz: #7a4a26;
  --holz-dunkel: #3f2210;
  --papier: #f6e7c1;
  --papier-dunkel: #e2cb94;
  --tinte: #2b1d12;
  --rot: #c8553d;
  --rot-dunkel: #7f2d1c;
  --gruen: #3f7a52;
  --gruen-dunkel: #24402f;
  --dlg: 176px; /* Höhe des Dialogfelds */
  --schrift: "Trebuchet MS", "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: #0b0916;
  color: var(--tinte);
  font-family: var(--schrift);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body { display: flex; justify-content: center; }

#app {
  position: relative;
  width: 100%;
  max-width: 440px;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--nacht);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

#bg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center top;
  image-rendering: pixelated;
}

/* Bühne für Auswahl, Minispiele und Tafeln – oberhalb des Dialogfelds */
#stage {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: calc(16px + env(safe-area-inset-top)) 12px calc(var(--dlg) + 24px + env(safe-area-inset-bottom));
}
#app.ohne-dialog #stage { padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
#stage:empty { pointer-events: none; }
.laden { color: #cfc6e6; font-size: 17px; animation: puls 1.6s ease-in-out infinite; }

/* ---------- Dialogfeld im Stil von Stardew Valley ---------- */
#dialog {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: calc(8px + env(safe-area-inset-bottom));
  height: var(--dlg);
  display: grid;
  grid-template-columns: 100px 1fr;
  gap: 10px;
  padding: 10px 12px 10px 10px;
  background: var(--papier);
  border: 4px solid var(--holz);
  border-radius: 6px;
  box-shadow: 0 0 0 3px var(--holz-dunkel), inset 0 0 0 3px var(--papier-dunkel), 0 8px 0 rgba(0, 0, 0, .35);
  z-index: 3;
}
.portraet-box { display: flex; flex-direction: column; align-items: center; gap: 6px; }
#bg.foto, #portrait.foto { image-rendering: auto; }
#portrait {
  width: 96px;
  height: 96px;
  image-rendering: pixelated;
  border: 3px solid var(--holz);
  border-radius: 3px;
  box-shadow: 0 0 0 2px var(--holz-dunkel);
}
#speaker {
  font-size: 13px;
  font-weight: 700;
  line-height: 1.15;
  text-align: center;
  background: var(--papier-dunkel);
  border-radius: 3px;
  padding: 3px 5px;
  width: 100%;
}
#text {
  font-size: 18px;
  line-height: 1.38;
  overflow-y: auto;
  padding-right: 40px;
  white-space: pre-line;
}
#dialog.erzaehler { grid-template-columns: 1fr; }
#dialog.erzaehler .portraet-box { display: none; }
#dialog.erzaehler #text { font-style: italic; }

#weiter-pfeil {
  position: absolute;
  right: 12px;
  bottom: 6px;
  color: var(--rot);
  font-size: 16px;
  visibility: hidden;
  animation: wippen .8s ease-in-out infinite;
}
#dialog.weiter #weiter-pfeil { visibility: visible; }
@keyframes wippen { 50% { transform: translateY(3px); } }

.mini {
  width: 36px;
  height: 36px;
  font-size: 18px;
  line-height: 1;
  border-radius: 6px;
  border: 2px solid var(--holz);
  background: var(--papier-dunkel);
  color: var(--tinte);
  cursor: pointer;
  padding: 0;
}
#speak { position: absolute; right: 8px; top: 8px; }
#ueberspringen {
  position: absolute;
  right: 8px;
  bottom: 8px;
  padding: 5px 10px;
  font: 700 13px/1.2 var(--schrift);
  color: var(--tinte);
  background: var(--papier-dunkel);
  border: 2px solid var(--holz);
  border-radius: 14px;
  cursor: pointer;
}
#ueberspringen.sicher { background: var(--rot); border-color: var(--rot-dunkel); color: #fff8e6; }
#dialog.mit-skip #text { padding-bottom: 30px; }
#menue-knopf {
  position: absolute;
  top: calc(8px + env(safe-area-inset-top));
  right: 8px;
  z-index: 6;
  opacity: .85;
}

/* ---------- Knöpfe und Tafeln ---------- */
.btn {
  font: 700 18px/1.2 var(--schrift);
  color: #fff8e6;
  background: var(--rot);
  border: 3px solid var(--rot-dunkel);
  border-radius: 6px;
  padding: 12px 16px;
  min-height: 50px;
  box-shadow: 0 4px 0 var(--rot-dunkel);
  text-shadow: 0 2px 0 rgba(0, 0, 0, .25);
  cursor: pointer;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--rot-dunkel); }
.btn.zweit {
  background: var(--papier);
  color: var(--tinte);
  border-color: var(--holz);
  box-shadow: 0 4px 0 var(--holz);
  text-shadow: none;
}
.btn.zweit:active { box-shadow: 0 1px 0 var(--holz); }
.btn.gruen { background: var(--gruen); border-color: var(--gruen-dunkel); box-shadow: 0 4px 0 var(--gruen-dunkel); }
.btn.an { background: var(--gruen); border-color: var(--gruen-dunkel); color: #fff8e6; box-shadow: 0 4px 0 var(--gruen-dunkel); }

.panel {
  width: 100%;
  max-width: 380px;
  max-height: 100%;
  overflow-y: auto;
  background: var(--papier);
  border: 4px solid var(--holz);
  border-radius: 6px;
  box-shadow: 0 0 0 3px var(--holz-dunkel), inset 0 0 0 3px var(--papier-dunkel), 0 8px 0 rgba(0, 0, 0, .35);
  padding: 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.panel h2 { margin: 0; font-size: 22px; text-align: center; }
.panel p { margin: 0; font-size: 17px; line-height: 1.4; }
.panel .klein { font-size: 14px; opacity: .8; }

.auswahl { display: flex; flex-direction: column; gap: 12px; width: 100%; max-width: 380px; }
.panel.oben { margin-bottom: auto; margin-top: 4vh; }

.namensfeld {
  font: 700 22px/1.2 var(--schrift);
  color: var(--tinte);
  text-align: center;
  padding: 12px;
  border: 3px solid var(--holz);
  border-radius: 6px;
  background: #fffaf0;
  user-select: text;
  -webkit-user-select: text;
}
.namensfeld:focus { outline: 3px solid var(--gruen); outline-offset: 1px; }
.wackeln { animation: wackeln .35s; }
@keyframes wackeln { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

#overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  background: rgba(10, 8, 20, .6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

#fade {
  position: absolute;
  inset: 0;
  background: #000;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s;
  z-index: 20;
}
#fade.an { opacity: 1; }

/* ---------- Titelbild ---------- */
/* Titel unten über dem Startknopf, damit oben die Sternwarte mit Kuppel frei bleibt */
.titel {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  gap: 2vh;
}
.titel-schild {
  padding: 14px 18px;
  border-radius: 10px;
  background: radial-gradient(ellipse at center, rgba(12, 10, 28, .62) 0%, rgba(12, 10, 28, .35) 60%, transparent 100%);
  text-align: center;
  color: #fff3d6;
  text-shadow: 0 3px 0 #000, 0 0 12px rgba(0, 0, 0, .6);
}
.titel-schild h1 {
  margin: 6px 0;
  font-size: 40px;
  letter-spacing: 1px;
  color: #ffd36b;
}
.titel-ober { font-size: 14px; letter-spacing: 2px; text-transform: uppercase; }
.titel-unter { font-size: 18px; }
.titel .auswahl { margin-bottom: 3vh; }

/* ---------- Briefe auf dem Schreibtisch ---------- */
.briefe { display: flex; flex-direction: column; gap: 14px; width: 100%; max-width: 350px; }
.brief {
  position: relative;
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  box-shadow: 0 6px 0 rgba(0, 0, 0, .35);
  transform: rotate(var(--neigung, 0deg));
  cursor: pointer;
  font: 14px/1.25 var(--schrift);
  color: var(--tinte);
  text-align: left;
}
.brief:active { transform: rotate(var(--neigung, 0deg)) translateY(3px); }
.brief canvas, .brief-gross canvas { display: block; width: 100%; image-rendering: pixelated; }
.brief-text {
  position: absolute;
  left: 4%;
  bottom: 9%;
  max-width: 38%;
}
.brief-text b { font-size: 18px; }
.brief-text small { font-size: 12px; opacity: .85; }
.brief-gross { box-shadow: 0 4px 0 rgba(0, 0, 0, .25); }

.briefbogen {
  align-self: stretch;
  margin: 40px 6px auto;
  padding: 14px 16px;
  background: #f4ead2;
  border: 2px solid #c9b48a;
  box-shadow: 0 6px 0 rgba(0, 0, 0, .35);
  transform: rotate(-1deg);
  font-family: "Segoe Print", "Bradley Hand", "Comic Sans MS", cursive;
  color: #2b2a5a;
}
.briefbogen-ort { text-align: right; font-size: 15px; }
.briefbogen-an { margin-top: 10px; font-size: 16px; }
.briefbogen-von { margin-top: 10px; font-size: 13px; opacity: .75; }

/* ---------- Blinkkomparator ---------- */
.mg { position: relative; display: flex; flex-direction: column; align-items: center; gap: 10px; width: 100%; height: 100%; justify-content: center; }
.mg-info-zeile { display: flex; align-items: center; gap: 8px; }
.mg-info-zeile .mini { width: 30px; height: 30px; font-weight: 700; }
.legende {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
}
.legende .panel { gap: 8px; padding: 14px; }
.legende h2 { font-size: 19px; }
.leg-kopf, .leg-zeile {
  display: grid;
  grid-template-columns: 52px 52px 1fr;
  gap: 8px;
  align-items: center;
}
.leg-kopf { font-size: 12px; font-weight: 700; text-align: center; opacity: .75; }
.leg-zeile { font-size: 14px; line-height: 1.25; }
.leg-bild canvas {
  display: block;
  width: 52px;
  height: 52px;
  image-rendering: pixelated;
  border: 2px solid #4a4f55;
  border-radius: 2px;
}
.mg-titel {
  color: #fff3d6;
  font-weight: 700;
  background: rgba(43, 29, 18, .8);
  padding: 4px 12px;
  border-radius: 4px;
}
.platte-rahmen {
  width: min(100%, calc(100dvh - var(--dlg) - 200px));
  aspect-ratio: 1;
  padding: 6px;
  background: #2c2f34;
  border: 4px solid #4a4f55;
  border-radius: 4px;
  box-shadow: 0 0 0 3px #1c1e22, 0 8px 0 rgba(0, 0, 0, .4);
}
.platte-rahmen canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  cursor: crosshair;
}
.platte-info {
  font-size: 15px;
  font-weight: 700;
  background: var(--papier);
  border: 2px solid var(--holz);
  border-radius: 4px;
  padding: 4px 10px;
}
.mg-knoepfe { display: flex; gap: 8px; width: 100%; max-width: 380px; }
.mg-knoepfe .btn { flex: 1; padding: 10px 4px; font-size: 16px; }
.puls { animation: puls 1.2s ease-in-out infinite; }

/* ---------- Akt 2: Messplatte, Eingabe, Rechenmaschine, Rechenblatt, Sonnensystem ---------- */
.platte-rahmen.mess {
  aspect-ratio: auto;
  width: min(100%, calc((100dvh - var(--dlg) - 270px) * 120 / 136));
}
.platte-rahmen.mess canvas { height: auto; }
.eingabe-frage .rot { color: #ff9a7a; }
.eingabe-frage .blau { color: #8fc0ff; }

.mg-eingabe { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 8px; }
.eingabe { display: flex; flex-direction: column; gap: 6px; }
.eingabe-frage {
  font-weight: 700;
  font-size: 15px;
  color: #fff3d6;
  background: rgba(43, 29, 18, .85);
  padding: 5px 10px;
  border-radius: 4px;
  text-align: center;
}
.eingabe-anzeige {
  display: inline-block;
  min-width: 3ch;
  margin-left: 4px;
  padding: 0 6px;
  border-radius: 3px;
  background: var(--papier);
  color: var(--tinte);
}
.tastatur { display: grid; grid-template-columns: repeat(var(--spalten, 6), 1fr); gap: 6px; }
.taste {
  min-height: 42px;
  font: 700 18px var(--schrift);
  color: var(--tinte);
  background: var(--papier);
  border: 2px solid var(--holz);
  border-radius: 5px;
  box-shadow: 0 3px 0 var(--holz);
  cursor: pointer;
}
.taste:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--holz); }
.taste.ok { background: var(--gruen); color: #fff8e6; border-color: var(--gruen-dunkel); box-shadow: 0 3px 0 var(--gruen-dunkel); }

.aufgabe-karte {
  width: 100%;
  max-width: 380px;
  padding: 8px 12px;
  font-size: 16px;
  line-height: 1.35;
  text-align: center;
  background: var(--papier);
  border: 2px solid var(--holz);
  border-radius: 5px;
}
.tresor { width: min(70%, 260px); }
.tresor canvas { display: block; width: 100%; image-rendering: pixelated; }
.gefunden { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; min-height: 0; }
.gefunden span {
  font: 700 15px var(--schrift);
  color: #fff8e6;
  background: var(--gruen);
  border: 2px solid var(--gruen-dunkel);
  border-radius: 14px;
  padding: 3px 10px;
}
.tresor-zeile {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font: 700 24px var(--schrift);
  color: #fff3d6;
  background: rgba(43, 29, 18, .85);
  border-radius: 6px;
  padding: 6px;
}
.tresor-zeile .slot {
  min-width: 56px;
  height: 42px;
  font: 700 22px var(--schrift);
  color: var(--tinte);
  background: var(--papier);
  border: 3px solid var(--holz);
  border-radius: 6px;
  cursor: pointer;
}
.tresor-zeile .slot.aktiv { border-color: #e0b84a; box-shadow: 0 0 0 2px #e0b84a; }

.urkunde-bild {
  display: block;
  width: 100%;
  max-height: 46dvh;
  object-fit: contain;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .35);
}
.urkunde-panel a.btn { text-align: center; text-decoration: none; }
.telegramm {
  width: 100%;
  max-width: 350px;
  padding: 10px 12px;
  background: #f3ecd2;
  border: 2px solid #b8a06a;
  box-shadow: 0 6px 0 rgba(0, 0, 0, .35);
  font: 600 13px/1.35 "Courier New", Courier, monospace;
  color: #2b2a2a;
}
.tg-kopf { font-weight: 700; letter-spacing: 1px; border-bottom: 2px solid #b8a06a; padding-bottom: 3px; margin-bottom: 4px; }
.tg-an { opacity: .75; }
.tg-text { margin: 4px 0; font-weight: 700; }
.tg-zeile { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; padding: 4px 0; border-top: 1px dashed #c9b88e; }
.tg-name { min-width: 92px; font-weight: 700; }
.tg-woerter { display: flex; flex-wrap: wrap; gap: 4px; }
.tg-wort {
  min-width: 52px;
  min-height: 22px;
  padding: 2px 4px;
  border-bottom: 2px solid #8a7a5a;
  text-align: center;
  font-weight: 700;
}
.tg-wort.aktiv { background: #ffe9a8; border-color: #e08a1e; }
.tg-wort.fertig { color: #2f7a4a; }
.codetabelle { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; width: 100%; max-width: 350px; }
.codetabelle .code {
  padding: 5px 2px;
  font: 700 11px/1.2 "Courier New", Courier, monospace;
  color: var(--tinte);
  background: var(--papier);
  border: 2px solid var(--holz);
  border-radius: 4px;
  text-align: center;
}
.codetabelle .code b { display: block; font: 700 17px var(--schrift); }
.codetabelle button.code { cursor: pointer; box-shadow: 0 2px 0 var(--holz); }
.codetabelle button.code:active { transform: translateY(2px); box-shadow: none; }
.tg-tabelle-platz { width: 100%; display: flex; justify-content: center; }
.rechenkarte {
  width: 100%;
  max-width: 340px;
  padding: 14px 16px 18px;
  text-align: center;
  background-color: #fbf6e6;
  background-image: linear-gradient(#d6e0ea 1px, transparent 1px), linear-gradient(90deg, #d6e0ea 1px, transparent 1px);
  background-size: 14px 14px;
  border: 2px solid #c9b48a;
  box-shadow: 0 6px 0 rgba(0, 0, 0, .35);
  transform: rotate(-.6deg);
  color: #2b2a5a;
}
.rk-sterne { font-size: 22px; letter-spacing: 4px; color: #d8cfb8; }
.rk-sterne .an { color: #e0a81e; }
.rk-nummer { font-size: 14px; opacity: .75; margin: 2px 0 8px; }
.rk-notizen {
  margin: 6px 0 4px;
  padding: 6px 10px;
  text-align: left;
  font: 600 15px/1.35 var(--schrift);
  background: rgba(255, 255, 255, .6);
  border-left: 4px solid #c9b48a;
}
.rk-notizen .rk-erledigt { color: #2f7a4a; }
.rk-aufgabe { font: 700 34px/1.2 "Segoe Print", "Bradley Hand", "Comic Sans MS", cursive; min-height: 42px; }
.rk-frage { font: 600 17px/1.35 var(--schrift); text-align: left; }
.rechenblatt {
  width: 100%;
  max-width: 340px;
  padding: 12px 16px;
  background-color: #fbf6e6;
  background-image: linear-gradient(#d6e0ea 1px, transparent 1px), linear-gradient(90deg, #d6e0ea 1px, transparent 1px);
  background-size: 14px 14px;
  border: 2px solid #c9b48a;
  box-shadow: 0 6px 0 rgba(0, 0, 0, .35);
  transform: rotate(-.8deg);
  font-family: "Segoe Print", "Bradley Hand", "Comic Sans MS", cursive;
  color: #2b2a5a;
}
.rb-kopf, .rb-fuss { font-size: 13px; }
.rb-kopf { margin-bottom: 6px; }
.rb-fuss { margin-top: 6px; opacity: .8; }
.rechenblatt table { width: 100%; border-collapse: collapse; font-size: 19px; }
.rechenblatt th { font-size: 13px; text-align: left; border-bottom: 2px solid #2b2a5a; padding: 2px 6px; }
.rechenblatt td { padding: 3px 6px; border-bottom: 1px dashed #b8b0a0; }
.rechenblatt tr[data-i] { cursor: pointer; }
.rechenblatt .rb-ok td { color: #2f8a4a; }
.rechenblatt .rb-falsch td { background: rgba(200, 85, 61, .18); }
.rechenblatt .rb-neu { color: #c8553d; font-weight: 700; }

.sonnen-namen .luecke {
  position: absolute;
  transform: translate(-50%, -50%);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid #ffd36b;
  background: rgba(20, 26, 51, .85);
  color: #ffd36b;
  font: 700 20px/1 var(--schrift);
  cursor: pointer;
  animation: puls 1.2s ease-in-out infinite;
}
.sonnen-namen .luecke.falsch { border-color: var(--rot); color: var(--rot); animation: none; }
.sonnen-rahmen {
  position: relative;
  width: min(100%, calc((100dvh - var(--dlg) - 250px) * 240 / 260));
  border: 4px solid #4a4f55;
  border-radius: 4px;
  box-shadow: 0 0 0 3px #1c1e22, 0 8px 0 rgba(0, 0, 0, .4);
}
.sonnen-rahmen canvas { display: block; width: 100%; image-rendering: pixelated; }
.sonnen-namen span {
  position: absolute;
  transform: translateX(-50%);
  font: 700 10px/1 var(--schrift);
  color: #cfd8ff;
  text-shadow: 0 1px 0 #000;
  white-space: nowrap;
  pointer-events: none;
}
.sonnen-namen .neu { color: #8fe0a0; }
.steine { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.stein {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px 4px 4px;
  font: 700 14px var(--schrift);
  color: var(--tinte);
  background: var(--papier);
  border: 2px solid var(--holz);
  border-radius: 6px;
  box-shadow: 0 3px 0 var(--holz);
  cursor: pointer;
}
.stein canvas { display: block; width: 32px; height: 32px; image-rendering: pixelated; border-radius: 3px; }
.stein.gewaehlt { outline: 3px solid var(--gruen); background: #e6f2dc; }
@keyframes puls { 50% { filter: brightness(1.25); transform: scale(1.04); } }
