:root {
  --bg: #f3f5f9;
  --card: #ffffff;
  --text: #1b2330;
  --muted: #5d6878;
  --border: #d9dee7;
  --primary: #1f5fbf;
  --primary-text: #ffffff;
  --success-bg: #e5f5ea; --success: #1d6b36;
  --warn-bg: #fff4dc; --warn: #7a5200;
  --error-bg: #fde8e8; --error: #9b1c1c;
  --chip: #eef2f8;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151c;
    --card: #1a202a;
    --text: #e6eaf0;
    --muted: #9aa5b5;
    --border: #2d3544;
    --primary: #5b95f0;
    --primary-text: #0b1220;
    --success-bg: #173222; --success: #8fe0a9;
    --warn-bg: #3a2e12; --warn: #f3cf7a;
    --error-bg: #3d1a1a; --error: #f5a3a3;
    --chip: #252d3a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--primary); color: var(--primary-text);
}
.topbar h1 {
  font-size: 1.15rem; margin: 0; min-width: 0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar-actions { display: flex; gap: 6px; flex-shrink: 0; }
.logout-btn {
  display: inline-flex; align-items: center; gap: 4px;
  font: inherit; font-size: .85rem; font-weight: 600; color: inherit;
  background: transparent; border: 1px solid currentColor; border-radius: 999px;
  padding: 6px 12px 6px 8px; min-height: 36px; cursor: pointer;
}
.logout-btn svg {
  width: 20px; height: 20px; fill: none; stroke: currentColor;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
/* Petit écran : la déconnexion devient une simple icône. */
@media (max-width: 480px) {
  .topbar h1 { font-size: 1.05rem; }
  .logout-btn { padding: 6px 8px; }
  .logout-btn span { display: none; }
}

/* --- Installation (bouton + fiche d'aide) --- */
.install-trigger {
  display: inline-flex; align-items: center; gap: 4px;
  font: inherit; font-size: .85rem; font-weight: 600; color: inherit;
  background: rgb(255 255 255 / .18); border: none; border-radius: 999px;
  padding: 6px 12px 6px 8px; min-height: 36px; cursor: pointer;
}
.install-trigger svg, .install-dialog svg {
  width: 20px; height: 20px; fill: none; stroke: currentColor;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.install-dialog {
  border: none; padding: 0; background: transparent; color: var(--text);
  width: 100%; max-width: 480px; margin: auto auto 0;
}
.install-dialog::backdrop { background: rgb(0 0 0 / .5); }
.install-dialog .sheet {
  background: var(--card); border-radius: 20px 20px 0 0;
  padding: 20px 16px calc(16px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 16px;
}
@media (min-width: 520px) {
  .install-dialog { margin: auto; }
  .install-dialog .sheet { border-radius: 20px; }
}
.sheet-head { display: flex; gap: 12px; align-items: center; }
.sheet-head img { border-radius: 11px; border: 1px solid var(--border); flex-shrink: 0; }
.sheet-head h2 { margin: 0; font-size: 1.05rem; }
.sheet-head p { margin: 2px 0 0; }
.tabs {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
  background: var(--chip); border-radius: 10px; padding: 3px;
}
.tabs button {
  font: inherit; font-size: .85rem; font-weight: 600; border: none; background: transparent;
  color: var(--muted); border-radius: 8px; padding: 8px 4px; cursor: pointer;
}
.tabs button.active { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / .15); }
.install-dialog ol { margin: 0; padding-left: 1.4em; display: flex; flex-direction: column; gap: 12px; line-height: 1.5; }
.install-dialog ol .small { display: block; }
.install-dialog .ico {
  display: inline-grid; place-items: center; vertical-align: middle;
  width: 28px; height: 28px; border-radius: 7px; background: var(--chip); color: #2f7cf6;
}
.install-dialog .ico svg { width: 18px; height: 18px; }
.install-dialog .note { margin: 0; padding: 10px 12px; border-radius: 10px; background: var(--chip); color: var(--muted); }
.install-dialog .center { text-align: center; margin: 8px 0 0; }
.btn.block { width: 100%; }

main {
  max-width: 720px; margin: 0 auto;
  padding: 16px 16px calc(32px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 16px;
}

.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
}
.card h2 { font-size: 1.05rem; margin: 0 0 12px; }
.auth-card { max-width: 420px; width: 100%; margin: 24px auto 0; }
.auth-card form { display: flex; flex-direction: column; gap: 12px; }
.auth-switch { margin: 14px 0 0; text-align: center; }
.user-line { margin: 0; text-align: right; }

.muted { color: var(--muted); }
.small { font-size: .875rem; }
code { font-size: .9em; }

.field { display: flex; flex-direction: column; gap: 4px; font-size: .9rem; color: var(--muted); }
.field input, .field select {
  font: inherit; font-size: 16px; color: var(--text);
  background: var(--card); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; min-height: 44px; width: 100%;
}
.check { display: flex; gap: 10px; align-items: flex-start; margin: 12px 0 4px; font-size: .95rem; }
.check input { width: 20px; height: 20px; margin-top: 1px; flex: none; }

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 14px; }
.span-2 { grid-column: span 2; }

.btn {
  font: inherit; font-weight: 600; border-radius: 10px; border: 1px solid transparent;
  padding: 10px 16px; min-height: 44px; cursor: pointer;
}
.btn:disabled { opacity: .5; cursor: default; }
.btn-primary { background: var(--primary); color: var(--primary-text); }
.btn-secondary { background: var(--chip); color: var(--text); border-color: var(--border); }
.btn-ghost { background: transparent; color: inherit; border-color: currentColor; min-height: 36px; padding: 6px 12px; font-size: .9rem; }
.btn-link { background: none; color: var(--primary); padding: 8px 4px; min-height: 0; font-weight: 500; }
.btn-icon { background: none; border: none; color: var(--muted); font-size: 1rem; cursor: pointer; padding: 8px; }

.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; }
.actions .btn-primary, .actions .btn-secondary { flex: 1 1 220px; }

.dropzone {
  display: block; position: relative; text-align: center; cursor: pointer;
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 22px 12px;
}
.dropzone.over { border-color: var(--primary); background: var(--chip); }
.dropzone.busy { opacity: .7; pointer-events: none; }
.dropzone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.status { margin: 0 0 12px; padding: 10px 12px; border-radius: 10px; background: var(--chip); }
.status-success { background: var(--success-bg); color: var(--success); }
.status-warn { background: var(--warn-bg); color: var(--warn); }
.status-error { background: var(--error-bg); color: var(--error); }

.shifts { list-style: none; margin: 0; padding: 0; }
.shifts li {
  display: grid; grid-template-columns: 6.6em 3.8em 1fr auto; gap: 8px; align-items: center;
  padding: 9px 2px; border-bottom: 1px solid var(--border); font-size: .95rem;
}
.shifts li:last-child { border-bottom: none; }
.shifts .date { font-weight: 600; white-space: nowrap; text-transform: capitalize; }
.shifts .code { font-family: ui-monospace, monospace; font-size: .85rem; color: var(--muted); }
.shifts .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.shifts .hours { font-variant-numeric: tabular-nums; color: var(--muted); }
.shifts li { grid-template-columns: 6.6em 3.8em 1fr auto 1.6em; }
.shifts .gstate {
  width: 1.5em; height: 1.5em; border-radius: 50%; display: inline-grid; place-items: center;
  font-size: .8rem; font-weight: 700; justify-self: end;
}
.shifts .gstate:empty { visibility: hidden; }
.shifts li[data-etat="present"] .gstate { background: var(--success-bg); color: var(--success); }
.shifts li[data-etat="nouveau"] .gstate { background: var(--chip); color: var(--primary); }
.shifts li[data-etat="maj"] .gstate { background: var(--warn-bg); color: var(--warn); }
.shifts li[data-etat="doublon"] .gstate { background: var(--error-bg); color: var(--error); }
.shifts .gnote { grid-column: 3 / -1; font-size: .8rem; color: var(--error); }
.gcheck { margin: 10px 0 0; color: var(--muted); }
.gcheck .btn-link { padding: 0 2px; }

.unknown { margin-top: 12px; font-size: .9rem; color: var(--muted); display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.chip { font: inherit; font-size: .85rem; border: 1px solid var(--border); background: var(--chip); color: var(--text); border-radius: 999px; padding: 4px 10px; cursor: pointer; }

details summary { cursor: pointer; list-style: none; }
details summary::-webkit-details-marker { display: none; }
details summary h2 { display: inline; }
details summary::after { content: " ▸"; color: var(--muted); }
details[open] summary::after { content: " ▾"; }

.mapping { width: 100%; border-collapse: collapse; font-size: .9rem; }
.mapping th { text-align: left; color: var(--muted); font-weight: 500; padding: 4px; }
.mapping td { padding: 3px; }
.mapping input {
  font: inherit; font-size: 16px; color: var(--text); background: var(--card);
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; width: 100%; min-width: 0;
}
.mapping .in-code { text-transform: uppercase; width: 5.5em; }
.mapping input[type="time"] { width: 6.8em; }
/* iOS impose aux champs horaires sa propre largeur (ils débordent de leur
   cellule) et centre la valeur : on neutralise l'apparence native. */
.mapping input[type="time"] {
  -webkit-appearance: none; appearance: none;
  display: block; min-width: 0; max-width: 100%;
}
.mapping input[type="time"]::-webkit-date-and-time-value { text-align: left; margin: 0; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(16px + env(safe-area-inset-bottom));
  max-width: min(560px, calc(100% - 32px)); z-index: 10;
  padding: 12px 16px; border-radius: 12px; box-shadow: 0 6px 24px rgb(0 0 0 / .2);
  background: var(--text); color: var(--bg);
}
.toast-success { background: var(--success-bg); color: var(--success); }
.toast-warn { background: var(--warn-bg); color: var(--warn); }
.toast-error { background: var(--error-bg); color: var(--error); }

@media (max-width: 520px) {
  /* Postes : date à gauche, libellé puis horaires empilés à droite. */
  .shifts li { grid-template-columns: 6.6em 1fr 1.6em; grid-template-areas: "date label state" "date hours state" "date note note"; row-gap: 0; }
  .shifts .gstate { grid-area: state; }
  .shifts .gnote { grid-area: note; }
  .shifts .date { grid-area: date; }
  .shifts .code { display: none; }
  .shifts .label { grid-area: label; white-space: normal; }
  .shifts .hours { grid-area: hours; font-size: .85rem; }
  /* Codes de poste : une carte par code — code + libellé, puis début + fin. */
  .mapping, .mapping tbody { display: block; }
  .mapping thead { display: none; }
  .mapping tr {
    display: grid; gap: 6px 8px; padding: 10px 0; border-bottom: 1px solid var(--border);
    grid-template-columns: repeat(4, minmax(0, 1fr)) 36px;
    grid-template-areas: "code label label label del" "debut debut fin fin del";
  }
  .mapping td { padding: 0; min-width: 0; }
  .mapping td::before {
    content: attr(data-label); display: block; margin-bottom: 2px;
    font-size: .75rem; color: var(--muted);
  }
  .mapping td:nth-child(1) { grid-area: code; }
  .mapping td:nth-child(2) { grid-area: label; }
  .mapping td:nth-child(3) { grid-area: debut; }
  .mapping td:nth-child(4) { grid-area: fin; }
  .mapping td:nth-child(5) { grid-area: del; align-self: center; }
  .mapping td:nth-child(5)::before { content: none; }
  .mapping input, .mapping .in-code, .mapping input[type="time"] { width: 100%; min-height: 40px; }
}

/* Champ mot de passe avec œil (afficher/masquer) */
.pw { position: relative; display: block; }
.pw input { padding-right: 46px; }
.pw-eye {
  position: absolute; top: 50%; right: 4px; transform: translateY(-50%);
  display: flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; padding: 0; border: 0; border-radius: 8px;
  background: transparent; color: var(--muted); cursor: pointer;
}
.pw-eye:hover, .pw-eye[aria-pressed="true"] { color: var(--text); }
.pw-eye .eye-off, .pw-eye[aria-pressed="true"] .eye-on { display: none; }
.pw-eye[aria-pressed="true"] .eye-off { display: block; }
