/* Clefdoc — jetons de design, remise à zéro et composants partagés.
   Aucune classe propre à une vue ici (celles-ci vivent dans css/<vue>.css). */

/* ------------------------------------------------------------------ jetons */

:root,
[data-theme="dark"] {
  color-scheme: dark;

  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #222633;
  --border: #2c3140;

  --text: #f2f4f8;
  --text-dim: #9aa3b2;

  --accent: #4f8cff;
  --accent-press: #3b74e6;
  --on-accent: #ffffff;

  --danger: #ff5d5d;
  --ok: #3ddc97;
  --warn: #ffb84d;

  --radius: 14px;
  --radius-sm: 10px;
  --tap: 48px;

  /* Zones sûres (encoche, Dynamic Island, barre d'état, barre de gestes). App Android bord à bord :
     Capacitor (SystemBars, insetsHandling « css ») pose --safe-area-inset-* sur <html> — valeurs exactes
     avec une WebView ≥ 140, 0 avec une WebView plus ancienne (qu'il décale alors lui-même) et 0 en bas
     quand le clavier est ouvert. Elles priment donc sur env(), inexact dans ces WebView anciennes.
     Web et iOS : variables absentes, env() (viewport-fit=cover). */
  --safe-t: var(--safe-area-inset-top, env(safe-area-inset-top, 0px));
  --safe-r: var(--safe-area-inset-right, env(safe-area-inset-right, 0px));
  --safe-b: var(--safe-area-inset-bottom, env(safe-area-inset-bottom, 0px));
  --safe-l: var(--safe-area-inset-left, env(safe-area-inset-left, 0px));

  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --shadow: 0 8px 30px rgba(0, 0, 0, .35);
  --ease: cubic-bezier(.2, .8, .2, 1);

  --scrim: rgba(6, 8, 12, .62);
}

/* Thème clair du système ; les vues qui forcent [data-theme="dark"] gardent les jetons sombres. */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;

    --bg: #f5f6f8;
    --surface: #ffffff;
    --surface-2: #eceff4;
    --border: #d8dde6;

    --text: #11151c;
    --text-dim: #5c6675;

    --accent: #2f6fe4;
    --accent-press: #1f55bd;
    --on-accent: #ffffff;

    --danger: #d92b2b;
    --ok: #0f8f5f;
    --warn: #a96a00;

    --shadow: 0 8px 30px rgba(17, 21, 28, .14);
    --scrim: rgba(17, 21, 28, .42);
  }
}

/* ------------------------------------------------------------ remise à zéro */

*,
*::before,
*::after {
  box-sizing: border-box;
}

* {
  margin: 0;
}

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

body {
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
  touch-action: manipulation;
}

html.modal-open,
html.modal-open body {
  overflow: hidden;
}

img,
svg,
video,
canvas {
  display: block;
  max-width: 100%;
}

