/* ── Reset & Base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #FAFAF8;
  --surface:   #FFFFFF;
  --border:    #E8E4DC;
  --text:      #1A1A1A;
  --muted:     #7A7570;
  --accent:    #6B5B45;
  --accent-l:  #F0EBE3;
  --green:     #4A6741;
  --amber:     #B07D2A;
  --red:       #A33A2B;
  --radius:    8px;
  --shadow:    0 1px 4px rgba(0,0,0,.08);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
}

/* ── Header ──────────────────────────────────────────────────────────────── */
header {
  border-bottom: 1px solid var(--border);
  padding: 0 2rem;
  background: var(--surface);
}
nav {
  max-width: 1100px;
  margin: 0 auto;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 1rem;
}
.logo {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--accent);
  text-decoration: none;
  letter-spacing: -.5px;
}
.tagline { color: var(--muted); font-size: .85rem; }
.logout-link { margin-left: auto; color: var(--muted); font-size: .8rem; text-decoration: none; }
.logout-link:hover { color: var(--accent); }

/* ── Login ───────────────────────────────────────────────────────────────── */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--bg); }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 2.5rem; width: 340px; box-shadow: 0 4px 24px rgba(0,0,0,.08); }
.login-logo { font-size: 1.6rem; font-weight: 700; color: var(--accent); letter-spacing: -.5px; margin-bottom: .25rem; }
.login-sub { color: var(--muted); font-size: .85rem; margin-bottom: 2rem; }
.login-field { margin-bottom: 1rem; }
.login-field label { display: block; font-size: .85rem; font-weight: 600; margin-bottom: .35rem; color: var(--muted); }
.login-field input { width: 100%; }
.login-btn { width: 100%; margin-top: .5rem; padding: .7rem; }
.login-error { background: #fdf0ee; border: 1px solid #f0bbb5; color: var(--red); border-radius: 6px; padding: .6rem .9rem; font-size: .85rem; margin-bottom: 1rem; }

/* ── Main layout ─────────────────────────────────────────────────────────── */
main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 2rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

/* ── Page header ─────────────────────────────────────────────────────────── */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 1rem;
}
.page-header h1 { font-size: 1.6rem; font-weight: 700; line-height: 1.2; }
.back-link { color: var(--muted); text-decoration: none; font-size: .85rem; display: block; margin-bottom: .25rem; }
.back-link:hover { color: var(--accent); }
.stats { display: flex; gap: 1rem; color: var(--muted); font-size: .9rem; align-items: center; }
.header-right { display: flex; align-items: center; gap: 1rem; }
.settings-menu { position: relative; }
.btn-settings {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--muted);
  border-radius: 6px;
  padding: .4rem .9rem;
  font-size: .85rem;
  cursor: pointer;
}
.btn-settings:hover { border-color: var(--accent); color: var(--accent); }
.settings-dropdown {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,.12);
  min-width: 200px;
  z-index: 50;
  padding: .4rem 0;
}
.settings-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: .6rem 1rem;
  font-size: .9rem;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text);
}
.settings-item:hover { background: var(--bg); }
.settings-item.danger { color: var(--red); }
.settings-item.danger:hover { background: #fdf0ee; }

/* ── Cards ───────────────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
}
.card h2 { font-size: 1rem; font-weight: 600; margin-bottom: 1rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-size: .8rem; }

/* ── Forms ───────────────────────────────────────────────────────────────── */
.form-row { display: flex; gap: .75rem; flex-wrap: wrap; align-items: center; }
.form-row input { flex: 1; min-width: 180px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
input[type=text], input[type=password], input[type=file], select {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: .5rem .75rem;
  font-size: .9rem;
  background: var(--bg);
  color: var(--text);
  width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type=password] { width: 100%; }
.hint { font-size: .8rem; color: var(--muted); margin-top: .5rem; }
code { background: var(--accent-l); padding: .1em .3em; border-radius: 3px; font-size: .85em; }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: .5rem 1.25rem;
  font-size: .9rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity .15s;
}
.btn-primary:hover { opacity: .85; }
.btn-secondary {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  padding: .35rem .9rem;
  font-size: .85rem;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.btn-analyze {
  background: var(--green);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: .35rem .9rem;
  font-size: .85rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.btn-analyze:hover { opacity: .85; }
.btn-danger-sm {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: .25rem .5rem;
  font-size: .9rem;
  border-radius: 4px;
}
.btn-danger-sm:hover { color: var(--red); background: #fdf0ee; }
.btn-close {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--muted);
  line-height: 1;
}

/* ── Project grid ────────────────────────────────────────────────────────── */
.project-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 1rem; }
.project-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  text-decoration: none;
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
  display: block;
}
.project-card:hover { border-color: var(--accent); box-shadow: 0 2px 8px rgba(0,0,0,.1); }
.project-title { font-size: 1.05rem; font-weight: 700; margin-bottom: .25rem; }
.project-genre { font-size: .85rem; color: var(--muted); }
.project-date { font-size: .8rem; color: var(--border); margin-top: .5rem; }

.new-project { border-style: dashed; }

/* ── Badge & Score ───────────────────────────────────────────────────────── */
.badge {
  display: inline-block;
  background: var(--accent-l);
  color: var(--accent);
  border-radius: 4px;
  padding: .15rem .6rem;
  font-size: .8rem;
  font-weight: 600;
}
.score-badge {
  display: inline-block;
  border-radius: 4px;
  padding: .15rem .6rem;
  font-size: .8rem;
  font-weight: 700;
}
.score-high { background: #eaf4e8; color: var(--green); }
.score-mid  { background: #fef7e4; color: var(--amber); }
.score-low  { background: #fdf0ee; color: var(--red); }

/* ── Act blocks ──────────────────────────────────────────────────────────── */
.act-block { margin-bottom: 1.5rem; }
.act-label { font-size: .75rem; font-weight: 700; text-transform: uppercase; letter-spacing: .1em; color: var(--accent); margin-bottom: .5rem; border-bottom: 1px solid var(--border); padding-bottom: .25rem; }
.muted-label { color: var(--muted) !important; }
.section-input {
  width: 100px !important;
  min-width: 0;
  padding: .3rem .5rem;
  font-size: .8rem;
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  transition: border-color .15s, background .15s;
  flex-shrink: 0;
}
.section-input:hover { border-color: var(--border); background: var(--bg); }
.section-input:focus { border-color: var(--accent); background: var(--surface); color: var(--text); outline: none; }

.chapter-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: .6rem .5rem;
  border-radius: 6px;
  flex-wrap: wrap;
  gap: .5rem;
}
.chapter-row:hover { background: var(--bg); }
.chapter-info { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
.chapter-num { font-size: .8rem; color: var(--muted); min-width: 3rem; }
.chapter-title { font-weight: 500; }
.chapter-words { font-size: .8rem; color: var(--muted); }
.chapter-actions { display: flex; align-items: center; gap: .5rem; flex-wrap: nowrap; }
.inline-form { display: inline; }

/* ── Characters ──────────────────────────────────────────────────────────── */
.char-table { width: 100%; border-collapse: collapse; margin-bottom: 1rem; font-size: .85rem; }
.char-table th { text-align: left; padding: .4rem .6rem; color: var(--muted); font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; border-bottom: 1px solid var(--border); }
.char-table td { padding: .5rem .6rem; border-bottom: 1px solid var(--border); }
.add-char summary { cursor: pointer; color: var(--accent); font-weight: 600; font-size: .9rem; margin-top: .5rem; }
.add-char[open] summary { margin-bottom: 1rem; }
.char-form { margin-top: .5rem; }

/* ── Analysis panel (modal) ──────────────────────────────────────────────── */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 100;
}
.analysis-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(900px, 95vw);
  max-height: 85vh;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 8px 32px rgba(0,0,0,.2);
  z-index: 101;
  display: flex;
  flex-direction: column;
}
.analysis-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
}
.analysis-header h3 { font-size: 1rem; font-weight: 600; }
.analysis-content {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  white-space: pre-wrap;
  font-size: .9rem;
  line-height: 1.7;
  font-family: 'Georgia', serif;
}
.analysis-score {
  padding: .75rem 1.5rem;
  border-top: 1px solid var(--border);
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--green);
  text-align: center;
}
.hidden { display: none !important; }

