/* ---------- Variablen ---------- */

:root {
  --header-btn-size: clamp(54px, 9.5vw, 72px);
  --header-btn-inset: 10px;
  /* Frame-Breite haengt am main-Container (max 720px). Die board-bg.png ist
     auf eine exakt 6:4-Innenflaeche zugeschnitten: 79.77% Breite x 59.19%
     Hoehe (bei 1023x919). 6 quadratische Zellen fuellen die Innenflaeche
     genau aus, edge-Spalten/-Zeilen sind im Grid 0 — die Endpunkte ragen
     bewusst etwas in den Holzrahmen hinein. */
  --viewport-safe-w: calc(
    100vw
    - env(safe-area-inset-left, 0px)
    - env(safe-area-inset-right, 0px)
  );
  --frame-w: min(var(--viewport-safe-w), 720px);
  --inner-w: calc(var(--frame-w) * 0.7977);
  --cell-size: calc(var(--inner-w) / 6);
  --edge-x-size: 0px;
  --edge-y-size: 0px;
  --gap: 0px;
  --board-pad: 0px;
  --cols: 6;
  --rows: 4;

  --red: #ff372f;
  --text: #ffffff;
  --text-soft: #c9d9ee;
  --panel-slice-top: 330;
  --panel-slice-right: 330;
  --panel-slice-bottom: 330;
  --panel-slice-left: 330;
  /* Border-Width skaliert mit der Card-Breite. Auf iPhone (~390 px) sind
     beide Cards 100% breit — gleicher Border auf beiden. */
  --panel-border-top:    clamp(40px, 9.15vw, 69px);
  --panel-border-right:  clamp(40px, 9.15vw, 69px);
  --panel-border-bottom: clamp(40px, 9.15vw, 69px);
  --panel-border-left:   clamp(40px, 9.15vw, 69px);

  /* Tray-Rahmen analog ueber border-image-slice — Hoehe der Tray-Box
     waechst dadurch mit der Anzahl der Slot-Reihen. Slice-Werte beziehen
     sich auf das tray-dock.png-Sprite (2015x820). */
  --tray-slice-top:    160;
  --tray-slice-right:  170;
  --tray-slice-bottom: 160;
  --tray-slice-left:   170;
  --tray-border-top:    clamp(18px, 4.5vw, 38px);
  --tray-border-right:  clamp(20px, 5vw,   42px);
  --tray-border-bottom: clamp(18px, 4.5vw, 38px);
  --tray-border-left:   clamp(20px, 5vw,   42px);

  /* Board-Rahmen ueber border-image-slice. board-bg.png ist 1023x759 mit
     Innenflaeche x 9.9%..89.66%, y 13.7%..85.38% — daraus die Slice-Pixel
     fuers 9-Slice-Scaling. Die border-width skaliert mit --frame-w, damit
     die Proportionen erhalten bleiben; max(...) sichert eine Mindestdicke
     auf schmalen Viewports, damit der Rahmen nicht zu duenn wird. */
  --board-slice-top:    104;
  --board-slice-right:  106;
  --board-slice-bottom: 111;
  --board-slice-left:   101;
  --board-border-top:    max(40px, calc(0.1017 * var(--frame-w)));
  --board-border-right:  max(40px, calc(0.1036 * var(--frame-w)));
  --board-border-bottom: max(42px, calc(0.1085 * var(--frame-w)));
  --board-border-left:   max(40px, calc(0.0987 * var(--frame-w)));
}

/* ---------- Reset ---------- */

* { box-sizing: border-box; }
html, body {
  overscroll-behavior: none;
  height: 100%;
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
  overflow-x: clip;
}
html { min-height: 100%; background: #02132a; }

body {
  min-height: 100dvh;
  width: 100%;
  max-width: 100%;
  margin: 0;
  /* Praktisch keine Raender auf Mobile, damit das Brett moeglichst gross
     wird. Auf Desktop bleibt etwas Luft. Plus Safe-Area-Insets, damit
     das Logo im iPhone-PWA-Modus nicht unter der Notch verschwindet. */
  padding:
    calc(env(safe-area-inset-top,    0px) + clamp(2px, 0.6vw, 14px))
    calc(env(safe-area-inset-right,  0px) + clamp(0px, 0.4vw, 18px))
    calc(env(safe-area-inset-bottom, 0px) + 12px)
    calc(env(safe-area-inset-left,   0px) + clamp(0px, 0.4vw, 18px));
  font-family: ui-rounded, "SF Pro Rounded", "Avenir Next Rounded",
               "Nunito", "Quicksand", system-ui, -apple-system, sans-serif;
  color: var(--text);
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overflow-x: hidden;
  overflow-x: clip;
  position: relative;
  z-index: 0;
}

/* ---------- Splash-Screen ---------- */

#splash {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: #04101e;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.5s ease;
}
#splash img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
}
#splash.fade-out { opacity: 0; pointer-events: none; }
#splash.hidden   { display: none; }
#splash-progress {
  position: absolute;
  bottom: clamp(32px, 6vh, 64px);
  left: 50%;
  transform: translateX(-50%);
  width: min(72vw, 320px);
  height: 20px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 0 0 1px rgba(255,255,255,0.1);
}
#splash-bar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #1f83cf, #ffc529);
  border-radius: 6px;
  transition: width 0.15s ease;
  box-shadow: 0 0 12px rgba(255, 197, 41, 0.7);
}
#splash-bar.complete {
  animation: none;
  width: 100%;
  transition: width 0.2s ease;
}

/* ---------- Hintergrund ---------- */

.sky {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100lvh;
  z-index: -1;
  overflow: hidden;
  /* Hintergrund-Bild wird in game.js per --bg-image gesetzt; wechselt alle
     5 Level. Default-Fallback ist Hintergrund 1. */
  background:
    linear-gradient(180deg,
      rgba(2, 19, 42, 0) 0%,
      rgba(2, 19, 42, 0.05) 52%,
      rgba(2, 19, 42, 0.36) 100%),
    var(--bg-image, url("assets/background/1-forgotten-worlds/background-1.jpg")) center top / cover no-repeat;
  filter: grayscale(1) saturate(0.15) brightness(0.72);
  transition: filter 0.45s ease;
}
body[data-stars="1"] .sky {
  filter: grayscale(0.45) saturate(0.75) brightness(0.88);
}
body[data-stars="2"] .sky {
  filter: grayscale(0.28) saturate(0.88) brightness(0.94);
}
body[data-stars="3"] .sky {
  filter: grayscale(0) saturate(1.08) brightness(1);
}
.sky::before {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(90deg,
      rgba(2, 16, 34, 0.18), transparent 14% 86%, rgba(2, 16, 34, 0.18)),
    linear-gradient(180deg, transparent 0 66%, rgba(20, 24, 32, 0.16) 100%);
}
.sky::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(5, 14, 28, 0.46);
  opacity: 1;
  transition: opacity 0.28s ease, background 0.28s ease;
}
body[data-stars="1"] .sky::after {
  background: rgba(5, 14, 28, 0.28);
}
body[data-stars="2"] .sky::after {
  background: rgba(5, 14, 28, 0.24);
}
body[data-stars="3"] .sky::after {
  background: rgba(5, 14, 28, 0.12);
}

/* ---------- Header ---------- */

header {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  width: 100%;
  max-width: 720px;
  /* Auf Mobile etwas niedriger, damit mehr Platz fuers Brett bleibt. */
  min-height: clamp(136px, 16vw, 180px);
  margin: 0 auto clamp(-14px, -1.8vw, -7px);
  padding: 0 clamp(58px, 9vw, 86px);
  z-index: 401;
}

#logo {
  width: min(78vw, 440px);
  height: auto;
  max-width: 100%;
  max-height: clamp(90px, 18vw, 192px);
  object-fit: contain;
  margin: 0 auto;
  display: block;
  filter:
    drop-shadow(0 4px 3px rgba(5, 22, 43, 0.5))
    drop-shadow(0 14px 18px rgba(0, 0, 0, 0.28));
  transform: translateZ(0);
  -webkit-user-drag: none;
  user-select: none;
}

.round-btn {
  position: absolute;
  top: clamp(10px, 1.8vw, 22px);
  width: var(--header-btn-size);
  height: var(--header-btn-size);
  padding: 0;
  border: 0;
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: url("assets/ui/button-square-blue.png") center / 100% 100% no-repeat;
  /* ueber dem Logo liegen, damit der Overview-Button (rechts neben Zahnrad)
     bei schmalen Viewports nicht vom Logo verdeckt wird. */
  z-index: 5;
}
.round-btn-left  { left:  var(--header-btn-inset); }
/* #overview-badge deaktiviert — "nicht angeschaut"-Zählung ausgeblendet */
#overview-badge { display: none !important; }
/* #overview-badge[hidden] { display: none; } */
.round-btn-right { right: var(--header-btn-inset); }
.round-btn-right.active {
  filter: brightness(1.18) saturate(1.2);
}
#menu-btn {
  background-image: url("assets/ui/utility-button-options.png?v=20260529d");
}
#overview-btn {
  background-image: url("assets/ui/utility-button-overview.png?v=20260529d");
}
#menu-btn .ui-icon,
#overview-btn .ui-icon {
  opacity: 0;
}
.header-right-stack {
  position: absolute;
  top: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0;
  z-index: 6;
}
.header-right-stack .round-btn-right {
  position: relative;
  top: auto;
  right: auto;
}
/* Zweiter Header-Button: unterhalb des Zahnrads, gleiche x-Position. */
.round-btn-second {
  left: var(--header-btn-inset);
  top: calc(clamp(10px, 1.8vw, 22px) + var(--header-btn-size) + 3px);
}

.level-stars-badge {
  position: relative;
  right: auto;
  top: auto;
  width: calc(var(--header-btn-size) * 1.55);
  height: calc(var(--header-btn-size) * 2.32);
  margin-top: calc(var(--header-btn-size) * -0.15);
  background: url("assets/ui/level-color-meter-stars-pyramid-0.png?v=20260529d") center / 100% 100% no-repeat;
  border: 0;
  padding: 0;
  cursor: pointer;
  filter:
    drop-shadow(0 3px 3px rgba(6, 24, 44, 0.48))
    drop-shadow(0 10px 12px rgba(0, 0, 0, 0.22));
  transform: translateZ(0);
  pointer-events: auto;
  z-index: 1;
}
.level-stars-badge:hover { filter:
    brightness(1.06)
    drop-shadow(0 4px 0 rgba(6, 24, 44, 0.42))
    drop-shadow(0 10px 12px rgba(0, 0, 0, 0.22)); }
.level-stars-badge:active { transform: translateY(3px); }
.level-stars-badge[data-stars="1"] { background-image: url("assets/ui/level-color-meter-stars-pyramid-1.png?v=20260529d"); }
.level-stars-badge[data-stars="2"] { background-image: url("assets/ui/level-color-meter-stars-pyramid-2.png?v=20260529d"); }
.level-stars-badge[data-stars="3"] { background-image: url("assets/ui/level-color-meter-stars-pyramid-3.png?v=20260529d"); }
body.bonus-level .level-stars-badge {
  width: clamp(80px, 15vw, 112px);
  height: clamp(120px, 22.5vw, 168px);
  background-image: url("assets/ui/level-gems-meter-pyramid-0.png?v=20260529d");
}
body.bonus-level .level-stars-badge[data-stars="1"] { background-image: url("assets/ui/level-gems-meter-pyramid-1.png?v=20260529d"); }
body.bonus-level .level-stars-badge[data-stars="2"] { background-image: url("assets/ui/level-gems-meter-pyramid-2.png?v=20260529d"); }
body.bonus-level .level-stars-badge[data-stars="3"] { background-image: url("assets/ui/level-gems-meter-pyramid-3.png?v=20260529d"); }

