/*
  Tokens — calqués sur les couleurs sémantiques Apple (grouped background,
  label, secondary label, separator). Contrastes vérifiés (WCAG AA) :
    clair  : texte #1C1C1E/#FFF 17:1 · secondaire #6C6C70/#FFF 5.2:1, /#F2F2F7 4.7:1
             accent #5856D6 + texte blanc 5.7:1 · erreur #D70015/#FFF 5.4:1
    sombre : texte #F2F2F7/#1C1C1E 15:1 · secondaire #98989F/#1C1C1E 5.9:1
             accent #5E5CE6 + texte blanc 5.1:1 · lien #7D7AFF/#1C1C1E 4.9:1
             erreur #FF6961/#1C1C1E 6.0:1
*/
:root {
  color-scheme: light dark;

  --bg: #F2F2F7;
  --surface: #FFFFFF;
  --surface-2: #F2F2F7;
  --fill: #E5E5EA;
  --label: #1C1C1E;
  --label-2: #6C6C70;
  --separator: #C6C6C8;
  --accent: #5856D6;
  --accent-pressed: #4B49C8;
  --accent-text: #5856D6;
  --on-accent: #FFFFFF;
  --danger: #D70015;
  --success: #1E7A34;
  --bg-elevated: #F2F2F7;
  --surface-elevated: #FFFFFF;
  --badge-bg: #E5E5EA;
  --badge-fg: #48484A;
  --scrim: rgb(0 0 0 / 0.32);
  --shadow: 0 10px 40px rgb(0 0 0 / 0.18);

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI Variable Text", "Segoe UI",
    Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-display: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI Variable Display",
    "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  /* Échelle typographique iOS (rem = 16px navigateur, multiplié par le zoom système) */
  --t-large: 2.125rem;   /* 34 — Large Title */
  --t-title: 1.375rem;   /* 22 — Title 2 */
  --t-headline: 1.0625rem; /* 17 semibold */
  --t-body: 1.0625rem;   /* 17 */
  --t-subhead: 0.9375rem; /* 15 */
  --t-footnote: 0.8125rem; /* 13 */

  --radius: 14px;
  --gutter: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1C1C1E;
    --surface-2: #2C2C2E;
    --fill: #2C2C2E;
    --label: #F2F2F7;
    --label-2: #98989F;
    --separator: #38383A;
    --accent: #5E5CE6;
    --accent-pressed: #4E4CD0;
    --accent-text: #7D7AFF;
    --danger: #FF6961;
    --success: #30D158;
    --bg-elevated: #1C1C1E;
    --surface-elevated: #2C2C2E;
    --badge-bg: #2C2C2E;
    --badge-fg: #C7C7CC;
    --scrim: rgb(0 0 0 / 0.55);
    --shadow: 0 10px 40px rgb(0 0 0 / 0.6);
  }
}

@media (prefers-contrast: more) {
  :root { --label-2: #48484A; --separator: #8E8E93; }
  @media (prefers-color-scheme: dark) {
    :root { --label-2: #C7C7CC; --separator: #8E8E93; }
  }
}

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

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--label);
  font: 400 var(--t-body)/1.41 var(--font);
  -webkit-font-smoothing: antialiased;
}

[hidden] { display: none !important; }

a { color: var(--accent-text); }

:focus-visible {
  outline: 3px solid var(--accent-text);
  outline-offset: 2px;
  border-radius: 8px;
}

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- Structure ---------- */
.page {
  max-width: 600px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 48px) var(--gutter) calc(env(safe-area-inset-bottom) + 40px);
}
.page--wide { max-width: 1000px; padding-top: calc(env(safe-area-inset-top) + 32px); }

.large-title {
  font: 700 var(--t-large)/1.2 var(--font-display);
  letter-spacing: -0.02em;
  margin: 0 0 8px;
  text-wrap: balance;
}
.intro {
  color: var(--label-2);
  font-size: var(--t-body);
  margin: 0 0 32px;
  max-width: 34em;
}

