:root {
  --bg: #f7f3ee;
  --surface: #ffffff;
  --text: #2b2420;
  --muted: #7a6f66;
  --border: #e4dcd2;
  --accent: #7a4b2a;
  --accent-text: #ffffff;
  --ok: #2f7a3f;
  --danger: #b3362c;
  --owned-bg: #fdf0d5; --owned-line: #d48a12;
  --read-bg: #e2edfb; --read-line: #3a78d0;
  --both-bg: #eee3f8; --both-line: #8650c4;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1714;
    --surface: #26211d;
    --text: #efe7de;
    --muted: #a99d92;
    --border: #3a332d;
    --accent: #d39a6a;
    --accent-text: #1b1714;
    --ok: #6cc07d;
    --danger: #ef7d73;
    --owned-bg: #3a2d14; --owned-line: #e3a53e;
    --read-bg: #1b2a3e; --read-line: #6ea3ec;
    --both-bg: #2e2240; --both-line: #b48ae6;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1714; --surface: #26211d; --text: #efe7de; --muted: #a99d92;
  --border: #3a332d; --accent: #d39a6a; --accent-text: #1b1714;
  --ok: #6cc07d; --danger: #ef7d73;
  --owned-bg: #3a2d14; --owned-line: #e3a53e;
  --read-bg: #1b2a3e; --read-line: #6ea3ec;
  --both-bg: #2e2240; --both-line: #b48ae6;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
a { color: var(--accent); }
[hidden] { display: none !important; }

/* Barre du haut ; les onglets passent en bas sur téléphone */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; text-decoration: none; color: var(--text); white-space: nowrap; }
.tabs { display: flex; gap: 4px; flex: 1; }
.tabs a {
  padding: 8px 12px; border-radius: 8px; text-decoration: none; color: var(--muted);
}
.tabs a.active { background: var(--bg); color: var(--accent); font-weight: 600; }
.user-btn {
  background: none; border: 1px solid var(--border); color: var(--muted);
  border-radius: 999px; padding: 6px 12px; cursor: pointer; font: inherit; font-size: 14px;
}
@media (max-width: 700px) {
  .topbar { justify-content: space-between; }
  .tabs {
    position: fixed; left: 0; right: 0; bottom: 0;
    background: var(--surface); border-top: 1px solid var(--border);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    justify-content: space-around;
  }
  .tabs a { font-size: 13px; padding: 8px 6px; text-align: center; flex: 1; }
}

main { max-width: 900px; margin: 0 auto; padding: 16px; }
h1 { font-size: 22px; margin: 4px 0 16px; }
.muted { color: var(--muted); }
.error { color: var(--danger); }

input[type="search"], input[type="text"], input[type="password"] {
  width: 100%; padding: 12px 14px; font: inherit;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
}
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 16px; border-radius: var(--radius); border: 1px solid var(--accent);
  background: var(--accent); color: var(--accent-text);
  font: inherit; font-weight: 600; cursor: pointer;
}
.btn.secondary { background: transparent; color: var(--accent); }
.btn.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }

/* Liste de livres */
.books { list-style: none; padding: 0; margin: 16px 0; display: grid; gap: 10px; }
.book {
  display: flex; gap: 12px; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px; text-decoration: none; color: inherit;
}
.cover {
  width: 48px; height: 70px; flex: none; border-radius: 4px; object-fit: cover;
  background: var(--border);
}
.book-info { min-width: 0; }
.book-title { font-weight: 600; overflow-wrap: anywhere; }
.book-meta { color: var(--muted); font-size: 14px; }
.badges { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.badge {
  font-size: 12px; padding: 2px 8px; border-radius: 999px;
  border: 1px solid var(--border); color: var(--muted);
}
.badge.ok { color: var(--ok); border-color: var(--ok); }

/* Sélection multiple */
.btn.small { padding: 6px 12px; font-size: 14px; }
.list-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin: 12px 0 0; }
.list-message { font-size: 14px; }
.book.selectable { cursor: pointer; }
.book.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; }
.select-box { width: 20px; height: 20px; flex: none; accent-color: var(--accent); pointer-events: none; }
.bulk-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px 16px;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--border);
  box-shadow: 0 -4px 16px rgba(0, 0, 0, .12);
}
.bulk-group { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.bulk-bar select {
  font: inherit; font-size: 14px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px; margin-left: 4px;
}
.bulk-spacer { height: 140px; }
@media (max-width: 700px) {
  .bulk-bar { bottom: calc(58px + env(safe-area-inset-bottom)); padding-bottom: 10px; }
  .bulk-spacer { height: 220px; }
}

/* Fiche livre */
.detail { display: flex; gap: 20px; flex-wrap: wrap; }
.detail .cover { width: 140px; height: 205px; }
.detail-info { flex: 1; min-width: 240px; }
.detail dl { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; margin: 12px 0; }
.detail dt { color: var(--muted); }
.detail dd { margin: 0; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin: 16px 0; }

/* Fiche hors FEL */
.source-note { font-size: 14px; }
.link-btn { background: none; border: none; padding: 0; color: var(--accent); font: inherit; cursor: pointer; text-decoration: underline; }
.book-form { display: grid; gap: 10px; max-width: 520px; margin-top: 12px; }
.book-form input {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
h2 { font-size: 18px; margin: 20px 0 4px; }

/* Scanner */
#reader { max-width: 480px; margin: 0 auto 16px; border-radius: var(--radius); overflow: hidden; }
#reader video { border-radius: var(--radius); }
.manual { display: flex; gap: 8px; max-width: 480px; margin: 0 auto; }

/* Liste de scan */
.scan-form { margin: 0 0 12px; max-width: none; }
.scan-form input { font-size: 18px; letter-spacing: .5px; }
#camera { font-size: 20px; padding: 8px 14px; }
#camera.active { background: var(--accent); color: var(--accent-text); }
.scan-status { min-height: 1.4em; margin: 4px 0 8px; }
.defaults {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 8px 12px; margin-bottom: 12px; display: grid; gap: 6px;
}
.defaults summary { cursor: pointer; color: var(--muted); }
.defaults[open] { display: grid; }
.defaults[open] summary { margin-bottom: 6px; }
.scan-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.scan-item {
  display: flex; gap: 12px; align-items: flex-start; position: relative;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 40px 10px 10px;
}
.scan-item .book-title a { color: inherit; text-decoration: none; }
.scan-checks { display: flex; flex-wrap: wrap; gap: 4px 16px; margin-top: 6px; font-size: 14px; }
.remove {
  position: absolute; top: 6px; right: 6px; width: 30px; height: 30px;
  border: none; background: none; color: var(--muted); font-size: 16px; cursor: pointer; border-radius: 50%;
}
.remove:hover { background: var(--bg); color: var(--danger); }
.scan-item.flash { animation: flash 1.2s ease-out; }
@keyframes flash { from { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent); } }

/* Couleurs d'état de la liste de scan */
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 13px; color: var(--muted); margin: -8px 0 14px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.swatch {
  width: 14px; height: 14px; border-radius: 4px; display: inline-block;
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--border);
}
.swatch.owned, .scan-item.owned { background: var(--owned-bg); border-color: var(--owned-line); }
.swatch.read, .scan-item.read { background: var(--read-bg); border-color: var(--read-line); }
.swatch.owned-read, .scan-item.owned-read { background: var(--both-bg); border-color: var(--both-line); }
.scan-item.owned, .scan-item.read, .scan-item.owned-read { border-left-width: 6px; }
.badge.state-owned { color: var(--owned-line); border-color: var(--owned-line); font-weight: 600; }
.badge.state-read { color: var(--read-line); border-color: var(--read-line); font-weight: 600; }

/* Import */
.import-options { display: grid; gap: 12px; margin-bottom: 16px; }
.field { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.field input, .field select, .preview select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px;
}
.check { display: flex; gap: 8px; align-items: center; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); }
table.preview { border-collapse: collapse; width: 100%; font-size: 14px; background: var(--surface); }
.preview th, .preview td {
  padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap;
}
.preview tr.headers th { color: var(--muted); font-weight: 600; }
.preview td:not(.mapped) { color: var(--muted); opacity: .6; }
.report {
  margin-top: 16px; padding: 12px 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.report h2 { font-size: 18px; margin: 4px 0 8px; }
.problems { font-size: 14px; max-height: 300px; overflow-y: auto; }

/* Connexion */
.login { max-width: 360px; margin: 10vh auto 0; display: grid; gap: 12px; }
.login h1 { text-align: center; }