.round-btn svg {
  width: 54%;
  height: 54%;
  display: block;
  flex: 0 0 auto;
  filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.35));
}
.ui-icon,
.menu-icon-svg,
.menu-share-btn svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.round-btn:active { transform: translateY(3px); }

/* ---------- Main ---------- */

/* Filter-Transition für Level-Einblende-Effekt */
.board-frame.fading-in,
.tray-frame.fading-in {
  transition: filter 0.3s ease;
}
main {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 720px;
  gap: clamp(5px, 0.9vw, 9px);
  position: relative;
  z-index: 2;
}

/* ---------- Board mit PNG-Rahmen ---------- */

/* Der Holz-Rahmen kommt per border-image-slice aus board-bg.png (1023x759).
   border-width skaliert mit --frame-w (analog tray-frame) und sichert per
   max(...) eine Mindestdicke, damit der Rahmen auf schmalen Viewports nicht
   zu duenn wird. Die Hoehe der Card waechst automatisch mit dem Inhalt
   (#board-wrapper); --frame-aspect/--frame-top-pct/--frame-inner-pct
   werden nicht mehr benoetigt. */
.board-area {
  position: relative;
  width: 100%;
  max-width: 720px;
  margin: max(clamp(90px, 16vw, 150px), calc(var(--header-btn-size) + 40px)) auto 0;
}

.board-frame {
  position: relative;
  width: 100%;
  max-width: 720px;
  border-style: solid;
  border-color: transparent;
  border-width:
    var(--board-border-top)
    var(--board-border-right)
    var(--board-border-bottom)
    var(--board-border-left);
  border-image-source: url("assets/ui/board-bg.png");
  border-image-slice:
    var(--board-slice-top)
    var(--board-slice-right)
    var(--board-slice-bottom)
    var(--board-slice-left)
    fill;
  border-image-width: 1;
  border-image-repeat: stretch;
  filter: drop-shadow(0 16px 22px rgba(3, 16, 34, 0.55));
}

/* ---------- Banner mit Level-Name + Code-Chip ---------- */

.board-banner {
  position: absolute;
  z-index: 4;
  bottom: calc(100% + clamp(6px, 2vw, 14px));
  left: 50%;
  transform: translateX(-50%);
  height: var(--header-btn-size);
  width: clamp(171px, 33.7vw, 228px);
  padding: 0 clamp(43px, 8.3vw, 57px) 0 clamp(16px, 3.1vw, 21px);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(6px, 1.5vw, 12px);
  background: url("assets/ui/utility-level-panel.png?v=20260529d") center / 100% 100% no-repeat;
  filter: drop-shadow(0 6px 8px rgba(0, 0, 0, 0.42));
}
body.bonus-level .board-banner {
  background-image: url("assets/ui/utility-level-panel-bonus.png?v=20260529d");
  filter:
    drop-shadow(0 7px 10px rgba(85, 45, 0, 0.58))
    drop-shadow(0 0 13px rgba(255, 214, 84, 0.32));
}

#level-name {
  flex: 1;
  min-width: 0;
  text-align: center;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: clamp(14px, 3.6vw, 22px);
  line-height: 1;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #a8eeff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-shadow:
    0 0 6px rgba(100, 220, 255, 0.9),
    0 0 14px rgba(60, 180, 255, 0.7),
    0 1px 0 rgba(0, 40, 80, 0.8);
}
body.bonus-level #level-name {
  color: #ffe66d;
  text-shadow:
    0 2px 0 #713f00,
    0 4px 0 rgba(0, 0, 0, 0.3),
    0 0 14px rgba(255, 222, 93, 0.55);
}


/* Help-Button: schmaler als normaler Code-Chip, größeres Fragezeichen */
#help-btn {
  position: absolute;
  top: 8%;
  right: 1.8%;
  width: 27%;
  height: 84%;
  min-width: 0;
  padding: 0;
  border: 0;
  font-size: 0;
  letter-spacing: 0;
  color: transparent;
  background: transparent;
  box-shadow: none;
  text-shadow: none;
}
#help-btn:hover {
  filter: brightness(1.08);
}
#help-btn:active {
  transform: none;
  box-shadow: none;
}

/* .code-chip auskommentiert — derzeit nicht verwendet */
.code-chip {
  position: relative;
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  min-width: clamp(46px, 9vw, 72px);
  height: clamp(24px, 4.7vw, 34px);
  margin: 0;
  padding: 0 clamp(8px, 1.8vw, 13px);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: clamp(12px, 2.4vw, 17px);
  font-weight: 950;
  line-height: 1;
  letter-spacing: 1.4px;
  color: #3d2604;
  border: 2px solid #fff0a8;
  border-radius: clamp(8px, 1.4vw, 11px);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.5), transparent 35%),
    linear-gradient(180deg, #ffe46a 0%, #ffbd27 52%, #cc7608 100%);
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.55);
  cursor: pointer;
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.5),
    inset 0 -3px 0 rgba(99, 48, 0, 0.28),
    0 3px 0 #6d3b05,
    0 5px 8px rgba(0, 0, 0, 0.22);
}
.code-chip:hover { filter: brightness(1.08); }
.code-chip:active {
  transform: translateY(2px);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.42),
    inset 0 -1px 0 rgba(99, 48, 0, 0.28),
    0 1px 0 #6d3b05,
    0 3px 5px rgba(0, 0, 0, 0.2);
}
.code-chip.copied {
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.45), transparent 35%),
    linear-gradient(180deg, #70ee86 0%, #35b94f 56%, #197833 100%);
  border-color: #dcffe2;
  color: #063a14;
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.42),
    inset 0 -3px 0 rgba(0, 64, 19, 0.28),
    0 3px 0 #0b551f,
    0 5px 8px rgba(0, 0, 0, 0.22);
}

/* Banner-Pfeile haengen jetzt direkt am Rahmen (board-frame), nicht mehr im
   Banner-Element — so vermeiden wir Safari-Clipping durch den transformierten
   Banner-Container. Vertikale Position: knapp ueber der Rahmenkante, in der
   Mitte des Banner-Sprites; horizontal: links/rechts neben dem Banner. */
.banner-arrow {
  --btn-size: var(--header-btn-size);
  position: absolute;
  bottom: calc(100% + clamp(6px, 2vw, 14px));
  width: var(--btn-size);
  aspect-ratio: 1;
  padding: 0;
  border: 0;
  font-size: 0;
  color: transparent;
  background: center / 100% 100% no-repeat;
  cursor: pointer;
}
#prev-btn {
  left: var(--header-btn-inset);
  background-image: url("assets/ui/utility-button-prev.png?v=20260529d");
}
#next-btn {
  right: calc(var(--header-btn-size) * 0.18);
  background-image: url("assets/ui/utility-button-next.png?v=20260529d");
}

.banner-arrow:hover  { filter: brightness(1.08); }
.banner-arrow:active { transform: translateY(2px); }

/* Wenn das aktuelle Level geloest ist und das naechste Level erreichbar
   ist, pulsiert der Next-Pfeil als visueller Hinweis "weiter geht's". */
@keyframes next-btn-pulse {
  0%, 100% { transform: scale(1);    filter: brightness(1)    drop-shadow(0 0 0 rgba(255, 255, 255, 0)); }
  50%      { transform: scale(1.12); filter: brightness(1.18) drop-shadow(0 0 14px rgba(140, 210, 255, 0.75)); }
}
#next-btn.pulse:not(:disabled) {
  animation: next-btn-pulse 1.1s ease-in-out infinite;
}
#next-btn.pulse:not(:disabled):active {
  animation: none;
  transform: translateY(2px);
}

@keyframes overview-btn-pulse {
  0%, 100% { transform: scale(1);    filter: brightness(1)    drop-shadow(0 0 0   rgba(255, 220, 80, 0)); }
  50%      { transform: scale(1.18); filter: brightness(1.25) drop-shadow(0 0 12px rgba(255, 220, 80, 0.9)); }
}
#overview-btn.pulse {
  animation: overview-btn-pulse 0.85s ease-in-out infinite;
}
body.overview-hint header {
  z-index: 535;
}

@keyframes hint-arrow-bounce {
  0%, 100% { transform: translateY(-50%) translateX(0);    opacity: 1; }
  50%       { transform: translateY(-50%) translateX(-8px); opacity: 0.7; }
}
body.overview-hint #overview-btn::after {
  content: "◄";
  position: absolute;
  top: 50%;
  left: calc(100% + 6px);
  transform: translateY(-50%);
  font-size: 3em;
  line-height: 1;
  color: #ffe050;
  text-shadow: 0 0 10px rgba(255, 160, 0, 1), 0 2px 4px rgba(0,0,0,0.6);
  pointer-events: none;
  z-index: 1;
  animation: hint-arrow-bounce 0.7s ease-in-out infinite;
}

.banner-arrow:disabled { opacity: 0; pointer-events: none; }

/* ---------- Board-Innenbereich ---------- */

#board-wrapper {
  /* Liegt im Inhaltsbereich der .board-frame Card (innerhalb der Border).
     aspect-ratio sorgt dafuer, dass die Zellen immer quadratisch bleiben —
     bei mehr Reihen waechst die Card-Hoehe entsprechend mit. */
  position: relative;
  width: 100%;
  aspect-ratio: var(--cols) / var(--rows);
  display: grid;
  grid-template-columns: var(--edge-x-size) repeat(var(--cols), minmax(0, 1fr)) var(--edge-x-size);
  grid-template-rows:    var(--edge-y-size) repeat(var(--rows), minmax(0, 1fr)) var(--edge-y-size);
  gap: var(--gap);
}
/* Kein Filter/keine Transition mehr im Won-State: in Safari clipped der
   filter-induzierte Compositing-Layer die Endpunkte, die ausserhalb des
   Wrappers liegen. Wenn ein dezenter Erfolgs-Indikator gewuenscht ist,
   sollte er auf ein nicht-ueberragendes Element (z.B. Hintergrund) gehen. */
#board-wrapper.flow { /* placeholder */ }

/* ---------- Zellen ---------- */

.cell {
  position: relative;
  touch-action: none;
  border-radius: clamp(6px, 1.4vw, 10px);
  background: url("assets/ui/cell-empty.png") center / 100% 100% no-repeat;
}
#board-wrapper.dragging .cell.valid-drop {
  background: url("assets/ui/cell-highlight.png") center / 100% 100% no-repeat;
  box-shadow: 0 0 0 3px rgba(255, 208, 80, 0.22);
}
.cell.locked {
  background: url("assets/ui/cell-locked.png") center / 100% 100% no-repeat;
}
/* Kaputte Bodenplatte: keine Interaktion, keine Steine. */
.cell.blocked {
  background: url("assets/ui/cell-rock-plate.png") center / 100% 100% no-repeat;
  cursor: not-allowed;
  pointer-events: none;
}
.cell.blocked.slime {
  background-image: url("assets/ui/cell-slime-plate.png");
}
.cell.blocked.bush {
  background-image: url("assets/ui/cell-bush-plate.png");
}
.cell.blocked.ice {
  background-image: url("assets/ui/cell-ice-plate.png");
}
.cell.has-piece > .piece-svg { cursor: grab; touch-action: none; }
.cell.locked    > .piece-svg { cursor: not-allowed; }

