/* ==========================================================================
   Anneau : feuille de style unique
   Couleurs en variables (clair par defaut, sombre via prefers-color-scheme).
   ========================================================================== */

:root {
  color-scheme: light dark;

  --bg: #f4f3ef;
  --surface: #ffffff;
  --surface-2: #f8f7f3;
  --ink: #1b2420;
  --muted: #5b6762;
  --border: #dcd9d0;
  --accent: #0d6b5e;
  --accent-ink: #ffffff;
  --accent-soft: #e0eeea;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --warn: #8a5300;
  --warn-soft: #fdf1dc;
  --ok: #1f7a4a;
  --ok-soft: #e3f3ea;
  --focus: #1257c7;
  --chart-grid: #e7e5de;

  /* Couleurs des courbes, une par mesure */
  --c-hr: #c2413a;
  --c-spo2: #1f6fb2;
  --c-hrv: #3d8b37;
  --c-stress: #b7791f;
  --c-stress2: #8f6b3a;
  --c-steps: #5b6b9a;
  --c-weight: #7a5c99;
  --c-temp: #b5476e;
  --c-bp: #3f7676;
  --c-bp2: #6c9d9d;
  --c-sleep: #4d5fa8;
  --c-resp: #5f7f1f;

  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgb(20 30 25 / 6%), 0 2px 8px rgb(20 30 25 / 4%);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;

  accent-color: var(--accent);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1412;
    --surface: #171d1b;
    --surface-2: #1c2321;
    --ink: #e5ebe8;
    --muted: #9ba7a2;
    --border: #2c3532;
    --accent: #5fc4b2;
    --accent-ink: #072019;
    --accent-soft: #16302b;
    --danger: #ff8a80;
    --danger-soft: #3a1c1a;
    --warn: #f0b35a;
    --warn-soft: #36290f;
    --ok: #6fd39b;
    --ok-soft: #15301f;
    --focus: #8ab4ff;
    --chart-grid: #26302d;

    --c-hr: #ff7a70;
    --c-spo2: #6fb3ff;
    --c-hrv: #84d27c;
    --c-stress: #f2b950;
    --c-stress2: #d0a56a;
    --c-steps: #9fb0e6;
    --c-weight: #c3a4e6;
    --c-temp: #f08cb2;
    --c-bp: #8fc9c9;
    --c-bp2: #b5dede;
    --c-sleep: #95a5f0;
    --c-resp: #b5d46a;

    --shadow: none;
  }
}

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

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

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 var(--font);
}

h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.6rem; letter-spacing: -0.01em; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75em; }

a { color: var(--accent); text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }

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

.num, .kpi-value, td.num { font-variant-numeric: tabular-nums; }
code, .mono { font-family: var(--mono); font-size: .9em; }

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

.skip-link { position: absolute; left: -9999px; top: 0; }
.skip-link:focus { left: 1rem; top: .5rem; z-index: 10; background: var(--surface); padding: .5rem 1rem; }

/* --------------------------------------------------------------------------
   Structure
   -------------------------------------------------------------------------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 1.5rem; flex-wrap: wrap;
  padding: .6rem clamp(1rem, 3vw, 2rem);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: 1.1rem; color: var(--ink); text-decoration: none; }
.brand-mark { width: 26px; height: 26px; color: var(--accent); }

.nav { display: flex; gap: .25rem; flex-wrap: wrap; flex: 1; }
.nav a {
  padding: .4rem .75rem; border-radius: var(--radius-sm);
  color: var(--muted); text-decoration: none; font-weight: 500;
}
.nav a:hover { color: var(--ink); background: var(--surface-2); }
.nav a[aria-current="page"] { color: var(--accent); background: var(--accent-soft); }
.nav .count { margin-left: .35rem; }

.session { display: flex; align-items: center; gap: .75rem; font-size: .9rem; color: var(--muted); }

.main { flex: 1; width: 100%; max-width: 1180px; margin: 0 auto; padding: clamp(1rem, 3vw, 2rem); }
.main:focus { outline: none; }

.footer {
  padding: 1rem clamp(1rem, 3vw, 2rem) 1.5rem; border-top: 1px solid var(--border);
  color: var(--muted); font-size: .85rem; text-align: center;
}
.footer p { margin: .25rem 0; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.page-head h1 { margin: 0; }
.page-head .sub { color: var(--muted); margin: .25rem 0 0; }

.stack > * + * { margin-top: 1.25rem; }
.row { display: flex; gap: .75rem; flex-wrap: wrap; align-items: center; }
.grow { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.loading { color: var(--muted); }

/* --------------------------------------------------------------------------
   Panneaux et cartes
   -------------------------------------------------------------------------- */
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.1rem 1.25rem; box-shadow: var(--shadow);
}
.panel > h2:first-child { margin-bottom: .75rem; }
.panel-head { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; flex-wrap: wrap; margin-bottom: .5rem; }
.panel-head h2 { margin: 0; }

