:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --card: #ffffff;
  --ink: #16181d;
  --muted: #646c7a;
  --line: #e2e5ea;
  --brand: #2563eb;
  --brand-ink: #ffffff;
  --ok: #157a4a;
  --warn: #9a5b00;
  --bad: #b3261e;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

a { color: var(--brand); }

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
}

.topbar h1 { font-size: 17px; margin: 0; min-width: 0; }
.topbar nav { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; min-width: 0; max-width: 100%; }
/* Lange Adressen dürfen die Kopfleiste auf 360 px nicht sprengen. */
.topbar .who { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; min-width: 0; max-width: 100%; }

.wrap { max-width: 1120px; margin: 0 auto; padding: 20px; }
.wrap.narrow { max-width: 460px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 18px;
}

.card h2 { margin: 0 0 12px; font-size: 16px; }
.card h3 { margin: 18px 0 8px; font-size: 14px; }

/* Grid- und Flex-Kinder schrumpfen ohne `min-width: 0` nicht unter ihren Inhalt.
   Genau daran lag der Überlauf der Verbrauchstabellen. */
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
.grid2 > * { min-width: 0; }

/* Zwei breite Tabellen nur nebeneinander, wenn der Container wirklich Platz hat.
   auto-fit/minmax misst den Container, nicht das Fenster. */
.grid-wide { display: grid; grid-template-columns: repeat(auto-fit, minmax(520px, 1fr)); gap: 18px; align-items: start; }
.grid-wide > * { min-width: 0; }

.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1 1 160px; min-width: 0; }
.row.tight > * { flex: 0 0 auto; }
/* Breitenklassen statt style-Attributen: die strikte CSP erlaubt keine Inline-Stile. */
.row > .grow-3 { flex: 3 1 220px; }
.row > .grow-0 { flex: 1 1 90px; }
.row > .grow-none { flex: 0 0 auto; }
.row > .grow-full { flex: 1 1 100%; }
/* Labels auf gleicher Höhe, auch wenn ein Block darunter noch einen Hilfetext hat. */
.row.row-top { align-items: flex-start; }
textarea.short { min-height: 70px; }

/* Name (flexibel) und Emoji (kompakt) in einer Zeile.
   Der Emoji-Block darf NICHT unter seine Inhaltsbreite schrumpfen — sonst
   ragt der Knopf aus der Karte. Statt zu schrumpfen rutscht er als Ganzes in
   die nächste Zeile, sobald der Container zu schmal wird (flex-wrap). */
.row > .field-grow { flex: 1 1 200px; min-width: 0; }
.row > .field-emoji { flex: 0 0 auto; max-width: 100%; min-width: 0; }
.field-emoji > .emoji-field { display: flex; gap: 6px; align-items: stretch; min-width: 0; }
.field-emoji input[type="text"] { flex: 0 0 64px; width: 64px; min-width: 0; text-align: center; font-size: 20px; }
.field-emoji button { flex: 0 1 auto; min-width: 0; white-space: nowrap; }
/* Der Hilfetext soll die Breite des Blocks NICHT bestimmen. Ein Flex-Element
   ohne feste Breite ist so breit wie sein breitester Inhalt — der ganze Satz
   hätte den Emoji-Block sonst unnötig aufgebläht. «width: 0» nimmt ihn aus
   dieser Rechnung heraus, «min-width: 100%» gibt ihm beim Zeichnen die volle
   Blockbreite zurück. Greift das nicht, wird der Block nur etwas breiter —
   aus der Karte ragen kann er wegen max-width und flex-wrap trotzdem nicht. */
.field-emoji > .muted { margin: 4px 0 0; width: 0; min-width: 100%; }

label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; }

input[type="text"], input[type="email"], input[type="number"], input[type="search"],
input[type="color"], select, textarea {
  width: 100%;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: inherit;
}

/* Ohne appearance:none ignoriert die Suchfeld-Darstellung Rahmen und Innenabstand. */
input[type="search"] { -webkit-appearance: none; appearance: none; }
input[type="color"] { padding: 3px; height: 38px; }
input[type="range"].slider { width: 100%; accent-color: var(--brand); }
textarea { min-height: 110px; resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }

button {
  padding: 9px 14px;
  border-radius: 8px;
  border: 1px solid var(--brand);
  background: var(--brand);
  color: var(--brand-ink);
  font: inherit;
  cursor: pointer;
}

