:root {
  --bg: #f4f1ea; --paper: #fffdf8; --ink: #1d2a3a; --muted: #6b7380; --line: #ddd6c8;
  --accent: #1f5f8b; --accent-2: #e9f1f7; --ok: #2f7d4f; --warn: #b7791f; --err: #b83232;
  --radius: 12px; --tabbar: 62px; --safe-b: env(safe-area-inset-bottom, 0px);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 15px/1.5 var(--sans); color: var(--ink); background: var(--bg); display: flex; flex-direction: column; overflow: hidden; }
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* Barre du haut */
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 8px 14px; background: var(--ink); color: #fff; padding-top: max(8px, env(safe-area-inset-top)); }
.brand { display: flex; gap: 10px; align-items: center; min-width: 0; }
.brand div { display: flex; flex-direction: column; min-width: 0; }
.brand small { opacity: .7; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 60vw; }
.logo { font: 700 24px var(--serif); width: 34px; height: 34px; display: grid; place-items: center; border: 1.5px solid #fff6; border-radius: 8px; }
.topbar .btn.ghost { color: #fff; border-color: #fff4; }

/* Étapes */
.phases { display: flex; gap: 6px; overflow-x: auto; padding: 8px 10px; background: var(--paper); border-bottom: 1px solid var(--line); scrollbar-width: none; }
.phases::-webkit-scrollbar { display: none; }
.phase { flex: 0 0 auto; display: flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 999px; border: 1px solid var(--line); background: #fff; font-size: 13px; white-space: nowrap; cursor: pointer; }
.phase b { width: 20px; height: 20px; border-radius: 50%; display: grid; place-items: center; background: var(--line); font-size: 11px; }
.phase.on { border-color: var(--accent); background: var(--accent-2); color: var(--accent); font-weight: 600; }
.phase.on b { background: var(--accent); color: #fff; }
.phase.done b { background: var(--ok); color: #fff; }

/* Mise en page : mobile = un onglet à la fois */
.layout { flex: 1; min-height: 0; position: relative; }
.panel { position: absolute; inset: 0; display: none; flex-direction: column; background: var(--paper); padding-bottom: calc(var(--tabbar) + var(--safe-b)); }
.panel.on { display: flex; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.panel-head h2 { font: 600 17px var(--serif); margin: 0; }
.panel-head h2 small { font: 500 12px var(--sans); color: var(--muted); margin-left: 6px; }
.head-actions { display: flex; gap: 6px; align-items: center; }
.panel-body { flex: 1; overflow-y: auto; padding: 14px; -webkit-overflow-scrolling: touch; }

/* Plein écran (bouton ⤢) */
.panel.plein { position: fixed; inset: 0; z-index: 50; display: flex; padding-bottom: var(--safe-b); }
.panel.plein .expand::after { content: ""; }

/* Barre d'onglets du bas */
.tabbar { position: fixed; left: 0; right: 0; bottom: 0; height: calc(var(--tabbar) + var(--safe-b)); padding-bottom: var(--safe-b); display: flex; background: #fff; border-top: 1px solid var(--line); z-index: 40; }
.tabbar button { flex: 1; border: 0; background: none; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 11px; color: var(--muted); gap: 2px; cursor: pointer; }
.tabbar button span { font-size: 19px; }
.tabbar button.on { color: var(--accent); font-weight: 600; }
.tabbar .badge { position: relative; }

/* Boutons */
.btn { border: 1px solid transparent; border-radius: 10px; padding: 9px 14px; cursor: pointer; background: #fff; display: inline-flex; align-items: center; gap: 6px; justify-content: center; min-height: 40px; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:disabled { opacity: .5; cursor: wait; }
.btn.ghost { border-color: var(--line); background: transparent; }
.btn.danger { border-color: #e6b8b8; color: var(--err); background: #fff; }
.btn.small { padding: 5px 10px; min-height: 32px; font-size: 13px; }
.btn.file { position: relative; overflow: hidden; }
.btn.file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.icon { border: 0; background: transparent; font-size: 18px; width: 36px; height: 36px; border-radius: 8px; cursor: pointer; }
.icon:hover { background: var(--accent-2); }
.row { display: flex; gap: 10px; align-items: center; margin: 10px 0; }
.row.wrap { flex-wrap: wrap; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 6px; color: var(--ink); }
select, input[type=text], textarea { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: #fff; width: 100%; }
.card { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; background: #fff; margin-bottom: 14px; }
.card h3 { margin: 0 0 6px; font: 600 16px var(--serif); }
.hint { color: var(--muted); font-size: 13px; margin: 6px 0 10px; }
.hint.small { font-size: 12px; }

/* Discussion */
.chat { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 12px; -webkit-overflow-scrolling: touch; }
.msg { max-width: 92%; padding: 10px 13px; border-radius: 14px; position: relative; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.bot { align-self: flex-start; background: #fff; border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.msg.bot .zoom { position: absolute; top: 4px; right: 4px; width: 28px; height: 28px; font-size: 14px; opacity: .5; }
.msg.bot .zoom:hover { opacity: 1; }
.msg.bot.long { max-height: 340px; overflow: hidden; }
.msg.bot.long::after { content: ""; position: absolute; left: 0; right: 0; bottom: 0; height: 60px; background: linear-gradient(transparent, #fff); border-radius: 0 0 14px 14px; pointer-events: none; }
.msg h1, .msg h2, .msg h3, .msg h4 { font: 600 15px var(--serif); margin: 10px 0 4px; }
.msg p { margin: 6px 0; } .msg ul, .msg ol { margin: 6px 0; padding-left: 20px; }
.msg code { background: var(--accent-2); padding: 1px 4px; border-radius: 4px; }
.msg.phase-note { align-self: center; background: transparent; border: 0; color: var(--muted); font-size: 12px; padding: 2px; }
.typing::after { content: "▍"; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: 0; } }
.phase-done { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 8px 14px; background: #eaf5ee; border-top: 1px solid #cfe6d8; color: var(--ok); font-weight: 600; }
.composer { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); background: #fff; }
.composer textarea { resize: none; max-height: 40vh; min-height: 42px; }
.composer .btn { min-width: 46px; }
.welcome { text-align: center; color: var(--muted); padding: 24px 10px; }
.welcome h3 { font: 600 20px var(--serif); color: var(--ink); }
.suggest { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 5px 12px; font-size: 13px; cursor: pointer; }
.chip.on { background: var(--ink); color: #fff; border-color: var(--ink); }

/* Figures */
.fig-list { display: grid; gap: 12px; margin-top: 14px; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.fig { border: 1px solid var(--line); border-radius: var(--radius); background: #fff; overflow: hidden; }
.fig img { width: 100%; height: 170px; object-fit: contain; background: #fff; border-bottom: 1px solid var(--line); cursor: zoom-in; }
.fig .fig-body { padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.fig .tag { font: 600 14px var(--serif); }
.fig .meta { font-size: 12px; color: var(--muted); }
.fig .meta .bad { color: var(--warn); font-weight: 600; }
.fig .row { margin: 0; flex-wrap: wrap; gap: 6px; }

/* Éditeur du brevet */
.editeur .bloc { margin-bottom: 16px; }
.editeur .bloc-head { display: flex; justify-content: space-between; align-items: center; }
.editeur .bloc-head h4 { margin: 0; font: 600 15px var(--serif); }
.editeur textarea { min-height: 140px; font: 14px/1.55 var(--serif); }
.editeur .compteur { font-size: 12px; color: var(--muted); text-align: right; }
.editeur .compteur.bad { color: var(--err); font-weight: 600; }
.vide { color: var(--muted); text-align: center; padding: 30px 10px; }
.progress { margin-top: 10px; font-size: 13px; }
.progress div { padding: 3px 0; }

/* Aperçu façon document INPI */
.doc { font: 15px/1.6 "Times New Roman", Times, serif; background: #fff; padding: 22px 18px; max-width: 760px; margin: 0 auto; box-shadow: 0 1px 6px #0002; }
.doc .ident { text-align: center; font-weight: 700; font-size: 20px; margin: 18px 0 10px; }
.doc .titre { text-align: center; font-weight: 700; margin-bottom: 14px; }
.doc .p { display: grid; grid-template-columns: 60px 1fr; gap: 6px; margin: 0 0 8px; text-align: justify; }
.doc .p .num { color: #555; }
.doc .p.nonnum { grid-template-columns: 60px 1fr; }
.doc .rev { display: grid; grid-template-columns: 130px 1fr; gap: 6px; margin-bottom: 8px; }
.doc .rev .sub { padding-left: 14px; }
.doc .figp { text-align: center; margin: 16px 0; }
.doc .figp img { max-width: 100%; max-height: 420px; }

/* Audit */
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 8px; margin: 12px 0; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.stat b { display: block; font: 600 20px var(--serif); }
.stat small { color: var(--muted); font-size: 12px; }
.stat.err b { color: var(--err); } .stat.warn b { color: var(--warn); } .stat.ok b { color: var(--ok); }
.filters { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.audit-list { display: flex; flex-direction: column; gap: 8px; }
.constat { background: #fff; border: 1px solid var(--line); border-left: 4px solid var(--muted); border-radius: 10px; padding: 10px 12px; }
.constat.erreur { border-left-color: var(--err); } .constat.alerte { border-left-color: var(--warn); } .constat.info { border-left-color: var(--accent); }
.constat .c-head { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.constat .g { font-weight: 700; text-transform: uppercase; font-size: 11px; }
.constat.erreur .g { color: var(--err); } .constat.alerte .g { color: var(--warn); } .constat.info .g { color: var(--accent); }
.constat .pb { margin: 4px 0; }
.constat .corr { font-size: 13px; color: #2c4a2f; }
.constat .src { font-size: 11px; color: var(--muted); }
.ia-tag { background: #efe7fb; color: #5b3a9c; border-radius: 4px; padding: 0 5px; font-size: 11px; }

/* Modale */
.modal { position: fixed; inset: 0; background: #0007; z-index: 100; display: flex; align-items: stretch; justify-content: center; }
.modal-box { background: var(--paper); width: 100%; max-width: 900px; display: flex; flex-direction: column; margin: 0; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--line); padding-top: max(10px, env(safe-area-inset-top)); }
.modal-head h2 { margin: 0; font: 600 17px var(--serif); }
.modal-body { flex: 1; overflow: auto; padding: 14px; padding-bottom: calc(14px + var(--safe-b)); }
.modal-body img.zoomimg { max-width: 100%; display: block; margin: auto; background: #fff; }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar) + var(--safe-b) + 12px); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 120; max-width: 90vw; font-size: 14px; }

/* Tablette / ordinateur : discussion à gauche, onglet actif à droite */
@media (min-width: 1000px) {
  body { overflow: hidden; }
  .layout { display: grid; grid-template-columns: minmax(380px, 44%) 1fr; }
  .panel { position: relative; inset: auto; padding-bottom: 0; border-right: 1px solid var(--line); }
  #tab-chat { display: flex; }
  .panel:not(#tab-chat).on { display: flex; }
  .tabbar { position: static; height: auto; padding: 0; order: -1; border-top: 0; border-bottom: 1px solid var(--line); justify-content: flex-start; }
  .tabbar button { flex: 0 0 auto; flex-direction: row; padding: 8px 16px; font-size: 14px; gap: 6px; }
  .tabbar button[data-go="chat"] { display: none; }
  .tabbar button span { font-size: 15px; }
  .modal { align-items: center; }
  .modal-box { margin: 3vh 0; max-height: 94vh; border-radius: 14px; }
  .toast { bottom: 20px; }
}
@media (min-width: 1000px) {
  .topbar { order: -3; } .phases { order: -2; } .tabbar { order: -1; }
}

/* Consommation */
.top-actions { display: flex; gap: 8px; align-items: center; }
.conso { border: 1px solid #fff4; background: #ffffff14; color: #fff; border-radius: 999px; padding: 6px 11px; font-size: 12.5px; cursor: pointer; white-space: nowrap; font-variant-numeric: tabular-nums; }
.conso.live { border-color: #ffd36b; color: #ffe49c; }
.conso-table { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
.conso-table th, .conso-table td { padding: 7px 6px; border-bottom: 1px solid var(--line); text-align: right; }
.conso-table th:first-child, .conso-table td:first-child { text-align: left; }
.conso-table tfoot td { font-weight: 700; border-top: 2px solid var(--ink); }
.msg .tok { display: block; margin-top: 6px; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
@media (max-width: 420px) { .brand small { max-width: 34vw; } .conso { padding: 5px 8px; } }
@media (max-width: 420px) { .brand b { font-size: 14px; white-space: nowrap; } .logo { width: 30px; height: 30px; font-size: 20px; } }
