/* === VARIABLES === */
:root {
  --bg:           #0d0d1a;
  --bg-surface:   #16162a;
  --bg-elevated:  #1e1e38;
  --bg-hover:     #252545;
  --accent:       #7c6af5;
  --accent-hover: #9b8dff;
  --accent-dim:   #3d3580;
  --text:         #e0e0f0;
  --text-muted:   #7a7a9a;
  --text-faint:   #4a4a6a;
  --border:       #2a2a4a;
  --border-light: #333360;
  --user-bg:      #1c1c40;
  --assistant-bg: #16162a;
  --danger:       #e05555;
  --success:      #4caf7d;
  --sidebar-w:    280px;
  --radius:       10px;
  --radius-sm:    6px;
}

/* === RESET === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; }
body { font-family: system-ui, -apple-system, sans-serif; background: var(--bg); color: var(--text); min-height: 100vh; font-size: 15px; line-height: 1.6; }
a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }
button { cursor: pointer; font-family: inherit; }
textarea, input, select { font-family: inherit; font-size: inherit; }

/* === BUTTONS === */
.btn-primary {
  background: var(--accent); color: #fff; border: none;
  padding: .55rem 1.2rem; border-radius: var(--radius-sm); font-weight: 600;
  transition: background .15s;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost {
  background: transparent; color: var(--text-muted); border: 1px solid var(--border);
  padding: .5rem 1rem; border-radius: var(--radius-sm);
  transition: border-color .15s, color .15s;
}
.btn-ghost:hover { border-color: var(--accent); color: var(--text); }
.btn-icon {
  background: none; border: none; color: var(--text-muted);
  padding: .2rem .4rem; border-radius: 4px; font-size: 14px;
  transition: color .15s, background .15s;
}
.btn-icon:hover { color: var(--text); background: var(--bg-hover); }
.btn-icon.btn-danger:hover { color: var(--danger); }
.btn-full { width: 100%; }

/* === ALERTS === */
.alert-error   { background: #2d1515; border: 1px solid #6b2222; color: #e08080; padding: .7rem 1rem; border-radius: var(--radius-sm); margin-bottom: 1rem; }
.alert-success { background: #152d1e; border: 1px solid #226b42; color: #80e0a8; padding: .7rem 1rem; border-radius: var(--radius-sm); margin-bottom: 1rem; }

/* === FIELDS === */
.field { margin-bottom: 1rem; }
.field label { display: block; color: var(--text-muted); font-size: 13px; margin-bottom: .4rem; }
.field label small { color: var(--text-faint); }
.field input[type="text"],
.field input[type="password"],
.field input[type="email"],
.field select,
.field textarea {
  width: 100%; background: var(--bg-elevated); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: .55rem .75rem; outline: none;
  transition: border-color .15s;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: var(--accent); }
.field textarea { resize: vertical; min-height: 80px; }
.field textarea.textarea-lg { min-height: 200px; }
.field textarea.textarea-xl { min-height: 300px; }
.field input[type="file"] { color: var(--text-muted); font-size: 13px; }
.field-label-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: .4rem; }
.field-label-row label { margin-bottom: 0; }
.btn-sm { padding: .25rem .6rem; font-size: 12px; }
.field-hint { display: block; font-size: 12px; color: var(--text-faint); margin-top: .35rem; }
.field-hint code { background: rgba(124,106,245,.12); border-radius: 3px; padding: .05em .3em; font-family: monospace; font-size: .9em; color: var(--accent); }
.field-small label { display: block; color: var(--text-faint); font-size: 11px; margin-bottom: .25rem; }
.field-small select { width: 100%; background: var(--bg); color: var(--text-muted); border: 1px solid var(--border); border-radius: 4px; padding: .3rem .5rem; font-size: 12px; outline: none; }

/* === LOGIN === */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-card { background: var(--bg-surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 2.5rem 2rem; width: 340px; }
.login-logo { font-size: 2.5rem; text-align: center; margin-bottom: .5rem; }
.login-title { text-align: center; font-size: 1.4rem; font-weight: 700; margin-bottom: 1.5rem; color: var(--accent); }

/* === TOPNAV === */
.topnav {
  display: flex; align-items: center; gap: 1rem;
  padding: 0 1rem; height: 100%;
}
.topnav-brand { font-weight: 800; color: var(--accent); font-size: 1rem; letter-spacing: .05em; }
.topnav-links { display: flex; gap: .2rem; flex: 1; }
.topnav-links a {
  color: var(--text-muted); padding: .3rem .7rem; border-radius: 6px; font-size: 14px;
  transition: color .15s, background .15s;
}
.topnav-links a:hover, .topnav-links a.active { color: var(--text); background: var(--bg-hover); }
.topnav-logout { color: var(--text-faint); font-size: 13px; white-space: nowrap; }
.topnav-logout:hover { color: var(--danger); }

/* === APP LAYOUT === */
.app-page { display: flex; height: 100vh; overflow: hidden; }

.sidebar {
  width: var(--sidebar-w); min-width: var(--sidebar-w);
  background: var(--bg-surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  transition: transform .25s;
  z-index: 100;
}
.sidebar-top { padding: .75rem; border-bottom: 1px solid var(--border); }
.btn-new-conv {
  width: 100%; background: var(--accent); color: #fff; border: none;
  padding: .6rem; border-radius: var(--radius-sm); font-weight: 600; font-size: 14px;
  transition: background .15s;
}
.btn-new-conv:hover { background: var(--accent-hover); }
.sidebar-list { flex: 1; overflow-y: auto; padding: .5rem; }
.conv-item {
  display: flex; align-items: center; gap: .3rem;
  border-radius: var(--radius-sm); margin-bottom: 2px;
  padding: .1rem .3rem;
}
.conv-item:hover, .conv-item.active { background: var(--bg-hover); }
.conv-link { flex: 1; color: var(--text-muted); font-size: 13px; padding: .4rem .2rem; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.conv-item.active .conv-link { color: var(--text); }
.conv-actions { display: none; gap: 2px; flex-shrink: 0; }
.conv-item:hover .conv-actions { display: flex; }
.conv-del-form { display: inline; }
.sidebar-empty { color: var(--text-faint); font-size: 13px; padding: 1rem .5rem; text-align: center; }
.sidebar-settings { padding: .75rem; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: .5rem; }
.scene-indicator { display: flex; align-items: center; gap: .5rem; padding: .25rem .1rem; font-size: 11px; color: var(--text-faint); }
.scene-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); flex-shrink: 0; transition: background .5s; }
.scene-dot[data-scene="quotidien"] { background: #4ade80; }
.scene-dot[data-scene="tension"]   { background: #fb923c; }
.scene-dot[data-scene="crucial"]   { background: #f43f5e; }

/* Scrollbar sidebar */
.sidebar-list::-webkit-scrollbar { width: 4px; }
.sidebar-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* Main wrap */
.main-wrap { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.chat-topbar {
  height: 50px; background: var(--bg-surface); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: .5rem; padding: 0 .5rem; flex-shrink: 0;
}
.hamburger { display: none; background: none; border: none; color: var(--text-muted); font-size: 1.2rem; padding: .3rem .5rem; }
.chat-topbar-meta { display: flex; align-items: center; gap: .5rem; margin-left: auto; }
.meta-tag {
  display: flex; align-items: center; gap: .3rem;
  background: var(--bg-elevated); border: 1px solid var(--border);
  border-radius: 20px; padding: .2rem .6rem; font-size: 12px; color: var(--text-muted);
  white-space: nowrap;
}
.meta-avatar { width: 18px; height: 18px; border-radius: 50%; object-fit: cover; }

/* Messages area */
.messages-area {
  flex: 1; overflow-y: auto; padding: 1.5rem 1rem;
  display: flex; flex-direction: column; gap: 1rem;
}
.messages-area::-webkit-scrollbar { width: 6px; }
.messages-area::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

#load-more-bar { text-align: center; margin-bottom: .5rem; }
#loadMoreBtn { background: var(--bg-elevated); color: var(--text-muted); border: 1px solid var(--border); padding: .4rem 1rem; border-radius: 20px; font-size: 13px; cursor: pointer; }
#loadMoreBtn:hover { border-color: var(--accent); color: var(--text); }

/* Messages */
.message { display: flex; gap: .75rem; max-width: 780px; }
.message.user { flex-direction: row-reverse; align-self: flex-end; }
.message.assistant { align-self: flex-start; }

.msg-avatar { flex-shrink: 0; width: 36px; height: 36px; margin-top: 2px; }
.msg-avatar-img { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; border: 2px solid var(--border); }
.msg-avatar-initials {
  width: 36px; height: 36px; border-radius: 50%; background: var(--accent-dim);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px; color: var(--accent-hover);
}

.msg-body { flex: 1; min-width: 0; }
.msg-name-row { display: flex; align-items: baseline; gap: 0; margin-bottom: .25rem; }
.msg-name { font-size: 12px; font-weight: 600; color: var(--accent); }
.msg-name + .msg-story-time::before { content: ' - '; }
.msg-story-time { font-size: 11px; color: var(--text-faint); }
.msg-bubble {
  padding: .75rem 1rem; border-radius: var(--radius);
  line-height: 1.7; white-space: pre-wrap; word-wrap: break-word;
}
.message.user .msg-bubble { background: var(--user-bg); border: 1px solid #2a2a60; }
.message.assistant .msg-bubble { background: var(--assistant-bg); border: 1px solid var(--border); }

.msg-footer {
  display: flex; align-items: center; gap: .5rem; margin-top: .35rem;
}
.msg-meta { font-size: 11px; color: var(--text-faint); flex: 1; }
.btn-copy, .btn-regen, .btn-copy-prompt {
  background: none; border: none; color: var(--text-faint);
  font-size: 13px; padding: .15rem .3rem; border-radius: 4px; cursor: pointer;
  transition: color .15s;
}
.btn-copy:hover { color: var(--accent); }
.btn-regen:hover { color: var(--success); }
.btn-copy-prompt:hover { color: var(--text-muted); }

/* Édition de message */
.msg-actions {
  display: flex; justify-content: flex-end;
  gap: 4px; margin-top: 4px;
}
.btn-edit {
  background: none; border: none; color: var(--text-faint);
  font-size: 13px; padding: .15rem .35rem; border-radius: 4px; cursor: pointer;
  transition: color .15s;
}
.btn-edit:hover { color: var(--accent); }
.edit-textarea {
  width: 100%; background: var(--bg-elevated); color: var(--text);
  border: 1px solid var(--accent); border-radius: var(--radius-sm);
  padding: .6rem .85rem; font-size: .95rem; line-height: 1.6;
  resize: vertical; min-height: 60px;
}
.edit-actions {
  display: flex; justify-content: flex-end; gap: .5rem; margin-top: .5rem;
}

/* Markdown dans les bulles */
.msg-bubble h1,.msg-bubble h2,.msg-bubble h3 { margin: .8em 0 .3em; font-weight: 700; }
.msg-bubble h1 { font-size: 1.2em; } .msg-bubble h2 { font-size: 1.1em; } .msg-bubble h3 { font-size: 1em; }
.msg-bubble strong { font-weight: 700; }
.msg-bubble em { font-style: italic; }
.msg-bubble code { background: rgba(124,106,245,.15); border-radius: 3px; padding: .1em .35em; font-family: monospace; font-size: .9em; }
.msg-bubble pre { background: #111128; border: 1px solid var(--border); border-radius: var(--radius-sm); padding: .8rem 1rem; overflow-x: auto; margin: .5em 0; position: relative; }
.msg-bubble pre code { background: none; padding: 0; }
.code-copy { position: absolute; top: .4rem; right: .4rem; background: var(--bg-elevated); color: var(--text-muted); border: none; border-radius: 4px; padding: .2rem .5rem; font-size: 11px; cursor: pointer; }
.code-copy:hover { color: var(--text); }
.msg-bubble ul, .msg-bubble ol { padding-left: 1.5rem; }
.msg-bubble li { margin: .2rem 0; }
.msg-bubble a { color: var(--accent); }
.msg-bubble hr { border: none; border-top: 1px solid var(--border); margin: 1em 0; }
.msg-bubble p { margin-bottom: .5em; }
.msg-bubble p:last-child { margin-bottom: 0; }

/* Cursor streaming */
.streaming-cursor { display: inline-block; width: 2px; height: 1em; background: var(--accent); animation: blink .7s step-end infinite; vertical-align: text-bottom; margin-left: 1px; }
@keyframes blink { 0%,100% { opacity: 1; } 50% { opacity: 0; } }

/* No conversation */
.no-conv-msg { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--text-faint); text-align: center; }
.no-conv-icon { font-size: 3rem; margin-bottom: 1rem; opacity: .3; }

/* Composer */
.composer-area { padding: .75rem 1rem; border-top: 1px solid var(--border); background: var(--bg-surface); flex-shrink: 0; }
.streaming-indicator { display: flex; align-items: center; gap: .5rem; color: var(--text-muted); font-size: 12px; margin-bottom: .4rem; }
.dot-pulse { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: .3; transform: scale(.8); } 50% { opacity: 1; transform: scale(1.1); } }
.composer { display: flex; gap: .5rem; align-items: flex-end; }
#composer-text {
  flex: 1; background: var(--bg-elevated); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: .65rem .9rem; outline: none; resize: none; max-height: 200px;
  transition: border-color .15s;
}
#composer-text:focus { border-color: var(--accent); }
.composer-btns { display: flex; flex-direction: column; gap: .3rem; }
.btn-send { background: var(--accent); color: #fff; border: none; padding: .65rem 1.2rem; border-radius: var(--radius-sm); font-weight: 600; }
.btn-send:hover { background: var(--accent-hover); }
.btn-stop { background: #3d1515; color: var(--danger); border: 1px solid #6b2222; padding: .55rem .9rem; border-radius: var(--radius-sm); font-size: 13px; }
.btn-stop:hover { background: #5a1515; }

/* === MODALS === */
.modal { position: fixed; inset: 0; z-index: 500; display: flex; align-items: center; justify-content: center; }
.modal[hidden] { display: none; }
.modal-overlay { position: absolute; inset: 0; background: rgba(0,0,0,.7); }
.modal-box {
  position: relative; z-index: 1;
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.5rem; width: 480px; max-width: 95vw; max-height: 90vh; overflow-y: auto;
}
.modal-box-sm { width: 360px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.25rem; }
.modal-header h2 { font-size: 1.1rem; }
.modal-close { background: none; border: none; color: var(--text-muted); font-size: 1.1rem; padding: .2rem .4rem; border-radius: 4px; }
.modal-close:hover { color: var(--text); background: var(--bg-hover); }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1.25rem; }

/* Temps de départ du récit */
.story-time-row { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.story-time-label { display: flex; align-items: center; gap: .3rem; font-size: 13px; color: var(--text-muted); }
.input-day { width: 60px; background: var(--bg-input); color: var(--text); border: 1px solid var(--border); border-radius: 4px; padding: .35rem .5rem; font-size: 13px; outline: none; }
.input-day:focus { border-color: var(--accent); }
.story-time-row select,
.story-time-row input[type="time"] { background: var(--bg-input); color: var(--text); border: 1px solid var(--border); border-radius: 4px; padding: .35rem .5rem; font-size: 13px; outline: none; }
.story-time-row select:focus,
.story-time-row input[type="time"]:focus { border-color: var(--accent); }

/* Radio group */
.radio-group { display: flex; flex-direction: column; gap: .4rem; margin-bottom: .5rem; }
.radio-label { display: flex; align-items: center; gap: .4rem; font-size: 13px; color: var(--text-muted); cursor: pointer; }
.modal-form { display: flex; flex-direction: column; }

/* Character checkboxes in modal */
.chars-checkboxes { display: flex; flex-direction: column; gap: .4rem; max-height: 220px; overflow-y: auto; padding: .3rem; }
.char-checkbox-label {
  display: flex; align-items: center; gap: .6rem; cursor: pointer;
  padding: .5rem .6rem; border-radius: var(--radius-sm);
  border: 1px solid var(--border); transition: border-color .15s, background .15s;
}
.char-checkbox-label:hover { border-color: var(--accent-dim); background: var(--bg-elevated); }
.char-checkbox-label input { accent-color: var(--accent); width: 16px; height: 16px; }
.char-checkbox-avatar { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; }
.char-checkbox-initials {
  width: 28px; height: 28px; border-radius: 50%; background: var(--accent-dim);
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 12px; color: var(--accent-hover); flex-shrink: 0;
}

/* === LIBRARY PAGES (worlds, personas, characters) === */
.lib-page { min-height: 100vh; }
.lib-main { max-width: 960px; margin: 0 auto; padding: 1.5rem 1rem; }
.lib-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.5rem; }
.lib-header h1 { font-size: 1.4rem; font-weight: 700; }

.card-form {
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1.5rem;
}
.card-form h2 { font-size: 1rem; margin-bottom: 1rem; color: var(--text-muted); }
.form-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: .5rem; }
.form-2col { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }

.cards-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: .75rem; }
.chars-grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.card-item {
  background: var(--bg-surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1rem;
  transition: border-color .15s;
}
.card-item:hover { border-color: var(--border-light); }
.card-item-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: .5rem; }
.card-item-name { font-weight: 600; font-size: .95rem; }
.card-item-desc { font-size: 13px; color: var(--text-muted); line-height: 1.5; }
.card-item-actions { display: flex; gap: .2rem; }

/* Character cards */
.char-card-top { display: flex; align-items: center; gap: .75rem; margin-bottom: .75rem; }
.char-avatar { width: 52px; height: 52px; border-radius: 50%; object-fit: cover; border: 2px solid var(--border); flex-shrink: 0; }
.char-avatar-placeholder {
  width: 52px; height: 52px; border-radius: 50%; background: var(--accent-dim);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.4rem; font-weight: 700; color: var(--accent-hover); flex-shrink: 0;
}
.char-card-info { flex: 1; min-width: 0; }
.avatar-preview { width: 60px; height: 60px; border-radius: 50%; object-fit: cover; border: 2px solid var(--border); display: block; margin-bottom: .5rem; }

.empty-state { color: var(--text-faint); text-align: center; padding: 2rem; font-size: 14px; }
.empty-state.small { padding: .5rem; font-size: 13px; }
.empty-state a { color: var(--accent); }

/* Sidebar backdrop (mobile) */
.sidebar-backdrop { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.6); z-index: 90; }

/* === RESPONSIVE === */
@media (max-width: 768px) {
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0;
    transform: translateX(-100%);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop.open { display: block; }
  .hamburger { display: block; }
  .topnav-links { display: none; }
  .chat-topbar-meta { flex-wrap: wrap; gap: .3rem; }
  .form-2col { grid-template-columns: 1fr; }
  .modal-box { padding: 1rem; }
}
