:root {
  --navy: #12355b;
  --navy-2: #0b2440;
  --red: #c62828;
  --amber: #f6c343;
  --ink: #17202b;
  --ink-2: #4a5565;
  --ink-3: #6b7685;
  --line: #d9dfe6;
  --bg: #eef1f5;
  --card: #fff;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --footer-h: 52px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }
body { font: 17px/1.4 var(--font); color: var(--ink); background: var(--bg); -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; touch-action: manipulation; -webkit-text-size-adjust: 100%; }
button { font: inherit; color: inherit; touch-action: manipulation; }
input { font: inherit; -webkit-user-select: text; user-select: text; }
[hidden] { display: none !important; }
#app { position: fixed; inset: 0 0 calc(var(--footer-h) + var(--safe-bottom)) 0; display: flex; flex-direction: column; }

.splash { margin: auto; text-align: center; color: var(--ink-3); display: flex; flex-direction: column; gap: 14px; align-items: center; }

/* Emergency footer */
.emergency { position: fixed; left: 0; right: 0; bottom: 0; min-height: calc(var(--footer-h) + var(--safe-bottom)); padding: 0 20px var(--safe-bottom); background: var(--red); color: #fff; font-weight: 650; font-size: 17px; display: flex; align-items: center; justify-content: center; text-align: center; z-index: 40; }

/* Header */
.topbar { background: var(--navy); color: #fff; padding: calc(12px + var(--safe-top)) 20px 12px; display: flex; align-items: center; gap: 14px; flex: none; }
.topbar .logo { width: 38px; height: 38px; flex: none; }
.topbar .title { font-size: 21px; font-weight: 700; letter-spacing: -.01em; line-height: 1.15; }
.topbar .subtitle { font-size: 13px; color: #b9c8d8; font-weight: 500; }
.topbar .spacer { flex: 1; }
.status { display: flex; align-items: center; gap: 8px; font-size: 13px; color: #d3dde8; background: rgba(255,255,255,.1); padding: 6px 12px; border-radius: 20px; white-space: nowrap; }
.status .dot { width: 9px; height: 9px; border-radius: 50%; background: #3ecf7a; flex: none; }
.status.offline .dot { background: var(--amber); }
.status.syncing .dot { background: #6fb3ff; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* Search and filters */
.controls { background: #fff; border-bottom: 1px solid var(--line); padding: 14px 20px 10px; flex: none; }
.searchbox { position: relative; }
.searchbox input { width: 100%; height: 56px; border-radius: 12px; border: 2px solid #c6d0db; padding: 0 56px 0 52px; font-size: 20px; background: #f8fafc; color: var(--ink); -webkit-appearance: none; appearance: none; }
.searchbox input:focus { outline: none; border-color: var(--navy); background: #fff; }
.searchbox .icon { position: absolute; left: 17px; top: 17px; width: 22px; height: 22px; color: var(--ink-3); pointer-events: none; }
.searchbox .clear { position: absolute; right: 8px; top: 8px; width: 40px; height: 40px; border: 0; border-radius: 50%; background: #dfe5ec; font-size: 20px; color: var(--ink-2); }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 10px 0 2px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; height: 40px; padding: 0 16px; border-radius: 20px; border: 1.5px solid var(--line); background: #fff; font-size: 15px; font-weight: 600; color: var(--ink-2); display: inline-flex; align-items: center; gap: 6px; }
.chip.on { background: var(--navy); border-color: var(--navy); color: #fff; }
.chip.picto { padding: 0 10px 0 4px; }
.chip.picto img { width: 32px; height: 32px; }
.chip-label { font-size: 12px; font-weight: 700; color: var(--ink-3); text-transform: uppercase; letter-spacing: .05em; align-self: center; flex: none; margin-right: 2px; }

/* Results */
.results-wrap { flex: 1; position: relative; min-height: 0; display: flex; }
.results { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 6px 20px 24px; }
.count { color: var(--ink-3); font-size: 14px; padding: 8px 2px 4px; }
.letter { font-size: 15px; font-weight: 800; color: var(--navy); padding: 14px 4px 6px; position: sticky; top: -6px; background: var(--bg); z-index: 1; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); gap: 10px; }
.item { display: flex; align-items: center; gap: 12px; text-align: left; width: 100%; min-height: 84px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; box-shadow: 0 1px 2px rgba(16,24,40,.05); }
.item:active { background: #e8eef5; transform: scale(.995); }
.item .info { flex: 1; min-width: 0; }
.item .name { font-weight: 700; font-size: 18px; line-height: 1.25; overflow-wrap: anywhere; }
.item .mfr { color: var(--ink-3); font-size: 14px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .meta { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.item .pictos { display: flex; flex-wrap: wrap; gap: 0; justify-content: flex-end; max-width: 132px; flex: none; }
.item .pictos img { width: 40px; height: 40px; }
.item .chev { color: #a5b0bd; font-size: 26px; flex: none; }
.badge { display: inline-block; font-size: 12px; font-weight: 800; padding: 2px 9px; border-radius: 10px; letter-spacing: .04em; }
.badge.danger { background: var(--red); color: #fff; }
.badge.warning { background: var(--amber); color: #3a2a00; }
.tagline { font-size: 13px; color: var(--ink-3); white-space: nowrap; }
.item .meta { flex-wrap: wrap; }
.empty { text-align: center; color: var(--ink-3); padding: 60px 20px; font-size: 18px; }
.empty strong { display: block; color: var(--ink); font-size: 21px; margin-bottom: 6px; }

.az { width: 34px; flex: none; display: flex; flex-direction: column; justify-content: space-evenly; align-items: center; padding: 6px 0; font-size: 12px; font-weight: 800; color: var(--navy); background: #e3e8ee; touch-action: none; }
.az span { padding: 1px 6px; }
.az span.dim { color: #a9b4c0; }

/* Enrollment */
.enroll { margin: auto; width: min(520px, calc(100% - 40px)); background: #fff; border-radius: 16px; padding: 34px 34px 30px; box-shadow: 0 10px 30px rgba(16,24,40,.12); text-align: center; }
.enroll img { width: 64px; height: 64px; }
.enroll h1 { font-size: 26px; margin: 14px 0 6px; }
.enroll p { color: var(--ink-2); margin: 0 0 20px; }
.enroll input { width: 100%; height: 64px; text-align: center; font: 700 30px ui-monospace, SFMono-Regular, Menlo, monospace; letter-spacing: .15em; text-transform: uppercase; border: 2px solid #c6d0db; border-radius: 12px; }
.enroll input:focus { outline: none; border-color: var(--navy); }
.enroll .err { color: var(--red); font-weight: 600; min-height: 24px; margin: 10px 0 4px; }
.btn-primary { width: 100%; height: 58px; border: 0; border-radius: 12px; background: var(--navy); color: #fff; font-size: 19px; font-weight: 700; }
.btn-primary:disabled { opacity: .6; }

/* Viewer */
.viewer { position: fixed; inset: 0 0 calc(var(--footer-h) + var(--safe-bottom)) 0; background: #525b66; display: flex; flex-direction: column; z-index: 30; }
.vbar { background: var(--navy); color: #fff; padding: calc(10px + var(--safe-top)) 14px 10px; display: flex; align-items: center; gap: 12px; flex: none; }
.back { height: 52px; min-width: 120px; padding: 0 18px 0 12px; border-radius: 10px; border: 0; background: #fff; color: var(--navy); font-size: 18px; font-weight: 800; display: inline-flex; align-items: center; gap: 4px; flex: none; }
.back:active { background: #dfe7f0; }
.vtitle { flex: 1; min-width: 0; }
.vtitle .name { font-size: 20px; font-weight: 700; line-height: 1.2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.vtitle .mfr { font-size: 13px; color: #b9c8d8; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.vpictos { display: flex; gap: 2px; flex: none; align-items: center; }
.vpictos img { width: 46px; height: 46px; }
.sections { background: #fff; border-bottom: 1px solid var(--line); display: flex; gap: 8px; padding: 10px 14px; overflow-x: auto; flex: none; scrollbar-width: none; align-items: center; }
.sections::-webkit-scrollbar { display: none; }
.sec { flex: none; height: 46px; padding: 0 14px; border-radius: 10px; border: 1.5px solid var(--line); background: #f6f8fb; font-size: 15px; font-weight: 700; color: var(--ink); display: inline-flex; align-items: center; gap: 7px; }
.sec .n { display: inline-flex; width: 24px; height: 24px; border-radius: 6px; background: var(--navy); color: #fff; font-size: 13px; align-items: center; justify-content: center; }
.sec.urgent { border-color: #e9b4b4; background: #fff3f3; }
.sec.urgent .n { background: var(--red); }
.sec:disabled { opacity: .38; }
.sec:active:not(:disabled) { background: #e3eaf3; }
.sections .sep { width: 1px; align-self: stretch; background: var(--line); flex: none; margin: 4px 2px; }
.zoom { flex: none; width: 46px; height: 46px; border-radius: 10px; border: 1.5px solid var(--line); background: #f6f8fb; font-size: 24px; font-weight: 700; }
.pages { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; padding: 14px 0 40px; touch-action: pan-x pan-y; }
.page { margin: 0 auto 14px; background: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.3); position: relative; }
.page canvas { display: block; width: 100%; height: 100%; }
.page .pno { position: absolute; right: 8px; bottom: -22px; font-size: 12px; color: #d3d9e0; }
.vmsg { margin: auto; color: #fff; text-align: center; padding: 30px; font-size: 18px; }
.vmsg .sub { color: #cfd6de; font-size: 15px; margin-top: 8px; }
.pageind { position: absolute; left: 50%; transform: translateX(-50%); bottom: 16px; background: rgba(15,25,40,.8); color: #fff; padding: 6px 14px; border-radius: 16px; font-size: 14px; font-weight: 600; pointer-events: none; transition: opacity .3s; }

@media (max-width: 700px) {
  .grid { grid-template-columns: 1fr; }
  .topbar .subtitle, .status .label-long { display: none; }
  .vpictos { display: none; }
  .back { min-width: 0; }
}

.tbadge { display: inline-block; font-size: 12px; font-weight: 800; padding: 2px 8px; border-radius: 5px; background: #e3e9f0; color: #22374f; letter-spacing: .04em; }
.tbadge.sds { background: #fdecec; color: var(--red); }

.nbadge { display: inline-block; font-size: 11px; font-weight: 800; padding: 2px 8px; border-radius: 10px; background: #1e7a46; color: #fff; letter-spacing: .05em; }
.nbadge.upd { background: #1f5fa8; }
