/* =========================================================
   Ma Bibliothèque — « Cabinet de lecture »
   Papier vieilli, encre sépia, terre cuite. Mode veillée la nuit.
   ========================================================= */

@font-face {
  font-family: 'Fraunces';
  font-style: normal;
  font-weight: 100 900;
  font-display: swap;
  src: url(./fonts/fraunces-var.woff2) format('woff2-variations');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA,
                 U+02DC, U+2000-206F, U+20AC, U+2122, U+2212, U+FEFF, U+FFFD;
}
@font-face {
  font-family: 'Newsreader'; font-style: normal; font-weight: 400; font-display: swap;
  src: url(./fonts/newsreader-400-normal.woff2) format('woff2');
}
@font-face {
  font-family: 'Newsreader'; font-style: normal; font-weight: 500; font-display: swap;
  src: url(./fonts/newsreader-500-normal.woff2) format('woff2');
}
@font-face {
  font-family: 'Newsreader'; font-style: normal; font-weight: 600; font-display: swap;
  src: url(./fonts/newsreader-600-normal.woff2) format('woff2');
}
@font-face {
  font-family: 'Newsreader'; font-style: italic; font-weight: 400; font-display: swap;
  src: url(./fonts/newsreader-400-italic.woff2) format('woff2');
}

/* ---------- Jetons ---------- */
:root {
  --paper:      #F1E7D9;
  --paper-2:    #FBF5EA;
  --card:       #FDF9F2;
  --ink:        #2B211A;
  --ink-2:      #6C5B4C;
  --ink-3:      #9C8A78;
  --rule:       rgba(43, 33, 26, .13);
  --rule-soft:  rgba(43, 33, 26, .07);
  --terre:      #B04E2B;
  --terre-deep: #8E3C1F;
  --or:         #C4892A;
  --mousse:     #6E7B52;
  --shadow-lo:  0 1px 2px rgba(60,40,25,.10), 0 3px 8px rgba(60,40,25,.08);
  --shadow-hi:  0 2px 4px rgba(60,40,25,.12), 0 14px 34px rgba(60,40,25,.20);
  --grain-op:   .30;
  --display: 'Fraunces', Georgia, serif;
  --body: 'Newsreader', Georgia, serif;
  --r: 10px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --tabs-h: 62px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper:      #17120F;
    --paper-2:    #1F1813;
    --card:       #241C16;
    --ink:        #ECDFCE;
    --ink-2:      #A6907A;
    --ink-3:      #7A6754;
    --rule:       rgba(236, 223, 206, .14);
    --rule-soft:  rgba(236, 223, 206, .07);
    --terre:      #D8703D;
    --terre-deep: #B85427;
    --or:         #DFA640;
    --mousse:     #8E9C6C;
    --shadow-lo:  0 1px 2px rgba(0,0,0,.4), 0 3px 10px rgba(0,0,0,.34);
    --shadow-hi:  0 2px 6px rgba(0,0,0,.5), 0 18px 40px rgba(0,0,0,.55);
    --grain-op:   .18;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--body);
  font-size: 17px;
  line-height: 1.55;
  overscroll-behavior-y: none;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(var(--tabs-h) + var(--safe-b));
}

/* Halo de lampe : la page est éclairée par en haut */
body::before {
  content: ''; position: fixed; inset: 0; z-index: -2; pointer-events: none;
  background:
    radial-gradient(120% 70% at 50% -10%, rgba(255, 233, 196, .55), transparent 62%),
    radial-gradient(90% 60% at 88% 108%, rgba(176, 78, 43, .10), transparent 70%);
}
@media (prefers-color-scheme: dark) {
  body::before {
    background:
      radial-gradient(120% 65% at 50% -12%, rgba(216, 150, 74, .16), transparent 64%),
      radial-gradient(90% 60% at 88% 108%, rgba(216, 112, 61, .09), transparent 70%);
  }
}

