/* ============================================================================
   IDSelect - Feuille de style unique.

   Elle sert DEUX consommateurs :
     - la SPA (/, /dashboard, /edit) ;
     - la page publique rendue par l'Edge Function SSR (/c/<slug>).

   Conséquence : les couleurs des themes sont définies ICI et nulle part
   ailleurs. Toute modification de palette se fait dans les blocs
   `[data-theme="..."]`, et doit rester alignée avec :
     - supabase/functions/_shared/themes.ts
     - la contrainte SQL `cards_theme_check`

   Approche : mobile-first, propriétés logiques (margin-inline-start...)
   pour que le passage en RTL (arabe) soit gratuit, contrastes AAA sur le
   texte principal.
   ========================================================================= */

/* ---------------------------------------------------------------- tokens -- */
:root {
  --font-sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto,
    "Helvetica Neue", "Noto Sans Arabic", "Segoe UI Arabic", Arial, sans-serif;

  /* Chrome de l'application */
  --bg-app: #f6f7f9;
  --bg-surface: #ffffff;
  --fg-app: #14181f;
  --fg-muted: #5b6472;
  --border: #dfe3e8;
  --accent: #1f6feb;
  --accent-fg: #ffffff;
  --accent-ink: #0b4bb8;
  --danger: #b42318;
  --danger-bg: #fef3f2;
  --success: #067647;
  --success-bg: #ecfdf3;
  --radius: 12px;
  --radius-lg: 20px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 8px 24px rgb(16 24 40 / 8%);

  /* Reutilisees par les boutons de la carte publique */
  --card-gap: 0.6rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-app: #0d1117;
    --bg-surface: #161b22;
    --fg-app: #e6edf3;
    --fg-muted: #9aa4b2;
    --border: #2a313c;
    --accent: #3b82f6;
    --accent-ink: #93c5fd;
    --danger: #ff8f85;
    --danger-bg: #3b1416;
    --success: #6ee7a8;
    --success-bg: #0f2a1c;
  }
}

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

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

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: 1rem;
  line-height: 1.55;
  color: var(--fg-app);
  background: var(--bg-app);
  min-height: 100vh;
}

h1, h2, h3 { line-height: 1.25; text-wrap: balance; }
p { margin: 0; }

/* ------------------------------------------------------- accessibilite ---- */
/* Lien d'evitement : visible uniquement au focus clavier. */
.skip-link {
  position: absolute;
  inset-inline-start: 0.5rem;
  top: -3rem;
  z-index: 100;
  padding: 0.6rem 1rem;
  background: var(--accent);
  color: var(--accent-fg);
  border-radius: var(--radius);
  text-decoration: none;
  transition: top 0.15s ease;
}
.skip-link:focus { top: 0.5rem; }

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

:where(a, button, input, textarea, select, [tabindex]):focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  border-radius: 6px;
}

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

/* ------------------------------------------------------------- structure -- */
.app { min-height: 100vh; }

.shell {
  max-width: 46rem;
  margin-inline: auto;
  padding: 1rem 1rem 4rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.shell__header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding-block: 0.5rem;
}

.shell__brand { display: flex; flex-direction: column; }
.shell__actions { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.shell__email { font-size: 0.8125rem; color: var(--fg-muted); word-break: break-all; }

.brand {
  font-weight: 700;
  font-size: 1.125rem;
  color: var(--fg-app);
  text-decoration: none;
  letter-spacing: -0.01em;
}

.page__title { font-size: 1.5rem; margin: 0; }
.page__subtitle { color: var(--fg-muted); font-size: 0.9375rem; overflow-wrap: anywhere; }

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 40vh;
  color: var(--fg-muted);
}

.banner {
  padding: 0.75rem 1rem;
  font-size: 0.875rem;
  text-align: center;
  border-bottom: 1px solid var(--border);
}
.banner--error { background: var(--danger-bg); color: var(--danger); }

/* ---------------------------------------------------------------- panels -- */
.card-panel {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.card-panel__title { font-size: 1rem; margin: 0; }
.card-panel__hint { font-size: 0.875rem; color: var(--fg-muted); }
.card-panel--actions { gap: 0.5rem; }

/* -------------------------------------------------------------- boutons --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-height: 2.75rem; /* cible tactile confortable */
  padding: 0.55rem 1.1rem;
  font: inherit;
  font-weight: 600;
  border-radius: var(--radius);
  border: 1px solid transparent;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}
.btn[disabled] { opacity: 0.6; cursor: not-allowed; }

