:root {
  --bg: #f5f6f8; --panel: #ffffff; --line: #e3e6ea; --text: #1f2937; --muted: #6b7280;
  --accent: #1d4ed8; --accent-text: #ffffff; --hover: #eef2f7; --selected: #dbe7fb;
  --unread: #111827; --danger: #b91c1c; --ai: #5b21b6; --ai-bg: #f3effb;
  --radius: 6px; --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #111418; --panel: #191d23; --line: #2a3038; --text: #e5e7eb; --muted: #9aa3ad;
    --accent: #3b82f6; --hover: #222831; --selected: #1e3354; --unread: #f9fafb; --ai: #c4b5fd; --ai-bg: #231d33; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.45 var(--font); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 12px; }
button:hover { background: var(--hover); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.icon { padding: 4px 10px; font-size: 16px; line-height: 1; }
button.link { border: 0; background: none; color: var(--accent); padding: 4px 0; }
button.ai-btn { color: var(--ai); border-color: color-mix(in srgb, var(--ai) 35%, var(--line)); }
button:disabled { opacity: .55; cursor: wait; }
input, select, textarea { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 7px 9px; width: 100%; }
textarea { resize: vertical; }
input[type=color] { width: 64px; height: 34px; padding: 2px; }
label { display: block; color: var(--muted); font-size: 12px; }
label > input, label > select, label > textarea { margin-top: 3px; font-size: 14px; color: var(--text); }
.muted { color: var(--muted); } .small { font-size: 12px; } .error { color: var(--danger); min-height: 1em; margin: 4px 0; }
.spacer { flex: 1; }
[hidden] { display: none !important; }
body.no-ai .ai-only, body.no-ai .ai-btn { display: none !important; }

/* Connexion */
.auth { display: grid; place-items: center; min-height: 100%; padding: 16px; }
.auth-box { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 28px; width: 100%; max-width: 360px; display: grid; gap: 12px; }
.auth-box h1 { margin: 0; font-size: 22px; }

/* Mise en page */
.app { display: grid; height: 100vh; height: 100dvh; grid-template-columns: 240px minmax(300px, 400px) 1fr; grid-template-rows: auto 1fr; }
.topbar { grid-column: 1 / -1; display: flex; gap: 8px; align-items: center; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); }
.search { flex: 1; max-width: 560px; }
.sidebar { overflow: auto; padding: 10px 6px; border-right: 1px solid var(--line); }
.views, .tree { list-style: none; margin: 0; padding: 0; }
.views li, .tree li { padding: 6px 10px; border-radius: var(--radius); cursor: pointer; display: flex; gap: 6px; align-items: center; }
.views li:hover, .tree li:hover { background: var(--hover); }
.views li.active, .tree li.active { background: var(--selected); font-weight: 600; }
.count { margin-left: auto; font-size: 12px; color: var(--accent); font-weight: 600; }
.acc { margin-top: 14px; }
.acc-name { padding: 4px 10px; font-size: 12px; font-weight: 600; color: var(--muted); display: flex; gap: 6px; align-items: center; cursor: pointer; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; display: inline-block; }
.acc-err { color: var(--danger); font-size: 11px; padding: 0 10px; }
.tree li { padding-left: 22px; font-size: 13px; }

.list-pane { overflow: auto; border-right: 1px solid var(--line); background: var(--panel); }
.list-head { display: flex; align-items: baseline; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); z-index: 1; }
.list-head h2 { margin: 0; font-size: 16px; }
.message-list { list-style: none; margin: 0; padding: 0; }
.message-list li { padding: 9px 14px 9px 12px; border-bottom: 1px solid var(--line); cursor: pointer; border-left: 3px solid transparent; }
.message-list li:hover { background: var(--hover); }
.message-list li.active { background: var(--selected); }
.message-list li.unread .m-from, .message-list li.unread .m-subject { font-weight: 700; color: var(--unread); }
.m-top { display: flex; gap: 8px; align-items: baseline; }
.m-from { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.m-date { font-size: 12px; color: var(--muted); white-space: nowrap; }
.m-subject { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.m-snippet { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.m-tags { display: flex; gap: 4px; margin-top: 3px; flex-wrap: wrap; }
.tag { font-size: 11px; padding: 0 6px; border-radius: 3px; background: var(--hover); color: var(--muted); }
.tag.p1 { background: #fde8e8; color: #9b1c1c; } .tag.reply { background: var(--ai-bg); color: var(--ai); }
.tag.spam { background: #fff4e5; color: #92400e; }
@media (prefers-color-scheme: dark) { .tag.p1 { background: #3b1717; color: #fca5a5; } .tag.spam { background: #3a2a10; color: #fcd34d; } }

.read-pane { overflow: auto; background: var(--panel); padding: 0 22px 30px; }
.empty { display: grid; place-items: center; height: 100%; }
.reader-actions { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 0; position: sticky; top: 0; background: var(--panel); border-bottom: 1px solid var(--line); z-index: 1; }
.reader-actions button { padding: 4px 10px; font-size: 13px; }
#r-subject { font-size: 20px; margin: 16px 0 8px; }
.r-meta { margin-bottom: 12px; }
.ai-note { background: var(--ai-bg); border-left: 3px solid var(--ai); padding: 10px 12px; border-radius: var(--radius); margin: 10px 0; white-space: pre-wrap; }
.notice { background: var(--hover); padding: 6px 10px; border-radius: var(--radius); font-size: 12px; margin: 8px 0; }
.attachments { list-style: none; padding: 0; display: flex; gap: 8px; flex-wrap: wrap; }
.attachments a { display: inline-block; border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 10px; font-size: 12px; color: var(--text); text-decoration: none; }
#r-html { width: 100%; border: 0; min-height: 300px; background: #fff; border-radius: var(--radius); }
.r-text { white-space: pre-wrap; word-wrap: break-word; font: inherit; }

/* Tableau de bord */
body:not(.is-admin) .admin-only { display: none !important; }
.alert-count { color: var(--danger); }
.dash { grid-column: 2 / -1; grid-row: 2; overflow: auto; background: var(--bg); padding: 0 18px 30px; }
body.dash-mode .list-pane, body.dash-mode .read-pane { display: none !important; }
.dash-head { display: flex; gap: 10px; align-items: center; padding: 12px 0; position: sticky; top: 0; background: var(--bg); z-index: 1; flex-wrap: wrap; }
.dash-head h2 { margin: 0; font-size: 18px; }
.dash-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 14px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px; min-width: 0; }
.card h3 { margin: 0 0 8px; font-size: 14px; display: flex; gap: 6px; align-items: baseline; }
.card h3 .muted { font-weight: normal; }
.card.wide { grid-column: 1 / -1; }
.card ul { list-style: none; margin: 0; padding: 0; }
.card li { padding: 6px 0; border-top: 1px solid var(--line); display: flex; gap: 8px; align-items: baseline; }
.card li:first-child { border-top: 0; }
.card li .grow { flex: 1; min-width: 0; }
.alert-card { border-color: color-mix(in srgb, var(--danger) 45%, var(--line)); }
.alert-card li .grow strong { color: var(--danger); }
.when { color: var(--muted); font-size: 12px; white-space: nowrap; min-width: 92px; }
.chain { display: inline-flex; gap: 4px; }
.chain span { font-size: 11px; padding: 0 6px; border-radius: 3px; }
.chain .ok { background: #e3f4ea; color: #166534; } .chain .ko { background: #fde8e8; color: #9b1c1c; }
@media (prefers-color-scheme: dark) { .chain .ok { background: #12301f; color: #86efac; } .chain .ko { background: #3b1717; color: #fca5a5; } }
.bars { display: grid; gap: 4px; }
.bar { display: grid; grid-template-columns: 130px 1fr 34px; gap: 8px; align-items: center; font-size: 12px; }
.bar i { display: block; height: 8px; background: var(--accent); border-radius: 4px; opacity: .75; }
.src-list { font-size: 12px; color: var(--muted); }
.src-list .ko { color: var(--danger); }

/* Claude */
.chat { position: fixed; right: 0; top: 49px; bottom: 0; width: min(420px, 100%); background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; z-index: 5; box-shadow: -6px 0 18px rgba(0,0,0,.08); }
.chat-head { display: flex; gap: 8px; align-items: center; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.chat-log { flex: 1; overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.bubble { padding: 8px 11px; border-radius: 8px; white-space: pre-wrap; max-width: 92%; }
.bubble.user { background: var(--selected); align-self: flex-end; }
.bubble.assistant { background: var(--ai-bg); align-self: flex-start; }
.chat-form { display: flex; gap: 6px; padding: 10px; border-top: 1px solid var(--line); }
a.ref { color: var(--accent); cursor: pointer; text-decoration: underline; }

/* Dialogues */
dialog { border: 1px solid var(--line); border-radius: 10px; padding: 0; background: var(--panel); color: var(--text); width: min(760px, calc(100% - 24px)); max-height: calc(100dvh - 24px); }
dialog::backdrop { background: rgba(0,0,0,.35); }
.dlg-head { display: flex; align-items: center; gap: 8px; padding: 10px 14px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); z-index: 1; }
.compose form { padding: 0 14px 14px; display: grid; gap: 6px; }
.compose .dlg-head { margin: 0 -14px; }
.row { display: grid; grid-template-columns: 52px 1fr; align-items: center; gap: 8px; font-size: 13px; }
.row > input, .row > select { margin: 0; }
.ai-bar { display: flex; gap: 6px; }
.dlg-foot { display: flex; gap: 10px; align-items: center; }
.settings > div[data-panel] { padding: 14px; }
.tabs { display: flex; gap: 4px; padding: 10px 14px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.tabs button { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; }
.tabs button.active { border-bottom-color: var(--accent); font-weight: 600; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; }
.grid .full, .grid h3, .grid details { grid-column: 1 / -1; }
.grid h3 { margin: 6px 0 0; font-size: 15px; }
label.check { display: flex; gap: 6px; align-items: center; color: var(--text); font-size: 14px; }
label.check input { width: auto; margin: 0; }
.acc-list, .rule-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.acc-list li, .rule-list li { border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 10px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.acc-list .grow, .rule-list .grow { flex: 1; min-width: 180px; }
.oauth-row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.brief-body { padding: 14px 18px 20px; white-space: pre-wrap; }
.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--panel); padding: 8px 14px; border-radius: 6px; z-index: 20; max-width: calc(100% - 32px); }
.only-mobile { display: none; }

/* Téléphone : un volet à la fois */
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .only-mobile { display: inline-block; }
  .topbar { flex-wrap: wrap; padding: 8px; padding-top: max(8px, env(safe-area-inset-top)); }
  .search { order: 10; flex-basis: 100%; max-width: none; }
  #btn-compose { margin-left: auto; }
  .sidebar { position: fixed; inset: 0 30% 0 0; background: var(--panel); z-index: 6; transform: translateX(-105%); transition: transform .15s; padding-top: max(10px, env(safe-area-inset-top)); }
  body.menu-open .sidebar { transform: none; box-shadow: 6px 0 18px rgba(0,0,0,.2); }
  .list-pane, .read-pane { grid-column: 1; grid-row: 2; border: 0; }
  body.reading .list-pane { display: none; }
  body:not(.reading) .read-pane { display: none; }
  .read-pane { padding: 0 12px 30px; }
  .dash { grid-column: 1; padding: 0 10px 30px; }
  .dash-grid { grid-template-columns: 1fr; }
  .grid { grid-template-columns: 1fr; }
  .chat { top: 0; }
  dialog { width: 100%; max-height: 100dvh; height: 100dvh; border-radius: 0; margin: 0; max-width: none; }
}
