/* ==========================================================
   Tauber Tourist Guide: Gestaltung
   Idee: ruhige Nachtfarben, Mondgold als einziger Akzent,
   jede Fraktion bekommt eine eigene Farbe (Farbton aus der JSON).
   Handy zuerst: eine Spalte, große Tippflächen, Leiste unten.
   ========================================================== */

:root {
  /* Hell (Standard) */
  --bg: #f2f1f6;
  --surface: #ffffff;
  --surface-2: #e9e7f1;
  --line: #d4d1e0;
  --fg: #1c1d2a;
  --muted: #5d5f74;
  --accent: #7a5a12;
  --accent-fg: #ffffff;
  --accent-soft: #f0e3bd;
  --danger: #a3262a;

  /* Fraktionsfarbe = Farbton (--h, kommt aus der JSON) + Sättigung + Helligkeit */
  --fac-s: 55%;
  --fac-l: 38%;
  --portrait-fg: #ffffff;

  --font-display: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --font-body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --radius: 10px;
  /* Unterer Sicherheitsabstand (Home-Balken beim iPhone). Safari ändert diesen Wert je nachdem, ob die Browserleiste
     ein- oder ausgeklappt ist, dadurch wurde die untere Leiste mal höher, mal niedriger. Auf iPhones im Hochformat
     gilt deshalb immer mindestens der große Wert, die Leiste bleibt gleich hoch. */
  --safe-b: env(safe-area-inset-bottom, 0px);
  --gutter: max(16px, env(safe-area-inset-left, 0px));
  --gutter-r: max(16px, env(safe-area-inset-right, 0px));
}

/* Dunkel, wenn das Gerät auf "dunkel" steht (und nicht ausdrücklich "hell" gewählt ist) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #13151f;
    --surface: #1c1f2e;
    --surface-2: #252a3d;
    --line: #33384f;
    --fg: #e9e7f2;
    --muted: #a2a5bd;
    --accent: #e0bf6a;
    --accent-fg: #1a1608;
    --accent-soft: #3a3322;
    --danger: #ff8a8d;
    --fac-l: 62%;
    --portrait-fg: #10121a;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #13151f;
  --surface: #1c1f2e;
  --surface-2: #252a3d;
  --line: #33384f;
  --fg: #e9e7f2;
  --muted: #a2a5bd;
  --accent: #e0bf6a;
  --accent-fg: #1a1608;
  --accent-soft: #3a3322;
  --danger: #ff8a8d;
  --fac-l: 62%;
  --portrait-fg: #10121a;
  color-scheme: dark;
}

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

html { -webkit-text-size-adjust: 100%; }

body {
  box-sizing: border-box;
  /* Die Seite ist immer 1px höher als der Bildschirm, also immer scrollbar. Sonst rutscht beim iPhone die untere
     Leiste auf kurzen Seiten (wenig Troupes) nach oben. */
  min-height: calc(100vh + 1px);
  min-height: calc(100dvh + 1px);
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 var(--font-body);
  padding-block: max(12px, env(safe-area-inset-top, 0px)) calc(5.5rem + var(--safe-b));
  padding-left: var(--gutter);
  padding-right: var(--gutter-r);
}
/* Im Editor gibt es keine untere Leiste, also weniger Platz darunter */
body.editing { padding-block-end: calc(2rem + var(--safe-b)); }

main, .topbar { max-width: 62rem; margin-inline: auto; }

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

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- Kopfzeile ---------- */
.topbar { display: flex; align-items: baseline; justify-content: space-between; gap: 0.75rem; flex-wrap: wrap; padding-block: 0.5rem 1rem; }
.topbar h1 { margin: 0; display: flex; align-items: center; gap: 0.5rem; font: 600 1.6rem/1.1 var(--font-display); letter-spacing: 0.01em; }
.logo { width: 1.5rem; height: 1.5rem; color: var(--accent); }
.sub { margin: 0; color: var(--muted); font-size: 0.85rem; }
body.editing .topbar { display: none; }

.note { margin: 0 0 1rem; padding: 0.6rem 0.8rem; background: var(--accent-soft); border-radius: var(--radius); font-size: 0.9rem; }

/* ---------- Formularelemente ---------- */
label { display: block; margin-block: 1rem 0.35rem; font-size: 0.85rem; color: var(--muted); }

