:root {
  --bg: #f4f1ec; --bg2: #ebe6de; --panel: #fffdf9; --ink: #26231f; --muted: #7a7268; --line: #ddd5ca;
  --accent: #b4432f; --accent-ink: #fff; --ok: #3f7d4e; --shadow: 0 1px 2px rgba(40,30,20,.18), 0 6px 16px rgba(40,30,20,.14);
  --cw: 150px; color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #16151a; --bg2: #1f1e24; --panel: #24232a; --ink: #ece8e2; --muted: #9a948c; --line: #34323a;
    --accent: #e0664d; --ok: #6cbf7f; --shadow: 0 1px 2px rgba(0,0,0,.5), 0 6px 18px rgba(0,0,0,.45); color-scheme: dark; }
}
body.size-s { --cw: 112px; } body.size-l { --cw: 200px; }
@media (max-width: 520px) { body { --cw: 100px; } body.size-s { --cw: 80px; } body.size-l { --cw: 150px; } }
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 15px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif; }
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---- barre du haut ---- */
#bar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 12px; padding: 8px 16px;
  padding-top: max(8px, env(safe-area-inset-top)); background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); border-bottom: 1px solid var(--line); }
#logo { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; letter-spacing: .2px; flex: none; }
#logo svg { fill: var(--accent); stroke: none; width: 24px; height: 24px; }
#crumbs { flex: 1; min-width: 0; display: flex; gap: 4px; align-items: center; overflow: hidden; white-space: nowrap; color: var(--muted); font-size: 14px; }
#crumbs a { overflow: hidden; text-overflow: ellipsis; padding: 2px 4px; border-radius: 6px; }
#crumbs a:hover { background: var(--bg2); color: var(--ink); }
#crumbs a:last-child { color: var(--ink); font-weight: 600; }
#crumbs .sep { opacity: .5; }
#tools { display: flex; align-items: center; gap: 2px; flex: none; }
#tools > button, #searchform button { background: none; border: 0; padding: 8px; border-radius: 10px; display: grid; place-items: center; }
#tools > button:hover, #searchform button:hover { background: var(--bg2); }
#btn-up[disabled] { opacity: .3; pointer-events: none; }
#searchform { display: flex; align-items: center; background: var(--bg2); border-radius: 10px; }
#q { border: 0; background: none; color: var(--ink); font: inherit; width: 180px; padding: 6px 0 6px 10px; outline: none; }
@media (max-width: 640px) {
  #logo span { display: none; }
  #crumbs { display: none; }
  #searchform { flex: 1; }
  #q { width: 100%; min-width: 0; }
  #tools { flex: 1; }
}

