:root {
  --bg: #f5f3ef;
  --card: #ffffff;
  --ink: #1f2933;
  --muted: #6b7280;
  --accent: #2f6f4f;
  --accent-2: #3d8b66;
  --bubble-user: #2f6f4f;
  --bubble-user-ink: #ffffff;
  --bubble-bot: #ffffff;
  --danger: #b3261e;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}

/* ---------- Login ---------- */

.center-body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: 0 8px 30px rgba(31, 41, 51, .1);
  padding: 32px 24px;
  width: 100%;
  max-width: 380px;
  text-align: center;
}

.logo {
  font-size: 42px;
  margin-bottom: 8px;
}

.card h1 {
  margin: 0 0 4px;
  font-size: 24px;
  letter-spacing: .5px;
}

.subtitle {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 14px;
}

.hint {
  color: var(--accent);
  font-size: 13px;
  margin: 0 0 12px;
}

.card form {
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-align: left;
}

.card label {
  font-size: 13px;
  color: var(--muted);
  margin-top: 6px;
}

.card input {
  padding: 12px 14px;
  border: 1.5px solid #d7d3cb;
  border-radius: 10px;
  font-size: 16px;
  background: #fbfaf8;
}

.card input:focus {
  outline: none;
  border-color: var(--accent);
  background: #fff;
}

.card button {
  margin-top: 14px;
  padding: 13px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

.card button:active { transform: scale(.98); }

.error {
  background: #fdecea;
  color: var(--danger);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 14px;
  margin-bottom: 12px;
}

/* ---------- Chat ---------- */

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

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--card);
  border-bottom: 1px solid #e6e2da;
  flex-shrink: 0;
}

.chat-header-info { display: flex; flex-direction: column; min-width: 0; }
.chat-header-info strong { font-size: 15px; }
.chat-header-info span { font-size: 12px; color: var(--muted); }

.header-actions { display: flex; align-items: center; gap: 2px; }

.reset-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 20px;
  padding: 6px 10px;
  cursor: pointer;
  border-radius: 8px;
  transition: background .15s ease, color .15s ease, transform .3s ease;
}

.reset-btn:hover { background: #efece6; color: var(--danger); }

.logout {
  text-decoration: none;
  color: var(--muted);
  font-size: 20px;
  padding: 6px 10px;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg.user {
  align-self: flex-end;
  background: var(--bubble-user);
  color: var(--bubble-user-ink);
  border-bottom-right-radius: 4px;
}

.msg.bot {
  align-self: stretch;
  max-width: none;
  background: transparent;
  border: none;
  padding: 2px 4px;
}

.msg .photo-flag {
  display: block;
  font-size: 12px;
  opacity: .8;
  margin-bottom: 4px;
}

.typing {
  align-self: flex-start;
  display: flex;
  gap: 5px;
  padding: 14px 4px;
  flex-shrink: 0;
}

.typing span {
  width: 7px; height: 7px;
  background: #b9b3a7;
  border-radius: 50%;
  animation: blink 1.2s infinite;
}

.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }

@keyframes blink {
  0%, 80%, 100% { opacity: .25; }
  40% { opacity: 1; }
}

.quickstart {
  display: flex;
  gap: 8px;
  padding: 0 12px 8px;
  flex-shrink: 0;
  flex-wrap: wrap;
}

.quickstart button {
  background: #fff;
  border: 1.5px solid var(--accent-2);
  color: var(--accent);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
}

.chat-input {
  flex-shrink: 0;
  padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
  background: var(--card);
  border-top: 1px solid #e6e2da;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.composer {
  width: 100%;
  max-width: 680px;
  display: flex;
  flex-direction: column;
  background: #fbfaf8;
  border: 1.5px solid #d7d3cb;
  border-radius: 18px;
  box-shadow: 0 2px 10px rgba(31, 41, 51, .06);
  transition: border-color .15s ease, box-shadow .15s ease;
}

.composer:focus-within {
  border-color: var(--accent);
  background: #fff;
  box-shadow: 0 4px 16px rgba(47, 111, 79, .12);
}

.composer textarea {
  width: 100%;
  border: none;
  background: transparent;
  padding: 12px 16px 6px;
  font-size: 16px;
  font-family: inherit;
  line-height: 1.45;
  resize: none;
  overflow-y: auto;
  max-height: 180px;
  color: var(--ink);
}

.composer textarea:focus { outline: none; }

.composer textarea::placeholder { color: #a8a29a; }

.composer-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  padding: 4px 8px 8px;
}

.photo-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px; height: 36px;
  background: transparent;
  border: none;
  border-radius: 10px;
  color: var(--muted);
  cursor: pointer;
  flex-shrink: 0;
  transition: background .15s ease, color .15s ease;
}

.photo-btn:hover { background: #efece6; color: var(--accent); }

.photo-btn input { display: none; }

#send {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px; height: 36px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background .15s ease, transform .1s ease;
}

#send:hover { background: var(--accent-2); }
#send:active { transform: scale(.94); }
#send:disabled { background: #a9c4b5; cursor: default; }

.photo-preview {
  position: relative;
  width: 100%;
  max-width: 680px;
  margin-bottom: 8px;
  flex-shrink: 0;
}

.photo-preview img {
  display: block;
  max-height: 140px;
  border-radius: 12px;
  border: 1px solid #e6e2da;
  object-fit: contain;
}

.photo-remove {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 26px;
  height: 26px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(31, 41, 51, .65);
  color: #fff;
  border: none;
  border-radius: 50%;
  font-size: 13px;
  cursor: pointer;
  transition: background .15s ease;
}

.photo-remove:hover { background: var(--danger); }

.msg-photo {
  display: block;
  max-width: 220px;
  max-height: 180px;
  border-radius: 10px;
  margin-bottom: 6px;
  object-fit: cover;
  cursor: pointer;
}

.photo-preview.hidden, .typing.hidden, .quickstart.hidden { display: none; }

/* ---------- Rendu markdown / LaTeX ---------- */
.msg-body :is(p, ul, ol) { margin: 0 0 8px; }
.msg-body :is(p, ul, ol):last-child { margin-bottom: 0; }
.msg-body ul, .msg-body ol { padding-left: 22px; }
.msg-body li { margin: 4px 0; }
.msg-body strong { font-weight: 700; }
.msg-body em { font-style: italic; }
.msg-body code {
  background: #efece6;
  border-radius: 5px;
  padding: 1px 5px;
  font-size: .9em;
}
.msg-body pre {
  background: #1f2933;
  color: #f5f3ef;
  border-radius: 10px;
  padding: 10px 12px;
  overflow-x: auto;
  font-size: 13px;
}
.msg-body pre code { background: none; padding: 0; color: inherit; }
.msg-body h1, .msg-body h2, .msg-body h3 {
  font-size: 1em;
  margin: 10px 0 6px;
}
.msg-body .katex { font-size: 1.05em; }
.msg-body .katex-display { margin: 10px 0; overflow-x: auto; }

@media (min-width: 700px) {
  .chat-messages { padding: 16px max(12px, calc((100% - 680px) / 2)); }
  .msg.user { max-width: 75%; }
}
