:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efeee9;
  --border: #d9d6ce;
  --text: #23211d;
  --muted: #6d6a62;
  --accent: #2f6f5e;
  --accent-soft: #dcece7;
  --danger: #a33a2c;
  --radius: 10px;
  --control: 2.3rem;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 4px 16px rgba(0, 0, 0, 0.05);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17191a;
    --surface: #202325;
    --surface-2: #282c2e;
    --border: #3a3f42;
    --text: #e7e5e0;
    --muted: #a0a09a;
    --accent: #6fbfa5;
    --accent-soft: #24382f;
    --danger: #e08373;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-text-size-adjust: 100%;
}

.app {
  max-width: 62rem;
  margin: 0 auto;
  padding: 0 1rem 1rem;
}

/* ------------------------------------------------------------- Bedienung */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  gap: 0.5rem;
  align-items: center;
  padding: 0.75rem 0;
  background: var(--bg);
}

.spacer {
  flex: 1;
}

.grow {
  flex: 1;
}

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

input[type="text"],
input[type="search"],
input[type="password"],
textarea,
select {
  width: 100%;
  padding: 0.4rem 0.65rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  outline-offset: 2px;
}

input[type="text"],
input[type="search"],
input[type="password"],
select,
.btn {
  min-height: var(--control);
}

input:focus-visible,
textarea:focus-visible,
select:focus-visible,
button:focus-visible,
a:focus-visible {
  outline: 2px solid var(--accent);
}

textarea {
  resize: vertical;
  min-height: 8rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.95rem;
  line-height: 1.5;
}