input[type="search"], input[type="text"], select, textarea {
  width: 100%;
  min-height: 44px;
  padding: 0.55rem 0.75rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
textarea { resize: vertical; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 0.85rem; }

button {
  min-height: 44px;
  padding: 0.5rem 1rem;
  background: var(--accent);
  color: var(--accent-fg);
  border: 0;
  border-radius: var(--radius);
  font-weight: 600;
}
button.ghost { background: transparent; color: var(--fg); border: 1px solid var(--line); }
button.ghost.danger { color: var(--danger); border-color: var(--danger); }
button.link { background: none; min-height: 44px; padding: 0 0.25rem; color: var(--accent); text-decoration: underline; font-weight: 500; }
button:disabled { opacity: 0.45; cursor: not-allowed; }

/* ---------- Filter (Kompendium und Editor) ---------- */
.filters { display: grid; gap: 0.6rem; margin-bottom: 1.25rem; }

/* Fraktionssymbole: mehrere gleichzeitig wählbar */
.faction-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(4.5rem, 1fr)); gap: 0.5rem; }
.faction-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  min-height: 64px;
  padding: 0.45rem 0.1rem;
  background: var(--surface);
  color: hsl(var(--h) var(--fac-s) var(--fac-l));
  border: 2px solid var(--line);
  border-radius: var(--radius);
  font-weight: 500;
}
.faction-icon { width: 1.7rem; height: 1.7rem; display: grid; place-items: center; font: 600 0.8rem/1 var(--font-display); }
.faction-icon svg, .faction-icon img { width: 100%; height: 100%; }
.faction-name { color: var(--fg); font-size: 0.64rem; line-height: 1.15; text-align: center; overflow-wrap: break-word; }
.faction-btn[aria-pressed="true"] { border-color: hsl(var(--h) var(--fac-s) var(--fac-l)); background: hsl(var(--h) var(--fac-s) var(--fac-l) / 0.18); }
.faction-btn.blocked { opacity: 0.4; }

/* Aufklappbares Keyword-Feld */
.keyword-box { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.keyword-box summary {
  min-height: 44px;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0 0.75rem;
  cursor: pointer;
  font-weight: 600;
  list-style: none;
}
.keyword-box summary::-webkit-details-marker { display: none; }
.keyword-box summary::before {
  content: "";
  flex: none;
  width: 0.45rem;
  height: 0.45rem;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(-45deg);
}
.keyword-box[open] summary::before { transform: rotate(45deg); }
.keyword-box .chips { padding: 0.25rem 0.75rem 0.75rem; }
.keyword-selected { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--accent); font-weight: 500; font-size: 0.9rem; }

.chips { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.chip {
  min-height: 40px;
  padding: 0.3rem 0.85rem;
  background: var(--surface);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: 999px;
  font-weight: 500;
  font-size: 0.9rem;
}
.chip[aria-pressed="true"] { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }

.filter-meta { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; color: var(--muted); font-size: 0.9rem; }

/* ---------- Karten im Kompendium ---------- */
.cards { display: grid; gap: 0.75rem; grid-template-columns: repeat(auto-fill, minmax(min(100%, 19rem), 1fr)); }

.card {
  display: flex;
  align-items: stretch;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.card-main {
  flex: 1;
  min-width: 0;
  display: flex;
  gap: 0.8rem;
  align-items: center;
  padding: 0.75rem;
  background: none;
  color: var(--fg);
  text-align: left;
  font-weight: 400;
  border-radius: 0;
}
.card-text { min-width: 0; display: grid; gap: 0.15rem; }
.card-name { font: 600 1.05rem/1.25 var(--font-display); overflow-wrap: anywhere; }
.card-faction { display: flex; align-items: center; flex-wrap: wrap; column-gap: 0.75rem; row-gap: 0.1rem; font-size: 0.85rem; color: var(--muted); }
.faction-tag { display: inline-flex; align-items: center; gap: 0.35rem; }
.dot { flex: none; width: 0.65rem; height: 0.65rem; border-radius: 50%; background: hsl(var(--h) var(--fac-s) var(--fac-l)); }
.card-kw { font-size: 0.8rem; color: var(--muted); }

.portrait {
  flex: none;
  width: 3.25rem;
  height: 3.25rem;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: hsl(var(--h) var(--fac-s) var(--fac-l));
  color: var(--portrait-fg);
  font: 600 1.1rem/1 var(--font-display);
  letter-spacing: 0.02em;
  overflow: hidden;
}
.portrait img { width: 100%; height: 100%; object-fit: cover; }
.portrait.big { width: 4.5rem; height: 4.5rem; font-size: 1.5rem; }
.portrait.small { width: 2.75rem; height: 2.75rem; font-size: 0.95rem; }
.portrait.mini { width: 1.9rem; height: 1.9rem; font-size: 0.7rem; }

.empty { color: var(--muted); padding: 1rem 0; }

/* ---------- Troupen-Übersicht ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; }
.page-head h2 { margin: 0; font: 600 1.3rem/1.2 var(--font-display); }
.add-btn {
  /* fest unten rechts, über der unteren Leiste */
  position: fixed;
  right: max(1rem, env(safe-area-inset-right, 0px));
  bottom: calc(5rem + var(--safe-b));
  z-index: 4;
  box-sizing: border-box;
  width: 3.5rem;
  height: 3.5rem;
  min-width: 3.5rem;
  min-height: 3.5rem;
  aspect-ratio: 1 / 1;   /* bleibt immer rund */
  padding: 0;
  border-radius: 50%;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
}
/* Das Plus wird aus zwei Strichen gezeichnet, so sitzt es immer genau in der Mitte */
.add-btn .plus { position: absolute; left: 50%; top: 50%; width: 1.4rem; height: 1.4rem; transform: translate(-50%, -50%); }
.add-btn .plus::before, .add-btn .plus::after {
  content: "";
  position: absolute;
  background: currentColor;
  border-radius: 2px;
}
.add-btn .plus::before { left: 0; right: 0; top: calc(50% - 1.5px); height: 3px; }
.add-btn .plus::after { top: 0; bottom: 0; left: calc(50% - 1.5px); width: 3px; }
.add-btn .add-text { display: none; }
.add-btn .mini-row { display: none; }
.new-wrap:not(.as-card) { display: contents; }
/* Solange Platz ist (Handy und Desktop): der Knopf sieht aus wie eine Troupe-Karte (gleiche Größe und Aufbau) */
/* Die Karte ist ein Rahmen (.new-wrap) mit zwei Knöpfen: Plus links unten, Import rechts unten */
.new-wrap.as-card {
  position: relative;
  display: grid;
  background: var(--surface);
  border: 1px dashed var(--muted);
  border-radius: var(--radius);
}
.new-wrap.as-card.full { opacity: 0.5; }
.new-wrap.as-card .add-btn {
  z-index: auto;
  position: static;
  display: grid;
  gap: 0.4rem;
  width: auto;
  height: auto;
  min-width: 0;
  min-height: 0;
  aspect-ratio: auto;
  padding: 0.9rem;
  border: 0;
  border-radius: var(--radius);
  box-shadow: none;
  background: none;
  color: var(--fg);
  text-align: left;
  font-weight: 400;
}
.new-wrap.as-card .add-text { display: contents; }
.new-wrap.as-card .mini-row { display: flex; }
.new-wrap.as-card .plus { position: absolute; left: 0.9rem; top: auto; bottom: 0.9rem; transform: none; width: 1.9rem; height: 1.9rem; }
/* Ohne Platz für die Karte: Import als grüner (Leshavult) runder Knopf links neben dem Plus */
.new-wrap:not(.as-card) #import-open {
  position: fixed;
  right: calc(5.25rem + max(0px, env(safe-area-inset-right, 0px)));   /* links neben dem Plus-Knopf */
  bottom: calc(5rem + var(--safe-b));
  z-index: 4;
  box-sizing: border-box;
  width: 3.5rem;
  height: 3.5rem;
  min-height: 0;
  padding: 0;
  border-radius: 50%;
  background: hsl(140 var(--fac-s) var(--fac-l));   /* gleicher Ton wie der Leshavult-Filter */
  border: 0;
  color: var(--accent-fg);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
}
.new-wrap:not(.as-card) #import-open span { display: none; }
.new-wrap:not(.as-card) #import-open .ico { width: 1.5rem; height: 1.5rem; }
.new-wrap.as-card #import-open { position: absolute; z-index: 2; right: 0.9rem; bottom: 0.9rem; min-height: 0; height: 2.4rem; padding: 0 0.8rem; }
#view-troupes { padding-bottom: 4.5rem; }

