:root {
  --bg: #0f172a;
  --surface: #111827;
  --surface-soft: rgba(17, 24, 39, 0.94);
  --text: #f9fafb;
  --muted: #9ca3af;
  --line: #374151;

  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --danger: #b91c1c;
  --success: #166534;

  --own-bubble: #1d4ed8;
  --own-text: #ffffff;
  --own-pseudo: #bfdbfe;
  --own-date: rgba(255,255,255,0.7);

  --radius-lg: 20px;
  --radius-md: 12px;
  --radius-sm: 10px;

  --shadow-sm: 0 8px 20px rgba(0, 0, 0, 0.25);
  --shadow-md: 0 16px 40px rgba(0, 0, 0, 0.35);

  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

body.theme-light {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-soft: rgba(255, 255, 255, 0.94);
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;

  --own-bubble: #dcf8c6;
  --own-text: #111827;
  --own-pseudo: #1d4ed8;
  --own-date: #6b7280;

  --shadow-sm: 0 8px 20px rgba(17, 24, 39, 0.06);
  --shadow-md: 0 16px 40px rgba(17, 24, 39, 0.10);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

html.font-size-large { font-size: 17.5px; }
html.font-size-xl { font-size: 19px; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  text-size-adjust: 100%;
}

button, input, select { font: inherit; color: inherit; }
button { touch-action: manipulation; }
a { color: inherit; }

.screen { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
.hidden { display: none !important; }

button:focus-visible, input:focus-visible, select:focus-visible, a:focus-visible {
  outline: 3px solid rgba(29, 78, 216, 0.35);
  outline-offset: 2px;
}

.note { color: var(--muted); font-size: 0.875rem; line-height: 1.45; }

/* Authentification */
#auth-screen, #banned-screen {
  justify-content: center; align-items: center; padding: 24px; overflow-y: auto;
  background: radial-gradient(circle at top right, rgba(29,78,216,0.15), transparent 30%), var(--bg);
}

.auth-card {
  width: min(100%, 400px); background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-md); overflow: hidden;
}

.auth-header { padding: 28px 24px 20px; border-bottom: 1px solid var(--line); background: rgba(255,255,255,0.02); }
.brand-mark {
  width: 44px; height: 44px; margin-bottom: 14px; border-radius: 12px; background: #111827;
  color: #fff; display: grid; place-items: center; font-size: 1.1rem; font-weight: 800;
}
body.theme-light .brand-mark { background: #111827; }

.auth-header h1 { font-size: 1.6rem; line-height: 1.1; letter-spacing: -0.02em; }
.subtitle { margin-top: 6px; color: var(--muted); font-size: 0.95rem; }

.auth-form { padding: 24px; display: grid; gap: 14px; }
.field { display: grid; gap: 6px; }
.field label { font-size: 0.875rem; font-weight: 600; color: var(--muted); }
.field input, .field select {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: var(--bg); color: var(--text); transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
.field input::placeholder { color: var(--muted); }
.field input:focus, .field select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 4px rgba(29,78,216,0.15); }

.btn-primary, .btn-secondary, .btn-header {
  border-radius: var(--radius-sm); font-weight: 700; cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease, transform 0.15s ease, box-shadow 0.2s ease;
}
.btn-primary {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: var(--primary); color: #fff; border: 1px solid var(--primary); padding: 12px 14px; text-decoration: none;
  box-shadow: 0 12px 24px rgba(29,78,216,0.25);
}
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); transform: translateY(-1px); }
.btn-secondary {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  background: transparent; color: var(--text); border: 1px solid var(--line); padding: 12px 14px; text-decoration: none;
}
.btn-secondary:hover { background: rgba(255,255,255,0.05); border-color: var(--muted); }
.btn-secondary.small { padding: 6px 10px; font-size: 0.85rem; }

.auth-form .btn-primary, .auth-form .btn-secondary { width: 100%; }

.btn-header {
  flex-shrink: 0; display: inline-flex; align-items: center; justify-content: center;
  padding: 9px 12px; border: 1px solid var(--line); background: transparent; color: var(--text);
  font-size: 0.875rem; font-weight: 600; text-decoration: none;
}
.btn-header:hover { background: rgba(255,255,255,0.05); border-color: var(--muted); }

.btn-primary:active, .btn-secondary:active, .btn-header:active { transform: scale(0.98); }

.msg { min-height: 22px; font-size: 0.9rem; color: var(--danger); line-height: 1.35; }
.msg.success { color: var(--success); }
.msg.error { color: var(--danger); }