/* ---------- Spielsteine ---------- */

.piece-svg {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  overflow: visible;
  user-select: none;
  -webkit-user-drag: none;
}
.cell > .piece-svg { position: relative; z-index: 1; }
/* Endpunkt-Sprites liegen ueber den Zell-Pipes, damit die Anschluesse am
   Rahmen sauber lesbar bleiben, auch wenn die erste Zelle bereits belegt ist. */
.endpoint > .piece-svg { position: relative; z-index: 3; }

.pipe-sprite {
  transform:
    rotate(var(--pipe-shape-rotate, 0deg))
    scale(1.07);
  filter:
    drop-shadow(0 4px 0 rgba(5, 17, 35, 0.36))
    drop-shadow(0 6px 5px rgba(0, 0, 0, 0.28));
  /* will-change haelt die GPU-Layer beim Toggeln von .dragging stabil —
     ohne diesen Hinweis baut Safari die Layer kurz neu auf und das
     ergibt das Aufblitzen aller Spielsteine beim Drag-Start. */
  will-change: filter, transform;
}
.pipe-sprite.pipe-h,
.pipe-sprite.pipe-ne,
.pipe-sprite.pipe-tn,
.pipe-sprite.pipe-full,
.pipe-sprite.pipe-x { --pipe-shape-rotate: 0deg; }
.pipe-sprite.pipe-v { --pipe-shape-rotate: -90deg; }
.pipe-sprite.pipe-se { --pipe-shape-rotate: 90deg; }
.pipe-sprite.pipe-nw { --pipe-shape-rotate: -90deg; }
.pipe-sprite.pipe-sw { --pipe-shape-rotate: 180deg; }
.pipe-sprite.pipe-te { --pipe-shape-rotate: 90deg; }
.pipe-sprite.pipe-ts { --pipe-shape-rotate: 180deg; }
.pipe-sprite.pipe-tw { --pipe-shape-rotate: -90deg; }

/* Wenn das Level geloest ist, pulsieren die Rohre wellenfoermig — so als
   wuerde Wasser durch sie hindurch fliessen. Pro-Zelle Delay aus
   --flow-delay (von game.js gesetzt) macht die Welle entlang des Pfades
   wandern. Die Helligkeit pulsiert dezent fuer alle Stuecke, die
   Sprite-Skalierung kommt ueber pipe-flow-stretch dazu. */
@keyframes pipe-flow-cell {
  0%, 100% { filter: brightness(1)    saturate(1);    }
  25%      { filter: brightness(1.14) saturate(1.12); }
  75%      { filter: brightness(1.04) saturate(1.04); }
}
@keyframes pipe-flow-stretch {
  0%, 100% {
    transform:
      translate(var(--pipe-x), var(--pipe-y))
      rotate(var(--pipe-shape-rotate, 0deg))
      scale(var(--pipe-scale))
      scale(1);
  }
  25% {
    transform:
      translate(var(--pipe-x), var(--pipe-y))
      rotate(var(--pipe-shape-rotate, 0deg))
      scale(var(--pipe-scale))
      scale(1.07);
  }
  75% {
    transform:
      translate(var(--pipe-x), var(--pipe-y))
      rotate(var(--pipe-shape-rotate, 0deg))
      scale(var(--pipe-scale))
      scale(1.02);
  }
}
@keyframes endpoint-flow-stretch {
  0%, 100% {
    transform:
      translate(-50%, -50%)
      translate(calc(var(--endpoint-x) + var(--endpoint-role-x, 0px)), calc(var(--endpoint-y) + var(--endpoint-role-y, 0px)))
      scale(var(--endpoint-scale))
      scale(var(--endpoint-side-scale, 1))
      scale(var(--endpoint-role-scale, 1))
      scale(1);
  }
  25% {
    transform:
      translate(-50%, -50%)
      translate(calc(var(--endpoint-x) + var(--endpoint-role-x, 0px)), calc(var(--endpoint-y) + var(--endpoint-role-y, 0px)))
      scale(var(--endpoint-scale))
      scale(var(--endpoint-side-scale, 1))
      scale(var(--endpoint-role-scale, 1))
      scale(1.07);
  }
  75% {
    transform:
      translate(-50%, -50%)
      translate(calc(var(--endpoint-x) + var(--endpoint-role-x, 0px)), calc(var(--endpoint-y) + var(--endpoint-role-y, 0px)))
      scale(var(--endpoint-scale))
      scale(var(--endpoint-side-scale, 1))
      scale(var(--endpoint-role-scale, 1))
      scale(1.02);
  }
}
/* Fließ-Animation: grauer Overlay schiebt sich weg, farbiges Rohr wird sichtbar */
.cell > .piece-svg.flow-overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  filter: none;
  will-change: clip-path;
  transition: clip-path 0.30s ease-in-out;
}

#board-wrapper.flow .cell.has-piece {
  animation: pipe-flow-cell 2.4s ease-in-out infinite;
  animation-delay: var(--flow-delay, 0s);
}
#board-wrapper.flow .cell.has-piece
  .pipe-sprite {
  animation: pipe-flow-stretch 0.95s ease-in-out infinite;
  animation-delay: var(--flow-delay, 0s);
}
#board-wrapper.flow .endpoint-sprite {
  animation: pipe-flow-cell 2.4s ease-in-out infinite;
  animation-delay: var(--flow-delay, 0s);
}
#board-wrapper.flow .endpoint > .endpoint-sprite {
  animation:
    pipe-flow-cell 2.4s ease-in-out infinite,
    endpoint-flow-stretch 0.95s ease-in-out infinite;
  animation-delay:
    var(--flow-delay, 0s),
    var(--flow-delay, 0s);
}

/* ---------- Endpunkte ---------- */

.endpoint {
  position: relative;
  overflow: visible;
  /* Endpunkte ragen per transform in die Nachbarzellen — wenn dort ein
     Rohr-Sprite mit eigenem Stacking-Context liegt, wuerde es ohne z-index
     am Wrapper drueber gezeichnet. z-index hoch genug, dass alle Pipes
     darunter liegen. */
  z-index: 5;
}
.endpoint > .endpoint-sprite {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 3;
  transform: translate(-50%, -50%);
  width: calc(var(--cell-size) * 1.0);
  height: calc(var(--cell-size) * 1.0);
  max-width: none;
  object-fit: contain;
  filter: drop-shadow(0 5px 4px rgba(0, 0, 0, 0.36));
}
/* Endpunkte ragen bewusst aus der Spielflaeche heraus, sodass die Sprites
   wie Pipeline-Anschluesse am Holzrahmen wirken. */
.endpoint-side-left   { transform: translateX(calc(-0.38 * var(--cell-size))); }
.endpoint-side-right  { transform: translateX(calc( 0.38 * var(--cell-size))); }
.endpoint-side-top    { transform: translateY(calc(-0.38 * var(--cell-size))); }
.endpoint-side-bottom { transform: translateY(calc( 0.38 * var(--cell-size))); }

/* ---------- Tray ---------- */

.tray-frame {
  /* Rahmen kommt jetzt ueber border-image-slice (gleiches Prinzip wie die
     Popover-Cards). Die Hoehe waechst dadurch mit dem Inhalt — bei einer
     zusaetzlichen Slot-Reihe streckt sich nur das Mittelteil. */
  width: 100%;
  max-width: 720px;
  box-sizing: border-box;
  /* Vertikales Padding bewusst knapp gehalten, damit das Tray oben/unten
     nicht unnoetig Platz frisst. */
  padding: clamp(2px, 0.6vw, 6px) clamp(8px, 2vw, 16px);
  border-style: solid;
  border-color: transparent;
  border-width:
    var(--tray-border-top)
    var(--tray-border-right)
    var(--tray-border-bottom)
    var(--tray-border-left);
  border-image-source: url("assets/ui/tray-dock.png");
  border-image-slice:
    var(--tray-slice-top)
    var(--tray-slice-right)
    var(--tray-slice-bottom)
    var(--tray-slice-left)
    fill;
  border-image-width: 1;
  border-image-repeat: stretch;
  background: none;
}

#tray {
  /* Slot-Groesse: auf Mobile mindestens 58 px (gut greifbar), auf Desktop
     deutlich kleiner gedeckelt als die Brett-Zellen (dort waren 88 px zu
     gross). */
  --slot-size: clamp(58px, calc(var(--frame-w) * 0.13), 74px);
  --tray-cols: 5;
  display: grid;
  /* Spaltenzahl wird von game.js per --tray-cols gesetzt, damit Stuecke
     gleichmaessig auf 2 Reihen verteilt sind (ceil(count/2) Spalten). */
  grid-template-columns: repeat(var(--tray-cols), var(--slot-size));
  grid-template-rows: repeat(2, var(--slot-size));
  grid-auto-flow: row;
  justify-content: center;
  align-content: center;
  /* Row-Gap: knapp gehalten, damit die Tray-Box weniger Hoehe braucht
     und der Header (Logo) mehr Luft zum Banner bekommt. */
  gap: clamp(4px, 1.4vw, 14px) clamp(6px, 1.4vw, 12px);
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  /* Zusaetzliches Innen-Padding links/rechts, damit die Slots auch bei
     6 Spalten nicht an die Sprite-Raender stossen. */
  padding-inline: clamp(8px, 2vw, 22px);
  touch-action: none;
  transform: translateY(clamp(-14px, -1.8vw, -8px));
}

.tray-piece {
  position: relative;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
  background: url("assets/ui/tray-slot.png") center / 100% 100% no-repeat;
  transition: transform 0.08s;
  /* Touch-Fläche in den Gap-Bereich erweitern ohne Layout zu ändern */
  margin: -2px -3px;
  /* will-change stabilisiert den GPU-Layer — verhindert das kurze Aufblitzen
     aller Tray-Sprites beim Drag-Start (analoges Problem wie bei .pipe-sprite). */
  will-change: transform;
}
.tray-piece.tray-row-top {
  transform: translateY(-3px);
}
.tray-piece.tray-row-bottom {
  transform: translateY(3px);
}
.tray-piece:not(.empty):active { transform: translateY(3px) scale(0.97); }
.tray-piece.tray-row-top:not(.empty):active { transform: translateY(0px) scale(0.97); }
.tray-piece.tray-row-bottom:not(.empty):active { transform: translateY(6px) scale(0.97); }
.tray-piece.empty { opacity: 0.35; cursor: default; }

