:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #dc2626;
  --topbar: #1f2937;
  --topbar-text: #f9fafb;
  --todo: #6b7280;
  --reading: #d97706;
  --read: #059669;
  --owned: #059669;
  --wish: #7c3aed;
  --tabbar-h: 60px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #1a1d23;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --border: #2a2f37;
    --accent: #60a5fa;
    --accent-text: #0b1220;
    --danger: #f87171;
    --topbar: #111318;
    --todo: #9ca3af;
    --reading: #fbbf24;
    --read: #34d399;
    --owned: #34d399;
    --wish: #a78bfa;
  }
}

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

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

a { color: var(--accent); }
h2 { font-size: 1.15rem; margin: 0 0 4px; }
h3 { font-size: 1rem; margin: 0 0 10px; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.grow { flex: 1; min-width: 0; }
.grow p { margin: 2px 0 0; }

/* ---------- Barre du haut ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: var(--topbar);
  color: var(--topbar-text);
}
.topbar h1 { font-size: 1.15rem; margin: 0; flex: 1; }
.topbar-link { color: var(--topbar-text); text-decoration: none; font-size: 1.3rem; padding: 0 4px; }
.tag { font-size: .75rem; padding: 2px 8px; border-radius: 999px; background: rgba(255, 255, 255, .18); }

main {
  max-width: 820px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: calc(var(--tabbar-h) + 24px + env(safe-area-inset-bottom));
}

/* ---------- Onglets du bas ---------- */
.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 5;
  display: flex;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.tabbar a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: .75rem;
  color: var(--muted);
  text-decoration: none;
}
.tabbar a span { font-size: 1.25rem; line-height: 1; }
.tabbar a[aria-current] { color: var(--accent); font-weight: 600; }

/* ---------- Boutons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  font: inherit;
  font-size: .9rem;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  text-decoration: none;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--accent); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; margin-top: 8px; }
.btn-small { padding: 5px 10px; font-size: .8rem; }
.icon-btn { border: none; background: transparent; color: var(--text); font-size: 1.2rem; padding: 4px 8px; cursor: pointer; }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
.actions-left { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }

/* ---------- Champs ---------- */
input, select, textarea {
  font: inherit;
  font-size: .95rem;
  padding: 9px 11px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  max-width: 100%;
}
textarea { width: 100%; resize: vertical; }
input:focus, select:focus, textarea:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }
.check { display: flex; align-items: center; gap: 8px; margin: 10px 0; font-size: .92rem; }
.field-inline { display: inline-flex; align-items: center; gap: 6px; font-size: .88rem; color: var(--muted); }
.label { font-size: .85rem; color: var(--muted); margin: 14px 0 6px; }

.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.form-grid label:not(.check) { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); }
.form-grid label input, .form-grid label select { color: var(--text); }
.form-grid .span-all, .form-grid fieldset { grid-column: 1 / -1; }
fieldset { border: 1px solid var(--border); }
legend { padding: 0 6px; font-weight: 600; }

/* ---------- Barre d'outils, puces de filtre ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 10px; }
.toolbar input[type="search"] { flex: 1 1 220px; min-width: 0; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; margin-bottom: 4px; scrollbar-width: none; }
.chip {
  flex-shrink: 0;
  padding: 6px 12px;
  font: inherit;
  font-size: .82rem;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.segmented { display: flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.segmented button {
  flex: 1;
  padding: 9px 6px;
  font: inherit;
  font-size: .9rem;
  color: var(--text);
  background: var(--surface);
  border: none;
  border-right: 1px solid var(--border);
  cursor: pointer;
}
.segmented button:last-child { border-right: none; }
.segmented button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); font-weight: 600; }

.status { color: var(--muted); font-size: .88rem; margin: 8px 2px; }
.empty { text-align: center; padding: 48px 16px; }
.empty .btn { margin-top: 8px; }

/* ---------- Couvertures ---------- */
.nocover {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px;
  font-size: .68rem;
  color: var(--muted);
  text-align: center;
  background: var(--border);
}
.card-cover, .result-cover, .row-cover, .detail-cover, .edition-cover { object-fit: cover; border-radius: 5px; flex-shrink: 0; background: var(--border); }
.card-cover { width: 100%; aspect-ratio: 2 / 3; }
.result-cover { width: 56px; height: 84px; }
.row-cover { width: 48px; height: 72px; }
.detail-cover { width: 110px; height: 165px; }
.edition-cover { width: 100%; aspect-ratio: 2 / 3; }

