:root {
  --bg: #14111f;
  --panel: #1e1a2e;
  --panel-2: #2a2540;
  --text: #f1eefb;
  --muted: #a39dbd;
  --accent: #7c5cff;
  --radius: 18px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --line: #ffffff18; --line2: #ffffff30; --hover: #ffffff12;
  --em: #cfc6ff; --link: #a994ff; --err-text: #ffd0d0; --err-bg: #3a1f2a; --bad: #ff8a8a; --good: #5fd3a0;
  color-scheme: dark;
}
/* Thème clair : choisi dans les réglages, ou automatique selon l'appareil */
:root[data-theme="light"] {
  --bg: #f6f4fb; --panel: #ffffff; --panel-2: #ece8f8; --text: #1d1a2b; --muted: #6a6384; --accent: #6a47f0;
  --line: #00000018; --line2: #00000026; --hover: #0000000d;
  --em: #5a3fd0; --link: #5a3fd0; --err-text: #7a1f1f; --err-bg: #fde8ec; --bad: #c23a3a; --good: #1f8a5f;
  color-scheme: light;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]):not([data-theme="light"]) {
    --bg: #f6f4fb; --panel: #ffffff; --panel-2: #ece8f8; --text: #1d1a2b; --muted: #6a6384; --accent: #6a47f0;
    --line: #00000018; --line2: #00000026; --hover: #0000000d;
    --em: #5a3fd0; --link: #5a3fd0; --err-text: #7a1f1f; --err-bg: #fde8ec; --bad: #c23a3a; --good: #1f8a5f;
    color-scheme: light;
  }
}
/* Taille du texte (réglages) */
:root[data-size="s"] { font-size: 14px; }
:root[data-size="l"] { font-size: 18px; }
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 1rem/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
#app { height: 100%; max-width: 820px; margin: 0 auto; display: flex; flex-direction: column; }

/* Accueil */
.home { padding: calc(24px + var(--safe-t)) 18px 32px; overflow-y: auto; }
.brand { font-size: 2rem; font-weight: 800; letter-spacing: -0.02em; }
.brand span { color: var(--accent); }
.subtitle { color: var(--muted); margin: 4px 0 24px; }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.card {
  display: flex; gap: 14px; align-items: center; text-align: left;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; color: inherit; font: inherit; cursor: pointer; transition: transform .12s, background .12s;
}
.card:hover { background: var(--panel-2); transform: translateY(-2px); }
.avatar {
  flex: none; width: 52px; height: 52px; border-radius: 50%;
  display: grid; place-items: center; font-size: 1.6rem; background: var(--c, var(--accent));
}
.card b { display: block; }
.card small { color: var(--muted); }

/* Chat */
.chat { height: 100%; display: flex; flex-direction: column; }
.chat-head {
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + var(--safe-t)) 14px 10px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.chat-head .avatar { width: 40px; height: 40px; font-size: 1.2rem; }
.chat-head .who { flex: 1; min-width: 0; line-height: 1.25; }
.chat-head small { color: var(--muted); }
.icon-btn {
  background: none; border: 0; color: var(--muted); font-size: 1.3rem; cursor: pointer;
  padding: 8px; border-radius: 10px;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }

.messages { flex: 1; overflow-y: auto; padding: 16px 14px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 85%; padding: 10px 14px; border-radius: var(--radius); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.bot { background: var(--panel-2); border-bottom-left-radius: 6px; align-self: flex-start; }
.msg.me { color: #fff; background: var(--accent); border-bottom-right-radius: 6px; align-self: flex-end; }
.msg em { color: var(--em); }
.msg.me em { color: #fff; }
.msg.typing::after { content: "▋"; animation: blink 1s steps(2) infinite; margin-left: 2px; }
@keyframes blink { 50% { opacity: 0; } }

.composer {
  display: flex; gap: 10px; align-items: flex-end;
  padding: 10px 14px calc(10px + var(--safe-b)); background: var(--panel); border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; resize: none; max-height: 140px; background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 22px; padding: 10px 16px; font: inherit; outline: none;
}
.composer textarea:focus { border-color: var(--accent); }
.composer button {
  flex: none; width: 44px; height: 44px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--accent); color: #fff; font-size: 1.2rem;
}
.composer button:disabled { opacity: .45; cursor: default; }

/* Création de personnage */
.card.new { border-style: dashed; border-color: var(--line2); background: transparent; }
.avatar.plus { background: var(--panel-2); color: var(--accent); font-weight: 700; }
.form-page { height: 100%; display: flex; flex-direction: column; }
.form { flex: 1; overflow-y: auto; padding: 18px 16px calc(28px + var(--safe-b)); display: flex; flex-direction: column; gap: 16px; }
.form label, .form .field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: .95rem; }
.form small { color: var(--muted); font-weight: 400; }
.form input[type="text"], .form input[type="password"], .form input[type="email"], .form input:not([type]), .form textarea {
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 14px; font: inherit; font-weight: 400; outline: none; resize: vertical;
}
.form input:focus, .form textarea:focus { border-color: var(--accent); }
.emoji-input { width: 90px; text-align: center; font-size: 1.4rem !important; }
.swatches { display: flex; gap: 12px; flex-wrap: wrap; }
.swatches label { flex-direction: row; cursor: pointer; }
.swatches input { position: absolute; opacity: 0; pointer-events: none; }
.swatches span { width: 34px; height: 34px; border-radius: 50%; border: 3px solid transparent; transition: transform .1s; }
.swatches input:checked + span { border-color: var(--text); transform: scale(1.12); }
.swatches input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.hint { color: var(--muted); font-size: .85rem; }
.hint.ok { color: var(--good); }
.hint.bad { color: var(--bad); }
.form .field .hint { font-weight: 400; }
.primary, .danger { border: 0; border-radius: 14px; padding: 12px 16px; font: inherit; font-weight: 700; cursor: pointer; color: #fff; }
.primary { background: var(--accent); }
.danger { background: transparent; color: #ff7a7a; border: 1px solid #ff7a7a55; }
.ghost { align-self: flex-start; background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 12px; padding: 9px 14px; font: inherit; cursor: pointer; }
.ghost:hover:not(:disabled) { border-color: var(--accent); }
.ghost:disabled { opacity: .6; cursor: default; }

/* Avatars en photo, aperçu, bouton Régénérer */
.avatar { overflow: hidden; }
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.photo-row .avatar { width: 64px; height: 64px; font-size: 1.9rem; }
.regen-row { padding: 6px 14px 0; background: var(--bg); display: flex; flex-wrap: wrap; gap: 8px; }
.regen-row[hidden] { display: none; }
.regen-row .ghost { padding: 6px 11px; font-size: .85rem; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; }
.chips:empty { display: none; }
.chip { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); border-radius: 16px; padding: 6px 12px; font: inherit; font-size: .9rem; text-align: left; cursor: pointer; }
.chip:hover { border-color: var(--accent); }

/* Actions sur les messages de l'utilisateur */
.msg-wrap { align-self: flex-end; max-width: 85%; display: flex; flex-direction: column; align-items: flex-end; }
.msg-wrap .msg { max-width: 100%; }
.msg-actions { display: flex; gap: 10px; margin-top: 2px; }
.msg-actions button { background: none; border: 0; color: var(--muted); font-size: .78rem; cursor: pointer; padding: 2px 4px; }
.msg-actions button:hover { color: var(--text); }
.edit-area { width: min(72vw, 520px); background: var(--bg); color: var(--text); border: 1px solid var(--accent); border-radius: 12px; padding: 8px 12px; font: inherit; resize: vertical; }

/* Compte */
.topbar { display: flex; align-items: center; gap: 10px; justify-content: flex-end; flex-wrap: wrap; margin-bottom: 8px; font-size: .8rem; color: var(--muted); }
.topbar .sync { margin-right: auto; }
.topbar .who-email { overflow: hidden; text-overflow: ellipsis; max-width: 45vw; }
.topbar .ghost { padding: 5px 10px; font-size: .8rem; align-self: auto; }
.gate .form { padding: 0; overflow: visible; }
.section-title { font-size: 1.1rem; margin: 28px 0 12px; }
.topbar .link { background: none; border: 0; color: var(--muted); font: inherit; font-size: .75rem; text-decoration: underline; cursor: pointer; }
.visi { display: flex; gap: 18px; font-weight: 400; }
.visi label { flex-direction: row; align-items: center; gap: 6px; font-weight: 400; cursor: pointer; }
.topbar a.link { color: var(--muted); font-size: .75rem; }
.hint a { color: var(--link); }
.pub-tools { display: flex; gap: 10px; margin-bottom: 14px; }
.pub-tools input, .pub-tools select { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 12px; padding: 9px 12px; font: inherit; outline: none; }
.pub-tools input { flex: 1; min-width: 0; }
.pub-tools input:focus, .pub-tools select:focus { border-color: var(--accent); }
.card small.uses { display: block; margin-top: 2px; font-size: .75rem; }
.msg.err { background: var(--err-bg); border: 1px solid #ff7a7a55; color: var(--err-text); }
.msg.err .ghost { margin-top: 8px; }
.topbar .lang { margin-left: auto; }
.gate .topbar { margin-bottom: 18px; }

/* Réglages */
.settings .group { margin: 20px 0; }
.settings h3 { font-size: 1rem; margin-bottom: 8px; }
.seg { display: flex; gap: 8px; flex-wrap: wrap; }
.seg label { cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: inline-block; padding: 9px 16px; border-radius: 12px; background: var(--panel); border: 1px solid var(--line); }
.seg input:checked + span { background: var(--accent); border-color: var(--accent); color: #fff; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Animations (désactivées si l'appareil demande moins de mouvement) */
@keyframes pop-in { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: none; } }
@keyframes card-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
@keyframes dots { 0%, 100% { opacity: .25; } 50% { opacity: 1; } }
.msg, .msg-wrap { animation: pop-in .22s ease-out; }
.card { animation: card-in .35s ease-out backwards; }
.grid .card:nth-child(2) { animation-delay: .04s; } .grid .card:nth-child(3) { animation-delay: .08s; }
.grid .card:nth-child(4) { animation-delay: .12s; } .grid .card:nth-child(5) { animation-delay: .16s; }
.grid .card:nth-child(n+6) { animation-delay: .2s; }
.card:active { transform: scale(.98); }
.msg.typing:empty::after { content: "●  ●  ●"; letter-spacing: 1px; font-size: .7em; animation: dots 1.2s ease-in-out infinite; margin: 0; }
.composer button:not(:disabled):active { transform: scale(.92); }
.composer button { transition: transform .1s; }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

.adm-item details { margin: 6px 0; font-size: .9rem; }
.adm-reasons { margin: 6px 0; padding-left: 18px; font-size: .9rem; color: var(--muted); }
.adm-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