.tray-piece .count {
  position: absolute;
  bottom: clamp(-9px, -1.6vw, -6px);
  right: clamp(-6px, -0.8vw, -2px);
  min-width: clamp(22px, 5vw, 30px);
  height: clamp(21px, 5vw, 28px);
  padding: 0 6px;
  display: grid;
  place-items: center;
  border: 3px solid #fff4bc;
  border-radius: 8px;
  background: linear-gradient(180deg, #ffdf55 0%, #ffb51d 62%, #d77a07 100%);
  color: #3e2602;
  font-size: clamp(14px, 3.2vw, 20px);
  font-weight: 900;
  line-height: 1;
  pointer-events: none;
  text-align: center;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.42);
  box-shadow:
    inset 0 -3px 0 rgba(112, 61, 5, 0.28),
    0 3px 0 #6f3e05,
    0 6px 8px rgba(0, 0, 0, 0.22);
}

/* ---------- Ghost (Drag-Vorschau) ---------- */

.ghost {
  position: fixed;
  pointer-events: none;
  width: var(--cell-size);
  height: var(--cell-size);
  z-index: 1000;
  transform: translate(-50%, -50%) scale(1.1);
  filter: drop-shadow(0 10px 14px rgba(0, 0, 0, 0.62));
}

/* ---------- Konfetti (beim Level-Win) ---------- */

.confetti {
  position: fixed;
  pointer-events: none;
  z-index: 5000;
  border-radius: 2px;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.28);
  animation-name: confetti-burst;
  animation-timing-function: cubic-bezier(0.22, 0.78, 0.31, 1);
  animation-fill-mode: forwards;
  will-change: transform, opacity;
}
@keyframes confetti-burst {
  0% {
    transform: translate(0, 0) rotate(0deg);
    opacity: 1;
  }
  70% { opacity: 1; }
  100% {
    transform: translate(var(--dx, 0), calc(var(--dy, 0) + 320px)) rotate(var(--rot, 540deg));
    opacity: 0;
  }
}

.flying-star {
  position: fixed;
  left: 0;
  top: 0;
  width: 44px;
  height: 44px;
  pointer-events: none;
  z-index: 5100;
  opacity: 0;
  clip-path: polygon(
    50% 0%,
    62% 34%,
    98% 36%,
    70% 58%,
    79% 94%,
    50% 74%,
    21% 94%,
    30% 58%,
    2% 36%,
    38% 34%
  );
  background:
    radial-gradient(circle at 35% 30%, #fff8cf 0 20%, rgba(255, 248, 207, 0.4) 21%, transparent 22%),
    linear-gradient(180deg, #fff6b0 0%, #ffd94a 32%, #ffb31a 68%, #f08b00 100%);
  box-shadow:
    0 0 0 2px rgba(123, 66, 0, 0.55) inset,
    0 6px 14px rgba(0, 0, 0, 0.28),
    0 0 20px rgba(255, 220, 92, 0.42);
  animation: star-fly 1.6s cubic-bezier(0.2, 0.72, 0.22, 1) forwards;
  will-change: transform, opacity;
}

.flying-star.prime {
  animation: none;
  opacity: 0;
  transform: translate(-9999px, -9999px) scale(0.01);
}

.flying-gem {
  clip-path: polygon(50% 0%, 94% 42%, 50% 100%, 6% 42%);
  background:
    radial-gradient(circle at 32% 24%, #ffffff 0 13%, rgba(255, 255, 255, 0.45) 14%, transparent 18%),
    linear-gradient(135deg, #dffcff 0 28%, #34dcff 29% 54%, #1789eb 55% 100%);
  box-shadow:
    0 0 0 2px rgba(255, 246, 152, 0.82) inset,
    0 6px 14px rgba(0, 0, 0, 0.28),
    0 0 20px rgba(73, 227, 255, 0.5);
}

@keyframes star-fly {
  0% {
    opacity: 0;
    transform: translate(0, 0) scale(0.62) rotate(0deg);
  }
  16% {
    opacity: 1;
    transform: translate(calc(var(--burst-x) * 0.35), calc(var(--burst-y) * 0.35))
      scale(0.98) rotate(calc(var(--star-rot) * 0.32));
  }
  42% {
    opacity: 1;
    transform: translate(var(--burst-x), var(--burst-y))
      scale(1.12) rotate(calc(var(--star-rot) * 0.9));
  }
  72% {
    opacity: 1;
    transform: translate(
      calc(var(--burst-x) * 0.38 + var(--target-x) * 0.62),
      calc(var(--burst-y) * 0.38 + var(--target-y) * 0.62)
    )
      scale(0.88) rotate(calc(var(--star-rot) * 1.7));
  }
  100% {
    opacity: 0;
    transform: translate(var(--target-x), var(--target-y))
      scale(0.46) rotate(calc(var(--star-rot) * 2.35));
  }
}

/* ---------- Status ---------- */

#status {
  min-height: 1.5em;
  text-align: center;
  color: #ffffff;
  font-size: clamp(16px, 3.6vw, 24px);
  font-weight: 900;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.42);
}
#status.win {
  color: #fff6b6;
  font-size: clamp(22px, 5vw, 36px);
  text-shadow:
    0 2px 0 #7b3c00,
    0 5px 10px rgba(255, 177, 25, 0.5);
}

/* ---------- Menue (Popover) ---------- */

#menu {
  --menu-scale: 1;
  position: fixed;
  inset: 0;
  z-index: 520;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(clamp(14px, 3vw, 32px), env(safe-area-inset-top, 0px))
    clamp(14px, 3vw, 32px)
    max(clamp(14px, 3vw, 32px), env(safe-area-inset-bottom, 0px));
  overflow: hidden;
  background:
    radial-gradient(ellipse at 50% 46%,
      rgba(6, 22, 42, 0.36), rgba(4, 9, 18, 0.78) 68%),
    rgba(3, 10, 20, 0.58);
  backdrop-filter: blur(5px) saturate(0.78);
  -webkit-backdrop-filter: blur(5px) saturate(0.78);
  animation: menuFadeIn 0.18s ease-out;
}
#menu[hidden] { display: none; }
@keyframes menuFadeIn { from { opacity: 0; } to { opacity: 1; } }

.menu-card {
  --menu-content-overflow-x: clamp(14px, 3.6vw, 32px);
  --menu-content-overflow-y: clamp(8px, 2.2vw, 18px);
  position: relative;
  width: 100%;
  max-width: 720px;
  box-sizing: border-box;
  /* Innen-Padding klein halten — den optischen Rand liefert schon der
     panel-slice-card-Border (58/52 px). Hoehe waechst mit dem Inhalt. */
  padding: clamp(4px, 1vw, 10px) clamp(8px, 1.8vw, 24px) clamp(0px, 0.6vw, 6px);
  gap: clamp(4px, 1vw, 8px);
  display: flex;
  flex-direction: column;
  animation: menuRise 0.22s ease-out;
  filter: drop-shadow(0 18px 28px rgba(0, 0, 0, 0.55));
}
.menu-card > :not(.menu-close) {
  margin-inline:
    calc(-1 * var(--menu-content-overflow-x))
    calc(-1 * var(--menu-content-overflow-x));
}
.menu-card > .menu-item:first-of-type {
  margin-top: calc(-1 * var(--menu-content-overflow-y));
}
.menu-card > .menu-version {
  margin-bottom: calc(-1 * var(--menu-content-overflow-y));
}
@keyframes menuRise {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}

.menu-close {
  /* Trefferfläche zentriert auf das ins Panel gezeichnete X (obere rechte Ecke).
     Dessen Zentrum liegt ~0.45 * Rahmenbreite außerhalb der Content-Box; Größe ≈ X-Kreis.
     Vorher: 72–108px mit Ecken-Anker -> Fläche stand nach links/unten über und
     reichte nicht bis in die Ecke. */
  --close-size: clamp(48px, 11vw, 80px);
  position: absolute;
  z-index: 5; /* immer über dem Karteninhalt (z. B. coach z-index:2) -> stets klickbar */
  top:   calc(-0.45 * var(--panel-border-top)   - var(--close-size) / 2);
  right: calc(-0.45 * var(--panel-border-right) - var(--close-size) / 2);
  width:  var(--close-size);
  height: var(--close-size);
  padding: 0;
  font-size: 0;
  color: transparent;
  border: 0;
  cursor: pointer;
  background: transparent;
}
.menu-close:hover  { filter: brightness(1.08); }
.menu-close:active { transform: translateY(3px); }

.code-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(86px, 22vw, 152px);
  column-gap: clamp(8px, 1.6vw, 18px);
  row-gap: clamp(2px, 0.6vw, 6px);
  align-items: center;
  padding: 0;
  margin: 0;
}
/* Divider liegt im normalen Flow als eigene Grid-Reihe — skaliert mit der
   Card-Breite und ueberlappt niemals das Eingabefeld. */
.code-row::after,
.menu-divider {
  display: block;
  width: 100%;
  height: clamp(28px, 7.5vw, 64px);
  margin: 0 auto;
  background: url("assets/ui/menu-divider.png?v=20260529d") center / contain no-repeat;
  pointer-events: none;
}
.menu-spacer {
  height: clamp(10px, 2.4vw, 18px);
  pointer-events: none;
}
.code-row::after { content: ""; grid-column: 1 / -1; }

#code-input {
  font: inherit;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  text-transform: uppercase;
  text-align: center;
  width: 100%;
  height: clamp(48px, 10vw, 76px);
  padding: 0 clamp(10px, 2.4vw, 22px);
  border: 0;
  border-radius: clamp(12px, 3vw, 22px);
  background: url("assets/ui/menu-input.png") center / 100% 100% no-repeat;
  color: #fff1a8;
  font-size: clamp(22px, 5.2vw, 40px);
  font-weight: 800;
  letter-spacing: clamp(2px, 0.8vw, 6px);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.7);
}
#code-input::placeholder {
  color: rgba(255, 241, 168, 0.8);
  font-size: clamp(16px, 3.8vw, 28px);
  letter-spacing: clamp(1px, 0.4vw, 3px);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.55);
}
#code-input.bad {
  border: 2px solid var(--red);
  animation: shake 0.4s;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25%      { transform: translateX(-5px); }
  75%      { transform: translateX(5px); }
}

#code-go {
  font: inherit;
  width: 100%;
  height: clamp(48px, 10vw, 76px);
  padding: 0 clamp(10px, 2.4vw, 22px);
  border: 0;
  border-radius: clamp(12px, 3vw, 22px);
  font-size: clamp(20px, 4.6vw, 34px);
  font-weight: 950;
  text-align: center;
  color: #ffffff;
  cursor: pointer;
  background: url("assets/ui/menu-button-green.png") center / 100% 100% no-repeat;
  text-shadow:
    0 2px 0 #174700,
    0 4px 7px rgba(0, 0, 0, 0.36);
}