.troupe-list { display: grid; gap: 0.75rem; grid-template-columns: repeat(auto-fill, minmax(min(100%, 19rem), 1fr)); }
.troupe-card {
  display: grid;
  gap: 0.4rem;
  min-height: 0;
  padding: 0.9rem;
  background: var(--surface);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  text-align: left;
  font-weight: 400;
}
.troupe-item { position: relative; display: grid; }
.troupe-item .troupe-card { padding-right: 3rem; }
/* Feststehende Fraktion: Rahmen und Ecke in den Farben des gewählten Fraktionsfilters */
.troupe-item.has-faction { border-radius: var(--radius); overflow: hidden; }
.troupe-item.has-faction .troupe-card { border: 2px solid hsl(var(--h) var(--fac-s) var(--fac-l)); padding: calc(0.9rem - 1px); padding-right: calc(3rem - 1px); }
/* Zwei mögliche Fraktionen (Troupe nur aus Zwei-Fraktionen-Modellen): Rahmen, Ecke und Symbole halb/halb wie bei Magic */
.troupe-item.dual { --c1: hsl(var(--h) var(--fac-s) var(--fac-l)); --c2: hsl(var(--h2) var(--fac-s2) var(--fac-l)); }
.troupe-item.dual .troupe-card {
  border-color: transparent;
  background: linear-gradient(var(--surface), var(--surface)) padding-box, linear-gradient(90deg, var(--c1), var(--c2)) border-box;
}
.troupe-item.dual .troupe-corner::before {
  background: linear-gradient(90deg, hsl(var(--h) var(--fac-s) var(--fac-l) / 0.2), hsl(var(--h2) var(--fac-s2) var(--fac-l) / 0.2));
}
.troupe-item.dual .troupe-corner { display: flex; align-items: flex-end; justify-content: flex-end; gap: 0.3rem; width: 10.5rem; height: 4.2rem; padding: 0 0.7rem 0.55rem 0; }
.troupe-item.dual .troupe-corner .faction-icon { width: 1.55rem; height: 1.55rem; }
.troupe-item.dual .troupe-corner .faction-icon:nth-child(1) { color: var(--c1); }
.troupe-item.dual .troupe-corner .faction-icon:nth-child(2) { color: var(--c2); }
.tile-tip { margin: 0.25rem 0 0.5rem; }
.troupe-corner {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 9rem;
  height: 3.9rem;
  display: grid;
  place-items: end end;
  padding: 0 0.8rem 0.6rem 0;
  box-sizing: border-box;
  pointer-events: none;   /* Tippen geht durch zur Kachel */
  color: hsl(var(--h) var(--fac-s) var(--fac-l));
}
.troupe-corner::before {
  content: "";
  position: absolute;
  inset: 0;
  background: hsl(var(--h) var(--fac-s) var(--fac-l) / 0.18);
  clip-path: polygon(100% 0, 100% 100%, 0 100%);
}
.troupe-corner .faction-icon { position: relative; width: 1.9rem; height: 1.9rem; }   /* Platz für das X */
.troupe-delete {
  position: absolute;
  top: 0;
  right: 0;
  min-height: 0;
  min-width: 2.75rem;
  height: 2.75rem;
  padding: 0 0.25rem;
  display: grid;
  place-items: center;
  background: none;
  border: 0;
  border-radius: 0;
}
.troupe-delete .slot-x { background: var(--surface-2); border-color: var(--line); color: var(--fg); }
.troupe-delete.armed .del-text {
  padding: 0.25rem 0.6rem;
  border-radius: 99px;
  background: var(--danger);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 600;
}
.icon-btn { display: inline-flex; align-items: center; justify-content: center; gap: 0.45rem; }
.icon-btn .ico { width: 1.15rem; height: 1.15rem; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.export-btn { margin-top: 0.75rem; }
#import-dialog { width: min(92vw, 28rem); }
#import-dialog textarea { width: 100%; box-sizing: border-box; font-size: 16px; }   /* mindestens 16px, sonst zoomt iOS beim Antippen hinein */
.dialog-actions { display: flex; gap: 0.5rem; margin-top: 0.75rem; }
.troupe-name { font: 600 1.15rem/1.25 var(--font-display); overflow-wrap: anywhere; }
.troupe-meta { color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.mini-row { display: flex; flex-wrap: wrap; gap: 0.25rem; min-height: 1.9rem; }
/* Alle Kacheln gleich hoch: Platz für zwei Portrait-Zeilen. Feste Anordnung: 4 = 2x2, 8 = 2x4, 9 = 5+4 */
.mini-row { min-height: 4.05rem; align-content: flex-start; }
.mini-row[data-n="4"], .mini-row[data-n="8"], .mini-row[data-n="9"] { display: grid; justify-content: start; grid-template-columns: repeat(var(--mcols), 1.9rem); }
.mini-row[data-n="4"] { --mcols: 2; }
.mini-row[data-n="8"] { --mcols: 4; }
.mini-row[data-n="9"] { --mcols: 5; }

.share { margin-top: 1.5rem; padding: 0.75rem 1rem 1rem; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.share summary { cursor: pointer; font-weight: 600; min-height: 44px; display: flex; align-items: center; }
.share button { margin-top: 0.6rem; margin-right: 0.5rem; }
.hint { color: var(--muted); font-size: 0.9rem; margin: 0.25rem 0 0; }

/* ---------- Troupe-Editor ---------- */
.editor-bar {
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-left: calc(-1 * var(--gutter));
  margin-right: calc(-1 * var(--gutter-r));
  padding: 0.5rem var(--gutter-r) 0.5rem var(--gutter);
  background: var(--bg);
  flex-wrap: wrap;
}
.editor-bar input { flex: 1; min-width: 0; font: 600 1.05rem/1.3 var(--font-display); }
.back { flex: none; display: inline-flex; align-items: center; gap: 0.4rem; padding-inline: 0.75rem; }
.back::before {
  content: "";
  width: 0.45rem;
  height: 0.45rem;
  border-left: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(45deg);
}

.badge { font-variant-numeric: tabular-nums; margin-left: 0.25rem; padding: 0.05rem 0.45rem; border-radius: 99px; background: var(--surface-2); color: var(--fg); font-size: 0.8rem; }
.badge.big { flex: none; margin: 0; padding: 0.2rem 0.65rem; font-size: 0.95rem; font-weight: 600; }


/* Portraitfläche (Bild oder Silhouette in der Fraktionsfarbe) */
.pcard {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: hsl(var(--h) var(--fac-s) 24%);
}
.pcard svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.pcard svg circle, .pcard svg path { fill: hsl(var(--h) var(--fac-s) 44%); }
.pcard img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* Die acht Plätze oben: Portraits, Tipp = Karte ansehen */
.slots { display: grid; gap: 0.5rem; grid-template-columns: repeat(4, minmax(0, 1fr)); margin: 0.6rem 0 0.5rem; }   /* Handy: 4 pro Reihe, also 2 x 4 */
@media (min-width: 720px) {
  .slots { grid-template-columns: repeat(8, minmax(0, 1fr)); }   /* größerer Bildschirm: alle 8 Plätze in einer Reihe */
}
.slot-portrait, .slot-empty {
  position: relative;
  display: block;
  aspect-ratio: 5 / 6;   /* gleiche Größe wie die Kacheln unten */
  min-height: 0;
  padding: 0;
  border-radius: var(--radius);
  overflow: hidden;
  border: 2px dashed var(--line);
  background: var(--surface);
  color: #f1effa;
  text-align: left;
  font-weight: 400;
}
.slot-portrait { border: 2px solid var(--accent); }
.slot-open {
  position: absolute;
  inset: 0;
  min-height: 0;
  padding: 0;
  background: none;
  border: 0;
  border-radius: 0;
  color: inherit;
  text-align: left;
  font-weight: 400;
}
.slot-remove {
  position: absolute;
  top: 0;
  right: 0;
  z-index: 2;
  width: 2.75rem;
  height: 2.75rem;
  min-height: 0;
  padding: 0;
  display: grid;
  place-items: center;
  background: none;
  border: 0;
  border-radius: 0;
}
.slot-x {
  width: 1.6rem;
  height: 1.6rem;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgba(19, 21, 31, 0.88);
  border: 1px solid rgba(255, 255, 255, 0.45);
  color: #fff;
  font-size: 1.15rem;
  line-height: 1;
}

/* Auswahlkacheln: drei pro Reihe, Portrait mit Namen darunter */
.tiles { display: grid; gap: 0.5rem; grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr)); }
.tile {
  position: relative;
  display: block;
  aspect-ratio: 5 / 6;
  min-height: 0;
  padding: 0;
  overflow: hidden;
  background: var(--surface);
  color: #f1effa;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  text-align: left;
  font-weight: 400;
}
.tile-empty { aspect-ratio: 5 / 6; border: 2px dashed var(--line); border-radius: var(--radius); opacity: 0.7; }
.tile-label {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: grid;
  gap: 0.25rem;
  padding: 1.1rem 0.45rem 0.4rem;
  background: linear-gradient(to top, rgba(19, 21, 31, 0.92) 70%, rgba(19, 21, 31, 0));
}
.tile-name {
  font: 600 0.78rem/1.2 var(--font-display);
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.tile-factions { display: flex; gap: 0.25rem; min-height: 0.65rem; }
.tile[aria-pressed="true"] { border-color: var(--accent); }
.tile-check {
  position: absolute;
  top: 0.4rem;
  right: 0.4rem;
  width: 1.4rem;
  height: 1.4rem;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 700;
  font-size: 0.85rem;
}
.tile.blocked { opacity: 0.45; }

/* ---------- Untere Leiste ---------- */
.tabs {
  position: fixed;
  inset: auto 0 0 0;
  display: flex;
  gap: 0.5rem;
  padding: 0.5rem var(--gutter-r) calc(0.5rem + var(--safe-b)) var(--gutter);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.tabs button {
  flex: 1;
  background: none;
  color: var(--muted);
  border: 1px solid transparent;
}
.tabs button[aria-selected="true"] { background: var(--accent-soft); color: var(--accent); border-color: var(--line); }
body.editing .tabs { display: none; }

/* ---------- Detailansicht ---------- */
dialog {
  width: min(34rem, calc(100% - 2rem));
  max-height: 85vh;
  padding: 0;
  background: var(--surface);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: 14px;
}
dialog::backdrop { background: rgba(8, 9, 16, 0.6); }
.detail { padding: 1.25rem; display: grid; gap: 1rem; }
.detail-head { display: flex; gap: 1rem; align-items: center; }
.detail-head h2 { margin: 0; font: 600 1.4rem/1.2 var(--font-display); overflow-wrap: anywhere; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem; margin: 0; }
.stats div { background: var(--surface-2); border-radius: var(--radius); padding: 0.5rem 0.25rem; text-align: center; }
.stats dt { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.stats dd { margin: 0; font: 600 1.4rem/1.2 var(--font-display); font-variant-numeric: tabular-nums; }
.abilities { display: grid; gap: 0.75rem; margin: 0; padding: 0; list-style: none; }
.abilities h3 { margin: 0 0 0.15rem; font-size: 1rem; }
.abilities p { margin: 0; max-width: 65ch; }
.detail-actions { display: flex; gap: 0.5rem; }
.detail-actions button { flex: 1; }
.tag-row { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.tag { padding: 0.15rem 0.65rem; border-radius: 99px; background: var(--surface-2); font-size: 0.85rem; }
.card-image { display: block; width: 100%; max-height: 65vh; object-fit: contain; border-radius: var(--radius); }

/* ---------- Hinweis-Meldung ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(5.5rem + var(--safe-b));
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 2rem);
  padding: 0.6rem 1rem;
  background: var(--fg);
  color: var(--bg);
  border-radius: var(--radius);
  font-size: 0.9rem;
  text-align: center;
  z-index: 10;
}
body.editing .toast { bottom: calc(1.5rem + var(--safe-b)); }

@media (prefers-reduced-motion: no-preference) {
  .chip, button { transition: background-color 0.15s, color 0.15s; }
}

/* Leer-Hinweis über die ganze Breite des Rasters */
.cards .empty, .tiles .empty, .troupe-list .empty { grid-column: 1 / -1; }


/* ---------- Kartenansicht (Karte groß, Tipp = umdrehen) ---------- */
dialog.card-view {
  width: min(22rem, calc((100dvh - 10rem) / 1.4), calc(100vw - 2rem));
  max-height: none;
  overflow: visible;
  background: transparent;
  border: 0;
}
dialog.card-view::backdrop { background: rgba(8, 9, 16, 0.88); }
.card-view .detail { padding: 0; gap: 0.75rem; justify-items: center; }

.card-side { min-height: 1.2rem; color: #c9cbe0; font-size: 0.8rem; letter-spacing: 0.08em; text-transform: uppercase; }
.card-stage { position: relative; width: 100%; }

.card-flip {
  display: block;
  width: 100%;
  aspect-ratio: 5 / 7;
  min-height: 0;
  padding: 0;
  background: none;
  border: 0;
  border-radius: 14px;
  perspective: 1400px;
}
.card-inner { position: relative; width: 100%; height: 100%; transform-style: preserve-3d; transition: transform 0.55s; }
.card-flip.flipped .card-inner { transform: rotateY(180deg); }
.card-face { position: absolute; inset: 0; border-radius: 14px; overflow: hidden; backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.card-face.back { transform: rotateY(180deg); }
.card-face img {
  /* absolut platziert: Vorder- und Rückseite füllen exakt dasselbe 5:7-Feld,
     auch wenn die Bilddateien leicht unterschiedlich groß sind */
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  object-fit: fill;
}

.card-close {
  width: 75%;
  min-height: 56px;
  background: var(--surface);
  color: var(--fg);
  border: 1px solid var(--line);
  font-size: 1.1rem;
}

/* Kleiner Tipp auf der Karte, mit "Nicht mehr anzeigen" */
.card-hint {
  position: absolute;
  left: 0.75rem;
  right: 0.75rem;
  bottom: 0.75rem;
  display: grid;
  gap: 0.25rem;
  padding: 0.75rem;
  background: var(--surface);
  color: var(--fg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  font-size: 0.9rem;
}
.card-hint p { margin: 0; }
.card-hint .hint-check { display: flex; align-items: center; gap: 0.5rem; min-height: 44px; margin: 0; color: var(--muted); font-size: 0.85rem; }
.card-hint .hint-check input { width: 1.25rem; height: 1.25rem; flex: none; }

@media (prefers-reduced-motion: reduce) {
  .card-inner { transition: none; }
}

/* Kleinere Namen auf den schmalen Plätzen oben (Handy, 4 pro Reihe) */
.slot-portrait .tile-label { padding-inline: 0.3rem; }
.slot-portrait .tile-name { font-size: 0.66rem; }
@media (min-width: 60rem) {
  .slot-portrait .tile-label { padding-inline: 0.45rem; }
  .slot-portrait .tile-name { font-size: 0.78rem; }
}

/* Fraktion der Troupe in der Hinweiszeile des Editors */
.rule-faction { font-weight: 600; color: var(--fg); }

/* Hinweiszeile im Editor: die Fraktion der Troupe */
.rule-faction { display: inline-flex; align-items: center; gap: 0.45rem; font-weight: 600; color: var(--fg); }

/* Beschworene Charaktere: eigene Zeile unter den Plätzen */
.section-label { margin: 0.75rem 0 0; font-size: 0.8rem; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.hint-inline { font-weight: 400; letter-spacing: 0; text-transform: none; }
#editor-summons .slots { margin-top: 0.4rem; }
.slot-portrait.summoned { border: 2px dashed var(--accent); }

/* ---------- Gameview ---------- */
.play-btn { width: 100%; margin-top: 0.75rem; }
.icon-btn .ico.solid { fill: currentColor; stroke-width: 1; }
/* Desktop: Play füllt die Zeile, Export steht rechts daneben (gleiche Höhe), zusammen so breit wie vorher der Play-Knopf allein */
@media (min-width: 720px) {
  .action-row { display: flex; gap: 0.75rem; margin: 0.75rem 0 1rem; }
  .action-row .play-btn { flex: 1 1 auto; width: auto; margin: 0; }
  .action-row .export-btn { flex: none; margin: 0; }
}
@media (max-width: 719.98px) { .action-row { display: contents; } }

/* Handy: Play und Export schweben als runde Knöpfe unten rechts (nur Symbol) */
@media (max-width: 719.98px) {
  #start-game, #copy {
    position: fixed;
    right: max(1rem, env(safe-area-inset-right, 0px));
    z-index: 4;
    box-sizing: border-box;
    width: 3.5rem;
    height: 3.5rem;
    min-height: 0;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 50%;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
  }
  #start-game span, #copy span { display: none; }
  #start-game .ico, #copy .ico { width: 1.6rem; height: 1.6rem; }
  #start-game { bottom: calc(1.5rem + var(--safe-b)); }   /* wie der Plus-Knopf: Akzentfarbe */
  #copy {
    bottom: calc(5.75rem + var(--safe-b));
    background: hsl(140 var(--fac-s) var(--fac-l));   /* gleiches Grün wie der Import-Knopf */
    color: var(--accent-fg);
  }
  #view-editor { padding-bottom: 9rem; }   /* damit die Knöpfe keine Kacheln verdecken */
}
.game-title { flex: 1; margin: 0; font: 600 1.15rem/1.2 var(--font-display); }
.game-tools { display: grid; grid-template-columns: 1fr 1fr; gap: 0.4rem; margin-top: 1rem; }   /* Abstand, damit das Ausblenden der Kopfzeile die Knöpfe nicht überdeckt */
.game-tools button { padding-inline: 0.4rem; font-size: 0.85rem; }
.game-startbar {
  position: sticky;
  bottom: 0;
  z-index: 4;
  padding: 0.6rem 0 calc(0.6rem + var(--safe-b));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.game-startbar button { width: 100%; }

/* 5 Modelle nebeneinander */
.grow { display: grid; grid-template-columns: repeat(var(--gcols, 5), minmax(0, 1fr)); gap: 0.3rem; align-items: start; }
/* leicht durchscheinender, gräulicher Hintergrund hinter jedem Modell: bessere Lesbarkeit von Leben und Energy */
.gcell { display: flex; flex-direction: column; gap: 0.2rem; min-width: 0; padding: 3px; border-radius: 10px; background: rgba(170, 175, 195, 0.13); }
.gcell.dead { opacity: 0.5; }
.gcell-portrait {
  position: relative;
  display: block;
  width: 100%;
  aspect-ratio: 5 / 6;
  min-height: 0;
  padding: 0;
  overflow: hidden;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: none;
}
.gcell-name { font: 600 0.6rem/1.15 var(--font-display); text-align: center; overflow-wrap: anywhere; min-height: 3.5em; }

/* Lebensleiste: blaue Punkte erzeugen Energy, verlorene Lebenspunkte sind rot */
.hboxes { min-height: 1.35rem; align-content: flex-start; display: flex; flex-wrap: wrap; justify-content: center; gap: 2px; }
.hboxes.none { font-size: 0.55rem; color: var(--muted); text-align: center; }
.hbox { width: 0.55rem; height: 0.55rem; border-radius: 50%; border: 1.5px solid var(--muted); box-sizing: border-box; }
.hbox.blue { background: #3d7fe0; border-color: #3d7fe0; }
.hbox.struck { background: #d94141; border-color: #d94141; opacity: 1; }

.gstep { display: flex; flex-direction: column; align-items: stretch; }
.gstep-label { font-size: 0.5rem; line-height: 1.2; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gstep-row { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 2px; }
.gstep-row button { min-height: 32px; padding: 0; border-radius: 6px; font-size: 1rem; line-height: 1; }
.gstep-value { min-width: 0.9rem; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; font-size: 0.85rem; }

@media (min-width: 720px) {
  .gcell-name { font-size: 0.8rem; }
  .hbox { width: 0.7rem; height: 0.7rem; }
  .gstep-label { font-size: 0.65rem; }
  .gstep-value { font-size: 1rem; }
}

/* Beschworene Modelle, die noch nicht im Spiel sind */
.gcell.unsummoned .gcell-portrait { filter: grayscale(1); opacity: 0.45; }
.gcell.unsummoned .gcell-name { opacity: 0.6; }
#game-foe-chosen { margin-bottom: 0.75rem; }
.gcell-media { position: relative; }
.transform-btn, .summon-btn {
  position: absolute;
  left: 4px;
  right: 4px;
  top: 4px;
  min-height: 26px;
  padding: 0;
  border-radius: 6px;
  font-size: 0.6rem;
  background: var(--surface);
  color: var(--accent);
  border: 1px solid var(--accent);
}
.summon-note {
  position: absolute;
  left: 4px;
  right: 4px;
  top: 4px;
  display: grid;
  place-items: center;
  min-height: 26px;
  border-radius: 6px;
  font-size: 0.6rem;
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--line);
  text-align: center;
  pointer-events: none;
}

/* Kein Doppeltipp-Zoom: schnelles mehrfaches Tippen (z. B. auf + und -) soll nicht zoomen.
   Das Zusammenziehen mit zwei Fingern bleibt erlaubt. */
html, body, button, input, summary { touch-action: manipulation; }
.gstep-row button, .summon-btn, .game-tools button { -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; }

/* Format-Auswahl: auf dem Handy eine eigene Zeile unter der Kopfzeile, am Computer rechts neben dem Namen */
#editor-format { order: 10; flex: 1 1 100%; min-width: 0; min-height: 44px; font-weight: 600; }
@media (min-width: 720px) {
  #editor-format { order: 0; flex: 0 0 auto; width: auto; }
  .editor-bar { flex-wrap: nowrap; }
}

/* Format 6/9: neun Plätze, auf dem Handy als 3 x 3, am Computer alle neun in einer Reihe */
.slots.nine { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (min-width: 720px) {
  .slots.nine { grid-template-columns: repeat(9, minmax(0, 1fr)); }
}

/* 6/9: die Plätze im Editor als 3x3-Raster (am Computer in einer Reihe) */
#editor-slots[data-format="6-9"] { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (min-width: 720px) {
  #editor-slots[data-format="6-9"] { grid-template-columns: repeat(9, minmax(0, 1fr)); }
}
@media (min-width: 720px) { .transform-btn, .summon-btn { font-size: 0.8rem; min-height: 30px; } .summon-note { font-size: 0.8rem; min-height: 30px; } }

/* Modellanzahl in der Kopfzeile: feste Breite, damit sich beim Formatwechsel nichts verschiebt */
#editor-count { min-width: 4.6rem; text-align: center; box-sizing: border-box; }

/* Kein Textcursor und keine Textmarkierung auf der Oberfläche; nur in Eingabefeldern (und in der Kartenansicht) bleibt beides */
body { cursor: default; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
input, textarea { cursor: text; -webkit-user-select: text; user-select: text; }
select, summary, label[for] { cursor: pointer; }
#detail-body { -webkit-user-select: text; user-select: text; }

/* Import und Export: immer im Leshavult-Grün (gleicher Ton wie der Fraktionsfilter) */
#import-open, #copy {
  background: hsl(140 var(--fac-s) var(--fac-l));
  color: var(--accent-fg);
  border-color: transparent;
}

/* Import und Export immer im Leshavult-Grün (gleicher Ton wie der Fraktionsfilter) */
#import-open, #copy {
  background: hsl(140 var(--fac-s) var(--fac-l));
  color: var(--accent-fg);
  border-color: transparent;
}

/* Import und Export immer im Leshavult-Grün (gleicher Ton wie der Filter) */
#import-open, #copy {
  background: hsl(140 var(--fac-s) var(--fac-l));
  color: var(--accent-fg);
  border: 0;
}

@supports (-webkit-touch-callout: none) {
  @media (orientation: portrait) { :root { --safe-b: max(env(safe-area-inset-bottom, 0px), 34px); } }
}

/* Scrollleiste ausblenden (Scrollen geht weiter): so verschiebt sich beim Wechsel zwischen
   langen und kurzen Seiten nichts mehr seitlich */
html { scrollbar-width: none; }
html::-webkit-scrollbar, body::-webkit-scrollbar { display: none; }

/* Spielansicht: Der Hintergrund ist die Farbe von Runde 1 (Nacht). Von Runde zu Runde kriecht eine rötere Farbe
   vom oberen Rand herunter und blendet nach unten aus. In Runde 5 geht die Sonne auf. */
body.in-game { background-color: #13151f; }   /* dieselbe Farbe wie die Kopfzeile bei "Choose models" */
body.in-game[data-round="1"] { --sky: #13151f; --sky-h: 20vh; }
body.in-game[data-round="2"] { --sky: hsl(262 36% 24%); --sky-h: 38vh; }
body.in-game[data-round="3"] { --sky: hsl(318 38% 30%); --sky-h: 55vh; }
body.in-game[data-round="4"] { --sky: hsl(348 55% 34%); --sky-h: 75vh; }
body.in-game[data-round="5"] { --sky: hsl(13 68% 36%); --sky-h: 100vh; }
body.in-game[data-round]::before {
  content: "";
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--sky-h);
  background-color: var(--sky);
  -webkit-mask-image: linear-gradient(to bottom, #000 0%, rgba(0, 0, 0, 0) 100%);
  mask-image: linear-gradient(to bottom, #000 0%, rgba(0, 0, 0, 0) 100%);
  z-index: -1;
  pointer-events: none;
  transition: height 0.8s ease, background-color 0.8s ease;
}
/* Kopfzeile in der Farbe des Himmels oben, Schrift weiß */
#view-game .editor-bar[data-round] { color: #fff; }
#view-game .editor-bar[data-round] .game-title { color: #fff; }
#view-game .editor-bar[data-round] .back { color: #fff; border-color: rgba(255, 255, 255, 0.35); background: rgba(255, 255, 255, 0.08); }
#view-game .editor-bar[data-round] .badge { background: rgba(255, 255, 255, 0.2); color: #fff; }

/* Weicher Rand unter der Kopfzeile der Spielansicht: Inhalt blendet beim Scrollen sanft unter ihr aus */
#view-game .editor-bar { --bar-bg: var(--bg); }
#view-game .editor-bar[data-round] { --bar-bg: var(--sky); }
#view-game .editor-bar::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  height: 1.25rem;
  background: linear-gradient(to bottom, var(--bar-bg), transparent);
  pointer-events: none;
}

/* Kopfzeile in der Spielansicht: Rand läuft unten und an den Seiten weich aus statt mit harter Kante.
   Der Hintergrund liegt dafür auf einem eigenen Element (::before) mit Maske. */
#view-game .editor-bar { --bar-bg: var(--bg); background: none; --fade-x: linear-gradient(to right, transparent 0, #000 1.5rem, #000 calc(100% - 1.5rem), transparent 100%); }
#view-game .editor-bar[data-round] { --bar-bg: var(--sky); }
#view-game .editor-bar::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: var(--bar-bg);
  -webkit-mask-image: var(--fade-x);
  mask-image: var(--fade-x);
  transition: background-color 0.8s ease;
}
#view-game .editor-bar::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  height: 1rem;
  z-index: -1;
  background: linear-gradient(to bottom, var(--bar-bg), transparent);
  -webkit-mask-image: var(--fade-x);
  mask-image: var(--fade-x);
  pointer-events: none;
}

/* Next round / Reset: kein Aufblitzen von Rahmen oder Übergang beim Antippen */
.game-tools button { outline: none; transition: none; -webkit-tap-highlight-color: transparent; }

/* Zwei Knöpfe teilen sich die Zeile, ein einzelner füllt sie ganz */
.foe-actions { display: flex; gap: 0.5rem; margin-bottom: 0.5rem; }
.foe-actions button { flex: 1 1 0; min-width: 0; padding-inline: 0.4rem; font-size: 0.9rem; }
.foe-actions:not(:has(button:not([hidden]))) { display: none; }
#game-foe-summons-panel { margin-bottom: 0.75rem; }

/* Fußzeile mit Impressum-Link (nicht im Editor und Spiel) */
.site-foot { margin-top: 2.5rem; font-size: 0.78rem; color: var(--muted); text-align: center; }
.site-foot a { color: inherit; }
body.editing .site-foot { display: none; }