:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #eef0ec;
  --text: #1c2321;
  --muted: #66706c;
  --border: #dde1dc;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --accent-soft: #d7efeb;
  --warn-soft: #fdf0d8;
  --danger: #b42318;
  --shadow: 0 1px 2px rgb(0 0 0 / .06), 0 4px 16px rgb(0 0 0 / .06);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121615;
    --surface: #1b211f;
    --surface-2: #242b29;
    --text: #e8ecea;
    --muted: #97a29e;
    --border: #2f3835;
    --accent: #2dd4bf;
    --accent-text: #06201d;
    --accent-soft: #163a36;
    --warn-soft: #3a2f16;
    --danger: #f87171;
    --shadow: 0 1px 2px rgb(0 0 0 / .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; overflow-x: hidden; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button, a { touch-action: manipulation; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); margin: 0; font-size: 15px; }
.hint { color: var(--muted); font-size: 14px; margin: 0; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 14px; }
h2 { margin: 0; font-size: 19px; }

.logo { font-size: 22px; margin: 0; letter-spacing: -.02em; color: var(--accent); }

/* Formulaires : 16 px minimum, sinon l'iPhone zoome sur le champ et la page bouge */
label, .field { display: flex; flex-direction: column; gap: 5px; font-size: 14px; color: var(--muted); min-width: 0; }
.field-label { font-size: 14px; }
input, select, textarea {
  font: inherit; font-size: 16px; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 11px 12px;
  width: 100%; min-width: 0; max-width: 100%; min-height: 46px;
}
input[type="date"] { -webkit-appearance: none; appearance: none; display: block; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

.btn {
  font: inherit; font-size: 16px; font-weight: 600; cursor: pointer; border-radius: 10px; padding: 10px 16px; min-height: 44px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); white-space: nowrap;
}
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn.small { padding: 6px 12px; font-size: 14px; min-height: 36px; }
.btn.block { width: 100%; }
.btn.icon { padding: 10px; display: inline-grid; place-items: center; }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.stack { display: flex; flex-direction: column; gap: 14px; }

.avatar {
  width: 76px; height: 76px; border-radius: 50%; display: grid; place-items: center; flex: none;
  color: #fff; font-weight: 700; font-size: 32px; background: var(--accent);
}
.avatar.small { width: 32px; height: 32px; font-size: 15px; }
.avatar.placeholder { background: transparent; color: var(--muted); border: 2px dashed var(--border); font-weight: 400; }

/* Choix du profil */
.hub { max-width: 560px; margin: 0 auto; padding: 0 16px 32px; min-height: 100dvh; }
.hub-header { text-align: center; padding: max(48px, env(safe-area-inset-top)) 0 28px; }
.hub-header .logo { font-size: 34px; margin-bottom: 4px; }
.profiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
.profile-card {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 22px 12px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  color: inherit; text-decoration: none; font: inherit; cursor: pointer; box-shadow: var(--shadow);
  transition: transform .15s;
}
.profile-card:active { transform: scale(.97); }
.profile-card.new { border-style: dashed; background: transparent; box-shadow: none; }
.profile-name { font-weight: 600; text-align: center; word-break: break-word; }
.profile-card .lock { position: absolute; top: 10px; right: 12px; font-size: 14px; opacity: .6; }

/* Fenêtres (code, nouveau profil, export) */
.modal {
  border: none; border-radius: var(--radius); padding: 20px; background: var(--surface); color: var(--text);
  width: min(380px, calc(100vw - 32px));
}
.modal::backdrop, .page::backdrop { background: rgb(0 0 0 / .45); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.pin-form { display: flex; flex-direction: column; align-items: center; gap: 10px; text-align: center; }
.pin-form p { margin: 0; }
.pin-input {
  font: 600 30px/1 ui-monospace, monospace; letter-spacing: .5em; text-align: center;
  width: 8.5ch; padding-left: .6em;
}
.pin-form .modal-actions { width: 100%; justify-content: center; margin-top: 6px; }
.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-6px); } 75% { transform: translateX(6px); } }

