:root {
  --red: #c8102e;
  --red-dark: #9e0c24;
  --red-soft: #fdecef;
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1c1e21;
  --muted: #65676b;
  --line: #e2e4e8;
  --ok: #1a7f37;
  --ok-soft: #e6f4ea;
  --warn: #9a6700;
  --warn-soft: #fff4d6;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.04);
  --nav-h: 64px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --red: #ef4b62;
    --red-dark: #ff6b80;
    --red-soft: #3a1a20;
    --bg: #111214;
    --card: #1c1d20;
    --text: #e8e9eb;
    --muted: #a3a6ab;
    --line: #2e3035;
    --ok: #4ac26b;
    --ok-soft: #16301e;
    --warn: #e3b341;
    --warn-soft: #33290f;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--red); }
button, input, select, textarea { font: inherit; color: inherit; }

/* Layout */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--red); color: #fff;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px;
  display: flex; align-items: center; gap: 12px;
}
.topbar h1 { font-size: 1.1rem; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .back { background: none; border: 0; color: #fff; font-size: 1.4rem; padding: 0 4px; cursor: pointer; }
main { max-width: 860px; margin: 0 auto; padding: 16px 16px calc(var(--nav-h) + env(safe-area-inset-bottom) + 24px); }

.bottomnav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  background: var(--card); border-top: 1px solid var(--line);
  display: flex; justify-content: space-around;
  padding-bottom: env(safe-area-inset-bottom);
}
.bottomnav a {
  flex: 1; text-align: center; text-decoration: none; color: var(--muted);
  font-size: .72rem; padding: 8px 0 6px; display: flex; flex-direction: column; align-items: center; gap: 2px;
  min-height: var(--nav-h); justify-content: center;
}
.bottomnav a svg { width: 24px; height: 24px; }
.bottomnav a.active { color: var(--red); font-weight: 600; }
.bottomnav .badge { position: absolute; margin-left: 18px; margin-top: -4px; background: var(--red); color: #fff; border-radius: 9px; font-size: .65rem; padding: 0 5px; }

@media (min-width: 900px) {
  .bottomnav { top: 0; bottom: 0; right: auto; width: 96px; flex-direction: column; justify-content: flex-start; border-top: 0; border-right: 1px solid var(--line); padding-top: 80px; }
  .topbar, main { margin-left: 96px; }
  main { padding-bottom: 32px; }
}

/* Bausteine */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 12px; border: 1px solid var(--line); }
.card h2, .card h3 { margin: 0 0 8px; }
h2.section { font-size: 1rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 24px 4px 8px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 10px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 0; border-radius: 10px; padding: 10px 16px; cursor: pointer;
  background: var(--red); color: #fff; font-weight: 600; text-decoration: none; min-height: 44px;
}
.btn:hover { background: var(--red-dark); }
.btn.secondary { background: var(--card); color: var(--text); border: 1px solid var(--line); }
.btn.ghost { background: transparent; color: var(--red); padding: 8px 10px; min-height: 36px; }
.btn.danger { background: transparent; color: var(--red); border: 1px solid var(--red); }
.btn.small { padding: 6px 10px; min-height: 34px; font-size: .85rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }

label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 4px; }
input[type=text], input[type=password], input[type=datetime-local], input[type=url], select, textarea {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--card);
}
textarea { min-height: 90px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--red); outline-offset: 0; border-color: transparent; }
.field { margin-bottom: 12px; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 400; }

.chip { display: inline-block; font-size: .72rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--red-soft); color: var(--red); white-space: nowrap; }
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.grey { background: var(--bg); color: var(--muted); border: 1px solid var(--line); }

.list { list-style: none; margin: 0; padding: 0; }
.list > li { border-top: 1px solid var(--line); }
.list > li:first-child { border-top: 0; }
.list a.item, .list .item { display: flex; gap: 12px; align-items: center; padding: 12px 4px; text-decoration: none; color: inherit; }
.code { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--red); min-width: 42px; }
.progress { height: 8px; background: var(--bg); border-radius: 4px; overflow: hidden; border: 1px solid var(--line); }
.progress > span { display: block; height: 100%; background: var(--ok); }

