@layer reset, tokens, base, layout, components, utilities;

@layer reset {
  /* L'attribut hidden doit toujours gagner : sinon un `display: flex/grid` d'une classe (ex. .field) l'emporte
     et l'élément « masqué » reste visible. !important dans la couche la plus basse prime sur toutes les autres. */
  [hidden] { display: none !important; }
  *, *::before, *::after { box-sizing: border-box; }
  body, h1, h2, h3, h4, p, ul, ol, figure { margin: 0; padding: 0; }
  ul, ol { list-style: none; }
  button { font: inherit; }
  img, svg { display: block; max-width: 100%; }
}

@layer tokens {
  :root {
    /* Marque Renaissance Québec (tirée de la maquette Android) */
    --c-navy:        #16295B;
    --c-navy-600:    #1f3573;
    --c-yellow:      #FBC800;
    --c-yellow-50:   #FBF7E6;

    --c-bg:          #F4F5F7;
    --c-surface:     #FFFFFF;
    --c-surface-alt: #FAFBFC;
    --c-border:      #E2E4E9;
    --c-border-soft: #EEF0F3;
    --c-border-ctl:  #D8DAE3;

    --c-text:        #1B1B1F;
    --c-text-2:      #55596B;
    --c-muted:       #8A8E9E;
    --c-faint:       #9A9DAC;

    --c-success:     #3DBE5C;
    --c-danger:      #D64545;
    --c-warning:     #E8A200;

    /* Catégories de comptage (graphiques, pastilles) */
    --c-cat-homme:   var(--c-navy);
    --c-cat-femme:   var(--c-yellow);
    --c-cat-autre:   #7C86A8;
    --c-cat-neutre:  #B9BED0;

    --font-body:    'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
    --font-display: 'Poppins', 'Inter', system-ui, sans-serif;

    --text-xs: 0.75rem; --text-sm: 0.875rem; --text-base: 1rem;
    --text-lg: 1.125rem; --text-xl: 1.375rem; --text-2xl: 1.75rem; --text-3xl: 3rem;

    --sp-1: 0.25rem; --sp-2: 0.5rem; --sp-3: 0.75rem; --sp-4: 1rem;
    --sp-5: 1.25rem; --sp-6: 1.5rem; --sp-8: 2rem; --sp-10: 2.5rem;

    --radius-sm: 0.5rem; --radius: 0.875rem; --radius-lg: 1rem;
    --radius-xl: 1.25rem; --radius-pill: 999px;

    --shadow-sm: 0 1px 3px rgb(16 24 64 / 0.06);
    --shadow:    0 2px 8px rgb(22 41 91 / 0.18);
    --shadow-lg: 0 8px 28px rgb(16 24 64 / 0.20);

    --nav-w: 96px;
  }
}

@layer base {
  html { -webkit-text-size-adjust: 100%; }
  body { font-family: var(--font-body); font-size: var(--text-base); color: var(--c-text); background: var(--c-bg); line-height: 1.5; }
  h1, h2, h3 { font-family: var(--font-display); color: var(--c-navy); font-weight: 800; line-height: 1.2; }
  h1 { font-size: var(--text-2xl); }
  h2 { font-size: var(--text-xl); }
  h3 { font-size: var(--text-lg); font-weight: 700; }
  a  { color: var(--c-navy); }
  :focus-visible { outline: 3px solid var(--c-yellow); outline-offset: 2px; }
}

@layer layout {
  /* Coquille de l'application : rail de navigation + contenu */
  .app { display: flex; min-height: 100dvh; }
  .app__main { flex: 1; min-width: 0; padding: var(--sp-8) var(--sp-10); display: flex; flex-direction: column; gap: var(--sp-5); }
  .page-header { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-4); flex-wrap: wrap; }
  .page-header__sub { font-size: var(--text-sm); color: var(--c-muted); font-weight: 500; }
  .page-header__actions { display: flex; align-items: center; gap: var(--sp-3); flex-wrap: wrap; }

  .grid-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: var(--sp-5); }
  .grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: var(--sp-5); }

  /* Page d'authentification (SSO) */
  .auth { min-height: 100dvh; display: grid; place-items: center; padding: var(--sp-6); background: var(--c-navy); }

  @media (max-width: 767px) {
    .app { flex-direction: column-reverse; }
    .app__main { padding: var(--sp-5) var(--sp-4); }
  }
}

