:root {
  --bg: #ffffff;
  --surface: #f3f4f6;
  --surface-2: #e7e9ed;
  --text: #16181c;
  --muted: #646b76;
  --border: #d9dce1;
  --accent: #b3122e;
  --accent-text: #ffffff;
  --user-bubble: #16181c;
  --user-text: #ffffff;
  --danger: #b3261e;
  --ok: #1d7a46;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181c;
    --surface: #22252b;
    --surface-2: #2d3138;
    --text: #eceef1;
    --muted: #9aa1ab;
    --border: #383d45;
    --accent: #e0405a;
    --accent-text: #ffffff;
    --user-bubble: #eceef1;
    --user-text: #16181c;
    --danger: #ff8a80;
    --ok: #6fcf97;
    color-scheme: dark;
  }
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }

/* --- Connexion ------------------------------------------------------------ */

.screen { min-height: 100dvh; }

#login, section.screen:not(#chat) {
  display: grid;
  place-items: center;
  padding: calc(env(safe-area-inset-top) + 24px) 20px calc(env(safe-area-inset-bottom) + 24px);
}

.login-card {
  width: 100%;
  max-width: 380px;
  text-align: center;
}

.logo {
  width: 64px; height: 64px;
  margin: 0 auto 12px;
  border-radius: 16px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 36px; font-weight: 700; line-height: 64px;
}

.login-card h1 { font-size: 1.4rem; margin: 0 0 24px; }
.login-card form { display: grid; gap: 10px; text-align: left; }
.login-card label { font-weight: 600; font-size: .9rem; }

.login-card input {
  width: 100%;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  font-size: 17px; /* ≥ 16px : pas de zoom automatique sur iPhone */
}

#code { letter-spacing: .4em; text-align: center; font-size: 22px; }

.primary {
  padding: 14px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
  width: 100%;
}
.primary:disabled { opacity: .6; }

.link { background: none; border: 0; color: var(--accent); padding: 8px; display: inline-block; text-decoration: none; }
.hint { color: var(--muted); font-size: .95rem; margin: 0 0 6px; }
.error { color: var(--danger); margin-top: 14px; }

/* --- Assistant ------------------------------------------------------------ */

#chat { display: flex; flex-direction: column; height: 100dvh; }