.menu-item {
  min-height: clamp(52px, 11vw, 82px);
  margin: 0;
  padding: 0 clamp(14px, 3vw, 36px);
  border: 0;
  border-radius: clamp(12px, 3vw, 22px);
  font: inherit;
  font-size: clamp(18px, 4.2vw, 32px);
  font-weight: 950;
  color: #ffffff;
  text-align: center;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: clamp(12px, 2vw, 16px);
  cursor: pointer;
  background: url("assets/ui/menu-button-blue.png?v=20260529d") center / 100% 100% no-repeat;
  text-shadow:
    0 2px 0 #052049,
    0 5px 8px rgba(0, 0, 0, 0.36);
}
.menu-item:hover  { filter: brightness(1.1); }
.menu-item:active,
#code-go:active   { transform: translateY(3px); }
.menu-item[aria-pressed="false"] {
  filter: saturate(0.72) brightness(0.9);
}
.menu-item-label {
  flex: 0 1 auto;
}
.menu-item-icon {
  flex: 0 0 auto;
  width: clamp(24px, 3.4vw, 30px);
  height: clamp(24px, 3.4vw, 30px);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.menu-icon-svg {
  width: 100%;
  height: 100%;
  display: block;
  filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.28));
  transform-box: fill-box;
  transform-origin: center;
}
.icon-sound .menu-icon-svg { transform: scale(0.9); }
.icon-restart-level .menu-icon-svg { transform: scale(0.92); }
.icon-restart-game .menu-icon-svg { transform: scale(1.18); }
.menu-item[aria-pressed="true"] .icon-muted-line {
  opacity: 0;
}
.menu-item[aria-pressed="false"] .icon-muted-line {
  opacity: 1;
}
.icon-whatsapp-brand {
  fill: none;
  stroke: none;
  transform: translateY(-1.5px) scale(1.08);
}
.icon-whatsapp-brand .whatsapp-bubble {
  fill: #25d366;
  stroke: #f7fff9;
  stroke-width: 1.25;
}
.icon-whatsapp-brand .whatsapp-phone {
  fill: #ffffff;
  stroke: none;
}
.menu-item.danger {
  background: url("assets/ui/menu-button-red.png?v=20260529d") center / 100% 100% no-repeat;
  text-shadow:
    0 2px 0 #520600,
    0 5px 8px rgba(0, 0, 0, 0.36);
}
/* Bronze-Färbung für Sound- und Auto-Help-Buttons (Toggle-Optionen) */
/* Musik- und Sound-Buttons: Bronze-Färbung */
#menu-music, #menu-sound {
  filter: sepia(1) saturate(2.5) brightness(1.15) hue-rotate(-12deg);
}
#menu-music:hover, #menu-sound:hover {
  filter: sepia(1) saturate(2.5) brightness(1.25) hue-rotate(-12deg);
}
#menu-music[aria-pressed="false"], #menu-sound[aria-pressed="false"] {
  filter: sepia(1) saturate(1.6) brightness(0.92) hue-rotate(-12deg);
}
/* Spacer nach den Sound-Schaltern (vor Restart) */
#menu-restart {
  margin-top: clamp(10px, 2.4vw, 18px);
  filter: sepia(0.6) saturate(2) hue-rotate(-20deg) brightness(0.95);
}
#menu-restart:hover { filter: sepia(0.6) saturate(2) hue-rotate(-20deg) brightness(1.08); }
.native-app .menu-share-label,
.native-app .menu-share-row { display: none; }

.menu-share-label {
  font-size: clamp(13px, 2.6vw, 17px);
  font-weight: 700;
  letter-spacing: 0.08em;
  color: #ffffff;
  text-align: center;
  margin-top: clamp(8px, 1.6vw, 14px);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.32);
}
.menu-share-row {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: clamp(8px, 1.8vw, 14px);
  margin-top: clamp(4px, 0.8vw, 7px);
}
.menu-share-btn {
  min-height: clamp(48px, 9.5vw, 68px);
  border: 0;
  border-radius: clamp(12px, 3vw, 20px);
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  cursor: pointer;
  background: url("assets/ui/menu-button-blue.png?v=20260529d") center / 100% 100% no-repeat;
  text-shadow:
    0 2px 0 #052049,
    0 5px 8px rgba(0, 0, 0, 0.36);
}
.menu-share-btn:hover { filter: brightness(1.1); }
.menu-share-btn:active { transform: translateY(3px); }
.menu-share-btn svg {
  width: clamp(23px, 4.8vw, 31px);
  height: clamp(23px, 4.8vw, 31px);
  display: block;
  filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.32));
  transform: translateY(-3px);
}
#share-whatsapp svg { fill: currentColor; stroke: none; }
.menu-version {
  margin-top: clamp(12px, 2.4vw, 18px);
  margin-bottom: clamp(-10px, -1.6vw, -5px);
  color: rgba(255, 255, 255, 0.5);
  font-size: clamp(10px, 2vw, 13px);
  font-weight: 700;
  line-height: 1.25;
  text-align: center;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.32);
}
.menu-version-sep {
  display: inline-block;
  margin-inline: 0.55em;
}
.menu-version a {
  color: rgba(255, 255, 255, 0.62);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.menu-version a:hover {
  color: rgba(255, 255, 255, 0.82);
}

/* ---------- Level-Übersicht (Popover) ---------- */

#overview {
  --menu-scale: 1;
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Padding-top reicht so weit, dass der Close-Button (haengt halb ueber
     die Card-Oberkante) zuverlaessig unter Notch / Statusbar Platz hat. */
  padding:
    max(env(safe-area-inset-top, 0px) + clamp(54px, 12vw, 80px),
        clamp(82px, 18vw, 120px))
    clamp(8px, 2vw, 22px)
    max(clamp(8px, 2vw, 22px), env(safe-area-inset-bottom, 0px));
  background:
    radial-gradient(ellipse at 50% 46%,
      rgba(6, 22, 42, 0.42), rgba(4, 9, 18, 0.82) 68%),
    rgba(3, 10, 20, 0.62);
  backdrop-filter: blur(5px) saturate(0.8);
  -webkit-backdrop-filter: blur(5px) saturate(0.8);
  animation: menuFadeIn 0.18s ease-out;
}
#overview[hidden] { display: none; }

#stars-info {
  position: fixed;
  inset: 0;
  z-index: 510;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(env(safe-area-inset-top, 0px) + clamp(54px, 12vw, 80px),
        clamp(82px, 18vw, 120px))
    clamp(12px, 3vw, 28px)
    max(clamp(12px, 3vw, 28px), env(safe-area-inset-bottom, 0px));
  background:
    radial-gradient(ellipse at 50% 46%,
      rgba(6, 22, 42, 0.42), rgba(4, 9, 18, 0.82) 68%),
    rgba(3, 10, 20, 0.62);
  backdrop-filter: blur(5px) saturate(0.8);
  -webkit-backdrop-filter: blur(5px) saturate(0.8);
  animation: menuFadeIn 0.18s ease-out;
}
#stars-info[hidden] { display: none; }

.stars-info-card {
  position: relative;
  width: min(100%, 620px);
  padding: 0 clamp(12px, 2vw, 22px) clamp(14px, 2.4vw, 22px);
  filter: drop-shadow(0 18px 28px rgba(0, 0, 0, 0.55));
  animation: menuRise 0.22s ease-out;
  overflow: visible;
}

.stars-info-body {
  display: flex;
  flex-direction: column;
  gap: clamp(10px, 2vw, 18px);
}

.stars-info-title {
  text-align: center;
  color: #fff7cf;
  font-size: clamp(24px, 4.8vw, 38px);
  font-weight: 950;
  text-shadow:
    0 2px 0 #052049,
    0 5px 8px rgba(0, 0, 0, 0.36);
}

.stars-info-row {
  display: grid;
  grid-template-columns: clamp(52px, 10vw, 76px) minmax(0, 1fr);
  align-items: center;
  gap: clamp(10px, 2vw, 18px);
}

.stars-info-row p {
  margin: 0;
  color: #ffffff;
  font-size: clamp(18px, 3.8vw, 28px);
  font-weight: 850;
  line-height: 1.15;
  text-shadow:
    0 2px 0 rgba(5, 32, 73, 0.9),
    0 4px 8px rgba(0, 0, 0, 0.36);
}

.stars-info-badge {
  width: 100%;
  aspect-ratio: 1 / 1;
  background: url("assets/ui/level-stars-0.png?v=20260529d") center / 100% 100% no-repeat;
  filter:
    drop-shadow(0 2px 0 rgba(6, 24, 44, 0.42))
    drop-shadow(0 4px 8px rgba(0, 0, 0, 0.35));
}
.stars-info-badge.stars1 { background-image: url("assets/ui/level-stars-1.png?v=20260529d"); }
.stars-info-badge.stars2 { background-image: url("assets/ui/level-stars-2.png?v=20260529d"); }
.stars-info-badge.stars3 { background-image: url("assets/ui/level-stars-3.png?v=20260529d"); }
#stars-info.gem-mode .stars-info-badge { background-image: url("assets/ui/level-gems-0.png?v=20260529d"); }
#stars-info.gem-mode .stars-info-badge.stars1 { background-image: url("assets/ui/level-gems-1.png?v=20260529d"); }
#stars-info.gem-mode .stars-info-badge.stars2 { background-image: url("assets/ui/level-gems-2.png?v=20260529d"); }
#stars-info.gem-mode .stars-info-badge.stars3 { background-image: url("assets/ui/level-gems-3.png?v=20260529d"); }

.overview-card {
  position: relative;
  width: 100%;
  max-width: 720px;
  max-height: 100%;
  /* Platz fuer den Close-Button wird vom #overview-Padding bereit gestellt. */
  margin-top: 0;
  display: flex;
  flex-direction: column;
  /* Nur kleines Innen-Padding — den Rahmen liefert die panel-slice-card. */
  padding: 0 clamp(6px, 1.2vw, 14px) clamp(6px, 1.2vw, 14px);
  filter: drop-shadow(0 18px 28px rgba(0, 0, 0, 0.55));
  animation: menuRise 0.22s ease-out;
  /* overflow visible, damit der X-Button oberhalb der Card sichtbar bleibt. */
  overflow: visible;
}

.panel-slice-card {
  border-style: solid;
  border-color: transparent;
  border-width:
    var(--panel-border-top)
    var(--panel-border-right)
    var(--panel-border-bottom)
    var(--panel-border-left);
  border-image-source: url("assets/ui/menu-panel.png?v=20260529d");
  border-image-slice:
    var(--panel-slice-top)
    var(--panel-slice-right)
    var(--panel-slice-bottom)
    var(--panel-slice-left)
    fill;
  border-image-width: 1;
  border-image-repeat: stretch;
  background: none;
}
/* Close-Button der Übersicht nutzt bewusst exakt dieselben Werte wie das
   normale Menü, damit beide Popover einheitlich wirken. */
.overview-close {}
/* Grid hat eigenen scroll-Container, damit nur die Liste scrollt, nicht die
   ganze Card (sonst rauscht der X-Button mit raus). */
.overview-card .overview-grid {
  flex: 1;
  min-height: 0;
}

