:root {
  --bg: #17130f;
  --bg2: #211b15;
  --card: #2a221a;
  --card2: #33291f;
  --line: #4a3c2d;
  --text: #efe4d0;
  --muted: #b3a38a;
  --accent: #d4a24c;
  --accent-ink: #1b140c;
  --danger: #d0604f;
  --ok: #7fb069;
  --radius: 12px;
  --shadow: 0 6px 24px rgba(0, 0, 0, .35);
  --font: -apple-system, "Segoe UI", Roboto, "Noto Sans", "Helvetica Neue", Arial, "Liberation Sans", sans-serif;
  --serif: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f4ecdd; --bg2: #ebe0cb; --card: #fffaf0; --card2: #f6eddc; --line: #d8c7a8;
    --text: #2b2117; --muted: #7a6852; --accent: #9c6a1c; --accent-ink: #fff;
    --shadow: 0 4px 18px rgba(80, 50, 10, .12); color-scheme: light;
  }
}
:root[data-theme="light"] {
  --bg: #f4ecdd; --bg2: #ebe0cb; --card: #fffaf0; --card2: #f6eddc; --line: #d8c7a8;
  --text: #2b2117; --muted: #7a6852; --accent: #9c6a1c; --accent-ink: #fff;
  --shadow: 0 4px 18px rgba(80, 50, 10, .12); color-scheme: light;
}

