:root {
  --plum: #6d4c7d;
  --terra: #c97b5a;
  --bg: #faf8f5;
  --surface: #ffffff;
  --surface-2: #f2eee9;
  --text: #221d26;
  --muted: #6d6472;
  --line: #e3dcd6;
  --sent: #efe6f2;
  --danger: #a8362f;
  --radius: 14px;
  --tabbar-h: 58px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17141a;
    --surface: #211d25;
    --surface-2: #2b2530;
    --text: #ece7ef;
    --muted: #a096a8;
    --line: #362f3c;
    --sent: #332640;
    --danger: #e0796f;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior-y: contain;
}

button, input, textarea { font: inherit; color: inherit; }

/* ------------------------------------------------------------- structure -- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  gap: 4px;
  padding: env(safe-area-inset-top) 8px 0;
  height: calc(52px + env(safe-area-inset-top));
  background: linear-gradient(135deg, var(--plum), var(--terra));
  color: #fff;
}

.topbar h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-btn {
  border: 0;
  background: transparent;
  color: #fff;
  font-size: 1.5rem;
  line-height: 1;
  padding: 8px;
  border-radius: 10px;
  cursor: pointer;
}

.icon-btn[hidden] { visibility: hidden; display: block; }

#main {
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 8px);
  min-height: calc(100dvh - 52px);
}

.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: flex;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.tab {
  flex: 1;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 0.8rem;
  cursor: pointer;
}

.tab.active { color: var(--plum); font-weight: 600; }

@media (prefers-color-scheme: dark) {
  .tab.active { color: var(--terra); }
}

/* ---------------------------------------------------------- déverrouillage -- */

.screen-center {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.lock-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px 22px;
}

.lock-title {
  margin: 0 0 4px;
  font-size: 1.8rem;
  background: linear-gradient(135deg, var(--plum), var(--terra));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.lock-sub { margin: 0 0 20px; color: var(--muted); font-size: 0.9rem; }

/* -------------------------------------------------------------- contrôles -- */

.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: 0.8rem; color: var(--muted); margin-bottom: 6px; }

.field input, .search-bar input, textarea {
  width: 100%;
  padding: 12px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  -webkit-appearance: none;
}

.field input:focus, .search-bar input:focus, textarea:focus {
  outline: 2px solid var(--plum);
  outline-offset: -1px;
}

.btn {
  display: inline-block;
  width: 100%;
  padding: 13px 16px;
  margin-bottom: 8px;
  border: 1px solid transparent;
  border-radius: 10px;
  text-align: center;
  font-weight: 600;
  cursor: pointer;
}

.btn-primary { background: var(--plum); color: #fff; }
.btn-quiet { background: var(--surface-2); border-color: var(--line); }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn:disabled { opacity: 0.5; cursor: default; }
.file-btn { position: relative; }

.hint { font-size: 0.82rem; color: var(--muted); margin: 8px 0 0; }
#unlock-error { color: var(--danger); }

.segmented {
  display: flex;
  gap: 4px;
  padding: 12px;
  overflow-x: auto;
}

.seg {
  flex: 1;
  white-space: nowrap;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.85rem;
  cursor: pointer;
}

.seg.active { background: var(--plum); border-color: var(--plum); color: #fff; }

/* ------------------------------------------------------------------ listes -- */

.list { list-style: none; margin: 0; padding: 0 12px; }

.row {
  display: block;
  width: 100%;
  text-align: left;
  padding: 13px 14px;
  margin-bottom: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  cursor: pointer;
}

.row-top { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.row-title { font-weight: 600; font-size: 0.95rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-date { font-size: 0.72rem; color: var(--muted); flex-shrink: 0; }
.row-snippet {
  margin-top: 3px;
  font-size: 0.85rem;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.badge {
  display: inline-block;
  margin-right: 6px;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 0.68rem;
  font-weight: 600;
  background: var(--terra);
  color: #fff;
  vertical-align: 1px;
}

.badge.wa { background: #3f8f6a; }

mark { background: rgba(201, 123, 90, 0.35); color: inherit; border-radius: 3px; }

.result-count, .empty { padding: 4px 16px 8px; color: var(--muted); font-size: 0.85rem; }
.empty { text-align: center; padding: 40px 24px; }

.search-bar { padding: 12px; }

/* ------------------------------------------------------------------- fil --- */

#thread-messages { padding: 12px; }

.msg {
  max-width: 88%;
  margin-bottom: 10px;
  padding: 10px 13px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.msg.sent { margin-left: auto; background: var(--sent); }

.msg-meta { font-size: 0.7rem; color: var(--muted); margin-bottom: 5px; }
.msg-subject { font-weight: 600; font-size: 0.9rem; margin-bottom: 4px; }
.msg-body { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 0.92rem; }

.msg-attach {
  margin-top: 6px;
  font-size: 0.75rem;
  color: var(--muted);
  border-top: 1px dashed var(--line);
  padding-top: 5px;
}

.quoted-toggle {
  margin-top: 6px;
  background: none;
  border: 0;
  padding: 0;
  color: var(--plum);
  font-size: 0.78rem;
  text-decoration: underline;
  cursor: pointer;
}

.thread-actions { padding: 4px 12px 20px; }

/* --------------------------------------------------------------- brouillon -- */

#draft-versions { padding: 12px; }

.draft {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}

.draft-label { font-size: 0.72rem; color: var(--muted); margin-bottom: 6px; }
.draft-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.draft-tools { display: flex; gap: 8px; margin-top: 12px; }
.draft-tools .btn { margin: 0; }

.draft.instruction {
  background: var(--surface-2);
  font-size: 0.88rem;
  color: var(--muted);
}

.composer {
  position: sticky;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 10px 12px;
  background: var(--bg);
  border-top: 1px solid var(--line);
}

.composer textarea { flex: 1; resize: none; }
.composer .btn { width: auto; margin: 0; flex-shrink: 0; }

/* ---------------------------------------------------------------- réglages -- */

.card {
  margin: 12px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.card h2 { margin: 0 0 12px; font-size: 1rem; }
.card.danger { border-color: var(--danger); }

.kv { margin: 0 0 14px; font-size: 0.88rem; }
.kv div { display: flex; justify-content: space-between; gap: 12px; padding: 4px 0; border-bottom: 1px solid var(--line); }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; }

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid var(--line);
  border-top-color: var(--plum);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -2px;
  margin-right: 6px;
}

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2s; }
}