.overview-grid {
  display: flex;
  flex-direction: column;
  gap: clamp(14px, 2.4vw, 22px);
  overflow-y: auto;
  padding: 4px;
  margin: -4px;
}
.overview-grid::-webkit-scrollbar { width: 10px; }
.overview-grid::-webkit-scrollbar-track { background: #0a1322; border-radius: 6px; }
.overview-grid::-webkit-scrollbar-thumb { background: #2c4773; border-radius: 6px; }

.overview-theme {
  display: flex;
  flex-direction: column;
  gap: clamp(7px, 1.3vw, 11px);
}
.overview-theme + .overview-theme {
  margin-top: clamp(16px, 2.2vw, 28px);
}
.overview-theme-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
  padding: 0 4px;
}
.overview-theme-header h3 {
  margin: 0;
  color: #ffffff;
  font-size: clamp(18px, 3vw, 28px);
  font-weight: 950;
  line-height: 1;
  text-shadow:
    0 2px 0 #062c68,
    0 4px 0 rgba(0, 0, 0, 0.28),
    0 0 12px rgba(255, 255, 255, 0.2);
}
.overview-theme-header p {
  margin: 0;
  color: rgba(255, 255, 255, 0.58);
  font-size: clamp(13px, 2.1vw, 17px);
  font-weight: 800;
  line-height: 1.1;
  text-align: right;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.3);
}
.overview-theme.locked .overview-theme-header h3 {
  color: rgba(255, 255, 255, 0.58);
}
.overview-theme-progress.progress-great {
  color: #70ff8a;
}
.overview-theme-progress.progress-good {
  color: #ffd95a;
}
.overview-theme-progress.progress-ok {
  color: #ffb257;
}
.overview-theme-progress.progress-start {
  color: rgba(255, 255, 255, 0.66);
}
.overview-theme-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  align-items: start;
  gap: clamp(6px, 1.4vw, 14px);
}
@media (max-width: 600px) {
  .overview-theme-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  .overview-theme + .overview-theme {
    margin-top: clamp(18px, 6vw, 30px);
  }
  .overview-theme-header {
    align-items: start;
    flex-direction: column;
    gap: 4px;
  }
  .overview-theme-header h3 {
    font-size: clamp(24px, 7vw, 34px);
  }
  .overview-theme-header p {
    text-align: left;
    font-size: clamp(15px, 4.3vw, 19px);
  }
}

.level-tile {
  /* Padding-bottom-Trick fuer aspect-ratio in Grid-Tracks: bei height:0 wird
     die Hoehe ausschliesslich vom prozentualen padding-bottom getrieben, und
     das wiederum bezieht sich auf die Track-Breite (1fr). aspect-ratio in
     Grid-Containern setzt keine Track-Hoehe und fuehrte zu Ueberlagerungen. */
  position: relative;
  width: 100%;
  height: 0;
  padding: 0 0 calc(100% * 200 / 120); /* 120:200 portrait */
  border: 0;
  margin: 0;
  border-radius: 12px;
  overflow: hidden;
  cursor: pointer;
  color: #fff;
  background: #0a1322;
  box-shadow:
    inset 0 0 0 2px rgba(255, 255, 255, 0.06),
    0 4px 10px rgba(0, 0, 0, 0.45);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
/* Eigener Bildlayer, damit der grayscale-Filter fuer locked-Tiles NUR das
   Vorschaubild ergreift und Schloss/Stern-Badges in voller Farbe bleiben. */
.level-tile-img {
  position: absolute;
  inset: 0;
  background: var(--tile-bg, none) center / cover no-repeat;
  z-index: 0;
}
.level-tile:hover {
  transform: translateY(-2px);
  box-shadow:
    inset 0 0 0 2px rgba(255, 220, 80, 0.55),
    0 8px 16px rgba(0, 0, 0, 0.55);
}
.level-tile::after {
  /* Dezenter dunkler Verlauf, damit die mittige Nummer auf hellen Bildern
     gut lesbar bleibt. */
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse at 50% 50%, rgba(0,0,0,0.45) 0%, rgba(0,0,0,0.2) 50%, rgba(0,0,0,0) 80%);
  pointer-events: none;
}
.level-tile-num {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  text-align: center;
  font-size: clamp(36px, 6vw, 64px);
  font-weight: 950;
  line-height: 1;
  color: #fff;
  text-shadow:
    0 3px 0 #062c68,
    0 6px 0 rgba(0, 0, 0, 0.5),
    0 0 16px rgba(0, 0, 0, 0.6);
  z-index: 2;
}
.level-tile-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 3;
}
.level-tile-badge.stars {
  width: clamp(40px, 7vw, 58px);
  aspect-ratio: 1 / 1;
  background: url("assets/ui/level-stars-0.png?v=20260529d") center / 100% 100% no-repeat;
  filter:
    drop-shadow(0 2px 0 rgba(6, 24, 44, 0.42))
    drop-shadow(0 4px 8px rgba(0, 0, 0, 0.45));
}
.level-tile-badge.stars[data-stars="1"] { background-image: url("assets/ui/level-stars-1.png?v=20260529d"); }
.level-tile-badge.stars[data-stars="2"] { background-image: url("assets/ui/level-stars-2.png?v=20260529d"); }
.level-tile-badge.stars[data-stars="3"] { background-image: url("assets/ui/level-stars-3.png?v=20260529d"); }
.level-tile-badge.gems {
  width: clamp(40px, 7vw, 58px);
  aspect-ratio: 1 / 1;
  background: url("assets/ui/level-gems-0.png?v=20260529d") center / 100% 100% no-repeat;
  filter:
    drop-shadow(0 2px 0 rgba(6, 24, 44, 0.42))
    drop-shadow(0 4px 8px rgba(0, 0, 0, 0.45));
}
.level-tile-badge.gems[data-stars="1"] { background-image: url("assets/ui/level-gems-1.png?v=20260529d"); }
.level-tile-badge.gems[data-stars="2"] { background-image: url("assets/ui/level-gems-2.png?v=20260529d"); }
.level-tile-badge.gems[data-stars="3"] { background-image: url("assets/ui/level-gems-3.png?v=20260529d"); }
.level-tile-badge.locked {
  width: clamp(34px, 5vw, 46px);
  height: clamp(34px, 5vw, 46px);
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: clamp(20px, 2.8vw, 28px);
  font-weight: 900;
  line-height: 1;
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.3),
    0 3px 6px rgba(0, 0, 0, 0.55);
}
.level-tile.locked .level-tile-badge.locked {
  background: linear-gradient(180deg, #60708c 0%, #273348 100%);
  color: #fff;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.45);
}
/* Nicht freigeschaltete Levels werden grau-weiss dargestellt. Freigespielte
   Levels (auch noch ungelöste) bleiben farbig. Filter NUR auf den Bildlayer,
   damit das rote Schloss-Badge in voller Farbe bleibt. */
/* Farbe kehrt zurück je nach Sternen — wie bei den Hintergrundbildern im Spiel */
.level-tile:not(.locked):not(.bonus-tile)[data-stars="0"] .level-tile-img { filter: grayscale(1); }
.level-tile:not(.locked):not(.bonus-tile)[data-stars="1"] .level-tile-img { filter: grayscale(0.55); }
.level-tile:not(.locked):not(.bonus-tile)[data-stars="2"] .level-tile-img { filter: grayscale(0.2); }
.level-tile:not(.locked):not(.bonus-tile)[data-stars="3"] .level-tile-img { filter: grayscale(0); }
/* Bonus-Tiles: Hintergrund liegt auf dem Tile selbst */
.playable-bonus-tile:not(.locked)[data-stars="0"] { filter: grayscale(1); }
.playable-bonus-tile:not(.locked)[data-stars="1"] { filter: grayscale(0.55); }
.playable-bonus-tile:not(.locked)[data-stars="2"] { filter: grayscale(0.2); }
.playable-bonus-tile:not(.locked)[data-stars="3"] { filter: grayscale(0); }

.level-tile.locked { cursor: not-allowed; }
.level-tile.locked .level-tile-img {
  filter: grayscale(1) brightness(0.55);
}
.level-tile.locked:hover {
  transform: none;
  box-shadow:
    inset 0 0 0 2px rgba(255, 255, 255, 0.06),
    0 4px 10px rgba(0, 0, 0, 0.45);
}
.level-tile.current {
  /* Outline liegt ueber dem Bildlayer (inset box-shadow waere darunter). */
  outline: 3px solid #ffc529;
  outline-offset: -3px;
  box-shadow:
    0 6px 14px rgba(255, 197, 41, 0.32);
}
.bonus-tile {
  display: block;
  cursor: default;
  background:
    radial-gradient(circle at 50% 18%, rgba(255, 255, 255, 0.12), transparent 30%),
    linear-gradient(180deg, #123e55 0%, #16294f 48%, #12172f 100%);
  box-shadow:
    inset 0 0 0 2px rgba(94, 178, 202, 0.34),
    inset 0 -12px 26px rgba(0, 0, 0, 0.34),
    0 4px 10px rgba(0, 0, 0, 0.45);
}
.bonus-tile-word,
.bonus-tile-number {
  display: block;
}
.bonus-tile-number {
  margin-top: 4px;
  font-size: 1.7em;
  line-height: 0.85;
}
.bonus-tile::after {
  background:
    linear-gradient(135deg, rgba(255,255,255,0.08) 0 18%, transparent 18% 36%, rgba(255,255,255,0.05) 36% 54%, transparent 54%),
    radial-gradient(ellipse at 50% 50%, rgba(0,0,0,0.28) 0%, rgba(0,0,0,0.14) 52%, rgba(0,0,0,0.02) 82%);
}
.bonus-tile:hover {
  transform: none;
  box-shadow:
    inset 0 0 0 2px rgba(94, 178, 202, 0.34),
    inset 0 -12px 26px rgba(0, 0, 0, 0.34),
    0 4px 10px rgba(0, 0, 0, 0.45);
}
.playable-bonus-tile {
  cursor: pointer;
  background:
    linear-gradient(180deg, rgba(7, 17, 35, 0.42), rgba(7, 17, 35, 0.7)),
    var(--tile-bg, none) center / cover no-repeat,
    linear-gradient(180deg, #1b2740 0%, #10172b 100%);
  box-shadow:
    inset 0 0 0 3px rgba(255, 217, 90, 0.92),
    inset 0 0 0 7px rgba(34, 48, 74, 0.72),
    inset 0 -18px 30px rgba(0, 0, 0, 0.38),
    0 5px 14px rgba(0, 0, 0, 0.5);
}
.playable-bonus-tile .level-tile-img {
  display: none;
}
.playable-bonus-tile .bonus-tile-label {
  top: 50%;
  color: #ffd95a;
  text-transform: uppercase;
  text-shadow:
    0 2px 0 rgba(83, 47, 0, 0.78),
    0 0 14px rgba(255, 217, 90, 0.42);
}
.playable-bonus-tile .bonus-tile-soon {
  display: none;
}
.playable-bonus-tile:hover {
  transform: translateY(-2px);
  box-shadow:
    inset 0 0 0 3px rgba(255, 230, 120, 0.98),
    inset 0 0 0 7px rgba(45, 64, 96, 0.8),
    inset 0 -18px 30px rgba(0, 0, 0, 0.34),
    0 9px 18px rgba(0, 0, 0, 0.58);
}
.bonus-tile-lock {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 3;
  width: clamp(34px, 5vw, 46px);
  height: clamp(34px, 5vw, 46px);
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: clamp(20px, 2.8vw, 28px);
  font-weight: 900;
  line-height: 1;
  color: #fff;
  background: linear-gradient(180deg, #60708c 0%, #273348 100%);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.45);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.22),
    0 3px 6px rgba(0, 0, 0, 0.55);
}
.bonus-tile-label,
.bonus-tile-soon {
  position: absolute;
  left: 50%;
  width: calc(100% - clamp(12px, 2vw, 24px));
  z-index: 2;
  display: block;
  text-align: center;
  text-shadow:
    0 2px 0 rgba(5, 28, 61, 0.72),
    0 0 12px rgba(105, 238, 255, 0.36);
}
.bonus-tile-label {
  top: 40%;
  transform: translate(-50%, -50%);
  color: #ffffff;
  font-size: clamp(16px, 2.4vw, 26px);
  font-weight: 950;
  line-height: 1.08;
}
.bonus-tile-soon {
  top: 66%;
  transform: translate(-50%, -50%);
  color: #9ef7ff;
  font-size: clamp(12px, 1.7vw, 17px);
  font-weight: 900;
  line-height: 1.15;
}

/* ---------- Welt-Reaktion: Sky-Flash bei 3 Sternen ---------- */

@keyframes sky-flash {
  0%   { opacity: 1; }
  18%  { opacity: 0.05; }
  55%  { opacity: 0.18; }
  100% { opacity: 1; }
}
/* Hintergrund-Vorschau per Long-Press: alle Spielelemente ausblenden */
body.show-background main,
body.show-background #logo,
body.show-background .round-btn,
body.show-background .header-right-stack,
body.show-background #stars-info,
body.show-background #menu,
body.show-background #toast {
  visibility: hidden;
}
body.show-background #level-stars {
  visibility: visible;
}
body.show-background .sky::after {
  opacity: 0;
  transition: opacity 0.2s ease;
}

body.sky-flash .sky::after {
  animation: sky-flash 1.8s ease-out forwards;
}

/* Stern-Bloom: Hintergrund leuchtet grell auf, pendelt in neuer Farbe ein */
@keyframes sky-bloom-after {
  0%   { opacity: 1; }
  14%  { opacity: 0; }
  40%  { opacity: 0; }
  100% { opacity: 1; }
}
@keyframes sky-bloom-img {
  0%   { filter: brightness(1)   saturate(1); }
  14%  { filter: brightness(2.4) saturate(2); }
  40%  { filter: brightness(1.5) saturate(1.5); }
  100% { filter: brightness(1)   saturate(1); }
}
body.star-bloom .sky::after {
  animation: sky-bloom-after 1.8s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}
body.star-bloom .sky {
  animation: sky-bloom-img 1.8s cubic-bezier(0.22, 1, 0.36, 1) forwards;
}

/* ---------- Story-Screen ---------- */

#story-screen {
  position: fixed;
  inset: 0;
  z-index: 400;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  /* Platz für Header oben (inkl. Safe-Area für PWA/Notch), Platz für Nora unten */
  padding-top: max(clamp(130px, 22vw, 260px), calc(env(safe-area-inset-top, 0px) + clamp(110px, 20vw, 220px)));
  padding-bottom: clamp(180px, 38vh, 340px);
  overflow: hidden;
}
#story-screen[hidden] { display: none; }
#story-wrap {
  position: relative;
  width: 100%;
  max-width: 720px;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}