.btn--primary { background: var(--accent); color: var(--accent-fg); }
.btn--primary:hover:not([disabled]) { background: var(--accent-ink); }

.btn--ghost {
  background: transparent;
  color: var(--fg-app);
  border-color: var(--border);
}
.btn--ghost:hover:not([disabled]) { border-color: var(--accent); color: var(--accent-ink); }

.btn--link {
  background: none;
  border: none;
  color: var(--accent-ink);
  padding: 0.25rem 0;
  min-height: auto;
  text-decoration: underline;
}

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

/* ------------------------------------------------------------ formulaires - */
.form { display: flex; flex-direction: column; gap: 0.75rem; }
.form__grid { display: grid; gap: 0.9rem; grid-template-columns: 1fr; }

@media (min-width: 34rem) {
  .form__grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

.form__group {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1rem;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  background: var(--bg-surface);
}

.form__legend {
  font-weight: 700;
  font-size: 0.9375rem;
  padding-inline: 0.35rem;
}

.form__actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.form__actions .btn { flex: 1 1 12rem; }

.form__error {
  background: var(--danger-bg);
  color: var(--danger);
  border-radius: var(--radius);
  padding: 0.6rem 0.75rem;
  font-size: 0.875rem;
  margin: 0;
}

.field { display: flex; flex-direction: column; gap: 0.3rem; }
.field__label { font-weight: 600; font-size: 0.875rem; }
.field__optional { font-size: 0.8125rem; color: var(--fg-muted); margin-block-start: -0.25rem; }

.field__input {
  font: inherit;
  color: inherit;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.6rem 0.75rem;
  min-height: 2.75rem;
  width: 100%;
}
.field__input:focus-visible { border-color: var(--accent); }

.field--error .field__input { border-color: var(--danger); }

.field__hint { font-size: 0.8125rem; color: var(--fg-muted); }
.field__error {
  font-size: 0.8125rem;
  color: var(--danger);
  font-weight: 600;
  margin: 0;
}

/* ---------------------------------------------------------- selecteur de langue */
.locale-switch { display: inline-flex; gap: 0.15rem; padding: 0.15rem; background: var(--bg-app); border: 1px solid var(--border); border-radius: 999px; }
.locale-switch__item {
  border: none;
  background: transparent;
  color: var(--fg-muted);
  font: inherit;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 0.25rem 0.55rem;
  border-radius: 999px;
  cursor: pointer;
  min-width: 2.25rem;
}
.locale-switch__item.is-active { background: var(--accent); color: var(--accent-fg); }

/* --------------------------------------------------------- vue connexion -- */
.auth {
  max-width: 26rem;
  margin-inline: auto;
  padding: 2rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  align-items: stretch;
}
.auth__title { font-size: 1.5rem; margin: 0; }
.auth__subtitle { color: var(--fg-muted); }
.auth .locale-switch { align-self: center; }

.empty {
  background: var(--bg-surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: 2rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  align-items: center;
  text-align: center;
}
.empty__title { font-size: 1.125rem; margin: 0; }
.empty__body { color: var(--fg-muted); }

/* ------------------------------------------------------------- dashboard -- */
.share { display: flex; flex-direction: column; gap: 0.5rem; }
.share__input {
  font: inherit;
  font-size: 0.875rem;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-app);
  color: var(--fg-app);
  width: 100%;
}

.qr { display: flex; flex-direction: column; gap: 0.75rem; align-items: center; }
.qr__canvas {
  width: min(100%, 20rem);
  height: auto;
  image-rendering: pixelated; /* modules nets, jamais flous */
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: #ffffff;
}
.qr__canvas--small { width: min(100%, 10rem); }

.actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.actions .btn { flex: 1 1 10rem; }

.card-switcher { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.card-switcher__item {
  font: inherit;
  font-size: 0.8125rem;
  padding: 0.4rem 0.75rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--fg-app);
  cursor: pointer;
}
.card-switcher__item.is-active { border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }

.preview { display: flex; justify-content: center; }
.preview__contact { display: inline-block; font-size: 0.875rem; }
.preview__qr { display: flex; justify-content: center; margin-block-start: 0.5rem; }

/* ------------------------------------------------------ photo & themes ---- */
.photo { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.photo__preview {
  width: 5rem; height: 5rem;
  border-radius: 50%;
  overflow: hidden;
  display: grid;
  place-items: center;
  background: var(--bg-app);
  border: 1px solid var(--border);
}
.photo__img { width: 100%; height: 100%; object-fit: cover; }
.photo__actions { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }

.themes { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.themes__item {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font: inherit;
  font-size: 0.8125rem;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--fg-app);
  cursor: pointer;
}
.themes__item[aria-checked="true"] { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; font-weight: 600; }
.themes__swatch { width: 1rem; height: 1rem; border-radius: 50%; border: 1px solid rgb(0 0 0 / 15%); }

/* Les pastilles reprennent EXACTEMENT les couleurs des themes ci-dessous. */
.themes__swatch--emerald { background: linear-gradient(135deg, #047857, #34d399); }
.themes__swatch--midnight { background: linear-gradient(135deg, #1e293b, #64748b); }
.themes__swatch--sunset  { background: linear-gradient(135deg, #c2410c, #fbbf24); }
.themes__swatch--rose    { background: linear-gradient(135deg, #be123c, #fda4af); }

/* ==========================================================================
   THEMES DE CARTE
   Ces variables sont consommees par .card-page (page publique SSR ET apercu
   du dashboard). Contraste texte/fond superieur a 7:1 sur les quatre themes.
   ========================================================================== */
.card-page {
  --c-bg: #ffffff;
  --c-fg: #0b1220;
  --c-muted: #46505f;
  --c-accent: #047857;
  --c-accent-fg: #ffffff;
  --c-surface: #f2f5f4;
  --c-border: #d9e2df;
}

.card-page[data-theme="emerald"] {
  --c-bg: #f0fdf6;
  --c-fg: #04231a;
  --c-muted: #2f5548;
  --c-accent: #047857;
  --c-accent-fg: #ffffff;
  --c-surface: #ffffff;
  --c-border: #c7e5d8;
}

.card-page[data-theme="midnight"] {
  --c-bg: #0f172a;
  --c-fg: #f1f5f9;
  --c-muted: #b3c0d4;
  --c-accent: #60a5fa;
  --c-accent-fg: #06121f;
  --c-surface: #1e293b;
  --c-border: #334155;
}

.card-page[data-theme="sunset"] {
  --c-bg: #fff7ed;
  --c-fg: #3b1503;
  --c-muted: #6b3a1c;
  --c-accent: #c2410c;
  --c-accent-fg: #ffffff;
  --c-surface: #ffffff;
  --c-border: #f3cdb2;
}

.card-page[data-theme="rose"] {
  --c-bg: #fff1f4;
  --c-fg: #3d0715;
  --c-muted: #6d2a3d;
  --c-accent: #be123c;
  --c-accent-fg: #ffffff;
  --c-surface: #ffffff;
  --c-border: #f5c6d2;
}

/* -------------------------------------------------- carte publique / apercu */
.card-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.25rem;
  padding: 1.5rem 1rem 2.5rem;
}

body.card-page {
  background:
    radial-gradient(120% 80% at 50% 0%, var(--c-surface) 0%, var(--c-bg) 60%);
  color: var(--c-fg);
  min-height: 100vh;
}

.card {
  width: min(100%, 30rem);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
  text-align: center;
  background: var(--c-surface);
  border: 1px solid var(--c-border);
  border-radius: 1.5rem;
  padding: 1.75rem 1.25rem 1.5rem;
  box-shadow: var(--shadow);
}

.card__avatar {
  width: 7rem;
  height: 7rem;
  border-radius: 50%;
  object-fit: cover;
  background: var(--c-bg);
  border: 3px solid var(--c-accent);
  display: grid;
  place-items: center;
  font-size: 2rem;
  font-weight: 700;
  color: var(--c-accent);
  flex: none;
}
.card__avatar--small { width: 3rem; height: 3rem; font-size: 1rem; border-width: 2px; }

.card__name { font-size: 1.5rem; margin: 0.5rem 0 0; }
.card__title { color: var(--c-muted); font-size: 1rem; margin: 0; }
.card__company { color: var(--c-muted); font-size: 0.9375rem; margin: 0; font-weight: 600; }
.card__empty { color: var(--c-muted); font-size: 0.9375rem; }

.card__contacts {
  list-style: none;
  margin: 0.75rem 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  width: 100%;
}
.card__contact { display: flex; }

/* Les liens de la carte publique herittent des couleurs du theme. */
.card .btn--ghost {
  width: 100%;
  justify-content: flex-start;
  gap: 0.65rem;
  background: transparent;
  border: 1px solid var(--c-border);
  color: var(--c-fg);
  text-align: start;
  font-weight: 500;
  overflow-wrap: anywhere;
}
.card .btn--ghost:hover { border-color: var(--c-accent); color: var(--c-accent); }

.card .btn--primary { background: var(--c-accent); color: var(--c-accent-fg); width: 100%; margin-block-start: 0.75rem; }
.card .btn--primary:hover { filter: brightness(1.08); }

.icon {
  width: 1.15rem;
  height: 1.15rem;
  fill: currentColor;
  flex: none;
}

.card--empty { gap: 1rem; }

/* Bouton "Ajouter aux contacts" = .btn--vcard */
.btn--vcard { min-height: 3rem; font-size: 1rem; }

.card-locale-bar { display: flex; justify-content: center; padding-block-start: 1rem; }

.card-page .locale-switch__item { color: var(--c-muted); background: transparent; }
.card-page .locale-switch__item.is-active { background: var(--c-accent); color: var(--c-accent-fg); }

.card-footer {
  font-size: 0.8125rem;
  color: var(--c-muted);
  text-align: center;
  padding-block: 1rem 2rem;
}

/* Apercu dans le dashboard : meme rendu, mais borne. */
.card-page--preview {
  padding: 1rem 0.75rem;
  background: var(--c-bg);
  border: 1px solid var(--c-border);
  border-radius: var(--radius-lg);
  width: min(100%, 24rem);
  gap: 0.5rem;
}
.card-page--preview .card { box-shadow: none; border: none; background: transparent; padding: 0; }
.card-page--preview .card__avatar { width: 5rem; height: 5rem; font-size: 1.4rem; }
.card-page--preview .card__name { font-size: 1.2rem; }
.card-page--preview .card__contacts { width: 100%; }
.card-page--preview .card__contact { justify-content: center; }

/* ------------------------------------------------------------------ toast - */
.toast {
  position: fixed;
  inset-inline: 1rem;
  bottom: 1rem;
  margin-inline: auto;
  max-width: 24rem;
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  background: var(--fg-app);
  color: var(--bg-surface);
  font-size: 0.9375rem;
  box-shadow: var(--shadow);
  z-index: 200;
  text-align: center;
}
.toast--error { background: var(--danger); color: #fff; }
.toast--success { background: var(--success); color: #04231a; }

/* --------------------------------------------------------- direction RTL -- */
/* Les proprietes logiques suffisent pour la plupart des cas ; on corrige
   explicitement les rares elements alignes sur un bord physique. */
[dir="rtl"] .card .btn--ghost { text-align: right; }
[dir="rtl"] .preview__contact { text-align: right; }

/* ==========================================================================
   MODALE D'INVITATION A ENREGISTRER LE CONTACT
   Utilise volontairement les variables NEUTRES de l'application (et non
   celles du theme de la carte) : la boite doit rester lisible sur les quatre
   themes, comme une boite de dialogue du systeme.
   ========================================================================== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 1rem;
}

.modal[hidden] { display: none; }

.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgb(11 18 32 / 55%);
  animation: modal-fade 0.18s ease both;
}

.modal__panel {
  position: relative;
  width: min(100%, 26rem);
  background: var(--bg-surface);
  color: var(--fg-app);
  border-radius: var(--radius-lg);
  box-shadow: 0 12px 40px rgb(11 18 32 / 30%);
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  animation: modal-rise 0.2s ease both;
}

.modal__title { font-size: 1.0625rem; margin: 0; padding-inline-end: 2rem; }
.modal__body { font-size: 0.9375rem; color: var(--fg-muted); margin: 0; }
.modal__actions { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-block-start: 0.35rem; }
.modal__actions .btn { flex: 1 1 10rem; }

.modal__close {
  position: absolute;
  top: 0.5rem;
  inset-inline-end: 0.5rem;
  width: 2.25rem;
  height: 2.25rem;
  display: grid;
  place-items: center;
  font-size: 1.375rem;
  line-height: 1;
  color: var(--fg-muted);
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
}
.modal__close:hover { color: var(--fg-app); border-color: var(--border); }

@keyframes modal-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes modal-rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

/* Pas d'animation si l'utilisateur l'a demandee. */
@media (prefers-reduced-motion: reduce) {
  .modal__backdrop, .modal__panel { animation: none; }
}

/* Le fond ne doit pas defiler derriere la modale. */
body.has-modal { overflow: hidden; }

@media (prefers-color-scheme: dark) {
  .modal__backdrop { background: rgb(0 0 0 / 65%); }
  .modal__panel { box-shadow: 0 12px 40px rgb(0 0 0 / 55%); }
}

/* ------------------------------------------------------------- impression - */
@media print {
  .card-locale-bar, .card-footer, .skip-link { display: none; }
  .card { box-shadow: none; border: none; }
}