* { box-sizing: border-box; }
[x-cloak] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 16px; }
body { min-height: 100vh; -webkit-text-size-adjust: 100%; }
a { color: var(--accent); }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; margin: 0 0 .6em; }
h1 { font-size: 1.7rem; } h2 { font-size: 1.35rem; } h3 { font-size: 1.1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.stack > * + * { margin-top: .8rem; }
.hidden { display: none; }

/* Header */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: .75rem;
  padding: .6rem 1rem; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--line);
}
.brand { font-family: var(--serif); font-size: 1.25rem; font-weight: 700; color: var(--text); text-decoration: none; white-space: nowrap; }
.brand span { color: var(--accent); }
.brand-short { display: none; }
.topbar .crumb { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.mode-switch { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; flex-shrink: 0; }
.mode-switch button { border: 0; background: transparent; color: var(--muted); padding: .35rem .75rem; font: inherit; font-size: .85rem; cursor: pointer; }
.mode-switch button.on { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.menu { position: relative; }
.menu-pop { position: absolute; right: 0; top: 2.6rem; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); min-width: 200px; padding: .4rem; z-index: 30; }
.menu-pop a, .menu-pop button { display: block; width: 100%; text-align: left; padding: .55rem .7rem; border-radius: 8px; color: var(--text); text-decoration: none; background: none; border: 0; font: inherit; cursor: pointer; }
.menu-pop a:hover, .menu-pop button:hover { background: var(--card2); }

main { max-width: 1200px; margin: 0 auto; padding: 1.2rem 1rem 4rem; }

/* Controls */
button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-size: .95rem; padding: .55rem 1rem; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--line); background: var(--card2); color: var(--text); text-decoration: none;
  min-height: 40px;
}
button:hover, .btn:hover { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: not-allowed; }
.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.danger { color: var(--danger); }
.ghost { background: transparent; }
.icon-btn { padding: .35rem .6rem; min-height: 34px; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--bg2); border: 1px solid var(--line);
  border-radius: 10px; padding: .55rem .7rem; width: 100%; min-height: 40px;
}
textarea { min-height: 90px; resize: vertical; }
input[type="checkbox"], input[type="radio"] { width: auto; min-height: 0; accent-color: var(--accent); }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 0; }
label.field { display: block; }
label.field > span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .25rem; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .8rem; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; box-shadow: var(--shadow); }
.badge { display: inline-block; font-size: .75rem; padding: .15rem .55rem; border-radius: 999px; background: var(--card2); border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
.badge.Regelwerk { color: #e7c16f; border-color: #8a6a2a; }
.badge.Quellband { color: #8cc0e8; border-color: #3e6a8c; }
.badge.Abenteuer { color: #e88c7c; border-color: #8c4a3e; }
.badge.ok { color: var(--ok); }
.badge.err { color: var(--danger); }

.toast { position: fixed; left: 50%; bottom: 1.2rem; transform: translateX(-50%); background: var(--card); border: 1px solid var(--line); border-left: 4px solid var(--accent); padding: .7rem 1rem; border-radius: 10px; box-shadow: var(--shadow); z-index: 100; max-width: calc(100vw - 2rem); }
.toast.error { border-left-color: var(--danger); }

/* Auth */
.auth-wrap { max-width: 420px; margin: 8vh auto; }
.auth-wrap h1 { text-align: center; font-size: 2rem; }

/* Systems */
.systems { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 1rem; }
.system-card { position: relative; overflow: hidden; cursor: pointer; text-decoration: none; color: var(--text); min-height: 150px; display: flex; flex-direction: column; transition: transform .15s; }
.system-card:hover { transform: translateY(-2px); border-color: var(--accent); }
.system-card::before { content: ""; position: absolute; inset: 0 0 auto 0; height: 6px; background: var(--sys); }
.system-card h2 { margin-top: .3rem; }
.system-card .counts { margin-top: auto; display: flex; gap: .35rem; flex-wrap: wrap; }

/* Tabs */
.tabs { display: flex; gap: .3rem; overflow-x: auto; border-bottom: 1px solid var(--line); margin: 0 -1rem 1.2rem; padding: 0 1rem; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button { border: 0; border-bottom: 3px solid transparent; border-radius: 0; background: transparent; color: var(--muted); white-space: nowrap; padding: .7rem .9rem; }
.tabs button.on { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.sys-head { display: flex; align-items: center; gap: .8rem; margin-bottom: .8rem; }
.sys-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--sys); flex-shrink: 0; }

/* Library */
.filters { display: flex; gap: .4rem; flex-wrap: wrap; margin-bottom: 1rem; align-items: center; }
.chip { border-radius: 999px; padding: .3rem .8rem; min-height: 34px; font-size: .9rem; }
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.docs { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 1rem; }
.doc { display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.doc .cover { aspect-ratio: 3 / 4; background: var(--bg2) center / cover no-repeat; cursor: pointer; display: flex; align-items: center; justify-content: center; color: var(--muted); font-family: var(--serif); font-size: 2rem; }
.doc .body { padding: .6rem .7rem .7rem; display: flex; flex-direction: column; gap: .35rem; flex: 1; }
.doc .title { font-weight: 600; line-height: 1.25; word-break: break-word; }
.doc .actions { display: flex; gap: .3rem; flex-wrap: wrap; margin-top: auto; }
.doc .actions button, .doc .actions a { padding: .3rem .55rem; min-height: 32px; font-size: .85rem; }
.upload { border: 2px dashed var(--line); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.upload.drag { border-color: var(--accent); background: var(--card2); }
.progress { height: 6px; background: var(--bg2); border-radius: 3px; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); transition: width .2s; }

/* Markdown */
.md { line-height: 1.6; overflow-wrap: anywhere; }
.md h1, .md h2, .md h3 { margin-top: 1.2em; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; margin: .8em 0; }
.md th, .md td { border: 1px solid var(--line); padding: .35rem .6rem; text-align: left; vertical-align: top; }
.md th { background: var(--card2); }
.md code { background: var(--bg2); padding: .1em .35em; border-radius: 4px; }
.md blockquote { border-left: 3px solid var(--accent); margin: .5em 0; padding: .2em .9em; color: var(--muted); }
.md p:first-child { margin-top: 0; }

/* Chat */
.chat { display: flex; flex-direction: column; height: calc(100vh - 210px); min-height: 420px; }
.chat-log { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: .8rem; padding: .2rem .1rem 1rem; }
.msg { max-width: 88%; padding: .7rem .9rem; border-radius: 14px; border: 1px solid var(--line); }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.msg .plain { white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--card); }
.msg .tools { display: flex; gap: .3rem; flex-wrap: wrap; margin-bottom: .4rem; }
.chat-input { display: flex; gap: .5rem; padding-top: .6rem; border-top: 1px solid var(--line); }
.chat-input textarea { min-height: 46px; height: 46px; max-height: 160px; }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* NPC */
.npc-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 1rem; align-items: start; }
.npc h3 { margin-bottom: .1rem; }
.kv { display: grid; grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); gap: .35rem; margin: .4rem 0; }
.kv div { background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; padding: .25rem .45rem; text-align: center; }
.kv b { display: block; font-size: 1.05rem; }
.kv span { font-size: .72rem; color: var(--muted); }
.hp { display: flex; align-items: center; gap: .4rem; margin: .5rem 0; }
.hp .bar { flex: 1; height: 10px; background: var(--bg2); border-radius: 5px; overflow: hidden; border: 1px solid var(--line); }
.hp .bar > div { height: 100%; background: var(--ok); transition: width .2s; }
.hp .bar.low > div { background: var(--danger); }
.npc ul { margin: .3rem 0; padding-left: 1.2rem; }
.section-title { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: .7rem 0 .2rem; }
.status-log { font-size: .85rem; color: var(--muted); }
.status-log div::before { content: "🔎 "; }

/* Character sheet */
.sheet-section { margin-bottom: 1rem; }
.sheet-section h3 { border-bottom: 1px solid var(--line); padding-bottom: .3rem; }
.sheet-fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: .6rem; }
.sheet-fields .wide { grid-column: 1 / -1; }
.sheet-table { width: 100%; border-collapse: collapse; }
.sheet-table th { text-align: left; font-size: .8rem; color: var(--muted); font-weight: 500; padding: .2rem; }
.sheet-table td { padding: .15rem; }
.sheet-table input { min-height: 34px; padding: .3rem .45rem; }
.table-wrap { overflow-x: auto; }

/* Reader */
.reader { position: fixed; inset: 0; z-index: 50; display: flex; flex-direction: column; background: var(--rbg, #fbf6ec); color: var(--rfg, #2b2117); }
.reader[data-rtheme="dark"] { --rbg: #121212; --rfg: #ddd; }
.reader[data-rtheme="sepia"] { --rbg: #f1e3c6; --rfg: #3d2e1c; }
.reader[data-rtheme="light"] { --rbg: #ffffff; --rfg: #1a1a1a; }
.reader-bar { display: flex; align-items: center; gap: .4rem; padding: .4rem .6rem; border-bottom: 1px solid rgba(128,128,128,.3); }
.reader-bar button, .reader-bar a { background: transparent; color: inherit; border-color: rgba(128,128,128,.35); padding: .3rem .6rem; min-height: 36px; }
.reader-title { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: var(--serif); }
.reader-view { flex: 1; position: relative; min-height: 0; }
#epub-area { position: absolute; inset: 0 2.6rem; }
.reader-nav { position: absolute; top: 0; bottom: 0; width: 2.6rem; display: flex; align-items: center; justify-content: center; background: transparent; border: 0; color: inherit; opacity: .45; font-size: 1.6rem; border-radius: 0; }
.reader-nav:hover { opacity: 1; }
.reader-nav.prev { left: 0; } .reader-nav.next { right: 0; }
.reader-foot { padding: .3rem .8rem; font-size: .8rem; opacity: .7; display: flex; gap: .6rem; }
.toc { position: absolute; top: 0; left: 0; bottom: 0; width: min(340px, 90vw); background: var(--rbg); border-right: 1px solid rgba(128,128,128,.35); overflow-y: auto; z-index: 5; padding: .5rem; box-shadow: var(--shadow); }
.toc a { display: block; padding: .45rem .5rem; color: inherit; text-decoration: none; border-radius: 6px; font-size: .92rem; }
.toc a:hover { background: rgba(128,128,128,.15); }
.toc a.sub { padding-left: 1.4rem; font-size: .85rem; opacity: .85; }
.pdf-frame { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: #555; }

/* Tables (admin) */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { padding: .5rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.table th { font-size: .8rem; color: var(--muted); font-weight: 500; }
.table select { min-height: 34px; padding: .25rem .4rem; width: auto; }

.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 60; display: flex; align-items: center; justify-content: center; padding: 1rem; }
.modal { max-width: 560px; width: 100%; max-height: 90vh; overflow-y: auto; }
.empty { text-align: center; padding: 2.5rem 1rem; color: var(--muted); }

@media (max-width: 700px) {
  main { padding: 1rem 1rem 4rem; }
  h1 { font-size: 1.4rem; }
  .topbar { padding: .5rem .75rem; gap: .5rem; }
  .topbar .crumb { display: none; }
  .brand-long { display: none; }
  .brand-short { display: inline; }
  .docs { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .7rem; }
  .msg { max-width: 94%; }
  .chat { height: calc(100vh - 190px); }
  #epub-area { inset: 0 .5rem; }
  .reader-nav { display: none; }
  .hide-mobile { display: none !important; }
  .table .hide-mobile { display: none; }
  .npc-grid { grid-template-columns: 1fr; }
}

/* Kampagnen-Wechsler */
.camp-switch { width: auto; max-width: 220px; min-height: 36px; padding: .3rem .5rem; font-size: .9rem; }
details summary { padding: .3rem 0; }
@media (max-width: 700px) { .camp-switch { max-width: 130px; } }