.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: .75rem; }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: .85rem 1rem; display: flex; flex-direction: column; gap: .15rem; box-shadow: var(--shadow);
  border-top: 3px solid var(--kpi-color, var(--border));
}
.kpi-label { font-size: .85rem; color: var(--muted); }
.kpi-value { font-size: 1.75rem; font-weight: 650; line-height: 1.1; }
.kpi-unit { font-size: .9rem; font-weight: 500; color: var(--muted); margin-left: .2rem; }
.kpi-meta { font-size: .8rem; color: var(--muted); }

.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 460px), 1fr)); gap: .9rem; }
.chart-card { min-width: 0; }
.chart-card.wide { grid-column: 1 / -1; }
.chart-box { position: relative; height: 230px; }
.chart-card.wide .chart-box { height: 260px; }
.chart-empty { display: grid; place-items: center; height: 100%; color: var(--muted); font-size: .9rem; border: 1px dashed var(--border); border-radius: var(--radius-sm); }

.two-cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 380px), 1fr)); gap: 1.25rem; align-items: start; }

/* --------------------------------------------------------------------------
   Badges, alertes, notices
   -------------------------------------------------------------------------- */
.badge {
  display: inline-flex; align-items: center; gap: .3rem;
  padding: .1rem .55rem; border-radius: 999px; font-size: .78rem; font-weight: 600;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
  white-space: nowrap;
}
.badge-ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge-warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge-danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge-accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

.notice { padding: .75rem 1rem; border-radius: var(--radius-sm); background: var(--surface-2); border-left: 4px solid var(--border); }
.notice-warn { background: var(--warn-soft); border-left-color: var(--warn); }
.notice-danger { background: var(--danger-soft); border-left-color: var(--danger); }
.notice-ok { background: var(--ok-soft); border-left-color: var(--ok); }
.notice-info { background: var(--accent-soft); border-left-color: var(--accent); }

.alert-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .5rem; }
.alert-item { display: flex; gap: .75rem; align-items: center; justify-content: space-between; padding: .6rem .8rem; border-radius: var(--radius-sm); background: var(--danger-soft); }
.alert-item.done { background: var(--surface-2); color: var(--muted); }

/* --------------------------------------------------------------------------
   Formulaires
   -------------------------------------------------------------------------- */
form { margin: 0; }
.form-grid { display: grid; gap: .9rem; }
.form-inline { display: flex; gap: .75rem; flex-wrap: wrap; align-items: flex-end; }
.form-inline .field { flex: 1 1 180px; }

.field { display: flex; flex-direction: column; gap: .3rem; }
.field label, .label { font-weight: 600; font-size: .92rem; }
.hint { font-size: .82rem; color: var(--muted); }
.error-msg { display: none; font-size: .85rem; color: var(--danger); }

input, select, textarea {
  font: inherit; color: var(--ink);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: .55rem .7rem; min-height: 2.6rem; width: 100%;
}
textarea { min-height: 5rem; resize: vertical; }
input[type="checkbox"], input[type="radio"] { width: 1.1rem; height: 1.1rem; min-height: 0; padding: 0; }
input:read-only:not([type="checkbox"]) { background: var(--surface-2); color: var(--muted); }

input:user-invalid, select:user-invalid, textarea:user-invalid { border-color: var(--danger); background: var(--danger-soft); }
:is(input, select, textarea):user-invalid ~ .error-msg { display: block; }

.check { display: flex; gap: .6rem; align-items: flex-start; font-weight: 400; }
.check input { margin-top: .2rem; flex: none; }

/* Choix oui / non (boutons radio) : fieldset sans le cadre par defaut du navigateur */
fieldset.choice { border: 0; margin: 0; padding: 0; min-width: 0; gap: .45rem; }
fieldset.choice legend { padding: 0; margin-bottom: .35rem; }
fieldset.choice .check { font-weight: 400; font-size: 1rem; }

.otp-input { font-family: var(--mono); font-size: 1.4rem; letter-spacing: .35em; text-align: center; max-width: 12rem; }

.password-wrap { display: flex; gap: .4rem; }
.password-wrap input { flex: 1; }

.rules { margin: 0; padding-left: 1.1rem; font-size: .82rem; color: var(--muted); }
.field:has(input:user-invalid) .rules { color: var(--danger); }