svg.icon {
  flex: none;
  max-width: none;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

button {
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

input,
select,
textarea {
  font-size: 16px;   /* pas de zoom automatique sur iOS */
}

h1,
h2,
h3 {
  font-size: inherit;
  font-weight: 650;
  line-height: 1.25;
}

ul,
ol {
  padding: 0;
  list-style: none;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

[tabindex="-1"]:focus {
  outline: none;   /* focus programmatique (titre d'une vue) : pas d'anneau parasite */
}

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

  .spinner {
    animation-duration: 1.2s !important;
    animation-iteration-count: infinite !important;
  }
}

/* ------------------------------------------------------------- utilitaires */

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

.safe-top {
  padding-top: var(--safe-t);
}

.safe-bottom {
  padding-bottom: var(--safe-b);
}

.safe-x {
  padding-left: var(--safe-l);
  padding-right: var(--safe-r);
}

/* -------------------------------------------------------------- structure */

#app {
  min-height: 100vh;
  min-height: 100dvh;
}

.view {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: calc(var(--tap) + 8px);
  padding: 4px calc(8px + var(--safe-r)) 4px calc(8px + var(--safe-l));
  padding-top: calc(4px + var(--safe-t));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(140%) blur(12px);
  -webkit-backdrop-filter: saturate(140%) blur(12px);
  border-bottom: 1px solid var(--border);
}

@supports not (background: color-mix(in srgb, red 50%, blue)) {
  .topbar {
    background: var(--bg);
  }
}

.topbar__start,
.topbar__end {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: none;
}

.topbar__title {
  flex: 1 1 auto;
  min-width: 0;
  padding: 0 4px;
  font-size: 18px;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bottombar {
  position: sticky;
  bottom: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px calc(12px + var(--safe-r)) calc(8px + var(--safe-b)) calc(12px + var(--safe-l));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.bottombar > .btn {
  flex: 1 1 0;
}

/* --------------------------------------------------------------- boutons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 0 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  text-decoration: none;
  transition: background-color .15s var(--ease), transform .12s var(--ease), opacity .15s var(--ease);
}

.btn:active {
  transform: scale(.975);
}

.btn--primary {
  background: var(--accent);
  border-color: transparent;
  color: var(--on-accent);
}

.btn--primary:active {
  background: var(--accent-press);
}

.btn--ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text);
}

.btn--ghost:active {
  background: var(--surface-2);
}

.btn--danger {
  background: var(--danger);
  border-color: transparent;
  color: #fff;
}

.btn--block {
  display: flex;
  width: 100%;
}

.btn[disabled],
.btn[aria-disabled="true"] {
  opacity: .45;
  pointer-events: none;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: var(--tap);
  height: var(--tap);
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text);
  transition: background-color .15s var(--ease), transform .12s var(--ease);
}

.icon-btn:active {
  background: var(--surface-2);
  transform: scale(.94);
}

.icon-btn[disabled] {
  opacity: .4;
  pointer-events: none;
}

.icon-btn--danger {
  color: var(--danger);
}

.fab {
  position: fixed;
  right: calc(16px + var(--safe-r));
  bottom: calc(16px + var(--safe-b));
  z-index: 30;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 56px;
  padding: 0 22px;
  border: 0;
  border-radius: 28px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 16px;
  font-weight: 650;
  box-shadow: var(--shadow);
  transition: background-color .15s var(--ease), transform .12s var(--ease);
}

.fab:active {
  background: var(--accent-press);
  transform: scale(.96);
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

.chip--active {
  background: var(--accent);
  border-color: transparent;
  color: var(--on-accent);
}

/* --------------------------------------------------- états et indicateurs */

.empty-state {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 40px 24px;
  text-align: center;
  color: var(--text-dim);
}

/* Illustration seulement (enfant direct) : les icônes des boutons gardent leur taille. */
.empty-state > .icon {
  width: 56px;
  height: 56px;
  opacity: .55;
}

.empty-state h1,
.empty-state h2 {
  font-size: 19px;
  color: var(--text);
}

.empty-state p {
  max-width: 34ch;
  font-size: 15px;
}

.spinner {
  flex: none;
  width: 24px;
  height: 24px;
  border: 2.5px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: scandoc-spin .7s linear infinite;
}

@keyframes scandoc-spin {
  to {
    transform: rotate(360deg);
  }
}

.progress {
  width: 100%;
  height: 6px;
  overflow: hidden;
  border-radius: 999px;
  background: var(--surface-2);
}

.progress__bar {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--accent);
  transition: width .2s var(--ease);
}

/* ---------------------------------------------------------------- toasts */

.toast-layer {
  position: fixed;
  left: calc(12px + var(--safe-l));
  right: calc(12px + var(--safe-r));
  bottom: calc(16px + var(--safe-b));
  z-index: 2000;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  width: min(100%, 420px);
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  font-size: 15px;
  box-shadow: var(--shadow);
  pointer-events: auto;
  opacity: 0;
  transform: translateY(10px);
  transition: opacity .2s var(--ease), transform .2s var(--ease);
}

.toast.is-open {
  opacity: 1;
  transform: translateY(0);
}

.toast.is-closing {
  opacity: 0;
  transform: translateY(6px);
}

.toast__text {
  flex: 1 1 auto;
  min-width: 0;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.toast--success .icon {
  color: var(--ok);
}

.toast--error {
  border-color: color-mix(in srgb, var(--danger) 45%, var(--border));
}

.toast--error .icon {
  color: var(--danger);
}

.toast--info .icon {
  color: var(--text-dim);
}

/* --------------------------------------------------------------- modales */

dialog.modal {
  display: flex;   /* prime sur display:none du navigateur : l'animation de sortie reste visible */
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  overflow: visible;
}

dialog.modal::backdrop {
  background: transparent;   /* le voile est un vrai élément (.modal__backdrop) */
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s var(--ease);
}

.modal.is-open {
  opacity: 1;
  pointer-events: auto;
}

.modal.is-closing {
  opacity: 0;
  pointer-events: none;
}

.modal--covered {
  pointer-events: none;   /* un seul niveau interactif à la fois */
}

.modal--covered .modal__backdrop {
  opacity: 0;             /* un seul voile assombri */
}

.modal__backdrop {
  position: absolute;
  inset: 0;
  background: var(--scrim);
  transition: opacity .2s var(--ease);
}

.modal__panel {
  position: relative;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.modal__panel:focus {
  outline: none;
}

/* --- boîte de dialogue --- */

.modal--dialog {
  padding: calc(16px + var(--safe-t)) calc(16px + var(--safe-r)) calc(16px + var(--safe-b)) calc(16px + var(--safe-l));
}

.modal--dialog .modal__panel {
  width: min(100%, 420px);
  max-height: 100%;
  gap: 14px;
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-y: auto;
  overscroll-behavior: contain;
  transform: scale(.96);
  transition: transform .2s var(--ease);
}

.modal--dialog.is-open .modal__panel {
  transform: scale(1);
}

.dialog__form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.dialog__title {
  font-size: 18px;
}

.dialog__message {
  color: var(--text-dim);
  word-break: break-word;   /* repli iOS < 15.4 (sans overflow-wrap: anywhere) */
  overflow-wrap: anywhere;
}

.dialog__input {
  width: 100%;
  min-height: var(--tap);
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
}

.dialog__input:focus-visible {
  border-color: var(--accent);
}

.dialog__actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

.dialog__actions .btn {
  flex: 1 1 auto;
  min-width: 120px;
}

/* --- feuille basse --- */

.modal--sheet {
  align-items: flex-end;
}

.modal--sheet .modal__panel {
  width: 100%;
  max-width: 560px;
  max-height: 86vh;              /* iOS < 15.4 (sans dvh) : sinon aucune borne, l'en-tête sortirait de l'écran */
  max-height: min(86vh, 86dvh);
  padding-bottom: calc(10px + var(--safe-b));
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  border-top: 1px solid var(--border);
  transform: translateY(100%);
  transition: transform .26s var(--ease);
}

.modal--sheet.is-open .modal__panel {
  transform: translateY(0);
}

.modal--sheet .modal__panel.is-dragging {
  transition: none;
}

.sheet__header {
  flex: none;
  padding: 8px 20px 4px;
  touch-action: none;   /* le glisser vers le bas referme la feuille */
  cursor: grab;
}

.sheet__grabber {
  width: 42px;
  height: 4px;
  margin: 0 auto 10px;
  border-radius: 999px;
  background: var(--border);
}

/* Le titre est souvent celui du document : très long ou sans espace, il se coupe (2 lignes au plus). */
.sheet__title {
  display: -webkit-box;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  font-size: 17px;
  text-align: center;
  margin-bottom: 6px;   /* pas de padding : il laisserait voir le haut de la 3e ligne coupée */
  word-break: break-word;
  overflow-wrap: anywhere;
}

.sheet__list {
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overscroll-behavior: contain;   /* le défilement ne passe pas à la page derrière */
  padding: 4px 8px 6px;
  -webkit-overflow-scrolling: touch;
}

.sheet__item {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  min-height: var(--tap);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  color: var(--text);
  text-align: left;
  transition: background-color .15s var(--ease);
}

.sheet__item:active {
  background: var(--surface-2);
}

.sheet__item[disabled] {
  opacity: .42;
  pointer-events: none;
}

.sheet__item--danger {
  color: var(--danger);
}

.sheet__item-icon {
  display: flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 26px;
  color: inherit;
}

.sheet__item-body {
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: 2px;
}

.sheet__item-label {
  font-weight: 600;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.sheet__item-hint {
  font-size: 13px;
  color: var(--text-dim);
  word-break: break-word;
  overflow-wrap: anywhere;
}

.sheet__content {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px 20px 8px;
  -webkit-overflow-scrolling: touch;
}

/* --- voile d'attente --- */

.modal--busy .modal__backdrop {
  background: color-mix(in srgb, var(--bg) 72%, transparent);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}

.modal--busy .modal__panel {
  align-items: center;
  gap: 14px;
  width: min(84%, 320px);
  padding: 26px 22px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-align: center;
}

.modal--busy .spinner {
  width: 30px;
  height: 30px;
}

.busy__message {
  font-size: 15px;
  color: var(--text);
  word-break: break-word;
  overflow-wrap: anywhere;
}

/* Safari < 16.2 : color-mix() contenant var() devient invalide au calcul → fond transparent,
   bordure currentColor. Placé APRÈS les règles d'origine pour les remplacer. */
@supports not (background: color-mix(in srgb, red 50%, blue)) {
  .toast--error {
    border-color: var(--danger);
  }

  .modal--busy .modal__backdrop {
    background: var(--scrim);
  }
}

/* ------------------------------------------------------ coquille (app.js) */

/* Bandeau de mise à jour du service worker : version web uniquement (jamais affiché dans l'app native). */
.app-banner {
  position: fixed;
  left: calc(12px + var(--safe-l));
  right: calc(12px + var(--safe-r));
  bottom: calc(12px + var(--safe-b));
  z-index: 900;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  box-shadow: var(--shadow);
}

.app-banner__text {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 15px;
}

.app-banner .btn {
  flex: none;
  min-height: 40px;
  padding: 0 14px;
  white-space: nowrap;   /* « Neu laden » : c'est le texte du bandeau qui passe à la ligne, pas le bouton */
}

.app-banner .icon-btn {
  margin: -8px -8px -8px -6px;   /* cible de 48 px sans épaissir le bandeau */
}

.app-fallback {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: calc(32px + var(--safe-t)) calc(24px + var(--safe-r)) calc(32px + var(--safe-b)) calc(24px + var(--safe-l));
  text-align: center;
}

.app-fallback__title {
  font-size: 20px;
}

.app-fallback__text {
  max-width: 40ch;
  color: var(--text-dim);
  word-break: break-word;
  overflow-wrap: anywhere;
}

.app-fallback .icon {
  width: 52px;
  height: 52px;
  color: var(--warn);
}

.app-fallback .btn {
  min-width: 220px;
}