.tabs { display: flex; gap: 6px; margin-bottom: 12px; overflow-x: auto; }
.tabs button { border: 1px solid var(--line); background: var(--card); padding: 8px 14px; border-radius: 999px; cursor: pointer; white-space: nowrap; }
.tabs button.active { background: var(--red); border-color: var(--red); color: #fff; }

/* Lerninhalt */
.lesson h2 { font-size: 1.1rem; margin: 20px 0 6px; }
.lesson ul, .lesson ol { padding-left: 22px; margin: 6px 0; }
.lesson li { margin: 3px 0; }
.lesson blockquote { margin: 12px 0; padding: 10px 14px; background: var(--warn-soft); border-left: 4px solid var(--warn); border-radius: 8px; }
.goals { background: var(--red-soft); border-radius: 10px; padding: 10px 14px; }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.gallery figure { margin: 0; background: var(--bg); border-radius: 10px; overflow: hidden; position: relative; }
.gallery img, .gallery video { width: 100%; aspect-ratio: 4/3; object-fit: cover; display: block; cursor: zoom-in; }
.gallery img[src$=".svg"] { object-fit: contain; background: #fff; }
.lightbox img[src$=".svg"] { background: #fff; width: min(100%, 1000px); }
.gallery figcaption { font-size: .78rem; padding: 4px 8px; }
.media-full video { width: 100%; border-radius: 10px; background: #000; }
.icon-del { position: absolute; top: 4px; right: 4px; background: rgba(0,0,0,.6); color: #fff; border: 0; border-radius: 50%; width: 28px; height: 28px; cursor: pointer; }

.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.9); z-index: 50; display: flex; align-items: center; justify-content: center; padding: 16px; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 8px; }

/* Kalender */
.cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; text-align: center; }
.cal .dow { font-size: .75rem; color: var(--muted); padding: 4px 0; }
.cal button { aspect-ratio: 1; border: 0; background: transparent; border-radius: 10px; cursor: pointer; position: relative; font-variant-numeric: tabular-nums; }
.cal button.other { color: var(--muted); opacity: .5; }
.cal button.today { font-weight: 800; color: var(--red); }
.cal button.sel { background: var(--red); color: #fff; }
.cal button.has::after { content: ''; position: absolute; bottom: 6px; left: 50%; transform: translateX(-50%); width: 6px; height: 6px; border-radius: 50%; background: var(--red); }
.cal button.sel.has::after { background: #fff; }
.event { display: flex; gap: 12px; }
.event .date { min-width: 52px; text-align: center; background: var(--red-soft); color: var(--red); border-radius: 10px; padding: 6px 4px; line-height: 1.1; }
.event .date b { display: block; font-size: 1.3rem; }

/* Nachrichten */
.chat { display: flex; flex-direction: column-reverse; gap: 8px; }
.msg { max-width: 85%; padding: 10px 12px; border-radius: 14px; background: var(--card); border: 1px solid var(--line); align-self: flex-start; white-space: pre-wrap; word-wrap: break-word; }
.msg.mine { align-self: flex-end; background: var(--red-soft); border-color: transparent; }
.msg.announce { max-width: 100%; align-self: stretch; border: 2px solid var(--red); }
.msg .meta { font-size: .75rem; color: var(--muted); margin-bottom: 2px; display: flex; gap: 6px; align-items: center; }
.composer { position: sticky; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); background: var(--bg); padding: 8px 0; }
@media (min-width: 900px) { .composer { bottom: 0; } }

/* Quiz */
.q { margin-bottom: 14px; }
.q .opt { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; margin-top: 6px; cursor: pointer; background: var(--card); }
.q .opt input { margin-top: 4px; accent-color: var(--red); }
.q .opt.right { border-color: var(--ok); background: var(--ok-soft); }
.q .opt.wrong { border-color: var(--red); background: var(--red-soft); }
.score { font-size: 2.4rem; font-weight: 800; text-align: center; }

table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); }
.table-wrap { overflow-x: auto; }

/* Login */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.login .card { width: 100%; max-width: 380px; }
.login .logo { width: 72px; height: 72px; display: block; margin: 0 auto 12px; }

#toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--nav-h) + 20px + env(safe-area-inset-bottom)); background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 60; max-width: calc(100% - 32px); }
#toast.show { opacity: 1; }

.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 40; display: flex; align-items: flex-end; justify-content: center; }
.modal { background: var(--card); width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto; border-radius: 18px 18px 0 0; padding: 20px 16px calc(20px + env(safe-area-inset-bottom)); }
@media (min-width: 600px) { .modal-bg { align-items: center; } .modal { border-radius: 18px; } }
.empty { text-align: center; color: var(--muted); padding: 24px 8px; }

