:root {
  color-scheme: dark;
  --bg-0: #0c0e14;
  --bg-1: #12141c;
  --bg-2: #191c26;
  --bg-3: #222634;
  --line: #2a2e3d;
  --line-2: #363b4d;
  --text: #ece6d6;
  --text-2: #bdb9ad;
  --muted: #8b8a96;
  --gold: #d9a74a;
  --gold-2: #f2c66d;
  --teal: #5fb3a1;
  --danger: #e0645a;
  --say: #f3d79c;
  --serif: 'Fraunces', Georgia, serif;
  --sans: 'Source Sans 3', system-ui, sans-serif;
  --r: 12px;
  --r-sm: 8px;
  --side: 272px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { background: var(--bg-1); }
body { margin: 0; min-height: 100dvh; font: 16px/1.5 var(--sans); color: var(--text); background: var(--bg-1); }
h1, h2, h3 { font-family: var(--serif); font-weight: 700; margin: 0; letter-spacing: -0.01em; line-height: 1.15; }
h1 { font-size: 2.4rem; }
h2 { font-size: 1.4rem; }
h3 { font-size: 1.05rem; }
a { color: inherit; text-decoration: none; }
p { margin: 0; }
ul { list-style: none; margin: 0; padding: 0; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
svg.i { width: 1.15em; height: 1.15em; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }

/* Contrôles */
button, .btn {
  font: inherit; font-weight: 600; color: var(--text);
  background: var(--bg-3); border: 1px solid var(--line-2); border-radius: var(--r-sm);
  padding: 0.5rem 1rem; cursor: pointer; display: inline-flex; align-items: center; gap: 0.45rem;
  transition: background 0.15s, border-color 0.15s;
}
button:hover, .btn:hover { border-color: var(--gold); }
.primary { background: var(--gold); color: #1a1408; border-color: var(--gold); }
.primary:hover { background: var(--gold-2); border-color: var(--gold-2); }
.ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.ghost:hover { background: var(--bg-3); color: var(--text); border-color: transparent; }
.danger { color: var(--danger); }
.danger:hover { border-color: var(--danger); }
.icon-btn { padding: 0.45rem; background: transparent; border-color: transparent; color: var(--text-2); border-radius: var(--r-sm); }
.icon-btn:hover { background: var(--bg-3); color: var(--text); border-color: transparent; }
button:disabled { opacity: 0.5; cursor: wait; }
input, textarea, select {
  width: 100%; font: inherit; color: var(--text); background: var(--bg-1);
  border: 1px solid var(--line-2); border-radius: var(--r-sm); padding: 0.55rem 0.8rem;
}
input:focus, textarea:focus, select:focus { border-color: var(--gold); outline: none; }
textarea { resize: vertical; line-height: 1.5; }
label { display: block; margin: 0.9rem 0; font-weight: 600; }
label > input, label > textarea, label > select { margin-top: 0.35rem; font-weight: 400; }
label .hint { display: block; font-weight: 400; color: var(--muted); font-size: 0.85rem; }
label.inline { display: flex; align-items: center; gap: 0.6rem; font-weight: 400; }
label.inline input { width: auto; margin: 0; }
.chip { display: inline-flex; align-items: center; gap: 0.3rem; padding: 0.25rem 0.75rem; border-radius: 999px; font-size: 0.875rem; background: var(--bg-3); border: 1px solid transparent; color: var(--text-2); cursor: pointer; }
.chip:hover { border-color: var(--line-2); color: var(--text); }
.chip.on { background: var(--text); color: var(--bg-0); }
.tag { font-size: 0.75rem; padding: 0.1rem 0.5rem; border-radius: 4px; background: var(--bg-3); color: var(--text-2); white-space: nowrap; }
.tags { display: flex; flex-wrap: wrap; gap: 0.3rem; }
.badge { font-size: 0.72rem; font-weight: 600; color: var(--gold); border: 1px solid color-mix(in srgb, var(--gold) 50%, transparent); border-radius: 4px; padding: 0 0.35rem; }

/* Avatars */
.avatar { width: 3rem; height: 3rem; border-radius: 10px; flex-shrink: 0; overflow: hidden; display: grid; place-items: center; font-family: var(--serif); font-weight: 700; color: #1a1408; background: var(--bg-3); }
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar.sm { width: 2.25rem; height: 2.25rem; border-radius: 8px; font-size: 1rem; }
.avatar.xs { width: 1.75rem; height: 1.75rem; border-radius: 6px; font-size: 0.85rem; }

/* Coquille */
#shell { display: grid; grid-template-columns: var(--side) 1fr; height: 100dvh; }
#sidebar { background: var(--bg-0); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-top { display: flex; align-items: center; justify-content: space-between; padding: 1.1rem 1.25rem 0.6rem; }
.brand { font-family: var(--serif); font-weight: 700; font-size: 1.6rem; color: var(--gold); letter-spacing: -0.02em; }
.side-nav { padding: 0.4rem 0.75rem; display: flex; flex-direction: column; gap: 0.15rem; }
.side-nav a, #side-user { display: flex; align-items: center; gap: 0.7rem; padding: 0.55rem 0.7rem; border-radius: var(--r-sm); color: var(--text-2); font-weight: 600; }
.side-nav a:hover, #side-user:hover { background: var(--bg-2); color: var(--text); }
.side-nav a.active { background: var(--bg-2); color: var(--gold); }
.side-title { padding: 1.1rem 1.4rem 0.4rem; font-size: 0.75rem; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
#side-chats { flex: 1; overflow-y: auto; padding: 0 0.75rem; min-height: 0; }
#side-chats li a { display: grid; grid-template-columns: 2.25rem minmax(0, 1fr) auto; gap: 0.7rem; align-items: center; padding: 0.5rem 0.6rem; border-radius: var(--r-sm); color: var(--text-2); }
#side-chats li a:hover { background: var(--bg-2); }
#side-chats li a.active { background: var(--bg-2); color: var(--text); box-shadow: inset 2px 0 0 var(--gold); }
#side-chats li strong { display: block; font-weight: 600; font-size: 0.95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#side-chats li small { display: block; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#side-chats li .icon-btn { opacity: 0; padding: 0.2rem; }
#side-chats li a:hover .icon-btn, #side-chats li .icon-btn:focus-visible { opacity: 1; }
#side-chats .empty-side { padding: 0.6rem; color: var(--muted); font-size: 0.9rem; }
.side-bottom { border-top: 1px solid var(--line); padding: 0.6rem 0.75rem; display: flex; align-items: center; gap: 0.3rem; }
#side-user { flex: 1; min-width: 0; }
#side-user span:nth-child(2) { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#content { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
#mobile-bar { display: none; }
#view { flex: 1; min-height: 0; overflow-y: auto; }
.page { max-width: 1280px; margin: 0 auto; padding: 2rem 2.5rem 4rem; }
.side-only { display: none; }
#scrim { display: none; }

/* Toasts */
#toasts { position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 0.5rem; z-index: 100; pointer-events: none; }
.toast { background: var(--bg-3); border: 1px solid var(--line-2); border-left: 3px solid var(--gold); color: var(--text); padding: 0.6rem 1rem; border-radius: var(--r-sm); box-shadow: 0 10px 30px #0008; animation: rise 0.2s ease-out; max-width: min(90vw, 32rem); }
.toast.error { border-left-color: var(--danger); }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

/* Connexion */
#auth { min-height: 100dvh; display: grid; place-items: center; padding: 1.5rem; background: radial-gradient(ellipse at top, #1c1a2a 0%, var(--bg-0) 60%); }
#auth form { width: min(100%, 22rem); }
#auth h1 { font-size: 3.4rem; color: var(--gold); }
#auth .lead { font-family: var(--serif); font-style: italic; font-size: 1.2rem; color: var(--text-2); margin: 0.2rem 0 1.8rem; }
#auth menu { display: flex; gap: 0.6rem; padding: 0; margin: 1.2rem 0 0; }
.error { color: var(--danger); min-height: 1.5em; font-size: 0.9rem; }

/* Explorer */
.explore-head { display: flex; flex-direction: column; gap: 1.1rem; margin-bottom: 1.6rem; }
.explore-head .row { display: flex; align-items: center; gap: 0.8rem; flex-wrap: wrap; }
.search { position: relative; flex: 1; min-width: 14rem; max-width: 34rem; }
.search svg { position: absolute; left: 0.8rem; top: 50%; transform: translateY(-50%); color: var(--muted); }
.search input { padding-left: 2.4rem; background: var(--bg-2); }
.chips { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 1.1rem; }
.card { position: relative; display: flex; flex-direction: column; border-radius: var(--r); overflow: hidden; background: var(--bg-2); border: 1px solid var(--line); transition: border-color 0.15s, transform 0.15s; }
.card:hover { border-color: var(--line-2); transform: translateY(-2px); }
.portrait { position: relative; aspect-ratio: 3 / 4; background: var(--bg-3); overflow: hidden; }
.portrait img { width: 100%; height: 100%; object-fit: cover; display: block; }
.portrait .initial { width: 100%; height: 100%; display: grid; place-items: center; font-family: var(--serif); font-size: 5rem; font-weight: 700; color: #1a1408; }
.card .portrait::after { content: ''; position: absolute; inset: 45% 0 0; background: linear-gradient(to bottom, transparent, color-mix(in srgb, var(--bg-2) 96%, transparent)); pointer-events: none; }
.card .name { position: absolute; left: 0.9rem; right: 0.9rem; bottom: 0.7rem; z-index: 1; font-family: var(--serif); font-weight: 700; font-size: 1.2rem; line-height: 1.15; text-shadow: 0 1px 4px #000c; }
.card .play { position: absolute; top: 0.7rem; right: 0.7rem; z-index: 3; opacity: 0; transform: translateY(-4px); transition: opacity 0.15s, transform 0.15s; padding: 0.4rem 0.8rem; }
.card:hover .play, .card:focus-within .play { opacity: 1; transform: none; }
.card .body { padding: 0.75rem 0.9rem 0.9rem; display: flex; flex-direction: column; gap: 0.5rem; flex: 1; }
.card .author { font-size: 0.85rem; color: var(--teal); display: flex; gap: 0.4rem; align-items: center; }
.card .tagline { font-size: 0.9rem; color: var(--text-2); display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.card .meta { margin-top: auto; font-size: 0.75rem; color: var(--muted); }
.card > a.cover { position: absolute; inset: 0; z-index: 1; }
.card .body { position: relative; z-index: 2; }
.empty { padding: 4rem 1rem; text-align: center; color: var(--muted); font-family: var(--serif); font-style: italic; font-size: 1.15rem; }
.empty .btn { font-family: var(--sans); font-style: normal; margin-top: 1rem; }

/* Fiche */
.sheet { display: grid; grid-template-columns: minmax(220px, 320px) minmax(0, 1fr); gap: 2.5rem; align-items: start; }
.sheet .portrait { border-radius: var(--r); position: sticky; top: 0; border: 1px solid var(--line); }
.sheet .head { display: flex; flex-direction: column; gap: 0.7rem; padding-bottom: 1.4rem; margin-bottom: 1.4rem; border-bottom: 1px solid var(--line); }
.sheet .head .row { display: flex; gap: 0.6rem; flex-wrap: wrap; align-items: center; }
.sheet .cta { display: flex; gap: 0.6rem; margin-top: 0.4rem; flex-wrap: wrap; }
.sheet .cta .primary { padding: 0.65rem 1.4rem; font-size: 1.05rem; }
.sheet .lead { font-size: 1.1rem; color: var(--text); }
.presentation { margin-bottom: 1.6rem; color: var(--text); }
.presentation s { color: var(--muted); }
.tabs { display: flex; gap: 0.2rem; border-bottom: 1px solid var(--line); margin-bottom: 1.2rem; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; border-radius: 0; color: var(--muted); padding: 0.5rem 0.9rem; }
.tabs button.on { color: var(--text); border-bottom-color: var(--gold); }
.prose { max-width: 68ch; white-space: pre-wrap; line-height: 1.65; color: var(--text-2); }
.prose em, .bubble em { color: var(--muted); font-style: italic; }
.prose strong, .bubble strong { color: var(--gold-2); }
.prose .say, .bubble .say { color: var(--say); }
.prose code, .bubble code { font-family: ui-monospace, monospace; font-size: 0.88em; color: var(--gold-2); background: var(--bg-3); padding: 0 0.3rem; border-radius: 4px; }
pre.definition { white-space: pre-wrap; font: 0.9rem/1.55 var(--sans); color: var(--text-2); max-width: 80ch; margin: 0; }
.back { display: inline-flex; align-items: center; gap: 0.3rem; color: var(--muted); font-weight: 600; margin-bottom: 1.2rem; }
.back:hover { color: var(--text); }

/* Éditeur */
.import-card { display: flex; align-items: center; gap: 1rem; padding: 1rem 1.2rem; margin: 0 0 1.5rem; max-width: 46rem; border: 1px dashed var(--line-2); border-radius: var(--r); background: var(--bg-2); cursor: pointer; font-weight: 400; }
.import-card:hover, .import-card.over { border-color: var(--gold); }
.import-card svg { width: 2rem; height: 2rem; color: var(--gold); }
.import-card > span { display: flex; flex-direction: column; gap: 0.15rem; }
.editor { display: grid; grid-template-columns: minmax(200px, 260px) minmax(0, 1fr); gap: 2.5rem; align-items: start; }
.editor .portrait { border-radius: var(--r); cursor: pointer; border: 1px dashed var(--line-2); }
.editor .portrait .hint { position: absolute; inset: auto 0 0; padding: 0.5rem; text-align: center; font-size: 0.85rem; background: #000a; color: var(--text-2); }
.editor form { max-width: 46rem; }
.editor menu { display: flex; gap: 0.6rem; padding: 0; margin: 1.5rem 0 0; }

/* Chat */
.chat { display: grid; grid-template-columns: minmax(0, 1fr) 0; height: 100%; transition: grid-template-columns 0.2s; }
.chat.open { grid-template-columns: minmax(0, 1fr) 420px; }
.thread { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.thread > header { display: flex; align-items: center; gap: 0.8rem; padding: 0.7rem 1.4rem; border-bottom: 1px solid var(--line); background: var(--bg-1); }
.thread > header h2 { font-size: 1.15rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread > header .spacer { flex: 1; }
.thread > header .persona-line { font-size: 0.85rem; color: var(--muted); display: block; font-family: var(--sans); font-weight: 400; }
#messages { flex: 1; overflow-y: auto; padding: 1.5rem 1.4rem; }
#messages .inner { max-width: 780px; margin: 0 auto; display: flex; flex-direction: column; gap: 1.6rem; }
.message { display: grid; grid-template-columns: 3rem minmax(0, 1fr); gap: 1rem; }
.message .who { display: flex; align-items: baseline; gap: 0.6rem; margin-bottom: 0.3rem; }
.message .who strong { font-family: var(--serif); font-size: 1.1rem; font-weight: 700; }
.message.assistant .who strong { color: var(--gold-2); }
.message .who menu { display: flex; gap: 0.1rem; margin: 0 0 0 auto; padding: 0; opacity: 0; transition: opacity 0.12s; }
.message:hover .who menu, .message .who menu:focus-within { opacity: 1; }
.bubble { white-space: pre-wrap; line-height: 1.7; color: var(--text-2); }
.message.user .bubble { color: var(--text); }
.message.streaming .bubble::after { content: '▍'; color: var(--gold); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: 0; } }
.bubble textarea { min-height: 8rem; background: var(--bg-2); }
.bubble menu { display: flex; gap: 0.5rem; padding: 0; margin: 0.5rem 0 0; }
.status { color: var(--muted); font-style: italic; font-family: var(--serif); display: flex; align-items: center; gap: 0.5rem; padding-left: 4rem; }
.status::before { content: ''; width: 0.5rem; height: 0.5rem; border-radius: 50%; background: var(--gold); animation: blink 1s infinite; }
#composer { padding: 0.8rem 1.4rem 1.1rem; border-top: 1px solid var(--line); background: var(--bg-1); }
#composer .box { max-width: 780px; margin: 0 auto; background: var(--bg-2); border: 1px solid var(--line-2); border-radius: var(--r); padding: 0.5rem 0.5rem 0.5rem 0.9rem; display: flex; flex-direction: column; gap: 0.4rem; }
#composer .top { display: flex; align-items: flex-end; gap: 0.5rem; }
#composer .top textarea { flex: 1; min-width: 0; }
#composer .box:focus-within { border-color: var(--gold); }
#composer textarea { border: 0; background: transparent; padding: 0.4rem 0; resize: none; max-height: 40vh; }
#composer textarea:focus { outline: none; }
#composer .bar { display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; }
#composer .bar .spacer { flex: 1; }
.select-chip { min-width: 0; }
.select-chip { position: relative; display: inline-flex; align-items: center; gap: 0.35rem; max-width: 15rem; padding: 0.25rem 0.6rem; border-radius: 999px; background: var(--bg-3); color: var(--text-2); font-size: 0.85rem; cursor: pointer; }
.select-chip:hover { color: var(--text); }
.select-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.select-chip select { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
#send { padding: 0.5rem; border-radius: 50%; width: 2.5rem; height: 2.5rem; justify-content: center; flex-shrink: 0; }

/* Chronique */
.chronicle { border-left: 1px solid var(--line); background: var(--bg-0); overflow-y: auto; min-height: 0; padding: 1rem 1.2rem 2rem; width: 420px; }
#toggle-chronicle .count:empty { display: none; }
.chat:not(.open) .chronicle { display: none; }
.chronicle > header { display: flex; align-items: center; gap: 0.4rem; padding-bottom: 0.6rem; margin-bottom: 1rem; border-bottom: 2px solid var(--gold); }
.chronicle > header h2 { color: var(--gold); flex: 1; }
.chronicle .lead { font-size: 0.85rem; color: var(--muted); margin-bottom: 0.8rem; }
.doc { margin-bottom: 1.2rem; }
.doc > header { display: flex; align-items: center; gap: 0.4rem; margin-bottom: 0.4rem; }
.doc > header h3 { flex: 1; font-weight: 700; }
.doc.type-enemy > header h3 { color: var(--danger); }
.doc.kind-text > header h3 { font-style: italic; font-weight: 500; color: var(--text-2); }
.notes { margin-top: 1.5rem; border-top: 1px solid var(--line); padding-top: 0.8rem; }
.notes > summary { cursor: pointer; font-family: var(--serif); font-weight: 700; color: var(--text-2); margin-bottom: 0.8rem; }
.notes > summary .muted { font-family: var(--sans); font-weight: 400; font-size: 0.85rem; display: block; }
.doc > header small { color: var(--muted); font-size: 0.72rem; }
.doc > header menu { display: flex; gap: 0; margin: 0; padding: 0; }
.doc > header menu .icon-btn { padding: 0.2rem; }
.doc pre { margin: 0; white-space: pre-wrap; font: 0.88rem/1.5 var(--sans); color: var(--text-2); max-height: 22rem; overflow-y: auto; background: var(--bg-2); border-radius: var(--r-sm); padding: 0.6rem 0.8rem; }
.doc iframe { width: 100%; border: 0; border-radius: var(--r-sm); background: transparent; }

/* Réglages */
.settings { display: grid; grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr)); gap: 2rem; max-width: 64rem; }
.panel { background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--r); padding: 1.2rem 1.4rem; }
.panel > header { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 0.3rem; }
.panel .lead { color: var(--muted); font-size: 0.9rem; margin-bottom: 0.8rem; }
.panel li { display: flex; align-items: center; gap: 0.7rem; padding: 0.55rem 0.5rem; border-radius: var(--r-sm); }
.panel li:hover { background: var(--bg-3); }
.panel li span { flex: 1; min-width: 0; }
.panel li small { display: block; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.panel li .icon-btn { opacity: 0; }
.panel li:hover .icon-btn, .panel li .icon-btn:focus-visible { opacity: 1; }

dialog { background: var(--bg-2); color: var(--text); border: 1px solid var(--line-2); border-radius: var(--r); width: min(92vw, 32rem); padding: 1.5rem 1.8rem; box-shadow: 0 30px 80px #000a; }
dialog::backdrop { background: #000a; backdrop-filter: blur(3px); }
dialog menu { display: flex; justify-content: flex-end; gap: 0.6rem; padding: 0; margin: 1.2rem 0 0; }

/* Mobile */
@media (max-width: 960px) {
  #shell { grid-template-columns: 1fr; }
  #sidebar { position: fixed; inset: 0 auto 0 0; width: min(85vw, var(--side)); z-index: 30; transform: translateX(-100%); transition: transform 0.2s; }
  #shell.side-open #sidebar { transform: none; }
  #shell.side-open #scrim { display: block; position: fixed; inset: 0; background: #0009; z-index: 25; }
  .side-only { display: inline-flex; }
  #mobile-bar { display: flex; align-items: center; gap: 0.5rem; padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--line); background: var(--bg-0); }
  #mobile-bar .brand { flex: 1; text-align: center; font-size: 1.3rem; }
  .page { padding: 1.2rem 1rem 3rem; }
  h1 { font-size: 1.9rem; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0.7rem; }
  .card .play { opacity: 1; transform: none; }
  .sheet, .editor { grid-template-columns: 1fr; gap: 1.4rem; }
  .sheet .portrait { position: static; max-width: 240px; }
  .chat.open { grid-template-columns: minmax(0, 1fr) 0; }
  .chat.open .chronicle { display: block; position: fixed; inset: 0; width: auto; z-index: 40; }
  .thread > header { padding: 0.6rem 0.8rem; }
  #messages { padding: 1rem 0.8rem; }
  .message { grid-template-columns: 2.5rem minmax(0, 1fr); gap: 0.7rem; }
  .message .who menu { opacity: 1; }
  #composer { padding: 0.5rem 0.6rem calc(0.6rem + env(safe-area-inset-bottom, 0px)); }
  .select-chip { max-width: 44%; flex: 1; }
  #composer .bar .ghost .label { display: none; }
  #composer .bar .ghost { padding: 0.35rem 0.5rem; }
  #tag-chips { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 0.3rem; scrollbar-width: none; }
  #tag-chips::-webkit-scrollbar { display: none; }
  #tag-chips .chip { white-space: nowrap; }
  .explore-head { gap: 0.8rem; margin-bottom: 1rem; }
  .explore-head .row { gap: 0.5rem; }
  h1 { font-size: 1.7rem; }
}
