:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --ink: #1a1f29;
  --muted: #6b7280;
  --line: #e5e7eb;
  --brand: #1f6feb;
  --brand-dark: #1a5fd0;
  --danger: #d64545;
  --ok: #1a9e5f;
  --warn: #e08a00;
  --radius: 14px;
  --bar-h: 54px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  overscroll-behavior-y: none;
}
h1, h2, h3 { margin: 0 0 .4em; }
.muted { color: var(--muted); font-size: .86rem; }
.err { color: var(--danger); font-size: .86rem; min-height: 1.1em; margin: .4em 0 0; }

/* ---- Barre supérieure ---- */
#appbar {
  position: sticky; top: 0; z-index: 40;
  height: calc(var(--bar-h) + var(--safe-top));
  padding-top: var(--safe-top);
  display: flex; align-items: center; justify-content: space-between;
  padding-left: 8px; padding-right: 8px;
  background: var(--brand); color: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.15);
}
.bar-left, .bar-right { display: flex; align-items: center; gap: 6px; min-width: 0; }
.logo { font-weight: 700; font-size: 1.05rem; }
.bar-title { font-weight: 600; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 52vw; }
.icon-btn {
  background: rgba(255,255,255,.14); color: #fff; border: none;
  width: 38px; height: 38px; border-radius: 10px; font-size: 1.4rem; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer;
}
.icon-btn:disabled { opacity: .4; }
.icon-btn:active { background: rgba(255,255,255,.28); }

