:root {
  --bg: #f6f4ef;
  --panel: #ffffff;
  --ink: #2a2723;
  --muted: #7a7268;
  --line: #e4ded3;
  --accent: #d9622b;
  --accent-ink: #ffffff;
  --accent-soft: #fbe7dc;
  --tag: #eef3e8;
  --tag-ink: #3f5a2a;
  --danger: #b3261e;
  --radius: 10px;
  --shadow: 0 8px 30px rgba(40, 30, 20, .15);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1a18; --panel: #262320; --ink: #eee8e0; --muted: #a59c90; --line: #3a3530;
    --accent: #ef7d45; --accent-soft: #4a2c1d; --tag: #2f3a26; --tag-ink: #c8e0b0; --danger: #ff8a80;
    --shadow: 0 8px 30px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
label > input, label > select, label > textarea, label > .tag-input { margin-top: 4px; color: var(--ink); font-size: 15px; }

.btn, .primary {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel);
  cursor: pointer; text-decoration: none; color: var(--ink); white-space: nowrap;
}
.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn:hover { border-color: var(--muted); }
.primary:hover { filter: brightness(1.07); }
button:disabled { opacity: .5; cursor: progress; }
.link { background: none; border: 0; padding: 4px; color: var(--accent); cursor: pointer; }
.danger { color: var(--danger); }
.hint { font-size: 12px; color: var(--muted); margin: 6px 0 0; }

/* Kopfzeile */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 16px;
  padding: 12px 20px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 19px; margin: 0; white-space: nowrap; }
.search { flex: 1; max-width: 640px; }
.search input { padding: 10px 14px; border-radius: 999px; background: var(--bg); }
.actions { display: flex; gap: 8px; margin-left: auto; }
.menu { position: relative; }
.menu summary { list-style: none; font-weight: 700; }
.menu summary::-webkit-details-marker { display: none; }
.menu-list {
  position: absolute; right: 0; top: calc(100% + 6px); display: flex; flex-direction: column; gap: 4px;
  padding: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); min-width: 260px;
}
.menu-list > * { justify-content: flex-start; border: 0; background: none; padding: 8px 10px; text-align: left; cursor: pointer; }
.menu-list > *:hover { background: var(--bg); }

/* Layout */
.layout { display: grid; grid-template-columns: 270px 1fr; gap: 20px; padding: 20px; align-items: start; }
.filters {
  position: sticky; top: 80px; background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px; max-height: calc(100vh - 100px); display: flex; flex-direction: column;
}
.filter-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.filter-head h2, .filter-head h3 { margin: 0; font-size: 15px; }
.filter-head select { width: auto; padding: 3px 6px; font-size: 12px; }
.tagfilter { display: flex; flex-direction: column; min-height: 0; flex: 1; border-top: 1px solid var(--line); padding-top: 12px; }
.taglist { overflow: auto; margin-top: 8px; display: flex; flex-wrap: wrap; gap: 5px; align-content: flex-start; }
.taglist button {
  border: 1px solid transparent; background: var(--tag); color: var(--tag-ink); border-radius: 999px;
  padding: 2px 9px; font-size: 13px; cursor: pointer;
}
.taglist button small { opacity: .6; margin-left: 3px; }
.taglist button.on { background: var(--accent); color: var(--accent-ink); }

main { min-width: 0; }
.listbar { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; flex-wrap: wrap; min-height: 32px; }
#count { color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 6px 2px 10px; border-radius: 999px;
  background: var(--accent-soft); font-size: 13px; border: 0; cursor: pointer;
}
.chip::after { content: "✕"; font-size: 11px; opacity: .6; }