/* ── Analysis history ────────────────────────────────────────────────────── */
.analysis-card { margin-bottom: 1rem; }
.analysis-meta { display: flex; align-items: center; gap: 1rem; margin-bottom: 1rem; }
.analysis-date { font-size: .8rem; color: var(--muted); }
.analysis-text {
  white-space: pre-wrap;
  font-family: 'Georgia', serif;
  font-size: .9rem;
  line-height: 1.8;
  color: var(--text);
}

/* ── Editor ──────────────────────────────────────────────────────────────── */
.editor-toolbar { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
.word-count { font-size: .85rem; color: var(--muted); }
.editor-wrap {
  display: flex;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.line-gutter {
  min-width: 48px;
  padding: 2rem .5rem 2rem .75rem;
  background: var(--bg);
  border-right: 1px solid var(--border);
  font-family: monospace;
  font-size: .8rem;
  line-height: 1.9;
  color: var(--border);
  text-align: right;
  overflow: hidden;
  user-select: none;
  flex-shrink: 0;
}
.line-gutter div { height: 1.9em; }
.chapter-editor {
  flex: 1;
  min-height: calc(100vh - 200px);
  padding: 2rem;
  font-family: 'Georgia', serif;
  font-size: 1rem;
  line-height: 1.9;
  border: none;
  background: var(--surface);
  color: var(--text);
  resize: none;
}
.chapter-editor:focus { outline: none; }
.editor-wrap:focus-within { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ── Markdown rendering ───────────────────────────────────────────────────── */
.markdown-body { font-family: 'Georgia', serif; font-size: .95rem; line-height: 1.85; color: var(--text); }
.markdown-body h2, .markdown-body h3 { font-family: -apple-system, sans-serif; margin: 1.5rem 0 .6rem; color: var(--accent); }
.markdown-body h2 { font-size: 1rem; text-transform: uppercase; letter-spacing: .06em; font-weight: 700; border-bottom: 1px solid var(--border); padding-bottom: .3rem; }
.markdown-body h3 { font-size: .95rem; font-weight: 600; }
.markdown-body p { margin-bottom: .9rem; }
.markdown-body ul, .markdown-body ol { margin: .5rem 0 .9rem 1.5rem; }
.markdown-body li { margin-bottom: .4rem; }
.markdown-body strong { font-weight: 700; color: var(--text); }
.markdown-body em { font-style: italic; }
.markdown-body code { background: var(--accent-l); padding: .1em .35em; border-radius: 3px; font-size: .85em; font-family: monospace; }
.markdown-body blockquote { border-left: 3px solid var(--border); padding-left: 1rem; color: var(--muted); margin: .5rem 0; font-style: italic; }

/* Pour le panneau streaming quand c'est converti en markdown */
.analysis-content.markdown-body { white-space: normal; }

/* ── Chat ────────────────────────────────────────────────────────────────── */
.chat-thread { margin-top: 2rem; border-top: 1px solid var(--border); padding-top: 1.5rem; }
.chat-divider { font-size: .75rem; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin-bottom: 1rem; }
.chat-messages { display: flex; flex-direction: column; gap: 1rem; margin-bottom: 1.25rem; max-height: 600px; overflow-y: auto; }
.chat-msg { display: flex; flex-direction: column; gap: .25rem; }
.chat-msg--user { align-items: flex-end; }
.chat-msg--assistant { align-items: flex-start; }
.chat-msg-label { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.chat-msg-body {
  max-width: 85%;
  padding: .75rem 1rem;
  border-radius: 10px;
  font-size: .9rem;
  line-height: 1.7;
}
.chat-msg--user .chat-msg-body { background: var(--accent); color: #fff; border-radius: 10px 10px 2px 10px; }
.chat-msg--user .chat-msg-body strong { color: #fff; }
.chat-msg--assistant .chat-msg-body { background: var(--bg); border: 1px solid var(--border); border-radius: 10px 10px 10px 2px; }
.chat-input-row { display: flex; gap: .75rem; align-items: flex-end; }
.chat-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .6rem .9rem;
  font-size: .9rem;
  font-family: inherit;
  resize: none;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}
.chat-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Blocs de modification proposée */
.mod-block {
  background: #f0f7f0;
  border: 1px solid #b8d4b8;
  border-radius: 8px;
  padding: 1rem;
  margin: .75rem 0;
}
.mod-label { font-size: .75rem; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--green); margin-bottom: .5rem; }
.mod-original { font-size: .85rem; color: var(--muted); margin-bottom: .35rem; }
.mod-nouveau { font-size: .85rem; color: var(--text); margin-bottom: .75rem; }
.btn-apply {
  background: var(--green);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: .4rem 1rem;
  font-size: .85rem;
  font-weight: 600;
  cursor: pointer;
}
.btn-apply:hover { opacity: .85; }
.btn-applied { background: var(--muted) !important; cursor: default; }

/* ── Misc ────────────────────────────────────────────────────────────────── */
.empty-state { color: var(--muted); font-style: italic; text-align: center; padding: 2rem; }