.topbar {
  position: relative;
  display: flex; align-items: center; gap: 6px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.topbar .title { font-weight: 700; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.ghost {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: .9rem;
}
.ghost.icon { font-size: 1.2rem; line-height: 1; padding: 6px 12px; }

.menu {
  position: absolute; right: 12px; top: calc(100% + 4px); z-index: 10;
  min-width: 220px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgb(0 0 0 / .15);
  padding: 6px;
  display: grid;
}
.menu > * { padding: 10px; border: 0; background: none; text-align: left; border-radius: 8px; color: var(--text); text-decoration: none; }
.menu > button:hover, .menu > a:hover { background: var(--surface); }
.menu-email { color: var(--muted); font-size: .85rem; overflow-wrap: anywhere; }

main#messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 14px 8px;
  display: flex; flex-direction: column; gap: 14px;
}

.empty { margin: auto 0; text-align: center; color: var(--text); padding: 0 12px; }
.empty p { margin: 6px 0; }

.msg { max-width: 100%; }
.msg.user {
  align-self: flex-end;
  max-width: 85%;
  background: var(--user-bubble);
  color: var(--user-text);
  padding: 10px 14px;
  border-radius: 18px 18px 4px 18px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.user img { display: block; max-width: 180px; border-radius: 10px; margin-bottom: 6px; }

.msg.bot { align-self: stretch; overflow-wrap: anywhere; }
.msg.bot .status { color: var(--muted); font-size: .9rem; display: flex; align-items: center; gap: 8px; }
.msg.bot .status::before {
  content: ""; width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid var(--muted); border-top-color: transparent;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.answer h1, .answer h2, .answer h3, .answer h4 { font-size: 1.05rem; margin: 14px 0 6px; }
.answer p { margin: 8px 0; }
.answer ul, .answer ol { margin: 8px 0; padding-left: 22px; }
.answer li { margin: 3px 0; }
.answer blockquote { margin: 8px 0; padding: 2px 12px; border-left: 3px solid var(--border); color: var(--muted); }
.answer code { background: var(--surface); padding: 1px 5px; border-radius: 5px; font-size: .9em; }
.answer hr { border: 0; border-top: 1px solid var(--border); margin: 12px 0; }
.answer .error { margin: 8px 0; }

.cite {
  display: inline-block;
  min-width: 1.6em;
  padding: 0 6px;
  margin: 0 1px;
  border: 0;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--accent);
  font-size: .78rem; font-weight: 700; line-height: 1.6;
  vertical-align: 1px;
}

.sources { margin-top: 12px; display: grid; gap: 6px; }
.sources-title { font-size: .8rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.source {
  display: flex; gap: 10px; align-items: flex-start;
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}
.source .n { font-weight: 700; color: var(--accent); min-width: 1.4em; }
.source .doc { flex: 1; font-size: .9rem; }
.source .page { color: var(--muted); font-size: .85rem; white-space: nowrap; }

.feedback { display: flex; align-items: center; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.feedback button { border: 1px solid var(--border); background: var(--bg); border-radius: 999px; padding: 4px 12px; font-size: 1.05rem; }
.feedback button.on { border-color: var(--accent); background: var(--surface); }
.feedback .thanks { color: var(--ok); font-size: .9rem; }
.feedback-comment { display: grid; gap: 6px; margin-top: 8px; }
.feedback-comment textarea { width: 100%; padding: 10px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); font-size: 16px; }
.feedback-comment button { justify-self: start; border: 0; background: var(--accent); color: var(--accent-text); border-radius: 10px; padding: 8px 14px; }

/* --- Zone de saisie ------------------------------------------------------- */

.composer {
  border-top: 1px solid var(--border);
  background: var(--bg);
  padding: 8px 10px calc(env(safe-area-inset-bottom) + 8px);
}

#ask-form { display: flex; align-items: flex-end; gap: 8px; }

#question {
  flex: 1;
  resize: none;
  max-height: 140px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--surface);
  font-size: 16px;
  line-height: 1.4;
}

.photo-btn, .send {
  flex: none;
  width: 44px; height: 44px;
  display: grid; place-items: center;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg);
}
.send { background: var(--accent); border-color: var(--accent); }
.send:disabled { opacity: .5; }
.photo-btn svg, .send svg { width: 22px; height: 22px; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.photo-btn svg { stroke: var(--text); }
.send svg { stroke: var(--accent-text); }

.preview { position: relative; display: inline-block; margin: 0 0 8px 52px; }
.preview img { height: 64px; border-radius: 10px; display: block; }
.preview button {
  position: absolute; top: -8px; right: -8px;
  width: 26px; height: 26px; border-radius: 50%;
  border: 0; background: var(--text); color: var(--bg); font-size: .8rem;
}

/* --- Visionneuse ------------------------------------------------------------ */

.viewer {
  position: fixed; inset: 0; z-index: 50;
  background: var(--bg);
  display: flex; flex-direction: column;
}
.viewer-bar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  border-bottom: 1px solid var(--border);
}
.viewer-title { font-size: .85rem; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.viewer-body { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; background: var(--surface); text-align: center; touch-action: pan-x pan-y pinch-zoom; }
.viewer-body img { max-width: 100%; height: auto; background: #fff; }
.viewer-body p { padding: 24px; }
.viewer-nav {
  display: flex; align-items: center; justify-content: center; gap: 14px;
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px);
  border-top: 1px solid var(--border);
}
.viewer-nav button { width: 44px; height: 40px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); font-size: 1.4rem; line-height: 1; }
.viewer-nav button:disabled { opacity: .35; }
.viewer-nav a { color: var(--accent); font-size: .9rem; text-decoration: none; margin-left: auto; }
#viewer-page { font-size: .9rem; color: var(--muted); min-width: 70px; text-align: center; }
