:root {
  --font-size: 19px;
  --serif: "Literata", ui-serif, Charter, "Iowan Old Style", Georgia, "Noto Serif", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --bg: #f8f5ef;
  --bg-elev: #fffdf9;
  --text: #2b2a27;
  --muted: #6f6b63;
  --accent: #a8622a;
  --accent-soft: rgba(184, 116, 58, 0.16);
  --accent-strong: rgba(184, 116, 58, 0.34);
  --border: rgba(43, 42, 39, 0.12);
  --error: #b3261e;
  --shadow: 0 8px 30px rgba(30, 20, 10, 0.18);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1e1f22;
    --bg-elev: #27282c;
    --text: #d8d5cf;
    --muted: #9a978f;
    --accent: #d9a76a;
    --accent-soft: rgba(217, 167, 106, 0.18);
    --accent-strong: rgba(217, 167, 106, 0.36);
    --border: rgba(216, 213, 207, 0.14);
    --error: #ff8a7a;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #1e1f22;
  --bg-elev: #27282c;
  --text: #d8d5cf;
  --muted: #9a978f;
  --accent: #d9a76a;
  --accent-soft: rgba(217, 167, 106, 0.18);
  --accent-strong: rgba(217, 167, 106, 0.36);
  --border: rgba(216, 213, 207, 0.14);
  --error: #ff8a7a;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

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

html { background: var(--bg); }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--serif);
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

/* Kopfzeile */
.bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 2px;
  min-height: 52px;
  padding: env(safe-area-inset-top) max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  font-family: var(--sans);
}
.bar:empty { display: none; }
.bar .title {
  flex: 1;
  min-width: 0;
  font-family: var(--serif);
  font-weight: 700;
  font-size: 18px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 6px;
}
.bar button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text);
  min-width: 44px;
  min-height: 44px;
  padding: 0 8px;
  border-radius: 10px;
  font: inherit;
  font-size: 16px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.bar button:hover { background: var(--accent-soft); }
.bar .a-minus { font-size: 14px; }
.bar .a-plus { font-size: 20px; }

/* Inhalt */
.view {
  max-width: 65ch;
  margin: 0 auto;
  padding: 16px max(16px, env(safe-area-inset-right)) 96px max(16px, env(safe-area-inset-left));
  font-size: var(--font-size);
  line-height: 1.6;
}
.view.library { max-width: 46rem; }

/* Passwort-Ansicht */
.gate {
  max-width: 22rem;
  margin: 12vh auto 0;
  text-align: center;
  font-family: var(--sans);
  font-size: 16px;
}
.gate h1 { font-family: var(--serif); font-size: 1.7em; margin: 0 0 4px; }
.gate p { color: var(--muted); margin: 0 0 16px; }
.gate input {
  width: 100%;
  font: inherit;
  font-size: 18px;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: inherit;
  margin: 0 0 12px;
}
.btn {
  font: inherit;
  font-family: var(--sans);
  font-size: 17px;
  padding: 12px 22px;
  border-radius: 10px;
  border: 0;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  min-height: 44px;
}
.btn:disabled { opacity: 0.6; cursor: wait; }
.error { color: var(--error); margin: 10px 0 0; min-height: 1.4em; }

/* Meldungen */
.msg {
  font-family: var(--sans);
  font-size: 16px;
  color: var(--muted);
  text-align: center;
  margin: 10vh auto;
  max-width: 30rem;
}
.msg a { color: var(--accent); }
.link-muted {
  background: none;
  border: 0;
  padding: 8px;
  color: var(--muted);
  text-decoration: underline;
  font: inherit;
  font-family: var(--sans);
  font-size: 14px;
  cursor: pointer;
}
.footer { margin-top: 32px; text-align: center; }

/* Bibliothek */
.library h1 { font-size: 1.5em; margin: 8px 0 18px; }
.card {
  display: block;
  text-decoration: none;
  color: inherit;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px 18px;
  margin: 0 0 14px;
  -webkit-tap-highlight-color: transparent;
}
.card:hover { border-color: var(--accent); }
.card h2 { margin: 0 0 6px; font-size: 1.15em; line-height: 1.3; }
.card p { margin: 8px 0 0; font-size: 0.95em; color: var(--muted); }
.meta {
  font-family: var(--sans);
  font-size: 0.78em;
  color: var(--muted);
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  align-items: center;
}
.meta a { color: var(--accent); text-decoration: none; }
.meta a:hover { text-decoration: underline; }
.badge {
  display: inline-block;
  font-family: var(--sans);
  font-size: 0.72em;
  font-weight: 600;
  letter-spacing: 0.02em;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  vertical-align: middle;
}