.sync-badge {
  font-size: .72rem; font-weight: 600; padding: 4px 9px; border-radius: 999px;
  background: rgba(255,255,255,.2); color: #fff; display: inline-flex; align-items: center; gap: 5px;
}
.sync-badge.ok { background: rgba(26,158,95,.9); }
.sync-badge.sync { background: rgba(255,255,255,.28); }
.sync-badge.off { background: rgba(0,0,0,.28); }
.sync-badge.warn { background: rgba(224,138,0,.95); }
.sync-badge.small { font-size: .68rem; padding: 2px 7px; }
.pending-count { background: #fff; color: var(--brand); border-radius: 999px; padding: 0 6px; font-size: .68rem; }

/* ---- Boutons & champs ---- */
.btn {
  appearance: none; border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: 12px 16px; border-radius: 12px; font-size: .95rem; font-weight: 600;
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:active { background: var(--brand-dark); }
.btn.small { padding: 8px 12px; font-size: .85rem; border-radius: 10px; }
.btn.block { width: 100%; margin-top: 8px; }
.btn.danger-outline { color: var(--danger); border-color: #f0c4c4; background: #fff; }
.link-btn { background: none; border: none; color: var(--brand); font-size: .85rem; cursor: pointer; padding: 4px; }
.link-btn.danger { color: var(--danger); }

input, textarea {
  width: 100%; padding: 13px 14px; border: 1px solid var(--line); border-radius: 12px;
  font-size: 1rem; background: #fff; color: var(--ink); margin-bottom: 10px; font-family: inherit;
}
input:focus, textarea:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(31,111,235,.15); }

/* ---- Vues génériques ---- */
#app { min-height: calc(100vh - var(--bar-h)); }
.loading, .empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.empty.small { padding: 24px; font-size: .9rem; }
.view { padding: 14px 14px calc(90px + var(--safe-bottom)); max-width: 760px; margin: 0 auto; }
.view-head h2 { font-size: 1.3rem; }

.list { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.list-row {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; cursor: pointer;
}
.list-row:active { background: #f0f3f8; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; font-size: 1rem; }
.row-sub { color: var(--muted); font-size: .82rem; margin-top: 2px; }
.chevron { color: var(--muted); font-size: 1.5rem; }

.section { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 14px; }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.section-head h3 { margin: 0; font-size: 1.05rem; }
.members { display: flex; flex-direction: column; gap: 6px; }
.member { display: flex; align-items: center; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--line); }
.member:last-child { border-bottom: none; }

.fab {
  position: fixed; right: 18px; bottom: calc(18px + var(--safe-bottom)); z-index: 30;
  width: 58px; height: 58px; border-radius: 50%; border: none; background: var(--brand); color: #fff;
  font-size: 2rem; line-height: 1; box-shadow: 0 4px 14px rgba(31,111,235,.4); cursor: pointer;
}
.fab:active { background: var(--brand-dark); }

/* ---- Auth ---- */
.auth { display: flex; align-items: center; justify-content: center; padding: 24px 16px; min-height: 70vh; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 26px; width: 100%; max-width: 380px; box-shadow: 0 6px 24px rgba(0,0,0,.06); }
.card h1 { font-size: 1.6rem; }
.tabs { display: flex; gap: 6px; margin: 16px 0; background: #eef1f5; padding: 4px; border-radius: 12px; }
.tab { flex: 1; border: none; background: none; padding: 10px; border-radius: 9px; font-weight: 600; color: var(--muted); cursor: pointer; }
.tab.active { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.1); }

/* ---- Visualiseur PDF ---- */
.plan-view { display: flex; flex-direction: column; height: calc(100vh - var(--bar-h) - var(--safe-top)); }
.pdf-toolbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  background: #fff; border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.tb-group { display: flex; align-items: center; gap: 4px; }
.tb-group .icon-btn { background: #eef1f5; color: var(--ink); width: 34px; height: 34px; font-size: 1.2rem; }
#pg-info { font-size: .82rem; color: var(--muted); min-width: 44px; text-align: center; }
.add-toggle { margin-left: auto; background: #fff; border-color: var(--brand); color: var(--brand); padding: 8px 14px; }
.add-toggle.active { background: var(--brand); color: #fff; }

.pdf-container { flex: 1; position: relative; overflow: hidden; background: #52565c; }
.pdf-scroller { position: absolute; inset: 0; overflow: auto; -webkit-overflow-scrolling: touch; touch-action: pan-x pan-y; display: flex; }
.pdf-page-wrap { position: relative; margin: auto; box-shadow: 0 2px 12px rgba(0,0,0,.4); background: #fff; }
.pdf-page-wrap canvas { display: block; }
.pdf-overlay { position: absolute; inset: 0; }
.pdf-overlay.add-mode { cursor: crosshair; background: rgba(31,111,235,.05); }

.pl-marker {
  position: absolute; transform: translate(-50%, -50%);
  min-width: 30px; height: 30px; padding: 0 6px; border-radius: 999px;
  background: var(--danger); color: #fff; border: 2px solid #fff;
  font-size: .8rem; font-weight: 700; cursor: pointer;
  box-shadow: 0 1px 5px rgba(0,0,0,.4); display: inline-flex; align-items: center; justify-content: center;
}
.pl-marker.pending { background: var(--warn); }
.pl-marker-badge {
  position: absolute; top: -7px; right: -7px; background: var(--brand); color: #fff;
  font-size: .62rem; min-width: 16px; height: 16px; border-radius: 999px; border: 1.5px solid #fff;
  display: inline-flex; align-items: center; justify-content: center; padding: 0 3px;
}
.plan-hint {
  position: absolute; left: 50%; bottom: calc(16px + var(--safe-bottom)); transform: translateX(-50%);
  background: rgba(0,0,0,.78); color: #fff; padding: 9px 16px; border-radius: 999px; font-size: .85rem; z-index: 20;
}

/* ---- Feuilles modales ---- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 60; background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  background: #fff; width: 100%; max-width: 560px; max-height: 88vh; overflow-y: auto;
  border-radius: 20px 20px 0 0; padding: 20px 18px calc(22px + var(--safe-bottom));
  animation: slideUp .22s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
@media (min-width: 620px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 20px; }
}
.file-drop { display: block; border: 1.5px dashed var(--line); border-radius: 12px; padding: 16px; text-align: center; color: var(--muted); margin-bottom: 10px; cursor: pointer; }
.file-drop input { display: none; }
.progress, .upload-progress { font-size: .85rem; color: var(--brand); padding: 8px 0; }

.marker-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.marker-pin { width: 34px; height: 34px; border-radius: 999px; background: var(--danger); color: #fff; font-weight: 700; display: inline-flex; align-items: center; justify-content: center; flex: none; }
.marker-label { width: 70px; margin: 0; padding: 8px; text-align: center; }
.marker-note { min-height: 64px; resize: vertical; }
.photo-actions { display: flex; gap: 8px; margin-bottom: 12px; }
.photo-actions .btn { flex: 1; }

.photo-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 14px; }
@media (min-width: 460px) { .photo-grid { grid-template-columns: repeat(4, 1fr); } }
.photo-cell { position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden; background: #eef1f5; cursor: pointer; }
.photo-cell img { width: 100%; height: 100%; object-fit: cover; display: block; }
.pending-dot { position: absolute; top: 5px; right: 5px; width: 10px; height: 10px; border-radius: 50%; background: var(--warn); border: 2px solid #fff; }

/* ---- Visionneuse plein écran ---- */
.lightbox {
  position: fixed; inset: 0; z-index: 80; background: rgba(0,0,0,.94);
  display: grid; grid-template-columns: auto 1fr auto; grid-template-rows: 1fr auto;
  align-items: center;
}
.lb-stage { grid-column: 1 / 4; grid-row: 1; display: flex; align-items: center; justify-content: center; overflow: hidden; padding: 8px; }
.lb-stage img { max-width: 100%; max-height: 100%; object-fit: contain; }
.lb-close { position: fixed; top: calc(10px + var(--safe-top)); right: 12px; z-index: 2; background: rgba(255,255,255,.15); color: #fff; border: none; width: 42px; height: 42px; border-radius: 50%; font-size: 1.2rem; cursor: pointer; }
.lb-nav { grid-row: 1; z-index: 2; background: rgba(255,255,255,.12); color: #fff; border: none; width: 46px; height: 70px; font-size: 2rem; cursor: pointer; align-self: center; }
.lb-nav.prev { grid-column: 1; border-radius: 0 12px 12px 0; }
.lb-nav.next { grid-column: 3; border-radius: 12px 0 0 12px; }
.lb-bar { grid-column: 1 / 4; grid-row: 2; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px calc(14px + var(--safe-bottom)); color: #fff; }
.lb-bar .muted { color: #bbb; }

/* ---- Toasts ---- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); transform: translate(-50%, 20px);
  background: #222; color: #fff; padding: 11px 18px; border-radius: 12px; font-size: .9rem; z-index: 100;
  opacity: 0; transition: all .25s ease; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.warn { background: var(--warn); }

/* ---- Administration ---- */
.chip {
  display: inline-block; font-size: .7rem; font-weight: 600; padding: .1em .55em;
  border-radius: 999px; background: var(--line); color: var(--muted); vertical-align: middle;
}
.chip.off { background: #fde2e2; color: var(--danger); }
.admin-label { display: block; margin: .95rem 0 .35rem; font-weight: 600; font-size: .82rem; color: var(--muted); }
.checks { display: flex; flex-direction: column; gap: .4rem; margin-bottom: .7rem; }
.check { display: flex; align-items: center; gap: .5rem; font-size: .95rem; }
.check input { width: auto; }
.row-inline { display: flex; gap: .5rem; align-items: center; margin: .4rem 0 .9rem; }
.row-inline select { flex: 1; min-width: 0; padding: 9px 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); font-size: .95rem; }
.row-inline .btn { flex: 0 0 auto; }