@layer components {
  /* ---------- Rail de navigation (barre du bas en mobile) ---------- */
  .nav-rail { width: var(--nav-w); flex: none; background: var(--c-navy); display: flex; flex-direction: column; align-items: center; padding: var(--sp-5) 0 var(--sp-4); position: sticky; top: 0; height: 100dvh; }
  .nav-rail__logo { width: 56px; height: 56px; margin-bottom: var(--sp-6); }
  .nav-rail__logo img { width: 100%; height: 100%; object-fit: contain; }
  .nav-rail__items { width: 100%; display: flex; flex-direction: column; }
  .nav-rail__spacer { flex: 1; }
  .nav-rail__user { display: flex; flex-direction: column; align-items: center; gap: var(--sp-1); width: 100%; padding: var(--sp-2); }
  .nav-rail__version { font-size: 0.6rem; color: rgb(255 255 255 / 0.55); }

  .nav-item { display: flex; flex-direction: column; align-items: center; gap: var(--sp-1); width: 100%; padding: 10px 0; border: 0; border-left: 3px solid transparent; background: none; color: rgb(255 255 255 / 0.62); font-size: 0.6875rem; font-weight: 600; text-decoration: none; cursor: pointer; }
  .nav-item:hover { color: #fff; }
  .nav-item[aria-current="page"] { color: var(--c-yellow); background: rgb(251 200 0 / 0.16); border-left-color: var(--c-yellow); }
  .nav-item svg { width: 24px; height: 24px; }

  @media (max-width: 767px) {
    .nav-rail { width: 100%; height: auto; position: sticky; bottom: 0; top: auto; flex-direction: row; padding: 0; z-index: 50; }
    .nav-rail__logo, .nav-rail__version, .nav-rail__spacer, .nav-rail__user { display: none; }
    .nav-rail__items { flex-direction: row; justify-content: space-around; }
    .nav-item { border-left: 0; border-top: 3px solid transparent; padding: 8px 0; }
    .nav-item[aria-current="page"] { border-top-color: var(--c-yellow); }
  }

  /* ---------- Cartes et statistiques ---------- */
  .card { background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); }
  .card__header { padding: var(--sp-4) var(--sp-6); border-bottom: 1px solid var(--c-border-soft); display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); }
  .card__body { padding: var(--sp-6); }
  .card__eyebrow, .stat-card__label { font-size: var(--text-xs); font-weight: 700; color: var(--c-muted); letter-spacing: 0.05em; text-transform: uppercase; }

  .stat-card { padding: var(--sp-6); display: flex; flex-direction: column; gap: var(--sp-2); }
  .stat-card--center { align-items: center; text-align: center; justify-content: center; }
  .stat-card__value { font-family: var(--font-display); font-size: var(--text-3xl); font-weight: 800; color: var(--c-navy); line-height: 1; }
  .stat-card__value--md { font-size: var(--text-xl); font-family: var(--font-body); font-weight: 700; color: var(--c-text); }
  .stat-card__hint { font-size: var(--text-sm); color: var(--c-text-2); }

  .kv { display: flex; flex-direction: column; gap: var(--sp-2); }
  .kv__row { display: flex; justify-content: space-between; gap: var(--sp-4); font-size: var(--text-sm); }
  .kv__row dt { color: var(--c-text-2); }
  .kv__row dd { margin: 0; font-weight: 600; text-align: right; }

  /* ---------- Pastilles, badges, statut ---------- */
  .pill { display: inline-flex; align-items: center; gap: var(--sp-2); background: var(--c-surface); border: 1px solid var(--c-border); border-radius: var(--radius-pill); padding: 6px 14px; font-size: var(--text-sm); font-weight: 500; color: var(--c-text-2); }
  .pill--navy { background: var(--c-navy); border-color: var(--c-navy); color: #fff; font-weight: 600; }
  .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--c-muted); flex: none; }
  .dot--ok { background: var(--c-success); }
  .dot--warn { background: var(--c-warning); }
  .dot--off { background: var(--c-danger); }

  .badge { display: inline-block; padding: 2px 10px; border-radius: var(--radius-pill); font-size: var(--text-xs); font-weight: 700; background: var(--c-border-soft); color: var(--c-text-2); }
  .badge--homme  { background: var(--c-cat-homme); color: #fff; }
  .badge--femme  { background: var(--c-cat-femme); color: var(--c-navy); }
  .badge--autre  { background: var(--c-cat-autre); color: #fff; }
  .badge--neutre { background: var(--c-cat-neutre); color: var(--c-navy); }

  /* ---------- Boutons ---------- */
  .btn { display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2); padding: 10px 20px; border: 1px solid transparent; border-radius: var(--radius-pill); font-size: var(--text-sm); font-weight: 600; cursor: pointer; text-decoration: none; transition: opacity .15s, transform .1s; }
  .btn:hover { opacity: .88; }
  .btn:active { transform: scale(.98); }
  .btn svg { width: 18px; height: 18px; }
  .btn--primary   { background: var(--c-navy); color: #fff; }
  .btn--accent    { background: var(--c-yellow); color: var(--c-navy); }
  .btn--secondary { background: var(--c-surface); color: var(--c-navy); border-color: var(--c-border-ctl); }
  .btn--danger    { background: var(--c-danger); color: #fff; }
  .btn--ghost     { background: transparent; color: var(--c-navy); }
  .btn--sm        { padding: 6px 14px; font-size: var(--text-xs); }
  .btn--block     { width: 100%; }
  .btn[disabled] { opacity: .5; cursor: not-allowed; }

  /* Puces de filtre (périodes, centres) */
  .chips { display: flex; gap: var(--sp-2); flex-wrap: wrap; }
  .chip { border: 1px solid var(--c-border-ctl); background: var(--c-surface); color: var(--c-text-2); border-radius: var(--radius-pill); padding: 8px 18px; font-size: var(--text-sm); font-weight: 600; cursor: pointer; text-decoration: none; }
  .chip[aria-pressed="true"], .chip[aria-current="true"] { background: var(--c-navy); border-color: var(--c-navy); color: #fff; }

  /* ---------- Formulaires ---------- */
  .field { display: flex; flex-direction: column; gap: var(--sp-1); margin-bottom: var(--sp-4); }
  .field__label { font-size: var(--text-sm); font-weight: 600; color: var(--c-navy); }
  .field__help { font-size: var(--text-xs); color: var(--c-muted); }
  .field__error { font-size: var(--text-xs); color: var(--c-danger); font-weight: 500; }
  .form-control, .form-select { width: 100%; padding: 10px 14px; border: 1px solid var(--c-border-ctl); border-radius: var(--radius-sm); background: var(--c-surface); font: inherit; font-size: var(--text-sm); color: var(--c-text); }
  .form-control:focus, .form-select:focus { outline: 2px solid var(--c-navy); outline-offset: 0; border-color: var(--c-navy); }
  .is-invalid { border-color: var(--c-danger); }

  /* Interrupteur (comptage genré, etc.) */
  .toggle { display: inline-flex; align-items: center; gap: var(--sp-3); cursor: pointer; font-size: var(--text-sm); color: var(--c-text-2); }
  .toggle input { position: absolute; opacity: 0; pointer-events: none; }
  .toggle__track { width: 38px; height: 22px; border-radius: var(--radius-pill); background: var(--c-border-ctl); position: relative; transition: background .15s; flex: none; }
  .toggle__track::after { content: ''; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: left .15s; }
  .toggle input:checked + .toggle__track { background: var(--c-navy); }
  .toggle input:checked + .toggle__track::after { left: 19px; }
  .toggle input:focus-visible + .toggle__track { outline: 3px solid var(--c-yellow); outline-offset: 2px; }

  /* ---------- Tableaux (en-tête marine, total jaune) ---------- */
  .table-wrap { overflow-x: auto; }
  .data-table { width: 100%; border-collapse: collapse; font-size: var(--text-base); }
  .data-table th { background: var(--c-navy); color: rgb(255 255 255 / 0.85); font-size: var(--text-xs); font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase; padding: 14px 24px; text-align: left; white-space: nowrap; }
  .data-table td { padding: 14px 24px; border-bottom: 1px solid var(--c-border-soft); }
  .data-table tbody tr:nth-child(even) { background: var(--c-surface-alt); }
  .data-table .num { text-align: center; font-variant-numeric: tabular-nums; }
  .data-table td.strong { color: var(--c-navy); font-weight: 700; }
  .data-table tfoot td { background: var(--c-yellow-50); border-top: 2px solid var(--c-yellow); color: var(--c-navy); font-weight: 700; }

  .empty-state { padding: var(--sp-10) var(--sp-6); text-align: center; color: var(--c-muted); display: flex; flex-direction: column; align-items: center; gap: var(--sp-3); }
  .empty-state svg { width: 40px; height: 40px; color: var(--c-border-ctl); }

  /* ---------- Modals natives <dialog> ---------- */
  dialog { border: 0; border-radius: var(--radius-xl); box-shadow: var(--shadow-lg); padding: 0; width: min(520px, 92vw); color: var(--c-text); background: var(--c-surface); }
  dialog::backdrop { background: rgb(22 41 91 / 0.55); }
  .dialog--lg { width: min(860px, 94vw); }
  .modal-header { display: flex; align-items: center; justify-content: space-between; padding: var(--sp-4) var(--sp-6); background: var(--c-navy); color: #fff; border-radius: var(--radius-xl) var(--radius-xl) 0 0; }
  .modal-title { font-family: var(--font-display); font-weight: 700; font-size: var(--text-lg); color: #fff; }
  .modal-body { padding: var(--sp-6); }
  .modal-footer { display: flex; justify-content: flex-end; gap: var(--sp-2); padding: var(--sp-4) var(--sp-6); border-top: 1px solid var(--c-border-soft); }
  .btn-close { background: none; border: 0; cursor: pointer; font-size: 1.5rem; line-height: 1; color: rgb(255 255 255 / 0.8); padding: var(--sp-1); }

  /* ---------- Toasts / messages ---------- */
  .toast-wrap { position: fixed; bottom: var(--sp-6); right: var(--sp-6); z-index: 9999; display: flex; flex-direction: column; gap: var(--sp-2); }
  .toast { display: flex; align-items: center; gap: var(--sp-3); padding: var(--sp-3) var(--sp-4); border-radius: var(--radius-sm); box-shadow: var(--shadow-lg); background: var(--c-navy); color: #fff; font-size: var(--text-sm); }
  #error-toast { opacity: 0; pointer-events: none; transition: opacity .2s; background: var(--c-danger); }
  #error-toast.toast--visible { opacity: 1; pointer-events: auto; }
  .toast--success { background: var(--c-success); }
  .toast--error { background: var(--c-danger); }
  .toast .btn-close { color: inherit; }

  /* ---------- Connexion SSO ---------- */
  .login-card { background: var(--c-surface); border-radius: var(--radius-xl); box-shadow: var(--shadow-lg); width: min(420px, 100%); padding: var(--sp-10) var(--sp-8); text-align: center; display: flex; flex-direction: column; align-items: center; gap: var(--sp-4); }
  .login-card__logo { width: 88px; height: 88px; }
  .login-card p { color: var(--c-text-2); font-size: var(--text-sm); }
  .login-form { width: 100%; text-align: left; display: flex; flex-direction: column; }
  .login-form .field:last-of-type { margin-bottom: var(--sp-5); }
  .login-divider { width: 100%; display: flex; align-items: center; gap: var(--sp-3); color: var(--c-muted); font-size: var(--text-xs); text-transform: uppercase; letter-spacing: .05em; }
  .login-divider::before, .login-divider::after { content: ''; flex: 1; height: 1px; background: var(--c-border); }
}

@layer utilities {
  .text-center { text-align: center; }
  .text-end { text-align: right; }
  .text-muted { color: var(--c-muted); }
  .text-navy { color: var(--c-navy); }
  .fw-bold { font-weight: 700; }
  .flex { display: flex; }
  .items-center { align-items: center; }
  .justify-between { justify-content: space-between; }
  .gap-2 { gap: var(--sp-2); }
  .gap-4 { gap: var(--sp-4); }
  .mt-4 { margin-top: var(--sp-4); }
  .mb-4 { margin-bottom: var(--sp-4); }
  .hidden { display: none !important; }
  .sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
}

@layer components {
  .filter-bar { display: flex; align-items: center; gap: var(--sp-3); flex-wrap: wrap; }
  .filter-bar .form-select, .page-header__actions .form-select { width: auto; min-width: 12rem; }
  .row-actions { display: inline-flex; gap: var(--sp-2); flex-wrap: wrap; justify-content: flex-end; }
  .badge--ok  { background: rgb(61 190 92 / 0.18); color: #1f7a38; }
  .badge--off { background: rgb(214 69 69 / 0.14); color: #a02929; }
  .alert { padding: var(--sp-3) var(--sp-4); border-radius: var(--radius-sm); font-size: var(--text-sm); margin-bottom: var(--sp-4); }
  .alert--warn { background: var(--c-yellow-50); border: 1px solid var(--c-yellow); color: var(--c-navy); }
  .secret-box { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); background: var(--c-surface-alt); border: 1px dashed var(--c-border-ctl); border-radius: var(--radius-sm); padding: var(--sp-3) var(--sp-4); }
  .secret-box code { word-break: break-all; font-size: var(--text-sm); color: var(--c-navy); }
}

@layer components {
  .pill { white-space: nowrap; }
  .data-table td strong, .data-table td code { white-space: nowrap; }
  .row-actions { flex-wrap: nowrap; }
  .row-actions .btn { white-space: nowrap; }
  @media (max-width: 1100px) {
    .data-table th, .data-table td { padding: 12px 12px; }
    .app__main { padding: var(--sp-6) var(--sp-5); }
  }
}

@layer components {
  .data-table td { white-space: nowrap; }
}

@layer components {
  .btn-row { display: flex; gap: var(--sp-2); flex-wrap: wrap; align-items: center; }
  .alert p { margin: var(--sp-2) 0 var(--sp-3); }
  .code-area { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: var(--text-xs); line-height: 1.5; resize: vertical; margin-top: var(--sp-2); }
  details summary { cursor: pointer; font-size: var(--text-sm); color: var(--c-navy); font-weight: 600; }
}

@layer components {
  .form-divider { border: 0; border-top: 1px solid var(--c-border); margin: var(--sp-4) 0 var(--sp-5); }
}

@layer components {
  .field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 0 var(--sp-4); }
  .field-row > .field { min-width: 0; }
  @media (max-width: 480px) { .field-row { grid-template-columns: 1fr; } }
}

/* Modals : en-tête et pied (boutons) fixes, seul le corps défile quand le contenu dépasse l'écran. */
@layer components {
  dialog[open] { display: flex; flex-direction: column; max-height: min(92dvh, 56rem); overflow: hidden; }
  dialog > div { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; }  /* #htmx-modal(-lg)-content */
  .modal-form { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; }
  .modal-header, .modal-footer { flex: none; }
  .modal-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
  .modal-footer { background: var(--c-surface); box-shadow: 0 -6px 12px -10px rgb(16 24 64 / 0.25); }
}