/* ---- contenu ---- */
#view { padding: 20px 16px 60px; max-width: 1500px; margin: 0 auto; }
h1 { font-size: 22px; margin: 4px 0 16px; }
h2.sec { font-size: 17px; margin: 28px 0 12px; display: flex; align-items: baseline; gap: 12px; }
h2.sec a { font-size: 14px; font-weight: 500; color: var(--accent); }
.empty { color: var(--muted); padding: 40px 0; text-align: center; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(var(--cw), 1fr)); gap: 14px 10px; }
.row-scroll { display: grid; grid-auto-flow: column; grid-auto-columns: var(--cw); gap: 18px; overflow-x: auto; padding-bottom: 8px; scrollbar-width: thin; }
.card { display: block; content-visibility: auto; contain-intrinsic-size: auto calc(var(--cw) * 1.45 + 52px); min-width: 0; padding: 8px 8px 0; }
.cv { position: relative; aspect-ratio: 160 / 230; display: flex; align-items: flex-end; justify-content: center; }
.cv img { max-width: 100%; max-height: 100%; border-radius: 3px; box-shadow: var(--shadow); background: var(--bg2); transition: transform .15s; display: block; }
.cv img.ph { width: 100%; height: 100%; }
.card:hover .cv img, .card:focus-visible .cv img { transform: translateY(-3px); }
.card.folder .cv::before { content: ""; position: absolute; inset: 6px -5px -5px 6px; border-radius: 3px; background: var(--line); box-shadow: var(--shadow); }
.card.folder .cv img { position: relative; }
.ph { display: grid; place-items: center; color: var(--muted); font-weight: 700; font-size: 13px; letter-spacing: 1px; background: var(--bg2); border-radius: 3px; box-shadow: var(--shadow); width: 100%; height: 100%; }
.badge { position: absolute; top: -6px; right: -6px; background: var(--accent); color: var(--accent-ink); font-size: 12px; font-weight: 700; min-width: 24px; height: 24px; padding: 0 6px; border-radius: 12px; display: grid; place-items: center; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.done-mark { position: absolute; top: 6px; right: 6px; width: 24px; height: 24px; border-radius: 50%; background: var(--ok); display: grid; place-items: center; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.done-mark svg { width: 15px; height: 15px; stroke: #fff; stroke-width: 3; }
.bar { position: absolute; left: 0; right: 0; bottom: 0; height: 4px; background: rgba(0,0,0,.35); border-radius: 0 0 3px 3px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); }
.card .t { margin-top: 8px; font-size: 13px; text-align: center; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.card .p { font-size: 11.5px; color: var(--muted); text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.cats { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.cat { display: flex; gap: 16px; align-items: center; padding: 16px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; transition: border-color .15s; }
.cat:hover { border-color: var(--accent); }
.cat img, .cat .ph { width: 64px; height: 92px; object-fit: cover; border-radius: 3px; box-shadow: var(--shadow); flex: none; }
.cat b { display: block; font-size: 18px; }
.cat span { color: var(--muted); font-size: 13px; }

/* ---- fenêtres ---- */
dialog { border: 0; padding: 0; border-radius: 16px; background: var(--panel); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.35); max-width: min(680px, calc(100vw - 24px)); width: 100%; }
dialog::backdrop { background: rgba(20,15,10,.45); backdrop-filter: blur(2px); }
#settings form { padding: 20px 22px; }
#settings h2 { margin: 0 0 12px; font-size: 19px; }
fieldset { border: 0; margin: 0 0 14px; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
legend { font-size: 13px; color: var(--muted); margin-bottom: 6px; padding: 0; width: 100%; }
fieldset label { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border: 1px solid var(--line); border-radius: 999px; cursor: pointer; font-size: 14px; }
fieldset label:has(input:checked) { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
fieldset input { position: absolute; opacity: 0; pointer-events: none; }
.row { display: flex; gap: 10px; justify-content: space-between; flex-wrap: wrap; margin-top: 8px; }
.primary, .ghost { border-radius: 10px; padding: 9px 16px; border: 1px solid var(--line); background: none; display: inline-flex; align-items: center; gap: 8px; }
.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.ghost:hover { background: var(--bg2); }
.primary svg, .ghost svg { width: 18px; height: 18px; }

#details .d { display: flex; gap: 20px; padding: 20px; }
#details .d-cover { flex: none; width: 150px; }
#details .d-cover img, #details .d-cover .ph { width: 150px; aspect-ratio: 160/230; object-fit: cover; border-radius: 4px; box-shadow: var(--shadow); }
#details .d-info { min-width: 0; flex: 1; }
#details h3 { margin: 0 0 4px; font-size: 19px; line-height: 1.25; overflow-wrap: anywhere; }
#details .authors { color: var(--accent); font-weight: 600; margin-bottom: 8px; }
#details .facts { color: var(--muted); font-size: 13.5px; display: grid; gap: 2px; }
#details .facts a { text-decoration: underline; text-underline-offset: 2px; }
#details .summary { font-size: 13.5px; margin-top: 10px; max-height: 120px; overflow: auto; }
#details .prog { margin-top: 10px; height: 6px; background: var(--bg2); border-radius: 3px; overflow: hidden; }
#details .prog i { display: block; height: 100%; background: var(--accent); }
#details .acts { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 20px 20px; }
#details .close { position: absolute; top: 10px; right: 10px; background: none; border: 0; padding: 6px; border-radius: 8px; }
#details .close:hover { background: var(--bg2); }
@media (max-width: 520px) {
  #details .d { flex-direction: column; align-items: center; text-align: center; }
  #details .acts { justify-content: center; }
}

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 100; font-size: 14px; }

/* ---- lecteur ---- */
#reader { position: fixed; inset: 0; z-index: 50; background: #0d0d0f; color: #eee; touch-action: pan-x pan-y pinch-zoom; }
#stage { position: absolute; inset: 0; overflow: auto; outline: none; display: flex; overscroll-behavior: contain; }
#stage .pg { margin: auto; flex: none; overflow: hidden; position: relative; }
#stage .pg img { display: block; user-select: none; -webkit-user-drag: none; -webkit-touch-callout: none; }
#spinner { position: absolute; left: 50%; top: 50%; width: 36px; height: 36px; margin: -18px; border: 3px solid rgba(255,255,255,.2); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; pointer-events: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.r-ui { position: absolute; left: 0; right: 0; background: rgba(15,15,18,.88); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); transition: transform .2s, opacity .2s; }
#reader:not(.ui) .r-ui { opacity: 0; pointer-events: none; }
#reader:not(.ui) #r-top { transform: translateY(-100%); }
#reader:not(.ui) #r-bottom { transform: translateY(100%); }
#r-top { top: 0; display: flex; align-items: center; gap: 8px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); }
#r-top button { background: none; border: 0; padding: 8px; border-radius: 10px; color: #fff; display: grid; }
#r-top button:hover { background: rgba(255,255,255,.12); }
#r-title { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
#r-bottom { bottom: 0; padding: 12px 16px; padding-bottom: max(12px, env(safe-area-inset-bottom)); }
#r-pos { display: flex; align-items: center; gap: 14px; }
#r-num { font-variant-numeric: tabular-nums; min-width: 72px; font-size: 14px; }
#r-slider { flex: 1; accent-color: #e0664d; }
#r-opts { display: flex; flex-wrap: wrap; gap: 10px 18px; margin-top: 12px; }
.seg { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.seg span { font-size: 12px; color: #aaa; margin-right: 4px; }
.seg button { background: rgba(255,255,255,.08); border: 0; color: #ddd; padding: 6px 10px; border-radius: 8px; font-size: 13px; }
.seg button.on { background: #e0664d; color: #fff; }
#r-help { font-size: 12px; color: #999; margin: 10px 0 0; }
@media (hover: none) { #r-help { display: none; } }

/* ---- administration ---- */
.admin { max-width: 860px; margin: 0 auto; padding-bottom: 90px; }
.admin section { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px 20px; margin-bottom: 18px; }
.admin h2 { font-size: 17px; margin: 0 0 6px; }
.hint { color: var(--muted); font-size: 13.5px; margin: 0 0 14px; }
code { font: 13px ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--bg2); padding: 2px 6px; border-radius: 6px; overflow-wrap: anywhere; }
code.copy { cursor: copy; display: inline-block; padding: 6px 10px; }
.note { background: color-mix(in srgb, var(--accent) 10%, var(--panel)); border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent); border-radius: 10px; padding: 12px 14px; font-size: 13.5px; margin-bottom: 14px; }
.note code { display: block; margin-top: 8px; padding: 8px 10px; }
.libcard { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; margin-bottom: 12px; background: var(--bg); }
.lib-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.libname { flex: 1; min-width: 140px; font: inherit; font-weight: 650; font-size: 16px; color: var(--ink); background: transparent; border: 1px solid transparent; border-radius: 8px; padding: 6px 8px; }
.libname:hover, .libname:focus { border-color: var(--line); background: var(--panel); outline: none; }
.lib-count { color: var(--muted); font-size: 13px; }
.lib-tools { display: flex; gap: 2px; }
button.icon { background: none; border: 0; padding: 6px; border-radius: 8px; display: grid; place-items: center; color: var(--muted); }
button.icon:hover:not([disabled]) { background: var(--bg2); color: var(--ink); }
button.icon[disabled] { opacity: .3; cursor: default; }
button.icon.danger:hover { color: #c0392b; }
button.icon svg { width: 18px; height: 18px; }
.paths { list-style: none; margin: 8px 0 10px; padding: 0; display: grid; gap: 4px; }
.paths li { display: flex; align-items: center; gap: 8px; padding: 6px 4px 6px 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; font-size: 14px; min-width: 0; }
.paths li > svg { width: 18px; height: 18px; color: var(--accent); flex: none; }
.paths li span { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.paths li.warn { color: var(--accent); }
.ghost.small { padding: 6px 12px; font-size: 14px; }
.field { display: grid; gap: 6px; margin-bottom: 14px; font-size: 14px; }
.field small { color: var(--muted); }
.field select, .field textarea { font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; max-width: 100%; }
.field textarea { font: 13px ui-monospace, Menlo, monospace; resize: vertical; }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; margin-bottom: 10px; }
.stats div { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.stats b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.stats span { color: var(--muted); font-size: 12.5px; }
#savebar { position: fixed; left: 50%; bottom: max(16px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 20; display: flex; align-items: center; gap: 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 10px 12px 10px 16px; box-shadow: 0 10px 30px rgba(0,0,0,.25); white-space: nowrap; max-width: calc(100vw - 24px); }
#savebar[hidden] { display: none; }
#savebar span { font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
.foot { text-align: center; margin-top: 40px; font-size: 13px; }
.foot a { color: var(--muted); }
.foot a:hover { color: var(--accent); }
.row-l { display: flex; gap: 8px; flex-wrap: wrap; }

#picker { max-width: min(620px, calc(100vw - 24px)); }
.pk { display: flex; flex-direction: column; max-height: min(640px, calc(100vh - 48px)); }
.pk-head { display: flex; align-items: center; gap: 10px; padding: 16px 16px 8px 20px; }
.pk-head h2 { flex: 1; font-size: 17px; margin: 0; }
.pk-crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 2px; padding: 0 16px 10px 16px; border-bottom: 1px solid var(--line); font-size: 13.5px; }
.pk-crumbs button { background: none; border: 0; padding: 4px 6px; border-radius: 6px; color: var(--muted); }
.pk-crumbs button:last-child { color: var(--ink); font-weight: 600; }
.pk-crumbs button:hover { background: var(--bg2); }
.pk-crumbs span { color: var(--muted); opacity: .6; }
.pk-list { list-style: none; margin: 0; padding: 6px 8px; overflow: auto; flex: 1; min-height: 160px; }
.pk-list button { width: 100%; display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: none; border: 0; border-radius: 8px; text-align: left; font-size: 14.5px; }
.pk-list button:hover { background: var(--bg2); }
.pk-list button svg { width: 20px; height: 20px; color: var(--accent); flex: none; }
.pk-list button span { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.pk-list em { font-size: 12px; color: var(--muted); font-style: normal; }
.pk-list .empty { padding: 30px 0; }
.pk-foot { display: flex; align-items: center; gap: 12px; justify-content: space-between; padding: 12px 16px; border-top: 1px solid var(--line); font-size: 13.5px; color: var(--muted); flex-wrap: wrap; }
.primary[disabled] { opacity: .45; cursor: default; }