button.secondary { background: #fff; color: var(--ink); border-color: var(--line); }
button.danger { background: #fff; color: var(--bad); border-color: #e8bdb9; }
button:disabled { opacity: .55; cursor: not-allowed; }

.muted { color: var(--muted); font-size: 13px; }
.error { color: var(--bad); }
.ok { color: var(--ok); }

.notice { padding: 10px 12px; border-radius: 8px; border: 1px solid var(--line); background: #fbfbfd; margin-bottom: 12px; }
.notice.error { border-color: #e8bdb9; background: #fdf3f2; }
.notice.ok { border-color: #bfe3cf; background: #f2fbf6; }
.notice.warn { border-color: #eed9ae; background: #fdf8ee; color: var(--warn); }

table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tfoot tr.total td { font-weight: 600; border-top: 2px solid var(--line); border-bottom: none; }

/* Gemeinsamer Wrapper für JEDE Tabelle: nichts ragt aus seiner Karte,
   im Notfall wird innerhalb des Wrappers gescrollt. */
.table-wrap { overflow-x: auto; max-width: 100%; min-width: 0; }
/* Die erste Spalte trägt Namen: umbrechen ja, überlaufen nein. */
table td:first-child, table th:first-child { overflow-wrap: anywhere; }
.wrap-id { overflow-wrap: anywhere; }
/* Lange Texte brechen notfalls mitten im Wort, statt die Karte zu sprengen. */
.card { overflow-wrap: break-word; }

.bar { height: 10px; border-radius: 999px; background: var(--line); overflow: hidden; margin: 6px 0; }
.bar > span { display: block; height: 100%; background: var(--brand); }
.bar.warn > span { background: #d98a00; }
.bar.full > span { background: var(--bad); }

ul.list { list-style: none; margin: 0; padding: 0; }
ul.list li {
  display: flex; gap: 10px; align-items: center; justify-content: space-between;
  flex-wrap: wrap;
  padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 13px;
}
/* Dateiname kürzt mit Ellipse, die Grösse steht daneben und wird nie gekürzt,
   die Aktionen bleiben rechts; schmal rutschen sie darunter. */
ul.list li > .name { flex: 1 1 200px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
ul.list li > .size { flex: 0 0 auto; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
ul.list li > .actions { flex: 0 0 auto; display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
ul.list li > .actions a.button-link,
ul.list li > .actions button { font-size: 12px; padding: 5px 9px; }

.botnav { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; min-width: 0; }
.botnav button { background: #fff; color: var(--ink); border-color: var(--line); max-width: 100%; overflow-wrap: anywhere; text-align: left; }
.botnav button.active { background: var(--brand); color: #fff; border-color: var(--brand); }

code, pre.code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  background: #f0f2f6;
  border-radius: 6px;
  padding: 2px 5px;
}
pre.code { display: block; padding: 10px; overflow-x: auto; white-space: pre-wrap; word-break: break-all; }

.dropzone { border: 2px dashed var(--line); border-radius: var(--radius); padding: 18px; text-align: center; color: var(--muted); font-size: 13px; }
.dropzone.hover { border-color: var(--brand); color: var(--brand); }

.tabs { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.tabs button { background: #fff; color: var(--ink); border-color: var(--line); }
.tabs button.active { background: var(--ink); color: #fff; border-color: var(--ink); }

/* Regler-Blöcke im Editor: Label, Wert, Regler, Erklärung */
.slider-block { padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: #fbfbfd; }
.slider-block label { color: var(--ink); font-size: 13px; margin-bottom: 6px; }
.slider-block p { margin: 6px 0 0; }

/* Kompakte Budgetanzeige in der Kopfleiste, springt zum Bereich unten */
.budget-mini {
  display: flex; align-items: center; gap: 8px;
  background: #fff; color: var(--ink); border-color: var(--line);
  padding: 5px 10px; font-size: 13px; white-space: nowrap;
}
.budget-mini:hover { border-color: var(--brand); color: var(--brand); }
.bar.mini { width: 70px; height: 6px; margin: 0; flex: 0 0 auto; }

/* «Mehr laden» am Ende einer langen Tabelle */
tr.more-row td.more { border-bottom: none; padding-top: 10px; }
tbody tr[tabindex]:focus { outline: 2px solid var(--brand); outline-offset: -2px; }

/* Vorlagen-Auswahl */
dialog.dialog {
  width: min(960px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  background: var(--card);
  color: var(--ink);
}
dialog.dialog::backdrop { background: rgba(15, 23, 42, .45); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.dialog-head h2 { margin: 0; font-size: 16px; }

/* Emoji-Auswahl: derselbe Dialog-Rahmen, nur schmaler und scrollbar. */
dialog.emoji-dialog { width: min(560px, calc(100vw - 32px)); overflow-y: auto; }
.emoji-group { margin-top: 12px; }
/* Beim Scrollen im Raster muss sichtbar bleiben, in welcher Kategorie man ist. */
.emoji-group h3 {
  position: sticky; top: 0; z-index: 1;
  margin: 0 0 6px; padding: 4px 0; font-size: 13px; color: var(--muted); font-weight: 600;
  background: var(--card);
}
/* Trefferflächen mindestens 40x40 px, damit sie auch auf dem Handy passen. */
.emoji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 4px; }
.emoji-cell {
  min-width: 44px; min-height: 44px; padding: 0;
  font-size: 22px; line-height: 1;
  background: transparent; color: inherit;
  border: 1px solid transparent; border-radius: 10px; cursor: pointer;
}
.emoji-cell:hover { background: #eef2ff; border-color: var(--line); }
.emoji-cell:focus-visible { outline: 2px solid var(--brand); outline-offset: 1px; }
#emojiResults { max-height: min(50vh, 420px); overflow-y: auto; margin-top: 6px; }

.template-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; margin-top: 14px; }
.template-card {
  border: 1px solid var(--line); border-radius: var(--radius); padding: 14px;
  display: flex; flex-direction: column; gap: 8px; background: #fff;
}
.template-card.blank { border-style: dashed; }
.template-card h3 { margin: 0; font-size: 15px; display: flex; align-items: center; gap: 8px; min-width: 0; overflow-wrap: anywhere; }
.template-card .tpl-emoji { font-size: 22px; line-height: 1; }
.template-card p { margin: 0; font-size: 13px; color: var(--muted); }
.template-card .tpl-facts { display: flex; flex-wrap: wrap; gap: 6px; }
.template-card .tpl-facts span {
  font-size: 12px; color: var(--muted); background: #f0f2f6; border-radius: 999px; padding: 2px 8px;
  white-space: nowrap;
}
/* Vorschau der Starter-Fragen auf der Vorlagen-Karte */
.template-card .tpl-starters { display: flex; flex-wrap: wrap; gap: 5px; }
/* In der Karten-Vorschau wird ein langer Starter nach einer Zeile gekürzt,
   damit alle Karten gleich hoch bleiben. Der volle Text steht im `title`. */
.template-card .tpl-starters span {
  font-size: 12px; border: 1px solid var(--tpl-primary, var(--brand)); color: var(--ink);
  border-radius: 999px; padding: 2px 9px; max-width: 100%;
  display: block; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.template-card .tpl-colors { display: flex; gap: 6px; align-items: center; }
.template-card .tpl-swatch { width: 22px; height: 22px; border-radius: 6px; border: 1px solid var(--line); }
.template-card .tpl-swatch.primary { background: var(--tpl-primary, var(--brand)); }
.template-card .tpl-swatch.accent { background: var(--tpl-accent, var(--ink)); }
.template-card details { font-size: 13px; }
.template-card details summary { cursor: pointer; color: var(--brand); }
.template-card details p { margin-top: 6px; }
.template-card .tpl-use { margin-top: auto; }

/* Wissensliste: Ansehen, Herunterladen, Löschen mit Rückfrage ohne Browser-Dialog */
a.button-link {
  display: inline-block; padding: 9px 14px; border-radius: 8px;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  font: inherit; text-decoration: none; cursor: pointer;
}
a.button-link:hover { border-color: var(--brand); color: var(--brand); }
.confirm { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; font-size: 12px; color: var(--bad); }
.file-view {
  flex: 1 1 100%;
  max-height: 260px; overflow: auto;
  margin: 6px 0 2px; padding: 10px;
  background: #f7f8fa; border: 1px solid var(--line); border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px;
  white-space: pre-wrap; overflow-wrap: anywhere;
}

/* Starter-Fragen im Editor */
.starter-row { border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 8px; background: #fbfbfd; }
.starter-row .row { align-items: flex-end; }
.starter-count { font-size: 12px; color: var(--muted); }
/* Mitwachsendes Feld: keine Mindesthöhe von 110px, keine Monospace-Schrift,
   kein Ziehgriff — die Höhe steuert das `rows`-Attribut. */
textarea.starter-input {
  min-height: 0; resize: none; overflow: hidden;
  font-family: inherit; font-size: inherit; line-height: 1.4;
}
.starter-row .order { display: flex; gap: 4px; }
.starter-row .order button { padding: 6px 10px; }

/* Vorschau des schwebenden Knopfes */
.launcher-preview { display: flex; align-items: center; gap: 12px; margin: 12px 0; }
.launcher-preview > div { flex: 0 0 auto; }

@media (max-width: 860px) {
  .grid2 { grid-template-columns: 1fr; }
}