/* En-tête et filtres */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 12px 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px);
}
.who { display: flex; align-items: center; gap: 8px; color: inherit; text-decoration: none; font-weight: 600; min-width: 0; }
.who span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-actions { display: flex; align-items: center; }
.toolbar, .list, .empty { max-width: 760px; margin: 0 auto; padding: 0 16px; }
.toolbar { display: flex; flex-direction: column; gap: 10px; padding-bottom: 12px; }
.summary strong { font-size: 28px; letter-spacing: -.02em; margin-right: 4px; }
.search-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.search-row select { width: auto; max-width: 44vw; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chips.scroll { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; margin: 0 -16px; padding: 0 16px; }
.chips.scroll::-webkit-scrollbar { display: none; }
.chip {
  font: inherit; font-size: 15px; cursor: pointer; border-radius: 999px; padding: 7px 13px; flex: none;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.chip .count { opacity: .6; }

/* Liste */
.list { display: flex; flex-direction: column; gap: 8px; padding-bottom: 120px; }
.month { display: flex; justify-content: space-between; margin: 10px 2px 0; font-size: 14px; font-weight: 600; color: var(--muted); }
.month span:first-child::first-letter { text-transform: uppercase; }
.receipt {
  display: grid; grid-template-columns: 52px 1fr auto; gap: 12px; align-items: center;
  padding: 10px 14px 10px 10px; cursor: pointer; text-align: left; width: 100%; border: none; font: inherit; color: inherit;
}
.thumb {
  width: 52px; height: 52px; border-radius: 10px; object-fit: cover; background: var(--surface-2);
  display: grid; place-items: center; font-size: 12px; font-weight: 700; color: var(--muted);
}
.receipt .main { min-width: 0; display: flex; flex-direction: column; align-items: flex-start; gap: 2px; }
.receipt .shop { font-weight: 600; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.receipt .meta { font-size: 14px; color: var(--muted); }
.receipt .amount { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.badge { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--text); }
.empty { text-align: center; color: var(--muted); padding-top: 40px; }

/* Bouton d'ajout */
.fab-wrap {
  position: fixed; left: 0; right: 0; bottom: 0; display: flex; justify-content: center; gap: 8px;
  padding: 12px 16px; padding-bottom: max(16px, env(safe-area-inset-bottom));
  background: linear-gradient(transparent, var(--bg) 45%); pointer-events: none;
}
.fab {
  pointer-events: auto; font: inherit; font-weight: 700; font-size: 17px; cursor: pointer; border: none;
  border-radius: 999px; padding: 15px 24px; background: var(--accent); color: var(--accent-text); box-shadow: var(--shadow);
}
.fab.secondary { background: var(--surface); color: var(--text); padding: 15px 17px; }

/* Fiche d'un ticket : page plein écran sur téléphone, fenêtre sur ordinateur.
   Seul le contenu défile, verticalement : rien ne bouge sur les côtés. */
.page {
  border: none; padding: 0; background: var(--bg); color: var(--text);
  width: 100%; max-width: 100%; height: 100%; max-height: 100%; margin: 0;
  overflow: hidden; overscroll-behavior: contain;
}
.page[open] { display: flex; }
.page form { display: flex; flex-direction: column; width: 100%; min-width: 0; }
.page-head {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 8px;
  padding: 8px 10px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.page-head h2 { text-align: center; font-size: 17px; }
.page-body {
  flex: 1; overflow-y: auto; overflow-x: hidden; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: 16px; padding-bottom: max(24px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px;
}
@media (min-width: 640px) {
  .page { width: 560px; height: min(860px, calc(100dvh - 48px)); margin: auto; border-radius: var(--radius); }
}

.photo-box { display: flex; align-items: center; gap: 14px; }
.photo-box img, .pdf-badge {
  width: 110px; height: 146px; object-fit: cover; border-radius: 10px; background: var(--surface-2); display: block;
}
.pdf-badge { display: grid; place-items: center; font-weight: 700; color: var(--muted); }

.scan-status { font-size: 15px; padding: 11px 13px; border-radius: 10px; background: var(--surface-2); }
.scan-status:empty { display: none; }
.scan-status.ok { background: var(--accent-soft); }
.scan-status.warn { background: var(--warn-soft); }
.scan-status.busy::before {
  content: ""; display: inline-block; width: 13px; height: 13px; margin-right: 8px; vertical-align: -1px;
  border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.fallback { display: flex; flex-direction: column; gap: 8px; }
.manual-id { display: grid; grid-template-columns: 1fr auto; gap: 8px; }

.row2 { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }

.items summary { cursor: pointer; font-weight: 600; font-size: 15px; padding: 4px 0; }
.items table { width: 100%; border-collapse: collapse; margin-top: 6px; font-size: 14px; table-layout: fixed; }
.items td { padding: 5px 0; border-bottom: 1px solid var(--border); overflow-wrap: anywhere; }
.items td.num { text-align: right; white-space: nowrap; padding-left: 8px; width: 4.5em; font-variant-numeric: tabular-nums; }
.items td.num:last-child { width: 5.5em; }

/* Scan en direct : seuls les boutons captent les touchers, le reste laisse passer */
.scanner {
  border: none; padding: 0; margin: 0; width: 100%; height: 100%; max-width: 100%; max-height: 100%;
  background: #000; overflow: hidden; color: #fff;
}
.scanner video, .scan-frame, .scan-hint, .scan-msg { pointer-events: none; }
.scanner video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scan-frame {
  position: absolute; left: 50%; top: 42%; width: min(70vw, 60vh); aspect-ratio: 1; transform: translate(-50%, -50%);
  border: 3px solid #fff; border-radius: 18px; box-shadow: 0 0 0 100vmax rgb(0 0 0 / .45);
}
.scan-hint {
  position: absolute; left: 16px; right: 16px; top: max(20px, env(safe-area-inset-top)); margin: 0;
  text-align: center; font-weight: 600; text-shadow: 0 1px 3px rgb(0 0 0 / .6);
}
.scan-msg {
  position: absolute; left: 16px; right: 16px; bottom: calc(max(24px, env(safe-area-inset-bottom)) + 104px); margin: 0;
  text-align: center; font-size: 15px; padding: 10px 14px; border-radius: 12px; background: rgb(0 0 0 / .65);
}
.scan-msg.busy::before {
  content: ""; display: inline-block; width: 13px; height: 13px; margin-right: 8px; vertical-align: -1px;
  border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite;
}
.scan-bar {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 1;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px;
  padding: 16px 20px max(24px, env(safe-area-inset-bottom));
}
.scan-bar #scanClose { justify-self: start; background: rgb(0 0 0 / .5); color: #fff; border-color: rgb(255 255 255 / .4); }
.shutter {
  width: 76px; height: 76px; border-radius: 50%; cursor: pointer; padding: 0;
  background: #fff; border: 5px solid rgb(255 255 255 / .45); background-clip: padding-box;
  box-shadow: 0 0 0 2px #fff;
}
.shutter:active { transform: scale(.92); }
.shutter:disabled { opacity: .5; }

.toast {
  position: fixed; left: 50%; bottom: 100px; transform: translateX(-50%); z-index: 20;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 15px;
  box-shadow: var(--shadow); max-width: calc(100vw - 32px); text-align: center;
}
.page:focus { outline: none; }