#story-bg {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center top;
  background-color: #04101e;
}
#story-bg::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(2, 10, 22, 0.72);
}
#story-content {
  position: relative;
  z-index: 1;
  width: 100%;
  padding: clamp(16px, 3vh, 40px) clamp(130px, 36vw, 270px) clamp(16px, 3vh, 36px) calc(var(--header-btn-inset) + 4px);
  text-align: left;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0;
  animation: story-fade-in 0.7s ease both;
}
@keyframes story-fade-in {
  from { opacity: 0; transform: translateY(18px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes story-character-slide-in {
  from { opacity: 0; transform: translateY(40px); }
  to   { opacity: 1; transform: translateY(0); }
}
#story-chapter {
  color: rgba(255, 210, 80, 0.9);
  font-size: clamp(14px, 2.8vw, 18px);
  font-weight: 900;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  margin: 0 0 clamp(4px, 1vh, 10px);
  text-shadow:
    0 1px 0 rgba(80, 40, 0, 0.6),
    0 0 16px rgba(255, 180, 0, 0.35);
}
#story-chapter:empty { display: none; }
#story-title {
  color: #ffffff;
  font-size: clamp(20px, 4.5vw, 42px);
  font-weight: 950;
  letter-spacing: 0.01em;
  margin: 0 0 clamp(12px, 2.5vh, 28px);
  text-shadow:
    0 3px 0 rgba(5, 20, 50, 0.5),
    0 6px 16px rgba(0, 0, 0, 0.5);
}
#story-title:empty { display: none; }
#story-text {
  color: #d8e8f8;
  font-size: clamp(19px, 3.8vw, 25px);
  font-weight: 600;
  line-height: 1.65;
  margin: 0 0 clamp(28px, 5vh, 72px);
  white-space: pre-line;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.7);
  letter-spacing: 0.01em;
}
#story-content.has-pieces #story-text {
  margin-bottom: clamp(14px, 2.8vh, 28px);
}
#story-pieces {
  display: flex;
  align-items: center;
  gap: clamp(12px, 3vw, 24px);
  margin: 0 0 clamp(22px, 4vh, 48px);
}
#story-pieces[hidden] { display: none; }
.story-piece {
  position: relative;
  width: clamp(62px, 13vw, 104px);
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  background: url("assets/ui/cell-empty.png") center / 100% 100% no-repeat;
  filter:
    drop-shadow(0 0 8px rgba(100, 220, 255, 0.9))
    drop-shadow(0 0 18px rgba(60, 180, 255, 0.6))
    drop-shadow(0 7px 10px rgba(0, 0, 0, 0.42));
}
.story-piece.locked {
  background-image: url("assets/ui/cell-locked.png");
}
.story-piece.blocked {
  background-image: url("assets/ui/cell-rock-plate.png");
}
.story-piece.blocked.slime {
  background-image: url("assets/ui/cell-slime-plate.png");
}
.story-piece.blocked.bush {
  background-image: url("assets/ui/cell-bush-plate.png");
}
.story-piece.blocked.ice {
  background-image: url("assets/ui/cell-ice-plate.png");
}
.story-piece-img {
  position: relative;
  z-index: 1;
  width: 88%;
  height: 88%;
  object-fit: contain;
}
#story-stars {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 0 0 clamp(20px, 3.5vh, 40px);
}
#story-stars[hidden] { display: none; }
#story-stars-badge {
  font-size: clamp(32px, 7vw, 52px);
  line-height: 1;
  flex-shrink: 0;
}
#story-stars-label {
  color: #fff;
  font-size: clamp(22px, 4.5vw, 34px);
  font-weight: 950;
  text-shadow:
    0 2px 0 rgba(5, 20, 50, 0.5),
    0 4px 12px rgba(0, 0, 0, 0.4);
}
#story-close {
  min-height: clamp(52px, 11vw, 82px);
  min-width: clamp(160px, 40vw, 280px);
  margin: 0;
  padding: 0 clamp(14px, 3vw, 36px);
  border: 0;
  border-radius: clamp(12px, 3vw, 22px);
  font: inherit;
  font-size: clamp(18px, 4.2vw, 32px);
  font-weight: 950;
  color: #ffffff;
  cursor: pointer;
  background: url("assets/ui/menu-button-blue.png?v=20260529d") center / 100% 100% no-repeat;
  text-shadow:
    0 2px 0 #052049,
    0 5px 8px rgba(0, 0, 0, 0.36);
}
#story-close:hover { filter: brightness(1.1); }
#story-close:active { transform: translateY(3px); }
#story-character {
  position: absolute;
  bottom: clamp(20px, 5vh, 60px);
  right: calc(max(0px, (100vw - 720px) / 2) - 40px);
  height: clamp(260px, 58vh, 500px);
  width: auto;
  object-fit: contain;
  object-position: bottom;
  pointer-events: none;
  animation: story-character-slide-in 0.7s 0.2s ease both;
  filter: drop-shadow(-4px 0 12px rgba(0,0,0,0.35));
}

/* ---------- Level-Tile Eye-Button ---------- */

.level-tile-eye {
  position: absolute;
  bottom: 7px;
  left: 7px;
  z-index: 4;
  width: clamp(44px, 8vw, 58px);
  height: clamp(44px, 8vw, 58px);
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  opacity: 1;
  transition: background 0.15s ease;
  padding: 0;
}
.level-tile-eye svg {
  width: 55%;
  height: 55%;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  display: block;
}
.level-tile-eye:hover {
  background: rgba(0, 0, 0, 0.78);
}
@keyframes eye-pulse {
  0%, 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(220, 50, 30, 0.5); }
  50%       { transform: scale(1.12); box-shadow: 0 0 0 5px rgba(220, 50, 30, 0); }
}
.level-tile-eye.unviewed {
  background: rgba(210, 40, 20, 0.82);
  animation: eye-pulse 0.9s ease-in-out infinite;
}
.level-tile-eye.unviewed:hover {
  background: rgba(230, 60, 30, 0.95);
  animation: none;
}

/* ---------- Coach-Dialog ---------- */

#coach-dialog {
  position: fixed;
  inset: 0;
  z-index: 530;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(2, 10, 24, 0.55);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  padding: clamp(16px, 4vw, 48px);
}
#coach-dialog[hidden] { display: none; }

.coach-wrap {
  position: relative;
  max-width: min(94vw, 680px);
  width: 100%;
}
.coach-card {
  /* WICHTIG: relativ positionieren, damit der absolute .menu-close-Button an der KARTE
     ankert (deckt sich mit dem ins Panel gezeichneten X) und nicht am größeren .coach-wrap
     — sonst schwebt das X-Trefferfeld neben/über der Kartenecke und der Tap verpufft. */
  position: relative;
  --coach-content-overflow-x: clamp(24px, 5.5vw, 52px);
  --coach-content-overflow-y: clamp(12px, 3vw, 24px);
  width: 100%;
  padding: clamp(20px, 3.5vw, 36px);
  padding-right: clamp(100px, 20vw, 160px);
  display: flex;
  flex-direction: column;
  gap: clamp(10px, 1.6vw, 18px);
  align-items: stretch;
}
.coach-card > #coach-text,
.coach-card > .coach-buttons,
.coach-card > .coach-stars {
  margin-left: calc(-1 * var(--coach-content-overflow-x));
  box-sizing: border-box;
}
.coach-card > :first-child {
  margin-top: calc(-1 * var(--coach-content-overflow-y));
}
.coach-card > :last-child {
  margin-bottom: calc(-1 * var(--coach-content-overflow-y));
}
#coach-text, .coach-buttons, .coach-stars {
  position: relative;
  z-index: 2;
}
.coach-nora {
  z-index: 1;
}
#coach-dialog.no-stars .coach-stars { display: none; }
.coach-stars {
  font-size: clamp(26px, 5.5vw, 40px);
  line-height: 1;
}
@keyframes star3-pop {
  0%   { transform: scale(0.3) rotate(-20deg); opacity: 0; }
  65%  { transform: scale(1.3) rotate(8deg);   opacity: 1; }
  100% { transform: scale(1) rotate(0deg);     opacity: 1; }
}
.coach-star3 {
  display: inline-block;
  animation: star3-pop 0.7s 0.35s cubic-bezier(0.34,1.56,0.64,1) both;
}
#coach-text {
  color: #d8e8f8;
  font-size: clamp(18px, 3.5vw, 23px);
  font-weight: 600;
  line-height: 1.55;
  margin: 0;
  white-space: pre-line;
  text-shadow: 0 2px 8px rgba(0,0,0,0.5);
}
.coach-buttons {
  display: flex;
  flex-direction: column;
  gap: clamp(8px, 1.8vw, 12px);
  margin-top: clamp(8px, 2vw, 16px);
}
.coach-buttons .menu-item {
  width: 100%;
  min-height: clamp(48px, 10vw, 68px);
  font-size: clamp(13px, 2.4vw, 18px);
  padding-inline: clamp(8px, 2vw, 20px);
}
.coach-nora {
  position: absolute;
  right: clamp(-8px, -1vw, 6px);
  bottom: clamp(40px, 8vw, 70px);
  height: clamp(250px, 50vh, 420px);
  width: auto;
  object-fit: contain;
  object-position: bottom;
  pointer-events: none;
  filter: drop-shadow(-4px 0 10px rgba(0,0,0,0.35));
}

