:root {
  color-scheme: light dark;
  --bg: #f7f4ec;
  --surface: #fffdf8;
  --surface-2: #efeadf;
  --ink: #26231d;
  --muted: #6d675b;
  --line: #e1dacb;
  --accent: #a8590f;
  --accent-ink: #ffffff;
  --accent-soft: #f6e6d0;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgb(60 45 20 / .08), 0 2px 8px rgb(60 45 20 / .06);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1b18;
    --surface: #282622;
    --surface-2: #33302a;
    --ink: #eee9dd;
    --muted: #a9a294;
    --line: #3d3a33;
    --accent: #e8a55c;
    --accent-ink: #2a1a06;
    --accent-soft: #443320;
    --danger: #f2877f;
    --shadow: 0 1px 2px rgb(0 0 0 / .3);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
body.editor-open, body.drawer-open { overflow: hidden; }
h1, h2, p { margin: 0; }
a { color: inherit; }
img { max-width: 100%; display: block; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.icon { width: 22px; height: 22px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* Boutons */
.icon-btn {
  display: inline-grid; place-items: center; flex: none;
  width: 44px; height: 44px; padding: 0;
  background: none; border: 0; border-radius: 50%; cursor: pointer; color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn.is-active { color: var(--accent); }
.icon-btn.is-active .icon { fill: currentColor; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 8px 16px;
  border: 1px solid transparent; border-radius: 10px;
  background: none; cursor: pointer; font-weight: 550; text-decoration: none;
}
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-ghost { border-color: var(--line); background: var(--surface); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: var(--danger); color: var(--surface); }
.btn-danger-ghost { border-color: var(--line); background: var(--surface); color: var(--danger); }

/* Structure */
.layout { display: flex; min-height: 100dvh; }
.main {
  flex: 1; min-width: 0;
  padding: var(--safe-top) 16px calc(96px + var(--safe-bottom));
}
.sidebar {
  position: fixed; inset: 0 auto 0 0; z-index: 30;
  display: flex; flex-direction: column;
  width: min(84vw, 300px);
  padding: calc(12px + var(--safe-top)) 10px calc(12px + var(--safe-bottom));
  overflow-y: auto;
  background: var(--surface);
  border-right: 1px solid var(--line);
  transform: translateX(-102%);
  transition: transform .18s ease;
}
.drawer-open .sidebar { transform: none; }
.backdrop { position: fixed; inset: 0; z-index: 20; background: rgb(0 0 0 / .4); opacity: 0; pointer-events: none; transition: opacity .18s ease; }
.drawer-open .backdrop { opacity: 1; pointer-events: auto; }

.brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 14px; font-size: 18px; font-weight: 650; }
.brand img { width: 30px; height: 30px; }
.nav { flex: 1; }
.nav-link {
  display: flex; align-items: center; gap: 10px;
  width: 100%; min-height: 44px; padding: 6px 10px;
  background: none; border: 0; border-radius: 10px;
  color: inherit; text-align: left; text-decoration: none; cursor: pointer;
}
.nav-link:hover { background: var(--surface-2); }
.nav-link.active { background: var(--accent-soft); font-weight: 600; }
.nav-link .icon { color: var(--muted); }
.nav-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-count { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.nav-heading { display: flex; align-items: center; justify-content: space-between; margin-top: 14px; padding-left: 10px; color: var(--muted); font-size: 13px; font-weight: 650; letter-spacing: .03em; text-transform: uppercase; }
.nav-empty { padding: 4px 10px 8px; color: var(--muted); font-size: 14px; }
.nav-sep { margin: 10px; border-top: 1px solid var(--line); }
.folder-row { display: flex; align-items: center; }
.folder-row .nav-link { flex: 1; min-width: 0; }
.folder-row.is-child { padding-left: 32px; }
.account { margin-top: 14px; padding-top: 10px; border-top: 1px solid var(--line); }
.quota, .account-email { padding: 2px 10px; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-email { padding-bottom: 6px; }

.topbar { position: sticky; top: var(--safe-top); z-index: 10; display: flex; align-items: center; gap: 6px; padding-block: 8px; background: var(--bg); }
.search { flex: 1; min-width: 0; display: flex; align-items: center; gap: 8px; height: 44px; padding: 0 14px; background: var(--surface); border: 1px solid var(--line); border-radius: 22px; color: var(--muted); }
.search:focus-within { border-color: var(--accent); }
.search-input { flex: 1; min-width: 0; height: 100%; background: none; border: 0; outline: 0; color: var(--ink); }
.view-title { margin: 6px 2px 12px; font-size: 21px; font-weight: 650; overflow-wrap: anywhere; }

/* Cartes */
.notes-grid { columns: 1; column-gap: 12px; }
.card { position: relative; margin-bottom: 12px; overflow: hidden; break-inside: avoid; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); }
.card.is-pinned { border-color: var(--accent); }
.card-link { display: block; padding-bottom: 12px; color: inherit; text-decoration: none; }
.card-cover { position: relative; margin-bottom: 10px; }
.card-cover img { width: 100%; aspect-ratio: 16 / 10; object-fit: cover; background: var(--surface-2); }
.card-count { position: absolute; right: 8px; bottom: 8px; padding: 1px 8px; border-radius: 10px; background: rgb(0 0 0 / .65); color: #fff; font-size: 13px; }
.card-title { margin: 12px 44px 4px 14px; font-size: 16px; font-weight: 650; overflow-wrap: anywhere; }
.card-cover + .card-title { margin-top: 0; }
.card-text { display: -webkit-box; margin: 12px 44px 0 14px; overflow: hidden; color: var(--ink); white-space: pre-wrap; overflow-wrap: anywhere; font-size: 15px; -webkit-box-orient: vertical; -webkit-line-clamp: 8; line-clamp: 8; }
.card-title + .card-text { margin-top: 0; }
.card-cover ~ .card-text { margin-right: 14px; }
.card-empty { color: var(--muted); font-style: italic; }
.card-folder { margin: 8px 14px 0; color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.card-pin { position: absolute; top: 2px; right: 2px; }
.card-cover ~ .card-pin, .card-link:has(.card-cover) + .card-pin { background: rgb(255 255 255 / .85); color: #3b3b3b; }
.card.is-pinned .card-pin { color: var(--accent); }
.card.is-pinned .card-pin .icon { fill: currentColor; }
.card.is-pinned .card-link:has(.card-cover) + .card-pin { color: #a8590f; }
.card-actions { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 12px 12px; }
.card-actions .btn { flex: 1; }
.empty { padding: 48px 16px; color: var(--muted); text-align: center; }
.more { display: flex; width: 100%; margin-top: 6px; }

.fab {
  position: fixed; right: 18px; bottom: calc(18px + var(--safe-bottom)); z-index: 10;
  display: grid; place-items: center;
  width: 60px; height: 60px; border-radius: 50%;
  background: var(--accent); color: var(--accent-ink);
  box-shadow: 0 3px 10px rgb(0 0 0 / .28);
}
.fab .icon { width: 28px; height: 28px; stroke-width: 2.2; }

/* Éditeur */
.editor-overlay { position: fixed; inset: 0; z-index: 40; display: flex; background: var(--surface); }
.editor { display: flex; flex-direction: column; width: 100%; height: 100dvh; padding-top: var(--safe-top); background: var(--surface); }
.editor-bar { display: flex; align-items: center; gap: 2px; padding: 4px 6px; border-bottom: 1px solid var(--line); }
.editor-status { flex: 1; min-width: 0; padding: 0 6px; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.editor-status.is-error { color: var(--danger); font-weight: 600; }
.editor-scroll { flex: 1; overflow-y: auto; padding: 14px 16px calc(24px + var(--safe-bottom)); }
.editor-title { width: 100%; margin-bottom: 8px; padding: 4px 0; background: none; border: 0; outline: 0; font-size: 20px; font-weight: 650; }
.editor-body { display: block; width: 100%; min-height: 40dvh; padding: 4px 0; overflow: hidden; resize: none; background: none; border: 0; outline: 0; line-height: 1.5; }
.editor-folder-row { display: flex; align-items: center; gap: 8px; margin-top: 16px; padding-top: 10px; border-top: 1px solid var(--line); color: var(--muted); }
.editor-folder { flex: 1; min-width: 0; height: 44px; padding: 0 8px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; color: var(--ink); }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; margin-bottom: 14px; }
.gallery-item { padding: 0; overflow: hidden; background: var(--surface-2); border: 0; border-radius: 10px; cursor: zoom-in; }
.gallery-item img { width: 100%; aspect-ratio: 1; object-fit: cover; }
.conflict { padding: 10px 16px; background: var(--accent-soft); }
.conflict-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.conflict-actions .btn { flex: 1; }

/* Boîtes de dialogue */
.dialog { width: min(92vw, 420px); padding: 0; background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-radius: 16px; }
.dialog::backdrop { background: rgb(0 0 0 / .5); }
.dialog-body { display: flex; flex-direction: column; gap: 12px; padding: 18px; }
.dialog-body h2 { font-size: 18px; overflow-wrap: anywhere; }
.dialog-body input { width: 100%; height: 46px; padding: 0 12px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.dialog-choices { display: flex; flex-direction: column; gap: 8px; }
.dialog:has(.lightbox) { width: 96vw; max-width: 96vw; max-height: 96dvh; background: #111; border: 0; }
.lightbox { display: flex; flex-direction: column; gap: 10px; padding: 10px; }
.lightbox img { max-height: calc(96dvh - 86px); margin: auto; object-fit: contain; }
.lightbox-actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; }

.toast { position: fixed; left: 50%; bottom: calc(92px + var(--safe-bottom)); z-index: 60; width: max-content; max-width: calc(100vw - 32px); padding: 10px 16px; transform: translateX(-50%); background: var(--ink); color: var(--bg); border-radius: 10px; box-shadow: 0 3px 12px rgb(0 0 0 / .3); }
.toast-error { background: var(--danger); color: var(--surface); }

/* Compte */
.auth-page { display: grid; place-items: start center; min-height: 100dvh; padding: calc(24px + var(--safe-top)) 16px calc(24px + var(--safe-bottom)); }
.auth-card { display: flex; flex-direction: column; gap: 14px; width: min(100%, 420px); padding: 22px; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; box-shadow: var(--shadow); }
.auth-card h1 { font-size: 22px; }
.auth-brand { display: flex; align-items: center; gap: 10px; font-weight: 650; }
.auth-brand img { width: 30px; height: 30px; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-section { padding-top: 14px; border-top: 1px solid var(--line); }
.auth-section h2 { margin-bottom: 10px; font-size: 16px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 14px; font-weight: 550; }
.field input { width: 100%; height: 46px; padding: 0 12px; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; }
.field input:focus { border-color: var(--accent); outline: 0; }
.msg { font-size: 14px; }
.msg-error { color: var(--danger); }
.msg-success { color: #2e7d32; }
.msg-info { color: var(--muted); }
.auth-links { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 10px; font-size: 15px; }
.auth-links a { padding-block: 8px; color: var(--accent); }
.auth-qr { margin: 0; padding: 10px; width: fit-content; background: #fff; border-radius: 10px; }
.auth-secret { overflow-wrap: anywhere; }
.auth-secrets { display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px; margin: 0; padding: 0; list-style: none; font-family: ui-monospace, monospace; }
.auth-loading { color: var(--muted); }

/* Grands écrans */
@media (min-width: 620px) {
  .notes-grid { columns: 2; }
}
@media (min-width: 900px) {
  .sidebar { position: sticky; top: 0; flex: none; width: 270px; height: 100dvh; transform: none; transition: none; }
  .backdrop, .menu-btn { display: none; }
  .main { padding-inline: 28px; }
  .topbar { max-width: 640px; }
  .notes-grid { columns: 3; }
  .editor-overlay { align-items: center; justify-content: center; padding: 24px; background: rgb(0 0 0 / .45); }
  .editor { max-width: 760px; height: min(86dvh, 900px); padding-top: 0; border-radius: 16px; overflow: hidden; }
  .folder-more { opacity: .55; }
  .folder-row:hover .folder-more { opacity: 1; }
}
@media (min-width: 1300px) {
  .notes-grid { columns: 4; }
}