/* --------------------------------------------------------------------------
   Boutons
   -------------------------------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  font: inherit; font-weight: 600; font-size: .95rem; line-height: 1.2;
  padding: .55rem 1rem; min-height: 2.6rem; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface); color: var(--ink);
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--danger); border-color: currentColor; background: transparent; }
.btn-danger:hover { background: var(--danger-soft); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--ink); }
.btn-sm { padding: .3rem .65rem; min-height: 2rem; font-size: .85rem; }
.btn-link { border: 0; background: none; padding: 0; min-height: 0; color: var(--accent); font-weight: 600; cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; background: var(--surface); }
.seg button { font: inherit; font-size: .9rem; padding: .4rem .8rem; border: 0; background: transparent; color: var(--muted); cursor: pointer; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 600; }

/* --------------------------------------------------------------------------
   Tableaux
   -------------------------------------------------------------------------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: .55rem .7rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-weight: 600; color: var(--muted); background: var(--surface-2); font-size: .82rem; white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; }
td.details { max-width: 340px; overflow-wrap: anywhere; font-family: var(--mono); font-size: .78rem; color: var(--muted); }
.empty { color: var(--muted); padding: .75rem 0; }

/* --------------------------------------------------------------------------
   Pages d'authentification
   -------------------------------------------------------------------------- */
.auth { max-width: 430px; margin: clamp(1rem, 6vh, 4rem) auto; }
.auth .panel { padding: 1.5rem; }
.auth h1 { font-size: 1.45rem; }
.auth .switch { margin-top: 1rem; font-size: .92rem; color: var(--muted); }
.qr { width: 200px; height: 200px; padding: 10px; background: #fff; border-radius: var(--radius-sm); border: 1px solid var(--border); }
.qr svg { width: 100%; height: 100%; display: block; }
.secret { font-family: var(--mono); font-size: 1rem; letter-spacing: .08em; word-break: break-all; background: var(--surface-2); padding: .5rem .7rem; border-radius: var(--radius-sm); }

/* --------------------------------------------------------------------------
   Messagerie
   -------------------------------------------------------------------------- */
.messenger { display: grid; grid-template-columns: minmax(200px, 280px) 1fr; gap: 1rem; min-height: 60vh; }
@media (max-width: 720px) { .messenger { grid-template-columns: 1fr; } }
.contacts { list-style: none; margin: 0; padding: 0; display: grid; gap: .35rem; align-content: start; }
.contacts a { display: flex; justify-content: space-between; gap: .5rem; padding: .6rem .75rem; border-radius: var(--radius-sm); color: var(--ink); text-decoration: none; border: 1px solid var(--border); background: var(--surface); }
.contacts a[aria-current="page"] { border-color: var(--accent); background: var(--accent-soft); }
.contact-name { font-weight: 600; }
.thread { display: flex; flex-direction: column; min-height: 0; }
.bubbles { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: .5rem; padding: .5rem 0 1rem; max-height: 55vh; }
.bubble { max-width: min(78%, 560px); padding: .55rem .8rem; border-radius: 14px; background: var(--surface-2); border: 1px solid var(--border); white-space: pre-wrap; overflow-wrap: anywhere; }
.bubble.mine { align-self: flex-end; background: var(--accent-soft); border-color: transparent; }
.bubble time { display: block; font-size: .72rem; color: var(--muted); margin-top: .2rem; }
.composer { display: flex; gap: .5rem; align-items: flex-end; border-top: 1px solid var(--border); padding-top: .75rem; }
.composer textarea { min-height: 2.6rem; }

/* --------------------------------------------------------------------------
   Dialogues (element natif <dialog>)
   -------------------------------------------------------------------------- */
dialog {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 1.4rem;
  background: var(--surface); color: var(--ink); width: min(92vw, 460px);
}
dialog:modal { overscroll-behavior: contain; }
dialog::backdrop { background: rgb(10 15 13 / 55%); overflow: hidden; overscroll-behavior: contain; }
dialog h1 { font-size: 1.2rem; }
.dialog-actions { display: flex; gap: .6rem; justify-content: flex-end; margin-top: 1.2rem; flex-wrap: wrap; }

/* --------------------------------------------------------------------------
   Notification temporaire
   -------------------------------------------------------------------------- */
.toast {
  position: fixed; bottom: 1.25rem; left: 50%; translate: -50% 0; z-index: 20;
  max-width: min(92vw, 520px); padding: .7rem 1.1rem; border-radius: var(--radius-sm);
  background: var(--ink); color: var(--bg); font-weight: 500; box-shadow: 0 6px 24px rgb(0 0 0 / 20%);
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
.toast.show { opacity: 1; }
.toast.error { background: var(--danger); color: #fff; }

.tabs { display: flex; gap: .25rem; flex-wrap: wrap; border-bottom: 1px solid var(--border); margin-bottom: 1.25rem; }
.tabs a { padding: .55rem .9rem; color: var(--muted); text-decoration: none; font-weight: 600; border-bottom: 3px solid transparent; margin-bottom: -1px; }
.tabs a[aria-current="page"] { color: var(--accent); border-bottom-color: var(--accent); }

.prose { max-width: 72ch; }
.prose h2 { margin-top: 1.5rem; }
.prose ul { padding-left: 1.2rem; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
