:root {
  --teal: #0f6b66;
  --teal-dark: #0a4a47;
  --teal-soft: #e3f1ef;
  --clay: #b5502f;
  --sand: #f6efe3;
  --ink: #1f2a2a;
  --muted: #5b6766;
  --line: #dfe7e5;
  --bg: #f4f6f5;
  --card: #ffffff;
  --danger: #b3261e;
  --radius: 14px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Noto Sans Arabic", Tahoma, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
h1 { font-size: 1.6rem; margin: 0 0 4px; line-height: 1.25; }
h2 { font-size: 1.25rem; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
button { font: inherit; cursor: pointer; }
input {
  font: inherit; width: 100%; padding: 12px 14px; border: 1px solid #cfdad7; border-radius: 10px;
  background: #fff; color: var(--ink);
}
input:focus-visible, button:focus-visible { outline: 3px solid #8cc7c1; outline-offset: 2px; }

.btn { border: 1px solid var(--line); background: #fff; color: var(--ink); padding: 10px 18px; border-radius: 10px; font-weight: 600; }
.btn.primary { background: var(--teal); border-color: var(--teal); color: #fff; }
.btn.primary:hover { background: var(--teal-dark); }
.btn.block { width: 100%; padding: 13px; font-size: 1.05rem; }
.btn:disabled { opacity: .6; cursor: default; }
.link-btn { background: none; border: none; color: var(--teal); font-weight: 600; padding: 10px; margin-top: 6px; }
.ghost-btn { background: none; border: none; color: #fff; padding: 8px 10px; border-radius: 8px; font-weight: 600; font-size: .92rem; }
.ghost-btn:hover { background: rgba(255,255,255,.12); }

/* ---------- Connexion ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; background: linear-gradient(160deg, var(--teal-dark), var(--teal) 55%, #2f8a83); }
.login-card { width: 100%; max-width: 400px; background: #fff; border-radius: 20px; padding: 28px 24px 18px; box-shadow: 0 20px 50px rgba(0,0,0,.18); text-align: center; }
.login-logo { border-radius: 18px; margin-bottom: 8px; }
.login-card label { display: block; text-align: start; font-weight: 600; font-size: .92rem; margin: 14px 0 0; }
.login-card label span { display: block; margin-bottom: 6px; }
.login-card .btn { margin-top: 18px; }
.error { color: var(--danger); min-height: 1.2em; margin: 10px 0 0; font-size: .92rem; }

/* ---------- Barre ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: var(--teal-dark); color: #fff; padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; white-space: nowrap; }
.brand img { border-radius: 9px; }
.top-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; }

/* ---------- Bibliothèque ---------- */
.library { max-width: 1100px; margin: 0 auto; padding: 24px 16px 40px; }
.lib-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 14px; }
.lib-head input { max-width: 320px; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 18px; }
.chip { border: 1px solid #cfdad7; background: #fff; color: #3d4a49; padding: 7px 16px; border-radius: 99px; font-weight: 600; font-size: .92rem; }
.chip[aria-pressed="true"] { background: var(--teal-soft); border-color: var(--teal); color: var(--teal-dark); }
.books { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 14px; }
.book {
  display: flex; gap: 14px; align-items: stretch; text-align: start; width: 100%;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; color: inherit;
  transition: box-shadow .15s, transform .15s;
}
.book:hover { box-shadow: 0 8px 24px rgba(16, 60, 56, .12); transform: translateY(-1px); }
.spine { flex: none; width: 54px; border-radius: 8px; display: grid; place-items: center; color: #fff; font-weight: 800; font-size: .8rem; letter-spacing: .5px; writing-mode: vertical-rl; transform: rotate(180deg); padding: 8px 0; }
.book-info { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.book-title { font-weight: 700; font-size: 1.02rem; line-height: 1.35; }
.book-meta { color: var(--muted); font-size: .85rem; margin-top: auto; }
.badge { display: inline-block; font-size: .75rem; font-weight: 700; padding: 2px 9px; border-radius: 99px; background: var(--teal-soft); color: var(--teal-dark); width: fit-content; }
.empty { grid-column: 1 / -1; background: #fff; border: 1px dashed #cfdad7; border-radius: var(--radius); padding: 32px; text-align: center; color: var(--muted); }
.install-hint { margin-top: 26px; text-align: center; }

/* ---------- Lecteur ---------- */
.reader { position: fixed; inset: 0; display: flex; flex-direction: column; background: #3a4342; z-index: 10; }
.reader-bar {
  flex: none; display: flex; align-items: center; gap: 8px; background: var(--teal-dark); color: #fff;
  padding: 6px max(8px, env(safe-area-inset-right)) 6px max(8px, env(safe-area-inset-left));
}
.reader-title { flex: 1; min-width: 0; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader-tools { display: flex; align-items: center; gap: 4px; }
.page-ind { font-variant-numeric: tabular-nums; font-size: .9rem; opacity: .9; padding: 0 6px; white-space: nowrap; }
.icon-btn { width: 40px; height: 40px; border-radius: 10px; border: none; background: rgba(255,255,255,.1); color: #fff; font-size: 1.4rem; line-height: 1; display: grid; place-items: center; }
.icon-btn:hover { background: rgba(255,255,255,.2); }
[dir="rtl"] .icon-btn svg { transform: scaleX(-1); }
.pages { flex: 1; overflow: auto; padding: 14px 8px 40px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.page {
  position: relative; margin: 0 auto 14px; background: #fff; box-shadow: 0 4px 18px rgba(0,0,0,.35);
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
.page canvas { display: block; width: 100%; height: 100%; pointer-events: none; }
/* Filigrane à l'écran (en plus de celui imprimé dans le PDF) */
.page .wm {
  position: absolute; inset: 0; pointer-events: none; overflow: hidden;
  background-image: var(--wm);
  background-repeat: repeat; background-size: 440px 260px; opacity: 1;
}
.reader-status { position: absolute; inset: 56px 0 0; display: grid; place-items: center; color: #fff; text-align: center; padding: 24px; pointer-events: none; }
.reader-status .box { background: rgba(0,0,0,.45); border-radius: 14px; padding: 18px 22px; max-width: 420px; pointer-events: auto; }
.spinner { width: 34px; height: 34px; border: 3px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; margin: 0 auto 12px; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Appareils ---------- */
dialog { border: none; border-radius: 18px; padding: 0; width: min(520px, calc(100vw - 32px)); box-shadow: 0 24px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(10, 30, 28, .45); }
.dialog-body { padding: 22px; }
.device { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.device:last-child { border-bottom: none; }
.device b { display: block; }
.device .btn { flex: none; padding: 7px 12px; font-size: .88rem; }
.actions { display: flex; justify-content: flex-end; margin-top: 16px; }
.this-device { font-size: .75rem; font-weight: 700; color: var(--teal); }

#toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translate(-50%, 30px);
  background: #1f2a2a; color: #fff; padding: 11px 18px; border-radius: 12px; opacity: 0; transition: .25s; z-index: 50;
  max-width: calc(100vw - 32px); text-align: center; pointer-events: none;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: var(--danger); }

/* Impression désactivée : les guides se lisent uniquement dans l'application */
@media print {
  body * { display: none !important; }
  body::after { content: "Impression non autorisée — ARIB DIETS"; display: block; padding: 40px; font-size: 20px; }
}

@media (max-width: 560px) {
  .brand span { display: none; }
  h1 { font-size: 1.35rem; }
  .lib-head input { max-width: none; }
  .books { grid-template-columns: 1fr; }
  .ghost-btn { padding: 7px 8px; font-size: .86rem; }
}

/* Inscription */
.switch-line { margin: 14px 0 0; font-size: .92rem; color: var(--muted, #5f6b6a); }
.link-btn.inline-link { padding: 4px 2px; margin: 0; font-size: .92rem; text-decoration: underline; text-underline-offset: 3px; }
#register-done .btn { margin-top: 20px; }