/* ---------- Sections groupées (inset grouped) ---------- */
.group { margin: 0 0 28px; border: 0; padding: 0; min-width: 0; }
.group-header {
  display: block;
  padding: 0 var(--gutter) 6px;
  color: var(--label-2);
  font-size: var(--t-footnote);
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.group-footer {
  padding: 6px var(--gutter) 0;
  margin: 0;
  color: var(--label-2);
  font-size: var(--t-footnote);
}
.cells {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
}
.cells--split { display: grid; grid-template-columns: 1fr 1fr; }
.cells--split > .cell + .cell { border-left: 0.5px solid var(--separator); }
.cells--split > .cell + .cell::before { display: none; }

.cell {
  position: relative;
  display: block;
  padding: 10px var(--gutter) 10px;
  min-height: 44px;
}
.cell + .cell::before {
  content: "";
  position: absolute;
  top: 0; left: var(--gutter); right: 0;
  border-top: 0.5px solid var(--separator);
}
.cell-label {
  display: block;
  font-size: var(--t-footnote);
  font-weight: 600;
  color: var(--label-2);
  margin-bottom: 2px;
}
.cell-label .optional { font-weight: 400; }

.field {
  display: block;
  width: 100%;
  border: 0;
  padding: 2px 0;
  margin: 0;
  background: transparent;
  color: var(--label);
  font: 400 var(--t-body)/1.41 var(--font); /* ≥ 16px : pas de zoom auto sur iOS */
  outline: none;
  border-radius: 0;
  -webkit-appearance: none;
  appearance: none;
}
.field::placeholder { color: var(--label-2); opacity: 0.85; }
.cell:focus-within { box-shadow: inset 3px 0 0 var(--accent-text); }
textarea.field { resize: vertical; min-height: 9.5em; }

.cell.invalid { box-shadow: inset 3px 0 0 var(--danger); }
.field-error {
  display: flex;
  gap: 6px;
  align-items: flex-start;
  margin: 6px 0 2px;
  color: var(--danger);
  font-size: var(--t-footnote);
}
.field-error svg { flex: none; margin-top: 1px; }

.counter {
  display: block;
  text-align: right;
  font-size: var(--t-footnote);
  color: var(--label-2);
  font-variant-numeric: tabular-nums;
}

/* ---------- Boutons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 20px;
  border: 0;
  border-radius: 999px;
  font: 600 var(--t-body)/1.2 var(--font);
  cursor: pointer;
  text-decoration: none;
  transition: background-color 0.15s ease, transform 0.15s ease, opacity 0.15s ease;
  -webkit-tap-highlight-color: transparent;
  white-space: nowrap;
}
.btn:active:not(:disabled) { transform: scale(0.98); }
.btn:disabled { cursor: not-allowed; opacity: 0.45; }

.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover:not(:disabled) { background: var(--accent-pressed); }

.btn-secondary { background: var(--fill); color: var(--accent-text); }
.btn-secondary:hover:not(:disabled) { filter: brightness(0.96); }

.btn-plain { background: transparent; color: var(--accent-text); padding: 0 12px; }
.btn-plain:hover:not(:disabled) { background: var(--fill); }

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

.btn-block { width: 100%; min-height: 50px; }

.spinner {
  width: 18px; height: 18px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Bannière d'erreur globale ---------- */
.banner {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 12px var(--gutter);
  margin: 0 0 20px;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--danger);
  font-size: var(--t-subhead);
}

/* ---------- Écran de confirmation ---------- */
.done {
  text-align: center;
  padding: 72px var(--gutter) 40px;
}
.done-mark { color: var(--success); margin: 0 auto 20px; display: block; }
.done-mark circle { stroke-dasharray: 160; stroke-dashoffset: 160; animation: draw 0.5s ease-out forwards; }
.done-mark path { stroke-dasharray: 40; stroke-dashoffset: 40; animation: draw 0.35s 0.35s ease-out forwards; }
@keyframes draw { to { stroke-dashoffset: 0; } }
.done h1 { font: 700 var(--t-title)/1.25 var(--font-display); margin: 0 0 8px; }
.done p { color: var(--label-2); margin: 0 auto 32px; max-width: 26em; }

.foot {
  margin-top: 40px;
  text-align: center;
  font-size: var(--t-footnote);
  color: var(--label-2);
}
.foot a { color: var(--label-2); }

/* ---------- Espace équipe ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px 16px;
  margin-bottom: 20px;
}
.toolbar .large-title { margin: 0; }
.toolbar-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.subtitle { color: var(--label-2); font-size: var(--t-subhead); margin: 2px 0 0; }

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  margin-bottom: 12px;
}
.search {
  position: relative;
  flex: 1 1 260px;
}
.search svg {
  position: absolute; left: 10px; top: 50%; transform: translateY(-50%);
  color: var(--label-2); pointer-events: none;
}
.search input {
  width: 100%;
  min-height: 40px;
  padding: 8px 12px 8px 36px;
  border: 0;
  border-radius: 12px;
  background: var(--fill);
  color: var(--label);
  font: 400 var(--t-body)/1.2 var(--font);
  -webkit-appearance: none;
}
.search input::placeholder { color: var(--label-2); }

.segmented {
  display: inline-grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  padding: 2px;
  border-radius: 10px;
  background: var(--fill);
  flex: 0 1 auto;
}
.segmented button {
  min-height: 36px;
  padding: 0 14px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--label);
  font: 500 var(--t-subhead)/1 var(--font);
  cursor: pointer;
  white-space: nowrap;
}
.segmented button[aria-pressed="true"] {
  background: var(--surface);
  font-weight: 600;
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.12), 0 0 0 0.5px rgb(0 0 0 / 0.04);
}
@media (prefers-color-scheme: dark) {
  .segmented button[aria-pressed="true"] { background: #636366; }
}
.segmented .n { color: var(--label-2); font-variant-numeric: tabular-nums; margin-left: 4px; }
.segmented button[aria-pressed="true"] .n { color: inherit; }

.export-note {
  display: flex; gap: 8px; align-items: flex-start;
  margin: 0 0 20px;
  padding: 0 4px;
  color: var(--label-2);
  font-size: var(--t-footnote);
}
.export-note svg { flex: none; margin-top: 1px; }

.list { list-style: none; margin: 0; padding: 0; }
.row {
  position: relative;
  display: grid;
  grid-template-columns: 40px 1fr auto;
  gap: 12px;
  align-items: center;
  width: 100%;
  min-height: 64px;
  padding: 10px var(--gutter);
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.row:hover { background: var(--surface-2); }
.list li + li .row::before {
  content: ""; position: absolute; top: 0; left: calc(var(--gutter) + 52px); right: 0;
  border-top: 0.5px solid var(--separator);
}
.avatar {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  background: linear-gradient(180deg, #8E8E93, #636366);
  color: #fff;
  font: 600 0.9375rem/1 var(--font);
  letter-spacing: 0.02em;
}
.row-main { min-width: 0; }
.row-name, .row-sub { display: block; }
.row-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub {
  color: var(--label-2); font-size: var(--t-subhead);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.row-meta {
  display: flex; flex-direction: column; align-items: flex-end; gap: 4px;
  color: var(--label-2); font-size: var(--t-footnote);
  font-variant-numeric: tabular-nums;
}
.chev { color: var(--separator); }

.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--badge-bg);
  color: var(--badge-fg);
  font-size: 0.75rem; /* 12 */
  font-weight: 600;
  white-space: nowrap;
}