.q {
  font-size: 1.05rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.3rem;
  padding: 0.4rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover {
  background: var(--surface-2);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

@media (prefers-color-scheme: dark) {
  .btn.primary {
    color: #10201b;
  }
}

.btn.ghost {
  background: transparent;
}

.btn.danger {
  color: var(--danger);
}

/* --------------------------------------------------------------- Filter */

/* Kopf- und Filterzeile kleben gemeinsam oben – als ein Block, damit beim
   Scrollen nichts unter etwas anderem verschwindet. */
.searchbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.75rem 0 0.7rem;
  margin-bottom: 0.4rem;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.searchbar-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.searchbar-row.secondary {
  flex-wrap: wrap;
}

.age-filter {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  white-space: nowrap;
}

.age-filter span {
  color: var(--muted);
  font-size: 0.9rem;
}

.age-filter select {
  width: auto;
}

.searchbar .q {
  min-width: 8rem;
}

/* ------------------------------------------------------- Tags und Chips */

.tagfield {
  position: relative; /* Anker für die Vorschlagsliste */
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  align-items: center;
  flex: 1;
  min-width: 0;
  min-height: var(--control);
  padding: 0.2rem 0.35rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.tagfield:focus-within {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.tagfield .tag-input {
  flex: 1 1 8rem;
  min-width: 6rem;
  min-height: 0;
  padding: 0.2rem 0.25rem;
  border: 0;
  background: transparent;
}

.tagfield .tag-input:focus-visible {
  outline: none;
}

/* Die Vorschlagsliste soll über der ganzen Feldbreite liegen, nicht nur
   unter dem Eingabestummel hinter den Chips. */
.tagfield .combo {
  display: contents;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem 0.3rem 0.2rem 0.55rem;
  background: var(--accent-soft);
  border-radius: 999px;
  font-size: 0.85rem;
}

.chip-remove {
  border: 0;
  background: transparent;
  cursor: pointer;
  color: var(--muted);
  padding: 0 0.25rem;
  line-height: 1;
  font-size: 1rem;
}

.tag {
  display: inline-block;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.78rem;
  white-space: nowrap;
}

a.tag.link {
  color: var(--accent);
  text-decoration: none;
}

/* ----------------------------------------------------------- Typeahead */

.combo {
  position: relative;
}

.suggest {
  position: absolute;
  z-index: 30;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  min-width: 14rem;
  max-height: 16rem;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.suggest-item {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.4rem 0.65rem;
  cursor: pointer;
}

.suggest-item.active {
  background: var(--accent-soft);
}

.suggest-hint {
  color: var(--muted);
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
}

/* --------------------------------------------------------- Trefferliste */

.status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--muted);
  font-size: 0.85rem;
  padding: 0.1rem 0 0.5rem;
}

.status .count {
  flex: 1;
}

.list-actions {
  display: flex;
  gap: 0.4rem;
}

.list-actions .btn {
  min-height: 1.9rem;
  padding: 0.15rem 0.55rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.btn[hidden] {
  display: none;
}

.results {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

a.result {
  display: block;
  padding: 0.6rem 0.8rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: inherit;
  text-decoration: none;
}

a.result:hover {
  border-color: var(--accent);
}

.result-head {
  display: flex;
  gap: 0.75rem;
  align-items: baseline;
}

.result-title {
  font-weight: 600;
  flex: 1;
}

.result-age {
  color: var(--muted);
  font-size: 0.8rem;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.result-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.4rem;
  align-items: center;
  color: var(--muted);
  font-size: 0.78rem;
}

.meta-book {
  font-weight: 600;
}

.meta-sep {
  opacity: 0.6;
}

.result-meta .tag:first-of-type {
  margin-left: 0.2rem;
}

.results.compact a.result {
  padding: 0.35rem 0.8rem;
}

.results.compact .result-meta {
  display: none;
}

/* ------------------------------------------------------------- Detail */

.detail-view:focus {
  outline: none;
}

.detail {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.detail h1 {
  margin: 0;
  font-size: 1.5rem;
}

.detail-meta,
.detail-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.6rem;
}

.pill {
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 0.85rem;
}

.pill.age {
  background: var(--accent-soft);
}

.markdown {
  margin-top: 1rem;
  border-top: 1px solid var(--border);
  padding-top: 1rem;
  overflow-wrap: anywhere;
}

.markdown > :first-child {
  margin-top: 0;
}

.markdown h1,
.markdown h2,
.markdown h3 {
  line-height: 1.25;
}

.markdown pre {
  background: var(--surface-2);
  padding: 0.75rem;
  border-radius: var(--radius);
  overflow-x: auto;
}

.markdown code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}

.markdown blockquote {
  margin: 1rem 0;
  padding-left: 0.9rem;
  border-left: 3px solid var(--border);
  color: var(--muted);
}

.markdown table {
  border-collapse: collapse;
}

.markdown th,
.markdown td {
  border: 1px solid var(--border);
  padding: 0.3rem 0.5rem;
}

/* ----------------------------------------------------------- Formular */

.form-title {
  font-size: 1.2rem;
  margin: 0;
}

.entry-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
}

.field {
  display: grid;
  grid-template-columns: 9rem 1fr;
  gap: 0.75rem;
  align-items: start;
}

.field-label {
  display: flex;
  flex-direction: column;
  padding-top: 0.5rem;
  font-weight: 600;
}

.field-label small {
  font-weight: 400;
  color: var(--muted);
  font-size: 0.75rem;
}

.age-range {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.age-range select {
  width: auto;
}

.form-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: flex-end;
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
}

/* -------------------------------------------------------------- Login */

.login {
  max-width: 22rem;
  margin: 15vh auto 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}

.login h1 {
  font-size: 1.25rem;
  margin: 0;
}

.login label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  color: var(--muted);
  font-size: 0.9rem;
}

/* ------------------------------------------------------ Hilfe & Toasts */

dialog.help {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 1.25rem;
  max-width: 30rem;
}

dialog.help::backdrop {
  background: rgba(0, 0, 0, 0.4);
}

dialog.help h2 {
  margin-top: 0;
  font-size: 1.1rem;
}

dialog.help dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.4rem 1rem;
  margin: 0 0 1rem;
}

dialog.help dd {
  margin: 0;
}

kbd {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85em;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0.05rem 0.35rem;
  white-space: nowrap;
}

/* ------------------------------------------------------------- Fußzeile */

.appfoot {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 0.4rem;
  max-width: 62rem;
  margin: 0 auto;
  padding: 1rem 1rem 2rem;
  color: var(--muted);
  font-size: 0.8rem;
}

.appfoot:empty {
  display: none;
}

.foot-dot {
  opacity: 0.5;
}

.appfoot .rev {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.95em;
  color: inherit;
}

a.rev:hover {
  color: var(--accent);
}

.linkish {
  border: 0;
  background: none;
  padding: 0;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  font-size: inherit;
}

.toasts {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  z-index: 50;
  pointer-events: none;
}

.toast {
  background: var(--text);
  color: var(--bg);
  padding: 0.5rem 0.9rem;
  border-radius: 999px;
  box-shadow: var(--shadow);
  font-size: 0.9rem;
  transition: opacity 0.35s;
}

.toast-error {
  background: var(--danger);
  color: #fff;
}

.toast.fade {
  opacity: 0;
}

/* --------------------------------------------------------------- Handy */

@media (max-width: 699px) {
  .app {
    padding: 0 0.75rem 3rem;
  }

  .topbar {
    padding: 0.5rem 0;
  }

  .filters {
    gap: 0.5rem;
  }

  .filter.actions {
    margin-left: 0;
    width: 100%;
    justify-content: space-between;
  }

  .field {
    grid-template-columns: 1fr;
    gap: 0.25rem;
  }

  .field-label {
    padding-top: 0;
  }

  .form-actions {
    position: sticky;
    bottom: 0;
    background: var(--surface);
  }

  .detail {
    padding: 1rem 0.9rem;
  }
}