/* Leseansicht */
.article h1 { font-size: 1.6em; line-height: 1.25; margin: 8px 0 8px; }
.article .byline { margin-bottom: 1.6em; }
.article h2 { font-size: 1.25em; line-height: 1.3; margin: 1.6em 0 0.6em; }
.article p { margin: 0 0 1em; }
.article blockquote {
  margin: 1em 0;
  padding: 0 0 0 1em;
  border-left: 3px solid var(--accent-strong);
  color: var(--muted);
  font-style: italic;
}
.article blockquote p { margin: 0; }

.w {
  cursor: pointer;
  border-radius: 4px;
  padding: 0 1px;
  margin: 0 -1px;
  transition: background-color 0.12s;
  -webkit-tap-highlight-color: transparent;
}
.w.phrase {
  text-decoration: underline dotted var(--accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}
.w.active { background: var(--accent-strong); }
@media (hover: hover) and (pointer: fine) {
  .w:hover { background: var(--accent-soft); }
}

/* Popup */
.popup {
  position: fixed;
  z-index: 20;
  background: var(--bg-elev);
  color: var(--text);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.45;
}
.pop-head { display: flex; align-items: baseline; flex-wrap: wrap; gap: 4px 8px; margin: 0 28px 6px 0; }
.pop-word { font-family: var(--serif); font-weight: 700; font-size: 1.15em; }
.pop-base { color: var(--muted); }
.pop-de { font-size: 1.1em; margin: 2px 0 4px; }
.pop-de.muted { color: var(--muted); font-style: italic; }
.pop-note { color: var(--muted); font-size: 0.93em; }
.pop-close {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
}

@media (max-width: 699px) {
  .popup {
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 18px 18px 0 0;
    border-bottom: 0;
    padding: 16px 18px calc(16px + env(safe-area-inset-bottom));
    max-height: 45vh;
    overflow-y: auto;
  }
}
@media (min-width: 700px) {
  .popup {
    max-width: 360px;
    border-radius: 12px;
    padding: 12px 14px;
  }
  .pop-head { margin-right: 0; }
  .pop-close { display: none; }
}

/* Hinzufügen: Knopf, Blatt, Formular */
.bar .add { font-size: 26px; line-height: 1; padding-bottom: 4px; }

.sheet {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet-inner {
  position: relative;
  width: 100%;
  max-width: 34rem;
  max-height: 92vh;
  overflow-y: auto;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-bottom: 0;
  border-radius: 18px 18px 0 0;
  box-shadow: var(--shadow);
  padding: 18px max(18px, env(safe-area-inset-right)) calc(18px + env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
  font-family: var(--sans);
  font-size: 16px;
}
@media (min-width: 700px) {
  .sheet { align-items: center; }
  .sheet-inner { border-radius: 16px; border-bottom: 1px solid var(--border); max-height: 86vh; }
}
.sheet-kopf { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.sheet-kopf h2 { font-family: var(--serif); font-size: 1.3em; margin: 0; }
.sheet .pop-close { position: static; display: inline-flex; align-items: center; justify-content: center; }

.tabs { display: flex; gap: 6px; margin-bottom: 12px; }
.tab {
  flex: 1;
  font: inherit;
  font-size: 16px;
  padding: 10px;
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.tab.aktiv { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); font-weight: 600; }

.addform input,
.addform textarea {
  width: 100%;
  font: inherit;
  font-size: 16px;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: inherit;
  margin: 0 0 10px;
}
.addform textarea { resize: vertical; min-height: 8em; font-family: var(--serif); line-height: 1.5; }
.addform .btn { width: 100%; margin-top: 4px; }
.addform .klein { font-size: 0.84em; color: var(--muted); margin: 10px 0 0; line-height: 1.45; }
.addform .klein a { color: var(--accent); }
.tokenbox {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px 4px;
  margin: 0 0 12px;
  background: var(--bg);
}
.tokenbox > p:first-child { margin: 0 0 10px; font-size: 0.92em; }

/* Warteschlange in der Bibliothek */
.abschnitt {
  font-family: var(--sans);
  font-size: 0.8em;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 28px 0 12px;
}
.card.wartend { border-style: dashed; }
.badge.wartet { background: transparent; border: 1px solid var(--accent); }
.card .url { font-family: var(--sans); font-size: 0.8em; word-break: break-all; }
.article .hinweis {
  font-family: var(--sans);
  font-size: 0.82em;
  color: var(--muted);
  background: var(--accent-soft);
  border-radius: 10px;
  padding: 10px 14px;
  margin: 0 0 1.6em;
  line-height: 1.5;
}