/* Tabelle */
table.books {
  width: 100%; border-collapse: separate; border-spacing: 0; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius); table-layout: fixed;
}
.books th:first-child { border-top-left-radius: var(--radius); }
.books th:last-child { border-top-right-radius: var(--radius); }
.books th {
  text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
  padding: 10px; border-bottom: 1px solid var(--line); background: var(--panel); position: sticky; top: var(--topbar-h, 68px); z-index: 1;
}
.books th[data-sort] { cursor: pointer; user-select: none; }
.books th[data-sort]:hover { color: var(--ink); }
.books th.asc::after { content: " ▲"; }
.books th.desc::after { content: " ▼"; }
.books td { overflow-wrap: anywhere; padding: 6px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.books tbody tr { cursor: pointer; }
.books tbody tr:hover { background: var(--bg); }
.books tbody tr:last-child td { border-bottom: 0; }
.c-cover { width: 64px; }
.c-nr { width: 64px; }
.books th[data-sort="title"] { width: 26%; }
.books th[data-sort="author"] { width: 17%; }
.books th[data-sort="publisher"] { width: 12%; }
.nr { color: var(--muted); font-variant-numeric: tabular-nums; }
.title { font-weight: 600; }
.mini {
  width: 40px; height: 54px; border-radius: 4px; object-fit: cover; display: block; background: var(--line);
  box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.mini.none { display: flex; align-items: center; justify-content: center; font-size: 18px; box-shadow: none; opacity: .7; }
.tags { display: flex; flex-wrap: wrap; gap: 4px; }
.tag { background: var(--tag); color: var(--tag-ink); border-radius: 999px; padding: 1px 8px; font-size: 12px; }
.empty { text-align: center; color: var(--muted); padding: 40px; }

/* Dialog */
.dialog {
  border: 0; border-radius: 14px; padding: 0; width: min(920px, calc(100vw - 32px)); max-height: calc(100vh - 32px);
  background: var(--panel); color: var(--ink); box-shadow: var(--shadow);
}
.dialog.small { width: min(460px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(30, 20, 10, .45); }
.dialog form { display: flex; flex-direction: column; max-height: calc(100vh - 32px); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 20px; border-bottom: 1px solid var(--line); }
.dlg-head h2 { margin: 0; font-size: 18px; }
.close { font-size: 18px; color: var(--muted); }
.dlg-body { display: grid; grid-template-columns: 200px 1fr; gap: 22px; padding: 20px; overflow: auto; }
.dlg-pad { padding: 20px; }
.dlg-pad progress { width: 100%; height: 14px; }
.dlg-foot { display: flex; align-items: center; gap: 8px; padding: 12px 20px; border-top: 1px solid var(--line); }
.spacer { flex: 1; }
.cover-col { display: flex; flex-direction: column; gap: 8px; }
.cover-big {
  width: 100%; aspect-ratio: 3 / 4; border-radius: 8px; background: var(--bg) center / cover no-repeat;
  border: 1px solid var(--line); display: flex; align-items: center; justify-content: center; font-size: 48px;
}
.file-btn { margin: 0; color: var(--ink); font-size: 15px; }
.row { display: flex; gap: 12px; }
.row .grow { flex: 1; }
.w-nr { width: 100px; }
.w-year { width: 100px; }
.tag-input {
  display: flex; flex-wrap: wrap; gap: 5px; padding: 6px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel);
}
.tag-input:focus-within { outline: 2px solid var(--accent); outline-offset: -1px; }
.tag-input input { border: 0; outline: 0; padding: 3px 4px; flex: 1; min-width: 160px; width: auto; }
.tag-input .tag { display: inline-flex; align-items: center; gap: 3px; font-size: 13px; padding: 2px 4px 2px 9px; }
.tag-input .tag button { border: 0; background: none; cursor: pointer; color: inherit; opacity: .6; padding: 0 3px; }

/* Suchergebnisse */
.lookup { border-top: 1px solid var(--line); padding-top: 12px; }
.lookup h3 { font-size: 14px; margin: 0 0 8px; }
.hits { display: grid; gap: 8px; }
.hit {
  display: grid; grid-template-columns: 52px 1fr auto; gap: 12px; align-items: center; padding: 8px;
  border: 1px solid var(--line); border-radius: 8px;
}
.hit img, .hit .mini { width: 52px; height: 70px; }
.hit .meta { font-size: 13px; color: var(--muted); }
.hit .meta b { color: var(--ink); font-size: 14px; display: block; }
.hit .btns { display: flex; flex-direction: column; gap: 4px; }
.hit .btns button { padding: 4px 10px; font-size: 13px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--bg);
  padding: 10px 18px; border-radius: 8px; box-shadow: var(--shadow); z-index: 100; max-width: calc(100vw - 32px);
}
.toast.error { background: var(--danger); color: #fff; }

.mobile-only { display: none; }

@media (max-width: 860px) {
  .topbar { flex-wrap: wrap; padding: 10px 16px; gap: 10px; }
  .topbar h1 { font-size: 17px; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .layout { grid-template-columns: 1fr; padding: 12px 16px; }
  .filters { display: none; position: static; max-height: 60vh; }
  .books th[data-sort="title"], .books th[data-sort="author"] { width: auto; }
  .filters.open { display: flex; }
  .mobile-only { display: inline-flex; }
  .books th { top: 0; position: static; }
  .books thead .c-tags, .books td.c-tags, .books thead th[data-sort="publisher"], .books td.c-pub { display: none; }
  .dlg-body { grid-template-columns: 1fr; }
  .cover-col { flex-direction: row; flex-wrap: wrap; }
  .cover-big { width: 120px; }
}
@media (max-width: 560px) {
  .books thead th[data-sort="author"], .books td.c-author { display: none; }
  .title small { display: block; font-weight: 400; color: var(--muted); }
}
@media (min-width: 561px) { .title small { display: none; } }

/* Anmeldeseite */
.login-page {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px 16px;
  background: radial-gradient(circle at 50% 0%, var(--accent-soft), var(--bg) 60%);
}
.login-card {
  width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 16px;
  box-shadow: var(--shadow); padding: 32px 28px 28px; text-align: center;
}
.login-logo { width: 112px; height: 112px; display: block; margin: 0 auto 8px; }
.login-card h1 { font-size: 24px; margin: 0 0 24px; letter-spacing: .01em; }
.login-card form { text-align: left; }
.login-card .primary { width: 100%; padding: 11px; margin-top: 4px; font-size: 16px; }
.login-error { color: var(--danger); font-size: 14px; margin: 0 0 12px; }
.topbar h1 { display: flex; align-items: center; gap: 8px; }
.topbar h1 img { width: 30px; height: 30px; }
.login-info { font-size: 14px; color: var(--muted); margin: -8px 0 18px; text-align: center; }
.login-info b { color: var(--ink); }
.login-switch { display: block; margin: 10px auto 0; }

/* Benutzerverwaltung */
.menu-user { padding: 8px 10px 4px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.menu-user b { color: var(--ink); font-size: 14px; }
.dialog.medium { width: min(640px, calc(100vw - 32px)); }
.dlg-pad .row label { margin-bottom: 0; }
.users { width: 100%; border-collapse: collapse; margin-top: 8px; }
.users td { padding: 8px 6px; border-top: 1px solid var(--line); vertical-align: middle; }
.users td:last-child { text-align: right; white-space: nowrap; }
.users .badge { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--accent-soft); margin-left: 6px; }
.users .sub { display: block; font-size: 12px; color: var(--muted); }
.users button { padding: 4px 8px; font-size: 13px; }
.dialog form.adduser { display: grid; flex-direction: initial; max-height: none; grid-template-columns: 1fr 1fr auto; gap: 8px; align-items: end; padding: 12px; background: var(--bg); border-radius: 8px; }
.adduser label { margin: 0; }
.adduser .chk { display: flex; align-items: center; gap: 6px; grid-column: 1 / -1; color: var(--ink); font-size: 14px; }
.adduser .chk input { width: auto; margin: 0; }
@media (max-width: 560px) { .adduser { grid-template-columns: 1fr; } }

/* ---------- Mobil: kein seitliches Verschieben ---------- */
html, body { max-width: 100%; overflow-x: hidden; }
@media (max-width: 860px) {
  /* iOS zoomt bei Eingabefeldern unter 16px automatisch hinein – danach ließe sich die Seite seitlich schieben */
  input, select, textarea { font-size: 16px; }
  .desktop-only { display: none !important; }
  .listbar .btn { padding: 6px 12px; }
}

/* ---------- Tags-Ansicht (mobil) ---------- */
.tagview {
  position: fixed; inset: 0; z-index: 50; background: var(--bg); display: flex; flex-direction: column;
  overflow: hidden; backface-visibility: hidden; transform-origin: center;
}
.tagview[hidden] { display: none; }
.tv-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--panel);
  border-bottom: 1px solid var(--line); flex-shrink: 0;
}
.tv-head h2 { margin: 0; font-size: 18px; flex: 1; }
.tv-head select { width: auto; padding: 5px 8px; font-size: 14px; }
.tv-body { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 12px 16px 24px; -webkit-overflow-scrolling: touch; }
.tv-tags {
  max-height: 42vh; overflow-y: auto; margin: 10px 0 0; padding: 10px; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius);
}
.tv-tags button { font-size: 14px; padding: 4px 11px; }
.tv-resulthead { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 16px 0 8px; color: var(--muted); font-size: 14px; }
.tv-results { list-style: none; margin: 0; padding: 0; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.tv-results:empty { display: none; }
.tv-results li { display: flex; align-items: center; gap: 12px; padding: 8px 12px; border-bottom: 1px solid var(--line); cursor: pointer; min-width: 0; }
.tv-results li:last-child { border-bottom: 0; }
.tv-results li > div { min-width: 0; }
.tv-results b { display: block; font-size: 15px; overflow-wrap: anywhere; }
.tv-results span { font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.tv-results .mini { flex-shrink: 0; }

/* Drehen beim Öffnen/Schließen */
@keyframes flipIn  { from { transform: perspective(1200px) rotateY(-90deg); opacity: .4; } to { transform: perspective(1200px) rotateY(0); opacity: 1; } }
@keyframes flipOut { from { transform: perspective(1200px) rotateY(0); opacity: 1; } to { transform: perspective(1200px) rotateY(90deg); opacity: .4; } }
.tagview.flip-in { animation: flipIn .38s ease-out both; }
.tagview.flip-out { animation: flipOut .38s ease-in both; }
body.tagview-open { overflow: hidden; }
@media (prefers-reduced-motion: reduce) { .tagview.flip-in, .tagview.flip-out { animation-duration: .01s; } }