/* Grain papier */
.grain {
  position: fixed; inset: 0; z-index: 9; pointer-events: none;
  opacity: var(--grain-op); mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='3'/%3E%3C/filter%3E%3Crect width='140' height='140' filter='url(%23n)' opacity='.42'/%3E%3C/svg%3E");
}
@media (prefers-color-scheme: dark) { .grain { mix-blend-mode: screen; } }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- En-tête ---------- */
.topbar {
  position: sticky; top: 0; z-index: 8;
  padding-top: var(--safe-t);
  background: var(--paper);
  background: color-mix(in srgb, var(--paper) 84%, transparent);
  backdrop-filter: saturate(1.5) blur(14px);
  -webkit-backdrop-filter: saturate(1.5) blur(14px);
  border-bottom: 1px solid var(--rule-soft);
}
.topbar__inner {
  display: flex; align-items: baseline; gap: 12px;
  padding: 14px 20px 12px; max-width: 900px; margin: 0 auto;
}
.wordmark {
  margin: 0; font-family: var(--display);
  font-size: clamp(23px, 6.2vw, 30px);
  font-weight: 600; letter-spacing: -.02em; line-height: 1;
  font-variation-settings: 'SOFT' 70, 'WONK' 1, 'opsz' 40;
}
.tally {
  margin: 0 0 0 auto; font-size: 13px; color: var(--ink-3);
  font-variant-numeric: tabular-nums; letter-spacing: .04em; text-transform: lowercase;
}

/* ---------- Vues ---------- */
.view { max-width: 900px; margin: 0 auto; padding: 0 20px; }
.view[hidden] { display: none; }

.toolbar { padding: 18px 0 14px; }

/* Champ de saisie */
.field { position: relative; }
.field__icon {
  position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
  width: 18px; height: 18px; fill: none; stroke: var(--ink-3);
  stroke-width: 1.8; stroke-linecap: round; pointer-events: none;
}
.field input {
  width: 100%; font: inherit; font-size: 16.5px; color: var(--ink);
  padding: 13px 14px 13px 42px;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: var(--r);
  box-shadow: inset 0 1px 2px rgba(60,40,25,.05);
  -webkit-appearance: none; appearance: none;
}
.field input::placeholder { color: var(--ink-3); }
.field input:focus {
  outline: none; border-color: var(--terre);
  box-shadow: 0 0 0 3px rgba(176, 78, 43, .18);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--terre) 18%, transparent);
}
.field--sm input { padding-block: 10px; font-size: 15.5px; }
.hint { margin: 8px 2px 0; font-size: 12.5px; color: var(--ink-3); font-style: italic; }