/* ---------- Hintergrundbild-Lightbox ---------- */

#bg-lightbox {
  position: fixed;
  inset: 0;
  z-index: 600;
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  cursor: pointer;
}
#bg-lightbox[hidden] { display: none; }
#bg-lightbox-img {
  max-height: 92vh;
  max-width: 96vw;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 10px;
  box-shadow: 0 12px 60px rgba(0, 0, 0, 0.72);
  pointer-events: none;
}

/* ---------- Toast ---------- */

/* Eigener Compositing-Layer für Drag-Ghosts — isoliert vom body, damit
   appendChild() keine tray-weite Repaint auf iOS auslöst. */
#ghost-container {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 999;
  will-change: transform;
}

#toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: linear-gradient(180deg, #2e3a5a 0%, #1c243b 100%);
  color: var(--text);
  padding: 12px 22px;
  border: 2px solid #4a5a82;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 800;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 540;
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.5);
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- Sprite-Finetuning ----------

   Die source2-Templates sind quadratisch auf Kante gebaut. Deshalb bleiben
   hier nur noch globale Regler fuer Brett, Tray und Endpunkte.
*/

:root {
  --pipe-scale: 1;
  --pipe-x: 0px;
  --pipe-y: 0px;

  --tray-pipe-scale: 0.84;
  --tray-pipe-x: 0px;
  --tray-pipe-y: -3px;

  --endpoint-size: 1;
  --endpoint-scale: 1;
  --endpoint-x: 0px;
  --endpoint-y: 0px;
  --endpoint-start-scale: 1;
  --endpoint-start-x: 0px;
  --endpoint-start-y: 0px;
  --endpoint-end-scale: 1;
  --endpoint-end-x: 0px;
  --endpoint-end-y: 0px;
  --endpoint-left-x: calc(-0.50 * var(--cell-size));
  --endpoint-left-y: 0px;
  --endpoint-left-scale: 1;
  --endpoint-right-x: calc(0.50 * var(--cell-size));
  --endpoint-right-y: 0px;
  --endpoint-right-scale: 1;
  --endpoint-top-x: 0px;
  --endpoint-top-y: calc(-0.5 * var(--cell-size));
  --endpoint-top-scale: 1;
  --endpoint-bottom-x: 0px;
  --endpoint-bottom-y: calc(0.50 * var(--cell-size));
  --endpoint-bottom-scale: 1;
}

.cell > .pipe-sprite {
  transform:
    translate(var(--pipe-x), var(--pipe-y))
    rotate(var(--pipe-shape-rotate, 0deg))
    scale(var(--pipe-scale));
}

.tray-piece > .pipe-sprite {
  transform:
    translate(var(--tray-pipe-x), var(--tray-pipe-y))
    rotate(var(--pipe-shape-rotate, 0deg))
    scale(var(--tray-pipe-scale));
}

.endpoint {
  position: relative;
  overflow: visible;
}
.endpoint-side-left {
  transform: translate(var(--endpoint-left-x), var(--endpoint-left-y));
  --endpoint-side-scale: var(--endpoint-left-scale);
}
.endpoint-side-right {
  transform: translate(var(--endpoint-right-x), var(--endpoint-right-y));
  --endpoint-side-scale: var(--endpoint-right-scale);
}
.endpoint-side-top {
  transform: translate(var(--endpoint-top-x), var(--endpoint-top-y));
  --endpoint-side-scale: var(--endpoint-top-scale);
}
.endpoint-side-bottom {
  transform: translate(var(--endpoint-bottom-x), var(--endpoint-bottom-y));
  --endpoint-side-scale: var(--endpoint-bottom-scale);
}
.endpoint-start {
  --endpoint-role-scale: var(--endpoint-start-scale);
  --endpoint-role-x: var(--endpoint-start-x);
  --endpoint-role-y: var(--endpoint-start-y);
}
.endpoint-end {
  --endpoint-role-scale: var(--endpoint-end-scale);
  --endpoint-role-x: var(--endpoint-end-x);
  --endpoint-role-y: var(--endpoint-end-y);
}
.endpoint > .endpoint-sprite {
  position: absolute;
  left: 50%;
  top: 50%;
  width: calc(var(--cell-size) * var(--endpoint-size));
  height: calc(var(--cell-size) * var(--endpoint-size));
  transform:
    translate(-50%, -50%)
    translate(calc(var(--endpoint-x) + var(--endpoint-role-x, 0px)), calc(var(--endpoint-y) + var(--endpoint-role-y, 0px)))
    scale(var(--endpoint-scale))
    scale(var(--endpoint-side-scale, 1))
    scale(var(--endpoint-role-scale, 1));
}

/* ---------- Mobile ---------- */

@media (max-width: 520px) {
  body { padding-inline: 6px; }

  header {
    min-height: clamp(108px, 32vw, 144px);
    padding-inline: 52px;
  }
  #story-content {
    padding-left: calc(var(--header-btn-inset) + 10px);
  }
  #logo {
    width: min(62vw, 260px);
    max-height: 110px;
  }

  .board-banner .code-chip { min-width: 44px; letter-spacing: 1px; }

  .tray-frame { padding: 4px 15px 6px; }
}

@media (max-width: 360px) {
  header { padding-inline: 50px; }
}

/* ---------- Paywall (Free-to-Play Prototyp) ---------- */
#paywall {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 12px max(16px, env(safe-area-inset-bottom));
  background:
    radial-gradient(ellipse at 50% 46%,
      rgba(6, 22, 42, 0.42), rgba(4, 9, 18, 0.82) 68%),
    rgba(3, 10, 20, 0.62);
  -webkit-backdrop-filter: blur(5px) saturate(0.8);
  backdrop-filter: blur(5px) saturate(0.8);
  animation: menuFadeIn 0.18s ease-out;
}
#paywall[hidden] { display: none; }
.paywall-card {
  position: relative;
  width: min(460px, 100%);
  max-height: 100%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  text-align: center;
  color: #eaf3ff;
  filter: drop-shadow(0 18px 28px rgba(0, 0, 0, 0.55));
  animation: menuRise 0.22s ease-out;
}
/* Scrollen im inneren Wrapper — die Card selbst bleibt overflow:visible, sonst
   würde sie den Schließen-Button (sitzt per Negativ-Offset in der Rahmenzone)
   abschneiden und unklickbar machen. */
.paywall-scroll {
  min-height: 0;
  overflow-y: auto;
  padding: clamp(2px, 1vw, 8px) clamp(4px, 1.5vw, 12px);
}
.paywall-title {
  margin: 0 0 6px;
  color: #fff7cf;
  font-size: clamp(21px, 5.2vw, 28px);
  font-weight: 950;
  text-shadow: 0 2px 0 #052049, 0 5px 8px rgba(0, 0, 0, 0.36);
}
.paywall-sub { margin: 0 0 18px; font-size: 15px; color: rgba(220,234,255,0.85); }
.paywall-sub strong { color: #ffd257; }

.paywall-teaser {
  margin: 0 0 18px; padding: 16px 12px 14px;
  background: rgba(0,0,0,0.22);
  border: 1px solid rgba(120,180,255,0.2);
  border-radius: 16px;
}
.pw-eq { display: flex; align-items: center; justify-content: center; gap: 6px; }
.pw-eq img { width: clamp(34px, 11vw, 48px); height: clamp(34px, 11vw, 48px); object-fit: contain; }
.pw-eq .pw-mixer {
  width: clamp(46px, 14vw, 62px); height: clamp(46px, 14vw, 62px);
  filter: drop-shadow(0 0 6px rgba(180, 120, 255, 0.9));
  animation: pw-mixer-glow 1.8s ease-in-out infinite;
}
@keyframes pw-mixer-glow {
  0%, 100% { filter: drop-shadow(0 0 5px rgba(180,120,255,0.6)); transform: scale(1); }
  50%      { filter: drop-shadow(0 0 14px rgba(200,140,255,1)); transform: scale(1.08); }
}
.pw-op { font-size: clamp(18px, 5vw, 22px); font-weight: 800; color: #cfe0f7; opacity: 0.9; }
.pw-teaser-label { margin: 12px 0 0; font-size: 13px; color: rgba(220,234,255,0.85); }
.pw-teaser-label strong { color: #d79bff; }

.paywall-features { list-style: none; margin: 0 0 20px; padding: 0; text-align: left; }
.paywall-features li {
  display: flex; align-items: flex-start; gap: 9px;
  padding: 8px 4px; font-size: 14.5px; color: #e6f0ff;
  border-bottom: 1px solid rgba(255,255,255,0.07);
}
.paywall-features li:last-child { border-bottom: 0; }
.paywall-features .pw-ico { flex: 0 0 1.5em; text-align: center; line-height: 1.35; }
.paywall-features .pw-txt { flex: 1 1 auto; line-height: 1.35; }

.paywall-cta {
  display: block; width: 100%;
  padding: 15px; margin: 0 0 10px;
  font-size: 17px; font-weight: 900; color: #06122a;
  background: linear-gradient(180deg, #ffd257, #ffb01f);
  border: 0; border-radius: 999px; cursor: pointer;
  box-shadow: 0 6px 16px rgba(255, 176, 31, 0.35);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.paywall-cta:hover { transform: translateY(-1px); box-shadow: 0 8px 20px rgba(255,176,31,0.45); }
.paywall-cta:active { transform: translateY(0); }
.paywall-restore {
  display: block; width: 100%; padding: 6px;
  background: none; border: 0; cursor: pointer;
  font-size: 13px; color: rgba(200,216,240,0.75); text-decoration: underline;
}
.paywall-restore:hover { color: #fff; }

/* Toast (Kauf-/Restore-Bestätigung) */
#pp-toast {
  position: fixed;
  left: 50%;
  bottom: calc(40px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%) translateY(12px);
  z-index: 600;
  max-width: 80%;
  padding: 12px 20px;
  background: rgba(10, 26, 52, 0.97);
  color: #eaf3ff;
  border: 1px solid rgba(120, 180, 255, 0.4);
  border-radius: 999px;
  font-size: 15px; font-weight: 700; text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  opacity: 0; pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}
#pp-toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Übersicht: kostenpflichtige (aber freigespielte) Kacheln markieren */
.level-tile.paywalled::after {
  content: "🔒";
  position: absolute; top: 4px; left: 4px;
  font-size: 13px;
  filter: drop-shadow(0 1px 2px rgba(0,0,0,0.6));
  pointer-events: none;
}