.empty {
  text-align: center;
  padding: 56px var(--gutter);
  color: var(--label-2);
}
.empty h2 { color: var(--label); font: 600 var(--t-headline)/1.3 var(--font); margin: 12px 0 4px; }
.empty p { margin: 0 auto 20px; max-width: 28em; font-size: var(--t-subhead); }

@media (max-width: 560px) {
  .row { grid-template-columns: 40px 1fr; }
  .row-meta { grid-column: 2; flex-direction: row; align-items: center; justify-content: flex-start; }
  .chev { display: none; }
  .segmented { width: 100%; }
  .toolbar-actions { width: 100%; }
  .toolbar-actions .btn-primary { flex: 1; }
}

/* ---------- Connexion ---------- */
.login { max-width: 400px; margin: 0 auto; padding: calc(env(safe-area-inset-top) + 15vh) var(--gutter) 40px; }

/* ---------- Feuilles (dialog) ---------- */
dialog.sheet {
  --bg: var(--bg-elevated);
  --surface: var(--surface-elevated);
  width: min(560px, 100%);
  max-height: min(92vh, 900px);
  padding: 0;
  border: 0;
  border-radius: 20px;
  background: var(--bg);
  color: var(--label);
  box-shadow: var(--shadow);
  overflow: hidden;
}
dialog.sheet[open] { display: flex; flex-direction: column; animation: sheet-in 0.28s cubic-bezier(.2,.8,.2,1); }
dialog.sheet::backdrop { background: var(--scrim); }
@keyframes sheet-in { from { transform: translateY(24px); opacity: 0; } }

.sheet-bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 8px 8px;
  background: var(--bg);
  border-bottom: 0.5px solid var(--separator);
}
.sheet-bar h2 { margin: 0; font: 600 var(--t-headline)/1.2 var(--font); text-align: center; }
.sheet-bar > :first-child { justify-self: start; }
.sheet-bar > :last-child { justify-self: end; }
.sheet-body { overflow-y: auto; padding: 20px var(--gutter) 24px; }

.detail-head { text-align: center; margin-bottom: 24px; }
.detail-head .avatar { width: 72px; height: 72px; margin: 0 auto 12px; font-size: 1.5rem; }
.detail-head h3 { font: 700 var(--t-title)/1.25 var(--font-display); margin: 0 0 6px; overflow-wrap: anywhere; }
.detail-value { overflow-wrap: anywhere; }
.detail-project { white-space: pre-wrap; overflow-wrap: anywhere; }

.confirm { text-align: center; padding: 4px 0 0; }
.confirm p { margin: 0 0 12px; color: var(--label-2); font-size: var(--t-subhead); }
.confirm-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

@media (max-width: 600px) {
  dialog.sheet {
    margin: auto 0 0;
    width: 100%;
    max-width: 100%;
    border-radius: 20px 20px 0 0;
    max-height: 94vh;
    padding-bottom: env(safe-area-inset-bottom);
  }
  @keyframes sheet-in { from { transform: translateY(100%); } }
  .cells--split { grid-template-columns: 1fr; }
  .cells--split > .cell + .cell { border-left: 0; }
  .cells--split > .cell + .cell::before { display: block; }
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%; bottom: calc(env(safe-area-inset-bottom) + 24px);
  transform: translateX(-50%);
  display: flex; gap: 8px; align-items: center;
  padding: 12px 18px;
  border-radius: 999px;
  background: var(--label);
  color: var(--bg);
  font-size: var(--t-subhead);
  font-weight: 600;
  box-shadow: var(--shadow);
  z-index: 10;
  animation: toast-in 0.25s ease-out;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 12px); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .done-mark circle, .done-mark path { stroke-dashoffset: 0; }
}