/* Puces de filtre */
.chips { display: flex; align-items: center; gap: 7px; margin-top: 12px; }
.chips__spacer { flex: 1; }
.chip {
  font: inherit; font-size: 13.5px; letter-spacing: .01em;
  padding: 5px 12px; border-radius: 999px; cursor: pointer;
  color: var(--ink-2); background: transparent;
  border: 1px solid var(--rule);
  transition: background .18s, color .18s, border-color .18s;
}
.chip--on {
  background: var(--terre); border-color: var(--terre);
  color: #FDF9F2;
}
@media (prefers-color-scheme: dark) { .chip--on { color: #17120F; } }
.sort select {
  font: inherit; font-size: 13.5px; color: var(--ink-2);
  background: transparent; border: 1px solid var(--rule);
  border-radius: 999px; padding: 5px 10px; -webkit-appearance: none; appearance: none;
}

/* ---------- Rayonnage ---------- */
.shelf {
  display: grid;
  align-items: start;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  gap: 26px 16px;
  padding: 6px 0 40px;
}
@media (min-width: 560px) {
  .shelf { grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 32px 22px; }
}

.book {
  position: relative; display: block; width: 100%;
  font: inherit; color: inherit;
  padding: 0; border: 0; background: none; cursor: pointer;
  text-align: left; -webkit-tap-highlight-color: transparent;
  animation: rise .5s cubic-bezier(.2,.7,.3,1) backwards;
}
@keyframes rise { from { opacity: 0; transform: translateY(10px); } }

.book__cover {
  display: block;
  position: relative; width: 100%; aspect-ratio: 2 / 3;
  border-radius: 3px 5px 5px 3px;
  overflow: hidden; background: var(--paper-2);
  box-shadow: var(--shadow-lo);
  transform-origin: bottom center;
  transition: transform .32s cubic-bezier(.2,.8,.3,1), box-shadow .32s;
}
.book__cover img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* Tranche + reliure : ce qui fait qu'une vignette devient un livre */
.book__cover::before {
  content: ''; position: absolute; inset: 0; pointer-events: none; z-index: 2;
  background:
    linear-gradient(90deg, rgba(0,0,0,.34) 0, rgba(0,0,0,.10) 3px, rgba(255,255,255,.16) 6px, transparent 11px),
    linear-gradient(270deg, rgba(0,0,0,.16) 0, transparent 5px);
}
.book__cover::after {
  content: ''; position: absolute; inset: 0; pointer-events: none; z-index: 3;
  background: linear-gradient(115deg, rgba(255,246,224,.30) 0%, transparent 34%);
  opacity: .8;
}
.book:active .book__cover { transform: translateY(-2px) scale(.985); }
@media (hover: hover) {
  .book:hover .book__cover { transform: translateY(-7px) rotate(-.9deg); box-shadow: var(--shadow-hi); }
}

/* Dos de secours quand aucune couverture n'existe */
.book__fallback {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  justify-content: center; gap: 8px; padding: 14px 12px;
  background:
    linear-gradient(160deg, var(--terre-deep), var(--terre) 55%, var(--terre-deep));
  color: #FBEFDF;
}
.book__fallback b {
  font-family: var(--display); font-size: 14px; font-weight: 600; line-height: 1.2;
  font-variation-settings: 'SOFT' 60, 'opsz' 20;
  display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden;
}
.book__fallback i { font-size: 11.5px; opacity: .72; font-style: normal; letter-spacing: .04em; }

/* La planche : une ligne de rayonnage sous chaque couverture */
.book__meta {
  display: block; padding: 9px 2px 0; position: relative;
  height: 79px; overflow: hidden;
}
.book__meta::before {
  content: ''; position: absolute; top: 0; left: -7%; right: -7%; height: 1px;
  background: linear-gradient(to right, transparent, var(--rule) 14%, var(--rule) 86%, transparent);
}
.book__title {
  font-family: var(--display); font-size: 13.5px; font-weight: 600; line-height: 1.25;
  font-variation-settings: 'SOFT' 60, 'opsz' 18;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.book__author {
  display: block;
  font-size: 12px; color: var(--ink-3); margin-top: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.book__stars { display: block; margin-top: 5px; line-height: 0; height: 13px; }

/* ---------- Étoiles (demi-étoiles par superposition) ---------- */
.stars { position: relative; display: inline-block; line-height: 0; }
.stars__lay { display: flex; gap: var(--gap, 2px); }
.stars__lay svg { width: var(--s, 13px); height: var(--s, 13px); display: block; flex: none; }
.stars__clip { position: absolute; top: 0; left: 0; bottom: 0; overflow: hidden; width: var(--p, 0%); }
.stars .void { fill: var(--rule); }
.stars .gold { fill: var(--or); }

/* Curseur d'étoiles interactif */
.rater {
  --s: 33px; --gap: 6px;
  padding: 8px 6px; margin: -8px -6px; border-radius: 10px;
  cursor: pointer; touch-action: none; -webkit-tap-highlight-color: transparent;
}
.rater:focus-visible { outline: 2px solid var(--terre); outline-offset: 0; }
.rater .stars__lay svg { transition: transform .18s cubic-bezier(.3,1.4,.5,1); }
.rater[data-live="1"] .stars__lay svg { transform: scale(1.08); }

/* ---------- Résultats de recherche ---------- */
.results { padding-bottom: 34px; }
.res {
  display: grid; grid-template-columns: 54px 1fr auto; gap: 14px; align-items: center;
  padding: 13px 0; border-bottom: 1px solid var(--rule-soft);
  animation: rise .4s cubic-bezier(.2,.7,.3,1) backwards;
}
.res__thumb {
  display: block;
  width: 54px; aspect-ratio: 2/3; border-radius: 2px 4px 4px 2px; overflow: hidden;
  background: var(--paper-2); box-shadow: var(--shadow-lo);
}
.res__thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.res__thumb .book__fallback b { font-size: 10px; -webkit-line-clamp: 3; }
.res__thumb .book__fallback { padding: 7px 6px; }
.res__thumb .book__fallback i { display: none; }
.res__t {
  font-family: var(--display); font-size: 16px; font-weight: 600; line-height: 1.25;
  font-variation-settings: 'SOFT' 60, 'opsz' 24;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.res__a { display: block; font-size: 13.5px; color: var(--ink-2); margin-top: 2px; }
.res__y { display: block; font-size: 12px; color: var(--ink-3); margin-top: 1px; letter-spacing: .02em; }

.add {
  width: 40px; height: 40px; border-radius: 50%; cursor: pointer;
  border: 1px solid var(--terre); background: transparent; color: var(--terre);
  display: grid; place-items: center; transition: background .2s, color .2s, transform .2s;
}
.add svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.add:active { transform: scale(.9); }
.add[data-in="1"] {
  background: var(--mousse); border-color: var(--mousse); color: var(--card); cursor: default;
}

/* ---------- États ---------- */
.state { padding: 26px 4px 40px; color: var(--ink-2); font-style: italic; text-align: center; }
.state:empty { display: none; }

.empty { text-align: center; padding: 54px 24px 70px; }
.empty__mark svg {
  width: 74px; height: 74px; fill: none; stroke: var(--ink-3);
  stroke-width: 2; stroke-linejoin: round; opacity: .5;
}
.empty h2 {
  font-family: var(--display); font-weight: 600; font-size: 22px; margin: 16px 0 6px;
  font-variation-settings: 'SOFT' 70, 'WONK' 1, 'opsz' 30;
}
.empty p { margin: 0 auto 22px; color: var(--ink-2); max-width: 30ch; }

/* ---------- Sauvegarde ---------- */
.backup {
  display: flex; align-items: center; justify-content: center; gap: 9px;
  padding: 6px 0 42px; font-size: 12.5px; color: var(--ink-3);
}
.linkbtn {
  font: inherit; font-size: 12.5px; background: none; border: 0; cursor: pointer;
  color: var(--ink-3); text-decoration: underline; text-underline-offset: 3px;
  text-decoration-color: var(--rule); padding: 4px 2px;
}
.linkbtn:hover { color: var(--terre); }

/* ---------- Boutons ---------- */
.btn {
  font: inherit; font-size: 15.5px; cursor: pointer; border-radius: 999px;
  padding: 11px 22px; border: 1px solid var(--rule); background: transparent; color: var(--ink);
  transition: transform .18s, background .2s, border-color .2s;
}
.btn:active { transform: scale(.97); }
.btn--primary { background: var(--terre); border-color: var(--terre); color: #FDF9F2; }
@media (prefers-color-scheme: dark) { .btn--primary { color: #17120F; } }
.btn--ghost { color: var(--ink-2); }
.btn--danger { color: var(--terre-deep); border-color: var(--rule); border-color: color-mix(in srgb, var(--terre) 40%, transparent); }
@media (prefers-color-scheme: dark) { .btn--danger { color: var(--terre); } }

/* ---------- Onglets ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 8;
  display: grid; grid-template-columns: 1fr 1fr;
  padding-bottom: var(--safe-b);
  background: var(--paper);
  background: color-mix(in srgb, var(--paper) 86%, transparent);
  backdrop-filter: saturate(1.5) blur(16px);
  -webkit-backdrop-filter: saturate(1.5) blur(16px);
  border-top: 1px solid var(--rule);
}
.tab {
  font: inherit; border: 0; background: none; cursor: pointer; color: var(--ink-3);
  height: var(--tabs-h); display: grid; place-items: center; gap: 1px;
  -webkit-tap-highlight-color: transparent; transition: color .2s;
}
.tab svg {
  width: 23px; height: 23px; fill: none; stroke: currentColor;
  stroke-width: 1.7; stroke-linejoin: round; stroke-linecap: round;
}
.tab span { font-size: 11px; letter-spacing: .05em; }
.tab--on { color: var(--terre); }

/* ---------- Fiche livre ---------- */
.sheet { position: fixed; inset: 0; z-index: 20; }
.sheet[hidden] { display: none; }
.sheet__scrim {
  position: absolute; inset: 0; background: rgba(28, 20, 14, .45);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  animation: fade .3s ease backwards;
}
@keyframes fade { from { opacity: 0; } }
.sheet__panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 93dvh;
  display: flex; flex-direction: column;
  background: var(--paper-2);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -8px 50px rgba(30,18,10,.4);
  animation: slide .42s cubic-bezier(.2,.8,.25,1) backwards;
}
@keyframes slide { from { transform: translateY(100%); } }
@media (min-width: 720px) {
  .sheet__panel {
    left: 50%; bottom: auto; top: 50%; transform: translate(-50%, -50%);
    width: min(560px, 92vw); border-radius: 16px; max-height: 88dvh;
  }
  @keyframes slide { from { transform: translate(-50%, -46%); opacity: 0; } }
}
.sheet__grip {
  width: 42px; height: 5px; border: 0; border-radius: 3px; background: var(--rule);
  margin: 10px auto 0; padding: 0; cursor: pointer; flex: none;
}
.sheet__body {
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 18px 24px calc(28px + var(--safe-b));
}

.fiche__head { display: grid; grid-template-columns: 92px 1fr; gap: 18px; align-items: start; }
.fiche__cover {
  display: block;
  width: 92px; aspect-ratio: 2/3; border-radius: 2px 5px 5px 2px; overflow: hidden;
  background: var(--paper); box-shadow: var(--shadow-hi); position: relative;
}
.fiche__cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.fiche__title {
  font-family: var(--display); font-size: 21px; font-weight: 600; line-height: 1.2; margin: 2px 0 0;
  font-variation-settings: 'SOFT' 70, 'WONK' 1, 'opsz' 32; letter-spacing: -.01em;
}
.fiche__author { margin: 5px 0 0; color: var(--ink-2); font-style: italic; }
.fiche__pub { margin: 6px 0 0; font-size: 12.5px; color: var(--ink-3); letter-spacing: .02em; }

.rule {
  height: 1px; margin: 22px 0; border: 0;
  background: linear-gradient(to right, transparent, var(--rule) 15%, var(--rule) 85%, transparent);
}

.label {
  font-size: 11.5px; letter-spacing: .13em; text-transform: uppercase;
  color: var(--ink-3); margin: 0 0 10px;
}

.rate-row { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.rate-val {
  font-family: var(--display); font-size: 21px; font-weight: 600;
  font-variation-settings: 'SOFT' 70, 'opsz' 28;
  font-variant-numeric: tabular-nums; color: var(--or);
}
.rate-val small { font-size: 13px; color: var(--ink-3); font-weight: 400; }
.rate-clear {
  font: inherit; font-size: 12.5px; background: none; border: 0; cursor: pointer;
  color: var(--ink-3); text-decoration: underline; text-underline-offset: 3px; margin-left: auto;
}

/* Carnet d'observations — fiche cartonnée réglée */
.note {
  position: relative; border-radius: 4px; background: var(--card);
  border: 1px solid var(--rule); box-shadow: var(--shadow-lo);
  padding: 14px 16px 14px 46px;
}
.note::before {
  content: ''; position: absolute; top: 0; bottom: 0; left: 30px;
  width: 1px; background: rgba(176, 78, 43, .38);
  background: color-mix(in srgb, var(--terre) 38%, transparent);
}
.note textarea {
  width: 100%; min-height: 116px; resize: vertical; font: inherit;
  font-size: 16px; font-style: italic; line-height: 27px; color: var(--ink);
  background: repeating-linear-gradient(
    to bottom, transparent 0, transparent 26px, var(--rule-soft) 26px, var(--rule-soft) 27px);
  border: 0; outline: none; display: block; padding: 0;
}
.note textarea::placeholder { color: var(--ink-3); }
.note__saved {
  position: absolute; right: 12px; bottom: 8px; font-size: 11px; color: var(--mousse);
  letter-spacing: .06em; opacity: 0; transition: opacity .3s;
}
.note__saved[data-on="1"] { opacity: 1; }

.fiche__actions { display: flex; gap: 10px; margin-top: 24px; }
.fiche__actions .btn { flex: 1; }

/* ---------- Astuce & toast ---------- */
.tip {
  position: fixed; left: 14px; right: 14px; bottom: calc(var(--tabs-h) + var(--safe-b) + 12px);
  z-index: 12; display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; border-radius: 12px;
  background: var(--ink); color: var(--paper-2);
  box-shadow: var(--shadow-hi); font-size: 14px;
  animation: rise .5s cubic-bezier(.2,.8,.3,1) backwards;
}
.tip[hidden] { display: none; }
.tip p { margin: 0; }
.tip__share { font-size: 15px; }
.tip__x {
  margin-left: auto; background: none; border: 0; color: inherit;
  font-size: 22px; line-height: 1; cursor: pointer; padding: 0 4px; opacity: .65;
}

.toast {
  position: fixed; left: 50%; transform: translate(-50%, 16px);
  bottom: calc(var(--tabs-h) + var(--safe-b) + 16px); z-index: 30;
  padding: 11px 20px; border-radius: 999px;
  background: var(--ink); color: var(--paper-2); font-size: 14.5px;
  box-shadow: var(--shadow-hi); opacity: 0; pointer-events: none;
  transition: opacity .28s, transform .28s;
}
.toast[data-on="1"] { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ---------- Bandeau du mode démo ---------- */
.demoflag {
  position: fixed; z-index: 14; left: 50%; transform: translateX(-50%);
  top: calc(var(--safe-t) + 62px);
  padding: 4px 12px; border-radius: 999px;
  font-size: 11px; letter-spacing: .09em; text-transform: uppercase;
  background: var(--mousse); color: #FBF5EA;
  box-shadow: var(--shadow-lo); pointer-events: none;
}