/* Das hidden-Attribut gilt auch für Elemente mit eigener display-Regel (z. B. .btn) */
[hidden] { display: none !important; }

/* Organisations-Übersicht */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 8px; }
.stats .card { margin: 0; padding: 10px 6px; text-align: center; }
.stats .stat { display: block; padding: 10px 6px; text-align: center; border: 1px solid var(--line); border-radius: 12px; color: inherit; text-decoration: none; }
.stats b { display: block; font-size: 1.4rem; line-height: 1.2; }
.stats span { font-size: .75rem; color: var(--muted); }
details.org { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 8px; box-shadow: var(--shadow); }
details.org details.org { box-shadow: none; margin: 6px 0 0; border-left: 3px solid var(--red); border-radius: 10px; }
details.org > summary { display: flex; gap: 10px; align-items: center; padding: 10px 12px; cursor: pointer; list-style: none; }
details.org > summary::-webkit-details-marker { display: none; }
details.org > summary::after { content: '›'; color: var(--muted); font-size: 1.3rem; transition: transform .15s; flex: none; }
details.org[open] > summary::after { transform: rotate(90deg); }
.org-icon { font-size: 1.4rem; flex: none; width: 30px; text-align: center; }
.org-body { padding: 0 10px 10px 12px; }
.org-people { background: var(--bg); border-radius: 10px; padding: 0 8px; margin-bottom: 4px; }
.org-person { cursor: pointer; padding: 8px 2px !important; }
.avatar { flex: none; width: 32px; height: 32px; border-radius: 50%; background: var(--red-soft); color: var(--red); display: inline-flex; align-items: center; justify-content: center; font-weight: 700; }

/* Smartphone */
html, body { overflow-x: hidden; }
.card, .msg, .item { overflow-wrap: anywhere; }
input[type=search], input[type=email], input[type=number], input[type=date], input[type=time] {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--card);
}
@media (max-width: 600px) {
  /* iOS zoomt bei Eingabefeldern unter 16px automatisch hinein */
  input, select, textarea { font-size: 16px; }
  main { padding-left: 12px; padding-right: 12px; }
  .card { padding: 14px 12px; }
  /* Zeilen mit Knopfleiste rechts: Knöpfe unter den Inhalt umbrechen */
  .list .item:has(> .row) { flex-wrap: wrap; }
  .list .item > .row { width: 100%; justify-content: flex-start !important; }
  .modal { max-height: 88dvh; }
  .tabs { scrollbar-width: none; }
  h2.section { margin-top: 16px; }
}