/* ---------- Bibliothèque ---------- */
.library { list-style: none; padding: 0; margin: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 14px; }
.book-card { position: relative; display: flex; flex-direction: column; gap: 4px; color: inherit; text-decoration: none; }
.card-title { font-size: .85rem; font-weight: 600; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-author { font-size: .76rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-flags { display: flex; gap: 4px; flex-wrap: wrap; }
.status-pill {
  position: absolute;
  top: 6px;
  left: 6px;
  padding: 2px 7px;
  font-size: .68rem;
  font-weight: 700;
  color: #fff;
  border-radius: 999px;
}
.status-todo { background: var(--todo); }
.status-reading { background: var(--reading); }
.status-read { background: var(--read); }
.flag { font-size: .68rem; font-weight: 600; padding: 1px 6px; border-radius: 999px; border: 1px solid currentColor; }
.flag-owned { color: var(--owned); }
.flag-wish { color: var(--wish); }
p.flag { display: inline-block; margin-top: 4px; }

/* ---------- Résultats de recherche ---------- */
.results { list-style: none; padding: 0; margin: 0 0 12px; display: grid; gap: 10px; }
.result {
  display: flex;
  gap: 12px;
  width: 100%;
  padding: 10px;
  text-align: left;
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
}
.result:hover, .result:focus-visible { border-color: var(--accent); }
.result h3 { font-size: .98rem; margin: 0 0 4px; }
.result p { margin: 0; color: var(--muted); font-size: .84rem; }

/* ---------- Fiche ---------- */
.back { display: inline-block; margin-bottom: 12px; font-size: .9rem; text-decoration: none; }
.detail-head { display: flex; gap: 16px; align-items: flex-start; margin-bottom: 14px; }
.detail-head p { margin: 4px 0; }
.description { margin-bottom: 14px; }
.description summary { cursor: pointer; color: var(--accent); }
.description p { white-space: pre-line; line-height: 1.5; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px; margin-bottom: 14px; }
.dates { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 12px; }
.edition-list, .wish-list { list-style: none; padding: 0; margin: 0 0 8px; display: grid; gap: 8px; }
.edition-row, .wish {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}
.edition-row > .btn, .edition-row > button { flex-shrink: 0; }
.wish-title { font-weight: 600; text-decoration: none; color: var(--text); }
.wish-actions { display: flex; flex-direction: column; gap: 6px; }
.cover-preview { display: flex; align-items: flex-end; }

/* ---------- Fenêtres (choix d'édition, ajout) ---------- */
.sheet {
  width: min(960px, 100vw - 16px);
  max-height: calc(100vh - 32px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg);
  color: var(--text);
}
.sheet::backdrop { background: rgba(0, 0, 0, .55); }
.sheet-head {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  padding: 14px 16px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.sheet-head p { margin: 2px 0 0; font-size: .88rem; }
.sheet-body { padding: 16px; }
.isbn-search { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 12px 16px 0; }
.isbn-search input { flex: 1 1 200px; min-width: 0; }
.sheet-tools { display: flex; flex-wrap: wrap; gap: 10px 18px; align-items: center; padding: 10px 16px 0; }
.sheet .status { padding: 0 16px; }
.sheet > .card { margin: 10px 16px; }

.editions { list-style: none; margin: 0; padding: 0 16px 16px; display: grid; grid-template-columns: repeat(auto-fill, minmax(124px, 1fr)); gap: 12px; }
.edition {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 100%;
  height: 100%;
  padding: 8px;
  text-align: left;
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
}
.edition:hover, .edition:focus-visible { border-color: var(--accent); }
.edition-title { font-weight: 600; font-size: .8rem; line-height: 1.3; }
.edition .small { font-size: .72rem; }

/* ---------- Scanner ---------- */
.scan-frame { position: relative; margin: 12px 16px 0; border-radius: 12px; overflow: hidden; background: #000; }
.scan-video { display: block; width: 100%; max-height: 50vh; object-fit: cover; }
.scan-target {
  position: absolute;
  inset: 30% 10%;
  border: 3px solid rgba(255, 255, 255, .85);
  border-radius: 10px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .35);
  pointer-events: none;
}
.scan-log { list-style: none; margin: 0 0 10px; padding: 0; display: grid; gap: 8px; }
.sheet .scan-log { padding: 0 16px 16px; }
.scan-item {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-left: 5px solid var(--muted);
  border-radius: 10px;
  background: var(--surface);
}
.scan-item p { margin: 2px 0 0; }
.tone-owned { border-left-color: var(--owned); }
.tone-wish { border-left-color: var(--wish); background: color-mix(in srgb, var(--wish) 10%, var(--surface)); }
.tone-new { border-left-color: var(--accent); }
.tone-info, .tone-unknown { border-left-color: var(--muted); }

/* ---------- Message bref ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
  z-index: 20;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  font-size: .9rem;
  color: var(--topbar-text);
  background: var(--topbar);
  border-radius: 10px;
  transform: translate(-50%, 20px);
  opacity: 0;
  transition: opacity .25s, transform .25s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