/* Chat */
#chat-screen { background: var(--bg); }
header {
  background: var(--surface-soft); backdrop-filter: blur(14px); border-bottom: 1px solid var(--line);
  padding: 12px 16px; display: flex; justify-content: space-between; align-items: center; gap: 12px;
  box-shadow: 0 4px 18px rgba(0,0,0,0.15);
}
.header-info { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
header h1 { font-size: 1.05rem; font-weight: 800; letter-spacing: -0.02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.current-user { color: var(--muted); font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

main {
  flex: 1; overflow-y: auto; overscroll-behavior: contain;
  padding: 18px 14px calc(18px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth; background: radial-gradient(circle at top right, rgba(29,78,216,0.08), transparent 28%), var(--bg);
}
main::-webkit-scrollbar { width: 8px; }
main::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.2); border-radius: 999px; }
main::-webkit-scrollbar-track { background: transparent; }

.loading, .empty-state {
  margin: auto; max-width: min(90%, 460px); text-align: center; color: var(--muted);
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 12px 18px;
  box-shadow: var(--shadow-sm); font-weight: 600;
}

.message { display: flex; align-items: flex-end; gap: 10px; max-width: min(86%, 760px); animation: messageIn 0.22s ease; }
.message.own { margin-left: auto; flex-direction: row-reverse; }
.avatar { width: 36px; height: 36px; border-radius: 10px; object-fit: cover; background: #e5e7eb; border: 1px solid var(--line); flex-shrink: 0; }

.bubble {
  min-width: 0; max-width: 100%; background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 10px 12px 8px; box-shadow: 0 8px 20px rgba(0,0,0,0.15); word-wrap: break-word;
}
.message:not(.own) .bubble { border-top-left-radius: 6px; }
.message.own .bubble { background: var(--own-bubble); border-color: var(--own-bubble); color: var(--own-text); border-top-right-radius: 6px; }

.pseudo { font-size: 0.78rem; font-weight: 700; color: var(--primary); margin-bottom: 4px; }
.message.own .pseudo { color: var(--own-pseudo); }
.bubble p { font-size: 0.97rem; line-height: 1.5; overflow-wrap: anywhere; white-space: pre-wrap; }
.date { margin-top: 6px; font-size: 0.68rem; color: var(--muted); text-align: right; }
.message.own .date { color: var(--own-date); }

.bubble img { display: block; max-width: min(290px, 72vw); max-height: 290px; width: auto; height: auto; object-fit: cover; border-radius: 12px; border: 1px solid var(--line); margin: 6px 0; background: #e5e7eb; }
.bubble audio { display: block; width: min(265px, 68vw); height: 40px; margin: 6px 0; }

/* Réponse */
.reply-block {
  border-left: 3px solid var(--primary); background: rgba(29,78,216,0.15); padding: 6px 10px;
  border-radius: 8px; margin-bottom: 8px; font-size: 0.85rem; color: var(--muted);
}
.message.own .reply-block { border-left-color: var(--own-pseudo); background: rgba(255,255,255,0.15); color: var(--own-date); }
.reply-block-label { font-weight: 700; margin-bottom: 2px; color: var(--primary); }
.message.own .reply-block-label { color: var(--own-pseudo); }

.reply-preview {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  width: min(100%, 920px); margin: 0 auto 8px; padding: 10px 12px; border: 1px solid var(--line);
  border-radius: var(--radius-md); background: var(--surface);
}
.reply-preview-content { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.reply-preview-label { font-size: 0.85rem; color: var(--muted); }
.reply-preview-excerpt { font-size: 0.85rem; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

/* Actions message */
.message-actions { display: flex; gap: 8px; margin-top: 8px; }
.action-btn {
  background: transparent; border: 1px solid var(--line); color: var(--muted);
  font-size: 0.75rem; padding: 4px 8px; border-radius: 999px; cursor: pointer;
}
.action-btn:hover { background: rgba(255,255,255,0.05); color: var(--text); }

.reactions-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.reaction-badge {
  background: var(--surface); border: 1px solid var(--line); color: var(--text);
  padding: 2px 8px; border-radius: 999px; font-size: 0.8rem; cursor: pointer; display: inline-flex; align-items: center; gap: 4px;
}
.reaction-badge.active { border-color: var(--primary); background: rgba(29,78,216,0.2); }
.message.own .reaction-badge { background: rgba(255,255,255,0.2); color: var(--own-text); border-color: rgba(255,255,255,0.3); }

.emoji-picker { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.emoji-btn { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 4px 8px; font-size: 1.1rem; cursor: pointer; }
.emoji-btn:hover { background: rgba(255,255,255,0.1); }

/* Footer */
footer { padding: 10px 12px calc(12px + var(--safe-bottom)); background: var(--surface-soft); backdrop-filter: blur(14px); border-top: 1px solid var(--line); box-shadow: 0 -10px 28px rgba(0,0,0,0.15); }

.chat-status { width: min(100%, 920px); margin: 0 auto 10px; padding: 10px 12px; border-radius: var(--radius-md); border: 1px solid transparent; font-size: 0.9rem; font-weight: 600; }
.chat-status.success { background: #f0fdf4; border-color: #bbf7d0; color: var(--success); }
.chat-status.error { background: #fef2f2; border-color: #fecaca; color: var(--danger); }
.chat-status.info { background: #eff6ff; border-color: #bfdbfe; color: var(--primary); }
body.theme-dark .chat-status.success { background: #052e16; border-color: #166534; color: #dcfce7; }
body.theme-dark .chat-status.error { background: #450a0a; border-color: #b91c1c; color: #fee2e2; }
body.theme-dark .chat-status.info { background: #172554; border-color: #1d4ed8; color: #dbeafe; }

#recording-indicator { width: min(100%, 920px); margin: 0 auto 10px; display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: var(--radius-md); border: 1px solid #fecaca; background: #fef2f2; color: var(--danger); font-size: 0.9rem; font-weight: 600; }
body.theme-dark #recording-indicator { background: #450a0a; border-color: #b91c1c; color: #fee2e2; }
.recording-dot { width: 9px; height: 9px; border-radius: 50%; background: #dc2626; animation: pulse 1.2s infinite; }
#stop-rec-btn { margin-left: auto; border: none; border-radius: 8px; background: var(--danger); color: #fff; padding: 7px 10px; font-size: 0.85rem; font-weight: 700; cursor: pointer; }
#stop-rec-btn:hover { background: #991b1b; }

.input-row { width: min(100%, 920px); margin: 0 auto; display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 6px; box-shadow: var(--shadow-sm); }
.input-row input[type="text"] { flex: 1; min-width: 0; border: none; outline: none; background: transparent; padding: 11px 12px; font-size: 1rem; color: var(--text); }
.input-row input[type="text"]::placeholder { color: var(--muted); }
.input-row button { width: 42px; height: 42px; flex: 0 0 auto; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--text); display: grid; place-items: center; cursor: pointer; transition: background 0.18s ease, border-color 0.18s ease, transform 0.15s ease, opacity 0.18s ease; }
.input-row button:hover { background: rgba(255,255,255,0.05); border-color: var(--muted); }
.input-row button:active { transform: scale(0.95); }
.input-row button:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
#send-btn { background: var(--primary); border-color: var(--primary); color: #fff; box-shadow: 0 10px 22px rgba(29,78,216,0.25); }
#send-btn:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.icon { width: 18px; height: 18px; display: block; }
.is-recording { background: #fef2f2 !important; border-color: #fecaca !important; color: var(--danger) !important; }
body.theme-dark .is-recording { background: #450a0a !important; border-color: #b91c1c !important; color: #fee2e2 !important; }
.is-busy { opacity: 0.65; }

/* Modal paramètres */
.modal { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 20px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.55); }
.modal-card { position: relative; width: min(100%, 560px); max-height: calc(100dvh - 40px); overflow: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-lg); box-shadow: var(--shadow-md); padding: 20px; display: grid; gap: 18px; }
.modal-header { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.modal-header h2 { font-size: 1.2rem; }
.modal-card section { display: grid; gap: 12px; padding: 16px; border: 1px solid var(--line); border-radius: 16px; }
.modal-card h3 { font-size: 1rem; }
.profile-row { display: flex; align-items: center; gap: 12px; }
.avatar-large { width: 64px; height: 64px; border-radius: 16px; object-fit: cover; border: 1px solid var(--line); background: #e5e7eb; }
.profile-actions { display: grid; gap: 8px; }

/* Animations */
@keyframes messageIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(220,38,38,0.30); } 70% { box-shadow: 0 0 0 8px rgba(220,38,38,0); } 100% { box-shadow: 0 0 0 0 rgba(220,38,38,0); } }

/* Responsive */
@media (max-width: 640px) {
  header { padding: 10px 12px; }
  .btn-header { padding: 8px 10px; font-size: 0.8rem; }
  main { padding: 12px 10px calc(14px + var(--safe-bottom)); gap: 10px; }
  .message { max-width: 94%; }
  .bubble { padding: 9px 11px 7px; border-radius: 13px; }
  .bubble p { font-size: 0.95rem; }
  .input-row { gap: 6px; padding: 5px; }
  .input-row button { width: 40px; height: 40px; border-radius: 10px; }
  .auth-header, .auth-form { padding-left: 18px; padding-right: 18px; }
}

@media (min-width: 900px) {
  header { padding-left: 24px; padding-right: 24px; }
  main { padding-left: 24px; padding-right: 24px; }
  footer { padding-left: 24px; padding-right: 24px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}