/* Nachrichten: Unterhaltungsliste + Chat */
.msg-layout { display: grid; gap: 12px; }
.msg-layout .card { margin: 0; }
.conv-list { padding: 10px; }
.conv-sec { font-size: .75rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 14px 8px 4px; }
.conv-row { display: flex; gap: 10px; align-items: center; padding: 9px 8px; border-radius: 12px; text-decoration: none; color: inherit; }
.conv-row:hover { background: var(--bg); }
.conv-row.active { background: var(--red-soft); }
.conv-row b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-prev { display: block; font-size: .82rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-av { flex: none; width: 40px; height: 40px; border-radius: 50%; background: var(--bg); display: inline-flex; align-items: center; justify-content: center; font-size: 1.25rem; border: 1px solid var(--line); }
.conv-av.person { background: var(--red-soft); color: var(--red); font-size: .9rem; font-weight: 700; border: 0; }
.conv-side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex: none; }
.conv-badge { background: var(--red); color: #fff; border-radius: 999px; font-size: .7rem; font-weight: 700; min-width: 20px; padding: 1px 6px; text-align: center; }
.conv { display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.conv-head { display: flex; gap: 10px; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.chat-thread { display: flex; flex-direction: column; gap: 8px; padding: 14px; overflow-y: auto; min-height: 240px; }
.conv .composer { position: static; padding: 10px 12px; border-top: 1px solid var(--line); background: var(--card); }
.conv .composer textarea { min-height: 44px; max-height: 160px; resize: none; }
.toggle { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 6px 12px; font-size: .85rem; cursor: pointer; }
.toggle[aria-pressed="true"] { background: var(--red); border-color: var(--red); color: #fff; }
@media (max-width: 899px) {
  .msg-layout.has-conv .conv-list { display: none; }
  .msg-layout:not(.has-conv) .conv { display: none; }
  .chat-thread { max-height: calc(100dvh - 330px); }
}
@media (min-width: 900px) {
  .msg-layout { grid-template-columns: 320px 1fr; height: calc(100dvh - 120px); }
  .conv-list { overflow-y: auto; }
  .chat-thread { flex: 1; }
}

/* Auswahl-Knöpfe (z. B. Gruppe, Dauer, Vorlage) */
.pick { display: flex; gap: 6px; flex-wrap: wrap; }
.pick button { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 7px 13px; cursor: pointer; font-size: .9rem; }
.pick button.on { background: var(--red); border-color: var(--red); color: #fff; }
.pick button:hover:not(.on) { border-color: var(--red); }

/* Browser-Ansicht auf großen Bildschirmen */
@media (min-width: 900px) {
  :root { --side: 220px; }
  .bottomnav { width: var(--side); padding: 18px 12px; gap: 4px; align-items: stretch; box-shadow: 1px 0 0 var(--line); }
  .bottomnav::before { content: '🚒  Gemeindeausbildung'; font-weight: 800; font-size: .95rem; padding: 6px 12px 18px; color: var(--red); white-space: nowrap; }
  .bottomnav a { flex: none; flex-direction: row; justify-content: flex-start; gap: 12px; padding: 10px 14px; min-height: 0; font-size: .95rem; border-radius: 12px; }
  .bottomnav a:hover { background: var(--bg); color: var(--text); }
  .bottomnav a.active { background: var(--red-soft); }
  .bottomnav .badge { position: static; margin: 0 0 0 auto; }
  .topbar, main { margin-left: var(--side); }
  .topbar { padding: 16px 32px; }
  .topbar h1 { font-size: 1.3rem; }
  main { max-width: 1100px; padding: 24px 32px 40px; }
  .card { transition: box-shadow .15s; }
  .list a.item:hover, .list .item[data-prog]:hover { background: var(--bg); border-radius: 10px; }
  .modal { max-width: 680px; padding: 24px; }
  .gallery { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
  #toast { bottom: 24px; }
  /* Startseite zweispaltig */
  .dash { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 0 20px; align-items: start; }
  .dash > div > h2.section:first-child { margin-top: 0; }
}
@media (min-width: 900px) { .cal button { aspect-ratio: auto; height: 54px; } }
@media (min-width: 1400px) { main { max-width: 1240px; } }
.chat-thread .msg { white-space: normal; }
.chat-thread .msg-body { white-space: pre-wrap; }
.chat-thread .msg .meta { flex-wrap: wrap; }
.chat-thread .msg .meta > span { white-space: nowrap; }

/* Adressbuch */
.ab-list { padding: 4px 12px; }
.ab-person { display: flex; gap: 12px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
.ab-person:first-child { border-top: 0; }
.ab-fn { font-size: .85rem; }
.ab-lead { color: var(--red); font-weight: 600; }
@media (max-width: 600px) { .hide-sm { display: none; } }
@media (min-width: 900px) { .ab-list { display: grid; grid-template-columns: 1fr 1fr; column-gap: 24px; } .ab-person:nth-child(2) { border-top: 0; } }
